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 /* 23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 24 */ 25 26 /* 27 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 /* 32 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 33 * All Rights Reserved 34 */ 35 36 /* 37 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 38 */ 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/systm.h> 43 #include <sys/cred.h> 44 #include <sys/time.h> 45 #include <sys/vnode.h> 46 #include <sys/vfs.h> 47 #include <sys/vfs_opreg.h> 48 #include <sys/file.h> 49 #include <sys/filio.h> 50 #include <sys/uio.h> 51 #include <sys/buf.h> 52 #include <sys/mman.h> 53 #include <sys/pathname.h> 54 #include <sys/dirent.h> 55 #include <sys/debug.h> 56 #include <sys/vmsystm.h> 57 #include <sys/fcntl.h> 58 #include <sys/flock.h> 59 #include <sys/swap.h> 60 #include <sys/errno.h> 61 #include <sys/strsubr.h> 62 #include <sys/sysmacros.h> 63 #include <sys/kmem.h> 64 #include <sys/cmn_err.h> 65 #include <sys/pathconf.h> 66 #include <sys/utsname.h> 67 #include <sys/dnlc.h> 68 #include <sys/acl.h> 69 #include <sys/systeminfo.h> 70 #include <sys/policy.h> 71 #include <sys/sdt.h> 72 #include <sys/list.h> 73 #include <sys/stat.h> 74 #include <sys/zone.h> 75 76 #include <rpc/types.h> 77 #include <rpc/auth.h> 78 #include <rpc/clnt.h> 79 80 #include <nfs/nfs.h> 81 #include <nfs/nfs_clnt.h> 82 #include <nfs/nfs_acl.h> 83 #include <nfs/lm.h> 84 #include <nfs/nfs4.h> 85 #include <nfs/nfs4_kprot.h> 86 #include <nfs/rnode4.h> 87 #include <nfs/nfs4_clnt.h> 88 89 #include <vm/hat.h> 90 #include <vm/as.h> 91 #include <vm/page.h> 92 #include <vm/pvn.h> 93 #include <vm/seg.h> 94 #include <vm/seg_map.h> 95 #include <vm/seg_kpm.h> 96 #include <vm/seg_vn.h> 97 98 #include <fs/fs_subr.h> 99 100 #include <sys/ddi.h> 101 #include <sys/int_fmtio.h> 102 #include <sys/fs/autofs.h> 103 104 typedef struct { 105 nfs4_ga_res_t *di_garp; 106 cred_t *di_cred; 107 hrtime_t di_time_call; 108 } dirattr_info_t; 109 110 typedef enum nfs4_acl_op { 111 NFS4_ACL_GET, 112 NFS4_ACL_SET 113 } nfs4_acl_op_t; 114 115 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi); 116 117 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 118 char *, dirattr_info_t *); 119 120 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 121 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 122 nfs4_error_t *, int *); 123 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 124 cred_t *); 125 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 126 stable_how4 *); 127 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 128 cred_t *, bool_t, struct uio *); 129 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 130 vsecattr_t *); 131 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 132 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 133 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 134 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 135 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 136 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 137 int, vnode_t **, cred_t *); 138 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 139 cred_t *, int, int, enum createmode4, int); 140 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 141 caller_context_t *); 142 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 143 vnode_t *, char *, cred_t *, nfsstat4 *); 144 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 145 vnode_t *, char *, cred_t *, nfsstat4 *); 146 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 147 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 148 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 149 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 150 page_t *[], size_t, struct seg *, caddr_t, 151 enum seg_rw, cred_t *); 152 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 153 cred_t *); 154 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 155 int, cred_t *); 156 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 157 int, cred_t *); 158 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 159 static void nfs4_set_mod(vnode_t *); 160 static void nfs4_get_commit(vnode_t *); 161 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 162 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 163 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 164 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 165 cred_t *); 166 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 167 cred_t *); 168 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 169 hrtime_t, vnode_t *, cred_t *); 170 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 171 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 172 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 173 u_offset_t); 174 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 175 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 176 static cred_t *state_to_cred(nfs4_open_stream_t *); 177 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 178 static pid_t lo_to_pid(lock_owner4 *); 179 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 180 cred_t *, nfs4_lock_owner_t *); 181 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 182 nfs4_lock_owner_t *); 183 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **); 184 static void nfs4_delmap_callback(struct as *, void *, uint_t); 185 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 186 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 187 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 188 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 189 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 190 uid_t, gid_t, int); 191 192 /* 193 * Routines that implement the setting of v4 args for the misc. ops 194 */ 195 static void nfs4args_lock_free(nfs_argop4 *); 196 static void nfs4args_lockt_free(nfs_argop4 *); 197 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 198 int, rnode4_t *, cred_t *, bitmap4, int *, 199 nfs4_stateid_types_t *); 200 static void nfs4args_setattr_free(nfs_argop4 *); 201 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 202 bitmap4); 203 static void nfs4args_verify_free(nfs_argop4 *); 204 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 205 WRITE4args **, nfs4_stateid_types_t *); 206 207 /* 208 * These are the vnode ops functions that implement the vnode interface to 209 * the networked file system. See more comments below at nfs4_vnodeops. 210 */ 211 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *); 212 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *, 213 caller_context_t *); 214 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 215 caller_context_t *); 216 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 217 caller_context_t *); 218 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *, 219 caller_context_t *); 220 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 221 caller_context_t *); 222 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *); 223 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *, 224 caller_context_t *); 225 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *); 226 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 227 int, vnode_t **, cred_t *, int, caller_context_t *, 228 vsecattr_t *); 229 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *, 230 int); 231 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *, 232 caller_context_t *, int); 233 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 234 caller_context_t *, int); 235 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **, 236 cred_t *, caller_context_t *, int, vsecattr_t *); 237 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *, 238 caller_context_t *, int); 239 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 240 cred_t *, caller_context_t *, int); 241 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *, 242 caller_context_t *, int); 243 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *); 244 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 245 page_t *[], size_t, struct seg *, caddr_t, 246 enum seg_rw, cred_t *, caller_context_t *); 247 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *, 248 caller_context_t *); 249 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t, 250 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 251 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 252 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 253 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *); 254 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 255 struct flk_callback *, cred_t *, caller_context_t *); 256 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 257 cred_t *, caller_context_t *); 258 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 259 uint_t, uint_t, uint_t, cred_t *, caller_context_t *); 260 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 261 cred_t *, caller_context_t *); 262 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *, 263 caller_context_t *); 264 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 265 caller_context_t *); 266 /* 267 * These vnode ops are required to be called from outside this source file, 268 * e.g. by ephemeral mount stub vnode ops, and so may not be declared 269 * as static. 270 */ 271 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *, 272 caller_context_t *); 273 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *); 274 int nfs4_lookup(vnode_t *, char *, vnode_t **, 275 struct pathname *, int, vnode_t *, cred_t *, 276 caller_context_t *, int *, pathname_t *); 277 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *); 278 int nfs4_rwlock(vnode_t *, int, caller_context_t *); 279 void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 280 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *); 281 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *, 282 caller_context_t *); 283 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 284 caller_context_t *); 285 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *, 286 caller_context_t *); 287 288 /* 289 * Used for nfs4_commit_vp() to indicate if we should 290 * wait on pending writes. 291 */ 292 #define NFS4_WRITE_NOWAIT 0 293 #define NFS4_WRITE_WAIT 1 294 295 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */ 296 297 /* 298 * Error flags used to pass information about certain special errors 299 * which need to be handled specially. 300 */ 301 #define NFS_EOF -98 302 #define NFS_VERF_MISMATCH -97 303 304 /* 305 * Flags used to differentiate between which operation drove the 306 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 307 */ 308 #define NFS4_CLOSE_OP 0x1 309 #define NFS4_DELMAP_OP 0x2 310 #define NFS4_INACTIVE_OP 0x3 311 312 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 313 314 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 315 #define ALIGN64(x, ptr, sz) \ 316 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 317 if (x) { \ 318 x = sizeof (uint64_t) - (x); \ 319 sz -= (x); \ 320 ptr += (x); \ 321 } 322 323 #ifdef DEBUG 324 int nfs4_client_attr_debug = 0; 325 int nfs4_client_state_debug = 0; 326 int nfs4_client_shadow_debug = 0; 327 int nfs4_client_lock_debug = 0; 328 int nfs4_seqid_sync = 0; 329 int nfs4_client_map_debug = 0; 330 static int nfs4_pageio_debug = 0; 331 int nfs4_client_inactive_debug = 0; 332 int nfs4_client_recov_debug = 0; 333 int nfs4_client_failover_debug = 0; 334 int nfs4_client_call_debug = 0; 335 int nfs4_client_lookup_debug = 0; 336 int nfs4_client_zone_debug = 0; 337 int nfs4_lost_rqst_debug = 0; 338 int nfs4_rdattrerr_debug = 0; 339 int nfs4_open_stream_debug = 0; 340 341 int nfs4read_error_inject; 342 343 static int nfs4_create_misses = 0; 344 345 static int nfs4_readdir_cache_shorts = 0; 346 static int nfs4_readdir_readahead = 0; 347 348 static int nfs4_bio_do_stop = 0; 349 350 static int nfs4_lostpage = 0; /* number of times we lost original page */ 351 352 int nfs4_mmap_debug = 0; 353 354 static int nfs4_pathconf_cache_hits = 0; 355 static int nfs4_pathconf_cache_misses = 0; 356 357 int nfs4close_all_cnt; 358 int nfs4close_one_debug = 0; 359 int nfs4close_notw_debug = 0; 360 361 int denied_to_flk_debug = 0; 362 void *lockt_denied_debug; 363 364 #endif 365 366 /* 367 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 368 * or NFS4ERR_RESOURCE. 369 */ 370 static int confirm_retry_sec = 30; 371 372 static int nfs4_lookup_neg_cache = 1; 373 374 /* 375 * number of pages to read ahead 376 * optimized for 100 base-T. 377 */ 378 static int nfs4_nra = 4; 379 380 static int nfs4_do_symlink_cache = 1; 381 382 static int nfs4_pathconf_disable_cache = 0; 383 384 /* 385 * These are the vnode ops routines which implement the vnode interface to 386 * the networked file system. These routines just take their parameters, 387 * make them look networkish by putting the right info into interface structs, 388 * and then calling the appropriate remote routine(s) to do the work. 389 * 390 * Note on directory name lookup cacheing: If we detect a stale fhandle, 391 * we purge the directory cache relative to that vnode. This way, the 392 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 393 * more details on rnode locking. 394 */ 395 396 struct vnodeops *nfs4_vnodeops; 397 398 const fs_operation_def_t nfs4_vnodeops_template[] = { 399 VOPNAME_OPEN, { .vop_open = nfs4_open }, 400 VOPNAME_CLOSE, { .vop_close = nfs4_close }, 401 VOPNAME_READ, { .vop_read = nfs4_read }, 402 VOPNAME_WRITE, { .vop_write = nfs4_write }, 403 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl }, 404 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr }, 405 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr }, 406 VOPNAME_ACCESS, { .vop_access = nfs4_access }, 407 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup }, 408 VOPNAME_CREATE, { .vop_create = nfs4_create }, 409 VOPNAME_REMOVE, { .vop_remove = nfs4_remove }, 410 VOPNAME_LINK, { .vop_link = nfs4_link }, 411 VOPNAME_RENAME, { .vop_rename = nfs4_rename }, 412 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir }, 413 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir }, 414 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir }, 415 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink }, 416 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink }, 417 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync }, 418 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive }, 419 VOPNAME_FID, { .vop_fid = nfs4_fid }, 420 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock }, 421 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock }, 422 VOPNAME_SEEK, { .vop_seek = nfs4_seek }, 423 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock }, 424 VOPNAME_SPACE, { .vop_space = nfs4_space }, 425 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp }, 426 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage }, 427 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage }, 428 VOPNAME_MAP, { .vop_map = nfs4_map }, 429 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap }, 430 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap }, 431 /* no separate nfs4_dump */ 432 VOPNAME_DUMP, { .vop_dump = nfs_dump }, 433 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf }, 434 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio }, 435 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose }, 436 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr }, 437 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr }, 438 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock }, 439 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 440 NULL, NULL 441 }; 442 443 /* 444 * The following are subroutines and definitions to set args or get res 445 * for the different nfsv4 ops 446 */ 447 448 void 449 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 450 { 451 int i; 452 453 for (i = 0; i < arglen; i++) { 454 if (argop[i].argop == OP_LOOKUP) { 455 kmem_free( 456 argop[i].nfs_argop4_u.oplookup. 457 objname.utf8string_val, 458 argop[i].nfs_argop4_u.oplookup. 459 objname.utf8string_len); 460 } 461 } 462 } 463 464 static void 465 nfs4args_lock_free(nfs_argop4 *argop) 466 { 467 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 468 469 if (locker->new_lock_owner == TRUE) { 470 open_to_lock_owner4 *open_owner; 471 472 open_owner = &locker->locker4_u.open_owner; 473 if (open_owner->lock_owner.owner_val != NULL) { 474 kmem_free(open_owner->lock_owner.owner_val, 475 open_owner->lock_owner.owner_len); 476 } 477 } 478 } 479 480 static void 481 nfs4args_lockt_free(nfs_argop4 *argop) 482 { 483 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 484 485 if (lowner->owner_val != NULL) { 486 kmem_free(lowner->owner_val, lowner->owner_len); 487 } 488 } 489 490 static void 491 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 492 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 493 nfs4_stateid_types_t *sid_types) 494 { 495 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 496 mntinfo4_t *mi; 497 498 argop->argop = OP_SETATTR; 499 /* 500 * The stateid is set to 0 if client is not modifying the size 501 * and otherwise to whatever nfs4_get_stateid() returns. 502 * 503 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 504 * state struct could be found for the process/file pair. We may 505 * want to change this in the future (by OPENing the file). See 506 * bug # 4474852. 507 */ 508 if (vap->va_mask & AT_SIZE) { 509 510 ASSERT(rp != NULL); 511 mi = VTOMI4(RTOV4(rp)); 512 513 argop->nfs_argop4_u.opsetattr.stateid = 514 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 515 OP_SETATTR, sid_types, FALSE); 516 } else { 517 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 518 sizeof (stateid4)); 519 } 520 521 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 522 if (*error) 523 bzero(attr, sizeof (*attr)); 524 } 525 526 static void 527 nfs4args_setattr_free(nfs_argop4 *argop) 528 { 529 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 530 } 531 532 static int 533 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 534 bitmap4 supp) 535 { 536 fattr4 *attr; 537 int error = 0; 538 539 argop->argop = op; 540 switch (op) { 541 case OP_VERIFY: 542 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 543 break; 544 case OP_NVERIFY: 545 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 546 break; 547 default: 548 return (EINVAL); 549 } 550 if (!error) 551 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 552 if (error) 553 bzero(attr, sizeof (*attr)); 554 return (error); 555 } 556 557 static void 558 nfs4args_verify_free(nfs_argop4 *argop) 559 { 560 switch (argop->argop) { 561 case OP_VERIFY: 562 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 563 break; 564 case OP_NVERIFY: 565 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 566 break; 567 default: 568 break; 569 } 570 } 571 572 static void 573 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 574 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 575 { 576 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 577 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 578 579 argop->argop = OP_WRITE; 580 wargs->stable = stable; 581 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 582 mi, OP_WRITE, sid_tp); 583 wargs->mblk = NULL; 584 *wargs_pp = wargs; 585 } 586 587 void 588 nfs4args_copen_free(OPEN4cargs *open_args) 589 { 590 if (open_args->owner.owner_val) { 591 kmem_free(open_args->owner.owner_val, 592 open_args->owner.owner_len); 593 } 594 if ((open_args->opentype == OPEN4_CREATE) && 595 (open_args->mode != EXCLUSIVE4)) { 596 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 597 } 598 } 599 600 /* 601 * XXX: This is referenced in modstubs.s 602 */ 603 struct vnodeops * 604 nfs4_getvnodeops(void) 605 { 606 return (nfs4_vnodeops); 607 } 608 609 /* 610 * The OPEN operation opens a regular file. 611 */ 612 /*ARGSUSED3*/ 613 static int 614 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 615 { 616 vnode_t *dvp = NULL; 617 rnode4_t *rp, *drp; 618 int error; 619 int just_been_created; 620 char fn[MAXNAMELEN]; 621 622 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 623 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 624 return (EIO); 625 rp = VTOR4(*vpp); 626 627 /* 628 * Check to see if opening something besides a regular file; 629 * if so skip the OTW call 630 */ 631 if ((*vpp)->v_type != VREG) { 632 error = nfs4_open_non_reg_file(vpp, flag, cr); 633 return (error); 634 } 635 636 /* 637 * XXX - would like a check right here to know if the file is 638 * executable or not, so as to skip OTW 639 */ 640 641 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 642 return (error); 643 644 drp = VTOR4(dvp); 645 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 646 return (EINTR); 647 648 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) { 649 nfs_rw_exit(&drp->r_rwlock); 650 return (error); 651 } 652 653 /* 654 * See if this file has just been CREATEd. 655 * If so, clear the flag and update the dnlc, which was previously 656 * skipped in nfs4_create. 657 * XXX need better serilization on this. 658 * XXX move this into the nf4open_otw call, after we have 659 * XXX acquired the open owner seqid sync. 660 */ 661 mutex_enter(&rp->r_statev4_lock); 662 if (rp->created_v4) { 663 rp->created_v4 = 0; 664 mutex_exit(&rp->r_statev4_lock); 665 666 dnlc_update(dvp, fn, *vpp); 667 /* This is needed so we don't bump the open ref count */ 668 just_been_created = 1; 669 } else { 670 mutex_exit(&rp->r_statev4_lock); 671 just_been_created = 0; 672 } 673 674 /* 675 * If caller specified O_TRUNC/FTRUNC, then be sure to set 676 * FWRITE (to drive successful setattr(size=0) after open) 677 */ 678 if (flag & FTRUNC) 679 flag |= FWRITE; 680 681 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 682 just_been_created); 683 684 if (!error && !((*vpp)->v_flag & VROOT)) 685 dnlc_update(dvp, fn, *vpp); 686 687 nfs_rw_exit(&drp->r_rwlock); 688 689 /* release the hold from vtodv */ 690 VN_RELE(dvp); 691 692 /* exchange the shadow for the master vnode, if needed */ 693 694 if (error == 0 && IS_SHADOW(*vpp, rp)) 695 sv_exchange(vpp); 696 697 return (error); 698 } 699 700 /* 701 * See if there's a "lost open" request to be saved and recovered. 702 */ 703 static void 704 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 705 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 706 vnode_t *dvp, OPEN4cargs *open_args) 707 { 708 vfs_t *vfsp; 709 char *srccfp; 710 711 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 712 713 if (error != ETIMEDOUT && error != EINTR && 714 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 715 lost_rqstp->lr_op = 0; 716 return; 717 } 718 719 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 720 "nfs4open_save_lost_rqst: error %d", error)); 721 722 lost_rqstp->lr_op = OP_OPEN; 723 724 /* 725 * The vp (if it is not NULL) and dvp are held and rele'd via 726 * the recovery code. See nfs4_save_lost_rqst. 727 */ 728 lost_rqstp->lr_vp = vp; 729 lost_rqstp->lr_dvp = dvp; 730 lost_rqstp->lr_oop = oop; 731 lost_rqstp->lr_osp = NULL; 732 lost_rqstp->lr_lop = NULL; 733 lost_rqstp->lr_cr = cr; 734 lost_rqstp->lr_flk = NULL; 735 lost_rqstp->lr_oacc = open_args->share_access; 736 lost_rqstp->lr_odeny = open_args->share_deny; 737 lost_rqstp->lr_oclaim = open_args->claim; 738 if (open_args->claim == CLAIM_DELEGATE_CUR) { 739 lost_rqstp->lr_ostateid = 740 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 741 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 742 } else { 743 srccfp = open_args->open_claim4_u.cfile; 744 } 745 lost_rqstp->lr_ofile.utf8string_len = 0; 746 lost_rqstp->lr_ofile.utf8string_val = NULL; 747 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 748 lost_rqstp->lr_putfirst = FALSE; 749 } 750 751 struct nfs4_excl_time { 752 uint32 seconds; 753 uint32 nseconds; 754 }; 755 756 /* 757 * The OPEN operation creates and/or opens a regular file 758 * 759 * ARGSUSED 760 */ 761 static int 762 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 763 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 764 enum createmode4 createmode, int file_just_been_created) 765 { 766 rnode4_t *rp; 767 rnode4_t *drp = VTOR4(dvp); 768 vnode_t *vp = NULL; 769 vnode_t *vpi = *vpp; 770 bool_t needrecov = FALSE; 771 772 int doqueue = 1; 773 774 COMPOUND4args_clnt args; 775 COMPOUND4res_clnt res; 776 nfs_argop4 *argop; 777 nfs_resop4 *resop; 778 int argoplist_size; 779 int idx_open, idx_fattr; 780 781 GETFH4res *gf_res = NULL; 782 OPEN4res *op_res = NULL; 783 nfs4_ga_res_t *garp; 784 fattr4 *attr = NULL; 785 struct nfs4_excl_time verf; 786 bool_t did_excl_setup = FALSE; 787 int created_osp; 788 789 OPEN4cargs *open_args; 790 nfs4_open_owner_t *oop = NULL; 791 nfs4_open_stream_t *osp = NULL; 792 seqid4 seqid = 0; 793 bool_t retry_open = FALSE; 794 nfs4_recov_state_t recov_state; 795 nfs4_lost_rqst_t lost_rqst; 796 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 797 hrtime_t t; 798 int acc = 0; 799 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 800 cred_t *ncr = NULL; 801 802 nfs4_sharedfh_t *otw_sfh; 803 nfs4_sharedfh_t *orig_sfh; 804 int fh_differs = 0; 805 int numops, setgid_flag; 806 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 807 808 /* 809 * Make sure we properly deal with setting the right gid on 810 * a newly created file to reflect the parent's setgid bit 811 */ 812 setgid_flag = 0; 813 if (create_flag && in_va) { 814 815 /* 816 * If there is grpid mount flag used or 817 * the parent's directory has the setgid bit set 818 * _and_ the client was able to get a valid mapping 819 * for the parent dir's owner_group, we want to 820 * append NVERIFY(owner_group == dva.va_gid) and 821 * SETATTR to the CREATE compound. 822 */ 823 mutex_enter(&drp->r_statelock); 824 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID || 825 drp->r_attr.va_mode & VSGID) && 826 drp->r_attr.va_gid != GID_NOBODY) { 827 in_va->va_mask |= AT_GID; 828 in_va->va_gid = drp->r_attr.va_gid; 829 setgid_flag = 1; 830 } 831 mutex_exit(&drp->r_statelock); 832 } 833 834 /* 835 * Normal/non-create compound: 836 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 837 * 838 * Open(create) compound no setgid: 839 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 840 * RESTOREFH + GETATTR 841 * 842 * Open(create) setgid: 843 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 844 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 845 * NVERIFY(grp) + SETATTR 846 */ 847 if (setgid_flag) { 848 numops = 10; 849 idx_open = 1; 850 idx_fattr = 3; 851 } else if (create_flag) { 852 numops = 7; 853 idx_open = 2; 854 idx_fattr = 4; 855 } else { 856 numops = 4; 857 idx_open = 1; 858 idx_fattr = 3; 859 } 860 861 args.array_len = numops; 862 argoplist_size = numops * sizeof (nfs_argop4); 863 argop = kmem_alloc(argoplist_size, KM_SLEEP); 864 865 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 866 "open %s open flag 0x%x cred %p", file_name, open_flag, 867 (void *)cr)); 868 869 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 870 if (create_flag) { 871 /* 872 * We are to create a file. Initialize the passed in vnode 873 * pointer. 874 */ 875 vpi = NULL; 876 } else { 877 /* 878 * Check to see if the client owns a read delegation and is 879 * trying to open for write. If so, then return the delegation 880 * to avoid the server doing a cb_recall and returning DELAY. 881 * NB - we don't use the statev4_lock here because we'd have 882 * to drop the lock anyway and the result would be stale. 883 */ 884 if ((open_flag & FWRITE) && 885 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 886 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 887 888 /* 889 * If the file has a delegation, then do an access check up 890 * front. This avoids having to an access check later after 891 * we've already done start_op, which could deadlock. 892 */ 893 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 894 if (open_flag & FREAD && 895 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0) 896 acc |= VREAD; 897 if (open_flag & FWRITE && 898 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0) 899 acc |= VWRITE; 900 } 901 } 902 903 drp = VTOR4(dvp); 904 905 recov_state.rs_flags = 0; 906 recov_state.rs_num_retry_despite_err = 0; 907 cred_otw = cr; 908 909 recov_retry: 910 fh_differs = 0; 911 nfs4_error_zinit(&e); 912 913 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 914 if (e.error) { 915 if (ncr != NULL) 916 crfree(ncr); 917 kmem_free(argop, argoplist_size); 918 return (e.error); 919 } 920 921 args.ctag = TAG_OPEN; 922 args.array_len = numops; 923 args.array = argop; 924 925 /* putfh directory fh */ 926 argop[0].argop = OP_CPUTFH; 927 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 928 929 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 930 argop[idx_open].argop = OP_COPEN; 931 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 932 open_args->claim = CLAIM_NULL; 933 934 /* name of file */ 935 open_args->open_claim4_u.cfile = file_name; 936 open_args->owner.owner_len = 0; 937 open_args->owner.owner_val = NULL; 938 939 if (create_flag) { 940 /* CREATE a file */ 941 open_args->opentype = OPEN4_CREATE; 942 open_args->mode = createmode; 943 if (createmode == EXCLUSIVE4) { 944 if (did_excl_setup == FALSE) { 945 verf.seconds = zone_get_hostid(NULL); 946 if (verf.seconds != 0) 947 verf.nseconds = newnum(); 948 else { 949 timestruc_t now; 950 951 gethrestime(&now); 952 verf.seconds = now.tv_sec; 953 verf.nseconds = now.tv_nsec; 954 } 955 /* 956 * Since the server will use this value for the 957 * mtime, make sure that it can't overflow. Zero 958 * out the MSB. The actual value does not matter 959 * here, only its uniqeness. 960 */ 961 verf.seconds &= INT32_MAX; 962 did_excl_setup = TRUE; 963 } 964 965 /* Now copy over verifier to OPEN4args. */ 966 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 967 } else { 968 int v_error; 969 bitmap4 supp_attrs; 970 servinfo4_t *svp; 971 972 attr = &open_args->createhow4_u.createattrs; 973 974 svp = drp->r_server; 975 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 976 supp_attrs = svp->sv_supp_attrs; 977 nfs_rw_exit(&svp->sv_lock); 978 979 /* GUARDED4 or UNCHECKED4 */ 980 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 981 supp_attrs); 982 if (v_error) { 983 bzero(attr, sizeof (*attr)); 984 nfs4args_copen_free(open_args); 985 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 986 &recov_state, FALSE); 987 if (ncr != NULL) 988 crfree(ncr); 989 kmem_free(argop, argoplist_size); 990 return (v_error); 991 } 992 } 993 } else { 994 /* NO CREATE */ 995 open_args->opentype = OPEN4_NOCREATE; 996 } 997 998 if (recov_state.rs_sp != NULL) { 999 mutex_enter(&recov_state.rs_sp->s_lock); 1000 open_args->owner.clientid = recov_state.rs_sp->clientid; 1001 mutex_exit(&recov_state.rs_sp->s_lock); 1002 } else { 1003 /* XXX should we just fail here? */ 1004 open_args->owner.clientid = 0; 1005 } 1006 1007 /* 1008 * This increments oop's ref count or creates a temporary 'just_created' 1009 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 1010 * completes. 1011 */ 1012 mutex_enter(&VTOMI4(dvp)->mi_lock); 1013 1014 /* See if a permanent or just created open owner exists */ 1015 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 1016 if (!oop) { 1017 /* 1018 * This open owner does not exist so create a temporary 1019 * just created one. 1020 */ 1021 oop = create_open_owner(cr, VTOMI4(dvp)); 1022 ASSERT(oop != NULL); 1023 } 1024 mutex_exit(&VTOMI4(dvp)->mi_lock); 1025 1026 /* this length never changes, do alloc before seqid sync */ 1027 open_args->owner.owner_len = sizeof (oop->oo_name); 1028 open_args->owner.owner_val = 1029 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1030 1031 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 1032 if (e.error == EAGAIN) { 1033 open_owner_rele(oop); 1034 nfs4args_copen_free(open_args); 1035 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1036 if (ncr != NULL) { 1037 crfree(ncr); 1038 ncr = NULL; 1039 } 1040 goto recov_retry; 1041 } 1042 1043 /* Check to see if we need to do the OTW call */ 1044 if (!create_flag) { 1045 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1046 file_just_been_created, &e.error, acc, &recov_state)) { 1047 1048 /* 1049 * The OTW open is not necessary. Either 1050 * the open can succeed without it (eg. 1051 * delegation, error == 0) or the open 1052 * must fail due to an access failure 1053 * (error != 0). In either case, tidy 1054 * up and return. 1055 */ 1056 1057 nfs4_end_open_seqid_sync(oop); 1058 open_owner_rele(oop); 1059 nfs4args_copen_free(open_args); 1060 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1061 if (ncr != NULL) 1062 crfree(ncr); 1063 kmem_free(argop, argoplist_size); 1064 return (e.error); 1065 } 1066 } 1067 1068 bcopy(&oop->oo_name, open_args->owner.owner_val, 1069 open_args->owner.owner_len); 1070 1071 seqid = nfs4_get_open_seqid(oop) + 1; 1072 open_args->seqid = seqid; 1073 open_args->share_access = 0; 1074 if (open_flag & FREAD) 1075 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1076 if (open_flag & FWRITE) 1077 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1078 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1079 1080 1081 1082 /* 1083 * getfh w/sanity check for idx_open/idx_fattr 1084 */ 1085 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1086 argop[idx_open + 1].argop = OP_GETFH; 1087 1088 /* getattr */ 1089 argop[idx_fattr].argop = OP_GETATTR; 1090 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1091 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1092 1093 if (setgid_flag) { 1094 vattr_t _v; 1095 servinfo4_t *svp; 1096 bitmap4 supp_attrs; 1097 1098 svp = drp->r_server; 1099 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1100 supp_attrs = svp->sv_supp_attrs; 1101 nfs_rw_exit(&svp->sv_lock); 1102 1103 /* 1104 * For setgid case, we need to: 1105 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1106 */ 1107 argop[4].argop = OP_SAVEFH; 1108 1109 argop[5].argop = OP_CPUTFH; 1110 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1111 1112 argop[6].argop = OP_GETATTR; 1113 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1114 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1115 1116 argop[7].argop = OP_RESTOREFH; 1117 1118 /* 1119 * nverify 1120 */ 1121 _v.va_mask = AT_GID; 1122 _v.va_gid = in_va->va_gid; 1123 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1124 supp_attrs))) { 1125 1126 /* 1127 * setattr 1128 * 1129 * We _know_ we're not messing with AT_SIZE or 1130 * AT_XTIME, so no need for stateid or flags. 1131 * Also we specify NULL rp since we're only 1132 * interested in setting owner_group attributes. 1133 */ 1134 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1135 supp_attrs, &e.error, 0); 1136 if (e.error) 1137 nfs4args_verify_free(&argop[8]); 1138 } 1139 1140 if (e.error) { 1141 /* 1142 * XXX - Revisit the last argument to nfs4_end_op() 1143 * once 5020486 is fixed. 1144 */ 1145 nfs4_end_open_seqid_sync(oop); 1146 open_owner_rele(oop); 1147 nfs4args_copen_free(open_args); 1148 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1149 if (ncr != NULL) 1150 crfree(ncr); 1151 kmem_free(argop, argoplist_size); 1152 return (e.error); 1153 } 1154 } else if (create_flag) { 1155 argop[1].argop = OP_SAVEFH; 1156 1157 argop[5].argop = OP_RESTOREFH; 1158 1159 argop[6].argop = OP_GETATTR; 1160 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1161 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1162 } 1163 1164 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1165 "nfs4open_otw: %s call, nm %s, rp %s", 1166 needrecov ? "recov" : "first", file_name, 1167 rnode4info(VTOR4(dvp)))); 1168 1169 t = gethrtime(); 1170 1171 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1172 1173 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1174 nfs4_set_open_seqid(seqid, oop, args.ctag); 1175 1176 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1177 1178 if (e.error || needrecov) { 1179 bool_t abort = FALSE; 1180 1181 if (needrecov) { 1182 nfs4_bseqid_entry_t *bsep = NULL; 1183 1184 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1185 cred_otw, vpi, dvp, open_args); 1186 1187 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1188 bsep = nfs4_create_bseqid_entry(oop, NULL, 1189 vpi, 0, args.ctag, open_args->seqid); 1190 num_bseqid_retry--; 1191 } 1192 1193 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1194 NULL, lost_rqst.lr_op == OP_OPEN ? 1195 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL); 1196 1197 if (bsep) 1198 kmem_free(bsep, sizeof (*bsep)); 1199 /* give up if we keep getting BAD_SEQID */ 1200 if (num_bseqid_retry == 0) 1201 abort = TRUE; 1202 if (abort == TRUE && e.error == 0) 1203 e.error = geterrno4(res.status); 1204 } 1205 nfs4_end_open_seqid_sync(oop); 1206 open_owner_rele(oop); 1207 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1208 nfs4args_copen_free(open_args); 1209 if (setgid_flag) { 1210 nfs4args_verify_free(&argop[8]); 1211 nfs4args_setattr_free(&argop[9]); 1212 } 1213 if (!e.error) 1214 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1215 if (ncr != NULL) { 1216 crfree(ncr); 1217 ncr = NULL; 1218 } 1219 if (!needrecov || abort == TRUE || e.error == EINTR || 1220 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1221 kmem_free(argop, argoplist_size); 1222 return (e.error); 1223 } 1224 goto recov_retry; 1225 } 1226 1227 /* 1228 * Will check and update lease after checking the rflag for 1229 * OPEN_CONFIRM in the successful OPEN call. 1230 */ 1231 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1232 1233 /* 1234 * XXX what if we're crossing mount points from server1:/drp 1235 * to server2:/drp/rp. 1236 */ 1237 1238 /* Signal our end of use of the open seqid */ 1239 nfs4_end_open_seqid_sync(oop); 1240 1241 /* 1242 * This will destroy the open owner if it was just created, 1243 * and no one else has put a reference on it. 1244 */ 1245 open_owner_rele(oop); 1246 if (create_flag && (createmode != EXCLUSIVE4) && 1247 res.status == NFS4ERR_BADOWNER) 1248 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1249 1250 e.error = geterrno4(res.status); 1251 nfs4args_copen_free(open_args); 1252 if (setgid_flag) { 1253 nfs4args_verify_free(&argop[8]); 1254 nfs4args_setattr_free(&argop[9]); 1255 } 1256 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1257 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1258 /* 1259 * If the reply is NFS4ERR_ACCESS, it may be because 1260 * we are root (no root net access). If the real uid 1261 * is not root, then retry with the real uid instead. 1262 */ 1263 if (ncr != NULL) { 1264 crfree(ncr); 1265 ncr = NULL; 1266 } 1267 if (res.status == NFS4ERR_ACCESS && 1268 (ncr = crnetadjust(cred_otw)) != NULL) { 1269 cred_otw = ncr; 1270 goto recov_retry; 1271 } 1272 kmem_free(argop, argoplist_size); 1273 return (e.error); 1274 } 1275 1276 resop = &res.array[idx_open]; /* open res */ 1277 op_res = &resop->nfs_resop4_u.opopen; 1278 1279 #ifdef DEBUG 1280 /* 1281 * verify attrset bitmap 1282 */ 1283 if (create_flag && 1284 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1285 /* make sure attrset returned is what we asked for */ 1286 /* XXX Ignore this 'error' for now */ 1287 if (attr->attrmask != op_res->attrset) 1288 /* EMPTY */; 1289 } 1290 #endif 1291 1292 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1293 mutex_enter(&VTOMI4(dvp)->mi_lock); 1294 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1295 mutex_exit(&VTOMI4(dvp)->mi_lock); 1296 } 1297 1298 resop = &res.array[idx_open + 1]; /* getfh res */ 1299 gf_res = &resop->nfs_resop4_u.opgetfh; 1300 1301 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1302 1303 /* 1304 * The open stateid has been updated on the server but not 1305 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1306 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1307 * WRITE call. That, however, will use the old stateid, so go ahead 1308 * and upate the open stateid now, before any call to makenfs4node. 1309 */ 1310 if (vpi) { 1311 nfs4_open_stream_t *tmp_osp; 1312 rnode4_t *tmp_rp = VTOR4(vpi); 1313 1314 tmp_osp = find_open_stream(oop, tmp_rp); 1315 if (tmp_osp) { 1316 tmp_osp->open_stateid = op_res->stateid; 1317 mutex_exit(&tmp_osp->os_sync_lock); 1318 open_stream_rele(tmp_osp, tmp_rp); 1319 } 1320 1321 /* 1322 * We must determine if the file handle given by the otw open 1323 * is the same as the file handle which was passed in with 1324 * *vpp. This case can be reached if the file we are trying 1325 * to open has been removed and another file has been created 1326 * having the same file name. The passed in vnode is released 1327 * later. 1328 */ 1329 orig_sfh = VTOR4(vpi)->r_fh; 1330 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1331 } 1332 1333 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1334 1335 if (create_flag || fh_differs) { 1336 int rnode_err = 0; 1337 1338 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1339 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh)); 1340 1341 if (e.error) 1342 PURGE_ATTRCACHE4(vp); 1343 /* 1344 * For the newly created vp case, make sure the rnode 1345 * isn't bad before using it. 1346 */ 1347 mutex_enter(&(VTOR4(vp))->r_statelock); 1348 if (VTOR4(vp)->r_flags & R4RECOVERR) 1349 rnode_err = EIO; 1350 mutex_exit(&(VTOR4(vp))->r_statelock); 1351 1352 if (rnode_err) { 1353 nfs4_end_open_seqid_sync(oop); 1354 nfs4args_copen_free(open_args); 1355 if (setgid_flag) { 1356 nfs4args_verify_free(&argop[8]); 1357 nfs4args_setattr_free(&argop[9]); 1358 } 1359 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1360 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1361 needrecov); 1362 open_owner_rele(oop); 1363 VN_RELE(vp); 1364 if (ncr != NULL) 1365 crfree(ncr); 1366 sfh4_rele(&otw_sfh); 1367 kmem_free(argop, argoplist_size); 1368 return (EIO); 1369 } 1370 } else { 1371 vp = vpi; 1372 } 1373 sfh4_rele(&otw_sfh); 1374 1375 /* 1376 * It seems odd to get a full set of attrs and then not update 1377 * the object's attrcache in the non-create case. Create case uses 1378 * the attrs since makenfs4node checks to see if the attrs need to 1379 * be updated (and then updates them). The non-create case should 1380 * update attrs also. 1381 */ 1382 if (! create_flag && ! fh_differs && !e.error) { 1383 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1384 } 1385 1386 nfs4_error_zinit(&e); 1387 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1388 /* This does not do recovery for vp explicitly. */ 1389 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1390 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1391 1392 if (e.error || e.stat) { 1393 nfs4_end_open_seqid_sync(oop); 1394 nfs4args_copen_free(open_args); 1395 if (setgid_flag) { 1396 nfs4args_verify_free(&argop[8]); 1397 nfs4args_setattr_free(&argop[9]); 1398 } 1399 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1400 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1401 needrecov); 1402 open_owner_rele(oop); 1403 if (create_flag || fh_differs) { 1404 /* rele the makenfs4node */ 1405 VN_RELE(vp); 1406 } 1407 if (ncr != NULL) { 1408 crfree(ncr); 1409 ncr = NULL; 1410 } 1411 if (retry_open == TRUE) { 1412 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1413 "nfs4open_otw: retry the open since OPEN " 1414 "CONFIRM failed with error %d stat %d", 1415 e.error, e.stat)); 1416 if (create_flag && createmode == GUARDED4) { 1417 NFS4_DEBUG(nfs4_client_recov_debug, 1418 (CE_NOTE, "nfs4open_otw: switch " 1419 "createmode from GUARDED4 to " 1420 "UNCHECKED4")); 1421 createmode = UNCHECKED4; 1422 } 1423 goto recov_retry; 1424 } 1425 if (!e.error) { 1426 if (create_flag && (createmode != EXCLUSIVE4) && 1427 e.stat == NFS4ERR_BADOWNER) 1428 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1429 1430 e.error = geterrno4(e.stat); 1431 } 1432 kmem_free(argop, argoplist_size); 1433 return (e.error); 1434 } 1435 } 1436 1437 rp = VTOR4(vp); 1438 1439 mutex_enter(&rp->r_statev4_lock); 1440 if (create_flag) 1441 rp->created_v4 = 1; 1442 mutex_exit(&rp->r_statev4_lock); 1443 1444 mutex_enter(&oop->oo_lock); 1445 /* Doesn't matter if 'oo_just_created' already was set as this */ 1446 oop->oo_just_created = NFS4_PERM_CREATED; 1447 if (oop->oo_cred_otw) 1448 crfree(oop->oo_cred_otw); 1449 oop->oo_cred_otw = cred_otw; 1450 crhold(oop->oo_cred_otw); 1451 mutex_exit(&oop->oo_lock); 1452 1453 /* returns with 'os_sync_lock' held */ 1454 osp = find_or_create_open_stream(oop, rp, &created_osp); 1455 if (!osp) { 1456 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1457 "nfs4open_otw: failed to create an open stream")); 1458 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1459 "signal our end of use of the open seqid")); 1460 1461 nfs4_end_open_seqid_sync(oop); 1462 open_owner_rele(oop); 1463 nfs4args_copen_free(open_args); 1464 if (setgid_flag) { 1465 nfs4args_verify_free(&argop[8]); 1466 nfs4args_setattr_free(&argop[9]); 1467 } 1468 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1469 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1470 if (create_flag || fh_differs) 1471 VN_RELE(vp); 1472 if (ncr != NULL) 1473 crfree(ncr); 1474 1475 kmem_free(argop, argoplist_size); 1476 return (EINVAL); 1477 1478 } 1479 1480 osp->open_stateid = op_res->stateid; 1481 1482 if (open_flag & FREAD) 1483 osp->os_share_acc_read++; 1484 if (open_flag & FWRITE) 1485 osp->os_share_acc_write++; 1486 osp->os_share_deny_none++; 1487 1488 /* 1489 * Need to reset this bitfield for the possible case where we were 1490 * going to OTW CLOSE the file, got a non-recoverable error, and before 1491 * we could retry the CLOSE, OPENed the file again. 1492 */ 1493 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1494 osp->os_final_close = 0; 1495 osp->os_force_close = 0; 1496 #ifdef DEBUG 1497 if (osp->os_failed_reopen) 1498 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1499 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1500 (void *)osp, (void *)cr, rnode4info(rp))); 1501 #endif 1502 osp->os_failed_reopen = 0; 1503 1504 mutex_exit(&osp->os_sync_lock); 1505 1506 nfs4_end_open_seqid_sync(oop); 1507 1508 if (created_osp && recov_state.rs_sp != NULL) { 1509 mutex_enter(&recov_state.rs_sp->s_lock); 1510 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1511 mutex_exit(&recov_state.rs_sp->s_lock); 1512 } 1513 1514 /* get rid of our reference to find oop */ 1515 open_owner_rele(oop); 1516 1517 open_stream_rele(osp, rp); 1518 1519 /* accept delegation, if any */ 1520 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1521 1522 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1523 1524 if (createmode == EXCLUSIVE4 && 1525 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1526 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1527 " EXCLUSIVE4: sending a SETATTR")); 1528 /* 1529 * If doing an exclusive create, then generate 1530 * a SETATTR to set the initial attributes. 1531 * Try to set the mtime and the atime to the 1532 * server's current time. It is somewhat 1533 * expected that these fields will be used to 1534 * store the exclusive create cookie. If not, 1535 * server implementors will need to know that 1536 * a SETATTR will follow an exclusive create 1537 * and the cookie should be destroyed if 1538 * appropriate. 1539 * 1540 * The AT_GID and AT_SIZE bits are turned off 1541 * so that the SETATTR request will not attempt 1542 * to process these. The gid will be set 1543 * separately if appropriate. The size is turned 1544 * off because it is assumed that a new file will 1545 * be created empty and if the file wasn't empty, 1546 * then the exclusive create will have failed 1547 * because the file must have existed already. 1548 * Therefore, no truncate operation is needed. 1549 */ 1550 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1551 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1552 1553 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1554 if (e.error) { 1555 /* 1556 * Couldn't correct the attributes of 1557 * the newly created file and the 1558 * attributes are wrong. Remove the 1559 * file and return an error to the 1560 * application. 1561 */ 1562 /* XXX will this take care of client state ? */ 1563 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1564 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1565 " remove file", e.error)); 1566 VN_RELE(vp); 1567 (void) nfs4_remove(dvp, file_name, cr, NULL, 0); 1568 /* 1569 * Since we've reled the vnode and removed 1570 * the file we now need to return the error. 1571 * At this point we don't want to update the 1572 * dircaches, call nfs4_waitfor_purge_complete 1573 * or set vpp to vp so we need to skip these 1574 * as well. 1575 */ 1576 goto skip_update_dircaches; 1577 } 1578 } 1579 1580 /* 1581 * If we created or found the correct vnode, due to create_flag or 1582 * fh_differs being set, then update directory cache attribute, readdir 1583 * and dnlc caches. 1584 */ 1585 if (create_flag || fh_differs) { 1586 dirattr_info_t dinfo, *dinfop; 1587 1588 /* 1589 * Make sure getattr succeeded before using results. 1590 * note: op 7 is getattr(dir) for both flavors of 1591 * open(create). 1592 */ 1593 if (create_flag && res.status == NFS4_OK) { 1594 dinfo.di_time_call = t; 1595 dinfo.di_cred = cr; 1596 dinfo.di_garp = 1597 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1598 dinfop = &dinfo; 1599 } else { 1600 dinfop = NULL; 1601 } 1602 1603 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1604 dinfop); 1605 } 1606 1607 /* 1608 * If the page cache for this file was flushed from actions 1609 * above, it was done asynchronously and if that is true, 1610 * there is a need to wait here for it to complete. This must 1611 * be done outside of start_fop/end_fop. 1612 */ 1613 (void) nfs4_waitfor_purge_complete(vp); 1614 1615 /* 1616 * It is implicit that we are in the open case (create_flag == 0) since 1617 * fh_differs can only be set to a non-zero value in the open case. 1618 */ 1619 if (fh_differs != 0 && vpi != NULL) 1620 VN_RELE(vpi); 1621 1622 /* 1623 * Be sure to set *vpp to the correct value before returning. 1624 */ 1625 *vpp = vp; 1626 1627 skip_update_dircaches: 1628 1629 nfs4args_copen_free(open_args); 1630 if (setgid_flag) { 1631 nfs4args_verify_free(&argop[8]); 1632 nfs4args_setattr_free(&argop[9]); 1633 } 1634 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1635 1636 if (ncr) 1637 crfree(ncr); 1638 kmem_free(argop, argoplist_size); 1639 return (e.error); 1640 } 1641 1642 /* 1643 * Reopen an open instance. cf. nfs4open_otw(). 1644 * 1645 * Errors are returned by the nfs4_error_t parameter. 1646 * - ep->error contains an errno value or zero. 1647 * - if it is zero, ep->stat is set to an NFS status code, if any. 1648 * If the file could not be reopened, but the caller should continue, the 1649 * file is marked dead and no error values are returned. If the caller 1650 * should stop recovering open files and start over, either the ep->error 1651 * value or ep->stat will indicate an error (either something that requires 1652 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1653 * filehandles) may be handled silently by this routine. 1654 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1655 * will be started, so the caller should not do it. 1656 * 1657 * Gotos: 1658 * - kill_file : reopen failed in such a fashion to constitute marking the 1659 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1660 * is for cases where recovery is not possible. 1661 * - failed_reopen : same as above, except that the file has already been 1662 * marked dead, so no need to do it again. 1663 * - bailout : reopen failed but we are able to recover and retry the reopen - 1664 * either within this function immediately or via the calling function. 1665 */ 1666 1667 void 1668 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1669 open_claim_type4 claim, bool_t frc_use_claim_previous, 1670 bool_t is_recov) 1671 { 1672 COMPOUND4args_clnt args; 1673 COMPOUND4res_clnt res; 1674 nfs_argop4 argop[4]; 1675 nfs_resop4 *resop; 1676 OPEN4res *op_res = NULL; 1677 OPEN4cargs *open_args; 1678 GETFH4res *gf_res; 1679 rnode4_t *rp = VTOR4(vp); 1680 int doqueue = 1; 1681 cred_t *cr = NULL, *cred_otw = NULL; 1682 nfs4_open_owner_t *oop = NULL; 1683 seqid4 seqid; 1684 nfs4_ga_res_t *garp; 1685 char fn[MAXNAMELEN]; 1686 nfs4_recov_state_t recov = {NULL, 0}; 1687 nfs4_lost_rqst_t lost_rqst; 1688 mntinfo4_t *mi = VTOMI4(vp); 1689 bool_t abort; 1690 char *failed_msg = ""; 1691 int fh_different; 1692 hrtime_t t; 1693 nfs4_bseqid_entry_t *bsep = NULL; 1694 1695 ASSERT(nfs4_consistent_type(vp)); 1696 ASSERT(nfs_zone() == mi->mi_zone); 1697 1698 nfs4_error_zinit(ep); 1699 1700 /* this is the cred used to find the open owner */ 1701 cr = state_to_cred(osp); 1702 if (cr == NULL) { 1703 failed_msg = "Couldn't reopen: no cred"; 1704 goto kill_file; 1705 } 1706 /* use this cred for OTW operations */ 1707 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1708 1709 top: 1710 nfs4_error_zinit(ep); 1711 1712 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1713 /* File system has been unmounted, quit */ 1714 ep->error = EIO; 1715 failed_msg = "Couldn't reopen: file system has been unmounted"; 1716 goto kill_file; 1717 } 1718 1719 oop = osp->os_open_owner; 1720 1721 ASSERT(oop != NULL); 1722 if (oop == NULL) { /* be defensive in non-DEBUG */ 1723 failed_msg = "can't reopen: no open owner"; 1724 goto kill_file; 1725 } 1726 open_owner_hold(oop); 1727 1728 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1729 if (ep->error) { 1730 open_owner_rele(oop); 1731 oop = NULL; 1732 goto bailout; 1733 } 1734 1735 /* 1736 * If the rnode has a delegation and the delegation has been 1737 * recovered and the server didn't request a recall and the caller 1738 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1739 * recovery) and the rnode hasn't been marked dead, then install 1740 * the delegation stateid in the open stream. Otherwise, proceed 1741 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1742 */ 1743 mutex_enter(&rp->r_statev4_lock); 1744 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1745 !rp->r_deleg_return_pending && 1746 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1747 !rp->r_deleg_needs_recall && 1748 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1749 !(rp->r_flags & R4RECOVERR)) { 1750 mutex_enter(&osp->os_sync_lock); 1751 osp->os_delegation = 1; 1752 osp->open_stateid = rp->r_deleg_stateid; 1753 mutex_exit(&osp->os_sync_lock); 1754 mutex_exit(&rp->r_statev4_lock); 1755 goto bailout; 1756 } 1757 mutex_exit(&rp->r_statev4_lock); 1758 1759 /* 1760 * If the file failed recovery, just quit. This failure need not 1761 * affect other reopens, so don't return an error. 1762 */ 1763 mutex_enter(&rp->r_statelock); 1764 if (rp->r_flags & R4RECOVERR) { 1765 mutex_exit(&rp->r_statelock); 1766 ep->error = 0; 1767 goto failed_reopen; 1768 } 1769 mutex_exit(&rp->r_statelock); 1770 1771 /* 1772 * argop is empty here 1773 * 1774 * PUTFH, OPEN, GETATTR 1775 */ 1776 args.ctag = TAG_REOPEN; 1777 args.array_len = 4; 1778 args.array = argop; 1779 1780 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1781 "nfs4_reopen: file is type %d, id %s", 1782 vp->v_type, rnode4info(VTOR4(vp)))); 1783 1784 argop[0].argop = OP_CPUTFH; 1785 1786 if (claim != CLAIM_PREVIOUS) { 1787 /* 1788 * if this is a file mount then 1789 * use the mntinfo parentfh 1790 */ 1791 argop[0].nfs_argop4_u.opcputfh.sfh = 1792 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1793 VTOSV(vp)->sv_dfh; 1794 } else { 1795 /* putfh fh to reopen */ 1796 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1797 } 1798 1799 argop[1].argop = OP_COPEN; 1800 open_args = &argop[1].nfs_argop4_u.opcopen; 1801 open_args->claim = claim; 1802 1803 if (claim == CLAIM_NULL) { 1804 1805 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1806 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1807 "failed for vp 0x%p for CLAIM_NULL with %m", 1808 (void *)vp); 1809 failed_msg = "Couldn't reopen: vtoname failed for " 1810 "CLAIM_NULL"; 1811 /* nothing allocated yet */ 1812 goto kill_file; 1813 } 1814 1815 open_args->open_claim4_u.cfile = fn; 1816 } else if (claim == CLAIM_PREVIOUS) { 1817 1818 /* 1819 * We have two cases to deal with here: 1820 * 1) We're being called to reopen files in order to satisfy 1821 * a lock operation request which requires us to explicitly 1822 * reopen files which were opened under a delegation. If 1823 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1824 * that case, frc_use_claim_previous is TRUE and we must 1825 * use the rnode's current delegation type (r_deleg_type). 1826 * 2) We're reopening files during some form of recovery. 1827 * In this case, frc_use_claim_previous is FALSE and we 1828 * use the delegation type appropriate for recovery 1829 * (r_deleg_needs_recovery). 1830 */ 1831 mutex_enter(&rp->r_statev4_lock); 1832 open_args->open_claim4_u.delegate_type = 1833 frc_use_claim_previous ? 1834 rp->r_deleg_type : 1835 rp->r_deleg_needs_recovery; 1836 mutex_exit(&rp->r_statev4_lock); 1837 1838 } else if (claim == CLAIM_DELEGATE_CUR) { 1839 1840 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1841 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1842 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1843 "with %m", (void *)vp); 1844 failed_msg = "Couldn't reopen: vtoname failed for " 1845 "CLAIM_DELEGATE_CUR"; 1846 /* nothing allocated yet */ 1847 goto kill_file; 1848 } 1849 1850 mutex_enter(&rp->r_statev4_lock); 1851 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1852 rp->r_deleg_stateid; 1853 mutex_exit(&rp->r_statev4_lock); 1854 1855 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1856 } 1857 open_args->opentype = OPEN4_NOCREATE; 1858 open_args->owner.clientid = mi2clientid(mi); 1859 open_args->owner.owner_len = sizeof (oop->oo_name); 1860 open_args->owner.owner_val = 1861 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1862 bcopy(&oop->oo_name, open_args->owner.owner_val, 1863 open_args->owner.owner_len); 1864 open_args->share_access = 0; 1865 open_args->share_deny = 0; 1866 1867 mutex_enter(&osp->os_sync_lock); 1868 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1869 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1870 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1871 (void *)osp, (void *)rp, osp->os_share_acc_read, 1872 osp->os_share_acc_write, osp->os_open_ref_count, 1873 osp->os_mmap_read, osp->os_mmap_write, claim)); 1874 1875 if (osp->os_share_acc_read || osp->os_mmap_read) 1876 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1877 if (osp->os_share_acc_write || osp->os_mmap_write) 1878 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1879 if (osp->os_share_deny_read) 1880 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1881 if (osp->os_share_deny_write) 1882 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1883 mutex_exit(&osp->os_sync_lock); 1884 1885 seqid = nfs4_get_open_seqid(oop) + 1; 1886 open_args->seqid = seqid; 1887 1888 /* Construct the getfh part of the compound */ 1889 argop[2].argop = OP_GETFH; 1890 1891 /* Construct the getattr part of the compound */ 1892 argop[3].argop = OP_GETATTR; 1893 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1894 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1895 1896 t = gethrtime(); 1897 1898 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1899 1900 if (ep->error) { 1901 if (!is_recov && !frc_use_claim_previous && 1902 (ep->error == EINTR || ep->error == ETIMEDOUT || 1903 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1904 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1905 cred_otw, vp, NULL, open_args); 1906 abort = nfs4_start_recovery(ep, 1907 VTOMI4(vp), vp, NULL, NULL, 1908 lost_rqst.lr_op == OP_OPEN ? 1909 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL); 1910 nfs4args_copen_free(open_args); 1911 goto bailout; 1912 } 1913 1914 nfs4args_copen_free(open_args); 1915 1916 if (ep->error == EACCES && cred_otw != cr) { 1917 crfree(cred_otw); 1918 cred_otw = cr; 1919 crhold(cred_otw); 1920 nfs4_end_open_seqid_sync(oop); 1921 open_owner_rele(oop); 1922 oop = NULL; 1923 goto top; 1924 } 1925 if (ep->error == ETIMEDOUT) 1926 goto bailout; 1927 failed_msg = "Couldn't reopen: rpc error"; 1928 goto kill_file; 1929 } 1930 1931 if (nfs4_need_to_bump_seqid(&res)) 1932 nfs4_set_open_seqid(seqid, oop, args.ctag); 1933 1934 switch (res.status) { 1935 case NFS4_OK: 1936 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1937 mutex_enter(&rp->r_statelock); 1938 rp->r_delay_interval = 0; 1939 mutex_exit(&rp->r_statelock); 1940 } 1941 break; 1942 case NFS4ERR_BAD_SEQID: 1943 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1944 args.ctag, open_args->seqid); 1945 1946 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1947 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1948 NULL, OP_OPEN, bsep, NULL, NULL); 1949 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 kmem_free(bsep, sizeof (*bsep)); 1956 1957 goto kill_file; 1958 case NFS4ERR_NO_GRACE: 1959 nfs4args_copen_free(open_args); 1960 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1961 nfs4_end_open_seqid_sync(oop); 1962 open_owner_rele(oop); 1963 oop = NULL; 1964 if (claim == CLAIM_PREVIOUS) { 1965 /* 1966 * Retry as a plain open. We don't need to worry about 1967 * checking the changeinfo: it is acceptable for a 1968 * client to re-open a file and continue processing 1969 * (in the absence of locks). 1970 */ 1971 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1972 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1973 "will retry as CLAIM_NULL")); 1974 claim = CLAIM_NULL; 1975 nfs4_mi_kstat_inc_no_grace(mi); 1976 goto top; 1977 } 1978 failed_msg = 1979 "Couldn't reopen: tried reclaim outside grace period. "; 1980 goto kill_file; 1981 case NFS4ERR_GRACE: 1982 nfs4_set_grace_wait(mi); 1983 nfs4args_copen_free(open_args); 1984 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1985 nfs4_end_open_seqid_sync(oop); 1986 open_owner_rele(oop); 1987 oop = NULL; 1988 ep->error = nfs4_wait_for_grace(mi, &recov); 1989 if (ep->error != 0) 1990 goto bailout; 1991 goto top; 1992 case NFS4ERR_DELAY: 1993 nfs4_set_delay_wait(vp); 1994 nfs4args_copen_free(open_args); 1995 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1996 nfs4_end_open_seqid_sync(oop); 1997 open_owner_rele(oop); 1998 oop = NULL; 1999 ep->error = nfs4_wait_for_delay(vp, &recov); 2000 nfs4_mi_kstat_inc_delay(mi); 2001 if (ep->error != 0) 2002 goto bailout; 2003 goto top; 2004 case NFS4ERR_FHEXPIRED: 2005 /* recover filehandle and retry */ 2006 abort = nfs4_start_recovery(ep, 2007 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL); 2008 nfs4args_copen_free(open_args); 2009 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2010 nfs4_end_open_seqid_sync(oop); 2011 open_owner_rele(oop); 2012 oop = NULL; 2013 if (abort == FALSE) 2014 goto top; 2015 failed_msg = "Couldn't reopen: recovery aborted"; 2016 goto kill_file; 2017 case NFS4ERR_RESOURCE: 2018 case NFS4ERR_STALE_CLIENTID: 2019 case NFS4ERR_WRONGSEC: 2020 case NFS4ERR_EXPIRED: 2021 /* 2022 * Do not mark the file dead and let the calling 2023 * function initiate recovery. 2024 */ 2025 nfs4args_copen_free(open_args); 2026 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2027 nfs4_end_open_seqid_sync(oop); 2028 open_owner_rele(oop); 2029 oop = NULL; 2030 goto bailout; 2031 case NFS4ERR_ACCESS: 2032 if (cred_otw != cr) { 2033 crfree(cred_otw); 2034 cred_otw = cr; 2035 crhold(cred_otw); 2036 nfs4args_copen_free(open_args); 2037 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2038 nfs4_end_open_seqid_sync(oop); 2039 open_owner_rele(oop); 2040 oop = NULL; 2041 goto top; 2042 } 2043 /* fall through */ 2044 default: 2045 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2046 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2047 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2048 rnode4info(VTOR4(vp)))); 2049 failed_msg = "Couldn't reopen: NFSv4 error"; 2050 nfs4args_copen_free(open_args); 2051 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2052 goto kill_file; 2053 } 2054 2055 resop = &res.array[1]; /* open res */ 2056 op_res = &resop->nfs_resop4_u.opopen; 2057 2058 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2059 2060 /* 2061 * Check if the path we reopened really is the same 2062 * file. We could end up in a situation where the file 2063 * was removed and a new file created with the same name. 2064 */ 2065 resop = &res.array[2]; 2066 gf_res = &resop->nfs_resop4_u.opgetfh; 2067 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2068 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2069 if (fh_different) { 2070 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2071 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2072 /* Oops, we don't have the same file */ 2073 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2074 failed_msg = "Couldn't reopen: Persistent " 2075 "file handle changed"; 2076 else 2077 failed_msg = "Couldn't reopen: Volatile " 2078 "(no expire on open) file handle changed"; 2079 2080 nfs4args_copen_free(open_args); 2081 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2082 nfs_rw_exit(&mi->mi_fh_lock); 2083 goto kill_file; 2084 2085 } else { 2086 /* 2087 * We have volatile file handles that don't compare. 2088 * If the fids are the same then we assume that the 2089 * file handle expired but the rnode still refers to 2090 * the same file object. 2091 * 2092 * First check that we have fids or not. 2093 * If we don't we have a dumb server so we will 2094 * just assume every thing is ok for now. 2095 */ 2096 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2097 rp->r_attr.va_mask & AT_NODEID && 2098 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2099 /* 2100 * We have fids, but they don't 2101 * compare. So kill the file. 2102 */ 2103 failed_msg = 2104 "Couldn't reopen: file handle changed" 2105 " due to mismatched fids"; 2106 nfs4args_copen_free(open_args); 2107 (void) xdr_free(xdr_COMPOUND4res_clnt, 2108 (caddr_t)&res); 2109 nfs_rw_exit(&mi->mi_fh_lock); 2110 goto kill_file; 2111 } else { 2112 /* 2113 * We have volatile file handles that refers 2114 * to the same file (at least they have the 2115 * same fid) or we don't have fids so we 2116 * can't tell. :(. We'll be a kind and accepting 2117 * client so we'll update the rnode's file 2118 * handle with the otw handle. 2119 * 2120 * We need to drop mi->mi_fh_lock since 2121 * sh4_update acquires it. Since there is 2122 * only one recovery thread there is no 2123 * race. 2124 */ 2125 nfs_rw_exit(&mi->mi_fh_lock); 2126 sfh4_update(rp->r_fh, &gf_res->object); 2127 } 2128 } 2129 } else { 2130 nfs_rw_exit(&mi->mi_fh_lock); 2131 } 2132 2133 ASSERT(nfs4_consistent_type(vp)); 2134 2135 /* 2136 * If the server wanted an OPEN_CONFIRM but that fails, just start 2137 * over. Presumably if there is a persistent error it will show up 2138 * when we resend the OPEN. 2139 */ 2140 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2141 bool_t retry_open = FALSE; 2142 2143 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2144 cred_otw, is_recov, &retry_open, 2145 oop, FALSE, ep, NULL); 2146 if (ep->error || ep->stat) { 2147 nfs4args_copen_free(open_args); 2148 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2149 nfs4_end_open_seqid_sync(oop); 2150 open_owner_rele(oop); 2151 oop = NULL; 2152 goto top; 2153 } 2154 } 2155 2156 mutex_enter(&osp->os_sync_lock); 2157 osp->open_stateid = op_res->stateid; 2158 osp->os_delegation = 0; 2159 /* 2160 * Need to reset this bitfield for the possible case where we were 2161 * going to OTW CLOSE the file, got a non-recoverable error, and before 2162 * we could retry the CLOSE, OPENed the file again. 2163 */ 2164 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2165 osp->os_final_close = 0; 2166 osp->os_force_close = 0; 2167 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2168 osp->os_dc_openacc = open_args->share_access; 2169 mutex_exit(&osp->os_sync_lock); 2170 2171 nfs4_end_open_seqid_sync(oop); 2172 2173 /* accept delegation, if any */ 2174 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2175 2176 nfs4args_copen_free(open_args); 2177 2178 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2179 2180 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2181 2182 ASSERT(nfs4_consistent_type(vp)); 2183 2184 open_owner_rele(oop); 2185 crfree(cr); 2186 crfree(cred_otw); 2187 return; 2188 2189 kill_file: 2190 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2191 failed_reopen: 2192 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2193 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2194 (void *)osp, (void *)cr, rnode4info(rp))); 2195 mutex_enter(&osp->os_sync_lock); 2196 osp->os_failed_reopen = 1; 2197 mutex_exit(&osp->os_sync_lock); 2198 bailout: 2199 if (oop != NULL) { 2200 nfs4_end_open_seqid_sync(oop); 2201 open_owner_rele(oop); 2202 } 2203 if (cr != NULL) 2204 crfree(cr); 2205 if (cred_otw != NULL) 2206 crfree(cred_otw); 2207 } 2208 2209 /* for . and .. OPENs */ 2210 /* ARGSUSED */ 2211 static int 2212 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2213 { 2214 rnode4_t *rp; 2215 nfs4_ga_res_t gar; 2216 2217 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2218 2219 /* 2220 * If close-to-open consistency checking is turned off or 2221 * if there is no cached data, we can avoid 2222 * the over the wire getattr. Otherwise, force a 2223 * call to the server to get fresh attributes and to 2224 * check caches. This is required for close-to-open 2225 * consistency. 2226 */ 2227 rp = VTOR4(*vpp); 2228 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2229 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2230 return (0); 2231 2232 gar.n4g_va.va_mask = AT_ALL; 2233 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2234 } 2235 2236 /* 2237 * CLOSE a file 2238 */ 2239 /* ARGSUSED */ 2240 static int 2241 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2242 caller_context_t *ct) 2243 { 2244 rnode4_t *rp; 2245 int error = 0; 2246 int r_error = 0; 2247 int n4error = 0; 2248 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2249 2250 /* 2251 * Remove client state for this (lockowner, file) pair. 2252 * Issue otw v4 call to have the server do the same. 2253 */ 2254 2255 rp = VTOR4(vp); 2256 2257 /* 2258 * zone_enter(2) prevents processes from changing zones with NFS files 2259 * open; if we happen to get here from the wrong zone we can't do 2260 * anything over the wire. 2261 */ 2262 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2263 /* 2264 * We could attempt to clean up locks, except we're sure 2265 * that the current process didn't acquire any locks on 2266 * the file: any attempt to lock a file belong to another zone 2267 * will fail, and one can't lock an NFS file and then change 2268 * zones, as that fails too. 2269 * 2270 * Returning an error here is the sane thing to do. A 2271 * subsequent call to VN_RELE() which translates to a 2272 * nfs4_inactive() will clean up state: if the zone of the 2273 * vnode's origin is still alive and kicking, the inactive 2274 * thread will handle the request (from the correct zone), and 2275 * everything (minus the OTW close call) should be OK. If the 2276 * zone is going away nfs4_async_inactive() will throw away 2277 * delegations, open streams and cached pages inline. 2278 */ 2279 return (EIO); 2280 } 2281 2282 /* 2283 * If we are using local locking for this filesystem, then 2284 * release all of the SYSV style record locks. Otherwise, 2285 * we are doing network locking and we need to release all 2286 * of the network locks. All of the locks held by this 2287 * process on this file are released no matter what the 2288 * incoming reference count is. 2289 */ 2290 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2291 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2292 cleanshares(vp, ttoproc(curthread)->p_pid); 2293 } else 2294 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2295 2296 if (e.error) { 2297 struct lm_sysid *lmsid; 2298 lmsid = nfs4_find_sysid(VTOMI4(vp)); 2299 if (lmsid == NULL) { 2300 DTRACE_PROBE2(unknown__sysid, int, e.error, 2301 vnode_t *, vp); 2302 } else { 2303 cleanlocks(vp, ttoproc(curthread)->p_pid, 2304 (lm_sysidt(lmsid) | LM_SYSID_CLIENT)); 2305 2306 lm_rel_sysid(lmsid); 2307 } 2308 return (e.error); 2309 } 2310 2311 if (count > 1) 2312 return (0); 2313 2314 /* 2315 * If the file has been `unlinked', then purge the 2316 * DNLC so that this vnode will get reycled quicker 2317 * and the .nfs* file on the server will get removed. 2318 */ 2319 if (rp->r_unldvp != NULL) 2320 dnlc_purge_vp(vp); 2321 2322 /* 2323 * If the file was open for write and there are pages, 2324 * do a synchronous flush and commit of all of the 2325 * dirty and uncommitted pages. 2326 */ 2327 ASSERT(!e.error); 2328 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2329 error = nfs4_putpage_commit(vp, 0, 0, cr); 2330 2331 mutex_enter(&rp->r_statelock); 2332 r_error = rp->r_error; 2333 rp->r_error = 0; 2334 mutex_exit(&rp->r_statelock); 2335 2336 /* 2337 * If this file type is one for which no explicit 'open' was 2338 * done, then bail now (ie. no need for protocol 'close'). If 2339 * there was an error w/the vm subsystem, return _that_ error, 2340 * otherwise, return any errors that may've been reported via 2341 * the rnode. 2342 */ 2343 if (vp->v_type != VREG) 2344 return (error ? error : r_error); 2345 2346 /* 2347 * The sync putpage commit may have failed above, but since 2348 * we're working w/a regular file, we need to do the protocol 2349 * 'close' (nfs4close_one will figure out if an otw close is 2350 * needed or not). Report any errors _after_ doing the protocol 2351 * 'close'. 2352 */ 2353 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2354 n4error = e.error ? e.error : geterrno4(e.stat); 2355 2356 /* 2357 * Error reporting prio (Hi -> Lo) 2358 * 2359 * i) nfs4_putpage_commit (error) 2360 * ii) rnode's (r_error) 2361 * iii) nfs4close_one (n4error) 2362 */ 2363 return (error ? error : (r_error ? r_error : n4error)); 2364 } 2365 2366 /* 2367 * Initialize *lost_rqstp. 2368 */ 2369 2370 static void 2371 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2372 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2373 vnode_t *vp) 2374 { 2375 if (error != ETIMEDOUT && error != EINTR && 2376 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2377 lost_rqstp->lr_op = 0; 2378 return; 2379 } 2380 2381 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2382 "nfs4close_save_lost_rqst: error %d", error)); 2383 2384 lost_rqstp->lr_op = OP_CLOSE; 2385 /* 2386 * The vp is held and rele'd via the recovery code. 2387 * See nfs4_save_lost_rqst. 2388 */ 2389 lost_rqstp->lr_vp = vp; 2390 lost_rqstp->lr_dvp = NULL; 2391 lost_rqstp->lr_oop = oop; 2392 lost_rqstp->lr_osp = osp; 2393 ASSERT(osp != NULL); 2394 ASSERT(mutex_owned(&osp->os_sync_lock)); 2395 osp->os_pending_close = 1; 2396 lost_rqstp->lr_lop = NULL; 2397 lost_rqstp->lr_cr = cr; 2398 lost_rqstp->lr_flk = NULL; 2399 lost_rqstp->lr_putfirst = FALSE; 2400 } 2401 2402 /* 2403 * Assumes you already have the open seqid sync grabbed as well as the 2404 * 'os_sync_lock'. Note: this will release the open seqid sync and 2405 * 'os_sync_lock' if client recovery starts. Calling functions have to 2406 * be prepared to handle this. 2407 * 2408 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2409 * was needed and was started, and that the calling function should retry 2410 * this function; otherwise it is returned as 0. 2411 * 2412 * Errors are returned via the nfs4_error_t parameter. 2413 */ 2414 static void 2415 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2416 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2417 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2418 { 2419 COMPOUND4args_clnt args; 2420 COMPOUND4res_clnt res; 2421 CLOSE4args *close_args; 2422 nfs_resop4 *resop; 2423 nfs_argop4 argop[3]; 2424 int doqueue = 1; 2425 mntinfo4_t *mi; 2426 seqid4 seqid; 2427 vnode_t *vp; 2428 bool_t needrecov = FALSE; 2429 nfs4_lost_rqst_t lost_rqst; 2430 hrtime_t t; 2431 2432 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2433 2434 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2435 2436 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2437 2438 /* Only set this to 1 if recovery is started */ 2439 *recov = 0; 2440 2441 /* do the OTW call to close the file */ 2442 2443 if (close_type == CLOSE_RESEND) 2444 args.ctag = TAG_CLOSE_LOST; 2445 else if (close_type == CLOSE_AFTER_RESEND) 2446 args.ctag = TAG_CLOSE_UNDO; 2447 else 2448 args.ctag = TAG_CLOSE; 2449 2450 args.array_len = 3; 2451 args.array = argop; 2452 2453 vp = RTOV4(rp); 2454 2455 mi = VTOMI4(vp); 2456 2457 /* putfh target fh */ 2458 argop[0].argop = OP_CPUTFH; 2459 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2460 2461 argop[1].argop = OP_GETATTR; 2462 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2463 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2464 2465 argop[2].argop = OP_CLOSE; 2466 close_args = &argop[2].nfs_argop4_u.opclose; 2467 2468 seqid = nfs4_get_open_seqid(oop) + 1; 2469 2470 close_args->seqid = seqid; 2471 close_args->open_stateid = osp->open_stateid; 2472 2473 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2474 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2475 rnode4info(rp))); 2476 2477 t = gethrtime(); 2478 2479 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2480 2481 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2482 nfs4_set_open_seqid(seqid, oop, args.ctag); 2483 } 2484 2485 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2486 if (ep->error && !needrecov) { 2487 /* 2488 * if there was an error and no recovery is to be done 2489 * then then set up the file to flush its cache if 2490 * needed for the next caller. 2491 */ 2492 mutex_enter(&rp->r_statelock); 2493 PURGE_ATTRCACHE4_LOCKED(rp); 2494 rp->r_flags &= ~R4WRITEMODIFIED; 2495 mutex_exit(&rp->r_statelock); 2496 return; 2497 } 2498 2499 if (needrecov) { 2500 bool_t abort; 2501 nfs4_bseqid_entry_t *bsep = NULL; 2502 2503 if (close_type != CLOSE_RESEND) 2504 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2505 osp, cred_otw, vp); 2506 2507 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2508 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2509 0, args.ctag, close_args->seqid); 2510 2511 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2512 "nfs4close_otw: initiating recovery. error %d " 2513 "res.status %d", ep->error, res.status)); 2514 2515 /* 2516 * Drop the 'os_sync_lock' here so we don't hit 2517 * a potential recursive mutex_enter via an 2518 * 'open_stream_hold()'. 2519 */ 2520 mutex_exit(&osp->os_sync_lock); 2521 *have_sync_lockp = 0; 2522 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2523 (close_type != CLOSE_RESEND && 2524 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2525 OP_CLOSE, bsep, NULL, NULL); 2526 2527 /* drop open seq sync, and let the calling function regrab it */ 2528 nfs4_end_open_seqid_sync(oop); 2529 *did_start_seqid_syncp = 0; 2530 2531 if (bsep) 2532 kmem_free(bsep, sizeof (*bsep)); 2533 /* 2534 * For signals, the caller wants to quit, so don't say to 2535 * retry. For forced unmount, if it's a user thread, it 2536 * wants to quit. If it's a recovery thread, the retry 2537 * will happen higher-up on the call stack. Either way, 2538 * don't say to retry. 2539 */ 2540 if (abort == FALSE && ep->error != EINTR && 2541 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2542 close_type != CLOSE_RESEND && 2543 close_type != CLOSE_AFTER_RESEND) 2544 *recov = 1; 2545 else 2546 *recov = 0; 2547 2548 if (!ep->error) 2549 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2550 return; 2551 } 2552 2553 if (res.status) { 2554 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2555 return; 2556 } 2557 2558 mutex_enter(&rp->r_statev4_lock); 2559 rp->created_v4 = 0; 2560 mutex_exit(&rp->r_statev4_lock); 2561 2562 resop = &res.array[2]; 2563 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2564 osp->os_valid = 0; 2565 2566 /* 2567 * This removes the reference obtained at OPEN; ie, when the 2568 * open stream structure was created. 2569 * 2570 * We don't have to worry about calling 'open_stream_rele' 2571 * since we our currently holding a reference to the open 2572 * stream which means the count cannot go to 0 with this 2573 * decrement. 2574 */ 2575 ASSERT(osp->os_ref_count >= 2); 2576 osp->os_ref_count--; 2577 2578 if (ep->error == 0) { 2579 /* 2580 * Avoid a deadlock with the r_serial thread waiting for 2581 * os_sync_lock in nfs4_get_otw_cred_by_osp() which might be 2582 * held by us. We will wait in nfs4_attr_cache() for the 2583 * completion of the r_serial thread. 2584 */ 2585 mutex_exit(&osp->os_sync_lock); 2586 *have_sync_lockp = 0; 2587 2588 nfs4_attr_cache(vp, 2589 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2590 t, cred_otw, TRUE, NULL); 2591 } 2592 2593 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2594 " returning %d", ep->error)); 2595 2596 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2597 } 2598 2599 /* ARGSUSED */ 2600 static int 2601 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2602 caller_context_t *ct) 2603 { 2604 rnode4_t *rp; 2605 u_offset_t off; 2606 offset_t diff; 2607 uint_t on; 2608 uint_t n; 2609 caddr_t base; 2610 uint_t flags; 2611 int error; 2612 mntinfo4_t *mi; 2613 2614 rp = VTOR4(vp); 2615 2616 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2617 2618 if (IS_SHADOW(vp, rp)) 2619 vp = RTOV4(rp); 2620 2621 if (vp->v_type != VREG) 2622 return (EISDIR); 2623 2624 mi = VTOMI4(vp); 2625 2626 if (nfs_zone() != mi->mi_zone) 2627 return (EIO); 2628 2629 if (uiop->uio_resid == 0) 2630 return (0); 2631 2632 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2633 return (EINVAL); 2634 2635 mutex_enter(&rp->r_statelock); 2636 if (rp->r_flags & R4RECOVERRP) 2637 error = (rp->r_error ? rp->r_error : EIO); 2638 else 2639 error = 0; 2640 mutex_exit(&rp->r_statelock); 2641 if (error) 2642 return (error); 2643 2644 /* 2645 * Bypass VM if caching has been disabled (e.g., locking) or if 2646 * using client-side direct I/O and the file is not mmap'd and 2647 * there are no cached pages. 2648 */ 2649 if ((vp->v_flag & VNOCACHE) || 2650 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2651 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2652 size_t resid = 0; 2653 2654 return (nfs4read(vp, NULL, uiop->uio_loffset, 2655 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2656 } 2657 2658 error = 0; 2659 2660 do { 2661 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2662 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2663 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2664 2665 if (error = nfs4_validate_caches(vp, cr)) 2666 break; 2667 2668 mutex_enter(&rp->r_statelock); 2669 while (rp->r_flags & R4INCACHEPURGE) { 2670 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2671 mutex_exit(&rp->r_statelock); 2672 return (EINTR); 2673 } 2674 } 2675 diff = rp->r_size - uiop->uio_loffset; 2676 mutex_exit(&rp->r_statelock); 2677 if (diff <= 0) 2678 break; 2679 if (diff < n) 2680 n = (uint_t)diff; 2681 2682 if (vpm_enable) { 2683 /* 2684 * Copy data. 2685 */ 2686 error = vpm_data_copy(vp, off + on, n, uiop, 2687 1, NULL, 0, S_READ); 2688 } else { 2689 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2690 S_READ); 2691 2692 error = uiomove(base + on, n, UIO_READ, uiop); 2693 } 2694 2695 if (!error) { 2696 /* 2697 * If read a whole block or read to eof, 2698 * won't need this buffer again soon. 2699 */ 2700 mutex_enter(&rp->r_statelock); 2701 if (n + on == MAXBSIZE || 2702 uiop->uio_loffset == rp->r_size) 2703 flags = SM_DONTNEED; 2704 else 2705 flags = 0; 2706 mutex_exit(&rp->r_statelock); 2707 if (vpm_enable) { 2708 error = vpm_sync_pages(vp, off, n, flags); 2709 } else { 2710 error = segmap_release(segkmap, base, flags); 2711 } 2712 } else { 2713 if (vpm_enable) { 2714 (void) vpm_sync_pages(vp, off, n, 0); 2715 } else { 2716 (void) segmap_release(segkmap, base, 0); 2717 } 2718 } 2719 } while (!error && uiop->uio_resid > 0); 2720 2721 return (error); 2722 } 2723 2724 /* ARGSUSED */ 2725 static int 2726 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2727 caller_context_t *ct) 2728 { 2729 rlim64_t limit = uiop->uio_llimit; 2730 rnode4_t *rp; 2731 u_offset_t off; 2732 caddr_t base; 2733 uint_t flags; 2734 int remainder; 2735 size_t n; 2736 int on; 2737 int error; 2738 int resid; 2739 u_offset_t offset; 2740 mntinfo4_t *mi; 2741 uint_t bsize; 2742 2743 rp = VTOR4(vp); 2744 2745 if (IS_SHADOW(vp, rp)) 2746 vp = RTOV4(rp); 2747 2748 if (vp->v_type != VREG) 2749 return (EISDIR); 2750 2751 mi = VTOMI4(vp); 2752 2753 if (nfs_zone() != mi->mi_zone) 2754 return (EIO); 2755 2756 if (uiop->uio_resid == 0) 2757 return (0); 2758 2759 mutex_enter(&rp->r_statelock); 2760 if (rp->r_flags & R4RECOVERRP) 2761 error = (rp->r_error ? rp->r_error : EIO); 2762 else 2763 error = 0; 2764 mutex_exit(&rp->r_statelock); 2765 if (error) 2766 return (error); 2767 2768 if (ioflag & FAPPEND) { 2769 struct vattr va; 2770 2771 /* 2772 * Must serialize if appending. 2773 */ 2774 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2775 nfs_rw_exit(&rp->r_rwlock); 2776 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2777 INTR4(vp))) 2778 return (EINTR); 2779 } 2780 2781 va.va_mask = AT_SIZE; 2782 error = nfs4getattr(vp, &va, cr); 2783 if (error) 2784 return (error); 2785 uiop->uio_loffset = va.va_size; 2786 } 2787 2788 offset = uiop->uio_loffset + uiop->uio_resid; 2789 2790 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2791 return (EINVAL); 2792 2793 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2794 limit = MAXOFFSET_T; 2795 2796 /* 2797 * Check to make sure that the process will not exceed 2798 * its limit on file size. It is okay to write up to 2799 * the limit, but not beyond. Thus, the write which 2800 * reaches the limit will be short and the next write 2801 * will return an error. 2802 */ 2803 remainder = 0; 2804 if (offset > uiop->uio_llimit) { 2805 remainder = offset - uiop->uio_llimit; 2806 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2807 if (uiop->uio_resid <= 0) { 2808 proc_t *p = ttoproc(curthread); 2809 2810 uiop->uio_resid += remainder; 2811 mutex_enter(&p->p_lock); 2812 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2813 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2814 mutex_exit(&p->p_lock); 2815 return (EFBIG); 2816 } 2817 } 2818 2819 /* update the change attribute, if we have a write delegation */ 2820 2821 mutex_enter(&rp->r_statev4_lock); 2822 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2823 rp->r_deleg_change++; 2824 2825 mutex_exit(&rp->r_statev4_lock); 2826 2827 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2828 return (EINTR); 2829 2830 /* 2831 * Bypass VM if caching has been disabled (e.g., locking) or if 2832 * using client-side direct I/O and the file is not mmap'd and 2833 * there are no cached pages. 2834 */ 2835 if ((vp->v_flag & VNOCACHE) || 2836 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2837 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2838 size_t bufsize; 2839 int count; 2840 u_offset_t org_offset; 2841 stable_how4 stab_comm; 2842 nfs4_fwrite: 2843 if (rp->r_flags & R4STALE) { 2844 resid = uiop->uio_resid; 2845 offset = uiop->uio_loffset; 2846 error = rp->r_error; 2847 /* 2848 * A close may have cleared r_error, if so, 2849 * propagate ESTALE error return properly 2850 */ 2851 if (error == 0) 2852 error = ESTALE; 2853 goto bottom; 2854 } 2855 2856 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2857 base = kmem_alloc(bufsize, KM_SLEEP); 2858 do { 2859 if (ioflag & FDSYNC) 2860 stab_comm = DATA_SYNC4; 2861 else 2862 stab_comm = FILE_SYNC4; 2863 resid = uiop->uio_resid; 2864 offset = uiop->uio_loffset; 2865 count = MIN(uiop->uio_resid, bufsize); 2866 org_offset = uiop->uio_loffset; 2867 error = uiomove(base, count, UIO_WRITE, uiop); 2868 if (!error) { 2869 error = nfs4write(vp, base, org_offset, 2870 count, cr, &stab_comm); 2871 if (!error) { 2872 mutex_enter(&rp->r_statelock); 2873 if (rp->r_size < uiop->uio_loffset) 2874 rp->r_size = uiop->uio_loffset; 2875 mutex_exit(&rp->r_statelock); 2876 } 2877 } 2878 } while (!error && uiop->uio_resid > 0); 2879 kmem_free(base, bufsize); 2880 goto bottom; 2881 } 2882 2883 bsize = vp->v_vfsp->vfs_bsize; 2884 2885 do { 2886 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2887 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2888 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2889 2890 resid = uiop->uio_resid; 2891 offset = uiop->uio_loffset; 2892 2893 if (rp->r_flags & R4STALE) { 2894 error = rp->r_error; 2895 /* 2896 * A close may have cleared r_error, if so, 2897 * propagate ESTALE error return properly 2898 */ 2899 if (error == 0) 2900 error = ESTALE; 2901 break; 2902 } 2903 2904 /* 2905 * Don't create dirty pages faster than they 2906 * can be cleaned so that the system doesn't 2907 * get imbalanced. If the async queue is 2908 * maxed out, then wait for it to drain before 2909 * creating more dirty pages. Also, wait for 2910 * any threads doing pagewalks in the vop_getattr 2911 * entry points so that they don't block for 2912 * long periods. 2913 */ 2914 mutex_enter(&rp->r_statelock); 2915 while ((mi->mi_max_threads != 0 && 2916 rp->r_awcount > 2 * mi->mi_max_threads) || 2917 rp->r_gcount > 0) { 2918 if (INTR4(vp)) { 2919 klwp_t *lwp = ttolwp(curthread); 2920 2921 if (lwp != NULL) 2922 lwp->lwp_nostop++; 2923 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2924 mutex_exit(&rp->r_statelock); 2925 if (lwp != NULL) 2926 lwp->lwp_nostop--; 2927 error = EINTR; 2928 goto bottom; 2929 } 2930 if (lwp != NULL) 2931 lwp->lwp_nostop--; 2932 } else 2933 cv_wait(&rp->r_cv, &rp->r_statelock); 2934 } 2935 mutex_exit(&rp->r_statelock); 2936 2937 /* 2938 * Touch the page and fault it in if it is not in core 2939 * before segmap_getmapflt or vpm_data_copy can lock it. 2940 * This is to avoid the deadlock if the buffer is mapped 2941 * to the same file through mmap which we want to write. 2942 */ 2943 uio_prefaultpages((long)n, uiop); 2944 2945 if (vpm_enable) { 2946 /* 2947 * It will use kpm mappings, so no need to 2948 * pass an address. 2949 */ 2950 error = writerp4(rp, NULL, n, uiop, 0); 2951 } else { 2952 if (segmap_kpm) { 2953 int pon = uiop->uio_loffset & PAGEOFFSET; 2954 size_t pn = MIN(PAGESIZE - pon, 2955 uiop->uio_resid); 2956 int pagecreate; 2957 2958 mutex_enter(&rp->r_statelock); 2959 pagecreate = (pon == 0) && (pn == PAGESIZE || 2960 uiop->uio_loffset + pn >= rp->r_size); 2961 mutex_exit(&rp->r_statelock); 2962 2963 base = segmap_getmapflt(segkmap, vp, off + on, 2964 pn, !pagecreate, S_WRITE); 2965 2966 error = writerp4(rp, base + pon, n, uiop, 2967 pagecreate); 2968 2969 } else { 2970 base = segmap_getmapflt(segkmap, vp, off + on, 2971 n, 0, S_READ); 2972 error = writerp4(rp, base + on, n, uiop, 0); 2973 } 2974 } 2975 2976 if (!error) { 2977 if (mi->mi_flags & MI4_NOAC) 2978 flags = SM_WRITE; 2979 else if ((uiop->uio_loffset % bsize) == 0 || 2980 IS_SWAPVP(vp)) { 2981 /* 2982 * Have written a whole block. 2983 * Start an asynchronous write 2984 * and mark the buffer to 2985 * indicate that it won't be 2986 * needed again soon. 2987 */ 2988 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2989 } else 2990 flags = 0; 2991 if ((ioflag & (FSYNC|FDSYNC)) || 2992 (rp->r_flags & R4OUTOFSPACE)) { 2993 flags &= ~SM_ASYNC; 2994 flags |= SM_WRITE; 2995 } 2996 if (vpm_enable) { 2997 error = vpm_sync_pages(vp, off, n, flags); 2998 } else { 2999 error = segmap_release(segkmap, base, flags); 3000 } 3001 } else { 3002 if (vpm_enable) { 3003 (void) vpm_sync_pages(vp, off, n, 0); 3004 } else { 3005 (void) segmap_release(segkmap, base, 0); 3006 } 3007 /* 3008 * In the event that we got an access error while 3009 * faulting in a page for a write-only file just 3010 * force a write. 3011 */ 3012 if (error == EACCES) 3013 goto nfs4_fwrite; 3014 } 3015 } while (!error && uiop->uio_resid > 0); 3016 3017 bottom: 3018 if (error) { 3019 uiop->uio_resid = resid + remainder; 3020 uiop->uio_loffset = offset; 3021 } else { 3022 uiop->uio_resid += remainder; 3023 3024 mutex_enter(&rp->r_statev4_lock); 3025 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 3026 gethrestime(&rp->r_attr.va_mtime); 3027 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3028 } 3029 mutex_exit(&rp->r_statev4_lock); 3030 } 3031 3032 nfs_rw_exit(&rp->r_lkserlock); 3033 3034 return (error); 3035 } 3036 3037 /* 3038 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 3039 */ 3040 static int 3041 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 3042 int flags, cred_t *cr) 3043 { 3044 struct buf *bp; 3045 int error; 3046 page_t *savepp; 3047 uchar_t fsdata; 3048 stable_how4 stab_comm; 3049 3050 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3051 bp = pageio_setup(pp, len, vp, flags); 3052 ASSERT(bp != NULL); 3053 3054 /* 3055 * pageio_setup should have set b_addr to 0. This 3056 * is correct since we want to do I/O on a page 3057 * boundary. bp_mapin will use this addr to calculate 3058 * an offset, and then set b_addr to the kernel virtual 3059 * address it allocated for us. 3060 */ 3061 ASSERT(bp->b_un.b_addr == 0); 3062 3063 bp->b_edev = 0; 3064 bp->b_dev = 0; 3065 bp->b_lblkno = lbtodb(off); 3066 bp->b_file = vp; 3067 bp->b_offset = (offset_t)off; 3068 bp_mapin(bp); 3069 3070 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 3071 freemem > desfree) 3072 stab_comm = UNSTABLE4; 3073 else 3074 stab_comm = FILE_SYNC4; 3075 3076 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 3077 3078 bp_mapout(bp); 3079 pageio_done(bp); 3080 3081 if (stab_comm == UNSTABLE4) 3082 fsdata = C_DELAYCOMMIT; 3083 else 3084 fsdata = C_NOCOMMIT; 3085 3086 savepp = pp; 3087 do { 3088 pp->p_fsdata = fsdata; 3089 } while ((pp = pp->p_next) != savepp); 3090 3091 return (error); 3092 } 3093 3094 /* 3095 */ 3096 static int 3097 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3098 { 3099 nfs4_open_owner_t *oop; 3100 nfs4_open_stream_t *osp; 3101 rnode4_t *rp = VTOR4(vp); 3102 mntinfo4_t *mi = VTOMI4(vp); 3103 int reopen_needed; 3104 3105 ASSERT(nfs_zone() == mi->mi_zone); 3106 3107 3108 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3109 if (!oop) 3110 return (EIO); 3111 3112 /* returns with 'os_sync_lock' held */ 3113 osp = find_open_stream(oop, rp); 3114 if (!osp) { 3115 open_owner_rele(oop); 3116 return (EIO); 3117 } 3118 3119 if (osp->os_failed_reopen) { 3120 mutex_exit(&osp->os_sync_lock); 3121 open_stream_rele(osp, rp); 3122 open_owner_rele(oop); 3123 return (EIO); 3124 } 3125 3126 /* 3127 * Determine whether a reopen is needed. If this 3128 * is a delegation open stream, then the os_delegation bit 3129 * should be set. 3130 */ 3131 3132 reopen_needed = osp->os_delegation; 3133 3134 mutex_exit(&osp->os_sync_lock); 3135 open_owner_rele(oop); 3136 3137 if (reopen_needed) { 3138 nfs4_error_zinit(ep); 3139 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3140 mutex_enter(&osp->os_sync_lock); 3141 if (ep->error || ep->stat || osp->os_failed_reopen) { 3142 mutex_exit(&osp->os_sync_lock); 3143 open_stream_rele(osp, rp); 3144 return (EIO); 3145 } 3146 mutex_exit(&osp->os_sync_lock); 3147 } 3148 open_stream_rele(osp, rp); 3149 3150 return (0); 3151 } 3152 3153 /* 3154 * Write to file. Writes to remote server in largest size 3155 * chunks that the server can handle. Write is synchronous. 3156 */ 3157 static int 3158 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3159 stable_how4 *stab_comm) 3160 { 3161 mntinfo4_t *mi; 3162 COMPOUND4args_clnt args; 3163 COMPOUND4res_clnt res; 3164 WRITE4args *wargs; 3165 WRITE4res *wres; 3166 nfs_argop4 argop[2]; 3167 nfs_resop4 *resop; 3168 int tsize; 3169 stable_how4 stable; 3170 rnode4_t *rp; 3171 int doqueue = 1; 3172 bool_t needrecov; 3173 nfs4_recov_state_t recov_state; 3174 nfs4_stateid_types_t sid_types; 3175 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3176 int recov; 3177 3178 rp = VTOR4(vp); 3179 mi = VTOMI4(vp); 3180 3181 ASSERT(nfs_zone() == mi->mi_zone); 3182 3183 stable = *stab_comm; 3184 *stab_comm = FILE_SYNC4; 3185 3186 needrecov = FALSE; 3187 recov_state.rs_flags = 0; 3188 recov_state.rs_num_retry_despite_err = 0; 3189 nfs4_init_stateid_types(&sid_types); 3190 3191 /* Is curthread the recovery thread? */ 3192 mutex_enter(&mi->mi_lock); 3193 recov = (mi->mi_recovthread == curthread); 3194 mutex_exit(&mi->mi_lock); 3195 3196 recov_retry: 3197 args.ctag = TAG_WRITE; 3198 args.array_len = 2; 3199 args.array = argop; 3200 3201 if (!recov) { 3202 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3203 &recov_state, NULL); 3204 if (e.error) 3205 return (e.error); 3206 } 3207 3208 /* 0. putfh target fh */ 3209 argop[0].argop = OP_CPUTFH; 3210 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3211 3212 /* 1. write */ 3213 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3214 3215 do { 3216 3217 wargs->offset = (offset4)offset; 3218 wargs->data_val = base; 3219 3220 if (mi->mi_io_kstats) { 3221 mutex_enter(&mi->mi_lock); 3222 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3223 mutex_exit(&mi->mi_lock); 3224 } 3225 3226 if ((vp->v_flag & VNOCACHE) || 3227 (rp->r_flags & R4DIRECTIO) || 3228 (mi->mi_flags & MI4_DIRECTIO)) 3229 tsize = MIN(mi->mi_stsize, count); 3230 else 3231 tsize = MIN(mi->mi_curwrite, count); 3232 wargs->data_len = (uint_t)tsize; 3233 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3234 3235 if (mi->mi_io_kstats) { 3236 mutex_enter(&mi->mi_lock); 3237 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3238 mutex_exit(&mi->mi_lock); 3239 } 3240 3241 if (!recov) { 3242 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3243 if (e.error && !needrecov) { 3244 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3245 &recov_state, needrecov); 3246 return (e.error); 3247 } 3248 } else { 3249 if (e.error) 3250 return (e.error); 3251 } 3252 3253 /* 3254 * Do handling of OLD_STATEID outside 3255 * of the normal recovery framework. 3256 * 3257 * If write receives a BAD stateid error while using a 3258 * delegation stateid, retry using the open stateid (if it 3259 * exists). If it doesn't have an open stateid, reopen the 3260 * file first, then retry. 3261 */ 3262 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3263 sid_types.cur_sid_type != SPEC_SID) { 3264 nfs4_save_stateid(&wargs->stateid, &sid_types); 3265 if (!recov) 3266 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3267 &recov_state, needrecov); 3268 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3269 goto recov_retry; 3270 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3271 sid_types.cur_sid_type == DEL_SID) { 3272 nfs4_save_stateid(&wargs->stateid, &sid_types); 3273 mutex_enter(&rp->r_statev4_lock); 3274 rp->r_deleg_return_pending = TRUE; 3275 mutex_exit(&rp->r_statev4_lock); 3276 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3277 if (!recov) 3278 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3279 &recov_state, needrecov); 3280 (void) xdr_free(xdr_COMPOUND4res_clnt, 3281 (caddr_t)&res); 3282 return (EIO); 3283 } 3284 if (!recov) 3285 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3286 &recov_state, needrecov); 3287 /* hold needed for nfs4delegreturn_thread */ 3288 VN_HOLD(vp); 3289 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3290 NFS4_DR_DISCARD), FALSE); 3291 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3292 goto recov_retry; 3293 } 3294 3295 if (needrecov) { 3296 bool_t abort; 3297 3298 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3299 "nfs4write: client got error %d, res.status %d" 3300 ", so start recovery", e.error, res.status)); 3301 3302 abort = nfs4_start_recovery(&e, 3303 VTOMI4(vp), vp, NULL, &wargs->stateid, 3304 NULL, OP_WRITE, NULL, NULL, NULL); 3305 if (!e.error) { 3306 e.error = geterrno4(res.status); 3307 (void) xdr_free(xdr_COMPOUND4res_clnt, 3308 (caddr_t)&res); 3309 } 3310 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3311 &recov_state, needrecov); 3312 if (abort == FALSE) 3313 goto recov_retry; 3314 return (e.error); 3315 } 3316 3317 if (res.status) { 3318 e.error = geterrno4(res.status); 3319 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3320 if (!recov) 3321 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3322 &recov_state, needrecov); 3323 return (e.error); 3324 } 3325 3326 resop = &res.array[1]; /* write res */ 3327 wres = &resop->nfs_resop4_u.opwrite; 3328 3329 if ((int)wres->count > tsize) { 3330 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3331 3332 zcmn_err(getzoneid(), CE_WARN, 3333 "nfs4write: server wrote %u, requested was %u", 3334 (int)wres->count, tsize); 3335 if (!recov) 3336 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3337 &recov_state, needrecov); 3338 return (EIO); 3339 } 3340 if (wres->committed == UNSTABLE4) { 3341 *stab_comm = UNSTABLE4; 3342 if (wargs->stable == DATA_SYNC4 || 3343 wargs->stable == FILE_SYNC4) { 3344 (void) xdr_free(xdr_COMPOUND4res_clnt, 3345 (caddr_t)&res); 3346 zcmn_err(getzoneid(), CE_WARN, 3347 "nfs4write: server %s did not commit " 3348 "to stable storage", 3349 rp->r_server->sv_hostname); 3350 if (!recov) 3351 nfs4_end_fop(VTOMI4(vp), vp, NULL, 3352 OH_WRITE, &recov_state, needrecov); 3353 return (EIO); 3354 } 3355 } 3356 3357 tsize = (int)wres->count; 3358 count -= tsize; 3359 base += tsize; 3360 offset += tsize; 3361 if (mi->mi_io_kstats) { 3362 mutex_enter(&mi->mi_lock); 3363 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3364 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3365 tsize; 3366 mutex_exit(&mi->mi_lock); 3367 } 3368 lwp_stat_update(LWP_STAT_OUBLK, 1); 3369 mutex_enter(&rp->r_statelock); 3370 if (rp->r_flags & R4HAVEVERF) { 3371 if (rp->r_writeverf != wres->writeverf) { 3372 nfs4_set_mod(vp); 3373 rp->r_writeverf = wres->writeverf; 3374 } 3375 } else { 3376 rp->r_writeverf = wres->writeverf; 3377 rp->r_flags |= R4HAVEVERF; 3378 } 3379 PURGE_ATTRCACHE4_LOCKED(rp); 3380 rp->r_flags |= R4WRITEMODIFIED; 3381 gethrestime(&rp->r_attr.va_mtime); 3382 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3383 mutex_exit(&rp->r_statelock); 3384 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3385 } while (count); 3386 3387 if (!recov) 3388 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, 3389 needrecov); 3390 3391 return (e.error); 3392 } 3393 3394 /* 3395 * Read from a file. Reads data in largest chunks our interface can handle. 3396 */ 3397 static int 3398 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3399 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3400 { 3401 mntinfo4_t *mi; 3402 COMPOUND4args_clnt args; 3403 COMPOUND4res_clnt res; 3404 READ4args *rargs; 3405 nfs_argop4 argop[2]; 3406 int tsize; 3407 int doqueue; 3408 rnode4_t *rp; 3409 int data_len; 3410 bool_t is_eof; 3411 bool_t needrecov = FALSE; 3412 nfs4_recov_state_t recov_state; 3413 nfs4_stateid_types_t sid_types; 3414 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3415 3416 rp = VTOR4(vp); 3417 mi = VTOMI4(vp); 3418 doqueue = 1; 3419 3420 ASSERT(nfs_zone() == mi->mi_zone); 3421 3422 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3423 3424 args.array_len = 2; 3425 args.array = argop; 3426 3427 nfs4_init_stateid_types(&sid_types); 3428 3429 recov_state.rs_flags = 0; 3430 recov_state.rs_num_retry_despite_err = 0; 3431 3432 recov_retry: 3433 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3434 &recov_state, NULL); 3435 if (e.error) 3436 return (e.error); 3437 3438 /* putfh target fh */ 3439 argop[0].argop = OP_CPUTFH; 3440 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3441 3442 /* read */ 3443 argop[1].argop = OP_READ; 3444 rargs = &argop[1].nfs_argop4_u.opread; 3445 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3446 OP_READ, &sid_types, async); 3447 3448 do { 3449 if (mi->mi_io_kstats) { 3450 mutex_enter(&mi->mi_lock); 3451 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3452 mutex_exit(&mi->mi_lock); 3453 } 3454 3455 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3456 "nfs4read: %s call, rp %s", 3457 needrecov ? "recov" : "first", 3458 rnode4info(rp))); 3459 3460 if ((vp->v_flag & VNOCACHE) || 3461 (rp->r_flags & R4DIRECTIO) || 3462 (mi->mi_flags & MI4_DIRECTIO)) 3463 tsize = MIN(mi->mi_tsize, count); 3464 else 3465 tsize = MIN(mi->mi_curread, count); 3466 3467 rargs->offset = (offset4)offset; 3468 rargs->count = (count4)tsize; 3469 rargs->res_data_val_alt = NULL; 3470 rargs->res_mblk = NULL; 3471 rargs->res_uiop = NULL; 3472 rargs->res_maxsize = 0; 3473 rargs->wlist = NULL; 3474 3475 if (uiop) 3476 rargs->res_uiop = uiop; 3477 else 3478 rargs->res_data_val_alt = base; 3479 rargs->res_maxsize = tsize; 3480 3481 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3482 #ifdef DEBUG 3483 if (nfs4read_error_inject) { 3484 res.status = nfs4read_error_inject; 3485 nfs4read_error_inject = 0; 3486 } 3487 #endif 3488 3489 if (mi->mi_io_kstats) { 3490 mutex_enter(&mi->mi_lock); 3491 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3492 mutex_exit(&mi->mi_lock); 3493 } 3494 3495 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3496 if (e.error != 0 && !needrecov) { 3497 nfs4_end_fop(mi, vp, NULL, OH_READ, 3498 &recov_state, needrecov); 3499 return (e.error); 3500 } 3501 3502 /* 3503 * Do proper retry for OLD and BAD stateid errors outside 3504 * of the normal recovery framework. There are two differences 3505 * between async and sync reads. The first is that we allow 3506 * retry on BAD_STATEID for async reads, but not sync reads. 3507 * The second is that we mark the file dead for a failed 3508 * attempt with a special stateid for sync reads, but just 3509 * return EIO for async reads. 3510 * 3511 * If a sync read receives a BAD stateid error while using a 3512 * delegation stateid, retry using the open stateid (if it 3513 * exists). If it doesn't have an open stateid, reopen the 3514 * file first, then retry. 3515 */ 3516 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3517 res.status == NFS4ERR_BAD_STATEID) && async) { 3518 nfs4_end_fop(mi, vp, NULL, OH_READ, 3519 &recov_state, needrecov); 3520 if (sid_types.cur_sid_type == SPEC_SID) { 3521 (void) xdr_free(xdr_COMPOUND4res_clnt, 3522 (caddr_t)&res); 3523 return (EIO); 3524 } 3525 nfs4_save_stateid(&rargs->stateid, &sid_types); 3526 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3527 goto recov_retry; 3528 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3529 !async && sid_types.cur_sid_type != SPEC_SID) { 3530 nfs4_save_stateid(&rargs->stateid, &sid_types); 3531 nfs4_end_fop(mi, vp, NULL, OH_READ, 3532 &recov_state, needrecov); 3533 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3534 goto recov_retry; 3535 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3536 sid_types.cur_sid_type == DEL_SID) { 3537 nfs4_save_stateid(&rargs->stateid, &sid_types); 3538 mutex_enter(&rp->r_statev4_lock); 3539 rp->r_deleg_return_pending = TRUE; 3540 mutex_exit(&rp->r_statev4_lock); 3541 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3542 nfs4_end_fop(mi, vp, NULL, OH_READ, 3543 &recov_state, needrecov); 3544 (void) xdr_free(xdr_COMPOUND4res_clnt, 3545 (caddr_t)&res); 3546 return (EIO); 3547 } 3548 nfs4_end_fop(mi, vp, NULL, OH_READ, 3549 &recov_state, needrecov); 3550 /* hold needed for nfs4delegreturn_thread */ 3551 VN_HOLD(vp); 3552 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3553 NFS4_DR_DISCARD), FALSE); 3554 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3555 goto recov_retry; 3556 } 3557 if (needrecov) { 3558 bool_t abort; 3559 3560 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3561 "nfs4read: initiating recovery\n")); 3562 abort = nfs4_start_recovery(&e, 3563 mi, vp, NULL, &rargs->stateid, 3564 NULL, OP_READ, NULL, NULL, NULL); 3565 nfs4_end_fop(mi, vp, NULL, OH_READ, 3566 &recov_state, needrecov); 3567 /* 3568 * Do not retry if we got OLD_STATEID using a special 3569 * stateid. This avoids looping with a broken server. 3570 */ 3571 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3572 sid_types.cur_sid_type == SPEC_SID) 3573 abort = TRUE; 3574 3575 if (abort == FALSE) { 3576 /* 3577 * Need to retry all possible stateids in 3578 * case the recovery error wasn't stateid 3579 * related or the stateids have become 3580 * stale (server reboot). 3581 */ 3582 nfs4_init_stateid_types(&sid_types); 3583 (void) xdr_free(xdr_COMPOUND4res_clnt, 3584 (caddr_t)&res); 3585 goto recov_retry; 3586 } 3587 3588 if (!e.error) { 3589 e.error = geterrno4(res.status); 3590 (void) xdr_free(xdr_COMPOUND4res_clnt, 3591 (caddr_t)&res); 3592 } 3593 return (e.error); 3594 } 3595 3596 if (res.status) { 3597 e.error = geterrno4(res.status); 3598 nfs4_end_fop(mi, vp, NULL, OH_READ, 3599 &recov_state, needrecov); 3600 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3601 return (e.error); 3602 } 3603 3604 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3605 count -= data_len; 3606 if (base) 3607 base += data_len; 3608 offset += data_len; 3609 if (mi->mi_io_kstats) { 3610 mutex_enter(&mi->mi_lock); 3611 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3612 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3613 mutex_exit(&mi->mi_lock); 3614 } 3615 lwp_stat_update(LWP_STAT_INBLK, 1); 3616 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3617 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3618 3619 } while (count && !is_eof); 3620 3621 *residp = count; 3622 3623 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3624 3625 return (e.error); 3626 } 3627 3628 /* ARGSUSED */ 3629 static int 3630 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp, 3631 caller_context_t *ct) 3632 { 3633 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3634 return (EIO); 3635 switch (cmd) { 3636 case _FIODIRECTIO: 3637 return (nfs4_directio(vp, (int)arg, cr)); 3638 default: 3639 return (ENOTTY); 3640 } 3641 } 3642 3643 /* ARGSUSED */ 3644 int 3645 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3646 caller_context_t *ct) 3647 { 3648 int error; 3649 rnode4_t *rp = VTOR4(vp); 3650 3651 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3652 return (EIO); 3653 /* 3654 * If it has been specified that the return value will 3655 * just be used as a hint, and we are only being asked 3656 * for size, fsid or rdevid, then return the client's 3657 * notion of these values without checking to make sure 3658 * that the attribute cache is up to date. 3659 * The whole point is to avoid an over the wire GETATTR 3660 * call. 3661 */ 3662 if (flags & ATTR_HINT) { 3663 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) { 3664 mutex_enter(&rp->r_statelock); 3665 if (vap->va_mask & AT_SIZE) 3666 vap->va_size = rp->r_size; 3667 if (vap->va_mask & AT_FSID) 3668 vap->va_fsid = rp->r_attr.va_fsid; 3669 if (vap->va_mask & AT_RDEV) 3670 vap->va_rdev = rp->r_attr.va_rdev; 3671 mutex_exit(&rp->r_statelock); 3672 return (0); 3673 } 3674 } 3675 3676 /* 3677 * Only need to flush pages if asking for the mtime 3678 * and if there any dirty pages or any outstanding 3679 * asynchronous (write) requests for this file. 3680 */ 3681 if (vap->va_mask & AT_MTIME) { 3682 rp = VTOR4(vp); 3683 if (nfs4_has_pages(vp)) { 3684 mutex_enter(&rp->r_statev4_lock); 3685 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3686 mutex_exit(&rp->r_statev4_lock); 3687 if (rp->r_flags & R4DIRTY || 3688 rp->r_awcount > 0) { 3689 mutex_enter(&rp->r_statelock); 3690 rp->r_gcount++; 3691 mutex_exit(&rp->r_statelock); 3692 error = 3693 nfs4_putpage(vp, (u_offset_t)0, 3694 0, 0, cr, NULL); 3695 mutex_enter(&rp->r_statelock); 3696 if (error && (error == ENOSPC || 3697 error == EDQUOT)) { 3698 if (!rp->r_error) 3699 rp->r_error = error; 3700 } 3701 if (--rp->r_gcount == 0) 3702 cv_broadcast(&rp->r_cv); 3703 mutex_exit(&rp->r_statelock); 3704 } 3705 } else { 3706 mutex_exit(&rp->r_statev4_lock); 3707 } 3708 } 3709 } 3710 return (nfs4getattr(vp, vap, cr)); 3711 } 3712 3713 int 3714 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3715 { 3716 /* 3717 * If these are the only two bits cleared 3718 * on the server then return 0 (OK) else 3719 * return 1 (BAD). 3720 */ 3721 on_client &= ~(S_ISUID|S_ISGID); 3722 if (on_client == from_server) 3723 return (0); 3724 else 3725 return (1); 3726 } 3727 3728 /*ARGSUSED4*/ 3729 static int 3730 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3731 caller_context_t *ct) 3732 { 3733 int error; 3734 3735 if (vap->va_mask & AT_NOSET) 3736 return (EINVAL); 3737 3738 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3739 return (EIO); 3740 3741 /* 3742 * Don't call secpolicy_vnode_setattr, the client cannot 3743 * use its cached attributes to make security decisions 3744 * as the server may be faking mode bits or mapping uid/gid. 3745 * Always just let the server to the checking. 3746 * If we provide the ability to remove basic priviledges 3747 * to setattr (e.g. basic without chmod) then we will 3748 * need to add a check here before calling the server. 3749 */ 3750 error = nfs4setattr(vp, vap, flags, cr, NULL); 3751 3752 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0) 3753 vnevent_truncate(vp, ct); 3754 3755 return (error); 3756 } 3757 3758 /* 3759 * To replace the "guarded" version 3 setattr, we use two types of compound 3760 * setattr requests: 3761 * 1. The "normal" setattr, used when the size of the file isn't being 3762 * changed - { Putfh <fh>; Setattr; Getattr }/ 3763 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3764 * with only ctime as the argument. If the server ctime differs from 3765 * what is cached on the client, the verify will fail, but we would 3766 * already have the ctime from the preceding getattr, so just set it 3767 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3768 * Setattr; Getattr }. 3769 * 3770 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3771 * this setattr and NULL if they are not. 3772 */ 3773 static int 3774 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3775 vsecattr_t *vsap) 3776 { 3777 COMPOUND4args_clnt args; 3778 COMPOUND4res_clnt res, *resp = NULL; 3779 nfs4_ga_res_t *garp = NULL; 3780 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3781 nfs_argop4 argop[5]; 3782 int verify_argop = -1; 3783 int setattr_argop = 1; 3784 nfs_resop4 *resop; 3785 vattr_t va; 3786 rnode4_t *rp; 3787 int doqueue = 1; 3788 uint_t mask = vap->va_mask; 3789 mode_t omode; 3790 vsecattr_t *vsp; 3791 timestruc_t ctime; 3792 bool_t needrecov = FALSE; 3793 nfs4_recov_state_t recov_state; 3794 nfs4_stateid_types_t sid_types; 3795 stateid4 stateid; 3796 hrtime_t t; 3797 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3798 servinfo4_t *svp; 3799 bitmap4 supp_attrs; 3800 3801 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3802 rp = VTOR4(vp); 3803 nfs4_init_stateid_types(&sid_types); 3804 3805 /* 3806 * Only need to flush pages if there are any pages and 3807 * if the file is marked as dirty in some fashion. The 3808 * file must be flushed so that we can accurately 3809 * determine the size of the file and the cached data 3810 * after the SETATTR returns. A file is considered to 3811 * be dirty if it is either marked with R4DIRTY, has 3812 * outstanding i/o's active, or is mmap'd. In this 3813 * last case, we can't tell whether there are dirty 3814 * pages, so we flush just to be sure. 3815 */ 3816 if (nfs4_has_pages(vp) && 3817 ((rp->r_flags & R4DIRTY) || 3818 rp->r_count > 0 || 3819 rp->r_mapcnt > 0)) { 3820 ASSERT(vp->v_type != VCHR); 3821 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL); 3822 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3823 mutex_enter(&rp->r_statelock); 3824 if (!rp->r_error) 3825 rp->r_error = e.error; 3826 mutex_exit(&rp->r_statelock); 3827 } 3828 } 3829 3830 if (mask & AT_SIZE) { 3831 /* 3832 * Verification setattr compound for non-deleg AT_SIZE: 3833 * { Putfh; Getattr; Verify; Setattr; Getattr } 3834 * Set ctime local here (outside the do_again label) 3835 * so that subsequent retries (after failed VERIFY) 3836 * will use ctime from GETATTR results (from failed 3837 * verify compound) as VERIFY arg. 3838 * If file has delegation, then VERIFY(time_metadata) 3839 * is of little added value, so don't bother. 3840 */ 3841 mutex_enter(&rp->r_statev4_lock); 3842 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3843 rp->r_deleg_return_pending) { 3844 numops = 5; 3845 ctime = rp->r_attr.va_ctime; 3846 } 3847 mutex_exit(&rp->r_statev4_lock); 3848 } 3849 3850 recov_state.rs_flags = 0; 3851 recov_state.rs_num_retry_despite_err = 0; 3852 3853 args.ctag = TAG_SETATTR; 3854 do_again: 3855 recov_retry: 3856 setattr_argop = numops - 2; 3857 3858 args.array = argop; 3859 args.array_len = numops; 3860 3861 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3862 if (e.error) 3863 return (e.error); 3864 3865 3866 /* putfh target fh */ 3867 argop[0].argop = OP_CPUTFH; 3868 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3869 3870 if (numops == 5) { 3871 /* 3872 * We only care about the ctime, but need to get mtime 3873 * and size for proper cache update. 3874 */ 3875 /* getattr */ 3876 argop[1].argop = OP_GETATTR; 3877 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3878 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3879 3880 /* verify - set later in loop */ 3881 verify_argop = 2; 3882 } 3883 3884 /* setattr */ 3885 svp = rp->r_server; 3886 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3887 supp_attrs = svp->sv_supp_attrs; 3888 nfs_rw_exit(&svp->sv_lock); 3889 3890 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3891 supp_attrs, &e.error, &sid_types); 3892 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3893 if (e.error) { 3894 /* req time field(s) overflow - return immediately */ 3895 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3896 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3897 opsetattr.obj_attributes); 3898 return (e.error); 3899 } 3900 omode = rp->r_attr.va_mode; 3901 3902 /* getattr */ 3903 argop[numops-1].argop = OP_GETATTR; 3904 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3905 /* 3906 * If we are setting the ACL (indicated only by vsap != NULL), request 3907 * the ACL in this getattr. The ACL returned from this getattr will be 3908 * used in updating the ACL cache. 3909 */ 3910 if (vsap != NULL) 3911 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3912 FATTR4_ACL_MASK; 3913 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3914 3915 /* 3916 * setattr iterates if the object size is set and the cached ctime 3917 * does not match the file ctime. In that case, verify the ctime first. 3918 */ 3919 3920 do { 3921 if (verify_argop != -1) { 3922 /* 3923 * Verify that the ctime match before doing setattr. 3924 */ 3925 va.va_mask = AT_CTIME; 3926 va.va_ctime = ctime; 3927 svp = rp->r_server; 3928 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3929 supp_attrs = svp->sv_supp_attrs; 3930 nfs_rw_exit(&svp->sv_lock); 3931 e.error = nfs4args_verify(&argop[verify_argop], &va, 3932 OP_VERIFY, supp_attrs); 3933 if (e.error) { 3934 /* req time field(s) overflow - return */ 3935 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3936 needrecov); 3937 break; 3938 } 3939 } 3940 3941 doqueue = 1; 3942 3943 t = gethrtime(); 3944 3945 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3946 3947 /* 3948 * Purge the access cache and ACL cache if changing either the 3949 * owner of the file, the group owner, or the mode. These may 3950 * change the access permissions of the file, so purge old 3951 * information and start over again. 3952 */ 3953 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3954 (void) nfs4_access_purge_rp(rp); 3955 if (rp->r_secattr != NULL) { 3956 mutex_enter(&rp->r_statelock); 3957 vsp = rp->r_secattr; 3958 rp->r_secattr = NULL; 3959 mutex_exit(&rp->r_statelock); 3960 if (vsp != NULL) 3961 nfs4_acl_free_cache(vsp); 3962 } 3963 } 3964 3965 /* 3966 * If res.array_len == numops, then everything succeeded, 3967 * except for possibly the final getattr. If only the 3968 * last getattr failed, give up, and don't try recovery. 3969 */ 3970 if (res.array_len == numops) { 3971 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3972 needrecov); 3973 if (! e.error) 3974 resp = &res; 3975 break; 3976 } 3977 3978 /* 3979 * if either rpc call failed or completely succeeded - done 3980 */ 3981 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3982 if (e.error) { 3983 PURGE_ATTRCACHE4(vp); 3984 if (!needrecov) { 3985 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3986 needrecov); 3987 break; 3988 } 3989 } 3990 3991 /* 3992 * Do proper retry for OLD_STATEID outside of the normal 3993 * recovery framework. 3994 */ 3995 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3996 sid_types.cur_sid_type != SPEC_SID && 3997 sid_types.cur_sid_type != NO_SID) { 3998 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3999 needrecov); 4000 nfs4_save_stateid(&stateid, &sid_types); 4001 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4002 opsetattr.obj_attributes); 4003 if (verify_argop != -1) { 4004 nfs4args_verify_free(&argop[verify_argop]); 4005 verify_argop = -1; 4006 } 4007 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4008 goto recov_retry; 4009 } 4010 4011 if (needrecov) { 4012 bool_t abort; 4013 4014 abort = nfs4_start_recovery(&e, 4015 VTOMI4(vp), vp, NULL, NULL, NULL, 4016 OP_SETATTR, NULL, NULL, NULL); 4017 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4018 needrecov); 4019 /* 4020 * Do not retry if we failed with OLD_STATEID using 4021 * a special stateid. This is done to avoid looping 4022 * with a broken server. 4023 */ 4024 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 4025 (sid_types.cur_sid_type == SPEC_SID || 4026 sid_types.cur_sid_type == NO_SID)) 4027 abort = TRUE; 4028 if (!e.error) { 4029 if (res.status == NFS4ERR_BADOWNER) 4030 nfs4_log_badowner(VTOMI4(vp), 4031 OP_SETATTR); 4032 4033 e.error = geterrno4(res.status); 4034 (void) xdr_free(xdr_COMPOUND4res_clnt, 4035 (caddr_t)&res); 4036 } 4037 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4038 opsetattr.obj_attributes); 4039 if (verify_argop != -1) { 4040 nfs4args_verify_free(&argop[verify_argop]); 4041 verify_argop = -1; 4042 } 4043 if (abort == FALSE) { 4044 /* 4045 * Need to retry all possible stateids in 4046 * case the recovery error wasn't stateid 4047 * related or the stateids have become 4048 * stale (server reboot). 4049 */ 4050 nfs4_init_stateid_types(&sid_types); 4051 goto recov_retry; 4052 } 4053 return (e.error); 4054 } 4055 4056 /* 4057 * Need to call nfs4_end_op before nfs4getattr to 4058 * avoid potential nfs4_start_op deadlock. See RFE 4059 * 4777612. Calls to nfs4_invalidate_pages() and 4060 * nfs4_purge_stale_fh() might also generate over the 4061 * wire calls which my cause nfs4_start_op() deadlock. 4062 */ 4063 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4064 4065 /* 4066 * Check to update lease. 4067 */ 4068 resp = &res; 4069 if (res.status == NFS4_OK) { 4070 break; 4071 } 4072 4073 /* 4074 * Check if verify failed to see if try again 4075 */ 4076 if ((verify_argop == -1) || (res.array_len != 3)) { 4077 /* 4078 * can't continue... 4079 */ 4080 if (res.status == NFS4ERR_BADOWNER) 4081 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 4082 4083 e.error = geterrno4(res.status); 4084 } else { 4085 /* 4086 * When the verify request fails, the client ctime is 4087 * not in sync with the server. This is the same as 4088 * the version 3 "not synchronized" error, and we 4089 * handle it in a similar manner (XXX do we need to???). 4090 * Use the ctime returned in the first getattr for 4091 * the input to the next verify. 4092 * If we couldn't get the attributes, then we give up 4093 * because we can't complete the operation as required. 4094 */ 4095 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 4096 } 4097 if (e.error) { 4098 PURGE_ATTRCACHE4(vp); 4099 nfs4_purge_stale_fh(e.error, vp, cr); 4100 } else { 4101 /* 4102 * retry with a new verify value 4103 */ 4104 ctime = garp->n4g_va.va_ctime; 4105 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4106 resp = NULL; 4107 } 4108 if (!e.error) { 4109 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4110 opsetattr.obj_attributes); 4111 if (verify_argop != -1) { 4112 nfs4args_verify_free(&argop[verify_argop]); 4113 verify_argop = -1; 4114 } 4115 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4116 goto do_again; 4117 } 4118 } while (!e.error); 4119 4120 if (e.error) { 4121 /* 4122 * If we are here, rfs4call has an irrecoverable error - return 4123 */ 4124 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4125 opsetattr.obj_attributes); 4126 if (verify_argop != -1) { 4127 nfs4args_verify_free(&argop[verify_argop]); 4128 verify_argop = -1; 4129 } 4130 if (resp) 4131 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4132 return (e.error); 4133 } 4134 4135 4136 4137 /* 4138 * If changing the size of the file, invalidate 4139 * any local cached data which is no longer part 4140 * of the file. We also possibly invalidate the 4141 * last page in the file. We could use 4142 * pvn_vpzero(), but this would mark the page as 4143 * modified and require it to be written back to 4144 * the server for no particularly good reason. 4145 * This way, if we access it, then we bring it 4146 * back in. A read should be cheaper than a 4147 * write. 4148 */ 4149 if (mask & AT_SIZE) { 4150 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4151 } 4152 4153 /* either no error or one of the postop getattr failed */ 4154 4155 /* 4156 * XXX Perform a simplified version of wcc checking. Instead of 4157 * have another getattr to get pre-op, just purge cache if 4158 * any of the ops prior to and including the getattr failed. 4159 * If the getattr succeeded then update the attrcache accordingly. 4160 */ 4161 4162 garp = NULL; 4163 if (res.status == NFS4_OK) { 4164 /* 4165 * Last getattr 4166 */ 4167 resop = &res.array[numops - 1]; 4168 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4169 } 4170 /* 4171 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4172 * rather than filling it. See the function itself for details. 4173 */ 4174 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4175 if (garp != NULL) { 4176 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4177 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4178 vs_ace4_destroy(&garp->n4g_vsa); 4179 } else { 4180 if (vsap != NULL) { 4181 /* 4182 * The ACL was supposed to be set and to be 4183 * returned in the last getattr of this 4184 * compound, but for some reason the getattr 4185 * result doesn't contain the ACL. In this 4186 * case, purge the ACL cache. 4187 */ 4188 if (rp->r_secattr != NULL) { 4189 mutex_enter(&rp->r_statelock); 4190 vsp = rp->r_secattr; 4191 rp->r_secattr = NULL; 4192 mutex_exit(&rp->r_statelock); 4193 if (vsp != NULL) 4194 nfs4_acl_free_cache(vsp); 4195 } 4196 } 4197 } 4198 } 4199 4200 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4201 /* 4202 * Set the size, rather than relying on getting it updated 4203 * via a GETATTR. With delegations the client tries to 4204 * suppress GETATTR calls. 4205 */ 4206 mutex_enter(&rp->r_statelock); 4207 rp->r_size = vap->va_size; 4208 mutex_exit(&rp->r_statelock); 4209 } 4210 4211 /* 4212 * Can free up request args and res 4213 */ 4214 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4215 opsetattr.obj_attributes); 4216 if (verify_argop != -1) { 4217 nfs4args_verify_free(&argop[verify_argop]); 4218 verify_argop = -1; 4219 } 4220 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4221 4222 /* 4223 * Some servers will change the mode to clear the setuid 4224 * and setgid bits when changing the uid or gid. The 4225 * client needs to compensate appropriately. 4226 */ 4227 if (mask & (AT_UID | AT_GID)) { 4228 int terror, do_setattr; 4229 4230 do_setattr = 0; 4231 va.va_mask = AT_MODE; 4232 terror = nfs4getattr(vp, &va, cr); 4233 if (!terror && 4234 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4235 (!(mask & AT_MODE) && va.va_mode != omode))) { 4236 va.va_mask = AT_MODE; 4237 if (mask & AT_MODE) { 4238 /* 4239 * We asked the mode to be changed and what 4240 * we just got from the server in getattr is 4241 * not what we wanted it to be, so set it now. 4242 */ 4243 va.va_mode = vap->va_mode; 4244 do_setattr = 1; 4245 } else { 4246 /* 4247 * We did not ask the mode to be changed, 4248 * Check to see that the server just cleared 4249 * I_SUID and I_GUID from it. If not then 4250 * set mode to omode with UID/GID cleared. 4251 */ 4252 if (nfs4_compare_modes(va.va_mode, omode)) { 4253 omode &= ~(S_ISUID|S_ISGID); 4254 va.va_mode = omode; 4255 do_setattr = 1; 4256 } 4257 } 4258 4259 if (do_setattr) 4260 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4261 } 4262 } 4263 4264 return (e.error); 4265 } 4266 4267 /* ARGSUSED */ 4268 static int 4269 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct) 4270 { 4271 COMPOUND4args_clnt args; 4272 COMPOUND4res_clnt res; 4273 int doqueue; 4274 uint32_t acc, resacc, argacc; 4275 rnode4_t *rp; 4276 cred_t *cred, *ncr, *ncrfree = NULL; 4277 nfs4_access_type_t cacc; 4278 int num_ops; 4279 nfs_argop4 argop[3]; 4280 nfs_resop4 *resop; 4281 bool_t needrecov = FALSE, do_getattr; 4282 nfs4_recov_state_t recov_state; 4283 int rpc_error; 4284 hrtime_t t; 4285 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4286 mntinfo4_t *mi = VTOMI4(vp); 4287 4288 if (nfs_zone() != mi->mi_zone) 4289 return (EIO); 4290 4291 acc = 0; 4292 if (mode & VREAD) 4293 acc |= ACCESS4_READ; 4294 if (mode & VWRITE) { 4295 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4296 return (EROFS); 4297 if (vp->v_type == VDIR) 4298 acc |= ACCESS4_DELETE; 4299 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4300 } 4301 if (mode & VEXEC) { 4302 if (vp->v_type == VDIR) 4303 acc |= ACCESS4_LOOKUP; 4304 else 4305 acc |= ACCESS4_EXECUTE; 4306 } 4307 4308 if (VTOR4(vp)->r_acache != NULL) { 4309 e.error = nfs4_validate_caches(vp, cr); 4310 if (e.error) 4311 return (e.error); 4312 } 4313 4314 rp = VTOR4(vp); 4315 if (vp->v_type == VDIR) 4316 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4317 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4318 else 4319 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4320 ACCESS4_EXECUTE; 4321 recov_state.rs_flags = 0; 4322 recov_state.rs_num_retry_despite_err = 0; 4323 4324 cred = cr; 4325 /* 4326 * ncr and ncrfree both initially 4327 * point to the memory area returned 4328 * by crnetadjust(); 4329 * ncrfree not NULL when exiting means 4330 * that we need to release it 4331 */ 4332 ncr = crnetadjust(cred); 4333 ncrfree = ncr; 4334 4335 tryagain: 4336 cacc = nfs4_access_check(rp, acc, cred); 4337 if (cacc == NFS4_ACCESS_ALLOWED) { 4338 if (ncrfree != NULL) 4339 crfree(ncrfree); 4340 return (0); 4341 } 4342 if (cacc == NFS4_ACCESS_DENIED) { 4343 /* 4344 * If the cred can be adjusted, try again 4345 * with the new cred. 4346 */ 4347 if (ncr != NULL) { 4348 cred = ncr; 4349 ncr = NULL; 4350 goto tryagain; 4351 } 4352 if (ncrfree != NULL) 4353 crfree(ncrfree); 4354 return (EACCES); 4355 } 4356 4357 recov_retry: 4358 /* 4359 * Don't take with r_statev4_lock here. r_deleg_type could 4360 * change as soon as lock is released. Since it is an int, 4361 * there is no atomicity issue. 4362 */ 4363 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4364 num_ops = do_getattr ? 3 : 2; 4365 4366 args.ctag = TAG_ACCESS; 4367 4368 args.array_len = num_ops; 4369 args.array = argop; 4370 4371 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4372 &recov_state, NULL)) { 4373 if (ncrfree != NULL) 4374 crfree(ncrfree); 4375 return (e.error); 4376 } 4377 4378 /* putfh target fh */ 4379 argop[0].argop = OP_CPUTFH; 4380 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4381 4382 /* access */ 4383 argop[1].argop = OP_ACCESS; 4384 argop[1].nfs_argop4_u.opaccess.access = argacc; 4385 4386 /* getattr */ 4387 if (do_getattr) { 4388 argop[2].argop = OP_GETATTR; 4389 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4390 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4391 } 4392 4393 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4394 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4395 rnode4info(VTOR4(vp)))); 4396 4397 doqueue = 1; 4398 t = gethrtime(); 4399 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4400 rpc_error = e.error; 4401 4402 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4403 if (needrecov) { 4404 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4405 "nfs4_access: initiating recovery\n")); 4406 4407 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4408 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) { 4409 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4410 &recov_state, needrecov); 4411 if (!e.error) 4412 (void) xdr_free(xdr_COMPOUND4res_clnt, 4413 (caddr_t)&res); 4414 goto recov_retry; 4415 } 4416 } 4417 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4418 4419 if (e.error) 4420 goto out; 4421 4422 if (res.status) { 4423 e.error = geterrno4(res.status); 4424 /* 4425 * This might generate over the wire calls throught 4426 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4427 * here to avoid a deadlock. 4428 */ 4429 nfs4_purge_stale_fh(e.error, vp, cr); 4430 goto out; 4431 } 4432 resop = &res.array[1]; /* access res */ 4433 4434 resacc = resop->nfs_resop4_u.opaccess.access; 4435 4436 if (do_getattr) { 4437 resop++; /* getattr res */ 4438 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4439 t, cr, FALSE, NULL); 4440 } 4441 4442 if (!e.error) { 4443 nfs4_access_cache(rp, argacc, resacc, cred); 4444 /* 4445 * we just cached results with cred; if cred is the 4446 * adjusted credentials from crnetadjust, we do not want 4447 * to release them before exiting: hence setting ncrfree 4448 * to NULL 4449 */ 4450 if (cred != cr) 4451 ncrfree = NULL; 4452 /* XXX check the supported bits too? */ 4453 if ((acc & resacc) != acc) { 4454 /* 4455 * The following code implements the semantic 4456 * that a setuid root program has *at least* the 4457 * permissions of the user that is running the 4458 * program. See rfs3call() for more portions 4459 * of the implementation of this functionality. 4460 */ 4461 /* XXX-LP */ 4462 if (ncr != NULL) { 4463 (void) xdr_free(xdr_COMPOUND4res_clnt, 4464 (caddr_t)&res); 4465 cred = ncr; 4466 ncr = NULL; 4467 goto tryagain; 4468 } 4469 e.error = EACCES; 4470 } 4471 } 4472 4473 out: 4474 if (!rpc_error) 4475 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4476 4477 if (ncrfree != NULL) 4478 crfree(ncrfree); 4479 4480 return (e.error); 4481 } 4482 4483 /* ARGSUSED */ 4484 static int 4485 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct) 4486 { 4487 COMPOUND4args_clnt args; 4488 COMPOUND4res_clnt res; 4489 int doqueue; 4490 rnode4_t *rp; 4491 nfs_argop4 argop[3]; 4492 nfs_resop4 *resop; 4493 READLINK4res *lr_res; 4494 nfs4_ga_res_t *garp; 4495 uint_t len; 4496 char *linkdata; 4497 bool_t needrecov = FALSE; 4498 nfs4_recov_state_t recov_state; 4499 hrtime_t t; 4500 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4501 4502 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4503 return (EIO); 4504 /* 4505 * Can't readlink anything other than a symbolic link. 4506 */ 4507 if (vp->v_type != VLNK) 4508 return (EINVAL); 4509 4510 rp = VTOR4(vp); 4511 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4512 e.error = nfs4_validate_caches(vp, cr); 4513 if (e.error) 4514 return (e.error); 4515 mutex_enter(&rp->r_statelock); 4516 if (rp->r_symlink.contents != NULL) { 4517 e.error = uiomove(rp->r_symlink.contents, 4518 rp->r_symlink.len, UIO_READ, uiop); 4519 mutex_exit(&rp->r_statelock); 4520 return (e.error); 4521 } 4522 mutex_exit(&rp->r_statelock); 4523 } 4524 recov_state.rs_flags = 0; 4525 recov_state.rs_num_retry_despite_err = 0; 4526 4527 recov_retry: 4528 args.array_len = 3; 4529 args.array = argop; 4530 args.ctag = TAG_READLINK; 4531 4532 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4533 if (e.error) { 4534 return (e.error); 4535 } 4536 4537 /* 0. putfh symlink fh */ 4538 argop[0].argop = OP_CPUTFH; 4539 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4540 4541 /* 1. readlink */ 4542 argop[1].argop = OP_READLINK; 4543 4544 /* 2. getattr */ 4545 argop[2].argop = OP_GETATTR; 4546 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4547 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4548 4549 doqueue = 1; 4550 4551 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4552 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4553 rnode4info(VTOR4(vp)))); 4554 4555 t = gethrtime(); 4556 4557 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4558 4559 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4560 if (needrecov) { 4561 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4562 "nfs4_readlink: initiating recovery\n")); 4563 4564 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4565 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) { 4566 if (!e.error) 4567 (void) xdr_free(xdr_COMPOUND4res_clnt, 4568 (caddr_t)&res); 4569 4570 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4571 needrecov); 4572 goto recov_retry; 4573 } 4574 } 4575 4576 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4577 4578 if (e.error) 4579 return (e.error); 4580 4581 /* 4582 * There is an path in the code below which calls 4583 * nfs4_purge_stale_fh(), which may generate otw calls through 4584 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4585 * here to avoid nfs4_start_op() deadlock. 4586 */ 4587 4588 if (res.status && (res.array_len < args.array_len)) { 4589 /* 4590 * either Putfh or Link failed 4591 */ 4592 e.error = geterrno4(res.status); 4593 nfs4_purge_stale_fh(e.error, vp, cr); 4594 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4595 return (e.error); 4596 } 4597 4598 resop = &res.array[1]; /* readlink res */ 4599 lr_res = &resop->nfs_resop4_u.opreadlink; 4600 4601 /* 4602 * treat symlink names as data 4603 */ 4604 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL); 4605 if (linkdata != NULL) { 4606 int uio_len = len - 1; 4607 /* len includes null byte, which we won't uiomove */ 4608 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4609 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4610 mutex_enter(&rp->r_statelock); 4611 if (rp->r_symlink.contents == NULL) { 4612 rp->r_symlink.contents = linkdata; 4613 rp->r_symlink.len = uio_len; 4614 rp->r_symlink.size = len; 4615 mutex_exit(&rp->r_statelock); 4616 } else { 4617 mutex_exit(&rp->r_statelock); 4618 kmem_free(linkdata, len); 4619 } 4620 } else { 4621 kmem_free(linkdata, len); 4622 } 4623 } 4624 if (res.status == NFS4_OK) { 4625 resop++; /* getattr res */ 4626 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4627 } 4628 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4629 4630 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4631 4632 /* 4633 * The over the wire error for attempting to readlink something 4634 * other than a symbolic link is ENXIO. However, we need to 4635 * return EINVAL instead of ENXIO, so we map it here. 4636 */ 4637 return (e.error == ENXIO ? EINVAL : e.error); 4638 } 4639 4640 /* 4641 * Flush local dirty pages to stable storage on the server. 4642 * 4643 * If FNODSYNC is specified, then there is nothing to do because 4644 * metadata changes are not cached on the client before being 4645 * sent to the server. 4646 */ 4647 /* ARGSUSED */ 4648 static int 4649 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 4650 { 4651 int error; 4652 4653 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4654 return (0); 4655 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4656 return (EIO); 4657 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4658 if (!error) 4659 error = VTOR4(vp)->r_error; 4660 return (error); 4661 } 4662 4663 /* 4664 * Weirdness: if the file was removed or the target of a rename 4665 * operation while it was open, it got renamed instead. Here we 4666 * remove the renamed file. 4667 */ 4668 /* ARGSUSED */ 4669 void 4670 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4671 { 4672 rnode4_t *rp; 4673 4674 ASSERT(vp != DNLC_NO_VNODE); 4675 4676 rp = VTOR4(vp); 4677 4678 if (IS_SHADOW(vp, rp)) { 4679 sv_inactive(vp); 4680 return; 4681 } 4682 4683 /* 4684 * If this is coming from the wrong zone, we let someone in the right 4685 * zone take care of it asynchronously. We can get here due to 4686 * VN_RELE() being called from pageout() or fsflush(). This call may 4687 * potentially turn into an expensive no-op if, for instance, v_count 4688 * gets incremented in the meantime, but it's still correct. 4689 */ 4690 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4691 nfs4_async_inactive(vp, cr); 4692 return; 4693 } 4694 4695 /* 4696 * Some of the cleanup steps might require over-the-wire 4697 * operations. Since VOP_INACTIVE can get called as a result of 4698 * other over-the-wire operations (e.g., an attribute cache update 4699 * can lead to a DNLC purge), doing those steps now would lead to a 4700 * nested call to the recovery framework, which can deadlock. So 4701 * do any over-the-wire cleanups asynchronously, in a separate 4702 * thread. 4703 */ 4704 4705 mutex_enter(&rp->r_os_lock); 4706 mutex_enter(&rp->r_statelock); 4707 mutex_enter(&rp->r_statev4_lock); 4708 4709 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4710 mutex_exit(&rp->r_statev4_lock); 4711 mutex_exit(&rp->r_statelock); 4712 mutex_exit(&rp->r_os_lock); 4713 nfs4_async_inactive(vp, cr); 4714 return; 4715 } 4716 4717 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4718 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4719 mutex_exit(&rp->r_statev4_lock); 4720 mutex_exit(&rp->r_statelock); 4721 mutex_exit(&rp->r_os_lock); 4722 nfs4_async_inactive(vp, cr); 4723 return; 4724 } 4725 4726 if (rp->r_unldvp != NULL) { 4727 mutex_exit(&rp->r_statev4_lock); 4728 mutex_exit(&rp->r_statelock); 4729 mutex_exit(&rp->r_os_lock); 4730 nfs4_async_inactive(vp, cr); 4731 return; 4732 } 4733 mutex_exit(&rp->r_statev4_lock); 4734 mutex_exit(&rp->r_statelock); 4735 mutex_exit(&rp->r_os_lock); 4736 4737 rp4_addfree(rp, cr); 4738 } 4739 4740 /* 4741 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4742 * various bits of state. The caller must not refer to vp after this call. 4743 */ 4744 4745 void 4746 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4747 { 4748 rnode4_t *rp = VTOR4(vp); 4749 nfs4_recov_state_t recov_state; 4750 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4751 vnode_t *unldvp; 4752 char *unlname; 4753 cred_t *unlcred; 4754 COMPOUND4args_clnt args; 4755 COMPOUND4res_clnt res, *resp; 4756 nfs_argop4 argop[2]; 4757 int doqueue; 4758 #ifdef DEBUG 4759 char *name; 4760 #endif 4761 4762 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4763 ASSERT(!IS_SHADOW(vp, rp)); 4764 4765 #ifdef DEBUG 4766 name = fn_name(VTOSV(vp)->sv_name); 4767 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4768 "release vnode %s", name)); 4769 kmem_free(name, MAXNAMELEN); 4770 #endif 4771 4772 if (vp->v_type == VREG) { 4773 bool_t recov_failed = FALSE; 4774 4775 e.error = nfs4close_all(vp, cr); 4776 if (e.error) { 4777 /* Check to see if recovery failed */ 4778 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4779 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4780 recov_failed = TRUE; 4781 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4782 if (!recov_failed) { 4783 mutex_enter(&rp->r_statelock); 4784 if (rp->r_flags & R4RECOVERR) 4785 recov_failed = TRUE; 4786 mutex_exit(&rp->r_statelock); 4787 } 4788 if (recov_failed) { 4789 NFS4_DEBUG(nfs4_client_recov_debug, 4790 (CE_NOTE, "nfs4_inactive_otw: " 4791 "close failed (recovery failure)")); 4792 } 4793 } 4794 } 4795 4796 redo: 4797 if (rp->r_unldvp == NULL) { 4798 rp4_addfree(rp, cr); 4799 return; 4800 } 4801 4802 /* 4803 * Save the vnode pointer for the directory where the 4804 * unlinked-open file got renamed, then set it to NULL 4805 * to prevent another thread from getting here before 4806 * we're done with the remove. While we have the 4807 * statelock, make local copies of the pertinent rnode 4808 * fields. If we weren't to do this in an atomic way, the 4809 * the unl* fields could become inconsistent with respect 4810 * to each other due to a race condition between this 4811 * code and nfs_remove(). See bug report 1034328. 4812 */ 4813 mutex_enter(&rp->r_statelock); 4814 if (rp->r_unldvp == NULL) { 4815 mutex_exit(&rp->r_statelock); 4816 rp4_addfree(rp, cr); 4817 return; 4818 } 4819 4820 unldvp = rp->r_unldvp; 4821 rp->r_unldvp = NULL; 4822 unlname = rp->r_unlname; 4823 rp->r_unlname = NULL; 4824 unlcred = rp->r_unlcred; 4825 rp->r_unlcred = NULL; 4826 mutex_exit(&rp->r_statelock); 4827 4828 /* 4829 * If there are any dirty pages left, then flush 4830 * them. This is unfortunate because they just 4831 * may get thrown away during the remove operation, 4832 * but we have to do this for correctness. 4833 */ 4834 if (nfs4_has_pages(vp) && 4835 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4836 ASSERT(vp->v_type != VCHR); 4837 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL); 4838 if (e.error) { 4839 mutex_enter(&rp->r_statelock); 4840 if (!rp->r_error) 4841 rp->r_error = e.error; 4842 mutex_exit(&rp->r_statelock); 4843 } 4844 } 4845 4846 recov_state.rs_flags = 0; 4847 recov_state.rs_num_retry_despite_err = 0; 4848 recov_retry_remove: 4849 /* 4850 * Do the remove operation on the renamed file 4851 */ 4852 args.ctag = TAG_INACTIVE; 4853 4854 /* 4855 * Remove ops: putfh dir; remove 4856 */ 4857 args.array_len = 2; 4858 args.array = argop; 4859 4860 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4861 if (e.error) { 4862 kmem_free(unlname, MAXNAMELEN); 4863 crfree(unlcred); 4864 VN_RELE(unldvp); 4865 /* 4866 * Try again; this time around r_unldvp will be NULL, so we'll 4867 * just call rp4_addfree() and return. 4868 */ 4869 goto redo; 4870 } 4871 4872 /* putfh directory */ 4873 argop[0].argop = OP_CPUTFH; 4874 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4875 4876 /* remove */ 4877 argop[1].argop = OP_CREMOVE; 4878 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4879 4880 doqueue = 1; 4881 resp = &res; 4882 4883 #if 0 /* notyet */ 4884 /* 4885 * Can't do this yet. We may be being called from 4886 * dnlc_purge_XXX while that routine is holding a 4887 * mutex lock to the nc_rele list. The calls to 4888 * nfs3_cache_wcc_data may result in calls to 4889 * dnlc_purge_XXX. This will result in a deadlock. 4890 */ 4891 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4892 if (e.error) { 4893 PURGE_ATTRCACHE4(unldvp); 4894 resp = NULL; 4895 } else if (res.status) { 4896 e.error = geterrno4(res.status); 4897 PURGE_ATTRCACHE4(unldvp); 4898 /* 4899 * This code is inactive right now 4900 * but if made active there should 4901 * be a nfs4_end_op() call before 4902 * nfs4_purge_stale_fh to avoid start_op() 4903 * deadlock. See BugId: 4948726 4904 */ 4905 nfs4_purge_stale_fh(error, unldvp, cr); 4906 } else { 4907 nfs_resop4 *resop; 4908 REMOVE4res *rm_res; 4909 4910 resop = &res.array[1]; 4911 rm_res = &resop->nfs_resop4_u.opremove; 4912 /* 4913 * Update directory cache attribute, 4914 * readdir and dnlc caches. 4915 */ 4916 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4917 } 4918 #else 4919 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4920 4921 PURGE_ATTRCACHE4(unldvp); 4922 #endif 4923 4924 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4925 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4926 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 4927 if (!e.error) 4928 (void) xdr_free(xdr_COMPOUND4res_clnt, 4929 (caddr_t)&res); 4930 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4931 &recov_state, TRUE); 4932 goto recov_retry_remove; 4933 } 4934 } 4935 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4936 4937 /* 4938 * Release stuff held for the remove 4939 */ 4940 VN_RELE(unldvp); 4941 if (!e.error && resp) 4942 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4943 4944 kmem_free(unlname, MAXNAMELEN); 4945 crfree(unlcred); 4946 goto redo; 4947 } 4948 4949 /* 4950 * Remote file system operations having to do with directory manipulation. 4951 */ 4952 /* ARGSUSED3 */ 4953 int 4954 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4955 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 4956 int *direntflags, pathname_t *realpnp) 4957 { 4958 int error; 4959 vnode_t *vp, *avp = NULL; 4960 rnode4_t *drp; 4961 4962 *vpp = NULL; 4963 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4964 return (EPERM); 4965 /* 4966 * if LOOKUP_XATTR, must replace dvp (object) with 4967 * object's attrdir before continuing with lookup 4968 */ 4969 if (flags & LOOKUP_XATTR) { 4970 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4971 if (error) 4972 return (error); 4973 4974 dvp = avp; 4975 4976 /* 4977 * If lookup is for "", just return dvp now. The attrdir 4978 * has already been activated (from nfs4lookup_xattr), and 4979 * the caller will RELE the original dvp -- not 4980 * the attrdir. So, set vpp and return. 4981 * Currently, when the LOOKUP_XATTR flag is 4982 * passed to VOP_LOOKUP, the name is always empty, and 4983 * shortcircuiting here avoids 3 unneeded lock/unlock 4984 * pairs. 4985 * 4986 * If a non-empty name was provided, then it is the 4987 * attribute name, and it will be looked up below. 4988 */ 4989 if (*nm == '\0') { 4990 *vpp = dvp; 4991 return (0); 4992 } 4993 4994 /* 4995 * The vfs layer never sends a name when asking for the 4996 * attrdir, so we should never get here (unless of course 4997 * name is passed at some time in future -- at which time 4998 * we'll blow up here). 4999 */ 5000 ASSERT(0); 5001 } 5002 5003 drp = VTOR4(dvp); 5004 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5005 return (EINTR); 5006 5007 error = nfs4lookup(dvp, nm, vpp, cr, 0); 5008 nfs_rw_exit(&drp->r_rwlock); 5009 5010 /* 5011 * If vnode is a device, create special vnode. 5012 */ 5013 if (!error && ISVDEV((*vpp)->v_type)) { 5014 vp = *vpp; 5015 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 5016 VN_RELE(vp); 5017 } 5018 5019 return (error); 5020 } 5021 5022 /* ARGSUSED */ 5023 static int 5024 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 5025 { 5026 int error; 5027 rnode4_t *drp; 5028 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 5029 mntinfo4_t *mi; 5030 5031 mi = VTOMI4(dvp); 5032 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) && 5033 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS)) 5034 return (EINVAL); 5035 5036 drp = VTOR4(dvp); 5037 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5038 return (EINTR); 5039 5040 mutex_enter(&drp->r_statelock); 5041 /* 5042 * If the server doesn't support xattrs just return EINVAL 5043 */ 5044 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 5045 mutex_exit(&drp->r_statelock); 5046 nfs_rw_exit(&drp->r_rwlock); 5047 return (EINVAL); 5048 } 5049 5050 /* 5051 * If there is a cached xattr directory entry, 5052 * use it as long as the attributes are valid. If the 5053 * attributes are not valid, take the simple approach and 5054 * free the cached value and re-fetch a new value. 5055 * 5056 * We don't negative entry cache for now, if we did we 5057 * would need to check if the file has changed on every 5058 * lookup. But xattrs don't exist very often and failing 5059 * an openattr is not much more expensive than and NVERIFY or GETATTR 5060 * so do an openattr over the wire for now. 5061 */ 5062 if (drp->r_xattr_dir != NULL) { 5063 if (ATTRCACHE4_VALID(dvp)) { 5064 VN_HOLD(drp->r_xattr_dir); 5065 *vpp = drp->r_xattr_dir; 5066 mutex_exit(&drp->r_statelock); 5067 nfs_rw_exit(&drp->r_rwlock); 5068 return (0); 5069 } 5070 VN_RELE(drp->r_xattr_dir); 5071 drp->r_xattr_dir = NULL; 5072 } 5073 mutex_exit(&drp->r_statelock); 5074 5075 error = nfs4openattr(dvp, vpp, cflag, cr); 5076 5077 nfs_rw_exit(&drp->r_rwlock); 5078 5079 return (error); 5080 } 5081 5082 static int 5083 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 5084 { 5085 int error; 5086 rnode4_t *drp; 5087 5088 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5089 5090 /* 5091 * If lookup is for "", just return dvp. Don't need 5092 * to send it over the wire, look it up in the dnlc, 5093 * or perform any access checks. 5094 */ 5095 if (*nm == '\0') { 5096 VN_HOLD(dvp); 5097 *vpp = dvp; 5098 return (0); 5099 } 5100 5101 /* 5102 * Can't do lookups in non-directories. 5103 */ 5104 if (dvp->v_type != VDIR) 5105 return (ENOTDIR); 5106 5107 /* 5108 * If lookup is for ".", just return dvp. Don't need 5109 * to send it over the wire or look it up in the dnlc, 5110 * just need to check access. 5111 */ 5112 if (nm[0] == '.' && nm[1] == '\0') { 5113 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5114 if (error) 5115 return (error); 5116 VN_HOLD(dvp); 5117 *vpp = dvp; 5118 return (0); 5119 } 5120 5121 drp = VTOR4(dvp); 5122 if (!(drp->r_flags & R4LOOKUP)) { 5123 mutex_enter(&drp->r_statelock); 5124 drp->r_flags |= R4LOOKUP; 5125 mutex_exit(&drp->r_statelock); 5126 } 5127 5128 *vpp = NULL; 5129 /* 5130 * Lookup this name in the DNLC. If there is no entry 5131 * lookup over the wire. 5132 */ 5133 if (!skipdnlc) 5134 *vpp = dnlc_lookup(dvp, nm); 5135 if (*vpp == NULL) { 5136 /* 5137 * We need to go over the wire to lookup the name. 5138 */ 5139 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5140 } 5141 5142 /* 5143 * We hit on the dnlc 5144 */ 5145 if (*vpp != DNLC_NO_VNODE || 5146 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5147 /* 5148 * But our attrs may not be valid. 5149 */ 5150 if (ATTRCACHE4_VALID(dvp)) { 5151 error = nfs4_waitfor_purge_complete(dvp); 5152 if (error) { 5153 VN_RELE(*vpp); 5154 *vpp = NULL; 5155 return (error); 5156 } 5157 5158 /* 5159 * If after the purge completes, check to make sure 5160 * our attrs are still valid. 5161 */ 5162 if (ATTRCACHE4_VALID(dvp)) { 5163 /* 5164 * If we waited for a purge we may have 5165 * lost our vnode so look it up again. 5166 */ 5167 VN_RELE(*vpp); 5168 *vpp = dnlc_lookup(dvp, nm); 5169 if (*vpp == NULL) 5170 return (nfs4lookupnew_otw(dvp, 5171 nm, vpp, cr)); 5172 5173 /* 5174 * The access cache should almost always hit 5175 */ 5176 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5177 5178 if (error) { 5179 VN_RELE(*vpp); 5180 *vpp = NULL; 5181 return (error); 5182 } 5183 if (*vpp == DNLC_NO_VNODE) { 5184 VN_RELE(*vpp); 5185 *vpp = NULL; 5186 return (ENOENT); 5187 } 5188 return (0); 5189 } 5190 } 5191 } 5192 5193 ASSERT(*vpp != NULL); 5194 5195 /* 5196 * We may have gotten here we have one of the following cases: 5197 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5198 * need to validate them. 5199 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5200 * must validate. 5201 * 5202 * Go to the server and check if the directory has changed, if 5203 * it hasn't we are done and can use the dnlc entry. 5204 */ 5205 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5206 } 5207 5208 /* 5209 * Go to the server and check if the directory has changed, if 5210 * it hasn't we are done and can use the dnlc entry. If it 5211 * has changed we get a new copy of its attributes and check 5212 * the access for VEXEC, then relookup the filename and 5213 * get its filehandle and attributes. 5214 * 5215 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5216 * if the NVERIFY failed we must 5217 * purge the caches 5218 * cache new attributes (will set r_time_attr_inval) 5219 * cache new access 5220 * recheck VEXEC access 5221 * add name to dnlc, possibly negative 5222 * if LOOKUP succeeded 5223 * cache new attributes 5224 * else 5225 * set a new r_time_attr_inval for dvp 5226 * check to make sure we have access 5227 * 5228 * The vpp returned is the vnode passed in if the directory is valid, 5229 * a new vnode if successful lookup, or NULL on error. 5230 */ 5231 static int 5232 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5233 { 5234 COMPOUND4args_clnt args; 5235 COMPOUND4res_clnt res; 5236 fattr4 *ver_fattr; 5237 fattr4_change dchange; 5238 int32_t *ptr; 5239 int argoplist_size = 7 * sizeof (nfs_argop4); 5240 nfs_argop4 *argop; 5241 int doqueue; 5242 mntinfo4_t *mi; 5243 nfs4_recov_state_t recov_state; 5244 hrtime_t t; 5245 int isdotdot; 5246 vnode_t *nvp; 5247 nfs_fh4 *fhp; 5248 nfs4_sharedfh_t *sfhp; 5249 nfs4_access_type_t cacc; 5250 rnode4_t *nrp; 5251 rnode4_t *drp = VTOR4(dvp); 5252 nfs4_ga_res_t *garp = NULL; 5253 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5254 5255 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5256 ASSERT(nm != NULL); 5257 ASSERT(nm[0] != '\0'); 5258 ASSERT(dvp->v_type == VDIR); 5259 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5260 ASSERT(*vpp != NULL); 5261 5262 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5263 isdotdot = 1; 5264 args.ctag = TAG_LOOKUP_VPARENT; 5265 } else { 5266 /* 5267 * If dvp were a stub, it should have triggered and caused 5268 * a mount for us to get this far. 5269 */ 5270 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5271 5272 isdotdot = 0; 5273 args.ctag = TAG_LOOKUP_VALID; 5274 } 5275 5276 mi = VTOMI4(dvp); 5277 recov_state.rs_flags = 0; 5278 recov_state.rs_num_retry_despite_err = 0; 5279 5280 nvp = NULL; 5281 5282 /* Save the original mount point security information */ 5283 (void) save_mnt_secinfo(mi->mi_curr_serv); 5284 5285 recov_retry: 5286 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5287 &recov_state, NULL); 5288 if (e.error) { 5289 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5290 VN_RELE(*vpp); 5291 *vpp = NULL; 5292 return (e.error); 5293 } 5294 5295 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5296 5297 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5298 args.array_len = 7; 5299 args.array = argop; 5300 5301 /* 0. putfh file */ 5302 argop[0].argop = OP_CPUTFH; 5303 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5304 5305 /* 1. nverify the change info */ 5306 argop[1].argop = OP_NVERIFY; 5307 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5308 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5309 ver_fattr->attrlist4 = (char *)&dchange; 5310 ptr = (int32_t *)&dchange; 5311 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5312 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5313 5314 /* 2. getattr directory */ 5315 argop[2].argop = OP_GETATTR; 5316 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5317 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5318 5319 /* 3. access directory */ 5320 argop[3].argop = OP_ACCESS; 5321 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5322 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5323 5324 /* 4. lookup name */ 5325 if (isdotdot) { 5326 argop[4].argop = OP_LOOKUPP; 5327 } else { 5328 argop[4].argop = OP_CLOOKUP; 5329 argop[4].nfs_argop4_u.opclookup.cname = nm; 5330 } 5331 5332 /* 5. resulting file handle */ 5333 argop[5].argop = OP_GETFH; 5334 5335 /* 6. resulting file attributes */ 5336 argop[6].argop = OP_GETATTR; 5337 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5338 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5339 5340 doqueue = 1; 5341 t = gethrtime(); 5342 5343 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5344 5345 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5346 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5347 if (e.error != 0 && *vpp != NULL) 5348 VN_RELE(*vpp); 5349 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5350 &recov_state, FALSE); 5351 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5352 kmem_free(argop, argoplist_size); 5353 return (e.error); 5354 } 5355 5356 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5357 /* 5358 * For WRONGSEC of a non-dotdot case, send secinfo directly 5359 * from this thread, do not go thru the recovery thread since 5360 * we need the nm information. 5361 * 5362 * Not doing dotdot case because there is no specification 5363 * for (PUTFH, SECINFO "..") yet. 5364 */ 5365 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5366 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5367 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5368 &recov_state, FALSE); 5369 else 5370 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5371 &recov_state, TRUE); 5372 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5373 kmem_free(argop, argoplist_size); 5374 if (!e.error) 5375 goto recov_retry; 5376 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5377 VN_RELE(*vpp); 5378 *vpp = NULL; 5379 return (e.error); 5380 } 5381 5382 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5383 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5384 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5385 &recov_state, TRUE); 5386 5387 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5388 kmem_free(argop, argoplist_size); 5389 goto recov_retry; 5390 } 5391 } 5392 5393 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5394 5395 if (e.error || res.array_len == 0) { 5396 /* 5397 * If e.error isn't set, then reply has no ops (or we couldn't 5398 * be here). The only legal way to reply without an op array 5399 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5400 * be in the reply for all other status values. 5401 * 5402 * For valid replies without an ops array, return ENOTSUP 5403 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5404 * return EIO -- don't trust status. 5405 */ 5406 if (e.error == 0) 5407 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5408 ENOTSUP : EIO; 5409 VN_RELE(*vpp); 5410 *vpp = NULL; 5411 kmem_free(argop, argoplist_size); 5412 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5413 return (e.error); 5414 } 5415 5416 if (res.status != NFS4ERR_SAME) { 5417 e.error = geterrno4(res.status); 5418 5419 /* 5420 * The NVERIFY "failed" so the directory has changed 5421 * First make sure PUTFH succeeded and NVERIFY "failed" 5422 * cleanly. 5423 */ 5424 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5425 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5426 nfs4_purge_stale_fh(e.error, dvp, cr); 5427 VN_RELE(*vpp); 5428 *vpp = NULL; 5429 goto exit; 5430 } 5431 5432 /* 5433 * We know the NVERIFY "failed" so we must: 5434 * purge the caches (access and indirectly dnlc if needed) 5435 */ 5436 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5437 5438 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5439 nfs4_purge_stale_fh(e.error, dvp, cr); 5440 VN_RELE(*vpp); 5441 *vpp = NULL; 5442 goto exit; 5443 } 5444 5445 /* 5446 * Install new cached attributes for the directory 5447 */ 5448 nfs4_attr_cache(dvp, 5449 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5450 t, cr, FALSE, NULL); 5451 5452 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5453 nfs4_purge_stale_fh(e.error, dvp, cr); 5454 VN_RELE(*vpp); 5455 *vpp = NULL; 5456 e.error = geterrno4(res.status); 5457 goto exit; 5458 } 5459 5460 /* 5461 * Now we know the directory is valid, 5462 * cache new directory access 5463 */ 5464 nfs4_access_cache(drp, 5465 args.array[3].nfs_argop4_u.opaccess.access, 5466 res.array[3].nfs_resop4_u.opaccess.access, cr); 5467 5468 /* 5469 * recheck VEXEC access 5470 */ 5471 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5472 if (cacc != NFS4_ACCESS_ALLOWED) { 5473 /* 5474 * Directory permissions might have been revoked 5475 */ 5476 if (cacc == NFS4_ACCESS_DENIED) { 5477 e.error = EACCES; 5478 VN_RELE(*vpp); 5479 *vpp = NULL; 5480 goto exit; 5481 } 5482 5483 /* 5484 * Somehow we must not have asked for enough 5485 * so try a singleton ACCESS, should never happen. 5486 */ 5487 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5488 if (e.error) { 5489 VN_RELE(*vpp); 5490 *vpp = NULL; 5491 goto exit; 5492 } 5493 } 5494 5495 e.error = geterrno4(res.status); 5496 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5497 /* 5498 * The lookup failed, probably no entry 5499 */ 5500 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5501 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5502 } else { 5503 /* 5504 * Might be some other error, so remove 5505 * the dnlc entry to make sure we start all 5506 * over again, next time. 5507 */ 5508 dnlc_remove(dvp, nm); 5509 } 5510 VN_RELE(*vpp); 5511 *vpp = NULL; 5512 goto exit; 5513 } 5514 5515 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5516 /* 5517 * The file exists but we can't get its fh for 5518 * some unknown reason. Remove it from the dnlc 5519 * and error out to be safe. 5520 */ 5521 dnlc_remove(dvp, nm); 5522 VN_RELE(*vpp); 5523 *vpp = NULL; 5524 goto exit; 5525 } 5526 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5527 if (fhp->nfs_fh4_len == 0) { 5528 /* 5529 * The file exists but a bogus fh 5530 * some unknown reason. Remove it from the dnlc 5531 * and error out to be safe. 5532 */ 5533 e.error = ENOENT; 5534 dnlc_remove(dvp, nm); 5535 VN_RELE(*vpp); 5536 *vpp = NULL; 5537 goto exit; 5538 } 5539 sfhp = sfh4_get(fhp, mi); 5540 5541 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5542 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5543 5544 /* 5545 * Make the new rnode 5546 */ 5547 if (isdotdot) { 5548 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5549 if (e.error) { 5550 sfh4_rele(&sfhp); 5551 VN_RELE(*vpp); 5552 *vpp = NULL; 5553 goto exit; 5554 } 5555 /* 5556 * XXX if nfs4_make_dotdot uses an existing rnode 5557 * XXX it doesn't update the attributes. 5558 * XXX for now just save them again to save an OTW 5559 */ 5560 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5561 } else { 5562 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5563 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 5564 /* 5565 * If v_type == VNON, then garp was NULL because 5566 * the last op in the compound failed and makenfs4node 5567 * could not find the vnode for sfhp. It created 5568 * a new vnode, so we have nothing to purge here. 5569 */ 5570 if (nvp->v_type == VNON) { 5571 vattr_t vattr; 5572 5573 vattr.va_mask = AT_TYPE; 5574 /* 5575 * N.B. We've already called nfs4_end_fop above. 5576 */ 5577 e.error = nfs4getattr(nvp, &vattr, cr); 5578 if (e.error) { 5579 sfh4_rele(&sfhp); 5580 VN_RELE(*vpp); 5581 *vpp = NULL; 5582 VN_RELE(nvp); 5583 goto exit; 5584 } 5585 nvp->v_type = vattr.va_type; 5586 } 5587 } 5588 sfh4_rele(&sfhp); 5589 5590 nrp = VTOR4(nvp); 5591 mutex_enter(&nrp->r_statev4_lock); 5592 if (!nrp->created_v4) { 5593 mutex_exit(&nrp->r_statev4_lock); 5594 dnlc_update(dvp, nm, nvp); 5595 } else 5596 mutex_exit(&nrp->r_statev4_lock); 5597 5598 VN_RELE(*vpp); 5599 *vpp = nvp; 5600 } else { 5601 hrtime_t now; 5602 hrtime_t delta = 0; 5603 5604 e.error = 0; 5605 5606 /* 5607 * Because the NVERIFY "succeeded" we know that the 5608 * directory attributes are still valid 5609 * so update r_time_attr_inval 5610 */ 5611 now = gethrtime(); 5612 mutex_enter(&drp->r_statelock); 5613 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5614 delta = now - drp->r_time_attr_saved; 5615 if (delta < mi->mi_acdirmin) 5616 delta = mi->mi_acdirmin; 5617 else if (delta > mi->mi_acdirmax) 5618 delta = mi->mi_acdirmax; 5619 } 5620 drp->r_time_attr_inval = now + delta; 5621 mutex_exit(&drp->r_statelock); 5622 dnlc_update(dvp, nm, *vpp); 5623 5624 /* 5625 * Even though we have a valid directory attr cache 5626 * and dnlc entry, we may not have access. 5627 * This should almost always hit the cache. 5628 */ 5629 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5630 if (e.error) { 5631 VN_RELE(*vpp); 5632 *vpp = NULL; 5633 } 5634 5635 if (*vpp == DNLC_NO_VNODE) { 5636 VN_RELE(*vpp); 5637 *vpp = NULL; 5638 e.error = ENOENT; 5639 } 5640 } 5641 5642 exit: 5643 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5644 kmem_free(argop, argoplist_size); 5645 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5646 return (e.error); 5647 } 5648 5649 /* 5650 * We need to go over the wire to lookup the name, but 5651 * while we are there verify the directory has not 5652 * changed but if it has, get new attributes and check access 5653 * 5654 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5655 * NVERIFY GETATTR ACCESS 5656 * 5657 * With the results: 5658 * if the NVERIFY failed we must purge the caches, add new attributes, 5659 * and cache new access. 5660 * set a new r_time_attr_inval 5661 * add name to dnlc, possibly negative 5662 * if LOOKUP succeeded 5663 * cache new attributes 5664 */ 5665 static int 5666 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5667 { 5668 COMPOUND4args_clnt args; 5669 COMPOUND4res_clnt res; 5670 fattr4 *ver_fattr; 5671 fattr4_change dchange; 5672 int32_t *ptr; 5673 nfs4_ga_res_t *garp = NULL; 5674 int argoplist_size = 9 * sizeof (nfs_argop4); 5675 nfs_argop4 *argop; 5676 int doqueue; 5677 mntinfo4_t *mi; 5678 nfs4_recov_state_t recov_state; 5679 hrtime_t t; 5680 int isdotdot; 5681 vnode_t *nvp; 5682 nfs_fh4 *fhp; 5683 nfs4_sharedfh_t *sfhp; 5684 nfs4_access_type_t cacc; 5685 rnode4_t *nrp; 5686 rnode4_t *drp = VTOR4(dvp); 5687 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5688 5689 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5690 ASSERT(nm != NULL); 5691 ASSERT(nm[0] != '\0'); 5692 ASSERT(dvp->v_type == VDIR); 5693 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5694 ASSERT(*vpp == NULL); 5695 5696 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5697 isdotdot = 1; 5698 args.ctag = TAG_LOOKUP_PARENT; 5699 } else { 5700 /* 5701 * If dvp were a stub, it should have triggered and caused 5702 * a mount for us to get this far. 5703 */ 5704 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5705 5706 isdotdot = 0; 5707 args.ctag = TAG_LOOKUP; 5708 } 5709 5710 mi = VTOMI4(dvp); 5711 recov_state.rs_flags = 0; 5712 recov_state.rs_num_retry_despite_err = 0; 5713 5714 nvp = NULL; 5715 5716 /* Save the original mount point security information */ 5717 (void) save_mnt_secinfo(mi->mi_curr_serv); 5718 5719 recov_retry: 5720 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5721 &recov_state, NULL); 5722 if (e.error) { 5723 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5724 return (e.error); 5725 } 5726 5727 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5728 5729 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5730 args.array_len = 9; 5731 args.array = argop; 5732 5733 /* 0. putfh file */ 5734 argop[0].argop = OP_CPUTFH; 5735 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5736 5737 /* 1. savefh for the nverify */ 5738 argop[1].argop = OP_SAVEFH; 5739 5740 /* 2. lookup name */ 5741 if (isdotdot) { 5742 argop[2].argop = OP_LOOKUPP; 5743 } else { 5744 argop[2].argop = OP_CLOOKUP; 5745 argop[2].nfs_argop4_u.opclookup.cname = nm; 5746 } 5747 5748 /* 3. resulting file handle */ 5749 argop[3].argop = OP_GETFH; 5750 5751 /* 4. resulting file attributes */ 5752 argop[4].argop = OP_GETATTR; 5753 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5754 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5755 5756 /* 5. restorefh back the directory for the nverify */ 5757 argop[5].argop = OP_RESTOREFH; 5758 5759 /* 6. nverify the change info */ 5760 argop[6].argop = OP_NVERIFY; 5761 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5762 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5763 ver_fattr->attrlist4 = (char *)&dchange; 5764 ptr = (int32_t *)&dchange; 5765 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5766 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5767 5768 /* 7. getattr directory */ 5769 argop[7].argop = OP_GETATTR; 5770 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5771 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5772 5773 /* 8. access directory */ 5774 argop[8].argop = OP_ACCESS; 5775 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5776 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5777 5778 doqueue = 1; 5779 t = gethrtime(); 5780 5781 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5782 5783 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5784 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5785 if (e.error != 0 && *vpp != NULL) 5786 VN_RELE(*vpp); 5787 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5788 &recov_state, FALSE); 5789 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5790 kmem_free(argop, argoplist_size); 5791 return (e.error); 5792 } 5793 5794 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5795 /* 5796 * For WRONGSEC of a non-dotdot case, send secinfo directly 5797 * from this thread, do not go thru the recovery thread since 5798 * we need the nm information. 5799 * 5800 * Not doing dotdot case because there is no specification 5801 * for (PUTFH, SECINFO "..") yet. 5802 */ 5803 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5804 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5805 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5806 &recov_state, FALSE); 5807 else 5808 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5809 &recov_state, TRUE); 5810 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5811 kmem_free(argop, argoplist_size); 5812 if (!e.error) 5813 goto recov_retry; 5814 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5815 return (e.error); 5816 } 5817 5818 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5819 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5820 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5821 &recov_state, TRUE); 5822 5823 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5824 kmem_free(argop, argoplist_size); 5825 goto recov_retry; 5826 } 5827 } 5828 5829 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5830 5831 if (e.error || res.array_len == 0) { 5832 /* 5833 * If e.error isn't set, then reply has no ops (or we couldn't 5834 * be here). The only legal way to reply without an op array 5835 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5836 * be in the reply for all other status values. 5837 * 5838 * For valid replies without an ops array, return ENOTSUP 5839 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5840 * return EIO -- don't trust status. 5841 */ 5842 if (e.error == 0) 5843 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5844 ENOTSUP : EIO; 5845 5846 kmem_free(argop, argoplist_size); 5847 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5848 return (e.error); 5849 } 5850 5851 e.error = geterrno4(res.status); 5852 5853 /* 5854 * The PUTFH and SAVEFH may have failed. 5855 */ 5856 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5857 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5858 nfs4_purge_stale_fh(e.error, dvp, cr); 5859 goto exit; 5860 } 5861 5862 /* 5863 * Check if the file exists, if it does delay entering 5864 * into the dnlc until after we update the directory 5865 * attributes so we don't cause it to get purged immediately. 5866 */ 5867 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5868 /* 5869 * The lookup failed, probably no entry 5870 */ 5871 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5872 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5873 goto exit; 5874 } 5875 5876 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5877 /* 5878 * The file exists but we can't get its fh for 5879 * some unknown reason. Error out to be safe. 5880 */ 5881 goto exit; 5882 } 5883 5884 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5885 if (fhp->nfs_fh4_len == 0) { 5886 /* 5887 * The file exists but a bogus fh 5888 * some unknown reason. Error out to be safe. 5889 */ 5890 e.error = EIO; 5891 goto exit; 5892 } 5893 sfhp = sfh4_get(fhp, mi); 5894 5895 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5896 sfh4_rele(&sfhp); 5897 goto exit; 5898 } 5899 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5900 5901 /* 5902 * The RESTOREFH may have failed 5903 */ 5904 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5905 sfh4_rele(&sfhp); 5906 e.error = EIO; 5907 goto exit; 5908 } 5909 5910 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5911 /* 5912 * First make sure the NVERIFY failed as we expected, 5913 * if it didn't then be conservative and error out 5914 * as we can't trust the directory. 5915 */ 5916 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5917 sfh4_rele(&sfhp); 5918 e.error = EIO; 5919 goto exit; 5920 } 5921 5922 /* 5923 * We know the NVERIFY "failed" so the directory has changed, 5924 * so we must: 5925 * purge the caches (access and indirectly dnlc if needed) 5926 */ 5927 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5928 5929 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5930 sfh4_rele(&sfhp); 5931 goto exit; 5932 } 5933 nfs4_attr_cache(dvp, 5934 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5935 t, cr, FALSE, NULL); 5936 5937 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5938 nfs4_purge_stale_fh(e.error, dvp, cr); 5939 sfh4_rele(&sfhp); 5940 e.error = geterrno4(res.status); 5941 goto exit; 5942 } 5943 5944 /* 5945 * Now we know the directory is valid, 5946 * cache new directory access 5947 */ 5948 nfs4_access_cache(drp, 5949 args.array[8].nfs_argop4_u.opaccess.access, 5950 res.array[8].nfs_resop4_u.opaccess.access, cr); 5951 5952 /* 5953 * recheck VEXEC access 5954 */ 5955 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5956 if (cacc != NFS4_ACCESS_ALLOWED) { 5957 /* 5958 * Directory permissions might have been revoked 5959 */ 5960 if (cacc == NFS4_ACCESS_DENIED) { 5961 sfh4_rele(&sfhp); 5962 e.error = EACCES; 5963 goto exit; 5964 } 5965 5966 /* 5967 * Somehow we must not have asked for enough 5968 * so try a singleton ACCESS should never happen 5969 */ 5970 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5971 if (e.error) { 5972 sfh4_rele(&sfhp); 5973 goto exit; 5974 } 5975 } 5976 5977 e.error = geterrno4(res.status); 5978 } else { 5979 hrtime_t now; 5980 hrtime_t delta = 0; 5981 5982 e.error = 0; 5983 5984 /* 5985 * Because the NVERIFY "succeeded" we know that the 5986 * directory attributes are still valid 5987 * so update r_time_attr_inval 5988 */ 5989 now = gethrtime(); 5990 mutex_enter(&drp->r_statelock); 5991 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5992 delta = now - drp->r_time_attr_saved; 5993 if (delta < mi->mi_acdirmin) 5994 delta = mi->mi_acdirmin; 5995 else if (delta > mi->mi_acdirmax) 5996 delta = mi->mi_acdirmax; 5997 } 5998 drp->r_time_attr_inval = now + delta; 5999 mutex_exit(&drp->r_statelock); 6000 6001 /* 6002 * Even though we have a valid directory attr cache, 6003 * we may not have access. 6004 * This should almost always hit the cache. 6005 */ 6006 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 6007 if (e.error) { 6008 sfh4_rele(&sfhp); 6009 goto exit; 6010 } 6011 } 6012 6013 /* 6014 * Now we have successfully completed the lookup, if the 6015 * directory has changed we now have the valid attributes. 6016 * We also know we have directory access. 6017 * Create the new rnode and insert it in the dnlc. 6018 */ 6019 if (isdotdot) { 6020 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 6021 if (e.error) { 6022 sfh4_rele(&sfhp); 6023 goto exit; 6024 } 6025 /* 6026 * XXX if nfs4_make_dotdot uses an existing rnode 6027 * XXX it doesn't update the attributes. 6028 * XXX for now just save them again to save an OTW 6029 */ 6030 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 6031 } else { 6032 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 6033 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 6034 } 6035 sfh4_rele(&sfhp); 6036 6037 nrp = VTOR4(nvp); 6038 mutex_enter(&nrp->r_statev4_lock); 6039 if (!nrp->created_v4) { 6040 mutex_exit(&nrp->r_statev4_lock); 6041 dnlc_update(dvp, nm, nvp); 6042 } else 6043 mutex_exit(&nrp->r_statev4_lock); 6044 6045 *vpp = nvp; 6046 6047 exit: 6048 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6049 kmem_free(argop, argoplist_size); 6050 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 6051 return (e.error); 6052 } 6053 6054 #ifdef DEBUG 6055 void 6056 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 6057 { 6058 uint_t i, len; 6059 zoneid_t zoneid = getzoneid(); 6060 char *s; 6061 6062 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 6063 for (i = 0; i < argcnt; i++) { 6064 nfs_argop4 *op = &argbase[i]; 6065 switch (op->argop) { 6066 case OP_CPUTFH: 6067 case OP_PUTFH: 6068 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 6069 break; 6070 case OP_PUTROOTFH: 6071 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 6072 break; 6073 case OP_CLOOKUP: 6074 s = op->nfs_argop4_u.opclookup.cname; 6075 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6076 break; 6077 case OP_LOOKUP: 6078 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 6079 &len, NULL); 6080 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6081 kmem_free(s, len); 6082 break; 6083 case OP_LOOKUPP: 6084 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 6085 break; 6086 case OP_GETFH: 6087 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 6088 break; 6089 case OP_GETATTR: 6090 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 6091 break; 6092 case OP_OPENATTR: 6093 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 6094 break; 6095 default: 6096 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 6097 op->argop); 6098 break; 6099 } 6100 } 6101 } 6102 #endif 6103 6104 /* 6105 * nfs4lookup_setup - constructs a multi-lookup compound request. 6106 * 6107 * Given the path "nm1/nm2/.../nmn", the following compound requests 6108 * may be created: 6109 * 6110 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 6111 * is faster, for now. 6112 * 6113 * l4_getattrs indicates the type of compound requested. 6114 * 6115 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 6116 * 6117 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 6118 * 6119 * total number of ops is n + 1. 6120 * 6121 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 6122 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 6123 * before the last component, and only get attributes 6124 * for the last component. Note that the second-to-last 6125 * pathname component is XATTR_RPATH, which does NOT go 6126 * over-the-wire as a lookup. 6127 * 6128 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 6129 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 6130 * 6131 * and total number of ops is n + 5. 6132 * 6133 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 6134 * attribute directory: create lookups plus an OPENATTR 6135 * replacing the last lookup. Note that the last pathname 6136 * component is XATTR_RPATH, which does NOT go over-the-wire 6137 * as a lookup. 6138 * 6139 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 6140 * Openattr; Getfh; Getattr } 6141 * 6142 * and total number of ops is n + 5. 6143 * 6144 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6145 * nodes too. 6146 * 6147 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6148 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6149 * 6150 * and total number of ops is 3*n + 1. 6151 * 6152 * All cases: returns the index in the arg array of the final LOOKUP op, or 6153 * -1 if no LOOKUPs were used. 6154 */ 6155 int 6156 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6157 { 6158 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6159 nfs_argop4 *argbase, *argop; 6160 int arglen, argcnt; 6161 int n = 1; /* number of components */ 6162 int nga = 1; /* number of Getattr's in request */ 6163 char c = '\0', *s, *p; 6164 int lookup_idx = -1; 6165 int argoplist_size; 6166 6167 /* set lookuparg response result to 0 */ 6168 lookupargp->resp->status = NFS4_OK; 6169 6170 /* skip leading "/" or "." e.g. ".//./" if there is */ 6171 for (; ; nm++) { 6172 if (*nm != '/' && *nm != '.') 6173 break; 6174 6175 /* ".." is counted as 1 component */ 6176 if (*nm == '.' && *(nm + 1) != '/') 6177 break; 6178 } 6179 6180 /* 6181 * Find n = number of components - nm must be null terminated 6182 * Skip "." components. 6183 */ 6184 if (*nm != '\0') 6185 for (n = 1, s = nm; *s != '\0'; s++) { 6186 if ((*s == '/') && (*(s + 1) != '/') && 6187 (*(s + 1) != '\0') && 6188 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6189 *(s + 2) == '\0'))) 6190 n++; 6191 } 6192 else 6193 n = 0; 6194 6195 /* 6196 * nga is number of components that need Getfh+Getattr 6197 */ 6198 switch (l4_getattrs) { 6199 case LKP4_NO_ATTRIBUTES: 6200 nga = 0; 6201 break; 6202 case LKP4_ALL_ATTRIBUTES: 6203 nga = n; 6204 /* 6205 * Always have at least 1 getfh, getattr pair 6206 */ 6207 if (nga == 0) 6208 nga++; 6209 break; 6210 case LKP4_LAST_ATTRDIR: 6211 case LKP4_LAST_NAMED_ATTR: 6212 nga = n+1; 6213 break; 6214 } 6215 6216 /* 6217 * If change to use the filehandle attr instead of getfh 6218 * the following line can be deleted. 6219 */ 6220 nga *= 2; 6221 6222 /* 6223 * calculate number of ops in request as 6224 * header + trailer + lookups + getattrs 6225 */ 6226 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6227 6228 argoplist_size = arglen * sizeof (nfs_argop4); 6229 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6230 lookupargp->argsp->array = argop; 6231 6232 argcnt = lookupargp->header_len; 6233 argop += argcnt; 6234 6235 /* 6236 * loop and create a lookup op and possibly getattr/getfh for 6237 * each component. Skip "." components. 6238 */ 6239 for (s = nm; *s != '\0'; s = p) { 6240 /* 6241 * Set up a pathname struct for each component if needed 6242 */ 6243 while (*s == '/') 6244 s++; 6245 if (*s == '\0') 6246 break; 6247 6248 for (p = s; (*p != '/') && (*p != '\0'); p++) 6249 ; 6250 c = *p; 6251 *p = '\0'; 6252 6253 if (s[0] == '.' && s[1] == '\0') { 6254 *p = c; 6255 continue; 6256 } 6257 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6258 strcmp(s, XATTR_RPATH) == 0) { 6259 /* getfh XXX may not be needed in future */ 6260 argop->argop = OP_GETFH; 6261 argop++; 6262 argcnt++; 6263 6264 /* getattr */ 6265 argop->argop = OP_GETATTR; 6266 argop->nfs_argop4_u.opgetattr.attr_request = 6267 lookupargp->ga_bits; 6268 argop->nfs_argop4_u.opgetattr.mi = 6269 lookupargp->mi; 6270 argop++; 6271 argcnt++; 6272 6273 /* openattr */ 6274 argop->argop = OP_OPENATTR; 6275 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6276 strcmp(s, XATTR_RPATH) == 0) { 6277 /* openattr */ 6278 argop->argop = OP_OPENATTR; 6279 argop++; 6280 argcnt++; 6281 6282 /* getfh XXX may not be needed in future */ 6283 argop->argop = OP_GETFH; 6284 argop++; 6285 argcnt++; 6286 6287 /* getattr */ 6288 argop->argop = OP_GETATTR; 6289 argop->nfs_argop4_u.opgetattr.attr_request = 6290 lookupargp->ga_bits; 6291 argop->nfs_argop4_u.opgetattr.mi = 6292 lookupargp->mi; 6293 argop++; 6294 argcnt++; 6295 *p = c; 6296 continue; 6297 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6298 /* lookupp */ 6299 argop->argop = OP_LOOKUPP; 6300 } else { 6301 /* lookup */ 6302 argop->argop = OP_LOOKUP; 6303 (void) str_to_utf8(s, 6304 &argop->nfs_argop4_u.oplookup.objname); 6305 } 6306 lookup_idx = argcnt; 6307 argop++; 6308 argcnt++; 6309 6310 *p = c; 6311 6312 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6313 /* getfh XXX may not be needed in future */ 6314 argop->argop = OP_GETFH; 6315 argop++; 6316 argcnt++; 6317 6318 /* getattr */ 6319 argop->argop = OP_GETATTR; 6320 argop->nfs_argop4_u.opgetattr.attr_request = 6321 lookupargp->ga_bits; 6322 argop->nfs_argop4_u.opgetattr.mi = 6323 lookupargp->mi; 6324 argop++; 6325 argcnt++; 6326 } 6327 } 6328 6329 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6330 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6331 if (needgetfh) { 6332 /* stick in a post-lookup getfh */ 6333 argop->argop = OP_GETFH; 6334 argcnt++; 6335 argop++; 6336 } 6337 /* post-lookup getattr */ 6338 argop->argop = OP_GETATTR; 6339 argop->nfs_argop4_u.opgetattr.attr_request = 6340 lookupargp->ga_bits; 6341 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6342 argcnt++; 6343 } 6344 argcnt += lookupargp->trailer_len; /* actual op count */ 6345 lookupargp->argsp->array_len = argcnt; 6346 lookupargp->arglen = arglen; 6347 6348 #ifdef DEBUG 6349 if (nfs4_client_lookup_debug) 6350 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6351 #endif 6352 6353 return (lookup_idx); 6354 } 6355 6356 static int 6357 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6358 { 6359 COMPOUND4args_clnt args; 6360 COMPOUND4res_clnt res; 6361 GETFH4res *gf_res = NULL; 6362 nfs_argop4 argop[4]; 6363 nfs_resop4 *resop = NULL; 6364 nfs4_sharedfh_t *sfhp; 6365 hrtime_t t; 6366 nfs4_error_t e; 6367 6368 rnode4_t *drp; 6369 int doqueue = 1; 6370 vnode_t *vp; 6371 int needrecov = 0; 6372 nfs4_recov_state_t recov_state; 6373 6374 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6375 6376 *avp = NULL; 6377 recov_state.rs_flags = 0; 6378 recov_state.rs_num_retry_despite_err = 0; 6379 6380 recov_retry: 6381 /* COMPOUND: putfh, openattr, getfh, getattr */ 6382 args.array_len = 4; 6383 args.array = argop; 6384 args.ctag = TAG_OPENATTR; 6385 6386 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6387 if (e.error) 6388 return (e.error); 6389 6390 drp = VTOR4(dvp); 6391 6392 /* putfh */ 6393 argop[0].argop = OP_CPUTFH; 6394 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6395 6396 /* openattr */ 6397 argop[1].argop = OP_OPENATTR; 6398 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6399 6400 /* getfh */ 6401 argop[2].argop = OP_GETFH; 6402 6403 /* getattr */ 6404 argop[3].argop = OP_GETATTR; 6405 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6406 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6407 6408 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6409 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6410 rnode4info(drp))); 6411 6412 t = gethrtime(); 6413 6414 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6415 6416 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6417 if (needrecov) { 6418 bool_t abort; 6419 6420 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6421 "nfs4openattr: initiating recovery\n")); 6422 6423 abort = nfs4_start_recovery(&e, 6424 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6425 OP_OPENATTR, NULL, NULL, NULL); 6426 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6427 if (!e.error) { 6428 e.error = geterrno4(res.status); 6429 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6430 } 6431 if (abort == FALSE) 6432 goto recov_retry; 6433 return (e.error); 6434 } 6435 6436 if (e.error) { 6437 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6438 return (e.error); 6439 } 6440 6441 if (res.status) { 6442 /* 6443 * If OTW errro is NOTSUPP, then it should be 6444 * translated to EINVAL. All Solaris file system 6445 * implementations return EINVAL to the syscall layer 6446 * when the attrdir cannot be created due to an 6447 * implementation restriction or noxattr mount option. 6448 */ 6449 if (res.status == NFS4ERR_NOTSUPP) { 6450 mutex_enter(&drp->r_statelock); 6451 if (drp->r_xattr_dir) 6452 VN_RELE(drp->r_xattr_dir); 6453 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6454 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6455 mutex_exit(&drp->r_statelock); 6456 6457 e.error = EINVAL; 6458 } else { 6459 e.error = geterrno4(res.status); 6460 } 6461 6462 if (e.error) { 6463 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6464 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6465 needrecov); 6466 return (e.error); 6467 } 6468 } 6469 6470 resop = &res.array[0]; /* putfh res */ 6471 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6472 6473 resop = &res.array[1]; /* openattr res */ 6474 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6475 6476 resop = &res.array[2]; /* getfh res */ 6477 gf_res = &resop->nfs_resop4_u.opgetfh; 6478 if (gf_res->object.nfs_fh4_len == 0) { 6479 *avp = NULL; 6480 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6481 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6482 return (ENOENT); 6483 } 6484 6485 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6486 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6487 dvp->v_vfsp, t, cr, dvp, 6488 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp)); 6489 sfh4_rele(&sfhp); 6490 6491 if (e.error) 6492 PURGE_ATTRCACHE4(vp); 6493 6494 mutex_enter(&vp->v_lock); 6495 vp->v_flag |= V_XATTRDIR; 6496 mutex_exit(&vp->v_lock); 6497 6498 *avp = vp; 6499 6500 mutex_enter(&drp->r_statelock); 6501 if (drp->r_xattr_dir) 6502 VN_RELE(drp->r_xattr_dir); 6503 VN_HOLD(vp); 6504 drp->r_xattr_dir = vp; 6505 6506 /* 6507 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6508 * NULL. xattrs could be created at any time, and we have no 6509 * way to update pc4_xattr_exists in the base object if/when 6510 * it happens. 6511 */ 6512 drp->r_pathconf.pc4_xattr_valid = 0; 6513 6514 mutex_exit(&drp->r_statelock); 6515 6516 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6517 6518 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6519 6520 return (0); 6521 } 6522 6523 /* ARGSUSED */ 6524 static int 6525 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6526 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct, 6527 vsecattr_t *vsecp) 6528 { 6529 int error; 6530 vnode_t *vp = NULL; 6531 rnode4_t *rp; 6532 struct vattr vattr; 6533 rnode4_t *drp; 6534 vnode_t *tempvp; 6535 enum createmode4 createmode; 6536 bool_t must_trunc = FALSE; 6537 int truncating = 0; 6538 6539 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6540 return (EPERM); 6541 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6542 return (EINVAL); 6543 } 6544 6545 /* . and .. have special meaning in the protocol, reject them. */ 6546 6547 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6548 return (EISDIR); 6549 6550 drp = VTOR4(dvp); 6551 6552 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6553 return (EINTR); 6554 6555 top: 6556 /* 6557 * We make a copy of the attributes because the caller does not 6558 * expect us to change what va points to. 6559 */ 6560 vattr = *va; 6561 6562 /* 6563 * If the pathname is "", then dvp is the root vnode of 6564 * a remote file mounted over a local directory. 6565 * All that needs to be done is access 6566 * checking and truncation. Note that we avoid doing 6567 * open w/ create because the parent directory might 6568 * be in pseudo-fs and the open would fail. 6569 */ 6570 if (*nm == '\0') { 6571 error = 0; 6572 VN_HOLD(dvp); 6573 vp = dvp; 6574 must_trunc = TRUE; 6575 } else { 6576 /* 6577 * We need to go over the wire, just to be sure whether the 6578 * file exists or not. Using the DNLC can be dangerous in 6579 * this case when making a decision regarding existence. 6580 */ 6581 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6582 } 6583 6584 if (exclusive) 6585 createmode = EXCLUSIVE4; 6586 else 6587 createmode = GUARDED4; 6588 6589 /* 6590 * error would be set if the file does not exist on the 6591 * server, so lets go create it. 6592 */ 6593 if (error) { 6594 goto create_otw; 6595 } 6596 6597 /* 6598 * File does exist on the server 6599 */ 6600 if (exclusive == EXCL) 6601 error = EEXIST; 6602 else if (vp->v_type == VDIR && (mode & VWRITE)) 6603 error = EISDIR; 6604 else { 6605 /* 6606 * If vnode is a device, create special vnode. 6607 */ 6608 if (ISVDEV(vp->v_type)) { 6609 tempvp = vp; 6610 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6611 VN_RELE(tempvp); 6612 } 6613 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) { 6614 if ((vattr.va_mask & AT_SIZE) && 6615 vp->v_type == VREG) { 6616 rp = VTOR4(vp); 6617 /* 6618 * Check here for large file handled 6619 * by LF-unaware process (as 6620 * ufs_create() does) 6621 */ 6622 if (!(flags & FOFFMAX)) { 6623 mutex_enter(&rp->r_statelock); 6624 if (rp->r_size > MAXOFF32_T) 6625 error = EOVERFLOW; 6626 mutex_exit(&rp->r_statelock); 6627 } 6628 6629 /* if error is set then we need to return */ 6630 if (error) { 6631 nfs_rw_exit(&drp->r_rwlock); 6632 VN_RELE(vp); 6633 return (error); 6634 } 6635 6636 if (must_trunc) { 6637 vattr.va_mask = AT_SIZE; 6638 error = nfs4setattr(vp, &vattr, 0, cr, 6639 NULL); 6640 } else { 6641 /* 6642 * we know we have a regular file that already 6643 * exists and we may end up truncating the file 6644 * as a result of the open_otw, so flush out 6645 * any dirty pages for this file first. 6646 */ 6647 if (nfs4_has_pages(vp) && 6648 ((rp->r_flags & R4DIRTY) || 6649 rp->r_count > 0 || 6650 rp->r_mapcnt > 0)) { 6651 error = nfs4_putpage(vp, 6652 (offset_t)0, 0, 0, cr, ct); 6653 if (error && (error == ENOSPC || 6654 error == EDQUOT)) { 6655 mutex_enter( 6656 &rp->r_statelock); 6657 if (!rp->r_error) 6658 rp->r_error = 6659 error; 6660 mutex_exit( 6661 &rp->r_statelock); 6662 } 6663 } 6664 vattr.va_mask = (AT_SIZE | 6665 AT_TYPE | AT_MODE); 6666 vattr.va_type = VREG; 6667 createmode = UNCHECKED4; 6668 truncating = 1; 6669 goto create_otw; 6670 } 6671 } 6672 } 6673 } 6674 nfs_rw_exit(&drp->r_rwlock); 6675 if (error) { 6676 VN_RELE(vp); 6677 } else { 6678 vnode_t *tvp; 6679 rnode4_t *trp; 6680 tvp = vp; 6681 if (vp->v_type == VREG) { 6682 trp = VTOR4(vp); 6683 if (IS_SHADOW(vp, trp)) 6684 tvp = RTOV4(trp); 6685 } 6686 6687 if (must_trunc) { 6688 /* 6689 * existing file got truncated, notify. 6690 */ 6691 vnevent_create(tvp, ct); 6692 } 6693 6694 *vpp = vp; 6695 } 6696 return (error); 6697 6698 create_otw: 6699 dnlc_remove(dvp, nm); 6700 6701 ASSERT(vattr.va_mask & AT_TYPE); 6702 6703 /* 6704 * If not a regular file let nfs4mknod() handle it. 6705 */ 6706 if (vattr.va_type != VREG) { 6707 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6708 nfs_rw_exit(&drp->r_rwlock); 6709 return (error); 6710 } 6711 6712 /* 6713 * It _is_ a regular file. 6714 */ 6715 ASSERT(vattr.va_mask & AT_MODE); 6716 if (MANDMODE(vattr.va_mode)) { 6717 nfs_rw_exit(&drp->r_rwlock); 6718 return (EACCES); 6719 } 6720 6721 /* 6722 * If this happens to be a mknod of a regular file, then flags will 6723 * have neither FREAD or FWRITE. However, we must set at least one 6724 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6725 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6726 * set (based on openmode specified by app). 6727 */ 6728 if ((flags & (FREAD|FWRITE)) == 0) 6729 flags |= (FREAD|FWRITE); 6730 6731 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6732 6733 if (vp != NULL) { 6734 /* if create was successful, throw away the file's pages */ 6735 if (!error && (vattr.va_mask & AT_SIZE)) 6736 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6737 cr); 6738 /* release the lookup hold */ 6739 VN_RELE(vp); 6740 vp = NULL; 6741 } 6742 6743 /* 6744 * validate that we opened a regular file. This handles a misbehaving 6745 * server that returns an incorrect FH. 6746 */ 6747 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6748 error = EISDIR; 6749 VN_RELE(*vpp); 6750 } 6751 6752 /* 6753 * If this is not an exclusive create, then the CREATE 6754 * request will be made with the GUARDED mode set. This 6755 * means that the server will return EEXIST if the file 6756 * exists. The file could exist because of a retransmitted 6757 * request. In this case, we recover by starting over and 6758 * checking to see whether the file exists. This second 6759 * time through it should and a CREATE request will not be 6760 * sent. 6761 * 6762 * This handles the problem of a dangling CREATE request 6763 * which contains attributes which indicate that the file 6764 * should be truncated. This retransmitted request could 6765 * possibly truncate valid data in the file if not caught 6766 * by the duplicate request mechanism on the server or if 6767 * not caught by other means. The scenario is: 6768 * 6769 * Client transmits CREATE request with size = 0 6770 * Client times out, retransmits request. 6771 * Response to the first request arrives from the server 6772 * and the client proceeds on. 6773 * Client writes data to the file. 6774 * The server now processes retransmitted CREATE request 6775 * and truncates file. 6776 * 6777 * The use of the GUARDED CREATE request prevents this from 6778 * happening because the retransmitted CREATE would fail 6779 * with EEXIST and would not truncate the file. 6780 */ 6781 if (error == EEXIST && exclusive == NONEXCL) { 6782 #ifdef DEBUG 6783 nfs4_create_misses++; 6784 #endif 6785 goto top; 6786 } 6787 nfs_rw_exit(&drp->r_rwlock); 6788 if (truncating && !error && *vpp) { 6789 vnode_t *tvp; 6790 rnode4_t *trp; 6791 /* 6792 * existing file got truncated, notify. 6793 */ 6794 tvp = *vpp; 6795 trp = VTOR4(tvp); 6796 if (IS_SHADOW(tvp, trp)) 6797 tvp = RTOV4(trp); 6798 vnevent_create(tvp, ct); 6799 } 6800 return (error); 6801 } 6802 6803 /* 6804 * Create compound (for mkdir, mknod, symlink): 6805 * { Putfh <dfh>; Create; Getfh; Getattr } 6806 * It's okay if setattr failed to set gid - this is not considered 6807 * an error, but purge attrs in that case. 6808 */ 6809 static int 6810 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6811 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6812 { 6813 int need_end_op = FALSE; 6814 COMPOUND4args_clnt args; 6815 COMPOUND4res_clnt res, *resp = NULL; 6816 nfs_argop4 *argop; 6817 nfs_resop4 *resop; 6818 int doqueue; 6819 mntinfo4_t *mi; 6820 rnode4_t *drp = VTOR4(dvp); 6821 change_info4 *cinfo; 6822 GETFH4res *gf_res; 6823 struct vattr vattr; 6824 vnode_t *vp; 6825 fattr4 *crattr; 6826 bool_t needrecov = FALSE; 6827 nfs4_recov_state_t recov_state; 6828 nfs4_sharedfh_t *sfhp = NULL; 6829 hrtime_t t; 6830 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6831 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6832 dirattr_info_t dinfo, *dinfop; 6833 servinfo4_t *svp; 6834 bitmap4 supp_attrs; 6835 6836 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6837 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6838 6839 mi = VTOMI4(dvp); 6840 6841 /* 6842 * Make sure we properly deal with setting the right gid 6843 * on a new directory to reflect the parent's setgid bit 6844 */ 6845 setgid_flag = 0; 6846 if (type == NF4DIR) { 6847 struct vattr dva; 6848 6849 va->va_mode &= ~VSGID; 6850 dva.va_mask = AT_MODE | AT_GID; 6851 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) { 6852 6853 /* 6854 * If the parent's directory has the setgid bit set 6855 * _and_ the client was able to get a valid mapping 6856 * for the parent dir's owner_group, we want to 6857 * append NVERIFY(owner_group == dva.va_gid) and 6858 * SETTATTR to the CREATE compound. 6859 */ 6860 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6861 setgid_flag = 1; 6862 va->va_mode |= VSGID; 6863 if (dva.va_gid != GID_NOBODY) { 6864 va->va_mask |= AT_GID; 6865 va->va_gid = dva.va_gid; 6866 } 6867 } 6868 } 6869 } 6870 6871 /* 6872 * Create ops: 6873 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6874 * 5:restorefh(dir) 6:getattr(dir) 6875 * 6876 * if (setgid) 6877 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6878 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6879 * 8:nverify 9:setattr 6880 */ 6881 if (setgid_flag) { 6882 numops = 10; 6883 idx_create = 1; 6884 idx_fattr = 3; 6885 } else { 6886 numops = 7; 6887 idx_create = 2; 6888 idx_fattr = 4; 6889 } 6890 6891 ASSERT(nfs_zone() == mi->mi_zone); 6892 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6893 return (EINTR); 6894 } 6895 recov_state.rs_flags = 0; 6896 recov_state.rs_num_retry_despite_err = 0; 6897 6898 argoplist_size = numops * sizeof (nfs_argop4); 6899 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6900 6901 recov_retry: 6902 if (type == NF4LNK) 6903 args.ctag = TAG_SYMLINK; 6904 else if (type == NF4DIR) 6905 args.ctag = TAG_MKDIR; 6906 else 6907 args.ctag = TAG_MKNOD; 6908 6909 args.array_len = numops; 6910 args.array = argop; 6911 6912 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6913 nfs_rw_exit(&drp->r_rwlock); 6914 kmem_free(argop, argoplist_size); 6915 return (e.error); 6916 } 6917 need_end_op = TRUE; 6918 6919 6920 /* 0: putfh directory */ 6921 argop[0].argop = OP_CPUTFH; 6922 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6923 6924 /* 1/2: Create object */ 6925 argop[idx_create].argop = OP_CCREATE; 6926 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6927 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6928 if (type == NF4LNK) { 6929 /* 6930 * symlink, treat name as data 6931 */ 6932 ASSERT(data != NULL); 6933 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6934 (char *)data; 6935 } 6936 if (type == NF4BLK || type == NF4CHR) { 6937 ASSERT(data != NULL); 6938 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6939 *((specdata4 *)data); 6940 } 6941 6942 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6943 6944 svp = drp->r_server; 6945 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6946 supp_attrs = svp->sv_supp_attrs; 6947 nfs_rw_exit(&svp->sv_lock); 6948 6949 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6950 nfs_rw_exit(&drp->r_rwlock); 6951 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6952 e.error = EINVAL; 6953 kmem_free(argop, argoplist_size); 6954 return (e.error); 6955 } 6956 6957 /* 2/3: getfh fh of created object */ 6958 ASSERT(idx_create + 1 == idx_fattr - 1); 6959 argop[idx_create + 1].argop = OP_GETFH; 6960 6961 /* 3/4: getattr of new object */ 6962 argop[idx_fattr].argop = OP_GETATTR; 6963 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6964 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6965 6966 if (setgid_flag) { 6967 vattr_t _v; 6968 6969 argop[4].argop = OP_SAVEFH; 6970 6971 argop[5].argop = OP_CPUTFH; 6972 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6973 6974 argop[6].argop = OP_GETATTR; 6975 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6976 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6977 6978 argop[7].argop = OP_RESTOREFH; 6979 6980 /* 6981 * nverify 6982 * 6983 * XXX - Revisit the last argument to nfs4_end_op() 6984 * once 5020486 is fixed. 6985 */ 6986 _v.va_mask = AT_GID; 6987 _v.va_gid = va->va_gid; 6988 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6989 supp_attrs)) { 6990 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6991 nfs_rw_exit(&drp->r_rwlock); 6992 nfs4_fattr4_free(crattr); 6993 kmem_free(argop, argoplist_size); 6994 return (e.error); 6995 } 6996 6997 /* 6998 * setattr 6999 * 7000 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 7001 * so no need for stateid or flags. Also we specify NULL 7002 * rp since we're only interested in setting owner_group 7003 * attributes. 7004 */ 7005 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 7006 &e.error, 0); 7007 7008 if (e.error) { 7009 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 7010 nfs_rw_exit(&drp->r_rwlock); 7011 nfs4_fattr4_free(crattr); 7012 nfs4args_verify_free(&argop[8]); 7013 kmem_free(argop, argoplist_size); 7014 return (e.error); 7015 } 7016 } else { 7017 argop[1].argop = OP_SAVEFH; 7018 7019 argop[5].argop = OP_RESTOREFH; 7020 7021 argop[6].argop = OP_GETATTR; 7022 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7023 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7024 } 7025 7026 dnlc_remove(dvp, nm); 7027 7028 doqueue = 1; 7029 t = gethrtime(); 7030 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7031 7032 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7033 if (e.error) { 7034 PURGE_ATTRCACHE4(dvp); 7035 if (!needrecov) 7036 goto out; 7037 } 7038 7039 if (needrecov) { 7040 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 7041 OP_CREATE, NULL, NULL, NULL) == FALSE) { 7042 nfs4_end_op(mi, dvp, NULL, &recov_state, 7043 needrecov); 7044 need_end_op = FALSE; 7045 nfs4_fattr4_free(crattr); 7046 if (setgid_flag) { 7047 nfs4args_verify_free(&argop[8]); 7048 nfs4args_setattr_free(&argop[9]); 7049 } 7050 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 7051 goto recov_retry; 7052 } 7053 } 7054 7055 resp = &res; 7056 7057 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 7058 7059 if (res.status == NFS4ERR_BADOWNER) 7060 nfs4_log_badowner(mi, OP_CREATE); 7061 7062 e.error = geterrno4(res.status); 7063 7064 /* 7065 * This check is left over from when create was implemented 7066 * using a setattr op (instead of createattrs). If the 7067 * putfh/create/getfh failed, the error was returned. If 7068 * setattr/getattr failed, we keep going. 7069 * 7070 * It might be better to get rid of the GETFH also, and just 7071 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 7072 * Then if any of the operations failed, we could return the 7073 * error now, and remove much of the error code below. 7074 */ 7075 if (res.array_len <= idx_fattr) { 7076 /* 7077 * Either Putfh, Create or Getfh failed. 7078 */ 7079 PURGE_ATTRCACHE4(dvp); 7080 /* 7081 * nfs4_purge_stale_fh() may generate otw calls through 7082 * nfs4_invalidate_pages. Hence the need to call 7083 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 7084 */ 7085 nfs4_end_op(mi, dvp, NULL, &recov_state, 7086 needrecov); 7087 need_end_op = FALSE; 7088 nfs4_purge_stale_fh(e.error, dvp, cr); 7089 goto out; 7090 } 7091 } 7092 7093 resop = &res.array[idx_create]; /* create res */ 7094 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 7095 7096 resop = &res.array[idx_create + 1]; /* getfh res */ 7097 gf_res = &resop->nfs_resop4_u.opgetfh; 7098 7099 sfhp = sfh4_get(&gf_res->object, mi); 7100 if (e.error) { 7101 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 7102 fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7103 if (vp->v_type == VNON) { 7104 vattr.va_mask = AT_TYPE; 7105 /* 7106 * Need to call nfs4_end_op before nfs4getattr to avoid 7107 * potential nfs4_start_op deadlock. See RFE 4777612. 7108 */ 7109 nfs4_end_op(mi, dvp, NULL, &recov_state, 7110 needrecov); 7111 need_end_op = FALSE; 7112 e.error = nfs4getattr(vp, &vattr, cr); 7113 if (e.error) { 7114 VN_RELE(vp); 7115 *vpp = NULL; 7116 goto out; 7117 } 7118 vp->v_type = vattr.va_type; 7119 } 7120 e.error = 0; 7121 } else { 7122 *vpp = vp = makenfs4node(sfhp, 7123 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 7124 dvp->v_vfsp, t, cr, 7125 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7126 } 7127 7128 /* 7129 * If compound succeeded, then update dir attrs 7130 */ 7131 if (res.status == NFS4_OK) { 7132 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 7133 dinfo.di_cred = cr; 7134 dinfo.di_time_call = t; 7135 dinfop = &dinfo; 7136 } else 7137 dinfop = NULL; 7138 7139 /* Update directory cache attribute, readdir and dnlc caches */ 7140 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 7141 7142 out: 7143 if (sfhp != NULL) 7144 sfh4_rele(&sfhp); 7145 nfs_rw_exit(&drp->r_rwlock); 7146 nfs4_fattr4_free(crattr); 7147 if (setgid_flag) { 7148 nfs4args_verify_free(&argop[8]); 7149 nfs4args_setattr_free(&argop[9]); 7150 } 7151 if (resp) 7152 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7153 if (need_end_op) 7154 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7155 7156 kmem_free(argop, argoplist_size); 7157 return (e.error); 7158 } 7159 7160 /* ARGSUSED */ 7161 static int 7162 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7163 int mode, vnode_t **vpp, cred_t *cr) 7164 { 7165 int error; 7166 vnode_t *vp; 7167 nfs_ftype4 type; 7168 specdata4 spec, *specp = NULL; 7169 7170 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7171 7172 switch (va->va_type) { 7173 case VCHR: 7174 case VBLK: 7175 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7176 spec.specdata1 = getmajor(va->va_rdev); 7177 spec.specdata2 = getminor(va->va_rdev); 7178 specp = &spec; 7179 break; 7180 7181 case VFIFO: 7182 type = NF4FIFO; 7183 break; 7184 case VSOCK: 7185 type = NF4SOCK; 7186 break; 7187 7188 default: 7189 return (EINVAL); 7190 } 7191 7192 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7193 if (error) { 7194 return (error); 7195 } 7196 7197 /* 7198 * This might not be needed any more; special case to deal 7199 * with problematic v2/v3 servers. Since create was unable 7200 * to set group correctly, not sure what hope setattr has. 7201 */ 7202 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7203 va->va_mask = AT_GID; 7204 (void) nfs4setattr(vp, va, 0, cr, NULL); 7205 } 7206 7207 /* 7208 * If vnode is a device create special vnode 7209 */ 7210 if (ISVDEV(vp->v_type)) { 7211 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7212 VN_RELE(vp); 7213 } else { 7214 *vpp = vp; 7215 } 7216 return (error); 7217 } 7218 7219 /* 7220 * Remove requires that the current fh be the target directory. 7221 * After the operation, the current fh is unchanged. 7222 * The compound op structure is: 7223 * PUTFH(targetdir), REMOVE 7224 * 7225 * Weirdness: if the vnode to be removed is open 7226 * we rename it instead of removing it and nfs_inactive 7227 * will remove the new name. 7228 */ 7229 /* ARGSUSED */ 7230 static int 7231 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags) 7232 { 7233 COMPOUND4args_clnt args; 7234 COMPOUND4res_clnt res, *resp = NULL; 7235 REMOVE4res *rm_res; 7236 nfs_argop4 argop[3]; 7237 nfs_resop4 *resop; 7238 vnode_t *vp; 7239 char *tmpname; 7240 int doqueue; 7241 mntinfo4_t *mi; 7242 rnode4_t *rp; 7243 rnode4_t *drp; 7244 int needrecov = 0; 7245 nfs4_recov_state_t recov_state; 7246 int isopen; 7247 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7248 dirattr_info_t dinfo; 7249 7250 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7251 return (EPERM); 7252 drp = VTOR4(dvp); 7253 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7254 return (EINTR); 7255 7256 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7257 if (e.error) { 7258 nfs_rw_exit(&drp->r_rwlock); 7259 return (e.error); 7260 } 7261 7262 if (vp->v_type == VDIR) { 7263 VN_RELE(vp); 7264 nfs_rw_exit(&drp->r_rwlock); 7265 return (EISDIR); 7266 } 7267 7268 /* 7269 * First just remove the entry from the name cache, as it 7270 * is most likely the only entry for this vp. 7271 */ 7272 dnlc_remove(dvp, nm); 7273 7274 rp = VTOR4(vp); 7275 7276 /* 7277 * For regular file types, check to see if the file is open by looking 7278 * at the open streams. 7279 * For all other types, check the reference count on the vnode. Since 7280 * they are not opened OTW they never have an open stream. 7281 * 7282 * If the file is open, rename it to .nfsXXXX. 7283 */ 7284 if (vp->v_type != VREG) { 7285 /* 7286 * If the file has a v_count > 1 then there may be more than one 7287 * entry in the name cache due multiple links or an open file, 7288 * but we don't have the real reference count so flush all 7289 * possible entries. 7290 */ 7291 if (vp->v_count > 1) 7292 dnlc_purge_vp(vp); 7293 7294 /* 7295 * Now we have the real reference count. 7296 */ 7297 isopen = vp->v_count > 1; 7298 } else { 7299 mutex_enter(&rp->r_os_lock); 7300 isopen = list_head(&rp->r_open_streams) != NULL; 7301 mutex_exit(&rp->r_os_lock); 7302 } 7303 7304 mutex_enter(&rp->r_statelock); 7305 if (isopen && 7306 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7307 mutex_exit(&rp->r_statelock); 7308 tmpname = newname(); 7309 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct); 7310 if (e.error) 7311 kmem_free(tmpname, MAXNAMELEN); 7312 else { 7313 mutex_enter(&rp->r_statelock); 7314 if (rp->r_unldvp == NULL) { 7315 VN_HOLD(dvp); 7316 rp->r_unldvp = dvp; 7317 if (rp->r_unlcred != NULL) 7318 crfree(rp->r_unlcred); 7319 crhold(cr); 7320 rp->r_unlcred = cr; 7321 rp->r_unlname = tmpname; 7322 } else { 7323 kmem_free(rp->r_unlname, MAXNAMELEN); 7324 rp->r_unlname = tmpname; 7325 } 7326 mutex_exit(&rp->r_statelock); 7327 } 7328 VN_RELE(vp); 7329 nfs_rw_exit(&drp->r_rwlock); 7330 return (e.error); 7331 } 7332 /* 7333 * Actually remove the file/dir 7334 */ 7335 mutex_exit(&rp->r_statelock); 7336 7337 /* 7338 * We need to flush any dirty pages which happen to 7339 * be hanging around before removing the file. 7340 * This shouldn't happen very often since in NFSv4 7341 * we should be close to open consistent. 7342 */ 7343 if (nfs4_has_pages(vp) && 7344 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7345 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct); 7346 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7347 mutex_enter(&rp->r_statelock); 7348 if (!rp->r_error) 7349 rp->r_error = e.error; 7350 mutex_exit(&rp->r_statelock); 7351 } 7352 } 7353 7354 mi = VTOMI4(dvp); 7355 7356 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7357 recov_state.rs_flags = 0; 7358 recov_state.rs_num_retry_despite_err = 0; 7359 7360 recov_retry: 7361 /* 7362 * Remove ops: putfh dir; remove 7363 */ 7364 args.ctag = TAG_REMOVE; 7365 args.array_len = 3; 7366 args.array = argop; 7367 7368 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7369 if (e.error) { 7370 nfs_rw_exit(&drp->r_rwlock); 7371 VN_RELE(vp); 7372 return (e.error); 7373 } 7374 7375 /* putfh directory */ 7376 argop[0].argop = OP_CPUTFH; 7377 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7378 7379 /* remove */ 7380 argop[1].argop = OP_CREMOVE; 7381 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7382 7383 /* getattr dir */ 7384 argop[2].argop = OP_GETATTR; 7385 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7386 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7387 7388 doqueue = 1; 7389 dinfo.di_time_call = gethrtime(); 7390 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7391 7392 PURGE_ATTRCACHE4(vp); 7393 7394 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7395 if (e.error) 7396 PURGE_ATTRCACHE4(dvp); 7397 7398 if (needrecov) { 7399 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7400 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 7401 if (!e.error) 7402 (void) xdr_free(xdr_COMPOUND4res_clnt, 7403 (caddr_t)&res); 7404 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7405 needrecov); 7406 goto recov_retry; 7407 } 7408 } 7409 7410 /* 7411 * Matching nfs4_end_op() for start_op() above. 7412 * There is a path in the code below which calls 7413 * nfs4_purge_stale_fh(), which may generate otw calls through 7414 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7415 * here to avoid nfs4_start_op() deadlock. 7416 */ 7417 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7418 7419 if (!e.error) { 7420 resp = &res; 7421 7422 if (res.status) { 7423 e.error = geterrno4(res.status); 7424 PURGE_ATTRCACHE4(dvp); 7425 nfs4_purge_stale_fh(e.error, dvp, cr); 7426 } else { 7427 resop = &res.array[1]; /* remove res */ 7428 rm_res = &resop->nfs_resop4_u.opremove; 7429 7430 dinfo.di_garp = 7431 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7432 dinfo.di_cred = cr; 7433 7434 /* Update directory attr, readdir and dnlc caches */ 7435 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7436 &dinfo); 7437 } 7438 } 7439 nfs_rw_exit(&drp->r_rwlock); 7440 if (resp) 7441 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7442 7443 if (e.error == 0) { 7444 vnode_t *tvp; 7445 rnode4_t *trp; 7446 trp = VTOR4(vp); 7447 tvp = vp; 7448 if (IS_SHADOW(vp, trp)) 7449 tvp = RTOV4(trp); 7450 vnevent_remove(tvp, dvp, nm, ct); 7451 } 7452 VN_RELE(vp); 7453 return (e.error); 7454 } 7455 7456 /* 7457 * Link requires that the current fh be the target directory and the 7458 * saved fh be the source fh. After the operation, the current fh is unchanged. 7459 * Thus the compound op structure is: 7460 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7461 * GETATTR(file) 7462 */ 7463 /* ARGSUSED */ 7464 static int 7465 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr, 7466 caller_context_t *ct, int flags) 7467 { 7468 COMPOUND4args_clnt args; 7469 COMPOUND4res_clnt res, *resp = NULL; 7470 LINK4res *ln_res; 7471 int argoplist_size = 7 * sizeof (nfs_argop4); 7472 nfs_argop4 *argop; 7473 nfs_resop4 *resop; 7474 vnode_t *realvp, *nvp; 7475 int doqueue; 7476 mntinfo4_t *mi; 7477 rnode4_t *tdrp; 7478 bool_t needrecov = FALSE; 7479 nfs4_recov_state_t recov_state; 7480 hrtime_t t; 7481 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7482 dirattr_info_t dinfo; 7483 7484 ASSERT(*tnm != '\0'); 7485 ASSERT(tdvp->v_type == VDIR); 7486 ASSERT(nfs4_consistent_type(tdvp)); 7487 ASSERT(nfs4_consistent_type(svp)); 7488 7489 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7490 return (EPERM); 7491 if (VOP_REALVP(svp, &realvp, ct) == 0) { 7492 svp = realvp; 7493 ASSERT(nfs4_consistent_type(svp)); 7494 } 7495 7496 tdrp = VTOR4(tdvp); 7497 mi = VTOMI4(svp); 7498 7499 if (!(mi->mi_flags & MI4_LINK)) { 7500 return (EOPNOTSUPP); 7501 } 7502 recov_state.rs_flags = 0; 7503 recov_state.rs_num_retry_despite_err = 0; 7504 7505 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7506 return (EINTR); 7507 7508 recov_retry: 7509 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7510 7511 args.ctag = TAG_LINK; 7512 7513 /* 7514 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7515 * restorefh; getattr(fl) 7516 */ 7517 args.array_len = 7; 7518 args.array = argop; 7519 7520 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7521 if (e.error) { 7522 kmem_free(argop, argoplist_size); 7523 nfs_rw_exit(&tdrp->r_rwlock); 7524 return (e.error); 7525 } 7526 7527 /* 0. putfh file */ 7528 argop[0].argop = OP_CPUTFH; 7529 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7530 7531 /* 1. save current fh to free up the space for the dir */ 7532 argop[1].argop = OP_SAVEFH; 7533 7534 /* 2. putfh targetdir */ 7535 argop[2].argop = OP_CPUTFH; 7536 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7537 7538 /* 3. link: current_fh is targetdir, saved_fh is source */ 7539 argop[3].argop = OP_CLINK; 7540 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7541 7542 /* 4. Get attributes of dir */ 7543 argop[4].argop = OP_GETATTR; 7544 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7545 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7546 7547 /* 5. If link was successful, restore current vp to file */ 7548 argop[5].argop = OP_RESTOREFH; 7549 7550 /* 6. Get attributes of linked object */ 7551 argop[6].argop = OP_GETATTR; 7552 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7553 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7554 7555 dnlc_remove(tdvp, tnm); 7556 7557 doqueue = 1; 7558 t = gethrtime(); 7559 7560 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7561 7562 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7563 if (e.error != 0 && !needrecov) { 7564 PURGE_ATTRCACHE4(tdvp); 7565 PURGE_ATTRCACHE4(svp); 7566 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7567 goto out; 7568 } 7569 7570 if (needrecov) { 7571 bool_t abort; 7572 7573 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7574 NULL, NULL, OP_LINK, NULL, NULL, NULL); 7575 if (abort == FALSE) { 7576 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7577 needrecov); 7578 kmem_free(argop, argoplist_size); 7579 if (!e.error) 7580 (void) xdr_free(xdr_COMPOUND4res_clnt, 7581 (caddr_t)&res); 7582 goto recov_retry; 7583 } else { 7584 if (e.error != 0) { 7585 PURGE_ATTRCACHE4(tdvp); 7586 PURGE_ATTRCACHE4(svp); 7587 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7588 &recov_state, needrecov); 7589 goto out; 7590 } 7591 /* fall through for res.status case */ 7592 } 7593 } 7594 7595 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7596 7597 resp = &res; 7598 if (res.status) { 7599 /* If link succeeded, then don't return error */ 7600 e.error = geterrno4(res.status); 7601 if (res.array_len <= 4) { 7602 /* 7603 * Either Putfh, Savefh, Putfh dir, or Link failed 7604 */ 7605 PURGE_ATTRCACHE4(svp); 7606 PURGE_ATTRCACHE4(tdvp); 7607 if (e.error == EOPNOTSUPP) { 7608 mutex_enter(&mi->mi_lock); 7609 mi->mi_flags &= ~MI4_LINK; 7610 mutex_exit(&mi->mi_lock); 7611 } 7612 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7613 /* XXX-LP */ 7614 if (e.error == EISDIR && crgetuid(cr) != 0) 7615 e.error = EPERM; 7616 goto out; 7617 } 7618 } 7619 7620 /* either no error or one of the postop getattr failed */ 7621 7622 /* 7623 * XXX - if LINK succeeded, but no attrs were returned for link 7624 * file, purge its cache. 7625 * 7626 * XXX Perform a simplified version of wcc checking. Instead of 7627 * have another getattr to get pre-op, just purge cache if 7628 * any of the ops prior to and including the getattr failed. 7629 * If the getattr succeeded then update the attrcache accordingly. 7630 */ 7631 7632 /* 7633 * update cache with link file postattrs. 7634 * Note: at this point resop points to link res. 7635 */ 7636 resop = &res.array[3]; /* link res */ 7637 ln_res = &resop->nfs_resop4_u.oplink; 7638 if (res.status == NFS4_OK) 7639 e.error = nfs4_update_attrcache(res.status, 7640 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7641 t, svp, cr); 7642 7643 /* 7644 * Call makenfs4node to create the new shadow vp for tnm. 7645 * We pass NULL attrs because we just cached attrs for 7646 * the src object. All we're trying to accomplish is to 7647 * to create the new shadow vnode. 7648 */ 7649 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7650 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh)); 7651 7652 /* Update target cache attribute, readdir and dnlc caches */ 7653 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7654 dinfo.di_time_call = t; 7655 dinfo.di_cred = cr; 7656 7657 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7658 ASSERT(nfs4_consistent_type(tdvp)); 7659 ASSERT(nfs4_consistent_type(svp)); 7660 ASSERT(nfs4_consistent_type(nvp)); 7661 VN_RELE(nvp); 7662 7663 if (!e.error) { 7664 vnode_t *tvp; 7665 rnode4_t *trp; 7666 /* 7667 * Notify the source file of this link operation. 7668 */ 7669 trp = VTOR4(svp); 7670 tvp = svp; 7671 if (IS_SHADOW(svp, trp)) 7672 tvp = RTOV4(trp); 7673 vnevent_link(tvp, ct); 7674 } 7675 out: 7676 kmem_free(argop, argoplist_size); 7677 if (resp) 7678 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7679 7680 nfs_rw_exit(&tdrp->r_rwlock); 7681 7682 return (e.error); 7683 } 7684 7685 /* ARGSUSED */ 7686 static int 7687 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7688 caller_context_t *ct, int flags) 7689 { 7690 vnode_t *realvp; 7691 7692 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7693 return (EPERM); 7694 if (VOP_REALVP(ndvp, &realvp, ct) == 0) 7695 ndvp = realvp; 7696 7697 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct)); 7698 } 7699 7700 /* 7701 * nfs4rename does the real work of renaming in NFS Version 4. 7702 * 7703 * A file handle is considered volatile for renaming purposes if either 7704 * of the volatile bits are turned on. However, the compound may differ 7705 * based on the likelihood of the filehandle to change during rename. 7706 */ 7707 static int 7708 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7709 caller_context_t *ct) 7710 { 7711 int error; 7712 mntinfo4_t *mi; 7713 vnode_t *nvp = NULL; 7714 vnode_t *ovp = NULL; 7715 char *tmpname = NULL; 7716 rnode4_t *rp; 7717 rnode4_t *odrp; 7718 rnode4_t *ndrp; 7719 int did_link = 0; 7720 int do_link = 1; 7721 nfsstat4 stat = NFS4_OK; 7722 7723 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7724 ASSERT(nfs4_consistent_type(odvp)); 7725 ASSERT(nfs4_consistent_type(ndvp)); 7726 7727 if (onm[0] == '.' && (onm[1] == '\0' || 7728 (onm[1] == '.' && onm[2] == '\0'))) 7729 return (EINVAL); 7730 7731 if (nnm[0] == '.' && (nnm[1] == '\0' || 7732 (nnm[1] == '.' && nnm[2] == '\0'))) 7733 return (EINVAL); 7734 7735 odrp = VTOR4(odvp); 7736 ndrp = VTOR4(ndvp); 7737 if ((intptr_t)odrp < (intptr_t)ndrp) { 7738 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7739 return (EINTR); 7740 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7741 nfs_rw_exit(&odrp->r_rwlock); 7742 return (EINTR); 7743 } 7744 } else { 7745 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7746 return (EINTR); 7747 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7748 nfs_rw_exit(&ndrp->r_rwlock); 7749 return (EINTR); 7750 } 7751 } 7752 7753 /* 7754 * Lookup the target file. If it exists, it needs to be 7755 * checked to see whether it is a mount point and whether 7756 * it is active (open). 7757 */ 7758 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7759 if (!error) { 7760 int isactive; 7761 7762 ASSERT(nfs4_consistent_type(nvp)); 7763 /* 7764 * If this file has been mounted on, then just 7765 * return busy because renaming to it would remove 7766 * the mounted file system from the name space. 7767 */ 7768 if (vn_ismntpt(nvp)) { 7769 VN_RELE(nvp); 7770 nfs_rw_exit(&odrp->r_rwlock); 7771 nfs_rw_exit(&ndrp->r_rwlock); 7772 return (EBUSY); 7773 } 7774 7775 /* 7776 * First just remove the entry from the name cache, as it 7777 * is most likely the only entry for this vp. 7778 */ 7779 dnlc_remove(ndvp, nnm); 7780 7781 rp = VTOR4(nvp); 7782 7783 if (nvp->v_type != VREG) { 7784 /* 7785 * Purge the name cache of all references to this vnode 7786 * so that we can check the reference count to infer 7787 * whether it is active or not. 7788 */ 7789 if (nvp->v_count > 1) 7790 dnlc_purge_vp(nvp); 7791 7792 isactive = nvp->v_count > 1; 7793 } else { 7794 mutex_enter(&rp->r_os_lock); 7795 isactive = list_head(&rp->r_open_streams) != NULL; 7796 mutex_exit(&rp->r_os_lock); 7797 } 7798 7799 /* 7800 * If the vnode is active and is not a directory, 7801 * arrange to rename it to a 7802 * temporary file so that it will continue to be 7803 * accessible. This implements the "unlink-open-file" 7804 * semantics for the target of a rename operation. 7805 * Before doing this though, make sure that the 7806 * source and target files are not already the same. 7807 */ 7808 if (isactive && nvp->v_type != VDIR) { 7809 /* 7810 * Lookup the source name. 7811 */ 7812 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7813 7814 /* 7815 * The source name *should* already exist. 7816 */ 7817 if (error) { 7818 VN_RELE(nvp); 7819 nfs_rw_exit(&odrp->r_rwlock); 7820 nfs_rw_exit(&ndrp->r_rwlock); 7821 return (error); 7822 } 7823 7824 ASSERT(nfs4_consistent_type(ovp)); 7825 7826 /* 7827 * Compare the two vnodes. If they are the same, 7828 * just release all held vnodes and return success. 7829 */ 7830 if (VN_CMP(ovp, nvp)) { 7831 VN_RELE(ovp); 7832 VN_RELE(nvp); 7833 nfs_rw_exit(&odrp->r_rwlock); 7834 nfs_rw_exit(&ndrp->r_rwlock); 7835 return (0); 7836 } 7837 7838 /* 7839 * Can't mix and match directories and non- 7840 * directories in rename operations. We already 7841 * know that the target is not a directory. If 7842 * the source is a directory, return an error. 7843 */ 7844 if (ovp->v_type == VDIR) { 7845 VN_RELE(ovp); 7846 VN_RELE(nvp); 7847 nfs_rw_exit(&odrp->r_rwlock); 7848 nfs_rw_exit(&ndrp->r_rwlock); 7849 return (ENOTDIR); 7850 } 7851 link_call: 7852 /* 7853 * The target file exists, is not the same as 7854 * the source file, and is active. We first 7855 * try to Link it to a temporary filename to 7856 * avoid having the server removing the file 7857 * completely (which could cause data loss to 7858 * the user's POV in the event the Rename fails 7859 * -- see bug 1165874). 7860 */ 7861 /* 7862 * The do_link and did_link booleans are 7863 * introduced in the event we get NFS4ERR_FILE_OPEN 7864 * returned for the Rename. Some servers can 7865 * not Rename over an Open file, so they return 7866 * this error. The client needs to Remove the 7867 * newly created Link and do two Renames, just 7868 * as if the server didn't support LINK. 7869 */ 7870 tmpname = newname(); 7871 error = 0; 7872 7873 if (do_link) { 7874 error = nfs4_link(ndvp, nvp, tmpname, cr, 7875 NULL, 0); 7876 } 7877 if (error == EOPNOTSUPP || !do_link) { 7878 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7879 cr, NULL, 0); 7880 did_link = 0; 7881 } else { 7882 did_link = 1; 7883 } 7884 if (error) { 7885 kmem_free(tmpname, MAXNAMELEN); 7886 VN_RELE(ovp); 7887 VN_RELE(nvp); 7888 nfs_rw_exit(&odrp->r_rwlock); 7889 nfs_rw_exit(&ndrp->r_rwlock); 7890 return (error); 7891 } 7892 7893 mutex_enter(&rp->r_statelock); 7894 if (rp->r_unldvp == NULL) { 7895 VN_HOLD(ndvp); 7896 rp->r_unldvp = ndvp; 7897 if (rp->r_unlcred != NULL) 7898 crfree(rp->r_unlcred); 7899 crhold(cr); 7900 rp->r_unlcred = cr; 7901 rp->r_unlname = tmpname; 7902 } else { 7903 if (rp->r_unlname) 7904 kmem_free(rp->r_unlname, MAXNAMELEN); 7905 rp->r_unlname = tmpname; 7906 } 7907 mutex_exit(&rp->r_statelock); 7908 } 7909 7910 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7911 7912 ASSERT(nfs4_consistent_type(nvp)); 7913 } 7914 7915 if (ovp == NULL) { 7916 /* 7917 * When renaming directories to be a subdirectory of a 7918 * different parent, the dnlc entry for ".." will no 7919 * longer be valid, so it must be removed. 7920 * 7921 * We do a lookup here to determine whether we are renaming 7922 * a directory and we need to check if we are renaming 7923 * an unlinked file. This might have already been done 7924 * in previous code, so we check ovp == NULL to avoid 7925 * doing it twice. 7926 */ 7927 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7928 /* 7929 * The source name *should* already exist. 7930 */ 7931 if (error) { 7932 nfs_rw_exit(&odrp->r_rwlock); 7933 nfs_rw_exit(&ndrp->r_rwlock); 7934 if (nvp) { 7935 VN_RELE(nvp); 7936 } 7937 return (error); 7938 } 7939 ASSERT(ovp != NULL); 7940 ASSERT(nfs4_consistent_type(ovp)); 7941 } 7942 7943 /* 7944 * Is the object being renamed a dir, and if so, is 7945 * it being renamed to a child of itself? The underlying 7946 * fs should ultimately return EINVAL for this case; 7947 * however, buggy beta non-Solaris NFSv4 servers at 7948 * interop testing events have allowed this behavior, 7949 * and it caused our client to panic due to a recursive 7950 * mutex_enter in fn_move. 7951 * 7952 * The tedious locking in fn_move could be changed to 7953 * deal with this case, and the client could avoid the 7954 * panic; however, the client would just confuse itself 7955 * later and misbehave. A better way to handle the broken 7956 * server is to detect this condition and return EINVAL 7957 * without ever sending the the bogus rename to the server. 7958 * We know the rename is invalid -- just fail it now. 7959 */ 7960 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7961 VN_RELE(ovp); 7962 nfs_rw_exit(&odrp->r_rwlock); 7963 nfs_rw_exit(&ndrp->r_rwlock); 7964 if (nvp) { 7965 VN_RELE(nvp); 7966 } 7967 return (EINVAL); 7968 } 7969 7970 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7971 7972 /* 7973 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7974 * possible for the filehandle to change due to the rename. 7975 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7976 * the fh will not change because of the rename, but we still need 7977 * to update its rnode entry with the new name for 7978 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7979 * has no effect on these for now, but for future improvements, 7980 * we might want to use it too to simplify handling of files 7981 * that are open with that flag on. (XXX) 7982 */ 7983 mi = VTOMI4(odvp); 7984 if (NFS4_VOLATILE_FH(mi)) 7985 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7986 &stat); 7987 else 7988 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7989 &stat); 7990 7991 ASSERT(nfs4_consistent_type(odvp)); 7992 ASSERT(nfs4_consistent_type(ndvp)); 7993 ASSERT(nfs4_consistent_type(ovp)); 7994 7995 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7996 do_link = 0; 7997 /* 7998 * Before the 'link_call' code, we did a nfs4_lookup 7999 * that puts a VN_HOLD on nvp. After the nfs4_link 8000 * call we call VN_RELE to match that hold. We need 8001 * to place an additional VN_HOLD here since we will 8002 * be hitting that VN_RELE again. 8003 */ 8004 VN_HOLD(nvp); 8005 8006 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0); 8007 8008 /* Undo the unlinked file naming stuff we just did */ 8009 mutex_enter(&rp->r_statelock); 8010 if (rp->r_unldvp) { 8011 VN_RELE(ndvp); 8012 rp->r_unldvp = NULL; 8013 if (rp->r_unlcred != NULL) 8014 crfree(rp->r_unlcred); 8015 rp->r_unlcred = NULL; 8016 /* rp->r_unlanme points to tmpname */ 8017 if (rp->r_unlname) 8018 kmem_free(rp->r_unlname, MAXNAMELEN); 8019 rp->r_unlname = NULL; 8020 } 8021 mutex_exit(&rp->r_statelock); 8022 8023 if (nvp) { 8024 VN_RELE(nvp); 8025 } 8026 goto link_call; 8027 } 8028 8029 if (error) { 8030 VN_RELE(ovp); 8031 nfs_rw_exit(&odrp->r_rwlock); 8032 nfs_rw_exit(&ndrp->r_rwlock); 8033 if (nvp) { 8034 VN_RELE(nvp); 8035 } 8036 return (error); 8037 } 8038 8039 /* 8040 * when renaming directories to be a subdirectory of a 8041 * different parent, the dnlc entry for ".." will no 8042 * longer be valid, so it must be removed 8043 */ 8044 rp = VTOR4(ovp); 8045 if (ndvp != odvp) { 8046 if (ovp->v_type == VDIR) { 8047 dnlc_remove(ovp, ".."); 8048 if (rp->r_dir != NULL) 8049 nfs4_purge_rddir_cache(ovp); 8050 } 8051 } 8052 8053 /* 8054 * If we are renaming the unlinked file, update the 8055 * r_unldvp and r_unlname as needed. 8056 */ 8057 mutex_enter(&rp->r_statelock); 8058 if (rp->r_unldvp != NULL) { 8059 if (strcmp(rp->r_unlname, onm) == 0) { 8060 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 8061 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 8062 if (ndvp != rp->r_unldvp) { 8063 VN_RELE(rp->r_unldvp); 8064 rp->r_unldvp = ndvp; 8065 VN_HOLD(ndvp); 8066 } 8067 } 8068 } 8069 mutex_exit(&rp->r_statelock); 8070 8071 /* 8072 * Notify the rename vnevents to source vnode, and to the target 8073 * vnode if it already existed. 8074 */ 8075 if (error == 0) { 8076 vnode_t *tvp; 8077 rnode4_t *trp; 8078 /* 8079 * Notify the vnode. Each links is represented by 8080 * a different vnode, in nfsv4. 8081 */ 8082 if (nvp) { 8083 trp = VTOR4(nvp); 8084 tvp = nvp; 8085 if (IS_SHADOW(nvp, trp)) 8086 tvp = RTOV4(trp); 8087 vnevent_rename_dest(tvp, ndvp, nnm, ct); 8088 } 8089 8090 /* 8091 * if the source and destination directory are not the 8092 * same notify the destination directory. 8093 */ 8094 if (VTOR4(odvp) != VTOR4(ndvp)) { 8095 trp = VTOR4(ndvp); 8096 tvp = ndvp; 8097 if (IS_SHADOW(ndvp, trp)) 8098 tvp = RTOV4(trp); 8099 vnevent_rename_dest_dir(tvp, ct); 8100 } 8101 8102 trp = VTOR4(ovp); 8103 tvp = ovp; 8104 if (IS_SHADOW(ovp, trp)) 8105 tvp = RTOV4(trp); 8106 vnevent_rename_src(tvp, odvp, onm, ct); 8107 } 8108 8109 if (nvp) { 8110 VN_RELE(nvp); 8111 } 8112 VN_RELE(ovp); 8113 8114 nfs_rw_exit(&odrp->r_rwlock); 8115 nfs_rw_exit(&ndrp->r_rwlock); 8116 8117 return (error); 8118 } 8119 8120 /* 8121 * When the parent directory has changed, sv_dfh must be updated 8122 */ 8123 static void 8124 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp) 8125 { 8126 svnode_t *sv = VTOSV(vp); 8127 nfs4_sharedfh_t *old_dfh = sv->sv_dfh; 8128 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh; 8129 8130 sfh4_hold(new_dfh); 8131 sv->sv_dfh = new_dfh; 8132 sfh4_rele(&old_dfh); 8133 } 8134 8135 /* 8136 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 8137 * when it is known that the filehandle is persistent through rename. 8138 * 8139 * Rename requires that the current fh be the target directory and the 8140 * saved fh be the source directory. After the operation, the current fh 8141 * is unchanged. 8142 * The compound op structure for persistent fh rename is: 8143 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 8144 * Rather than bother with the directory postop args, we'll simply 8145 * update that a change occurred in the cache, so no post-op getattrs. 8146 */ 8147 static int 8148 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 8149 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8150 { 8151 COMPOUND4args_clnt args; 8152 COMPOUND4res_clnt res, *resp = NULL; 8153 nfs_argop4 *argop; 8154 nfs_resop4 *resop; 8155 int doqueue, argoplist_size; 8156 mntinfo4_t *mi; 8157 rnode4_t *odrp = VTOR4(odvp); 8158 rnode4_t *ndrp = VTOR4(ndvp); 8159 RENAME4res *rn_res; 8160 bool_t needrecov; 8161 nfs4_recov_state_t recov_state; 8162 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8163 dirattr_info_t dinfo, *dinfop; 8164 8165 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8166 8167 recov_state.rs_flags = 0; 8168 recov_state.rs_num_retry_despite_err = 0; 8169 8170 /* 8171 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 8172 * 8173 * If source/target are different dirs, then append putfh(src); getattr 8174 */ 8175 args.array_len = (odvp == ndvp) ? 5 : 7; 8176 argoplist_size = args.array_len * sizeof (nfs_argop4); 8177 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8178 8179 recov_retry: 8180 *statp = NFS4_OK; 8181 8182 /* No need to Lookup the file, persistent fh */ 8183 args.ctag = TAG_RENAME; 8184 8185 mi = VTOMI4(odvp); 8186 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8187 if (e.error) { 8188 kmem_free(argop, argoplist_size); 8189 return (e.error); 8190 } 8191 8192 /* 0: putfh source directory */ 8193 argop[0].argop = OP_CPUTFH; 8194 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8195 8196 /* 1: Save source fh to free up current for target */ 8197 argop[1].argop = OP_SAVEFH; 8198 8199 /* 2: putfh targetdir */ 8200 argop[2].argop = OP_CPUTFH; 8201 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8202 8203 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8204 argop[3].argop = OP_CRENAME; 8205 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8206 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8207 8208 /* 4: getattr (targetdir) */ 8209 argop[4].argop = OP_GETATTR; 8210 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8211 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8212 8213 if (ndvp != odvp) { 8214 8215 /* 5: putfh (sourcedir) */ 8216 argop[5].argop = OP_CPUTFH; 8217 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8218 8219 /* 6: getattr (sourcedir) */ 8220 argop[6].argop = OP_GETATTR; 8221 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8222 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8223 } 8224 8225 dnlc_remove(odvp, onm); 8226 dnlc_remove(ndvp, nnm); 8227 8228 doqueue = 1; 8229 dinfo.di_time_call = gethrtime(); 8230 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8231 8232 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8233 if (e.error) { 8234 PURGE_ATTRCACHE4(odvp); 8235 PURGE_ATTRCACHE4(ndvp); 8236 } else { 8237 *statp = res.status; 8238 } 8239 8240 if (needrecov) { 8241 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8242 OP_RENAME, NULL, NULL, NULL) == FALSE) { 8243 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8244 if (!e.error) 8245 (void) xdr_free(xdr_COMPOUND4res_clnt, 8246 (caddr_t)&res); 8247 goto recov_retry; 8248 } 8249 } 8250 8251 if (!e.error) { 8252 resp = &res; 8253 /* 8254 * as long as OP_RENAME 8255 */ 8256 if (res.status != NFS4_OK && res.array_len <= 4) { 8257 e.error = geterrno4(res.status); 8258 PURGE_ATTRCACHE4(odvp); 8259 PURGE_ATTRCACHE4(ndvp); 8260 /* 8261 * System V defines rename to return EEXIST, not 8262 * ENOTEMPTY if the target directory is not empty. 8263 * Over the wire, the error is NFSERR_ENOTEMPTY 8264 * which geterrno4 maps to ENOTEMPTY. 8265 */ 8266 if (e.error == ENOTEMPTY) 8267 e.error = EEXIST; 8268 } else { 8269 8270 resop = &res.array[3]; /* rename res */ 8271 rn_res = &resop->nfs_resop4_u.oprename; 8272 8273 if (res.status == NFS4_OK) { 8274 /* 8275 * Update target attribute, readdir and dnlc 8276 * caches. 8277 */ 8278 dinfo.di_garp = 8279 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8280 dinfo.di_cred = cr; 8281 dinfop = &dinfo; 8282 } else 8283 dinfop = NULL; 8284 8285 nfs4_update_dircaches(&rn_res->target_cinfo, 8286 ndvp, NULL, NULL, dinfop); 8287 8288 /* 8289 * Update source attribute, readdir and dnlc caches 8290 * 8291 */ 8292 if (ndvp != odvp) { 8293 update_parentdir_sfh(renvp, ndvp); 8294 8295 if (dinfop) 8296 dinfo.di_garp = 8297 &(res.array[6].nfs_resop4_u. 8298 opgetattr.ga_res); 8299 8300 nfs4_update_dircaches(&rn_res->source_cinfo, 8301 odvp, NULL, NULL, dinfop); 8302 } 8303 8304 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8305 nnm); 8306 } 8307 } 8308 8309 if (resp) 8310 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8311 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8312 kmem_free(argop, argoplist_size); 8313 8314 return (e.error); 8315 } 8316 8317 /* 8318 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8319 * it is possible for the filehandle to change due to the rename. 8320 * 8321 * The compound req in this case includes a post-rename lookup and getattr 8322 * to ensure that we have the correct fh and attributes for the object. 8323 * 8324 * Rename requires that the current fh be the target directory and the 8325 * saved fh be the source directory. After the operation, the current fh 8326 * is unchanged. 8327 * 8328 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8329 * update the filehandle for the renamed object. We also get the old 8330 * filehandle for historical reasons; this should be taken out sometime. 8331 * This results in a rather cumbersome compound... 8332 * 8333 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8334 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8335 * 8336 */ 8337 static int 8338 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8339 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8340 { 8341 COMPOUND4args_clnt args; 8342 COMPOUND4res_clnt res, *resp = NULL; 8343 int argoplist_size; 8344 nfs_argop4 *argop; 8345 nfs_resop4 *resop; 8346 int doqueue; 8347 mntinfo4_t *mi; 8348 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8349 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8350 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8351 RENAME4res *rn_res; 8352 GETFH4res *ngf_res; 8353 bool_t needrecov; 8354 nfs4_recov_state_t recov_state; 8355 hrtime_t t; 8356 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8357 dirattr_info_t dinfo, *dinfop = &dinfo; 8358 8359 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8360 8361 recov_state.rs_flags = 0; 8362 recov_state.rs_num_retry_despite_err = 0; 8363 8364 recov_retry: 8365 *statp = NFS4_OK; 8366 8367 /* 8368 * There is a window between the RPC and updating the path and 8369 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8370 * code, so that it doesn't try to use the old path during that 8371 * window. 8372 */ 8373 mutex_enter(&orp->r_statelock); 8374 while (orp->r_flags & R4RECEXPFH) { 8375 klwp_t *lwp = ttolwp(curthread); 8376 8377 if (lwp != NULL) 8378 lwp->lwp_nostop++; 8379 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8380 mutex_exit(&orp->r_statelock); 8381 if (lwp != NULL) 8382 lwp->lwp_nostop--; 8383 return (EINTR); 8384 } 8385 if (lwp != NULL) 8386 lwp->lwp_nostop--; 8387 } 8388 orp->r_flags |= R4RECEXPFH; 8389 mutex_exit(&orp->r_statelock); 8390 8391 mi = VTOMI4(odvp); 8392 8393 args.ctag = TAG_RENAME_VFH; 8394 args.array_len = (odvp == ndvp) ? 10 : 12; 8395 argoplist_size = args.array_len * sizeof (nfs_argop4); 8396 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8397 8398 /* 8399 * Rename ops: 8400 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8401 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8402 * LOOKUP(trgt), GETFH(new), GETATTR, 8403 * 8404 * if (odvp != ndvp) 8405 * add putfh(sourcedir), getattr(sourcedir) } 8406 */ 8407 args.array = argop; 8408 8409 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8410 &recov_state, NULL); 8411 if (e.error) { 8412 kmem_free(argop, argoplist_size); 8413 mutex_enter(&orp->r_statelock); 8414 orp->r_flags &= ~R4RECEXPFH; 8415 cv_broadcast(&orp->r_cv); 8416 mutex_exit(&orp->r_statelock); 8417 return (e.error); 8418 } 8419 8420 /* 0: putfh source directory */ 8421 argop[0].argop = OP_CPUTFH; 8422 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8423 8424 /* 1: Save source fh to free up current for target */ 8425 argop[1].argop = OP_SAVEFH; 8426 8427 /* 2: Lookup pre-rename fh of renamed object */ 8428 argop[2].argop = OP_CLOOKUP; 8429 argop[2].nfs_argop4_u.opclookup.cname = onm; 8430 8431 /* 3: getfh fh of renamed object (before rename) */ 8432 argop[3].argop = OP_GETFH; 8433 8434 /* 4: putfh targetdir */ 8435 argop[4].argop = OP_CPUTFH; 8436 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8437 8438 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8439 argop[5].argop = OP_CRENAME; 8440 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8441 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8442 8443 /* 6: getattr of target dir (post op attrs) */ 8444 argop[6].argop = OP_GETATTR; 8445 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8446 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8447 8448 /* 7: Lookup post-rename fh of renamed object */ 8449 argop[7].argop = OP_CLOOKUP; 8450 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8451 8452 /* 8: getfh fh of renamed object (after rename) */ 8453 argop[8].argop = OP_GETFH; 8454 8455 /* 9: getattr of renamed object */ 8456 argop[9].argop = OP_GETATTR; 8457 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8458 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8459 8460 /* 8461 * If source/target dirs are different, then get new post-op 8462 * attrs for source dir also. 8463 */ 8464 if (ndvp != odvp) { 8465 /* 10: putfh (sourcedir) */ 8466 argop[10].argop = OP_CPUTFH; 8467 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8468 8469 /* 11: getattr (sourcedir) */ 8470 argop[11].argop = OP_GETATTR; 8471 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8472 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8473 } 8474 8475 dnlc_remove(odvp, onm); 8476 dnlc_remove(ndvp, nnm); 8477 8478 doqueue = 1; 8479 t = gethrtime(); 8480 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8481 8482 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8483 if (e.error) { 8484 PURGE_ATTRCACHE4(odvp); 8485 PURGE_ATTRCACHE4(ndvp); 8486 if (!needrecov) { 8487 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8488 &recov_state, needrecov); 8489 goto out; 8490 } 8491 } else { 8492 *statp = res.status; 8493 } 8494 8495 if (needrecov) { 8496 bool_t abort; 8497 8498 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8499 OP_RENAME, NULL, NULL, NULL); 8500 if (abort == FALSE) { 8501 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8502 &recov_state, needrecov); 8503 kmem_free(argop, argoplist_size); 8504 if (!e.error) 8505 (void) xdr_free(xdr_COMPOUND4res_clnt, 8506 (caddr_t)&res); 8507 mutex_enter(&orp->r_statelock); 8508 orp->r_flags &= ~R4RECEXPFH; 8509 cv_broadcast(&orp->r_cv); 8510 mutex_exit(&orp->r_statelock); 8511 goto recov_retry; 8512 } else { 8513 if (e.error != 0) { 8514 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8515 &recov_state, needrecov); 8516 goto out; 8517 } 8518 /* fall through for res.status case */ 8519 } 8520 } 8521 8522 resp = &res; 8523 /* 8524 * If OP_RENAME (or any prev op) failed, then return an error. 8525 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8526 */ 8527 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8528 /* 8529 * Error in an op other than last Getattr 8530 */ 8531 e.error = geterrno4(res.status); 8532 PURGE_ATTRCACHE4(odvp); 8533 PURGE_ATTRCACHE4(ndvp); 8534 /* 8535 * System V defines rename to return EEXIST, not 8536 * ENOTEMPTY if the target directory is not empty. 8537 * Over the wire, the error is NFSERR_ENOTEMPTY 8538 * which geterrno4 maps to ENOTEMPTY. 8539 */ 8540 if (e.error == ENOTEMPTY) 8541 e.error = EEXIST; 8542 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8543 needrecov); 8544 goto out; 8545 } 8546 8547 /* rename results */ 8548 rn_res = &res.array[5].nfs_resop4_u.oprename; 8549 8550 if (res.status == NFS4_OK) { 8551 /* Update target attribute, readdir and dnlc caches */ 8552 dinfo.di_garp = 8553 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8554 dinfo.di_cred = cr; 8555 dinfo.di_time_call = t; 8556 } else 8557 dinfop = NULL; 8558 8559 /* Update source cache attribute, readdir and dnlc caches */ 8560 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8561 8562 /* Update source cache attribute, readdir and dnlc caches */ 8563 if (ndvp != odvp) { 8564 update_parentdir_sfh(ovp, ndvp); 8565 8566 /* 8567 * If dinfop is non-NULL, then compound succeded, so 8568 * set di_garp to attrs for source dir. dinfop is only 8569 * set to NULL when compound fails. 8570 */ 8571 if (dinfop) 8572 dinfo.di_garp = 8573 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8574 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8575 dinfop); 8576 } 8577 8578 /* 8579 * Update the rnode with the new component name and args, 8580 * and if the file handle changed, also update it with the new fh. 8581 * This is only necessary if the target object has an rnode 8582 * entry and there is no need to create one for it. 8583 */ 8584 resop = &res.array[8]; /* getfh new res */ 8585 ngf_res = &resop->nfs_resop4_u.opgetfh; 8586 8587 /* 8588 * Update the path and filehandle for the renamed object. 8589 */ 8590 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8591 8592 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8593 8594 if (res.status == NFS4_OK) { 8595 resop++; /* getattr res */ 8596 e.error = nfs4_update_attrcache(res.status, 8597 &resop->nfs_resop4_u.opgetattr.ga_res, 8598 t, ovp, cr); 8599 } 8600 8601 out: 8602 kmem_free(argop, argoplist_size); 8603 if (resp) 8604 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8605 mutex_enter(&orp->r_statelock); 8606 orp->r_flags &= ~R4RECEXPFH; 8607 cv_broadcast(&orp->r_cv); 8608 mutex_exit(&orp->r_statelock); 8609 8610 return (e.error); 8611 } 8612 8613 /* ARGSUSED */ 8614 static int 8615 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr, 8616 caller_context_t *ct, int flags, vsecattr_t *vsecp) 8617 { 8618 int error; 8619 vnode_t *vp; 8620 8621 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8622 return (EPERM); 8623 /* 8624 * As ".." has special meaning and rather than send a mkdir 8625 * over the wire to just let the server freak out, we just 8626 * short circuit it here and return EEXIST 8627 */ 8628 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8629 return (EEXIST); 8630 8631 /* 8632 * Decision to get the right gid and setgid bit of the 8633 * new directory is now made in call_nfs4_create_req. 8634 */ 8635 va->va_mask |= AT_MODE; 8636 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8637 if (error) 8638 return (error); 8639 8640 *vpp = vp; 8641 return (0); 8642 } 8643 8644 8645 /* 8646 * rmdir is using the same remove v4 op as does remove. 8647 * Remove requires that the current fh be the target directory. 8648 * After the operation, the current fh is unchanged. 8649 * The compound op structure is: 8650 * PUTFH(targetdir), REMOVE 8651 */ 8652 /*ARGSUSED4*/ 8653 static int 8654 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr, 8655 caller_context_t *ct, int flags) 8656 { 8657 int need_end_op = FALSE; 8658 COMPOUND4args_clnt args; 8659 COMPOUND4res_clnt res, *resp = NULL; 8660 REMOVE4res *rm_res; 8661 nfs_argop4 argop[3]; 8662 nfs_resop4 *resop; 8663 vnode_t *vp; 8664 int doqueue; 8665 mntinfo4_t *mi; 8666 rnode4_t *drp; 8667 bool_t needrecov = FALSE; 8668 nfs4_recov_state_t recov_state; 8669 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8670 dirattr_info_t dinfo, *dinfop; 8671 8672 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8673 return (EPERM); 8674 /* 8675 * As ".." has special meaning and rather than send a rmdir 8676 * over the wire to just let the server freak out, we just 8677 * short circuit it here and return EEXIST 8678 */ 8679 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8680 return (EEXIST); 8681 8682 drp = VTOR4(dvp); 8683 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8684 return (EINTR); 8685 8686 /* 8687 * Attempt to prevent a rmdir(".") from succeeding. 8688 */ 8689 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8690 if (e.error) { 8691 nfs_rw_exit(&drp->r_rwlock); 8692 return (e.error); 8693 } 8694 if (vp == cdir) { 8695 VN_RELE(vp); 8696 nfs_rw_exit(&drp->r_rwlock); 8697 return (EINVAL); 8698 } 8699 8700 /* 8701 * Since nfsv4 remove op works on both files and directories, 8702 * check that the removed object is indeed a directory. 8703 */ 8704 if (vp->v_type != VDIR) { 8705 VN_RELE(vp); 8706 nfs_rw_exit(&drp->r_rwlock); 8707 return (ENOTDIR); 8708 } 8709 8710 /* 8711 * First just remove the entry from the name cache, as it 8712 * is most likely an entry for this vp. 8713 */ 8714 dnlc_remove(dvp, nm); 8715 8716 /* 8717 * If there vnode reference count is greater than one, then 8718 * there may be additional references in the DNLC which will 8719 * need to be purged. First, trying removing the entry for 8720 * the parent directory and see if that removes the additional 8721 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8722 * to completely remove any references to the directory which 8723 * might still exist in the DNLC. 8724 */ 8725 if (vp->v_count > 1) { 8726 dnlc_remove(vp, ".."); 8727 if (vp->v_count > 1) 8728 dnlc_purge_vp(vp); 8729 } 8730 8731 mi = VTOMI4(dvp); 8732 recov_state.rs_flags = 0; 8733 recov_state.rs_num_retry_despite_err = 0; 8734 8735 recov_retry: 8736 args.ctag = TAG_RMDIR; 8737 8738 /* 8739 * Rmdir ops: putfh dir; remove 8740 */ 8741 args.array_len = 3; 8742 args.array = argop; 8743 8744 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8745 if (e.error) { 8746 nfs_rw_exit(&drp->r_rwlock); 8747 return (e.error); 8748 } 8749 need_end_op = TRUE; 8750 8751 /* putfh directory */ 8752 argop[0].argop = OP_CPUTFH; 8753 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8754 8755 /* remove */ 8756 argop[1].argop = OP_CREMOVE; 8757 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8758 8759 /* getattr (postop attrs for dir that contained removed dir) */ 8760 argop[2].argop = OP_GETATTR; 8761 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8762 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8763 8764 dinfo.di_time_call = gethrtime(); 8765 doqueue = 1; 8766 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8767 8768 PURGE_ATTRCACHE4(vp); 8769 8770 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8771 if (e.error) { 8772 PURGE_ATTRCACHE4(dvp); 8773 } 8774 8775 if (needrecov) { 8776 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8777 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 8778 if (!e.error) 8779 (void) xdr_free(xdr_COMPOUND4res_clnt, 8780 (caddr_t)&res); 8781 8782 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8783 needrecov); 8784 need_end_op = FALSE; 8785 goto recov_retry; 8786 } 8787 } 8788 8789 if (!e.error) { 8790 resp = &res; 8791 8792 /* 8793 * Only return error if first 2 ops (OP_REMOVE or earlier) 8794 * failed. 8795 */ 8796 if (res.status != NFS4_OK && res.array_len <= 2) { 8797 e.error = geterrno4(res.status); 8798 PURGE_ATTRCACHE4(dvp); 8799 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8800 &recov_state, needrecov); 8801 need_end_op = FALSE; 8802 nfs4_purge_stale_fh(e.error, dvp, cr); 8803 /* 8804 * System V defines rmdir to return EEXIST, not 8805 * ENOTEMPTY if the directory is not empty. Over 8806 * the wire, the error is NFSERR_ENOTEMPTY which 8807 * geterrno4 maps to ENOTEMPTY. 8808 */ 8809 if (e.error == ENOTEMPTY) 8810 e.error = EEXIST; 8811 } else { 8812 resop = &res.array[1]; /* remove res */ 8813 rm_res = &resop->nfs_resop4_u.opremove; 8814 8815 if (res.status == NFS4_OK) { 8816 resop = &res.array[2]; /* dir attrs */ 8817 dinfo.di_garp = 8818 &resop->nfs_resop4_u.opgetattr.ga_res; 8819 dinfo.di_cred = cr; 8820 dinfop = &dinfo; 8821 } else 8822 dinfop = NULL; 8823 8824 /* Update dir attribute, readdir and dnlc caches */ 8825 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8826 dinfop); 8827 8828 /* destroy rddir cache for dir that was removed */ 8829 if (VTOR4(vp)->r_dir != NULL) 8830 nfs4_purge_rddir_cache(vp); 8831 } 8832 } 8833 8834 if (need_end_op) 8835 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8836 8837 nfs_rw_exit(&drp->r_rwlock); 8838 8839 if (resp) 8840 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8841 8842 if (e.error == 0) { 8843 vnode_t *tvp; 8844 rnode4_t *trp; 8845 trp = VTOR4(vp); 8846 tvp = vp; 8847 if (IS_SHADOW(vp, trp)) 8848 tvp = RTOV4(trp); 8849 vnevent_rmdir(tvp, dvp, nm, ct); 8850 } 8851 8852 VN_RELE(vp); 8853 8854 return (e.error); 8855 } 8856 8857 /* ARGSUSED */ 8858 static int 8859 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr, 8860 caller_context_t *ct, int flags) 8861 { 8862 int error; 8863 vnode_t *vp; 8864 rnode4_t *rp; 8865 char *contents; 8866 mntinfo4_t *mi = VTOMI4(dvp); 8867 8868 if (nfs_zone() != mi->mi_zone) 8869 return (EPERM); 8870 if (!(mi->mi_flags & MI4_SYMLINK)) 8871 return (EOPNOTSUPP); 8872 8873 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8874 if (error) 8875 return (error); 8876 8877 ASSERT(nfs4_consistent_type(vp)); 8878 rp = VTOR4(vp); 8879 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8880 8881 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8882 8883 if (contents != NULL) { 8884 mutex_enter(&rp->r_statelock); 8885 if (rp->r_symlink.contents == NULL) { 8886 rp->r_symlink.len = strlen(tnm); 8887 bcopy(tnm, contents, rp->r_symlink.len); 8888 rp->r_symlink.contents = contents; 8889 rp->r_symlink.size = MAXPATHLEN; 8890 mutex_exit(&rp->r_statelock); 8891 } else { 8892 mutex_exit(&rp->r_statelock); 8893 kmem_free((void *)contents, MAXPATHLEN); 8894 } 8895 } 8896 } 8897 VN_RELE(vp); 8898 8899 return (error); 8900 } 8901 8902 8903 /* 8904 * Read directory entries. 8905 * There are some weird things to look out for here. The uio_loffset 8906 * field is either 0 or it is the offset returned from a previous 8907 * readdir. It is an opaque value used by the server to find the 8908 * correct directory block to read. The count field is the number 8909 * of blocks to read on the server. This is advisory only, the server 8910 * may return only one block's worth of entries. Entries may be compressed 8911 * on the server. 8912 */ 8913 /* ARGSUSED */ 8914 static int 8915 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp, 8916 caller_context_t *ct, int flags) 8917 { 8918 int error; 8919 uint_t count; 8920 rnode4_t *rp; 8921 rddir4_cache *rdc; 8922 rddir4_cache *rrdc; 8923 8924 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8925 return (EIO); 8926 rp = VTOR4(vp); 8927 8928 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8929 8930 /* 8931 * Make sure that the directory cache is valid. 8932 */ 8933 if (rp->r_dir != NULL) { 8934 if (nfs_disable_rddir_cache != 0) { 8935 /* 8936 * Setting nfs_disable_rddir_cache in /etc/system 8937 * allows interoperability with servers that do not 8938 * properly update the attributes of directories. 8939 * Any cached information gets purged before an 8940 * access is made to it. 8941 */ 8942 nfs4_purge_rddir_cache(vp); 8943 } 8944 8945 error = nfs4_validate_caches(vp, cr); 8946 if (error) 8947 return (error); 8948 } 8949 8950 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8951 8952 /* 8953 * Short circuit last readdir which always returns 0 bytes. 8954 * This can be done after the directory has been read through 8955 * completely at least once. This will set r_direof which 8956 * can be used to find the value of the last cookie. 8957 */ 8958 mutex_enter(&rp->r_statelock); 8959 if (rp->r_direof != NULL && 8960 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8961 mutex_exit(&rp->r_statelock); 8962 #ifdef DEBUG 8963 nfs4_readdir_cache_shorts++; 8964 #endif 8965 if (eofp) 8966 *eofp = 1; 8967 return (0); 8968 } 8969 8970 /* 8971 * Look for a cache entry. Cache entries are identified 8972 * by the NFS cookie value and the byte count requested. 8973 */ 8974 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8975 8976 /* 8977 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8978 */ 8979 if (rdc == NULL) { 8980 mutex_exit(&rp->r_statelock); 8981 return (EINTR); 8982 } 8983 8984 /* 8985 * Check to see if we need to fill this entry in. 8986 */ 8987 if (rdc->flags & RDDIRREQ) { 8988 rdc->flags &= ~RDDIRREQ; 8989 rdc->flags |= RDDIR; 8990 mutex_exit(&rp->r_statelock); 8991 8992 /* 8993 * Do the readdir. 8994 */ 8995 nfs4readdir(vp, rdc, cr); 8996 8997 /* 8998 * Reacquire the lock, so that we can continue 8999 */ 9000 mutex_enter(&rp->r_statelock); 9001 /* 9002 * The entry is now complete 9003 */ 9004 rdc->flags &= ~RDDIR; 9005 } 9006 9007 ASSERT(!(rdc->flags & RDDIR)); 9008 9009 /* 9010 * If an error occurred while attempting 9011 * to fill the cache entry, mark the entry invalid and 9012 * just return the error. 9013 */ 9014 if (rdc->error) { 9015 error = rdc->error; 9016 rdc->flags |= RDDIRREQ; 9017 rddir4_cache_rele(rp, rdc); 9018 mutex_exit(&rp->r_statelock); 9019 return (error); 9020 } 9021 9022 /* 9023 * The cache entry is complete and good, 9024 * copyout the dirent structs to the calling 9025 * thread. 9026 */ 9027 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 9028 9029 /* 9030 * If no error occurred during the copyout, 9031 * update the offset in the uio struct to 9032 * contain the value of the next NFS 4 cookie 9033 * and set the eof value appropriately. 9034 */ 9035 if (!error) { 9036 uiop->uio_loffset = rdc->nfs4_ncookie; 9037 if (eofp) 9038 *eofp = rdc->eof; 9039 } 9040 9041 /* 9042 * Decide whether to do readahead. Don't if we 9043 * have already read to the end of directory. 9044 */ 9045 if (rdc->eof) { 9046 /* 9047 * Make the entry the direof only if it is cached 9048 */ 9049 if (rdc->flags & RDDIRCACHED) 9050 rp->r_direof = rdc; 9051 rddir4_cache_rele(rp, rdc); 9052 mutex_exit(&rp->r_statelock); 9053 return (error); 9054 } 9055 9056 /* Determine if a readdir readahead should be done */ 9057 if (!(rp->r_flags & R4LOOKUP)) { 9058 rddir4_cache_rele(rp, rdc); 9059 mutex_exit(&rp->r_statelock); 9060 return (error); 9061 } 9062 9063 /* 9064 * Now look for a readahead entry. 9065 * 9066 * Check to see whether we found an entry for the readahead. 9067 * If so, we don't need to do anything further, so free the new 9068 * entry if one was allocated. Otherwise, allocate a new entry, add 9069 * it to the cache, and then initiate an asynchronous readdir 9070 * operation to fill it. 9071 */ 9072 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 9073 9074 /* 9075 * A readdir cache entry could not be obtained for the readahead. In 9076 * this case we skip the readahead and return. 9077 */ 9078 if (rrdc == NULL) { 9079 rddir4_cache_rele(rp, rdc); 9080 mutex_exit(&rp->r_statelock); 9081 return (error); 9082 } 9083 9084 /* 9085 * Check to see if we need to fill this entry in. 9086 */ 9087 if (rrdc->flags & RDDIRREQ) { 9088 rrdc->flags &= ~RDDIRREQ; 9089 rrdc->flags |= RDDIR; 9090 rddir4_cache_rele(rp, rdc); 9091 mutex_exit(&rp->r_statelock); 9092 #ifdef DEBUG 9093 nfs4_readdir_readahead++; 9094 #endif 9095 /* 9096 * Do the readdir. 9097 */ 9098 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 9099 return (error); 9100 } 9101 9102 rddir4_cache_rele(rp, rrdc); 9103 rddir4_cache_rele(rp, rdc); 9104 mutex_exit(&rp->r_statelock); 9105 return (error); 9106 } 9107 9108 static int 9109 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9110 { 9111 int error; 9112 rnode4_t *rp; 9113 9114 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9115 9116 rp = VTOR4(vp); 9117 9118 /* 9119 * Obtain the readdir results for the caller. 9120 */ 9121 nfs4readdir(vp, rdc, cr); 9122 9123 mutex_enter(&rp->r_statelock); 9124 /* 9125 * The entry is now complete 9126 */ 9127 rdc->flags &= ~RDDIR; 9128 9129 error = rdc->error; 9130 if (error) 9131 rdc->flags |= RDDIRREQ; 9132 rddir4_cache_rele(rp, rdc); 9133 mutex_exit(&rp->r_statelock); 9134 9135 return (error); 9136 } 9137 9138 /* 9139 * Read directory entries. 9140 * There are some weird things to look out for here. The uio_loffset 9141 * field is either 0 or it is the offset returned from a previous 9142 * readdir. It is an opaque value used by the server to find the 9143 * correct directory block to read. The count field is the number 9144 * of blocks to read on the server. This is advisory only, the server 9145 * may return only one block's worth of entries. Entries may be compressed 9146 * on the server. 9147 * 9148 * Generates the following compound request: 9149 * 1. If readdir offset is zero and no dnlc entry for parent exists, 9150 * must include a Lookupp as well. In this case, send: 9151 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 9152 * 2. Otherwise just do: { Putfh <fh>; Readdir } 9153 * 9154 * Get complete attributes and filehandles for entries if this is the 9155 * first read of the directory. Otherwise, just get fileid's. 9156 */ 9157 static void 9158 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9159 { 9160 COMPOUND4args_clnt args; 9161 COMPOUND4res_clnt res; 9162 READDIR4args *rargs; 9163 READDIR4res_clnt *rd_res; 9164 bitmap4 rd_bitsval; 9165 nfs_argop4 argop[5]; 9166 nfs_resop4 *resop; 9167 rnode4_t *rp = VTOR4(vp); 9168 mntinfo4_t *mi = VTOMI4(vp); 9169 int doqueue; 9170 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 9171 vnode_t *dvp; 9172 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 9173 int num_ops, res_opcnt; 9174 bool_t needrecov = FALSE; 9175 nfs4_recov_state_t recov_state; 9176 hrtime_t t; 9177 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 9178 9179 ASSERT(nfs_zone() == mi->mi_zone); 9180 ASSERT(rdc->flags & RDDIR); 9181 ASSERT(rdc->entries == NULL); 9182 9183 /* 9184 * If rp were a stub, it should have triggered and caused 9185 * a mount for us to get this far. 9186 */ 9187 ASSERT(!RP_ISSTUB(rp)); 9188 9189 num_ops = 2; 9190 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9191 /* 9192 * Since nfsv4 readdir may not return entries for "." and "..", 9193 * the client must recreate them: 9194 * To find the correct nodeid, do the following: 9195 * For current node, get nodeid from dnlc. 9196 * - if current node is rootvp, set pnodeid to nodeid. 9197 * - else if parent is in the dnlc, get its nodeid from there. 9198 * - else add LOOKUPP+GETATTR to compound. 9199 */ 9200 nodeid = rp->r_attr.va_nodeid; 9201 if (vp->v_flag & VROOT) { 9202 pnodeid = nodeid; /* root of mount point */ 9203 } else { 9204 dvp = dnlc_lookup(vp, ".."); 9205 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9206 /* parent in dnlc cache - no need for otw */ 9207 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9208 } else { 9209 /* 9210 * parent not in dnlc cache, 9211 * do lookupp to get its id 9212 */ 9213 num_ops = 5; 9214 pnodeid = 0; /* set later by getattr parent */ 9215 } 9216 if (dvp) 9217 VN_RELE(dvp); 9218 } 9219 } 9220 recov_state.rs_flags = 0; 9221 recov_state.rs_num_retry_despite_err = 0; 9222 9223 /* Save the original mount point security flavor */ 9224 (void) save_mnt_secinfo(mi->mi_curr_serv); 9225 9226 recov_retry: 9227 args.ctag = TAG_READDIR; 9228 9229 args.array = argop; 9230 args.array_len = num_ops; 9231 9232 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9233 &recov_state, NULL)) { 9234 /* 9235 * If readdir a node that is a stub for a crossed mount point, 9236 * keep the original secinfo flavor for the current file 9237 * system, not the crossed one. 9238 */ 9239 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9240 rdc->error = e.error; 9241 return; 9242 } 9243 9244 /* 9245 * Determine which attrs to request for dirents. This code 9246 * must be protected by nfs4_start/end_fop because of r_server 9247 * (which will change during failover recovery). 9248 * 9249 */ 9250 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9251 /* 9252 * Get all vattr attrs plus filehandle and rdattr_error 9253 */ 9254 rd_bitsval = NFS4_VATTR_MASK | 9255 FATTR4_RDATTR_ERROR_MASK | 9256 FATTR4_FILEHANDLE_MASK; 9257 9258 if (rp->r_flags & R4READDIRWATTR) { 9259 mutex_enter(&rp->r_statelock); 9260 rp->r_flags &= ~R4READDIRWATTR; 9261 mutex_exit(&rp->r_statelock); 9262 } 9263 } else { 9264 servinfo4_t *svp = rp->r_server; 9265 9266 /* 9267 * Already read directory. Use readdir with 9268 * no attrs (except for mounted_on_fileid) for updates. 9269 */ 9270 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9271 9272 /* 9273 * request mounted on fileid if supported, else request 9274 * fileid. maybe we should verify that fileid is supported 9275 * and request something else if not. 9276 */ 9277 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9278 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9279 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9280 nfs_rw_exit(&svp->sv_lock); 9281 } 9282 9283 /* putfh directory fh */ 9284 argop[0].argop = OP_CPUTFH; 9285 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9286 9287 argop[1].argop = OP_READDIR; 9288 rargs = &argop[1].nfs_argop4_u.opreaddir; 9289 /* 9290 * 1 and 2 are reserved for client "." and ".." entry offset. 9291 * cookie 0 should be used over-the-wire to start reading at 9292 * the beginning of the directory excluding "." and "..". 9293 */ 9294 if (rdc->nfs4_cookie == 0 || 9295 rdc->nfs4_cookie == 1 || 9296 rdc->nfs4_cookie == 2) { 9297 rargs->cookie = (nfs_cookie4)0; 9298 rargs->cookieverf = 0; 9299 } else { 9300 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9301 mutex_enter(&rp->r_statelock); 9302 rargs->cookieverf = rp->r_cookieverf4; 9303 mutex_exit(&rp->r_statelock); 9304 } 9305 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9306 rargs->maxcount = mi->mi_tsize; 9307 rargs->attr_request = rd_bitsval; 9308 rargs->rdc = rdc; 9309 rargs->dvp = vp; 9310 rargs->mi = mi; 9311 rargs->cr = cr; 9312 9313 9314 /* 9315 * If count < than the minimum required, we return no entries 9316 * and fail with EINVAL 9317 */ 9318 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9319 rdc->error = EINVAL; 9320 goto out; 9321 } 9322 9323 if (args.array_len == 5) { 9324 /* 9325 * Add lookupp and getattr for parent nodeid. 9326 */ 9327 argop[2].argop = OP_LOOKUPP; 9328 9329 argop[3].argop = OP_GETFH; 9330 9331 /* getattr parent */ 9332 argop[4].argop = OP_GETATTR; 9333 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9334 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9335 } 9336 9337 doqueue = 1; 9338 9339 if (mi->mi_io_kstats) { 9340 mutex_enter(&mi->mi_lock); 9341 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9342 mutex_exit(&mi->mi_lock); 9343 } 9344 9345 /* capture the time of this call */ 9346 rargs->t = t = gethrtime(); 9347 9348 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9349 9350 if (mi->mi_io_kstats) { 9351 mutex_enter(&mi->mi_lock); 9352 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9353 mutex_exit(&mi->mi_lock); 9354 } 9355 9356 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9357 9358 /* 9359 * If RPC error occurred and it isn't an error that 9360 * triggers recovery, then go ahead and fail now. 9361 */ 9362 if (e.error != 0 && !needrecov) { 9363 rdc->error = e.error; 9364 goto out; 9365 } 9366 9367 if (needrecov) { 9368 bool_t abort; 9369 9370 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9371 "nfs4readdir: initiating recovery.\n")); 9372 9373 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9374 NULL, OP_READDIR, NULL, NULL, NULL); 9375 if (abort == FALSE) { 9376 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9377 &recov_state, needrecov); 9378 if (!e.error) 9379 (void) xdr_free(xdr_COMPOUND4res_clnt, 9380 (caddr_t)&res); 9381 if (rdc->entries != NULL) { 9382 kmem_free(rdc->entries, rdc->entlen); 9383 rdc->entries = NULL; 9384 } 9385 goto recov_retry; 9386 } 9387 9388 if (e.error != 0) { 9389 rdc->error = e.error; 9390 goto out; 9391 } 9392 9393 /* fall through for res.status case */ 9394 } 9395 9396 res_opcnt = res.array_len; 9397 9398 /* 9399 * If compound failed first 2 ops (PUTFH+READDIR), then return 9400 * failure here. Subsequent ops are for filling out dot-dot 9401 * dirent, and if they fail, we still want to give the caller 9402 * the dirents returned by (the successful) READDIR op, so we need 9403 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9404 * 9405 * One example where PUTFH+READDIR ops would succeed but 9406 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9407 * but lacks x. In this case, a POSIX server's VOP_READDIR 9408 * would succeed; however, VOP_LOOKUP(..) would fail since no 9409 * x perm. We need to come up with a non-vendor-specific way 9410 * for a POSIX server to return d_ino from dotdot's dirent if 9411 * client only requests mounted_on_fileid, and just say the 9412 * LOOKUPP succeeded and fill out the GETATTR. However, if 9413 * client requested any mandatory attrs, server would be required 9414 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9415 * for dotdot. 9416 */ 9417 9418 if (res.status) { 9419 if (res_opcnt <= 2) { 9420 e.error = geterrno4(res.status); 9421 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9422 &recov_state, needrecov); 9423 nfs4_purge_stale_fh(e.error, vp, cr); 9424 rdc->error = e.error; 9425 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9426 if (rdc->entries != NULL) { 9427 kmem_free(rdc->entries, rdc->entlen); 9428 rdc->entries = NULL; 9429 } 9430 /* 9431 * If readdir a node that is a stub for a 9432 * crossed mount point, keep the original 9433 * secinfo flavor for the current file system, 9434 * not the crossed one. 9435 */ 9436 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9437 return; 9438 } 9439 } 9440 9441 resop = &res.array[1]; /* readdir res */ 9442 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9443 9444 mutex_enter(&rp->r_statelock); 9445 rp->r_cookieverf4 = rd_res->cookieverf; 9446 mutex_exit(&rp->r_statelock); 9447 9448 /* 9449 * For "." and ".." entries 9450 * e.g. 9451 * seek(cookie=0) -> "." entry with d_off = 1 9452 * seek(cookie=1) -> ".." entry with d_off = 2 9453 */ 9454 if (cookie == (nfs_cookie4) 0) { 9455 if (rd_res->dotp) 9456 rd_res->dotp->d_ino = nodeid; 9457 if (rd_res->dotdotp) 9458 rd_res->dotdotp->d_ino = pnodeid; 9459 } 9460 if (cookie == (nfs_cookie4) 1) { 9461 if (rd_res->dotdotp) 9462 rd_res->dotdotp->d_ino = pnodeid; 9463 } 9464 9465 9466 /* LOOKUPP+GETATTR attemped */ 9467 if (args.array_len == 5 && rd_res->dotdotp) { 9468 if (res.status == NFS4_OK && res_opcnt == 5) { 9469 nfs_fh4 *fhp; 9470 nfs4_sharedfh_t *sfhp; 9471 vnode_t *pvp; 9472 nfs4_ga_res_t *garp; 9473 9474 resop++; /* lookupp */ 9475 resop++; /* getfh */ 9476 fhp = &resop->nfs_resop4_u.opgetfh.object; 9477 9478 resop++; /* getattr of parent */ 9479 9480 /* 9481 * First, take care of finishing the 9482 * readdir results. 9483 */ 9484 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9485 /* 9486 * The d_ino of .. must be the inode number 9487 * of the mounted filesystem. 9488 */ 9489 if (garp->n4g_va.va_mask & AT_NODEID) 9490 rd_res->dotdotp->d_ino = 9491 garp->n4g_va.va_nodeid; 9492 9493 9494 /* 9495 * Next, create the ".." dnlc entry 9496 */ 9497 sfhp = sfh4_get(fhp, mi); 9498 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9499 dnlc_update(vp, "..", pvp); 9500 VN_RELE(pvp); 9501 } 9502 sfh4_rele(&sfhp); 9503 } 9504 } 9505 9506 if (mi->mi_io_kstats) { 9507 mutex_enter(&mi->mi_lock); 9508 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9509 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9510 mutex_exit(&mi->mi_lock); 9511 } 9512 9513 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9514 9515 out: 9516 /* 9517 * If readdir a node that is a stub for a crossed mount point, 9518 * keep the original secinfo flavor for the current file system, 9519 * not the crossed one. 9520 */ 9521 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9522 9523 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9524 } 9525 9526 9527 static int 9528 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9529 { 9530 rnode4_t *rp = VTOR4(bp->b_vp); 9531 int count; 9532 int error; 9533 cred_t *cred_otw = NULL; 9534 offset_t offset; 9535 nfs4_open_stream_t *osp = NULL; 9536 bool_t first_time = TRUE; /* first time getting otw cred */ 9537 bool_t last_time = FALSE; /* last time getting otw cred */ 9538 9539 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9540 9541 DTRACE_IO1(start, struct buf *, bp); 9542 offset = ldbtob(bp->b_lblkno); 9543 9544 if (bp->b_flags & B_READ) { 9545 read_again: 9546 /* 9547 * Releases the osp, if it is provided. 9548 * Puts a hold on the cred_otw and the new osp (if found). 9549 */ 9550 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9551 &first_time, &last_time); 9552 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9553 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9554 readahead, NULL); 9555 crfree(cred_otw); 9556 if (!error) { 9557 if (bp->b_resid) { 9558 /* 9559 * Didn't get it all because we hit EOF, 9560 * zero all the memory beyond the EOF. 9561 */ 9562 /* bzero(rdaddr + */ 9563 bzero(bp->b_un.b_addr + 9564 bp->b_bcount - bp->b_resid, bp->b_resid); 9565 } 9566 mutex_enter(&rp->r_statelock); 9567 if (bp->b_resid == bp->b_bcount && 9568 offset >= rp->r_size) { 9569 /* 9570 * We didn't read anything at all as we are 9571 * past EOF. Return an error indicator back 9572 * but don't destroy the pages (yet). 9573 */ 9574 error = NFS_EOF; 9575 } 9576 mutex_exit(&rp->r_statelock); 9577 } else if (error == EACCES && last_time == FALSE) { 9578 goto read_again; 9579 } 9580 } else { 9581 if (!(rp->r_flags & R4STALE)) { 9582 write_again: 9583 /* 9584 * Releases the osp, if it is provided. 9585 * Puts a hold on the cred_otw and the new 9586 * osp (if found). 9587 */ 9588 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9589 &first_time, &last_time); 9590 mutex_enter(&rp->r_statelock); 9591 count = MIN(bp->b_bcount, rp->r_size - offset); 9592 mutex_exit(&rp->r_statelock); 9593 if (count < 0) 9594 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9595 #ifdef DEBUG 9596 if (count == 0) { 9597 zoneid_t zoneid = getzoneid(); 9598 9599 zcmn_err(zoneid, CE_WARN, 9600 "nfs4_bio: zero length write at %lld", 9601 offset); 9602 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9603 "b_bcount=%ld, file size=%lld", 9604 rp->r_flags, (long)bp->b_bcount, 9605 rp->r_size); 9606 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9607 if (nfs4_bio_do_stop) 9608 debug_enter("nfs4_bio"); 9609 } 9610 #endif 9611 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9612 count, cred_otw, stab_comm); 9613 if (error == EACCES && last_time == FALSE) { 9614 crfree(cred_otw); 9615 goto write_again; 9616 } 9617 bp->b_error = error; 9618 if (error && error != EINTR && 9619 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9620 /* 9621 * Don't print EDQUOT errors on the console. 9622 * Don't print asynchronous EACCES errors. 9623 * Don't print EFBIG errors. 9624 * Print all other write errors. 9625 */ 9626 if (error != EDQUOT && error != EFBIG && 9627 (error != EACCES || 9628 !(bp->b_flags & B_ASYNC))) 9629 nfs4_write_error(bp->b_vp, 9630 error, cred_otw); 9631 /* 9632 * Update r_error and r_flags as appropriate. 9633 * If the error was ESTALE, then mark the 9634 * rnode as not being writeable and save 9635 * the error status. Otherwise, save any 9636 * errors which occur from asynchronous 9637 * page invalidations. Any errors occurring 9638 * from other operations should be saved 9639 * by the caller. 9640 */ 9641 mutex_enter(&rp->r_statelock); 9642 if (error == ESTALE) { 9643 rp->r_flags |= R4STALE; 9644 if (!rp->r_error) 9645 rp->r_error = error; 9646 } else if (!rp->r_error && 9647 (bp->b_flags & 9648 (B_INVAL|B_FORCE|B_ASYNC)) == 9649 (B_INVAL|B_FORCE|B_ASYNC)) { 9650 rp->r_error = error; 9651 } 9652 mutex_exit(&rp->r_statelock); 9653 } 9654 crfree(cred_otw); 9655 } else { 9656 error = rp->r_error; 9657 /* 9658 * A close may have cleared r_error, if so, 9659 * propagate ESTALE error return properly 9660 */ 9661 if (error == 0) 9662 error = ESTALE; 9663 } 9664 } 9665 9666 if (error != 0 && error != NFS_EOF) 9667 bp->b_flags |= B_ERROR; 9668 9669 if (osp) 9670 open_stream_rele(osp, rp); 9671 9672 DTRACE_IO1(done, struct buf *, bp); 9673 9674 return (error); 9675 } 9676 9677 /* ARGSUSED */ 9678 int 9679 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 9680 { 9681 return (EREMOTE); 9682 } 9683 9684 /* ARGSUSED2 */ 9685 int 9686 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9687 { 9688 rnode4_t *rp = VTOR4(vp); 9689 9690 if (!write_lock) { 9691 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9692 return (V_WRITELOCK_FALSE); 9693 } 9694 9695 if ((rp->r_flags & R4DIRECTIO) || 9696 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9697 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9698 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9699 return (V_WRITELOCK_FALSE); 9700 nfs_rw_exit(&rp->r_rwlock); 9701 } 9702 9703 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9704 return (V_WRITELOCK_TRUE); 9705 } 9706 9707 /* ARGSUSED */ 9708 void 9709 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9710 { 9711 rnode4_t *rp = VTOR4(vp); 9712 9713 nfs_rw_exit(&rp->r_rwlock); 9714 } 9715 9716 /* ARGSUSED */ 9717 static int 9718 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 9719 { 9720 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9721 return (EIO); 9722 9723 /* 9724 * Because we stuff the readdir cookie into the offset field 9725 * someone may attempt to do an lseek with the cookie which 9726 * we want to succeed. 9727 */ 9728 if (vp->v_type == VDIR) 9729 return (0); 9730 if (*noffp < 0) 9731 return (EINVAL); 9732 return (0); 9733 } 9734 9735 9736 /* 9737 * Return all the pages from [off..off+len) in file 9738 */ 9739 /* ARGSUSED */ 9740 static int 9741 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9742 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9743 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 9744 { 9745 rnode4_t *rp; 9746 int error; 9747 mntinfo4_t *mi; 9748 9749 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9750 return (EIO); 9751 rp = VTOR4(vp); 9752 if (IS_SHADOW(vp, rp)) 9753 vp = RTOV4(rp); 9754 9755 if (vp->v_flag & VNOMAP) 9756 return (ENOSYS); 9757 9758 if (protp != NULL) 9759 *protp = PROT_ALL; 9760 9761 /* 9762 * Now validate that the caches are up to date. 9763 */ 9764 if (error = nfs4_validate_caches(vp, cr)) 9765 return (error); 9766 9767 mi = VTOMI4(vp); 9768 retry: 9769 mutex_enter(&rp->r_statelock); 9770 9771 /* 9772 * Don't create dirty pages faster than they 9773 * can be cleaned so that the system doesn't 9774 * get imbalanced. If the async queue is 9775 * maxed out, then wait for it to drain before 9776 * creating more dirty pages. Also, wait for 9777 * any threads doing pagewalks in the vop_getattr 9778 * entry points so that they don't block for 9779 * long periods. 9780 */ 9781 if (rw == S_CREATE) { 9782 while ((mi->mi_max_threads != 0 && 9783 rp->r_awcount > 2 * mi->mi_max_threads) || 9784 rp->r_gcount > 0) 9785 cv_wait(&rp->r_cv, &rp->r_statelock); 9786 } 9787 9788 /* 9789 * If we are getting called as a side effect of an nfs_write() 9790 * operation the local file size might not be extended yet. 9791 * In this case we want to be able to return pages of zeroes. 9792 */ 9793 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9794 NFS4_DEBUG(nfs4_pageio_debug, 9795 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9796 "len=%llu, size=%llu, attrsize =%llu", off, 9797 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9798 mutex_exit(&rp->r_statelock); 9799 return (EFAULT); /* beyond EOF */ 9800 } 9801 9802 mutex_exit(&rp->r_statelock); 9803 9804 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9805 pl, plsz, seg, addr, rw, cr); 9806 NFS4_DEBUG(nfs4_pageio_debug && error, 9807 (CE_NOTE, "getpages error %d; off=%lld, len=%lld", 9808 error, off, (u_longlong_t)len)); 9809 9810 switch (error) { 9811 case NFS_EOF: 9812 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9813 goto retry; 9814 case ESTALE: 9815 nfs4_purge_stale_fh(error, vp, cr); 9816 } 9817 9818 return (error); 9819 } 9820 9821 /* 9822 * Called from pvn_getpages to get a particular page. 9823 */ 9824 /* ARGSUSED */ 9825 static int 9826 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9827 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9828 enum seg_rw rw, cred_t *cr) 9829 { 9830 rnode4_t *rp; 9831 uint_t bsize; 9832 struct buf *bp; 9833 page_t *pp; 9834 u_offset_t lbn; 9835 u_offset_t io_off; 9836 u_offset_t blkoff; 9837 u_offset_t rablkoff; 9838 size_t io_len; 9839 uint_t blksize; 9840 int error; 9841 int readahead; 9842 int readahead_issued = 0; 9843 int ra_window; /* readahead window */ 9844 page_t *pagefound; 9845 page_t *savepp; 9846 9847 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9848 return (EIO); 9849 9850 rp = VTOR4(vp); 9851 ASSERT(!IS_SHADOW(vp, rp)); 9852 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9853 9854 reread: 9855 bp = NULL; 9856 pp = NULL; 9857 pagefound = NULL; 9858 9859 if (pl != NULL) 9860 pl[0] = NULL; 9861 9862 error = 0; 9863 lbn = off / bsize; 9864 blkoff = lbn * bsize; 9865 9866 /* 9867 * Queueing up the readahead before doing the synchronous read 9868 * results in a significant increase in read throughput because 9869 * of the increased parallelism between the async threads and 9870 * the process context. 9871 */ 9872 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9873 rw != S_CREATE && 9874 !(vp->v_flag & VNOCACHE)) { 9875 mutex_enter(&rp->r_statelock); 9876 9877 /* 9878 * Calculate the number of readaheads to do. 9879 * a) No readaheads at offset = 0. 9880 * b) Do maximum(nfs4_nra) readaheads when the readahead 9881 * window is closed. 9882 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9883 * upon how far the readahead window is open or close. 9884 * d) No readaheads if rp->r_nextr is not within the scope 9885 * of the readahead window (random i/o). 9886 */ 9887 9888 if (off == 0) 9889 readahead = 0; 9890 else if (blkoff == rp->r_nextr) 9891 readahead = nfs4_nra; 9892 else if (rp->r_nextr > blkoff && 9893 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9894 <= (nfs4_nra - 1))) 9895 readahead = nfs4_nra - ra_window; 9896 else 9897 readahead = 0; 9898 9899 rablkoff = rp->r_nextr; 9900 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9901 mutex_exit(&rp->r_statelock); 9902 if (nfs4_async_readahead(vp, rablkoff + bsize, 9903 addr + (rablkoff + bsize - off), 9904 seg, cr, nfs4_readahead) < 0) { 9905 mutex_enter(&rp->r_statelock); 9906 break; 9907 } 9908 readahead--; 9909 rablkoff += bsize; 9910 /* 9911 * Indicate that we did a readahead so 9912 * readahead offset is not updated 9913 * by the synchronous read below. 9914 */ 9915 readahead_issued = 1; 9916 mutex_enter(&rp->r_statelock); 9917 /* 9918 * set readahead offset to 9919 * offset of last async readahead 9920 * request. 9921 */ 9922 rp->r_nextr = rablkoff; 9923 } 9924 mutex_exit(&rp->r_statelock); 9925 } 9926 9927 again: 9928 if ((pagefound = page_exists(vp, off)) == NULL) { 9929 if (pl == NULL) { 9930 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9931 nfs4_readahead); 9932 } else if (rw == S_CREATE) { 9933 /* 9934 * Block for this page is not allocated, or the offset 9935 * is beyond the current allocation size, or we're 9936 * allocating a swap slot and the page was not found, 9937 * so allocate it and return a zero page. 9938 */ 9939 if ((pp = page_create_va(vp, off, 9940 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9941 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9942 io_len = PAGESIZE; 9943 mutex_enter(&rp->r_statelock); 9944 rp->r_nextr = off + PAGESIZE; 9945 mutex_exit(&rp->r_statelock); 9946 } else { 9947 /* 9948 * Need to go to server to get a block 9949 */ 9950 mutex_enter(&rp->r_statelock); 9951 if (blkoff < rp->r_size && 9952 blkoff + bsize > rp->r_size) { 9953 /* 9954 * If less than a block left in 9955 * file read less than a block. 9956 */ 9957 if (rp->r_size <= off) { 9958 /* 9959 * Trying to access beyond EOF, 9960 * set up to get at least one page. 9961 */ 9962 blksize = off + PAGESIZE - blkoff; 9963 } else 9964 blksize = rp->r_size - blkoff; 9965 } else if ((off == 0) || 9966 (off != rp->r_nextr && !readahead_issued)) { 9967 blksize = PAGESIZE; 9968 blkoff = off; /* block = page here */ 9969 } else 9970 blksize = bsize; 9971 mutex_exit(&rp->r_statelock); 9972 9973 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9974 &io_len, blkoff, blksize, 0); 9975 9976 /* 9977 * Some other thread has entered the page, 9978 * so just use it. 9979 */ 9980 if (pp == NULL) 9981 goto again; 9982 9983 /* 9984 * Now round the request size up to page boundaries. 9985 * This ensures that the entire page will be 9986 * initialized to zeroes if EOF is encountered. 9987 */ 9988 io_len = ptob(btopr(io_len)); 9989 9990 bp = pageio_setup(pp, io_len, vp, B_READ); 9991 ASSERT(bp != NULL); 9992 9993 /* 9994 * pageio_setup should have set b_addr to 0. This 9995 * is correct since we want to do I/O on a page 9996 * boundary. bp_mapin will use this addr to calculate 9997 * an offset, and then set b_addr to the kernel virtual 9998 * address it allocated for us. 9999 */ 10000 ASSERT(bp->b_un.b_addr == 0); 10001 10002 bp->b_edev = 0; 10003 bp->b_dev = 0; 10004 bp->b_lblkno = lbtodb(io_off); 10005 bp->b_file = vp; 10006 bp->b_offset = (offset_t)off; 10007 bp_mapin(bp); 10008 10009 /* 10010 * If doing a write beyond what we believe is EOF, 10011 * don't bother trying to read the pages from the 10012 * server, we'll just zero the pages here. We 10013 * don't check that the rw flag is S_WRITE here 10014 * because some implementations may attempt a 10015 * read access to the buffer before copying data. 10016 */ 10017 mutex_enter(&rp->r_statelock); 10018 if (io_off >= rp->r_size && seg == segkmap) { 10019 mutex_exit(&rp->r_statelock); 10020 bzero(bp->b_un.b_addr, io_len); 10021 } else { 10022 mutex_exit(&rp->r_statelock); 10023 error = nfs4_bio(bp, NULL, cr, FALSE); 10024 } 10025 10026 /* 10027 * Unmap the buffer before freeing it. 10028 */ 10029 bp_mapout(bp); 10030 pageio_done(bp); 10031 10032 savepp = pp; 10033 do { 10034 pp->p_fsdata = C_NOCOMMIT; 10035 } while ((pp = pp->p_next) != savepp); 10036 10037 if (error == NFS_EOF) { 10038 /* 10039 * If doing a write system call just return 10040 * zeroed pages, else user tried to get pages 10041 * beyond EOF, return error. We don't check 10042 * that the rw flag is S_WRITE here because 10043 * some implementations may attempt a read 10044 * access to the buffer before copying data. 10045 */ 10046 if (seg == segkmap) 10047 error = 0; 10048 else 10049 error = EFAULT; 10050 } 10051 10052 if (!readahead_issued && !error) { 10053 mutex_enter(&rp->r_statelock); 10054 rp->r_nextr = io_off + io_len; 10055 mutex_exit(&rp->r_statelock); 10056 } 10057 } 10058 } 10059 10060 out: 10061 if (pl == NULL) 10062 return (error); 10063 10064 if (error) { 10065 if (pp != NULL) 10066 pvn_read_done(pp, B_ERROR); 10067 return (error); 10068 } 10069 10070 if (pagefound) { 10071 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 10072 10073 /* 10074 * Page exists in the cache, acquire the appropriate lock. 10075 * If this fails, start all over again. 10076 */ 10077 if ((pp = page_lookup(vp, off, se)) == NULL) { 10078 #ifdef DEBUG 10079 nfs4_lostpage++; 10080 #endif 10081 goto reread; 10082 } 10083 pl[0] = pp; 10084 pl[1] = NULL; 10085 return (0); 10086 } 10087 10088 if (pp != NULL) 10089 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 10090 10091 return (error); 10092 } 10093 10094 static void 10095 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 10096 cred_t *cr) 10097 { 10098 int error; 10099 page_t *pp; 10100 u_offset_t io_off; 10101 size_t io_len; 10102 struct buf *bp; 10103 uint_t bsize, blksize; 10104 rnode4_t *rp = VTOR4(vp); 10105 page_t *savepp; 10106 10107 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10108 10109 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10110 10111 mutex_enter(&rp->r_statelock); 10112 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 10113 /* 10114 * If less than a block left in file read less 10115 * than a block. 10116 */ 10117 blksize = rp->r_size - blkoff; 10118 } else 10119 blksize = bsize; 10120 mutex_exit(&rp->r_statelock); 10121 10122 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 10123 &io_off, &io_len, blkoff, blksize, 1); 10124 /* 10125 * The isra flag passed to the kluster function is 1, we may have 10126 * gotten a return value of NULL for a variety of reasons (# of free 10127 * pages < minfree, someone entered the page on the vnode etc). In all 10128 * cases, we want to punt on the readahead. 10129 */ 10130 if (pp == NULL) 10131 return; 10132 10133 /* 10134 * Now round the request size up to page boundaries. 10135 * This ensures that the entire page will be 10136 * initialized to zeroes if EOF is encountered. 10137 */ 10138 io_len = ptob(btopr(io_len)); 10139 10140 bp = pageio_setup(pp, io_len, vp, B_READ); 10141 ASSERT(bp != NULL); 10142 10143 /* 10144 * pageio_setup should have set b_addr to 0. This is correct since 10145 * we want to do I/O on a page boundary. bp_mapin() will use this addr 10146 * to calculate an offset, and then set b_addr to the kernel virtual 10147 * address it allocated for us. 10148 */ 10149 ASSERT(bp->b_un.b_addr == 0); 10150 10151 bp->b_edev = 0; 10152 bp->b_dev = 0; 10153 bp->b_lblkno = lbtodb(io_off); 10154 bp->b_file = vp; 10155 bp->b_offset = (offset_t)blkoff; 10156 bp_mapin(bp); 10157 10158 /* 10159 * If doing a write beyond what we believe is EOF, don't bother trying 10160 * to read the pages from the server, we'll just zero the pages here. 10161 * We don't check that the rw flag is S_WRITE here because some 10162 * implementations may attempt a read access to the buffer before 10163 * copying data. 10164 */ 10165 mutex_enter(&rp->r_statelock); 10166 if (io_off >= rp->r_size && seg == segkmap) { 10167 mutex_exit(&rp->r_statelock); 10168 bzero(bp->b_un.b_addr, io_len); 10169 error = 0; 10170 } else { 10171 mutex_exit(&rp->r_statelock); 10172 error = nfs4_bio(bp, NULL, cr, TRUE); 10173 if (error == NFS_EOF) 10174 error = 0; 10175 } 10176 10177 /* 10178 * Unmap the buffer before freeing it. 10179 */ 10180 bp_mapout(bp); 10181 pageio_done(bp); 10182 10183 savepp = pp; 10184 do { 10185 pp->p_fsdata = C_NOCOMMIT; 10186 } while ((pp = pp->p_next) != savepp); 10187 10188 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10189 10190 /* 10191 * In case of error set readahead offset 10192 * to the lowest offset. 10193 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10194 */ 10195 if (error && rp->r_nextr > io_off) { 10196 mutex_enter(&rp->r_statelock); 10197 if (rp->r_nextr > io_off) 10198 rp->r_nextr = io_off; 10199 mutex_exit(&rp->r_statelock); 10200 } 10201 } 10202 10203 /* 10204 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10205 * If len == 0, do from off to EOF. 10206 * 10207 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10208 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10209 * (from pageout). 10210 */ 10211 /* ARGSUSED */ 10212 static int 10213 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 10214 caller_context_t *ct) 10215 { 10216 int error; 10217 rnode4_t *rp; 10218 10219 ASSERT(cr != NULL); 10220 10221 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10222 return (EIO); 10223 10224 rp = VTOR4(vp); 10225 if (IS_SHADOW(vp, rp)) 10226 vp = RTOV4(rp); 10227 10228 /* 10229 * XXX - Why should this check be made here? 10230 */ 10231 if (vp->v_flag & VNOMAP) 10232 return (ENOSYS); 10233 10234 if (len == 0 && !(flags & B_INVAL) && 10235 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10236 return (0); 10237 10238 mutex_enter(&rp->r_statelock); 10239 rp->r_count++; 10240 mutex_exit(&rp->r_statelock); 10241 error = nfs4_putpages(vp, off, len, flags, cr); 10242 mutex_enter(&rp->r_statelock); 10243 rp->r_count--; 10244 cv_broadcast(&rp->r_cv); 10245 mutex_exit(&rp->r_statelock); 10246 10247 return (error); 10248 } 10249 10250 /* 10251 * Write out a single page, possibly klustering adjacent dirty pages. 10252 */ 10253 int 10254 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10255 int flags, cred_t *cr) 10256 { 10257 u_offset_t io_off; 10258 u_offset_t lbn_off; 10259 u_offset_t lbn; 10260 size_t io_len; 10261 uint_t bsize; 10262 int error; 10263 rnode4_t *rp; 10264 10265 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10266 ASSERT(pp != NULL); 10267 ASSERT(cr != NULL); 10268 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10269 10270 rp = VTOR4(vp); 10271 ASSERT(rp->r_count > 0); 10272 ASSERT(!IS_SHADOW(vp, rp)); 10273 10274 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10275 lbn = pp->p_offset / bsize; 10276 lbn_off = lbn * bsize; 10277 10278 /* 10279 * Find a kluster that fits in one block, or in 10280 * one page if pages are bigger than blocks. If 10281 * there is less file space allocated than a whole 10282 * page, we'll shorten the i/o request below. 10283 */ 10284 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10285 roundup(bsize, PAGESIZE), flags); 10286 10287 /* 10288 * pvn_write_kluster shouldn't have returned a page with offset 10289 * behind the original page we were given. Verify that. 10290 */ 10291 ASSERT((pp->p_offset / bsize) >= lbn); 10292 10293 /* 10294 * Now pp will have the list of kept dirty pages marked for 10295 * write back. It will also handle invalidation and freeing 10296 * of pages that are not dirty. Check for page length rounding 10297 * problems. 10298 */ 10299 if (io_off + io_len > lbn_off + bsize) { 10300 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10301 io_len = lbn_off + bsize - io_off; 10302 } 10303 /* 10304 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10305 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10306 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10307 * progress and the r_size has not been made consistent with the 10308 * new size of the file. When the uiomove() completes the r_size is 10309 * updated and the R4MODINPROGRESS flag is cleared. 10310 * 10311 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10312 * consistent value of r_size. Without this handshaking, it is 10313 * possible that nfs4_bio() picks up the old value of r_size 10314 * before the uiomove() in writerp4() completes. This will result 10315 * in the write through nfs4_bio() being dropped. 10316 * 10317 * More precisely, there is a window between the time the uiomove() 10318 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10319 * operation intervenes in this window, the page will be picked up, 10320 * because it is dirty (it will be unlocked, unless it was 10321 * pagecreate'd). When the page is picked up as dirty, the dirty 10322 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10323 * checked. This will still be the old size. Therefore the page will 10324 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10325 * the page will be found to be clean and the write will be dropped. 10326 */ 10327 if (rp->r_flags & R4MODINPROGRESS) { 10328 mutex_enter(&rp->r_statelock); 10329 if ((rp->r_flags & R4MODINPROGRESS) && 10330 rp->r_modaddr + MAXBSIZE > io_off && 10331 rp->r_modaddr < io_off + io_len) { 10332 page_t *plist; 10333 /* 10334 * A write is in progress for this region of the file. 10335 * If we did not detect R4MODINPROGRESS here then this 10336 * path through nfs_putapage() would eventually go to 10337 * nfs4_bio() and may not write out all of the data 10338 * in the pages. We end up losing data. So we decide 10339 * to set the modified bit on each page in the page 10340 * list and mark the rnode with R4DIRTY. This write 10341 * will be restarted at some later time. 10342 */ 10343 plist = pp; 10344 while (plist != NULL) { 10345 pp = plist; 10346 page_sub(&plist, pp); 10347 hat_setmod(pp); 10348 page_io_unlock(pp); 10349 page_unlock(pp); 10350 } 10351 rp->r_flags |= R4DIRTY; 10352 mutex_exit(&rp->r_statelock); 10353 if (offp) 10354 *offp = io_off; 10355 if (lenp) 10356 *lenp = io_len; 10357 return (0); 10358 } 10359 mutex_exit(&rp->r_statelock); 10360 } 10361 10362 if (flags & B_ASYNC) { 10363 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10364 nfs4_sync_putapage); 10365 } else 10366 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10367 10368 if (offp) 10369 *offp = io_off; 10370 if (lenp) 10371 *lenp = io_len; 10372 return (error); 10373 } 10374 10375 static int 10376 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10377 int flags, cred_t *cr) 10378 { 10379 int error; 10380 rnode4_t *rp; 10381 10382 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10383 10384 flags |= B_WRITE; 10385 10386 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10387 10388 rp = VTOR4(vp); 10389 10390 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10391 error == EACCES) && 10392 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10393 if (!(rp->r_flags & R4OUTOFSPACE)) { 10394 mutex_enter(&rp->r_statelock); 10395 rp->r_flags |= R4OUTOFSPACE; 10396 mutex_exit(&rp->r_statelock); 10397 } 10398 flags |= B_ERROR; 10399 pvn_write_done(pp, flags); 10400 /* 10401 * If this was not an async thread, then try again to 10402 * write out the pages, but this time, also destroy 10403 * them whether or not the write is successful. This 10404 * will prevent memory from filling up with these 10405 * pages and destroying them is the only alternative 10406 * if they can't be written out. 10407 * 10408 * Don't do this if this is an async thread because 10409 * when the pages are unlocked in pvn_write_done, 10410 * some other thread could have come along, locked 10411 * them, and queued for an async thread. It would be 10412 * possible for all of the async threads to be tied 10413 * up waiting to lock the pages again and they would 10414 * all already be locked and waiting for an async 10415 * thread to handle them. Deadlock. 10416 */ 10417 if (!(flags & B_ASYNC)) { 10418 error = nfs4_putpage(vp, io_off, io_len, 10419 B_INVAL | B_FORCE, cr, NULL); 10420 } 10421 } else { 10422 if (error) 10423 flags |= B_ERROR; 10424 else if (rp->r_flags & R4OUTOFSPACE) { 10425 mutex_enter(&rp->r_statelock); 10426 rp->r_flags &= ~R4OUTOFSPACE; 10427 mutex_exit(&rp->r_statelock); 10428 } 10429 pvn_write_done(pp, flags); 10430 if (freemem < desfree) 10431 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10432 NFS4_WRITE_NOWAIT); 10433 } 10434 10435 return (error); 10436 } 10437 10438 #ifdef DEBUG 10439 int nfs4_force_open_before_mmap = 0; 10440 #endif 10441 10442 /* ARGSUSED */ 10443 static int 10444 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10445 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10446 caller_context_t *ct) 10447 { 10448 struct segvn_crargs vn_a; 10449 int error = 0; 10450 rnode4_t *rp = VTOR4(vp); 10451 mntinfo4_t *mi = VTOMI4(vp); 10452 10453 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10454 return (EIO); 10455 10456 if (vp->v_flag & VNOMAP) 10457 return (ENOSYS); 10458 10459 if (off < 0 || (off + len) < 0) 10460 return (ENXIO); 10461 10462 if (vp->v_type != VREG) 10463 return (ENODEV); 10464 10465 /* 10466 * If the file is delegated to the client don't do anything. 10467 * If the file is not delegated, then validate the data cache. 10468 */ 10469 mutex_enter(&rp->r_statev4_lock); 10470 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10471 mutex_exit(&rp->r_statev4_lock); 10472 error = nfs4_validate_caches(vp, cr); 10473 if (error) 10474 return (error); 10475 } else { 10476 mutex_exit(&rp->r_statev4_lock); 10477 } 10478 10479 /* 10480 * Check to see if the vnode is currently marked as not cachable. 10481 * This means portions of the file are locked (through VOP_FRLOCK). 10482 * In this case the map request must be refused. We use 10483 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10484 * 10485 * Atomically increment r_inmap after acquiring r_rwlock. The 10486 * idea here is to acquire r_rwlock to block read/write and 10487 * not to protect r_inmap. r_inmap will inform nfs4_read/write() 10488 * that we are in nfs4_map(). Now, r_rwlock is acquired in order 10489 * and we can prevent the deadlock that would have occurred 10490 * when nfs4_addmap() would have acquired it out of order. 10491 * 10492 * Since we are not protecting r_inmap by any lock, we do not 10493 * hold any lock when we decrement it. We atomically decrement 10494 * r_inmap after we release r_lkserlock. 10495 */ 10496 10497 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp))) 10498 return (EINTR); 10499 atomic_inc_uint(&rp->r_inmap); 10500 nfs_rw_exit(&rp->r_rwlock); 10501 10502 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) { 10503 atomic_dec_uint(&rp->r_inmap); 10504 return (EINTR); 10505 } 10506 10507 10508 if (vp->v_flag & VNOCACHE) { 10509 error = EAGAIN; 10510 goto done; 10511 } 10512 10513 /* 10514 * Don't allow concurrent locks and mapping if mandatory locking is 10515 * enabled. 10516 */ 10517 if (flk_has_remote_locks(vp)) { 10518 struct vattr va; 10519 va.va_mask = AT_MODE; 10520 error = nfs4getattr(vp, &va, cr); 10521 if (error != 0) 10522 goto done; 10523 if (MANDLOCK(vp, va.va_mode)) { 10524 error = EAGAIN; 10525 goto done; 10526 } 10527 } 10528 10529 /* 10530 * It is possible that the rnode has a lost lock request that we 10531 * are still trying to recover, and that the request conflicts with 10532 * this map request. 10533 * 10534 * An alternative approach would be for nfs4_safemap() to consider 10535 * queued lock requests when deciding whether to set or clear 10536 * VNOCACHE. This would require the frlock code path to call 10537 * nfs4_safemap() after enqueing a lost request. 10538 */ 10539 if (nfs4_map_lost_lock_conflict(vp)) { 10540 error = EAGAIN; 10541 goto done; 10542 } 10543 10544 as_rangelock(as); 10545 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 10546 if (error != 0) { 10547 as_rangeunlock(as); 10548 goto done; 10549 } 10550 10551 if (vp->v_type == VREG) { 10552 /* 10553 * We need to retrieve the open stream 10554 */ 10555 nfs4_open_stream_t *osp = NULL; 10556 nfs4_open_owner_t *oop = NULL; 10557 10558 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10559 if (oop != NULL) { 10560 /* returns with 'os_sync_lock' held */ 10561 osp = find_open_stream(oop, rp); 10562 open_owner_rele(oop); 10563 } 10564 if (osp == NULL) { 10565 #ifdef DEBUG 10566 if (nfs4_force_open_before_mmap) { 10567 error = EIO; 10568 goto done; 10569 } 10570 #endif 10571 /* returns with 'os_sync_lock' held */ 10572 error = open_and_get_osp(vp, cr, &osp); 10573 if (osp == NULL) { 10574 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10575 "nfs4_map: we tried to OPEN the file " 10576 "but again no osp, so fail with EIO")); 10577 goto done; 10578 } 10579 } 10580 10581 if (osp->os_failed_reopen) { 10582 mutex_exit(&osp->os_sync_lock); 10583 open_stream_rele(osp, rp); 10584 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10585 "nfs4_map: os_failed_reopen set on " 10586 "osp %p, cr %p, rp %s", (void *)osp, 10587 (void *)cr, rnode4info(rp))); 10588 error = EIO; 10589 goto done; 10590 } 10591 mutex_exit(&osp->os_sync_lock); 10592 open_stream_rele(osp, rp); 10593 } 10594 10595 vn_a.vp = vp; 10596 vn_a.offset = off; 10597 vn_a.type = (flags & MAP_TYPE); 10598 vn_a.prot = (uchar_t)prot; 10599 vn_a.maxprot = (uchar_t)maxprot; 10600 vn_a.flags = (flags & ~MAP_TYPE); 10601 vn_a.cred = cr; 10602 vn_a.amp = NULL; 10603 vn_a.szc = 0; 10604 vn_a.lgrp_mem_policy_flags = 0; 10605 10606 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10607 as_rangeunlock(as); 10608 10609 done: 10610 nfs_rw_exit(&rp->r_lkserlock); 10611 atomic_dec_uint(&rp->r_inmap); 10612 return (error); 10613 } 10614 10615 /* 10616 * We're most likely dealing with a kernel module that likes to READ 10617 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10618 * officially OPEN the file to create the necessary client state 10619 * for bookkeeping of os_mmap_read/write counts. 10620 * 10621 * Since VOP_MAP only passes in a pointer to the vnode rather than 10622 * a double pointer, we can't handle the case where nfs4open_otw() 10623 * returns a different vnode than the one passed into VOP_MAP (since 10624 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10625 * we return NULL and let nfs4_map() fail. Note: the only case where 10626 * this should happen is if the file got removed and replaced with the 10627 * same name on the server (in addition to the fact that we're trying 10628 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10629 */ 10630 static int 10631 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10632 { 10633 rnode4_t *rp, *drp; 10634 vnode_t *dvp, *open_vp; 10635 char file_name[MAXNAMELEN]; 10636 int just_created; 10637 nfs4_open_stream_t *osp; 10638 nfs4_open_owner_t *oop; 10639 int error; 10640 10641 *ospp = NULL; 10642 open_vp = map_vp; 10643 10644 rp = VTOR4(open_vp); 10645 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10646 return (error); 10647 drp = VTOR4(dvp); 10648 10649 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10650 VN_RELE(dvp); 10651 return (EINTR); 10652 } 10653 10654 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10655 nfs_rw_exit(&drp->r_rwlock); 10656 VN_RELE(dvp); 10657 return (error); 10658 } 10659 10660 mutex_enter(&rp->r_statev4_lock); 10661 if (rp->created_v4) { 10662 rp->created_v4 = 0; 10663 mutex_exit(&rp->r_statev4_lock); 10664 10665 dnlc_update(dvp, file_name, open_vp); 10666 /* This is needed so we don't bump the open ref count */ 10667 just_created = 1; 10668 } else { 10669 mutex_exit(&rp->r_statev4_lock); 10670 just_created = 0; 10671 } 10672 10673 VN_HOLD(map_vp); 10674 10675 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10676 just_created); 10677 if (error) { 10678 nfs_rw_exit(&drp->r_rwlock); 10679 VN_RELE(dvp); 10680 VN_RELE(map_vp); 10681 return (error); 10682 } 10683 10684 nfs_rw_exit(&drp->r_rwlock); 10685 VN_RELE(dvp); 10686 10687 /* 10688 * If nfs4open_otw() returned a different vnode then "undo" 10689 * the open and return failure to the caller. 10690 */ 10691 if (!VN_CMP(open_vp, map_vp)) { 10692 nfs4_error_t e; 10693 10694 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10695 "open returned a different vnode")); 10696 /* 10697 * If there's an error, ignore it, 10698 * and let VOP_INACTIVE handle it. 10699 */ 10700 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10701 CLOSE_NORM, 0, 0, 0); 10702 VN_RELE(map_vp); 10703 return (EIO); 10704 } 10705 10706 VN_RELE(map_vp); 10707 10708 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10709 if (!oop) { 10710 nfs4_error_t e; 10711 10712 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10713 "no open owner")); 10714 /* 10715 * If there's an error, ignore it, 10716 * and let VOP_INACTIVE handle it. 10717 */ 10718 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10719 CLOSE_NORM, 0, 0, 0); 10720 return (EIO); 10721 } 10722 osp = find_open_stream(oop, rp); 10723 open_owner_rele(oop); 10724 *ospp = osp; 10725 return (0); 10726 } 10727 10728 /* 10729 * Please be aware that when this function is called, the address space write 10730 * a_lock is held. Do not put over the wire calls in this function. 10731 */ 10732 /* ARGSUSED */ 10733 static int 10734 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10735 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10736 caller_context_t *ct) 10737 { 10738 rnode4_t *rp; 10739 int error = 0; 10740 mntinfo4_t *mi; 10741 10742 mi = VTOMI4(vp); 10743 rp = VTOR4(vp); 10744 10745 if (nfs_zone() != mi->mi_zone) 10746 return (EIO); 10747 if (vp->v_flag & VNOMAP) 10748 return (ENOSYS); 10749 10750 /* 10751 * Don't need to update the open stream first, since this 10752 * mmap can't add any additional share access that isn't 10753 * already contained in the open stream (for the case where we 10754 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10755 * take into account os_mmap_read[write] counts). 10756 */ 10757 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10758 10759 if (vp->v_type == VREG) { 10760 /* 10761 * We need to retrieve the open stream and update the counts. 10762 * If there is no open stream here, something is wrong. 10763 */ 10764 nfs4_open_stream_t *osp = NULL; 10765 nfs4_open_owner_t *oop = NULL; 10766 10767 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10768 if (oop != NULL) { 10769 /* returns with 'os_sync_lock' held */ 10770 osp = find_open_stream(oop, rp); 10771 open_owner_rele(oop); 10772 } 10773 if (osp == NULL) { 10774 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10775 "nfs4_addmap: we should have an osp" 10776 "but we don't, so fail with EIO")); 10777 error = EIO; 10778 goto out; 10779 } 10780 10781 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10782 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10783 10784 /* 10785 * Update the map count in the open stream. 10786 * This is necessary in the case where we 10787 * open/mmap/close/, then the server reboots, and we 10788 * attempt to reopen. If the mmap doesn't add share 10789 * access then we send an invalid reopen with 10790 * access = NONE. 10791 * 10792 * We need to specifically check each PROT_* so a mmap 10793 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10794 * read and write access. A simple comparison of prot 10795 * to ~PROT_WRITE to determine read access is insufficient 10796 * since prot can be |= with PROT_USER, etc. 10797 */ 10798 10799 /* 10800 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10801 */ 10802 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10803 osp->os_mmap_write += btopr(len); 10804 if (maxprot & PROT_READ) 10805 osp->os_mmap_read += btopr(len); 10806 if (maxprot & PROT_EXEC) 10807 osp->os_mmap_read += btopr(len); 10808 /* 10809 * Ensure that os_mmap_read gets incremented, even if 10810 * maxprot were to look like PROT_NONE. 10811 */ 10812 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10813 !(maxprot & PROT_EXEC)) 10814 osp->os_mmap_read += btopr(len); 10815 osp->os_mapcnt += btopr(len); 10816 mutex_exit(&osp->os_sync_lock); 10817 open_stream_rele(osp, rp); 10818 } 10819 10820 out: 10821 /* 10822 * If we got an error, then undo our 10823 * incrementing of 'r_mapcnt'. 10824 */ 10825 10826 if (error) { 10827 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10828 ASSERT(rp->r_mapcnt >= 0); 10829 } 10830 return (error); 10831 } 10832 10833 /* ARGSUSED */ 10834 static int 10835 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct) 10836 { 10837 10838 return (VTOR4(vp1) == VTOR4(vp2)); 10839 } 10840 10841 /* ARGSUSED */ 10842 static int 10843 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10844 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr, 10845 caller_context_t *ct) 10846 { 10847 int rc; 10848 u_offset_t start, end; 10849 rnode4_t *rp; 10850 int error = 0, intr = INTR4(vp); 10851 nfs4_error_t e; 10852 10853 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10854 return (EIO); 10855 10856 /* check for valid cmd parameter */ 10857 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10858 return (EINVAL); 10859 10860 /* Verify l_type. */ 10861 switch (bfp->l_type) { 10862 case F_RDLCK: 10863 if (cmd != F_GETLK && !(flag & FREAD)) 10864 return (EBADF); 10865 break; 10866 case F_WRLCK: 10867 if (cmd != F_GETLK && !(flag & FWRITE)) 10868 return (EBADF); 10869 break; 10870 case F_UNLCK: 10871 intr = 0; 10872 break; 10873 10874 default: 10875 return (EINVAL); 10876 } 10877 10878 /* check the validity of the lock range */ 10879 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10880 return (rc); 10881 if (rc = flk_check_lock_data(start, end, MAXEND)) 10882 return (rc); 10883 10884 /* 10885 * If the filesystem is mounted using local locking, pass the 10886 * request off to the local locking code. 10887 */ 10888 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10889 if (cmd == F_SETLK || cmd == F_SETLKW) { 10890 /* 10891 * For complete safety, we should be holding 10892 * r_lkserlock. However, we can't call 10893 * nfs4_safelock and then fs_frlock while 10894 * holding r_lkserlock, so just invoke 10895 * nfs4_safelock and expect that this will 10896 * catch enough of the cases. 10897 */ 10898 if (!nfs4_safelock(vp, bfp, cr)) 10899 return (EAGAIN); 10900 } 10901 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 10902 } 10903 10904 rp = VTOR4(vp); 10905 10906 /* 10907 * Check whether the given lock request can proceed, given the 10908 * current file mappings. 10909 */ 10910 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10911 return (EINTR); 10912 if (cmd == F_SETLK || cmd == F_SETLKW) { 10913 if (!nfs4_safelock(vp, bfp, cr)) { 10914 rc = EAGAIN; 10915 goto done; 10916 } 10917 } 10918 10919 /* 10920 * Flush the cache after waiting for async I/O to finish. For new 10921 * locks, this is so that the process gets the latest bits from the 10922 * server. For unlocks, this is so that other clients see the 10923 * latest bits once the file has been unlocked. If currently dirty 10924 * pages can't be flushed, then don't allow a lock to be set. But 10925 * allow unlocks to succeed, to avoid having orphan locks on the 10926 * server. 10927 */ 10928 if (cmd != F_GETLK) { 10929 mutex_enter(&rp->r_statelock); 10930 while (rp->r_count > 0) { 10931 if (intr) { 10932 klwp_t *lwp = ttolwp(curthread); 10933 10934 if (lwp != NULL) 10935 lwp->lwp_nostop++; 10936 if (cv_wait_sig(&rp->r_cv, 10937 &rp->r_statelock) == 0) { 10938 if (lwp != NULL) 10939 lwp->lwp_nostop--; 10940 rc = EINTR; 10941 break; 10942 } 10943 if (lwp != NULL) 10944 lwp->lwp_nostop--; 10945 } else 10946 cv_wait(&rp->r_cv, &rp->r_statelock); 10947 } 10948 mutex_exit(&rp->r_statelock); 10949 if (rc != 0) 10950 goto done; 10951 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct); 10952 if (error) { 10953 if (error == ENOSPC || error == EDQUOT) { 10954 mutex_enter(&rp->r_statelock); 10955 if (!rp->r_error) 10956 rp->r_error = error; 10957 mutex_exit(&rp->r_statelock); 10958 } 10959 if (bfp->l_type != F_UNLCK) { 10960 rc = ENOLCK; 10961 goto done; 10962 } 10963 } 10964 } 10965 10966 /* 10967 * Call the lock manager to do the real work of contacting 10968 * the server and obtaining the lock. 10969 */ 10970 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10971 cr, &e, NULL, NULL); 10972 rc = e.error; 10973 10974 if (rc == 0) 10975 nfs4_lockcompletion(vp, cmd); 10976 10977 done: 10978 nfs_rw_exit(&rp->r_lkserlock); 10979 10980 return (rc); 10981 } 10982 10983 /* 10984 * Free storage space associated with the specified vnode. The portion 10985 * to be freed is specified by bfp->l_start and bfp->l_len (already 10986 * normalized to a "whence" of 0). 10987 * 10988 * This is an experimental facility whose continued existence is not 10989 * guaranteed. Currently, we only support the special case 10990 * of l_len == 0, meaning free to end of file. 10991 */ 10992 /* ARGSUSED */ 10993 static int 10994 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10995 offset_t offset, cred_t *cr, caller_context_t *ct) 10996 { 10997 int error; 10998 10999 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11000 return (EIO); 11001 ASSERT(vp->v_type == VREG); 11002 if (cmd != F_FREESP) 11003 return (EINVAL); 11004 11005 error = convoff(vp, bfp, 0, offset); 11006 if (!error) { 11007 ASSERT(bfp->l_start >= 0); 11008 if (bfp->l_len == 0) { 11009 struct vattr va; 11010 11011 va.va_mask = AT_SIZE; 11012 va.va_size = bfp->l_start; 11013 error = nfs4setattr(vp, &va, 0, cr, NULL); 11014 11015 if (error == 0 && bfp->l_start == 0) 11016 vnevent_truncate(vp, ct); 11017 } else 11018 error = EINVAL; 11019 } 11020 11021 return (error); 11022 } 11023 11024 /* ARGSUSED */ 11025 int 11026 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 11027 { 11028 rnode4_t *rp; 11029 rp = VTOR4(vp); 11030 11031 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 11032 vp = RTOV4(rp); 11033 } 11034 *vpp = vp; 11035 return (0); 11036 } 11037 11038 /* 11039 * Setup and add an address space callback to do the work of the delmap call. 11040 * The callback will (and must be) deleted in the actual callback function. 11041 * 11042 * This is done in order to take care of the problem that we have with holding 11043 * the address space's a_lock for a long period of time (e.g. if the NFS server 11044 * is down). Callbacks will be executed in the address space code while the 11045 * a_lock is not held. Holding the address space's a_lock causes things such 11046 * as ps and fork to hang because they are trying to acquire this lock as well. 11047 */ 11048 /* ARGSUSED */ 11049 static int 11050 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 11051 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 11052 caller_context_t *ct) 11053 { 11054 int caller_found; 11055 int error; 11056 rnode4_t *rp; 11057 nfs4_delmap_args_t *dmapp; 11058 nfs4_delmapcall_t *delmap_call; 11059 11060 if (vp->v_flag & VNOMAP) 11061 return (ENOSYS); 11062 11063 /* 11064 * A process may not change zones if it has NFS pages mmap'ed 11065 * in, so we can't legitimately get here from the wrong zone. 11066 */ 11067 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11068 11069 rp = VTOR4(vp); 11070 11071 /* 11072 * The way that the address space of this process deletes its mapping 11073 * of this file is via the following call chains: 11074 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11075 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11076 * 11077 * With the use of address space callbacks we are allowed to drop the 11078 * address space lock, a_lock, while executing the NFS operations that 11079 * need to go over the wire. Returning EAGAIN to the caller of this 11080 * function is what drives the execution of the callback that we add 11081 * below. The callback will be executed by the address space code 11082 * after dropping the a_lock. When the callback is finished, since 11083 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 11084 * is called again on the same segment to finish the rest of the work 11085 * that needs to happen during unmapping. 11086 * 11087 * This action of calling back into the segment driver causes 11088 * nfs4_delmap() to get called again, but since the callback was 11089 * already executed at this point, it already did the work and there 11090 * is nothing left for us to do. 11091 * 11092 * To Summarize: 11093 * - The first time nfs4_delmap is called by the current thread is when 11094 * we add the caller associated with this delmap to the delmap caller 11095 * list, add the callback, and return EAGAIN. 11096 * - The second time in this call chain when nfs4_delmap is called we 11097 * will find this caller in the delmap caller list and realize there 11098 * is no more work to do thus removing this caller from the list and 11099 * returning the error that was set in the callback execution. 11100 */ 11101 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 11102 if (caller_found) { 11103 /* 11104 * 'error' is from the actual delmap operations. To avoid 11105 * hangs, we need to handle the return of EAGAIN differently 11106 * since this is what drives the callback execution. 11107 * In this case, we don't want to return EAGAIN and do the 11108 * callback execution because there are none to execute. 11109 */ 11110 if (error == EAGAIN) 11111 return (0); 11112 else 11113 return (error); 11114 } 11115 11116 /* current caller was not in the list */ 11117 delmap_call = nfs4_init_delmapcall(); 11118 11119 mutex_enter(&rp->r_statelock); 11120 list_insert_tail(&rp->r_indelmap, delmap_call); 11121 mutex_exit(&rp->r_statelock); 11122 11123 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 11124 11125 dmapp->vp = vp; 11126 dmapp->off = off; 11127 dmapp->addr = addr; 11128 dmapp->len = len; 11129 dmapp->prot = prot; 11130 dmapp->maxprot = maxprot; 11131 dmapp->flags = flags; 11132 dmapp->cr = cr; 11133 dmapp->caller = delmap_call; 11134 11135 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 11136 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 11137 11138 return (error ? error : EAGAIN); 11139 } 11140 11141 static nfs4_delmapcall_t * 11142 nfs4_init_delmapcall() 11143 { 11144 nfs4_delmapcall_t *delmap_call; 11145 11146 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 11147 delmap_call->call_id = curthread; 11148 delmap_call->error = 0; 11149 11150 return (delmap_call); 11151 } 11152 11153 static void 11154 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 11155 { 11156 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 11157 } 11158 11159 /* 11160 * Searches for the current delmap caller (based on curthread) in the list of 11161 * callers. If it is found, we remove it and free the delmap caller. 11162 * Returns: 11163 * 0 if the caller wasn't found 11164 * 1 if the caller was found, removed and freed. *errp will be set 11165 * to what the result of the delmap was. 11166 */ 11167 static int 11168 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 11169 { 11170 nfs4_delmapcall_t *delmap_call; 11171 11172 /* 11173 * If the list doesn't exist yet, we create it and return 11174 * that the caller wasn't found. No list = no callers. 11175 */ 11176 mutex_enter(&rp->r_statelock); 11177 if (!(rp->r_flags & R4DELMAPLIST)) { 11178 /* The list does not exist */ 11179 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 11180 offsetof(nfs4_delmapcall_t, call_node)); 11181 rp->r_flags |= R4DELMAPLIST; 11182 mutex_exit(&rp->r_statelock); 11183 return (0); 11184 } else { 11185 /* The list exists so search it */ 11186 for (delmap_call = list_head(&rp->r_indelmap); 11187 delmap_call != NULL; 11188 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 11189 if (delmap_call->call_id == curthread) { 11190 /* current caller is in the list */ 11191 *errp = delmap_call->error; 11192 list_remove(&rp->r_indelmap, delmap_call); 11193 mutex_exit(&rp->r_statelock); 11194 nfs4_free_delmapcall(delmap_call); 11195 return (1); 11196 } 11197 } 11198 } 11199 mutex_exit(&rp->r_statelock); 11200 return (0); 11201 } 11202 11203 /* 11204 * Remove some pages from an mmap'd vnode. Just update the 11205 * count of pages. If doing close-to-open, then flush and 11206 * commit all of the pages associated with this file. 11207 * Otherwise, start an asynchronous page flush to write out 11208 * any dirty pages. This will also associate a credential 11209 * with the rnode which can be used to write the pages. 11210 */ 11211 /* ARGSUSED */ 11212 static void 11213 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11214 { 11215 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11216 rnode4_t *rp; 11217 mntinfo4_t *mi; 11218 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11219 11220 rp = VTOR4(dmapp->vp); 11221 mi = VTOMI4(dmapp->vp); 11222 11223 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11224 ASSERT(rp->r_mapcnt >= 0); 11225 11226 /* 11227 * Initiate a page flush and potential commit if there are 11228 * pages, the file system was not mounted readonly, the segment 11229 * was mapped shared, and the pages themselves were writeable. 11230 */ 11231 if (nfs4_has_pages(dmapp->vp) && 11232 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11233 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11234 mutex_enter(&rp->r_statelock); 11235 rp->r_flags |= R4DIRTY; 11236 mutex_exit(&rp->r_statelock); 11237 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11238 dmapp->len, dmapp->cr); 11239 if (!e.error) { 11240 mutex_enter(&rp->r_statelock); 11241 e.error = rp->r_error; 11242 rp->r_error = 0; 11243 mutex_exit(&rp->r_statelock); 11244 } 11245 } else 11246 e.error = 0; 11247 11248 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11249 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11250 B_INVAL, dmapp->cr, NULL); 11251 11252 if (e.error) { 11253 e.stat = puterrno4(e.error); 11254 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11255 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11256 dmapp->caller->error = e.error; 11257 } 11258 11259 /* Check to see if we need to close the file */ 11260 11261 if (dmapp->vp->v_type == VREG) { 11262 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11263 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11264 11265 if (e.error != 0 || e.stat != NFS4_OK) { 11266 /* 11267 * Since it is possible that e.error == 0 and 11268 * e.stat != NFS4_OK (and vice versa), 11269 * we do the proper checking in order to get both 11270 * e.error and e.stat reporting the correct info. 11271 */ 11272 if (e.stat == NFS4_OK) 11273 e.stat = puterrno4(e.error); 11274 if (e.error == 0) 11275 e.error = geterrno4(e.stat); 11276 11277 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11278 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11279 dmapp->caller->error = e.error; 11280 } 11281 } 11282 11283 (void) as_delete_callback(as, arg); 11284 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11285 } 11286 11287 11288 static uint_t 11289 fattr4_maxfilesize_to_bits(uint64_t ll) 11290 { 11291 uint_t l = 1; 11292 11293 if (ll == 0) { 11294 return (0); 11295 } 11296 11297 if (ll & 0xffffffff00000000) { 11298 l += 32; ll >>= 32; 11299 } 11300 if (ll & 0xffff0000) { 11301 l += 16; ll >>= 16; 11302 } 11303 if (ll & 0xff00) { 11304 l += 8; ll >>= 8; 11305 } 11306 if (ll & 0xf0) { 11307 l += 4; ll >>= 4; 11308 } 11309 if (ll & 0xc) { 11310 l += 2; ll >>= 2; 11311 } 11312 if (ll & 0x2) { 11313 l += 1; 11314 } 11315 return (l); 11316 } 11317 11318 static int 11319 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr) 11320 { 11321 vnode_t *avp = NULL; 11322 int error; 11323 11324 if ((error = nfs4lookup_xattr(vp, "", &avp, 11325 LOOKUP_XATTR, cr)) == 0) 11326 error = do_xattr_exists_check(avp, valp, cr); 11327 if (avp) 11328 VN_RELE(avp); 11329 11330 return (error); 11331 } 11332 11333 /* ARGSUSED */ 11334 int 11335 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 11336 caller_context_t *ct) 11337 { 11338 int error; 11339 hrtime_t t; 11340 rnode4_t *rp; 11341 nfs4_ga_res_t gar; 11342 nfs4_ga_ext_res_t ger; 11343 11344 gar.n4g_ext_res = &ger; 11345 11346 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11347 return (EIO); 11348 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11349 *valp = MAXPATHLEN; 11350 return (0); 11351 } 11352 if (cmd == _PC_ACL_ENABLED) { 11353 *valp = _ACL_ACE_ENABLED; 11354 return (0); 11355 } 11356 11357 rp = VTOR4(vp); 11358 if (cmd == _PC_XATTR_EXISTS) { 11359 /* 11360 * The existence of the xattr directory is not sufficient 11361 * for determining whether generic user attributes exists. 11362 * The attribute directory could only be a transient directory 11363 * used for Solaris sysattr support. Do a small readdir 11364 * to verify if the only entries are sysattrs or not. 11365 * 11366 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11367 * is NULL. Once the xadir vp exists, we can create xattrs, 11368 * and we don't have any way to update the "base" object's 11369 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11370 * could help out. 11371 */ 11372 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11373 rp->r_xattr_dir == NULL) { 11374 return (nfs4_have_xattrs(vp, valp, cr)); 11375 } 11376 } else { /* OLD CODE */ 11377 if (ATTRCACHE4_VALID(vp)) { 11378 mutex_enter(&rp->r_statelock); 11379 if (rp->r_pathconf.pc4_cache_valid) { 11380 error = 0; 11381 switch (cmd) { 11382 case _PC_FILESIZEBITS: 11383 *valp = 11384 rp->r_pathconf.pc4_filesizebits; 11385 break; 11386 case _PC_LINK_MAX: 11387 *valp = 11388 rp->r_pathconf.pc4_link_max; 11389 break; 11390 case _PC_NAME_MAX: 11391 *valp = 11392 rp->r_pathconf.pc4_name_max; 11393 break; 11394 case _PC_CHOWN_RESTRICTED: 11395 *valp = 11396 rp->r_pathconf.pc4_chown_restricted; 11397 break; 11398 case _PC_NO_TRUNC: 11399 *valp = 11400 rp->r_pathconf.pc4_no_trunc; 11401 break; 11402 default: 11403 error = EINVAL; 11404 break; 11405 } 11406 mutex_exit(&rp->r_statelock); 11407 #ifdef DEBUG 11408 nfs4_pathconf_cache_hits++; 11409 #endif 11410 return (error); 11411 } 11412 mutex_exit(&rp->r_statelock); 11413 } 11414 } 11415 #ifdef DEBUG 11416 nfs4_pathconf_cache_misses++; 11417 #endif 11418 11419 t = gethrtime(); 11420 11421 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11422 11423 if (error) { 11424 mutex_enter(&rp->r_statelock); 11425 rp->r_pathconf.pc4_cache_valid = FALSE; 11426 rp->r_pathconf.pc4_xattr_valid = FALSE; 11427 mutex_exit(&rp->r_statelock); 11428 return (error); 11429 } 11430 11431 /* interpret the max filesize */ 11432 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11433 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11434 11435 /* Store the attributes we just received */ 11436 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11437 11438 switch (cmd) { 11439 case _PC_FILESIZEBITS: 11440 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11441 break; 11442 case _PC_LINK_MAX: 11443 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11444 break; 11445 case _PC_NAME_MAX: 11446 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11447 break; 11448 case _PC_CHOWN_RESTRICTED: 11449 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11450 break; 11451 case _PC_NO_TRUNC: 11452 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11453 break; 11454 case _PC_XATTR_EXISTS: 11455 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) { 11456 if (error = nfs4_have_xattrs(vp, valp, cr)) 11457 return (error); 11458 } 11459 break; 11460 default: 11461 return (EINVAL); 11462 } 11463 11464 return (0); 11465 } 11466 11467 /* 11468 * Called by async thread to do synchronous pageio. Do the i/o, wait 11469 * for it to complete, and cleanup the page list when done. 11470 */ 11471 static int 11472 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11473 int flags, cred_t *cr) 11474 { 11475 int error; 11476 11477 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11478 11479 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11480 if (flags & B_READ) 11481 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11482 else 11483 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11484 return (error); 11485 } 11486 11487 /* ARGSUSED */ 11488 static int 11489 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11490 int flags, cred_t *cr, caller_context_t *ct) 11491 { 11492 int error; 11493 rnode4_t *rp; 11494 11495 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11496 return (EIO); 11497 11498 if (pp == NULL) 11499 return (EINVAL); 11500 11501 rp = VTOR4(vp); 11502 mutex_enter(&rp->r_statelock); 11503 rp->r_count++; 11504 mutex_exit(&rp->r_statelock); 11505 11506 if (flags & B_ASYNC) { 11507 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11508 nfs4_sync_pageio); 11509 } else 11510 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11511 mutex_enter(&rp->r_statelock); 11512 rp->r_count--; 11513 cv_broadcast(&rp->r_cv); 11514 mutex_exit(&rp->r_statelock); 11515 return (error); 11516 } 11517 11518 /* ARGSUSED */ 11519 static void 11520 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr, 11521 caller_context_t *ct) 11522 { 11523 int error; 11524 rnode4_t *rp; 11525 page_t *plist; 11526 page_t *pptr; 11527 offset3 offset; 11528 count3 len; 11529 k_sigset_t smask; 11530 11531 /* 11532 * We should get called with fl equal to either B_FREE or 11533 * B_INVAL. Any other value is illegal. 11534 * 11535 * The page that we are either supposed to free or destroy 11536 * should be exclusive locked and its io lock should not 11537 * be held. 11538 */ 11539 ASSERT(fl == B_FREE || fl == B_INVAL); 11540 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11541 11542 rp = VTOR4(vp); 11543 11544 /* 11545 * If the page doesn't need to be committed or we shouldn't 11546 * even bother attempting to commit it, then just make sure 11547 * that the p_fsdata byte is clear and then either free or 11548 * destroy the page as appropriate. 11549 */ 11550 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11551 pp->p_fsdata = C_NOCOMMIT; 11552 if (fl == B_FREE) 11553 page_free(pp, dn); 11554 else 11555 page_destroy(pp, dn); 11556 return; 11557 } 11558 11559 /* 11560 * If there is a page invalidation operation going on, then 11561 * if this is one of the pages being destroyed, then just 11562 * clear the p_fsdata byte and then either free or destroy 11563 * the page as appropriate. 11564 */ 11565 mutex_enter(&rp->r_statelock); 11566 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11567 mutex_exit(&rp->r_statelock); 11568 pp->p_fsdata = C_NOCOMMIT; 11569 if (fl == B_FREE) 11570 page_free(pp, dn); 11571 else 11572 page_destroy(pp, dn); 11573 return; 11574 } 11575 11576 /* 11577 * If we are freeing this page and someone else is already 11578 * waiting to do a commit, then just unlock the page and 11579 * return. That other thread will take care of commiting 11580 * this page. The page can be freed sometime after the 11581 * commit has finished. Otherwise, if the page is marked 11582 * as delay commit, then we may be getting called from 11583 * pvn_write_done, one page at a time. This could result 11584 * in one commit per page, so we end up doing lots of small 11585 * commits instead of fewer larger commits. This is bad, 11586 * we want do as few commits as possible. 11587 */ 11588 if (fl == B_FREE) { 11589 if (rp->r_flags & R4COMMITWAIT) { 11590 page_unlock(pp); 11591 mutex_exit(&rp->r_statelock); 11592 return; 11593 } 11594 if (pp->p_fsdata == C_DELAYCOMMIT) { 11595 pp->p_fsdata = C_COMMIT; 11596 page_unlock(pp); 11597 mutex_exit(&rp->r_statelock); 11598 return; 11599 } 11600 } 11601 11602 /* 11603 * Check to see if there is a signal which would prevent an 11604 * attempt to commit the pages from being successful. If so, 11605 * then don't bother with all of the work to gather pages and 11606 * generate the unsuccessful RPC. Just return from here and 11607 * let the page be committed at some later time. 11608 */ 11609 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11610 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11611 sigunintr(&smask); 11612 page_unlock(pp); 11613 mutex_exit(&rp->r_statelock); 11614 return; 11615 } 11616 sigunintr(&smask); 11617 11618 /* 11619 * We are starting to need to commit pages, so let's try 11620 * to commit as many as possible at once to reduce the 11621 * overhead. 11622 * 11623 * Set the `commit inprogress' state bit. We must 11624 * first wait until any current one finishes. Then 11625 * we initialize the c_pages list with this page. 11626 */ 11627 while (rp->r_flags & R4COMMIT) { 11628 rp->r_flags |= R4COMMITWAIT; 11629 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11630 rp->r_flags &= ~R4COMMITWAIT; 11631 } 11632 rp->r_flags |= R4COMMIT; 11633 mutex_exit(&rp->r_statelock); 11634 ASSERT(rp->r_commit.c_pages == NULL); 11635 rp->r_commit.c_pages = pp; 11636 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11637 rp->r_commit.c_commlen = PAGESIZE; 11638 11639 /* 11640 * Gather together all other pages which can be committed. 11641 * They will all be chained off r_commit.c_pages. 11642 */ 11643 nfs4_get_commit(vp); 11644 11645 /* 11646 * Clear the `commit inprogress' status and disconnect 11647 * the list of pages to be committed from the rnode. 11648 * At this same time, we also save the starting offset 11649 * and length of data to be committed on the server. 11650 */ 11651 plist = rp->r_commit.c_pages; 11652 rp->r_commit.c_pages = NULL; 11653 offset = rp->r_commit.c_commbase; 11654 len = rp->r_commit.c_commlen; 11655 mutex_enter(&rp->r_statelock); 11656 rp->r_flags &= ~R4COMMIT; 11657 cv_broadcast(&rp->r_commit.c_cv); 11658 mutex_exit(&rp->r_statelock); 11659 11660 if (curproc == proc_pageout || curproc == proc_fsflush || 11661 nfs_zone() != VTOMI4(vp)->mi_zone) { 11662 nfs4_async_commit(vp, plist, offset, len, 11663 cr, do_nfs4_async_commit); 11664 return; 11665 } 11666 11667 /* 11668 * Actually generate the COMMIT op over the wire operation. 11669 */ 11670 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11671 11672 /* 11673 * If we got an error during the commit, just unlock all 11674 * of the pages. The pages will get retransmitted to the 11675 * server during a putpage operation. 11676 */ 11677 if (error) { 11678 while (plist != NULL) { 11679 pptr = plist; 11680 page_sub(&plist, pptr); 11681 page_unlock(pptr); 11682 } 11683 return; 11684 } 11685 11686 /* 11687 * We've tried as hard as we can to commit the data to stable 11688 * storage on the server. We just unlock the rest of the pages 11689 * and clear the commit required state. They will be put 11690 * onto the tail of the cachelist if they are nolonger 11691 * mapped. 11692 */ 11693 while (plist != pp) { 11694 pptr = plist; 11695 page_sub(&plist, pptr); 11696 pptr->p_fsdata = C_NOCOMMIT; 11697 page_unlock(pptr); 11698 } 11699 11700 /* 11701 * It is possible that nfs4_commit didn't return error but 11702 * some other thread has modified the page we are going 11703 * to free/destroy. 11704 * In this case we need to rewrite the page. Do an explicit check 11705 * before attempting to free/destroy the page. If modified, needs to 11706 * be rewritten so unlock the page and return. 11707 */ 11708 if (hat_ismod(pp)) { 11709 pp->p_fsdata = C_NOCOMMIT; 11710 page_unlock(pp); 11711 return; 11712 } 11713 11714 /* 11715 * Now, as appropriate, either free or destroy the page 11716 * that we were called with. 11717 */ 11718 pp->p_fsdata = C_NOCOMMIT; 11719 if (fl == B_FREE) 11720 page_free(pp, dn); 11721 else 11722 page_destroy(pp, dn); 11723 } 11724 11725 /* 11726 * Commit requires that the current fh be the file written to. 11727 * The compound op structure is: 11728 * PUTFH(file), COMMIT 11729 */ 11730 static int 11731 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11732 { 11733 COMPOUND4args_clnt args; 11734 COMPOUND4res_clnt res; 11735 COMMIT4res *cm_res; 11736 nfs_argop4 argop[2]; 11737 nfs_resop4 *resop; 11738 int doqueue; 11739 mntinfo4_t *mi; 11740 rnode4_t *rp; 11741 cred_t *cred_otw = NULL; 11742 bool_t needrecov = FALSE; 11743 nfs4_recov_state_t recov_state; 11744 nfs4_open_stream_t *osp = NULL; 11745 bool_t first_time = TRUE; /* first time getting OTW cred */ 11746 bool_t last_time = FALSE; /* last time getting OTW cred */ 11747 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11748 11749 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11750 11751 rp = VTOR4(vp); 11752 11753 mi = VTOMI4(vp); 11754 recov_state.rs_flags = 0; 11755 recov_state.rs_num_retry_despite_err = 0; 11756 get_commit_cred: 11757 /* 11758 * Releases the osp, if a valid open stream is provided. 11759 * Puts a hold on the cred_otw and the new osp (if found). 11760 */ 11761 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11762 &first_time, &last_time); 11763 args.ctag = TAG_COMMIT; 11764 recov_retry: 11765 /* 11766 * Commit ops: putfh file; commit 11767 */ 11768 args.array_len = 2; 11769 args.array = argop; 11770 11771 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11772 &recov_state, NULL); 11773 if (e.error) { 11774 crfree(cred_otw); 11775 if (osp != NULL) 11776 open_stream_rele(osp, rp); 11777 return (e.error); 11778 } 11779 11780 /* putfh directory */ 11781 argop[0].argop = OP_CPUTFH; 11782 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11783 11784 /* commit */ 11785 argop[1].argop = OP_COMMIT; 11786 argop[1].nfs_argop4_u.opcommit.offset = offset; 11787 argop[1].nfs_argop4_u.opcommit.count = count; 11788 11789 doqueue = 1; 11790 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11791 11792 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11793 if (!needrecov && e.error) { 11794 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11795 needrecov); 11796 crfree(cred_otw); 11797 if (e.error == EACCES && last_time == FALSE) 11798 goto get_commit_cred; 11799 if (osp != NULL) 11800 open_stream_rele(osp, rp); 11801 return (e.error); 11802 } 11803 11804 if (needrecov) { 11805 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11806 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) { 11807 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11808 &recov_state, needrecov); 11809 if (!e.error) 11810 (void) xdr_free(xdr_COMPOUND4res_clnt, 11811 (caddr_t)&res); 11812 goto recov_retry; 11813 } 11814 if (e.error) { 11815 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11816 &recov_state, needrecov); 11817 crfree(cred_otw); 11818 if (osp != NULL) 11819 open_stream_rele(osp, rp); 11820 return (e.error); 11821 } 11822 /* fall through for res.status case */ 11823 } 11824 11825 if (res.status) { 11826 e.error = geterrno4(res.status); 11827 if (e.error == EACCES && last_time == FALSE) { 11828 crfree(cred_otw); 11829 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11830 &recov_state, needrecov); 11831 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11832 goto get_commit_cred; 11833 } 11834 /* 11835 * Can't do a nfs4_purge_stale_fh here because this 11836 * can cause a deadlock. nfs4_commit can 11837 * be called from nfs4_dispose which can be called 11838 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11839 * can call back to pvn_vplist_dirty. 11840 */ 11841 if (e.error == ESTALE) { 11842 mutex_enter(&rp->r_statelock); 11843 rp->r_flags |= R4STALE; 11844 if (!rp->r_error) 11845 rp->r_error = e.error; 11846 mutex_exit(&rp->r_statelock); 11847 PURGE_ATTRCACHE4(vp); 11848 } else { 11849 mutex_enter(&rp->r_statelock); 11850 if (!rp->r_error) 11851 rp->r_error = e.error; 11852 mutex_exit(&rp->r_statelock); 11853 } 11854 } else { 11855 ASSERT(rp->r_flags & R4HAVEVERF); 11856 resop = &res.array[1]; /* commit res */ 11857 cm_res = &resop->nfs_resop4_u.opcommit; 11858 mutex_enter(&rp->r_statelock); 11859 if (cm_res->writeverf == rp->r_writeverf) { 11860 mutex_exit(&rp->r_statelock); 11861 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11862 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11863 &recov_state, needrecov); 11864 crfree(cred_otw); 11865 if (osp != NULL) 11866 open_stream_rele(osp, rp); 11867 return (0); 11868 } 11869 nfs4_set_mod(vp); 11870 rp->r_writeverf = cm_res->writeverf; 11871 mutex_exit(&rp->r_statelock); 11872 e.error = NFS_VERF_MISMATCH; 11873 } 11874 11875 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11876 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11877 crfree(cred_otw); 11878 if (osp != NULL) 11879 open_stream_rele(osp, rp); 11880 11881 return (e.error); 11882 } 11883 11884 static void 11885 nfs4_set_mod(vnode_t *vp) 11886 { 11887 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11888 11889 /* make sure we're looking at the master vnode, not a shadow */ 11890 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check); 11891 } 11892 11893 /* 11894 * This function is used to gather a page list of the pages which 11895 * can be committed on the server. 11896 * 11897 * The calling thread must have set R4COMMIT. This bit is used to 11898 * serialize access to the commit structure in the rnode. As long 11899 * as the thread has set R4COMMIT, then it can manipulate the commit 11900 * structure without requiring any other locks. 11901 * 11902 * When this function is called from nfs4_dispose() the page passed 11903 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11904 * will skip it. This is not a problem since we initially add the 11905 * page to the r_commit page list. 11906 * 11907 */ 11908 static void 11909 nfs4_get_commit(vnode_t *vp) 11910 { 11911 rnode4_t *rp; 11912 page_t *pp; 11913 kmutex_t *vphm; 11914 11915 rp = VTOR4(vp); 11916 11917 ASSERT(rp->r_flags & R4COMMIT); 11918 11919 /* make sure we're looking at the master vnode, not a shadow */ 11920 11921 if (IS_SHADOW(vp, rp)) 11922 vp = RTOV4(rp); 11923 11924 vphm = page_vnode_mutex(vp); 11925 mutex_enter(vphm); 11926 11927 /* 11928 * If there are no pages associated with this vnode, then 11929 * just return. 11930 */ 11931 if ((pp = vp->v_pages) == NULL) { 11932 mutex_exit(vphm); 11933 return; 11934 } 11935 11936 /* 11937 * Step through all of the pages associated with this vnode 11938 * looking for pages which need to be committed. 11939 */ 11940 do { 11941 /* Skip marker pages. */ 11942 if (pp->p_hash == PVN_VPLIST_HASH_TAG) 11943 continue; 11944 11945 /* 11946 * First short-cut everything (without the page_lock) 11947 * and see if this page does not need to be committed 11948 * or is modified if so then we'll just skip it. 11949 */ 11950 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11951 continue; 11952 11953 /* 11954 * Attempt to lock the page. If we can't, then 11955 * someone else is messing with it or we have been 11956 * called from nfs4_dispose and this is the page that 11957 * nfs4_dispose was called with.. anyway just skip it. 11958 */ 11959 if (!page_trylock(pp, SE_EXCL)) 11960 continue; 11961 11962 /* 11963 * Lets check again now that we have the page lock. 11964 */ 11965 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11966 page_unlock(pp); 11967 continue; 11968 } 11969 11970 /* this had better not be a free page */ 11971 ASSERT(PP_ISFREE(pp) == 0); 11972 11973 /* 11974 * The page needs to be committed and we locked it. 11975 * Update the base and length parameters and add it 11976 * to r_pages. 11977 */ 11978 if (rp->r_commit.c_pages == NULL) { 11979 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11980 rp->r_commit.c_commlen = PAGESIZE; 11981 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11982 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11983 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11984 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11985 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11986 <= pp->p_offset) { 11987 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11988 rp->r_commit.c_commbase + PAGESIZE; 11989 } 11990 page_add(&rp->r_commit.c_pages, pp); 11991 } while ((pp = pp->p_vpnext) != vp->v_pages); 11992 11993 mutex_exit(vphm); 11994 } 11995 11996 /* 11997 * This routine is used to gather together a page list of the pages 11998 * which are to be committed on the server. This routine must not 11999 * be called if the calling thread holds any locked pages. 12000 * 12001 * The calling thread must have set R4COMMIT. This bit is used to 12002 * serialize access to the commit structure in the rnode. As long 12003 * as the thread has set R4COMMIT, then it can manipulate the commit 12004 * structure without requiring any other locks. 12005 */ 12006 static void 12007 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 12008 { 12009 12010 rnode4_t *rp; 12011 page_t *pp; 12012 u_offset_t end; 12013 u_offset_t off; 12014 ASSERT(len != 0); 12015 rp = VTOR4(vp); 12016 ASSERT(rp->r_flags & R4COMMIT); 12017 12018 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12019 12020 /* make sure we're looking at the master vnode, not a shadow */ 12021 12022 if (IS_SHADOW(vp, rp)) 12023 vp = RTOV4(rp); 12024 12025 /* 12026 * If there are no pages associated with this vnode, then 12027 * just return. 12028 */ 12029 if ((pp = vp->v_pages) == NULL) 12030 return; 12031 /* 12032 * Calculate the ending offset. 12033 */ 12034 end = soff + len; 12035 for (off = soff; off < end; off += PAGESIZE) { 12036 /* 12037 * Lookup each page by vp, offset. 12038 */ 12039 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 12040 continue; 12041 /* 12042 * If this page does not need to be committed or is 12043 * modified, then just skip it. 12044 */ 12045 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 12046 page_unlock(pp); 12047 continue; 12048 } 12049 12050 ASSERT(PP_ISFREE(pp) == 0); 12051 /* 12052 * The page needs to be committed and we locked it. 12053 * Update the base and length parameters and add it 12054 * to r_pages. 12055 */ 12056 if (rp->r_commit.c_pages == NULL) { 12057 rp->r_commit.c_commbase = (offset3)pp->p_offset; 12058 rp->r_commit.c_commlen = PAGESIZE; 12059 } else { 12060 rp->r_commit.c_commlen = (offset3)pp->p_offset - 12061 rp->r_commit.c_commbase + PAGESIZE; 12062 } 12063 page_add(&rp->r_commit.c_pages, pp); 12064 } 12065 } 12066 12067 /* 12068 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 12069 * Flushes and commits data to the server. 12070 */ 12071 static int 12072 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 12073 { 12074 int error; 12075 verifier4 write_verf; 12076 rnode4_t *rp = VTOR4(vp); 12077 12078 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12079 12080 /* 12081 * Flush the data portion of the file and then commit any 12082 * portions which need to be committed. This may need to 12083 * be done twice if the server has changed state since 12084 * data was last written. The data will need to be 12085 * rewritten to the server and then a new commit done. 12086 * 12087 * In fact, this may need to be done several times if the 12088 * server is having problems and crashing while we are 12089 * attempting to do this. 12090 */ 12091 12092 top: 12093 /* 12094 * Do a flush based on the poff and plen arguments. This 12095 * will synchronously write out any modified pages in the 12096 * range specified by (poff, plen). This starts all of the 12097 * i/o operations which will be waited for in the next 12098 * call to nfs4_putpage 12099 */ 12100 12101 mutex_enter(&rp->r_statelock); 12102 write_verf = rp->r_writeverf; 12103 mutex_exit(&rp->r_statelock); 12104 12105 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL); 12106 if (error == EAGAIN) 12107 error = 0; 12108 12109 /* 12110 * Do a flush based on the poff and plen arguments. This 12111 * will synchronously write out any modified pages in the 12112 * range specified by (poff, plen) and wait until all of 12113 * the asynchronous i/o's in that range are done as well. 12114 */ 12115 if (!error) 12116 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL); 12117 12118 if (error) 12119 return (error); 12120 12121 mutex_enter(&rp->r_statelock); 12122 if (rp->r_writeverf != write_verf) { 12123 mutex_exit(&rp->r_statelock); 12124 goto top; 12125 } 12126 mutex_exit(&rp->r_statelock); 12127 12128 /* 12129 * Now commit any pages which might need to be committed. 12130 * If the error, NFS_VERF_MISMATCH, is returned, then 12131 * start over with the flush operation. 12132 */ 12133 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 12134 12135 if (error == NFS_VERF_MISMATCH) 12136 goto top; 12137 12138 return (error); 12139 } 12140 12141 /* 12142 * nfs4_commit_vp() will wait for other pending commits and 12143 * will either commit the whole file or a range, plen dictates 12144 * if we commit whole file. a value of zero indicates the whole 12145 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 12146 */ 12147 static int 12148 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 12149 cred_t *cr, int wait_on_writes) 12150 { 12151 rnode4_t *rp; 12152 page_t *plist; 12153 offset3 offset; 12154 count3 len; 12155 12156 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12157 12158 rp = VTOR4(vp); 12159 12160 /* 12161 * before we gather commitable pages make 12162 * sure there are no outstanding async writes 12163 */ 12164 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 12165 mutex_enter(&rp->r_statelock); 12166 while (rp->r_count > 0) { 12167 cv_wait(&rp->r_cv, &rp->r_statelock); 12168 } 12169 mutex_exit(&rp->r_statelock); 12170 } 12171 12172 /* 12173 * Set the `commit inprogress' state bit. We must 12174 * first wait until any current one finishes. 12175 */ 12176 mutex_enter(&rp->r_statelock); 12177 while (rp->r_flags & R4COMMIT) { 12178 rp->r_flags |= R4COMMITWAIT; 12179 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 12180 rp->r_flags &= ~R4COMMITWAIT; 12181 } 12182 rp->r_flags |= R4COMMIT; 12183 mutex_exit(&rp->r_statelock); 12184 12185 /* 12186 * Gather all of the pages which need to be 12187 * committed. 12188 */ 12189 if (plen == 0) 12190 nfs4_get_commit(vp); 12191 else 12192 nfs4_get_commit_range(vp, poff, plen); 12193 12194 /* 12195 * Clear the `commit inprogress' bit and disconnect the 12196 * page list which was gathered by nfs4_get_commit. 12197 */ 12198 plist = rp->r_commit.c_pages; 12199 rp->r_commit.c_pages = NULL; 12200 offset = rp->r_commit.c_commbase; 12201 len = rp->r_commit.c_commlen; 12202 mutex_enter(&rp->r_statelock); 12203 rp->r_flags &= ~R4COMMIT; 12204 cv_broadcast(&rp->r_commit.c_cv); 12205 mutex_exit(&rp->r_statelock); 12206 12207 /* 12208 * If any pages need to be committed, commit them and 12209 * then unlock them so that they can be freed some 12210 * time later. 12211 */ 12212 if (plist == NULL) 12213 return (0); 12214 12215 /* 12216 * No error occurred during the flush portion 12217 * of this operation, so now attempt to commit 12218 * the data to stable storage on the server. 12219 * 12220 * This will unlock all of the pages on the list. 12221 */ 12222 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12223 } 12224 12225 static int 12226 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12227 cred_t *cr) 12228 { 12229 int error; 12230 page_t *pp; 12231 12232 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12233 12234 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12235 12236 /* 12237 * If we got an error, then just unlock all of the pages 12238 * on the list. 12239 */ 12240 if (error) { 12241 while (plist != NULL) { 12242 pp = plist; 12243 page_sub(&plist, pp); 12244 page_unlock(pp); 12245 } 12246 return (error); 12247 } 12248 /* 12249 * We've tried as hard as we can to commit the data to stable 12250 * storage on the server. We just unlock the pages and clear 12251 * the commit required state. They will get freed later. 12252 */ 12253 while (plist != NULL) { 12254 pp = plist; 12255 page_sub(&plist, pp); 12256 pp->p_fsdata = C_NOCOMMIT; 12257 page_unlock(pp); 12258 } 12259 12260 return (error); 12261 } 12262 12263 static void 12264 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12265 cred_t *cr) 12266 { 12267 12268 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12269 } 12270 12271 /*ARGSUSED*/ 12272 static int 12273 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12274 caller_context_t *ct) 12275 { 12276 int error = 0; 12277 mntinfo4_t *mi; 12278 vattr_t va; 12279 vsecattr_t nfsace4_vsap; 12280 12281 mi = VTOMI4(vp); 12282 if (nfs_zone() != mi->mi_zone) 12283 return (EIO); 12284 if (mi->mi_flags & MI4_ACL) { 12285 /* if we have a delegation, return it */ 12286 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12287 (void) nfs4delegreturn(VTOR4(vp), 12288 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12289 12290 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12291 NFS4_ACL_SET); 12292 if (error) /* EINVAL */ 12293 return (error); 12294 12295 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12296 /* 12297 * These are aclent_t type entries. 12298 */ 12299 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12300 vp->v_type == VDIR, FALSE); 12301 if (error) 12302 return (error); 12303 } else { 12304 /* 12305 * These are ace_t type entries. 12306 */ 12307 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12308 FALSE); 12309 if (error) 12310 return (error); 12311 } 12312 bzero(&va, sizeof (va)); 12313 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12314 vs_ace4_destroy(&nfsace4_vsap); 12315 return (error); 12316 } 12317 return (ENOSYS); 12318 } 12319 12320 /* ARGSUSED */ 12321 int 12322 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12323 caller_context_t *ct) 12324 { 12325 int error; 12326 mntinfo4_t *mi; 12327 nfs4_ga_res_t gar; 12328 rnode4_t *rp = VTOR4(vp); 12329 12330 mi = VTOMI4(vp); 12331 if (nfs_zone() != mi->mi_zone) 12332 return (EIO); 12333 12334 bzero(&gar, sizeof (gar)); 12335 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12336 12337 /* 12338 * vsecattr->vsa_mask holds the original acl request mask. 12339 * This is needed when determining what to return. 12340 * (See: nfs4_create_getsecattr_return()) 12341 */ 12342 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12343 if (error) /* EINVAL */ 12344 return (error); 12345 12346 /* 12347 * If this is a referral stub, don't try to go OTW for an ACL 12348 */ 12349 if (RP_ISSTUB_REFERRAL(VTOR4(vp))) 12350 return (fs_fab_acl(vp, vsecattr, flag, cr, ct)); 12351 12352 if (mi->mi_flags & MI4_ACL) { 12353 /* 12354 * Check if the data is cached and the cache is valid. If it 12355 * is we don't go over the wire. 12356 */ 12357 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12358 mutex_enter(&rp->r_statelock); 12359 if (rp->r_secattr != NULL) { 12360 error = nfs4_create_getsecattr_return( 12361 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12362 rp->r_attr.va_gid, 12363 vp->v_type == VDIR); 12364 if (!error) { /* error == 0 - Success! */ 12365 mutex_exit(&rp->r_statelock); 12366 return (error); 12367 } 12368 } 12369 mutex_exit(&rp->r_statelock); 12370 } 12371 12372 /* 12373 * The getattr otw call will always get both the acl, in 12374 * the form of a list of nfsace4's, and the number of acl 12375 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12376 */ 12377 gar.n4g_va.va_mask = AT_ALL; 12378 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12379 if (error) { 12380 vs_ace4_destroy(&gar.n4g_vsa); 12381 if (error == ENOTSUP || error == EOPNOTSUPP) 12382 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12383 return (error); 12384 } 12385 12386 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12387 /* 12388 * No error was returned, but according to the response 12389 * bitmap, neither was an acl. 12390 */ 12391 vs_ace4_destroy(&gar.n4g_vsa); 12392 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12393 return (error); 12394 } 12395 12396 /* 12397 * Update the cache with the ACL. 12398 */ 12399 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12400 12401 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12402 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12403 vp->v_type == VDIR); 12404 vs_ace4_destroy(&gar.n4g_vsa); 12405 if ((error) && (vsecattr->vsa_mask & 12406 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12407 (error != EACCES)) { 12408 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12409 } 12410 return (error); 12411 } 12412 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12413 return (error); 12414 } 12415 12416 /* 12417 * The function returns: 12418 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12419 * - EINVAL if the passed in "acl_mask" is an invalid request. 12420 * 12421 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12422 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12423 * 12424 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12425 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12426 * - We have a count field set without the corresponding acl field set. (e.g. - 12427 * VSA_ACECNT is set, but VSA_ACE is not) 12428 */ 12429 static int 12430 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12431 { 12432 /* Shortcut the masks that are always valid. */ 12433 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12434 return (0); 12435 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12436 return (0); 12437 12438 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12439 /* 12440 * We can't have any VSA_ACL type stuff in the mask now. 12441 */ 12442 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12443 VSA_DFACLCNT)) 12444 return (EINVAL); 12445 12446 if (op == NFS4_ACL_SET) { 12447 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12448 return (EINVAL); 12449 } 12450 } 12451 12452 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12453 /* 12454 * We can't have any VSA_ACE type stuff in the mask now. 12455 */ 12456 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12457 return (EINVAL); 12458 12459 if (op == NFS4_ACL_SET) { 12460 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12461 return (EINVAL); 12462 12463 if ((acl_mask & VSA_DFACLCNT) && 12464 !(acl_mask & VSA_DFACL)) 12465 return (EINVAL); 12466 } 12467 } 12468 return (0); 12469 } 12470 12471 /* 12472 * The theory behind creating the correct getsecattr return is simply this: 12473 * "Don't return anything that the caller is not expecting to have to free." 12474 */ 12475 static int 12476 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12477 uid_t uid, gid_t gid, int isdir) 12478 { 12479 int error = 0; 12480 /* Save the mask since the translators modify it. */ 12481 uint_t orig_mask = vsap->vsa_mask; 12482 12483 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12484 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE); 12485 12486 if (error) 12487 return (error); 12488 12489 /* 12490 * If the caller only asked for the ace count (VSA_ACECNT) 12491 * don't give them the full acl (VSA_ACE), free it. 12492 */ 12493 if (!orig_mask & VSA_ACE) { 12494 if (vsap->vsa_aclentp != NULL) { 12495 kmem_free(vsap->vsa_aclentp, 12496 vsap->vsa_aclcnt * sizeof (ace_t)); 12497 vsap->vsa_aclentp = NULL; 12498 } 12499 } 12500 vsap->vsa_mask = orig_mask; 12501 12502 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12503 VSA_DFACLCNT)) { 12504 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12505 isdir, FALSE); 12506 12507 if (error) 12508 return (error); 12509 12510 /* 12511 * If the caller only asked for the acl count (VSA_ACLCNT) 12512 * and/or the default acl count (VSA_DFACLCNT) don't give them 12513 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12514 */ 12515 if (!orig_mask & VSA_ACL) { 12516 if (vsap->vsa_aclentp != NULL) { 12517 kmem_free(vsap->vsa_aclentp, 12518 vsap->vsa_aclcnt * sizeof (aclent_t)); 12519 vsap->vsa_aclentp = NULL; 12520 } 12521 } 12522 12523 if (!orig_mask & VSA_DFACL) { 12524 if (vsap->vsa_dfaclentp != NULL) { 12525 kmem_free(vsap->vsa_dfaclentp, 12526 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12527 vsap->vsa_dfaclentp = NULL; 12528 } 12529 } 12530 vsap->vsa_mask = orig_mask; 12531 } 12532 return (0); 12533 } 12534 12535 /* ARGSUSED */ 12536 int 12537 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr, 12538 caller_context_t *ct) 12539 { 12540 int error; 12541 12542 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12543 return (EIO); 12544 /* 12545 * check for valid cmd parameter 12546 */ 12547 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12548 return (EINVAL); 12549 12550 /* 12551 * Check access permissions 12552 */ 12553 if ((cmd & F_SHARE) && 12554 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12555 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12556 return (EBADF); 12557 12558 /* 12559 * If the filesystem is mounted using local locking, pass the 12560 * request off to the local share code. 12561 */ 12562 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12563 return (fs_shrlock(vp, cmd, shr, flag, cr, ct)); 12564 12565 switch (cmd) { 12566 case F_SHARE: 12567 case F_UNSHARE: 12568 /* 12569 * This will be properly implemented later, 12570 * see RFE: 4823948 . 12571 */ 12572 error = EAGAIN; 12573 break; 12574 12575 case F_HASREMOTELOCKS: 12576 /* 12577 * NFS client can't store remote locks itself 12578 */ 12579 shr->s_access = 0; 12580 error = 0; 12581 break; 12582 12583 default: 12584 error = EINVAL; 12585 break; 12586 } 12587 12588 return (error); 12589 } 12590 12591 /* 12592 * Common code called by directory ops to update the attrcache 12593 */ 12594 static int 12595 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12596 hrtime_t t, vnode_t *vp, cred_t *cr) 12597 { 12598 int error = 0; 12599 12600 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12601 12602 if (status != NFS4_OK) { 12603 /* getattr not done or failed */ 12604 PURGE_ATTRCACHE4(vp); 12605 return (error); 12606 } 12607 12608 if (garp) { 12609 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12610 } else { 12611 PURGE_ATTRCACHE4(vp); 12612 } 12613 return (error); 12614 } 12615 12616 /* 12617 * Update directory caches for directory modification ops (link, rename, etc.) 12618 * When dinfo is NULL, manage dircaches in the old way. 12619 */ 12620 static void 12621 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12622 dirattr_info_t *dinfo) 12623 { 12624 rnode4_t *drp = VTOR4(dvp); 12625 12626 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12627 12628 /* Purge rddir cache for dir since it changed */ 12629 if (drp->r_dir != NULL) 12630 nfs4_purge_rddir_cache(dvp); 12631 12632 /* 12633 * If caller provided dinfo, then use it to manage dir caches. 12634 */ 12635 if (dinfo != NULL) { 12636 if (vp != NULL) { 12637 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12638 if (!VTOR4(vp)->created_v4) { 12639 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12640 dnlc_update(dvp, nm, vp); 12641 } else { 12642 /* 12643 * XXX don't update if the created_v4 flag is 12644 * set 12645 */ 12646 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12647 NFS4_DEBUG(nfs4_client_state_debug, 12648 (CE_NOTE, "nfs4_update_dircaches: " 12649 "don't update dnlc: created_v4 flag")); 12650 } 12651 } 12652 12653 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12654 dinfo->di_cred, FALSE, cinfo); 12655 12656 return; 12657 } 12658 12659 /* 12660 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12661 * Since caller modified dir but didn't receive post-dirmod-op dir 12662 * attrs, the dir's attrs must be purged. 12663 * 12664 * XXX this check and dnlc update/purge should really be atomic, 12665 * XXX but can't use rnode statelock because it'll deadlock in 12666 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12667 * XXX does occur. 12668 * 12669 * XXX We also may want to check that atomic is true in the 12670 * XXX change_info struct. If it is not, the change_info may 12671 * XXX reflect changes by more than one clients which means that 12672 * XXX our cache may not be valid. 12673 */ 12674 PURGE_ATTRCACHE4(dvp); 12675 if (drp->r_change == cinfo->before) { 12676 /* no changes took place in the directory prior to our link */ 12677 if (vp != NULL) { 12678 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12679 if (!VTOR4(vp)->created_v4) { 12680 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12681 dnlc_update(dvp, nm, vp); 12682 } else { 12683 /* 12684 * XXX dont' update if the created_v4 flag 12685 * is set 12686 */ 12687 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12688 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12689 "nfs4_update_dircaches: don't" 12690 " update dnlc: created_v4 flag")); 12691 } 12692 } 12693 } else { 12694 /* Another client modified directory - purge its dnlc cache */ 12695 dnlc_purge_vp(dvp); 12696 } 12697 } 12698 12699 /* 12700 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12701 * file. 12702 * 12703 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12704 * file (ie: client recovery) and otherwise set to FALSE. 12705 * 12706 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12707 * initiated) calling functions. 12708 * 12709 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12710 * of resending a 'lost' open request. 12711 * 12712 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12713 * server that hands out BAD_SEQID on open confirm. 12714 * 12715 * Errors are returned via the nfs4_error_t parameter. 12716 */ 12717 void 12718 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12719 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12720 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12721 { 12722 COMPOUND4args_clnt args; 12723 COMPOUND4res_clnt res; 12724 nfs_argop4 argop[2]; 12725 nfs_resop4 *resop; 12726 int doqueue = 1; 12727 mntinfo4_t *mi; 12728 OPEN_CONFIRM4args *open_confirm_args; 12729 int needrecov; 12730 12731 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12732 #if DEBUG 12733 mutex_enter(&oop->oo_lock); 12734 ASSERT(oop->oo_seqid_inuse); 12735 mutex_exit(&oop->oo_lock); 12736 #endif 12737 12738 recov_retry_confirm: 12739 nfs4_error_zinit(ep); 12740 *retry_open = FALSE; 12741 12742 if (resend) 12743 args.ctag = TAG_OPEN_CONFIRM_LOST; 12744 else 12745 args.ctag = TAG_OPEN_CONFIRM; 12746 12747 args.array_len = 2; 12748 args.array = argop; 12749 12750 /* putfh target fh */ 12751 argop[0].argop = OP_CPUTFH; 12752 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12753 12754 argop[1].argop = OP_OPEN_CONFIRM; 12755 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12756 12757 (*seqid) += 1; 12758 open_confirm_args->seqid = *seqid; 12759 open_confirm_args->open_stateid = *stateid; 12760 12761 mi = VTOMI4(vp); 12762 12763 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12764 12765 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12766 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12767 } 12768 12769 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12770 if (!needrecov && ep->error) 12771 return; 12772 12773 if (needrecov) { 12774 bool_t abort = FALSE; 12775 12776 if (reopening_file == FALSE) { 12777 nfs4_bseqid_entry_t *bsep = NULL; 12778 12779 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12780 bsep = nfs4_create_bseqid_entry(oop, NULL, 12781 vp, 0, args.ctag, 12782 open_confirm_args->seqid); 12783 12784 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 12785 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL); 12786 if (bsep) { 12787 kmem_free(bsep, sizeof (*bsep)); 12788 if (num_bseqid_retryp && 12789 --(*num_bseqid_retryp) == 0) 12790 abort = TRUE; 12791 } 12792 } 12793 if ((ep->error == ETIMEDOUT || 12794 res.status == NFS4ERR_RESOURCE) && 12795 abort == FALSE && resend == FALSE) { 12796 if (!ep->error) 12797 (void) xdr_free(xdr_COMPOUND4res_clnt, 12798 (caddr_t)&res); 12799 12800 delay(SEC_TO_TICK(confirm_retry_sec)); 12801 goto recov_retry_confirm; 12802 } 12803 /* State may have changed so retry the entire OPEN op */ 12804 if (abort == FALSE) 12805 *retry_open = TRUE; 12806 else 12807 *retry_open = FALSE; 12808 if (!ep->error) 12809 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12810 return; 12811 } 12812 12813 if (res.status) { 12814 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12815 return; 12816 } 12817 12818 resop = &res.array[1]; /* open confirm res */ 12819 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12820 stateid, sizeof (*stateid)); 12821 12822 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12823 } 12824 12825 /* 12826 * Return the credentials associated with a client state object. The 12827 * caller is responsible for freeing the credentials. 12828 */ 12829 12830 static cred_t * 12831 state_to_cred(nfs4_open_stream_t *osp) 12832 { 12833 cred_t *cr; 12834 12835 /* 12836 * It's ok to not lock the open stream and open owner to get 12837 * the oo_cred since this is only written once (upon creation) 12838 * and will not change. 12839 */ 12840 cr = osp->os_open_owner->oo_cred; 12841 crhold(cr); 12842 12843 return (cr); 12844 } 12845 12846 /* 12847 * nfs4_find_sysid 12848 * 12849 * Find the sysid for the knetconfig associated with the given mi. 12850 */ 12851 static struct lm_sysid * 12852 nfs4_find_sysid(mntinfo4_t *mi) 12853 { 12854 ASSERT(nfs_zone() == mi->mi_zone); 12855 12856 /* 12857 * Switch from RDMA knconf to original mount knconf 12858 */ 12859 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12860 mi->mi_curr_serv->sv_hostname, NULL)); 12861 } 12862 12863 #ifdef DEBUG 12864 /* 12865 * Return a string version of the call type for easy reading. 12866 */ 12867 static char * 12868 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12869 { 12870 switch (ctype) { 12871 case NFS4_LCK_CTYPE_NORM: 12872 return ("NORMAL"); 12873 case NFS4_LCK_CTYPE_RECLAIM: 12874 return ("RECLAIM"); 12875 case NFS4_LCK_CTYPE_RESEND: 12876 return ("RESEND"); 12877 case NFS4_LCK_CTYPE_REINSTATE: 12878 return ("REINSTATE"); 12879 default: 12880 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12881 "type %d", ctype); 12882 return (""); 12883 } 12884 } 12885 #endif 12886 12887 /* 12888 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12889 * Unlock requests don't have an over-the-wire locktype, so we just return 12890 * something non-threatening. 12891 */ 12892 12893 static nfs_lock_type4 12894 flk_to_locktype(int cmd, int l_type) 12895 { 12896 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12897 12898 switch (l_type) { 12899 case F_UNLCK: 12900 return (READ_LT); 12901 case F_RDLCK: 12902 if (cmd == F_SETLK) 12903 return (READ_LT); 12904 else 12905 return (READW_LT); 12906 case F_WRLCK: 12907 if (cmd == F_SETLK) 12908 return (WRITE_LT); 12909 else 12910 return (WRITEW_LT); 12911 } 12912 panic("flk_to_locktype"); 12913 /*NOTREACHED*/ 12914 } 12915 12916 /* 12917 * Do some preliminary checks for nfs4frlock. 12918 */ 12919 static int 12920 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12921 u_offset_t offset) 12922 { 12923 int error = 0; 12924 12925 /* 12926 * If we are setting a lock, check that the file is opened 12927 * with the correct mode. 12928 */ 12929 if (cmd == F_SETLK || cmd == F_SETLKW) { 12930 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12931 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12932 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12933 "nfs4frlock_validate_args: file was opened with " 12934 "incorrect mode")); 12935 return (EBADF); 12936 } 12937 } 12938 12939 /* Convert the offset. It may need to be restored before returning. */ 12940 if (error = convoff(vp, flk, 0, offset)) { 12941 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12942 "nfs4frlock_validate_args: convoff => error= %d\n", 12943 error)); 12944 return (error); 12945 } 12946 12947 return (error); 12948 } 12949 12950 /* 12951 * Set the flock64's lm_sysid for nfs4frlock. 12952 */ 12953 static int 12954 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12955 { 12956 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12957 12958 /* Find the lm_sysid */ 12959 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12960 12961 if (*lspp == NULL) { 12962 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12963 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12964 return (ENOLCK); 12965 } 12966 12967 flk->l_sysid = lm_sysidt(*lspp); 12968 12969 return (0); 12970 } 12971 12972 /* 12973 * Do the remaining preliminary setup for nfs4frlock. 12974 */ 12975 static void 12976 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12977 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12978 cred_t **cred_otw) 12979 { 12980 /* 12981 * set tick_delay to the base delay time. 12982 * (NFS4_BASE_WAIT_TIME is in secs) 12983 */ 12984 12985 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000); 12986 12987 /* 12988 * If lock is relative to EOF, we need the newest length of the 12989 * file. Therefore invalidate the ATTR_CACHE. 12990 */ 12991 12992 *whencep = flk->l_whence; 12993 12994 if (*whencep == 2) /* SEEK_END */ 12995 PURGE_ATTRCACHE4(vp); 12996 12997 recov_statep->rs_flags = 0; 12998 recov_statep->rs_num_retry_despite_err = 0; 12999 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 13000 } 13001 13002 /* 13003 * Initialize and allocate the data structures necessary for 13004 * the nfs4frlock call. 13005 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 13006 */ 13007 static void 13008 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 13009 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 13010 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 13011 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 13012 { 13013 int argoplist_size; 13014 int num_ops = 2; 13015 13016 *retry = FALSE; 13017 *did_start_fop = FALSE; 13018 *skip_get_err = FALSE; 13019 lost_rqstp->lr_op = 0; 13020 argoplist_size = num_ops * sizeof (nfs_argop4); 13021 /* fill array with zero */ 13022 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 13023 13024 *argspp = argsp; 13025 *respp = NULL; 13026 13027 argsp->array_len = num_ops; 13028 argsp->array = *argopp; 13029 13030 /* initialize in case of error; will get real value down below */ 13031 argsp->ctag = TAG_NONE; 13032 13033 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 13034 *op_hintp = OH_LOCKU; 13035 else 13036 *op_hintp = OH_OTHER; 13037 } 13038 13039 /* 13040 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 13041 * the proper nfs4_server_t for this instance of nfs4frlock. 13042 * Returns 0 (success) or an errno value. 13043 */ 13044 static int 13045 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 13046 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 13047 bool_t *did_start_fop, bool_t *startrecovp) 13048 { 13049 int error = 0; 13050 rnode4_t *rp; 13051 13052 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13053 13054 if (ctype == NFS4_LCK_CTYPE_NORM) { 13055 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 13056 recov_statep, startrecovp); 13057 if (error) 13058 return (error); 13059 *did_start_fop = TRUE; 13060 } else { 13061 *did_start_fop = FALSE; 13062 *startrecovp = FALSE; 13063 } 13064 13065 if (!error) { 13066 rp = VTOR4(vp); 13067 13068 /* If the file failed recovery, just quit. */ 13069 mutex_enter(&rp->r_statelock); 13070 if (rp->r_flags & R4RECOVERR) { 13071 error = EIO; 13072 } 13073 mutex_exit(&rp->r_statelock); 13074 } 13075 13076 return (error); 13077 } 13078 13079 /* 13080 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 13081 * resend nfs4frlock call is initiated by the recovery framework. 13082 * Acquires the lop and oop seqid synchronization. 13083 */ 13084 static void 13085 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 13086 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 13087 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13088 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 13089 { 13090 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 13091 int error; 13092 13093 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 13094 (CE_NOTE, 13095 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 13096 ASSERT(resend_rqstp != NULL); 13097 ASSERT(resend_rqstp->lr_op == OP_LOCK || 13098 resend_rqstp->lr_op == OP_LOCKU); 13099 13100 *oopp = resend_rqstp->lr_oop; 13101 if (resend_rqstp->lr_oop) { 13102 open_owner_hold(resend_rqstp->lr_oop); 13103 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 13104 ASSERT(error == 0); /* recov thread always succeeds */ 13105 } 13106 13107 /* Must resend this lost lock/locku request. */ 13108 ASSERT(resend_rqstp->lr_lop != NULL); 13109 *lopp = resend_rqstp->lr_lop; 13110 lock_owner_hold(resend_rqstp->lr_lop); 13111 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 13112 ASSERT(error == 0); /* recov thread always succeeds */ 13113 13114 *ospp = resend_rqstp->lr_osp; 13115 if (*ospp) 13116 open_stream_hold(resend_rqstp->lr_osp); 13117 13118 if (resend_rqstp->lr_op == OP_LOCK) { 13119 LOCK4args *lock_args; 13120 13121 argop->argop = OP_LOCK; 13122 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 13123 lock_args->locktype = resend_rqstp->lr_locktype; 13124 lock_args->reclaim = 13125 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 13126 lock_args->offset = resend_rqstp->lr_flk->l_start; 13127 lock_args->length = resend_rqstp->lr_flk->l_len; 13128 if (lock_args->length == 0) 13129 lock_args->length = ~lock_args->length; 13130 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 13131 mi2clientid(mi), &lock_args->locker); 13132 13133 switch (resend_rqstp->lr_ctype) { 13134 case NFS4_LCK_CTYPE_RESEND: 13135 argsp->ctag = TAG_LOCK_RESEND; 13136 break; 13137 case NFS4_LCK_CTYPE_REINSTATE: 13138 argsp->ctag = TAG_LOCK_REINSTATE; 13139 break; 13140 case NFS4_LCK_CTYPE_RECLAIM: 13141 argsp->ctag = TAG_LOCK_RECLAIM; 13142 break; 13143 default: 13144 argsp->ctag = TAG_LOCK_UNKNOWN; 13145 break; 13146 } 13147 } else { 13148 LOCKU4args *locku_args; 13149 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 13150 13151 argop->argop = OP_LOCKU; 13152 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 13153 locku_args->locktype = READ_LT; 13154 locku_args->seqid = lop->lock_seqid + 1; 13155 mutex_enter(&lop->lo_lock); 13156 locku_args->lock_stateid = lop->lock_stateid; 13157 mutex_exit(&lop->lo_lock); 13158 locku_args->offset = resend_rqstp->lr_flk->l_start; 13159 locku_args->length = resend_rqstp->lr_flk->l_len; 13160 if (locku_args->length == 0) 13161 locku_args->length = ~locku_args->length; 13162 13163 switch (resend_rqstp->lr_ctype) { 13164 case NFS4_LCK_CTYPE_RESEND: 13165 argsp->ctag = TAG_LOCKU_RESEND; 13166 break; 13167 case NFS4_LCK_CTYPE_REINSTATE: 13168 argsp->ctag = TAG_LOCKU_REINSTATE; 13169 break; 13170 default: 13171 argsp->ctag = TAG_LOCK_UNKNOWN; 13172 break; 13173 } 13174 } 13175 } 13176 13177 /* 13178 * Setup the LOCKT4 arguments. 13179 */ 13180 static void 13181 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13182 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 13183 rnode4_t *rp) 13184 { 13185 LOCKT4args *lockt_args; 13186 13187 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 13188 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13189 argop->argop = OP_LOCKT; 13190 argsp->ctag = TAG_LOCKT; 13191 lockt_args = &argop->nfs_argop4_u.oplockt; 13192 13193 /* 13194 * The locktype will be READ_LT unless it's 13195 * a write lock. We do this because the Solaris 13196 * system call allows the combination of 13197 * F_UNLCK and F_GETLK* and so in that case the 13198 * unlock is mapped to a read. 13199 */ 13200 if (flk->l_type == F_WRLCK) 13201 lockt_args->locktype = WRITE_LT; 13202 else 13203 lockt_args->locktype = READ_LT; 13204 13205 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 13206 /* set the lock owner4 args */ 13207 nfs4_setlockowner_args(&lockt_args->owner, rp, 13208 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13209 flk->l_pid); 13210 lockt_args->offset = flk->l_start; 13211 lockt_args->length = flk->l_len; 13212 if (flk->l_len == 0) 13213 lockt_args->length = ~lockt_args->length; 13214 13215 *lockt_argsp = lockt_args; 13216 } 13217 13218 /* 13219 * If the client is holding a delegation, and the open stream to be used 13220 * with this lock request is a delegation open stream, then re-open the stream. 13221 * Sets the nfs4_error_t to all zeros unless the open stream has already 13222 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13223 * means the caller should retry (like a recovery retry). 13224 */ 13225 static void 13226 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13227 { 13228 open_delegation_type4 dt; 13229 bool_t reopen_needed, force; 13230 nfs4_open_stream_t *osp; 13231 open_claim_type4 oclaim; 13232 rnode4_t *rp = VTOR4(vp); 13233 mntinfo4_t *mi = VTOMI4(vp); 13234 13235 ASSERT(nfs_zone() == mi->mi_zone); 13236 13237 nfs4_error_zinit(ep); 13238 13239 mutex_enter(&rp->r_statev4_lock); 13240 dt = rp->r_deleg_type; 13241 mutex_exit(&rp->r_statev4_lock); 13242 13243 if (dt != OPEN_DELEGATE_NONE) { 13244 nfs4_open_owner_t *oop; 13245 13246 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13247 if (!oop) { 13248 ep->stat = NFS4ERR_IO; 13249 return; 13250 } 13251 /* returns with 'os_sync_lock' held */ 13252 osp = find_open_stream(oop, rp); 13253 if (!osp) { 13254 open_owner_rele(oop); 13255 ep->stat = NFS4ERR_IO; 13256 return; 13257 } 13258 13259 if (osp->os_failed_reopen) { 13260 NFS4_DEBUG((nfs4_open_stream_debug || 13261 nfs4_client_lock_debug), (CE_NOTE, 13262 "nfs4frlock_check_deleg: os_failed_reopen set " 13263 "for osp %p, cr %p, rp %s", (void *)osp, 13264 (void *)cr, rnode4info(rp))); 13265 mutex_exit(&osp->os_sync_lock); 13266 open_stream_rele(osp, rp); 13267 open_owner_rele(oop); 13268 ep->stat = NFS4ERR_IO; 13269 return; 13270 } 13271 13272 /* 13273 * Determine whether a reopen is needed. If this 13274 * is a delegation open stream, then send the open 13275 * to the server to give visibility to the open owner. 13276 * Even if it isn't a delegation open stream, we need 13277 * to check if the previous open CLAIM_DELEGATE_CUR 13278 * was sufficient. 13279 */ 13280 13281 reopen_needed = osp->os_delegation || 13282 ((lt == F_RDLCK && 13283 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13284 (lt == F_WRLCK && 13285 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13286 13287 mutex_exit(&osp->os_sync_lock); 13288 open_owner_rele(oop); 13289 13290 if (reopen_needed) { 13291 /* 13292 * Always use CLAIM_PREVIOUS after server reboot. 13293 * The server will reject CLAIM_DELEGATE_CUR if 13294 * it is used during the grace period. 13295 */ 13296 mutex_enter(&mi->mi_lock); 13297 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13298 oclaim = CLAIM_PREVIOUS; 13299 force = TRUE; 13300 } else { 13301 oclaim = CLAIM_DELEGATE_CUR; 13302 force = FALSE; 13303 } 13304 mutex_exit(&mi->mi_lock); 13305 13306 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13307 if (ep->error == EAGAIN) { 13308 nfs4_error_zinit(ep); 13309 ep->stat = NFS4ERR_DELAY; 13310 } 13311 } 13312 open_stream_rele(osp, rp); 13313 osp = NULL; 13314 } 13315 } 13316 13317 /* 13318 * Setup the LOCKU4 arguments. 13319 * Returns errors via the nfs4_error_t. 13320 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13321 * over-the-wire. The caller must release the 13322 * reference on *lopp. 13323 * NFS4ERR_DELAY caller should retry (like recovery retry) 13324 * (other) unrecoverable error. 13325 */ 13326 static void 13327 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13328 LOCKU4args **locku_argsp, flock64_t *flk, 13329 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13330 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13331 bool_t *skip_get_err, bool_t *go_otwp) 13332 { 13333 nfs4_lock_owner_t *lop = NULL; 13334 LOCKU4args *locku_args; 13335 pid_t pid; 13336 bool_t is_spec = FALSE; 13337 rnode4_t *rp = VTOR4(vp); 13338 13339 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13340 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13341 13342 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13343 if (ep->error || ep->stat) 13344 return; 13345 13346 argop->argop = OP_LOCKU; 13347 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13348 argsp->ctag = TAG_LOCKU_REINSTATE; 13349 else 13350 argsp->ctag = TAG_LOCKU; 13351 locku_args = &argop->nfs_argop4_u.oplocku; 13352 *locku_argsp = locku_args; 13353 13354 /* 13355 * XXX what should locku_args->locktype be? 13356 * setting to ALWAYS be READ_LT so at least 13357 * it is a valid locktype. 13358 */ 13359 13360 locku_args->locktype = READ_LT; 13361 13362 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13363 flk->l_pid; 13364 13365 /* 13366 * Get the lock owner stateid. If no lock owner 13367 * exists, return success. 13368 */ 13369 lop = find_lock_owner(rp, pid, LOWN_ANY); 13370 *lopp = lop; 13371 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13372 is_spec = TRUE; 13373 if (!lop || is_spec) { 13374 /* 13375 * No lock owner so no locks to unlock. 13376 * Return success. If there was a failed 13377 * reclaim earlier, the lock might still be 13378 * registered with the local locking code, 13379 * so notify it of the unlock. 13380 * 13381 * If the lockowner is using a special stateid, 13382 * then the original lock request (that created 13383 * this lockowner) was never successful, so we 13384 * have no lock to undo OTW. 13385 */ 13386 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13387 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13388 "(%ld) so return success", (long)pid)); 13389 13390 if (ctype == NFS4_LCK_CTYPE_NORM) 13391 flk->l_pid = curproc->p_pid; 13392 nfs4_register_lock_locally(vp, flk, flag, offset); 13393 /* 13394 * Release our hold and NULL out so final_cleanup 13395 * doesn't try to end a lock seqid sync we 13396 * never started. 13397 */ 13398 if (is_spec) { 13399 lock_owner_rele(lop); 13400 *lopp = NULL; 13401 } 13402 *skip_get_err = TRUE; 13403 *go_otwp = FALSE; 13404 return; 13405 } 13406 13407 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13408 if (ep->error == EAGAIN) { 13409 lock_owner_rele(lop); 13410 *lopp = NULL; 13411 return; 13412 } 13413 13414 mutex_enter(&lop->lo_lock); 13415 locku_args->lock_stateid = lop->lock_stateid; 13416 mutex_exit(&lop->lo_lock); 13417 locku_args->seqid = lop->lock_seqid + 1; 13418 13419 /* leave the ref count on lop, rele after RPC call */ 13420 13421 locku_args->offset = flk->l_start; 13422 locku_args->length = flk->l_len; 13423 if (flk->l_len == 0) 13424 locku_args->length = ~locku_args->length; 13425 13426 *go_otwp = TRUE; 13427 } 13428 13429 /* 13430 * Setup the LOCK4 arguments. 13431 * 13432 * Returns errors via the nfs4_error_t. 13433 * NFS4_OK no problems 13434 * NFS4ERR_DELAY caller should retry (like recovery retry) 13435 * (other) unrecoverable error 13436 */ 13437 static void 13438 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13439 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13440 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13441 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13442 { 13443 LOCK4args *lock_args; 13444 nfs4_open_owner_t *oop = NULL; 13445 nfs4_open_stream_t *osp = NULL; 13446 nfs4_lock_owner_t *lop = NULL; 13447 pid_t pid; 13448 rnode4_t *rp = VTOR4(vp); 13449 13450 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13451 13452 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13453 if (ep->error || ep->stat != NFS4_OK) 13454 return; 13455 13456 argop->argop = OP_LOCK; 13457 if (ctype == NFS4_LCK_CTYPE_NORM) 13458 argsp->ctag = TAG_LOCK; 13459 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13460 argsp->ctag = TAG_RELOCK; 13461 else 13462 argsp->ctag = TAG_LOCK_REINSTATE; 13463 lock_args = &argop->nfs_argop4_u.oplock; 13464 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13465 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13466 /* 13467 * Get the lock owner. If no lock owner exists, 13468 * create a 'temporary' one and grab the open seqid 13469 * synchronization (which puts a hold on the open 13470 * owner and open stream). 13471 * This also grabs the lock seqid synchronization. 13472 */ 13473 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13474 ep->stat = 13475 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13476 13477 if (ep->stat != NFS4_OK) 13478 goto out; 13479 13480 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13481 &lock_args->locker); 13482 13483 lock_args->offset = flk->l_start; 13484 lock_args->length = flk->l_len; 13485 if (flk->l_len == 0) 13486 lock_args->length = ~lock_args->length; 13487 *lock_argsp = lock_args; 13488 out: 13489 *oopp = oop; 13490 *ospp = osp; 13491 *lopp = lop; 13492 } 13493 13494 /* 13495 * After we get the reply from the server, record the proper information 13496 * for possible resend lock requests. 13497 * 13498 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13499 */ 13500 static void 13501 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13502 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13503 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13504 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13505 { 13506 bool_t unlock = (flk->l_type == F_UNLCK); 13507 13508 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13509 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13510 ctype == NFS4_LCK_CTYPE_REINSTATE); 13511 13512 if (error != 0 && !unlock) { 13513 NFS4_DEBUG((nfs4_lost_rqst_debug || 13514 nfs4_client_lock_debug), (CE_NOTE, 13515 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13516 " for lop %p", (void *)lop)); 13517 ASSERT(lop != NULL); 13518 mutex_enter(&lop->lo_lock); 13519 lop->lo_pending_rqsts = 1; 13520 mutex_exit(&lop->lo_lock); 13521 } 13522 13523 lost_rqstp->lr_putfirst = FALSE; 13524 lost_rqstp->lr_op = 0; 13525 13526 /* 13527 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13528 * recovery purposes so that the lock request that was sent 13529 * can be saved and re-issued later. Ditto for EIO from a forced 13530 * unmount. This is done to have the client's local locking state 13531 * match the v4 server's state; that is, the request was 13532 * potentially received and accepted by the server but the client 13533 * thinks it was not. 13534 */ 13535 if (error == ETIMEDOUT || error == EINTR || 13536 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13537 NFS4_DEBUG((nfs4_lost_rqst_debug || 13538 nfs4_client_lock_debug), (CE_NOTE, 13539 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13540 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13541 (void *)lop, (void *)oop, (void *)osp)); 13542 if (unlock) 13543 lost_rqstp->lr_op = OP_LOCKU; 13544 else { 13545 lost_rqstp->lr_op = OP_LOCK; 13546 lost_rqstp->lr_locktype = locktype; 13547 } 13548 /* 13549 * Objects are held and rele'd via the recovery code. 13550 * See nfs4_save_lost_rqst. 13551 */ 13552 lost_rqstp->lr_vp = vp; 13553 lost_rqstp->lr_dvp = NULL; 13554 lost_rqstp->lr_oop = oop; 13555 lost_rqstp->lr_osp = osp; 13556 lost_rqstp->lr_lop = lop; 13557 lost_rqstp->lr_cr = cr; 13558 switch (ctype) { 13559 case NFS4_LCK_CTYPE_NORM: 13560 flk->l_pid = ttoproc(curthread)->p_pid; 13561 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13562 break; 13563 case NFS4_LCK_CTYPE_REINSTATE: 13564 lost_rqstp->lr_putfirst = TRUE; 13565 lost_rqstp->lr_ctype = ctype; 13566 break; 13567 default: 13568 break; 13569 } 13570 lost_rqstp->lr_flk = flk; 13571 } 13572 } 13573 13574 /* 13575 * Update lop's seqid. Also update the seqid stored in a resend request, 13576 * if any. (Some recovery errors increment the seqid, and we may have to 13577 * send the resend request again.) 13578 */ 13579 13580 static void 13581 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13582 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13583 { 13584 if (lock_args) { 13585 if (lock_args->locker.new_lock_owner == TRUE) 13586 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13587 else { 13588 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13589 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13590 } 13591 } else if (locku_args) { 13592 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13593 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13594 } 13595 } 13596 13597 /* 13598 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13599 * COMPOUND4 args/res for calls that need to retry. 13600 * Switches the *cred_otwp to base_cr. 13601 */ 13602 static void 13603 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13604 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13605 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13606 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13607 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13608 { 13609 nfs4_open_owner_t *oop = *oopp; 13610 nfs4_open_stream_t *osp = *ospp; 13611 nfs4_lock_owner_t *lop = *lopp; 13612 nfs_argop4 *argop = (*argspp)->array; 13613 13614 if (*did_start_fop) { 13615 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13616 needrecov); 13617 *did_start_fop = FALSE; 13618 } 13619 ASSERT((*argspp)->array_len == 2); 13620 if (argop[1].argop == OP_LOCK) 13621 nfs4args_lock_free(&argop[1]); 13622 else if (argop[1].argop == OP_LOCKT) 13623 nfs4args_lockt_free(&argop[1]); 13624 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13625 if (!error) 13626 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13627 *argspp = NULL; 13628 *respp = NULL; 13629 13630 if (lop) { 13631 nfs4_end_lock_seqid_sync(lop); 13632 lock_owner_rele(lop); 13633 *lopp = NULL; 13634 } 13635 13636 /* need to free up the reference on osp for lock args */ 13637 if (osp != NULL) { 13638 open_stream_rele(osp, VTOR4(vp)); 13639 *ospp = NULL; 13640 } 13641 13642 /* need to free up the reference on oop for lock args */ 13643 if (oop != NULL) { 13644 nfs4_end_open_seqid_sync(oop); 13645 open_owner_rele(oop); 13646 *oopp = NULL; 13647 } 13648 13649 crfree(*cred_otwp); 13650 *cred_otwp = base_cr; 13651 crhold(*cred_otwp); 13652 } 13653 13654 /* 13655 * Function to process the client's recovery for nfs4frlock. 13656 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13657 * 13658 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13659 * COMPOUND4 args/res for calls that need to retry. 13660 * 13661 * Note: the rp's r_lkserlock is *not* dropped during this path. 13662 */ 13663 static bool_t 13664 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13665 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13666 LOCK4args *lock_args, LOCKU4args *locku_args, 13667 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13668 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13669 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13670 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13671 { 13672 nfs4_open_owner_t *oop = *oopp; 13673 nfs4_open_stream_t *osp = *ospp; 13674 nfs4_lock_owner_t *lop = *lopp; 13675 13676 bool_t abort, retry; 13677 13678 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13679 ASSERT((*argspp) != NULL); 13680 ASSERT((*respp) != NULL); 13681 if (lock_args || locku_args) 13682 ASSERT(lop != NULL); 13683 13684 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13685 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13686 13687 retry = TRUE; 13688 abort = FALSE; 13689 if (needrecov) { 13690 nfs4_bseqid_entry_t *bsep = NULL; 13691 nfs_opnum4 op; 13692 13693 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13694 13695 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13696 seqid4 seqid; 13697 13698 if (lock_args) { 13699 if (lock_args->locker.new_lock_owner == TRUE) 13700 seqid = lock_args->locker.locker4_u. 13701 open_owner.open_seqid; 13702 else 13703 seqid = lock_args->locker.locker4_u. 13704 lock_owner.lock_seqid; 13705 } else if (locku_args) { 13706 seqid = locku_args->seqid; 13707 } else { 13708 seqid = 0; 13709 } 13710 13711 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13712 flk->l_pid, (*argspp)->ctag, seqid); 13713 } 13714 13715 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13716 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13717 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13718 NULL, op, bsep, NULL, NULL); 13719 13720 if (bsep) 13721 kmem_free(bsep, sizeof (*bsep)); 13722 } 13723 13724 /* 13725 * Return that we do not want to retry the request for 3 cases: 13726 * 1. If we received EINTR or are bailing out because of a forced 13727 * unmount, we came into this code path just for the sake of 13728 * initiating recovery, we now need to return the error. 13729 * 2. If we have aborted recovery. 13730 * 3. We received NFS4ERR_BAD_SEQID. 13731 */ 13732 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13733 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13734 retry = FALSE; 13735 13736 if (*did_start_fop == TRUE) { 13737 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13738 needrecov); 13739 *did_start_fop = FALSE; 13740 } 13741 13742 if (retry == TRUE) { 13743 nfs_argop4 *argop; 13744 13745 argop = (*argspp)->array; 13746 ASSERT((*argspp)->array_len == 2); 13747 13748 if (argop[1].argop == OP_LOCK) 13749 nfs4args_lock_free(&argop[1]); 13750 else if (argop[1].argop == OP_LOCKT) 13751 nfs4args_lockt_free(&argop[1]); 13752 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13753 if (!ep->error) 13754 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13755 *respp = NULL; 13756 *argspp = NULL; 13757 } 13758 13759 if (lop != NULL) { 13760 nfs4_end_lock_seqid_sync(lop); 13761 lock_owner_rele(lop); 13762 } 13763 13764 *lopp = NULL; 13765 13766 /* need to free up the reference on osp for lock args */ 13767 if (osp != NULL) { 13768 open_stream_rele(osp, rp); 13769 *ospp = NULL; 13770 } 13771 13772 /* need to free up the reference on oop for lock args */ 13773 if (oop != NULL) { 13774 nfs4_end_open_seqid_sync(oop); 13775 open_owner_rele(oop); 13776 *oopp = NULL; 13777 } 13778 13779 return (retry); 13780 } 13781 13782 /* 13783 * Handles the successful reply from the server for nfs4frlock. 13784 */ 13785 static void 13786 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13787 vnode_t *vp, int flag, u_offset_t offset, 13788 nfs4_lost_rqst_t *resend_rqstp) 13789 { 13790 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13791 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13792 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13793 if (ctype == NFS4_LCK_CTYPE_NORM) { 13794 flk->l_pid = ttoproc(curthread)->p_pid; 13795 /* 13796 * We do not register lost locks locally in 13797 * the 'resend' case since the user/application 13798 * doesn't think we have the lock. 13799 */ 13800 ASSERT(!resend_rqstp); 13801 nfs4_register_lock_locally(vp, flk, flag, offset); 13802 } 13803 } 13804 } 13805 13806 /* 13807 * Handle the DENIED reply from the server for nfs4frlock. 13808 * Returns TRUE if we should retry the request; FALSE otherwise. 13809 * 13810 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13811 * COMPOUND4 args/res for calls that need to retry. Can also 13812 * drop and regrab the r_lkserlock. 13813 */ 13814 static bool_t 13815 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13816 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13817 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13818 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13819 nfs4_recov_state_t *recov_statep, int needrecov, 13820 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13821 clock_t *tick_delayp, short *whencep, int *errorp, 13822 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13823 bool_t *skip_get_err) 13824 { 13825 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13826 13827 if (lock_args) { 13828 nfs4_open_owner_t *oop = *oopp; 13829 nfs4_open_stream_t *osp = *ospp; 13830 nfs4_lock_owner_t *lop = *lopp; 13831 int intr; 13832 13833 /* 13834 * Blocking lock needs to sleep and retry from the request. 13835 * 13836 * Do not block and wait for 'resend' or 'reinstate' 13837 * lock requests, just return the error. 13838 * 13839 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13840 */ 13841 if (cmd == F_SETLKW) { 13842 rnode4_t *rp = VTOR4(vp); 13843 nfs_argop4 *argop = (*argspp)->array; 13844 13845 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13846 13847 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13848 recov_statep, needrecov); 13849 *did_start_fop = FALSE; 13850 ASSERT((*argspp)->array_len == 2); 13851 if (argop[1].argop == OP_LOCK) 13852 nfs4args_lock_free(&argop[1]); 13853 else if (argop[1].argop == OP_LOCKT) 13854 nfs4args_lockt_free(&argop[1]); 13855 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13856 if (*respp) 13857 (void) xdr_free(xdr_COMPOUND4res_clnt, 13858 (caddr_t)*respp); 13859 *argspp = NULL; 13860 *respp = NULL; 13861 nfs4_end_lock_seqid_sync(lop); 13862 lock_owner_rele(lop); 13863 *lopp = NULL; 13864 if (osp != NULL) { 13865 open_stream_rele(osp, rp); 13866 *ospp = NULL; 13867 } 13868 if (oop != NULL) { 13869 nfs4_end_open_seqid_sync(oop); 13870 open_owner_rele(oop); 13871 *oopp = NULL; 13872 } 13873 13874 nfs_rw_exit(&rp->r_lkserlock); 13875 13876 intr = nfs4_block_and_wait(tick_delayp, rp); 13877 13878 if (intr) { 13879 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13880 RW_WRITER, FALSE); 13881 *errorp = EINTR; 13882 return (FALSE); 13883 } 13884 13885 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13886 RW_WRITER, FALSE); 13887 13888 /* 13889 * Make sure we are still safe to lock with 13890 * regards to mmapping. 13891 */ 13892 if (!nfs4_safelock(vp, flk, cr)) { 13893 *errorp = EAGAIN; 13894 return (FALSE); 13895 } 13896 13897 return (TRUE); 13898 } 13899 if (ctype == NFS4_LCK_CTYPE_NORM) 13900 *errorp = EAGAIN; 13901 *skip_get_err = TRUE; 13902 flk->l_whence = 0; 13903 *whencep = 0; 13904 return (FALSE); 13905 } else if (lockt_args) { 13906 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13907 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13908 13909 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13910 flk, lockt_args); 13911 13912 /* according to NLM code */ 13913 *errorp = 0; 13914 *whencep = 0; 13915 *skip_get_err = TRUE; 13916 return (FALSE); 13917 } 13918 return (FALSE); 13919 } 13920 13921 /* 13922 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13923 */ 13924 static void 13925 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13926 { 13927 switch (resp->status) { 13928 case NFS4ERR_ACCESS: 13929 case NFS4ERR_ADMIN_REVOKED: 13930 case NFS4ERR_BADHANDLE: 13931 case NFS4ERR_BAD_RANGE: 13932 case NFS4ERR_BAD_SEQID: 13933 case NFS4ERR_BAD_STATEID: 13934 case NFS4ERR_BADXDR: 13935 case NFS4ERR_DEADLOCK: 13936 case NFS4ERR_DELAY: 13937 case NFS4ERR_EXPIRED: 13938 case NFS4ERR_FHEXPIRED: 13939 case NFS4ERR_GRACE: 13940 case NFS4ERR_INVAL: 13941 case NFS4ERR_ISDIR: 13942 case NFS4ERR_LEASE_MOVED: 13943 case NFS4ERR_LOCK_NOTSUPP: 13944 case NFS4ERR_LOCK_RANGE: 13945 case NFS4ERR_MOVED: 13946 case NFS4ERR_NOFILEHANDLE: 13947 case NFS4ERR_NO_GRACE: 13948 case NFS4ERR_OLD_STATEID: 13949 case NFS4ERR_OPENMODE: 13950 case NFS4ERR_RECLAIM_BAD: 13951 case NFS4ERR_RECLAIM_CONFLICT: 13952 case NFS4ERR_RESOURCE: 13953 case NFS4ERR_SERVERFAULT: 13954 case NFS4ERR_STALE: 13955 case NFS4ERR_STALE_CLIENTID: 13956 case NFS4ERR_STALE_STATEID: 13957 return; 13958 default: 13959 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13960 "nfs4frlock_results_default: got unrecognizable " 13961 "res.status %d", resp->status)); 13962 *errorp = NFS4ERR_INVAL; 13963 } 13964 } 13965 13966 /* 13967 * The lock request was successful, so update the client's state. 13968 */ 13969 static void 13970 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13971 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13972 vnode_t *vp, flock64_t *flk, cred_t *cr, 13973 nfs4_lost_rqst_t *resend_rqstp) 13974 { 13975 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13976 13977 if (lock_args) { 13978 LOCK4res *lock_res; 13979 13980 lock_res = &resop->nfs_resop4_u.oplock; 13981 /* update the stateid with server's response */ 13982 13983 if (lock_args->locker.new_lock_owner == TRUE) { 13984 mutex_enter(&lop->lo_lock); 13985 lop->lo_just_created = NFS4_PERM_CREATED; 13986 mutex_exit(&lop->lo_lock); 13987 } 13988 13989 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13990 13991 /* 13992 * If the lock was the result of a resending a lost 13993 * request, we've synched up the stateid and seqid 13994 * with the server, but now the server might be out of sync 13995 * with what the application thinks it has for locks. 13996 * Clean that up here. It's unclear whether we should do 13997 * this even if the filesystem has been forcibly unmounted. 13998 * For most servers, it's probably wasted effort, but 13999 * RFC3530 lets servers require that unlocks exactly match 14000 * the locks that are held. 14001 */ 14002 if (resend_rqstp != NULL && 14003 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 14004 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 14005 } else { 14006 flk->l_whence = 0; 14007 } 14008 } else if (locku_args) { 14009 LOCKU4res *locku_res; 14010 14011 locku_res = &resop->nfs_resop4_u.oplocku; 14012 14013 /* Update the stateid with the server's response */ 14014 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 14015 } else if (lockt_args) { 14016 /* Switch the lock type to express success, see fcntl */ 14017 flk->l_type = F_UNLCK; 14018 flk->l_whence = 0; 14019 } 14020 } 14021 14022 /* 14023 * Do final cleanup before exiting nfs4frlock. 14024 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 14025 * COMPOUND4 args/res for calls that haven't already. 14026 */ 14027 static void 14028 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 14029 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 14030 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 14031 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 14032 short whence, u_offset_t offset, struct lm_sysid *ls, 14033 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 14034 bool_t did_start_fop, bool_t skip_get_err, 14035 cred_t *cred_otw, cred_t *cred) 14036 { 14037 mntinfo4_t *mi = VTOMI4(vp); 14038 rnode4_t *rp = VTOR4(vp); 14039 int error = *errorp; 14040 nfs_argop4 *argop; 14041 int do_flush_pages = 0; 14042 14043 ASSERT(nfs_zone() == mi->mi_zone); 14044 /* 14045 * The client recovery code wants the raw status information, 14046 * so don't map the NFS status code to an errno value for 14047 * non-normal call types. 14048 */ 14049 if (ctype == NFS4_LCK_CTYPE_NORM) { 14050 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 14051 *errorp = geterrno4(resp->status); 14052 if (did_start_fop == TRUE) 14053 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 14054 needrecov); 14055 14056 /* 14057 * We've established a new lock on the server, so invalidate 14058 * the pages associated with the vnode to get the most up to 14059 * date pages from the server after acquiring the lock. We 14060 * want to be sure that the read operation gets the newest data. 14061 * N.B. 14062 * We used to do this in nfs4frlock_results_ok but that doesn't 14063 * work since VOP_PUTPAGE can call nfs4_commit which calls 14064 * nfs4_start_fop. We flush the pages below after calling 14065 * nfs4_end_fop above 14066 * The flush of the page cache must be done after 14067 * nfs4_end_open_seqid_sync() to avoid a 4-way hang. 14068 */ 14069 if (!error && resp && resp->status == NFS4_OK) 14070 do_flush_pages = 1; 14071 } 14072 if (argsp) { 14073 ASSERT(argsp->array_len == 2); 14074 argop = argsp->array; 14075 if (argop[1].argop == OP_LOCK) 14076 nfs4args_lock_free(&argop[1]); 14077 else if (argop[1].argop == OP_LOCKT) 14078 nfs4args_lockt_free(&argop[1]); 14079 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14080 if (resp) 14081 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 14082 } 14083 14084 /* free the reference on the lock owner */ 14085 if (lop != NULL) { 14086 nfs4_end_lock_seqid_sync(lop); 14087 lock_owner_rele(lop); 14088 } 14089 14090 /* need to free up the reference on osp for lock args */ 14091 if (osp != NULL) 14092 open_stream_rele(osp, rp); 14093 14094 /* need to free up the reference on oop for lock args */ 14095 if (oop != NULL) { 14096 nfs4_end_open_seqid_sync(oop); 14097 open_owner_rele(oop); 14098 } 14099 14100 if (do_flush_pages) 14101 nfs4_flush_pages(vp, cred); 14102 14103 (void) convoff(vp, flk, whence, offset); 14104 14105 lm_rel_sysid(ls); 14106 14107 /* 14108 * Record debug information in the event we get EINVAL. 14109 */ 14110 mutex_enter(&mi->mi_lock); 14111 if (*errorp == EINVAL && (lock_args || locku_args) && 14112 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 14113 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 14114 zcmn_err(getzoneid(), CE_NOTE, 14115 "%s operation failed with " 14116 "EINVAL probably since the server, %s," 14117 " doesn't support POSIX style locking", 14118 lock_args ? "LOCK" : "LOCKU", 14119 mi->mi_curr_serv->sv_hostname); 14120 mi->mi_flags |= MI4_LOCK_DEBUG; 14121 } 14122 } 14123 mutex_exit(&mi->mi_lock); 14124 14125 if (cred_otw) 14126 crfree(cred_otw); 14127 } 14128 14129 /* 14130 * This calls the server and the local locking code. 14131 * 14132 * Client locks are registerred locally by oring the sysid with 14133 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 14134 * We need to distinguish between the two to avoid collision in case one 14135 * machine is used as both client and server. 14136 * 14137 * Blocking lock requests will continually retry to acquire the lock 14138 * forever. 14139 * 14140 * The ctype is defined as follows: 14141 * NFS4_LCK_CTYPE_NORM: normal lock request. 14142 * 14143 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 14144 * recovery, get the pid from flk instead of curproc, and don't reregister 14145 * the lock locally. 14146 * 14147 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 14148 * that we will use the information passed in via resend_rqstp to setup the 14149 * lock/locku request. This resend is the exact same request as the 'lost 14150 * lock', and is initiated by the recovery framework. A successful resend 14151 * request can initiate one or more reinstate requests. 14152 * 14153 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 14154 * does not trigger additional reinstate requests. This lock call type is 14155 * set for setting the v4 server's locking state back to match what the 14156 * client's local locking state is in the event of a received 'lost lock'. 14157 * 14158 * Errors are returned via the nfs4_error_t parameter. 14159 */ 14160 void 14161 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 14162 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 14163 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 14164 { 14165 COMPOUND4args_clnt args, *argsp = NULL; 14166 COMPOUND4res_clnt res, *resp = NULL; 14167 nfs_argop4 *argop; 14168 nfs_resop4 *resop; 14169 rnode4_t *rp; 14170 int doqueue = 1; 14171 clock_t tick_delay; /* delay in clock ticks */ 14172 struct lm_sysid *ls; 14173 LOCK4args *lock_args = NULL; 14174 LOCKU4args *locku_args = NULL; 14175 LOCKT4args *lockt_args = NULL; 14176 nfs4_open_owner_t *oop = NULL; 14177 nfs4_open_stream_t *osp = NULL; 14178 nfs4_lock_owner_t *lop = NULL; 14179 bool_t needrecov = FALSE; 14180 nfs4_recov_state_t recov_state; 14181 short whence; 14182 nfs4_op_hint_t op_hint; 14183 nfs4_lost_rqst_t lost_rqst; 14184 bool_t retry = FALSE; 14185 bool_t did_start_fop = FALSE; 14186 bool_t skip_get_err = FALSE; 14187 cred_t *cred_otw = NULL; 14188 bool_t recovonly; /* just queue request */ 14189 int frc_no_reclaim = 0; 14190 #ifdef DEBUG 14191 char *name; 14192 #endif 14193 14194 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14195 14196 #ifdef DEBUG 14197 name = fn_name(VTOSV(vp)->sv_name); 14198 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 14199 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 14200 "length %"PRIu64", pid %d, sysid %d, call type %s, " 14201 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 14202 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 14203 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 14204 resend_rqstp ? "TRUE" : "FALSE")); 14205 kmem_free(name, MAXNAMELEN); 14206 #endif 14207 14208 nfs4_error_zinit(ep); 14209 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14210 if (ep->error) 14211 return; 14212 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14213 if (ep->error) 14214 return; 14215 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14216 vp, cr, &cred_otw); 14217 14218 recov_retry: 14219 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14220 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14221 rp = VTOR4(vp); 14222 14223 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14224 &did_start_fop, &recovonly); 14225 14226 if (ep->error) 14227 goto out; 14228 14229 if (recovonly) { 14230 /* 14231 * Leave the request for the recovery system to deal with. 14232 */ 14233 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14234 ASSERT(cmd != F_GETLK); 14235 ASSERT(flk->l_type == F_UNLCK); 14236 14237 nfs4_error_init(ep, EINTR); 14238 needrecov = TRUE; 14239 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14240 if (lop != NULL) { 14241 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14242 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14243 (void) nfs4_start_recovery(ep, 14244 VTOMI4(vp), vp, NULL, NULL, 14245 (lost_rqst.lr_op == OP_LOCK || 14246 lost_rqst.lr_op == OP_LOCKU) ? 14247 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL); 14248 lock_owner_rele(lop); 14249 lop = NULL; 14250 } 14251 flk->l_pid = curproc->p_pid; 14252 nfs4_register_lock_locally(vp, flk, flag, offset); 14253 goto out; 14254 } 14255 14256 /* putfh directory fh */ 14257 argop[0].argop = OP_CPUTFH; 14258 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14259 14260 /* 14261 * Set up the over-the-wire arguments and get references to the 14262 * open owner, etc. 14263 */ 14264 14265 if (ctype == NFS4_LCK_CTYPE_RESEND || 14266 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14267 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14268 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14269 } else { 14270 bool_t go_otw = TRUE; 14271 14272 ASSERT(resend_rqstp == NULL); 14273 14274 switch (cmd) { 14275 case F_GETLK: 14276 case F_O_GETLK: 14277 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14278 &lockt_args, argsp, flk, rp); 14279 break; 14280 case F_SETLKW: 14281 case F_SETLK: 14282 if (flk->l_type == F_UNLCK) 14283 nfs4frlock_setup_locku_args(ctype, 14284 &argop[1], &locku_args, flk, 14285 &lop, ep, argsp, 14286 vp, flag, offset, cr, 14287 &skip_get_err, &go_otw); 14288 else 14289 nfs4frlock_setup_lock_args(ctype, 14290 &lock_args, &oop, &osp, &lop, &argop[1], 14291 argsp, flk, cmd, vp, cr, ep); 14292 14293 if (ep->error) 14294 goto out; 14295 14296 switch (ep->stat) { 14297 case NFS4_OK: 14298 break; 14299 case NFS4ERR_DELAY: 14300 /* recov thread never gets this error */ 14301 ASSERT(resend_rqstp == NULL); 14302 ASSERT(did_start_fop); 14303 14304 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14305 &recov_state, TRUE); 14306 did_start_fop = FALSE; 14307 if (argop[1].argop == OP_LOCK) 14308 nfs4args_lock_free(&argop[1]); 14309 else if (argop[1].argop == OP_LOCKT) 14310 nfs4args_lockt_free(&argop[1]); 14311 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14312 argsp = NULL; 14313 goto recov_retry; 14314 default: 14315 ep->error = EIO; 14316 goto out; 14317 } 14318 break; 14319 default: 14320 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14321 "nfs4_frlock: invalid cmd %d", cmd)); 14322 ep->error = EINVAL; 14323 goto out; 14324 } 14325 14326 if (!go_otw) 14327 goto out; 14328 } 14329 14330 /* XXX should we use the local reclock as a cache ? */ 14331 /* 14332 * Unregister the lock with the local locking code before 14333 * contacting the server. This avoids a potential race where 14334 * another process gets notified that it has been granted a lock 14335 * before we can unregister ourselves locally. 14336 */ 14337 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14338 if (ctype == NFS4_LCK_CTYPE_NORM) 14339 flk->l_pid = ttoproc(curthread)->p_pid; 14340 nfs4_register_lock_locally(vp, flk, flag, offset); 14341 } 14342 14343 /* 14344 * Send the server the lock request. Continually loop with a delay 14345 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14346 */ 14347 resp = &res; 14348 14349 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14350 (CE_NOTE, 14351 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14352 rnode4info(rp))); 14353 14354 if (lock_args && frc_no_reclaim) { 14355 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14356 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14357 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14358 lock_args->reclaim = FALSE; 14359 if (did_reclaimp) 14360 *did_reclaimp = 0; 14361 } 14362 14363 /* 14364 * Do the OTW call. 14365 */ 14366 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14367 14368 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14369 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14370 14371 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14372 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14373 "nfs4frlock: needrecov %d", needrecov)); 14374 14375 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14376 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14377 args.ctag); 14378 14379 /* 14380 * Check if one of these mutually exclusive error cases has 14381 * happened: 14382 * need to swap credentials due to access error 14383 * recovery is needed 14384 * different error (only known case is missing Kerberos ticket) 14385 */ 14386 14387 if ((ep->error == EACCES || 14388 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14389 cred_otw != cr) { 14390 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14391 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14392 cr, &cred_otw); 14393 goto recov_retry; 14394 } 14395 14396 if (needrecov) { 14397 /* 14398 * LOCKT requests don't need to recover from lost 14399 * requests since they don't create/modify state. 14400 */ 14401 if ((ep->error == EINTR || 14402 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14403 lockt_args) 14404 goto out; 14405 /* 14406 * Do not attempt recovery for requests initiated by 14407 * the recovery framework. Let the framework redrive them. 14408 */ 14409 if (ctype != NFS4_LCK_CTYPE_NORM) 14410 goto out; 14411 else { 14412 ASSERT(resend_rqstp == NULL); 14413 } 14414 14415 nfs4frlock_save_lost_rqst(ctype, ep->error, 14416 flk_to_locktype(cmd, flk->l_type), 14417 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14418 14419 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14420 &resp, lock_args, locku_args, &oop, &osp, &lop, 14421 rp, vp, &recov_state, op_hint, &did_start_fop, 14422 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14423 14424 if (retry) { 14425 ASSERT(oop == NULL); 14426 ASSERT(osp == NULL); 14427 ASSERT(lop == NULL); 14428 goto recov_retry; 14429 } 14430 goto out; 14431 } 14432 14433 /* 14434 * Bail out if have reached this point with ep->error set. Can 14435 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14436 * This happens if Kerberos ticket has expired or has been 14437 * destroyed. 14438 */ 14439 if (ep->error != 0) 14440 goto out; 14441 14442 /* 14443 * Process the reply. 14444 */ 14445 switch (resp->status) { 14446 case NFS4_OK: 14447 resop = &resp->array[1]; 14448 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14449 resend_rqstp); 14450 /* 14451 * Have a successful lock operation, now update state. 14452 */ 14453 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14454 resop, lop, vp, flk, cr, resend_rqstp); 14455 break; 14456 14457 case NFS4ERR_DENIED: 14458 resop = &resp->array[1]; 14459 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14460 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14461 &recov_state, needrecov, &argsp, &resp, 14462 &tick_delay, &whence, &ep->error, resop, cr, 14463 &did_start_fop, &skip_get_err); 14464 14465 if (retry) { 14466 ASSERT(oop == NULL); 14467 ASSERT(osp == NULL); 14468 ASSERT(lop == NULL); 14469 goto recov_retry; 14470 } 14471 break; 14472 /* 14473 * If the server won't let us reclaim, fall-back to trying to lock 14474 * the file from scratch. Code elsewhere will check the changeinfo 14475 * to ensure the file hasn't been changed. 14476 */ 14477 case NFS4ERR_NO_GRACE: 14478 if (lock_args && lock_args->reclaim == TRUE) { 14479 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14480 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14481 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14482 frc_no_reclaim = 1; 14483 /* clean up before retrying */ 14484 needrecov = 0; 14485 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14486 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14487 &recov_state, op_hint, &did_start_fop, NULL, flk); 14488 goto recov_retry; 14489 } 14490 /* FALLTHROUGH */ 14491 14492 default: 14493 nfs4frlock_results_default(resp, &ep->error); 14494 break; 14495 } 14496 out: 14497 /* 14498 * Process and cleanup from error. Make interrupted unlock 14499 * requests look successful, since they will be handled by the 14500 * client recovery code. 14501 */ 14502 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14503 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14504 lock_args, locku_args, did_start_fop, 14505 skip_get_err, cred_otw, cr); 14506 14507 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14508 (cmd == F_SETLK || cmd == F_SETLKW)) 14509 ep->error = 0; 14510 } 14511 14512 /* 14513 * nfs4_safelock: 14514 * 14515 * Return non-zero if the given lock request can be handled without 14516 * violating the constraints on concurrent mapping and locking. 14517 */ 14518 14519 static int 14520 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14521 { 14522 rnode4_t *rp = VTOR4(vp); 14523 struct vattr va; 14524 int error; 14525 14526 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14527 ASSERT(rp->r_mapcnt >= 0); 14528 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14529 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14530 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14531 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14532 14533 if (rp->r_mapcnt == 0) 14534 return (1); /* always safe if not mapped */ 14535 14536 /* 14537 * If the file is already mapped and there are locks, then they 14538 * should be all safe locks. So adding or removing a lock is safe 14539 * as long as the new request is safe (i.e., whole-file, meaning 14540 * length and starting offset are both zero). 14541 */ 14542 14543 if (bfp->l_start != 0 || bfp->l_len != 0) { 14544 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14545 "cannot lock a memory mapped file unless locking the " 14546 "entire file: start %"PRIx64", len %"PRIx64, 14547 bfp->l_start, bfp->l_len)); 14548 return (0); 14549 } 14550 14551 /* mandatory locking and mapping don't mix */ 14552 va.va_mask = AT_MODE; 14553 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 14554 if (error != 0) { 14555 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14556 "getattr error %d", error)); 14557 return (0); /* treat errors conservatively */ 14558 } 14559 if (MANDLOCK(vp, va.va_mode)) { 14560 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14561 "cannot mandatory lock and mmap a file")); 14562 return (0); 14563 } 14564 14565 return (1); 14566 } 14567 14568 14569 /* 14570 * Register the lock locally within Solaris. 14571 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14572 * recording locks locally. 14573 * 14574 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14575 * are registered locally. 14576 */ 14577 void 14578 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14579 u_offset_t offset) 14580 { 14581 int oldsysid; 14582 int error; 14583 #ifdef DEBUG 14584 char *name; 14585 #endif 14586 14587 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14588 14589 #ifdef DEBUG 14590 name = fn_name(VTOSV(vp)->sv_name); 14591 NFS4_DEBUG(nfs4_client_lock_debug, 14592 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14593 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14594 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14595 flk->l_sysid)); 14596 kmem_free(name, MAXNAMELEN); 14597 #endif 14598 14599 /* register the lock with local locking */ 14600 oldsysid = flk->l_sysid; 14601 flk->l_sysid |= LM_SYSID_CLIENT; 14602 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14603 #ifdef DEBUG 14604 if (error != 0) { 14605 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14606 "nfs4_register_lock_locally: could not register with" 14607 " local locking")); 14608 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14609 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14610 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14611 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14612 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14613 flk->l_type, flk->l_start, flk->l_len)); 14614 (void) reclock(vp, flk, 0, flag, offset, NULL); 14615 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14616 "blocked by pid %d sysid 0x%x type %d " 14617 "off 0x%" PRIx64 " len 0x%" PRIx64, 14618 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14619 flk->l_len)); 14620 } 14621 #endif 14622 flk->l_sysid = oldsysid; 14623 } 14624 14625 /* 14626 * nfs4_lockrelease: 14627 * 14628 * Release any locks on the given vnode that are held by the current 14629 * process. Also removes the lock owner (if one exists) from the rnode's 14630 * list. 14631 */ 14632 static int 14633 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14634 { 14635 flock64_t ld; 14636 int ret, error; 14637 rnode4_t *rp; 14638 nfs4_lock_owner_t *lop; 14639 nfs4_recov_state_t recov_state; 14640 mntinfo4_t *mi; 14641 bool_t possible_orphan = FALSE; 14642 bool_t recovonly; 14643 14644 ASSERT((uintptr_t)vp > KERNELBASE); 14645 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14646 14647 rp = VTOR4(vp); 14648 mi = VTOMI4(vp); 14649 14650 /* 14651 * If we have not locked anything then we can 14652 * just return since we have no work to do. 14653 */ 14654 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14655 return (0); 14656 } 14657 14658 /* 14659 * We need to comprehend that another thread may 14660 * kick off recovery and the lock_owner we have stashed 14661 * in lop might be invalid so we should NOT cache it 14662 * locally! 14663 */ 14664 recov_state.rs_flags = 0; 14665 recov_state.rs_num_retry_despite_err = 0; 14666 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14667 &recovonly); 14668 if (error) { 14669 mutex_enter(&rp->r_statelock); 14670 rp->r_flags |= R4LODANGLERS; 14671 mutex_exit(&rp->r_statelock); 14672 return (error); 14673 } 14674 14675 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14676 14677 /* 14678 * Check if the lock owner might have a lock (request was sent but 14679 * no response was received). Also check if there are any remote 14680 * locks on the file. (In theory we shouldn't have to make this 14681 * second check if there's no lock owner, but for now we'll be 14682 * conservative and do it anyway.) If either condition is true, 14683 * send an unlock for the entire file to the server. 14684 * 14685 * Note that no explicit synchronization is needed here. At worst, 14686 * flk_has_remote_locks() will return a false positive, in which case 14687 * the unlock call wastes time but doesn't harm correctness. 14688 */ 14689 14690 if (lop) { 14691 mutex_enter(&lop->lo_lock); 14692 possible_orphan = lop->lo_pending_rqsts; 14693 mutex_exit(&lop->lo_lock); 14694 lock_owner_rele(lop); 14695 } 14696 14697 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14698 14699 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14700 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14701 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14702 (void *)lop)); 14703 14704 if (possible_orphan || flk_has_remote_locks(vp)) { 14705 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14706 ld.l_whence = 0; /* unlock from start of file */ 14707 ld.l_start = 0; 14708 ld.l_len = 0; /* do entire file */ 14709 14710 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, 14711 cr, NULL); 14712 14713 if (ret != 0) { 14714 /* 14715 * If VOP_FRLOCK fails, make sure we unregister 14716 * local locks before we continue. 14717 */ 14718 ld.l_pid = ttoproc(curthread)->p_pid; 14719 nfs4_register_lock_locally(vp, &ld, flag, offset); 14720 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14721 "nfs4_lockrelease: lock release error on vp" 14722 " %p: error %d.\n", (void *)vp, ret)); 14723 } 14724 } 14725 14726 recov_state.rs_flags = 0; 14727 recov_state.rs_num_retry_despite_err = 0; 14728 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14729 &recovonly); 14730 if (error) { 14731 mutex_enter(&rp->r_statelock); 14732 rp->r_flags |= R4LODANGLERS; 14733 mutex_exit(&rp->r_statelock); 14734 return (error); 14735 } 14736 14737 /* 14738 * So, here we're going to need to retrieve the lock-owner 14739 * again (in case recovery has done a switch-a-roo) and 14740 * remove it because we can. 14741 */ 14742 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14743 14744 if (lop) { 14745 nfs4_rnode_remove_lock_owner(rp, lop); 14746 lock_owner_rele(lop); 14747 } 14748 14749 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14750 return (0); 14751 } 14752 14753 /* 14754 * Wait for 'tick_delay' clock ticks. 14755 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14756 * NOTE: lock_lease_time is in seconds. 14757 * 14758 * XXX For future improvements, should implement a waiting queue scheme. 14759 */ 14760 static int 14761 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14762 { 14763 long milliseconds_delay; 14764 time_t lock_lease_time; 14765 14766 /* wait tick_delay clock ticks or siginteruptus */ 14767 if (delay_sig(*tick_delay)) { 14768 return (EINTR); 14769 } 14770 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14771 "reissue the lock request: blocked for %ld clock ticks: %ld " 14772 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14773 14774 /* get the lease time */ 14775 lock_lease_time = r2lease_time(rp); 14776 14777 /* drv_hztousec converts ticks to microseconds */ 14778 milliseconds_delay = drv_hztousec(*tick_delay) / 1000; 14779 if (milliseconds_delay < lock_lease_time * 1000) { 14780 *tick_delay = 2 * *tick_delay; 14781 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000) 14782 *tick_delay = drv_usectohz(lock_lease_time*1000*1000); 14783 } 14784 return (0); 14785 } 14786 14787 14788 void 14789 nfs4_vnops_init(void) 14790 { 14791 } 14792 14793 void 14794 nfs4_vnops_fini(void) 14795 { 14796 } 14797 14798 /* 14799 * Return a reference to the directory (parent) vnode for a given vnode, 14800 * using the saved pathname information and the directory file handle. The 14801 * caller is responsible for disposing of the reference. 14802 * Returns zero or an errno value. 14803 * 14804 * Caller should set need_start_op to FALSE if it is the recovery 14805 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14806 */ 14807 int 14808 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14809 { 14810 svnode_t *svnp; 14811 vnode_t *dvp = NULL; 14812 servinfo4_t *svp; 14813 nfs4_fname_t *mfname; 14814 int error; 14815 14816 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14817 14818 if (vp->v_flag & VROOT) { 14819 nfs4_sharedfh_t *sfh; 14820 nfs_fh4 fh; 14821 mntinfo4_t *mi; 14822 14823 ASSERT(vp->v_type == VREG); 14824 14825 mi = VTOMI4(vp); 14826 svp = mi->mi_curr_serv; 14827 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14828 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14829 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14830 sfh = sfh4_get(&fh, VTOMI4(vp)); 14831 nfs_rw_exit(&svp->sv_lock); 14832 mfname = mi->mi_fname; 14833 fn_hold(mfname); 14834 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14835 sfh4_rele(&sfh); 14836 14837 if (dvp->v_type == VNON) 14838 dvp->v_type = VDIR; 14839 *dvpp = dvp; 14840 return (0); 14841 } 14842 14843 svnp = VTOSV(vp); 14844 14845 if (svnp == NULL) { 14846 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14847 "shadow node is NULL")); 14848 return (EINVAL); 14849 } 14850 14851 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14852 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14853 "shadow node name or dfh val == NULL")); 14854 return (EINVAL); 14855 } 14856 14857 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14858 (int)need_start_op); 14859 if (error != 0) { 14860 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14861 "nfs4_make_dotdot returned %d", error)); 14862 return (error); 14863 } 14864 if (!dvp) { 14865 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14866 "nfs4_make_dotdot returned a NULL dvp")); 14867 return (EIO); 14868 } 14869 if (dvp->v_type == VNON) 14870 dvp->v_type = VDIR; 14871 ASSERT(dvp->v_type == VDIR); 14872 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14873 mutex_enter(&dvp->v_lock); 14874 dvp->v_flag |= V_XATTRDIR; 14875 mutex_exit(&dvp->v_lock); 14876 } 14877 *dvpp = dvp; 14878 return (0); 14879 } 14880 14881 /* 14882 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14883 * length that fnamep can accept, including the trailing null. 14884 * Returns 0 if okay, returns an errno value if there was a problem. 14885 */ 14886 14887 int 14888 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14889 { 14890 char *fn; 14891 int err = 0; 14892 servinfo4_t *svp; 14893 svnode_t *shvp; 14894 14895 /* 14896 * If the file being opened has VROOT set, then this is 14897 * a "file" mount. sv_name will not be interesting, so 14898 * go back to the servinfo4 to get the original mount 14899 * path and strip off all but the final edge. Otherwise 14900 * just return the name from the shadow vnode. 14901 */ 14902 14903 if (vp->v_flag & VROOT) { 14904 14905 svp = VTOMI4(vp)->mi_curr_serv; 14906 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14907 14908 fn = strrchr(svp->sv_path, '/'); 14909 if (fn == NULL) 14910 err = EINVAL; 14911 else 14912 fn++; 14913 } else { 14914 shvp = VTOSV(vp); 14915 fn = fn_name(shvp->sv_name); 14916 } 14917 14918 if (err == 0) 14919 if (strlen(fn) < maxlen) 14920 (void) strcpy(fnamep, fn); 14921 else 14922 err = ENAMETOOLONG; 14923 14924 if (vp->v_flag & VROOT) 14925 nfs_rw_exit(&svp->sv_lock); 14926 else 14927 kmem_free(fn, MAXNAMELEN); 14928 14929 return (err); 14930 } 14931 14932 /* 14933 * Bookkeeping for a close that doesn't need to go over the wire. 14934 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14935 * it is left at 1. 14936 */ 14937 void 14938 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14939 { 14940 rnode4_t *rp; 14941 mntinfo4_t *mi; 14942 14943 mi = VTOMI4(vp); 14944 rp = VTOR4(vp); 14945 14946 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14947 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14948 ASSERT(nfs_zone() == mi->mi_zone); 14949 ASSERT(mutex_owned(&osp->os_sync_lock)); 14950 ASSERT(*have_lockp); 14951 14952 if (!osp->os_valid || 14953 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14954 return; 14955 } 14956 14957 /* 14958 * This removes the reference obtained at OPEN; ie, 14959 * when the open stream structure was created. 14960 * 14961 * We don't have to worry about calling 'open_stream_rele' 14962 * since we our currently holding a reference to this 14963 * open stream which means the count can not go to 0 with 14964 * this decrement. 14965 */ 14966 ASSERT(osp->os_ref_count >= 2); 14967 osp->os_ref_count--; 14968 osp->os_valid = 0; 14969 mutex_exit(&osp->os_sync_lock); 14970 *have_lockp = 0; 14971 14972 nfs4_dec_state_ref_count(mi); 14973 } 14974 14975 /* 14976 * Close all remaining open streams on the rnode. These open streams 14977 * could be here because: 14978 * - The close attempted at either close or delmap failed 14979 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14980 * - Someone did mknod on a regular file but never opened it 14981 */ 14982 int 14983 nfs4close_all(vnode_t *vp, cred_t *cr) 14984 { 14985 nfs4_open_stream_t *osp; 14986 int error; 14987 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14988 rnode4_t *rp; 14989 14990 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14991 14992 error = 0; 14993 rp = VTOR4(vp); 14994 14995 /* 14996 * At this point, all we know is that the last time 14997 * someone called vn_rele, the count was 1. Since then, 14998 * the vnode could have been re-activated. We want to 14999 * loop through the open streams and close each one, but 15000 * we have to be careful since once we release the rnode 15001 * hash bucket lock, someone else is free to come in and 15002 * re-activate the rnode and add new open streams. The 15003 * strategy is take the rnode hash bucket lock, verify that 15004 * the count is still 1, grab the open stream off the 15005 * head of the list and mark it invalid, then release the 15006 * rnode hash bucket lock and proceed with that open stream. 15007 * This is ok because nfs4close_one() will acquire the proper 15008 * open/create to close/destroy synchronization for open 15009 * streams, and will ensure that if someone has reopened 15010 * the open stream after we've dropped the hash bucket lock 15011 * then we'll just simply return without destroying the 15012 * open stream. 15013 * Repeat until the list is empty. 15014 */ 15015 15016 for (;;) { 15017 15018 /* make sure vnode hasn't been reactivated */ 15019 rw_enter(&rp->r_hashq->r_lock, RW_READER); 15020 mutex_enter(&vp->v_lock); 15021 if (vp->v_count > 1) { 15022 mutex_exit(&vp->v_lock); 15023 rw_exit(&rp->r_hashq->r_lock); 15024 break; 15025 } 15026 /* 15027 * Grabbing r_os_lock before releasing v_lock prevents 15028 * a window where the rnode/open stream could get 15029 * reactivated (and os_force_close set to 0) before we 15030 * had a chance to set os_force_close to 1. 15031 */ 15032 mutex_enter(&rp->r_os_lock); 15033 mutex_exit(&vp->v_lock); 15034 15035 osp = list_head(&rp->r_open_streams); 15036 if (!osp) { 15037 /* nothing left to CLOSE OTW, so return */ 15038 mutex_exit(&rp->r_os_lock); 15039 rw_exit(&rp->r_hashq->r_lock); 15040 break; 15041 } 15042 15043 mutex_enter(&rp->r_statev4_lock); 15044 /* the file can't still be mem mapped */ 15045 ASSERT(rp->r_mapcnt == 0); 15046 if (rp->created_v4) 15047 rp->created_v4 = 0; 15048 mutex_exit(&rp->r_statev4_lock); 15049 15050 /* 15051 * Grab a ref on this open stream; nfs4close_one 15052 * will mark it as invalid 15053 */ 15054 mutex_enter(&osp->os_sync_lock); 15055 osp->os_ref_count++; 15056 osp->os_force_close = 1; 15057 mutex_exit(&osp->os_sync_lock); 15058 mutex_exit(&rp->r_os_lock); 15059 rw_exit(&rp->r_hashq->r_lock); 15060 15061 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 15062 15063 /* Update error if it isn't already non-zero */ 15064 if (error == 0) { 15065 if (e.error) 15066 error = e.error; 15067 else if (e.stat) 15068 error = geterrno4(e.stat); 15069 } 15070 15071 #ifdef DEBUG 15072 nfs4close_all_cnt++; 15073 #endif 15074 /* Release the ref on osp acquired above. */ 15075 open_stream_rele(osp, rp); 15076 15077 /* Proceed to the next open stream, if any */ 15078 } 15079 return (error); 15080 } 15081 15082 /* 15083 * nfs4close_one - close one open stream for a file if needed. 15084 * 15085 * "close_type" indicates which close path this is: 15086 * CLOSE_NORM: close initiated via VOP_CLOSE. 15087 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 15088 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 15089 * the close and release of client state for this open stream 15090 * (unless someone else has the open stream open). 15091 * CLOSE_RESEND: indicates the request is a replay of an earlier request 15092 * (e.g., due to abort because of a signal). 15093 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 15094 * 15095 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 15096 * recovery. Instead, the caller is expected to deal with retries. 15097 * 15098 * The caller can either pass in the osp ('provided_osp') or not. 15099 * 15100 * 'access_bits' represents the access we are closing/downgrading. 15101 * 15102 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 15103 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 15104 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 15105 * 15106 * Errors are returned via the nfs4_error_t. 15107 */ 15108 void 15109 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 15110 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 15111 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 15112 uint_t mmap_flags) 15113 { 15114 nfs4_open_owner_t *oop; 15115 nfs4_open_stream_t *osp = NULL; 15116 int retry = 0; 15117 int num_retries = NFS4_NUM_RECOV_RETRIES; 15118 rnode4_t *rp; 15119 mntinfo4_t *mi; 15120 nfs4_recov_state_t recov_state; 15121 cred_t *cred_otw = NULL; 15122 bool_t recovonly = FALSE; 15123 int isrecov; 15124 int force_close; 15125 int close_failed = 0; 15126 int did_dec_count = 0; 15127 int did_start_op = 0; 15128 int did_force_recovlock = 0; 15129 int did_start_seqid_sync = 0; 15130 int have_sync_lock = 0; 15131 15132 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15133 15134 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 15135 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 15136 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 15137 len, maxprot, mmap_flags, access_bits)); 15138 15139 nfs4_error_zinit(ep); 15140 rp = VTOR4(vp); 15141 mi = VTOMI4(vp); 15142 isrecov = (close_type == CLOSE_RESEND || 15143 close_type == CLOSE_AFTER_RESEND); 15144 15145 /* 15146 * First get the open owner. 15147 */ 15148 if (!provided_osp) { 15149 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 15150 } else { 15151 oop = provided_osp->os_open_owner; 15152 ASSERT(oop != NULL); 15153 open_owner_hold(oop); 15154 } 15155 15156 if (!oop) { 15157 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15158 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 15159 "close type %d", (void *)rp, (void *)mi, (void *)cr, 15160 (void *)provided_osp, close_type)); 15161 ep->error = EIO; 15162 goto out; 15163 } 15164 15165 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 15166 recov_retry: 15167 osp = NULL; 15168 close_failed = 0; 15169 force_close = (close_type == CLOSE_FORCE); 15170 retry = 0; 15171 did_start_op = 0; 15172 did_force_recovlock = 0; 15173 did_start_seqid_sync = 0; 15174 have_sync_lock = 0; 15175 recovonly = FALSE; 15176 recov_state.rs_flags = 0; 15177 recov_state.rs_num_retry_despite_err = 0; 15178 15179 /* 15180 * Second synchronize with recovery. 15181 */ 15182 if (!isrecov) { 15183 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 15184 &recov_state, &recovonly); 15185 if (!ep->error) { 15186 did_start_op = 1; 15187 } else { 15188 close_failed = 1; 15189 /* 15190 * If we couldn't get start_fop, but have to 15191 * cleanup state, then at least acquire the 15192 * mi_recovlock so we can synchronize with 15193 * recovery. 15194 */ 15195 if (close_type == CLOSE_FORCE) { 15196 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 15197 RW_READER, FALSE); 15198 did_force_recovlock = 1; 15199 } else 15200 goto out; 15201 } 15202 } 15203 15204 /* 15205 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15206 * set 'recovonly' to TRUE since most likely this is due to 15207 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15208 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15209 * to retry, causing us to loop until recovery finishes. Plus we 15210 * don't need protection over the open seqid since we're not going 15211 * OTW, hence don't need to use the seqid. 15212 */ 15213 if (recovonly == FALSE) { 15214 /* need to grab the open owner sync before 'os_sync_lock' */ 15215 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15216 if (ep->error == EAGAIN) { 15217 ASSERT(!isrecov); 15218 if (did_start_op) 15219 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15220 &recov_state, TRUE); 15221 if (did_force_recovlock) 15222 nfs_rw_exit(&mi->mi_recovlock); 15223 goto recov_retry; 15224 } 15225 did_start_seqid_sync = 1; 15226 } 15227 15228 /* 15229 * Third get an open stream and acquire 'os_sync_lock' to 15230 * sychronize the opening/creating of an open stream with the 15231 * closing/destroying of an open stream. 15232 */ 15233 if (!provided_osp) { 15234 /* returns with 'os_sync_lock' held */ 15235 osp = find_open_stream(oop, rp); 15236 if (!osp) { 15237 ep->error = EIO; 15238 goto out; 15239 } 15240 } else { 15241 osp = provided_osp; 15242 open_stream_hold(osp); 15243 mutex_enter(&osp->os_sync_lock); 15244 } 15245 have_sync_lock = 1; 15246 15247 ASSERT(oop == osp->os_open_owner); 15248 15249 /* 15250 * Fourth, do any special pre-OTW CLOSE processing 15251 * based on the specific close type. 15252 */ 15253 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15254 !did_dec_count) { 15255 ASSERT(osp->os_open_ref_count > 0); 15256 osp->os_open_ref_count--; 15257 did_dec_count = 1; 15258 if (osp->os_open_ref_count == 0) 15259 osp->os_final_close = 1; 15260 } 15261 15262 if (close_type == CLOSE_FORCE) { 15263 /* see if somebody reopened the open stream. */ 15264 if (!osp->os_force_close) { 15265 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15266 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15267 "was reopened, vp %p", (void *)osp, (void *)vp)); 15268 ep->error = 0; 15269 ep->stat = NFS4_OK; 15270 goto out; 15271 } 15272 15273 if (!osp->os_final_close && !did_dec_count) { 15274 osp->os_open_ref_count--; 15275 did_dec_count = 1; 15276 } 15277 15278 /* 15279 * We can't depend on os_open_ref_count being 0 due to the 15280 * way executables are opened (VN_RELE to match a VOP_OPEN). 15281 */ 15282 #ifdef NOTYET 15283 ASSERT(osp->os_open_ref_count == 0); 15284 #endif 15285 if (osp->os_open_ref_count != 0) { 15286 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15287 "nfs4close_one: should panic here on an " 15288 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15289 "since this is probably the exec problem.")); 15290 15291 osp->os_open_ref_count = 0; 15292 } 15293 15294 /* 15295 * There is the possibility that nfs4close_one() 15296 * for close_type == CLOSE_DELMAP couldn't find the 15297 * open stream, thus couldn't decrement its os_mapcnt; 15298 * therefore we can't use this ASSERT yet. 15299 */ 15300 #ifdef NOTYET 15301 ASSERT(osp->os_mapcnt == 0); 15302 #endif 15303 osp->os_mapcnt = 0; 15304 } 15305 15306 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15307 ASSERT(osp->os_mapcnt >= btopr(len)); 15308 15309 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15310 osp->os_mmap_write -= btopr(len); 15311 if (maxprot & PROT_READ) 15312 osp->os_mmap_read -= btopr(len); 15313 if (maxprot & PROT_EXEC) 15314 osp->os_mmap_read -= btopr(len); 15315 /* mirror the PROT_NONE check in nfs4_addmap() */ 15316 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15317 !(maxprot & PROT_EXEC)) 15318 osp->os_mmap_read -= btopr(len); 15319 osp->os_mapcnt -= btopr(len); 15320 did_dec_count = 1; 15321 } 15322 15323 if (recovonly) { 15324 nfs4_lost_rqst_t lost_rqst; 15325 15326 /* request should not already be in recovery queue */ 15327 ASSERT(lrp == NULL); 15328 nfs4_error_init(ep, EINTR); 15329 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15330 osp, cred_otw, vp); 15331 mutex_exit(&osp->os_sync_lock); 15332 have_sync_lock = 0; 15333 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15334 lost_rqst.lr_op == OP_CLOSE ? 15335 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL); 15336 close_failed = 1; 15337 force_close = 0; 15338 goto close_cleanup; 15339 } 15340 15341 /* 15342 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15343 * we stopped operating on the open owner's <old oo_name, old seqid> 15344 * space, which means we stopped operating on the open stream 15345 * too. So don't go OTW (as the seqid is likely bad, and the 15346 * stateid could be stale, potentially triggering a false 15347 * setclientid), and just clean up the client's internal state. 15348 */ 15349 if (osp->os_orig_oo_name != oop->oo_name) { 15350 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15351 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15352 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15353 "oo_name %" PRIx64")", 15354 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15355 oop->oo_name)); 15356 close_failed = 1; 15357 } 15358 15359 /* If the file failed recovery, just quit. */ 15360 mutex_enter(&rp->r_statelock); 15361 if (rp->r_flags & R4RECOVERR) { 15362 close_failed = 1; 15363 } 15364 mutex_exit(&rp->r_statelock); 15365 15366 /* 15367 * If the force close path failed to obtain start_fop 15368 * then skip the OTW close and just remove the state. 15369 */ 15370 if (close_failed) 15371 goto close_cleanup; 15372 15373 /* 15374 * Fifth, check to see if there are still mapped pages or other 15375 * opens using this open stream. If there are then we can't 15376 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15377 */ 15378 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15379 nfs4_lost_rqst_t new_lost_rqst; 15380 bool_t needrecov = FALSE; 15381 cred_t *odg_cred_otw = NULL; 15382 seqid4 open_dg_seqid = 0; 15383 15384 if (osp->os_delegation) { 15385 /* 15386 * If this open stream was never OPENed OTW then we 15387 * surely can't DOWNGRADE it (especially since the 15388 * osp->open_stateid is really a delegation stateid 15389 * when os_delegation is 1). 15390 */ 15391 if (access_bits & FREAD) 15392 osp->os_share_acc_read--; 15393 if (access_bits & FWRITE) 15394 osp->os_share_acc_write--; 15395 osp->os_share_deny_none--; 15396 nfs4_error_zinit(ep); 15397 goto out; 15398 } 15399 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15400 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15401 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15402 if (needrecov && !isrecov) { 15403 bool_t abort; 15404 nfs4_bseqid_entry_t *bsep = NULL; 15405 15406 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15407 bsep = nfs4_create_bseqid_entry(oop, NULL, 15408 vp, 0, 15409 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15410 open_dg_seqid); 15411 15412 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15413 oop, osp, odg_cred_otw, vp, access_bits, 0); 15414 mutex_exit(&osp->os_sync_lock); 15415 have_sync_lock = 0; 15416 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15417 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15418 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15419 bsep, NULL, NULL); 15420 if (odg_cred_otw) 15421 crfree(odg_cred_otw); 15422 if (bsep) 15423 kmem_free(bsep, sizeof (*bsep)); 15424 15425 if (abort == TRUE) 15426 goto out; 15427 15428 if (did_start_seqid_sync) { 15429 nfs4_end_open_seqid_sync(oop); 15430 did_start_seqid_sync = 0; 15431 } 15432 open_stream_rele(osp, rp); 15433 15434 if (did_start_op) 15435 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15436 &recov_state, FALSE); 15437 if (did_force_recovlock) 15438 nfs_rw_exit(&mi->mi_recovlock); 15439 15440 goto recov_retry; 15441 } else { 15442 if (odg_cred_otw) 15443 crfree(odg_cred_otw); 15444 } 15445 goto out; 15446 } 15447 15448 /* 15449 * If this open stream was created as the results of an open 15450 * while holding a delegation, then just release it; no need 15451 * to do an OTW close. Otherwise do a "normal" OTW close. 15452 */ 15453 if (osp->os_delegation) { 15454 nfs4close_notw(vp, osp, &have_sync_lock); 15455 nfs4_error_zinit(ep); 15456 goto out; 15457 } 15458 15459 /* 15460 * If this stream is not valid, we're done. 15461 */ 15462 if (!osp->os_valid) { 15463 nfs4_error_zinit(ep); 15464 goto out; 15465 } 15466 15467 /* 15468 * Last open or mmap ref has vanished, need to do an OTW close. 15469 * First check to see if a close is still necessary. 15470 */ 15471 if (osp->os_failed_reopen) { 15472 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15473 "don't close OTW osp %p since reopen failed.", 15474 (void *)osp)); 15475 /* 15476 * Reopen of the open stream failed, hence the 15477 * stateid of the open stream is invalid/stale, and 15478 * sending this OTW would incorrectly cause another 15479 * round of recovery. In this case, we need to set 15480 * the 'os_valid' bit to 0 so another thread doesn't 15481 * come in and re-open this open stream before 15482 * this "closing" thread cleans up state (decrementing 15483 * the nfs4_server_t's state_ref_count and decrementing 15484 * the os_ref_count). 15485 */ 15486 osp->os_valid = 0; 15487 /* 15488 * This removes the reference obtained at OPEN; ie, 15489 * when the open stream structure was created. 15490 * 15491 * We don't have to worry about calling 'open_stream_rele' 15492 * since we our currently holding a reference to this 15493 * open stream which means the count can not go to 0 with 15494 * this decrement. 15495 */ 15496 ASSERT(osp->os_ref_count >= 2); 15497 osp->os_ref_count--; 15498 nfs4_error_zinit(ep); 15499 close_failed = 0; 15500 goto close_cleanup; 15501 } 15502 15503 ASSERT(osp->os_ref_count > 1); 15504 15505 /* 15506 * Sixth, try the CLOSE OTW. 15507 */ 15508 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15509 close_type, ep, &have_sync_lock); 15510 15511 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15512 /* 15513 * Let the recovery thread be responsible for 15514 * removing the state for CLOSE. 15515 */ 15516 close_failed = 1; 15517 force_close = 0; 15518 retry = 0; 15519 } 15520 15521 /* See if we need to retry with a different cred */ 15522 if ((ep->error == EACCES || 15523 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15524 cred_otw != cr) { 15525 crfree(cred_otw); 15526 cred_otw = cr; 15527 crhold(cred_otw); 15528 retry = 1; 15529 } 15530 15531 if (ep->error || ep->stat) 15532 close_failed = 1; 15533 15534 if (retry && !isrecov && num_retries-- > 0) { 15535 if (have_sync_lock) { 15536 mutex_exit(&osp->os_sync_lock); 15537 have_sync_lock = 0; 15538 } 15539 if (did_start_seqid_sync) { 15540 nfs4_end_open_seqid_sync(oop); 15541 did_start_seqid_sync = 0; 15542 } 15543 open_stream_rele(osp, rp); 15544 15545 if (did_start_op) 15546 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15547 &recov_state, FALSE); 15548 if (did_force_recovlock) 15549 nfs_rw_exit(&mi->mi_recovlock); 15550 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15551 "nfs4close_one: need to retry the close " 15552 "operation")); 15553 goto recov_retry; 15554 } 15555 close_cleanup: 15556 /* 15557 * Seventh and lastly, process our results. 15558 */ 15559 if (close_failed && force_close) { 15560 /* 15561 * It's ok to drop and regrab the 'os_sync_lock' since 15562 * nfs4close_notw() will recheck to make sure the 15563 * "close"/removal of state should happen. 15564 */ 15565 if (!have_sync_lock) { 15566 mutex_enter(&osp->os_sync_lock); 15567 have_sync_lock = 1; 15568 } 15569 /* 15570 * This is last call, remove the ref on the open 15571 * stream created by open and clean everything up. 15572 */ 15573 osp->os_pending_close = 0; 15574 nfs4close_notw(vp, osp, &have_sync_lock); 15575 nfs4_error_zinit(ep); 15576 } 15577 15578 if (!close_failed) { 15579 if (have_sync_lock) { 15580 osp->os_pending_close = 0; 15581 mutex_exit(&osp->os_sync_lock); 15582 have_sync_lock = 0; 15583 } else { 15584 mutex_enter(&osp->os_sync_lock); 15585 osp->os_pending_close = 0; 15586 mutex_exit(&osp->os_sync_lock); 15587 } 15588 if (did_start_op && recov_state.rs_sp != NULL) { 15589 mutex_enter(&recov_state.rs_sp->s_lock); 15590 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15591 mutex_exit(&recov_state.rs_sp->s_lock); 15592 } else { 15593 nfs4_dec_state_ref_count(mi); 15594 } 15595 nfs4_error_zinit(ep); 15596 } 15597 15598 out: 15599 if (have_sync_lock) 15600 mutex_exit(&osp->os_sync_lock); 15601 if (did_start_op) 15602 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15603 recovonly ? TRUE : FALSE); 15604 if (did_force_recovlock) 15605 nfs_rw_exit(&mi->mi_recovlock); 15606 if (cred_otw) 15607 crfree(cred_otw); 15608 if (osp) 15609 open_stream_rele(osp, rp); 15610 if (oop) { 15611 if (did_start_seqid_sync) 15612 nfs4_end_open_seqid_sync(oop); 15613 open_owner_rele(oop); 15614 } 15615 } 15616 15617 /* 15618 * Convert information returned by the server in the LOCK4denied 15619 * structure to the form required by fcntl. 15620 */ 15621 static void 15622 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15623 { 15624 nfs4_lo_name_t *lo; 15625 15626 #ifdef DEBUG 15627 if (denied_to_flk_debug) { 15628 lockt_denied_debug = lockt_denied; 15629 debug_enter("lockt_denied"); 15630 } 15631 #endif 15632 15633 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15634 flk->l_whence = 0; /* aka SEEK_SET */ 15635 flk->l_start = lockt_denied->offset; 15636 flk->l_len = lockt_denied->length; 15637 15638 /* 15639 * If the blocking clientid matches our client id, then we can 15640 * interpret the lockowner (since we built it). If not, then 15641 * fabricate a sysid and pid. Note that the l_sysid field 15642 * in *flk already has the local sysid. 15643 */ 15644 15645 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15646 15647 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15648 lo = (nfs4_lo_name_t *) 15649 lockt_denied->owner.owner_val; 15650 15651 flk->l_pid = lo->ln_pid; 15652 } else { 15653 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15654 "denied_to_flk: bad lock owner length\n")); 15655 15656 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15657 } 15658 } else { 15659 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15660 "denied_to_flk: foreign clientid\n")); 15661 15662 /* 15663 * Construct a new sysid which should be different from 15664 * sysids of other systems. 15665 */ 15666 15667 flk->l_sysid++; 15668 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15669 } 15670 } 15671 15672 static pid_t 15673 lo_to_pid(lock_owner4 *lop) 15674 { 15675 pid_t pid = 0; 15676 uchar_t *cp; 15677 int i; 15678 15679 cp = (uchar_t *)&lop->clientid; 15680 15681 for (i = 0; i < sizeof (lop->clientid); i++) 15682 pid += (pid_t)*cp++; 15683 15684 cp = (uchar_t *)lop->owner_val; 15685 15686 for (i = 0; i < lop->owner_len; i++) 15687 pid += (pid_t)*cp++; 15688 15689 return (pid); 15690 } 15691 15692 /* 15693 * Given a lock pointer, returns the length of that lock. 15694 * "end" is the last locked offset the "l_len" covers from 15695 * the start of the lock. 15696 */ 15697 static off64_t 15698 lock_to_end(flock64_t *lock) 15699 { 15700 off64_t lock_end; 15701 15702 if (lock->l_len == 0) 15703 lock_end = (off64_t)MAXEND; 15704 else 15705 lock_end = lock->l_start + lock->l_len - 1; 15706 15707 return (lock_end); 15708 } 15709 15710 /* 15711 * Given the end of a lock, it will return you the length "l_len" for that lock. 15712 */ 15713 static off64_t 15714 end_to_len(off64_t start, off64_t end) 15715 { 15716 off64_t lock_len; 15717 15718 ASSERT(end >= start); 15719 if (end == MAXEND) 15720 lock_len = 0; 15721 else 15722 lock_len = end - start + 1; 15723 15724 return (lock_len); 15725 } 15726 15727 /* 15728 * On given end for a lock it determines if it is the last locked offset 15729 * or not, if so keeps it as is, else adds one to return the length for 15730 * valid start. 15731 */ 15732 static off64_t 15733 start_check(off64_t x) 15734 { 15735 if (x == MAXEND) 15736 return (x); 15737 else 15738 return (x + 1); 15739 } 15740 15741 /* 15742 * See if these two locks overlap, and if so return 1; 15743 * otherwise, return 0. 15744 */ 15745 static int 15746 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15747 { 15748 off64_t llfp_end, curfp_end; 15749 15750 llfp_end = lock_to_end(llfp); 15751 curfp_end = lock_to_end(curfp); 15752 15753 if (((llfp_end >= curfp->l_start) && 15754 (llfp->l_start <= curfp->l_start)) || 15755 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15756 return (1); 15757 return (0); 15758 } 15759 15760 /* 15761 * Determine what the intersecting lock region is, and add that to the 15762 * 'nl_llpp' locklist in increasing order (by l_start). 15763 */ 15764 static void 15765 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15766 locklist_t **nl_llpp, vnode_t *vp) 15767 { 15768 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15769 off64_t lost_flp_end, local_flp_end, len, start; 15770 15771 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15772 15773 if (!locks_intersect(lost_flp, local_flp)) 15774 return; 15775 15776 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15777 "locks intersect")); 15778 15779 lost_flp_end = lock_to_end(lost_flp); 15780 local_flp_end = lock_to_end(local_flp); 15781 15782 /* Find the starting point of the intersecting region */ 15783 if (local_flp->l_start > lost_flp->l_start) 15784 start = local_flp->l_start; 15785 else 15786 start = lost_flp->l_start; 15787 15788 /* Find the lenght of the intersecting region */ 15789 if (lost_flp_end < local_flp_end) 15790 len = end_to_len(start, lost_flp_end); 15791 else 15792 len = end_to_len(start, local_flp_end); 15793 15794 /* 15795 * Prepare the flock structure for the intersection found and insert 15796 * it into the new list in increasing l_start order. This list contains 15797 * intersections of locks registered by the client with the local host 15798 * and the lost lock. 15799 * The lock type of this lock is the same as that of the local_flp. 15800 */ 15801 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15802 intersect_llp->ll_flock.l_start = start; 15803 intersect_llp->ll_flock.l_len = len; 15804 intersect_llp->ll_flock.l_type = local_flp->l_type; 15805 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15806 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15807 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15808 intersect_llp->ll_vp = vp; 15809 15810 tmp_fllp = *nl_llpp; 15811 cur_fllp = NULL; 15812 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15813 intersect_llp->ll_flock.l_start) { 15814 cur_fllp = tmp_fllp; 15815 tmp_fllp = tmp_fllp->ll_next; 15816 } 15817 if (cur_fllp == NULL) { 15818 /* first on the list */ 15819 intersect_llp->ll_next = *nl_llpp; 15820 *nl_llpp = intersect_llp; 15821 } else { 15822 intersect_llp->ll_next = cur_fllp->ll_next; 15823 cur_fllp->ll_next = intersect_llp; 15824 } 15825 15826 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15827 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15828 intersect_llp->ll_flock.l_start, 15829 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15830 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15831 } 15832 15833 /* 15834 * Our local locking current state is potentially different than 15835 * what the NFSv4 server thinks we have due to a lost lock that was 15836 * resent and then received. We need to reset our "NFSv4" locking 15837 * state to match the current local locking state for this pid since 15838 * that is what the user/application sees as what the world is. 15839 * 15840 * We cannot afford to drop the open/lock seqid sync since then we can 15841 * get confused about what the current local locking state "is" versus 15842 * "was". 15843 * 15844 * If we are unable to fix up the locks, we send SIGLOST to the affected 15845 * process. This is not done if the filesystem has been forcibly 15846 * unmounted, in case the process has already exited and a new process 15847 * exists with the same pid. 15848 */ 15849 static void 15850 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15851 nfs4_lock_owner_t *lop) 15852 { 15853 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15854 mntinfo4_t *mi = VTOMI4(vp); 15855 const int cmd = F_SETLK; 15856 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15857 flock64_t ul_fl; 15858 15859 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15860 "nfs4_reinstitute_local_lock_state")); 15861 15862 /* 15863 * Find active locks for this vp from the local locking code. 15864 * Scan through this list and find out the locks that intersect with 15865 * the lost lock. Once we find the lock that intersects, add the 15866 * intersection area as a new lock to a new list "ri_llp". The lock 15867 * type of the intersection region lock added to ri_llp is the same 15868 * as that found in the active lock list, "list". The intersecting 15869 * region locks are added to ri_llp in increasing l_start order. 15870 */ 15871 ASSERT(nfs_zone() == mi->mi_zone); 15872 15873 locks = flk_active_locks_for_vp(vp); 15874 ri_llp = NULL; 15875 15876 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15877 ASSERT(llp->ll_vp == vp); 15878 /* 15879 * Pick locks that belong to this pid/lockowner 15880 */ 15881 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15882 continue; 15883 15884 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15885 } 15886 15887 /* 15888 * Now we have the list of intersections with the lost lock. These are 15889 * the locks that were/are active before the server replied to the 15890 * last/lost lock. Issue these locks to the server here. Playing these 15891 * locks to the server will re-establish aur current local locking state 15892 * with the v4 server. 15893 * If we get an error, send SIGLOST to the application for that lock. 15894 */ 15895 15896 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15897 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15898 "nfs4_reinstitute_local_lock_state: need to issue " 15899 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15900 llp->ll_flock.l_start, 15901 llp->ll_flock.l_start + llp->ll_flock.l_len, 15902 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15903 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15904 /* 15905 * No need to relock what we already have 15906 */ 15907 if (llp->ll_flock.l_type == lost_flp->l_type) 15908 continue; 15909 15910 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15911 } 15912 15913 /* 15914 * Now keeping the start of the lost lock as our reference parse the 15915 * newly created ri_llp locklist to find the ranges that we have locked 15916 * with the v4 server but not in the current local locking. We need 15917 * to unlock these ranges. 15918 * These ranges can also be reffered to as those ranges, where the lost 15919 * lock does not overlap with the locks in the ri_llp but are locked 15920 * since the server replied to the lost lock. 15921 */ 15922 cur_start = lost_flp->l_start; 15923 lost_flp_end = lock_to_end(lost_flp); 15924 15925 ul_fl.l_type = F_UNLCK; 15926 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15927 ul_fl.l_sysid = lost_flp->l_sysid; 15928 ul_fl.l_pid = lost_flp->l_pid; 15929 15930 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15931 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15932 15933 if (llp->ll_flock.l_start <= cur_start) { 15934 cur_start = start_check(llp_ll_flock_end); 15935 continue; 15936 } 15937 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15938 "nfs4_reinstitute_local_lock_state: " 15939 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15940 cur_start, llp->ll_flock.l_start)); 15941 15942 ul_fl.l_start = cur_start; 15943 ul_fl.l_len = end_to_len(cur_start, 15944 (llp->ll_flock.l_start - 1)); 15945 15946 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15947 cur_start = start_check(llp_ll_flock_end); 15948 } 15949 15950 /* 15951 * In the case where the lost lock ends after all intersecting locks, 15952 * unlock the last part of the lost lock range. 15953 */ 15954 if (cur_start != start_check(lost_flp_end)) { 15955 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15956 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15957 "lost lock region [%"PRIx64" - %"PRIx64"]", 15958 cur_start, lost_flp->l_start + lost_flp->l_len)); 15959 15960 ul_fl.l_start = cur_start; 15961 /* 15962 * Is it an to-EOF lock? if so unlock till the end 15963 */ 15964 if (lost_flp->l_len == 0) 15965 ul_fl.l_len = 0; 15966 else 15967 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15968 15969 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15970 } 15971 15972 if (locks != NULL) 15973 flk_free_locklist(locks); 15974 15975 /* Free up our newly created locklist */ 15976 for (llp = ri_llp; llp != NULL; ) { 15977 tmp_llp = llp->ll_next; 15978 kmem_free(llp, sizeof (locklist_t)); 15979 llp = tmp_llp; 15980 } 15981 15982 /* 15983 * Now return back to the original calling nfs4frlock() 15984 * and let us naturally drop our seqid syncs. 15985 */ 15986 } 15987 15988 /* 15989 * Create a lost state record for the given lock reinstantiation request 15990 * and push it onto the lost state queue. 15991 */ 15992 static void 15993 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15994 nfs4_lock_owner_t *lop) 15995 { 15996 nfs4_lost_rqst_t req; 15997 nfs_lock_type4 locktype; 15998 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15999 16000 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 16001 16002 locktype = flk_to_locktype(cmd, flk->l_type); 16003 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 16004 NULL, NULL, lop, flk, &req, cr, vp); 16005 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 16006 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 16007 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 16008 NULL, NULL, NULL); 16009 }