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 /* 41 * Copyright (c) 2014, STRATO AG. All rights reserved. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/types.h> 46 #include <sys/systm.h> 47 #include <sys/cred.h> 48 #include <sys/time.h> 49 #include <sys/vnode.h> 50 #include <sys/vfs.h> 51 #include <sys/vfs_opreg.h> 52 #include <sys/file.h> 53 #include <sys/filio.h> 54 #include <sys/uio.h> 55 #include <sys/buf.h> 56 #include <sys/mman.h> 57 #include <sys/pathname.h> 58 #include <sys/dirent.h> 59 #include <sys/debug.h> 60 #include <sys/vmsystm.h> 61 #include <sys/fcntl.h> 62 #include <sys/flock.h> 63 #include <sys/swap.h> 64 #include <sys/errno.h> 65 #include <sys/strsubr.h> 66 #include <sys/sysmacros.h> 67 #include <sys/kmem.h> 68 #include <sys/cmn_err.h> 69 #include <sys/pathconf.h> 70 #include <sys/utsname.h> 71 #include <sys/dnlc.h> 72 #include <sys/acl.h> 73 #include <sys/systeminfo.h> 74 #include <sys/policy.h> 75 #include <sys/sdt.h> 76 #include <sys/list.h> 77 #include <sys/stat.h> 78 #include <sys/zone.h> 79 80 #include <rpc/types.h> 81 #include <rpc/auth.h> 82 #include <rpc/clnt.h> 83 84 #include <nfs/nfs.h> 85 #include <nfs/nfs_clnt.h> 86 #include <nfs/nfs_acl.h> 87 #include <nfs/lm.h> 88 #include <nfs/nfs4.h> 89 #include <nfs/nfs4_kprot.h> 90 #include <nfs/rnode4.h> 91 #include <nfs/nfs4_clnt.h> 92 93 #include <vm/hat.h> 94 #include <vm/as.h> 95 #include <vm/page.h> 96 #include <vm/pvn.h> 97 #include <vm/seg.h> 98 #include <vm/seg_map.h> 99 #include <vm/seg_kpm.h> 100 #include <vm/seg_vn.h> 101 102 #include <fs/fs_subr.h> 103 104 #include <sys/ddi.h> 105 #include <sys/int_fmtio.h> 106 #include <sys/fs/autofs.h> 107 108 typedef struct { 109 nfs4_ga_res_t *di_garp; 110 cred_t *di_cred; 111 hrtime_t di_time_call; 112 } dirattr_info_t; 113 114 typedef enum nfs4_acl_op { 115 NFS4_ACL_GET, 116 NFS4_ACL_SET 117 } nfs4_acl_op_t; 118 119 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi); 120 121 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 122 char *, dirattr_info_t *); 123 124 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 125 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 126 nfs4_error_t *, int *); 127 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 128 cred_t *); 129 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 130 stable_how4 *); 131 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 132 cred_t *, bool_t, struct uio *); 133 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 134 vsecattr_t *); 135 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 136 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 137 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 138 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 139 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 140 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 141 int, vnode_t **, cred_t *); 142 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 143 cred_t *, int, int, enum createmode4, int); 144 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 145 caller_context_t *); 146 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 147 vnode_t *, char *, cred_t *, nfsstat4 *); 148 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 149 vnode_t *, char *, cred_t *, nfsstat4 *); 150 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 151 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 152 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 153 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 154 page_t *[], size_t, struct seg *, caddr_t, 155 enum seg_rw, cred_t *); 156 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 157 cred_t *); 158 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 159 int, cred_t *); 160 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 161 int, cred_t *); 162 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 163 static void nfs4_set_mod(vnode_t *); 164 static void nfs4_get_commit(vnode_t *); 165 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 166 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 167 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 168 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 169 cred_t *); 170 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 171 cred_t *); 172 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 173 hrtime_t, vnode_t *, cred_t *); 174 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 175 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 176 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 177 u_offset_t); 178 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 179 static int nfs4_block_and_wait(clock_t *); 180 static cred_t *state_to_cred(nfs4_open_stream_t *); 181 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 182 static pid_t lo_to_pid(lock_owner4 *); 183 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 184 cred_t *, nfs4_lock_owner_t *); 185 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 186 nfs4_lock_owner_t *); 187 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **); 188 static void nfs4_delmap_callback(struct as *, void *, uint_t); 189 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 190 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 191 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 192 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 193 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 194 uid_t, gid_t, int); 195 196 /* 197 * Routines that implement the setting of v4 args for the misc. ops 198 */ 199 static void nfs4args_lock_free(nfs_argop4 *); 200 static void nfs4args_lockt_free(nfs_argop4 *); 201 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 202 int, rnode4_t *, cred_t *, bitmap4, int *, 203 nfs4_stateid_types_t *); 204 static void nfs4args_setattr_free(nfs_argop4 *); 205 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 206 bitmap4); 207 static void nfs4args_verify_free(nfs_argop4 *); 208 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 209 WRITE4args **, nfs4_stateid_types_t *); 210 211 /* 212 * These are the vnode ops functions that implement the vnode interface to 213 * the networked file system. See more comments below at nfs4_vnodeops. 214 */ 215 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *); 216 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *, 217 caller_context_t *); 218 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 219 caller_context_t *); 220 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 221 caller_context_t *); 222 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *, 223 caller_context_t *); 224 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 225 caller_context_t *); 226 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *); 227 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *, 228 caller_context_t *); 229 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *); 230 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 231 int, vnode_t **, cred_t *, int, caller_context_t *, 232 vsecattr_t *); 233 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *, 234 int); 235 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *, 236 caller_context_t *, int); 237 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 238 caller_context_t *, int); 239 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **, 240 cred_t *, caller_context_t *, int, vsecattr_t *); 241 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *, 242 caller_context_t *, int); 243 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 244 cred_t *, caller_context_t *, int); 245 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *, 246 caller_context_t *, int); 247 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *); 248 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 249 page_t *[], size_t, struct seg *, caddr_t, 250 enum seg_rw, cred_t *, caller_context_t *); 251 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *, 252 caller_context_t *); 253 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t, 254 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 255 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 256 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 257 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *); 258 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 259 struct flk_callback *, cred_t *, caller_context_t *); 260 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 261 cred_t *, caller_context_t *); 262 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 263 uint_t, uint_t, uint_t, cred_t *, caller_context_t *); 264 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 265 cred_t *, caller_context_t *); 266 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *, 267 caller_context_t *); 268 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 269 caller_context_t *); 270 /* 271 * These vnode ops are required to be called from outside this source file, 272 * e.g. by ephemeral mount stub vnode ops, and so may not be declared 273 * as static. 274 */ 275 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *, 276 caller_context_t *); 277 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *); 278 int nfs4_lookup(vnode_t *, char *, vnode_t **, 279 struct pathname *, int, vnode_t *, cred_t *, 280 caller_context_t *, int *, pathname_t *); 281 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *); 282 int nfs4_rwlock(vnode_t *, int, caller_context_t *); 283 void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 284 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *); 285 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *, 286 caller_context_t *); 287 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 288 caller_context_t *); 289 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *, 290 caller_context_t *); 291 292 /* 293 * Used for nfs4_commit_vp() to indicate if we should 294 * wait on pending writes. 295 */ 296 #define NFS4_WRITE_NOWAIT 0 297 #define NFS4_WRITE_WAIT 1 298 299 /* 300 * Error flags used to pass information about certain special errors 301 * which need to be handled specially. 302 */ 303 #define NFS_EOF -98 304 #define NFS_VERF_MISMATCH -97 305 306 /* 307 * Flags used to differentiate between which operation drove the 308 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 309 */ 310 #define NFS4_CLOSE_OP 0x1 311 #define NFS4_DELMAP_OP 0x2 312 #define NFS4_INACTIVE_OP 0x3 313 314 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 315 316 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 317 #define ALIGN64(x, ptr, sz) \ 318 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 319 if (x) { \ 320 x = sizeof (uint64_t) - (x); \ 321 sz -= (x); \ 322 ptr += (x); \ 323 } 324 325 #ifdef DEBUG 326 int nfs4_client_attr_debug = 0; 327 int nfs4_client_state_debug = 0; 328 int nfs4_client_shadow_debug = 0; 329 int nfs4_client_lock_debug = 0; 330 int nfs4_seqid_sync = 0; 331 int nfs4_client_map_debug = 0; 332 static int nfs4_pageio_debug = 0; 333 int nfs4_client_inactive_debug = 0; 334 int nfs4_client_recov_debug = 0; 335 int nfs4_client_failover_debug = 0; 336 int nfs4_client_call_debug = 0; 337 int nfs4_client_lookup_debug = 0; 338 int nfs4_client_zone_debug = 0; 339 int nfs4_lost_rqst_debug = 0; 340 int nfs4_rdattrerr_debug = 0; 341 int nfs4_open_stream_debug = 0; 342 343 int nfs4read_error_inject; 344 345 static int nfs4_create_misses = 0; 346 347 static int nfs4_readdir_cache_shorts = 0; 348 static int nfs4_readdir_readahead = 0; 349 350 static int nfs4_bio_do_stop = 0; 351 352 static int nfs4_lostpage = 0; /* number of times we lost original page */ 353 354 int nfs4_mmap_debug = 0; 355 356 static int nfs4_pathconf_cache_hits = 0; 357 static int nfs4_pathconf_cache_misses = 0; 358 359 int nfs4close_all_cnt; 360 int nfs4close_one_debug = 0; 361 int nfs4close_notw_debug = 0; 362 363 int denied_to_flk_debug = 0; 364 void *lockt_denied_debug; 365 366 #endif 367 368 /* 369 * In milliseconds. Should be less than half of the lease time or better, 370 * less than one second. 371 */ 372 int nfs4_base_wait_time = 20; 373 int nfs4_max_base_wait_time = 1 * 1000; /* 1 sec */ 374 375 /* 376 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 377 * or NFS4ERR_RESOURCE. 378 */ 379 static int confirm_retry_sec = 30; 380 381 static int nfs4_lookup_neg_cache = 1; 382 383 /* 384 * number of pages to read ahead 385 * optimized for 100 base-T. 386 */ 387 static int nfs4_nra = 4; 388 389 static int nfs4_do_symlink_cache = 1; 390 391 static int nfs4_pathconf_disable_cache = 0; 392 393 /* 394 * These are the vnode ops routines which implement the vnode interface to 395 * the networked file system. These routines just take their parameters, 396 * make them look networkish by putting the right info into interface structs, 397 * and then calling the appropriate remote routine(s) to do the work. 398 * 399 * Note on directory name lookup cacheing: If we detect a stale fhandle, 400 * we purge the directory cache relative to that vnode. This way, the 401 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 402 * more details on rnode locking. 403 */ 404 405 struct vnodeops *nfs4_vnodeops; 406 407 const fs_operation_def_t nfs4_vnodeops_template[] = { 408 VOPNAME_OPEN, { .vop_open = nfs4_open }, 409 VOPNAME_CLOSE, { .vop_close = nfs4_close }, 410 VOPNAME_READ, { .vop_read = nfs4_read }, 411 VOPNAME_WRITE, { .vop_write = nfs4_write }, 412 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl }, 413 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr }, 414 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr }, 415 VOPNAME_ACCESS, { .vop_access = nfs4_access }, 416 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup }, 417 VOPNAME_CREATE, { .vop_create = nfs4_create }, 418 VOPNAME_REMOVE, { .vop_remove = nfs4_remove }, 419 VOPNAME_LINK, { .vop_link = nfs4_link }, 420 VOPNAME_RENAME, { .vop_rename = nfs4_rename }, 421 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir }, 422 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir }, 423 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir }, 424 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink }, 425 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink }, 426 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync }, 427 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive }, 428 VOPNAME_FID, { .vop_fid = nfs4_fid }, 429 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock }, 430 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock }, 431 VOPNAME_SEEK, { .vop_seek = nfs4_seek }, 432 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock }, 433 VOPNAME_SPACE, { .vop_space = nfs4_space }, 434 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp }, 435 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage }, 436 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage }, 437 VOPNAME_MAP, { .vop_map = nfs4_map }, 438 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap }, 439 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap }, 440 /* no separate nfs4_dump */ 441 VOPNAME_DUMP, { .vop_dump = nfs_dump }, 442 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf }, 443 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio }, 444 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose }, 445 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr }, 446 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr }, 447 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock }, 448 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 449 NULL, NULL 450 }; 451 452 /* 453 * The following are subroutines and definitions to set args or get res 454 * for the different nfsv4 ops 455 */ 456 457 void 458 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 459 { 460 int i; 461 462 for (i = 0; i < arglen; i++) { 463 if (argop[i].argop == OP_LOOKUP) { 464 kmem_free( 465 argop[i].nfs_argop4_u.oplookup. 466 objname.utf8string_val, 467 argop[i].nfs_argop4_u.oplookup. 468 objname.utf8string_len); 469 } 470 } 471 } 472 473 static void 474 nfs4args_lock_free(nfs_argop4 *argop) 475 { 476 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 477 478 if (locker->new_lock_owner == TRUE) { 479 open_to_lock_owner4 *open_owner; 480 481 open_owner = &locker->locker4_u.open_owner; 482 if (open_owner->lock_owner.owner_val != NULL) { 483 kmem_free(open_owner->lock_owner.owner_val, 484 open_owner->lock_owner.owner_len); 485 } 486 } 487 } 488 489 static void 490 nfs4args_lockt_free(nfs_argop4 *argop) 491 { 492 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 493 494 if (lowner->owner_val != NULL) { 495 kmem_free(lowner->owner_val, lowner->owner_len); 496 } 497 } 498 499 static void 500 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 501 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 502 nfs4_stateid_types_t *sid_types) 503 { 504 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 505 mntinfo4_t *mi; 506 507 argop->argop = OP_SETATTR; 508 /* 509 * The stateid is set to 0 if client is not modifying the size 510 * and otherwise to whatever nfs4_get_stateid() returns. 511 * 512 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 513 * state struct could be found for the process/file pair. We may 514 * want to change this in the future (by OPENing the file). See 515 * bug # 4474852. 516 */ 517 if (vap->va_mask & AT_SIZE) { 518 519 ASSERT(rp != NULL); 520 mi = VTOMI4(RTOV4(rp)); 521 522 argop->nfs_argop4_u.opsetattr.stateid = 523 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 524 OP_SETATTR, sid_types, FALSE); 525 } else { 526 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 527 sizeof (stateid4)); 528 } 529 530 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 531 if (*error) 532 bzero(attr, sizeof (*attr)); 533 } 534 535 static void 536 nfs4args_setattr_free(nfs_argop4 *argop) 537 { 538 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 539 } 540 541 static int 542 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 543 bitmap4 supp) 544 { 545 fattr4 *attr; 546 int error = 0; 547 548 argop->argop = op; 549 switch (op) { 550 case OP_VERIFY: 551 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 552 break; 553 case OP_NVERIFY: 554 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 555 break; 556 default: 557 return (EINVAL); 558 } 559 if (!error) 560 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 561 if (error) 562 bzero(attr, sizeof (*attr)); 563 return (error); 564 } 565 566 static void 567 nfs4args_verify_free(nfs_argop4 *argop) 568 { 569 switch (argop->argop) { 570 case OP_VERIFY: 571 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 572 break; 573 case OP_NVERIFY: 574 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 575 break; 576 default: 577 break; 578 } 579 } 580 581 static void 582 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 583 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 584 { 585 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 586 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 587 588 argop->argop = OP_WRITE; 589 wargs->stable = stable; 590 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 591 mi, OP_WRITE, sid_tp); 592 wargs->mblk = NULL; 593 *wargs_pp = wargs; 594 } 595 596 void 597 nfs4args_copen_free(OPEN4cargs *open_args) 598 { 599 if (open_args->owner.owner_val) { 600 kmem_free(open_args->owner.owner_val, 601 open_args->owner.owner_len); 602 } 603 if ((open_args->opentype == OPEN4_CREATE) && 604 (open_args->mode != EXCLUSIVE4)) { 605 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 606 } 607 } 608 609 /* 610 * XXX: This is referenced in modstubs.s 611 */ 612 struct vnodeops * 613 nfs4_getvnodeops(void) 614 { 615 return (nfs4_vnodeops); 616 } 617 618 /* 619 * The OPEN operation opens a regular file. 620 */ 621 /*ARGSUSED3*/ 622 static int 623 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 624 { 625 vnode_t *dvp = NULL; 626 rnode4_t *rp, *drp; 627 int error; 628 int just_been_created; 629 char fn[MAXNAMELEN]; 630 631 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 632 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 633 return (EIO); 634 rp = VTOR4(*vpp); 635 636 /* 637 * Check to see if opening something besides a regular file; 638 * if so skip the OTW call 639 */ 640 if ((*vpp)->v_type != VREG) { 641 error = nfs4_open_non_reg_file(vpp, flag, cr); 642 return (error); 643 } 644 645 /* 646 * XXX - would like a check right here to know if the file is 647 * executable or not, so as to skip OTW 648 */ 649 650 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 651 return (error); 652 653 drp = VTOR4(dvp); 654 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 655 return (EINTR); 656 657 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) { 658 nfs_rw_exit(&drp->r_rwlock); 659 return (error); 660 } 661 662 /* 663 * See if this file has just been CREATEd. 664 * If so, clear the flag and update the dnlc, which was previously 665 * skipped in nfs4_create. 666 * XXX need better serilization on this. 667 * XXX move this into the nf4open_otw call, after we have 668 * XXX acquired the open owner seqid sync. 669 */ 670 mutex_enter(&rp->r_statev4_lock); 671 if (rp->created_v4) { 672 rp->created_v4 = 0; 673 mutex_exit(&rp->r_statev4_lock); 674 675 dnlc_update(dvp, fn, *vpp); 676 /* This is needed so we don't bump the open ref count */ 677 just_been_created = 1; 678 } else { 679 mutex_exit(&rp->r_statev4_lock); 680 just_been_created = 0; 681 } 682 683 /* 684 * If caller specified O_TRUNC/FTRUNC, then be sure to set 685 * FWRITE (to drive successful setattr(size=0) after open) 686 */ 687 if (flag & FTRUNC) 688 flag |= FWRITE; 689 690 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 691 just_been_created); 692 693 if (!error && !((*vpp)->v_flag & VROOT)) 694 dnlc_update(dvp, fn, *vpp); 695 696 nfs_rw_exit(&drp->r_rwlock); 697 698 /* release the hold from vtodv */ 699 VN_RELE(dvp); 700 701 /* exchange the shadow for the master vnode, if needed */ 702 703 if (error == 0 && IS_SHADOW(*vpp, rp)) 704 sv_exchange(vpp); 705 706 return (error); 707 } 708 709 /* 710 * See if there's a "lost open" request to be saved and recovered. 711 */ 712 static void 713 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 714 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 715 vnode_t *dvp, OPEN4cargs *open_args) 716 { 717 vfs_t *vfsp; 718 char *srccfp; 719 720 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 721 722 if (error != ETIMEDOUT && error != EINTR && 723 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 724 lost_rqstp->lr_op = 0; 725 return; 726 } 727 728 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 729 "nfs4open_save_lost_rqst: error %d", error)); 730 731 lost_rqstp->lr_op = OP_OPEN; 732 733 /* 734 * The vp (if it is not NULL) and dvp are held and rele'd via 735 * the recovery code. See nfs4_save_lost_rqst. 736 */ 737 lost_rqstp->lr_vp = vp; 738 lost_rqstp->lr_dvp = dvp; 739 lost_rqstp->lr_oop = oop; 740 lost_rqstp->lr_osp = NULL; 741 lost_rqstp->lr_lop = NULL; 742 lost_rqstp->lr_cr = cr; 743 lost_rqstp->lr_flk = NULL; 744 lost_rqstp->lr_oacc = open_args->share_access; 745 lost_rqstp->lr_odeny = open_args->share_deny; 746 lost_rqstp->lr_oclaim = open_args->claim; 747 if (open_args->claim == CLAIM_DELEGATE_CUR) { 748 lost_rqstp->lr_ostateid = 749 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 750 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 751 } else { 752 srccfp = open_args->open_claim4_u.cfile; 753 } 754 lost_rqstp->lr_ofile.utf8string_len = 0; 755 lost_rqstp->lr_ofile.utf8string_val = NULL; 756 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 757 lost_rqstp->lr_putfirst = FALSE; 758 } 759 760 struct nfs4_excl_time { 761 uint32 seconds; 762 uint32 nseconds; 763 }; 764 765 /* 766 * The OPEN operation creates and/or opens a regular file 767 * 768 * ARGSUSED 769 */ 770 static int 771 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 772 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 773 enum createmode4 createmode, int file_just_been_created) 774 { 775 rnode4_t *rp; 776 rnode4_t *drp = VTOR4(dvp); 777 vnode_t *vp = NULL; 778 vnode_t *vpi = *vpp; 779 bool_t needrecov = FALSE; 780 781 int doqueue = 1; 782 783 COMPOUND4args_clnt args; 784 COMPOUND4res_clnt res; 785 nfs_argop4 *argop; 786 nfs_resop4 *resop; 787 int argoplist_size; 788 int idx_open, idx_fattr; 789 790 GETFH4res *gf_res = NULL; 791 OPEN4res *op_res = NULL; 792 nfs4_ga_res_t *garp; 793 fattr4 *attr = NULL; 794 struct nfs4_excl_time verf; 795 bool_t did_excl_setup = FALSE; 796 int created_osp; 797 798 OPEN4cargs *open_args; 799 nfs4_open_owner_t *oop = NULL; 800 nfs4_open_stream_t *osp = NULL; 801 seqid4 seqid = 0; 802 bool_t retry_open = FALSE; 803 nfs4_recov_state_t recov_state; 804 nfs4_lost_rqst_t lost_rqst; 805 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 806 hrtime_t t; 807 int acc = 0; 808 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 809 cred_t *ncr = NULL; 810 811 nfs4_sharedfh_t *otw_sfh; 812 nfs4_sharedfh_t *orig_sfh; 813 int fh_differs = 0; 814 int numops, setgid_flag; 815 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 816 817 /* 818 * Make sure we properly deal with setting the right gid on 819 * a newly created file to reflect the parent's setgid bit 820 */ 821 setgid_flag = 0; 822 if (create_flag && in_va) { 823 824 /* 825 * If there is grpid mount flag used or 826 * the parent's directory has the setgid bit set 827 * _and_ the client was able to get a valid mapping 828 * for the parent dir's owner_group, we want to 829 * append NVERIFY(owner_group == dva.va_gid) and 830 * SETATTR to the CREATE compound. 831 */ 832 mutex_enter(&drp->r_statelock); 833 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID || 834 drp->r_attr.va_mode & VSGID) && 835 drp->r_attr.va_gid != GID_NOBODY) { 836 in_va->va_mask |= AT_GID; 837 in_va->va_gid = drp->r_attr.va_gid; 838 setgid_flag = 1; 839 } 840 mutex_exit(&drp->r_statelock); 841 } 842 843 /* 844 * Normal/non-create compound: 845 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 846 * 847 * Open(create) compound no setgid: 848 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 849 * RESTOREFH + GETATTR 850 * 851 * Open(create) setgid: 852 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 853 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 854 * NVERIFY(grp) + SETATTR 855 */ 856 if (setgid_flag) { 857 numops = 10; 858 idx_open = 1; 859 idx_fattr = 3; 860 } else if (create_flag) { 861 numops = 7; 862 idx_open = 2; 863 idx_fattr = 4; 864 } else { 865 numops = 4; 866 idx_open = 1; 867 idx_fattr = 3; 868 } 869 870 args.array_len = numops; 871 argoplist_size = numops * sizeof (nfs_argop4); 872 argop = kmem_alloc(argoplist_size, KM_SLEEP); 873 874 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 875 "open %s open flag 0x%x cred %p", file_name, open_flag, 876 (void *)cr)); 877 878 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 879 if (create_flag) { 880 /* 881 * We are to create a file. Initialize the passed in vnode 882 * pointer. 883 */ 884 vpi = NULL; 885 } else { 886 /* 887 * Check to see if the client owns a read delegation and is 888 * trying to open for write. If so, then return the delegation 889 * to avoid the server doing a cb_recall and returning DELAY. 890 * NB - we don't use the statev4_lock here because we'd have 891 * to drop the lock anyway and the result would be stale. 892 */ 893 if ((open_flag & FWRITE) && 894 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 895 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 896 897 /* 898 * If the file has a delegation, then do an access check up 899 * front. This avoids having to an access check later after 900 * we've already done start_op, which could deadlock. 901 */ 902 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 903 if (open_flag & FREAD && 904 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0) 905 acc |= VREAD; 906 if (open_flag & FWRITE && 907 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0) 908 acc |= VWRITE; 909 } 910 } 911 912 drp = VTOR4(dvp); 913 914 recov_state.rs_flags = 0; 915 recov_state.rs_num_retry_despite_err = 0; 916 cred_otw = cr; 917 918 recov_retry: 919 fh_differs = 0; 920 nfs4_error_zinit(&e); 921 922 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 923 if (e.error) { 924 if (ncr != NULL) 925 crfree(ncr); 926 kmem_free(argop, argoplist_size); 927 return (e.error); 928 } 929 930 args.ctag = TAG_OPEN; 931 args.array_len = numops; 932 args.array = argop; 933 934 /* putfh directory fh */ 935 argop[0].argop = OP_CPUTFH; 936 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 937 938 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 939 argop[idx_open].argop = OP_COPEN; 940 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 941 open_args->claim = CLAIM_NULL; 942 943 /* name of file */ 944 open_args->open_claim4_u.cfile = file_name; 945 open_args->owner.owner_len = 0; 946 open_args->owner.owner_val = NULL; 947 948 if (create_flag) { 949 /* CREATE a file */ 950 open_args->opentype = OPEN4_CREATE; 951 open_args->mode = createmode; 952 if (createmode == EXCLUSIVE4) { 953 if (did_excl_setup == FALSE) { 954 verf.seconds = zone_get_hostid(NULL); 955 if (verf.seconds != 0) 956 verf.nseconds = newnum(); 957 else { 958 timestruc_t now; 959 960 gethrestime(&now); 961 verf.seconds = now.tv_sec; 962 verf.nseconds = now.tv_nsec; 963 } 964 /* 965 * Since the server will use this value for the 966 * mtime, make sure that it can't overflow. Zero 967 * out the MSB. The actual value does not matter 968 * here, only its uniqeness. 969 */ 970 verf.seconds &= INT32_MAX; 971 did_excl_setup = TRUE; 972 } 973 974 /* Now copy over verifier to OPEN4args. */ 975 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 976 } else { 977 int v_error; 978 bitmap4 supp_attrs; 979 servinfo4_t *svp; 980 981 attr = &open_args->createhow4_u.createattrs; 982 983 svp = drp->r_server; 984 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 985 supp_attrs = svp->sv_supp_attrs; 986 nfs_rw_exit(&svp->sv_lock); 987 988 /* GUARDED4 or UNCHECKED4 */ 989 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 990 supp_attrs); 991 if (v_error) { 992 bzero(attr, sizeof (*attr)); 993 nfs4args_copen_free(open_args); 994 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 995 &recov_state, FALSE); 996 if (ncr != NULL) 997 crfree(ncr); 998 kmem_free(argop, argoplist_size); 999 return (v_error); 1000 } 1001 } 1002 } else { 1003 /* NO CREATE */ 1004 open_args->opentype = OPEN4_NOCREATE; 1005 } 1006 1007 if (recov_state.rs_sp != NULL) { 1008 mutex_enter(&recov_state.rs_sp->s_lock); 1009 open_args->owner.clientid = recov_state.rs_sp->clientid; 1010 mutex_exit(&recov_state.rs_sp->s_lock); 1011 } else { 1012 /* XXX should we just fail here? */ 1013 open_args->owner.clientid = 0; 1014 } 1015 1016 /* 1017 * This increments oop's ref count or creates a temporary 'just_created' 1018 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 1019 * completes. 1020 */ 1021 mutex_enter(&VTOMI4(dvp)->mi_lock); 1022 1023 /* See if a permanent or just created open owner exists */ 1024 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 1025 if (!oop) { 1026 /* 1027 * This open owner does not exist so create a temporary 1028 * just created one. 1029 */ 1030 oop = create_open_owner(cr, VTOMI4(dvp)); 1031 ASSERT(oop != NULL); 1032 } 1033 mutex_exit(&VTOMI4(dvp)->mi_lock); 1034 1035 /* this length never changes, do alloc before seqid sync */ 1036 open_args->owner.owner_len = sizeof (oop->oo_name); 1037 open_args->owner.owner_val = 1038 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1039 1040 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 1041 if (e.error == EAGAIN) { 1042 open_owner_rele(oop); 1043 nfs4args_copen_free(open_args); 1044 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1045 if (ncr != NULL) { 1046 crfree(ncr); 1047 ncr = NULL; 1048 } 1049 goto recov_retry; 1050 } 1051 1052 /* Check to see if we need to do the OTW call */ 1053 if (!create_flag) { 1054 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1055 file_just_been_created, &e.error, acc, &recov_state)) { 1056 1057 /* 1058 * The OTW open is not necessary. Either 1059 * the open can succeed without it (eg. 1060 * delegation, error == 0) or the open 1061 * must fail due to an access failure 1062 * (error != 0). In either case, tidy 1063 * up and return. 1064 */ 1065 1066 nfs4_end_open_seqid_sync(oop); 1067 open_owner_rele(oop); 1068 nfs4args_copen_free(open_args); 1069 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1070 if (ncr != NULL) 1071 crfree(ncr); 1072 kmem_free(argop, argoplist_size); 1073 return (e.error); 1074 } 1075 } 1076 1077 bcopy(&oop->oo_name, open_args->owner.owner_val, 1078 open_args->owner.owner_len); 1079 1080 seqid = nfs4_get_open_seqid(oop) + 1; 1081 open_args->seqid = seqid; 1082 open_args->share_access = 0; 1083 if (open_flag & FREAD) 1084 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1085 if (open_flag & FWRITE) 1086 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1087 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1088 1089 1090 1091 /* 1092 * getfh w/sanity check for idx_open/idx_fattr 1093 */ 1094 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1095 argop[idx_open + 1].argop = OP_GETFH; 1096 1097 /* getattr */ 1098 argop[idx_fattr].argop = OP_GETATTR; 1099 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1100 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1101 1102 if (setgid_flag) { 1103 vattr_t _v; 1104 servinfo4_t *svp; 1105 bitmap4 supp_attrs; 1106 1107 svp = drp->r_server; 1108 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1109 supp_attrs = svp->sv_supp_attrs; 1110 nfs_rw_exit(&svp->sv_lock); 1111 1112 /* 1113 * For setgid case, we need to: 1114 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1115 */ 1116 argop[4].argop = OP_SAVEFH; 1117 1118 argop[5].argop = OP_CPUTFH; 1119 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1120 1121 argop[6].argop = OP_GETATTR; 1122 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1123 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1124 1125 argop[7].argop = OP_RESTOREFH; 1126 1127 /* 1128 * nverify 1129 */ 1130 _v.va_mask = AT_GID; 1131 _v.va_gid = in_va->va_gid; 1132 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1133 supp_attrs))) { 1134 1135 /* 1136 * setattr 1137 * 1138 * We _know_ we're not messing with AT_SIZE or 1139 * AT_XTIME, so no need for stateid or flags. 1140 * Also we specify NULL rp since we're only 1141 * interested in setting owner_group attributes. 1142 */ 1143 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1144 supp_attrs, &e.error, 0); 1145 if (e.error) 1146 nfs4args_verify_free(&argop[8]); 1147 } 1148 1149 if (e.error) { 1150 /* 1151 * XXX - Revisit the last argument to nfs4_end_op() 1152 * once 5020486 is fixed. 1153 */ 1154 nfs4_end_open_seqid_sync(oop); 1155 open_owner_rele(oop); 1156 nfs4args_copen_free(open_args); 1157 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1158 if (ncr != NULL) 1159 crfree(ncr); 1160 kmem_free(argop, argoplist_size); 1161 return (e.error); 1162 } 1163 } else if (create_flag) { 1164 argop[1].argop = OP_SAVEFH; 1165 1166 argop[5].argop = OP_RESTOREFH; 1167 1168 argop[6].argop = OP_GETATTR; 1169 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1170 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1171 } 1172 1173 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1174 "nfs4open_otw: %s call, nm %s, rp %s", 1175 needrecov ? "recov" : "first", file_name, 1176 rnode4info(VTOR4(dvp)))); 1177 1178 t = gethrtime(); 1179 1180 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1181 1182 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1183 nfs4_set_open_seqid(seqid, oop, args.ctag); 1184 1185 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1186 1187 if (e.error || needrecov) { 1188 bool_t abort = FALSE; 1189 1190 if (needrecov) { 1191 nfs4_bseqid_entry_t *bsep = NULL; 1192 1193 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1194 cred_otw, vpi, dvp, open_args); 1195 1196 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1197 bsep = nfs4_create_bseqid_entry(oop, NULL, 1198 vpi, 0, args.ctag, open_args->seqid); 1199 num_bseqid_retry--; 1200 } 1201 1202 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1203 NULL, lost_rqst.lr_op == OP_OPEN ? 1204 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL); 1205 1206 if (bsep) 1207 kmem_free(bsep, sizeof (*bsep)); 1208 /* give up if we keep getting BAD_SEQID */ 1209 if (num_bseqid_retry == 0) 1210 abort = TRUE; 1211 if (abort == TRUE && e.error == 0) 1212 e.error = geterrno4(res.status); 1213 } 1214 nfs4_end_open_seqid_sync(oop); 1215 open_owner_rele(oop); 1216 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1217 nfs4args_copen_free(open_args); 1218 if (setgid_flag) { 1219 nfs4args_verify_free(&argop[8]); 1220 nfs4args_setattr_free(&argop[9]); 1221 } 1222 if (!e.error) 1223 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1224 if (ncr != NULL) { 1225 crfree(ncr); 1226 ncr = NULL; 1227 } 1228 if (!needrecov || abort == TRUE || e.error == EINTR || 1229 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1230 kmem_free(argop, argoplist_size); 1231 return (e.error); 1232 } 1233 goto recov_retry; 1234 } 1235 1236 /* 1237 * Will check and update lease after checking the rflag for 1238 * OPEN_CONFIRM in the successful OPEN call. 1239 */ 1240 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1241 1242 /* 1243 * XXX what if we're crossing mount points from server1:/drp 1244 * to server2:/drp/rp. 1245 */ 1246 1247 /* Signal our end of use of the open seqid */ 1248 nfs4_end_open_seqid_sync(oop); 1249 1250 /* 1251 * This will destroy the open owner if it was just created, 1252 * and no one else has put a reference on it. 1253 */ 1254 open_owner_rele(oop); 1255 if (create_flag && (createmode != EXCLUSIVE4) && 1256 res.status == NFS4ERR_BADOWNER) 1257 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1258 1259 e.error = geterrno4(res.status); 1260 nfs4args_copen_free(open_args); 1261 if (setgid_flag) { 1262 nfs4args_verify_free(&argop[8]); 1263 nfs4args_setattr_free(&argop[9]); 1264 } 1265 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1266 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1267 /* 1268 * If the reply is NFS4ERR_ACCESS, it may be because 1269 * we are root (no root net access). If the real uid 1270 * is not root, then retry with the real uid instead. 1271 */ 1272 if (ncr != NULL) { 1273 crfree(ncr); 1274 ncr = NULL; 1275 } 1276 if (res.status == NFS4ERR_ACCESS && 1277 (ncr = crnetadjust(cred_otw)) != NULL) { 1278 cred_otw = ncr; 1279 goto recov_retry; 1280 } 1281 kmem_free(argop, argoplist_size); 1282 return (e.error); 1283 } 1284 1285 resop = &res.array[idx_open]; /* open res */ 1286 op_res = &resop->nfs_resop4_u.opopen; 1287 1288 #ifdef DEBUG 1289 /* 1290 * verify attrset bitmap 1291 */ 1292 if (create_flag && 1293 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1294 /* make sure attrset returned is what we asked for */ 1295 /* XXX Ignore this 'error' for now */ 1296 if (attr->attrmask != op_res->attrset) 1297 /* EMPTY */; 1298 } 1299 #endif 1300 1301 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1302 mutex_enter(&VTOMI4(dvp)->mi_lock); 1303 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1304 mutex_exit(&VTOMI4(dvp)->mi_lock); 1305 } 1306 1307 resop = &res.array[idx_open + 1]; /* getfh res */ 1308 gf_res = &resop->nfs_resop4_u.opgetfh; 1309 1310 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1311 1312 /* 1313 * The open stateid has been updated on the server but not 1314 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1315 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1316 * WRITE call. That, however, will use the old stateid, so go ahead 1317 * and upate the open stateid now, before any call to makenfs4node. 1318 */ 1319 if (vpi) { 1320 nfs4_open_stream_t *tmp_osp; 1321 rnode4_t *tmp_rp = VTOR4(vpi); 1322 1323 tmp_osp = find_open_stream(oop, tmp_rp); 1324 if (tmp_osp) { 1325 tmp_osp->open_stateid = op_res->stateid; 1326 mutex_exit(&tmp_osp->os_sync_lock); 1327 open_stream_rele(tmp_osp, tmp_rp); 1328 } 1329 1330 /* 1331 * We must determine if the file handle given by the otw open 1332 * is the same as the file handle which was passed in with 1333 * *vpp. This case can be reached if the file we are trying 1334 * to open has been removed and another file has been created 1335 * having the same file name. The passed in vnode is released 1336 * later. 1337 */ 1338 orig_sfh = VTOR4(vpi)->r_fh; 1339 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1340 } 1341 1342 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1343 1344 if (create_flag || fh_differs) { 1345 int rnode_err = 0; 1346 1347 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1348 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh)); 1349 1350 if (e.error) 1351 PURGE_ATTRCACHE4(vp); 1352 /* 1353 * For the newly created vp case, make sure the rnode 1354 * isn't bad before using it. 1355 */ 1356 mutex_enter(&(VTOR4(vp))->r_statelock); 1357 if (VTOR4(vp)->r_flags & R4RECOVERR) 1358 rnode_err = EIO; 1359 mutex_exit(&(VTOR4(vp))->r_statelock); 1360 1361 if (rnode_err) { 1362 nfs4_end_open_seqid_sync(oop); 1363 nfs4args_copen_free(open_args); 1364 if (setgid_flag) { 1365 nfs4args_verify_free(&argop[8]); 1366 nfs4args_setattr_free(&argop[9]); 1367 } 1368 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1369 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1370 needrecov); 1371 open_owner_rele(oop); 1372 VN_RELE(vp); 1373 if (ncr != NULL) 1374 crfree(ncr); 1375 sfh4_rele(&otw_sfh); 1376 kmem_free(argop, argoplist_size); 1377 return (EIO); 1378 } 1379 } else { 1380 vp = vpi; 1381 } 1382 sfh4_rele(&otw_sfh); 1383 1384 /* 1385 * It seems odd to get a full set of attrs and then not update 1386 * the object's attrcache in the non-create case. Create case uses 1387 * the attrs since makenfs4node checks to see if the attrs need to 1388 * be updated (and then updates them). The non-create case should 1389 * update attrs also. 1390 */ 1391 if (! create_flag && ! fh_differs && !e.error) { 1392 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1393 } 1394 1395 nfs4_error_zinit(&e); 1396 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1397 /* This does not do recovery for vp explicitly. */ 1398 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1399 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1400 1401 if (e.error || e.stat) { 1402 nfs4_end_open_seqid_sync(oop); 1403 nfs4args_copen_free(open_args); 1404 if (setgid_flag) { 1405 nfs4args_verify_free(&argop[8]); 1406 nfs4args_setattr_free(&argop[9]); 1407 } 1408 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1409 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1410 needrecov); 1411 open_owner_rele(oop); 1412 if (create_flag || fh_differs) { 1413 /* rele the makenfs4node */ 1414 VN_RELE(vp); 1415 } 1416 if (ncr != NULL) { 1417 crfree(ncr); 1418 ncr = NULL; 1419 } 1420 if (retry_open == TRUE) { 1421 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1422 "nfs4open_otw: retry the open since OPEN " 1423 "CONFIRM failed with error %d stat %d", 1424 e.error, e.stat)); 1425 if (create_flag && createmode == GUARDED4) { 1426 NFS4_DEBUG(nfs4_client_recov_debug, 1427 (CE_NOTE, "nfs4open_otw: switch " 1428 "createmode from GUARDED4 to " 1429 "UNCHECKED4")); 1430 createmode = UNCHECKED4; 1431 } 1432 goto recov_retry; 1433 } 1434 if (!e.error) { 1435 if (create_flag && (createmode != EXCLUSIVE4) && 1436 e.stat == NFS4ERR_BADOWNER) 1437 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1438 1439 e.error = geterrno4(e.stat); 1440 } 1441 kmem_free(argop, argoplist_size); 1442 return (e.error); 1443 } 1444 } 1445 1446 rp = VTOR4(vp); 1447 1448 mutex_enter(&rp->r_statev4_lock); 1449 if (create_flag) 1450 rp->created_v4 = 1; 1451 mutex_exit(&rp->r_statev4_lock); 1452 1453 mutex_enter(&oop->oo_lock); 1454 /* Doesn't matter if 'oo_just_created' already was set as this */ 1455 oop->oo_just_created = NFS4_PERM_CREATED; 1456 if (oop->oo_cred_otw) 1457 crfree(oop->oo_cred_otw); 1458 oop->oo_cred_otw = cred_otw; 1459 crhold(oop->oo_cred_otw); 1460 mutex_exit(&oop->oo_lock); 1461 1462 /* returns with 'os_sync_lock' held */ 1463 osp = find_or_create_open_stream(oop, rp, &created_osp); 1464 if (!osp) { 1465 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1466 "nfs4open_otw: failed to create an open stream")); 1467 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1468 "signal our end of use of the open seqid")); 1469 1470 nfs4_end_open_seqid_sync(oop); 1471 open_owner_rele(oop); 1472 nfs4args_copen_free(open_args); 1473 if (setgid_flag) { 1474 nfs4args_verify_free(&argop[8]); 1475 nfs4args_setattr_free(&argop[9]); 1476 } 1477 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1478 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1479 if (create_flag || fh_differs) 1480 VN_RELE(vp); 1481 if (ncr != NULL) 1482 crfree(ncr); 1483 1484 kmem_free(argop, argoplist_size); 1485 return (EINVAL); 1486 1487 } 1488 1489 osp->open_stateid = op_res->stateid; 1490 1491 if (open_flag & FREAD) 1492 osp->os_share_acc_read++; 1493 if (open_flag & FWRITE) 1494 osp->os_share_acc_write++; 1495 osp->os_share_deny_none++; 1496 1497 /* 1498 * Need to reset this bitfield for the possible case where we were 1499 * going to OTW CLOSE the file, got a non-recoverable error, and before 1500 * we could retry the CLOSE, OPENed the file again. 1501 */ 1502 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1503 osp->os_final_close = 0; 1504 osp->os_force_close = 0; 1505 #ifdef DEBUG 1506 if (osp->os_failed_reopen) 1507 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1508 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1509 (void *)osp, (void *)cr, rnode4info(rp))); 1510 #endif 1511 osp->os_failed_reopen = 0; 1512 1513 mutex_exit(&osp->os_sync_lock); 1514 1515 nfs4_end_open_seqid_sync(oop); 1516 1517 if (created_osp && recov_state.rs_sp != NULL) { 1518 mutex_enter(&recov_state.rs_sp->s_lock); 1519 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1520 mutex_exit(&recov_state.rs_sp->s_lock); 1521 } 1522 1523 /* get rid of our reference to find oop */ 1524 open_owner_rele(oop); 1525 1526 open_stream_rele(osp, rp); 1527 1528 /* accept delegation, if any */ 1529 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1530 1531 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1532 1533 if (createmode == EXCLUSIVE4 && 1534 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1535 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1536 " EXCLUSIVE4: sending a SETATTR")); 1537 /* 1538 * If doing an exclusive create, then generate 1539 * a SETATTR to set the initial attributes. 1540 * Try to set the mtime and the atime to the 1541 * server's current time. It is somewhat 1542 * expected that these fields will be used to 1543 * store the exclusive create cookie. If not, 1544 * server implementors will need to know that 1545 * a SETATTR will follow an exclusive create 1546 * and the cookie should be destroyed if 1547 * appropriate. 1548 * 1549 * The AT_GID and AT_SIZE bits are turned off 1550 * so that the SETATTR request will not attempt 1551 * to process these. The gid will be set 1552 * separately if appropriate. The size is turned 1553 * off because it is assumed that a new file will 1554 * be created empty and if the file wasn't empty, 1555 * then the exclusive create will have failed 1556 * because the file must have existed already. 1557 * Therefore, no truncate operation is needed. 1558 */ 1559 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1560 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1561 1562 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1563 if (e.error) { 1564 /* 1565 * Couldn't correct the attributes of 1566 * the newly created file and the 1567 * attributes are wrong. Remove the 1568 * file and return an error to the 1569 * application. 1570 */ 1571 /* XXX will this take care of client state ? */ 1572 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1573 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1574 " remove file", e.error)); 1575 VN_RELE(vp); 1576 (void) nfs4_remove(dvp, file_name, cr, NULL, 0); 1577 /* 1578 * Since we've reled the vnode and removed 1579 * the file we now need to return the error. 1580 * At this point we don't want to update the 1581 * dircaches, call nfs4_waitfor_purge_complete 1582 * or set vpp to vp so we need to skip these 1583 * as well. 1584 */ 1585 goto skip_update_dircaches; 1586 } 1587 } 1588 1589 /* 1590 * If we created or found the correct vnode, due to create_flag or 1591 * fh_differs being set, then update directory cache attribute, readdir 1592 * and dnlc caches. 1593 */ 1594 if (create_flag || fh_differs) { 1595 dirattr_info_t dinfo, *dinfop; 1596 1597 /* 1598 * Make sure getattr succeeded before using results. 1599 * note: op 7 is getattr(dir) for both flavors of 1600 * open(create). 1601 */ 1602 if (create_flag && res.status == NFS4_OK) { 1603 dinfo.di_time_call = t; 1604 dinfo.di_cred = cr; 1605 dinfo.di_garp = 1606 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1607 dinfop = &dinfo; 1608 } else { 1609 dinfop = NULL; 1610 } 1611 1612 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1613 dinfop); 1614 } 1615 1616 /* 1617 * If the page cache for this file was flushed from actions 1618 * above, it was done asynchronously and if that is true, 1619 * there is a need to wait here for it to complete. This must 1620 * be done outside of start_fop/end_fop. 1621 */ 1622 (void) nfs4_waitfor_purge_complete(vp); 1623 1624 /* 1625 * It is implicit that we are in the open case (create_flag == 0) since 1626 * fh_differs can only be set to a non-zero value in the open case. 1627 */ 1628 if (fh_differs != 0 && vpi != NULL) 1629 VN_RELE(vpi); 1630 1631 /* 1632 * Be sure to set *vpp to the correct value before returning. 1633 */ 1634 *vpp = vp; 1635 1636 skip_update_dircaches: 1637 1638 nfs4args_copen_free(open_args); 1639 if (setgid_flag) { 1640 nfs4args_verify_free(&argop[8]); 1641 nfs4args_setattr_free(&argop[9]); 1642 } 1643 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1644 1645 if (ncr) 1646 crfree(ncr); 1647 kmem_free(argop, argoplist_size); 1648 return (e.error); 1649 } 1650 1651 /* 1652 * Reopen an open instance. cf. nfs4open_otw(). 1653 * 1654 * Errors are returned by the nfs4_error_t parameter. 1655 * - ep->error contains an errno value or zero. 1656 * - if it is zero, ep->stat is set to an NFS status code, if any. 1657 * If the file could not be reopened, but the caller should continue, the 1658 * file is marked dead and no error values are returned. If the caller 1659 * should stop recovering open files and start over, either the ep->error 1660 * value or ep->stat will indicate an error (either something that requires 1661 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1662 * filehandles) may be handled silently by this routine. 1663 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1664 * will be started, so the caller should not do it. 1665 * 1666 * Gotos: 1667 * - kill_file : reopen failed in such a fashion to constitute marking the 1668 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1669 * is for cases where recovery is not possible. 1670 * - failed_reopen : same as above, except that the file has already been 1671 * marked dead, so no need to do it again. 1672 * - bailout : reopen failed but we are able to recover and retry the reopen - 1673 * either within this function immediately or via the calling function. 1674 */ 1675 1676 void 1677 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1678 open_claim_type4 claim, bool_t frc_use_claim_previous, 1679 bool_t is_recov) 1680 { 1681 COMPOUND4args_clnt args; 1682 COMPOUND4res_clnt res; 1683 nfs_argop4 argop[4]; 1684 nfs_resop4 *resop; 1685 OPEN4res *op_res = NULL; 1686 OPEN4cargs *open_args; 1687 GETFH4res *gf_res; 1688 rnode4_t *rp = VTOR4(vp); 1689 int doqueue = 1; 1690 cred_t *cr = NULL, *cred_otw = NULL; 1691 nfs4_open_owner_t *oop = NULL; 1692 seqid4 seqid; 1693 nfs4_ga_res_t *garp; 1694 char fn[MAXNAMELEN]; 1695 nfs4_recov_state_t recov = {NULL, 0}; 1696 nfs4_lost_rqst_t lost_rqst; 1697 mntinfo4_t *mi = VTOMI4(vp); 1698 bool_t abort; 1699 char *failed_msg = ""; 1700 int fh_different; 1701 hrtime_t t; 1702 nfs4_bseqid_entry_t *bsep = NULL; 1703 1704 ASSERT(nfs4_consistent_type(vp)); 1705 ASSERT(nfs_zone() == mi->mi_zone); 1706 1707 nfs4_error_zinit(ep); 1708 1709 /* this is the cred used to find the open owner */ 1710 cr = state_to_cred(osp); 1711 if (cr == NULL) { 1712 failed_msg = "Couldn't reopen: no cred"; 1713 goto kill_file; 1714 } 1715 /* use this cred for OTW operations */ 1716 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1717 1718 top: 1719 nfs4_error_zinit(ep); 1720 1721 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1722 /* File system has been unmounted, quit */ 1723 ep->error = EIO; 1724 failed_msg = "Couldn't reopen: file system has been unmounted"; 1725 goto kill_file; 1726 } 1727 1728 oop = osp->os_open_owner; 1729 1730 ASSERT(oop != NULL); 1731 if (oop == NULL) { /* be defensive in non-DEBUG */ 1732 failed_msg = "can't reopen: no open owner"; 1733 goto kill_file; 1734 } 1735 open_owner_hold(oop); 1736 1737 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1738 if (ep->error) { 1739 open_owner_rele(oop); 1740 oop = NULL; 1741 goto bailout; 1742 } 1743 1744 /* 1745 * If the rnode has a delegation and the delegation has been 1746 * recovered and the server didn't request a recall and the caller 1747 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1748 * recovery) and the rnode hasn't been marked dead, then install 1749 * the delegation stateid in the open stream. Otherwise, proceed 1750 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1751 */ 1752 mutex_enter(&rp->r_statev4_lock); 1753 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1754 !rp->r_deleg_return_pending && 1755 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1756 !rp->r_deleg_needs_recall && 1757 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1758 !(rp->r_flags & R4RECOVERR)) { 1759 mutex_enter(&osp->os_sync_lock); 1760 osp->os_delegation = 1; 1761 osp->open_stateid = rp->r_deleg_stateid; 1762 mutex_exit(&osp->os_sync_lock); 1763 mutex_exit(&rp->r_statev4_lock); 1764 goto bailout; 1765 } 1766 mutex_exit(&rp->r_statev4_lock); 1767 1768 /* 1769 * If the file failed recovery, just quit. This failure need not 1770 * affect other reopens, so don't return an error. 1771 */ 1772 mutex_enter(&rp->r_statelock); 1773 if (rp->r_flags & R4RECOVERR) { 1774 mutex_exit(&rp->r_statelock); 1775 ep->error = 0; 1776 goto failed_reopen; 1777 } 1778 mutex_exit(&rp->r_statelock); 1779 1780 /* 1781 * argop is empty here 1782 * 1783 * PUTFH, OPEN, GETATTR 1784 */ 1785 args.ctag = TAG_REOPEN; 1786 args.array_len = 4; 1787 args.array = argop; 1788 1789 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1790 "nfs4_reopen: file is type %d, id %s", 1791 vp->v_type, rnode4info(VTOR4(vp)))); 1792 1793 argop[0].argop = OP_CPUTFH; 1794 1795 if (claim != CLAIM_PREVIOUS) { 1796 /* 1797 * if this is a file mount then 1798 * use the mntinfo parentfh 1799 */ 1800 argop[0].nfs_argop4_u.opcputfh.sfh = 1801 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1802 VTOSV(vp)->sv_dfh; 1803 } else { 1804 /* putfh fh to reopen */ 1805 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1806 } 1807 1808 argop[1].argop = OP_COPEN; 1809 open_args = &argop[1].nfs_argop4_u.opcopen; 1810 open_args->claim = claim; 1811 1812 if (claim == CLAIM_NULL) { 1813 1814 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1815 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1816 "failed for vp 0x%p for CLAIM_NULL with %m", 1817 (void *)vp); 1818 failed_msg = "Couldn't reopen: vtoname failed for " 1819 "CLAIM_NULL"; 1820 /* nothing allocated yet */ 1821 goto kill_file; 1822 } 1823 1824 open_args->open_claim4_u.cfile = fn; 1825 } else if (claim == CLAIM_PREVIOUS) { 1826 1827 /* 1828 * We have two cases to deal with here: 1829 * 1) We're being called to reopen files in order to satisfy 1830 * a lock operation request which requires us to explicitly 1831 * reopen files which were opened under a delegation. If 1832 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1833 * that case, frc_use_claim_previous is TRUE and we must 1834 * use the rnode's current delegation type (r_deleg_type). 1835 * 2) We're reopening files during some form of recovery. 1836 * In this case, frc_use_claim_previous is FALSE and we 1837 * use the delegation type appropriate for recovery 1838 * (r_deleg_needs_recovery). 1839 */ 1840 mutex_enter(&rp->r_statev4_lock); 1841 open_args->open_claim4_u.delegate_type = 1842 frc_use_claim_previous ? 1843 rp->r_deleg_type : 1844 rp->r_deleg_needs_recovery; 1845 mutex_exit(&rp->r_statev4_lock); 1846 1847 } else if (claim == CLAIM_DELEGATE_CUR) { 1848 1849 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1850 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1851 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1852 "with %m", (void *)vp); 1853 failed_msg = "Couldn't reopen: vtoname failed for " 1854 "CLAIM_DELEGATE_CUR"; 1855 /* nothing allocated yet */ 1856 goto kill_file; 1857 } 1858 1859 mutex_enter(&rp->r_statev4_lock); 1860 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1861 rp->r_deleg_stateid; 1862 mutex_exit(&rp->r_statev4_lock); 1863 1864 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1865 } 1866 open_args->opentype = OPEN4_NOCREATE; 1867 open_args->owner.clientid = mi2clientid(mi); 1868 open_args->owner.owner_len = sizeof (oop->oo_name); 1869 open_args->owner.owner_val = 1870 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1871 bcopy(&oop->oo_name, open_args->owner.owner_val, 1872 open_args->owner.owner_len); 1873 open_args->share_access = 0; 1874 open_args->share_deny = 0; 1875 1876 mutex_enter(&osp->os_sync_lock); 1877 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1878 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1879 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1880 (void *)osp, (void *)rp, osp->os_share_acc_read, 1881 osp->os_share_acc_write, osp->os_open_ref_count, 1882 osp->os_mmap_read, osp->os_mmap_write, claim)); 1883 1884 if (osp->os_share_acc_read || osp->os_mmap_read) 1885 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1886 if (osp->os_share_acc_write || osp->os_mmap_write) 1887 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1888 if (osp->os_share_deny_read) 1889 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1890 if (osp->os_share_deny_write) 1891 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1892 mutex_exit(&osp->os_sync_lock); 1893 1894 seqid = nfs4_get_open_seqid(oop) + 1; 1895 open_args->seqid = seqid; 1896 1897 /* Construct the getfh part of the compound */ 1898 argop[2].argop = OP_GETFH; 1899 1900 /* Construct the getattr part of the compound */ 1901 argop[3].argop = OP_GETATTR; 1902 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1903 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1904 1905 t = gethrtime(); 1906 1907 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1908 1909 if (ep->error) { 1910 if (!is_recov && !frc_use_claim_previous && 1911 (ep->error == EINTR || ep->error == ETIMEDOUT || 1912 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1913 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1914 cred_otw, vp, NULL, open_args); 1915 abort = nfs4_start_recovery(ep, 1916 VTOMI4(vp), vp, NULL, NULL, 1917 lost_rqst.lr_op == OP_OPEN ? 1918 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL); 1919 nfs4args_copen_free(open_args); 1920 goto bailout; 1921 } 1922 1923 nfs4args_copen_free(open_args); 1924 1925 if (ep->error == EACCES && cred_otw != cr) { 1926 crfree(cred_otw); 1927 cred_otw = cr; 1928 crhold(cred_otw); 1929 nfs4_end_open_seqid_sync(oop); 1930 open_owner_rele(oop); 1931 oop = NULL; 1932 goto top; 1933 } 1934 if (ep->error == ETIMEDOUT) 1935 goto bailout; 1936 failed_msg = "Couldn't reopen: rpc error"; 1937 goto kill_file; 1938 } 1939 1940 if (nfs4_need_to_bump_seqid(&res)) 1941 nfs4_set_open_seqid(seqid, oop, args.ctag); 1942 1943 switch (res.status) { 1944 case NFS4_OK: 1945 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1946 mutex_enter(&rp->r_statelock); 1947 rp->r_delay_interval = 0; 1948 mutex_exit(&rp->r_statelock); 1949 } 1950 break; 1951 case NFS4ERR_BAD_SEQID: 1952 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1953 args.ctag, open_args->seqid); 1954 1955 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1956 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1957 NULL, OP_OPEN, bsep, NULL, NULL); 1958 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 kmem_free(bsep, sizeof (*bsep)); 1965 1966 goto kill_file; 1967 case NFS4ERR_NO_GRACE: 1968 nfs4args_copen_free(open_args); 1969 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1970 nfs4_end_open_seqid_sync(oop); 1971 open_owner_rele(oop); 1972 oop = NULL; 1973 if (claim == CLAIM_PREVIOUS) { 1974 /* 1975 * Retry as a plain open. We don't need to worry about 1976 * checking the changeinfo: it is acceptable for a 1977 * client to re-open a file and continue processing 1978 * (in the absence of locks). 1979 */ 1980 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1981 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1982 "will retry as CLAIM_NULL")); 1983 claim = CLAIM_NULL; 1984 nfs4_mi_kstat_inc_no_grace(mi); 1985 goto top; 1986 } 1987 failed_msg = 1988 "Couldn't reopen: tried reclaim outside grace period. "; 1989 goto kill_file; 1990 case NFS4ERR_GRACE: 1991 nfs4_set_grace_wait(mi); 1992 nfs4args_copen_free(open_args); 1993 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1994 nfs4_end_open_seqid_sync(oop); 1995 open_owner_rele(oop); 1996 oop = NULL; 1997 ep->error = nfs4_wait_for_grace(mi, &recov); 1998 if (ep->error != 0) 1999 goto bailout; 2000 goto top; 2001 case NFS4ERR_DELAY: 2002 nfs4_set_delay_wait(vp); 2003 nfs4args_copen_free(open_args); 2004 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2005 nfs4_end_open_seqid_sync(oop); 2006 open_owner_rele(oop); 2007 oop = NULL; 2008 ep->error = nfs4_wait_for_delay(vp, &recov); 2009 nfs4_mi_kstat_inc_delay(mi); 2010 if (ep->error != 0) 2011 goto bailout; 2012 goto top; 2013 case NFS4ERR_FHEXPIRED: 2014 /* recover filehandle and retry */ 2015 abort = nfs4_start_recovery(ep, 2016 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL); 2017 nfs4args_copen_free(open_args); 2018 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2019 nfs4_end_open_seqid_sync(oop); 2020 open_owner_rele(oop); 2021 oop = NULL; 2022 if (abort == FALSE) 2023 goto top; 2024 failed_msg = "Couldn't reopen: recovery aborted"; 2025 goto kill_file; 2026 case NFS4ERR_RESOURCE: 2027 case NFS4ERR_STALE_CLIENTID: 2028 case NFS4ERR_WRONGSEC: 2029 case NFS4ERR_EXPIRED: 2030 /* 2031 * Do not mark the file dead and let the calling 2032 * function initiate recovery. 2033 */ 2034 nfs4args_copen_free(open_args); 2035 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2036 nfs4_end_open_seqid_sync(oop); 2037 open_owner_rele(oop); 2038 oop = NULL; 2039 goto bailout; 2040 case NFS4ERR_ACCESS: 2041 if (cred_otw != cr) { 2042 crfree(cred_otw); 2043 cred_otw = cr; 2044 crhold(cred_otw); 2045 nfs4args_copen_free(open_args); 2046 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2047 nfs4_end_open_seqid_sync(oop); 2048 open_owner_rele(oop); 2049 oop = NULL; 2050 goto top; 2051 } 2052 /* fall through */ 2053 default: 2054 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2055 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2056 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2057 rnode4info(VTOR4(vp)))); 2058 failed_msg = "Couldn't reopen: NFSv4 error"; 2059 nfs4args_copen_free(open_args); 2060 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2061 goto kill_file; 2062 } 2063 2064 resop = &res.array[1]; /* open res */ 2065 op_res = &resop->nfs_resop4_u.opopen; 2066 2067 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2068 2069 /* 2070 * Check if the path we reopened really is the same 2071 * file. We could end up in a situation where the file 2072 * was removed and a new file created with the same name. 2073 */ 2074 resop = &res.array[2]; 2075 gf_res = &resop->nfs_resop4_u.opgetfh; 2076 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2077 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2078 if (fh_different) { 2079 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2080 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2081 /* Oops, we don't have the same file */ 2082 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2083 failed_msg = "Couldn't reopen: Persistent " 2084 "file handle changed"; 2085 else 2086 failed_msg = "Couldn't reopen: Volatile " 2087 "(no expire on open) file handle changed"; 2088 2089 nfs4args_copen_free(open_args); 2090 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2091 nfs_rw_exit(&mi->mi_fh_lock); 2092 goto kill_file; 2093 2094 } else { 2095 /* 2096 * We have volatile file handles that don't compare. 2097 * If the fids are the same then we assume that the 2098 * file handle expired but the rnode still refers to 2099 * the same file object. 2100 * 2101 * First check that we have fids or not. 2102 * If we don't we have a dumb server so we will 2103 * just assume every thing is ok for now. 2104 */ 2105 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2106 rp->r_attr.va_mask & AT_NODEID && 2107 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2108 /* 2109 * We have fids, but they don't 2110 * compare. So kill the file. 2111 */ 2112 failed_msg = 2113 "Couldn't reopen: file handle changed" 2114 " due to mismatched fids"; 2115 nfs4args_copen_free(open_args); 2116 (void) xdr_free(xdr_COMPOUND4res_clnt, 2117 (caddr_t)&res); 2118 nfs_rw_exit(&mi->mi_fh_lock); 2119 goto kill_file; 2120 } else { 2121 /* 2122 * We have volatile file handles that refers 2123 * to the same file (at least they have the 2124 * same fid) or we don't have fids so we 2125 * can't tell. :(. We'll be a kind and accepting 2126 * client so we'll update the rnode's file 2127 * handle with the otw handle. 2128 * 2129 * We need to drop mi->mi_fh_lock since 2130 * sh4_update acquires it. Since there is 2131 * only one recovery thread there is no 2132 * race. 2133 */ 2134 nfs_rw_exit(&mi->mi_fh_lock); 2135 sfh4_update(rp->r_fh, &gf_res->object); 2136 } 2137 } 2138 } else { 2139 nfs_rw_exit(&mi->mi_fh_lock); 2140 } 2141 2142 ASSERT(nfs4_consistent_type(vp)); 2143 2144 /* 2145 * If the server wanted an OPEN_CONFIRM but that fails, just start 2146 * over. Presumably if there is a persistent error it will show up 2147 * when we resend the OPEN. 2148 */ 2149 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2150 bool_t retry_open = FALSE; 2151 2152 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2153 cred_otw, is_recov, &retry_open, 2154 oop, FALSE, ep, NULL); 2155 if (ep->error || ep->stat) { 2156 nfs4args_copen_free(open_args); 2157 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2158 nfs4_end_open_seqid_sync(oop); 2159 open_owner_rele(oop); 2160 oop = NULL; 2161 goto top; 2162 } 2163 } 2164 2165 mutex_enter(&osp->os_sync_lock); 2166 osp->open_stateid = op_res->stateid; 2167 osp->os_delegation = 0; 2168 /* 2169 * Need to reset this bitfield for the possible case where we were 2170 * going to OTW CLOSE the file, got a non-recoverable error, and before 2171 * we could retry the CLOSE, OPENed the file again. 2172 */ 2173 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2174 osp->os_final_close = 0; 2175 osp->os_force_close = 0; 2176 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2177 osp->os_dc_openacc = open_args->share_access; 2178 mutex_exit(&osp->os_sync_lock); 2179 2180 nfs4_end_open_seqid_sync(oop); 2181 2182 /* accept delegation, if any */ 2183 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2184 2185 nfs4args_copen_free(open_args); 2186 2187 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2188 2189 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2190 2191 ASSERT(nfs4_consistent_type(vp)); 2192 2193 open_owner_rele(oop); 2194 crfree(cr); 2195 crfree(cred_otw); 2196 return; 2197 2198 kill_file: 2199 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2200 failed_reopen: 2201 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2202 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2203 (void *)osp, (void *)cr, rnode4info(rp))); 2204 mutex_enter(&osp->os_sync_lock); 2205 osp->os_failed_reopen = 1; 2206 mutex_exit(&osp->os_sync_lock); 2207 bailout: 2208 if (oop != NULL) { 2209 nfs4_end_open_seqid_sync(oop); 2210 open_owner_rele(oop); 2211 } 2212 if (cr != NULL) 2213 crfree(cr); 2214 if (cred_otw != NULL) 2215 crfree(cred_otw); 2216 } 2217 2218 /* for . and .. OPENs */ 2219 /* ARGSUSED */ 2220 static int 2221 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2222 { 2223 rnode4_t *rp; 2224 nfs4_ga_res_t gar; 2225 2226 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2227 2228 /* 2229 * If close-to-open consistency checking is turned off or 2230 * if there is no cached data, we can avoid 2231 * the over the wire getattr. Otherwise, force a 2232 * call to the server to get fresh attributes and to 2233 * check caches. This is required for close-to-open 2234 * consistency. 2235 */ 2236 rp = VTOR4(*vpp); 2237 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2238 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2239 return (0); 2240 2241 gar.n4g_va.va_mask = AT_ALL; 2242 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2243 } 2244 2245 /* 2246 * CLOSE a file 2247 */ 2248 /* ARGSUSED */ 2249 static int 2250 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2251 caller_context_t *ct) 2252 { 2253 rnode4_t *rp; 2254 int error = 0; 2255 int r_error = 0; 2256 int n4error = 0; 2257 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2258 2259 /* 2260 * Remove client state for this (lockowner, file) pair. 2261 * Issue otw v4 call to have the server do the same. 2262 */ 2263 2264 rp = VTOR4(vp); 2265 2266 /* 2267 * zone_enter(2) prevents processes from changing zones with NFS files 2268 * open; if we happen to get here from the wrong zone we can't do 2269 * anything over the wire. 2270 */ 2271 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2272 /* 2273 * We could attempt to clean up locks, except we're sure 2274 * that the current process didn't acquire any locks on 2275 * the file: any attempt to lock a file belong to another zone 2276 * will fail, and one can't lock an NFS file and then change 2277 * zones, as that fails too. 2278 * 2279 * Returning an error here is the sane thing to do. A 2280 * subsequent call to VN_RELE() which translates to a 2281 * nfs4_inactive() will clean up state: if the zone of the 2282 * vnode's origin is still alive and kicking, the inactive 2283 * thread will handle the request (from the correct zone), and 2284 * everything (minus the OTW close call) should be OK. If the 2285 * zone is going away nfs4_async_inactive() will throw away 2286 * delegations, open streams and cached pages inline. 2287 */ 2288 return (EIO); 2289 } 2290 2291 /* 2292 * If we are using local locking for this filesystem, then 2293 * release all of the SYSV style record locks. Otherwise, 2294 * we are doing network locking and we need to release all 2295 * of the network locks. All of the locks held by this 2296 * process on this file are released no matter what the 2297 * incoming reference count is. 2298 */ 2299 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2300 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2301 cleanshares(vp, ttoproc(curthread)->p_pid); 2302 } else 2303 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2304 2305 if (e.error) { 2306 struct lm_sysid *lmsid; 2307 lmsid = nfs4_find_sysid(VTOMI4(vp)); 2308 if (lmsid == NULL) { 2309 DTRACE_PROBE2(unknown__sysid, int, e.error, 2310 vnode_t *, vp); 2311 } else { 2312 cleanlocks(vp, ttoproc(curthread)->p_pid, 2313 (lm_sysidt(lmsid) | LM_SYSID_CLIENT)); 2314 } 2315 return (e.error); 2316 } 2317 2318 if (count > 1) 2319 return (0); 2320 2321 /* 2322 * If the file has been `unlinked', then purge the 2323 * DNLC so that this vnode will get reycled quicker 2324 * and the .nfs* file on the server will get removed. 2325 */ 2326 if (rp->r_unldvp != NULL) 2327 dnlc_purge_vp(vp); 2328 2329 /* 2330 * If the file was open for write and there are pages, 2331 * do a synchronous flush and commit of all of the 2332 * dirty and uncommitted pages. 2333 */ 2334 ASSERT(!e.error); 2335 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2336 error = nfs4_putpage_commit(vp, 0, 0, cr); 2337 2338 mutex_enter(&rp->r_statelock); 2339 r_error = rp->r_error; 2340 rp->r_error = 0; 2341 mutex_exit(&rp->r_statelock); 2342 2343 /* 2344 * If this file type is one for which no explicit 'open' was 2345 * done, then bail now (ie. no need for protocol 'close'). If 2346 * there was an error w/the vm subsystem, return _that_ error, 2347 * otherwise, return any errors that may've been reported via 2348 * the rnode. 2349 */ 2350 if (vp->v_type != VREG) 2351 return (error ? error : r_error); 2352 2353 /* 2354 * The sync putpage commit may have failed above, but since 2355 * we're working w/a regular file, we need to do the protocol 2356 * 'close' (nfs4close_one will figure out if an otw close is 2357 * needed or not). Report any errors _after_ doing the protocol 2358 * 'close'. 2359 */ 2360 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2361 n4error = e.error ? e.error : geterrno4(e.stat); 2362 2363 /* 2364 * Error reporting prio (Hi -> Lo) 2365 * 2366 * i) nfs4_putpage_commit (error) 2367 * ii) rnode's (r_error) 2368 * iii) nfs4close_one (n4error) 2369 */ 2370 return (error ? error : (r_error ? r_error : n4error)); 2371 } 2372 2373 /* 2374 * Initialize *lost_rqstp. 2375 */ 2376 2377 static void 2378 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2379 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2380 vnode_t *vp) 2381 { 2382 if (error != ETIMEDOUT && error != EINTR && 2383 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2384 lost_rqstp->lr_op = 0; 2385 return; 2386 } 2387 2388 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2389 "nfs4close_save_lost_rqst: error %d", error)); 2390 2391 lost_rqstp->lr_op = OP_CLOSE; 2392 /* 2393 * The vp is held and rele'd via the recovery code. 2394 * See nfs4_save_lost_rqst. 2395 */ 2396 lost_rqstp->lr_vp = vp; 2397 lost_rqstp->lr_dvp = NULL; 2398 lost_rqstp->lr_oop = oop; 2399 lost_rqstp->lr_osp = osp; 2400 ASSERT(osp != NULL); 2401 ASSERT(mutex_owned(&osp->os_sync_lock)); 2402 osp->os_pending_close = 1; 2403 lost_rqstp->lr_lop = NULL; 2404 lost_rqstp->lr_cr = cr; 2405 lost_rqstp->lr_flk = NULL; 2406 lost_rqstp->lr_putfirst = FALSE; 2407 } 2408 2409 /* 2410 * Assumes you already have the open seqid sync grabbed as well as the 2411 * 'os_sync_lock'. Note: this will release the open seqid sync and 2412 * 'os_sync_lock' if client recovery starts. Calling functions have to 2413 * be prepared to handle this. 2414 * 2415 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2416 * was needed and was started, and that the calling function should retry 2417 * this function; otherwise it is returned as 0. 2418 * 2419 * Errors are returned via the nfs4_error_t parameter. 2420 */ 2421 static void 2422 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2423 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2424 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2425 { 2426 COMPOUND4args_clnt args; 2427 COMPOUND4res_clnt res; 2428 CLOSE4args *close_args; 2429 nfs_resop4 *resop; 2430 nfs_argop4 argop[3]; 2431 int doqueue = 1; 2432 mntinfo4_t *mi; 2433 seqid4 seqid; 2434 vnode_t *vp; 2435 bool_t needrecov = FALSE; 2436 nfs4_lost_rqst_t lost_rqst; 2437 hrtime_t t; 2438 2439 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2440 2441 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2442 2443 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2444 2445 /* Only set this to 1 if recovery is started */ 2446 *recov = 0; 2447 2448 /* do the OTW call to close the file */ 2449 2450 if (close_type == CLOSE_RESEND) 2451 args.ctag = TAG_CLOSE_LOST; 2452 else if (close_type == CLOSE_AFTER_RESEND) 2453 args.ctag = TAG_CLOSE_UNDO; 2454 else 2455 args.ctag = TAG_CLOSE; 2456 2457 args.array_len = 3; 2458 args.array = argop; 2459 2460 vp = RTOV4(rp); 2461 2462 mi = VTOMI4(vp); 2463 2464 /* putfh target fh */ 2465 argop[0].argop = OP_CPUTFH; 2466 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2467 2468 argop[1].argop = OP_GETATTR; 2469 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2470 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2471 2472 argop[2].argop = OP_CLOSE; 2473 close_args = &argop[2].nfs_argop4_u.opclose; 2474 2475 seqid = nfs4_get_open_seqid(oop) + 1; 2476 2477 close_args->seqid = seqid; 2478 close_args->open_stateid = osp->open_stateid; 2479 2480 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2481 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2482 rnode4info(rp))); 2483 2484 t = gethrtime(); 2485 2486 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2487 2488 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2489 nfs4_set_open_seqid(seqid, oop, args.ctag); 2490 } 2491 2492 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2493 if (ep->error && !needrecov) { 2494 /* 2495 * if there was an error and no recovery is to be done 2496 * then then set up the file to flush its cache if 2497 * needed for the next caller. 2498 */ 2499 mutex_enter(&rp->r_statelock); 2500 PURGE_ATTRCACHE4_LOCKED(rp); 2501 rp->r_flags &= ~R4WRITEMODIFIED; 2502 mutex_exit(&rp->r_statelock); 2503 return; 2504 } 2505 2506 if (needrecov) { 2507 bool_t abort; 2508 nfs4_bseqid_entry_t *bsep = NULL; 2509 2510 if (close_type != CLOSE_RESEND) 2511 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2512 osp, cred_otw, vp); 2513 2514 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2515 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2516 0, args.ctag, close_args->seqid); 2517 2518 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2519 "nfs4close_otw: initiating recovery. error %d " 2520 "res.status %d", ep->error, res.status)); 2521 2522 /* 2523 * Drop the 'os_sync_lock' here so we don't hit 2524 * a potential recursive mutex_enter via an 2525 * 'open_stream_hold()'. 2526 */ 2527 mutex_exit(&osp->os_sync_lock); 2528 *have_sync_lockp = 0; 2529 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2530 (close_type != CLOSE_RESEND && 2531 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2532 OP_CLOSE, bsep, NULL, NULL); 2533 2534 /* drop open seq sync, and let the calling function regrab it */ 2535 nfs4_end_open_seqid_sync(oop); 2536 *did_start_seqid_syncp = 0; 2537 2538 if (bsep) 2539 kmem_free(bsep, sizeof (*bsep)); 2540 /* 2541 * For signals, the caller wants to quit, so don't say to 2542 * retry. For forced unmount, if it's a user thread, it 2543 * wants to quit. If it's a recovery thread, the retry 2544 * will happen higher-up on the call stack. Either way, 2545 * don't say to retry. 2546 */ 2547 if (abort == FALSE && ep->error != EINTR && 2548 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2549 close_type != CLOSE_RESEND && 2550 close_type != CLOSE_AFTER_RESEND) 2551 *recov = 1; 2552 else 2553 *recov = 0; 2554 2555 if (!ep->error) 2556 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2557 return; 2558 } 2559 2560 if (res.status) { 2561 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2562 return; 2563 } 2564 2565 mutex_enter(&rp->r_statev4_lock); 2566 rp->created_v4 = 0; 2567 mutex_exit(&rp->r_statev4_lock); 2568 2569 resop = &res.array[2]; 2570 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2571 osp->os_valid = 0; 2572 2573 /* 2574 * This removes the reference obtained at OPEN; ie, when the 2575 * open stream structure was created. 2576 * 2577 * We don't have to worry about calling 'open_stream_rele' 2578 * since we our currently holding a reference to the open 2579 * stream which means the count cannot go to 0 with this 2580 * decrement. 2581 */ 2582 ASSERT(osp->os_ref_count >= 2); 2583 osp->os_ref_count--; 2584 2585 if (!ep->error) 2586 nfs4_attr_cache(vp, 2587 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2588 t, cred_otw, TRUE, NULL); 2589 2590 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2591 " returning %d", ep->error)); 2592 2593 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2594 } 2595 2596 /* ARGSUSED */ 2597 static int 2598 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2599 caller_context_t *ct) 2600 { 2601 rnode4_t *rp; 2602 u_offset_t off; 2603 offset_t diff; 2604 uint_t on; 2605 uint_t n; 2606 caddr_t base; 2607 uint_t flags; 2608 int error; 2609 mntinfo4_t *mi; 2610 2611 rp = VTOR4(vp); 2612 2613 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2614 2615 if (IS_SHADOW(vp, rp)) 2616 vp = RTOV4(rp); 2617 2618 if (vp->v_type != VREG) 2619 return (EISDIR); 2620 2621 mi = VTOMI4(vp); 2622 2623 if (nfs_zone() != mi->mi_zone) 2624 return (EIO); 2625 2626 if (uiop->uio_resid == 0) 2627 return (0); 2628 2629 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2630 return (EINVAL); 2631 2632 mutex_enter(&rp->r_statelock); 2633 if (rp->r_flags & R4RECOVERRP) 2634 error = (rp->r_error ? rp->r_error : EIO); 2635 else 2636 error = 0; 2637 mutex_exit(&rp->r_statelock); 2638 if (error) 2639 return (error); 2640 2641 /* 2642 * Bypass VM if caching has been disabled (e.g., locking) or if 2643 * using client-side direct I/O and the file is not mmap'd and 2644 * there are no cached pages. 2645 */ 2646 if ((vp->v_flag & VNOCACHE) || 2647 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2648 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2649 size_t resid = 0; 2650 2651 return (nfs4read(vp, NULL, uiop->uio_loffset, 2652 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2653 } 2654 2655 error = 0; 2656 2657 do { 2658 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2659 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2660 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2661 2662 if (error = nfs4_validate_caches(vp, cr)) 2663 break; 2664 2665 mutex_enter(&rp->r_statelock); 2666 while (rp->r_flags & R4INCACHEPURGE) { 2667 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2668 mutex_exit(&rp->r_statelock); 2669 return (EINTR); 2670 } 2671 } 2672 diff = rp->r_size - uiop->uio_loffset; 2673 mutex_exit(&rp->r_statelock); 2674 if (diff <= 0) 2675 break; 2676 if (diff < n) 2677 n = (uint_t)diff; 2678 2679 if (vpm_enable) { 2680 /* 2681 * Copy data. 2682 */ 2683 error = vpm_data_copy(vp, off + on, n, uiop, 2684 1, NULL, 0, S_READ); 2685 } else { 2686 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2687 S_READ); 2688 2689 error = uiomove(base + on, n, UIO_READ, uiop); 2690 } 2691 2692 if (!error) { 2693 /* 2694 * If read a whole block or read to eof, 2695 * won't need this buffer again soon. 2696 */ 2697 mutex_enter(&rp->r_statelock); 2698 if (n + on == MAXBSIZE || 2699 uiop->uio_loffset == rp->r_size) 2700 flags = SM_DONTNEED; 2701 else 2702 flags = 0; 2703 mutex_exit(&rp->r_statelock); 2704 if (vpm_enable) { 2705 error = vpm_sync_pages(vp, off, n, flags); 2706 } else { 2707 error = segmap_release(segkmap, base, flags); 2708 } 2709 } else { 2710 if (vpm_enable) { 2711 (void) vpm_sync_pages(vp, off, n, 0); 2712 } else { 2713 (void) segmap_release(segkmap, base, 0); 2714 } 2715 } 2716 } while (!error && uiop->uio_resid > 0); 2717 2718 return (error); 2719 } 2720 2721 /* ARGSUSED */ 2722 static int 2723 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2724 caller_context_t *ct) 2725 { 2726 rlim64_t limit = uiop->uio_llimit; 2727 rnode4_t *rp; 2728 u_offset_t off; 2729 caddr_t base; 2730 uint_t flags; 2731 int remainder; 2732 size_t n; 2733 int on; 2734 int error; 2735 int resid; 2736 u_offset_t offset; 2737 mntinfo4_t *mi; 2738 uint_t bsize; 2739 2740 rp = VTOR4(vp); 2741 2742 if (IS_SHADOW(vp, rp)) 2743 vp = RTOV4(rp); 2744 2745 if (vp->v_type != VREG) 2746 return (EISDIR); 2747 2748 mi = VTOMI4(vp); 2749 2750 if (nfs_zone() != mi->mi_zone) 2751 return (EIO); 2752 2753 if (uiop->uio_resid == 0) 2754 return (0); 2755 2756 mutex_enter(&rp->r_statelock); 2757 if (rp->r_flags & R4RECOVERRP) 2758 error = (rp->r_error ? rp->r_error : EIO); 2759 else 2760 error = 0; 2761 mutex_exit(&rp->r_statelock); 2762 if (error) 2763 return (error); 2764 2765 if (ioflag & FAPPEND) { 2766 struct vattr va; 2767 2768 /* 2769 * Must serialize if appending. 2770 */ 2771 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2772 nfs_rw_exit(&rp->r_rwlock); 2773 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2774 INTR4(vp))) 2775 return (EINTR); 2776 } 2777 2778 va.va_mask = AT_SIZE; 2779 error = nfs4getattr(vp, &va, cr); 2780 if (error) 2781 return (error); 2782 uiop->uio_loffset = va.va_size; 2783 } 2784 2785 offset = uiop->uio_loffset + uiop->uio_resid; 2786 2787 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2788 return (EINVAL); 2789 2790 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2791 limit = MAXOFFSET_T; 2792 2793 /* 2794 * Check to make sure that the process will not exceed 2795 * its limit on file size. It is okay to write up to 2796 * the limit, but not beyond. Thus, the write which 2797 * reaches the limit will be short and the next write 2798 * will return an error. 2799 */ 2800 remainder = 0; 2801 if (offset > uiop->uio_llimit) { 2802 remainder = offset - uiop->uio_llimit; 2803 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2804 if (uiop->uio_resid <= 0) { 2805 proc_t *p = ttoproc(curthread); 2806 2807 uiop->uio_resid += remainder; 2808 mutex_enter(&p->p_lock); 2809 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2810 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2811 mutex_exit(&p->p_lock); 2812 return (EFBIG); 2813 } 2814 } 2815 2816 /* update the change attribute, if we have a write delegation */ 2817 2818 mutex_enter(&rp->r_statev4_lock); 2819 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2820 rp->r_deleg_change++; 2821 2822 mutex_exit(&rp->r_statev4_lock); 2823 2824 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, INTR4(vp))) 2825 return (EINTR); 2826 2827 /* 2828 * Bypass VM if caching has been disabled (e.g., locking) or if 2829 * using client-side direct I/O and the file is not mmap'd and 2830 * there are no cached pages. 2831 */ 2832 if ((vp->v_flag & VNOCACHE) || 2833 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2834 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2835 size_t bufsize; 2836 int count; 2837 u_offset_t org_offset; 2838 stable_how4 stab_comm; 2839 nfs4_fwrite: 2840 if (rp->r_flags & R4STALE) { 2841 resid = uiop->uio_resid; 2842 offset = uiop->uio_loffset; 2843 error = rp->r_error; 2844 /* 2845 * A close may have cleared r_error, if so, 2846 * propagate ESTALE error return properly 2847 */ 2848 if (error == 0) 2849 error = ESTALE; 2850 goto bottom; 2851 } 2852 2853 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2854 base = kmem_alloc(bufsize, KM_SLEEP); 2855 do { 2856 if (ioflag & FDSYNC) 2857 stab_comm = DATA_SYNC4; 2858 else 2859 stab_comm = FILE_SYNC4; 2860 resid = uiop->uio_resid; 2861 offset = uiop->uio_loffset; 2862 count = MIN(uiop->uio_resid, bufsize); 2863 org_offset = uiop->uio_loffset; 2864 error = uiomove(base, count, UIO_WRITE, uiop); 2865 if (!error) { 2866 error = nfs4write(vp, base, org_offset, 2867 count, cr, &stab_comm); 2868 if (!error) { 2869 mutex_enter(&rp->r_statelock); 2870 if (rp->r_size < uiop->uio_loffset) 2871 rp->r_size = uiop->uio_loffset; 2872 mutex_exit(&rp->r_statelock); 2873 } 2874 } 2875 } while (!error && uiop->uio_resid > 0); 2876 kmem_free(base, bufsize); 2877 goto bottom; 2878 } 2879 2880 bsize = vp->v_vfsp->vfs_bsize; 2881 2882 do { 2883 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2884 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2885 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2886 2887 resid = uiop->uio_resid; 2888 offset = uiop->uio_loffset; 2889 2890 if (rp->r_flags & R4STALE) { 2891 error = rp->r_error; 2892 /* 2893 * A close may have cleared r_error, if so, 2894 * propagate ESTALE error return properly 2895 */ 2896 if (error == 0) 2897 error = ESTALE; 2898 break; 2899 } 2900 2901 /* 2902 * Don't create dirty pages faster than they 2903 * can be cleaned so that the system doesn't 2904 * get imbalanced. If the async queue is 2905 * maxed out, then wait for it to drain before 2906 * creating more dirty pages. Also, wait for 2907 * any threads doing pagewalks in the vop_getattr 2908 * entry points so that they don't block for 2909 * long periods. 2910 */ 2911 mutex_enter(&rp->r_statelock); 2912 while ((mi->mi_max_threads != 0 && 2913 rp->r_awcount > 2 * mi->mi_max_threads) || 2914 rp->r_gcount > 0) { 2915 if (INTR4(vp)) { 2916 klwp_t *lwp = ttolwp(curthread); 2917 2918 if (lwp != NULL) 2919 lwp->lwp_nostop++; 2920 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2921 mutex_exit(&rp->r_statelock); 2922 if (lwp != NULL) 2923 lwp->lwp_nostop--; 2924 error = EINTR; 2925 goto bottom; 2926 } 2927 if (lwp != NULL) 2928 lwp->lwp_nostop--; 2929 } else 2930 cv_wait(&rp->r_cv, &rp->r_statelock); 2931 } 2932 mutex_exit(&rp->r_statelock); 2933 2934 /* 2935 * Touch the page and fault it in if it is not in core 2936 * before segmap_getmapflt or vpm_data_copy can lock it. 2937 * This is to avoid the deadlock if the buffer is mapped 2938 * to the same file through mmap which we want to write. 2939 */ 2940 uio_prefaultpages((long)n, uiop); 2941 2942 if (vpm_enable) { 2943 /* 2944 * It will use kpm mappings, so no need to 2945 * pass an address. 2946 */ 2947 error = writerp4(rp, NULL, n, uiop, 0); 2948 } else { 2949 if (segmap_kpm) { 2950 int pon = uiop->uio_loffset & PAGEOFFSET; 2951 size_t pn = MIN(PAGESIZE - pon, 2952 uiop->uio_resid); 2953 int pagecreate; 2954 2955 mutex_enter(&rp->r_statelock); 2956 pagecreate = (pon == 0) && (pn == PAGESIZE || 2957 uiop->uio_loffset + pn >= rp->r_size); 2958 mutex_exit(&rp->r_statelock); 2959 2960 base = segmap_getmapflt(segkmap, vp, off + on, 2961 pn, !pagecreate, S_WRITE); 2962 2963 error = writerp4(rp, base + pon, n, uiop, 2964 pagecreate); 2965 2966 } else { 2967 base = segmap_getmapflt(segkmap, vp, off + on, 2968 n, 0, S_READ); 2969 error = writerp4(rp, base + on, n, uiop, 0); 2970 } 2971 } 2972 2973 if (!error) { 2974 if (mi->mi_flags & MI4_NOAC) 2975 flags = SM_WRITE; 2976 else if ((uiop->uio_loffset % bsize) == 0 || 2977 IS_SWAPVP(vp)) { 2978 /* 2979 * Have written a whole block. 2980 * Start an asynchronous write 2981 * and mark the buffer to 2982 * indicate that it won't be 2983 * needed again soon. 2984 */ 2985 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2986 } else 2987 flags = 0; 2988 if ((ioflag & (FSYNC|FDSYNC)) || 2989 (rp->r_flags & R4OUTOFSPACE)) { 2990 flags &= ~SM_ASYNC; 2991 flags |= SM_WRITE; 2992 } 2993 if (vpm_enable) { 2994 error = vpm_sync_pages(vp, off, n, flags); 2995 } else { 2996 error = segmap_release(segkmap, base, flags); 2997 } 2998 } else { 2999 if (vpm_enable) { 3000 (void) vpm_sync_pages(vp, off, n, 0); 3001 } else { 3002 (void) segmap_release(segkmap, base, 0); 3003 } 3004 /* 3005 * In the event that we got an access error while 3006 * faulting in a page for a write-only file just 3007 * force a write. 3008 */ 3009 if (error == EACCES) 3010 goto nfs4_fwrite; 3011 } 3012 } while (!error && uiop->uio_resid > 0); 3013 3014 bottom: 3015 if (error) { 3016 uiop->uio_resid = resid + remainder; 3017 uiop->uio_loffset = offset; 3018 } else { 3019 uiop->uio_resid += remainder; 3020 3021 mutex_enter(&rp->r_statev4_lock); 3022 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 3023 gethrestime(&rp->r_attr.va_mtime); 3024 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3025 } 3026 mutex_exit(&rp->r_statev4_lock); 3027 } 3028 3029 nfs_rw_exit(&rp->r_lkserlock); 3030 3031 return (error); 3032 } 3033 3034 /* 3035 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 3036 */ 3037 static int 3038 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 3039 int flags, cred_t *cr) 3040 { 3041 struct buf *bp; 3042 int error; 3043 page_t *savepp; 3044 uchar_t fsdata; 3045 stable_how4 stab_comm; 3046 3047 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3048 bp = pageio_setup(pp, len, vp, flags); 3049 ASSERT(bp != NULL); 3050 3051 /* 3052 * pageio_setup should have set b_addr to 0. This 3053 * is correct since we want to do I/O on a page 3054 * boundary. bp_mapin will use this addr to calculate 3055 * an offset, and then set b_addr to the kernel virtual 3056 * address it allocated for us. 3057 */ 3058 ASSERT(bp->b_un.b_addr == 0); 3059 3060 bp->b_edev = 0; 3061 bp->b_dev = 0; 3062 bp->b_lblkno = lbtodb(off); 3063 bp->b_file = vp; 3064 bp->b_offset = (offset_t)off; 3065 bp_mapin(bp); 3066 3067 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 3068 freemem > desfree) 3069 stab_comm = UNSTABLE4; 3070 else 3071 stab_comm = FILE_SYNC4; 3072 3073 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 3074 3075 bp_mapout(bp); 3076 pageio_done(bp); 3077 3078 if (stab_comm == UNSTABLE4) 3079 fsdata = C_DELAYCOMMIT; 3080 else 3081 fsdata = C_NOCOMMIT; 3082 3083 savepp = pp; 3084 do { 3085 pp->p_fsdata = fsdata; 3086 } while ((pp = pp->p_next) != savepp); 3087 3088 return (error); 3089 } 3090 3091 /* 3092 */ 3093 static int 3094 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3095 { 3096 nfs4_open_owner_t *oop; 3097 nfs4_open_stream_t *osp; 3098 rnode4_t *rp = VTOR4(vp); 3099 mntinfo4_t *mi = VTOMI4(vp); 3100 int reopen_needed; 3101 3102 ASSERT(nfs_zone() == mi->mi_zone); 3103 3104 3105 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3106 if (!oop) 3107 return (EIO); 3108 3109 /* returns with 'os_sync_lock' held */ 3110 osp = find_open_stream(oop, rp); 3111 if (!osp) { 3112 open_owner_rele(oop); 3113 return (EIO); 3114 } 3115 3116 if (osp->os_failed_reopen) { 3117 mutex_exit(&osp->os_sync_lock); 3118 open_stream_rele(osp, rp); 3119 open_owner_rele(oop); 3120 return (EIO); 3121 } 3122 3123 /* 3124 * Determine whether a reopen is needed. If this 3125 * is a delegation open stream, then the os_delegation bit 3126 * should be set. 3127 */ 3128 3129 reopen_needed = osp->os_delegation; 3130 3131 mutex_exit(&osp->os_sync_lock); 3132 open_owner_rele(oop); 3133 3134 if (reopen_needed) { 3135 nfs4_error_zinit(ep); 3136 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3137 mutex_enter(&osp->os_sync_lock); 3138 if (ep->error || ep->stat || osp->os_failed_reopen) { 3139 mutex_exit(&osp->os_sync_lock); 3140 open_stream_rele(osp, rp); 3141 return (EIO); 3142 } 3143 mutex_exit(&osp->os_sync_lock); 3144 } 3145 open_stream_rele(osp, rp); 3146 3147 return (0); 3148 } 3149 3150 /* 3151 * Write to file. Writes to remote server in largest size 3152 * chunks that the server can handle. Write is synchronous. 3153 */ 3154 static int 3155 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3156 stable_how4 *stab_comm) 3157 { 3158 mntinfo4_t *mi; 3159 COMPOUND4args_clnt args; 3160 COMPOUND4res_clnt res; 3161 WRITE4args *wargs; 3162 WRITE4res *wres; 3163 nfs_argop4 argop[2]; 3164 nfs_resop4 *resop; 3165 int tsize; 3166 stable_how4 stable; 3167 rnode4_t *rp; 3168 int doqueue = 1; 3169 bool_t needrecov; 3170 nfs4_recov_state_t recov_state; 3171 nfs4_stateid_types_t sid_types; 3172 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3173 int recov; 3174 3175 rp = VTOR4(vp); 3176 mi = VTOMI4(vp); 3177 3178 ASSERT(nfs_zone() == mi->mi_zone); 3179 3180 stable = *stab_comm; 3181 *stab_comm = FILE_SYNC4; 3182 3183 needrecov = FALSE; 3184 recov_state.rs_flags = 0; 3185 recov_state.rs_num_retry_despite_err = 0; 3186 nfs4_init_stateid_types(&sid_types); 3187 3188 /* Is curthread the recovery thread? */ 3189 mutex_enter(&mi->mi_lock); 3190 recov = (mi->mi_recovthread == curthread); 3191 mutex_exit(&mi->mi_lock); 3192 3193 recov_retry: 3194 args.ctag = TAG_WRITE; 3195 args.array_len = 2; 3196 args.array = argop; 3197 3198 if (!recov) { 3199 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3200 &recov_state, NULL); 3201 if (e.error) 3202 return (e.error); 3203 } 3204 3205 /* 0. putfh target fh */ 3206 argop[0].argop = OP_CPUTFH; 3207 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3208 3209 /* 1. write */ 3210 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3211 3212 do { 3213 3214 wargs->offset = (offset4)offset; 3215 wargs->data_val = base; 3216 3217 if (mi->mi_io_kstats) { 3218 mutex_enter(&mi->mi_lock); 3219 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3220 mutex_exit(&mi->mi_lock); 3221 } 3222 3223 if ((vp->v_flag & VNOCACHE) || 3224 (rp->r_flags & R4DIRECTIO) || 3225 (mi->mi_flags & MI4_DIRECTIO)) 3226 tsize = MIN(mi->mi_stsize, count); 3227 else 3228 tsize = MIN(mi->mi_curwrite, count); 3229 wargs->data_len = (uint_t)tsize; 3230 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3231 3232 if (mi->mi_io_kstats) { 3233 mutex_enter(&mi->mi_lock); 3234 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3235 mutex_exit(&mi->mi_lock); 3236 } 3237 3238 if (!recov) { 3239 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3240 if (e.error && !needrecov) { 3241 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3242 &recov_state, needrecov); 3243 return (e.error); 3244 } 3245 } else { 3246 if (e.error) 3247 return (e.error); 3248 } 3249 3250 /* 3251 * Do handling of OLD_STATEID outside 3252 * of the normal recovery framework. 3253 * 3254 * If write receives a BAD stateid error while using a 3255 * delegation stateid, retry using the open stateid (if it 3256 * exists). If it doesn't have an open stateid, reopen the 3257 * file first, then retry. 3258 */ 3259 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3260 sid_types.cur_sid_type != SPEC_SID) { 3261 nfs4_save_stateid(&wargs->stateid, &sid_types); 3262 if (!recov) 3263 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3264 &recov_state, needrecov); 3265 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3266 goto recov_retry; 3267 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3268 sid_types.cur_sid_type == DEL_SID) { 3269 nfs4_save_stateid(&wargs->stateid, &sid_types); 3270 mutex_enter(&rp->r_statev4_lock); 3271 rp->r_deleg_return_pending = TRUE; 3272 mutex_exit(&rp->r_statev4_lock); 3273 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3274 if (!recov) 3275 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3276 &recov_state, needrecov); 3277 (void) xdr_free(xdr_COMPOUND4res_clnt, 3278 (caddr_t)&res); 3279 return (EIO); 3280 } 3281 if (!recov) 3282 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3283 &recov_state, needrecov); 3284 /* hold needed for nfs4delegreturn_thread */ 3285 VN_HOLD(vp); 3286 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3287 NFS4_DR_DISCARD), FALSE); 3288 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3289 goto recov_retry; 3290 } 3291 3292 if (needrecov) { 3293 bool_t abort; 3294 3295 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3296 "nfs4write: client got error %d, res.status %d" 3297 ", so start recovery", e.error, res.status)); 3298 3299 abort = nfs4_start_recovery(&e, 3300 VTOMI4(vp), vp, NULL, &wargs->stateid, 3301 NULL, OP_WRITE, NULL, NULL, NULL); 3302 if (!e.error) { 3303 e.error = geterrno4(res.status); 3304 (void) xdr_free(xdr_COMPOUND4res_clnt, 3305 (caddr_t)&res); 3306 } 3307 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3308 &recov_state, needrecov); 3309 if (abort == FALSE) 3310 goto recov_retry; 3311 return (e.error); 3312 } 3313 3314 if (res.status) { 3315 e.error = geterrno4(res.status); 3316 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3317 if (!recov) 3318 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3319 &recov_state, needrecov); 3320 return (e.error); 3321 } 3322 3323 resop = &res.array[1]; /* write res */ 3324 wres = &resop->nfs_resop4_u.opwrite; 3325 3326 if ((int)wres->count > tsize) { 3327 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3328 3329 zcmn_err(getzoneid(), CE_WARN, 3330 "nfs4write: server wrote %u, requested was %u", 3331 (int)wres->count, tsize); 3332 if (!recov) 3333 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3334 &recov_state, needrecov); 3335 return (EIO); 3336 } 3337 if (wres->committed == UNSTABLE4) { 3338 *stab_comm = UNSTABLE4; 3339 if (wargs->stable == DATA_SYNC4 || 3340 wargs->stable == FILE_SYNC4) { 3341 (void) xdr_free(xdr_COMPOUND4res_clnt, 3342 (caddr_t)&res); 3343 zcmn_err(getzoneid(), CE_WARN, 3344 "nfs4write: server %s did not commit " 3345 "to stable storage", 3346 rp->r_server->sv_hostname); 3347 if (!recov) 3348 nfs4_end_fop(VTOMI4(vp), vp, NULL, 3349 OH_WRITE, &recov_state, needrecov); 3350 return (EIO); 3351 } 3352 } 3353 3354 tsize = (int)wres->count; 3355 count -= tsize; 3356 base += tsize; 3357 offset += tsize; 3358 if (mi->mi_io_kstats) { 3359 mutex_enter(&mi->mi_lock); 3360 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3361 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3362 tsize; 3363 mutex_exit(&mi->mi_lock); 3364 } 3365 lwp_stat_update(LWP_STAT_OUBLK, 1); 3366 mutex_enter(&rp->r_statelock); 3367 if (rp->r_flags & R4HAVEVERF) { 3368 if (rp->r_writeverf != wres->writeverf) { 3369 nfs4_set_mod(vp); 3370 rp->r_writeverf = wres->writeverf; 3371 } 3372 } else { 3373 rp->r_writeverf = wres->writeverf; 3374 rp->r_flags |= R4HAVEVERF; 3375 } 3376 PURGE_ATTRCACHE4_LOCKED(rp); 3377 rp->r_flags |= R4WRITEMODIFIED; 3378 gethrestime(&rp->r_attr.va_mtime); 3379 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3380 mutex_exit(&rp->r_statelock); 3381 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3382 } while (count); 3383 3384 if (!recov) 3385 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, 3386 needrecov); 3387 3388 return (e.error); 3389 } 3390 3391 /* 3392 * Read from a file. Reads data in largest chunks our interface can handle. 3393 */ 3394 static int 3395 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3396 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3397 { 3398 mntinfo4_t *mi; 3399 COMPOUND4args_clnt args; 3400 COMPOUND4res_clnt res; 3401 READ4args *rargs; 3402 nfs_argop4 argop[2]; 3403 int tsize; 3404 int doqueue; 3405 rnode4_t *rp; 3406 int data_len; 3407 bool_t is_eof; 3408 bool_t needrecov = FALSE; 3409 nfs4_recov_state_t recov_state; 3410 nfs4_stateid_types_t sid_types; 3411 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3412 3413 rp = VTOR4(vp); 3414 mi = VTOMI4(vp); 3415 doqueue = 1; 3416 3417 ASSERT(nfs_zone() == mi->mi_zone); 3418 3419 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3420 3421 args.array_len = 2; 3422 args.array = argop; 3423 3424 nfs4_init_stateid_types(&sid_types); 3425 3426 recov_state.rs_flags = 0; 3427 recov_state.rs_num_retry_despite_err = 0; 3428 3429 recov_retry: 3430 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3431 &recov_state, NULL); 3432 if (e.error) 3433 return (e.error); 3434 3435 /* putfh target fh */ 3436 argop[0].argop = OP_CPUTFH; 3437 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3438 3439 /* read */ 3440 argop[1].argop = OP_READ; 3441 rargs = &argop[1].nfs_argop4_u.opread; 3442 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3443 OP_READ, &sid_types, async); 3444 3445 do { 3446 if (mi->mi_io_kstats) { 3447 mutex_enter(&mi->mi_lock); 3448 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3449 mutex_exit(&mi->mi_lock); 3450 } 3451 3452 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3453 "nfs4read: %s call, rp %s", 3454 needrecov ? "recov" : "first", 3455 rnode4info(rp))); 3456 3457 if ((vp->v_flag & VNOCACHE) || 3458 (rp->r_flags & R4DIRECTIO) || 3459 (mi->mi_flags & MI4_DIRECTIO)) 3460 tsize = MIN(mi->mi_tsize, count); 3461 else 3462 tsize = MIN(mi->mi_curread, count); 3463 3464 rargs->offset = (offset4)offset; 3465 rargs->count = (count4)tsize; 3466 rargs->res_data_val_alt = NULL; 3467 rargs->res_mblk = NULL; 3468 rargs->res_uiop = NULL; 3469 rargs->res_maxsize = 0; 3470 rargs->wlist = NULL; 3471 3472 if (uiop) 3473 rargs->res_uiop = uiop; 3474 else 3475 rargs->res_data_val_alt = base; 3476 rargs->res_maxsize = tsize; 3477 3478 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3479 #ifdef DEBUG 3480 if (nfs4read_error_inject) { 3481 res.status = nfs4read_error_inject; 3482 nfs4read_error_inject = 0; 3483 } 3484 #endif 3485 3486 if (mi->mi_io_kstats) { 3487 mutex_enter(&mi->mi_lock); 3488 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3489 mutex_exit(&mi->mi_lock); 3490 } 3491 3492 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3493 if (e.error != 0 && !needrecov) { 3494 nfs4_end_fop(mi, vp, NULL, OH_READ, 3495 &recov_state, needrecov); 3496 return (e.error); 3497 } 3498 3499 /* 3500 * Do proper retry for OLD and BAD stateid errors outside 3501 * of the normal recovery framework. There are two differences 3502 * between async and sync reads. The first is that we allow 3503 * retry on BAD_STATEID for async reads, but not sync reads. 3504 * The second is that we mark the file dead for a failed 3505 * attempt with a special stateid for sync reads, but just 3506 * return EIO for async reads. 3507 * 3508 * If a sync read receives a BAD stateid error while using a 3509 * delegation stateid, retry using the open stateid (if it 3510 * exists). If it doesn't have an open stateid, reopen the 3511 * file first, then retry. 3512 */ 3513 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3514 res.status == NFS4ERR_BAD_STATEID) && async) { 3515 nfs4_end_fop(mi, vp, NULL, OH_READ, 3516 &recov_state, needrecov); 3517 if (sid_types.cur_sid_type == SPEC_SID) { 3518 (void) xdr_free(xdr_COMPOUND4res_clnt, 3519 (caddr_t)&res); 3520 return (EIO); 3521 } 3522 nfs4_save_stateid(&rargs->stateid, &sid_types); 3523 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3524 goto recov_retry; 3525 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3526 !async && sid_types.cur_sid_type != SPEC_SID) { 3527 nfs4_save_stateid(&rargs->stateid, &sid_types); 3528 nfs4_end_fop(mi, vp, NULL, OH_READ, 3529 &recov_state, needrecov); 3530 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3531 goto recov_retry; 3532 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3533 sid_types.cur_sid_type == DEL_SID) { 3534 nfs4_save_stateid(&rargs->stateid, &sid_types); 3535 mutex_enter(&rp->r_statev4_lock); 3536 rp->r_deleg_return_pending = TRUE; 3537 mutex_exit(&rp->r_statev4_lock); 3538 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3539 nfs4_end_fop(mi, vp, NULL, OH_READ, 3540 &recov_state, needrecov); 3541 (void) xdr_free(xdr_COMPOUND4res_clnt, 3542 (caddr_t)&res); 3543 return (EIO); 3544 } 3545 nfs4_end_fop(mi, vp, NULL, OH_READ, 3546 &recov_state, needrecov); 3547 /* hold needed for nfs4delegreturn_thread */ 3548 VN_HOLD(vp); 3549 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3550 NFS4_DR_DISCARD), FALSE); 3551 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3552 goto recov_retry; 3553 } 3554 if (needrecov) { 3555 bool_t abort; 3556 3557 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3558 "nfs4read: initiating recovery\n")); 3559 abort = nfs4_start_recovery(&e, 3560 mi, vp, NULL, &rargs->stateid, 3561 NULL, OP_READ, NULL, NULL, NULL); 3562 nfs4_end_fop(mi, vp, NULL, OH_READ, 3563 &recov_state, needrecov); 3564 /* 3565 * Do not retry if we got OLD_STATEID using a special 3566 * stateid. This avoids looping with a broken server. 3567 */ 3568 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3569 sid_types.cur_sid_type == SPEC_SID) 3570 abort = TRUE; 3571 3572 if (abort == FALSE) { 3573 /* 3574 * Need to retry all possible stateids in 3575 * case the recovery error wasn't stateid 3576 * related or the stateids have become 3577 * stale (server reboot). 3578 */ 3579 nfs4_init_stateid_types(&sid_types); 3580 (void) xdr_free(xdr_COMPOUND4res_clnt, 3581 (caddr_t)&res); 3582 goto recov_retry; 3583 } 3584 3585 if (!e.error) { 3586 e.error = geterrno4(res.status); 3587 (void) xdr_free(xdr_COMPOUND4res_clnt, 3588 (caddr_t)&res); 3589 } 3590 return (e.error); 3591 } 3592 3593 if (res.status) { 3594 e.error = geterrno4(res.status); 3595 nfs4_end_fop(mi, vp, NULL, OH_READ, 3596 &recov_state, needrecov); 3597 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3598 return (e.error); 3599 } 3600 3601 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3602 count -= data_len; 3603 if (base) 3604 base += data_len; 3605 offset += data_len; 3606 if (mi->mi_io_kstats) { 3607 mutex_enter(&mi->mi_lock); 3608 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3609 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3610 mutex_exit(&mi->mi_lock); 3611 } 3612 lwp_stat_update(LWP_STAT_INBLK, 1); 3613 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3614 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3615 3616 } while (count && !is_eof); 3617 3618 *residp = count; 3619 3620 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3621 3622 return (e.error); 3623 } 3624 3625 /* ARGSUSED */ 3626 static int 3627 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp, 3628 caller_context_t *ct) 3629 { 3630 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3631 return (EIO); 3632 switch (cmd) { 3633 case _FIODIRECTIO: 3634 return (nfs4_directio(vp, (int)arg, cr)); 3635 default: 3636 return (ENOTTY); 3637 } 3638 } 3639 3640 /* ARGSUSED */ 3641 int 3642 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3643 caller_context_t *ct) 3644 { 3645 int error; 3646 rnode4_t *rp = VTOR4(vp); 3647 3648 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3649 return (EIO); 3650 /* 3651 * If it has been specified that the return value will 3652 * just be used as a hint, and we are only being asked 3653 * for size, fsid or rdevid, then return the client's 3654 * notion of these values without checking to make sure 3655 * that the attribute cache is up to date. 3656 * The whole point is to avoid an over the wire GETATTR 3657 * call. 3658 */ 3659 if (flags & ATTR_HINT) { 3660 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) { 3661 mutex_enter(&rp->r_statelock); 3662 if (vap->va_mask & AT_SIZE) 3663 vap->va_size = rp->r_size; 3664 if (vap->va_mask & AT_FSID) 3665 vap->va_fsid = rp->r_attr.va_fsid; 3666 if (vap->va_mask & AT_RDEV) 3667 vap->va_rdev = rp->r_attr.va_rdev; 3668 mutex_exit(&rp->r_statelock); 3669 return (0); 3670 } 3671 } 3672 3673 /* 3674 * Only need to flush pages if asking for the mtime 3675 * and if there any dirty pages or any outstanding 3676 * asynchronous (write) requests for this file. 3677 */ 3678 if (vap->va_mask & AT_MTIME) { 3679 rp = VTOR4(vp); 3680 if (nfs4_has_pages(vp)) { 3681 mutex_enter(&rp->r_statev4_lock); 3682 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3683 mutex_exit(&rp->r_statev4_lock); 3684 if (rp->r_flags & R4DIRTY || 3685 rp->r_awcount > 0) { 3686 mutex_enter(&rp->r_statelock); 3687 rp->r_gcount++; 3688 mutex_exit(&rp->r_statelock); 3689 error = 3690 nfs4_putpage(vp, (u_offset_t)0, 3691 0, 0, cr, NULL); 3692 mutex_enter(&rp->r_statelock); 3693 if (error && (error == ENOSPC || 3694 error == EDQUOT)) { 3695 if (!rp->r_error) 3696 rp->r_error = error; 3697 } 3698 if (--rp->r_gcount == 0) 3699 cv_broadcast(&rp->r_cv); 3700 mutex_exit(&rp->r_statelock); 3701 } 3702 } else { 3703 mutex_exit(&rp->r_statev4_lock); 3704 } 3705 } 3706 } 3707 return (nfs4getattr(vp, vap, cr)); 3708 } 3709 3710 int 3711 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3712 { 3713 /* 3714 * If these are the only two bits cleared 3715 * on the server then return 0 (OK) else 3716 * return 1 (BAD). 3717 */ 3718 on_client &= ~(S_ISUID|S_ISGID); 3719 if (on_client == from_server) 3720 return (0); 3721 else 3722 return (1); 3723 } 3724 3725 /*ARGSUSED4*/ 3726 static int 3727 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3728 caller_context_t *ct) 3729 { 3730 int error; 3731 3732 if (vap->va_mask & AT_NOSET) 3733 return (EINVAL); 3734 3735 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3736 return (EIO); 3737 3738 /* 3739 * Don't call secpolicy_vnode_setattr, the client cannot 3740 * use its cached attributes to make security decisions 3741 * as the server may be faking mode bits or mapping uid/gid. 3742 * Always just let the server to the checking. 3743 * If we provide the ability to remove basic priviledges 3744 * to setattr (e.g. basic without chmod) then we will 3745 * need to add a check here before calling the server. 3746 */ 3747 error = nfs4setattr(vp, vap, flags, cr, NULL); 3748 3749 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0) 3750 vnevent_truncate(vp, ct); 3751 3752 return (error); 3753 } 3754 3755 /* 3756 * To replace the "guarded" version 3 setattr, we use two types of compound 3757 * setattr requests: 3758 * 1. The "normal" setattr, used when the size of the file isn't being 3759 * changed - { Putfh <fh>; Setattr; Getattr }/ 3760 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3761 * with only ctime as the argument. If the server ctime differs from 3762 * what is cached on the client, the verify will fail, but we would 3763 * already have the ctime from the preceding getattr, so just set it 3764 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3765 * Setattr; Getattr }. 3766 * 3767 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3768 * this setattr and NULL if they are not. 3769 */ 3770 static int 3771 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3772 vsecattr_t *vsap) 3773 { 3774 COMPOUND4args_clnt args; 3775 COMPOUND4res_clnt res, *resp = NULL; 3776 nfs4_ga_res_t *garp = NULL; 3777 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3778 nfs_argop4 argop[5]; 3779 int verify_argop = -1; 3780 int setattr_argop = 1; 3781 nfs_resop4 *resop; 3782 vattr_t va; 3783 rnode4_t *rp; 3784 int doqueue = 1; 3785 uint_t mask = vap->va_mask; 3786 mode_t omode; 3787 vsecattr_t *vsp; 3788 timestruc_t ctime; 3789 bool_t needrecov = FALSE; 3790 nfs4_recov_state_t recov_state; 3791 nfs4_stateid_types_t sid_types; 3792 stateid4 stateid; 3793 hrtime_t t; 3794 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3795 servinfo4_t *svp; 3796 bitmap4 supp_attrs; 3797 3798 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3799 rp = VTOR4(vp); 3800 nfs4_init_stateid_types(&sid_types); 3801 3802 /* 3803 * Only need to flush pages if there are any pages and 3804 * if the file is marked as dirty in some fashion. The 3805 * file must be flushed so that we can accurately 3806 * determine the size of the file and the cached data 3807 * after the SETATTR returns. A file is considered to 3808 * be dirty if it is either marked with R4DIRTY, has 3809 * outstanding i/o's active, or is mmap'd. In this 3810 * last case, we can't tell whether there are dirty 3811 * pages, so we flush just to be sure. 3812 */ 3813 if (nfs4_has_pages(vp) && 3814 ((rp->r_flags & R4DIRTY) || 3815 rp->r_count > 0 || 3816 rp->r_mapcnt > 0)) { 3817 ASSERT(vp->v_type != VCHR); 3818 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL); 3819 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3820 mutex_enter(&rp->r_statelock); 3821 if (!rp->r_error) 3822 rp->r_error = e.error; 3823 mutex_exit(&rp->r_statelock); 3824 } 3825 } 3826 3827 if (mask & AT_SIZE) { 3828 /* 3829 * Verification setattr compound for non-deleg AT_SIZE: 3830 * { Putfh; Getattr; Verify; Setattr; Getattr } 3831 * Set ctime local here (outside the do_again label) 3832 * so that subsequent retries (after failed VERIFY) 3833 * will use ctime from GETATTR results (from failed 3834 * verify compound) as VERIFY arg. 3835 * If file has delegation, then VERIFY(time_metadata) 3836 * is of little added value, so don't bother. 3837 */ 3838 mutex_enter(&rp->r_statev4_lock); 3839 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3840 rp->r_deleg_return_pending) { 3841 numops = 5; 3842 ctime = rp->r_attr.va_ctime; 3843 } 3844 mutex_exit(&rp->r_statev4_lock); 3845 } 3846 3847 recov_state.rs_flags = 0; 3848 recov_state.rs_num_retry_despite_err = 0; 3849 3850 args.ctag = TAG_SETATTR; 3851 do_again: 3852 recov_retry: 3853 setattr_argop = numops - 2; 3854 3855 args.array = argop; 3856 args.array_len = numops; 3857 3858 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3859 if (e.error) 3860 return (e.error); 3861 3862 3863 /* putfh target fh */ 3864 argop[0].argop = OP_CPUTFH; 3865 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3866 3867 if (numops == 5) { 3868 /* 3869 * We only care about the ctime, but need to get mtime 3870 * and size for proper cache update. 3871 */ 3872 /* getattr */ 3873 argop[1].argop = OP_GETATTR; 3874 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3875 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3876 3877 /* verify - set later in loop */ 3878 verify_argop = 2; 3879 } 3880 3881 /* setattr */ 3882 svp = rp->r_server; 3883 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3884 supp_attrs = svp->sv_supp_attrs; 3885 nfs_rw_exit(&svp->sv_lock); 3886 3887 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3888 supp_attrs, &e.error, &sid_types); 3889 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3890 if (e.error) { 3891 /* req time field(s) overflow - return immediately */ 3892 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3893 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3894 opsetattr.obj_attributes); 3895 return (e.error); 3896 } 3897 omode = rp->r_attr.va_mode; 3898 3899 /* getattr */ 3900 argop[numops-1].argop = OP_GETATTR; 3901 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3902 /* 3903 * If we are setting the ACL (indicated only by vsap != NULL), request 3904 * the ACL in this getattr. The ACL returned from this getattr will be 3905 * used in updating the ACL cache. 3906 */ 3907 if (vsap != NULL) 3908 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3909 FATTR4_ACL_MASK; 3910 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3911 3912 /* 3913 * setattr iterates if the object size is set and the cached ctime 3914 * does not match the file ctime. In that case, verify the ctime first. 3915 */ 3916 3917 do { 3918 if (verify_argop != -1) { 3919 /* 3920 * Verify that the ctime match before doing setattr. 3921 */ 3922 va.va_mask = AT_CTIME; 3923 va.va_ctime = ctime; 3924 svp = rp->r_server; 3925 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3926 supp_attrs = svp->sv_supp_attrs; 3927 nfs_rw_exit(&svp->sv_lock); 3928 e.error = nfs4args_verify(&argop[verify_argop], &va, 3929 OP_VERIFY, supp_attrs); 3930 if (e.error) { 3931 /* req time field(s) overflow - return */ 3932 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3933 needrecov); 3934 break; 3935 } 3936 } 3937 3938 doqueue = 1; 3939 3940 t = gethrtime(); 3941 3942 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3943 3944 /* 3945 * Purge the access cache and ACL cache if changing either the 3946 * owner of the file, the group owner, or the mode. These may 3947 * change the access permissions of the file, so purge old 3948 * information and start over again. 3949 */ 3950 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3951 (void) nfs4_access_purge_rp(rp); 3952 if (rp->r_secattr != NULL) { 3953 mutex_enter(&rp->r_statelock); 3954 vsp = rp->r_secattr; 3955 rp->r_secattr = NULL; 3956 mutex_exit(&rp->r_statelock); 3957 if (vsp != NULL) 3958 nfs4_acl_free_cache(vsp); 3959 } 3960 } 3961 3962 /* 3963 * If res.array_len == numops, then everything succeeded, 3964 * except for possibly the final getattr. If only the 3965 * last getattr failed, give up, and don't try recovery. 3966 */ 3967 if (res.array_len == numops) { 3968 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3969 needrecov); 3970 if (! e.error) 3971 resp = &res; 3972 break; 3973 } 3974 3975 /* 3976 * if either rpc call failed or completely succeeded - done 3977 */ 3978 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3979 if (e.error) { 3980 PURGE_ATTRCACHE4(vp); 3981 if (!needrecov) { 3982 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3983 needrecov); 3984 break; 3985 } 3986 } 3987 3988 /* 3989 * Do proper retry for OLD_STATEID outside of the normal 3990 * recovery framework. 3991 */ 3992 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3993 sid_types.cur_sid_type != SPEC_SID && 3994 sid_types.cur_sid_type != NO_SID) { 3995 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3996 needrecov); 3997 nfs4_save_stateid(&stateid, &sid_types); 3998 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3999 opsetattr.obj_attributes); 4000 if (verify_argop != -1) { 4001 nfs4args_verify_free(&argop[verify_argop]); 4002 verify_argop = -1; 4003 } 4004 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4005 goto recov_retry; 4006 } 4007 4008 if (needrecov) { 4009 bool_t abort; 4010 4011 abort = nfs4_start_recovery(&e, 4012 VTOMI4(vp), vp, NULL, NULL, NULL, 4013 OP_SETATTR, NULL, NULL, NULL); 4014 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4015 needrecov); 4016 /* 4017 * Do not retry if we failed with OLD_STATEID using 4018 * a special stateid. This is done to avoid looping 4019 * with a broken server. 4020 */ 4021 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 4022 (sid_types.cur_sid_type == SPEC_SID || 4023 sid_types.cur_sid_type == NO_SID)) 4024 abort = TRUE; 4025 if (!e.error) { 4026 if (res.status == NFS4ERR_BADOWNER) 4027 nfs4_log_badowner(VTOMI4(vp), 4028 OP_SETATTR); 4029 4030 e.error = geterrno4(res.status); 4031 (void) xdr_free(xdr_COMPOUND4res_clnt, 4032 (caddr_t)&res); 4033 } 4034 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4035 opsetattr.obj_attributes); 4036 if (verify_argop != -1) { 4037 nfs4args_verify_free(&argop[verify_argop]); 4038 verify_argop = -1; 4039 } 4040 if (abort == FALSE) { 4041 /* 4042 * Need to retry all possible stateids in 4043 * case the recovery error wasn't stateid 4044 * related or the stateids have become 4045 * stale (server reboot). 4046 */ 4047 nfs4_init_stateid_types(&sid_types); 4048 goto recov_retry; 4049 } 4050 return (e.error); 4051 } 4052 4053 /* 4054 * Need to call nfs4_end_op before nfs4getattr to 4055 * avoid potential nfs4_start_op deadlock. See RFE 4056 * 4777612. Calls to nfs4_invalidate_pages() and 4057 * nfs4_purge_stale_fh() might also generate over the 4058 * wire calls which my cause nfs4_start_op() deadlock. 4059 */ 4060 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4061 4062 /* 4063 * Check to update lease. 4064 */ 4065 resp = &res; 4066 if (res.status == NFS4_OK) { 4067 break; 4068 } 4069 4070 /* 4071 * Check if verify failed to see if try again 4072 */ 4073 if ((verify_argop == -1) || (res.array_len != 3)) { 4074 /* 4075 * can't continue... 4076 */ 4077 if (res.status == NFS4ERR_BADOWNER) 4078 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 4079 4080 e.error = geterrno4(res.status); 4081 } else { 4082 /* 4083 * When the verify request fails, the client ctime is 4084 * not in sync with the server. This is the same as 4085 * the version 3 "not synchronized" error, and we 4086 * handle it in a similar manner (XXX do we need to???). 4087 * Use the ctime returned in the first getattr for 4088 * the input to the next verify. 4089 * If we couldn't get the attributes, then we give up 4090 * because we can't complete the operation as required. 4091 */ 4092 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 4093 } 4094 if (e.error) { 4095 PURGE_ATTRCACHE4(vp); 4096 nfs4_purge_stale_fh(e.error, vp, cr); 4097 } else { 4098 /* 4099 * retry with a new verify value 4100 */ 4101 ctime = garp->n4g_va.va_ctime; 4102 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4103 resp = NULL; 4104 } 4105 if (!e.error) { 4106 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4107 opsetattr.obj_attributes); 4108 if (verify_argop != -1) { 4109 nfs4args_verify_free(&argop[verify_argop]); 4110 verify_argop = -1; 4111 } 4112 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4113 goto do_again; 4114 } 4115 } while (!e.error); 4116 4117 if (e.error) { 4118 /* 4119 * If we are here, rfs4call has an irrecoverable error - return 4120 */ 4121 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4122 opsetattr.obj_attributes); 4123 if (verify_argop != -1) { 4124 nfs4args_verify_free(&argop[verify_argop]); 4125 verify_argop = -1; 4126 } 4127 if (resp) 4128 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4129 return (e.error); 4130 } 4131 4132 4133 4134 /* 4135 * If changing the size of the file, invalidate 4136 * any local cached data which is no longer part 4137 * of the file. We also possibly invalidate the 4138 * last page in the file. We could use 4139 * pvn_vpzero(), but this would mark the page as 4140 * modified and require it to be written back to 4141 * the server for no particularly good reason. 4142 * This way, if we access it, then we bring it 4143 * back in. A read should be cheaper than a 4144 * write. 4145 */ 4146 if (mask & AT_SIZE) { 4147 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4148 } 4149 4150 /* either no error or one of the postop getattr failed */ 4151 4152 /* 4153 * XXX Perform a simplified version of wcc checking. Instead of 4154 * have another getattr to get pre-op, just purge cache if 4155 * any of the ops prior to and including the getattr failed. 4156 * If the getattr succeeded then update the attrcache accordingly. 4157 */ 4158 4159 garp = NULL; 4160 if (res.status == NFS4_OK) { 4161 /* 4162 * Last getattr 4163 */ 4164 resop = &res.array[numops - 1]; 4165 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4166 } 4167 /* 4168 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4169 * rather than filling it. See the function itself for details. 4170 */ 4171 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4172 if (garp != NULL) { 4173 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4174 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4175 vs_ace4_destroy(&garp->n4g_vsa); 4176 } else { 4177 if (vsap != NULL) { 4178 /* 4179 * The ACL was supposed to be set and to be 4180 * returned in the last getattr of this 4181 * compound, but for some reason the getattr 4182 * result doesn't contain the ACL. In this 4183 * case, purge the ACL cache. 4184 */ 4185 if (rp->r_secattr != NULL) { 4186 mutex_enter(&rp->r_statelock); 4187 vsp = rp->r_secattr; 4188 rp->r_secattr = NULL; 4189 mutex_exit(&rp->r_statelock); 4190 if (vsp != NULL) 4191 nfs4_acl_free_cache(vsp); 4192 } 4193 } 4194 } 4195 } 4196 4197 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4198 /* 4199 * Set the size, rather than relying on getting it updated 4200 * via a GETATTR. With delegations the client tries to 4201 * suppress GETATTR calls. 4202 */ 4203 mutex_enter(&rp->r_statelock); 4204 rp->r_size = vap->va_size; 4205 mutex_exit(&rp->r_statelock); 4206 } 4207 4208 /* 4209 * Can free up request args and res 4210 */ 4211 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4212 opsetattr.obj_attributes); 4213 if (verify_argop != -1) { 4214 nfs4args_verify_free(&argop[verify_argop]); 4215 verify_argop = -1; 4216 } 4217 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4218 4219 /* 4220 * Some servers will change the mode to clear the setuid 4221 * and setgid bits when changing the uid or gid. The 4222 * client needs to compensate appropriately. 4223 */ 4224 if (mask & (AT_UID | AT_GID)) { 4225 int terror, do_setattr; 4226 4227 do_setattr = 0; 4228 va.va_mask = AT_MODE; 4229 terror = nfs4getattr(vp, &va, cr); 4230 if (!terror && 4231 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4232 (!(mask & AT_MODE) && va.va_mode != omode))) { 4233 va.va_mask = AT_MODE; 4234 if (mask & AT_MODE) { 4235 /* 4236 * We asked the mode to be changed and what 4237 * we just got from the server in getattr is 4238 * not what we wanted it to be, so set it now. 4239 */ 4240 va.va_mode = vap->va_mode; 4241 do_setattr = 1; 4242 } else { 4243 /* 4244 * We did not ask the mode to be changed, 4245 * Check to see that the server just cleared 4246 * I_SUID and I_GUID from it. If not then 4247 * set mode to omode with UID/GID cleared. 4248 */ 4249 if (nfs4_compare_modes(va.va_mode, omode)) { 4250 omode &= ~(S_ISUID|S_ISGID); 4251 va.va_mode = omode; 4252 do_setattr = 1; 4253 } 4254 } 4255 4256 if (do_setattr) 4257 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4258 } 4259 } 4260 4261 return (e.error); 4262 } 4263 4264 /* ARGSUSED */ 4265 static int 4266 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct) 4267 { 4268 COMPOUND4args_clnt args; 4269 COMPOUND4res_clnt res; 4270 int doqueue; 4271 uint32_t acc, resacc, argacc; 4272 rnode4_t *rp; 4273 cred_t *cred, *ncr, *ncrfree = NULL; 4274 nfs4_access_type_t cacc; 4275 int num_ops; 4276 nfs_argop4 argop[3]; 4277 nfs_resop4 *resop; 4278 bool_t needrecov = FALSE, do_getattr; 4279 nfs4_recov_state_t recov_state; 4280 int rpc_error; 4281 hrtime_t t; 4282 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4283 mntinfo4_t *mi = VTOMI4(vp); 4284 4285 if (nfs_zone() != mi->mi_zone) 4286 return (EIO); 4287 4288 acc = 0; 4289 if (mode & VREAD) 4290 acc |= ACCESS4_READ; 4291 if (mode & VWRITE) { 4292 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4293 return (EROFS); 4294 if (vp->v_type == VDIR) 4295 acc |= ACCESS4_DELETE; 4296 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4297 } 4298 if (mode & VEXEC) { 4299 if (vp->v_type == VDIR) 4300 acc |= ACCESS4_LOOKUP; 4301 else 4302 acc |= ACCESS4_EXECUTE; 4303 } 4304 4305 if (VTOR4(vp)->r_acache != NULL) { 4306 e.error = nfs4_validate_caches(vp, cr); 4307 if (e.error) 4308 return (e.error); 4309 } 4310 4311 rp = VTOR4(vp); 4312 if (vp->v_type == VDIR) 4313 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4314 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4315 else 4316 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4317 ACCESS4_EXECUTE; 4318 recov_state.rs_flags = 0; 4319 recov_state.rs_num_retry_despite_err = 0; 4320 4321 cred = cr; 4322 /* 4323 * ncr and ncrfree both initially 4324 * point to the memory area returned 4325 * by crnetadjust(); 4326 * ncrfree not NULL when exiting means 4327 * that we need to release it 4328 */ 4329 ncr = crnetadjust(cred); 4330 ncrfree = ncr; 4331 4332 tryagain: 4333 cacc = nfs4_access_check(rp, acc, cred); 4334 if (cacc == NFS4_ACCESS_ALLOWED) { 4335 if (ncrfree != NULL) 4336 crfree(ncrfree); 4337 return (0); 4338 } 4339 if (cacc == NFS4_ACCESS_DENIED) { 4340 /* 4341 * If the cred can be adjusted, try again 4342 * with the new cred. 4343 */ 4344 if (ncr != NULL) { 4345 cred = ncr; 4346 ncr = NULL; 4347 goto tryagain; 4348 } 4349 if (ncrfree != NULL) 4350 crfree(ncrfree); 4351 return (EACCES); 4352 } 4353 4354 recov_retry: 4355 /* 4356 * Don't take with r_statev4_lock here. r_deleg_type could 4357 * change as soon as lock is released. Since it is an int, 4358 * there is no atomicity issue. 4359 */ 4360 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4361 num_ops = do_getattr ? 3 : 2; 4362 4363 args.ctag = TAG_ACCESS; 4364 4365 args.array_len = num_ops; 4366 args.array = argop; 4367 4368 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4369 &recov_state, NULL)) { 4370 if (ncrfree != NULL) 4371 crfree(ncrfree); 4372 return (e.error); 4373 } 4374 4375 /* putfh target fh */ 4376 argop[0].argop = OP_CPUTFH; 4377 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4378 4379 /* access */ 4380 argop[1].argop = OP_ACCESS; 4381 argop[1].nfs_argop4_u.opaccess.access = argacc; 4382 4383 /* getattr */ 4384 if (do_getattr) { 4385 argop[2].argop = OP_GETATTR; 4386 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4387 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4388 } 4389 4390 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4391 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4392 rnode4info(VTOR4(vp)))); 4393 4394 doqueue = 1; 4395 t = gethrtime(); 4396 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4397 rpc_error = e.error; 4398 4399 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4400 if (needrecov) { 4401 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4402 "nfs4_access: initiating recovery\n")); 4403 4404 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4405 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) { 4406 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4407 &recov_state, needrecov); 4408 if (!e.error) 4409 (void) xdr_free(xdr_COMPOUND4res_clnt, 4410 (caddr_t)&res); 4411 goto recov_retry; 4412 } 4413 } 4414 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4415 4416 if (e.error) 4417 goto out; 4418 4419 if (res.status) { 4420 e.error = geterrno4(res.status); 4421 /* 4422 * This might generate over the wire calls throught 4423 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4424 * here to avoid a deadlock. 4425 */ 4426 nfs4_purge_stale_fh(e.error, vp, cr); 4427 goto out; 4428 } 4429 resop = &res.array[1]; /* access res */ 4430 4431 resacc = resop->nfs_resop4_u.opaccess.access; 4432 4433 if (do_getattr) { 4434 resop++; /* getattr res */ 4435 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4436 t, cr, FALSE, NULL); 4437 } 4438 4439 if (!e.error) { 4440 nfs4_access_cache(rp, argacc, resacc, cred); 4441 /* 4442 * we just cached results with cred; if cred is the 4443 * adjusted credentials from crnetadjust, we do not want 4444 * to release them before exiting: hence setting ncrfree 4445 * to NULL 4446 */ 4447 if (cred != cr) 4448 ncrfree = NULL; 4449 /* XXX check the supported bits too? */ 4450 if ((acc & resacc) != acc) { 4451 /* 4452 * The following code implements the semantic 4453 * that a setuid root program has *at least* the 4454 * permissions of the user that is running the 4455 * program. See rfs3call() for more portions 4456 * of the implementation of this functionality. 4457 */ 4458 /* XXX-LP */ 4459 if (ncr != NULL) { 4460 (void) xdr_free(xdr_COMPOUND4res_clnt, 4461 (caddr_t)&res); 4462 cred = ncr; 4463 ncr = NULL; 4464 goto tryagain; 4465 } 4466 e.error = EACCES; 4467 } 4468 } 4469 4470 out: 4471 if (!rpc_error) 4472 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4473 4474 if (ncrfree != NULL) 4475 crfree(ncrfree); 4476 4477 return (e.error); 4478 } 4479 4480 /* ARGSUSED */ 4481 static int 4482 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct) 4483 { 4484 COMPOUND4args_clnt args; 4485 COMPOUND4res_clnt res; 4486 int doqueue; 4487 rnode4_t *rp; 4488 nfs_argop4 argop[3]; 4489 nfs_resop4 *resop; 4490 READLINK4res *lr_res; 4491 nfs4_ga_res_t *garp; 4492 uint_t len; 4493 char *linkdata; 4494 bool_t needrecov = FALSE; 4495 nfs4_recov_state_t recov_state; 4496 hrtime_t t; 4497 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4498 4499 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4500 return (EIO); 4501 /* 4502 * Can't readlink anything other than a symbolic link. 4503 */ 4504 if (vp->v_type != VLNK) 4505 return (EINVAL); 4506 4507 rp = VTOR4(vp); 4508 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4509 e.error = nfs4_validate_caches(vp, cr); 4510 if (e.error) 4511 return (e.error); 4512 mutex_enter(&rp->r_statelock); 4513 if (rp->r_symlink.contents != NULL) { 4514 e.error = uiomove(rp->r_symlink.contents, 4515 rp->r_symlink.len, UIO_READ, uiop); 4516 mutex_exit(&rp->r_statelock); 4517 return (e.error); 4518 } 4519 mutex_exit(&rp->r_statelock); 4520 } 4521 recov_state.rs_flags = 0; 4522 recov_state.rs_num_retry_despite_err = 0; 4523 4524 recov_retry: 4525 args.array_len = 3; 4526 args.array = argop; 4527 args.ctag = TAG_READLINK; 4528 4529 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4530 if (e.error) { 4531 return (e.error); 4532 } 4533 4534 /* 0. putfh symlink fh */ 4535 argop[0].argop = OP_CPUTFH; 4536 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4537 4538 /* 1. readlink */ 4539 argop[1].argop = OP_READLINK; 4540 4541 /* 2. getattr */ 4542 argop[2].argop = OP_GETATTR; 4543 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4544 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4545 4546 doqueue = 1; 4547 4548 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4549 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4550 rnode4info(VTOR4(vp)))); 4551 4552 t = gethrtime(); 4553 4554 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4555 4556 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4557 if (needrecov) { 4558 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4559 "nfs4_readlink: initiating recovery\n")); 4560 4561 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4562 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) { 4563 if (!e.error) 4564 (void) xdr_free(xdr_COMPOUND4res_clnt, 4565 (caddr_t)&res); 4566 4567 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4568 needrecov); 4569 goto recov_retry; 4570 } 4571 } 4572 4573 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4574 4575 if (e.error) 4576 return (e.error); 4577 4578 /* 4579 * There is an path in the code below which calls 4580 * nfs4_purge_stale_fh(), which may generate otw calls through 4581 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4582 * here to avoid nfs4_start_op() deadlock. 4583 */ 4584 4585 if (res.status && (res.array_len < args.array_len)) { 4586 /* 4587 * either Putfh or Link failed 4588 */ 4589 e.error = geterrno4(res.status); 4590 nfs4_purge_stale_fh(e.error, vp, cr); 4591 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4592 return (e.error); 4593 } 4594 4595 resop = &res.array[1]; /* readlink res */ 4596 lr_res = &resop->nfs_resop4_u.opreadlink; 4597 4598 /* 4599 * treat symlink names as data 4600 */ 4601 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL); 4602 if (linkdata != NULL) { 4603 int uio_len = len - 1; 4604 /* len includes null byte, which we won't uiomove */ 4605 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4606 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4607 mutex_enter(&rp->r_statelock); 4608 if (rp->r_symlink.contents == NULL) { 4609 rp->r_symlink.contents = linkdata; 4610 rp->r_symlink.len = uio_len; 4611 rp->r_symlink.size = len; 4612 mutex_exit(&rp->r_statelock); 4613 } else { 4614 mutex_exit(&rp->r_statelock); 4615 kmem_free(linkdata, len); 4616 } 4617 } else { 4618 kmem_free(linkdata, len); 4619 } 4620 } 4621 if (res.status == NFS4_OK) { 4622 resop++; /* getattr res */ 4623 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4624 } 4625 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4626 4627 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4628 4629 /* 4630 * The over the wire error for attempting to readlink something 4631 * other than a symbolic link is ENXIO. However, we need to 4632 * return EINVAL instead of ENXIO, so we map it here. 4633 */ 4634 return (e.error == ENXIO ? EINVAL : e.error); 4635 } 4636 4637 /* 4638 * Flush local dirty pages to stable storage on the server. 4639 * 4640 * If FNODSYNC is specified, then there is nothing to do because 4641 * metadata changes are not cached on the client before being 4642 * sent to the server. 4643 */ 4644 /* ARGSUSED */ 4645 static int 4646 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 4647 { 4648 int error; 4649 4650 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4651 return (0); 4652 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4653 return (EIO); 4654 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4655 if (!error) 4656 error = VTOR4(vp)->r_error; 4657 return (error); 4658 } 4659 4660 /* 4661 * Weirdness: if the file was removed or the target of a rename 4662 * operation while it was open, it got renamed instead. Here we 4663 * remove the renamed file. 4664 */ 4665 /* ARGSUSED */ 4666 void 4667 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4668 { 4669 rnode4_t *rp; 4670 4671 ASSERT(vp != DNLC_NO_VNODE); 4672 4673 rp = VTOR4(vp); 4674 4675 if (IS_SHADOW(vp, rp)) { 4676 sv_inactive(vp); 4677 return; 4678 } 4679 4680 /* 4681 * If this is coming from the wrong zone, we let someone in the right 4682 * zone take care of it asynchronously. We can get here due to 4683 * VN_RELE() being called from pageout() or fsflush(). This call may 4684 * potentially turn into an expensive no-op if, for instance, v_count 4685 * gets incremented in the meantime, but it's still correct. 4686 */ 4687 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4688 nfs4_async_inactive(vp, cr); 4689 return; 4690 } 4691 4692 /* 4693 * Some of the cleanup steps might require over-the-wire 4694 * operations. Since VOP_INACTIVE can get called as a result of 4695 * other over-the-wire operations (e.g., an attribute cache update 4696 * can lead to a DNLC purge), doing those steps now would lead to a 4697 * nested call to the recovery framework, which can deadlock. So 4698 * do any over-the-wire cleanups asynchronously, in a separate 4699 * thread. 4700 */ 4701 4702 mutex_enter(&rp->r_os_lock); 4703 mutex_enter(&rp->r_statelock); 4704 mutex_enter(&rp->r_statev4_lock); 4705 4706 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4707 mutex_exit(&rp->r_statev4_lock); 4708 mutex_exit(&rp->r_statelock); 4709 mutex_exit(&rp->r_os_lock); 4710 nfs4_async_inactive(vp, cr); 4711 return; 4712 } 4713 4714 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4715 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4716 mutex_exit(&rp->r_statev4_lock); 4717 mutex_exit(&rp->r_statelock); 4718 mutex_exit(&rp->r_os_lock); 4719 nfs4_async_inactive(vp, cr); 4720 return; 4721 } 4722 4723 if (rp->r_unldvp != NULL) { 4724 mutex_exit(&rp->r_statev4_lock); 4725 mutex_exit(&rp->r_statelock); 4726 mutex_exit(&rp->r_os_lock); 4727 nfs4_async_inactive(vp, cr); 4728 return; 4729 } 4730 mutex_exit(&rp->r_statev4_lock); 4731 mutex_exit(&rp->r_statelock); 4732 mutex_exit(&rp->r_os_lock); 4733 4734 rp4_addfree(rp, cr); 4735 } 4736 4737 /* 4738 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4739 * various bits of state. The caller must not refer to vp after this call. 4740 */ 4741 4742 void 4743 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4744 { 4745 rnode4_t *rp = VTOR4(vp); 4746 nfs4_recov_state_t recov_state; 4747 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4748 vnode_t *unldvp; 4749 char *unlname; 4750 cred_t *unlcred; 4751 COMPOUND4args_clnt args; 4752 COMPOUND4res_clnt res, *resp; 4753 nfs_argop4 argop[2]; 4754 int doqueue; 4755 #ifdef DEBUG 4756 char *name; 4757 #endif 4758 4759 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4760 ASSERT(!IS_SHADOW(vp, rp)); 4761 4762 #ifdef DEBUG 4763 name = fn_name(VTOSV(vp)->sv_name); 4764 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4765 "release vnode %s", name)); 4766 kmem_free(name, MAXNAMELEN); 4767 #endif 4768 4769 if (vp->v_type == VREG) { 4770 bool_t recov_failed = FALSE; 4771 4772 e.error = nfs4close_all(vp, cr); 4773 if (e.error) { 4774 /* Check to see if recovery failed */ 4775 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4776 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4777 recov_failed = TRUE; 4778 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4779 if (!recov_failed) { 4780 mutex_enter(&rp->r_statelock); 4781 if (rp->r_flags & R4RECOVERR) 4782 recov_failed = TRUE; 4783 mutex_exit(&rp->r_statelock); 4784 } 4785 if (recov_failed) { 4786 NFS4_DEBUG(nfs4_client_recov_debug, 4787 (CE_NOTE, "nfs4_inactive_otw: " 4788 "close failed (recovery failure)")); 4789 } 4790 } 4791 } 4792 4793 redo: 4794 if (rp->r_unldvp == NULL) { 4795 rp4_addfree(rp, cr); 4796 return; 4797 } 4798 4799 /* 4800 * Save the vnode pointer for the directory where the 4801 * unlinked-open file got renamed, then set it to NULL 4802 * to prevent another thread from getting here before 4803 * we're done with the remove. While we have the 4804 * statelock, make local copies of the pertinent rnode 4805 * fields. If we weren't to do this in an atomic way, the 4806 * the unl* fields could become inconsistent with respect 4807 * to each other due to a race condition between this 4808 * code and nfs_remove(). See bug report 1034328. 4809 */ 4810 mutex_enter(&rp->r_statelock); 4811 if (rp->r_unldvp == NULL) { 4812 mutex_exit(&rp->r_statelock); 4813 rp4_addfree(rp, cr); 4814 return; 4815 } 4816 4817 unldvp = rp->r_unldvp; 4818 rp->r_unldvp = NULL; 4819 unlname = rp->r_unlname; 4820 rp->r_unlname = NULL; 4821 unlcred = rp->r_unlcred; 4822 rp->r_unlcred = NULL; 4823 mutex_exit(&rp->r_statelock); 4824 4825 /* 4826 * If there are any dirty pages left, then flush 4827 * them. This is unfortunate because they just 4828 * may get thrown away during the remove operation, 4829 * but we have to do this for correctness. 4830 */ 4831 if (nfs4_has_pages(vp) && 4832 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4833 ASSERT(vp->v_type != VCHR); 4834 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL); 4835 if (e.error) { 4836 mutex_enter(&rp->r_statelock); 4837 if (!rp->r_error) 4838 rp->r_error = e.error; 4839 mutex_exit(&rp->r_statelock); 4840 } 4841 } 4842 4843 recov_state.rs_flags = 0; 4844 recov_state.rs_num_retry_despite_err = 0; 4845 recov_retry_remove: 4846 /* 4847 * Do the remove operation on the renamed file 4848 */ 4849 args.ctag = TAG_INACTIVE; 4850 4851 /* 4852 * Remove ops: putfh dir; remove 4853 */ 4854 args.array_len = 2; 4855 args.array = argop; 4856 4857 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4858 if (e.error) { 4859 kmem_free(unlname, MAXNAMELEN); 4860 crfree(unlcred); 4861 VN_RELE(unldvp); 4862 /* 4863 * Try again; this time around r_unldvp will be NULL, so we'll 4864 * just call rp4_addfree() and return. 4865 */ 4866 goto redo; 4867 } 4868 4869 /* putfh directory */ 4870 argop[0].argop = OP_CPUTFH; 4871 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4872 4873 /* remove */ 4874 argop[1].argop = OP_CREMOVE; 4875 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4876 4877 doqueue = 1; 4878 resp = &res; 4879 4880 #if 0 /* notyet */ 4881 /* 4882 * Can't do this yet. We may be being called from 4883 * dnlc_purge_XXX while that routine is holding a 4884 * mutex lock to the nc_rele list. The calls to 4885 * nfs3_cache_wcc_data may result in calls to 4886 * dnlc_purge_XXX. This will result in a deadlock. 4887 */ 4888 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4889 if (e.error) { 4890 PURGE_ATTRCACHE4(unldvp); 4891 resp = NULL; 4892 } else if (res.status) { 4893 e.error = geterrno4(res.status); 4894 PURGE_ATTRCACHE4(unldvp); 4895 /* 4896 * This code is inactive right now 4897 * but if made active there should 4898 * be a nfs4_end_op() call before 4899 * nfs4_purge_stale_fh to avoid start_op() 4900 * deadlock. See BugId: 4948726 4901 */ 4902 nfs4_purge_stale_fh(error, unldvp, cr); 4903 } else { 4904 nfs_resop4 *resop; 4905 REMOVE4res *rm_res; 4906 4907 resop = &res.array[1]; 4908 rm_res = &resop->nfs_resop4_u.opremove; 4909 /* 4910 * Update directory cache attribute, 4911 * readdir and dnlc caches. 4912 */ 4913 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4914 } 4915 #else 4916 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4917 4918 PURGE_ATTRCACHE4(unldvp); 4919 #endif 4920 4921 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4922 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4923 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 4924 if (!e.error) 4925 (void) xdr_free(xdr_COMPOUND4res_clnt, 4926 (caddr_t)&res); 4927 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4928 &recov_state, TRUE); 4929 goto recov_retry_remove; 4930 } 4931 } 4932 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4933 4934 /* 4935 * Release stuff held for the remove 4936 */ 4937 VN_RELE(unldvp); 4938 if (!e.error && resp) 4939 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4940 4941 kmem_free(unlname, MAXNAMELEN); 4942 crfree(unlcred); 4943 goto redo; 4944 } 4945 4946 /* 4947 * Remote file system operations having to do with directory manipulation. 4948 */ 4949 /* ARGSUSED3 */ 4950 int 4951 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4952 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 4953 int *direntflags, pathname_t *realpnp) 4954 { 4955 int error; 4956 vnode_t *vp, *avp = NULL; 4957 rnode4_t *drp; 4958 4959 *vpp = NULL; 4960 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4961 return (EPERM); 4962 /* 4963 * if LOOKUP_XATTR, must replace dvp (object) with 4964 * object's attrdir before continuing with lookup 4965 */ 4966 if (flags & LOOKUP_XATTR) { 4967 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4968 if (error) 4969 return (error); 4970 4971 dvp = avp; 4972 4973 /* 4974 * If lookup is for "", just return dvp now. The attrdir 4975 * has already been activated (from nfs4lookup_xattr), and 4976 * the caller will RELE the original dvp -- not 4977 * the attrdir. So, set vpp and return. 4978 * Currently, when the LOOKUP_XATTR flag is 4979 * passed to VOP_LOOKUP, the name is always empty, and 4980 * shortcircuiting here avoids 3 unneeded lock/unlock 4981 * pairs. 4982 * 4983 * If a non-empty name was provided, then it is the 4984 * attribute name, and it will be looked up below. 4985 */ 4986 if (*nm == '\0') { 4987 *vpp = dvp; 4988 return (0); 4989 } 4990 4991 /* 4992 * The vfs layer never sends a name when asking for the 4993 * attrdir, so we should never get here (unless of course 4994 * name is passed at some time in future -- at which time 4995 * we'll blow up here). 4996 */ 4997 ASSERT(0); 4998 } 4999 5000 drp = VTOR4(dvp); 5001 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5002 return (EINTR); 5003 5004 error = nfs4lookup(dvp, nm, vpp, cr, 0); 5005 nfs_rw_exit(&drp->r_rwlock); 5006 5007 /* 5008 * If vnode is a device, create special vnode. 5009 */ 5010 if (!error && ISVDEV((*vpp)->v_type)) { 5011 vp = *vpp; 5012 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 5013 VN_RELE(vp); 5014 } 5015 5016 return (error); 5017 } 5018 5019 /* ARGSUSED */ 5020 static int 5021 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 5022 { 5023 int error; 5024 rnode4_t *drp; 5025 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 5026 mntinfo4_t *mi; 5027 5028 mi = VTOMI4(dvp); 5029 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) && 5030 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS)) 5031 return (EINVAL); 5032 5033 drp = VTOR4(dvp); 5034 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5035 return (EINTR); 5036 5037 mutex_enter(&drp->r_statelock); 5038 /* 5039 * If the server doesn't support xattrs just return EINVAL 5040 */ 5041 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 5042 mutex_exit(&drp->r_statelock); 5043 nfs_rw_exit(&drp->r_rwlock); 5044 return (EINVAL); 5045 } 5046 5047 /* 5048 * If there is a cached xattr directory entry, 5049 * use it as long as the attributes are valid. If the 5050 * attributes are not valid, take the simple approach and 5051 * free the cached value and re-fetch a new value. 5052 * 5053 * We don't negative entry cache for now, if we did we 5054 * would need to check if the file has changed on every 5055 * lookup. But xattrs don't exist very often and failing 5056 * an openattr is not much more expensive than and NVERIFY or GETATTR 5057 * so do an openattr over the wire for now. 5058 */ 5059 if (drp->r_xattr_dir != NULL) { 5060 if (ATTRCACHE4_VALID(dvp)) { 5061 VN_HOLD(drp->r_xattr_dir); 5062 *vpp = drp->r_xattr_dir; 5063 mutex_exit(&drp->r_statelock); 5064 nfs_rw_exit(&drp->r_rwlock); 5065 return (0); 5066 } 5067 VN_RELE(drp->r_xattr_dir); 5068 drp->r_xattr_dir = NULL; 5069 } 5070 mutex_exit(&drp->r_statelock); 5071 5072 error = nfs4openattr(dvp, vpp, cflag, cr); 5073 5074 nfs_rw_exit(&drp->r_rwlock); 5075 5076 return (error); 5077 } 5078 5079 static int 5080 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 5081 { 5082 int error; 5083 rnode4_t *drp; 5084 5085 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5086 5087 /* 5088 * If lookup is for "", just return dvp. Don't need 5089 * to send it over the wire, look it up in the dnlc, 5090 * or perform any access checks. 5091 */ 5092 if (*nm == '\0') { 5093 VN_HOLD(dvp); 5094 *vpp = dvp; 5095 return (0); 5096 } 5097 5098 /* 5099 * Can't do lookups in non-directories. 5100 */ 5101 if (dvp->v_type != VDIR) 5102 return (ENOTDIR); 5103 5104 /* 5105 * If lookup is for ".", just return dvp. Don't need 5106 * to send it over the wire or look it up in the dnlc, 5107 * just need to check access. 5108 */ 5109 if (nm[0] == '.' && nm[1] == '\0') { 5110 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5111 if (error) 5112 return (error); 5113 VN_HOLD(dvp); 5114 *vpp = dvp; 5115 return (0); 5116 } 5117 5118 drp = VTOR4(dvp); 5119 if (!(drp->r_flags & R4LOOKUP)) { 5120 mutex_enter(&drp->r_statelock); 5121 drp->r_flags |= R4LOOKUP; 5122 mutex_exit(&drp->r_statelock); 5123 } 5124 5125 *vpp = NULL; 5126 /* 5127 * Lookup this name in the DNLC. If there is no entry 5128 * lookup over the wire. 5129 */ 5130 if (!skipdnlc) 5131 *vpp = dnlc_lookup(dvp, nm); 5132 if (*vpp == NULL) { 5133 /* 5134 * We need to go over the wire to lookup the name. 5135 */ 5136 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5137 } 5138 5139 /* 5140 * We hit on the dnlc 5141 */ 5142 if (*vpp != DNLC_NO_VNODE || 5143 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5144 /* 5145 * But our attrs may not be valid. 5146 */ 5147 if (ATTRCACHE4_VALID(dvp)) { 5148 error = nfs4_waitfor_purge_complete(dvp); 5149 if (error) { 5150 VN_RELE(*vpp); 5151 *vpp = NULL; 5152 return (error); 5153 } 5154 5155 /* 5156 * If after the purge completes, check to make sure 5157 * our attrs are still valid. 5158 */ 5159 if (ATTRCACHE4_VALID(dvp)) { 5160 /* 5161 * If we waited for a purge we may have 5162 * lost our vnode so look it up again. 5163 */ 5164 VN_RELE(*vpp); 5165 *vpp = dnlc_lookup(dvp, nm); 5166 if (*vpp == NULL) 5167 return (nfs4lookupnew_otw(dvp, 5168 nm, vpp, cr)); 5169 5170 /* 5171 * The access cache should almost always hit 5172 */ 5173 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5174 5175 if (error) { 5176 VN_RELE(*vpp); 5177 *vpp = NULL; 5178 return (error); 5179 } 5180 if (*vpp == DNLC_NO_VNODE) { 5181 VN_RELE(*vpp); 5182 *vpp = NULL; 5183 return (ENOENT); 5184 } 5185 return (0); 5186 } 5187 } 5188 } 5189 5190 ASSERT(*vpp != NULL); 5191 5192 /* 5193 * We may have gotten here we have one of the following cases: 5194 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5195 * need to validate them. 5196 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5197 * must validate. 5198 * 5199 * Go to the server and check if the directory has changed, if 5200 * it hasn't we are done and can use the dnlc entry. 5201 */ 5202 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5203 } 5204 5205 /* 5206 * Go to the server and check if the directory has changed, if 5207 * it hasn't we are done and can use the dnlc entry. If it 5208 * has changed we get a new copy of its attributes and check 5209 * the access for VEXEC, then relookup the filename and 5210 * get its filehandle and attributes. 5211 * 5212 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5213 * if the NVERIFY failed we must 5214 * purge the caches 5215 * cache new attributes (will set r_time_attr_inval) 5216 * cache new access 5217 * recheck VEXEC access 5218 * add name to dnlc, possibly negative 5219 * if LOOKUP succeeded 5220 * cache new attributes 5221 * else 5222 * set a new r_time_attr_inval for dvp 5223 * check to make sure we have access 5224 * 5225 * The vpp returned is the vnode passed in if the directory is valid, 5226 * a new vnode if successful lookup, or NULL on error. 5227 */ 5228 static int 5229 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5230 { 5231 COMPOUND4args_clnt args; 5232 COMPOUND4res_clnt res; 5233 fattr4 *ver_fattr; 5234 fattr4_change dchange; 5235 int32_t *ptr; 5236 int argoplist_size = 7 * sizeof (nfs_argop4); 5237 nfs_argop4 *argop; 5238 int doqueue; 5239 mntinfo4_t *mi; 5240 nfs4_recov_state_t recov_state; 5241 hrtime_t t; 5242 int isdotdot; 5243 vnode_t *nvp; 5244 nfs_fh4 *fhp; 5245 nfs4_sharedfh_t *sfhp; 5246 nfs4_access_type_t cacc; 5247 rnode4_t *nrp; 5248 rnode4_t *drp = VTOR4(dvp); 5249 nfs4_ga_res_t *garp = NULL; 5250 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5251 5252 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5253 ASSERT(nm != NULL); 5254 ASSERT(nm[0] != '\0'); 5255 ASSERT(dvp->v_type == VDIR); 5256 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5257 ASSERT(*vpp != NULL); 5258 5259 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5260 isdotdot = 1; 5261 args.ctag = TAG_LOOKUP_VPARENT; 5262 } else { 5263 /* 5264 * If dvp were a stub, it should have triggered and caused 5265 * a mount for us to get this far. 5266 */ 5267 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5268 5269 isdotdot = 0; 5270 args.ctag = TAG_LOOKUP_VALID; 5271 } 5272 5273 mi = VTOMI4(dvp); 5274 recov_state.rs_flags = 0; 5275 recov_state.rs_num_retry_despite_err = 0; 5276 5277 nvp = NULL; 5278 5279 /* Save the original mount point security information */ 5280 (void) save_mnt_secinfo(mi->mi_curr_serv); 5281 5282 recov_retry: 5283 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5284 &recov_state, NULL); 5285 if (e.error) { 5286 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5287 VN_RELE(*vpp); 5288 *vpp = NULL; 5289 return (e.error); 5290 } 5291 5292 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5293 5294 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5295 args.array_len = 7; 5296 args.array = argop; 5297 5298 /* 0. putfh file */ 5299 argop[0].argop = OP_CPUTFH; 5300 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5301 5302 /* 1. nverify the change info */ 5303 argop[1].argop = OP_NVERIFY; 5304 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5305 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5306 ver_fattr->attrlist4 = (char *)&dchange; 5307 ptr = (int32_t *)&dchange; 5308 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5309 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5310 5311 /* 2. getattr directory */ 5312 argop[2].argop = OP_GETATTR; 5313 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5314 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5315 5316 /* 3. access directory */ 5317 argop[3].argop = OP_ACCESS; 5318 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5319 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5320 5321 /* 4. lookup name */ 5322 if (isdotdot) { 5323 argop[4].argop = OP_LOOKUPP; 5324 } else { 5325 argop[4].argop = OP_CLOOKUP; 5326 argop[4].nfs_argop4_u.opclookup.cname = nm; 5327 } 5328 5329 /* 5. resulting file handle */ 5330 argop[5].argop = OP_GETFH; 5331 5332 /* 6. resulting file attributes */ 5333 argop[6].argop = OP_GETATTR; 5334 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5335 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5336 5337 doqueue = 1; 5338 t = gethrtime(); 5339 5340 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5341 5342 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5343 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5344 if (e.error != 0 && *vpp != NULL) 5345 VN_RELE(*vpp); 5346 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5347 &recov_state, FALSE); 5348 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5349 kmem_free(argop, argoplist_size); 5350 return (e.error); 5351 } 5352 5353 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5354 /* 5355 * For WRONGSEC of a non-dotdot case, send secinfo directly 5356 * from this thread, do not go thru the recovery thread since 5357 * we need the nm information. 5358 * 5359 * Not doing dotdot case because there is no specification 5360 * for (PUTFH, SECINFO "..") yet. 5361 */ 5362 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5363 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5364 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5365 &recov_state, FALSE); 5366 else 5367 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5368 &recov_state, TRUE); 5369 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5370 kmem_free(argop, argoplist_size); 5371 if (!e.error) 5372 goto recov_retry; 5373 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5374 VN_RELE(*vpp); 5375 *vpp = NULL; 5376 return (e.error); 5377 } 5378 5379 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5380 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5381 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5382 &recov_state, TRUE); 5383 5384 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5385 kmem_free(argop, argoplist_size); 5386 goto recov_retry; 5387 } 5388 } 5389 5390 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5391 5392 if (e.error || res.array_len == 0) { 5393 /* 5394 * If e.error isn't set, then reply has no ops (or we couldn't 5395 * be here). The only legal way to reply without an op array 5396 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5397 * be in the reply for all other status values. 5398 * 5399 * For valid replies without an ops array, return ENOTSUP 5400 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5401 * return EIO -- don't trust status. 5402 */ 5403 if (e.error == 0) 5404 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5405 ENOTSUP : EIO; 5406 VN_RELE(*vpp); 5407 *vpp = NULL; 5408 kmem_free(argop, argoplist_size); 5409 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5410 return (e.error); 5411 } 5412 5413 if (res.status != NFS4ERR_SAME) { 5414 e.error = geterrno4(res.status); 5415 5416 /* 5417 * The NVERIFY "failed" so the directory has changed 5418 * First make sure PUTFH succeeded and NVERIFY "failed" 5419 * cleanly. 5420 */ 5421 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5422 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5423 nfs4_purge_stale_fh(e.error, dvp, cr); 5424 VN_RELE(*vpp); 5425 *vpp = NULL; 5426 goto exit; 5427 } 5428 5429 /* 5430 * We know the NVERIFY "failed" so we must: 5431 * purge the caches (access and indirectly dnlc if needed) 5432 */ 5433 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5434 5435 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5436 nfs4_purge_stale_fh(e.error, dvp, cr); 5437 VN_RELE(*vpp); 5438 *vpp = NULL; 5439 goto exit; 5440 } 5441 5442 /* 5443 * Install new cached attributes for the directory 5444 */ 5445 nfs4_attr_cache(dvp, 5446 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5447 t, cr, FALSE, NULL); 5448 5449 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5450 nfs4_purge_stale_fh(e.error, dvp, cr); 5451 VN_RELE(*vpp); 5452 *vpp = NULL; 5453 e.error = geterrno4(res.status); 5454 goto exit; 5455 } 5456 5457 /* 5458 * Now we know the directory is valid, 5459 * cache new directory access 5460 */ 5461 nfs4_access_cache(drp, 5462 args.array[3].nfs_argop4_u.opaccess.access, 5463 res.array[3].nfs_resop4_u.opaccess.access, cr); 5464 5465 /* 5466 * recheck VEXEC access 5467 */ 5468 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5469 if (cacc != NFS4_ACCESS_ALLOWED) { 5470 /* 5471 * Directory permissions might have been revoked 5472 */ 5473 if (cacc == NFS4_ACCESS_DENIED) { 5474 e.error = EACCES; 5475 VN_RELE(*vpp); 5476 *vpp = NULL; 5477 goto exit; 5478 } 5479 5480 /* 5481 * Somehow we must not have asked for enough 5482 * so try a singleton ACCESS, should never happen. 5483 */ 5484 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5485 if (e.error) { 5486 VN_RELE(*vpp); 5487 *vpp = NULL; 5488 goto exit; 5489 } 5490 } 5491 5492 e.error = geterrno4(res.status); 5493 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5494 /* 5495 * The lookup failed, probably no entry 5496 */ 5497 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5498 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5499 } else { 5500 /* 5501 * Might be some other error, so remove 5502 * the dnlc entry to make sure we start all 5503 * over again, next time. 5504 */ 5505 dnlc_remove(dvp, nm); 5506 } 5507 VN_RELE(*vpp); 5508 *vpp = NULL; 5509 goto exit; 5510 } 5511 5512 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5513 /* 5514 * The file exists but we can't get its fh for 5515 * some unknown reason. Remove it from the dnlc 5516 * and error out to be safe. 5517 */ 5518 dnlc_remove(dvp, nm); 5519 VN_RELE(*vpp); 5520 *vpp = NULL; 5521 goto exit; 5522 } 5523 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5524 if (fhp->nfs_fh4_len == 0) { 5525 /* 5526 * The file exists but a bogus fh 5527 * some unknown reason. Remove it from the dnlc 5528 * and error out to be safe. 5529 */ 5530 e.error = ENOENT; 5531 dnlc_remove(dvp, nm); 5532 VN_RELE(*vpp); 5533 *vpp = NULL; 5534 goto exit; 5535 } 5536 sfhp = sfh4_get(fhp, mi); 5537 5538 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5539 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5540 5541 /* 5542 * Make the new rnode 5543 */ 5544 if (isdotdot) { 5545 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5546 if (e.error) { 5547 sfh4_rele(&sfhp); 5548 VN_RELE(*vpp); 5549 *vpp = NULL; 5550 goto exit; 5551 } 5552 /* 5553 * XXX if nfs4_make_dotdot uses an existing rnode 5554 * XXX it doesn't update the attributes. 5555 * XXX for now just save them again to save an OTW 5556 */ 5557 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5558 } else { 5559 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5560 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 5561 /* 5562 * If v_type == VNON, then garp was NULL because 5563 * the last op in the compound failed and makenfs4node 5564 * could not find the vnode for sfhp. It created 5565 * a new vnode, so we have nothing to purge here. 5566 */ 5567 if (nvp->v_type == VNON) { 5568 vattr_t vattr; 5569 5570 vattr.va_mask = AT_TYPE; 5571 /* 5572 * N.B. We've already called nfs4_end_fop above. 5573 */ 5574 e.error = nfs4getattr(nvp, &vattr, cr); 5575 if (e.error) { 5576 sfh4_rele(&sfhp); 5577 VN_RELE(*vpp); 5578 *vpp = NULL; 5579 VN_RELE(nvp); 5580 goto exit; 5581 } 5582 nvp->v_type = vattr.va_type; 5583 } 5584 } 5585 sfh4_rele(&sfhp); 5586 5587 nrp = VTOR4(nvp); 5588 mutex_enter(&nrp->r_statev4_lock); 5589 if (!nrp->created_v4) { 5590 mutex_exit(&nrp->r_statev4_lock); 5591 dnlc_update(dvp, nm, nvp); 5592 } else 5593 mutex_exit(&nrp->r_statev4_lock); 5594 5595 VN_RELE(*vpp); 5596 *vpp = nvp; 5597 } else { 5598 hrtime_t now; 5599 hrtime_t delta = 0; 5600 5601 e.error = 0; 5602 5603 /* 5604 * Because the NVERIFY "succeeded" we know that the 5605 * directory attributes are still valid 5606 * so update r_time_attr_inval 5607 */ 5608 now = gethrtime(); 5609 mutex_enter(&drp->r_statelock); 5610 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5611 delta = now - drp->r_time_attr_saved; 5612 if (delta < mi->mi_acdirmin) 5613 delta = mi->mi_acdirmin; 5614 else if (delta > mi->mi_acdirmax) 5615 delta = mi->mi_acdirmax; 5616 } 5617 drp->r_time_attr_inval = now + delta; 5618 mutex_exit(&drp->r_statelock); 5619 dnlc_update(dvp, nm, *vpp); 5620 5621 /* 5622 * Even though we have a valid directory attr cache 5623 * and dnlc entry, we may not have access. 5624 * This should almost always hit the cache. 5625 */ 5626 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5627 if (e.error) { 5628 VN_RELE(*vpp); 5629 *vpp = NULL; 5630 } 5631 5632 if (*vpp == DNLC_NO_VNODE) { 5633 VN_RELE(*vpp); 5634 *vpp = NULL; 5635 e.error = ENOENT; 5636 } 5637 } 5638 5639 exit: 5640 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5641 kmem_free(argop, argoplist_size); 5642 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5643 return (e.error); 5644 } 5645 5646 /* 5647 * We need to go over the wire to lookup the name, but 5648 * while we are there verify the directory has not 5649 * changed but if it has, get new attributes and check access 5650 * 5651 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5652 * NVERIFY GETATTR ACCESS 5653 * 5654 * With the results: 5655 * if the NVERIFY failed we must purge the caches, add new attributes, 5656 * and cache new access. 5657 * set a new r_time_attr_inval 5658 * add name to dnlc, possibly negative 5659 * if LOOKUP succeeded 5660 * cache new attributes 5661 */ 5662 static int 5663 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5664 { 5665 COMPOUND4args_clnt args; 5666 COMPOUND4res_clnt res; 5667 fattr4 *ver_fattr; 5668 fattr4_change dchange; 5669 int32_t *ptr; 5670 nfs4_ga_res_t *garp = NULL; 5671 int argoplist_size = 9 * sizeof (nfs_argop4); 5672 nfs_argop4 *argop; 5673 int doqueue; 5674 mntinfo4_t *mi; 5675 nfs4_recov_state_t recov_state; 5676 hrtime_t t; 5677 int isdotdot; 5678 vnode_t *nvp; 5679 nfs_fh4 *fhp; 5680 nfs4_sharedfh_t *sfhp; 5681 nfs4_access_type_t cacc; 5682 rnode4_t *nrp; 5683 rnode4_t *drp = VTOR4(dvp); 5684 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5685 5686 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5687 ASSERT(nm != NULL); 5688 ASSERT(nm[0] != '\0'); 5689 ASSERT(dvp->v_type == VDIR); 5690 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5691 ASSERT(*vpp == NULL); 5692 5693 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5694 isdotdot = 1; 5695 args.ctag = TAG_LOOKUP_PARENT; 5696 } else { 5697 /* 5698 * If dvp were a stub, it should have triggered and caused 5699 * a mount for us to get this far. 5700 */ 5701 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5702 5703 isdotdot = 0; 5704 args.ctag = TAG_LOOKUP; 5705 } 5706 5707 mi = VTOMI4(dvp); 5708 recov_state.rs_flags = 0; 5709 recov_state.rs_num_retry_despite_err = 0; 5710 5711 nvp = NULL; 5712 5713 /* Save the original mount point security information */ 5714 (void) save_mnt_secinfo(mi->mi_curr_serv); 5715 5716 recov_retry: 5717 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5718 &recov_state, NULL); 5719 if (e.error) { 5720 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5721 return (e.error); 5722 } 5723 5724 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5725 5726 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5727 args.array_len = 9; 5728 args.array = argop; 5729 5730 /* 0. putfh file */ 5731 argop[0].argop = OP_CPUTFH; 5732 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5733 5734 /* 1. savefh for the nverify */ 5735 argop[1].argop = OP_SAVEFH; 5736 5737 /* 2. lookup name */ 5738 if (isdotdot) { 5739 argop[2].argop = OP_LOOKUPP; 5740 } else { 5741 argop[2].argop = OP_CLOOKUP; 5742 argop[2].nfs_argop4_u.opclookup.cname = nm; 5743 } 5744 5745 /* 3. resulting file handle */ 5746 argop[3].argop = OP_GETFH; 5747 5748 /* 4. resulting file attributes */ 5749 argop[4].argop = OP_GETATTR; 5750 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5751 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5752 5753 /* 5. restorefh back the directory for the nverify */ 5754 argop[5].argop = OP_RESTOREFH; 5755 5756 /* 6. nverify the change info */ 5757 argop[6].argop = OP_NVERIFY; 5758 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5759 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5760 ver_fattr->attrlist4 = (char *)&dchange; 5761 ptr = (int32_t *)&dchange; 5762 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5763 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5764 5765 /* 7. getattr directory */ 5766 argop[7].argop = OP_GETATTR; 5767 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5768 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5769 5770 /* 8. access directory */ 5771 argop[8].argop = OP_ACCESS; 5772 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5773 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5774 5775 doqueue = 1; 5776 t = gethrtime(); 5777 5778 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5779 5780 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5781 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5782 if (e.error != 0 && *vpp != NULL) 5783 VN_RELE(*vpp); 5784 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5785 &recov_state, FALSE); 5786 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5787 kmem_free(argop, argoplist_size); 5788 return (e.error); 5789 } 5790 5791 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5792 /* 5793 * For WRONGSEC of a non-dotdot case, send secinfo directly 5794 * from this thread, do not go thru the recovery thread since 5795 * we need the nm information. 5796 * 5797 * Not doing dotdot case because there is no specification 5798 * for (PUTFH, SECINFO "..") yet. 5799 */ 5800 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5801 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5802 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5803 &recov_state, FALSE); 5804 else 5805 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5806 &recov_state, TRUE); 5807 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5808 kmem_free(argop, argoplist_size); 5809 if (!e.error) 5810 goto recov_retry; 5811 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5812 return (e.error); 5813 } 5814 5815 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5816 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5817 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5818 &recov_state, TRUE); 5819 5820 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5821 kmem_free(argop, argoplist_size); 5822 goto recov_retry; 5823 } 5824 } 5825 5826 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5827 5828 if (e.error || res.array_len == 0) { 5829 /* 5830 * If e.error isn't set, then reply has no ops (or we couldn't 5831 * be here). The only legal way to reply without an op array 5832 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5833 * be in the reply for all other status values. 5834 * 5835 * For valid replies without an ops array, return ENOTSUP 5836 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5837 * return EIO -- don't trust status. 5838 */ 5839 if (e.error == 0) 5840 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5841 ENOTSUP : EIO; 5842 5843 kmem_free(argop, argoplist_size); 5844 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5845 return (e.error); 5846 } 5847 5848 e.error = geterrno4(res.status); 5849 5850 /* 5851 * The PUTFH and SAVEFH may have failed. 5852 */ 5853 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5854 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5855 nfs4_purge_stale_fh(e.error, dvp, cr); 5856 goto exit; 5857 } 5858 5859 /* 5860 * Check if the file exists, if it does delay entering 5861 * into the dnlc until after we update the directory 5862 * attributes so we don't cause it to get purged immediately. 5863 */ 5864 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5865 /* 5866 * The lookup failed, probably no entry 5867 */ 5868 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5869 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5870 goto exit; 5871 } 5872 5873 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5874 /* 5875 * The file exists but we can't get its fh for 5876 * some unknown reason. Error out to be safe. 5877 */ 5878 goto exit; 5879 } 5880 5881 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5882 if (fhp->nfs_fh4_len == 0) { 5883 /* 5884 * The file exists but a bogus fh 5885 * some unknown reason. Error out to be safe. 5886 */ 5887 e.error = EIO; 5888 goto exit; 5889 } 5890 sfhp = sfh4_get(fhp, mi); 5891 5892 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5893 sfh4_rele(&sfhp); 5894 goto exit; 5895 } 5896 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5897 5898 /* 5899 * The RESTOREFH may have failed 5900 */ 5901 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5902 sfh4_rele(&sfhp); 5903 e.error = EIO; 5904 goto exit; 5905 } 5906 5907 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5908 /* 5909 * First make sure the NVERIFY failed as we expected, 5910 * if it didn't then be conservative and error out 5911 * as we can't trust the directory. 5912 */ 5913 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5914 sfh4_rele(&sfhp); 5915 e.error = EIO; 5916 goto exit; 5917 } 5918 5919 /* 5920 * We know the NVERIFY "failed" so the directory has changed, 5921 * so we must: 5922 * purge the caches (access and indirectly dnlc if needed) 5923 */ 5924 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5925 5926 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5927 sfh4_rele(&sfhp); 5928 goto exit; 5929 } 5930 nfs4_attr_cache(dvp, 5931 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5932 t, cr, FALSE, NULL); 5933 5934 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5935 nfs4_purge_stale_fh(e.error, dvp, cr); 5936 sfh4_rele(&sfhp); 5937 e.error = geterrno4(res.status); 5938 goto exit; 5939 } 5940 5941 /* 5942 * Now we know the directory is valid, 5943 * cache new directory access 5944 */ 5945 nfs4_access_cache(drp, 5946 args.array[8].nfs_argop4_u.opaccess.access, 5947 res.array[8].nfs_resop4_u.opaccess.access, cr); 5948 5949 /* 5950 * recheck VEXEC access 5951 */ 5952 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5953 if (cacc != NFS4_ACCESS_ALLOWED) { 5954 /* 5955 * Directory permissions might have been revoked 5956 */ 5957 if (cacc == NFS4_ACCESS_DENIED) { 5958 sfh4_rele(&sfhp); 5959 e.error = EACCES; 5960 goto exit; 5961 } 5962 5963 /* 5964 * Somehow we must not have asked for enough 5965 * so try a singleton ACCESS should never happen 5966 */ 5967 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5968 if (e.error) { 5969 sfh4_rele(&sfhp); 5970 goto exit; 5971 } 5972 } 5973 5974 e.error = geterrno4(res.status); 5975 } else { 5976 hrtime_t now; 5977 hrtime_t delta = 0; 5978 5979 e.error = 0; 5980 5981 /* 5982 * Because the NVERIFY "succeeded" we know that the 5983 * directory attributes are still valid 5984 * so update r_time_attr_inval 5985 */ 5986 now = gethrtime(); 5987 mutex_enter(&drp->r_statelock); 5988 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5989 delta = now - drp->r_time_attr_saved; 5990 if (delta < mi->mi_acdirmin) 5991 delta = mi->mi_acdirmin; 5992 else if (delta > mi->mi_acdirmax) 5993 delta = mi->mi_acdirmax; 5994 } 5995 drp->r_time_attr_inval = now + delta; 5996 mutex_exit(&drp->r_statelock); 5997 5998 /* 5999 * Even though we have a valid directory attr cache, 6000 * we may not have access. 6001 * This should almost always hit the cache. 6002 */ 6003 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 6004 if (e.error) { 6005 sfh4_rele(&sfhp); 6006 goto exit; 6007 } 6008 } 6009 6010 /* 6011 * Now we have successfully completed the lookup, if the 6012 * directory has changed we now have the valid attributes. 6013 * We also know we have directory access. 6014 * Create the new rnode and insert it in the dnlc. 6015 */ 6016 if (isdotdot) { 6017 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 6018 if (e.error) { 6019 sfh4_rele(&sfhp); 6020 goto exit; 6021 } 6022 /* 6023 * XXX if nfs4_make_dotdot uses an existing rnode 6024 * XXX it doesn't update the attributes. 6025 * XXX for now just save them again to save an OTW 6026 */ 6027 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 6028 } else { 6029 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 6030 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 6031 } 6032 sfh4_rele(&sfhp); 6033 6034 nrp = VTOR4(nvp); 6035 mutex_enter(&nrp->r_statev4_lock); 6036 if (!nrp->created_v4) { 6037 mutex_exit(&nrp->r_statev4_lock); 6038 dnlc_update(dvp, nm, nvp); 6039 } else 6040 mutex_exit(&nrp->r_statev4_lock); 6041 6042 *vpp = nvp; 6043 6044 exit: 6045 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6046 kmem_free(argop, argoplist_size); 6047 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 6048 return (e.error); 6049 } 6050 6051 #ifdef DEBUG 6052 void 6053 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 6054 { 6055 uint_t i, len; 6056 zoneid_t zoneid = getzoneid(); 6057 char *s; 6058 6059 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 6060 for (i = 0; i < argcnt; i++) { 6061 nfs_argop4 *op = &argbase[i]; 6062 switch (op->argop) { 6063 case OP_CPUTFH: 6064 case OP_PUTFH: 6065 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 6066 break; 6067 case OP_PUTROOTFH: 6068 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 6069 break; 6070 case OP_CLOOKUP: 6071 s = op->nfs_argop4_u.opclookup.cname; 6072 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6073 break; 6074 case OP_LOOKUP: 6075 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 6076 &len, NULL); 6077 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6078 kmem_free(s, len); 6079 break; 6080 case OP_LOOKUPP: 6081 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 6082 break; 6083 case OP_GETFH: 6084 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 6085 break; 6086 case OP_GETATTR: 6087 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 6088 break; 6089 case OP_OPENATTR: 6090 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 6091 break; 6092 default: 6093 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 6094 op->argop); 6095 break; 6096 } 6097 } 6098 } 6099 #endif 6100 6101 /* 6102 * nfs4lookup_setup - constructs a multi-lookup compound request. 6103 * 6104 * Given the path "nm1/nm2/.../nmn", the following compound requests 6105 * may be created: 6106 * 6107 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 6108 * is faster, for now. 6109 * 6110 * l4_getattrs indicates the type of compound requested. 6111 * 6112 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 6113 * 6114 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 6115 * 6116 * total number of ops is n + 1. 6117 * 6118 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 6119 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 6120 * before the last component, and only get attributes 6121 * for the last component. Note that the second-to-last 6122 * pathname component is XATTR_RPATH, which does NOT go 6123 * over-the-wire as a lookup. 6124 * 6125 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 6126 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 6127 * 6128 * and total number of ops is n + 5. 6129 * 6130 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 6131 * attribute directory: create lookups plus an OPENATTR 6132 * replacing the last lookup. Note that the last pathname 6133 * component is XATTR_RPATH, which does NOT go over-the-wire 6134 * as a lookup. 6135 * 6136 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 6137 * Openattr; Getfh; Getattr } 6138 * 6139 * and total number of ops is n + 5. 6140 * 6141 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6142 * nodes too. 6143 * 6144 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6145 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6146 * 6147 * and total number of ops is 3*n + 1. 6148 * 6149 * All cases: returns the index in the arg array of the final LOOKUP op, or 6150 * -1 if no LOOKUPs were used. 6151 */ 6152 int 6153 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6154 { 6155 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6156 nfs_argop4 *argbase, *argop; 6157 int arglen, argcnt; 6158 int n = 1; /* number of components */ 6159 int nga = 1; /* number of Getattr's in request */ 6160 char c = '\0', *s, *p; 6161 int lookup_idx = -1; 6162 int argoplist_size; 6163 6164 /* set lookuparg response result to 0 */ 6165 lookupargp->resp->status = NFS4_OK; 6166 6167 /* skip leading "/" or "." e.g. ".//./" if there is */ 6168 for (; ; nm++) { 6169 if (*nm != '/' && *nm != '.') 6170 break; 6171 6172 /* ".." is counted as 1 component */ 6173 if (*nm == '.' && *(nm + 1) != '/') 6174 break; 6175 } 6176 6177 /* 6178 * Find n = number of components - nm must be null terminated 6179 * Skip "." components. 6180 */ 6181 if (*nm != '\0') 6182 for (n = 1, s = nm; *s != '\0'; s++) { 6183 if ((*s == '/') && (*(s + 1) != '/') && 6184 (*(s + 1) != '\0') && 6185 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6186 *(s + 2) == '\0'))) 6187 n++; 6188 } 6189 else 6190 n = 0; 6191 6192 /* 6193 * nga is number of components that need Getfh+Getattr 6194 */ 6195 switch (l4_getattrs) { 6196 case LKP4_NO_ATTRIBUTES: 6197 nga = 0; 6198 break; 6199 case LKP4_ALL_ATTRIBUTES: 6200 nga = n; 6201 /* 6202 * Always have at least 1 getfh, getattr pair 6203 */ 6204 if (nga == 0) 6205 nga++; 6206 break; 6207 case LKP4_LAST_ATTRDIR: 6208 case LKP4_LAST_NAMED_ATTR: 6209 nga = n+1; 6210 break; 6211 } 6212 6213 /* 6214 * If change to use the filehandle attr instead of getfh 6215 * the following line can be deleted. 6216 */ 6217 nga *= 2; 6218 6219 /* 6220 * calculate number of ops in request as 6221 * header + trailer + lookups + getattrs 6222 */ 6223 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6224 6225 argoplist_size = arglen * sizeof (nfs_argop4); 6226 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6227 lookupargp->argsp->array = argop; 6228 6229 argcnt = lookupargp->header_len; 6230 argop += argcnt; 6231 6232 /* 6233 * loop and create a lookup op and possibly getattr/getfh for 6234 * each component. Skip "." components. 6235 */ 6236 for (s = nm; *s != '\0'; s = p) { 6237 /* 6238 * Set up a pathname struct for each component if needed 6239 */ 6240 while (*s == '/') 6241 s++; 6242 if (*s == '\0') 6243 break; 6244 6245 for (p = s; (*p != '/') && (*p != '\0'); p++) 6246 ; 6247 c = *p; 6248 *p = '\0'; 6249 6250 if (s[0] == '.' && s[1] == '\0') { 6251 *p = c; 6252 continue; 6253 } 6254 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6255 strcmp(s, XATTR_RPATH) == 0) { 6256 /* getfh XXX may not be needed in future */ 6257 argop->argop = OP_GETFH; 6258 argop++; 6259 argcnt++; 6260 6261 /* getattr */ 6262 argop->argop = OP_GETATTR; 6263 argop->nfs_argop4_u.opgetattr.attr_request = 6264 lookupargp->ga_bits; 6265 argop->nfs_argop4_u.opgetattr.mi = 6266 lookupargp->mi; 6267 argop++; 6268 argcnt++; 6269 6270 /* openattr */ 6271 argop->argop = OP_OPENATTR; 6272 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6273 strcmp(s, XATTR_RPATH) == 0) { 6274 /* openattr */ 6275 argop->argop = OP_OPENATTR; 6276 argop++; 6277 argcnt++; 6278 6279 /* getfh XXX may not be needed in future */ 6280 argop->argop = OP_GETFH; 6281 argop++; 6282 argcnt++; 6283 6284 /* getattr */ 6285 argop->argop = OP_GETATTR; 6286 argop->nfs_argop4_u.opgetattr.attr_request = 6287 lookupargp->ga_bits; 6288 argop->nfs_argop4_u.opgetattr.mi = 6289 lookupargp->mi; 6290 argop++; 6291 argcnt++; 6292 *p = c; 6293 continue; 6294 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6295 /* lookupp */ 6296 argop->argop = OP_LOOKUPP; 6297 } else { 6298 /* lookup */ 6299 argop->argop = OP_LOOKUP; 6300 (void) str_to_utf8(s, 6301 &argop->nfs_argop4_u.oplookup.objname); 6302 } 6303 lookup_idx = argcnt; 6304 argop++; 6305 argcnt++; 6306 6307 *p = c; 6308 6309 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6310 /* getfh XXX may not be needed in future */ 6311 argop->argop = OP_GETFH; 6312 argop++; 6313 argcnt++; 6314 6315 /* getattr */ 6316 argop->argop = OP_GETATTR; 6317 argop->nfs_argop4_u.opgetattr.attr_request = 6318 lookupargp->ga_bits; 6319 argop->nfs_argop4_u.opgetattr.mi = 6320 lookupargp->mi; 6321 argop++; 6322 argcnt++; 6323 } 6324 } 6325 6326 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6327 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6328 if (needgetfh) { 6329 /* stick in a post-lookup getfh */ 6330 argop->argop = OP_GETFH; 6331 argcnt++; 6332 argop++; 6333 } 6334 /* post-lookup getattr */ 6335 argop->argop = OP_GETATTR; 6336 argop->nfs_argop4_u.opgetattr.attr_request = 6337 lookupargp->ga_bits; 6338 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6339 argcnt++; 6340 } 6341 argcnt += lookupargp->trailer_len; /* actual op count */ 6342 lookupargp->argsp->array_len = argcnt; 6343 lookupargp->arglen = arglen; 6344 6345 #ifdef DEBUG 6346 if (nfs4_client_lookup_debug) 6347 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6348 #endif 6349 6350 return (lookup_idx); 6351 } 6352 6353 static int 6354 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6355 { 6356 COMPOUND4args_clnt args; 6357 COMPOUND4res_clnt res; 6358 GETFH4res *gf_res = NULL; 6359 nfs_argop4 argop[4]; 6360 nfs_resop4 *resop = NULL; 6361 nfs4_sharedfh_t *sfhp; 6362 hrtime_t t; 6363 nfs4_error_t e; 6364 6365 rnode4_t *drp; 6366 int doqueue = 1; 6367 vnode_t *vp; 6368 int needrecov = 0; 6369 nfs4_recov_state_t recov_state; 6370 6371 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6372 6373 *avp = NULL; 6374 recov_state.rs_flags = 0; 6375 recov_state.rs_num_retry_despite_err = 0; 6376 6377 recov_retry: 6378 /* COMPOUND: putfh, openattr, getfh, getattr */ 6379 args.array_len = 4; 6380 args.array = argop; 6381 args.ctag = TAG_OPENATTR; 6382 6383 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6384 if (e.error) 6385 return (e.error); 6386 6387 drp = VTOR4(dvp); 6388 6389 /* putfh */ 6390 argop[0].argop = OP_CPUTFH; 6391 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6392 6393 /* openattr */ 6394 argop[1].argop = OP_OPENATTR; 6395 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6396 6397 /* getfh */ 6398 argop[2].argop = OP_GETFH; 6399 6400 /* getattr */ 6401 argop[3].argop = OP_GETATTR; 6402 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6403 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6404 6405 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6406 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6407 rnode4info(drp))); 6408 6409 t = gethrtime(); 6410 6411 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6412 6413 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6414 if (needrecov) { 6415 bool_t abort; 6416 6417 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6418 "nfs4openattr: initiating recovery\n")); 6419 6420 abort = nfs4_start_recovery(&e, 6421 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6422 OP_OPENATTR, NULL, NULL, NULL); 6423 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6424 if (!e.error) { 6425 e.error = geterrno4(res.status); 6426 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6427 } 6428 if (abort == FALSE) 6429 goto recov_retry; 6430 return (e.error); 6431 } 6432 6433 if (e.error) { 6434 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6435 return (e.error); 6436 } 6437 6438 if (res.status) { 6439 /* 6440 * If OTW errro is NOTSUPP, then it should be 6441 * translated to EINVAL. All Solaris file system 6442 * implementations return EINVAL to the syscall layer 6443 * when the attrdir cannot be created due to an 6444 * implementation restriction or noxattr mount option. 6445 */ 6446 if (res.status == NFS4ERR_NOTSUPP) { 6447 mutex_enter(&drp->r_statelock); 6448 if (drp->r_xattr_dir) 6449 VN_RELE(drp->r_xattr_dir); 6450 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6451 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6452 mutex_exit(&drp->r_statelock); 6453 6454 e.error = EINVAL; 6455 } else { 6456 e.error = geterrno4(res.status); 6457 } 6458 6459 if (e.error) { 6460 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6461 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6462 needrecov); 6463 return (e.error); 6464 } 6465 } 6466 6467 resop = &res.array[0]; /* putfh res */ 6468 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6469 6470 resop = &res.array[1]; /* openattr res */ 6471 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6472 6473 resop = &res.array[2]; /* getfh res */ 6474 gf_res = &resop->nfs_resop4_u.opgetfh; 6475 if (gf_res->object.nfs_fh4_len == 0) { 6476 *avp = NULL; 6477 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6478 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6479 return (ENOENT); 6480 } 6481 6482 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6483 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6484 dvp->v_vfsp, t, cr, dvp, 6485 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp)); 6486 sfh4_rele(&sfhp); 6487 6488 if (e.error) 6489 PURGE_ATTRCACHE4(vp); 6490 6491 mutex_enter(&vp->v_lock); 6492 vp->v_flag |= V_XATTRDIR; 6493 mutex_exit(&vp->v_lock); 6494 6495 *avp = vp; 6496 6497 mutex_enter(&drp->r_statelock); 6498 if (drp->r_xattr_dir) 6499 VN_RELE(drp->r_xattr_dir); 6500 VN_HOLD(vp); 6501 drp->r_xattr_dir = vp; 6502 6503 /* 6504 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6505 * NULL. xattrs could be created at any time, and we have no 6506 * way to update pc4_xattr_exists in the base object if/when 6507 * it happens. 6508 */ 6509 drp->r_pathconf.pc4_xattr_valid = 0; 6510 6511 mutex_exit(&drp->r_statelock); 6512 6513 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6514 6515 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6516 6517 return (0); 6518 } 6519 6520 /* ARGSUSED */ 6521 static int 6522 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6523 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct, 6524 vsecattr_t *vsecp) 6525 { 6526 int error; 6527 vnode_t *vp = NULL; 6528 rnode4_t *rp; 6529 struct vattr vattr; 6530 rnode4_t *drp; 6531 vnode_t *tempvp; 6532 enum createmode4 createmode; 6533 bool_t must_trunc = FALSE; 6534 int truncating = 0; 6535 6536 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6537 return (EPERM); 6538 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6539 return (EINVAL); 6540 } 6541 6542 /* . and .. have special meaning in the protocol, reject them. */ 6543 6544 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6545 return (EISDIR); 6546 6547 drp = VTOR4(dvp); 6548 6549 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6550 return (EINTR); 6551 6552 top: 6553 /* 6554 * We make a copy of the attributes because the caller does not 6555 * expect us to change what va points to. 6556 */ 6557 vattr = *va; 6558 6559 /* 6560 * If the pathname is "", then dvp is the root vnode of 6561 * a remote file mounted over a local directory. 6562 * All that needs to be done is access 6563 * checking and truncation. Note that we avoid doing 6564 * open w/ create because the parent directory might 6565 * be in pseudo-fs and the open would fail. 6566 */ 6567 if (*nm == '\0') { 6568 error = 0; 6569 VN_HOLD(dvp); 6570 vp = dvp; 6571 must_trunc = TRUE; 6572 } else { 6573 /* 6574 * We need to go over the wire, just to be sure whether the 6575 * file exists or not. Using the DNLC can be dangerous in 6576 * this case when making a decision regarding existence. 6577 */ 6578 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6579 } 6580 6581 if (exclusive) 6582 createmode = EXCLUSIVE4; 6583 else 6584 createmode = GUARDED4; 6585 6586 /* 6587 * error would be set if the file does not exist on the 6588 * server, so lets go create it. 6589 */ 6590 if (error) { 6591 goto create_otw; 6592 } 6593 6594 /* 6595 * File does exist on the server 6596 */ 6597 if (exclusive == EXCL) 6598 error = EEXIST; 6599 else if (vp->v_type == VDIR && (mode & VWRITE)) 6600 error = EISDIR; 6601 else { 6602 /* 6603 * If vnode is a device, create special vnode. 6604 */ 6605 if (ISVDEV(vp->v_type)) { 6606 tempvp = vp; 6607 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6608 VN_RELE(tempvp); 6609 } 6610 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) { 6611 if ((vattr.va_mask & AT_SIZE) && 6612 vp->v_type == VREG) { 6613 rp = VTOR4(vp); 6614 /* 6615 * Check here for large file handled 6616 * by LF-unaware process (as 6617 * ufs_create() does) 6618 */ 6619 if (!(flags & FOFFMAX)) { 6620 mutex_enter(&rp->r_statelock); 6621 if (rp->r_size > MAXOFF32_T) 6622 error = EOVERFLOW; 6623 mutex_exit(&rp->r_statelock); 6624 } 6625 6626 /* if error is set then we need to return */ 6627 if (error) { 6628 nfs_rw_exit(&drp->r_rwlock); 6629 VN_RELE(vp); 6630 return (error); 6631 } 6632 6633 if (must_trunc) { 6634 vattr.va_mask = AT_SIZE; 6635 error = nfs4setattr(vp, &vattr, 0, cr, 6636 NULL); 6637 } else { 6638 /* 6639 * we know we have a regular file that already 6640 * exists and we may end up truncating the file 6641 * as a result of the open_otw, so flush out 6642 * any dirty pages for this file first. 6643 */ 6644 if (nfs4_has_pages(vp) && 6645 ((rp->r_flags & R4DIRTY) || 6646 rp->r_count > 0 || 6647 rp->r_mapcnt > 0)) { 6648 error = nfs4_putpage(vp, 6649 (offset_t)0, 0, 0, cr, ct); 6650 if (error && (error == ENOSPC || 6651 error == EDQUOT)) { 6652 mutex_enter( 6653 &rp->r_statelock); 6654 if (!rp->r_error) 6655 rp->r_error = 6656 error; 6657 mutex_exit( 6658 &rp->r_statelock); 6659 } 6660 } 6661 vattr.va_mask = (AT_SIZE | 6662 AT_TYPE | AT_MODE); 6663 vattr.va_type = VREG; 6664 createmode = UNCHECKED4; 6665 truncating = 1; 6666 goto create_otw; 6667 } 6668 } 6669 } 6670 } 6671 nfs_rw_exit(&drp->r_rwlock); 6672 if (error) { 6673 VN_RELE(vp); 6674 } else { 6675 vnode_t *tvp; 6676 rnode4_t *trp; 6677 tvp = vp; 6678 if (vp->v_type == VREG) { 6679 trp = VTOR4(vp); 6680 if (IS_SHADOW(vp, trp)) 6681 tvp = RTOV4(trp); 6682 } 6683 6684 if (must_trunc) { 6685 /* 6686 * existing file got truncated, notify. 6687 */ 6688 vnevent_create(tvp, ct); 6689 } 6690 6691 *vpp = vp; 6692 } 6693 return (error); 6694 6695 create_otw: 6696 dnlc_remove(dvp, nm); 6697 6698 ASSERT(vattr.va_mask & AT_TYPE); 6699 6700 /* 6701 * If not a regular file let nfs4mknod() handle it. 6702 */ 6703 if (vattr.va_type != VREG) { 6704 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6705 nfs_rw_exit(&drp->r_rwlock); 6706 return (error); 6707 } 6708 6709 /* 6710 * It _is_ a regular file. 6711 */ 6712 ASSERT(vattr.va_mask & AT_MODE); 6713 if (MANDMODE(vattr.va_mode)) { 6714 nfs_rw_exit(&drp->r_rwlock); 6715 return (EACCES); 6716 } 6717 6718 /* 6719 * If this happens to be a mknod of a regular file, then flags will 6720 * have neither FREAD or FWRITE. However, we must set at least one 6721 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6722 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6723 * set (based on openmode specified by app). 6724 */ 6725 if ((flags & (FREAD|FWRITE)) == 0) 6726 flags |= (FREAD|FWRITE); 6727 6728 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6729 6730 if (vp != NULL) { 6731 /* if create was successful, throw away the file's pages */ 6732 if (!error && (vattr.va_mask & AT_SIZE)) 6733 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6734 cr); 6735 /* release the lookup hold */ 6736 VN_RELE(vp); 6737 vp = NULL; 6738 } 6739 6740 /* 6741 * validate that we opened a regular file. This handles a misbehaving 6742 * server that returns an incorrect FH. 6743 */ 6744 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6745 error = EISDIR; 6746 VN_RELE(*vpp); 6747 } 6748 6749 /* 6750 * If this is not an exclusive create, then the CREATE 6751 * request will be made with the GUARDED mode set. This 6752 * means that the server will return EEXIST if the file 6753 * exists. The file could exist because of a retransmitted 6754 * request. In this case, we recover by starting over and 6755 * checking to see whether the file exists. This second 6756 * time through it should and a CREATE request will not be 6757 * sent. 6758 * 6759 * This handles the problem of a dangling CREATE request 6760 * which contains attributes which indicate that the file 6761 * should be truncated. This retransmitted request could 6762 * possibly truncate valid data in the file if not caught 6763 * by the duplicate request mechanism on the server or if 6764 * not caught by other means. The scenario is: 6765 * 6766 * Client transmits CREATE request with size = 0 6767 * Client times out, retransmits request. 6768 * Response to the first request arrives from the server 6769 * and the client proceeds on. 6770 * Client writes data to the file. 6771 * The server now processes retransmitted CREATE request 6772 * and truncates file. 6773 * 6774 * The use of the GUARDED CREATE request prevents this from 6775 * happening because the retransmitted CREATE would fail 6776 * with EEXIST and would not truncate the file. 6777 */ 6778 if (error == EEXIST && exclusive == NONEXCL) { 6779 #ifdef DEBUG 6780 nfs4_create_misses++; 6781 #endif 6782 goto top; 6783 } 6784 nfs_rw_exit(&drp->r_rwlock); 6785 if (truncating && !error && *vpp) { 6786 vnode_t *tvp; 6787 rnode4_t *trp; 6788 /* 6789 * existing file got truncated, notify. 6790 */ 6791 tvp = *vpp; 6792 trp = VTOR4(tvp); 6793 if (IS_SHADOW(tvp, trp)) 6794 tvp = RTOV4(trp); 6795 vnevent_create(tvp, ct); 6796 } 6797 return (error); 6798 } 6799 6800 /* 6801 * Create compound (for mkdir, mknod, symlink): 6802 * { Putfh <dfh>; Create; Getfh; Getattr } 6803 * It's okay if setattr failed to set gid - this is not considered 6804 * an error, but purge attrs in that case. 6805 */ 6806 static int 6807 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6808 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6809 { 6810 int need_end_op = FALSE; 6811 COMPOUND4args_clnt args; 6812 COMPOUND4res_clnt res, *resp = NULL; 6813 nfs_argop4 *argop; 6814 nfs_resop4 *resop; 6815 int doqueue; 6816 mntinfo4_t *mi; 6817 rnode4_t *drp = VTOR4(dvp); 6818 change_info4 *cinfo; 6819 GETFH4res *gf_res; 6820 struct vattr vattr; 6821 vnode_t *vp; 6822 fattr4 *crattr; 6823 bool_t needrecov = FALSE; 6824 nfs4_recov_state_t recov_state; 6825 nfs4_sharedfh_t *sfhp = NULL; 6826 hrtime_t t; 6827 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6828 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6829 dirattr_info_t dinfo, *dinfop; 6830 servinfo4_t *svp; 6831 bitmap4 supp_attrs; 6832 6833 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6834 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6835 6836 mi = VTOMI4(dvp); 6837 6838 /* 6839 * Make sure we properly deal with setting the right gid 6840 * on a new directory to reflect the parent's setgid bit 6841 */ 6842 setgid_flag = 0; 6843 if (type == NF4DIR) { 6844 struct vattr dva; 6845 6846 va->va_mode &= ~VSGID; 6847 dva.va_mask = AT_MODE | AT_GID; 6848 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) { 6849 6850 /* 6851 * If the parent's directory has the setgid bit set 6852 * _and_ the client was able to get a valid mapping 6853 * for the parent dir's owner_group, we want to 6854 * append NVERIFY(owner_group == dva.va_gid) and 6855 * SETTATTR to the CREATE compound. 6856 */ 6857 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6858 setgid_flag = 1; 6859 va->va_mode |= VSGID; 6860 if (dva.va_gid != GID_NOBODY) { 6861 va->va_mask |= AT_GID; 6862 va->va_gid = dva.va_gid; 6863 } 6864 } 6865 } 6866 } 6867 6868 /* 6869 * Create ops: 6870 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6871 * 5:restorefh(dir) 6:getattr(dir) 6872 * 6873 * if (setgid) 6874 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6875 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6876 * 8:nverify 9:setattr 6877 */ 6878 if (setgid_flag) { 6879 numops = 10; 6880 idx_create = 1; 6881 idx_fattr = 3; 6882 } else { 6883 numops = 7; 6884 idx_create = 2; 6885 idx_fattr = 4; 6886 } 6887 6888 ASSERT(nfs_zone() == mi->mi_zone); 6889 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6890 return (EINTR); 6891 } 6892 recov_state.rs_flags = 0; 6893 recov_state.rs_num_retry_despite_err = 0; 6894 6895 argoplist_size = numops * sizeof (nfs_argop4); 6896 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6897 6898 recov_retry: 6899 if (type == NF4LNK) 6900 args.ctag = TAG_SYMLINK; 6901 else if (type == NF4DIR) 6902 args.ctag = TAG_MKDIR; 6903 else 6904 args.ctag = TAG_MKNOD; 6905 6906 args.array_len = numops; 6907 args.array = argop; 6908 6909 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6910 nfs_rw_exit(&drp->r_rwlock); 6911 kmem_free(argop, argoplist_size); 6912 return (e.error); 6913 } 6914 need_end_op = TRUE; 6915 6916 6917 /* 0: putfh directory */ 6918 argop[0].argop = OP_CPUTFH; 6919 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6920 6921 /* 1/2: Create object */ 6922 argop[idx_create].argop = OP_CCREATE; 6923 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6924 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6925 if (type == NF4LNK) { 6926 /* 6927 * symlink, treat name as data 6928 */ 6929 ASSERT(data != NULL); 6930 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6931 (char *)data; 6932 } 6933 if (type == NF4BLK || type == NF4CHR) { 6934 ASSERT(data != NULL); 6935 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6936 *((specdata4 *)data); 6937 } 6938 6939 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6940 6941 svp = drp->r_server; 6942 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6943 supp_attrs = svp->sv_supp_attrs; 6944 nfs_rw_exit(&svp->sv_lock); 6945 6946 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6947 nfs_rw_exit(&drp->r_rwlock); 6948 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6949 e.error = EINVAL; 6950 kmem_free(argop, argoplist_size); 6951 return (e.error); 6952 } 6953 6954 /* 2/3: getfh fh of created object */ 6955 ASSERT(idx_create + 1 == idx_fattr - 1); 6956 argop[idx_create + 1].argop = OP_GETFH; 6957 6958 /* 3/4: getattr of new object */ 6959 argop[idx_fattr].argop = OP_GETATTR; 6960 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6961 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6962 6963 if (setgid_flag) { 6964 vattr_t _v; 6965 6966 argop[4].argop = OP_SAVEFH; 6967 6968 argop[5].argop = OP_CPUTFH; 6969 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6970 6971 argop[6].argop = OP_GETATTR; 6972 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6973 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6974 6975 argop[7].argop = OP_RESTOREFH; 6976 6977 /* 6978 * nverify 6979 * 6980 * XXX - Revisit the last argument to nfs4_end_op() 6981 * once 5020486 is fixed. 6982 */ 6983 _v.va_mask = AT_GID; 6984 _v.va_gid = va->va_gid; 6985 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6986 supp_attrs)) { 6987 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6988 nfs_rw_exit(&drp->r_rwlock); 6989 nfs4_fattr4_free(crattr); 6990 kmem_free(argop, argoplist_size); 6991 return (e.error); 6992 } 6993 6994 /* 6995 * setattr 6996 * 6997 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6998 * so no need for stateid or flags. Also we specify NULL 6999 * rp since we're only interested in setting owner_group 7000 * attributes. 7001 */ 7002 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 7003 &e.error, 0); 7004 7005 if (e.error) { 7006 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 7007 nfs_rw_exit(&drp->r_rwlock); 7008 nfs4_fattr4_free(crattr); 7009 nfs4args_verify_free(&argop[8]); 7010 kmem_free(argop, argoplist_size); 7011 return (e.error); 7012 } 7013 } else { 7014 argop[1].argop = OP_SAVEFH; 7015 7016 argop[5].argop = OP_RESTOREFH; 7017 7018 argop[6].argop = OP_GETATTR; 7019 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7020 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7021 } 7022 7023 dnlc_remove(dvp, nm); 7024 7025 doqueue = 1; 7026 t = gethrtime(); 7027 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7028 7029 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7030 if (e.error) { 7031 PURGE_ATTRCACHE4(dvp); 7032 if (!needrecov) 7033 goto out; 7034 } 7035 7036 if (needrecov) { 7037 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 7038 OP_CREATE, NULL, NULL, NULL) == FALSE) { 7039 nfs4_end_op(mi, dvp, NULL, &recov_state, 7040 needrecov); 7041 need_end_op = FALSE; 7042 nfs4_fattr4_free(crattr); 7043 if (setgid_flag) { 7044 nfs4args_verify_free(&argop[8]); 7045 nfs4args_setattr_free(&argop[9]); 7046 } 7047 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 7048 goto recov_retry; 7049 } 7050 } 7051 7052 resp = &res; 7053 7054 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 7055 7056 if (res.status == NFS4ERR_BADOWNER) 7057 nfs4_log_badowner(mi, OP_CREATE); 7058 7059 e.error = geterrno4(res.status); 7060 7061 /* 7062 * This check is left over from when create was implemented 7063 * using a setattr op (instead of createattrs). If the 7064 * putfh/create/getfh failed, the error was returned. If 7065 * setattr/getattr failed, we keep going. 7066 * 7067 * It might be better to get rid of the GETFH also, and just 7068 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 7069 * Then if any of the operations failed, we could return the 7070 * error now, and remove much of the error code below. 7071 */ 7072 if (res.array_len <= idx_fattr) { 7073 /* 7074 * Either Putfh, Create or Getfh failed. 7075 */ 7076 PURGE_ATTRCACHE4(dvp); 7077 /* 7078 * nfs4_purge_stale_fh() may generate otw calls through 7079 * nfs4_invalidate_pages. Hence the need to call 7080 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 7081 */ 7082 nfs4_end_op(mi, dvp, NULL, &recov_state, 7083 needrecov); 7084 need_end_op = FALSE; 7085 nfs4_purge_stale_fh(e.error, dvp, cr); 7086 goto out; 7087 } 7088 } 7089 7090 resop = &res.array[idx_create]; /* create res */ 7091 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 7092 7093 resop = &res.array[idx_create + 1]; /* getfh res */ 7094 gf_res = &resop->nfs_resop4_u.opgetfh; 7095 7096 sfhp = sfh4_get(&gf_res->object, mi); 7097 if (e.error) { 7098 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 7099 fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7100 if (vp->v_type == VNON) { 7101 vattr.va_mask = AT_TYPE; 7102 /* 7103 * Need to call nfs4_end_op before nfs4getattr to avoid 7104 * potential nfs4_start_op deadlock. See RFE 4777612. 7105 */ 7106 nfs4_end_op(mi, dvp, NULL, &recov_state, 7107 needrecov); 7108 need_end_op = FALSE; 7109 e.error = nfs4getattr(vp, &vattr, cr); 7110 if (e.error) { 7111 VN_RELE(vp); 7112 *vpp = NULL; 7113 goto out; 7114 } 7115 vp->v_type = vattr.va_type; 7116 } 7117 e.error = 0; 7118 } else { 7119 *vpp = vp = makenfs4node(sfhp, 7120 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 7121 dvp->v_vfsp, t, cr, 7122 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7123 } 7124 7125 /* 7126 * If compound succeeded, then update dir attrs 7127 */ 7128 if (res.status == NFS4_OK) { 7129 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 7130 dinfo.di_cred = cr; 7131 dinfo.di_time_call = t; 7132 dinfop = &dinfo; 7133 } else 7134 dinfop = NULL; 7135 7136 /* Update directory cache attribute, readdir and dnlc caches */ 7137 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 7138 7139 out: 7140 if (sfhp != NULL) 7141 sfh4_rele(&sfhp); 7142 nfs_rw_exit(&drp->r_rwlock); 7143 nfs4_fattr4_free(crattr); 7144 if (setgid_flag) { 7145 nfs4args_verify_free(&argop[8]); 7146 nfs4args_setattr_free(&argop[9]); 7147 } 7148 if (resp) 7149 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7150 if (need_end_op) 7151 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7152 7153 kmem_free(argop, argoplist_size); 7154 return (e.error); 7155 } 7156 7157 /* ARGSUSED */ 7158 static int 7159 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7160 int mode, vnode_t **vpp, cred_t *cr) 7161 { 7162 int error; 7163 vnode_t *vp; 7164 nfs_ftype4 type; 7165 specdata4 spec, *specp = NULL; 7166 7167 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7168 7169 switch (va->va_type) { 7170 case VCHR: 7171 case VBLK: 7172 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7173 spec.specdata1 = getmajor(va->va_rdev); 7174 spec.specdata2 = getminor(va->va_rdev); 7175 specp = &spec; 7176 break; 7177 7178 case VFIFO: 7179 type = NF4FIFO; 7180 break; 7181 case VSOCK: 7182 type = NF4SOCK; 7183 break; 7184 7185 default: 7186 return (EINVAL); 7187 } 7188 7189 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7190 if (error) { 7191 return (error); 7192 } 7193 7194 /* 7195 * This might not be needed any more; special case to deal 7196 * with problematic v2/v3 servers. Since create was unable 7197 * to set group correctly, not sure what hope setattr has. 7198 */ 7199 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7200 va->va_mask = AT_GID; 7201 (void) nfs4setattr(vp, va, 0, cr, NULL); 7202 } 7203 7204 /* 7205 * If vnode is a device create special vnode 7206 */ 7207 if (ISVDEV(vp->v_type)) { 7208 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7209 VN_RELE(vp); 7210 } else { 7211 *vpp = vp; 7212 } 7213 return (error); 7214 } 7215 7216 /* 7217 * Remove requires that the current fh be the target directory. 7218 * After the operation, the current fh is unchanged. 7219 * The compound op structure is: 7220 * PUTFH(targetdir), REMOVE 7221 * 7222 * Weirdness: if the vnode to be removed is open 7223 * we rename it instead of removing it and nfs_inactive 7224 * will remove the new name. 7225 */ 7226 /* ARGSUSED */ 7227 static int 7228 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags) 7229 { 7230 COMPOUND4args_clnt args; 7231 COMPOUND4res_clnt res, *resp = NULL; 7232 REMOVE4res *rm_res; 7233 nfs_argop4 argop[3]; 7234 nfs_resop4 *resop; 7235 vnode_t *vp; 7236 char *tmpname; 7237 int doqueue; 7238 mntinfo4_t *mi; 7239 rnode4_t *rp; 7240 rnode4_t *drp; 7241 int needrecov = 0; 7242 nfs4_recov_state_t recov_state; 7243 int isopen; 7244 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7245 dirattr_info_t dinfo; 7246 7247 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7248 return (EPERM); 7249 drp = VTOR4(dvp); 7250 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7251 return (EINTR); 7252 7253 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7254 if (e.error) { 7255 nfs_rw_exit(&drp->r_rwlock); 7256 return (e.error); 7257 } 7258 7259 if (vp->v_type == VDIR) { 7260 VN_RELE(vp); 7261 nfs_rw_exit(&drp->r_rwlock); 7262 return (EISDIR); 7263 } 7264 7265 /* 7266 * First just remove the entry from the name cache, as it 7267 * is most likely the only entry for this vp. 7268 */ 7269 dnlc_remove(dvp, nm); 7270 7271 rp = VTOR4(vp); 7272 7273 /* 7274 * For regular file types, check to see if the file is open by looking 7275 * at the open streams. 7276 * For all other types, check the reference count on the vnode. Since 7277 * they are not opened OTW they never have an open stream. 7278 * 7279 * If the file is open, rename it to .nfsXXXX. 7280 */ 7281 if (vp->v_type != VREG) { 7282 /* 7283 * If the file has a v_count > 1 then there may be more than one 7284 * entry in the name cache due multiple links or an open file, 7285 * but we don't have the real reference count so flush all 7286 * possible entries. 7287 */ 7288 if (vp->v_count > 1) 7289 dnlc_purge_vp(vp); 7290 7291 /* 7292 * Now we have the real reference count. 7293 */ 7294 isopen = vp->v_count > 1; 7295 } else { 7296 mutex_enter(&rp->r_os_lock); 7297 isopen = list_head(&rp->r_open_streams) != NULL; 7298 mutex_exit(&rp->r_os_lock); 7299 } 7300 7301 mutex_enter(&rp->r_statelock); 7302 if (isopen && 7303 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7304 mutex_exit(&rp->r_statelock); 7305 tmpname = newname(); 7306 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct); 7307 if (e.error) 7308 kmem_free(tmpname, MAXNAMELEN); 7309 else { 7310 mutex_enter(&rp->r_statelock); 7311 if (rp->r_unldvp == NULL) { 7312 VN_HOLD(dvp); 7313 rp->r_unldvp = dvp; 7314 if (rp->r_unlcred != NULL) 7315 crfree(rp->r_unlcred); 7316 crhold(cr); 7317 rp->r_unlcred = cr; 7318 rp->r_unlname = tmpname; 7319 } else { 7320 kmem_free(rp->r_unlname, MAXNAMELEN); 7321 rp->r_unlname = tmpname; 7322 } 7323 mutex_exit(&rp->r_statelock); 7324 } 7325 VN_RELE(vp); 7326 nfs_rw_exit(&drp->r_rwlock); 7327 return (e.error); 7328 } 7329 /* 7330 * Actually remove the file/dir 7331 */ 7332 mutex_exit(&rp->r_statelock); 7333 7334 /* 7335 * We need to flush any dirty pages which happen to 7336 * be hanging around before removing the file. 7337 * This shouldn't happen very often since in NFSv4 7338 * we should be close to open consistent. 7339 */ 7340 if (nfs4_has_pages(vp) && 7341 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7342 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct); 7343 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7344 mutex_enter(&rp->r_statelock); 7345 if (!rp->r_error) 7346 rp->r_error = e.error; 7347 mutex_exit(&rp->r_statelock); 7348 } 7349 } 7350 7351 mi = VTOMI4(dvp); 7352 7353 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7354 recov_state.rs_flags = 0; 7355 recov_state.rs_num_retry_despite_err = 0; 7356 7357 recov_retry: 7358 /* 7359 * Remove ops: putfh dir; remove 7360 */ 7361 args.ctag = TAG_REMOVE; 7362 args.array_len = 3; 7363 args.array = argop; 7364 7365 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7366 if (e.error) { 7367 nfs_rw_exit(&drp->r_rwlock); 7368 VN_RELE(vp); 7369 return (e.error); 7370 } 7371 7372 /* putfh directory */ 7373 argop[0].argop = OP_CPUTFH; 7374 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7375 7376 /* remove */ 7377 argop[1].argop = OP_CREMOVE; 7378 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7379 7380 /* getattr dir */ 7381 argop[2].argop = OP_GETATTR; 7382 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7383 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7384 7385 doqueue = 1; 7386 dinfo.di_time_call = gethrtime(); 7387 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7388 7389 PURGE_ATTRCACHE4(vp); 7390 7391 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7392 if (e.error) 7393 PURGE_ATTRCACHE4(dvp); 7394 7395 if (needrecov) { 7396 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7397 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 7398 if (!e.error) 7399 (void) xdr_free(xdr_COMPOUND4res_clnt, 7400 (caddr_t)&res); 7401 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7402 needrecov); 7403 goto recov_retry; 7404 } 7405 } 7406 7407 /* 7408 * Matching nfs4_end_op() for start_op() above. 7409 * There is a path in the code below which calls 7410 * nfs4_purge_stale_fh(), which may generate otw calls through 7411 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7412 * here to avoid nfs4_start_op() deadlock. 7413 */ 7414 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7415 7416 if (!e.error) { 7417 resp = &res; 7418 7419 if (res.status) { 7420 e.error = geterrno4(res.status); 7421 PURGE_ATTRCACHE4(dvp); 7422 nfs4_purge_stale_fh(e.error, dvp, cr); 7423 } else { 7424 resop = &res.array[1]; /* remove res */ 7425 rm_res = &resop->nfs_resop4_u.opremove; 7426 7427 dinfo.di_garp = 7428 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7429 dinfo.di_cred = cr; 7430 7431 /* Update directory attr, readdir and dnlc caches */ 7432 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7433 &dinfo); 7434 } 7435 } 7436 nfs_rw_exit(&drp->r_rwlock); 7437 if (resp) 7438 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7439 7440 if (e.error == 0) { 7441 vnode_t *tvp; 7442 rnode4_t *trp; 7443 trp = VTOR4(vp); 7444 tvp = vp; 7445 if (IS_SHADOW(vp, trp)) 7446 tvp = RTOV4(trp); 7447 vnevent_remove(tvp, dvp, nm, ct); 7448 } 7449 VN_RELE(vp); 7450 return (e.error); 7451 } 7452 7453 /* 7454 * Link requires that the current fh be the target directory and the 7455 * saved fh be the source fh. After the operation, the current fh is unchanged. 7456 * Thus the compound op structure is: 7457 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7458 * GETATTR(file) 7459 */ 7460 /* ARGSUSED */ 7461 static int 7462 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr, 7463 caller_context_t *ct, int flags) 7464 { 7465 COMPOUND4args_clnt args; 7466 COMPOUND4res_clnt res, *resp = NULL; 7467 LINK4res *ln_res; 7468 int argoplist_size = 7 * sizeof (nfs_argop4); 7469 nfs_argop4 *argop; 7470 nfs_resop4 *resop; 7471 vnode_t *realvp, *nvp; 7472 int doqueue; 7473 mntinfo4_t *mi; 7474 rnode4_t *tdrp; 7475 bool_t needrecov = FALSE; 7476 nfs4_recov_state_t recov_state; 7477 hrtime_t t; 7478 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7479 dirattr_info_t dinfo; 7480 7481 ASSERT(*tnm != '\0'); 7482 ASSERT(tdvp->v_type == VDIR); 7483 ASSERT(nfs4_consistent_type(tdvp)); 7484 ASSERT(nfs4_consistent_type(svp)); 7485 7486 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7487 return (EPERM); 7488 if (VOP_REALVP(svp, &realvp, ct) == 0) { 7489 svp = realvp; 7490 ASSERT(nfs4_consistent_type(svp)); 7491 } 7492 7493 tdrp = VTOR4(tdvp); 7494 mi = VTOMI4(svp); 7495 7496 if (!(mi->mi_flags & MI4_LINK)) { 7497 return (EOPNOTSUPP); 7498 } 7499 recov_state.rs_flags = 0; 7500 recov_state.rs_num_retry_despite_err = 0; 7501 7502 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7503 return (EINTR); 7504 7505 recov_retry: 7506 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7507 7508 args.ctag = TAG_LINK; 7509 7510 /* 7511 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7512 * restorefh; getattr(fl) 7513 */ 7514 args.array_len = 7; 7515 args.array = argop; 7516 7517 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7518 if (e.error) { 7519 kmem_free(argop, argoplist_size); 7520 nfs_rw_exit(&tdrp->r_rwlock); 7521 return (e.error); 7522 } 7523 7524 /* 0. putfh file */ 7525 argop[0].argop = OP_CPUTFH; 7526 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7527 7528 /* 1. save current fh to free up the space for the dir */ 7529 argop[1].argop = OP_SAVEFH; 7530 7531 /* 2. putfh targetdir */ 7532 argop[2].argop = OP_CPUTFH; 7533 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7534 7535 /* 3. link: current_fh is targetdir, saved_fh is source */ 7536 argop[3].argop = OP_CLINK; 7537 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7538 7539 /* 4. Get attributes of dir */ 7540 argop[4].argop = OP_GETATTR; 7541 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7542 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7543 7544 /* 5. If link was successful, restore current vp to file */ 7545 argop[5].argop = OP_RESTOREFH; 7546 7547 /* 6. Get attributes of linked object */ 7548 argop[6].argop = OP_GETATTR; 7549 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7550 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7551 7552 dnlc_remove(tdvp, tnm); 7553 7554 doqueue = 1; 7555 t = gethrtime(); 7556 7557 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7558 7559 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7560 if (e.error != 0 && !needrecov) { 7561 PURGE_ATTRCACHE4(tdvp); 7562 PURGE_ATTRCACHE4(svp); 7563 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7564 goto out; 7565 } 7566 7567 if (needrecov) { 7568 bool_t abort; 7569 7570 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7571 NULL, NULL, OP_LINK, NULL, NULL, NULL); 7572 if (abort == FALSE) { 7573 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7574 needrecov); 7575 kmem_free(argop, argoplist_size); 7576 if (!e.error) 7577 (void) xdr_free(xdr_COMPOUND4res_clnt, 7578 (caddr_t)&res); 7579 goto recov_retry; 7580 } else { 7581 if (e.error != 0) { 7582 PURGE_ATTRCACHE4(tdvp); 7583 PURGE_ATTRCACHE4(svp); 7584 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7585 &recov_state, needrecov); 7586 goto out; 7587 } 7588 /* fall through for res.status case */ 7589 } 7590 } 7591 7592 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7593 7594 resp = &res; 7595 if (res.status) { 7596 /* If link succeeded, then don't return error */ 7597 e.error = geterrno4(res.status); 7598 if (res.array_len <= 4) { 7599 /* 7600 * Either Putfh, Savefh, Putfh dir, or Link failed 7601 */ 7602 PURGE_ATTRCACHE4(svp); 7603 PURGE_ATTRCACHE4(tdvp); 7604 if (e.error == EOPNOTSUPP) { 7605 mutex_enter(&mi->mi_lock); 7606 mi->mi_flags &= ~MI4_LINK; 7607 mutex_exit(&mi->mi_lock); 7608 } 7609 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7610 /* XXX-LP */ 7611 if (e.error == EISDIR && crgetuid(cr) != 0) 7612 e.error = EPERM; 7613 goto out; 7614 } 7615 } 7616 7617 /* either no error or one of the postop getattr failed */ 7618 7619 /* 7620 * XXX - if LINK succeeded, but no attrs were returned for link 7621 * file, purge its cache. 7622 * 7623 * XXX Perform a simplified version of wcc checking. Instead of 7624 * have another getattr to get pre-op, just purge cache if 7625 * any of the ops prior to and including the getattr failed. 7626 * If the getattr succeeded then update the attrcache accordingly. 7627 */ 7628 7629 /* 7630 * update cache with link file postattrs. 7631 * Note: at this point resop points to link res. 7632 */ 7633 resop = &res.array[3]; /* link res */ 7634 ln_res = &resop->nfs_resop4_u.oplink; 7635 if (res.status == NFS4_OK) 7636 e.error = nfs4_update_attrcache(res.status, 7637 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7638 t, svp, cr); 7639 7640 /* 7641 * Call makenfs4node to create the new shadow vp for tnm. 7642 * We pass NULL attrs because we just cached attrs for 7643 * the src object. All we're trying to accomplish is to 7644 * to create the new shadow vnode. 7645 */ 7646 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7647 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh)); 7648 7649 /* Update target cache attribute, readdir and dnlc caches */ 7650 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7651 dinfo.di_time_call = t; 7652 dinfo.di_cred = cr; 7653 7654 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7655 ASSERT(nfs4_consistent_type(tdvp)); 7656 ASSERT(nfs4_consistent_type(svp)); 7657 ASSERT(nfs4_consistent_type(nvp)); 7658 VN_RELE(nvp); 7659 7660 if (!e.error) { 7661 vnode_t *tvp; 7662 rnode4_t *trp; 7663 /* 7664 * Notify the source file of this link operation. 7665 */ 7666 trp = VTOR4(svp); 7667 tvp = svp; 7668 if (IS_SHADOW(svp, trp)) 7669 tvp = RTOV4(trp); 7670 vnevent_link(tvp, ct); 7671 } 7672 out: 7673 kmem_free(argop, argoplist_size); 7674 if (resp) 7675 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7676 7677 nfs_rw_exit(&tdrp->r_rwlock); 7678 7679 return (e.error); 7680 } 7681 7682 /* ARGSUSED */ 7683 static int 7684 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7685 caller_context_t *ct, int flags) 7686 { 7687 vnode_t *realvp; 7688 7689 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7690 return (EPERM); 7691 if (VOP_REALVP(ndvp, &realvp, ct) == 0) 7692 ndvp = realvp; 7693 7694 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct)); 7695 } 7696 7697 /* 7698 * nfs4rename does the real work of renaming in NFS Version 4. 7699 * 7700 * A file handle is considered volatile for renaming purposes if either 7701 * of the volatile bits are turned on. However, the compound may differ 7702 * based on the likelihood of the filehandle to change during rename. 7703 */ 7704 static int 7705 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7706 caller_context_t *ct) 7707 { 7708 int error; 7709 mntinfo4_t *mi; 7710 vnode_t *nvp = NULL; 7711 vnode_t *ovp = NULL; 7712 char *tmpname = NULL; 7713 rnode4_t *rp; 7714 rnode4_t *odrp; 7715 rnode4_t *ndrp; 7716 int did_link = 0; 7717 int do_link = 1; 7718 nfsstat4 stat = NFS4_OK; 7719 7720 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7721 ASSERT(nfs4_consistent_type(odvp)); 7722 ASSERT(nfs4_consistent_type(ndvp)); 7723 7724 if (onm[0] == '.' && (onm[1] == '\0' || 7725 (onm[1] == '.' && onm[2] == '\0'))) 7726 return (EINVAL); 7727 7728 if (nnm[0] == '.' && (nnm[1] == '\0' || 7729 (nnm[1] == '.' && nnm[2] == '\0'))) 7730 return (EINVAL); 7731 7732 odrp = VTOR4(odvp); 7733 ndrp = VTOR4(ndvp); 7734 if ((intptr_t)odrp < (intptr_t)ndrp) { 7735 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7736 return (EINTR); 7737 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7738 nfs_rw_exit(&odrp->r_rwlock); 7739 return (EINTR); 7740 } 7741 } else { 7742 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7743 return (EINTR); 7744 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7745 nfs_rw_exit(&ndrp->r_rwlock); 7746 return (EINTR); 7747 } 7748 } 7749 7750 /* 7751 * Lookup the target file. If it exists, it needs to be 7752 * checked to see whether it is a mount point and whether 7753 * it is active (open). 7754 */ 7755 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7756 if (!error) { 7757 int isactive; 7758 7759 ASSERT(nfs4_consistent_type(nvp)); 7760 /* 7761 * If this file has been mounted on, then just 7762 * return busy because renaming to it would remove 7763 * the mounted file system from the name space. 7764 */ 7765 if (vn_ismntpt(nvp)) { 7766 VN_RELE(nvp); 7767 nfs_rw_exit(&odrp->r_rwlock); 7768 nfs_rw_exit(&ndrp->r_rwlock); 7769 return (EBUSY); 7770 } 7771 7772 /* 7773 * First just remove the entry from the name cache, as it 7774 * is most likely the only entry for this vp. 7775 */ 7776 dnlc_remove(ndvp, nnm); 7777 7778 rp = VTOR4(nvp); 7779 7780 if (nvp->v_type != VREG) { 7781 /* 7782 * Purge the name cache of all references to this vnode 7783 * so that we can check the reference count to infer 7784 * whether it is active or not. 7785 */ 7786 if (nvp->v_count > 1) 7787 dnlc_purge_vp(nvp); 7788 7789 isactive = nvp->v_count > 1; 7790 } else { 7791 mutex_enter(&rp->r_os_lock); 7792 isactive = list_head(&rp->r_open_streams) != NULL; 7793 mutex_exit(&rp->r_os_lock); 7794 } 7795 7796 /* 7797 * If the vnode is active and is not a directory, 7798 * arrange to rename it to a 7799 * temporary file so that it will continue to be 7800 * accessible. This implements the "unlink-open-file" 7801 * semantics for the target of a rename operation. 7802 * Before doing this though, make sure that the 7803 * source and target files are not already the same. 7804 */ 7805 if (isactive && nvp->v_type != VDIR) { 7806 /* 7807 * Lookup the source name. 7808 */ 7809 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7810 7811 /* 7812 * The source name *should* already exist. 7813 */ 7814 if (error) { 7815 VN_RELE(nvp); 7816 nfs_rw_exit(&odrp->r_rwlock); 7817 nfs_rw_exit(&ndrp->r_rwlock); 7818 return (error); 7819 } 7820 7821 ASSERT(nfs4_consistent_type(ovp)); 7822 7823 /* 7824 * Compare the two vnodes. If they are the same, 7825 * just release all held vnodes and return success. 7826 */ 7827 if (VN_CMP(ovp, nvp)) { 7828 VN_RELE(ovp); 7829 VN_RELE(nvp); 7830 nfs_rw_exit(&odrp->r_rwlock); 7831 nfs_rw_exit(&ndrp->r_rwlock); 7832 return (0); 7833 } 7834 7835 /* 7836 * Can't mix and match directories and non- 7837 * directories in rename operations. We already 7838 * know that the target is not a directory. If 7839 * the source is a directory, return an error. 7840 */ 7841 if (ovp->v_type == VDIR) { 7842 VN_RELE(ovp); 7843 VN_RELE(nvp); 7844 nfs_rw_exit(&odrp->r_rwlock); 7845 nfs_rw_exit(&ndrp->r_rwlock); 7846 return (ENOTDIR); 7847 } 7848 link_call: 7849 /* 7850 * The target file exists, is not the same as 7851 * the source file, and is active. We first 7852 * try to Link it to a temporary filename to 7853 * avoid having the server removing the file 7854 * completely (which could cause data loss to 7855 * the user's POV in the event the Rename fails 7856 * -- see bug 1165874). 7857 */ 7858 /* 7859 * The do_link and did_link booleans are 7860 * introduced in the event we get NFS4ERR_FILE_OPEN 7861 * returned for the Rename. Some servers can 7862 * not Rename over an Open file, so they return 7863 * this error. The client needs to Remove the 7864 * newly created Link and do two Renames, just 7865 * as if the server didn't support LINK. 7866 */ 7867 tmpname = newname(); 7868 error = 0; 7869 7870 if (do_link) { 7871 error = nfs4_link(ndvp, nvp, tmpname, cr, 7872 NULL, 0); 7873 } 7874 if (error == EOPNOTSUPP || !do_link) { 7875 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7876 cr, NULL, 0); 7877 did_link = 0; 7878 } else { 7879 did_link = 1; 7880 } 7881 if (error) { 7882 kmem_free(tmpname, MAXNAMELEN); 7883 VN_RELE(ovp); 7884 VN_RELE(nvp); 7885 nfs_rw_exit(&odrp->r_rwlock); 7886 nfs_rw_exit(&ndrp->r_rwlock); 7887 return (error); 7888 } 7889 7890 mutex_enter(&rp->r_statelock); 7891 if (rp->r_unldvp == NULL) { 7892 VN_HOLD(ndvp); 7893 rp->r_unldvp = ndvp; 7894 if (rp->r_unlcred != NULL) 7895 crfree(rp->r_unlcred); 7896 crhold(cr); 7897 rp->r_unlcred = cr; 7898 rp->r_unlname = tmpname; 7899 } else { 7900 if (rp->r_unlname) 7901 kmem_free(rp->r_unlname, MAXNAMELEN); 7902 rp->r_unlname = tmpname; 7903 } 7904 mutex_exit(&rp->r_statelock); 7905 } 7906 7907 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7908 7909 ASSERT(nfs4_consistent_type(nvp)); 7910 } 7911 7912 if (ovp == NULL) { 7913 /* 7914 * When renaming directories to be a subdirectory of a 7915 * different parent, the dnlc entry for ".." will no 7916 * longer be valid, so it must be removed. 7917 * 7918 * We do a lookup here to determine whether we are renaming 7919 * a directory and we need to check if we are renaming 7920 * an unlinked file. This might have already been done 7921 * in previous code, so we check ovp == NULL to avoid 7922 * doing it twice. 7923 */ 7924 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7925 /* 7926 * The source name *should* already exist. 7927 */ 7928 if (error) { 7929 nfs_rw_exit(&odrp->r_rwlock); 7930 nfs_rw_exit(&ndrp->r_rwlock); 7931 if (nvp) { 7932 VN_RELE(nvp); 7933 } 7934 return (error); 7935 } 7936 ASSERT(ovp != NULL); 7937 ASSERT(nfs4_consistent_type(ovp)); 7938 } 7939 7940 /* 7941 * Is the object being renamed a dir, and if so, is 7942 * it being renamed to a child of itself? The underlying 7943 * fs should ultimately return EINVAL for this case; 7944 * however, buggy beta non-Solaris NFSv4 servers at 7945 * interop testing events have allowed this behavior, 7946 * and it caused our client to panic due to a recursive 7947 * mutex_enter in fn_move. 7948 * 7949 * The tedious locking in fn_move could be changed to 7950 * deal with this case, and the client could avoid the 7951 * panic; however, the client would just confuse itself 7952 * later and misbehave. A better way to handle the broken 7953 * server is to detect this condition and return EINVAL 7954 * without ever sending the the bogus rename to the server. 7955 * We know the rename is invalid -- just fail it now. 7956 */ 7957 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7958 VN_RELE(ovp); 7959 nfs_rw_exit(&odrp->r_rwlock); 7960 nfs_rw_exit(&ndrp->r_rwlock); 7961 if (nvp) { 7962 VN_RELE(nvp); 7963 } 7964 return (EINVAL); 7965 } 7966 7967 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7968 7969 /* 7970 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7971 * possible for the filehandle to change due to the rename. 7972 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7973 * the fh will not change because of the rename, but we still need 7974 * to update its rnode entry with the new name for 7975 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7976 * has no effect on these for now, but for future improvements, 7977 * we might want to use it too to simplify handling of files 7978 * that are open with that flag on. (XXX) 7979 */ 7980 mi = VTOMI4(odvp); 7981 if (NFS4_VOLATILE_FH(mi)) 7982 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7983 &stat); 7984 else 7985 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7986 &stat); 7987 7988 ASSERT(nfs4_consistent_type(odvp)); 7989 ASSERT(nfs4_consistent_type(ndvp)); 7990 ASSERT(nfs4_consistent_type(ovp)); 7991 7992 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7993 do_link = 0; 7994 /* 7995 * Before the 'link_call' code, we did a nfs4_lookup 7996 * that puts a VN_HOLD on nvp. After the nfs4_link 7997 * call we call VN_RELE to match that hold. We need 7998 * to place an additional VN_HOLD here since we will 7999 * be hitting that VN_RELE again. 8000 */ 8001 VN_HOLD(nvp); 8002 8003 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0); 8004 8005 /* Undo the unlinked file naming stuff we just did */ 8006 mutex_enter(&rp->r_statelock); 8007 if (rp->r_unldvp) { 8008 VN_RELE(ndvp); 8009 rp->r_unldvp = NULL; 8010 if (rp->r_unlcred != NULL) 8011 crfree(rp->r_unlcred); 8012 rp->r_unlcred = NULL; 8013 /* rp->r_unlanme points to tmpname */ 8014 if (rp->r_unlname) 8015 kmem_free(rp->r_unlname, MAXNAMELEN); 8016 rp->r_unlname = NULL; 8017 } 8018 mutex_exit(&rp->r_statelock); 8019 8020 if (nvp) { 8021 VN_RELE(nvp); 8022 } 8023 goto link_call; 8024 } 8025 8026 if (error) { 8027 VN_RELE(ovp); 8028 nfs_rw_exit(&odrp->r_rwlock); 8029 nfs_rw_exit(&ndrp->r_rwlock); 8030 if (nvp) { 8031 VN_RELE(nvp); 8032 } 8033 return (error); 8034 } 8035 8036 /* 8037 * when renaming directories to be a subdirectory of a 8038 * different parent, the dnlc entry for ".." will no 8039 * longer be valid, so it must be removed 8040 */ 8041 rp = VTOR4(ovp); 8042 if (ndvp != odvp) { 8043 if (ovp->v_type == VDIR) { 8044 dnlc_remove(ovp, ".."); 8045 if (rp->r_dir != NULL) 8046 nfs4_purge_rddir_cache(ovp); 8047 } 8048 } 8049 8050 /* 8051 * If we are renaming the unlinked file, update the 8052 * r_unldvp and r_unlname as needed. 8053 */ 8054 mutex_enter(&rp->r_statelock); 8055 if (rp->r_unldvp != NULL) { 8056 if (strcmp(rp->r_unlname, onm) == 0) { 8057 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 8058 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 8059 if (ndvp != rp->r_unldvp) { 8060 VN_RELE(rp->r_unldvp); 8061 rp->r_unldvp = ndvp; 8062 VN_HOLD(ndvp); 8063 } 8064 } 8065 } 8066 mutex_exit(&rp->r_statelock); 8067 8068 /* 8069 * Notify the rename vnevents to source vnode, and to the target 8070 * vnode if it already existed. 8071 */ 8072 if (error == 0) { 8073 vnode_t *tvp; 8074 rnode4_t *trp; 8075 /* 8076 * Notify the vnode. Each links is represented by 8077 * a different vnode, in nfsv4. 8078 */ 8079 if (nvp) { 8080 trp = VTOR4(nvp); 8081 tvp = nvp; 8082 if (IS_SHADOW(nvp, trp)) 8083 tvp = RTOV4(trp); 8084 vnevent_rename_dest(tvp, ndvp, nnm, ct); 8085 } 8086 8087 /* 8088 * if the source and destination directory are not the 8089 * same notify the destination directory. 8090 */ 8091 if (VTOR4(odvp) != VTOR4(ndvp)) { 8092 trp = VTOR4(ndvp); 8093 tvp = ndvp; 8094 if (IS_SHADOW(ndvp, trp)) 8095 tvp = RTOV4(trp); 8096 vnevent_rename_dest_dir(tvp, ct); 8097 } 8098 8099 trp = VTOR4(ovp); 8100 tvp = ovp; 8101 if (IS_SHADOW(ovp, trp)) 8102 tvp = RTOV4(trp); 8103 vnevent_rename_src(tvp, odvp, onm, ct); 8104 } 8105 8106 if (nvp) { 8107 VN_RELE(nvp); 8108 } 8109 VN_RELE(ovp); 8110 8111 nfs_rw_exit(&odrp->r_rwlock); 8112 nfs_rw_exit(&ndrp->r_rwlock); 8113 8114 return (error); 8115 } 8116 8117 /* 8118 * When the parent directory has changed, sv_dfh must be updated 8119 */ 8120 static void 8121 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp) 8122 { 8123 svnode_t *sv = VTOSV(vp); 8124 nfs4_sharedfh_t *old_dfh = sv->sv_dfh; 8125 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh; 8126 8127 sfh4_hold(new_dfh); 8128 sv->sv_dfh = new_dfh; 8129 sfh4_rele(&old_dfh); 8130 } 8131 8132 /* 8133 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 8134 * when it is known that the filehandle is persistent through rename. 8135 * 8136 * Rename requires that the current fh be the target directory and the 8137 * saved fh be the source directory. After the operation, the current fh 8138 * is unchanged. 8139 * The compound op structure for persistent fh rename is: 8140 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 8141 * Rather than bother with the directory postop args, we'll simply 8142 * update that a change occurred in the cache, so no post-op getattrs. 8143 */ 8144 static int 8145 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 8146 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8147 { 8148 COMPOUND4args_clnt args; 8149 COMPOUND4res_clnt res, *resp = NULL; 8150 nfs_argop4 *argop; 8151 nfs_resop4 *resop; 8152 int doqueue, argoplist_size; 8153 mntinfo4_t *mi; 8154 rnode4_t *odrp = VTOR4(odvp); 8155 rnode4_t *ndrp = VTOR4(ndvp); 8156 RENAME4res *rn_res; 8157 bool_t needrecov; 8158 nfs4_recov_state_t recov_state; 8159 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8160 dirattr_info_t dinfo, *dinfop; 8161 8162 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8163 8164 recov_state.rs_flags = 0; 8165 recov_state.rs_num_retry_despite_err = 0; 8166 8167 /* 8168 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 8169 * 8170 * If source/target are different dirs, then append putfh(src); getattr 8171 */ 8172 args.array_len = (odvp == ndvp) ? 5 : 7; 8173 argoplist_size = args.array_len * sizeof (nfs_argop4); 8174 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8175 8176 recov_retry: 8177 *statp = NFS4_OK; 8178 8179 /* No need to Lookup the file, persistent fh */ 8180 args.ctag = TAG_RENAME; 8181 8182 mi = VTOMI4(odvp); 8183 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8184 if (e.error) { 8185 kmem_free(argop, argoplist_size); 8186 return (e.error); 8187 } 8188 8189 /* 0: putfh source directory */ 8190 argop[0].argop = OP_CPUTFH; 8191 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8192 8193 /* 1: Save source fh to free up current for target */ 8194 argop[1].argop = OP_SAVEFH; 8195 8196 /* 2: putfh targetdir */ 8197 argop[2].argop = OP_CPUTFH; 8198 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8199 8200 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8201 argop[3].argop = OP_CRENAME; 8202 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8203 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8204 8205 /* 4: getattr (targetdir) */ 8206 argop[4].argop = OP_GETATTR; 8207 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8208 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8209 8210 if (ndvp != odvp) { 8211 8212 /* 5: putfh (sourcedir) */ 8213 argop[5].argop = OP_CPUTFH; 8214 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8215 8216 /* 6: getattr (sourcedir) */ 8217 argop[6].argop = OP_GETATTR; 8218 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8219 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8220 } 8221 8222 dnlc_remove(odvp, onm); 8223 dnlc_remove(ndvp, nnm); 8224 8225 doqueue = 1; 8226 dinfo.di_time_call = gethrtime(); 8227 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8228 8229 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8230 if (e.error) { 8231 PURGE_ATTRCACHE4(odvp); 8232 PURGE_ATTRCACHE4(ndvp); 8233 } else { 8234 *statp = res.status; 8235 } 8236 8237 if (needrecov) { 8238 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8239 OP_RENAME, NULL, NULL, NULL) == FALSE) { 8240 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8241 if (!e.error) 8242 (void) xdr_free(xdr_COMPOUND4res_clnt, 8243 (caddr_t)&res); 8244 goto recov_retry; 8245 } 8246 } 8247 8248 if (!e.error) { 8249 resp = &res; 8250 /* 8251 * as long as OP_RENAME 8252 */ 8253 if (res.status != NFS4_OK && res.array_len <= 4) { 8254 e.error = geterrno4(res.status); 8255 PURGE_ATTRCACHE4(odvp); 8256 PURGE_ATTRCACHE4(ndvp); 8257 /* 8258 * System V defines rename to return EEXIST, not 8259 * ENOTEMPTY if the target directory is not empty. 8260 * Over the wire, the error is NFSERR_ENOTEMPTY 8261 * which geterrno4 maps to ENOTEMPTY. 8262 */ 8263 if (e.error == ENOTEMPTY) 8264 e.error = EEXIST; 8265 } else { 8266 8267 resop = &res.array[3]; /* rename res */ 8268 rn_res = &resop->nfs_resop4_u.oprename; 8269 8270 if (res.status == NFS4_OK) { 8271 /* 8272 * Update target attribute, readdir and dnlc 8273 * caches. 8274 */ 8275 dinfo.di_garp = 8276 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8277 dinfo.di_cred = cr; 8278 dinfop = &dinfo; 8279 } else 8280 dinfop = NULL; 8281 8282 nfs4_update_dircaches(&rn_res->target_cinfo, 8283 ndvp, NULL, NULL, dinfop); 8284 8285 /* 8286 * Update source attribute, readdir and dnlc caches 8287 * 8288 */ 8289 if (ndvp != odvp) { 8290 update_parentdir_sfh(renvp, ndvp); 8291 8292 if (dinfop) 8293 dinfo.di_garp = 8294 &(res.array[6].nfs_resop4_u. 8295 opgetattr.ga_res); 8296 8297 nfs4_update_dircaches(&rn_res->source_cinfo, 8298 odvp, NULL, NULL, dinfop); 8299 } 8300 8301 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8302 nnm); 8303 } 8304 } 8305 8306 if (resp) 8307 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8308 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8309 kmem_free(argop, argoplist_size); 8310 8311 return (e.error); 8312 } 8313 8314 /* 8315 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8316 * it is possible for the filehandle to change due to the rename. 8317 * 8318 * The compound req in this case includes a post-rename lookup and getattr 8319 * to ensure that we have the correct fh and attributes for the object. 8320 * 8321 * Rename requires that the current fh be the target directory and the 8322 * saved fh be the source directory. After the operation, the current fh 8323 * is unchanged. 8324 * 8325 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8326 * update the filehandle for the renamed object. We also get the old 8327 * filehandle for historical reasons; this should be taken out sometime. 8328 * This results in a rather cumbersome compound... 8329 * 8330 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8331 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8332 * 8333 */ 8334 static int 8335 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8336 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8337 { 8338 COMPOUND4args_clnt args; 8339 COMPOUND4res_clnt res, *resp = NULL; 8340 int argoplist_size; 8341 nfs_argop4 *argop; 8342 nfs_resop4 *resop; 8343 int doqueue; 8344 mntinfo4_t *mi; 8345 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8346 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8347 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8348 RENAME4res *rn_res; 8349 GETFH4res *ngf_res; 8350 bool_t needrecov; 8351 nfs4_recov_state_t recov_state; 8352 hrtime_t t; 8353 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8354 dirattr_info_t dinfo, *dinfop = &dinfo; 8355 8356 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8357 8358 recov_state.rs_flags = 0; 8359 recov_state.rs_num_retry_despite_err = 0; 8360 8361 recov_retry: 8362 *statp = NFS4_OK; 8363 8364 /* 8365 * There is a window between the RPC and updating the path and 8366 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8367 * code, so that it doesn't try to use the old path during that 8368 * window. 8369 */ 8370 mutex_enter(&orp->r_statelock); 8371 while (orp->r_flags & R4RECEXPFH) { 8372 klwp_t *lwp = ttolwp(curthread); 8373 8374 if (lwp != NULL) 8375 lwp->lwp_nostop++; 8376 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8377 mutex_exit(&orp->r_statelock); 8378 if (lwp != NULL) 8379 lwp->lwp_nostop--; 8380 return (EINTR); 8381 } 8382 if (lwp != NULL) 8383 lwp->lwp_nostop--; 8384 } 8385 orp->r_flags |= R4RECEXPFH; 8386 mutex_exit(&orp->r_statelock); 8387 8388 mi = VTOMI4(odvp); 8389 8390 args.ctag = TAG_RENAME_VFH; 8391 args.array_len = (odvp == ndvp) ? 10 : 12; 8392 argoplist_size = args.array_len * sizeof (nfs_argop4); 8393 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8394 8395 /* 8396 * Rename ops: 8397 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8398 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8399 * LOOKUP(trgt), GETFH(new), GETATTR, 8400 * 8401 * if (odvp != ndvp) 8402 * add putfh(sourcedir), getattr(sourcedir) } 8403 */ 8404 args.array = argop; 8405 8406 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8407 &recov_state, NULL); 8408 if (e.error) { 8409 kmem_free(argop, argoplist_size); 8410 mutex_enter(&orp->r_statelock); 8411 orp->r_flags &= ~R4RECEXPFH; 8412 cv_broadcast(&orp->r_cv); 8413 mutex_exit(&orp->r_statelock); 8414 return (e.error); 8415 } 8416 8417 /* 0: putfh source directory */ 8418 argop[0].argop = OP_CPUTFH; 8419 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8420 8421 /* 1: Save source fh to free up current for target */ 8422 argop[1].argop = OP_SAVEFH; 8423 8424 /* 2: Lookup pre-rename fh of renamed object */ 8425 argop[2].argop = OP_CLOOKUP; 8426 argop[2].nfs_argop4_u.opclookup.cname = onm; 8427 8428 /* 3: getfh fh of renamed object (before rename) */ 8429 argop[3].argop = OP_GETFH; 8430 8431 /* 4: putfh targetdir */ 8432 argop[4].argop = OP_CPUTFH; 8433 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8434 8435 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8436 argop[5].argop = OP_CRENAME; 8437 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8438 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8439 8440 /* 6: getattr of target dir (post op attrs) */ 8441 argop[6].argop = OP_GETATTR; 8442 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8443 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8444 8445 /* 7: Lookup post-rename fh of renamed object */ 8446 argop[7].argop = OP_CLOOKUP; 8447 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8448 8449 /* 8: getfh fh of renamed object (after rename) */ 8450 argop[8].argop = OP_GETFH; 8451 8452 /* 9: getattr of renamed object */ 8453 argop[9].argop = OP_GETATTR; 8454 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8455 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8456 8457 /* 8458 * If source/target dirs are different, then get new post-op 8459 * attrs for source dir also. 8460 */ 8461 if (ndvp != odvp) { 8462 /* 10: putfh (sourcedir) */ 8463 argop[10].argop = OP_CPUTFH; 8464 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8465 8466 /* 11: getattr (sourcedir) */ 8467 argop[11].argop = OP_GETATTR; 8468 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8469 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8470 } 8471 8472 dnlc_remove(odvp, onm); 8473 dnlc_remove(ndvp, nnm); 8474 8475 doqueue = 1; 8476 t = gethrtime(); 8477 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8478 8479 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8480 if (e.error) { 8481 PURGE_ATTRCACHE4(odvp); 8482 PURGE_ATTRCACHE4(ndvp); 8483 if (!needrecov) { 8484 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8485 &recov_state, needrecov); 8486 goto out; 8487 } 8488 } else { 8489 *statp = res.status; 8490 } 8491 8492 if (needrecov) { 8493 bool_t abort; 8494 8495 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8496 OP_RENAME, NULL, NULL, NULL); 8497 if (abort == FALSE) { 8498 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8499 &recov_state, needrecov); 8500 kmem_free(argop, argoplist_size); 8501 if (!e.error) 8502 (void) xdr_free(xdr_COMPOUND4res_clnt, 8503 (caddr_t)&res); 8504 mutex_enter(&orp->r_statelock); 8505 orp->r_flags &= ~R4RECEXPFH; 8506 cv_broadcast(&orp->r_cv); 8507 mutex_exit(&orp->r_statelock); 8508 goto recov_retry; 8509 } else { 8510 if (e.error != 0) { 8511 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8512 &recov_state, needrecov); 8513 goto out; 8514 } 8515 /* fall through for res.status case */ 8516 } 8517 } 8518 8519 resp = &res; 8520 /* 8521 * If OP_RENAME (or any prev op) failed, then return an error. 8522 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8523 */ 8524 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8525 /* 8526 * Error in an op other than last Getattr 8527 */ 8528 e.error = geterrno4(res.status); 8529 PURGE_ATTRCACHE4(odvp); 8530 PURGE_ATTRCACHE4(ndvp); 8531 /* 8532 * System V defines rename to return EEXIST, not 8533 * ENOTEMPTY if the target directory is not empty. 8534 * Over the wire, the error is NFSERR_ENOTEMPTY 8535 * which geterrno4 maps to ENOTEMPTY. 8536 */ 8537 if (e.error == ENOTEMPTY) 8538 e.error = EEXIST; 8539 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8540 needrecov); 8541 goto out; 8542 } 8543 8544 /* rename results */ 8545 rn_res = &res.array[5].nfs_resop4_u.oprename; 8546 8547 if (res.status == NFS4_OK) { 8548 /* Update target attribute, readdir and dnlc caches */ 8549 dinfo.di_garp = 8550 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8551 dinfo.di_cred = cr; 8552 dinfo.di_time_call = t; 8553 } else 8554 dinfop = NULL; 8555 8556 /* Update source cache attribute, readdir and dnlc caches */ 8557 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8558 8559 /* Update source cache attribute, readdir and dnlc caches */ 8560 if (ndvp != odvp) { 8561 update_parentdir_sfh(ovp, ndvp); 8562 8563 /* 8564 * If dinfop is non-NULL, then compound succeded, so 8565 * set di_garp to attrs for source dir. dinfop is only 8566 * set to NULL when compound fails. 8567 */ 8568 if (dinfop) 8569 dinfo.di_garp = 8570 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8571 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8572 dinfop); 8573 } 8574 8575 /* 8576 * Update the rnode with the new component name and args, 8577 * and if the file handle changed, also update it with the new fh. 8578 * This is only necessary if the target object has an rnode 8579 * entry and there is no need to create one for it. 8580 */ 8581 resop = &res.array[8]; /* getfh new res */ 8582 ngf_res = &resop->nfs_resop4_u.opgetfh; 8583 8584 /* 8585 * Update the path and filehandle for the renamed object. 8586 */ 8587 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8588 8589 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8590 8591 if (res.status == NFS4_OK) { 8592 resop++; /* getattr res */ 8593 e.error = nfs4_update_attrcache(res.status, 8594 &resop->nfs_resop4_u.opgetattr.ga_res, 8595 t, ovp, cr); 8596 } 8597 8598 out: 8599 kmem_free(argop, argoplist_size); 8600 if (resp) 8601 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8602 mutex_enter(&orp->r_statelock); 8603 orp->r_flags &= ~R4RECEXPFH; 8604 cv_broadcast(&orp->r_cv); 8605 mutex_exit(&orp->r_statelock); 8606 8607 return (e.error); 8608 } 8609 8610 /* ARGSUSED */ 8611 static int 8612 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr, 8613 caller_context_t *ct, int flags, vsecattr_t *vsecp) 8614 { 8615 int error; 8616 vnode_t *vp; 8617 8618 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8619 return (EPERM); 8620 /* 8621 * As ".." has special meaning and rather than send a mkdir 8622 * over the wire to just let the server freak out, we just 8623 * short circuit it here and return EEXIST 8624 */ 8625 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8626 return (EEXIST); 8627 8628 /* 8629 * Decision to get the right gid and setgid bit of the 8630 * new directory is now made in call_nfs4_create_req. 8631 */ 8632 va->va_mask |= AT_MODE; 8633 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8634 if (error) 8635 return (error); 8636 8637 *vpp = vp; 8638 return (0); 8639 } 8640 8641 8642 /* 8643 * rmdir is using the same remove v4 op as does remove. 8644 * Remove requires that the current fh be the target directory. 8645 * After the operation, the current fh is unchanged. 8646 * The compound op structure is: 8647 * PUTFH(targetdir), REMOVE 8648 */ 8649 /*ARGSUSED4*/ 8650 static int 8651 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr, 8652 caller_context_t *ct, int flags) 8653 { 8654 int need_end_op = FALSE; 8655 COMPOUND4args_clnt args; 8656 COMPOUND4res_clnt res, *resp = NULL; 8657 REMOVE4res *rm_res; 8658 nfs_argop4 argop[3]; 8659 nfs_resop4 *resop; 8660 vnode_t *vp; 8661 int doqueue; 8662 mntinfo4_t *mi; 8663 rnode4_t *drp; 8664 bool_t needrecov = FALSE; 8665 nfs4_recov_state_t recov_state; 8666 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8667 dirattr_info_t dinfo, *dinfop; 8668 8669 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8670 return (EPERM); 8671 /* 8672 * As ".." has special meaning and rather than send a rmdir 8673 * over the wire to just let the server freak out, we just 8674 * short circuit it here and return EEXIST 8675 */ 8676 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8677 return (EEXIST); 8678 8679 drp = VTOR4(dvp); 8680 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8681 return (EINTR); 8682 8683 /* 8684 * Attempt to prevent a rmdir(".") from succeeding. 8685 */ 8686 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8687 if (e.error) { 8688 nfs_rw_exit(&drp->r_rwlock); 8689 return (e.error); 8690 } 8691 if (vp == cdir) { 8692 VN_RELE(vp); 8693 nfs_rw_exit(&drp->r_rwlock); 8694 return (EINVAL); 8695 } 8696 8697 /* 8698 * Since nfsv4 remove op works on both files and directories, 8699 * check that the removed object is indeed a directory. 8700 */ 8701 if (vp->v_type != VDIR) { 8702 VN_RELE(vp); 8703 nfs_rw_exit(&drp->r_rwlock); 8704 return (ENOTDIR); 8705 } 8706 8707 /* 8708 * First just remove the entry from the name cache, as it 8709 * is most likely an entry for this vp. 8710 */ 8711 dnlc_remove(dvp, nm); 8712 8713 /* 8714 * If there vnode reference count is greater than one, then 8715 * there may be additional references in the DNLC which will 8716 * need to be purged. First, trying removing the entry for 8717 * the parent directory and see if that removes the additional 8718 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8719 * to completely remove any references to the directory which 8720 * might still exist in the DNLC. 8721 */ 8722 if (vp->v_count > 1) { 8723 dnlc_remove(vp, ".."); 8724 if (vp->v_count > 1) 8725 dnlc_purge_vp(vp); 8726 } 8727 8728 mi = VTOMI4(dvp); 8729 recov_state.rs_flags = 0; 8730 recov_state.rs_num_retry_despite_err = 0; 8731 8732 recov_retry: 8733 args.ctag = TAG_RMDIR; 8734 8735 /* 8736 * Rmdir ops: putfh dir; remove 8737 */ 8738 args.array_len = 3; 8739 args.array = argop; 8740 8741 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8742 if (e.error) { 8743 nfs_rw_exit(&drp->r_rwlock); 8744 return (e.error); 8745 } 8746 need_end_op = TRUE; 8747 8748 /* putfh directory */ 8749 argop[0].argop = OP_CPUTFH; 8750 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8751 8752 /* remove */ 8753 argop[1].argop = OP_CREMOVE; 8754 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8755 8756 /* getattr (postop attrs for dir that contained removed dir) */ 8757 argop[2].argop = OP_GETATTR; 8758 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8759 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8760 8761 dinfo.di_time_call = gethrtime(); 8762 doqueue = 1; 8763 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8764 8765 PURGE_ATTRCACHE4(vp); 8766 8767 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8768 if (e.error) { 8769 PURGE_ATTRCACHE4(dvp); 8770 } 8771 8772 if (needrecov) { 8773 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8774 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 8775 if (!e.error) 8776 (void) xdr_free(xdr_COMPOUND4res_clnt, 8777 (caddr_t)&res); 8778 8779 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8780 needrecov); 8781 need_end_op = FALSE; 8782 goto recov_retry; 8783 } 8784 } 8785 8786 if (!e.error) { 8787 resp = &res; 8788 8789 /* 8790 * Only return error if first 2 ops (OP_REMOVE or earlier) 8791 * failed. 8792 */ 8793 if (res.status != NFS4_OK && res.array_len <= 2) { 8794 e.error = geterrno4(res.status); 8795 PURGE_ATTRCACHE4(dvp); 8796 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8797 &recov_state, needrecov); 8798 need_end_op = FALSE; 8799 nfs4_purge_stale_fh(e.error, dvp, cr); 8800 /* 8801 * System V defines rmdir to return EEXIST, not 8802 * ENOTEMPTY if the directory is not empty. Over 8803 * the wire, the error is NFSERR_ENOTEMPTY which 8804 * geterrno4 maps to ENOTEMPTY. 8805 */ 8806 if (e.error == ENOTEMPTY) 8807 e.error = EEXIST; 8808 } else { 8809 resop = &res.array[1]; /* remove res */ 8810 rm_res = &resop->nfs_resop4_u.opremove; 8811 8812 if (res.status == NFS4_OK) { 8813 resop = &res.array[2]; /* dir attrs */ 8814 dinfo.di_garp = 8815 &resop->nfs_resop4_u.opgetattr.ga_res; 8816 dinfo.di_cred = cr; 8817 dinfop = &dinfo; 8818 } else 8819 dinfop = NULL; 8820 8821 /* Update dir attribute, readdir and dnlc caches */ 8822 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8823 dinfop); 8824 8825 /* destroy rddir cache for dir that was removed */ 8826 if (VTOR4(vp)->r_dir != NULL) 8827 nfs4_purge_rddir_cache(vp); 8828 } 8829 } 8830 8831 if (need_end_op) 8832 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8833 8834 nfs_rw_exit(&drp->r_rwlock); 8835 8836 if (resp) 8837 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8838 8839 if (e.error == 0) { 8840 vnode_t *tvp; 8841 rnode4_t *trp; 8842 trp = VTOR4(vp); 8843 tvp = vp; 8844 if (IS_SHADOW(vp, trp)) 8845 tvp = RTOV4(trp); 8846 vnevent_rmdir(tvp, dvp, nm, ct); 8847 } 8848 8849 VN_RELE(vp); 8850 8851 return (e.error); 8852 } 8853 8854 /* ARGSUSED */ 8855 static int 8856 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr, 8857 caller_context_t *ct, int flags) 8858 { 8859 int error; 8860 vnode_t *vp; 8861 rnode4_t *rp; 8862 char *contents; 8863 mntinfo4_t *mi = VTOMI4(dvp); 8864 8865 if (nfs_zone() != mi->mi_zone) 8866 return (EPERM); 8867 if (!(mi->mi_flags & MI4_SYMLINK)) 8868 return (EOPNOTSUPP); 8869 8870 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8871 if (error) 8872 return (error); 8873 8874 ASSERT(nfs4_consistent_type(vp)); 8875 rp = VTOR4(vp); 8876 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8877 8878 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8879 8880 if (contents != NULL) { 8881 mutex_enter(&rp->r_statelock); 8882 if (rp->r_symlink.contents == NULL) { 8883 rp->r_symlink.len = strlen(tnm); 8884 bcopy(tnm, contents, rp->r_symlink.len); 8885 rp->r_symlink.contents = contents; 8886 rp->r_symlink.size = MAXPATHLEN; 8887 mutex_exit(&rp->r_statelock); 8888 } else { 8889 mutex_exit(&rp->r_statelock); 8890 kmem_free((void *)contents, MAXPATHLEN); 8891 } 8892 } 8893 } 8894 VN_RELE(vp); 8895 8896 return (error); 8897 } 8898 8899 8900 /* 8901 * Read directory entries. 8902 * There are some weird things to look out for here. The uio_loffset 8903 * field is either 0 or it is the offset returned from a previous 8904 * readdir. It is an opaque value used by the server to find the 8905 * correct directory block to read. The count field is the number 8906 * of blocks to read on the server. This is advisory only, the server 8907 * may return only one block's worth of entries. Entries may be compressed 8908 * on the server. 8909 */ 8910 /* ARGSUSED */ 8911 static int 8912 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp, 8913 caller_context_t *ct, int flags) 8914 { 8915 int error; 8916 uint_t count; 8917 rnode4_t *rp; 8918 rddir4_cache *rdc; 8919 rddir4_cache *rrdc; 8920 8921 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8922 return (EIO); 8923 rp = VTOR4(vp); 8924 8925 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8926 8927 /* 8928 * Make sure that the directory cache is valid. 8929 */ 8930 if (rp->r_dir != NULL) { 8931 if (nfs_disable_rddir_cache != 0) { 8932 /* 8933 * Setting nfs_disable_rddir_cache in /etc/system 8934 * allows interoperability with servers that do not 8935 * properly update the attributes of directories. 8936 * Any cached information gets purged before an 8937 * access is made to it. 8938 */ 8939 nfs4_purge_rddir_cache(vp); 8940 } 8941 8942 error = nfs4_validate_caches(vp, cr); 8943 if (error) 8944 return (error); 8945 } 8946 8947 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8948 8949 /* 8950 * Short circuit last readdir which always returns 0 bytes. 8951 * This can be done after the directory has been read through 8952 * completely at least once. This will set r_direof which 8953 * can be used to find the value of the last cookie. 8954 */ 8955 mutex_enter(&rp->r_statelock); 8956 if (rp->r_direof != NULL && 8957 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8958 mutex_exit(&rp->r_statelock); 8959 #ifdef DEBUG 8960 nfs4_readdir_cache_shorts++; 8961 #endif 8962 if (eofp) 8963 *eofp = 1; 8964 return (0); 8965 } 8966 8967 /* 8968 * Look for a cache entry. Cache entries are identified 8969 * by the NFS cookie value and the byte count requested. 8970 */ 8971 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8972 8973 /* 8974 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8975 */ 8976 if (rdc == NULL) { 8977 mutex_exit(&rp->r_statelock); 8978 return (EINTR); 8979 } 8980 8981 /* 8982 * Check to see if we need to fill this entry in. 8983 */ 8984 if (rdc->flags & RDDIRREQ) { 8985 rdc->flags &= ~RDDIRREQ; 8986 rdc->flags |= RDDIR; 8987 mutex_exit(&rp->r_statelock); 8988 8989 /* 8990 * Do the readdir. 8991 */ 8992 nfs4readdir(vp, rdc, cr); 8993 8994 /* 8995 * Reacquire the lock, so that we can continue 8996 */ 8997 mutex_enter(&rp->r_statelock); 8998 /* 8999 * The entry is now complete 9000 */ 9001 rdc->flags &= ~RDDIR; 9002 } 9003 9004 ASSERT(!(rdc->flags & RDDIR)); 9005 9006 /* 9007 * If an error occurred while attempting 9008 * to fill the cache entry, mark the entry invalid and 9009 * just return the error. 9010 */ 9011 if (rdc->error) { 9012 error = rdc->error; 9013 rdc->flags |= RDDIRREQ; 9014 rddir4_cache_rele(rp, rdc); 9015 mutex_exit(&rp->r_statelock); 9016 return (error); 9017 } 9018 9019 /* 9020 * The cache entry is complete and good, 9021 * copyout the dirent structs to the calling 9022 * thread. 9023 */ 9024 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 9025 9026 /* 9027 * If no error occurred during the copyout, 9028 * update the offset in the uio struct to 9029 * contain the value of the next NFS 4 cookie 9030 * and set the eof value appropriately. 9031 */ 9032 if (!error) { 9033 uiop->uio_loffset = rdc->nfs4_ncookie; 9034 if (eofp) 9035 *eofp = rdc->eof; 9036 } 9037 9038 /* 9039 * Decide whether to do readahead. Don't if we 9040 * have already read to the end of directory. 9041 */ 9042 if (rdc->eof) { 9043 /* 9044 * Make the entry the direof only if it is cached 9045 */ 9046 if (rdc->flags & RDDIRCACHED) 9047 rp->r_direof = rdc; 9048 rddir4_cache_rele(rp, rdc); 9049 mutex_exit(&rp->r_statelock); 9050 return (error); 9051 } 9052 9053 /* Determine if a readdir readahead should be done */ 9054 if (!(rp->r_flags & R4LOOKUP)) { 9055 rddir4_cache_rele(rp, rdc); 9056 mutex_exit(&rp->r_statelock); 9057 return (error); 9058 } 9059 9060 /* 9061 * Now look for a readahead entry. 9062 * 9063 * Check to see whether we found an entry for the readahead. 9064 * If so, we don't need to do anything further, so free the new 9065 * entry if one was allocated. Otherwise, allocate a new entry, add 9066 * it to the cache, and then initiate an asynchronous readdir 9067 * operation to fill it. 9068 */ 9069 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 9070 9071 /* 9072 * A readdir cache entry could not be obtained for the readahead. In 9073 * this case we skip the readahead and return. 9074 */ 9075 if (rrdc == NULL) { 9076 rddir4_cache_rele(rp, rdc); 9077 mutex_exit(&rp->r_statelock); 9078 return (error); 9079 } 9080 9081 /* 9082 * Check to see if we need to fill this entry in. 9083 */ 9084 if (rrdc->flags & RDDIRREQ) { 9085 rrdc->flags &= ~RDDIRREQ; 9086 rrdc->flags |= RDDIR; 9087 rddir4_cache_rele(rp, rdc); 9088 mutex_exit(&rp->r_statelock); 9089 #ifdef DEBUG 9090 nfs4_readdir_readahead++; 9091 #endif 9092 /* 9093 * Do the readdir. 9094 */ 9095 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 9096 return (error); 9097 } 9098 9099 rddir4_cache_rele(rp, rrdc); 9100 rddir4_cache_rele(rp, rdc); 9101 mutex_exit(&rp->r_statelock); 9102 return (error); 9103 } 9104 9105 static int 9106 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9107 { 9108 int error; 9109 rnode4_t *rp; 9110 9111 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9112 9113 rp = VTOR4(vp); 9114 9115 /* 9116 * Obtain the readdir results for the caller. 9117 */ 9118 nfs4readdir(vp, rdc, cr); 9119 9120 mutex_enter(&rp->r_statelock); 9121 /* 9122 * The entry is now complete 9123 */ 9124 rdc->flags &= ~RDDIR; 9125 9126 error = rdc->error; 9127 if (error) 9128 rdc->flags |= RDDIRREQ; 9129 rddir4_cache_rele(rp, rdc); 9130 mutex_exit(&rp->r_statelock); 9131 9132 return (error); 9133 } 9134 9135 /* 9136 * Read directory entries. 9137 * There are some weird things to look out for here. The uio_loffset 9138 * field is either 0 or it is the offset returned from a previous 9139 * readdir. It is an opaque value used by the server to find the 9140 * correct directory block to read. The count field is the number 9141 * of blocks to read on the server. This is advisory only, the server 9142 * may return only one block's worth of entries. Entries may be compressed 9143 * on the server. 9144 * 9145 * Generates the following compound request: 9146 * 1. If readdir offset is zero and no dnlc entry for parent exists, 9147 * must include a Lookupp as well. In this case, send: 9148 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 9149 * 2. Otherwise just do: { Putfh <fh>; Readdir } 9150 * 9151 * Get complete attributes and filehandles for entries if this is the 9152 * first read of the directory. Otherwise, just get fileid's. 9153 */ 9154 static void 9155 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9156 { 9157 COMPOUND4args_clnt args; 9158 COMPOUND4res_clnt res; 9159 READDIR4args *rargs; 9160 READDIR4res_clnt *rd_res; 9161 bitmap4 rd_bitsval; 9162 nfs_argop4 argop[5]; 9163 nfs_resop4 *resop; 9164 rnode4_t *rp = VTOR4(vp); 9165 mntinfo4_t *mi = VTOMI4(vp); 9166 int doqueue; 9167 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 9168 vnode_t *dvp; 9169 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 9170 int num_ops, res_opcnt; 9171 bool_t needrecov = FALSE; 9172 nfs4_recov_state_t recov_state; 9173 hrtime_t t; 9174 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 9175 9176 ASSERT(nfs_zone() == mi->mi_zone); 9177 ASSERT(rdc->flags & RDDIR); 9178 ASSERT(rdc->entries == NULL); 9179 9180 /* 9181 * If rp were a stub, it should have triggered and caused 9182 * a mount for us to get this far. 9183 */ 9184 ASSERT(!RP_ISSTUB(rp)); 9185 9186 num_ops = 2; 9187 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9188 /* 9189 * Since nfsv4 readdir may not return entries for "." and "..", 9190 * the client must recreate them: 9191 * To find the correct nodeid, do the following: 9192 * For current node, get nodeid from dnlc. 9193 * - if current node is rootvp, set pnodeid to nodeid. 9194 * - else if parent is in the dnlc, get its nodeid from there. 9195 * - else add LOOKUPP+GETATTR to compound. 9196 */ 9197 nodeid = rp->r_attr.va_nodeid; 9198 if (vp->v_flag & VROOT) { 9199 pnodeid = nodeid; /* root of mount point */ 9200 } else { 9201 dvp = dnlc_lookup(vp, ".."); 9202 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9203 /* parent in dnlc cache - no need for otw */ 9204 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9205 } else { 9206 /* 9207 * parent not in dnlc cache, 9208 * do lookupp to get its id 9209 */ 9210 num_ops = 5; 9211 pnodeid = 0; /* set later by getattr parent */ 9212 } 9213 if (dvp) 9214 VN_RELE(dvp); 9215 } 9216 } 9217 recov_state.rs_flags = 0; 9218 recov_state.rs_num_retry_despite_err = 0; 9219 9220 /* Save the original mount point security flavor */ 9221 (void) save_mnt_secinfo(mi->mi_curr_serv); 9222 9223 recov_retry: 9224 args.ctag = TAG_READDIR; 9225 9226 args.array = argop; 9227 args.array_len = num_ops; 9228 9229 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9230 &recov_state, NULL)) { 9231 /* 9232 * If readdir a node that is a stub for a crossed mount point, 9233 * keep the original secinfo flavor for the current file 9234 * system, not the crossed one. 9235 */ 9236 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9237 rdc->error = e.error; 9238 return; 9239 } 9240 9241 /* 9242 * Determine which attrs to request for dirents. This code 9243 * must be protected by nfs4_start/end_fop because of r_server 9244 * (which will change during failover recovery). 9245 * 9246 */ 9247 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9248 /* 9249 * Get all vattr attrs plus filehandle and rdattr_error 9250 */ 9251 rd_bitsval = NFS4_VATTR_MASK | 9252 FATTR4_RDATTR_ERROR_MASK | 9253 FATTR4_FILEHANDLE_MASK; 9254 9255 if (rp->r_flags & R4READDIRWATTR) { 9256 mutex_enter(&rp->r_statelock); 9257 rp->r_flags &= ~R4READDIRWATTR; 9258 mutex_exit(&rp->r_statelock); 9259 } 9260 } else { 9261 servinfo4_t *svp = rp->r_server; 9262 9263 /* 9264 * Already read directory. Use readdir with 9265 * no attrs (except for mounted_on_fileid) for updates. 9266 */ 9267 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9268 9269 /* 9270 * request mounted on fileid if supported, else request 9271 * fileid. maybe we should verify that fileid is supported 9272 * and request something else if not. 9273 */ 9274 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9275 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9276 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9277 nfs_rw_exit(&svp->sv_lock); 9278 } 9279 9280 /* putfh directory fh */ 9281 argop[0].argop = OP_CPUTFH; 9282 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9283 9284 argop[1].argop = OP_READDIR; 9285 rargs = &argop[1].nfs_argop4_u.opreaddir; 9286 /* 9287 * 1 and 2 are reserved for client "." and ".." entry offset. 9288 * cookie 0 should be used over-the-wire to start reading at 9289 * the beginning of the directory excluding "." and "..". 9290 */ 9291 if (rdc->nfs4_cookie == 0 || 9292 rdc->nfs4_cookie == 1 || 9293 rdc->nfs4_cookie == 2) { 9294 rargs->cookie = (nfs_cookie4)0; 9295 rargs->cookieverf = 0; 9296 } else { 9297 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9298 mutex_enter(&rp->r_statelock); 9299 rargs->cookieverf = rp->r_cookieverf4; 9300 mutex_exit(&rp->r_statelock); 9301 } 9302 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9303 rargs->maxcount = mi->mi_tsize; 9304 rargs->attr_request = rd_bitsval; 9305 rargs->rdc = rdc; 9306 rargs->dvp = vp; 9307 rargs->mi = mi; 9308 rargs->cr = cr; 9309 9310 9311 /* 9312 * If count < than the minimum required, we return no entries 9313 * and fail with EINVAL 9314 */ 9315 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9316 rdc->error = EINVAL; 9317 goto out; 9318 } 9319 9320 if (args.array_len == 5) { 9321 /* 9322 * Add lookupp and getattr for parent nodeid. 9323 */ 9324 argop[2].argop = OP_LOOKUPP; 9325 9326 argop[3].argop = OP_GETFH; 9327 9328 /* getattr parent */ 9329 argop[4].argop = OP_GETATTR; 9330 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9331 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9332 } 9333 9334 doqueue = 1; 9335 9336 if (mi->mi_io_kstats) { 9337 mutex_enter(&mi->mi_lock); 9338 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9339 mutex_exit(&mi->mi_lock); 9340 } 9341 9342 /* capture the time of this call */ 9343 rargs->t = t = gethrtime(); 9344 9345 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9346 9347 if (mi->mi_io_kstats) { 9348 mutex_enter(&mi->mi_lock); 9349 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9350 mutex_exit(&mi->mi_lock); 9351 } 9352 9353 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9354 9355 /* 9356 * If RPC error occurred and it isn't an error that 9357 * triggers recovery, then go ahead and fail now. 9358 */ 9359 if (e.error != 0 && !needrecov) { 9360 rdc->error = e.error; 9361 goto out; 9362 } 9363 9364 if (needrecov) { 9365 bool_t abort; 9366 9367 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9368 "nfs4readdir: initiating recovery.\n")); 9369 9370 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9371 NULL, OP_READDIR, NULL, NULL, NULL); 9372 if (abort == FALSE) { 9373 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9374 &recov_state, needrecov); 9375 if (!e.error) 9376 (void) xdr_free(xdr_COMPOUND4res_clnt, 9377 (caddr_t)&res); 9378 if (rdc->entries != NULL) { 9379 kmem_free(rdc->entries, rdc->entlen); 9380 rdc->entries = NULL; 9381 } 9382 goto recov_retry; 9383 } 9384 9385 if (e.error != 0) { 9386 rdc->error = e.error; 9387 goto out; 9388 } 9389 9390 /* fall through for res.status case */ 9391 } 9392 9393 res_opcnt = res.array_len; 9394 9395 /* 9396 * If compound failed first 2 ops (PUTFH+READDIR), then return 9397 * failure here. Subsequent ops are for filling out dot-dot 9398 * dirent, and if they fail, we still want to give the caller 9399 * the dirents returned by (the successful) READDIR op, so we need 9400 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9401 * 9402 * One example where PUTFH+READDIR ops would succeed but 9403 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9404 * but lacks x. In this case, a POSIX server's VOP_READDIR 9405 * would succeed; however, VOP_LOOKUP(..) would fail since no 9406 * x perm. We need to come up with a non-vendor-specific way 9407 * for a POSIX server to return d_ino from dotdot's dirent if 9408 * client only requests mounted_on_fileid, and just say the 9409 * LOOKUPP succeeded and fill out the GETATTR. However, if 9410 * client requested any mandatory attrs, server would be required 9411 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9412 * for dotdot. 9413 */ 9414 9415 if (res.status) { 9416 if (res_opcnt <= 2) { 9417 e.error = geterrno4(res.status); 9418 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9419 &recov_state, needrecov); 9420 nfs4_purge_stale_fh(e.error, vp, cr); 9421 rdc->error = e.error; 9422 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9423 if (rdc->entries != NULL) { 9424 kmem_free(rdc->entries, rdc->entlen); 9425 rdc->entries = NULL; 9426 } 9427 /* 9428 * If readdir a node that is a stub for a 9429 * crossed mount point, keep the original 9430 * secinfo flavor for the current file system, 9431 * not the crossed one. 9432 */ 9433 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9434 return; 9435 } 9436 } 9437 9438 resop = &res.array[1]; /* readdir res */ 9439 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9440 9441 mutex_enter(&rp->r_statelock); 9442 rp->r_cookieverf4 = rd_res->cookieverf; 9443 mutex_exit(&rp->r_statelock); 9444 9445 /* 9446 * For "." and ".." entries 9447 * e.g. 9448 * seek(cookie=0) -> "." entry with d_off = 1 9449 * seek(cookie=1) -> ".." entry with d_off = 2 9450 */ 9451 if (cookie == (nfs_cookie4) 0) { 9452 if (rd_res->dotp) 9453 rd_res->dotp->d_ino = nodeid; 9454 if (rd_res->dotdotp) 9455 rd_res->dotdotp->d_ino = pnodeid; 9456 } 9457 if (cookie == (nfs_cookie4) 1) { 9458 if (rd_res->dotdotp) 9459 rd_res->dotdotp->d_ino = pnodeid; 9460 } 9461 9462 9463 /* LOOKUPP+GETATTR attemped */ 9464 if (args.array_len == 5 && rd_res->dotdotp) { 9465 if (res.status == NFS4_OK && res_opcnt == 5) { 9466 nfs_fh4 *fhp; 9467 nfs4_sharedfh_t *sfhp; 9468 vnode_t *pvp; 9469 nfs4_ga_res_t *garp; 9470 9471 resop++; /* lookupp */ 9472 resop++; /* getfh */ 9473 fhp = &resop->nfs_resop4_u.opgetfh.object; 9474 9475 resop++; /* getattr of parent */ 9476 9477 /* 9478 * First, take care of finishing the 9479 * readdir results. 9480 */ 9481 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9482 /* 9483 * The d_ino of .. must be the inode number 9484 * of the mounted filesystem. 9485 */ 9486 if (garp->n4g_va.va_mask & AT_NODEID) 9487 rd_res->dotdotp->d_ino = 9488 garp->n4g_va.va_nodeid; 9489 9490 9491 /* 9492 * Next, create the ".." dnlc entry 9493 */ 9494 sfhp = sfh4_get(fhp, mi); 9495 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9496 dnlc_update(vp, "..", pvp); 9497 VN_RELE(pvp); 9498 } 9499 sfh4_rele(&sfhp); 9500 } 9501 } 9502 9503 if (mi->mi_io_kstats) { 9504 mutex_enter(&mi->mi_lock); 9505 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9506 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9507 mutex_exit(&mi->mi_lock); 9508 } 9509 9510 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9511 9512 out: 9513 /* 9514 * If readdir a node that is a stub for a crossed mount point, 9515 * keep the original secinfo flavor for the current file system, 9516 * not the crossed one. 9517 */ 9518 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9519 9520 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9521 } 9522 9523 9524 static int 9525 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9526 { 9527 rnode4_t *rp = VTOR4(bp->b_vp); 9528 int count; 9529 int error; 9530 cred_t *cred_otw = NULL; 9531 offset_t offset; 9532 nfs4_open_stream_t *osp = NULL; 9533 bool_t first_time = TRUE; /* first time getting otw cred */ 9534 bool_t last_time = FALSE; /* last time getting otw cred */ 9535 9536 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9537 9538 DTRACE_IO1(start, struct buf *, bp); 9539 offset = ldbtob(bp->b_lblkno); 9540 9541 if (bp->b_flags & B_READ) { 9542 read_again: 9543 /* 9544 * Releases the osp, if it is provided. 9545 * Puts a hold on the cred_otw and the new osp (if found). 9546 */ 9547 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9548 &first_time, &last_time); 9549 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9550 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9551 readahead, NULL); 9552 crfree(cred_otw); 9553 if (!error) { 9554 if (bp->b_resid) { 9555 /* 9556 * Didn't get it all because we hit EOF, 9557 * zero all the memory beyond the EOF. 9558 */ 9559 /* bzero(rdaddr + */ 9560 bzero(bp->b_un.b_addr + 9561 bp->b_bcount - bp->b_resid, bp->b_resid); 9562 } 9563 mutex_enter(&rp->r_statelock); 9564 if (bp->b_resid == bp->b_bcount && 9565 offset >= rp->r_size) { 9566 /* 9567 * We didn't read anything at all as we are 9568 * past EOF. Return an error indicator back 9569 * but don't destroy the pages (yet). 9570 */ 9571 error = NFS_EOF; 9572 } 9573 mutex_exit(&rp->r_statelock); 9574 } else if (error == EACCES && last_time == FALSE) { 9575 goto read_again; 9576 } 9577 } else { 9578 if (!(rp->r_flags & R4STALE)) { 9579 write_again: 9580 /* 9581 * Releases the osp, if it is provided. 9582 * Puts a hold on the cred_otw and the new 9583 * osp (if found). 9584 */ 9585 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9586 &first_time, &last_time); 9587 mutex_enter(&rp->r_statelock); 9588 count = MIN(bp->b_bcount, rp->r_size - offset); 9589 mutex_exit(&rp->r_statelock); 9590 if (count < 0) 9591 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9592 #ifdef DEBUG 9593 if (count == 0) { 9594 zoneid_t zoneid = getzoneid(); 9595 9596 zcmn_err(zoneid, CE_WARN, 9597 "nfs4_bio: zero length write at %lld", 9598 offset); 9599 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9600 "b_bcount=%ld, file size=%lld", 9601 rp->r_flags, (long)bp->b_bcount, 9602 rp->r_size); 9603 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9604 if (nfs4_bio_do_stop) 9605 debug_enter("nfs4_bio"); 9606 } 9607 #endif 9608 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9609 count, cred_otw, stab_comm); 9610 if (error == EACCES && last_time == FALSE) { 9611 crfree(cred_otw); 9612 goto write_again; 9613 } 9614 bp->b_error = error; 9615 if (error && error != EINTR && 9616 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9617 /* 9618 * Don't print EDQUOT errors on the console. 9619 * Don't print asynchronous EACCES errors. 9620 * Don't print EFBIG errors. 9621 * Print all other write errors. 9622 */ 9623 if (error != EDQUOT && error != EFBIG && 9624 (error != EACCES || 9625 !(bp->b_flags & B_ASYNC))) 9626 nfs4_write_error(bp->b_vp, 9627 error, cred_otw); 9628 /* 9629 * Update r_error and r_flags as appropriate. 9630 * If the error was ESTALE, then mark the 9631 * rnode as not being writeable and save 9632 * the error status. Otherwise, save any 9633 * errors which occur from asynchronous 9634 * page invalidations. Any errors occurring 9635 * from other operations should be saved 9636 * by the caller. 9637 */ 9638 mutex_enter(&rp->r_statelock); 9639 if (error == ESTALE) { 9640 rp->r_flags |= R4STALE; 9641 if (!rp->r_error) 9642 rp->r_error = error; 9643 } else if (!rp->r_error && 9644 (bp->b_flags & 9645 (B_INVAL|B_FORCE|B_ASYNC)) == 9646 (B_INVAL|B_FORCE|B_ASYNC)) { 9647 rp->r_error = error; 9648 } 9649 mutex_exit(&rp->r_statelock); 9650 } 9651 crfree(cred_otw); 9652 } else { 9653 error = rp->r_error; 9654 /* 9655 * A close may have cleared r_error, if so, 9656 * propagate ESTALE error return properly 9657 */ 9658 if (error == 0) 9659 error = ESTALE; 9660 } 9661 } 9662 9663 if (error != 0 && error != NFS_EOF) 9664 bp->b_flags |= B_ERROR; 9665 9666 if (osp) 9667 open_stream_rele(osp, rp); 9668 9669 DTRACE_IO1(done, struct buf *, bp); 9670 9671 return (error); 9672 } 9673 9674 /* ARGSUSED */ 9675 int 9676 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 9677 { 9678 return (EREMOTE); 9679 } 9680 9681 /* ARGSUSED2 */ 9682 int 9683 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9684 { 9685 rnode4_t *rp = VTOR4(vp); 9686 9687 if (!write_lock) { 9688 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9689 return (V_WRITELOCK_FALSE); 9690 } 9691 9692 if ((rp->r_flags & R4DIRECTIO) || 9693 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9694 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9695 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9696 return (V_WRITELOCK_FALSE); 9697 nfs_rw_exit(&rp->r_rwlock); 9698 } 9699 9700 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9701 return (V_WRITELOCK_TRUE); 9702 } 9703 9704 /* ARGSUSED */ 9705 void 9706 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9707 { 9708 rnode4_t *rp = VTOR4(vp); 9709 9710 nfs_rw_exit(&rp->r_rwlock); 9711 } 9712 9713 /* ARGSUSED */ 9714 static int 9715 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 9716 { 9717 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9718 return (EIO); 9719 9720 /* 9721 * Because we stuff the readdir cookie into the offset field 9722 * someone may attempt to do an lseek with the cookie which 9723 * we want to succeed. 9724 */ 9725 if (vp->v_type == VDIR) 9726 return (0); 9727 if (*noffp < 0) 9728 return (EINVAL); 9729 return (0); 9730 } 9731 9732 9733 /* 9734 * Return all the pages from [off..off+len) in file 9735 */ 9736 /* ARGSUSED */ 9737 static int 9738 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9739 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9740 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 9741 { 9742 rnode4_t *rp; 9743 int error; 9744 mntinfo4_t *mi; 9745 9746 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9747 return (EIO); 9748 rp = VTOR4(vp); 9749 if (IS_SHADOW(vp, rp)) 9750 vp = RTOV4(rp); 9751 9752 if (vp->v_flag & VNOMAP) 9753 return (ENOSYS); 9754 9755 if (protp != NULL) 9756 *protp = PROT_ALL; 9757 9758 /* 9759 * Now validate that the caches are up to date. 9760 */ 9761 if (error = nfs4_validate_caches(vp, cr)) 9762 return (error); 9763 9764 mi = VTOMI4(vp); 9765 retry: 9766 mutex_enter(&rp->r_statelock); 9767 9768 /* 9769 * Don't create dirty pages faster than they 9770 * can be cleaned so that the system doesn't 9771 * get imbalanced. If the async queue is 9772 * maxed out, then wait for it to drain before 9773 * creating more dirty pages. Also, wait for 9774 * any threads doing pagewalks in the vop_getattr 9775 * entry points so that they don't block for 9776 * long periods. 9777 */ 9778 if (rw == S_CREATE) { 9779 while ((mi->mi_max_threads != 0 && 9780 rp->r_awcount > 2 * mi->mi_max_threads) || 9781 rp->r_gcount > 0) 9782 cv_wait(&rp->r_cv, &rp->r_statelock); 9783 } 9784 9785 /* 9786 * If we are getting called as a side effect of an nfs_write() 9787 * operation the local file size might not be extended yet. 9788 * In this case we want to be able to return pages of zeroes. 9789 */ 9790 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9791 NFS4_DEBUG(nfs4_pageio_debug, 9792 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9793 "len=%llu, size=%llu, attrsize =%llu", off, 9794 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9795 mutex_exit(&rp->r_statelock); 9796 return (EFAULT); /* beyond EOF */ 9797 } 9798 9799 mutex_exit(&rp->r_statelock); 9800 9801 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9802 pl, plsz, seg, addr, rw, cr); 9803 NFS4_DEBUG(nfs4_pageio_debug && error, 9804 (CE_NOTE, "getpages error %d; off=%lld, len=%lld", 9805 error, off, (u_longlong_t)len)); 9806 9807 switch (error) { 9808 case NFS_EOF: 9809 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9810 goto retry; 9811 case ESTALE: 9812 nfs4_purge_stale_fh(error, vp, cr); 9813 } 9814 9815 return (error); 9816 } 9817 9818 /* 9819 * Called from pvn_getpages to get a particular page. 9820 */ 9821 /* ARGSUSED */ 9822 static int 9823 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9824 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9825 enum seg_rw rw, cred_t *cr) 9826 { 9827 rnode4_t *rp; 9828 uint_t bsize; 9829 struct buf *bp; 9830 page_t *pp; 9831 u_offset_t lbn; 9832 u_offset_t io_off; 9833 u_offset_t blkoff; 9834 u_offset_t rablkoff; 9835 size_t io_len; 9836 uint_t blksize; 9837 int error; 9838 int readahead; 9839 int readahead_issued = 0; 9840 int ra_window; /* readahead window */ 9841 page_t *pagefound; 9842 page_t *savepp; 9843 9844 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9845 return (EIO); 9846 9847 rp = VTOR4(vp); 9848 ASSERT(!IS_SHADOW(vp, rp)); 9849 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9850 9851 reread: 9852 bp = NULL; 9853 pp = NULL; 9854 pagefound = NULL; 9855 9856 if (pl != NULL) 9857 pl[0] = NULL; 9858 9859 error = 0; 9860 lbn = off / bsize; 9861 blkoff = lbn * bsize; 9862 9863 /* 9864 * Queueing up the readahead before doing the synchronous read 9865 * results in a significant increase in read throughput because 9866 * of the increased parallelism between the async threads and 9867 * the process context. 9868 */ 9869 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9870 rw != S_CREATE && 9871 !(vp->v_flag & VNOCACHE)) { 9872 mutex_enter(&rp->r_statelock); 9873 9874 /* 9875 * Calculate the number of readaheads to do. 9876 * a) No readaheads at offset = 0. 9877 * b) Do maximum(nfs4_nra) readaheads when the readahead 9878 * window is closed. 9879 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9880 * upon how far the readahead window is open or close. 9881 * d) No readaheads if rp->r_nextr is not within the scope 9882 * of the readahead window (random i/o). 9883 */ 9884 9885 if (off == 0) 9886 readahead = 0; 9887 else if (blkoff == rp->r_nextr) 9888 readahead = nfs4_nra; 9889 else if (rp->r_nextr > blkoff && 9890 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9891 <= (nfs4_nra - 1))) 9892 readahead = nfs4_nra - ra_window; 9893 else 9894 readahead = 0; 9895 9896 rablkoff = rp->r_nextr; 9897 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9898 mutex_exit(&rp->r_statelock); 9899 if (nfs4_async_readahead(vp, rablkoff + bsize, 9900 addr + (rablkoff + bsize - off), 9901 seg, cr, nfs4_readahead) < 0) { 9902 mutex_enter(&rp->r_statelock); 9903 break; 9904 } 9905 readahead--; 9906 rablkoff += bsize; 9907 /* 9908 * Indicate that we did a readahead so 9909 * readahead offset is not updated 9910 * by the synchronous read below. 9911 */ 9912 readahead_issued = 1; 9913 mutex_enter(&rp->r_statelock); 9914 /* 9915 * set readahead offset to 9916 * offset of last async readahead 9917 * request. 9918 */ 9919 rp->r_nextr = rablkoff; 9920 } 9921 mutex_exit(&rp->r_statelock); 9922 } 9923 9924 again: 9925 if ((pagefound = page_exists(vp, off)) == NULL) { 9926 if (pl == NULL) { 9927 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9928 nfs4_readahead); 9929 } else if (rw == S_CREATE) { 9930 /* 9931 * Block for this page is not allocated, or the offset 9932 * is beyond the current allocation size, or we're 9933 * allocating a swap slot and the page was not found, 9934 * so allocate it and return a zero page. 9935 */ 9936 if ((pp = page_create_va(vp, off, 9937 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9938 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9939 io_len = PAGESIZE; 9940 mutex_enter(&rp->r_statelock); 9941 rp->r_nextr = off + PAGESIZE; 9942 mutex_exit(&rp->r_statelock); 9943 } else { 9944 /* 9945 * Need to go to server to get a block 9946 */ 9947 mutex_enter(&rp->r_statelock); 9948 if (blkoff < rp->r_size && 9949 blkoff + bsize > rp->r_size) { 9950 /* 9951 * If less than a block left in 9952 * file read less than a block. 9953 */ 9954 if (rp->r_size <= off) { 9955 /* 9956 * Trying to access beyond EOF, 9957 * set up to get at least one page. 9958 */ 9959 blksize = off + PAGESIZE - blkoff; 9960 } else 9961 blksize = rp->r_size - blkoff; 9962 } else if ((off == 0) || 9963 (off != rp->r_nextr && !readahead_issued)) { 9964 blksize = PAGESIZE; 9965 blkoff = off; /* block = page here */ 9966 } else 9967 blksize = bsize; 9968 mutex_exit(&rp->r_statelock); 9969 9970 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9971 &io_len, blkoff, blksize, 0); 9972 9973 /* 9974 * Some other thread has entered the page, 9975 * so just use it. 9976 */ 9977 if (pp == NULL) 9978 goto again; 9979 9980 /* 9981 * Now round the request size up to page boundaries. 9982 * This ensures that the entire page will be 9983 * initialized to zeroes if EOF is encountered. 9984 */ 9985 io_len = ptob(btopr(io_len)); 9986 9987 bp = pageio_setup(pp, io_len, vp, B_READ); 9988 ASSERT(bp != NULL); 9989 9990 /* 9991 * pageio_setup should have set b_addr to 0. This 9992 * is correct since we want to do I/O on a page 9993 * boundary. bp_mapin will use this addr to calculate 9994 * an offset, and then set b_addr to the kernel virtual 9995 * address it allocated for us. 9996 */ 9997 ASSERT(bp->b_un.b_addr == 0); 9998 9999 bp->b_edev = 0; 10000 bp->b_dev = 0; 10001 bp->b_lblkno = lbtodb(io_off); 10002 bp->b_file = vp; 10003 bp->b_offset = (offset_t)off; 10004 bp_mapin(bp); 10005 10006 /* 10007 * If doing a write beyond what we believe is EOF, 10008 * don't bother trying to read the pages from the 10009 * server, we'll just zero the pages here. We 10010 * don't check that the rw flag is S_WRITE here 10011 * because some implementations may attempt a 10012 * read access to the buffer before copying data. 10013 */ 10014 mutex_enter(&rp->r_statelock); 10015 if (io_off >= rp->r_size && seg == segkmap) { 10016 mutex_exit(&rp->r_statelock); 10017 bzero(bp->b_un.b_addr, io_len); 10018 } else { 10019 mutex_exit(&rp->r_statelock); 10020 error = nfs4_bio(bp, NULL, cr, FALSE); 10021 } 10022 10023 /* 10024 * Unmap the buffer before freeing it. 10025 */ 10026 bp_mapout(bp); 10027 pageio_done(bp); 10028 10029 savepp = pp; 10030 do { 10031 pp->p_fsdata = C_NOCOMMIT; 10032 } while ((pp = pp->p_next) != savepp); 10033 10034 if (error == NFS_EOF) { 10035 /* 10036 * If doing a write system call just return 10037 * zeroed pages, else user tried to get pages 10038 * beyond EOF, return error. We don't check 10039 * that the rw flag is S_WRITE here because 10040 * some implementations may attempt a read 10041 * access to the buffer before copying data. 10042 */ 10043 if (seg == segkmap) 10044 error = 0; 10045 else 10046 error = EFAULT; 10047 } 10048 10049 if (!readahead_issued && !error) { 10050 mutex_enter(&rp->r_statelock); 10051 rp->r_nextr = io_off + io_len; 10052 mutex_exit(&rp->r_statelock); 10053 } 10054 } 10055 } 10056 10057 out: 10058 if (pl == NULL) 10059 return (error); 10060 10061 if (error) { 10062 if (pp != NULL) 10063 pvn_read_done(pp, B_ERROR); 10064 return (error); 10065 } 10066 10067 if (pagefound) { 10068 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 10069 10070 /* 10071 * Page exists in the cache, acquire the appropriate lock. 10072 * If this fails, start all over again. 10073 */ 10074 if ((pp = page_lookup(vp, off, se)) == NULL) { 10075 #ifdef DEBUG 10076 nfs4_lostpage++; 10077 #endif 10078 goto reread; 10079 } 10080 pl[0] = pp; 10081 pl[1] = NULL; 10082 return (0); 10083 } 10084 10085 if (pp != NULL) 10086 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 10087 10088 return (error); 10089 } 10090 10091 static void 10092 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 10093 cred_t *cr) 10094 { 10095 int error; 10096 page_t *pp; 10097 u_offset_t io_off; 10098 size_t io_len; 10099 struct buf *bp; 10100 uint_t bsize, blksize; 10101 rnode4_t *rp = VTOR4(vp); 10102 page_t *savepp; 10103 10104 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10105 10106 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10107 10108 mutex_enter(&rp->r_statelock); 10109 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 10110 /* 10111 * If less than a block left in file read less 10112 * than a block. 10113 */ 10114 blksize = rp->r_size - blkoff; 10115 } else 10116 blksize = bsize; 10117 mutex_exit(&rp->r_statelock); 10118 10119 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 10120 &io_off, &io_len, blkoff, blksize, 1); 10121 /* 10122 * The isra flag passed to the kluster function is 1, we may have 10123 * gotten a return value of NULL for a variety of reasons (# of free 10124 * pages < minfree, someone entered the page on the vnode etc). In all 10125 * cases, we want to punt on the readahead. 10126 */ 10127 if (pp == NULL) 10128 return; 10129 10130 /* 10131 * Now round the request size up to page boundaries. 10132 * This ensures that the entire page will be 10133 * initialized to zeroes if EOF is encountered. 10134 */ 10135 io_len = ptob(btopr(io_len)); 10136 10137 bp = pageio_setup(pp, io_len, vp, B_READ); 10138 ASSERT(bp != NULL); 10139 10140 /* 10141 * pageio_setup should have set b_addr to 0. This is correct since 10142 * we want to do I/O on a page boundary. bp_mapin() will use this addr 10143 * to calculate an offset, and then set b_addr to the kernel virtual 10144 * address it allocated for us. 10145 */ 10146 ASSERT(bp->b_un.b_addr == 0); 10147 10148 bp->b_edev = 0; 10149 bp->b_dev = 0; 10150 bp->b_lblkno = lbtodb(io_off); 10151 bp->b_file = vp; 10152 bp->b_offset = (offset_t)blkoff; 10153 bp_mapin(bp); 10154 10155 /* 10156 * If doing a write beyond what we believe is EOF, don't bother trying 10157 * to read the pages from the server, we'll just zero the pages here. 10158 * We don't check that the rw flag is S_WRITE here because some 10159 * implementations may attempt a read access to the buffer before 10160 * copying data. 10161 */ 10162 mutex_enter(&rp->r_statelock); 10163 if (io_off >= rp->r_size && seg == segkmap) { 10164 mutex_exit(&rp->r_statelock); 10165 bzero(bp->b_un.b_addr, io_len); 10166 error = 0; 10167 } else { 10168 mutex_exit(&rp->r_statelock); 10169 error = nfs4_bio(bp, NULL, cr, TRUE); 10170 if (error == NFS_EOF) 10171 error = 0; 10172 } 10173 10174 /* 10175 * Unmap the buffer before freeing it. 10176 */ 10177 bp_mapout(bp); 10178 pageio_done(bp); 10179 10180 savepp = pp; 10181 do { 10182 pp->p_fsdata = C_NOCOMMIT; 10183 } while ((pp = pp->p_next) != savepp); 10184 10185 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10186 10187 /* 10188 * In case of error set readahead offset 10189 * to the lowest offset. 10190 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10191 */ 10192 if (error && rp->r_nextr > io_off) { 10193 mutex_enter(&rp->r_statelock); 10194 if (rp->r_nextr > io_off) 10195 rp->r_nextr = io_off; 10196 mutex_exit(&rp->r_statelock); 10197 } 10198 } 10199 10200 /* 10201 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10202 * If len == 0, do from off to EOF. 10203 * 10204 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10205 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10206 * (from pageout). 10207 */ 10208 /* ARGSUSED */ 10209 static int 10210 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 10211 caller_context_t *ct) 10212 { 10213 int error; 10214 rnode4_t *rp; 10215 10216 ASSERT(cr != NULL); 10217 10218 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10219 return (EIO); 10220 10221 rp = VTOR4(vp); 10222 if (IS_SHADOW(vp, rp)) 10223 vp = RTOV4(rp); 10224 10225 /* 10226 * XXX - Why should this check be made here? 10227 */ 10228 if (vp->v_flag & VNOMAP) 10229 return (ENOSYS); 10230 10231 if (len == 0 && !(flags & B_INVAL) && 10232 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10233 return (0); 10234 10235 mutex_enter(&rp->r_statelock); 10236 rp->r_count++; 10237 mutex_exit(&rp->r_statelock); 10238 error = nfs4_putpages(vp, off, len, flags, cr); 10239 mutex_enter(&rp->r_statelock); 10240 rp->r_count--; 10241 cv_broadcast(&rp->r_cv); 10242 mutex_exit(&rp->r_statelock); 10243 10244 return (error); 10245 } 10246 10247 /* 10248 * Write out a single page, possibly klustering adjacent dirty pages. 10249 */ 10250 int 10251 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10252 int flags, cred_t *cr) 10253 { 10254 u_offset_t io_off; 10255 u_offset_t lbn_off; 10256 u_offset_t lbn; 10257 size_t io_len; 10258 uint_t bsize; 10259 int error; 10260 rnode4_t *rp; 10261 10262 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10263 ASSERT(pp != NULL); 10264 ASSERT(cr != NULL); 10265 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10266 10267 rp = VTOR4(vp); 10268 ASSERT(rp->r_count > 0); 10269 ASSERT(!IS_SHADOW(vp, rp)); 10270 10271 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10272 lbn = pp->p_offset / bsize; 10273 lbn_off = lbn * bsize; 10274 10275 /* 10276 * Find a kluster that fits in one block, or in 10277 * one page if pages are bigger than blocks. If 10278 * there is less file space allocated than a whole 10279 * page, we'll shorten the i/o request below. 10280 */ 10281 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10282 roundup(bsize, PAGESIZE), flags); 10283 10284 /* 10285 * pvn_write_kluster shouldn't have returned a page with offset 10286 * behind the original page we were given. Verify that. 10287 */ 10288 ASSERT((pp->p_offset / bsize) >= lbn); 10289 10290 /* 10291 * Now pp will have the list of kept dirty pages marked for 10292 * write back. It will also handle invalidation and freeing 10293 * of pages that are not dirty. Check for page length rounding 10294 * problems. 10295 */ 10296 if (io_off + io_len > lbn_off + bsize) { 10297 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10298 io_len = lbn_off + bsize - io_off; 10299 } 10300 /* 10301 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10302 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10303 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10304 * progress and the r_size has not been made consistent with the 10305 * new size of the file. When the uiomove() completes the r_size is 10306 * updated and the R4MODINPROGRESS flag is cleared. 10307 * 10308 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10309 * consistent value of r_size. Without this handshaking, it is 10310 * possible that nfs4_bio() picks up the old value of r_size 10311 * before the uiomove() in writerp4() completes. This will result 10312 * in the write through nfs4_bio() being dropped. 10313 * 10314 * More precisely, there is a window between the time the uiomove() 10315 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10316 * operation intervenes in this window, the page will be picked up, 10317 * because it is dirty (it will be unlocked, unless it was 10318 * pagecreate'd). When the page is picked up as dirty, the dirty 10319 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10320 * checked. This will still be the old size. Therefore the page will 10321 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10322 * the page will be found to be clean and the write will be dropped. 10323 */ 10324 if (rp->r_flags & R4MODINPROGRESS) { 10325 mutex_enter(&rp->r_statelock); 10326 if ((rp->r_flags & R4MODINPROGRESS) && 10327 rp->r_modaddr + MAXBSIZE > io_off && 10328 rp->r_modaddr < io_off + io_len) { 10329 page_t *plist; 10330 /* 10331 * A write is in progress for this region of the file. 10332 * If we did not detect R4MODINPROGRESS here then this 10333 * path through nfs_putapage() would eventually go to 10334 * nfs4_bio() and may not write out all of the data 10335 * in the pages. We end up losing data. So we decide 10336 * to set the modified bit on each page in the page 10337 * list and mark the rnode with R4DIRTY. This write 10338 * will be restarted at some later time. 10339 */ 10340 plist = pp; 10341 while (plist != NULL) { 10342 pp = plist; 10343 page_sub(&plist, pp); 10344 hat_setmod(pp); 10345 page_io_unlock(pp); 10346 page_unlock(pp); 10347 } 10348 rp->r_flags |= R4DIRTY; 10349 mutex_exit(&rp->r_statelock); 10350 if (offp) 10351 *offp = io_off; 10352 if (lenp) 10353 *lenp = io_len; 10354 return (0); 10355 } 10356 mutex_exit(&rp->r_statelock); 10357 } 10358 10359 if (flags & B_ASYNC) { 10360 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10361 nfs4_sync_putapage); 10362 } else 10363 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10364 10365 if (offp) 10366 *offp = io_off; 10367 if (lenp) 10368 *lenp = io_len; 10369 return (error); 10370 } 10371 10372 static int 10373 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10374 int flags, cred_t *cr) 10375 { 10376 int error; 10377 rnode4_t *rp; 10378 10379 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10380 10381 flags |= B_WRITE; 10382 10383 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10384 10385 rp = VTOR4(vp); 10386 10387 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10388 error == EACCES) && 10389 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10390 if (!(rp->r_flags & R4OUTOFSPACE)) { 10391 mutex_enter(&rp->r_statelock); 10392 rp->r_flags |= R4OUTOFSPACE; 10393 mutex_exit(&rp->r_statelock); 10394 } 10395 flags |= B_ERROR; 10396 pvn_write_done(pp, flags); 10397 /* 10398 * If this was not an async thread, then try again to 10399 * write out the pages, but this time, also destroy 10400 * them whether or not the write is successful. This 10401 * will prevent memory from filling up with these 10402 * pages and destroying them is the only alternative 10403 * if they can't be written out. 10404 * 10405 * Don't do this if this is an async thread because 10406 * when the pages are unlocked in pvn_write_done, 10407 * some other thread could have come along, locked 10408 * them, and queued for an async thread. It would be 10409 * possible for all of the async threads to be tied 10410 * up waiting to lock the pages again and they would 10411 * all already be locked and waiting for an async 10412 * thread to handle them. Deadlock. 10413 */ 10414 if (!(flags & B_ASYNC)) { 10415 error = nfs4_putpage(vp, io_off, io_len, 10416 B_INVAL | B_FORCE, cr, NULL); 10417 } 10418 } else { 10419 if (error) 10420 flags |= B_ERROR; 10421 else if (rp->r_flags & R4OUTOFSPACE) { 10422 mutex_enter(&rp->r_statelock); 10423 rp->r_flags &= ~R4OUTOFSPACE; 10424 mutex_exit(&rp->r_statelock); 10425 } 10426 pvn_write_done(pp, flags); 10427 if (freemem < desfree) 10428 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10429 NFS4_WRITE_NOWAIT); 10430 } 10431 10432 return (error); 10433 } 10434 10435 #ifdef DEBUG 10436 int nfs4_force_open_before_mmap = 0; 10437 #endif 10438 10439 /* ARGSUSED */ 10440 static int 10441 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10442 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10443 caller_context_t *ct) 10444 { 10445 struct segvn_crargs vn_a; 10446 int error = 0; 10447 rnode4_t *rp = VTOR4(vp); 10448 mntinfo4_t *mi = VTOMI4(vp); 10449 10450 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10451 return (EIO); 10452 10453 if (vp->v_flag & VNOMAP) 10454 return (ENOSYS); 10455 10456 if (off < 0 || (off + len) < 0) 10457 return (ENXIO); 10458 10459 if (vp->v_type != VREG) 10460 return (ENODEV); 10461 10462 /* 10463 * If the file is delegated to the client don't do anything. 10464 * If the file is not delegated, then validate the data cache. 10465 */ 10466 mutex_enter(&rp->r_statev4_lock); 10467 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10468 mutex_exit(&rp->r_statev4_lock); 10469 error = nfs4_validate_caches(vp, cr); 10470 if (error) 10471 return (error); 10472 } else { 10473 mutex_exit(&rp->r_statev4_lock); 10474 } 10475 10476 /* 10477 * Check to see if the vnode is currently marked as not cachable. 10478 * This means portions of the file are locked (through VOP_FRLOCK). 10479 * In this case the map request must be refused. We use 10480 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10481 * 10482 * Atomically increment r_inmap after acquiring r_rwlock. The 10483 * idea here is to acquire r_rwlock to block read/write and 10484 * not to protect r_inmap. r_inmap will inform nfs4_read/write() 10485 * that we are in nfs4_map(). Now, r_rwlock is acquired in order 10486 * and we can prevent the deadlock that would have occurred 10487 * when nfs4_addmap() would have acquired it out of order. 10488 * 10489 * Since we are not protecting r_inmap by any lock, we do not 10490 * hold any lock when we decrement it. We atomically decrement 10491 * r_inmap after we release r_lkserlock. 10492 */ 10493 10494 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp))) 10495 return (EINTR); 10496 atomic_inc_uint(&rp->r_inmap); 10497 nfs_rw_exit(&rp->r_rwlock); 10498 10499 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) { 10500 atomic_dec_uint(&rp->r_inmap); 10501 return (EINTR); 10502 } 10503 10504 10505 if (vp->v_flag & VNOCACHE) { 10506 error = EAGAIN; 10507 goto done; 10508 } 10509 10510 /* 10511 * Don't allow concurrent locks and mapping if mandatory locking is 10512 * enabled. 10513 */ 10514 if (flk_has_remote_locks(vp)) { 10515 struct vattr va; 10516 va.va_mask = AT_MODE; 10517 error = nfs4getattr(vp, &va, cr); 10518 if (error != 0) 10519 goto done; 10520 if (MANDLOCK(vp, va.va_mode)) { 10521 error = EAGAIN; 10522 goto done; 10523 } 10524 } 10525 10526 /* 10527 * It is possible that the rnode has a lost lock request that we 10528 * are still trying to recover, and that the request conflicts with 10529 * this map request. 10530 * 10531 * An alternative approach would be for nfs4_safemap() to consider 10532 * queued lock requests when deciding whether to set or clear 10533 * VNOCACHE. This would require the frlock code path to call 10534 * nfs4_safemap() after enqueing a lost request. 10535 */ 10536 if (nfs4_map_lost_lock_conflict(vp)) { 10537 error = EAGAIN; 10538 goto done; 10539 } 10540 10541 as_rangelock(as); 10542 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 10543 if (error != 0) { 10544 as_rangeunlock(as); 10545 goto done; 10546 } 10547 10548 if (vp->v_type == VREG) { 10549 /* 10550 * We need to retrieve the open stream 10551 */ 10552 nfs4_open_stream_t *osp = NULL; 10553 nfs4_open_owner_t *oop = NULL; 10554 10555 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10556 if (oop != NULL) { 10557 /* returns with 'os_sync_lock' held */ 10558 osp = find_open_stream(oop, rp); 10559 open_owner_rele(oop); 10560 } 10561 if (osp == NULL) { 10562 #ifdef DEBUG 10563 if (nfs4_force_open_before_mmap) { 10564 error = EIO; 10565 goto done; 10566 } 10567 #endif 10568 /* returns with 'os_sync_lock' held */ 10569 error = open_and_get_osp(vp, cr, &osp); 10570 if (osp == NULL) { 10571 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10572 "nfs4_map: we tried to OPEN the file " 10573 "but again no osp, so fail with EIO")); 10574 goto done; 10575 } 10576 } 10577 10578 if (osp->os_failed_reopen) { 10579 mutex_exit(&osp->os_sync_lock); 10580 open_stream_rele(osp, rp); 10581 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10582 "nfs4_map: os_failed_reopen set on " 10583 "osp %p, cr %p, rp %s", (void *)osp, 10584 (void *)cr, rnode4info(rp))); 10585 error = EIO; 10586 goto done; 10587 } 10588 mutex_exit(&osp->os_sync_lock); 10589 open_stream_rele(osp, rp); 10590 } 10591 10592 vn_a.vp = vp; 10593 vn_a.offset = off; 10594 vn_a.type = (flags & MAP_TYPE); 10595 vn_a.prot = (uchar_t)prot; 10596 vn_a.maxprot = (uchar_t)maxprot; 10597 vn_a.flags = (flags & ~MAP_TYPE); 10598 vn_a.cred = cr; 10599 vn_a.amp = NULL; 10600 vn_a.szc = 0; 10601 vn_a.lgrp_mem_policy_flags = 0; 10602 10603 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10604 as_rangeunlock(as); 10605 10606 done: 10607 nfs_rw_exit(&rp->r_lkserlock); 10608 atomic_dec_uint(&rp->r_inmap); 10609 return (error); 10610 } 10611 10612 /* 10613 * We're most likely dealing with a kernel module that likes to READ 10614 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10615 * officially OPEN the file to create the necessary client state 10616 * for bookkeeping of os_mmap_read/write counts. 10617 * 10618 * Since VOP_MAP only passes in a pointer to the vnode rather than 10619 * a double pointer, we can't handle the case where nfs4open_otw() 10620 * returns a different vnode than the one passed into VOP_MAP (since 10621 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10622 * we return NULL and let nfs4_map() fail. Note: the only case where 10623 * this should happen is if the file got removed and replaced with the 10624 * same name on the server (in addition to the fact that we're trying 10625 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10626 */ 10627 static int 10628 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10629 { 10630 rnode4_t *rp, *drp; 10631 vnode_t *dvp, *open_vp; 10632 char file_name[MAXNAMELEN]; 10633 int just_created; 10634 nfs4_open_stream_t *osp; 10635 nfs4_open_owner_t *oop; 10636 int error; 10637 10638 *ospp = NULL; 10639 open_vp = map_vp; 10640 10641 rp = VTOR4(open_vp); 10642 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10643 return (error); 10644 drp = VTOR4(dvp); 10645 10646 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10647 VN_RELE(dvp); 10648 return (EINTR); 10649 } 10650 10651 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10652 nfs_rw_exit(&drp->r_rwlock); 10653 VN_RELE(dvp); 10654 return (error); 10655 } 10656 10657 mutex_enter(&rp->r_statev4_lock); 10658 if (rp->created_v4) { 10659 rp->created_v4 = 0; 10660 mutex_exit(&rp->r_statev4_lock); 10661 10662 dnlc_update(dvp, file_name, open_vp); 10663 /* This is needed so we don't bump the open ref count */ 10664 just_created = 1; 10665 } else { 10666 mutex_exit(&rp->r_statev4_lock); 10667 just_created = 0; 10668 } 10669 10670 VN_HOLD(map_vp); 10671 10672 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10673 just_created); 10674 if (error) { 10675 nfs_rw_exit(&drp->r_rwlock); 10676 VN_RELE(dvp); 10677 VN_RELE(map_vp); 10678 return (error); 10679 } 10680 10681 nfs_rw_exit(&drp->r_rwlock); 10682 VN_RELE(dvp); 10683 10684 /* 10685 * If nfs4open_otw() returned a different vnode then "undo" 10686 * the open and return failure to the caller. 10687 */ 10688 if (!VN_CMP(open_vp, map_vp)) { 10689 nfs4_error_t e; 10690 10691 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10692 "open returned a different vnode")); 10693 /* 10694 * If there's an error, ignore it, 10695 * and let VOP_INACTIVE handle it. 10696 */ 10697 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10698 CLOSE_NORM, 0, 0, 0); 10699 VN_RELE(map_vp); 10700 return (EIO); 10701 } 10702 10703 VN_RELE(map_vp); 10704 10705 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10706 if (!oop) { 10707 nfs4_error_t e; 10708 10709 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10710 "no open owner")); 10711 /* 10712 * If there's an error, ignore it, 10713 * and let VOP_INACTIVE handle it. 10714 */ 10715 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10716 CLOSE_NORM, 0, 0, 0); 10717 return (EIO); 10718 } 10719 osp = find_open_stream(oop, rp); 10720 open_owner_rele(oop); 10721 *ospp = osp; 10722 return (0); 10723 } 10724 10725 /* 10726 * Please be aware that when this function is called, the address space write 10727 * a_lock is held. Do not put over the wire calls in this function. 10728 */ 10729 /* ARGSUSED */ 10730 static int 10731 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10732 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10733 caller_context_t *ct) 10734 { 10735 rnode4_t *rp; 10736 int error = 0; 10737 mntinfo4_t *mi; 10738 10739 mi = VTOMI4(vp); 10740 rp = VTOR4(vp); 10741 10742 if (nfs_zone() != mi->mi_zone) 10743 return (EIO); 10744 if (vp->v_flag & VNOMAP) 10745 return (ENOSYS); 10746 10747 /* 10748 * Don't need to update the open stream first, since this 10749 * mmap can't add any additional share access that isn't 10750 * already contained in the open stream (for the case where we 10751 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10752 * take into account os_mmap_read[write] counts). 10753 */ 10754 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10755 10756 if (vp->v_type == VREG) { 10757 /* 10758 * We need to retrieve the open stream and update the counts. 10759 * If there is no open stream here, something is wrong. 10760 */ 10761 nfs4_open_stream_t *osp = NULL; 10762 nfs4_open_owner_t *oop = NULL; 10763 10764 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10765 if (oop != NULL) { 10766 /* returns with 'os_sync_lock' held */ 10767 osp = find_open_stream(oop, rp); 10768 open_owner_rele(oop); 10769 } 10770 if (osp == NULL) { 10771 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10772 "nfs4_addmap: we should have an osp" 10773 "but we don't, so fail with EIO")); 10774 error = EIO; 10775 goto out; 10776 } 10777 10778 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10779 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10780 10781 /* 10782 * Update the map count in the open stream. 10783 * This is necessary in the case where we 10784 * open/mmap/close/, then the server reboots, and we 10785 * attempt to reopen. If the mmap doesn't add share 10786 * access then we send an invalid reopen with 10787 * access = NONE. 10788 * 10789 * We need to specifically check each PROT_* so a mmap 10790 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10791 * read and write access. A simple comparison of prot 10792 * to ~PROT_WRITE to determine read access is insufficient 10793 * since prot can be |= with PROT_USER, etc. 10794 */ 10795 10796 /* 10797 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10798 */ 10799 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10800 osp->os_mmap_write += btopr(len); 10801 if (maxprot & PROT_READ) 10802 osp->os_mmap_read += btopr(len); 10803 if (maxprot & PROT_EXEC) 10804 osp->os_mmap_read += btopr(len); 10805 /* 10806 * Ensure that os_mmap_read gets incremented, even if 10807 * maxprot were to look like PROT_NONE. 10808 */ 10809 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10810 !(maxprot & PROT_EXEC)) 10811 osp->os_mmap_read += btopr(len); 10812 osp->os_mapcnt += btopr(len); 10813 mutex_exit(&osp->os_sync_lock); 10814 open_stream_rele(osp, rp); 10815 } 10816 10817 out: 10818 /* 10819 * If we got an error, then undo our 10820 * incrementing of 'r_mapcnt'. 10821 */ 10822 10823 if (error) { 10824 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10825 ASSERT(rp->r_mapcnt >= 0); 10826 } 10827 return (error); 10828 } 10829 10830 /* ARGSUSED */ 10831 static int 10832 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct) 10833 { 10834 10835 return (VTOR4(vp1) == VTOR4(vp2)); 10836 } 10837 10838 /* ARGSUSED */ 10839 static int 10840 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10841 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr, 10842 caller_context_t *ct) 10843 { 10844 int rc; 10845 u_offset_t start, end; 10846 rnode4_t *rp; 10847 int error = 0, intr = INTR4(vp); 10848 nfs4_error_t e; 10849 10850 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10851 return (EIO); 10852 10853 /* check for valid cmd parameter */ 10854 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10855 return (EINVAL); 10856 10857 /* Verify l_type. */ 10858 switch (bfp->l_type) { 10859 case F_RDLCK: 10860 if (cmd != F_GETLK && !(flag & FREAD)) 10861 return (EBADF); 10862 break; 10863 case F_WRLCK: 10864 if (cmd != F_GETLK && !(flag & FWRITE)) 10865 return (EBADF); 10866 break; 10867 case F_UNLCK: 10868 intr = 0; 10869 break; 10870 10871 default: 10872 return (EINVAL); 10873 } 10874 10875 /* check the validity of the lock range */ 10876 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10877 return (rc); 10878 if (rc = flk_check_lock_data(start, end, MAXEND)) 10879 return (rc); 10880 10881 /* 10882 * If the filesystem is mounted using local locking, pass the 10883 * request off to the local locking code. 10884 */ 10885 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10886 if (cmd == F_SETLK || cmd == F_SETLKW) { 10887 /* 10888 * For complete safety, we should be holding 10889 * r_lkserlock. However, we can't call 10890 * nfs4_safelock and then fs_frlock while 10891 * holding r_lkserlock, so just invoke 10892 * nfs4_safelock and expect that this will 10893 * catch enough of the cases. 10894 */ 10895 if (!nfs4_safelock(vp, bfp, cr)) 10896 return (EAGAIN); 10897 } 10898 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 10899 } 10900 10901 rp = VTOR4(vp); 10902 10903 /* 10904 * Check whether the given lock request can proceed, given the 10905 * current file mappings. 10906 */ 10907 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10908 return (EINTR); 10909 if (cmd == F_SETLK || cmd == F_SETLKW) { 10910 if (!nfs4_safelock(vp, bfp, cr)) { 10911 rc = EAGAIN; 10912 goto done; 10913 } 10914 } 10915 10916 /* 10917 * Flush the cache after waiting for async I/O to finish. For new 10918 * locks, this is so that the process gets the latest bits from the 10919 * server. For unlocks, this is so that other clients see the 10920 * latest bits once the file has been unlocked. If currently dirty 10921 * pages can't be flushed, then don't allow a lock to be set. But 10922 * allow unlocks to succeed, to avoid having orphan locks on the 10923 * server. 10924 */ 10925 if (cmd != F_GETLK) { 10926 mutex_enter(&rp->r_statelock); 10927 while (rp->r_count > 0) { 10928 if (intr) { 10929 klwp_t *lwp = ttolwp(curthread); 10930 10931 if (lwp != NULL) 10932 lwp->lwp_nostop++; 10933 if (cv_wait_sig(&rp->r_cv, 10934 &rp->r_statelock) == 0) { 10935 if (lwp != NULL) 10936 lwp->lwp_nostop--; 10937 rc = EINTR; 10938 break; 10939 } 10940 if (lwp != NULL) 10941 lwp->lwp_nostop--; 10942 } else 10943 cv_wait(&rp->r_cv, &rp->r_statelock); 10944 } 10945 mutex_exit(&rp->r_statelock); 10946 if (rc != 0) 10947 goto done; 10948 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct); 10949 if (error) { 10950 if (error == ENOSPC || error == EDQUOT) { 10951 mutex_enter(&rp->r_statelock); 10952 if (!rp->r_error) 10953 rp->r_error = error; 10954 mutex_exit(&rp->r_statelock); 10955 } 10956 if (bfp->l_type != F_UNLCK) { 10957 rc = ENOLCK; 10958 goto done; 10959 } 10960 } 10961 } 10962 10963 /* 10964 * Call the lock manager to do the real work of contacting 10965 * the server and obtaining the lock. 10966 */ 10967 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10968 cr, &e, NULL, NULL); 10969 rc = e.error; 10970 10971 if (rc == 0) 10972 nfs4_lockcompletion(vp, cmd); 10973 10974 done: 10975 nfs_rw_exit(&rp->r_lkserlock); 10976 10977 return (rc); 10978 } 10979 10980 /* 10981 * Free storage space associated with the specified vnode. The portion 10982 * to be freed is specified by bfp->l_start and bfp->l_len (already 10983 * normalized to a "whence" of 0). 10984 * 10985 * This is an experimental facility whose continued existence is not 10986 * guaranteed. Currently, we only support the special case 10987 * of l_len == 0, meaning free to end of file. 10988 */ 10989 /* ARGSUSED */ 10990 static int 10991 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10992 offset_t offset, cred_t *cr, caller_context_t *ct) 10993 { 10994 int error; 10995 10996 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10997 return (EIO); 10998 ASSERT(vp->v_type == VREG); 10999 if (cmd != F_FREESP) 11000 return (EINVAL); 11001 11002 error = convoff(vp, bfp, 0, offset); 11003 if (!error) { 11004 ASSERT(bfp->l_start >= 0); 11005 if (bfp->l_len == 0) { 11006 struct vattr va; 11007 11008 va.va_mask = AT_SIZE; 11009 va.va_size = bfp->l_start; 11010 error = nfs4setattr(vp, &va, 0, cr, NULL); 11011 11012 if (error == 0 && bfp->l_start == 0) 11013 vnevent_truncate(vp, ct); 11014 } else 11015 error = EINVAL; 11016 } 11017 11018 return (error); 11019 } 11020 11021 /* ARGSUSED */ 11022 int 11023 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 11024 { 11025 rnode4_t *rp; 11026 rp = VTOR4(vp); 11027 11028 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 11029 vp = RTOV4(rp); 11030 } 11031 *vpp = vp; 11032 return (0); 11033 } 11034 11035 /* 11036 * Setup and add an address space callback to do the work of the delmap call. 11037 * The callback will (and must be) deleted in the actual callback function. 11038 * 11039 * This is done in order to take care of the problem that we have with holding 11040 * the address space's a_lock for a long period of time (e.g. if the NFS server 11041 * is down). Callbacks will be executed in the address space code while the 11042 * a_lock is not held. Holding the address space's a_lock causes things such 11043 * as ps and fork to hang because they are trying to acquire this lock as well. 11044 */ 11045 /* ARGSUSED */ 11046 static int 11047 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 11048 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 11049 caller_context_t *ct) 11050 { 11051 int caller_found; 11052 int error; 11053 rnode4_t *rp; 11054 nfs4_delmap_args_t *dmapp; 11055 nfs4_delmapcall_t *delmap_call; 11056 11057 if (vp->v_flag & VNOMAP) 11058 return (ENOSYS); 11059 11060 /* 11061 * A process may not change zones if it has NFS pages mmap'ed 11062 * in, so we can't legitimately get here from the wrong zone. 11063 */ 11064 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11065 11066 rp = VTOR4(vp); 11067 11068 /* 11069 * The way that the address space of this process deletes its mapping 11070 * of this file is via the following call chains: 11071 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11072 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11073 * 11074 * With the use of address space callbacks we are allowed to drop the 11075 * address space lock, a_lock, while executing the NFS operations that 11076 * need to go over the wire. Returning EAGAIN to the caller of this 11077 * function is what drives the execution of the callback that we add 11078 * below. The callback will be executed by the address space code 11079 * after dropping the a_lock. When the callback is finished, since 11080 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 11081 * is called again on the same segment to finish the rest of the work 11082 * that needs to happen during unmapping. 11083 * 11084 * This action of calling back into the segment driver causes 11085 * nfs4_delmap() to get called again, but since the callback was 11086 * already executed at this point, it already did the work and there 11087 * is nothing left for us to do. 11088 * 11089 * To Summarize: 11090 * - The first time nfs4_delmap is called by the current thread is when 11091 * we add the caller associated with this delmap to the delmap caller 11092 * list, add the callback, and return EAGAIN. 11093 * - The second time in this call chain when nfs4_delmap is called we 11094 * will find this caller in the delmap caller list and realize there 11095 * is no more work to do thus removing this caller from the list and 11096 * returning the error that was set in the callback execution. 11097 */ 11098 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 11099 if (caller_found) { 11100 /* 11101 * 'error' is from the actual delmap operations. To avoid 11102 * hangs, we need to handle the return of EAGAIN differently 11103 * since this is what drives the callback execution. 11104 * In this case, we don't want to return EAGAIN and do the 11105 * callback execution because there are none to execute. 11106 */ 11107 if (error == EAGAIN) 11108 return (0); 11109 else 11110 return (error); 11111 } 11112 11113 /* current caller was not in the list */ 11114 delmap_call = nfs4_init_delmapcall(); 11115 11116 mutex_enter(&rp->r_statelock); 11117 list_insert_tail(&rp->r_indelmap, delmap_call); 11118 mutex_exit(&rp->r_statelock); 11119 11120 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 11121 11122 dmapp->vp = vp; 11123 dmapp->off = off; 11124 dmapp->addr = addr; 11125 dmapp->len = len; 11126 dmapp->prot = prot; 11127 dmapp->maxprot = maxprot; 11128 dmapp->flags = flags; 11129 dmapp->cr = cr; 11130 dmapp->caller = delmap_call; 11131 11132 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 11133 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 11134 11135 return (error ? error : EAGAIN); 11136 } 11137 11138 static nfs4_delmapcall_t * 11139 nfs4_init_delmapcall() 11140 { 11141 nfs4_delmapcall_t *delmap_call; 11142 11143 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 11144 delmap_call->call_id = curthread; 11145 delmap_call->error = 0; 11146 11147 return (delmap_call); 11148 } 11149 11150 static void 11151 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 11152 { 11153 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 11154 } 11155 11156 /* 11157 * Searches for the current delmap caller (based on curthread) in the list of 11158 * callers. If it is found, we remove it and free the delmap caller. 11159 * Returns: 11160 * 0 if the caller wasn't found 11161 * 1 if the caller was found, removed and freed. *errp will be set 11162 * to what the result of the delmap was. 11163 */ 11164 static int 11165 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 11166 { 11167 nfs4_delmapcall_t *delmap_call; 11168 11169 /* 11170 * If the list doesn't exist yet, we create it and return 11171 * that the caller wasn't found. No list = no callers. 11172 */ 11173 mutex_enter(&rp->r_statelock); 11174 if (!(rp->r_flags & R4DELMAPLIST)) { 11175 /* The list does not exist */ 11176 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 11177 offsetof(nfs4_delmapcall_t, call_node)); 11178 rp->r_flags |= R4DELMAPLIST; 11179 mutex_exit(&rp->r_statelock); 11180 return (0); 11181 } else { 11182 /* The list exists so search it */ 11183 for (delmap_call = list_head(&rp->r_indelmap); 11184 delmap_call != NULL; 11185 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 11186 if (delmap_call->call_id == curthread) { 11187 /* current caller is in the list */ 11188 *errp = delmap_call->error; 11189 list_remove(&rp->r_indelmap, delmap_call); 11190 mutex_exit(&rp->r_statelock); 11191 nfs4_free_delmapcall(delmap_call); 11192 return (1); 11193 } 11194 } 11195 } 11196 mutex_exit(&rp->r_statelock); 11197 return (0); 11198 } 11199 11200 /* 11201 * Remove some pages from an mmap'd vnode. Just update the 11202 * count of pages. If doing close-to-open, then flush and 11203 * commit all of the pages associated with this file. 11204 * Otherwise, start an asynchronous page flush to write out 11205 * any dirty pages. This will also associate a credential 11206 * with the rnode which can be used to write the pages. 11207 */ 11208 /* ARGSUSED */ 11209 static void 11210 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11211 { 11212 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11213 rnode4_t *rp; 11214 mntinfo4_t *mi; 11215 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11216 11217 rp = VTOR4(dmapp->vp); 11218 mi = VTOMI4(dmapp->vp); 11219 11220 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11221 ASSERT(rp->r_mapcnt >= 0); 11222 11223 /* 11224 * Initiate a page flush and potential commit if there are 11225 * pages, the file system was not mounted readonly, the segment 11226 * was mapped shared, and the pages themselves were writeable. 11227 */ 11228 if (nfs4_has_pages(dmapp->vp) && 11229 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11230 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11231 mutex_enter(&rp->r_statelock); 11232 rp->r_flags |= R4DIRTY; 11233 mutex_exit(&rp->r_statelock); 11234 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11235 dmapp->len, dmapp->cr); 11236 if (!e.error) { 11237 mutex_enter(&rp->r_statelock); 11238 e.error = rp->r_error; 11239 rp->r_error = 0; 11240 mutex_exit(&rp->r_statelock); 11241 } 11242 } else 11243 e.error = 0; 11244 11245 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11246 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11247 B_INVAL, dmapp->cr, NULL); 11248 11249 if (e.error) { 11250 e.stat = puterrno4(e.error); 11251 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11252 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11253 dmapp->caller->error = e.error; 11254 } 11255 11256 /* Check to see if we need to close the file */ 11257 11258 if (dmapp->vp->v_type == VREG) { 11259 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11260 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11261 11262 if (e.error != 0 || e.stat != NFS4_OK) { 11263 /* 11264 * Since it is possible that e.error == 0 and 11265 * e.stat != NFS4_OK (and vice versa), 11266 * we do the proper checking in order to get both 11267 * e.error and e.stat reporting the correct info. 11268 */ 11269 if (e.stat == NFS4_OK) 11270 e.stat = puterrno4(e.error); 11271 if (e.error == 0) 11272 e.error = geterrno4(e.stat); 11273 11274 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11275 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11276 dmapp->caller->error = e.error; 11277 } 11278 } 11279 11280 (void) as_delete_callback(as, arg); 11281 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11282 } 11283 11284 11285 static uint_t 11286 fattr4_maxfilesize_to_bits(uint64_t ll) 11287 { 11288 uint_t l = 1; 11289 11290 if (ll == 0) { 11291 return (0); 11292 } 11293 11294 if (ll & 0xffffffff00000000) { 11295 l += 32; ll >>= 32; 11296 } 11297 if (ll & 0xffff0000) { 11298 l += 16; ll >>= 16; 11299 } 11300 if (ll & 0xff00) { 11301 l += 8; ll >>= 8; 11302 } 11303 if (ll & 0xf0) { 11304 l += 4; ll >>= 4; 11305 } 11306 if (ll & 0xc) { 11307 l += 2; ll >>= 2; 11308 } 11309 if (ll & 0x2) { 11310 l += 1; 11311 } 11312 return (l); 11313 } 11314 11315 static int 11316 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr) 11317 { 11318 vnode_t *avp = NULL; 11319 int error; 11320 11321 if ((error = nfs4lookup_xattr(vp, "", &avp, 11322 LOOKUP_XATTR, cr)) == 0) 11323 error = do_xattr_exists_check(avp, valp, cr); 11324 if (avp) 11325 VN_RELE(avp); 11326 11327 return (error); 11328 } 11329 11330 /* ARGSUSED */ 11331 int 11332 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 11333 caller_context_t *ct) 11334 { 11335 int error; 11336 hrtime_t t; 11337 rnode4_t *rp; 11338 nfs4_ga_res_t gar; 11339 nfs4_ga_ext_res_t ger; 11340 11341 gar.n4g_ext_res = &ger; 11342 11343 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11344 return (EIO); 11345 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11346 *valp = MAXPATHLEN; 11347 return (0); 11348 } 11349 if (cmd == _PC_ACL_ENABLED) { 11350 *valp = _ACL_ACE_ENABLED; 11351 return (0); 11352 } 11353 11354 rp = VTOR4(vp); 11355 if (cmd == _PC_XATTR_EXISTS) { 11356 /* 11357 * The existence of the xattr directory is not sufficient 11358 * for determining whether generic user attributes exists. 11359 * The attribute directory could only be a transient directory 11360 * used for Solaris sysattr support. Do a small readdir 11361 * to verify if the only entries are sysattrs or not. 11362 * 11363 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11364 * is NULL. Once the xadir vp exists, we can create xattrs, 11365 * and we don't have any way to update the "base" object's 11366 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11367 * could help out. 11368 */ 11369 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11370 rp->r_xattr_dir == NULL) { 11371 return (nfs4_have_xattrs(vp, valp, cr)); 11372 } 11373 } else { /* OLD CODE */ 11374 if (ATTRCACHE4_VALID(vp)) { 11375 mutex_enter(&rp->r_statelock); 11376 if (rp->r_pathconf.pc4_cache_valid) { 11377 error = 0; 11378 switch (cmd) { 11379 case _PC_FILESIZEBITS: 11380 *valp = 11381 rp->r_pathconf.pc4_filesizebits; 11382 break; 11383 case _PC_LINK_MAX: 11384 *valp = 11385 rp->r_pathconf.pc4_link_max; 11386 break; 11387 case _PC_NAME_MAX: 11388 *valp = 11389 rp->r_pathconf.pc4_name_max; 11390 break; 11391 case _PC_CHOWN_RESTRICTED: 11392 *valp = 11393 rp->r_pathconf.pc4_chown_restricted; 11394 break; 11395 case _PC_NO_TRUNC: 11396 *valp = 11397 rp->r_pathconf.pc4_no_trunc; 11398 break; 11399 default: 11400 error = EINVAL; 11401 break; 11402 } 11403 mutex_exit(&rp->r_statelock); 11404 #ifdef DEBUG 11405 nfs4_pathconf_cache_hits++; 11406 #endif 11407 return (error); 11408 } 11409 mutex_exit(&rp->r_statelock); 11410 } 11411 } 11412 #ifdef DEBUG 11413 nfs4_pathconf_cache_misses++; 11414 #endif 11415 11416 t = gethrtime(); 11417 11418 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11419 11420 if (error) { 11421 mutex_enter(&rp->r_statelock); 11422 rp->r_pathconf.pc4_cache_valid = FALSE; 11423 rp->r_pathconf.pc4_xattr_valid = FALSE; 11424 mutex_exit(&rp->r_statelock); 11425 return (error); 11426 } 11427 11428 /* interpret the max filesize */ 11429 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11430 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11431 11432 /* Store the attributes we just received */ 11433 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11434 11435 switch (cmd) { 11436 case _PC_FILESIZEBITS: 11437 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11438 break; 11439 case _PC_LINK_MAX: 11440 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11441 break; 11442 case _PC_NAME_MAX: 11443 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11444 break; 11445 case _PC_CHOWN_RESTRICTED: 11446 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11447 break; 11448 case _PC_NO_TRUNC: 11449 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11450 break; 11451 case _PC_XATTR_EXISTS: 11452 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) { 11453 if (error = nfs4_have_xattrs(vp, valp, cr)) 11454 return (error); 11455 } 11456 break; 11457 default: 11458 return (EINVAL); 11459 } 11460 11461 return (0); 11462 } 11463 11464 /* 11465 * Called by async thread to do synchronous pageio. Do the i/o, wait 11466 * for it to complete, and cleanup the page list when done. 11467 */ 11468 static int 11469 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11470 int flags, cred_t *cr) 11471 { 11472 int error; 11473 11474 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11475 11476 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11477 if (flags & B_READ) 11478 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11479 else 11480 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11481 return (error); 11482 } 11483 11484 /* ARGSUSED */ 11485 static int 11486 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11487 int flags, cred_t *cr, caller_context_t *ct) 11488 { 11489 int error; 11490 rnode4_t *rp; 11491 11492 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11493 return (EIO); 11494 11495 if (pp == NULL) 11496 return (EINVAL); 11497 11498 rp = VTOR4(vp); 11499 mutex_enter(&rp->r_statelock); 11500 rp->r_count++; 11501 mutex_exit(&rp->r_statelock); 11502 11503 if (flags & B_ASYNC) { 11504 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11505 nfs4_sync_pageio); 11506 } else 11507 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11508 mutex_enter(&rp->r_statelock); 11509 rp->r_count--; 11510 cv_broadcast(&rp->r_cv); 11511 mutex_exit(&rp->r_statelock); 11512 return (error); 11513 } 11514 11515 /* ARGSUSED */ 11516 static void 11517 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr, 11518 caller_context_t *ct) 11519 { 11520 int error; 11521 rnode4_t *rp; 11522 page_t *plist; 11523 page_t *pptr; 11524 offset3 offset; 11525 count3 len; 11526 k_sigset_t smask; 11527 11528 /* 11529 * We should get called with fl equal to either B_FREE or 11530 * B_INVAL. Any other value is illegal. 11531 * 11532 * The page that we are either supposed to free or destroy 11533 * should be exclusive locked and its io lock should not 11534 * be held. 11535 */ 11536 ASSERT(fl == B_FREE || fl == B_INVAL); 11537 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11538 11539 rp = VTOR4(vp); 11540 11541 /* 11542 * If the page doesn't need to be committed or we shouldn't 11543 * even bother attempting to commit it, then just make sure 11544 * that the p_fsdata byte is clear and then either free or 11545 * destroy the page as appropriate. 11546 */ 11547 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11548 pp->p_fsdata = C_NOCOMMIT; 11549 if (fl == B_FREE) 11550 page_free(pp, dn); 11551 else 11552 page_destroy(pp, dn); 11553 return; 11554 } 11555 11556 /* 11557 * If there is a page invalidation operation going on, then 11558 * if this is one of the pages being destroyed, then just 11559 * clear the p_fsdata byte and then either free or destroy 11560 * the page as appropriate. 11561 */ 11562 mutex_enter(&rp->r_statelock); 11563 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11564 mutex_exit(&rp->r_statelock); 11565 pp->p_fsdata = C_NOCOMMIT; 11566 if (fl == B_FREE) 11567 page_free(pp, dn); 11568 else 11569 page_destroy(pp, dn); 11570 return; 11571 } 11572 11573 /* 11574 * If we are freeing this page and someone else is already 11575 * waiting to do a commit, then just unlock the page and 11576 * return. That other thread will take care of commiting 11577 * this page. The page can be freed sometime after the 11578 * commit has finished. Otherwise, if the page is marked 11579 * as delay commit, then we may be getting called from 11580 * pvn_write_done, one page at a time. This could result 11581 * in one commit per page, so we end up doing lots of small 11582 * commits instead of fewer larger commits. This is bad, 11583 * we want do as few commits as possible. 11584 */ 11585 if (fl == B_FREE) { 11586 if (rp->r_flags & R4COMMITWAIT) { 11587 page_unlock(pp); 11588 mutex_exit(&rp->r_statelock); 11589 return; 11590 } 11591 if (pp->p_fsdata == C_DELAYCOMMIT) { 11592 pp->p_fsdata = C_COMMIT; 11593 page_unlock(pp); 11594 mutex_exit(&rp->r_statelock); 11595 return; 11596 } 11597 } 11598 11599 /* 11600 * Check to see if there is a signal which would prevent an 11601 * attempt to commit the pages from being successful. If so, 11602 * then don't bother with all of the work to gather pages and 11603 * generate the unsuccessful RPC. Just return from here and 11604 * let the page be committed at some later time. 11605 */ 11606 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11607 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11608 sigunintr(&smask); 11609 page_unlock(pp); 11610 mutex_exit(&rp->r_statelock); 11611 return; 11612 } 11613 sigunintr(&smask); 11614 11615 /* 11616 * We are starting to need to commit pages, so let's try 11617 * to commit as many as possible at once to reduce the 11618 * overhead. 11619 * 11620 * Set the `commit inprogress' state bit. We must 11621 * first wait until any current one finishes. Then 11622 * we initialize the c_pages list with this page. 11623 */ 11624 while (rp->r_flags & R4COMMIT) { 11625 rp->r_flags |= R4COMMITWAIT; 11626 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11627 rp->r_flags &= ~R4COMMITWAIT; 11628 } 11629 rp->r_flags |= R4COMMIT; 11630 mutex_exit(&rp->r_statelock); 11631 ASSERT(rp->r_commit.c_pages == NULL); 11632 rp->r_commit.c_pages = pp; 11633 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11634 rp->r_commit.c_commlen = PAGESIZE; 11635 11636 /* 11637 * Gather together all other pages which can be committed. 11638 * They will all be chained off r_commit.c_pages. 11639 */ 11640 nfs4_get_commit(vp); 11641 11642 /* 11643 * Clear the `commit inprogress' status and disconnect 11644 * the list of pages to be committed from the rnode. 11645 * At this same time, we also save the starting offset 11646 * and length of data to be committed on the server. 11647 */ 11648 plist = rp->r_commit.c_pages; 11649 rp->r_commit.c_pages = NULL; 11650 offset = rp->r_commit.c_commbase; 11651 len = rp->r_commit.c_commlen; 11652 mutex_enter(&rp->r_statelock); 11653 rp->r_flags &= ~R4COMMIT; 11654 cv_broadcast(&rp->r_commit.c_cv); 11655 mutex_exit(&rp->r_statelock); 11656 11657 if (curproc == proc_pageout || curproc == proc_fsflush || 11658 nfs_zone() != VTOMI4(vp)->mi_zone) { 11659 nfs4_async_commit(vp, plist, offset, len, 11660 cr, do_nfs4_async_commit); 11661 return; 11662 } 11663 11664 /* 11665 * Actually generate the COMMIT op over the wire operation. 11666 */ 11667 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11668 11669 /* 11670 * If we got an error during the commit, just unlock all 11671 * of the pages. The pages will get retransmitted to the 11672 * server during a putpage operation. 11673 */ 11674 if (error) { 11675 while (plist != NULL) { 11676 pptr = plist; 11677 page_sub(&plist, pptr); 11678 page_unlock(pptr); 11679 } 11680 return; 11681 } 11682 11683 /* 11684 * We've tried as hard as we can to commit the data to stable 11685 * storage on the server. We just unlock the rest of the pages 11686 * and clear the commit required state. They will be put 11687 * onto the tail of the cachelist if they are nolonger 11688 * mapped. 11689 */ 11690 while (plist != pp) { 11691 pptr = plist; 11692 page_sub(&plist, pptr); 11693 pptr->p_fsdata = C_NOCOMMIT; 11694 page_unlock(pptr); 11695 } 11696 11697 /* 11698 * It is possible that nfs4_commit didn't return error but 11699 * some other thread has modified the page we are going 11700 * to free/destroy. 11701 * In this case we need to rewrite the page. Do an explicit check 11702 * before attempting to free/destroy the page. If modified, needs to 11703 * be rewritten so unlock the page and return. 11704 */ 11705 if (hat_ismod(pp)) { 11706 pp->p_fsdata = C_NOCOMMIT; 11707 page_unlock(pp); 11708 return; 11709 } 11710 11711 /* 11712 * Now, as appropriate, either free or destroy the page 11713 * that we were called with. 11714 */ 11715 pp->p_fsdata = C_NOCOMMIT; 11716 if (fl == B_FREE) 11717 page_free(pp, dn); 11718 else 11719 page_destroy(pp, dn); 11720 } 11721 11722 /* 11723 * Commit requires that the current fh be the file written to. 11724 * The compound op structure is: 11725 * PUTFH(file), COMMIT 11726 */ 11727 static int 11728 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11729 { 11730 COMPOUND4args_clnt args; 11731 COMPOUND4res_clnt res; 11732 COMMIT4res *cm_res; 11733 nfs_argop4 argop[2]; 11734 nfs_resop4 *resop; 11735 int doqueue; 11736 mntinfo4_t *mi; 11737 rnode4_t *rp; 11738 cred_t *cred_otw = NULL; 11739 bool_t needrecov = FALSE; 11740 nfs4_recov_state_t recov_state; 11741 nfs4_open_stream_t *osp = NULL; 11742 bool_t first_time = TRUE; /* first time getting OTW cred */ 11743 bool_t last_time = FALSE; /* last time getting OTW cred */ 11744 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11745 11746 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11747 11748 rp = VTOR4(vp); 11749 11750 mi = VTOMI4(vp); 11751 recov_state.rs_flags = 0; 11752 recov_state.rs_num_retry_despite_err = 0; 11753 get_commit_cred: 11754 /* 11755 * Releases the osp, if a valid open stream is provided. 11756 * Puts a hold on the cred_otw and the new osp (if found). 11757 */ 11758 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11759 &first_time, &last_time); 11760 args.ctag = TAG_COMMIT; 11761 recov_retry: 11762 /* 11763 * Commit ops: putfh file; commit 11764 */ 11765 args.array_len = 2; 11766 args.array = argop; 11767 11768 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11769 &recov_state, NULL); 11770 if (e.error) { 11771 crfree(cred_otw); 11772 if (osp != NULL) 11773 open_stream_rele(osp, rp); 11774 return (e.error); 11775 } 11776 11777 /* putfh directory */ 11778 argop[0].argop = OP_CPUTFH; 11779 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11780 11781 /* commit */ 11782 argop[1].argop = OP_COMMIT; 11783 argop[1].nfs_argop4_u.opcommit.offset = offset; 11784 argop[1].nfs_argop4_u.opcommit.count = count; 11785 11786 doqueue = 1; 11787 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11788 11789 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11790 if (!needrecov && e.error) { 11791 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11792 needrecov); 11793 crfree(cred_otw); 11794 if (e.error == EACCES && last_time == FALSE) 11795 goto get_commit_cred; 11796 if (osp != NULL) 11797 open_stream_rele(osp, rp); 11798 return (e.error); 11799 } 11800 11801 if (needrecov) { 11802 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11803 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) { 11804 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11805 &recov_state, needrecov); 11806 if (!e.error) 11807 (void) xdr_free(xdr_COMPOUND4res_clnt, 11808 (caddr_t)&res); 11809 goto recov_retry; 11810 } 11811 if (e.error) { 11812 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11813 &recov_state, needrecov); 11814 crfree(cred_otw); 11815 if (osp != NULL) 11816 open_stream_rele(osp, rp); 11817 return (e.error); 11818 } 11819 /* fall through for res.status case */ 11820 } 11821 11822 if (res.status) { 11823 e.error = geterrno4(res.status); 11824 if (e.error == EACCES && last_time == FALSE) { 11825 crfree(cred_otw); 11826 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11827 &recov_state, needrecov); 11828 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11829 goto get_commit_cred; 11830 } 11831 /* 11832 * Can't do a nfs4_purge_stale_fh here because this 11833 * can cause a deadlock. nfs4_commit can 11834 * be called from nfs4_dispose which can be called 11835 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11836 * can call back to pvn_vplist_dirty. 11837 */ 11838 if (e.error == ESTALE) { 11839 mutex_enter(&rp->r_statelock); 11840 rp->r_flags |= R4STALE; 11841 if (!rp->r_error) 11842 rp->r_error = e.error; 11843 mutex_exit(&rp->r_statelock); 11844 PURGE_ATTRCACHE4(vp); 11845 } else { 11846 mutex_enter(&rp->r_statelock); 11847 if (!rp->r_error) 11848 rp->r_error = e.error; 11849 mutex_exit(&rp->r_statelock); 11850 } 11851 } else { 11852 ASSERT(rp->r_flags & R4HAVEVERF); 11853 resop = &res.array[1]; /* commit res */ 11854 cm_res = &resop->nfs_resop4_u.opcommit; 11855 mutex_enter(&rp->r_statelock); 11856 if (cm_res->writeverf == rp->r_writeverf) { 11857 mutex_exit(&rp->r_statelock); 11858 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11859 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11860 &recov_state, needrecov); 11861 crfree(cred_otw); 11862 if (osp != NULL) 11863 open_stream_rele(osp, rp); 11864 return (0); 11865 } 11866 nfs4_set_mod(vp); 11867 rp->r_writeverf = cm_res->writeverf; 11868 mutex_exit(&rp->r_statelock); 11869 e.error = NFS_VERF_MISMATCH; 11870 } 11871 11872 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11873 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11874 crfree(cred_otw); 11875 if (osp != NULL) 11876 open_stream_rele(osp, rp); 11877 11878 return (e.error); 11879 } 11880 11881 static void 11882 nfs4_set_mod(vnode_t *vp) 11883 { 11884 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11885 11886 /* make sure we're looking at the master vnode, not a shadow */ 11887 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check); 11888 } 11889 11890 /* 11891 * This function is used to gather a page list of the pages which 11892 * can be committed on the server. 11893 * 11894 * The calling thread must have set R4COMMIT. This bit is used to 11895 * serialize access to the commit structure in the rnode. As long 11896 * as the thread has set R4COMMIT, then it can manipulate the commit 11897 * structure without requiring any other locks. 11898 * 11899 * When this function is called from nfs4_dispose() the page passed 11900 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11901 * will skip it. This is not a problem since we initially add the 11902 * page to the r_commit page list. 11903 * 11904 */ 11905 static void 11906 nfs4_get_commit(vnode_t *vp) 11907 { 11908 rnode4_t *rp; 11909 page_t *pp; 11910 kmutex_t *vphm; 11911 11912 rp = VTOR4(vp); 11913 11914 ASSERT(rp->r_flags & R4COMMIT); 11915 11916 /* make sure we're looking at the master vnode, not a shadow */ 11917 11918 if (IS_SHADOW(vp, rp)) 11919 vp = RTOV4(rp); 11920 11921 vphm = page_vnode_mutex(vp); 11922 mutex_enter(vphm); 11923 11924 /* 11925 * If there are no pages associated with this vnode, then 11926 * just return. 11927 */ 11928 if ((pp = vp->v_pages) == NULL) { 11929 mutex_exit(vphm); 11930 return; 11931 } 11932 11933 /* 11934 * Step through all of the pages associated with this vnode 11935 * looking for pages which need to be committed. 11936 */ 11937 do { 11938 /* Skip marker pages. */ 11939 if (pp->p_hash == PVN_VPLIST_HASH_TAG) 11940 continue; 11941 11942 /* 11943 * First short-cut everything (without the page_lock) 11944 * and see if this page does not need to be committed 11945 * or is modified if so then we'll just skip it. 11946 */ 11947 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11948 continue; 11949 11950 /* 11951 * Attempt to lock the page. If we can't, then 11952 * someone else is messing with it or we have been 11953 * called from nfs4_dispose and this is the page that 11954 * nfs4_dispose was called with.. anyway just skip it. 11955 */ 11956 if (!page_trylock(pp, SE_EXCL)) 11957 continue; 11958 11959 /* 11960 * Lets check again now that we have the page lock. 11961 */ 11962 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11963 page_unlock(pp); 11964 continue; 11965 } 11966 11967 /* this had better not be a free page */ 11968 ASSERT(PP_ISFREE(pp) == 0); 11969 11970 /* 11971 * The page needs to be committed and we locked it. 11972 * Update the base and length parameters and add it 11973 * to r_pages. 11974 */ 11975 if (rp->r_commit.c_pages == NULL) { 11976 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11977 rp->r_commit.c_commlen = PAGESIZE; 11978 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11979 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11980 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11981 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11982 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11983 <= pp->p_offset) { 11984 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11985 rp->r_commit.c_commbase + PAGESIZE; 11986 } 11987 page_add(&rp->r_commit.c_pages, pp); 11988 } while ((pp = pp->p_vpnext) != vp->v_pages); 11989 11990 mutex_exit(vphm); 11991 } 11992 11993 /* 11994 * This routine is used to gather together a page list of the pages 11995 * which are to be committed on the server. This routine must not 11996 * be called if the calling thread holds any locked pages. 11997 * 11998 * The calling thread must have set R4COMMIT. This bit is used to 11999 * serialize access to the commit structure in the rnode. As long 12000 * as the thread has set R4COMMIT, then it can manipulate the commit 12001 * structure without requiring any other locks. 12002 */ 12003 static void 12004 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 12005 { 12006 12007 rnode4_t *rp; 12008 page_t *pp; 12009 u_offset_t end; 12010 u_offset_t off; 12011 ASSERT(len != 0); 12012 rp = VTOR4(vp); 12013 ASSERT(rp->r_flags & R4COMMIT); 12014 12015 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12016 12017 /* make sure we're looking at the master vnode, not a shadow */ 12018 12019 if (IS_SHADOW(vp, rp)) 12020 vp = RTOV4(rp); 12021 12022 /* 12023 * If there are no pages associated with this vnode, then 12024 * just return. 12025 */ 12026 if ((pp = vp->v_pages) == NULL) 12027 return; 12028 /* 12029 * Calculate the ending offset. 12030 */ 12031 end = soff + len; 12032 for (off = soff; off < end; off += PAGESIZE) { 12033 /* 12034 * Lookup each page by vp, offset. 12035 */ 12036 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 12037 continue; 12038 /* 12039 * If this page does not need to be committed or is 12040 * modified, then just skip it. 12041 */ 12042 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 12043 page_unlock(pp); 12044 continue; 12045 } 12046 12047 ASSERT(PP_ISFREE(pp) == 0); 12048 /* 12049 * The page needs to be committed and we locked it. 12050 * Update the base and length parameters and add it 12051 * to r_pages. 12052 */ 12053 if (rp->r_commit.c_pages == NULL) { 12054 rp->r_commit.c_commbase = (offset3)pp->p_offset; 12055 rp->r_commit.c_commlen = PAGESIZE; 12056 } else { 12057 rp->r_commit.c_commlen = (offset3)pp->p_offset - 12058 rp->r_commit.c_commbase + PAGESIZE; 12059 } 12060 page_add(&rp->r_commit.c_pages, pp); 12061 } 12062 } 12063 12064 /* 12065 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 12066 * Flushes and commits data to the server. 12067 */ 12068 static int 12069 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 12070 { 12071 int error; 12072 verifier4 write_verf; 12073 rnode4_t *rp = VTOR4(vp); 12074 12075 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12076 12077 /* 12078 * Flush the data portion of the file and then commit any 12079 * portions which need to be committed. This may need to 12080 * be done twice if the server has changed state since 12081 * data was last written. The data will need to be 12082 * rewritten to the server and then a new commit done. 12083 * 12084 * In fact, this may need to be done several times if the 12085 * server is having problems and crashing while we are 12086 * attempting to do this. 12087 */ 12088 12089 top: 12090 /* 12091 * Do a flush based on the poff and plen arguments. This 12092 * will synchronously write out any modified pages in the 12093 * range specified by (poff, plen). This starts all of the 12094 * i/o operations which will be waited for in the next 12095 * call to nfs4_putpage 12096 */ 12097 12098 mutex_enter(&rp->r_statelock); 12099 write_verf = rp->r_writeverf; 12100 mutex_exit(&rp->r_statelock); 12101 12102 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL); 12103 if (error == EAGAIN) 12104 error = 0; 12105 12106 /* 12107 * Do a flush based on the poff and plen arguments. This 12108 * will synchronously write out any modified pages in the 12109 * range specified by (poff, plen) and wait until all of 12110 * the asynchronous i/o's in that range are done as well. 12111 */ 12112 if (!error) 12113 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL); 12114 12115 if (error) 12116 return (error); 12117 12118 mutex_enter(&rp->r_statelock); 12119 if (rp->r_writeverf != write_verf) { 12120 mutex_exit(&rp->r_statelock); 12121 goto top; 12122 } 12123 mutex_exit(&rp->r_statelock); 12124 12125 /* 12126 * Now commit any pages which might need to be committed. 12127 * If the error, NFS_VERF_MISMATCH, is returned, then 12128 * start over with the flush operation. 12129 */ 12130 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 12131 12132 if (error == NFS_VERF_MISMATCH) 12133 goto top; 12134 12135 return (error); 12136 } 12137 12138 /* 12139 * nfs4_commit_vp() will wait for other pending commits and 12140 * will either commit the whole file or a range, plen dictates 12141 * if we commit whole file. a value of zero indicates the whole 12142 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 12143 */ 12144 static int 12145 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 12146 cred_t *cr, int wait_on_writes) 12147 { 12148 rnode4_t *rp; 12149 page_t *plist; 12150 offset3 offset; 12151 count3 len; 12152 12153 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12154 12155 rp = VTOR4(vp); 12156 12157 /* 12158 * before we gather commitable pages make 12159 * sure there are no outstanding async writes 12160 */ 12161 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 12162 mutex_enter(&rp->r_statelock); 12163 while (rp->r_count > 0) { 12164 cv_wait(&rp->r_cv, &rp->r_statelock); 12165 } 12166 mutex_exit(&rp->r_statelock); 12167 } 12168 12169 /* 12170 * Set the `commit inprogress' state bit. We must 12171 * first wait until any current one finishes. 12172 */ 12173 mutex_enter(&rp->r_statelock); 12174 while (rp->r_flags & R4COMMIT) { 12175 rp->r_flags |= R4COMMITWAIT; 12176 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 12177 rp->r_flags &= ~R4COMMITWAIT; 12178 } 12179 rp->r_flags |= R4COMMIT; 12180 mutex_exit(&rp->r_statelock); 12181 12182 /* 12183 * Gather all of the pages which need to be 12184 * committed. 12185 */ 12186 if (plen == 0) 12187 nfs4_get_commit(vp); 12188 else 12189 nfs4_get_commit_range(vp, poff, plen); 12190 12191 /* 12192 * Clear the `commit inprogress' bit and disconnect the 12193 * page list which was gathered by nfs4_get_commit. 12194 */ 12195 plist = rp->r_commit.c_pages; 12196 rp->r_commit.c_pages = NULL; 12197 offset = rp->r_commit.c_commbase; 12198 len = rp->r_commit.c_commlen; 12199 mutex_enter(&rp->r_statelock); 12200 rp->r_flags &= ~R4COMMIT; 12201 cv_broadcast(&rp->r_commit.c_cv); 12202 mutex_exit(&rp->r_statelock); 12203 12204 /* 12205 * If any pages need to be committed, commit them and 12206 * then unlock them so that they can be freed some 12207 * time later. 12208 */ 12209 if (plist == NULL) 12210 return (0); 12211 12212 /* 12213 * No error occurred during the flush portion 12214 * of this operation, so now attempt to commit 12215 * the data to stable storage on the server. 12216 * 12217 * This will unlock all of the pages on the list. 12218 */ 12219 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12220 } 12221 12222 static int 12223 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12224 cred_t *cr) 12225 { 12226 int error; 12227 page_t *pp; 12228 12229 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12230 12231 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12232 12233 /* 12234 * If we got an error, then just unlock all of the pages 12235 * on the list. 12236 */ 12237 if (error) { 12238 while (plist != NULL) { 12239 pp = plist; 12240 page_sub(&plist, pp); 12241 page_unlock(pp); 12242 } 12243 return (error); 12244 } 12245 /* 12246 * We've tried as hard as we can to commit the data to stable 12247 * storage on the server. We just unlock the pages and clear 12248 * the commit required state. They will get freed later. 12249 */ 12250 while (plist != NULL) { 12251 pp = plist; 12252 page_sub(&plist, pp); 12253 pp->p_fsdata = C_NOCOMMIT; 12254 page_unlock(pp); 12255 } 12256 12257 return (error); 12258 } 12259 12260 static void 12261 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12262 cred_t *cr) 12263 { 12264 12265 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12266 } 12267 12268 /*ARGSUSED*/ 12269 static int 12270 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12271 caller_context_t *ct) 12272 { 12273 int error = 0; 12274 mntinfo4_t *mi; 12275 vattr_t va; 12276 vsecattr_t nfsace4_vsap; 12277 12278 mi = VTOMI4(vp); 12279 if (nfs_zone() != mi->mi_zone) 12280 return (EIO); 12281 if (mi->mi_flags & MI4_ACL) { 12282 /* if we have a delegation, return it */ 12283 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12284 (void) nfs4delegreturn(VTOR4(vp), 12285 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12286 12287 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12288 NFS4_ACL_SET); 12289 if (error) /* EINVAL */ 12290 return (error); 12291 12292 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12293 /* 12294 * These are aclent_t type entries. 12295 */ 12296 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12297 vp->v_type == VDIR, FALSE); 12298 if (error) 12299 return (error); 12300 } else { 12301 /* 12302 * These are ace_t type entries. 12303 */ 12304 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12305 FALSE); 12306 if (error) 12307 return (error); 12308 } 12309 bzero(&va, sizeof (va)); 12310 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12311 vs_ace4_destroy(&nfsace4_vsap); 12312 return (error); 12313 } 12314 return (ENOSYS); 12315 } 12316 12317 /* ARGSUSED */ 12318 int 12319 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12320 caller_context_t *ct) 12321 { 12322 int error; 12323 mntinfo4_t *mi; 12324 nfs4_ga_res_t gar; 12325 rnode4_t *rp = VTOR4(vp); 12326 12327 mi = VTOMI4(vp); 12328 if (nfs_zone() != mi->mi_zone) 12329 return (EIO); 12330 12331 bzero(&gar, sizeof (gar)); 12332 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12333 12334 /* 12335 * vsecattr->vsa_mask holds the original acl request mask. 12336 * This is needed when determining what to return. 12337 * (See: nfs4_create_getsecattr_return()) 12338 */ 12339 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12340 if (error) /* EINVAL */ 12341 return (error); 12342 12343 /* 12344 * If this is a referral stub, don't try to go OTW for an ACL 12345 */ 12346 if (RP_ISSTUB_REFERRAL(VTOR4(vp))) 12347 return (fs_fab_acl(vp, vsecattr, flag, cr, ct)); 12348 12349 if (mi->mi_flags & MI4_ACL) { 12350 /* 12351 * Check if the data is cached and the cache is valid. If it 12352 * is we don't go over the wire. 12353 */ 12354 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12355 mutex_enter(&rp->r_statelock); 12356 if (rp->r_secattr != NULL) { 12357 error = nfs4_create_getsecattr_return( 12358 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12359 rp->r_attr.va_gid, 12360 vp->v_type == VDIR); 12361 if (!error) { /* error == 0 - Success! */ 12362 mutex_exit(&rp->r_statelock); 12363 return (error); 12364 } 12365 } 12366 mutex_exit(&rp->r_statelock); 12367 } 12368 12369 /* 12370 * The getattr otw call will always get both the acl, in 12371 * the form of a list of nfsace4's, and the number of acl 12372 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12373 */ 12374 gar.n4g_va.va_mask = AT_ALL; 12375 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12376 if (error) { 12377 vs_ace4_destroy(&gar.n4g_vsa); 12378 if (error == ENOTSUP || error == EOPNOTSUPP) 12379 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12380 return (error); 12381 } 12382 12383 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12384 /* 12385 * No error was returned, but according to the response 12386 * bitmap, neither was an acl. 12387 */ 12388 vs_ace4_destroy(&gar.n4g_vsa); 12389 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12390 return (error); 12391 } 12392 12393 /* 12394 * Update the cache with the ACL. 12395 */ 12396 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12397 12398 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12399 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12400 vp->v_type == VDIR); 12401 vs_ace4_destroy(&gar.n4g_vsa); 12402 if ((error) && (vsecattr->vsa_mask & 12403 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12404 (error != EACCES)) { 12405 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12406 } 12407 return (error); 12408 } 12409 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12410 return (error); 12411 } 12412 12413 /* 12414 * The function returns: 12415 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12416 * - EINVAL if the passed in "acl_mask" is an invalid request. 12417 * 12418 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12419 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12420 * 12421 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12422 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12423 * - We have a count field set without the corresponding acl field set. (e.g. - 12424 * VSA_ACECNT is set, but VSA_ACE is not) 12425 */ 12426 static int 12427 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12428 { 12429 /* Shortcut the masks that are always valid. */ 12430 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12431 return (0); 12432 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12433 return (0); 12434 12435 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12436 /* 12437 * We can't have any VSA_ACL type stuff in the mask now. 12438 */ 12439 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12440 VSA_DFACLCNT)) 12441 return (EINVAL); 12442 12443 if (op == NFS4_ACL_SET) { 12444 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12445 return (EINVAL); 12446 } 12447 } 12448 12449 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12450 /* 12451 * We can't have any VSA_ACE type stuff in the mask now. 12452 */ 12453 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12454 return (EINVAL); 12455 12456 if (op == NFS4_ACL_SET) { 12457 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12458 return (EINVAL); 12459 12460 if ((acl_mask & VSA_DFACLCNT) && 12461 !(acl_mask & VSA_DFACL)) 12462 return (EINVAL); 12463 } 12464 } 12465 return (0); 12466 } 12467 12468 /* 12469 * The theory behind creating the correct getsecattr return is simply this: 12470 * "Don't return anything that the caller is not expecting to have to free." 12471 */ 12472 static int 12473 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12474 uid_t uid, gid_t gid, int isdir) 12475 { 12476 int error = 0; 12477 /* Save the mask since the translators modify it. */ 12478 uint_t orig_mask = vsap->vsa_mask; 12479 12480 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12481 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE); 12482 12483 if (error) 12484 return (error); 12485 12486 /* 12487 * If the caller only asked for the ace count (VSA_ACECNT) 12488 * don't give them the full acl (VSA_ACE), free it. 12489 */ 12490 if (!orig_mask & VSA_ACE) { 12491 if (vsap->vsa_aclentp != NULL) { 12492 kmem_free(vsap->vsa_aclentp, 12493 vsap->vsa_aclcnt * sizeof (ace_t)); 12494 vsap->vsa_aclentp = NULL; 12495 } 12496 } 12497 vsap->vsa_mask = orig_mask; 12498 12499 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12500 VSA_DFACLCNT)) { 12501 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12502 isdir, FALSE); 12503 12504 if (error) 12505 return (error); 12506 12507 /* 12508 * If the caller only asked for the acl count (VSA_ACLCNT) 12509 * and/or the default acl count (VSA_DFACLCNT) don't give them 12510 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12511 */ 12512 if (!orig_mask & VSA_ACL) { 12513 if (vsap->vsa_aclentp != NULL) { 12514 kmem_free(vsap->vsa_aclentp, 12515 vsap->vsa_aclcnt * sizeof (aclent_t)); 12516 vsap->vsa_aclentp = NULL; 12517 } 12518 } 12519 12520 if (!orig_mask & VSA_DFACL) { 12521 if (vsap->vsa_dfaclentp != NULL) { 12522 kmem_free(vsap->vsa_dfaclentp, 12523 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12524 vsap->vsa_dfaclentp = NULL; 12525 } 12526 } 12527 vsap->vsa_mask = orig_mask; 12528 } 12529 return (0); 12530 } 12531 12532 /* ARGSUSED */ 12533 int 12534 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr, 12535 caller_context_t *ct) 12536 { 12537 int error; 12538 12539 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12540 return (EIO); 12541 /* 12542 * check for valid cmd parameter 12543 */ 12544 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12545 return (EINVAL); 12546 12547 /* 12548 * Check access permissions 12549 */ 12550 if ((cmd & F_SHARE) && 12551 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12552 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12553 return (EBADF); 12554 12555 /* 12556 * If the filesystem is mounted using local locking, pass the 12557 * request off to the local share code. 12558 */ 12559 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12560 return (fs_shrlock(vp, cmd, shr, flag, cr, ct)); 12561 12562 switch (cmd) { 12563 case F_SHARE: 12564 case F_UNSHARE: 12565 /* 12566 * This will be properly implemented later, 12567 * see RFE: 4823948 . 12568 */ 12569 error = EAGAIN; 12570 break; 12571 12572 case F_HASREMOTELOCKS: 12573 /* 12574 * NFS client can't store remote locks itself 12575 */ 12576 shr->s_access = 0; 12577 error = 0; 12578 break; 12579 12580 default: 12581 error = EINVAL; 12582 break; 12583 } 12584 12585 return (error); 12586 } 12587 12588 /* 12589 * Common code called by directory ops to update the attrcache 12590 */ 12591 static int 12592 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12593 hrtime_t t, vnode_t *vp, cred_t *cr) 12594 { 12595 int error = 0; 12596 12597 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12598 12599 if (status != NFS4_OK) { 12600 /* getattr not done or failed */ 12601 PURGE_ATTRCACHE4(vp); 12602 return (error); 12603 } 12604 12605 if (garp) { 12606 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12607 } else { 12608 PURGE_ATTRCACHE4(vp); 12609 } 12610 return (error); 12611 } 12612 12613 /* 12614 * Update directory caches for directory modification ops (link, rename, etc.) 12615 * When dinfo is NULL, manage dircaches in the old way. 12616 */ 12617 static void 12618 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12619 dirattr_info_t *dinfo) 12620 { 12621 rnode4_t *drp = VTOR4(dvp); 12622 12623 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12624 12625 /* Purge rddir cache for dir since it changed */ 12626 if (drp->r_dir != NULL) 12627 nfs4_purge_rddir_cache(dvp); 12628 12629 /* 12630 * If caller provided dinfo, then use it to manage dir caches. 12631 */ 12632 if (dinfo != NULL) { 12633 if (vp != NULL) { 12634 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12635 if (!VTOR4(vp)->created_v4) { 12636 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12637 dnlc_update(dvp, nm, vp); 12638 } else { 12639 /* 12640 * XXX don't update if the created_v4 flag is 12641 * set 12642 */ 12643 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12644 NFS4_DEBUG(nfs4_client_state_debug, 12645 (CE_NOTE, "nfs4_update_dircaches: " 12646 "don't update dnlc: created_v4 flag")); 12647 } 12648 } 12649 12650 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12651 dinfo->di_cred, FALSE, cinfo); 12652 12653 return; 12654 } 12655 12656 /* 12657 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12658 * Since caller modified dir but didn't receive post-dirmod-op dir 12659 * attrs, the dir's attrs must be purged. 12660 * 12661 * XXX this check and dnlc update/purge should really be atomic, 12662 * XXX but can't use rnode statelock because it'll deadlock in 12663 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12664 * XXX does occur. 12665 * 12666 * XXX We also may want to check that atomic is true in the 12667 * XXX change_info struct. If it is not, the change_info may 12668 * XXX reflect changes by more than one clients which means that 12669 * XXX our cache may not be valid. 12670 */ 12671 PURGE_ATTRCACHE4(dvp); 12672 if (drp->r_change == cinfo->before) { 12673 /* no changes took place in the directory prior to our link */ 12674 if (vp != NULL) { 12675 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12676 if (!VTOR4(vp)->created_v4) { 12677 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12678 dnlc_update(dvp, nm, vp); 12679 } else { 12680 /* 12681 * XXX dont' update if the created_v4 flag 12682 * is set 12683 */ 12684 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12685 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12686 "nfs4_update_dircaches: don't" 12687 " update dnlc: created_v4 flag")); 12688 } 12689 } 12690 } else { 12691 /* Another client modified directory - purge its dnlc cache */ 12692 dnlc_purge_vp(dvp); 12693 } 12694 } 12695 12696 /* 12697 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12698 * file. 12699 * 12700 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12701 * file (ie: client recovery) and otherwise set to FALSE. 12702 * 12703 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12704 * initiated) calling functions. 12705 * 12706 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12707 * of resending a 'lost' open request. 12708 * 12709 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12710 * server that hands out BAD_SEQID on open confirm. 12711 * 12712 * Errors are returned via the nfs4_error_t parameter. 12713 */ 12714 void 12715 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12716 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12717 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12718 { 12719 COMPOUND4args_clnt args; 12720 COMPOUND4res_clnt res; 12721 nfs_argop4 argop[2]; 12722 nfs_resop4 *resop; 12723 int doqueue = 1; 12724 mntinfo4_t *mi; 12725 OPEN_CONFIRM4args *open_confirm_args; 12726 int needrecov; 12727 12728 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12729 #if DEBUG 12730 mutex_enter(&oop->oo_lock); 12731 ASSERT(oop->oo_seqid_inuse); 12732 mutex_exit(&oop->oo_lock); 12733 #endif 12734 12735 recov_retry_confirm: 12736 nfs4_error_zinit(ep); 12737 *retry_open = FALSE; 12738 12739 if (resend) 12740 args.ctag = TAG_OPEN_CONFIRM_LOST; 12741 else 12742 args.ctag = TAG_OPEN_CONFIRM; 12743 12744 args.array_len = 2; 12745 args.array = argop; 12746 12747 /* putfh target fh */ 12748 argop[0].argop = OP_CPUTFH; 12749 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12750 12751 argop[1].argop = OP_OPEN_CONFIRM; 12752 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12753 12754 (*seqid) += 1; 12755 open_confirm_args->seqid = *seqid; 12756 open_confirm_args->open_stateid = *stateid; 12757 12758 mi = VTOMI4(vp); 12759 12760 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12761 12762 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12763 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12764 } 12765 12766 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12767 if (!needrecov && ep->error) 12768 return; 12769 12770 if (needrecov) { 12771 bool_t abort = FALSE; 12772 12773 if (reopening_file == FALSE) { 12774 nfs4_bseqid_entry_t *bsep = NULL; 12775 12776 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12777 bsep = nfs4_create_bseqid_entry(oop, NULL, 12778 vp, 0, args.ctag, 12779 open_confirm_args->seqid); 12780 12781 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 12782 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL); 12783 if (bsep) { 12784 kmem_free(bsep, sizeof (*bsep)); 12785 if (num_bseqid_retryp && 12786 --(*num_bseqid_retryp) == 0) 12787 abort = TRUE; 12788 } 12789 } 12790 if ((ep->error == ETIMEDOUT || 12791 res.status == NFS4ERR_RESOURCE) && 12792 abort == FALSE && resend == FALSE) { 12793 if (!ep->error) 12794 (void) xdr_free(xdr_COMPOUND4res_clnt, 12795 (caddr_t)&res); 12796 12797 delay(SEC_TO_TICK(confirm_retry_sec)); 12798 goto recov_retry_confirm; 12799 } 12800 /* State may have changed so retry the entire OPEN op */ 12801 if (abort == FALSE) 12802 *retry_open = TRUE; 12803 else 12804 *retry_open = FALSE; 12805 if (!ep->error) 12806 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12807 return; 12808 } 12809 12810 if (res.status) { 12811 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12812 return; 12813 } 12814 12815 resop = &res.array[1]; /* open confirm res */ 12816 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12817 stateid, sizeof (*stateid)); 12818 12819 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12820 } 12821 12822 /* 12823 * Return the credentials associated with a client state object. The 12824 * caller is responsible for freeing the credentials. 12825 */ 12826 12827 static cred_t * 12828 state_to_cred(nfs4_open_stream_t *osp) 12829 { 12830 cred_t *cr; 12831 12832 /* 12833 * It's ok to not lock the open stream and open owner to get 12834 * the oo_cred since this is only written once (upon creation) 12835 * and will not change. 12836 */ 12837 cr = osp->os_open_owner->oo_cred; 12838 crhold(cr); 12839 12840 return (cr); 12841 } 12842 12843 /* 12844 * nfs4_find_sysid 12845 * 12846 * Find the sysid for the knetconfig associated with the given mi. 12847 */ 12848 static struct lm_sysid * 12849 nfs4_find_sysid(mntinfo4_t *mi) 12850 { 12851 ASSERT(nfs_zone() == mi->mi_zone); 12852 12853 /* 12854 * Switch from RDMA knconf to original mount knconf 12855 */ 12856 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12857 mi->mi_curr_serv->sv_hostname, NULL)); 12858 } 12859 12860 #ifdef DEBUG 12861 /* 12862 * Return a string version of the call type for easy reading. 12863 */ 12864 static char * 12865 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12866 { 12867 switch (ctype) { 12868 case NFS4_LCK_CTYPE_NORM: 12869 return ("NORMAL"); 12870 case NFS4_LCK_CTYPE_RECLAIM: 12871 return ("RECLAIM"); 12872 case NFS4_LCK_CTYPE_RESEND: 12873 return ("RESEND"); 12874 case NFS4_LCK_CTYPE_REINSTATE: 12875 return ("REINSTATE"); 12876 default: 12877 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12878 "type %d", ctype); 12879 return (""); 12880 } 12881 } 12882 #endif 12883 12884 /* 12885 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12886 * Unlock requests don't have an over-the-wire locktype, so we just return 12887 * something non-threatening. 12888 */ 12889 12890 static nfs_lock_type4 12891 flk_to_locktype(int cmd, int l_type) 12892 { 12893 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12894 12895 switch (l_type) { 12896 case F_UNLCK: 12897 return (READ_LT); 12898 case F_RDLCK: 12899 if (cmd == F_SETLK) 12900 return (READ_LT); 12901 else 12902 return (READW_LT); 12903 case F_WRLCK: 12904 if (cmd == F_SETLK) 12905 return (WRITE_LT); 12906 else 12907 return (WRITEW_LT); 12908 } 12909 panic("flk_to_locktype"); 12910 /*NOTREACHED*/ 12911 } 12912 12913 /* 12914 * Do some preliminary checks for nfs4frlock. 12915 */ 12916 static int 12917 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12918 u_offset_t offset) 12919 { 12920 int error = 0; 12921 12922 /* 12923 * If we are setting a lock, check that the file is opened 12924 * with the correct mode. 12925 */ 12926 if (cmd == F_SETLK || cmd == F_SETLKW) { 12927 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12928 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12929 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12930 "nfs4frlock_validate_args: file was opened with " 12931 "incorrect mode")); 12932 return (EBADF); 12933 } 12934 } 12935 12936 /* Convert the offset. It may need to be restored before returning. */ 12937 if (error = convoff(vp, flk, 0, offset)) { 12938 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12939 "nfs4frlock_validate_args: convoff => error= %d\n", 12940 error)); 12941 return (error); 12942 } 12943 12944 return (error); 12945 } 12946 12947 /* 12948 * Set the flock64's lm_sysid for nfs4frlock. 12949 */ 12950 static int 12951 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12952 { 12953 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12954 12955 /* Find the lm_sysid */ 12956 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12957 12958 if (*lspp == NULL) { 12959 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12960 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12961 return (ENOLCK); 12962 } 12963 12964 flk->l_sysid = lm_sysidt(*lspp); 12965 12966 return (0); 12967 } 12968 12969 /* 12970 * Do the remaining preliminary setup for nfs4frlock. 12971 */ 12972 static void 12973 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12974 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12975 cred_t **cred_otw) 12976 { 12977 /* 12978 * set tick_delay to the base delay time. 12979 * (nfs4_base_wait_time is in msecs) 12980 */ 12981 12982 *tick_delayp = drv_usectohz(nfs4_base_wait_time * 1000); 12983 12984 /* 12985 * If lock is relative to EOF, we need the newest length of the 12986 * file. Therefore invalidate the ATTR_CACHE. 12987 */ 12988 12989 *whencep = flk->l_whence; 12990 12991 if (*whencep == 2) /* SEEK_END */ 12992 PURGE_ATTRCACHE4(vp); 12993 12994 recov_statep->rs_flags = 0; 12995 recov_statep->rs_num_retry_despite_err = 0; 12996 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 12997 } 12998 12999 /* 13000 * Initialize and allocate the data structures necessary for 13001 * the nfs4frlock call. 13002 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 13003 */ 13004 static void 13005 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 13006 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 13007 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 13008 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 13009 { 13010 int argoplist_size; 13011 int num_ops = 2; 13012 13013 *retry = FALSE; 13014 *did_start_fop = FALSE; 13015 *skip_get_err = FALSE; 13016 lost_rqstp->lr_op = 0; 13017 argoplist_size = num_ops * sizeof (nfs_argop4); 13018 /* fill array with zero */ 13019 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 13020 13021 *argspp = argsp; 13022 *respp = NULL; 13023 13024 argsp->array_len = num_ops; 13025 argsp->array = *argopp; 13026 13027 /* initialize in case of error; will get real value down below */ 13028 argsp->ctag = TAG_NONE; 13029 13030 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 13031 *op_hintp = OH_LOCKU; 13032 else 13033 *op_hintp = OH_OTHER; 13034 } 13035 13036 /* 13037 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 13038 * the proper nfs4_server_t for this instance of nfs4frlock. 13039 * Returns 0 (success) or an errno value. 13040 */ 13041 static int 13042 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 13043 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 13044 bool_t *did_start_fop, bool_t *startrecovp) 13045 { 13046 int error = 0; 13047 rnode4_t *rp; 13048 13049 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13050 13051 if (ctype == NFS4_LCK_CTYPE_NORM) { 13052 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 13053 recov_statep, startrecovp); 13054 if (error) 13055 return (error); 13056 *did_start_fop = TRUE; 13057 } else { 13058 *did_start_fop = FALSE; 13059 *startrecovp = FALSE; 13060 } 13061 13062 if (!error) { 13063 rp = VTOR4(vp); 13064 13065 /* If the file failed recovery, just quit. */ 13066 mutex_enter(&rp->r_statelock); 13067 if (rp->r_flags & R4RECOVERR) { 13068 error = EIO; 13069 } 13070 mutex_exit(&rp->r_statelock); 13071 } 13072 13073 return (error); 13074 } 13075 13076 /* 13077 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 13078 * resend nfs4frlock call is initiated by the recovery framework. 13079 * Acquires the lop and oop seqid synchronization. 13080 */ 13081 static void 13082 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 13083 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 13084 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13085 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 13086 { 13087 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 13088 int error; 13089 13090 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 13091 (CE_NOTE, 13092 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 13093 ASSERT(resend_rqstp != NULL); 13094 ASSERT(resend_rqstp->lr_op == OP_LOCK || 13095 resend_rqstp->lr_op == OP_LOCKU); 13096 13097 *oopp = resend_rqstp->lr_oop; 13098 if (resend_rqstp->lr_oop) { 13099 open_owner_hold(resend_rqstp->lr_oop); 13100 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 13101 ASSERT(error == 0); /* recov thread always succeeds */ 13102 } 13103 13104 /* Must resend this lost lock/locku request. */ 13105 ASSERT(resend_rqstp->lr_lop != NULL); 13106 *lopp = resend_rqstp->lr_lop; 13107 lock_owner_hold(resend_rqstp->lr_lop); 13108 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 13109 ASSERT(error == 0); /* recov thread always succeeds */ 13110 13111 *ospp = resend_rqstp->lr_osp; 13112 if (*ospp) 13113 open_stream_hold(resend_rqstp->lr_osp); 13114 13115 if (resend_rqstp->lr_op == OP_LOCK) { 13116 LOCK4args *lock_args; 13117 13118 argop->argop = OP_LOCK; 13119 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 13120 lock_args->locktype = resend_rqstp->lr_locktype; 13121 lock_args->reclaim = 13122 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 13123 lock_args->offset = resend_rqstp->lr_flk->l_start; 13124 lock_args->length = resend_rqstp->lr_flk->l_len; 13125 if (lock_args->length == 0) 13126 lock_args->length = ~lock_args->length; 13127 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 13128 mi2clientid(mi), &lock_args->locker); 13129 13130 switch (resend_rqstp->lr_ctype) { 13131 case NFS4_LCK_CTYPE_RESEND: 13132 argsp->ctag = TAG_LOCK_RESEND; 13133 break; 13134 case NFS4_LCK_CTYPE_REINSTATE: 13135 argsp->ctag = TAG_LOCK_REINSTATE; 13136 break; 13137 case NFS4_LCK_CTYPE_RECLAIM: 13138 argsp->ctag = TAG_LOCK_RECLAIM; 13139 break; 13140 default: 13141 argsp->ctag = TAG_LOCK_UNKNOWN; 13142 break; 13143 } 13144 } else { 13145 LOCKU4args *locku_args; 13146 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 13147 13148 argop->argop = OP_LOCKU; 13149 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 13150 locku_args->locktype = READ_LT; 13151 locku_args->seqid = lop->lock_seqid + 1; 13152 mutex_enter(&lop->lo_lock); 13153 locku_args->lock_stateid = lop->lock_stateid; 13154 mutex_exit(&lop->lo_lock); 13155 locku_args->offset = resend_rqstp->lr_flk->l_start; 13156 locku_args->length = resend_rqstp->lr_flk->l_len; 13157 if (locku_args->length == 0) 13158 locku_args->length = ~locku_args->length; 13159 13160 switch (resend_rqstp->lr_ctype) { 13161 case NFS4_LCK_CTYPE_RESEND: 13162 argsp->ctag = TAG_LOCKU_RESEND; 13163 break; 13164 case NFS4_LCK_CTYPE_REINSTATE: 13165 argsp->ctag = TAG_LOCKU_REINSTATE; 13166 break; 13167 default: 13168 argsp->ctag = TAG_LOCK_UNKNOWN; 13169 break; 13170 } 13171 } 13172 } 13173 13174 /* 13175 * Setup the LOCKT4 arguments. 13176 */ 13177 static void 13178 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13179 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 13180 rnode4_t *rp) 13181 { 13182 LOCKT4args *lockt_args; 13183 13184 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 13185 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13186 argop->argop = OP_LOCKT; 13187 argsp->ctag = TAG_LOCKT; 13188 lockt_args = &argop->nfs_argop4_u.oplockt; 13189 13190 /* 13191 * The locktype will be READ_LT unless it's 13192 * a write lock. We do this because the Solaris 13193 * system call allows the combination of 13194 * F_UNLCK and F_GETLK* and so in that case the 13195 * unlock is mapped to a read. 13196 */ 13197 if (flk->l_type == F_WRLCK) 13198 lockt_args->locktype = WRITE_LT; 13199 else 13200 lockt_args->locktype = READ_LT; 13201 13202 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 13203 /* set the lock owner4 args */ 13204 nfs4_setlockowner_args(&lockt_args->owner, rp, 13205 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13206 flk->l_pid); 13207 lockt_args->offset = flk->l_start; 13208 lockt_args->length = flk->l_len; 13209 if (flk->l_len == 0) 13210 lockt_args->length = ~lockt_args->length; 13211 13212 *lockt_argsp = lockt_args; 13213 } 13214 13215 /* 13216 * If the client is holding a delegation, and the open stream to be used 13217 * with this lock request is a delegation open stream, then re-open the stream. 13218 * Sets the nfs4_error_t to all zeros unless the open stream has already 13219 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13220 * means the caller should retry (like a recovery retry). 13221 */ 13222 static void 13223 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13224 { 13225 open_delegation_type4 dt; 13226 bool_t reopen_needed, force; 13227 nfs4_open_stream_t *osp; 13228 open_claim_type4 oclaim; 13229 rnode4_t *rp = VTOR4(vp); 13230 mntinfo4_t *mi = VTOMI4(vp); 13231 13232 ASSERT(nfs_zone() == mi->mi_zone); 13233 13234 nfs4_error_zinit(ep); 13235 13236 mutex_enter(&rp->r_statev4_lock); 13237 dt = rp->r_deleg_type; 13238 mutex_exit(&rp->r_statev4_lock); 13239 13240 if (dt != OPEN_DELEGATE_NONE) { 13241 nfs4_open_owner_t *oop; 13242 13243 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13244 if (!oop) { 13245 ep->stat = NFS4ERR_IO; 13246 return; 13247 } 13248 /* returns with 'os_sync_lock' held */ 13249 osp = find_open_stream(oop, rp); 13250 if (!osp) { 13251 open_owner_rele(oop); 13252 ep->stat = NFS4ERR_IO; 13253 return; 13254 } 13255 13256 if (osp->os_failed_reopen) { 13257 NFS4_DEBUG((nfs4_open_stream_debug || 13258 nfs4_client_lock_debug), (CE_NOTE, 13259 "nfs4frlock_check_deleg: os_failed_reopen set " 13260 "for osp %p, cr %p, rp %s", (void *)osp, 13261 (void *)cr, rnode4info(rp))); 13262 mutex_exit(&osp->os_sync_lock); 13263 open_stream_rele(osp, rp); 13264 open_owner_rele(oop); 13265 ep->stat = NFS4ERR_IO; 13266 return; 13267 } 13268 13269 /* 13270 * Determine whether a reopen is needed. If this 13271 * is a delegation open stream, then send the open 13272 * to the server to give visibility to the open owner. 13273 * Even if it isn't a delegation open stream, we need 13274 * to check if the previous open CLAIM_DELEGATE_CUR 13275 * was sufficient. 13276 */ 13277 13278 reopen_needed = osp->os_delegation || 13279 ((lt == F_RDLCK && 13280 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13281 (lt == F_WRLCK && 13282 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13283 13284 mutex_exit(&osp->os_sync_lock); 13285 open_owner_rele(oop); 13286 13287 if (reopen_needed) { 13288 /* 13289 * Always use CLAIM_PREVIOUS after server reboot. 13290 * The server will reject CLAIM_DELEGATE_CUR if 13291 * it is used during the grace period. 13292 */ 13293 mutex_enter(&mi->mi_lock); 13294 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13295 oclaim = CLAIM_PREVIOUS; 13296 force = TRUE; 13297 } else { 13298 oclaim = CLAIM_DELEGATE_CUR; 13299 force = FALSE; 13300 } 13301 mutex_exit(&mi->mi_lock); 13302 13303 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13304 if (ep->error == EAGAIN) { 13305 nfs4_error_zinit(ep); 13306 ep->stat = NFS4ERR_DELAY; 13307 } 13308 } 13309 open_stream_rele(osp, rp); 13310 osp = NULL; 13311 } 13312 } 13313 13314 /* 13315 * Setup the LOCKU4 arguments. 13316 * Returns errors via the nfs4_error_t. 13317 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13318 * over-the-wire. The caller must release the 13319 * reference on *lopp. 13320 * NFS4ERR_DELAY caller should retry (like recovery retry) 13321 * (other) unrecoverable error. 13322 */ 13323 static void 13324 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13325 LOCKU4args **locku_argsp, flock64_t *flk, 13326 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13327 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13328 bool_t *skip_get_err, bool_t *go_otwp) 13329 { 13330 nfs4_lock_owner_t *lop = NULL; 13331 LOCKU4args *locku_args; 13332 pid_t pid; 13333 bool_t is_spec = FALSE; 13334 rnode4_t *rp = VTOR4(vp); 13335 13336 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13337 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13338 13339 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13340 if (ep->error || ep->stat) 13341 return; 13342 13343 argop->argop = OP_LOCKU; 13344 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13345 argsp->ctag = TAG_LOCKU_REINSTATE; 13346 else 13347 argsp->ctag = TAG_LOCKU; 13348 locku_args = &argop->nfs_argop4_u.oplocku; 13349 *locku_argsp = locku_args; 13350 13351 /* 13352 * XXX what should locku_args->locktype be? 13353 * setting to ALWAYS be READ_LT so at least 13354 * it is a valid locktype. 13355 */ 13356 13357 locku_args->locktype = READ_LT; 13358 13359 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13360 flk->l_pid; 13361 13362 /* 13363 * Get the lock owner stateid. If no lock owner 13364 * exists, return success. 13365 */ 13366 lop = find_lock_owner(rp, pid, LOWN_ANY); 13367 *lopp = lop; 13368 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13369 is_spec = TRUE; 13370 if (!lop || is_spec) { 13371 /* 13372 * No lock owner so no locks to unlock. 13373 * Return success. If there was a failed 13374 * reclaim earlier, the lock might still be 13375 * registered with the local locking code, 13376 * so notify it of the unlock. 13377 * 13378 * If the lockowner is using a special stateid, 13379 * then the original lock request (that created 13380 * this lockowner) was never successful, so we 13381 * have no lock to undo OTW. 13382 */ 13383 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13384 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13385 "(%ld) so return success", (long)pid)); 13386 13387 if (ctype == NFS4_LCK_CTYPE_NORM) 13388 flk->l_pid = curproc->p_pid; 13389 nfs4_register_lock_locally(vp, flk, flag, offset); 13390 /* 13391 * Release our hold and NULL out so final_cleanup 13392 * doesn't try to end a lock seqid sync we 13393 * never started. 13394 */ 13395 if (is_spec) { 13396 lock_owner_rele(lop); 13397 *lopp = NULL; 13398 } 13399 *skip_get_err = TRUE; 13400 *go_otwp = FALSE; 13401 return; 13402 } 13403 13404 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13405 if (ep->error == EAGAIN) { 13406 lock_owner_rele(lop); 13407 *lopp = NULL; 13408 return; 13409 } 13410 13411 mutex_enter(&lop->lo_lock); 13412 locku_args->lock_stateid = lop->lock_stateid; 13413 mutex_exit(&lop->lo_lock); 13414 locku_args->seqid = lop->lock_seqid + 1; 13415 13416 /* leave the ref count on lop, rele after RPC call */ 13417 13418 locku_args->offset = flk->l_start; 13419 locku_args->length = flk->l_len; 13420 if (flk->l_len == 0) 13421 locku_args->length = ~locku_args->length; 13422 13423 *go_otwp = TRUE; 13424 } 13425 13426 /* 13427 * Setup the LOCK4 arguments. 13428 * 13429 * Returns errors via the nfs4_error_t. 13430 * NFS4_OK no problems 13431 * NFS4ERR_DELAY caller should retry (like recovery retry) 13432 * (other) unrecoverable error 13433 */ 13434 static void 13435 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13436 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13437 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13438 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13439 { 13440 LOCK4args *lock_args; 13441 nfs4_open_owner_t *oop = NULL; 13442 nfs4_open_stream_t *osp = NULL; 13443 nfs4_lock_owner_t *lop = NULL; 13444 pid_t pid; 13445 rnode4_t *rp = VTOR4(vp); 13446 13447 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13448 13449 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13450 if (ep->error || ep->stat != NFS4_OK) 13451 return; 13452 13453 argop->argop = OP_LOCK; 13454 if (ctype == NFS4_LCK_CTYPE_NORM) 13455 argsp->ctag = TAG_LOCK; 13456 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13457 argsp->ctag = TAG_RELOCK; 13458 else 13459 argsp->ctag = TAG_LOCK_REINSTATE; 13460 lock_args = &argop->nfs_argop4_u.oplock; 13461 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13462 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13463 /* 13464 * Get the lock owner. If no lock owner exists, 13465 * create a 'temporary' one and grab the open seqid 13466 * synchronization (which puts a hold on the open 13467 * owner and open stream). 13468 * This also grabs the lock seqid synchronization. 13469 */ 13470 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13471 ep->stat = 13472 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13473 13474 if (ep->stat != NFS4_OK) 13475 goto out; 13476 13477 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13478 &lock_args->locker); 13479 13480 lock_args->offset = flk->l_start; 13481 lock_args->length = flk->l_len; 13482 if (flk->l_len == 0) 13483 lock_args->length = ~lock_args->length; 13484 *lock_argsp = lock_args; 13485 out: 13486 *oopp = oop; 13487 *ospp = osp; 13488 *lopp = lop; 13489 } 13490 13491 /* 13492 * After we get the reply from the server, record the proper information 13493 * for possible resend lock requests. 13494 * 13495 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13496 */ 13497 static void 13498 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13499 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13500 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13501 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13502 { 13503 bool_t unlock = (flk->l_type == F_UNLCK); 13504 13505 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13506 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13507 ctype == NFS4_LCK_CTYPE_REINSTATE); 13508 13509 if (error != 0 && !unlock) { 13510 NFS4_DEBUG((nfs4_lost_rqst_debug || 13511 nfs4_client_lock_debug), (CE_NOTE, 13512 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13513 " for lop %p", (void *)lop)); 13514 ASSERT(lop != NULL); 13515 mutex_enter(&lop->lo_lock); 13516 lop->lo_pending_rqsts = 1; 13517 mutex_exit(&lop->lo_lock); 13518 } 13519 13520 lost_rqstp->lr_putfirst = FALSE; 13521 lost_rqstp->lr_op = 0; 13522 13523 /* 13524 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13525 * recovery purposes so that the lock request that was sent 13526 * can be saved and re-issued later. Ditto for EIO from a forced 13527 * unmount. This is done to have the client's local locking state 13528 * match the v4 server's state; that is, the request was 13529 * potentially received and accepted by the server but the client 13530 * thinks it was not. 13531 */ 13532 if (error == ETIMEDOUT || error == EINTR || 13533 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13534 NFS4_DEBUG((nfs4_lost_rqst_debug || 13535 nfs4_client_lock_debug), (CE_NOTE, 13536 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13537 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13538 (void *)lop, (void *)oop, (void *)osp)); 13539 if (unlock) 13540 lost_rqstp->lr_op = OP_LOCKU; 13541 else { 13542 lost_rqstp->lr_op = OP_LOCK; 13543 lost_rqstp->lr_locktype = locktype; 13544 } 13545 /* 13546 * Objects are held and rele'd via the recovery code. 13547 * See nfs4_save_lost_rqst. 13548 */ 13549 lost_rqstp->lr_vp = vp; 13550 lost_rqstp->lr_dvp = NULL; 13551 lost_rqstp->lr_oop = oop; 13552 lost_rqstp->lr_osp = osp; 13553 lost_rqstp->lr_lop = lop; 13554 lost_rqstp->lr_cr = cr; 13555 switch (ctype) { 13556 case NFS4_LCK_CTYPE_NORM: 13557 flk->l_pid = ttoproc(curthread)->p_pid; 13558 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13559 break; 13560 case NFS4_LCK_CTYPE_REINSTATE: 13561 lost_rqstp->lr_putfirst = TRUE; 13562 lost_rqstp->lr_ctype = ctype; 13563 break; 13564 default: 13565 break; 13566 } 13567 lost_rqstp->lr_flk = flk; 13568 } 13569 } 13570 13571 /* 13572 * Update lop's seqid. Also update the seqid stored in a resend request, 13573 * if any. (Some recovery errors increment the seqid, and we may have to 13574 * send the resend request again.) 13575 */ 13576 13577 static void 13578 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13579 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13580 { 13581 if (lock_args) { 13582 if (lock_args->locker.new_lock_owner == TRUE) 13583 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13584 else { 13585 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13586 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13587 } 13588 } else if (locku_args) { 13589 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13590 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13591 } 13592 } 13593 13594 /* 13595 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13596 * COMPOUND4 args/res for calls that need to retry. 13597 * Switches the *cred_otwp to base_cr. 13598 */ 13599 static void 13600 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13601 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13602 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13603 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13604 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13605 { 13606 nfs4_open_owner_t *oop = *oopp; 13607 nfs4_open_stream_t *osp = *ospp; 13608 nfs4_lock_owner_t *lop = *lopp; 13609 nfs_argop4 *argop = (*argspp)->array; 13610 13611 if (*did_start_fop) { 13612 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13613 needrecov); 13614 *did_start_fop = FALSE; 13615 } 13616 ASSERT((*argspp)->array_len == 2); 13617 if (argop[1].argop == OP_LOCK) 13618 nfs4args_lock_free(&argop[1]); 13619 else if (argop[1].argop == OP_LOCKT) 13620 nfs4args_lockt_free(&argop[1]); 13621 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13622 if (!error) 13623 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13624 *argspp = NULL; 13625 *respp = NULL; 13626 13627 if (lop) { 13628 nfs4_end_lock_seqid_sync(lop); 13629 lock_owner_rele(lop); 13630 *lopp = NULL; 13631 } 13632 13633 /* need to free up the reference on osp for lock args */ 13634 if (osp != NULL) { 13635 open_stream_rele(osp, VTOR4(vp)); 13636 *ospp = NULL; 13637 } 13638 13639 /* need to free up the reference on oop for lock args */ 13640 if (oop != NULL) { 13641 nfs4_end_open_seqid_sync(oop); 13642 open_owner_rele(oop); 13643 *oopp = NULL; 13644 } 13645 13646 crfree(*cred_otwp); 13647 *cred_otwp = base_cr; 13648 crhold(*cred_otwp); 13649 } 13650 13651 /* 13652 * Function to process the client's recovery for nfs4frlock. 13653 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13654 * 13655 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13656 * COMPOUND4 args/res for calls that need to retry. 13657 * 13658 * Note: the rp's r_lkserlock is *not* dropped during this path. 13659 */ 13660 static bool_t 13661 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13662 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13663 LOCK4args *lock_args, LOCKU4args *locku_args, 13664 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13665 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13666 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13667 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13668 { 13669 nfs4_open_owner_t *oop = *oopp; 13670 nfs4_open_stream_t *osp = *ospp; 13671 nfs4_lock_owner_t *lop = *lopp; 13672 13673 bool_t abort, retry; 13674 13675 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13676 ASSERT((*argspp) != NULL); 13677 ASSERT((*respp) != NULL); 13678 if (lock_args || locku_args) 13679 ASSERT(lop != NULL); 13680 13681 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13682 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13683 13684 retry = TRUE; 13685 abort = FALSE; 13686 if (needrecov) { 13687 nfs4_bseqid_entry_t *bsep = NULL; 13688 nfs_opnum4 op; 13689 13690 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13691 13692 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13693 seqid4 seqid; 13694 13695 if (lock_args) { 13696 if (lock_args->locker.new_lock_owner == TRUE) 13697 seqid = lock_args->locker.locker4_u. 13698 open_owner.open_seqid; 13699 else 13700 seqid = lock_args->locker.locker4_u. 13701 lock_owner.lock_seqid; 13702 } else if (locku_args) { 13703 seqid = locku_args->seqid; 13704 } else { 13705 seqid = 0; 13706 } 13707 13708 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13709 flk->l_pid, (*argspp)->ctag, seqid); 13710 } 13711 13712 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13713 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13714 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13715 NULL, op, bsep, NULL, NULL); 13716 13717 if (bsep) 13718 kmem_free(bsep, sizeof (*bsep)); 13719 } 13720 13721 /* 13722 * Return that we do not want to retry the request for 3 cases: 13723 * 1. If we received EINTR or are bailing out because of a forced 13724 * unmount, we came into this code path just for the sake of 13725 * initiating recovery, we now need to return the error. 13726 * 2. If we have aborted recovery. 13727 * 3. We received NFS4ERR_BAD_SEQID. 13728 */ 13729 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13730 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13731 retry = FALSE; 13732 13733 if (*did_start_fop == TRUE) { 13734 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13735 needrecov); 13736 *did_start_fop = FALSE; 13737 } 13738 13739 if (retry == TRUE) { 13740 nfs_argop4 *argop; 13741 13742 argop = (*argspp)->array; 13743 ASSERT((*argspp)->array_len == 2); 13744 13745 if (argop[1].argop == OP_LOCK) 13746 nfs4args_lock_free(&argop[1]); 13747 else if (argop[1].argop == OP_LOCKT) 13748 nfs4args_lockt_free(&argop[1]); 13749 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13750 if (!ep->error) 13751 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13752 *respp = NULL; 13753 *argspp = NULL; 13754 } 13755 13756 if (lop != NULL) { 13757 nfs4_end_lock_seqid_sync(lop); 13758 lock_owner_rele(lop); 13759 } 13760 13761 *lopp = NULL; 13762 13763 /* need to free up the reference on osp for lock args */ 13764 if (osp != NULL) { 13765 open_stream_rele(osp, rp); 13766 *ospp = NULL; 13767 } 13768 13769 /* need to free up the reference on oop for lock args */ 13770 if (oop != NULL) { 13771 nfs4_end_open_seqid_sync(oop); 13772 open_owner_rele(oop); 13773 *oopp = NULL; 13774 } 13775 13776 return (retry); 13777 } 13778 13779 /* 13780 * Handles the successful reply from the server for nfs4frlock. 13781 */ 13782 static void 13783 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13784 vnode_t *vp, int flag, u_offset_t offset, 13785 nfs4_lost_rqst_t *resend_rqstp) 13786 { 13787 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13788 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13789 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13790 if (ctype == NFS4_LCK_CTYPE_NORM) { 13791 flk->l_pid = ttoproc(curthread)->p_pid; 13792 /* 13793 * We do not register lost locks locally in 13794 * the 'resend' case since the user/application 13795 * doesn't think we have the lock. 13796 */ 13797 ASSERT(!resend_rqstp); 13798 nfs4_register_lock_locally(vp, flk, flag, offset); 13799 } 13800 } 13801 } 13802 13803 /* 13804 * Handle the DENIED reply from the server for nfs4frlock. 13805 * Returns TRUE if we should retry the request; FALSE otherwise. 13806 * 13807 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13808 * COMPOUND4 args/res for calls that need to retry. Can also 13809 * drop and regrab the r_lkserlock. 13810 */ 13811 static bool_t 13812 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13813 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13814 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13815 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13816 nfs4_recov_state_t *recov_statep, int needrecov, 13817 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13818 clock_t *tick_delayp, short *whencep, int *errorp, 13819 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13820 bool_t *skip_get_err) 13821 { 13822 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13823 13824 if (lock_args) { 13825 nfs4_open_owner_t *oop = *oopp; 13826 nfs4_open_stream_t *osp = *ospp; 13827 nfs4_lock_owner_t *lop = *lopp; 13828 int intr; 13829 13830 /* 13831 * Blocking lock needs to sleep and retry from the request. 13832 * 13833 * Do not block and wait for 'resend' or 'reinstate' 13834 * lock requests, just return the error. 13835 * 13836 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13837 */ 13838 if (cmd == F_SETLKW) { 13839 rnode4_t *rp = VTOR4(vp); 13840 nfs_argop4 *argop = (*argspp)->array; 13841 13842 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13843 13844 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13845 recov_statep, needrecov); 13846 *did_start_fop = FALSE; 13847 ASSERT((*argspp)->array_len == 2); 13848 if (argop[1].argop == OP_LOCK) 13849 nfs4args_lock_free(&argop[1]); 13850 else if (argop[1].argop == OP_LOCKT) 13851 nfs4args_lockt_free(&argop[1]); 13852 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13853 if (*respp) 13854 (void) xdr_free(xdr_COMPOUND4res_clnt, 13855 (caddr_t)*respp); 13856 *argspp = NULL; 13857 *respp = NULL; 13858 nfs4_end_lock_seqid_sync(lop); 13859 lock_owner_rele(lop); 13860 *lopp = NULL; 13861 if (osp != NULL) { 13862 open_stream_rele(osp, rp); 13863 *ospp = NULL; 13864 } 13865 if (oop != NULL) { 13866 nfs4_end_open_seqid_sync(oop); 13867 open_owner_rele(oop); 13868 *oopp = NULL; 13869 } 13870 13871 nfs_rw_exit(&rp->r_lkserlock); 13872 13873 intr = nfs4_block_and_wait(tick_delayp); 13874 13875 if (intr) { 13876 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13877 RW_WRITER, FALSE); 13878 *errorp = EINTR; 13879 return (FALSE); 13880 } 13881 13882 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13883 RW_WRITER, FALSE); 13884 13885 /* 13886 * Make sure we are still safe to lock with 13887 * regards to mmapping. 13888 */ 13889 if (!nfs4_safelock(vp, flk, cr)) { 13890 *errorp = EAGAIN; 13891 return (FALSE); 13892 } 13893 13894 return (TRUE); 13895 } 13896 if (ctype == NFS4_LCK_CTYPE_NORM) 13897 *errorp = EAGAIN; 13898 *skip_get_err = TRUE; 13899 flk->l_whence = 0; 13900 *whencep = 0; 13901 return (FALSE); 13902 } else if (lockt_args) { 13903 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13904 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13905 13906 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13907 flk, lockt_args); 13908 13909 /* according to NLM code */ 13910 *errorp = 0; 13911 *whencep = 0; 13912 *skip_get_err = TRUE; 13913 return (FALSE); 13914 } 13915 return (FALSE); 13916 } 13917 13918 /* 13919 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13920 */ 13921 static void 13922 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13923 { 13924 switch (resp->status) { 13925 case NFS4ERR_ACCESS: 13926 case NFS4ERR_ADMIN_REVOKED: 13927 case NFS4ERR_BADHANDLE: 13928 case NFS4ERR_BAD_RANGE: 13929 case NFS4ERR_BAD_SEQID: 13930 case NFS4ERR_BAD_STATEID: 13931 case NFS4ERR_BADXDR: 13932 case NFS4ERR_DEADLOCK: 13933 case NFS4ERR_DELAY: 13934 case NFS4ERR_EXPIRED: 13935 case NFS4ERR_FHEXPIRED: 13936 case NFS4ERR_GRACE: 13937 case NFS4ERR_INVAL: 13938 case NFS4ERR_ISDIR: 13939 case NFS4ERR_LEASE_MOVED: 13940 case NFS4ERR_LOCK_NOTSUPP: 13941 case NFS4ERR_LOCK_RANGE: 13942 case NFS4ERR_MOVED: 13943 case NFS4ERR_NOFILEHANDLE: 13944 case NFS4ERR_NO_GRACE: 13945 case NFS4ERR_OLD_STATEID: 13946 case NFS4ERR_OPENMODE: 13947 case NFS4ERR_RECLAIM_BAD: 13948 case NFS4ERR_RECLAIM_CONFLICT: 13949 case NFS4ERR_RESOURCE: 13950 case NFS4ERR_SERVERFAULT: 13951 case NFS4ERR_STALE: 13952 case NFS4ERR_STALE_CLIENTID: 13953 case NFS4ERR_STALE_STATEID: 13954 return; 13955 default: 13956 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13957 "nfs4frlock_results_default: got unrecognizable " 13958 "res.status %d", resp->status)); 13959 *errorp = NFS4ERR_INVAL; 13960 } 13961 } 13962 13963 /* 13964 * The lock request was successful, so update the client's state. 13965 */ 13966 static void 13967 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13968 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13969 vnode_t *vp, flock64_t *flk, cred_t *cr, 13970 nfs4_lost_rqst_t *resend_rqstp) 13971 { 13972 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13973 13974 if (lock_args) { 13975 LOCK4res *lock_res; 13976 13977 lock_res = &resop->nfs_resop4_u.oplock; 13978 /* update the stateid with server's response */ 13979 13980 if (lock_args->locker.new_lock_owner == TRUE) { 13981 mutex_enter(&lop->lo_lock); 13982 lop->lo_just_created = NFS4_PERM_CREATED; 13983 mutex_exit(&lop->lo_lock); 13984 } 13985 13986 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13987 13988 /* 13989 * If the lock was the result of a resending a lost 13990 * request, we've synched up the stateid and seqid 13991 * with the server, but now the server might be out of sync 13992 * with what the application thinks it has for locks. 13993 * Clean that up here. It's unclear whether we should do 13994 * this even if the filesystem has been forcibly unmounted. 13995 * For most servers, it's probably wasted effort, but 13996 * RFC3530 lets servers require that unlocks exactly match 13997 * the locks that are held. 13998 */ 13999 if (resend_rqstp != NULL && 14000 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 14001 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 14002 } else { 14003 flk->l_whence = 0; 14004 } 14005 } else if (locku_args) { 14006 LOCKU4res *locku_res; 14007 14008 locku_res = &resop->nfs_resop4_u.oplocku; 14009 14010 /* Update the stateid with the server's response */ 14011 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 14012 } else if (lockt_args) { 14013 /* Switch the lock type to express success, see fcntl */ 14014 flk->l_type = F_UNLCK; 14015 flk->l_whence = 0; 14016 } 14017 } 14018 14019 /* 14020 * Do final cleanup before exiting nfs4frlock. 14021 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 14022 * COMPOUND4 args/res for calls that haven't already. 14023 */ 14024 static void 14025 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 14026 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 14027 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 14028 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 14029 short whence, u_offset_t offset, struct lm_sysid *ls, 14030 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 14031 bool_t did_start_fop, bool_t skip_get_err, 14032 cred_t *cred_otw, cred_t *cred) 14033 { 14034 mntinfo4_t *mi = VTOMI4(vp); 14035 rnode4_t *rp = VTOR4(vp); 14036 int error = *errorp; 14037 nfs_argop4 *argop; 14038 int do_flush_pages = 0; 14039 14040 ASSERT(nfs_zone() == mi->mi_zone); 14041 /* 14042 * The client recovery code wants the raw status information, 14043 * so don't map the NFS status code to an errno value for 14044 * non-normal call types. 14045 */ 14046 if (ctype == NFS4_LCK_CTYPE_NORM) { 14047 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 14048 *errorp = geterrno4(resp->status); 14049 if (did_start_fop == TRUE) 14050 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 14051 needrecov); 14052 14053 /* 14054 * We've established a new lock on the server, so invalidate 14055 * the pages associated with the vnode to get the most up to 14056 * date pages from the server after acquiring the lock. We 14057 * want to be sure that the read operation gets the newest data. 14058 * N.B. 14059 * We used to do this in nfs4frlock_results_ok but that doesn't 14060 * work since VOP_PUTPAGE can call nfs4_commit which calls 14061 * nfs4_start_fop. We flush the pages below after calling 14062 * nfs4_end_fop above 14063 * The flush of the page cache must be done after 14064 * nfs4_end_open_seqid_sync() to avoid a 4-way hang. 14065 */ 14066 if (!error && resp && resp->status == NFS4_OK) 14067 do_flush_pages = 1; 14068 } 14069 if (argsp) { 14070 ASSERT(argsp->array_len == 2); 14071 argop = argsp->array; 14072 if (argop[1].argop == OP_LOCK) 14073 nfs4args_lock_free(&argop[1]); 14074 else if (argop[1].argop == OP_LOCKT) 14075 nfs4args_lockt_free(&argop[1]); 14076 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14077 if (resp) 14078 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 14079 } 14080 14081 /* free the reference on the lock owner */ 14082 if (lop != NULL) { 14083 nfs4_end_lock_seqid_sync(lop); 14084 lock_owner_rele(lop); 14085 } 14086 14087 /* need to free up the reference on osp for lock args */ 14088 if (osp != NULL) 14089 open_stream_rele(osp, rp); 14090 14091 /* need to free up the reference on oop for lock args */ 14092 if (oop != NULL) { 14093 nfs4_end_open_seqid_sync(oop); 14094 open_owner_rele(oop); 14095 } 14096 14097 if (do_flush_pages) 14098 nfs4_flush_pages(vp, cred); 14099 14100 (void) convoff(vp, flk, whence, offset); 14101 14102 lm_rel_sysid(ls); 14103 14104 /* 14105 * Record debug information in the event we get EINVAL. 14106 */ 14107 mutex_enter(&mi->mi_lock); 14108 if (*errorp == EINVAL && (lock_args || locku_args) && 14109 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 14110 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 14111 zcmn_err(getzoneid(), CE_NOTE, 14112 "%s operation failed with " 14113 "EINVAL probably since the server, %s," 14114 " doesn't support POSIX style locking", 14115 lock_args ? "LOCK" : "LOCKU", 14116 mi->mi_curr_serv->sv_hostname); 14117 mi->mi_flags |= MI4_LOCK_DEBUG; 14118 } 14119 } 14120 mutex_exit(&mi->mi_lock); 14121 14122 if (cred_otw) 14123 crfree(cred_otw); 14124 } 14125 14126 /* 14127 * This calls the server and the local locking code. 14128 * 14129 * Client locks are registerred locally by oring the sysid with 14130 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 14131 * We need to distinguish between the two to avoid collision in case one 14132 * machine is used as both client and server. 14133 * 14134 * Blocking lock requests will continually retry to acquire the lock 14135 * forever. 14136 * 14137 * The ctype is defined as follows: 14138 * NFS4_LCK_CTYPE_NORM: normal lock request. 14139 * 14140 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 14141 * recovery, get the pid from flk instead of curproc, and don't reregister 14142 * the lock locally. 14143 * 14144 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 14145 * that we will use the information passed in via resend_rqstp to setup the 14146 * lock/locku request. This resend is the exact same request as the 'lost 14147 * lock', and is initiated by the recovery framework. A successful resend 14148 * request can initiate one or more reinstate requests. 14149 * 14150 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 14151 * does not trigger additional reinstate requests. This lock call type is 14152 * set for setting the v4 server's locking state back to match what the 14153 * client's local locking state is in the event of a received 'lost lock'. 14154 * 14155 * Errors are returned via the nfs4_error_t parameter. 14156 */ 14157 void 14158 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 14159 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 14160 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 14161 { 14162 COMPOUND4args_clnt args, *argsp = NULL; 14163 COMPOUND4res_clnt res, *resp = NULL; 14164 nfs_argop4 *argop; 14165 nfs_resop4 *resop; 14166 rnode4_t *rp; 14167 int doqueue = 1; 14168 clock_t tick_delay; /* delay in clock ticks */ 14169 struct lm_sysid *ls; 14170 LOCK4args *lock_args = NULL; 14171 LOCKU4args *locku_args = NULL; 14172 LOCKT4args *lockt_args = NULL; 14173 nfs4_open_owner_t *oop = NULL; 14174 nfs4_open_stream_t *osp = NULL; 14175 nfs4_lock_owner_t *lop = NULL; 14176 bool_t needrecov = FALSE; 14177 nfs4_recov_state_t recov_state; 14178 short whence; 14179 nfs4_op_hint_t op_hint; 14180 nfs4_lost_rqst_t lost_rqst; 14181 bool_t retry = FALSE; 14182 bool_t did_start_fop = FALSE; 14183 bool_t skip_get_err = FALSE; 14184 cred_t *cred_otw = NULL; 14185 bool_t recovonly; /* just queue request */ 14186 int frc_no_reclaim = 0; 14187 #ifdef DEBUG 14188 char *name; 14189 #endif 14190 14191 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14192 14193 #ifdef DEBUG 14194 name = fn_name(VTOSV(vp)->sv_name); 14195 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 14196 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 14197 "length %"PRIu64", pid %d, sysid %d, call type %s, " 14198 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 14199 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 14200 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 14201 resend_rqstp ? "TRUE" : "FALSE")); 14202 kmem_free(name, MAXNAMELEN); 14203 #endif 14204 14205 nfs4_error_zinit(ep); 14206 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14207 if (ep->error) 14208 return; 14209 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14210 if (ep->error) 14211 return; 14212 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14213 vp, cr, &cred_otw); 14214 14215 recov_retry: 14216 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14217 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14218 rp = VTOR4(vp); 14219 14220 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14221 &did_start_fop, &recovonly); 14222 14223 if (ep->error) 14224 goto out; 14225 14226 if (recovonly) { 14227 /* 14228 * Leave the request for the recovery system to deal with. 14229 */ 14230 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14231 ASSERT(cmd != F_GETLK); 14232 ASSERT(flk->l_type == F_UNLCK); 14233 14234 nfs4_error_init(ep, EINTR); 14235 needrecov = TRUE; 14236 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14237 if (lop != NULL) { 14238 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14239 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14240 (void) nfs4_start_recovery(ep, 14241 VTOMI4(vp), vp, NULL, NULL, 14242 (lost_rqst.lr_op == OP_LOCK || 14243 lost_rqst.lr_op == OP_LOCKU) ? 14244 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL); 14245 lock_owner_rele(lop); 14246 lop = NULL; 14247 } 14248 flk->l_pid = curproc->p_pid; 14249 nfs4_register_lock_locally(vp, flk, flag, offset); 14250 goto out; 14251 } 14252 14253 /* putfh directory fh */ 14254 argop[0].argop = OP_CPUTFH; 14255 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14256 14257 /* 14258 * Set up the over-the-wire arguments and get references to the 14259 * open owner, etc. 14260 */ 14261 14262 if (ctype == NFS4_LCK_CTYPE_RESEND || 14263 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14264 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14265 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14266 } else { 14267 bool_t go_otw = TRUE; 14268 14269 ASSERT(resend_rqstp == NULL); 14270 14271 switch (cmd) { 14272 case F_GETLK: 14273 case F_O_GETLK: 14274 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14275 &lockt_args, argsp, flk, rp); 14276 break; 14277 case F_SETLKW: 14278 case F_SETLK: 14279 if (flk->l_type == F_UNLCK) 14280 nfs4frlock_setup_locku_args(ctype, 14281 &argop[1], &locku_args, flk, 14282 &lop, ep, argsp, 14283 vp, flag, offset, cr, 14284 &skip_get_err, &go_otw); 14285 else 14286 nfs4frlock_setup_lock_args(ctype, 14287 &lock_args, &oop, &osp, &lop, &argop[1], 14288 argsp, flk, cmd, vp, cr, ep); 14289 14290 if (ep->error) 14291 goto out; 14292 14293 switch (ep->stat) { 14294 case NFS4_OK: 14295 break; 14296 case NFS4ERR_DELAY: 14297 /* recov thread never gets this error */ 14298 ASSERT(resend_rqstp == NULL); 14299 ASSERT(did_start_fop); 14300 14301 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14302 &recov_state, TRUE); 14303 did_start_fop = FALSE; 14304 if (argop[1].argop == OP_LOCK) 14305 nfs4args_lock_free(&argop[1]); 14306 else if (argop[1].argop == OP_LOCKT) 14307 nfs4args_lockt_free(&argop[1]); 14308 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14309 argsp = NULL; 14310 goto recov_retry; 14311 default: 14312 ep->error = EIO; 14313 goto out; 14314 } 14315 break; 14316 default: 14317 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14318 "nfs4_frlock: invalid cmd %d", cmd)); 14319 ep->error = EINVAL; 14320 goto out; 14321 } 14322 14323 if (!go_otw) 14324 goto out; 14325 } 14326 14327 /* XXX should we use the local reclock as a cache ? */ 14328 /* 14329 * Unregister the lock with the local locking code before 14330 * contacting the server. This avoids a potential race where 14331 * another process gets notified that it has been granted a lock 14332 * before we can unregister ourselves locally. 14333 */ 14334 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14335 if (ctype == NFS4_LCK_CTYPE_NORM) 14336 flk->l_pid = ttoproc(curthread)->p_pid; 14337 nfs4_register_lock_locally(vp, flk, flag, offset); 14338 } 14339 14340 /* 14341 * Send the server the lock request. Continually loop with a delay 14342 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14343 */ 14344 resp = &res; 14345 14346 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14347 (CE_NOTE, 14348 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14349 rnode4info(rp))); 14350 14351 if (lock_args && frc_no_reclaim) { 14352 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14353 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14354 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14355 lock_args->reclaim = FALSE; 14356 if (did_reclaimp) 14357 *did_reclaimp = 0; 14358 } 14359 14360 /* 14361 * Do the OTW call. 14362 */ 14363 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14364 14365 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14366 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14367 14368 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14369 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14370 "nfs4frlock: needrecov %d", needrecov)); 14371 14372 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14373 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14374 args.ctag); 14375 14376 /* 14377 * Check if one of these mutually exclusive error cases has 14378 * happened: 14379 * need to swap credentials due to access error 14380 * recovery is needed 14381 * different error (only known case is missing Kerberos ticket) 14382 */ 14383 14384 if ((ep->error == EACCES || 14385 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14386 cred_otw != cr) { 14387 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14388 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14389 cr, &cred_otw); 14390 goto recov_retry; 14391 } 14392 14393 if (needrecov) { 14394 /* 14395 * LOCKT requests don't need to recover from lost 14396 * requests since they don't create/modify state. 14397 */ 14398 if ((ep->error == EINTR || 14399 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14400 lockt_args) 14401 goto out; 14402 /* 14403 * Do not attempt recovery for requests initiated by 14404 * the recovery framework. Let the framework redrive them. 14405 */ 14406 if (ctype != NFS4_LCK_CTYPE_NORM) 14407 goto out; 14408 else { 14409 ASSERT(resend_rqstp == NULL); 14410 } 14411 14412 nfs4frlock_save_lost_rqst(ctype, ep->error, 14413 flk_to_locktype(cmd, flk->l_type), 14414 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14415 14416 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14417 &resp, lock_args, locku_args, &oop, &osp, &lop, 14418 rp, vp, &recov_state, op_hint, &did_start_fop, 14419 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14420 14421 if (retry) { 14422 ASSERT(oop == NULL); 14423 ASSERT(osp == NULL); 14424 ASSERT(lop == NULL); 14425 goto recov_retry; 14426 } 14427 goto out; 14428 } 14429 14430 /* 14431 * Bail out if have reached this point with ep->error set. Can 14432 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14433 * This happens if Kerberos ticket has expired or has been 14434 * destroyed. 14435 */ 14436 if (ep->error != 0) 14437 goto out; 14438 14439 /* 14440 * Process the reply. 14441 */ 14442 switch (resp->status) { 14443 case NFS4_OK: 14444 resop = &resp->array[1]; 14445 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14446 resend_rqstp); 14447 /* 14448 * Have a successful lock operation, now update state. 14449 */ 14450 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14451 resop, lop, vp, flk, cr, resend_rqstp); 14452 break; 14453 14454 case NFS4ERR_DENIED: 14455 resop = &resp->array[1]; 14456 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14457 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14458 &recov_state, needrecov, &argsp, &resp, 14459 &tick_delay, &whence, &ep->error, resop, cr, 14460 &did_start_fop, &skip_get_err); 14461 14462 if (retry) { 14463 ASSERT(oop == NULL); 14464 ASSERT(osp == NULL); 14465 ASSERT(lop == NULL); 14466 goto recov_retry; 14467 } 14468 break; 14469 /* 14470 * If the server won't let us reclaim, fall-back to trying to lock 14471 * the file from scratch. Code elsewhere will check the changeinfo 14472 * to ensure the file hasn't been changed. 14473 */ 14474 case NFS4ERR_NO_GRACE: 14475 if (lock_args && lock_args->reclaim == TRUE) { 14476 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14477 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14478 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14479 frc_no_reclaim = 1; 14480 /* clean up before retrying */ 14481 needrecov = 0; 14482 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14483 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14484 &recov_state, op_hint, &did_start_fop, NULL, flk); 14485 goto recov_retry; 14486 } 14487 /* FALLTHROUGH */ 14488 14489 default: 14490 nfs4frlock_results_default(resp, &ep->error); 14491 break; 14492 } 14493 out: 14494 /* 14495 * Process and cleanup from error. Make interrupted unlock 14496 * requests look successful, since they will be handled by the 14497 * client recovery code. 14498 */ 14499 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14500 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14501 lock_args, locku_args, did_start_fop, 14502 skip_get_err, cred_otw, cr); 14503 14504 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14505 (cmd == F_SETLK || cmd == F_SETLKW)) 14506 ep->error = 0; 14507 } 14508 14509 /* 14510 * nfs4_safelock: 14511 * 14512 * Return non-zero if the given lock request can be handled without 14513 * violating the constraints on concurrent mapping and locking. 14514 */ 14515 14516 static int 14517 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14518 { 14519 rnode4_t *rp = VTOR4(vp); 14520 struct vattr va; 14521 int error; 14522 14523 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14524 ASSERT(rp->r_mapcnt >= 0); 14525 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14526 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14527 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14528 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14529 14530 if (rp->r_mapcnt == 0) 14531 return (1); /* always safe if not mapped */ 14532 14533 /* 14534 * If the file is already mapped and there are locks, then they 14535 * should be all safe locks. So adding or removing a lock is safe 14536 * as long as the new request is safe (i.e., whole-file, meaning 14537 * length and starting offset are both zero). 14538 */ 14539 14540 if (bfp->l_start != 0 || bfp->l_len != 0) { 14541 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14542 "cannot lock a memory mapped file unless locking the " 14543 "entire file: start %"PRIx64", len %"PRIx64, 14544 bfp->l_start, bfp->l_len)); 14545 return (0); 14546 } 14547 14548 /* mandatory locking and mapping don't mix */ 14549 va.va_mask = AT_MODE; 14550 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 14551 if (error != 0) { 14552 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14553 "getattr error %d", error)); 14554 return (0); /* treat errors conservatively */ 14555 } 14556 if (MANDLOCK(vp, va.va_mode)) { 14557 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14558 "cannot mandatory lock and mmap a file")); 14559 return (0); 14560 } 14561 14562 return (1); 14563 } 14564 14565 14566 /* 14567 * Register the lock locally within Solaris. 14568 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14569 * recording locks locally. 14570 * 14571 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14572 * are registered locally. 14573 */ 14574 void 14575 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14576 u_offset_t offset) 14577 { 14578 int oldsysid; 14579 int error; 14580 #ifdef DEBUG 14581 char *name; 14582 #endif 14583 14584 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14585 14586 #ifdef DEBUG 14587 name = fn_name(VTOSV(vp)->sv_name); 14588 NFS4_DEBUG(nfs4_client_lock_debug, 14589 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14590 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14591 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14592 flk->l_sysid)); 14593 kmem_free(name, MAXNAMELEN); 14594 #endif 14595 14596 /* register the lock with local locking */ 14597 oldsysid = flk->l_sysid; 14598 flk->l_sysid |= LM_SYSID_CLIENT; 14599 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14600 #ifdef DEBUG 14601 if (error != 0) { 14602 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14603 "nfs4_register_lock_locally: could not register with" 14604 " local locking")); 14605 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14606 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14607 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14608 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14609 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14610 flk->l_type, flk->l_start, flk->l_len)); 14611 (void) reclock(vp, flk, 0, flag, offset, NULL); 14612 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14613 "blocked by pid %d sysid 0x%x type %d " 14614 "off 0x%" PRIx64 " len 0x%" PRIx64, 14615 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14616 flk->l_len)); 14617 } 14618 #endif 14619 flk->l_sysid = oldsysid; 14620 } 14621 14622 /* 14623 * nfs4_lockrelease: 14624 * 14625 * Release any locks on the given vnode that are held by the current 14626 * process. Also removes the lock owner (if one exists) from the rnode's 14627 * list. 14628 */ 14629 static int 14630 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14631 { 14632 flock64_t ld; 14633 int ret, error; 14634 rnode4_t *rp; 14635 nfs4_lock_owner_t *lop; 14636 nfs4_recov_state_t recov_state; 14637 mntinfo4_t *mi; 14638 bool_t possible_orphan = FALSE; 14639 bool_t recovonly; 14640 14641 ASSERT((uintptr_t)vp > KERNELBASE); 14642 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14643 14644 rp = VTOR4(vp); 14645 mi = VTOMI4(vp); 14646 14647 /* 14648 * If we have not locked anything then we can 14649 * just return since we have no work to do. 14650 */ 14651 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14652 return (0); 14653 } 14654 14655 /* 14656 * We need to comprehend that another thread may 14657 * kick off recovery and the lock_owner we have stashed 14658 * in lop might be invalid so we should NOT cache it 14659 * locally! 14660 */ 14661 recov_state.rs_flags = 0; 14662 recov_state.rs_num_retry_despite_err = 0; 14663 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14664 &recovonly); 14665 if (error) { 14666 mutex_enter(&rp->r_statelock); 14667 rp->r_flags |= R4LODANGLERS; 14668 mutex_exit(&rp->r_statelock); 14669 return (error); 14670 } 14671 14672 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14673 14674 /* 14675 * Check if the lock owner might have a lock (request was sent but 14676 * no response was received). Also check if there are any remote 14677 * locks on the file. (In theory we shouldn't have to make this 14678 * second check if there's no lock owner, but for now we'll be 14679 * conservative and do it anyway.) If either condition is true, 14680 * send an unlock for the entire file to the server. 14681 * 14682 * Note that no explicit synchronization is needed here. At worst, 14683 * flk_has_remote_locks() will return a false positive, in which case 14684 * the unlock call wastes time but doesn't harm correctness. 14685 */ 14686 14687 if (lop) { 14688 mutex_enter(&lop->lo_lock); 14689 possible_orphan = lop->lo_pending_rqsts; 14690 mutex_exit(&lop->lo_lock); 14691 lock_owner_rele(lop); 14692 } 14693 14694 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14695 14696 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14697 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14698 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14699 (void *)lop)); 14700 14701 if (possible_orphan || flk_has_remote_locks(vp)) { 14702 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14703 ld.l_whence = 0; /* unlock from start of file */ 14704 ld.l_start = 0; 14705 ld.l_len = 0; /* do entire file */ 14706 14707 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, 14708 cr, NULL); 14709 14710 if (ret != 0) { 14711 /* 14712 * If VOP_FRLOCK fails, make sure we unregister 14713 * local locks before we continue. 14714 */ 14715 ld.l_pid = ttoproc(curthread)->p_pid; 14716 nfs4_register_lock_locally(vp, &ld, flag, offset); 14717 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14718 "nfs4_lockrelease: lock release error on vp" 14719 " %p: error %d.\n", (void *)vp, ret)); 14720 } 14721 } 14722 14723 recov_state.rs_flags = 0; 14724 recov_state.rs_num_retry_despite_err = 0; 14725 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14726 &recovonly); 14727 if (error) { 14728 mutex_enter(&rp->r_statelock); 14729 rp->r_flags |= R4LODANGLERS; 14730 mutex_exit(&rp->r_statelock); 14731 return (error); 14732 } 14733 14734 /* 14735 * So, here we're going to need to retrieve the lock-owner 14736 * again (in case recovery has done a switch-a-roo) and 14737 * remove it because we can. 14738 */ 14739 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14740 14741 if (lop) { 14742 nfs4_rnode_remove_lock_owner(rp, lop); 14743 lock_owner_rele(lop); 14744 } 14745 14746 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14747 return (0); 14748 } 14749 14750 /* 14751 * Wait for 'tick_delay' clock ticks. 14752 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14753 * 14754 * The client should retry to acquire the lock faster than the lease period. 14755 * We use roughly half of the lease time to use a similar calculation as it is 14756 * used in nfs4_renew_lease_thread(). 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) 14762 { 14763 /* wait tick_delay clock ticks or siginteruptus */ 14764 if (delay_sig(*tick_delay)) { 14765 return (EINTR); 14766 } 14767 14768 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14769 "reissue the lock request: blocked for %ld clock ticks: %ld " 14770 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14771 14772 *tick_delay = MIN(drv_usectohz(nfs4_max_base_wait_time * 1000), 14773 *tick_delay * 1.5); 14774 return (0); 14775 } 14776 14777 void 14778 nfs4_vnops_init(void) 14779 { 14780 } 14781 14782 void 14783 nfs4_vnops_fini(void) 14784 { 14785 } 14786 14787 /* 14788 * Return a reference to the directory (parent) vnode for a given vnode, 14789 * using the saved pathname information and the directory file handle. The 14790 * caller is responsible for disposing of the reference. 14791 * Returns zero or an errno value. 14792 * 14793 * Caller should set need_start_op to FALSE if it is the recovery 14794 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14795 */ 14796 int 14797 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14798 { 14799 svnode_t *svnp; 14800 vnode_t *dvp = NULL; 14801 servinfo4_t *svp; 14802 nfs4_fname_t *mfname; 14803 int error; 14804 14805 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14806 14807 if (vp->v_flag & VROOT) { 14808 nfs4_sharedfh_t *sfh; 14809 nfs_fh4 fh; 14810 mntinfo4_t *mi; 14811 14812 ASSERT(vp->v_type == VREG); 14813 14814 mi = VTOMI4(vp); 14815 svp = mi->mi_curr_serv; 14816 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14817 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14818 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14819 sfh = sfh4_get(&fh, VTOMI4(vp)); 14820 nfs_rw_exit(&svp->sv_lock); 14821 mfname = mi->mi_fname; 14822 fn_hold(mfname); 14823 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14824 sfh4_rele(&sfh); 14825 14826 if (dvp->v_type == VNON) 14827 dvp->v_type = VDIR; 14828 *dvpp = dvp; 14829 return (0); 14830 } 14831 14832 svnp = VTOSV(vp); 14833 14834 if (svnp == NULL) { 14835 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14836 "shadow node is NULL")); 14837 return (EINVAL); 14838 } 14839 14840 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14841 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14842 "shadow node name or dfh val == NULL")); 14843 return (EINVAL); 14844 } 14845 14846 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14847 (int)need_start_op); 14848 if (error != 0) { 14849 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14850 "nfs4_make_dotdot returned %d", error)); 14851 return (error); 14852 } 14853 if (!dvp) { 14854 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14855 "nfs4_make_dotdot returned a NULL dvp")); 14856 return (EIO); 14857 } 14858 if (dvp->v_type == VNON) 14859 dvp->v_type = VDIR; 14860 ASSERT(dvp->v_type == VDIR); 14861 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14862 mutex_enter(&dvp->v_lock); 14863 dvp->v_flag |= V_XATTRDIR; 14864 mutex_exit(&dvp->v_lock); 14865 } 14866 *dvpp = dvp; 14867 return (0); 14868 } 14869 14870 /* 14871 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14872 * length that fnamep can accept, including the trailing null. 14873 * Returns 0 if okay, returns an errno value if there was a problem. 14874 */ 14875 14876 int 14877 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14878 { 14879 char *fn; 14880 int err = 0; 14881 servinfo4_t *svp; 14882 svnode_t *shvp; 14883 14884 /* 14885 * If the file being opened has VROOT set, then this is 14886 * a "file" mount. sv_name will not be interesting, so 14887 * go back to the servinfo4 to get the original mount 14888 * path and strip off all but the final edge. Otherwise 14889 * just return the name from the shadow vnode. 14890 */ 14891 14892 if (vp->v_flag & VROOT) { 14893 14894 svp = VTOMI4(vp)->mi_curr_serv; 14895 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14896 14897 fn = strrchr(svp->sv_path, '/'); 14898 if (fn == NULL) 14899 err = EINVAL; 14900 else 14901 fn++; 14902 } else { 14903 shvp = VTOSV(vp); 14904 fn = fn_name(shvp->sv_name); 14905 } 14906 14907 if (err == 0) 14908 if (strlen(fn) < maxlen) 14909 (void) strcpy(fnamep, fn); 14910 else 14911 err = ENAMETOOLONG; 14912 14913 if (vp->v_flag & VROOT) 14914 nfs_rw_exit(&svp->sv_lock); 14915 else 14916 kmem_free(fn, MAXNAMELEN); 14917 14918 return (err); 14919 } 14920 14921 /* 14922 * Bookkeeping for a close that doesn't need to go over the wire. 14923 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14924 * it is left at 1. 14925 */ 14926 void 14927 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14928 { 14929 rnode4_t *rp; 14930 mntinfo4_t *mi; 14931 14932 mi = VTOMI4(vp); 14933 rp = VTOR4(vp); 14934 14935 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14936 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14937 ASSERT(nfs_zone() == mi->mi_zone); 14938 ASSERT(mutex_owned(&osp->os_sync_lock)); 14939 ASSERT(*have_lockp); 14940 14941 if (!osp->os_valid || 14942 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14943 return; 14944 } 14945 14946 /* 14947 * This removes the reference obtained at OPEN; ie, 14948 * when the open stream structure was created. 14949 * 14950 * We don't have to worry about calling 'open_stream_rele' 14951 * since we our currently holding a reference to this 14952 * open stream which means the count can not go to 0 with 14953 * this decrement. 14954 */ 14955 ASSERT(osp->os_ref_count >= 2); 14956 osp->os_ref_count--; 14957 osp->os_valid = 0; 14958 mutex_exit(&osp->os_sync_lock); 14959 *have_lockp = 0; 14960 14961 nfs4_dec_state_ref_count(mi); 14962 } 14963 14964 /* 14965 * Close all remaining open streams on the rnode. These open streams 14966 * could be here because: 14967 * - The close attempted at either close or delmap failed 14968 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14969 * - Someone did mknod on a regular file but never opened it 14970 */ 14971 int 14972 nfs4close_all(vnode_t *vp, cred_t *cr) 14973 { 14974 nfs4_open_stream_t *osp; 14975 int error; 14976 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14977 rnode4_t *rp; 14978 14979 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14980 14981 error = 0; 14982 rp = VTOR4(vp); 14983 14984 /* 14985 * At this point, all we know is that the last time 14986 * someone called vn_rele, the count was 1. Since then, 14987 * the vnode could have been re-activated. We want to 14988 * loop through the open streams and close each one, but 14989 * we have to be careful since once we release the rnode 14990 * hash bucket lock, someone else is free to come in and 14991 * re-activate the rnode and add new open streams. The 14992 * strategy is take the rnode hash bucket lock, verify that 14993 * the count is still 1, grab the open stream off the 14994 * head of the list and mark it invalid, then release the 14995 * rnode hash bucket lock and proceed with that open stream. 14996 * This is ok because nfs4close_one() will acquire the proper 14997 * open/create to close/destroy synchronization for open 14998 * streams, and will ensure that if someone has reopened 14999 * the open stream after we've dropped the hash bucket lock 15000 * then we'll just simply return without destroying the 15001 * open stream. 15002 * Repeat until the list is empty. 15003 */ 15004 15005 for (;;) { 15006 15007 /* make sure vnode hasn't been reactivated */ 15008 rw_enter(&rp->r_hashq->r_lock, RW_READER); 15009 mutex_enter(&vp->v_lock); 15010 if (vp->v_count > 1) { 15011 mutex_exit(&vp->v_lock); 15012 rw_exit(&rp->r_hashq->r_lock); 15013 break; 15014 } 15015 /* 15016 * Grabbing r_os_lock before releasing v_lock prevents 15017 * a window where the rnode/open stream could get 15018 * reactivated (and os_force_close set to 0) before we 15019 * had a chance to set os_force_close to 1. 15020 */ 15021 mutex_enter(&rp->r_os_lock); 15022 mutex_exit(&vp->v_lock); 15023 15024 osp = list_head(&rp->r_open_streams); 15025 if (!osp) { 15026 /* nothing left to CLOSE OTW, so return */ 15027 mutex_exit(&rp->r_os_lock); 15028 rw_exit(&rp->r_hashq->r_lock); 15029 break; 15030 } 15031 15032 mutex_enter(&rp->r_statev4_lock); 15033 /* the file can't still be mem mapped */ 15034 ASSERT(rp->r_mapcnt == 0); 15035 if (rp->created_v4) 15036 rp->created_v4 = 0; 15037 mutex_exit(&rp->r_statev4_lock); 15038 15039 /* 15040 * Grab a ref on this open stream; nfs4close_one 15041 * will mark it as invalid 15042 */ 15043 mutex_enter(&osp->os_sync_lock); 15044 osp->os_ref_count++; 15045 osp->os_force_close = 1; 15046 mutex_exit(&osp->os_sync_lock); 15047 mutex_exit(&rp->r_os_lock); 15048 rw_exit(&rp->r_hashq->r_lock); 15049 15050 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 15051 15052 /* Update error if it isn't already non-zero */ 15053 if (error == 0) { 15054 if (e.error) 15055 error = e.error; 15056 else if (e.stat) 15057 error = geterrno4(e.stat); 15058 } 15059 15060 #ifdef DEBUG 15061 nfs4close_all_cnt++; 15062 #endif 15063 /* Release the ref on osp acquired above. */ 15064 open_stream_rele(osp, rp); 15065 15066 /* Proceed to the next open stream, if any */ 15067 } 15068 return (error); 15069 } 15070 15071 /* 15072 * nfs4close_one - close one open stream for a file if needed. 15073 * 15074 * "close_type" indicates which close path this is: 15075 * CLOSE_NORM: close initiated via VOP_CLOSE. 15076 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 15077 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 15078 * the close and release of client state for this open stream 15079 * (unless someone else has the open stream open). 15080 * CLOSE_RESEND: indicates the request is a replay of an earlier request 15081 * (e.g., due to abort because of a signal). 15082 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 15083 * 15084 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 15085 * recovery. Instead, the caller is expected to deal with retries. 15086 * 15087 * The caller can either pass in the osp ('provided_osp') or not. 15088 * 15089 * 'access_bits' represents the access we are closing/downgrading. 15090 * 15091 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 15092 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 15093 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 15094 * 15095 * Errors are returned via the nfs4_error_t. 15096 */ 15097 void 15098 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 15099 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 15100 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 15101 uint_t mmap_flags) 15102 { 15103 nfs4_open_owner_t *oop; 15104 nfs4_open_stream_t *osp = NULL; 15105 int retry = 0; 15106 int num_retries = NFS4_NUM_RECOV_RETRIES; 15107 rnode4_t *rp; 15108 mntinfo4_t *mi; 15109 nfs4_recov_state_t recov_state; 15110 cred_t *cred_otw = NULL; 15111 bool_t recovonly = FALSE; 15112 int isrecov; 15113 int force_close; 15114 int close_failed = 0; 15115 int did_dec_count = 0; 15116 int did_start_op = 0; 15117 int did_force_recovlock = 0; 15118 int did_start_seqid_sync = 0; 15119 int have_sync_lock = 0; 15120 15121 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15122 15123 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 15124 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 15125 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 15126 len, maxprot, mmap_flags, access_bits)); 15127 15128 nfs4_error_zinit(ep); 15129 rp = VTOR4(vp); 15130 mi = VTOMI4(vp); 15131 isrecov = (close_type == CLOSE_RESEND || 15132 close_type == CLOSE_AFTER_RESEND); 15133 15134 /* 15135 * First get the open owner. 15136 */ 15137 if (!provided_osp) { 15138 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 15139 } else { 15140 oop = provided_osp->os_open_owner; 15141 ASSERT(oop != NULL); 15142 open_owner_hold(oop); 15143 } 15144 15145 if (!oop) { 15146 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15147 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 15148 "close type %d", (void *)rp, (void *)mi, (void *)cr, 15149 (void *)provided_osp, close_type)); 15150 ep->error = EIO; 15151 goto out; 15152 } 15153 15154 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 15155 recov_retry: 15156 osp = NULL; 15157 close_failed = 0; 15158 force_close = (close_type == CLOSE_FORCE); 15159 retry = 0; 15160 did_start_op = 0; 15161 did_force_recovlock = 0; 15162 did_start_seqid_sync = 0; 15163 have_sync_lock = 0; 15164 recovonly = FALSE; 15165 recov_state.rs_flags = 0; 15166 recov_state.rs_num_retry_despite_err = 0; 15167 15168 /* 15169 * Second synchronize with recovery. 15170 */ 15171 if (!isrecov) { 15172 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 15173 &recov_state, &recovonly); 15174 if (!ep->error) { 15175 did_start_op = 1; 15176 } else { 15177 close_failed = 1; 15178 /* 15179 * If we couldn't get start_fop, but have to 15180 * cleanup state, then at least acquire the 15181 * mi_recovlock so we can synchronize with 15182 * recovery. 15183 */ 15184 if (close_type == CLOSE_FORCE) { 15185 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 15186 RW_READER, FALSE); 15187 did_force_recovlock = 1; 15188 } else 15189 goto out; 15190 } 15191 } 15192 15193 /* 15194 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15195 * set 'recovonly' to TRUE since most likely this is due to 15196 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15197 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15198 * to retry, causing us to loop until recovery finishes. Plus we 15199 * don't need protection over the open seqid since we're not going 15200 * OTW, hence don't need to use the seqid. 15201 */ 15202 if (recovonly == FALSE) { 15203 /* need to grab the open owner sync before 'os_sync_lock' */ 15204 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15205 if (ep->error == EAGAIN) { 15206 ASSERT(!isrecov); 15207 if (did_start_op) 15208 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15209 &recov_state, TRUE); 15210 if (did_force_recovlock) 15211 nfs_rw_exit(&mi->mi_recovlock); 15212 goto recov_retry; 15213 } 15214 did_start_seqid_sync = 1; 15215 } 15216 15217 /* 15218 * Third get an open stream and acquire 'os_sync_lock' to 15219 * sychronize the opening/creating of an open stream with the 15220 * closing/destroying of an open stream. 15221 */ 15222 if (!provided_osp) { 15223 /* returns with 'os_sync_lock' held */ 15224 osp = find_open_stream(oop, rp); 15225 if (!osp) { 15226 ep->error = EIO; 15227 goto out; 15228 } 15229 } else { 15230 osp = provided_osp; 15231 open_stream_hold(osp); 15232 mutex_enter(&osp->os_sync_lock); 15233 } 15234 have_sync_lock = 1; 15235 15236 ASSERT(oop == osp->os_open_owner); 15237 15238 /* 15239 * Fourth, do any special pre-OTW CLOSE processing 15240 * based on the specific close type. 15241 */ 15242 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15243 !did_dec_count) { 15244 ASSERT(osp->os_open_ref_count > 0); 15245 osp->os_open_ref_count--; 15246 did_dec_count = 1; 15247 if (osp->os_open_ref_count == 0) 15248 osp->os_final_close = 1; 15249 } 15250 15251 if (close_type == CLOSE_FORCE) { 15252 /* see if somebody reopened the open stream. */ 15253 if (!osp->os_force_close) { 15254 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15255 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15256 "was reopened, vp %p", (void *)osp, (void *)vp)); 15257 ep->error = 0; 15258 ep->stat = NFS4_OK; 15259 goto out; 15260 } 15261 15262 if (!osp->os_final_close && !did_dec_count) { 15263 osp->os_open_ref_count--; 15264 did_dec_count = 1; 15265 } 15266 15267 /* 15268 * We can't depend on os_open_ref_count being 0 due to the 15269 * way executables are opened (VN_RELE to match a VOP_OPEN). 15270 */ 15271 #ifdef NOTYET 15272 ASSERT(osp->os_open_ref_count == 0); 15273 #endif 15274 if (osp->os_open_ref_count != 0) { 15275 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15276 "nfs4close_one: should panic here on an " 15277 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15278 "since this is probably the exec problem.")); 15279 15280 osp->os_open_ref_count = 0; 15281 } 15282 15283 /* 15284 * There is the possibility that nfs4close_one() 15285 * for close_type == CLOSE_DELMAP couldn't find the 15286 * open stream, thus couldn't decrement its os_mapcnt; 15287 * therefore we can't use this ASSERT yet. 15288 */ 15289 #ifdef NOTYET 15290 ASSERT(osp->os_mapcnt == 0); 15291 #endif 15292 osp->os_mapcnt = 0; 15293 } 15294 15295 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15296 ASSERT(osp->os_mapcnt >= btopr(len)); 15297 15298 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15299 osp->os_mmap_write -= btopr(len); 15300 if (maxprot & PROT_READ) 15301 osp->os_mmap_read -= btopr(len); 15302 if (maxprot & PROT_EXEC) 15303 osp->os_mmap_read -= btopr(len); 15304 /* mirror the PROT_NONE check in nfs4_addmap() */ 15305 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15306 !(maxprot & PROT_EXEC)) 15307 osp->os_mmap_read -= btopr(len); 15308 osp->os_mapcnt -= btopr(len); 15309 did_dec_count = 1; 15310 } 15311 15312 if (recovonly) { 15313 nfs4_lost_rqst_t lost_rqst; 15314 15315 /* request should not already be in recovery queue */ 15316 ASSERT(lrp == NULL); 15317 nfs4_error_init(ep, EINTR); 15318 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15319 osp, cred_otw, vp); 15320 mutex_exit(&osp->os_sync_lock); 15321 have_sync_lock = 0; 15322 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15323 lost_rqst.lr_op == OP_CLOSE ? 15324 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL); 15325 close_failed = 1; 15326 force_close = 0; 15327 goto close_cleanup; 15328 } 15329 15330 /* 15331 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15332 * we stopped operating on the open owner's <old oo_name, old seqid> 15333 * space, which means we stopped operating on the open stream 15334 * too. So don't go OTW (as the seqid is likely bad, and the 15335 * stateid could be stale, potentially triggering a false 15336 * setclientid), and just clean up the client's internal state. 15337 */ 15338 if (osp->os_orig_oo_name != oop->oo_name) { 15339 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15340 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15341 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15342 "oo_name %" PRIx64")", 15343 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15344 oop->oo_name)); 15345 close_failed = 1; 15346 } 15347 15348 /* If the file failed recovery, just quit. */ 15349 mutex_enter(&rp->r_statelock); 15350 if (rp->r_flags & R4RECOVERR) { 15351 close_failed = 1; 15352 } 15353 mutex_exit(&rp->r_statelock); 15354 15355 /* 15356 * If the force close path failed to obtain start_fop 15357 * then skip the OTW close and just remove the state. 15358 */ 15359 if (close_failed) 15360 goto close_cleanup; 15361 15362 /* 15363 * Fifth, check to see if there are still mapped pages or other 15364 * opens using this open stream. If there are then we can't 15365 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15366 */ 15367 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15368 nfs4_lost_rqst_t new_lost_rqst; 15369 bool_t needrecov = FALSE; 15370 cred_t *odg_cred_otw = NULL; 15371 seqid4 open_dg_seqid = 0; 15372 15373 if (osp->os_delegation) { 15374 /* 15375 * If this open stream was never OPENed OTW then we 15376 * surely can't DOWNGRADE it (especially since the 15377 * osp->open_stateid is really a delegation stateid 15378 * when os_delegation is 1). 15379 */ 15380 if (access_bits & FREAD) 15381 osp->os_share_acc_read--; 15382 if (access_bits & FWRITE) 15383 osp->os_share_acc_write--; 15384 osp->os_share_deny_none--; 15385 nfs4_error_zinit(ep); 15386 goto out; 15387 } 15388 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15389 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15390 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15391 if (needrecov && !isrecov) { 15392 bool_t abort; 15393 nfs4_bseqid_entry_t *bsep = NULL; 15394 15395 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15396 bsep = nfs4_create_bseqid_entry(oop, NULL, 15397 vp, 0, 15398 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15399 open_dg_seqid); 15400 15401 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15402 oop, osp, odg_cred_otw, vp, access_bits, 0); 15403 mutex_exit(&osp->os_sync_lock); 15404 have_sync_lock = 0; 15405 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15406 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15407 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15408 bsep, NULL, NULL); 15409 if (odg_cred_otw) 15410 crfree(odg_cred_otw); 15411 if (bsep) 15412 kmem_free(bsep, sizeof (*bsep)); 15413 15414 if (abort == TRUE) 15415 goto out; 15416 15417 if (did_start_seqid_sync) { 15418 nfs4_end_open_seqid_sync(oop); 15419 did_start_seqid_sync = 0; 15420 } 15421 open_stream_rele(osp, rp); 15422 15423 if (did_start_op) 15424 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15425 &recov_state, FALSE); 15426 if (did_force_recovlock) 15427 nfs_rw_exit(&mi->mi_recovlock); 15428 15429 goto recov_retry; 15430 } else { 15431 if (odg_cred_otw) 15432 crfree(odg_cred_otw); 15433 } 15434 goto out; 15435 } 15436 15437 /* 15438 * If this open stream was created as the results of an open 15439 * while holding a delegation, then just release it; no need 15440 * to do an OTW close. Otherwise do a "normal" OTW close. 15441 */ 15442 if (osp->os_delegation) { 15443 nfs4close_notw(vp, osp, &have_sync_lock); 15444 nfs4_error_zinit(ep); 15445 goto out; 15446 } 15447 15448 /* 15449 * If this stream is not valid, we're done. 15450 */ 15451 if (!osp->os_valid) { 15452 nfs4_error_zinit(ep); 15453 goto out; 15454 } 15455 15456 /* 15457 * Last open or mmap ref has vanished, need to do an OTW close. 15458 * First check to see if a close is still necessary. 15459 */ 15460 if (osp->os_failed_reopen) { 15461 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15462 "don't close OTW osp %p since reopen failed.", 15463 (void *)osp)); 15464 /* 15465 * Reopen of the open stream failed, hence the 15466 * stateid of the open stream is invalid/stale, and 15467 * sending this OTW would incorrectly cause another 15468 * round of recovery. In this case, we need to set 15469 * the 'os_valid' bit to 0 so another thread doesn't 15470 * come in and re-open this open stream before 15471 * this "closing" thread cleans up state (decrementing 15472 * the nfs4_server_t's state_ref_count and decrementing 15473 * the os_ref_count). 15474 */ 15475 osp->os_valid = 0; 15476 /* 15477 * This removes the reference obtained at OPEN; ie, 15478 * when the open stream structure was created. 15479 * 15480 * We don't have to worry about calling 'open_stream_rele' 15481 * since we our currently holding a reference to this 15482 * open stream which means the count can not go to 0 with 15483 * this decrement. 15484 */ 15485 ASSERT(osp->os_ref_count >= 2); 15486 osp->os_ref_count--; 15487 nfs4_error_zinit(ep); 15488 close_failed = 0; 15489 goto close_cleanup; 15490 } 15491 15492 ASSERT(osp->os_ref_count > 1); 15493 15494 /* 15495 * Sixth, try the CLOSE OTW. 15496 */ 15497 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15498 close_type, ep, &have_sync_lock); 15499 15500 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15501 /* 15502 * Let the recovery thread be responsible for 15503 * removing the state for CLOSE. 15504 */ 15505 close_failed = 1; 15506 force_close = 0; 15507 retry = 0; 15508 } 15509 15510 /* See if we need to retry with a different cred */ 15511 if ((ep->error == EACCES || 15512 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15513 cred_otw != cr) { 15514 crfree(cred_otw); 15515 cred_otw = cr; 15516 crhold(cred_otw); 15517 retry = 1; 15518 } 15519 15520 if (ep->error || ep->stat) 15521 close_failed = 1; 15522 15523 if (retry && !isrecov && num_retries-- > 0) { 15524 if (have_sync_lock) { 15525 mutex_exit(&osp->os_sync_lock); 15526 have_sync_lock = 0; 15527 } 15528 if (did_start_seqid_sync) { 15529 nfs4_end_open_seqid_sync(oop); 15530 did_start_seqid_sync = 0; 15531 } 15532 open_stream_rele(osp, rp); 15533 15534 if (did_start_op) 15535 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15536 &recov_state, FALSE); 15537 if (did_force_recovlock) 15538 nfs_rw_exit(&mi->mi_recovlock); 15539 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15540 "nfs4close_one: need to retry the close " 15541 "operation")); 15542 goto recov_retry; 15543 } 15544 close_cleanup: 15545 /* 15546 * Seventh and lastly, process our results. 15547 */ 15548 if (close_failed && force_close) { 15549 /* 15550 * It's ok to drop and regrab the 'os_sync_lock' since 15551 * nfs4close_notw() will recheck to make sure the 15552 * "close"/removal of state should happen. 15553 */ 15554 if (!have_sync_lock) { 15555 mutex_enter(&osp->os_sync_lock); 15556 have_sync_lock = 1; 15557 } 15558 /* 15559 * This is last call, remove the ref on the open 15560 * stream created by open and clean everything up. 15561 */ 15562 osp->os_pending_close = 0; 15563 nfs4close_notw(vp, osp, &have_sync_lock); 15564 nfs4_error_zinit(ep); 15565 } 15566 15567 if (!close_failed) { 15568 if (have_sync_lock) { 15569 osp->os_pending_close = 0; 15570 mutex_exit(&osp->os_sync_lock); 15571 have_sync_lock = 0; 15572 } else { 15573 mutex_enter(&osp->os_sync_lock); 15574 osp->os_pending_close = 0; 15575 mutex_exit(&osp->os_sync_lock); 15576 } 15577 if (did_start_op && recov_state.rs_sp != NULL) { 15578 mutex_enter(&recov_state.rs_sp->s_lock); 15579 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15580 mutex_exit(&recov_state.rs_sp->s_lock); 15581 } else { 15582 nfs4_dec_state_ref_count(mi); 15583 } 15584 nfs4_error_zinit(ep); 15585 } 15586 15587 out: 15588 if (have_sync_lock) 15589 mutex_exit(&osp->os_sync_lock); 15590 if (did_start_op) 15591 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15592 recovonly ? TRUE : FALSE); 15593 if (did_force_recovlock) 15594 nfs_rw_exit(&mi->mi_recovlock); 15595 if (cred_otw) 15596 crfree(cred_otw); 15597 if (osp) 15598 open_stream_rele(osp, rp); 15599 if (oop) { 15600 if (did_start_seqid_sync) 15601 nfs4_end_open_seqid_sync(oop); 15602 open_owner_rele(oop); 15603 } 15604 } 15605 15606 /* 15607 * Convert information returned by the server in the LOCK4denied 15608 * structure to the form required by fcntl. 15609 */ 15610 static void 15611 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15612 { 15613 nfs4_lo_name_t *lo; 15614 15615 #ifdef DEBUG 15616 if (denied_to_flk_debug) { 15617 lockt_denied_debug = lockt_denied; 15618 debug_enter("lockt_denied"); 15619 } 15620 #endif 15621 15622 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15623 flk->l_whence = 0; /* aka SEEK_SET */ 15624 flk->l_start = lockt_denied->offset; 15625 flk->l_len = lockt_denied->length; 15626 15627 /* 15628 * If the blocking clientid matches our client id, then we can 15629 * interpret the lockowner (since we built it). If not, then 15630 * fabricate a sysid and pid. Note that the l_sysid field 15631 * in *flk already has the local sysid. 15632 */ 15633 15634 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15635 15636 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15637 lo = (nfs4_lo_name_t *) 15638 lockt_denied->owner.owner_val; 15639 15640 flk->l_pid = lo->ln_pid; 15641 } else { 15642 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15643 "denied_to_flk: bad lock owner length\n")); 15644 15645 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15646 } 15647 } else { 15648 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15649 "denied_to_flk: foreign clientid\n")); 15650 15651 /* 15652 * Construct a new sysid which should be different from 15653 * sysids of other systems. 15654 */ 15655 15656 flk->l_sysid++; 15657 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15658 } 15659 } 15660 15661 static pid_t 15662 lo_to_pid(lock_owner4 *lop) 15663 { 15664 pid_t pid = 0; 15665 uchar_t *cp; 15666 int i; 15667 15668 cp = (uchar_t *)&lop->clientid; 15669 15670 for (i = 0; i < sizeof (lop->clientid); i++) 15671 pid += (pid_t)*cp++; 15672 15673 cp = (uchar_t *)lop->owner_val; 15674 15675 for (i = 0; i < lop->owner_len; i++) 15676 pid += (pid_t)*cp++; 15677 15678 return (pid); 15679 } 15680 15681 /* 15682 * Given a lock pointer, returns the length of that lock. 15683 * "end" is the last locked offset the "l_len" covers from 15684 * the start of the lock. 15685 */ 15686 static off64_t 15687 lock_to_end(flock64_t *lock) 15688 { 15689 off64_t lock_end; 15690 15691 if (lock->l_len == 0) 15692 lock_end = (off64_t)MAXEND; 15693 else 15694 lock_end = lock->l_start + lock->l_len - 1; 15695 15696 return (lock_end); 15697 } 15698 15699 /* 15700 * Given the end of a lock, it will return you the length "l_len" for that lock. 15701 */ 15702 static off64_t 15703 end_to_len(off64_t start, off64_t end) 15704 { 15705 off64_t lock_len; 15706 15707 ASSERT(end >= start); 15708 if (end == MAXEND) 15709 lock_len = 0; 15710 else 15711 lock_len = end - start + 1; 15712 15713 return (lock_len); 15714 } 15715 15716 /* 15717 * On given end for a lock it determines if it is the last locked offset 15718 * or not, if so keeps it as is, else adds one to return the length for 15719 * valid start. 15720 */ 15721 static off64_t 15722 start_check(off64_t x) 15723 { 15724 if (x == MAXEND) 15725 return (x); 15726 else 15727 return (x + 1); 15728 } 15729 15730 /* 15731 * See if these two locks overlap, and if so return 1; 15732 * otherwise, return 0. 15733 */ 15734 static int 15735 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15736 { 15737 off64_t llfp_end, curfp_end; 15738 15739 llfp_end = lock_to_end(llfp); 15740 curfp_end = lock_to_end(curfp); 15741 15742 if (((llfp_end >= curfp->l_start) && 15743 (llfp->l_start <= curfp->l_start)) || 15744 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15745 return (1); 15746 return (0); 15747 } 15748 15749 /* 15750 * Determine what the intersecting lock region is, and add that to the 15751 * 'nl_llpp' locklist in increasing order (by l_start). 15752 */ 15753 static void 15754 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15755 locklist_t **nl_llpp, vnode_t *vp) 15756 { 15757 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15758 off64_t lost_flp_end, local_flp_end, len, start; 15759 15760 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15761 15762 if (!locks_intersect(lost_flp, local_flp)) 15763 return; 15764 15765 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15766 "locks intersect")); 15767 15768 lost_flp_end = lock_to_end(lost_flp); 15769 local_flp_end = lock_to_end(local_flp); 15770 15771 /* Find the starting point of the intersecting region */ 15772 if (local_flp->l_start > lost_flp->l_start) 15773 start = local_flp->l_start; 15774 else 15775 start = lost_flp->l_start; 15776 15777 /* Find the lenght of the intersecting region */ 15778 if (lost_flp_end < local_flp_end) 15779 len = end_to_len(start, lost_flp_end); 15780 else 15781 len = end_to_len(start, local_flp_end); 15782 15783 /* 15784 * Prepare the flock structure for the intersection found and insert 15785 * it into the new list in increasing l_start order. This list contains 15786 * intersections of locks registered by the client with the local host 15787 * and the lost lock. 15788 * The lock type of this lock is the same as that of the local_flp. 15789 */ 15790 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15791 intersect_llp->ll_flock.l_start = start; 15792 intersect_llp->ll_flock.l_len = len; 15793 intersect_llp->ll_flock.l_type = local_flp->l_type; 15794 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15795 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15796 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15797 intersect_llp->ll_vp = vp; 15798 15799 tmp_fllp = *nl_llpp; 15800 cur_fllp = NULL; 15801 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15802 intersect_llp->ll_flock.l_start) { 15803 cur_fllp = tmp_fllp; 15804 tmp_fllp = tmp_fllp->ll_next; 15805 } 15806 if (cur_fllp == NULL) { 15807 /* first on the list */ 15808 intersect_llp->ll_next = *nl_llpp; 15809 *nl_llpp = intersect_llp; 15810 } else { 15811 intersect_llp->ll_next = cur_fllp->ll_next; 15812 cur_fllp->ll_next = intersect_llp; 15813 } 15814 15815 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15816 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15817 intersect_llp->ll_flock.l_start, 15818 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15819 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15820 } 15821 15822 /* 15823 * Our local locking current state is potentially different than 15824 * what the NFSv4 server thinks we have due to a lost lock that was 15825 * resent and then received. We need to reset our "NFSv4" locking 15826 * state to match the current local locking state for this pid since 15827 * that is what the user/application sees as what the world is. 15828 * 15829 * We cannot afford to drop the open/lock seqid sync since then we can 15830 * get confused about what the current local locking state "is" versus 15831 * "was". 15832 * 15833 * If we are unable to fix up the locks, we send SIGLOST to the affected 15834 * process. This is not done if the filesystem has been forcibly 15835 * unmounted, in case the process has already exited and a new process 15836 * exists with the same pid. 15837 */ 15838 static void 15839 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15840 nfs4_lock_owner_t *lop) 15841 { 15842 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15843 mntinfo4_t *mi = VTOMI4(vp); 15844 const int cmd = F_SETLK; 15845 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15846 flock64_t ul_fl; 15847 15848 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15849 "nfs4_reinstitute_local_lock_state")); 15850 15851 /* 15852 * Find active locks for this vp from the local locking code. 15853 * Scan through this list and find out the locks that intersect with 15854 * the lost lock. Once we find the lock that intersects, add the 15855 * intersection area as a new lock to a new list "ri_llp". The lock 15856 * type of the intersection region lock added to ri_llp is the same 15857 * as that found in the active lock list, "list". The intersecting 15858 * region locks are added to ri_llp in increasing l_start order. 15859 */ 15860 ASSERT(nfs_zone() == mi->mi_zone); 15861 15862 locks = flk_active_locks_for_vp(vp); 15863 ri_llp = NULL; 15864 15865 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15866 ASSERT(llp->ll_vp == vp); 15867 /* 15868 * Pick locks that belong to this pid/lockowner 15869 */ 15870 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15871 continue; 15872 15873 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15874 } 15875 15876 /* 15877 * Now we have the list of intersections with the lost lock. These are 15878 * the locks that were/are active before the server replied to the 15879 * last/lost lock. Issue these locks to the server here. Playing these 15880 * locks to the server will re-establish aur current local locking state 15881 * with the v4 server. 15882 * If we get an error, send SIGLOST to the application for that lock. 15883 */ 15884 15885 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15886 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15887 "nfs4_reinstitute_local_lock_state: need to issue " 15888 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15889 llp->ll_flock.l_start, 15890 llp->ll_flock.l_start + llp->ll_flock.l_len, 15891 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15892 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15893 /* 15894 * No need to relock what we already have 15895 */ 15896 if (llp->ll_flock.l_type == lost_flp->l_type) 15897 continue; 15898 15899 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15900 } 15901 15902 /* 15903 * Now keeping the start of the lost lock as our reference parse the 15904 * newly created ri_llp locklist to find the ranges that we have locked 15905 * with the v4 server but not in the current local locking. We need 15906 * to unlock these ranges. 15907 * These ranges can also be reffered to as those ranges, where the lost 15908 * lock does not overlap with the locks in the ri_llp but are locked 15909 * since the server replied to the lost lock. 15910 */ 15911 cur_start = lost_flp->l_start; 15912 lost_flp_end = lock_to_end(lost_flp); 15913 15914 ul_fl.l_type = F_UNLCK; 15915 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15916 ul_fl.l_sysid = lost_flp->l_sysid; 15917 ul_fl.l_pid = lost_flp->l_pid; 15918 15919 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15920 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15921 15922 if (llp->ll_flock.l_start <= cur_start) { 15923 cur_start = start_check(llp_ll_flock_end); 15924 continue; 15925 } 15926 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15927 "nfs4_reinstitute_local_lock_state: " 15928 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15929 cur_start, llp->ll_flock.l_start)); 15930 15931 ul_fl.l_start = cur_start; 15932 ul_fl.l_len = end_to_len(cur_start, 15933 (llp->ll_flock.l_start - 1)); 15934 15935 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15936 cur_start = start_check(llp_ll_flock_end); 15937 } 15938 15939 /* 15940 * In the case where the lost lock ends after all intersecting locks, 15941 * unlock the last part of the lost lock range. 15942 */ 15943 if (cur_start != start_check(lost_flp_end)) { 15944 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15945 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15946 "lost lock region [%"PRIx64" - %"PRIx64"]", 15947 cur_start, lost_flp->l_start + lost_flp->l_len)); 15948 15949 ul_fl.l_start = cur_start; 15950 /* 15951 * Is it an to-EOF lock? if so unlock till the end 15952 */ 15953 if (lost_flp->l_len == 0) 15954 ul_fl.l_len = 0; 15955 else 15956 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15957 15958 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15959 } 15960 15961 if (locks != NULL) 15962 flk_free_locklist(locks); 15963 15964 /* Free up our newly created locklist */ 15965 for (llp = ri_llp; llp != NULL; ) { 15966 tmp_llp = llp->ll_next; 15967 kmem_free(llp, sizeof (locklist_t)); 15968 llp = tmp_llp; 15969 } 15970 15971 /* 15972 * Now return back to the original calling nfs4frlock() 15973 * and let us naturally drop our seqid syncs. 15974 */ 15975 } 15976 15977 /* 15978 * Create a lost state record for the given lock reinstantiation request 15979 * and push it onto the lost state queue. 15980 */ 15981 static void 15982 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15983 nfs4_lock_owner_t *lop) 15984 { 15985 nfs4_lost_rqst_t req; 15986 nfs_lock_type4 locktype; 15987 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15988 15989 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15990 15991 locktype = flk_to_locktype(cmd, flk->l_type); 15992 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 15993 NULL, NULL, lop, flk, &req, cr, vp); 15994 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 15995 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 15996 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 15997 NULL, NULL, NULL); 15998 } 15999