1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 29 * All Rights Reserved 30 */ 31 32 #pragma ident "%Z%%M% %I% %E% SMI" 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/systm.h> 37 #include <sys/cred.h> 38 #include <sys/time.h> 39 #include <sys/vnode.h> 40 #include <sys/vfs.h> 41 #include <sys/file.h> 42 #include <sys/filio.h> 43 #include <sys/uio.h> 44 #include <sys/buf.h> 45 #include <sys/mman.h> 46 #include <sys/pathname.h> 47 #include <sys/dirent.h> 48 #include <sys/debug.h> 49 #include <sys/vmsystm.h> 50 #include <sys/fcntl.h> 51 #include <sys/flock.h> 52 #include <sys/swap.h> 53 #include <sys/errno.h> 54 #include <sys/strsubr.h> 55 #include <sys/sysmacros.h> 56 #include <sys/kmem.h> 57 #include <sys/cmn_err.h> 58 #include <sys/pathconf.h> 59 #include <sys/utsname.h> 60 #include <sys/dnlc.h> 61 #include <sys/acl.h> 62 #include <sys/systeminfo.h> 63 #include <sys/policy.h> 64 #include <sys/sdt.h> 65 #include <sys/list.h> 66 #include <sys/stat.h> 67 68 #include <rpc/types.h> 69 #include <rpc/auth.h> 70 #include <rpc/clnt.h> 71 72 #include <nfs/nfs.h> 73 #include <nfs/nfs_clnt.h> 74 #include <nfs/nfs_acl.h> 75 #include <nfs/lm.h> 76 #include <nfs/nfs4.h> 77 #include <nfs/nfs4_kprot.h> 78 #include <nfs/rnode4.h> 79 #include <nfs/nfs4_clnt.h> 80 81 #include <vm/hat.h> 82 #include <vm/as.h> 83 #include <vm/page.h> 84 #include <vm/pvn.h> 85 #include <vm/seg.h> 86 #include <vm/seg_map.h> 87 #include <vm/seg_kpm.h> 88 #include <vm/seg_vn.h> 89 90 #include <fs/fs_subr.h> 91 92 #include <sys/ddi.h> 93 #include <sys/int_fmtio.h> 94 95 typedef struct { 96 nfs4_ga_res_t *di_garp; 97 cred_t *di_cred; 98 hrtime_t di_time_call; 99 } dirattr_info_t; 100 101 typedef enum nfs4_acl_op { 102 NFS4_ACL_GET, 103 NFS4_ACL_SET 104 } nfs4_acl_op_t; 105 106 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 107 char *, dirattr_info_t *); 108 109 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 110 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 111 nfs4_error_t *, int *); 112 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 113 cred_t *); 114 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 115 stable_how4 *); 116 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 117 cred_t *, bool_t, struct uio *); 118 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 119 vsecattr_t *); 120 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 121 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 122 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 123 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 124 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 125 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 126 int, vnode_t **, cred_t *); 127 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 128 cred_t *, int, int, enum createmode4, int); 129 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 130 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 131 vnode_t *, char *, cred_t *, nfsstat4 *); 132 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 133 vnode_t *, char *, cred_t *, nfsstat4 *); 134 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 135 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 136 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 137 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 138 page_t *[], size_t, struct seg *, caddr_t, 139 enum seg_rw, cred_t *); 140 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 141 cred_t *); 142 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 143 int, cred_t *); 144 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 145 int, cred_t *); 146 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 147 static void nfs4_set_mod(vnode_t *); 148 static void nfs4_get_commit(vnode_t *); 149 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 150 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 151 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 152 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 153 cred_t *); 154 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 155 cred_t *); 156 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 157 hrtime_t, vnode_t *, cred_t *); 158 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 159 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 160 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 161 u_offset_t); 162 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 163 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 164 static cred_t *state_to_cred(nfs4_open_stream_t *); 165 static int vtoname(vnode_t *, char *, ssize_t); 166 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 167 static pid_t lo_to_pid(lock_owner4 *); 168 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 169 cred_t *, nfs4_lock_owner_t *); 170 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 171 nfs4_lock_owner_t *); 172 static nfs4_open_stream_t *open_and_get_osp(vnode_t *, cred_t *, mntinfo4_t *); 173 static void nfs4_delmap_callback(struct as *, void *, uint_t); 174 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 175 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 176 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 177 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 178 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 179 uid_t, gid_t, int); 180 181 /* 182 * Routines that implement the setting of v4 args for the misc. ops 183 */ 184 static void nfs4args_lock_free(nfs_argop4 *); 185 static void nfs4args_lockt_free(nfs_argop4 *); 186 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 187 int, rnode4_t *, cred_t *, bitmap4, int *, 188 nfs4_stateid_types_t *); 189 static void nfs4args_setattr_free(nfs_argop4 *); 190 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 191 bitmap4); 192 static void nfs4args_verify_free(nfs_argop4 *); 193 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 194 WRITE4args **, nfs4_stateid_types_t *); 195 196 /* 197 * These are the vnode ops functions that implement the vnode interface to 198 * the networked file system. See more comments below at nfs4_vnodeops. 199 */ 200 static int nfs4_open(vnode_t **, int, cred_t *); 201 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *); 202 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 203 caller_context_t *); 204 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 205 caller_context_t *); 206 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *); 207 static int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *); 208 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 209 caller_context_t *); 210 static int nfs4_access(vnode_t *, int, int, cred_t *); 211 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *); 212 static int nfs4_fsync(vnode_t *, int, cred_t *); 213 static void nfs4_inactive(vnode_t *, cred_t *); 214 static int nfs4_lookup(vnode_t *, char *, vnode_t **, 215 struct pathname *, int, vnode_t *, cred_t *); 216 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 217 int, vnode_t **, cred_t *, int); 218 static int nfs4_remove(vnode_t *, char *, cred_t *); 219 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *); 220 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 221 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, 222 vnode_t **, cred_t *); 223 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *); 224 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 225 cred_t *); 226 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *); 227 static int nfs4_fid(vnode_t *, fid_t *); 228 static int nfs4_rwlock(vnode_t *, int, caller_context_t *); 229 static void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 230 static int nfs4_seek(vnode_t *, offset_t, offset_t *); 231 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 232 page_t *[], size_t, struct seg *, caddr_t, 233 enum seg_rw, cred_t *); 234 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *); 235 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, 236 size_t, uchar_t, uchar_t, uint_t, cred_t *); 237 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, 238 size_t, uchar_t, uchar_t, uint_t, cred_t *); 239 static int nfs4_cmp(vnode_t *, vnode_t *); 240 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 241 struct flk_callback *, cred_t *); 242 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 243 cred_t *, caller_context_t *); 244 static int nfs4_realvp(vnode_t *, vnode_t **); 245 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, 246 size_t, uint_t, uint_t, uint_t, cred_t *); 247 static int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *); 248 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 249 cred_t *); 250 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *); 251 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 252 static int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 253 static int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *); 254 255 /* 256 * Used for nfs4_commit_vp() to indicate if we should 257 * wait on pending writes. 258 */ 259 #define NFS4_WRITE_NOWAIT 0 260 #define NFS4_WRITE_WAIT 1 261 262 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */ 263 264 /* 265 * Error flags used to pass information about certain special errors 266 * which need to be handled specially. 267 */ 268 #define NFS_EOF -98 269 #define NFS_VERF_MISMATCH -97 270 271 /* 272 * Flags used to differentiate between which operation drove the 273 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 274 */ 275 #define NFS4_CLOSE_OP 0x1 276 #define NFS4_DELMAP_OP 0x2 277 #define NFS4_INACTIVE_OP 0x3 278 279 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 280 281 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 282 #define ALIGN64(x, ptr, sz) \ 283 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 284 if (x) { \ 285 x = sizeof (uint64_t) - (x); \ 286 sz -= (x); \ 287 ptr += (x); \ 288 } 289 290 #ifdef DEBUG 291 int nfs4_client_attr_debug = 0; 292 int nfs4_client_state_debug = 0; 293 int nfs4_client_shadow_debug = 0; 294 int nfs4_client_lock_debug = 0; 295 int nfs4_seqid_sync = 0; 296 int nfs4_client_map_debug = 0; 297 static int nfs4_pageio_debug = 0; 298 int nfs4_client_inactive_debug = 0; 299 int nfs4_client_recov_debug = 0; 300 int nfs4_client_recov_stub_debug = 0; 301 int nfs4_client_failover_debug = 0; 302 int nfs4_client_call_debug = 0; 303 int nfs4_client_lookup_debug = 0; 304 int nfs4_client_zone_debug = 0; 305 int nfs4_lost_rqst_debug = 0; 306 int nfs4_rdattrerr_debug = 0; 307 int nfs4_open_stream_debug = 0; 308 309 int nfs4read_error_inject; 310 311 static int nfs4_create_misses = 0; 312 313 static int nfs4_readdir_cache_shorts = 0; 314 static int nfs4_readdir_readahead = 0; 315 316 static int nfs4_bio_do_stop = 0; 317 318 static int nfs4_lostpage = 0; /* number of times we lost original page */ 319 320 int nfs4_mmap_debug = 0; 321 322 static int nfs4_pathconf_cache_hits = 0; 323 static int nfs4_pathconf_cache_misses = 0; 324 325 int nfs4close_all_cnt; 326 int nfs4close_one_debug = 0; 327 int nfs4close_notw_debug = 0; 328 329 int denied_to_flk_debug = 0; 330 void *lockt_denied_debug; 331 332 #endif 333 334 /* 335 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 336 * or NFS4ERR_RESOURCE. 337 */ 338 static int confirm_retry_sec = 30; 339 340 static int nfs4_lookup_neg_cache = 1; 341 342 /* 343 * number of pages to read ahead 344 * optimized for 100 base-T. 345 */ 346 static int nfs4_nra = 4; 347 348 static int nfs4_do_symlink_cache = 1; 349 350 static int nfs4_pathconf_disable_cache = 0; 351 352 /* 353 * These are the vnode ops routines which implement the vnode interface to 354 * the networked file system. These routines just take their parameters, 355 * make them look networkish by putting the right info into interface structs, 356 * and then calling the appropriate remote routine(s) to do the work. 357 * 358 * Note on directory name lookup cacheing: If we detect a stale fhandle, 359 * we purge the directory cache relative to that vnode. This way, the 360 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 361 * more details on rnode locking. 362 */ 363 364 struct vnodeops *nfs4_vnodeops; 365 366 const fs_operation_def_t nfs4_vnodeops_template[] = { 367 VOPNAME_OPEN, nfs4_open, 368 VOPNAME_CLOSE, nfs4_close, 369 VOPNAME_READ, nfs4_read, 370 VOPNAME_WRITE, nfs4_write, 371 VOPNAME_IOCTL, nfs4_ioctl, 372 VOPNAME_GETATTR, nfs4_getattr, 373 VOPNAME_SETATTR, nfs4_setattr, 374 VOPNAME_ACCESS, nfs4_access, 375 VOPNAME_LOOKUP, nfs4_lookup, 376 VOPNAME_CREATE, nfs4_create, 377 VOPNAME_REMOVE, nfs4_remove, 378 VOPNAME_LINK, nfs4_link, 379 VOPNAME_RENAME, nfs4_rename, 380 VOPNAME_MKDIR, nfs4_mkdir, 381 VOPNAME_RMDIR, nfs4_rmdir, 382 VOPNAME_READDIR, nfs4_readdir, 383 VOPNAME_SYMLINK, nfs4_symlink, 384 VOPNAME_READLINK, nfs4_readlink, 385 VOPNAME_FSYNC, nfs4_fsync, 386 VOPNAME_INACTIVE, (fs_generic_func_p) nfs4_inactive, 387 VOPNAME_FID, nfs4_fid, 388 VOPNAME_RWLOCK, nfs4_rwlock, 389 VOPNAME_RWUNLOCK, (fs_generic_func_p) nfs4_rwunlock, 390 VOPNAME_SEEK, nfs4_seek, 391 VOPNAME_FRLOCK, nfs4_frlock, 392 VOPNAME_SPACE, nfs4_space, 393 VOPNAME_REALVP, nfs4_realvp, 394 VOPNAME_GETPAGE, nfs4_getpage, 395 VOPNAME_PUTPAGE, nfs4_putpage, 396 VOPNAME_MAP, (fs_generic_func_p) nfs4_map, 397 VOPNAME_ADDMAP, (fs_generic_func_p) nfs4_addmap, 398 VOPNAME_DELMAP, nfs4_delmap, 399 VOPNAME_DUMP, nfs_dump, /* there is no separate nfs4_dump */ 400 VOPNAME_PATHCONF, nfs4_pathconf, 401 VOPNAME_PAGEIO, nfs4_pageio, 402 VOPNAME_DISPOSE, (fs_generic_func_p) nfs4_dispose, 403 VOPNAME_SETSECATTR, nfs4_setsecattr, 404 VOPNAME_GETSECATTR, nfs4_getsecattr, 405 VOPNAME_SHRLOCK, nfs4_shrlock, 406 NULL, NULL 407 }; 408 409 /* 410 * The following are subroutines and definitions to set args or get res 411 * for the different nfsv4 ops 412 */ 413 414 void 415 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 416 { 417 int i; 418 419 for (i = 0; i < arglen; i++) { 420 if (argop[i].argop == OP_LOOKUP) 421 kmem_free( 422 argop[i].nfs_argop4_u.oplookup.objname.utf8string_val, 423 argop[i].nfs_argop4_u.oplookup.objname.utf8string_len); 424 } 425 } 426 427 static void 428 nfs4args_lock_free(nfs_argop4 *argop) 429 { 430 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 431 432 if (locker->new_lock_owner == TRUE) { 433 open_to_lock_owner4 *open_owner; 434 435 open_owner = &locker->locker4_u.open_owner; 436 if (open_owner->lock_owner.owner_val != NULL) { 437 kmem_free(open_owner->lock_owner.owner_val, 438 open_owner->lock_owner.owner_len); 439 } 440 } 441 } 442 443 static void 444 nfs4args_lockt_free(nfs_argop4 *argop) 445 { 446 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 447 448 if (lowner->owner_val != NULL) { 449 kmem_free(lowner->owner_val, lowner->owner_len); 450 } 451 } 452 453 static void 454 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 455 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 456 nfs4_stateid_types_t *sid_types) 457 { 458 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 459 mntinfo4_t *mi; 460 461 argop->argop = OP_SETATTR; 462 /* 463 * The stateid is set to 0 if client is not modifying the size 464 * and otherwise to whatever nfs4_get_stateid() returns. 465 * 466 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 467 * state struct could be found for the process/file pair. We may 468 * want to change this in the future (by OPENing the file). See 469 * bug # 4474852. 470 */ 471 if (vap->va_mask & AT_SIZE) { 472 473 ASSERT(rp != NULL); 474 mi = VTOMI4(RTOV4(rp)); 475 476 argop->nfs_argop4_u.opsetattr.stateid = 477 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 478 OP_SETATTR, sid_types, FALSE); 479 } else { 480 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 481 sizeof (stateid4)); 482 } 483 484 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 485 if (*error) 486 bzero(attr, sizeof (*attr)); 487 } 488 489 static void 490 nfs4args_setattr_free(nfs_argop4 *argop) 491 { 492 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 493 } 494 495 static int 496 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 497 bitmap4 supp) 498 { 499 fattr4 *attr; 500 int error = 0; 501 502 argop->argop = op; 503 switch (op) { 504 case OP_VERIFY: 505 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 506 break; 507 case OP_NVERIFY: 508 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 509 break; 510 default: 511 return (EINVAL); 512 /*NOTREACHED*/ 513 break; 514 } 515 if (!error) 516 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 517 if (error) 518 bzero(attr, sizeof (*attr)); 519 return (error); 520 } 521 522 static void 523 nfs4args_verify_free(nfs_argop4 *argop) 524 { 525 switch (argop->argop) { 526 case OP_VERIFY: 527 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 528 break; 529 case OP_NVERIFY: 530 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 531 break; 532 default: 533 break; 534 } 535 } 536 537 static void 538 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 539 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 540 { 541 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 542 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 543 544 argop->argop = OP_WRITE; 545 wargs->stable = stable; 546 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 547 mi, OP_WRITE, sid_tp); 548 wargs->mblk = NULL; 549 *wargs_pp = wargs; 550 } 551 552 void 553 nfs4args_copen_free(OPEN4cargs *open_args) 554 { 555 if (open_args->owner.owner_val) { 556 kmem_free(open_args->owner.owner_val, 557 open_args->owner.owner_len); 558 } 559 if ((open_args->opentype == OPEN4_CREATE) && 560 (open_args->mode != EXCLUSIVE4)) { 561 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 562 } 563 } 564 565 /* 566 * XXX: This is referenced in modstubs.s 567 */ 568 struct vnodeops * 569 nfs4_getvnodeops(void) 570 { 571 return (nfs4_vnodeops); 572 } 573 574 /* 575 * The OPEN operation opens a regular file. 576 * 577 * ARGSUSED 578 */ 579 static int 580 nfs4_open(vnode_t **vpp, int flag, cred_t *cr) 581 { 582 vnode_t *dvp = NULL; 583 rnode4_t *rp; 584 int error; 585 int just_been_created; 586 char fn[MAXNAMELEN]; 587 588 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 589 if (curproc->p_zone != VTOMI4(*vpp)->mi_zone) 590 return (EIO); 591 rp = VTOR4(*vpp); 592 593 /* 594 * Check to see if opening something besides a regular file; 595 * if so skip the OTW call 596 */ 597 if ((*vpp)->v_type != VREG) { 598 error = nfs4_open_non_reg_file(vpp, flag, cr); 599 return (error); 600 } 601 602 /* 603 * XXX - would like a check right here to know if the file is 604 * executable or not, so as to skip OTW 605 */ 606 607 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) 608 return (error); 609 610 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 611 return (error); 612 613 /* 614 * See if this file has just been CREATEd. 615 * If so, clear the flag and update the dnlc, which was previously 616 * skipped in nfs4_create. 617 * XXX need better serilization on this. 618 * XXX move this into the nf4open_otw call, after we have 619 * XXX acquired the open owner seqid sync. 620 */ 621 mutex_enter(&rp->r_statev4_lock); 622 if (rp->created_v4) { 623 rp->created_v4 = 0; 624 mutex_exit(&rp->r_statev4_lock); 625 626 dnlc_update(dvp, fn, *vpp); 627 /* This is needed so we don't bump the open ref count */ 628 just_been_created = 1; 629 } else { 630 mutex_exit(&rp->r_statev4_lock); 631 just_been_created = 0; 632 } 633 634 /* 635 * If caller specified O_TRUNC/FTRUNC, then be sure to set 636 * FWRITE (to drive successful setattr(size=0) after open) 637 */ 638 if (flag & FTRUNC) 639 flag |= FWRITE; 640 641 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 642 just_been_created); 643 644 if (!error && !((*vpp)->v_flag & VROOT)) 645 dnlc_update(dvp, fn, *vpp); 646 647 /* release the hold from vtodv */ 648 VN_RELE(dvp); 649 650 /* exchange the shadow for the master vnode, if needed */ 651 652 if (error == 0 && IS_SHADOW(*vpp, rp)) 653 sv_exchange(vpp); 654 655 return (error); 656 } 657 658 /* 659 * See if there's a "lost open" request to be saved and recovered. 660 */ 661 static void 662 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 663 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 664 vnode_t *dvp, OPEN4cargs *open_args) 665 { 666 vfs_t *vfsp; 667 char *srccfp; 668 669 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 670 671 if (error != ETIMEDOUT && error != EINTR && 672 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 673 lost_rqstp->lr_op = 0; 674 return; 675 } 676 677 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 678 "nfs4open_save_lost_rqst: error %d", error)); 679 680 lost_rqstp->lr_op = OP_OPEN; 681 /* 682 * The vp (if it is not NULL) and dvp are held and rele'd via 683 * the recovery code. See nfs4_save_lost_rqst. 684 */ 685 lost_rqstp->lr_vp = vp; 686 lost_rqstp->lr_dvp = dvp; 687 lost_rqstp->lr_oop = oop; 688 lost_rqstp->lr_osp = NULL; 689 lost_rqstp->lr_lop = NULL; 690 lost_rqstp->lr_cr = cr; 691 lost_rqstp->lr_flk = NULL; 692 lost_rqstp->lr_oacc = open_args->share_access; 693 lost_rqstp->lr_odeny = open_args->share_deny; 694 lost_rqstp->lr_oclaim = open_args->claim; 695 if (open_args->claim == CLAIM_DELEGATE_CUR) { 696 lost_rqstp->lr_ostateid = 697 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 698 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 699 } else { 700 srccfp = open_args->open_claim4_u.cfile; 701 } 702 lost_rqstp->lr_ofile.utf8string_len = 0; 703 lost_rqstp->lr_ofile.utf8string_val = NULL; 704 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 705 lost_rqstp->lr_putfirst = FALSE; 706 } 707 708 struct nfs4_excl_time { 709 uint32 seconds; 710 uint32 nseconds; 711 }; 712 713 /* 714 * The OPEN operation creates and/or opens a regular file 715 * 716 * ARGSUSED 717 */ 718 static int 719 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 720 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 721 enum createmode4 createmode, int file_just_been_created) 722 { 723 rnode4_t *rp; 724 rnode4_t *drp = VTOR4(dvp); 725 vnode_t *vp = NULL; 726 vnode_t *vpi = *vpp; 727 bool_t needrecov = FALSE; 728 729 int doqueue = 1; 730 731 COMPOUND4args_clnt args; 732 COMPOUND4res_clnt res; 733 nfs_argop4 *argop; 734 nfs_resop4 *resop; 735 int argoplist_size; 736 int idx_open, idx_fattr; 737 738 GETFH4res *gf_res = NULL; 739 OPEN4res *op_res = NULL; 740 nfs4_ga_res_t *garp; 741 fattr4 *attr = NULL; 742 struct nfs4_excl_time verf; 743 bool_t did_excl_setup = FALSE; 744 int created_osp; 745 746 OPEN4cargs *open_args; 747 nfs4_open_owner_t *oop = NULL; 748 nfs4_open_stream_t *osp = NULL; 749 seqid4 seqid = 0; 750 bool_t retry_open = FALSE; 751 nfs4_recov_state_t recov_state; 752 nfs4_lost_rqst_t lost_rqst; 753 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 754 hrtime_t t; 755 int acc = 0; 756 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 757 cred_t *ncr = NULL; 758 759 nfs4_sharedfh_t *otw_sfh; 760 nfs4_sharedfh_t *orig_sfh; 761 int fh_differs = 0; 762 int numops, setgid_flag; 763 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 764 765 /* 766 * Make sure we properly deal with setting the right gid on 767 * a newly created file to reflect the parent's setgid bit 768 */ 769 setgid_flag = 0; 770 if (create_flag && in_va) { 771 772 /* 773 * If the parent's directory has the setgid bit set 774 * _and_ the client was able to get a valid mapping 775 * for the parent dir's owner_group, we want to 776 * append NVERIFY(owner_group == dva.va_gid) and 777 * SETATTR to the CREATE compound. 778 */ 779 mutex_enter(&drp->r_statelock); 780 if (drp->r_attr.va_mode & VSGID && 781 drp->r_attr.va_gid != GID_NOBODY) { 782 in_va->va_gid = drp->r_attr.va_gid; 783 setgid_flag = 1; 784 } 785 mutex_exit(&drp->r_statelock); 786 } 787 788 /* 789 * Normal/non-create compound: 790 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 791 * 792 * Open(create) compound no setgid: 793 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 794 * RESTOREFH + GETATTR 795 * 796 * Open(create) setgid: 797 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 798 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 799 * NVERIFY(grp) + SETATTR 800 */ 801 if (setgid_flag) { 802 numops = 10; 803 idx_open = 1; 804 idx_fattr = 3; 805 } else if (create_flag) { 806 numops = 7; 807 idx_open = 2; 808 idx_fattr = 4; 809 } else { 810 numops = 4; 811 idx_open = 1; 812 idx_fattr = 3; 813 } 814 815 args.array_len = numops; 816 argoplist_size = numops * sizeof (nfs_argop4); 817 argop = kmem_alloc(argoplist_size, KM_SLEEP); 818 819 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 820 "open %s open flag 0x%x cred %p", file_name, open_flag, 821 (void *)cr)); 822 823 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 824 if (create_flag) { 825 /* 826 * We are to create a file. Initialize the passed in vnode 827 * pointer. 828 */ 829 vpi = NULL; 830 } else { 831 /* 832 * Check to see if the client owns a read delegation and is 833 * trying to open for write. If so, then return the delegation 834 * to avoid the server doing a cb_recall and returning DELAY. 835 * NB - we don't use the statev4_lock here because we'd have 836 * to drop the lock anyway and the result would be stale. 837 */ 838 if ((open_flag & FWRITE) && 839 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 840 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 841 842 /* 843 * If the file has a delegation, then do an access check up 844 * front. This avoids having to an access check later after 845 * we've already done start_op, which could deadlock. 846 */ 847 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 848 if (open_flag & FREAD && 849 nfs4_access(vpi, VREAD, 0, cr) == 0) 850 acc |= VREAD; 851 if (open_flag & FWRITE && 852 nfs4_access(vpi, VWRITE, 0, cr) == 0) 853 acc |= VWRITE; 854 } 855 } 856 857 drp = VTOR4(dvp); 858 859 recov_state.rs_flags = 0; 860 recov_state.rs_num_retry_despite_err = 0; 861 cred_otw = cr; 862 863 recov_retry: 864 fh_differs = 0; 865 nfs4_error_zinit(&e); 866 867 /* argop is empty here */ 868 869 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 870 if (ncr != NULL) 871 crfree(ncr); 872 kmem_free(argop, argoplist_size); 873 return (EINTR); 874 } 875 876 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 877 if (e.error) { 878 nfs_rw_exit(&drp->r_rwlock); 879 if (ncr != NULL) 880 crfree(ncr); 881 kmem_free(argop, argoplist_size); 882 return (e.error); 883 } 884 885 args.ctag = TAG_OPEN; 886 args.array_len = numops; 887 args.array = argop; 888 889 /* putfh directory fh */ 890 argop[0].argop = OP_CPUTFH; 891 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 892 893 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 894 argop[idx_open].argop = OP_COPEN; 895 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 896 open_args->claim = CLAIM_NULL; 897 898 /* name of file */ 899 open_args->open_claim4_u.cfile = file_name; 900 open_args->owner.owner_len = 0; 901 open_args->owner.owner_val = NULL; 902 903 if (create_flag) { 904 /* CREATE a file */ 905 open_args->opentype = OPEN4_CREATE; 906 open_args->mode = createmode; 907 if (createmode == EXCLUSIVE4) { 908 if (did_excl_setup == FALSE) { 909 verf.seconds = nfs_atoi(hw_serial); 910 if (verf.seconds != 0) 911 verf.nseconds = newnum(); 912 else { 913 timestruc_t now; 914 915 gethrestime(&now); 916 verf.seconds = now.tv_sec; 917 verf.nseconds = now.tv_nsec; 918 } 919 /* 920 * Since the server will use this value for the 921 * mtime, make sure that it can't overflow. Zero 922 * out the MSB. The actual value does not matter 923 * here, only its uniqeness. 924 */ 925 verf.seconds &= INT32_MAX; 926 did_excl_setup = TRUE; 927 } 928 929 /* Now copy over verifier to OPEN4args. */ 930 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 931 } else { 932 int v_error; 933 bitmap4 supp_attrs; 934 servinfo4_t *svp; 935 936 attr = &open_args->createhow4_u.createattrs; 937 938 svp = drp->r_server; 939 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 940 supp_attrs = svp->sv_supp_attrs; 941 nfs_rw_exit(&svp->sv_lock); 942 943 /* GUARDED4 or UNCHECKED4 */ 944 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 945 supp_attrs); 946 if (v_error) { 947 bzero(attr, sizeof (*attr)); 948 nfs4args_copen_free(open_args); 949 nfs_rw_exit(&drp->r_rwlock); 950 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 951 &recov_state, FALSE); 952 if (ncr != NULL) 953 crfree(ncr); 954 kmem_free(argop, argoplist_size); 955 return (v_error); 956 } 957 } 958 } else { 959 /* NO CREATE */ 960 open_args->opentype = OPEN4_NOCREATE; 961 } 962 963 if (recov_state.rs_sp != NULL) { 964 mutex_enter(&recov_state.rs_sp->s_lock); 965 open_args->owner.clientid = recov_state.rs_sp->clientid; 966 mutex_exit(&recov_state.rs_sp->s_lock); 967 } else { 968 /* XXX should we just fail here? */ 969 open_args->owner.clientid = 0; 970 } 971 972 /* 973 * This increments oop's ref count or creates a temporary 'just_created' 974 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 975 * completes. 976 */ 977 mutex_enter(&VTOMI4(dvp)->mi_lock); 978 979 /* See if a permanent or just created open owner exists */ 980 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 981 if (!oop) { 982 /* 983 * This open owner does not exist so create a temporary 984 * just created one. 985 */ 986 oop = create_open_owner(cr, VTOMI4(dvp)); 987 ASSERT(oop != NULL); 988 } 989 mutex_exit(&VTOMI4(dvp)->mi_lock); 990 991 /* this length never changes, do alloc before seqid sync */ 992 open_args->owner.owner_len = sizeof (oop->oo_name); 993 open_args->owner.owner_val = 994 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 995 996 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 997 if (e.error == EAGAIN) { 998 open_owner_rele(oop); 999 nfs4args_copen_free(open_args); 1000 nfs_rw_exit(&drp->r_rwlock); 1001 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1002 if (ncr != NULL) { 1003 crfree(ncr); 1004 ncr = NULL; 1005 } 1006 goto recov_retry; 1007 } 1008 1009 /* Check to see if we need to do the OTW call */ 1010 if (!create_flag) { 1011 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1012 file_just_been_created, &e.error, acc, &recov_state)) { 1013 1014 /* 1015 * The OTW open is not necessary. Either 1016 * the open can succeed without it (eg. 1017 * delegation, error == 0) or the open 1018 * must fail due to an access failure 1019 * (error != 0). In either case, tidy 1020 * up and return. 1021 */ 1022 1023 nfs4_end_open_seqid_sync(oop); 1024 open_owner_rele(oop); 1025 nfs4args_copen_free(open_args); 1026 nfs_rw_exit(&drp->r_rwlock); 1027 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1028 if (ncr != NULL) 1029 crfree(ncr); 1030 kmem_free(argop, argoplist_size); 1031 return (e.error); 1032 } 1033 } 1034 1035 bcopy(&oop->oo_name, open_args->owner.owner_val, 1036 open_args->owner.owner_len); 1037 1038 seqid = nfs4_get_open_seqid(oop) + 1; 1039 open_args->seqid = seqid; 1040 open_args->share_access = 0; 1041 if (open_flag & FREAD) 1042 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1043 if (open_flag & FWRITE) 1044 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1045 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1046 1047 1048 1049 /* 1050 * getfh w/sanity check for idx_open/idx_fattr 1051 */ 1052 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1053 argop[idx_open + 1].argop = OP_GETFH; 1054 1055 /* getattr */ 1056 argop[idx_fattr].argop = OP_GETATTR; 1057 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1058 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1059 1060 if (setgid_flag) { 1061 vattr_t _v; 1062 servinfo4_t *svp; 1063 bitmap4 supp_attrs; 1064 1065 svp = drp->r_server; 1066 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1067 supp_attrs = svp->sv_supp_attrs; 1068 nfs_rw_exit(&svp->sv_lock); 1069 1070 /* 1071 * For setgid case, we need to: 1072 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1073 */ 1074 argop[4].argop = OP_SAVEFH; 1075 1076 argop[5].argop = OP_CPUTFH; 1077 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1078 1079 argop[6].argop = OP_GETATTR; 1080 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1081 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1082 1083 argop[7].argop = OP_RESTOREFH; 1084 1085 /* 1086 * nverify 1087 */ 1088 _v.va_mask = AT_GID; 1089 _v.va_gid = in_va->va_gid; 1090 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1091 supp_attrs))) { 1092 1093 /* 1094 * setattr 1095 * 1096 * We _know_ we're not messing with AT_SIZE or 1097 * AT_XTIME, so no need for stateid or flags. 1098 * Also we specify NULL rp since we're only 1099 * interested in setting owner_group attributes. 1100 */ 1101 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1102 supp_attrs, &e.error, 0); 1103 if (e.error) 1104 nfs4args_verify_free(&argop[8]); 1105 } 1106 1107 if (e.error) { 1108 /* 1109 * XXX - Revisit the last argument to nfs4_end_op() 1110 * once 5020486 is fixed. 1111 */ 1112 nfs4_end_open_seqid_sync(oop); 1113 open_owner_rele(oop); 1114 nfs4args_copen_free(open_args); 1115 nfs_rw_exit(&drp->r_rwlock); 1116 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1117 if (ncr != NULL) 1118 crfree(ncr); 1119 kmem_free(argop, argoplist_size); 1120 return (e.error); 1121 } 1122 } else if (create_flag) { 1123 /* 1124 * For setgid case, we need to: 1125 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1126 */ 1127 argop[1].argop = OP_SAVEFH; 1128 1129 argop[5].argop = OP_RESTOREFH; 1130 1131 argop[6].argop = OP_GETATTR; 1132 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1133 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1134 } 1135 1136 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1137 "nfs4open_otw: %s call, nm %s, rp %s", 1138 needrecov ? "recov" : "first", file_name, 1139 rnode4info(VTOR4(dvp)))); 1140 1141 t = gethrtime(); 1142 1143 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1144 1145 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1146 nfs4_set_open_seqid(seqid, oop, args.ctag); 1147 1148 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1149 1150 if (e.error || needrecov) { 1151 bool_t abort = FALSE; 1152 1153 if (needrecov) { 1154 nfs4_bseqid_entry_t *bsep = NULL; 1155 1156 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1157 cred_otw, vpi, dvp, open_args); 1158 1159 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1160 bsep = nfs4_create_bseqid_entry(oop, NULL, 1161 vpi, 0, args.ctag, open_args->seqid); 1162 num_bseqid_retry--; 1163 } 1164 1165 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1166 NULL, lost_rqst.lr_op == OP_OPEN ? 1167 &lost_rqst : NULL, OP_OPEN, bsep); 1168 1169 if (bsep) 1170 kmem_free(bsep, sizeof (*bsep)); 1171 /* give up if we keep getting BAD_SEQID */ 1172 if (num_bseqid_retry == 0) 1173 abort = TRUE; 1174 if (abort == TRUE && e.error == 0) 1175 e.error = geterrno4(res.status); 1176 } 1177 nfs4_end_open_seqid_sync(oop); 1178 open_owner_rele(oop); 1179 nfs_rw_exit(&drp->r_rwlock); 1180 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1181 nfs4args_copen_free(open_args); 1182 if (setgid_flag) { 1183 nfs4args_verify_free(&argop[8]); 1184 nfs4args_setattr_free(&argop[9]); 1185 } 1186 if (!e.error) 1187 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1188 if (ncr != NULL) { 1189 crfree(ncr); 1190 ncr = NULL; 1191 } 1192 if (!needrecov || abort == TRUE || e.error == EINTR || 1193 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1194 kmem_free(argop, argoplist_size); 1195 return (e.error); 1196 } 1197 goto recov_retry; 1198 } 1199 1200 /* 1201 * Will check and update lease after checking the rflag for 1202 * OPEN_CONFIRM in the successful OPEN call. 1203 */ 1204 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1205 1206 /* 1207 * XXX what if we're crossing mount points from server1:/drp 1208 * to server2:/drp/rp. 1209 */ 1210 1211 /* Signal our end of use of the open seqid */ 1212 nfs4_end_open_seqid_sync(oop); 1213 1214 /* 1215 * This will destroy the open owner if it was just created, 1216 * and no one else has put a reference on it. 1217 */ 1218 open_owner_rele(oop); 1219 if (create_flag && (createmode != EXCLUSIVE4) && 1220 res.status == NFS4ERR_BADOWNER) 1221 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1222 1223 e.error = geterrno4(res.status); 1224 nfs4args_copen_free(open_args); 1225 if (setgid_flag) { 1226 nfs4args_verify_free(&argop[8]); 1227 nfs4args_setattr_free(&argop[9]); 1228 } 1229 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1230 nfs_rw_exit(&drp->r_rwlock); 1231 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1232 /* 1233 * If the reply is NFS4ERR_ACCESS, it may be because 1234 * we are root (no root net access). If the real uid 1235 * is not root, then retry with the real uid instead. 1236 */ 1237 if (ncr != NULL) { 1238 crfree(ncr); 1239 ncr = NULL; 1240 } 1241 if (res.status == NFS4ERR_ACCESS && 1242 (ncr = crnetadjust(cred_otw)) != NULL) { 1243 cred_otw = ncr; 1244 goto recov_retry; 1245 } 1246 kmem_free(argop, argoplist_size); 1247 return (e.error); 1248 } 1249 1250 resop = &res.array[idx_open]; /* open res */ 1251 op_res = &resop->nfs_resop4_u.opopen; 1252 1253 #ifdef DEBUG 1254 /* 1255 * verify attrset bitmap 1256 */ 1257 if (create_flag && 1258 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1259 /* make sure attrset returned is what we asked for */ 1260 /* XXX Ignore this 'error' for now */ 1261 if (attr->attrmask != op_res->attrset) 1262 /* EMPTY */; 1263 } 1264 #endif 1265 1266 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1267 mutex_enter(&VTOMI4(dvp)->mi_lock); 1268 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1269 mutex_exit(&VTOMI4(dvp)->mi_lock); 1270 } 1271 1272 resop = &res.array[idx_open + 1]; /* getfh res */ 1273 gf_res = &resop->nfs_resop4_u.opgetfh; 1274 1275 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1276 1277 /* 1278 * The open stateid has been updated on the server but not 1279 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1280 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1281 * WRITE call. That, however, will use the old stateid, so go ahead 1282 * and upate the open stateid now, before any call to makenfs4node. 1283 */ 1284 if (vpi) { 1285 nfs4_open_stream_t *tmp_osp; 1286 rnode4_t *tmp_rp = VTOR4(vpi); 1287 1288 tmp_osp = find_open_stream(oop, tmp_rp); 1289 if (tmp_osp) { 1290 tmp_osp->open_stateid = op_res->stateid; 1291 mutex_exit(&tmp_osp->os_sync_lock); 1292 open_stream_rele(tmp_osp, tmp_rp); 1293 } 1294 1295 /* 1296 * We must determine if the file handle given by the otw open 1297 * is the same as the file handle which was passed in with 1298 * *vpp. This case can be reached if the file we are trying 1299 * to open has been removed and another file has been created 1300 * having the same file name. The passed in vnode is released 1301 * later. 1302 */ 1303 orig_sfh = VTOR4(vpi)->r_fh; 1304 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1305 } 1306 1307 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1308 1309 if (create_flag || fh_differs) { 1310 int rnode_err = 0; 1311 1312 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1313 dvp, fn_get(VTOSV(dvp)->sv_name, file_name)); 1314 1315 if (e.error) 1316 PURGE_ATTRCACHE4(vp); 1317 /* 1318 * For the newly created vp case, make sure the rnode 1319 * isn't bad before using it. 1320 */ 1321 mutex_enter(&(VTOR4(vp))->r_statelock); 1322 if (VTOR4(vp)->r_flags & R4RECOVERR) 1323 rnode_err = EIO; 1324 mutex_exit(&(VTOR4(vp))->r_statelock); 1325 1326 if (rnode_err) { 1327 nfs4_end_open_seqid_sync(oop); 1328 nfs4args_copen_free(open_args); 1329 if (setgid_flag) { 1330 nfs4args_verify_free(&argop[8]); 1331 nfs4args_setattr_free(&argop[9]); 1332 } 1333 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1334 nfs_rw_exit(&drp->r_rwlock); 1335 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1336 needrecov); 1337 open_owner_rele(oop); 1338 VN_RELE(vp); 1339 if (ncr != NULL) 1340 crfree(ncr); 1341 sfh4_rele(&otw_sfh); 1342 kmem_free(argop, argoplist_size); 1343 return (EIO); 1344 } 1345 } else { 1346 vp = vpi; 1347 } 1348 sfh4_rele(&otw_sfh); 1349 1350 /* 1351 * It seems odd to get a full set of attrs and then not update 1352 * the object's attrcache in the non-create case. Create case uses 1353 * the attrs since makenfs4node checks to see if the attrs need to 1354 * be updated (and then updates them). The non-create case should 1355 * update attrs also. 1356 */ 1357 if (! create_flag && ! fh_differs && !e.error) { 1358 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1359 } 1360 1361 nfs4_error_zinit(&e); 1362 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1363 /* This does not do recovery for vp explicitly. */ 1364 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1365 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1366 1367 if (e.error || e.stat) { 1368 nfs4_end_open_seqid_sync(oop); 1369 nfs4args_copen_free(open_args); 1370 if (setgid_flag) { 1371 nfs4args_verify_free(&argop[8]); 1372 nfs4args_setattr_free(&argop[9]); 1373 } 1374 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1375 nfs_rw_exit(&drp->r_rwlock); 1376 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1377 needrecov); 1378 open_owner_rele(oop); 1379 if (create_flag || fh_differs) { 1380 /* rele the makenfs4node */ 1381 VN_RELE(vp); 1382 } 1383 if (ncr != NULL) { 1384 crfree(ncr); 1385 ncr = NULL; 1386 } 1387 if (retry_open == TRUE) { 1388 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1389 "nfs4open_otw: retry the open since OPEN " 1390 "CONFIRM failed with error %d stat %d", 1391 e.error, e.stat)); 1392 if (create_flag && createmode == GUARDED4) { 1393 NFS4_DEBUG(nfs4_client_recov_debug, 1394 (CE_NOTE, "nfs4open_otw: switch " 1395 "createmode from GUARDED4 to " 1396 "UNCHECKED4")); 1397 createmode = UNCHECKED4; 1398 } 1399 goto recov_retry; 1400 } 1401 if (!e.error) { 1402 if (create_flag && (createmode != EXCLUSIVE4) && 1403 e.stat == NFS4ERR_BADOWNER) 1404 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1405 1406 e.error = geterrno4(e.stat); 1407 } 1408 kmem_free(argop, argoplist_size); 1409 return (e.error); 1410 } 1411 } 1412 1413 rp = VTOR4(vp); 1414 1415 mutex_enter(&rp->r_statev4_lock); 1416 if (create_flag) 1417 rp->created_v4 = 1; 1418 mutex_exit(&rp->r_statev4_lock); 1419 1420 mutex_enter(&oop->oo_lock); 1421 /* Doesn't matter if 'oo_just_created' already was set as this */ 1422 oop->oo_just_created = NFS4_PERM_CREATED; 1423 if (oop->oo_cred_otw) 1424 crfree(oop->oo_cred_otw); 1425 oop->oo_cred_otw = cred_otw; 1426 crhold(oop->oo_cred_otw); 1427 mutex_exit(&oop->oo_lock); 1428 1429 /* returns with 'os_sync_lock' held */ 1430 osp = find_or_create_open_stream(oop, rp, &created_osp); 1431 if (!osp) { 1432 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1433 "nfs4open_otw: failed to create an open stream")); 1434 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1435 "signal our end of use of the open seqid")); 1436 1437 nfs4_end_open_seqid_sync(oop); 1438 open_owner_rele(oop); 1439 nfs4args_copen_free(open_args); 1440 if (setgid_flag) { 1441 nfs4args_verify_free(&argop[8]); 1442 nfs4args_setattr_free(&argop[9]); 1443 } 1444 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1445 nfs_rw_exit(&drp->r_rwlock); 1446 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1447 if (create_flag || fh_differs) 1448 VN_RELE(vp); 1449 if (ncr != NULL) 1450 crfree(ncr); 1451 1452 kmem_free(argop, argoplist_size); 1453 return (EINVAL); 1454 1455 } 1456 1457 osp->open_stateid = op_res->stateid; 1458 1459 if (open_flag & FREAD) 1460 osp->os_share_acc_read++; 1461 if (open_flag & FWRITE) 1462 osp->os_share_acc_write++; 1463 osp->os_share_deny_none++; 1464 1465 /* 1466 * Need to reset this bitfield for the possible case where we were 1467 * going to OTW CLOSE the file, got a non-recoverable error, and before 1468 * we could retry the CLOSE, OPENed the file again. 1469 */ 1470 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1471 osp->os_final_close = 0; 1472 osp->os_force_close = 0; 1473 #ifdef DEBUG 1474 if (osp->os_failed_reopen) 1475 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1476 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1477 (void *)osp, (void *)cr, rnode4info(rp))); 1478 #endif 1479 osp->os_failed_reopen = 0; 1480 1481 mutex_exit(&osp->os_sync_lock); 1482 1483 nfs4_end_open_seqid_sync(oop); 1484 1485 if (created_osp && recov_state.rs_sp != NULL) { 1486 mutex_enter(&recov_state.rs_sp->s_lock); 1487 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1488 mutex_exit(&recov_state.rs_sp->s_lock); 1489 } 1490 1491 /* get rid of our reference to find oop */ 1492 open_owner_rele(oop); 1493 1494 open_stream_rele(osp, rp); 1495 1496 /* accept delegation, if any */ 1497 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1498 1499 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1500 1501 if (createmode == EXCLUSIVE4 && 1502 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1503 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1504 " EXCLUSIVE4: sending a SETATTR")); 1505 /* 1506 * If doing an exclusive create, then generate 1507 * a SETATTR to set the initial attributes. 1508 * Try to set the mtime and the atime to the 1509 * server's current time. It is somewhat 1510 * expected that these fields will be used to 1511 * store the exclusive create cookie. If not, 1512 * server implementors will need to know that 1513 * a SETATTR will follow an exclusive create 1514 * and the cookie should be destroyed if 1515 * appropriate. 1516 * 1517 * The AT_GID and AT_SIZE bits are turned off 1518 * so that the SETATTR request will not attempt 1519 * to process these. The gid will be set 1520 * separately if appropriate. The size is turned 1521 * off because it is assumed that a new file will 1522 * be created empty and if the file wasn't empty, 1523 * then the exclusive create will have failed 1524 * because the file must have existed already. 1525 * Therefore, no truncate operation is needed. 1526 */ 1527 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1528 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1529 1530 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1531 if (e.error) { 1532 /* 1533 * Couldn't correct the attributes of 1534 * the newly created file and the 1535 * attributes are wrong. Remove the 1536 * file and return an error to the 1537 * application. 1538 */ 1539 /* XXX will this take care of client state ? */ 1540 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1541 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1542 " remove file", e.error)); 1543 VN_RELE(vp); 1544 (void) nfs4_remove(dvp, file_name, cr); 1545 nfs_rw_exit(&drp->r_rwlock); 1546 goto skip_rwlock_exit; 1547 } 1548 } 1549 1550 /* 1551 * If we created or found the correct vnode, due to create_flag or 1552 * fh_differs being set, then update directory cache attribute, readdir 1553 * and dnlc caches. 1554 */ 1555 if (create_flag || fh_differs) { 1556 dirattr_info_t dinfo, *dinfop; 1557 1558 /* 1559 * Make sure getattr succeeded before using results. 1560 * note: op 7 is getattr(dir) for both flavors of 1561 * open(create). 1562 */ 1563 if (create_flag && res.status == NFS4_OK) { 1564 dinfo.di_time_call = t; 1565 dinfo.di_cred = cr; 1566 dinfo.di_garp = 1567 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1568 dinfop = &dinfo; 1569 } else { 1570 dinfop = NULL; 1571 } 1572 1573 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1574 dinfop); 1575 } 1576 nfs_rw_exit(&drp->r_rwlock); 1577 skip_rwlock_exit: 1578 1579 /* 1580 * If the page cache for this file was flushed from actions 1581 * above, it was done asynchronously and if that is true, 1582 * there is a need to wait here for it to complete. This must 1583 * be done outside of start_fop/end_fop. 1584 */ 1585 (void) nfs4_waitfor_purge_complete(vp); 1586 1587 /* 1588 * It is implicit that we are in the open case (create_flag == 0) since 1589 * fh_differs can only be set to a non-zero value in the open case. 1590 */ 1591 if (fh_differs != 0 && vpi != NULL) 1592 VN_RELE(vpi); 1593 1594 /* 1595 * Be sure to set *vpp to the correct value before returning. 1596 */ 1597 *vpp = vp; 1598 1599 nfs4args_copen_free(open_args); 1600 if (setgid_flag) { 1601 nfs4args_verify_free(&argop[8]); 1602 nfs4args_setattr_free(&argop[9]); 1603 } 1604 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1605 1606 if (ncr) 1607 crfree(ncr); 1608 kmem_free(argop, argoplist_size); 1609 return (e.error); 1610 } 1611 1612 /* 1613 * Reopen an open instance. cf. nfs4open_otw(). 1614 * 1615 * Errors are returned by the nfs4_error_t parameter. 1616 * - ep->error contains an errno value or zero. 1617 * - if it is zero, ep->stat is set to an NFS status code, if any. 1618 * If the file could not be reopened, but the caller should continue, the 1619 * file is marked dead and no error values are returned. If the caller 1620 * should stop recovering open files and start over, either the ep->error 1621 * value or ep->stat will indicate an error (either something that requires 1622 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1623 * filehandles) may be handled silently by this routine. 1624 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1625 * will be started, so the caller should not do it. 1626 * 1627 * Gotos: 1628 * - kill_file : reopen failed in such a fashion to constitute marking the 1629 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1630 * is for cases where recovery is not possible. 1631 * - failed_reopen : same as above, except that the file has already been 1632 * marked dead, so no need to do it again. 1633 * - bailout : reopen failed but we are able to recover and retry the reopen - 1634 * either within this function immediatley or via the calling function. 1635 */ 1636 1637 void 1638 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1639 open_claim_type4 claim, bool_t frc_use_claim_previous, 1640 bool_t is_recov) 1641 { 1642 COMPOUND4args_clnt args; 1643 COMPOUND4res_clnt res; 1644 nfs_argop4 argop[4]; 1645 nfs_resop4 *resop; 1646 OPEN4res *op_res = NULL; 1647 OPEN4cargs *open_args; 1648 GETFH4res *gf_res; 1649 rnode4_t *rp = VTOR4(vp); 1650 int doqueue = 1; 1651 cred_t *cr = NULL, *cred_otw = NULL; 1652 nfs4_open_owner_t *oop = NULL; 1653 seqid4 seqid; 1654 nfs4_ga_res_t *garp; 1655 char fn[MAXNAMELEN]; 1656 nfs4_recov_state_t recov = {NULL, 0}; 1657 nfs4_lost_rqst_t lost_rqst; 1658 mntinfo4_t *mi = VTOMI4(vp); 1659 bool_t abort; 1660 char *failed_msg = ""; 1661 int fh_different; 1662 hrtime_t t; 1663 nfs4_bseqid_entry_t *bsep = NULL; 1664 1665 ASSERT(nfs4_consistent_type(vp)); 1666 ASSERT(curproc->p_zone == mi->mi_zone); 1667 1668 nfs4_error_zinit(ep); 1669 1670 /* this is the cred used to find the open owner */ 1671 cr = state_to_cred(osp); 1672 if (cr == NULL) { 1673 failed_msg = "Couldn't reopen: no cred"; 1674 goto kill_file; 1675 } 1676 /* use this cred for OTW operations */ 1677 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1678 1679 top: 1680 nfs4_error_zinit(ep); 1681 1682 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1683 /* File system has been unmounted, quit */ 1684 ep->error = EIO; 1685 failed_msg = "Couldn't reopen: file system has been unmounted"; 1686 goto kill_file; 1687 } 1688 1689 oop = osp->os_open_owner; 1690 1691 ASSERT(oop != NULL); 1692 if (oop == NULL) { /* be defensive in non-DEBUG */ 1693 failed_msg = "can't reopen: no open owner"; 1694 goto kill_file; 1695 } 1696 open_owner_hold(oop); 1697 1698 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1699 if (ep->error) { 1700 open_owner_rele(oop); 1701 oop = NULL; 1702 goto bailout; 1703 } 1704 1705 /* 1706 * If the rnode has a delegation and the delegation has been 1707 * recovered and the server didn't request a recall and the caller 1708 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1709 * recovery) and the rnode hasn't been marked dead, then install 1710 * the delegation stateid in the open stream. Otherwise, proceed 1711 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1712 */ 1713 mutex_enter(&rp->r_statev4_lock); 1714 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1715 !rp->r_deleg_return_pending && 1716 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1717 !rp->r_deleg_needs_recall && 1718 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1719 !(rp->r_flags & R4RECOVERR)) { 1720 mutex_enter(&osp->os_sync_lock); 1721 osp->os_delegation = 1; 1722 osp->open_stateid = rp->r_deleg_stateid; 1723 mutex_exit(&osp->os_sync_lock); 1724 mutex_exit(&rp->r_statev4_lock); 1725 goto bailout; 1726 } 1727 mutex_exit(&rp->r_statev4_lock); 1728 1729 /* 1730 * If the file failed recovery, just quit. This failure need not 1731 * affect other reopens, so don't return an error. 1732 */ 1733 mutex_enter(&rp->r_statelock); 1734 if (rp->r_flags & R4RECOVERR) { 1735 mutex_exit(&rp->r_statelock); 1736 ep->error = 0; 1737 goto failed_reopen; 1738 } 1739 mutex_exit(&rp->r_statelock); 1740 1741 /* 1742 * argop is empty here 1743 * 1744 * PUTFH, OPEN, GETATTR 1745 */ 1746 args.ctag = TAG_REOPEN; 1747 args.array_len = 4; 1748 args.array = argop; 1749 1750 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1751 "nfs4_reopen: file is type %d, id %s", 1752 vp->v_type, rnode4info(VTOR4(vp)))); 1753 1754 argop[0].argop = OP_CPUTFH; 1755 1756 if (claim != CLAIM_PREVIOUS) { 1757 /* 1758 * if this is a file mount then 1759 * use the mntinfo parentfh 1760 */ 1761 argop[0].nfs_argop4_u.opcputfh.sfh = 1762 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1763 VTOSV(vp)->sv_dfh; 1764 } else { 1765 /* putfh fh to reopen */ 1766 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1767 } 1768 1769 argop[1].argop = OP_COPEN; 1770 open_args = &argop[1].nfs_argop4_u.opcopen; 1771 open_args->claim = claim; 1772 1773 if (claim == CLAIM_NULL) { 1774 1775 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1776 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1777 "failed for vp 0x%p for CLAIM_NULL with %m", 1778 (void *)vp); 1779 failed_msg = "Couldn't reopen: vtoname failed for " 1780 "CLAIM_NULL"; 1781 /* nothing allocated yet */ 1782 goto kill_file; 1783 } 1784 1785 open_args->open_claim4_u.cfile = fn; 1786 } else if (claim == CLAIM_PREVIOUS) { 1787 1788 /* 1789 * We have two cases to deal with here: 1790 * 1) We're being called to reopen files in order to satisfy 1791 * a lock operation request which requires us to explicitly 1792 * reopen files which were opened under a delegation. If 1793 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1794 * that case, frc_use_claim_previous is TRUE and we must 1795 * use the rnode's current delegation type (r_deleg_type). 1796 * 2) We're reopening files during some form of recovery. 1797 * In this case, frc_use_claim_previous is FALSE and we 1798 * use the delegation type appropriate for recovery 1799 * (r_deleg_needs_recovery). 1800 */ 1801 mutex_enter(&rp->r_statev4_lock); 1802 open_args->open_claim4_u.delegate_type = 1803 frc_use_claim_previous ? 1804 rp->r_deleg_type : 1805 rp->r_deleg_needs_recovery; 1806 mutex_exit(&rp->r_statev4_lock); 1807 1808 } else if (claim == CLAIM_DELEGATE_CUR) { 1809 1810 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1811 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1812 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1813 "with %m", (void *)vp); 1814 failed_msg = "Couldn't reopen: vtoname failed for " 1815 "CLAIM_DELEGATE_CUR"; 1816 /* nothing allocated yet */ 1817 goto kill_file; 1818 } 1819 1820 mutex_enter(&rp->r_statev4_lock); 1821 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1822 rp->r_deleg_stateid; 1823 mutex_exit(&rp->r_statev4_lock); 1824 1825 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1826 } 1827 open_args->opentype = OPEN4_NOCREATE; 1828 open_args->owner.clientid = mi2clientid(mi); 1829 open_args->owner.owner_len = sizeof (oop->oo_name); 1830 open_args->owner.owner_val = 1831 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1832 bcopy(&oop->oo_name, open_args->owner.owner_val, 1833 open_args->owner.owner_len); 1834 open_args->share_access = 0; 1835 open_args->share_deny = 0; 1836 1837 mutex_enter(&osp->os_sync_lock); 1838 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1839 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1840 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1841 (void *)osp, (void *)rp, osp->os_share_acc_read, 1842 osp->os_share_acc_write, osp->os_open_ref_count, 1843 osp->os_mmap_read, osp->os_mmap_write, claim)); 1844 1845 if (osp->os_share_acc_read || osp->os_mmap_read) 1846 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1847 if (osp->os_share_acc_write || osp->os_mmap_write) 1848 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1849 if (osp->os_share_deny_read) 1850 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1851 if (osp->os_share_deny_write) 1852 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1853 mutex_exit(&osp->os_sync_lock); 1854 1855 seqid = nfs4_get_open_seqid(oop) + 1; 1856 open_args->seqid = seqid; 1857 1858 /* Construct the getfh part of the compound */ 1859 argop[2].argop = OP_GETFH; 1860 1861 /* Construct the getattr part of the compound */ 1862 argop[3].argop = OP_GETATTR; 1863 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1864 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1865 1866 t = gethrtime(); 1867 1868 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1869 1870 if (ep->error) { 1871 if (!is_recov && !frc_use_claim_previous && 1872 (ep->error == EINTR || ep->error == ETIMEDOUT || 1873 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1874 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1875 cred_otw, vp, NULL, open_args); 1876 abort = nfs4_start_recovery(ep, 1877 VTOMI4(vp), vp, NULL, NULL, 1878 lost_rqst.lr_op == OP_OPEN ? 1879 &lost_rqst : NULL, OP_OPEN, NULL); 1880 nfs4args_copen_free(open_args); 1881 goto bailout; 1882 } 1883 1884 nfs4args_copen_free(open_args); 1885 1886 if (ep->error == EACCES && cred_otw != cr) { 1887 crfree(cred_otw); 1888 cred_otw = cr; 1889 crhold(cred_otw); 1890 nfs4_end_open_seqid_sync(oop); 1891 open_owner_rele(oop); 1892 oop = NULL; 1893 goto top; 1894 } 1895 if (ep->error == ETIMEDOUT) 1896 goto bailout; 1897 failed_msg = "Couldn't reopen: rpc error"; 1898 goto kill_file; 1899 } 1900 1901 if (nfs4_need_to_bump_seqid(&res)) 1902 nfs4_set_open_seqid(seqid, oop, args.ctag); 1903 1904 switch (res.status) { 1905 case NFS4_OK: 1906 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1907 mutex_enter(&rp->r_statelock); 1908 rp->r_delay_interval = 0; 1909 mutex_exit(&rp->r_statelock); 1910 } 1911 break; 1912 case NFS4ERR_BAD_SEQID: 1913 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1914 args.ctag, open_args->seqid); 1915 1916 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1917 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1918 NULL, OP_OPEN, bsep); 1919 1920 nfs4args_copen_free(open_args); 1921 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1922 nfs4_end_open_seqid_sync(oop); 1923 open_owner_rele(oop); 1924 oop = NULL; 1925 kmem_free(bsep, sizeof (*bsep)); 1926 1927 goto kill_file; 1928 case NFS4ERR_NO_GRACE: 1929 nfs4args_copen_free(open_args); 1930 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1931 nfs4_end_open_seqid_sync(oop); 1932 open_owner_rele(oop); 1933 oop = NULL; 1934 if (claim == CLAIM_PREVIOUS) { 1935 /* 1936 * Retry as a plain open. We don't need to worry about 1937 * checking the changeinfo: it is acceptable for a 1938 * client to re-open a file and continue processing 1939 * (in the absence of locks). 1940 */ 1941 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1942 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1943 "will retry as CLAIM_NULL")); 1944 claim = CLAIM_NULL; 1945 nfs4_mi_kstat_inc_no_grace(mi); 1946 goto top; 1947 } 1948 failed_msg = 1949 "Couldn't reopen: tried reclaim outside grace period. "; 1950 goto kill_file; 1951 case NFS4ERR_GRACE: 1952 nfs4_set_grace_wait(mi); 1953 nfs4args_copen_free(open_args); 1954 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1955 nfs4_end_open_seqid_sync(oop); 1956 open_owner_rele(oop); 1957 oop = NULL; 1958 ep->error = nfs4_wait_for_grace(mi, &recov); 1959 if (ep->error != 0) 1960 goto bailout; 1961 goto top; 1962 case NFS4ERR_DELAY: 1963 nfs4_set_delay_wait(vp); 1964 nfs4args_copen_free(open_args); 1965 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1966 nfs4_end_open_seqid_sync(oop); 1967 open_owner_rele(oop); 1968 oop = NULL; 1969 ep->error = nfs4_wait_for_delay(vp, &recov); 1970 nfs4_mi_kstat_inc_delay(mi); 1971 if (ep->error != 0) 1972 goto bailout; 1973 goto top; 1974 case NFS4ERR_FHEXPIRED: 1975 /* recover filehandle and retry */ 1976 abort = nfs4_start_recovery(ep, 1977 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL); 1978 nfs4args_copen_free(open_args); 1979 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1980 nfs4_end_open_seqid_sync(oop); 1981 open_owner_rele(oop); 1982 oop = NULL; 1983 if (abort == FALSE) 1984 goto top; 1985 failed_msg = "Couldn't reopen: recovery aborted"; 1986 goto kill_file; 1987 case NFS4ERR_RESOURCE: 1988 case NFS4ERR_STALE_CLIENTID: 1989 case NFS4ERR_WRONGSEC: 1990 case NFS4ERR_EXPIRED: 1991 /* 1992 * Do not mark the file dead and let the calling 1993 * function initiate recovery. 1994 */ 1995 nfs4args_copen_free(open_args); 1996 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1997 nfs4_end_open_seqid_sync(oop); 1998 open_owner_rele(oop); 1999 oop = NULL; 2000 goto bailout; 2001 case NFS4ERR_ACCESS: 2002 if (cred_otw != cr) { 2003 crfree(cred_otw); 2004 cred_otw = cr; 2005 crhold(cred_otw); 2006 nfs4args_copen_free(open_args); 2007 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2008 nfs4_end_open_seqid_sync(oop); 2009 open_owner_rele(oop); 2010 oop = NULL; 2011 goto top; 2012 } 2013 /* fall through */ 2014 default: 2015 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2016 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2017 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2018 rnode4info(VTOR4(vp)))); 2019 failed_msg = "Couldn't reopen: NFSv4 error"; 2020 nfs4args_copen_free(open_args); 2021 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2022 goto kill_file; 2023 } 2024 2025 resop = &res.array[1]; /* open res */ 2026 op_res = &resop->nfs_resop4_u.opopen; 2027 2028 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2029 2030 /* 2031 * Check if the path we reopened really is the same 2032 * file. We could end up in a situation where the file 2033 * was removed and a new file created with the same name. 2034 */ 2035 resop = &res.array[2]; 2036 gf_res = &resop->nfs_resop4_u.opgetfh; 2037 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2038 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2039 if (fh_different) { 2040 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2041 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2042 /* Oops, we don't have the same file */ 2043 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2044 failed_msg = "Couldn't reopen: Persistent " 2045 "file handle changed"; 2046 else 2047 failed_msg = "Couldn't reopen: Volatile " 2048 "(no expire on open) file handle changed"; 2049 2050 nfs4args_copen_free(open_args); 2051 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2052 nfs_rw_exit(&mi->mi_fh_lock); 2053 goto kill_file; 2054 2055 } else { 2056 /* 2057 * We have volatile file handles that don't compare. 2058 * If the fids are the same then we assume that the 2059 * file handle expired but the rnode still refers to 2060 * the same file object. 2061 * 2062 * First check that we have fids or not. 2063 * If we don't we have a dumb server so we will 2064 * just assume every thing is ok for now. 2065 */ 2066 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2067 rp->r_attr.va_mask & AT_NODEID && 2068 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2069 /* 2070 * We have fids, but they don't 2071 * compare. So kill the file. 2072 */ 2073 failed_msg = 2074 "Couldn't reopen: file handle changed" 2075 " due to mismatched fids"; 2076 nfs4args_copen_free(open_args); 2077 (void) xdr_free(xdr_COMPOUND4res_clnt, 2078 (caddr_t)&res); 2079 nfs_rw_exit(&mi->mi_fh_lock); 2080 goto kill_file; 2081 } else { 2082 /* 2083 * We have volatile file handles that refers 2084 * to the same file (at least they have the 2085 * same fid) or we don't have fids so we 2086 * can't tell. :(. We'll be a kind and accepting 2087 * client so we'll update the rnode's file 2088 * handle with the otw handle. 2089 * 2090 * We need to drop mi->mi_fh_lock since 2091 * sh4_update acquires it. Since there is 2092 * only one recovery thread there is no 2093 * race. 2094 */ 2095 nfs_rw_exit(&mi->mi_fh_lock); 2096 sfh4_update(rp->r_fh, &gf_res->object); 2097 } 2098 } 2099 } else { 2100 nfs_rw_exit(&mi->mi_fh_lock); 2101 } 2102 2103 ASSERT(nfs4_consistent_type(vp)); 2104 2105 /* 2106 * If the server wanted an OPEN_CONFIRM but that fails, just start 2107 * over. Presumably if there is a persistent error it will show up 2108 * when we resend the OPEN. 2109 */ 2110 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2111 bool_t retry_open = FALSE; 2112 2113 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2114 cred_otw, is_recov, &retry_open, 2115 oop, FALSE, ep, NULL); 2116 if (ep->error || ep->stat) { 2117 nfs4args_copen_free(open_args); 2118 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2119 nfs4_end_open_seqid_sync(oop); 2120 open_owner_rele(oop); 2121 oop = NULL; 2122 goto top; 2123 } 2124 } 2125 2126 mutex_enter(&osp->os_sync_lock); 2127 osp->open_stateid = op_res->stateid; 2128 osp->os_delegation = 0; 2129 /* 2130 * Need to reset this bitfield for the possible case where we were 2131 * going to OTW CLOSE the file, got a non-recoverable error, and before 2132 * we could retry the CLOSE, OPENed the file again. 2133 */ 2134 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2135 osp->os_final_close = 0; 2136 osp->os_force_close = 0; 2137 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2138 osp->os_dc_openacc = open_args->share_access; 2139 mutex_exit(&osp->os_sync_lock); 2140 2141 nfs4_end_open_seqid_sync(oop); 2142 2143 /* accept delegation, if any */ 2144 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2145 2146 nfs4args_copen_free(open_args); 2147 2148 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2149 2150 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2151 2152 ASSERT(nfs4_consistent_type(vp)); 2153 2154 open_owner_rele(oop); 2155 crfree(cr); 2156 crfree(cred_otw); 2157 return; 2158 2159 kill_file: 2160 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2161 failed_reopen: 2162 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2163 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2164 (void *)osp, (void *)cr, rnode4info(rp))); 2165 mutex_enter(&osp->os_sync_lock); 2166 osp->os_failed_reopen = 1; 2167 mutex_exit(&osp->os_sync_lock); 2168 bailout: 2169 if (oop != NULL) { 2170 nfs4_end_open_seqid_sync(oop); 2171 open_owner_rele(oop); 2172 } 2173 if (cr != NULL) 2174 crfree(cr); 2175 if (cred_otw != NULL) 2176 crfree(cred_otw); 2177 } 2178 2179 /* for . and .. OPENs */ 2180 /* ARGSUSED */ 2181 static int 2182 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2183 { 2184 rnode4_t *rp; 2185 nfs4_ga_res_t gar; 2186 2187 ASSERT(curproc->p_zone == VTOMI4(*vpp)->mi_zone); 2188 2189 /* 2190 * If close-to-open consistency checking is turned off or 2191 * if there is no cached data, we can avoid 2192 * the over the wire getattr. Otherwise, force a 2193 * call to the server to get fresh attributes and to 2194 * check caches. This is required for close-to-open 2195 * consistency. 2196 */ 2197 rp = VTOR4(*vpp); 2198 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2199 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2200 return (0); 2201 2202 gar.n4g_va.va_mask = AT_ALL; 2203 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2204 } 2205 2206 /* 2207 * CLOSE a file 2208 */ 2209 static int 2210 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 2211 { 2212 rnode4_t *rp; 2213 int pc_err = 0; 2214 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2215 2216 /* 2217 * Remove client state for this (lockowner, file) pair. 2218 * Issue otw v4 call to have the server do the same. 2219 */ 2220 2221 rp = VTOR4(vp); 2222 2223 /* 2224 * zone_enter(2) prevents processes from changing zones with NFS files 2225 * open; if we happen to get here from the wrong zone we can't do 2226 * anything over the wire. 2227 */ 2228 if (VTOMI4(vp)->mi_zone != curproc->p_zone) { 2229 /* 2230 * We could attempt to clean up locks, except we're sure 2231 * that the current process didn't acquire any locks on 2232 * the file: any attempt to lock a file belong to another zone 2233 * will fail, and one can't lock an NFS file and then change 2234 * zones, as that fails too. 2235 * 2236 * Returning an error here is the sane thing to do. A 2237 * subsequent call to VN_RELE() which translates to a 2238 * nfs4_inactive() will clean up state: if the zone of the 2239 * vnode's origin is still alive and kicking, the inactive 2240 * thread will handle the request (from the correct zone), and 2241 * everything (minus the OTW close call) should be OK. If the 2242 * zone is going away nfs4_async_inactive() will throw away 2243 * delegations, open streams and cached pages inline. 2244 */ 2245 return (EIO); 2246 } 2247 2248 /* 2249 * If we are using local locking for this filesystem, then 2250 * release all of the SYSV style record locks. Otherwise, 2251 * we are doing network locking and we need to release all 2252 * of the network locks. All of the locks held by this 2253 * process on this file are released no matter what the 2254 * incoming reference count is. 2255 */ 2256 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2257 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2258 cleanshares(vp, ttoproc(curthread)->p_pid); 2259 } else 2260 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2261 2262 if (e.error) 2263 return (e.error); 2264 2265 if (count > 1) 2266 return (0); 2267 2268 /* 2269 * If the file has been `unlinked', then purge the 2270 * DNLC so that this vnode will get reycled quicker 2271 * and the .nfs* file on the server will get removed. 2272 */ 2273 if (rp->r_unldvp != NULL) 2274 dnlc_purge_vp(vp); 2275 2276 /* 2277 * If the file was open for write and there are pages, 2278 * do a synchronous flush and commit of all of the 2279 * dirty and uncommitted pages. 2280 */ 2281 ASSERT(!e.error); 2282 if ((flag & FWRITE) && nfs4_has_pages(vp)) { 2283 pc_err = nfs4_putpage_commit(vp, 0, 0, cr); 2284 } 2285 2286 mutex_enter(&rp->r_statelock); 2287 e.error = rp->r_error; 2288 rp->r_error = 0; 2289 mutex_exit(&rp->r_statelock); 2290 2291 /* Check to see if we need to close the file */ 2292 2293 if (vp->v_type != VREG) 2294 return (pc_err ? pc_err : e.error); 2295 2296 /* Let nfs4close_one figure out if an OTW close is needed. */ 2297 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2298 2299 if (pc_err) 2300 return (pc_err); 2301 2302 return (e.error ? e.error : geterrno4(e.stat)); 2303 } 2304 2305 /* 2306 * Initialize *lost_rqstp. 2307 */ 2308 2309 static void 2310 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2311 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2312 vnode_t *vp) 2313 { 2314 if (error != ETIMEDOUT && error != EINTR && 2315 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2316 lost_rqstp->lr_op = 0; 2317 return; 2318 } 2319 2320 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2321 "nfs4close_save_lost_rqst: error %d", error)); 2322 2323 lost_rqstp->lr_op = OP_CLOSE; 2324 /* 2325 * The vp is held and rele'd via the recovery code. 2326 * See nfs4_save_lost_rqst. 2327 */ 2328 lost_rqstp->lr_vp = vp; 2329 lost_rqstp->lr_dvp = NULL; 2330 lost_rqstp->lr_oop = oop; 2331 lost_rqstp->lr_osp = osp; 2332 ASSERT(osp != NULL); 2333 ASSERT(mutex_owned(&osp->os_sync_lock)); 2334 osp->os_pending_close = 1; 2335 lost_rqstp->lr_lop = NULL; 2336 lost_rqstp->lr_cr = cr; 2337 lost_rqstp->lr_flk = NULL; 2338 lost_rqstp->lr_putfirst = FALSE; 2339 } 2340 2341 /* 2342 * Assumes you already have the open seqid sync grabbed as well as the 2343 * 'os_sync_lock'. Note: this will release the open seqid sync and 2344 * 'os_sync_lock' if client recovery starts. Calling functions have to 2345 * be prepared to handle this. 2346 * 2347 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2348 * was needed and was started, and that the calling function should retry 2349 * this function; otherwise it is returned as 0. 2350 * 2351 * Errors are returned via the nfs4_error_t parameter. 2352 */ 2353 static void 2354 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2355 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2356 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2357 { 2358 COMPOUND4args_clnt args; 2359 COMPOUND4res_clnt res; 2360 CLOSE4args *close_args; 2361 nfs_resop4 *resop; 2362 nfs_argop4 argop[3]; 2363 int doqueue = 1; 2364 mntinfo4_t *mi; 2365 seqid4 seqid; 2366 vnode_t *vp; 2367 bool_t needrecov = FALSE; 2368 nfs4_lost_rqst_t lost_rqst; 2369 hrtime_t t; 2370 2371 ASSERT(curproc->p_zone == VTOMI4(RTOV4(rp))->mi_zone); 2372 2373 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2374 2375 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2376 2377 /* Only set this to 1 if recovery is started */ 2378 *recov = 0; 2379 2380 /* do the OTW call to close the file */ 2381 2382 if (close_type == CLOSE_RESEND) 2383 args.ctag = TAG_CLOSE_LOST; 2384 else if (close_type == CLOSE_AFTER_RESEND) 2385 args.ctag = TAG_CLOSE_UNDO; 2386 else 2387 args.ctag = TAG_CLOSE; 2388 2389 args.array_len = 3; 2390 args.array = argop; 2391 2392 vp = RTOV4(rp); 2393 2394 mi = VTOMI4(vp); 2395 2396 /* putfh target fh */ 2397 argop[0].argop = OP_CPUTFH; 2398 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2399 2400 argop[1].argop = OP_GETATTR; 2401 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2402 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2403 2404 argop[2].argop = OP_CLOSE; 2405 close_args = &argop[2].nfs_argop4_u.opclose; 2406 2407 seqid = nfs4_get_open_seqid(oop) + 1; 2408 2409 close_args->seqid = seqid; 2410 close_args->open_stateid = osp->open_stateid; 2411 2412 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2413 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2414 rnode4info(rp))); 2415 2416 t = gethrtime(); 2417 2418 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2419 2420 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2421 nfs4_set_open_seqid(seqid, oop, args.ctag); 2422 } 2423 2424 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2425 if (ep->error && !needrecov) { 2426 /* 2427 * if there was an error and no recovery is to be done 2428 * then then set up the file to flush its cache if 2429 * needed for the next caller. 2430 */ 2431 mutex_enter(&rp->r_statelock); 2432 PURGE_ATTRCACHE4_LOCKED(rp); 2433 rp->r_flags &= ~R4WRITEMODIFIED; 2434 mutex_exit(&rp->r_statelock); 2435 return; 2436 } 2437 2438 if (needrecov) { 2439 bool_t abort; 2440 nfs4_bseqid_entry_t *bsep = NULL; 2441 2442 if (close_type != CLOSE_RESEND) 2443 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2444 osp, cred_otw, vp); 2445 2446 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2447 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2448 0, args.ctag, close_args->seqid); 2449 2450 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2451 "nfs4close_otw: initiating recovery. error %d " 2452 "res.status %d", ep->error, res.status)); 2453 2454 /* 2455 * Drop the 'os_sync_lock' here so we don't hit 2456 * a potential recursive mutex_enter via an 2457 * 'open_stream_hold()'. 2458 */ 2459 mutex_exit(&osp->os_sync_lock); 2460 *have_sync_lockp = 0; 2461 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2462 (close_type != CLOSE_RESEND && 2463 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2464 OP_CLOSE, bsep); 2465 2466 /* drop open seq sync, and let the calling function regrab it */ 2467 nfs4_end_open_seqid_sync(oop); 2468 *did_start_seqid_syncp = 0; 2469 2470 if (bsep) 2471 kmem_free(bsep, sizeof (*bsep)); 2472 /* 2473 * For signals, the caller wants to quit, so don't say to 2474 * retry. For forced unmount, if it's a user thread, it 2475 * wants to quit. If it's a recovery thread, the retry 2476 * will happen higher-up on the call stack. Either way, 2477 * don't say to retry. 2478 */ 2479 if (abort == FALSE && ep->error != EINTR && 2480 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2481 close_type != CLOSE_RESEND && 2482 close_type != CLOSE_AFTER_RESEND) 2483 *recov = 1; 2484 else 2485 *recov = 0; 2486 2487 if (!ep->error) 2488 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2489 return; 2490 } 2491 2492 if (res.status) { 2493 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2494 return; 2495 } 2496 2497 mutex_enter(&rp->r_statev4_lock); 2498 rp->created_v4 = 0; 2499 mutex_exit(&rp->r_statev4_lock); 2500 2501 resop = &res.array[2]; 2502 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2503 osp->os_valid = 0; 2504 2505 /* 2506 * This removes the reference obtained at OPEN; ie, when the 2507 * open stream structure was created. 2508 * 2509 * We don't have to worry about calling 'open_stream_rele' 2510 * since we our currently holding a reference to the open 2511 * stream which means the count cannot go to 0 with this 2512 * decrement. 2513 */ 2514 ASSERT(osp->os_ref_count >= 2); 2515 osp->os_ref_count--; 2516 2517 if (!ep->error) 2518 nfs4_attr_cache(vp, 2519 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2520 t, cred_otw, TRUE, NULL); 2521 2522 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2523 " returning %d", ep->error)); 2524 2525 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2526 } 2527 2528 /* ARGSUSED */ 2529 static int 2530 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2531 caller_context_t *ct) 2532 { 2533 rnode4_t *rp; 2534 u_offset_t off; 2535 offset_t diff; 2536 uint_t on; 2537 uint_t n; 2538 caddr_t base; 2539 uint_t flags; 2540 int error; 2541 mntinfo4_t *mi; 2542 2543 rp = VTOR4(vp); 2544 2545 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2546 2547 if (IS_SHADOW(vp, rp)) 2548 vp = RTOV4(rp); 2549 2550 if (vp->v_type != VREG) 2551 return (EISDIR); 2552 2553 mi = VTOMI4(vp); 2554 2555 if (curproc->p_zone != mi->mi_zone) 2556 return (EIO); 2557 2558 if (uiop->uio_resid == 0) 2559 return (0); 2560 2561 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2562 return (EINVAL); 2563 2564 mutex_enter(&rp->r_statelock); 2565 if (rp->r_flags & R4RECOVERRP) 2566 error = (rp->r_error ? rp->r_error : EIO); 2567 else 2568 error = 0; 2569 mutex_exit(&rp->r_statelock); 2570 if (error) 2571 return (error); 2572 2573 /* 2574 * Bypass VM if caching has been disabled (e.g., locking) or if 2575 * using client-side direct I/O and the file is not mmap'd and 2576 * there are no cached pages. 2577 */ 2578 if ((vp->v_flag & VNOCACHE) || 2579 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2580 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2581 size_t resid = 0; 2582 2583 return (nfs4read(vp, NULL, uiop->uio_loffset, 2584 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2585 } 2586 2587 error = 0; 2588 2589 do { 2590 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2591 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2592 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2593 2594 if (error = nfs4_validate_caches(vp, cr)) 2595 break; 2596 2597 mutex_enter(&rp->r_statelock); 2598 diff = rp->r_size - uiop->uio_loffset; 2599 mutex_exit(&rp->r_statelock); 2600 if (diff <= 0) 2601 break; 2602 if (diff < n) 2603 n = (uint_t)diff; 2604 2605 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, S_READ); 2606 2607 error = uiomove(base + on, n, UIO_READ, uiop); 2608 2609 if (!error) { 2610 /* 2611 * If read a whole block or read to eof, 2612 * won't need this buffer again soon. 2613 */ 2614 mutex_enter(&rp->r_statelock); 2615 if (n + on == MAXBSIZE || 2616 uiop->uio_loffset == rp->r_size) 2617 flags = SM_DONTNEED; 2618 else 2619 flags = 0; 2620 mutex_exit(&rp->r_statelock); 2621 error = segmap_release(segkmap, base, flags); 2622 } else 2623 (void) segmap_release(segkmap, base, 0); 2624 } while (!error && uiop->uio_resid > 0); 2625 2626 return (error); 2627 } 2628 2629 /* ARGSUSED */ 2630 static int 2631 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2632 caller_context_t *ct) 2633 { 2634 rlim64_t limit = uiop->uio_llimit; 2635 rnode4_t *rp; 2636 u_offset_t off; 2637 caddr_t base; 2638 uint_t flags; 2639 int remainder; 2640 size_t n; 2641 int on; 2642 int error; 2643 int resid; 2644 u_offset_t offset; 2645 mntinfo4_t *mi; 2646 uint_t bsize; 2647 2648 rp = VTOR4(vp); 2649 2650 if (IS_SHADOW(vp, rp)) 2651 vp = RTOV4(rp); 2652 2653 if (vp->v_type != VREG) 2654 return (EISDIR); 2655 2656 mi = VTOMI4(vp); 2657 2658 if (curproc->p_zone != mi->mi_zone) 2659 return (EIO); 2660 2661 if (uiop->uio_resid == 0) 2662 return (0); 2663 2664 mutex_enter(&rp->r_statelock); 2665 if (rp->r_flags & R4RECOVERRP) 2666 error = (rp->r_error ? rp->r_error : EIO); 2667 else 2668 error = 0; 2669 mutex_exit(&rp->r_statelock); 2670 if (error) 2671 return (error); 2672 2673 if (ioflag & FAPPEND) { 2674 struct vattr va; 2675 2676 /* 2677 * Must serialize if appending. 2678 */ 2679 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2680 nfs_rw_exit(&rp->r_rwlock); 2681 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2682 INTR(vp))) 2683 return (EINTR); 2684 } 2685 2686 va.va_mask = AT_SIZE; 2687 error = nfs4getattr(vp, &va, cr); 2688 if (error) 2689 return (error); 2690 uiop->uio_loffset = va.va_size; 2691 } 2692 2693 offset = uiop->uio_loffset + uiop->uio_resid; 2694 2695 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2696 return (EINVAL); 2697 2698 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2699 limit = MAXOFFSET_T; 2700 2701 /* 2702 * Check to make sure that the process will not exceed 2703 * its limit on file size. It is okay to write up to 2704 * the limit, but not beyond. Thus, the write which 2705 * reaches the limit will be short and the next write 2706 * will return an error. 2707 */ 2708 remainder = 0; 2709 if (offset > uiop->uio_llimit) { 2710 remainder = offset - uiop->uio_llimit; 2711 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2712 if (uiop->uio_resid <= 0) { 2713 proc_t *p = ttoproc(curthread); 2714 2715 uiop->uio_resid += remainder; 2716 mutex_enter(&p->p_lock); 2717 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2718 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2719 mutex_exit(&p->p_lock); 2720 return (EFBIG); 2721 } 2722 } 2723 2724 /* update the change attribute, if we have a write delegation */ 2725 2726 mutex_enter(&rp->r_statev4_lock); 2727 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2728 rp->r_deleg_change++; 2729 2730 mutex_exit(&rp->r_statev4_lock); 2731 2732 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2733 return (EINTR); 2734 2735 /* 2736 * Bypass VM if caching has been disabled (e.g., locking) or if 2737 * using client-side direct I/O and the file is not mmap'd and 2738 * there are no cached pages. 2739 */ 2740 if ((vp->v_flag & VNOCACHE) || 2741 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2742 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2743 size_t bufsize; 2744 int count; 2745 u_offset_t org_offset; 2746 stable_how4 stab_comm; 2747 nfs4_fwrite: 2748 if (rp->r_flags & R4STALE) { 2749 resid = uiop->uio_resid; 2750 offset = uiop->uio_loffset; 2751 error = rp->r_error; 2752 goto bottom; 2753 } 2754 2755 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2756 base = kmem_alloc(bufsize, KM_SLEEP); 2757 do { 2758 if (ioflag & FDSYNC) 2759 stab_comm = DATA_SYNC4; 2760 else 2761 stab_comm = FILE_SYNC4; 2762 resid = uiop->uio_resid; 2763 offset = uiop->uio_loffset; 2764 count = MIN(uiop->uio_resid, bufsize); 2765 org_offset = uiop->uio_loffset; 2766 error = uiomove(base, count, UIO_WRITE, uiop); 2767 if (!error) { 2768 error = nfs4write(vp, base, org_offset, 2769 count, cr, &stab_comm); 2770 if (!error) { 2771 mutex_enter(&rp->r_statelock); 2772 if (rp->r_size < uiop->uio_loffset) 2773 rp->r_size = uiop->uio_loffset; 2774 mutex_exit(&rp->r_statelock); 2775 } 2776 } 2777 } while (!error && uiop->uio_resid > 0); 2778 kmem_free(base, bufsize); 2779 goto bottom; 2780 } 2781 2782 bsize = vp->v_vfsp->vfs_bsize; 2783 2784 do { 2785 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2786 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2787 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2788 2789 resid = uiop->uio_resid; 2790 offset = uiop->uio_loffset; 2791 2792 if (rp->r_flags & R4STALE) { 2793 error = rp->r_error; 2794 break; 2795 } 2796 2797 /* 2798 * Don't create dirty pages faster than they 2799 * can be cleaned so that the system doesn't 2800 * get imbalanced. If the async queue is 2801 * maxed out, then wait for it to drain before 2802 * creating more dirty pages. Also, wait for 2803 * any threads doing pagewalks in the vop_getattr 2804 * entry points so that they don't block for 2805 * long periods. 2806 */ 2807 mutex_enter(&rp->r_statelock); 2808 while ((mi->mi_max_threads != 0 && 2809 rp->r_awcount > 2 * mi->mi_max_threads) || 2810 rp->r_gcount > 0) 2811 cv_wait(&rp->r_cv, &rp->r_statelock); 2812 mutex_exit(&rp->r_statelock); 2813 2814 if (segmap_kpm) { 2815 int pon = uiop->uio_loffset & PAGEOFFSET; 2816 size_t pn = MIN(PAGESIZE - pon, uiop->uio_resid); 2817 int pagecreate; 2818 2819 mutex_enter(&rp->r_statelock); 2820 pagecreate = (pon == 0) && (pn == PAGESIZE || 2821 uiop->uio_loffset + pn >= rp->r_size); 2822 mutex_exit(&rp->r_statelock); 2823 2824 base = segmap_getmapflt(segkmap, vp, off + on, 2825 pn, !pagecreate, S_WRITE); 2826 2827 error = writerp4(rp, base + pon, n, uiop, pagecreate); 2828 2829 } else { 2830 base = segmap_getmapflt(segkmap, vp, off + on, 2831 n, 0, S_READ); 2832 error = writerp4(rp, base + on, n, uiop, 0); 2833 } 2834 2835 if (!error) { 2836 if (mi->mi_flags & MI4_NOAC) 2837 flags = SM_WRITE; 2838 else if ((uiop->uio_loffset % bsize) == 0 || 2839 IS_SWAPVP(vp)) { 2840 /* 2841 * Have written a whole block. 2842 * Start an asynchronous write 2843 * and mark the buffer to 2844 * indicate that it won't be 2845 * needed again soon. 2846 */ 2847 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2848 } else 2849 flags = 0; 2850 if ((ioflag & (FSYNC|FDSYNC)) || 2851 (rp->r_flags & R4OUTOFSPACE)) { 2852 flags &= ~SM_ASYNC; 2853 flags |= SM_WRITE; 2854 } 2855 error = segmap_release(segkmap, base, flags); 2856 } else { 2857 (void) segmap_release(segkmap, base, 0); 2858 /* 2859 * In the event that we got an access error while 2860 * faulting in a page for a write-only file just 2861 * force a write. 2862 */ 2863 if (error == EACCES) 2864 goto nfs4_fwrite; 2865 } 2866 } while (!error && uiop->uio_resid > 0); 2867 2868 bottom: 2869 if (error) { 2870 uiop->uio_resid = resid + remainder; 2871 uiop->uio_loffset = offset; 2872 } else { 2873 uiop->uio_resid += remainder; 2874 2875 mutex_enter(&rp->r_statev4_lock); 2876 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 2877 gethrestime(&rp->r_attr.va_mtime); 2878 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 2879 } 2880 mutex_exit(&rp->r_statev4_lock); 2881 } 2882 2883 nfs_rw_exit(&rp->r_lkserlock); 2884 2885 return (error); 2886 } 2887 2888 /* 2889 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 2890 */ 2891 static int 2892 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 2893 int flags, cred_t *cr) 2894 { 2895 struct buf *bp; 2896 int error; 2897 page_t *savepp; 2898 uchar_t fsdata; 2899 stable_how4 stab_comm; 2900 2901 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 2902 bp = pageio_setup(pp, len, vp, flags); 2903 ASSERT(bp != NULL); 2904 2905 /* 2906 * pageio_setup should have set b_addr to 0. This 2907 * is correct since we want to do I/O on a page 2908 * boundary. bp_mapin will use this addr to calculate 2909 * an offset, and then set b_addr to the kernel virtual 2910 * address it allocated for us. 2911 */ 2912 ASSERT(bp->b_un.b_addr == 0); 2913 2914 bp->b_edev = 0; 2915 bp->b_dev = 0; 2916 bp->b_lblkno = lbtodb(off); 2917 bp->b_file = vp; 2918 bp->b_offset = (offset_t)off; 2919 bp_mapin(bp); 2920 2921 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 2922 freemem > desfree) 2923 stab_comm = UNSTABLE4; 2924 else 2925 stab_comm = FILE_SYNC4; 2926 2927 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 2928 2929 bp_mapout(bp); 2930 pageio_done(bp); 2931 2932 if (stab_comm == UNSTABLE4) 2933 fsdata = C_DELAYCOMMIT; 2934 else 2935 fsdata = C_NOCOMMIT; 2936 2937 savepp = pp; 2938 do { 2939 pp->p_fsdata = fsdata; 2940 } while ((pp = pp->p_next) != savepp); 2941 2942 return (error); 2943 } 2944 2945 /* 2946 */ 2947 static int 2948 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 2949 { 2950 nfs4_open_owner_t *oop; 2951 nfs4_open_stream_t *osp; 2952 rnode4_t *rp = VTOR4(vp); 2953 mntinfo4_t *mi = VTOMI4(vp); 2954 int reopen_needed; 2955 2956 ASSERT(curproc->p_zone == mi->mi_zone); 2957 2958 2959 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 2960 if (!oop) 2961 return (EIO); 2962 2963 /* returns with 'os_sync_lock' held */ 2964 osp = find_open_stream(oop, rp); 2965 if (!osp) { 2966 open_owner_rele(oop); 2967 return (EIO); 2968 } 2969 2970 if (osp->os_failed_reopen) { 2971 mutex_exit(&osp->os_sync_lock); 2972 open_stream_rele(osp, rp); 2973 open_owner_rele(oop); 2974 return (EIO); 2975 } 2976 2977 /* 2978 * Determine whether a reopen is needed. If this 2979 * is a delegation open stream, then the os_delegation bit 2980 * should be set. 2981 */ 2982 2983 reopen_needed = osp->os_delegation; 2984 2985 mutex_exit(&osp->os_sync_lock); 2986 open_owner_rele(oop); 2987 2988 if (reopen_needed) { 2989 nfs4_error_zinit(ep); 2990 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 2991 mutex_enter(&osp->os_sync_lock); 2992 if (ep->error || ep->stat || osp->os_failed_reopen) { 2993 mutex_exit(&osp->os_sync_lock); 2994 open_stream_rele(osp, rp); 2995 return (EIO); 2996 } 2997 mutex_exit(&osp->os_sync_lock); 2998 } 2999 open_stream_rele(osp, rp); 3000 3001 return (0); 3002 } 3003 3004 /* 3005 * Write to file. Writes to remote server in largest size 3006 * chunks that the server can handle. Write is synchronous. 3007 */ 3008 static int 3009 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3010 stable_how4 *stab_comm) 3011 { 3012 mntinfo4_t *mi; 3013 COMPOUND4args_clnt args; 3014 COMPOUND4res_clnt res; 3015 WRITE4args *wargs; 3016 WRITE4res *wres; 3017 nfs_argop4 argop[2]; 3018 nfs_resop4 *resop; 3019 int tsize; 3020 stable_how4 stable; 3021 rnode4_t *rp; 3022 int doqueue = 1; 3023 bool_t needrecov; 3024 nfs4_recov_state_t recov_state; 3025 nfs4_stateid_types_t sid_types; 3026 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3027 3028 rp = VTOR4(vp); 3029 mi = VTOMI4(vp); 3030 3031 ASSERT(curproc->p_zone == mi->mi_zone); 3032 3033 stable = *stab_comm; 3034 *stab_comm = FILE_SYNC4; 3035 3036 needrecov = FALSE; 3037 recov_state.rs_flags = 0; 3038 recov_state.rs_num_retry_despite_err = 0; 3039 nfs4_init_stateid_types(&sid_types); 3040 3041 recov_retry: 3042 args.ctag = TAG_WRITE; 3043 args.array_len = 2; 3044 args.array = argop; 3045 3046 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3047 &recov_state, NULL); 3048 if (e.error) 3049 return (e.error); 3050 3051 /* 0. putfh target fh */ 3052 argop[0].argop = OP_CPUTFH; 3053 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3054 3055 /* 1. write */ 3056 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3057 3058 do { 3059 3060 wargs->offset = (offset4)offset; 3061 wargs->data_val = base; 3062 3063 if (mi->mi_io_kstats) { 3064 mutex_enter(&mi->mi_lock); 3065 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3066 mutex_exit(&mi->mi_lock); 3067 } 3068 3069 if ((vp->v_flag & VNOCACHE) || 3070 (rp->r_flags & R4DIRECTIO) || 3071 (mi->mi_flags & MI4_DIRECTIO)) 3072 tsize = MIN(mi->mi_stsize, count); 3073 else 3074 tsize = MIN(mi->mi_curwrite, count); 3075 wargs->data_len = (uint_t)tsize; 3076 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3077 3078 if (mi->mi_io_kstats) { 3079 mutex_enter(&mi->mi_lock); 3080 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3081 mutex_exit(&mi->mi_lock); 3082 } 3083 3084 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3085 if (e.error && !needrecov) { 3086 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3087 &recov_state, needrecov); 3088 return (e.error); 3089 } 3090 3091 3092 /* 3093 * Do handling of OLD_STATEID outside 3094 * of the normal recovery framework. 3095 * 3096 * If write receives a BAD stateid error while using a 3097 * delegation stateid, retry using the open stateid (if it 3098 * exists). If it doesn't have an open stateid, reopen the 3099 * file first, then retry. 3100 */ 3101 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3102 sid_types.cur_sid_type != SPEC_SID) { 3103 nfs4_save_stateid(&wargs->stateid, &sid_types); 3104 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3105 &recov_state, needrecov); 3106 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3107 goto recov_retry; 3108 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3109 sid_types.cur_sid_type == DEL_SID) { 3110 nfs4_save_stateid(&wargs->stateid, &sid_types); 3111 mutex_enter(&rp->r_statev4_lock); 3112 rp->r_deleg_return_pending = TRUE; 3113 mutex_exit(&rp->r_statev4_lock); 3114 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3115 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3116 &recov_state, needrecov); 3117 (void) xdr_free(xdr_COMPOUND4res_clnt, 3118 (caddr_t)&res); 3119 return (EIO); 3120 } 3121 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3122 &recov_state, needrecov); 3123 /* hold needed for nfs4delegreturn_thread */ 3124 VN_HOLD(vp); 3125 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3126 NFS4_DR_DISCARD), FALSE); 3127 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3128 goto recov_retry; 3129 } 3130 3131 if (needrecov) { 3132 bool_t abort; 3133 3134 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3135 "nfs4write: client got error %d, res.status %d" 3136 ", so start recovery", e.error, res.status)); 3137 3138 abort = nfs4_start_recovery(&e, 3139 VTOMI4(vp), vp, NULL, &wargs->stateid, 3140 NULL, OP_WRITE, NULL); 3141 if (!e.error) { 3142 e.error = geterrno4(res.status); 3143 (void) xdr_free(xdr_COMPOUND4res_clnt, 3144 (caddr_t)&res); 3145 } 3146 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3147 &recov_state, needrecov); 3148 if (abort == FALSE) 3149 goto recov_retry; 3150 return (e.error); 3151 } 3152 3153 if (res.status) { 3154 e.error = geterrno4(res.status); 3155 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3156 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3157 &recov_state, needrecov); 3158 return (e.error); 3159 } 3160 3161 resop = &res.array[1]; /* write res */ 3162 wres = &resop->nfs_resop4_u.opwrite; 3163 3164 if ((int)wres->count > tsize) { 3165 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3166 3167 zcmn_err(getzoneid(), CE_WARN, 3168 "nfs4write: server wrote %u, requested was %u", 3169 (int)wres->count, tsize); 3170 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3171 &recov_state, needrecov); 3172 return (EIO); 3173 } 3174 if (wres->committed == UNSTABLE4) { 3175 *stab_comm = UNSTABLE4; 3176 if (wargs->stable == DATA_SYNC4 || 3177 wargs->stable == FILE_SYNC4) { 3178 (void) xdr_free(xdr_COMPOUND4res_clnt, 3179 (caddr_t)&res); 3180 zcmn_err(getzoneid(), CE_WARN, 3181 "nfs4write: server %s did not commit " 3182 "to stable storage", 3183 rp->r_server->sv_hostname); 3184 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3185 &recov_state, needrecov); 3186 return (EIO); 3187 } 3188 } 3189 3190 tsize = (int)wres->count; 3191 count -= tsize; 3192 base += tsize; 3193 offset += tsize; 3194 if (mi->mi_io_kstats) { 3195 mutex_enter(&mi->mi_lock); 3196 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3197 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3198 tsize; 3199 mutex_exit(&mi->mi_lock); 3200 } 3201 lwp_stat_update(LWP_STAT_OUBLK, 1); 3202 mutex_enter(&rp->r_statelock); 3203 if (rp->r_flags & R4HAVEVERF) { 3204 if (rp->r_writeverf != wres->writeverf) { 3205 nfs4_set_mod(vp); 3206 rp->r_writeverf = wres->writeverf; 3207 } 3208 } else { 3209 rp->r_writeverf = wres->writeverf; 3210 rp->r_flags |= R4HAVEVERF; 3211 } 3212 PURGE_ATTRCACHE4_LOCKED(rp); 3213 rp->r_flags |= R4WRITEMODIFIED; 3214 gethrestime(&rp->r_attr.va_mtime); 3215 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3216 mutex_exit(&rp->r_statelock); 3217 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3218 } while (count); 3219 3220 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, needrecov); 3221 3222 return (e.error); 3223 } 3224 3225 /* 3226 * Read from a file. Reads data in largest chunks our interface can handle. 3227 */ 3228 static int 3229 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3230 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3231 { 3232 mntinfo4_t *mi; 3233 COMPOUND4args_clnt args; 3234 COMPOUND4res_clnt res; 3235 READ4args *rargs; 3236 nfs_argop4 argop[2]; 3237 int tsize; 3238 int doqueue; 3239 rnode4_t *rp; 3240 int data_len; 3241 bool_t is_eof; 3242 bool_t needrecov = FALSE; 3243 nfs4_recov_state_t recov_state; 3244 nfs4_stateid_types_t sid_types; 3245 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3246 3247 rp = VTOR4(vp); 3248 mi = VTOMI4(vp); 3249 doqueue = 1; 3250 3251 ASSERT(curproc->p_zone == mi->mi_zone); 3252 3253 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3254 3255 args.array_len = 2; 3256 args.array = argop; 3257 3258 nfs4_init_stateid_types(&sid_types); 3259 3260 recov_state.rs_flags = 0; 3261 recov_state.rs_num_retry_despite_err = 0; 3262 3263 recov_retry: 3264 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3265 &recov_state, NULL); 3266 if (e.error) 3267 return (e.error); 3268 3269 /* putfh target fh */ 3270 argop[0].argop = OP_CPUTFH; 3271 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3272 3273 /* read */ 3274 argop[1].argop = OP_READ; 3275 rargs = &argop[1].nfs_argop4_u.opread; 3276 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3277 OP_READ, &sid_types, async); 3278 3279 do { 3280 if (mi->mi_io_kstats) { 3281 mutex_enter(&mi->mi_lock); 3282 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3283 mutex_exit(&mi->mi_lock); 3284 } 3285 3286 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3287 "nfs4read: %s call, rp %s", 3288 needrecov ? "recov" : "first", 3289 rnode4info(rp))); 3290 3291 if ((vp->v_flag & VNOCACHE) || 3292 (rp->r_flags & R4DIRECTIO) || 3293 (mi->mi_flags & MI4_DIRECTIO)) 3294 tsize = MIN(mi->mi_tsize, count); 3295 else 3296 tsize = MIN(mi->mi_curread, count); 3297 rargs->offset = (offset4)offset; 3298 rargs->count = (count4)tsize; 3299 rargs->res_data_val_alt = NULL; 3300 rargs->res_mblk = NULL; 3301 rargs->res_uiop = NULL; 3302 rargs->res_maxsize = 0; 3303 if (uiop) 3304 rargs->res_uiop = uiop; 3305 else 3306 rargs->res_data_val_alt = base; 3307 rargs->res_maxsize = tsize; 3308 3309 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3310 #ifdef DEBUG 3311 if (nfs4read_error_inject) { 3312 res.status = nfs4read_error_inject; 3313 nfs4read_error_inject = 0; 3314 } 3315 #endif 3316 3317 if (mi->mi_io_kstats) { 3318 mutex_enter(&mi->mi_lock); 3319 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3320 mutex_exit(&mi->mi_lock); 3321 } 3322 3323 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3324 if (e.error != 0 && !needrecov) { 3325 nfs4_end_fop(mi, vp, NULL, OH_READ, 3326 &recov_state, needrecov); 3327 return (e.error); 3328 } 3329 3330 /* 3331 * Do proper retry for OLD and BAD stateid errors outside 3332 * of the normal recovery framework. There are two differences 3333 * between async and sync reads. The first is that we allow 3334 * retry on BAD_STATEID for async reads, but not sync reads. 3335 * The second is that we mark the file dead for a failed 3336 * attempt with a special stateid for sync reads, but just 3337 * return EIO for async reads. 3338 * 3339 * If a sync read receives a BAD stateid error while using a 3340 * delegation stateid, retry using the open stateid (if it 3341 * exists). If it doesn't have an open stateid, reopen the 3342 * file first, then retry. 3343 */ 3344 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3345 res.status == NFS4ERR_BAD_STATEID) && async) { 3346 nfs4_end_fop(mi, vp, NULL, OH_READ, 3347 &recov_state, needrecov); 3348 if (sid_types.cur_sid_type == SPEC_SID) { 3349 (void) xdr_free(xdr_COMPOUND4res_clnt, 3350 (caddr_t)&res); 3351 return (EIO); 3352 } 3353 nfs4_save_stateid(&rargs->stateid, &sid_types); 3354 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3355 goto recov_retry; 3356 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3357 !async && sid_types.cur_sid_type != SPEC_SID) { 3358 nfs4_save_stateid(&rargs->stateid, &sid_types); 3359 nfs4_end_fop(mi, vp, NULL, OH_READ, 3360 &recov_state, needrecov); 3361 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3362 goto recov_retry; 3363 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3364 sid_types.cur_sid_type == DEL_SID) { 3365 nfs4_save_stateid(&rargs->stateid, &sid_types); 3366 mutex_enter(&rp->r_statev4_lock); 3367 rp->r_deleg_return_pending = TRUE; 3368 mutex_exit(&rp->r_statev4_lock); 3369 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3370 nfs4_end_fop(mi, vp, NULL, OH_READ, 3371 &recov_state, needrecov); 3372 (void) xdr_free(xdr_COMPOUND4res_clnt, 3373 (caddr_t)&res); 3374 return (EIO); 3375 } 3376 nfs4_end_fop(mi, vp, NULL, OH_READ, 3377 &recov_state, needrecov); 3378 /* hold needed for nfs4delegreturn_thread */ 3379 VN_HOLD(vp); 3380 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3381 NFS4_DR_DISCARD), FALSE); 3382 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3383 goto recov_retry; 3384 } 3385 if (needrecov) { 3386 bool_t abort; 3387 3388 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3389 "nfs4read: initiating recovery\n")); 3390 3391 abort = nfs4_start_recovery(&e, 3392 mi, vp, NULL, &rargs->stateid, 3393 NULL, OP_READ, NULL); 3394 nfs4_end_fop(mi, vp, NULL, OH_READ, 3395 &recov_state, needrecov); 3396 /* 3397 * Do not retry if we got OLD_STATEID using a special 3398 * stateid. This avoids looping with a broken server. 3399 */ 3400 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3401 sid_types.cur_sid_type == SPEC_SID) 3402 abort = TRUE; 3403 3404 if (abort == FALSE) { 3405 /* 3406 * Need to retry all possible stateids in 3407 * case the recovery error wasn't stateid 3408 * related or the stateids have become 3409 * stale (server reboot). 3410 */ 3411 nfs4_init_stateid_types(&sid_types); 3412 (void) xdr_free(xdr_COMPOUND4res_clnt, 3413 (caddr_t)&res); 3414 goto recov_retry; 3415 } 3416 3417 if (!e.error) { 3418 e.error = geterrno4(res.status); 3419 (void) xdr_free(xdr_COMPOUND4res_clnt, 3420 (caddr_t)&res); 3421 } 3422 return (e.error); 3423 } 3424 3425 if (res.status) { 3426 e.error = geterrno4(res.status); 3427 nfs4_end_fop(mi, vp, NULL, OH_READ, 3428 &recov_state, needrecov); 3429 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3430 return (e.error); 3431 } 3432 3433 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3434 count -= data_len; 3435 if (base) 3436 base += data_len; 3437 offset += data_len; 3438 if (mi->mi_io_kstats) { 3439 mutex_enter(&mi->mi_lock); 3440 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3441 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3442 mutex_exit(&mi->mi_lock); 3443 } 3444 lwp_stat_update(LWP_STAT_INBLK, 1); 3445 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3446 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3447 3448 } while (count && !is_eof); 3449 3450 *residp = count; 3451 3452 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3453 3454 return (e.error); 3455 } 3456 3457 /* ARGSUSED */ 3458 static int 3459 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 3460 { 3461 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 3462 return (EIO); 3463 switch (cmd) { 3464 case _FIODIRECTIO: 3465 return (nfs4_directio(vp, (int)arg, cr)); 3466 default: 3467 return (ENOTTY); 3468 } 3469 } 3470 3471 static int 3472 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr) 3473 { 3474 int error; 3475 rnode4_t *rp = VTOR4(vp); 3476 3477 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 3478 return (EIO); 3479 /* 3480 * If it has been specified that the return value will 3481 * just be used as a hint, and we are only being asked 3482 * for size, fsid or rdevid, then return the client's 3483 * notion of these values without checking to make sure 3484 * that the attribute cache is up to date. 3485 * The whole point is to avoid an over the wire GETATTR 3486 * call. 3487 */ 3488 if (flags & ATTR_HINT) { 3489 if (vap->va_mask == 3490 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) { 3491 mutex_enter(&rp->r_statelock); 3492 if (vap->va_mask | AT_SIZE) 3493 vap->va_size = rp->r_size; 3494 if (vap->va_mask | AT_FSID) 3495 vap->va_fsid = rp->r_attr.va_fsid; 3496 if (vap->va_mask | AT_RDEV) 3497 vap->va_rdev = rp->r_attr.va_rdev; 3498 mutex_exit(&rp->r_statelock); 3499 return (0); 3500 } 3501 } 3502 3503 /* 3504 * Only need to flush pages if asking for the mtime 3505 * and if there any dirty pages or any outstanding 3506 * asynchronous (write) requests for this file. 3507 */ 3508 if (vap->va_mask & AT_MTIME) { 3509 rp = VTOR4(vp); 3510 if (nfs4_has_pages(vp)) { 3511 mutex_enter(&rp->r_statev4_lock); 3512 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3513 mutex_exit(&rp->r_statev4_lock); 3514 if (rp->r_flags & R4DIRTY || 3515 rp->r_awcount > 0) { 3516 mutex_enter(&rp->r_statelock); 3517 rp->r_gcount++; 3518 mutex_exit(&rp->r_statelock); 3519 error = 3520 nfs4_putpage(vp, (u_offset_t)0, 3521 0, 0, cr); 3522 mutex_enter(&rp->r_statelock); 3523 if (error && (error == ENOSPC || 3524 error == EDQUOT)) { 3525 if (!rp->r_error) 3526 rp->r_error = error; 3527 } 3528 if (--rp->r_gcount == 0) 3529 cv_broadcast(&rp->r_cv); 3530 mutex_exit(&rp->r_statelock); 3531 } 3532 } else { 3533 mutex_exit(&rp->r_statev4_lock); 3534 } 3535 } 3536 } 3537 return (nfs4getattr(vp, vap, cr)); 3538 } 3539 3540 int 3541 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3542 { 3543 /* 3544 * If these are the only two bits cleared 3545 * on the server then return 0 (OK) else 3546 * return 1 (BAD). 3547 */ 3548 on_client &= ~(S_ISUID|S_ISGID); 3549 if (on_client == from_server) 3550 return (0); 3551 else 3552 return (1); 3553 } 3554 3555 /*ARGSUSED4*/ 3556 static int 3557 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3558 caller_context_t *ct) 3559 { 3560 if (vap->va_mask & AT_NOSET) 3561 return (EINVAL); 3562 3563 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 3564 return (EIO); 3565 3566 /* 3567 * Don't call secpolicy_vnode_setattr, the client cannot 3568 * use its cached attributes to make security decisions 3569 * as the server may be faking mode bits or mapping uid/gid. 3570 * Always just let the server to the checking. 3571 * If we provide the ability to remove basic priviledges 3572 * to setattr (e.g. basic without chmod) then we will 3573 * need to add a check here before calling the server. 3574 */ 3575 3576 return (nfs4setattr(vp, vap, flags, cr, NULL)); 3577 } 3578 3579 /* 3580 * To replace the "guarded" version 3 setattr, we use two types of compound 3581 * setattr requests: 3582 * 1. The "normal" setattr, used when the size of the file isn't being 3583 * changed - { Putfh <fh>; Setattr; Getattr }/ 3584 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3585 * with only ctime as the argument. If the server ctime differs from 3586 * what is cached on the client, the verify will fail, but we would 3587 * already have the ctime from the preceding getattr, so just set it 3588 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3589 * Setattr; Getattr }. 3590 * 3591 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3592 * this setattr and NULL if they are not. 3593 */ 3594 static int 3595 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3596 vsecattr_t *vsap) 3597 { 3598 COMPOUND4args_clnt args; 3599 COMPOUND4res_clnt res, *resp = NULL; 3600 nfs4_ga_res_t *garp = NULL; 3601 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3602 nfs_argop4 argop[5]; 3603 int verify_argop = -1; 3604 int setattr_argop = 1; 3605 nfs_resop4 *resop; 3606 vattr_t va; 3607 rnode4_t *rp; 3608 int doqueue = 1; 3609 uint_t mask = vap->va_mask; 3610 mode_t omode; 3611 vsecattr_t *vsp; 3612 timestruc_t ctime; 3613 bool_t needrecov = FALSE; 3614 nfs4_recov_state_t recov_state; 3615 nfs4_stateid_types_t sid_types; 3616 stateid4 stateid; 3617 hrtime_t t; 3618 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3619 servinfo4_t *svp; 3620 bitmap4 supp_attrs; 3621 3622 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 3623 rp = VTOR4(vp); 3624 nfs4_init_stateid_types(&sid_types); 3625 3626 /* 3627 * Only need to flush pages if there are any pages and 3628 * if the file is marked as dirty in some fashion. The 3629 * file must be flushed so that we can accurately 3630 * determine the size of the file and the cached data 3631 * after the SETATTR returns. A file is considered to 3632 * be dirty if it is either marked with R4DIRTY, has 3633 * outstanding i/o's active, or is mmap'd. In this 3634 * last case, we can't tell whether there are dirty 3635 * pages, so we flush just to be sure. 3636 */ 3637 if (nfs4_has_pages(vp) && 3638 ((rp->r_flags & R4DIRTY) || 3639 rp->r_count > 0 || 3640 rp->r_mapcnt > 0)) { 3641 ASSERT(vp->v_type != VCHR); 3642 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr); 3643 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3644 mutex_enter(&rp->r_statelock); 3645 if (!rp->r_error) 3646 rp->r_error = e.error; 3647 mutex_exit(&rp->r_statelock); 3648 } 3649 } 3650 3651 if (mask & AT_SIZE) { 3652 /* 3653 * Verification setattr compound for non-deleg AT_SIZE: 3654 * { Putfh; Getattr; Verify; Setattr; Getattr } 3655 * Set ctime local here (outside the do_again label) 3656 * so that subsequent retries (after failed VERIFY) 3657 * will use ctime from GETATTR results (from failed 3658 * verify compound) as VERIFY arg. 3659 * If file has delegation, then VERIFY(time_metadata) 3660 * is of little added value, so don't bother. 3661 */ 3662 mutex_enter(&rp->r_statev4_lock); 3663 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3664 rp->r_deleg_return_pending) { 3665 numops = 5; 3666 ctime = rp->r_attr.va_ctime; 3667 } 3668 mutex_exit(&rp->r_statev4_lock); 3669 } 3670 3671 recov_state.rs_flags = 0; 3672 recov_state.rs_num_retry_despite_err = 0; 3673 3674 args.ctag = TAG_SETATTR; 3675 do_again: 3676 recov_retry: 3677 setattr_argop = numops - 2; 3678 3679 args.array = argop; 3680 args.array_len = numops; 3681 3682 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3683 if (e.error) 3684 return (e.error); 3685 3686 3687 /* putfh target fh */ 3688 argop[0].argop = OP_CPUTFH; 3689 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3690 3691 if (numops == 5) { 3692 /* 3693 * We only care about the ctime, but need to get mtime 3694 * and size for proper cache update. 3695 */ 3696 /* getattr */ 3697 argop[1].argop = OP_GETATTR; 3698 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3699 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3700 3701 /* verify - set later in loop */ 3702 verify_argop = 2; 3703 } 3704 3705 /* setattr */ 3706 svp = rp->r_server; 3707 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3708 supp_attrs = svp->sv_supp_attrs; 3709 nfs_rw_exit(&svp->sv_lock); 3710 3711 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3712 supp_attrs, &e.error, &sid_types); 3713 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3714 if (e.error) { 3715 /* req time field(s) overflow - return immediately */ 3716 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3717 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3718 opsetattr.obj_attributes); 3719 return (e.error); 3720 } 3721 omode = rp->r_attr.va_mode; 3722 3723 /* getattr */ 3724 argop[numops-1].argop = OP_GETATTR; 3725 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3726 /* 3727 * If we are setting the ACL (indicated only by vsap != NULL), request 3728 * the ACL in this getattr. The ACL returned from this getattr will be 3729 * used in updating the ACL cache. 3730 */ 3731 if (vsap != NULL) 3732 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3733 FATTR4_ACL_MASK; 3734 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3735 3736 /* 3737 * setattr iterates if the object size is set and the cached ctime 3738 * does not match the file ctime. In that case, verify the ctime first. 3739 */ 3740 3741 do { 3742 if (verify_argop != -1) { 3743 /* 3744 * Verify that the ctime match before doing setattr. 3745 */ 3746 va.va_mask = AT_CTIME; 3747 va.va_ctime = ctime; 3748 svp = rp->r_server; 3749 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3750 supp_attrs = svp->sv_supp_attrs; 3751 nfs_rw_exit(&svp->sv_lock); 3752 e.error = nfs4args_verify(&argop[verify_argop], &va, 3753 OP_VERIFY, supp_attrs); 3754 if (e.error) { 3755 /* req time field(s) overflow - return */ 3756 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3757 needrecov); 3758 break; 3759 } 3760 } 3761 3762 doqueue = 1; 3763 3764 t = gethrtime(); 3765 3766 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3767 3768 /* 3769 * Purge the access cache and ACL cache if changing either the 3770 * owner of the file, the group owner, or the mode. These may 3771 * change the access permissions of the file, so purge old 3772 * information and start over again. 3773 */ 3774 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3775 (void) nfs4_access_purge_rp(rp); 3776 if (rp->r_secattr != NULL) { 3777 mutex_enter(&rp->r_statelock); 3778 vsp = rp->r_secattr; 3779 rp->r_secattr = NULL; 3780 mutex_exit(&rp->r_statelock); 3781 if (vsp != NULL) 3782 nfs4_acl_free_cache(vsp); 3783 } 3784 } 3785 3786 /* 3787 * If res.array_len == numops, then everything succeeded, 3788 * except for possibly the final getattr. If only the 3789 * last getattr failed, give up, and don't try recovery. 3790 */ 3791 if (res.array_len == numops) { 3792 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3793 needrecov); 3794 if (! e.error) 3795 resp = &res; 3796 break; 3797 } 3798 3799 /* 3800 * if either rpc call failed or completely succeeded - done 3801 */ 3802 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3803 if (e.error) { 3804 PURGE_ATTRCACHE4(vp); 3805 if (!needrecov) { 3806 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3807 needrecov); 3808 break; 3809 } 3810 } 3811 3812 /* 3813 * Do proper retry for OLD_STATEID outside of the normal 3814 * recovery framework. 3815 */ 3816 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3817 sid_types.cur_sid_type != SPEC_SID && 3818 sid_types.cur_sid_type != NO_SID) { 3819 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3820 needrecov); 3821 nfs4_save_stateid(&stateid, &sid_types); 3822 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3823 opsetattr.obj_attributes); 3824 if (verify_argop != -1) { 3825 nfs4args_verify_free(&argop[verify_argop]); 3826 verify_argop = -1; 3827 } 3828 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3829 goto recov_retry; 3830 } 3831 3832 if (needrecov) { 3833 bool_t abort; 3834 3835 abort = nfs4_start_recovery(&e, 3836 VTOMI4(vp), vp, NULL, NULL, NULL, 3837 OP_SETATTR, NULL); 3838 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3839 needrecov); 3840 /* 3841 * Do not retry if we failed with OLD_STATEID using 3842 * a special stateid. This is done to avoid looping 3843 * with a broken server. 3844 */ 3845 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3846 (sid_types.cur_sid_type == SPEC_SID || 3847 sid_types.cur_sid_type == NO_SID)) 3848 abort = TRUE; 3849 if (!e.error) { 3850 if (res.status == NFS4ERR_BADOWNER) 3851 nfs4_log_badowner(VTOMI4(vp), 3852 OP_SETATTR); 3853 3854 e.error = geterrno4(res.status); 3855 (void) xdr_free(xdr_COMPOUND4res_clnt, 3856 (caddr_t)&res); 3857 } 3858 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3859 opsetattr.obj_attributes); 3860 if (verify_argop != -1) { 3861 nfs4args_verify_free(&argop[verify_argop]); 3862 verify_argop = -1; 3863 } 3864 if (abort == FALSE) { 3865 /* 3866 * Need to retry all possible stateids in 3867 * case the recovery error wasn't stateid 3868 * related or the stateids have become 3869 * stale (server reboot). 3870 */ 3871 nfs4_init_stateid_types(&sid_types); 3872 goto recov_retry; 3873 } 3874 return (e.error); 3875 } 3876 3877 /* 3878 * Need to call nfs4_end_op before nfs4getattr to 3879 * avoid potential nfs4_start_op deadlock. See RFE 3880 * 4777612. Calls to nfs4_invalidate_pages() and 3881 * nfs4_purge_stale_fh() might also generate over the 3882 * wire calls which my cause nfs4_start_op() deadlock. 3883 */ 3884 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3885 3886 /* 3887 * Check to update lease. 3888 */ 3889 resp = &res; 3890 if (res.status == NFS4_OK) { 3891 break; 3892 } 3893 3894 /* 3895 * Check if verify failed to see if try again 3896 */ 3897 if ((verify_argop == -1) || (res.array_len != 3)) { 3898 /* 3899 * can't continue... 3900 */ 3901 if (res.status == NFS4ERR_BADOWNER) 3902 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 3903 3904 e.error = geterrno4(res.status); 3905 } else { 3906 /* 3907 * When the verify request fails, the client ctime is 3908 * not in sync with the server. This is the same as 3909 * the version 3 "not synchronized" error, and we 3910 * handle it in a similar manner (XXX do we need to???). 3911 * Use the ctime returned in the first getattr for 3912 * the input to the next verify. 3913 * If we couldn't get the attributes, then we give up 3914 * because we can't complete the operation as required. 3915 */ 3916 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 3917 } 3918 if (e.error) { 3919 PURGE_ATTRCACHE4(vp); 3920 nfs4_purge_stale_fh(e.error, vp, cr); 3921 } else { 3922 /* 3923 * retry with a new verify value 3924 */ 3925 ctime = garp->n4g_va.va_ctime; 3926 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3927 resp = NULL; 3928 } 3929 if (!e.error) { 3930 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3931 opsetattr.obj_attributes); 3932 if (verify_argop != -1) { 3933 nfs4args_verify_free(&argop[verify_argop]); 3934 verify_argop = -1; 3935 } 3936 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3937 goto do_again; 3938 } 3939 } while (!e.error); 3940 3941 if (e.error) { 3942 /* 3943 * If we are here, rfs4call has an irrecoverable error - return 3944 */ 3945 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3946 opsetattr.obj_attributes); 3947 if (verify_argop != -1) { 3948 nfs4args_verify_free(&argop[verify_argop]); 3949 verify_argop = -1; 3950 } 3951 if (resp) 3952 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 3953 return (e.error); 3954 } 3955 3956 3957 3958 /* 3959 * If changing the size of the file, invalidate 3960 * any local cached data which is no longer part 3961 * of the file. We also possibly invalidate the 3962 * last page in the file. We could use 3963 * pvn_vpzero(), but this would mark the page as 3964 * modified and require it to be written back to 3965 * the server for no particularly good reason. 3966 * This way, if we access it, then we bring it 3967 * back in. A read should be cheaper than a 3968 * write. 3969 */ 3970 if (mask & AT_SIZE) { 3971 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 3972 } 3973 3974 /* either no error or one of the postop getattr failed */ 3975 3976 /* 3977 * XXX Perform a simplified version of wcc checking. Instead of 3978 * have another getattr to get pre-op, just purge cache if 3979 * any of the ops prior to and including the getattr failed. 3980 * If the getattr succeeded then update the attrcache accordingly. 3981 */ 3982 3983 garp = NULL; 3984 if (res.status == NFS4_OK) { 3985 /* 3986 * Last getattr 3987 */ 3988 resop = &res.array[numops - 1]; 3989 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 3990 } 3991 /* 3992 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 3993 * rather than filling it. See the function itself for details. 3994 */ 3995 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 3996 if (garp != NULL) { 3997 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 3998 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 3999 vs_ace4_destroy(&garp->n4g_vsa); 4000 } else { 4001 if (vsap != NULL) { 4002 /* 4003 * The ACL was supposed to be set and to be 4004 * returned in the last getattr of this 4005 * compound, but for some reason the getattr 4006 * result doesn't contain the ACL. In this 4007 * case, purge the ACL cache. 4008 */ 4009 if (rp->r_secattr != NULL) { 4010 mutex_enter(&rp->r_statelock); 4011 vsp = rp->r_secattr; 4012 rp->r_secattr = NULL; 4013 mutex_exit(&rp->r_statelock); 4014 if (vsp != NULL) 4015 nfs4_acl_free_cache(vsp); 4016 } 4017 } 4018 } 4019 } 4020 4021 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4022 /* 4023 * Set the size, rather than relying on getting it updated 4024 * via a GETATTR. With delegations the client tries to 4025 * suppress GETATTR calls. 4026 */ 4027 mutex_enter(&rp->r_statelock); 4028 rp->r_size = vap->va_size; 4029 mutex_exit(&rp->r_statelock); 4030 } 4031 4032 /* 4033 * Can free up request args and res 4034 */ 4035 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4036 opsetattr.obj_attributes); 4037 if (verify_argop != -1) { 4038 nfs4args_verify_free(&argop[verify_argop]); 4039 verify_argop = -1; 4040 } 4041 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4042 4043 /* 4044 * Some servers will change the mode to clear the setuid 4045 * and setgid bits when changing the uid or gid. The 4046 * client needs to compensate appropriately. 4047 */ 4048 if (mask & (AT_UID | AT_GID)) { 4049 int terror, do_setattr; 4050 4051 do_setattr = 0; 4052 va.va_mask = AT_MODE; 4053 terror = nfs4getattr(vp, &va, cr); 4054 if (!terror && 4055 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4056 (!(mask & AT_MODE) && va.va_mode != omode))) { 4057 va.va_mask = AT_MODE; 4058 if (mask & AT_MODE) { 4059 /* 4060 * We asked the mode to be changed and what 4061 * we just got from the server in getattr is 4062 * not what we wanted it to be, so set it now. 4063 */ 4064 va.va_mode = vap->va_mode; 4065 do_setattr = 1; 4066 } else { 4067 /* 4068 * We did not ask the mode to be changed, 4069 * Check to see that the server just cleared 4070 * I_SUID and I_GUID from it. If not then 4071 * set mode to omode with UID/GID cleared. 4072 */ 4073 if (nfs4_compare_modes(va.va_mode, omode)) { 4074 omode &= ~(S_ISUID|S_ISGID); 4075 va.va_mode = omode; 4076 do_setattr = 1; 4077 } 4078 } 4079 4080 if (do_setattr) 4081 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4082 } 4083 } 4084 4085 return (e.error); 4086 } 4087 4088 /* ARGSUSED */ 4089 static int 4090 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr) 4091 { 4092 COMPOUND4args_clnt args; 4093 COMPOUND4res_clnt res; 4094 int doqueue; 4095 uint32_t acc, resacc, argacc; 4096 rnode4_t *rp; 4097 cred_t *cred, *ncr; 4098 nfs4_access_type_t cacc; 4099 int num_ops; 4100 nfs_argop4 argop[3]; 4101 nfs_resop4 *resop; 4102 bool_t needrecov = FALSE, do_getattr; 4103 nfs4_recov_state_t recov_state; 4104 int rpc_error; 4105 hrtime_t t; 4106 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4107 mntinfo4_t *mi = VTOMI4(vp); 4108 4109 if (curproc->p_zone != mi->mi_zone) 4110 return (EIO); 4111 4112 acc = 0; 4113 if (mode & VREAD) 4114 acc |= ACCESS4_READ; 4115 if (mode & VWRITE) { 4116 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4117 return (EROFS); 4118 if (vp->v_type == VDIR) 4119 acc |= ACCESS4_DELETE; 4120 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4121 } 4122 if (mode & VEXEC) { 4123 if (vp->v_type == VDIR) 4124 acc |= ACCESS4_LOOKUP; 4125 else 4126 acc |= ACCESS4_EXECUTE; 4127 } 4128 4129 if (VTOR4(vp)->r_acache != NULL) { 4130 e.error = nfs4_validate_caches(vp, cr); 4131 if (e.error) 4132 return (e.error); 4133 } 4134 4135 rp = VTOR4(vp); 4136 if (vp->v_type == VDIR) { 4137 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4138 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4139 } else { 4140 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4141 ACCESS4_EXECUTE; 4142 } 4143 recov_state.rs_flags = 0; 4144 recov_state.rs_num_retry_despite_err = 0; 4145 4146 cred = cr; 4147 ncr = crnetadjust(cred); 4148 4149 tryagain: 4150 cacc = nfs4_access_check(rp, acc, cred); 4151 if (cacc == NFS4_ACCESS_ALLOWED) 4152 return (0); 4153 if (cacc == NFS4_ACCESS_DENIED) { 4154 /* 4155 * If the cred can be adjusted, try again 4156 * with the new cred. 4157 */ 4158 if (ncr != NULL) { 4159 cred = ncr; 4160 ncr = NULL; 4161 goto tryagain; 4162 } 4163 return (EACCES); 4164 } 4165 4166 recov_retry: 4167 /* 4168 * Don't take with r_statev4_lock here. r_deleg_type could 4169 * change as soon as lock is released. Since it is an int, 4170 * there is no atomicity issue. 4171 */ 4172 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4173 num_ops = do_getattr ? 3 : 2; 4174 4175 args.ctag = TAG_ACCESS; 4176 4177 args.array_len = num_ops; 4178 args.array = argop; 4179 4180 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4181 &recov_state, NULL)) { 4182 return (e.error); 4183 } 4184 4185 /* putfh target fh */ 4186 argop[0].argop = OP_CPUTFH; 4187 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4188 4189 /* access */ 4190 argop[1].argop = OP_ACCESS; 4191 argop[1].nfs_argop4_u.opaccess.access = argacc; 4192 4193 /* getattr */ 4194 if (do_getattr) { 4195 argop[2].argop = OP_GETATTR; 4196 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4197 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4198 } 4199 4200 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4201 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4202 rnode4info(VTOR4(vp)))); 4203 4204 doqueue = 1; 4205 t = gethrtime(); 4206 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4207 rpc_error = e.error; 4208 4209 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4210 if (needrecov) { 4211 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4212 "nfs4_access: initiating recovery\n")); 4213 4214 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4215 NULL, OP_ACCESS, NULL) == FALSE) { 4216 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4217 &recov_state, needrecov); 4218 if (!e.error) 4219 (void) xdr_free(xdr_COMPOUND4res_clnt, 4220 (caddr_t)&res); 4221 goto recov_retry; 4222 } 4223 } 4224 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4225 4226 if (e.error) 4227 goto out; 4228 4229 if (res.status) { 4230 e.error = geterrno4(res.status); 4231 /* 4232 * This might generate over the wire calls throught 4233 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4234 * here to avoid a deadlock. 4235 */ 4236 nfs4_purge_stale_fh(e.error, vp, cr); 4237 goto out; 4238 } 4239 resop = &res.array[1]; /* access res */ 4240 4241 resacc = resop->nfs_resop4_u.opaccess.access; 4242 4243 if (do_getattr) { 4244 resop++; /* getattr res */ 4245 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4246 t, cr, FALSE, NULL); 4247 } 4248 4249 if (!e.error) { 4250 nfs4_access_cache(rp, argacc, resacc, cred); 4251 /* XXX check the supported bits too? */ 4252 if ((acc & resacc) != acc) { 4253 /* 4254 * The following code implements the semantic 4255 * that a setuid root program has *at least* the 4256 * permissions of the user that is running the 4257 * program. See rfs3call() for more portions 4258 * of the implementation of this functionality. 4259 */ 4260 /* XXX-LP */ 4261 if (ncr != NULL) { 4262 (void) xdr_free(xdr_COMPOUND4res_clnt, 4263 (caddr_t)&res); 4264 cred = ncr; 4265 ncr = NULL; 4266 goto tryagain; 4267 } 4268 e.error = EACCES; 4269 } 4270 } 4271 4272 out: 4273 if (!rpc_error) 4274 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4275 4276 if (cred != cr) 4277 crfree(cred); 4278 4279 return (e.error); 4280 } 4281 4282 static int 4283 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr) 4284 { 4285 COMPOUND4args_clnt args; 4286 COMPOUND4res_clnt res; 4287 int doqueue; 4288 rnode4_t *rp; 4289 nfs_argop4 argop[3]; 4290 nfs_resop4 *resop; 4291 READLINK4res *lr_res; 4292 nfs4_ga_res_t *garp; 4293 uint_t len; 4294 char *linkdata; 4295 bool_t needrecov = FALSE; 4296 nfs4_recov_state_t recov_state; 4297 hrtime_t t; 4298 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4299 4300 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 4301 return (EIO); 4302 /* 4303 * Can't readlink anything other than a symbolic link. 4304 */ 4305 if (vp->v_type != VLNK) 4306 return (EINVAL); 4307 4308 rp = VTOR4(vp); 4309 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4310 e.error = nfs4_validate_caches(vp, cr); 4311 if (e.error) 4312 return (e.error); 4313 mutex_enter(&rp->r_statelock); 4314 if (rp->r_symlink.contents != NULL) { 4315 e.error = uiomove(rp->r_symlink.contents, 4316 rp->r_symlink.len, UIO_READ, uiop); 4317 mutex_exit(&rp->r_statelock); 4318 return (e.error); 4319 } 4320 mutex_exit(&rp->r_statelock); 4321 } 4322 recov_state.rs_flags = 0; 4323 recov_state.rs_num_retry_despite_err = 0; 4324 4325 recov_retry: 4326 args.array_len = 3; 4327 args.array = argop; 4328 args.ctag = TAG_READLINK; 4329 4330 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4331 if (e.error) { 4332 return (e.error); 4333 } 4334 4335 /* 0. putfh symlink fh */ 4336 argop[0].argop = OP_CPUTFH; 4337 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4338 4339 /* 1. readlink */ 4340 argop[1].argop = OP_READLINK; 4341 4342 /* 2. getattr */ 4343 argop[2].argop = OP_GETATTR; 4344 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4345 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4346 4347 doqueue = 1; 4348 4349 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4350 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4351 rnode4info(VTOR4(vp)))); 4352 4353 t = gethrtime(); 4354 4355 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4356 4357 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4358 if (needrecov) { 4359 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4360 "nfs4_readlink: initiating recovery\n")); 4361 4362 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4363 NULL, OP_READLINK, NULL) == FALSE) { 4364 if (!e.error) 4365 (void) xdr_free(xdr_COMPOUND4res_clnt, 4366 (caddr_t)&res); 4367 4368 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4369 needrecov); 4370 goto recov_retry; 4371 } 4372 } 4373 4374 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4375 4376 if (e.error) 4377 return (e.error); 4378 4379 /* 4380 * There is an path in the code below which calls 4381 * nfs4_purge_stale_fh(), which may generate otw calls through 4382 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4383 * here to avoid nfs4_start_op() deadlock. 4384 */ 4385 4386 if (res.status && (res.array_len < args.array_len)) { 4387 /* 4388 * either Putfh or Link failed 4389 */ 4390 e.error = geterrno4(res.status); 4391 nfs4_purge_stale_fh(e.error, vp, cr); 4392 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4393 return (e.error); 4394 } 4395 4396 resop = &res.array[1]; /* readlink res */ 4397 lr_res = &resop->nfs_resop4_u.opreadlink; 4398 4399 /* 4400 * treat symlink names as data 4401 */ 4402 linkdata = utf8_to_str(&lr_res->link, &len, NULL); 4403 if (linkdata != NULL) { 4404 int uio_len = len - 1; 4405 /* len includes null byte, which we won't uiomove */ 4406 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4407 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4408 mutex_enter(&rp->r_statelock); 4409 if (rp->r_symlink.contents == NULL) { 4410 rp->r_symlink.contents = linkdata; 4411 rp->r_symlink.len = uio_len; 4412 rp->r_symlink.size = len; 4413 mutex_exit(&rp->r_statelock); 4414 } else { 4415 mutex_exit(&rp->r_statelock); 4416 kmem_free(linkdata, len); 4417 } 4418 } else { 4419 kmem_free(linkdata, len); 4420 } 4421 } 4422 if (res.status == NFS4_OK) { 4423 resop++; /* getattr res */ 4424 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4425 } 4426 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4427 4428 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4429 4430 /* 4431 * The over the wire error for attempting to readlink something 4432 * other than a symbolic link is ENXIO. However, we need to 4433 * return EINVAL instead of ENXIO, so we map it here. 4434 */ 4435 return (e.error == ENXIO ? EINVAL : e.error); 4436 } 4437 4438 /* 4439 * Flush local dirty pages to stable storage on the server. 4440 * 4441 * If FNODSYNC is specified, then there is nothing to do because 4442 * metadata changes are not cached on the client before being 4443 * sent to the server. 4444 */ 4445 static int 4446 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr) 4447 { 4448 int error; 4449 4450 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4451 return (0); 4452 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 4453 return (EIO); 4454 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4455 if (!error) 4456 error = VTOR4(vp)->r_error; 4457 return (error); 4458 } 4459 4460 /* 4461 * Weirdness: if the file was removed or the target of a rename 4462 * operation while it was open, it got renamed instead. Here we 4463 * remove the renamed file. 4464 */ 4465 static void 4466 nfs4_inactive(vnode_t *vp, cred_t *cr) 4467 { 4468 rnode4_t *rp; 4469 4470 ASSERT(vp != DNLC_NO_VNODE); 4471 4472 rp = VTOR4(vp); 4473 4474 if (IS_SHADOW(vp, rp)) { 4475 sv_inactive(vp); 4476 return; 4477 } 4478 4479 /* 4480 * If this is coming from the wrong zone, we let someone in the right 4481 * zone take care of it asynchronously. We can get here due to 4482 * VN_RELE() being called from pageout() or fsflush(). This call may 4483 * potentially turn into an expensive no-op if, for instance, v_count 4484 * gets incremented in the meantime, but it's still correct. 4485 */ 4486 if (curproc->p_zone != VTOMI4(vp)->mi_zone) { 4487 nfs4_async_inactive(vp, cr); 4488 return; 4489 } 4490 4491 /* 4492 * Some of the cleanup steps might require over-the-wire 4493 * operations. Since VOP_INACTIVE can get called as a result of 4494 * other over-the-wire operations (e.g., an attribute cache update 4495 * can lead to a DNLC purge), doing those steps now would lead to a 4496 * nested call to the recovery framework, which can deadlock. So 4497 * do any over-the-wire cleanups asynchronously, in a separate 4498 * thread. 4499 */ 4500 4501 mutex_enter(&rp->r_os_lock); 4502 mutex_enter(&rp->r_statelock); 4503 mutex_enter(&rp->r_statev4_lock); 4504 4505 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4506 mutex_exit(&rp->r_statev4_lock); 4507 mutex_exit(&rp->r_statelock); 4508 mutex_exit(&rp->r_os_lock); 4509 nfs4_async_inactive(vp, cr); 4510 return; 4511 } 4512 4513 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4514 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4515 mutex_exit(&rp->r_statev4_lock); 4516 mutex_exit(&rp->r_statelock); 4517 mutex_exit(&rp->r_os_lock); 4518 nfs4_async_inactive(vp, cr); 4519 return; 4520 } 4521 4522 if (rp->r_unldvp != NULL) { 4523 mutex_exit(&rp->r_statev4_lock); 4524 mutex_exit(&rp->r_statelock); 4525 mutex_exit(&rp->r_os_lock); 4526 nfs4_async_inactive(vp, cr); 4527 return; 4528 } 4529 mutex_exit(&rp->r_statev4_lock); 4530 mutex_exit(&rp->r_statelock); 4531 mutex_exit(&rp->r_os_lock); 4532 4533 rp4_addfree(rp, cr); 4534 } 4535 4536 /* 4537 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4538 * various bits of state. The caller must not refer to vp after this call. 4539 */ 4540 4541 void 4542 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4543 { 4544 rnode4_t *rp = VTOR4(vp); 4545 nfs4_recov_state_t recov_state; 4546 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4547 vnode_t *unldvp; 4548 char *unlname; 4549 cred_t *unlcred; 4550 COMPOUND4args_clnt args; 4551 COMPOUND4res_clnt res, *resp; 4552 nfs_argop4 argop[2]; 4553 int doqueue; 4554 #ifdef DEBUG 4555 char *name; 4556 #endif 4557 4558 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 4559 ASSERT(!IS_SHADOW(vp, rp)); 4560 4561 #ifdef DEBUG 4562 name = fn_name(VTOSV(vp)->sv_name); 4563 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4564 "release vnode %s", name)); 4565 kmem_free(name, MAXNAMELEN); 4566 #endif 4567 4568 if (vp->v_type == VREG) { 4569 bool_t recov_failed = FALSE; 4570 4571 e.error = nfs4close_all(vp, cr); 4572 if (e.error) { 4573 /* Check to see if recovery failed */ 4574 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4575 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4576 recov_failed = TRUE; 4577 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4578 if (!recov_failed) { 4579 mutex_enter(&rp->r_statelock); 4580 if (rp->r_flags & R4RECOVERR) 4581 recov_failed = TRUE; 4582 mutex_exit(&rp->r_statelock); 4583 } 4584 if (recov_failed) { 4585 NFS4_DEBUG(nfs4_client_recov_debug, 4586 (CE_NOTE, "nfs4_inactive_otw: " 4587 "close failed (recovery failure)")); 4588 } 4589 } 4590 } 4591 4592 redo: 4593 if (rp->r_unldvp == NULL) { 4594 rp4_addfree(rp, cr); 4595 return; 4596 } 4597 4598 /* 4599 * Save the vnode pointer for the directory where the 4600 * unlinked-open file got renamed, then set it to NULL 4601 * to prevent another thread from getting here before 4602 * we're done with the remove. While we have the 4603 * statelock, make local copies of the pertinent rnode 4604 * fields. If we weren't to do this in an atomic way, the 4605 * the unl* fields could become inconsistent with respect 4606 * to each other due to a race condition between this 4607 * code and nfs_remove(). See bug report 1034328. 4608 */ 4609 mutex_enter(&rp->r_statelock); 4610 if (rp->r_unldvp == NULL) { 4611 mutex_exit(&rp->r_statelock); 4612 rp4_addfree(rp, cr); 4613 return; 4614 } 4615 4616 unldvp = rp->r_unldvp; 4617 rp->r_unldvp = NULL; 4618 unlname = rp->r_unlname; 4619 rp->r_unlname = NULL; 4620 unlcred = rp->r_unlcred; 4621 rp->r_unlcred = NULL; 4622 mutex_exit(&rp->r_statelock); 4623 4624 /* 4625 * If there are any dirty pages left, then flush 4626 * them. This is unfortunate because they just 4627 * may get thrown away during the remove operation, 4628 * but we have to do this for correctness. 4629 */ 4630 if (nfs4_has_pages(vp) && 4631 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4632 ASSERT(vp->v_type != VCHR); 4633 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr); 4634 if (e.error) { 4635 mutex_enter(&rp->r_statelock); 4636 if (!rp->r_error) 4637 rp->r_error = e.error; 4638 mutex_exit(&rp->r_statelock); 4639 } 4640 } 4641 4642 recov_state.rs_flags = 0; 4643 recov_state.rs_num_retry_despite_err = 0; 4644 recov_retry_remove: 4645 /* 4646 * Do the remove operation on the renamed file 4647 */ 4648 args.ctag = TAG_INACTIVE; 4649 4650 /* 4651 * Remove ops: putfh dir; remove 4652 */ 4653 args.array_len = 2; 4654 args.array = argop; 4655 4656 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4657 if (e.error) { 4658 kmem_free(unlname, MAXNAMELEN); 4659 crfree(unlcred); 4660 VN_RELE(unldvp); 4661 /* 4662 * Try again; this time around r_unldvp will be NULL, so we'll 4663 * just call rp4_addfree() and return. 4664 */ 4665 goto redo; 4666 } 4667 4668 /* putfh directory */ 4669 argop[0].argop = OP_CPUTFH; 4670 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4671 4672 /* remove */ 4673 argop[1].argop = OP_CREMOVE; 4674 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4675 4676 doqueue = 1; 4677 resp = &res; 4678 4679 #if 0 /* notyet */ 4680 /* 4681 * Can't do this yet. We may be being called from 4682 * dnlc_purge_XXX while that routine is holding a 4683 * mutex lock to the nc_rele list. The calls to 4684 * nfs3_cache_wcc_data may result in calls to 4685 * dnlc_purge_XXX. This will result in a deadlock. 4686 */ 4687 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4688 if (e.error) { 4689 PURGE_ATTRCACHE4(unldvp); 4690 resp = NULL; 4691 } else if (res.status) { 4692 e.error = geterrno4(res.status); 4693 PURGE_ATTRCACHE4(unldvp); 4694 /* 4695 * This code is inactive right now 4696 * but if made active there should 4697 * be a nfs4_end_op() call before 4698 * nfs4_purge_stale_fh to avoid start_op() 4699 * deadlock. See BugId: 4948726 4700 */ 4701 nfs4_purge_stale_fh(error, unldvp, cr); 4702 } else { 4703 nfs_resop4 *resop; 4704 REMOVE4res *rm_res; 4705 4706 resop = &res.array[1]; 4707 rm_res = &resop->nfs_resop4_u.opremove; 4708 /* 4709 * Update directory cache attribute, 4710 * readdir and dnlc caches. 4711 */ 4712 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4713 } 4714 #else 4715 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4716 4717 PURGE_ATTRCACHE4(unldvp); 4718 #endif 4719 4720 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4721 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4722 NULL, NULL, OP_REMOVE, NULL) == FALSE) { 4723 if (!e.error) 4724 (void) xdr_free(xdr_COMPOUND4res_clnt, 4725 (caddr_t)&res); 4726 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4727 &recov_state, TRUE); 4728 goto recov_retry_remove; 4729 } 4730 } 4731 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4732 4733 /* 4734 * Release stuff held for the remove 4735 */ 4736 VN_RELE(unldvp); 4737 if (!e.error && resp) 4738 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4739 4740 kmem_free(unlname, MAXNAMELEN); 4741 crfree(unlcred); 4742 goto redo; 4743 } 4744 4745 /* 4746 * Remote file system operations having to do with directory manipulation. 4747 */ 4748 /* ARGSUSED3 */ 4749 static int 4750 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4751 int flags, vnode_t *rdir, cred_t *cr) 4752 { 4753 int error; 4754 vnode_t *vp, *avp = NULL; 4755 rnode4_t *drp; 4756 4757 *vpp = NULL; 4758 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 4759 return (EPERM); 4760 /* 4761 * if LOOKUP_XATTR, must replace dvp (object) with 4762 * object's attrdir before continuing with lookup 4763 */ 4764 if (flags & LOOKUP_XATTR) { 4765 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4766 if (error) 4767 return (error); 4768 4769 dvp = avp; 4770 4771 /* 4772 * If lookup is for "", just return dvp now. The attrdir 4773 * has already been activated (from nfs4lookup_xattr), and 4774 * the caller will RELE the original dvp -- not 4775 * the attrdir. So, set vpp and return. 4776 * Currently, when the LOOKUP_XATTR flag is 4777 * passed to VOP_LOOKUP, the name is always empty, and 4778 * shortcircuiting here avoids 3 unneeded lock/unlock 4779 * pairs. 4780 * 4781 * If a non-empty name was provided, then it is the 4782 * attribute name, and it will be looked up below. 4783 */ 4784 if (*nm == '\0') { 4785 *vpp = dvp; 4786 return (0); 4787 } 4788 4789 /* 4790 * The vfs layer never sends a name when asking for the 4791 * attrdir, so we should never get here (unless of course 4792 * name is passed at some time in future -- at which time 4793 * we'll blow up here). 4794 */ 4795 ASSERT(0); 4796 } 4797 4798 drp = VTOR4(dvp); 4799 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4800 return (EINTR); 4801 4802 error = nfs4lookup(dvp, nm, vpp, cr, 0); 4803 nfs_rw_exit(&drp->r_rwlock); 4804 4805 /* 4806 * If vnode is a device, create special vnode. 4807 */ 4808 if (!error && ISVDEV((*vpp)->v_type)) { 4809 vp = *vpp; 4810 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 4811 VN_RELE(vp); 4812 } 4813 4814 return (error); 4815 } 4816 4817 /* ARGSUSED */ 4818 static int 4819 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 4820 { 4821 int error; 4822 rnode4_t *drp; 4823 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 4824 mntinfo4_t *mi; 4825 4826 mi = VTOMI4(dvp); 4827 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR)) 4828 return (EINVAL); 4829 4830 drp = VTOR4(dvp); 4831 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4832 return (EINTR); 4833 4834 mutex_enter(&drp->r_statelock); 4835 /* 4836 * If the server doesn't support xattrs just return EINVAL 4837 */ 4838 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 4839 mutex_exit(&drp->r_statelock); 4840 nfs_rw_exit(&drp->r_rwlock); 4841 return (EINVAL); 4842 } 4843 4844 /* 4845 * If there is a cached xattr directory entry, 4846 * use it as long as the attributes are valid. If the 4847 * attributes are not valid, take the simple approach and 4848 * free the cached value and re-fetch a new value. 4849 * 4850 * We don't negative entry cache for now, if we did we 4851 * would need to check if the file has changed on every 4852 * lookup. But xattrs don't exist very often and failing 4853 * an openattr is not much more expensive than and NVERIFY or GETATTR 4854 * so do an openattr over the wire for now. 4855 */ 4856 if (drp->r_xattr_dir != NULL) { 4857 if (ATTRCACHE4_VALID(dvp)) { 4858 VN_HOLD(drp->r_xattr_dir); 4859 *vpp = drp->r_xattr_dir; 4860 mutex_exit(&drp->r_statelock); 4861 nfs_rw_exit(&drp->r_rwlock); 4862 return (0); 4863 } 4864 VN_RELE(drp->r_xattr_dir); 4865 drp->r_xattr_dir = NULL; 4866 } 4867 mutex_exit(&drp->r_statelock); 4868 4869 error = nfs4openattr(dvp, vpp, cflag, cr); 4870 4871 nfs_rw_exit(&drp->r_rwlock); 4872 4873 return (error); 4874 } 4875 4876 static int 4877 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 4878 { 4879 int error; 4880 rnode4_t *drp; 4881 4882 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 4883 4884 /* 4885 * If lookup is for "", just return dvp. Don't need 4886 * to send it over the wire, look it up in the dnlc, 4887 * or perform any access checks. 4888 */ 4889 if (*nm == '\0') { 4890 VN_HOLD(dvp); 4891 *vpp = dvp; 4892 return (0); 4893 } 4894 4895 /* 4896 * Can't do lookups in non-directories. 4897 */ 4898 if (dvp->v_type != VDIR) 4899 return (ENOTDIR); 4900 4901 /* 4902 * If lookup is for ".", just return dvp. Don't need 4903 * to send it over the wire or look it up in the dnlc, 4904 * just need to check access. 4905 */ 4906 if (nm[0] == '.' && nm[1] == '\0') { 4907 error = nfs4_access(dvp, VEXEC, 0, cr); 4908 if (error) 4909 return (error); 4910 VN_HOLD(dvp); 4911 *vpp = dvp; 4912 return (0); 4913 } 4914 4915 drp = VTOR4(dvp); 4916 if (!(drp->r_flags & R4LOOKUP)) { 4917 mutex_enter(&drp->r_statelock); 4918 drp->r_flags |= R4LOOKUP; 4919 mutex_exit(&drp->r_statelock); 4920 } 4921 4922 *vpp = NULL; 4923 /* 4924 * Lookup this name in the DNLC. If there is no entry 4925 * lookup over the wire. 4926 */ 4927 if (!skipdnlc) 4928 *vpp = dnlc_lookup(dvp, nm); 4929 if (*vpp == NULL) { 4930 /* 4931 * We need to go over the wire to lookup the name. 4932 */ 4933 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 4934 } 4935 4936 /* 4937 * We hit on the dnlc 4938 */ 4939 if (*vpp != DNLC_NO_VNODE || 4940 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 4941 /* 4942 * But our attrs may not be valid. 4943 */ 4944 if (ATTRCACHE4_VALID(dvp)) { 4945 error = nfs4_waitfor_purge_complete(dvp); 4946 if (error) { 4947 VN_RELE(*vpp); 4948 *vpp = NULL; 4949 return (error); 4950 } 4951 4952 /* 4953 * If after the purge completes, check to make sure 4954 * our attrs are still valid. 4955 */ 4956 if (ATTRCACHE4_VALID(dvp)) { 4957 /* 4958 * If we waited for a purge we may have 4959 * lost our vnode so look it up again. 4960 */ 4961 VN_RELE(*vpp); 4962 *vpp = dnlc_lookup(dvp, nm); 4963 if (*vpp == NULL) 4964 return (nfs4lookupnew_otw(dvp, 4965 nm, vpp, cr)); 4966 4967 /* 4968 * The access cache should almost always hit 4969 */ 4970 error = nfs4_access(dvp, VEXEC, 0, cr); 4971 4972 if (error) { 4973 VN_RELE(*vpp); 4974 *vpp = NULL; 4975 return (error); 4976 } 4977 if (*vpp == DNLC_NO_VNODE) { 4978 VN_RELE(*vpp); 4979 *vpp = NULL; 4980 return (ENOENT); 4981 } 4982 return (0); 4983 } 4984 } 4985 } 4986 4987 ASSERT(*vpp != NULL); 4988 4989 /* 4990 * We may have gotten here we have one of the following cases: 4991 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 4992 * need to validate them. 4993 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 4994 * must validate. 4995 * 4996 * Go to the server and check if the directory has changed, if 4997 * it hasn't we are done and can use the dnlc entry. 4998 */ 4999 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5000 } 5001 5002 /* 5003 * Go to the server and check if the directory has changed, if 5004 * it hasn't we are done and can use the dnlc entry. If it 5005 * has changed we get a new copy of its attributes and check 5006 * the access for VEXEC, then relookup the filename and 5007 * get its filehandle and attributes. 5008 * 5009 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5010 * if the NVERIFY failed we must 5011 * purge the caches 5012 * cache new attributes (will set r_time_attr_inval) 5013 * cache new access 5014 * recheck VEXEC access 5015 * add name to dnlc, possibly negative 5016 * if LOOKUP succeeded 5017 * cache new attributes 5018 * else 5019 * set a new r_time_attr_inval for dvp 5020 * check to make sure we have access 5021 * 5022 * The vpp returned is the vnode passed in if the directory is valid, 5023 * a new vnode if successful lookup, or NULL on error. 5024 */ 5025 static int 5026 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5027 { 5028 COMPOUND4args_clnt args; 5029 COMPOUND4res_clnt res; 5030 fattr4 *ver_fattr; 5031 fattr4_change dchange; 5032 int32_t *ptr; 5033 int argoplist_size = 7 * sizeof (nfs_argop4); 5034 nfs_argop4 *argop; 5035 int doqueue; 5036 mntinfo4_t *mi; 5037 nfs4_recov_state_t recov_state; 5038 hrtime_t t; 5039 int isdotdot; 5040 vnode_t *nvp; 5041 nfs_fh4 *fhp; 5042 nfs4_sharedfh_t *sfhp; 5043 nfs4_access_type_t cacc; 5044 rnode4_t *nrp; 5045 rnode4_t *drp = VTOR4(dvp); 5046 nfs4_ga_res_t *garp = NULL; 5047 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5048 5049 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 5050 ASSERT(nm != NULL); 5051 ASSERT(nm[0] != '\0'); 5052 ASSERT(dvp->v_type == VDIR); 5053 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5054 ASSERT(*vpp != NULL); 5055 5056 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5057 isdotdot = 1; 5058 args.ctag = TAG_LOOKUP_VPARENT; 5059 } else { 5060 /* 5061 * Do not allow crossing of server mount points. The 5062 * only visible entries in a SRVSTUB dir are . and .. 5063 * This code handles the non-.. case. We can't even get 5064 * this far if looking up ".". 5065 */ 5066 if (VTOR4(dvp)->r_flags & R4SRVSTUB) { 5067 VN_RELE(*vpp); 5068 *vpp = NULL; 5069 return (ENOENT); 5070 } 5071 isdotdot = 0; 5072 args.ctag = TAG_LOOKUP_VALID; 5073 } 5074 5075 mi = VTOMI4(dvp); 5076 recov_state.rs_flags = 0; 5077 recov_state.rs_num_retry_despite_err = 0; 5078 5079 nvp = NULL; 5080 5081 /* Save the original mount point security information */ 5082 (void) save_mnt_secinfo(mi->mi_curr_serv); 5083 5084 recov_retry: 5085 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5086 &recov_state, NULL); 5087 if (e.error) { 5088 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5089 VN_RELE(*vpp); 5090 *vpp = NULL; 5091 return (e.error); 5092 } 5093 5094 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5095 5096 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5097 args.array_len = 7; 5098 args.array = argop; 5099 5100 /* 0. putfh file */ 5101 argop[0].argop = OP_CPUTFH; 5102 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5103 5104 /* 1. nverify the change info */ 5105 argop[1].argop = OP_NVERIFY; 5106 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5107 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5108 ver_fattr->attrlist4 = (char *)&dchange; 5109 ptr = (int32_t *)&dchange; 5110 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5111 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5112 5113 /* 2. getattr directory */ 5114 argop[2].argop = OP_GETATTR; 5115 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5116 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5117 5118 /* 3. access directory */ 5119 argop[3].argop = OP_ACCESS; 5120 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5121 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5122 5123 /* 4. lookup name */ 5124 if (isdotdot) { 5125 argop[4].argop = OP_LOOKUPP; 5126 } else { 5127 argop[4].argop = OP_CLOOKUP; 5128 argop[4].nfs_argop4_u.opclookup.cname = nm; 5129 } 5130 5131 /* 5. resulting file handle */ 5132 argop[5].argop = OP_GETFH; 5133 5134 /* 6. resulting file attributes */ 5135 argop[6].argop = OP_GETATTR; 5136 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5137 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5138 5139 doqueue = 1; 5140 t = gethrtime(); 5141 5142 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5143 5144 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5145 /* 5146 * For WRONGSEC of a non-dotdot case, send secinfo directly 5147 * from this thread, do not go thru the recovery thread since 5148 * we need the nm information. 5149 * 5150 * Not doing dotdot case because there is no specification 5151 * for (PUTFH, SECINFO "..") yet. 5152 */ 5153 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5154 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) { 5155 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5156 &recov_state, FALSE); 5157 } else { 5158 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5159 &recov_state, TRUE); 5160 } 5161 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5162 kmem_free(argop, argoplist_size); 5163 if (!e.error) 5164 goto recov_retry; 5165 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5166 VN_RELE(*vpp); 5167 *vpp = NULL; 5168 return (e.error); 5169 } 5170 5171 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5172 OP_LOOKUP, NULL) == FALSE) { 5173 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5174 &recov_state, TRUE); 5175 5176 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5177 kmem_free(argop, argoplist_size); 5178 goto recov_retry; 5179 } 5180 } 5181 5182 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5183 5184 if (e.error || res.array_len == 0) { 5185 /* 5186 * If e.error isn't set, then reply has no ops (or we couldn't 5187 * be here). The only legal way to reply without an op array 5188 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5189 * be in the reply for all other status values. 5190 * 5191 * For valid replies without an ops array, return ENOTSUP 5192 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5193 * return EIO -- don't trust status. 5194 */ 5195 if (e.error == 0) 5196 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5197 ENOTSUP : EIO; 5198 VN_RELE(*vpp); 5199 *vpp = NULL; 5200 kmem_free(argop, argoplist_size); 5201 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5202 return (e.error); 5203 } 5204 5205 if (res.status != NFS4ERR_SAME) { 5206 e.error = geterrno4(res.status); 5207 5208 /* 5209 * The NVERIFY "failed" so the directory has changed 5210 * First make sure PUTFH succeeded and NVERIFY "failed" 5211 * cleanly. 5212 */ 5213 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5214 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5215 nfs4_purge_stale_fh(e.error, dvp, cr); 5216 VN_RELE(*vpp); 5217 *vpp = NULL; 5218 goto exit; 5219 } 5220 5221 /* 5222 * We know the NVERIFY "failed" so we must: 5223 * purge the caches (access and indirectly dnlc if needed) 5224 */ 5225 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5226 5227 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5228 nfs4_purge_stale_fh(e.error, dvp, cr); 5229 VN_RELE(*vpp); 5230 *vpp = NULL; 5231 goto exit; 5232 } 5233 5234 /* 5235 * Install new cached attributes for the directory 5236 */ 5237 nfs4_attr_cache(dvp, 5238 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5239 t, cr, FALSE, NULL); 5240 5241 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5242 nfs4_purge_stale_fh(e.error, dvp, cr); 5243 VN_RELE(*vpp); 5244 *vpp = NULL; 5245 e.error = geterrno4(res.status); 5246 goto exit; 5247 } 5248 5249 /* 5250 * Now we know the directory is valid, 5251 * cache new directory access 5252 */ 5253 nfs4_access_cache(drp, 5254 args.array[3].nfs_argop4_u.opaccess.access, 5255 res.array[3].nfs_resop4_u.opaccess.access, cr); 5256 5257 /* 5258 * recheck VEXEC access 5259 */ 5260 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5261 if (cacc != NFS4_ACCESS_ALLOWED) { 5262 /* 5263 * Directory permissions might have been revoked 5264 */ 5265 if (cacc == NFS4_ACCESS_DENIED) { 5266 e.error = EACCES; 5267 VN_RELE(*vpp); 5268 *vpp = NULL; 5269 goto exit; 5270 } 5271 5272 /* 5273 * Somehow we must not have asked for enough 5274 * so try a singleton ACCESS, should never happen. 5275 */ 5276 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5277 if (e.error) { 5278 VN_RELE(*vpp); 5279 *vpp = NULL; 5280 goto exit; 5281 } 5282 } 5283 5284 e.error = geterrno4(res.status); 5285 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5286 /* 5287 * The lookup failed, probably no entry 5288 */ 5289 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5290 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5291 } else { 5292 /* 5293 * Might be some other error, so remove 5294 * the dnlc entry to make sure we start all 5295 * over again, next time. 5296 */ 5297 dnlc_remove(dvp, nm); 5298 } 5299 VN_RELE(*vpp); 5300 *vpp = NULL; 5301 goto exit; 5302 } 5303 5304 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5305 /* 5306 * The file exists but we can't get its fh for 5307 * some unknown reason. Remove it from the dnlc 5308 * and error out to be safe. 5309 */ 5310 dnlc_remove(dvp, nm); 5311 VN_RELE(*vpp); 5312 *vpp = NULL; 5313 goto exit; 5314 } 5315 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5316 if (fhp->nfs_fh4_len == 0) { 5317 /* 5318 * The file exists but a bogus fh 5319 * some unknown reason. Remove it from the dnlc 5320 * and error out to be safe. 5321 */ 5322 e.error = ENOENT; 5323 dnlc_remove(dvp, nm); 5324 VN_RELE(*vpp); 5325 *vpp = NULL; 5326 goto exit; 5327 } 5328 sfhp = sfh4_get(fhp, mi); 5329 5330 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5331 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5332 5333 /* 5334 * Make the new rnode 5335 */ 5336 if (isdotdot) { 5337 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5338 if (e.error) { 5339 sfh4_rele(&sfhp); 5340 VN_RELE(*vpp); 5341 *vpp = NULL; 5342 goto exit; 5343 } 5344 /* 5345 * XXX if nfs4_make_dotdot uses an existing rnode 5346 * XXX it doesn't update the attributes. 5347 * XXX for now just save them again to save an OTW 5348 */ 5349 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5350 } else { 5351 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5352 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5353 /* 5354 * If v_type == VNON, then garp was NULL because 5355 * the last op in the compound failed and makenfs4node 5356 * could not find the vnode for sfhp. It created 5357 * a new vnode, so we have nothing to purge here. 5358 */ 5359 if (nvp->v_type == VNON) { 5360 vattr_t vattr; 5361 5362 vattr.va_mask = AT_TYPE; 5363 /* 5364 * N.B. We've already called nfs4_end_fop above. 5365 */ 5366 e.error = nfs4getattr(nvp, &vattr, cr); 5367 if (e.error) { 5368 sfh4_rele(&sfhp); 5369 VN_RELE(*vpp); 5370 *vpp = NULL; 5371 VN_RELE(nvp); 5372 goto exit; 5373 } 5374 nvp->v_type = vattr.va_type; 5375 } 5376 } 5377 sfh4_rele(&sfhp); 5378 5379 nrp = VTOR4(nvp); 5380 mutex_enter(&nrp->r_statev4_lock); 5381 if (!nrp->created_v4) { 5382 mutex_exit(&nrp->r_statev4_lock); 5383 dnlc_update(dvp, nm, nvp); 5384 } else 5385 mutex_exit(&nrp->r_statev4_lock); 5386 5387 VN_RELE(*vpp); 5388 *vpp = nvp; 5389 } else { 5390 hrtime_t now; 5391 hrtime_t delta = 0; 5392 5393 e.error = 0; 5394 5395 /* 5396 * Because the NVERIFY "succeeded" we know that the 5397 * directory attributes are still valid 5398 * so update r_time_attr_inval 5399 */ 5400 now = gethrtime(); 5401 mutex_enter(&drp->r_statelock); 5402 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5403 delta = now - drp->r_time_attr_saved; 5404 if (delta < mi->mi_acdirmin) 5405 delta = mi->mi_acdirmin; 5406 else if (delta > mi->mi_acdirmax) 5407 delta = mi->mi_acdirmax; 5408 } 5409 drp->r_time_attr_inval = now + delta; 5410 mutex_exit(&drp->r_statelock); 5411 dnlc_update(dvp, nm, *vpp); 5412 5413 /* 5414 * Even though we have a valid directory attr cache 5415 * and dnlc entry, we may not have access. 5416 * This should almost always hit the cache. 5417 */ 5418 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5419 if (e.error) { 5420 VN_RELE(*vpp); 5421 *vpp = NULL; 5422 } 5423 5424 if (*vpp == DNLC_NO_VNODE) { 5425 VN_RELE(*vpp); 5426 *vpp = NULL; 5427 e.error = ENOENT; 5428 } 5429 } 5430 5431 exit: 5432 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5433 kmem_free(argop, argoplist_size); 5434 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5435 return (e.error); 5436 } 5437 5438 /* 5439 * We need to go over the wire to lookup the name, but 5440 * while we are there verify the directory has not 5441 * changed but if it has, get new attributes and check access 5442 * 5443 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5444 * NVERIFY GETATTR ACCESS 5445 * 5446 * With the results: 5447 * if the NVERIFY failed we must purge the caches, add new attributes, 5448 * and cache new access. 5449 * set a new r_time_attr_inval 5450 * add name to dnlc, possibly negative 5451 * if LOOKUP succeeded 5452 * cache new attributes 5453 */ 5454 static int 5455 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5456 { 5457 COMPOUND4args_clnt args; 5458 COMPOUND4res_clnt res; 5459 fattr4 *ver_fattr; 5460 fattr4_change dchange; 5461 int32_t *ptr; 5462 nfs4_ga_res_t *garp = NULL; 5463 int argoplist_size = 9 * sizeof (nfs_argop4); 5464 nfs_argop4 *argop; 5465 int doqueue; 5466 mntinfo4_t *mi; 5467 nfs4_recov_state_t recov_state; 5468 hrtime_t t; 5469 int isdotdot; 5470 vnode_t *nvp; 5471 nfs_fh4 *fhp; 5472 nfs4_sharedfh_t *sfhp; 5473 nfs4_access_type_t cacc; 5474 rnode4_t *nrp; 5475 rnode4_t *drp = VTOR4(dvp); 5476 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5477 5478 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 5479 ASSERT(nm != NULL); 5480 ASSERT(nm[0] != '\0'); 5481 ASSERT(dvp->v_type == VDIR); 5482 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5483 ASSERT(*vpp == NULL); 5484 5485 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5486 isdotdot = 1; 5487 args.ctag = TAG_LOOKUP_PARENT; 5488 } else { 5489 /* 5490 * Do not allow crossing of server mount points. The 5491 * only visible entries in a SRVSTUB dir are . and .. 5492 * This code handles the non-.. case. We can't even get 5493 * this far if looking up ".". 5494 */ 5495 if (VTOR4(dvp)->r_flags & R4SRVSTUB) 5496 return (ENOENT); 5497 5498 isdotdot = 0; 5499 args.ctag = TAG_LOOKUP; 5500 } 5501 5502 mi = VTOMI4(dvp); 5503 recov_state.rs_flags = 0; 5504 recov_state.rs_num_retry_despite_err = 0; 5505 5506 nvp = NULL; 5507 5508 /* Save the original mount point security information */ 5509 (void) save_mnt_secinfo(mi->mi_curr_serv); 5510 5511 recov_retry: 5512 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5513 &recov_state, NULL); 5514 if (e.error) { 5515 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5516 return (e.error); 5517 } 5518 5519 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5520 5521 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5522 args.array_len = 9; 5523 args.array = argop; 5524 5525 /* 0. putfh file */ 5526 argop[0].argop = OP_CPUTFH; 5527 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5528 5529 /* 1. savefh for the nverify */ 5530 argop[1].argop = OP_SAVEFH; 5531 5532 /* 2. lookup name */ 5533 if (isdotdot) { 5534 argop[2].argop = OP_LOOKUPP; 5535 } else { 5536 argop[2].argop = OP_CLOOKUP; 5537 argop[2].nfs_argop4_u.opclookup.cname = nm; 5538 } 5539 5540 /* 3. resulting file handle */ 5541 argop[3].argop = OP_GETFH; 5542 5543 /* 4. resulting file attributes */ 5544 argop[4].argop = OP_GETATTR; 5545 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5546 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5547 5548 /* 5. restorefh back the directory for the nverify */ 5549 argop[5].argop = OP_RESTOREFH; 5550 5551 /* 6. nverify the change info */ 5552 argop[6].argop = OP_NVERIFY; 5553 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5554 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5555 ver_fattr->attrlist4 = (char *)&dchange; 5556 ptr = (int32_t *)&dchange; 5557 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5558 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5559 5560 /* 7. getattr directory */ 5561 argop[7].argop = OP_GETATTR; 5562 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5563 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5564 5565 /* 8. access directory */ 5566 argop[8].argop = OP_ACCESS; 5567 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5568 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5569 5570 doqueue = 1; 5571 t = gethrtime(); 5572 5573 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5574 5575 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5576 /* 5577 * For WRONGSEC of a non-dotdot case, send secinfo directly 5578 * from this thread, do not go thru the recovery thread since 5579 * we need the nm information. 5580 * 5581 * Not doing dotdot case because there is no specification 5582 * for (PUTFH, SECINFO "..") yet. 5583 */ 5584 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5585 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) { 5586 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5587 &recov_state, FALSE); 5588 } else { 5589 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5590 &recov_state, TRUE); 5591 } 5592 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5593 kmem_free(argop, argoplist_size); 5594 if (!e.error) 5595 goto recov_retry; 5596 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5597 return (e.error); 5598 } 5599 5600 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5601 OP_LOOKUP, NULL) == FALSE) { 5602 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5603 &recov_state, TRUE); 5604 5605 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5606 kmem_free(argop, argoplist_size); 5607 goto recov_retry; 5608 } 5609 } 5610 5611 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5612 5613 if (e.error || res.array_len == 0) { 5614 /* 5615 * If e.error isn't set, then reply has no ops (or we couldn't 5616 * be here). The only legal way to reply without an op array 5617 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5618 * be in the reply for all other status values. 5619 * 5620 * For valid replies without an ops array, return ENOTSUP 5621 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5622 * return EIO -- don't trust status. 5623 */ 5624 if (e.error == 0) 5625 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5626 ENOTSUP : EIO; 5627 5628 kmem_free(argop, argoplist_size); 5629 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5630 return (e.error); 5631 } 5632 5633 e.error = geterrno4(res.status); 5634 5635 /* 5636 * The PUTFH and SAVEFH may have failed. 5637 */ 5638 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5639 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5640 nfs4_purge_stale_fh(e.error, dvp, cr); 5641 goto exit; 5642 } 5643 5644 /* 5645 * Check if the file exists, if it does delay entering 5646 * into the dnlc until after we update the directory 5647 * attributes so we don't cause it to get purged immediately. 5648 */ 5649 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5650 /* 5651 * The lookup failed, probably no entry 5652 */ 5653 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5654 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5655 } 5656 goto exit; 5657 } 5658 5659 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5660 /* 5661 * The file exists but we can't get its fh for 5662 * some unknown reason. Error out to be safe. 5663 */ 5664 goto exit; 5665 } 5666 5667 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5668 if (fhp->nfs_fh4_len == 0) { 5669 /* 5670 * The file exists but a bogus fh 5671 * some unknown reason. Error out to be safe. 5672 */ 5673 e.error = EIO; 5674 goto exit; 5675 } 5676 sfhp = sfh4_get(fhp, mi); 5677 5678 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5679 sfh4_rele(&sfhp); 5680 e.error = EIO; 5681 goto exit; 5682 } 5683 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5684 5685 /* 5686 * The RESTOREFH may have failed 5687 */ 5688 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5689 sfh4_rele(&sfhp); 5690 e.error = EIO; 5691 goto exit; 5692 } 5693 5694 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5695 /* 5696 * First make sure the NVERIFY failed as we expected, 5697 * if it didn't then be conservative and error out 5698 * as we can't trust the directory. 5699 */ 5700 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5701 sfh4_rele(&sfhp); 5702 e.error = EIO; 5703 goto exit; 5704 } 5705 5706 /* 5707 * We know the NVERIFY "failed" so the directory has changed, 5708 * so we must: 5709 * purge the caches (access and indirectly dnlc if needed) 5710 */ 5711 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5712 5713 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5714 sfh4_rele(&sfhp); 5715 goto exit; 5716 } 5717 nfs4_attr_cache(dvp, 5718 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5719 t, cr, FALSE, NULL); 5720 5721 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5722 nfs4_purge_stale_fh(e.error, dvp, cr); 5723 sfh4_rele(&sfhp); 5724 e.error = geterrno4(res.status); 5725 goto exit; 5726 } 5727 5728 /* 5729 * Now we know the directory is valid, 5730 * cache new directory access 5731 */ 5732 nfs4_access_cache(drp, 5733 args.array[8].nfs_argop4_u.opaccess.access, 5734 res.array[8].nfs_resop4_u.opaccess.access, cr); 5735 5736 /* 5737 * recheck VEXEC access 5738 */ 5739 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5740 if (cacc != NFS4_ACCESS_ALLOWED) { 5741 /* 5742 * Directory permissions might have been revoked 5743 */ 5744 if (cacc == NFS4_ACCESS_DENIED) { 5745 sfh4_rele(&sfhp); 5746 e.error = EACCES; 5747 goto exit; 5748 } 5749 5750 /* 5751 * Somehow we must not have asked for enough 5752 * so try a singleton ACCESS should never happen 5753 */ 5754 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5755 if (e.error) { 5756 sfh4_rele(&sfhp); 5757 goto exit; 5758 } 5759 } 5760 5761 e.error = geterrno4(res.status); 5762 } else { 5763 hrtime_t now; 5764 hrtime_t delta = 0; 5765 5766 e.error = 0; 5767 5768 /* 5769 * Because the NVERIFY "succeeded" we know that the 5770 * directory attributes are still valid 5771 * so update r_time_attr_inval 5772 */ 5773 now = gethrtime(); 5774 mutex_enter(&drp->r_statelock); 5775 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5776 delta = now - drp->r_time_attr_saved; 5777 if (delta < mi->mi_acdirmin) 5778 delta = mi->mi_acdirmin; 5779 else if (delta > mi->mi_acdirmax) 5780 delta = mi->mi_acdirmax; 5781 } 5782 drp->r_time_attr_inval = now + delta; 5783 mutex_exit(&drp->r_statelock); 5784 5785 /* 5786 * Even though we have a valid directory attr cache, 5787 * we may not have access. 5788 * This should almost always hit the cache. 5789 */ 5790 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5791 if (e.error) { 5792 sfh4_rele(&sfhp); 5793 goto exit; 5794 } 5795 } 5796 5797 /* 5798 * Now we have successfully completed the lookup, if the 5799 * directory has changed we now have the valid attributes. 5800 * We also know we have directory access. 5801 * Create the new rnode and insert it in the dnlc. 5802 */ 5803 if (isdotdot) { 5804 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5805 if (e.error) { 5806 sfh4_rele(&sfhp); 5807 goto exit; 5808 } 5809 /* 5810 * XXX if nfs4_make_dotdot uses an existing rnode 5811 * XXX it doesn't update the attributes. 5812 * XXX for now just save them again to save an OTW 5813 */ 5814 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5815 } else { 5816 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5817 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5818 } 5819 sfh4_rele(&sfhp); 5820 5821 nrp = VTOR4(nvp); 5822 mutex_enter(&nrp->r_statev4_lock); 5823 if (!nrp->created_v4) { 5824 mutex_exit(&nrp->r_statev4_lock); 5825 dnlc_update(dvp, nm, nvp); 5826 } else 5827 mutex_exit(&nrp->r_statev4_lock); 5828 5829 *vpp = nvp; 5830 5831 exit: 5832 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5833 kmem_free(argop, argoplist_size); 5834 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5835 return (e.error); 5836 } 5837 5838 #ifdef DEBUG 5839 void 5840 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 5841 { 5842 uint_t i, len; 5843 zoneid_t zoneid = getzoneid(); 5844 char *s; 5845 5846 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 5847 for (i = 0; i < argcnt; i++) { 5848 nfs_argop4 *op = &argbase[i]; 5849 switch (op->argop) { 5850 case OP_CPUTFH: 5851 case OP_PUTFH: 5852 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 5853 break; 5854 case OP_PUTROOTFH: 5855 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 5856 break; 5857 case OP_CLOOKUP: 5858 s = op->nfs_argop4_u.opclookup.cname; 5859 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5860 break; 5861 case OP_LOOKUP: 5862 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 5863 &len, NULL); 5864 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5865 kmem_free(s, len); 5866 break; 5867 case OP_LOOKUPP: 5868 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 5869 break; 5870 case OP_GETFH: 5871 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 5872 break; 5873 case OP_GETATTR: 5874 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 5875 break; 5876 case OP_OPENATTR: 5877 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 5878 break; 5879 default: 5880 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 5881 op->argop); 5882 break; 5883 } 5884 } 5885 } 5886 #endif 5887 5888 /* 5889 * nfs4lookup_setup - constructs a multi-lookup compound request. 5890 * 5891 * Given the path "nm1/nm2/.../nmn", the following compound requests 5892 * may be created: 5893 * 5894 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 5895 * is faster, for now. 5896 * 5897 * l4_getattrs indicates the type of compound requested. 5898 * 5899 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 5900 * 5901 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 5902 * 5903 * total number of ops is n + 1. 5904 * 5905 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 5906 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 5907 * before the last component, and only get attributes 5908 * for the last component. Note that the second-to-last 5909 * pathname component is XATTR_RPATH, which does NOT go 5910 * over-the-wire as a lookup. 5911 * 5912 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 5913 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 5914 * 5915 * and total number of ops is n + 5. 5916 * 5917 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 5918 * attribute directory: create lookups plus an OPENATTR 5919 * replacing the last lookup. Note that the last pathname 5920 * component is XATTR_RPATH, which does NOT go over-the-wire 5921 * as a lookup. 5922 * 5923 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 5924 * Openattr; Getfh; Getattr } 5925 * 5926 * and total number of ops is n + 5. 5927 * 5928 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 5929 * nodes too. 5930 * 5931 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 5932 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 5933 * 5934 * and total number of ops is 3*n + 1. 5935 * 5936 * All cases: returns the index in the arg array of the final LOOKUP op, or 5937 * -1 if no LOOKUPs were used. 5938 */ 5939 int 5940 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 5941 { 5942 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 5943 nfs_argop4 *argbase, *argop; 5944 int arglen, argcnt; 5945 int n = 1; /* number of components */ 5946 int nga = 1; /* number of Getattr's in request */ 5947 char c = '\0', *s, *p; 5948 int lookup_idx = -1; 5949 int argoplist_size; 5950 5951 /* set lookuparg response result to 0 */ 5952 lookupargp->resp->status = NFS4_OK; 5953 5954 /* skip leading "/" or "." e.g. ".//./" if there is */ 5955 for (; ; nm++) { 5956 if (*nm != '/' && *nm != '.') 5957 break; 5958 5959 /* ".." is counted as 1 component */ 5960 if (*nm == '.' && *(nm + 1) == '.') 5961 break; 5962 } 5963 5964 /* 5965 * Find n = number of components - nm must be null terminated 5966 * Skip "." components. 5967 */ 5968 if (*nm != '\0') { 5969 for (n = 1, s = nm; *s != '\0'; s++) { 5970 if ((*s == '/') && (*(s + 1) != '/') && 5971 (*(s + 1) != '\0') && 5972 !(*(s + 1) == '.' && (*(s + 2) == '/' || 5973 *(s + 2) == '\0'))) 5974 n++; 5975 } 5976 } else 5977 n = 0; 5978 5979 /* 5980 * nga is number of components that need Getfh+Getattr 5981 */ 5982 switch (l4_getattrs) { 5983 case LKP4_NO_ATTRIBUTES: 5984 nga = 0; 5985 break; 5986 case LKP4_ALL_ATTRIBUTES: 5987 nga = n; 5988 /* 5989 * Always have at least 1 getfh, getattr pair 5990 */ 5991 if (nga == 0) 5992 nga++; 5993 break; 5994 case LKP4_LAST_ATTRDIR: 5995 case LKP4_LAST_NAMED_ATTR: 5996 nga = n+1; 5997 break; 5998 } 5999 6000 /* 6001 * If change to use the filehandle attr instead of getfh 6002 * the following line can be deleted. 6003 */ 6004 nga *= 2; 6005 6006 /* 6007 * calculate number of ops in request as 6008 * header + trailer + lookups + getattrs 6009 */ 6010 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6011 6012 argoplist_size = arglen * sizeof (nfs_argop4); 6013 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6014 lookupargp->argsp->array = argop; 6015 6016 argcnt = lookupargp->header_len; 6017 argop += argcnt; 6018 6019 /* 6020 * loop and create a lookup op and possibly getattr/getfh for 6021 * each component. Skip "." components. 6022 */ 6023 for (s = nm; *s != '\0'; s = p) { 6024 /* 6025 * Set up a pathname struct for each component if needed 6026 */ 6027 while (*s == '/') 6028 s++; 6029 if (*s == '\0') 6030 break; 6031 for (p = s; (*p != '/') && (*p != '\0'); p++); 6032 c = *p; 6033 *p = '\0'; 6034 6035 if (s[0] == '.' && s[1] == '\0') { 6036 *p = c; 6037 continue; 6038 } 6039 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6040 strcmp(s, XATTR_RPATH) == 0) { 6041 /* getfh XXX may not be needed in future */ 6042 argop->argop = OP_GETFH; 6043 argop++; 6044 argcnt++; 6045 6046 /* getattr */ 6047 argop->argop = OP_GETATTR; 6048 argop->nfs_argop4_u.opgetattr.attr_request = 6049 lookupargp->ga_bits; 6050 argop->nfs_argop4_u.opgetattr.mi = 6051 lookupargp->mi; 6052 argop++; 6053 argcnt++; 6054 6055 /* openattr */ 6056 argop->argop = OP_OPENATTR; 6057 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6058 strcmp(s, XATTR_RPATH) == 0) { 6059 /* openattr */ 6060 argop->argop = OP_OPENATTR; 6061 argop++; 6062 argcnt++; 6063 6064 /* getfh XXX may not be needed in future */ 6065 argop->argop = OP_GETFH; 6066 argop++; 6067 argcnt++; 6068 6069 /* getattr */ 6070 argop->argop = OP_GETATTR; 6071 argop->nfs_argop4_u.opgetattr.attr_request = 6072 lookupargp->ga_bits; 6073 argop->nfs_argop4_u.opgetattr.mi = 6074 lookupargp->mi; 6075 argop++; 6076 argcnt++; 6077 *p = c; 6078 continue; 6079 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6080 /* lookupp */ 6081 argop->argop = OP_LOOKUPP; 6082 } else { 6083 /* lookup */ 6084 argop->argop = OP_LOOKUP; 6085 (void) str_to_utf8(s, 6086 &argop->nfs_argop4_u.oplookup.objname); 6087 } 6088 lookup_idx = argcnt; 6089 argop++; 6090 argcnt++; 6091 6092 *p = c; 6093 6094 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6095 /* getfh XXX may not be needed in future */ 6096 argop->argop = OP_GETFH; 6097 argop++; 6098 argcnt++; 6099 6100 /* getattr */ 6101 argop->argop = OP_GETATTR; 6102 argop->nfs_argop4_u.opgetattr.attr_request = 6103 lookupargp->ga_bits; 6104 argop->nfs_argop4_u.opgetattr.mi = 6105 lookupargp->mi; 6106 argop++; 6107 argcnt++; 6108 } 6109 } 6110 6111 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6112 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6113 if (needgetfh) { 6114 /* stick in a post-lookup getfh */ 6115 argop->argop = OP_GETFH; 6116 argcnt++; 6117 argop++; 6118 } 6119 /* post-lookup getattr */ 6120 argop->argop = OP_GETATTR; 6121 argop->nfs_argop4_u.opgetattr.attr_request = 6122 lookupargp->ga_bits; 6123 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6124 argcnt++; 6125 } 6126 argcnt += lookupargp->trailer_len; /* actual op count */ 6127 lookupargp->argsp->array_len = argcnt; 6128 lookupargp->arglen = arglen; 6129 6130 #ifdef DEBUG 6131 if (nfs4_client_lookup_debug) 6132 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6133 #endif 6134 6135 return (lookup_idx); 6136 } 6137 6138 static int 6139 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6140 { 6141 COMPOUND4args_clnt args; 6142 COMPOUND4res_clnt res; 6143 GETFH4res *gf_res = NULL; 6144 nfs_argop4 argop[4]; 6145 nfs_resop4 *resop = NULL; 6146 nfs4_sharedfh_t *sfhp; 6147 hrtime_t t; 6148 nfs4_error_t e; 6149 6150 rnode4_t *drp; 6151 int doqueue = 1; 6152 vnode_t *vp; 6153 int needrecov = 0; 6154 nfs4_recov_state_t recov_state; 6155 6156 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 6157 6158 *avp = NULL; 6159 recov_state.rs_flags = 0; 6160 recov_state.rs_num_retry_despite_err = 0; 6161 6162 recov_retry: 6163 /* COMPOUND: putfh, openattr, getfh, getattr */ 6164 args.array_len = 4; 6165 args.array = argop; 6166 args.ctag = TAG_OPENATTR; 6167 6168 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6169 if (e.error) 6170 return (e.error); 6171 6172 drp = VTOR4(dvp); 6173 6174 /* putfh */ 6175 argop[0].argop = OP_CPUTFH; 6176 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6177 6178 /* openattr */ 6179 argop[1].argop = OP_OPENATTR; 6180 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6181 6182 /* getfh */ 6183 argop[2].argop = OP_GETFH; 6184 6185 /* getattr */ 6186 argop[3].argop = OP_GETATTR; 6187 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6188 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6189 6190 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6191 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6192 rnode4info(drp))); 6193 6194 t = gethrtime(); 6195 6196 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6197 6198 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6199 if (needrecov) { 6200 bool_t abort; 6201 6202 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6203 "nfs4openattr: initiating recovery\n")); 6204 6205 abort = nfs4_start_recovery(&e, 6206 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6207 OP_OPENATTR, NULL); 6208 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6209 if (!e.error) { 6210 e.error = geterrno4(res.status); 6211 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6212 } 6213 if (abort == FALSE) 6214 goto recov_retry; 6215 return (e.error); 6216 } 6217 6218 if (e.error) { 6219 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6220 return (e.error); 6221 } 6222 6223 if (res.status) { 6224 /* 6225 * If OTW errro is NOTSUPP, then it should be 6226 * translated to EINVAL. All Solaris file system 6227 * implementations return EINVAL to the syscall layer 6228 * when the attrdir cannot be created due to an 6229 * implementation restriction or noxattr mount option. 6230 */ 6231 if (res.status == NFS4ERR_NOTSUPP) { 6232 mutex_enter(&drp->r_statelock); 6233 if (drp->r_xattr_dir) 6234 VN_RELE(drp->r_xattr_dir); 6235 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6236 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6237 mutex_exit(&drp->r_statelock); 6238 6239 e.error = EINVAL; 6240 } else { 6241 e.error = geterrno4(res.status); 6242 } 6243 6244 if (e.error) { 6245 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6246 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6247 needrecov); 6248 return (e.error); 6249 } 6250 } 6251 6252 resop = &res.array[0]; /* putfh res */ 6253 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6254 6255 resop = &res.array[1]; /* openattr res */ 6256 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6257 6258 resop = &res.array[2]; /* getfh res */ 6259 gf_res = &resop->nfs_resop4_u.opgetfh; 6260 if (gf_res->object.nfs_fh4_len == 0) { 6261 *avp = NULL; 6262 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6263 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6264 return (ENOENT); 6265 } 6266 6267 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6268 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6269 dvp->v_vfsp, t, cr, dvp, 6270 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH)); 6271 sfh4_rele(&sfhp); 6272 6273 if (e.error) 6274 PURGE_ATTRCACHE4(vp); 6275 6276 mutex_enter(&vp->v_lock); 6277 vp->v_flag |= V_XATTRDIR; 6278 mutex_exit(&vp->v_lock); 6279 6280 *avp = vp; 6281 6282 mutex_enter(&drp->r_statelock); 6283 if (drp->r_xattr_dir) 6284 VN_RELE(drp->r_xattr_dir); 6285 VN_HOLD(vp); 6286 drp->r_xattr_dir = vp; 6287 6288 /* 6289 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6290 * NULL. xattrs could be created at any time, and we have no 6291 * way to update pc4_xattr_exists in the base object if/when 6292 * it happens. 6293 */ 6294 drp->r_pathconf.pc4_xattr_valid = 0; 6295 6296 mutex_exit(&drp->r_statelock); 6297 6298 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6299 6300 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6301 6302 return (0); 6303 } 6304 6305 /* ARGSUSED */ 6306 static int 6307 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6308 int mode, vnode_t **vpp, cred_t *cr, int flags) 6309 { 6310 int error; 6311 vnode_t *vp = NULL; 6312 rnode4_t *rp; 6313 struct vattr vattr; 6314 rnode4_t *drp; 6315 vnode_t *tempvp; 6316 enum createmode4 createmode; 6317 bool_t must_trunc = FALSE; 6318 6319 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 6320 return (EPERM); 6321 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6322 return (EINVAL); 6323 } 6324 6325 /* . and .. have special meaning in the protocol, reject them. */ 6326 6327 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6328 return (EISDIR); 6329 6330 drp = VTOR4(dvp); 6331 6332 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6333 return (EINTR); 6334 6335 top: 6336 /* 6337 * We make a copy of the attributes because the caller does not 6338 * expect us to change what va points to. 6339 */ 6340 vattr = *va; 6341 6342 /* 6343 * If the pathname is "", then dvp is the root vnode of 6344 * a remote file mounted over a local directory. 6345 * All that needs to be done is access 6346 * checking and truncation. Note that we avoid doing 6347 * open w/ create because the parent directory might 6348 * be in pseudo-fs and the open would fail. 6349 */ 6350 if (*nm == '\0') { 6351 error = 0; 6352 VN_HOLD(dvp); 6353 vp = dvp; 6354 must_trunc = TRUE; 6355 } else { 6356 /* 6357 * We need to go over the wire, just to be sure whether the 6358 * file exists or not. Using the DNLC can be dangerous in 6359 * this case when making a decision regarding existence. 6360 */ 6361 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6362 } 6363 6364 if (exclusive) 6365 createmode = EXCLUSIVE4; 6366 else 6367 createmode = GUARDED4; 6368 6369 /* 6370 * error would be set if the file does not exist on the 6371 * server, so lets go create it. 6372 */ 6373 if (error) { 6374 goto create_otw; 6375 } 6376 6377 /* 6378 * File does exist on the server 6379 */ 6380 if (exclusive == EXCL) 6381 error = EEXIST; 6382 else if (vp->v_type == VDIR && (mode & VWRITE)) 6383 error = EISDIR; 6384 else { 6385 /* 6386 * If vnode is a device, create special vnode. 6387 */ 6388 if (ISVDEV(vp->v_type)) { 6389 tempvp = vp; 6390 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6391 VN_RELE(tempvp); 6392 } 6393 if (!(error = VOP_ACCESS(vp, mode, 0, cr))) { 6394 if ((vattr.va_mask & AT_SIZE) && 6395 vp->v_type == VREG) { 6396 rp = VTOR4(vp); 6397 /* 6398 * Check here for large file handled 6399 * by LF-unaware process (as 6400 * ufs_create() does) 6401 */ 6402 if (!(flags & FOFFMAX)) { 6403 mutex_enter(&rp->r_statelock); 6404 if (rp->r_size > MAXOFF32_T) 6405 error = EOVERFLOW; 6406 mutex_exit(&rp->r_statelock); 6407 } 6408 6409 /* if error is set then we need to return */ 6410 if (error) { 6411 nfs_rw_exit(&drp->r_rwlock); 6412 VN_RELE(vp); 6413 return (error); 6414 } 6415 6416 if (must_trunc) { 6417 vattr.va_mask = AT_SIZE; 6418 error = nfs4setattr(vp, &vattr, 0, cr, 6419 NULL); 6420 } else { 6421 /* 6422 * we know we have a regular file that already 6423 * exists and we may end up truncating the file 6424 * as a result of the open_otw, so flush out 6425 * any dirty pages for this file first. 6426 */ 6427 if (nfs4_has_pages(vp) && 6428 ((rp->r_flags & R4DIRTY) || 6429 rp->r_count > 0 || 6430 rp->r_mapcnt > 0)) { 6431 error = nfs4_putpage(vp, 6432 (offset_t)0, 0, 0, cr); 6433 if (error && (error == ENOSPC || 6434 error == EDQUOT)) { 6435 mutex_enter( 6436 &rp->r_statelock); 6437 if (!rp->r_error) 6438 rp->r_error = 6439 error; 6440 mutex_exit( 6441 &rp->r_statelock); 6442 } 6443 } 6444 vattr.va_mask = (AT_SIZE | 6445 AT_TYPE | AT_MODE); 6446 vattr.va_type = VREG; 6447 createmode = UNCHECKED4; 6448 goto create_otw; 6449 } 6450 } 6451 } 6452 } 6453 nfs_rw_exit(&drp->r_rwlock); 6454 if (error) { 6455 VN_RELE(vp); 6456 } else { 6457 *vpp = vp; 6458 } 6459 return (error); 6460 6461 create_otw: 6462 dnlc_remove(dvp, nm); 6463 6464 ASSERT(vattr.va_mask & AT_TYPE); 6465 6466 /* 6467 * If not a regular file let nfs4mknod() handle it. 6468 */ 6469 if (vattr.va_type != VREG) { 6470 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6471 nfs_rw_exit(&drp->r_rwlock); 6472 return (error); 6473 } 6474 6475 /* 6476 * It _is_ a regular file. 6477 */ 6478 ASSERT(vattr.va_mask & AT_MODE); 6479 if (MANDMODE(vattr.va_mode)) { 6480 nfs_rw_exit(&drp->r_rwlock); 6481 return (EACCES); 6482 } 6483 6484 /* 6485 * If this happens to be a mknod of a regular file, then flags will 6486 * have neither FREAD or FWRITE. However, we must set at least one 6487 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6488 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6489 * set (based on openmode specified by app). 6490 */ 6491 if ((flags & (FREAD|FWRITE)) == 0) 6492 flags |= (FREAD|FWRITE); 6493 6494 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6495 6496 if (vp != NULL) { 6497 /* if create was successful, throw away the file's pages */ 6498 if (!error && (vattr.va_mask & AT_SIZE)) 6499 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6500 cr); 6501 /* release the lookup hold */ 6502 VN_RELE(vp); 6503 vp = NULL; 6504 } 6505 6506 /* 6507 * validate that we opened a regular file. This handles a misbehaving 6508 * server that returns an incorrect FH. 6509 */ 6510 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6511 error = EISDIR; 6512 VN_RELE(*vpp); 6513 } 6514 6515 /* 6516 * If this is not an exclusive create, then the CREATE 6517 * request will be made with the GUARDED mode set. This 6518 * means that the server will return EEXIST if the file 6519 * exists. The file could exist because of a retransmitted 6520 * request. In this case, we recover by starting over and 6521 * checking to see whether the file exists. This second 6522 * time through it should and a CREATE request will not be 6523 * sent. 6524 * 6525 * This handles the problem of a dangling CREATE request 6526 * which contains attributes which indicate that the file 6527 * should be truncated. This retransmitted request could 6528 * possibly truncate valid data in the file if not caught 6529 * by the duplicate request mechanism on the server or if 6530 * not caught by other means. The scenario is: 6531 * 6532 * Client transmits CREATE request with size = 0 6533 * Client times out, retransmits request. 6534 * Response to the first request arrives from the server 6535 * and the client proceeds on. 6536 * Client writes data to the file. 6537 * The server now processes retransmitted CREATE request 6538 * and truncates file. 6539 * 6540 * The use of the GUARDED CREATE request prevents this from 6541 * happening because the retransmitted CREATE would fail 6542 * with EEXIST and would not truncate the file. 6543 */ 6544 if (error == EEXIST && exclusive == NONEXCL) { 6545 #ifdef DEBUG 6546 nfs4_create_misses++; 6547 #endif 6548 goto top; 6549 } 6550 nfs_rw_exit(&drp->r_rwlock); 6551 return (error); 6552 } 6553 6554 /* 6555 * Create compound (for mkdir, mknod, symlink): 6556 * { Putfh <dfh>; Create; Getfh; Getattr } 6557 * It's okay if setattr failed to set gid - this is not considered 6558 * an error, but purge attrs in that case. 6559 */ 6560 static int 6561 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6562 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6563 { 6564 int need_end_op = FALSE; 6565 COMPOUND4args_clnt args; 6566 COMPOUND4res_clnt res, *resp = NULL; 6567 nfs_argop4 *argop; 6568 nfs_resop4 *resop; 6569 int doqueue; 6570 mntinfo4_t *mi; 6571 rnode4_t *drp = VTOR4(dvp); 6572 change_info4 *cinfo; 6573 GETFH4res *gf_res; 6574 struct vattr vattr; 6575 vnode_t *vp; 6576 fattr4 *crattr; 6577 bool_t needrecov = FALSE; 6578 nfs4_recov_state_t recov_state; 6579 nfs4_sharedfh_t *sfhp = NULL; 6580 hrtime_t t; 6581 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6582 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6583 dirattr_info_t dinfo, *dinfop; 6584 servinfo4_t *svp; 6585 bitmap4 supp_attrs; 6586 6587 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6588 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6589 6590 mi = VTOMI4(dvp); 6591 6592 /* 6593 * Make sure we properly deal with setting the right gid 6594 * on a new directory to reflect the parent's setgid bit 6595 */ 6596 setgid_flag = 0; 6597 if (type == NF4DIR) { 6598 struct vattr dva; 6599 6600 va->va_mode &= ~VSGID; 6601 dva.va_mask = AT_MODE | AT_GID; 6602 if (VOP_GETATTR(dvp, &dva, 0, cr) == 0) { 6603 6604 /* 6605 * If the parent's directory has the setgid bit set 6606 * _and_ the client was able to get a valid mapping 6607 * for the parent dir's owner_group, we want to 6608 * append NVERIFY(owner_group == dva.va_gid) and 6609 * SETTATTR to the CREATE compound. 6610 */ 6611 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6612 setgid_flag = 1; 6613 va->va_mode |= VSGID; 6614 if (dva.va_gid != GID_NOBODY) { 6615 va->va_mask |= AT_GID; 6616 va->va_gid = dva.va_gid; 6617 } 6618 } 6619 } 6620 } 6621 6622 /* 6623 * Create ops: 6624 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6625 * 5:restorefh(dir) 6:getattr(dir) 6626 * 6627 * if (setgid) 6628 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6629 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6630 * 8:nverify 9:setattr 6631 */ 6632 if (setgid_flag) { 6633 numops = 10; 6634 idx_create = 1; 6635 idx_fattr = 3; 6636 } else { 6637 numops = 7; 6638 idx_create = 2; 6639 idx_fattr = 4; 6640 } 6641 6642 ASSERT(curproc->p_zone == mi->mi_zone); 6643 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6644 return (EINTR); 6645 } 6646 recov_state.rs_flags = 0; 6647 recov_state.rs_num_retry_despite_err = 0; 6648 6649 argoplist_size = numops * sizeof (nfs_argop4); 6650 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6651 6652 recov_retry: 6653 if (type == NF4LNK) 6654 args.ctag = TAG_SYMLINK; 6655 else if (type == NF4DIR) 6656 args.ctag = TAG_MKDIR; 6657 else 6658 args.ctag = TAG_MKNOD; 6659 6660 args.array_len = numops; 6661 args.array = argop; 6662 6663 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6664 nfs_rw_exit(&drp->r_rwlock); 6665 kmem_free(argop, argoplist_size); 6666 return (e.error); 6667 } 6668 need_end_op = TRUE; 6669 6670 6671 /* 0: putfh directory */ 6672 argop[0].argop = OP_CPUTFH; 6673 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6674 6675 /* 1/2: Create object */ 6676 argop[idx_create].argop = OP_CCREATE; 6677 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6678 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6679 if (type == NF4LNK) { 6680 /* 6681 * symlink, treat name as data 6682 */ 6683 ASSERT(data != NULL); 6684 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6685 (char *)data; 6686 } 6687 if (type == NF4BLK || type == NF4CHR) { 6688 ASSERT(data != NULL); 6689 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6690 *((specdata4 *)data); 6691 } 6692 6693 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6694 6695 svp = drp->r_server; 6696 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6697 supp_attrs = svp->sv_supp_attrs; 6698 nfs_rw_exit(&svp->sv_lock); 6699 6700 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6701 nfs_rw_exit(&drp->r_rwlock); 6702 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6703 e.error = EINVAL; 6704 kmem_free(argop, argoplist_size); 6705 return (e.error); 6706 } 6707 6708 /* 2/3: getfh fh of created object */ 6709 ASSERT(idx_create + 1 == idx_fattr - 1); 6710 argop[idx_create + 1].argop = OP_GETFH; 6711 6712 /* 3/4: getattr of new object */ 6713 argop[idx_fattr].argop = OP_GETATTR; 6714 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6715 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6716 6717 if (setgid_flag) { 6718 vattr_t _v; 6719 6720 argop[4].argop = OP_SAVEFH; 6721 6722 argop[5].argop = OP_CPUTFH; 6723 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6724 6725 argop[6].argop = OP_GETATTR; 6726 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6727 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6728 6729 argop[7].argop = OP_RESTOREFH; 6730 6731 /* 6732 * nverify 6733 * 6734 * XXX - Revisit the last argument to nfs4_end_op() 6735 * once 5020486 is fixed. 6736 */ 6737 _v.va_mask = AT_GID; 6738 _v.va_gid = va->va_gid; 6739 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6740 supp_attrs)) { 6741 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6742 nfs_rw_exit(&drp->r_rwlock); 6743 nfs4_fattr4_free(crattr); 6744 kmem_free(argop, argoplist_size); 6745 return (e.error); 6746 } 6747 6748 /* 6749 * setattr 6750 * 6751 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6752 * so no need for stateid or flags. Also we specify NULL 6753 * rp since we're only interested in setting owner_group 6754 * attributes. 6755 */ 6756 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 6757 &e.error, 0); 6758 6759 if (e.error) { 6760 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6761 nfs_rw_exit(&drp->r_rwlock); 6762 nfs4_fattr4_free(crattr); 6763 nfs4args_verify_free(&argop[8]); 6764 kmem_free(argop, argoplist_size); 6765 return (e.error); 6766 } 6767 } else { 6768 argop[1].argop = OP_SAVEFH; 6769 6770 argop[5].argop = OP_RESTOREFH; 6771 6772 argop[6].argop = OP_GETATTR; 6773 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6774 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6775 } 6776 6777 dnlc_remove(dvp, nm); 6778 6779 doqueue = 1; 6780 t = gethrtime(); 6781 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 6782 6783 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 6784 if (e.error) { 6785 PURGE_ATTRCACHE4(dvp); 6786 if (!needrecov) 6787 goto out; 6788 } 6789 6790 if (needrecov) { 6791 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 6792 OP_CREATE, NULL) == FALSE) { 6793 nfs4_end_op(mi, dvp, NULL, &recov_state, 6794 needrecov); 6795 need_end_op = FALSE; 6796 nfs4_fattr4_free(crattr); 6797 if (setgid_flag) { 6798 nfs4args_verify_free(&argop[8]); 6799 nfs4args_setattr_free(&argop[9]); 6800 } 6801 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6802 goto recov_retry; 6803 } 6804 } 6805 6806 resp = &res; 6807 6808 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 6809 6810 if (res.status == NFS4ERR_BADOWNER) 6811 nfs4_log_badowner(mi, OP_CREATE); 6812 6813 e.error = geterrno4(res.status); 6814 6815 /* 6816 * This check is left over from when create was implemented 6817 * using a setattr op (instead of createattrs). If the 6818 * putfh/create/getfh failed, the error was returned. If 6819 * setattr/getattr failed, we keep going. 6820 * 6821 * It might be better to get rid of the GETFH also, and just 6822 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 6823 * Then if any of the operations failed, we could return the 6824 * error now, and remove much of the error code below. 6825 */ 6826 if (res.array_len <= idx_fattr) { 6827 /* 6828 * Either Putfh, Create or Getfh failed. 6829 */ 6830 PURGE_ATTRCACHE4(dvp); 6831 /* 6832 * nfs4_purge_stale_fh() may generate otw calls through 6833 * nfs4_invalidate_pages. Hence the need to call 6834 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 6835 */ 6836 nfs4_end_op(mi, dvp, NULL, &recov_state, 6837 needrecov); 6838 need_end_op = FALSE; 6839 nfs4_purge_stale_fh(e.error, dvp, cr); 6840 goto out; 6841 } 6842 } 6843 6844 resop = &res.array[idx_create]; /* create res */ 6845 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 6846 6847 resop = &res.array[idx_create + 1]; /* getfh res */ 6848 gf_res = &resop->nfs_resop4_u.opgetfh; 6849 6850 sfhp = sfh4_get(&gf_res->object, mi); 6851 if (e.error) { 6852 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 6853 fn_get(VTOSV(dvp)->sv_name, nm)); 6854 if (vp->v_type == VNON) { 6855 vattr.va_mask = AT_TYPE; 6856 /* 6857 * Need to call nfs4_end_op before nfs4getattr to avoid 6858 * potential nfs4_start_op deadlock. See RFE 4777612. 6859 */ 6860 nfs4_end_op(mi, dvp, NULL, &recov_state, 6861 needrecov); 6862 need_end_op = FALSE; 6863 e.error = nfs4getattr(vp, &vattr, cr); 6864 if (e.error) { 6865 VN_RELE(vp); 6866 *vpp = NULL; 6867 goto out; 6868 } 6869 vp->v_type = vattr.va_type; 6870 } 6871 e.error = 0; 6872 } else { 6873 *vpp = vp = makenfs4node(sfhp, 6874 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 6875 dvp->v_vfsp, t, cr, 6876 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 6877 } 6878 6879 /* 6880 * If compound succeeded, then update dir attrs 6881 */ 6882 if (res.status == NFS4_OK) { 6883 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 6884 dinfo.di_cred = cr; 6885 dinfo.di_time_call = t; 6886 dinfop = &dinfo; 6887 } else 6888 dinfop = NULL; 6889 6890 /* Update directory cache attribute, readdir and dnlc caches */ 6891 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 6892 6893 out: 6894 if (sfhp != NULL) 6895 sfh4_rele(&sfhp); 6896 nfs_rw_exit(&drp->r_rwlock); 6897 nfs4_fattr4_free(crattr); 6898 if (setgid_flag) { 6899 nfs4args_verify_free(&argop[8]); 6900 nfs4args_setattr_free(&argop[9]); 6901 } 6902 if (resp) 6903 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 6904 if (need_end_op) 6905 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6906 6907 kmem_free(argop, argoplist_size); 6908 return (e.error); 6909 } 6910 6911 /* ARGSUSED */ 6912 static int 6913 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6914 int mode, vnode_t **vpp, cred_t *cr) 6915 { 6916 int error; 6917 vnode_t *vp; 6918 nfs_ftype4 type; 6919 specdata4 spec, *specp = NULL; 6920 6921 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 6922 6923 switch (va->va_type) { 6924 case VCHR: 6925 case VBLK: 6926 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 6927 spec.specdata1 = getmajor(va->va_rdev); 6928 spec.specdata2 = getminor(va->va_rdev); 6929 specp = &spec; 6930 break; 6931 6932 case VFIFO: 6933 type = NF4FIFO; 6934 break; 6935 case VSOCK: 6936 type = NF4SOCK; 6937 break; 6938 6939 default: 6940 return (EINVAL); 6941 } 6942 6943 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 6944 if (error) { 6945 return (error); 6946 } 6947 6948 /* 6949 * This might not be needed any more; special case to deal 6950 * with problematic v2/v3 servers. Since create was unable 6951 * to set group correctly, not sure what hope setattr has. 6952 */ 6953 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 6954 va->va_mask = AT_GID; 6955 (void) nfs4setattr(vp, va, 0, cr, NULL); 6956 } 6957 6958 /* 6959 * If vnode is a device create special vnode 6960 */ 6961 if (ISVDEV(vp->v_type)) { 6962 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6963 VN_RELE(vp); 6964 } else { 6965 *vpp = vp; 6966 } 6967 return (error); 6968 } 6969 6970 /* 6971 * Remove requires that the current fh be the target directory. 6972 * After the operation, the current fh is unchanged. 6973 * The compound op structure is: 6974 * PUTFH(targetdir), REMOVE 6975 * 6976 * Weirdness: if the vnode to be removed is open 6977 * we rename it instead of removing it and nfs_inactive 6978 * will remove the new name. 6979 */ 6980 static int 6981 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr) 6982 { 6983 COMPOUND4args_clnt args; 6984 COMPOUND4res_clnt res, *resp = NULL; 6985 REMOVE4res *rm_res; 6986 nfs_argop4 argop[3]; 6987 nfs_resop4 *resop; 6988 vnode_t *vp; 6989 char *tmpname; 6990 int doqueue; 6991 mntinfo4_t *mi; 6992 rnode4_t *rp; 6993 rnode4_t *drp; 6994 int needrecov = 0; 6995 nfs4_recov_state_t recov_state; 6996 int isopen; 6997 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6998 dirattr_info_t dinfo; 6999 7000 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 7001 return (EPERM); 7002 drp = VTOR4(dvp); 7003 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7004 return (EINTR); 7005 7006 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7007 if (e.error) { 7008 nfs_rw_exit(&drp->r_rwlock); 7009 return (e.error); 7010 } 7011 7012 if (vp->v_type == VDIR) { 7013 VN_RELE(vp); 7014 nfs_rw_exit(&drp->r_rwlock); 7015 return (EISDIR); 7016 } 7017 7018 /* 7019 * First just remove the entry from the name cache, as it 7020 * is most likely the only entry for this vp. 7021 */ 7022 dnlc_remove(dvp, nm); 7023 7024 rp = VTOR4(vp); 7025 7026 /* 7027 * For regular file types, check to see if the file is open by looking 7028 * at the open streams. 7029 * For all other types, check the reference count on the vnode. Since 7030 * they are not opened OTW they never have an open stream. 7031 * 7032 * If the file is open, rename it to .nfsXXXX. 7033 */ 7034 if (vp->v_type != VREG) { 7035 /* 7036 * If the file has a v_count > 1 then there may be more than one 7037 * entry in the name cache due multiple links or an open file, 7038 * but we don't have the real reference count so flush all 7039 * possible entries. 7040 */ 7041 if (vp->v_count > 1) 7042 dnlc_purge_vp(vp); 7043 7044 /* 7045 * Now we have the real reference count. 7046 */ 7047 isopen = vp->v_count > 1; 7048 } else { 7049 mutex_enter(&rp->r_os_lock); 7050 isopen = list_head(&rp->r_open_streams) != NULL; 7051 mutex_exit(&rp->r_os_lock); 7052 } 7053 7054 mutex_enter(&rp->r_statelock); 7055 if (isopen && 7056 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7057 mutex_exit(&rp->r_statelock); 7058 tmpname = newname(); 7059 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr); 7060 if (e.error) 7061 kmem_free(tmpname, MAXNAMELEN); 7062 else { 7063 mutex_enter(&rp->r_statelock); 7064 if (rp->r_unldvp == NULL) { 7065 VN_HOLD(dvp); 7066 rp->r_unldvp = dvp; 7067 if (rp->r_unlcred != NULL) 7068 crfree(rp->r_unlcred); 7069 crhold(cr); 7070 rp->r_unlcred = cr; 7071 rp->r_unlname = tmpname; 7072 } else { 7073 kmem_free(rp->r_unlname, MAXNAMELEN); 7074 rp->r_unlname = tmpname; 7075 } 7076 mutex_exit(&rp->r_statelock); 7077 } 7078 VN_RELE(vp); 7079 nfs_rw_exit(&drp->r_rwlock); 7080 return (e.error); 7081 } 7082 /* 7083 * Actually remove the file/dir 7084 */ 7085 mutex_exit(&rp->r_statelock); 7086 7087 /* 7088 * We need to flush any dirty pages which happen to 7089 * be hanging around before removing the file. 7090 * This shouldn't happen very often since in NFSv4 7091 * we should be close to open consistent. 7092 */ 7093 if (nfs4_has_pages(vp) && 7094 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7095 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr); 7096 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7097 mutex_enter(&rp->r_statelock); 7098 if (!rp->r_error) 7099 rp->r_error = e.error; 7100 mutex_exit(&rp->r_statelock); 7101 } 7102 } 7103 7104 mi = VTOMI4(dvp); 7105 7106 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7107 recov_state.rs_flags = 0; 7108 recov_state.rs_num_retry_despite_err = 0; 7109 7110 recov_retry: 7111 /* 7112 * Remove ops: putfh dir; remove 7113 */ 7114 args.ctag = TAG_REMOVE; 7115 args.array_len = 3; 7116 args.array = argop; 7117 7118 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7119 if (e.error) { 7120 nfs_rw_exit(&drp->r_rwlock); 7121 VN_RELE(vp); 7122 return (e.error); 7123 } 7124 7125 /* putfh directory */ 7126 argop[0].argop = OP_CPUTFH; 7127 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7128 7129 /* remove */ 7130 argop[1].argop = OP_CREMOVE; 7131 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7132 7133 /* getattr dir */ 7134 argop[2].argop = OP_GETATTR; 7135 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7136 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7137 7138 doqueue = 1; 7139 dinfo.di_time_call = gethrtime(); 7140 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7141 7142 PURGE_ATTRCACHE4(vp); 7143 7144 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7145 if (e.error) 7146 PURGE_ATTRCACHE4(dvp); 7147 7148 if (needrecov) { 7149 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7150 NULL, NULL, NULL, OP_REMOVE, NULL) == FALSE) { 7151 if (!e.error) 7152 (void) xdr_free(xdr_COMPOUND4res_clnt, 7153 (caddr_t)&res); 7154 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7155 needrecov); 7156 goto recov_retry; 7157 } 7158 } 7159 7160 /* 7161 * Matching nfs4_end_op() for start_op() above. 7162 * There is a path in the code below which calls 7163 * nfs4_purge_stale_fh(), which may generate otw calls through 7164 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7165 * here to avoid nfs4_start_op() deadlock. 7166 */ 7167 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7168 7169 if (!e.error) { 7170 resp = &res; 7171 7172 if (res.status) { 7173 e.error = geterrno4(res.status); 7174 PURGE_ATTRCACHE4(dvp); 7175 nfs4_purge_stale_fh(e.error, dvp, cr); 7176 } else { 7177 resop = &res.array[1]; /* remove res */ 7178 rm_res = &resop->nfs_resop4_u.opremove; 7179 7180 dinfo.di_garp = 7181 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7182 dinfo.di_cred = cr; 7183 7184 /* Update directory attr, readdir and dnlc caches */ 7185 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7186 &dinfo); 7187 } 7188 } 7189 nfs_rw_exit(&drp->r_rwlock); 7190 if (resp) 7191 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7192 7193 VN_RELE(vp); 7194 return (e.error); 7195 } 7196 7197 /* 7198 * Link requires that the current fh be the target directory and the 7199 * saved fh be the source fh. After the operation, the current fh is unchanged. 7200 * Thus the compound op structure is: 7201 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7202 * GETATTR(file) 7203 */ 7204 static int 7205 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr) 7206 { 7207 COMPOUND4args_clnt args; 7208 COMPOUND4res_clnt res, *resp = NULL; 7209 LINK4res *ln_res; 7210 int argoplist_size = 7 * sizeof (nfs_argop4); 7211 nfs_argop4 *argop; 7212 nfs_resop4 *resop; 7213 vnode_t *realvp, *nvp; 7214 int doqueue; 7215 mntinfo4_t *mi; 7216 rnode4_t *tdrp; 7217 bool_t needrecov = FALSE; 7218 nfs4_recov_state_t recov_state; 7219 hrtime_t t; 7220 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7221 dirattr_info_t dinfo; 7222 7223 ASSERT(*tnm != '\0'); 7224 ASSERT(tdvp->v_type == VDIR); 7225 ASSERT(nfs4_consistent_type(tdvp)); 7226 ASSERT(nfs4_consistent_type(svp)); 7227 7228 if (curproc->p_zone != VTOMI4(tdvp)->mi_zone) 7229 return (EPERM); 7230 if (VOP_REALVP(svp, &realvp) == 0) { 7231 svp = realvp; 7232 ASSERT(nfs4_consistent_type(svp)); 7233 } 7234 7235 tdrp = VTOR4(tdvp); 7236 mi = VTOMI4(svp); 7237 7238 if (!(mi->mi_flags & MI4_LINK)) { 7239 return (EOPNOTSUPP); 7240 } 7241 recov_state.rs_flags = 0; 7242 recov_state.rs_num_retry_despite_err = 0; 7243 7244 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7245 return (EINTR); 7246 7247 recov_retry: 7248 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7249 7250 args.ctag = TAG_LINK; 7251 7252 /* 7253 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7254 * restorefh; getattr(fl) 7255 */ 7256 args.array_len = 7; 7257 args.array = argop; 7258 7259 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7260 if (e.error) { 7261 kmem_free(argop, argoplist_size); 7262 nfs_rw_exit(&tdrp->r_rwlock); 7263 return (e.error); 7264 } 7265 7266 /* 0. putfh file */ 7267 argop[0].argop = OP_CPUTFH; 7268 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7269 7270 /* 1. save current fh to free up the space for the dir */ 7271 argop[1].argop = OP_SAVEFH; 7272 7273 /* 2. putfh targetdir */ 7274 argop[2].argop = OP_CPUTFH; 7275 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7276 7277 /* 3. link: current_fh is targetdir, saved_fh is source */ 7278 argop[3].argop = OP_CLINK; 7279 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7280 7281 /* 4. Get attributes of dir */ 7282 argop[4].argop = OP_GETATTR; 7283 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7284 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7285 7286 /* 5. If link was successful, restore current vp to file */ 7287 argop[5].argop = OP_RESTOREFH; 7288 7289 /* 6. Get attributes of linked object */ 7290 argop[6].argop = OP_GETATTR; 7291 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7292 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7293 7294 dnlc_remove(tdvp, tnm); 7295 7296 doqueue = 1; 7297 t = gethrtime(); 7298 7299 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7300 7301 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7302 if (e.error != 0 && !needrecov) { 7303 PURGE_ATTRCACHE4(tdvp); 7304 PURGE_ATTRCACHE4(svp); 7305 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7306 goto out; 7307 } 7308 7309 if (needrecov) { 7310 bool_t abort; 7311 7312 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7313 NULL, NULL, OP_LINK, NULL); 7314 if (abort == FALSE) { 7315 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7316 needrecov); 7317 kmem_free(argop, argoplist_size); 7318 if (!e.error) 7319 (void) xdr_free(xdr_COMPOUND4res_clnt, 7320 (caddr_t)&res); 7321 goto recov_retry; 7322 } else { 7323 if (e.error != 0) { 7324 PURGE_ATTRCACHE4(tdvp); 7325 PURGE_ATTRCACHE4(svp); 7326 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7327 &recov_state, needrecov); 7328 goto out; 7329 } 7330 /* fall through for res.status case */ 7331 } 7332 } 7333 7334 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7335 7336 resp = &res; 7337 if (res.status) { 7338 /* If link succeeded, then don't return error */ 7339 e.error = geterrno4(res.status); 7340 if (res.array_len <= 4) { 7341 /* 7342 * Either Putfh, Savefh, Putfh dir, or Link failed 7343 */ 7344 PURGE_ATTRCACHE4(svp); 7345 PURGE_ATTRCACHE4(tdvp); 7346 if (e.error == EOPNOTSUPP) { 7347 mutex_enter(&mi->mi_lock); 7348 mi->mi_flags &= ~MI4_LINK; 7349 mutex_exit(&mi->mi_lock); 7350 } 7351 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7352 /* XXX-LP */ 7353 if (e.error == EISDIR && crgetuid(cr) != 0) 7354 e.error = EPERM; 7355 goto out; 7356 } 7357 } 7358 7359 /* either no error or one of the postop getattr failed */ 7360 7361 /* 7362 * XXX - if LINK succeeded, but no attrs were returned for link 7363 * file, purge its cache. 7364 * 7365 * XXX Perform a simplified version of wcc checking. Instead of 7366 * have another getattr to get pre-op, just purge cache if 7367 * any of the ops prior to and including the getattr failed. 7368 * If the getattr succeeded then update the attrcache accordingly. 7369 */ 7370 7371 /* 7372 * update cache with link file postattrs. 7373 * Note: at this point resop points to link res. 7374 */ 7375 resop = &res.array[3]; /* link res */ 7376 ln_res = &resop->nfs_resop4_u.oplink; 7377 if (res.status == NFS4_OK) { 7378 e.error = nfs4_update_attrcache(res.status, 7379 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7380 t, svp, cr); 7381 } 7382 7383 /* 7384 * Call makenfs4node to create the new shadow vp for tnm. 7385 * We pass NULL attrs because we just cached attrs for 7386 * the src object. All we're trying to accomplish is to 7387 * to create the new shadow vnode. 7388 */ 7389 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7390 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm)); 7391 7392 /* Update target cache attribute, readdir and dnlc caches */ 7393 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7394 dinfo.di_time_call = t; 7395 dinfo.di_cred = cr; 7396 7397 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7398 ASSERT(nfs4_consistent_type(tdvp)); 7399 ASSERT(nfs4_consistent_type(svp)); 7400 ASSERT(nfs4_consistent_type(nvp)); 7401 VN_RELE(nvp); 7402 7403 out: 7404 kmem_free(argop, argoplist_size); 7405 if (resp) 7406 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7407 7408 nfs_rw_exit(&tdrp->r_rwlock); 7409 7410 return (e.error); 7411 } 7412 7413 static int 7414 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 7415 { 7416 vnode_t *realvp; 7417 7418 if (curproc->p_zone != VTOMI4(odvp)->mi_zone) 7419 return (EPERM); 7420 if (VOP_REALVP(ndvp, &realvp) == 0) 7421 ndvp = realvp; 7422 7423 return (nfs4rename(odvp, onm, ndvp, nnm, cr)); 7424 } 7425 7426 /* 7427 * nfs4rename does the real work of renaming in NFS Version 4. 7428 * 7429 * A file handle is considered volatile for renaming purposes if either 7430 * of the volatile bits are turned on. However, the compound may differ 7431 * based on the likelihood of the filehandle to change during rename. 7432 */ 7433 static int 7434 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 7435 { 7436 int error; 7437 mntinfo4_t *mi; 7438 vnode_t *nvp; 7439 vnode_t *ovp = NULL; 7440 char *tmpname = NULL; 7441 rnode4_t *rp; 7442 rnode4_t *odrp; 7443 rnode4_t *ndrp; 7444 int did_link = 0; 7445 int do_link = 1; 7446 nfsstat4 stat = NFS4_OK; 7447 7448 ASSERT(curproc->p_zone == VTOMI4(odvp)->mi_zone); 7449 ASSERT(nfs4_consistent_type(odvp)); 7450 ASSERT(nfs4_consistent_type(ndvp)); 7451 7452 if (onm[0] == '.' && (onm[1] == '\0' || 7453 (onm[1] == '.' && onm[2] == '\0'))) 7454 return (EINVAL); 7455 7456 if (nnm[0] == '.' && (nnm[1] == '\0' || 7457 (nnm[1] == '.' && nnm[2] == '\0'))) 7458 return (EINVAL); 7459 7460 odrp = VTOR4(odvp); 7461 ndrp = VTOR4(ndvp); 7462 if ((intptr_t)odrp < (intptr_t)ndrp) { 7463 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7464 return (EINTR); 7465 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7466 nfs_rw_exit(&odrp->r_rwlock); 7467 return (EINTR); 7468 } 7469 } else { 7470 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7471 return (EINTR); 7472 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7473 nfs_rw_exit(&ndrp->r_rwlock); 7474 return (EINTR); 7475 } 7476 } 7477 7478 /* 7479 * Lookup the target file. If it exists, it needs to be 7480 * checked to see whether it is a mount point and whether 7481 * it is active (open). 7482 */ 7483 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7484 if (!error) { 7485 int isactive; 7486 7487 ASSERT(nfs4_consistent_type(nvp)); 7488 /* 7489 * If this file has been mounted on, then just 7490 * return busy because renaming to it would remove 7491 * the mounted file system from the name space. 7492 */ 7493 if (vn_ismntpt(nvp)) { 7494 VN_RELE(nvp); 7495 nfs_rw_exit(&odrp->r_rwlock); 7496 nfs_rw_exit(&ndrp->r_rwlock); 7497 return (EBUSY); 7498 } 7499 7500 /* 7501 * First just remove the entry from the name cache, as it 7502 * is most likely the only entry for this vp. 7503 */ 7504 dnlc_remove(ndvp, nnm); 7505 7506 rp = VTOR4(nvp); 7507 7508 if (nvp->v_type != VREG) { 7509 /* 7510 * Purge the name cache of all references to this vnode 7511 * so that we can check the reference count to infer 7512 * whether it is active or not. 7513 */ 7514 if (nvp->v_count > 1) 7515 dnlc_purge_vp(nvp); 7516 7517 isactive = nvp->v_count > 1; 7518 } else { 7519 mutex_enter(&rp->r_os_lock); 7520 isactive = list_head(&rp->r_open_streams) != NULL; 7521 mutex_exit(&rp->r_os_lock); 7522 } 7523 7524 /* 7525 * If the vnode is active and is not a directory, 7526 * arrange to rename it to a 7527 * temporary file so that it will continue to be 7528 * accessible. This implements the "unlink-open-file" 7529 * semantics for the target of a rename operation. 7530 * Before doing this though, make sure that the 7531 * source and target files are not already the same. 7532 */ 7533 if (isactive && nvp->v_type != VDIR) { 7534 /* 7535 * Lookup the source name. 7536 */ 7537 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7538 7539 /* 7540 * The source name *should* already exist. 7541 */ 7542 if (error) { 7543 VN_RELE(nvp); 7544 nfs_rw_exit(&odrp->r_rwlock); 7545 nfs_rw_exit(&ndrp->r_rwlock); 7546 return (error); 7547 } 7548 7549 ASSERT(nfs4_consistent_type(ovp)); 7550 7551 /* 7552 * Compare the two vnodes. If they are the same, 7553 * just release all held vnodes and return success. 7554 */ 7555 if (VN_CMP(ovp, nvp)) { 7556 VN_RELE(ovp); 7557 VN_RELE(nvp); 7558 nfs_rw_exit(&odrp->r_rwlock); 7559 nfs_rw_exit(&ndrp->r_rwlock); 7560 return (0); 7561 } 7562 7563 /* 7564 * Can't mix and match directories and non- 7565 * directories in rename operations. We already 7566 * know that the target is not a directory. If 7567 * the source is a directory, return an error. 7568 */ 7569 if (ovp->v_type == VDIR) { 7570 VN_RELE(ovp); 7571 VN_RELE(nvp); 7572 nfs_rw_exit(&odrp->r_rwlock); 7573 nfs_rw_exit(&ndrp->r_rwlock); 7574 return (ENOTDIR); 7575 } 7576 link_call: 7577 /* 7578 * The target file exists, is not the same as 7579 * the source file, and is active. We first 7580 * try to Link it to a temporary filename to 7581 * avoid having the server removing the file 7582 * completely (which could cause data loss to 7583 * the user's POV in the event the Rename fails 7584 * -- see bug 1165874). 7585 */ 7586 /* 7587 * The do_link and did_link booleans are 7588 * introduced in the event we get NFS4ERR_FILE_OPEN 7589 * returned for the Rename. Some servers can 7590 * not Rename over an Open file, so they return 7591 * this error. The client needs to Remove the 7592 * newly created Link and do two Renames, just 7593 * as if the server didn't support LINK. 7594 */ 7595 tmpname = newname(); 7596 error = 0; 7597 7598 if (do_link) { 7599 error = nfs4_link(ndvp, nvp, tmpname, cr); 7600 } 7601 if (error == EOPNOTSUPP || !do_link) { 7602 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7603 cr); 7604 did_link = 0; 7605 } else { 7606 did_link = 1; 7607 } 7608 if (error) { 7609 kmem_free(tmpname, MAXNAMELEN); 7610 VN_RELE(ovp); 7611 VN_RELE(nvp); 7612 nfs_rw_exit(&odrp->r_rwlock); 7613 nfs_rw_exit(&ndrp->r_rwlock); 7614 return (error); 7615 } 7616 7617 mutex_enter(&rp->r_statelock); 7618 if (rp->r_unldvp == NULL) { 7619 VN_HOLD(ndvp); 7620 rp->r_unldvp = ndvp; 7621 if (rp->r_unlcred != NULL) 7622 crfree(rp->r_unlcred); 7623 crhold(cr); 7624 rp->r_unlcred = cr; 7625 rp->r_unlname = tmpname; 7626 } else { 7627 if (rp->r_unlname) 7628 kmem_free(rp->r_unlname, MAXNAMELEN); 7629 rp->r_unlname = tmpname; 7630 } 7631 mutex_exit(&rp->r_statelock); 7632 } 7633 7634 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7635 7636 ASSERT(nfs4_consistent_type(nvp)); 7637 VN_RELE(nvp); 7638 } 7639 7640 if (ovp == NULL) { 7641 /* 7642 * When renaming directories to be a subdirectory of a 7643 * different parent, the dnlc entry for ".." will no 7644 * longer be valid, so it must be removed. 7645 * 7646 * We do a lookup here to determine whether we are renaming 7647 * a directory and we need to check if we are renaming 7648 * an unlinked file. This might have already been done 7649 * in previous code, so we check ovp == NULL to avoid 7650 * doing it twice. 7651 */ 7652 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7653 /* 7654 * The source name *should* already exist. 7655 */ 7656 if (error) { 7657 nfs_rw_exit(&odrp->r_rwlock); 7658 nfs_rw_exit(&ndrp->r_rwlock); 7659 return (error); 7660 } 7661 ASSERT(ovp != NULL); 7662 ASSERT(nfs4_consistent_type(ovp)); 7663 } 7664 7665 /* 7666 * Is the object being renamed a dir, and if so, is 7667 * it being renamed to a child of itself? The underlying 7668 * fs should ultimately return EINVAL for this case; 7669 * however, buggy beta non-Solaris NFSv4 servers at 7670 * interop testing events have allowed this behavior, 7671 * and it caused our client to panic due to a recursive 7672 * mutex_enter in fn_move. 7673 * 7674 * The tedious locking in fn_move could be changed to 7675 * deal with this case, and the client could avoid the 7676 * panic; however, the client would just confuse itself 7677 * later and misbehave. A better way to handle the broken 7678 * server is to detect this condition and return EINVAL 7679 * without ever sending the the bogus rename to the server. 7680 * We know the rename is invalid -- just fail it now. 7681 */ 7682 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7683 VN_RELE(ovp); 7684 nfs_rw_exit(&odrp->r_rwlock); 7685 nfs_rw_exit(&ndrp->r_rwlock); 7686 return (EINVAL); 7687 } 7688 7689 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7690 7691 /* 7692 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7693 * possible for the filehandle to change due to the rename. 7694 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7695 * the fh will not change because of the rename, but we still need 7696 * to update its rnode entry with the new name for 7697 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7698 * has no effect on these for now, but for future improvements, 7699 * we might want to use it too to simplify handling of files 7700 * that are open with that flag on. (XXX) 7701 */ 7702 mi = VTOMI4(odvp); 7703 if (NFS4_VOLATILE_FH(mi)) { 7704 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7705 &stat); 7706 } else { 7707 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7708 &stat); 7709 } 7710 ASSERT(nfs4_consistent_type(odvp)); 7711 ASSERT(nfs4_consistent_type(ndvp)); 7712 ASSERT(nfs4_consistent_type(ovp)); 7713 7714 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7715 do_link = 0; 7716 /* 7717 * Before the 'link_call' code, we did a nfs4_lookup 7718 * that puts a VN_HOLD on nvp. After the nfs4_link 7719 * call we call VN_RELE to match that hold. We need 7720 * to place an additional VN_HOLD here since we will 7721 * be hitting that VN_RELE again. 7722 */ 7723 VN_HOLD(nvp); 7724 7725 (void) nfs4_remove(ndvp, tmpname, cr); 7726 7727 /* Undo the unlinked file naming stuff we just did */ 7728 mutex_enter(&rp->r_statelock); 7729 if (rp->r_unldvp) { 7730 VN_RELE(ndvp); 7731 rp->r_unldvp = NULL; 7732 if (rp->r_unlcred != NULL) 7733 crfree(rp->r_unlcred); 7734 rp->r_unlcred = NULL; 7735 /* rp->r_unlanme points to tmpname */ 7736 if (rp->r_unlname) 7737 kmem_free(rp->r_unlname, MAXNAMELEN); 7738 rp->r_unlname = NULL; 7739 } 7740 mutex_exit(&rp->r_statelock); 7741 7742 goto link_call; 7743 } 7744 7745 if (error) { 7746 VN_RELE(ovp); 7747 nfs_rw_exit(&odrp->r_rwlock); 7748 nfs_rw_exit(&ndrp->r_rwlock); 7749 return (error); 7750 } 7751 7752 /* 7753 * when renaming directories to be a subdirectory of a 7754 * different parent, the dnlc entry for ".." will no 7755 * longer be valid, so it must be removed 7756 */ 7757 rp = VTOR4(ovp); 7758 if (ndvp != odvp) { 7759 if (ovp->v_type == VDIR) { 7760 dnlc_remove(ovp, ".."); 7761 if (rp->r_dir != NULL) 7762 nfs4_purge_rddir_cache(ovp); 7763 } 7764 } 7765 7766 /* 7767 * If we are renaming the unlinked file, update the 7768 * r_unldvp and r_unlname as needed. 7769 */ 7770 mutex_enter(&rp->r_statelock); 7771 if (rp->r_unldvp != NULL) { 7772 if (strcmp(rp->r_unlname, onm) == 0) { 7773 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 7774 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 7775 if (ndvp != rp->r_unldvp) { 7776 VN_RELE(rp->r_unldvp); 7777 rp->r_unldvp = ndvp; 7778 VN_HOLD(ndvp); 7779 } 7780 } 7781 } 7782 mutex_exit(&rp->r_statelock); 7783 7784 VN_RELE(ovp); 7785 7786 nfs_rw_exit(&odrp->r_rwlock); 7787 nfs_rw_exit(&ndrp->r_rwlock); 7788 7789 return (error); 7790 } 7791 7792 /* 7793 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 7794 * when it is known that the filehandle is persistent through rename. 7795 * 7796 * Rename requires that the current fh be the target directory and the 7797 * saved fh be the source directory. After the operation, the current fh 7798 * is unchanged. 7799 * The compound op structure for persistent fh rename is: 7800 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 7801 * Rather than bother with the directory postop args, we'll simply 7802 * update that a change occured in the cache, so no post-op getattrs. 7803 */ 7804 static int 7805 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 7806 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 7807 { 7808 COMPOUND4args_clnt args; 7809 COMPOUND4res_clnt res, *resp = NULL; 7810 nfs_argop4 *argop; 7811 nfs_resop4 *resop; 7812 int doqueue, argoplist_size; 7813 mntinfo4_t *mi; 7814 rnode4_t *odrp = VTOR4(odvp); 7815 rnode4_t *ndrp = VTOR4(ndvp); 7816 RENAME4res *rn_res; 7817 bool_t needrecov; 7818 nfs4_recov_state_t recov_state; 7819 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7820 dirattr_info_t dinfo, *dinfop; 7821 7822 ASSERT(curproc->p_zone == VTOMI4(odvp)->mi_zone); 7823 7824 recov_state.rs_flags = 0; 7825 recov_state.rs_num_retry_despite_err = 0; 7826 7827 /* 7828 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 7829 * 7830 * If source/target are different dirs, then append putfh(src); getattr 7831 */ 7832 args.array_len = (odvp == ndvp) ? 5 : 7; 7833 argoplist_size = args.array_len * sizeof (nfs_argop4); 7834 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 7835 7836 recov_retry: 7837 *statp = NFS4_OK; 7838 7839 /* No need to Lookup the file, persistent fh */ 7840 args.ctag = TAG_RENAME; 7841 7842 mi = VTOMI4(odvp); 7843 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 7844 if (e.error) { 7845 kmem_free(argop, argoplist_size); 7846 return (e.error); 7847 } 7848 7849 /* 0: putfh source directory */ 7850 argop[0].argop = OP_CPUTFH; 7851 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 7852 7853 /* 1: Save source fh to free up current for target */ 7854 argop[1].argop = OP_SAVEFH; 7855 7856 /* 2: putfh targetdir */ 7857 argop[2].argop = OP_CPUTFH; 7858 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 7859 7860 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 7861 argop[3].argop = OP_CRENAME; 7862 argop[3].nfs_argop4_u.opcrename.coldname = onm; 7863 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 7864 7865 /* 4: getattr (targetdir) */ 7866 argop[4].argop = OP_GETATTR; 7867 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7868 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7869 7870 if (ndvp != odvp) { 7871 7872 /* 5: putfh (sourcedir) */ 7873 argop[5].argop = OP_CPUTFH; 7874 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 7875 7876 /* 6: getattr (sourcedir) */ 7877 argop[6].argop = OP_GETATTR; 7878 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7879 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7880 } 7881 7882 dnlc_remove(odvp, onm); 7883 dnlc_remove(ndvp, nnm); 7884 7885 doqueue = 1; 7886 dinfo.di_time_call = gethrtime(); 7887 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7888 7889 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7890 if (e.error) { 7891 PURGE_ATTRCACHE4(odvp); 7892 PURGE_ATTRCACHE4(ndvp); 7893 } else { 7894 *statp = res.status; 7895 } 7896 7897 if (needrecov) { 7898 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 7899 OP_RENAME, NULL) == FALSE) { 7900 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 7901 if (!e.error) 7902 (void) xdr_free(xdr_COMPOUND4res_clnt, 7903 (caddr_t)&res); 7904 goto recov_retry; 7905 } 7906 } 7907 7908 if (!e.error) { 7909 resp = &res; 7910 /* 7911 * as long as OP_RENAME 7912 */ 7913 if (res.status != NFS4_OK && res.array_len <= 4) { 7914 e.error = geterrno4(res.status); 7915 PURGE_ATTRCACHE4(odvp); 7916 PURGE_ATTRCACHE4(ndvp); 7917 /* 7918 * System V defines rename to return EEXIST, not 7919 * ENOTEMPTY if the target directory is not empty. 7920 * Over the wire, the error is NFSERR_ENOTEMPTY 7921 * which geterrno4 maps to ENOTEMPTY. 7922 */ 7923 if (e.error == ENOTEMPTY) 7924 e.error = EEXIST; 7925 } else { 7926 7927 resop = &res.array[3]; /* rename res */ 7928 rn_res = &resop->nfs_resop4_u.oprename; 7929 7930 if (res.status == NFS4_OK) { 7931 /* 7932 * Update target attribute, readdir and dnlc 7933 * caches. 7934 */ 7935 dinfo.di_garp = 7936 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7937 dinfo.di_cred = cr; 7938 dinfop = &dinfo; 7939 } else 7940 dinfop = NULL; 7941 7942 nfs4_update_dircaches(&rn_res->target_cinfo, 7943 ndvp, NULL, NULL, dinfop); 7944 7945 /* 7946 * Update source attribute, readdir and dnlc caches 7947 * 7948 */ 7949 if (ndvp != odvp) { 7950 if (dinfop) 7951 dinfo.di_garp = 7952 &(res.array[6].nfs_resop4_u. 7953 opgetattr.ga_res); 7954 7955 nfs4_update_dircaches(&rn_res->source_cinfo, 7956 odvp, NULL, NULL, dinfop); 7957 } 7958 7959 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 7960 nnm); 7961 } 7962 } 7963 7964 if (resp) 7965 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7966 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 7967 kmem_free(argop, argoplist_size); 7968 7969 return (e.error); 7970 } 7971 7972 /* 7973 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 7974 * it is possible for the filehandle to change due to the rename. 7975 * 7976 * The compound req in this case includes a post-rename lookup and getattr 7977 * to ensure that we have the correct fh and attributes for the object. 7978 * 7979 * Rename requires that the current fh be the target directory and the 7980 * saved fh be the source directory. After the operation, the current fh 7981 * is unchanged. 7982 * 7983 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 7984 * update the filehandle for the renamed object. We also get the old 7985 * filehandle for historical reasons; this should be taken out sometime. 7986 * This results in a rather cumbersome compound... 7987 * 7988 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 7989 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 7990 * 7991 */ 7992 static int 7993 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 7994 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 7995 { 7996 COMPOUND4args_clnt args; 7997 COMPOUND4res_clnt res, *resp = NULL; 7998 int argoplist_size; 7999 nfs_argop4 *argop; 8000 nfs_resop4 *resop; 8001 int doqueue; 8002 mntinfo4_t *mi; 8003 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8004 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8005 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8006 RENAME4res *rn_res; 8007 GETFH4res *ngf_res; 8008 bool_t needrecov; 8009 nfs4_recov_state_t recov_state; 8010 hrtime_t t; 8011 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8012 dirattr_info_t dinfo, *dinfop = &dinfo; 8013 8014 ASSERT(curproc->p_zone == VTOMI4(odvp)->mi_zone); 8015 8016 recov_state.rs_flags = 0; 8017 recov_state.rs_num_retry_despite_err = 0; 8018 8019 recov_retry: 8020 *statp = NFS4_OK; 8021 8022 /* 8023 * There is a window between the RPC and updating the path and 8024 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8025 * code, so that it doesn't try to use the old path during that 8026 * window. 8027 */ 8028 mutex_enter(&orp->r_statelock); 8029 while (orp->r_flags & R4RECEXPFH) { 8030 klwp_t *lwp = ttolwp(curthread); 8031 8032 if (lwp != NULL) 8033 lwp->lwp_nostop++; 8034 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8035 mutex_exit(&orp->r_statelock); 8036 if (lwp != NULL) 8037 lwp->lwp_nostop--; 8038 return (EINTR); 8039 } 8040 if (lwp != NULL) 8041 lwp->lwp_nostop--; 8042 } 8043 orp->r_flags |= R4RECEXPFH; 8044 mutex_exit(&orp->r_statelock); 8045 8046 mi = VTOMI4(odvp); 8047 8048 args.ctag = TAG_RENAME_VFH; 8049 args.array_len = (odvp == ndvp) ? 10 : 12; 8050 argoplist_size = args.array_len * sizeof (nfs_argop4); 8051 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8052 8053 /* 8054 * Rename ops: 8055 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8056 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8057 * LOOKUP(trgt), GETFH(new), GETATTR, 8058 * 8059 * if (odvp != ndvp) 8060 * add putfh(sourcedir), getattr(sourcedir) } 8061 */ 8062 args.array = argop; 8063 8064 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8065 &recov_state, NULL); 8066 if (e.error) { 8067 kmem_free(argop, argoplist_size); 8068 mutex_enter(&orp->r_statelock); 8069 orp->r_flags &= ~R4RECEXPFH; 8070 cv_broadcast(&orp->r_cv); 8071 mutex_exit(&orp->r_statelock); 8072 return (e.error); 8073 } 8074 8075 /* 0: putfh source directory */ 8076 argop[0].argop = OP_CPUTFH; 8077 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8078 8079 /* 1: Save source fh to free up current for target */ 8080 argop[1].argop = OP_SAVEFH; 8081 8082 /* 2: Lookup pre-rename fh of renamed object */ 8083 argop[2].argop = OP_CLOOKUP; 8084 argop[2].nfs_argop4_u.opclookup.cname = onm; 8085 8086 /* 3: getfh fh of renamed object (before rename) */ 8087 argop[3].argop = OP_GETFH; 8088 8089 /* 4: putfh targetdir */ 8090 argop[4].argop = OP_CPUTFH; 8091 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8092 8093 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8094 argop[5].argop = OP_CRENAME; 8095 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8096 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8097 8098 /* 6: getattr of target dir (post op attrs) */ 8099 argop[6].argop = OP_GETATTR; 8100 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8101 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8102 8103 /* 7: Lookup post-rename fh of renamed object */ 8104 argop[7].argop = OP_CLOOKUP; 8105 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8106 8107 /* 8: getfh fh of renamed object (after rename) */ 8108 argop[8].argop = OP_GETFH; 8109 8110 /* 9: getattr of renamed object */ 8111 argop[9].argop = OP_GETATTR; 8112 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8113 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8114 8115 /* 8116 * If source/target dirs are different, then get new post-op 8117 * attrs for source dir also. 8118 */ 8119 if (ndvp != odvp) { 8120 /* 10: putfh (sourcedir) */ 8121 argop[10].argop = OP_CPUTFH; 8122 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8123 8124 /* 11: getattr (sourcedir) */ 8125 argop[11].argop = OP_GETATTR; 8126 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8127 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8128 } 8129 8130 dnlc_remove(odvp, onm); 8131 dnlc_remove(ndvp, nnm); 8132 8133 doqueue = 1; 8134 t = gethrtime(); 8135 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8136 8137 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8138 if (e.error) { 8139 PURGE_ATTRCACHE4(odvp); 8140 PURGE_ATTRCACHE4(ndvp); 8141 if (!needrecov) { 8142 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8143 &recov_state, needrecov); 8144 goto out; 8145 } 8146 } else { 8147 *statp = res.status; 8148 } 8149 8150 if (needrecov) { 8151 bool_t abort; 8152 8153 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8154 OP_RENAME, NULL); 8155 if (abort == FALSE) { 8156 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8157 &recov_state, needrecov); 8158 kmem_free(argop, argoplist_size); 8159 if (!e.error) 8160 (void) xdr_free(xdr_COMPOUND4res_clnt, 8161 (caddr_t)&res); 8162 mutex_enter(&orp->r_statelock); 8163 orp->r_flags &= ~R4RECEXPFH; 8164 cv_broadcast(&orp->r_cv); 8165 mutex_exit(&orp->r_statelock); 8166 goto recov_retry; 8167 } else { 8168 if (e.error != 0) { 8169 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8170 &recov_state, needrecov); 8171 goto out; 8172 } 8173 /* fall through for res.status case */ 8174 } 8175 } 8176 8177 resp = &res; 8178 /* 8179 * If OP_RENAME (or any prev op) failed, then return an error. 8180 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8181 */ 8182 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8183 /* 8184 * Error in an op other than last Getattr 8185 */ 8186 e.error = geterrno4(res.status); 8187 PURGE_ATTRCACHE4(odvp); 8188 PURGE_ATTRCACHE4(ndvp); 8189 /* 8190 * System V defines rename to return EEXIST, not 8191 * ENOTEMPTY if the target directory is not empty. 8192 * Over the wire, the error is NFSERR_ENOTEMPTY 8193 * which geterrno4 maps to ENOTEMPTY. 8194 */ 8195 if (e.error == ENOTEMPTY) 8196 e.error = EEXIST; 8197 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8198 needrecov); 8199 goto out; 8200 } 8201 8202 /* rename results */ 8203 rn_res = &res.array[5].nfs_resop4_u.oprename; 8204 8205 if (res.status == NFS4_OK) { 8206 /* Update target attribute, readdir and dnlc caches */ 8207 dinfo.di_garp = 8208 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8209 dinfo.di_cred = cr; 8210 dinfo.di_time_call = t; 8211 } else 8212 dinfop = NULL; 8213 8214 /* Update source cache attribute, readdir and dnlc caches */ 8215 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8216 8217 /* Update source cache attribute, readdir and dnlc caches */ 8218 if (ndvp != odvp) { 8219 8220 /* 8221 * If dinfop is non-NULL, then compound succeded, so 8222 * set di_garp to attrs for source dir. dinfop is only 8223 * set to NULL when compound fails. 8224 */ 8225 if (dinfop) 8226 dinfo.di_garp = 8227 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8228 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8229 dinfop); 8230 } 8231 8232 /* 8233 * Update the rnode with the new component name and args, 8234 * and if the file handle changed, also update it with the new fh. 8235 * This is only necessary if the target object has an rnode 8236 * entry and there is no need to create one for it. 8237 */ 8238 resop = &res.array[8]; /* getfh new res */ 8239 ngf_res = &resop->nfs_resop4_u.opgetfh; 8240 8241 /* 8242 * Update the path and filehandle for the renamed object. 8243 */ 8244 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8245 8246 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8247 8248 if (res.status == NFS4_OK) { 8249 resop++; /* getattr res */ 8250 e.error = nfs4_update_attrcache(res.status, 8251 &resop->nfs_resop4_u.opgetattr.ga_res, 8252 t, ovp, cr); 8253 } 8254 8255 out: 8256 kmem_free(argop, argoplist_size); 8257 if (resp) 8258 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8259 mutex_enter(&orp->r_statelock); 8260 orp->r_flags &= ~R4RECEXPFH; 8261 cv_broadcast(&orp->r_cv); 8262 mutex_exit(&orp->r_statelock); 8263 8264 return (e.error); 8265 } 8266 8267 static int 8268 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr) 8269 { 8270 int error; 8271 vnode_t *vp; 8272 8273 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 8274 return (EPERM); 8275 /* 8276 * As ".." has special meaning and rather than send a mkdir 8277 * over the wire to just let the server freak out, we just 8278 * short circuit it here and return EEXIST 8279 */ 8280 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8281 return (EEXIST); 8282 8283 /* 8284 * Decision to get the right gid and setgid bit of the 8285 * new directory is now made in call_nfs4_create_req. 8286 */ 8287 va->va_mask |= AT_MODE; 8288 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8289 if (error) 8290 return (error); 8291 8292 *vpp = vp; 8293 return (0); 8294 } 8295 8296 8297 /* 8298 * rmdir is using the same remove v4 op as does remove. 8299 * Remove requires that the current fh be the target directory. 8300 * After the operation, the current fh is unchanged. 8301 * The compound op structure is: 8302 * PUTFH(targetdir), REMOVE 8303 */ 8304 static int 8305 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr) 8306 { 8307 int need_end_op = FALSE; 8308 COMPOUND4args_clnt args; 8309 COMPOUND4res_clnt res, *resp = NULL; 8310 REMOVE4res *rm_res; 8311 nfs_argop4 argop[3]; 8312 nfs_resop4 *resop; 8313 vnode_t *vp; 8314 int doqueue; 8315 mntinfo4_t *mi; 8316 rnode4_t *drp; 8317 bool_t needrecov = FALSE; 8318 nfs4_recov_state_t recov_state; 8319 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8320 dirattr_info_t dinfo, *dinfop; 8321 8322 if (curproc->p_zone != VTOMI4(dvp)->mi_zone) 8323 return (EPERM); 8324 /* 8325 * As ".." has special meaning and rather than send a rmdir 8326 * over the wire to just let the server freak out, we just 8327 * short circuit it here and return EEXIST 8328 */ 8329 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8330 return (EEXIST); 8331 8332 drp = VTOR4(dvp); 8333 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8334 return (EINTR); 8335 8336 /* 8337 * Attempt to prevent a rmdir(".") from succeeding. 8338 */ 8339 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8340 if (e.error) { 8341 nfs_rw_exit(&drp->r_rwlock); 8342 return (e.error); 8343 } 8344 if (vp == cdir) { 8345 VN_RELE(vp); 8346 nfs_rw_exit(&drp->r_rwlock); 8347 return (EINVAL); 8348 } 8349 8350 /* 8351 * Since nfsv4 remove op works on both files and directories, 8352 * check that the removed object is indeed a directory. 8353 */ 8354 if (vp->v_type != VDIR) { 8355 VN_RELE(vp); 8356 nfs_rw_exit(&drp->r_rwlock); 8357 return (ENOTDIR); 8358 } 8359 8360 /* 8361 * First just remove the entry from the name cache, as it 8362 * is most likely an entry for this vp. 8363 */ 8364 dnlc_remove(dvp, nm); 8365 8366 /* 8367 * If there vnode reference count is greater than one, then 8368 * there may be additional references in the DNLC which will 8369 * need to be purged. First, trying removing the entry for 8370 * the parent directory and see if that removes the additional 8371 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8372 * to completely remove any references to the directory which 8373 * might still exist in the DNLC. 8374 */ 8375 if (vp->v_count > 1) { 8376 dnlc_remove(vp, ".."); 8377 if (vp->v_count > 1) 8378 dnlc_purge_vp(vp); 8379 } 8380 8381 mi = VTOMI4(dvp); 8382 recov_state.rs_flags = 0; 8383 recov_state.rs_num_retry_despite_err = 0; 8384 8385 recov_retry: 8386 args.ctag = TAG_RMDIR; 8387 8388 /* 8389 * Rmdir ops: putfh dir; remove 8390 */ 8391 args.array_len = 3; 8392 args.array = argop; 8393 8394 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8395 if (e.error) { 8396 nfs_rw_exit(&drp->r_rwlock); 8397 return (e.error); 8398 } 8399 need_end_op = TRUE; 8400 8401 /* putfh directory */ 8402 argop[0].argop = OP_CPUTFH; 8403 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8404 8405 /* remove */ 8406 argop[1].argop = OP_CREMOVE; 8407 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8408 8409 /* getattr (postop attrs for dir that contained removed dir) */ 8410 argop[2].argop = OP_GETATTR; 8411 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8412 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8413 8414 dinfo.di_time_call = gethrtime(); 8415 doqueue = 1; 8416 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8417 8418 PURGE_ATTRCACHE4(vp); 8419 8420 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8421 if (e.error) { 8422 PURGE_ATTRCACHE4(dvp); 8423 } 8424 8425 if (needrecov) { 8426 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8427 NULL, OP_REMOVE, NULL) == FALSE) { 8428 if (!e.error) 8429 (void) xdr_free(xdr_COMPOUND4res_clnt, 8430 (caddr_t)&res); 8431 8432 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8433 needrecov); 8434 need_end_op = FALSE; 8435 goto recov_retry; 8436 } 8437 } 8438 8439 if (!e.error) { 8440 resp = &res; 8441 8442 /* 8443 * Only return error if first 2 ops (OP_REMOVE or earlier) 8444 * failed. 8445 */ 8446 if (res.status != NFS4_OK && res.array_len <= 2) { 8447 e.error = geterrno4(res.status); 8448 PURGE_ATTRCACHE4(dvp); 8449 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8450 &recov_state, needrecov); 8451 need_end_op = FALSE; 8452 nfs4_purge_stale_fh(e.error, dvp, cr); 8453 /* 8454 * System V defines rmdir to return EEXIST, not 8455 * ENOTEMPTY if the directory is not empty. Over 8456 * the wire, the error is NFSERR_ENOTEMPTY which 8457 * geterrno4 maps to ENOTEMPTY. 8458 */ 8459 if (e.error == ENOTEMPTY) 8460 e.error = EEXIST; 8461 } else { 8462 resop = &res.array[1]; /* remove res */ 8463 rm_res = &resop->nfs_resop4_u.opremove; 8464 8465 if (res.status == NFS4_OK) { 8466 resop = &res.array[2]; /* dir attrs */ 8467 dinfo.di_garp = 8468 &resop->nfs_resop4_u.opgetattr.ga_res; 8469 dinfo.di_cred = cr; 8470 dinfop = &dinfo; 8471 } else 8472 dinfop = NULL; 8473 8474 /* Update dir attribute, readdir and dnlc caches */ 8475 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8476 dinfop); 8477 8478 /* destroy rddir cache for dir that was removed */ 8479 if (VTOR4(vp)->r_dir != NULL) 8480 nfs4_purge_rddir_cache(vp); 8481 } 8482 } 8483 8484 if (need_end_op) 8485 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8486 8487 nfs_rw_exit(&drp->r_rwlock); 8488 8489 if (resp) 8490 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8491 8492 VN_RELE(vp); 8493 8494 return (e.error); 8495 } 8496 8497 static int 8498 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr) 8499 { 8500 int error; 8501 vnode_t *vp; 8502 rnode4_t *rp; 8503 char *contents; 8504 mntinfo4_t *mi = VTOMI4(dvp); 8505 8506 if (curproc->p_zone != mi->mi_zone) 8507 return (EPERM); 8508 if (!(mi->mi_flags & MI4_SYMLINK)) 8509 return (EOPNOTSUPP); 8510 8511 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8512 if (error) { 8513 return (error); 8514 } 8515 8516 ASSERT(nfs4_consistent_type(vp)); 8517 rp = VTOR4(vp); 8518 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8519 8520 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8521 8522 if (contents != NULL) { 8523 mutex_enter(&rp->r_statelock); 8524 if (rp->r_symlink.contents == NULL) { 8525 rp->r_symlink.len = strlen(tnm); 8526 bcopy(tnm, contents, rp->r_symlink.len); 8527 rp->r_symlink.contents = contents; 8528 rp->r_symlink.size = MAXPATHLEN; 8529 mutex_exit(&rp->r_statelock); 8530 } else { 8531 mutex_exit(&rp->r_statelock); 8532 kmem_free((void *)contents, MAXPATHLEN); 8533 } 8534 } 8535 } 8536 VN_RELE(vp); 8537 8538 return (error); 8539 } 8540 8541 8542 /* 8543 * Read directory entries. 8544 * There are some weird things to look out for here. The uio_loffset 8545 * field is either 0 or it is the offset returned from a previous 8546 * readdir. It is an opaque value used by the server to find the 8547 * correct directory block to read. The count field is the number 8548 * of blocks to read on the server. This is advisory only, the server 8549 * may return only one block's worth of entries. Entries may be compressed 8550 * on the server. 8551 */ 8552 static int 8553 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp) 8554 { 8555 int error; 8556 uint_t count; 8557 rnode4_t *rp; 8558 rddir4_cache *rdc; 8559 rddir4_cache *rrdc; 8560 8561 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 8562 return (EIO); 8563 rp = VTOR4(vp); 8564 8565 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8566 8567 /* 8568 * Make sure that the directory cache is valid. 8569 */ 8570 if (rp->r_dir != NULL) { 8571 if (nfs_disable_rddir_cache != 0) { 8572 /* 8573 * Setting nfs_disable_rddir_cache in /etc/system 8574 * allows interoperability with servers that do not 8575 * properly update the attributes of directories. 8576 * Any cached information gets purged before an 8577 * access is made to it. 8578 */ 8579 nfs4_purge_rddir_cache(vp); 8580 } 8581 8582 error = nfs4_validate_caches(vp, cr); 8583 if (error) 8584 return (error); 8585 } 8586 8587 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8588 8589 /* 8590 * Short circuit last readdir which always returns 0 bytes. 8591 * This can be done after the directory has been read through 8592 * completely at least once. This will set r_direof which 8593 * can be used to find the value of the last cookie. 8594 */ 8595 mutex_enter(&rp->r_statelock); 8596 if (rp->r_direof != NULL && 8597 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8598 mutex_exit(&rp->r_statelock); 8599 #ifdef DEBUG 8600 nfs4_readdir_cache_shorts++; 8601 #endif 8602 if (eofp) 8603 *eofp = 1; 8604 return (0); 8605 } 8606 8607 /* 8608 * Look for a cache entry. Cache entries are identified 8609 * by the NFS cookie value and the byte count requested. 8610 */ 8611 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8612 8613 /* 8614 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8615 */ 8616 if (rdc == NULL) { 8617 mutex_exit(&rp->r_statelock); 8618 return (EINTR); 8619 } 8620 8621 /* 8622 * Check to see if we need to fill this entry in. 8623 */ 8624 if (rdc->flags & RDDIRREQ) { 8625 rdc->flags &= ~RDDIRREQ; 8626 rdc->flags |= RDDIR; 8627 mutex_exit(&rp->r_statelock); 8628 8629 /* 8630 * Do the readdir. 8631 */ 8632 nfs4readdir(vp, rdc, cr); 8633 8634 /* 8635 * Reaquire the lock, so that we can continue 8636 */ 8637 mutex_enter(&rp->r_statelock); 8638 /* 8639 * The entry is now complete 8640 */ 8641 rdc->flags &= ~RDDIR; 8642 } 8643 8644 ASSERT(!(rdc->flags & RDDIR)); 8645 8646 /* 8647 * If an error occurred while attempting 8648 * to fill the cache entry, mark the entry invalid and 8649 * just return the error. 8650 */ 8651 if (rdc->error) { 8652 error = rdc->error; 8653 rdc->flags |= RDDIRREQ; 8654 rddir4_cache_rele(rp, rdc); 8655 mutex_exit(&rp->r_statelock); 8656 return (error); 8657 } 8658 8659 /* 8660 * The cache entry is complete and good, 8661 * copyout the dirent structs to the calling 8662 * thread. 8663 */ 8664 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 8665 8666 /* 8667 * If no error occurred during the copyout, 8668 * update the offset in the uio struct to 8669 * contain the value of the next NFS 4 cookie 8670 * and set the eof value appropriately. 8671 */ 8672 if (!error) { 8673 uiop->uio_loffset = rdc->nfs4_ncookie; 8674 if (eofp) 8675 *eofp = rdc->eof; 8676 } 8677 8678 /* 8679 * Decide whether to do readahead. Don't if we 8680 * have already read to the end of directory. 8681 */ 8682 if (rdc->eof) { 8683 /* 8684 * Make the entry the direof only if it is cached 8685 */ 8686 if (rdc->flags & RDDIRCACHED) 8687 rp->r_direof = rdc; 8688 rddir4_cache_rele(rp, rdc); 8689 mutex_exit(&rp->r_statelock); 8690 return (error); 8691 } 8692 8693 /* Determine if a readdir readahead should be done */ 8694 if (!(rp->r_flags & R4LOOKUP)) { 8695 rddir4_cache_rele(rp, rdc); 8696 mutex_exit(&rp->r_statelock); 8697 return (error); 8698 } 8699 8700 /* 8701 * Now look for a readahead entry. 8702 * 8703 * Check to see whether we found an entry for the readahead. 8704 * If so, we don't need to do anything further, so free the new 8705 * entry if one was allocated. Otherwise, allocate a new entry, add 8706 * it to the cache, and then initiate an asynchronous readdir 8707 * operation to fill it. 8708 */ 8709 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 8710 8711 /* 8712 * A readdir cache entry could not be obtained for the readahead. In 8713 * this case we skip the readahead and return. 8714 */ 8715 if (rrdc == NULL) { 8716 rddir4_cache_rele(rp, rdc); 8717 mutex_exit(&rp->r_statelock); 8718 return (error); 8719 } 8720 8721 /* 8722 * Check to see if we need to fill this entry in. 8723 */ 8724 if (rrdc->flags & RDDIRREQ) { 8725 rrdc->flags &= ~RDDIRREQ; 8726 rrdc->flags |= RDDIR; 8727 rddir4_cache_rele(rp, rdc); 8728 mutex_exit(&rp->r_statelock); 8729 #ifdef DEBUG 8730 nfs4_readdir_readahead++; 8731 #endif 8732 /* 8733 * Do the readdir. 8734 */ 8735 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 8736 return (error); 8737 } 8738 8739 rddir4_cache_rele(rp, rrdc); 8740 rddir4_cache_rele(rp, rdc); 8741 mutex_exit(&rp->r_statelock); 8742 return (error); 8743 } 8744 8745 static int 8746 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8747 { 8748 int error; 8749 rnode4_t *rp; 8750 8751 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 8752 8753 rp = VTOR4(vp); 8754 8755 /* 8756 * Obtain the readdir results for the caller. 8757 */ 8758 nfs4readdir(vp, rdc, cr); 8759 8760 mutex_enter(&rp->r_statelock); 8761 /* 8762 * The entry is now complete 8763 */ 8764 rdc->flags &= ~RDDIR; 8765 8766 error = rdc->error; 8767 if (error) 8768 rdc->flags |= RDDIRREQ; 8769 rddir4_cache_rele(rp, rdc); 8770 mutex_exit(&rp->r_statelock); 8771 8772 return (error); 8773 } 8774 8775 static void 8776 nfs4readdir_stub(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8777 { 8778 int stublength; 8779 dirent64_t *dp; 8780 u_longlong_t nodeid, pnodeid; 8781 vnode_t *dotdotvp = NULL; 8782 rnode4_t *rp = VTOR4(vp); 8783 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 8784 8785 rdc->error = 0; 8786 rdc->entries = 0; 8787 rdc->actlen = rdc->entlen = 0; 8788 rdc->eof = TRUE; 8789 8790 /* Check for EOF case for readdir of stub */ 8791 if (cookie != 0 && cookie != 1) 8792 return; 8793 8794 nodeid = rp->r_attr.va_nodeid; 8795 if (vp->v_flag & VROOT) { 8796 pnodeid = nodeid; /* root of mount point */ 8797 } else { 8798 if (rdc->error = nfs4_lookup(vp, "..", &dotdotvp, 0, 0, 0, cr)) 8799 return; 8800 pnodeid = VTOR4(dotdotvp)->r_attr.va_nodeid; 8801 VN_RELE(dotdotvp); 8802 } 8803 8804 stublength = DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2); 8805 rdc->entries = kmem_alloc(stublength, KM_SLEEP); 8806 rdc->entlen = rdc->buflen = stublength; 8807 rdc->eof = TRUE; 8808 8809 dp = (dirent64_t *)rdc->entries; 8810 8811 if (rdc->nfs4_cookie == (nfs_cookie4)0) { 8812 bcopy(nfs4_dot_entries, rdc->entries, 8813 DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2)); 8814 dp->d_ino = nodeid; 8815 dp = (struct dirent64 *)(((char *)dp) + DIRENT64_RECLEN(1)); 8816 dp->d_ino = pnodeid; 8817 rdc->actlen = DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2); 8818 } else { /* for ".." entry */ 8819 bcopy(nfs4_dot_dot_entry, rdc->entries, DIRENT64_RECLEN(2)); 8820 dp->d_ino = pnodeid; 8821 rdc->actlen = DIRENT64_RECLEN(2); 8822 } 8823 rdc->nfs4_ncookie = rdc->actlen; 8824 } 8825 8826 /* 8827 * Read directory entries. 8828 * There are some weird things to look out for here. The uio_loffset 8829 * field is either 0 or it is the offset returned from a previous 8830 * readdir. It is an opaque value used by the server to find the 8831 * correct directory block to read. The count field is the number 8832 * of blocks to read on the server. This is advisory only, the server 8833 * may return only one block's worth of entries. Entries may be compressed 8834 * on the server. 8835 * 8836 * Generates the following compound request: 8837 * 1. If readdir offset is zero and no dnlc entry for parent exists, 8838 * must include a Lookupp as well. In this case, send: 8839 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 8840 * 2. Otherwise just do: { Putfh <fh>; Readdir } 8841 * 8842 * Get complete attributes and filehandles for entries if this is the 8843 * first read of the directory. Otherwise, just get fileid's. 8844 */ 8845 static void 8846 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8847 { 8848 COMPOUND4args_clnt args; 8849 COMPOUND4res_clnt res; 8850 READDIR4args *rargs; 8851 READDIR4res_clnt *rd_res; 8852 bitmap4 rd_bitsval; 8853 nfs_argop4 argop[5]; 8854 nfs_resop4 *resop; 8855 rnode4_t *rp = VTOR4(vp); 8856 mntinfo4_t *mi = VTOMI4(vp); 8857 int doqueue; 8858 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 8859 vnode_t *dvp; 8860 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 8861 int num_ops, res_opcnt; 8862 bool_t needrecov = FALSE; 8863 nfs4_recov_state_t recov_state; 8864 hrtime_t t; 8865 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8866 8867 ASSERT(curproc->p_zone == mi->mi_zone); 8868 ASSERT(rdc->flags & RDDIR); 8869 ASSERT(rdc->entries == NULL); 8870 8871 if (rp->r_flags & R4SRVSTUB) { 8872 nfs4readdir_stub(vp, rdc, cr); 8873 return; 8874 } 8875 8876 num_ops = 2; 8877 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 8878 /* 8879 * Since nfsv4 readdir may not return entries for "." and "..", 8880 * the client must recreate them: 8881 * To find the correct nodeid, do the following: 8882 * For current node, get nodeid from dnlc. 8883 * - if current node is rootvp, set pnodeid to nodeid. 8884 * - else if parent is in the dnlc, get its nodeid from there. 8885 * - else add LOOKUPP+GETATTR to compound. 8886 */ 8887 nodeid = rp->r_attr.va_nodeid; 8888 if (vp->v_flag & VROOT) { 8889 pnodeid = nodeid; /* root of mount point */ 8890 } else { 8891 dvp = dnlc_lookup(vp, ".."); 8892 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 8893 /* parent in dnlc cache - no need for otw */ 8894 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 8895 } else { 8896 /* 8897 * parent not in dnlc cache, 8898 * do lookupp to get its id 8899 */ 8900 num_ops = 5; 8901 pnodeid = 0; /* set later by getattr parent */ 8902 } 8903 if (dvp) 8904 VN_RELE(dvp); 8905 } 8906 } 8907 recov_state.rs_flags = 0; 8908 recov_state.rs_num_retry_despite_err = 0; 8909 8910 /* Save the original mount point security flavor */ 8911 (void) save_mnt_secinfo(mi->mi_curr_serv); 8912 8913 recov_retry: 8914 args.ctag = TAG_READDIR; 8915 8916 args.array = argop; 8917 args.array_len = num_ops; 8918 8919 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 8920 &recov_state, NULL)) { 8921 /* 8922 * If readdir a node that is a stub for a crossed mount point, 8923 * keep the original secinfo flavor for the current file 8924 * system, not the crossed one. 8925 */ 8926 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 8927 rdc->error = e.error; 8928 return; 8929 } 8930 8931 /* 8932 * Determine which attrs to request for dirents. This code 8933 * must be protected by nfs4_start/end_fop because of r_server 8934 * (which will change during failover recovery). 8935 * 8936 */ 8937 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 8938 /* 8939 * Get all vattr attrs plus filehandle and rdattr_error 8940 */ 8941 rd_bitsval = NFS4_VATTR_MASK | 8942 FATTR4_RDATTR_ERROR_MASK | 8943 FATTR4_FILEHANDLE_MASK; 8944 8945 if (rp->r_flags & R4READDIRWATTR) { 8946 mutex_enter(&rp->r_statelock); 8947 rp->r_flags &= ~R4READDIRWATTR; 8948 mutex_exit(&rp->r_statelock); 8949 } 8950 } else { 8951 servinfo4_t *svp = rp->r_server; 8952 8953 /* 8954 * Already read directory. Use readdir with 8955 * no attrs (except for mounted_on_fileid) for updates. 8956 */ 8957 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 8958 8959 /* 8960 * request mounted on fileid if supported, else request 8961 * fileid. maybe we should verify that fileid is supported 8962 * and request something else if not. 8963 */ 8964 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 8965 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 8966 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 8967 nfs_rw_exit(&svp->sv_lock); 8968 } 8969 8970 /* putfh directory fh */ 8971 argop[0].argop = OP_CPUTFH; 8972 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 8973 8974 argop[1].argop = OP_READDIR; 8975 rargs = &argop[1].nfs_argop4_u.opreaddir; 8976 /* 8977 * 1 and 2 are reserved for client "." and ".." entry offset. 8978 * cookie 0 should be used over-the-wire to start reading at 8979 * the beginning of the directory excluding "." and "..". 8980 */ 8981 if (rdc->nfs4_cookie == 0 || 8982 rdc->nfs4_cookie == 1 || 8983 rdc->nfs4_cookie == 2) { 8984 rargs->cookie = (nfs_cookie4)0; 8985 rargs->cookieverf = 0; 8986 } else { 8987 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 8988 mutex_enter(&rp->r_statelock); 8989 rargs->cookieverf = rp->r_cookieverf4; 8990 mutex_exit(&rp->r_statelock); 8991 } 8992 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 8993 rargs->maxcount = mi->mi_tsize; 8994 rargs->attr_request = rd_bitsval; 8995 rargs->rdc = rdc; 8996 rargs->dvp = vp; 8997 rargs->mi = mi; 8998 rargs->cr = cr; 8999 9000 9001 /* 9002 * If count < than the minimum required, we return no entries 9003 * and fail with EINVAL 9004 */ 9005 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9006 rdc->error = EINVAL; 9007 goto out; 9008 } 9009 9010 if (args.array_len == 5) { 9011 /* 9012 * Add lookupp and getattr for parent nodeid. 9013 */ 9014 argop[2].argop = OP_LOOKUPP; 9015 9016 argop[3].argop = OP_GETFH; 9017 9018 /* getattr parent */ 9019 argop[4].argop = OP_GETATTR; 9020 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9021 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9022 } 9023 9024 doqueue = 1; 9025 9026 if (mi->mi_io_kstats) { 9027 mutex_enter(&mi->mi_lock); 9028 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9029 mutex_exit(&mi->mi_lock); 9030 } 9031 9032 /* capture the time of this call */ 9033 rargs->t = t = gethrtime(); 9034 9035 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9036 9037 if (mi->mi_io_kstats) { 9038 mutex_enter(&mi->mi_lock); 9039 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9040 mutex_exit(&mi->mi_lock); 9041 } 9042 9043 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9044 9045 /* 9046 * If RPC error occurred and it isn't an error that 9047 * triggers recovery, then go ahead and fail now. 9048 */ 9049 if (e.error != 0 && !needrecov) { 9050 rdc->error = e.error; 9051 goto out; 9052 } 9053 9054 if (needrecov) { 9055 bool_t abort; 9056 9057 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9058 "nfs4readdir: initiating recovery.\n")); 9059 9060 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9061 NULL, OP_READDIR, NULL); 9062 if (abort == FALSE) { 9063 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9064 &recov_state, needrecov); 9065 if (!e.error) 9066 (void) xdr_free(xdr_COMPOUND4res_clnt, 9067 (caddr_t)&res); 9068 if (rdc->entries != NULL) { 9069 kmem_free(rdc->entries, rdc->entlen); 9070 rdc->entries = NULL; 9071 } 9072 goto recov_retry; 9073 } 9074 9075 if (e.error != 0) { 9076 rdc->error = e.error; 9077 goto out; 9078 } 9079 9080 /* fall through for res.status case */ 9081 } 9082 9083 res_opcnt = res.array_len; 9084 9085 /* 9086 * If compound failed first 2 ops (PUTFH+READDIR), then return 9087 * failure here. Subsequent ops are for filling out dot-dot 9088 * dirent, and if they fail, we still want to give the caller 9089 * the dirents returned by (the successful) READDIR op, so we need 9090 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9091 * 9092 * One example where PUTFH+READDIR ops would succeed but 9093 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9094 * but lacks x. In this case, a POSIX server's VOP_READDIR 9095 * would succeed; however, VOP_LOOKUP(..) would fail since no 9096 * x perm. We need to come up with a non-vendor-specific way 9097 * for a POSIX server to return d_ino from dotdot's dirent if 9098 * client only requests mounted_on_fileid, and just say the 9099 * LOOKUPP succeeded and fill out the GETATTR. However, if 9100 * client requested any mandatory attrs, server would be required 9101 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9102 * for dotdot. 9103 */ 9104 9105 if (res.status) { 9106 if (res_opcnt <= 2) { 9107 e.error = geterrno4(res.status); 9108 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9109 &recov_state, needrecov); 9110 nfs4_purge_stale_fh(e.error, vp, cr); 9111 rdc->error = e.error; 9112 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9113 if (rdc->entries != NULL) { 9114 kmem_free(rdc->entries, rdc->entlen); 9115 rdc->entries = NULL; 9116 } 9117 /* 9118 * If readdir a node that is a stub for a 9119 * crossed mount point, keep the original 9120 * secinfo flavor for the current file system, 9121 * not the crossed one. 9122 */ 9123 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9124 return; 9125 } 9126 } 9127 9128 resop = &res.array[1]; /* readdir res */ 9129 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9130 9131 mutex_enter(&rp->r_statelock); 9132 rp->r_cookieverf4 = rd_res->cookieverf; 9133 mutex_exit(&rp->r_statelock); 9134 9135 /* 9136 * For "." and ".." entries 9137 * e.g. 9138 * seek(cookie=0) -> "." entry with d_off = 1 9139 * seek(cookie=1) -> ".." entry with d_off = 2 9140 */ 9141 if (cookie == (nfs_cookie4) 0) { 9142 if (rd_res->dotp) 9143 rd_res->dotp->d_ino = nodeid; 9144 if (rd_res->dotdotp) 9145 rd_res->dotdotp->d_ino = pnodeid; 9146 } 9147 if (cookie == (nfs_cookie4) 1) { 9148 if (rd_res->dotdotp) 9149 rd_res->dotdotp->d_ino = pnodeid; 9150 } 9151 9152 9153 /* LOOKUPP+GETATTR attemped */ 9154 if (args.array_len == 5 && rd_res->dotdotp) { 9155 if (res.status == NFS4_OK && res_opcnt == 5) { 9156 nfs_fh4 *fhp; 9157 nfs4_sharedfh_t *sfhp; 9158 vnode_t *pvp; 9159 nfs4_ga_res_t *garp; 9160 9161 resop++; /* lookupp */ 9162 resop++; /* getfh */ 9163 fhp = &resop->nfs_resop4_u.opgetfh.object; 9164 9165 resop++; /* getattr of parent */ 9166 9167 /* 9168 * First, take care of finishing the 9169 * readdir results. 9170 */ 9171 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9172 /* 9173 * The d_ino of .. must be the inode number 9174 * of the mounted filesystem. 9175 */ 9176 if (garp->n4g_va.va_mask & AT_NODEID) 9177 rd_res->dotdotp->d_ino = 9178 garp->n4g_va.va_nodeid; 9179 9180 9181 /* 9182 * Next, create the ".." dnlc entry 9183 */ 9184 sfhp = sfh4_get(fhp, mi); 9185 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9186 dnlc_update(vp, "..", pvp); 9187 VN_RELE(pvp); 9188 } 9189 sfh4_rele(&sfhp); 9190 } 9191 } 9192 9193 if (mi->mi_io_kstats) { 9194 mutex_enter(&mi->mi_lock); 9195 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9196 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9197 mutex_exit(&mi->mi_lock); 9198 } 9199 9200 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9201 9202 out: 9203 /* 9204 * If readdir a node that is a stub for a crossed mount point, 9205 * keep the original secinfo flavor for the current file system, 9206 * not the crossed one. 9207 */ 9208 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9209 9210 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9211 } 9212 9213 9214 static int 9215 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9216 { 9217 rnode4_t *rp = VTOR4(bp->b_vp); 9218 int count; 9219 int error; 9220 cred_t *cred_otw = NULL; 9221 offset_t offset; 9222 nfs4_open_stream_t *osp = NULL; 9223 bool_t first_time = TRUE; /* first time getting otw cred */ 9224 bool_t last_time = FALSE; /* last time getting otw cred */ 9225 9226 ASSERT(curproc->p_zone == VTOMI4(bp->b_vp)->mi_zone); 9227 9228 DTRACE_IO1(start, struct buf *, bp); 9229 offset = ldbtob(bp->b_lblkno); 9230 9231 if (bp->b_flags & B_READ) { 9232 read_again: 9233 /* 9234 * Releases the osp, if it is provided. 9235 * Puts a hold on the cred_otw and the new osp (if found). 9236 */ 9237 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9238 &first_time, &last_time); 9239 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9240 offset, bp->b_bcount, 9241 &bp->b_resid, cred_otw, 9242 readahead, NULL); 9243 crfree(cred_otw); 9244 if (!error) { 9245 if (bp->b_resid) { 9246 /* 9247 * Didn't get it all because we hit EOF, 9248 * zero all the memory beyond the EOF. 9249 */ 9250 /* bzero(rdaddr + */ 9251 bzero(bp->b_un.b_addr + 9252 bp->b_bcount - bp->b_resid, bp->b_resid); 9253 } 9254 mutex_enter(&rp->r_statelock); 9255 if (bp->b_resid == bp->b_bcount && 9256 offset >= rp->r_size) { 9257 /* 9258 * We didn't read anything at all as we are 9259 * past EOF. Return an error indicator back 9260 * but don't destroy the pages (yet). 9261 */ 9262 error = NFS_EOF; 9263 } 9264 mutex_exit(&rp->r_statelock); 9265 } else if (error == EACCES && last_time == FALSE) { 9266 goto read_again; 9267 } 9268 } else { 9269 if (!(rp->r_flags & R4STALE)) { 9270 write_again: 9271 /* 9272 * Releases the osp, if it is provided. 9273 * Puts a hold on the cred_otw and the new 9274 * osp (if found). 9275 */ 9276 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9277 &first_time, &last_time); 9278 mutex_enter(&rp->r_statelock); 9279 count = MIN(bp->b_bcount, rp->r_size - offset); 9280 mutex_exit(&rp->r_statelock); 9281 if (count < 0) 9282 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9283 #ifdef DEBUG 9284 if (count == 0) { 9285 zoneid_t zoneid = getzoneid(); 9286 9287 zcmn_err(zoneid, CE_WARN, 9288 "nfs4_bio: zero length write at %lld", 9289 offset); 9290 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9291 "b_bcount=%ld, file size=%lld", 9292 rp->r_flags, (long)bp->b_bcount, 9293 rp->r_size); 9294 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9295 if (nfs4_bio_do_stop) 9296 debug_enter("nfs4_bio"); 9297 } 9298 #endif 9299 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9300 count, cred_otw, stab_comm); 9301 if (error == EACCES && last_time == FALSE) { 9302 crfree(cred_otw); 9303 goto write_again; 9304 } 9305 bp->b_error = error; 9306 if (error && error != EINTR && 9307 !(bp->b_vp->v_vfsp->vfs_flag && VFS_UNMOUNTED)) { 9308 /* 9309 * Don't print EDQUOT errors on the console. 9310 * Don't print asynchronous EACCES errors. 9311 * Don't print EFBIG errors. 9312 * Print all other write errors. 9313 */ 9314 if (error != EDQUOT && error != EFBIG && 9315 (error != EACCES || 9316 !(bp->b_flags & B_ASYNC))) 9317 nfs4_write_error(bp->b_vp, 9318 error, cred_otw); 9319 /* 9320 * Update r_error and r_flags as appropriate. 9321 * If the error was ESTALE, then mark the 9322 * rnode as not being writeable and save 9323 * the error status. Otherwise, save any 9324 * errors which occur from asynchronous 9325 * page invalidations. Any errors occurring 9326 * from other operations should be saved 9327 * by the caller. 9328 */ 9329 mutex_enter(&rp->r_statelock); 9330 if (error == ESTALE) { 9331 rp->r_flags |= R4STALE; 9332 if (!rp->r_error) 9333 rp->r_error = error; 9334 } else if (!rp->r_error && 9335 (bp->b_flags & 9336 (B_INVAL|B_FORCE|B_ASYNC)) == 9337 (B_INVAL|B_FORCE|B_ASYNC)) { 9338 rp->r_error = error; 9339 } 9340 mutex_exit(&rp->r_statelock); 9341 } 9342 crfree(cred_otw); 9343 } else 9344 error = rp->r_error; 9345 } 9346 9347 if (error != 0 && error != NFS_EOF) 9348 bp->b_flags |= B_ERROR; 9349 9350 if (osp) 9351 open_stream_rele(osp, rp); 9352 9353 DTRACE_IO1(done, struct buf *, bp); 9354 9355 return (error); 9356 } 9357 9358 /* ARGSUSED */ 9359 static int 9360 nfs4_fid(vnode_t *vp, fid_t *fidp) 9361 { 9362 return (EREMOTE); 9363 } 9364 9365 /* ARGSUSED2 */ 9366 static int 9367 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9368 { 9369 rnode4_t *rp = VTOR4(vp); 9370 9371 if (!write_lock) { 9372 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9373 return (V_WRITELOCK_FALSE); 9374 } 9375 9376 if ((rp->r_flags & R4DIRECTIO) || 9377 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9378 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9379 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9380 return (V_WRITELOCK_FALSE); 9381 nfs_rw_exit(&rp->r_rwlock); 9382 } 9383 9384 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9385 return (V_WRITELOCK_TRUE); 9386 } 9387 9388 /* ARGSUSED */ 9389 static void 9390 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9391 { 9392 rnode4_t *rp = VTOR4(vp); 9393 9394 nfs_rw_exit(&rp->r_rwlock); 9395 } 9396 9397 /* ARGSUSED */ 9398 static int 9399 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 9400 { 9401 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 9402 return (EIO); 9403 9404 /* 9405 * Because we stuff the readdir cookie into the offset field 9406 * someone may attempt to do an lseek with the cookie which 9407 * we want to succeed. 9408 */ 9409 if (vp->v_type == VDIR) 9410 return (0); 9411 if (*noffp < 0) 9412 return (EINVAL); 9413 return (0); 9414 } 9415 9416 9417 /* 9418 * Return all the pages from [off..off+len) in file 9419 */ 9420 static int 9421 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9422 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9423 enum seg_rw rw, cred_t *cr) 9424 { 9425 rnode4_t *rp; 9426 int error; 9427 mntinfo4_t *mi; 9428 9429 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 9430 return (EIO); 9431 rp = VTOR4(vp); 9432 if (IS_SHADOW(vp, rp)) 9433 vp = RTOV4(rp); 9434 9435 if (vp->v_flag & VNOMAP) 9436 return (ENOSYS); 9437 9438 if (protp != NULL) 9439 *protp = PROT_ALL; 9440 9441 /* 9442 * Now validate that the caches are up to date. 9443 */ 9444 if (error = nfs4_validate_caches(vp, cr)) 9445 return (error); 9446 9447 mi = VTOMI4(vp); 9448 retry: 9449 mutex_enter(&rp->r_statelock); 9450 9451 /* 9452 * Don't create dirty pages faster than they 9453 * can be cleaned so that the system doesn't 9454 * get imbalanced. If the async queue is 9455 * maxed out, then wait for it to drain before 9456 * creating more dirty pages. Also, wait for 9457 * any threads doing pagewalks in the vop_getattr 9458 * entry points so that they don't block for 9459 * long periods. 9460 */ 9461 if (rw == S_CREATE) { 9462 while ((mi->mi_max_threads != 0 && 9463 rp->r_awcount > 2 * mi->mi_max_threads) || 9464 rp->r_gcount > 0) 9465 cv_wait(&rp->r_cv, &rp->r_statelock); 9466 } 9467 9468 /* 9469 * If we are getting called as a side effect of an nfs_write() 9470 * operation the local file size might not be extended yet. 9471 * In this case we want to be able to return pages of zeroes. 9472 */ 9473 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9474 NFS4_DEBUG(nfs4_pageio_debug, 9475 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9476 "len=%llu, size=%llu, attrsize =%llu", off, 9477 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9478 mutex_exit(&rp->r_statelock); 9479 return (EFAULT); /* beyond EOF */ 9480 } 9481 9482 mutex_exit(&rp->r_statelock); 9483 9484 if (len <= PAGESIZE) { 9485 error = nfs4_getapage(vp, off, len, protp, pl, plsz, 9486 seg, addr, rw, cr); 9487 NFS4_DEBUG(nfs4_pageio_debug && error, 9488 (CE_NOTE, "getpage error %d; off=%lld, " 9489 "len=%lld", error, off, (u_longlong_t)len)); 9490 } else { 9491 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9492 pl, plsz, seg, addr, rw, cr); 9493 NFS4_DEBUG(nfs4_pageio_debug && error, 9494 (CE_NOTE, "getpages error %d; off=%lld, " 9495 "len=%lld", error, off, (u_longlong_t)len)); 9496 } 9497 9498 switch (error) { 9499 case NFS_EOF: 9500 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9501 goto retry; 9502 case ESTALE: 9503 nfs4_purge_stale_fh(error, vp, cr); 9504 } 9505 9506 return (error); 9507 } 9508 9509 /* 9510 * Called from pvn_getpages or nfs4_getpage to get a particular page. 9511 */ 9512 /* ARGSUSED */ 9513 static int 9514 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9515 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9516 enum seg_rw rw, cred_t *cr) 9517 { 9518 rnode4_t *rp; 9519 uint_t bsize; 9520 struct buf *bp; 9521 page_t *pp; 9522 u_offset_t lbn; 9523 u_offset_t io_off; 9524 u_offset_t blkoff; 9525 u_offset_t rablkoff; 9526 size_t io_len; 9527 uint_t blksize; 9528 int error; 9529 int readahead; 9530 int readahead_issued = 0; 9531 int ra_window; /* readahead window */ 9532 page_t *pagefound; 9533 page_t *savepp; 9534 9535 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 9536 return (EIO); 9537 9538 rp = VTOR4(vp); 9539 ASSERT(!IS_SHADOW(vp, rp)); 9540 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9541 9542 reread: 9543 bp = NULL; 9544 pp = NULL; 9545 pagefound = NULL; 9546 9547 if (pl != NULL) 9548 pl[0] = NULL; 9549 9550 error = 0; 9551 lbn = off / bsize; 9552 blkoff = lbn * bsize; 9553 9554 /* 9555 * Queueing up the readahead before doing the synchronous read 9556 * results in a significant increase in read throughput because 9557 * of the increased parallelism between the async threads and 9558 * the process context. 9559 */ 9560 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9561 rw != S_CREATE && 9562 !(vp->v_flag & VNOCACHE)) { 9563 mutex_enter(&rp->r_statelock); 9564 9565 /* 9566 * Calculate the number of readaheads to do. 9567 * a) No readaheads at offset = 0. 9568 * b) Do maximum(nfs4_nra) readaheads when the readahead 9569 * window is closed. 9570 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9571 * upon how far the readahead window is open or close. 9572 * d) No readaheads if rp->r_nextr is not within the scope 9573 * of the readahead window (random i/o). 9574 */ 9575 9576 if (off == 0) 9577 readahead = 0; 9578 else if (blkoff == rp->r_nextr) 9579 readahead = nfs4_nra; 9580 else if (rp->r_nextr > blkoff && 9581 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9582 <= (nfs4_nra - 1))) 9583 readahead = nfs4_nra - ra_window; 9584 else 9585 readahead = 0; 9586 9587 rablkoff = rp->r_nextr; 9588 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9589 mutex_exit(&rp->r_statelock); 9590 if (nfs4_async_readahead(vp, rablkoff + bsize, 9591 addr + (rablkoff + bsize - off), 9592 seg, cr, nfs4_readahead) < 0) { 9593 mutex_enter(&rp->r_statelock); 9594 break; 9595 } 9596 readahead--; 9597 rablkoff += bsize; 9598 /* 9599 * Indicate that we did a readahead so 9600 * readahead offset is not updated 9601 * by the synchronous read below. 9602 */ 9603 readahead_issued = 1; 9604 mutex_enter(&rp->r_statelock); 9605 /* 9606 * set readahead offset to 9607 * offset of last async readahead 9608 * request. 9609 */ 9610 rp->r_nextr = rablkoff; 9611 } 9612 mutex_exit(&rp->r_statelock); 9613 } 9614 9615 again: 9616 if ((pagefound = page_exists(vp, off)) == NULL) { 9617 if (pl == NULL) { 9618 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9619 nfs4_readahead); 9620 } else if (rw == S_CREATE) { 9621 /* 9622 * Block for this page is not allocated, or the offset 9623 * is beyond the current allocation size, or we're 9624 * allocating a swap slot and the page was not found, 9625 * so allocate it and return a zero page. 9626 */ 9627 if ((pp = page_create_va(vp, off, 9628 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9629 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9630 io_len = PAGESIZE; 9631 mutex_enter(&rp->r_statelock); 9632 rp->r_nextr = off + PAGESIZE; 9633 mutex_exit(&rp->r_statelock); 9634 } else { 9635 /* 9636 * Need to go to server to get a block 9637 */ 9638 mutex_enter(&rp->r_statelock); 9639 if (blkoff < rp->r_size && 9640 blkoff + bsize > rp->r_size) { 9641 /* 9642 * If less than a block left in 9643 * file read less than a block. 9644 */ 9645 if (rp->r_size <= off) { 9646 /* 9647 * Trying to access beyond EOF, 9648 * set up to get at least one page. 9649 */ 9650 blksize = off + PAGESIZE - blkoff; 9651 } else 9652 blksize = rp->r_size - blkoff; 9653 } else if ((off == 0) || 9654 (off != rp->r_nextr && !readahead_issued)) { 9655 blksize = PAGESIZE; 9656 blkoff = off; /* block = page here */ 9657 } else 9658 blksize = bsize; 9659 mutex_exit(&rp->r_statelock); 9660 9661 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9662 &io_len, blkoff, blksize, 0); 9663 9664 /* 9665 * Some other thread has entered the page, 9666 * so just use it. 9667 */ 9668 if (pp == NULL) 9669 goto again; 9670 9671 /* 9672 * Now round the request size up to page boundaries. 9673 * This ensures that the entire page will be 9674 * initialized to zeroes if EOF is encountered. 9675 */ 9676 io_len = ptob(btopr(io_len)); 9677 9678 bp = pageio_setup(pp, io_len, vp, B_READ); 9679 ASSERT(bp != NULL); 9680 9681 /* 9682 * pageio_setup should have set b_addr to 0. This 9683 * is correct since we want to do I/O on a page 9684 * boundary. bp_mapin will use this addr to calculate 9685 * an offset, and then set b_addr to the kernel virtual 9686 * address it allocated for us. 9687 */ 9688 ASSERT(bp->b_un.b_addr == 0); 9689 9690 bp->b_edev = 0; 9691 bp->b_dev = 0; 9692 bp->b_lblkno = lbtodb(io_off); 9693 bp->b_file = vp; 9694 bp->b_offset = (offset_t)off; 9695 bp_mapin(bp); 9696 9697 /* 9698 * If doing a write beyond what we believe is EOF, 9699 * don't bother trying to read the pages from the 9700 * server, we'll just zero the pages here. We 9701 * don't check that the rw flag is S_WRITE here 9702 * because some implementations may attempt a 9703 * read access to the buffer before copying data. 9704 */ 9705 mutex_enter(&rp->r_statelock); 9706 if (io_off >= rp->r_size && seg == segkmap) { 9707 mutex_exit(&rp->r_statelock); 9708 bzero(bp->b_un.b_addr, io_len); 9709 } else { 9710 mutex_exit(&rp->r_statelock); 9711 error = nfs4_bio(bp, NULL, cr, FALSE); 9712 } 9713 9714 /* 9715 * Unmap the buffer before freeing it. 9716 */ 9717 bp_mapout(bp); 9718 pageio_done(bp); 9719 9720 savepp = pp; 9721 do { 9722 pp->p_fsdata = C_NOCOMMIT; 9723 } while ((pp = pp->p_next) != savepp); 9724 9725 if (error == NFS_EOF) { 9726 /* 9727 * If doing a write system call just return 9728 * zeroed pages, else user tried to get pages 9729 * beyond EOF, return error. We don't check 9730 * that the rw flag is S_WRITE here because 9731 * some implementations may attempt a read 9732 * access to the buffer before copying data. 9733 */ 9734 if (seg == segkmap) 9735 error = 0; 9736 else 9737 error = EFAULT; 9738 } 9739 9740 if (!readahead_issued && !error) { 9741 mutex_enter(&rp->r_statelock); 9742 rp->r_nextr = io_off + io_len; 9743 mutex_exit(&rp->r_statelock); 9744 } 9745 } 9746 } 9747 9748 out: 9749 if (pl == NULL) 9750 return (error); 9751 9752 if (error) { 9753 if (pp != NULL) 9754 pvn_read_done(pp, B_ERROR); 9755 return (error); 9756 } 9757 9758 if (pagefound) { 9759 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 9760 9761 /* 9762 * Page exists in the cache, acquire the appropriate lock. 9763 * If this fails, start all over again. 9764 */ 9765 if ((pp = page_lookup(vp, off, se)) == NULL) { 9766 #ifdef DEBUG 9767 nfs4_lostpage++; 9768 #endif 9769 goto reread; 9770 } 9771 pl[0] = pp; 9772 pl[1] = NULL; 9773 return (0); 9774 } 9775 9776 if (pp != NULL) 9777 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 9778 9779 return (error); 9780 } 9781 9782 static void 9783 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 9784 cred_t *cr) 9785 { 9786 int error; 9787 page_t *pp; 9788 u_offset_t io_off; 9789 size_t io_len; 9790 struct buf *bp; 9791 uint_t bsize, blksize; 9792 rnode4_t *rp = VTOR4(vp); 9793 page_t *savepp; 9794 9795 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 9796 9797 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9798 9799 mutex_enter(&rp->r_statelock); 9800 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 9801 /* 9802 * If less than a block left in file read less 9803 * than a block. 9804 */ 9805 blksize = rp->r_size - blkoff; 9806 } else 9807 blksize = bsize; 9808 mutex_exit(&rp->r_statelock); 9809 9810 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 9811 &io_off, &io_len, blkoff, blksize, 1); 9812 /* 9813 * The isra flag passed to the kluster function is 1, we may have 9814 * gotten a return value of NULL for a variety of reasons (# of free 9815 * pages < minfree, someone entered the page on the vnode etc). In all 9816 * cases, we want to punt on the readahead. 9817 */ 9818 if (pp == NULL) 9819 return; 9820 9821 /* 9822 * Now round the request size up to page boundaries. 9823 * This ensures that the entire page will be 9824 * initialized to zeroes if EOF is encountered. 9825 */ 9826 io_len = ptob(btopr(io_len)); 9827 9828 bp = pageio_setup(pp, io_len, vp, B_READ); 9829 ASSERT(bp != NULL); 9830 9831 /* 9832 * pageio_setup should have set b_addr to 0. This is correct since 9833 * we want to do I/O on a page boundary. bp_mapin() will use this addr 9834 * to calculate an offset, and then set b_addr to the kernel virtual 9835 * address it allocated for us. 9836 */ 9837 ASSERT(bp->b_un.b_addr == 0); 9838 9839 bp->b_edev = 0; 9840 bp->b_dev = 0; 9841 bp->b_lblkno = lbtodb(io_off); 9842 bp->b_file = vp; 9843 bp->b_offset = (offset_t)blkoff; 9844 bp_mapin(bp); 9845 9846 /* 9847 * If doing a write beyond what we believe is EOF, don't bother trying 9848 * to read the pages from the server, we'll just zero the pages here. 9849 * We don't check that the rw flag is S_WRITE here because some 9850 * implementations may attempt a read access to the buffer before 9851 * copying data. 9852 */ 9853 mutex_enter(&rp->r_statelock); 9854 if (io_off >= rp->r_size && seg == segkmap) { 9855 mutex_exit(&rp->r_statelock); 9856 bzero(bp->b_un.b_addr, io_len); 9857 error = 0; 9858 } else { 9859 mutex_exit(&rp->r_statelock); 9860 error = nfs4_bio(bp, NULL, cr, TRUE); 9861 if (error == NFS_EOF) 9862 error = 0; 9863 } 9864 9865 /* 9866 * Unmap the buffer before freeing it. 9867 */ 9868 bp_mapout(bp); 9869 pageio_done(bp); 9870 9871 savepp = pp; 9872 do { 9873 pp->p_fsdata = C_NOCOMMIT; 9874 } while ((pp = pp->p_next) != savepp); 9875 9876 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 9877 9878 /* 9879 * In case of error set readahead offset 9880 * to the lowest offset. 9881 * pvn_read_done() calls VN_DISPOSE to destroy the pages 9882 */ 9883 if (error && rp->r_nextr > io_off) { 9884 mutex_enter(&rp->r_statelock); 9885 if (rp->r_nextr > io_off) 9886 rp->r_nextr = io_off; 9887 mutex_exit(&rp->r_statelock); 9888 } 9889 } 9890 9891 /* 9892 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 9893 * If len == 0, do from off to EOF. 9894 * 9895 * The normal cases should be len == 0 && off == 0 (entire vp list) or 9896 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 9897 * (from pageout). 9898 */ 9899 static int 9900 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 9901 { 9902 int error; 9903 rnode4_t *rp; 9904 9905 ASSERT(cr != NULL); 9906 9907 if (!(flags & B_ASYNC) && curproc->p_zone != VTOMI4(vp)->mi_zone) 9908 return (EIO); 9909 9910 rp = VTOR4(vp); 9911 if (IS_SHADOW(vp, rp)) 9912 vp = RTOV4(rp); 9913 9914 /* 9915 * XXX - Why should this check be made here? 9916 */ 9917 if (vp->v_flag & VNOMAP) 9918 return (ENOSYS); 9919 9920 if (len == 0 && !(flags & B_INVAL) && 9921 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 9922 return (0); 9923 9924 mutex_enter(&rp->r_statelock); 9925 rp->r_count++; 9926 mutex_exit(&rp->r_statelock); 9927 error = nfs4_putpages(vp, off, len, flags, cr); 9928 mutex_enter(&rp->r_statelock); 9929 rp->r_count--; 9930 cv_broadcast(&rp->r_cv); 9931 mutex_exit(&rp->r_statelock); 9932 9933 return (error); 9934 } 9935 9936 /* 9937 * Write out a single page, possibly klustering adjacent dirty pages. 9938 */ 9939 int 9940 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 9941 int flags, cred_t *cr) 9942 { 9943 u_offset_t io_off; 9944 u_offset_t lbn_off; 9945 u_offset_t lbn; 9946 size_t io_len; 9947 uint_t bsize; 9948 int error; 9949 rnode4_t *rp; 9950 9951 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 9952 ASSERT(pp != NULL); 9953 ASSERT(cr != NULL); 9954 ASSERT((flags & B_ASYNC) || curproc->p_zone == VTOMI4(vp)->mi_zone); 9955 9956 rp = VTOR4(vp); 9957 ASSERT(rp->r_count > 0); 9958 ASSERT(!IS_SHADOW(vp, rp)); 9959 9960 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9961 lbn = pp->p_offset / bsize; 9962 lbn_off = lbn * bsize; 9963 9964 /* 9965 * Find a kluster that fits in one block, or in 9966 * one page if pages are bigger than blocks. If 9967 * there is less file space allocated than a whole 9968 * page, we'll shorten the i/o request below. 9969 */ 9970 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 9971 roundup(bsize, PAGESIZE), flags); 9972 9973 /* 9974 * pvn_write_kluster shouldn't have returned a page with offset 9975 * behind the original page we were given. Verify that. 9976 */ 9977 ASSERT((pp->p_offset / bsize) >= lbn); 9978 9979 /* 9980 * Now pp will have the list of kept dirty pages marked for 9981 * write back. It will also handle invalidation and freeing 9982 * of pages that are not dirty. Check for page length rounding 9983 * problems. 9984 */ 9985 if (io_off + io_len > lbn_off + bsize) { 9986 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 9987 io_len = lbn_off + bsize - io_off; 9988 } 9989 /* 9990 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 9991 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 9992 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 9993 * progress and the r_size has not been made consistent with the 9994 * new size of the file. When the uiomove() completes the r_size is 9995 * updated and the R4MODINPROGRESS flag is cleared. 9996 * 9997 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 9998 * consistent value of r_size. Without this handshaking, it is 9999 * possible that nfs4_bio() picks up the old value of r_size 10000 * before the uiomove() in writerp4() completes. This will result 10001 * in the write through nfs4_bio() being dropped. 10002 * 10003 * More precisely, there is a window between the time the uiomove() 10004 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10005 * operation intervenes in this window, the page will be picked up, 10006 * because it is dirty (it will be unlocked, unless it was 10007 * pagecreate'd). When the page is picked up as dirty, the dirty 10008 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10009 * checked. This will still be the old size. Therefore the page will 10010 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10011 * the page will be found to be clean and the write will be dropped. 10012 */ 10013 if (rp->r_flags & R4MODINPROGRESS) { 10014 mutex_enter(&rp->r_statelock); 10015 if ((rp->r_flags & R4MODINPROGRESS) && 10016 rp->r_modaddr + MAXBSIZE > io_off && 10017 rp->r_modaddr < io_off + io_len) { 10018 page_t *plist; 10019 /* 10020 * A write is in progress for this region of the file. 10021 * If we did not detect R4MODINPROGRESS here then this 10022 * path through nfs_putapage() would eventually go to 10023 * nfs4_bio() and may not write out all of the data 10024 * in the pages. We end up losing data. So we decide 10025 * to set the modified bit on each page in the page 10026 * list and mark the rnode with R4DIRTY. This write 10027 * will be restarted at some later time. 10028 */ 10029 plist = pp; 10030 while (plist != NULL) { 10031 pp = plist; 10032 page_sub(&plist, pp); 10033 hat_setmod(pp); 10034 page_io_unlock(pp); 10035 page_unlock(pp); 10036 } 10037 rp->r_flags |= R4DIRTY; 10038 mutex_exit(&rp->r_statelock); 10039 if (offp) 10040 *offp = io_off; 10041 if (lenp) 10042 *lenp = io_len; 10043 return (0); 10044 } 10045 mutex_exit(&rp->r_statelock); 10046 } 10047 10048 if (flags & B_ASYNC) { 10049 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10050 nfs4_sync_putapage); 10051 } else 10052 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10053 10054 if (offp) 10055 *offp = io_off; 10056 if (lenp) 10057 *lenp = io_len; 10058 return (error); 10059 } 10060 10061 static int 10062 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10063 int flags, cred_t *cr) 10064 { 10065 int error; 10066 rnode4_t *rp; 10067 10068 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 10069 10070 flags |= B_WRITE; 10071 10072 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10073 10074 rp = VTOR4(vp); 10075 10076 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10077 error == EACCES) && 10078 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10079 if (!(rp->r_flags & R4OUTOFSPACE)) { 10080 mutex_enter(&rp->r_statelock); 10081 rp->r_flags |= R4OUTOFSPACE; 10082 mutex_exit(&rp->r_statelock); 10083 } 10084 flags |= B_ERROR; 10085 pvn_write_done(pp, flags); 10086 /* 10087 * If this was not an async thread, then try again to 10088 * write out the pages, but this time, also destroy 10089 * them whether or not the write is successful. This 10090 * will prevent memory from filling up with these 10091 * pages and destroying them is the only alternative 10092 * if they can't be written out. 10093 * 10094 * Don't do this if this is an async thread because 10095 * when the pages are unlocked in pvn_write_done, 10096 * some other thread could have come along, locked 10097 * them, and queued for an async thread. It would be 10098 * possible for all of the async threads to be tied 10099 * up waiting to lock the pages again and they would 10100 * all already be locked and waiting for an async 10101 * thread to handle them. Deadlock. 10102 */ 10103 if (!(flags & B_ASYNC)) { 10104 error = nfs4_putpage(vp, io_off, io_len, 10105 B_INVAL | B_FORCE, cr); 10106 } 10107 } else { 10108 if (error) 10109 flags |= B_ERROR; 10110 else if (rp->r_flags & R4OUTOFSPACE) { 10111 mutex_enter(&rp->r_statelock); 10112 rp->r_flags &= ~R4OUTOFSPACE; 10113 mutex_exit(&rp->r_statelock); 10114 } 10115 pvn_write_done(pp, flags); 10116 if (freemem < desfree) 10117 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10118 NFS4_WRITE_NOWAIT); 10119 } 10120 10121 return (error); 10122 } 10123 10124 #ifdef DEBUG 10125 int nfs4_force_open_before_mmap = 0; 10126 #endif 10127 10128 static int 10129 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10130 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 10131 { 10132 struct segvn_crargs vn_a; 10133 int error = 0; 10134 rnode4_t *rp = VTOR4(vp); 10135 mntinfo4_t *mi = VTOMI4(vp); 10136 10137 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 10138 return (EIO); 10139 10140 if (vp->v_flag & VNOMAP) 10141 return (ENOSYS); 10142 10143 if (off < 0 || (off + len) < 0) 10144 return (ENXIO); 10145 10146 if (vp->v_type != VREG) 10147 return (ENODEV); 10148 10149 /* 10150 * If the file is delegated to the client don't do anything. 10151 * If the file is not delegated, then validate the data cache. 10152 */ 10153 mutex_enter(&rp->r_statev4_lock); 10154 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10155 mutex_exit(&rp->r_statev4_lock); 10156 error = nfs4_validate_caches(vp, cr); 10157 if (error) 10158 return (error); 10159 } else { 10160 mutex_exit(&rp->r_statev4_lock); 10161 } 10162 10163 /* 10164 * Check to see if the vnode is currently marked as not cachable. 10165 * This means portions of the file are locked (through VOP_FRLOCK). 10166 * In this case the map request must be refused. We use 10167 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10168 */ 10169 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 10170 return (EINTR); 10171 10172 if (vp->v_flag & VNOCACHE) { 10173 error = EAGAIN; 10174 goto done; 10175 } 10176 10177 /* 10178 * Don't allow concurrent locks and mapping if mandatory locking is 10179 * enabled. 10180 */ 10181 if (flk_has_remote_locks(vp)) { 10182 struct vattr va; 10183 va.va_mask = AT_MODE; 10184 error = nfs4getattr(vp, &va, cr); 10185 if (error != 0) 10186 goto done; 10187 if (MANDLOCK(vp, va.va_mode)) { 10188 error = EAGAIN; 10189 goto done; 10190 } 10191 } 10192 10193 /* 10194 * It is possible that the rnode has a lost lock request that we 10195 * are still trying to recover, and that the request conflicts with 10196 * this map request. 10197 * 10198 * An alternative approach would be for nfs4_safemap() to consider 10199 * queued lock requests when deciding whether to set or clear 10200 * VNOCACHE. This would require the frlock code path to call 10201 * nfs4_safemap() after enqueing a lost request. 10202 */ 10203 if (nfs4_map_lost_lock_conflict(vp)) { 10204 error = EAGAIN; 10205 goto done; 10206 } 10207 10208 as_rangelock(as); 10209 if (!(flags & MAP_FIXED)) { 10210 map_addr(addrp, len, off, 1, flags); 10211 if (*addrp == NULL) { 10212 as_rangeunlock(as); 10213 error = ENOMEM; 10214 goto done; 10215 } 10216 } else { 10217 /* 10218 * User specified address - blow away any previous mappings 10219 */ 10220 (void) as_unmap(as, *addrp, len); 10221 } 10222 10223 if (vp->v_type == VREG) { 10224 /* 10225 * We need to retrieve the open stream 10226 */ 10227 nfs4_open_stream_t *osp = NULL; 10228 nfs4_open_owner_t *oop = NULL; 10229 10230 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10231 if (oop != NULL) { 10232 /* returns with 'os_sync_lock' held */ 10233 osp = find_open_stream(oop, rp); 10234 open_owner_rele(oop); 10235 } 10236 if (osp == NULL) { 10237 #ifdef DEBUG 10238 if (nfs4_force_open_before_mmap) { 10239 error = EIO; 10240 goto done; 10241 } 10242 #endif 10243 /* returns with 'os_sync_lock' held */ 10244 osp = open_and_get_osp(vp, cr, mi); 10245 if (osp == NULL) { 10246 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10247 "nfs4_map: we tried to OPEN the file " 10248 "but again no osp, so fail with EIO")); 10249 error = EIO; 10250 goto done; 10251 } 10252 } 10253 10254 if (osp->os_failed_reopen) { 10255 mutex_exit(&osp->os_sync_lock); 10256 open_stream_rele(osp, rp); 10257 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10258 "nfs4_map: os_failed_reopen set on " 10259 "osp %p, cr %p, rp %s", (void *)osp, 10260 (void *)cr, rnode4info(rp))); 10261 error = EIO; 10262 goto done; 10263 } 10264 mutex_exit(&osp->os_sync_lock); 10265 open_stream_rele(osp, rp); 10266 } 10267 10268 vn_a.vp = vp; 10269 vn_a.offset = off; 10270 vn_a.type = (flags & MAP_TYPE); 10271 vn_a.prot = (uchar_t)prot; 10272 vn_a.maxprot = (uchar_t)maxprot; 10273 vn_a.flags = (flags & ~MAP_TYPE); 10274 vn_a.cred = cr; 10275 vn_a.amp = NULL; 10276 vn_a.szc = 0; 10277 vn_a.lgrp_mem_policy_flags = 0; 10278 10279 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10280 as_rangeunlock(as); 10281 10282 done: 10283 nfs_rw_exit(&rp->r_lkserlock); 10284 return (error); 10285 } 10286 10287 /* 10288 * We're most likely dealing with a kernel module that likes to READ 10289 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10290 * officially OPEN the file to create the necessary client state 10291 * for bookkeeping of os_mmap_read/write counts. 10292 * 10293 * Since VOP_MAP only passes in a pointer to the vnode rather than 10294 * a double pointer, we can't handle the case where nfs4open_otw() 10295 * returns a different vnode than the one passed into VOP_MAP (since 10296 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10297 * we return NULL and let nfs4_map() fail. Note: the only case where 10298 * this should happen is if the file got removed and replaced with the 10299 * same name on the server (in addition to the fact that we're trying 10300 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10301 */ 10302 static nfs4_open_stream_t * 10303 open_and_get_osp(vnode_t *map_vp, cred_t *cr, mntinfo4_t *mi) 10304 { 10305 rnode4_t *rp, *drp; 10306 vnode_t *dvp, *open_vp; 10307 char *file_name; 10308 int just_created; 10309 nfs4_sharedfh_t *sfh; 10310 nfs4_open_stream_t *osp; 10311 nfs4_open_owner_t *oop; 10312 10313 open_vp = map_vp; 10314 sfh = (open_vp->v_flag & VROOT) ? mi->mi_srvparentfh : 10315 VTOSV(open_vp)->sv_dfh; 10316 drp = r4find_unlocked(sfh, open_vp->v_vfsp); 10317 if (!drp) 10318 return (NULL); 10319 10320 file_name = fn_name(VTOSV(open_vp)->sv_name); 10321 10322 rp = VTOR4(open_vp); 10323 dvp = RTOV4(drp); 10324 mutex_enter(&rp->r_statev4_lock); 10325 if (rp->created_v4) { 10326 rp->created_v4 = 0; 10327 mutex_exit(&rp->r_statev4_lock); 10328 10329 dnlc_update(dvp, file_name, open_vp); 10330 /* This is needed so we don't bump the open ref count */ 10331 just_created = 1; 10332 } else { 10333 mutex_exit(&rp->r_statev4_lock); 10334 just_created = 0; 10335 } 10336 10337 VN_HOLD(map_vp); 10338 10339 if (nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10340 just_created)) { 10341 kmem_free(file_name, MAXNAMELEN); 10342 VN_RELE(dvp); 10343 VN_RELE(map_vp); 10344 return (NULL); 10345 } 10346 10347 kmem_free(file_name, MAXNAMELEN); 10348 VN_RELE(dvp); 10349 10350 /* 10351 * If nfs4open_otw() returned a different vnode then "undo" 10352 * the open and return failure to the caller. 10353 */ 10354 if (!VN_CMP(open_vp, map_vp)) { 10355 nfs4_error_t e; 10356 10357 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10358 "open returned a different vnode")); 10359 /* 10360 * If there's an error, ignore it, 10361 * and let VOP_INACTIVE handle it. 10362 */ 10363 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10364 CLOSE_NORM, 0, 0, 0); 10365 VN_RELE(map_vp); 10366 return (NULL); 10367 } 10368 10369 VN_RELE(map_vp); 10370 10371 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10372 if (!oop) { 10373 nfs4_error_t e; 10374 10375 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10376 "no open owner")); 10377 /* 10378 * If there's an error, ignore it, 10379 * and let VOP_INACTIVE handle it. 10380 */ 10381 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10382 CLOSE_NORM, 0, 0, 0); 10383 return (NULL); 10384 } 10385 osp = find_open_stream(oop, rp); 10386 open_owner_rele(oop); 10387 return (osp); 10388 } 10389 10390 /* 10391 * Please be aware that when this function is called, the address space write 10392 * a_lock is held. Do not put over the wire calls in this function. 10393 */ 10394 /* ARGSUSED */ 10395 static int 10396 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10397 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 10398 { 10399 rnode4_t *rp; 10400 int error = 0; 10401 mntinfo4_t *mi; 10402 10403 mi = VTOMI4(vp); 10404 rp = VTOR4(vp); 10405 10406 if (curproc->p_zone != mi->mi_zone) 10407 return (EIO); 10408 if (vp->v_flag & VNOMAP) 10409 return (ENOSYS); 10410 10411 /* 10412 * Need to hold rwlock while incrementing the mapcnt so that 10413 * mmap'ing can be serialized with writes so that the caching 10414 * can be handled correctly. 10415 * 10416 * Don't need to update the open stream first, since this 10417 * mmap can't add any additional share access that isn't 10418 * already contained in the open stream (for the case where we 10419 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10420 * take into account os_mmap_read[write] counts). 10421 */ 10422 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp))) 10423 return (EINTR); 10424 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10425 nfs_rw_exit(&rp->r_rwlock); 10426 10427 if (vp->v_type == VREG) { 10428 /* 10429 * We need to retrieve the open stream and update the counts. 10430 * If there is no open stream here, something is wrong. 10431 */ 10432 nfs4_open_stream_t *osp = NULL; 10433 nfs4_open_owner_t *oop = NULL; 10434 10435 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10436 if (oop != NULL) { 10437 /* returns with 'os_sync_lock' held */ 10438 osp = find_open_stream(oop, rp); 10439 open_owner_rele(oop); 10440 } 10441 if (osp == NULL) { 10442 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10443 "nfs4_addmap: we should have an osp" 10444 "but we don't, so fail with EIO")); 10445 error = EIO; 10446 goto out; 10447 } 10448 10449 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10450 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10451 10452 /* 10453 * Update the map count in the open stream. 10454 * This is necessary in the case where we 10455 * open/mmap/close/, then the server reboots, and we 10456 * attempt to reopen. If the mmap doesn't add share 10457 * access then we send an invalid reopen with 10458 * access = NONE. 10459 * 10460 * We need to specifically check each PROT_* so a mmap 10461 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10462 * read and write access. A simple comparison of prot 10463 * to ~PROT_WRITE to determine read access is insufficient 10464 * since prot can be |= with PROT_USER, etc. 10465 */ 10466 10467 /* 10468 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10469 */ 10470 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10471 osp->os_mmap_write += btopr(len); 10472 if (maxprot & PROT_READ) 10473 osp->os_mmap_read += btopr(len); 10474 if (maxprot & PROT_EXEC) 10475 osp->os_mmap_read += btopr(len); 10476 /* 10477 * Ensure that os_mmap_read gets incremented, even if 10478 * maxprot were to look like PROT_NONE. 10479 */ 10480 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10481 !(maxprot & PROT_EXEC)) 10482 osp->os_mmap_read += btopr(len); 10483 osp->os_mapcnt += btopr(len); 10484 mutex_exit(&osp->os_sync_lock); 10485 open_stream_rele(osp, rp); 10486 } 10487 10488 out: 10489 /* 10490 * If we got an error, then undo our 10491 * incrementing of 'r_mapcnt'. 10492 */ 10493 10494 if (error) { 10495 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10496 ASSERT(rp->r_mapcnt >= 0); 10497 } 10498 return (error); 10499 } 10500 10501 static int 10502 nfs4_cmp(vnode_t *vp1, vnode_t *vp2) 10503 { 10504 10505 return (VTOR4(vp1) == VTOR4(vp2)); 10506 } 10507 10508 static int 10509 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10510 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr) 10511 { 10512 int rc; 10513 u_offset_t start, end; 10514 rnode4_t *rp; 10515 int error = 0, intr = INTR4(vp); 10516 nfs4_error_t e; 10517 10518 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 10519 return (EIO); 10520 10521 /* check for valid cmd parameter */ 10522 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10523 return (EINVAL); 10524 10525 /* Verify l_type. */ 10526 switch (bfp->l_type) { 10527 case F_RDLCK: 10528 if (cmd != F_GETLK && !(flag & FREAD)) 10529 return (EBADF); 10530 break; 10531 case F_WRLCK: 10532 if (cmd != F_GETLK && !(flag & FWRITE)) 10533 return (EBADF); 10534 break; 10535 case F_UNLCK: 10536 intr = 0; 10537 break; 10538 10539 default: 10540 return (EINVAL); 10541 } 10542 10543 /* check the validity of the lock range */ 10544 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10545 return (rc); 10546 if (rc = flk_check_lock_data(start, end, MAXEND)) 10547 return (rc); 10548 10549 /* 10550 * If the filesystem is mounted using local locking, pass the 10551 * request off to the local locking code. 10552 */ 10553 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10554 if (cmd == F_SETLK || cmd == F_SETLKW) { 10555 /* 10556 * For complete safety, we should be holding 10557 * r_lkserlock. However, we can't call 10558 * nfs4_safelock and then fs_frlock while 10559 * holding r_lkserlock, so just invoke 10560 * nfs4_safelock and expect that this will 10561 * catch enough of the cases. 10562 */ 10563 if (!nfs4_safelock(vp, bfp, cr)) 10564 return (EAGAIN); 10565 } 10566 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr)); 10567 } 10568 10569 rp = VTOR4(vp); 10570 10571 /* 10572 * Check whether the given lock request can proceed, given the 10573 * current file mappings. 10574 */ 10575 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10576 return (EINTR); 10577 if (cmd == F_SETLK || cmd == F_SETLKW) { 10578 if (!nfs4_safelock(vp, bfp, cr)) { 10579 rc = EAGAIN; 10580 goto done; 10581 } 10582 } 10583 10584 /* 10585 * Flush the cache after waiting for async I/O to finish. For new 10586 * locks, this is so that the process gets the latest bits from the 10587 * server. For unlocks, this is so that other clients see the 10588 * latest bits once the file has been unlocked. If currently dirty 10589 * pages can't be flushed, then don't allow a lock to be set. But 10590 * allow unlocks to succeed, to avoid having orphan locks on the 10591 * server. 10592 */ 10593 if (cmd != F_GETLK) { 10594 mutex_enter(&rp->r_statelock); 10595 while (rp->r_count > 0) { 10596 if (intr) { 10597 klwp_t *lwp = ttolwp(curthread); 10598 10599 if (lwp != NULL) 10600 lwp->lwp_nostop++; 10601 if (cv_wait_sig(&rp->r_cv, &rp->r_statelock) == 0) { 10602 if (lwp != NULL) 10603 lwp->lwp_nostop--; 10604 rc = EINTR; 10605 break; 10606 } 10607 if (lwp != NULL) 10608 lwp->lwp_nostop--; 10609 } else 10610 cv_wait(&rp->r_cv, &rp->r_statelock); 10611 } 10612 mutex_exit(&rp->r_statelock); 10613 if (rc != 0) 10614 goto done; 10615 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr); 10616 if (error) { 10617 if (error == ENOSPC || error == EDQUOT) { 10618 mutex_enter(&rp->r_statelock); 10619 if (!rp->r_error) 10620 rp->r_error = error; 10621 mutex_exit(&rp->r_statelock); 10622 } 10623 if (bfp->l_type != F_UNLCK) { 10624 rc = ENOLCK; 10625 goto done; 10626 } 10627 } 10628 } 10629 10630 /* 10631 * Call the lock manager to do the real work of contacting 10632 * the server and obtaining the lock. 10633 */ 10634 10635 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10636 cr, &e, NULL, NULL); 10637 rc = e.error; 10638 10639 if (rc == 0) 10640 nfs4_lockcompletion(vp, cmd); 10641 10642 done: 10643 nfs_rw_exit(&rp->r_lkserlock); 10644 10645 return (rc); 10646 } 10647 10648 /* 10649 * Free storage space associated with the specified vnode. The portion 10650 * to be freed is specified by bfp->l_start and bfp->l_len (already 10651 * normalized to a "whence" of 0). 10652 * 10653 * This is an experimental facility whose continued existence is not 10654 * guaranteed. Currently, we only support the special case 10655 * of l_len == 0, meaning free to end of file. 10656 */ 10657 /* ARGSUSED */ 10658 static int 10659 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10660 offset_t offset, cred_t *cr, caller_context_t *ct) 10661 { 10662 int error; 10663 10664 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 10665 return (EIO); 10666 ASSERT(vp->v_type == VREG); 10667 if (cmd != F_FREESP) 10668 return (EINVAL); 10669 10670 error = convoff(vp, bfp, 0, offset); 10671 if (!error) { 10672 ASSERT(bfp->l_start >= 0); 10673 if (bfp->l_len == 0) { 10674 struct vattr va; 10675 10676 va.va_mask = AT_SIZE; 10677 va.va_size = bfp->l_start; 10678 error = nfs4setattr(vp, &va, 0, cr, NULL); 10679 } else 10680 error = EINVAL; 10681 } 10682 10683 return (error); 10684 } 10685 10686 /* ARGSUSED */ 10687 static int 10688 nfs4_realvp(vnode_t *vp, vnode_t **vpp) 10689 { 10690 return (EINVAL); 10691 } 10692 10693 /* 10694 * Setup and add an address space callback to do the work of the delmap call. 10695 * The callback will (and must be) deleted in the actual callback function. 10696 * 10697 * This is done in order to take care of the problem that we have with holding 10698 * the address space's a_lock for a long period of time (e.g. if the NFS server 10699 * is down). Callbacks will be executed in the address space code while the 10700 * a_lock is not held. Holding the address space's a_lock causes things such 10701 * as ps and fork to hang because they are trying to acquire this lock as well. 10702 */ 10703 /* ARGSUSED */ 10704 static int 10705 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10706 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 10707 { 10708 int caller_found; 10709 int error; 10710 rnode4_t *rp; 10711 nfs4_delmap_args_t *dmapp; 10712 nfs4_delmapcall_t *delmap_call; 10713 10714 if (vp->v_flag & VNOMAP) 10715 return (ENOSYS); 10716 10717 /* 10718 * A process may not change zones if it has NFS pages mmap'ed 10719 * in, so we can't legitimately get here from the wrong zone. 10720 */ 10721 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 10722 10723 rp = VTOR4(vp); 10724 10725 /* 10726 * The way that the address space of this process deletes its mapping 10727 * of this file is via the following call chains: 10728 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10729 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10730 * 10731 * With the use of address space callbacks we are allowed to drop the 10732 * address space lock, a_lock, while executing the NFS operations that 10733 * need to go over the wire. Returning EAGAIN to the caller of this 10734 * function is what drives the execution of the callback that we add 10735 * below. The callback will be executed by the address space code 10736 * after dropping the a_lock. When the callback is finished, since 10737 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 10738 * is called again on the same segment to finish the rest of the work 10739 * that needs to happen during unmapping. 10740 * 10741 * This action of calling back into the segment driver causes 10742 * nfs4_delmap() to get called again, but since the callback was 10743 * already executed at this point, it already did the work and there 10744 * is nothing left for us to do. 10745 * 10746 * To Summarize: 10747 * - The first time nfs4_delmap is called by the current thread is when 10748 * we add the caller associated with this delmap to the delmap caller 10749 * list, add the callback, and return EAGAIN. 10750 * - The second time in this call chain when nfs4_delmap is called we 10751 * will find this caller in the delmap caller list and realize there 10752 * is no more work to do thus removing this caller from the list and 10753 * returning the error that was set in the callback execution. 10754 */ 10755 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 10756 if (caller_found) { 10757 /* 10758 * 'error' is from the actual delmap operations. To avoid 10759 * hangs, we need to handle the return of EAGAIN differently 10760 * since this is what drives the callback execution. 10761 * In this case, we don't want to return EAGAIN and do the 10762 * callback execution because there are none to execute. 10763 */ 10764 if (error == EAGAIN) 10765 return (0); 10766 else 10767 return (error); 10768 } 10769 10770 /* current caller was not in the list */ 10771 delmap_call = nfs4_init_delmapcall(); 10772 10773 mutex_enter(&rp->r_statelock); 10774 list_insert_tail(&rp->r_indelmap, delmap_call); 10775 mutex_exit(&rp->r_statelock); 10776 10777 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 10778 10779 dmapp->vp = vp; 10780 dmapp->off = off; 10781 dmapp->addr = addr; 10782 dmapp->len = len; 10783 dmapp->prot = prot; 10784 dmapp->maxprot = maxprot; 10785 dmapp->flags = flags; 10786 dmapp->cr = cr; 10787 dmapp->caller = delmap_call; 10788 10789 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 10790 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 10791 10792 return (error ? error : EAGAIN); 10793 } 10794 10795 static nfs4_delmapcall_t * 10796 nfs4_init_delmapcall() 10797 { 10798 nfs4_delmapcall_t *delmap_call; 10799 10800 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 10801 delmap_call->call_id = curthread; 10802 delmap_call->error = 0; 10803 10804 return (delmap_call); 10805 } 10806 10807 static void 10808 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 10809 { 10810 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 10811 } 10812 10813 /* 10814 * Searches for the current delmap caller (based on curthread) in the list of 10815 * callers. If it is found, we remove it and free the delmap caller. 10816 * Returns: 10817 * 0 if the caller wasn't found 10818 * 1 if the caller was found, removed and freed. *errp will be set 10819 * to what the result of the delmap was. 10820 */ 10821 static int 10822 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 10823 { 10824 nfs4_delmapcall_t *delmap_call; 10825 10826 /* 10827 * If the list doesn't exist yet, we create it and return 10828 * that the caller wasn't found. No list = no callers. 10829 */ 10830 mutex_enter(&rp->r_statelock); 10831 if (!(rp->r_flags & R4DELMAPLIST)) { 10832 /* The list does not exist */ 10833 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 10834 offsetof(nfs4_delmapcall_t, call_node)); 10835 rp->r_flags |= R4DELMAPLIST; 10836 mutex_exit(&rp->r_statelock); 10837 return (0); 10838 } else { 10839 /* The list exists so search it */ 10840 for (delmap_call = list_head(&rp->r_indelmap); 10841 delmap_call != NULL; 10842 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 10843 if (delmap_call->call_id == curthread) { 10844 /* current caller is in the list */ 10845 *errp = delmap_call->error; 10846 list_remove(&rp->r_indelmap, delmap_call); 10847 mutex_exit(&rp->r_statelock); 10848 nfs4_free_delmapcall(delmap_call); 10849 return (1); 10850 } 10851 } 10852 } 10853 mutex_exit(&rp->r_statelock); 10854 return (0); 10855 } 10856 10857 /* 10858 * Remove some pages from an mmap'd vnode. Just update the 10859 * count of pages. If doing close-to-open, then flush and 10860 * commit all of the pages associated with this file. 10861 * Otherwise, start an asynchronous page flush to write out 10862 * any dirty pages. This will also associate a credential 10863 * with the rnode which can be used to write the pages. 10864 */ 10865 /* ARGSUSED */ 10866 static void 10867 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 10868 { 10869 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 10870 rnode4_t *rp; 10871 mntinfo4_t *mi; 10872 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 10873 10874 rp = VTOR4(dmapp->vp); 10875 mi = VTOMI4(dmapp->vp); 10876 10877 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 10878 ASSERT(rp->r_mapcnt >= 0); 10879 10880 /* 10881 * Initiate a page flush and potential commit if there are 10882 * pages, the file system was not mounted readonly, the segment 10883 * was mapped shared, and the pages themselves were writeable. 10884 */ 10885 if (nfs4_has_pages(dmapp->vp) && 10886 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 10887 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 10888 mutex_enter(&rp->r_statelock); 10889 rp->r_flags |= R4DIRTY; 10890 mutex_exit(&rp->r_statelock); 10891 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 10892 dmapp->len, dmapp->cr); 10893 if (!e.error) { 10894 mutex_enter(&rp->r_statelock); 10895 e.error = rp->r_error; 10896 rp->r_error = 0; 10897 mutex_exit(&rp->r_statelock); 10898 } 10899 } else 10900 e.error = 0; 10901 10902 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 10903 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 10904 B_INVAL, dmapp->cr); 10905 10906 if (e.error) { 10907 e.stat = puterrno4(e.error); 10908 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 10909 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 10910 dmapp->caller->error = e.error; 10911 } 10912 10913 /* Check to see if we need to close the file */ 10914 10915 if (dmapp->vp->v_type == VREG) { 10916 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 10917 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 10918 10919 if (e.error != 0 || e.stat != NFS4_OK) { 10920 /* 10921 * Since it is possible that e.error == 0 and 10922 * e.stat != NFS4_OK (and vice versa), 10923 * we do the proper checking in order to get both 10924 * e.error and e.stat reporting the correct info. 10925 */ 10926 if (e.stat == NFS4_OK) 10927 e.stat = puterrno4(e.error); 10928 if (e.error == 0) 10929 e.error = geterrno4(e.stat); 10930 10931 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 10932 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 10933 dmapp->caller->error = e.error; 10934 } 10935 } 10936 10937 (void) as_delete_callback(as, arg); 10938 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 10939 } 10940 10941 10942 static uint_t 10943 fattr4_maxfilesize_to_bits(uint64_t ll) 10944 { 10945 uint_t l = 1; 10946 10947 if (ll == 0) { 10948 return (0); 10949 } 10950 10951 if (ll & 0xffffffff00000000) { 10952 l += 32; ll >>= 32; 10953 } 10954 if (ll & 0xffff0000) { 10955 l += 16; ll >>= 16; 10956 } 10957 if (ll & 0xff00) { 10958 l += 8; ll >>= 8; 10959 } 10960 if (ll & 0xf0) { 10961 l += 4; ll >>= 4; 10962 } 10963 if (ll & 0xc) { 10964 l += 2; ll >>= 2; 10965 } 10966 if (ll & 0x2) { 10967 l += 1; 10968 } 10969 return (l); 10970 } 10971 10972 static int 10973 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 10974 { 10975 int error; 10976 hrtime_t t; 10977 rnode4_t *rp; 10978 nfs4_ga_res_t gar; 10979 nfs4_ga_ext_res_t ger; 10980 10981 gar.n4g_ext_res = &ger; 10982 10983 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 10984 return (EIO); 10985 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 10986 *valp = MAXPATHLEN; 10987 return (0); 10988 } 10989 if (cmd == _PC_ACL_ENABLED) { 10990 *valp = _ACL_ACE_ENABLED; 10991 return (0); 10992 } 10993 10994 rp = VTOR4(vp); 10995 if (cmd == _PC_XATTR_EXISTS) { 10996 /* 10997 * Eventually should attempt small client readdir before 10998 * going otw with GETATTR(FATTR4_NAMED_ATTR). For now 10999 * just drive the OTW getattr. This is required because 11000 * _PC_XATTR_EXISTS can only return true if attributes 11001 * exist -- simply checking for existance of the attrdir 11002 * is not sufficient. 11003 * 11004 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11005 * is NULL. Once the xadir vp exists, we can create xattrs, 11006 * and we don't have any way to update the "base" object's 11007 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11008 * could help out. 11009 */ 11010 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11011 rp->r_xattr_dir == NULL) { 11012 *valp = rp->r_pathconf.pc4_xattr_exists; 11013 return (0); 11014 } 11015 } else { /* OLD CODE */ 11016 if (ATTRCACHE4_VALID(vp)) { 11017 mutex_enter(&rp->r_statelock); 11018 if (rp->r_pathconf.pc4_cache_valid) { 11019 error = 0; 11020 switch (cmd) { 11021 case _PC_FILESIZEBITS: 11022 *valp = 11023 rp->r_pathconf.pc4_filesizebits; 11024 break; 11025 case _PC_LINK_MAX: 11026 *valp = 11027 rp->r_pathconf.pc4_link_max; 11028 break; 11029 case _PC_NAME_MAX: 11030 *valp = 11031 rp->r_pathconf.pc4_name_max; 11032 break; 11033 case _PC_CHOWN_RESTRICTED: 11034 *valp = 11035 rp->r_pathconf.pc4_chown_restricted; 11036 break; 11037 case _PC_NO_TRUNC: 11038 *valp = 11039 rp->r_pathconf.pc4_no_trunc; 11040 break; 11041 default: 11042 error = EINVAL; 11043 break; 11044 } 11045 mutex_exit(&rp->r_statelock); 11046 #ifdef DEBUG 11047 nfs4_pathconf_cache_hits++; 11048 #endif 11049 return (error); 11050 } 11051 mutex_exit(&rp->r_statelock); 11052 } 11053 } 11054 #ifdef DEBUG 11055 nfs4_pathconf_cache_misses++; 11056 #endif 11057 11058 t = gethrtime(); 11059 11060 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11061 11062 if (error) { 11063 mutex_enter(&rp->r_statelock); 11064 rp->r_pathconf.pc4_cache_valid = FALSE; 11065 rp->r_pathconf.pc4_xattr_valid = FALSE; 11066 mutex_exit(&rp->r_statelock); 11067 return (error); 11068 } 11069 11070 /* interpret the max filesize */ 11071 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11072 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11073 11074 /* Store the attributes we just received */ 11075 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11076 11077 switch (cmd) { 11078 case _PC_FILESIZEBITS: 11079 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11080 break; 11081 case _PC_LINK_MAX: 11082 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11083 break; 11084 case _PC_NAME_MAX: 11085 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11086 break; 11087 case _PC_CHOWN_RESTRICTED: 11088 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11089 break; 11090 case _PC_NO_TRUNC: 11091 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11092 break; 11093 case _PC_XATTR_EXISTS: 11094 *valp = gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists; 11095 break; 11096 default: 11097 return (EINVAL); 11098 } 11099 11100 return (0); 11101 } 11102 11103 /* 11104 * Called by async thread to do synchronous pageio. Do the i/o, wait 11105 * for it to complete, and cleanup the page list when done. 11106 */ 11107 static int 11108 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11109 int flags, cred_t *cr) 11110 { 11111 int error; 11112 11113 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11114 11115 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11116 if (flags & B_READ) 11117 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11118 else 11119 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11120 return (error); 11121 } 11122 11123 static int 11124 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11125 int flags, cred_t *cr) 11126 { 11127 int error; 11128 rnode4_t *rp; 11129 11130 if (!(flags & B_ASYNC) && curproc->p_zone != VTOMI4(vp)->mi_zone) 11131 return (EIO); 11132 11133 if (pp == NULL) 11134 return (EINVAL); 11135 11136 rp = VTOR4(vp); 11137 mutex_enter(&rp->r_statelock); 11138 rp->r_count++; 11139 mutex_exit(&rp->r_statelock); 11140 11141 if (flags & B_ASYNC) { 11142 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11143 nfs4_sync_pageio); 11144 } else 11145 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11146 mutex_enter(&rp->r_statelock); 11147 rp->r_count--; 11148 cv_broadcast(&rp->r_cv); 11149 mutex_exit(&rp->r_statelock); 11150 return (error); 11151 } 11152 11153 static void 11154 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr) 11155 { 11156 int error; 11157 rnode4_t *rp; 11158 page_t *plist; 11159 page_t *pptr; 11160 offset3 offset; 11161 count3 len; 11162 k_sigset_t smask; 11163 11164 /* 11165 * We should get called with fl equal to either B_FREE or 11166 * B_INVAL. Any other value is illegal. 11167 * 11168 * The page that we are either supposed to free or destroy 11169 * should be exclusive locked and its io lock should not 11170 * be held. 11171 */ 11172 ASSERT(fl == B_FREE || fl == B_INVAL); 11173 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11174 11175 rp = VTOR4(vp); 11176 11177 /* 11178 * If the page doesn't need to be committed or we shouldn't 11179 * even bother attempting to commit it, then just make sure 11180 * that the p_fsdata byte is clear and then either free or 11181 * destroy the page as appropriate. 11182 */ 11183 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11184 pp->p_fsdata = C_NOCOMMIT; 11185 if (fl == B_FREE) 11186 page_free(pp, dn); 11187 else 11188 page_destroy(pp, dn); 11189 return; 11190 } 11191 11192 /* 11193 * If there is a page invalidation operation going on, then 11194 * if this is one of the pages being destroyed, then just 11195 * clear the p_fsdata byte and then either free or destroy 11196 * the page as appropriate. 11197 */ 11198 mutex_enter(&rp->r_statelock); 11199 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11200 mutex_exit(&rp->r_statelock); 11201 pp->p_fsdata = C_NOCOMMIT; 11202 if (fl == B_FREE) 11203 page_free(pp, dn); 11204 else 11205 page_destroy(pp, dn); 11206 return; 11207 } 11208 11209 /* 11210 * If we are freeing this page and someone else is already 11211 * waiting to do a commit, then just unlock the page and 11212 * return. That other thread will take care of commiting 11213 * this page. The page can be freed sometime after the 11214 * commit has finished. Otherwise, if the page is marked 11215 * as delay commit, then we may be getting called from 11216 * pvn_write_done, one page at a time. This could result 11217 * in one commit per page, so we end up doing lots of small 11218 * commits instead of fewer larger commits. This is bad, 11219 * we want do as few commits as possible. 11220 */ 11221 if (fl == B_FREE) { 11222 if (rp->r_flags & R4COMMITWAIT) { 11223 page_unlock(pp); 11224 mutex_exit(&rp->r_statelock); 11225 return; 11226 } 11227 if (pp->p_fsdata == C_DELAYCOMMIT) { 11228 pp->p_fsdata = C_COMMIT; 11229 page_unlock(pp); 11230 mutex_exit(&rp->r_statelock); 11231 return; 11232 } 11233 } 11234 11235 /* 11236 * Check to see if there is a signal which would prevent an 11237 * attempt to commit the pages from being successful. If so, 11238 * then don't bother with all of the work to gather pages and 11239 * generate the unsuccessful RPC. Just return from here and 11240 * let the page be committed at some later time. 11241 */ 11242 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11243 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11244 sigunintr(&smask); 11245 page_unlock(pp); 11246 mutex_exit(&rp->r_statelock); 11247 return; 11248 } 11249 sigunintr(&smask); 11250 11251 /* 11252 * We are starting to need to commit pages, so let's try 11253 * to commit as many as possible at once to reduce the 11254 * overhead. 11255 * 11256 * Set the `commit inprogress' state bit. We must 11257 * first wait until any current one finishes. Then 11258 * we initialize the c_pages list with this page. 11259 */ 11260 while (rp->r_flags & R4COMMIT) { 11261 rp->r_flags |= R4COMMITWAIT; 11262 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11263 rp->r_flags &= ~R4COMMITWAIT; 11264 } 11265 rp->r_flags |= R4COMMIT; 11266 mutex_exit(&rp->r_statelock); 11267 ASSERT(rp->r_commit.c_pages == NULL); 11268 rp->r_commit.c_pages = pp; 11269 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11270 rp->r_commit.c_commlen = PAGESIZE; 11271 11272 /* 11273 * Gather together all other pages which can be committed. 11274 * They will all be chained off r_commit.c_pages. 11275 */ 11276 nfs4_get_commit(vp); 11277 11278 /* 11279 * Clear the `commit inprogress' status and disconnect 11280 * the list of pages to be committed from the rnode. 11281 * At this same time, we also save the starting offset 11282 * and length of data to be committed on the server. 11283 */ 11284 plist = rp->r_commit.c_pages; 11285 rp->r_commit.c_pages = NULL; 11286 offset = rp->r_commit.c_commbase; 11287 len = rp->r_commit.c_commlen; 11288 mutex_enter(&rp->r_statelock); 11289 rp->r_flags &= ~R4COMMIT; 11290 cv_broadcast(&rp->r_commit.c_cv); 11291 mutex_exit(&rp->r_statelock); 11292 11293 if (curproc == proc_pageout || curproc == proc_fsflush || 11294 curproc->p_zone != VTOMI4(vp)->mi_zone) { 11295 nfs4_async_commit(vp, plist, offset, len, 11296 cr, do_nfs4_async_commit); 11297 return; 11298 } 11299 11300 /* 11301 * Actually generate the COMMIT op over the wire operation. 11302 */ 11303 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11304 11305 /* 11306 * If we got an error during the commit, just unlock all 11307 * of the pages. The pages will get retransmitted to the 11308 * server during a putpage operation. 11309 */ 11310 if (error) { 11311 while (plist != NULL) { 11312 pptr = plist; 11313 page_sub(&plist, pptr); 11314 page_unlock(pptr); 11315 } 11316 return; 11317 } 11318 11319 /* 11320 * We've tried as hard as we can to commit the data to stable 11321 * storage on the server. We just unlock the rest of the pages 11322 * and clear the commit required state. They will be put 11323 * onto the tail of the cachelist if they are nolonger 11324 * mapped. 11325 */ 11326 while (plist != pp) { 11327 pptr = plist; 11328 page_sub(&plist, pptr); 11329 pptr->p_fsdata = C_NOCOMMIT; 11330 page_unlock(pptr); 11331 } 11332 11333 /* 11334 * It is possible that nfs4_commit didn't return error but 11335 * some other thread has modified the page we are going 11336 * to free/destroy. 11337 * In this case we need to rewrite the page. Do an explicit check 11338 * before attempting to free/destroy the page. If modified, needs to 11339 * be rewritten so unlock the page and return. 11340 */ 11341 if (hat_ismod(pp)) { 11342 pp->p_fsdata = C_NOCOMMIT; 11343 page_unlock(pp); 11344 return; 11345 } 11346 11347 /* 11348 * Now, as appropriate, either free or destroy the page 11349 * that we were called with. 11350 */ 11351 pp->p_fsdata = C_NOCOMMIT; 11352 if (fl == B_FREE) 11353 page_free(pp, dn); 11354 else 11355 page_destroy(pp, dn); 11356 } 11357 11358 /* 11359 * Commit requires that the current fh be the file written to. 11360 * The compound op structure is: 11361 * PUTFH(file), COMMIT 11362 */ 11363 static int 11364 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11365 { 11366 COMPOUND4args_clnt args; 11367 COMPOUND4res_clnt res; 11368 COMMIT4res *cm_res; 11369 nfs_argop4 argop[2]; 11370 nfs_resop4 *resop; 11371 int doqueue; 11372 mntinfo4_t *mi; 11373 rnode4_t *rp; 11374 cred_t *cred_otw = NULL; 11375 bool_t needrecov = FALSE; 11376 nfs4_recov_state_t recov_state; 11377 nfs4_open_stream_t *osp = NULL; 11378 bool_t first_time = TRUE; /* first time getting OTW cred */ 11379 bool_t last_time = FALSE; /* last time getting OTW cred */ 11380 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11381 11382 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11383 11384 rp = VTOR4(vp); 11385 11386 mi = VTOMI4(vp); 11387 recov_state.rs_flags = 0; 11388 recov_state.rs_num_retry_despite_err = 0; 11389 get_commit_cred: 11390 /* 11391 * Releases the osp, if a valid open stream is provided. 11392 * Puts a hold on the cred_otw and the new osp (if found). 11393 */ 11394 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11395 &first_time, &last_time); 11396 args.ctag = TAG_COMMIT; 11397 recov_retry: 11398 /* 11399 * Commit ops: putfh file; commit 11400 */ 11401 args.array_len = 2; 11402 args.array = argop; 11403 11404 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11405 &recov_state, NULL); 11406 if (e.error) { 11407 crfree(cred_otw); 11408 if (osp != NULL) 11409 open_stream_rele(osp, rp); 11410 return (e.error); 11411 } 11412 11413 /* putfh directory */ 11414 argop[0].argop = OP_CPUTFH; 11415 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11416 11417 /* commit */ 11418 argop[1].argop = OP_COMMIT; 11419 argop[1].nfs_argop4_u.opcommit.offset = offset; 11420 argop[1].nfs_argop4_u.opcommit.count = count; 11421 11422 doqueue = 1; 11423 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11424 11425 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11426 if (!needrecov && e.error) { 11427 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11428 needrecov); 11429 crfree(cred_otw); 11430 if (e.error == EACCES && last_time == FALSE) 11431 goto get_commit_cred; 11432 if (osp != NULL) 11433 open_stream_rele(osp, rp); 11434 return (e.error); 11435 } 11436 11437 if (needrecov) { 11438 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11439 NULL, OP_COMMIT, NULL) == FALSE) { 11440 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11441 &recov_state, needrecov); 11442 if (!e.error) 11443 (void) xdr_free(xdr_COMPOUND4res_clnt, 11444 (caddr_t)&res); 11445 goto recov_retry; 11446 } 11447 if (e.error) { 11448 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11449 &recov_state, needrecov); 11450 crfree(cred_otw); 11451 if (osp != NULL) 11452 open_stream_rele(osp, rp); 11453 return (e.error); 11454 } 11455 /* fall through for res.status case */ 11456 } 11457 11458 if (res.status) { 11459 e.error = geterrno4(res.status); 11460 if (e.error == EACCES && last_time == FALSE) { 11461 crfree(cred_otw); 11462 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11463 &recov_state, needrecov); 11464 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11465 goto get_commit_cred; 11466 } 11467 /* 11468 * Can't do a nfs4_purge_stale_fh here because this 11469 * can cause a deadlock. nfs4_commit can 11470 * be called from nfs4_dispose which can be called 11471 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11472 * can call back to pvn_vplist_dirty. 11473 */ 11474 if (e.error == ESTALE) { 11475 mutex_enter(&rp->r_statelock); 11476 rp->r_flags |= R4STALE; 11477 if (!rp->r_error) 11478 rp->r_error = e.error; 11479 mutex_exit(&rp->r_statelock); 11480 PURGE_ATTRCACHE4(vp); 11481 } else { 11482 mutex_enter(&rp->r_statelock); 11483 if (!rp->r_error) 11484 rp->r_error = e.error; 11485 mutex_exit(&rp->r_statelock); 11486 } 11487 } else { 11488 ASSERT(rp->r_flags & R4HAVEVERF); 11489 resop = &res.array[1]; /* commit res */ 11490 cm_res = &resop->nfs_resop4_u.opcommit; 11491 mutex_enter(&rp->r_statelock); 11492 if (cm_res->writeverf == rp->r_writeverf) { 11493 mutex_exit(&rp->r_statelock); 11494 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11495 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11496 &recov_state, needrecov); 11497 crfree(cred_otw); 11498 if (osp != NULL) 11499 open_stream_rele(osp, rp); 11500 return (0); 11501 } 11502 nfs4_set_mod(vp); 11503 rp->r_writeverf = cm_res->writeverf; 11504 mutex_exit(&rp->r_statelock); 11505 e.error = NFS_VERF_MISMATCH; 11506 } 11507 11508 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11509 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11510 crfree(cred_otw); 11511 if (osp != NULL) 11512 open_stream_rele(osp, rp); 11513 11514 return (e.error); 11515 } 11516 11517 static void 11518 nfs4_set_mod(vnode_t *vp) 11519 { 11520 page_t *pp; 11521 kmutex_t *vphm; 11522 rnode4_t *rp; 11523 11524 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11525 11526 /* make sure we're looking at the master vnode, not a shadow */ 11527 11528 rp = VTOR4(vp); 11529 if (IS_SHADOW(vp, rp)) 11530 vp = RTOV4(rp); 11531 11532 vphm = page_vnode_mutex(vp); 11533 mutex_enter(vphm); 11534 /* 11535 * If there are no pages associated with this vnode, then 11536 * just return. 11537 */ 11538 if ((pp = vp->v_pages) == NULL) { 11539 mutex_exit(vphm); 11540 return; 11541 } 11542 11543 do { 11544 if (pp->p_fsdata != C_NOCOMMIT) { 11545 hat_setmod(pp); 11546 pp->p_fsdata = C_NOCOMMIT; 11547 } 11548 } while ((pp = pp->p_vpnext) != vp->v_pages); 11549 mutex_exit(vphm); 11550 } 11551 11552 /* 11553 * This function is used to gather a page list of the pages which 11554 * can be committed on the server. 11555 * 11556 * The calling thread must have set R4COMMIT. This bit is used to 11557 * serialize access to the commit structure in the rnode. As long 11558 * as the thread has set R4COMMIT, then it can manipulate the commit 11559 * structure without requiring any other locks. 11560 * 11561 * When this function is called from nfs4_dispose() the page passed 11562 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11563 * will skip it. This is not a problem since we initially add the 11564 * page to the r_commit page list. 11565 * 11566 */ 11567 static void 11568 nfs4_get_commit(vnode_t *vp) 11569 { 11570 rnode4_t *rp; 11571 page_t *pp; 11572 kmutex_t *vphm; 11573 11574 rp = VTOR4(vp); 11575 11576 ASSERT(rp->r_flags & R4COMMIT); 11577 11578 /* make sure we're looking at the master vnode, not a shadow */ 11579 11580 if (IS_SHADOW(vp, rp)) 11581 vp = RTOV4(rp); 11582 11583 vphm = page_vnode_mutex(vp); 11584 mutex_enter(vphm); 11585 11586 /* 11587 * If there are no pages associated with this vnode, then 11588 * just return. 11589 */ 11590 if ((pp = vp->v_pages) == NULL) { 11591 mutex_exit(vphm); 11592 return; 11593 } 11594 11595 /* 11596 * Step through all of the pages associated with this vnode 11597 * looking for pages which need to be committed. 11598 */ 11599 do { 11600 /* 11601 * First short-cut everything (without the page_lock) 11602 * and see if this page does not need to be committed 11603 * or is modified if so then we'll just skip it. 11604 */ 11605 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11606 continue; 11607 11608 /* 11609 * Attempt to lock the page. If we can't, then 11610 * someone else is messing with it or we have been 11611 * called from nfs4_dispose and this is the page that 11612 * nfs4_dispose was called with.. anyway just skip it. 11613 */ 11614 if (!page_trylock(pp, SE_EXCL)) 11615 continue; 11616 11617 /* 11618 * Lets check again now that we have the page lock. 11619 */ 11620 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11621 page_unlock(pp); 11622 continue; 11623 } 11624 11625 /* this had better not be a free page */ 11626 ASSERT(PP_ISFREE(pp) == 0); 11627 11628 /* 11629 * The page needs to be committed and we locked it. 11630 * Update the base and length parameters and add it 11631 * to r_pages. 11632 */ 11633 if (rp->r_commit.c_pages == NULL) { 11634 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11635 rp->r_commit.c_commlen = PAGESIZE; 11636 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11637 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11638 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11639 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11640 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11641 <= pp->p_offset) { 11642 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11643 rp->r_commit.c_commbase + PAGESIZE; 11644 } 11645 page_add(&rp->r_commit.c_pages, pp); 11646 } while ((pp = pp->p_vpnext) != vp->v_pages); 11647 11648 mutex_exit(vphm); 11649 } 11650 11651 /* 11652 * This routine is used to gather together a page list of the pages 11653 * which are to be committed on the server. This routine must not 11654 * be called if the calling thread holds any locked pages. 11655 * 11656 * The calling thread must have set R4COMMIT. This bit is used to 11657 * serialize access to the commit structure in the rnode. As long 11658 * as the thread has set R4COMMIT, then it can manipulate the commit 11659 * structure without requiring any other locks. 11660 */ 11661 static void 11662 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 11663 { 11664 11665 rnode4_t *rp; 11666 page_t *pp; 11667 u_offset_t end; 11668 u_offset_t off; 11669 ASSERT(len != 0); 11670 rp = VTOR4(vp); 11671 ASSERT(rp->r_flags & R4COMMIT); 11672 11673 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11674 11675 /* make sure we're looking at the master vnode, not a shadow */ 11676 11677 if (IS_SHADOW(vp, rp)) 11678 vp = RTOV4(rp); 11679 11680 /* 11681 * If there are no pages associated with this vnode, then 11682 * just return. 11683 */ 11684 if ((pp = vp->v_pages) == NULL) 11685 return; 11686 /* 11687 * Calculate the ending offset. 11688 */ 11689 end = soff + len; 11690 for (off = soff; off < end; off += PAGESIZE) { 11691 /* 11692 * Lookup each page by vp, offset. 11693 */ 11694 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 11695 continue; 11696 /* 11697 * If this page does not need to be committed or is 11698 * modified, then just skip it. 11699 */ 11700 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11701 page_unlock(pp); 11702 continue; 11703 } 11704 11705 ASSERT(PP_ISFREE(pp) == 0); 11706 /* 11707 * The page needs to be committed and we locked it. 11708 * Update the base and length parameters and add it 11709 * to r_pages. 11710 */ 11711 if (rp->r_commit.c_pages == NULL) { 11712 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11713 rp->r_commit.c_commlen = PAGESIZE; 11714 } else { 11715 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11716 rp->r_commit.c_commbase + PAGESIZE; 11717 } 11718 page_add(&rp->r_commit.c_pages, pp); 11719 } 11720 } 11721 11722 /* 11723 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 11724 * Flushes and commits data to the server. 11725 */ 11726 static int 11727 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 11728 { 11729 int error; 11730 verifier4 write_verf; 11731 rnode4_t *rp = VTOR4(vp); 11732 11733 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11734 11735 /* 11736 * Flush the data portion of the file and then commit any 11737 * portions which need to be committed. This may need to 11738 * be done twice if the server has changed state since 11739 * data was last written. The data will need to be 11740 * rewritten to the server and then a new commit done. 11741 * 11742 * In fact, this may need to be done several times if the 11743 * server is having problems and crashing while we are 11744 * attempting to do this. 11745 */ 11746 11747 top: 11748 /* 11749 * Do a flush based on the poff and plen arguments. This 11750 * will synchronously write out any modified pages in the 11751 * range specified by (poff, plen). This starts all of the 11752 * i/o operations which will be waited for in the next 11753 * call to nfs4_putpage 11754 */ 11755 11756 mutex_enter(&rp->r_statelock); 11757 write_verf = rp->r_writeverf; 11758 mutex_exit(&rp->r_statelock); 11759 11760 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr); 11761 if (error == EAGAIN) 11762 error = 0; 11763 11764 /* 11765 * Do a flush based on the poff and plen arguments. This 11766 * will synchronously write out any modified pages in the 11767 * range specified by (poff, plen) and wait until all of 11768 * the asynchronous i/o's in that range are done as well. 11769 */ 11770 if (!error) 11771 error = nfs4_putpage(vp, poff, plen, 0, cr); 11772 11773 if (error) 11774 return (error); 11775 11776 mutex_enter(&rp->r_statelock); 11777 if (rp->r_writeverf != write_verf) { 11778 mutex_exit(&rp->r_statelock); 11779 goto top; 11780 } 11781 mutex_exit(&rp->r_statelock); 11782 11783 /* 11784 * Now commit any pages which might need to be committed. 11785 * If the error, NFS_VERF_MISMATCH, is returned, then 11786 * start over with the flush operation. 11787 */ 11788 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 11789 11790 if (error == NFS_VERF_MISMATCH) 11791 goto top; 11792 11793 return (error); 11794 } 11795 11796 /* 11797 * nfs4_commit_vp() will wait for other pending commits and 11798 * will either commit the whole file or a range, plen dictates 11799 * if we commit whole file. a value of zero indicates the whole 11800 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 11801 */ 11802 static int 11803 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 11804 cred_t *cr, int wait_on_writes) 11805 { 11806 rnode4_t *rp; 11807 page_t *plist; 11808 offset3 offset; 11809 count3 len; 11810 11811 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11812 11813 rp = VTOR4(vp); 11814 11815 /* 11816 * before we gather commitable pages make 11817 * sure there are no outstanding async writes 11818 */ 11819 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 11820 mutex_enter(&rp->r_statelock); 11821 while (rp->r_count > 0) { 11822 cv_wait(&rp->r_cv, &rp->r_statelock); 11823 } 11824 mutex_exit(&rp->r_statelock); 11825 } 11826 11827 /* 11828 * Set the `commit inprogress' state bit. We must 11829 * first wait until any current one finishes. 11830 */ 11831 mutex_enter(&rp->r_statelock); 11832 while (rp->r_flags & R4COMMIT) { 11833 rp->r_flags |= R4COMMITWAIT; 11834 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11835 rp->r_flags &= ~R4COMMITWAIT; 11836 } 11837 rp->r_flags |= R4COMMIT; 11838 mutex_exit(&rp->r_statelock); 11839 11840 /* 11841 * Gather all of the pages which need to be 11842 * committed. 11843 */ 11844 if (plen == 0) 11845 nfs4_get_commit(vp); 11846 else 11847 nfs4_get_commit_range(vp, poff, plen); 11848 11849 /* 11850 * Clear the `commit inprogress' bit and disconnect the 11851 * page list which was gathered by nfs4_get_commit. 11852 */ 11853 plist = rp->r_commit.c_pages; 11854 rp->r_commit.c_pages = NULL; 11855 offset = rp->r_commit.c_commbase; 11856 len = rp->r_commit.c_commlen; 11857 mutex_enter(&rp->r_statelock); 11858 rp->r_flags &= ~R4COMMIT; 11859 cv_broadcast(&rp->r_commit.c_cv); 11860 mutex_exit(&rp->r_statelock); 11861 11862 /* 11863 * If any pages need to be committed, commit them and 11864 * then unlock them so that they can be freed some 11865 * time later. 11866 */ 11867 if (plist == NULL) 11868 return (0); 11869 11870 /* 11871 * No error occurred during the flush portion 11872 * of this operation, so now attempt to commit 11873 * the data to stable storage on the server. 11874 * 11875 * This will unlock all of the pages on the list. 11876 */ 11877 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 11878 } 11879 11880 static int 11881 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 11882 cred_t *cr) 11883 { 11884 int error; 11885 page_t *pp; 11886 11887 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 11888 11889 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 11890 11891 /* 11892 * If we got an error, then just unlock all of the pages 11893 * on the list. 11894 */ 11895 if (error) { 11896 while (plist != NULL) { 11897 pp = plist; 11898 page_sub(&plist, pp); 11899 page_unlock(pp); 11900 } 11901 return (error); 11902 } 11903 /* 11904 * We've tried as hard as we can to commit the data to stable 11905 * storage on the server. We just unlock the pages and clear 11906 * the commit required state. They will get freed later. 11907 */ 11908 while (plist != NULL) { 11909 pp = plist; 11910 page_sub(&plist, pp); 11911 pp->p_fsdata = C_NOCOMMIT; 11912 page_unlock(pp); 11913 } 11914 11915 return (error); 11916 } 11917 11918 static void 11919 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 11920 cred_t *cr) 11921 { 11922 11923 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 11924 } 11925 11926 /*ARGSUSED*/ 11927 static int 11928 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 11929 { 11930 int error = 0; 11931 mntinfo4_t *mi; 11932 vattr_t va; 11933 vsecattr_t nfsace4_vsap; 11934 11935 mi = VTOMI4(vp); 11936 if (curproc->p_zone != mi->mi_zone) 11937 return (EIO); 11938 if (mi->mi_flags & MI4_ACL) { 11939 /* if we have a delegation, return it */ 11940 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 11941 (void) nfs4delegreturn(VTOR4(vp), 11942 NFS4_DR_REOPEN|NFS4_DR_PUSH); 11943 11944 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 11945 NFS4_ACL_SET); 11946 if (error) /* EINVAL */ 11947 return (error); 11948 11949 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 11950 /* 11951 * These are aclent_t type entries. 11952 */ 11953 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 11954 vp->v_type == VDIR, FALSE); 11955 if (error) 11956 return (error); 11957 } else { 11958 /* 11959 * These are ace_t type entries. 11960 */ 11961 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 11962 vp->v_type == VDIR, FALSE); 11963 if (error) 11964 return (error); 11965 } 11966 bzero(&va, sizeof (va)); 11967 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 11968 vs_ace4_destroy(&nfsace4_vsap); 11969 return (error); 11970 } 11971 return (ENOSYS); 11972 } 11973 11974 static int 11975 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 11976 { 11977 int error; 11978 mntinfo4_t *mi; 11979 nfs4_ga_res_t gar; 11980 rnode4_t *rp = VTOR4(vp); 11981 11982 mi = VTOMI4(vp); 11983 if (curproc->p_zone != mi->mi_zone) 11984 return (EIO); 11985 11986 bzero(&gar, sizeof (gar)); 11987 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 11988 11989 /* 11990 * vsecattr->vsa_mask holds the original acl request mask. 11991 * This is needed when determining what to return. 11992 * (See: nfs4_create_getsecattr_return()) 11993 */ 11994 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 11995 if (error) /* EINVAL */ 11996 return (error); 11997 11998 if (mi->mi_flags & MI4_ACL) { 11999 /* 12000 * Check if the data is cached and the cache is valid. If it 12001 * is we don't go over the wire. 12002 */ 12003 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12004 mutex_enter(&rp->r_statelock); 12005 if (rp->r_secattr != NULL) { 12006 error = nfs4_create_getsecattr_return( 12007 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12008 rp->r_attr.va_gid, 12009 vp->v_type == VDIR); 12010 if (!error) { /* error == 0 - Success! */ 12011 mutex_exit(&rp->r_statelock); 12012 return (error); 12013 } 12014 } 12015 mutex_exit(&rp->r_statelock); 12016 } 12017 12018 /* 12019 * The getattr otw call will always get both the acl, in 12020 * the form of a list of nfsace4's, and the number of acl 12021 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12022 */ 12023 gar.n4g_va.va_mask = AT_ALL; 12024 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12025 if (error) { 12026 vs_ace4_destroy(&gar.n4g_vsa); 12027 if (error == ENOTSUP || error == EOPNOTSUPP) 12028 error = fs_fab_acl(vp, vsecattr, flag, cr); 12029 return (error); 12030 } 12031 12032 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12033 /* 12034 * No error was returned, but according to the response 12035 * bitmap, neither was an acl. 12036 */ 12037 vs_ace4_destroy(&gar.n4g_vsa); 12038 error = fs_fab_acl(vp, vsecattr, flag, cr); 12039 return (error); 12040 } 12041 12042 /* 12043 * Update the cache with the ACL. 12044 */ 12045 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12046 12047 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12048 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12049 vp->v_type == VDIR); 12050 vs_ace4_destroy(&gar.n4g_vsa); 12051 if ((error) && (vsecattr->vsa_mask & 12052 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12053 (error != EACCES)) { 12054 error = fs_fab_acl(vp, vsecattr, flag, cr); 12055 } 12056 return (error); 12057 } 12058 error = fs_fab_acl(vp, vsecattr, flag, cr); 12059 return (error); 12060 } 12061 12062 /* 12063 * The function returns: 12064 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12065 * - EINVAL if the passed in "acl_mask" is an invalid request. 12066 * 12067 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12068 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12069 * 12070 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12071 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12072 * - We have a count field set without the corresponding acl field set. (e.g. - 12073 * VSA_ACECNT is set, but VSA_ACE is not) 12074 */ 12075 static int 12076 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12077 { 12078 /* Shortcut the masks that are always valid. */ 12079 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12080 return (0); 12081 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12082 return (0); 12083 12084 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12085 /* 12086 * We can't have any VSA_ACL type stuff in the mask now. 12087 */ 12088 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12089 VSA_DFACLCNT)) 12090 return (EINVAL); 12091 12092 if (op == NFS4_ACL_SET) { 12093 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12094 return (EINVAL); 12095 } 12096 } 12097 12098 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12099 /* 12100 * We can't have any VSA_ACE type stuff in the mask now. 12101 */ 12102 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12103 return (EINVAL); 12104 12105 if (op == NFS4_ACL_SET) { 12106 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12107 return (EINVAL); 12108 12109 if ((acl_mask & VSA_DFACLCNT) && 12110 !(acl_mask & VSA_DFACL)) 12111 return (EINVAL); 12112 } 12113 } 12114 return (0); 12115 } 12116 12117 /* 12118 * The theory behind creating the correct getsecattr return is simply this: 12119 * "Don't return anything that the caller is not expecting to have to free." 12120 */ 12121 static int 12122 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12123 uid_t uid, gid_t gid, int isdir) 12124 { 12125 int error = 0; 12126 /* Save the mask since the translators modify it. */ 12127 uint_t orig_mask = vsap->vsa_mask; 12128 12129 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12130 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, 12131 isdir, FALSE, ((orig_mask & VSA_ACE) ? FALSE : TRUE)); 12132 12133 if (error) 12134 return (error); 12135 12136 /* 12137 * If the caller only asked for the ace count (VSA_ACECNT) 12138 * don't give them the full acl (VSA_ACE), free it. 12139 */ 12140 if (!orig_mask & VSA_ACE) { 12141 if (vsap->vsa_aclentp != NULL) { 12142 kmem_free(vsap->vsa_aclentp, 12143 vsap->vsa_aclcnt * sizeof (ace_t)); 12144 vsap->vsa_aclentp = NULL; 12145 } 12146 } 12147 vsap->vsa_mask = orig_mask; 12148 12149 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12150 VSA_DFACLCNT)) { 12151 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12152 isdir, FALSE, 12153 ((orig_mask & (VSA_ACL | VSA_DFACL)) ? FALSE : TRUE)); 12154 12155 if (error) 12156 return (error); 12157 12158 /* 12159 * If the caller only asked for the acl count (VSA_ACLCNT) 12160 * and/or the default acl count (VSA_DFACLCNT) don't give them 12161 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12162 */ 12163 if (!orig_mask & VSA_ACL) { 12164 if (vsap->vsa_aclentp != NULL) { 12165 kmem_free(vsap->vsa_aclentp, 12166 vsap->vsa_aclcnt * sizeof (aclent_t)); 12167 vsap->vsa_aclentp = NULL; 12168 } 12169 } 12170 12171 if (!orig_mask & VSA_DFACL) { 12172 if (vsap->vsa_dfaclentp != NULL) { 12173 kmem_free(vsap->vsa_dfaclentp, 12174 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12175 vsap->vsa_dfaclentp = NULL; 12176 } 12177 } 12178 vsap->vsa_mask = orig_mask; 12179 } 12180 return (0); 12181 } 12182 12183 static int 12184 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr) 12185 { 12186 int error; 12187 12188 if (curproc->p_zone != VTOMI4(vp)->mi_zone) 12189 return (EIO); 12190 /* 12191 * check for valid cmd parameter 12192 */ 12193 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12194 return (EINVAL); 12195 12196 /* 12197 * Check access permissions 12198 */ 12199 if ((cmd & F_SHARE) && 12200 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12201 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12202 return (EBADF); 12203 12204 /* 12205 * If the filesystem is mounted using local locking, pass the 12206 * request off to the local share code. 12207 */ 12208 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12209 return (fs_shrlock(vp, cmd, shr, flag, cr)); 12210 12211 switch (cmd) { 12212 case F_SHARE: 12213 case F_UNSHARE: 12214 /* 12215 * This will be properly implemented later, 12216 * see RFE: 4823948 . 12217 */ 12218 error = EAGAIN; 12219 break; 12220 12221 case F_HASREMOTELOCKS: 12222 /* 12223 * NFS client can't store remote locks itself 12224 */ 12225 shr->s_access = 0; 12226 error = 0; 12227 break; 12228 12229 default: 12230 error = EINVAL; 12231 break; 12232 } 12233 12234 return (error); 12235 } 12236 12237 /* 12238 * Common code called by directory ops to update the attrcache 12239 */ 12240 static int 12241 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12242 hrtime_t t, vnode_t *vp, cred_t *cr) 12243 { 12244 int error = 0; 12245 12246 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12247 12248 if (status != NFS4_OK) { 12249 /* getattr not done or failed */ 12250 PURGE_ATTRCACHE4(vp); 12251 return (error); 12252 } 12253 12254 if (garp) { 12255 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12256 } else { 12257 PURGE_ATTRCACHE4(vp); 12258 } 12259 return (error); 12260 } 12261 12262 /* 12263 * Update directory caches for directory modification ops (link, rename, etc.) 12264 * When dinfo is NULL, manage dircaches in the old way. 12265 */ 12266 static void 12267 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12268 dirattr_info_t *dinfo) 12269 { 12270 rnode4_t *drp = VTOR4(dvp); 12271 12272 ASSERT(curproc->p_zone == VTOMI4(dvp)->mi_zone); 12273 12274 /* Purge rddir cache for dir since it changed */ 12275 if (drp->r_dir != NULL) 12276 nfs4_purge_rddir_cache(dvp); 12277 12278 /* 12279 * If caller provided dinfo, then use it to manage dir caches. 12280 */ 12281 if (dinfo != NULL) { 12282 if (vp != NULL) { 12283 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12284 if (!VTOR4(vp)->created_v4) { 12285 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12286 dnlc_update(dvp, nm, vp); 12287 } else { 12288 /* 12289 * XXX don't update if the created_v4 flag is 12290 * set 12291 */ 12292 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12293 NFS4_DEBUG(nfs4_client_state_debug, 12294 (CE_NOTE, "nfs4_update_dircaches: " 12295 "don't update dnlc: created_v4 flag")); 12296 } 12297 } 12298 12299 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12300 dinfo->di_cred, FALSE, cinfo); 12301 12302 return; 12303 } 12304 12305 /* 12306 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12307 * Since caller modified dir but didn't receive post-dirmod-op dir 12308 * attrs, the dir's attrs must be purged. 12309 * 12310 * XXX this check and dnlc update/purge should really be atomic, 12311 * XXX but can't use rnode statelock because it'll deadlock in 12312 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12313 * XXX does occur. 12314 * 12315 * XXX We also may want to check that atomic is true in the 12316 * XXX change_info struct. If it is not, the change_info may 12317 * XXX reflect changes by more than one clients which means that 12318 * XXX our cache may not be valid. 12319 */ 12320 PURGE_ATTRCACHE4(dvp); 12321 if (drp->r_change == cinfo->before) { 12322 /* no changes took place in the directory prior to our link */ 12323 if (vp != NULL) { 12324 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12325 if (!VTOR4(vp)->created_v4) { 12326 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12327 dnlc_update(dvp, nm, vp); 12328 } else { 12329 /* 12330 * XXX dont' update if the created_v4 flag 12331 * is set 12332 */ 12333 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12334 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12335 "nfs4_update_dircaches: don't" 12336 " update dnlc: created_v4 flag")); 12337 } 12338 } 12339 } else { 12340 /* Another client modified directory - purge its dnlc cache */ 12341 dnlc_purge_vp(dvp); 12342 } 12343 } 12344 12345 /* 12346 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12347 * file. 12348 * 12349 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12350 * file (ie: client recovery) and otherwise set to FALSE. 12351 * 12352 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12353 * initiated) calling functions. 12354 * 12355 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12356 * of resending a 'lost' open request. 12357 * 12358 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12359 * server that hands out BAD_SEQID on open confirm. 12360 * 12361 * Errors are returned via the nfs4_error_t parameter. 12362 */ 12363 void 12364 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12365 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12366 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12367 { 12368 COMPOUND4args_clnt args; 12369 COMPOUND4res_clnt res; 12370 nfs_argop4 argop[2]; 12371 nfs_resop4 *resop; 12372 int doqueue = 1; 12373 mntinfo4_t *mi; 12374 OPEN_CONFIRM4args *open_confirm_args; 12375 int needrecov; 12376 12377 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12378 #if DEBUG 12379 mutex_enter(&oop->oo_lock); 12380 ASSERT(oop->oo_seqid_inuse); 12381 mutex_exit(&oop->oo_lock); 12382 #endif 12383 12384 recov_retry_confirm: 12385 nfs4_error_zinit(ep); 12386 *retry_open = FALSE; 12387 12388 if (resend) 12389 args.ctag = TAG_OPEN_CONFIRM_LOST; 12390 else 12391 args.ctag = TAG_OPEN_CONFIRM; 12392 12393 args.array_len = 2; 12394 args.array = argop; 12395 12396 /* putfh target fh */ 12397 argop[0].argop = OP_CPUTFH; 12398 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12399 12400 argop[1].argop = OP_OPEN_CONFIRM; 12401 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12402 12403 (*seqid) += 1; 12404 open_confirm_args->seqid = *seqid; 12405 open_confirm_args->open_stateid = *stateid; 12406 12407 mi = VTOMI4(vp); 12408 12409 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12410 12411 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12412 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12413 } 12414 12415 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12416 if (!needrecov && ep->error) 12417 return; 12418 12419 if (needrecov) { 12420 bool_t abort = FALSE; 12421 12422 if (reopening_file == FALSE) { 12423 nfs4_bseqid_entry_t *bsep = NULL; 12424 12425 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12426 bsep = nfs4_create_bseqid_entry(oop, NULL, 12427 vp, 0, args.ctag, 12428 open_confirm_args->seqid); 12429 12430 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, 12431 NULL, NULL, NULL, OP_OPEN_CONFIRM, bsep); 12432 if (bsep) { 12433 kmem_free(bsep, sizeof (*bsep)); 12434 if (num_bseqid_retryp && 12435 --(*num_bseqid_retryp) == 0) 12436 abort = TRUE; 12437 } 12438 } 12439 if ((ep->error == ETIMEDOUT || 12440 res.status == NFS4ERR_RESOURCE) && 12441 abort == FALSE && resend == FALSE) { 12442 if (!ep->error) 12443 (void) xdr_free(xdr_COMPOUND4res_clnt, 12444 (caddr_t)&res); 12445 12446 delay(SEC_TO_TICK(confirm_retry_sec)); 12447 goto recov_retry_confirm; 12448 } 12449 /* State may have changed so retry the entire OPEN op */ 12450 if (abort == FALSE) 12451 *retry_open = TRUE; 12452 else 12453 *retry_open = FALSE; 12454 if (!ep->error) 12455 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12456 return; 12457 } 12458 12459 if (res.status) { 12460 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12461 return; 12462 } 12463 12464 resop = &res.array[1]; /* open confirm res */ 12465 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12466 stateid, sizeof (*stateid)); 12467 12468 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12469 } 12470 12471 /* 12472 * Return the credentials associated with a client state object. The 12473 * caller is responsible for freeing the credentials. 12474 */ 12475 12476 static cred_t * 12477 state_to_cred(nfs4_open_stream_t *osp) 12478 { 12479 cred_t *cr; 12480 12481 /* 12482 * It's ok to not lock the open stream and open owner to get 12483 * the oo_cred since this is only written once (upon creation) 12484 * and will not change. 12485 */ 12486 cr = osp->os_open_owner->oo_cred; 12487 crhold(cr); 12488 12489 return (cr); 12490 } 12491 12492 /* 12493 * nfs4_find_sysid 12494 * 12495 * Find the sysid for the knetconfig associated with the given mi. 12496 */ 12497 static struct lm_sysid * 12498 nfs4_find_sysid(mntinfo4_t *mi) 12499 { 12500 ASSERT(curproc->p_zone == mi->mi_zone); 12501 12502 /* 12503 * Switch from RDMA knconf to original mount knconf 12504 */ 12505 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12506 mi->mi_curr_serv->sv_hostname, NULL)); 12507 } 12508 12509 #ifdef DEBUG 12510 /* 12511 * Return a string version of the call type for easy reading. 12512 */ 12513 static char * 12514 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12515 { 12516 switch (ctype) { 12517 case NFS4_LCK_CTYPE_NORM: 12518 return ("NORMAL"); 12519 case NFS4_LCK_CTYPE_RECLAIM: 12520 return ("RECLAIM"); 12521 case NFS4_LCK_CTYPE_RESEND: 12522 return ("RESEND"); 12523 case NFS4_LCK_CTYPE_REINSTATE: 12524 return ("REINSTATE"); 12525 default: 12526 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12527 "type %d", ctype); 12528 return (""); 12529 } 12530 } 12531 #endif 12532 12533 /* 12534 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12535 * Unlock requests don't have an over-the-wire locktype, so we just return 12536 * something non-threatening. 12537 */ 12538 12539 static nfs_lock_type4 12540 flk_to_locktype(int cmd, int l_type) 12541 { 12542 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12543 12544 switch (l_type) { 12545 case F_UNLCK: 12546 return (READ_LT); 12547 case F_RDLCK: 12548 if (cmd == F_SETLK) 12549 return (READ_LT); 12550 else 12551 return (READW_LT); 12552 case F_WRLCK: 12553 if (cmd == F_SETLK) 12554 return (WRITE_LT); 12555 else 12556 return (WRITEW_LT); 12557 } 12558 panic("flk_to_locktype"); 12559 /*NOTREACHED*/ 12560 } 12561 12562 /* 12563 * Do some preliminary checks for nfs4frlock. 12564 */ 12565 static int 12566 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12567 u_offset_t offset) 12568 { 12569 int error = 0; 12570 12571 /* 12572 * If we are setting a lock, check that the file is opened 12573 * with the correct mode. 12574 */ 12575 if (cmd == F_SETLK || cmd == F_SETLKW) { 12576 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12577 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12578 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12579 "nfs4frlock_validate_args: file was opened with " 12580 "incorrect mode")); 12581 return (EBADF); 12582 } 12583 } 12584 12585 /* Convert the offset. It may need to be restored before returning. */ 12586 if (error = convoff(vp, flk, 0, offset)) { 12587 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12588 "nfs4frlock_validate_args: convoff => error= %d\n", 12589 error)); 12590 return (error); 12591 } 12592 12593 return (error); 12594 } 12595 12596 /* 12597 * Set the flock64's lm_sysid for nfs4frlock. 12598 */ 12599 static int 12600 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12601 { 12602 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12603 12604 /* Find the lm_sysid */ 12605 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12606 12607 if (*lspp == NULL) { 12608 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12609 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12610 return (ENOLCK); 12611 } 12612 12613 flk->l_sysid = lm_sysidt(*lspp); 12614 12615 return (0); 12616 } 12617 12618 /* 12619 * Do the remaining preliminary setup for nfs4frlock. 12620 */ 12621 static void 12622 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12623 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12624 cred_t **cred_otw) 12625 { 12626 /* 12627 * set tick_delay to the base delay time. 12628 * (NFS4_BASE_WAIT_TIME is in secs) 12629 */ 12630 12631 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000); 12632 12633 /* 12634 * If lock is relative to EOF, we need the newest length of the 12635 * file. Therefore invalidate the ATTR_CACHE. 12636 */ 12637 12638 *whencep = flk->l_whence; 12639 12640 if (*whencep == 2) /* SEEK_END */ 12641 PURGE_ATTRCACHE4(vp); 12642 12643 recov_statep->rs_flags = 0; 12644 recov_statep->rs_num_retry_despite_err = 0; 12645 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 12646 } 12647 12648 /* 12649 * Initialize and allocate the data structures necessary for 12650 * the nfs4frlock call. 12651 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 12652 */ 12653 static void 12654 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 12655 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 12656 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 12657 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 12658 { 12659 int argoplist_size; 12660 int num_ops = 2; 12661 12662 *retry = FALSE; 12663 *did_start_fop = FALSE; 12664 *skip_get_err = FALSE; 12665 lost_rqstp->lr_op = 0; 12666 argoplist_size = num_ops * sizeof (nfs_argop4); 12667 /* fill array with zero */ 12668 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 12669 12670 *argspp = argsp; 12671 *respp = NULL; 12672 12673 argsp->array_len = num_ops; 12674 argsp->array = *argopp; 12675 12676 /* initialize in case of error; will get real value down below */ 12677 argsp->ctag = TAG_NONE; 12678 12679 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 12680 *op_hintp = OH_LOCKU; 12681 else 12682 *op_hintp = OH_OTHER; 12683 } 12684 12685 /* 12686 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 12687 * the proper nfs4_server_t for this instance of nfs4frlock. 12688 * Returns 0 (success) or an errno value. 12689 */ 12690 static int 12691 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 12692 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 12693 bool_t *did_start_fop, bool_t *startrecovp) 12694 { 12695 int error = 0; 12696 rnode4_t *rp; 12697 12698 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12699 12700 if (ctype == NFS4_LCK_CTYPE_NORM) { 12701 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 12702 recov_statep, startrecovp); 12703 if (error) 12704 return (error); 12705 *did_start_fop = TRUE; 12706 } else { 12707 *did_start_fop = FALSE; 12708 *startrecovp = FALSE; 12709 } 12710 12711 if (!error) { 12712 rp = VTOR4(vp); 12713 12714 /* If the file failed recovery, just quit. */ 12715 mutex_enter(&rp->r_statelock); 12716 if (rp->r_flags & R4RECOVERR) { 12717 error = EIO; 12718 } 12719 mutex_exit(&rp->r_statelock); 12720 } 12721 12722 return (error); 12723 } 12724 12725 /* 12726 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 12727 * resend nfs4frlock call is initiated by the recovery framework. 12728 * Acquires the lop and oop seqid synchronization. 12729 */ 12730 static void 12731 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 12732 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 12733 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 12734 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 12735 { 12736 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 12737 int error; 12738 12739 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 12740 (CE_NOTE, 12741 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 12742 ASSERT(resend_rqstp != NULL); 12743 ASSERT(resend_rqstp->lr_op == OP_LOCK || 12744 resend_rqstp->lr_op == OP_LOCKU); 12745 12746 *oopp = resend_rqstp->lr_oop; 12747 if (resend_rqstp->lr_oop) { 12748 open_owner_hold(resend_rqstp->lr_oop); 12749 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 12750 ASSERT(error == 0); /* recov thread always succeeds */ 12751 } 12752 12753 /* Must resend this lost lock/locku request. */ 12754 ASSERT(resend_rqstp->lr_lop != NULL); 12755 *lopp = resend_rqstp->lr_lop; 12756 lock_owner_hold(resend_rqstp->lr_lop); 12757 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 12758 ASSERT(error == 0); /* recov thread always succeeds */ 12759 12760 *ospp = resend_rqstp->lr_osp; 12761 if (*ospp) 12762 open_stream_hold(resend_rqstp->lr_osp); 12763 12764 if (resend_rqstp->lr_op == OP_LOCK) { 12765 LOCK4args *lock_args; 12766 12767 argop->argop = OP_LOCK; 12768 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 12769 lock_args->locktype = resend_rqstp->lr_locktype; 12770 lock_args->reclaim = 12771 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 12772 lock_args->offset = resend_rqstp->lr_flk->l_start; 12773 lock_args->length = resend_rqstp->lr_flk->l_len; 12774 if (lock_args->length == 0) 12775 lock_args->length = ~lock_args->length; 12776 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 12777 mi2clientid(mi), &lock_args->locker); 12778 12779 switch (resend_rqstp->lr_ctype) { 12780 case NFS4_LCK_CTYPE_RESEND: 12781 argsp->ctag = TAG_LOCK_RESEND; 12782 break; 12783 case NFS4_LCK_CTYPE_REINSTATE: 12784 argsp->ctag = TAG_LOCK_REINSTATE; 12785 break; 12786 case NFS4_LCK_CTYPE_RECLAIM: 12787 argsp->ctag = TAG_LOCK_RECLAIM; 12788 break; 12789 default: 12790 argsp->ctag = TAG_LOCK_UNKNOWN; 12791 break; 12792 } 12793 } else { 12794 LOCKU4args *locku_args; 12795 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 12796 12797 argop->argop = OP_LOCKU; 12798 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 12799 locku_args->locktype = READ_LT; 12800 locku_args->seqid = lop->lock_seqid + 1; 12801 mutex_enter(&lop->lo_lock); 12802 locku_args->lock_stateid = lop->lock_stateid; 12803 mutex_exit(&lop->lo_lock); 12804 locku_args->offset = resend_rqstp->lr_flk->l_start; 12805 locku_args->length = resend_rqstp->lr_flk->l_len; 12806 if (locku_args->length == 0) 12807 locku_args->length = ~locku_args->length; 12808 12809 switch (resend_rqstp->lr_ctype) { 12810 case NFS4_LCK_CTYPE_RESEND: 12811 argsp->ctag = TAG_LOCKU_RESEND; 12812 break; 12813 case NFS4_LCK_CTYPE_REINSTATE: 12814 argsp->ctag = TAG_LOCKU_REINSTATE; 12815 break; 12816 default: 12817 argsp->ctag = TAG_LOCK_UNKNOWN; 12818 break; 12819 } 12820 } 12821 } 12822 12823 /* 12824 * Setup the LOCKT4 arguments. 12825 */ 12826 static void 12827 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 12828 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 12829 rnode4_t *rp) 12830 { 12831 LOCKT4args *lockt_args; 12832 12833 ASSERT(curproc->p_zone == VTOMI4(RTOV4(rp))->mi_zone); 12834 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 12835 argop->argop = OP_LOCKT; 12836 argsp->ctag = TAG_LOCKT; 12837 lockt_args = &argop->nfs_argop4_u.oplockt; 12838 12839 /* 12840 * The locktype will be READ_LT unless it's 12841 * a write lock. We do this because the Solaris 12842 * system call allows the combination of 12843 * F_UNLCK and F_GETLK* and so in that case the 12844 * unlock is mapped to a read. 12845 */ 12846 if (flk->l_type == F_WRLCK) 12847 lockt_args->locktype = WRITE_LT; 12848 else 12849 lockt_args->locktype = READ_LT; 12850 12851 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 12852 /* set the lock owner4 args */ 12853 nfs4_setlockowner_args(&lockt_args->owner, rp, 12854 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 12855 flk->l_pid); 12856 lockt_args->offset = flk->l_start; 12857 lockt_args->length = flk->l_len; 12858 if (flk->l_len == 0) 12859 lockt_args->length = ~lockt_args->length; 12860 12861 *lockt_argsp = lockt_args; 12862 } 12863 12864 /* 12865 * If the client is holding a delegation, and the open stream to be used 12866 * with this lock request is a delegation open stream, then re-open the stream. 12867 * Sets the nfs4_error_t to all zeros unless the open stream has already 12868 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 12869 * means the caller should retry (like a recovery retry). 12870 */ 12871 static void 12872 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 12873 { 12874 open_delegation_type4 dt; 12875 bool_t reopen_needed, force; 12876 nfs4_open_stream_t *osp; 12877 open_claim_type4 oclaim; 12878 rnode4_t *rp = VTOR4(vp); 12879 mntinfo4_t *mi = VTOMI4(vp); 12880 12881 ASSERT(curproc->p_zone == mi->mi_zone); 12882 12883 nfs4_error_zinit(ep); 12884 12885 mutex_enter(&rp->r_statev4_lock); 12886 dt = rp->r_deleg_type; 12887 mutex_exit(&rp->r_statev4_lock); 12888 12889 if (dt != OPEN_DELEGATE_NONE) { 12890 nfs4_open_owner_t *oop; 12891 12892 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 12893 if (!oop) { 12894 ep->stat = NFS4ERR_IO; 12895 return; 12896 } 12897 /* returns with 'os_sync_lock' held */ 12898 osp = find_open_stream(oop, rp); 12899 if (!osp) { 12900 open_owner_rele(oop); 12901 ep->stat = NFS4ERR_IO; 12902 return; 12903 } 12904 12905 if (osp->os_failed_reopen) { 12906 NFS4_DEBUG((nfs4_open_stream_debug || 12907 nfs4_client_lock_debug), (CE_NOTE, 12908 "nfs4frlock_check_deleg: os_failed_reopen set " 12909 "for osp %p, cr %p, rp %s", (void *)osp, 12910 (void *)cr, rnode4info(rp))); 12911 mutex_exit(&osp->os_sync_lock); 12912 open_stream_rele(osp, rp); 12913 open_owner_rele(oop); 12914 ep->stat = NFS4ERR_IO; 12915 return; 12916 } 12917 12918 /* 12919 * Determine whether a reopen is needed. If this 12920 * is a delegation open stream, then send the open 12921 * to the server to give visibility to the open owner. 12922 * Even if it isn't a delegation open stream, we need 12923 * to check if the previous open CLAIM_DELEGATE_CUR 12924 * was sufficient. 12925 */ 12926 12927 reopen_needed = osp->os_delegation || 12928 ((lt == F_RDLCK && 12929 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 12930 (lt == F_WRLCK && 12931 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 12932 12933 mutex_exit(&osp->os_sync_lock); 12934 open_owner_rele(oop); 12935 12936 if (reopen_needed) { 12937 /* 12938 * Always use CLAIM_PREVIOUS after server reboot. 12939 * The server will reject CLAIM_DELEGATE_CUR if 12940 * it is used during the grace period. 12941 */ 12942 mutex_enter(&mi->mi_lock); 12943 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 12944 oclaim = CLAIM_PREVIOUS; 12945 force = TRUE; 12946 } else { 12947 oclaim = CLAIM_DELEGATE_CUR; 12948 force = FALSE; 12949 } 12950 mutex_exit(&mi->mi_lock); 12951 12952 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 12953 if (ep->error == EAGAIN) { 12954 nfs4_error_zinit(ep); 12955 ep->stat = NFS4ERR_DELAY; 12956 } 12957 } 12958 open_stream_rele(osp, rp); 12959 osp = NULL; 12960 } 12961 } 12962 12963 /* 12964 * Setup the LOCKU4 arguments. 12965 * Returns errors via the nfs4_error_t. 12966 * NFS4_OK no problems. *go_otwp is TRUE if call should go 12967 * over-the-wire. The caller must release the 12968 * reference on *lopp. 12969 * NFS4ERR_DELAY caller should retry (like recovery retry) 12970 * (other) unrecoverable error. 12971 */ 12972 static void 12973 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 12974 LOCKU4args **locku_argsp, flock64_t *flk, 12975 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 12976 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 12977 bool_t *skip_get_err, bool_t *go_otwp) 12978 { 12979 nfs4_lock_owner_t *lop = NULL; 12980 LOCKU4args *locku_args; 12981 pid_t pid; 12982 bool_t is_spec = FALSE; 12983 rnode4_t *rp = VTOR4(vp); 12984 12985 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 12986 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 12987 12988 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 12989 if (ep->error || ep->stat) 12990 return; 12991 12992 argop->argop = OP_LOCKU; 12993 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 12994 argsp->ctag = TAG_LOCKU_REINSTATE; 12995 else 12996 argsp->ctag = TAG_LOCKU; 12997 locku_args = &argop->nfs_argop4_u.oplocku; 12998 *locku_argsp = locku_args; 12999 13000 /* 13001 * XXX what should locku_args->locktype be? 13002 * setting to ALWAYS be READ_LT so at least 13003 * it is a valid locktype. 13004 */ 13005 13006 locku_args->locktype = READ_LT; 13007 13008 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13009 flk->l_pid; 13010 13011 /* 13012 * Get the lock owner stateid. If no lock owner 13013 * exists, return success. 13014 */ 13015 lop = find_lock_owner(rp, pid, LOWN_ANY); 13016 *lopp = lop; 13017 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13018 is_spec = TRUE; 13019 if (!lop || is_spec) { 13020 /* 13021 * No lock owner so no locks to unlock. 13022 * Return success. If there was a failed 13023 * reclaim earlier, the lock might still be 13024 * registered with the local locking code, 13025 * so notify it of the unlock. 13026 * 13027 * If the lockowner is using a special stateid, 13028 * then the original lock request (that created 13029 * this lockowner) was never successful, so we 13030 * have no lock to undo OTW. 13031 */ 13032 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13033 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13034 "(%ld) so return success", (long)pid)); 13035 13036 if (ctype == NFS4_LCK_CTYPE_NORM) 13037 flk->l_pid = curproc->p_pid; 13038 nfs4_register_lock_locally(vp, flk, flag, offset); 13039 /* 13040 * Release our hold and NULL out so final_cleanup 13041 * doesn't try to end a lock seqid sync we 13042 * never started. 13043 */ 13044 if (is_spec) { 13045 lock_owner_rele(lop); 13046 *lopp = NULL; 13047 } 13048 *skip_get_err = TRUE; 13049 *go_otwp = FALSE; 13050 return; 13051 } 13052 13053 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13054 if (ep->error == EAGAIN) { 13055 lock_owner_rele(lop); 13056 *lopp = NULL; 13057 return; 13058 } 13059 13060 mutex_enter(&lop->lo_lock); 13061 locku_args->lock_stateid = lop->lock_stateid; 13062 mutex_exit(&lop->lo_lock); 13063 locku_args->seqid = lop->lock_seqid + 1; 13064 13065 /* leave the ref count on lop, rele after RPC call */ 13066 13067 locku_args->offset = flk->l_start; 13068 locku_args->length = flk->l_len; 13069 if (flk->l_len == 0) 13070 locku_args->length = ~locku_args->length; 13071 13072 *go_otwp = TRUE; 13073 } 13074 13075 /* 13076 * Setup the LOCK4 arguments. 13077 * 13078 * Returns errors via the nfs4_error_t. 13079 * NFS4_OK no problems 13080 * NFS4ERR_DELAY caller should retry (like recovery retry) 13081 * (other) unrecoverable error 13082 */ 13083 static void 13084 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13085 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13086 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13087 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13088 { 13089 LOCK4args *lock_args; 13090 nfs4_open_owner_t *oop = NULL; 13091 nfs4_open_stream_t *osp = NULL; 13092 nfs4_lock_owner_t *lop = NULL; 13093 pid_t pid; 13094 rnode4_t *rp = VTOR4(vp); 13095 13096 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13097 13098 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13099 if (ep->error || ep->stat != NFS4_OK) 13100 return; 13101 13102 argop->argop = OP_LOCK; 13103 if (ctype == NFS4_LCK_CTYPE_NORM) 13104 argsp->ctag = TAG_LOCK; 13105 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13106 argsp->ctag = TAG_RELOCK; 13107 else 13108 argsp->ctag = TAG_LOCK_REINSTATE; 13109 lock_args = &argop->nfs_argop4_u.oplock; 13110 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13111 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13112 /* 13113 * Get the lock owner. If no lock owner exists, 13114 * create a 'temporary' one and grab the open seqid 13115 * synchronization (which puts a hold on the open 13116 * owner and open stream). 13117 * This also grabs the lock seqid synchronization. 13118 */ 13119 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13120 ep->stat = 13121 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13122 13123 if (ep->stat != NFS4_OK) 13124 goto out; 13125 13126 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13127 &lock_args->locker); 13128 13129 lock_args->offset = flk->l_start; 13130 lock_args->length = flk->l_len; 13131 if (flk->l_len == 0) 13132 lock_args->length = ~lock_args->length; 13133 *lock_argsp = lock_args; 13134 out: 13135 *oopp = oop; 13136 *ospp = osp; 13137 *lopp = lop; 13138 } 13139 13140 /* 13141 * After we get the reply from the server, record the proper information 13142 * for possible resend lock requests. 13143 * 13144 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13145 */ 13146 static void 13147 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13148 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13149 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13150 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13151 { 13152 bool_t unlock = (flk->l_type == F_UNLCK); 13153 13154 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13155 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13156 ctype == NFS4_LCK_CTYPE_REINSTATE); 13157 13158 if (error != 0 && !unlock) { 13159 NFS4_DEBUG((nfs4_lost_rqst_debug || 13160 nfs4_client_lock_debug), (CE_NOTE, 13161 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13162 " for lop %p", (void *)lop)); 13163 ASSERT(lop != NULL); 13164 mutex_enter(&lop->lo_lock); 13165 lop->lo_pending_rqsts = 1; 13166 mutex_exit(&lop->lo_lock); 13167 } 13168 13169 lost_rqstp->lr_putfirst = FALSE; 13170 lost_rqstp->lr_op = 0; 13171 13172 /* 13173 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13174 * recovery purposes so that the lock request that was sent 13175 * can be saved and re-issued later. Ditto for EIO from a forced 13176 * unmount. This is done to have the client's local locking state 13177 * match the v4 server's state; that is, the request was 13178 * potentially received and accepted by the server but the client 13179 * thinks it was not. 13180 */ 13181 if (error == ETIMEDOUT || error == EINTR || 13182 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13183 NFS4_DEBUG((nfs4_lost_rqst_debug || 13184 nfs4_client_lock_debug), (CE_NOTE, 13185 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13186 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13187 (void *)lop, (void *)oop, (void *)osp)); 13188 if (unlock) 13189 lost_rqstp->lr_op = OP_LOCKU; 13190 else { 13191 lost_rqstp->lr_op = OP_LOCK; 13192 lost_rqstp->lr_locktype = locktype; 13193 } 13194 /* 13195 * Objects are held and rele'd via the recovery code. 13196 * See nfs4_save_lost_rqst. 13197 */ 13198 lost_rqstp->lr_vp = vp; 13199 lost_rqstp->lr_dvp = NULL; 13200 lost_rqstp->lr_oop = oop; 13201 lost_rqstp->lr_osp = osp; 13202 lost_rqstp->lr_lop = lop; 13203 lost_rqstp->lr_cr = cr; 13204 switch (ctype) { 13205 case NFS4_LCK_CTYPE_NORM: 13206 flk->l_pid = ttoproc(curthread)->p_pid; 13207 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13208 break; 13209 case NFS4_LCK_CTYPE_REINSTATE: 13210 lost_rqstp->lr_putfirst = TRUE; 13211 lost_rqstp->lr_ctype = ctype; 13212 break; 13213 default: 13214 break; 13215 } 13216 lost_rqstp->lr_flk = flk; 13217 } 13218 } 13219 13220 /* 13221 * Update lop's seqid. Also update the seqid stored in a resend request, 13222 * if any. (Some recovery errors increment the seqid, and we may have to 13223 * send the resend request again.) 13224 */ 13225 13226 static void 13227 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13228 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13229 { 13230 if (lock_args) { 13231 if (lock_args->locker.new_lock_owner == TRUE) 13232 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13233 else { 13234 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13235 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13236 } 13237 } else if (locku_args) { 13238 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13239 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13240 } 13241 } 13242 13243 /* 13244 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13245 * COMPOUND4 args/res for calls that need to retry. 13246 * Switches the *cred_otwp to base_cr. 13247 */ 13248 static void 13249 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13250 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13251 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13252 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13253 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13254 { 13255 nfs4_open_owner_t *oop = *oopp; 13256 nfs4_open_stream_t *osp = *ospp; 13257 nfs4_lock_owner_t *lop = *lopp; 13258 nfs_argop4 *argop = (*argspp)->array; 13259 13260 if (*did_start_fop) { 13261 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13262 needrecov); 13263 *did_start_fop = FALSE; 13264 } 13265 ASSERT((*argspp)->array_len == 2); 13266 if (argop[1].argop == OP_LOCK) 13267 nfs4args_lock_free(&argop[1]); 13268 else if (argop[1].argop == OP_LOCKT) 13269 nfs4args_lockt_free(&argop[1]); 13270 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13271 if (!error) 13272 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13273 *argspp = NULL; 13274 *respp = NULL; 13275 13276 if (lop) { 13277 nfs4_end_lock_seqid_sync(lop); 13278 lock_owner_rele(lop); 13279 *lopp = NULL; 13280 } 13281 13282 /* need to free up the reference on osp for lock args */ 13283 if (osp != NULL) { 13284 open_stream_rele(osp, VTOR4(vp)); 13285 *ospp = NULL; 13286 } 13287 13288 /* need to free up the reference on oop for lock args */ 13289 if (oop != NULL) { 13290 nfs4_end_open_seqid_sync(oop); 13291 open_owner_rele(oop); 13292 *oopp = NULL; 13293 } 13294 13295 crfree(*cred_otwp); 13296 *cred_otwp = base_cr; 13297 crhold(*cred_otwp); 13298 } 13299 13300 /* 13301 * Function to process the client's recovery for nfs4frlock. 13302 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13303 * 13304 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13305 * COMPOUND4 args/res for calls that need to retry. 13306 * 13307 * Note: the rp's r_lkserlock is *not* dropped during this path. 13308 */ 13309 static bool_t 13310 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13311 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13312 LOCK4args *lock_args, LOCKU4args *locku_args, 13313 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13314 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13315 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13316 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13317 { 13318 nfs4_open_owner_t *oop = *oopp; 13319 nfs4_open_stream_t *osp = *ospp; 13320 nfs4_lock_owner_t *lop = *lopp; 13321 13322 bool_t abort, retry; 13323 13324 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13325 ASSERT((*argspp) != NULL); 13326 ASSERT((*respp) != NULL); 13327 if (lock_args || locku_args) 13328 ASSERT(lop != NULL); 13329 13330 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13331 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13332 13333 retry = TRUE; 13334 abort = FALSE; 13335 if (needrecov) { 13336 nfs4_bseqid_entry_t *bsep = NULL; 13337 nfs_opnum4 op; 13338 13339 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13340 13341 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13342 seqid4 seqid; 13343 13344 if (lock_args) { 13345 if (lock_args->locker.new_lock_owner == TRUE) 13346 seqid = lock_args->locker.locker4_u. 13347 open_owner.open_seqid; 13348 else 13349 seqid = lock_args->locker.locker4_u. 13350 lock_owner.lock_seqid; 13351 } else if (locku_args) { 13352 seqid = locku_args->seqid; 13353 } else { 13354 seqid = 0; 13355 } 13356 13357 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13358 flk->l_pid, (*argspp)->ctag, seqid); 13359 } 13360 13361 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13362 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13363 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13364 NULL, op, bsep); 13365 13366 if (bsep) 13367 kmem_free(bsep, sizeof (*bsep)); 13368 } 13369 13370 /* 13371 * Return that we do not want to retry the request for 3 cases: 13372 * 1. If we received EINTR or are bailing out because of a forced 13373 * unmount, we came into this code path just for the sake of 13374 * initiating recovery, we now need to return the error. 13375 * 2. If we have aborted recovery. 13376 * 3. We received NFS4ERR_BAD_SEQID. 13377 */ 13378 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13379 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13380 retry = FALSE; 13381 13382 if (*did_start_fop == TRUE) { 13383 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13384 needrecov); 13385 *did_start_fop = FALSE; 13386 } 13387 13388 if (retry == TRUE) { 13389 nfs_argop4 *argop; 13390 13391 argop = (*argspp)->array; 13392 ASSERT((*argspp)->array_len == 2); 13393 13394 if (argop[1].argop == OP_LOCK) 13395 nfs4args_lock_free(&argop[1]); 13396 else if (argop[1].argop == OP_LOCKT) 13397 nfs4args_lockt_free(&argop[1]); 13398 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13399 if (!ep->error) 13400 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13401 *respp = NULL; 13402 *argspp = NULL; 13403 } 13404 13405 if (lop != NULL) { 13406 nfs4_end_lock_seqid_sync(lop); 13407 lock_owner_rele(lop); 13408 } 13409 13410 *lopp = NULL; 13411 13412 /* need to free up the reference on osp for lock args */ 13413 if (osp != NULL) { 13414 open_stream_rele(osp, rp); 13415 *ospp = NULL; 13416 } 13417 13418 /* need to free up the reference on oop for lock args */ 13419 if (oop != NULL) { 13420 nfs4_end_open_seqid_sync(oop); 13421 open_owner_rele(oop); 13422 *oopp = NULL; 13423 } 13424 13425 return (retry); 13426 } 13427 13428 /* 13429 * Handles the succesful reply from the server for nfs4frlock. 13430 */ 13431 static void 13432 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13433 vnode_t *vp, int flag, u_offset_t offset, 13434 nfs4_lost_rqst_t *resend_rqstp) 13435 { 13436 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13437 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13438 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13439 if (ctype == NFS4_LCK_CTYPE_NORM) { 13440 flk->l_pid = ttoproc(curthread)->p_pid; 13441 /* 13442 * We do not register lost locks locally in 13443 * the 'resend' case since the user/application 13444 * doesn't think we have the lock. 13445 */ 13446 ASSERT(!resend_rqstp); 13447 nfs4_register_lock_locally(vp, flk, flag, offset); 13448 } 13449 } 13450 } 13451 13452 /* 13453 * Handle the DENIED reply from the server for nfs4frlock. 13454 * Returns TRUE if we should retry the request; FALSE otherwise. 13455 * 13456 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13457 * COMPOUND4 args/res for calls that need to retry. Can also 13458 * drop and regrab the r_lkserlock. 13459 */ 13460 static bool_t 13461 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13462 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13463 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13464 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13465 nfs4_recov_state_t *recov_statep, int needrecov, 13466 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13467 clock_t *tick_delayp, short *whencep, int *errorp, 13468 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13469 bool_t *skip_get_err) 13470 { 13471 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13472 13473 if (lock_args) { 13474 nfs4_open_owner_t *oop = *oopp; 13475 nfs4_open_stream_t *osp = *ospp; 13476 nfs4_lock_owner_t *lop = *lopp; 13477 int intr; 13478 13479 /* 13480 * Blocking lock needs to sleep and retry from the request. 13481 * 13482 * Do not block and wait for 'resend' or 'reinstate' 13483 * lock requests, just return the error. 13484 * 13485 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13486 */ 13487 if (cmd == F_SETLKW) { 13488 rnode4_t *rp = VTOR4(vp); 13489 nfs_argop4 *argop = (*argspp)->array; 13490 13491 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13492 13493 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13494 recov_statep, needrecov); 13495 *did_start_fop = FALSE; 13496 ASSERT((*argspp)->array_len == 2); 13497 if (argop[1].argop == OP_LOCK) 13498 nfs4args_lock_free(&argop[1]); 13499 else if (argop[1].argop == OP_LOCKT) 13500 nfs4args_lockt_free(&argop[1]); 13501 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13502 if (*respp) 13503 (void) xdr_free(xdr_COMPOUND4res_clnt, 13504 (caddr_t)*respp); 13505 *argspp = NULL; 13506 *respp = NULL; 13507 nfs4_end_lock_seqid_sync(lop); 13508 lock_owner_rele(lop); 13509 *lopp = NULL; 13510 if (osp != NULL) { 13511 open_stream_rele(osp, rp); 13512 *ospp = NULL; 13513 } 13514 if (oop != NULL) { 13515 nfs4_end_open_seqid_sync(oop); 13516 open_owner_rele(oop); 13517 *oopp = NULL; 13518 } 13519 13520 nfs_rw_exit(&rp->r_lkserlock); 13521 13522 intr = nfs4_block_and_wait(tick_delayp, rp); 13523 13524 if (intr) { 13525 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13526 RW_WRITER, FALSE); 13527 *errorp = EINTR; 13528 return (FALSE); 13529 } 13530 13531 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13532 RW_WRITER, FALSE); 13533 13534 /* 13535 * Make sure we are still safe to lock with 13536 * regards to mmapping. 13537 */ 13538 if (!nfs4_safelock(vp, flk, cr)) { 13539 *errorp = EAGAIN; 13540 return (FALSE); 13541 } 13542 13543 return (TRUE); 13544 } 13545 if (ctype == NFS4_LCK_CTYPE_NORM) 13546 *errorp = EAGAIN; 13547 *skip_get_err = TRUE; 13548 flk->l_whence = 0; 13549 *whencep = 0; 13550 return (FALSE); 13551 } else if (lockt_args) { 13552 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13553 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13554 13555 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13556 flk, lockt_args); 13557 13558 /* according to NLM code */ 13559 *errorp = 0; 13560 *whencep = 0; 13561 *skip_get_err = TRUE; 13562 return (FALSE); 13563 } 13564 return (FALSE); 13565 } 13566 13567 /* 13568 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13569 */ 13570 static void 13571 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13572 { 13573 switch (resp->status) { 13574 case NFS4ERR_ACCESS: 13575 case NFS4ERR_ADMIN_REVOKED: 13576 case NFS4ERR_BADHANDLE: 13577 case NFS4ERR_BAD_RANGE: 13578 case NFS4ERR_BAD_SEQID: 13579 case NFS4ERR_BAD_STATEID: 13580 case NFS4ERR_BADXDR: 13581 case NFS4ERR_DEADLOCK: 13582 case NFS4ERR_DELAY: 13583 case NFS4ERR_EXPIRED: 13584 case NFS4ERR_FHEXPIRED: 13585 case NFS4ERR_GRACE: 13586 case NFS4ERR_INVAL: 13587 case NFS4ERR_ISDIR: 13588 case NFS4ERR_LEASE_MOVED: 13589 case NFS4ERR_LOCK_NOTSUPP: 13590 case NFS4ERR_LOCK_RANGE: 13591 case NFS4ERR_MOVED: 13592 case NFS4ERR_NOFILEHANDLE: 13593 case NFS4ERR_NO_GRACE: 13594 case NFS4ERR_OLD_STATEID: 13595 case NFS4ERR_OPENMODE: 13596 case NFS4ERR_RECLAIM_BAD: 13597 case NFS4ERR_RECLAIM_CONFLICT: 13598 case NFS4ERR_RESOURCE: 13599 case NFS4ERR_SERVERFAULT: 13600 case NFS4ERR_STALE: 13601 case NFS4ERR_STALE_CLIENTID: 13602 case NFS4ERR_STALE_STATEID: 13603 return; 13604 default: 13605 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13606 "nfs4frlock_results_default: got unrecognizable " 13607 "res.status %d", resp->status)); 13608 *errorp = NFS4ERR_INVAL; 13609 } 13610 } 13611 13612 /* 13613 * The lock request was successful, so update the client's state. 13614 */ 13615 static void 13616 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13617 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13618 vnode_t *vp, flock64_t *flk, cred_t *cr, 13619 nfs4_lost_rqst_t *resend_rqstp) 13620 { 13621 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13622 13623 if (lock_args) { 13624 LOCK4res *lock_res; 13625 13626 lock_res = &resop->nfs_resop4_u.oplock; 13627 /* update the stateid with server's response */ 13628 13629 if (lock_args->locker.new_lock_owner == TRUE) { 13630 mutex_enter(&lop->lo_lock); 13631 lop->lo_just_created = NFS4_PERM_CREATED; 13632 mutex_exit(&lop->lo_lock); 13633 } 13634 13635 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13636 13637 /* 13638 * If the lock was the result of a resending a lost 13639 * request, we've synched up the stateid and seqid 13640 * with the server, but now the server might be out of sync 13641 * with what the application thinks it has for locks. 13642 * Clean that up here. It's unclear whether we should do 13643 * this even if the filesystem has been forcibly unmounted. 13644 * For most servers, it's probably wasted effort, but 13645 * RFC3530 lets servers require that unlocks exactly match 13646 * the locks that are held. 13647 */ 13648 if (resend_rqstp != NULL && 13649 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 13650 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 13651 } else { 13652 flk->l_whence = 0; 13653 } 13654 } else if (locku_args) { 13655 LOCKU4res *locku_res; 13656 13657 locku_res = &resop->nfs_resop4_u.oplocku; 13658 13659 /* Update the stateid with the server's response */ 13660 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 13661 } else if (lockt_args) { 13662 /* Switch the lock type to express success, see fcntl */ 13663 flk->l_type = F_UNLCK; 13664 flk->l_whence = 0; 13665 } 13666 } 13667 13668 /* 13669 * Do final cleanup before exiting nfs4frlock. 13670 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13671 * COMPOUND4 args/res for calls that haven't already. 13672 */ 13673 static void 13674 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 13675 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 13676 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 13677 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13678 short whence, u_offset_t offset, struct lm_sysid *ls, 13679 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 13680 bool_t did_start_fop, bool_t skip_get_err, 13681 cred_t *cred_otw, cred_t *cred) 13682 { 13683 mntinfo4_t *mi = VTOMI4(vp); 13684 rnode4_t *rp = VTOR4(vp); 13685 int error = *errorp; 13686 nfs_argop4 *argop; 13687 13688 ASSERT(curproc->p_zone == mi->mi_zone); 13689 /* 13690 * The client recovery code wants the raw status information, 13691 * so don't map the NFS status code to an errno value for 13692 * non-normal call types. 13693 */ 13694 if (ctype == NFS4_LCK_CTYPE_NORM) { 13695 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 13696 *errorp = geterrno4(resp->status); 13697 if (did_start_fop == TRUE) 13698 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 13699 needrecov); 13700 13701 if (!error && resp && resp->status == NFS4_OK) { 13702 /* 13703 * We've established a new lock on the server, so invalidate 13704 * the pages associated with the vnode to get the most up to 13705 * date pages from the server after acquiring the lock. We 13706 * want to be sure that the read operation gets the newest data. 13707 * N.B. 13708 * We used to do this in nfs4frlock_results_ok but that doesn't 13709 * work since VOP_PUTPAGE can call nfs4_commit which calls 13710 * nfs4_start_fop. We flush the pages below after calling 13711 * nfs4_end_fop above 13712 */ 13713 int error; 13714 13715 error = VOP_PUTPAGE(vp, (u_offset_t)0, 13716 0, B_INVAL, cred); 13717 13718 if (error && (error == ENOSPC || error == EDQUOT)) { 13719 rnode4_t *rp = VTOR4(vp); 13720 13721 mutex_enter(&rp->r_statelock); 13722 if (!rp->r_error) 13723 rp->r_error = error; 13724 mutex_exit(&rp->r_statelock); 13725 } 13726 } 13727 } 13728 if (argsp) { 13729 ASSERT(argsp->array_len == 2); 13730 argop = argsp->array; 13731 if (argop[1].argop == OP_LOCK) 13732 nfs4args_lock_free(&argop[1]); 13733 else if (argop[1].argop == OP_LOCKT) 13734 nfs4args_lockt_free(&argop[1]); 13735 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13736 if (resp) 13737 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 13738 } 13739 13740 /* free the reference on the lock owner */ 13741 if (lop != NULL) { 13742 nfs4_end_lock_seqid_sync(lop); 13743 lock_owner_rele(lop); 13744 } 13745 13746 /* need to free up the reference on osp for lock args */ 13747 if (osp != NULL) 13748 open_stream_rele(osp, rp); 13749 13750 /* need to free up the reference on oop for lock args */ 13751 if (oop != NULL) { 13752 nfs4_end_open_seqid_sync(oop); 13753 open_owner_rele(oop); 13754 } 13755 13756 (void) convoff(vp, flk, whence, offset); 13757 13758 lm_rel_sysid(ls); 13759 13760 /* 13761 * Record debug information in the event we get EINVAL. 13762 */ 13763 mutex_enter(&mi->mi_lock); 13764 if (*errorp == EINVAL && (lock_args || locku_args) && 13765 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 13766 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 13767 zcmn_err(getzoneid(), CE_NOTE, 13768 "%s operation failed with " 13769 "EINVAL probably since the server, %s," 13770 " doesn't support POSIX style locking", 13771 lock_args ? "LOCK" : "LOCKU", 13772 mi->mi_curr_serv->sv_hostname); 13773 mi->mi_flags |= MI4_LOCK_DEBUG; 13774 } 13775 } 13776 mutex_exit(&mi->mi_lock); 13777 13778 if (cred_otw) 13779 crfree(cred_otw); 13780 } 13781 13782 /* 13783 * This calls the server and the local locking code. 13784 * 13785 * Client locks are registerred locally by oring the sysid with 13786 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 13787 * We need to distinguish between the two to avoid collision in case one 13788 * machine is used as both client and server. 13789 * 13790 * Blocking lock requests will continually retry to acquire the lock 13791 * forever. 13792 * 13793 * The ctype is defined as follows: 13794 * NFS4_LCK_CTYPE_NORM: normal lock request. 13795 * 13796 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 13797 * recovery, get the pid from flk instead of curproc, and don't reregister 13798 * the lock locally. 13799 * 13800 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 13801 * that we will use the information passed in via resend_rqstp to setup the 13802 * lock/locku request. This resend is the exact same request as the 'lost 13803 * lock', and is initiated by the recovery framework. A successful resend 13804 * request can initiate one or more reinstate requests. 13805 * 13806 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 13807 * does not trigger additional reinstate requests. This lock call type is 13808 * set for setting the v4 server's locking state back to match what the 13809 * client's local locking state is in the event of a received 'lost lock'. 13810 * 13811 * Errors are returned via the nfs4_error_t parameter. 13812 */ 13813 void 13814 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 13815 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 13816 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 13817 { 13818 COMPOUND4args_clnt args, *argsp = NULL; 13819 COMPOUND4res_clnt res, *resp = NULL; 13820 nfs_argop4 *argop; 13821 nfs_resop4 *resop; 13822 rnode4_t *rp; 13823 int doqueue = 1; 13824 clock_t tick_delay; /* delay in clock ticks */ 13825 struct lm_sysid *ls; 13826 LOCK4args *lock_args = NULL; 13827 LOCKU4args *locku_args = NULL; 13828 LOCKT4args *lockt_args = NULL; 13829 nfs4_open_owner_t *oop = NULL; 13830 nfs4_open_stream_t *osp = NULL; 13831 nfs4_lock_owner_t *lop = NULL; 13832 bool_t needrecov = FALSE; 13833 nfs4_recov_state_t recov_state; 13834 short whence; 13835 nfs4_op_hint_t op_hint; 13836 nfs4_lost_rqst_t lost_rqst; 13837 bool_t retry = FALSE; 13838 bool_t did_start_fop = FALSE; 13839 bool_t skip_get_err = FALSE; 13840 cred_t *cred_otw = NULL; 13841 bool_t recovonly; /* just queue request */ 13842 int frc_no_reclaim = 0; 13843 #ifdef DEBUG 13844 char *name; 13845 #endif 13846 13847 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 13848 13849 #ifdef DEBUG 13850 name = fn_name(VTOSV(vp)->sv_name); 13851 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 13852 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 13853 "length %"PRIu64", pid %d, sysid %d, call type %s, " 13854 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 13855 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 13856 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 13857 resend_rqstp ? "TRUE" : "FALSE")); 13858 kmem_free(name, MAXNAMELEN); 13859 #endif 13860 13861 nfs4_error_zinit(ep); 13862 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 13863 if (ep->error) 13864 return; 13865 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 13866 if (ep->error) 13867 return; 13868 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 13869 vp, cr, &cred_otw); 13870 13871 recov_retry: 13872 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 13873 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 13874 rp = VTOR4(vp); 13875 13876 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 13877 &did_start_fop, &recovonly); 13878 13879 if (ep->error) 13880 goto out; 13881 13882 if (recovonly) { 13883 /* 13884 * Leave the request for the recovery system to deal with. 13885 */ 13886 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13887 ASSERT(cmd != F_GETLK); 13888 ASSERT(flk->l_type == F_UNLCK); 13889 13890 nfs4_error_init(ep, EINTR); 13891 needrecov = TRUE; 13892 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 13893 if (lop != NULL) { 13894 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 13895 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 13896 (void) nfs4_start_recovery(ep, 13897 VTOMI4(vp), vp, NULL, NULL, 13898 (lost_rqst.lr_op == OP_LOCK || 13899 lost_rqst.lr_op == OP_LOCKU) ? 13900 &lost_rqst : NULL, OP_LOCKU, NULL); 13901 lock_owner_rele(lop); 13902 lop = NULL; 13903 } 13904 flk->l_pid = curproc->p_pid; 13905 nfs4_register_lock_locally(vp, flk, flag, offset); 13906 goto out; 13907 } 13908 13909 /* putfh directory fh */ 13910 argop[0].argop = OP_CPUTFH; 13911 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 13912 13913 /* 13914 * Set up the over-the-wire arguments and get references to the 13915 * open owner, etc. 13916 */ 13917 13918 if (ctype == NFS4_LCK_CTYPE_RESEND || 13919 ctype == NFS4_LCK_CTYPE_REINSTATE) { 13920 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 13921 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 13922 } else { 13923 bool_t go_otw = TRUE; 13924 13925 ASSERT(resend_rqstp == NULL); 13926 13927 switch (cmd) { 13928 case F_GETLK: 13929 case F_O_GETLK: 13930 nfs4frlock_setup_lockt_args(ctype, &argop[1], 13931 &lockt_args, argsp, flk, rp); 13932 break; 13933 case F_SETLKW: 13934 case F_SETLK: 13935 if (flk->l_type == F_UNLCK) 13936 nfs4frlock_setup_locku_args(ctype, 13937 &argop[1], &locku_args, flk, 13938 &lop, ep, argsp, 13939 vp, flag, offset, cr, 13940 &skip_get_err, &go_otw); 13941 else 13942 nfs4frlock_setup_lock_args(ctype, 13943 &lock_args, &oop, &osp, &lop, &argop[1], 13944 argsp, flk, cmd, vp, cr, ep); 13945 13946 if (ep->error) 13947 goto out; 13948 13949 switch (ep->stat) { 13950 case NFS4_OK: 13951 break; 13952 case NFS4ERR_DELAY: 13953 /* recov thread never gets this error */ 13954 ASSERT(resend_rqstp == NULL); 13955 ASSERT(did_start_fop); 13956 13957 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13958 &recov_state, TRUE); 13959 did_start_fop = FALSE; 13960 if (argop[1].argop == OP_LOCK) 13961 nfs4args_lock_free(&argop[1]); 13962 else if (argop[1].argop == OP_LOCKT) 13963 nfs4args_lockt_free(&argop[1]); 13964 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13965 argsp = NULL; 13966 goto recov_retry; 13967 default: 13968 ep->error = EIO; 13969 goto out; 13970 } 13971 break; 13972 default: 13973 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13974 "nfs4_frlock: invalid cmd %d", cmd)); 13975 ep->error = EINVAL; 13976 goto out; 13977 } 13978 13979 if (!go_otw) 13980 goto out; 13981 } 13982 13983 /* XXX should we use the local reclock as a cache ? */ 13984 /* 13985 * Unregister the lock with the local locking code before 13986 * contacting the server. This avoids a potential race where 13987 * another process gets notified that it has been granted a lock 13988 * before we can unregister ourselves locally. 13989 */ 13990 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 13991 if (ctype == NFS4_LCK_CTYPE_NORM) 13992 flk->l_pid = ttoproc(curthread)->p_pid; 13993 nfs4_register_lock_locally(vp, flk, flag, offset); 13994 } 13995 13996 /* 13997 * Send the server the lock request. Continually loop with a delay 13998 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 13999 */ 14000 resp = &res; 14001 14002 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14003 (CE_NOTE, 14004 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14005 rnode4info(rp))); 14006 14007 if (lock_args && frc_no_reclaim) { 14008 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14009 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14010 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14011 lock_args->reclaim = FALSE; 14012 if (did_reclaimp) 14013 *did_reclaimp = 0; 14014 } 14015 14016 /* 14017 * Do the OTW call. 14018 */ 14019 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14020 14021 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14022 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14023 14024 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14025 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14026 "nfs4frlock: needrecov %d", needrecov)); 14027 14028 if (ep->error != 0 && !needrecov && ep->error != EACCES) 14029 goto out; 14030 14031 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14032 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14033 args.ctag); 14034 14035 if ((ep->error == EACCES || 14036 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14037 cred_otw != cr) { 14038 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14039 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14040 cr, &cred_otw); 14041 goto recov_retry; 14042 } 14043 14044 if (needrecov) { 14045 /* 14046 * LOCKT requests don't need to recover from lost 14047 * requests since they don't create/modify state. 14048 */ 14049 if ((ep->error == EINTR || 14050 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14051 lockt_args) 14052 goto out; 14053 /* 14054 * Do not attempt recovery for requests initiated by 14055 * the recovery framework. Let the framework redrive them. 14056 */ 14057 if (ctype != NFS4_LCK_CTYPE_NORM) 14058 goto out; 14059 else { 14060 ASSERT(resend_rqstp == NULL); 14061 } 14062 14063 nfs4frlock_save_lost_rqst(ctype, ep->error, 14064 flk_to_locktype(cmd, flk->l_type), 14065 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14066 14067 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14068 &resp, lock_args, locku_args, &oop, &osp, &lop, 14069 rp, vp, &recov_state, op_hint, &did_start_fop, 14070 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14071 14072 if (retry) { 14073 ASSERT(oop == NULL); 14074 ASSERT(osp == NULL); 14075 ASSERT(lop == NULL); 14076 goto recov_retry; 14077 } 14078 goto out; 14079 } 14080 14081 /* 14082 * Process the reply. 14083 */ 14084 switch (resp->status) { 14085 case NFS4_OK: 14086 resop = &resp->array[1]; 14087 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14088 resend_rqstp); 14089 /* 14090 * Have a successful lock operation, now update state. 14091 */ 14092 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14093 resop, lop, vp, flk, cr, resend_rqstp); 14094 break; 14095 14096 case NFS4ERR_DENIED: 14097 resop = &resp->array[1]; 14098 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14099 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14100 &recov_state, needrecov, &argsp, &resp, 14101 &tick_delay, &whence, &ep->error, resop, cr, 14102 &did_start_fop, &skip_get_err); 14103 14104 if (retry) { 14105 ASSERT(oop == NULL); 14106 ASSERT(osp == NULL); 14107 ASSERT(lop == NULL); 14108 goto recov_retry; 14109 } 14110 break; 14111 /* 14112 * If the server won't let us reclaim, fall-back to trying to lock 14113 * the file from scratch. Code elsewhere will check the changeinfo 14114 * to ensure the file hasn't been changed. 14115 */ 14116 case NFS4ERR_NO_GRACE: 14117 if (lock_args && lock_args->reclaim == TRUE) { 14118 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14119 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14120 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14121 frc_no_reclaim = 1; 14122 /* clean up before retrying */ 14123 needrecov = 0; 14124 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14125 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14126 &recov_state, op_hint, &did_start_fop, NULL, flk); 14127 goto recov_retry; 14128 } 14129 /* FALLTHROUGH */ 14130 14131 default: 14132 nfs4frlock_results_default(resp, &ep->error); 14133 break; 14134 } 14135 out: 14136 /* 14137 * Process and cleanup from error. Make interrupted unlock 14138 * requests look successful, since they will be handled by the 14139 * client recovery code. 14140 */ 14141 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14142 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14143 lock_args, locku_args, did_start_fop, 14144 skip_get_err, cred_otw, cr); 14145 14146 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14147 (cmd == F_SETLK || cmd == F_SETLKW)) 14148 ep->error = 0; 14149 } 14150 14151 /* 14152 * nfs4_safelock: 14153 * 14154 * Return non-zero if the given lock request can be handled without 14155 * violating the constraints on concurrent mapping and locking. 14156 */ 14157 14158 static int 14159 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14160 { 14161 rnode4_t *rp = VTOR4(vp); 14162 struct vattr va; 14163 int error; 14164 14165 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14166 ASSERT(rp->r_mapcnt >= 0); 14167 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14168 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14169 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14170 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14171 14172 if (rp->r_mapcnt == 0) 14173 return (1); /* always safe if not mapped */ 14174 14175 /* 14176 * If the file is already mapped and there are locks, then they 14177 * should be all safe locks. So adding or removing a lock is safe 14178 * as long as the new request is safe (i.e., whole-file, meaning 14179 * length and starting offset are both zero). 14180 */ 14181 14182 if (bfp->l_start != 0 || bfp->l_len != 0) { 14183 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14184 "cannot lock a memory mapped file unless locking the " 14185 "entire file: start %"PRIx64", len %"PRIx64, 14186 bfp->l_start, bfp->l_len)); 14187 return (0); 14188 } 14189 14190 /* mandatory locking and mapping don't mix */ 14191 va.va_mask = AT_MODE; 14192 error = VOP_GETATTR(vp, &va, 0, cr); 14193 if (error != 0) { 14194 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14195 "getattr error %d", error)); 14196 return (0); /* treat errors conservatively */ 14197 } 14198 if (MANDLOCK(vp, va.va_mode)) { 14199 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14200 "cannot mandatory lock and mmap a file")); 14201 return (0); 14202 } 14203 14204 return (1); 14205 } 14206 14207 14208 /* 14209 * Register the lock locally within Solaris. 14210 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14211 * recording locks locally. 14212 * 14213 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14214 * are registered locally. 14215 */ 14216 void 14217 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14218 u_offset_t offset) 14219 { 14220 int oldsysid; 14221 int error; 14222 #ifdef DEBUG 14223 char *name; 14224 #endif 14225 14226 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14227 14228 #ifdef DEBUG 14229 name = fn_name(VTOSV(vp)->sv_name); 14230 NFS4_DEBUG(nfs4_client_lock_debug, 14231 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14232 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14233 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14234 flk->l_sysid)); 14235 kmem_free(name, MAXNAMELEN); 14236 #endif 14237 14238 /* register the lock with local locking */ 14239 oldsysid = flk->l_sysid; 14240 flk->l_sysid |= LM_SYSID_CLIENT; 14241 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14242 #ifdef DEBUG 14243 if (error != 0) { 14244 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14245 "nfs4_register_lock_locally: could not register with" 14246 " local locking")); 14247 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14248 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14249 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14250 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14251 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14252 flk->l_type, flk->l_start, flk->l_len)); 14253 (void) reclock(vp, flk, 0, flag, offset, NULL); 14254 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14255 "blocked by pid %d sysid 0x%x type %d " 14256 "off 0x%" PRIx64 " len 0x%" PRIx64, 14257 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14258 flk->l_len)); 14259 } 14260 #endif 14261 flk->l_sysid = oldsysid; 14262 } 14263 14264 /* 14265 * nfs4_lockrelease: 14266 * 14267 * Release any locks on the given vnode that are held by the current 14268 * process. Also removes the lock owner (if one exists) from the rnode's 14269 * list. 14270 */ 14271 static int 14272 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14273 { 14274 flock64_t ld; 14275 int ret, error; 14276 rnode4_t *rp; 14277 nfs4_lock_owner_t *lop; 14278 nfs4_recov_state_t recov_state; 14279 mntinfo4_t *mi; 14280 bool_t possible_orphan = FALSE; 14281 bool_t recovonly; 14282 14283 ASSERT((uintptr_t)vp > KERNELBASE); 14284 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14285 14286 rp = VTOR4(vp); 14287 mi = VTOMI4(vp); 14288 14289 /* 14290 * If we have not locked anything then we can 14291 * just return since we have no work to do. 14292 */ 14293 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14294 return (0); 14295 } 14296 14297 /* 14298 * We need to comprehend that another thread may 14299 * kick off recovery and the lock_owner we have stashed 14300 * in lop might be invalid so we should NOT cache it 14301 * locally! 14302 */ 14303 recov_state.rs_flags = 0; 14304 recov_state.rs_num_retry_despite_err = 0; 14305 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14306 &recovonly); 14307 if (error) { 14308 mutex_enter(&rp->r_statelock); 14309 rp->r_flags |= R4LODANGLERS; 14310 mutex_exit(&rp->r_statelock); 14311 return (error); 14312 } 14313 14314 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14315 14316 /* 14317 * Check if the lock owner might have a lock (request was sent but 14318 * no response was received). Also check if there are any remote 14319 * locks on the file. (In theory we shouldn't have to make this 14320 * second check if there's no lock owner, but for now we'll be 14321 * conservative and do it anyway.) If either condition is true, 14322 * send an unlock for the entire file to the server. 14323 * 14324 * Note that no explicit synchronization is needed here. At worst, 14325 * flk_has_remote_locks() will return a false positive, in which case 14326 * the unlock call wastes time but doesn't harm correctness. 14327 */ 14328 14329 if (lop) { 14330 mutex_enter(&lop->lo_lock); 14331 possible_orphan = lop->lo_pending_rqsts; 14332 mutex_exit(&lop->lo_lock); 14333 lock_owner_rele(lop); 14334 } 14335 14336 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14337 14338 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14339 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14340 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14341 (void *)lop)); 14342 14343 if (possible_orphan || flk_has_remote_locks(vp)) { 14344 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14345 ld.l_whence = 0; /* unlock from start of file */ 14346 ld.l_start = 0; 14347 ld.l_len = 0; /* do entire file */ 14348 14349 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, cr); 14350 14351 if (ret != 0) { 14352 /* 14353 * If VOP_FRLOCK fails, make sure we unregister 14354 * local locks before we continue. 14355 */ 14356 ld.l_pid = ttoproc(curthread)->p_pid; 14357 nfs4_register_lock_locally(vp, &ld, flag, offset); 14358 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14359 "nfs4_lockrelease: lock release error on vp" 14360 " %p: error %d.\n", (void *)vp, ret)); 14361 } 14362 } 14363 14364 recov_state.rs_flags = 0; 14365 recov_state.rs_num_retry_despite_err = 0; 14366 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14367 &recovonly); 14368 if (error) { 14369 mutex_enter(&rp->r_statelock); 14370 rp->r_flags |= R4LODANGLERS; 14371 mutex_exit(&rp->r_statelock); 14372 return (error); 14373 } 14374 14375 /* 14376 * So, here we're going to need to retrieve the lock-owner 14377 * again (in case recovery has done a switch-a-roo) and 14378 * remove it because we can. 14379 */ 14380 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14381 14382 if (lop) { 14383 nfs4_rnode_remove_lock_owner(rp, lop); 14384 lock_owner_rele(lop); 14385 } 14386 14387 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14388 return (0); 14389 } 14390 14391 /* 14392 * Wait for 'tick_delay' clock ticks. 14393 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14394 * NOTE: lock_lease_time is in seconds. 14395 * 14396 * XXX For future improvements, should implement a waiting queue scheme. 14397 */ 14398 static int 14399 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14400 { 14401 long milliseconds_delay; 14402 time_t lock_lease_time; 14403 14404 /* wait tick_delay clock ticks or siginteruptus */ 14405 if (delay_sig(*tick_delay)) { 14406 return (EINTR); 14407 } 14408 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14409 "reissue the lock request: blocked for %ld clock ticks: %ld " 14410 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14411 14412 /* get the lease time */ 14413 lock_lease_time = r2lease_time(rp); 14414 14415 /* drv_hztousec converts ticks to microseconds */ 14416 milliseconds_delay = drv_hztousec(*tick_delay) / 1000; 14417 if (milliseconds_delay < lock_lease_time * 1000) { 14418 *tick_delay = 2 * *tick_delay; 14419 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000) 14420 *tick_delay = drv_usectohz(lock_lease_time*1000*1000); 14421 } 14422 return (0); 14423 } 14424 14425 14426 void 14427 nfs4_vnops_init(void) 14428 { 14429 } 14430 14431 void 14432 nfs4_vnops_fini(void) 14433 { 14434 } 14435 14436 /* 14437 * Return a reference to the directory (parent) vnode for a given vnode, 14438 * using the saved pathname information and the directory file handle. The 14439 * caller is responsible for disposing of the reference. 14440 * Returns zero or an errno value. 14441 * 14442 * Caller should set need_start_op to FALSE if it is the recovery 14443 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14444 */ 14445 int 14446 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14447 { 14448 svnode_t *svnp; 14449 vnode_t *dvp = NULL; 14450 servinfo4_t *svp; 14451 nfs4_fname_t *mfname; 14452 int error; 14453 14454 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14455 14456 if (vp->v_flag & VROOT) { 14457 nfs4_sharedfh_t *sfh; 14458 nfs_fh4 fh; 14459 mntinfo4_t *mi; 14460 14461 ASSERT(vp->v_type == VREG); 14462 14463 mi = VTOMI4(vp); 14464 svp = mi->mi_curr_serv; 14465 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14466 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14467 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14468 sfh = sfh4_get(&fh, VTOMI4(vp)); 14469 nfs_rw_exit(&svp->sv_lock); 14470 mfname = mi->mi_fname; 14471 fn_hold(mfname); 14472 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14473 sfh4_rele(&sfh); 14474 14475 if (dvp->v_type == VNON) 14476 dvp->v_type = VDIR; 14477 *dvpp = dvp; 14478 return (0); 14479 } 14480 14481 svnp = VTOSV(vp); 14482 14483 if (svnp == NULL) { 14484 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14485 "shadow node is NULL")); 14486 return (EINVAL); 14487 } 14488 14489 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14490 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14491 "shadow node name or dfh val == NULL")); 14492 return (EINVAL); 14493 } 14494 14495 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14496 (int)need_start_op); 14497 if (error != 0) { 14498 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14499 "nfs4_make_dotdot returned %d", error)); 14500 return (error); 14501 } 14502 if (!dvp) { 14503 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14504 "nfs4_make_dotdot returned a NULL dvp")); 14505 return (EIO); 14506 } 14507 if (dvp->v_type == VNON) 14508 dvp->v_type = VDIR; 14509 ASSERT(dvp->v_type == VDIR); 14510 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14511 mutex_enter(&dvp->v_lock); 14512 dvp->v_flag |= V_XATTRDIR; 14513 mutex_exit(&dvp->v_lock); 14514 } 14515 *dvpp = dvp; 14516 return (0); 14517 } 14518 14519 /* 14520 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14521 * length that fnamep can accept, including the trailing null. 14522 * Returns 0 if okay, returns an errno value if there was a problem. 14523 */ 14524 14525 int 14526 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14527 { 14528 char *fn; 14529 int err = 0; 14530 servinfo4_t *svp; 14531 svnode_t *shvp; 14532 14533 /* 14534 * If the file being opened has VROOT set, then this is 14535 * a "file" mount. sv_name will not be interesting, so 14536 * go back to the servinfo4 to get the original mount 14537 * path and strip off all but the final edge. Otherwise 14538 * just return the name from the shadow vnode. 14539 */ 14540 14541 if (vp->v_flag & VROOT) { 14542 14543 svp = VTOMI4(vp)->mi_curr_serv; 14544 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14545 14546 fn = strrchr(svp->sv_path, '/'); 14547 if (fn == NULL) 14548 err = EINVAL; 14549 else 14550 fn++; 14551 } else { 14552 shvp = VTOSV(vp); 14553 fn = fn_name(shvp->sv_name); 14554 } 14555 14556 if (err == 0) 14557 if (strlen(fn) < maxlen) 14558 (void) strcpy(fnamep, fn); 14559 else 14560 err = ENAMETOOLONG; 14561 14562 if (vp->v_flag & VROOT) 14563 nfs_rw_exit(&svp->sv_lock); 14564 else 14565 kmem_free(fn, MAXNAMELEN); 14566 14567 return (err); 14568 } 14569 14570 /* 14571 * If the vnode has pages, run the list and check for 14572 * any that are still dangling. We call this function 14573 * before the OTW CLOSE occurs so we can B_INVAL the 14574 * danglers. 14575 */ 14576 static int 14577 nfs4_dross_pages(vnode_t *vp) 14578 { 14579 page_t *pp; 14580 kmutex_t *vphm; 14581 rnode4_t *rp; 14582 14583 /* make sure we're looking at the master vnode, not a shadow */ 14584 rp = VTOR4(vp); 14585 if (IS_SHADOW(vp, rp)) 14586 vp = RTOV4(rp); 14587 14588 vphm = page_vnode_mutex(vp); 14589 mutex_enter(vphm); 14590 if ((pp = vp->v_pages) != NULL) { 14591 do { 14592 if (pp->p_fsdata != C_NOCOMMIT) { 14593 mutex_exit(vphm); 14594 return (1); 14595 } 14596 } while ((pp = pp->p_vpnext) != vp->v_pages); 14597 } 14598 mutex_exit(vphm); 14599 14600 return (0); 14601 } 14602 14603 /* 14604 * Bookkeeping for a close that doesn't need to go over the wire. 14605 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14606 * it is left at 1. 14607 */ 14608 void 14609 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14610 { 14611 rnode4_t *rp; 14612 mntinfo4_t *mi; 14613 14614 mi = VTOMI4(vp); 14615 rp = VTOR4(vp); 14616 14617 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14618 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14619 ASSERT(curproc->p_zone == mi->mi_zone); 14620 ASSERT(mutex_owned(&osp->os_sync_lock)); 14621 ASSERT(*have_lockp); 14622 14623 if (!osp->os_valid || 14624 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14625 return; 14626 } 14627 14628 /* 14629 * This removes the reference obtained at OPEN; ie, 14630 * when the open stream structure was created. 14631 * 14632 * We don't have to worry about calling 'open_stream_rele' 14633 * since we our currently holding a reference to this 14634 * open stream which means the count can not go to 0 with 14635 * this decrement. 14636 */ 14637 ASSERT(osp->os_ref_count >= 2); 14638 osp->os_ref_count--; 14639 osp->os_valid = 0; 14640 mutex_exit(&osp->os_sync_lock); 14641 *have_lockp = 0; 14642 14643 nfs4_dec_state_ref_count(mi); 14644 } 14645 14646 /* 14647 * Close all remaining open streams on the rnode. These open streams 14648 * could be here because: 14649 * - The close attempted at either close or delmap failed 14650 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14651 * - Someone did mknod on a regular file but never opened it 14652 */ 14653 int 14654 nfs4close_all(vnode_t *vp, cred_t *cr) 14655 { 14656 nfs4_open_stream_t *osp; 14657 int error; 14658 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14659 rnode4_t *rp; 14660 14661 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14662 14663 error = 0; 14664 rp = VTOR4(vp); 14665 14666 /* 14667 * At this point, all we know is that the last time 14668 * someone called vn_rele, the count was 1. Since then, 14669 * the vnode could have been re-activated. We want to 14670 * loop through the open streams and close each one, but 14671 * we have to be careful since once we release the rnode 14672 * hash bucket lock, someone else is free to come in and 14673 * re-activate the rnode and add new open streams. The 14674 * strategy is take the rnode hash bucket lock, verify that 14675 * the count is still 1, grab the open stream off the 14676 * head of the list and mark it invalid, then release the 14677 * rnode hash bucket lock and proceed with that open stream. 14678 * This is ok because nfs4close_one() will acquire the proper 14679 * open/create to close/destroy synchronization for open 14680 * streams, and will ensure that if someone has reopened 14681 * the open stream after we've dropped the hash bucket lock 14682 * then we'll just simply return without destroying the 14683 * open stream. 14684 * Repeat until the list is empty. 14685 */ 14686 14687 for (;;) { 14688 14689 /* make sure vnode hasn't been reactivated */ 14690 rw_enter(&rp->r_hashq->r_lock, RW_READER); 14691 mutex_enter(&vp->v_lock); 14692 if (vp->v_count > 1) { 14693 mutex_exit(&vp->v_lock); 14694 rw_exit(&rp->r_hashq->r_lock); 14695 break; 14696 } 14697 /* 14698 * Grabbing r_os_lock before releasing v_lock prevents 14699 * a window where the rnode/open stream could get 14700 * reactivated (and os_force_close set to 0) before we 14701 * had a chance to set os_force_close to 1. 14702 */ 14703 mutex_enter(&rp->r_os_lock); 14704 mutex_exit(&vp->v_lock); 14705 14706 osp = list_head(&rp->r_open_streams); 14707 if (!osp) { 14708 /* nothing left to CLOSE OTW, so return */ 14709 mutex_exit(&rp->r_os_lock); 14710 rw_exit(&rp->r_hashq->r_lock); 14711 break; 14712 } 14713 14714 mutex_enter(&rp->r_statev4_lock); 14715 /* the file can't still be mem mapped */ 14716 ASSERT(rp->r_mapcnt == 0); 14717 if (rp->created_v4) 14718 rp->created_v4 = 0; 14719 mutex_exit(&rp->r_statev4_lock); 14720 14721 /* 14722 * Grab a ref on this open stream; nfs4close_one 14723 * will mark it as invalid 14724 */ 14725 mutex_enter(&osp->os_sync_lock); 14726 osp->os_ref_count++; 14727 osp->os_force_close = 1; 14728 mutex_exit(&osp->os_sync_lock); 14729 mutex_exit(&rp->r_os_lock); 14730 rw_exit(&rp->r_hashq->r_lock); 14731 14732 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 14733 14734 /* Update error if it isn't already non-zero */ 14735 if (error == 0) { 14736 if (e.error) 14737 error = e.error; 14738 else if (e.stat) 14739 error = geterrno4(e.stat); 14740 } 14741 14742 #ifdef DEBUG 14743 nfs4close_all_cnt++; 14744 #endif 14745 /* Release the ref on osp acquired above. */ 14746 open_stream_rele(osp, rp); 14747 14748 /* Proceed to the next open stream, if any */ 14749 } 14750 return (error); 14751 } 14752 14753 /* 14754 * nfs4close_one - close one open stream for a file if needed. 14755 * 14756 * "close_type" indicates which close path this is: 14757 * CLOSE_NORM: close initiated via VOP_CLOSE. 14758 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 14759 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 14760 * the close and release of client state for this open stream 14761 * (unless someone else has the open stream open). 14762 * CLOSE_RESEND: indicates the request is a replay of an earlier request 14763 * (e.g., due to abort because of a signal). 14764 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 14765 * 14766 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 14767 * recovery. Instead, the caller is expected to deal with retries. 14768 * 14769 * The caller can either pass in the osp ('provided_osp') or not. 14770 * 14771 * 'access_bits' represents the access we are closing/downgrading. 14772 * 14773 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 14774 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 14775 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 14776 * 14777 * Errors are returned via the nfs4_error_t. 14778 */ 14779 void 14780 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 14781 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 14782 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 14783 uint_t mmap_flags) 14784 { 14785 nfs4_open_owner_t *oop; 14786 nfs4_open_stream_t *osp = NULL; 14787 int retry = 0; 14788 int num_retries = NFS4_NUM_RECOV_RETRIES; 14789 rnode4_t *rp; 14790 mntinfo4_t *mi; 14791 nfs4_recov_state_t recov_state; 14792 cred_t *cred_otw = NULL; 14793 bool_t recovonly = FALSE; 14794 int isrecov; 14795 int force_close; 14796 int close_failed = 0; 14797 int did_dec_count = 0; 14798 int did_start_op = 0; 14799 int did_force_recovlock = 0; 14800 int did_start_seqid_sync = 0; 14801 int have_sync_lock = 0; 14802 14803 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 14804 14805 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 14806 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 14807 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 14808 len, maxprot, mmap_flags, access_bits)); 14809 14810 nfs4_error_zinit(ep); 14811 rp = VTOR4(vp); 14812 mi = VTOMI4(vp); 14813 isrecov = (close_type == CLOSE_RESEND || 14814 close_type == CLOSE_AFTER_RESEND); 14815 14816 /* 14817 * First get the open owner. 14818 */ 14819 if (!provided_osp) { 14820 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 14821 } else { 14822 oop = provided_osp->os_open_owner; 14823 ASSERT(oop != NULL); 14824 open_owner_hold(oop); 14825 } 14826 14827 if (!oop) { 14828 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 14829 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 14830 "close type %d", (void *)rp, (void *)mi, (void *)cr, 14831 (void *)provided_osp, close_type)); 14832 ep->error = EIO; 14833 goto out; 14834 } 14835 14836 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 14837 recov_retry: 14838 osp = NULL; 14839 close_failed = 0; 14840 force_close = (close_type == CLOSE_FORCE); 14841 retry = 0; 14842 did_start_op = 0; 14843 did_force_recovlock = 0; 14844 did_start_seqid_sync = 0; 14845 have_sync_lock = 0; 14846 recovonly = FALSE; 14847 recov_state.rs_flags = 0; 14848 recov_state.rs_num_retry_despite_err = 0; 14849 14850 /* 14851 * Second synchronize with recovery. 14852 */ 14853 if (!isrecov) { 14854 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 14855 &recov_state, &recovonly); 14856 if (!ep->error) { 14857 did_start_op = 1; 14858 } else { 14859 close_failed = 1; 14860 /* 14861 * If we couldn't get start_fop, but have to 14862 * cleanup state, then at least acquire the 14863 * mi_recovlock so we can synchronize with 14864 * recovery. 14865 */ 14866 if (close_type == CLOSE_FORCE) { 14867 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 14868 RW_READER, FALSE); 14869 did_force_recovlock = 1; 14870 } else 14871 goto out; 14872 } 14873 } 14874 14875 /* 14876 * We cannot attempt to get the open seqid sync if nfs4_start_fop 14877 * set 'recovonly' to TRUE since most likely this is due to 14878 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 14879 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 14880 * to retry, causing us to loop until recovery finishes. Plus we 14881 * don't need protection over the open seqid since we're not going 14882 * OTW, hence don't need to use the seqid. 14883 */ 14884 if (recovonly == FALSE) { 14885 /* need to grab the open owner sync before 'os_sync_lock' */ 14886 ep->error = nfs4_start_open_seqid_sync(oop, mi); 14887 if (ep->error == EAGAIN) { 14888 ASSERT(!isrecov); 14889 if (did_start_op) 14890 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 14891 &recov_state, TRUE); 14892 if (did_force_recovlock) 14893 nfs_rw_exit(&mi->mi_recovlock); 14894 goto recov_retry; 14895 } 14896 did_start_seqid_sync = 1; 14897 } 14898 14899 /* 14900 * Third get an open stream and acquire 'os_sync_lock' to 14901 * sychronize the opening/creating of an open stream with the 14902 * closing/destroying of an open stream. 14903 */ 14904 if (!provided_osp) { 14905 /* returns with 'os_sync_lock' held */ 14906 osp = find_open_stream(oop, rp); 14907 if (!osp) { 14908 ep->error = EIO; 14909 goto out; 14910 } 14911 } else { 14912 osp = provided_osp; 14913 open_stream_hold(osp); 14914 mutex_enter(&osp->os_sync_lock); 14915 } 14916 have_sync_lock = 1; 14917 14918 ASSERT(oop == osp->os_open_owner); 14919 14920 /* 14921 * Fourth, do any special pre-OTW CLOSE processing 14922 * based on the specific close type. 14923 */ 14924 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 14925 !did_dec_count) { 14926 ASSERT(osp->os_open_ref_count > 0); 14927 osp->os_open_ref_count--; 14928 did_dec_count = 1; 14929 if (osp->os_open_ref_count == 0) 14930 osp->os_final_close = 1; 14931 } 14932 14933 if (close_type == CLOSE_FORCE) { 14934 /* see if somebody reopened the open stream. */ 14935 if (!osp->os_force_close) { 14936 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 14937 "nfs4close_one: skip CLOSE_FORCE as osp %p " 14938 "was reopened, vp %p", (void *)osp, (void *)vp)); 14939 ep->error = 0; 14940 ep->stat = NFS4_OK; 14941 goto out; 14942 } 14943 14944 if (!osp->os_final_close && !did_dec_count) { 14945 osp->os_open_ref_count--; 14946 did_dec_count = 1; 14947 } 14948 14949 /* 14950 * We can't depend on os_open_ref_count being 0 due to the 14951 * way executables are opened (VN_RELE to match a VOP_OPEN). 14952 */ 14953 #ifdef NOTYET 14954 ASSERT(osp->os_open_ref_count == 0); 14955 #endif 14956 if (osp->os_open_ref_count != 0) { 14957 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 14958 "nfs4close_one: should panic here on an " 14959 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 14960 "since this is probably the exec problem.")); 14961 14962 osp->os_open_ref_count = 0; 14963 } 14964 14965 /* 14966 * There is the possibility that nfs4close_one() 14967 * for close_type == CLOSE_DELMAP couldn't find the 14968 * open stream, thus couldn't decrement its os_mapcnt; 14969 * therefore we can't use this ASSERT yet. 14970 */ 14971 #ifdef NOTYET 14972 ASSERT(osp->os_mapcnt == 0); 14973 #endif 14974 osp->os_mapcnt = 0; 14975 } 14976 14977 if (close_type == CLOSE_DELMAP && !did_dec_count) { 14978 ASSERT(osp->os_mapcnt >= btopr(len)); 14979 14980 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 14981 osp->os_mmap_write -= btopr(len); 14982 if (maxprot & PROT_READ) 14983 osp->os_mmap_read -= btopr(len); 14984 if (maxprot & PROT_EXEC) 14985 osp->os_mmap_read -= btopr(len); 14986 /* mirror the PROT_NONE check in nfs4_addmap() */ 14987 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 14988 !(maxprot & PROT_EXEC)) 14989 osp->os_mmap_read -= btopr(len); 14990 osp->os_mapcnt -= btopr(len); 14991 did_dec_count = 1; 14992 } 14993 14994 if (recovonly) { 14995 nfs4_lost_rqst_t lost_rqst; 14996 14997 /* request should not already be in recovery queue */ 14998 ASSERT(lrp == NULL); 14999 nfs4_error_init(ep, EINTR); 15000 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15001 osp, cred_otw, vp); 15002 mutex_exit(&osp->os_sync_lock); 15003 have_sync_lock = 0; 15004 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15005 lost_rqst.lr_op == OP_CLOSE ? 15006 &lost_rqst : NULL, OP_CLOSE, NULL); 15007 close_failed = 1; 15008 force_close = 0; 15009 goto close_cleanup; 15010 } 15011 15012 /* 15013 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15014 * we stopped operating on the open owner's <old oo_name, old seqid> 15015 * space, which means we stopped operating on the open stream 15016 * too. So don't go OTW (as the seqid is likely bad, and the 15017 * stateid could be stale, potentially triggering a false 15018 * setclientid), and just clean up the client's internal state. 15019 */ 15020 if (osp->os_orig_oo_name != oop->oo_name) { 15021 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15022 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15023 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15024 "oo_name %" PRIx64")", 15025 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15026 oop->oo_name)); 15027 close_failed = 1; 15028 } 15029 15030 /* If the file failed recovery, just quit. */ 15031 mutex_enter(&rp->r_statelock); 15032 if (rp->r_flags & R4RECOVERR) { 15033 close_failed = 1; 15034 } 15035 mutex_exit(&rp->r_statelock); 15036 15037 /* 15038 * If the force close path failed to obtain start_fop 15039 * then skip the OTW close and just remove the state. 15040 */ 15041 if (close_failed) 15042 goto close_cleanup; 15043 15044 /* 15045 * Fifth, check to see if there are still mapped pages or other 15046 * opens using this open stream. If there are then we can't 15047 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15048 */ 15049 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15050 nfs4_lost_rqst_t new_lost_rqst; 15051 bool_t needrecov = FALSE; 15052 cred_t *odg_cred_otw = NULL; 15053 seqid4 open_dg_seqid = 0; 15054 15055 if (osp->os_delegation) { 15056 /* 15057 * If this open stream was never OPENed OTW then we 15058 * surely can't DOWNGRADE it (especially since the 15059 * osp->open_stateid is really a delegation stateid 15060 * when os_delegation is 1). 15061 */ 15062 if (access_bits & FREAD) 15063 osp->os_share_acc_read--; 15064 if (access_bits & FWRITE) 15065 osp->os_share_acc_write--; 15066 osp->os_share_deny_none--; 15067 nfs4_error_zinit(ep); 15068 goto out; 15069 } 15070 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15071 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15072 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15073 if (needrecov && !isrecov) { 15074 bool_t abort; 15075 nfs4_bseqid_entry_t *bsep = NULL; 15076 15077 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15078 bsep = nfs4_create_bseqid_entry(oop, NULL, 15079 vp, 0, 15080 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15081 open_dg_seqid); 15082 15083 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15084 oop, osp, odg_cred_otw, vp, access_bits, 0); 15085 mutex_exit(&osp->os_sync_lock); 15086 have_sync_lock = 0; 15087 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15088 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15089 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15090 bsep); 15091 if (odg_cred_otw) 15092 crfree(odg_cred_otw); 15093 if (bsep) 15094 kmem_free(bsep, sizeof (*bsep)); 15095 15096 if (abort == TRUE) 15097 goto out; 15098 15099 if (did_start_seqid_sync) { 15100 nfs4_end_open_seqid_sync(oop); 15101 did_start_seqid_sync = 0; 15102 } 15103 open_stream_rele(osp, rp); 15104 15105 if (did_start_op) 15106 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15107 &recov_state, FALSE); 15108 if (did_force_recovlock) 15109 nfs_rw_exit(&mi->mi_recovlock); 15110 15111 goto recov_retry; 15112 } else { 15113 if (odg_cred_otw) 15114 crfree(odg_cred_otw); 15115 } 15116 goto out; 15117 } 15118 15119 /* 15120 * If this open stream was created as the results of an open 15121 * while holding a delegation, then just release it; no need 15122 * to do an OTW close. Otherwise do a "normal" OTW close. 15123 */ 15124 if (osp->os_delegation) { 15125 nfs4close_notw(vp, osp, &have_sync_lock); 15126 nfs4_error_zinit(ep); 15127 goto out; 15128 } 15129 15130 /* 15131 * If this stream is not valid, we're done. 15132 */ 15133 if (!osp->os_valid) { 15134 nfs4_error_zinit(ep); 15135 goto out; 15136 } 15137 15138 /* 15139 * Last open or mmap ref has vanished, need to do an OTW close. 15140 * First check to see if a close is still necessary. 15141 */ 15142 if (osp->os_failed_reopen) { 15143 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15144 "don't close OTW osp %p since reopen failed.", 15145 (void *)osp)); 15146 /* 15147 * Reopen of the open stream failed, hence the 15148 * stateid of the open stream is invalid/stale, and 15149 * sending this OTW would incorrectly cause another 15150 * round of recovery. In this case, we need to set 15151 * the 'os_valid' bit to 0 so another thread doesn't 15152 * come in and re-open this open stream before 15153 * this "closing" thread cleans up state (decrementing 15154 * the nfs4_server_t's state_ref_count and decrementing 15155 * the os_ref_count). 15156 */ 15157 osp->os_valid = 0; 15158 /* 15159 * This removes the reference obtained at OPEN; ie, 15160 * when the open stream structure was created. 15161 * 15162 * We don't have to worry about calling 'open_stream_rele' 15163 * since we our currently holding a reference to this 15164 * open stream which means the count can not go to 0 with 15165 * this decrement. 15166 */ 15167 ASSERT(osp->os_ref_count >= 2); 15168 osp->os_ref_count--; 15169 nfs4_error_zinit(ep); 15170 close_failed = 0; 15171 goto close_cleanup; 15172 } 15173 15174 ASSERT(osp->os_ref_count > 1); 15175 15176 if (!(vp->v_vfsp->vfs_flag & VFS_RDONLY) && 15177 nfs4_dross_pages(vp)) { 15178 nfs4_invalidate_pages(vp, 0, cred_otw); 15179 } 15180 15181 /* 15182 * Sixth, try the CLOSE OTW. 15183 */ 15184 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15185 close_type, ep, &have_sync_lock); 15186 15187 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15188 /* 15189 * Let the recovery thread be responsible for 15190 * removing the state for CLOSE. 15191 */ 15192 close_failed = 1; 15193 force_close = 0; 15194 retry = 0; 15195 } 15196 15197 /* See if we need to retry with a different cred */ 15198 if ((ep->error == EACCES || 15199 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15200 cred_otw != cr) { 15201 crfree(cred_otw); 15202 cred_otw = cr; 15203 crhold(cred_otw); 15204 retry = 1; 15205 } 15206 15207 if (ep->error || ep->stat) 15208 close_failed = 1; 15209 15210 if (retry && !isrecov && num_retries-- > 0) { 15211 if (have_sync_lock) { 15212 mutex_exit(&osp->os_sync_lock); 15213 have_sync_lock = 0; 15214 } 15215 if (did_start_seqid_sync) { 15216 nfs4_end_open_seqid_sync(oop); 15217 did_start_seqid_sync = 0; 15218 } 15219 open_stream_rele(osp, rp); 15220 15221 if (did_start_op) 15222 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15223 &recov_state, FALSE); 15224 if (did_force_recovlock) 15225 nfs_rw_exit(&mi->mi_recovlock); 15226 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15227 "nfs4close_one: need to retry the close " 15228 "operation")); 15229 goto recov_retry; 15230 } 15231 close_cleanup: 15232 /* 15233 * Seventh and lastly, process our results. 15234 */ 15235 if (close_failed && force_close) { 15236 /* 15237 * It's ok to drop and regrab the 'os_sync_lock' since 15238 * nfs4close_notw() will recheck to make sure the 15239 * "close"/removal of state should happen. 15240 */ 15241 if (!have_sync_lock) { 15242 mutex_enter(&osp->os_sync_lock); 15243 have_sync_lock = 1; 15244 } 15245 /* 15246 * This is last call, remove the ref on the open 15247 * stream created by open and clean everything up. 15248 */ 15249 osp->os_pending_close = 0; 15250 nfs4close_notw(vp, osp, &have_sync_lock); 15251 nfs4_error_zinit(ep); 15252 } 15253 15254 if (!close_failed) { 15255 if (have_sync_lock) { 15256 osp->os_pending_close = 0; 15257 mutex_exit(&osp->os_sync_lock); 15258 have_sync_lock = 0; 15259 } else { 15260 mutex_enter(&osp->os_sync_lock); 15261 osp->os_pending_close = 0; 15262 mutex_exit(&osp->os_sync_lock); 15263 } 15264 if (did_start_op && recov_state.rs_sp != NULL) { 15265 mutex_enter(&recov_state.rs_sp->s_lock); 15266 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15267 mutex_exit(&recov_state.rs_sp->s_lock); 15268 } else { 15269 nfs4_dec_state_ref_count(mi); 15270 } 15271 nfs4_error_zinit(ep); 15272 } 15273 15274 out: 15275 if (have_sync_lock) 15276 mutex_exit(&osp->os_sync_lock); 15277 if (did_start_op) 15278 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15279 recovonly ? TRUE : FALSE); 15280 if (did_force_recovlock) 15281 nfs_rw_exit(&mi->mi_recovlock); 15282 if (cred_otw) 15283 crfree(cred_otw); 15284 if (osp) 15285 open_stream_rele(osp, rp); 15286 if (oop) { 15287 if (did_start_seqid_sync) 15288 nfs4_end_open_seqid_sync(oop); 15289 open_owner_rele(oop); 15290 } 15291 } 15292 15293 /* 15294 * Convert information returned by the server in the LOCK4denied 15295 * structure to the form required by fcntl. 15296 */ 15297 static void 15298 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15299 { 15300 nfs4_lo_name_t *lo; 15301 15302 #ifdef DEBUG 15303 if (denied_to_flk_debug) { 15304 lockt_denied_debug = lockt_denied; 15305 debug_enter("lockt_denied"); 15306 } 15307 #endif 15308 15309 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15310 flk->l_whence = 0; /* aka SEEK_SET */ 15311 flk->l_start = lockt_denied->offset; 15312 flk->l_len = lockt_denied->length; 15313 15314 /* 15315 * If the blocking clientid matches our client id, then we can 15316 * interpret the lockowner (since we built it). If not, then 15317 * fabricate a sysid and pid. Note that the l_sysid field 15318 * in *flk already has the local sysid. 15319 */ 15320 15321 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15322 15323 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15324 lo = (nfs4_lo_name_t *) 15325 lockt_denied->owner.owner_val; 15326 15327 flk->l_pid = lo->ln_pid; 15328 } else { 15329 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15330 "denied_to_flk: bad lock owner length\n")); 15331 15332 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15333 } 15334 } else { 15335 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15336 "denied_to_flk: foreign clientid\n")); 15337 15338 /* 15339 * Construct a new sysid which should be different from 15340 * sysids of other systems. 15341 */ 15342 15343 flk->l_sysid++; 15344 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15345 } 15346 } 15347 15348 static pid_t 15349 lo_to_pid(lock_owner4 *lop) 15350 { 15351 pid_t pid = 0; 15352 uchar_t *cp; 15353 int i; 15354 15355 cp = (uchar_t *)&lop->clientid; 15356 15357 for (i = 0; i < sizeof (lop->clientid); i++) 15358 pid += (pid_t)*cp++; 15359 15360 cp = (uchar_t *)lop->owner_val; 15361 15362 for (i = 0; i < lop->owner_len; i++) 15363 pid += (pid_t)*cp++; 15364 15365 return (pid); 15366 } 15367 15368 /* 15369 * Given a lock pointer, returns the length of that lock. 15370 * "end" is the last locked offset the "l_len" covers from 15371 * the start of the lock. 15372 */ 15373 static off64_t 15374 lock_to_end(flock64_t *lock) 15375 { 15376 off64_t lock_end; 15377 15378 if (lock->l_len == 0) 15379 lock_end = (off64_t)MAXEND; 15380 else 15381 lock_end = lock->l_start + lock->l_len - 1; 15382 15383 return (lock_end); 15384 } 15385 15386 /* 15387 * Given the end of a lock, it will return you the length "l_len" for that lock. 15388 */ 15389 static off64_t 15390 end_to_len(off64_t start, off64_t end) 15391 { 15392 off64_t lock_len; 15393 15394 ASSERT(end >= start); 15395 if (end == MAXEND) 15396 lock_len = 0; 15397 else 15398 lock_len = end - start + 1; 15399 15400 return (lock_len); 15401 } 15402 15403 /* 15404 * On given end for a lock it determines if it is the last locked offset 15405 * or not, if so keeps it as is, else adds one to return the length for 15406 * valid start. 15407 */ 15408 static off64_t 15409 start_check(off64_t x) 15410 { 15411 if (x == MAXEND) 15412 return (x); 15413 else 15414 return (x + 1); 15415 } 15416 15417 /* 15418 * See if these two locks overlap, and if so return 1; 15419 * otherwise, return 0. 15420 */ 15421 static int 15422 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15423 { 15424 off64_t llfp_end, curfp_end; 15425 15426 llfp_end = lock_to_end(llfp); 15427 curfp_end = lock_to_end(curfp); 15428 15429 if (((llfp_end >= curfp->l_start) && 15430 (llfp->l_start <= curfp->l_start)) || 15431 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15432 return (1); 15433 return (0); 15434 } 15435 15436 /* 15437 * Determine what the interseting lock region is, and add that to the 15438 * 'nl_llpp' locklist in increasing order (by l_start). 15439 */ 15440 static void 15441 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15442 locklist_t **nl_llpp, vnode_t *vp) 15443 { 15444 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15445 off64_t lost_flp_end, local_flp_end, len, start; 15446 15447 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15448 15449 if (!locks_intersect(lost_flp, local_flp)) 15450 return; 15451 15452 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15453 "locks intersect")); 15454 15455 lost_flp_end = lock_to_end(lost_flp); 15456 local_flp_end = lock_to_end(local_flp); 15457 15458 /* Find the starting point of the intersecting region */ 15459 if (local_flp->l_start > lost_flp->l_start) 15460 start = local_flp->l_start; 15461 else 15462 start = lost_flp->l_start; 15463 15464 /* Find the lenght of the intersecting region */ 15465 if (lost_flp_end < local_flp_end) 15466 len = end_to_len(start, lost_flp_end); 15467 else 15468 len = end_to_len(start, local_flp_end); 15469 15470 /* 15471 * Prepare the flock structure for the intersection found and insert 15472 * it into the new list in increasing l_start order. This list contains 15473 * intersections of locks registered by the client with the local host 15474 * and the lost lock. 15475 * The lock type of this lock is the same as that of the local_flp. 15476 */ 15477 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15478 intersect_llp->ll_flock.l_start = start; 15479 intersect_llp->ll_flock.l_len = len; 15480 intersect_llp->ll_flock.l_type = local_flp->l_type; 15481 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15482 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15483 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15484 intersect_llp->ll_vp = vp; 15485 15486 tmp_fllp = *nl_llpp; 15487 cur_fllp = NULL; 15488 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15489 intersect_llp->ll_flock.l_start) { 15490 cur_fllp = tmp_fllp; 15491 tmp_fllp = tmp_fllp->ll_next; 15492 } 15493 if (cur_fllp == NULL) { 15494 /* first on the list */ 15495 intersect_llp->ll_next = *nl_llpp; 15496 *nl_llpp = intersect_llp; 15497 } else { 15498 intersect_llp->ll_next = cur_fllp->ll_next; 15499 cur_fllp->ll_next = intersect_llp; 15500 } 15501 15502 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15503 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15504 intersect_llp->ll_flock.l_start, 15505 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15506 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15507 } 15508 15509 /* 15510 * Our local locking current state is potentially different than 15511 * what the NFSv4 server thinks we have due to a lost lock that was 15512 * resent and then received. We need to reset our "NFSv4" locking 15513 * state to match the current local locking state for this pid since 15514 * that is what the user/application sees as what the world is. 15515 * 15516 * We cannot afford to drop the open/lock seqid sync since then we can 15517 * get confused about what the current local locking state "is" versus 15518 * "was". 15519 * 15520 * If we are unable to fix up the locks, we send SIGLOST to the affected 15521 * process. This is not done if the filesystem has been forcibly 15522 * unmounted, in case the process has already exited and a new process 15523 * exists with the same pid. 15524 */ 15525 static void 15526 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15527 nfs4_lock_owner_t *lop) 15528 { 15529 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15530 mntinfo4_t *mi = VTOMI4(vp); 15531 const int cmd = F_SETLK; 15532 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15533 flock64_t ul_fl; 15534 15535 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15536 "nfs4_reinstitute_local_lock_state")); 15537 15538 /* 15539 * Find active locks for this vp from the local locking code. 15540 * Scan through this list and find out the locks that intersect with 15541 * the lost lock. Once we find the lock that intersects, add the 15542 * intersection area as a new lock to a new list "ri_llp". The lock 15543 * type of the intersection region lock added to ri_llp is the same 15544 * as that found in the active lock list, "list". The intersecting 15545 * region locks are added to ri_llp in increasing l_start order. 15546 */ 15547 ASSERT(curproc->p_zone == mi->mi_zone); 15548 15549 locks = flk_active_locks_for_vp(vp); 15550 ri_llp = NULL; 15551 15552 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15553 ASSERT(llp->ll_vp == vp); 15554 /* 15555 * Pick locks that belong to this pid/lockowner 15556 */ 15557 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15558 continue; 15559 15560 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15561 } 15562 15563 /* 15564 * Now we have the list of intersections with the lost lock. These are 15565 * the locks that were/are active before the server replied to the 15566 * last/lost lock. Issue these locks to the server here. Playing these 15567 * locks to the server will re-establish aur current local locking state 15568 * with the v4 server. 15569 * If we get an error, send SIGLOST to the application for that lock. 15570 */ 15571 15572 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15573 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15574 "nfs4_reinstitute_local_lock_state: need to issue " 15575 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15576 llp->ll_flock.l_start, 15577 llp->ll_flock.l_start + llp->ll_flock.l_len, 15578 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15579 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15580 /* 15581 * No need to relock what we already have 15582 */ 15583 if (llp->ll_flock.l_type == lost_flp->l_type) 15584 continue; 15585 15586 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15587 } 15588 15589 /* 15590 * Now keeping the start of the lost lock as our reference parse the 15591 * newly created ri_llp locklist to find the ranges that we have locked 15592 * with the v4 server but not in the current local locking. We need 15593 * to unlock these ranges. 15594 * These ranges can also be reffered to as those ranges, where the lost 15595 * lock does not overlap with the locks in the ri_llp but are locked 15596 * since the server replied to the lost lock. 15597 */ 15598 cur_start = lost_flp->l_start; 15599 lost_flp_end = lock_to_end(lost_flp); 15600 15601 ul_fl.l_type = F_UNLCK; 15602 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15603 ul_fl.l_sysid = lost_flp->l_sysid; 15604 ul_fl.l_pid = lost_flp->l_pid; 15605 15606 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15607 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15608 15609 if (llp->ll_flock.l_start <= cur_start) { 15610 cur_start = start_check(llp_ll_flock_end); 15611 continue; 15612 } 15613 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15614 "nfs4_reinstitute_local_lock_state: " 15615 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15616 cur_start, llp->ll_flock.l_start)); 15617 15618 ul_fl.l_start = cur_start; 15619 ul_fl.l_len = end_to_len(cur_start, 15620 (llp->ll_flock.l_start - 1)); 15621 15622 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15623 cur_start = start_check(llp_ll_flock_end); 15624 } 15625 15626 /* 15627 * In the case where the lost lock ends after all intersecting locks, 15628 * unlock the last part of the lost lock range. 15629 */ 15630 if (cur_start != start_check(lost_flp_end)) { 15631 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15632 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15633 "lost lock region [%"PRIx64" - %"PRIx64"]", 15634 cur_start, lost_flp->l_start + lost_flp->l_len)); 15635 15636 ul_fl.l_start = cur_start; 15637 /* 15638 * Is it an to-EOF lock? if so unlock till the end 15639 */ 15640 if (lost_flp->l_len == 0) 15641 ul_fl.l_len = 0; 15642 else 15643 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15644 15645 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15646 } 15647 15648 if (locks != NULL) 15649 flk_free_locklist(locks); 15650 15651 /* Free up our newly created locklist */ 15652 for (llp = ri_llp; llp != NULL; ) { 15653 tmp_llp = llp->ll_next; 15654 kmem_free(llp, sizeof (locklist_t)); 15655 llp = tmp_llp; 15656 } 15657 15658 /* 15659 * Now return back to the original calling nfs4frlock() 15660 * and let us naturally drop our seqid syncs. 15661 */ 15662 } 15663 15664 /* 15665 * Create a lost state record for the given lock reinstantiation request 15666 * and push it onto the lost state queue. 15667 */ 15668 static void 15669 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15670 nfs4_lock_owner_t *lop) 15671 { 15672 nfs4_lost_rqst_t req; 15673 nfs_lock_type4 locktype; 15674 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15675 15676 ASSERT(curproc->p_zone == VTOMI4(vp)->mi_zone); 15677 15678 locktype = flk_to_locktype(cmd, flk->l_type); 15679 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 15680 NULL, NULL, lop, flk, &req, cr, vp); 15681 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 15682 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 15683 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 15684 NULL); 15685 } 15686