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 lm_rel_sysid(lmsid); 2316 } 2317 return (e.error); 2318 } 2319 2320 if (count > 1) 2321 return (0); 2322 2323 /* 2324 * If the file has been `unlinked', then purge the 2325 * DNLC so that this vnode will get reycled quicker 2326 * and the .nfs* file on the server will get removed. 2327 */ 2328 if (rp->r_unldvp != NULL) 2329 dnlc_purge_vp(vp); 2330 2331 /* 2332 * If the file was open for write and there are pages, 2333 * do a synchronous flush and commit of all of the 2334 * dirty and uncommitted pages. 2335 */ 2336 ASSERT(!e.error); 2337 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2338 error = nfs4_putpage_commit(vp, 0, 0, cr); 2339 2340 mutex_enter(&rp->r_statelock); 2341 r_error = rp->r_error; 2342 rp->r_error = 0; 2343 mutex_exit(&rp->r_statelock); 2344 2345 /* 2346 * If this file type is one for which no explicit 'open' was 2347 * done, then bail now (ie. no need for protocol 'close'). If 2348 * there was an error w/the vm subsystem, return _that_ error, 2349 * otherwise, return any errors that may've been reported via 2350 * the rnode. 2351 */ 2352 if (vp->v_type != VREG) 2353 return (error ? error : r_error); 2354 2355 /* 2356 * The sync putpage commit may have failed above, but since 2357 * we're working w/a regular file, we need to do the protocol 2358 * 'close' (nfs4close_one will figure out if an otw close is 2359 * needed or not). Report any errors _after_ doing the protocol 2360 * 'close'. 2361 */ 2362 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2363 n4error = e.error ? e.error : geterrno4(e.stat); 2364 2365 /* 2366 * Error reporting prio (Hi -> Lo) 2367 * 2368 * i) nfs4_putpage_commit (error) 2369 * ii) rnode's (r_error) 2370 * iii) nfs4close_one (n4error) 2371 */ 2372 return (error ? error : (r_error ? r_error : n4error)); 2373 } 2374 2375 /* 2376 * Initialize *lost_rqstp. 2377 */ 2378 2379 static void 2380 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2381 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2382 vnode_t *vp) 2383 { 2384 if (error != ETIMEDOUT && error != EINTR && 2385 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2386 lost_rqstp->lr_op = 0; 2387 return; 2388 } 2389 2390 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2391 "nfs4close_save_lost_rqst: error %d", error)); 2392 2393 lost_rqstp->lr_op = OP_CLOSE; 2394 /* 2395 * The vp is held and rele'd via the recovery code. 2396 * See nfs4_save_lost_rqst. 2397 */ 2398 lost_rqstp->lr_vp = vp; 2399 lost_rqstp->lr_dvp = NULL; 2400 lost_rqstp->lr_oop = oop; 2401 lost_rqstp->lr_osp = osp; 2402 ASSERT(osp != NULL); 2403 ASSERT(mutex_owned(&osp->os_sync_lock)); 2404 osp->os_pending_close = 1; 2405 lost_rqstp->lr_lop = NULL; 2406 lost_rqstp->lr_cr = cr; 2407 lost_rqstp->lr_flk = NULL; 2408 lost_rqstp->lr_putfirst = FALSE; 2409 } 2410 2411 /* 2412 * Assumes you already have the open seqid sync grabbed as well as the 2413 * 'os_sync_lock'. Note: this will release the open seqid sync and 2414 * 'os_sync_lock' if client recovery starts. Calling functions have to 2415 * be prepared to handle this. 2416 * 2417 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2418 * was needed and was started, and that the calling function should retry 2419 * this function; otherwise it is returned as 0. 2420 * 2421 * Errors are returned via the nfs4_error_t parameter. 2422 */ 2423 static void 2424 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2425 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2426 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2427 { 2428 COMPOUND4args_clnt args; 2429 COMPOUND4res_clnt res; 2430 CLOSE4args *close_args; 2431 nfs_resop4 *resop; 2432 nfs_argop4 argop[3]; 2433 int doqueue = 1; 2434 mntinfo4_t *mi; 2435 seqid4 seqid; 2436 vnode_t *vp; 2437 bool_t needrecov = FALSE; 2438 nfs4_lost_rqst_t lost_rqst; 2439 hrtime_t t; 2440 2441 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2442 2443 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2444 2445 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2446 2447 /* Only set this to 1 if recovery is started */ 2448 *recov = 0; 2449 2450 /* do the OTW call to close the file */ 2451 2452 if (close_type == CLOSE_RESEND) 2453 args.ctag = TAG_CLOSE_LOST; 2454 else if (close_type == CLOSE_AFTER_RESEND) 2455 args.ctag = TAG_CLOSE_UNDO; 2456 else 2457 args.ctag = TAG_CLOSE; 2458 2459 args.array_len = 3; 2460 args.array = argop; 2461 2462 vp = RTOV4(rp); 2463 2464 mi = VTOMI4(vp); 2465 2466 /* putfh target fh */ 2467 argop[0].argop = OP_CPUTFH; 2468 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2469 2470 argop[1].argop = OP_GETATTR; 2471 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2472 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2473 2474 argop[2].argop = OP_CLOSE; 2475 close_args = &argop[2].nfs_argop4_u.opclose; 2476 2477 seqid = nfs4_get_open_seqid(oop) + 1; 2478 2479 close_args->seqid = seqid; 2480 close_args->open_stateid = osp->open_stateid; 2481 2482 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2483 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2484 rnode4info(rp))); 2485 2486 t = gethrtime(); 2487 2488 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2489 2490 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2491 nfs4_set_open_seqid(seqid, oop, args.ctag); 2492 } 2493 2494 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2495 if (ep->error && !needrecov) { 2496 /* 2497 * if there was an error and no recovery is to be done 2498 * then then set up the file to flush its cache if 2499 * needed for the next caller. 2500 */ 2501 mutex_enter(&rp->r_statelock); 2502 PURGE_ATTRCACHE4_LOCKED(rp); 2503 rp->r_flags &= ~R4WRITEMODIFIED; 2504 mutex_exit(&rp->r_statelock); 2505 return; 2506 } 2507 2508 if (needrecov) { 2509 bool_t abort; 2510 nfs4_bseqid_entry_t *bsep = NULL; 2511 2512 if (close_type != CLOSE_RESEND) 2513 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2514 osp, cred_otw, vp); 2515 2516 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2517 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2518 0, args.ctag, close_args->seqid); 2519 2520 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2521 "nfs4close_otw: initiating recovery. error %d " 2522 "res.status %d", ep->error, res.status)); 2523 2524 /* 2525 * Drop the 'os_sync_lock' here so we don't hit 2526 * a potential recursive mutex_enter via an 2527 * 'open_stream_hold()'. 2528 */ 2529 mutex_exit(&osp->os_sync_lock); 2530 *have_sync_lockp = 0; 2531 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2532 (close_type != CLOSE_RESEND && 2533 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2534 OP_CLOSE, bsep, NULL, NULL); 2535 2536 /* drop open seq sync, and let the calling function regrab it */ 2537 nfs4_end_open_seqid_sync(oop); 2538 *did_start_seqid_syncp = 0; 2539 2540 if (bsep) 2541 kmem_free(bsep, sizeof (*bsep)); 2542 /* 2543 * For signals, the caller wants to quit, so don't say to 2544 * retry. For forced unmount, if it's a user thread, it 2545 * wants to quit. If it's a recovery thread, the retry 2546 * will happen higher-up on the call stack. Either way, 2547 * don't say to retry. 2548 */ 2549 if (abort == FALSE && ep->error != EINTR && 2550 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2551 close_type != CLOSE_RESEND && 2552 close_type != CLOSE_AFTER_RESEND) 2553 *recov = 1; 2554 else 2555 *recov = 0; 2556 2557 if (!ep->error) 2558 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2559 return; 2560 } 2561 2562 if (res.status) { 2563 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2564 return; 2565 } 2566 2567 mutex_enter(&rp->r_statev4_lock); 2568 rp->created_v4 = 0; 2569 mutex_exit(&rp->r_statev4_lock); 2570 2571 resop = &res.array[2]; 2572 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2573 osp->os_valid = 0; 2574 2575 /* 2576 * This removes the reference obtained at OPEN; ie, when the 2577 * open stream structure was created. 2578 * 2579 * We don't have to worry about calling 'open_stream_rele' 2580 * since we our currently holding a reference to the open 2581 * stream which means the count cannot go to 0 with this 2582 * decrement. 2583 */ 2584 ASSERT(osp->os_ref_count >= 2); 2585 osp->os_ref_count--; 2586 2587 if (ep->error == 0) { 2588 /* 2589 * Avoid a deadlock with the r_serial thread waiting for 2590 * os_sync_lock in nfs4_get_otw_cred_by_osp() which might be 2591 * held by us. We will wait in nfs4_attr_cache() for the 2592 * completion of the r_serial thread. 2593 */ 2594 mutex_exit(&osp->os_sync_lock); 2595 *have_sync_lockp = 0; 2596 2597 nfs4_attr_cache(vp, 2598 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2599 t, cred_otw, TRUE, NULL); 2600 } 2601 2602 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2603 " returning %d", ep->error)); 2604 2605 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2606 } 2607 2608 /* ARGSUSED */ 2609 static int 2610 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2611 caller_context_t *ct) 2612 { 2613 rnode4_t *rp; 2614 u_offset_t off; 2615 offset_t diff; 2616 uint_t on; 2617 uint_t n; 2618 caddr_t base; 2619 uint_t flags; 2620 int error; 2621 mntinfo4_t *mi; 2622 2623 rp = VTOR4(vp); 2624 2625 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2626 2627 if (IS_SHADOW(vp, rp)) 2628 vp = RTOV4(rp); 2629 2630 if (vp->v_type != VREG) 2631 return (EISDIR); 2632 2633 mi = VTOMI4(vp); 2634 2635 if (nfs_zone() != mi->mi_zone) 2636 return (EIO); 2637 2638 if (uiop->uio_resid == 0) 2639 return (0); 2640 2641 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2642 return (EINVAL); 2643 2644 mutex_enter(&rp->r_statelock); 2645 if (rp->r_flags & R4RECOVERRP) 2646 error = (rp->r_error ? rp->r_error : EIO); 2647 else 2648 error = 0; 2649 mutex_exit(&rp->r_statelock); 2650 if (error) 2651 return (error); 2652 2653 /* 2654 * Bypass VM if caching has been disabled (e.g., locking) or if 2655 * using client-side direct I/O and the file is not mmap'd and 2656 * there are no cached pages. 2657 */ 2658 if ((vp->v_flag & VNOCACHE) || 2659 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2660 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2661 size_t resid = 0; 2662 2663 return (nfs4read(vp, NULL, uiop->uio_loffset, 2664 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2665 } 2666 2667 error = 0; 2668 2669 do { 2670 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2671 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2672 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2673 2674 if (error = nfs4_validate_caches(vp, cr)) 2675 break; 2676 2677 mutex_enter(&rp->r_statelock); 2678 while (rp->r_flags & R4INCACHEPURGE) { 2679 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2680 mutex_exit(&rp->r_statelock); 2681 return (EINTR); 2682 } 2683 } 2684 diff = rp->r_size - uiop->uio_loffset; 2685 mutex_exit(&rp->r_statelock); 2686 if (diff <= 0) 2687 break; 2688 if (diff < n) 2689 n = (uint_t)diff; 2690 2691 if (vpm_enable) { 2692 /* 2693 * Copy data. 2694 */ 2695 error = vpm_data_copy(vp, off + on, n, uiop, 2696 1, NULL, 0, S_READ); 2697 } else { 2698 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2699 S_READ); 2700 2701 error = uiomove(base + on, n, UIO_READ, uiop); 2702 } 2703 2704 if (!error) { 2705 /* 2706 * If read a whole block or read to eof, 2707 * won't need this buffer again soon. 2708 */ 2709 mutex_enter(&rp->r_statelock); 2710 if (n + on == MAXBSIZE || 2711 uiop->uio_loffset == rp->r_size) 2712 flags = SM_DONTNEED; 2713 else 2714 flags = 0; 2715 mutex_exit(&rp->r_statelock); 2716 if (vpm_enable) { 2717 error = vpm_sync_pages(vp, off, n, flags); 2718 } else { 2719 error = segmap_release(segkmap, base, flags); 2720 } 2721 } else { 2722 if (vpm_enable) { 2723 (void) vpm_sync_pages(vp, off, n, 0); 2724 } else { 2725 (void) segmap_release(segkmap, base, 0); 2726 } 2727 } 2728 } while (!error && uiop->uio_resid > 0); 2729 2730 return (error); 2731 } 2732 2733 /* ARGSUSED */ 2734 static int 2735 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2736 caller_context_t *ct) 2737 { 2738 rlim64_t limit = uiop->uio_llimit; 2739 rnode4_t *rp; 2740 u_offset_t off; 2741 caddr_t base; 2742 uint_t flags; 2743 int remainder; 2744 size_t n; 2745 int on; 2746 int error; 2747 int resid; 2748 u_offset_t offset; 2749 mntinfo4_t *mi; 2750 uint_t bsize; 2751 2752 rp = VTOR4(vp); 2753 2754 if (IS_SHADOW(vp, rp)) 2755 vp = RTOV4(rp); 2756 2757 if (vp->v_type != VREG) 2758 return (EISDIR); 2759 2760 mi = VTOMI4(vp); 2761 2762 if (nfs_zone() != mi->mi_zone) 2763 return (EIO); 2764 2765 if (uiop->uio_resid == 0) 2766 return (0); 2767 2768 mutex_enter(&rp->r_statelock); 2769 if (rp->r_flags & R4RECOVERRP) 2770 error = (rp->r_error ? rp->r_error : EIO); 2771 else 2772 error = 0; 2773 mutex_exit(&rp->r_statelock); 2774 if (error) 2775 return (error); 2776 2777 if (ioflag & FAPPEND) { 2778 struct vattr va; 2779 2780 /* 2781 * Must serialize if appending. 2782 */ 2783 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2784 nfs_rw_exit(&rp->r_rwlock); 2785 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2786 INTR4(vp))) 2787 return (EINTR); 2788 } 2789 2790 va.va_mask = AT_SIZE; 2791 error = nfs4getattr(vp, &va, cr); 2792 if (error) 2793 return (error); 2794 uiop->uio_loffset = va.va_size; 2795 } 2796 2797 offset = uiop->uio_loffset + uiop->uio_resid; 2798 2799 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2800 return (EINVAL); 2801 2802 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2803 limit = MAXOFFSET_T; 2804 2805 /* 2806 * Check to make sure that the process will not exceed 2807 * its limit on file size. It is okay to write up to 2808 * the limit, but not beyond. Thus, the write which 2809 * reaches the limit will be short and the next write 2810 * will return an error. 2811 */ 2812 remainder = 0; 2813 if (offset > uiop->uio_llimit) { 2814 remainder = offset - uiop->uio_llimit; 2815 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2816 if (uiop->uio_resid <= 0) { 2817 proc_t *p = ttoproc(curthread); 2818 2819 uiop->uio_resid += remainder; 2820 mutex_enter(&p->p_lock); 2821 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2822 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2823 mutex_exit(&p->p_lock); 2824 return (EFBIG); 2825 } 2826 } 2827 2828 /* update the change attribute, if we have a write delegation */ 2829 2830 mutex_enter(&rp->r_statev4_lock); 2831 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2832 rp->r_deleg_change++; 2833 2834 mutex_exit(&rp->r_statev4_lock); 2835 2836 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, INTR4(vp))) 2837 return (EINTR); 2838 2839 /* 2840 * Bypass VM if caching has been disabled (e.g., locking) or if 2841 * using client-side direct I/O and the file is not mmap'd and 2842 * there are no cached pages. 2843 */ 2844 if ((vp->v_flag & VNOCACHE) || 2845 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2846 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2847 size_t bufsize; 2848 int count; 2849 u_offset_t org_offset; 2850 stable_how4 stab_comm; 2851 nfs4_fwrite: 2852 if (rp->r_flags & R4STALE) { 2853 resid = uiop->uio_resid; 2854 offset = uiop->uio_loffset; 2855 error = rp->r_error; 2856 /* 2857 * A close may have cleared r_error, if so, 2858 * propagate ESTALE error return properly 2859 */ 2860 if (error == 0) 2861 error = ESTALE; 2862 goto bottom; 2863 } 2864 2865 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2866 base = kmem_alloc(bufsize, KM_SLEEP); 2867 do { 2868 if (ioflag & FDSYNC) 2869 stab_comm = DATA_SYNC4; 2870 else 2871 stab_comm = FILE_SYNC4; 2872 resid = uiop->uio_resid; 2873 offset = uiop->uio_loffset; 2874 count = MIN(uiop->uio_resid, bufsize); 2875 org_offset = uiop->uio_loffset; 2876 error = uiomove(base, count, UIO_WRITE, uiop); 2877 if (!error) { 2878 error = nfs4write(vp, base, org_offset, 2879 count, cr, &stab_comm); 2880 if (!error) { 2881 mutex_enter(&rp->r_statelock); 2882 if (rp->r_size < uiop->uio_loffset) 2883 rp->r_size = uiop->uio_loffset; 2884 mutex_exit(&rp->r_statelock); 2885 } 2886 } 2887 } while (!error && uiop->uio_resid > 0); 2888 kmem_free(base, bufsize); 2889 goto bottom; 2890 } 2891 2892 bsize = vp->v_vfsp->vfs_bsize; 2893 2894 do { 2895 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2896 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2897 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2898 2899 resid = uiop->uio_resid; 2900 offset = uiop->uio_loffset; 2901 2902 if (rp->r_flags & R4STALE) { 2903 error = rp->r_error; 2904 /* 2905 * A close may have cleared r_error, if so, 2906 * propagate ESTALE error return properly 2907 */ 2908 if (error == 0) 2909 error = ESTALE; 2910 break; 2911 } 2912 2913 /* 2914 * Don't create dirty pages faster than they 2915 * can be cleaned so that the system doesn't 2916 * get imbalanced. If the async queue is 2917 * maxed out, then wait for it to drain before 2918 * creating more dirty pages. Also, wait for 2919 * any threads doing pagewalks in the vop_getattr 2920 * entry points so that they don't block for 2921 * long periods. 2922 */ 2923 mutex_enter(&rp->r_statelock); 2924 while ((mi->mi_max_threads != 0 && 2925 rp->r_awcount > 2 * mi->mi_max_threads) || 2926 rp->r_gcount > 0) { 2927 if (INTR4(vp)) { 2928 klwp_t *lwp = ttolwp(curthread); 2929 2930 if (lwp != NULL) 2931 lwp->lwp_nostop++; 2932 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2933 mutex_exit(&rp->r_statelock); 2934 if (lwp != NULL) 2935 lwp->lwp_nostop--; 2936 error = EINTR; 2937 goto bottom; 2938 } 2939 if (lwp != NULL) 2940 lwp->lwp_nostop--; 2941 } else 2942 cv_wait(&rp->r_cv, &rp->r_statelock); 2943 } 2944 mutex_exit(&rp->r_statelock); 2945 2946 /* 2947 * Touch the page and fault it in if it is not in core 2948 * before segmap_getmapflt or vpm_data_copy can lock it. 2949 * This is to avoid the deadlock if the buffer is mapped 2950 * to the same file through mmap which we want to write. 2951 */ 2952 uio_prefaultpages((long)n, uiop); 2953 2954 if (vpm_enable) { 2955 /* 2956 * It will use kpm mappings, so no need to 2957 * pass an address. 2958 */ 2959 error = writerp4(rp, NULL, n, uiop, 0); 2960 } else { 2961 if (segmap_kpm) { 2962 int pon = uiop->uio_loffset & PAGEOFFSET; 2963 size_t pn = MIN(PAGESIZE - pon, 2964 uiop->uio_resid); 2965 int pagecreate; 2966 2967 mutex_enter(&rp->r_statelock); 2968 pagecreate = (pon == 0) && (pn == PAGESIZE || 2969 uiop->uio_loffset + pn >= rp->r_size); 2970 mutex_exit(&rp->r_statelock); 2971 2972 base = segmap_getmapflt(segkmap, vp, off + on, 2973 pn, !pagecreate, S_WRITE); 2974 2975 error = writerp4(rp, base + pon, n, uiop, 2976 pagecreate); 2977 2978 } else { 2979 base = segmap_getmapflt(segkmap, vp, off + on, 2980 n, 0, S_READ); 2981 error = writerp4(rp, base + on, n, uiop, 0); 2982 } 2983 } 2984 2985 if (!error) { 2986 if (mi->mi_flags & MI4_NOAC) 2987 flags = SM_WRITE; 2988 else if ((uiop->uio_loffset % bsize) == 0 || 2989 IS_SWAPVP(vp)) { 2990 /* 2991 * Have written a whole block. 2992 * Start an asynchronous write 2993 * and mark the buffer to 2994 * indicate that it won't be 2995 * needed again soon. 2996 */ 2997 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2998 } else 2999 flags = 0; 3000 if ((ioflag & (FSYNC|FDSYNC)) || 3001 (rp->r_flags & R4OUTOFSPACE)) { 3002 flags &= ~SM_ASYNC; 3003 flags |= SM_WRITE; 3004 } 3005 if (vpm_enable) { 3006 error = vpm_sync_pages(vp, off, n, flags); 3007 } else { 3008 error = segmap_release(segkmap, base, flags); 3009 } 3010 } else { 3011 if (vpm_enable) { 3012 (void) vpm_sync_pages(vp, off, n, 0); 3013 } else { 3014 (void) segmap_release(segkmap, base, 0); 3015 } 3016 /* 3017 * In the event that we got an access error while 3018 * faulting in a page for a write-only file just 3019 * force a write. 3020 */ 3021 if (error == EACCES) 3022 goto nfs4_fwrite; 3023 } 3024 } while (!error && uiop->uio_resid > 0); 3025 3026 bottom: 3027 if (error) { 3028 uiop->uio_resid = resid + remainder; 3029 uiop->uio_loffset = offset; 3030 } else { 3031 uiop->uio_resid += remainder; 3032 3033 mutex_enter(&rp->r_statev4_lock); 3034 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 3035 gethrestime(&rp->r_attr.va_mtime); 3036 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3037 } 3038 mutex_exit(&rp->r_statev4_lock); 3039 } 3040 3041 nfs_rw_exit(&rp->r_lkserlock); 3042 3043 return (error); 3044 } 3045 3046 /* 3047 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 3048 */ 3049 static int 3050 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 3051 int flags, cred_t *cr) 3052 { 3053 struct buf *bp; 3054 int error; 3055 page_t *savepp; 3056 uchar_t fsdata; 3057 stable_how4 stab_comm; 3058 3059 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3060 bp = pageio_setup(pp, len, vp, flags); 3061 ASSERT(bp != NULL); 3062 3063 /* 3064 * pageio_setup should have set b_addr to 0. This 3065 * is correct since we want to do I/O on a page 3066 * boundary. bp_mapin will use this addr to calculate 3067 * an offset, and then set b_addr to the kernel virtual 3068 * address it allocated for us. 3069 */ 3070 ASSERT(bp->b_un.b_addr == 0); 3071 3072 bp->b_edev = 0; 3073 bp->b_dev = 0; 3074 bp->b_lblkno = lbtodb(off); 3075 bp->b_file = vp; 3076 bp->b_offset = (offset_t)off; 3077 bp_mapin(bp); 3078 3079 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 3080 freemem > desfree) 3081 stab_comm = UNSTABLE4; 3082 else 3083 stab_comm = FILE_SYNC4; 3084 3085 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 3086 3087 bp_mapout(bp); 3088 pageio_done(bp); 3089 3090 if (stab_comm == UNSTABLE4) 3091 fsdata = C_DELAYCOMMIT; 3092 else 3093 fsdata = C_NOCOMMIT; 3094 3095 savepp = pp; 3096 do { 3097 pp->p_fsdata = fsdata; 3098 } while ((pp = pp->p_next) != savepp); 3099 3100 return (error); 3101 } 3102 3103 /* 3104 */ 3105 static int 3106 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3107 { 3108 nfs4_open_owner_t *oop; 3109 nfs4_open_stream_t *osp; 3110 rnode4_t *rp = VTOR4(vp); 3111 mntinfo4_t *mi = VTOMI4(vp); 3112 int reopen_needed; 3113 3114 ASSERT(nfs_zone() == mi->mi_zone); 3115 3116 3117 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3118 if (!oop) 3119 return (EIO); 3120 3121 /* returns with 'os_sync_lock' held */ 3122 osp = find_open_stream(oop, rp); 3123 if (!osp) { 3124 open_owner_rele(oop); 3125 return (EIO); 3126 } 3127 3128 if (osp->os_failed_reopen) { 3129 mutex_exit(&osp->os_sync_lock); 3130 open_stream_rele(osp, rp); 3131 open_owner_rele(oop); 3132 return (EIO); 3133 } 3134 3135 /* 3136 * Determine whether a reopen is needed. If this 3137 * is a delegation open stream, then the os_delegation bit 3138 * should be set. 3139 */ 3140 3141 reopen_needed = osp->os_delegation; 3142 3143 mutex_exit(&osp->os_sync_lock); 3144 open_owner_rele(oop); 3145 3146 if (reopen_needed) { 3147 nfs4_error_zinit(ep); 3148 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3149 mutex_enter(&osp->os_sync_lock); 3150 if (ep->error || ep->stat || osp->os_failed_reopen) { 3151 mutex_exit(&osp->os_sync_lock); 3152 open_stream_rele(osp, rp); 3153 return (EIO); 3154 } 3155 mutex_exit(&osp->os_sync_lock); 3156 } 3157 open_stream_rele(osp, rp); 3158 3159 return (0); 3160 } 3161 3162 /* 3163 * Write to file. Writes to remote server in largest size 3164 * chunks that the server can handle. Write is synchronous. 3165 */ 3166 static int 3167 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3168 stable_how4 *stab_comm) 3169 { 3170 mntinfo4_t *mi; 3171 COMPOUND4args_clnt args; 3172 COMPOUND4res_clnt res; 3173 WRITE4args *wargs; 3174 WRITE4res *wres; 3175 nfs_argop4 argop[2]; 3176 nfs_resop4 *resop; 3177 int tsize; 3178 stable_how4 stable; 3179 rnode4_t *rp; 3180 int doqueue = 1; 3181 bool_t needrecov; 3182 nfs4_recov_state_t recov_state; 3183 nfs4_stateid_types_t sid_types; 3184 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3185 int recov; 3186 3187 rp = VTOR4(vp); 3188 mi = VTOMI4(vp); 3189 3190 ASSERT(nfs_zone() == mi->mi_zone); 3191 3192 stable = *stab_comm; 3193 *stab_comm = FILE_SYNC4; 3194 3195 needrecov = FALSE; 3196 recov_state.rs_flags = 0; 3197 recov_state.rs_num_retry_despite_err = 0; 3198 nfs4_init_stateid_types(&sid_types); 3199 3200 /* Is curthread the recovery thread? */ 3201 mutex_enter(&mi->mi_lock); 3202 recov = (mi->mi_recovthread == curthread); 3203 mutex_exit(&mi->mi_lock); 3204 3205 recov_retry: 3206 args.ctag = TAG_WRITE; 3207 args.array_len = 2; 3208 args.array = argop; 3209 3210 if (!recov) { 3211 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3212 &recov_state, NULL); 3213 if (e.error) 3214 return (e.error); 3215 } 3216 3217 /* 0. putfh target fh */ 3218 argop[0].argop = OP_CPUTFH; 3219 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3220 3221 /* 1. write */ 3222 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3223 3224 do { 3225 3226 wargs->offset = (offset4)offset; 3227 wargs->data_val = base; 3228 3229 if (mi->mi_io_kstats) { 3230 mutex_enter(&mi->mi_lock); 3231 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3232 mutex_exit(&mi->mi_lock); 3233 } 3234 3235 if ((vp->v_flag & VNOCACHE) || 3236 (rp->r_flags & R4DIRECTIO) || 3237 (mi->mi_flags & MI4_DIRECTIO)) 3238 tsize = MIN(mi->mi_stsize, count); 3239 else 3240 tsize = MIN(mi->mi_curwrite, count); 3241 wargs->data_len = (uint_t)tsize; 3242 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3243 3244 if (mi->mi_io_kstats) { 3245 mutex_enter(&mi->mi_lock); 3246 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3247 mutex_exit(&mi->mi_lock); 3248 } 3249 3250 if (!recov) { 3251 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3252 if (e.error && !needrecov) { 3253 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3254 &recov_state, needrecov); 3255 return (e.error); 3256 } 3257 } else { 3258 if (e.error) 3259 return (e.error); 3260 } 3261 3262 /* 3263 * Do handling of OLD_STATEID outside 3264 * of the normal recovery framework. 3265 * 3266 * If write receives a BAD stateid error while using a 3267 * delegation stateid, retry using the open stateid (if it 3268 * exists). If it doesn't have an open stateid, reopen the 3269 * file first, then retry. 3270 */ 3271 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3272 sid_types.cur_sid_type != SPEC_SID) { 3273 nfs4_save_stateid(&wargs->stateid, &sid_types); 3274 if (!recov) 3275 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3276 &recov_state, needrecov); 3277 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3278 goto recov_retry; 3279 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3280 sid_types.cur_sid_type == DEL_SID) { 3281 nfs4_save_stateid(&wargs->stateid, &sid_types); 3282 mutex_enter(&rp->r_statev4_lock); 3283 rp->r_deleg_return_pending = TRUE; 3284 mutex_exit(&rp->r_statev4_lock); 3285 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3286 if (!recov) 3287 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3288 &recov_state, needrecov); 3289 (void) xdr_free(xdr_COMPOUND4res_clnt, 3290 (caddr_t)&res); 3291 return (EIO); 3292 } 3293 if (!recov) 3294 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3295 &recov_state, needrecov); 3296 /* hold needed for nfs4delegreturn_thread */ 3297 VN_HOLD(vp); 3298 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3299 NFS4_DR_DISCARD), FALSE); 3300 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3301 goto recov_retry; 3302 } 3303 3304 if (needrecov) { 3305 bool_t abort; 3306 3307 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3308 "nfs4write: client got error %d, res.status %d" 3309 ", so start recovery", e.error, res.status)); 3310 3311 abort = nfs4_start_recovery(&e, 3312 VTOMI4(vp), vp, NULL, &wargs->stateid, 3313 NULL, OP_WRITE, NULL, NULL, NULL); 3314 if (!e.error) { 3315 e.error = geterrno4(res.status); 3316 (void) xdr_free(xdr_COMPOUND4res_clnt, 3317 (caddr_t)&res); 3318 } 3319 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3320 &recov_state, needrecov); 3321 if (abort == FALSE) 3322 goto recov_retry; 3323 return (e.error); 3324 } 3325 3326 if (res.status) { 3327 e.error = geterrno4(res.status); 3328 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3329 if (!recov) 3330 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3331 &recov_state, needrecov); 3332 return (e.error); 3333 } 3334 3335 resop = &res.array[1]; /* write res */ 3336 wres = &resop->nfs_resop4_u.opwrite; 3337 3338 if ((int)wres->count > tsize) { 3339 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3340 3341 zcmn_err(getzoneid(), CE_WARN, 3342 "nfs4write: server wrote %u, requested was %u", 3343 (int)wres->count, tsize); 3344 if (!recov) 3345 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3346 &recov_state, needrecov); 3347 return (EIO); 3348 } 3349 if (wres->committed == UNSTABLE4) { 3350 *stab_comm = UNSTABLE4; 3351 if (wargs->stable == DATA_SYNC4 || 3352 wargs->stable == FILE_SYNC4) { 3353 (void) xdr_free(xdr_COMPOUND4res_clnt, 3354 (caddr_t)&res); 3355 zcmn_err(getzoneid(), CE_WARN, 3356 "nfs4write: server %s did not commit " 3357 "to stable storage", 3358 rp->r_server->sv_hostname); 3359 if (!recov) 3360 nfs4_end_fop(VTOMI4(vp), vp, NULL, 3361 OH_WRITE, &recov_state, needrecov); 3362 return (EIO); 3363 } 3364 } 3365 3366 tsize = (int)wres->count; 3367 count -= tsize; 3368 base += tsize; 3369 offset += tsize; 3370 if (mi->mi_io_kstats) { 3371 mutex_enter(&mi->mi_lock); 3372 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3373 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3374 tsize; 3375 mutex_exit(&mi->mi_lock); 3376 } 3377 lwp_stat_update(LWP_STAT_OUBLK, 1); 3378 mutex_enter(&rp->r_statelock); 3379 if (rp->r_flags & R4HAVEVERF) { 3380 if (rp->r_writeverf != wres->writeverf) { 3381 nfs4_set_mod(vp); 3382 rp->r_writeverf = wres->writeverf; 3383 } 3384 } else { 3385 rp->r_writeverf = wres->writeverf; 3386 rp->r_flags |= R4HAVEVERF; 3387 } 3388 PURGE_ATTRCACHE4_LOCKED(rp); 3389 rp->r_flags |= R4WRITEMODIFIED; 3390 gethrestime(&rp->r_attr.va_mtime); 3391 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3392 mutex_exit(&rp->r_statelock); 3393 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3394 } while (count); 3395 3396 if (!recov) 3397 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, 3398 needrecov); 3399 3400 return (e.error); 3401 } 3402 3403 /* 3404 * Read from a file. Reads data in largest chunks our interface can handle. 3405 */ 3406 static int 3407 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3408 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3409 { 3410 mntinfo4_t *mi; 3411 COMPOUND4args_clnt args; 3412 COMPOUND4res_clnt res; 3413 READ4args *rargs; 3414 nfs_argop4 argop[2]; 3415 int tsize; 3416 int doqueue; 3417 rnode4_t *rp; 3418 int data_len; 3419 bool_t is_eof; 3420 bool_t needrecov = FALSE; 3421 nfs4_recov_state_t recov_state; 3422 nfs4_stateid_types_t sid_types; 3423 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3424 3425 rp = VTOR4(vp); 3426 mi = VTOMI4(vp); 3427 doqueue = 1; 3428 3429 ASSERT(nfs_zone() == mi->mi_zone); 3430 3431 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3432 3433 args.array_len = 2; 3434 args.array = argop; 3435 3436 nfs4_init_stateid_types(&sid_types); 3437 3438 recov_state.rs_flags = 0; 3439 recov_state.rs_num_retry_despite_err = 0; 3440 3441 recov_retry: 3442 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3443 &recov_state, NULL); 3444 if (e.error) 3445 return (e.error); 3446 3447 /* putfh target fh */ 3448 argop[0].argop = OP_CPUTFH; 3449 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3450 3451 /* read */ 3452 argop[1].argop = OP_READ; 3453 rargs = &argop[1].nfs_argop4_u.opread; 3454 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3455 OP_READ, &sid_types, async); 3456 3457 do { 3458 if (mi->mi_io_kstats) { 3459 mutex_enter(&mi->mi_lock); 3460 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3461 mutex_exit(&mi->mi_lock); 3462 } 3463 3464 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3465 "nfs4read: %s call, rp %s", 3466 needrecov ? "recov" : "first", 3467 rnode4info(rp))); 3468 3469 if ((vp->v_flag & VNOCACHE) || 3470 (rp->r_flags & R4DIRECTIO) || 3471 (mi->mi_flags & MI4_DIRECTIO)) 3472 tsize = MIN(mi->mi_tsize, count); 3473 else 3474 tsize = MIN(mi->mi_curread, count); 3475 3476 rargs->offset = (offset4)offset; 3477 rargs->count = (count4)tsize; 3478 rargs->res_data_val_alt = NULL; 3479 rargs->res_mblk = NULL; 3480 rargs->res_uiop = NULL; 3481 rargs->res_maxsize = 0; 3482 rargs->wlist = NULL; 3483 3484 if (uiop) 3485 rargs->res_uiop = uiop; 3486 else 3487 rargs->res_data_val_alt = base; 3488 rargs->res_maxsize = tsize; 3489 3490 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3491 #ifdef DEBUG 3492 if (nfs4read_error_inject) { 3493 res.status = nfs4read_error_inject; 3494 nfs4read_error_inject = 0; 3495 } 3496 #endif 3497 3498 if (mi->mi_io_kstats) { 3499 mutex_enter(&mi->mi_lock); 3500 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3501 mutex_exit(&mi->mi_lock); 3502 } 3503 3504 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3505 if (e.error != 0 && !needrecov) { 3506 nfs4_end_fop(mi, vp, NULL, OH_READ, 3507 &recov_state, needrecov); 3508 return (e.error); 3509 } 3510 3511 /* 3512 * Do proper retry for OLD and BAD stateid errors outside 3513 * of the normal recovery framework. There are two differences 3514 * between async and sync reads. The first is that we allow 3515 * retry on BAD_STATEID for async reads, but not sync reads. 3516 * The second is that we mark the file dead for a failed 3517 * attempt with a special stateid for sync reads, but just 3518 * return EIO for async reads. 3519 * 3520 * If a sync read receives a BAD stateid error while using a 3521 * delegation stateid, retry using the open stateid (if it 3522 * exists). If it doesn't have an open stateid, reopen the 3523 * file first, then retry. 3524 */ 3525 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3526 res.status == NFS4ERR_BAD_STATEID) && async) { 3527 nfs4_end_fop(mi, vp, NULL, OH_READ, 3528 &recov_state, needrecov); 3529 if (sid_types.cur_sid_type == SPEC_SID) { 3530 (void) xdr_free(xdr_COMPOUND4res_clnt, 3531 (caddr_t)&res); 3532 return (EIO); 3533 } 3534 nfs4_save_stateid(&rargs->stateid, &sid_types); 3535 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3536 goto recov_retry; 3537 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3538 !async && sid_types.cur_sid_type != SPEC_SID) { 3539 nfs4_save_stateid(&rargs->stateid, &sid_types); 3540 nfs4_end_fop(mi, vp, NULL, OH_READ, 3541 &recov_state, needrecov); 3542 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3543 goto recov_retry; 3544 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3545 sid_types.cur_sid_type == DEL_SID) { 3546 nfs4_save_stateid(&rargs->stateid, &sid_types); 3547 mutex_enter(&rp->r_statev4_lock); 3548 rp->r_deleg_return_pending = TRUE; 3549 mutex_exit(&rp->r_statev4_lock); 3550 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3551 nfs4_end_fop(mi, vp, NULL, OH_READ, 3552 &recov_state, needrecov); 3553 (void) xdr_free(xdr_COMPOUND4res_clnt, 3554 (caddr_t)&res); 3555 return (EIO); 3556 } 3557 nfs4_end_fop(mi, vp, NULL, OH_READ, 3558 &recov_state, needrecov); 3559 /* hold needed for nfs4delegreturn_thread */ 3560 VN_HOLD(vp); 3561 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3562 NFS4_DR_DISCARD), FALSE); 3563 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3564 goto recov_retry; 3565 } 3566 if (needrecov) { 3567 bool_t abort; 3568 3569 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3570 "nfs4read: initiating recovery\n")); 3571 abort = nfs4_start_recovery(&e, 3572 mi, vp, NULL, &rargs->stateid, 3573 NULL, OP_READ, NULL, NULL, NULL); 3574 nfs4_end_fop(mi, vp, NULL, OH_READ, 3575 &recov_state, needrecov); 3576 /* 3577 * Do not retry if we got OLD_STATEID using a special 3578 * stateid. This avoids looping with a broken server. 3579 */ 3580 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3581 sid_types.cur_sid_type == SPEC_SID) 3582 abort = TRUE; 3583 3584 if (abort == FALSE) { 3585 /* 3586 * Need to retry all possible stateids in 3587 * case the recovery error wasn't stateid 3588 * related or the stateids have become 3589 * stale (server reboot). 3590 */ 3591 nfs4_init_stateid_types(&sid_types); 3592 (void) xdr_free(xdr_COMPOUND4res_clnt, 3593 (caddr_t)&res); 3594 goto recov_retry; 3595 } 3596 3597 if (!e.error) { 3598 e.error = geterrno4(res.status); 3599 (void) xdr_free(xdr_COMPOUND4res_clnt, 3600 (caddr_t)&res); 3601 } 3602 return (e.error); 3603 } 3604 3605 if (res.status) { 3606 e.error = geterrno4(res.status); 3607 nfs4_end_fop(mi, vp, NULL, OH_READ, 3608 &recov_state, needrecov); 3609 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3610 return (e.error); 3611 } 3612 3613 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3614 count -= data_len; 3615 if (base) 3616 base += data_len; 3617 offset += data_len; 3618 if (mi->mi_io_kstats) { 3619 mutex_enter(&mi->mi_lock); 3620 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3621 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3622 mutex_exit(&mi->mi_lock); 3623 } 3624 lwp_stat_update(LWP_STAT_INBLK, 1); 3625 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3626 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3627 3628 } while (count && !is_eof); 3629 3630 *residp = count; 3631 3632 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3633 3634 return (e.error); 3635 } 3636 3637 /* ARGSUSED */ 3638 static int 3639 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp, 3640 caller_context_t *ct) 3641 { 3642 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3643 return (EIO); 3644 switch (cmd) { 3645 case _FIODIRECTIO: 3646 return (nfs4_directio(vp, (int)arg, cr)); 3647 default: 3648 return (ENOTTY); 3649 } 3650 } 3651 3652 /* ARGSUSED */ 3653 int 3654 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3655 caller_context_t *ct) 3656 { 3657 int error; 3658 rnode4_t *rp = VTOR4(vp); 3659 3660 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3661 return (EIO); 3662 /* 3663 * If it has been specified that the return value will 3664 * just be used as a hint, and we are only being asked 3665 * for size, fsid or rdevid, then return the client's 3666 * notion of these values without checking to make sure 3667 * that the attribute cache is up to date. 3668 * The whole point is to avoid an over the wire GETATTR 3669 * call. 3670 */ 3671 if (flags & ATTR_HINT) { 3672 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) { 3673 mutex_enter(&rp->r_statelock); 3674 if (vap->va_mask & AT_SIZE) 3675 vap->va_size = rp->r_size; 3676 if (vap->va_mask & AT_FSID) 3677 vap->va_fsid = rp->r_attr.va_fsid; 3678 if (vap->va_mask & AT_RDEV) 3679 vap->va_rdev = rp->r_attr.va_rdev; 3680 mutex_exit(&rp->r_statelock); 3681 return (0); 3682 } 3683 } 3684 3685 /* 3686 * Only need to flush pages if asking for the mtime 3687 * and if there any dirty pages or any outstanding 3688 * asynchronous (write) requests for this file. 3689 */ 3690 if (vap->va_mask & AT_MTIME) { 3691 rp = VTOR4(vp); 3692 if (nfs4_has_pages(vp)) { 3693 mutex_enter(&rp->r_statev4_lock); 3694 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3695 mutex_exit(&rp->r_statev4_lock); 3696 if (rp->r_flags & R4DIRTY || 3697 rp->r_awcount > 0) { 3698 mutex_enter(&rp->r_statelock); 3699 rp->r_gcount++; 3700 mutex_exit(&rp->r_statelock); 3701 error = 3702 nfs4_putpage(vp, (u_offset_t)0, 3703 0, 0, cr, NULL); 3704 mutex_enter(&rp->r_statelock); 3705 if (error && (error == ENOSPC || 3706 error == EDQUOT)) { 3707 if (!rp->r_error) 3708 rp->r_error = error; 3709 } 3710 if (--rp->r_gcount == 0) 3711 cv_broadcast(&rp->r_cv); 3712 mutex_exit(&rp->r_statelock); 3713 } 3714 } else { 3715 mutex_exit(&rp->r_statev4_lock); 3716 } 3717 } 3718 } 3719 return (nfs4getattr(vp, vap, cr)); 3720 } 3721 3722 int 3723 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3724 { 3725 /* 3726 * If these are the only two bits cleared 3727 * on the server then return 0 (OK) else 3728 * return 1 (BAD). 3729 */ 3730 on_client &= ~(S_ISUID|S_ISGID); 3731 if (on_client == from_server) 3732 return (0); 3733 else 3734 return (1); 3735 } 3736 3737 /*ARGSUSED4*/ 3738 static int 3739 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3740 caller_context_t *ct) 3741 { 3742 int error; 3743 3744 if (vap->va_mask & AT_NOSET) 3745 return (EINVAL); 3746 3747 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3748 return (EIO); 3749 3750 /* 3751 * Don't call secpolicy_vnode_setattr, the client cannot 3752 * use its cached attributes to make security decisions 3753 * as the server may be faking mode bits or mapping uid/gid. 3754 * Always just let the server to the checking. 3755 * If we provide the ability to remove basic priviledges 3756 * to setattr (e.g. basic without chmod) then we will 3757 * need to add a check here before calling the server. 3758 */ 3759 error = nfs4setattr(vp, vap, flags, cr, NULL); 3760 3761 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0) 3762 vnevent_truncate(vp, ct); 3763 3764 return (error); 3765 } 3766 3767 /* 3768 * To replace the "guarded" version 3 setattr, we use two types of compound 3769 * setattr requests: 3770 * 1. The "normal" setattr, used when the size of the file isn't being 3771 * changed - { Putfh <fh>; Setattr; Getattr }/ 3772 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3773 * with only ctime as the argument. If the server ctime differs from 3774 * what is cached on the client, the verify will fail, but we would 3775 * already have the ctime from the preceding getattr, so just set it 3776 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3777 * Setattr; Getattr }. 3778 * 3779 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3780 * this setattr and NULL if they are not. 3781 */ 3782 static int 3783 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3784 vsecattr_t *vsap) 3785 { 3786 COMPOUND4args_clnt args; 3787 COMPOUND4res_clnt res, *resp = NULL; 3788 nfs4_ga_res_t *garp = NULL; 3789 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3790 nfs_argop4 argop[5]; 3791 int verify_argop = -1; 3792 int setattr_argop = 1; 3793 nfs_resop4 *resop; 3794 vattr_t va; 3795 rnode4_t *rp; 3796 int doqueue = 1; 3797 uint_t mask = vap->va_mask; 3798 mode_t omode; 3799 vsecattr_t *vsp; 3800 timestruc_t ctime; 3801 bool_t needrecov = FALSE; 3802 nfs4_recov_state_t recov_state; 3803 nfs4_stateid_types_t sid_types; 3804 stateid4 stateid; 3805 hrtime_t t; 3806 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3807 servinfo4_t *svp; 3808 bitmap4 supp_attrs; 3809 3810 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3811 rp = VTOR4(vp); 3812 nfs4_init_stateid_types(&sid_types); 3813 3814 /* 3815 * Only need to flush pages if there are any pages and 3816 * if the file is marked as dirty in some fashion. The 3817 * file must be flushed so that we can accurately 3818 * determine the size of the file and the cached data 3819 * after the SETATTR returns. A file is considered to 3820 * be dirty if it is either marked with R4DIRTY, has 3821 * outstanding i/o's active, or is mmap'd. In this 3822 * last case, we can't tell whether there are dirty 3823 * pages, so we flush just to be sure. 3824 */ 3825 if (nfs4_has_pages(vp) && 3826 ((rp->r_flags & R4DIRTY) || 3827 rp->r_count > 0 || 3828 rp->r_mapcnt > 0)) { 3829 ASSERT(vp->v_type != VCHR); 3830 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL); 3831 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3832 mutex_enter(&rp->r_statelock); 3833 if (!rp->r_error) 3834 rp->r_error = e.error; 3835 mutex_exit(&rp->r_statelock); 3836 } 3837 } 3838 3839 if (mask & AT_SIZE) { 3840 /* 3841 * Verification setattr compound for non-deleg AT_SIZE: 3842 * { Putfh; Getattr; Verify; Setattr; Getattr } 3843 * Set ctime local here (outside the do_again label) 3844 * so that subsequent retries (after failed VERIFY) 3845 * will use ctime from GETATTR results (from failed 3846 * verify compound) as VERIFY arg. 3847 * If file has delegation, then VERIFY(time_metadata) 3848 * is of little added value, so don't bother. 3849 */ 3850 mutex_enter(&rp->r_statev4_lock); 3851 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3852 rp->r_deleg_return_pending) { 3853 numops = 5; 3854 ctime = rp->r_attr.va_ctime; 3855 } 3856 mutex_exit(&rp->r_statev4_lock); 3857 } 3858 3859 recov_state.rs_flags = 0; 3860 recov_state.rs_num_retry_despite_err = 0; 3861 3862 args.ctag = TAG_SETATTR; 3863 do_again: 3864 recov_retry: 3865 setattr_argop = numops - 2; 3866 3867 args.array = argop; 3868 args.array_len = numops; 3869 3870 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3871 if (e.error) 3872 return (e.error); 3873 3874 3875 /* putfh target fh */ 3876 argop[0].argop = OP_CPUTFH; 3877 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3878 3879 if (numops == 5) { 3880 /* 3881 * We only care about the ctime, but need to get mtime 3882 * and size for proper cache update. 3883 */ 3884 /* getattr */ 3885 argop[1].argop = OP_GETATTR; 3886 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3887 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3888 3889 /* verify - set later in loop */ 3890 verify_argop = 2; 3891 } 3892 3893 /* setattr */ 3894 svp = rp->r_server; 3895 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3896 supp_attrs = svp->sv_supp_attrs; 3897 nfs_rw_exit(&svp->sv_lock); 3898 3899 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3900 supp_attrs, &e.error, &sid_types); 3901 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3902 if (e.error) { 3903 /* req time field(s) overflow - return immediately */ 3904 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3905 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3906 opsetattr.obj_attributes); 3907 return (e.error); 3908 } 3909 omode = rp->r_attr.va_mode; 3910 3911 /* getattr */ 3912 argop[numops-1].argop = OP_GETATTR; 3913 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3914 /* 3915 * If we are setting the ACL (indicated only by vsap != NULL), request 3916 * the ACL in this getattr. The ACL returned from this getattr will be 3917 * used in updating the ACL cache. 3918 */ 3919 if (vsap != NULL) 3920 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3921 FATTR4_ACL_MASK; 3922 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3923 3924 /* 3925 * setattr iterates if the object size is set and the cached ctime 3926 * does not match the file ctime. In that case, verify the ctime first. 3927 */ 3928 3929 do { 3930 if (verify_argop != -1) { 3931 /* 3932 * Verify that the ctime match before doing setattr. 3933 */ 3934 va.va_mask = AT_CTIME; 3935 va.va_ctime = ctime; 3936 svp = rp->r_server; 3937 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3938 supp_attrs = svp->sv_supp_attrs; 3939 nfs_rw_exit(&svp->sv_lock); 3940 e.error = nfs4args_verify(&argop[verify_argop], &va, 3941 OP_VERIFY, supp_attrs); 3942 if (e.error) { 3943 /* req time field(s) overflow - return */ 3944 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3945 needrecov); 3946 break; 3947 } 3948 } 3949 3950 doqueue = 1; 3951 3952 t = gethrtime(); 3953 3954 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3955 3956 /* 3957 * Purge the access cache and ACL cache if changing either the 3958 * owner of the file, the group owner, or the mode. These may 3959 * change the access permissions of the file, so purge old 3960 * information and start over again. 3961 */ 3962 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3963 (void) nfs4_access_purge_rp(rp); 3964 if (rp->r_secattr != NULL) { 3965 mutex_enter(&rp->r_statelock); 3966 vsp = rp->r_secattr; 3967 rp->r_secattr = NULL; 3968 mutex_exit(&rp->r_statelock); 3969 if (vsp != NULL) 3970 nfs4_acl_free_cache(vsp); 3971 } 3972 } 3973 3974 /* 3975 * If res.array_len == numops, then everything succeeded, 3976 * except for possibly the final getattr. If only the 3977 * last getattr failed, give up, and don't try recovery. 3978 */ 3979 if (res.array_len == numops) { 3980 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3981 needrecov); 3982 if (! e.error) 3983 resp = &res; 3984 break; 3985 } 3986 3987 /* 3988 * if either rpc call failed or completely succeeded - done 3989 */ 3990 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3991 if (e.error) { 3992 PURGE_ATTRCACHE4(vp); 3993 if (!needrecov) { 3994 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3995 needrecov); 3996 break; 3997 } 3998 } 3999 4000 /* 4001 * Do proper retry for OLD_STATEID outside of the normal 4002 * recovery framework. 4003 */ 4004 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 4005 sid_types.cur_sid_type != SPEC_SID && 4006 sid_types.cur_sid_type != NO_SID) { 4007 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4008 needrecov); 4009 nfs4_save_stateid(&stateid, &sid_types); 4010 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4011 opsetattr.obj_attributes); 4012 if (verify_argop != -1) { 4013 nfs4args_verify_free(&argop[verify_argop]); 4014 verify_argop = -1; 4015 } 4016 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4017 goto recov_retry; 4018 } 4019 4020 if (needrecov) { 4021 bool_t abort; 4022 4023 abort = nfs4_start_recovery(&e, 4024 VTOMI4(vp), vp, NULL, NULL, NULL, 4025 OP_SETATTR, NULL, NULL, NULL); 4026 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4027 needrecov); 4028 /* 4029 * Do not retry if we failed with OLD_STATEID using 4030 * a special stateid. This is done to avoid looping 4031 * with a broken server. 4032 */ 4033 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 4034 (sid_types.cur_sid_type == SPEC_SID || 4035 sid_types.cur_sid_type == NO_SID)) 4036 abort = TRUE; 4037 if (!e.error) { 4038 if (res.status == NFS4ERR_BADOWNER) 4039 nfs4_log_badowner(VTOMI4(vp), 4040 OP_SETATTR); 4041 4042 e.error = geterrno4(res.status); 4043 (void) xdr_free(xdr_COMPOUND4res_clnt, 4044 (caddr_t)&res); 4045 } 4046 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4047 opsetattr.obj_attributes); 4048 if (verify_argop != -1) { 4049 nfs4args_verify_free(&argop[verify_argop]); 4050 verify_argop = -1; 4051 } 4052 if (abort == FALSE) { 4053 /* 4054 * Need to retry all possible stateids in 4055 * case the recovery error wasn't stateid 4056 * related or the stateids have become 4057 * stale (server reboot). 4058 */ 4059 nfs4_init_stateid_types(&sid_types); 4060 goto recov_retry; 4061 } 4062 return (e.error); 4063 } 4064 4065 /* 4066 * Need to call nfs4_end_op before nfs4getattr to 4067 * avoid potential nfs4_start_op deadlock. See RFE 4068 * 4777612. Calls to nfs4_invalidate_pages() and 4069 * nfs4_purge_stale_fh() might also generate over the 4070 * wire calls which my cause nfs4_start_op() deadlock. 4071 */ 4072 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4073 4074 /* 4075 * Check to update lease. 4076 */ 4077 resp = &res; 4078 if (res.status == NFS4_OK) { 4079 break; 4080 } 4081 4082 /* 4083 * Check if verify failed to see if try again 4084 */ 4085 if ((verify_argop == -1) || (res.array_len != 3)) { 4086 /* 4087 * can't continue... 4088 */ 4089 if (res.status == NFS4ERR_BADOWNER) 4090 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 4091 4092 e.error = geterrno4(res.status); 4093 } else { 4094 /* 4095 * When the verify request fails, the client ctime is 4096 * not in sync with the server. This is the same as 4097 * the version 3 "not synchronized" error, and we 4098 * handle it in a similar manner (XXX do we need to???). 4099 * Use the ctime returned in the first getattr for 4100 * the input to the next verify. 4101 * If we couldn't get the attributes, then we give up 4102 * because we can't complete the operation as required. 4103 */ 4104 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 4105 } 4106 if (e.error) { 4107 PURGE_ATTRCACHE4(vp); 4108 nfs4_purge_stale_fh(e.error, vp, cr); 4109 } else { 4110 /* 4111 * retry with a new verify value 4112 */ 4113 ctime = garp->n4g_va.va_ctime; 4114 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4115 resp = NULL; 4116 } 4117 if (!e.error) { 4118 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4119 opsetattr.obj_attributes); 4120 if (verify_argop != -1) { 4121 nfs4args_verify_free(&argop[verify_argop]); 4122 verify_argop = -1; 4123 } 4124 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4125 goto do_again; 4126 } 4127 } while (!e.error); 4128 4129 if (e.error) { 4130 /* 4131 * If we are here, rfs4call has an irrecoverable error - return 4132 */ 4133 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4134 opsetattr.obj_attributes); 4135 if (verify_argop != -1) { 4136 nfs4args_verify_free(&argop[verify_argop]); 4137 verify_argop = -1; 4138 } 4139 if (resp) 4140 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4141 return (e.error); 4142 } 4143 4144 4145 4146 /* 4147 * If changing the size of the file, invalidate 4148 * any local cached data which is no longer part 4149 * of the file. We also possibly invalidate the 4150 * last page in the file. We could use 4151 * pvn_vpzero(), but this would mark the page as 4152 * modified and require it to be written back to 4153 * the server for no particularly good reason. 4154 * This way, if we access it, then we bring it 4155 * back in. A read should be cheaper than a 4156 * write. 4157 */ 4158 if (mask & AT_SIZE) { 4159 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4160 } 4161 4162 /* either no error or one of the postop getattr failed */ 4163 4164 /* 4165 * XXX Perform a simplified version of wcc checking. Instead of 4166 * have another getattr to get pre-op, just purge cache if 4167 * any of the ops prior to and including the getattr failed. 4168 * If the getattr succeeded then update the attrcache accordingly. 4169 */ 4170 4171 garp = NULL; 4172 if (res.status == NFS4_OK) { 4173 /* 4174 * Last getattr 4175 */ 4176 resop = &res.array[numops - 1]; 4177 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4178 } 4179 /* 4180 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4181 * rather than filling it. See the function itself for details. 4182 */ 4183 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4184 if (garp != NULL) { 4185 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4186 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4187 vs_ace4_destroy(&garp->n4g_vsa); 4188 } else { 4189 if (vsap != NULL) { 4190 /* 4191 * The ACL was supposed to be set and to be 4192 * returned in the last getattr of this 4193 * compound, but for some reason the getattr 4194 * result doesn't contain the ACL. In this 4195 * case, purge the ACL cache. 4196 */ 4197 if (rp->r_secattr != NULL) { 4198 mutex_enter(&rp->r_statelock); 4199 vsp = rp->r_secattr; 4200 rp->r_secattr = NULL; 4201 mutex_exit(&rp->r_statelock); 4202 if (vsp != NULL) 4203 nfs4_acl_free_cache(vsp); 4204 } 4205 } 4206 } 4207 } 4208 4209 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4210 /* 4211 * Set the size, rather than relying on getting it updated 4212 * via a GETATTR. With delegations the client tries to 4213 * suppress GETATTR calls. 4214 */ 4215 mutex_enter(&rp->r_statelock); 4216 rp->r_size = vap->va_size; 4217 mutex_exit(&rp->r_statelock); 4218 } 4219 4220 /* 4221 * Can free up request args and res 4222 */ 4223 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4224 opsetattr.obj_attributes); 4225 if (verify_argop != -1) { 4226 nfs4args_verify_free(&argop[verify_argop]); 4227 verify_argop = -1; 4228 } 4229 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4230 4231 /* 4232 * Some servers will change the mode to clear the setuid 4233 * and setgid bits when changing the uid or gid. The 4234 * client needs to compensate appropriately. 4235 */ 4236 if (mask & (AT_UID | AT_GID)) { 4237 int terror, do_setattr; 4238 4239 do_setattr = 0; 4240 va.va_mask = AT_MODE; 4241 terror = nfs4getattr(vp, &va, cr); 4242 if (!terror && 4243 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4244 (!(mask & AT_MODE) && va.va_mode != omode))) { 4245 va.va_mask = AT_MODE; 4246 if (mask & AT_MODE) { 4247 /* 4248 * We asked the mode to be changed and what 4249 * we just got from the server in getattr is 4250 * not what we wanted it to be, so set it now. 4251 */ 4252 va.va_mode = vap->va_mode; 4253 do_setattr = 1; 4254 } else { 4255 /* 4256 * We did not ask the mode to be changed, 4257 * Check to see that the server just cleared 4258 * I_SUID and I_GUID from it. If not then 4259 * set mode to omode with UID/GID cleared. 4260 */ 4261 if (nfs4_compare_modes(va.va_mode, omode)) { 4262 omode &= ~(S_ISUID|S_ISGID); 4263 va.va_mode = omode; 4264 do_setattr = 1; 4265 } 4266 } 4267 4268 if (do_setattr) 4269 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4270 } 4271 } 4272 4273 return (e.error); 4274 } 4275 4276 /* ARGSUSED */ 4277 static int 4278 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct) 4279 { 4280 COMPOUND4args_clnt args; 4281 COMPOUND4res_clnt res; 4282 int doqueue; 4283 uint32_t acc, resacc, argacc; 4284 rnode4_t *rp; 4285 cred_t *cred, *ncr, *ncrfree = NULL; 4286 nfs4_access_type_t cacc; 4287 int num_ops; 4288 nfs_argop4 argop[3]; 4289 nfs_resop4 *resop; 4290 bool_t needrecov = FALSE, do_getattr; 4291 nfs4_recov_state_t recov_state; 4292 int rpc_error; 4293 hrtime_t t; 4294 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4295 mntinfo4_t *mi = VTOMI4(vp); 4296 4297 if (nfs_zone() != mi->mi_zone) 4298 return (EIO); 4299 4300 acc = 0; 4301 if (mode & VREAD) 4302 acc |= ACCESS4_READ; 4303 if (mode & VWRITE) { 4304 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4305 return (EROFS); 4306 if (vp->v_type == VDIR) 4307 acc |= ACCESS4_DELETE; 4308 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4309 } 4310 if (mode & VEXEC) { 4311 if (vp->v_type == VDIR) 4312 acc |= ACCESS4_LOOKUP; 4313 else 4314 acc |= ACCESS4_EXECUTE; 4315 } 4316 4317 if (VTOR4(vp)->r_acache != NULL) { 4318 e.error = nfs4_validate_caches(vp, cr); 4319 if (e.error) 4320 return (e.error); 4321 } 4322 4323 rp = VTOR4(vp); 4324 if (vp->v_type == VDIR) 4325 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4326 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4327 else 4328 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4329 ACCESS4_EXECUTE; 4330 recov_state.rs_flags = 0; 4331 recov_state.rs_num_retry_despite_err = 0; 4332 4333 cred = cr; 4334 /* 4335 * ncr and ncrfree both initially 4336 * point to the memory area returned 4337 * by crnetadjust(); 4338 * ncrfree not NULL when exiting means 4339 * that we need to release it 4340 */ 4341 ncr = crnetadjust(cred); 4342 ncrfree = ncr; 4343 4344 tryagain: 4345 cacc = nfs4_access_check(rp, acc, cred); 4346 if (cacc == NFS4_ACCESS_ALLOWED) { 4347 if (ncrfree != NULL) 4348 crfree(ncrfree); 4349 return (0); 4350 } 4351 if (cacc == NFS4_ACCESS_DENIED) { 4352 /* 4353 * If the cred can be adjusted, try again 4354 * with the new cred. 4355 */ 4356 if (ncr != NULL) { 4357 cred = ncr; 4358 ncr = NULL; 4359 goto tryagain; 4360 } 4361 if (ncrfree != NULL) 4362 crfree(ncrfree); 4363 return (EACCES); 4364 } 4365 4366 recov_retry: 4367 /* 4368 * Don't take with r_statev4_lock here. r_deleg_type could 4369 * change as soon as lock is released. Since it is an int, 4370 * there is no atomicity issue. 4371 */ 4372 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4373 num_ops = do_getattr ? 3 : 2; 4374 4375 args.ctag = TAG_ACCESS; 4376 4377 args.array_len = num_ops; 4378 args.array = argop; 4379 4380 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4381 &recov_state, NULL)) { 4382 if (ncrfree != NULL) 4383 crfree(ncrfree); 4384 return (e.error); 4385 } 4386 4387 /* putfh target fh */ 4388 argop[0].argop = OP_CPUTFH; 4389 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4390 4391 /* access */ 4392 argop[1].argop = OP_ACCESS; 4393 argop[1].nfs_argop4_u.opaccess.access = argacc; 4394 4395 /* getattr */ 4396 if (do_getattr) { 4397 argop[2].argop = OP_GETATTR; 4398 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4399 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4400 } 4401 4402 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4403 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4404 rnode4info(VTOR4(vp)))); 4405 4406 doqueue = 1; 4407 t = gethrtime(); 4408 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4409 rpc_error = e.error; 4410 4411 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4412 if (needrecov) { 4413 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4414 "nfs4_access: initiating recovery\n")); 4415 4416 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4417 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) { 4418 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4419 &recov_state, needrecov); 4420 if (!e.error) 4421 (void) xdr_free(xdr_COMPOUND4res_clnt, 4422 (caddr_t)&res); 4423 goto recov_retry; 4424 } 4425 } 4426 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4427 4428 if (e.error) 4429 goto out; 4430 4431 if (res.status) { 4432 e.error = geterrno4(res.status); 4433 /* 4434 * This might generate over the wire calls throught 4435 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4436 * here to avoid a deadlock. 4437 */ 4438 nfs4_purge_stale_fh(e.error, vp, cr); 4439 goto out; 4440 } 4441 resop = &res.array[1]; /* access res */ 4442 4443 resacc = resop->nfs_resop4_u.opaccess.access; 4444 4445 if (do_getattr) { 4446 resop++; /* getattr res */ 4447 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4448 t, cr, FALSE, NULL); 4449 } 4450 4451 if (!e.error) { 4452 nfs4_access_cache(rp, argacc, resacc, cred); 4453 /* 4454 * we just cached results with cred; if cred is the 4455 * adjusted credentials from crnetadjust, we do not want 4456 * to release them before exiting: hence setting ncrfree 4457 * to NULL 4458 */ 4459 if (cred != cr) 4460 ncrfree = NULL; 4461 /* XXX check the supported bits too? */ 4462 if ((acc & resacc) != acc) { 4463 /* 4464 * The following code implements the semantic 4465 * that a setuid root program has *at least* the 4466 * permissions of the user that is running the 4467 * program. See rfs3call() for more portions 4468 * of the implementation of this functionality. 4469 */ 4470 /* XXX-LP */ 4471 if (ncr != NULL) { 4472 (void) xdr_free(xdr_COMPOUND4res_clnt, 4473 (caddr_t)&res); 4474 cred = ncr; 4475 ncr = NULL; 4476 goto tryagain; 4477 } 4478 e.error = EACCES; 4479 } 4480 } 4481 4482 out: 4483 if (!rpc_error) 4484 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4485 4486 if (ncrfree != NULL) 4487 crfree(ncrfree); 4488 4489 return (e.error); 4490 } 4491 4492 /* ARGSUSED */ 4493 static int 4494 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct) 4495 { 4496 COMPOUND4args_clnt args; 4497 COMPOUND4res_clnt res; 4498 int doqueue; 4499 rnode4_t *rp; 4500 nfs_argop4 argop[3]; 4501 nfs_resop4 *resop; 4502 READLINK4res *lr_res; 4503 nfs4_ga_res_t *garp; 4504 uint_t len; 4505 char *linkdata; 4506 bool_t needrecov = FALSE; 4507 nfs4_recov_state_t recov_state; 4508 hrtime_t t; 4509 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4510 4511 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4512 return (EIO); 4513 /* 4514 * Can't readlink anything other than a symbolic link. 4515 */ 4516 if (vp->v_type != VLNK) 4517 return (EINVAL); 4518 4519 rp = VTOR4(vp); 4520 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4521 e.error = nfs4_validate_caches(vp, cr); 4522 if (e.error) 4523 return (e.error); 4524 mutex_enter(&rp->r_statelock); 4525 if (rp->r_symlink.contents != NULL) { 4526 e.error = uiomove(rp->r_symlink.contents, 4527 rp->r_symlink.len, UIO_READ, uiop); 4528 mutex_exit(&rp->r_statelock); 4529 return (e.error); 4530 } 4531 mutex_exit(&rp->r_statelock); 4532 } 4533 recov_state.rs_flags = 0; 4534 recov_state.rs_num_retry_despite_err = 0; 4535 4536 recov_retry: 4537 args.array_len = 3; 4538 args.array = argop; 4539 args.ctag = TAG_READLINK; 4540 4541 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4542 if (e.error) { 4543 return (e.error); 4544 } 4545 4546 /* 0. putfh symlink fh */ 4547 argop[0].argop = OP_CPUTFH; 4548 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4549 4550 /* 1. readlink */ 4551 argop[1].argop = OP_READLINK; 4552 4553 /* 2. getattr */ 4554 argop[2].argop = OP_GETATTR; 4555 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4556 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4557 4558 doqueue = 1; 4559 4560 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4561 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4562 rnode4info(VTOR4(vp)))); 4563 4564 t = gethrtime(); 4565 4566 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4567 4568 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4569 if (needrecov) { 4570 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4571 "nfs4_readlink: initiating recovery\n")); 4572 4573 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4574 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) { 4575 if (!e.error) 4576 (void) xdr_free(xdr_COMPOUND4res_clnt, 4577 (caddr_t)&res); 4578 4579 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4580 needrecov); 4581 goto recov_retry; 4582 } 4583 } 4584 4585 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4586 4587 if (e.error) 4588 return (e.error); 4589 4590 /* 4591 * There is an path in the code below which calls 4592 * nfs4_purge_stale_fh(), which may generate otw calls through 4593 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4594 * here to avoid nfs4_start_op() deadlock. 4595 */ 4596 4597 if (res.status && (res.array_len < args.array_len)) { 4598 /* 4599 * either Putfh or Link failed 4600 */ 4601 e.error = geterrno4(res.status); 4602 nfs4_purge_stale_fh(e.error, vp, cr); 4603 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4604 return (e.error); 4605 } 4606 4607 resop = &res.array[1]; /* readlink res */ 4608 lr_res = &resop->nfs_resop4_u.opreadlink; 4609 4610 /* 4611 * treat symlink names as data 4612 */ 4613 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL); 4614 if (linkdata != NULL) { 4615 int uio_len = len - 1; 4616 /* len includes null byte, which we won't uiomove */ 4617 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4618 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4619 mutex_enter(&rp->r_statelock); 4620 if (rp->r_symlink.contents == NULL) { 4621 rp->r_symlink.contents = linkdata; 4622 rp->r_symlink.len = uio_len; 4623 rp->r_symlink.size = len; 4624 mutex_exit(&rp->r_statelock); 4625 } else { 4626 mutex_exit(&rp->r_statelock); 4627 kmem_free(linkdata, len); 4628 } 4629 } else { 4630 kmem_free(linkdata, len); 4631 } 4632 } 4633 if (res.status == NFS4_OK) { 4634 resop++; /* getattr res */ 4635 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4636 } 4637 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4638 4639 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4640 4641 /* 4642 * The over the wire error for attempting to readlink something 4643 * other than a symbolic link is ENXIO. However, we need to 4644 * return EINVAL instead of ENXIO, so we map it here. 4645 */ 4646 return (e.error == ENXIO ? EINVAL : e.error); 4647 } 4648 4649 /* 4650 * Flush local dirty pages to stable storage on the server. 4651 * 4652 * If FNODSYNC is specified, then there is nothing to do because 4653 * metadata changes are not cached on the client before being 4654 * sent to the server. 4655 */ 4656 /* ARGSUSED */ 4657 static int 4658 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 4659 { 4660 int error; 4661 4662 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4663 return (0); 4664 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4665 return (EIO); 4666 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4667 if (!error) 4668 error = VTOR4(vp)->r_error; 4669 return (error); 4670 } 4671 4672 /* 4673 * Weirdness: if the file was removed or the target of a rename 4674 * operation while it was open, it got renamed instead. Here we 4675 * remove the renamed file. 4676 */ 4677 /* ARGSUSED */ 4678 void 4679 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4680 { 4681 rnode4_t *rp; 4682 4683 ASSERT(vp != DNLC_NO_VNODE); 4684 4685 rp = VTOR4(vp); 4686 4687 if (IS_SHADOW(vp, rp)) { 4688 sv_inactive(vp); 4689 return; 4690 } 4691 4692 /* 4693 * If this is coming from the wrong zone, we let someone in the right 4694 * zone take care of it asynchronously. We can get here due to 4695 * VN_RELE() being called from pageout() or fsflush(). This call may 4696 * potentially turn into an expensive no-op if, for instance, v_count 4697 * gets incremented in the meantime, but it's still correct. 4698 */ 4699 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4700 nfs4_async_inactive(vp, cr); 4701 return; 4702 } 4703 4704 /* 4705 * Some of the cleanup steps might require over-the-wire 4706 * operations. Since VOP_INACTIVE can get called as a result of 4707 * other over-the-wire operations (e.g., an attribute cache update 4708 * can lead to a DNLC purge), doing those steps now would lead to a 4709 * nested call to the recovery framework, which can deadlock. So 4710 * do any over-the-wire cleanups asynchronously, in a separate 4711 * thread. 4712 */ 4713 4714 mutex_enter(&rp->r_os_lock); 4715 mutex_enter(&rp->r_statelock); 4716 mutex_enter(&rp->r_statev4_lock); 4717 4718 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4719 mutex_exit(&rp->r_statev4_lock); 4720 mutex_exit(&rp->r_statelock); 4721 mutex_exit(&rp->r_os_lock); 4722 nfs4_async_inactive(vp, cr); 4723 return; 4724 } 4725 4726 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4727 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4728 mutex_exit(&rp->r_statev4_lock); 4729 mutex_exit(&rp->r_statelock); 4730 mutex_exit(&rp->r_os_lock); 4731 nfs4_async_inactive(vp, cr); 4732 return; 4733 } 4734 4735 if (rp->r_unldvp != NULL) { 4736 mutex_exit(&rp->r_statev4_lock); 4737 mutex_exit(&rp->r_statelock); 4738 mutex_exit(&rp->r_os_lock); 4739 nfs4_async_inactive(vp, cr); 4740 return; 4741 } 4742 mutex_exit(&rp->r_statev4_lock); 4743 mutex_exit(&rp->r_statelock); 4744 mutex_exit(&rp->r_os_lock); 4745 4746 rp4_addfree(rp, cr); 4747 } 4748 4749 /* 4750 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4751 * various bits of state. The caller must not refer to vp after this call. 4752 */ 4753 4754 void 4755 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4756 { 4757 rnode4_t *rp = VTOR4(vp); 4758 nfs4_recov_state_t recov_state; 4759 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4760 vnode_t *unldvp; 4761 char *unlname; 4762 cred_t *unlcred; 4763 COMPOUND4args_clnt args; 4764 COMPOUND4res_clnt res, *resp; 4765 nfs_argop4 argop[2]; 4766 int doqueue; 4767 #ifdef DEBUG 4768 char *name; 4769 #endif 4770 4771 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4772 ASSERT(!IS_SHADOW(vp, rp)); 4773 4774 #ifdef DEBUG 4775 name = fn_name(VTOSV(vp)->sv_name); 4776 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4777 "release vnode %s", name)); 4778 kmem_free(name, MAXNAMELEN); 4779 #endif 4780 4781 if (vp->v_type == VREG) { 4782 bool_t recov_failed = FALSE; 4783 4784 e.error = nfs4close_all(vp, cr); 4785 if (e.error) { 4786 /* Check to see if recovery failed */ 4787 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4788 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4789 recov_failed = TRUE; 4790 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4791 if (!recov_failed) { 4792 mutex_enter(&rp->r_statelock); 4793 if (rp->r_flags & R4RECOVERR) 4794 recov_failed = TRUE; 4795 mutex_exit(&rp->r_statelock); 4796 } 4797 if (recov_failed) { 4798 NFS4_DEBUG(nfs4_client_recov_debug, 4799 (CE_NOTE, "nfs4_inactive_otw: " 4800 "close failed (recovery failure)")); 4801 } 4802 } 4803 } 4804 4805 redo: 4806 if (rp->r_unldvp == NULL) { 4807 rp4_addfree(rp, cr); 4808 return; 4809 } 4810 4811 /* 4812 * Save the vnode pointer for the directory where the 4813 * unlinked-open file got renamed, then set it to NULL 4814 * to prevent another thread from getting here before 4815 * we're done with the remove. While we have the 4816 * statelock, make local copies of the pertinent rnode 4817 * fields. If we weren't to do this in an atomic way, the 4818 * the unl* fields could become inconsistent with respect 4819 * to each other due to a race condition between this 4820 * code and nfs_remove(). See bug report 1034328. 4821 */ 4822 mutex_enter(&rp->r_statelock); 4823 if (rp->r_unldvp == NULL) { 4824 mutex_exit(&rp->r_statelock); 4825 rp4_addfree(rp, cr); 4826 return; 4827 } 4828 4829 unldvp = rp->r_unldvp; 4830 rp->r_unldvp = NULL; 4831 unlname = rp->r_unlname; 4832 rp->r_unlname = NULL; 4833 unlcred = rp->r_unlcred; 4834 rp->r_unlcred = NULL; 4835 mutex_exit(&rp->r_statelock); 4836 4837 /* 4838 * If there are any dirty pages left, then flush 4839 * them. This is unfortunate because they just 4840 * may get thrown away during the remove operation, 4841 * but we have to do this for correctness. 4842 */ 4843 if (nfs4_has_pages(vp) && 4844 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4845 ASSERT(vp->v_type != VCHR); 4846 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL); 4847 if (e.error) { 4848 mutex_enter(&rp->r_statelock); 4849 if (!rp->r_error) 4850 rp->r_error = e.error; 4851 mutex_exit(&rp->r_statelock); 4852 } 4853 } 4854 4855 recov_state.rs_flags = 0; 4856 recov_state.rs_num_retry_despite_err = 0; 4857 recov_retry_remove: 4858 /* 4859 * Do the remove operation on the renamed file 4860 */ 4861 args.ctag = TAG_INACTIVE; 4862 4863 /* 4864 * Remove ops: putfh dir; remove 4865 */ 4866 args.array_len = 2; 4867 args.array = argop; 4868 4869 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4870 if (e.error) { 4871 kmem_free(unlname, MAXNAMELEN); 4872 crfree(unlcred); 4873 VN_RELE(unldvp); 4874 /* 4875 * Try again; this time around r_unldvp will be NULL, so we'll 4876 * just call rp4_addfree() and return. 4877 */ 4878 goto redo; 4879 } 4880 4881 /* putfh directory */ 4882 argop[0].argop = OP_CPUTFH; 4883 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4884 4885 /* remove */ 4886 argop[1].argop = OP_CREMOVE; 4887 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4888 4889 doqueue = 1; 4890 resp = &res; 4891 4892 #if 0 /* notyet */ 4893 /* 4894 * Can't do this yet. We may be being called from 4895 * dnlc_purge_XXX while that routine is holding a 4896 * mutex lock to the nc_rele list. The calls to 4897 * nfs3_cache_wcc_data may result in calls to 4898 * dnlc_purge_XXX. This will result in a deadlock. 4899 */ 4900 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4901 if (e.error) { 4902 PURGE_ATTRCACHE4(unldvp); 4903 resp = NULL; 4904 } else if (res.status) { 4905 e.error = geterrno4(res.status); 4906 PURGE_ATTRCACHE4(unldvp); 4907 /* 4908 * This code is inactive right now 4909 * but if made active there should 4910 * be a nfs4_end_op() call before 4911 * nfs4_purge_stale_fh to avoid start_op() 4912 * deadlock. See BugId: 4948726 4913 */ 4914 nfs4_purge_stale_fh(error, unldvp, cr); 4915 } else { 4916 nfs_resop4 *resop; 4917 REMOVE4res *rm_res; 4918 4919 resop = &res.array[1]; 4920 rm_res = &resop->nfs_resop4_u.opremove; 4921 /* 4922 * Update directory cache attribute, 4923 * readdir and dnlc caches. 4924 */ 4925 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4926 } 4927 #else 4928 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4929 4930 PURGE_ATTRCACHE4(unldvp); 4931 #endif 4932 4933 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4934 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4935 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 4936 if (!e.error) 4937 (void) xdr_free(xdr_COMPOUND4res_clnt, 4938 (caddr_t)&res); 4939 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4940 &recov_state, TRUE); 4941 goto recov_retry_remove; 4942 } 4943 } 4944 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4945 4946 /* 4947 * Release stuff held for the remove 4948 */ 4949 VN_RELE(unldvp); 4950 if (!e.error && resp) 4951 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4952 4953 kmem_free(unlname, MAXNAMELEN); 4954 crfree(unlcred); 4955 goto redo; 4956 } 4957 4958 /* 4959 * Remote file system operations having to do with directory manipulation. 4960 */ 4961 /* ARGSUSED3 */ 4962 int 4963 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4964 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 4965 int *direntflags, pathname_t *realpnp) 4966 { 4967 int error; 4968 vnode_t *vp, *avp = NULL; 4969 rnode4_t *drp; 4970 4971 *vpp = NULL; 4972 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4973 return (EPERM); 4974 /* 4975 * if LOOKUP_XATTR, must replace dvp (object) with 4976 * object's attrdir before continuing with lookup 4977 */ 4978 if (flags & LOOKUP_XATTR) { 4979 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4980 if (error) 4981 return (error); 4982 4983 dvp = avp; 4984 4985 /* 4986 * If lookup is for "", just return dvp now. The attrdir 4987 * has already been activated (from nfs4lookup_xattr), and 4988 * the caller will RELE the original dvp -- not 4989 * the attrdir. So, set vpp and return. 4990 * Currently, when the LOOKUP_XATTR flag is 4991 * passed to VOP_LOOKUP, the name is always empty, and 4992 * shortcircuiting here avoids 3 unneeded lock/unlock 4993 * pairs. 4994 * 4995 * If a non-empty name was provided, then it is the 4996 * attribute name, and it will be looked up below. 4997 */ 4998 if (*nm == '\0') { 4999 *vpp = dvp; 5000 return (0); 5001 } 5002 5003 /* 5004 * The vfs layer never sends a name when asking for the 5005 * attrdir, so we should never get here (unless of course 5006 * name is passed at some time in future -- at which time 5007 * we'll blow up here). 5008 */ 5009 ASSERT(0); 5010 } 5011 5012 drp = VTOR4(dvp); 5013 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5014 return (EINTR); 5015 5016 error = nfs4lookup(dvp, nm, vpp, cr, 0); 5017 nfs_rw_exit(&drp->r_rwlock); 5018 5019 /* 5020 * If vnode is a device, create special vnode. 5021 */ 5022 if (!error && ISVDEV((*vpp)->v_type)) { 5023 vp = *vpp; 5024 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 5025 VN_RELE(vp); 5026 } 5027 5028 return (error); 5029 } 5030 5031 /* ARGSUSED */ 5032 static int 5033 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 5034 { 5035 int error; 5036 rnode4_t *drp; 5037 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 5038 mntinfo4_t *mi; 5039 5040 mi = VTOMI4(dvp); 5041 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) && 5042 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS)) 5043 return (EINVAL); 5044 5045 drp = VTOR4(dvp); 5046 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5047 return (EINTR); 5048 5049 mutex_enter(&drp->r_statelock); 5050 /* 5051 * If the server doesn't support xattrs just return EINVAL 5052 */ 5053 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 5054 mutex_exit(&drp->r_statelock); 5055 nfs_rw_exit(&drp->r_rwlock); 5056 return (EINVAL); 5057 } 5058 5059 /* 5060 * If there is a cached xattr directory entry, 5061 * use it as long as the attributes are valid. If the 5062 * attributes are not valid, take the simple approach and 5063 * free the cached value and re-fetch a new value. 5064 * 5065 * We don't negative entry cache for now, if we did we 5066 * would need to check if the file has changed on every 5067 * lookup. But xattrs don't exist very often and failing 5068 * an openattr is not much more expensive than and NVERIFY or GETATTR 5069 * so do an openattr over the wire for now. 5070 */ 5071 if (drp->r_xattr_dir != NULL) { 5072 if (ATTRCACHE4_VALID(dvp)) { 5073 VN_HOLD(drp->r_xattr_dir); 5074 *vpp = drp->r_xattr_dir; 5075 mutex_exit(&drp->r_statelock); 5076 nfs_rw_exit(&drp->r_rwlock); 5077 return (0); 5078 } 5079 VN_RELE(drp->r_xattr_dir); 5080 drp->r_xattr_dir = NULL; 5081 } 5082 mutex_exit(&drp->r_statelock); 5083 5084 error = nfs4openattr(dvp, vpp, cflag, cr); 5085 5086 nfs_rw_exit(&drp->r_rwlock); 5087 5088 return (error); 5089 } 5090 5091 static int 5092 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 5093 { 5094 int error; 5095 rnode4_t *drp; 5096 5097 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5098 5099 /* 5100 * If lookup is for "", just return dvp. Don't need 5101 * to send it over the wire, look it up in the dnlc, 5102 * or perform any access checks. 5103 */ 5104 if (*nm == '\0') { 5105 VN_HOLD(dvp); 5106 *vpp = dvp; 5107 return (0); 5108 } 5109 5110 /* 5111 * Can't do lookups in non-directories. 5112 */ 5113 if (dvp->v_type != VDIR) 5114 return (ENOTDIR); 5115 5116 /* 5117 * If lookup is for ".", just return dvp. Don't need 5118 * to send it over the wire or look it up in the dnlc, 5119 * just need to check access. 5120 */ 5121 if (nm[0] == '.' && nm[1] == '\0') { 5122 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5123 if (error) 5124 return (error); 5125 VN_HOLD(dvp); 5126 *vpp = dvp; 5127 return (0); 5128 } 5129 5130 drp = VTOR4(dvp); 5131 if (!(drp->r_flags & R4LOOKUP)) { 5132 mutex_enter(&drp->r_statelock); 5133 drp->r_flags |= R4LOOKUP; 5134 mutex_exit(&drp->r_statelock); 5135 } 5136 5137 *vpp = NULL; 5138 /* 5139 * Lookup this name in the DNLC. If there is no entry 5140 * lookup over the wire. 5141 */ 5142 if (!skipdnlc) 5143 *vpp = dnlc_lookup(dvp, nm); 5144 if (*vpp == NULL) { 5145 /* 5146 * We need to go over the wire to lookup the name. 5147 */ 5148 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5149 } 5150 5151 /* 5152 * We hit on the dnlc 5153 */ 5154 if (*vpp != DNLC_NO_VNODE || 5155 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5156 /* 5157 * But our attrs may not be valid. 5158 */ 5159 if (ATTRCACHE4_VALID(dvp)) { 5160 error = nfs4_waitfor_purge_complete(dvp); 5161 if (error) { 5162 VN_RELE(*vpp); 5163 *vpp = NULL; 5164 return (error); 5165 } 5166 5167 /* 5168 * If after the purge completes, check to make sure 5169 * our attrs are still valid. 5170 */ 5171 if (ATTRCACHE4_VALID(dvp)) { 5172 /* 5173 * If we waited for a purge we may have 5174 * lost our vnode so look it up again. 5175 */ 5176 VN_RELE(*vpp); 5177 *vpp = dnlc_lookup(dvp, nm); 5178 if (*vpp == NULL) 5179 return (nfs4lookupnew_otw(dvp, 5180 nm, vpp, cr)); 5181 5182 /* 5183 * The access cache should almost always hit 5184 */ 5185 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5186 5187 if (error) { 5188 VN_RELE(*vpp); 5189 *vpp = NULL; 5190 return (error); 5191 } 5192 if (*vpp == DNLC_NO_VNODE) { 5193 VN_RELE(*vpp); 5194 *vpp = NULL; 5195 return (ENOENT); 5196 } 5197 return (0); 5198 } 5199 } 5200 } 5201 5202 ASSERT(*vpp != NULL); 5203 5204 /* 5205 * We may have gotten here we have one of the following cases: 5206 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5207 * need to validate them. 5208 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5209 * must validate. 5210 * 5211 * Go to the server and check if the directory has changed, if 5212 * it hasn't we are done and can use the dnlc entry. 5213 */ 5214 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5215 } 5216 5217 /* 5218 * Go to the server and check if the directory has changed, if 5219 * it hasn't we are done and can use the dnlc entry. If it 5220 * has changed we get a new copy of its attributes and check 5221 * the access for VEXEC, then relookup the filename and 5222 * get its filehandle and attributes. 5223 * 5224 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5225 * if the NVERIFY failed we must 5226 * purge the caches 5227 * cache new attributes (will set r_time_attr_inval) 5228 * cache new access 5229 * recheck VEXEC access 5230 * add name to dnlc, possibly negative 5231 * if LOOKUP succeeded 5232 * cache new attributes 5233 * else 5234 * set a new r_time_attr_inval for dvp 5235 * check to make sure we have access 5236 * 5237 * The vpp returned is the vnode passed in if the directory is valid, 5238 * a new vnode if successful lookup, or NULL on error. 5239 */ 5240 static int 5241 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5242 { 5243 COMPOUND4args_clnt args; 5244 COMPOUND4res_clnt res; 5245 fattr4 *ver_fattr; 5246 fattr4_change dchange; 5247 int32_t *ptr; 5248 int argoplist_size = 7 * sizeof (nfs_argop4); 5249 nfs_argop4 *argop; 5250 int doqueue; 5251 mntinfo4_t *mi; 5252 nfs4_recov_state_t recov_state; 5253 hrtime_t t; 5254 int isdotdot; 5255 vnode_t *nvp; 5256 nfs_fh4 *fhp; 5257 nfs4_sharedfh_t *sfhp; 5258 nfs4_access_type_t cacc; 5259 rnode4_t *nrp; 5260 rnode4_t *drp = VTOR4(dvp); 5261 nfs4_ga_res_t *garp = NULL; 5262 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5263 5264 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5265 ASSERT(nm != NULL); 5266 ASSERT(nm[0] != '\0'); 5267 ASSERT(dvp->v_type == VDIR); 5268 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5269 ASSERT(*vpp != NULL); 5270 5271 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5272 isdotdot = 1; 5273 args.ctag = TAG_LOOKUP_VPARENT; 5274 } else { 5275 /* 5276 * If dvp were a stub, it should have triggered and caused 5277 * a mount for us to get this far. 5278 */ 5279 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5280 5281 isdotdot = 0; 5282 args.ctag = TAG_LOOKUP_VALID; 5283 } 5284 5285 mi = VTOMI4(dvp); 5286 recov_state.rs_flags = 0; 5287 recov_state.rs_num_retry_despite_err = 0; 5288 5289 nvp = NULL; 5290 5291 /* Save the original mount point security information */ 5292 (void) save_mnt_secinfo(mi->mi_curr_serv); 5293 5294 recov_retry: 5295 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5296 &recov_state, NULL); 5297 if (e.error) { 5298 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5299 VN_RELE(*vpp); 5300 *vpp = NULL; 5301 return (e.error); 5302 } 5303 5304 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5305 5306 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5307 args.array_len = 7; 5308 args.array = argop; 5309 5310 /* 0. putfh file */ 5311 argop[0].argop = OP_CPUTFH; 5312 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5313 5314 /* 1. nverify the change info */ 5315 argop[1].argop = OP_NVERIFY; 5316 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5317 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5318 ver_fattr->attrlist4 = (char *)&dchange; 5319 ptr = (int32_t *)&dchange; 5320 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5321 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5322 5323 /* 2. getattr directory */ 5324 argop[2].argop = OP_GETATTR; 5325 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5326 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5327 5328 /* 3. access directory */ 5329 argop[3].argop = OP_ACCESS; 5330 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5331 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5332 5333 /* 4. lookup name */ 5334 if (isdotdot) { 5335 argop[4].argop = OP_LOOKUPP; 5336 } else { 5337 argop[4].argop = OP_CLOOKUP; 5338 argop[4].nfs_argop4_u.opclookup.cname = nm; 5339 } 5340 5341 /* 5. resulting file handle */ 5342 argop[5].argop = OP_GETFH; 5343 5344 /* 6. resulting file attributes */ 5345 argop[6].argop = OP_GETATTR; 5346 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5347 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5348 5349 doqueue = 1; 5350 t = gethrtime(); 5351 5352 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5353 5354 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5355 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5356 if (e.error != 0 && *vpp != NULL) 5357 VN_RELE(*vpp); 5358 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5359 &recov_state, FALSE); 5360 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5361 kmem_free(argop, argoplist_size); 5362 return (e.error); 5363 } 5364 5365 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5366 /* 5367 * For WRONGSEC of a non-dotdot case, send secinfo directly 5368 * from this thread, do not go thru the recovery thread since 5369 * we need the nm information. 5370 * 5371 * Not doing dotdot case because there is no specification 5372 * for (PUTFH, SECINFO "..") yet. 5373 */ 5374 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5375 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5376 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5377 &recov_state, FALSE); 5378 else 5379 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5380 &recov_state, TRUE); 5381 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5382 kmem_free(argop, argoplist_size); 5383 if (!e.error) 5384 goto recov_retry; 5385 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5386 VN_RELE(*vpp); 5387 *vpp = NULL; 5388 return (e.error); 5389 } 5390 5391 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5392 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5393 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5394 &recov_state, TRUE); 5395 5396 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5397 kmem_free(argop, argoplist_size); 5398 goto recov_retry; 5399 } 5400 } 5401 5402 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5403 5404 if (e.error || res.array_len == 0) { 5405 /* 5406 * If e.error isn't set, then reply has no ops (or we couldn't 5407 * be here). The only legal way to reply without an op array 5408 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5409 * be in the reply for all other status values. 5410 * 5411 * For valid replies without an ops array, return ENOTSUP 5412 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5413 * return EIO -- don't trust status. 5414 */ 5415 if (e.error == 0) 5416 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5417 ENOTSUP : EIO; 5418 VN_RELE(*vpp); 5419 *vpp = NULL; 5420 kmem_free(argop, argoplist_size); 5421 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5422 return (e.error); 5423 } 5424 5425 if (res.status != NFS4ERR_SAME) { 5426 e.error = geterrno4(res.status); 5427 5428 /* 5429 * The NVERIFY "failed" so the directory has changed 5430 * First make sure PUTFH succeeded and NVERIFY "failed" 5431 * cleanly. 5432 */ 5433 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5434 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5435 nfs4_purge_stale_fh(e.error, dvp, cr); 5436 VN_RELE(*vpp); 5437 *vpp = NULL; 5438 goto exit; 5439 } 5440 5441 /* 5442 * We know the NVERIFY "failed" so we must: 5443 * purge the caches (access and indirectly dnlc if needed) 5444 */ 5445 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5446 5447 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5448 nfs4_purge_stale_fh(e.error, dvp, cr); 5449 VN_RELE(*vpp); 5450 *vpp = NULL; 5451 goto exit; 5452 } 5453 5454 /* 5455 * Install new cached attributes for the directory 5456 */ 5457 nfs4_attr_cache(dvp, 5458 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5459 t, cr, FALSE, NULL); 5460 5461 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5462 nfs4_purge_stale_fh(e.error, dvp, cr); 5463 VN_RELE(*vpp); 5464 *vpp = NULL; 5465 e.error = geterrno4(res.status); 5466 goto exit; 5467 } 5468 5469 /* 5470 * Now we know the directory is valid, 5471 * cache new directory access 5472 */ 5473 nfs4_access_cache(drp, 5474 args.array[3].nfs_argop4_u.opaccess.access, 5475 res.array[3].nfs_resop4_u.opaccess.access, cr); 5476 5477 /* 5478 * recheck VEXEC access 5479 */ 5480 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5481 if (cacc != NFS4_ACCESS_ALLOWED) { 5482 /* 5483 * Directory permissions might have been revoked 5484 */ 5485 if (cacc == NFS4_ACCESS_DENIED) { 5486 e.error = EACCES; 5487 VN_RELE(*vpp); 5488 *vpp = NULL; 5489 goto exit; 5490 } 5491 5492 /* 5493 * Somehow we must not have asked for enough 5494 * so try a singleton ACCESS, should never happen. 5495 */ 5496 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5497 if (e.error) { 5498 VN_RELE(*vpp); 5499 *vpp = NULL; 5500 goto exit; 5501 } 5502 } 5503 5504 e.error = geterrno4(res.status); 5505 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5506 /* 5507 * The lookup failed, probably no entry 5508 */ 5509 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5510 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5511 } else { 5512 /* 5513 * Might be some other error, so remove 5514 * the dnlc entry to make sure we start all 5515 * over again, next time. 5516 */ 5517 dnlc_remove(dvp, nm); 5518 } 5519 VN_RELE(*vpp); 5520 *vpp = NULL; 5521 goto exit; 5522 } 5523 5524 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5525 /* 5526 * The file exists but we can't get its fh for 5527 * some unknown reason. Remove it from the dnlc 5528 * and error out to be safe. 5529 */ 5530 dnlc_remove(dvp, nm); 5531 VN_RELE(*vpp); 5532 *vpp = NULL; 5533 goto exit; 5534 } 5535 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5536 if (fhp->nfs_fh4_len == 0) { 5537 /* 5538 * The file exists but a bogus fh 5539 * some unknown reason. Remove it from the dnlc 5540 * and error out to be safe. 5541 */ 5542 e.error = ENOENT; 5543 dnlc_remove(dvp, nm); 5544 VN_RELE(*vpp); 5545 *vpp = NULL; 5546 goto exit; 5547 } 5548 sfhp = sfh4_get(fhp, mi); 5549 5550 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5551 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5552 5553 /* 5554 * Make the new rnode 5555 */ 5556 if (isdotdot) { 5557 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5558 if (e.error) { 5559 sfh4_rele(&sfhp); 5560 VN_RELE(*vpp); 5561 *vpp = NULL; 5562 goto exit; 5563 } 5564 /* 5565 * XXX if nfs4_make_dotdot uses an existing rnode 5566 * XXX it doesn't update the attributes. 5567 * XXX for now just save them again to save an OTW 5568 */ 5569 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5570 } else { 5571 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5572 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 5573 /* 5574 * If v_type == VNON, then garp was NULL because 5575 * the last op in the compound failed and makenfs4node 5576 * could not find the vnode for sfhp. It created 5577 * a new vnode, so we have nothing to purge here. 5578 */ 5579 if (nvp->v_type == VNON) { 5580 vattr_t vattr; 5581 5582 vattr.va_mask = AT_TYPE; 5583 /* 5584 * N.B. We've already called nfs4_end_fop above. 5585 */ 5586 e.error = nfs4getattr(nvp, &vattr, cr); 5587 if (e.error) { 5588 sfh4_rele(&sfhp); 5589 VN_RELE(*vpp); 5590 *vpp = NULL; 5591 VN_RELE(nvp); 5592 goto exit; 5593 } 5594 nvp->v_type = vattr.va_type; 5595 } 5596 } 5597 sfh4_rele(&sfhp); 5598 5599 nrp = VTOR4(nvp); 5600 mutex_enter(&nrp->r_statev4_lock); 5601 if (!nrp->created_v4) { 5602 mutex_exit(&nrp->r_statev4_lock); 5603 dnlc_update(dvp, nm, nvp); 5604 } else 5605 mutex_exit(&nrp->r_statev4_lock); 5606 5607 VN_RELE(*vpp); 5608 *vpp = nvp; 5609 } else { 5610 hrtime_t now; 5611 hrtime_t delta = 0; 5612 5613 e.error = 0; 5614 5615 /* 5616 * Because the NVERIFY "succeeded" we know that the 5617 * directory attributes are still valid 5618 * so update r_time_attr_inval 5619 */ 5620 now = gethrtime(); 5621 mutex_enter(&drp->r_statelock); 5622 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5623 delta = now - drp->r_time_attr_saved; 5624 if (delta < mi->mi_acdirmin) 5625 delta = mi->mi_acdirmin; 5626 else if (delta > mi->mi_acdirmax) 5627 delta = mi->mi_acdirmax; 5628 } 5629 drp->r_time_attr_inval = now + delta; 5630 mutex_exit(&drp->r_statelock); 5631 dnlc_update(dvp, nm, *vpp); 5632 5633 /* 5634 * Even though we have a valid directory attr cache 5635 * and dnlc entry, we may not have access. 5636 * This should almost always hit the cache. 5637 */ 5638 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5639 if (e.error) { 5640 VN_RELE(*vpp); 5641 *vpp = NULL; 5642 } 5643 5644 if (*vpp == DNLC_NO_VNODE) { 5645 VN_RELE(*vpp); 5646 *vpp = NULL; 5647 e.error = ENOENT; 5648 } 5649 } 5650 5651 exit: 5652 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5653 kmem_free(argop, argoplist_size); 5654 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5655 return (e.error); 5656 } 5657 5658 /* 5659 * We need to go over the wire to lookup the name, but 5660 * while we are there verify the directory has not 5661 * changed but if it has, get new attributes and check access 5662 * 5663 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5664 * NVERIFY GETATTR ACCESS 5665 * 5666 * With the results: 5667 * if the NVERIFY failed we must purge the caches, add new attributes, 5668 * and cache new access. 5669 * set a new r_time_attr_inval 5670 * add name to dnlc, possibly negative 5671 * if LOOKUP succeeded 5672 * cache new attributes 5673 */ 5674 static int 5675 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5676 { 5677 COMPOUND4args_clnt args; 5678 COMPOUND4res_clnt res; 5679 fattr4 *ver_fattr; 5680 fattr4_change dchange; 5681 int32_t *ptr; 5682 nfs4_ga_res_t *garp = NULL; 5683 int argoplist_size = 9 * sizeof (nfs_argop4); 5684 nfs_argop4 *argop; 5685 int doqueue; 5686 mntinfo4_t *mi; 5687 nfs4_recov_state_t recov_state; 5688 hrtime_t t; 5689 int isdotdot; 5690 vnode_t *nvp; 5691 nfs_fh4 *fhp; 5692 nfs4_sharedfh_t *sfhp; 5693 nfs4_access_type_t cacc; 5694 rnode4_t *nrp; 5695 rnode4_t *drp = VTOR4(dvp); 5696 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5697 5698 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5699 ASSERT(nm != NULL); 5700 ASSERT(nm[0] != '\0'); 5701 ASSERT(dvp->v_type == VDIR); 5702 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5703 ASSERT(*vpp == NULL); 5704 5705 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5706 isdotdot = 1; 5707 args.ctag = TAG_LOOKUP_PARENT; 5708 } else { 5709 /* 5710 * If dvp were a stub, it should have triggered and caused 5711 * a mount for us to get this far. 5712 */ 5713 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5714 5715 isdotdot = 0; 5716 args.ctag = TAG_LOOKUP; 5717 } 5718 5719 mi = VTOMI4(dvp); 5720 recov_state.rs_flags = 0; 5721 recov_state.rs_num_retry_despite_err = 0; 5722 5723 nvp = NULL; 5724 5725 /* Save the original mount point security information */ 5726 (void) save_mnt_secinfo(mi->mi_curr_serv); 5727 5728 recov_retry: 5729 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5730 &recov_state, NULL); 5731 if (e.error) { 5732 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5733 return (e.error); 5734 } 5735 5736 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5737 5738 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5739 args.array_len = 9; 5740 args.array = argop; 5741 5742 /* 0. putfh file */ 5743 argop[0].argop = OP_CPUTFH; 5744 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5745 5746 /* 1. savefh for the nverify */ 5747 argop[1].argop = OP_SAVEFH; 5748 5749 /* 2. lookup name */ 5750 if (isdotdot) { 5751 argop[2].argop = OP_LOOKUPP; 5752 } else { 5753 argop[2].argop = OP_CLOOKUP; 5754 argop[2].nfs_argop4_u.opclookup.cname = nm; 5755 } 5756 5757 /* 3. resulting file handle */ 5758 argop[3].argop = OP_GETFH; 5759 5760 /* 4. resulting file attributes */ 5761 argop[4].argop = OP_GETATTR; 5762 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5763 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5764 5765 /* 5. restorefh back the directory for the nverify */ 5766 argop[5].argop = OP_RESTOREFH; 5767 5768 /* 6. nverify the change info */ 5769 argop[6].argop = OP_NVERIFY; 5770 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5771 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5772 ver_fattr->attrlist4 = (char *)&dchange; 5773 ptr = (int32_t *)&dchange; 5774 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5775 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5776 5777 /* 7. getattr directory */ 5778 argop[7].argop = OP_GETATTR; 5779 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5780 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5781 5782 /* 8. access directory */ 5783 argop[8].argop = OP_ACCESS; 5784 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5785 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5786 5787 doqueue = 1; 5788 t = gethrtime(); 5789 5790 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5791 5792 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5793 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5794 if (e.error != 0 && *vpp != NULL) 5795 VN_RELE(*vpp); 5796 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5797 &recov_state, FALSE); 5798 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5799 kmem_free(argop, argoplist_size); 5800 return (e.error); 5801 } 5802 5803 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5804 /* 5805 * For WRONGSEC of a non-dotdot case, send secinfo directly 5806 * from this thread, do not go thru the recovery thread since 5807 * we need the nm information. 5808 * 5809 * Not doing dotdot case because there is no specification 5810 * for (PUTFH, SECINFO "..") yet. 5811 */ 5812 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5813 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5814 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5815 &recov_state, FALSE); 5816 else 5817 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5818 &recov_state, TRUE); 5819 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5820 kmem_free(argop, argoplist_size); 5821 if (!e.error) 5822 goto recov_retry; 5823 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5824 return (e.error); 5825 } 5826 5827 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5828 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5829 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5830 &recov_state, TRUE); 5831 5832 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5833 kmem_free(argop, argoplist_size); 5834 goto recov_retry; 5835 } 5836 } 5837 5838 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5839 5840 if (e.error || res.array_len == 0) { 5841 /* 5842 * If e.error isn't set, then reply has no ops (or we couldn't 5843 * be here). The only legal way to reply without an op array 5844 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5845 * be in the reply for all other status values. 5846 * 5847 * For valid replies without an ops array, return ENOTSUP 5848 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5849 * return EIO -- don't trust status. 5850 */ 5851 if (e.error == 0) 5852 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5853 ENOTSUP : EIO; 5854 5855 kmem_free(argop, argoplist_size); 5856 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5857 return (e.error); 5858 } 5859 5860 e.error = geterrno4(res.status); 5861 5862 /* 5863 * The PUTFH and SAVEFH may have failed. 5864 */ 5865 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5866 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5867 nfs4_purge_stale_fh(e.error, dvp, cr); 5868 goto exit; 5869 } 5870 5871 /* 5872 * Check if the file exists, if it does delay entering 5873 * into the dnlc until after we update the directory 5874 * attributes so we don't cause it to get purged immediately. 5875 */ 5876 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5877 /* 5878 * The lookup failed, probably no entry 5879 */ 5880 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5881 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5882 goto exit; 5883 } 5884 5885 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5886 /* 5887 * The file exists but we can't get its fh for 5888 * some unknown reason. Error out to be safe. 5889 */ 5890 goto exit; 5891 } 5892 5893 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5894 if (fhp->nfs_fh4_len == 0) { 5895 /* 5896 * The file exists but a bogus fh 5897 * some unknown reason. Error out to be safe. 5898 */ 5899 e.error = EIO; 5900 goto exit; 5901 } 5902 sfhp = sfh4_get(fhp, mi); 5903 5904 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5905 sfh4_rele(&sfhp); 5906 goto exit; 5907 } 5908 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5909 5910 /* 5911 * The RESTOREFH may have failed 5912 */ 5913 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5914 sfh4_rele(&sfhp); 5915 e.error = EIO; 5916 goto exit; 5917 } 5918 5919 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5920 /* 5921 * First make sure the NVERIFY failed as we expected, 5922 * if it didn't then be conservative and error out 5923 * as we can't trust the directory. 5924 */ 5925 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5926 sfh4_rele(&sfhp); 5927 e.error = EIO; 5928 goto exit; 5929 } 5930 5931 /* 5932 * We know the NVERIFY "failed" so the directory has changed, 5933 * so we must: 5934 * purge the caches (access and indirectly dnlc if needed) 5935 */ 5936 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5937 5938 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5939 sfh4_rele(&sfhp); 5940 goto exit; 5941 } 5942 nfs4_attr_cache(dvp, 5943 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5944 t, cr, FALSE, NULL); 5945 5946 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5947 nfs4_purge_stale_fh(e.error, dvp, cr); 5948 sfh4_rele(&sfhp); 5949 e.error = geterrno4(res.status); 5950 goto exit; 5951 } 5952 5953 /* 5954 * Now we know the directory is valid, 5955 * cache new directory access 5956 */ 5957 nfs4_access_cache(drp, 5958 args.array[8].nfs_argop4_u.opaccess.access, 5959 res.array[8].nfs_resop4_u.opaccess.access, cr); 5960 5961 /* 5962 * recheck VEXEC access 5963 */ 5964 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5965 if (cacc != NFS4_ACCESS_ALLOWED) { 5966 /* 5967 * Directory permissions might have been revoked 5968 */ 5969 if (cacc == NFS4_ACCESS_DENIED) { 5970 sfh4_rele(&sfhp); 5971 e.error = EACCES; 5972 goto exit; 5973 } 5974 5975 /* 5976 * Somehow we must not have asked for enough 5977 * so try a singleton ACCESS should never happen 5978 */ 5979 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5980 if (e.error) { 5981 sfh4_rele(&sfhp); 5982 goto exit; 5983 } 5984 } 5985 5986 e.error = geterrno4(res.status); 5987 } else { 5988 hrtime_t now; 5989 hrtime_t delta = 0; 5990 5991 e.error = 0; 5992 5993 /* 5994 * Because the NVERIFY "succeeded" we know that the 5995 * directory attributes are still valid 5996 * so update r_time_attr_inval 5997 */ 5998 now = gethrtime(); 5999 mutex_enter(&drp->r_statelock); 6000 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 6001 delta = now - drp->r_time_attr_saved; 6002 if (delta < mi->mi_acdirmin) 6003 delta = mi->mi_acdirmin; 6004 else if (delta > mi->mi_acdirmax) 6005 delta = mi->mi_acdirmax; 6006 } 6007 drp->r_time_attr_inval = now + delta; 6008 mutex_exit(&drp->r_statelock); 6009 6010 /* 6011 * Even though we have a valid directory attr cache, 6012 * we may not have access. 6013 * This should almost always hit the cache. 6014 */ 6015 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 6016 if (e.error) { 6017 sfh4_rele(&sfhp); 6018 goto exit; 6019 } 6020 } 6021 6022 /* 6023 * Now we have successfully completed the lookup, if the 6024 * directory has changed we now have the valid attributes. 6025 * We also know we have directory access. 6026 * Create the new rnode and insert it in the dnlc. 6027 */ 6028 if (isdotdot) { 6029 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 6030 if (e.error) { 6031 sfh4_rele(&sfhp); 6032 goto exit; 6033 } 6034 /* 6035 * XXX if nfs4_make_dotdot uses an existing rnode 6036 * XXX it doesn't update the attributes. 6037 * XXX for now just save them again to save an OTW 6038 */ 6039 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 6040 } else { 6041 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 6042 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 6043 } 6044 sfh4_rele(&sfhp); 6045 6046 nrp = VTOR4(nvp); 6047 mutex_enter(&nrp->r_statev4_lock); 6048 if (!nrp->created_v4) { 6049 mutex_exit(&nrp->r_statev4_lock); 6050 dnlc_update(dvp, nm, nvp); 6051 } else 6052 mutex_exit(&nrp->r_statev4_lock); 6053 6054 *vpp = nvp; 6055 6056 exit: 6057 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6058 kmem_free(argop, argoplist_size); 6059 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 6060 return (e.error); 6061 } 6062 6063 #ifdef DEBUG 6064 void 6065 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 6066 { 6067 uint_t i, len; 6068 zoneid_t zoneid = getzoneid(); 6069 char *s; 6070 6071 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 6072 for (i = 0; i < argcnt; i++) { 6073 nfs_argop4 *op = &argbase[i]; 6074 switch (op->argop) { 6075 case OP_CPUTFH: 6076 case OP_PUTFH: 6077 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 6078 break; 6079 case OP_PUTROOTFH: 6080 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 6081 break; 6082 case OP_CLOOKUP: 6083 s = op->nfs_argop4_u.opclookup.cname; 6084 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6085 break; 6086 case OP_LOOKUP: 6087 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 6088 &len, NULL); 6089 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6090 kmem_free(s, len); 6091 break; 6092 case OP_LOOKUPP: 6093 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 6094 break; 6095 case OP_GETFH: 6096 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 6097 break; 6098 case OP_GETATTR: 6099 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 6100 break; 6101 case OP_OPENATTR: 6102 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 6103 break; 6104 default: 6105 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 6106 op->argop); 6107 break; 6108 } 6109 } 6110 } 6111 #endif 6112 6113 /* 6114 * nfs4lookup_setup - constructs a multi-lookup compound request. 6115 * 6116 * Given the path "nm1/nm2/.../nmn", the following compound requests 6117 * may be created: 6118 * 6119 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 6120 * is faster, for now. 6121 * 6122 * l4_getattrs indicates the type of compound requested. 6123 * 6124 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 6125 * 6126 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 6127 * 6128 * total number of ops is n + 1. 6129 * 6130 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 6131 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 6132 * before the last component, and only get attributes 6133 * for the last component. Note that the second-to-last 6134 * pathname component is XATTR_RPATH, which does NOT go 6135 * over-the-wire as a lookup. 6136 * 6137 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 6138 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 6139 * 6140 * and total number of ops is n + 5. 6141 * 6142 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 6143 * attribute directory: create lookups plus an OPENATTR 6144 * replacing the last lookup. Note that the last pathname 6145 * component is XATTR_RPATH, which does NOT go over-the-wire 6146 * as a lookup. 6147 * 6148 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 6149 * Openattr; Getfh; Getattr } 6150 * 6151 * and total number of ops is n + 5. 6152 * 6153 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6154 * nodes too. 6155 * 6156 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6157 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6158 * 6159 * and total number of ops is 3*n + 1. 6160 * 6161 * All cases: returns the index in the arg array of the final LOOKUP op, or 6162 * -1 if no LOOKUPs were used. 6163 */ 6164 int 6165 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6166 { 6167 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6168 nfs_argop4 *argbase, *argop; 6169 int arglen, argcnt; 6170 int n = 1; /* number of components */ 6171 int nga = 1; /* number of Getattr's in request */ 6172 char c = '\0', *s, *p; 6173 int lookup_idx = -1; 6174 int argoplist_size; 6175 6176 /* set lookuparg response result to 0 */ 6177 lookupargp->resp->status = NFS4_OK; 6178 6179 /* skip leading "/" or "." e.g. ".//./" if there is */ 6180 for (; ; nm++) { 6181 if (*nm != '/' && *nm != '.') 6182 break; 6183 6184 /* ".." is counted as 1 component */ 6185 if (*nm == '.' && *(nm + 1) != '/') 6186 break; 6187 } 6188 6189 /* 6190 * Find n = number of components - nm must be null terminated 6191 * Skip "." components. 6192 */ 6193 if (*nm != '\0') 6194 for (n = 1, s = nm; *s != '\0'; s++) { 6195 if ((*s == '/') && (*(s + 1) != '/') && 6196 (*(s + 1) != '\0') && 6197 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6198 *(s + 2) == '\0'))) 6199 n++; 6200 } 6201 else 6202 n = 0; 6203 6204 /* 6205 * nga is number of components that need Getfh+Getattr 6206 */ 6207 switch (l4_getattrs) { 6208 case LKP4_NO_ATTRIBUTES: 6209 nga = 0; 6210 break; 6211 case LKP4_ALL_ATTRIBUTES: 6212 nga = n; 6213 /* 6214 * Always have at least 1 getfh, getattr pair 6215 */ 6216 if (nga == 0) 6217 nga++; 6218 break; 6219 case LKP4_LAST_ATTRDIR: 6220 case LKP4_LAST_NAMED_ATTR: 6221 nga = n+1; 6222 break; 6223 } 6224 6225 /* 6226 * If change to use the filehandle attr instead of getfh 6227 * the following line can be deleted. 6228 */ 6229 nga *= 2; 6230 6231 /* 6232 * calculate number of ops in request as 6233 * header + trailer + lookups + getattrs 6234 */ 6235 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6236 6237 argoplist_size = arglen * sizeof (nfs_argop4); 6238 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6239 lookupargp->argsp->array = argop; 6240 6241 argcnt = lookupargp->header_len; 6242 argop += argcnt; 6243 6244 /* 6245 * loop and create a lookup op and possibly getattr/getfh for 6246 * each component. Skip "." components. 6247 */ 6248 for (s = nm; *s != '\0'; s = p) { 6249 /* 6250 * Set up a pathname struct for each component if needed 6251 */ 6252 while (*s == '/') 6253 s++; 6254 if (*s == '\0') 6255 break; 6256 6257 for (p = s; (*p != '/') && (*p != '\0'); p++) 6258 ; 6259 c = *p; 6260 *p = '\0'; 6261 6262 if (s[0] == '.' && s[1] == '\0') { 6263 *p = c; 6264 continue; 6265 } 6266 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6267 strcmp(s, XATTR_RPATH) == 0) { 6268 /* getfh XXX may not be needed in future */ 6269 argop->argop = OP_GETFH; 6270 argop++; 6271 argcnt++; 6272 6273 /* getattr */ 6274 argop->argop = OP_GETATTR; 6275 argop->nfs_argop4_u.opgetattr.attr_request = 6276 lookupargp->ga_bits; 6277 argop->nfs_argop4_u.opgetattr.mi = 6278 lookupargp->mi; 6279 argop++; 6280 argcnt++; 6281 6282 /* openattr */ 6283 argop->argop = OP_OPENATTR; 6284 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6285 strcmp(s, XATTR_RPATH) == 0) { 6286 /* openattr */ 6287 argop->argop = OP_OPENATTR; 6288 argop++; 6289 argcnt++; 6290 6291 /* getfh XXX may not be needed in future */ 6292 argop->argop = OP_GETFH; 6293 argop++; 6294 argcnt++; 6295 6296 /* getattr */ 6297 argop->argop = OP_GETATTR; 6298 argop->nfs_argop4_u.opgetattr.attr_request = 6299 lookupargp->ga_bits; 6300 argop->nfs_argop4_u.opgetattr.mi = 6301 lookupargp->mi; 6302 argop++; 6303 argcnt++; 6304 *p = c; 6305 continue; 6306 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6307 /* lookupp */ 6308 argop->argop = OP_LOOKUPP; 6309 } else { 6310 /* lookup */ 6311 argop->argop = OP_LOOKUP; 6312 (void) str_to_utf8(s, 6313 &argop->nfs_argop4_u.oplookup.objname); 6314 } 6315 lookup_idx = argcnt; 6316 argop++; 6317 argcnt++; 6318 6319 *p = c; 6320 6321 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6322 /* getfh XXX may not be needed in future */ 6323 argop->argop = OP_GETFH; 6324 argop++; 6325 argcnt++; 6326 6327 /* getattr */ 6328 argop->argop = OP_GETATTR; 6329 argop->nfs_argop4_u.opgetattr.attr_request = 6330 lookupargp->ga_bits; 6331 argop->nfs_argop4_u.opgetattr.mi = 6332 lookupargp->mi; 6333 argop++; 6334 argcnt++; 6335 } 6336 } 6337 6338 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6339 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6340 if (needgetfh) { 6341 /* stick in a post-lookup getfh */ 6342 argop->argop = OP_GETFH; 6343 argcnt++; 6344 argop++; 6345 } 6346 /* post-lookup getattr */ 6347 argop->argop = OP_GETATTR; 6348 argop->nfs_argop4_u.opgetattr.attr_request = 6349 lookupargp->ga_bits; 6350 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6351 argcnt++; 6352 } 6353 argcnt += lookupargp->trailer_len; /* actual op count */ 6354 lookupargp->argsp->array_len = argcnt; 6355 lookupargp->arglen = arglen; 6356 6357 #ifdef DEBUG 6358 if (nfs4_client_lookup_debug) 6359 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6360 #endif 6361 6362 return (lookup_idx); 6363 } 6364 6365 static int 6366 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6367 { 6368 COMPOUND4args_clnt args; 6369 COMPOUND4res_clnt res; 6370 GETFH4res *gf_res = NULL; 6371 nfs_argop4 argop[4]; 6372 nfs_resop4 *resop = NULL; 6373 nfs4_sharedfh_t *sfhp; 6374 hrtime_t t; 6375 nfs4_error_t e; 6376 6377 rnode4_t *drp; 6378 int doqueue = 1; 6379 vnode_t *vp; 6380 int needrecov = 0; 6381 nfs4_recov_state_t recov_state; 6382 6383 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6384 6385 *avp = NULL; 6386 recov_state.rs_flags = 0; 6387 recov_state.rs_num_retry_despite_err = 0; 6388 6389 recov_retry: 6390 /* COMPOUND: putfh, openattr, getfh, getattr */ 6391 args.array_len = 4; 6392 args.array = argop; 6393 args.ctag = TAG_OPENATTR; 6394 6395 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6396 if (e.error) 6397 return (e.error); 6398 6399 drp = VTOR4(dvp); 6400 6401 /* putfh */ 6402 argop[0].argop = OP_CPUTFH; 6403 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6404 6405 /* openattr */ 6406 argop[1].argop = OP_OPENATTR; 6407 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6408 6409 /* getfh */ 6410 argop[2].argop = OP_GETFH; 6411 6412 /* getattr */ 6413 argop[3].argop = OP_GETATTR; 6414 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6415 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6416 6417 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6418 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6419 rnode4info(drp))); 6420 6421 t = gethrtime(); 6422 6423 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6424 6425 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6426 if (needrecov) { 6427 bool_t abort; 6428 6429 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6430 "nfs4openattr: initiating recovery\n")); 6431 6432 abort = nfs4_start_recovery(&e, 6433 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6434 OP_OPENATTR, NULL, NULL, NULL); 6435 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6436 if (!e.error) { 6437 e.error = geterrno4(res.status); 6438 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6439 } 6440 if (abort == FALSE) 6441 goto recov_retry; 6442 return (e.error); 6443 } 6444 6445 if (e.error) { 6446 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6447 return (e.error); 6448 } 6449 6450 if (res.status) { 6451 /* 6452 * If OTW errro is NOTSUPP, then it should be 6453 * translated to EINVAL. All Solaris file system 6454 * implementations return EINVAL to the syscall layer 6455 * when the attrdir cannot be created due to an 6456 * implementation restriction or noxattr mount option. 6457 */ 6458 if (res.status == NFS4ERR_NOTSUPP) { 6459 mutex_enter(&drp->r_statelock); 6460 if (drp->r_xattr_dir) 6461 VN_RELE(drp->r_xattr_dir); 6462 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6463 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6464 mutex_exit(&drp->r_statelock); 6465 6466 e.error = EINVAL; 6467 } else { 6468 e.error = geterrno4(res.status); 6469 } 6470 6471 if (e.error) { 6472 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6473 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6474 needrecov); 6475 return (e.error); 6476 } 6477 } 6478 6479 resop = &res.array[0]; /* putfh res */ 6480 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6481 6482 resop = &res.array[1]; /* openattr res */ 6483 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6484 6485 resop = &res.array[2]; /* getfh res */ 6486 gf_res = &resop->nfs_resop4_u.opgetfh; 6487 if (gf_res->object.nfs_fh4_len == 0) { 6488 *avp = NULL; 6489 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6490 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6491 return (ENOENT); 6492 } 6493 6494 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6495 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6496 dvp->v_vfsp, t, cr, dvp, 6497 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp)); 6498 sfh4_rele(&sfhp); 6499 6500 if (e.error) 6501 PURGE_ATTRCACHE4(vp); 6502 6503 mutex_enter(&vp->v_lock); 6504 vp->v_flag |= V_XATTRDIR; 6505 mutex_exit(&vp->v_lock); 6506 6507 *avp = vp; 6508 6509 mutex_enter(&drp->r_statelock); 6510 if (drp->r_xattr_dir) 6511 VN_RELE(drp->r_xattr_dir); 6512 VN_HOLD(vp); 6513 drp->r_xattr_dir = vp; 6514 6515 /* 6516 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6517 * NULL. xattrs could be created at any time, and we have no 6518 * way to update pc4_xattr_exists in the base object if/when 6519 * it happens. 6520 */ 6521 drp->r_pathconf.pc4_xattr_valid = 0; 6522 6523 mutex_exit(&drp->r_statelock); 6524 6525 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6526 6527 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6528 6529 return (0); 6530 } 6531 6532 /* ARGSUSED */ 6533 static int 6534 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6535 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct, 6536 vsecattr_t *vsecp) 6537 { 6538 int error; 6539 vnode_t *vp = NULL; 6540 rnode4_t *rp; 6541 struct vattr vattr; 6542 rnode4_t *drp; 6543 vnode_t *tempvp; 6544 enum createmode4 createmode; 6545 bool_t must_trunc = FALSE; 6546 int truncating = 0; 6547 6548 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6549 return (EPERM); 6550 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6551 return (EINVAL); 6552 } 6553 6554 /* . and .. have special meaning in the protocol, reject them. */ 6555 6556 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6557 return (EISDIR); 6558 6559 drp = VTOR4(dvp); 6560 6561 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6562 return (EINTR); 6563 6564 top: 6565 /* 6566 * We make a copy of the attributes because the caller does not 6567 * expect us to change what va points to. 6568 */ 6569 vattr = *va; 6570 6571 /* 6572 * If the pathname is "", then dvp is the root vnode of 6573 * a remote file mounted over a local directory. 6574 * All that needs to be done is access 6575 * checking and truncation. Note that we avoid doing 6576 * open w/ create because the parent directory might 6577 * be in pseudo-fs and the open would fail. 6578 */ 6579 if (*nm == '\0') { 6580 error = 0; 6581 VN_HOLD(dvp); 6582 vp = dvp; 6583 must_trunc = TRUE; 6584 } else { 6585 /* 6586 * We need to go over the wire, just to be sure whether the 6587 * file exists or not. Using the DNLC can be dangerous in 6588 * this case when making a decision regarding existence. 6589 */ 6590 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6591 } 6592 6593 if (exclusive) 6594 createmode = EXCLUSIVE4; 6595 else 6596 createmode = GUARDED4; 6597 6598 /* 6599 * error would be set if the file does not exist on the 6600 * server, so lets go create it. 6601 */ 6602 if (error) { 6603 goto create_otw; 6604 } 6605 6606 /* 6607 * File does exist on the server 6608 */ 6609 if (exclusive == EXCL) 6610 error = EEXIST; 6611 else if (vp->v_type == VDIR && (mode & VWRITE)) 6612 error = EISDIR; 6613 else { 6614 /* 6615 * If vnode is a device, create special vnode. 6616 */ 6617 if (ISVDEV(vp->v_type)) { 6618 tempvp = vp; 6619 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6620 VN_RELE(tempvp); 6621 } 6622 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) { 6623 if ((vattr.va_mask & AT_SIZE) && 6624 vp->v_type == VREG) { 6625 rp = VTOR4(vp); 6626 /* 6627 * Check here for large file handled 6628 * by LF-unaware process (as 6629 * ufs_create() does) 6630 */ 6631 if (!(flags & FOFFMAX)) { 6632 mutex_enter(&rp->r_statelock); 6633 if (rp->r_size > MAXOFF32_T) 6634 error = EOVERFLOW; 6635 mutex_exit(&rp->r_statelock); 6636 } 6637 6638 /* if error is set then we need to return */ 6639 if (error) { 6640 nfs_rw_exit(&drp->r_rwlock); 6641 VN_RELE(vp); 6642 return (error); 6643 } 6644 6645 if (must_trunc) { 6646 vattr.va_mask = AT_SIZE; 6647 error = nfs4setattr(vp, &vattr, 0, cr, 6648 NULL); 6649 } else { 6650 /* 6651 * we know we have a regular file that already 6652 * exists and we may end up truncating the file 6653 * as a result of the open_otw, so flush out 6654 * any dirty pages for this file first. 6655 */ 6656 if (nfs4_has_pages(vp) && 6657 ((rp->r_flags & R4DIRTY) || 6658 rp->r_count > 0 || 6659 rp->r_mapcnt > 0)) { 6660 error = nfs4_putpage(vp, 6661 (offset_t)0, 0, 0, cr, ct); 6662 if (error && (error == ENOSPC || 6663 error == EDQUOT)) { 6664 mutex_enter( 6665 &rp->r_statelock); 6666 if (!rp->r_error) 6667 rp->r_error = 6668 error; 6669 mutex_exit( 6670 &rp->r_statelock); 6671 } 6672 } 6673 vattr.va_mask = (AT_SIZE | 6674 AT_TYPE | AT_MODE); 6675 vattr.va_type = VREG; 6676 createmode = UNCHECKED4; 6677 truncating = 1; 6678 goto create_otw; 6679 } 6680 } 6681 } 6682 } 6683 nfs_rw_exit(&drp->r_rwlock); 6684 if (error) { 6685 VN_RELE(vp); 6686 } else { 6687 vnode_t *tvp; 6688 rnode4_t *trp; 6689 tvp = vp; 6690 if (vp->v_type == VREG) { 6691 trp = VTOR4(vp); 6692 if (IS_SHADOW(vp, trp)) 6693 tvp = RTOV4(trp); 6694 } 6695 6696 if (must_trunc) { 6697 /* 6698 * existing file got truncated, notify. 6699 */ 6700 vnevent_create(tvp, ct); 6701 } 6702 6703 *vpp = vp; 6704 } 6705 return (error); 6706 6707 create_otw: 6708 dnlc_remove(dvp, nm); 6709 6710 ASSERT(vattr.va_mask & AT_TYPE); 6711 6712 /* 6713 * If not a regular file let nfs4mknod() handle it. 6714 */ 6715 if (vattr.va_type != VREG) { 6716 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6717 nfs_rw_exit(&drp->r_rwlock); 6718 return (error); 6719 } 6720 6721 /* 6722 * It _is_ a regular file. 6723 */ 6724 ASSERT(vattr.va_mask & AT_MODE); 6725 if (MANDMODE(vattr.va_mode)) { 6726 nfs_rw_exit(&drp->r_rwlock); 6727 return (EACCES); 6728 } 6729 6730 /* 6731 * If this happens to be a mknod of a regular file, then flags will 6732 * have neither FREAD or FWRITE. However, we must set at least one 6733 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6734 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6735 * set (based on openmode specified by app). 6736 */ 6737 if ((flags & (FREAD|FWRITE)) == 0) 6738 flags |= (FREAD|FWRITE); 6739 6740 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6741 6742 if (vp != NULL) { 6743 /* if create was successful, throw away the file's pages */ 6744 if (!error && (vattr.va_mask & AT_SIZE)) 6745 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6746 cr); 6747 /* release the lookup hold */ 6748 VN_RELE(vp); 6749 vp = NULL; 6750 } 6751 6752 /* 6753 * validate that we opened a regular file. This handles a misbehaving 6754 * server that returns an incorrect FH. 6755 */ 6756 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6757 error = EISDIR; 6758 VN_RELE(*vpp); 6759 } 6760 6761 /* 6762 * If this is not an exclusive create, then the CREATE 6763 * request will be made with the GUARDED mode set. This 6764 * means that the server will return EEXIST if the file 6765 * exists. The file could exist because of a retransmitted 6766 * request. In this case, we recover by starting over and 6767 * checking to see whether the file exists. This second 6768 * time through it should and a CREATE request will not be 6769 * sent. 6770 * 6771 * This handles the problem of a dangling CREATE request 6772 * which contains attributes which indicate that the file 6773 * should be truncated. This retransmitted request could 6774 * possibly truncate valid data in the file if not caught 6775 * by the duplicate request mechanism on the server or if 6776 * not caught by other means. The scenario is: 6777 * 6778 * Client transmits CREATE request with size = 0 6779 * Client times out, retransmits request. 6780 * Response to the first request arrives from the server 6781 * and the client proceeds on. 6782 * Client writes data to the file. 6783 * The server now processes retransmitted CREATE request 6784 * and truncates file. 6785 * 6786 * The use of the GUARDED CREATE request prevents this from 6787 * happening because the retransmitted CREATE would fail 6788 * with EEXIST and would not truncate the file. 6789 */ 6790 if (error == EEXIST && exclusive == NONEXCL) { 6791 #ifdef DEBUG 6792 nfs4_create_misses++; 6793 #endif 6794 goto top; 6795 } 6796 nfs_rw_exit(&drp->r_rwlock); 6797 if (truncating && !error && *vpp) { 6798 vnode_t *tvp; 6799 rnode4_t *trp; 6800 /* 6801 * existing file got truncated, notify. 6802 */ 6803 tvp = *vpp; 6804 trp = VTOR4(tvp); 6805 if (IS_SHADOW(tvp, trp)) 6806 tvp = RTOV4(trp); 6807 vnevent_create(tvp, ct); 6808 } 6809 return (error); 6810 } 6811 6812 /* 6813 * Create compound (for mkdir, mknod, symlink): 6814 * { Putfh <dfh>; Create; Getfh; Getattr } 6815 * It's okay if setattr failed to set gid - this is not considered 6816 * an error, but purge attrs in that case. 6817 */ 6818 static int 6819 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6820 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6821 { 6822 int need_end_op = FALSE; 6823 COMPOUND4args_clnt args; 6824 COMPOUND4res_clnt res, *resp = NULL; 6825 nfs_argop4 *argop; 6826 nfs_resop4 *resop; 6827 int doqueue; 6828 mntinfo4_t *mi; 6829 rnode4_t *drp = VTOR4(dvp); 6830 change_info4 *cinfo; 6831 GETFH4res *gf_res; 6832 struct vattr vattr; 6833 vnode_t *vp; 6834 fattr4 *crattr; 6835 bool_t needrecov = FALSE; 6836 nfs4_recov_state_t recov_state; 6837 nfs4_sharedfh_t *sfhp = NULL; 6838 hrtime_t t; 6839 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6840 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6841 dirattr_info_t dinfo, *dinfop; 6842 servinfo4_t *svp; 6843 bitmap4 supp_attrs; 6844 6845 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6846 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6847 6848 mi = VTOMI4(dvp); 6849 6850 /* 6851 * Make sure we properly deal with setting the right gid 6852 * on a new directory to reflect the parent's setgid bit 6853 */ 6854 setgid_flag = 0; 6855 if (type == NF4DIR) { 6856 struct vattr dva; 6857 6858 va->va_mode &= ~VSGID; 6859 dva.va_mask = AT_MODE | AT_GID; 6860 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) { 6861 6862 /* 6863 * If the parent's directory has the setgid bit set 6864 * _and_ the client was able to get a valid mapping 6865 * for the parent dir's owner_group, we want to 6866 * append NVERIFY(owner_group == dva.va_gid) and 6867 * SETTATTR to the CREATE compound. 6868 */ 6869 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6870 setgid_flag = 1; 6871 va->va_mode |= VSGID; 6872 if (dva.va_gid != GID_NOBODY) { 6873 va->va_mask |= AT_GID; 6874 va->va_gid = dva.va_gid; 6875 } 6876 } 6877 } 6878 } 6879 6880 /* 6881 * Create ops: 6882 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6883 * 5:restorefh(dir) 6:getattr(dir) 6884 * 6885 * if (setgid) 6886 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6887 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6888 * 8:nverify 9:setattr 6889 */ 6890 if (setgid_flag) { 6891 numops = 10; 6892 idx_create = 1; 6893 idx_fattr = 3; 6894 } else { 6895 numops = 7; 6896 idx_create = 2; 6897 idx_fattr = 4; 6898 } 6899 6900 ASSERT(nfs_zone() == mi->mi_zone); 6901 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6902 return (EINTR); 6903 } 6904 recov_state.rs_flags = 0; 6905 recov_state.rs_num_retry_despite_err = 0; 6906 6907 argoplist_size = numops * sizeof (nfs_argop4); 6908 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6909 6910 recov_retry: 6911 if (type == NF4LNK) 6912 args.ctag = TAG_SYMLINK; 6913 else if (type == NF4DIR) 6914 args.ctag = TAG_MKDIR; 6915 else 6916 args.ctag = TAG_MKNOD; 6917 6918 args.array_len = numops; 6919 args.array = argop; 6920 6921 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6922 nfs_rw_exit(&drp->r_rwlock); 6923 kmem_free(argop, argoplist_size); 6924 return (e.error); 6925 } 6926 need_end_op = TRUE; 6927 6928 6929 /* 0: putfh directory */ 6930 argop[0].argop = OP_CPUTFH; 6931 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6932 6933 /* 1/2: Create object */ 6934 argop[idx_create].argop = OP_CCREATE; 6935 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6936 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6937 if (type == NF4LNK) { 6938 /* 6939 * symlink, treat name as data 6940 */ 6941 ASSERT(data != NULL); 6942 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6943 (char *)data; 6944 } 6945 if (type == NF4BLK || type == NF4CHR) { 6946 ASSERT(data != NULL); 6947 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6948 *((specdata4 *)data); 6949 } 6950 6951 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6952 6953 svp = drp->r_server; 6954 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6955 supp_attrs = svp->sv_supp_attrs; 6956 nfs_rw_exit(&svp->sv_lock); 6957 6958 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6959 nfs_rw_exit(&drp->r_rwlock); 6960 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6961 e.error = EINVAL; 6962 kmem_free(argop, argoplist_size); 6963 return (e.error); 6964 } 6965 6966 /* 2/3: getfh fh of created object */ 6967 ASSERT(idx_create + 1 == idx_fattr - 1); 6968 argop[idx_create + 1].argop = OP_GETFH; 6969 6970 /* 3/4: getattr of new object */ 6971 argop[idx_fattr].argop = OP_GETATTR; 6972 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6973 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6974 6975 if (setgid_flag) { 6976 vattr_t _v; 6977 6978 argop[4].argop = OP_SAVEFH; 6979 6980 argop[5].argop = OP_CPUTFH; 6981 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6982 6983 argop[6].argop = OP_GETATTR; 6984 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6985 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6986 6987 argop[7].argop = OP_RESTOREFH; 6988 6989 /* 6990 * nverify 6991 * 6992 * XXX - Revisit the last argument to nfs4_end_op() 6993 * once 5020486 is fixed. 6994 */ 6995 _v.va_mask = AT_GID; 6996 _v.va_gid = va->va_gid; 6997 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6998 supp_attrs)) { 6999 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 7000 nfs_rw_exit(&drp->r_rwlock); 7001 nfs4_fattr4_free(crattr); 7002 kmem_free(argop, argoplist_size); 7003 return (e.error); 7004 } 7005 7006 /* 7007 * setattr 7008 * 7009 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 7010 * so no need for stateid or flags. Also we specify NULL 7011 * rp since we're only interested in setting owner_group 7012 * attributes. 7013 */ 7014 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 7015 &e.error, 0); 7016 7017 if (e.error) { 7018 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 7019 nfs_rw_exit(&drp->r_rwlock); 7020 nfs4_fattr4_free(crattr); 7021 nfs4args_verify_free(&argop[8]); 7022 kmem_free(argop, argoplist_size); 7023 return (e.error); 7024 } 7025 } else { 7026 argop[1].argop = OP_SAVEFH; 7027 7028 argop[5].argop = OP_RESTOREFH; 7029 7030 argop[6].argop = OP_GETATTR; 7031 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7032 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7033 } 7034 7035 dnlc_remove(dvp, nm); 7036 7037 doqueue = 1; 7038 t = gethrtime(); 7039 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7040 7041 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7042 if (e.error) { 7043 PURGE_ATTRCACHE4(dvp); 7044 if (!needrecov) 7045 goto out; 7046 } 7047 7048 if (needrecov) { 7049 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 7050 OP_CREATE, NULL, NULL, NULL) == FALSE) { 7051 nfs4_end_op(mi, dvp, NULL, &recov_state, 7052 needrecov); 7053 need_end_op = FALSE; 7054 nfs4_fattr4_free(crattr); 7055 if (setgid_flag) { 7056 nfs4args_verify_free(&argop[8]); 7057 nfs4args_setattr_free(&argop[9]); 7058 } 7059 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 7060 goto recov_retry; 7061 } 7062 } 7063 7064 resp = &res; 7065 7066 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 7067 7068 if (res.status == NFS4ERR_BADOWNER) 7069 nfs4_log_badowner(mi, OP_CREATE); 7070 7071 e.error = geterrno4(res.status); 7072 7073 /* 7074 * This check is left over from when create was implemented 7075 * using a setattr op (instead of createattrs). If the 7076 * putfh/create/getfh failed, the error was returned. If 7077 * setattr/getattr failed, we keep going. 7078 * 7079 * It might be better to get rid of the GETFH also, and just 7080 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 7081 * Then if any of the operations failed, we could return the 7082 * error now, and remove much of the error code below. 7083 */ 7084 if (res.array_len <= idx_fattr) { 7085 /* 7086 * Either Putfh, Create or Getfh failed. 7087 */ 7088 PURGE_ATTRCACHE4(dvp); 7089 /* 7090 * nfs4_purge_stale_fh() may generate otw calls through 7091 * nfs4_invalidate_pages. Hence the need to call 7092 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 7093 */ 7094 nfs4_end_op(mi, dvp, NULL, &recov_state, 7095 needrecov); 7096 need_end_op = FALSE; 7097 nfs4_purge_stale_fh(e.error, dvp, cr); 7098 goto out; 7099 } 7100 } 7101 7102 resop = &res.array[idx_create]; /* create res */ 7103 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 7104 7105 resop = &res.array[idx_create + 1]; /* getfh res */ 7106 gf_res = &resop->nfs_resop4_u.opgetfh; 7107 7108 sfhp = sfh4_get(&gf_res->object, mi); 7109 if (e.error) { 7110 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 7111 fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7112 if (vp->v_type == VNON) { 7113 vattr.va_mask = AT_TYPE; 7114 /* 7115 * Need to call nfs4_end_op before nfs4getattr to avoid 7116 * potential nfs4_start_op deadlock. See RFE 4777612. 7117 */ 7118 nfs4_end_op(mi, dvp, NULL, &recov_state, 7119 needrecov); 7120 need_end_op = FALSE; 7121 e.error = nfs4getattr(vp, &vattr, cr); 7122 if (e.error) { 7123 VN_RELE(vp); 7124 *vpp = NULL; 7125 goto out; 7126 } 7127 vp->v_type = vattr.va_type; 7128 } 7129 e.error = 0; 7130 } else { 7131 *vpp = vp = makenfs4node(sfhp, 7132 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 7133 dvp->v_vfsp, t, cr, 7134 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7135 } 7136 7137 /* 7138 * If compound succeeded, then update dir attrs 7139 */ 7140 if (res.status == NFS4_OK) { 7141 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 7142 dinfo.di_cred = cr; 7143 dinfo.di_time_call = t; 7144 dinfop = &dinfo; 7145 } else 7146 dinfop = NULL; 7147 7148 /* Update directory cache attribute, readdir and dnlc caches */ 7149 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 7150 7151 out: 7152 if (sfhp != NULL) 7153 sfh4_rele(&sfhp); 7154 nfs_rw_exit(&drp->r_rwlock); 7155 nfs4_fattr4_free(crattr); 7156 if (setgid_flag) { 7157 nfs4args_verify_free(&argop[8]); 7158 nfs4args_setattr_free(&argop[9]); 7159 } 7160 if (resp) 7161 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7162 if (need_end_op) 7163 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7164 7165 kmem_free(argop, argoplist_size); 7166 return (e.error); 7167 } 7168 7169 /* ARGSUSED */ 7170 static int 7171 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7172 int mode, vnode_t **vpp, cred_t *cr) 7173 { 7174 int error; 7175 vnode_t *vp; 7176 nfs_ftype4 type; 7177 specdata4 spec, *specp = NULL; 7178 7179 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7180 7181 switch (va->va_type) { 7182 case VCHR: 7183 case VBLK: 7184 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7185 spec.specdata1 = getmajor(va->va_rdev); 7186 spec.specdata2 = getminor(va->va_rdev); 7187 specp = &spec; 7188 break; 7189 7190 case VFIFO: 7191 type = NF4FIFO; 7192 break; 7193 case VSOCK: 7194 type = NF4SOCK; 7195 break; 7196 7197 default: 7198 return (EINVAL); 7199 } 7200 7201 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7202 if (error) { 7203 return (error); 7204 } 7205 7206 /* 7207 * This might not be needed any more; special case to deal 7208 * with problematic v2/v3 servers. Since create was unable 7209 * to set group correctly, not sure what hope setattr has. 7210 */ 7211 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7212 va->va_mask = AT_GID; 7213 (void) nfs4setattr(vp, va, 0, cr, NULL); 7214 } 7215 7216 /* 7217 * If vnode is a device create special vnode 7218 */ 7219 if (ISVDEV(vp->v_type)) { 7220 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7221 VN_RELE(vp); 7222 } else { 7223 *vpp = vp; 7224 } 7225 return (error); 7226 } 7227 7228 /* 7229 * Remove requires that the current fh be the target directory. 7230 * After the operation, the current fh is unchanged. 7231 * The compound op structure is: 7232 * PUTFH(targetdir), REMOVE 7233 * 7234 * Weirdness: if the vnode to be removed is open 7235 * we rename it instead of removing it and nfs_inactive 7236 * will remove the new name. 7237 */ 7238 /* ARGSUSED */ 7239 static int 7240 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags) 7241 { 7242 COMPOUND4args_clnt args; 7243 COMPOUND4res_clnt res, *resp = NULL; 7244 REMOVE4res *rm_res; 7245 nfs_argop4 argop[3]; 7246 nfs_resop4 *resop; 7247 vnode_t *vp; 7248 char *tmpname; 7249 int doqueue; 7250 mntinfo4_t *mi; 7251 rnode4_t *rp; 7252 rnode4_t *drp; 7253 int needrecov = 0; 7254 nfs4_recov_state_t recov_state; 7255 int isopen; 7256 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7257 dirattr_info_t dinfo; 7258 7259 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7260 return (EPERM); 7261 drp = VTOR4(dvp); 7262 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7263 return (EINTR); 7264 7265 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7266 if (e.error) { 7267 nfs_rw_exit(&drp->r_rwlock); 7268 return (e.error); 7269 } 7270 7271 if (vp->v_type == VDIR) { 7272 VN_RELE(vp); 7273 nfs_rw_exit(&drp->r_rwlock); 7274 return (EISDIR); 7275 } 7276 7277 /* 7278 * First just remove the entry from the name cache, as it 7279 * is most likely the only entry for this vp. 7280 */ 7281 dnlc_remove(dvp, nm); 7282 7283 rp = VTOR4(vp); 7284 7285 /* 7286 * For regular file types, check to see if the file is open by looking 7287 * at the open streams. 7288 * For all other types, check the reference count on the vnode. Since 7289 * they are not opened OTW they never have an open stream. 7290 * 7291 * If the file is open, rename it to .nfsXXXX. 7292 */ 7293 if (vp->v_type != VREG) { 7294 /* 7295 * If the file has a v_count > 1 then there may be more than one 7296 * entry in the name cache due multiple links or an open file, 7297 * but we don't have the real reference count so flush all 7298 * possible entries. 7299 */ 7300 if (vp->v_count > 1) 7301 dnlc_purge_vp(vp); 7302 7303 /* 7304 * Now we have the real reference count. 7305 */ 7306 isopen = vp->v_count > 1; 7307 } else { 7308 mutex_enter(&rp->r_os_lock); 7309 isopen = list_head(&rp->r_open_streams) != NULL; 7310 mutex_exit(&rp->r_os_lock); 7311 } 7312 7313 mutex_enter(&rp->r_statelock); 7314 if (isopen && 7315 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7316 mutex_exit(&rp->r_statelock); 7317 tmpname = newname(); 7318 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct); 7319 if (e.error) 7320 kmem_free(tmpname, MAXNAMELEN); 7321 else { 7322 mutex_enter(&rp->r_statelock); 7323 if (rp->r_unldvp == NULL) { 7324 VN_HOLD(dvp); 7325 rp->r_unldvp = dvp; 7326 if (rp->r_unlcred != NULL) 7327 crfree(rp->r_unlcred); 7328 crhold(cr); 7329 rp->r_unlcred = cr; 7330 rp->r_unlname = tmpname; 7331 } else { 7332 kmem_free(rp->r_unlname, MAXNAMELEN); 7333 rp->r_unlname = tmpname; 7334 } 7335 mutex_exit(&rp->r_statelock); 7336 } 7337 VN_RELE(vp); 7338 nfs_rw_exit(&drp->r_rwlock); 7339 return (e.error); 7340 } 7341 /* 7342 * Actually remove the file/dir 7343 */ 7344 mutex_exit(&rp->r_statelock); 7345 7346 /* 7347 * We need to flush any dirty pages which happen to 7348 * be hanging around before removing the file. 7349 * This shouldn't happen very often since in NFSv4 7350 * we should be close to open consistent. 7351 */ 7352 if (nfs4_has_pages(vp) && 7353 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7354 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct); 7355 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7356 mutex_enter(&rp->r_statelock); 7357 if (!rp->r_error) 7358 rp->r_error = e.error; 7359 mutex_exit(&rp->r_statelock); 7360 } 7361 } 7362 7363 mi = VTOMI4(dvp); 7364 7365 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7366 recov_state.rs_flags = 0; 7367 recov_state.rs_num_retry_despite_err = 0; 7368 7369 recov_retry: 7370 /* 7371 * Remove ops: putfh dir; remove 7372 */ 7373 args.ctag = TAG_REMOVE; 7374 args.array_len = 3; 7375 args.array = argop; 7376 7377 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7378 if (e.error) { 7379 nfs_rw_exit(&drp->r_rwlock); 7380 VN_RELE(vp); 7381 return (e.error); 7382 } 7383 7384 /* putfh directory */ 7385 argop[0].argop = OP_CPUTFH; 7386 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7387 7388 /* remove */ 7389 argop[1].argop = OP_CREMOVE; 7390 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7391 7392 /* getattr dir */ 7393 argop[2].argop = OP_GETATTR; 7394 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7395 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7396 7397 doqueue = 1; 7398 dinfo.di_time_call = gethrtime(); 7399 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7400 7401 PURGE_ATTRCACHE4(vp); 7402 7403 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7404 if (e.error) 7405 PURGE_ATTRCACHE4(dvp); 7406 7407 if (needrecov) { 7408 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7409 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 7410 if (!e.error) 7411 (void) xdr_free(xdr_COMPOUND4res_clnt, 7412 (caddr_t)&res); 7413 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7414 needrecov); 7415 goto recov_retry; 7416 } 7417 } 7418 7419 /* 7420 * Matching nfs4_end_op() for start_op() above. 7421 * There is a path in the code below which calls 7422 * nfs4_purge_stale_fh(), which may generate otw calls through 7423 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7424 * here to avoid nfs4_start_op() deadlock. 7425 */ 7426 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7427 7428 if (!e.error) { 7429 resp = &res; 7430 7431 if (res.status) { 7432 e.error = geterrno4(res.status); 7433 PURGE_ATTRCACHE4(dvp); 7434 nfs4_purge_stale_fh(e.error, dvp, cr); 7435 } else { 7436 resop = &res.array[1]; /* remove res */ 7437 rm_res = &resop->nfs_resop4_u.opremove; 7438 7439 dinfo.di_garp = 7440 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7441 dinfo.di_cred = cr; 7442 7443 /* Update directory attr, readdir and dnlc caches */ 7444 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7445 &dinfo); 7446 } 7447 } 7448 nfs_rw_exit(&drp->r_rwlock); 7449 if (resp) 7450 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7451 7452 if (e.error == 0) { 7453 vnode_t *tvp; 7454 rnode4_t *trp; 7455 trp = VTOR4(vp); 7456 tvp = vp; 7457 if (IS_SHADOW(vp, trp)) 7458 tvp = RTOV4(trp); 7459 vnevent_remove(tvp, dvp, nm, ct); 7460 } 7461 VN_RELE(vp); 7462 return (e.error); 7463 } 7464 7465 /* 7466 * Link requires that the current fh be the target directory and the 7467 * saved fh be the source fh. After the operation, the current fh is unchanged. 7468 * Thus the compound op structure is: 7469 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7470 * GETATTR(file) 7471 */ 7472 /* ARGSUSED */ 7473 static int 7474 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr, 7475 caller_context_t *ct, int flags) 7476 { 7477 COMPOUND4args_clnt args; 7478 COMPOUND4res_clnt res, *resp = NULL; 7479 LINK4res *ln_res; 7480 int argoplist_size = 7 * sizeof (nfs_argop4); 7481 nfs_argop4 *argop; 7482 nfs_resop4 *resop; 7483 vnode_t *realvp, *nvp; 7484 int doqueue; 7485 mntinfo4_t *mi; 7486 rnode4_t *tdrp; 7487 bool_t needrecov = FALSE; 7488 nfs4_recov_state_t recov_state; 7489 hrtime_t t; 7490 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7491 dirattr_info_t dinfo; 7492 7493 ASSERT(*tnm != '\0'); 7494 ASSERT(tdvp->v_type == VDIR); 7495 ASSERT(nfs4_consistent_type(tdvp)); 7496 ASSERT(nfs4_consistent_type(svp)); 7497 7498 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7499 return (EPERM); 7500 if (VOP_REALVP(svp, &realvp, ct) == 0) { 7501 svp = realvp; 7502 ASSERT(nfs4_consistent_type(svp)); 7503 } 7504 7505 tdrp = VTOR4(tdvp); 7506 mi = VTOMI4(svp); 7507 7508 if (!(mi->mi_flags & MI4_LINK)) { 7509 return (EOPNOTSUPP); 7510 } 7511 recov_state.rs_flags = 0; 7512 recov_state.rs_num_retry_despite_err = 0; 7513 7514 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7515 return (EINTR); 7516 7517 recov_retry: 7518 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7519 7520 args.ctag = TAG_LINK; 7521 7522 /* 7523 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7524 * restorefh; getattr(fl) 7525 */ 7526 args.array_len = 7; 7527 args.array = argop; 7528 7529 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7530 if (e.error) { 7531 kmem_free(argop, argoplist_size); 7532 nfs_rw_exit(&tdrp->r_rwlock); 7533 return (e.error); 7534 } 7535 7536 /* 0. putfh file */ 7537 argop[0].argop = OP_CPUTFH; 7538 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7539 7540 /* 1. save current fh to free up the space for the dir */ 7541 argop[1].argop = OP_SAVEFH; 7542 7543 /* 2. putfh targetdir */ 7544 argop[2].argop = OP_CPUTFH; 7545 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7546 7547 /* 3. link: current_fh is targetdir, saved_fh is source */ 7548 argop[3].argop = OP_CLINK; 7549 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7550 7551 /* 4. Get attributes of dir */ 7552 argop[4].argop = OP_GETATTR; 7553 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7554 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7555 7556 /* 5. If link was successful, restore current vp to file */ 7557 argop[5].argop = OP_RESTOREFH; 7558 7559 /* 6. Get attributes of linked object */ 7560 argop[6].argop = OP_GETATTR; 7561 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7562 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7563 7564 dnlc_remove(tdvp, tnm); 7565 7566 doqueue = 1; 7567 t = gethrtime(); 7568 7569 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7570 7571 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7572 if (e.error != 0 && !needrecov) { 7573 PURGE_ATTRCACHE4(tdvp); 7574 PURGE_ATTRCACHE4(svp); 7575 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7576 goto out; 7577 } 7578 7579 if (needrecov) { 7580 bool_t abort; 7581 7582 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7583 NULL, NULL, OP_LINK, NULL, NULL, NULL); 7584 if (abort == FALSE) { 7585 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7586 needrecov); 7587 kmem_free(argop, argoplist_size); 7588 if (!e.error) 7589 (void) xdr_free(xdr_COMPOUND4res_clnt, 7590 (caddr_t)&res); 7591 goto recov_retry; 7592 } else { 7593 if (e.error != 0) { 7594 PURGE_ATTRCACHE4(tdvp); 7595 PURGE_ATTRCACHE4(svp); 7596 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7597 &recov_state, needrecov); 7598 goto out; 7599 } 7600 /* fall through for res.status case */ 7601 } 7602 } 7603 7604 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7605 7606 resp = &res; 7607 if (res.status) { 7608 /* If link succeeded, then don't return error */ 7609 e.error = geterrno4(res.status); 7610 if (res.array_len <= 4) { 7611 /* 7612 * Either Putfh, Savefh, Putfh dir, or Link failed 7613 */ 7614 PURGE_ATTRCACHE4(svp); 7615 PURGE_ATTRCACHE4(tdvp); 7616 if (e.error == EOPNOTSUPP) { 7617 mutex_enter(&mi->mi_lock); 7618 mi->mi_flags &= ~MI4_LINK; 7619 mutex_exit(&mi->mi_lock); 7620 } 7621 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7622 /* XXX-LP */ 7623 if (e.error == EISDIR && crgetuid(cr) != 0) 7624 e.error = EPERM; 7625 goto out; 7626 } 7627 } 7628 7629 /* either no error or one of the postop getattr failed */ 7630 7631 /* 7632 * XXX - if LINK succeeded, but no attrs were returned for link 7633 * file, purge its cache. 7634 * 7635 * XXX Perform a simplified version of wcc checking. Instead of 7636 * have another getattr to get pre-op, just purge cache if 7637 * any of the ops prior to and including the getattr failed. 7638 * If the getattr succeeded then update the attrcache accordingly. 7639 */ 7640 7641 /* 7642 * update cache with link file postattrs. 7643 * Note: at this point resop points to link res. 7644 */ 7645 resop = &res.array[3]; /* link res */ 7646 ln_res = &resop->nfs_resop4_u.oplink; 7647 if (res.status == NFS4_OK) 7648 e.error = nfs4_update_attrcache(res.status, 7649 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7650 t, svp, cr); 7651 7652 /* 7653 * Call makenfs4node to create the new shadow vp for tnm. 7654 * We pass NULL attrs because we just cached attrs for 7655 * the src object. All we're trying to accomplish is to 7656 * to create the new shadow vnode. 7657 */ 7658 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7659 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh)); 7660 7661 /* Update target cache attribute, readdir and dnlc caches */ 7662 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7663 dinfo.di_time_call = t; 7664 dinfo.di_cred = cr; 7665 7666 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7667 ASSERT(nfs4_consistent_type(tdvp)); 7668 ASSERT(nfs4_consistent_type(svp)); 7669 ASSERT(nfs4_consistent_type(nvp)); 7670 VN_RELE(nvp); 7671 7672 if (!e.error) { 7673 vnode_t *tvp; 7674 rnode4_t *trp; 7675 /* 7676 * Notify the source file of this link operation. 7677 */ 7678 trp = VTOR4(svp); 7679 tvp = svp; 7680 if (IS_SHADOW(svp, trp)) 7681 tvp = RTOV4(trp); 7682 vnevent_link(tvp, ct); 7683 } 7684 out: 7685 kmem_free(argop, argoplist_size); 7686 if (resp) 7687 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7688 7689 nfs_rw_exit(&tdrp->r_rwlock); 7690 7691 return (e.error); 7692 } 7693 7694 /* ARGSUSED */ 7695 static int 7696 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7697 caller_context_t *ct, int flags) 7698 { 7699 vnode_t *realvp; 7700 7701 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7702 return (EPERM); 7703 if (VOP_REALVP(ndvp, &realvp, ct) == 0) 7704 ndvp = realvp; 7705 7706 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct)); 7707 } 7708 7709 /* 7710 * nfs4rename does the real work of renaming in NFS Version 4. 7711 * 7712 * A file handle is considered volatile for renaming purposes if either 7713 * of the volatile bits are turned on. However, the compound may differ 7714 * based on the likelihood of the filehandle to change during rename. 7715 */ 7716 static int 7717 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7718 caller_context_t *ct) 7719 { 7720 int error; 7721 mntinfo4_t *mi; 7722 vnode_t *nvp = NULL; 7723 vnode_t *ovp = NULL; 7724 char *tmpname = NULL; 7725 rnode4_t *rp; 7726 rnode4_t *odrp; 7727 rnode4_t *ndrp; 7728 int did_link = 0; 7729 int do_link = 1; 7730 nfsstat4 stat = NFS4_OK; 7731 7732 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7733 ASSERT(nfs4_consistent_type(odvp)); 7734 ASSERT(nfs4_consistent_type(ndvp)); 7735 7736 if (onm[0] == '.' && (onm[1] == '\0' || 7737 (onm[1] == '.' && onm[2] == '\0'))) 7738 return (EINVAL); 7739 7740 if (nnm[0] == '.' && (nnm[1] == '\0' || 7741 (nnm[1] == '.' && nnm[2] == '\0'))) 7742 return (EINVAL); 7743 7744 odrp = VTOR4(odvp); 7745 ndrp = VTOR4(ndvp); 7746 if ((intptr_t)odrp < (intptr_t)ndrp) { 7747 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7748 return (EINTR); 7749 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7750 nfs_rw_exit(&odrp->r_rwlock); 7751 return (EINTR); 7752 } 7753 } else { 7754 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7755 return (EINTR); 7756 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7757 nfs_rw_exit(&ndrp->r_rwlock); 7758 return (EINTR); 7759 } 7760 } 7761 7762 /* 7763 * Lookup the target file. If it exists, it needs to be 7764 * checked to see whether it is a mount point and whether 7765 * it is active (open). 7766 */ 7767 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7768 if (!error) { 7769 int isactive; 7770 7771 ASSERT(nfs4_consistent_type(nvp)); 7772 /* 7773 * If this file has been mounted on, then just 7774 * return busy because renaming to it would remove 7775 * the mounted file system from the name space. 7776 */ 7777 if (vn_ismntpt(nvp)) { 7778 VN_RELE(nvp); 7779 nfs_rw_exit(&odrp->r_rwlock); 7780 nfs_rw_exit(&ndrp->r_rwlock); 7781 return (EBUSY); 7782 } 7783 7784 /* 7785 * First just remove the entry from the name cache, as it 7786 * is most likely the only entry for this vp. 7787 */ 7788 dnlc_remove(ndvp, nnm); 7789 7790 rp = VTOR4(nvp); 7791 7792 if (nvp->v_type != VREG) { 7793 /* 7794 * Purge the name cache of all references to this vnode 7795 * so that we can check the reference count to infer 7796 * whether it is active or not. 7797 */ 7798 if (nvp->v_count > 1) 7799 dnlc_purge_vp(nvp); 7800 7801 isactive = nvp->v_count > 1; 7802 } else { 7803 mutex_enter(&rp->r_os_lock); 7804 isactive = list_head(&rp->r_open_streams) != NULL; 7805 mutex_exit(&rp->r_os_lock); 7806 } 7807 7808 /* 7809 * If the vnode is active and is not a directory, 7810 * arrange to rename it to a 7811 * temporary file so that it will continue to be 7812 * accessible. This implements the "unlink-open-file" 7813 * semantics for the target of a rename operation. 7814 * Before doing this though, make sure that the 7815 * source and target files are not already the same. 7816 */ 7817 if (isactive && nvp->v_type != VDIR) { 7818 /* 7819 * Lookup the source name. 7820 */ 7821 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7822 7823 /* 7824 * The source name *should* already exist. 7825 */ 7826 if (error) { 7827 VN_RELE(nvp); 7828 nfs_rw_exit(&odrp->r_rwlock); 7829 nfs_rw_exit(&ndrp->r_rwlock); 7830 return (error); 7831 } 7832 7833 ASSERT(nfs4_consistent_type(ovp)); 7834 7835 /* 7836 * Compare the two vnodes. If they are the same, 7837 * just release all held vnodes and return success. 7838 */ 7839 if (VN_CMP(ovp, nvp)) { 7840 VN_RELE(ovp); 7841 VN_RELE(nvp); 7842 nfs_rw_exit(&odrp->r_rwlock); 7843 nfs_rw_exit(&ndrp->r_rwlock); 7844 return (0); 7845 } 7846 7847 /* 7848 * Can't mix and match directories and non- 7849 * directories in rename operations. We already 7850 * know that the target is not a directory. If 7851 * the source is a directory, return an error. 7852 */ 7853 if (ovp->v_type == VDIR) { 7854 VN_RELE(ovp); 7855 VN_RELE(nvp); 7856 nfs_rw_exit(&odrp->r_rwlock); 7857 nfs_rw_exit(&ndrp->r_rwlock); 7858 return (ENOTDIR); 7859 } 7860 link_call: 7861 /* 7862 * The target file exists, is not the same as 7863 * the source file, and is active. We first 7864 * try to Link it to a temporary filename to 7865 * avoid having the server removing the file 7866 * completely (which could cause data loss to 7867 * the user's POV in the event the Rename fails 7868 * -- see bug 1165874). 7869 */ 7870 /* 7871 * The do_link and did_link booleans are 7872 * introduced in the event we get NFS4ERR_FILE_OPEN 7873 * returned for the Rename. Some servers can 7874 * not Rename over an Open file, so they return 7875 * this error. The client needs to Remove the 7876 * newly created Link and do two Renames, just 7877 * as if the server didn't support LINK. 7878 */ 7879 tmpname = newname(); 7880 error = 0; 7881 7882 if (do_link) { 7883 error = nfs4_link(ndvp, nvp, tmpname, cr, 7884 NULL, 0); 7885 } 7886 if (error == EOPNOTSUPP || !do_link) { 7887 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7888 cr, NULL, 0); 7889 did_link = 0; 7890 } else { 7891 did_link = 1; 7892 } 7893 if (error) { 7894 kmem_free(tmpname, MAXNAMELEN); 7895 VN_RELE(ovp); 7896 VN_RELE(nvp); 7897 nfs_rw_exit(&odrp->r_rwlock); 7898 nfs_rw_exit(&ndrp->r_rwlock); 7899 return (error); 7900 } 7901 7902 mutex_enter(&rp->r_statelock); 7903 if (rp->r_unldvp == NULL) { 7904 VN_HOLD(ndvp); 7905 rp->r_unldvp = ndvp; 7906 if (rp->r_unlcred != NULL) 7907 crfree(rp->r_unlcred); 7908 crhold(cr); 7909 rp->r_unlcred = cr; 7910 rp->r_unlname = tmpname; 7911 } else { 7912 if (rp->r_unlname) 7913 kmem_free(rp->r_unlname, MAXNAMELEN); 7914 rp->r_unlname = tmpname; 7915 } 7916 mutex_exit(&rp->r_statelock); 7917 } 7918 7919 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7920 7921 ASSERT(nfs4_consistent_type(nvp)); 7922 } 7923 7924 if (ovp == NULL) { 7925 /* 7926 * When renaming directories to be a subdirectory of a 7927 * different parent, the dnlc entry for ".." will no 7928 * longer be valid, so it must be removed. 7929 * 7930 * We do a lookup here to determine whether we are renaming 7931 * a directory and we need to check if we are renaming 7932 * an unlinked file. This might have already been done 7933 * in previous code, so we check ovp == NULL to avoid 7934 * doing it twice. 7935 */ 7936 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7937 /* 7938 * The source name *should* already exist. 7939 */ 7940 if (error) { 7941 nfs_rw_exit(&odrp->r_rwlock); 7942 nfs_rw_exit(&ndrp->r_rwlock); 7943 if (nvp) { 7944 VN_RELE(nvp); 7945 } 7946 return (error); 7947 } 7948 ASSERT(ovp != NULL); 7949 ASSERT(nfs4_consistent_type(ovp)); 7950 } 7951 7952 /* 7953 * Is the object being renamed a dir, and if so, is 7954 * it being renamed to a child of itself? The underlying 7955 * fs should ultimately return EINVAL for this case; 7956 * however, buggy beta non-Solaris NFSv4 servers at 7957 * interop testing events have allowed this behavior, 7958 * and it caused our client to panic due to a recursive 7959 * mutex_enter in fn_move. 7960 * 7961 * The tedious locking in fn_move could be changed to 7962 * deal with this case, and the client could avoid the 7963 * panic; however, the client would just confuse itself 7964 * later and misbehave. A better way to handle the broken 7965 * server is to detect this condition and return EINVAL 7966 * without ever sending the the bogus rename to the server. 7967 * We know the rename is invalid -- just fail it now. 7968 */ 7969 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7970 VN_RELE(ovp); 7971 nfs_rw_exit(&odrp->r_rwlock); 7972 nfs_rw_exit(&ndrp->r_rwlock); 7973 if (nvp) { 7974 VN_RELE(nvp); 7975 } 7976 return (EINVAL); 7977 } 7978 7979 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7980 7981 /* 7982 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7983 * possible for the filehandle to change due to the rename. 7984 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7985 * the fh will not change because of the rename, but we still need 7986 * to update its rnode entry with the new name for 7987 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7988 * has no effect on these for now, but for future improvements, 7989 * we might want to use it too to simplify handling of files 7990 * that are open with that flag on. (XXX) 7991 */ 7992 mi = VTOMI4(odvp); 7993 if (NFS4_VOLATILE_FH(mi)) 7994 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7995 &stat); 7996 else 7997 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7998 &stat); 7999 8000 ASSERT(nfs4_consistent_type(odvp)); 8001 ASSERT(nfs4_consistent_type(ndvp)); 8002 ASSERT(nfs4_consistent_type(ovp)); 8003 8004 if (stat == NFS4ERR_FILE_OPEN && did_link) { 8005 do_link = 0; 8006 /* 8007 * Before the 'link_call' code, we did a nfs4_lookup 8008 * that puts a VN_HOLD on nvp. After the nfs4_link 8009 * call we call VN_RELE to match that hold. We need 8010 * to place an additional VN_HOLD here since we will 8011 * be hitting that VN_RELE again. 8012 */ 8013 VN_HOLD(nvp); 8014 8015 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0); 8016 8017 /* Undo the unlinked file naming stuff we just did */ 8018 mutex_enter(&rp->r_statelock); 8019 if (rp->r_unldvp) { 8020 VN_RELE(ndvp); 8021 rp->r_unldvp = NULL; 8022 if (rp->r_unlcred != NULL) 8023 crfree(rp->r_unlcred); 8024 rp->r_unlcred = NULL; 8025 /* rp->r_unlanme points to tmpname */ 8026 if (rp->r_unlname) 8027 kmem_free(rp->r_unlname, MAXNAMELEN); 8028 rp->r_unlname = NULL; 8029 } 8030 mutex_exit(&rp->r_statelock); 8031 8032 if (nvp) { 8033 VN_RELE(nvp); 8034 } 8035 goto link_call; 8036 } 8037 8038 if (error) { 8039 VN_RELE(ovp); 8040 nfs_rw_exit(&odrp->r_rwlock); 8041 nfs_rw_exit(&ndrp->r_rwlock); 8042 if (nvp) { 8043 VN_RELE(nvp); 8044 } 8045 return (error); 8046 } 8047 8048 /* 8049 * when renaming directories to be a subdirectory of a 8050 * different parent, the dnlc entry for ".." will no 8051 * longer be valid, so it must be removed 8052 */ 8053 rp = VTOR4(ovp); 8054 if (ndvp != odvp) { 8055 if (ovp->v_type == VDIR) { 8056 dnlc_remove(ovp, ".."); 8057 if (rp->r_dir != NULL) 8058 nfs4_purge_rddir_cache(ovp); 8059 } 8060 } 8061 8062 /* 8063 * If we are renaming the unlinked file, update the 8064 * r_unldvp and r_unlname as needed. 8065 */ 8066 mutex_enter(&rp->r_statelock); 8067 if (rp->r_unldvp != NULL) { 8068 if (strcmp(rp->r_unlname, onm) == 0) { 8069 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 8070 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 8071 if (ndvp != rp->r_unldvp) { 8072 VN_RELE(rp->r_unldvp); 8073 rp->r_unldvp = ndvp; 8074 VN_HOLD(ndvp); 8075 } 8076 } 8077 } 8078 mutex_exit(&rp->r_statelock); 8079 8080 /* 8081 * Notify the rename vnevents to source vnode, and to the target 8082 * vnode if it already existed. 8083 */ 8084 if (error == 0) { 8085 vnode_t *tvp; 8086 rnode4_t *trp; 8087 /* 8088 * Notify the vnode. Each links is represented by 8089 * a different vnode, in nfsv4. 8090 */ 8091 if (nvp) { 8092 trp = VTOR4(nvp); 8093 tvp = nvp; 8094 if (IS_SHADOW(nvp, trp)) 8095 tvp = RTOV4(trp); 8096 vnevent_rename_dest(tvp, ndvp, nnm, ct); 8097 } 8098 8099 /* 8100 * if the source and destination directory are not the 8101 * same notify the destination directory. 8102 */ 8103 if (VTOR4(odvp) != VTOR4(ndvp)) { 8104 trp = VTOR4(ndvp); 8105 tvp = ndvp; 8106 if (IS_SHADOW(ndvp, trp)) 8107 tvp = RTOV4(trp); 8108 vnevent_rename_dest_dir(tvp, ct); 8109 } 8110 8111 trp = VTOR4(ovp); 8112 tvp = ovp; 8113 if (IS_SHADOW(ovp, trp)) 8114 tvp = RTOV4(trp); 8115 vnevent_rename_src(tvp, odvp, onm, ct); 8116 } 8117 8118 if (nvp) { 8119 VN_RELE(nvp); 8120 } 8121 VN_RELE(ovp); 8122 8123 nfs_rw_exit(&odrp->r_rwlock); 8124 nfs_rw_exit(&ndrp->r_rwlock); 8125 8126 return (error); 8127 } 8128 8129 /* 8130 * When the parent directory has changed, sv_dfh must be updated 8131 */ 8132 static void 8133 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp) 8134 { 8135 svnode_t *sv = VTOSV(vp); 8136 nfs4_sharedfh_t *old_dfh = sv->sv_dfh; 8137 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh; 8138 8139 sfh4_hold(new_dfh); 8140 sv->sv_dfh = new_dfh; 8141 sfh4_rele(&old_dfh); 8142 } 8143 8144 /* 8145 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 8146 * when it is known that the filehandle is persistent through rename. 8147 * 8148 * Rename requires that the current fh be the target directory and the 8149 * saved fh be the source directory. After the operation, the current fh 8150 * is unchanged. 8151 * The compound op structure for persistent fh rename is: 8152 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 8153 * Rather than bother with the directory postop args, we'll simply 8154 * update that a change occurred in the cache, so no post-op getattrs. 8155 */ 8156 static int 8157 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 8158 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8159 { 8160 COMPOUND4args_clnt args; 8161 COMPOUND4res_clnt res, *resp = NULL; 8162 nfs_argop4 *argop; 8163 nfs_resop4 *resop; 8164 int doqueue, argoplist_size; 8165 mntinfo4_t *mi; 8166 rnode4_t *odrp = VTOR4(odvp); 8167 rnode4_t *ndrp = VTOR4(ndvp); 8168 RENAME4res *rn_res; 8169 bool_t needrecov; 8170 nfs4_recov_state_t recov_state; 8171 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8172 dirattr_info_t dinfo, *dinfop; 8173 8174 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8175 8176 recov_state.rs_flags = 0; 8177 recov_state.rs_num_retry_despite_err = 0; 8178 8179 /* 8180 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 8181 * 8182 * If source/target are different dirs, then append putfh(src); getattr 8183 */ 8184 args.array_len = (odvp == ndvp) ? 5 : 7; 8185 argoplist_size = args.array_len * sizeof (nfs_argop4); 8186 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8187 8188 recov_retry: 8189 *statp = NFS4_OK; 8190 8191 /* No need to Lookup the file, persistent fh */ 8192 args.ctag = TAG_RENAME; 8193 8194 mi = VTOMI4(odvp); 8195 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8196 if (e.error) { 8197 kmem_free(argop, argoplist_size); 8198 return (e.error); 8199 } 8200 8201 /* 0: putfh source directory */ 8202 argop[0].argop = OP_CPUTFH; 8203 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8204 8205 /* 1: Save source fh to free up current for target */ 8206 argop[1].argop = OP_SAVEFH; 8207 8208 /* 2: putfh targetdir */ 8209 argop[2].argop = OP_CPUTFH; 8210 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8211 8212 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8213 argop[3].argop = OP_CRENAME; 8214 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8215 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8216 8217 /* 4: getattr (targetdir) */ 8218 argop[4].argop = OP_GETATTR; 8219 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8220 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8221 8222 if (ndvp != odvp) { 8223 8224 /* 5: putfh (sourcedir) */ 8225 argop[5].argop = OP_CPUTFH; 8226 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8227 8228 /* 6: getattr (sourcedir) */ 8229 argop[6].argop = OP_GETATTR; 8230 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8231 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8232 } 8233 8234 dnlc_remove(odvp, onm); 8235 dnlc_remove(ndvp, nnm); 8236 8237 doqueue = 1; 8238 dinfo.di_time_call = gethrtime(); 8239 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8240 8241 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8242 if (e.error) { 8243 PURGE_ATTRCACHE4(odvp); 8244 PURGE_ATTRCACHE4(ndvp); 8245 } else { 8246 *statp = res.status; 8247 } 8248 8249 if (needrecov) { 8250 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8251 OP_RENAME, NULL, NULL, NULL) == FALSE) { 8252 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8253 if (!e.error) 8254 (void) xdr_free(xdr_COMPOUND4res_clnt, 8255 (caddr_t)&res); 8256 goto recov_retry; 8257 } 8258 } 8259 8260 if (!e.error) { 8261 resp = &res; 8262 /* 8263 * as long as OP_RENAME 8264 */ 8265 if (res.status != NFS4_OK && res.array_len <= 4) { 8266 e.error = geterrno4(res.status); 8267 PURGE_ATTRCACHE4(odvp); 8268 PURGE_ATTRCACHE4(ndvp); 8269 /* 8270 * System V defines rename to return EEXIST, not 8271 * ENOTEMPTY if the target directory is not empty. 8272 * Over the wire, the error is NFSERR_ENOTEMPTY 8273 * which geterrno4 maps to ENOTEMPTY. 8274 */ 8275 if (e.error == ENOTEMPTY) 8276 e.error = EEXIST; 8277 } else { 8278 8279 resop = &res.array[3]; /* rename res */ 8280 rn_res = &resop->nfs_resop4_u.oprename; 8281 8282 if (res.status == NFS4_OK) { 8283 /* 8284 * Update target attribute, readdir and dnlc 8285 * caches. 8286 */ 8287 dinfo.di_garp = 8288 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8289 dinfo.di_cred = cr; 8290 dinfop = &dinfo; 8291 } else 8292 dinfop = NULL; 8293 8294 nfs4_update_dircaches(&rn_res->target_cinfo, 8295 ndvp, NULL, NULL, dinfop); 8296 8297 /* 8298 * Update source attribute, readdir and dnlc caches 8299 * 8300 */ 8301 if (ndvp != odvp) { 8302 update_parentdir_sfh(renvp, ndvp); 8303 8304 if (dinfop) 8305 dinfo.di_garp = 8306 &(res.array[6].nfs_resop4_u. 8307 opgetattr.ga_res); 8308 8309 nfs4_update_dircaches(&rn_res->source_cinfo, 8310 odvp, NULL, NULL, dinfop); 8311 } 8312 8313 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8314 nnm); 8315 } 8316 } 8317 8318 if (resp) 8319 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8320 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8321 kmem_free(argop, argoplist_size); 8322 8323 return (e.error); 8324 } 8325 8326 /* 8327 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8328 * it is possible for the filehandle to change due to the rename. 8329 * 8330 * The compound req in this case includes a post-rename lookup and getattr 8331 * to ensure that we have the correct fh and attributes for the object. 8332 * 8333 * Rename requires that the current fh be the target directory and the 8334 * saved fh be the source directory. After the operation, the current fh 8335 * is unchanged. 8336 * 8337 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8338 * update the filehandle for the renamed object. We also get the old 8339 * filehandle for historical reasons; this should be taken out sometime. 8340 * This results in a rather cumbersome compound... 8341 * 8342 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8343 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8344 * 8345 */ 8346 static int 8347 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8348 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8349 { 8350 COMPOUND4args_clnt args; 8351 COMPOUND4res_clnt res, *resp = NULL; 8352 int argoplist_size; 8353 nfs_argop4 *argop; 8354 nfs_resop4 *resop; 8355 int doqueue; 8356 mntinfo4_t *mi; 8357 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8358 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8359 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8360 RENAME4res *rn_res; 8361 GETFH4res *ngf_res; 8362 bool_t needrecov; 8363 nfs4_recov_state_t recov_state; 8364 hrtime_t t; 8365 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8366 dirattr_info_t dinfo, *dinfop = &dinfo; 8367 8368 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8369 8370 recov_state.rs_flags = 0; 8371 recov_state.rs_num_retry_despite_err = 0; 8372 8373 recov_retry: 8374 *statp = NFS4_OK; 8375 8376 /* 8377 * There is a window between the RPC and updating the path and 8378 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8379 * code, so that it doesn't try to use the old path during that 8380 * window. 8381 */ 8382 mutex_enter(&orp->r_statelock); 8383 while (orp->r_flags & R4RECEXPFH) { 8384 klwp_t *lwp = ttolwp(curthread); 8385 8386 if (lwp != NULL) 8387 lwp->lwp_nostop++; 8388 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8389 mutex_exit(&orp->r_statelock); 8390 if (lwp != NULL) 8391 lwp->lwp_nostop--; 8392 return (EINTR); 8393 } 8394 if (lwp != NULL) 8395 lwp->lwp_nostop--; 8396 } 8397 orp->r_flags |= R4RECEXPFH; 8398 mutex_exit(&orp->r_statelock); 8399 8400 mi = VTOMI4(odvp); 8401 8402 args.ctag = TAG_RENAME_VFH; 8403 args.array_len = (odvp == ndvp) ? 10 : 12; 8404 argoplist_size = args.array_len * sizeof (nfs_argop4); 8405 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8406 8407 /* 8408 * Rename ops: 8409 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8410 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8411 * LOOKUP(trgt), GETFH(new), GETATTR, 8412 * 8413 * if (odvp != ndvp) 8414 * add putfh(sourcedir), getattr(sourcedir) } 8415 */ 8416 args.array = argop; 8417 8418 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8419 &recov_state, NULL); 8420 if (e.error) { 8421 kmem_free(argop, argoplist_size); 8422 mutex_enter(&orp->r_statelock); 8423 orp->r_flags &= ~R4RECEXPFH; 8424 cv_broadcast(&orp->r_cv); 8425 mutex_exit(&orp->r_statelock); 8426 return (e.error); 8427 } 8428 8429 /* 0: putfh source directory */ 8430 argop[0].argop = OP_CPUTFH; 8431 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8432 8433 /* 1: Save source fh to free up current for target */ 8434 argop[1].argop = OP_SAVEFH; 8435 8436 /* 2: Lookup pre-rename fh of renamed object */ 8437 argop[2].argop = OP_CLOOKUP; 8438 argop[2].nfs_argop4_u.opclookup.cname = onm; 8439 8440 /* 3: getfh fh of renamed object (before rename) */ 8441 argop[3].argop = OP_GETFH; 8442 8443 /* 4: putfh targetdir */ 8444 argop[4].argop = OP_CPUTFH; 8445 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8446 8447 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8448 argop[5].argop = OP_CRENAME; 8449 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8450 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8451 8452 /* 6: getattr of target dir (post op attrs) */ 8453 argop[6].argop = OP_GETATTR; 8454 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8455 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8456 8457 /* 7: Lookup post-rename fh of renamed object */ 8458 argop[7].argop = OP_CLOOKUP; 8459 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8460 8461 /* 8: getfh fh of renamed object (after rename) */ 8462 argop[8].argop = OP_GETFH; 8463 8464 /* 9: getattr of renamed object */ 8465 argop[9].argop = OP_GETATTR; 8466 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8467 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8468 8469 /* 8470 * If source/target dirs are different, then get new post-op 8471 * attrs for source dir also. 8472 */ 8473 if (ndvp != odvp) { 8474 /* 10: putfh (sourcedir) */ 8475 argop[10].argop = OP_CPUTFH; 8476 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8477 8478 /* 11: getattr (sourcedir) */ 8479 argop[11].argop = OP_GETATTR; 8480 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8481 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8482 } 8483 8484 dnlc_remove(odvp, onm); 8485 dnlc_remove(ndvp, nnm); 8486 8487 doqueue = 1; 8488 t = gethrtime(); 8489 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8490 8491 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8492 if (e.error) { 8493 PURGE_ATTRCACHE4(odvp); 8494 PURGE_ATTRCACHE4(ndvp); 8495 if (!needrecov) { 8496 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8497 &recov_state, needrecov); 8498 goto out; 8499 } 8500 } else { 8501 *statp = res.status; 8502 } 8503 8504 if (needrecov) { 8505 bool_t abort; 8506 8507 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8508 OP_RENAME, NULL, NULL, NULL); 8509 if (abort == FALSE) { 8510 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8511 &recov_state, needrecov); 8512 kmem_free(argop, argoplist_size); 8513 if (!e.error) 8514 (void) xdr_free(xdr_COMPOUND4res_clnt, 8515 (caddr_t)&res); 8516 mutex_enter(&orp->r_statelock); 8517 orp->r_flags &= ~R4RECEXPFH; 8518 cv_broadcast(&orp->r_cv); 8519 mutex_exit(&orp->r_statelock); 8520 goto recov_retry; 8521 } else { 8522 if (e.error != 0) { 8523 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8524 &recov_state, needrecov); 8525 goto out; 8526 } 8527 /* fall through for res.status case */ 8528 } 8529 } 8530 8531 resp = &res; 8532 /* 8533 * If OP_RENAME (or any prev op) failed, then return an error. 8534 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8535 */ 8536 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8537 /* 8538 * Error in an op other than last Getattr 8539 */ 8540 e.error = geterrno4(res.status); 8541 PURGE_ATTRCACHE4(odvp); 8542 PURGE_ATTRCACHE4(ndvp); 8543 /* 8544 * System V defines rename to return EEXIST, not 8545 * ENOTEMPTY if the target directory is not empty. 8546 * Over the wire, the error is NFSERR_ENOTEMPTY 8547 * which geterrno4 maps to ENOTEMPTY. 8548 */ 8549 if (e.error == ENOTEMPTY) 8550 e.error = EEXIST; 8551 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8552 needrecov); 8553 goto out; 8554 } 8555 8556 /* rename results */ 8557 rn_res = &res.array[5].nfs_resop4_u.oprename; 8558 8559 if (res.status == NFS4_OK) { 8560 /* Update target attribute, readdir and dnlc caches */ 8561 dinfo.di_garp = 8562 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8563 dinfo.di_cred = cr; 8564 dinfo.di_time_call = t; 8565 } else 8566 dinfop = NULL; 8567 8568 /* Update source cache attribute, readdir and dnlc caches */ 8569 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8570 8571 /* Update source cache attribute, readdir and dnlc caches */ 8572 if (ndvp != odvp) { 8573 update_parentdir_sfh(ovp, ndvp); 8574 8575 /* 8576 * If dinfop is non-NULL, then compound succeded, so 8577 * set di_garp to attrs for source dir. dinfop is only 8578 * set to NULL when compound fails. 8579 */ 8580 if (dinfop) 8581 dinfo.di_garp = 8582 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8583 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8584 dinfop); 8585 } 8586 8587 /* 8588 * Update the rnode with the new component name and args, 8589 * and if the file handle changed, also update it with the new fh. 8590 * This is only necessary if the target object has an rnode 8591 * entry and there is no need to create one for it. 8592 */ 8593 resop = &res.array[8]; /* getfh new res */ 8594 ngf_res = &resop->nfs_resop4_u.opgetfh; 8595 8596 /* 8597 * Update the path and filehandle for the renamed object. 8598 */ 8599 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8600 8601 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8602 8603 if (res.status == NFS4_OK) { 8604 resop++; /* getattr res */ 8605 e.error = nfs4_update_attrcache(res.status, 8606 &resop->nfs_resop4_u.opgetattr.ga_res, 8607 t, ovp, cr); 8608 } 8609 8610 out: 8611 kmem_free(argop, argoplist_size); 8612 if (resp) 8613 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8614 mutex_enter(&orp->r_statelock); 8615 orp->r_flags &= ~R4RECEXPFH; 8616 cv_broadcast(&orp->r_cv); 8617 mutex_exit(&orp->r_statelock); 8618 8619 return (e.error); 8620 } 8621 8622 /* ARGSUSED */ 8623 static int 8624 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr, 8625 caller_context_t *ct, int flags, vsecattr_t *vsecp) 8626 { 8627 int error; 8628 vnode_t *vp; 8629 8630 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8631 return (EPERM); 8632 /* 8633 * As ".." has special meaning and rather than send a mkdir 8634 * over the wire to just let the server freak out, we just 8635 * short circuit it here and return EEXIST 8636 */ 8637 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8638 return (EEXIST); 8639 8640 /* 8641 * Decision to get the right gid and setgid bit of the 8642 * new directory is now made in call_nfs4_create_req. 8643 */ 8644 va->va_mask |= AT_MODE; 8645 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8646 if (error) 8647 return (error); 8648 8649 *vpp = vp; 8650 return (0); 8651 } 8652 8653 8654 /* 8655 * rmdir is using the same remove v4 op as does remove. 8656 * Remove requires that the current fh be the target directory. 8657 * After the operation, the current fh is unchanged. 8658 * The compound op structure is: 8659 * PUTFH(targetdir), REMOVE 8660 */ 8661 /*ARGSUSED4*/ 8662 static int 8663 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr, 8664 caller_context_t *ct, int flags) 8665 { 8666 int need_end_op = FALSE; 8667 COMPOUND4args_clnt args; 8668 COMPOUND4res_clnt res, *resp = NULL; 8669 REMOVE4res *rm_res; 8670 nfs_argop4 argop[3]; 8671 nfs_resop4 *resop; 8672 vnode_t *vp; 8673 int doqueue; 8674 mntinfo4_t *mi; 8675 rnode4_t *drp; 8676 bool_t needrecov = FALSE; 8677 nfs4_recov_state_t recov_state; 8678 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8679 dirattr_info_t dinfo, *dinfop; 8680 8681 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8682 return (EPERM); 8683 /* 8684 * As ".." has special meaning and rather than send a rmdir 8685 * over the wire to just let the server freak out, we just 8686 * short circuit it here and return EEXIST 8687 */ 8688 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8689 return (EEXIST); 8690 8691 drp = VTOR4(dvp); 8692 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8693 return (EINTR); 8694 8695 /* 8696 * Attempt to prevent a rmdir(".") from succeeding. 8697 */ 8698 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8699 if (e.error) { 8700 nfs_rw_exit(&drp->r_rwlock); 8701 return (e.error); 8702 } 8703 if (vp == cdir) { 8704 VN_RELE(vp); 8705 nfs_rw_exit(&drp->r_rwlock); 8706 return (EINVAL); 8707 } 8708 8709 /* 8710 * Since nfsv4 remove op works on both files and directories, 8711 * check that the removed object is indeed a directory. 8712 */ 8713 if (vp->v_type != VDIR) { 8714 VN_RELE(vp); 8715 nfs_rw_exit(&drp->r_rwlock); 8716 return (ENOTDIR); 8717 } 8718 8719 /* 8720 * First just remove the entry from the name cache, as it 8721 * is most likely an entry for this vp. 8722 */ 8723 dnlc_remove(dvp, nm); 8724 8725 /* 8726 * If there vnode reference count is greater than one, then 8727 * there may be additional references in the DNLC which will 8728 * need to be purged. First, trying removing the entry for 8729 * the parent directory and see if that removes the additional 8730 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8731 * to completely remove any references to the directory which 8732 * might still exist in the DNLC. 8733 */ 8734 if (vp->v_count > 1) { 8735 dnlc_remove(vp, ".."); 8736 if (vp->v_count > 1) 8737 dnlc_purge_vp(vp); 8738 } 8739 8740 mi = VTOMI4(dvp); 8741 recov_state.rs_flags = 0; 8742 recov_state.rs_num_retry_despite_err = 0; 8743 8744 recov_retry: 8745 args.ctag = TAG_RMDIR; 8746 8747 /* 8748 * Rmdir ops: putfh dir; remove 8749 */ 8750 args.array_len = 3; 8751 args.array = argop; 8752 8753 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8754 if (e.error) { 8755 nfs_rw_exit(&drp->r_rwlock); 8756 return (e.error); 8757 } 8758 need_end_op = TRUE; 8759 8760 /* putfh directory */ 8761 argop[0].argop = OP_CPUTFH; 8762 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8763 8764 /* remove */ 8765 argop[1].argop = OP_CREMOVE; 8766 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8767 8768 /* getattr (postop attrs for dir that contained removed dir) */ 8769 argop[2].argop = OP_GETATTR; 8770 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8771 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8772 8773 dinfo.di_time_call = gethrtime(); 8774 doqueue = 1; 8775 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8776 8777 PURGE_ATTRCACHE4(vp); 8778 8779 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8780 if (e.error) { 8781 PURGE_ATTRCACHE4(dvp); 8782 } 8783 8784 if (needrecov) { 8785 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8786 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 8787 if (!e.error) 8788 (void) xdr_free(xdr_COMPOUND4res_clnt, 8789 (caddr_t)&res); 8790 8791 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8792 needrecov); 8793 need_end_op = FALSE; 8794 goto recov_retry; 8795 } 8796 } 8797 8798 if (!e.error) { 8799 resp = &res; 8800 8801 /* 8802 * Only return error if first 2 ops (OP_REMOVE or earlier) 8803 * failed. 8804 */ 8805 if (res.status != NFS4_OK && res.array_len <= 2) { 8806 e.error = geterrno4(res.status); 8807 PURGE_ATTRCACHE4(dvp); 8808 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8809 &recov_state, needrecov); 8810 need_end_op = FALSE; 8811 nfs4_purge_stale_fh(e.error, dvp, cr); 8812 /* 8813 * System V defines rmdir to return EEXIST, not 8814 * ENOTEMPTY if the directory is not empty. Over 8815 * the wire, the error is NFSERR_ENOTEMPTY which 8816 * geterrno4 maps to ENOTEMPTY. 8817 */ 8818 if (e.error == ENOTEMPTY) 8819 e.error = EEXIST; 8820 } else { 8821 resop = &res.array[1]; /* remove res */ 8822 rm_res = &resop->nfs_resop4_u.opremove; 8823 8824 if (res.status == NFS4_OK) { 8825 resop = &res.array[2]; /* dir attrs */ 8826 dinfo.di_garp = 8827 &resop->nfs_resop4_u.opgetattr.ga_res; 8828 dinfo.di_cred = cr; 8829 dinfop = &dinfo; 8830 } else 8831 dinfop = NULL; 8832 8833 /* Update dir attribute, readdir and dnlc caches */ 8834 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8835 dinfop); 8836 8837 /* destroy rddir cache for dir that was removed */ 8838 if (VTOR4(vp)->r_dir != NULL) 8839 nfs4_purge_rddir_cache(vp); 8840 } 8841 } 8842 8843 if (need_end_op) 8844 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8845 8846 nfs_rw_exit(&drp->r_rwlock); 8847 8848 if (resp) 8849 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8850 8851 if (e.error == 0) { 8852 vnode_t *tvp; 8853 rnode4_t *trp; 8854 trp = VTOR4(vp); 8855 tvp = vp; 8856 if (IS_SHADOW(vp, trp)) 8857 tvp = RTOV4(trp); 8858 vnevent_rmdir(tvp, dvp, nm, ct); 8859 } 8860 8861 VN_RELE(vp); 8862 8863 return (e.error); 8864 } 8865 8866 /* ARGSUSED */ 8867 static int 8868 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr, 8869 caller_context_t *ct, int flags) 8870 { 8871 int error; 8872 vnode_t *vp; 8873 rnode4_t *rp; 8874 char *contents; 8875 mntinfo4_t *mi = VTOMI4(dvp); 8876 8877 if (nfs_zone() != mi->mi_zone) 8878 return (EPERM); 8879 if (!(mi->mi_flags & MI4_SYMLINK)) 8880 return (EOPNOTSUPP); 8881 8882 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8883 if (error) 8884 return (error); 8885 8886 ASSERT(nfs4_consistent_type(vp)); 8887 rp = VTOR4(vp); 8888 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8889 8890 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8891 8892 if (contents != NULL) { 8893 mutex_enter(&rp->r_statelock); 8894 if (rp->r_symlink.contents == NULL) { 8895 rp->r_symlink.len = strlen(tnm); 8896 bcopy(tnm, contents, rp->r_symlink.len); 8897 rp->r_symlink.contents = contents; 8898 rp->r_symlink.size = MAXPATHLEN; 8899 mutex_exit(&rp->r_statelock); 8900 } else { 8901 mutex_exit(&rp->r_statelock); 8902 kmem_free((void *)contents, MAXPATHLEN); 8903 } 8904 } 8905 } 8906 VN_RELE(vp); 8907 8908 return (error); 8909 } 8910 8911 8912 /* 8913 * Read directory entries. 8914 * There are some weird things to look out for here. The uio_loffset 8915 * field is either 0 or it is the offset returned from a previous 8916 * readdir. It is an opaque value used by the server to find the 8917 * correct directory block to read. The count field is the number 8918 * of blocks to read on the server. This is advisory only, the server 8919 * may return only one block's worth of entries. Entries may be compressed 8920 * on the server. 8921 */ 8922 /* ARGSUSED */ 8923 static int 8924 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp, 8925 caller_context_t *ct, int flags) 8926 { 8927 int error; 8928 uint_t count; 8929 rnode4_t *rp; 8930 rddir4_cache *rdc; 8931 rddir4_cache *rrdc; 8932 8933 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8934 return (EIO); 8935 rp = VTOR4(vp); 8936 8937 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8938 8939 /* 8940 * Make sure that the directory cache is valid. 8941 */ 8942 if (rp->r_dir != NULL) { 8943 if (nfs_disable_rddir_cache != 0) { 8944 /* 8945 * Setting nfs_disable_rddir_cache in /etc/system 8946 * allows interoperability with servers that do not 8947 * properly update the attributes of directories. 8948 * Any cached information gets purged before an 8949 * access is made to it. 8950 */ 8951 nfs4_purge_rddir_cache(vp); 8952 } 8953 8954 error = nfs4_validate_caches(vp, cr); 8955 if (error) 8956 return (error); 8957 } 8958 8959 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8960 8961 /* 8962 * Short circuit last readdir which always returns 0 bytes. 8963 * This can be done after the directory has been read through 8964 * completely at least once. This will set r_direof which 8965 * can be used to find the value of the last cookie. 8966 */ 8967 mutex_enter(&rp->r_statelock); 8968 if (rp->r_direof != NULL && 8969 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8970 mutex_exit(&rp->r_statelock); 8971 #ifdef DEBUG 8972 nfs4_readdir_cache_shorts++; 8973 #endif 8974 if (eofp) 8975 *eofp = 1; 8976 return (0); 8977 } 8978 8979 /* 8980 * Look for a cache entry. Cache entries are identified 8981 * by the NFS cookie value and the byte count requested. 8982 */ 8983 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8984 8985 /* 8986 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8987 */ 8988 if (rdc == NULL) { 8989 mutex_exit(&rp->r_statelock); 8990 return (EINTR); 8991 } 8992 8993 /* 8994 * Check to see if we need to fill this entry in. 8995 */ 8996 if (rdc->flags & RDDIRREQ) { 8997 rdc->flags &= ~RDDIRREQ; 8998 rdc->flags |= RDDIR; 8999 mutex_exit(&rp->r_statelock); 9000 9001 /* 9002 * Do the readdir. 9003 */ 9004 nfs4readdir(vp, rdc, cr); 9005 9006 /* 9007 * Reacquire the lock, so that we can continue 9008 */ 9009 mutex_enter(&rp->r_statelock); 9010 /* 9011 * The entry is now complete 9012 */ 9013 rdc->flags &= ~RDDIR; 9014 } 9015 9016 ASSERT(!(rdc->flags & RDDIR)); 9017 9018 /* 9019 * If an error occurred while attempting 9020 * to fill the cache entry, mark the entry invalid and 9021 * just return the error. 9022 */ 9023 if (rdc->error) { 9024 error = rdc->error; 9025 rdc->flags |= RDDIRREQ; 9026 rddir4_cache_rele(rp, rdc); 9027 mutex_exit(&rp->r_statelock); 9028 return (error); 9029 } 9030 9031 /* 9032 * The cache entry is complete and good, 9033 * copyout the dirent structs to the calling 9034 * thread. 9035 */ 9036 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 9037 9038 /* 9039 * If no error occurred during the copyout, 9040 * update the offset in the uio struct to 9041 * contain the value of the next NFS 4 cookie 9042 * and set the eof value appropriately. 9043 */ 9044 if (!error) { 9045 uiop->uio_loffset = rdc->nfs4_ncookie; 9046 if (eofp) 9047 *eofp = rdc->eof; 9048 } 9049 9050 /* 9051 * Decide whether to do readahead. Don't if we 9052 * have already read to the end of directory. 9053 */ 9054 if (rdc->eof) { 9055 /* 9056 * Make the entry the direof only if it is cached 9057 */ 9058 if (rdc->flags & RDDIRCACHED) 9059 rp->r_direof = rdc; 9060 rddir4_cache_rele(rp, rdc); 9061 mutex_exit(&rp->r_statelock); 9062 return (error); 9063 } 9064 9065 /* Determine if a readdir readahead should be done */ 9066 if (!(rp->r_flags & R4LOOKUP)) { 9067 rddir4_cache_rele(rp, rdc); 9068 mutex_exit(&rp->r_statelock); 9069 return (error); 9070 } 9071 9072 /* 9073 * Now look for a readahead entry. 9074 * 9075 * Check to see whether we found an entry for the readahead. 9076 * If so, we don't need to do anything further, so free the new 9077 * entry if one was allocated. Otherwise, allocate a new entry, add 9078 * it to the cache, and then initiate an asynchronous readdir 9079 * operation to fill it. 9080 */ 9081 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 9082 9083 /* 9084 * A readdir cache entry could not be obtained for the readahead. In 9085 * this case we skip the readahead and return. 9086 */ 9087 if (rrdc == NULL) { 9088 rddir4_cache_rele(rp, rdc); 9089 mutex_exit(&rp->r_statelock); 9090 return (error); 9091 } 9092 9093 /* 9094 * Check to see if we need to fill this entry in. 9095 */ 9096 if (rrdc->flags & RDDIRREQ) { 9097 rrdc->flags &= ~RDDIRREQ; 9098 rrdc->flags |= RDDIR; 9099 rddir4_cache_rele(rp, rdc); 9100 mutex_exit(&rp->r_statelock); 9101 #ifdef DEBUG 9102 nfs4_readdir_readahead++; 9103 #endif 9104 /* 9105 * Do the readdir. 9106 */ 9107 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 9108 return (error); 9109 } 9110 9111 rddir4_cache_rele(rp, rrdc); 9112 rddir4_cache_rele(rp, rdc); 9113 mutex_exit(&rp->r_statelock); 9114 return (error); 9115 } 9116 9117 static int 9118 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9119 { 9120 int error; 9121 rnode4_t *rp; 9122 9123 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9124 9125 rp = VTOR4(vp); 9126 9127 /* 9128 * Obtain the readdir results for the caller. 9129 */ 9130 nfs4readdir(vp, rdc, cr); 9131 9132 mutex_enter(&rp->r_statelock); 9133 /* 9134 * The entry is now complete 9135 */ 9136 rdc->flags &= ~RDDIR; 9137 9138 error = rdc->error; 9139 if (error) 9140 rdc->flags |= RDDIRREQ; 9141 rddir4_cache_rele(rp, rdc); 9142 mutex_exit(&rp->r_statelock); 9143 9144 return (error); 9145 } 9146 9147 /* 9148 * Read directory entries. 9149 * There are some weird things to look out for here. The uio_loffset 9150 * field is either 0 or it is the offset returned from a previous 9151 * readdir. It is an opaque value used by the server to find the 9152 * correct directory block to read. The count field is the number 9153 * of blocks to read on the server. This is advisory only, the server 9154 * may return only one block's worth of entries. Entries may be compressed 9155 * on the server. 9156 * 9157 * Generates the following compound request: 9158 * 1. If readdir offset is zero and no dnlc entry for parent exists, 9159 * must include a Lookupp as well. In this case, send: 9160 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 9161 * 2. Otherwise just do: { Putfh <fh>; Readdir } 9162 * 9163 * Get complete attributes and filehandles for entries if this is the 9164 * first read of the directory. Otherwise, just get fileid's. 9165 */ 9166 static void 9167 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9168 { 9169 COMPOUND4args_clnt args; 9170 COMPOUND4res_clnt res; 9171 READDIR4args *rargs; 9172 READDIR4res_clnt *rd_res; 9173 bitmap4 rd_bitsval; 9174 nfs_argop4 argop[5]; 9175 nfs_resop4 *resop; 9176 rnode4_t *rp = VTOR4(vp); 9177 mntinfo4_t *mi = VTOMI4(vp); 9178 int doqueue; 9179 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 9180 vnode_t *dvp; 9181 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 9182 int num_ops, res_opcnt; 9183 bool_t needrecov = FALSE; 9184 nfs4_recov_state_t recov_state; 9185 hrtime_t t; 9186 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 9187 9188 ASSERT(nfs_zone() == mi->mi_zone); 9189 ASSERT(rdc->flags & RDDIR); 9190 ASSERT(rdc->entries == NULL); 9191 9192 /* 9193 * If rp were a stub, it should have triggered and caused 9194 * a mount for us to get this far. 9195 */ 9196 ASSERT(!RP_ISSTUB(rp)); 9197 9198 num_ops = 2; 9199 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9200 /* 9201 * Since nfsv4 readdir may not return entries for "." and "..", 9202 * the client must recreate them: 9203 * To find the correct nodeid, do the following: 9204 * For current node, get nodeid from dnlc. 9205 * - if current node is rootvp, set pnodeid to nodeid. 9206 * - else if parent is in the dnlc, get its nodeid from there. 9207 * - else add LOOKUPP+GETATTR to compound. 9208 */ 9209 nodeid = rp->r_attr.va_nodeid; 9210 if (vp->v_flag & VROOT) { 9211 pnodeid = nodeid; /* root of mount point */ 9212 } else { 9213 dvp = dnlc_lookup(vp, ".."); 9214 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9215 /* parent in dnlc cache - no need for otw */ 9216 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9217 } else { 9218 /* 9219 * parent not in dnlc cache, 9220 * do lookupp to get its id 9221 */ 9222 num_ops = 5; 9223 pnodeid = 0; /* set later by getattr parent */ 9224 } 9225 if (dvp) 9226 VN_RELE(dvp); 9227 } 9228 } 9229 recov_state.rs_flags = 0; 9230 recov_state.rs_num_retry_despite_err = 0; 9231 9232 /* Save the original mount point security flavor */ 9233 (void) save_mnt_secinfo(mi->mi_curr_serv); 9234 9235 recov_retry: 9236 args.ctag = TAG_READDIR; 9237 9238 args.array = argop; 9239 args.array_len = num_ops; 9240 9241 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9242 &recov_state, NULL)) { 9243 /* 9244 * If readdir a node that is a stub for a crossed mount point, 9245 * keep the original secinfo flavor for the current file 9246 * system, not the crossed one. 9247 */ 9248 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9249 rdc->error = e.error; 9250 return; 9251 } 9252 9253 /* 9254 * Determine which attrs to request for dirents. This code 9255 * must be protected by nfs4_start/end_fop because of r_server 9256 * (which will change during failover recovery). 9257 * 9258 */ 9259 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9260 /* 9261 * Get all vattr attrs plus filehandle and rdattr_error 9262 */ 9263 rd_bitsval = NFS4_VATTR_MASK | 9264 FATTR4_RDATTR_ERROR_MASK | 9265 FATTR4_FILEHANDLE_MASK; 9266 9267 if (rp->r_flags & R4READDIRWATTR) { 9268 mutex_enter(&rp->r_statelock); 9269 rp->r_flags &= ~R4READDIRWATTR; 9270 mutex_exit(&rp->r_statelock); 9271 } 9272 } else { 9273 servinfo4_t *svp = rp->r_server; 9274 9275 /* 9276 * Already read directory. Use readdir with 9277 * no attrs (except for mounted_on_fileid) for updates. 9278 */ 9279 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9280 9281 /* 9282 * request mounted on fileid if supported, else request 9283 * fileid. maybe we should verify that fileid is supported 9284 * and request something else if not. 9285 */ 9286 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9287 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9288 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9289 nfs_rw_exit(&svp->sv_lock); 9290 } 9291 9292 /* putfh directory fh */ 9293 argop[0].argop = OP_CPUTFH; 9294 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9295 9296 argop[1].argop = OP_READDIR; 9297 rargs = &argop[1].nfs_argop4_u.opreaddir; 9298 /* 9299 * 1 and 2 are reserved for client "." and ".." entry offset. 9300 * cookie 0 should be used over-the-wire to start reading at 9301 * the beginning of the directory excluding "." and "..". 9302 */ 9303 if (rdc->nfs4_cookie == 0 || 9304 rdc->nfs4_cookie == 1 || 9305 rdc->nfs4_cookie == 2) { 9306 rargs->cookie = (nfs_cookie4)0; 9307 rargs->cookieverf = 0; 9308 } else { 9309 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9310 mutex_enter(&rp->r_statelock); 9311 rargs->cookieverf = rp->r_cookieverf4; 9312 mutex_exit(&rp->r_statelock); 9313 } 9314 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9315 rargs->maxcount = mi->mi_tsize; 9316 rargs->attr_request = rd_bitsval; 9317 rargs->rdc = rdc; 9318 rargs->dvp = vp; 9319 rargs->mi = mi; 9320 rargs->cr = cr; 9321 9322 9323 /* 9324 * If count < than the minimum required, we return no entries 9325 * and fail with EINVAL 9326 */ 9327 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9328 rdc->error = EINVAL; 9329 goto out; 9330 } 9331 9332 if (args.array_len == 5) { 9333 /* 9334 * Add lookupp and getattr for parent nodeid. 9335 */ 9336 argop[2].argop = OP_LOOKUPP; 9337 9338 argop[3].argop = OP_GETFH; 9339 9340 /* getattr parent */ 9341 argop[4].argop = OP_GETATTR; 9342 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9343 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9344 } 9345 9346 doqueue = 1; 9347 9348 if (mi->mi_io_kstats) { 9349 mutex_enter(&mi->mi_lock); 9350 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9351 mutex_exit(&mi->mi_lock); 9352 } 9353 9354 /* capture the time of this call */ 9355 rargs->t = t = gethrtime(); 9356 9357 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9358 9359 if (mi->mi_io_kstats) { 9360 mutex_enter(&mi->mi_lock); 9361 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9362 mutex_exit(&mi->mi_lock); 9363 } 9364 9365 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9366 9367 /* 9368 * If RPC error occurred and it isn't an error that 9369 * triggers recovery, then go ahead and fail now. 9370 */ 9371 if (e.error != 0 && !needrecov) { 9372 rdc->error = e.error; 9373 goto out; 9374 } 9375 9376 if (needrecov) { 9377 bool_t abort; 9378 9379 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9380 "nfs4readdir: initiating recovery.\n")); 9381 9382 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9383 NULL, OP_READDIR, NULL, NULL, NULL); 9384 if (abort == FALSE) { 9385 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9386 &recov_state, needrecov); 9387 if (!e.error) 9388 (void) xdr_free(xdr_COMPOUND4res_clnt, 9389 (caddr_t)&res); 9390 if (rdc->entries != NULL) { 9391 kmem_free(rdc->entries, rdc->entlen); 9392 rdc->entries = NULL; 9393 } 9394 goto recov_retry; 9395 } 9396 9397 if (e.error != 0) { 9398 rdc->error = e.error; 9399 goto out; 9400 } 9401 9402 /* fall through for res.status case */ 9403 } 9404 9405 res_opcnt = res.array_len; 9406 9407 /* 9408 * If compound failed first 2 ops (PUTFH+READDIR), then return 9409 * failure here. Subsequent ops are for filling out dot-dot 9410 * dirent, and if they fail, we still want to give the caller 9411 * the dirents returned by (the successful) READDIR op, so we need 9412 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9413 * 9414 * One example where PUTFH+READDIR ops would succeed but 9415 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9416 * but lacks x. In this case, a POSIX server's VOP_READDIR 9417 * would succeed; however, VOP_LOOKUP(..) would fail since no 9418 * x perm. We need to come up with a non-vendor-specific way 9419 * for a POSIX server to return d_ino from dotdot's dirent if 9420 * client only requests mounted_on_fileid, and just say the 9421 * LOOKUPP succeeded and fill out the GETATTR. However, if 9422 * client requested any mandatory attrs, server would be required 9423 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9424 * for dotdot. 9425 */ 9426 9427 if (res.status) { 9428 if (res_opcnt <= 2) { 9429 e.error = geterrno4(res.status); 9430 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9431 &recov_state, needrecov); 9432 nfs4_purge_stale_fh(e.error, vp, cr); 9433 rdc->error = e.error; 9434 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9435 if (rdc->entries != NULL) { 9436 kmem_free(rdc->entries, rdc->entlen); 9437 rdc->entries = NULL; 9438 } 9439 /* 9440 * If readdir a node that is a stub for a 9441 * crossed mount point, keep the original 9442 * secinfo flavor for the current file system, 9443 * not the crossed one. 9444 */ 9445 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9446 return; 9447 } 9448 } 9449 9450 resop = &res.array[1]; /* readdir res */ 9451 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9452 9453 mutex_enter(&rp->r_statelock); 9454 rp->r_cookieverf4 = rd_res->cookieverf; 9455 mutex_exit(&rp->r_statelock); 9456 9457 /* 9458 * For "." and ".." entries 9459 * e.g. 9460 * seek(cookie=0) -> "." entry with d_off = 1 9461 * seek(cookie=1) -> ".." entry with d_off = 2 9462 */ 9463 if (cookie == (nfs_cookie4) 0) { 9464 if (rd_res->dotp) 9465 rd_res->dotp->d_ino = nodeid; 9466 if (rd_res->dotdotp) 9467 rd_res->dotdotp->d_ino = pnodeid; 9468 } 9469 if (cookie == (nfs_cookie4) 1) { 9470 if (rd_res->dotdotp) 9471 rd_res->dotdotp->d_ino = pnodeid; 9472 } 9473 9474 9475 /* LOOKUPP+GETATTR attemped */ 9476 if (args.array_len == 5 && rd_res->dotdotp) { 9477 if (res.status == NFS4_OK && res_opcnt == 5) { 9478 nfs_fh4 *fhp; 9479 nfs4_sharedfh_t *sfhp; 9480 vnode_t *pvp; 9481 nfs4_ga_res_t *garp; 9482 9483 resop++; /* lookupp */ 9484 resop++; /* getfh */ 9485 fhp = &resop->nfs_resop4_u.opgetfh.object; 9486 9487 resop++; /* getattr of parent */ 9488 9489 /* 9490 * First, take care of finishing the 9491 * readdir results. 9492 */ 9493 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9494 /* 9495 * The d_ino of .. must be the inode number 9496 * of the mounted filesystem. 9497 */ 9498 if (garp->n4g_va.va_mask & AT_NODEID) 9499 rd_res->dotdotp->d_ino = 9500 garp->n4g_va.va_nodeid; 9501 9502 9503 /* 9504 * Next, create the ".." dnlc entry 9505 */ 9506 sfhp = sfh4_get(fhp, mi); 9507 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9508 dnlc_update(vp, "..", pvp); 9509 VN_RELE(pvp); 9510 } 9511 sfh4_rele(&sfhp); 9512 } 9513 } 9514 9515 if (mi->mi_io_kstats) { 9516 mutex_enter(&mi->mi_lock); 9517 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9518 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9519 mutex_exit(&mi->mi_lock); 9520 } 9521 9522 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9523 9524 out: 9525 /* 9526 * If readdir a node that is a stub for a crossed mount point, 9527 * keep the original secinfo flavor for the current file system, 9528 * not the crossed one. 9529 */ 9530 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9531 9532 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9533 } 9534 9535 9536 static int 9537 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9538 { 9539 rnode4_t *rp = VTOR4(bp->b_vp); 9540 int count; 9541 int error; 9542 cred_t *cred_otw = NULL; 9543 offset_t offset; 9544 nfs4_open_stream_t *osp = NULL; 9545 bool_t first_time = TRUE; /* first time getting otw cred */ 9546 bool_t last_time = FALSE; /* last time getting otw cred */ 9547 9548 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9549 9550 DTRACE_IO1(start, struct buf *, bp); 9551 offset = ldbtob(bp->b_lblkno); 9552 9553 if (bp->b_flags & B_READ) { 9554 read_again: 9555 /* 9556 * Releases the osp, if it is provided. 9557 * Puts a hold on the cred_otw and the new osp (if found). 9558 */ 9559 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9560 &first_time, &last_time); 9561 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9562 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9563 readahead, NULL); 9564 crfree(cred_otw); 9565 if (!error) { 9566 if (bp->b_resid) { 9567 /* 9568 * Didn't get it all because we hit EOF, 9569 * zero all the memory beyond the EOF. 9570 */ 9571 /* bzero(rdaddr + */ 9572 bzero(bp->b_un.b_addr + 9573 bp->b_bcount - bp->b_resid, bp->b_resid); 9574 } 9575 mutex_enter(&rp->r_statelock); 9576 if (bp->b_resid == bp->b_bcount && 9577 offset >= rp->r_size) { 9578 /* 9579 * We didn't read anything at all as we are 9580 * past EOF. Return an error indicator back 9581 * but don't destroy the pages (yet). 9582 */ 9583 error = NFS_EOF; 9584 } 9585 mutex_exit(&rp->r_statelock); 9586 } else if (error == EACCES && last_time == FALSE) { 9587 goto read_again; 9588 } 9589 } else { 9590 if (!(rp->r_flags & R4STALE)) { 9591 write_again: 9592 /* 9593 * Releases the osp, if it is provided. 9594 * Puts a hold on the cred_otw and the new 9595 * osp (if found). 9596 */ 9597 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9598 &first_time, &last_time); 9599 mutex_enter(&rp->r_statelock); 9600 count = MIN(bp->b_bcount, rp->r_size - offset); 9601 mutex_exit(&rp->r_statelock); 9602 if (count < 0) 9603 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9604 #ifdef DEBUG 9605 if (count == 0) { 9606 zoneid_t zoneid = getzoneid(); 9607 9608 zcmn_err(zoneid, CE_WARN, 9609 "nfs4_bio: zero length write at %lld", 9610 offset); 9611 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9612 "b_bcount=%ld, file size=%lld", 9613 rp->r_flags, (long)bp->b_bcount, 9614 rp->r_size); 9615 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9616 if (nfs4_bio_do_stop) 9617 debug_enter("nfs4_bio"); 9618 } 9619 #endif 9620 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9621 count, cred_otw, stab_comm); 9622 if (error == EACCES && last_time == FALSE) { 9623 crfree(cred_otw); 9624 goto write_again; 9625 } 9626 bp->b_error = error; 9627 if (error && error != EINTR && 9628 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9629 /* 9630 * Don't print EDQUOT errors on the console. 9631 * Don't print asynchronous EACCES errors. 9632 * Don't print EFBIG errors. 9633 * Print all other write errors. 9634 */ 9635 if (error != EDQUOT && error != EFBIG && 9636 (error != EACCES || 9637 !(bp->b_flags & B_ASYNC))) 9638 nfs4_write_error(bp->b_vp, 9639 error, cred_otw); 9640 /* 9641 * Update r_error and r_flags as appropriate. 9642 * If the error was ESTALE, then mark the 9643 * rnode as not being writeable and save 9644 * the error status. Otherwise, save any 9645 * errors which occur from asynchronous 9646 * page invalidations. Any errors occurring 9647 * from other operations should be saved 9648 * by the caller. 9649 */ 9650 mutex_enter(&rp->r_statelock); 9651 if (error == ESTALE) { 9652 rp->r_flags |= R4STALE; 9653 if (!rp->r_error) 9654 rp->r_error = error; 9655 } else if (!rp->r_error && 9656 (bp->b_flags & 9657 (B_INVAL|B_FORCE|B_ASYNC)) == 9658 (B_INVAL|B_FORCE|B_ASYNC)) { 9659 rp->r_error = error; 9660 } 9661 mutex_exit(&rp->r_statelock); 9662 } 9663 crfree(cred_otw); 9664 } else { 9665 error = rp->r_error; 9666 /* 9667 * A close may have cleared r_error, if so, 9668 * propagate ESTALE error return properly 9669 */ 9670 if (error == 0) 9671 error = ESTALE; 9672 } 9673 } 9674 9675 if (error != 0 && error != NFS_EOF) 9676 bp->b_flags |= B_ERROR; 9677 9678 if (osp) 9679 open_stream_rele(osp, rp); 9680 9681 DTRACE_IO1(done, struct buf *, bp); 9682 9683 return (error); 9684 } 9685 9686 /* ARGSUSED */ 9687 int 9688 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 9689 { 9690 return (EREMOTE); 9691 } 9692 9693 /* ARGSUSED2 */ 9694 int 9695 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9696 { 9697 rnode4_t *rp = VTOR4(vp); 9698 9699 if (!write_lock) { 9700 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9701 return (V_WRITELOCK_FALSE); 9702 } 9703 9704 if ((rp->r_flags & R4DIRECTIO) || 9705 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9706 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9707 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9708 return (V_WRITELOCK_FALSE); 9709 nfs_rw_exit(&rp->r_rwlock); 9710 } 9711 9712 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9713 return (V_WRITELOCK_TRUE); 9714 } 9715 9716 /* ARGSUSED */ 9717 void 9718 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9719 { 9720 rnode4_t *rp = VTOR4(vp); 9721 9722 nfs_rw_exit(&rp->r_rwlock); 9723 } 9724 9725 /* ARGSUSED */ 9726 static int 9727 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 9728 { 9729 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9730 return (EIO); 9731 9732 /* 9733 * Because we stuff the readdir cookie into the offset field 9734 * someone may attempt to do an lseek with the cookie which 9735 * we want to succeed. 9736 */ 9737 if (vp->v_type == VDIR) 9738 return (0); 9739 if (*noffp < 0) 9740 return (EINVAL); 9741 return (0); 9742 } 9743 9744 9745 /* 9746 * Return all the pages from [off..off+len) in file 9747 */ 9748 /* ARGSUSED */ 9749 static int 9750 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9751 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9752 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 9753 { 9754 rnode4_t *rp; 9755 int error; 9756 mntinfo4_t *mi; 9757 9758 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9759 return (EIO); 9760 rp = VTOR4(vp); 9761 if (IS_SHADOW(vp, rp)) 9762 vp = RTOV4(rp); 9763 9764 if (vp->v_flag & VNOMAP) 9765 return (ENOSYS); 9766 9767 if (protp != NULL) 9768 *protp = PROT_ALL; 9769 9770 /* 9771 * Now validate that the caches are up to date. 9772 */ 9773 if (error = nfs4_validate_caches(vp, cr)) 9774 return (error); 9775 9776 mi = VTOMI4(vp); 9777 retry: 9778 mutex_enter(&rp->r_statelock); 9779 9780 /* 9781 * Don't create dirty pages faster than they 9782 * can be cleaned so that the system doesn't 9783 * get imbalanced. If the async queue is 9784 * maxed out, then wait for it to drain before 9785 * creating more dirty pages. Also, wait for 9786 * any threads doing pagewalks in the vop_getattr 9787 * entry points so that they don't block for 9788 * long periods. 9789 */ 9790 if (rw == S_CREATE) { 9791 while ((mi->mi_max_threads != 0 && 9792 rp->r_awcount > 2 * mi->mi_max_threads) || 9793 rp->r_gcount > 0) 9794 cv_wait(&rp->r_cv, &rp->r_statelock); 9795 } 9796 9797 /* 9798 * If we are getting called as a side effect of an nfs_write() 9799 * operation the local file size might not be extended yet. 9800 * In this case we want to be able to return pages of zeroes. 9801 */ 9802 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9803 NFS4_DEBUG(nfs4_pageio_debug, 9804 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9805 "len=%llu, size=%llu, attrsize =%llu", off, 9806 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9807 mutex_exit(&rp->r_statelock); 9808 return (EFAULT); /* beyond EOF */ 9809 } 9810 9811 mutex_exit(&rp->r_statelock); 9812 9813 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9814 pl, plsz, seg, addr, rw, cr); 9815 NFS4_DEBUG(nfs4_pageio_debug && error, 9816 (CE_NOTE, "getpages error %d; off=%lld, len=%lld", 9817 error, off, (u_longlong_t)len)); 9818 9819 switch (error) { 9820 case NFS_EOF: 9821 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9822 goto retry; 9823 case ESTALE: 9824 nfs4_purge_stale_fh(error, vp, cr); 9825 } 9826 9827 return (error); 9828 } 9829 9830 /* 9831 * Called from pvn_getpages to get a particular page. 9832 */ 9833 /* ARGSUSED */ 9834 static int 9835 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9836 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9837 enum seg_rw rw, cred_t *cr) 9838 { 9839 rnode4_t *rp; 9840 uint_t bsize; 9841 struct buf *bp; 9842 page_t *pp; 9843 u_offset_t lbn; 9844 u_offset_t io_off; 9845 u_offset_t blkoff; 9846 u_offset_t rablkoff; 9847 size_t io_len; 9848 uint_t blksize; 9849 int error; 9850 int readahead; 9851 int readahead_issued = 0; 9852 int ra_window; /* readahead window */ 9853 page_t *pagefound; 9854 page_t *savepp; 9855 9856 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9857 return (EIO); 9858 9859 rp = VTOR4(vp); 9860 ASSERT(!IS_SHADOW(vp, rp)); 9861 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9862 9863 reread: 9864 bp = NULL; 9865 pp = NULL; 9866 pagefound = NULL; 9867 9868 if (pl != NULL) 9869 pl[0] = NULL; 9870 9871 error = 0; 9872 lbn = off / bsize; 9873 blkoff = lbn * bsize; 9874 9875 /* 9876 * Queueing up the readahead before doing the synchronous read 9877 * results in a significant increase in read throughput because 9878 * of the increased parallelism between the async threads and 9879 * the process context. 9880 */ 9881 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9882 rw != S_CREATE && 9883 !(vp->v_flag & VNOCACHE)) { 9884 mutex_enter(&rp->r_statelock); 9885 9886 /* 9887 * Calculate the number of readaheads to do. 9888 * a) No readaheads at offset = 0. 9889 * b) Do maximum(nfs4_nra) readaheads when the readahead 9890 * window is closed. 9891 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9892 * upon how far the readahead window is open or close. 9893 * d) No readaheads if rp->r_nextr is not within the scope 9894 * of the readahead window (random i/o). 9895 */ 9896 9897 if (off == 0) 9898 readahead = 0; 9899 else if (blkoff == rp->r_nextr) 9900 readahead = nfs4_nra; 9901 else if (rp->r_nextr > blkoff && 9902 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9903 <= (nfs4_nra - 1))) 9904 readahead = nfs4_nra - ra_window; 9905 else 9906 readahead = 0; 9907 9908 rablkoff = rp->r_nextr; 9909 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9910 mutex_exit(&rp->r_statelock); 9911 if (nfs4_async_readahead(vp, rablkoff + bsize, 9912 addr + (rablkoff + bsize - off), 9913 seg, cr, nfs4_readahead) < 0) { 9914 mutex_enter(&rp->r_statelock); 9915 break; 9916 } 9917 readahead--; 9918 rablkoff += bsize; 9919 /* 9920 * Indicate that we did a readahead so 9921 * readahead offset is not updated 9922 * by the synchronous read below. 9923 */ 9924 readahead_issued = 1; 9925 mutex_enter(&rp->r_statelock); 9926 /* 9927 * set readahead offset to 9928 * offset of last async readahead 9929 * request. 9930 */ 9931 rp->r_nextr = rablkoff; 9932 } 9933 mutex_exit(&rp->r_statelock); 9934 } 9935 9936 again: 9937 if ((pagefound = page_exists(vp, off)) == NULL) { 9938 if (pl == NULL) { 9939 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9940 nfs4_readahead); 9941 } else if (rw == S_CREATE) { 9942 /* 9943 * Block for this page is not allocated, or the offset 9944 * is beyond the current allocation size, or we're 9945 * allocating a swap slot and the page was not found, 9946 * so allocate it and return a zero page. 9947 */ 9948 if ((pp = page_create_va(vp, off, 9949 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9950 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9951 io_len = PAGESIZE; 9952 mutex_enter(&rp->r_statelock); 9953 rp->r_nextr = off + PAGESIZE; 9954 mutex_exit(&rp->r_statelock); 9955 } else { 9956 /* 9957 * Need to go to server to get a block 9958 */ 9959 mutex_enter(&rp->r_statelock); 9960 if (blkoff < rp->r_size && 9961 blkoff + bsize > rp->r_size) { 9962 /* 9963 * If less than a block left in 9964 * file read less than a block. 9965 */ 9966 if (rp->r_size <= off) { 9967 /* 9968 * Trying to access beyond EOF, 9969 * set up to get at least one page. 9970 */ 9971 blksize = off + PAGESIZE - blkoff; 9972 } else 9973 blksize = rp->r_size - blkoff; 9974 } else if ((off == 0) || 9975 (off != rp->r_nextr && !readahead_issued)) { 9976 blksize = PAGESIZE; 9977 blkoff = off; /* block = page here */ 9978 } else 9979 blksize = bsize; 9980 mutex_exit(&rp->r_statelock); 9981 9982 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9983 &io_len, blkoff, blksize, 0); 9984 9985 /* 9986 * Some other thread has entered the page, 9987 * so just use it. 9988 */ 9989 if (pp == NULL) 9990 goto again; 9991 9992 /* 9993 * Now round the request size up to page boundaries. 9994 * This ensures that the entire page will be 9995 * initialized to zeroes if EOF is encountered. 9996 */ 9997 io_len = ptob(btopr(io_len)); 9998 9999 bp = pageio_setup(pp, io_len, vp, B_READ); 10000 ASSERT(bp != NULL); 10001 10002 /* 10003 * pageio_setup should have set b_addr to 0. This 10004 * is correct since we want to do I/O on a page 10005 * boundary. bp_mapin will use this addr to calculate 10006 * an offset, and then set b_addr to the kernel virtual 10007 * address it allocated for us. 10008 */ 10009 ASSERT(bp->b_un.b_addr == 0); 10010 10011 bp->b_edev = 0; 10012 bp->b_dev = 0; 10013 bp->b_lblkno = lbtodb(io_off); 10014 bp->b_file = vp; 10015 bp->b_offset = (offset_t)off; 10016 bp_mapin(bp); 10017 10018 /* 10019 * If doing a write beyond what we believe is EOF, 10020 * don't bother trying to read the pages from the 10021 * server, we'll just zero the pages here. We 10022 * don't check that the rw flag is S_WRITE here 10023 * because some implementations may attempt a 10024 * read access to the buffer before copying data. 10025 */ 10026 mutex_enter(&rp->r_statelock); 10027 if (io_off >= rp->r_size && seg == segkmap) { 10028 mutex_exit(&rp->r_statelock); 10029 bzero(bp->b_un.b_addr, io_len); 10030 } else { 10031 mutex_exit(&rp->r_statelock); 10032 error = nfs4_bio(bp, NULL, cr, FALSE); 10033 } 10034 10035 /* 10036 * Unmap the buffer before freeing it. 10037 */ 10038 bp_mapout(bp); 10039 pageio_done(bp); 10040 10041 savepp = pp; 10042 do { 10043 pp->p_fsdata = C_NOCOMMIT; 10044 } while ((pp = pp->p_next) != savepp); 10045 10046 if (error == NFS_EOF) { 10047 /* 10048 * If doing a write system call just return 10049 * zeroed pages, else user tried to get pages 10050 * beyond EOF, return error. We don't check 10051 * that the rw flag is S_WRITE here because 10052 * some implementations may attempt a read 10053 * access to the buffer before copying data. 10054 */ 10055 if (seg == segkmap) 10056 error = 0; 10057 else 10058 error = EFAULT; 10059 } 10060 10061 if (!readahead_issued && !error) { 10062 mutex_enter(&rp->r_statelock); 10063 rp->r_nextr = io_off + io_len; 10064 mutex_exit(&rp->r_statelock); 10065 } 10066 } 10067 } 10068 10069 out: 10070 if (pl == NULL) 10071 return (error); 10072 10073 if (error) { 10074 if (pp != NULL) 10075 pvn_read_done(pp, B_ERROR); 10076 return (error); 10077 } 10078 10079 if (pagefound) { 10080 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 10081 10082 /* 10083 * Page exists in the cache, acquire the appropriate lock. 10084 * If this fails, start all over again. 10085 */ 10086 if ((pp = page_lookup(vp, off, se)) == NULL) { 10087 #ifdef DEBUG 10088 nfs4_lostpage++; 10089 #endif 10090 goto reread; 10091 } 10092 pl[0] = pp; 10093 pl[1] = NULL; 10094 return (0); 10095 } 10096 10097 if (pp != NULL) 10098 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 10099 10100 return (error); 10101 } 10102 10103 static void 10104 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 10105 cred_t *cr) 10106 { 10107 int error; 10108 page_t *pp; 10109 u_offset_t io_off; 10110 size_t io_len; 10111 struct buf *bp; 10112 uint_t bsize, blksize; 10113 rnode4_t *rp = VTOR4(vp); 10114 page_t *savepp; 10115 10116 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10117 10118 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10119 10120 mutex_enter(&rp->r_statelock); 10121 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 10122 /* 10123 * If less than a block left in file read less 10124 * than a block. 10125 */ 10126 blksize = rp->r_size - blkoff; 10127 } else 10128 blksize = bsize; 10129 mutex_exit(&rp->r_statelock); 10130 10131 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 10132 &io_off, &io_len, blkoff, blksize, 1); 10133 /* 10134 * The isra flag passed to the kluster function is 1, we may have 10135 * gotten a return value of NULL for a variety of reasons (# of free 10136 * pages < minfree, someone entered the page on the vnode etc). In all 10137 * cases, we want to punt on the readahead. 10138 */ 10139 if (pp == NULL) 10140 return; 10141 10142 /* 10143 * Now round the request size up to page boundaries. 10144 * This ensures that the entire page will be 10145 * initialized to zeroes if EOF is encountered. 10146 */ 10147 io_len = ptob(btopr(io_len)); 10148 10149 bp = pageio_setup(pp, io_len, vp, B_READ); 10150 ASSERT(bp != NULL); 10151 10152 /* 10153 * pageio_setup should have set b_addr to 0. This is correct since 10154 * we want to do I/O on a page boundary. bp_mapin() will use this addr 10155 * to calculate an offset, and then set b_addr to the kernel virtual 10156 * address it allocated for us. 10157 */ 10158 ASSERT(bp->b_un.b_addr == 0); 10159 10160 bp->b_edev = 0; 10161 bp->b_dev = 0; 10162 bp->b_lblkno = lbtodb(io_off); 10163 bp->b_file = vp; 10164 bp->b_offset = (offset_t)blkoff; 10165 bp_mapin(bp); 10166 10167 /* 10168 * If doing a write beyond what we believe is EOF, don't bother trying 10169 * to read the pages from the server, we'll just zero the pages here. 10170 * We don't check that the rw flag is S_WRITE here because some 10171 * implementations may attempt a read access to the buffer before 10172 * copying data. 10173 */ 10174 mutex_enter(&rp->r_statelock); 10175 if (io_off >= rp->r_size && seg == segkmap) { 10176 mutex_exit(&rp->r_statelock); 10177 bzero(bp->b_un.b_addr, io_len); 10178 error = 0; 10179 } else { 10180 mutex_exit(&rp->r_statelock); 10181 error = nfs4_bio(bp, NULL, cr, TRUE); 10182 if (error == NFS_EOF) 10183 error = 0; 10184 } 10185 10186 /* 10187 * Unmap the buffer before freeing it. 10188 */ 10189 bp_mapout(bp); 10190 pageio_done(bp); 10191 10192 savepp = pp; 10193 do { 10194 pp->p_fsdata = C_NOCOMMIT; 10195 } while ((pp = pp->p_next) != savepp); 10196 10197 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10198 10199 /* 10200 * In case of error set readahead offset 10201 * to the lowest offset. 10202 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10203 */ 10204 if (error && rp->r_nextr > io_off) { 10205 mutex_enter(&rp->r_statelock); 10206 if (rp->r_nextr > io_off) 10207 rp->r_nextr = io_off; 10208 mutex_exit(&rp->r_statelock); 10209 } 10210 } 10211 10212 /* 10213 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10214 * If len == 0, do from off to EOF. 10215 * 10216 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10217 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10218 * (from pageout). 10219 */ 10220 /* ARGSUSED */ 10221 static int 10222 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 10223 caller_context_t *ct) 10224 { 10225 int error; 10226 rnode4_t *rp; 10227 10228 ASSERT(cr != NULL); 10229 10230 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10231 return (EIO); 10232 10233 rp = VTOR4(vp); 10234 if (IS_SHADOW(vp, rp)) 10235 vp = RTOV4(rp); 10236 10237 /* 10238 * XXX - Why should this check be made here? 10239 */ 10240 if (vp->v_flag & VNOMAP) 10241 return (ENOSYS); 10242 10243 if (len == 0 && !(flags & B_INVAL) && 10244 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10245 return (0); 10246 10247 mutex_enter(&rp->r_statelock); 10248 rp->r_count++; 10249 mutex_exit(&rp->r_statelock); 10250 error = nfs4_putpages(vp, off, len, flags, cr); 10251 mutex_enter(&rp->r_statelock); 10252 rp->r_count--; 10253 cv_broadcast(&rp->r_cv); 10254 mutex_exit(&rp->r_statelock); 10255 10256 return (error); 10257 } 10258 10259 /* 10260 * Write out a single page, possibly klustering adjacent dirty pages. 10261 */ 10262 int 10263 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10264 int flags, cred_t *cr) 10265 { 10266 u_offset_t io_off; 10267 u_offset_t lbn_off; 10268 u_offset_t lbn; 10269 size_t io_len; 10270 uint_t bsize; 10271 int error; 10272 rnode4_t *rp; 10273 10274 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10275 ASSERT(pp != NULL); 10276 ASSERT(cr != NULL); 10277 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10278 10279 rp = VTOR4(vp); 10280 ASSERT(rp->r_count > 0); 10281 ASSERT(!IS_SHADOW(vp, rp)); 10282 10283 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10284 lbn = pp->p_offset / bsize; 10285 lbn_off = lbn * bsize; 10286 10287 /* 10288 * Find a kluster that fits in one block, or in 10289 * one page if pages are bigger than blocks. If 10290 * there is less file space allocated than a whole 10291 * page, we'll shorten the i/o request below. 10292 */ 10293 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10294 roundup(bsize, PAGESIZE), flags); 10295 10296 /* 10297 * pvn_write_kluster shouldn't have returned a page with offset 10298 * behind the original page we were given. Verify that. 10299 */ 10300 ASSERT((pp->p_offset / bsize) >= lbn); 10301 10302 /* 10303 * Now pp will have the list of kept dirty pages marked for 10304 * write back. It will also handle invalidation and freeing 10305 * of pages that are not dirty. Check for page length rounding 10306 * problems. 10307 */ 10308 if (io_off + io_len > lbn_off + bsize) { 10309 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10310 io_len = lbn_off + bsize - io_off; 10311 } 10312 /* 10313 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10314 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10315 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10316 * progress and the r_size has not been made consistent with the 10317 * new size of the file. When the uiomove() completes the r_size is 10318 * updated and the R4MODINPROGRESS flag is cleared. 10319 * 10320 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10321 * consistent value of r_size. Without this handshaking, it is 10322 * possible that nfs4_bio() picks up the old value of r_size 10323 * before the uiomove() in writerp4() completes. This will result 10324 * in the write through nfs4_bio() being dropped. 10325 * 10326 * More precisely, there is a window between the time the uiomove() 10327 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10328 * operation intervenes in this window, the page will be picked up, 10329 * because it is dirty (it will be unlocked, unless it was 10330 * pagecreate'd). When the page is picked up as dirty, the dirty 10331 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10332 * checked. This will still be the old size. Therefore the page will 10333 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10334 * the page will be found to be clean and the write will be dropped. 10335 */ 10336 if (rp->r_flags & R4MODINPROGRESS) { 10337 mutex_enter(&rp->r_statelock); 10338 if ((rp->r_flags & R4MODINPROGRESS) && 10339 rp->r_modaddr + MAXBSIZE > io_off && 10340 rp->r_modaddr < io_off + io_len) { 10341 page_t *plist; 10342 /* 10343 * A write is in progress for this region of the file. 10344 * If we did not detect R4MODINPROGRESS here then this 10345 * path through nfs_putapage() would eventually go to 10346 * nfs4_bio() and may not write out all of the data 10347 * in the pages. We end up losing data. So we decide 10348 * to set the modified bit on each page in the page 10349 * list and mark the rnode with R4DIRTY. This write 10350 * will be restarted at some later time. 10351 */ 10352 plist = pp; 10353 while (plist != NULL) { 10354 pp = plist; 10355 page_sub(&plist, pp); 10356 hat_setmod(pp); 10357 page_io_unlock(pp); 10358 page_unlock(pp); 10359 } 10360 rp->r_flags |= R4DIRTY; 10361 mutex_exit(&rp->r_statelock); 10362 if (offp) 10363 *offp = io_off; 10364 if (lenp) 10365 *lenp = io_len; 10366 return (0); 10367 } 10368 mutex_exit(&rp->r_statelock); 10369 } 10370 10371 if (flags & B_ASYNC) { 10372 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10373 nfs4_sync_putapage); 10374 } else 10375 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10376 10377 if (offp) 10378 *offp = io_off; 10379 if (lenp) 10380 *lenp = io_len; 10381 return (error); 10382 } 10383 10384 static int 10385 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10386 int flags, cred_t *cr) 10387 { 10388 int error; 10389 rnode4_t *rp; 10390 10391 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10392 10393 flags |= B_WRITE; 10394 10395 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10396 10397 rp = VTOR4(vp); 10398 10399 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10400 error == EACCES) && 10401 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10402 if (!(rp->r_flags & R4OUTOFSPACE)) { 10403 mutex_enter(&rp->r_statelock); 10404 rp->r_flags |= R4OUTOFSPACE; 10405 mutex_exit(&rp->r_statelock); 10406 } 10407 flags |= B_ERROR; 10408 pvn_write_done(pp, flags); 10409 /* 10410 * If this was not an async thread, then try again to 10411 * write out the pages, but this time, also destroy 10412 * them whether or not the write is successful. This 10413 * will prevent memory from filling up with these 10414 * pages and destroying them is the only alternative 10415 * if they can't be written out. 10416 * 10417 * Don't do this if this is an async thread because 10418 * when the pages are unlocked in pvn_write_done, 10419 * some other thread could have come along, locked 10420 * them, and queued for an async thread. It would be 10421 * possible for all of the async threads to be tied 10422 * up waiting to lock the pages again and they would 10423 * all already be locked and waiting for an async 10424 * thread to handle them. Deadlock. 10425 */ 10426 if (!(flags & B_ASYNC)) { 10427 error = nfs4_putpage(vp, io_off, io_len, 10428 B_INVAL | B_FORCE, cr, NULL); 10429 } 10430 } else { 10431 if (error) 10432 flags |= B_ERROR; 10433 else if (rp->r_flags & R4OUTOFSPACE) { 10434 mutex_enter(&rp->r_statelock); 10435 rp->r_flags &= ~R4OUTOFSPACE; 10436 mutex_exit(&rp->r_statelock); 10437 } 10438 pvn_write_done(pp, flags); 10439 if (freemem < desfree) 10440 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10441 NFS4_WRITE_NOWAIT); 10442 } 10443 10444 return (error); 10445 } 10446 10447 #ifdef DEBUG 10448 int nfs4_force_open_before_mmap = 0; 10449 #endif 10450 10451 /* ARGSUSED */ 10452 static int 10453 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10454 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10455 caller_context_t *ct) 10456 { 10457 struct segvn_crargs vn_a; 10458 int error = 0; 10459 rnode4_t *rp = VTOR4(vp); 10460 mntinfo4_t *mi = VTOMI4(vp); 10461 10462 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10463 return (EIO); 10464 10465 if (vp->v_flag & VNOMAP) 10466 return (ENOSYS); 10467 10468 if (off < 0 || (off + len) < 0) 10469 return (ENXIO); 10470 10471 if (vp->v_type != VREG) 10472 return (ENODEV); 10473 10474 /* 10475 * If the file is delegated to the client don't do anything. 10476 * If the file is not delegated, then validate the data cache. 10477 */ 10478 mutex_enter(&rp->r_statev4_lock); 10479 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10480 mutex_exit(&rp->r_statev4_lock); 10481 error = nfs4_validate_caches(vp, cr); 10482 if (error) 10483 return (error); 10484 } else { 10485 mutex_exit(&rp->r_statev4_lock); 10486 } 10487 10488 /* 10489 * Check to see if the vnode is currently marked as not cachable. 10490 * This means portions of the file are locked (through VOP_FRLOCK). 10491 * In this case the map request must be refused. We use 10492 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10493 * 10494 * Atomically increment r_inmap after acquiring r_rwlock. The 10495 * idea here is to acquire r_rwlock to block read/write and 10496 * not to protect r_inmap. r_inmap will inform nfs4_read/write() 10497 * that we are in nfs4_map(). Now, r_rwlock is acquired in order 10498 * and we can prevent the deadlock that would have occurred 10499 * when nfs4_addmap() would have acquired it out of order. 10500 * 10501 * Since we are not protecting r_inmap by any lock, we do not 10502 * hold any lock when we decrement it. We atomically decrement 10503 * r_inmap after we release r_lkserlock. 10504 */ 10505 10506 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp))) 10507 return (EINTR); 10508 atomic_inc_uint(&rp->r_inmap); 10509 nfs_rw_exit(&rp->r_rwlock); 10510 10511 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) { 10512 atomic_dec_uint(&rp->r_inmap); 10513 return (EINTR); 10514 } 10515 10516 10517 if (vp->v_flag & VNOCACHE) { 10518 error = EAGAIN; 10519 goto done; 10520 } 10521 10522 /* 10523 * Don't allow concurrent locks and mapping if mandatory locking is 10524 * enabled. 10525 */ 10526 if (flk_has_remote_locks(vp)) { 10527 struct vattr va; 10528 va.va_mask = AT_MODE; 10529 error = nfs4getattr(vp, &va, cr); 10530 if (error != 0) 10531 goto done; 10532 if (MANDLOCK(vp, va.va_mode)) { 10533 error = EAGAIN; 10534 goto done; 10535 } 10536 } 10537 10538 /* 10539 * It is possible that the rnode has a lost lock request that we 10540 * are still trying to recover, and that the request conflicts with 10541 * this map request. 10542 * 10543 * An alternative approach would be for nfs4_safemap() to consider 10544 * queued lock requests when deciding whether to set or clear 10545 * VNOCACHE. This would require the frlock code path to call 10546 * nfs4_safemap() after enqueing a lost request. 10547 */ 10548 if (nfs4_map_lost_lock_conflict(vp)) { 10549 error = EAGAIN; 10550 goto done; 10551 } 10552 10553 as_rangelock(as); 10554 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 10555 if (error != 0) { 10556 as_rangeunlock(as); 10557 goto done; 10558 } 10559 10560 if (vp->v_type == VREG) { 10561 /* 10562 * We need to retrieve the open stream 10563 */ 10564 nfs4_open_stream_t *osp = NULL; 10565 nfs4_open_owner_t *oop = NULL; 10566 10567 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10568 if (oop != NULL) { 10569 /* returns with 'os_sync_lock' held */ 10570 osp = find_open_stream(oop, rp); 10571 open_owner_rele(oop); 10572 } 10573 if (osp == NULL) { 10574 #ifdef DEBUG 10575 if (nfs4_force_open_before_mmap) { 10576 error = EIO; 10577 goto done; 10578 } 10579 #endif 10580 /* returns with 'os_sync_lock' held */ 10581 error = open_and_get_osp(vp, cr, &osp); 10582 if (osp == NULL) { 10583 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10584 "nfs4_map: we tried to OPEN the file " 10585 "but again no osp, so fail with EIO")); 10586 goto done; 10587 } 10588 } 10589 10590 if (osp->os_failed_reopen) { 10591 mutex_exit(&osp->os_sync_lock); 10592 open_stream_rele(osp, rp); 10593 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10594 "nfs4_map: os_failed_reopen set on " 10595 "osp %p, cr %p, rp %s", (void *)osp, 10596 (void *)cr, rnode4info(rp))); 10597 error = EIO; 10598 goto done; 10599 } 10600 mutex_exit(&osp->os_sync_lock); 10601 open_stream_rele(osp, rp); 10602 } 10603 10604 vn_a.vp = vp; 10605 vn_a.offset = off; 10606 vn_a.type = (flags & MAP_TYPE); 10607 vn_a.prot = (uchar_t)prot; 10608 vn_a.maxprot = (uchar_t)maxprot; 10609 vn_a.flags = (flags & ~MAP_TYPE); 10610 vn_a.cred = cr; 10611 vn_a.amp = NULL; 10612 vn_a.szc = 0; 10613 vn_a.lgrp_mem_policy_flags = 0; 10614 10615 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10616 as_rangeunlock(as); 10617 10618 done: 10619 nfs_rw_exit(&rp->r_lkserlock); 10620 atomic_dec_uint(&rp->r_inmap); 10621 return (error); 10622 } 10623 10624 /* 10625 * We're most likely dealing with a kernel module that likes to READ 10626 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10627 * officially OPEN the file to create the necessary client state 10628 * for bookkeeping of os_mmap_read/write counts. 10629 * 10630 * Since VOP_MAP only passes in a pointer to the vnode rather than 10631 * a double pointer, we can't handle the case where nfs4open_otw() 10632 * returns a different vnode than the one passed into VOP_MAP (since 10633 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10634 * we return NULL and let nfs4_map() fail. Note: the only case where 10635 * this should happen is if the file got removed and replaced with the 10636 * same name on the server (in addition to the fact that we're trying 10637 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10638 */ 10639 static int 10640 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10641 { 10642 rnode4_t *rp, *drp; 10643 vnode_t *dvp, *open_vp; 10644 char file_name[MAXNAMELEN]; 10645 int just_created; 10646 nfs4_open_stream_t *osp; 10647 nfs4_open_owner_t *oop; 10648 int error; 10649 10650 *ospp = NULL; 10651 open_vp = map_vp; 10652 10653 rp = VTOR4(open_vp); 10654 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10655 return (error); 10656 drp = VTOR4(dvp); 10657 10658 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10659 VN_RELE(dvp); 10660 return (EINTR); 10661 } 10662 10663 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10664 nfs_rw_exit(&drp->r_rwlock); 10665 VN_RELE(dvp); 10666 return (error); 10667 } 10668 10669 mutex_enter(&rp->r_statev4_lock); 10670 if (rp->created_v4) { 10671 rp->created_v4 = 0; 10672 mutex_exit(&rp->r_statev4_lock); 10673 10674 dnlc_update(dvp, file_name, open_vp); 10675 /* This is needed so we don't bump the open ref count */ 10676 just_created = 1; 10677 } else { 10678 mutex_exit(&rp->r_statev4_lock); 10679 just_created = 0; 10680 } 10681 10682 VN_HOLD(map_vp); 10683 10684 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10685 just_created); 10686 if (error) { 10687 nfs_rw_exit(&drp->r_rwlock); 10688 VN_RELE(dvp); 10689 VN_RELE(map_vp); 10690 return (error); 10691 } 10692 10693 nfs_rw_exit(&drp->r_rwlock); 10694 VN_RELE(dvp); 10695 10696 /* 10697 * If nfs4open_otw() returned a different vnode then "undo" 10698 * the open and return failure to the caller. 10699 */ 10700 if (!VN_CMP(open_vp, map_vp)) { 10701 nfs4_error_t e; 10702 10703 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10704 "open returned a different vnode")); 10705 /* 10706 * If there's an error, ignore it, 10707 * and let VOP_INACTIVE handle it. 10708 */ 10709 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10710 CLOSE_NORM, 0, 0, 0); 10711 VN_RELE(map_vp); 10712 return (EIO); 10713 } 10714 10715 VN_RELE(map_vp); 10716 10717 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10718 if (!oop) { 10719 nfs4_error_t e; 10720 10721 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10722 "no open owner")); 10723 /* 10724 * If there's an error, ignore it, 10725 * and let VOP_INACTIVE handle it. 10726 */ 10727 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10728 CLOSE_NORM, 0, 0, 0); 10729 return (EIO); 10730 } 10731 osp = find_open_stream(oop, rp); 10732 open_owner_rele(oop); 10733 *ospp = osp; 10734 return (0); 10735 } 10736 10737 /* 10738 * Please be aware that when this function is called, the address space write 10739 * a_lock is held. Do not put over the wire calls in this function. 10740 */ 10741 /* ARGSUSED */ 10742 static int 10743 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10744 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10745 caller_context_t *ct) 10746 { 10747 rnode4_t *rp; 10748 int error = 0; 10749 mntinfo4_t *mi; 10750 10751 mi = VTOMI4(vp); 10752 rp = VTOR4(vp); 10753 10754 if (nfs_zone() != mi->mi_zone) 10755 return (EIO); 10756 if (vp->v_flag & VNOMAP) 10757 return (ENOSYS); 10758 10759 /* 10760 * Don't need to update the open stream first, since this 10761 * mmap can't add any additional share access that isn't 10762 * already contained in the open stream (for the case where we 10763 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10764 * take into account os_mmap_read[write] counts). 10765 */ 10766 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10767 10768 if (vp->v_type == VREG) { 10769 /* 10770 * We need to retrieve the open stream and update the counts. 10771 * If there is no open stream here, something is wrong. 10772 */ 10773 nfs4_open_stream_t *osp = NULL; 10774 nfs4_open_owner_t *oop = NULL; 10775 10776 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10777 if (oop != NULL) { 10778 /* returns with 'os_sync_lock' held */ 10779 osp = find_open_stream(oop, rp); 10780 open_owner_rele(oop); 10781 } 10782 if (osp == NULL) { 10783 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10784 "nfs4_addmap: we should have an osp" 10785 "but we don't, so fail with EIO")); 10786 error = EIO; 10787 goto out; 10788 } 10789 10790 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10791 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10792 10793 /* 10794 * Update the map count in the open stream. 10795 * This is necessary in the case where we 10796 * open/mmap/close/, then the server reboots, and we 10797 * attempt to reopen. If the mmap doesn't add share 10798 * access then we send an invalid reopen with 10799 * access = NONE. 10800 * 10801 * We need to specifically check each PROT_* so a mmap 10802 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10803 * read and write access. A simple comparison of prot 10804 * to ~PROT_WRITE to determine read access is insufficient 10805 * since prot can be |= with PROT_USER, etc. 10806 */ 10807 10808 /* 10809 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10810 */ 10811 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10812 osp->os_mmap_write += btopr(len); 10813 if (maxprot & PROT_READ) 10814 osp->os_mmap_read += btopr(len); 10815 if (maxprot & PROT_EXEC) 10816 osp->os_mmap_read += btopr(len); 10817 /* 10818 * Ensure that os_mmap_read gets incremented, even if 10819 * maxprot were to look like PROT_NONE. 10820 */ 10821 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10822 !(maxprot & PROT_EXEC)) 10823 osp->os_mmap_read += btopr(len); 10824 osp->os_mapcnt += btopr(len); 10825 mutex_exit(&osp->os_sync_lock); 10826 open_stream_rele(osp, rp); 10827 } 10828 10829 out: 10830 /* 10831 * If we got an error, then undo our 10832 * incrementing of 'r_mapcnt'. 10833 */ 10834 10835 if (error) { 10836 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10837 ASSERT(rp->r_mapcnt >= 0); 10838 } 10839 return (error); 10840 } 10841 10842 /* ARGSUSED */ 10843 static int 10844 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct) 10845 { 10846 10847 return (VTOR4(vp1) == VTOR4(vp2)); 10848 } 10849 10850 /* ARGSUSED */ 10851 static int 10852 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10853 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr, 10854 caller_context_t *ct) 10855 { 10856 int rc; 10857 u_offset_t start, end; 10858 rnode4_t *rp; 10859 int error = 0, intr = INTR4(vp); 10860 nfs4_error_t e; 10861 10862 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10863 return (EIO); 10864 10865 /* check for valid cmd parameter */ 10866 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10867 return (EINVAL); 10868 10869 /* Verify l_type. */ 10870 switch (bfp->l_type) { 10871 case F_RDLCK: 10872 if (cmd != F_GETLK && !(flag & FREAD)) 10873 return (EBADF); 10874 break; 10875 case F_WRLCK: 10876 if (cmd != F_GETLK && !(flag & FWRITE)) 10877 return (EBADF); 10878 break; 10879 case F_UNLCK: 10880 intr = 0; 10881 break; 10882 10883 default: 10884 return (EINVAL); 10885 } 10886 10887 /* check the validity of the lock range */ 10888 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10889 return (rc); 10890 if (rc = flk_check_lock_data(start, end, MAXEND)) 10891 return (rc); 10892 10893 /* 10894 * If the filesystem is mounted using local locking, pass the 10895 * request off to the local locking code. 10896 */ 10897 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10898 if (cmd == F_SETLK || cmd == F_SETLKW) { 10899 /* 10900 * For complete safety, we should be holding 10901 * r_lkserlock. However, we can't call 10902 * nfs4_safelock and then fs_frlock while 10903 * holding r_lkserlock, so just invoke 10904 * nfs4_safelock and expect that this will 10905 * catch enough of the cases. 10906 */ 10907 if (!nfs4_safelock(vp, bfp, cr)) 10908 return (EAGAIN); 10909 } 10910 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 10911 } 10912 10913 rp = VTOR4(vp); 10914 10915 /* 10916 * Check whether the given lock request can proceed, given the 10917 * current file mappings. 10918 */ 10919 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10920 return (EINTR); 10921 if (cmd == F_SETLK || cmd == F_SETLKW) { 10922 if (!nfs4_safelock(vp, bfp, cr)) { 10923 rc = EAGAIN; 10924 goto done; 10925 } 10926 } 10927 10928 /* 10929 * Flush the cache after waiting for async I/O to finish. For new 10930 * locks, this is so that the process gets the latest bits from the 10931 * server. For unlocks, this is so that other clients see the 10932 * latest bits once the file has been unlocked. If currently dirty 10933 * pages can't be flushed, then don't allow a lock to be set. But 10934 * allow unlocks to succeed, to avoid having orphan locks on the 10935 * server. 10936 */ 10937 if (cmd != F_GETLK) { 10938 mutex_enter(&rp->r_statelock); 10939 while (rp->r_count > 0) { 10940 if (intr) { 10941 klwp_t *lwp = ttolwp(curthread); 10942 10943 if (lwp != NULL) 10944 lwp->lwp_nostop++; 10945 if (cv_wait_sig(&rp->r_cv, 10946 &rp->r_statelock) == 0) { 10947 if (lwp != NULL) 10948 lwp->lwp_nostop--; 10949 rc = EINTR; 10950 break; 10951 } 10952 if (lwp != NULL) 10953 lwp->lwp_nostop--; 10954 } else 10955 cv_wait(&rp->r_cv, &rp->r_statelock); 10956 } 10957 mutex_exit(&rp->r_statelock); 10958 if (rc != 0) 10959 goto done; 10960 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct); 10961 if (error) { 10962 if (error == ENOSPC || error == EDQUOT) { 10963 mutex_enter(&rp->r_statelock); 10964 if (!rp->r_error) 10965 rp->r_error = error; 10966 mutex_exit(&rp->r_statelock); 10967 } 10968 if (bfp->l_type != F_UNLCK) { 10969 rc = ENOLCK; 10970 goto done; 10971 } 10972 } 10973 } 10974 10975 /* 10976 * Call the lock manager to do the real work of contacting 10977 * the server and obtaining the lock. 10978 */ 10979 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10980 cr, &e, NULL, NULL); 10981 rc = e.error; 10982 10983 if (rc == 0) 10984 nfs4_lockcompletion(vp, cmd); 10985 10986 done: 10987 nfs_rw_exit(&rp->r_lkserlock); 10988 10989 return (rc); 10990 } 10991 10992 /* 10993 * Free storage space associated with the specified vnode. The portion 10994 * to be freed is specified by bfp->l_start and bfp->l_len (already 10995 * normalized to a "whence" of 0). 10996 * 10997 * This is an experimental facility whose continued existence is not 10998 * guaranteed. Currently, we only support the special case 10999 * of l_len == 0, meaning free to end of file. 11000 */ 11001 /* ARGSUSED */ 11002 static int 11003 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 11004 offset_t offset, cred_t *cr, caller_context_t *ct) 11005 { 11006 int error; 11007 11008 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11009 return (EIO); 11010 ASSERT(vp->v_type == VREG); 11011 if (cmd != F_FREESP) 11012 return (EINVAL); 11013 11014 error = convoff(vp, bfp, 0, offset); 11015 if (!error) { 11016 ASSERT(bfp->l_start >= 0); 11017 if (bfp->l_len == 0) { 11018 struct vattr va; 11019 11020 va.va_mask = AT_SIZE; 11021 va.va_size = bfp->l_start; 11022 error = nfs4setattr(vp, &va, 0, cr, NULL); 11023 11024 if (error == 0 && bfp->l_start == 0) 11025 vnevent_truncate(vp, ct); 11026 } else 11027 error = EINVAL; 11028 } 11029 11030 return (error); 11031 } 11032 11033 /* ARGSUSED */ 11034 int 11035 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 11036 { 11037 rnode4_t *rp; 11038 rp = VTOR4(vp); 11039 11040 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 11041 vp = RTOV4(rp); 11042 } 11043 *vpp = vp; 11044 return (0); 11045 } 11046 11047 /* 11048 * Setup and add an address space callback to do the work of the delmap call. 11049 * The callback will (and must be) deleted in the actual callback function. 11050 * 11051 * This is done in order to take care of the problem that we have with holding 11052 * the address space's a_lock for a long period of time (e.g. if the NFS server 11053 * is down). Callbacks will be executed in the address space code while the 11054 * a_lock is not held. Holding the address space's a_lock causes things such 11055 * as ps and fork to hang because they are trying to acquire this lock as well. 11056 */ 11057 /* ARGSUSED */ 11058 static int 11059 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 11060 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 11061 caller_context_t *ct) 11062 { 11063 int caller_found; 11064 int error; 11065 rnode4_t *rp; 11066 nfs4_delmap_args_t *dmapp; 11067 nfs4_delmapcall_t *delmap_call; 11068 11069 if (vp->v_flag & VNOMAP) 11070 return (ENOSYS); 11071 11072 /* 11073 * A process may not change zones if it has NFS pages mmap'ed 11074 * in, so we can't legitimately get here from the wrong zone. 11075 */ 11076 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11077 11078 rp = VTOR4(vp); 11079 11080 /* 11081 * The way that the address space of this process deletes its mapping 11082 * of this file is via the following call chains: 11083 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11084 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11085 * 11086 * With the use of address space callbacks we are allowed to drop the 11087 * address space lock, a_lock, while executing the NFS operations that 11088 * need to go over the wire. Returning EAGAIN to the caller of this 11089 * function is what drives the execution of the callback that we add 11090 * below. The callback will be executed by the address space code 11091 * after dropping the a_lock. When the callback is finished, since 11092 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 11093 * is called again on the same segment to finish the rest of the work 11094 * that needs to happen during unmapping. 11095 * 11096 * This action of calling back into the segment driver causes 11097 * nfs4_delmap() to get called again, but since the callback was 11098 * already executed at this point, it already did the work and there 11099 * is nothing left for us to do. 11100 * 11101 * To Summarize: 11102 * - The first time nfs4_delmap is called by the current thread is when 11103 * we add the caller associated with this delmap to the delmap caller 11104 * list, add the callback, and return EAGAIN. 11105 * - The second time in this call chain when nfs4_delmap is called we 11106 * will find this caller in the delmap caller list and realize there 11107 * is no more work to do thus removing this caller from the list and 11108 * returning the error that was set in the callback execution. 11109 */ 11110 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 11111 if (caller_found) { 11112 /* 11113 * 'error' is from the actual delmap operations. To avoid 11114 * hangs, we need to handle the return of EAGAIN differently 11115 * since this is what drives the callback execution. 11116 * In this case, we don't want to return EAGAIN and do the 11117 * callback execution because there are none to execute. 11118 */ 11119 if (error == EAGAIN) 11120 return (0); 11121 else 11122 return (error); 11123 } 11124 11125 /* current caller was not in the list */ 11126 delmap_call = nfs4_init_delmapcall(); 11127 11128 mutex_enter(&rp->r_statelock); 11129 list_insert_tail(&rp->r_indelmap, delmap_call); 11130 mutex_exit(&rp->r_statelock); 11131 11132 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 11133 11134 dmapp->vp = vp; 11135 dmapp->off = off; 11136 dmapp->addr = addr; 11137 dmapp->len = len; 11138 dmapp->prot = prot; 11139 dmapp->maxprot = maxprot; 11140 dmapp->flags = flags; 11141 dmapp->cr = cr; 11142 dmapp->caller = delmap_call; 11143 11144 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 11145 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 11146 11147 return (error ? error : EAGAIN); 11148 } 11149 11150 static nfs4_delmapcall_t * 11151 nfs4_init_delmapcall() 11152 { 11153 nfs4_delmapcall_t *delmap_call; 11154 11155 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 11156 delmap_call->call_id = curthread; 11157 delmap_call->error = 0; 11158 11159 return (delmap_call); 11160 } 11161 11162 static void 11163 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 11164 { 11165 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 11166 } 11167 11168 /* 11169 * Searches for the current delmap caller (based on curthread) in the list of 11170 * callers. If it is found, we remove it and free the delmap caller. 11171 * Returns: 11172 * 0 if the caller wasn't found 11173 * 1 if the caller was found, removed and freed. *errp will be set 11174 * to what the result of the delmap was. 11175 */ 11176 static int 11177 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 11178 { 11179 nfs4_delmapcall_t *delmap_call; 11180 11181 /* 11182 * If the list doesn't exist yet, we create it and return 11183 * that the caller wasn't found. No list = no callers. 11184 */ 11185 mutex_enter(&rp->r_statelock); 11186 if (!(rp->r_flags & R4DELMAPLIST)) { 11187 /* The list does not exist */ 11188 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 11189 offsetof(nfs4_delmapcall_t, call_node)); 11190 rp->r_flags |= R4DELMAPLIST; 11191 mutex_exit(&rp->r_statelock); 11192 return (0); 11193 } else { 11194 /* The list exists so search it */ 11195 for (delmap_call = list_head(&rp->r_indelmap); 11196 delmap_call != NULL; 11197 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 11198 if (delmap_call->call_id == curthread) { 11199 /* current caller is in the list */ 11200 *errp = delmap_call->error; 11201 list_remove(&rp->r_indelmap, delmap_call); 11202 mutex_exit(&rp->r_statelock); 11203 nfs4_free_delmapcall(delmap_call); 11204 return (1); 11205 } 11206 } 11207 } 11208 mutex_exit(&rp->r_statelock); 11209 return (0); 11210 } 11211 11212 /* 11213 * Remove some pages from an mmap'd vnode. Just update the 11214 * count of pages. If doing close-to-open, then flush and 11215 * commit all of the pages associated with this file. 11216 * Otherwise, start an asynchronous page flush to write out 11217 * any dirty pages. This will also associate a credential 11218 * with the rnode which can be used to write the pages. 11219 */ 11220 /* ARGSUSED */ 11221 static void 11222 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11223 { 11224 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11225 rnode4_t *rp; 11226 mntinfo4_t *mi; 11227 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11228 11229 rp = VTOR4(dmapp->vp); 11230 mi = VTOMI4(dmapp->vp); 11231 11232 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11233 ASSERT(rp->r_mapcnt >= 0); 11234 11235 /* 11236 * Initiate a page flush and potential commit if there are 11237 * pages, the file system was not mounted readonly, the segment 11238 * was mapped shared, and the pages themselves were writeable. 11239 */ 11240 if (nfs4_has_pages(dmapp->vp) && 11241 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11242 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11243 mutex_enter(&rp->r_statelock); 11244 rp->r_flags |= R4DIRTY; 11245 mutex_exit(&rp->r_statelock); 11246 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11247 dmapp->len, dmapp->cr); 11248 if (!e.error) { 11249 mutex_enter(&rp->r_statelock); 11250 e.error = rp->r_error; 11251 rp->r_error = 0; 11252 mutex_exit(&rp->r_statelock); 11253 } 11254 } else 11255 e.error = 0; 11256 11257 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11258 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11259 B_INVAL, dmapp->cr, NULL); 11260 11261 if (e.error) { 11262 e.stat = puterrno4(e.error); 11263 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11264 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11265 dmapp->caller->error = e.error; 11266 } 11267 11268 /* Check to see if we need to close the file */ 11269 11270 if (dmapp->vp->v_type == VREG) { 11271 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11272 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11273 11274 if (e.error != 0 || e.stat != NFS4_OK) { 11275 /* 11276 * Since it is possible that e.error == 0 and 11277 * e.stat != NFS4_OK (and vice versa), 11278 * we do the proper checking in order to get both 11279 * e.error and e.stat reporting the correct info. 11280 */ 11281 if (e.stat == NFS4_OK) 11282 e.stat = puterrno4(e.error); 11283 if (e.error == 0) 11284 e.error = geterrno4(e.stat); 11285 11286 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11287 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11288 dmapp->caller->error = e.error; 11289 } 11290 } 11291 11292 (void) as_delete_callback(as, arg); 11293 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11294 } 11295 11296 11297 static uint_t 11298 fattr4_maxfilesize_to_bits(uint64_t ll) 11299 { 11300 uint_t l = 1; 11301 11302 if (ll == 0) { 11303 return (0); 11304 } 11305 11306 if (ll & 0xffffffff00000000) { 11307 l += 32; ll >>= 32; 11308 } 11309 if (ll & 0xffff0000) { 11310 l += 16; ll >>= 16; 11311 } 11312 if (ll & 0xff00) { 11313 l += 8; ll >>= 8; 11314 } 11315 if (ll & 0xf0) { 11316 l += 4; ll >>= 4; 11317 } 11318 if (ll & 0xc) { 11319 l += 2; ll >>= 2; 11320 } 11321 if (ll & 0x2) { 11322 l += 1; 11323 } 11324 return (l); 11325 } 11326 11327 static int 11328 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr) 11329 { 11330 vnode_t *avp = NULL; 11331 int error; 11332 11333 if ((error = nfs4lookup_xattr(vp, "", &avp, 11334 LOOKUP_XATTR, cr)) == 0) 11335 error = do_xattr_exists_check(avp, valp, cr); 11336 if (avp) 11337 VN_RELE(avp); 11338 11339 return (error); 11340 } 11341 11342 /* ARGSUSED */ 11343 int 11344 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 11345 caller_context_t *ct) 11346 { 11347 int error; 11348 hrtime_t t; 11349 rnode4_t *rp; 11350 nfs4_ga_res_t gar; 11351 nfs4_ga_ext_res_t ger; 11352 11353 gar.n4g_ext_res = &ger; 11354 11355 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11356 return (EIO); 11357 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11358 *valp = MAXPATHLEN; 11359 return (0); 11360 } 11361 if (cmd == _PC_ACL_ENABLED) { 11362 *valp = _ACL_ACE_ENABLED; 11363 return (0); 11364 } 11365 11366 rp = VTOR4(vp); 11367 if (cmd == _PC_XATTR_EXISTS) { 11368 /* 11369 * The existence of the xattr directory is not sufficient 11370 * for determining whether generic user attributes exists. 11371 * The attribute directory could only be a transient directory 11372 * used for Solaris sysattr support. Do a small readdir 11373 * to verify if the only entries are sysattrs or not. 11374 * 11375 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11376 * is NULL. Once the xadir vp exists, we can create xattrs, 11377 * and we don't have any way to update the "base" object's 11378 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11379 * could help out. 11380 */ 11381 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11382 rp->r_xattr_dir == NULL) { 11383 return (nfs4_have_xattrs(vp, valp, cr)); 11384 } 11385 } else { /* OLD CODE */ 11386 if (ATTRCACHE4_VALID(vp)) { 11387 mutex_enter(&rp->r_statelock); 11388 if (rp->r_pathconf.pc4_cache_valid) { 11389 error = 0; 11390 switch (cmd) { 11391 case _PC_FILESIZEBITS: 11392 *valp = 11393 rp->r_pathconf.pc4_filesizebits; 11394 break; 11395 case _PC_LINK_MAX: 11396 *valp = 11397 rp->r_pathconf.pc4_link_max; 11398 break; 11399 case _PC_NAME_MAX: 11400 *valp = 11401 rp->r_pathconf.pc4_name_max; 11402 break; 11403 case _PC_CHOWN_RESTRICTED: 11404 *valp = 11405 rp->r_pathconf.pc4_chown_restricted; 11406 break; 11407 case _PC_NO_TRUNC: 11408 *valp = 11409 rp->r_pathconf.pc4_no_trunc; 11410 break; 11411 default: 11412 error = EINVAL; 11413 break; 11414 } 11415 mutex_exit(&rp->r_statelock); 11416 #ifdef DEBUG 11417 nfs4_pathconf_cache_hits++; 11418 #endif 11419 return (error); 11420 } 11421 mutex_exit(&rp->r_statelock); 11422 } 11423 } 11424 #ifdef DEBUG 11425 nfs4_pathconf_cache_misses++; 11426 #endif 11427 11428 t = gethrtime(); 11429 11430 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11431 11432 if (error) { 11433 mutex_enter(&rp->r_statelock); 11434 rp->r_pathconf.pc4_cache_valid = FALSE; 11435 rp->r_pathconf.pc4_xattr_valid = FALSE; 11436 mutex_exit(&rp->r_statelock); 11437 return (error); 11438 } 11439 11440 /* interpret the max filesize */ 11441 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11442 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11443 11444 /* Store the attributes we just received */ 11445 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11446 11447 switch (cmd) { 11448 case _PC_FILESIZEBITS: 11449 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11450 break; 11451 case _PC_LINK_MAX: 11452 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11453 break; 11454 case _PC_NAME_MAX: 11455 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11456 break; 11457 case _PC_CHOWN_RESTRICTED: 11458 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11459 break; 11460 case _PC_NO_TRUNC: 11461 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11462 break; 11463 case _PC_XATTR_EXISTS: 11464 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) { 11465 if (error = nfs4_have_xattrs(vp, valp, cr)) 11466 return (error); 11467 } 11468 break; 11469 default: 11470 return (EINVAL); 11471 } 11472 11473 return (0); 11474 } 11475 11476 /* 11477 * Called by async thread to do synchronous pageio. Do the i/o, wait 11478 * for it to complete, and cleanup the page list when done. 11479 */ 11480 static int 11481 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11482 int flags, cred_t *cr) 11483 { 11484 int error; 11485 11486 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11487 11488 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11489 if (flags & B_READ) 11490 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11491 else 11492 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11493 return (error); 11494 } 11495 11496 /* ARGSUSED */ 11497 static int 11498 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11499 int flags, cred_t *cr, caller_context_t *ct) 11500 { 11501 int error; 11502 rnode4_t *rp; 11503 11504 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11505 return (EIO); 11506 11507 if (pp == NULL) 11508 return (EINVAL); 11509 11510 rp = VTOR4(vp); 11511 mutex_enter(&rp->r_statelock); 11512 rp->r_count++; 11513 mutex_exit(&rp->r_statelock); 11514 11515 if (flags & B_ASYNC) { 11516 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11517 nfs4_sync_pageio); 11518 } else 11519 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11520 mutex_enter(&rp->r_statelock); 11521 rp->r_count--; 11522 cv_broadcast(&rp->r_cv); 11523 mutex_exit(&rp->r_statelock); 11524 return (error); 11525 } 11526 11527 /* ARGSUSED */ 11528 static void 11529 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr, 11530 caller_context_t *ct) 11531 { 11532 int error; 11533 rnode4_t *rp; 11534 page_t *plist; 11535 page_t *pptr; 11536 offset3 offset; 11537 count3 len; 11538 k_sigset_t smask; 11539 11540 /* 11541 * We should get called with fl equal to either B_FREE or 11542 * B_INVAL. Any other value is illegal. 11543 * 11544 * The page that we are either supposed to free or destroy 11545 * should be exclusive locked and its io lock should not 11546 * be held. 11547 */ 11548 ASSERT(fl == B_FREE || fl == B_INVAL); 11549 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11550 11551 rp = VTOR4(vp); 11552 11553 /* 11554 * If the page doesn't need to be committed or we shouldn't 11555 * even bother attempting to commit it, then just make sure 11556 * that the p_fsdata byte is clear and then either free or 11557 * destroy the page as appropriate. 11558 */ 11559 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11560 pp->p_fsdata = C_NOCOMMIT; 11561 if (fl == B_FREE) 11562 page_free(pp, dn); 11563 else 11564 page_destroy(pp, dn); 11565 return; 11566 } 11567 11568 /* 11569 * If there is a page invalidation operation going on, then 11570 * if this is one of the pages being destroyed, then just 11571 * clear the p_fsdata byte and then either free or destroy 11572 * the page as appropriate. 11573 */ 11574 mutex_enter(&rp->r_statelock); 11575 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11576 mutex_exit(&rp->r_statelock); 11577 pp->p_fsdata = C_NOCOMMIT; 11578 if (fl == B_FREE) 11579 page_free(pp, dn); 11580 else 11581 page_destroy(pp, dn); 11582 return; 11583 } 11584 11585 /* 11586 * If we are freeing this page and someone else is already 11587 * waiting to do a commit, then just unlock the page and 11588 * return. That other thread will take care of commiting 11589 * this page. The page can be freed sometime after the 11590 * commit has finished. Otherwise, if the page is marked 11591 * as delay commit, then we may be getting called from 11592 * pvn_write_done, one page at a time. This could result 11593 * in one commit per page, so we end up doing lots of small 11594 * commits instead of fewer larger commits. This is bad, 11595 * we want do as few commits as possible. 11596 */ 11597 if (fl == B_FREE) { 11598 if (rp->r_flags & R4COMMITWAIT) { 11599 page_unlock(pp); 11600 mutex_exit(&rp->r_statelock); 11601 return; 11602 } 11603 if (pp->p_fsdata == C_DELAYCOMMIT) { 11604 pp->p_fsdata = C_COMMIT; 11605 page_unlock(pp); 11606 mutex_exit(&rp->r_statelock); 11607 return; 11608 } 11609 } 11610 11611 /* 11612 * Check to see if there is a signal which would prevent an 11613 * attempt to commit the pages from being successful. If so, 11614 * then don't bother with all of the work to gather pages and 11615 * generate the unsuccessful RPC. Just return from here and 11616 * let the page be committed at some later time. 11617 */ 11618 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11619 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11620 sigunintr(&smask); 11621 page_unlock(pp); 11622 mutex_exit(&rp->r_statelock); 11623 return; 11624 } 11625 sigunintr(&smask); 11626 11627 /* 11628 * We are starting to need to commit pages, so let's try 11629 * to commit as many as possible at once to reduce the 11630 * overhead. 11631 * 11632 * Set the `commit inprogress' state bit. We must 11633 * first wait until any current one finishes. Then 11634 * we initialize the c_pages list with this page. 11635 */ 11636 while (rp->r_flags & R4COMMIT) { 11637 rp->r_flags |= R4COMMITWAIT; 11638 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11639 rp->r_flags &= ~R4COMMITWAIT; 11640 } 11641 rp->r_flags |= R4COMMIT; 11642 mutex_exit(&rp->r_statelock); 11643 ASSERT(rp->r_commit.c_pages == NULL); 11644 rp->r_commit.c_pages = pp; 11645 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11646 rp->r_commit.c_commlen = PAGESIZE; 11647 11648 /* 11649 * Gather together all other pages which can be committed. 11650 * They will all be chained off r_commit.c_pages. 11651 */ 11652 nfs4_get_commit(vp); 11653 11654 /* 11655 * Clear the `commit inprogress' status and disconnect 11656 * the list of pages to be committed from the rnode. 11657 * At this same time, we also save the starting offset 11658 * and length of data to be committed on the server. 11659 */ 11660 plist = rp->r_commit.c_pages; 11661 rp->r_commit.c_pages = NULL; 11662 offset = rp->r_commit.c_commbase; 11663 len = rp->r_commit.c_commlen; 11664 mutex_enter(&rp->r_statelock); 11665 rp->r_flags &= ~R4COMMIT; 11666 cv_broadcast(&rp->r_commit.c_cv); 11667 mutex_exit(&rp->r_statelock); 11668 11669 if (curproc == proc_pageout || curproc == proc_fsflush || 11670 nfs_zone() != VTOMI4(vp)->mi_zone) { 11671 nfs4_async_commit(vp, plist, offset, len, 11672 cr, do_nfs4_async_commit); 11673 return; 11674 } 11675 11676 /* 11677 * Actually generate the COMMIT op over the wire operation. 11678 */ 11679 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11680 11681 /* 11682 * If we got an error during the commit, just unlock all 11683 * of the pages. The pages will get retransmitted to the 11684 * server during a putpage operation. 11685 */ 11686 if (error) { 11687 while (plist != NULL) { 11688 pptr = plist; 11689 page_sub(&plist, pptr); 11690 page_unlock(pptr); 11691 } 11692 return; 11693 } 11694 11695 /* 11696 * We've tried as hard as we can to commit the data to stable 11697 * storage on the server. We just unlock the rest of the pages 11698 * and clear the commit required state. They will be put 11699 * onto the tail of the cachelist if they are nolonger 11700 * mapped. 11701 */ 11702 while (plist != pp) { 11703 pptr = plist; 11704 page_sub(&plist, pptr); 11705 pptr->p_fsdata = C_NOCOMMIT; 11706 page_unlock(pptr); 11707 } 11708 11709 /* 11710 * It is possible that nfs4_commit didn't return error but 11711 * some other thread has modified the page we are going 11712 * to free/destroy. 11713 * In this case we need to rewrite the page. Do an explicit check 11714 * before attempting to free/destroy the page. If modified, needs to 11715 * be rewritten so unlock the page and return. 11716 */ 11717 if (hat_ismod(pp)) { 11718 pp->p_fsdata = C_NOCOMMIT; 11719 page_unlock(pp); 11720 return; 11721 } 11722 11723 /* 11724 * Now, as appropriate, either free or destroy the page 11725 * that we were called with. 11726 */ 11727 pp->p_fsdata = C_NOCOMMIT; 11728 if (fl == B_FREE) 11729 page_free(pp, dn); 11730 else 11731 page_destroy(pp, dn); 11732 } 11733 11734 /* 11735 * Commit requires that the current fh be the file written to. 11736 * The compound op structure is: 11737 * PUTFH(file), COMMIT 11738 */ 11739 static int 11740 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11741 { 11742 COMPOUND4args_clnt args; 11743 COMPOUND4res_clnt res; 11744 COMMIT4res *cm_res; 11745 nfs_argop4 argop[2]; 11746 nfs_resop4 *resop; 11747 int doqueue; 11748 mntinfo4_t *mi; 11749 rnode4_t *rp; 11750 cred_t *cred_otw = NULL; 11751 bool_t needrecov = FALSE; 11752 nfs4_recov_state_t recov_state; 11753 nfs4_open_stream_t *osp = NULL; 11754 bool_t first_time = TRUE; /* first time getting OTW cred */ 11755 bool_t last_time = FALSE; /* last time getting OTW cred */ 11756 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11757 11758 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11759 11760 rp = VTOR4(vp); 11761 11762 mi = VTOMI4(vp); 11763 recov_state.rs_flags = 0; 11764 recov_state.rs_num_retry_despite_err = 0; 11765 get_commit_cred: 11766 /* 11767 * Releases the osp, if a valid open stream is provided. 11768 * Puts a hold on the cred_otw and the new osp (if found). 11769 */ 11770 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11771 &first_time, &last_time); 11772 args.ctag = TAG_COMMIT; 11773 recov_retry: 11774 /* 11775 * Commit ops: putfh file; commit 11776 */ 11777 args.array_len = 2; 11778 args.array = argop; 11779 11780 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11781 &recov_state, NULL); 11782 if (e.error) { 11783 crfree(cred_otw); 11784 if (osp != NULL) 11785 open_stream_rele(osp, rp); 11786 return (e.error); 11787 } 11788 11789 /* putfh directory */ 11790 argop[0].argop = OP_CPUTFH; 11791 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11792 11793 /* commit */ 11794 argop[1].argop = OP_COMMIT; 11795 argop[1].nfs_argop4_u.opcommit.offset = offset; 11796 argop[1].nfs_argop4_u.opcommit.count = count; 11797 11798 doqueue = 1; 11799 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11800 11801 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11802 if (!needrecov && e.error) { 11803 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11804 needrecov); 11805 crfree(cred_otw); 11806 if (e.error == EACCES && last_time == FALSE) 11807 goto get_commit_cred; 11808 if (osp != NULL) 11809 open_stream_rele(osp, rp); 11810 return (e.error); 11811 } 11812 11813 if (needrecov) { 11814 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11815 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) { 11816 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11817 &recov_state, needrecov); 11818 if (!e.error) 11819 (void) xdr_free(xdr_COMPOUND4res_clnt, 11820 (caddr_t)&res); 11821 goto recov_retry; 11822 } 11823 if (e.error) { 11824 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11825 &recov_state, needrecov); 11826 crfree(cred_otw); 11827 if (osp != NULL) 11828 open_stream_rele(osp, rp); 11829 return (e.error); 11830 } 11831 /* fall through for res.status case */ 11832 } 11833 11834 if (res.status) { 11835 e.error = geterrno4(res.status); 11836 if (e.error == EACCES && last_time == FALSE) { 11837 crfree(cred_otw); 11838 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11839 &recov_state, needrecov); 11840 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11841 goto get_commit_cred; 11842 } 11843 /* 11844 * Can't do a nfs4_purge_stale_fh here because this 11845 * can cause a deadlock. nfs4_commit can 11846 * be called from nfs4_dispose which can be called 11847 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11848 * can call back to pvn_vplist_dirty. 11849 */ 11850 if (e.error == ESTALE) { 11851 mutex_enter(&rp->r_statelock); 11852 rp->r_flags |= R4STALE; 11853 if (!rp->r_error) 11854 rp->r_error = e.error; 11855 mutex_exit(&rp->r_statelock); 11856 PURGE_ATTRCACHE4(vp); 11857 } else { 11858 mutex_enter(&rp->r_statelock); 11859 if (!rp->r_error) 11860 rp->r_error = e.error; 11861 mutex_exit(&rp->r_statelock); 11862 } 11863 } else { 11864 ASSERT(rp->r_flags & R4HAVEVERF); 11865 resop = &res.array[1]; /* commit res */ 11866 cm_res = &resop->nfs_resop4_u.opcommit; 11867 mutex_enter(&rp->r_statelock); 11868 if (cm_res->writeverf == rp->r_writeverf) { 11869 mutex_exit(&rp->r_statelock); 11870 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11871 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11872 &recov_state, needrecov); 11873 crfree(cred_otw); 11874 if (osp != NULL) 11875 open_stream_rele(osp, rp); 11876 return (0); 11877 } 11878 nfs4_set_mod(vp); 11879 rp->r_writeverf = cm_res->writeverf; 11880 mutex_exit(&rp->r_statelock); 11881 e.error = NFS_VERF_MISMATCH; 11882 } 11883 11884 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11885 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11886 crfree(cred_otw); 11887 if (osp != NULL) 11888 open_stream_rele(osp, rp); 11889 11890 return (e.error); 11891 } 11892 11893 static void 11894 nfs4_set_mod(vnode_t *vp) 11895 { 11896 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11897 11898 /* make sure we're looking at the master vnode, not a shadow */ 11899 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check); 11900 } 11901 11902 /* 11903 * This function is used to gather a page list of the pages which 11904 * can be committed on the server. 11905 * 11906 * The calling thread must have set R4COMMIT. This bit is used to 11907 * serialize access to the commit structure in the rnode. As long 11908 * as the thread has set R4COMMIT, then it can manipulate the commit 11909 * structure without requiring any other locks. 11910 * 11911 * When this function is called from nfs4_dispose() the page passed 11912 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11913 * will skip it. This is not a problem since we initially add the 11914 * page to the r_commit page list. 11915 * 11916 */ 11917 static void 11918 nfs4_get_commit(vnode_t *vp) 11919 { 11920 rnode4_t *rp; 11921 page_t *pp; 11922 kmutex_t *vphm; 11923 11924 rp = VTOR4(vp); 11925 11926 ASSERT(rp->r_flags & R4COMMIT); 11927 11928 /* make sure we're looking at the master vnode, not a shadow */ 11929 11930 if (IS_SHADOW(vp, rp)) 11931 vp = RTOV4(rp); 11932 11933 vphm = page_vnode_mutex(vp); 11934 mutex_enter(vphm); 11935 11936 /* 11937 * If there are no pages associated with this vnode, then 11938 * just return. 11939 */ 11940 if ((pp = vp->v_pages) == NULL) { 11941 mutex_exit(vphm); 11942 return; 11943 } 11944 11945 /* 11946 * Step through all of the pages associated with this vnode 11947 * looking for pages which need to be committed. 11948 */ 11949 do { 11950 /* Skip marker pages. */ 11951 if (pp->p_hash == PVN_VPLIST_HASH_TAG) 11952 continue; 11953 11954 /* 11955 * First short-cut everything (without the page_lock) 11956 * and see if this page does not need to be committed 11957 * or is modified if so then we'll just skip it. 11958 */ 11959 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11960 continue; 11961 11962 /* 11963 * Attempt to lock the page. If we can't, then 11964 * someone else is messing with it or we have been 11965 * called from nfs4_dispose and this is the page that 11966 * nfs4_dispose was called with.. anyway just skip it. 11967 */ 11968 if (!page_trylock(pp, SE_EXCL)) 11969 continue; 11970 11971 /* 11972 * Lets check again now that we have the page lock. 11973 */ 11974 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11975 page_unlock(pp); 11976 continue; 11977 } 11978 11979 /* this had better not be a free page */ 11980 ASSERT(PP_ISFREE(pp) == 0); 11981 11982 /* 11983 * The page needs to be committed and we locked it. 11984 * Update the base and length parameters and add it 11985 * to r_pages. 11986 */ 11987 if (rp->r_commit.c_pages == NULL) { 11988 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11989 rp->r_commit.c_commlen = PAGESIZE; 11990 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11991 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11992 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11993 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11994 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11995 <= pp->p_offset) { 11996 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11997 rp->r_commit.c_commbase + PAGESIZE; 11998 } 11999 page_add(&rp->r_commit.c_pages, pp); 12000 } while ((pp = pp->p_vpnext) != vp->v_pages); 12001 12002 mutex_exit(vphm); 12003 } 12004 12005 /* 12006 * This routine is used to gather together a page list of the pages 12007 * which are to be committed on the server. This routine must not 12008 * be called if the calling thread holds any locked pages. 12009 * 12010 * The calling thread must have set R4COMMIT. This bit is used to 12011 * serialize access to the commit structure in the rnode. As long 12012 * as the thread has set R4COMMIT, then it can manipulate the commit 12013 * structure without requiring any other locks. 12014 */ 12015 static void 12016 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 12017 { 12018 12019 rnode4_t *rp; 12020 page_t *pp; 12021 u_offset_t end; 12022 u_offset_t off; 12023 ASSERT(len != 0); 12024 rp = VTOR4(vp); 12025 ASSERT(rp->r_flags & R4COMMIT); 12026 12027 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12028 12029 /* make sure we're looking at the master vnode, not a shadow */ 12030 12031 if (IS_SHADOW(vp, rp)) 12032 vp = RTOV4(rp); 12033 12034 /* 12035 * If there are no pages associated with this vnode, then 12036 * just return. 12037 */ 12038 if ((pp = vp->v_pages) == NULL) 12039 return; 12040 /* 12041 * Calculate the ending offset. 12042 */ 12043 end = soff + len; 12044 for (off = soff; off < end; off += PAGESIZE) { 12045 /* 12046 * Lookup each page by vp, offset. 12047 */ 12048 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 12049 continue; 12050 /* 12051 * If this page does not need to be committed or is 12052 * modified, then just skip it. 12053 */ 12054 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 12055 page_unlock(pp); 12056 continue; 12057 } 12058 12059 ASSERT(PP_ISFREE(pp) == 0); 12060 /* 12061 * The page needs to be committed and we locked it. 12062 * Update the base and length parameters and add it 12063 * to r_pages. 12064 */ 12065 if (rp->r_commit.c_pages == NULL) { 12066 rp->r_commit.c_commbase = (offset3)pp->p_offset; 12067 rp->r_commit.c_commlen = PAGESIZE; 12068 } else { 12069 rp->r_commit.c_commlen = (offset3)pp->p_offset - 12070 rp->r_commit.c_commbase + PAGESIZE; 12071 } 12072 page_add(&rp->r_commit.c_pages, pp); 12073 } 12074 } 12075 12076 /* 12077 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 12078 * Flushes and commits data to the server. 12079 */ 12080 static int 12081 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 12082 { 12083 int error; 12084 verifier4 write_verf; 12085 rnode4_t *rp = VTOR4(vp); 12086 12087 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12088 12089 /* 12090 * Flush the data portion of the file and then commit any 12091 * portions which need to be committed. This may need to 12092 * be done twice if the server has changed state since 12093 * data was last written. The data will need to be 12094 * rewritten to the server and then a new commit done. 12095 * 12096 * In fact, this may need to be done several times if the 12097 * server is having problems and crashing while we are 12098 * attempting to do this. 12099 */ 12100 12101 top: 12102 /* 12103 * Do a flush based on the poff and plen arguments. This 12104 * will synchronously write out any modified pages in the 12105 * range specified by (poff, plen). This starts all of the 12106 * i/o operations which will be waited for in the next 12107 * call to nfs4_putpage 12108 */ 12109 12110 mutex_enter(&rp->r_statelock); 12111 write_verf = rp->r_writeverf; 12112 mutex_exit(&rp->r_statelock); 12113 12114 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL); 12115 if (error == EAGAIN) 12116 error = 0; 12117 12118 /* 12119 * Do a flush based on the poff and plen arguments. This 12120 * will synchronously write out any modified pages in the 12121 * range specified by (poff, plen) and wait until all of 12122 * the asynchronous i/o's in that range are done as well. 12123 */ 12124 if (!error) 12125 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL); 12126 12127 if (error) 12128 return (error); 12129 12130 mutex_enter(&rp->r_statelock); 12131 if (rp->r_writeverf != write_verf) { 12132 mutex_exit(&rp->r_statelock); 12133 goto top; 12134 } 12135 mutex_exit(&rp->r_statelock); 12136 12137 /* 12138 * Now commit any pages which might need to be committed. 12139 * If the error, NFS_VERF_MISMATCH, is returned, then 12140 * start over with the flush operation. 12141 */ 12142 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 12143 12144 if (error == NFS_VERF_MISMATCH) 12145 goto top; 12146 12147 return (error); 12148 } 12149 12150 /* 12151 * nfs4_commit_vp() will wait for other pending commits and 12152 * will either commit the whole file or a range, plen dictates 12153 * if we commit whole file. a value of zero indicates the whole 12154 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 12155 */ 12156 static int 12157 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 12158 cred_t *cr, int wait_on_writes) 12159 { 12160 rnode4_t *rp; 12161 page_t *plist; 12162 offset3 offset; 12163 count3 len; 12164 12165 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12166 12167 rp = VTOR4(vp); 12168 12169 /* 12170 * before we gather commitable pages make 12171 * sure there are no outstanding async writes 12172 */ 12173 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 12174 mutex_enter(&rp->r_statelock); 12175 while (rp->r_count > 0) { 12176 cv_wait(&rp->r_cv, &rp->r_statelock); 12177 } 12178 mutex_exit(&rp->r_statelock); 12179 } 12180 12181 /* 12182 * Set the `commit inprogress' state bit. We must 12183 * first wait until any current one finishes. 12184 */ 12185 mutex_enter(&rp->r_statelock); 12186 while (rp->r_flags & R4COMMIT) { 12187 rp->r_flags |= R4COMMITWAIT; 12188 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 12189 rp->r_flags &= ~R4COMMITWAIT; 12190 } 12191 rp->r_flags |= R4COMMIT; 12192 mutex_exit(&rp->r_statelock); 12193 12194 /* 12195 * Gather all of the pages which need to be 12196 * committed. 12197 */ 12198 if (plen == 0) 12199 nfs4_get_commit(vp); 12200 else 12201 nfs4_get_commit_range(vp, poff, plen); 12202 12203 /* 12204 * Clear the `commit inprogress' bit and disconnect the 12205 * page list which was gathered by nfs4_get_commit. 12206 */ 12207 plist = rp->r_commit.c_pages; 12208 rp->r_commit.c_pages = NULL; 12209 offset = rp->r_commit.c_commbase; 12210 len = rp->r_commit.c_commlen; 12211 mutex_enter(&rp->r_statelock); 12212 rp->r_flags &= ~R4COMMIT; 12213 cv_broadcast(&rp->r_commit.c_cv); 12214 mutex_exit(&rp->r_statelock); 12215 12216 /* 12217 * If any pages need to be committed, commit them and 12218 * then unlock them so that they can be freed some 12219 * time later. 12220 */ 12221 if (plist == NULL) 12222 return (0); 12223 12224 /* 12225 * No error occurred during the flush portion 12226 * of this operation, so now attempt to commit 12227 * the data to stable storage on the server. 12228 * 12229 * This will unlock all of the pages on the list. 12230 */ 12231 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12232 } 12233 12234 static int 12235 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12236 cred_t *cr) 12237 { 12238 int error; 12239 page_t *pp; 12240 12241 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12242 12243 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12244 12245 /* 12246 * If we got an error, then just unlock all of the pages 12247 * on the list. 12248 */ 12249 if (error) { 12250 while (plist != NULL) { 12251 pp = plist; 12252 page_sub(&plist, pp); 12253 page_unlock(pp); 12254 } 12255 return (error); 12256 } 12257 /* 12258 * We've tried as hard as we can to commit the data to stable 12259 * storage on the server. We just unlock the pages and clear 12260 * the commit required state. They will get freed later. 12261 */ 12262 while (plist != NULL) { 12263 pp = plist; 12264 page_sub(&plist, pp); 12265 pp->p_fsdata = C_NOCOMMIT; 12266 page_unlock(pp); 12267 } 12268 12269 return (error); 12270 } 12271 12272 static void 12273 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12274 cred_t *cr) 12275 { 12276 12277 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12278 } 12279 12280 /*ARGSUSED*/ 12281 static int 12282 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12283 caller_context_t *ct) 12284 { 12285 int error = 0; 12286 mntinfo4_t *mi; 12287 vattr_t va; 12288 vsecattr_t nfsace4_vsap; 12289 12290 mi = VTOMI4(vp); 12291 if (nfs_zone() != mi->mi_zone) 12292 return (EIO); 12293 if (mi->mi_flags & MI4_ACL) { 12294 /* if we have a delegation, return it */ 12295 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12296 (void) nfs4delegreturn(VTOR4(vp), 12297 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12298 12299 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12300 NFS4_ACL_SET); 12301 if (error) /* EINVAL */ 12302 return (error); 12303 12304 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12305 /* 12306 * These are aclent_t type entries. 12307 */ 12308 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12309 vp->v_type == VDIR, FALSE); 12310 if (error) 12311 return (error); 12312 } else { 12313 /* 12314 * These are ace_t type entries. 12315 */ 12316 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12317 FALSE); 12318 if (error) 12319 return (error); 12320 } 12321 bzero(&va, sizeof (va)); 12322 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12323 vs_ace4_destroy(&nfsace4_vsap); 12324 return (error); 12325 } 12326 return (ENOSYS); 12327 } 12328 12329 /* ARGSUSED */ 12330 int 12331 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12332 caller_context_t *ct) 12333 { 12334 int error; 12335 mntinfo4_t *mi; 12336 nfs4_ga_res_t gar; 12337 rnode4_t *rp = VTOR4(vp); 12338 12339 mi = VTOMI4(vp); 12340 if (nfs_zone() != mi->mi_zone) 12341 return (EIO); 12342 12343 bzero(&gar, sizeof (gar)); 12344 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12345 12346 /* 12347 * vsecattr->vsa_mask holds the original acl request mask. 12348 * This is needed when determining what to return. 12349 * (See: nfs4_create_getsecattr_return()) 12350 */ 12351 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12352 if (error) /* EINVAL */ 12353 return (error); 12354 12355 /* 12356 * If this is a referral stub, don't try to go OTW for an ACL 12357 */ 12358 if (RP_ISSTUB_REFERRAL(VTOR4(vp))) 12359 return (fs_fab_acl(vp, vsecattr, flag, cr, ct)); 12360 12361 if (mi->mi_flags & MI4_ACL) { 12362 /* 12363 * Check if the data is cached and the cache is valid. If it 12364 * is we don't go over the wire. 12365 */ 12366 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12367 mutex_enter(&rp->r_statelock); 12368 if (rp->r_secattr != NULL) { 12369 error = nfs4_create_getsecattr_return( 12370 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12371 rp->r_attr.va_gid, 12372 vp->v_type == VDIR); 12373 if (!error) { /* error == 0 - Success! */ 12374 mutex_exit(&rp->r_statelock); 12375 return (error); 12376 } 12377 } 12378 mutex_exit(&rp->r_statelock); 12379 } 12380 12381 /* 12382 * The getattr otw call will always get both the acl, in 12383 * the form of a list of nfsace4's, and the number of acl 12384 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12385 */ 12386 gar.n4g_va.va_mask = AT_ALL; 12387 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12388 if (error) { 12389 vs_ace4_destroy(&gar.n4g_vsa); 12390 if (error == ENOTSUP || error == EOPNOTSUPP) 12391 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12392 return (error); 12393 } 12394 12395 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12396 /* 12397 * No error was returned, but according to the response 12398 * bitmap, neither was an acl. 12399 */ 12400 vs_ace4_destroy(&gar.n4g_vsa); 12401 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12402 return (error); 12403 } 12404 12405 /* 12406 * Update the cache with the ACL. 12407 */ 12408 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12409 12410 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12411 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12412 vp->v_type == VDIR); 12413 vs_ace4_destroy(&gar.n4g_vsa); 12414 if ((error) && (vsecattr->vsa_mask & 12415 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12416 (error != EACCES)) { 12417 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12418 } 12419 return (error); 12420 } 12421 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12422 return (error); 12423 } 12424 12425 /* 12426 * The function returns: 12427 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12428 * - EINVAL if the passed in "acl_mask" is an invalid request. 12429 * 12430 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12431 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12432 * 12433 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12434 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12435 * - We have a count field set without the corresponding acl field set. (e.g. - 12436 * VSA_ACECNT is set, but VSA_ACE is not) 12437 */ 12438 static int 12439 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12440 { 12441 /* Shortcut the masks that are always valid. */ 12442 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12443 return (0); 12444 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12445 return (0); 12446 12447 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12448 /* 12449 * We can't have any VSA_ACL type stuff in the mask now. 12450 */ 12451 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12452 VSA_DFACLCNT)) 12453 return (EINVAL); 12454 12455 if (op == NFS4_ACL_SET) { 12456 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12457 return (EINVAL); 12458 } 12459 } 12460 12461 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12462 /* 12463 * We can't have any VSA_ACE type stuff in the mask now. 12464 */ 12465 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12466 return (EINVAL); 12467 12468 if (op == NFS4_ACL_SET) { 12469 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12470 return (EINVAL); 12471 12472 if ((acl_mask & VSA_DFACLCNT) && 12473 !(acl_mask & VSA_DFACL)) 12474 return (EINVAL); 12475 } 12476 } 12477 return (0); 12478 } 12479 12480 /* 12481 * The theory behind creating the correct getsecattr return is simply this: 12482 * "Don't return anything that the caller is not expecting to have to free." 12483 */ 12484 static int 12485 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12486 uid_t uid, gid_t gid, int isdir) 12487 { 12488 int error = 0; 12489 /* Save the mask since the translators modify it. */ 12490 uint_t orig_mask = vsap->vsa_mask; 12491 12492 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12493 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE); 12494 12495 if (error) 12496 return (error); 12497 12498 /* 12499 * If the caller only asked for the ace count (VSA_ACECNT) 12500 * don't give them the full acl (VSA_ACE), free it. 12501 */ 12502 if (!orig_mask & VSA_ACE) { 12503 if (vsap->vsa_aclentp != NULL) { 12504 kmem_free(vsap->vsa_aclentp, 12505 vsap->vsa_aclcnt * sizeof (ace_t)); 12506 vsap->vsa_aclentp = NULL; 12507 } 12508 } 12509 vsap->vsa_mask = orig_mask; 12510 12511 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12512 VSA_DFACLCNT)) { 12513 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12514 isdir, FALSE); 12515 12516 if (error) 12517 return (error); 12518 12519 /* 12520 * If the caller only asked for the acl count (VSA_ACLCNT) 12521 * and/or the default acl count (VSA_DFACLCNT) don't give them 12522 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12523 */ 12524 if (!orig_mask & VSA_ACL) { 12525 if (vsap->vsa_aclentp != NULL) { 12526 kmem_free(vsap->vsa_aclentp, 12527 vsap->vsa_aclcnt * sizeof (aclent_t)); 12528 vsap->vsa_aclentp = NULL; 12529 } 12530 } 12531 12532 if (!orig_mask & VSA_DFACL) { 12533 if (vsap->vsa_dfaclentp != NULL) { 12534 kmem_free(vsap->vsa_dfaclentp, 12535 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12536 vsap->vsa_dfaclentp = NULL; 12537 } 12538 } 12539 vsap->vsa_mask = orig_mask; 12540 } 12541 return (0); 12542 } 12543 12544 /* ARGSUSED */ 12545 int 12546 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr, 12547 caller_context_t *ct) 12548 { 12549 int error; 12550 12551 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12552 return (EIO); 12553 /* 12554 * check for valid cmd parameter 12555 */ 12556 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12557 return (EINVAL); 12558 12559 /* 12560 * Check access permissions 12561 */ 12562 if ((cmd & F_SHARE) && 12563 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12564 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12565 return (EBADF); 12566 12567 /* 12568 * If the filesystem is mounted using local locking, pass the 12569 * request off to the local share code. 12570 */ 12571 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12572 return (fs_shrlock(vp, cmd, shr, flag, cr, ct)); 12573 12574 switch (cmd) { 12575 case F_SHARE: 12576 case F_UNSHARE: 12577 /* 12578 * This will be properly implemented later, 12579 * see RFE: 4823948 . 12580 */ 12581 error = EAGAIN; 12582 break; 12583 12584 case F_HASREMOTELOCKS: 12585 /* 12586 * NFS client can't store remote locks itself 12587 */ 12588 shr->s_access = 0; 12589 error = 0; 12590 break; 12591 12592 default: 12593 error = EINVAL; 12594 break; 12595 } 12596 12597 return (error); 12598 } 12599 12600 /* 12601 * Common code called by directory ops to update the attrcache 12602 */ 12603 static int 12604 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12605 hrtime_t t, vnode_t *vp, cred_t *cr) 12606 { 12607 int error = 0; 12608 12609 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12610 12611 if (status != NFS4_OK) { 12612 /* getattr not done or failed */ 12613 PURGE_ATTRCACHE4(vp); 12614 return (error); 12615 } 12616 12617 if (garp) { 12618 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12619 } else { 12620 PURGE_ATTRCACHE4(vp); 12621 } 12622 return (error); 12623 } 12624 12625 /* 12626 * Update directory caches for directory modification ops (link, rename, etc.) 12627 * When dinfo is NULL, manage dircaches in the old way. 12628 */ 12629 static void 12630 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12631 dirattr_info_t *dinfo) 12632 { 12633 rnode4_t *drp = VTOR4(dvp); 12634 12635 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12636 12637 /* Purge rddir cache for dir since it changed */ 12638 if (drp->r_dir != NULL) 12639 nfs4_purge_rddir_cache(dvp); 12640 12641 /* 12642 * If caller provided dinfo, then use it to manage dir caches. 12643 */ 12644 if (dinfo != NULL) { 12645 if (vp != NULL) { 12646 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12647 if (!VTOR4(vp)->created_v4) { 12648 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12649 dnlc_update(dvp, nm, vp); 12650 } else { 12651 /* 12652 * XXX don't update if the created_v4 flag is 12653 * set 12654 */ 12655 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12656 NFS4_DEBUG(nfs4_client_state_debug, 12657 (CE_NOTE, "nfs4_update_dircaches: " 12658 "don't update dnlc: created_v4 flag")); 12659 } 12660 } 12661 12662 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12663 dinfo->di_cred, FALSE, cinfo); 12664 12665 return; 12666 } 12667 12668 /* 12669 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12670 * Since caller modified dir but didn't receive post-dirmod-op dir 12671 * attrs, the dir's attrs must be purged. 12672 * 12673 * XXX this check and dnlc update/purge should really be atomic, 12674 * XXX but can't use rnode statelock because it'll deadlock in 12675 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12676 * XXX does occur. 12677 * 12678 * XXX We also may want to check that atomic is true in the 12679 * XXX change_info struct. If it is not, the change_info may 12680 * XXX reflect changes by more than one clients which means that 12681 * XXX our cache may not be valid. 12682 */ 12683 PURGE_ATTRCACHE4(dvp); 12684 if (drp->r_change == cinfo->before) { 12685 /* no changes took place in the directory prior to our link */ 12686 if (vp != NULL) { 12687 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12688 if (!VTOR4(vp)->created_v4) { 12689 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12690 dnlc_update(dvp, nm, vp); 12691 } else { 12692 /* 12693 * XXX dont' update if the created_v4 flag 12694 * is set 12695 */ 12696 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12697 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12698 "nfs4_update_dircaches: don't" 12699 " update dnlc: created_v4 flag")); 12700 } 12701 } 12702 } else { 12703 /* Another client modified directory - purge its dnlc cache */ 12704 dnlc_purge_vp(dvp); 12705 } 12706 } 12707 12708 /* 12709 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12710 * file. 12711 * 12712 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12713 * file (ie: client recovery) and otherwise set to FALSE. 12714 * 12715 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12716 * initiated) calling functions. 12717 * 12718 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12719 * of resending a 'lost' open request. 12720 * 12721 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12722 * server that hands out BAD_SEQID on open confirm. 12723 * 12724 * Errors are returned via the nfs4_error_t parameter. 12725 */ 12726 void 12727 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12728 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12729 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12730 { 12731 COMPOUND4args_clnt args; 12732 COMPOUND4res_clnt res; 12733 nfs_argop4 argop[2]; 12734 nfs_resop4 *resop; 12735 int doqueue = 1; 12736 mntinfo4_t *mi; 12737 OPEN_CONFIRM4args *open_confirm_args; 12738 int needrecov; 12739 12740 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12741 #if DEBUG 12742 mutex_enter(&oop->oo_lock); 12743 ASSERT(oop->oo_seqid_inuse); 12744 mutex_exit(&oop->oo_lock); 12745 #endif 12746 12747 recov_retry_confirm: 12748 nfs4_error_zinit(ep); 12749 *retry_open = FALSE; 12750 12751 if (resend) 12752 args.ctag = TAG_OPEN_CONFIRM_LOST; 12753 else 12754 args.ctag = TAG_OPEN_CONFIRM; 12755 12756 args.array_len = 2; 12757 args.array = argop; 12758 12759 /* putfh target fh */ 12760 argop[0].argop = OP_CPUTFH; 12761 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12762 12763 argop[1].argop = OP_OPEN_CONFIRM; 12764 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12765 12766 (*seqid) += 1; 12767 open_confirm_args->seqid = *seqid; 12768 open_confirm_args->open_stateid = *stateid; 12769 12770 mi = VTOMI4(vp); 12771 12772 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12773 12774 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12775 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12776 } 12777 12778 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12779 if (!needrecov && ep->error) 12780 return; 12781 12782 if (needrecov) { 12783 bool_t abort = FALSE; 12784 12785 if (reopening_file == FALSE) { 12786 nfs4_bseqid_entry_t *bsep = NULL; 12787 12788 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12789 bsep = nfs4_create_bseqid_entry(oop, NULL, 12790 vp, 0, args.ctag, 12791 open_confirm_args->seqid); 12792 12793 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 12794 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL); 12795 if (bsep) { 12796 kmem_free(bsep, sizeof (*bsep)); 12797 if (num_bseqid_retryp && 12798 --(*num_bseqid_retryp) == 0) 12799 abort = TRUE; 12800 } 12801 } 12802 if ((ep->error == ETIMEDOUT || 12803 res.status == NFS4ERR_RESOURCE) && 12804 abort == FALSE && resend == FALSE) { 12805 if (!ep->error) 12806 (void) xdr_free(xdr_COMPOUND4res_clnt, 12807 (caddr_t)&res); 12808 12809 delay(SEC_TO_TICK(confirm_retry_sec)); 12810 goto recov_retry_confirm; 12811 } 12812 /* State may have changed so retry the entire OPEN op */ 12813 if (abort == FALSE) 12814 *retry_open = TRUE; 12815 else 12816 *retry_open = FALSE; 12817 if (!ep->error) 12818 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12819 return; 12820 } 12821 12822 if (res.status) { 12823 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12824 return; 12825 } 12826 12827 resop = &res.array[1]; /* open confirm res */ 12828 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12829 stateid, sizeof (*stateid)); 12830 12831 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12832 } 12833 12834 /* 12835 * Return the credentials associated with a client state object. The 12836 * caller is responsible for freeing the credentials. 12837 */ 12838 12839 static cred_t * 12840 state_to_cred(nfs4_open_stream_t *osp) 12841 { 12842 cred_t *cr; 12843 12844 /* 12845 * It's ok to not lock the open stream and open owner to get 12846 * the oo_cred since this is only written once (upon creation) 12847 * and will not change. 12848 */ 12849 cr = osp->os_open_owner->oo_cred; 12850 crhold(cr); 12851 12852 return (cr); 12853 } 12854 12855 /* 12856 * nfs4_find_sysid 12857 * 12858 * Find the sysid for the knetconfig associated with the given mi. 12859 */ 12860 static struct lm_sysid * 12861 nfs4_find_sysid(mntinfo4_t *mi) 12862 { 12863 ASSERT(nfs_zone() == mi->mi_zone); 12864 12865 /* 12866 * Switch from RDMA knconf to original mount knconf 12867 */ 12868 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12869 mi->mi_curr_serv->sv_hostname, NULL)); 12870 } 12871 12872 #ifdef DEBUG 12873 /* 12874 * Return a string version of the call type for easy reading. 12875 */ 12876 static char * 12877 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12878 { 12879 switch (ctype) { 12880 case NFS4_LCK_CTYPE_NORM: 12881 return ("NORMAL"); 12882 case NFS4_LCK_CTYPE_RECLAIM: 12883 return ("RECLAIM"); 12884 case NFS4_LCK_CTYPE_RESEND: 12885 return ("RESEND"); 12886 case NFS4_LCK_CTYPE_REINSTATE: 12887 return ("REINSTATE"); 12888 default: 12889 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12890 "type %d", ctype); 12891 return (""); 12892 } 12893 } 12894 #endif 12895 12896 /* 12897 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12898 * Unlock requests don't have an over-the-wire locktype, so we just return 12899 * something non-threatening. 12900 */ 12901 12902 static nfs_lock_type4 12903 flk_to_locktype(int cmd, int l_type) 12904 { 12905 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12906 12907 switch (l_type) { 12908 case F_UNLCK: 12909 return (READ_LT); 12910 case F_RDLCK: 12911 if (cmd == F_SETLK) 12912 return (READ_LT); 12913 else 12914 return (READW_LT); 12915 case F_WRLCK: 12916 if (cmd == F_SETLK) 12917 return (WRITE_LT); 12918 else 12919 return (WRITEW_LT); 12920 } 12921 panic("flk_to_locktype"); 12922 /*NOTREACHED*/ 12923 } 12924 12925 /* 12926 * Do some preliminary checks for nfs4frlock. 12927 */ 12928 static int 12929 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12930 u_offset_t offset) 12931 { 12932 int error = 0; 12933 12934 /* 12935 * If we are setting a lock, check that the file is opened 12936 * with the correct mode. 12937 */ 12938 if (cmd == F_SETLK || cmd == F_SETLKW) { 12939 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12940 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12941 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12942 "nfs4frlock_validate_args: file was opened with " 12943 "incorrect mode")); 12944 return (EBADF); 12945 } 12946 } 12947 12948 /* Convert the offset. It may need to be restored before returning. */ 12949 if (error = convoff(vp, flk, 0, offset)) { 12950 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12951 "nfs4frlock_validate_args: convoff => error= %d\n", 12952 error)); 12953 return (error); 12954 } 12955 12956 return (error); 12957 } 12958 12959 /* 12960 * Set the flock64's lm_sysid for nfs4frlock. 12961 */ 12962 static int 12963 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12964 { 12965 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12966 12967 /* Find the lm_sysid */ 12968 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12969 12970 if (*lspp == NULL) { 12971 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12972 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12973 return (ENOLCK); 12974 } 12975 12976 flk->l_sysid = lm_sysidt(*lspp); 12977 12978 return (0); 12979 } 12980 12981 /* 12982 * Do the remaining preliminary setup for nfs4frlock. 12983 */ 12984 static void 12985 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12986 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12987 cred_t **cred_otw) 12988 { 12989 /* 12990 * set tick_delay to the base delay time. 12991 * (nfs4_base_wait_time is in msecs) 12992 */ 12993 12994 *tick_delayp = drv_usectohz(nfs4_base_wait_time * 1000); 12995 12996 /* 12997 * If lock is relative to EOF, we need the newest length of the 12998 * file. Therefore invalidate the ATTR_CACHE. 12999 */ 13000 13001 *whencep = flk->l_whence; 13002 13003 if (*whencep == 2) /* SEEK_END */ 13004 PURGE_ATTRCACHE4(vp); 13005 13006 recov_statep->rs_flags = 0; 13007 recov_statep->rs_num_retry_despite_err = 0; 13008 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 13009 } 13010 13011 /* 13012 * Initialize and allocate the data structures necessary for 13013 * the nfs4frlock call. 13014 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 13015 */ 13016 static void 13017 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 13018 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 13019 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 13020 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 13021 { 13022 int argoplist_size; 13023 int num_ops = 2; 13024 13025 *retry = FALSE; 13026 *did_start_fop = FALSE; 13027 *skip_get_err = FALSE; 13028 lost_rqstp->lr_op = 0; 13029 argoplist_size = num_ops * sizeof (nfs_argop4); 13030 /* fill array with zero */ 13031 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 13032 13033 *argspp = argsp; 13034 *respp = NULL; 13035 13036 argsp->array_len = num_ops; 13037 argsp->array = *argopp; 13038 13039 /* initialize in case of error; will get real value down below */ 13040 argsp->ctag = TAG_NONE; 13041 13042 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 13043 *op_hintp = OH_LOCKU; 13044 else 13045 *op_hintp = OH_OTHER; 13046 } 13047 13048 /* 13049 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 13050 * the proper nfs4_server_t for this instance of nfs4frlock. 13051 * Returns 0 (success) or an errno value. 13052 */ 13053 static int 13054 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 13055 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 13056 bool_t *did_start_fop, bool_t *startrecovp) 13057 { 13058 int error = 0; 13059 rnode4_t *rp; 13060 13061 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13062 13063 if (ctype == NFS4_LCK_CTYPE_NORM) { 13064 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 13065 recov_statep, startrecovp); 13066 if (error) 13067 return (error); 13068 *did_start_fop = TRUE; 13069 } else { 13070 *did_start_fop = FALSE; 13071 *startrecovp = FALSE; 13072 } 13073 13074 if (!error) { 13075 rp = VTOR4(vp); 13076 13077 /* If the file failed recovery, just quit. */ 13078 mutex_enter(&rp->r_statelock); 13079 if (rp->r_flags & R4RECOVERR) { 13080 error = EIO; 13081 } 13082 mutex_exit(&rp->r_statelock); 13083 } 13084 13085 return (error); 13086 } 13087 13088 /* 13089 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 13090 * resend nfs4frlock call is initiated by the recovery framework. 13091 * Acquires the lop and oop seqid synchronization. 13092 */ 13093 static void 13094 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 13095 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 13096 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13097 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 13098 { 13099 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 13100 int error; 13101 13102 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 13103 (CE_NOTE, 13104 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 13105 ASSERT(resend_rqstp != NULL); 13106 ASSERT(resend_rqstp->lr_op == OP_LOCK || 13107 resend_rqstp->lr_op == OP_LOCKU); 13108 13109 *oopp = resend_rqstp->lr_oop; 13110 if (resend_rqstp->lr_oop) { 13111 open_owner_hold(resend_rqstp->lr_oop); 13112 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 13113 ASSERT(error == 0); /* recov thread always succeeds */ 13114 } 13115 13116 /* Must resend this lost lock/locku request. */ 13117 ASSERT(resend_rqstp->lr_lop != NULL); 13118 *lopp = resend_rqstp->lr_lop; 13119 lock_owner_hold(resend_rqstp->lr_lop); 13120 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 13121 ASSERT(error == 0); /* recov thread always succeeds */ 13122 13123 *ospp = resend_rqstp->lr_osp; 13124 if (*ospp) 13125 open_stream_hold(resend_rqstp->lr_osp); 13126 13127 if (resend_rqstp->lr_op == OP_LOCK) { 13128 LOCK4args *lock_args; 13129 13130 argop->argop = OP_LOCK; 13131 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 13132 lock_args->locktype = resend_rqstp->lr_locktype; 13133 lock_args->reclaim = 13134 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 13135 lock_args->offset = resend_rqstp->lr_flk->l_start; 13136 lock_args->length = resend_rqstp->lr_flk->l_len; 13137 if (lock_args->length == 0) 13138 lock_args->length = ~lock_args->length; 13139 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 13140 mi2clientid(mi), &lock_args->locker); 13141 13142 switch (resend_rqstp->lr_ctype) { 13143 case NFS4_LCK_CTYPE_RESEND: 13144 argsp->ctag = TAG_LOCK_RESEND; 13145 break; 13146 case NFS4_LCK_CTYPE_REINSTATE: 13147 argsp->ctag = TAG_LOCK_REINSTATE; 13148 break; 13149 case NFS4_LCK_CTYPE_RECLAIM: 13150 argsp->ctag = TAG_LOCK_RECLAIM; 13151 break; 13152 default: 13153 argsp->ctag = TAG_LOCK_UNKNOWN; 13154 break; 13155 } 13156 } else { 13157 LOCKU4args *locku_args; 13158 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 13159 13160 argop->argop = OP_LOCKU; 13161 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 13162 locku_args->locktype = READ_LT; 13163 locku_args->seqid = lop->lock_seqid + 1; 13164 mutex_enter(&lop->lo_lock); 13165 locku_args->lock_stateid = lop->lock_stateid; 13166 mutex_exit(&lop->lo_lock); 13167 locku_args->offset = resend_rqstp->lr_flk->l_start; 13168 locku_args->length = resend_rqstp->lr_flk->l_len; 13169 if (locku_args->length == 0) 13170 locku_args->length = ~locku_args->length; 13171 13172 switch (resend_rqstp->lr_ctype) { 13173 case NFS4_LCK_CTYPE_RESEND: 13174 argsp->ctag = TAG_LOCKU_RESEND; 13175 break; 13176 case NFS4_LCK_CTYPE_REINSTATE: 13177 argsp->ctag = TAG_LOCKU_REINSTATE; 13178 break; 13179 default: 13180 argsp->ctag = TAG_LOCK_UNKNOWN; 13181 break; 13182 } 13183 } 13184 } 13185 13186 /* 13187 * Setup the LOCKT4 arguments. 13188 */ 13189 static void 13190 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13191 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 13192 rnode4_t *rp) 13193 { 13194 LOCKT4args *lockt_args; 13195 13196 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 13197 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13198 argop->argop = OP_LOCKT; 13199 argsp->ctag = TAG_LOCKT; 13200 lockt_args = &argop->nfs_argop4_u.oplockt; 13201 13202 /* 13203 * The locktype will be READ_LT unless it's 13204 * a write lock. We do this because the Solaris 13205 * system call allows the combination of 13206 * F_UNLCK and F_GETLK* and so in that case the 13207 * unlock is mapped to a read. 13208 */ 13209 if (flk->l_type == F_WRLCK) 13210 lockt_args->locktype = WRITE_LT; 13211 else 13212 lockt_args->locktype = READ_LT; 13213 13214 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 13215 /* set the lock owner4 args */ 13216 nfs4_setlockowner_args(&lockt_args->owner, rp, 13217 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13218 flk->l_pid); 13219 lockt_args->offset = flk->l_start; 13220 lockt_args->length = flk->l_len; 13221 if (flk->l_len == 0) 13222 lockt_args->length = ~lockt_args->length; 13223 13224 *lockt_argsp = lockt_args; 13225 } 13226 13227 /* 13228 * If the client is holding a delegation, and the open stream to be used 13229 * with this lock request is a delegation open stream, then re-open the stream. 13230 * Sets the nfs4_error_t to all zeros unless the open stream has already 13231 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13232 * means the caller should retry (like a recovery retry). 13233 */ 13234 static void 13235 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13236 { 13237 open_delegation_type4 dt; 13238 bool_t reopen_needed, force; 13239 nfs4_open_stream_t *osp; 13240 open_claim_type4 oclaim; 13241 rnode4_t *rp = VTOR4(vp); 13242 mntinfo4_t *mi = VTOMI4(vp); 13243 13244 ASSERT(nfs_zone() == mi->mi_zone); 13245 13246 nfs4_error_zinit(ep); 13247 13248 mutex_enter(&rp->r_statev4_lock); 13249 dt = rp->r_deleg_type; 13250 mutex_exit(&rp->r_statev4_lock); 13251 13252 if (dt != OPEN_DELEGATE_NONE) { 13253 nfs4_open_owner_t *oop; 13254 13255 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13256 if (!oop) { 13257 ep->stat = NFS4ERR_IO; 13258 return; 13259 } 13260 /* returns with 'os_sync_lock' held */ 13261 osp = find_open_stream(oop, rp); 13262 if (!osp) { 13263 open_owner_rele(oop); 13264 ep->stat = NFS4ERR_IO; 13265 return; 13266 } 13267 13268 if (osp->os_failed_reopen) { 13269 NFS4_DEBUG((nfs4_open_stream_debug || 13270 nfs4_client_lock_debug), (CE_NOTE, 13271 "nfs4frlock_check_deleg: os_failed_reopen set " 13272 "for osp %p, cr %p, rp %s", (void *)osp, 13273 (void *)cr, rnode4info(rp))); 13274 mutex_exit(&osp->os_sync_lock); 13275 open_stream_rele(osp, rp); 13276 open_owner_rele(oop); 13277 ep->stat = NFS4ERR_IO; 13278 return; 13279 } 13280 13281 /* 13282 * Determine whether a reopen is needed. If this 13283 * is a delegation open stream, then send the open 13284 * to the server to give visibility to the open owner. 13285 * Even if it isn't a delegation open stream, we need 13286 * to check if the previous open CLAIM_DELEGATE_CUR 13287 * was sufficient. 13288 */ 13289 13290 reopen_needed = osp->os_delegation || 13291 ((lt == F_RDLCK && 13292 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13293 (lt == F_WRLCK && 13294 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13295 13296 mutex_exit(&osp->os_sync_lock); 13297 open_owner_rele(oop); 13298 13299 if (reopen_needed) { 13300 /* 13301 * Always use CLAIM_PREVIOUS after server reboot. 13302 * The server will reject CLAIM_DELEGATE_CUR if 13303 * it is used during the grace period. 13304 */ 13305 mutex_enter(&mi->mi_lock); 13306 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13307 oclaim = CLAIM_PREVIOUS; 13308 force = TRUE; 13309 } else { 13310 oclaim = CLAIM_DELEGATE_CUR; 13311 force = FALSE; 13312 } 13313 mutex_exit(&mi->mi_lock); 13314 13315 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13316 if (ep->error == EAGAIN) { 13317 nfs4_error_zinit(ep); 13318 ep->stat = NFS4ERR_DELAY; 13319 } 13320 } 13321 open_stream_rele(osp, rp); 13322 osp = NULL; 13323 } 13324 } 13325 13326 /* 13327 * Setup the LOCKU4 arguments. 13328 * Returns errors via the nfs4_error_t. 13329 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13330 * over-the-wire. The caller must release the 13331 * reference on *lopp. 13332 * NFS4ERR_DELAY caller should retry (like recovery retry) 13333 * (other) unrecoverable error. 13334 */ 13335 static void 13336 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13337 LOCKU4args **locku_argsp, flock64_t *flk, 13338 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13339 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13340 bool_t *skip_get_err, bool_t *go_otwp) 13341 { 13342 nfs4_lock_owner_t *lop = NULL; 13343 LOCKU4args *locku_args; 13344 pid_t pid; 13345 bool_t is_spec = FALSE; 13346 rnode4_t *rp = VTOR4(vp); 13347 13348 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13349 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13350 13351 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13352 if (ep->error || ep->stat) 13353 return; 13354 13355 argop->argop = OP_LOCKU; 13356 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13357 argsp->ctag = TAG_LOCKU_REINSTATE; 13358 else 13359 argsp->ctag = TAG_LOCKU; 13360 locku_args = &argop->nfs_argop4_u.oplocku; 13361 *locku_argsp = locku_args; 13362 13363 /* 13364 * XXX what should locku_args->locktype be? 13365 * setting to ALWAYS be READ_LT so at least 13366 * it is a valid locktype. 13367 */ 13368 13369 locku_args->locktype = READ_LT; 13370 13371 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13372 flk->l_pid; 13373 13374 /* 13375 * Get the lock owner stateid. If no lock owner 13376 * exists, return success. 13377 */ 13378 lop = find_lock_owner(rp, pid, LOWN_ANY); 13379 *lopp = lop; 13380 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13381 is_spec = TRUE; 13382 if (!lop || is_spec) { 13383 /* 13384 * No lock owner so no locks to unlock. 13385 * Return success. If there was a failed 13386 * reclaim earlier, the lock might still be 13387 * registered with the local locking code, 13388 * so notify it of the unlock. 13389 * 13390 * If the lockowner is using a special stateid, 13391 * then the original lock request (that created 13392 * this lockowner) was never successful, so we 13393 * have no lock to undo OTW. 13394 */ 13395 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13396 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13397 "(%ld) so return success", (long)pid)); 13398 13399 if (ctype == NFS4_LCK_CTYPE_NORM) 13400 flk->l_pid = curproc->p_pid; 13401 nfs4_register_lock_locally(vp, flk, flag, offset); 13402 /* 13403 * Release our hold and NULL out so final_cleanup 13404 * doesn't try to end a lock seqid sync we 13405 * never started. 13406 */ 13407 if (is_spec) { 13408 lock_owner_rele(lop); 13409 *lopp = NULL; 13410 } 13411 *skip_get_err = TRUE; 13412 *go_otwp = FALSE; 13413 return; 13414 } 13415 13416 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13417 if (ep->error == EAGAIN) { 13418 lock_owner_rele(lop); 13419 *lopp = NULL; 13420 return; 13421 } 13422 13423 mutex_enter(&lop->lo_lock); 13424 locku_args->lock_stateid = lop->lock_stateid; 13425 mutex_exit(&lop->lo_lock); 13426 locku_args->seqid = lop->lock_seqid + 1; 13427 13428 /* leave the ref count on lop, rele after RPC call */ 13429 13430 locku_args->offset = flk->l_start; 13431 locku_args->length = flk->l_len; 13432 if (flk->l_len == 0) 13433 locku_args->length = ~locku_args->length; 13434 13435 *go_otwp = TRUE; 13436 } 13437 13438 /* 13439 * Setup the LOCK4 arguments. 13440 * 13441 * Returns errors via the nfs4_error_t. 13442 * NFS4_OK no problems 13443 * NFS4ERR_DELAY caller should retry (like recovery retry) 13444 * (other) unrecoverable error 13445 */ 13446 static void 13447 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13448 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13449 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13450 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13451 { 13452 LOCK4args *lock_args; 13453 nfs4_open_owner_t *oop = NULL; 13454 nfs4_open_stream_t *osp = NULL; 13455 nfs4_lock_owner_t *lop = NULL; 13456 pid_t pid; 13457 rnode4_t *rp = VTOR4(vp); 13458 13459 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13460 13461 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13462 if (ep->error || ep->stat != NFS4_OK) 13463 return; 13464 13465 argop->argop = OP_LOCK; 13466 if (ctype == NFS4_LCK_CTYPE_NORM) 13467 argsp->ctag = TAG_LOCK; 13468 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13469 argsp->ctag = TAG_RELOCK; 13470 else 13471 argsp->ctag = TAG_LOCK_REINSTATE; 13472 lock_args = &argop->nfs_argop4_u.oplock; 13473 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13474 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13475 /* 13476 * Get the lock owner. If no lock owner exists, 13477 * create a 'temporary' one and grab the open seqid 13478 * synchronization (which puts a hold on the open 13479 * owner and open stream). 13480 * This also grabs the lock seqid synchronization. 13481 */ 13482 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13483 ep->stat = 13484 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13485 13486 if (ep->stat != NFS4_OK) 13487 goto out; 13488 13489 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13490 &lock_args->locker); 13491 13492 lock_args->offset = flk->l_start; 13493 lock_args->length = flk->l_len; 13494 if (flk->l_len == 0) 13495 lock_args->length = ~lock_args->length; 13496 *lock_argsp = lock_args; 13497 out: 13498 *oopp = oop; 13499 *ospp = osp; 13500 *lopp = lop; 13501 } 13502 13503 /* 13504 * After we get the reply from the server, record the proper information 13505 * for possible resend lock requests. 13506 * 13507 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13508 */ 13509 static void 13510 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13511 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13512 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13513 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13514 { 13515 bool_t unlock = (flk->l_type == F_UNLCK); 13516 13517 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13518 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13519 ctype == NFS4_LCK_CTYPE_REINSTATE); 13520 13521 if (error != 0 && !unlock) { 13522 NFS4_DEBUG((nfs4_lost_rqst_debug || 13523 nfs4_client_lock_debug), (CE_NOTE, 13524 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13525 " for lop %p", (void *)lop)); 13526 ASSERT(lop != NULL); 13527 mutex_enter(&lop->lo_lock); 13528 lop->lo_pending_rqsts = 1; 13529 mutex_exit(&lop->lo_lock); 13530 } 13531 13532 lost_rqstp->lr_putfirst = FALSE; 13533 lost_rqstp->lr_op = 0; 13534 13535 /* 13536 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13537 * recovery purposes so that the lock request that was sent 13538 * can be saved and re-issued later. Ditto for EIO from a forced 13539 * unmount. This is done to have the client's local locking state 13540 * match the v4 server's state; that is, the request was 13541 * potentially received and accepted by the server but the client 13542 * thinks it was not. 13543 */ 13544 if (error == ETIMEDOUT || error == EINTR || 13545 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13546 NFS4_DEBUG((nfs4_lost_rqst_debug || 13547 nfs4_client_lock_debug), (CE_NOTE, 13548 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13549 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13550 (void *)lop, (void *)oop, (void *)osp)); 13551 if (unlock) 13552 lost_rqstp->lr_op = OP_LOCKU; 13553 else { 13554 lost_rqstp->lr_op = OP_LOCK; 13555 lost_rqstp->lr_locktype = locktype; 13556 } 13557 /* 13558 * Objects are held and rele'd via the recovery code. 13559 * See nfs4_save_lost_rqst. 13560 */ 13561 lost_rqstp->lr_vp = vp; 13562 lost_rqstp->lr_dvp = NULL; 13563 lost_rqstp->lr_oop = oop; 13564 lost_rqstp->lr_osp = osp; 13565 lost_rqstp->lr_lop = lop; 13566 lost_rqstp->lr_cr = cr; 13567 switch (ctype) { 13568 case NFS4_LCK_CTYPE_NORM: 13569 flk->l_pid = ttoproc(curthread)->p_pid; 13570 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13571 break; 13572 case NFS4_LCK_CTYPE_REINSTATE: 13573 lost_rqstp->lr_putfirst = TRUE; 13574 lost_rqstp->lr_ctype = ctype; 13575 break; 13576 default: 13577 break; 13578 } 13579 lost_rqstp->lr_flk = flk; 13580 } 13581 } 13582 13583 /* 13584 * Update lop's seqid. Also update the seqid stored in a resend request, 13585 * if any. (Some recovery errors increment the seqid, and we may have to 13586 * send the resend request again.) 13587 */ 13588 13589 static void 13590 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13591 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13592 { 13593 if (lock_args) { 13594 if (lock_args->locker.new_lock_owner == TRUE) 13595 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13596 else { 13597 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13598 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13599 } 13600 } else if (locku_args) { 13601 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13602 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13603 } 13604 } 13605 13606 /* 13607 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13608 * COMPOUND4 args/res for calls that need to retry. 13609 * Switches the *cred_otwp to base_cr. 13610 */ 13611 static void 13612 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13613 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13614 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13615 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13616 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13617 { 13618 nfs4_open_owner_t *oop = *oopp; 13619 nfs4_open_stream_t *osp = *ospp; 13620 nfs4_lock_owner_t *lop = *lopp; 13621 nfs_argop4 *argop = (*argspp)->array; 13622 13623 if (*did_start_fop) { 13624 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13625 needrecov); 13626 *did_start_fop = FALSE; 13627 } 13628 ASSERT((*argspp)->array_len == 2); 13629 if (argop[1].argop == OP_LOCK) 13630 nfs4args_lock_free(&argop[1]); 13631 else if (argop[1].argop == OP_LOCKT) 13632 nfs4args_lockt_free(&argop[1]); 13633 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13634 if (!error) 13635 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13636 *argspp = NULL; 13637 *respp = NULL; 13638 13639 if (lop) { 13640 nfs4_end_lock_seqid_sync(lop); 13641 lock_owner_rele(lop); 13642 *lopp = NULL; 13643 } 13644 13645 /* need to free up the reference on osp for lock args */ 13646 if (osp != NULL) { 13647 open_stream_rele(osp, VTOR4(vp)); 13648 *ospp = NULL; 13649 } 13650 13651 /* need to free up the reference on oop for lock args */ 13652 if (oop != NULL) { 13653 nfs4_end_open_seqid_sync(oop); 13654 open_owner_rele(oop); 13655 *oopp = NULL; 13656 } 13657 13658 crfree(*cred_otwp); 13659 *cred_otwp = base_cr; 13660 crhold(*cred_otwp); 13661 } 13662 13663 /* 13664 * Function to process the client's recovery for nfs4frlock. 13665 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13666 * 13667 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13668 * COMPOUND4 args/res for calls that need to retry. 13669 * 13670 * Note: the rp's r_lkserlock is *not* dropped during this path. 13671 */ 13672 static bool_t 13673 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13674 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13675 LOCK4args *lock_args, LOCKU4args *locku_args, 13676 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13677 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13678 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13679 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13680 { 13681 nfs4_open_owner_t *oop = *oopp; 13682 nfs4_open_stream_t *osp = *ospp; 13683 nfs4_lock_owner_t *lop = *lopp; 13684 13685 bool_t abort, retry; 13686 13687 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13688 ASSERT((*argspp) != NULL); 13689 ASSERT((*respp) != NULL); 13690 if (lock_args || locku_args) 13691 ASSERT(lop != NULL); 13692 13693 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13694 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13695 13696 retry = TRUE; 13697 abort = FALSE; 13698 if (needrecov) { 13699 nfs4_bseqid_entry_t *bsep = NULL; 13700 nfs_opnum4 op; 13701 13702 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13703 13704 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13705 seqid4 seqid; 13706 13707 if (lock_args) { 13708 if (lock_args->locker.new_lock_owner == TRUE) 13709 seqid = lock_args->locker.locker4_u. 13710 open_owner.open_seqid; 13711 else 13712 seqid = lock_args->locker.locker4_u. 13713 lock_owner.lock_seqid; 13714 } else if (locku_args) { 13715 seqid = locku_args->seqid; 13716 } else { 13717 seqid = 0; 13718 } 13719 13720 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13721 flk->l_pid, (*argspp)->ctag, seqid); 13722 } 13723 13724 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13725 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13726 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13727 NULL, op, bsep, NULL, NULL); 13728 13729 if (bsep) 13730 kmem_free(bsep, sizeof (*bsep)); 13731 } 13732 13733 /* 13734 * Return that we do not want to retry the request for 3 cases: 13735 * 1. If we received EINTR or are bailing out because of a forced 13736 * unmount, we came into this code path just for the sake of 13737 * initiating recovery, we now need to return the error. 13738 * 2. If we have aborted recovery. 13739 * 3. We received NFS4ERR_BAD_SEQID. 13740 */ 13741 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13742 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13743 retry = FALSE; 13744 13745 if (*did_start_fop == TRUE) { 13746 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13747 needrecov); 13748 *did_start_fop = FALSE; 13749 } 13750 13751 if (retry == TRUE) { 13752 nfs_argop4 *argop; 13753 13754 argop = (*argspp)->array; 13755 ASSERT((*argspp)->array_len == 2); 13756 13757 if (argop[1].argop == OP_LOCK) 13758 nfs4args_lock_free(&argop[1]); 13759 else if (argop[1].argop == OP_LOCKT) 13760 nfs4args_lockt_free(&argop[1]); 13761 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13762 if (!ep->error) 13763 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13764 *respp = NULL; 13765 *argspp = NULL; 13766 } 13767 13768 if (lop != NULL) { 13769 nfs4_end_lock_seqid_sync(lop); 13770 lock_owner_rele(lop); 13771 } 13772 13773 *lopp = NULL; 13774 13775 /* need to free up the reference on osp for lock args */ 13776 if (osp != NULL) { 13777 open_stream_rele(osp, rp); 13778 *ospp = NULL; 13779 } 13780 13781 /* need to free up the reference on oop for lock args */ 13782 if (oop != NULL) { 13783 nfs4_end_open_seqid_sync(oop); 13784 open_owner_rele(oop); 13785 *oopp = NULL; 13786 } 13787 13788 return (retry); 13789 } 13790 13791 /* 13792 * Handles the successful reply from the server for nfs4frlock. 13793 */ 13794 static void 13795 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13796 vnode_t *vp, int flag, u_offset_t offset, 13797 nfs4_lost_rqst_t *resend_rqstp) 13798 { 13799 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13800 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13801 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13802 if (ctype == NFS4_LCK_CTYPE_NORM) { 13803 flk->l_pid = ttoproc(curthread)->p_pid; 13804 /* 13805 * We do not register lost locks locally in 13806 * the 'resend' case since the user/application 13807 * doesn't think we have the lock. 13808 */ 13809 ASSERT(!resend_rqstp); 13810 nfs4_register_lock_locally(vp, flk, flag, offset); 13811 } 13812 } 13813 } 13814 13815 /* 13816 * Handle the DENIED reply from the server for nfs4frlock. 13817 * Returns TRUE if we should retry the request; FALSE otherwise. 13818 * 13819 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13820 * COMPOUND4 args/res for calls that need to retry. Can also 13821 * drop and regrab the r_lkserlock. 13822 */ 13823 static bool_t 13824 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13825 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13826 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13827 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13828 nfs4_recov_state_t *recov_statep, int needrecov, 13829 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13830 clock_t *tick_delayp, short *whencep, int *errorp, 13831 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13832 bool_t *skip_get_err) 13833 { 13834 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13835 13836 if (lock_args) { 13837 nfs4_open_owner_t *oop = *oopp; 13838 nfs4_open_stream_t *osp = *ospp; 13839 nfs4_lock_owner_t *lop = *lopp; 13840 int intr; 13841 13842 /* 13843 * Blocking lock needs to sleep and retry from the request. 13844 * 13845 * Do not block and wait for 'resend' or 'reinstate' 13846 * lock requests, just return the error. 13847 * 13848 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13849 */ 13850 if (cmd == F_SETLKW) { 13851 rnode4_t *rp = VTOR4(vp); 13852 nfs_argop4 *argop = (*argspp)->array; 13853 13854 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13855 13856 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13857 recov_statep, needrecov); 13858 *did_start_fop = FALSE; 13859 ASSERT((*argspp)->array_len == 2); 13860 if (argop[1].argop == OP_LOCK) 13861 nfs4args_lock_free(&argop[1]); 13862 else if (argop[1].argop == OP_LOCKT) 13863 nfs4args_lockt_free(&argop[1]); 13864 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13865 if (*respp) 13866 (void) xdr_free(xdr_COMPOUND4res_clnt, 13867 (caddr_t)*respp); 13868 *argspp = NULL; 13869 *respp = NULL; 13870 nfs4_end_lock_seqid_sync(lop); 13871 lock_owner_rele(lop); 13872 *lopp = NULL; 13873 if (osp != NULL) { 13874 open_stream_rele(osp, rp); 13875 *ospp = NULL; 13876 } 13877 if (oop != NULL) { 13878 nfs4_end_open_seqid_sync(oop); 13879 open_owner_rele(oop); 13880 *oopp = NULL; 13881 } 13882 13883 nfs_rw_exit(&rp->r_lkserlock); 13884 13885 intr = nfs4_block_and_wait(tick_delayp); 13886 13887 if (intr) { 13888 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13889 RW_WRITER, FALSE); 13890 *errorp = EINTR; 13891 return (FALSE); 13892 } 13893 13894 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13895 RW_WRITER, FALSE); 13896 13897 /* 13898 * Make sure we are still safe to lock with 13899 * regards to mmapping. 13900 */ 13901 if (!nfs4_safelock(vp, flk, cr)) { 13902 *errorp = EAGAIN; 13903 return (FALSE); 13904 } 13905 13906 return (TRUE); 13907 } 13908 if (ctype == NFS4_LCK_CTYPE_NORM) 13909 *errorp = EAGAIN; 13910 *skip_get_err = TRUE; 13911 flk->l_whence = 0; 13912 *whencep = 0; 13913 return (FALSE); 13914 } else if (lockt_args) { 13915 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13916 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13917 13918 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13919 flk, lockt_args); 13920 13921 /* according to NLM code */ 13922 *errorp = 0; 13923 *whencep = 0; 13924 *skip_get_err = TRUE; 13925 return (FALSE); 13926 } 13927 return (FALSE); 13928 } 13929 13930 /* 13931 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13932 */ 13933 static void 13934 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13935 { 13936 switch (resp->status) { 13937 case NFS4ERR_ACCESS: 13938 case NFS4ERR_ADMIN_REVOKED: 13939 case NFS4ERR_BADHANDLE: 13940 case NFS4ERR_BAD_RANGE: 13941 case NFS4ERR_BAD_SEQID: 13942 case NFS4ERR_BAD_STATEID: 13943 case NFS4ERR_BADXDR: 13944 case NFS4ERR_DEADLOCK: 13945 case NFS4ERR_DELAY: 13946 case NFS4ERR_EXPIRED: 13947 case NFS4ERR_FHEXPIRED: 13948 case NFS4ERR_GRACE: 13949 case NFS4ERR_INVAL: 13950 case NFS4ERR_ISDIR: 13951 case NFS4ERR_LEASE_MOVED: 13952 case NFS4ERR_LOCK_NOTSUPP: 13953 case NFS4ERR_LOCK_RANGE: 13954 case NFS4ERR_MOVED: 13955 case NFS4ERR_NOFILEHANDLE: 13956 case NFS4ERR_NO_GRACE: 13957 case NFS4ERR_OLD_STATEID: 13958 case NFS4ERR_OPENMODE: 13959 case NFS4ERR_RECLAIM_BAD: 13960 case NFS4ERR_RECLAIM_CONFLICT: 13961 case NFS4ERR_RESOURCE: 13962 case NFS4ERR_SERVERFAULT: 13963 case NFS4ERR_STALE: 13964 case NFS4ERR_STALE_CLIENTID: 13965 case NFS4ERR_STALE_STATEID: 13966 return; 13967 default: 13968 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13969 "nfs4frlock_results_default: got unrecognizable " 13970 "res.status %d", resp->status)); 13971 *errorp = NFS4ERR_INVAL; 13972 } 13973 } 13974 13975 /* 13976 * The lock request was successful, so update the client's state. 13977 */ 13978 static void 13979 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13980 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13981 vnode_t *vp, flock64_t *flk, cred_t *cr, 13982 nfs4_lost_rqst_t *resend_rqstp) 13983 { 13984 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13985 13986 if (lock_args) { 13987 LOCK4res *lock_res; 13988 13989 lock_res = &resop->nfs_resop4_u.oplock; 13990 /* update the stateid with server's response */ 13991 13992 if (lock_args->locker.new_lock_owner == TRUE) { 13993 mutex_enter(&lop->lo_lock); 13994 lop->lo_just_created = NFS4_PERM_CREATED; 13995 mutex_exit(&lop->lo_lock); 13996 } 13997 13998 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13999 14000 /* 14001 * If the lock was the result of a resending a lost 14002 * request, we've synched up the stateid and seqid 14003 * with the server, but now the server might be out of sync 14004 * with what the application thinks it has for locks. 14005 * Clean that up here. It's unclear whether we should do 14006 * this even if the filesystem has been forcibly unmounted. 14007 * For most servers, it's probably wasted effort, but 14008 * RFC3530 lets servers require that unlocks exactly match 14009 * the locks that are held. 14010 */ 14011 if (resend_rqstp != NULL && 14012 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 14013 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 14014 } else { 14015 flk->l_whence = 0; 14016 } 14017 } else if (locku_args) { 14018 LOCKU4res *locku_res; 14019 14020 locku_res = &resop->nfs_resop4_u.oplocku; 14021 14022 /* Update the stateid with the server's response */ 14023 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 14024 } else if (lockt_args) { 14025 /* Switch the lock type to express success, see fcntl */ 14026 flk->l_type = F_UNLCK; 14027 flk->l_whence = 0; 14028 } 14029 } 14030 14031 /* 14032 * Do final cleanup before exiting nfs4frlock. 14033 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 14034 * COMPOUND4 args/res for calls that haven't already. 14035 */ 14036 static void 14037 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 14038 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 14039 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 14040 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 14041 short whence, u_offset_t offset, struct lm_sysid *ls, 14042 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 14043 bool_t did_start_fop, bool_t skip_get_err, 14044 cred_t *cred_otw, cred_t *cred) 14045 { 14046 mntinfo4_t *mi = VTOMI4(vp); 14047 rnode4_t *rp = VTOR4(vp); 14048 int error = *errorp; 14049 nfs_argop4 *argop; 14050 int do_flush_pages = 0; 14051 14052 ASSERT(nfs_zone() == mi->mi_zone); 14053 /* 14054 * The client recovery code wants the raw status information, 14055 * so don't map the NFS status code to an errno value for 14056 * non-normal call types. 14057 */ 14058 if (ctype == NFS4_LCK_CTYPE_NORM) { 14059 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 14060 *errorp = geterrno4(resp->status); 14061 if (did_start_fop == TRUE) 14062 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 14063 needrecov); 14064 14065 /* 14066 * We've established a new lock on the server, so invalidate 14067 * the pages associated with the vnode to get the most up to 14068 * date pages from the server after acquiring the lock. We 14069 * want to be sure that the read operation gets the newest data. 14070 * N.B. 14071 * We used to do this in nfs4frlock_results_ok but that doesn't 14072 * work since VOP_PUTPAGE can call nfs4_commit which calls 14073 * nfs4_start_fop. We flush the pages below after calling 14074 * nfs4_end_fop above 14075 * The flush of the page cache must be done after 14076 * nfs4_end_open_seqid_sync() to avoid a 4-way hang. 14077 */ 14078 if (!error && resp && resp->status == NFS4_OK) 14079 do_flush_pages = 1; 14080 } 14081 if (argsp) { 14082 ASSERT(argsp->array_len == 2); 14083 argop = argsp->array; 14084 if (argop[1].argop == OP_LOCK) 14085 nfs4args_lock_free(&argop[1]); 14086 else if (argop[1].argop == OP_LOCKT) 14087 nfs4args_lockt_free(&argop[1]); 14088 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14089 if (resp) 14090 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 14091 } 14092 14093 /* free the reference on the lock owner */ 14094 if (lop != NULL) { 14095 nfs4_end_lock_seqid_sync(lop); 14096 lock_owner_rele(lop); 14097 } 14098 14099 /* need to free up the reference on osp for lock args */ 14100 if (osp != NULL) 14101 open_stream_rele(osp, rp); 14102 14103 /* need to free up the reference on oop for lock args */ 14104 if (oop != NULL) { 14105 nfs4_end_open_seqid_sync(oop); 14106 open_owner_rele(oop); 14107 } 14108 14109 if (do_flush_pages) 14110 nfs4_flush_pages(vp, cred); 14111 14112 (void) convoff(vp, flk, whence, offset); 14113 14114 lm_rel_sysid(ls); 14115 14116 /* 14117 * Record debug information in the event we get EINVAL. 14118 */ 14119 mutex_enter(&mi->mi_lock); 14120 if (*errorp == EINVAL && (lock_args || locku_args) && 14121 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 14122 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 14123 zcmn_err(getzoneid(), CE_NOTE, 14124 "%s operation failed with " 14125 "EINVAL probably since the server, %s," 14126 " doesn't support POSIX style locking", 14127 lock_args ? "LOCK" : "LOCKU", 14128 mi->mi_curr_serv->sv_hostname); 14129 mi->mi_flags |= MI4_LOCK_DEBUG; 14130 } 14131 } 14132 mutex_exit(&mi->mi_lock); 14133 14134 if (cred_otw) 14135 crfree(cred_otw); 14136 } 14137 14138 /* 14139 * This calls the server and the local locking code. 14140 * 14141 * Client locks are registerred locally by oring the sysid with 14142 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 14143 * We need to distinguish between the two to avoid collision in case one 14144 * machine is used as both client and server. 14145 * 14146 * Blocking lock requests will continually retry to acquire the lock 14147 * forever. 14148 * 14149 * The ctype is defined as follows: 14150 * NFS4_LCK_CTYPE_NORM: normal lock request. 14151 * 14152 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 14153 * recovery, get the pid from flk instead of curproc, and don't reregister 14154 * the lock locally. 14155 * 14156 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 14157 * that we will use the information passed in via resend_rqstp to setup the 14158 * lock/locku request. This resend is the exact same request as the 'lost 14159 * lock', and is initiated by the recovery framework. A successful resend 14160 * request can initiate one or more reinstate requests. 14161 * 14162 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 14163 * does not trigger additional reinstate requests. This lock call type is 14164 * set for setting the v4 server's locking state back to match what the 14165 * client's local locking state is in the event of a received 'lost lock'. 14166 * 14167 * Errors are returned via the nfs4_error_t parameter. 14168 */ 14169 void 14170 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 14171 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 14172 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 14173 { 14174 COMPOUND4args_clnt args, *argsp = NULL; 14175 COMPOUND4res_clnt res, *resp = NULL; 14176 nfs_argop4 *argop; 14177 nfs_resop4 *resop; 14178 rnode4_t *rp; 14179 int doqueue = 1; 14180 clock_t tick_delay; /* delay in clock ticks */ 14181 struct lm_sysid *ls; 14182 LOCK4args *lock_args = NULL; 14183 LOCKU4args *locku_args = NULL; 14184 LOCKT4args *lockt_args = NULL; 14185 nfs4_open_owner_t *oop = NULL; 14186 nfs4_open_stream_t *osp = NULL; 14187 nfs4_lock_owner_t *lop = NULL; 14188 bool_t needrecov = FALSE; 14189 nfs4_recov_state_t recov_state; 14190 short whence; 14191 nfs4_op_hint_t op_hint; 14192 nfs4_lost_rqst_t lost_rqst; 14193 bool_t retry = FALSE; 14194 bool_t did_start_fop = FALSE; 14195 bool_t skip_get_err = FALSE; 14196 cred_t *cred_otw = NULL; 14197 bool_t recovonly; /* just queue request */ 14198 int frc_no_reclaim = 0; 14199 #ifdef DEBUG 14200 char *name; 14201 #endif 14202 14203 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14204 14205 #ifdef DEBUG 14206 name = fn_name(VTOSV(vp)->sv_name); 14207 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 14208 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 14209 "length %"PRIu64", pid %d, sysid %d, call type %s, " 14210 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 14211 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 14212 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 14213 resend_rqstp ? "TRUE" : "FALSE")); 14214 kmem_free(name, MAXNAMELEN); 14215 #endif 14216 14217 nfs4_error_zinit(ep); 14218 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14219 if (ep->error) 14220 return; 14221 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14222 if (ep->error) 14223 return; 14224 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14225 vp, cr, &cred_otw); 14226 14227 recov_retry: 14228 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14229 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14230 rp = VTOR4(vp); 14231 14232 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14233 &did_start_fop, &recovonly); 14234 14235 if (ep->error) 14236 goto out; 14237 14238 if (recovonly) { 14239 /* 14240 * Leave the request for the recovery system to deal with. 14241 */ 14242 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14243 ASSERT(cmd != F_GETLK); 14244 ASSERT(flk->l_type == F_UNLCK); 14245 14246 nfs4_error_init(ep, EINTR); 14247 needrecov = TRUE; 14248 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14249 if (lop != NULL) { 14250 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14251 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14252 (void) nfs4_start_recovery(ep, 14253 VTOMI4(vp), vp, NULL, NULL, 14254 (lost_rqst.lr_op == OP_LOCK || 14255 lost_rqst.lr_op == OP_LOCKU) ? 14256 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL); 14257 lock_owner_rele(lop); 14258 lop = NULL; 14259 } 14260 flk->l_pid = curproc->p_pid; 14261 nfs4_register_lock_locally(vp, flk, flag, offset); 14262 goto out; 14263 } 14264 14265 /* putfh directory fh */ 14266 argop[0].argop = OP_CPUTFH; 14267 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14268 14269 /* 14270 * Set up the over-the-wire arguments and get references to the 14271 * open owner, etc. 14272 */ 14273 14274 if (ctype == NFS4_LCK_CTYPE_RESEND || 14275 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14276 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14277 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14278 } else { 14279 bool_t go_otw = TRUE; 14280 14281 ASSERT(resend_rqstp == NULL); 14282 14283 switch (cmd) { 14284 case F_GETLK: 14285 case F_O_GETLK: 14286 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14287 &lockt_args, argsp, flk, rp); 14288 break; 14289 case F_SETLKW: 14290 case F_SETLK: 14291 if (flk->l_type == F_UNLCK) 14292 nfs4frlock_setup_locku_args(ctype, 14293 &argop[1], &locku_args, flk, 14294 &lop, ep, argsp, 14295 vp, flag, offset, cr, 14296 &skip_get_err, &go_otw); 14297 else 14298 nfs4frlock_setup_lock_args(ctype, 14299 &lock_args, &oop, &osp, &lop, &argop[1], 14300 argsp, flk, cmd, vp, cr, ep); 14301 14302 if (ep->error) 14303 goto out; 14304 14305 switch (ep->stat) { 14306 case NFS4_OK: 14307 break; 14308 case NFS4ERR_DELAY: 14309 /* recov thread never gets this error */ 14310 ASSERT(resend_rqstp == NULL); 14311 ASSERT(did_start_fop); 14312 14313 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14314 &recov_state, TRUE); 14315 did_start_fop = FALSE; 14316 if (argop[1].argop == OP_LOCK) 14317 nfs4args_lock_free(&argop[1]); 14318 else if (argop[1].argop == OP_LOCKT) 14319 nfs4args_lockt_free(&argop[1]); 14320 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14321 argsp = NULL; 14322 goto recov_retry; 14323 default: 14324 ep->error = EIO; 14325 goto out; 14326 } 14327 break; 14328 default: 14329 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14330 "nfs4_frlock: invalid cmd %d", cmd)); 14331 ep->error = EINVAL; 14332 goto out; 14333 } 14334 14335 if (!go_otw) 14336 goto out; 14337 } 14338 14339 /* XXX should we use the local reclock as a cache ? */ 14340 /* 14341 * Unregister the lock with the local locking code before 14342 * contacting the server. This avoids a potential race where 14343 * another process gets notified that it has been granted a lock 14344 * before we can unregister ourselves locally. 14345 */ 14346 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14347 if (ctype == NFS4_LCK_CTYPE_NORM) 14348 flk->l_pid = ttoproc(curthread)->p_pid; 14349 nfs4_register_lock_locally(vp, flk, flag, offset); 14350 } 14351 14352 /* 14353 * Send the server the lock request. Continually loop with a delay 14354 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14355 */ 14356 resp = &res; 14357 14358 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14359 (CE_NOTE, 14360 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14361 rnode4info(rp))); 14362 14363 if (lock_args && frc_no_reclaim) { 14364 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14365 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14366 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14367 lock_args->reclaim = FALSE; 14368 if (did_reclaimp) 14369 *did_reclaimp = 0; 14370 } 14371 14372 /* 14373 * Do the OTW call. 14374 */ 14375 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14376 14377 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14378 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14379 14380 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14381 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14382 "nfs4frlock: needrecov %d", needrecov)); 14383 14384 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14385 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14386 args.ctag); 14387 14388 /* 14389 * Check if one of these mutually exclusive error cases has 14390 * happened: 14391 * need to swap credentials due to access error 14392 * recovery is needed 14393 * different error (only known case is missing Kerberos ticket) 14394 */ 14395 14396 if ((ep->error == EACCES || 14397 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14398 cred_otw != cr) { 14399 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14400 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14401 cr, &cred_otw); 14402 goto recov_retry; 14403 } 14404 14405 if (needrecov) { 14406 /* 14407 * LOCKT requests don't need to recover from lost 14408 * requests since they don't create/modify state. 14409 */ 14410 if ((ep->error == EINTR || 14411 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14412 lockt_args) 14413 goto out; 14414 /* 14415 * Do not attempt recovery for requests initiated by 14416 * the recovery framework. Let the framework redrive them. 14417 */ 14418 if (ctype != NFS4_LCK_CTYPE_NORM) 14419 goto out; 14420 else { 14421 ASSERT(resend_rqstp == NULL); 14422 } 14423 14424 nfs4frlock_save_lost_rqst(ctype, ep->error, 14425 flk_to_locktype(cmd, flk->l_type), 14426 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14427 14428 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14429 &resp, lock_args, locku_args, &oop, &osp, &lop, 14430 rp, vp, &recov_state, op_hint, &did_start_fop, 14431 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14432 14433 if (retry) { 14434 ASSERT(oop == NULL); 14435 ASSERT(osp == NULL); 14436 ASSERT(lop == NULL); 14437 goto recov_retry; 14438 } 14439 goto out; 14440 } 14441 14442 /* 14443 * Bail out if have reached this point with ep->error set. Can 14444 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14445 * This happens if Kerberos ticket has expired or has been 14446 * destroyed. 14447 */ 14448 if (ep->error != 0) 14449 goto out; 14450 14451 /* 14452 * Process the reply. 14453 */ 14454 switch (resp->status) { 14455 case NFS4_OK: 14456 resop = &resp->array[1]; 14457 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14458 resend_rqstp); 14459 /* 14460 * Have a successful lock operation, now update state. 14461 */ 14462 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14463 resop, lop, vp, flk, cr, resend_rqstp); 14464 break; 14465 14466 case NFS4ERR_DENIED: 14467 resop = &resp->array[1]; 14468 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14469 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14470 &recov_state, needrecov, &argsp, &resp, 14471 &tick_delay, &whence, &ep->error, resop, cr, 14472 &did_start_fop, &skip_get_err); 14473 14474 if (retry) { 14475 ASSERT(oop == NULL); 14476 ASSERT(osp == NULL); 14477 ASSERT(lop == NULL); 14478 goto recov_retry; 14479 } 14480 break; 14481 /* 14482 * If the server won't let us reclaim, fall-back to trying to lock 14483 * the file from scratch. Code elsewhere will check the changeinfo 14484 * to ensure the file hasn't been changed. 14485 */ 14486 case NFS4ERR_NO_GRACE: 14487 if (lock_args && lock_args->reclaim == TRUE) { 14488 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14489 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14490 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14491 frc_no_reclaim = 1; 14492 /* clean up before retrying */ 14493 needrecov = 0; 14494 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14495 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14496 &recov_state, op_hint, &did_start_fop, NULL, flk); 14497 goto recov_retry; 14498 } 14499 /* FALLTHROUGH */ 14500 14501 default: 14502 nfs4frlock_results_default(resp, &ep->error); 14503 break; 14504 } 14505 out: 14506 /* 14507 * Process and cleanup from error. Make interrupted unlock 14508 * requests look successful, since they will be handled by the 14509 * client recovery code. 14510 */ 14511 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14512 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14513 lock_args, locku_args, did_start_fop, 14514 skip_get_err, cred_otw, cr); 14515 14516 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14517 (cmd == F_SETLK || cmd == F_SETLKW)) 14518 ep->error = 0; 14519 } 14520 14521 /* 14522 * nfs4_safelock: 14523 * 14524 * Return non-zero if the given lock request can be handled without 14525 * violating the constraints on concurrent mapping and locking. 14526 */ 14527 14528 static int 14529 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14530 { 14531 rnode4_t *rp = VTOR4(vp); 14532 struct vattr va; 14533 int error; 14534 14535 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14536 ASSERT(rp->r_mapcnt >= 0); 14537 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14538 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14539 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14540 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14541 14542 if (rp->r_mapcnt == 0) 14543 return (1); /* always safe if not mapped */ 14544 14545 /* 14546 * If the file is already mapped and there are locks, then they 14547 * should be all safe locks. So adding or removing a lock is safe 14548 * as long as the new request is safe (i.e., whole-file, meaning 14549 * length and starting offset are both zero). 14550 */ 14551 14552 if (bfp->l_start != 0 || bfp->l_len != 0) { 14553 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14554 "cannot lock a memory mapped file unless locking the " 14555 "entire file: start %"PRIx64", len %"PRIx64, 14556 bfp->l_start, bfp->l_len)); 14557 return (0); 14558 } 14559 14560 /* mandatory locking and mapping don't mix */ 14561 va.va_mask = AT_MODE; 14562 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 14563 if (error != 0) { 14564 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14565 "getattr error %d", error)); 14566 return (0); /* treat errors conservatively */ 14567 } 14568 if (MANDLOCK(vp, va.va_mode)) { 14569 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14570 "cannot mandatory lock and mmap a file")); 14571 return (0); 14572 } 14573 14574 return (1); 14575 } 14576 14577 14578 /* 14579 * Register the lock locally within Solaris. 14580 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14581 * recording locks locally. 14582 * 14583 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14584 * are registered locally. 14585 */ 14586 void 14587 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14588 u_offset_t offset) 14589 { 14590 int oldsysid; 14591 int error; 14592 #ifdef DEBUG 14593 char *name; 14594 #endif 14595 14596 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14597 14598 #ifdef DEBUG 14599 name = fn_name(VTOSV(vp)->sv_name); 14600 NFS4_DEBUG(nfs4_client_lock_debug, 14601 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14602 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14603 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14604 flk->l_sysid)); 14605 kmem_free(name, MAXNAMELEN); 14606 #endif 14607 14608 /* register the lock with local locking */ 14609 oldsysid = flk->l_sysid; 14610 flk->l_sysid |= LM_SYSID_CLIENT; 14611 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14612 #ifdef DEBUG 14613 if (error != 0) { 14614 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14615 "nfs4_register_lock_locally: could not register with" 14616 " local locking")); 14617 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14618 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14619 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14620 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14621 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14622 flk->l_type, flk->l_start, flk->l_len)); 14623 (void) reclock(vp, flk, 0, flag, offset, NULL); 14624 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14625 "blocked by pid %d sysid 0x%x type %d " 14626 "off 0x%" PRIx64 " len 0x%" PRIx64, 14627 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14628 flk->l_len)); 14629 } 14630 #endif 14631 flk->l_sysid = oldsysid; 14632 } 14633 14634 /* 14635 * nfs4_lockrelease: 14636 * 14637 * Release any locks on the given vnode that are held by the current 14638 * process. Also removes the lock owner (if one exists) from the rnode's 14639 * list. 14640 */ 14641 static int 14642 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14643 { 14644 flock64_t ld; 14645 int ret, error; 14646 rnode4_t *rp; 14647 nfs4_lock_owner_t *lop; 14648 nfs4_recov_state_t recov_state; 14649 mntinfo4_t *mi; 14650 bool_t possible_orphan = FALSE; 14651 bool_t recovonly; 14652 14653 ASSERT((uintptr_t)vp > KERNELBASE); 14654 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14655 14656 rp = VTOR4(vp); 14657 mi = VTOMI4(vp); 14658 14659 /* 14660 * If we have not locked anything then we can 14661 * just return since we have no work to do. 14662 */ 14663 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14664 return (0); 14665 } 14666 14667 /* 14668 * We need to comprehend that another thread may 14669 * kick off recovery and the lock_owner we have stashed 14670 * in lop might be invalid so we should NOT cache it 14671 * locally! 14672 */ 14673 recov_state.rs_flags = 0; 14674 recov_state.rs_num_retry_despite_err = 0; 14675 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14676 &recovonly); 14677 if (error) { 14678 mutex_enter(&rp->r_statelock); 14679 rp->r_flags |= R4LODANGLERS; 14680 mutex_exit(&rp->r_statelock); 14681 return (error); 14682 } 14683 14684 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14685 14686 /* 14687 * Check if the lock owner might have a lock (request was sent but 14688 * no response was received). Also check if there are any remote 14689 * locks on the file. (In theory we shouldn't have to make this 14690 * second check if there's no lock owner, but for now we'll be 14691 * conservative and do it anyway.) If either condition is true, 14692 * send an unlock for the entire file to the server. 14693 * 14694 * Note that no explicit synchronization is needed here. At worst, 14695 * flk_has_remote_locks() will return a false positive, in which case 14696 * the unlock call wastes time but doesn't harm correctness. 14697 */ 14698 14699 if (lop) { 14700 mutex_enter(&lop->lo_lock); 14701 possible_orphan = lop->lo_pending_rqsts; 14702 mutex_exit(&lop->lo_lock); 14703 lock_owner_rele(lop); 14704 } 14705 14706 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14707 14708 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14709 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14710 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14711 (void *)lop)); 14712 14713 if (possible_orphan || flk_has_remote_locks(vp)) { 14714 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14715 ld.l_whence = 0; /* unlock from start of file */ 14716 ld.l_start = 0; 14717 ld.l_len = 0; /* do entire file */ 14718 14719 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, 14720 cr, NULL); 14721 14722 if (ret != 0) { 14723 /* 14724 * If VOP_FRLOCK fails, make sure we unregister 14725 * local locks before we continue. 14726 */ 14727 ld.l_pid = ttoproc(curthread)->p_pid; 14728 nfs4_register_lock_locally(vp, &ld, flag, offset); 14729 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14730 "nfs4_lockrelease: lock release error on vp" 14731 " %p: error %d.\n", (void *)vp, ret)); 14732 } 14733 } 14734 14735 recov_state.rs_flags = 0; 14736 recov_state.rs_num_retry_despite_err = 0; 14737 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14738 &recovonly); 14739 if (error) { 14740 mutex_enter(&rp->r_statelock); 14741 rp->r_flags |= R4LODANGLERS; 14742 mutex_exit(&rp->r_statelock); 14743 return (error); 14744 } 14745 14746 /* 14747 * So, here we're going to need to retrieve the lock-owner 14748 * again (in case recovery has done a switch-a-roo) and 14749 * remove it because we can. 14750 */ 14751 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14752 14753 if (lop) { 14754 nfs4_rnode_remove_lock_owner(rp, lop); 14755 lock_owner_rele(lop); 14756 } 14757 14758 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14759 return (0); 14760 } 14761 14762 /* 14763 * Wait for 'tick_delay' clock ticks. 14764 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14765 * 14766 * The client should retry to acquire the lock faster than the lease period. 14767 * We use roughly half of the lease time to use a similar calculation as it is 14768 * used in nfs4_renew_lease_thread(). 14769 * 14770 * XXX For future improvements, should implement a waiting queue scheme. 14771 */ 14772 static int 14773 nfs4_block_and_wait(clock_t *tick_delay) 14774 { 14775 /* wait tick_delay clock ticks or siginteruptus */ 14776 if (delay_sig(*tick_delay)) { 14777 return (EINTR); 14778 } 14779 14780 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14781 "reissue the lock request: blocked for %ld clock ticks: %ld " 14782 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14783 14784 *tick_delay = MIN(drv_usectohz(nfs4_max_base_wait_time * 1000), 14785 *tick_delay * 1.5); 14786 return (0); 14787 } 14788 14789 void 14790 nfs4_vnops_init(void) 14791 { 14792 } 14793 14794 void 14795 nfs4_vnops_fini(void) 14796 { 14797 } 14798 14799 /* 14800 * Return a reference to the directory (parent) vnode for a given vnode, 14801 * using the saved pathname information and the directory file handle. The 14802 * caller is responsible for disposing of the reference. 14803 * Returns zero or an errno value. 14804 * 14805 * Caller should set need_start_op to FALSE if it is the recovery 14806 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14807 */ 14808 int 14809 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14810 { 14811 svnode_t *svnp; 14812 vnode_t *dvp = NULL; 14813 servinfo4_t *svp; 14814 nfs4_fname_t *mfname; 14815 int error; 14816 14817 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14818 14819 if (vp->v_flag & VROOT) { 14820 nfs4_sharedfh_t *sfh; 14821 nfs_fh4 fh; 14822 mntinfo4_t *mi; 14823 14824 ASSERT(vp->v_type == VREG); 14825 14826 mi = VTOMI4(vp); 14827 svp = mi->mi_curr_serv; 14828 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14829 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14830 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14831 sfh = sfh4_get(&fh, VTOMI4(vp)); 14832 nfs_rw_exit(&svp->sv_lock); 14833 mfname = mi->mi_fname; 14834 fn_hold(mfname); 14835 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14836 sfh4_rele(&sfh); 14837 14838 if (dvp->v_type == VNON) 14839 dvp->v_type = VDIR; 14840 *dvpp = dvp; 14841 return (0); 14842 } 14843 14844 svnp = VTOSV(vp); 14845 14846 if (svnp == NULL) { 14847 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14848 "shadow node is NULL")); 14849 return (EINVAL); 14850 } 14851 14852 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14853 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14854 "shadow node name or dfh val == NULL")); 14855 return (EINVAL); 14856 } 14857 14858 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14859 (int)need_start_op); 14860 if (error != 0) { 14861 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14862 "nfs4_make_dotdot returned %d", error)); 14863 return (error); 14864 } 14865 if (!dvp) { 14866 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14867 "nfs4_make_dotdot returned a NULL dvp")); 14868 return (EIO); 14869 } 14870 if (dvp->v_type == VNON) 14871 dvp->v_type = VDIR; 14872 ASSERT(dvp->v_type == VDIR); 14873 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14874 mutex_enter(&dvp->v_lock); 14875 dvp->v_flag |= V_XATTRDIR; 14876 mutex_exit(&dvp->v_lock); 14877 } 14878 *dvpp = dvp; 14879 return (0); 14880 } 14881 14882 /* 14883 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14884 * length that fnamep can accept, including the trailing null. 14885 * Returns 0 if okay, returns an errno value if there was a problem. 14886 */ 14887 14888 int 14889 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14890 { 14891 char *fn; 14892 int err = 0; 14893 servinfo4_t *svp; 14894 svnode_t *shvp; 14895 14896 /* 14897 * If the file being opened has VROOT set, then this is 14898 * a "file" mount. sv_name will not be interesting, so 14899 * go back to the servinfo4 to get the original mount 14900 * path and strip off all but the final edge. Otherwise 14901 * just return the name from the shadow vnode. 14902 */ 14903 14904 if (vp->v_flag & VROOT) { 14905 14906 svp = VTOMI4(vp)->mi_curr_serv; 14907 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14908 14909 fn = strrchr(svp->sv_path, '/'); 14910 if (fn == NULL) 14911 err = EINVAL; 14912 else 14913 fn++; 14914 } else { 14915 shvp = VTOSV(vp); 14916 fn = fn_name(shvp->sv_name); 14917 } 14918 14919 if (err == 0) 14920 if (strlen(fn) < maxlen) 14921 (void) strcpy(fnamep, fn); 14922 else 14923 err = ENAMETOOLONG; 14924 14925 if (vp->v_flag & VROOT) 14926 nfs_rw_exit(&svp->sv_lock); 14927 else 14928 kmem_free(fn, MAXNAMELEN); 14929 14930 return (err); 14931 } 14932 14933 /* 14934 * Bookkeeping for a close that doesn't need to go over the wire. 14935 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14936 * it is left at 1. 14937 */ 14938 void 14939 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14940 { 14941 rnode4_t *rp; 14942 mntinfo4_t *mi; 14943 14944 mi = VTOMI4(vp); 14945 rp = VTOR4(vp); 14946 14947 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14948 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14949 ASSERT(nfs_zone() == mi->mi_zone); 14950 ASSERT(mutex_owned(&osp->os_sync_lock)); 14951 ASSERT(*have_lockp); 14952 14953 if (!osp->os_valid || 14954 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14955 return; 14956 } 14957 14958 /* 14959 * This removes the reference obtained at OPEN; ie, 14960 * when the open stream structure was created. 14961 * 14962 * We don't have to worry about calling 'open_stream_rele' 14963 * since we our currently holding a reference to this 14964 * open stream which means the count can not go to 0 with 14965 * this decrement. 14966 */ 14967 ASSERT(osp->os_ref_count >= 2); 14968 osp->os_ref_count--; 14969 osp->os_valid = 0; 14970 mutex_exit(&osp->os_sync_lock); 14971 *have_lockp = 0; 14972 14973 nfs4_dec_state_ref_count(mi); 14974 } 14975 14976 /* 14977 * Close all remaining open streams on the rnode. These open streams 14978 * could be here because: 14979 * - The close attempted at either close or delmap failed 14980 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14981 * - Someone did mknod on a regular file but never opened it 14982 */ 14983 int 14984 nfs4close_all(vnode_t *vp, cred_t *cr) 14985 { 14986 nfs4_open_stream_t *osp; 14987 int error; 14988 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14989 rnode4_t *rp; 14990 14991 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14992 14993 error = 0; 14994 rp = VTOR4(vp); 14995 14996 /* 14997 * At this point, all we know is that the last time 14998 * someone called vn_rele, the count was 1. Since then, 14999 * the vnode could have been re-activated. We want to 15000 * loop through the open streams and close each one, but 15001 * we have to be careful since once we release the rnode 15002 * hash bucket lock, someone else is free to come in and 15003 * re-activate the rnode and add new open streams. The 15004 * strategy is take the rnode hash bucket lock, verify that 15005 * the count is still 1, grab the open stream off the 15006 * head of the list and mark it invalid, then release the 15007 * rnode hash bucket lock and proceed with that open stream. 15008 * This is ok because nfs4close_one() will acquire the proper 15009 * open/create to close/destroy synchronization for open 15010 * streams, and will ensure that if someone has reopened 15011 * the open stream after we've dropped the hash bucket lock 15012 * then we'll just simply return without destroying the 15013 * open stream. 15014 * Repeat until the list is empty. 15015 */ 15016 15017 for (;;) { 15018 15019 /* make sure vnode hasn't been reactivated */ 15020 rw_enter(&rp->r_hashq->r_lock, RW_READER); 15021 mutex_enter(&vp->v_lock); 15022 if (vp->v_count > 1) { 15023 mutex_exit(&vp->v_lock); 15024 rw_exit(&rp->r_hashq->r_lock); 15025 break; 15026 } 15027 /* 15028 * Grabbing r_os_lock before releasing v_lock prevents 15029 * a window where the rnode/open stream could get 15030 * reactivated (and os_force_close set to 0) before we 15031 * had a chance to set os_force_close to 1. 15032 */ 15033 mutex_enter(&rp->r_os_lock); 15034 mutex_exit(&vp->v_lock); 15035 15036 osp = list_head(&rp->r_open_streams); 15037 if (!osp) { 15038 /* nothing left to CLOSE OTW, so return */ 15039 mutex_exit(&rp->r_os_lock); 15040 rw_exit(&rp->r_hashq->r_lock); 15041 break; 15042 } 15043 15044 mutex_enter(&rp->r_statev4_lock); 15045 /* the file can't still be mem mapped */ 15046 ASSERT(rp->r_mapcnt == 0); 15047 if (rp->created_v4) 15048 rp->created_v4 = 0; 15049 mutex_exit(&rp->r_statev4_lock); 15050 15051 /* 15052 * Grab a ref on this open stream; nfs4close_one 15053 * will mark it as invalid 15054 */ 15055 mutex_enter(&osp->os_sync_lock); 15056 osp->os_ref_count++; 15057 osp->os_force_close = 1; 15058 mutex_exit(&osp->os_sync_lock); 15059 mutex_exit(&rp->r_os_lock); 15060 rw_exit(&rp->r_hashq->r_lock); 15061 15062 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 15063 15064 /* Update error if it isn't already non-zero */ 15065 if (error == 0) { 15066 if (e.error) 15067 error = e.error; 15068 else if (e.stat) 15069 error = geterrno4(e.stat); 15070 } 15071 15072 #ifdef DEBUG 15073 nfs4close_all_cnt++; 15074 #endif 15075 /* Release the ref on osp acquired above. */ 15076 open_stream_rele(osp, rp); 15077 15078 /* Proceed to the next open stream, if any */ 15079 } 15080 return (error); 15081 } 15082 15083 /* 15084 * nfs4close_one - close one open stream for a file if needed. 15085 * 15086 * "close_type" indicates which close path this is: 15087 * CLOSE_NORM: close initiated via VOP_CLOSE. 15088 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 15089 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 15090 * the close and release of client state for this open stream 15091 * (unless someone else has the open stream open). 15092 * CLOSE_RESEND: indicates the request is a replay of an earlier request 15093 * (e.g., due to abort because of a signal). 15094 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 15095 * 15096 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 15097 * recovery. Instead, the caller is expected to deal with retries. 15098 * 15099 * The caller can either pass in the osp ('provided_osp') or not. 15100 * 15101 * 'access_bits' represents the access we are closing/downgrading. 15102 * 15103 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 15104 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 15105 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 15106 * 15107 * Errors are returned via the nfs4_error_t. 15108 */ 15109 void 15110 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 15111 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 15112 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 15113 uint_t mmap_flags) 15114 { 15115 nfs4_open_owner_t *oop; 15116 nfs4_open_stream_t *osp = NULL; 15117 int retry = 0; 15118 int num_retries = NFS4_NUM_RECOV_RETRIES; 15119 rnode4_t *rp; 15120 mntinfo4_t *mi; 15121 nfs4_recov_state_t recov_state; 15122 cred_t *cred_otw = NULL; 15123 bool_t recovonly = FALSE; 15124 int isrecov; 15125 int force_close; 15126 int close_failed = 0; 15127 int did_dec_count = 0; 15128 int did_start_op = 0; 15129 int did_force_recovlock = 0; 15130 int did_start_seqid_sync = 0; 15131 int have_sync_lock = 0; 15132 15133 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15134 15135 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 15136 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 15137 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 15138 len, maxprot, mmap_flags, access_bits)); 15139 15140 nfs4_error_zinit(ep); 15141 rp = VTOR4(vp); 15142 mi = VTOMI4(vp); 15143 isrecov = (close_type == CLOSE_RESEND || 15144 close_type == CLOSE_AFTER_RESEND); 15145 15146 /* 15147 * First get the open owner. 15148 */ 15149 if (!provided_osp) { 15150 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 15151 } else { 15152 oop = provided_osp->os_open_owner; 15153 ASSERT(oop != NULL); 15154 open_owner_hold(oop); 15155 } 15156 15157 if (!oop) { 15158 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15159 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 15160 "close type %d", (void *)rp, (void *)mi, (void *)cr, 15161 (void *)provided_osp, close_type)); 15162 ep->error = EIO; 15163 goto out; 15164 } 15165 15166 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 15167 recov_retry: 15168 osp = NULL; 15169 close_failed = 0; 15170 force_close = (close_type == CLOSE_FORCE); 15171 retry = 0; 15172 did_start_op = 0; 15173 did_force_recovlock = 0; 15174 did_start_seqid_sync = 0; 15175 have_sync_lock = 0; 15176 recovonly = FALSE; 15177 recov_state.rs_flags = 0; 15178 recov_state.rs_num_retry_despite_err = 0; 15179 15180 /* 15181 * Second synchronize with recovery. 15182 */ 15183 if (!isrecov) { 15184 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 15185 &recov_state, &recovonly); 15186 if (!ep->error) { 15187 did_start_op = 1; 15188 } else { 15189 close_failed = 1; 15190 /* 15191 * If we couldn't get start_fop, but have to 15192 * cleanup state, then at least acquire the 15193 * mi_recovlock so we can synchronize with 15194 * recovery. 15195 */ 15196 if (close_type == CLOSE_FORCE) { 15197 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 15198 RW_READER, FALSE); 15199 did_force_recovlock = 1; 15200 } else 15201 goto out; 15202 } 15203 } 15204 15205 /* 15206 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15207 * set 'recovonly' to TRUE since most likely this is due to 15208 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15209 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15210 * to retry, causing us to loop until recovery finishes. Plus we 15211 * don't need protection over the open seqid since we're not going 15212 * OTW, hence don't need to use the seqid. 15213 */ 15214 if (recovonly == FALSE) { 15215 /* need to grab the open owner sync before 'os_sync_lock' */ 15216 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15217 if (ep->error == EAGAIN) { 15218 ASSERT(!isrecov); 15219 if (did_start_op) 15220 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15221 &recov_state, TRUE); 15222 if (did_force_recovlock) 15223 nfs_rw_exit(&mi->mi_recovlock); 15224 goto recov_retry; 15225 } 15226 did_start_seqid_sync = 1; 15227 } 15228 15229 /* 15230 * Third get an open stream and acquire 'os_sync_lock' to 15231 * sychronize the opening/creating of an open stream with the 15232 * closing/destroying of an open stream. 15233 */ 15234 if (!provided_osp) { 15235 /* returns with 'os_sync_lock' held */ 15236 osp = find_open_stream(oop, rp); 15237 if (!osp) { 15238 ep->error = EIO; 15239 goto out; 15240 } 15241 } else { 15242 osp = provided_osp; 15243 open_stream_hold(osp); 15244 mutex_enter(&osp->os_sync_lock); 15245 } 15246 have_sync_lock = 1; 15247 15248 ASSERT(oop == osp->os_open_owner); 15249 15250 /* 15251 * Fourth, do any special pre-OTW CLOSE processing 15252 * based on the specific close type. 15253 */ 15254 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15255 !did_dec_count) { 15256 ASSERT(osp->os_open_ref_count > 0); 15257 osp->os_open_ref_count--; 15258 did_dec_count = 1; 15259 if (osp->os_open_ref_count == 0) 15260 osp->os_final_close = 1; 15261 } 15262 15263 if (close_type == CLOSE_FORCE) { 15264 /* see if somebody reopened the open stream. */ 15265 if (!osp->os_force_close) { 15266 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15267 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15268 "was reopened, vp %p", (void *)osp, (void *)vp)); 15269 ep->error = 0; 15270 ep->stat = NFS4_OK; 15271 goto out; 15272 } 15273 15274 if (!osp->os_final_close && !did_dec_count) { 15275 osp->os_open_ref_count--; 15276 did_dec_count = 1; 15277 } 15278 15279 /* 15280 * We can't depend on os_open_ref_count being 0 due to the 15281 * way executables are opened (VN_RELE to match a VOP_OPEN). 15282 */ 15283 #ifdef NOTYET 15284 ASSERT(osp->os_open_ref_count == 0); 15285 #endif 15286 if (osp->os_open_ref_count != 0) { 15287 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15288 "nfs4close_one: should panic here on an " 15289 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15290 "since this is probably the exec problem.")); 15291 15292 osp->os_open_ref_count = 0; 15293 } 15294 15295 /* 15296 * There is the possibility that nfs4close_one() 15297 * for close_type == CLOSE_DELMAP couldn't find the 15298 * open stream, thus couldn't decrement its os_mapcnt; 15299 * therefore we can't use this ASSERT yet. 15300 */ 15301 #ifdef NOTYET 15302 ASSERT(osp->os_mapcnt == 0); 15303 #endif 15304 osp->os_mapcnt = 0; 15305 } 15306 15307 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15308 ASSERT(osp->os_mapcnt >= btopr(len)); 15309 15310 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15311 osp->os_mmap_write -= btopr(len); 15312 if (maxprot & PROT_READ) 15313 osp->os_mmap_read -= btopr(len); 15314 if (maxprot & PROT_EXEC) 15315 osp->os_mmap_read -= btopr(len); 15316 /* mirror the PROT_NONE check in nfs4_addmap() */ 15317 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15318 !(maxprot & PROT_EXEC)) 15319 osp->os_mmap_read -= btopr(len); 15320 osp->os_mapcnt -= btopr(len); 15321 did_dec_count = 1; 15322 } 15323 15324 if (recovonly) { 15325 nfs4_lost_rqst_t lost_rqst; 15326 15327 /* request should not already be in recovery queue */ 15328 ASSERT(lrp == NULL); 15329 nfs4_error_init(ep, EINTR); 15330 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15331 osp, cred_otw, vp); 15332 mutex_exit(&osp->os_sync_lock); 15333 have_sync_lock = 0; 15334 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15335 lost_rqst.lr_op == OP_CLOSE ? 15336 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL); 15337 close_failed = 1; 15338 force_close = 0; 15339 goto close_cleanup; 15340 } 15341 15342 /* 15343 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15344 * we stopped operating on the open owner's <old oo_name, old seqid> 15345 * space, which means we stopped operating on the open stream 15346 * too. So don't go OTW (as the seqid is likely bad, and the 15347 * stateid could be stale, potentially triggering a false 15348 * setclientid), and just clean up the client's internal state. 15349 */ 15350 if (osp->os_orig_oo_name != oop->oo_name) { 15351 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15352 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15353 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15354 "oo_name %" PRIx64")", 15355 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15356 oop->oo_name)); 15357 close_failed = 1; 15358 } 15359 15360 /* If the file failed recovery, just quit. */ 15361 mutex_enter(&rp->r_statelock); 15362 if (rp->r_flags & R4RECOVERR) { 15363 close_failed = 1; 15364 } 15365 mutex_exit(&rp->r_statelock); 15366 15367 /* 15368 * If the force close path failed to obtain start_fop 15369 * then skip the OTW close and just remove the state. 15370 */ 15371 if (close_failed) 15372 goto close_cleanup; 15373 15374 /* 15375 * Fifth, check to see if there are still mapped pages or other 15376 * opens using this open stream. If there are then we can't 15377 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15378 */ 15379 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15380 nfs4_lost_rqst_t new_lost_rqst; 15381 bool_t needrecov = FALSE; 15382 cred_t *odg_cred_otw = NULL; 15383 seqid4 open_dg_seqid = 0; 15384 15385 if (osp->os_delegation) { 15386 /* 15387 * If this open stream was never OPENed OTW then we 15388 * surely can't DOWNGRADE it (especially since the 15389 * osp->open_stateid is really a delegation stateid 15390 * when os_delegation is 1). 15391 */ 15392 if (access_bits & FREAD) 15393 osp->os_share_acc_read--; 15394 if (access_bits & FWRITE) 15395 osp->os_share_acc_write--; 15396 osp->os_share_deny_none--; 15397 nfs4_error_zinit(ep); 15398 goto out; 15399 } 15400 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15401 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15402 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15403 if (needrecov && !isrecov) { 15404 bool_t abort; 15405 nfs4_bseqid_entry_t *bsep = NULL; 15406 15407 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15408 bsep = nfs4_create_bseqid_entry(oop, NULL, 15409 vp, 0, 15410 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15411 open_dg_seqid); 15412 15413 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15414 oop, osp, odg_cred_otw, vp, access_bits, 0); 15415 mutex_exit(&osp->os_sync_lock); 15416 have_sync_lock = 0; 15417 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15418 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15419 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15420 bsep, NULL, NULL); 15421 if (odg_cred_otw) 15422 crfree(odg_cred_otw); 15423 if (bsep) 15424 kmem_free(bsep, sizeof (*bsep)); 15425 15426 if (abort == TRUE) 15427 goto out; 15428 15429 if (did_start_seqid_sync) { 15430 nfs4_end_open_seqid_sync(oop); 15431 did_start_seqid_sync = 0; 15432 } 15433 open_stream_rele(osp, rp); 15434 15435 if (did_start_op) 15436 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15437 &recov_state, FALSE); 15438 if (did_force_recovlock) 15439 nfs_rw_exit(&mi->mi_recovlock); 15440 15441 goto recov_retry; 15442 } else { 15443 if (odg_cred_otw) 15444 crfree(odg_cred_otw); 15445 } 15446 goto out; 15447 } 15448 15449 /* 15450 * If this open stream was created as the results of an open 15451 * while holding a delegation, then just release it; no need 15452 * to do an OTW close. Otherwise do a "normal" OTW close. 15453 */ 15454 if (osp->os_delegation) { 15455 nfs4close_notw(vp, osp, &have_sync_lock); 15456 nfs4_error_zinit(ep); 15457 goto out; 15458 } 15459 15460 /* 15461 * If this stream is not valid, we're done. 15462 */ 15463 if (!osp->os_valid) { 15464 nfs4_error_zinit(ep); 15465 goto out; 15466 } 15467 15468 /* 15469 * Last open or mmap ref has vanished, need to do an OTW close. 15470 * First check to see if a close is still necessary. 15471 */ 15472 if (osp->os_failed_reopen) { 15473 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15474 "don't close OTW osp %p since reopen failed.", 15475 (void *)osp)); 15476 /* 15477 * Reopen of the open stream failed, hence the 15478 * stateid of the open stream is invalid/stale, and 15479 * sending this OTW would incorrectly cause another 15480 * round of recovery. In this case, we need to set 15481 * the 'os_valid' bit to 0 so another thread doesn't 15482 * come in and re-open this open stream before 15483 * this "closing" thread cleans up state (decrementing 15484 * the nfs4_server_t's state_ref_count and decrementing 15485 * the os_ref_count). 15486 */ 15487 osp->os_valid = 0; 15488 /* 15489 * This removes the reference obtained at OPEN; ie, 15490 * when the open stream structure was created. 15491 * 15492 * We don't have to worry about calling 'open_stream_rele' 15493 * since we our currently holding a reference to this 15494 * open stream which means the count can not go to 0 with 15495 * this decrement. 15496 */ 15497 ASSERT(osp->os_ref_count >= 2); 15498 osp->os_ref_count--; 15499 nfs4_error_zinit(ep); 15500 close_failed = 0; 15501 goto close_cleanup; 15502 } 15503 15504 ASSERT(osp->os_ref_count > 1); 15505 15506 /* 15507 * Sixth, try the CLOSE OTW. 15508 */ 15509 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15510 close_type, ep, &have_sync_lock); 15511 15512 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15513 /* 15514 * Let the recovery thread be responsible for 15515 * removing the state for CLOSE. 15516 */ 15517 close_failed = 1; 15518 force_close = 0; 15519 retry = 0; 15520 } 15521 15522 /* See if we need to retry with a different cred */ 15523 if ((ep->error == EACCES || 15524 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15525 cred_otw != cr) { 15526 crfree(cred_otw); 15527 cred_otw = cr; 15528 crhold(cred_otw); 15529 retry = 1; 15530 } 15531 15532 if (ep->error || ep->stat) 15533 close_failed = 1; 15534 15535 if (retry && !isrecov && num_retries-- > 0) { 15536 if (have_sync_lock) { 15537 mutex_exit(&osp->os_sync_lock); 15538 have_sync_lock = 0; 15539 } 15540 if (did_start_seqid_sync) { 15541 nfs4_end_open_seqid_sync(oop); 15542 did_start_seqid_sync = 0; 15543 } 15544 open_stream_rele(osp, rp); 15545 15546 if (did_start_op) 15547 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15548 &recov_state, FALSE); 15549 if (did_force_recovlock) 15550 nfs_rw_exit(&mi->mi_recovlock); 15551 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15552 "nfs4close_one: need to retry the close " 15553 "operation")); 15554 goto recov_retry; 15555 } 15556 close_cleanup: 15557 /* 15558 * Seventh and lastly, process our results. 15559 */ 15560 if (close_failed && force_close) { 15561 /* 15562 * It's ok to drop and regrab the 'os_sync_lock' since 15563 * nfs4close_notw() will recheck to make sure the 15564 * "close"/removal of state should happen. 15565 */ 15566 if (!have_sync_lock) { 15567 mutex_enter(&osp->os_sync_lock); 15568 have_sync_lock = 1; 15569 } 15570 /* 15571 * This is last call, remove the ref on the open 15572 * stream created by open and clean everything up. 15573 */ 15574 osp->os_pending_close = 0; 15575 nfs4close_notw(vp, osp, &have_sync_lock); 15576 nfs4_error_zinit(ep); 15577 } 15578 15579 if (!close_failed) { 15580 if (have_sync_lock) { 15581 osp->os_pending_close = 0; 15582 mutex_exit(&osp->os_sync_lock); 15583 have_sync_lock = 0; 15584 } else { 15585 mutex_enter(&osp->os_sync_lock); 15586 osp->os_pending_close = 0; 15587 mutex_exit(&osp->os_sync_lock); 15588 } 15589 if (did_start_op && recov_state.rs_sp != NULL) { 15590 mutex_enter(&recov_state.rs_sp->s_lock); 15591 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15592 mutex_exit(&recov_state.rs_sp->s_lock); 15593 } else { 15594 nfs4_dec_state_ref_count(mi); 15595 } 15596 nfs4_error_zinit(ep); 15597 } 15598 15599 out: 15600 if (have_sync_lock) 15601 mutex_exit(&osp->os_sync_lock); 15602 if (did_start_op) 15603 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15604 recovonly ? TRUE : FALSE); 15605 if (did_force_recovlock) 15606 nfs_rw_exit(&mi->mi_recovlock); 15607 if (cred_otw) 15608 crfree(cred_otw); 15609 if (osp) 15610 open_stream_rele(osp, rp); 15611 if (oop) { 15612 if (did_start_seqid_sync) 15613 nfs4_end_open_seqid_sync(oop); 15614 open_owner_rele(oop); 15615 } 15616 } 15617 15618 /* 15619 * Convert information returned by the server in the LOCK4denied 15620 * structure to the form required by fcntl. 15621 */ 15622 static void 15623 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15624 { 15625 nfs4_lo_name_t *lo; 15626 15627 #ifdef DEBUG 15628 if (denied_to_flk_debug) { 15629 lockt_denied_debug = lockt_denied; 15630 debug_enter("lockt_denied"); 15631 } 15632 #endif 15633 15634 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15635 flk->l_whence = 0; /* aka SEEK_SET */ 15636 flk->l_start = lockt_denied->offset; 15637 flk->l_len = lockt_denied->length; 15638 15639 /* 15640 * If the blocking clientid matches our client id, then we can 15641 * interpret the lockowner (since we built it). If not, then 15642 * fabricate a sysid and pid. Note that the l_sysid field 15643 * in *flk already has the local sysid. 15644 */ 15645 15646 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15647 15648 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15649 lo = (nfs4_lo_name_t *) 15650 lockt_denied->owner.owner_val; 15651 15652 flk->l_pid = lo->ln_pid; 15653 } else { 15654 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15655 "denied_to_flk: bad lock owner length\n")); 15656 15657 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15658 } 15659 } else { 15660 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15661 "denied_to_flk: foreign clientid\n")); 15662 15663 /* 15664 * Construct a new sysid which should be different from 15665 * sysids of other systems. 15666 */ 15667 15668 flk->l_sysid++; 15669 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15670 } 15671 } 15672 15673 static pid_t 15674 lo_to_pid(lock_owner4 *lop) 15675 { 15676 pid_t pid = 0; 15677 uchar_t *cp; 15678 int i; 15679 15680 cp = (uchar_t *)&lop->clientid; 15681 15682 for (i = 0; i < sizeof (lop->clientid); i++) 15683 pid += (pid_t)*cp++; 15684 15685 cp = (uchar_t *)lop->owner_val; 15686 15687 for (i = 0; i < lop->owner_len; i++) 15688 pid += (pid_t)*cp++; 15689 15690 return (pid); 15691 } 15692 15693 /* 15694 * Given a lock pointer, returns the length of that lock. 15695 * "end" is the last locked offset the "l_len" covers from 15696 * the start of the lock. 15697 */ 15698 static off64_t 15699 lock_to_end(flock64_t *lock) 15700 { 15701 off64_t lock_end; 15702 15703 if (lock->l_len == 0) 15704 lock_end = (off64_t)MAXEND; 15705 else 15706 lock_end = lock->l_start + lock->l_len - 1; 15707 15708 return (lock_end); 15709 } 15710 15711 /* 15712 * Given the end of a lock, it will return you the length "l_len" for that lock. 15713 */ 15714 static off64_t 15715 end_to_len(off64_t start, off64_t end) 15716 { 15717 off64_t lock_len; 15718 15719 ASSERT(end >= start); 15720 if (end == MAXEND) 15721 lock_len = 0; 15722 else 15723 lock_len = end - start + 1; 15724 15725 return (lock_len); 15726 } 15727 15728 /* 15729 * On given end for a lock it determines if it is the last locked offset 15730 * or not, if so keeps it as is, else adds one to return the length for 15731 * valid start. 15732 */ 15733 static off64_t 15734 start_check(off64_t x) 15735 { 15736 if (x == MAXEND) 15737 return (x); 15738 else 15739 return (x + 1); 15740 } 15741 15742 /* 15743 * See if these two locks overlap, and if so return 1; 15744 * otherwise, return 0. 15745 */ 15746 static int 15747 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15748 { 15749 off64_t llfp_end, curfp_end; 15750 15751 llfp_end = lock_to_end(llfp); 15752 curfp_end = lock_to_end(curfp); 15753 15754 if (((llfp_end >= curfp->l_start) && 15755 (llfp->l_start <= curfp->l_start)) || 15756 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15757 return (1); 15758 return (0); 15759 } 15760 15761 /* 15762 * Determine what the intersecting lock region is, and add that to the 15763 * 'nl_llpp' locklist in increasing order (by l_start). 15764 */ 15765 static void 15766 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15767 locklist_t **nl_llpp, vnode_t *vp) 15768 { 15769 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15770 off64_t lost_flp_end, local_flp_end, len, start; 15771 15772 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15773 15774 if (!locks_intersect(lost_flp, local_flp)) 15775 return; 15776 15777 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15778 "locks intersect")); 15779 15780 lost_flp_end = lock_to_end(lost_flp); 15781 local_flp_end = lock_to_end(local_flp); 15782 15783 /* Find the starting point of the intersecting region */ 15784 if (local_flp->l_start > lost_flp->l_start) 15785 start = local_flp->l_start; 15786 else 15787 start = lost_flp->l_start; 15788 15789 /* Find the lenght of the intersecting region */ 15790 if (lost_flp_end < local_flp_end) 15791 len = end_to_len(start, lost_flp_end); 15792 else 15793 len = end_to_len(start, local_flp_end); 15794 15795 /* 15796 * Prepare the flock structure for the intersection found and insert 15797 * it into the new list in increasing l_start order. This list contains 15798 * intersections of locks registered by the client with the local host 15799 * and the lost lock. 15800 * The lock type of this lock is the same as that of the local_flp. 15801 */ 15802 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15803 intersect_llp->ll_flock.l_start = start; 15804 intersect_llp->ll_flock.l_len = len; 15805 intersect_llp->ll_flock.l_type = local_flp->l_type; 15806 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15807 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15808 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15809 intersect_llp->ll_vp = vp; 15810 15811 tmp_fllp = *nl_llpp; 15812 cur_fllp = NULL; 15813 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15814 intersect_llp->ll_flock.l_start) { 15815 cur_fllp = tmp_fllp; 15816 tmp_fllp = tmp_fllp->ll_next; 15817 } 15818 if (cur_fllp == NULL) { 15819 /* first on the list */ 15820 intersect_llp->ll_next = *nl_llpp; 15821 *nl_llpp = intersect_llp; 15822 } else { 15823 intersect_llp->ll_next = cur_fllp->ll_next; 15824 cur_fllp->ll_next = intersect_llp; 15825 } 15826 15827 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15828 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15829 intersect_llp->ll_flock.l_start, 15830 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15831 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15832 } 15833 15834 /* 15835 * Our local locking current state is potentially different than 15836 * what the NFSv4 server thinks we have due to a lost lock that was 15837 * resent and then received. We need to reset our "NFSv4" locking 15838 * state to match the current local locking state for this pid since 15839 * that is what the user/application sees as what the world is. 15840 * 15841 * We cannot afford to drop the open/lock seqid sync since then we can 15842 * get confused about what the current local locking state "is" versus 15843 * "was". 15844 * 15845 * If we are unable to fix up the locks, we send SIGLOST to the affected 15846 * process. This is not done if the filesystem has been forcibly 15847 * unmounted, in case the process has already exited and a new process 15848 * exists with the same pid. 15849 */ 15850 static void 15851 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15852 nfs4_lock_owner_t *lop) 15853 { 15854 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15855 mntinfo4_t *mi = VTOMI4(vp); 15856 const int cmd = F_SETLK; 15857 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15858 flock64_t ul_fl; 15859 15860 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15861 "nfs4_reinstitute_local_lock_state")); 15862 15863 /* 15864 * Find active locks for this vp from the local locking code. 15865 * Scan through this list and find out the locks that intersect with 15866 * the lost lock. Once we find the lock that intersects, add the 15867 * intersection area as a new lock to a new list "ri_llp". The lock 15868 * type of the intersection region lock added to ri_llp is the same 15869 * as that found in the active lock list, "list". The intersecting 15870 * region locks are added to ri_llp in increasing l_start order. 15871 */ 15872 ASSERT(nfs_zone() == mi->mi_zone); 15873 15874 locks = flk_active_locks_for_vp(vp); 15875 ri_llp = NULL; 15876 15877 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15878 ASSERT(llp->ll_vp == vp); 15879 /* 15880 * Pick locks that belong to this pid/lockowner 15881 */ 15882 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15883 continue; 15884 15885 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15886 } 15887 15888 /* 15889 * Now we have the list of intersections with the lost lock. These are 15890 * the locks that were/are active before the server replied to the 15891 * last/lost lock. Issue these locks to the server here. Playing these 15892 * locks to the server will re-establish aur current local locking state 15893 * with the v4 server. 15894 * If we get an error, send SIGLOST to the application for that lock. 15895 */ 15896 15897 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15898 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15899 "nfs4_reinstitute_local_lock_state: need to issue " 15900 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15901 llp->ll_flock.l_start, 15902 llp->ll_flock.l_start + llp->ll_flock.l_len, 15903 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15904 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15905 /* 15906 * No need to relock what we already have 15907 */ 15908 if (llp->ll_flock.l_type == lost_flp->l_type) 15909 continue; 15910 15911 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15912 } 15913 15914 /* 15915 * Now keeping the start of the lost lock as our reference parse the 15916 * newly created ri_llp locklist to find the ranges that we have locked 15917 * with the v4 server but not in the current local locking. We need 15918 * to unlock these ranges. 15919 * These ranges can also be reffered to as those ranges, where the lost 15920 * lock does not overlap with the locks in the ri_llp but are locked 15921 * since the server replied to the lost lock. 15922 */ 15923 cur_start = lost_flp->l_start; 15924 lost_flp_end = lock_to_end(lost_flp); 15925 15926 ul_fl.l_type = F_UNLCK; 15927 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15928 ul_fl.l_sysid = lost_flp->l_sysid; 15929 ul_fl.l_pid = lost_flp->l_pid; 15930 15931 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15932 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15933 15934 if (llp->ll_flock.l_start <= cur_start) { 15935 cur_start = start_check(llp_ll_flock_end); 15936 continue; 15937 } 15938 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15939 "nfs4_reinstitute_local_lock_state: " 15940 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15941 cur_start, llp->ll_flock.l_start)); 15942 15943 ul_fl.l_start = cur_start; 15944 ul_fl.l_len = end_to_len(cur_start, 15945 (llp->ll_flock.l_start - 1)); 15946 15947 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15948 cur_start = start_check(llp_ll_flock_end); 15949 } 15950 15951 /* 15952 * In the case where the lost lock ends after all intersecting locks, 15953 * unlock the last part of the lost lock range. 15954 */ 15955 if (cur_start != start_check(lost_flp_end)) { 15956 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15957 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15958 "lost lock region [%"PRIx64" - %"PRIx64"]", 15959 cur_start, lost_flp->l_start + lost_flp->l_len)); 15960 15961 ul_fl.l_start = cur_start; 15962 /* 15963 * Is it an to-EOF lock? if so unlock till the end 15964 */ 15965 if (lost_flp->l_len == 0) 15966 ul_fl.l_len = 0; 15967 else 15968 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15969 15970 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15971 } 15972 15973 if (locks != NULL) 15974 flk_free_locklist(locks); 15975 15976 /* Free up our newly created locklist */ 15977 for (llp = ri_llp; llp != NULL; ) { 15978 tmp_llp = llp->ll_next; 15979 kmem_free(llp, sizeof (locklist_t)); 15980 llp = tmp_llp; 15981 } 15982 15983 /* 15984 * Now return back to the original calling nfs4frlock() 15985 * and let us naturally drop our seqid syncs. 15986 */ 15987 } 15988 15989 /* 15990 * Create a lost state record for the given lock reinstantiation request 15991 * and push it onto the lost state queue. 15992 */ 15993 static void 15994 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15995 nfs4_lock_owner_t *lop) 15996 { 15997 nfs4_lost_rqst_t req; 15998 nfs_lock_type4 locktype; 15999 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 16000 16001 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 16002 16003 locktype = flk_to_locktype(cmd, flk->l_type); 16004 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 16005 NULL, NULL, lop, flk, &req, cr, vp); 16006 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 16007 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 16008 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 16009 NULL, NULL, NULL); 16010 } 16011