1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 28 * All Rights Reserved 29 */ 30 31 #include <sys/param.h> 32 #include <sys/types.h> 33 #include <sys/systm.h> 34 #include <sys/cred.h> 35 #include <sys/time.h> 36 #include <sys/vnode.h> 37 #include <sys/vfs.h> 38 #include <sys/vfs_opreg.h> 39 #include <sys/file.h> 40 #include <sys/filio.h> 41 #include <sys/uio.h> 42 #include <sys/buf.h> 43 #include <sys/mman.h> 44 #include <sys/pathname.h> 45 #include <sys/dirent.h> 46 #include <sys/debug.h> 47 #include <sys/vmsystm.h> 48 #include <sys/fcntl.h> 49 #include <sys/flock.h> 50 #include <sys/swap.h> 51 #include <sys/errno.h> 52 #include <sys/strsubr.h> 53 #include <sys/sysmacros.h> 54 #include <sys/kmem.h> 55 #include <sys/cmn_err.h> 56 #include <sys/pathconf.h> 57 #include <sys/utsname.h> 58 #include <sys/dnlc.h> 59 #include <sys/acl.h> 60 #include <sys/systeminfo.h> 61 #include <sys/policy.h> 62 #include <sys/sdt.h> 63 #include <sys/list.h> 64 #include <sys/stat.h> 65 #include <sys/zone.h> 66 67 #include <rpc/types.h> 68 #include <rpc/auth.h> 69 #include <rpc/clnt.h> 70 71 #include <nfs/nfs.h> 72 #include <nfs/nfs_clnt.h> 73 #include <nfs/nfs_acl.h> 74 #include <nfs/lm.h> 75 #include <nfs/nfs4.h> 76 #include <nfs/nfs4_kprot.h> 77 #include <nfs/rnode4.h> 78 #include <nfs/nfs4_clnt.h> 79 80 #include <vm/hat.h> 81 #include <vm/as.h> 82 #include <vm/page.h> 83 #include <vm/pvn.h> 84 #include <vm/seg.h> 85 #include <vm/seg_map.h> 86 #include <vm/seg_kpm.h> 87 #include <vm/seg_vn.h> 88 89 #include <fs/fs_subr.h> 90 91 #include <sys/ddi.h> 92 #include <sys/int_fmtio.h> 93 #include <sys/fs/autofs.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 struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi); 107 108 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 109 char *, dirattr_info_t *); 110 111 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 112 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 113 nfs4_error_t *, int *); 114 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 115 cred_t *); 116 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 117 stable_how4 *); 118 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 119 cred_t *, bool_t, struct uio *); 120 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 121 vsecattr_t *); 122 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 123 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 124 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 125 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 126 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 127 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 128 int, vnode_t **, cred_t *); 129 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 130 cred_t *, int, int, enum createmode4, int); 131 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 132 caller_context_t *); 133 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 134 vnode_t *, char *, cred_t *, nfsstat4 *); 135 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 136 vnode_t *, char *, cred_t *, nfsstat4 *); 137 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 138 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 139 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 140 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 141 page_t *[], size_t, struct seg *, caddr_t, 142 enum seg_rw, cred_t *); 143 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 144 cred_t *); 145 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 146 int, cred_t *); 147 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 148 int, cred_t *); 149 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 150 static void nfs4_set_mod(vnode_t *); 151 static void nfs4_get_commit(vnode_t *); 152 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 153 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 154 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 155 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 156 cred_t *); 157 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 158 cred_t *); 159 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 160 hrtime_t, vnode_t *, cred_t *); 161 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 162 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 163 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 164 u_offset_t); 165 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 166 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 167 static cred_t *state_to_cred(nfs4_open_stream_t *); 168 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 169 static pid_t lo_to_pid(lock_owner4 *); 170 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 171 cred_t *, nfs4_lock_owner_t *); 172 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 173 nfs4_lock_owner_t *); 174 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **); 175 static void nfs4_delmap_callback(struct as *, void *, uint_t); 176 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 177 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 178 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 179 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 180 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 181 uid_t, gid_t, int); 182 183 /* 184 * Routines that implement the setting of v4 args for the misc. ops 185 */ 186 static void nfs4args_lock_free(nfs_argop4 *); 187 static void nfs4args_lockt_free(nfs_argop4 *); 188 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 189 int, rnode4_t *, cred_t *, bitmap4, int *, 190 nfs4_stateid_types_t *); 191 static void nfs4args_setattr_free(nfs_argop4 *); 192 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 193 bitmap4); 194 static void nfs4args_verify_free(nfs_argop4 *); 195 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 196 WRITE4args **, nfs4_stateid_types_t *); 197 198 /* 199 * These are the vnode ops functions that implement the vnode interface to 200 * the networked file system. See more comments below at nfs4_vnodeops. 201 */ 202 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *); 203 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *, 204 caller_context_t *); 205 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 206 caller_context_t *); 207 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 208 caller_context_t *); 209 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *, 210 caller_context_t *); 211 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 212 caller_context_t *); 213 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *); 214 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *, 215 caller_context_t *); 216 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *); 217 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 218 int, vnode_t **, cred_t *, int, caller_context_t *, 219 vsecattr_t *); 220 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *, 221 int); 222 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *, 223 caller_context_t *, int); 224 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 225 caller_context_t *, int); 226 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **, 227 cred_t *, caller_context_t *, int, vsecattr_t *); 228 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *, 229 caller_context_t *, int); 230 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 231 cred_t *, caller_context_t *, int); 232 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *, 233 caller_context_t *, int); 234 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *); 235 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 236 page_t *[], size_t, struct seg *, caddr_t, 237 enum seg_rw, cred_t *, caller_context_t *); 238 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *, 239 caller_context_t *); 240 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t, 241 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 242 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 243 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 244 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *); 245 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 246 struct flk_callback *, cred_t *, caller_context_t *); 247 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 248 cred_t *, caller_context_t *); 249 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 250 uint_t, uint_t, uint_t, cred_t *, caller_context_t *); 251 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 252 cred_t *, caller_context_t *); 253 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *, 254 caller_context_t *); 255 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 256 caller_context_t *); 257 /* 258 * These vnode ops are required to be called from outside this source file, 259 * e.g. by ephemeral mount stub vnode ops, and so may not be declared 260 * as static. 261 */ 262 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *, 263 caller_context_t *); 264 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *); 265 int nfs4_lookup(vnode_t *, char *, vnode_t **, 266 struct pathname *, int, vnode_t *, cred_t *, 267 caller_context_t *, int *, pathname_t *); 268 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *); 269 int nfs4_rwlock(vnode_t *, int, caller_context_t *); 270 void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 271 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *); 272 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *, 273 caller_context_t *); 274 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 275 caller_context_t *); 276 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *, 277 caller_context_t *); 278 279 /* 280 * Used for nfs4_commit_vp() to indicate if we should 281 * wait on pending writes. 282 */ 283 #define NFS4_WRITE_NOWAIT 0 284 #define NFS4_WRITE_WAIT 1 285 286 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */ 287 288 /* 289 * Error flags used to pass information about certain special errors 290 * which need to be handled specially. 291 */ 292 #define NFS_EOF -98 293 #define NFS_VERF_MISMATCH -97 294 295 /* 296 * Flags used to differentiate between which operation drove the 297 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 298 */ 299 #define NFS4_CLOSE_OP 0x1 300 #define NFS4_DELMAP_OP 0x2 301 #define NFS4_INACTIVE_OP 0x3 302 303 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 304 305 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 306 #define ALIGN64(x, ptr, sz) \ 307 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 308 if (x) { \ 309 x = sizeof (uint64_t) - (x); \ 310 sz -= (x); \ 311 ptr += (x); \ 312 } 313 314 #ifdef DEBUG 315 int nfs4_client_attr_debug = 0; 316 int nfs4_client_state_debug = 0; 317 int nfs4_client_shadow_debug = 0; 318 int nfs4_client_lock_debug = 0; 319 int nfs4_seqid_sync = 0; 320 int nfs4_client_map_debug = 0; 321 static int nfs4_pageio_debug = 0; 322 int nfs4_client_inactive_debug = 0; 323 int nfs4_client_recov_debug = 0; 324 int nfs4_client_failover_debug = 0; 325 int nfs4_client_call_debug = 0; 326 int nfs4_client_lookup_debug = 0; 327 int nfs4_client_zone_debug = 0; 328 int nfs4_lost_rqst_debug = 0; 329 int nfs4_rdattrerr_debug = 0; 330 int nfs4_open_stream_debug = 0; 331 332 int nfs4read_error_inject; 333 334 static int nfs4_create_misses = 0; 335 336 static int nfs4_readdir_cache_shorts = 0; 337 static int nfs4_readdir_readahead = 0; 338 339 static int nfs4_bio_do_stop = 0; 340 341 static int nfs4_lostpage = 0; /* number of times we lost original page */ 342 343 int nfs4_mmap_debug = 0; 344 345 static int nfs4_pathconf_cache_hits = 0; 346 static int nfs4_pathconf_cache_misses = 0; 347 348 int nfs4close_all_cnt; 349 int nfs4close_one_debug = 0; 350 int nfs4close_notw_debug = 0; 351 352 int denied_to_flk_debug = 0; 353 void *lockt_denied_debug; 354 355 #endif 356 357 /* 358 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 359 * or NFS4ERR_RESOURCE. 360 */ 361 static int confirm_retry_sec = 30; 362 363 static int nfs4_lookup_neg_cache = 1; 364 365 /* 366 * number of pages to read ahead 367 * optimized for 100 base-T. 368 */ 369 static int nfs4_nra = 4; 370 371 static int nfs4_do_symlink_cache = 1; 372 373 static int nfs4_pathconf_disable_cache = 0; 374 375 /* 376 * These are the vnode ops routines which implement the vnode interface to 377 * the networked file system. These routines just take their parameters, 378 * make them look networkish by putting the right info into interface structs, 379 * and then calling the appropriate remote routine(s) to do the work. 380 * 381 * Note on directory name lookup cacheing: If we detect a stale fhandle, 382 * we purge the directory cache relative to that vnode. This way, the 383 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 384 * more details on rnode locking. 385 */ 386 387 struct vnodeops *nfs4_vnodeops; 388 389 const fs_operation_def_t nfs4_vnodeops_template[] = { 390 VOPNAME_OPEN, { .vop_open = nfs4_open }, 391 VOPNAME_CLOSE, { .vop_close = nfs4_close }, 392 VOPNAME_READ, { .vop_read = nfs4_read }, 393 VOPNAME_WRITE, { .vop_write = nfs4_write }, 394 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl }, 395 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr }, 396 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr }, 397 VOPNAME_ACCESS, { .vop_access = nfs4_access }, 398 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup }, 399 VOPNAME_CREATE, { .vop_create = nfs4_create }, 400 VOPNAME_REMOVE, { .vop_remove = nfs4_remove }, 401 VOPNAME_LINK, { .vop_link = nfs4_link }, 402 VOPNAME_RENAME, { .vop_rename = nfs4_rename }, 403 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir }, 404 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir }, 405 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir }, 406 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink }, 407 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink }, 408 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync }, 409 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive }, 410 VOPNAME_FID, { .vop_fid = nfs4_fid }, 411 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock }, 412 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock }, 413 VOPNAME_SEEK, { .vop_seek = nfs4_seek }, 414 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock }, 415 VOPNAME_SPACE, { .vop_space = nfs4_space }, 416 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp }, 417 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage }, 418 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage }, 419 VOPNAME_MAP, { .vop_map = nfs4_map }, 420 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap }, 421 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap }, 422 /* no separate nfs4_dump */ 423 VOPNAME_DUMP, { .vop_dump = nfs_dump }, 424 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf }, 425 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio }, 426 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose }, 427 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr }, 428 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr }, 429 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock }, 430 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 431 NULL, NULL 432 }; 433 434 /* 435 * The following are subroutines and definitions to set args or get res 436 * for the different nfsv4 ops 437 */ 438 439 void 440 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 441 { 442 int i; 443 444 for (i = 0; i < arglen; i++) { 445 if (argop[i].argop == OP_LOOKUP) { 446 kmem_free( 447 argop[i].nfs_argop4_u.oplookup. 448 objname.utf8string_val, 449 argop[i].nfs_argop4_u.oplookup. 450 objname.utf8string_len); 451 } 452 } 453 } 454 455 static void 456 nfs4args_lock_free(nfs_argop4 *argop) 457 { 458 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 459 460 if (locker->new_lock_owner == TRUE) { 461 open_to_lock_owner4 *open_owner; 462 463 open_owner = &locker->locker4_u.open_owner; 464 if (open_owner->lock_owner.owner_val != NULL) { 465 kmem_free(open_owner->lock_owner.owner_val, 466 open_owner->lock_owner.owner_len); 467 } 468 } 469 } 470 471 static void 472 nfs4args_lockt_free(nfs_argop4 *argop) 473 { 474 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 475 476 if (lowner->owner_val != NULL) { 477 kmem_free(lowner->owner_val, lowner->owner_len); 478 } 479 } 480 481 static void 482 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 483 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 484 nfs4_stateid_types_t *sid_types) 485 { 486 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 487 mntinfo4_t *mi; 488 489 argop->argop = OP_SETATTR; 490 /* 491 * The stateid is set to 0 if client is not modifying the size 492 * and otherwise to whatever nfs4_get_stateid() returns. 493 * 494 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 495 * state struct could be found for the process/file pair. We may 496 * want to change this in the future (by OPENing the file). See 497 * bug # 4474852. 498 */ 499 if (vap->va_mask & AT_SIZE) { 500 501 ASSERT(rp != NULL); 502 mi = VTOMI4(RTOV4(rp)); 503 504 argop->nfs_argop4_u.opsetattr.stateid = 505 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 506 OP_SETATTR, sid_types, FALSE); 507 } else { 508 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 509 sizeof (stateid4)); 510 } 511 512 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 513 if (*error) 514 bzero(attr, sizeof (*attr)); 515 } 516 517 static void 518 nfs4args_setattr_free(nfs_argop4 *argop) 519 { 520 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 521 } 522 523 static int 524 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 525 bitmap4 supp) 526 { 527 fattr4 *attr; 528 int error = 0; 529 530 argop->argop = op; 531 switch (op) { 532 case OP_VERIFY: 533 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 534 break; 535 case OP_NVERIFY: 536 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 537 break; 538 default: 539 return (EINVAL); 540 } 541 if (!error) 542 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 543 if (error) 544 bzero(attr, sizeof (*attr)); 545 return (error); 546 } 547 548 static void 549 nfs4args_verify_free(nfs_argop4 *argop) 550 { 551 switch (argop->argop) { 552 case OP_VERIFY: 553 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 554 break; 555 case OP_NVERIFY: 556 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 557 break; 558 default: 559 break; 560 } 561 } 562 563 static void 564 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 565 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 566 { 567 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 568 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 569 570 argop->argop = OP_WRITE; 571 wargs->stable = stable; 572 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 573 mi, OP_WRITE, sid_tp); 574 wargs->mblk = NULL; 575 *wargs_pp = wargs; 576 } 577 578 void 579 nfs4args_copen_free(OPEN4cargs *open_args) 580 { 581 if (open_args->owner.owner_val) { 582 kmem_free(open_args->owner.owner_val, 583 open_args->owner.owner_len); 584 } 585 if ((open_args->opentype == OPEN4_CREATE) && 586 (open_args->mode != EXCLUSIVE4)) { 587 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 588 } 589 } 590 591 /* 592 * XXX: This is referenced in modstubs.s 593 */ 594 struct vnodeops * 595 nfs4_getvnodeops(void) 596 { 597 return (nfs4_vnodeops); 598 } 599 600 /* 601 * The OPEN operation opens a regular file. 602 */ 603 /*ARGSUSED3*/ 604 static int 605 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 606 { 607 vnode_t *dvp = NULL; 608 rnode4_t *rp, *drp; 609 int error; 610 int just_been_created; 611 char fn[MAXNAMELEN]; 612 613 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 614 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 615 return (EIO); 616 rp = VTOR4(*vpp); 617 618 /* 619 * Check to see if opening something besides a regular file; 620 * if so skip the OTW call 621 */ 622 if ((*vpp)->v_type != VREG) { 623 error = nfs4_open_non_reg_file(vpp, flag, cr); 624 return (error); 625 } 626 627 /* 628 * XXX - would like a check right here to know if the file is 629 * executable or not, so as to skip OTW 630 */ 631 632 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 633 return (error); 634 635 drp = VTOR4(dvp); 636 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 637 return (EINTR); 638 639 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) { 640 nfs_rw_exit(&drp->r_rwlock); 641 return (error); 642 } 643 644 /* 645 * See if this file has just been CREATEd. 646 * If so, clear the flag and update the dnlc, which was previously 647 * skipped in nfs4_create. 648 * XXX need better serilization on this. 649 * XXX move this into the nf4open_otw call, after we have 650 * XXX acquired the open owner seqid sync. 651 */ 652 mutex_enter(&rp->r_statev4_lock); 653 if (rp->created_v4) { 654 rp->created_v4 = 0; 655 mutex_exit(&rp->r_statev4_lock); 656 657 dnlc_update(dvp, fn, *vpp); 658 /* This is needed so we don't bump the open ref count */ 659 just_been_created = 1; 660 } else { 661 mutex_exit(&rp->r_statev4_lock); 662 just_been_created = 0; 663 } 664 665 /* 666 * If caller specified O_TRUNC/FTRUNC, then be sure to set 667 * FWRITE (to drive successful setattr(size=0) after open) 668 */ 669 if (flag & FTRUNC) 670 flag |= FWRITE; 671 672 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 673 just_been_created); 674 675 if (!error && !((*vpp)->v_flag & VROOT)) 676 dnlc_update(dvp, fn, *vpp); 677 678 nfs_rw_exit(&drp->r_rwlock); 679 680 /* release the hold from vtodv */ 681 VN_RELE(dvp); 682 683 /* exchange the shadow for the master vnode, if needed */ 684 685 if (error == 0 && IS_SHADOW(*vpp, rp)) 686 sv_exchange(vpp); 687 688 return (error); 689 } 690 691 /* 692 * See if there's a "lost open" request to be saved and recovered. 693 */ 694 static void 695 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 696 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 697 vnode_t *dvp, OPEN4cargs *open_args) 698 { 699 vfs_t *vfsp; 700 char *srccfp; 701 702 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 703 704 if (error != ETIMEDOUT && error != EINTR && 705 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 706 lost_rqstp->lr_op = 0; 707 return; 708 } 709 710 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 711 "nfs4open_save_lost_rqst: error %d", error)); 712 713 lost_rqstp->lr_op = OP_OPEN; 714 715 /* 716 * The vp (if it is not NULL) and dvp are held and rele'd via 717 * the recovery code. See nfs4_save_lost_rqst. 718 */ 719 lost_rqstp->lr_vp = vp; 720 lost_rqstp->lr_dvp = dvp; 721 lost_rqstp->lr_oop = oop; 722 lost_rqstp->lr_osp = NULL; 723 lost_rqstp->lr_lop = NULL; 724 lost_rqstp->lr_cr = cr; 725 lost_rqstp->lr_flk = NULL; 726 lost_rqstp->lr_oacc = open_args->share_access; 727 lost_rqstp->lr_odeny = open_args->share_deny; 728 lost_rqstp->lr_oclaim = open_args->claim; 729 if (open_args->claim == CLAIM_DELEGATE_CUR) { 730 lost_rqstp->lr_ostateid = 731 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 732 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 733 } else { 734 srccfp = open_args->open_claim4_u.cfile; 735 } 736 lost_rqstp->lr_ofile.utf8string_len = 0; 737 lost_rqstp->lr_ofile.utf8string_val = NULL; 738 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 739 lost_rqstp->lr_putfirst = FALSE; 740 } 741 742 struct nfs4_excl_time { 743 uint32 seconds; 744 uint32 nseconds; 745 }; 746 747 /* 748 * The OPEN operation creates and/or opens a regular file 749 * 750 * ARGSUSED 751 */ 752 static int 753 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 754 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 755 enum createmode4 createmode, int file_just_been_created) 756 { 757 rnode4_t *rp; 758 rnode4_t *drp = VTOR4(dvp); 759 vnode_t *vp = NULL; 760 vnode_t *vpi = *vpp; 761 bool_t needrecov = FALSE; 762 763 int doqueue = 1; 764 765 COMPOUND4args_clnt args; 766 COMPOUND4res_clnt res; 767 nfs_argop4 *argop; 768 nfs_resop4 *resop; 769 int argoplist_size; 770 int idx_open, idx_fattr; 771 772 GETFH4res *gf_res = NULL; 773 OPEN4res *op_res = NULL; 774 nfs4_ga_res_t *garp; 775 fattr4 *attr = NULL; 776 struct nfs4_excl_time verf; 777 bool_t did_excl_setup = FALSE; 778 int created_osp; 779 780 OPEN4cargs *open_args; 781 nfs4_open_owner_t *oop = NULL; 782 nfs4_open_stream_t *osp = NULL; 783 seqid4 seqid = 0; 784 bool_t retry_open = FALSE; 785 nfs4_recov_state_t recov_state; 786 nfs4_lost_rqst_t lost_rqst; 787 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 788 hrtime_t t; 789 int acc = 0; 790 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 791 cred_t *ncr = NULL; 792 793 nfs4_sharedfh_t *otw_sfh; 794 nfs4_sharedfh_t *orig_sfh; 795 int fh_differs = 0; 796 int numops, setgid_flag; 797 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 798 799 /* 800 * Make sure we properly deal with setting the right gid on 801 * a newly created file to reflect the parent's setgid bit 802 */ 803 setgid_flag = 0; 804 if (create_flag && in_va) { 805 806 /* 807 * If there is grpid mount flag used or 808 * the parent's directory has the setgid bit set 809 * _and_ the client was able to get a valid mapping 810 * for the parent dir's owner_group, we want to 811 * append NVERIFY(owner_group == dva.va_gid) and 812 * SETATTR to the CREATE compound. 813 */ 814 mutex_enter(&drp->r_statelock); 815 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID || 816 drp->r_attr.va_mode & VSGID) && 817 drp->r_attr.va_gid != GID_NOBODY) { 818 in_va->va_mask |= AT_GID; 819 in_va->va_gid = drp->r_attr.va_gid; 820 setgid_flag = 1; 821 } 822 mutex_exit(&drp->r_statelock); 823 } 824 825 /* 826 * Normal/non-create compound: 827 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 828 * 829 * Open(create) compound no setgid: 830 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 831 * RESTOREFH + GETATTR 832 * 833 * Open(create) setgid: 834 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 835 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 836 * NVERIFY(grp) + SETATTR 837 */ 838 if (setgid_flag) { 839 numops = 10; 840 idx_open = 1; 841 idx_fattr = 3; 842 } else if (create_flag) { 843 numops = 7; 844 idx_open = 2; 845 idx_fattr = 4; 846 } else { 847 numops = 4; 848 idx_open = 1; 849 idx_fattr = 3; 850 } 851 852 args.array_len = numops; 853 argoplist_size = numops * sizeof (nfs_argop4); 854 argop = kmem_alloc(argoplist_size, KM_SLEEP); 855 856 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 857 "open %s open flag 0x%x cred %p", file_name, open_flag, 858 (void *)cr)); 859 860 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 861 if (create_flag) { 862 /* 863 * We are to create a file. Initialize the passed in vnode 864 * pointer. 865 */ 866 vpi = NULL; 867 } else { 868 /* 869 * Check to see if the client owns a read delegation and is 870 * trying to open for write. If so, then return the delegation 871 * to avoid the server doing a cb_recall and returning DELAY. 872 * NB - we don't use the statev4_lock here because we'd have 873 * to drop the lock anyway and the result would be stale. 874 */ 875 if ((open_flag & FWRITE) && 876 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 877 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 878 879 /* 880 * If the file has a delegation, then do an access check up 881 * front. This avoids having to an access check later after 882 * we've already done start_op, which could deadlock. 883 */ 884 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 885 if (open_flag & FREAD && 886 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0) 887 acc |= VREAD; 888 if (open_flag & FWRITE && 889 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0) 890 acc |= VWRITE; 891 } 892 } 893 894 drp = VTOR4(dvp); 895 896 recov_state.rs_flags = 0; 897 recov_state.rs_num_retry_despite_err = 0; 898 cred_otw = cr; 899 900 recov_retry: 901 fh_differs = 0; 902 nfs4_error_zinit(&e); 903 904 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 905 if (e.error) { 906 if (ncr != NULL) 907 crfree(ncr); 908 kmem_free(argop, argoplist_size); 909 return (e.error); 910 } 911 912 args.ctag = TAG_OPEN; 913 args.array_len = numops; 914 args.array = argop; 915 916 /* putfh directory fh */ 917 argop[0].argop = OP_CPUTFH; 918 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 919 920 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 921 argop[idx_open].argop = OP_COPEN; 922 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 923 open_args->claim = CLAIM_NULL; 924 925 /* name of file */ 926 open_args->open_claim4_u.cfile = file_name; 927 open_args->owner.owner_len = 0; 928 open_args->owner.owner_val = NULL; 929 930 if (create_flag) { 931 /* CREATE a file */ 932 open_args->opentype = OPEN4_CREATE; 933 open_args->mode = createmode; 934 if (createmode == EXCLUSIVE4) { 935 if (did_excl_setup == FALSE) { 936 verf.seconds = zone_get_hostid(NULL); 937 if (verf.seconds != 0) 938 verf.nseconds = newnum(); 939 else { 940 timestruc_t now; 941 942 gethrestime(&now); 943 verf.seconds = now.tv_sec; 944 verf.nseconds = now.tv_nsec; 945 } 946 /* 947 * Since the server will use this value for the 948 * mtime, make sure that it can't overflow. Zero 949 * out the MSB. The actual value does not matter 950 * here, only its uniqeness. 951 */ 952 verf.seconds &= INT32_MAX; 953 did_excl_setup = TRUE; 954 } 955 956 /* Now copy over verifier to OPEN4args. */ 957 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 958 } else { 959 int v_error; 960 bitmap4 supp_attrs; 961 servinfo4_t *svp; 962 963 attr = &open_args->createhow4_u.createattrs; 964 965 svp = drp->r_server; 966 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 967 supp_attrs = svp->sv_supp_attrs; 968 nfs_rw_exit(&svp->sv_lock); 969 970 /* GUARDED4 or UNCHECKED4 */ 971 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 972 supp_attrs); 973 if (v_error) { 974 bzero(attr, sizeof (*attr)); 975 nfs4args_copen_free(open_args); 976 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 977 &recov_state, FALSE); 978 if (ncr != NULL) 979 crfree(ncr); 980 kmem_free(argop, argoplist_size); 981 return (v_error); 982 } 983 } 984 } else { 985 /* NO CREATE */ 986 open_args->opentype = OPEN4_NOCREATE; 987 } 988 989 if (recov_state.rs_sp != NULL) { 990 mutex_enter(&recov_state.rs_sp->s_lock); 991 open_args->owner.clientid = recov_state.rs_sp->clientid; 992 mutex_exit(&recov_state.rs_sp->s_lock); 993 } else { 994 /* XXX should we just fail here? */ 995 open_args->owner.clientid = 0; 996 } 997 998 /* 999 * This increments oop's ref count or creates a temporary 'just_created' 1000 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 1001 * completes. 1002 */ 1003 mutex_enter(&VTOMI4(dvp)->mi_lock); 1004 1005 /* See if a permanent or just created open owner exists */ 1006 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 1007 if (!oop) { 1008 /* 1009 * This open owner does not exist so create a temporary 1010 * just created one. 1011 */ 1012 oop = create_open_owner(cr, VTOMI4(dvp)); 1013 ASSERT(oop != NULL); 1014 } 1015 mutex_exit(&VTOMI4(dvp)->mi_lock); 1016 1017 /* this length never changes, do alloc before seqid sync */ 1018 open_args->owner.owner_len = sizeof (oop->oo_name); 1019 open_args->owner.owner_val = 1020 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1021 1022 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 1023 if (e.error == EAGAIN) { 1024 open_owner_rele(oop); 1025 nfs4args_copen_free(open_args); 1026 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1027 if (ncr != NULL) { 1028 crfree(ncr); 1029 ncr = NULL; 1030 } 1031 goto recov_retry; 1032 } 1033 1034 /* Check to see if we need to do the OTW call */ 1035 if (!create_flag) { 1036 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1037 file_just_been_created, &e.error, acc, &recov_state)) { 1038 1039 /* 1040 * The OTW open is not necessary. Either 1041 * the open can succeed without it (eg. 1042 * delegation, error == 0) or the open 1043 * must fail due to an access failure 1044 * (error != 0). In either case, tidy 1045 * up and return. 1046 */ 1047 1048 nfs4_end_open_seqid_sync(oop); 1049 open_owner_rele(oop); 1050 nfs4args_copen_free(open_args); 1051 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1052 if (ncr != NULL) 1053 crfree(ncr); 1054 kmem_free(argop, argoplist_size); 1055 return (e.error); 1056 } 1057 } 1058 1059 bcopy(&oop->oo_name, open_args->owner.owner_val, 1060 open_args->owner.owner_len); 1061 1062 seqid = nfs4_get_open_seqid(oop) + 1; 1063 open_args->seqid = seqid; 1064 open_args->share_access = 0; 1065 if (open_flag & FREAD) 1066 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1067 if (open_flag & FWRITE) 1068 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1069 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1070 1071 1072 1073 /* 1074 * getfh w/sanity check for idx_open/idx_fattr 1075 */ 1076 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1077 argop[idx_open + 1].argop = OP_GETFH; 1078 1079 /* getattr */ 1080 argop[idx_fattr].argop = OP_GETATTR; 1081 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1082 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1083 1084 if (setgid_flag) { 1085 vattr_t _v; 1086 servinfo4_t *svp; 1087 bitmap4 supp_attrs; 1088 1089 svp = drp->r_server; 1090 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1091 supp_attrs = svp->sv_supp_attrs; 1092 nfs_rw_exit(&svp->sv_lock); 1093 1094 /* 1095 * For setgid case, we need to: 1096 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1097 */ 1098 argop[4].argop = OP_SAVEFH; 1099 1100 argop[5].argop = OP_CPUTFH; 1101 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1102 1103 argop[6].argop = OP_GETATTR; 1104 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1105 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1106 1107 argop[7].argop = OP_RESTOREFH; 1108 1109 /* 1110 * nverify 1111 */ 1112 _v.va_mask = AT_GID; 1113 _v.va_gid = in_va->va_gid; 1114 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1115 supp_attrs))) { 1116 1117 /* 1118 * setattr 1119 * 1120 * We _know_ we're not messing with AT_SIZE or 1121 * AT_XTIME, so no need for stateid or flags. 1122 * Also we specify NULL rp since we're only 1123 * interested in setting owner_group attributes. 1124 */ 1125 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1126 supp_attrs, &e.error, 0); 1127 if (e.error) 1128 nfs4args_verify_free(&argop[8]); 1129 } 1130 1131 if (e.error) { 1132 /* 1133 * XXX - Revisit the last argument to nfs4_end_op() 1134 * once 5020486 is fixed. 1135 */ 1136 nfs4_end_open_seqid_sync(oop); 1137 open_owner_rele(oop); 1138 nfs4args_copen_free(open_args); 1139 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1140 if (ncr != NULL) 1141 crfree(ncr); 1142 kmem_free(argop, argoplist_size); 1143 return (e.error); 1144 } 1145 } else if (create_flag) { 1146 argop[1].argop = OP_SAVEFH; 1147 1148 argop[5].argop = OP_RESTOREFH; 1149 1150 argop[6].argop = OP_GETATTR; 1151 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1152 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1153 } 1154 1155 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1156 "nfs4open_otw: %s call, nm %s, rp %s", 1157 needrecov ? "recov" : "first", file_name, 1158 rnode4info(VTOR4(dvp)))); 1159 1160 t = gethrtime(); 1161 1162 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1163 1164 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1165 nfs4_set_open_seqid(seqid, oop, args.ctag); 1166 1167 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1168 1169 if (e.error || needrecov) { 1170 bool_t abort = FALSE; 1171 1172 if (needrecov) { 1173 nfs4_bseqid_entry_t *bsep = NULL; 1174 1175 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1176 cred_otw, vpi, dvp, open_args); 1177 1178 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1179 bsep = nfs4_create_bseqid_entry(oop, NULL, 1180 vpi, 0, args.ctag, open_args->seqid); 1181 num_bseqid_retry--; 1182 } 1183 1184 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1185 NULL, lost_rqst.lr_op == OP_OPEN ? 1186 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL); 1187 1188 if (bsep) 1189 kmem_free(bsep, sizeof (*bsep)); 1190 /* give up if we keep getting BAD_SEQID */ 1191 if (num_bseqid_retry == 0) 1192 abort = TRUE; 1193 if (abort == TRUE && e.error == 0) 1194 e.error = geterrno4(res.status); 1195 } 1196 nfs4_end_open_seqid_sync(oop); 1197 open_owner_rele(oop); 1198 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1199 nfs4args_copen_free(open_args); 1200 if (setgid_flag) { 1201 nfs4args_verify_free(&argop[8]); 1202 nfs4args_setattr_free(&argop[9]); 1203 } 1204 if (!e.error) 1205 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1206 if (ncr != NULL) { 1207 crfree(ncr); 1208 ncr = NULL; 1209 } 1210 if (!needrecov || abort == TRUE || e.error == EINTR || 1211 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1212 kmem_free(argop, argoplist_size); 1213 return (e.error); 1214 } 1215 goto recov_retry; 1216 } 1217 1218 /* 1219 * Will check and update lease after checking the rflag for 1220 * OPEN_CONFIRM in the successful OPEN call. 1221 */ 1222 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1223 1224 /* 1225 * XXX what if we're crossing mount points from server1:/drp 1226 * to server2:/drp/rp. 1227 */ 1228 1229 /* Signal our end of use of the open seqid */ 1230 nfs4_end_open_seqid_sync(oop); 1231 1232 /* 1233 * This will destroy the open owner if it was just created, 1234 * and no one else has put a reference on it. 1235 */ 1236 open_owner_rele(oop); 1237 if (create_flag && (createmode != EXCLUSIVE4) && 1238 res.status == NFS4ERR_BADOWNER) 1239 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1240 1241 e.error = geterrno4(res.status); 1242 nfs4args_copen_free(open_args); 1243 if (setgid_flag) { 1244 nfs4args_verify_free(&argop[8]); 1245 nfs4args_setattr_free(&argop[9]); 1246 } 1247 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1248 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1249 /* 1250 * If the reply is NFS4ERR_ACCESS, it may be because 1251 * we are root (no root net access). If the real uid 1252 * is not root, then retry with the real uid instead. 1253 */ 1254 if (ncr != NULL) { 1255 crfree(ncr); 1256 ncr = NULL; 1257 } 1258 if (res.status == NFS4ERR_ACCESS && 1259 (ncr = crnetadjust(cred_otw)) != NULL) { 1260 cred_otw = ncr; 1261 goto recov_retry; 1262 } 1263 kmem_free(argop, argoplist_size); 1264 return (e.error); 1265 } 1266 1267 resop = &res.array[idx_open]; /* open res */ 1268 op_res = &resop->nfs_resop4_u.opopen; 1269 1270 #ifdef DEBUG 1271 /* 1272 * verify attrset bitmap 1273 */ 1274 if (create_flag && 1275 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1276 /* make sure attrset returned is what we asked for */ 1277 /* XXX Ignore this 'error' for now */ 1278 if (attr->attrmask != op_res->attrset) 1279 /* EMPTY */; 1280 } 1281 #endif 1282 1283 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1284 mutex_enter(&VTOMI4(dvp)->mi_lock); 1285 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1286 mutex_exit(&VTOMI4(dvp)->mi_lock); 1287 } 1288 1289 resop = &res.array[idx_open + 1]; /* getfh res */ 1290 gf_res = &resop->nfs_resop4_u.opgetfh; 1291 1292 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1293 1294 /* 1295 * The open stateid has been updated on the server but not 1296 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1297 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1298 * WRITE call. That, however, will use the old stateid, so go ahead 1299 * and upate the open stateid now, before any call to makenfs4node. 1300 */ 1301 if (vpi) { 1302 nfs4_open_stream_t *tmp_osp; 1303 rnode4_t *tmp_rp = VTOR4(vpi); 1304 1305 tmp_osp = find_open_stream(oop, tmp_rp); 1306 if (tmp_osp) { 1307 tmp_osp->open_stateid = op_res->stateid; 1308 mutex_exit(&tmp_osp->os_sync_lock); 1309 open_stream_rele(tmp_osp, tmp_rp); 1310 } 1311 1312 /* 1313 * We must determine if the file handle given by the otw open 1314 * is the same as the file handle which was passed in with 1315 * *vpp. This case can be reached if the file we are trying 1316 * to open has been removed and another file has been created 1317 * having the same file name. The passed in vnode is released 1318 * later. 1319 */ 1320 orig_sfh = VTOR4(vpi)->r_fh; 1321 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1322 } 1323 1324 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1325 1326 if (create_flag || fh_differs) { 1327 int rnode_err = 0; 1328 1329 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1330 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh)); 1331 1332 if (e.error) 1333 PURGE_ATTRCACHE4(vp); 1334 /* 1335 * For the newly created vp case, make sure the rnode 1336 * isn't bad before using it. 1337 */ 1338 mutex_enter(&(VTOR4(vp))->r_statelock); 1339 if (VTOR4(vp)->r_flags & R4RECOVERR) 1340 rnode_err = EIO; 1341 mutex_exit(&(VTOR4(vp))->r_statelock); 1342 1343 if (rnode_err) { 1344 nfs4_end_open_seqid_sync(oop); 1345 nfs4args_copen_free(open_args); 1346 if (setgid_flag) { 1347 nfs4args_verify_free(&argop[8]); 1348 nfs4args_setattr_free(&argop[9]); 1349 } 1350 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1351 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1352 needrecov); 1353 open_owner_rele(oop); 1354 VN_RELE(vp); 1355 if (ncr != NULL) 1356 crfree(ncr); 1357 sfh4_rele(&otw_sfh); 1358 kmem_free(argop, argoplist_size); 1359 return (EIO); 1360 } 1361 } else { 1362 vp = vpi; 1363 } 1364 sfh4_rele(&otw_sfh); 1365 1366 /* 1367 * It seems odd to get a full set of attrs and then not update 1368 * the object's attrcache in the non-create case. Create case uses 1369 * the attrs since makenfs4node checks to see if the attrs need to 1370 * be updated (and then updates them). The non-create case should 1371 * update attrs also. 1372 */ 1373 if (! create_flag && ! fh_differs && !e.error) { 1374 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1375 } 1376 1377 nfs4_error_zinit(&e); 1378 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1379 /* This does not do recovery for vp explicitly. */ 1380 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1381 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1382 1383 if (e.error || e.stat) { 1384 nfs4_end_open_seqid_sync(oop); 1385 nfs4args_copen_free(open_args); 1386 if (setgid_flag) { 1387 nfs4args_verify_free(&argop[8]); 1388 nfs4args_setattr_free(&argop[9]); 1389 } 1390 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1391 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1392 needrecov); 1393 open_owner_rele(oop); 1394 if (create_flag || fh_differs) { 1395 /* rele the makenfs4node */ 1396 VN_RELE(vp); 1397 } 1398 if (ncr != NULL) { 1399 crfree(ncr); 1400 ncr = NULL; 1401 } 1402 if (retry_open == TRUE) { 1403 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1404 "nfs4open_otw: retry the open since OPEN " 1405 "CONFIRM failed with error %d stat %d", 1406 e.error, e.stat)); 1407 if (create_flag && createmode == GUARDED4) { 1408 NFS4_DEBUG(nfs4_client_recov_debug, 1409 (CE_NOTE, "nfs4open_otw: switch " 1410 "createmode from GUARDED4 to " 1411 "UNCHECKED4")); 1412 createmode = UNCHECKED4; 1413 } 1414 goto recov_retry; 1415 } 1416 if (!e.error) { 1417 if (create_flag && (createmode != EXCLUSIVE4) && 1418 e.stat == NFS4ERR_BADOWNER) 1419 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1420 1421 e.error = geterrno4(e.stat); 1422 } 1423 kmem_free(argop, argoplist_size); 1424 return (e.error); 1425 } 1426 } 1427 1428 rp = VTOR4(vp); 1429 1430 mutex_enter(&rp->r_statev4_lock); 1431 if (create_flag) 1432 rp->created_v4 = 1; 1433 mutex_exit(&rp->r_statev4_lock); 1434 1435 mutex_enter(&oop->oo_lock); 1436 /* Doesn't matter if 'oo_just_created' already was set as this */ 1437 oop->oo_just_created = NFS4_PERM_CREATED; 1438 if (oop->oo_cred_otw) 1439 crfree(oop->oo_cred_otw); 1440 oop->oo_cred_otw = cred_otw; 1441 crhold(oop->oo_cred_otw); 1442 mutex_exit(&oop->oo_lock); 1443 1444 /* returns with 'os_sync_lock' held */ 1445 osp = find_or_create_open_stream(oop, rp, &created_osp); 1446 if (!osp) { 1447 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1448 "nfs4open_otw: failed to create an open stream")); 1449 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1450 "signal our end of use of the open seqid")); 1451 1452 nfs4_end_open_seqid_sync(oop); 1453 open_owner_rele(oop); 1454 nfs4args_copen_free(open_args); 1455 if (setgid_flag) { 1456 nfs4args_verify_free(&argop[8]); 1457 nfs4args_setattr_free(&argop[9]); 1458 } 1459 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1460 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1461 if (create_flag || fh_differs) 1462 VN_RELE(vp); 1463 if (ncr != NULL) 1464 crfree(ncr); 1465 1466 kmem_free(argop, argoplist_size); 1467 return (EINVAL); 1468 1469 } 1470 1471 osp->open_stateid = op_res->stateid; 1472 1473 if (open_flag & FREAD) 1474 osp->os_share_acc_read++; 1475 if (open_flag & FWRITE) 1476 osp->os_share_acc_write++; 1477 osp->os_share_deny_none++; 1478 1479 /* 1480 * Need to reset this bitfield for the possible case where we were 1481 * going to OTW CLOSE the file, got a non-recoverable error, and before 1482 * we could retry the CLOSE, OPENed the file again. 1483 */ 1484 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1485 osp->os_final_close = 0; 1486 osp->os_force_close = 0; 1487 #ifdef DEBUG 1488 if (osp->os_failed_reopen) 1489 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1490 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1491 (void *)osp, (void *)cr, rnode4info(rp))); 1492 #endif 1493 osp->os_failed_reopen = 0; 1494 1495 mutex_exit(&osp->os_sync_lock); 1496 1497 nfs4_end_open_seqid_sync(oop); 1498 1499 if (created_osp && recov_state.rs_sp != NULL) { 1500 mutex_enter(&recov_state.rs_sp->s_lock); 1501 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1502 mutex_exit(&recov_state.rs_sp->s_lock); 1503 } 1504 1505 /* get rid of our reference to find oop */ 1506 open_owner_rele(oop); 1507 1508 open_stream_rele(osp, rp); 1509 1510 /* accept delegation, if any */ 1511 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1512 1513 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1514 1515 if (createmode == EXCLUSIVE4 && 1516 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1517 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1518 " EXCLUSIVE4: sending a SETATTR")); 1519 /* 1520 * If doing an exclusive create, then generate 1521 * a SETATTR to set the initial attributes. 1522 * Try to set the mtime and the atime to the 1523 * server's current time. It is somewhat 1524 * expected that these fields will be used to 1525 * store the exclusive create cookie. If not, 1526 * server implementors will need to know that 1527 * a SETATTR will follow an exclusive create 1528 * and the cookie should be destroyed if 1529 * appropriate. 1530 * 1531 * The AT_GID and AT_SIZE bits are turned off 1532 * so that the SETATTR request will not attempt 1533 * to process these. The gid will be set 1534 * separately if appropriate. The size is turned 1535 * off because it is assumed that a new file will 1536 * be created empty and if the file wasn't empty, 1537 * then the exclusive create will have failed 1538 * because the file must have existed already. 1539 * Therefore, no truncate operation is needed. 1540 */ 1541 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1542 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1543 1544 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1545 if (e.error) { 1546 /* 1547 * Couldn't correct the attributes of 1548 * the newly created file and the 1549 * attributes are wrong. Remove the 1550 * file and return an error to the 1551 * application. 1552 */ 1553 /* XXX will this take care of client state ? */ 1554 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1555 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1556 " remove file", e.error)); 1557 VN_RELE(vp); 1558 (void) nfs4_remove(dvp, file_name, cr, NULL, 0); 1559 /* 1560 * Since we've reled the vnode and removed 1561 * the file we now need to return the error. 1562 * At this point we don't want to update the 1563 * dircaches, call nfs4_waitfor_purge_complete 1564 * or set vpp to vp so we need to skip these 1565 * as well. 1566 */ 1567 goto skip_update_dircaches; 1568 } 1569 } 1570 1571 /* 1572 * If we created or found the correct vnode, due to create_flag or 1573 * fh_differs being set, then update directory cache attribute, readdir 1574 * and dnlc caches. 1575 */ 1576 if (create_flag || fh_differs) { 1577 dirattr_info_t dinfo, *dinfop; 1578 1579 /* 1580 * Make sure getattr succeeded before using results. 1581 * note: op 7 is getattr(dir) for both flavors of 1582 * open(create). 1583 */ 1584 if (create_flag && res.status == NFS4_OK) { 1585 dinfo.di_time_call = t; 1586 dinfo.di_cred = cr; 1587 dinfo.di_garp = 1588 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1589 dinfop = &dinfo; 1590 } else { 1591 dinfop = NULL; 1592 } 1593 1594 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1595 dinfop); 1596 } 1597 1598 /* 1599 * If the page cache for this file was flushed from actions 1600 * above, it was done asynchronously and if that is true, 1601 * there is a need to wait here for it to complete. This must 1602 * be done outside of start_fop/end_fop. 1603 */ 1604 (void) nfs4_waitfor_purge_complete(vp); 1605 1606 /* 1607 * It is implicit that we are in the open case (create_flag == 0) since 1608 * fh_differs can only be set to a non-zero value in the open case. 1609 */ 1610 if (fh_differs != 0 && vpi != NULL) 1611 VN_RELE(vpi); 1612 1613 /* 1614 * Be sure to set *vpp to the correct value before returning. 1615 */ 1616 *vpp = vp; 1617 1618 skip_update_dircaches: 1619 1620 nfs4args_copen_free(open_args); 1621 if (setgid_flag) { 1622 nfs4args_verify_free(&argop[8]); 1623 nfs4args_setattr_free(&argop[9]); 1624 } 1625 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1626 1627 if (ncr) 1628 crfree(ncr); 1629 kmem_free(argop, argoplist_size); 1630 return (e.error); 1631 } 1632 1633 /* 1634 * Reopen an open instance. cf. nfs4open_otw(). 1635 * 1636 * Errors are returned by the nfs4_error_t parameter. 1637 * - ep->error contains an errno value or zero. 1638 * - if it is zero, ep->stat is set to an NFS status code, if any. 1639 * If the file could not be reopened, but the caller should continue, the 1640 * file is marked dead and no error values are returned. If the caller 1641 * should stop recovering open files and start over, either the ep->error 1642 * value or ep->stat will indicate an error (either something that requires 1643 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1644 * filehandles) may be handled silently by this routine. 1645 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1646 * will be started, so the caller should not do it. 1647 * 1648 * Gotos: 1649 * - kill_file : reopen failed in such a fashion to constitute marking the 1650 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1651 * is for cases where recovery is not possible. 1652 * - failed_reopen : same as above, except that the file has already been 1653 * marked dead, so no need to do it again. 1654 * - bailout : reopen failed but we are able to recover and retry the reopen - 1655 * either within this function immediately or via the calling function. 1656 */ 1657 1658 void 1659 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1660 open_claim_type4 claim, bool_t frc_use_claim_previous, 1661 bool_t is_recov) 1662 { 1663 COMPOUND4args_clnt args; 1664 COMPOUND4res_clnt res; 1665 nfs_argop4 argop[4]; 1666 nfs_resop4 *resop; 1667 OPEN4res *op_res = NULL; 1668 OPEN4cargs *open_args; 1669 GETFH4res *gf_res; 1670 rnode4_t *rp = VTOR4(vp); 1671 int doqueue = 1; 1672 cred_t *cr = NULL, *cred_otw = NULL; 1673 nfs4_open_owner_t *oop = NULL; 1674 seqid4 seqid; 1675 nfs4_ga_res_t *garp; 1676 char fn[MAXNAMELEN]; 1677 nfs4_recov_state_t recov = {NULL, 0}; 1678 nfs4_lost_rqst_t lost_rqst; 1679 mntinfo4_t *mi = VTOMI4(vp); 1680 bool_t abort; 1681 char *failed_msg = ""; 1682 int fh_different; 1683 hrtime_t t; 1684 nfs4_bseqid_entry_t *bsep = NULL; 1685 1686 ASSERT(nfs4_consistent_type(vp)); 1687 ASSERT(nfs_zone() == mi->mi_zone); 1688 1689 nfs4_error_zinit(ep); 1690 1691 /* this is the cred used to find the open owner */ 1692 cr = state_to_cred(osp); 1693 if (cr == NULL) { 1694 failed_msg = "Couldn't reopen: no cred"; 1695 goto kill_file; 1696 } 1697 /* use this cred for OTW operations */ 1698 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1699 1700 top: 1701 nfs4_error_zinit(ep); 1702 1703 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1704 /* File system has been unmounted, quit */ 1705 ep->error = EIO; 1706 failed_msg = "Couldn't reopen: file system has been unmounted"; 1707 goto kill_file; 1708 } 1709 1710 oop = osp->os_open_owner; 1711 1712 ASSERT(oop != NULL); 1713 if (oop == NULL) { /* be defensive in non-DEBUG */ 1714 failed_msg = "can't reopen: no open owner"; 1715 goto kill_file; 1716 } 1717 open_owner_hold(oop); 1718 1719 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1720 if (ep->error) { 1721 open_owner_rele(oop); 1722 oop = NULL; 1723 goto bailout; 1724 } 1725 1726 /* 1727 * If the rnode has a delegation and the delegation has been 1728 * recovered and the server didn't request a recall and the caller 1729 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1730 * recovery) and the rnode hasn't been marked dead, then install 1731 * the delegation stateid in the open stream. Otherwise, proceed 1732 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1733 */ 1734 mutex_enter(&rp->r_statev4_lock); 1735 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1736 !rp->r_deleg_return_pending && 1737 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1738 !rp->r_deleg_needs_recall && 1739 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1740 !(rp->r_flags & R4RECOVERR)) { 1741 mutex_enter(&osp->os_sync_lock); 1742 osp->os_delegation = 1; 1743 osp->open_stateid = rp->r_deleg_stateid; 1744 mutex_exit(&osp->os_sync_lock); 1745 mutex_exit(&rp->r_statev4_lock); 1746 goto bailout; 1747 } 1748 mutex_exit(&rp->r_statev4_lock); 1749 1750 /* 1751 * If the file failed recovery, just quit. This failure need not 1752 * affect other reopens, so don't return an error. 1753 */ 1754 mutex_enter(&rp->r_statelock); 1755 if (rp->r_flags & R4RECOVERR) { 1756 mutex_exit(&rp->r_statelock); 1757 ep->error = 0; 1758 goto failed_reopen; 1759 } 1760 mutex_exit(&rp->r_statelock); 1761 1762 /* 1763 * argop is empty here 1764 * 1765 * PUTFH, OPEN, GETATTR 1766 */ 1767 args.ctag = TAG_REOPEN; 1768 args.array_len = 4; 1769 args.array = argop; 1770 1771 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1772 "nfs4_reopen: file is type %d, id %s", 1773 vp->v_type, rnode4info(VTOR4(vp)))); 1774 1775 argop[0].argop = OP_CPUTFH; 1776 1777 if (claim != CLAIM_PREVIOUS) { 1778 /* 1779 * if this is a file mount then 1780 * use the mntinfo parentfh 1781 */ 1782 argop[0].nfs_argop4_u.opcputfh.sfh = 1783 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1784 VTOSV(vp)->sv_dfh; 1785 } else { 1786 /* putfh fh to reopen */ 1787 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1788 } 1789 1790 argop[1].argop = OP_COPEN; 1791 open_args = &argop[1].nfs_argop4_u.opcopen; 1792 open_args->claim = claim; 1793 1794 if (claim == CLAIM_NULL) { 1795 1796 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1797 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1798 "failed for vp 0x%p for CLAIM_NULL with %m", 1799 (void *)vp); 1800 failed_msg = "Couldn't reopen: vtoname failed for " 1801 "CLAIM_NULL"; 1802 /* nothing allocated yet */ 1803 goto kill_file; 1804 } 1805 1806 open_args->open_claim4_u.cfile = fn; 1807 } else if (claim == CLAIM_PREVIOUS) { 1808 1809 /* 1810 * We have two cases to deal with here: 1811 * 1) We're being called to reopen files in order to satisfy 1812 * a lock operation request which requires us to explicitly 1813 * reopen files which were opened under a delegation. If 1814 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1815 * that case, frc_use_claim_previous is TRUE and we must 1816 * use the rnode's current delegation type (r_deleg_type). 1817 * 2) We're reopening files during some form of recovery. 1818 * In this case, frc_use_claim_previous is FALSE and we 1819 * use the delegation type appropriate for recovery 1820 * (r_deleg_needs_recovery). 1821 */ 1822 mutex_enter(&rp->r_statev4_lock); 1823 open_args->open_claim4_u.delegate_type = 1824 frc_use_claim_previous ? 1825 rp->r_deleg_type : 1826 rp->r_deleg_needs_recovery; 1827 mutex_exit(&rp->r_statev4_lock); 1828 1829 } else if (claim == CLAIM_DELEGATE_CUR) { 1830 1831 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1832 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1833 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1834 "with %m", (void *)vp); 1835 failed_msg = "Couldn't reopen: vtoname failed for " 1836 "CLAIM_DELEGATE_CUR"; 1837 /* nothing allocated yet */ 1838 goto kill_file; 1839 } 1840 1841 mutex_enter(&rp->r_statev4_lock); 1842 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1843 rp->r_deleg_stateid; 1844 mutex_exit(&rp->r_statev4_lock); 1845 1846 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1847 } 1848 open_args->opentype = OPEN4_NOCREATE; 1849 open_args->owner.clientid = mi2clientid(mi); 1850 open_args->owner.owner_len = sizeof (oop->oo_name); 1851 open_args->owner.owner_val = 1852 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1853 bcopy(&oop->oo_name, open_args->owner.owner_val, 1854 open_args->owner.owner_len); 1855 open_args->share_access = 0; 1856 open_args->share_deny = 0; 1857 1858 mutex_enter(&osp->os_sync_lock); 1859 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1860 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1861 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1862 (void *)osp, (void *)rp, osp->os_share_acc_read, 1863 osp->os_share_acc_write, osp->os_open_ref_count, 1864 osp->os_mmap_read, osp->os_mmap_write, claim)); 1865 1866 if (osp->os_share_acc_read || osp->os_mmap_read) 1867 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1868 if (osp->os_share_acc_write || osp->os_mmap_write) 1869 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1870 if (osp->os_share_deny_read) 1871 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1872 if (osp->os_share_deny_write) 1873 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1874 mutex_exit(&osp->os_sync_lock); 1875 1876 seqid = nfs4_get_open_seqid(oop) + 1; 1877 open_args->seqid = seqid; 1878 1879 /* Construct the getfh part of the compound */ 1880 argop[2].argop = OP_GETFH; 1881 1882 /* Construct the getattr part of the compound */ 1883 argop[3].argop = OP_GETATTR; 1884 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1885 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1886 1887 t = gethrtime(); 1888 1889 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1890 1891 if (ep->error) { 1892 if (!is_recov && !frc_use_claim_previous && 1893 (ep->error == EINTR || ep->error == ETIMEDOUT || 1894 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1895 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1896 cred_otw, vp, NULL, open_args); 1897 abort = nfs4_start_recovery(ep, 1898 VTOMI4(vp), vp, NULL, NULL, 1899 lost_rqst.lr_op == OP_OPEN ? 1900 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL); 1901 nfs4args_copen_free(open_args); 1902 goto bailout; 1903 } 1904 1905 nfs4args_copen_free(open_args); 1906 1907 if (ep->error == EACCES && cred_otw != cr) { 1908 crfree(cred_otw); 1909 cred_otw = cr; 1910 crhold(cred_otw); 1911 nfs4_end_open_seqid_sync(oop); 1912 open_owner_rele(oop); 1913 oop = NULL; 1914 goto top; 1915 } 1916 if (ep->error == ETIMEDOUT) 1917 goto bailout; 1918 failed_msg = "Couldn't reopen: rpc error"; 1919 goto kill_file; 1920 } 1921 1922 if (nfs4_need_to_bump_seqid(&res)) 1923 nfs4_set_open_seqid(seqid, oop, args.ctag); 1924 1925 switch (res.status) { 1926 case NFS4_OK: 1927 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1928 mutex_enter(&rp->r_statelock); 1929 rp->r_delay_interval = 0; 1930 mutex_exit(&rp->r_statelock); 1931 } 1932 break; 1933 case NFS4ERR_BAD_SEQID: 1934 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1935 args.ctag, open_args->seqid); 1936 1937 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1938 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1939 NULL, OP_OPEN, bsep, NULL, NULL); 1940 1941 nfs4args_copen_free(open_args); 1942 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1943 nfs4_end_open_seqid_sync(oop); 1944 open_owner_rele(oop); 1945 oop = NULL; 1946 kmem_free(bsep, sizeof (*bsep)); 1947 1948 goto kill_file; 1949 case NFS4ERR_NO_GRACE: 1950 nfs4args_copen_free(open_args); 1951 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1952 nfs4_end_open_seqid_sync(oop); 1953 open_owner_rele(oop); 1954 oop = NULL; 1955 if (claim == CLAIM_PREVIOUS) { 1956 /* 1957 * Retry as a plain open. We don't need to worry about 1958 * checking the changeinfo: it is acceptable for a 1959 * client to re-open a file and continue processing 1960 * (in the absence of locks). 1961 */ 1962 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1963 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1964 "will retry as CLAIM_NULL")); 1965 claim = CLAIM_NULL; 1966 nfs4_mi_kstat_inc_no_grace(mi); 1967 goto top; 1968 } 1969 failed_msg = 1970 "Couldn't reopen: tried reclaim outside grace period. "; 1971 goto kill_file; 1972 case NFS4ERR_GRACE: 1973 nfs4_set_grace_wait(mi); 1974 nfs4args_copen_free(open_args); 1975 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1976 nfs4_end_open_seqid_sync(oop); 1977 open_owner_rele(oop); 1978 oop = NULL; 1979 ep->error = nfs4_wait_for_grace(mi, &recov); 1980 if (ep->error != 0) 1981 goto bailout; 1982 goto top; 1983 case NFS4ERR_DELAY: 1984 nfs4_set_delay_wait(vp); 1985 nfs4args_copen_free(open_args); 1986 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1987 nfs4_end_open_seqid_sync(oop); 1988 open_owner_rele(oop); 1989 oop = NULL; 1990 ep->error = nfs4_wait_for_delay(vp, &recov); 1991 nfs4_mi_kstat_inc_delay(mi); 1992 if (ep->error != 0) 1993 goto bailout; 1994 goto top; 1995 case NFS4ERR_FHEXPIRED: 1996 /* recover filehandle and retry */ 1997 abort = nfs4_start_recovery(ep, 1998 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL); 1999 nfs4args_copen_free(open_args); 2000 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2001 nfs4_end_open_seqid_sync(oop); 2002 open_owner_rele(oop); 2003 oop = NULL; 2004 if (abort == FALSE) 2005 goto top; 2006 failed_msg = "Couldn't reopen: recovery aborted"; 2007 goto kill_file; 2008 case NFS4ERR_RESOURCE: 2009 case NFS4ERR_STALE_CLIENTID: 2010 case NFS4ERR_WRONGSEC: 2011 case NFS4ERR_EXPIRED: 2012 /* 2013 * Do not mark the file dead and let the calling 2014 * function initiate recovery. 2015 */ 2016 nfs4args_copen_free(open_args); 2017 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2018 nfs4_end_open_seqid_sync(oop); 2019 open_owner_rele(oop); 2020 oop = NULL; 2021 goto bailout; 2022 case NFS4ERR_ACCESS: 2023 if (cred_otw != cr) { 2024 crfree(cred_otw); 2025 cred_otw = cr; 2026 crhold(cred_otw); 2027 nfs4args_copen_free(open_args); 2028 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2029 nfs4_end_open_seqid_sync(oop); 2030 open_owner_rele(oop); 2031 oop = NULL; 2032 goto top; 2033 } 2034 /* fall through */ 2035 default: 2036 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2037 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2038 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2039 rnode4info(VTOR4(vp)))); 2040 failed_msg = "Couldn't reopen: NFSv4 error"; 2041 nfs4args_copen_free(open_args); 2042 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2043 goto kill_file; 2044 } 2045 2046 resop = &res.array[1]; /* open res */ 2047 op_res = &resop->nfs_resop4_u.opopen; 2048 2049 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2050 2051 /* 2052 * Check if the path we reopened really is the same 2053 * file. We could end up in a situation where the file 2054 * was removed and a new file created with the same name. 2055 */ 2056 resop = &res.array[2]; 2057 gf_res = &resop->nfs_resop4_u.opgetfh; 2058 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2059 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2060 if (fh_different) { 2061 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2062 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2063 /* Oops, we don't have the same file */ 2064 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2065 failed_msg = "Couldn't reopen: Persistent " 2066 "file handle changed"; 2067 else 2068 failed_msg = "Couldn't reopen: Volatile " 2069 "(no expire on open) file handle changed"; 2070 2071 nfs4args_copen_free(open_args); 2072 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2073 nfs_rw_exit(&mi->mi_fh_lock); 2074 goto kill_file; 2075 2076 } else { 2077 /* 2078 * We have volatile file handles that don't compare. 2079 * If the fids are the same then we assume that the 2080 * file handle expired but the rnode still refers to 2081 * the same file object. 2082 * 2083 * First check that we have fids or not. 2084 * If we don't we have a dumb server so we will 2085 * just assume every thing is ok for now. 2086 */ 2087 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2088 rp->r_attr.va_mask & AT_NODEID && 2089 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2090 /* 2091 * We have fids, but they don't 2092 * compare. So kill the file. 2093 */ 2094 failed_msg = 2095 "Couldn't reopen: file handle changed" 2096 " due to mismatched fids"; 2097 nfs4args_copen_free(open_args); 2098 (void) xdr_free(xdr_COMPOUND4res_clnt, 2099 (caddr_t)&res); 2100 nfs_rw_exit(&mi->mi_fh_lock); 2101 goto kill_file; 2102 } else { 2103 /* 2104 * We have volatile file handles that refers 2105 * to the same file (at least they have the 2106 * same fid) or we don't have fids so we 2107 * can't tell. :(. We'll be a kind and accepting 2108 * client so we'll update the rnode's file 2109 * handle with the otw handle. 2110 * 2111 * We need to drop mi->mi_fh_lock since 2112 * sh4_update acquires it. Since there is 2113 * only one recovery thread there is no 2114 * race. 2115 */ 2116 nfs_rw_exit(&mi->mi_fh_lock); 2117 sfh4_update(rp->r_fh, &gf_res->object); 2118 } 2119 } 2120 } else { 2121 nfs_rw_exit(&mi->mi_fh_lock); 2122 } 2123 2124 ASSERT(nfs4_consistent_type(vp)); 2125 2126 /* 2127 * If the server wanted an OPEN_CONFIRM but that fails, just start 2128 * over. Presumably if there is a persistent error it will show up 2129 * when we resend the OPEN. 2130 */ 2131 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2132 bool_t retry_open = FALSE; 2133 2134 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2135 cred_otw, is_recov, &retry_open, 2136 oop, FALSE, ep, NULL); 2137 if (ep->error || ep->stat) { 2138 nfs4args_copen_free(open_args); 2139 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2140 nfs4_end_open_seqid_sync(oop); 2141 open_owner_rele(oop); 2142 oop = NULL; 2143 goto top; 2144 } 2145 } 2146 2147 mutex_enter(&osp->os_sync_lock); 2148 osp->open_stateid = op_res->stateid; 2149 osp->os_delegation = 0; 2150 /* 2151 * Need to reset this bitfield for the possible case where we were 2152 * going to OTW CLOSE the file, got a non-recoverable error, and before 2153 * we could retry the CLOSE, OPENed the file again. 2154 */ 2155 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2156 osp->os_final_close = 0; 2157 osp->os_force_close = 0; 2158 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2159 osp->os_dc_openacc = open_args->share_access; 2160 mutex_exit(&osp->os_sync_lock); 2161 2162 nfs4_end_open_seqid_sync(oop); 2163 2164 /* accept delegation, if any */ 2165 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2166 2167 nfs4args_copen_free(open_args); 2168 2169 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2170 2171 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2172 2173 ASSERT(nfs4_consistent_type(vp)); 2174 2175 open_owner_rele(oop); 2176 crfree(cr); 2177 crfree(cred_otw); 2178 return; 2179 2180 kill_file: 2181 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2182 failed_reopen: 2183 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2184 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2185 (void *)osp, (void *)cr, rnode4info(rp))); 2186 mutex_enter(&osp->os_sync_lock); 2187 osp->os_failed_reopen = 1; 2188 mutex_exit(&osp->os_sync_lock); 2189 bailout: 2190 if (oop != NULL) { 2191 nfs4_end_open_seqid_sync(oop); 2192 open_owner_rele(oop); 2193 } 2194 if (cr != NULL) 2195 crfree(cr); 2196 if (cred_otw != NULL) 2197 crfree(cred_otw); 2198 } 2199 2200 /* for . and .. OPENs */ 2201 /* ARGSUSED */ 2202 static int 2203 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2204 { 2205 rnode4_t *rp; 2206 nfs4_ga_res_t gar; 2207 2208 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2209 2210 /* 2211 * If close-to-open consistency checking is turned off or 2212 * if there is no cached data, we can avoid 2213 * the over the wire getattr. Otherwise, force a 2214 * call to the server to get fresh attributes and to 2215 * check caches. This is required for close-to-open 2216 * consistency. 2217 */ 2218 rp = VTOR4(*vpp); 2219 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2220 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2221 return (0); 2222 2223 gar.n4g_va.va_mask = AT_ALL; 2224 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2225 } 2226 2227 /* 2228 * CLOSE a file 2229 */ 2230 /* ARGSUSED */ 2231 static int 2232 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2233 caller_context_t *ct) 2234 { 2235 rnode4_t *rp; 2236 int error = 0; 2237 int r_error = 0; 2238 int n4error = 0; 2239 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2240 2241 /* 2242 * Remove client state for this (lockowner, file) pair. 2243 * Issue otw v4 call to have the server do the same. 2244 */ 2245 2246 rp = VTOR4(vp); 2247 2248 /* 2249 * zone_enter(2) prevents processes from changing zones with NFS files 2250 * open; if we happen to get here from the wrong zone we can't do 2251 * anything over the wire. 2252 */ 2253 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2254 /* 2255 * We could attempt to clean up locks, except we're sure 2256 * that the current process didn't acquire any locks on 2257 * the file: any attempt to lock a file belong to another zone 2258 * will fail, and one can't lock an NFS file and then change 2259 * zones, as that fails too. 2260 * 2261 * Returning an error here is the sane thing to do. A 2262 * subsequent call to VN_RELE() which translates to a 2263 * nfs4_inactive() will clean up state: if the zone of the 2264 * vnode's origin is still alive and kicking, the inactive 2265 * thread will handle the request (from the correct zone), and 2266 * everything (minus the OTW close call) should be OK. If the 2267 * zone is going away nfs4_async_inactive() will throw away 2268 * delegations, open streams and cached pages inline. 2269 */ 2270 return (EIO); 2271 } 2272 2273 /* 2274 * If we are using local locking for this filesystem, then 2275 * release all of the SYSV style record locks. Otherwise, 2276 * we are doing network locking and we need to release all 2277 * of the network locks. All of the locks held by this 2278 * process on this file are released no matter what the 2279 * incoming reference count is. 2280 */ 2281 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2282 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2283 cleanshares(vp, ttoproc(curthread)->p_pid); 2284 } else 2285 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2286 2287 if (e.error) { 2288 struct lm_sysid *lmsid; 2289 lmsid = nfs4_find_sysid(VTOMI4(vp)); 2290 if (lmsid == NULL) { 2291 DTRACE_PROBE2(unknown__sysid, int, e.error, 2292 vnode_t *, vp); 2293 } else { 2294 cleanlocks(vp, ttoproc(curthread)->p_pid, 2295 (lm_sysidt(lmsid) | LM_SYSID_CLIENT)); 2296 } 2297 return (e.error); 2298 } 2299 2300 if (count > 1) 2301 return (0); 2302 2303 /* 2304 * If the file has been `unlinked', then purge the 2305 * DNLC so that this vnode will get reycled quicker 2306 * and the .nfs* file on the server will get removed. 2307 */ 2308 if (rp->r_unldvp != NULL) 2309 dnlc_purge_vp(vp); 2310 2311 /* 2312 * If the file was open for write and there are pages, 2313 * do a synchronous flush and commit of all of the 2314 * dirty and uncommitted pages. 2315 */ 2316 ASSERT(!e.error); 2317 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2318 error = nfs4_putpage_commit(vp, 0, 0, cr); 2319 2320 mutex_enter(&rp->r_statelock); 2321 r_error = rp->r_error; 2322 rp->r_error = 0; 2323 mutex_exit(&rp->r_statelock); 2324 2325 /* 2326 * If this file type is one for which no explicit 'open' was 2327 * done, then bail now (ie. no need for protocol 'close'). If 2328 * there was an error w/the vm subsystem, return _that_ error, 2329 * otherwise, return any errors that may've been reported via 2330 * the rnode. 2331 */ 2332 if (vp->v_type != VREG) 2333 return (error ? error : r_error); 2334 2335 /* 2336 * The sync putpage commit may have failed above, but since 2337 * we're working w/a regular file, we need to do the protocol 2338 * 'close' (nfs4close_one will figure out if an otw close is 2339 * needed or not). Report any errors _after_ doing the protocol 2340 * 'close'. 2341 */ 2342 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2343 n4error = e.error ? e.error : geterrno4(e.stat); 2344 2345 /* 2346 * Error reporting prio (Hi -> Lo) 2347 * 2348 * i) nfs4_putpage_commit (error) 2349 * ii) rnode's (r_error) 2350 * iii) nfs4close_one (n4error) 2351 */ 2352 return (error ? error : (r_error ? r_error : n4error)); 2353 } 2354 2355 /* 2356 * Initialize *lost_rqstp. 2357 */ 2358 2359 static void 2360 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2361 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2362 vnode_t *vp) 2363 { 2364 if (error != ETIMEDOUT && error != EINTR && 2365 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2366 lost_rqstp->lr_op = 0; 2367 return; 2368 } 2369 2370 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2371 "nfs4close_save_lost_rqst: error %d", error)); 2372 2373 lost_rqstp->lr_op = OP_CLOSE; 2374 /* 2375 * The vp is held and rele'd via the recovery code. 2376 * See nfs4_save_lost_rqst. 2377 */ 2378 lost_rqstp->lr_vp = vp; 2379 lost_rqstp->lr_dvp = NULL; 2380 lost_rqstp->lr_oop = oop; 2381 lost_rqstp->lr_osp = osp; 2382 ASSERT(osp != NULL); 2383 ASSERT(mutex_owned(&osp->os_sync_lock)); 2384 osp->os_pending_close = 1; 2385 lost_rqstp->lr_lop = NULL; 2386 lost_rqstp->lr_cr = cr; 2387 lost_rqstp->lr_flk = NULL; 2388 lost_rqstp->lr_putfirst = FALSE; 2389 } 2390 2391 /* 2392 * Assumes you already have the open seqid sync grabbed as well as the 2393 * 'os_sync_lock'. Note: this will release the open seqid sync and 2394 * 'os_sync_lock' if client recovery starts. Calling functions have to 2395 * be prepared to handle this. 2396 * 2397 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2398 * was needed and was started, and that the calling function should retry 2399 * this function; otherwise it is returned as 0. 2400 * 2401 * Errors are returned via the nfs4_error_t parameter. 2402 */ 2403 static void 2404 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2405 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2406 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2407 { 2408 COMPOUND4args_clnt args; 2409 COMPOUND4res_clnt res; 2410 CLOSE4args *close_args; 2411 nfs_resop4 *resop; 2412 nfs_argop4 argop[3]; 2413 int doqueue = 1; 2414 mntinfo4_t *mi; 2415 seqid4 seqid; 2416 vnode_t *vp; 2417 bool_t needrecov = FALSE; 2418 nfs4_lost_rqst_t lost_rqst; 2419 hrtime_t t; 2420 2421 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2422 2423 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2424 2425 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2426 2427 /* Only set this to 1 if recovery is started */ 2428 *recov = 0; 2429 2430 /* do the OTW call to close the file */ 2431 2432 if (close_type == CLOSE_RESEND) 2433 args.ctag = TAG_CLOSE_LOST; 2434 else if (close_type == CLOSE_AFTER_RESEND) 2435 args.ctag = TAG_CLOSE_UNDO; 2436 else 2437 args.ctag = TAG_CLOSE; 2438 2439 args.array_len = 3; 2440 args.array = argop; 2441 2442 vp = RTOV4(rp); 2443 2444 mi = VTOMI4(vp); 2445 2446 /* putfh target fh */ 2447 argop[0].argop = OP_CPUTFH; 2448 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2449 2450 argop[1].argop = OP_GETATTR; 2451 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2452 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2453 2454 argop[2].argop = OP_CLOSE; 2455 close_args = &argop[2].nfs_argop4_u.opclose; 2456 2457 seqid = nfs4_get_open_seqid(oop) + 1; 2458 2459 close_args->seqid = seqid; 2460 close_args->open_stateid = osp->open_stateid; 2461 2462 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2463 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2464 rnode4info(rp))); 2465 2466 t = gethrtime(); 2467 2468 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2469 2470 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2471 nfs4_set_open_seqid(seqid, oop, args.ctag); 2472 } 2473 2474 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2475 if (ep->error && !needrecov) { 2476 /* 2477 * if there was an error and no recovery is to be done 2478 * then then set up the file to flush its cache if 2479 * needed for the next caller. 2480 */ 2481 mutex_enter(&rp->r_statelock); 2482 PURGE_ATTRCACHE4_LOCKED(rp); 2483 rp->r_flags &= ~R4WRITEMODIFIED; 2484 mutex_exit(&rp->r_statelock); 2485 return; 2486 } 2487 2488 if (needrecov) { 2489 bool_t abort; 2490 nfs4_bseqid_entry_t *bsep = NULL; 2491 2492 if (close_type != CLOSE_RESEND) 2493 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2494 osp, cred_otw, vp); 2495 2496 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2497 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2498 0, args.ctag, close_args->seqid); 2499 2500 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2501 "nfs4close_otw: initiating recovery. error %d " 2502 "res.status %d", ep->error, res.status)); 2503 2504 /* 2505 * Drop the 'os_sync_lock' here so we don't hit 2506 * a potential recursive mutex_enter via an 2507 * 'open_stream_hold()'. 2508 */ 2509 mutex_exit(&osp->os_sync_lock); 2510 *have_sync_lockp = 0; 2511 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2512 (close_type != CLOSE_RESEND && 2513 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2514 OP_CLOSE, bsep, NULL, NULL); 2515 2516 /* drop open seq sync, and let the calling function regrab it */ 2517 nfs4_end_open_seqid_sync(oop); 2518 *did_start_seqid_syncp = 0; 2519 2520 if (bsep) 2521 kmem_free(bsep, sizeof (*bsep)); 2522 /* 2523 * For signals, the caller wants to quit, so don't say to 2524 * retry. For forced unmount, if it's a user thread, it 2525 * wants to quit. If it's a recovery thread, the retry 2526 * will happen higher-up on the call stack. Either way, 2527 * don't say to retry. 2528 */ 2529 if (abort == FALSE && ep->error != EINTR && 2530 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2531 close_type != CLOSE_RESEND && 2532 close_type != CLOSE_AFTER_RESEND) 2533 *recov = 1; 2534 else 2535 *recov = 0; 2536 2537 if (!ep->error) 2538 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2539 return; 2540 } 2541 2542 if (res.status) { 2543 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2544 return; 2545 } 2546 2547 mutex_enter(&rp->r_statev4_lock); 2548 rp->created_v4 = 0; 2549 mutex_exit(&rp->r_statev4_lock); 2550 2551 resop = &res.array[2]; 2552 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2553 osp->os_valid = 0; 2554 2555 /* 2556 * This removes the reference obtained at OPEN; ie, when the 2557 * open stream structure was created. 2558 * 2559 * We don't have to worry about calling 'open_stream_rele' 2560 * since we our currently holding a reference to the open 2561 * stream which means the count cannot go to 0 with this 2562 * decrement. 2563 */ 2564 ASSERT(osp->os_ref_count >= 2); 2565 osp->os_ref_count--; 2566 2567 if (!ep->error) 2568 nfs4_attr_cache(vp, 2569 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2570 t, cred_otw, TRUE, NULL); 2571 2572 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2573 " returning %d", ep->error)); 2574 2575 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2576 } 2577 2578 /* ARGSUSED */ 2579 static int 2580 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2581 caller_context_t *ct) 2582 { 2583 rnode4_t *rp; 2584 u_offset_t off; 2585 offset_t diff; 2586 uint_t on; 2587 uint_t n; 2588 caddr_t base; 2589 uint_t flags; 2590 int error; 2591 mntinfo4_t *mi; 2592 2593 rp = VTOR4(vp); 2594 2595 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2596 2597 if (IS_SHADOW(vp, rp)) 2598 vp = RTOV4(rp); 2599 2600 if (vp->v_type != VREG) 2601 return (EISDIR); 2602 2603 mi = VTOMI4(vp); 2604 2605 if (nfs_zone() != mi->mi_zone) 2606 return (EIO); 2607 2608 if (uiop->uio_resid == 0) 2609 return (0); 2610 2611 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2612 return (EINVAL); 2613 2614 mutex_enter(&rp->r_statelock); 2615 if (rp->r_flags & R4RECOVERRP) 2616 error = (rp->r_error ? rp->r_error : EIO); 2617 else 2618 error = 0; 2619 mutex_exit(&rp->r_statelock); 2620 if (error) 2621 return (error); 2622 2623 /* 2624 * Bypass VM if caching has been disabled (e.g., locking) or if 2625 * using client-side direct I/O and the file is not mmap'd and 2626 * there are no cached pages. 2627 */ 2628 if ((vp->v_flag & VNOCACHE) || 2629 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2630 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2631 size_t resid = 0; 2632 2633 return (nfs4read(vp, NULL, uiop->uio_loffset, 2634 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2635 } 2636 2637 error = 0; 2638 2639 do { 2640 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2641 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2642 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2643 2644 if (error = nfs4_validate_caches(vp, cr)) 2645 break; 2646 2647 mutex_enter(&rp->r_statelock); 2648 while (rp->r_flags & R4INCACHEPURGE) { 2649 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2650 mutex_exit(&rp->r_statelock); 2651 return (EINTR); 2652 } 2653 } 2654 diff = rp->r_size - uiop->uio_loffset; 2655 mutex_exit(&rp->r_statelock); 2656 if (diff <= 0) 2657 break; 2658 if (diff < n) 2659 n = (uint_t)diff; 2660 2661 if (vpm_enable) { 2662 /* 2663 * Copy data. 2664 */ 2665 error = vpm_data_copy(vp, off + on, n, uiop, 2666 1, NULL, 0, S_READ); 2667 } else { 2668 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2669 S_READ); 2670 2671 error = uiomove(base + on, n, UIO_READ, uiop); 2672 } 2673 2674 if (!error) { 2675 /* 2676 * If read a whole block or read to eof, 2677 * won't need this buffer again soon. 2678 */ 2679 mutex_enter(&rp->r_statelock); 2680 if (n + on == MAXBSIZE || 2681 uiop->uio_loffset == rp->r_size) 2682 flags = SM_DONTNEED; 2683 else 2684 flags = 0; 2685 mutex_exit(&rp->r_statelock); 2686 if (vpm_enable) { 2687 error = vpm_sync_pages(vp, off, n, flags); 2688 } else { 2689 error = segmap_release(segkmap, base, flags); 2690 } 2691 } else { 2692 if (vpm_enable) { 2693 (void) vpm_sync_pages(vp, off, n, 0); 2694 } else { 2695 (void) segmap_release(segkmap, base, 0); 2696 } 2697 } 2698 } while (!error && uiop->uio_resid > 0); 2699 2700 return (error); 2701 } 2702 2703 /* ARGSUSED */ 2704 static int 2705 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2706 caller_context_t *ct) 2707 { 2708 rlim64_t limit = uiop->uio_llimit; 2709 rnode4_t *rp; 2710 u_offset_t off; 2711 caddr_t base; 2712 uint_t flags; 2713 int remainder; 2714 size_t n; 2715 int on; 2716 int error; 2717 int resid; 2718 u_offset_t offset; 2719 mntinfo4_t *mi; 2720 uint_t bsize; 2721 2722 rp = VTOR4(vp); 2723 2724 if (IS_SHADOW(vp, rp)) 2725 vp = RTOV4(rp); 2726 2727 if (vp->v_type != VREG) 2728 return (EISDIR); 2729 2730 mi = VTOMI4(vp); 2731 2732 if (nfs_zone() != mi->mi_zone) 2733 return (EIO); 2734 2735 if (uiop->uio_resid == 0) 2736 return (0); 2737 2738 mutex_enter(&rp->r_statelock); 2739 if (rp->r_flags & R4RECOVERRP) 2740 error = (rp->r_error ? rp->r_error : EIO); 2741 else 2742 error = 0; 2743 mutex_exit(&rp->r_statelock); 2744 if (error) 2745 return (error); 2746 2747 if (ioflag & FAPPEND) { 2748 struct vattr va; 2749 2750 /* 2751 * Must serialize if appending. 2752 */ 2753 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2754 nfs_rw_exit(&rp->r_rwlock); 2755 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2756 INTR4(vp))) 2757 return (EINTR); 2758 } 2759 2760 va.va_mask = AT_SIZE; 2761 error = nfs4getattr(vp, &va, cr); 2762 if (error) 2763 return (error); 2764 uiop->uio_loffset = va.va_size; 2765 } 2766 2767 offset = uiop->uio_loffset + uiop->uio_resid; 2768 2769 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2770 return (EINVAL); 2771 2772 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2773 limit = MAXOFFSET_T; 2774 2775 /* 2776 * Check to make sure that the process will not exceed 2777 * its limit on file size. It is okay to write up to 2778 * the limit, but not beyond. Thus, the write which 2779 * reaches the limit will be short and the next write 2780 * will return an error. 2781 */ 2782 remainder = 0; 2783 if (offset > uiop->uio_llimit) { 2784 remainder = offset - uiop->uio_llimit; 2785 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2786 if (uiop->uio_resid <= 0) { 2787 proc_t *p = ttoproc(curthread); 2788 2789 uiop->uio_resid += remainder; 2790 mutex_enter(&p->p_lock); 2791 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2792 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2793 mutex_exit(&p->p_lock); 2794 return (EFBIG); 2795 } 2796 } 2797 2798 /* update the change attribute, if we have a write delegation */ 2799 2800 mutex_enter(&rp->r_statev4_lock); 2801 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2802 rp->r_deleg_change++; 2803 2804 mutex_exit(&rp->r_statev4_lock); 2805 2806 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2807 return (EINTR); 2808 2809 /* 2810 * Bypass VM if caching has been disabled (e.g., locking) or if 2811 * using client-side direct I/O and the file is not mmap'd and 2812 * there are no cached pages. 2813 */ 2814 if ((vp->v_flag & VNOCACHE) || 2815 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2816 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2817 size_t bufsize; 2818 int count; 2819 u_offset_t org_offset; 2820 stable_how4 stab_comm; 2821 nfs4_fwrite: 2822 if (rp->r_flags & R4STALE) { 2823 resid = uiop->uio_resid; 2824 offset = uiop->uio_loffset; 2825 error = rp->r_error; 2826 /* 2827 * A close may have cleared r_error, if so, 2828 * propagate ESTALE error return properly 2829 */ 2830 if (error == 0) 2831 error = ESTALE; 2832 goto bottom; 2833 } 2834 2835 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2836 base = kmem_alloc(bufsize, KM_SLEEP); 2837 do { 2838 if (ioflag & FDSYNC) 2839 stab_comm = DATA_SYNC4; 2840 else 2841 stab_comm = FILE_SYNC4; 2842 resid = uiop->uio_resid; 2843 offset = uiop->uio_loffset; 2844 count = MIN(uiop->uio_resid, bufsize); 2845 org_offset = uiop->uio_loffset; 2846 error = uiomove(base, count, UIO_WRITE, uiop); 2847 if (!error) { 2848 error = nfs4write(vp, base, org_offset, 2849 count, cr, &stab_comm); 2850 if (!error) { 2851 mutex_enter(&rp->r_statelock); 2852 if (rp->r_size < uiop->uio_loffset) 2853 rp->r_size = uiop->uio_loffset; 2854 mutex_exit(&rp->r_statelock); 2855 } 2856 } 2857 } while (!error && uiop->uio_resid > 0); 2858 kmem_free(base, bufsize); 2859 goto bottom; 2860 } 2861 2862 bsize = vp->v_vfsp->vfs_bsize; 2863 2864 do { 2865 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2866 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2867 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2868 2869 resid = uiop->uio_resid; 2870 offset = uiop->uio_loffset; 2871 2872 if (rp->r_flags & R4STALE) { 2873 error = rp->r_error; 2874 /* 2875 * A close may have cleared r_error, if so, 2876 * propagate ESTALE error return properly 2877 */ 2878 if (error == 0) 2879 error = ESTALE; 2880 break; 2881 } 2882 2883 /* 2884 * Don't create dirty pages faster than they 2885 * can be cleaned so that the system doesn't 2886 * get imbalanced. If the async queue is 2887 * maxed out, then wait for it to drain before 2888 * creating more dirty pages. Also, wait for 2889 * any threads doing pagewalks in the vop_getattr 2890 * entry points so that they don't block for 2891 * long periods. 2892 */ 2893 mutex_enter(&rp->r_statelock); 2894 while ((mi->mi_max_threads != 0 && 2895 rp->r_awcount > 2 * mi->mi_max_threads) || 2896 rp->r_gcount > 0) { 2897 if (INTR4(vp)) { 2898 klwp_t *lwp = ttolwp(curthread); 2899 2900 if (lwp != NULL) 2901 lwp->lwp_nostop++; 2902 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2903 mutex_exit(&rp->r_statelock); 2904 if (lwp != NULL) 2905 lwp->lwp_nostop--; 2906 error = EINTR; 2907 goto bottom; 2908 } 2909 if (lwp != NULL) 2910 lwp->lwp_nostop--; 2911 } else 2912 cv_wait(&rp->r_cv, &rp->r_statelock); 2913 } 2914 mutex_exit(&rp->r_statelock); 2915 2916 /* 2917 * Touch the page and fault it in if it is not in core 2918 * before segmap_getmapflt or vpm_data_copy can lock it. 2919 * This is to avoid the deadlock if the buffer is mapped 2920 * to the same file through mmap which we want to write. 2921 */ 2922 uio_prefaultpages((long)n, uiop); 2923 2924 if (vpm_enable) { 2925 /* 2926 * It will use kpm mappings, so no need to 2927 * pass an address. 2928 */ 2929 error = writerp4(rp, NULL, n, uiop, 0); 2930 } else { 2931 if (segmap_kpm) { 2932 int pon = uiop->uio_loffset & PAGEOFFSET; 2933 size_t pn = MIN(PAGESIZE - pon, 2934 uiop->uio_resid); 2935 int pagecreate; 2936 2937 mutex_enter(&rp->r_statelock); 2938 pagecreate = (pon == 0) && (pn == PAGESIZE || 2939 uiop->uio_loffset + pn >= rp->r_size); 2940 mutex_exit(&rp->r_statelock); 2941 2942 base = segmap_getmapflt(segkmap, vp, off + on, 2943 pn, !pagecreate, S_WRITE); 2944 2945 error = writerp4(rp, base + pon, n, uiop, 2946 pagecreate); 2947 2948 } else { 2949 base = segmap_getmapflt(segkmap, vp, off + on, 2950 n, 0, S_READ); 2951 error = writerp4(rp, base + on, n, uiop, 0); 2952 } 2953 } 2954 2955 if (!error) { 2956 if (mi->mi_flags & MI4_NOAC) 2957 flags = SM_WRITE; 2958 else if ((uiop->uio_loffset % bsize) == 0 || 2959 IS_SWAPVP(vp)) { 2960 /* 2961 * Have written a whole block. 2962 * Start an asynchronous write 2963 * and mark the buffer to 2964 * indicate that it won't be 2965 * needed again soon. 2966 */ 2967 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2968 } else 2969 flags = 0; 2970 if ((ioflag & (FSYNC|FDSYNC)) || 2971 (rp->r_flags & R4OUTOFSPACE)) { 2972 flags &= ~SM_ASYNC; 2973 flags |= SM_WRITE; 2974 } 2975 if (vpm_enable) { 2976 error = vpm_sync_pages(vp, off, n, flags); 2977 } else { 2978 error = segmap_release(segkmap, base, flags); 2979 } 2980 } else { 2981 if (vpm_enable) { 2982 (void) vpm_sync_pages(vp, off, n, 0); 2983 } else { 2984 (void) segmap_release(segkmap, base, 0); 2985 } 2986 /* 2987 * In the event that we got an access error while 2988 * faulting in a page for a write-only file just 2989 * force a write. 2990 */ 2991 if (error == EACCES) 2992 goto nfs4_fwrite; 2993 } 2994 } while (!error && uiop->uio_resid > 0); 2995 2996 bottom: 2997 if (error) { 2998 uiop->uio_resid = resid + remainder; 2999 uiop->uio_loffset = offset; 3000 } else { 3001 uiop->uio_resid += remainder; 3002 3003 mutex_enter(&rp->r_statev4_lock); 3004 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 3005 gethrestime(&rp->r_attr.va_mtime); 3006 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3007 } 3008 mutex_exit(&rp->r_statev4_lock); 3009 } 3010 3011 nfs_rw_exit(&rp->r_lkserlock); 3012 3013 return (error); 3014 } 3015 3016 /* 3017 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 3018 */ 3019 static int 3020 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 3021 int flags, cred_t *cr) 3022 { 3023 struct buf *bp; 3024 int error; 3025 page_t *savepp; 3026 uchar_t fsdata; 3027 stable_how4 stab_comm; 3028 3029 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3030 bp = pageio_setup(pp, len, vp, flags); 3031 ASSERT(bp != NULL); 3032 3033 /* 3034 * pageio_setup should have set b_addr to 0. This 3035 * is correct since we want to do I/O on a page 3036 * boundary. bp_mapin will use this addr to calculate 3037 * an offset, and then set b_addr to the kernel virtual 3038 * address it allocated for us. 3039 */ 3040 ASSERT(bp->b_un.b_addr == 0); 3041 3042 bp->b_edev = 0; 3043 bp->b_dev = 0; 3044 bp->b_lblkno = lbtodb(off); 3045 bp->b_file = vp; 3046 bp->b_offset = (offset_t)off; 3047 bp_mapin(bp); 3048 3049 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 3050 freemem > desfree) 3051 stab_comm = UNSTABLE4; 3052 else 3053 stab_comm = FILE_SYNC4; 3054 3055 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 3056 3057 bp_mapout(bp); 3058 pageio_done(bp); 3059 3060 if (stab_comm == UNSTABLE4) 3061 fsdata = C_DELAYCOMMIT; 3062 else 3063 fsdata = C_NOCOMMIT; 3064 3065 savepp = pp; 3066 do { 3067 pp->p_fsdata = fsdata; 3068 } while ((pp = pp->p_next) != savepp); 3069 3070 return (error); 3071 } 3072 3073 /* 3074 */ 3075 static int 3076 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3077 { 3078 nfs4_open_owner_t *oop; 3079 nfs4_open_stream_t *osp; 3080 rnode4_t *rp = VTOR4(vp); 3081 mntinfo4_t *mi = VTOMI4(vp); 3082 int reopen_needed; 3083 3084 ASSERT(nfs_zone() == mi->mi_zone); 3085 3086 3087 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3088 if (!oop) 3089 return (EIO); 3090 3091 /* returns with 'os_sync_lock' held */ 3092 osp = find_open_stream(oop, rp); 3093 if (!osp) { 3094 open_owner_rele(oop); 3095 return (EIO); 3096 } 3097 3098 if (osp->os_failed_reopen) { 3099 mutex_exit(&osp->os_sync_lock); 3100 open_stream_rele(osp, rp); 3101 open_owner_rele(oop); 3102 return (EIO); 3103 } 3104 3105 /* 3106 * Determine whether a reopen is needed. If this 3107 * is a delegation open stream, then the os_delegation bit 3108 * should be set. 3109 */ 3110 3111 reopen_needed = osp->os_delegation; 3112 3113 mutex_exit(&osp->os_sync_lock); 3114 open_owner_rele(oop); 3115 3116 if (reopen_needed) { 3117 nfs4_error_zinit(ep); 3118 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3119 mutex_enter(&osp->os_sync_lock); 3120 if (ep->error || ep->stat || osp->os_failed_reopen) { 3121 mutex_exit(&osp->os_sync_lock); 3122 open_stream_rele(osp, rp); 3123 return (EIO); 3124 } 3125 mutex_exit(&osp->os_sync_lock); 3126 } 3127 open_stream_rele(osp, rp); 3128 3129 return (0); 3130 } 3131 3132 /* 3133 * Write to file. Writes to remote server in largest size 3134 * chunks that the server can handle. Write is synchronous. 3135 */ 3136 static int 3137 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3138 stable_how4 *stab_comm) 3139 { 3140 mntinfo4_t *mi; 3141 COMPOUND4args_clnt args; 3142 COMPOUND4res_clnt res; 3143 WRITE4args *wargs; 3144 WRITE4res *wres; 3145 nfs_argop4 argop[2]; 3146 nfs_resop4 *resop; 3147 int tsize; 3148 stable_how4 stable; 3149 rnode4_t *rp; 3150 int doqueue = 1; 3151 bool_t needrecov; 3152 nfs4_recov_state_t recov_state; 3153 nfs4_stateid_types_t sid_types; 3154 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3155 int recov; 3156 3157 rp = VTOR4(vp); 3158 mi = VTOMI4(vp); 3159 3160 ASSERT(nfs_zone() == mi->mi_zone); 3161 3162 stable = *stab_comm; 3163 *stab_comm = FILE_SYNC4; 3164 3165 needrecov = FALSE; 3166 recov_state.rs_flags = 0; 3167 recov_state.rs_num_retry_despite_err = 0; 3168 nfs4_init_stateid_types(&sid_types); 3169 3170 /* Is curthread the recovery thread? */ 3171 mutex_enter(&mi->mi_lock); 3172 recov = (mi->mi_recovthread == curthread); 3173 mutex_exit(&mi->mi_lock); 3174 3175 recov_retry: 3176 args.ctag = TAG_WRITE; 3177 args.array_len = 2; 3178 args.array = argop; 3179 3180 if (!recov) { 3181 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3182 &recov_state, NULL); 3183 if (e.error) 3184 return (e.error); 3185 } 3186 3187 /* 0. putfh target fh */ 3188 argop[0].argop = OP_CPUTFH; 3189 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3190 3191 /* 1. write */ 3192 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3193 3194 do { 3195 3196 wargs->offset = (offset4)offset; 3197 wargs->data_val = base; 3198 3199 if (mi->mi_io_kstats) { 3200 mutex_enter(&mi->mi_lock); 3201 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3202 mutex_exit(&mi->mi_lock); 3203 } 3204 3205 if ((vp->v_flag & VNOCACHE) || 3206 (rp->r_flags & R4DIRECTIO) || 3207 (mi->mi_flags & MI4_DIRECTIO)) 3208 tsize = MIN(mi->mi_stsize, count); 3209 else 3210 tsize = MIN(mi->mi_curwrite, count); 3211 wargs->data_len = (uint_t)tsize; 3212 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3213 3214 if (mi->mi_io_kstats) { 3215 mutex_enter(&mi->mi_lock); 3216 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3217 mutex_exit(&mi->mi_lock); 3218 } 3219 3220 if (!recov) { 3221 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3222 if (e.error && !needrecov) { 3223 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3224 &recov_state, needrecov); 3225 return (e.error); 3226 } 3227 } else { 3228 if (e.error) 3229 return (e.error); 3230 } 3231 3232 /* 3233 * Do handling of OLD_STATEID outside 3234 * of the normal recovery framework. 3235 * 3236 * If write receives a BAD stateid error while using a 3237 * delegation stateid, retry using the open stateid (if it 3238 * exists). If it doesn't have an open stateid, reopen the 3239 * file first, then retry. 3240 */ 3241 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3242 sid_types.cur_sid_type != SPEC_SID) { 3243 nfs4_save_stateid(&wargs->stateid, &sid_types); 3244 if (!recov) 3245 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3246 &recov_state, needrecov); 3247 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3248 goto recov_retry; 3249 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3250 sid_types.cur_sid_type == DEL_SID) { 3251 nfs4_save_stateid(&wargs->stateid, &sid_types); 3252 mutex_enter(&rp->r_statev4_lock); 3253 rp->r_deleg_return_pending = TRUE; 3254 mutex_exit(&rp->r_statev4_lock); 3255 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3256 if (!recov) 3257 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3258 &recov_state, needrecov); 3259 (void) xdr_free(xdr_COMPOUND4res_clnt, 3260 (caddr_t)&res); 3261 return (EIO); 3262 } 3263 if (!recov) 3264 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3265 &recov_state, needrecov); 3266 /* hold needed for nfs4delegreturn_thread */ 3267 VN_HOLD(vp); 3268 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3269 NFS4_DR_DISCARD), FALSE); 3270 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3271 goto recov_retry; 3272 } 3273 3274 if (needrecov) { 3275 bool_t abort; 3276 3277 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3278 "nfs4write: client got error %d, res.status %d" 3279 ", so start recovery", e.error, res.status)); 3280 3281 abort = nfs4_start_recovery(&e, 3282 VTOMI4(vp), vp, NULL, &wargs->stateid, 3283 NULL, OP_WRITE, NULL, NULL, NULL); 3284 if (!e.error) { 3285 e.error = geterrno4(res.status); 3286 (void) xdr_free(xdr_COMPOUND4res_clnt, 3287 (caddr_t)&res); 3288 } 3289 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3290 &recov_state, needrecov); 3291 if (abort == FALSE) 3292 goto recov_retry; 3293 return (e.error); 3294 } 3295 3296 if (res.status) { 3297 e.error = geterrno4(res.status); 3298 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3299 if (!recov) 3300 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3301 &recov_state, needrecov); 3302 return (e.error); 3303 } 3304 3305 resop = &res.array[1]; /* write res */ 3306 wres = &resop->nfs_resop4_u.opwrite; 3307 3308 if ((int)wres->count > tsize) { 3309 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3310 3311 zcmn_err(getzoneid(), CE_WARN, 3312 "nfs4write: server wrote %u, requested was %u", 3313 (int)wres->count, tsize); 3314 if (!recov) 3315 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3316 &recov_state, needrecov); 3317 return (EIO); 3318 } 3319 if (wres->committed == UNSTABLE4) { 3320 *stab_comm = UNSTABLE4; 3321 if (wargs->stable == DATA_SYNC4 || 3322 wargs->stable == FILE_SYNC4) { 3323 (void) xdr_free(xdr_COMPOUND4res_clnt, 3324 (caddr_t)&res); 3325 zcmn_err(getzoneid(), CE_WARN, 3326 "nfs4write: server %s did not commit " 3327 "to stable storage", 3328 rp->r_server->sv_hostname); 3329 if (!recov) 3330 nfs4_end_fop(VTOMI4(vp), vp, NULL, 3331 OH_WRITE, &recov_state, needrecov); 3332 return (EIO); 3333 } 3334 } 3335 3336 tsize = (int)wres->count; 3337 count -= tsize; 3338 base += tsize; 3339 offset += tsize; 3340 if (mi->mi_io_kstats) { 3341 mutex_enter(&mi->mi_lock); 3342 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3343 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3344 tsize; 3345 mutex_exit(&mi->mi_lock); 3346 } 3347 lwp_stat_update(LWP_STAT_OUBLK, 1); 3348 mutex_enter(&rp->r_statelock); 3349 if (rp->r_flags & R4HAVEVERF) { 3350 if (rp->r_writeverf != wres->writeverf) { 3351 nfs4_set_mod(vp); 3352 rp->r_writeverf = wres->writeverf; 3353 } 3354 } else { 3355 rp->r_writeverf = wres->writeverf; 3356 rp->r_flags |= R4HAVEVERF; 3357 } 3358 PURGE_ATTRCACHE4_LOCKED(rp); 3359 rp->r_flags |= R4WRITEMODIFIED; 3360 gethrestime(&rp->r_attr.va_mtime); 3361 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3362 mutex_exit(&rp->r_statelock); 3363 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3364 } while (count); 3365 3366 if (!recov) 3367 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, 3368 needrecov); 3369 3370 return (e.error); 3371 } 3372 3373 /* 3374 * Read from a file. Reads data in largest chunks our interface can handle. 3375 */ 3376 static int 3377 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3378 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3379 { 3380 mntinfo4_t *mi; 3381 COMPOUND4args_clnt args; 3382 COMPOUND4res_clnt res; 3383 READ4args *rargs; 3384 nfs_argop4 argop[2]; 3385 int tsize; 3386 int doqueue; 3387 rnode4_t *rp; 3388 int data_len; 3389 bool_t is_eof; 3390 bool_t needrecov = FALSE; 3391 nfs4_recov_state_t recov_state; 3392 nfs4_stateid_types_t sid_types; 3393 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3394 3395 rp = VTOR4(vp); 3396 mi = VTOMI4(vp); 3397 doqueue = 1; 3398 3399 ASSERT(nfs_zone() == mi->mi_zone); 3400 3401 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3402 3403 args.array_len = 2; 3404 args.array = argop; 3405 3406 nfs4_init_stateid_types(&sid_types); 3407 3408 recov_state.rs_flags = 0; 3409 recov_state.rs_num_retry_despite_err = 0; 3410 3411 recov_retry: 3412 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3413 &recov_state, NULL); 3414 if (e.error) 3415 return (e.error); 3416 3417 /* putfh target fh */ 3418 argop[0].argop = OP_CPUTFH; 3419 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3420 3421 /* read */ 3422 argop[1].argop = OP_READ; 3423 rargs = &argop[1].nfs_argop4_u.opread; 3424 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3425 OP_READ, &sid_types, async); 3426 3427 do { 3428 if (mi->mi_io_kstats) { 3429 mutex_enter(&mi->mi_lock); 3430 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3431 mutex_exit(&mi->mi_lock); 3432 } 3433 3434 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3435 "nfs4read: %s call, rp %s", 3436 needrecov ? "recov" : "first", 3437 rnode4info(rp))); 3438 3439 if ((vp->v_flag & VNOCACHE) || 3440 (rp->r_flags & R4DIRECTIO) || 3441 (mi->mi_flags & MI4_DIRECTIO)) 3442 tsize = MIN(mi->mi_tsize, count); 3443 else 3444 tsize = MIN(mi->mi_curread, count); 3445 3446 rargs->offset = (offset4)offset; 3447 rargs->count = (count4)tsize; 3448 rargs->res_data_val_alt = NULL; 3449 rargs->res_mblk = NULL; 3450 rargs->res_uiop = NULL; 3451 rargs->res_maxsize = 0; 3452 rargs->wlist = NULL; 3453 3454 if (uiop) 3455 rargs->res_uiop = uiop; 3456 else 3457 rargs->res_data_val_alt = base; 3458 rargs->res_maxsize = tsize; 3459 3460 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3461 #ifdef DEBUG 3462 if (nfs4read_error_inject) { 3463 res.status = nfs4read_error_inject; 3464 nfs4read_error_inject = 0; 3465 } 3466 #endif 3467 3468 if (mi->mi_io_kstats) { 3469 mutex_enter(&mi->mi_lock); 3470 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3471 mutex_exit(&mi->mi_lock); 3472 } 3473 3474 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3475 if (e.error != 0 && !needrecov) { 3476 nfs4_end_fop(mi, vp, NULL, OH_READ, 3477 &recov_state, needrecov); 3478 return (e.error); 3479 } 3480 3481 /* 3482 * Do proper retry for OLD and BAD stateid errors outside 3483 * of the normal recovery framework. There are two differences 3484 * between async and sync reads. The first is that we allow 3485 * retry on BAD_STATEID for async reads, but not sync reads. 3486 * The second is that we mark the file dead for a failed 3487 * attempt with a special stateid for sync reads, but just 3488 * return EIO for async reads. 3489 * 3490 * If a sync read receives a BAD stateid error while using a 3491 * delegation stateid, retry using the open stateid (if it 3492 * exists). If it doesn't have an open stateid, reopen the 3493 * file first, then retry. 3494 */ 3495 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3496 res.status == NFS4ERR_BAD_STATEID) && async) { 3497 nfs4_end_fop(mi, vp, NULL, OH_READ, 3498 &recov_state, needrecov); 3499 if (sid_types.cur_sid_type == SPEC_SID) { 3500 (void) xdr_free(xdr_COMPOUND4res_clnt, 3501 (caddr_t)&res); 3502 return (EIO); 3503 } 3504 nfs4_save_stateid(&rargs->stateid, &sid_types); 3505 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3506 goto recov_retry; 3507 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3508 !async && sid_types.cur_sid_type != SPEC_SID) { 3509 nfs4_save_stateid(&rargs->stateid, &sid_types); 3510 nfs4_end_fop(mi, vp, NULL, OH_READ, 3511 &recov_state, needrecov); 3512 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3513 goto recov_retry; 3514 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3515 sid_types.cur_sid_type == DEL_SID) { 3516 nfs4_save_stateid(&rargs->stateid, &sid_types); 3517 mutex_enter(&rp->r_statev4_lock); 3518 rp->r_deleg_return_pending = TRUE; 3519 mutex_exit(&rp->r_statev4_lock); 3520 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3521 nfs4_end_fop(mi, vp, NULL, OH_READ, 3522 &recov_state, needrecov); 3523 (void) xdr_free(xdr_COMPOUND4res_clnt, 3524 (caddr_t)&res); 3525 return (EIO); 3526 } 3527 nfs4_end_fop(mi, vp, NULL, OH_READ, 3528 &recov_state, needrecov); 3529 /* hold needed for nfs4delegreturn_thread */ 3530 VN_HOLD(vp); 3531 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3532 NFS4_DR_DISCARD), FALSE); 3533 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3534 goto recov_retry; 3535 } 3536 if (needrecov) { 3537 bool_t abort; 3538 3539 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3540 "nfs4read: initiating recovery\n")); 3541 abort = nfs4_start_recovery(&e, 3542 mi, vp, NULL, &rargs->stateid, 3543 NULL, OP_READ, NULL, NULL, NULL); 3544 nfs4_end_fop(mi, vp, NULL, OH_READ, 3545 &recov_state, needrecov); 3546 /* 3547 * Do not retry if we got OLD_STATEID using a special 3548 * stateid. This avoids looping with a broken server. 3549 */ 3550 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3551 sid_types.cur_sid_type == SPEC_SID) 3552 abort = TRUE; 3553 3554 if (abort == FALSE) { 3555 /* 3556 * Need to retry all possible stateids in 3557 * case the recovery error wasn't stateid 3558 * related or the stateids have become 3559 * stale (server reboot). 3560 */ 3561 nfs4_init_stateid_types(&sid_types); 3562 (void) xdr_free(xdr_COMPOUND4res_clnt, 3563 (caddr_t)&res); 3564 goto recov_retry; 3565 } 3566 3567 if (!e.error) { 3568 e.error = geterrno4(res.status); 3569 (void) xdr_free(xdr_COMPOUND4res_clnt, 3570 (caddr_t)&res); 3571 } 3572 return (e.error); 3573 } 3574 3575 if (res.status) { 3576 e.error = geterrno4(res.status); 3577 nfs4_end_fop(mi, vp, NULL, OH_READ, 3578 &recov_state, needrecov); 3579 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3580 return (e.error); 3581 } 3582 3583 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3584 count -= data_len; 3585 if (base) 3586 base += data_len; 3587 offset += data_len; 3588 if (mi->mi_io_kstats) { 3589 mutex_enter(&mi->mi_lock); 3590 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3591 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3592 mutex_exit(&mi->mi_lock); 3593 } 3594 lwp_stat_update(LWP_STAT_INBLK, 1); 3595 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3596 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3597 3598 } while (count && !is_eof); 3599 3600 *residp = count; 3601 3602 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3603 3604 return (e.error); 3605 } 3606 3607 /* ARGSUSED */ 3608 static int 3609 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp, 3610 caller_context_t *ct) 3611 { 3612 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3613 return (EIO); 3614 switch (cmd) { 3615 case _FIODIRECTIO: 3616 return (nfs4_directio(vp, (int)arg, cr)); 3617 default: 3618 return (ENOTTY); 3619 } 3620 } 3621 3622 /* ARGSUSED */ 3623 int 3624 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3625 caller_context_t *ct) 3626 { 3627 int error; 3628 rnode4_t *rp = VTOR4(vp); 3629 3630 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3631 return (EIO); 3632 /* 3633 * If it has been specified that the return value will 3634 * just be used as a hint, and we are only being asked 3635 * for size, fsid or rdevid, then return the client's 3636 * notion of these values without checking to make sure 3637 * that the attribute cache is up to date. 3638 * The whole point is to avoid an over the wire GETATTR 3639 * call. 3640 */ 3641 if (flags & ATTR_HINT) { 3642 if (vap->va_mask == 3643 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) { 3644 mutex_enter(&rp->r_statelock); 3645 if (vap->va_mask | AT_SIZE) 3646 vap->va_size = rp->r_size; 3647 if (vap->va_mask | AT_FSID) 3648 vap->va_fsid = rp->r_attr.va_fsid; 3649 if (vap->va_mask | AT_RDEV) 3650 vap->va_rdev = rp->r_attr.va_rdev; 3651 mutex_exit(&rp->r_statelock); 3652 return (0); 3653 } 3654 } 3655 3656 /* 3657 * Only need to flush pages if asking for the mtime 3658 * and if there any dirty pages or any outstanding 3659 * asynchronous (write) requests for this file. 3660 */ 3661 if (vap->va_mask & AT_MTIME) { 3662 rp = VTOR4(vp); 3663 if (nfs4_has_pages(vp)) { 3664 mutex_enter(&rp->r_statev4_lock); 3665 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3666 mutex_exit(&rp->r_statev4_lock); 3667 if (rp->r_flags & R4DIRTY || 3668 rp->r_awcount > 0) { 3669 mutex_enter(&rp->r_statelock); 3670 rp->r_gcount++; 3671 mutex_exit(&rp->r_statelock); 3672 error = 3673 nfs4_putpage(vp, (u_offset_t)0, 3674 0, 0, cr, NULL); 3675 mutex_enter(&rp->r_statelock); 3676 if (error && (error == ENOSPC || 3677 error == EDQUOT)) { 3678 if (!rp->r_error) 3679 rp->r_error = error; 3680 } 3681 if (--rp->r_gcount == 0) 3682 cv_broadcast(&rp->r_cv); 3683 mutex_exit(&rp->r_statelock); 3684 } 3685 } else { 3686 mutex_exit(&rp->r_statev4_lock); 3687 } 3688 } 3689 } 3690 return (nfs4getattr(vp, vap, cr)); 3691 } 3692 3693 int 3694 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3695 { 3696 /* 3697 * If these are the only two bits cleared 3698 * on the server then return 0 (OK) else 3699 * return 1 (BAD). 3700 */ 3701 on_client &= ~(S_ISUID|S_ISGID); 3702 if (on_client == from_server) 3703 return (0); 3704 else 3705 return (1); 3706 } 3707 3708 /*ARGSUSED4*/ 3709 static int 3710 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3711 caller_context_t *ct) 3712 { 3713 if (vap->va_mask & AT_NOSET) 3714 return (EINVAL); 3715 3716 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3717 return (EIO); 3718 3719 /* 3720 * Don't call secpolicy_vnode_setattr, the client cannot 3721 * use its cached attributes to make security decisions 3722 * as the server may be faking mode bits or mapping uid/gid. 3723 * Always just let the server to the checking. 3724 * If we provide the ability to remove basic priviledges 3725 * to setattr (e.g. basic without chmod) then we will 3726 * need to add a check here before calling the server. 3727 */ 3728 3729 return (nfs4setattr(vp, vap, flags, cr, NULL)); 3730 } 3731 3732 /* 3733 * To replace the "guarded" version 3 setattr, we use two types of compound 3734 * setattr requests: 3735 * 1. The "normal" setattr, used when the size of the file isn't being 3736 * changed - { Putfh <fh>; Setattr; Getattr }/ 3737 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3738 * with only ctime as the argument. If the server ctime differs from 3739 * what is cached on the client, the verify will fail, but we would 3740 * already have the ctime from the preceding getattr, so just set it 3741 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3742 * Setattr; Getattr }. 3743 * 3744 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3745 * this setattr and NULL if they are not. 3746 */ 3747 static int 3748 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3749 vsecattr_t *vsap) 3750 { 3751 COMPOUND4args_clnt args; 3752 COMPOUND4res_clnt res, *resp = NULL; 3753 nfs4_ga_res_t *garp = NULL; 3754 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3755 nfs_argop4 argop[5]; 3756 int verify_argop = -1; 3757 int setattr_argop = 1; 3758 nfs_resop4 *resop; 3759 vattr_t va; 3760 rnode4_t *rp; 3761 int doqueue = 1; 3762 uint_t mask = vap->va_mask; 3763 mode_t omode; 3764 vsecattr_t *vsp; 3765 timestruc_t ctime; 3766 bool_t needrecov = FALSE; 3767 nfs4_recov_state_t recov_state; 3768 nfs4_stateid_types_t sid_types; 3769 stateid4 stateid; 3770 hrtime_t t; 3771 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3772 servinfo4_t *svp; 3773 bitmap4 supp_attrs; 3774 3775 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3776 rp = VTOR4(vp); 3777 nfs4_init_stateid_types(&sid_types); 3778 3779 /* 3780 * Only need to flush pages if there are any pages and 3781 * if the file is marked as dirty in some fashion. The 3782 * file must be flushed so that we can accurately 3783 * determine the size of the file and the cached data 3784 * after the SETATTR returns. A file is considered to 3785 * be dirty if it is either marked with R4DIRTY, has 3786 * outstanding i/o's active, or is mmap'd. In this 3787 * last case, we can't tell whether there are dirty 3788 * pages, so we flush just to be sure. 3789 */ 3790 if (nfs4_has_pages(vp) && 3791 ((rp->r_flags & R4DIRTY) || 3792 rp->r_count > 0 || 3793 rp->r_mapcnt > 0)) { 3794 ASSERT(vp->v_type != VCHR); 3795 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL); 3796 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3797 mutex_enter(&rp->r_statelock); 3798 if (!rp->r_error) 3799 rp->r_error = e.error; 3800 mutex_exit(&rp->r_statelock); 3801 } 3802 } 3803 3804 if (mask & AT_SIZE) { 3805 /* 3806 * Verification setattr compound for non-deleg AT_SIZE: 3807 * { Putfh; Getattr; Verify; Setattr; Getattr } 3808 * Set ctime local here (outside the do_again label) 3809 * so that subsequent retries (after failed VERIFY) 3810 * will use ctime from GETATTR results (from failed 3811 * verify compound) as VERIFY arg. 3812 * If file has delegation, then VERIFY(time_metadata) 3813 * is of little added value, so don't bother. 3814 */ 3815 mutex_enter(&rp->r_statev4_lock); 3816 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3817 rp->r_deleg_return_pending) { 3818 numops = 5; 3819 ctime = rp->r_attr.va_ctime; 3820 } 3821 mutex_exit(&rp->r_statev4_lock); 3822 } 3823 3824 recov_state.rs_flags = 0; 3825 recov_state.rs_num_retry_despite_err = 0; 3826 3827 args.ctag = TAG_SETATTR; 3828 do_again: 3829 recov_retry: 3830 setattr_argop = numops - 2; 3831 3832 args.array = argop; 3833 args.array_len = numops; 3834 3835 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3836 if (e.error) 3837 return (e.error); 3838 3839 3840 /* putfh target fh */ 3841 argop[0].argop = OP_CPUTFH; 3842 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3843 3844 if (numops == 5) { 3845 /* 3846 * We only care about the ctime, but need to get mtime 3847 * and size for proper cache update. 3848 */ 3849 /* getattr */ 3850 argop[1].argop = OP_GETATTR; 3851 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3852 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3853 3854 /* verify - set later in loop */ 3855 verify_argop = 2; 3856 } 3857 3858 /* setattr */ 3859 svp = rp->r_server; 3860 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3861 supp_attrs = svp->sv_supp_attrs; 3862 nfs_rw_exit(&svp->sv_lock); 3863 3864 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3865 supp_attrs, &e.error, &sid_types); 3866 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3867 if (e.error) { 3868 /* req time field(s) overflow - return immediately */ 3869 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3870 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3871 opsetattr.obj_attributes); 3872 return (e.error); 3873 } 3874 omode = rp->r_attr.va_mode; 3875 3876 /* getattr */ 3877 argop[numops-1].argop = OP_GETATTR; 3878 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3879 /* 3880 * If we are setting the ACL (indicated only by vsap != NULL), request 3881 * the ACL in this getattr. The ACL returned from this getattr will be 3882 * used in updating the ACL cache. 3883 */ 3884 if (vsap != NULL) 3885 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3886 FATTR4_ACL_MASK; 3887 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3888 3889 /* 3890 * setattr iterates if the object size is set and the cached ctime 3891 * does not match the file ctime. In that case, verify the ctime first. 3892 */ 3893 3894 do { 3895 if (verify_argop != -1) { 3896 /* 3897 * Verify that the ctime match before doing setattr. 3898 */ 3899 va.va_mask = AT_CTIME; 3900 va.va_ctime = ctime; 3901 svp = rp->r_server; 3902 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3903 supp_attrs = svp->sv_supp_attrs; 3904 nfs_rw_exit(&svp->sv_lock); 3905 e.error = nfs4args_verify(&argop[verify_argop], &va, 3906 OP_VERIFY, supp_attrs); 3907 if (e.error) { 3908 /* req time field(s) overflow - return */ 3909 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3910 needrecov); 3911 break; 3912 } 3913 } 3914 3915 doqueue = 1; 3916 3917 t = gethrtime(); 3918 3919 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3920 3921 /* 3922 * Purge the access cache and ACL cache if changing either the 3923 * owner of the file, the group owner, or the mode. These may 3924 * change the access permissions of the file, so purge old 3925 * information and start over again. 3926 */ 3927 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3928 (void) nfs4_access_purge_rp(rp); 3929 if (rp->r_secattr != NULL) { 3930 mutex_enter(&rp->r_statelock); 3931 vsp = rp->r_secattr; 3932 rp->r_secattr = NULL; 3933 mutex_exit(&rp->r_statelock); 3934 if (vsp != NULL) 3935 nfs4_acl_free_cache(vsp); 3936 } 3937 } 3938 3939 /* 3940 * If res.array_len == numops, then everything succeeded, 3941 * except for possibly the final getattr. If only the 3942 * last getattr failed, give up, and don't try recovery. 3943 */ 3944 if (res.array_len == numops) { 3945 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3946 needrecov); 3947 if (! e.error) 3948 resp = &res; 3949 break; 3950 } 3951 3952 /* 3953 * if either rpc call failed or completely succeeded - done 3954 */ 3955 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3956 if (e.error) { 3957 PURGE_ATTRCACHE4(vp); 3958 if (!needrecov) { 3959 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3960 needrecov); 3961 break; 3962 } 3963 } 3964 3965 /* 3966 * Do proper retry for OLD_STATEID outside of the normal 3967 * recovery framework. 3968 */ 3969 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3970 sid_types.cur_sid_type != SPEC_SID && 3971 sid_types.cur_sid_type != NO_SID) { 3972 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3973 needrecov); 3974 nfs4_save_stateid(&stateid, &sid_types); 3975 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3976 opsetattr.obj_attributes); 3977 if (verify_argop != -1) { 3978 nfs4args_verify_free(&argop[verify_argop]); 3979 verify_argop = -1; 3980 } 3981 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3982 goto recov_retry; 3983 } 3984 3985 if (needrecov) { 3986 bool_t abort; 3987 3988 abort = nfs4_start_recovery(&e, 3989 VTOMI4(vp), vp, NULL, NULL, NULL, 3990 OP_SETATTR, NULL, NULL, NULL); 3991 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3992 needrecov); 3993 /* 3994 * Do not retry if we failed with OLD_STATEID using 3995 * a special stateid. This is done to avoid looping 3996 * with a broken server. 3997 */ 3998 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3999 (sid_types.cur_sid_type == SPEC_SID || 4000 sid_types.cur_sid_type == NO_SID)) 4001 abort = TRUE; 4002 if (!e.error) { 4003 if (res.status == NFS4ERR_BADOWNER) 4004 nfs4_log_badowner(VTOMI4(vp), 4005 OP_SETATTR); 4006 4007 e.error = geterrno4(res.status); 4008 (void) xdr_free(xdr_COMPOUND4res_clnt, 4009 (caddr_t)&res); 4010 } 4011 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4012 opsetattr.obj_attributes); 4013 if (verify_argop != -1) { 4014 nfs4args_verify_free(&argop[verify_argop]); 4015 verify_argop = -1; 4016 } 4017 if (abort == FALSE) { 4018 /* 4019 * Need to retry all possible stateids in 4020 * case the recovery error wasn't stateid 4021 * related or the stateids have become 4022 * stale (server reboot). 4023 */ 4024 nfs4_init_stateid_types(&sid_types); 4025 goto recov_retry; 4026 } 4027 return (e.error); 4028 } 4029 4030 /* 4031 * Need to call nfs4_end_op before nfs4getattr to 4032 * avoid potential nfs4_start_op deadlock. See RFE 4033 * 4777612. Calls to nfs4_invalidate_pages() and 4034 * nfs4_purge_stale_fh() might also generate over the 4035 * wire calls which my cause nfs4_start_op() deadlock. 4036 */ 4037 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4038 4039 /* 4040 * Check to update lease. 4041 */ 4042 resp = &res; 4043 if (res.status == NFS4_OK) { 4044 break; 4045 } 4046 4047 /* 4048 * Check if verify failed to see if try again 4049 */ 4050 if ((verify_argop == -1) || (res.array_len != 3)) { 4051 /* 4052 * can't continue... 4053 */ 4054 if (res.status == NFS4ERR_BADOWNER) 4055 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 4056 4057 e.error = geterrno4(res.status); 4058 } else { 4059 /* 4060 * When the verify request fails, the client ctime is 4061 * not in sync with the server. This is the same as 4062 * the version 3 "not synchronized" error, and we 4063 * handle it in a similar manner (XXX do we need to???). 4064 * Use the ctime returned in the first getattr for 4065 * the input to the next verify. 4066 * If we couldn't get the attributes, then we give up 4067 * because we can't complete the operation as required. 4068 */ 4069 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 4070 } 4071 if (e.error) { 4072 PURGE_ATTRCACHE4(vp); 4073 nfs4_purge_stale_fh(e.error, vp, cr); 4074 } else { 4075 /* 4076 * retry with a new verify value 4077 */ 4078 ctime = garp->n4g_va.va_ctime; 4079 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4080 resp = NULL; 4081 } 4082 if (!e.error) { 4083 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4084 opsetattr.obj_attributes); 4085 if (verify_argop != -1) { 4086 nfs4args_verify_free(&argop[verify_argop]); 4087 verify_argop = -1; 4088 } 4089 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4090 goto do_again; 4091 } 4092 } while (!e.error); 4093 4094 if (e.error) { 4095 /* 4096 * If we are here, rfs4call has an irrecoverable error - return 4097 */ 4098 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4099 opsetattr.obj_attributes); 4100 if (verify_argop != -1) { 4101 nfs4args_verify_free(&argop[verify_argop]); 4102 verify_argop = -1; 4103 } 4104 if (resp) 4105 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4106 return (e.error); 4107 } 4108 4109 4110 4111 /* 4112 * If changing the size of the file, invalidate 4113 * any local cached data which is no longer part 4114 * of the file. We also possibly invalidate the 4115 * last page in the file. We could use 4116 * pvn_vpzero(), but this would mark the page as 4117 * modified and require it to be written back to 4118 * the server for no particularly good reason. 4119 * This way, if we access it, then we bring it 4120 * back in. A read should be cheaper than a 4121 * write. 4122 */ 4123 if (mask & AT_SIZE) { 4124 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4125 } 4126 4127 /* either no error or one of the postop getattr failed */ 4128 4129 /* 4130 * XXX Perform a simplified version of wcc checking. Instead of 4131 * have another getattr to get pre-op, just purge cache if 4132 * any of the ops prior to and including the getattr failed. 4133 * If the getattr succeeded then update the attrcache accordingly. 4134 */ 4135 4136 garp = NULL; 4137 if (res.status == NFS4_OK) { 4138 /* 4139 * Last getattr 4140 */ 4141 resop = &res.array[numops - 1]; 4142 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4143 } 4144 /* 4145 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4146 * rather than filling it. See the function itself for details. 4147 */ 4148 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4149 if (garp != NULL) { 4150 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4151 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4152 vs_ace4_destroy(&garp->n4g_vsa); 4153 } else { 4154 if (vsap != NULL) { 4155 /* 4156 * The ACL was supposed to be set and to be 4157 * returned in the last getattr of this 4158 * compound, but for some reason the getattr 4159 * result doesn't contain the ACL. In this 4160 * case, purge the ACL cache. 4161 */ 4162 if (rp->r_secattr != NULL) { 4163 mutex_enter(&rp->r_statelock); 4164 vsp = rp->r_secattr; 4165 rp->r_secattr = NULL; 4166 mutex_exit(&rp->r_statelock); 4167 if (vsp != NULL) 4168 nfs4_acl_free_cache(vsp); 4169 } 4170 } 4171 } 4172 } 4173 4174 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4175 /* 4176 * Set the size, rather than relying on getting it updated 4177 * via a GETATTR. With delegations the client tries to 4178 * suppress GETATTR calls. 4179 */ 4180 mutex_enter(&rp->r_statelock); 4181 rp->r_size = vap->va_size; 4182 mutex_exit(&rp->r_statelock); 4183 } 4184 4185 /* 4186 * Can free up request args and res 4187 */ 4188 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4189 opsetattr.obj_attributes); 4190 if (verify_argop != -1) { 4191 nfs4args_verify_free(&argop[verify_argop]); 4192 verify_argop = -1; 4193 } 4194 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4195 4196 /* 4197 * Some servers will change the mode to clear the setuid 4198 * and setgid bits when changing the uid or gid. The 4199 * client needs to compensate appropriately. 4200 */ 4201 if (mask & (AT_UID | AT_GID)) { 4202 int terror, do_setattr; 4203 4204 do_setattr = 0; 4205 va.va_mask = AT_MODE; 4206 terror = nfs4getattr(vp, &va, cr); 4207 if (!terror && 4208 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4209 (!(mask & AT_MODE) && va.va_mode != omode))) { 4210 va.va_mask = AT_MODE; 4211 if (mask & AT_MODE) { 4212 /* 4213 * We asked the mode to be changed and what 4214 * we just got from the server in getattr is 4215 * not what we wanted it to be, so set it now. 4216 */ 4217 va.va_mode = vap->va_mode; 4218 do_setattr = 1; 4219 } else { 4220 /* 4221 * We did not ask the mode to be changed, 4222 * Check to see that the server just cleared 4223 * I_SUID and I_GUID from it. If not then 4224 * set mode to omode with UID/GID cleared. 4225 */ 4226 if (nfs4_compare_modes(va.va_mode, omode)) { 4227 omode &= ~(S_ISUID|S_ISGID); 4228 va.va_mode = omode; 4229 do_setattr = 1; 4230 } 4231 } 4232 4233 if (do_setattr) 4234 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4235 } 4236 } 4237 4238 return (e.error); 4239 } 4240 4241 /* ARGSUSED */ 4242 static int 4243 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct) 4244 { 4245 COMPOUND4args_clnt args; 4246 COMPOUND4res_clnt res; 4247 int doqueue; 4248 uint32_t acc, resacc, argacc; 4249 rnode4_t *rp; 4250 cred_t *cred, *ncr, *ncrfree = NULL; 4251 nfs4_access_type_t cacc; 4252 int num_ops; 4253 nfs_argop4 argop[3]; 4254 nfs_resop4 *resop; 4255 bool_t needrecov = FALSE, do_getattr; 4256 nfs4_recov_state_t recov_state; 4257 int rpc_error; 4258 hrtime_t t; 4259 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4260 mntinfo4_t *mi = VTOMI4(vp); 4261 4262 if (nfs_zone() != mi->mi_zone) 4263 return (EIO); 4264 4265 acc = 0; 4266 if (mode & VREAD) 4267 acc |= ACCESS4_READ; 4268 if (mode & VWRITE) { 4269 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4270 return (EROFS); 4271 if (vp->v_type == VDIR) 4272 acc |= ACCESS4_DELETE; 4273 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4274 } 4275 if (mode & VEXEC) { 4276 if (vp->v_type == VDIR) 4277 acc |= ACCESS4_LOOKUP; 4278 else 4279 acc |= ACCESS4_EXECUTE; 4280 } 4281 4282 if (VTOR4(vp)->r_acache != NULL) { 4283 e.error = nfs4_validate_caches(vp, cr); 4284 if (e.error) 4285 return (e.error); 4286 } 4287 4288 rp = VTOR4(vp); 4289 if (vp->v_type == VDIR) 4290 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4291 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4292 else 4293 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4294 ACCESS4_EXECUTE; 4295 recov_state.rs_flags = 0; 4296 recov_state.rs_num_retry_despite_err = 0; 4297 4298 cred = cr; 4299 /* 4300 * ncr and ncrfree both initially 4301 * point to the memory area returned 4302 * by crnetadjust(); 4303 * ncrfree not NULL when exiting means 4304 * that we need to release it 4305 */ 4306 ncr = crnetadjust(cred); 4307 ncrfree = ncr; 4308 4309 tryagain: 4310 cacc = nfs4_access_check(rp, acc, cred); 4311 if (cacc == NFS4_ACCESS_ALLOWED) { 4312 if (ncrfree != NULL) 4313 crfree(ncrfree); 4314 return (0); 4315 } 4316 if (cacc == NFS4_ACCESS_DENIED) { 4317 /* 4318 * If the cred can be adjusted, try again 4319 * with the new cred. 4320 */ 4321 if (ncr != NULL) { 4322 cred = ncr; 4323 ncr = NULL; 4324 goto tryagain; 4325 } 4326 if (ncrfree != NULL) 4327 crfree(ncrfree); 4328 return (EACCES); 4329 } 4330 4331 recov_retry: 4332 /* 4333 * Don't take with r_statev4_lock here. r_deleg_type could 4334 * change as soon as lock is released. Since it is an int, 4335 * there is no atomicity issue. 4336 */ 4337 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4338 num_ops = do_getattr ? 3 : 2; 4339 4340 args.ctag = TAG_ACCESS; 4341 4342 args.array_len = num_ops; 4343 args.array = argop; 4344 4345 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4346 &recov_state, NULL)) { 4347 if (ncrfree != NULL) 4348 crfree(ncrfree); 4349 return (e.error); 4350 } 4351 4352 /* putfh target fh */ 4353 argop[0].argop = OP_CPUTFH; 4354 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4355 4356 /* access */ 4357 argop[1].argop = OP_ACCESS; 4358 argop[1].nfs_argop4_u.opaccess.access = argacc; 4359 4360 /* getattr */ 4361 if (do_getattr) { 4362 argop[2].argop = OP_GETATTR; 4363 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4364 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4365 } 4366 4367 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4368 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4369 rnode4info(VTOR4(vp)))); 4370 4371 doqueue = 1; 4372 t = gethrtime(); 4373 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4374 rpc_error = e.error; 4375 4376 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4377 if (needrecov) { 4378 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4379 "nfs4_access: initiating recovery\n")); 4380 4381 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4382 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) { 4383 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4384 &recov_state, needrecov); 4385 if (!e.error) 4386 (void) xdr_free(xdr_COMPOUND4res_clnt, 4387 (caddr_t)&res); 4388 goto recov_retry; 4389 } 4390 } 4391 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4392 4393 if (e.error) 4394 goto out; 4395 4396 if (res.status) { 4397 e.error = geterrno4(res.status); 4398 /* 4399 * This might generate over the wire calls throught 4400 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4401 * here to avoid a deadlock. 4402 */ 4403 nfs4_purge_stale_fh(e.error, vp, cr); 4404 goto out; 4405 } 4406 resop = &res.array[1]; /* access res */ 4407 4408 resacc = resop->nfs_resop4_u.opaccess.access; 4409 4410 if (do_getattr) { 4411 resop++; /* getattr res */ 4412 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4413 t, cr, FALSE, NULL); 4414 } 4415 4416 if (!e.error) { 4417 nfs4_access_cache(rp, argacc, resacc, cred); 4418 /* 4419 * we just cached results with cred; if cred is the 4420 * adjusted credentials from crnetadjust, we do not want 4421 * to release them before exiting: hence setting ncrfree 4422 * to NULL 4423 */ 4424 if (cred != cr) 4425 ncrfree = NULL; 4426 /* XXX check the supported bits too? */ 4427 if ((acc & resacc) != acc) { 4428 /* 4429 * The following code implements the semantic 4430 * that a setuid root program has *at least* the 4431 * permissions of the user that is running the 4432 * program. See rfs3call() for more portions 4433 * of the implementation of this functionality. 4434 */ 4435 /* XXX-LP */ 4436 if (ncr != NULL) { 4437 (void) xdr_free(xdr_COMPOUND4res_clnt, 4438 (caddr_t)&res); 4439 cred = ncr; 4440 ncr = NULL; 4441 goto tryagain; 4442 } 4443 e.error = EACCES; 4444 } 4445 } 4446 4447 out: 4448 if (!rpc_error) 4449 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4450 4451 if (ncrfree != NULL) 4452 crfree(ncrfree); 4453 4454 return (e.error); 4455 } 4456 4457 /* ARGSUSED */ 4458 static int 4459 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct) 4460 { 4461 COMPOUND4args_clnt args; 4462 COMPOUND4res_clnt res; 4463 int doqueue; 4464 rnode4_t *rp; 4465 nfs_argop4 argop[3]; 4466 nfs_resop4 *resop; 4467 READLINK4res *lr_res; 4468 nfs4_ga_res_t *garp; 4469 uint_t len; 4470 char *linkdata; 4471 bool_t needrecov = FALSE; 4472 nfs4_recov_state_t recov_state; 4473 hrtime_t t; 4474 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4475 4476 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4477 return (EIO); 4478 /* 4479 * Can't readlink anything other than a symbolic link. 4480 */ 4481 if (vp->v_type != VLNK) 4482 return (EINVAL); 4483 4484 rp = VTOR4(vp); 4485 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4486 e.error = nfs4_validate_caches(vp, cr); 4487 if (e.error) 4488 return (e.error); 4489 mutex_enter(&rp->r_statelock); 4490 if (rp->r_symlink.contents != NULL) { 4491 e.error = uiomove(rp->r_symlink.contents, 4492 rp->r_symlink.len, UIO_READ, uiop); 4493 mutex_exit(&rp->r_statelock); 4494 return (e.error); 4495 } 4496 mutex_exit(&rp->r_statelock); 4497 } 4498 recov_state.rs_flags = 0; 4499 recov_state.rs_num_retry_despite_err = 0; 4500 4501 recov_retry: 4502 args.array_len = 3; 4503 args.array = argop; 4504 args.ctag = TAG_READLINK; 4505 4506 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4507 if (e.error) { 4508 return (e.error); 4509 } 4510 4511 /* 0. putfh symlink fh */ 4512 argop[0].argop = OP_CPUTFH; 4513 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4514 4515 /* 1. readlink */ 4516 argop[1].argop = OP_READLINK; 4517 4518 /* 2. getattr */ 4519 argop[2].argop = OP_GETATTR; 4520 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4521 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4522 4523 doqueue = 1; 4524 4525 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4526 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4527 rnode4info(VTOR4(vp)))); 4528 4529 t = gethrtime(); 4530 4531 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4532 4533 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4534 if (needrecov) { 4535 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4536 "nfs4_readlink: initiating recovery\n")); 4537 4538 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4539 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) { 4540 if (!e.error) 4541 (void) xdr_free(xdr_COMPOUND4res_clnt, 4542 (caddr_t)&res); 4543 4544 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4545 needrecov); 4546 goto recov_retry; 4547 } 4548 } 4549 4550 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4551 4552 if (e.error) 4553 return (e.error); 4554 4555 /* 4556 * There is an path in the code below which calls 4557 * nfs4_purge_stale_fh(), which may generate otw calls through 4558 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4559 * here to avoid nfs4_start_op() deadlock. 4560 */ 4561 4562 if (res.status && (res.array_len < args.array_len)) { 4563 /* 4564 * either Putfh or Link failed 4565 */ 4566 e.error = geterrno4(res.status); 4567 nfs4_purge_stale_fh(e.error, vp, cr); 4568 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4569 return (e.error); 4570 } 4571 4572 resop = &res.array[1]; /* readlink res */ 4573 lr_res = &resop->nfs_resop4_u.opreadlink; 4574 4575 /* 4576 * treat symlink names as data 4577 */ 4578 linkdata = utf8_to_str(&lr_res->link, &len, NULL); 4579 if (linkdata != NULL) { 4580 int uio_len = len - 1; 4581 /* len includes null byte, which we won't uiomove */ 4582 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4583 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4584 mutex_enter(&rp->r_statelock); 4585 if (rp->r_symlink.contents == NULL) { 4586 rp->r_symlink.contents = linkdata; 4587 rp->r_symlink.len = uio_len; 4588 rp->r_symlink.size = len; 4589 mutex_exit(&rp->r_statelock); 4590 } else { 4591 mutex_exit(&rp->r_statelock); 4592 kmem_free(linkdata, len); 4593 } 4594 } else { 4595 kmem_free(linkdata, len); 4596 } 4597 } 4598 if (res.status == NFS4_OK) { 4599 resop++; /* getattr res */ 4600 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4601 } 4602 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4603 4604 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4605 4606 /* 4607 * The over the wire error for attempting to readlink something 4608 * other than a symbolic link is ENXIO. However, we need to 4609 * return EINVAL instead of ENXIO, so we map it here. 4610 */ 4611 return (e.error == ENXIO ? EINVAL : e.error); 4612 } 4613 4614 /* 4615 * Flush local dirty pages to stable storage on the server. 4616 * 4617 * If FNODSYNC is specified, then there is nothing to do because 4618 * metadata changes are not cached on the client before being 4619 * sent to the server. 4620 */ 4621 /* ARGSUSED */ 4622 static int 4623 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 4624 { 4625 int error; 4626 4627 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4628 return (0); 4629 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4630 return (EIO); 4631 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4632 if (!error) 4633 error = VTOR4(vp)->r_error; 4634 return (error); 4635 } 4636 4637 /* 4638 * Weirdness: if the file was removed or the target of a rename 4639 * operation while it was open, it got renamed instead. Here we 4640 * remove the renamed file. 4641 */ 4642 /* ARGSUSED */ 4643 void 4644 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4645 { 4646 rnode4_t *rp; 4647 4648 ASSERT(vp != DNLC_NO_VNODE); 4649 4650 rp = VTOR4(vp); 4651 4652 if (IS_SHADOW(vp, rp)) { 4653 sv_inactive(vp); 4654 return; 4655 } 4656 4657 /* 4658 * If this is coming from the wrong zone, we let someone in the right 4659 * zone take care of it asynchronously. We can get here due to 4660 * VN_RELE() being called from pageout() or fsflush(). This call may 4661 * potentially turn into an expensive no-op if, for instance, v_count 4662 * gets incremented in the meantime, but it's still correct. 4663 */ 4664 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4665 nfs4_async_inactive(vp, cr); 4666 return; 4667 } 4668 4669 /* 4670 * Some of the cleanup steps might require over-the-wire 4671 * operations. Since VOP_INACTIVE can get called as a result of 4672 * other over-the-wire operations (e.g., an attribute cache update 4673 * can lead to a DNLC purge), doing those steps now would lead to a 4674 * nested call to the recovery framework, which can deadlock. So 4675 * do any over-the-wire cleanups asynchronously, in a separate 4676 * thread. 4677 */ 4678 4679 mutex_enter(&rp->r_os_lock); 4680 mutex_enter(&rp->r_statelock); 4681 mutex_enter(&rp->r_statev4_lock); 4682 4683 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4684 mutex_exit(&rp->r_statev4_lock); 4685 mutex_exit(&rp->r_statelock); 4686 mutex_exit(&rp->r_os_lock); 4687 nfs4_async_inactive(vp, cr); 4688 return; 4689 } 4690 4691 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4692 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4693 mutex_exit(&rp->r_statev4_lock); 4694 mutex_exit(&rp->r_statelock); 4695 mutex_exit(&rp->r_os_lock); 4696 nfs4_async_inactive(vp, cr); 4697 return; 4698 } 4699 4700 if (rp->r_unldvp != NULL) { 4701 mutex_exit(&rp->r_statev4_lock); 4702 mutex_exit(&rp->r_statelock); 4703 mutex_exit(&rp->r_os_lock); 4704 nfs4_async_inactive(vp, cr); 4705 return; 4706 } 4707 mutex_exit(&rp->r_statev4_lock); 4708 mutex_exit(&rp->r_statelock); 4709 mutex_exit(&rp->r_os_lock); 4710 4711 rp4_addfree(rp, cr); 4712 } 4713 4714 /* 4715 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4716 * various bits of state. The caller must not refer to vp after this call. 4717 */ 4718 4719 void 4720 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4721 { 4722 rnode4_t *rp = VTOR4(vp); 4723 nfs4_recov_state_t recov_state; 4724 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4725 vnode_t *unldvp; 4726 char *unlname; 4727 cred_t *unlcred; 4728 COMPOUND4args_clnt args; 4729 COMPOUND4res_clnt res, *resp; 4730 nfs_argop4 argop[2]; 4731 int doqueue; 4732 #ifdef DEBUG 4733 char *name; 4734 #endif 4735 4736 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4737 ASSERT(!IS_SHADOW(vp, rp)); 4738 4739 #ifdef DEBUG 4740 name = fn_name(VTOSV(vp)->sv_name); 4741 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4742 "release vnode %s", name)); 4743 kmem_free(name, MAXNAMELEN); 4744 #endif 4745 4746 if (vp->v_type == VREG) { 4747 bool_t recov_failed = FALSE; 4748 4749 e.error = nfs4close_all(vp, cr); 4750 if (e.error) { 4751 /* Check to see if recovery failed */ 4752 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4753 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4754 recov_failed = TRUE; 4755 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4756 if (!recov_failed) { 4757 mutex_enter(&rp->r_statelock); 4758 if (rp->r_flags & R4RECOVERR) 4759 recov_failed = TRUE; 4760 mutex_exit(&rp->r_statelock); 4761 } 4762 if (recov_failed) { 4763 NFS4_DEBUG(nfs4_client_recov_debug, 4764 (CE_NOTE, "nfs4_inactive_otw: " 4765 "close failed (recovery failure)")); 4766 } 4767 } 4768 } 4769 4770 redo: 4771 if (rp->r_unldvp == NULL) { 4772 rp4_addfree(rp, cr); 4773 return; 4774 } 4775 4776 /* 4777 * Save the vnode pointer for the directory where the 4778 * unlinked-open file got renamed, then set it to NULL 4779 * to prevent another thread from getting here before 4780 * we're done with the remove. While we have the 4781 * statelock, make local copies of the pertinent rnode 4782 * fields. If we weren't to do this in an atomic way, the 4783 * the unl* fields could become inconsistent with respect 4784 * to each other due to a race condition between this 4785 * code and nfs_remove(). See bug report 1034328. 4786 */ 4787 mutex_enter(&rp->r_statelock); 4788 if (rp->r_unldvp == NULL) { 4789 mutex_exit(&rp->r_statelock); 4790 rp4_addfree(rp, cr); 4791 return; 4792 } 4793 4794 unldvp = rp->r_unldvp; 4795 rp->r_unldvp = NULL; 4796 unlname = rp->r_unlname; 4797 rp->r_unlname = NULL; 4798 unlcred = rp->r_unlcred; 4799 rp->r_unlcred = NULL; 4800 mutex_exit(&rp->r_statelock); 4801 4802 /* 4803 * If there are any dirty pages left, then flush 4804 * them. This is unfortunate because they just 4805 * may get thrown away during the remove operation, 4806 * but we have to do this for correctness. 4807 */ 4808 if (nfs4_has_pages(vp) && 4809 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4810 ASSERT(vp->v_type != VCHR); 4811 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL); 4812 if (e.error) { 4813 mutex_enter(&rp->r_statelock); 4814 if (!rp->r_error) 4815 rp->r_error = e.error; 4816 mutex_exit(&rp->r_statelock); 4817 } 4818 } 4819 4820 recov_state.rs_flags = 0; 4821 recov_state.rs_num_retry_despite_err = 0; 4822 recov_retry_remove: 4823 /* 4824 * Do the remove operation on the renamed file 4825 */ 4826 args.ctag = TAG_INACTIVE; 4827 4828 /* 4829 * Remove ops: putfh dir; remove 4830 */ 4831 args.array_len = 2; 4832 args.array = argop; 4833 4834 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4835 if (e.error) { 4836 kmem_free(unlname, MAXNAMELEN); 4837 crfree(unlcred); 4838 VN_RELE(unldvp); 4839 /* 4840 * Try again; this time around r_unldvp will be NULL, so we'll 4841 * just call rp4_addfree() and return. 4842 */ 4843 goto redo; 4844 } 4845 4846 /* putfh directory */ 4847 argop[0].argop = OP_CPUTFH; 4848 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4849 4850 /* remove */ 4851 argop[1].argop = OP_CREMOVE; 4852 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4853 4854 doqueue = 1; 4855 resp = &res; 4856 4857 #if 0 /* notyet */ 4858 /* 4859 * Can't do this yet. We may be being called from 4860 * dnlc_purge_XXX while that routine is holding a 4861 * mutex lock to the nc_rele list. The calls to 4862 * nfs3_cache_wcc_data may result in calls to 4863 * dnlc_purge_XXX. This will result in a deadlock. 4864 */ 4865 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4866 if (e.error) { 4867 PURGE_ATTRCACHE4(unldvp); 4868 resp = NULL; 4869 } else if (res.status) { 4870 e.error = geterrno4(res.status); 4871 PURGE_ATTRCACHE4(unldvp); 4872 /* 4873 * This code is inactive right now 4874 * but if made active there should 4875 * be a nfs4_end_op() call before 4876 * nfs4_purge_stale_fh to avoid start_op() 4877 * deadlock. See BugId: 4948726 4878 */ 4879 nfs4_purge_stale_fh(error, unldvp, cr); 4880 } else { 4881 nfs_resop4 *resop; 4882 REMOVE4res *rm_res; 4883 4884 resop = &res.array[1]; 4885 rm_res = &resop->nfs_resop4_u.opremove; 4886 /* 4887 * Update directory cache attribute, 4888 * readdir and dnlc caches. 4889 */ 4890 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4891 } 4892 #else 4893 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4894 4895 PURGE_ATTRCACHE4(unldvp); 4896 #endif 4897 4898 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4899 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4900 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 4901 if (!e.error) 4902 (void) xdr_free(xdr_COMPOUND4res_clnt, 4903 (caddr_t)&res); 4904 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4905 &recov_state, TRUE); 4906 goto recov_retry_remove; 4907 } 4908 } 4909 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4910 4911 /* 4912 * Release stuff held for the remove 4913 */ 4914 VN_RELE(unldvp); 4915 if (!e.error && resp) 4916 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4917 4918 kmem_free(unlname, MAXNAMELEN); 4919 crfree(unlcred); 4920 goto redo; 4921 } 4922 4923 /* 4924 * Remote file system operations having to do with directory manipulation. 4925 */ 4926 /* ARGSUSED3 */ 4927 int 4928 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4929 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 4930 int *direntflags, pathname_t *realpnp) 4931 { 4932 int error; 4933 vnode_t *vp, *avp = NULL; 4934 rnode4_t *drp; 4935 4936 *vpp = NULL; 4937 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4938 return (EPERM); 4939 /* 4940 * if LOOKUP_XATTR, must replace dvp (object) with 4941 * object's attrdir before continuing with lookup 4942 */ 4943 if (flags & LOOKUP_XATTR) { 4944 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4945 if (error) 4946 return (error); 4947 4948 dvp = avp; 4949 4950 /* 4951 * If lookup is for "", just return dvp now. The attrdir 4952 * has already been activated (from nfs4lookup_xattr), and 4953 * the caller will RELE the original dvp -- not 4954 * the attrdir. So, set vpp and return. 4955 * Currently, when the LOOKUP_XATTR flag is 4956 * passed to VOP_LOOKUP, the name is always empty, and 4957 * shortcircuiting here avoids 3 unneeded lock/unlock 4958 * pairs. 4959 * 4960 * If a non-empty name was provided, then it is the 4961 * attribute name, and it will be looked up below. 4962 */ 4963 if (*nm == '\0') { 4964 *vpp = dvp; 4965 return (0); 4966 } 4967 4968 /* 4969 * The vfs layer never sends a name when asking for the 4970 * attrdir, so we should never get here (unless of course 4971 * name is passed at some time in future -- at which time 4972 * we'll blow up here). 4973 */ 4974 ASSERT(0); 4975 } 4976 4977 drp = VTOR4(dvp); 4978 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4979 return (EINTR); 4980 4981 error = nfs4lookup(dvp, nm, vpp, cr, 0); 4982 nfs_rw_exit(&drp->r_rwlock); 4983 4984 /* 4985 * If vnode is a device, create special vnode. 4986 */ 4987 if (!error && ISVDEV((*vpp)->v_type)) { 4988 vp = *vpp; 4989 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 4990 VN_RELE(vp); 4991 } 4992 4993 return (error); 4994 } 4995 4996 /* ARGSUSED */ 4997 static int 4998 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 4999 { 5000 int error; 5001 rnode4_t *drp; 5002 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 5003 mntinfo4_t *mi; 5004 5005 mi = VTOMI4(dvp); 5006 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) && 5007 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS)) 5008 return (EINVAL); 5009 5010 drp = VTOR4(dvp); 5011 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5012 return (EINTR); 5013 5014 mutex_enter(&drp->r_statelock); 5015 /* 5016 * If the server doesn't support xattrs just return EINVAL 5017 */ 5018 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 5019 mutex_exit(&drp->r_statelock); 5020 nfs_rw_exit(&drp->r_rwlock); 5021 return (EINVAL); 5022 } 5023 5024 /* 5025 * If there is a cached xattr directory entry, 5026 * use it as long as the attributes are valid. If the 5027 * attributes are not valid, take the simple approach and 5028 * free the cached value and re-fetch a new value. 5029 * 5030 * We don't negative entry cache for now, if we did we 5031 * would need to check if the file has changed on every 5032 * lookup. But xattrs don't exist very often and failing 5033 * an openattr is not much more expensive than and NVERIFY or GETATTR 5034 * so do an openattr over the wire for now. 5035 */ 5036 if (drp->r_xattr_dir != NULL) { 5037 if (ATTRCACHE4_VALID(dvp)) { 5038 VN_HOLD(drp->r_xattr_dir); 5039 *vpp = drp->r_xattr_dir; 5040 mutex_exit(&drp->r_statelock); 5041 nfs_rw_exit(&drp->r_rwlock); 5042 return (0); 5043 } 5044 VN_RELE(drp->r_xattr_dir); 5045 drp->r_xattr_dir = NULL; 5046 } 5047 mutex_exit(&drp->r_statelock); 5048 5049 error = nfs4openattr(dvp, vpp, cflag, cr); 5050 5051 nfs_rw_exit(&drp->r_rwlock); 5052 5053 return (error); 5054 } 5055 5056 static int 5057 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 5058 { 5059 int error; 5060 rnode4_t *drp; 5061 5062 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5063 5064 /* 5065 * If lookup is for "", just return dvp. Don't need 5066 * to send it over the wire, look it up in the dnlc, 5067 * or perform any access checks. 5068 */ 5069 if (*nm == '\0') { 5070 VN_HOLD(dvp); 5071 *vpp = dvp; 5072 return (0); 5073 } 5074 5075 /* 5076 * Can't do lookups in non-directories. 5077 */ 5078 if (dvp->v_type != VDIR) 5079 return (ENOTDIR); 5080 5081 /* 5082 * If lookup is for ".", just return dvp. Don't need 5083 * to send it over the wire or look it up in the dnlc, 5084 * just need to check access. 5085 */ 5086 if (nm[0] == '.' && nm[1] == '\0') { 5087 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5088 if (error) 5089 return (error); 5090 VN_HOLD(dvp); 5091 *vpp = dvp; 5092 return (0); 5093 } 5094 5095 drp = VTOR4(dvp); 5096 if (!(drp->r_flags & R4LOOKUP)) { 5097 mutex_enter(&drp->r_statelock); 5098 drp->r_flags |= R4LOOKUP; 5099 mutex_exit(&drp->r_statelock); 5100 } 5101 5102 *vpp = NULL; 5103 /* 5104 * Lookup this name in the DNLC. If there is no entry 5105 * lookup over the wire. 5106 */ 5107 if (!skipdnlc) 5108 *vpp = dnlc_lookup(dvp, nm); 5109 if (*vpp == NULL) { 5110 /* 5111 * We need to go over the wire to lookup the name. 5112 */ 5113 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5114 } 5115 5116 /* 5117 * We hit on the dnlc 5118 */ 5119 if (*vpp != DNLC_NO_VNODE || 5120 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5121 /* 5122 * But our attrs may not be valid. 5123 */ 5124 if (ATTRCACHE4_VALID(dvp)) { 5125 error = nfs4_waitfor_purge_complete(dvp); 5126 if (error) { 5127 VN_RELE(*vpp); 5128 *vpp = NULL; 5129 return (error); 5130 } 5131 5132 /* 5133 * If after the purge completes, check to make sure 5134 * our attrs are still valid. 5135 */ 5136 if (ATTRCACHE4_VALID(dvp)) { 5137 /* 5138 * If we waited for a purge we may have 5139 * lost our vnode so look it up again. 5140 */ 5141 VN_RELE(*vpp); 5142 *vpp = dnlc_lookup(dvp, nm); 5143 if (*vpp == NULL) 5144 return (nfs4lookupnew_otw(dvp, 5145 nm, vpp, cr)); 5146 5147 /* 5148 * The access cache should almost always hit 5149 */ 5150 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5151 5152 if (error) { 5153 VN_RELE(*vpp); 5154 *vpp = NULL; 5155 return (error); 5156 } 5157 if (*vpp == DNLC_NO_VNODE) { 5158 VN_RELE(*vpp); 5159 *vpp = NULL; 5160 return (ENOENT); 5161 } 5162 return (0); 5163 } 5164 } 5165 } 5166 5167 ASSERT(*vpp != NULL); 5168 5169 /* 5170 * We may have gotten here we have one of the following cases: 5171 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5172 * need to validate them. 5173 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5174 * must validate. 5175 * 5176 * Go to the server and check if the directory has changed, if 5177 * it hasn't we are done and can use the dnlc entry. 5178 */ 5179 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5180 } 5181 5182 /* 5183 * Go to the server and check if the directory has changed, if 5184 * it hasn't we are done and can use the dnlc entry. If it 5185 * has changed we get a new copy of its attributes and check 5186 * the access for VEXEC, then relookup the filename and 5187 * get its filehandle and attributes. 5188 * 5189 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5190 * if the NVERIFY failed we must 5191 * purge the caches 5192 * cache new attributes (will set r_time_attr_inval) 5193 * cache new access 5194 * recheck VEXEC access 5195 * add name to dnlc, possibly negative 5196 * if LOOKUP succeeded 5197 * cache new attributes 5198 * else 5199 * set a new r_time_attr_inval for dvp 5200 * check to make sure we have access 5201 * 5202 * The vpp returned is the vnode passed in if the directory is valid, 5203 * a new vnode if successful lookup, or NULL on error. 5204 */ 5205 static int 5206 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5207 { 5208 COMPOUND4args_clnt args; 5209 COMPOUND4res_clnt res; 5210 fattr4 *ver_fattr; 5211 fattr4_change dchange; 5212 int32_t *ptr; 5213 int argoplist_size = 7 * sizeof (nfs_argop4); 5214 nfs_argop4 *argop; 5215 int doqueue; 5216 mntinfo4_t *mi; 5217 nfs4_recov_state_t recov_state; 5218 hrtime_t t; 5219 int isdotdot; 5220 vnode_t *nvp; 5221 nfs_fh4 *fhp; 5222 nfs4_sharedfh_t *sfhp; 5223 nfs4_access_type_t cacc; 5224 rnode4_t *nrp; 5225 rnode4_t *drp = VTOR4(dvp); 5226 nfs4_ga_res_t *garp = NULL; 5227 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5228 5229 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5230 ASSERT(nm != NULL); 5231 ASSERT(nm[0] != '\0'); 5232 ASSERT(dvp->v_type == VDIR); 5233 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5234 ASSERT(*vpp != NULL); 5235 5236 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5237 isdotdot = 1; 5238 args.ctag = TAG_LOOKUP_VPARENT; 5239 } else { 5240 /* 5241 * If dvp were a stub, it should have triggered and caused 5242 * a mount for us to get this far. 5243 */ 5244 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5245 5246 isdotdot = 0; 5247 args.ctag = TAG_LOOKUP_VALID; 5248 } 5249 5250 mi = VTOMI4(dvp); 5251 recov_state.rs_flags = 0; 5252 recov_state.rs_num_retry_despite_err = 0; 5253 5254 nvp = NULL; 5255 5256 /* Save the original mount point security information */ 5257 (void) save_mnt_secinfo(mi->mi_curr_serv); 5258 5259 recov_retry: 5260 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5261 &recov_state, NULL); 5262 if (e.error) { 5263 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5264 VN_RELE(*vpp); 5265 *vpp = NULL; 5266 return (e.error); 5267 } 5268 5269 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5270 5271 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5272 args.array_len = 7; 5273 args.array = argop; 5274 5275 /* 0. putfh file */ 5276 argop[0].argop = OP_CPUTFH; 5277 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5278 5279 /* 1. nverify the change info */ 5280 argop[1].argop = OP_NVERIFY; 5281 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5282 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5283 ver_fattr->attrlist4 = (char *)&dchange; 5284 ptr = (int32_t *)&dchange; 5285 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5286 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5287 5288 /* 2. getattr directory */ 5289 argop[2].argop = OP_GETATTR; 5290 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5291 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5292 5293 /* 3. access directory */ 5294 argop[3].argop = OP_ACCESS; 5295 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5296 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5297 5298 /* 4. lookup name */ 5299 if (isdotdot) { 5300 argop[4].argop = OP_LOOKUPP; 5301 } else { 5302 argop[4].argop = OP_CLOOKUP; 5303 argop[4].nfs_argop4_u.opclookup.cname = nm; 5304 } 5305 5306 /* 5. resulting file handle */ 5307 argop[5].argop = OP_GETFH; 5308 5309 /* 6. resulting file attributes */ 5310 argop[6].argop = OP_GETATTR; 5311 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5312 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5313 5314 doqueue = 1; 5315 t = gethrtime(); 5316 5317 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5318 5319 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5320 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5321 if (e.error != 0 && *vpp != NULL) 5322 VN_RELE(*vpp); 5323 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5324 &recov_state, FALSE); 5325 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5326 kmem_free(argop, argoplist_size); 5327 return (e.error); 5328 } 5329 5330 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5331 /* 5332 * For WRONGSEC of a non-dotdot case, send secinfo directly 5333 * from this thread, do not go thru the recovery thread since 5334 * we need the nm information. 5335 * 5336 * Not doing dotdot case because there is no specification 5337 * for (PUTFH, SECINFO "..") yet. 5338 */ 5339 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5340 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5341 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5342 &recov_state, FALSE); 5343 else 5344 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5345 &recov_state, TRUE); 5346 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5347 kmem_free(argop, argoplist_size); 5348 if (!e.error) 5349 goto recov_retry; 5350 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5351 VN_RELE(*vpp); 5352 *vpp = NULL; 5353 return (e.error); 5354 } 5355 5356 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5357 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5358 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5359 &recov_state, TRUE); 5360 5361 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5362 kmem_free(argop, argoplist_size); 5363 goto recov_retry; 5364 } 5365 } 5366 5367 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5368 5369 if (e.error || res.array_len == 0) { 5370 /* 5371 * If e.error isn't set, then reply has no ops (or we couldn't 5372 * be here). The only legal way to reply without an op array 5373 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5374 * be in the reply for all other status values. 5375 * 5376 * For valid replies without an ops array, return ENOTSUP 5377 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5378 * return EIO -- don't trust status. 5379 */ 5380 if (e.error == 0) 5381 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5382 ENOTSUP : EIO; 5383 VN_RELE(*vpp); 5384 *vpp = NULL; 5385 kmem_free(argop, argoplist_size); 5386 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5387 return (e.error); 5388 } 5389 5390 if (res.status != NFS4ERR_SAME) { 5391 e.error = geterrno4(res.status); 5392 5393 /* 5394 * The NVERIFY "failed" so the directory has changed 5395 * First make sure PUTFH succeeded and NVERIFY "failed" 5396 * cleanly. 5397 */ 5398 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5399 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5400 nfs4_purge_stale_fh(e.error, dvp, cr); 5401 VN_RELE(*vpp); 5402 *vpp = NULL; 5403 goto exit; 5404 } 5405 5406 /* 5407 * We know the NVERIFY "failed" so we must: 5408 * purge the caches (access and indirectly dnlc if needed) 5409 */ 5410 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5411 5412 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5413 nfs4_purge_stale_fh(e.error, dvp, cr); 5414 VN_RELE(*vpp); 5415 *vpp = NULL; 5416 goto exit; 5417 } 5418 5419 /* 5420 * Install new cached attributes for the directory 5421 */ 5422 nfs4_attr_cache(dvp, 5423 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5424 t, cr, FALSE, NULL); 5425 5426 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5427 nfs4_purge_stale_fh(e.error, dvp, cr); 5428 VN_RELE(*vpp); 5429 *vpp = NULL; 5430 e.error = geterrno4(res.status); 5431 goto exit; 5432 } 5433 5434 /* 5435 * Now we know the directory is valid, 5436 * cache new directory access 5437 */ 5438 nfs4_access_cache(drp, 5439 args.array[3].nfs_argop4_u.opaccess.access, 5440 res.array[3].nfs_resop4_u.opaccess.access, cr); 5441 5442 /* 5443 * recheck VEXEC access 5444 */ 5445 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5446 if (cacc != NFS4_ACCESS_ALLOWED) { 5447 /* 5448 * Directory permissions might have been revoked 5449 */ 5450 if (cacc == NFS4_ACCESS_DENIED) { 5451 e.error = EACCES; 5452 VN_RELE(*vpp); 5453 *vpp = NULL; 5454 goto exit; 5455 } 5456 5457 /* 5458 * Somehow we must not have asked for enough 5459 * so try a singleton ACCESS, should never happen. 5460 */ 5461 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5462 if (e.error) { 5463 VN_RELE(*vpp); 5464 *vpp = NULL; 5465 goto exit; 5466 } 5467 } 5468 5469 e.error = geterrno4(res.status); 5470 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5471 /* 5472 * The lookup failed, probably no entry 5473 */ 5474 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5475 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5476 } else { 5477 /* 5478 * Might be some other error, so remove 5479 * the dnlc entry to make sure we start all 5480 * over again, next time. 5481 */ 5482 dnlc_remove(dvp, nm); 5483 } 5484 VN_RELE(*vpp); 5485 *vpp = NULL; 5486 goto exit; 5487 } 5488 5489 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5490 /* 5491 * The file exists but we can't get its fh for 5492 * some unknown reason. Remove it from the dnlc 5493 * and error out to be safe. 5494 */ 5495 dnlc_remove(dvp, nm); 5496 VN_RELE(*vpp); 5497 *vpp = NULL; 5498 goto exit; 5499 } 5500 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5501 if (fhp->nfs_fh4_len == 0) { 5502 /* 5503 * The file exists but a bogus fh 5504 * some unknown reason. Remove it from the dnlc 5505 * and error out to be safe. 5506 */ 5507 e.error = ENOENT; 5508 dnlc_remove(dvp, nm); 5509 VN_RELE(*vpp); 5510 *vpp = NULL; 5511 goto exit; 5512 } 5513 sfhp = sfh4_get(fhp, mi); 5514 5515 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5516 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5517 5518 /* 5519 * Make the new rnode 5520 */ 5521 if (isdotdot) { 5522 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5523 if (e.error) { 5524 sfh4_rele(&sfhp); 5525 VN_RELE(*vpp); 5526 *vpp = NULL; 5527 goto exit; 5528 } 5529 /* 5530 * XXX if nfs4_make_dotdot uses an existing rnode 5531 * XXX it doesn't update the attributes. 5532 * XXX for now just save them again to save an OTW 5533 */ 5534 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5535 } else { 5536 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5537 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 5538 /* 5539 * If v_type == VNON, then garp was NULL because 5540 * the last op in the compound failed and makenfs4node 5541 * could not find the vnode for sfhp. It created 5542 * a new vnode, so we have nothing to purge here. 5543 */ 5544 if (nvp->v_type == VNON) { 5545 vattr_t vattr; 5546 5547 vattr.va_mask = AT_TYPE; 5548 /* 5549 * N.B. We've already called nfs4_end_fop above. 5550 */ 5551 e.error = nfs4getattr(nvp, &vattr, cr); 5552 if (e.error) { 5553 sfh4_rele(&sfhp); 5554 VN_RELE(*vpp); 5555 *vpp = NULL; 5556 VN_RELE(nvp); 5557 goto exit; 5558 } 5559 nvp->v_type = vattr.va_type; 5560 } 5561 } 5562 sfh4_rele(&sfhp); 5563 5564 nrp = VTOR4(nvp); 5565 mutex_enter(&nrp->r_statev4_lock); 5566 if (!nrp->created_v4) { 5567 mutex_exit(&nrp->r_statev4_lock); 5568 dnlc_update(dvp, nm, nvp); 5569 } else 5570 mutex_exit(&nrp->r_statev4_lock); 5571 5572 VN_RELE(*vpp); 5573 *vpp = nvp; 5574 } else { 5575 hrtime_t now; 5576 hrtime_t delta = 0; 5577 5578 e.error = 0; 5579 5580 /* 5581 * Because the NVERIFY "succeeded" we know that the 5582 * directory attributes are still valid 5583 * so update r_time_attr_inval 5584 */ 5585 now = gethrtime(); 5586 mutex_enter(&drp->r_statelock); 5587 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5588 delta = now - drp->r_time_attr_saved; 5589 if (delta < mi->mi_acdirmin) 5590 delta = mi->mi_acdirmin; 5591 else if (delta > mi->mi_acdirmax) 5592 delta = mi->mi_acdirmax; 5593 } 5594 drp->r_time_attr_inval = now + delta; 5595 mutex_exit(&drp->r_statelock); 5596 dnlc_update(dvp, nm, *vpp); 5597 5598 /* 5599 * Even though we have a valid directory attr cache 5600 * and dnlc entry, we may not have access. 5601 * This should almost always hit the cache. 5602 */ 5603 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5604 if (e.error) { 5605 VN_RELE(*vpp); 5606 *vpp = NULL; 5607 } 5608 5609 if (*vpp == DNLC_NO_VNODE) { 5610 VN_RELE(*vpp); 5611 *vpp = NULL; 5612 e.error = ENOENT; 5613 } 5614 } 5615 5616 exit: 5617 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5618 kmem_free(argop, argoplist_size); 5619 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5620 return (e.error); 5621 } 5622 5623 /* 5624 * We need to go over the wire to lookup the name, but 5625 * while we are there verify the directory has not 5626 * changed but if it has, get new attributes and check access 5627 * 5628 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5629 * NVERIFY GETATTR ACCESS 5630 * 5631 * With the results: 5632 * if the NVERIFY failed we must purge the caches, add new attributes, 5633 * and cache new access. 5634 * set a new r_time_attr_inval 5635 * add name to dnlc, possibly negative 5636 * if LOOKUP succeeded 5637 * cache new attributes 5638 */ 5639 static int 5640 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5641 { 5642 COMPOUND4args_clnt args; 5643 COMPOUND4res_clnt res; 5644 fattr4 *ver_fattr; 5645 fattr4_change dchange; 5646 int32_t *ptr; 5647 nfs4_ga_res_t *garp = NULL; 5648 int argoplist_size = 9 * sizeof (nfs_argop4); 5649 nfs_argop4 *argop; 5650 int doqueue; 5651 mntinfo4_t *mi; 5652 nfs4_recov_state_t recov_state; 5653 hrtime_t t; 5654 int isdotdot; 5655 vnode_t *nvp; 5656 nfs_fh4 *fhp; 5657 nfs4_sharedfh_t *sfhp; 5658 nfs4_access_type_t cacc; 5659 rnode4_t *nrp; 5660 rnode4_t *drp = VTOR4(dvp); 5661 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5662 5663 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5664 ASSERT(nm != NULL); 5665 ASSERT(nm[0] != '\0'); 5666 ASSERT(dvp->v_type == VDIR); 5667 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5668 ASSERT(*vpp == NULL); 5669 5670 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5671 isdotdot = 1; 5672 args.ctag = TAG_LOOKUP_PARENT; 5673 } else { 5674 /* 5675 * If dvp were a stub, it should have triggered and caused 5676 * a mount for us to get this far. 5677 */ 5678 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5679 5680 isdotdot = 0; 5681 args.ctag = TAG_LOOKUP; 5682 } 5683 5684 mi = VTOMI4(dvp); 5685 recov_state.rs_flags = 0; 5686 recov_state.rs_num_retry_despite_err = 0; 5687 5688 nvp = NULL; 5689 5690 /* Save the original mount point security information */ 5691 (void) save_mnt_secinfo(mi->mi_curr_serv); 5692 5693 recov_retry: 5694 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5695 &recov_state, NULL); 5696 if (e.error) { 5697 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5698 return (e.error); 5699 } 5700 5701 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5702 5703 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5704 args.array_len = 9; 5705 args.array = argop; 5706 5707 /* 0. putfh file */ 5708 argop[0].argop = OP_CPUTFH; 5709 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5710 5711 /* 1. savefh for the nverify */ 5712 argop[1].argop = OP_SAVEFH; 5713 5714 /* 2. lookup name */ 5715 if (isdotdot) { 5716 argop[2].argop = OP_LOOKUPP; 5717 } else { 5718 argop[2].argop = OP_CLOOKUP; 5719 argop[2].nfs_argop4_u.opclookup.cname = nm; 5720 } 5721 5722 /* 3. resulting file handle */ 5723 argop[3].argop = OP_GETFH; 5724 5725 /* 4. resulting file attributes */ 5726 argop[4].argop = OP_GETATTR; 5727 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5728 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5729 5730 /* 5. restorefh back the directory for the nverify */ 5731 argop[5].argop = OP_RESTOREFH; 5732 5733 /* 6. nverify the change info */ 5734 argop[6].argop = OP_NVERIFY; 5735 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5736 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5737 ver_fattr->attrlist4 = (char *)&dchange; 5738 ptr = (int32_t *)&dchange; 5739 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5740 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5741 5742 /* 7. getattr directory */ 5743 argop[7].argop = OP_GETATTR; 5744 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5745 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5746 5747 /* 8. access directory */ 5748 argop[8].argop = OP_ACCESS; 5749 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5750 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5751 5752 doqueue = 1; 5753 t = gethrtime(); 5754 5755 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5756 5757 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5758 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5759 if (e.error != 0 && *vpp != NULL) 5760 VN_RELE(*vpp); 5761 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5762 &recov_state, FALSE); 5763 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5764 kmem_free(argop, argoplist_size); 5765 return (e.error); 5766 } 5767 5768 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5769 /* 5770 * For WRONGSEC of a non-dotdot case, send secinfo directly 5771 * from this thread, do not go thru the recovery thread since 5772 * we need the nm information. 5773 * 5774 * Not doing dotdot case because there is no specification 5775 * for (PUTFH, SECINFO "..") yet. 5776 */ 5777 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5778 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5779 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5780 &recov_state, FALSE); 5781 else 5782 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5783 &recov_state, TRUE); 5784 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5785 kmem_free(argop, argoplist_size); 5786 if (!e.error) 5787 goto recov_retry; 5788 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5789 return (e.error); 5790 } 5791 5792 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5793 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5794 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5795 &recov_state, TRUE); 5796 5797 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5798 kmem_free(argop, argoplist_size); 5799 goto recov_retry; 5800 } 5801 } 5802 5803 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5804 5805 if (e.error || res.array_len == 0) { 5806 /* 5807 * If e.error isn't set, then reply has no ops (or we couldn't 5808 * be here). The only legal way to reply without an op array 5809 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5810 * be in the reply for all other status values. 5811 * 5812 * For valid replies without an ops array, return ENOTSUP 5813 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5814 * return EIO -- don't trust status. 5815 */ 5816 if (e.error == 0) 5817 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5818 ENOTSUP : EIO; 5819 5820 kmem_free(argop, argoplist_size); 5821 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5822 return (e.error); 5823 } 5824 5825 e.error = geterrno4(res.status); 5826 5827 /* 5828 * The PUTFH and SAVEFH may have failed. 5829 */ 5830 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5831 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5832 nfs4_purge_stale_fh(e.error, dvp, cr); 5833 goto exit; 5834 } 5835 5836 /* 5837 * Check if the file exists, if it does delay entering 5838 * into the dnlc until after we update the directory 5839 * attributes so we don't cause it to get purged immediately. 5840 */ 5841 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5842 /* 5843 * The lookup failed, probably no entry 5844 */ 5845 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5846 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5847 goto exit; 5848 } 5849 5850 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5851 /* 5852 * The file exists but we can't get its fh for 5853 * some unknown reason. Error out to be safe. 5854 */ 5855 goto exit; 5856 } 5857 5858 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5859 if (fhp->nfs_fh4_len == 0) { 5860 /* 5861 * The file exists but a bogus fh 5862 * some unknown reason. Error out to be safe. 5863 */ 5864 e.error = EIO; 5865 goto exit; 5866 } 5867 sfhp = sfh4_get(fhp, mi); 5868 5869 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5870 sfh4_rele(&sfhp); 5871 goto exit; 5872 } 5873 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5874 5875 /* 5876 * The RESTOREFH may have failed 5877 */ 5878 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5879 sfh4_rele(&sfhp); 5880 e.error = EIO; 5881 goto exit; 5882 } 5883 5884 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5885 /* 5886 * First make sure the NVERIFY failed as we expected, 5887 * if it didn't then be conservative and error out 5888 * as we can't trust the directory. 5889 */ 5890 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5891 sfh4_rele(&sfhp); 5892 e.error = EIO; 5893 goto exit; 5894 } 5895 5896 /* 5897 * We know the NVERIFY "failed" so the directory has changed, 5898 * so we must: 5899 * purge the caches (access and indirectly dnlc if needed) 5900 */ 5901 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5902 5903 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5904 sfh4_rele(&sfhp); 5905 goto exit; 5906 } 5907 nfs4_attr_cache(dvp, 5908 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5909 t, cr, FALSE, NULL); 5910 5911 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5912 nfs4_purge_stale_fh(e.error, dvp, cr); 5913 sfh4_rele(&sfhp); 5914 e.error = geterrno4(res.status); 5915 goto exit; 5916 } 5917 5918 /* 5919 * Now we know the directory is valid, 5920 * cache new directory access 5921 */ 5922 nfs4_access_cache(drp, 5923 args.array[8].nfs_argop4_u.opaccess.access, 5924 res.array[8].nfs_resop4_u.opaccess.access, cr); 5925 5926 /* 5927 * recheck VEXEC access 5928 */ 5929 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5930 if (cacc != NFS4_ACCESS_ALLOWED) { 5931 /* 5932 * Directory permissions might have been revoked 5933 */ 5934 if (cacc == NFS4_ACCESS_DENIED) { 5935 sfh4_rele(&sfhp); 5936 e.error = EACCES; 5937 goto exit; 5938 } 5939 5940 /* 5941 * Somehow we must not have asked for enough 5942 * so try a singleton ACCESS should never happen 5943 */ 5944 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5945 if (e.error) { 5946 sfh4_rele(&sfhp); 5947 goto exit; 5948 } 5949 } 5950 5951 e.error = geterrno4(res.status); 5952 } else { 5953 hrtime_t now; 5954 hrtime_t delta = 0; 5955 5956 e.error = 0; 5957 5958 /* 5959 * Because the NVERIFY "succeeded" we know that the 5960 * directory attributes are still valid 5961 * so update r_time_attr_inval 5962 */ 5963 now = gethrtime(); 5964 mutex_enter(&drp->r_statelock); 5965 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5966 delta = now - drp->r_time_attr_saved; 5967 if (delta < mi->mi_acdirmin) 5968 delta = mi->mi_acdirmin; 5969 else if (delta > mi->mi_acdirmax) 5970 delta = mi->mi_acdirmax; 5971 } 5972 drp->r_time_attr_inval = now + delta; 5973 mutex_exit(&drp->r_statelock); 5974 5975 /* 5976 * Even though we have a valid directory attr cache, 5977 * we may not have access. 5978 * This should almost always hit the cache. 5979 */ 5980 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5981 if (e.error) { 5982 sfh4_rele(&sfhp); 5983 goto exit; 5984 } 5985 } 5986 5987 /* 5988 * Now we have successfully completed the lookup, if the 5989 * directory has changed we now have the valid attributes. 5990 * We also know we have directory access. 5991 * Create the new rnode and insert it in the dnlc. 5992 */ 5993 if (isdotdot) { 5994 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5995 if (e.error) { 5996 sfh4_rele(&sfhp); 5997 goto exit; 5998 } 5999 /* 6000 * XXX if nfs4_make_dotdot uses an existing rnode 6001 * XXX it doesn't update the attributes. 6002 * XXX for now just save them again to save an OTW 6003 */ 6004 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 6005 } else { 6006 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 6007 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 6008 } 6009 sfh4_rele(&sfhp); 6010 6011 nrp = VTOR4(nvp); 6012 mutex_enter(&nrp->r_statev4_lock); 6013 if (!nrp->created_v4) { 6014 mutex_exit(&nrp->r_statev4_lock); 6015 dnlc_update(dvp, nm, nvp); 6016 } else 6017 mutex_exit(&nrp->r_statev4_lock); 6018 6019 *vpp = nvp; 6020 6021 exit: 6022 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6023 kmem_free(argop, argoplist_size); 6024 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 6025 return (e.error); 6026 } 6027 6028 #ifdef DEBUG 6029 void 6030 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 6031 { 6032 uint_t i, len; 6033 zoneid_t zoneid = getzoneid(); 6034 char *s; 6035 6036 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 6037 for (i = 0; i < argcnt; i++) { 6038 nfs_argop4 *op = &argbase[i]; 6039 switch (op->argop) { 6040 case OP_CPUTFH: 6041 case OP_PUTFH: 6042 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 6043 break; 6044 case OP_PUTROOTFH: 6045 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 6046 break; 6047 case OP_CLOOKUP: 6048 s = op->nfs_argop4_u.opclookup.cname; 6049 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6050 break; 6051 case OP_LOOKUP: 6052 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 6053 &len, NULL); 6054 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6055 kmem_free(s, len); 6056 break; 6057 case OP_LOOKUPP: 6058 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 6059 break; 6060 case OP_GETFH: 6061 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 6062 break; 6063 case OP_GETATTR: 6064 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 6065 break; 6066 case OP_OPENATTR: 6067 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 6068 break; 6069 default: 6070 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 6071 op->argop); 6072 break; 6073 } 6074 } 6075 } 6076 #endif 6077 6078 /* 6079 * nfs4lookup_setup - constructs a multi-lookup compound request. 6080 * 6081 * Given the path "nm1/nm2/.../nmn", the following compound requests 6082 * may be created: 6083 * 6084 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 6085 * is faster, for now. 6086 * 6087 * l4_getattrs indicates the type of compound requested. 6088 * 6089 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 6090 * 6091 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 6092 * 6093 * total number of ops is n + 1. 6094 * 6095 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 6096 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 6097 * before the last component, and only get attributes 6098 * for the last component. Note that the second-to-last 6099 * pathname component is XATTR_RPATH, which does NOT go 6100 * over-the-wire as a lookup. 6101 * 6102 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 6103 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 6104 * 6105 * and total number of ops is n + 5. 6106 * 6107 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 6108 * attribute directory: create lookups plus an OPENATTR 6109 * replacing the last lookup. Note that the last pathname 6110 * component is XATTR_RPATH, which does NOT go over-the-wire 6111 * as a lookup. 6112 * 6113 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 6114 * Openattr; Getfh; Getattr } 6115 * 6116 * and total number of ops is n + 5. 6117 * 6118 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6119 * nodes too. 6120 * 6121 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6122 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6123 * 6124 * and total number of ops is 3*n + 1. 6125 * 6126 * All cases: returns the index in the arg array of the final LOOKUP op, or 6127 * -1 if no LOOKUPs were used. 6128 */ 6129 int 6130 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6131 { 6132 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6133 nfs_argop4 *argbase, *argop; 6134 int arglen, argcnt; 6135 int n = 1; /* number of components */ 6136 int nga = 1; /* number of Getattr's in request */ 6137 char c = '\0', *s, *p; 6138 int lookup_idx = -1; 6139 int argoplist_size; 6140 6141 /* set lookuparg response result to 0 */ 6142 lookupargp->resp->status = NFS4_OK; 6143 6144 /* skip leading "/" or "." e.g. ".//./" if there is */ 6145 for (; ; nm++) { 6146 if (*nm != '/' && *nm != '.') 6147 break; 6148 6149 /* ".." is counted as 1 component */ 6150 if (*nm == '.' && *(nm + 1) != '/') 6151 break; 6152 } 6153 6154 /* 6155 * Find n = number of components - nm must be null terminated 6156 * Skip "." components. 6157 */ 6158 if (*nm != '\0') 6159 for (n = 1, s = nm; *s != '\0'; s++) { 6160 if ((*s == '/') && (*(s + 1) != '/') && 6161 (*(s + 1) != '\0') && 6162 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6163 *(s + 2) == '\0'))) 6164 n++; 6165 } 6166 else 6167 n = 0; 6168 6169 /* 6170 * nga is number of components that need Getfh+Getattr 6171 */ 6172 switch (l4_getattrs) { 6173 case LKP4_NO_ATTRIBUTES: 6174 nga = 0; 6175 break; 6176 case LKP4_ALL_ATTRIBUTES: 6177 nga = n; 6178 /* 6179 * Always have at least 1 getfh, getattr pair 6180 */ 6181 if (nga == 0) 6182 nga++; 6183 break; 6184 case LKP4_LAST_ATTRDIR: 6185 case LKP4_LAST_NAMED_ATTR: 6186 nga = n+1; 6187 break; 6188 } 6189 6190 /* 6191 * If change to use the filehandle attr instead of getfh 6192 * the following line can be deleted. 6193 */ 6194 nga *= 2; 6195 6196 /* 6197 * calculate number of ops in request as 6198 * header + trailer + lookups + getattrs 6199 */ 6200 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6201 6202 argoplist_size = arglen * sizeof (nfs_argop4); 6203 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6204 lookupargp->argsp->array = argop; 6205 6206 argcnt = lookupargp->header_len; 6207 argop += argcnt; 6208 6209 /* 6210 * loop and create a lookup op and possibly getattr/getfh for 6211 * each component. Skip "." components. 6212 */ 6213 for (s = nm; *s != '\0'; s = p) { 6214 /* 6215 * Set up a pathname struct for each component if needed 6216 */ 6217 while (*s == '/') 6218 s++; 6219 if (*s == '\0') 6220 break; 6221 6222 for (p = s; (*p != '/') && (*p != '\0'); p++) 6223 ; 6224 c = *p; 6225 *p = '\0'; 6226 6227 if (s[0] == '.' && s[1] == '\0') { 6228 *p = c; 6229 continue; 6230 } 6231 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6232 strcmp(s, XATTR_RPATH) == 0) { 6233 /* getfh XXX may not be needed in future */ 6234 argop->argop = OP_GETFH; 6235 argop++; 6236 argcnt++; 6237 6238 /* getattr */ 6239 argop->argop = OP_GETATTR; 6240 argop->nfs_argop4_u.opgetattr.attr_request = 6241 lookupargp->ga_bits; 6242 argop->nfs_argop4_u.opgetattr.mi = 6243 lookupargp->mi; 6244 argop++; 6245 argcnt++; 6246 6247 /* openattr */ 6248 argop->argop = OP_OPENATTR; 6249 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6250 strcmp(s, XATTR_RPATH) == 0) { 6251 /* openattr */ 6252 argop->argop = OP_OPENATTR; 6253 argop++; 6254 argcnt++; 6255 6256 /* getfh XXX may not be needed in future */ 6257 argop->argop = OP_GETFH; 6258 argop++; 6259 argcnt++; 6260 6261 /* getattr */ 6262 argop->argop = OP_GETATTR; 6263 argop->nfs_argop4_u.opgetattr.attr_request = 6264 lookupargp->ga_bits; 6265 argop->nfs_argop4_u.opgetattr.mi = 6266 lookupargp->mi; 6267 argop++; 6268 argcnt++; 6269 *p = c; 6270 continue; 6271 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6272 /* lookupp */ 6273 argop->argop = OP_LOOKUPP; 6274 } else { 6275 /* lookup */ 6276 argop->argop = OP_LOOKUP; 6277 (void) str_to_utf8(s, 6278 &argop->nfs_argop4_u.oplookup.objname); 6279 } 6280 lookup_idx = argcnt; 6281 argop++; 6282 argcnt++; 6283 6284 *p = c; 6285 6286 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6287 /* getfh XXX may not be needed in future */ 6288 argop->argop = OP_GETFH; 6289 argop++; 6290 argcnt++; 6291 6292 /* getattr */ 6293 argop->argop = OP_GETATTR; 6294 argop->nfs_argop4_u.opgetattr.attr_request = 6295 lookupargp->ga_bits; 6296 argop->nfs_argop4_u.opgetattr.mi = 6297 lookupargp->mi; 6298 argop++; 6299 argcnt++; 6300 } 6301 } 6302 6303 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6304 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6305 if (needgetfh) { 6306 /* stick in a post-lookup getfh */ 6307 argop->argop = OP_GETFH; 6308 argcnt++; 6309 argop++; 6310 } 6311 /* post-lookup getattr */ 6312 argop->argop = OP_GETATTR; 6313 argop->nfs_argop4_u.opgetattr.attr_request = 6314 lookupargp->ga_bits; 6315 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6316 argcnt++; 6317 } 6318 argcnt += lookupargp->trailer_len; /* actual op count */ 6319 lookupargp->argsp->array_len = argcnt; 6320 lookupargp->arglen = arglen; 6321 6322 #ifdef DEBUG 6323 if (nfs4_client_lookup_debug) 6324 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6325 #endif 6326 6327 return (lookup_idx); 6328 } 6329 6330 static int 6331 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6332 { 6333 COMPOUND4args_clnt args; 6334 COMPOUND4res_clnt res; 6335 GETFH4res *gf_res = NULL; 6336 nfs_argop4 argop[4]; 6337 nfs_resop4 *resop = NULL; 6338 nfs4_sharedfh_t *sfhp; 6339 hrtime_t t; 6340 nfs4_error_t e; 6341 6342 rnode4_t *drp; 6343 int doqueue = 1; 6344 vnode_t *vp; 6345 int needrecov = 0; 6346 nfs4_recov_state_t recov_state; 6347 6348 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6349 6350 *avp = NULL; 6351 recov_state.rs_flags = 0; 6352 recov_state.rs_num_retry_despite_err = 0; 6353 6354 recov_retry: 6355 /* COMPOUND: putfh, openattr, getfh, getattr */ 6356 args.array_len = 4; 6357 args.array = argop; 6358 args.ctag = TAG_OPENATTR; 6359 6360 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6361 if (e.error) 6362 return (e.error); 6363 6364 drp = VTOR4(dvp); 6365 6366 /* putfh */ 6367 argop[0].argop = OP_CPUTFH; 6368 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6369 6370 /* openattr */ 6371 argop[1].argop = OP_OPENATTR; 6372 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6373 6374 /* getfh */ 6375 argop[2].argop = OP_GETFH; 6376 6377 /* getattr */ 6378 argop[3].argop = OP_GETATTR; 6379 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6380 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6381 6382 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6383 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6384 rnode4info(drp))); 6385 6386 t = gethrtime(); 6387 6388 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6389 6390 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6391 if (needrecov) { 6392 bool_t abort; 6393 6394 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6395 "nfs4openattr: initiating recovery\n")); 6396 6397 abort = nfs4_start_recovery(&e, 6398 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6399 OP_OPENATTR, NULL, NULL, NULL); 6400 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6401 if (!e.error) { 6402 e.error = geterrno4(res.status); 6403 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6404 } 6405 if (abort == FALSE) 6406 goto recov_retry; 6407 return (e.error); 6408 } 6409 6410 if (e.error) { 6411 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6412 return (e.error); 6413 } 6414 6415 if (res.status) { 6416 /* 6417 * If OTW errro is NOTSUPP, then it should be 6418 * translated to EINVAL. All Solaris file system 6419 * implementations return EINVAL to the syscall layer 6420 * when the attrdir cannot be created due to an 6421 * implementation restriction or noxattr mount option. 6422 */ 6423 if (res.status == NFS4ERR_NOTSUPP) { 6424 mutex_enter(&drp->r_statelock); 6425 if (drp->r_xattr_dir) 6426 VN_RELE(drp->r_xattr_dir); 6427 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6428 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6429 mutex_exit(&drp->r_statelock); 6430 6431 e.error = EINVAL; 6432 } else { 6433 e.error = geterrno4(res.status); 6434 } 6435 6436 if (e.error) { 6437 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6438 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6439 needrecov); 6440 return (e.error); 6441 } 6442 } 6443 6444 resop = &res.array[0]; /* putfh res */ 6445 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6446 6447 resop = &res.array[1]; /* openattr res */ 6448 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6449 6450 resop = &res.array[2]; /* getfh res */ 6451 gf_res = &resop->nfs_resop4_u.opgetfh; 6452 if (gf_res->object.nfs_fh4_len == 0) { 6453 *avp = NULL; 6454 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6455 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6456 return (ENOENT); 6457 } 6458 6459 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6460 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6461 dvp->v_vfsp, t, cr, dvp, 6462 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp)); 6463 sfh4_rele(&sfhp); 6464 6465 if (e.error) 6466 PURGE_ATTRCACHE4(vp); 6467 6468 mutex_enter(&vp->v_lock); 6469 vp->v_flag |= V_XATTRDIR; 6470 mutex_exit(&vp->v_lock); 6471 6472 *avp = vp; 6473 6474 mutex_enter(&drp->r_statelock); 6475 if (drp->r_xattr_dir) 6476 VN_RELE(drp->r_xattr_dir); 6477 VN_HOLD(vp); 6478 drp->r_xattr_dir = vp; 6479 6480 /* 6481 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6482 * NULL. xattrs could be created at any time, and we have no 6483 * way to update pc4_xattr_exists in the base object if/when 6484 * it happens. 6485 */ 6486 drp->r_pathconf.pc4_xattr_valid = 0; 6487 6488 mutex_exit(&drp->r_statelock); 6489 6490 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6491 6492 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6493 6494 return (0); 6495 } 6496 6497 /* ARGSUSED */ 6498 static int 6499 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6500 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct, 6501 vsecattr_t *vsecp) 6502 { 6503 int error; 6504 vnode_t *vp = NULL; 6505 rnode4_t *rp; 6506 struct vattr vattr; 6507 rnode4_t *drp; 6508 vnode_t *tempvp; 6509 enum createmode4 createmode; 6510 bool_t must_trunc = FALSE; 6511 int truncating = 0; 6512 6513 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6514 return (EPERM); 6515 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6516 return (EINVAL); 6517 } 6518 6519 /* . and .. have special meaning in the protocol, reject them. */ 6520 6521 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6522 return (EISDIR); 6523 6524 drp = VTOR4(dvp); 6525 6526 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6527 return (EINTR); 6528 6529 top: 6530 /* 6531 * We make a copy of the attributes because the caller does not 6532 * expect us to change what va points to. 6533 */ 6534 vattr = *va; 6535 6536 /* 6537 * If the pathname is "", then dvp is the root vnode of 6538 * a remote file mounted over a local directory. 6539 * All that needs to be done is access 6540 * checking and truncation. Note that we avoid doing 6541 * open w/ create because the parent directory might 6542 * be in pseudo-fs and the open would fail. 6543 */ 6544 if (*nm == '\0') { 6545 error = 0; 6546 VN_HOLD(dvp); 6547 vp = dvp; 6548 must_trunc = TRUE; 6549 } else { 6550 /* 6551 * We need to go over the wire, just to be sure whether the 6552 * file exists or not. Using the DNLC can be dangerous in 6553 * this case when making a decision regarding existence. 6554 */ 6555 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6556 } 6557 6558 if (exclusive) 6559 createmode = EXCLUSIVE4; 6560 else 6561 createmode = GUARDED4; 6562 6563 /* 6564 * error would be set if the file does not exist on the 6565 * server, so lets go create it. 6566 */ 6567 if (error) { 6568 goto create_otw; 6569 } 6570 6571 /* 6572 * File does exist on the server 6573 */ 6574 if (exclusive == EXCL) 6575 error = EEXIST; 6576 else if (vp->v_type == VDIR && (mode & VWRITE)) 6577 error = EISDIR; 6578 else { 6579 /* 6580 * If vnode is a device, create special vnode. 6581 */ 6582 if (ISVDEV(vp->v_type)) { 6583 tempvp = vp; 6584 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6585 VN_RELE(tempvp); 6586 } 6587 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) { 6588 if ((vattr.va_mask & AT_SIZE) && 6589 vp->v_type == VREG) { 6590 rp = VTOR4(vp); 6591 /* 6592 * Check here for large file handled 6593 * by LF-unaware process (as 6594 * ufs_create() does) 6595 */ 6596 if (!(flags & FOFFMAX)) { 6597 mutex_enter(&rp->r_statelock); 6598 if (rp->r_size > MAXOFF32_T) 6599 error = EOVERFLOW; 6600 mutex_exit(&rp->r_statelock); 6601 } 6602 6603 /* if error is set then we need to return */ 6604 if (error) { 6605 nfs_rw_exit(&drp->r_rwlock); 6606 VN_RELE(vp); 6607 return (error); 6608 } 6609 6610 if (must_trunc) { 6611 vattr.va_mask = AT_SIZE; 6612 error = nfs4setattr(vp, &vattr, 0, cr, 6613 NULL); 6614 } else { 6615 /* 6616 * we know we have a regular file that already 6617 * exists and we may end up truncating the file 6618 * as a result of the open_otw, so flush out 6619 * any dirty pages for this file first. 6620 */ 6621 if (nfs4_has_pages(vp) && 6622 ((rp->r_flags & R4DIRTY) || 6623 rp->r_count > 0 || 6624 rp->r_mapcnt > 0)) { 6625 error = nfs4_putpage(vp, 6626 (offset_t)0, 0, 0, cr, ct); 6627 if (error && (error == ENOSPC || 6628 error == EDQUOT)) { 6629 mutex_enter( 6630 &rp->r_statelock); 6631 if (!rp->r_error) 6632 rp->r_error = 6633 error; 6634 mutex_exit( 6635 &rp->r_statelock); 6636 } 6637 } 6638 vattr.va_mask = (AT_SIZE | 6639 AT_TYPE | AT_MODE); 6640 vattr.va_type = VREG; 6641 createmode = UNCHECKED4; 6642 truncating = 1; 6643 goto create_otw; 6644 } 6645 } 6646 } 6647 } 6648 nfs_rw_exit(&drp->r_rwlock); 6649 if (error) { 6650 VN_RELE(vp); 6651 } else { 6652 vnode_t *tvp; 6653 rnode4_t *trp; 6654 /* 6655 * existing file got truncated, notify. 6656 */ 6657 tvp = vp; 6658 if (vp->v_type == VREG) { 6659 trp = VTOR4(vp); 6660 if (IS_SHADOW(vp, trp)) 6661 tvp = RTOV4(trp); 6662 } 6663 vnevent_create(tvp, ct); 6664 *vpp = vp; 6665 } 6666 return (error); 6667 6668 create_otw: 6669 dnlc_remove(dvp, nm); 6670 6671 ASSERT(vattr.va_mask & AT_TYPE); 6672 6673 /* 6674 * If not a regular file let nfs4mknod() handle it. 6675 */ 6676 if (vattr.va_type != VREG) { 6677 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6678 nfs_rw_exit(&drp->r_rwlock); 6679 return (error); 6680 } 6681 6682 /* 6683 * It _is_ a regular file. 6684 */ 6685 ASSERT(vattr.va_mask & AT_MODE); 6686 if (MANDMODE(vattr.va_mode)) { 6687 nfs_rw_exit(&drp->r_rwlock); 6688 return (EACCES); 6689 } 6690 6691 /* 6692 * If this happens to be a mknod of a regular file, then flags will 6693 * have neither FREAD or FWRITE. However, we must set at least one 6694 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6695 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6696 * set (based on openmode specified by app). 6697 */ 6698 if ((flags & (FREAD|FWRITE)) == 0) 6699 flags |= (FREAD|FWRITE); 6700 6701 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6702 6703 if (vp != NULL) { 6704 /* if create was successful, throw away the file's pages */ 6705 if (!error && (vattr.va_mask & AT_SIZE)) 6706 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6707 cr); 6708 /* release the lookup hold */ 6709 VN_RELE(vp); 6710 vp = NULL; 6711 } 6712 6713 /* 6714 * validate that we opened a regular file. This handles a misbehaving 6715 * server that returns an incorrect FH. 6716 */ 6717 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6718 error = EISDIR; 6719 VN_RELE(*vpp); 6720 } 6721 6722 /* 6723 * If this is not an exclusive create, then the CREATE 6724 * request will be made with the GUARDED mode set. This 6725 * means that the server will return EEXIST if the file 6726 * exists. The file could exist because of a retransmitted 6727 * request. In this case, we recover by starting over and 6728 * checking to see whether the file exists. This second 6729 * time through it should and a CREATE request will not be 6730 * sent. 6731 * 6732 * This handles the problem of a dangling CREATE request 6733 * which contains attributes which indicate that the file 6734 * should be truncated. This retransmitted request could 6735 * possibly truncate valid data in the file if not caught 6736 * by the duplicate request mechanism on the server or if 6737 * not caught by other means. The scenario is: 6738 * 6739 * Client transmits CREATE request with size = 0 6740 * Client times out, retransmits request. 6741 * Response to the first request arrives from the server 6742 * and the client proceeds on. 6743 * Client writes data to the file. 6744 * The server now processes retransmitted CREATE request 6745 * and truncates file. 6746 * 6747 * The use of the GUARDED CREATE request prevents this from 6748 * happening because the retransmitted CREATE would fail 6749 * with EEXIST and would not truncate the file. 6750 */ 6751 if (error == EEXIST && exclusive == NONEXCL) { 6752 #ifdef DEBUG 6753 nfs4_create_misses++; 6754 #endif 6755 goto top; 6756 } 6757 nfs_rw_exit(&drp->r_rwlock); 6758 if (truncating && !error && *vpp) { 6759 vnode_t *tvp; 6760 rnode4_t *trp; 6761 /* 6762 * existing file got truncated, notify. 6763 */ 6764 tvp = *vpp; 6765 trp = VTOR4(tvp); 6766 if (IS_SHADOW(tvp, trp)) 6767 tvp = RTOV4(trp); 6768 vnevent_create(tvp, ct); 6769 } 6770 return (error); 6771 } 6772 6773 /* 6774 * Create compound (for mkdir, mknod, symlink): 6775 * { Putfh <dfh>; Create; Getfh; Getattr } 6776 * It's okay if setattr failed to set gid - this is not considered 6777 * an error, but purge attrs in that case. 6778 */ 6779 static int 6780 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6781 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6782 { 6783 int need_end_op = FALSE; 6784 COMPOUND4args_clnt args; 6785 COMPOUND4res_clnt res, *resp = NULL; 6786 nfs_argop4 *argop; 6787 nfs_resop4 *resop; 6788 int doqueue; 6789 mntinfo4_t *mi; 6790 rnode4_t *drp = VTOR4(dvp); 6791 change_info4 *cinfo; 6792 GETFH4res *gf_res; 6793 struct vattr vattr; 6794 vnode_t *vp; 6795 fattr4 *crattr; 6796 bool_t needrecov = FALSE; 6797 nfs4_recov_state_t recov_state; 6798 nfs4_sharedfh_t *sfhp = NULL; 6799 hrtime_t t; 6800 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6801 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6802 dirattr_info_t dinfo, *dinfop; 6803 servinfo4_t *svp; 6804 bitmap4 supp_attrs; 6805 6806 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6807 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6808 6809 mi = VTOMI4(dvp); 6810 6811 /* 6812 * Make sure we properly deal with setting the right gid 6813 * on a new directory to reflect the parent's setgid bit 6814 */ 6815 setgid_flag = 0; 6816 if (type == NF4DIR) { 6817 struct vattr dva; 6818 6819 va->va_mode &= ~VSGID; 6820 dva.va_mask = AT_MODE | AT_GID; 6821 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) { 6822 6823 /* 6824 * If the parent's directory has the setgid bit set 6825 * _and_ the client was able to get a valid mapping 6826 * for the parent dir's owner_group, we want to 6827 * append NVERIFY(owner_group == dva.va_gid) and 6828 * SETTATTR to the CREATE compound. 6829 */ 6830 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6831 setgid_flag = 1; 6832 va->va_mode |= VSGID; 6833 if (dva.va_gid != GID_NOBODY) { 6834 va->va_mask |= AT_GID; 6835 va->va_gid = dva.va_gid; 6836 } 6837 } 6838 } 6839 } 6840 6841 /* 6842 * Create ops: 6843 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6844 * 5:restorefh(dir) 6:getattr(dir) 6845 * 6846 * if (setgid) 6847 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6848 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6849 * 8:nverify 9:setattr 6850 */ 6851 if (setgid_flag) { 6852 numops = 10; 6853 idx_create = 1; 6854 idx_fattr = 3; 6855 } else { 6856 numops = 7; 6857 idx_create = 2; 6858 idx_fattr = 4; 6859 } 6860 6861 ASSERT(nfs_zone() == mi->mi_zone); 6862 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6863 return (EINTR); 6864 } 6865 recov_state.rs_flags = 0; 6866 recov_state.rs_num_retry_despite_err = 0; 6867 6868 argoplist_size = numops * sizeof (nfs_argop4); 6869 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6870 6871 recov_retry: 6872 if (type == NF4LNK) 6873 args.ctag = TAG_SYMLINK; 6874 else if (type == NF4DIR) 6875 args.ctag = TAG_MKDIR; 6876 else 6877 args.ctag = TAG_MKNOD; 6878 6879 args.array_len = numops; 6880 args.array = argop; 6881 6882 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6883 nfs_rw_exit(&drp->r_rwlock); 6884 kmem_free(argop, argoplist_size); 6885 return (e.error); 6886 } 6887 need_end_op = TRUE; 6888 6889 6890 /* 0: putfh directory */ 6891 argop[0].argop = OP_CPUTFH; 6892 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6893 6894 /* 1/2: Create object */ 6895 argop[idx_create].argop = OP_CCREATE; 6896 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6897 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6898 if (type == NF4LNK) { 6899 /* 6900 * symlink, treat name as data 6901 */ 6902 ASSERT(data != NULL); 6903 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6904 (char *)data; 6905 } 6906 if (type == NF4BLK || type == NF4CHR) { 6907 ASSERT(data != NULL); 6908 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6909 *((specdata4 *)data); 6910 } 6911 6912 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6913 6914 svp = drp->r_server; 6915 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6916 supp_attrs = svp->sv_supp_attrs; 6917 nfs_rw_exit(&svp->sv_lock); 6918 6919 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6920 nfs_rw_exit(&drp->r_rwlock); 6921 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6922 e.error = EINVAL; 6923 kmem_free(argop, argoplist_size); 6924 return (e.error); 6925 } 6926 6927 /* 2/3: getfh fh of created object */ 6928 ASSERT(idx_create + 1 == idx_fattr - 1); 6929 argop[idx_create + 1].argop = OP_GETFH; 6930 6931 /* 3/4: getattr of new object */ 6932 argop[idx_fattr].argop = OP_GETATTR; 6933 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6934 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6935 6936 if (setgid_flag) { 6937 vattr_t _v; 6938 6939 argop[4].argop = OP_SAVEFH; 6940 6941 argop[5].argop = OP_CPUTFH; 6942 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6943 6944 argop[6].argop = OP_GETATTR; 6945 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6946 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6947 6948 argop[7].argop = OP_RESTOREFH; 6949 6950 /* 6951 * nverify 6952 * 6953 * XXX - Revisit the last argument to nfs4_end_op() 6954 * once 5020486 is fixed. 6955 */ 6956 _v.va_mask = AT_GID; 6957 _v.va_gid = va->va_gid; 6958 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6959 supp_attrs)) { 6960 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6961 nfs_rw_exit(&drp->r_rwlock); 6962 nfs4_fattr4_free(crattr); 6963 kmem_free(argop, argoplist_size); 6964 return (e.error); 6965 } 6966 6967 /* 6968 * setattr 6969 * 6970 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6971 * so no need for stateid or flags. Also we specify NULL 6972 * rp since we're only interested in setting owner_group 6973 * attributes. 6974 */ 6975 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 6976 &e.error, 0); 6977 6978 if (e.error) { 6979 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6980 nfs_rw_exit(&drp->r_rwlock); 6981 nfs4_fattr4_free(crattr); 6982 nfs4args_verify_free(&argop[8]); 6983 kmem_free(argop, argoplist_size); 6984 return (e.error); 6985 } 6986 } else { 6987 argop[1].argop = OP_SAVEFH; 6988 6989 argop[5].argop = OP_RESTOREFH; 6990 6991 argop[6].argop = OP_GETATTR; 6992 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6993 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6994 } 6995 6996 dnlc_remove(dvp, nm); 6997 6998 doqueue = 1; 6999 t = gethrtime(); 7000 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7001 7002 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7003 if (e.error) { 7004 PURGE_ATTRCACHE4(dvp); 7005 if (!needrecov) 7006 goto out; 7007 } 7008 7009 if (needrecov) { 7010 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 7011 OP_CREATE, NULL, NULL, NULL) == FALSE) { 7012 nfs4_end_op(mi, dvp, NULL, &recov_state, 7013 needrecov); 7014 need_end_op = FALSE; 7015 nfs4_fattr4_free(crattr); 7016 if (setgid_flag) { 7017 nfs4args_verify_free(&argop[8]); 7018 nfs4args_setattr_free(&argop[9]); 7019 } 7020 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 7021 goto recov_retry; 7022 } 7023 } 7024 7025 resp = &res; 7026 7027 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 7028 7029 if (res.status == NFS4ERR_BADOWNER) 7030 nfs4_log_badowner(mi, OP_CREATE); 7031 7032 e.error = geterrno4(res.status); 7033 7034 /* 7035 * This check is left over from when create was implemented 7036 * using a setattr op (instead of createattrs). If the 7037 * putfh/create/getfh failed, the error was returned. If 7038 * setattr/getattr failed, we keep going. 7039 * 7040 * It might be better to get rid of the GETFH also, and just 7041 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 7042 * Then if any of the operations failed, we could return the 7043 * error now, and remove much of the error code below. 7044 */ 7045 if (res.array_len <= idx_fattr) { 7046 /* 7047 * Either Putfh, Create or Getfh failed. 7048 */ 7049 PURGE_ATTRCACHE4(dvp); 7050 /* 7051 * nfs4_purge_stale_fh() may generate otw calls through 7052 * nfs4_invalidate_pages. Hence the need to call 7053 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 7054 */ 7055 nfs4_end_op(mi, dvp, NULL, &recov_state, 7056 needrecov); 7057 need_end_op = FALSE; 7058 nfs4_purge_stale_fh(e.error, dvp, cr); 7059 goto out; 7060 } 7061 } 7062 7063 resop = &res.array[idx_create]; /* create res */ 7064 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 7065 7066 resop = &res.array[idx_create + 1]; /* getfh res */ 7067 gf_res = &resop->nfs_resop4_u.opgetfh; 7068 7069 sfhp = sfh4_get(&gf_res->object, mi); 7070 if (e.error) { 7071 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 7072 fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7073 if (vp->v_type == VNON) { 7074 vattr.va_mask = AT_TYPE; 7075 /* 7076 * Need to call nfs4_end_op before nfs4getattr to avoid 7077 * potential nfs4_start_op deadlock. See RFE 4777612. 7078 */ 7079 nfs4_end_op(mi, dvp, NULL, &recov_state, 7080 needrecov); 7081 need_end_op = FALSE; 7082 e.error = nfs4getattr(vp, &vattr, cr); 7083 if (e.error) { 7084 VN_RELE(vp); 7085 *vpp = NULL; 7086 goto out; 7087 } 7088 vp->v_type = vattr.va_type; 7089 } 7090 e.error = 0; 7091 } else { 7092 *vpp = vp = makenfs4node(sfhp, 7093 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 7094 dvp->v_vfsp, t, cr, 7095 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7096 } 7097 7098 /* 7099 * If compound succeeded, then update dir attrs 7100 */ 7101 if (res.status == NFS4_OK) { 7102 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 7103 dinfo.di_cred = cr; 7104 dinfo.di_time_call = t; 7105 dinfop = &dinfo; 7106 } else 7107 dinfop = NULL; 7108 7109 /* Update directory cache attribute, readdir and dnlc caches */ 7110 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 7111 7112 out: 7113 if (sfhp != NULL) 7114 sfh4_rele(&sfhp); 7115 nfs_rw_exit(&drp->r_rwlock); 7116 nfs4_fattr4_free(crattr); 7117 if (setgid_flag) { 7118 nfs4args_verify_free(&argop[8]); 7119 nfs4args_setattr_free(&argop[9]); 7120 } 7121 if (resp) 7122 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7123 if (need_end_op) 7124 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7125 7126 kmem_free(argop, argoplist_size); 7127 return (e.error); 7128 } 7129 7130 /* ARGSUSED */ 7131 static int 7132 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7133 int mode, vnode_t **vpp, cred_t *cr) 7134 { 7135 int error; 7136 vnode_t *vp; 7137 nfs_ftype4 type; 7138 specdata4 spec, *specp = NULL; 7139 7140 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7141 7142 switch (va->va_type) { 7143 case VCHR: 7144 case VBLK: 7145 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7146 spec.specdata1 = getmajor(va->va_rdev); 7147 spec.specdata2 = getminor(va->va_rdev); 7148 specp = &spec; 7149 break; 7150 7151 case VFIFO: 7152 type = NF4FIFO; 7153 break; 7154 case VSOCK: 7155 type = NF4SOCK; 7156 break; 7157 7158 default: 7159 return (EINVAL); 7160 } 7161 7162 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7163 if (error) { 7164 return (error); 7165 } 7166 7167 /* 7168 * This might not be needed any more; special case to deal 7169 * with problematic v2/v3 servers. Since create was unable 7170 * to set group correctly, not sure what hope setattr has. 7171 */ 7172 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7173 va->va_mask = AT_GID; 7174 (void) nfs4setattr(vp, va, 0, cr, NULL); 7175 } 7176 7177 /* 7178 * If vnode is a device create special vnode 7179 */ 7180 if (ISVDEV(vp->v_type)) { 7181 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7182 VN_RELE(vp); 7183 } else { 7184 *vpp = vp; 7185 } 7186 return (error); 7187 } 7188 7189 /* 7190 * Remove requires that the current fh be the target directory. 7191 * After the operation, the current fh is unchanged. 7192 * The compound op structure is: 7193 * PUTFH(targetdir), REMOVE 7194 * 7195 * Weirdness: if the vnode to be removed is open 7196 * we rename it instead of removing it and nfs_inactive 7197 * will remove the new name. 7198 */ 7199 /* ARGSUSED */ 7200 static int 7201 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags) 7202 { 7203 COMPOUND4args_clnt args; 7204 COMPOUND4res_clnt res, *resp = NULL; 7205 REMOVE4res *rm_res; 7206 nfs_argop4 argop[3]; 7207 nfs_resop4 *resop; 7208 vnode_t *vp; 7209 char *tmpname; 7210 int doqueue; 7211 mntinfo4_t *mi; 7212 rnode4_t *rp; 7213 rnode4_t *drp; 7214 int needrecov = 0; 7215 nfs4_recov_state_t recov_state; 7216 int isopen; 7217 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7218 dirattr_info_t dinfo; 7219 7220 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7221 return (EPERM); 7222 drp = VTOR4(dvp); 7223 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7224 return (EINTR); 7225 7226 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7227 if (e.error) { 7228 nfs_rw_exit(&drp->r_rwlock); 7229 return (e.error); 7230 } 7231 7232 if (vp->v_type == VDIR) { 7233 VN_RELE(vp); 7234 nfs_rw_exit(&drp->r_rwlock); 7235 return (EISDIR); 7236 } 7237 7238 /* 7239 * First just remove the entry from the name cache, as it 7240 * is most likely the only entry for this vp. 7241 */ 7242 dnlc_remove(dvp, nm); 7243 7244 rp = VTOR4(vp); 7245 7246 /* 7247 * For regular file types, check to see if the file is open by looking 7248 * at the open streams. 7249 * For all other types, check the reference count on the vnode. Since 7250 * they are not opened OTW they never have an open stream. 7251 * 7252 * If the file is open, rename it to .nfsXXXX. 7253 */ 7254 if (vp->v_type != VREG) { 7255 /* 7256 * If the file has a v_count > 1 then there may be more than one 7257 * entry in the name cache due multiple links or an open file, 7258 * but we don't have the real reference count so flush all 7259 * possible entries. 7260 */ 7261 if (vp->v_count > 1) 7262 dnlc_purge_vp(vp); 7263 7264 /* 7265 * Now we have the real reference count. 7266 */ 7267 isopen = vp->v_count > 1; 7268 } else { 7269 mutex_enter(&rp->r_os_lock); 7270 isopen = list_head(&rp->r_open_streams) != NULL; 7271 mutex_exit(&rp->r_os_lock); 7272 } 7273 7274 mutex_enter(&rp->r_statelock); 7275 if (isopen && 7276 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7277 mutex_exit(&rp->r_statelock); 7278 tmpname = newname(); 7279 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct); 7280 if (e.error) 7281 kmem_free(tmpname, MAXNAMELEN); 7282 else { 7283 mutex_enter(&rp->r_statelock); 7284 if (rp->r_unldvp == NULL) { 7285 VN_HOLD(dvp); 7286 rp->r_unldvp = dvp; 7287 if (rp->r_unlcred != NULL) 7288 crfree(rp->r_unlcred); 7289 crhold(cr); 7290 rp->r_unlcred = cr; 7291 rp->r_unlname = tmpname; 7292 } else { 7293 kmem_free(rp->r_unlname, MAXNAMELEN); 7294 rp->r_unlname = tmpname; 7295 } 7296 mutex_exit(&rp->r_statelock); 7297 } 7298 VN_RELE(vp); 7299 nfs_rw_exit(&drp->r_rwlock); 7300 return (e.error); 7301 } 7302 /* 7303 * Actually remove the file/dir 7304 */ 7305 mutex_exit(&rp->r_statelock); 7306 7307 /* 7308 * We need to flush any dirty pages which happen to 7309 * be hanging around before removing the file. 7310 * This shouldn't happen very often since in NFSv4 7311 * we should be close to open consistent. 7312 */ 7313 if (nfs4_has_pages(vp) && 7314 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7315 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct); 7316 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7317 mutex_enter(&rp->r_statelock); 7318 if (!rp->r_error) 7319 rp->r_error = e.error; 7320 mutex_exit(&rp->r_statelock); 7321 } 7322 } 7323 7324 mi = VTOMI4(dvp); 7325 7326 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7327 recov_state.rs_flags = 0; 7328 recov_state.rs_num_retry_despite_err = 0; 7329 7330 recov_retry: 7331 /* 7332 * Remove ops: putfh dir; remove 7333 */ 7334 args.ctag = TAG_REMOVE; 7335 args.array_len = 3; 7336 args.array = argop; 7337 7338 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7339 if (e.error) { 7340 nfs_rw_exit(&drp->r_rwlock); 7341 VN_RELE(vp); 7342 return (e.error); 7343 } 7344 7345 /* putfh directory */ 7346 argop[0].argop = OP_CPUTFH; 7347 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7348 7349 /* remove */ 7350 argop[1].argop = OP_CREMOVE; 7351 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7352 7353 /* getattr dir */ 7354 argop[2].argop = OP_GETATTR; 7355 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7356 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7357 7358 doqueue = 1; 7359 dinfo.di_time_call = gethrtime(); 7360 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7361 7362 PURGE_ATTRCACHE4(vp); 7363 7364 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7365 if (e.error) 7366 PURGE_ATTRCACHE4(dvp); 7367 7368 if (needrecov) { 7369 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7370 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 7371 if (!e.error) 7372 (void) xdr_free(xdr_COMPOUND4res_clnt, 7373 (caddr_t)&res); 7374 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7375 needrecov); 7376 goto recov_retry; 7377 } 7378 } 7379 7380 /* 7381 * Matching nfs4_end_op() for start_op() above. 7382 * There is a path in the code below which calls 7383 * nfs4_purge_stale_fh(), which may generate otw calls through 7384 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7385 * here to avoid nfs4_start_op() deadlock. 7386 */ 7387 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7388 7389 if (!e.error) { 7390 resp = &res; 7391 7392 if (res.status) { 7393 e.error = geterrno4(res.status); 7394 PURGE_ATTRCACHE4(dvp); 7395 nfs4_purge_stale_fh(e.error, dvp, cr); 7396 } else { 7397 resop = &res.array[1]; /* remove res */ 7398 rm_res = &resop->nfs_resop4_u.opremove; 7399 7400 dinfo.di_garp = 7401 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7402 dinfo.di_cred = cr; 7403 7404 /* Update directory attr, readdir and dnlc caches */ 7405 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7406 &dinfo); 7407 } 7408 } 7409 nfs_rw_exit(&drp->r_rwlock); 7410 if (resp) 7411 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7412 7413 if (e.error == 0) { 7414 vnode_t *tvp; 7415 rnode4_t *trp; 7416 trp = VTOR4(vp); 7417 tvp = vp; 7418 if (IS_SHADOW(vp, trp)) 7419 tvp = RTOV4(trp); 7420 vnevent_remove(tvp, dvp, nm, ct); 7421 } 7422 VN_RELE(vp); 7423 return (e.error); 7424 } 7425 7426 /* 7427 * Link requires that the current fh be the target directory and the 7428 * saved fh be the source fh. After the operation, the current fh is unchanged. 7429 * Thus the compound op structure is: 7430 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7431 * GETATTR(file) 7432 */ 7433 /* ARGSUSED */ 7434 static int 7435 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr, 7436 caller_context_t *ct, int flags) 7437 { 7438 COMPOUND4args_clnt args; 7439 COMPOUND4res_clnt res, *resp = NULL; 7440 LINK4res *ln_res; 7441 int argoplist_size = 7 * sizeof (nfs_argop4); 7442 nfs_argop4 *argop; 7443 nfs_resop4 *resop; 7444 vnode_t *realvp, *nvp; 7445 int doqueue; 7446 mntinfo4_t *mi; 7447 rnode4_t *tdrp; 7448 bool_t needrecov = FALSE; 7449 nfs4_recov_state_t recov_state; 7450 hrtime_t t; 7451 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7452 dirattr_info_t dinfo; 7453 7454 ASSERT(*tnm != '\0'); 7455 ASSERT(tdvp->v_type == VDIR); 7456 ASSERT(nfs4_consistent_type(tdvp)); 7457 ASSERT(nfs4_consistent_type(svp)); 7458 7459 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7460 return (EPERM); 7461 if (VOP_REALVP(svp, &realvp, ct) == 0) { 7462 svp = realvp; 7463 ASSERT(nfs4_consistent_type(svp)); 7464 } 7465 7466 tdrp = VTOR4(tdvp); 7467 mi = VTOMI4(svp); 7468 7469 if (!(mi->mi_flags & MI4_LINK)) { 7470 return (EOPNOTSUPP); 7471 } 7472 recov_state.rs_flags = 0; 7473 recov_state.rs_num_retry_despite_err = 0; 7474 7475 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7476 return (EINTR); 7477 7478 recov_retry: 7479 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7480 7481 args.ctag = TAG_LINK; 7482 7483 /* 7484 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7485 * restorefh; getattr(fl) 7486 */ 7487 args.array_len = 7; 7488 args.array = argop; 7489 7490 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7491 if (e.error) { 7492 kmem_free(argop, argoplist_size); 7493 nfs_rw_exit(&tdrp->r_rwlock); 7494 return (e.error); 7495 } 7496 7497 /* 0. putfh file */ 7498 argop[0].argop = OP_CPUTFH; 7499 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7500 7501 /* 1. save current fh to free up the space for the dir */ 7502 argop[1].argop = OP_SAVEFH; 7503 7504 /* 2. putfh targetdir */ 7505 argop[2].argop = OP_CPUTFH; 7506 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7507 7508 /* 3. link: current_fh is targetdir, saved_fh is source */ 7509 argop[3].argop = OP_CLINK; 7510 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7511 7512 /* 4. Get attributes of dir */ 7513 argop[4].argop = OP_GETATTR; 7514 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7515 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7516 7517 /* 5. If link was successful, restore current vp to file */ 7518 argop[5].argop = OP_RESTOREFH; 7519 7520 /* 6. Get attributes of linked object */ 7521 argop[6].argop = OP_GETATTR; 7522 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7523 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7524 7525 dnlc_remove(tdvp, tnm); 7526 7527 doqueue = 1; 7528 t = gethrtime(); 7529 7530 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7531 7532 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7533 if (e.error != 0 && !needrecov) { 7534 PURGE_ATTRCACHE4(tdvp); 7535 PURGE_ATTRCACHE4(svp); 7536 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7537 goto out; 7538 } 7539 7540 if (needrecov) { 7541 bool_t abort; 7542 7543 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7544 NULL, NULL, OP_LINK, NULL, NULL, NULL); 7545 if (abort == FALSE) { 7546 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7547 needrecov); 7548 kmem_free(argop, argoplist_size); 7549 if (!e.error) 7550 (void) xdr_free(xdr_COMPOUND4res_clnt, 7551 (caddr_t)&res); 7552 goto recov_retry; 7553 } else { 7554 if (e.error != 0) { 7555 PURGE_ATTRCACHE4(tdvp); 7556 PURGE_ATTRCACHE4(svp); 7557 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7558 &recov_state, needrecov); 7559 goto out; 7560 } 7561 /* fall through for res.status case */ 7562 } 7563 } 7564 7565 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7566 7567 resp = &res; 7568 if (res.status) { 7569 /* If link succeeded, then don't return error */ 7570 e.error = geterrno4(res.status); 7571 if (res.array_len <= 4) { 7572 /* 7573 * Either Putfh, Savefh, Putfh dir, or Link failed 7574 */ 7575 PURGE_ATTRCACHE4(svp); 7576 PURGE_ATTRCACHE4(tdvp); 7577 if (e.error == EOPNOTSUPP) { 7578 mutex_enter(&mi->mi_lock); 7579 mi->mi_flags &= ~MI4_LINK; 7580 mutex_exit(&mi->mi_lock); 7581 } 7582 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7583 /* XXX-LP */ 7584 if (e.error == EISDIR && crgetuid(cr) != 0) 7585 e.error = EPERM; 7586 goto out; 7587 } 7588 } 7589 7590 /* either no error or one of the postop getattr failed */ 7591 7592 /* 7593 * XXX - if LINK succeeded, but no attrs were returned for link 7594 * file, purge its cache. 7595 * 7596 * XXX Perform a simplified version of wcc checking. Instead of 7597 * have another getattr to get pre-op, just purge cache if 7598 * any of the ops prior to and including the getattr failed. 7599 * If the getattr succeeded then update the attrcache accordingly. 7600 */ 7601 7602 /* 7603 * update cache with link file postattrs. 7604 * Note: at this point resop points to link res. 7605 */ 7606 resop = &res.array[3]; /* link res */ 7607 ln_res = &resop->nfs_resop4_u.oplink; 7608 if (res.status == NFS4_OK) 7609 e.error = nfs4_update_attrcache(res.status, 7610 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7611 t, svp, cr); 7612 7613 /* 7614 * Call makenfs4node to create the new shadow vp for tnm. 7615 * We pass NULL attrs because we just cached attrs for 7616 * the src object. All we're trying to accomplish is to 7617 * to create the new shadow vnode. 7618 */ 7619 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7620 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh)); 7621 7622 /* Update target cache attribute, readdir and dnlc caches */ 7623 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7624 dinfo.di_time_call = t; 7625 dinfo.di_cred = cr; 7626 7627 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7628 ASSERT(nfs4_consistent_type(tdvp)); 7629 ASSERT(nfs4_consistent_type(svp)); 7630 ASSERT(nfs4_consistent_type(nvp)); 7631 VN_RELE(nvp); 7632 7633 if (!e.error) { 7634 vnode_t *tvp; 7635 rnode4_t *trp; 7636 /* 7637 * Notify the source file of this link operation. 7638 */ 7639 trp = VTOR4(svp); 7640 tvp = svp; 7641 if (IS_SHADOW(svp, trp)) 7642 tvp = RTOV4(trp); 7643 vnevent_link(tvp, ct); 7644 } 7645 out: 7646 kmem_free(argop, argoplist_size); 7647 if (resp) 7648 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7649 7650 nfs_rw_exit(&tdrp->r_rwlock); 7651 7652 return (e.error); 7653 } 7654 7655 /* ARGSUSED */ 7656 static int 7657 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7658 caller_context_t *ct, int flags) 7659 { 7660 vnode_t *realvp; 7661 7662 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7663 return (EPERM); 7664 if (VOP_REALVP(ndvp, &realvp, ct) == 0) 7665 ndvp = realvp; 7666 7667 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct)); 7668 } 7669 7670 /* 7671 * nfs4rename does the real work of renaming in NFS Version 4. 7672 * 7673 * A file handle is considered volatile for renaming purposes if either 7674 * of the volatile bits are turned on. However, the compound may differ 7675 * based on the likelihood of the filehandle to change during rename. 7676 */ 7677 static int 7678 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7679 caller_context_t *ct) 7680 { 7681 int error; 7682 mntinfo4_t *mi; 7683 vnode_t *nvp = NULL; 7684 vnode_t *ovp = NULL; 7685 char *tmpname = NULL; 7686 rnode4_t *rp; 7687 rnode4_t *odrp; 7688 rnode4_t *ndrp; 7689 int did_link = 0; 7690 int do_link = 1; 7691 nfsstat4 stat = NFS4_OK; 7692 7693 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7694 ASSERT(nfs4_consistent_type(odvp)); 7695 ASSERT(nfs4_consistent_type(ndvp)); 7696 7697 if (onm[0] == '.' && (onm[1] == '\0' || 7698 (onm[1] == '.' && onm[2] == '\0'))) 7699 return (EINVAL); 7700 7701 if (nnm[0] == '.' && (nnm[1] == '\0' || 7702 (nnm[1] == '.' && nnm[2] == '\0'))) 7703 return (EINVAL); 7704 7705 odrp = VTOR4(odvp); 7706 ndrp = VTOR4(ndvp); 7707 if ((intptr_t)odrp < (intptr_t)ndrp) { 7708 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7709 return (EINTR); 7710 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7711 nfs_rw_exit(&odrp->r_rwlock); 7712 return (EINTR); 7713 } 7714 } else { 7715 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7716 return (EINTR); 7717 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7718 nfs_rw_exit(&ndrp->r_rwlock); 7719 return (EINTR); 7720 } 7721 } 7722 7723 /* 7724 * Lookup the target file. If it exists, it needs to be 7725 * checked to see whether it is a mount point and whether 7726 * it is active (open). 7727 */ 7728 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7729 if (!error) { 7730 int isactive; 7731 7732 ASSERT(nfs4_consistent_type(nvp)); 7733 /* 7734 * If this file has been mounted on, then just 7735 * return busy because renaming to it would remove 7736 * the mounted file system from the name space. 7737 */ 7738 if (vn_ismntpt(nvp)) { 7739 VN_RELE(nvp); 7740 nfs_rw_exit(&odrp->r_rwlock); 7741 nfs_rw_exit(&ndrp->r_rwlock); 7742 return (EBUSY); 7743 } 7744 7745 /* 7746 * First just remove the entry from the name cache, as it 7747 * is most likely the only entry for this vp. 7748 */ 7749 dnlc_remove(ndvp, nnm); 7750 7751 rp = VTOR4(nvp); 7752 7753 if (nvp->v_type != VREG) { 7754 /* 7755 * Purge the name cache of all references to this vnode 7756 * so that we can check the reference count to infer 7757 * whether it is active or not. 7758 */ 7759 if (nvp->v_count > 1) 7760 dnlc_purge_vp(nvp); 7761 7762 isactive = nvp->v_count > 1; 7763 } else { 7764 mutex_enter(&rp->r_os_lock); 7765 isactive = list_head(&rp->r_open_streams) != NULL; 7766 mutex_exit(&rp->r_os_lock); 7767 } 7768 7769 /* 7770 * If the vnode is active and is not a directory, 7771 * arrange to rename it to a 7772 * temporary file so that it will continue to be 7773 * accessible. This implements the "unlink-open-file" 7774 * semantics for the target of a rename operation. 7775 * Before doing this though, make sure that the 7776 * source and target files are not already the same. 7777 */ 7778 if (isactive && nvp->v_type != VDIR) { 7779 /* 7780 * Lookup the source name. 7781 */ 7782 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7783 7784 /* 7785 * The source name *should* already exist. 7786 */ 7787 if (error) { 7788 VN_RELE(nvp); 7789 nfs_rw_exit(&odrp->r_rwlock); 7790 nfs_rw_exit(&ndrp->r_rwlock); 7791 return (error); 7792 } 7793 7794 ASSERT(nfs4_consistent_type(ovp)); 7795 7796 /* 7797 * Compare the two vnodes. If they are the same, 7798 * just release all held vnodes and return success. 7799 */ 7800 if (VN_CMP(ovp, nvp)) { 7801 VN_RELE(ovp); 7802 VN_RELE(nvp); 7803 nfs_rw_exit(&odrp->r_rwlock); 7804 nfs_rw_exit(&ndrp->r_rwlock); 7805 return (0); 7806 } 7807 7808 /* 7809 * Can't mix and match directories and non- 7810 * directories in rename operations. We already 7811 * know that the target is not a directory. If 7812 * the source is a directory, return an error. 7813 */ 7814 if (ovp->v_type == VDIR) { 7815 VN_RELE(ovp); 7816 VN_RELE(nvp); 7817 nfs_rw_exit(&odrp->r_rwlock); 7818 nfs_rw_exit(&ndrp->r_rwlock); 7819 return (ENOTDIR); 7820 } 7821 link_call: 7822 /* 7823 * The target file exists, is not the same as 7824 * the source file, and is active. We first 7825 * try to Link it to a temporary filename to 7826 * avoid having the server removing the file 7827 * completely (which could cause data loss to 7828 * the user's POV in the event the Rename fails 7829 * -- see bug 1165874). 7830 */ 7831 /* 7832 * The do_link and did_link booleans are 7833 * introduced in the event we get NFS4ERR_FILE_OPEN 7834 * returned for the Rename. Some servers can 7835 * not Rename over an Open file, so they return 7836 * this error. The client needs to Remove the 7837 * newly created Link and do two Renames, just 7838 * as if the server didn't support LINK. 7839 */ 7840 tmpname = newname(); 7841 error = 0; 7842 7843 if (do_link) { 7844 error = nfs4_link(ndvp, nvp, tmpname, cr, 7845 NULL, 0); 7846 } 7847 if (error == EOPNOTSUPP || !do_link) { 7848 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7849 cr, NULL, 0); 7850 did_link = 0; 7851 } else { 7852 did_link = 1; 7853 } 7854 if (error) { 7855 kmem_free(tmpname, MAXNAMELEN); 7856 VN_RELE(ovp); 7857 VN_RELE(nvp); 7858 nfs_rw_exit(&odrp->r_rwlock); 7859 nfs_rw_exit(&ndrp->r_rwlock); 7860 return (error); 7861 } 7862 7863 mutex_enter(&rp->r_statelock); 7864 if (rp->r_unldvp == NULL) { 7865 VN_HOLD(ndvp); 7866 rp->r_unldvp = ndvp; 7867 if (rp->r_unlcred != NULL) 7868 crfree(rp->r_unlcred); 7869 crhold(cr); 7870 rp->r_unlcred = cr; 7871 rp->r_unlname = tmpname; 7872 } else { 7873 if (rp->r_unlname) 7874 kmem_free(rp->r_unlname, MAXNAMELEN); 7875 rp->r_unlname = tmpname; 7876 } 7877 mutex_exit(&rp->r_statelock); 7878 } 7879 7880 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7881 7882 ASSERT(nfs4_consistent_type(nvp)); 7883 } 7884 7885 if (ovp == NULL) { 7886 /* 7887 * When renaming directories to be a subdirectory of a 7888 * different parent, the dnlc entry for ".." will no 7889 * longer be valid, so it must be removed. 7890 * 7891 * We do a lookup here to determine whether we are renaming 7892 * a directory and we need to check if we are renaming 7893 * an unlinked file. This might have already been done 7894 * in previous code, so we check ovp == NULL to avoid 7895 * doing it twice. 7896 */ 7897 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7898 /* 7899 * The source name *should* already exist. 7900 */ 7901 if (error) { 7902 nfs_rw_exit(&odrp->r_rwlock); 7903 nfs_rw_exit(&ndrp->r_rwlock); 7904 if (nvp) { 7905 VN_RELE(nvp); 7906 } 7907 return (error); 7908 } 7909 ASSERT(ovp != NULL); 7910 ASSERT(nfs4_consistent_type(ovp)); 7911 } 7912 7913 /* 7914 * Is the object being renamed a dir, and if so, is 7915 * it being renamed to a child of itself? The underlying 7916 * fs should ultimately return EINVAL for this case; 7917 * however, buggy beta non-Solaris NFSv4 servers at 7918 * interop testing events have allowed this behavior, 7919 * and it caused our client to panic due to a recursive 7920 * mutex_enter in fn_move. 7921 * 7922 * The tedious locking in fn_move could be changed to 7923 * deal with this case, and the client could avoid the 7924 * panic; however, the client would just confuse itself 7925 * later and misbehave. A better way to handle the broken 7926 * server is to detect this condition and return EINVAL 7927 * without ever sending the the bogus rename to the server. 7928 * We know the rename is invalid -- just fail it now. 7929 */ 7930 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7931 VN_RELE(ovp); 7932 nfs_rw_exit(&odrp->r_rwlock); 7933 nfs_rw_exit(&ndrp->r_rwlock); 7934 if (nvp) { 7935 VN_RELE(nvp); 7936 } 7937 return (EINVAL); 7938 } 7939 7940 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7941 7942 /* 7943 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7944 * possible for the filehandle to change due to the rename. 7945 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7946 * the fh will not change because of the rename, but we still need 7947 * to update its rnode entry with the new name for 7948 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7949 * has no effect on these for now, but for future improvements, 7950 * we might want to use it too to simplify handling of files 7951 * that are open with that flag on. (XXX) 7952 */ 7953 mi = VTOMI4(odvp); 7954 if (NFS4_VOLATILE_FH(mi)) 7955 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7956 &stat); 7957 else 7958 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7959 &stat); 7960 7961 ASSERT(nfs4_consistent_type(odvp)); 7962 ASSERT(nfs4_consistent_type(ndvp)); 7963 ASSERT(nfs4_consistent_type(ovp)); 7964 7965 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7966 do_link = 0; 7967 /* 7968 * Before the 'link_call' code, we did a nfs4_lookup 7969 * that puts a VN_HOLD on nvp. After the nfs4_link 7970 * call we call VN_RELE to match that hold. We need 7971 * to place an additional VN_HOLD here since we will 7972 * be hitting that VN_RELE again. 7973 */ 7974 VN_HOLD(nvp); 7975 7976 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0); 7977 7978 /* Undo the unlinked file naming stuff we just did */ 7979 mutex_enter(&rp->r_statelock); 7980 if (rp->r_unldvp) { 7981 VN_RELE(ndvp); 7982 rp->r_unldvp = NULL; 7983 if (rp->r_unlcred != NULL) 7984 crfree(rp->r_unlcred); 7985 rp->r_unlcred = NULL; 7986 /* rp->r_unlanme points to tmpname */ 7987 if (rp->r_unlname) 7988 kmem_free(rp->r_unlname, MAXNAMELEN); 7989 rp->r_unlname = NULL; 7990 } 7991 mutex_exit(&rp->r_statelock); 7992 7993 if (nvp) { 7994 VN_RELE(nvp); 7995 } 7996 goto link_call; 7997 } 7998 7999 if (error) { 8000 VN_RELE(ovp); 8001 nfs_rw_exit(&odrp->r_rwlock); 8002 nfs_rw_exit(&ndrp->r_rwlock); 8003 if (nvp) { 8004 VN_RELE(nvp); 8005 } 8006 return (error); 8007 } 8008 8009 /* 8010 * when renaming directories to be a subdirectory of a 8011 * different parent, the dnlc entry for ".." will no 8012 * longer be valid, so it must be removed 8013 */ 8014 rp = VTOR4(ovp); 8015 if (ndvp != odvp) { 8016 if (ovp->v_type == VDIR) { 8017 dnlc_remove(ovp, ".."); 8018 if (rp->r_dir != NULL) 8019 nfs4_purge_rddir_cache(ovp); 8020 } 8021 } 8022 8023 /* 8024 * If we are renaming the unlinked file, update the 8025 * r_unldvp and r_unlname as needed. 8026 */ 8027 mutex_enter(&rp->r_statelock); 8028 if (rp->r_unldvp != NULL) { 8029 if (strcmp(rp->r_unlname, onm) == 0) { 8030 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 8031 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 8032 if (ndvp != rp->r_unldvp) { 8033 VN_RELE(rp->r_unldvp); 8034 rp->r_unldvp = ndvp; 8035 VN_HOLD(ndvp); 8036 } 8037 } 8038 } 8039 mutex_exit(&rp->r_statelock); 8040 8041 /* 8042 * Notify the rename vnevents to source vnode, and to the target 8043 * vnode if it already existed. 8044 */ 8045 if (error == 0) { 8046 vnode_t *tvp; 8047 rnode4_t *trp; 8048 /* 8049 * Notify the vnode. Each links is represented by 8050 * a different vnode, in nfsv4. 8051 */ 8052 if (nvp) { 8053 trp = VTOR4(nvp); 8054 tvp = nvp; 8055 if (IS_SHADOW(nvp, trp)) 8056 tvp = RTOV4(trp); 8057 vnevent_rename_dest(tvp, ndvp, nnm, ct); 8058 } 8059 8060 /* 8061 * if the source and destination directory are not the 8062 * same notify the destination directory. 8063 */ 8064 if (VTOR4(odvp) != VTOR4(ndvp)) { 8065 trp = VTOR4(ndvp); 8066 tvp = ndvp; 8067 if (IS_SHADOW(ndvp, trp)) 8068 tvp = RTOV4(trp); 8069 vnevent_rename_dest_dir(tvp, ct); 8070 } 8071 8072 trp = VTOR4(ovp); 8073 tvp = ovp; 8074 if (IS_SHADOW(ovp, trp)) 8075 tvp = RTOV4(trp); 8076 vnevent_rename_src(tvp, odvp, onm, ct); 8077 } 8078 8079 if (nvp) { 8080 VN_RELE(nvp); 8081 } 8082 VN_RELE(ovp); 8083 8084 nfs_rw_exit(&odrp->r_rwlock); 8085 nfs_rw_exit(&ndrp->r_rwlock); 8086 8087 return (error); 8088 } 8089 8090 /* 8091 * When the parent directory has changed, sv_dfh must be updated 8092 */ 8093 static void 8094 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp) 8095 { 8096 svnode_t *sv = VTOSV(vp); 8097 nfs4_sharedfh_t *old_dfh = sv->sv_dfh; 8098 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh; 8099 8100 sfh4_hold(new_dfh); 8101 sv->sv_dfh = new_dfh; 8102 sfh4_rele(&old_dfh); 8103 } 8104 8105 /* 8106 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 8107 * when it is known that the filehandle is persistent through rename. 8108 * 8109 * Rename requires that the current fh be the target directory and the 8110 * saved fh be the source directory. After the operation, the current fh 8111 * is unchanged. 8112 * The compound op structure for persistent fh rename is: 8113 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 8114 * Rather than bother with the directory postop args, we'll simply 8115 * update that a change occurred in the cache, so no post-op getattrs. 8116 */ 8117 static int 8118 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 8119 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8120 { 8121 COMPOUND4args_clnt args; 8122 COMPOUND4res_clnt res, *resp = NULL; 8123 nfs_argop4 *argop; 8124 nfs_resop4 *resop; 8125 int doqueue, argoplist_size; 8126 mntinfo4_t *mi; 8127 rnode4_t *odrp = VTOR4(odvp); 8128 rnode4_t *ndrp = VTOR4(ndvp); 8129 RENAME4res *rn_res; 8130 bool_t needrecov; 8131 nfs4_recov_state_t recov_state; 8132 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8133 dirattr_info_t dinfo, *dinfop; 8134 8135 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8136 8137 recov_state.rs_flags = 0; 8138 recov_state.rs_num_retry_despite_err = 0; 8139 8140 /* 8141 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 8142 * 8143 * If source/target are different dirs, then append putfh(src); getattr 8144 */ 8145 args.array_len = (odvp == ndvp) ? 5 : 7; 8146 argoplist_size = args.array_len * sizeof (nfs_argop4); 8147 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8148 8149 recov_retry: 8150 *statp = NFS4_OK; 8151 8152 /* No need to Lookup the file, persistent fh */ 8153 args.ctag = TAG_RENAME; 8154 8155 mi = VTOMI4(odvp); 8156 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8157 if (e.error) { 8158 kmem_free(argop, argoplist_size); 8159 return (e.error); 8160 } 8161 8162 /* 0: putfh source directory */ 8163 argop[0].argop = OP_CPUTFH; 8164 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8165 8166 /* 1: Save source fh to free up current for target */ 8167 argop[1].argop = OP_SAVEFH; 8168 8169 /* 2: putfh targetdir */ 8170 argop[2].argop = OP_CPUTFH; 8171 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8172 8173 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8174 argop[3].argop = OP_CRENAME; 8175 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8176 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8177 8178 /* 4: getattr (targetdir) */ 8179 argop[4].argop = OP_GETATTR; 8180 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8181 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8182 8183 if (ndvp != odvp) { 8184 8185 /* 5: putfh (sourcedir) */ 8186 argop[5].argop = OP_CPUTFH; 8187 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8188 8189 /* 6: getattr (sourcedir) */ 8190 argop[6].argop = OP_GETATTR; 8191 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8192 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8193 } 8194 8195 dnlc_remove(odvp, onm); 8196 dnlc_remove(ndvp, nnm); 8197 8198 doqueue = 1; 8199 dinfo.di_time_call = gethrtime(); 8200 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8201 8202 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8203 if (e.error) { 8204 PURGE_ATTRCACHE4(odvp); 8205 PURGE_ATTRCACHE4(ndvp); 8206 } else { 8207 *statp = res.status; 8208 } 8209 8210 if (needrecov) { 8211 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8212 OP_RENAME, NULL, NULL, NULL) == FALSE) { 8213 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8214 if (!e.error) 8215 (void) xdr_free(xdr_COMPOUND4res_clnt, 8216 (caddr_t)&res); 8217 goto recov_retry; 8218 } 8219 } 8220 8221 if (!e.error) { 8222 resp = &res; 8223 /* 8224 * as long as OP_RENAME 8225 */ 8226 if (res.status != NFS4_OK && res.array_len <= 4) { 8227 e.error = geterrno4(res.status); 8228 PURGE_ATTRCACHE4(odvp); 8229 PURGE_ATTRCACHE4(ndvp); 8230 /* 8231 * System V defines rename to return EEXIST, not 8232 * ENOTEMPTY if the target directory is not empty. 8233 * Over the wire, the error is NFSERR_ENOTEMPTY 8234 * which geterrno4 maps to ENOTEMPTY. 8235 */ 8236 if (e.error == ENOTEMPTY) 8237 e.error = EEXIST; 8238 } else { 8239 8240 resop = &res.array[3]; /* rename res */ 8241 rn_res = &resop->nfs_resop4_u.oprename; 8242 8243 if (res.status == NFS4_OK) { 8244 /* 8245 * Update target attribute, readdir and dnlc 8246 * caches. 8247 */ 8248 dinfo.di_garp = 8249 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8250 dinfo.di_cred = cr; 8251 dinfop = &dinfo; 8252 } else 8253 dinfop = NULL; 8254 8255 nfs4_update_dircaches(&rn_res->target_cinfo, 8256 ndvp, NULL, NULL, dinfop); 8257 8258 /* 8259 * Update source attribute, readdir and dnlc caches 8260 * 8261 */ 8262 if (ndvp != odvp) { 8263 update_parentdir_sfh(renvp, ndvp); 8264 8265 if (dinfop) 8266 dinfo.di_garp = 8267 &(res.array[6].nfs_resop4_u. 8268 opgetattr.ga_res); 8269 8270 nfs4_update_dircaches(&rn_res->source_cinfo, 8271 odvp, NULL, NULL, dinfop); 8272 } 8273 8274 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8275 nnm); 8276 } 8277 } 8278 8279 if (resp) 8280 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8281 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8282 kmem_free(argop, argoplist_size); 8283 8284 return (e.error); 8285 } 8286 8287 /* 8288 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8289 * it is possible for the filehandle to change due to the rename. 8290 * 8291 * The compound req in this case includes a post-rename lookup and getattr 8292 * to ensure that we have the correct fh and attributes for the object. 8293 * 8294 * Rename requires that the current fh be the target directory and the 8295 * saved fh be the source directory. After the operation, the current fh 8296 * is unchanged. 8297 * 8298 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8299 * update the filehandle for the renamed object. We also get the old 8300 * filehandle for historical reasons; this should be taken out sometime. 8301 * This results in a rather cumbersome compound... 8302 * 8303 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8304 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8305 * 8306 */ 8307 static int 8308 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8309 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8310 { 8311 COMPOUND4args_clnt args; 8312 COMPOUND4res_clnt res, *resp = NULL; 8313 int argoplist_size; 8314 nfs_argop4 *argop; 8315 nfs_resop4 *resop; 8316 int doqueue; 8317 mntinfo4_t *mi; 8318 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8319 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8320 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8321 RENAME4res *rn_res; 8322 GETFH4res *ngf_res; 8323 bool_t needrecov; 8324 nfs4_recov_state_t recov_state; 8325 hrtime_t t; 8326 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8327 dirattr_info_t dinfo, *dinfop = &dinfo; 8328 8329 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8330 8331 recov_state.rs_flags = 0; 8332 recov_state.rs_num_retry_despite_err = 0; 8333 8334 recov_retry: 8335 *statp = NFS4_OK; 8336 8337 /* 8338 * There is a window between the RPC and updating the path and 8339 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8340 * code, so that it doesn't try to use the old path during that 8341 * window. 8342 */ 8343 mutex_enter(&orp->r_statelock); 8344 while (orp->r_flags & R4RECEXPFH) { 8345 klwp_t *lwp = ttolwp(curthread); 8346 8347 if (lwp != NULL) 8348 lwp->lwp_nostop++; 8349 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8350 mutex_exit(&orp->r_statelock); 8351 if (lwp != NULL) 8352 lwp->lwp_nostop--; 8353 return (EINTR); 8354 } 8355 if (lwp != NULL) 8356 lwp->lwp_nostop--; 8357 } 8358 orp->r_flags |= R4RECEXPFH; 8359 mutex_exit(&orp->r_statelock); 8360 8361 mi = VTOMI4(odvp); 8362 8363 args.ctag = TAG_RENAME_VFH; 8364 args.array_len = (odvp == ndvp) ? 10 : 12; 8365 argoplist_size = args.array_len * sizeof (nfs_argop4); 8366 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8367 8368 /* 8369 * Rename ops: 8370 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8371 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8372 * LOOKUP(trgt), GETFH(new), GETATTR, 8373 * 8374 * if (odvp != ndvp) 8375 * add putfh(sourcedir), getattr(sourcedir) } 8376 */ 8377 args.array = argop; 8378 8379 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8380 &recov_state, NULL); 8381 if (e.error) { 8382 kmem_free(argop, argoplist_size); 8383 mutex_enter(&orp->r_statelock); 8384 orp->r_flags &= ~R4RECEXPFH; 8385 cv_broadcast(&orp->r_cv); 8386 mutex_exit(&orp->r_statelock); 8387 return (e.error); 8388 } 8389 8390 /* 0: putfh source directory */ 8391 argop[0].argop = OP_CPUTFH; 8392 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8393 8394 /* 1: Save source fh to free up current for target */ 8395 argop[1].argop = OP_SAVEFH; 8396 8397 /* 2: Lookup pre-rename fh of renamed object */ 8398 argop[2].argop = OP_CLOOKUP; 8399 argop[2].nfs_argop4_u.opclookup.cname = onm; 8400 8401 /* 3: getfh fh of renamed object (before rename) */ 8402 argop[3].argop = OP_GETFH; 8403 8404 /* 4: putfh targetdir */ 8405 argop[4].argop = OP_CPUTFH; 8406 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8407 8408 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8409 argop[5].argop = OP_CRENAME; 8410 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8411 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8412 8413 /* 6: getattr of target dir (post op attrs) */ 8414 argop[6].argop = OP_GETATTR; 8415 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8416 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8417 8418 /* 7: Lookup post-rename fh of renamed object */ 8419 argop[7].argop = OP_CLOOKUP; 8420 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8421 8422 /* 8: getfh fh of renamed object (after rename) */ 8423 argop[8].argop = OP_GETFH; 8424 8425 /* 9: getattr of renamed object */ 8426 argop[9].argop = OP_GETATTR; 8427 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8428 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8429 8430 /* 8431 * If source/target dirs are different, then get new post-op 8432 * attrs for source dir also. 8433 */ 8434 if (ndvp != odvp) { 8435 /* 10: putfh (sourcedir) */ 8436 argop[10].argop = OP_CPUTFH; 8437 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8438 8439 /* 11: getattr (sourcedir) */ 8440 argop[11].argop = OP_GETATTR; 8441 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8442 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8443 } 8444 8445 dnlc_remove(odvp, onm); 8446 dnlc_remove(ndvp, nnm); 8447 8448 doqueue = 1; 8449 t = gethrtime(); 8450 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8451 8452 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8453 if (e.error) { 8454 PURGE_ATTRCACHE4(odvp); 8455 PURGE_ATTRCACHE4(ndvp); 8456 if (!needrecov) { 8457 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8458 &recov_state, needrecov); 8459 goto out; 8460 } 8461 } else { 8462 *statp = res.status; 8463 } 8464 8465 if (needrecov) { 8466 bool_t abort; 8467 8468 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8469 OP_RENAME, NULL, NULL, NULL); 8470 if (abort == FALSE) { 8471 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8472 &recov_state, needrecov); 8473 kmem_free(argop, argoplist_size); 8474 if (!e.error) 8475 (void) xdr_free(xdr_COMPOUND4res_clnt, 8476 (caddr_t)&res); 8477 mutex_enter(&orp->r_statelock); 8478 orp->r_flags &= ~R4RECEXPFH; 8479 cv_broadcast(&orp->r_cv); 8480 mutex_exit(&orp->r_statelock); 8481 goto recov_retry; 8482 } else { 8483 if (e.error != 0) { 8484 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8485 &recov_state, needrecov); 8486 goto out; 8487 } 8488 /* fall through for res.status case */ 8489 } 8490 } 8491 8492 resp = &res; 8493 /* 8494 * If OP_RENAME (or any prev op) failed, then return an error. 8495 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8496 */ 8497 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8498 /* 8499 * Error in an op other than last Getattr 8500 */ 8501 e.error = geterrno4(res.status); 8502 PURGE_ATTRCACHE4(odvp); 8503 PURGE_ATTRCACHE4(ndvp); 8504 /* 8505 * System V defines rename to return EEXIST, not 8506 * ENOTEMPTY if the target directory is not empty. 8507 * Over the wire, the error is NFSERR_ENOTEMPTY 8508 * which geterrno4 maps to ENOTEMPTY. 8509 */ 8510 if (e.error == ENOTEMPTY) 8511 e.error = EEXIST; 8512 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8513 needrecov); 8514 goto out; 8515 } 8516 8517 /* rename results */ 8518 rn_res = &res.array[5].nfs_resop4_u.oprename; 8519 8520 if (res.status == NFS4_OK) { 8521 /* Update target attribute, readdir and dnlc caches */ 8522 dinfo.di_garp = 8523 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8524 dinfo.di_cred = cr; 8525 dinfo.di_time_call = t; 8526 } else 8527 dinfop = NULL; 8528 8529 /* Update source cache attribute, readdir and dnlc caches */ 8530 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8531 8532 /* Update source cache attribute, readdir and dnlc caches */ 8533 if (ndvp != odvp) { 8534 update_parentdir_sfh(ovp, ndvp); 8535 8536 /* 8537 * If dinfop is non-NULL, then compound succeded, so 8538 * set di_garp to attrs for source dir. dinfop is only 8539 * set to NULL when compound fails. 8540 */ 8541 if (dinfop) 8542 dinfo.di_garp = 8543 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8544 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8545 dinfop); 8546 } 8547 8548 /* 8549 * Update the rnode with the new component name and args, 8550 * and if the file handle changed, also update it with the new fh. 8551 * This is only necessary if the target object has an rnode 8552 * entry and there is no need to create one for it. 8553 */ 8554 resop = &res.array[8]; /* getfh new res */ 8555 ngf_res = &resop->nfs_resop4_u.opgetfh; 8556 8557 /* 8558 * Update the path and filehandle for the renamed object. 8559 */ 8560 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8561 8562 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8563 8564 if (res.status == NFS4_OK) { 8565 resop++; /* getattr res */ 8566 e.error = nfs4_update_attrcache(res.status, 8567 &resop->nfs_resop4_u.opgetattr.ga_res, 8568 t, ovp, cr); 8569 } 8570 8571 out: 8572 kmem_free(argop, argoplist_size); 8573 if (resp) 8574 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8575 mutex_enter(&orp->r_statelock); 8576 orp->r_flags &= ~R4RECEXPFH; 8577 cv_broadcast(&orp->r_cv); 8578 mutex_exit(&orp->r_statelock); 8579 8580 return (e.error); 8581 } 8582 8583 /* ARGSUSED */ 8584 static int 8585 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr, 8586 caller_context_t *ct, int flags, vsecattr_t *vsecp) 8587 { 8588 int error; 8589 vnode_t *vp; 8590 8591 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8592 return (EPERM); 8593 /* 8594 * As ".." has special meaning and rather than send a mkdir 8595 * over the wire to just let the server freak out, we just 8596 * short circuit it here and return EEXIST 8597 */ 8598 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8599 return (EEXIST); 8600 8601 /* 8602 * Decision to get the right gid and setgid bit of the 8603 * new directory is now made in call_nfs4_create_req. 8604 */ 8605 va->va_mask |= AT_MODE; 8606 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8607 if (error) 8608 return (error); 8609 8610 *vpp = vp; 8611 return (0); 8612 } 8613 8614 8615 /* 8616 * rmdir is using the same remove v4 op as does remove. 8617 * Remove requires that the current fh be the target directory. 8618 * After the operation, the current fh is unchanged. 8619 * The compound op structure is: 8620 * PUTFH(targetdir), REMOVE 8621 */ 8622 /*ARGSUSED4*/ 8623 static int 8624 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr, 8625 caller_context_t *ct, int flags) 8626 { 8627 int need_end_op = FALSE; 8628 COMPOUND4args_clnt args; 8629 COMPOUND4res_clnt res, *resp = NULL; 8630 REMOVE4res *rm_res; 8631 nfs_argop4 argop[3]; 8632 nfs_resop4 *resop; 8633 vnode_t *vp; 8634 int doqueue; 8635 mntinfo4_t *mi; 8636 rnode4_t *drp; 8637 bool_t needrecov = FALSE; 8638 nfs4_recov_state_t recov_state; 8639 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8640 dirattr_info_t dinfo, *dinfop; 8641 8642 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8643 return (EPERM); 8644 /* 8645 * As ".." has special meaning and rather than send a rmdir 8646 * over the wire to just let the server freak out, we just 8647 * short circuit it here and return EEXIST 8648 */ 8649 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8650 return (EEXIST); 8651 8652 drp = VTOR4(dvp); 8653 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8654 return (EINTR); 8655 8656 /* 8657 * Attempt to prevent a rmdir(".") from succeeding. 8658 */ 8659 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8660 if (e.error) { 8661 nfs_rw_exit(&drp->r_rwlock); 8662 return (e.error); 8663 } 8664 if (vp == cdir) { 8665 VN_RELE(vp); 8666 nfs_rw_exit(&drp->r_rwlock); 8667 return (EINVAL); 8668 } 8669 8670 /* 8671 * Since nfsv4 remove op works on both files and directories, 8672 * check that the removed object is indeed a directory. 8673 */ 8674 if (vp->v_type != VDIR) { 8675 VN_RELE(vp); 8676 nfs_rw_exit(&drp->r_rwlock); 8677 return (ENOTDIR); 8678 } 8679 8680 /* 8681 * First just remove the entry from the name cache, as it 8682 * is most likely an entry for this vp. 8683 */ 8684 dnlc_remove(dvp, nm); 8685 8686 /* 8687 * If there vnode reference count is greater than one, then 8688 * there may be additional references in the DNLC which will 8689 * need to be purged. First, trying removing the entry for 8690 * the parent directory and see if that removes the additional 8691 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8692 * to completely remove any references to the directory which 8693 * might still exist in the DNLC. 8694 */ 8695 if (vp->v_count > 1) { 8696 dnlc_remove(vp, ".."); 8697 if (vp->v_count > 1) 8698 dnlc_purge_vp(vp); 8699 } 8700 8701 mi = VTOMI4(dvp); 8702 recov_state.rs_flags = 0; 8703 recov_state.rs_num_retry_despite_err = 0; 8704 8705 recov_retry: 8706 args.ctag = TAG_RMDIR; 8707 8708 /* 8709 * Rmdir ops: putfh dir; remove 8710 */ 8711 args.array_len = 3; 8712 args.array = argop; 8713 8714 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8715 if (e.error) { 8716 nfs_rw_exit(&drp->r_rwlock); 8717 return (e.error); 8718 } 8719 need_end_op = TRUE; 8720 8721 /* putfh directory */ 8722 argop[0].argop = OP_CPUTFH; 8723 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8724 8725 /* remove */ 8726 argop[1].argop = OP_CREMOVE; 8727 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8728 8729 /* getattr (postop attrs for dir that contained removed dir) */ 8730 argop[2].argop = OP_GETATTR; 8731 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8732 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8733 8734 dinfo.di_time_call = gethrtime(); 8735 doqueue = 1; 8736 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8737 8738 PURGE_ATTRCACHE4(vp); 8739 8740 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8741 if (e.error) { 8742 PURGE_ATTRCACHE4(dvp); 8743 } 8744 8745 if (needrecov) { 8746 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8747 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 8748 if (!e.error) 8749 (void) xdr_free(xdr_COMPOUND4res_clnt, 8750 (caddr_t)&res); 8751 8752 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8753 needrecov); 8754 need_end_op = FALSE; 8755 goto recov_retry; 8756 } 8757 } 8758 8759 if (!e.error) { 8760 resp = &res; 8761 8762 /* 8763 * Only return error if first 2 ops (OP_REMOVE or earlier) 8764 * failed. 8765 */ 8766 if (res.status != NFS4_OK && res.array_len <= 2) { 8767 e.error = geterrno4(res.status); 8768 PURGE_ATTRCACHE4(dvp); 8769 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8770 &recov_state, needrecov); 8771 need_end_op = FALSE; 8772 nfs4_purge_stale_fh(e.error, dvp, cr); 8773 /* 8774 * System V defines rmdir to return EEXIST, not 8775 * ENOTEMPTY if the directory is not empty. Over 8776 * the wire, the error is NFSERR_ENOTEMPTY which 8777 * geterrno4 maps to ENOTEMPTY. 8778 */ 8779 if (e.error == ENOTEMPTY) 8780 e.error = EEXIST; 8781 } else { 8782 resop = &res.array[1]; /* remove res */ 8783 rm_res = &resop->nfs_resop4_u.opremove; 8784 8785 if (res.status == NFS4_OK) { 8786 resop = &res.array[2]; /* dir attrs */ 8787 dinfo.di_garp = 8788 &resop->nfs_resop4_u.opgetattr.ga_res; 8789 dinfo.di_cred = cr; 8790 dinfop = &dinfo; 8791 } else 8792 dinfop = NULL; 8793 8794 /* Update dir attribute, readdir and dnlc caches */ 8795 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8796 dinfop); 8797 8798 /* destroy rddir cache for dir that was removed */ 8799 if (VTOR4(vp)->r_dir != NULL) 8800 nfs4_purge_rddir_cache(vp); 8801 } 8802 } 8803 8804 if (need_end_op) 8805 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8806 8807 nfs_rw_exit(&drp->r_rwlock); 8808 8809 if (resp) 8810 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8811 8812 if (e.error == 0) { 8813 vnode_t *tvp; 8814 rnode4_t *trp; 8815 trp = VTOR4(vp); 8816 tvp = vp; 8817 if (IS_SHADOW(vp, trp)) 8818 tvp = RTOV4(trp); 8819 vnevent_rmdir(tvp, dvp, nm, ct); 8820 } 8821 8822 VN_RELE(vp); 8823 8824 return (e.error); 8825 } 8826 8827 /* ARGSUSED */ 8828 static int 8829 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr, 8830 caller_context_t *ct, int flags) 8831 { 8832 int error; 8833 vnode_t *vp; 8834 rnode4_t *rp; 8835 char *contents; 8836 mntinfo4_t *mi = VTOMI4(dvp); 8837 8838 if (nfs_zone() != mi->mi_zone) 8839 return (EPERM); 8840 if (!(mi->mi_flags & MI4_SYMLINK)) 8841 return (EOPNOTSUPP); 8842 8843 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8844 if (error) 8845 return (error); 8846 8847 ASSERT(nfs4_consistent_type(vp)); 8848 rp = VTOR4(vp); 8849 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8850 8851 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8852 8853 if (contents != NULL) { 8854 mutex_enter(&rp->r_statelock); 8855 if (rp->r_symlink.contents == NULL) { 8856 rp->r_symlink.len = strlen(tnm); 8857 bcopy(tnm, contents, rp->r_symlink.len); 8858 rp->r_symlink.contents = contents; 8859 rp->r_symlink.size = MAXPATHLEN; 8860 mutex_exit(&rp->r_statelock); 8861 } else { 8862 mutex_exit(&rp->r_statelock); 8863 kmem_free((void *)contents, MAXPATHLEN); 8864 } 8865 } 8866 } 8867 VN_RELE(vp); 8868 8869 return (error); 8870 } 8871 8872 8873 /* 8874 * Read directory entries. 8875 * There are some weird things to look out for here. The uio_loffset 8876 * field is either 0 or it is the offset returned from a previous 8877 * readdir. It is an opaque value used by the server to find the 8878 * correct directory block to read. The count field is the number 8879 * of blocks to read on the server. This is advisory only, the server 8880 * may return only one block's worth of entries. Entries may be compressed 8881 * on the server. 8882 */ 8883 /* ARGSUSED */ 8884 static int 8885 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp, 8886 caller_context_t *ct, int flags) 8887 { 8888 int error; 8889 uint_t count; 8890 rnode4_t *rp; 8891 rddir4_cache *rdc; 8892 rddir4_cache *rrdc; 8893 8894 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8895 return (EIO); 8896 rp = VTOR4(vp); 8897 8898 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8899 8900 /* 8901 * Make sure that the directory cache is valid. 8902 */ 8903 if (rp->r_dir != NULL) { 8904 if (nfs_disable_rddir_cache != 0) { 8905 /* 8906 * Setting nfs_disable_rddir_cache in /etc/system 8907 * allows interoperability with servers that do not 8908 * properly update the attributes of directories. 8909 * Any cached information gets purged before an 8910 * access is made to it. 8911 */ 8912 nfs4_purge_rddir_cache(vp); 8913 } 8914 8915 error = nfs4_validate_caches(vp, cr); 8916 if (error) 8917 return (error); 8918 } 8919 8920 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8921 8922 /* 8923 * Short circuit last readdir which always returns 0 bytes. 8924 * This can be done after the directory has been read through 8925 * completely at least once. This will set r_direof which 8926 * can be used to find the value of the last cookie. 8927 */ 8928 mutex_enter(&rp->r_statelock); 8929 if (rp->r_direof != NULL && 8930 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8931 mutex_exit(&rp->r_statelock); 8932 #ifdef DEBUG 8933 nfs4_readdir_cache_shorts++; 8934 #endif 8935 if (eofp) 8936 *eofp = 1; 8937 return (0); 8938 } 8939 8940 /* 8941 * Look for a cache entry. Cache entries are identified 8942 * by the NFS cookie value and the byte count requested. 8943 */ 8944 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8945 8946 /* 8947 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8948 */ 8949 if (rdc == NULL) { 8950 mutex_exit(&rp->r_statelock); 8951 return (EINTR); 8952 } 8953 8954 /* 8955 * Check to see if we need to fill this entry in. 8956 */ 8957 if (rdc->flags & RDDIRREQ) { 8958 rdc->flags &= ~RDDIRREQ; 8959 rdc->flags |= RDDIR; 8960 mutex_exit(&rp->r_statelock); 8961 8962 /* 8963 * Do the readdir. 8964 */ 8965 nfs4readdir(vp, rdc, cr); 8966 8967 /* 8968 * Reacquire the lock, so that we can continue 8969 */ 8970 mutex_enter(&rp->r_statelock); 8971 /* 8972 * The entry is now complete 8973 */ 8974 rdc->flags &= ~RDDIR; 8975 } 8976 8977 ASSERT(!(rdc->flags & RDDIR)); 8978 8979 /* 8980 * If an error occurred while attempting 8981 * to fill the cache entry, mark the entry invalid and 8982 * just return the error. 8983 */ 8984 if (rdc->error) { 8985 error = rdc->error; 8986 rdc->flags |= RDDIRREQ; 8987 rddir4_cache_rele(rp, rdc); 8988 mutex_exit(&rp->r_statelock); 8989 return (error); 8990 } 8991 8992 /* 8993 * The cache entry is complete and good, 8994 * copyout the dirent structs to the calling 8995 * thread. 8996 */ 8997 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 8998 8999 /* 9000 * If no error occurred during the copyout, 9001 * update the offset in the uio struct to 9002 * contain the value of the next NFS 4 cookie 9003 * and set the eof value appropriately. 9004 */ 9005 if (!error) { 9006 uiop->uio_loffset = rdc->nfs4_ncookie; 9007 if (eofp) 9008 *eofp = rdc->eof; 9009 } 9010 9011 /* 9012 * Decide whether to do readahead. Don't if we 9013 * have already read to the end of directory. 9014 */ 9015 if (rdc->eof) { 9016 /* 9017 * Make the entry the direof only if it is cached 9018 */ 9019 if (rdc->flags & RDDIRCACHED) 9020 rp->r_direof = rdc; 9021 rddir4_cache_rele(rp, rdc); 9022 mutex_exit(&rp->r_statelock); 9023 return (error); 9024 } 9025 9026 /* Determine if a readdir readahead should be done */ 9027 if (!(rp->r_flags & R4LOOKUP)) { 9028 rddir4_cache_rele(rp, rdc); 9029 mutex_exit(&rp->r_statelock); 9030 return (error); 9031 } 9032 9033 /* 9034 * Now look for a readahead entry. 9035 * 9036 * Check to see whether we found an entry for the readahead. 9037 * If so, we don't need to do anything further, so free the new 9038 * entry if one was allocated. Otherwise, allocate a new entry, add 9039 * it to the cache, and then initiate an asynchronous readdir 9040 * operation to fill it. 9041 */ 9042 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 9043 9044 /* 9045 * A readdir cache entry could not be obtained for the readahead. In 9046 * this case we skip the readahead and return. 9047 */ 9048 if (rrdc == NULL) { 9049 rddir4_cache_rele(rp, rdc); 9050 mutex_exit(&rp->r_statelock); 9051 return (error); 9052 } 9053 9054 /* 9055 * Check to see if we need to fill this entry in. 9056 */ 9057 if (rrdc->flags & RDDIRREQ) { 9058 rrdc->flags &= ~RDDIRREQ; 9059 rrdc->flags |= RDDIR; 9060 rddir4_cache_rele(rp, rdc); 9061 mutex_exit(&rp->r_statelock); 9062 #ifdef DEBUG 9063 nfs4_readdir_readahead++; 9064 #endif 9065 /* 9066 * Do the readdir. 9067 */ 9068 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 9069 return (error); 9070 } 9071 9072 rddir4_cache_rele(rp, rrdc); 9073 rddir4_cache_rele(rp, rdc); 9074 mutex_exit(&rp->r_statelock); 9075 return (error); 9076 } 9077 9078 static int 9079 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9080 { 9081 int error; 9082 rnode4_t *rp; 9083 9084 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9085 9086 rp = VTOR4(vp); 9087 9088 /* 9089 * Obtain the readdir results for the caller. 9090 */ 9091 nfs4readdir(vp, rdc, cr); 9092 9093 mutex_enter(&rp->r_statelock); 9094 /* 9095 * The entry is now complete 9096 */ 9097 rdc->flags &= ~RDDIR; 9098 9099 error = rdc->error; 9100 if (error) 9101 rdc->flags |= RDDIRREQ; 9102 rddir4_cache_rele(rp, rdc); 9103 mutex_exit(&rp->r_statelock); 9104 9105 return (error); 9106 } 9107 9108 /* 9109 * Read directory entries. 9110 * There are some weird things to look out for here. The uio_loffset 9111 * field is either 0 or it is the offset returned from a previous 9112 * readdir. It is an opaque value used by the server to find the 9113 * correct directory block to read. The count field is the number 9114 * of blocks to read on the server. This is advisory only, the server 9115 * may return only one block's worth of entries. Entries may be compressed 9116 * on the server. 9117 * 9118 * Generates the following compound request: 9119 * 1. If readdir offset is zero and no dnlc entry for parent exists, 9120 * must include a Lookupp as well. In this case, send: 9121 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 9122 * 2. Otherwise just do: { Putfh <fh>; Readdir } 9123 * 9124 * Get complete attributes and filehandles for entries if this is the 9125 * first read of the directory. Otherwise, just get fileid's. 9126 */ 9127 static void 9128 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9129 { 9130 COMPOUND4args_clnt args; 9131 COMPOUND4res_clnt res; 9132 READDIR4args *rargs; 9133 READDIR4res_clnt *rd_res; 9134 bitmap4 rd_bitsval; 9135 nfs_argop4 argop[5]; 9136 nfs_resop4 *resop; 9137 rnode4_t *rp = VTOR4(vp); 9138 mntinfo4_t *mi = VTOMI4(vp); 9139 int doqueue; 9140 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 9141 vnode_t *dvp; 9142 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 9143 int num_ops, res_opcnt; 9144 bool_t needrecov = FALSE; 9145 nfs4_recov_state_t recov_state; 9146 hrtime_t t; 9147 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 9148 9149 ASSERT(nfs_zone() == mi->mi_zone); 9150 ASSERT(rdc->flags & RDDIR); 9151 ASSERT(rdc->entries == NULL); 9152 9153 /* 9154 * If rp were a stub, it should have triggered and caused 9155 * a mount for us to get this far. 9156 */ 9157 ASSERT(!RP_ISSTUB(rp)); 9158 9159 num_ops = 2; 9160 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9161 /* 9162 * Since nfsv4 readdir may not return entries for "." and "..", 9163 * the client must recreate them: 9164 * To find the correct nodeid, do the following: 9165 * For current node, get nodeid from dnlc. 9166 * - if current node is rootvp, set pnodeid to nodeid. 9167 * - else if parent is in the dnlc, get its nodeid from there. 9168 * - else add LOOKUPP+GETATTR to compound. 9169 */ 9170 nodeid = rp->r_attr.va_nodeid; 9171 if (vp->v_flag & VROOT) { 9172 pnodeid = nodeid; /* root of mount point */ 9173 } else { 9174 dvp = dnlc_lookup(vp, ".."); 9175 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9176 /* parent in dnlc cache - no need for otw */ 9177 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9178 } else { 9179 /* 9180 * parent not in dnlc cache, 9181 * do lookupp to get its id 9182 */ 9183 num_ops = 5; 9184 pnodeid = 0; /* set later by getattr parent */ 9185 } 9186 if (dvp) 9187 VN_RELE(dvp); 9188 } 9189 } 9190 recov_state.rs_flags = 0; 9191 recov_state.rs_num_retry_despite_err = 0; 9192 9193 /* Save the original mount point security flavor */ 9194 (void) save_mnt_secinfo(mi->mi_curr_serv); 9195 9196 recov_retry: 9197 args.ctag = TAG_READDIR; 9198 9199 args.array = argop; 9200 args.array_len = num_ops; 9201 9202 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9203 &recov_state, NULL)) { 9204 /* 9205 * If readdir a node that is a stub for a crossed mount point, 9206 * keep the original secinfo flavor for the current file 9207 * system, not the crossed one. 9208 */ 9209 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9210 rdc->error = e.error; 9211 return; 9212 } 9213 9214 /* 9215 * Determine which attrs to request for dirents. This code 9216 * must be protected by nfs4_start/end_fop because of r_server 9217 * (which will change during failover recovery). 9218 * 9219 */ 9220 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9221 /* 9222 * Get all vattr attrs plus filehandle and rdattr_error 9223 */ 9224 rd_bitsval = NFS4_VATTR_MASK | 9225 FATTR4_RDATTR_ERROR_MASK | 9226 FATTR4_FILEHANDLE_MASK; 9227 9228 if (rp->r_flags & R4READDIRWATTR) { 9229 mutex_enter(&rp->r_statelock); 9230 rp->r_flags &= ~R4READDIRWATTR; 9231 mutex_exit(&rp->r_statelock); 9232 } 9233 } else { 9234 servinfo4_t *svp = rp->r_server; 9235 9236 /* 9237 * Already read directory. Use readdir with 9238 * no attrs (except for mounted_on_fileid) for updates. 9239 */ 9240 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9241 9242 /* 9243 * request mounted on fileid if supported, else request 9244 * fileid. maybe we should verify that fileid is supported 9245 * and request something else if not. 9246 */ 9247 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9248 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9249 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9250 nfs_rw_exit(&svp->sv_lock); 9251 } 9252 9253 /* putfh directory fh */ 9254 argop[0].argop = OP_CPUTFH; 9255 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9256 9257 argop[1].argop = OP_READDIR; 9258 rargs = &argop[1].nfs_argop4_u.opreaddir; 9259 /* 9260 * 1 and 2 are reserved for client "." and ".." entry offset. 9261 * cookie 0 should be used over-the-wire to start reading at 9262 * the beginning of the directory excluding "." and "..". 9263 */ 9264 if (rdc->nfs4_cookie == 0 || 9265 rdc->nfs4_cookie == 1 || 9266 rdc->nfs4_cookie == 2) { 9267 rargs->cookie = (nfs_cookie4)0; 9268 rargs->cookieverf = 0; 9269 } else { 9270 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9271 mutex_enter(&rp->r_statelock); 9272 rargs->cookieverf = rp->r_cookieverf4; 9273 mutex_exit(&rp->r_statelock); 9274 } 9275 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9276 rargs->maxcount = mi->mi_tsize; 9277 rargs->attr_request = rd_bitsval; 9278 rargs->rdc = rdc; 9279 rargs->dvp = vp; 9280 rargs->mi = mi; 9281 rargs->cr = cr; 9282 9283 9284 /* 9285 * If count < than the minimum required, we return no entries 9286 * and fail with EINVAL 9287 */ 9288 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9289 rdc->error = EINVAL; 9290 goto out; 9291 } 9292 9293 if (args.array_len == 5) { 9294 /* 9295 * Add lookupp and getattr for parent nodeid. 9296 */ 9297 argop[2].argop = OP_LOOKUPP; 9298 9299 argop[3].argop = OP_GETFH; 9300 9301 /* getattr parent */ 9302 argop[4].argop = OP_GETATTR; 9303 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9304 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9305 } 9306 9307 doqueue = 1; 9308 9309 if (mi->mi_io_kstats) { 9310 mutex_enter(&mi->mi_lock); 9311 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9312 mutex_exit(&mi->mi_lock); 9313 } 9314 9315 /* capture the time of this call */ 9316 rargs->t = t = gethrtime(); 9317 9318 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9319 9320 if (mi->mi_io_kstats) { 9321 mutex_enter(&mi->mi_lock); 9322 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9323 mutex_exit(&mi->mi_lock); 9324 } 9325 9326 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9327 9328 /* 9329 * If RPC error occurred and it isn't an error that 9330 * triggers recovery, then go ahead and fail now. 9331 */ 9332 if (e.error != 0 && !needrecov) { 9333 rdc->error = e.error; 9334 goto out; 9335 } 9336 9337 if (needrecov) { 9338 bool_t abort; 9339 9340 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9341 "nfs4readdir: initiating recovery.\n")); 9342 9343 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9344 NULL, OP_READDIR, NULL, NULL, NULL); 9345 if (abort == FALSE) { 9346 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9347 &recov_state, needrecov); 9348 if (!e.error) 9349 (void) xdr_free(xdr_COMPOUND4res_clnt, 9350 (caddr_t)&res); 9351 if (rdc->entries != NULL) { 9352 kmem_free(rdc->entries, rdc->entlen); 9353 rdc->entries = NULL; 9354 } 9355 goto recov_retry; 9356 } 9357 9358 if (e.error != 0) { 9359 rdc->error = e.error; 9360 goto out; 9361 } 9362 9363 /* fall through for res.status case */ 9364 } 9365 9366 res_opcnt = res.array_len; 9367 9368 /* 9369 * If compound failed first 2 ops (PUTFH+READDIR), then return 9370 * failure here. Subsequent ops are for filling out dot-dot 9371 * dirent, and if they fail, we still want to give the caller 9372 * the dirents returned by (the successful) READDIR op, so we need 9373 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9374 * 9375 * One example where PUTFH+READDIR ops would succeed but 9376 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9377 * but lacks x. In this case, a POSIX server's VOP_READDIR 9378 * would succeed; however, VOP_LOOKUP(..) would fail since no 9379 * x perm. We need to come up with a non-vendor-specific way 9380 * for a POSIX server to return d_ino from dotdot's dirent if 9381 * client only requests mounted_on_fileid, and just say the 9382 * LOOKUPP succeeded and fill out the GETATTR. However, if 9383 * client requested any mandatory attrs, server would be required 9384 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9385 * for dotdot. 9386 */ 9387 9388 if (res.status) { 9389 if (res_opcnt <= 2) { 9390 e.error = geterrno4(res.status); 9391 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9392 &recov_state, needrecov); 9393 nfs4_purge_stale_fh(e.error, vp, cr); 9394 rdc->error = e.error; 9395 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9396 if (rdc->entries != NULL) { 9397 kmem_free(rdc->entries, rdc->entlen); 9398 rdc->entries = NULL; 9399 } 9400 /* 9401 * If readdir a node that is a stub for a 9402 * crossed mount point, keep the original 9403 * secinfo flavor for the current file system, 9404 * not the crossed one. 9405 */ 9406 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9407 return; 9408 } 9409 } 9410 9411 resop = &res.array[1]; /* readdir res */ 9412 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9413 9414 mutex_enter(&rp->r_statelock); 9415 rp->r_cookieverf4 = rd_res->cookieverf; 9416 mutex_exit(&rp->r_statelock); 9417 9418 /* 9419 * For "." and ".." entries 9420 * e.g. 9421 * seek(cookie=0) -> "." entry with d_off = 1 9422 * seek(cookie=1) -> ".." entry with d_off = 2 9423 */ 9424 if (cookie == (nfs_cookie4) 0) { 9425 if (rd_res->dotp) 9426 rd_res->dotp->d_ino = nodeid; 9427 if (rd_res->dotdotp) 9428 rd_res->dotdotp->d_ino = pnodeid; 9429 } 9430 if (cookie == (nfs_cookie4) 1) { 9431 if (rd_res->dotdotp) 9432 rd_res->dotdotp->d_ino = pnodeid; 9433 } 9434 9435 9436 /* LOOKUPP+GETATTR attemped */ 9437 if (args.array_len == 5 && rd_res->dotdotp) { 9438 if (res.status == NFS4_OK && res_opcnt == 5) { 9439 nfs_fh4 *fhp; 9440 nfs4_sharedfh_t *sfhp; 9441 vnode_t *pvp; 9442 nfs4_ga_res_t *garp; 9443 9444 resop++; /* lookupp */ 9445 resop++; /* getfh */ 9446 fhp = &resop->nfs_resop4_u.opgetfh.object; 9447 9448 resop++; /* getattr of parent */ 9449 9450 /* 9451 * First, take care of finishing the 9452 * readdir results. 9453 */ 9454 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9455 /* 9456 * The d_ino of .. must be the inode number 9457 * of the mounted filesystem. 9458 */ 9459 if (garp->n4g_va.va_mask & AT_NODEID) 9460 rd_res->dotdotp->d_ino = 9461 garp->n4g_va.va_nodeid; 9462 9463 9464 /* 9465 * Next, create the ".." dnlc entry 9466 */ 9467 sfhp = sfh4_get(fhp, mi); 9468 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9469 dnlc_update(vp, "..", pvp); 9470 VN_RELE(pvp); 9471 } 9472 sfh4_rele(&sfhp); 9473 } 9474 } 9475 9476 if (mi->mi_io_kstats) { 9477 mutex_enter(&mi->mi_lock); 9478 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9479 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9480 mutex_exit(&mi->mi_lock); 9481 } 9482 9483 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9484 9485 out: 9486 /* 9487 * If readdir a node that is a stub for a crossed mount point, 9488 * keep the original secinfo flavor for the current file system, 9489 * not the crossed one. 9490 */ 9491 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9492 9493 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9494 } 9495 9496 9497 static int 9498 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9499 { 9500 rnode4_t *rp = VTOR4(bp->b_vp); 9501 int count; 9502 int error; 9503 cred_t *cred_otw = NULL; 9504 offset_t offset; 9505 nfs4_open_stream_t *osp = NULL; 9506 bool_t first_time = TRUE; /* first time getting otw cred */ 9507 bool_t last_time = FALSE; /* last time getting otw cred */ 9508 9509 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9510 9511 DTRACE_IO1(start, struct buf *, bp); 9512 offset = ldbtob(bp->b_lblkno); 9513 9514 if (bp->b_flags & B_READ) { 9515 read_again: 9516 /* 9517 * Releases the osp, if it is provided. 9518 * Puts a hold on the cred_otw and the new osp (if found). 9519 */ 9520 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9521 &first_time, &last_time); 9522 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9523 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9524 readahead, NULL); 9525 crfree(cred_otw); 9526 if (!error) { 9527 if (bp->b_resid) { 9528 /* 9529 * Didn't get it all because we hit EOF, 9530 * zero all the memory beyond the EOF. 9531 */ 9532 /* bzero(rdaddr + */ 9533 bzero(bp->b_un.b_addr + 9534 bp->b_bcount - bp->b_resid, bp->b_resid); 9535 } 9536 mutex_enter(&rp->r_statelock); 9537 if (bp->b_resid == bp->b_bcount && 9538 offset >= rp->r_size) { 9539 /* 9540 * We didn't read anything at all as we are 9541 * past EOF. Return an error indicator back 9542 * but don't destroy the pages (yet). 9543 */ 9544 error = NFS_EOF; 9545 } 9546 mutex_exit(&rp->r_statelock); 9547 } else if (error == EACCES && last_time == FALSE) { 9548 goto read_again; 9549 } 9550 } else { 9551 if (!(rp->r_flags & R4STALE)) { 9552 write_again: 9553 /* 9554 * Releases the osp, if it is provided. 9555 * Puts a hold on the cred_otw and the new 9556 * osp (if found). 9557 */ 9558 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9559 &first_time, &last_time); 9560 mutex_enter(&rp->r_statelock); 9561 count = MIN(bp->b_bcount, rp->r_size - offset); 9562 mutex_exit(&rp->r_statelock); 9563 if (count < 0) 9564 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9565 #ifdef DEBUG 9566 if (count == 0) { 9567 zoneid_t zoneid = getzoneid(); 9568 9569 zcmn_err(zoneid, CE_WARN, 9570 "nfs4_bio: zero length write at %lld", 9571 offset); 9572 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9573 "b_bcount=%ld, file size=%lld", 9574 rp->r_flags, (long)bp->b_bcount, 9575 rp->r_size); 9576 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9577 if (nfs4_bio_do_stop) 9578 debug_enter("nfs4_bio"); 9579 } 9580 #endif 9581 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9582 count, cred_otw, stab_comm); 9583 if (error == EACCES && last_time == FALSE) { 9584 crfree(cred_otw); 9585 goto write_again; 9586 } 9587 bp->b_error = error; 9588 if (error && error != EINTR && 9589 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9590 /* 9591 * Don't print EDQUOT errors on the console. 9592 * Don't print asynchronous EACCES errors. 9593 * Don't print EFBIG errors. 9594 * Print all other write errors. 9595 */ 9596 if (error != EDQUOT && error != EFBIG && 9597 (error != EACCES || 9598 !(bp->b_flags & B_ASYNC))) 9599 nfs4_write_error(bp->b_vp, 9600 error, cred_otw); 9601 /* 9602 * Update r_error and r_flags as appropriate. 9603 * If the error was ESTALE, then mark the 9604 * rnode as not being writeable and save 9605 * the error status. Otherwise, save any 9606 * errors which occur from asynchronous 9607 * page invalidations. Any errors occurring 9608 * from other operations should be saved 9609 * by the caller. 9610 */ 9611 mutex_enter(&rp->r_statelock); 9612 if (error == ESTALE) { 9613 rp->r_flags |= R4STALE; 9614 if (!rp->r_error) 9615 rp->r_error = error; 9616 } else if (!rp->r_error && 9617 (bp->b_flags & 9618 (B_INVAL|B_FORCE|B_ASYNC)) == 9619 (B_INVAL|B_FORCE|B_ASYNC)) { 9620 rp->r_error = error; 9621 } 9622 mutex_exit(&rp->r_statelock); 9623 } 9624 crfree(cred_otw); 9625 } else { 9626 error = rp->r_error; 9627 /* 9628 * A close may have cleared r_error, if so, 9629 * propagate ESTALE error return properly 9630 */ 9631 if (error == 0) 9632 error = ESTALE; 9633 } 9634 } 9635 9636 if (error != 0 && error != NFS_EOF) 9637 bp->b_flags |= B_ERROR; 9638 9639 if (osp) 9640 open_stream_rele(osp, rp); 9641 9642 DTRACE_IO1(done, struct buf *, bp); 9643 9644 return (error); 9645 } 9646 9647 /* ARGSUSED */ 9648 int 9649 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 9650 { 9651 return (EREMOTE); 9652 } 9653 9654 /* ARGSUSED2 */ 9655 int 9656 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9657 { 9658 rnode4_t *rp = VTOR4(vp); 9659 9660 if (!write_lock) { 9661 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9662 return (V_WRITELOCK_FALSE); 9663 } 9664 9665 if ((rp->r_flags & R4DIRECTIO) || 9666 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9667 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9668 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9669 return (V_WRITELOCK_FALSE); 9670 nfs_rw_exit(&rp->r_rwlock); 9671 } 9672 9673 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9674 return (V_WRITELOCK_TRUE); 9675 } 9676 9677 /* ARGSUSED */ 9678 void 9679 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9680 { 9681 rnode4_t *rp = VTOR4(vp); 9682 9683 nfs_rw_exit(&rp->r_rwlock); 9684 } 9685 9686 /* ARGSUSED */ 9687 static int 9688 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 9689 { 9690 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9691 return (EIO); 9692 9693 /* 9694 * Because we stuff the readdir cookie into the offset field 9695 * someone may attempt to do an lseek with the cookie which 9696 * we want to succeed. 9697 */ 9698 if (vp->v_type == VDIR) 9699 return (0); 9700 if (*noffp < 0) 9701 return (EINVAL); 9702 return (0); 9703 } 9704 9705 9706 /* 9707 * Return all the pages from [off..off+len) in file 9708 */ 9709 /* ARGSUSED */ 9710 static int 9711 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9712 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9713 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 9714 { 9715 rnode4_t *rp; 9716 int error; 9717 mntinfo4_t *mi; 9718 9719 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9720 return (EIO); 9721 rp = VTOR4(vp); 9722 if (IS_SHADOW(vp, rp)) 9723 vp = RTOV4(rp); 9724 9725 if (vp->v_flag & VNOMAP) 9726 return (ENOSYS); 9727 9728 if (protp != NULL) 9729 *protp = PROT_ALL; 9730 9731 /* 9732 * Now validate that the caches are up to date. 9733 */ 9734 if (error = nfs4_validate_caches(vp, cr)) 9735 return (error); 9736 9737 mi = VTOMI4(vp); 9738 retry: 9739 mutex_enter(&rp->r_statelock); 9740 9741 /* 9742 * Don't create dirty pages faster than they 9743 * can be cleaned so that the system doesn't 9744 * get imbalanced. If the async queue is 9745 * maxed out, then wait for it to drain before 9746 * creating more dirty pages. Also, wait for 9747 * any threads doing pagewalks in the vop_getattr 9748 * entry points so that they don't block for 9749 * long periods. 9750 */ 9751 if (rw == S_CREATE) { 9752 while ((mi->mi_max_threads != 0 && 9753 rp->r_awcount > 2 * mi->mi_max_threads) || 9754 rp->r_gcount > 0) 9755 cv_wait(&rp->r_cv, &rp->r_statelock); 9756 } 9757 9758 /* 9759 * If we are getting called as a side effect of an nfs_write() 9760 * operation the local file size might not be extended yet. 9761 * In this case we want to be able to return pages of zeroes. 9762 */ 9763 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9764 NFS4_DEBUG(nfs4_pageio_debug, 9765 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9766 "len=%llu, size=%llu, attrsize =%llu", off, 9767 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9768 mutex_exit(&rp->r_statelock); 9769 return (EFAULT); /* beyond EOF */ 9770 } 9771 9772 mutex_exit(&rp->r_statelock); 9773 9774 if (len <= PAGESIZE) { 9775 error = nfs4_getapage(vp, off, len, protp, pl, plsz, 9776 seg, addr, rw, cr); 9777 NFS4_DEBUG(nfs4_pageio_debug && error, 9778 (CE_NOTE, "getpage error %d; off=%lld, " 9779 "len=%lld", error, off, (u_longlong_t)len)); 9780 } else { 9781 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9782 pl, plsz, seg, addr, rw, cr); 9783 NFS4_DEBUG(nfs4_pageio_debug && error, 9784 (CE_NOTE, "getpages error %d; off=%lld, " 9785 "len=%lld", error, off, (u_longlong_t)len)); 9786 } 9787 9788 switch (error) { 9789 case NFS_EOF: 9790 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9791 goto retry; 9792 case ESTALE: 9793 nfs4_purge_stale_fh(error, vp, cr); 9794 } 9795 9796 return (error); 9797 } 9798 9799 /* 9800 * Called from pvn_getpages or nfs4_getpage to get a particular page. 9801 */ 9802 /* ARGSUSED */ 9803 static int 9804 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9805 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9806 enum seg_rw rw, cred_t *cr) 9807 { 9808 rnode4_t *rp; 9809 uint_t bsize; 9810 struct buf *bp; 9811 page_t *pp; 9812 u_offset_t lbn; 9813 u_offset_t io_off; 9814 u_offset_t blkoff; 9815 u_offset_t rablkoff; 9816 size_t io_len; 9817 uint_t blksize; 9818 int error; 9819 int readahead; 9820 int readahead_issued = 0; 9821 int ra_window; /* readahead window */ 9822 page_t *pagefound; 9823 page_t *savepp; 9824 9825 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9826 return (EIO); 9827 9828 rp = VTOR4(vp); 9829 ASSERT(!IS_SHADOW(vp, rp)); 9830 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9831 9832 reread: 9833 bp = NULL; 9834 pp = NULL; 9835 pagefound = NULL; 9836 9837 if (pl != NULL) 9838 pl[0] = NULL; 9839 9840 error = 0; 9841 lbn = off / bsize; 9842 blkoff = lbn * bsize; 9843 9844 /* 9845 * Queueing up the readahead before doing the synchronous read 9846 * results in a significant increase in read throughput because 9847 * of the increased parallelism between the async threads and 9848 * the process context. 9849 */ 9850 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9851 rw != S_CREATE && 9852 !(vp->v_flag & VNOCACHE)) { 9853 mutex_enter(&rp->r_statelock); 9854 9855 /* 9856 * Calculate the number of readaheads to do. 9857 * a) No readaheads at offset = 0. 9858 * b) Do maximum(nfs4_nra) readaheads when the readahead 9859 * window is closed. 9860 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9861 * upon how far the readahead window is open or close. 9862 * d) No readaheads if rp->r_nextr is not within the scope 9863 * of the readahead window (random i/o). 9864 */ 9865 9866 if (off == 0) 9867 readahead = 0; 9868 else if (blkoff == rp->r_nextr) 9869 readahead = nfs4_nra; 9870 else if (rp->r_nextr > blkoff && 9871 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9872 <= (nfs4_nra - 1))) 9873 readahead = nfs4_nra - ra_window; 9874 else 9875 readahead = 0; 9876 9877 rablkoff = rp->r_nextr; 9878 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9879 mutex_exit(&rp->r_statelock); 9880 if (nfs4_async_readahead(vp, rablkoff + bsize, 9881 addr + (rablkoff + bsize - off), 9882 seg, cr, nfs4_readahead) < 0) { 9883 mutex_enter(&rp->r_statelock); 9884 break; 9885 } 9886 readahead--; 9887 rablkoff += bsize; 9888 /* 9889 * Indicate that we did a readahead so 9890 * readahead offset is not updated 9891 * by the synchronous read below. 9892 */ 9893 readahead_issued = 1; 9894 mutex_enter(&rp->r_statelock); 9895 /* 9896 * set readahead offset to 9897 * offset of last async readahead 9898 * request. 9899 */ 9900 rp->r_nextr = rablkoff; 9901 } 9902 mutex_exit(&rp->r_statelock); 9903 } 9904 9905 again: 9906 if ((pagefound = page_exists(vp, off)) == NULL) { 9907 if (pl == NULL) { 9908 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9909 nfs4_readahead); 9910 } else if (rw == S_CREATE) { 9911 /* 9912 * Block for this page is not allocated, or the offset 9913 * is beyond the current allocation size, or we're 9914 * allocating a swap slot and the page was not found, 9915 * so allocate it and return a zero page. 9916 */ 9917 if ((pp = page_create_va(vp, off, 9918 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9919 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9920 io_len = PAGESIZE; 9921 mutex_enter(&rp->r_statelock); 9922 rp->r_nextr = off + PAGESIZE; 9923 mutex_exit(&rp->r_statelock); 9924 } else { 9925 /* 9926 * Need to go to server to get a block 9927 */ 9928 mutex_enter(&rp->r_statelock); 9929 if (blkoff < rp->r_size && 9930 blkoff + bsize > rp->r_size) { 9931 /* 9932 * If less than a block left in 9933 * file read less than a block. 9934 */ 9935 if (rp->r_size <= off) { 9936 /* 9937 * Trying to access beyond EOF, 9938 * set up to get at least one page. 9939 */ 9940 blksize = off + PAGESIZE - blkoff; 9941 } else 9942 blksize = rp->r_size - blkoff; 9943 } else if ((off == 0) || 9944 (off != rp->r_nextr && !readahead_issued)) { 9945 blksize = PAGESIZE; 9946 blkoff = off; /* block = page here */ 9947 } else 9948 blksize = bsize; 9949 mutex_exit(&rp->r_statelock); 9950 9951 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9952 &io_len, blkoff, blksize, 0); 9953 9954 /* 9955 * Some other thread has entered the page, 9956 * so just use it. 9957 */ 9958 if (pp == NULL) 9959 goto again; 9960 9961 /* 9962 * Now round the request size up to page boundaries. 9963 * This ensures that the entire page will be 9964 * initialized to zeroes if EOF is encountered. 9965 */ 9966 io_len = ptob(btopr(io_len)); 9967 9968 bp = pageio_setup(pp, io_len, vp, B_READ); 9969 ASSERT(bp != NULL); 9970 9971 /* 9972 * pageio_setup should have set b_addr to 0. This 9973 * is correct since we want to do I/O on a page 9974 * boundary. bp_mapin will use this addr to calculate 9975 * an offset, and then set b_addr to the kernel virtual 9976 * address it allocated for us. 9977 */ 9978 ASSERT(bp->b_un.b_addr == 0); 9979 9980 bp->b_edev = 0; 9981 bp->b_dev = 0; 9982 bp->b_lblkno = lbtodb(io_off); 9983 bp->b_file = vp; 9984 bp->b_offset = (offset_t)off; 9985 bp_mapin(bp); 9986 9987 /* 9988 * If doing a write beyond what we believe is EOF, 9989 * don't bother trying to read the pages from the 9990 * server, we'll just zero the pages here. We 9991 * don't check that the rw flag is S_WRITE here 9992 * because some implementations may attempt a 9993 * read access to the buffer before copying data. 9994 */ 9995 mutex_enter(&rp->r_statelock); 9996 if (io_off >= rp->r_size && seg == segkmap) { 9997 mutex_exit(&rp->r_statelock); 9998 bzero(bp->b_un.b_addr, io_len); 9999 } else { 10000 mutex_exit(&rp->r_statelock); 10001 error = nfs4_bio(bp, NULL, cr, FALSE); 10002 } 10003 10004 /* 10005 * Unmap the buffer before freeing it. 10006 */ 10007 bp_mapout(bp); 10008 pageio_done(bp); 10009 10010 savepp = pp; 10011 do { 10012 pp->p_fsdata = C_NOCOMMIT; 10013 } while ((pp = pp->p_next) != savepp); 10014 10015 if (error == NFS_EOF) { 10016 /* 10017 * If doing a write system call just return 10018 * zeroed pages, else user tried to get pages 10019 * beyond EOF, return error. We don't check 10020 * that the rw flag is S_WRITE here because 10021 * some implementations may attempt a read 10022 * access to the buffer before copying data. 10023 */ 10024 if (seg == segkmap) 10025 error = 0; 10026 else 10027 error = EFAULT; 10028 } 10029 10030 if (!readahead_issued && !error) { 10031 mutex_enter(&rp->r_statelock); 10032 rp->r_nextr = io_off + io_len; 10033 mutex_exit(&rp->r_statelock); 10034 } 10035 } 10036 } 10037 10038 out: 10039 if (pl == NULL) 10040 return (error); 10041 10042 if (error) { 10043 if (pp != NULL) 10044 pvn_read_done(pp, B_ERROR); 10045 return (error); 10046 } 10047 10048 if (pagefound) { 10049 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 10050 10051 /* 10052 * Page exists in the cache, acquire the appropriate lock. 10053 * If this fails, start all over again. 10054 */ 10055 if ((pp = page_lookup(vp, off, se)) == NULL) { 10056 #ifdef DEBUG 10057 nfs4_lostpage++; 10058 #endif 10059 goto reread; 10060 } 10061 pl[0] = pp; 10062 pl[1] = NULL; 10063 return (0); 10064 } 10065 10066 if (pp != NULL) 10067 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 10068 10069 return (error); 10070 } 10071 10072 static void 10073 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 10074 cred_t *cr) 10075 { 10076 int error; 10077 page_t *pp; 10078 u_offset_t io_off; 10079 size_t io_len; 10080 struct buf *bp; 10081 uint_t bsize, blksize; 10082 rnode4_t *rp = VTOR4(vp); 10083 page_t *savepp; 10084 10085 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10086 10087 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10088 10089 mutex_enter(&rp->r_statelock); 10090 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 10091 /* 10092 * If less than a block left in file read less 10093 * than a block. 10094 */ 10095 blksize = rp->r_size - blkoff; 10096 } else 10097 blksize = bsize; 10098 mutex_exit(&rp->r_statelock); 10099 10100 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 10101 &io_off, &io_len, blkoff, blksize, 1); 10102 /* 10103 * The isra flag passed to the kluster function is 1, we may have 10104 * gotten a return value of NULL for a variety of reasons (# of free 10105 * pages < minfree, someone entered the page on the vnode etc). In all 10106 * cases, we want to punt on the readahead. 10107 */ 10108 if (pp == NULL) 10109 return; 10110 10111 /* 10112 * Now round the request size up to page boundaries. 10113 * This ensures that the entire page will be 10114 * initialized to zeroes if EOF is encountered. 10115 */ 10116 io_len = ptob(btopr(io_len)); 10117 10118 bp = pageio_setup(pp, io_len, vp, B_READ); 10119 ASSERT(bp != NULL); 10120 10121 /* 10122 * pageio_setup should have set b_addr to 0. This is correct since 10123 * we want to do I/O on a page boundary. bp_mapin() will use this addr 10124 * to calculate an offset, and then set b_addr to the kernel virtual 10125 * address it allocated for us. 10126 */ 10127 ASSERT(bp->b_un.b_addr == 0); 10128 10129 bp->b_edev = 0; 10130 bp->b_dev = 0; 10131 bp->b_lblkno = lbtodb(io_off); 10132 bp->b_file = vp; 10133 bp->b_offset = (offset_t)blkoff; 10134 bp_mapin(bp); 10135 10136 /* 10137 * If doing a write beyond what we believe is EOF, don't bother trying 10138 * to read the pages from the server, we'll just zero the pages here. 10139 * We don't check that the rw flag is S_WRITE here because some 10140 * implementations may attempt a read access to the buffer before 10141 * copying data. 10142 */ 10143 mutex_enter(&rp->r_statelock); 10144 if (io_off >= rp->r_size && seg == segkmap) { 10145 mutex_exit(&rp->r_statelock); 10146 bzero(bp->b_un.b_addr, io_len); 10147 error = 0; 10148 } else { 10149 mutex_exit(&rp->r_statelock); 10150 error = nfs4_bio(bp, NULL, cr, TRUE); 10151 if (error == NFS_EOF) 10152 error = 0; 10153 } 10154 10155 /* 10156 * Unmap the buffer before freeing it. 10157 */ 10158 bp_mapout(bp); 10159 pageio_done(bp); 10160 10161 savepp = pp; 10162 do { 10163 pp->p_fsdata = C_NOCOMMIT; 10164 } while ((pp = pp->p_next) != savepp); 10165 10166 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10167 10168 /* 10169 * In case of error set readahead offset 10170 * to the lowest offset. 10171 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10172 */ 10173 if (error && rp->r_nextr > io_off) { 10174 mutex_enter(&rp->r_statelock); 10175 if (rp->r_nextr > io_off) 10176 rp->r_nextr = io_off; 10177 mutex_exit(&rp->r_statelock); 10178 } 10179 } 10180 10181 /* 10182 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10183 * If len == 0, do from off to EOF. 10184 * 10185 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10186 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10187 * (from pageout). 10188 */ 10189 /* ARGSUSED */ 10190 static int 10191 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 10192 caller_context_t *ct) 10193 { 10194 int error; 10195 rnode4_t *rp; 10196 10197 ASSERT(cr != NULL); 10198 10199 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10200 return (EIO); 10201 10202 rp = VTOR4(vp); 10203 if (IS_SHADOW(vp, rp)) 10204 vp = RTOV4(rp); 10205 10206 /* 10207 * XXX - Why should this check be made here? 10208 */ 10209 if (vp->v_flag & VNOMAP) 10210 return (ENOSYS); 10211 10212 if (len == 0 && !(flags & B_INVAL) && 10213 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10214 return (0); 10215 10216 mutex_enter(&rp->r_statelock); 10217 rp->r_count++; 10218 mutex_exit(&rp->r_statelock); 10219 error = nfs4_putpages(vp, off, len, flags, cr); 10220 mutex_enter(&rp->r_statelock); 10221 rp->r_count--; 10222 cv_broadcast(&rp->r_cv); 10223 mutex_exit(&rp->r_statelock); 10224 10225 return (error); 10226 } 10227 10228 /* 10229 * Write out a single page, possibly klustering adjacent dirty pages. 10230 */ 10231 int 10232 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10233 int flags, cred_t *cr) 10234 { 10235 u_offset_t io_off; 10236 u_offset_t lbn_off; 10237 u_offset_t lbn; 10238 size_t io_len; 10239 uint_t bsize; 10240 int error; 10241 rnode4_t *rp; 10242 10243 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10244 ASSERT(pp != NULL); 10245 ASSERT(cr != NULL); 10246 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10247 10248 rp = VTOR4(vp); 10249 ASSERT(rp->r_count > 0); 10250 ASSERT(!IS_SHADOW(vp, rp)); 10251 10252 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10253 lbn = pp->p_offset / bsize; 10254 lbn_off = lbn * bsize; 10255 10256 /* 10257 * Find a kluster that fits in one block, or in 10258 * one page if pages are bigger than blocks. If 10259 * there is less file space allocated than a whole 10260 * page, we'll shorten the i/o request below. 10261 */ 10262 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10263 roundup(bsize, PAGESIZE), flags); 10264 10265 /* 10266 * pvn_write_kluster shouldn't have returned a page with offset 10267 * behind the original page we were given. Verify that. 10268 */ 10269 ASSERT((pp->p_offset / bsize) >= lbn); 10270 10271 /* 10272 * Now pp will have the list of kept dirty pages marked for 10273 * write back. It will also handle invalidation and freeing 10274 * of pages that are not dirty. Check for page length rounding 10275 * problems. 10276 */ 10277 if (io_off + io_len > lbn_off + bsize) { 10278 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10279 io_len = lbn_off + bsize - io_off; 10280 } 10281 /* 10282 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10283 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10284 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10285 * progress and the r_size has not been made consistent with the 10286 * new size of the file. When the uiomove() completes the r_size is 10287 * updated and the R4MODINPROGRESS flag is cleared. 10288 * 10289 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10290 * consistent value of r_size. Without this handshaking, it is 10291 * possible that nfs4_bio() picks up the old value of r_size 10292 * before the uiomove() in writerp4() completes. This will result 10293 * in the write through nfs4_bio() being dropped. 10294 * 10295 * More precisely, there is a window between the time the uiomove() 10296 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10297 * operation intervenes in this window, the page will be picked up, 10298 * because it is dirty (it will be unlocked, unless it was 10299 * pagecreate'd). When the page is picked up as dirty, the dirty 10300 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10301 * checked. This will still be the old size. Therefore the page will 10302 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10303 * the page will be found to be clean and the write will be dropped. 10304 */ 10305 if (rp->r_flags & R4MODINPROGRESS) { 10306 mutex_enter(&rp->r_statelock); 10307 if ((rp->r_flags & R4MODINPROGRESS) && 10308 rp->r_modaddr + MAXBSIZE > io_off && 10309 rp->r_modaddr < io_off + io_len) { 10310 page_t *plist; 10311 /* 10312 * A write is in progress for this region of the file. 10313 * If we did not detect R4MODINPROGRESS here then this 10314 * path through nfs_putapage() would eventually go to 10315 * nfs4_bio() and may not write out all of the data 10316 * in the pages. We end up losing data. So we decide 10317 * to set the modified bit on each page in the page 10318 * list and mark the rnode with R4DIRTY. This write 10319 * will be restarted at some later time. 10320 */ 10321 plist = pp; 10322 while (plist != NULL) { 10323 pp = plist; 10324 page_sub(&plist, pp); 10325 hat_setmod(pp); 10326 page_io_unlock(pp); 10327 page_unlock(pp); 10328 } 10329 rp->r_flags |= R4DIRTY; 10330 mutex_exit(&rp->r_statelock); 10331 if (offp) 10332 *offp = io_off; 10333 if (lenp) 10334 *lenp = io_len; 10335 return (0); 10336 } 10337 mutex_exit(&rp->r_statelock); 10338 } 10339 10340 if (flags & B_ASYNC) { 10341 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10342 nfs4_sync_putapage); 10343 } else 10344 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10345 10346 if (offp) 10347 *offp = io_off; 10348 if (lenp) 10349 *lenp = io_len; 10350 return (error); 10351 } 10352 10353 static int 10354 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10355 int flags, cred_t *cr) 10356 { 10357 int error; 10358 rnode4_t *rp; 10359 10360 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10361 10362 flags |= B_WRITE; 10363 10364 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10365 10366 rp = VTOR4(vp); 10367 10368 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10369 error == EACCES) && 10370 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10371 if (!(rp->r_flags & R4OUTOFSPACE)) { 10372 mutex_enter(&rp->r_statelock); 10373 rp->r_flags |= R4OUTOFSPACE; 10374 mutex_exit(&rp->r_statelock); 10375 } 10376 flags |= B_ERROR; 10377 pvn_write_done(pp, flags); 10378 /* 10379 * If this was not an async thread, then try again to 10380 * write out the pages, but this time, also destroy 10381 * them whether or not the write is successful. This 10382 * will prevent memory from filling up with these 10383 * pages and destroying them is the only alternative 10384 * if they can't be written out. 10385 * 10386 * Don't do this if this is an async thread because 10387 * when the pages are unlocked in pvn_write_done, 10388 * some other thread could have come along, locked 10389 * them, and queued for an async thread. It would be 10390 * possible for all of the async threads to be tied 10391 * up waiting to lock the pages again and they would 10392 * all already be locked and waiting for an async 10393 * thread to handle them. Deadlock. 10394 */ 10395 if (!(flags & B_ASYNC)) { 10396 error = nfs4_putpage(vp, io_off, io_len, 10397 B_INVAL | B_FORCE, cr, NULL); 10398 } 10399 } else { 10400 if (error) 10401 flags |= B_ERROR; 10402 else if (rp->r_flags & R4OUTOFSPACE) { 10403 mutex_enter(&rp->r_statelock); 10404 rp->r_flags &= ~R4OUTOFSPACE; 10405 mutex_exit(&rp->r_statelock); 10406 } 10407 pvn_write_done(pp, flags); 10408 if (freemem < desfree) 10409 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10410 NFS4_WRITE_NOWAIT); 10411 } 10412 10413 return (error); 10414 } 10415 10416 #ifdef DEBUG 10417 int nfs4_force_open_before_mmap = 0; 10418 #endif 10419 10420 /* ARGSUSED */ 10421 static int 10422 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10423 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10424 caller_context_t *ct) 10425 { 10426 struct segvn_crargs vn_a; 10427 int error = 0; 10428 rnode4_t *rp = VTOR4(vp); 10429 mntinfo4_t *mi = VTOMI4(vp); 10430 10431 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10432 return (EIO); 10433 10434 if (vp->v_flag & VNOMAP) 10435 return (ENOSYS); 10436 10437 if (off < 0 || (off + len) < 0) 10438 return (ENXIO); 10439 10440 if (vp->v_type != VREG) 10441 return (ENODEV); 10442 10443 /* 10444 * If the file is delegated to the client don't do anything. 10445 * If the file is not delegated, then validate the data cache. 10446 */ 10447 mutex_enter(&rp->r_statev4_lock); 10448 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10449 mutex_exit(&rp->r_statev4_lock); 10450 error = nfs4_validate_caches(vp, cr); 10451 if (error) 10452 return (error); 10453 } else { 10454 mutex_exit(&rp->r_statev4_lock); 10455 } 10456 10457 /* 10458 * Check to see if the vnode is currently marked as not cachable. 10459 * This means portions of the file are locked (through VOP_FRLOCK). 10460 * In this case the map request must be refused. We use 10461 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10462 * 10463 * Atomically increment r_inmap after acquiring r_rwlock. The 10464 * idea here is to acquire r_rwlock to block read/write and 10465 * not to protect r_inmap. r_inmap will inform nfs4_read/write() 10466 * that we are in nfs4_map(). Now, r_rwlock is acquired in order 10467 * and we can prevent the deadlock that would have occurred 10468 * when nfs4_addmap() would have acquired it out of order. 10469 * 10470 * Since we are not protecting r_inmap by any lock, we do not 10471 * hold any lock when we decrement it. We atomically decrement 10472 * r_inmap after we release r_lkserlock. 10473 */ 10474 10475 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp))) 10476 return (EINTR); 10477 atomic_add_int(&rp->r_inmap, 1); 10478 nfs_rw_exit(&rp->r_rwlock); 10479 10480 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) { 10481 atomic_add_int(&rp->r_inmap, -1); 10482 return (EINTR); 10483 } 10484 10485 10486 if (vp->v_flag & VNOCACHE) { 10487 error = EAGAIN; 10488 goto done; 10489 } 10490 10491 /* 10492 * Don't allow concurrent locks and mapping if mandatory locking is 10493 * enabled. 10494 */ 10495 if (flk_has_remote_locks(vp)) { 10496 struct vattr va; 10497 va.va_mask = AT_MODE; 10498 error = nfs4getattr(vp, &va, cr); 10499 if (error != 0) 10500 goto done; 10501 if (MANDLOCK(vp, va.va_mode)) { 10502 error = EAGAIN; 10503 goto done; 10504 } 10505 } 10506 10507 /* 10508 * It is possible that the rnode has a lost lock request that we 10509 * are still trying to recover, and that the request conflicts with 10510 * this map request. 10511 * 10512 * An alternative approach would be for nfs4_safemap() to consider 10513 * queued lock requests when deciding whether to set or clear 10514 * VNOCACHE. This would require the frlock code path to call 10515 * nfs4_safemap() after enqueing a lost request. 10516 */ 10517 if (nfs4_map_lost_lock_conflict(vp)) { 10518 error = EAGAIN; 10519 goto done; 10520 } 10521 10522 as_rangelock(as); 10523 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 10524 if (error != 0) { 10525 as_rangeunlock(as); 10526 goto done; 10527 } 10528 10529 if (vp->v_type == VREG) { 10530 /* 10531 * We need to retrieve the open stream 10532 */ 10533 nfs4_open_stream_t *osp = NULL; 10534 nfs4_open_owner_t *oop = NULL; 10535 10536 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10537 if (oop != NULL) { 10538 /* returns with 'os_sync_lock' held */ 10539 osp = find_open_stream(oop, rp); 10540 open_owner_rele(oop); 10541 } 10542 if (osp == NULL) { 10543 #ifdef DEBUG 10544 if (nfs4_force_open_before_mmap) { 10545 error = EIO; 10546 goto done; 10547 } 10548 #endif 10549 /* returns with 'os_sync_lock' held */ 10550 error = open_and_get_osp(vp, cr, &osp); 10551 if (osp == NULL) { 10552 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10553 "nfs4_map: we tried to OPEN the file " 10554 "but again no osp, so fail with EIO")); 10555 goto done; 10556 } 10557 } 10558 10559 if (osp->os_failed_reopen) { 10560 mutex_exit(&osp->os_sync_lock); 10561 open_stream_rele(osp, rp); 10562 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10563 "nfs4_map: os_failed_reopen set on " 10564 "osp %p, cr %p, rp %s", (void *)osp, 10565 (void *)cr, rnode4info(rp))); 10566 error = EIO; 10567 goto done; 10568 } 10569 mutex_exit(&osp->os_sync_lock); 10570 open_stream_rele(osp, rp); 10571 } 10572 10573 vn_a.vp = vp; 10574 vn_a.offset = off; 10575 vn_a.type = (flags & MAP_TYPE); 10576 vn_a.prot = (uchar_t)prot; 10577 vn_a.maxprot = (uchar_t)maxprot; 10578 vn_a.flags = (flags & ~MAP_TYPE); 10579 vn_a.cred = cr; 10580 vn_a.amp = NULL; 10581 vn_a.szc = 0; 10582 vn_a.lgrp_mem_policy_flags = 0; 10583 10584 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10585 as_rangeunlock(as); 10586 10587 done: 10588 nfs_rw_exit(&rp->r_lkserlock); 10589 atomic_add_int(&rp->r_inmap, -1); 10590 return (error); 10591 } 10592 10593 /* 10594 * We're most likely dealing with a kernel module that likes to READ 10595 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10596 * officially OPEN the file to create the necessary client state 10597 * for bookkeeping of os_mmap_read/write counts. 10598 * 10599 * Since VOP_MAP only passes in a pointer to the vnode rather than 10600 * a double pointer, we can't handle the case where nfs4open_otw() 10601 * returns a different vnode than the one passed into VOP_MAP (since 10602 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10603 * we return NULL and let nfs4_map() fail. Note: the only case where 10604 * this should happen is if the file got removed and replaced with the 10605 * same name on the server (in addition to the fact that we're trying 10606 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10607 */ 10608 static int 10609 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10610 { 10611 rnode4_t *rp, *drp; 10612 vnode_t *dvp, *open_vp; 10613 char file_name[MAXNAMELEN]; 10614 int just_created; 10615 nfs4_open_stream_t *osp; 10616 nfs4_open_owner_t *oop; 10617 int error; 10618 10619 *ospp = NULL; 10620 open_vp = map_vp; 10621 10622 rp = VTOR4(open_vp); 10623 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10624 return (error); 10625 drp = VTOR4(dvp); 10626 10627 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10628 VN_RELE(dvp); 10629 return (EINTR); 10630 } 10631 10632 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10633 nfs_rw_exit(&drp->r_rwlock); 10634 VN_RELE(dvp); 10635 return (error); 10636 } 10637 10638 mutex_enter(&rp->r_statev4_lock); 10639 if (rp->created_v4) { 10640 rp->created_v4 = 0; 10641 mutex_exit(&rp->r_statev4_lock); 10642 10643 dnlc_update(dvp, file_name, open_vp); 10644 /* This is needed so we don't bump the open ref count */ 10645 just_created = 1; 10646 } else { 10647 mutex_exit(&rp->r_statev4_lock); 10648 just_created = 0; 10649 } 10650 10651 VN_HOLD(map_vp); 10652 10653 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10654 just_created); 10655 if (error) { 10656 nfs_rw_exit(&drp->r_rwlock); 10657 VN_RELE(dvp); 10658 VN_RELE(map_vp); 10659 return (error); 10660 } 10661 10662 nfs_rw_exit(&drp->r_rwlock); 10663 VN_RELE(dvp); 10664 10665 /* 10666 * If nfs4open_otw() returned a different vnode then "undo" 10667 * the open and return failure to the caller. 10668 */ 10669 if (!VN_CMP(open_vp, map_vp)) { 10670 nfs4_error_t e; 10671 10672 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10673 "open returned a different vnode")); 10674 /* 10675 * If there's an error, ignore it, 10676 * and let VOP_INACTIVE handle it. 10677 */ 10678 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10679 CLOSE_NORM, 0, 0, 0); 10680 VN_RELE(map_vp); 10681 return (EIO); 10682 } 10683 10684 VN_RELE(map_vp); 10685 10686 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10687 if (!oop) { 10688 nfs4_error_t e; 10689 10690 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10691 "no open owner")); 10692 /* 10693 * If there's an error, ignore it, 10694 * and let VOP_INACTIVE handle it. 10695 */ 10696 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10697 CLOSE_NORM, 0, 0, 0); 10698 return (EIO); 10699 } 10700 osp = find_open_stream(oop, rp); 10701 open_owner_rele(oop); 10702 *ospp = osp; 10703 return (0); 10704 } 10705 10706 /* 10707 * Please be aware that when this function is called, the address space write 10708 * a_lock is held. Do not put over the wire calls in this function. 10709 */ 10710 /* ARGSUSED */ 10711 static int 10712 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10713 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10714 caller_context_t *ct) 10715 { 10716 rnode4_t *rp; 10717 int error = 0; 10718 mntinfo4_t *mi; 10719 10720 mi = VTOMI4(vp); 10721 rp = VTOR4(vp); 10722 10723 if (nfs_zone() != mi->mi_zone) 10724 return (EIO); 10725 if (vp->v_flag & VNOMAP) 10726 return (ENOSYS); 10727 10728 /* 10729 * Don't need to update the open stream first, since this 10730 * mmap can't add any additional share access that isn't 10731 * already contained in the open stream (for the case where we 10732 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10733 * take into account os_mmap_read[write] counts). 10734 */ 10735 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10736 10737 if (vp->v_type == VREG) { 10738 /* 10739 * We need to retrieve the open stream and update the counts. 10740 * If there is no open stream here, something is wrong. 10741 */ 10742 nfs4_open_stream_t *osp = NULL; 10743 nfs4_open_owner_t *oop = NULL; 10744 10745 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10746 if (oop != NULL) { 10747 /* returns with 'os_sync_lock' held */ 10748 osp = find_open_stream(oop, rp); 10749 open_owner_rele(oop); 10750 } 10751 if (osp == NULL) { 10752 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10753 "nfs4_addmap: we should have an osp" 10754 "but we don't, so fail with EIO")); 10755 error = EIO; 10756 goto out; 10757 } 10758 10759 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10760 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10761 10762 /* 10763 * Update the map count in the open stream. 10764 * This is necessary in the case where we 10765 * open/mmap/close/, then the server reboots, and we 10766 * attempt to reopen. If the mmap doesn't add share 10767 * access then we send an invalid reopen with 10768 * access = NONE. 10769 * 10770 * We need to specifically check each PROT_* so a mmap 10771 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10772 * read and write access. A simple comparison of prot 10773 * to ~PROT_WRITE to determine read access is insufficient 10774 * since prot can be |= with PROT_USER, etc. 10775 */ 10776 10777 /* 10778 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10779 */ 10780 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10781 osp->os_mmap_write += btopr(len); 10782 if (maxprot & PROT_READ) 10783 osp->os_mmap_read += btopr(len); 10784 if (maxprot & PROT_EXEC) 10785 osp->os_mmap_read += btopr(len); 10786 /* 10787 * Ensure that os_mmap_read gets incremented, even if 10788 * maxprot were to look like PROT_NONE. 10789 */ 10790 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10791 !(maxprot & PROT_EXEC)) 10792 osp->os_mmap_read += btopr(len); 10793 osp->os_mapcnt += btopr(len); 10794 mutex_exit(&osp->os_sync_lock); 10795 open_stream_rele(osp, rp); 10796 } 10797 10798 out: 10799 /* 10800 * If we got an error, then undo our 10801 * incrementing of 'r_mapcnt'. 10802 */ 10803 10804 if (error) { 10805 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10806 ASSERT(rp->r_mapcnt >= 0); 10807 } 10808 return (error); 10809 } 10810 10811 /* ARGSUSED */ 10812 static int 10813 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct) 10814 { 10815 10816 return (VTOR4(vp1) == VTOR4(vp2)); 10817 } 10818 10819 /* ARGSUSED */ 10820 static int 10821 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10822 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr, 10823 caller_context_t *ct) 10824 { 10825 int rc; 10826 u_offset_t start, end; 10827 rnode4_t *rp; 10828 int error = 0, intr = INTR4(vp); 10829 nfs4_error_t e; 10830 10831 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10832 return (EIO); 10833 10834 /* check for valid cmd parameter */ 10835 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10836 return (EINVAL); 10837 10838 /* Verify l_type. */ 10839 switch (bfp->l_type) { 10840 case F_RDLCK: 10841 if (cmd != F_GETLK && !(flag & FREAD)) 10842 return (EBADF); 10843 break; 10844 case F_WRLCK: 10845 if (cmd != F_GETLK && !(flag & FWRITE)) 10846 return (EBADF); 10847 break; 10848 case F_UNLCK: 10849 intr = 0; 10850 break; 10851 10852 default: 10853 return (EINVAL); 10854 } 10855 10856 /* check the validity of the lock range */ 10857 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10858 return (rc); 10859 if (rc = flk_check_lock_data(start, end, MAXEND)) 10860 return (rc); 10861 10862 /* 10863 * If the filesystem is mounted using local locking, pass the 10864 * request off to the local locking code. 10865 */ 10866 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10867 if (cmd == F_SETLK || cmd == F_SETLKW) { 10868 /* 10869 * For complete safety, we should be holding 10870 * r_lkserlock. However, we can't call 10871 * nfs4_safelock and then fs_frlock while 10872 * holding r_lkserlock, so just invoke 10873 * nfs4_safelock and expect that this will 10874 * catch enough of the cases. 10875 */ 10876 if (!nfs4_safelock(vp, bfp, cr)) 10877 return (EAGAIN); 10878 } 10879 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 10880 } 10881 10882 rp = VTOR4(vp); 10883 10884 /* 10885 * Check whether the given lock request can proceed, given the 10886 * current file mappings. 10887 */ 10888 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10889 return (EINTR); 10890 if (cmd == F_SETLK || cmd == F_SETLKW) { 10891 if (!nfs4_safelock(vp, bfp, cr)) { 10892 rc = EAGAIN; 10893 goto done; 10894 } 10895 } 10896 10897 /* 10898 * Flush the cache after waiting for async I/O to finish. For new 10899 * locks, this is so that the process gets the latest bits from the 10900 * server. For unlocks, this is so that other clients see the 10901 * latest bits once the file has been unlocked. If currently dirty 10902 * pages can't be flushed, then don't allow a lock to be set. But 10903 * allow unlocks to succeed, to avoid having orphan locks on the 10904 * server. 10905 */ 10906 if (cmd != F_GETLK) { 10907 mutex_enter(&rp->r_statelock); 10908 while (rp->r_count > 0) { 10909 if (intr) { 10910 klwp_t *lwp = ttolwp(curthread); 10911 10912 if (lwp != NULL) 10913 lwp->lwp_nostop++; 10914 if (cv_wait_sig(&rp->r_cv, 10915 &rp->r_statelock) == 0) { 10916 if (lwp != NULL) 10917 lwp->lwp_nostop--; 10918 rc = EINTR; 10919 break; 10920 } 10921 if (lwp != NULL) 10922 lwp->lwp_nostop--; 10923 } else 10924 cv_wait(&rp->r_cv, &rp->r_statelock); 10925 } 10926 mutex_exit(&rp->r_statelock); 10927 if (rc != 0) 10928 goto done; 10929 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct); 10930 if (error) { 10931 if (error == ENOSPC || error == EDQUOT) { 10932 mutex_enter(&rp->r_statelock); 10933 if (!rp->r_error) 10934 rp->r_error = error; 10935 mutex_exit(&rp->r_statelock); 10936 } 10937 if (bfp->l_type != F_UNLCK) { 10938 rc = ENOLCK; 10939 goto done; 10940 } 10941 } 10942 } 10943 10944 /* 10945 * Call the lock manager to do the real work of contacting 10946 * the server and obtaining the lock. 10947 */ 10948 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10949 cr, &e, NULL, NULL); 10950 rc = e.error; 10951 10952 if (rc == 0) 10953 nfs4_lockcompletion(vp, cmd); 10954 10955 done: 10956 nfs_rw_exit(&rp->r_lkserlock); 10957 10958 return (rc); 10959 } 10960 10961 /* 10962 * Free storage space associated with the specified vnode. The portion 10963 * to be freed is specified by bfp->l_start and bfp->l_len (already 10964 * normalized to a "whence" of 0). 10965 * 10966 * This is an experimental facility whose continued existence is not 10967 * guaranteed. Currently, we only support the special case 10968 * of l_len == 0, meaning free to end of file. 10969 */ 10970 /* ARGSUSED */ 10971 static int 10972 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10973 offset_t offset, cred_t *cr, caller_context_t *ct) 10974 { 10975 int error; 10976 10977 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10978 return (EIO); 10979 ASSERT(vp->v_type == VREG); 10980 if (cmd != F_FREESP) 10981 return (EINVAL); 10982 10983 error = convoff(vp, bfp, 0, offset); 10984 if (!error) { 10985 ASSERT(bfp->l_start >= 0); 10986 if (bfp->l_len == 0) { 10987 struct vattr va; 10988 10989 va.va_mask = AT_SIZE; 10990 va.va_size = bfp->l_start; 10991 error = nfs4setattr(vp, &va, 0, cr, NULL); 10992 } else 10993 error = EINVAL; 10994 } 10995 10996 return (error); 10997 } 10998 10999 /* ARGSUSED */ 11000 int 11001 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 11002 { 11003 rnode4_t *rp; 11004 rp = VTOR4(vp); 11005 11006 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 11007 vp = RTOV4(rp); 11008 } 11009 *vpp = vp; 11010 return (0); 11011 } 11012 11013 /* 11014 * Setup and add an address space callback to do the work of the delmap call. 11015 * The callback will (and must be) deleted in the actual callback function. 11016 * 11017 * This is done in order to take care of the problem that we have with holding 11018 * the address space's a_lock for a long period of time (e.g. if the NFS server 11019 * is down). Callbacks will be executed in the address space code while the 11020 * a_lock is not held. Holding the address space's a_lock causes things such 11021 * as ps and fork to hang because they are trying to acquire this lock as well. 11022 */ 11023 /* ARGSUSED */ 11024 static int 11025 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 11026 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 11027 caller_context_t *ct) 11028 { 11029 int caller_found; 11030 int error; 11031 rnode4_t *rp; 11032 nfs4_delmap_args_t *dmapp; 11033 nfs4_delmapcall_t *delmap_call; 11034 11035 if (vp->v_flag & VNOMAP) 11036 return (ENOSYS); 11037 11038 /* 11039 * A process may not change zones if it has NFS pages mmap'ed 11040 * in, so we can't legitimately get here from the wrong zone. 11041 */ 11042 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11043 11044 rp = VTOR4(vp); 11045 11046 /* 11047 * The way that the address space of this process deletes its mapping 11048 * of this file is via the following call chains: 11049 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11050 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11051 * 11052 * With the use of address space callbacks we are allowed to drop the 11053 * address space lock, a_lock, while executing the NFS operations that 11054 * need to go over the wire. Returning EAGAIN to the caller of this 11055 * function is what drives the execution of the callback that we add 11056 * below. The callback will be executed by the address space code 11057 * after dropping the a_lock. When the callback is finished, since 11058 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 11059 * is called again on the same segment to finish the rest of the work 11060 * that needs to happen during unmapping. 11061 * 11062 * This action of calling back into the segment driver causes 11063 * nfs4_delmap() to get called again, but since the callback was 11064 * already executed at this point, it already did the work and there 11065 * is nothing left for us to do. 11066 * 11067 * To Summarize: 11068 * - The first time nfs4_delmap is called by the current thread is when 11069 * we add the caller associated with this delmap to the delmap caller 11070 * list, add the callback, and return EAGAIN. 11071 * - The second time in this call chain when nfs4_delmap is called we 11072 * will find this caller in the delmap caller list and realize there 11073 * is no more work to do thus removing this caller from the list and 11074 * returning the error that was set in the callback execution. 11075 */ 11076 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 11077 if (caller_found) { 11078 /* 11079 * 'error' is from the actual delmap operations. To avoid 11080 * hangs, we need to handle the return of EAGAIN differently 11081 * since this is what drives the callback execution. 11082 * In this case, we don't want to return EAGAIN and do the 11083 * callback execution because there are none to execute. 11084 */ 11085 if (error == EAGAIN) 11086 return (0); 11087 else 11088 return (error); 11089 } 11090 11091 /* current caller was not in the list */ 11092 delmap_call = nfs4_init_delmapcall(); 11093 11094 mutex_enter(&rp->r_statelock); 11095 list_insert_tail(&rp->r_indelmap, delmap_call); 11096 mutex_exit(&rp->r_statelock); 11097 11098 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 11099 11100 dmapp->vp = vp; 11101 dmapp->off = off; 11102 dmapp->addr = addr; 11103 dmapp->len = len; 11104 dmapp->prot = prot; 11105 dmapp->maxprot = maxprot; 11106 dmapp->flags = flags; 11107 dmapp->cr = cr; 11108 dmapp->caller = delmap_call; 11109 11110 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 11111 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 11112 11113 return (error ? error : EAGAIN); 11114 } 11115 11116 static nfs4_delmapcall_t * 11117 nfs4_init_delmapcall() 11118 { 11119 nfs4_delmapcall_t *delmap_call; 11120 11121 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 11122 delmap_call->call_id = curthread; 11123 delmap_call->error = 0; 11124 11125 return (delmap_call); 11126 } 11127 11128 static void 11129 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 11130 { 11131 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 11132 } 11133 11134 /* 11135 * Searches for the current delmap caller (based on curthread) in the list of 11136 * callers. If it is found, we remove it and free the delmap caller. 11137 * Returns: 11138 * 0 if the caller wasn't found 11139 * 1 if the caller was found, removed and freed. *errp will be set 11140 * to what the result of the delmap was. 11141 */ 11142 static int 11143 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 11144 { 11145 nfs4_delmapcall_t *delmap_call; 11146 11147 /* 11148 * If the list doesn't exist yet, we create it and return 11149 * that the caller wasn't found. No list = no callers. 11150 */ 11151 mutex_enter(&rp->r_statelock); 11152 if (!(rp->r_flags & R4DELMAPLIST)) { 11153 /* The list does not exist */ 11154 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 11155 offsetof(nfs4_delmapcall_t, call_node)); 11156 rp->r_flags |= R4DELMAPLIST; 11157 mutex_exit(&rp->r_statelock); 11158 return (0); 11159 } else { 11160 /* The list exists so search it */ 11161 for (delmap_call = list_head(&rp->r_indelmap); 11162 delmap_call != NULL; 11163 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 11164 if (delmap_call->call_id == curthread) { 11165 /* current caller is in the list */ 11166 *errp = delmap_call->error; 11167 list_remove(&rp->r_indelmap, delmap_call); 11168 mutex_exit(&rp->r_statelock); 11169 nfs4_free_delmapcall(delmap_call); 11170 return (1); 11171 } 11172 } 11173 } 11174 mutex_exit(&rp->r_statelock); 11175 return (0); 11176 } 11177 11178 /* 11179 * Remove some pages from an mmap'd vnode. Just update the 11180 * count of pages. If doing close-to-open, then flush and 11181 * commit all of the pages associated with this file. 11182 * Otherwise, start an asynchronous page flush to write out 11183 * any dirty pages. This will also associate a credential 11184 * with the rnode which can be used to write the pages. 11185 */ 11186 /* ARGSUSED */ 11187 static void 11188 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11189 { 11190 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11191 rnode4_t *rp; 11192 mntinfo4_t *mi; 11193 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11194 11195 rp = VTOR4(dmapp->vp); 11196 mi = VTOMI4(dmapp->vp); 11197 11198 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11199 ASSERT(rp->r_mapcnt >= 0); 11200 11201 /* 11202 * Initiate a page flush and potential commit if there are 11203 * pages, the file system was not mounted readonly, the segment 11204 * was mapped shared, and the pages themselves were writeable. 11205 */ 11206 if (nfs4_has_pages(dmapp->vp) && 11207 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11208 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11209 mutex_enter(&rp->r_statelock); 11210 rp->r_flags |= R4DIRTY; 11211 mutex_exit(&rp->r_statelock); 11212 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11213 dmapp->len, dmapp->cr); 11214 if (!e.error) { 11215 mutex_enter(&rp->r_statelock); 11216 e.error = rp->r_error; 11217 rp->r_error = 0; 11218 mutex_exit(&rp->r_statelock); 11219 } 11220 } else 11221 e.error = 0; 11222 11223 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11224 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11225 B_INVAL, dmapp->cr, NULL); 11226 11227 if (e.error) { 11228 e.stat = puterrno4(e.error); 11229 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11230 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11231 dmapp->caller->error = e.error; 11232 } 11233 11234 /* Check to see if we need to close the file */ 11235 11236 if (dmapp->vp->v_type == VREG) { 11237 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11238 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11239 11240 if (e.error != 0 || e.stat != NFS4_OK) { 11241 /* 11242 * Since it is possible that e.error == 0 and 11243 * e.stat != NFS4_OK (and vice versa), 11244 * we do the proper checking in order to get both 11245 * e.error and e.stat reporting the correct info. 11246 */ 11247 if (e.stat == NFS4_OK) 11248 e.stat = puterrno4(e.error); 11249 if (e.error == 0) 11250 e.error = geterrno4(e.stat); 11251 11252 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11253 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11254 dmapp->caller->error = e.error; 11255 } 11256 } 11257 11258 (void) as_delete_callback(as, arg); 11259 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11260 } 11261 11262 11263 static uint_t 11264 fattr4_maxfilesize_to_bits(uint64_t ll) 11265 { 11266 uint_t l = 1; 11267 11268 if (ll == 0) { 11269 return (0); 11270 } 11271 11272 if (ll & 0xffffffff00000000) { 11273 l += 32; ll >>= 32; 11274 } 11275 if (ll & 0xffff0000) { 11276 l += 16; ll >>= 16; 11277 } 11278 if (ll & 0xff00) { 11279 l += 8; ll >>= 8; 11280 } 11281 if (ll & 0xf0) { 11282 l += 4; ll >>= 4; 11283 } 11284 if (ll & 0xc) { 11285 l += 2; ll >>= 2; 11286 } 11287 if (ll & 0x2) { 11288 l += 1; 11289 } 11290 return (l); 11291 } 11292 11293 static int 11294 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr) 11295 { 11296 vnode_t *avp = NULL; 11297 int error; 11298 11299 if ((error = nfs4lookup_xattr(vp, "", &avp, 11300 LOOKUP_XATTR, cr)) == 0) 11301 error = do_xattr_exists_check(avp, valp, cr); 11302 if (avp) 11303 VN_RELE(avp); 11304 11305 return (error); 11306 } 11307 11308 /* ARGSUSED */ 11309 int 11310 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 11311 caller_context_t *ct) 11312 { 11313 int error; 11314 hrtime_t t; 11315 rnode4_t *rp; 11316 nfs4_ga_res_t gar; 11317 nfs4_ga_ext_res_t ger; 11318 11319 gar.n4g_ext_res = &ger; 11320 11321 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11322 return (EIO); 11323 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11324 *valp = MAXPATHLEN; 11325 return (0); 11326 } 11327 if (cmd == _PC_ACL_ENABLED) { 11328 *valp = _ACL_ACE_ENABLED; 11329 return (0); 11330 } 11331 11332 rp = VTOR4(vp); 11333 if (cmd == _PC_XATTR_EXISTS) { 11334 /* 11335 * The existence of the xattr directory is not sufficient 11336 * for determining whether generic user attributes exists. 11337 * The attribute directory could only be a transient directory 11338 * used for Solaris sysattr support. Do a small readdir 11339 * to verify if the only entries are sysattrs or not. 11340 * 11341 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11342 * is NULL. Once the xadir vp exists, we can create xattrs, 11343 * and we don't have any way to update the "base" object's 11344 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11345 * could help out. 11346 */ 11347 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11348 rp->r_xattr_dir == NULL) { 11349 return (nfs4_have_xattrs(vp, valp, cr)); 11350 } 11351 } else { /* OLD CODE */ 11352 if (ATTRCACHE4_VALID(vp)) { 11353 mutex_enter(&rp->r_statelock); 11354 if (rp->r_pathconf.pc4_cache_valid) { 11355 error = 0; 11356 switch (cmd) { 11357 case _PC_FILESIZEBITS: 11358 *valp = 11359 rp->r_pathconf.pc4_filesizebits; 11360 break; 11361 case _PC_LINK_MAX: 11362 *valp = 11363 rp->r_pathconf.pc4_link_max; 11364 break; 11365 case _PC_NAME_MAX: 11366 *valp = 11367 rp->r_pathconf.pc4_name_max; 11368 break; 11369 case _PC_CHOWN_RESTRICTED: 11370 *valp = 11371 rp->r_pathconf.pc4_chown_restricted; 11372 break; 11373 case _PC_NO_TRUNC: 11374 *valp = 11375 rp->r_pathconf.pc4_no_trunc; 11376 break; 11377 default: 11378 error = EINVAL; 11379 break; 11380 } 11381 mutex_exit(&rp->r_statelock); 11382 #ifdef DEBUG 11383 nfs4_pathconf_cache_hits++; 11384 #endif 11385 return (error); 11386 } 11387 mutex_exit(&rp->r_statelock); 11388 } 11389 } 11390 #ifdef DEBUG 11391 nfs4_pathconf_cache_misses++; 11392 #endif 11393 11394 t = gethrtime(); 11395 11396 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11397 11398 if (error) { 11399 mutex_enter(&rp->r_statelock); 11400 rp->r_pathconf.pc4_cache_valid = FALSE; 11401 rp->r_pathconf.pc4_xattr_valid = FALSE; 11402 mutex_exit(&rp->r_statelock); 11403 return (error); 11404 } 11405 11406 /* interpret the max filesize */ 11407 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11408 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11409 11410 /* Store the attributes we just received */ 11411 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11412 11413 switch (cmd) { 11414 case _PC_FILESIZEBITS: 11415 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11416 break; 11417 case _PC_LINK_MAX: 11418 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11419 break; 11420 case _PC_NAME_MAX: 11421 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11422 break; 11423 case _PC_CHOWN_RESTRICTED: 11424 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11425 break; 11426 case _PC_NO_TRUNC: 11427 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11428 break; 11429 case _PC_XATTR_EXISTS: 11430 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) { 11431 if (error = nfs4_have_xattrs(vp, valp, cr)) 11432 return (error); 11433 } 11434 break; 11435 default: 11436 return (EINVAL); 11437 } 11438 11439 return (0); 11440 } 11441 11442 /* 11443 * Called by async thread to do synchronous pageio. Do the i/o, wait 11444 * for it to complete, and cleanup the page list when done. 11445 */ 11446 static int 11447 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11448 int flags, cred_t *cr) 11449 { 11450 int error; 11451 11452 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11453 11454 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11455 if (flags & B_READ) 11456 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11457 else 11458 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11459 return (error); 11460 } 11461 11462 /* ARGSUSED */ 11463 static int 11464 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11465 int flags, cred_t *cr, caller_context_t *ct) 11466 { 11467 int error; 11468 rnode4_t *rp; 11469 11470 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11471 return (EIO); 11472 11473 if (pp == NULL) 11474 return (EINVAL); 11475 11476 rp = VTOR4(vp); 11477 mutex_enter(&rp->r_statelock); 11478 rp->r_count++; 11479 mutex_exit(&rp->r_statelock); 11480 11481 if (flags & B_ASYNC) { 11482 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11483 nfs4_sync_pageio); 11484 } else 11485 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11486 mutex_enter(&rp->r_statelock); 11487 rp->r_count--; 11488 cv_broadcast(&rp->r_cv); 11489 mutex_exit(&rp->r_statelock); 11490 return (error); 11491 } 11492 11493 /* ARGSUSED */ 11494 static void 11495 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr, 11496 caller_context_t *ct) 11497 { 11498 int error; 11499 rnode4_t *rp; 11500 page_t *plist; 11501 page_t *pptr; 11502 offset3 offset; 11503 count3 len; 11504 k_sigset_t smask; 11505 11506 /* 11507 * We should get called with fl equal to either B_FREE or 11508 * B_INVAL. Any other value is illegal. 11509 * 11510 * The page that we are either supposed to free or destroy 11511 * should be exclusive locked and its io lock should not 11512 * be held. 11513 */ 11514 ASSERT(fl == B_FREE || fl == B_INVAL); 11515 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11516 11517 rp = VTOR4(vp); 11518 11519 /* 11520 * If the page doesn't need to be committed or we shouldn't 11521 * even bother attempting to commit it, then just make sure 11522 * that the p_fsdata byte is clear and then either free or 11523 * destroy the page as appropriate. 11524 */ 11525 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11526 pp->p_fsdata = C_NOCOMMIT; 11527 if (fl == B_FREE) 11528 page_free(pp, dn); 11529 else 11530 page_destroy(pp, dn); 11531 return; 11532 } 11533 11534 /* 11535 * If there is a page invalidation operation going on, then 11536 * if this is one of the pages being destroyed, then just 11537 * clear the p_fsdata byte and then either free or destroy 11538 * the page as appropriate. 11539 */ 11540 mutex_enter(&rp->r_statelock); 11541 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11542 mutex_exit(&rp->r_statelock); 11543 pp->p_fsdata = C_NOCOMMIT; 11544 if (fl == B_FREE) 11545 page_free(pp, dn); 11546 else 11547 page_destroy(pp, dn); 11548 return; 11549 } 11550 11551 /* 11552 * If we are freeing this page and someone else is already 11553 * waiting to do a commit, then just unlock the page and 11554 * return. That other thread will take care of commiting 11555 * this page. The page can be freed sometime after the 11556 * commit has finished. Otherwise, if the page is marked 11557 * as delay commit, then we may be getting called from 11558 * pvn_write_done, one page at a time. This could result 11559 * in one commit per page, so we end up doing lots of small 11560 * commits instead of fewer larger commits. This is bad, 11561 * we want do as few commits as possible. 11562 */ 11563 if (fl == B_FREE) { 11564 if (rp->r_flags & R4COMMITWAIT) { 11565 page_unlock(pp); 11566 mutex_exit(&rp->r_statelock); 11567 return; 11568 } 11569 if (pp->p_fsdata == C_DELAYCOMMIT) { 11570 pp->p_fsdata = C_COMMIT; 11571 page_unlock(pp); 11572 mutex_exit(&rp->r_statelock); 11573 return; 11574 } 11575 } 11576 11577 /* 11578 * Check to see if there is a signal which would prevent an 11579 * attempt to commit the pages from being successful. If so, 11580 * then don't bother with all of the work to gather pages and 11581 * generate the unsuccessful RPC. Just return from here and 11582 * let the page be committed at some later time. 11583 */ 11584 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11585 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11586 sigunintr(&smask); 11587 page_unlock(pp); 11588 mutex_exit(&rp->r_statelock); 11589 return; 11590 } 11591 sigunintr(&smask); 11592 11593 /* 11594 * We are starting to need to commit pages, so let's try 11595 * to commit as many as possible at once to reduce the 11596 * overhead. 11597 * 11598 * Set the `commit inprogress' state bit. We must 11599 * first wait until any current one finishes. Then 11600 * we initialize the c_pages list with this page. 11601 */ 11602 while (rp->r_flags & R4COMMIT) { 11603 rp->r_flags |= R4COMMITWAIT; 11604 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11605 rp->r_flags &= ~R4COMMITWAIT; 11606 } 11607 rp->r_flags |= R4COMMIT; 11608 mutex_exit(&rp->r_statelock); 11609 ASSERT(rp->r_commit.c_pages == NULL); 11610 rp->r_commit.c_pages = pp; 11611 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11612 rp->r_commit.c_commlen = PAGESIZE; 11613 11614 /* 11615 * Gather together all other pages which can be committed. 11616 * They will all be chained off r_commit.c_pages. 11617 */ 11618 nfs4_get_commit(vp); 11619 11620 /* 11621 * Clear the `commit inprogress' status and disconnect 11622 * the list of pages to be committed from the rnode. 11623 * At this same time, we also save the starting offset 11624 * and length of data to be committed on the server. 11625 */ 11626 plist = rp->r_commit.c_pages; 11627 rp->r_commit.c_pages = NULL; 11628 offset = rp->r_commit.c_commbase; 11629 len = rp->r_commit.c_commlen; 11630 mutex_enter(&rp->r_statelock); 11631 rp->r_flags &= ~R4COMMIT; 11632 cv_broadcast(&rp->r_commit.c_cv); 11633 mutex_exit(&rp->r_statelock); 11634 11635 if (curproc == proc_pageout || curproc == proc_fsflush || 11636 nfs_zone() != VTOMI4(vp)->mi_zone) { 11637 nfs4_async_commit(vp, plist, offset, len, 11638 cr, do_nfs4_async_commit); 11639 return; 11640 } 11641 11642 /* 11643 * Actually generate the COMMIT op over the wire operation. 11644 */ 11645 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11646 11647 /* 11648 * If we got an error during the commit, just unlock all 11649 * of the pages. The pages will get retransmitted to the 11650 * server during a putpage operation. 11651 */ 11652 if (error) { 11653 while (plist != NULL) { 11654 pptr = plist; 11655 page_sub(&plist, pptr); 11656 page_unlock(pptr); 11657 } 11658 return; 11659 } 11660 11661 /* 11662 * We've tried as hard as we can to commit the data to stable 11663 * storage on the server. We just unlock the rest of the pages 11664 * and clear the commit required state. They will be put 11665 * onto the tail of the cachelist if they are nolonger 11666 * mapped. 11667 */ 11668 while (plist != pp) { 11669 pptr = plist; 11670 page_sub(&plist, pptr); 11671 pptr->p_fsdata = C_NOCOMMIT; 11672 page_unlock(pptr); 11673 } 11674 11675 /* 11676 * It is possible that nfs4_commit didn't return error but 11677 * some other thread has modified the page we are going 11678 * to free/destroy. 11679 * In this case we need to rewrite the page. Do an explicit check 11680 * before attempting to free/destroy the page. If modified, needs to 11681 * be rewritten so unlock the page and return. 11682 */ 11683 if (hat_ismod(pp)) { 11684 pp->p_fsdata = C_NOCOMMIT; 11685 page_unlock(pp); 11686 return; 11687 } 11688 11689 /* 11690 * Now, as appropriate, either free or destroy the page 11691 * that we were called with. 11692 */ 11693 pp->p_fsdata = C_NOCOMMIT; 11694 if (fl == B_FREE) 11695 page_free(pp, dn); 11696 else 11697 page_destroy(pp, dn); 11698 } 11699 11700 /* 11701 * Commit requires that the current fh be the file written to. 11702 * The compound op structure is: 11703 * PUTFH(file), COMMIT 11704 */ 11705 static int 11706 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11707 { 11708 COMPOUND4args_clnt args; 11709 COMPOUND4res_clnt res; 11710 COMMIT4res *cm_res; 11711 nfs_argop4 argop[2]; 11712 nfs_resop4 *resop; 11713 int doqueue; 11714 mntinfo4_t *mi; 11715 rnode4_t *rp; 11716 cred_t *cred_otw = NULL; 11717 bool_t needrecov = FALSE; 11718 nfs4_recov_state_t recov_state; 11719 nfs4_open_stream_t *osp = NULL; 11720 bool_t first_time = TRUE; /* first time getting OTW cred */ 11721 bool_t last_time = FALSE; /* last time getting OTW cred */ 11722 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11723 11724 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11725 11726 rp = VTOR4(vp); 11727 11728 mi = VTOMI4(vp); 11729 recov_state.rs_flags = 0; 11730 recov_state.rs_num_retry_despite_err = 0; 11731 get_commit_cred: 11732 /* 11733 * Releases the osp, if a valid open stream is provided. 11734 * Puts a hold on the cred_otw and the new osp (if found). 11735 */ 11736 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11737 &first_time, &last_time); 11738 args.ctag = TAG_COMMIT; 11739 recov_retry: 11740 /* 11741 * Commit ops: putfh file; commit 11742 */ 11743 args.array_len = 2; 11744 args.array = argop; 11745 11746 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11747 &recov_state, NULL); 11748 if (e.error) { 11749 crfree(cred_otw); 11750 if (osp != NULL) 11751 open_stream_rele(osp, rp); 11752 return (e.error); 11753 } 11754 11755 /* putfh directory */ 11756 argop[0].argop = OP_CPUTFH; 11757 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11758 11759 /* commit */ 11760 argop[1].argop = OP_COMMIT; 11761 argop[1].nfs_argop4_u.opcommit.offset = offset; 11762 argop[1].nfs_argop4_u.opcommit.count = count; 11763 11764 doqueue = 1; 11765 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11766 11767 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11768 if (!needrecov && e.error) { 11769 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11770 needrecov); 11771 crfree(cred_otw); 11772 if (e.error == EACCES && last_time == FALSE) 11773 goto get_commit_cred; 11774 if (osp != NULL) 11775 open_stream_rele(osp, rp); 11776 return (e.error); 11777 } 11778 11779 if (needrecov) { 11780 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11781 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) { 11782 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11783 &recov_state, needrecov); 11784 if (!e.error) 11785 (void) xdr_free(xdr_COMPOUND4res_clnt, 11786 (caddr_t)&res); 11787 goto recov_retry; 11788 } 11789 if (e.error) { 11790 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11791 &recov_state, needrecov); 11792 crfree(cred_otw); 11793 if (osp != NULL) 11794 open_stream_rele(osp, rp); 11795 return (e.error); 11796 } 11797 /* fall through for res.status case */ 11798 } 11799 11800 if (res.status) { 11801 e.error = geterrno4(res.status); 11802 if (e.error == EACCES && last_time == FALSE) { 11803 crfree(cred_otw); 11804 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11805 &recov_state, needrecov); 11806 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11807 goto get_commit_cred; 11808 } 11809 /* 11810 * Can't do a nfs4_purge_stale_fh here because this 11811 * can cause a deadlock. nfs4_commit can 11812 * be called from nfs4_dispose which can be called 11813 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11814 * can call back to pvn_vplist_dirty. 11815 */ 11816 if (e.error == ESTALE) { 11817 mutex_enter(&rp->r_statelock); 11818 rp->r_flags |= R4STALE; 11819 if (!rp->r_error) 11820 rp->r_error = e.error; 11821 mutex_exit(&rp->r_statelock); 11822 PURGE_ATTRCACHE4(vp); 11823 } else { 11824 mutex_enter(&rp->r_statelock); 11825 if (!rp->r_error) 11826 rp->r_error = e.error; 11827 mutex_exit(&rp->r_statelock); 11828 } 11829 } else { 11830 ASSERT(rp->r_flags & R4HAVEVERF); 11831 resop = &res.array[1]; /* commit res */ 11832 cm_res = &resop->nfs_resop4_u.opcommit; 11833 mutex_enter(&rp->r_statelock); 11834 if (cm_res->writeverf == rp->r_writeverf) { 11835 mutex_exit(&rp->r_statelock); 11836 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11837 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11838 &recov_state, needrecov); 11839 crfree(cred_otw); 11840 if (osp != NULL) 11841 open_stream_rele(osp, rp); 11842 return (0); 11843 } 11844 nfs4_set_mod(vp); 11845 rp->r_writeverf = cm_res->writeverf; 11846 mutex_exit(&rp->r_statelock); 11847 e.error = NFS_VERF_MISMATCH; 11848 } 11849 11850 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11851 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11852 crfree(cred_otw); 11853 if (osp != NULL) 11854 open_stream_rele(osp, rp); 11855 11856 return (e.error); 11857 } 11858 11859 static void 11860 nfs4_set_mod(vnode_t *vp) 11861 { 11862 page_t *pp; 11863 kmutex_t *vphm; 11864 rnode4_t *rp; 11865 11866 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11867 11868 /* make sure we're looking at the master vnode, not a shadow */ 11869 11870 rp = VTOR4(vp); 11871 if (IS_SHADOW(vp, rp)) 11872 vp = RTOV4(rp); 11873 11874 vphm = page_vnode_mutex(vp); 11875 mutex_enter(vphm); 11876 /* 11877 * If there are no pages associated with this vnode, then 11878 * just return. 11879 */ 11880 if ((pp = vp->v_pages) == NULL) { 11881 mutex_exit(vphm); 11882 return; 11883 } 11884 11885 do { 11886 if (pp->p_fsdata != C_NOCOMMIT) { 11887 hat_setmod(pp); 11888 pp->p_fsdata = C_NOCOMMIT; 11889 } 11890 } while ((pp = pp->p_vpnext) != vp->v_pages); 11891 mutex_exit(vphm); 11892 } 11893 11894 /* 11895 * This function is used to gather a page list of the pages which 11896 * can be committed on the server. 11897 * 11898 * The calling thread must have set R4COMMIT. This bit is used to 11899 * serialize access to the commit structure in the rnode. As long 11900 * as the thread has set R4COMMIT, then it can manipulate the commit 11901 * structure without requiring any other locks. 11902 * 11903 * When this function is called from nfs4_dispose() the page passed 11904 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11905 * will skip it. This is not a problem since we initially add the 11906 * page to the r_commit page list. 11907 * 11908 */ 11909 static void 11910 nfs4_get_commit(vnode_t *vp) 11911 { 11912 rnode4_t *rp; 11913 page_t *pp; 11914 kmutex_t *vphm; 11915 11916 rp = VTOR4(vp); 11917 11918 ASSERT(rp->r_flags & R4COMMIT); 11919 11920 /* make sure we're looking at the master vnode, not a shadow */ 11921 11922 if (IS_SHADOW(vp, rp)) 11923 vp = RTOV4(rp); 11924 11925 vphm = page_vnode_mutex(vp); 11926 mutex_enter(vphm); 11927 11928 /* 11929 * If there are no pages associated with this vnode, then 11930 * just return. 11931 */ 11932 if ((pp = vp->v_pages) == NULL) { 11933 mutex_exit(vphm); 11934 return; 11935 } 11936 11937 /* 11938 * Step through all of the pages associated with this vnode 11939 * looking for pages which need to be committed. 11940 */ 11941 do { 11942 /* 11943 * First short-cut everything (without the page_lock) 11944 * and see if this page does not need to be committed 11945 * or is modified if so then we'll just skip it. 11946 */ 11947 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11948 continue; 11949 11950 /* 11951 * Attempt to lock the page. If we can't, then 11952 * someone else is messing with it or we have been 11953 * called from nfs4_dispose and this is the page that 11954 * nfs4_dispose was called with.. anyway just skip it. 11955 */ 11956 if (!page_trylock(pp, SE_EXCL)) 11957 continue; 11958 11959 /* 11960 * Lets check again now that we have the page lock. 11961 */ 11962 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11963 page_unlock(pp); 11964 continue; 11965 } 11966 11967 /* this had better not be a free page */ 11968 ASSERT(PP_ISFREE(pp) == 0); 11969 11970 /* 11971 * The page needs to be committed and we locked it. 11972 * Update the base and length parameters and add it 11973 * to r_pages. 11974 */ 11975 if (rp->r_commit.c_pages == NULL) { 11976 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11977 rp->r_commit.c_commlen = PAGESIZE; 11978 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11979 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11980 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11981 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11982 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11983 <= pp->p_offset) { 11984 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11985 rp->r_commit.c_commbase + PAGESIZE; 11986 } 11987 page_add(&rp->r_commit.c_pages, pp); 11988 } while ((pp = pp->p_vpnext) != vp->v_pages); 11989 11990 mutex_exit(vphm); 11991 } 11992 11993 /* 11994 * This routine is used to gather together a page list of the pages 11995 * which are to be committed on the server. This routine must not 11996 * be called if the calling thread holds any locked pages. 11997 * 11998 * The calling thread must have set R4COMMIT. This bit is used to 11999 * serialize access to the commit structure in the rnode. As long 12000 * as the thread has set R4COMMIT, then it can manipulate the commit 12001 * structure without requiring any other locks. 12002 */ 12003 static void 12004 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 12005 { 12006 12007 rnode4_t *rp; 12008 page_t *pp; 12009 u_offset_t end; 12010 u_offset_t off; 12011 ASSERT(len != 0); 12012 rp = VTOR4(vp); 12013 ASSERT(rp->r_flags & R4COMMIT); 12014 12015 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12016 12017 /* make sure we're looking at the master vnode, not a shadow */ 12018 12019 if (IS_SHADOW(vp, rp)) 12020 vp = RTOV4(rp); 12021 12022 /* 12023 * If there are no pages associated with this vnode, then 12024 * just return. 12025 */ 12026 if ((pp = vp->v_pages) == NULL) 12027 return; 12028 /* 12029 * Calculate the ending offset. 12030 */ 12031 end = soff + len; 12032 for (off = soff; off < end; off += PAGESIZE) { 12033 /* 12034 * Lookup each page by vp, offset. 12035 */ 12036 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 12037 continue; 12038 /* 12039 * If this page does not need to be committed or is 12040 * modified, then just skip it. 12041 */ 12042 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 12043 page_unlock(pp); 12044 continue; 12045 } 12046 12047 ASSERT(PP_ISFREE(pp) == 0); 12048 /* 12049 * The page needs to be committed and we locked it. 12050 * Update the base and length parameters and add it 12051 * to r_pages. 12052 */ 12053 if (rp->r_commit.c_pages == NULL) { 12054 rp->r_commit.c_commbase = (offset3)pp->p_offset; 12055 rp->r_commit.c_commlen = PAGESIZE; 12056 } else { 12057 rp->r_commit.c_commlen = (offset3)pp->p_offset - 12058 rp->r_commit.c_commbase + PAGESIZE; 12059 } 12060 page_add(&rp->r_commit.c_pages, pp); 12061 } 12062 } 12063 12064 /* 12065 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 12066 * Flushes and commits data to the server. 12067 */ 12068 static int 12069 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 12070 { 12071 int error; 12072 verifier4 write_verf; 12073 rnode4_t *rp = VTOR4(vp); 12074 12075 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12076 12077 /* 12078 * Flush the data portion of the file and then commit any 12079 * portions which need to be committed. This may need to 12080 * be done twice if the server has changed state since 12081 * data was last written. The data will need to be 12082 * rewritten to the server and then a new commit done. 12083 * 12084 * In fact, this may need to be done several times if the 12085 * server is having problems and crashing while we are 12086 * attempting to do this. 12087 */ 12088 12089 top: 12090 /* 12091 * Do a flush based on the poff and plen arguments. This 12092 * will synchronously write out any modified pages in the 12093 * range specified by (poff, plen). This starts all of the 12094 * i/o operations which will be waited for in the next 12095 * call to nfs4_putpage 12096 */ 12097 12098 mutex_enter(&rp->r_statelock); 12099 write_verf = rp->r_writeverf; 12100 mutex_exit(&rp->r_statelock); 12101 12102 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL); 12103 if (error == EAGAIN) 12104 error = 0; 12105 12106 /* 12107 * Do a flush based on the poff and plen arguments. This 12108 * will synchronously write out any modified pages in the 12109 * range specified by (poff, plen) and wait until all of 12110 * the asynchronous i/o's in that range are done as well. 12111 */ 12112 if (!error) 12113 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL); 12114 12115 if (error) 12116 return (error); 12117 12118 mutex_enter(&rp->r_statelock); 12119 if (rp->r_writeverf != write_verf) { 12120 mutex_exit(&rp->r_statelock); 12121 goto top; 12122 } 12123 mutex_exit(&rp->r_statelock); 12124 12125 /* 12126 * Now commit any pages which might need to be committed. 12127 * If the error, NFS_VERF_MISMATCH, is returned, then 12128 * start over with the flush operation. 12129 */ 12130 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 12131 12132 if (error == NFS_VERF_MISMATCH) 12133 goto top; 12134 12135 return (error); 12136 } 12137 12138 /* 12139 * nfs4_commit_vp() will wait for other pending commits and 12140 * will either commit the whole file or a range, plen dictates 12141 * if we commit whole file. a value of zero indicates the whole 12142 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 12143 */ 12144 static int 12145 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 12146 cred_t *cr, int wait_on_writes) 12147 { 12148 rnode4_t *rp; 12149 page_t *plist; 12150 offset3 offset; 12151 count3 len; 12152 12153 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12154 12155 rp = VTOR4(vp); 12156 12157 /* 12158 * before we gather commitable pages make 12159 * sure there are no outstanding async writes 12160 */ 12161 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 12162 mutex_enter(&rp->r_statelock); 12163 while (rp->r_count > 0) { 12164 cv_wait(&rp->r_cv, &rp->r_statelock); 12165 } 12166 mutex_exit(&rp->r_statelock); 12167 } 12168 12169 /* 12170 * Set the `commit inprogress' state bit. We must 12171 * first wait until any current one finishes. 12172 */ 12173 mutex_enter(&rp->r_statelock); 12174 while (rp->r_flags & R4COMMIT) { 12175 rp->r_flags |= R4COMMITWAIT; 12176 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 12177 rp->r_flags &= ~R4COMMITWAIT; 12178 } 12179 rp->r_flags |= R4COMMIT; 12180 mutex_exit(&rp->r_statelock); 12181 12182 /* 12183 * Gather all of the pages which need to be 12184 * committed. 12185 */ 12186 if (plen == 0) 12187 nfs4_get_commit(vp); 12188 else 12189 nfs4_get_commit_range(vp, poff, plen); 12190 12191 /* 12192 * Clear the `commit inprogress' bit and disconnect the 12193 * page list which was gathered by nfs4_get_commit. 12194 */ 12195 plist = rp->r_commit.c_pages; 12196 rp->r_commit.c_pages = NULL; 12197 offset = rp->r_commit.c_commbase; 12198 len = rp->r_commit.c_commlen; 12199 mutex_enter(&rp->r_statelock); 12200 rp->r_flags &= ~R4COMMIT; 12201 cv_broadcast(&rp->r_commit.c_cv); 12202 mutex_exit(&rp->r_statelock); 12203 12204 /* 12205 * If any pages need to be committed, commit them and 12206 * then unlock them so that they can be freed some 12207 * time later. 12208 */ 12209 if (plist == NULL) 12210 return (0); 12211 12212 /* 12213 * No error occurred during the flush portion 12214 * of this operation, so now attempt to commit 12215 * the data to stable storage on the server. 12216 * 12217 * This will unlock all of the pages on the list. 12218 */ 12219 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12220 } 12221 12222 static int 12223 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12224 cred_t *cr) 12225 { 12226 int error; 12227 page_t *pp; 12228 12229 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12230 12231 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12232 12233 /* 12234 * If we got an error, then just unlock all of the pages 12235 * on the list. 12236 */ 12237 if (error) { 12238 while (plist != NULL) { 12239 pp = plist; 12240 page_sub(&plist, pp); 12241 page_unlock(pp); 12242 } 12243 return (error); 12244 } 12245 /* 12246 * We've tried as hard as we can to commit the data to stable 12247 * storage on the server. We just unlock the pages and clear 12248 * the commit required state. They will get freed later. 12249 */ 12250 while (plist != NULL) { 12251 pp = plist; 12252 page_sub(&plist, pp); 12253 pp->p_fsdata = C_NOCOMMIT; 12254 page_unlock(pp); 12255 } 12256 12257 return (error); 12258 } 12259 12260 static void 12261 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12262 cred_t *cr) 12263 { 12264 12265 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12266 } 12267 12268 /*ARGSUSED*/ 12269 static int 12270 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12271 caller_context_t *ct) 12272 { 12273 int error = 0; 12274 mntinfo4_t *mi; 12275 vattr_t va; 12276 vsecattr_t nfsace4_vsap; 12277 12278 mi = VTOMI4(vp); 12279 if (nfs_zone() != mi->mi_zone) 12280 return (EIO); 12281 if (mi->mi_flags & MI4_ACL) { 12282 /* if we have a delegation, return it */ 12283 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12284 (void) nfs4delegreturn(VTOR4(vp), 12285 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12286 12287 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12288 NFS4_ACL_SET); 12289 if (error) /* EINVAL */ 12290 return (error); 12291 12292 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12293 /* 12294 * These are aclent_t type entries. 12295 */ 12296 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12297 vp->v_type == VDIR, FALSE); 12298 if (error) 12299 return (error); 12300 } else { 12301 /* 12302 * These are ace_t type entries. 12303 */ 12304 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12305 FALSE); 12306 if (error) 12307 return (error); 12308 } 12309 bzero(&va, sizeof (va)); 12310 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12311 vs_ace4_destroy(&nfsace4_vsap); 12312 return (error); 12313 } 12314 return (ENOSYS); 12315 } 12316 12317 /* ARGSUSED */ 12318 int 12319 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12320 caller_context_t *ct) 12321 { 12322 int error; 12323 mntinfo4_t *mi; 12324 nfs4_ga_res_t gar; 12325 rnode4_t *rp = VTOR4(vp); 12326 12327 mi = VTOMI4(vp); 12328 if (nfs_zone() != mi->mi_zone) 12329 return (EIO); 12330 12331 bzero(&gar, sizeof (gar)); 12332 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12333 12334 /* 12335 * vsecattr->vsa_mask holds the original acl request mask. 12336 * This is needed when determining what to return. 12337 * (See: nfs4_create_getsecattr_return()) 12338 */ 12339 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12340 if (error) /* EINVAL */ 12341 return (error); 12342 12343 /* 12344 * If this is a referral stub, don't try to go OTW for an ACL 12345 */ 12346 if (RP_ISSTUB_REFERRAL(VTOR4(vp))) 12347 return (fs_fab_acl(vp, vsecattr, flag, cr, ct)); 12348 12349 if (mi->mi_flags & MI4_ACL) { 12350 /* 12351 * Check if the data is cached and the cache is valid. If it 12352 * is we don't go over the wire. 12353 */ 12354 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12355 mutex_enter(&rp->r_statelock); 12356 if (rp->r_secattr != NULL) { 12357 error = nfs4_create_getsecattr_return( 12358 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12359 rp->r_attr.va_gid, 12360 vp->v_type == VDIR); 12361 if (!error) { /* error == 0 - Success! */ 12362 mutex_exit(&rp->r_statelock); 12363 return (error); 12364 } 12365 } 12366 mutex_exit(&rp->r_statelock); 12367 } 12368 12369 /* 12370 * The getattr otw call will always get both the acl, in 12371 * the form of a list of nfsace4's, and the number of acl 12372 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12373 */ 12374 gar.n4g_va.va_mask = AT_ALL; 12375 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12376 if (error) { 12377 vs_ace4_destroy(&gar.n4g_vsa); 12378 if (error == ENOTSUP || error == EOPNOTSUPP) 12379 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12380 return (error); 12381 } 12382 12383 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12384 /* 12385 * No error was returned, but according to the response 12386 * bitmap, neither was an acl. 12387 */ 12388 vs_ace4_destroy(&gar.n4g_vsa); 12389 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12390 return (error); 12391 } 12392 12393 /* 12394 * Update the cache with the ACL. 12395 */ 12396 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12397 12398 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12399 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12400 vp->v_type == VDIR); 12401 vs_ace4_destroy(&gar.n4g_vsa); 12402 if ((error) && (vsecattr->vsa_mask & 12403 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12404 (error != EACCES)) { 12405 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12406 } 12407 return (error); 12408 } 12409 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12410 return (error); 12411 } 12412 12413 /* 12414 * The function returns: 12415 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12416 * - EINVAL if the passed in "acl_mask" is an invalid request. 12417 * 12418 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12419 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12420 * 12421 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12422 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12423 * - We have a count field set without the corresponding acl field set. (e.g. - 12424 * VSA_ACECNT is set, but VSA_ACE is not) 12425 */ 12426 static int 12427 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12428 { 12429 /* Shortcut the masks that are always valid. */ 12430 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12431 return (0); 12432 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12433 return (0); 12434 12435 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12436 /* 12437 * We can't have any VSA_ACL type stuff in the mask now. 12438 */ 12439 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12440 VSA_DFACLCNT)) 12441 return (EINVAL); 12442 12443 if (op == NFS4_ACL_SET) { 12444 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12445 return (EINVAL); 12446 } 12447 } 12448 12449 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12450 /* 12451 * We can't have any VSA_ACE type stuff in the mask now. 12452 */ 12453 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12454 return (EINVAL); 12455 12456 if (op == NFS4_ACL_SET) { 12457 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12458 return (EINVAL); 12459 12460 if ((acl_mask & VSA_DFACLCNT) && 12461 !(acl_mask & VSA_DFACL)) 12462 return (EINVAL); 12463 } 12464 } 12465 return (0); 12466 } 12467 12468 /* 12469 * The theory behind creating the correct getsecattr return is simply this: 12470 * "Don't return anything that the caller is not expecting to have to free." 12471 */ 12472 static int 12473 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12474 uid_t uid, gid_t gid, int isdir) 12475 { 12476 int error = 0; 12477 /* Save the mask since the translators modify it. */ 12478 uint_t orig_mask = vsap->vsa_mask; 12479 12480 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12481 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, 12482 FALSE, ((orig_mask & VSA_ACE) ? FALSE : TRUE)); 12483 12484 if (error) 12485 return (error); 12486 12487 /* 12488 * If the caller only asked for the ace count (VSA_ACECNT) 12489 * don't give them the full acl (VSA_ACE), free it. 12490 */ 12491 if (!orig_mask & VSA_ACE) { 12492 if (vsap->vsa_aclentp != NULL) { 12493 kmem_free(vsap->vsa_aclentp, 12494 vsap->vsa_aclcnt * sizeof (ace_t)); 12495 vsap->vsa_aclentp = NULL; 12496 } 12497 } 12498 vsap->vsa_mask = orig_mask; 12499 12500 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12501 VSA_DFACLCNT)) { 12502 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12503 isdir, FALSE, 12504 ((orig_mask & (VSA_ACL | VSA_DFACL)) ? FALSE : TRUE)); 12505 12506 if (error) 12507 return (error); 12508 12509 /* 12510 * If the caller only asked for the acl count (VSA_ACLCNT) 12511 * and/or the default acl count (VSA_DFACLCNT) don't give them 12512 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12513 */ 12514 if (!orig_mask & VSA_ACL) { 12515 if (vsap->vsa_aclentp != NULL) { 12516 kmem_free(vsap->vsa_aclentp, 12517 vsap->vsa_aclcnt * sizeof (aclent_t)); 12518 vsap->vsa_aclentp = NULL; 12519 } 12520 } 12521 12522 if (!orig_mask & VSA_DFACL) { 12523 if (vsap->vsa_dfaclentp != NULL) { 12524 kmem_free(vsap->vsa_dfaclentp, 12525 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12526 vsap->vsa_dfaclentp = NULL; 12527 } 12528 } 12529 vsap->vsa_mask = orig_mask; 12530 } 12531 return (0); 12532 } 12533 12534 /* ARGSUSED */ 12535 int 12536 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr, 12537 caller_context_t *ct) 12538 { 12539 int error; 12540 12541 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12542 return (EIO); 12543 /* 12544 * check for valid cmd parameter 12545 */ 12546 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12547 return (EINVAL); 12548 12549 /* 12550 * Check access permissions 12551 */ 12552 if ((cmd & F_SHARE) && 12553 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12554 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12555 return (EBADF); 12556 12557 /* 12558 * If the filesystem is mounted using local locking, pass the 12559 * request off to the local share code. 12560 */ 12561 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12562 return (fs_shrlock(vp, cmd, shr, flag, cr, ct)); 12563 12564 switch (cmd) { 12565 case F_SHARE: 12566 case F_UNSHARE: 12567 /* 12568 * This will be properly implemented later, 12569 * see RFE: 4823948 . 12570 */ 12571 error = EAGAIN; 12572 break; 12573 12574 case F_HASREMOTELOCKS: 12575 /* 12576 * NFS client can't store remote locks itself 12577 */ 12578 shr->s_access = 0; 12579 error = 0; 12580 break; 12581 12582 default: 12583 error = EINVAL; 12584 break; 12585 } 12586 12587 return (error); 12588 } 12589 12590 /* 12591 * Common code called by directory ops to update the attrcache 12592 */ 12593 static int 12594 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12595 hrtime_t t, vnode_t *vp, cred_t *cr) 12596 { 12597 int error = 0; 12598 12599 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12600 12601 if (status != NFS4_OK) { 12602 /* getattr not done or failed */ 12603 PURGE_ATTRCACHE4(vp); 12604 return (error); 12605 } 12606 12607 if (garp) { 12608 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12609 } else { 12610 PURGE_ATTRCACHE4(vp); 12611 } 12612 return (error); 12613 } 12614 12615 /* 12616 * Update directory caches for directory modification ops (link, rename, etc.) 12617 * When dinfo is NULL, manage dircaches in the old way. 12618 */ 12619 static void 12620 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12621 dirattr_info_t *dinfo) 12622 { 12623 rnode4_t *drp = VTOR4(dvp); 12624 12625 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12626 12627 /* Purge rddir cache for dir since it changed */ 12628 if (drp->r_dir != NULL) 12629 nfs4_purge_rddir_cache(dvp); 12630 12631 /* 12632 * If caller provided dinfo, then use it to manage dir caches. 12633 */ 12634 if (dinfo != NULL) { 12635 if (vp != NULL) { 12636 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12637 if (!VTOR4(vp)->created_v4) { 12638 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12639 dnlc_update(dvp, nm, vp); 12640 } else { 12641 /* 12642 * XXX don't update if the created_v4 flag is 12643 * set 12644 */ 12645 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12646 NFS4_DEBUG(nfs4_client_state_debug, 12647 (CE_NOTE, "nfs4_update_dircaches: " 12648 "don't update dnlc: created_v4 flag")); 12649 } 12650 } 12651 12652 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12653 dinfo->di_cred, FALSE, cinfo); 12654 12655 return; 12656 } 12657 12658 /* 12659 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12660 * Since caller modified dir but didn't receive post-dirmod-op dir 12661 * attrs, the dir's attrs must be purged. 12662 * 12663 * XXX this check and dnlc update/purge should really be atomic, 12664 * XXX but can't use rnode statelock because it'll deadlock in 12665 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12666 * XXX does occur. 12667 * 12668 * XXX We also may want to check that atomic is true in the 12669 * XXX change_info struct. If it is not, the change_info may 12670 * XXX reflect changes by more than one clients which means that 12671 * XXX our cache may not be valid. 12672 */ 12673 PURGE_ATTRCACHE4(dvp); 12674 if (drp->r_change == cinfo->before) { 12675 /* no changes took place in the directory prior to our link */ 12676 if (vp != NULL) { 12677 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12678 if (!VTOR4(vp)->created_v4) { 12679 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12680 dnlc_update(dvp, nm, vp); 12681 } else { 12682 /* 12683 * XXX dont' update if the created_v4 flag 12684 * is set 12685 */ 12686 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12687 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12688 "nfs4_update_dircaches: don't" 12689 " update dnlc: created_v4 flag")); 12690 } 12691 } 12692 } else { 12693 /* Another client modified directory - purge its dnlc cache */ 12694 dnlc_purge_vp(dvp); 12695 } 12696 } 12697 12698 /* 12699 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12700 * file. 12701 * 12702 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12703 * file (ie: client recovery) and otherwise set to FALSE. 12704 * 12705 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12706 * initiated) calling functions. 12707 * 12708 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12709 * of resending a 'lost' open request. 12710 * 12711 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12712 * server that hands out BAD_SEQID on open confirm. 12713 * 12714 * Errors are returned via the nfs4_error_t parameter. 12715 */ 12716 void 12717 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12718 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12719 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12720 { 12721 COMPOUND4args_clnt args; 12722 COMPOUND4res_clnt res; 12723 nfs_argop4 argop[2]; 12724 nfs_resop4 *resop; 12725 int doqueue = 1; 12726 mntinfo4_t *mi; 12727 OPEN_CONFIRM4args *open_confirm_args; 12728 int needrecov; 12729 12730 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12731 #if DEBUG 12732 mutex_enter(&oop->oo_lock); 12733 ASSERT(oop->oo_seqid_inuse); 12734 mutex_exit(&oop->oo_lock); 12735 #endif 12736 12737 recov_retry_confirm: 12738 nfs4_error_zinit(ep); 12739 *retry_open = FALSE; 12740 12741 if (resend) 12742 args.ctag = TAG_OPEN_CONFIRM_LOST; 12743 else 12744 args.ctag = TAG_OPEN_CONFIRM; 12745 12746 args.array_len = 2; 12747 args.array = argop; 12748 12749 /* putfh target fh */ 12750 argop[0].argop = OP_CPUTFH; 12751 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12752 12753 argop[1].argop = OP_OPEN_CONFIRM; 12754 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12755 12756 (*seqid) += 1; 12757 open_confirm_args->seqid = *seqid; 12758 open_confirm_args->open_stateid = *stateid; 12759 12760 mi = VTOMI4(vp); 12761 12762 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12763 12764 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12765 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12766 } 12767 12768 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12769 if (!needrecov && ep->error) 12770 return; 12771 12772 if (needrecov) { 12773 bool_t abort = FALSE; 12774 12775 if (reopening_file == FALSE) { 12776 nfs4_bseqid_entry_t *bsep = NULL; 12777 12778 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12779 bsep = nfs4_create_bseqid_entry(oop, NULL, 12780 vp, 0, args.ctag, 12781 open_confirm_args->seqid); 12782 12783 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 12784 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL); 12785 if (bsep) { 12786 kmem_free(bsep, sizeof (*bsep)); 12787 if (num_bseqid_retryp && 12788 --(*num_bseqid_retryp) == 0) 12789 abort = TRUE; 12790 } 12791 } 12792 if ((ep->error == ETIMEDOUT || 12793 res.status == NFS4ERR_RESOURCE) && 12794 abort == FALSE && resend == FALSE) { 12795 if (!ep->error) 12796 (void) xdr_free(xdr_COMPOUND4res_clnt, 12797 (caddr_t)&res); 12798 12799 delay(SEC_TO_TICK(confirm_retry_sec)); 12800 goto recov_retry_confirm; 12801 } 12802 /* State may have changed so retry the entire OPEN op */ 12803 if (abort == FALSE) 12804 *retry_open = TRUE; 12805 else 12806 *retry_open = FALSE; 12807 if (!ep->error) 12808 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12809 return; 12810 } 12811 12812 if (res.status) { 12813 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12814 return; 12815 } 12816 12817 resop = &res.array[1]; /* open confirm res */ 12818 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12819 stateid, sizeof (*stateid)); 12820 12821 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12822 } 12823 12824 /* 12825 * Return the credentials associated with a client state object. The 12826 * caller is responsible for freeing the credentials. 12827 */ 12828 12829 static cred_t * 12830 state_to_cred(nfs4_open_stream_t *osp) 12831 { 12832 cred_t *cr; 12833 12834 /* 12835 * It's ok to not lock the open stream and open owner to get 12836 * the oo_cred since this is only written once (upon creation) 12837 * and will not change. 12838 */ 12839 cr = osp->os_open_owner->oo_cred; 12840 crhold(cr); 12841 12842 return (cr); 12843 } 12844 12845 /* 12846 * nfs4_find_sysid 12847 * 12848 * Find the sysid for the knetconfig associated with the given mi. 12849 */ 12850 static struct lm_sysid * 12851 nfs4_find_sysid(mntinfo4_t *mi) 12852 { 12853 ASSERT(nfs_zone() == mi->mi_zone); 12854 12855 /* 12856 * Switch from RDMA knconf to original mount knconf 12857 */ 12858 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12859 mi->mi_curr_serv->sv_hostname, NULL)); 12860 } 12861 12862 #ifdef DEBUG 12863 /* 12864 * Return a string version of the call type for easy reading. 12865 */ 12866 static char * 12867 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12868 { 12869 switch (ctype) { 12870 case NFS4_LCK_CTYPE_NORM: 12871 return ("NORMAL"); 12872 case NFS4_LCK_CTYPE_RECLAIM: 12873 return ("RECLAIM"); 12874 case NFS4_LCK_CTYPE_RESEND: 12875 return ("RESEND"); 12876 case NFS4_LCK_CTYPE_REINSTATE: 12877 return ("REINSTATE"); 12878 default: 12879 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12880 "type %d", ctype); 12881 return (""); 12882 } 12883 } 12884 #endif 12885 12886 /* 12887 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12888 * Unlock requests don't have an over-the-wire locktype, so we just return 12889 * something non-threatening. 12890 */ 12891 12892 static nfs_lock_type4 12893 flk_to_locktype(int cmd, int l_type) 12894 { 12895 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12896 12897 switch (l_type) { 12898 case F_UNLCK: 12899 return (READ_LT); 12900 case F_RDLCK: 12901 if (cmd == F_SETLK) 12902 return (READ_LT); 12903 else 12904 return (READW_LT); 12905 case F_WRLCK: 12906 if (cmd == F_SETLK) 12907 return (WRITE_LT); 12908 else 12909 return (WRITEW_LT); 12910 } 12911 panic("flk_to_locktype"); 12912 /*NOTREACHED*/ 12913 } 12914 12915 /* 12916 * Do some preliminary checks for nfs4frlock. 12917 */ 12918 static int 12919 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12920 u_offset_t offset) 12921 { 12922 int error = 0; 12923 12924 /* 12925 * If we are setting a lock, check that the file is opened 12926 * with the correct mode. 12927 */ 12928 if (cmd == F_SETLK || cmd == F_SETLKW) { 12929 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12930 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12931 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12932 "nfs4frlock_validate_args: file was opened with " 12933 "incorrect mode")); 12934 return (EBADF); 12935 } 12936 } 12937 12938 /* Convert the offset. It may need to be restored before returning. */ 12939 if (error = convoff(vp, flk, 0, offset)) { 12940 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12941 "nfs4frlock_validate_args: convoff => error= %d\n", 12942 error)); 12943 return (error); 12944 } 12945 12946 return (error); 12947 } 12948 12949 /* 12950 * Set the flock64's lm_sysid for nfs4frlock. 12951 */ 12952 static int 12953 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12954 { 12955 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12956 12957 /* Find the lm_sysid */ 12958 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12959 12960 if (*lspp == NULL) { 12961 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12962 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12963 return (ENOLCK); 12964 } 12965 12966 flk->l_sysid = lm_sysidt(*lspp); 12967 12968 return (0); 12969 } 12970 12971 /* 12972 * Do the remaining preliminary setup for nfs4frlock. 12973 */ 12974 static void 12975 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12976 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12977 cred_t **cred_otw) 12978 { 12979 /* 12980 * set tick_delay to the base delay time. 12981 * (NFS4_BASE_WAIT_TIME is in secs) 12982 */ 12983 12984 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000); 12985 12986 /* 12987 * If lock is relative to EOF, we need the newest length of the 12988 * file. Therefore invalidate the ATTR_CACHE. 12989 */ 12990 12991 *whencep = flk->l_whence; 12992 12993 if (*whencep == 2) /* SEEK_END */ 12994 PURGE_ATTRCACHE4(vp); 12995 12996 recov_statep->rs_flags = 0; 12997 recov_statep->rs_num_retry_despite_err = 0; 12998 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 12999 } 13000 13001 /* 13002 * Initialize and allocate the data structures necessary for 13003 * the nfs4frlock call. 13004 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 13005 */ 13006 static void 13007 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 13008 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 13009 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 13010 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 13011 { 13012 int argoplist_size; 13013 int num_ops = 2; 13014 13015 *retry = FALSE; 13016 *did_start_fop = FALSE; 13017 *skip_get_err = FALSE; 13018 lost_rqstp->lr_op = 0; 13019 argoplist_size = num_ops * sizeof (nfs_argop4); 13020 /* fill array with zero */ 13021 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 13022 13023 *argspp = argsp; 13024 *respp = NULL; 13025 13026 argsp->array_len = num_ops; 13027 argsp->array = *argopp; 13028 13029 /* initialize in case of error; will get real value down below */ 13030 argsp->ctag = TAG_NONE; 13031 13032 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 13033 *op_hintp = OH_LOCKU; 13034 else 13035 *op_hintp = OH_OTHER; 13036 } 13037 13038 /* 13039 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 13040 * the proper nfs4_server_t for this instance of nfs4frlock. 13041 * Returns 0 (success) or an errno value. 13042 */ 13043 static int 13044 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 13045 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 13046 bool_t *did_start_fop, bool_t *startrecovp) 13047 { 13048 int error = 0; 13049 rnode4_t *rp; 13050 13051 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13052 13053 if (ctype == NFS4_LCK_CTYPE_NORM) { 13054 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 13055 recov_statep, startrecovp); 13056 if (error) 13057 return (error); 13058 *did_start_fop = TRUE; 13059 } else { 13060 *did_start_fop = FALSE; 13061 *startrecovp = FALSE; 13062 } 13063 13064 if (!error) { 13065 rp = VTOR4(vp); 13066 13067 /* If the file failed recovery, just quit. */ 13068 mutex_enter(&rp->r_statelock); 13069 if (rp->r_flags & R4RECOVERR) { 13070 error = EIO; 13071 } 13072 mutex_exit(&rp->r_statelock); 13073 } 13074 13075 return (error); 13076 } 13077 13078 /* 13079 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 13080 * resend nfs4frlock call is initiated by the recovery framework. 13081 * Acquires the lop and oop seqid synchronization. 13082 */ 13083 static void 13084 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 13085 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 13086 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13087 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 13088 { 13089 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 13090 int error; 13091 13092 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 13093 (CE_NOTE, 13094 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 13095 ASSERT(resend_rqstp != NULL); 13096 ASSERT(resend_rqstp->lr_op == OP_LOCK || 13097 resend_rqstp->lr_op == OP_LOCKU); 13098 13099 *oopp = resend_rqstp->lr_oop; 13100 if (resend_rqstp->lr_oop) { 13101 open_owner_hold(resend_rqstp->lr_oop); 13102 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 13103 ASSERT(error == 0); /* recov thread always succeeds */ 13104 } 13105 13106 /* Must resend this lost lock/locku request. */ 13107 ASSERT(resend_rqstp->lr_lop != NULL); 13108 *lopp = resend_rqstp->lr_lop; 13109 lock_owner_hold(resend_rqstp->lr_lop); 13110 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 13111 ASSERT(error == 0); /* recov thread always succeeds */ 13112 13113 *ospp = resend_rqstp->lr_osp; 13114 if (*ospp) 13115 open_stream_hold(resend_rqstp->lr_osp); 13116 13117 if (resend_rqstp->lr_op == OP_LOCK) { 13118 LOCK4args *lock_args; 13119 13120 argop->argop = OP_LOCK; 13121 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 13122 lock_args->locktype = resend_rqstp->lr_locktype; 13123 lock_args->reclaim = 13124 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 13125 lock_args->offset = resend_rqstp->lr_flk->l_start; 13126 lock_args->length = resend_rqstp->lr_flk->l_len; 13127 if (lock_args->length == 0) 13128 lock_args->length = ~lock_args->length; 13129 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 13130 mi2clientid(mi), &lock_args->locker); 13131 13132 switch (resend_rqstp->lr_ctype) { 13133 case NFS4_LCK_CTYPE_RESEND: 13134 argsp->ctag = TAG_LOCK_RESEND; 13135 break; 13136 case NFS4_LCK_CTYPE_REINSTATE: 13137 argsp->ctag = TAG_LOCK_REINSTATE; 13138 break; 13139 case NFS4_LCK_CTYPE_RECLAIM: 13140 argsp->ctag = TAG_LOCK_RECLAIM; 13141 break; 13142 default: 13143 argsp->ctag = TAG_LOCK_UNKNOWN; 13144 break; 13145 } 13146 } else { 13147 LOCKU4args *locku_args; 13148 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 13149 13150 argop->argop = OP_LOCKU; 13151 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 13152 locku_args->locktype = READ_LT; 13153 locku_args->seqid = lop->lock_seqid + 1; 13154 mutex_enter(&lop->lo_lock); 13155 locku_args->lock_stateid = lop->lock_stateid; 13156 mutex_exit(&lop->lo_lock); 13157 locku_args->offset = resend_rqstp->lr_flk->l_start; 13158 locku_args->length = resend_rqstp->lr_flk->l_len; 13159 if (locku_args->length == 0) 13160 locku_args->length = ~locku_args->length; 13161 13162 switch (resend_rqstp->lr_ctype) { 13163 case NFS4_LCK_CTYPE_RESEND: 13164 argsp->ctag = TAG_LOCKU_RESEND; 13165 break; 13166 case NFS4_LCK_CTYPE_REINSTATE: 13167 argsp->ctag = TAG_LOCKU_REINSTATE; 13168 break; 13169 default: 13170 argsp->ctag = TAG_LOCK_UNKNOWN; 13171 break; 13172 } 13173 } 13174 } 13175 13176 /* 13177 * Setup the LOCKT4 arguments. 13178 */ 13179 static void 13180 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13181 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 13182 rnode4_t *rp) 13183 { 13184 LOCKT4args *lockt_args; 13185 13186 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 13187 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13188 argop->argop = OP_LOCKT; 13189 argsp->ctag = TAG_LOCKT; 13190 lockt_args = &argop->nfs_argop4_u.oplockt; 13191 13192 /* 13193 * The locktype will be READ_LT unless it's 13194 * a write lock. We do this because the Solaris 13195 * system call allows the combination of 13196 * F_UNLCK and F_GETLK* and so in that case the 13197 * unlock is mapped to a read. 13198 */ 13199 if (flk->l_type == F_WRLCK) 13200 lockt_args->locktype = WRITE_LT; 13201 else 13202 lockt_args->locktype = READ_LT; 13203 13204 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 13205 /* set the lock owner4 args */ 13206 nfs4_setlockowner_args(&lockt_args->owner, rp, 13207 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13208 flk->l_pid); 13209 lockt_args->offset = flk->l_start; 13210 lockt_args->length = flk->l_len; 13211 if (flk->l_len == 0) 13212 lockt_args->length = ~lockt_args->length; 13213 13214 *lockt_argsp = lockt_args; 13215 } 13216 13217 /* 13218 * If the client is holding a delegation, and the open stream to be used 13219 * with this lock request is a delegation open stream, then re-open the stream. 13220 * Sets the nfs4_error_t to all zeros unless the open stream has already 13221 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13222 * means the caller should retry (like a recovery retry). 13223 */ 13224 static void 13225 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13226 { 13227 open_delegation_type4 dt; 13228 bool_t reopen_needed, force; 13229 nfs4_open_stream_t *osp; 13230 open_claim_type4 oclaim; 13231 rnode4_t *rp = VTOR4(vp); 13232 mntinfo4_t *mi = VTOMI4(vp); 13233 13234 ASSERT(nfs_zone() == mi->mi_zone); 13235 13236 nfs4_error_zinit(ep); 13237 13238 mutex_enter(&rp->r_statev4_lock); 13239 dt = rp->r_deleg_type; 13240 mutex_exit(&rp->r_statev4_lock); 13241 13242 if (dt != OPEN_DELEGATE_NONE) { 13243 nfs4_open_owner_t *oop; 13244 13245 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13246 if (!oop) { 13247 ep->stat = NFS4ERR_IO; 13248 return; 13249 } 13250 /* returns with 'os_sync_lock' held */ 13251 osp = find_open_stream(oop, rp); 13252 if (!osp) { 13253 open_owner_rele(oop); 13254 ep->stat = NFS4ERR_IO; 13255 return; 13256 } 13257 13258 if (osp->os_failed_reopen) { 13259 NFS4_DEBUG((nfs4_open_stream_debug || 13260 nfs4_client_lock_debug), (CE_NOTE, 13261 "nfs4frlock_check_deleg: os_failed_reopen set " 13262 "for osp %p, cr %p, rp %s", (void *)osp, 13263 (void *)cr, rnode4info(rp))); 13264 mutex_exit(&osp->os_sync_lock); 13265 open_stream_rele(osp, rp); 13266 open_owner_rele(oop); 13267 ep->stat = NFS4ERR_IO; 13268 return; 13269 } 13270 13271 /* 13272 * Determine whether a reopen is needed. If this 13273 * is a delegation open stream, then send the open 13274 * to the server to give visibility to the open owner. 13275 * Even if it isn't a delegation open stream, we need 13276 * to check if the previous open CLAIM_DELEGATE_CUR 13277 * was sufficient. 13278 */ 13279 13280 reopen_needed = osp->os_delegation || 13281 ((lt == F_RDLCK && 13282 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13283 (lt == F_WRLCK && 13284 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13285 13286 mutex_exit(&osp->os_sync_lock); 13287 open_owner_rele(oop); 13288 13289 if (reopen_needed) { 13290 /* 13291 * Always use CLAIM_PREVIOUS after server reboot. 13292 * The server will reject CLAIM_DELEGATE_CUR if 13293 * it is used during the grace period. 13294 */ 13295 mutex_enter(&mi->mi_lock); 13296 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13297 oclaim = CLAIM_PREVIOUS; 13298 force = TRUE; 13299 } else { 13300 oclaim = CLAIM_DELEGATE_CUR; 13301 force = FALSE; 13302 } 13303 mutex_exit(&mi->mi_lock); 13304 13305 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13306 if (ep->error == EAGAIN) { 13307 nfs4_error_zinit(ep); 13308 ep->stat = NFS4ERR_DELAY; 13309 } 13310 } 13311 open_stream_rele(osp, rp); 13312 osp = NULL; 13313 } 13314 } 13315 13316 /* 13317 * Setup the LOCKU4 arguments. 13318 * Returns errors via the nfs4_error_t. 13319 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13320 * over-the-wire. The caller must release the 13321 * reference on *lopp. 13322 * NFS4ERR_DELAY caller should retry (like recovery retry) 13323 * (other) unrecoverable error. 13324 */ 13325 static void 13326 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13327 LOCKU4args **locku_argsp, flock64_t *flk, 13328 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13329 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13330 bool_t *skip_get_err, bool_t *go_otwp) 13331 { 13332 nfs4_lock_owner_t *lop = NULL; 13333 LOCKU4args *locku_args; 13334 pid_t pid; 13335 bool_t is_spec = FALSE; 13336 rnode4_t *rp = VTOR4(vp); 13337 13338 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13339 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13340 13341 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13342 if (ep->error || ep->stat) 13343 return; 13344 13345 argop->argop = OP_LOCKU; 13346 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13347 argsp->ctag = TAG_LOCKU_REINSTATE; 13348 else 13349 argsp->ctag = TAG_LOCKU; 13350 locku_args = &argop->nfs_argop4_u.oplocku; 13351 *locku_argsp = locku_args; 13352 13353 /* 13354 * XXX what should locku_args->locktype be? 13355 * setting to ALWAYS be READ_LT so at least 13356 * it is a valid locktype. 13357 */ 13358 13359 locku_args->locktype = READ_LT; 13360 13361 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13362 flk->l_pid; 13363 13364 /* 13365 * Get the lock owner stateid. If no lock owner 13366 * exists, return success. 13367 */ 13368 lop = find_lock_owner(rp, pid, LOWN_ANY); 13369 *lopp = lop; 13370 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13371 is_spec = TRUE; 13372 if (!lop || is_spec) { 13373 /* 13374 * No lock owner so no locks to unlock. 13375 * Return success. If there was a failed 13376 * reclaim earlier, the lock might still be 13377 * registered with the local locking code, 13378 * so notify it of the unlock. 13379 * 13380 * If the lockowner is using a special stateid, 13381 * then the original lock request (that created 13382 * this lockowner) was never successful, so we 13383 * have no lock to undo OTW. 13384 */ 13385 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13386 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13387 "(%ld) so return success", (long)pid)); 13388 13389 if (ctype == NFS4_LCK_CTYPE_NORM) 13390 flk->l_pid = curproc->p_pid; 13391 nfs4_register_lock_locally(vp, flk, flag, offset); 13392 /* 13393 * Release our hold and NULL out so final_cleanup 13394 * doesn't try to end a lock seqid sync we 13395 * never started. 13396 */ 13397 if (is_spec) { 13398 lock_owner_rele(lop); 13399 *lopp = NULL; 13400 } 13401 *skip_get_err = TRUE; 13402 *go_otwp = FALSE; 13403 return; 13404 } 13405 13406 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13407 if (ep->error == EAGAIN) { 13408 lock_owner_rele(lop); 13409 *lopp = NULL; 13410 return; 13411 } 13412 13413 mutex_enter(&lop->lo_lock); 13414 locku_args->lock_stateid = lop->lock_stateid; 13415 mutex_exit(&lop->lo_lock); 13416 locku_args->seqid = lop->lock_seqid + 1; 13417 13418 /* leave the ref count on lop, rele after RPC call */ 13419 13420 locku_args->offset = flk->l_start; 13421 locku_args->length = flk->l_len; 13422 if (flk->l_len == 0) 13423 locku_args->length = ~locku_args->length; 13424 13425 *go_otwp = TRUE; 13426 } 13427 13428 /* 13429 * Setup the LOCK4 arguments. 13430 * 13431 * Returns errors via the nfs4_error_t. 13432 * NFS4_OK no problems 13433 * NFS4ERR_DELAY caller should retry (like recovery retry) 13434 * (other) unrecoverable error 13435 */ 13436 static void 13437 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13438 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13439 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13440 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13441 { 13442 LOCK4args *lock_args; 13443 nfs4_open_owner_t *oop = NULL; 13444 nfs4_open_stream_t *osp = NULL; 13445 nfs4_lock_owner_t *lop = NULL; 13446 pid_t pid; 13447 rnode4_t *rp = VTOR4(vp); 13448 13449 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13450 13451 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13452 if (ep->error || ep->stat != NFS4_OK) 13453 return; 13454 13455 argop->argop = OP_LOCK; 13456 if (ctype == NFS4_LCK_CTYPE_NORM) 13457 argsp->ctag = TAG_LOCK; 13458 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13459 argsp->ctag = TAG_RELOCK; 13460 else 13461 argsp->ctag = TAG_LOCK_REINSTATE; 13462 lock_args = &argop->nfs_argop4_u.oplock; 13463 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13464 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13465 /* 13466 * Get the lock owner. If no lock owner exists, 13467 * create a 'temporary' one and grab the open seqid 13468 * synchronization (which puts a hold on the open 13469 * owner and open stream). 13470 * This also grabs the lock seqid synchronization. 13471 */ 13472 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13473 ep->stat = 13474 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13475 13476 if (ep->stat != NFS4_OK) 13477 goto out; 13478 13479 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13480 &lock_args->locker); 13481 13482 lock_args->offset = flk->l_start; 13483 lock_args->length = flk->l_len; 13484 if (flk->l_len == 0) 13485 lock_args->length = ~lock_args->length; 13486 *lock_argsp = lock_args; 13487 out: 13488 *oopp = oop; 13489 *ospp = osp; 13490 *lopp = lop; 13491 } 13492 13493 /* 13494 * After we get the reply from the server, record the proper information 13495 * for possible resend lock requests. 13496 * 13497 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13498 */ 13499 static void 13500 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13501 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13502 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13503 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13504 { 13505 bool_t unlock = (flk->l_type == F_UNLCK); 13506 13507 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13508 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13509 ctype == NFS4_LCK_CTYPE_REINSTATE); 13510 13511 if (error != 0 && !unlock) { 13512 NFS4_DEBUG((nfs4_lost_rqst_debug || 13513 nfs4_client_lock_debug), (CE_NOTE, 13514 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13515 " for lop %p", (void *)lop)); 13516 ASSERT(lop != NULL); 13517 mutex_enter(&lop->lo_lock); 13518 lop->lo_pending_rqsts = 1; 13519 mutex_exit(&lop->lo_lock); 13520 } 13521 13522 lost_rqstp->lr_putfirst = FALSE; 13523 lost_rqstp->lr_op = 0; 13524 13525 /* 13526 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13527 * recovery purposes so that the lock request that was sent 13528 * can be saved and re-issued later. Ditto for EIO from a forced 13529 * unmount. This is done to have the client's local locking state 13530 * match the v4 server's state; that is, the request was 13531 * potentially received and accepted by the server but the client 13532 * thinks it was not. 13533 */ 13534 if (error == ETIMEDOUT || error == EINTR || 13535 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13536 NFS4_DEBUG((nfs4_lost_rqst_debug || 13537 nfs4_client_lock_debug), (CE_NOTE, 13538 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13539 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13540 (void *)lop, (void *)oop, (void *)osp)); 13541 if (unlock) 13542 lost_rqstp->lr_op = OP_LOCKU; 13543 else { 13544 lost_rqstp->lr_op = OP_LOCK; 13545 lost_rqstp->lr_locktype = locktype; 13546 } 13547 /* 13548 * Objects are held and rele'd via the recovery code. 13549 * See nfs4_save_lost_rqst. 13550 */ 13551 lost_rqstp->lr_vp = vp; 13552 lost_rqstp->lr_dvp = NULL; 13553 lost_rqstp->lr_oop = oop; 13554 lost_rqstp->lr_osp = osp; 13555 lost_rqstp->lr_lop = lop; 13556 lost_rqstp->lr_cr = cr; 13557 switch (ctype) { 13558 case NFS4_LCK_CTYPE_NORM: 13559 flk->l_pid = ttoproc(curthread)->p_pid; 13560 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13561 break; 13562 case NFS4_LCK_CTYPE_REINSTATE: 13563 lost_rqstp->lr_putfirst = TRUE; 13564 lost_rqstp->lr_ctype = ctype; 13565 break; 13566 default: 13567 break; 13568 } 13569 lost_rqstp->lr_flk = flk; 13570 } 13571 } 13572 13573 /* 13574 * Update lop's seqid. Also update the seqid stored in a resend request, 13575 * if any. (Some recovery errors increment the seqid, and we may have to 13576 * send the resend request again.) 13577 */ 13578 13579 static void 13580 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13581 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13582 { 13583 if (lock_args) { 13584 if (lock_args->locker.new_lock_owner == TRUE) 13585 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13586 else { 13587 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13588 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13589 } 13590 } else if (locku_args) { 13591 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13592 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13593 } 13594 } 13595 13596 /* 13597 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13598 * COMPOUND4 args/res for calls that need to retry. 13599 * Switches the *cred_otwp to base_cr. 13600 */ 13601 static void 13602 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13603 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13604 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13605 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13606 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13607 { 13608 nfs4_open_owner_t *oop = *oopp; 13609 nfs4_open_stream_t *osp = *ospp; 13610 nfs4_lock_owner_t *lop = *lopp; 13611 nfs_argop4 *argop = (*argspp)->array; 13612 13613 if (*did_start_fop) { 13614 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13615 needrecov); 13616 *did_start_fop = FALSE; 13617 } 13618 ASSERT((*argspp)->array_len == 2); 13619 if (argop[1].argop == OP_LOCK) 13620 nfs4args_lock_free(&argop[1]); 13621 else if (argop[1].argop == OP_LOCKT) 13622 nfs4args_lockt_free(&argop[1]); 13623 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13624 if (!error) 13625 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13626 *argspp = NULL; 13627 *respp = NULL; 13628 13629 if (lop) { 13630 nfs4_end_lock_seqid_sync(lop); 13631 lock_owner_rele(lop); 13632 *lopp = NULL; 13633 } 13634 13635 /* need to free up the reference on osp for lock args */ 13636 if (osp != NULL) { 13637 open_stream_rele(osp, VTOR4(vp)); 13638 *ospp = NULL; 13639 } 13640 13641 /* need to free up the reference on oop for lock args */ 13642 if (oop != NULL) { 13643 nfs4_end_open_seqid_sync(oop); 13644 open_owner_rele(oop); 13645 *oopp = NULL; 13646 } 13647 13648 crfree(*cred_otwp); 13649 *cred_otwp = base_cr; 13650 crhold(*cred_otwp); 13651 } 13652 13653 /* 13654 * Function to process the client's recovery for nfs4frlock. 13655 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13656 * 13657 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13658 * COMPOUND4 args/res for calls that need to retry. 13659 * 13660 * Note: the rp's r_lkserlock is *not* dropped during this path. 13661 */ 13662 static bool_t 13663 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13664 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13665 LOCK4args *lock_args, LOCKU4args *locku_args, 13666 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13667 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13668 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13669 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13670 { 13671 nfs4_open_owner_t *oop = *oopp; 13672 nfs4_open_stream_t *osp = *ospp; 13673 nfs4_lock_owner_t *lop = *lopp; 13674 13675 bool_t abort, retry; 13676 13677 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13678 ASSERT((*argspp) != NULL); 13679 ASSERT((*respp) != NULL); 13680 if (lock_args || locku_args) 13681 ASSERT(lop != NULL); 13682 13683 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13684 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13685 13686 retry = TRUE; 13687 abort = FALSE; 13688 if (needrecov) { 13689 nfs4_bseqid_entry_t *bsep = NULL; 13690 nfs_opnum4 op; 13691 13692 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13693 13694 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13695 seqid4 seqid; 13696 13697 if (lock_args) { 13698 if (lock_args->locker.new_lock_owner == TRUE) 13699 seqid = lock_args->locker.locker4_u. 13700 open_owner.open_seqid; 13701 else 13702 seqid = lock_args->locker.locker4_u. 13703 lock_owner.lock_seqid; 13704 } else if (locku_args) { 13705 seqid = locku_args->seqid; 13706 } else { 13707 seqid = 0; 13708 } 13709 13710 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13711 flk->l_pid, (*argspp)->ctag, seqid); 13712 } 13713 13714 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13715 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13716 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13717 NULL, op, bsep, NULL, NULL); 13718 13719 if (bsep) 13720 kmem_free(bsep, sizeof (*bsep)); 13721 } 13722 13723 /* 13724 * Return that we do not want to retry the request for 3 cases: 13725 * 1. If we received EINTR or are bailing out because of a forced 13726 * unmount, we came into this code path just for the sake of 13727 * initiating recovery, we now need to return the error. 13728 * 2. If we have aborted recovery. 13729 * 3. We received NFS4ERR_BAD_SEQID. 13730 */ 13731 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13732 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13733 retry = FALSE; 13734 13735 if (*did_start_fop == TRUE) { 13736 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13737 needrecov); 13738 *did_start_fop = FALSE; 13739 } 13740 13741 if (retry == TRUE) { 13742 nfs_argop4 *argop; 13743 13744 argop = (*argspp)->array; 13745 ASSERT((*argspp)->array_len == 2); 13746 13747 if (argop[1].argop == OP_LOCK) 13748 nfs4args_lock_free(&argop[1]); 13749 else if (argop[1].argop == OP_LOCKT) 13750 nfs4args_lockt_free(&argop[1]); 13751 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13752 if (!ep->error) 13753 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13754 *respp = NULL; 13755 *argspp = NULL; 13756 } 13757 13758 if (lop != NULL) { 13759 nfs4_end_lock_seqid_sync(lop); 13760 lock_owner_rele(lop); 13761 } 13762 13763 *lopp = NULL; 13764 13765 /* need to free up the reference on osp for lock args */ 13766 if (osp != NULL) { 13767 open_stream_rele(osp, rp); 13768 *ospp = NULL; 13769 } 13770 13771 /* need to free up the reference on oop for lock args */ 13772 if (oop != NULL) { 13773 nfs4_end_open_seqid_sync(oop); 13774 open_owner_rele(oop); 13775 *oopp = NULL; 13776 } 13777 13778 return (retry); 13779 } 13780 13781 /* 13782 * Handles the successful reply from the server for nfs4frlock. 13783 */ 13784 static void 13785 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13786 vnode_t *vp, int flag, u_offset_t offset, 13787 nfs4_lost_rqst_t *resend_rqstp) 13788 { 13789 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13790 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13791 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13792 if (ctype == NFS4_LCK_CTYPE_NORM) { 13793 flk->l_pid = ttoproc(curthread)->p_pid; 13794 /* 13795 * We do not register lost locks locally in 13796 * the 'resend' case since the user/application 13797 * doesn't think we have the lock. 13798 */ 13799 ASSERT(!resend_rqstp); 13800 nfs4_register_lock_locally(vp, flk, flag, offset); 13801 } 13802 } 13803 } 13804 13805 /* 13806 * Handle the DENIED reply from the server for nfs4frlock. 13807 * Returns TRUE if we should retry the request; FALSE otherwise. 13808 * 13809 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13810 * COMPOUND4 args/res for calls that need to retry. Can also 13811 * drop and regrab the r_lkserlock. 13812 */ 13813 static bool_t 13814 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13815 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13816 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13817 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13818 nfs4_recov_state_t *recov_statep, int needrecov, 13819 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13820 clock_t *tick_delayp, short *whencep, int *errorp, 13821 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13822 bool_t *skip_get_err) 13823 { 13824 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13825 13826 if (lock_args) { 13827 nfs4_open_owner_t *oop = *oopp; 13828 nfs4_open_stream_t *osp = *ospp; 13829 nfs4_lock_owner_t *lop = *lopp; 13830 int intr; 13831 13832 /* 13833 * Blocking lock needs to sleep and retry from the request. 13834 * 13835 * Do not block and wait for 'resend' or 'reinstate' 13836 * lock requests, just return the error. 13837 * 13838 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13839 */ 13840 if (cmd == F_SETLKW) { 13841 rnode4_t *rp = VTOR4(vp); 13842 nfs_argop4 *argop = (*argspp)->array; 13843 13844 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13845 13846 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13847 recov_statep, needrecov); 13848 *did_start_fop = FALSE; 13849 ASSERT((*argspp)->array_len == 2); 13850 if (argop[1].argop == OP_LOCK) 13851 nfs4args_lock_free(&argop[1]); 13852 else if (argop[1].argop == OP_LOCKT) 13853 nfs4args_lockt_free(&argop[1]); 13854 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13855 if (*respp) 13856 (void) xdr_free(xdr_COMPOUND4res_clnt, 13857 (caddr_t)*respp); 13858 *argspp = NULL; 13859 *respp = NULL; 13860 nfs4_end_lock_seqid_sync(lop); 13861 lock_owner_rele(lop); 13862 *lopp = NULL; 13863 if (osp != NULL) { 13864 open_stream_rele(osp, rp); 13865 *ospp = NULL; 13866 } 13867 if (oop != NULL) { 13868 nfs4_end_open_seqid_sync(oop); 13869 open_owner_rele(oop); 13870 *oopp = NULL; 13871 } 13872 13873 nfs_rw_exit(&rp->r_lkserlock); 13874 13875 intr = nfs4_block_and_wait(tick_delayp, rp); 13876 13877 if (intr) { 13878 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13879 RW_WRITER, FALSE); 13880 *errorp = EINTR; 13881 return (FALSE); 13882 } 13883 13884 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13885 RW_WRITER, FALSE); 13886 13887 /* 13888 * Make sure we are still safe to lock with 13889 * regards to mmapping. 13890 */ 13891 if (!nfs4_safelock(vp, flk, cr)) { 13892 *errorp = EAGAIN; 13893 return (FALSE); 13894 } 13895 13896 return (TRUE); 13897 } 13898 if (ctype == NFS4_LCK_CTYPE_NORM) 13899 *errorp = EAGAIN; 13900 *skip_get_err = TRUE; 13901 flk->l_whence = 0; 13902 *whencep = 0; 13903 return (FALSE); 13904 } else if (lockt_args) { 13905 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13906 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13907 13908 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13909 flk, lockt_args); 13910 13911 /* according to NLM code */ 13912 *errorp = 0; 13913 *whencep = 0; 13914 *skip_get_err = TRUE; 13915 return (FALSE); 13916 } 13917 return (FALSE); 13918 } 13919 13920 /* 13921 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13922 */ 13923 static void 13924 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13925 { 13926 switch (resp->status) { 13927 case NFS4ERR_ACCESS: 13928 case NFS4ERR_ADMIN_REVOKED: 13929 case NFS4ERR_BADHANDLE: 13930 case NFS4ERR_BAD_RANGE: 13931 case NFS4ERR_BAD_SEQID: 13932 case NFS4ERR_BAD_STATEID: 13933 case NFS4ERR_BADXDR: 13934 case NFS4ERR_DEADLOCK: 13935 case NFS4ERR_DELAY: 13936 case NFS4ERR_EXPIRED: 13937 case NFS4ERR_FHEXPIRED: 13938 case NFS4ERR_GRACE: 13939 case NFS4ERR_INVAL: 13940 case NFS4ERR_ISDIR: 13941 case NFS4ERR_LEASE_MOVED: 13942 case NFS4ERR_LOCK_NOTSUPP: 13943 case NFS4ERR_LOCK_RANGE: 13944 case NFS4ERR_MOVED: 13945 case NFS4ERR_NOFILEHANDLE: 13946 case NFS4ERR_NO_GRACE: 13947 case NFS4ERR_OLD_STATEID: 13948 case NFS4ERR_OPENMODE: 13949 case NFS4ERR_RECLAIM_BAD: 13950 case NFS4ERR_RECLAIM_CONFLICT: 13951 case NFS4ERR_RESOURCE: 13952 case NFS4ERR_SERVERFAULT: 13953 case NFS4ERR_STALE: 13954 case NFS4ERR_STALE_CLIENTID: 13955 case NFS4ERR_STALE_STATEID: 13956 return; 13957 default: 13958 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13959 "nfs4frlock_results_default: got unrecognizable " 13960 "res.status %d", resp->status)); 13961 *errorp = NFS4ERR_INVAL; 13962 } 13963 } 13964 13965 /* 13966 * The lock request was successful, so update the client's state. 13967 */ 13968 static void 13969 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13970 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13971 vnode_t *vp, flock64_t *flk, cred_t *cr, 13972 nfs4_lost_rqst_t *resend_rqstp) 13973 { 13974 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13975 13976 if (lock_args) { 13977 LOCK4res *lock_res; 13978 13979 lock_res = &resop->nfs_resop4_u.oplock; 13980 /* update the stateid with server's response */ 13981 13982 if (lock_args->locker.new_lock_owner == TRUE) { 13983 mutex_enter(&lop->lo_lock); 13984 lop->lo_just_created = NFS4_PERM_CREATED; 13985 mutex_exit(&lop->lo_lock); 13986 } 13987 13988 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13989 13990 /* 13991 * If the lock was the result of a resending a lost 13992 * request, we've synched up the stateid and seqid 13993 * with the server, but now the server might be out of sync 13994 * with what the application thinks it has for locks. 13995 * Clean that up here. It's unclear whether we should do 13996 * this even if the filesystem has been forcibly unmounted. 13997 * For most servers, it's probably wasted effort, but 13998 * RFC3530 lets servers require that unlocks exactly match 13999 * the locks that are held. 14000 */ 14001 if (resend_rqstp != NULL && 14002 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 14003 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 14004 } else { 14005 flk->l_whence = 0; 14006 } 14007 } else if (locku_args) { 14008 LOCKU4res *locku_res; 14009 14010 locku_res = &resop->nfs_resop4_u.oplocku; 14011 14012 /* Update the stateid with the server's response */ 14013 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 14014 } else if (lockt_args) { 14015 /* Switch the lock type to express success, see fcntl */ 14016 flk->l_type = F_UNLCK; 14017 flk->l_whence = 0; 14018 } 14019 } 14020 14021 /* 14022 * Do final cleanup before exiting nfs4frlock. 14023 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 14024 * COMPOUND4 args/res for calls that haven't already. 14025 */ 14026 static void 14027 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 14028 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 14029 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 14030 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 14031 short whence, u_offset_t offset, struct lm_sysid *ls, 14032 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 14033 bool_t did_start_fop, bool_t skip_get_err, 14034 cred_t *cred_otw, cred_t *cred) 14035 { 14036 mntinfo4_t *mi = VTOMI4(vp); 14037 rnode4_t *rp = VTOR4(vp); 14038 int error = *errorp; 14039 nfs_argop4 *argop; 14040 int do_flush_pages = 0; 14041 14042 ASSERT(nfs_zone() == mi->mi_zone); 14043 /* 14044 * The client recovery code wants the raw status information, 14045 * so don't map the NFS status code to an errno value for 14046 * non-normal call types. 14047 */ 14048 if (ctype == NFS4_LCK_CTYPE_NORM) { 14049 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 14050 *errorp = geterrno4(resp->status); 14051 if (did_start_fop == TRUE) 14052 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 14053 needrecov); 14054 14055 /* 14056 * We've established a new lock on the server, so invalidate 14057 * the pages associated with the vnode to get the most up to 14058 * date pages from the server after acquiring the lock. We 14059 * want to be sure that the read operation gets the newest data. 14060 * N.B. 14061 * We used to do this in nfs4frlock_results_ok but that doesn't 14062 * work since VOP_PUTPAGE can call nfs4_commit which calls 14063 * nfs4_start_fop. We flush the pages below after calling 14064 * nfs4_end_fop above 14065 * The flush of the page cache must be done after 14066 * nfs4_end_open_seqid_sync() to avoid a 4-way hang. 14067 */ 14068 if (!error && resp && resp->status == NFS4_OK) 14069 do_flush_pages = 1; 14070 } 14071 if (argsp) { 14072 ASSERT(argsp->array_len == 2); 14073 argop = argsp->array; 14074 if (argop[1].argop == OP_LOCK) 14075 nfs4args_lock_free(&argop[1]); 14076 else if (argop[1].argop == OP_LOCKT) 14077 nfs4args_lockt_free(&argop[1]); 14078 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14079 if (resp) 14080 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 14081 } 14082 14083 /* free the reference on the lock owner */ 14084 if (lop != NULL) { 14085 nfs4_end_lock_seqid_sync(lop); 14086 lock_owner_rele(lop); 14087 } 14088 14089 /* need to free up the reference on osp for lock args */ 14090 if (osp != NULL) 14091 open_stream_rele(osp, rp); 14092 14093 /* need to free up the reference on oop for lock args */ 14094 if (oop != NULL) { 14095 nfs4_end_open_seqid_sync(oop); 14096 open_owner_rele(oop); 14097 } 14098 14099 if (do_flush_pages) 14100 nfs4_flush_pages(vp, cred); 14101 14102 (void) convoff(vp, flk, whence, offset); 14103 14104 lm_rel_sysid(ls); 14105 14106 /* 14107 * Record debug information in the event we get EINVAL. 14108 */ 14109 mutex_enter(&mi->mi_lock); 14110 if (*errorp == EINVAL && (lock_args || locku_args) && 14111 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 14112 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 14113 zcmn_err(getzoneid(), CE_NOTE, 14114 "%s operation failed with " 14115 "EINVAL probably since the server, %s," 14116 " doesn't support POSIX style locking", 14117 lock_args ? "LOCK" : "LOCKU", 14118 mi->mi_curr_serv->sv_hostname); 14119 mi->mi_flags |= MI4_LOCK_DEBUG; 14120 } 14121 } 14122 mutex_exit(&mi->mi_lock); 14123 14124 if (cred_otw) 14125 crfree(cred_otw); 14126 } 14127 14128 /* 14129 * This calls the server and the local locking code. 14130 * 14131 * Client locks are registerred locally by oring the sysid with 14132 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 14133 * We need to distinguish between the two to avoid collision in case one 14134 * machine is used as both client and server. 14135 * 14136 * Blocking lock requests will continually retry to acquire the lock 14137 * forever. 14138 * 14139 * The ctype is defined as follows: 14140 * NFS4_LCK_CTYPE_NORM: normal lock request. 14141 * 14142 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 14143 * recovery, get the pid from flk instead of curproc, and don't reregister 14144 * the lock locally. 14145 * 14146 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 14147 * that we will use the information passed in via resend_rqstp to setup the 14148 * lock/locku request. This resend is the exact same request as the 'lost 14149 * lock', and is initiated by the recovery framework. A successful resend 14150 * request can initiate one or more reinstate requests. 14151 * 14152 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 14153 * does not trigger additional reinstate requests. This lock call type is 14154 * set for setting the v4 server's locking state back to match what the 14155 * client's local locking state is in the event of a received 'lost lock'. 14156 * 14157 * Errors are returned via the nfs4_error_t parameter. 14158 */ 14159 void 14160 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 14161 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 14162 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 14163 { 14164 COMPOUND4args_clnt args, *argsp = NULL; 14165 COMPOUND4res_clnt res, *resp = NULL; 14166 nfs_argop4 *argop; 14167 nfs_resop4 *resop; 14168 rnode4_t *rp; 14169 int doqueue = 1; 14170 clock_t tick_delay; /* delay in clock ticks */ 14171 struct lm_sysid *ls; 14172 LOCK4args *lock_args = NULL; 14173 LOCKU4args *locku_args = NULL; 14174 LOCKT4args *lockt_args = NULL; 14175 nfs4_open_owner_t *oop = NULL; 14176 nfs4_open_stream_t *osp = NULL; 14177 nfs4_lock_owner_t *lop = NULL; 14178 bool_t needrecov = FALSE; 14179 nfs4_recov_state_t recov_state; 14180 short whence; 14181 nfs4_op_hint_t op_hint; 14182 nfs4_lost_rqst_t lost_rqst; 14183 bool_t retry = FALSE; 14184 bool_t did_start_fop = FALSE; 14185 bool_t skip_get_err = FALSE; 14186 cred_t *cred_otw = NULL; 14187 bool_t recovonly; /* just queue request */ 14188 int frc_no_reclaim = 0; 14189 #ifdef DEBUG 14190 char *name; 14191 #endif 14192 14193 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14194 14195 #ifdef DEBUG 14196 name = fn_name(VTOSV(vp)->sv_name); 14197 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 14198 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 14199 "length %"PRIu64", pid %d, sysid %d, call type %s, " 14200 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 14201 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 14202 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 14203 resend_rqstp ? "TRUE" : "FALSE")); 14204 kmem_free(name, MAXNAMELEN); 14205 #endif 14206 14207 nfs4_error_zinit(ep); 14208 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14209 if (ep->error) 14210 return; 14211 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14212 if (ep->error) 14213 return; 14214 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14215 vp, cr, &cred_otw); 14216 14217 recov_retry: 14218 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14219 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14220 rp = VTOR4(vp); 14221 14222 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14223 &did_start_fop, &recovonly); 14224 14225 if (ep->error) 14226 goto out; 14227 14228 if (recovonly) { 14229 /* 14230 * Leave the request for the recovery system to deal with. 14231 */ 14232 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14233 ASSERT(cmd != F_GETLK); 14234 ASSERT(flk->l_type == F_UNLCK); 14235 14236 nfs4_error_init(ep, EINTR); 14237 needrecov = TRUE; 14238 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14239 if (lop != NULL) { 14240 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14241 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14242 (void) nfs4_start_recovery(ep, 14243 VTOMI4(vp), vp, NULL, NULL, 14244 (lost_rqst.lr_op == OP_LOCK || 14245 lost_rqst.lr_op == OP_LOCKU) ? 14246 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL); 14247 lock_owner_rele(lop); 14248 lop = NULL; 14249 } 14250 flk->l_pid = curproc->p_pid; 14251 nfs4_register_lock_locally(vp, flk, flag, offset); 14252 goto out; 14253 } 14254 14255 /* putfh directory fh */ 14256 argop[0].argop = OP_CPUTFH; 14257 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14258 14259 /* 14260 * Set up the over-the-wire arguments and get references to the 14261 * open owner, etc. 14262 */ 14263 14264 if (ctype == NFS4_LCK_CTYPE_RESEND || 14265 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14266 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14267 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14268 } else { 14269 bool_t go_otw = TRUE; 14270 14271 ASSERT(resend_rqstp == NULL); 14272 14273 switch (cmd) { 14274 case F_GETLK: 14275 case F_O_GETLK: 14276 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14277 &lockt_args, argsp, flk, rp); 14278 break; 14279 case F_SETLKW: 14280 case F_SETLK: 14281 if (flk->l_type == F_UNLCK) 14282 nfs4frlock_setup_locku_args(ctype, 14283 &argop[1], &locku_args, flk, 14284 &lop, ep, argsp, 14285 vp, flag, offset, cr, 14286 &skip_get_err, &go_otw); 14287 else 14288 nfs4frlock_setup_lock_args(ctype, 14289 &lock_args, &oop, &osp, &lop, &argop[1], 14290 argsp, flk, cmd, vp, cr, ep); 14291 14292 if (ep->error) 14293 goto out; 14294 14295 switch (ep->stat) { 14296 case NFS4_OK: 14297 break; 14298 case NFS4ERR_DELAY: 14299 /* recov thread never gets this error */ 14300 ASSERT(resend_rqstp == NULL); 14301 ASSERT(did_start_fop); 14302 14303 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14304 &recov_state, TRUE); 14305 did_start_fop = FALSE; 14306 if (argop[1].argop == OP_LOCK) 14307 nfs4args_lock_free(&argop[1]); 14308 else if (argop[1].argop == OP_LOCKT) 14309 nfs4args_lockt_free(&argop[1]); 14310 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14311 argsp = NULL; 14312 goto recov_retry; 14313 default: 14314 ep->error = EIO; 14315 goto out; 14316 } 14317 break; 14318 default: 14319 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14320 "nfs4_frlock: invalid cmd %d", cmd)); 14321 ep->error = EINVAL; 14322 goto out; 14323 } 14324 14325 if (!go_otw) 14326 goto out; 14327 } 14328 14329 /* XXX should we use the local reclock as a cache ? */ 14330 /* 14331 * Unregister the lock with the local locking code before 14332 * contacting the server. This avoids a potential race where 14333 * another process gets notified that it has been granted a lock 14334 * before we can unregister ourselves locally. 14335 */ 14336 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14337 if (ctype == NFS4_LCK_CTYPE_NORM) 14338 flk->l_pid = ttoproc(curthread)->p_pid; 14339 nfs4_register_lock_locally(vp, flk, flag, offset); 14340 } 14341 14342 /* 14343 * Send the server the lock request. Continually loop with a delay 14344 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14345 */ 14346 resp = &res; 14347 14348 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14349 (CE_NOTE, 14350 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14351 rnode4info(rp))); 14352 14353 if (lock_args && frc_no_reclaim) { 14354 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14355 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14356 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14357 lock_args->reclaim = FALSE; 14358 if (did_reclaimp) 14359 *did_reclaimp = 0; 14360 } 14361 14362 /* 14363 * Do the OTW call. 14364 */ 14365 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14366 14367 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14368 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14369 14370 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14371 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14372 "nfs4frlock: needrecov %d", needrecov)); 14373 14374 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14375 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14376 args.ctag); 14377 14378 /* 14379 * Check if one of these mutually exclusive error cases has 14380 * happened: 14381 * need to swap credentials due to access error 14382 * recovery is needed 14383 * different error (only known case is missing Kerberos ticket) 14384 */ 14385 14386 if ((ep->error == EACCES || 14387 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14388 cred_otw != cr) { 14389 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14390 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14391 cr, &cred_otw); 14392 goto recov_retry; 14393 } 14394 14395 if (needrecov) { 14396 /* 14397 * LOCKT requests don't need to recover from lost 14398 * requests since they don't create/modify state. 14399 */ 14400 if ((ep->error == EINTR || 14401 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14402 lockt_args) 14403 goto out; 14404 /* 14405 * Do not attempt recovery for requests initiated by 14406 * the recovery framework. Let the framework redrive them. 14407 */ 14408 if (ctype != NFS4_LCK_CTYPE_NORM) 14409 goto out; 14410 else { 14411 ASSERT(resend_rqstp == NULL); 14412 } 14413 14414 nfs4frlock_save_lost_rqst(ctype, ep->error, 14415 flk_to_locktype(cmd, flk->l_type), 14416 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14417 14418 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14419 &resp, lock_args, locku_args, &oop, &osp, &lop, 14420 rp, vp, &recov_state, op_hint, &did_start_fop, 14421 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14422 14423 if (retry) { 14424 ASSERT(oop == NULL); 14425 ASSERT(osp == NULL); 14426 ASSERT(lop == NULL); 14427 goto recov_retry; 14428 } 14429 goto out; 14430 } 14431 14432 /* 14433 * Bail out if have reached this point with ep->error set. Can 14434 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14435 * This happens if Kerberos ticket has expired or has been 14436 * destroyed. 14437 */ 14438 if (ep->error != 0) 14439 goto out; 14440 14441 /* 14442 * Process the reply. 14443 */ 14444 switch (resp->status) { 14445 case NFS4_OK: 14446 resop = &resp->array[1]; 14447 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14448 resend_rqstp); 14449 /* 14450 * Have a successful lock operation, now update state. 14451 */ 14452 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14453 resop, lop, vp, flk, cr, resend_rqstp); 14454 break; 14455 14456 case NFS4ERR_DENIED: 14457 resop = &resp->array[1]; 14458 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14459 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14460 &recov_state, needrecov, &argsp, &resp, 14461 &tick_delay, &whence, &ep->error, resop, cr, 14462 &did_start_fop, &skip_get_err); 14463 14464 if (retry) { 14465 ASSERT(oop == NULL); 14466 ASSERT(osp == NULL); 14467 ASSERT(lop == NULL); 14468 goto recov_retry; 14469 } 14470 break; 14471 /* 14472 * If the server won't let us reclaim, fall-back to trying to lock 14473 * the file from scratch. Code elsewhere will check the changeinfo 14474 * to ensure the file hasn't been changed. 14475 */ 14476 case NFS4ERR_NO_GRACE: 14477 if (lock_args && lock_args->reclaim == TRUE) { 14478 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14479 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14480 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14481 frc_no_reclaim = 1; 14482 /* clean up before retrying */ 14483 needrecov = 0; 14484 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14485 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14486 &recov_state, op_hint, &did_start_fop, NULL, flk); 14487 goto recov_retry; 14488 } 14489 /* FALLTHROUGH */ 14490 14491 default: 14492 nfs4frlock_results_default(resp, &ep->error); 14493 break; 14494 } 14495 out: 14496 /* 14497 * Process and cleanup from error. Make interrupted unlock 14498 * requests look successful, since they will be handled by the 14499 * client recovery code. 14500 */ 14501 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14502 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14503 lock_args, locku_args, did_start_fop, 14504 skip_get_err, cred_otw, cr); 14505 14506 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14507 (cmd == F_SETLK || cmd == F_SETLKW)) 14508 ep->error = 0; 14509 } 14510 14511 /* 14512 * nfs4_safelock: 14513 * 14514 * Return non-zero if the given lock request can be handled without 14515 * violating the constraints on concurrent mapping and locking. 14516 */ 14517 14518 static int 14519 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14520 { 14521 rnode4_t *rp = VTOR4(vp); 14522 struct vattr va; 14523 int error; 14524 14525 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14526 ASSERT(rp->r_mapcnt >= 0); 14527 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14528 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14529 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14530 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14531 14532 if (rp->r_mapcnt == 0) 14533 return (1); /* always safe if not mapped */ 14534 14535 /* 14536 * If the file is already mapped and there are locks, then they 14537 * should be all safe locks. So adding or removing a lock is safe 14538 * as long as the new request is safe (i.e., whole-file, meaning 14539 * length and starting offset are both zero). 14540 */ 14541 14542 if (bfp->l_start != 0 || bfp->l_len != 0) { 14543 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14544 "cannot lock a memory mapped file unless locking the " 14545 "entire file: start %"PRIx64", len %"PRIx64, 14546 bfp->l_start, bfp->l_len)); 14547 return (0); 14548 } 14549 14550 /* mandatory locking and mapping don't mix */ 14551 va.va_mask = AT_MODE; 14552 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 14553 if (error != 0) { 14554 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14555 "getattr error %d", error)); 14556 return (0); /* treat errors conservatively */ 14557 } 14558 if (MANDLOCK(vp, va.va_mode)) { 14559 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14560 "cannot mandatory lock and mmap a file")); 14561 return (0); 14562 } 14563 14564 return (1); 14565 } 14566 14567 14568 /* 14569 * Register the lock locally within Solaris. 14570 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14571 * recording locks locally. 14572 * 14573 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14574 * are registered locally. 14575 */ 14576 void 14577 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14578 u_offset_t offset) 14579 { 14580 int oldsysid; 14581 int error; 14582 #ifdef DEBUG 14583 char *name; 14584 #endif 14585 14586 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14587 14588 #ifdef DEBUG 14589 name = fn_name(VTOSV(vp)->sv_name); 14590 NFS4_DEBUG(nfs4_client_lock_debug, 14591 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14592 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14593 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14594 flk->l_sysid)); 14595 kmem_free(name, MAXNAMELEN); 14596 #endif 14597 14598 /* register the lock with local locking */ 14599 oldsysid = flk->l_sysid; 14600 flk->l_sysid |= LM_SYSID_CLIENT; 14601 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14602 #ifdef DEBUG 14603 if (error != 0) { 14604 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14605 "nfs4_register_lock_locally: could not register with" 14606 " local locking")); 14607 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14608 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14609 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14610 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14611 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14612 flk->l_type, flk->l_start, flk->l_len)); 14613 (void) reclock(vp, flk, 0, flag, offset, NULL); 14614 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14615 "blocked by pid %d sysid 0x%x type %d " 14616 "off 0x%" PRIx64 " len 0x%" PRIx64, 14617 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14618 flk->l_len)); 14619 } 14620 #endif 14621 flk->l_sysid = oldsysid; 14622 } 14623 14624 /* 14625 * nfs4_lockrelease: 14626 * 14627 * Release any locks on the given vnode that are held by the current 14628 * process. Also removes the lock owner (if one exists) from the rnode's 14629 * list. 14630 */ 14631 static int 14632 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14633 { 14634 flock64_t ld; 14635 int ret, error; 14636 rnode4_t *rp; 14637 nfs4_lock_owner_t *lop; 14638 nfs4_recov_state_t recov_state; 14639 mntinfo4_t *mi; 14640 bool_t possible_orphan = FALSE; 14641 bool_t recovonly; 14642 14643 ASSERT((uintptr_t)vp > KERNELBASE); 14644 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14645 14646 rp = VTOR4(vp); 14647 mi = VTOMI4(vp); 14648 14649 /* 14650 * If we have not locked anything then we can 14651 * just return since we have no work to do. 14652 */ 14653 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14654 return (0); 14655 } 14656 14657 /* 14658 * We need to comprehend that another thread may 14659 * kick off recovery and the lock_owner we have stashed 14660 * in lop might be invalid so we should NOT cache it 14661 * locally! 14662 */ 14663 recov_state.rs_flags = 0; 14664 recov_state.rs_num_retry_despite_err = 0; 14665 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14666 &recovonly); 14667 if (error) { 14668 mutex_enter(&rp->r_statelock); 14669 rp->r_flags |= R4LODANGLERS; 14670 mutex_exit(&rp->r_statelock); 14671 return (error); 14672 } 14673 14674 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14675 14676 /* 14677 * Check if the lock owner might have a lock (request was sent but 14678 * no response was received). Also check if there are any remote 14679 * locks on the file. (In theory we shouldn't have to make this 14680 * second check if there's no lock owner, but for now we'll be 14681 * conservative and do it anyway.) If either condition is true, 14682 * send an unlock for the entire file to the server. 14683 * 14684 * Note that no explicit synchronization is needed here. At worst, 14685 * flk_has_remote_locks() will return a false positive, in which case 14686 * the unlock call wastes time but doesn't harm correctness. 14687 */ 14688 14689 if (lop) { 14690 mutex_enter(&lop->lo_lock); 14691 possible_orphan = lop->lo_pending_rqsts; 14692 mutex_exit(&lop->lo_lock); 14693 lock_owner_rele(lop); 14694 } 14695 14696 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14697 14698 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14699 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14700 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14701 (void *)lop)); 14702 14703 if (possible_orphan || flk_has_remote_locks(vp)) { 14704 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14705 ld.l_whence = 0; /* unlock from start of file */ 14706 ld.l_start = 0; 14707 ld.l_len = 0; /* do entire file */ 14708 14709 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, 14710 cr, NULL); 14711 14712 if (ret != 0) { 14713 /* 14714 * If VOP_FRLOCK fails, make sure we unregister 14715 * local locks before we continue. 14716 */ 14717 ld.l_pid = ttoproc(curthread)->p_pid; 14718 nfs4_register_lock_locally(vp, &ld, flag, offset); 14719 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14720 "nfs4_lockrelease: lock release error on vp" 14721 " %p: error %d.\n", (void *)vp, ret)); 14722 } 14723 } 14724 14725 recov_state.rs_flags = 0; 14726 recov_state.rs_num_retry_despite_err = 0; 14727 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14728 &recovonly); 14729 if (error) { 14730 mutex_enter(&rp->r_statelock); 14731 rp->r_flags |= R4LODANGLERS; 14732 mutex_exit(&rp->r_statelock); 14733 return (error); 14734 } 14735 14736 /* 14737 * So, here we're going to need to retrieve the lock-owner 14738 * again (in case recovery has done a switch-a-roo) and 14739 * remove it because we can. 14740 */ 14741 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14742 14743 if (lop) { 14744 nfs4_rnode_remove_lock_owner(rp, lop); 14745 lock_owner_rele(lop); 14746 } 14747 14748 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14749 return (0); 14750 } 14751 14752 /* 14753 * Wait for 'tick_delay' clock ticks. 14754 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14755 * NOTE: lock_lease_time is in seconds. 14756 * 14757 * XXX For future improvements, should implement a waiting queue scheme. 14758 */ 14759 static int 14760 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14761 { 14762 long milliseconds_delay; 14763 time_t lock_lease_time; 14764 14765 /* wait tick_delay clock ticks or siginteruptus */ 14766 if (delay_sig(*tick_delay)) { 14767 return (EINTR); 14768 } 14769 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14770 "reissue the lock request: blocked for %ld clock ticks: %ld " 14771 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14772 14773 /* get the lease time */ 14774 lock_lease_time = r2lease_time(rp); 14775 14776 /* drv_hztousec converts ticks to microseconds */ 14777 milliseconds_delay = drv_hztousec(*tick_delay) / 1000; 14778 if (milliseconds_delay < lock_lease_time * 1000) { 14779 *tick_delay = 2 * *tick_delay; 14780 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000) 14781 *tick_delay = drv_usectohz(lock_lease_time*1000*1000); 14782 } 14783 return (0); 14784 } 14785 14786 14787 void 14788 nfs4_vnops_init(void) 14789 { 14790 } 14791 14792 void 14793 nfs4_vnops_fini(void) 14794 { 14795 } 14796 14797 /* 14798 * Return a reference to the directory (parent) vnode for a given vnode, 14799 * using the saved pathname information and the directory file handle. The 14800 * caller is responsible for disposing of the reference. 14801 * Returns zero or an errno value. 14802 * 14803 * Caller should set need_start_op to FALSE if it is the recovery 14804 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14805 */ 14806 int 14807 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14808 { 14809 svnode_t *svnp; 14810 vnode_t *dvp = NULL; 14811 servinfo4_t *svp; 14812 nfs4_fname_t *mfname; 14813 int error; 14814 14815 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14816 14817 if (vp->v_flag & VROOT) { 14818 nfs4_sharedfh_t *sfh; 14819 nfs_fh4 fh; 14820 mntinfo4_t *mi; 14821 14822 ASSERT(vp->v_type == VREG); 14823 14824 mi = VTOMI4(vp); 14825 svp = mi->mi_curr_serv; 14826 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14827 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14828 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14829 sfh = sfh4_get(&fh, VTOMI4(vp)); 14830 nfs_rw_exit(&svp->sv_lock); 14831 mfname = mi->mi_fname; 14832 fn_hold(mfname); 14833 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14834 sfh4_rele(&sfh); 14835 14836 if (dvp->v_type == VNON) 14837 dvp->v_type = VDIR; 14838 *dvpp = dvp; 14839 return (0); 14840 } 14841 14842 svnp = VTOSV(vp); 14843 14844 if (svnp == NULL) { 14845 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14846 "shadow node is NULL")); 14847 return (EINVAL); 14848 } 14849 14850 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14851 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14852 "shadow node name or dfh val == NULL")); 14853 return (EINVAL); 14854 } 14855 14856 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14857 (int)need_start_op); 14858 if (error != 0) { 14859 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14860 "nfs4_make_dotdot returned %d", error)); 14861 return (error); 14862 } 14863 if (!dvp) { 14864 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14865 "nfs4_make_dotdot returned a NULL dvp")); 14866 return (EIO); 14867 } 14868 if (dvp->v_type == VNON) 14869 dvp->v_type = VDIR; 14870 ASSERT(dvp->v_type == VDIR); 14871 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14872 mutex_enter(&dvp->v_lock); 14873 dvp->v_flag |= V_XATTRDIR; 14874 mutex_exit(&dvp->v_lock); 14875 } 14876 *dvpp = dvp; 14877 return (0); 14878 } 14879 14880 /* 14881 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14882 * length that fnamep can accept, including the trailing null. 14883 * Returns 0 if okay, returns an errno value if there was a problem. 14884 */ 14885 14886 int 14887 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14888 { 14889 char *fn; 14890 int err = 0; 14891 servinfo4_t *svp; 14892 svnode_t *shvp; 14893 14894 /* 14895 * If the file being opened has VROOT set, then this is 14896 * a "file" mount. sv_name will not be interesting, so 14897 * go back to the servinfo4 to get the original mount 14898 * path and strip off all but the final edge. Otherwise 14899 * just return the name from the shadow vnode. 14900 */ 14901 14902 if (vp->v_flag & VROOT) { 14903 14904 svp = VTOMI4(vp)->mi_curr_serv; 14905 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14906 14907 fn = strrchr(svp->sv_path, '/'); 14908 if (fn == NULL) 14909 err = EINVAL; 14910 else 14911 fn++; 14912 } else { 14913 shvp = VTOSV(vp); 14914 fn = fn_name(shvp->sv_name); 14915 } 14916 14917 if (err == 0) 14918 if (strlen(fn) < maxlen) 14919 (void) strcpy(fnamep, fn); 14920 else 14921 err = ENAMETOOLONG; 14922 14923 if (vp->v_flag & VROOT) 14924 nfs_rw_exit(&svp->sv_lock); 14925 else 14926 kmem_free(fn, MAXNAMELEN); 14927 14928 return (err); 14929 } 14930 14931 /* 14932 * Bookkeeping for a close that doesn't need to go over the wire. 14933 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14934 * it is left at 1. 14935 */ 14936 void 14937 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14938 { 14939 rnode4_t *rp; 14940 mntinfo4_t *mi; 14941 14942 mi = VTOMI4(vp); 14943 rp = VTOR4(vp); 14944 14945 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14946 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14947 ASSERT(nfs_zone() == mi->mi_zone); 14948 ASSERT(mutex_owned(&osp->os_sync_lock)); 14949 ASSERT(*have_lockp); 14950 14951 if (!osp->os_valid || 14952 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14953 return; 14954 } 14955 14956 /* 14957 * This removes the reference obtained at OPEN; ie, 14958 * when the open stream structure was created. 14959 * 14960 * We don't have to worry about calling 'open_stream_rele' 14961 * since we our currently holding a reference to this 14962 * open stream which means the count can not go to 0 with 14963 * this decrement. 14964 */ 14965 ASSERT(osp->os_ref_count >= 2); 14966 osp->os_ref_count--; 14967 osp->os_valid = 0; 14968 mutex_exit(&osp->os_sync_lock); 14969 *have_lockp = 0; 14970 14971 nfs4_dec_state_ref_count(mi); 14972 } 14973 14974 /* 14975 * Close all remaining open streams on the rnode. These open streams 14976 * could be here because: 14977 * - The close attempted at either close or delmap failed 14978 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14979 * - Someone did mknod on a regular file but never opened it 14980 */ 14981 int 14982 nfs4close_all(vnode_t *vp, cred_t *cr) 14983 { 14984 nfs4_open_stream_t *osp; 14985 int error; 14986 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14987 rnode4_t *rp; 14988 14989 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14990 14991 error = 0; 14992 rp = VTOR4(vp); 14993 14994 /* 14995 * At this point, all we know is that the last time 14996 * someone called vn_rele, the count was 1. Since then, 14997 * the vnode could have been re-activated. We want to 14998 * loop through the open streams and close each one, but 14999 * we have to be careful since once we release the rnode 15000 * hash bucket lock, someone else is free to come in and 15001 * re-activate the rnode and add new open streams. The 15002 * strategy is take the rnode hash bucket lock, verify that 15003 * the count is still 1, grab the open stream off the 15004 * head of the list and mark it invalid, then release the 15005 * rnode hash bucket lock and proceed with that open stream. 15006 * This is ok because nfs4close_one() will acquire the proper 15007 * open/create to close/destroy synchronization for open 15008 * streams, and will ensure that if someone has reopened 15009 * the open stream after we've dropped the hash bucket lock 15010 * then we'll just simply return without destroying the 15011 * open stream. 15012 * Repeat until the list is empty. 15013 */ 15014 15015 for (;;) { 15016 15017 /* make sure vnode hasn't been reactivated */ 15018 rw_enter(&rp->r_hashq->r_lock, RW_READER); 15019 mutex_enter(&vp->v_lock); 15020 if (vp->v_count > 1) { 15021 mutex_exit(&vp->v_lock); 15022 rw_exit(&rp->r_hashq->r_lock); 15023 break; 15024 } 15025 /* 15026 * Grabbing r_os_lock before releasing v_lock prevents 15027 * a window where the rnode/open stream could get 15028 * reactivated (and os_force_close set to 0) before we 15029 * had a chance to set os_force_close to 1. 15030 */ 15031 mutex_enter(&rp->r_os_lock); 15032 mutex_exit(&vp->v_lock); 15033 15034 osp = list_head(&rp->r_open_streams); 15035 if (!osp) { 15036 /* nothing left to CLOSE OTW, so return */ 15037 mutex_exit(&rp->r_os_lock); 15038 rw_exit(&rp->r_hashq->r_lock); 15039 break; 15040 } 15041 15042 mutex_enter(&rp->r_statev4_lock); 15043 /* the file can't still be mem mapped */ 15044 ASSERT(rp->r_mapcnt == 0); 15045 if (rp->created_v4) 15046 rp->created_v4 = 0; 15047 mutex_exit(&rp->r_statev4_lock); 15048 15049 /* 15050 * Grab a ref on this open stream; nfs4close_one 15051 * will mark it as invalid 15052 */ 15053 mutex_enter(&osp->os_sync_lock); 15054 osp->os_ref_count++; 15055 osp->os_force_close = 1; 15056 mutex_exit(&osp->os_sync_lock); 15057 mutex_exit(&rp->r_os_lock); 15058 rw_exit(&rp->r_hashq->r_lock); 15059 15060 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 15061 15062 /* Update error if it isn't already non-zero */ 15063 if (error == 0) { 15064 if (e.error) 15065 error = e.error; 15066 else if (e.stat) 15067 error = geterrno4(e.stat); 15068 } 15069 15070 #ifdef DEBUG 15071 nfs4close_all_cnt++; 15072 #endif 15073 /* Release the ref on osp acquired above. */ 15074 open_stream_rele(osp, rp); 15075 15076 /* Proceed to the next open stream, if any */ 15077 } 15078 return (error); 15079 } 15080 15081 /* 15082 * nfs4close_one - close one open stream for a file if needed. 15083 * 15084 * "close_type" indicates which close path this is: 15085 * CLOSE_NORM: close initiated via VOP_CLOSE. 15086 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 15087 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 15088 * the close and release of client state for this open stream 15089 * (unless someone else has the open stream open). 15090 * CLOSE_RESEND: indicates the request is a replay of an earlier request 15091 * (e.g., due to abort because of a signal). 15092 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 15093 * 15094 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 15095 * recovery. Instead, the caller is expected to deal with retries. 15096 * 15097 * The caller can either pass in the osp ('provided_osp') or not. 15098 * 15099 * 'access_bits' represents the access we are closing/downgrading. 15100 * 15101 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 15102 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 15103 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 15104 * 15105 * Errors are returned via the nfs4_error_t. 15106 */ 15107 void 15108 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 15109 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 15110 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 15111 uint_t mmap_flags) 15112 { 15113 nfs4_open_owner_t *oop; 15114 nfs4_open_stream_t *osp = NULL; 15115 int retry = 0; 15116 int num_retries = NFS4_NUM_RECOV_RETRIES; 15117 rnode4_t *rp; 15118 mntinfo4_t *mi; 15119 nfs4_recov_state_t recov_state; 15120 cred_t *cred_otw = NULL; 15121 bool_t recovonly = FALSE; 15122 int isrecov; 15123 int force_close; 15124 int close_failed = 0; 15125 int did_dec_count = 0; 15126 int did_start_op = 0; 15127 int did_force_recovlock = 0; 15128 int did_start_seqid_sync = 0; 15129 int have_sync_lock = 0; 15130 15131 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15132 15133 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 15134 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 15135 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 15136 len, maxprot, mmap_flags, access_bits)); 15137 15138 nfs4_error_zinit(ep); 15139 rp = VTOR4(vp); 15140 mi = VTOMI4(vp); 15141 isrecov = (close_type == CLOSE_RESEND || 15142 close_type == CLOSE_AFTER_RESEND); 15143 15144 /* 15145 * First get the open owner. 15146 */ 15147 if (!provided_osp) { 15148 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 15149 } else { 15150 oop = provided_osp->os_open_owner; 15151 ASSERT(oop != NULL); 15152 open_owner_hold(oop); 15153 } 15154 15155 if (!oop) { 15156 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15157 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 15158 "close type %d", (void *)rp, (void *)mi, (void *)cr, 15159 (void *)provided_osp, close_type)); 15160 ep->error = EIO; 15161 goto out; 15162 } 15163 15164 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 15165 recov_retry: 15166 osp = NULL; 15167 close_failed = 0; 15168 force_close = (close_type == CLOSE_FORCE); 15169 retry = 0; 15170 did_start_op = 0; 15171 did_force_recovlock = 0; 15172 did_start_seqid_sync = 0; 15173 have_sync_lock = 0; 15174 recovonly = FALSE; 15175 recov_state.rs_flags = 0; 15176 recov_state.rs_num_retry_despite_err = 0; 15177 15178 /* 15179 * Second synchronize with recovery. 15180 */ 15181 if (!isrecov) { 15182 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 15183 &recov_state, &recovonly); 15184 if (!ep->error) { 15185 did_start_op = 1; 15186 } else { 15187 close_failed = 1; 15188 /* 15189 * If we couldn't get start_fop, but have to 15190 * cleanup state, then at least acquire the 15191 * mi_recovlock so we can synchronize with 15192 * recovery. 15193 */ 15194 if (close_type == CLOSE_FORCE) { 15195 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 15196 RW_READER, FALSE); 15197 did_force_recovlock = 1; 15198 } else 15199 goto out; 15200 } 15201 } 15202 15203 /* 15204 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15205 * set 'recovonly' to TRUE since most likely this is due to 15206 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15207 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15208 * to retry, causing us to loop until recovery finishes. Plus we 15209 * don't need protection over the open seqid since we're not going 15210 * OTW, hence don't need to use the seqid. 15211 */ 15212 if (recovonly == FALSE) { 15213 /* need to grab the open owner sync before 'os_sync_lock' */ 15214 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15215 if (ep->error == EAGAIN) { 15216 ASSERT(!isrecov); 15217 if (did_start_op) 15218 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15219 &recov_state, TRUE); 15220 if (did_force_recovlock) 15221 nfs_rw_exit(&mi->mi_recovlock); 15222 goto recov_retry; 15223 } 15224 did_start_seqid_sync = 1; 15225 } 15226 15227 /* 15228 * Third get an open stream and acquire 'os_sync_lock' to 15229 * sychronize the opening/creating of an open stream with the 15230 * closing/destroying of an open stream. 15231 */ 15232 if (!provided_osp) { 15233 /* returns with 'os_sync_lock' held */ 15234 osp = find_open_stream(oop, rp); 15235 if (!osp) { 15236 ep->error = EIO; 15237 goto out; 15238 } 15239 } else { 15240 osp = provided_osp; 15241 open_stream_hold(osp); 15242 mutex_enter(&osp->os_sync_lock); 15243 } 15244 have_sync_lock = 1; 15245 15246 ASSERT(oop == osp->os_open_owner); 15247 15248 /* 15249 * Fourth, do any special pre-OTW CLOSE processing 15250 * based on the specific close type. 15251 */ 15252 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15253 !did_dec_count) { 15254 ASSERT(osp->os_open_ref_count > 0); 15255 osp->os_open_ref_count--; 15256 did_dec_count = 1; 15257 if (osp->os_open_ref_count == 0) 15258 osp->os_final_close = 1; 15259 } 15260 15261 if (close_type == CLOSE_FORCE) { 15262 /* see if somebody reopened the open stream. */ 15263 if (!osp->os_force_close) { 15264 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15265 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15266 "was reopened, vp %p", (void *)osp, (void *)vp)); 15267 ep->error = 0; 15268 ep->stat = NFS4_OK; 15269 goto out; 15270 } 15271 15272 if (!osp->os_final_close && !did_dec_count) { 15273 osp->os_open_ref_count--; 15274 did_dec_count = 1; 15275 } 15276 15277 /* 15278 * We can't depend on os_open_ref_count being 0 due to the 15279 * way executables are opened (VN_RELE to match a VOP_OPEN). 15280 */ 15281 #ifdef NOTYET 15282 ASSERT(osp->os_open_ref_count == 0); 15283 #endif 15284 if (osp->os_open_ref_count != 0) { 15285 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15286 "nfs4close_one: should panic here on an " 15287 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15288 "since this is probably the exec problem.")); 15289 15290 osp->os_open_ref_count = 0; 15291 } 15292 15293 /* 15294 * There is the possibility that nfs4close_one() 15295 * for close_type == CLOSE_DELMAP couldn't find the 15296 * open stream, thus couldn't decrement its os_mapcnt; 15297 * therefore we can't use this ASSERT yet. 15298 */ 15299 #ifdef NOTYET 15300 ASSERT(osp->os_mapcnt == 0); 15301 #endif 15302 osp->os_mapcnt = 0; 15303 } 15304 15305 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15306 ASSERT(osp->os_mapcnt >= btopr(len)); 15307 15308 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15309 osp->os_mmap_write -= btopr(len); 15310 if (maxprot & PROT_READ) 15311 osp->os_mmap_read -= btopr(len); 15312 if (maxprot & PROT_EXEC) 15313 osp->os_mmap_read -= btopr(len); 15314 /* mirror the PROT_NONE check in nfs4_addmap() */ 15315 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15316 !(maxprot & PROT_EXEC)) 15317 osp->os_mmap_read -= btopr(len); 15318 osp->os_mapcnt -= btopr(len); 15319 did_dec_count = 1; 15320 } 15321 15322 if (recovonly) { 15323 nfs4_lost_rqst_t lost_rqst; 15324 15325 /* request should not already be in recovery queue */ 15326 ASSERT(lrp == NULL); 15327 nfs4_error_init(ep, EINTR); 15328 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15329 osp, cred_otw, vp); 15330 mutex_exit(&osp->os_sync_lock); 15331 have_sync_lock = 0; 15332 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15333 lost_rqst.lr_op == OP_CLOSE ? 15334 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL); 15335 close_failed = 1; 15336 force_close = 0; 15337 goto close_cleanup; 15338 } 15339 15340 /* 15341 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15342 * we stopped operating on the open owner's <old oo_name, old seqid> 15343 * space, which means we stopped operating on the open stream 15344 * too. So don't go OTW (as the seqid is likely bad, and the 15345 * stateid could be stale, potentially triggering a false 15346 * setclientid), and just clean up the client's internal state. 15347 */ 15348 if (osp->os_orig_oo_name != oop->oo_name) { 15349 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15350 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15351 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15352 "oo_name %" PRIx64")", 15353 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15354 oop->oo_name)); 15355 close_failed = 1; 15356 } 15357 15358 /* If the file failed recovery, just quit. */ 15359 mutex_enter(&rp->r_statelock); 15360 if (rp->r_flags & R4RECOVERR) { 15361 close_failed = 1; 15362 } 15363 mutex_exit(&rp->r_statelock); 15364 15365 /* 15366 * If the force close path failed to obtain start_fop 15367 * then skip the OTW close and just remove the state. 15368 */ 15369 if (close_failed) 15370 goto close_cleanup; 15371 15372 /* 15373 * Fifth, check to see if there are still mapped pages or other 15374 * opens using this open stream. If there are then we can't 15375 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15376 */ 15377 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15378 nfs4_lost_rqst_t new_lost_rqst; 15379 bool_t needrecov = FALSE; 15380 cred_t *odg_cred_otw = NULL; 15381 seqid4 open_dg_seqid = 0; 15382 15383 if (osp->os_delegation) { 15384 /* 15385 * If this open stream was never OPENed OTW then we 15386 * surely can't DOWNGRADE it (especially since the 15387 * osp->open_stateid is really a delegation stateid 15388 * when os_delegation is 1). 15389 */ 15390 if (access_bits & FREAD) 15391 osp->os_share_acc_read--; 15392 if (access_bits & FWRITE) 15393 osp->os_share_acc_write--; 15394 osp->os_share_deny_none--; 15395 nfs4_error_zinit(ep); 15396 goto out; 15397 } 15398 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15399 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15400 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15401 if (needrecov && !isrecov) { 15402 bool_t abort; 15403 nfs4_bseqid_entry_t *bsep = NULL; 15404 15405 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15406 bsep = nfs4_create_bseqid_entry(oop, NULL, 15407 vp, 0, 15408 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15409 open_dg_seqid); 15410 15411 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15412 oop, osp, odg_cred_otw, vp, access_bits, 0); 15413 mutex_exit(&osp->os_sync_lock); 15414 have_sync_lock = 0; 15415 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15416 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15417 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15418 bsep, NULL, NULL); 15419 if (odg_cred_otw) 15420 crfree(odg_cred_otw); 15421 if (bsep) 15422 kmem_free(bsep, sizeof (*bsep)); 15423 15424 if (abort == TRUE) 15425 goto out; 15426 15427 if (did_start_seqid_sync) { 15428 nfs4_end_open_seqid_sync(oop); 15429 did_start_seqid_sync = 0; 15430 } 15431 open_stream_rele(osp, rp); 15432 15433 if (did_start_op) 15434 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15435 &recov_state, FALSE); 15436 if (did_force_recovlock) 15437 nfs_rw_exit(&mi->mi_recovlock); 15438 15439 goto recov_retry; 15440 } else { 15441 if (odg_cred_otw) 15442 crfree(odg_cred_otw); 15443 } 15444 goto out; 15445 } 15446 15447 /* 15448 * If this open stream was created as the results of an open 15449 * while holding a delegation, then just release it; no need 15450 * to do an OTW close. Otherwise do a "normal" OTW close. 15451 */ 15452 if (osp->os_delegation) { 15453 nfs4close_notw(vp, osp, &have_sync_lock); 15454 nfs4_error_zinit(ep); 15455 goto out; 15456 } 15457 15458 /* 15459 * If this stream is not valid, we're done. 15460 */ 15461 if (!osp->os_valid) { 15462 nfs4_error_zinit(ep); 15463 goto out; 15464 } 15465 15466 /* 15467 * Last open or mmap ref has vanished, need to do an OTW close. 15468 * First check to see if a close is still necessary. 15469 */ 15470 if (osp->os_failed_reopen) { 15471 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15472 "don't close OTW osp %p since reopen failed.", 15473 (void *)osp)); 15474 /* 15475 * Reopen of the open stream failed, hence the 15476 * stateid of the open stream is invalid/stale, and 15477 * sending this OTW would incorrectly cause another 15478 * round of recovery. In this case, we need to set 15479 * the 'os_valid' bit to 0 so another thread doesn't 15480 * come in and re-open this open stream before 15481 * this "closing" thread cleans up state (decrementing 15482 * the nfs4_server_t's state_ref_count and decrementing 15483 * the os_ref_count). 15484 */ 15485 osp->os_valid = 0; 15486 /* 15487 * This removes the reference obtained at OPEN; ie, 15488 * when the open stream structure was created. 15489 * 15490 * We don't have to worry about calling 'open_stream_rele' 15491 * since we our currently holding a reference to this 15492 * open stream which means the count can not go to 0 with 15493 * this decrement. 15494 */ 15495 ASSERT(osp->os_ref_count >= 2); 15496 osp->os_ref_count--; 15497 nfs4_error_zinit(ep); 15498 close_failed = 0; 15499 goto close_cleanup; 15500 } 15501 15502 ASSERT(osp->os_ref_count > 1); 15503 15504 /* 15505 * Sixth, try the CLOSE OTW. 15506 */ 15507 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15508 close_type, ep, &have_sync_lock); 15509 15510 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15511 /* 15512 * Let the recovery thread be responsible for 15513 * removing the state for CLOSE. 15514 */ 15515 close_failed = 1; 15516 force_close = 0; 15517 retry = 0; 15518 } 15519 15520 /* See if we need to retry with a different cred */ 15521 if ((ep->error == EACCES || 15522 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15523 cred_otw != cr) { 15524 crfree(cred_otw); 15525 cred_otw = cr; 15526 crhold(cred_otw); 15527 retry = 1; 15528 } 15529 15530 if (ep->error || ep->stat) 15531 close_failed = 1; 15532 15533 if (retry && !isrecov && num_retries-- > 0) { 15534 if (have_sync_lock) { 15535 mutex_exit(&osp->os_sync_lock); 15536 have_sync_lock = 0; 15537 } 15538 if (did_start_seqid_sync) { 15539 nfs4_end_open_seqid_sync(oop); 15540 did_start_seqid_sync = 0; 15541 } 15542 open_stream_rele(osp, rp); 15543 15544 if (did_start_op) 15545 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15546 &recov_state, FALSE); 15547 if (did_force_recovlock) 15548 nfs_rw_exit(&mi->mi_recovlock); 15549 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15550 "nfs4close_one: need to retry the close " 15551 "operation")); 15552 goto recov_retry; 15553 } 15554 close_cleanup: 15555 /* 15556 * Seventh and lastly, process our results. 15557 */ 15558 if (close_failed && force_close) { 15559 /* 15560 * It's ok to drop and regrab the 'os_sync_lock' since 15561 * nfs4close_notw() will recheck to make sure the 15562 * "close"/removal of state should happen. 15563 */ 15564 if (!have_sync_lock) { 15565 mutex_enter(&osp->os_sync_lock); 15566 have_sync_lock = 1; 15567 } 15568 /* 15569 * This is last call, remove the ref on the open 15570 * stream created by open and clean everything up. 15571 */ 15572 osp->os_pending_close = 0; 15573 nfs4close_notw(vp, osp, &have_sync_lock); 15574 nfs4_error_zinit(ep); 15575 } 15576 15577 if (!close_failed) { 15578 if (have_sync_lock) { 15579 osp->os_pending_close = 0; 15580 mutex_exit(&osp->os_sync_lock); 15581 have_sync_lock = 0; 15582 } else { 15583 mutex_enter(&osp->os_sync_lock); 15584 osp->os_pending_close = 0; 15585 mutex_exit(&osp->os_sync_lock); 15586 } 15587 if (did_start_op && recov_state.rs_sp != NULL) { 15588 mutex_enter(&recov_state.rs_sp->s_lock); 15589 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15590 mutex_exit(&recov_state.rs_sp->s_lock); 15591 } else { 15592 nfs4_dec_state_ref_count(mi); 15593 } 15594 nfs4_error_zinit(ep); 15595 } 15596 15597 out: 15598 if (have_sync_lock) 15599 mutex_exit(&osp->os_sync_lock); 15600 if (did_start_op) 15601 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15602 recovonly ? TRUE : FALSE); 15603 if (did_force_recovlock) 15604 nfs_rw_exit(&mi->mi_recovlock); 15605 if (cred_otw) 15606 crfree(cred_otw); 15607 if (osp) 15608 open_stream_rele(osp, rp); 15609 if (oop) { 15610 if (did_start_seqid_sync) 15611 nfs4_end_open_seqid_sync(oop); 15612 open_owner_rele(oop); 15613 } 15614 } 15615 15616 /* 15617 * Convert information returned by the server in the LOCK4denied 15618 * structure to the form required by fcntl. 15619 */ 15620 static void 15621 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15622 { 15623 nfs4_lo_name_t *lo; 15624 15625 #ifdef DEBUG 15626 if (denied_to_flk_debug) { 15627 lockt_denied_debug = lockt_denied; 15628 debug_enter("lockt_denied"); 15629 } 15630 #endif 15631 15632 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15633 flk->l_whence = 0; /* aka SEEK_SET */ 15634 flk->l_start = lockt_denied->offset; 15635 flk->l_len = lockt_denied->length; 15636 15637 /* 15638 * If the blocking clientid matches our client id, then we can 15639 * interpret the lockowner (since we built it). If not, then 15640 * fabricate a sysid and pid. Note that the l_sysid field 15641 * in *flk already has the local sysid. 15642 */ 15643 15644 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15645 15646 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15647 lo = (nfs4_lo_name_t *) 15648 lockt_denied->owner.owner_val; 15649 15650 flk->l_pid = lo->ln_pid; 15651 } else { 15652 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15653 "denied_to_flk: bad lock owner length\n")); 15654 15655 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15656 } 15657 } else { 15658 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15659 "denied_to_flk: foreign clientid\n")); 15660 15661 /* 15662 * Construct a new sysid which should be different from 15663 * sysids of other systems. 15664 */ 15665 15666 flk->l_sysid++; 15667 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15668 } 15669 } 15670 15671 static pid_t 15672 lo_to_pid(lock_owner4 *lop) 15673 { 15674 pid_t pid = 0; 15675 uchar_t *cp; 15676 int i; 15677 15678 cp = (uchar_t *)&lop->clientid; 15679 15680 for (i = 0; i < sizeof (lop->clientid); i++) 15681 pid += (pid_t)*cp++; 15682 15683 cp = (uchar_t *)lop->owner_val; 15684 15685 for (i = 0; i < lop->owner_len; i++) 15686 pid += (pid_t)*cp++; 15687 15688 return (pid); 15689 } 15690 15691 /* 15692 * Given a lock pointer, returns the length of that lock. 15693 * "end" is the last locked offset the "l_len" covers from 15694 * the start of the lock. 15695 */ 15696 static off64_t 15697 lock_to_end(flock64_t *lock) 15698 { 15699 off64_t lock_end; 15700 15701 if (lock->l_len == 0) 15702 lock_end = (off64_t)MAXEND; 15703 else 15704 lock_end = lock->l_start + lock->l_len - 1; 15705 15706 return (lock_end); 15707 } 15708 15709 /* 15710 * Given the end of a lock, it will return you the length "l_len" for that lock. 15711 */ 15712 static off64_t 15713 end_to_len(off64_t start, off64_t end) 15714 { 15715 off64_t lock_len; 15716 15717 ASSERT(end >= start); 15718 if (end == MAXEND) 15719 lock_len = 0; 15720 else 15721 lock_len = end - start + 1; 15722 15723 return (lock_len); 15724 } 15725 15726 /* 15727 * On given end for a lock it determines if it is the last locked offset 15728 * or not, if so keeps it as is, else adds one to return the length for 15729 * valid start. 15730 */ 15731 static off64_t 15732 start_check(off64_t x) 15733 { 15734 if (x == MAXEND) 15735 return (x); 15736 else 15737 return (x + 1); 15738 } 15739 15740 /* 15741 * See if these two locks overlap, and if so return 1; 15742 * otherwise, return 0. 15743 */ 15744 static int 15745 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15746 { 15747 off64_t llfp_end, curfp_end; 15748 15749 llfp_end = lock_to_end(llfp); 15750 curfp_end = lock_to_end(curfp); 15751 15752 if (((llfp_end >= curfp->l_start) && 15753 (llfp->l_start <= curfp->l_start)) || 15754 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15755 return (1); 15756 return (0); 15757 } 15758 15759 /* 15760 * Determine what the intersecting lock region is, and add that to the 15761 * 'nl_llpp' locklist in increasing order (by l_start). 15762 */ 15763 static void 15764 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15765 locklist_t **nl_llpp, vnode_t *vp) 15766 { 15767 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15768 off64_t lost_flp_end, local_flp_end, len, start; 15769 15770 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15771 15772 if (!locks_intersect(lost_flp, local_flp)) 15773 return; 15774 15775 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15776 "locks intersect")); 15777 15778 lost_flp_end = lock_to_end(lost_flp); 15779 local_flp_end = lock_to_end(local_flp); 15780 15781 /* Find the starting point of the intersecting region */ 15782 if (local_flp->l_start > lost_flp->l_start) 15783 start = local_flp->l_start; 15784 else 15785 start = lost_flp->l_start; 15786 15787 /* Find the lenght of the intersecting region */ 15788 if (lost_flp_end < local_flp_end) 15789 len = end_to_len(start, lost_flp_end); 15790 else 15791 len = end_to_len(start, local_flp_end); 15792 15793 /* 15794 * Prepare the flock structure for the intersection found and insert 15795 * it into the new list in increasing l_start order. This list contains 15796 * intersections of locks registered by the client with the local host 15797 * and the lost lock. 15798 * The lock type of this lock is the same as that of the local_flp. 15799 */ 15800 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15801 intersect_llp->ll_flock.l_start = start; 15802 intersect_llp->ll_flock.l_len = len; 15803 intersect_llp->ll_flock.l_type = local_flp->l_type; 15804 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15805 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15806 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15807 intersect_llp->ll_vp = vp; 15808 15809 tmp_fllp = *nl_llpp; 15810 cur_fllp = NULL; 15811 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15812 intersect_llp->ll_flock.l_start) { 15813 cur_fllp = tmp_fllp; 15814 tmp_fllp = tmp_fllp->ll_next; 15815 } 15816 if (cur_fllp == NULL) { 15817 /* first on the list */ 15818 intersect_llp->ll_next = *nl_llpp; 15819 *nl_llpp = intersect_llp; 15820 } else { 15821 intersect_llp->ll_next = cur_fllp->ll_next; 15822 cur_fllp->ll_next = intersect_llp; 15823 } 15824 15825 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15826 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15827 intersect_llp->ll_flock.l_start, 15828 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15829 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15830 } 15831 15832 /* 15833 * Our local locking current state is potentially different than 15834 * what the NFSv4 server thinks we have due to a lost lock that was 15835 * resent and then received. We need to reset our "NFSv4" locking 15836 * state to match the current local locking state for this pid since 15837 * that is what the user/application sees as what the world is. 15838 * 15839 * We cannot afford to drop the open/lock seqid sync since then we can 15840 * get confused about what the current local locking state "is" versus 15841 * "was". 15842 * 15843 * If we are unable to fix up the locks, we send SIGLOST to the affected 15844 * process. This is not done if the filesystem has been forcibly 15845 * unmounted, in case the process has already exited and a new process 15846 * exists with the same pid. 15847 */ 15848 static void 15849 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15850 nfs4_lock_owner_t *lop) 15851 { 15852 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15853 mntinfo4_t *mi = VTOMI4(vp); 15854 const int cmd = F_SETLK; 15855 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15856 flock64_t ul_fl; 15857 15858 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15859 "nfs4_reinstitute_local_lock_state")); 15860 15861 /* 15862 * Find active locks for this vp from the local locking code. 15863 * Scan through this list and find out the locks that intersect with 15864 * the lost lock. Once we find the lock that intersects, add the 15865 * intersection area as a new lock to a new list "ri_llp". The lock 15866 * type of the intersection region lock added to ri_llp is the same 15867 * as that found in the active lock list, "list". The intersecting 15868 * region locks are added to ri_llp in increasing l_start order. 15869 */ 15870 ASSERT(nfs_zone() == mi->mi_zone); 15871 15872 locks = flk_active_locks_for_vp(vp); 15873 ri_llp = NULL; 15874 15875 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15876 ASSERT(llp->ll_vp == vp); 15877 /* 15878 * Pick locks that belong to this pid/lockowner 15879 */ 15880 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15881 continue; 15882 15883 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15884 } 15885 15886 /* 15887 * Now we have the list of intersections with the lost lock. These are 15888 * the locks that were/are active before the server replied to the 15889 * last/lost lock. Issue these locks to the server here. Playing these 15890 * locks to the server will re-establish aur current local locking state 15891 * with the v4 server. 15892 * If we get an error, send SIGLOST to the application for that lock. 15893 */ 15894 15895 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15896 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15897 "nfs4_reinstitute_local_lock_state: need to issue " 15898 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15899 llp->ll_flock.l_start, 15900 llp->ll_flock.l_start + llp->ll_flock.l_len, 15901 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15902 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15903 /* 15904 * No need to relock what we already have 15905 */ 15906 if (llp->ll_flock.l_type == lost_flp->l_type) 15907 continue; 15908 15909 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15910 } 15911 15912 /* 15913 * Now keeping the start of the lost lock as our reference parse the 15914 * newly created ri_llp locklist to find the ranges that we have locked 15915 * with the v4 server but not in the current local locking. We need 15916 * to unlock these ranges. 15917 * These ranges can also be reffered to as those ranges, where the lost 15918 * lock does not overlap with the locks in the ri_llp but are locked 15919 * since the server replied to the lost lock. 15920 */ 15921 cur_start = lost_flp->l_start; 15922 lost_flp_end = lock_to_end(lost_flp); 15923 15924 ul_fl.l_type = F_UNLCK; 15925 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15926 ul_fl.l_sysid = lost_flp->l_sysid; 15927 ul_fl.l_pid = lost_flp->l_pid; 15928 15929 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15930 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15931 15932 if (llp->ll_flock.l_start <= cur_start) { 15933 cur_start = start_check(llp_ll_flock_end); 15934 continue; 15935 } 15936 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15937 "nfs4_reinstitute_local_lock_state: " 15938 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15939 cur_start, llp->ll_flock.l_start)); 15940 15941 ul_fl.l_start = cur_start; 15942 ul_fl.l_len = end_to_len(cur_start, 15943 (llp->ll_flock.l_start - 1)); 15944 15945 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15946 cur_start = start_check(llp_ll_flock_end); 15947 } 15948 15949 /* 15950 * In the case where the lost lock ends after all intersecting locks, 15951 * unlock the last part of the lost lock range. 15952 */ 15953 if (cur_start != start_check(lost_flp_end)) { 15954 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15955 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15956 "lost lock region [%"PRIx64" - %"PRIx64"]", 15957 cur_start, lost_flp->l_start + lost_flp->l_len)); 15958 15959 ul_fl.l_start = cur_start; 15960 /* 15961 * Is it an to-EOF lock? if so unlock till the end 15962 */ 15963 if (lost_flp->l_len == 0) 15964 ul_fl.l_len = 0; 15965 else 15966 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15967 15968 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15969 } 15970 15971 if (locks != NULL) 15972 flk_free_locklist(locks); 15973 15974 /* Free up our newly created locklist */ 15975 for (llp = ri_llp; llp != NULL; ) { 15976 tmp_llp = llp->ll_next; 15977 kmem_free(llp, sizeof (locklist_t)); 15978 llp = tmp_llp; 15979 } 15980 15981 /* 15982 * Now return back to the original calling nfs4frlock() 15983 * and let us naturally drop our seqid syncs. 15984 */ 15985 } 15986 15987 /* 15988 * Create a lost state record for the given lock reinstantiation request 15989 * and push it onto the lost state queue. 15990 */ 15991 static void 15992 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15993 nfs4_lock_owner_t *lop) 15994 { 15995 nfs4_lost_rqst_t req; 15996 nfs_lock_type4 locktype; 15997 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15998 15999 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 16000 16001 locktype = flk_to_locktype(cmd, flk->l_type); 16002 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 16003 NULL, NULL, lop, flk, &req, cr, vp); 16004 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 16005 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 16006 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 16007 NULL, NULL, NULL); 16008 } 16009