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 (c) 2016 STRATO AG. All rights reserved. 24 */ 25 26 /* 27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 28 */ 29 30 /* 31 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 32 * Use is subject to license terms. 33 */ 34 35 /* 36 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 37 * All Rights Reserved 38 */ 39 40 /* 41 * Copyright (c) 2013, Joyent, Inc. 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 *, rnode4_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 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */ 300 301 /* 302 * Error flags used to pass information about certain special errors 303 * which need to be handled specially. 304 */ 305 #define NFS_EOF -98 306 #define NFS_VERF_MISMATCH -97 307 308 /* 309 * Flags used to differentiate between which operation drove the 310 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 311 */ 312 #define NFS4_CLOSE_OP 0x1 313 #define NFS4_DELMAP_OP 0x2 314 #define NFS4_INACTIVE_OP 0x3 315 316 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 317 318 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 319 #define ALIGN64(x, ptr, sz) \ 320 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 321 if (x) { \ 322 x = sizeof (uint64_t) - (x); \ 323 sz -= (x); \ 324 ptr += (x); \ 325 } 326 327 #ifdef DEBUG 328 int nfs4_client_attr_debug = 0; 329 int nfs4_client_state_debug = 0; 330 int nfs4_client_shadow_debug = 0; 331 int nfs4_client_lock_debug = 0; 332 int nfs4_seqid_sync = 0; 333 int nfs4_client_map_debug = 0; 334 static int nfs4_pageio_debug = 0; 335 int nfs4_client_inactive_debug = 0; 336 int nfs4_client_recov_debug = 0; 337 int nfs4_client_failover_debug = 0; 338 int nfs4_client_call_debug = 0; 339 int nfs4_client_lookup_debug = 0; 340 int nfs4_client_zone_debug = 0; 341 int nfs4_lost_rqst_debug = 0; 342 int nfs4_rdattrerr_debug = 0; 343 int nfs4_open_stream_debug = 0; 344 345 int nfs4read_error_inject; 346 347 static int nfs4_create_misses = 0; 348 349 static int nfs4_readdir_cache_shorts = 0; 350 static int nfs4_readdir_readahead = 0; 351 352 static int nfs4_bio_do_stop = 0; 353 354 static int nfs4_lostpage = 0; /* number of times we lost original page */ 355 356 int nfs4_mmap_debug = 0; 357 358 static int nfs4_pathconf_cache_hits = 0; 359 static int nfs4_pathconf_cache_misses = 0; 360 361 int nfs4close_all_cnt; 362 int nfs4close_one_debug = 0; 363 int nfs4close_notw_debug = 0; 364 365 int denied_to_flk_debug = 0; 366 void *lockt_denied_debug; 367 368 #endif 369 370 /* 371 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 372 * or NFS4ERR_RESOURCE. 373 */ 374 static int confirm_retry_sec = 30; 375 376 static int nfs4_lookup_neg_cache = 1; 377 378 /* 379 * number of pages to read ahead 380 * optimized for 100 base-T. 381 */ 382 static int nfs4_nra = 4; 383 384 static int nfs4_do_symlink_cache = 1; 385 386 static int nfs4_pathconf_disable_cache = 0; 387 388 /* 389 * These are the vnode ops routines which implement the vnode interface to 390 * the networked file system. These routines just take their parameters, 391 * make them look networkish by putting the right info into interface structs, 392 * and then calling the appropriate remote routine(s) to do the work. 393 * 394 * Note on directory name lookup cacheing: If we detect a stale fhandle, 395 * we purge the directory cache relative to that vnode. This way, the 396 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 397 * more details on rnode locking. 398 */ 399 400 struct vnodeops *nfs4_vnodeops; 401 402 const fs_operation_def_t nfs4_vnodeops_template[] = { 403 VOPNAME_OPEN, { .vop_open = nfs4_open }, 404 VOPNAME_CLOSE, { .vop_close = nfs4_close }, 405 VOPNAME_READ, { .vop_read = nfs4_read }, 406 VOPNAME_WRITE, { .vop_write = nfs4_write }, 407 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl }, 408 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr }, 409 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr }, 410 VOPNAME_ACCESS, { .vop_access = nfs4_access }, 411 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup }, 412 VOPNAME_CREATE, { .vop_create = nfs4_create }, 413 VOPNAME_REMOVE, { .vop_remove = nfs4_remove }, 414 VOPNAME_LINK, { .vop_link = nfs4_link }, 415 VOPNAME_RENAME, { .vop_rename = nfs4_rename }, 416 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir }, 417 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir }, 418 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir }, 419 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink }, 420 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink }, 421 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync }, 422 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive }, 423 VOPNAME_FID, { .vop_fid = nfs4_fid }, 424 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock }, 425 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock }, 426 VOPNAME_SEEK, { .vop_seek = nfs4_seek }, 427 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock }, 428 VOPNAME_SPACE, { .vop_space = nfs4_space }, 429 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp }, 430 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage }, 431 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage }, 432 VOPNAME_MAP, { .vop_map = nfs4_map }, 433 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap }, 434 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap }, 435 /* no separate nfs4_dump */ 436 VOPNAME_DUMP, { .vop_dump = nfs_dump }, 437 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf }, 438 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio }, 439 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose }, 440 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr }, 441 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr }, 442 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock }, 443 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 444 NULL, NULL 445 }; 446 447 /* 448 * The following are subroutines and definitions to set args or get res 449 * for the different nfsv4 ops 450 */ 451 452 void 453 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 454 { 455 int i; 456 457 for (i = 0; i < arglen; i++) { 458 if (argop[i].argop == OP_LOOKUP) { 459 kmem_free( 460 argop[i].nfs_argop4_u.oplookup. 461 objname.utf8string_val, 462 argop[i].nfs_argop4_u.oplookup. 463 objname.utf8string_len); 464 } 465 } 466 } 467 468 static void 469 nfs4args_lock_free(nfs_argop4 *argop) 470 { 471 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 472 473 if (locker->new_lock_owner == TRUE) { 474 open_to_lock_owner4 *open_owner; 475 476 open_owner = &locker->locker4_u.open_owner; 477 if (open_owner->lock_owner.owner_val != NULL) { 478 kmem_free(open_owner->lock_owner.owner_val, 479 open_owner->lock_owner.owner_len); 480 } 481 } 482 } 483 484 static void 485 nfs4args_lockt_free(nfs_argop4 *argop) 486 { 487 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 488 489 if (lowner->owner_val != NULL) { 490 kmem_free(lowner->owner_val, lowner->owner_len); 491 } 492 } 493 494 static void 495 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 496 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 497 nfs4_stateid_types_t *sid_types) 498 { 499 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 500 mntinfo4_t *mi; 501 502 argop->argop = OP_SETATTR; 503 /* 504 * The stateid is set to 0 if client is not modifying the size 505 * and otherwise to whatever nfs4_get_stateid() returns. 506 * 507 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 508 * state struct could be found for the process/file pair. We may 509 * want to change this in the future (by OPENing the file). See 510 * bug # 4474852. 511 */ 512 if (vap->va_mask & AT_SIZE) { 513 514 ASSERT(rp != NULL); 515 mi = VTOMI4(RTOV4(rp)); 516 517 argop->nfs_argop4_u.opsetattr.stateid = 518 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 519 OP_SETATTR, sid_types, FALSE); 520 } else { 521 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 522 sizeof (stateid4)); 523 } 524 525 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 526 if (*error) 527 bzero(attr, sizeof (*attr)); 528 } 529 530 static void 531 nfs4args_setattr_free(nfs_argop4 *argop) 532 { 533 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 534 } 535 536 static int 537 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 538 bitmap4 supp) 539 { 540 fattr4 *attr; 541 int error = 0; 542 543 argop->argop = op; 544 switch (op) { 545 case OP_VERIFY: 546 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 547 break; 548 case OP_NVERIFY: 549 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 550 break; 551 default: 552 return (EINVAL); 553 } 554 if (!error) 555 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 556 if (error) 557 bzero(attr, sizeof (*attr)); 558 return (error); 559 } 560 561 static void 562 nfs4args_verify_free(nfs_argop4 *argop) 563 { 564 switch (argop->argop) { 565 case OP_VERIFY: 566 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 567 break; 568 case OP_NVERIFY: 569 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 570 break; 571 default: 572 break; 573 } 574 } 575 576 static void 577 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 578 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 579 { 580 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 581 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 582 583 argop->argop = OP_WRITE; 584 wargs->stable = stable; 585 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 586 mi, OP_WRITE, sid_tp); 587 wargs->mblk = NULL; 588 *wargs_pp = wargs; 589 } 590 591 void 592 nfs4args_copen_free(OPEN4cargs *open_args) 593 { 594 if (open_args->owner.owner_val) { 595 kmem_free(open_args->owner.owner_val, 596 open_args->owner.owner_len); 597 } 598 if ((open_args->opentype == OPEN4_CREATE) && 599 (open_args->mode != EXCLUSIVE4)) { 600 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 601 } 602 } 603 604 /* 605 * XXX: This is referenced in modstubs.s 606 */ 607 struct vnodeops * 608 nfs4_getvnodeops(void) 609 { 610 return (nfs4_vnodeops); 611 } 612 613 /* 614 * The OPEN operation opens a regular file. 615 */ 616 /*ARGSUSED3*/ 617 static int 618 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 619 { 620 vnode_t *dvp = NULL; 621 rnode4_t *rp, *drp; 622 int error; 623 int just_been_created; 624 char fn[MAXNAMELEN]; 625 626 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 627 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 628 return (EIO); 629 rp = VTOR4(*vpp); 630 631 /* 632 * Check to see if opening something besides a regular file; 633 * if so skip the OTW call 634 */ 635 if ((*vpp)->v_type != VREG) { 636 error = nfs4_open_non_reg_file(vpp, flag, cr); 637 return (error); 638 } 639 640 /* 641 * XXX - would like a check right here to know if the file is 642 * executable or not, so as to skip OTW 643 */ 644 645 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 646 return (error); 647 648 drp = VTOR4(dvp); 649 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 650 return (EINTR); 651 652 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) { 653 nfs_rw_exit(&drp->r_rwlock); 654 return (error); 655 } 656 657 /* 658 * See if this file has just been CREATEd. 659 * If so, clear the flag and update the dnlc, which was previously 660 * skipped in nfs4_create. 661 * XXX need better serilization on this. 662 * XXX move this into the nf4open_otw call, after we have 663 * XXX acquired the open owner seqid sync. 664 */ 665 mutex_enter(&rp->r_statev4_lock); 666 if (rp->created_v4) { 667 rp->created_v4 = 0; 668 mutex_exit(&rp->r_statev4_lock); 669 670 dnlc_update(dvp, fn, *vpp); 671 /* This is needed so we don't bump the open ref count */ 672 just_been_created = 1; 673 } else { 674 mutex_exit(&rp->r_statev4_lock); 675 just_been_created = 0; 676 } 677 678 /* 679 * If caller specified O_TRUNC/FTRUNC, then be sure to set 680 * FWRITE (to drive successful setattr(size=0) after open) 681 */ 682 if (flag & FTRUNC) 683 flag |= FWRITE; 684 685 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 686 just_been_created); 687 688 if (!error && !((*vpp)->v_flag & VROOT)) 689 dnlc_update(dvp, fn, *vpp); 690 691 nfs_rw_exit(&drp->r_rwlock); 692 693 /* release the hold from vtodv */ 694 VN_RELE(dvp); 695 696 /* exchange the shadow for the master vnode, if needed */ 697 698 if (error == 0 && IS_SHADOW(*vpp, rp)) 699 sv_exchange(vpp); 700 701 return (error); 702 } 703 704 /* 705 * See if there's a "lost open" request to be saved and recovered. 706 */ 707 static void 708 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 709 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 710 vnode_t *dvp, OPEN4cargs *open_args) 711 { 712 vfs_t *vfsp; 713 char *srccfp; 714 715 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 716 717 if (error != ETIMEDOUT && error != EINTR && 718 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 719 lost_rqstp->lr_op = 0; 720 return; 721 } 722 723 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 724 "nfs4open_save_lost_rqst: error %d", error)); 725 726 lost_rqstp->lr_op = OP_OPEN; 727 728 /* 729 * The vp (if it is not NULL) and dvp are held and rele'd via 730 * the recovery code. See nfs4_save_lost_rqst. 731 */ 732 lost_rqstp->lr_vp = vp; 733 lost_rqstp->lr_dvp = dvp; 734 lost_rqstp->lr_oop = oop; 735 lost_rqstp->lr_osp = NULL; 736 lost_rqstp->lr_lop = NULL; 737 lost_rqstp->lr_cr = cr; 738 lost_rqstp->lr_flk = NULL; 739 lost_rqstp->lr_oacc = open_args->share_access; 740 lost_rqstp->lr_odeny = open_args->share_deny; 741 lost_rqstp->lr_oclaim = open_args->claim; 742 if (open_args->claim == CLAIM_DELEGATE_CUR) { 743 lost_rqstp->lr_ostateid = 744 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 745 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 746 } else { 747 srccfp = open_args->open_claim4_u.cfile; 748 } 749 lost_rqstp->lr_ofile.utf8string_len = 0; 750 lost_rqstp->lr_ofile.utf8string_val = NULL; 751 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 752 lost_rqstp->lr_putfirst = FALSE; 753 } 754 755 struct nfs4_excl_time { 756 uint32 seconds; 757 uint32 nseconds; 758 }; 759 760 /* 761 * The OPEN operation creates and/or opens a regular file 762 * 763 * ARGSUSED 764 */ 765 static int 766 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 767 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 768 enum createmode4 createmode, int file_just_been_created) 769 { 770 rnode4_t *rp; 771 rnode4_t *drp = VTOR4(dvp); 772 vnode_t *vp = NULL; 773 vnode_t *vpi = *vpp; 774 bool_t needrecov = FALSE; 775 776 int doqueue = 1; 777 778 COMPOUND4args_clnt args; 779 COMPOUND4res_clnt res; 780 nfs_argop4 *argop; 781 nfs_resop4 *resop; 782 int argoplist_size; 783 int idx_open, idx_fattr; 784 785 GETFH4res *gf_res = NULL; 786 OPEN4res *op_res = NULL; 787 nfs4_ga_res_t *garp; 788 fattr4 *attr = NULL; 789 struct nfs4_excl_time verf; 790 bool_t did_excl_setup = FALSE; 791 int created_osp; 792 793 OPEN4cargs *open_args; 794 nfs4_open_owner_t *oop = NULL; 795 nfs4_open_stream_t *osp = NULL; 796 seqid4 seqid = 0; 797 bool_t retry_open = FALSE; 798 nfs4_recov_state_t recov_state; 799 nfs4_lost_rqst_t lost_rqst; 800 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 801 hrtime_t t; 802 int acc = 0; 803 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 804 cred_t *ncr = NULL; 805 806 nfs4_sharedfh_t *otw_sfh; 807 nfs4_sharedfh_t *orig_sfh; 808 int fh_differs = 0; 809 int numops, setgid_flag; 810 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 811 812 /* 813 * Make sure we properly deal with setting the right gid on 814 * a newly created file to reflect the parent's setgid bit 815 */ 816 setgid_flag = 0; 817 if (create_flag && in_va) { 818 819 /* 820 * If there is grpid mount flag used or 821 * the parent's directory has the setgid bit set 822 * _and_ the client was able to get a valid mapping 823 * for the parent dir's owner_group, we want to 824 * append NVERIFY(owner_group == dva.va_gid) and 825 * SETATTR to the CREATE compound. 826 */ 827 mutex_enter(&drp->r_statelock); 828 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID || 829 drp->r_attr.va_mode & VSGID) && 830 drp->r_attr.va_gid != GID_NOBODY) { 831 in_va->va_mask |= AT_GID; 832 in_va->va_gid = drp->r_attr.va_gid; 833 setgid_flag = 1; 834 } 835 mutex_exit(&drp->r_statelock); 836 } 837 838 /* 839 * Normal/non-create compound: 840 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 841 * 842 * Open(create) compound no setgid: 843 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 844 * RESTOREFH + GETATTR 845 * 846 * Open(create) setgid: 847 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 848 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 849 * NVERIFY(grp) + SETATTR 850 */ 851 if (setgid_flag) { 852 numops = 10; 853 idx_open = 1; 854 idx_fattr = 3; 855 } else if (create_flag) { 856 numops = 7; 857 idx_open = 2; 858 idx_fattr = 4; 859 } else { 860 numops = 4; 861 idx_open = 1; 862 idx_fattr = 3; 863 } 864 865 args.array_len = numops; 866 argoplist_size = numops * sizeof (nfs_argop4); 867 argop = kmem_alloc(argoplist_size, KM_SLEEP); 868 869 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 870 "open %s open flag 0x%x cred %p", file_name, open_flag, 871 (void *)cr)); 872 873 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 874 if (create_flag) { 875 /* 876 * We are to create a file. Initialize the passed in vnode 877 * pointer. 878 */ 879 vpi = NULL; 880 } else { 881 /* 882 * Check to see if the client owns a read delegation and is 883 * trying to open for write. If so, then return the delegation 884 * to avoid the server doing a cb_recall and returning DELAY. 885 * NB - we don't use the statev4_lock here because we'd have 886 * to drop the lock anyway and the result would be stale. 887 */ 888 if ((open_flag & FWRITE) && 889 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 890 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 891 892 /* 893 * If the file has a delegation, then do an access check up 894 * front. This avoids having to an access check later after 895 * we've already done start_op, which could deadlock. 896 */ 897 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 898 if (open_flag & FREAD && 899 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0) 900 acc |= VREAD; 901 if (open_flag & FWRITE && 902 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0) 903 acc |= VWRITE; 904 } 905 } 906 907 drp = VTOR4(dvp); 908 909 recov_state.rs_flags = 0; 910 recov_state.rs_num_retry_despite_err = 0; 911 cred_otw = cr; 912 913 recov_retry: 914 fh_differs = 0; 915 nfs4_error_zinit(&e); 916 917 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 918 if (e.error) { 919 if (ncr != NULL) 920 crfree(ncr); 921 kmem_free(argop, argoplist_size); 922 return (e.error); 923 } 924 925 args.ctag = TAG_OPEN; 926 args.array_len = numops; 927 args.array = argop; 928 929 /* putfh directory fh */ 930 argop[0].argop = OP_CPUTFH; 931 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 932 933 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 934 argop[idx_open].argop = OP_COPEN; 935 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 936 open_args->claim = CLAIM_NULL; 937 938 /* name of file */ 939 open_args->open_claim4_u.cfile = file_name; 940 open_args->owner.owner_len = 0; 941 open_args->owner.owner_val = NULL; 942 943 if (create_flag) { 944 /* CREATE a file */ 945 open_args->opentype = OPEN4_CREATE; 946 open_args->mode = createmode; 947 if (createmode == EXCLUSIVE4) { 948 if (did_excl_setup == FALSE) { 949 verf.seconds = zone_get_hostid(NULL); 950 if (verf.seconds != 0) 951 verf.nseconds = newnum(); 952 else { 953 timestruc_t now; 954 955 gethrestime(&now); 956 verf.seconds = now.tv_sec; 957 verf.nseconds = now.tv_nsec; 958 } 959 /* 960 * Since the server will use this value for the 961 * mtime, make sure that it can't overflow. Zero 962 * out the MSB. The actual value does not matter 963 * here, only its uniqeness. 964 */ 965 verf.seconds &= INT32_MAX; 966 did_excl_setup = TRUE; 967 } 968 969 /* Now copy over verifier to OPEN4args. */ 970 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 971 } else { 972 int v_error; 973 bitmap4 supp_attrs; 974 servinfo4_t *svp; 975 976 attr = &open_args->createhow4_u.createattrs; 977 978 svp = drp->r_server; 979 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 980 supp_attrs = svp->sv_supp_attrs; 981 nfs_rw_exit(&svp->sv_lock); 982 983 /* GUARDED4 or UNCHECKED4 */ 984 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 985 supp_attrs); 986 if (v_error) { 987 bzero(attr, sizeof (*attr)); 988 nfs4args_copen_free(open_args); 989 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 990 &recov_state, FALSE); 991 if (ncr != NULL) 992 crfree(ncr); 993 kmem_free(argop, argoplist_size); 994 return (v_error); 995 } 996 } 997 } else { 998 /* NO CREATE */ 999 open_args->opentype = OPEN4_NOCREATE; 1000 } 1001 1002 if (recov_state.rs_sp != NULL) { 1003 mutex_enter(&recov_state.rs_sp->s_lock); 1004 open_args->owner.clientid = recov_state.rs_sp->clientid; 1005 mutex_exit(&recov_state.rs_sp->s_lock); 1006 } else { 1007 /* XXX should we just fail here? */ 1008 open_args->owner.clientid = 0; 1009 } 1010 1011 /* 1012 * This increments oop's ref count or creates a temporary 'just_created' 1013 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 1014 * completes. 1015 */ 1016 mutex_enter(&VTOMI4(dvp)->mi_lock); 1017 1018 /* See if a permanent or just created open owner exists */ 1019 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 1020 if (!oop) { 1021 /* 1022 * This open owner does not exist so create a temporary 1023 * just created one. 1024 */ 1025 oop = create_open_owner(cr, VTOMI4(dvp)); 1026 ASSERT(oop != NULL); 1027 } 1028 mutex_exit(&VTOMI4(dvp)->mi_lock); 1029 1030 /* this length never changes, do alloc before seqid sync */ 1031 open_args->owner.owner_len = sizeof (oop->oo_name); 1032 open_args->owner.owner_val = 1033 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1034 1035 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 1036 if (e.error == EAGAIN) { 1037 open_owner_rele(oop); 1038 nfs4args_copen_free(open_args); 1039 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1040 if (ncr != NULL) { 1041 crfree(ncr); 1042 ncr = NULL; 1043 } 1044 goto recov_retry; 1045 } 1046 1047 /* Check to see if we need to do the OTW call */ 1048 if (!create_flag) { 1049 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1050 file_just_been_created, &e.error, acc, &recov_state)) { 1051 1052 /* 1053 * The OTW open is not necessary. Either 1054 * the open can succeed without it (eg. 1055 * delegation, error == 0) or the open 1056 * must fail due to an access failure 1057 * (error != 0). In either case, tidy 1058 * up and return. 1059 */ 1060 1061 nfs4_end_open_seqid_sync(oop); 1062 open_owner_rele(oop); 1063 nfs4args_copen_free(open_args); 1064 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1065 if (ncr != NULL) 1066 crfree(ncr); 1067 kmem_free(argop, argoplist_size); 1068 return (e.error); 1069 } 1070 } 1071 1072 bcopy(&oop->oo_name, open_args->owner.owner_val, 1073 open_args->owner.owner_len); 1074 1075 seqid = nfs4_get_open_seqid(oop) + 1; 1076 open_args->seqid = seqid; 1077 open_args->share_access = 0; 1078 if (open_flag & FREAD) 1079 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1080 if (open_flag & FWRITE) 1081 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1082 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1083 1084 1085 1086 /* 1087 * getfh w/sanity check for idx_open/idx_fattr 1088 */ 1089 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1090 argop[idx_open + 1].argop = OP_GETFH; 1091 1092 /* getattr */ 1093 argop[idx_fattr].argop = OP_GETATTR; 1094 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1095 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1096 1097 if (setgid_flag) { 1098 vattr_t _v; 1099 servinfo4_t *svp; 1100 bitmap4 supp_attrs; 1101 1102 svp = drp->r_server; 1103 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1104 supp_attrs = svp->sv_supp_attrs; 1105 nfs_rw_exit(&svp->sv_lock); 1106 1107 /* 1108 * For setgid case, we need to: 1109 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1110 */ 1111 argop[4].argop = OP_SAVEFH; 1112 1113 argop[5].argop = OP_CPUTFH; 1114 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1115 1116 argop[6].argop = OP_GETATTR; 1117 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1118 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1119 1120 argop[7].argop = OP_RESTOREFH; 1121 1122 /* 1123 * nverify 1124 */ 1125 _v.va_mask = AT_GID; 1126 _v.va_gid = in_va->va_gid; 1127 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1128 supp_attrs))) { 1129 1130 /* 1131 * setattr 1132 * 1133 * We _know_ we're not messing with AT_SIZE or 1134 * AT_XTIME, so no need for stateid or flags. 1135 * Also we specify NULL rp since we're only 1136 * interested in setting owner_group attributes. 1137 */ 1138 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1139 supp_attrs, &e.error, 0); 1140 if (e.error) 1141 nfs4args_verify_free(&argop[8]); 1142 } 1143 1144 if (e.error) { 1145 /* 1146 * XXX - Revisit the last argument to nfs4_end_op() 1147 * once 5020486 is fixed. 1148 */ 1149 nfs4_end_open_seqid_sync(oop); 1150 open_owner_rele(oop); 1151 nfs4args_copen_free(open_args); 1152 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1153 if (ncr != NULL) 1154 crfree(ncr); 1155 kmem_free(argop, argoplist_size); 1156 return (e.error); 1157 } 1158 } else if (create_flag) { 1159 argop[1].argop = OP_SAVEFH; 1160 1161 argop[5].argop = OP_RESTOREFH; 1162 1163 argop[6].argop = OP_GETATTR; 1164 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1165 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1166 } 1167 1168 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1169 "nfs4open_otw: %s call, nm %s, rp %s", 1170 needrecov ? "recov" : "first", file_name, 1171 rnode4info(VTOR4(dvp)))); 1172 1173 t = gethrtime(); 1174 1175 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1176 1177 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1178 nfs4_set_open_seqid(seqid, oop, args.ctag); 1179 1180 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1181 1182 if (e.error || needrecov) { 1183 bool_t abort = FALSE; 1184 1185 if (needrecov) { 1186 nfs4_bseqid_entry_t *bsep = NULL; 1187 1188 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1189 cred_otw, vpi, dvp, open_args); 1190 1191 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1192 bsep = nfs4_create_bseqid_entry(oop, NULL, 1193 vpi, 0, args.ctag, open_args->seqid); 1194 num_bseqid_retry--; 1195 } 1196 1197 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1198 NULL, lost_rqst.lr_op == OP_OPEN ? 1199 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL); 1200 1201 if (bsep) 1202 kmem_free(bsep, sizeof (*bsep)); 1203 /* give up if we keep getting BAD_SEQID */ 1204 if (num_bseqid_retry == 0) 1205 abort = TRUE; 1206 if (abort == TRUE && e.error == 0) 1207 e.error = geterrno4(res.status); 1208 } 1209 nfs4_end_open_seqid_sync(oop); 1210 open_owner_rele(oop); 1211 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1212 nfs4args_copen_free(open_args); 1213 if (setgid_flag) { 1214 nfs4args_verify_free(&argop[8]); 1215 nfs4args_setattr_free(&argop[9]); 1216 } 1217 if (!e.error) 1218 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1219 if (ncr != NULL) { 1220 crfree(ncr); 1221 ncr = NULL; 1222 } 1223 if (!needrecov || abort == TRUE || e.error == EINTR || 1224 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1225 kmem_free(argop, argoplist_size); 1226 return (e.error); 1227 } 1228 goto recov_retry; 1229 } 1230 1231 /* 1232 * Will check and update lease after checking the rflag for 1233 * OPEN_CONFIRM in the successful OPEN call. 1234 */ 1235 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1236 1237 /* 1238 * XXX what if we're crossing mount points from server1:/drp 1239 * to server2:/drp/rp. 1240 */ 1241 1242 /* Signal our end of use of the open seqid */ 1243 nfs4_end_open_seqid_sync(oop); 1244 1245 /* 1246 * This will destroy the open owner if it was just created, 1247 * and no one else has put a reference on it. 1248 */ 1249 open_owner_rele(oop); 1250 if (create_flag && (createmode != EXCLUSIVE4) && 1251 res.status == NFS4ERR_BADOWNER) 1252 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1253 1254 e.error = geterrno4(res.status); 1255 nfs4args_copen_free(open_args); 1256 if (setgid_flag) { 1257 nfs4args_verify_free(&argop[8]); 1258 nfs4args_setattr_free(&argop[9]); 1259 } 1260 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1261 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1262 /* 1263 * If the reply is NFS4ERR_ACCESS, it may be because 1264 * we are root (no root net access). If the real uid 1265 * is not root, then retry with the real uid instead. 1266 */ 1267 if (ncr != NULL) { 1268 crfree(ncr); 1269 ncr = NULL; 1270 } 1271 if (res.status == NFS4ERR_ACCESS && 1272 (ncr = crnetadjust(cred_otw)) != NULL) { 1273 cred_otw = ncr; 1274 goto recov_retry; 1275 } 1276 kmem_free(argop, argoplist_size); 1277 return (e.error); 1278 } 1279 1280 resop = &res.array[idx_open]; /* open res */ 1281 op_res = &resop->nfs_resop4_u.opopen; 1282 1283 #ifdef DEBUG 1284 /* 1285 * verify attrset bitmap 1286 */ 1287 if (create_flag && 1288 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1289 /* make sure attrset returned is what we asked for */ 1290 /* XXX Ignore this 'error' for now */ 1291 if (attr->attrmask != op_res->attrset) 1292 /* EMPTY */; 1293 } 1294 #endif 1295 1296 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1297 mutex_enter(&VTOMI4(dvp)->mi_lock); 1298 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1299 mutex_exit(&VTOMI4(dvp)->mi_lock); 1300 } 1301 1302 resop = &res.array[idx_open + 1]; /* getfh res */ 1303 gf_res = &resop->nfs_resop4_u.opgetfh; 1304 1305 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1306 1307 /* 1308 * The open stateid has been updated on the server but not 1309 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1310 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1311 * WRITE call. That, however, will use the old stateid, so go ahead 1312 * and upate the open stateid now, before any call to makenfs4node. 1313 */ 1314 if (vpi) { 1315 nfs4_open_stream_t *tmp_osp; 1316 rnode4_t *tmp_rp = VTOR4(vpi); 1317 1318 tmp_osp = find_open_stream(oop, tmp_rp); 1319 if (tmp_osp) { 1320 tmp_osp->open_stateid = op_res->stateid; 1321 mutex_exit(&tmp_osp->os_sync_lock); 1322 open_stream_rele(tmp_osp, tmp_rp); 1323 } 1324 1325 /* 1326 * We must determine if the file handle given by the otw open 1327 * is the same as the file handle which was passed in with 1328 * *vpp. This case can be reached if the file we are trying 1329 * to open has been removed and another file has been created 1330 * having the same file name. The passed in vnode is released 1331 * later. 1332 */ 1333 orig_sfh = VTOR4(vpi)->r_fh; 1334 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1335 } 1336 1337 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1338 1339 if (create_flag || fh_differs) { 1340 int rnode_err = 0; 1341 1342 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1343 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh)); 1344 1345 if (e.error) 1346 PURGE_ATTRCACHE4(vp); 1347 /* 1348 * For the newly created vp case, make sure the rnode 1349 * isn't bad before using it. 1350 */ 1351 mutex_enter(&(VTOR4(vp))->r_statelock); 1352 if (VTOR4(vp)->r_flags & R4RECOVERR) 1353 rnode_err = EIO; 1354 mutex_exit(&(VTOR4(vp))->r_statelock); 1355 1356 if (rnode_err) { 1357 nfs4_end_open_seqid_sync(oop); 1358 nfs4args_copen_free(open_args); 1359 if (setgid_flag) { 1360 nfs4args_verify_free(&argop[8]); 1361 nfs4args_setattr_free(&argop[9]); 1362 } 1363 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1364 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1365 needrecov); 1366 open_owner_rele(oop); 1367 VN_RELE(vp); 1368 if (ncr != NULL) 1369 crfree(ncr); 1370 sfh4_rele(&otw_sfh); 1371 kmem_free(argop, argoplist_size); 1372 return (EIO); 1373 } 1374 } else { 1375 vp = vpi; 1376 } 1377 sfh4_rele(&otw_sfh); 1378 1379 /* 1380 * It seems odd to get a full set of attrs and then not update 1381 * the object's attrcache in the non-create case. Create case uses 1382 * the attrs since makenfs4node checks to see if the attrs need to 1383 * be updated (and then updates them). The non-create case should 1384 * update attrs also. 1385 */ 1386 if (! create_flag && ! fh_differs && !e.error) { 1387 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1388 } 1389 1390 nfs4_error_zinit(&e); 1391 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1392 /* This does not do recovery for vp explicitly. */ 1393 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1394 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1395 1396 if (e.error || e.stat) { 1397 nfs4_end_open_seqid_sync(oop); 1398 nfs4args_copen_free(open_args); 1399 if (setgid_flag) { 1400 nfs4args_verify_free(&argop[8]); 1401 nfs4args_setattr_free(&argop[9]); 1402 } 1403 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1404 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1405 needrecov); 1406 open_owner_rele(oop); 1407 if (create_flag || fh_differs) { 1408 /* rele the makenfs4node */ 1409 VN_RELE(vp); 1410 } 1411 if (ncr != NULL) { 1412 crfree(ncr); 1413 ncr = NULL; 1414 } 1415 if (retry_open == TRUE) { 1416 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1417 "nfs4open_otw: retry the open since OPEN " 1418 "CONFIRM failed with error %d stat %d", 1419 e.error, e.stat)); 1420 if (create_flag && createmode == GUARDED4) { 1421 NFS4_DEBUG(nfs4_client_recov_debug, 1422 (CE_NOTE, "nfs4open_otw: switch " 1423 "createmode from GUARDED4 to " 1424 "UNCHECKED4")); 1425 createmode = UNCHECKED4; 1426 } 1427 goto recov_retry; 1428 } 1429 if (!e.error) { 1430 if (create_flag && (createmode != EXCLUSIVE4) && 1431 e.stat == NFS4ERR_BADOWNER) 1432 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1433 1434 e.error = geterrno4(e.stat); 1435 } 1436 kmem_free(argop, argoplist_size); 1437 return (e.error); 1438 } 1439 } 1440 1441 rp = VTOR4(vp); 1442 1443 mutex_enter(&rp->r_statev4_lock); 1444 if (create_flag) 1445 rp->created_v4 = 1; 1446 mutex_exit(&rp->r_statev4_lock); 1447 1448 mutex_enter(&oop->oo_lock); 1449 /* Doesn't matter if 'oo_just_created' already was set as this */ 1450 oop->oo_just_created = NFS4_PERM_CREATED; 1451 if (oop->oo_cred_otw) 1452 crfree(oop->oo_cred_otw); 1453 oop->oo_cred_otw = cred_otw; 1454 crhold(oop->oo_cred_otw); 1455 mutex_exit(&oop->oo_lock); 1456 1457 /* returns with 'os_sync_lock' held */ 1458 osp = find_or_create_open_stream(oop, rp, &created_osp); 1459 if (!osp) { 1460 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1461 "nfs4open_otw: failed to create an open stream")); 1462 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1463 "signal our end of use of the open seqid")); 1464 1465 nfs4_end_open_seqid_sync(oop); 1466 open_owner_rele(oop); 1467 nfs4args_copen_free(open_args); 1468 if (setgid_flag) { 1469 nfs4args_verify_free(&argop[8]); 1470 nfs4args_setattr_free(&argop[9]); 1471 } 1472 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1473 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1474 if (create_flag || fh_differs) 1475 VN_RELE(vp); 1476 if (ncr != NULL) 1477 crfree(ncr); 1478 1479 kmem_free(argop, argoplist_size); 1480 return (EINVAL); 1481 1482 } 1483 1484 osp->open_stateid = op_res->stateid; 1485 1486 if (open_flag & FREAD) 1487 osp->os_share_acc_read++; 1488 if (open_flag & FWRITE) 1489 osp->os_share_acc_write++; 1490 osp->os_share_deny_none++; 1491 1492 /* 1493 * Need to reset this bitfield for the possible case where we were 1494 * going to OTW CLOSE the file, got a non-recoverable error, and before 1495 * we could retry the CLOSE, OPENed the file again. 1496 */ 1497 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1498 osp->os_final_close = 0; 1499 osp->os_force_close = 0; 1500 #ifdef DEBUG 1501 if (osp->os_failed_reopen) 1502 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1503 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1504 (void *)osp, (void *)cr, rnode4info(rp))); 1505 #endif 1506 osp->os_failed_reopen = 0; 1507 1508 mutex_exit(&osp->os_sync_lock); 1509 1510 nfs4_end_open_seqid_sync(oop); 1511 1512 if (created_osp && recov_state.rs_sp != NULL) { 1513 mutex_enter(&recov_state.rs_sp->s_lock); 1514 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1515 mutex_exit(&recov_state.rs_sp->s_lock); 1516 } 1517 1518 /* get rid of our reference to find oop */ 1519 open_owner_rele(oop); 1520 1521 open_stream_rele(osp, rp); 1522 1523 /* accept delegation, if any */ 1524 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1525 1526 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1527 1528 if (createmode == EXCLUSIVE4 && 1529 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1530 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1531 " EXCLUSIVE4: sending a SETATTR")); 1532 /* 1533 * If doing an exclusive create, then generate 1534 * a SETATTR to set the initial attributes. 1535 * Try to set the mtime and the atime to the 1536 * server's current time. It is somewhat 1537 * expected that these fields will be used to 1538 * store the exclusive create cookie. If not, 1539 * server implementors will need to know that 1540 * a SETATTR will follow an exclusive create 1541 * and the cookie should be destroyed if 1542 * appropriate. 1543 * 1544 * The AT_GID and AT_SIZE bits are turned off 1545 * so that the SETATTR request will not attempt 1546 * to process these. The gid will be set 1547 * separately if appropriate. The size is turned 1548 * off because it is assumed that a new file will 1549 * be created empty and if the file wasn't empty, 1550 * then the exclusive create will have failed 1551 * because the file must have existed already. 1552 * Therefore, no truncate operation is needed. 1553 */ 1554 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1555 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1556 1557 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1558 if (e.error) { 1559 /* 1560 * Couldn't correct the attributes of 1561 * the newly created file and the 1562 * attributes are wrong. Remove the 1563 * file and return an error to the 1564 * application. 1565 */ 1566 /* XXX will this take care of client state ? */ 1567 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1568 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1569 " remove file", e.error)); 1570 VN_RELE(vp); 1571 (void) nfs4_remove(dvp, file_name, cr, NULL, 0); 1572 /* 1573 * Since we've reled the vnode and removed 1574 * the file we now need to return the error. 1575 * At this point we don't want to update the 1576 * dircaches, call nfs4_waitfor_purge_complete 1577 * or set vpp to vp so we need to skip these 1578 * as well. 1579 */ 1580 goto skip_update_dircaches; 1581 } 1582 } 1583 1584 /* 1585 * If we created or found the correct vnode, due to create_flag or 1586 * fh_differs being set, then update directory cache attribute, readdir 1587 * and dnlc caches. 1588 */ 1589 if (create_flag || fh_differs) { 1590 dirattr_info_t dinfo, *dinfop; 1591 1592 /* 1593 * Make sure getattr succeeded before using results. 1594 * note: op 7 is getattr(dir) for both flavors of 1595 * open(create). 1596 */ 1597 if (create_flag && res.status == NFS4_OK) { 1598 dinfo.di_time_call = t; 1599 dinfo.di_cred = cr; 1600 dinfo.di_garp = 1601 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1602 dinfop = &dinfo; 1603 } else { 1604 dinfop = NULL; 1605 } 1606 1607 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1608 dinfop); 1609 } 1610 1611 /* 1612 * If the page cache for this file was flushed from actions 1613 * above, it was done asynchronously and if that is true, 1614 * there is a need to wait here for it to complete. This must 1615 * be done outside of start_fop/end_fop. 1616 */ 1617 (void) nfs4_waitfor_purge_complete(vp); 1618 1619 /* 1620 * It is implicit that we are in the open case (create_flag == 0) since 1621 * fh_differs can only be set to a non-zero value in the open case. 1622 */ 1623 if (fh_differs != 0 && vpi != NULL) 1624 VN_RELE(vpi); 1625 1626 /* 1627 * Be sure to set *vpp to the correct value before returning. 1628 */ 1629 *vpp = vp; 1630 1631 skip_update_dircaches: 1632 1633 nfs4args_copen_free(open_args); 1634 if (setgid_flag) { 1635 nfs4args_verify_free(&argop[8]); 1636 nfs4args_setattr_free(&argop[9]); 1637 } 1638 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1639 1640 if (ncr) 1641 crfree(ncr); 1642 kmem_free(argop, argoplist_size); 1643 return (e.error); 1644 } 1645 1646 /* 1647 * Reopen an open instance. cf. nfs4open_otw(). 1648 * 1649 * Errors are returned by the nfs4_error_t parameter. 1650 * - ep->error contains an errno value or zero. 1651 * - if it is zero, ep->stat is set to an NFS status code, if any. 1652 * If the file could not be reopened, but the caller should continue, the 1653 * file is marked dead and no error values are returned. If the caller 1654 * should stop recovering open files and start over, either the ep->error 1655 * value or ep->stat will indicate an error (either something that requires 1656 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1657 * filehandles) may be handled silently by this routine. 1658 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1659 * will be started, so the caller should not do it. 1660 * 1661 * Gotos: 1662 * - kill_file : reopen failed in such a fashion to constitute marking the 1663 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1664 * is for cases where recovery is not possible. 1665 * - failed_reopen : same as above, except that the file has already been 1666 * marked dead, so no need to do it again. 1667 * - bailout : reopen failed but we are able to recover and retry the reopen - 1668 * either within this function immediately or via the calling function. 1669 */ 1670 1671 void 1672 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1673 open_claim_type4 claim, bool_t frc_use_claim_previous, 1674 bool_t is_recov) 1675 { 1676 COMPOUND4args_clnt args; 1677 COMPOUND4res_clnt res; 1678 nfs_argop4 argop[4]; 1679 nfs_resop4 *resop; 1680 OPEN4res *op_res = NULL; 1681 OPEN4cargs *open_args; 1682 GETFH4res *gf_res; 1683 rnode4_t *rp = VTOR4(vp); 1684 int doqueue = 1; 1685 cred_t *cr = NULL, *cred_otw = NULL; 1686 nfs4_open_owner_t *oop = NULL; 1687 seqid4 seqid; 1688 nfs4_ga_res_t *garp; 1689 char fn[MAXNAMELEN]; 1690 nfs4_recov_state_t recov = {NULL, 0}; 1691 nfs4_lost_rqst_t lost_rqst; 1692 mntinfo4_t *mi = VTOMI4(vp); 1693 bool_t abort; 1694 char *failed_msg = ""; 1695 int fh_different; 1696 hrtime_t t; 1697 nfs4_bseqid_entry_t *bsep = NULL; 1698 1699 ASSERT(nfs4_consistent_type(vp)); 1700 ASSERT(nfs_zone() == mi->mi_zone); 1701 1702 nfs4_error_zinit(ep); 1703 1704 /* this is the cred used to find the open owner */ 1705 cr = state_to_cred(osp); 1706 if (cr == NULL) { 1707 failed_msg = "Couldn't reopen: no cred"; 1708 goto kill_file; 1709 } 1710 /* use this cred for OTW operations */ 1711 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1712 1713 top: 1714 nfs4_error_zinit(ep); 1715 1716 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1717 /* File system has been unmounted, quit */ 1718 ep->error = EIO; 1719 failed_msg = "Couldn't reopen: file system has been unmounted"; 1720 goto kill_file; 1721 } 1722 1723 oop = osp->os_open_owner; 1724 1725 ASSERT(oop != NULL); 1726 if (oop == NULL) { /* be defensive in non-DEBUG */ 1727 failed_msg = "can't reopen: no open owner"; 1728 goto kill_file; 1729 } 1730 open_owner_hold(oop); 1731 1732 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1733 if (ep->error) { 1734 open_owner_rele(oop); 1735 oop = NULL; 1736 goto bailout; 1737 } 1738 1739 /* 1740 * If the rnode has a delegation and the delegation has been 1741 * recovered and the server didn't request a recall and the caller 1742 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1743 * recovery) and the rnode hasn't been marked dead, then install 1744 * the delegation stateid in the open stream. Otherwise, proceed 1745 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1746 */ 1747 mutex_enter(&rp->r_statev4_lock); 1748 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1749 !rp->r_deleg_return_pending && 1750 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1751 !rp->r_deleg_needs_recall && 1752 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1753 !(rp->r_flags & R4RECOVERR)) { 1754 mutex_enter(&osp->os_sync_lock); 1755 osp->os_delegation = 1; 1756 osp->open_stateid = rp->r_deleg_stateid; 1757 mutex_exit(&osp->os_sync_lock); 1758 mutex_exit(&rp->r_statev4_lock); 1759 goto bailout; 1760 } 1761 mutex_exit(&rp->r_statev4_lock); 1762 1763 /* 1764 * If the file failed recovery, just quit. This failure need not 1765 * affect other reopens, so don't return an error. 1766 */ 1767 mutex_enter(&rp->r_statelock); 1768 if (rp->r_flags & R4RECOVERR) { 1769 mutex_exit(&rp->r_statelock); 1770 ep->error = 0; 1771 goto failed_reopen; 1772 } 1773 mutex_exit(&rp->r_statelock); 1774 1775 /* 1776 * argop is empty here 1777 * 1778 * PUTFH, OPEN, GETATTR 1779 */ 1780 args.ctag = TAG_REOPEN; 1781 args.array_len = 4; 1782 args.array = argop; 1783 1784 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1785 "nfs4_reopen: file is type %d, id %s", 1786 vp->v_type, rnode4info(VTOR4(vp)))); 1787 1788 argop[0].argop = OP_CPUTFH; 1789 1790 if (claim != CLAIM_PREVIOUS) { 1791 /* 1792 * if this is a file mount then 1793 * use the mntinfo parentfh 1794 */ 1795 argop[0].nfs_argop4_u.opcputfh.sfh = 1796 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1797 VTOSV(vp)->sv_dfh; 1798 } else { 1799 /* putfh fh to reopen */ 1800 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1801 } 1802 1803 argop[1].argop = OP_COPEN; 1804 open_args = &argop[1].nfs_argop4_u.opcopen; 1805 open_args->claim = claim; 1806 1807 if (claim == CLAIM_NULL) { 1808 1809 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1810 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1811 "failed for vp 0x%p for CLAIM_NULL with %m", 1812 (void *)vp); 1813 failed_msg = "Couldn't reopen: vtoname failed for " 1814 "CLAIM_NULL"; 1815 /* nothing allocated yet */ 1816 goto kill_file; 1817 } 1818 1819 open_args->open_claim4_u.cfile = fn; 1820 } else if (claim == CLAIM_PREVIOUS) { 1821 1822 /* 1823 * We have two cases to deal with here: 1824 * 1) We're being called to reopen files in order to satisfy 1825 * a lock operation request which requires us to explicitly 1826 * reopen files which were opened under a delegation. If 1827 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1828 * that case, frc_use_claim_previous is TRUE and we must 1829 * use the rnode's current delegation type (r_deleg_type). 1830 * 2) We're reopening files during some form of recovery. 1831 * In this case, frc_use_claim_previous is FALSE and we 1832 * use the delegation type appropriate for recovery 1833 * (r_deleg_needs_recovery). 1834 */ 1835 mutex_enter(&rp->r_statev4_lock); 1836 open_args->open_claim4_u.delegate_type = 1837 frc_use_claim_previous ? 1838 rp->r_deleg_type : 1839 rp->r_deleg_needs_recovery; 1840 mutex_exit(&rp->r_statev4_lock); 1841 1842 } else if (claim == CLAIM_DELEGATE_CUR) { 1843 1844 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1845 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1846 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1847 "with %m", (void *)vp); 1848 failed_msg = "Couldn't reopen: vtoname failed for " 1849 "CLAIM_DELEGATE_CUR"; 1850 /* nothing allocated yet */ 1851 goto kill_file; 1852 } 1853 1854 mutex_enter(&rp->r_statev4_lock); 1855 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1856 rp->r_deleg_stateid; 1857 mutex_exit(&rp->r_statev4_lock); 1858 1859 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1860 } 1861 open_args->opentype = OPEN4_NOCREATE; 1862 open_args->owner.clientid = mi2clientid(mi); 1863 open_args->owner.owner_len = sizeof (oop->oo_name); 1864 open_args->owner.owner_val = 1865 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1866 bcopy(&oop->oo_name, open_args->owner.owner_val, 1867 open_args->owner.owner_len); 1868 open_args->share_access = 0; 1869 open_args->share_deny = 0; 1870 1871 mutex_enter(&osp->os_sync_lock); 1872 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1873 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1874 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1875 (void *)osp, (void *)rp, osp->os_share_acc_read, 1876 osp->os_share_acc_write, osp->os_open_ref_count, 1877 osp->os_mmap_read, osp->os_mmap_write, claim)); 1878 1879 if (osp->os_share_acc_read || osp->os_mmap_read) 1880 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1881 if (osp->os_share_acc_write || osp->os_mmap_write) 1882 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1883 if (osp->os_share_deny_read) 1884 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1885 if (osp->os_share_deny_write) 1886 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1887 mutex_exit(&osp->os_sync_lock); 1888 1889 seqid = nfs4_get_open_seqid(oop) + 1; 1890 open_args->seqid = seqid; 1891 1892 /* Construct the getfh part of the compound */ 1893 argop[2].argop = OP_GETFH; 1894 1895 /* Construct the getattr part of the compound */ 1896 argop[3].argop = OP_GETATTR; 1897 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1898 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1899 1900 t = gethrtime(); 1901 1902 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1903 1904 if (ep->error) { 1905 if (!is_recov && !frc_use_claim_previous && 1906 (ep->error == EINTR || ep->error == ETIMEDOUT || 1907 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1908 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1909 cred_otw, vp, NULL, open_args); 1910 abort = nfs4_start_recovery(ep, 1911 VTOMI4(vp), vp, NULL, NULL, 1912 lost_rqst.lr_op == OP_OPEN ? 1913 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL); 1914 nfs4args_copen_free(open_args); 1915 goto bailout; 1916 } 1917 1918 nfs4args_copen_free(open_args); 1919 1920 if (ep->error == EACCES && cred_otw != cr) { 1921 crfree(cred_otw); 1922 cred_otw = cr; 1923 crhold(cred_otw); 1924 nfs4_end_open_seqid_sync(oop); 1925 open_owner_rele(oop); 1926 oop = NULL; 1927 goto top; 1928 } 1929 if (ep->error == ETIMEDOUT) 1930 goto bailout; 1931 failed_msg = "Couldn't reopen: rpc error"; 1932 goto kill_file; 1933 } 1934 1935 if (nfs4_need_to_bump_seqid(&res)) 1936 nfs4_set_open_seqid(seqid, oop, args.ctag); 1937 1938 switch (res.status) { 1939 case NFS4_OK: 1940 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1941 mutex_enter(&rp->r_statelock); 1942 rp->r_delay_interval = 0; 1943 mutex_exit(&rp->r_statelock); 1944 } 1945 break; 1946 case NFS4ERR_BAD_SEQID: 1947 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1948 args.ctag, open_args->seqid); 1949 1950 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1951 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1952 NULL, OP_OPEN, bsep, NULL, NULL); 1953 1954 nfs4args_copen_free(open_args); 1955 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1956 nfs4_end_open_seqid_sync(oop); 1957 open_owner_rele(oop); 1958 oop = NULL; 1959 kmem_free(bsep, sizeof (*bsep)); 1960 1961 goto kill_file; 1962 case NFS4ERR_NO_GRACE: 1963 nfs4args_copen_free(open_args); 1964 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1965 nfs4_end_open_seqid_sync(oop); 1966 open_owner_rele(oop); 1967 oop = NULL; 1968 if (claim == CLAIM_PREVIOUS) { 1969 /* 1970 * Retry as a plain open. We don't need to worry about 1971 * checking the changeinfo: it is acceptable for a 1972 * client to re-open a file and continue processing 1973 * (in the absence of locks). 1974 */ 1975 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1976 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1977 "will retry as CLAIM_NULL")); 1978 claim = CLAIM_NULL; 1979 nfs4_mi_kstat_inc_no_grace(mi); 1980 goto top; 1981 } 1982 failed_msg = 1983 "Couldn't reopen: tried reclaim outside grace period. "; 1984 goto kill_file; 1985 case NFS4ERR_GRACE: 1986 nfs4_set_grace_wait(mi); 1987 nfs4args_copen_free(open_args); 1988 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1989 nfs4_end_open_seqid_sync(oop); 1990 open_owner_rele(oop); 1991 oop = NULL; 1992 ep->error = nfs4_wait_for_grace(mi, &recov); 1993 if (ep->error != 0) 1994 goto bailout; 1995 goto top; 1996 case NFS4ERR_DELAY: 1997 nfs4_set_delay_wait(vp); 1998 nfs4args_copen_free(open_args); 1999 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2000 nfs4_end_open_seqid_sync(oop); 2001 open_owner_rele(oop); 2002 oop = NULL; 2003 ep->error = nfs4_wait_for_delay(vp, &recov); 2004 nfs4_mi_kstat_inc_delay(mi); 2005 if (ep->error != 0) 2006 goto bailout; 2007 goto top; 2008 case NFS4ERR_FHEXPIRED: 2009 /* recover filehandle and retry */ 2010 abort = nfs4_start_recovery(ep, 2011 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL); 2012 nfs4args_copen_free(open_args); 2013 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2014 nfs4_end_open_seqid_sync(oop); 2015 open_owner_rele(oop); 2016 oop = NULL; 2017 if (abort == FALSE) 2018 goto top; 2019 failed_msg = "Couldn't reopen: recovery aborted"; 2020 goto kill_file; 2021 case NFS4ERR_RESOURCE: 2022 case NFS4ERR_STALE_CLIENTID: 2023 case NFS4ERR_WRONGSEC: 2024 case NFS4ERR_EXPIRED: 2025 /* 2026 * Do not mark the file dead and let the calling 2027 * function initiate recovery. 2028 */ 2029 nfs4args_copen_free(open_args); 2030 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2031 nfs4_end_open_seqid_sync(oop); 2032 open_owner_rele(oop); 2033 oop = NULL; 2034 goto bailout; 2035 case NFS4ERR_ACCESS: 2036 if (cred_otw != cr) { 2037 crfree(cred_otw); 2038 cred_otw = cr; 2039 crhold(cred_otw); 2040 nfs4args_copen_free(open_args); 2041 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2042 nfs4_end_open_seqid_sync(oop); 2043 open_owner_rele(oop); 2044 oop = NULL; 2045 goto top; 2046 } 2047 /* fall through */ 2048 default: 2049 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2050 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2051 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2052 rnode4info(VTOR4(vp)))); 2053 failed_msg = "Couldn't reopen: NFSv4 error"; 2054 nfs4args_copen_free(open_args); 2055 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2056 goto kill_file; 2057 } 2058 2059 resop = &res.array[1]; /* open res */ 2060 op_res = &resop->nfs_resop4_u.opopen; 2061 2062 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2063 2064 /* 2065 * Check if the path we reopened really is the same 2066 * file. We could end up in a situation where the file 2067 * was removed and a new file created with the same name. 2068 */ 2069 resop = &res.array[2]; 2070 gf_res = &resop->nfs_resop4_u.opgetfh; 2071 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2072 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2073 if (fh_different) { 2074 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2075 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2076 /* Oops, we don't have the same file */ 2077 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2078 failed_msg = "Couldn't reopen: Persistent " 2079 "file handle changed"; 2080 else 2081 failed_msg = "Couldn't reopen: Volatile " 2082 "(no expire on open) file handle changed"; 2083 2084 nfs4args_copen_free(open_args); 2085 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2086 nfs_rw_exit(&mi->mi_fh_lock); 2087 goto kill_file; 2088 2089 } else { 2090 /* 2091 * We have volatile file handles that don't compare. 2092 * If the fids are the same then we assume that the 2093 * file handle expired but the rnode still refers to 2094 * the same file object. 2095 * 2096 * First check that we have fids or not. 2097 * If we don't we have a dumb server so we will 2098 * just assume every thing is ok for now. 2099 */ 2100 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2101 rp->r_attr.va_mask & AT_NODEID && 2102 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2103 /* 2104 * We have fids, but they don't 2105 * compare. So kill the file. 2106 */ 2107 failed_msg = 2108 "Couldn't reopen: file handle changed" 2109 " due to mismatched fids"; 2110 nfs4args_copen_free(open_args); 2111 (void) xdr_free(xdr_COMPOUND4res_clnt, 2112 (caddr_t)&res); 2113 nfs_rw_exit(&mi->mi_fh_lock); 2114 goto kill_file; 2115 } else { 2116 /* 2117 * We have volatile file handles that refers 2118 * to the same file (at least they have the 2119 * same fid) or we don't have fids so we 2120 * can't tell. :(. We'll be a kind and accepting 2121 * client so we'll update the rnode's file 2122 * handle with the otw handle. 2123 * 2124 * We need to drop mi->mi_fh_lock since 2125 * sh4_update acquires it. Since there is 2126 * only one recovery thread there is no 2127 * race. 2128 */ 2129 nfs_rw_exit(&mi->mi_fh_lock); 2130 sfh4_update(rp->r_fh, &gf_res->object); 2131 } 2132 } 2133 } else { 2134 nfs_rw_exit(&mi->mi_fh_lock); 2135 } 2136 2137 ASSERT(nfs4_consistent_type(vp)); 2138 2139 /* 2140 * If the server wanted an OPEN_CONFIRM but that fails, just start 2141 * over. Presumably if there is a persistent error it will show up 2142 * when we resend the OPEN. 2143 */ 2144 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2145 bool_t retry_open = FALSE; 2146 2147 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2148 cred_otw, is_recov, &retry_open, 2149 oop, FALSE, ep, NULL); 2150 if (ep->error || ep->stat) { 2151 nfs4args_copen_free(open_args); 2152 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2153 nfs4_end_open_seqid_sync(oop); 2154 open_owner_rele(oop); 2155 oop = NULL; 2156 goto top; 2157 } 2158 } 2159 2160 mutex_enter(&osp->os_sync_lock); 2161 osp->open_stateid = op_res->stateid; 2162 osp->os_delegation = 0; 2163 /* 2164 * Need to reset this bitfield for the possible case where we were 2165 * going to OTW CLOSE the file, got a non-recoverable error, and before 2166 * we could retry the CLOSE, OPENed the file again. 2167 */ 2168 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2169 osp->os_final_close = 0; 2170 osp->os_force_close = 0; 2171 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2172 osp->os_dc_openacc = open_args->share_access; 2173 mutex_exit(&osp->os_sync_lock); 2174 2175 nfs4_end_open_seqid_sync(oop); 2176 2177 /* accept delegation, if any */ 2178 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2179 2180 nfs4args_copen_free(open_args); 2181 2182 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2183 2184 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2185 2186 ASSERT(nfs4_consistent_type(vp)); 2187 2188 open_owner_rele(oop); 2189 crfree(cr); 2190 crfree(cred_otw); 2191 return; 2192 2193 kill_file: 2194 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2195 failed_reopen: 2196 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2197 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2198 (void *)osp, (void *)cr, rnode4info(rp))); 2199 mutex_enter(&osp->os_sync_lock); 2200 osp->os_failed_reopen = 1; 2201 mutex_exit(&osp->os_sync_lock); 2202 bailout: 2203 if (oop != NULL) { 2204 nfs4_end_open_seqid_sync(oop); 2205 open_owner_rele(oop); 2206 } 2207 if (cr != NULL) 2208 crfree(cr); 2209 if (cred_otw != NULL) 2210 crfree(cred_otw); 2211 } 2212 2213 /* for . and .. OPENs */ 2214 /* ARGSUSED */ 2215 static int 2216 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2217 { 2218 rnode4_t *rp; 2219 nfs4_ga_res_t gar; 2220 2221 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2222 2223 /* 2224 * If close-to-open consistency checking is turned off or 2225 * if there is no cached data, we can avoid 2226 * the over the wire getattr. Otherwise, force a 2227 * call to the server to get fresh attributes and to 2228 * check caches. This is required for close-to-open 2229 * consistency. 2230 */ 2231 rp = VTOR4(*vpp); 2232 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2233 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2234 return (0); 2235 2236 gar.n4g_va.va_mask = AT_ALL; 2237 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2238 } 2239 2240 /* 2241 * CLOSE a file 2242 */ 2243 /* ARGSUSED */ 2244 static int 2245 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2246 caller_context_t *ct) 2247 { 2248 rnode4_t *rp; 2249 int error = 0; 2250 int r_error = 0; 2251 int n4error = 0; 2252 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2253 2254 /* 2255 * Remove client state for this (lockowner, file) pair. 2256 * Issue otw v4 call to have the server do the same. 2257 */ 2258 2259 rp = VTOR4(vp); 2260 2261 /* 2262 * zone_enter(2) prevents processes from changing zones with NFS files 2263 * open; if we happen to get here from the wrong zone we can't do 2264 * anything over the wire. 2265 */ 2266 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2267 /* 2268 * We could attempt to clean up locks, except we're sure 2269 * that the current process didn't acquire any locks on 2270 * the file: any attempt to lock a file belong to another zone 2271 * will fail, and one can't lock an NFS file and then change 2272 * zones, as that fails too. 2273 * 2274 * Returning an error here is the sane thing to do. A 2275 * subsequent call to VN_RELE() which translates to a 2276 * nfs4_inactive() will clean up state: if the zone of the 2277 * vnode's origin is still alive and kicking, the inactive 2278 * thread will handle the request (from the correct zone), and 2279 * everything (minus the OTW close call) should be OK. If the 2280 * zone is going away nfs4_async_inactive() will throw away 2281 * delegations, open streams and cached pages inline. 2282 */ 2283 return (EIO); 2284 } 2285 2286 /* 2287 * If we are using local locking for this filesystem, then 2288 * release all of the SYSV style record locks. Otherwise, 2289 * we are doing network locking and we need to release all 2290 * of the network locks. All of the locks held by this 2291 * process on this file are released no matter what the 2292 * incoming reference count is. 2293 */ 2294 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2295 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2296 cleanshares(vp, ttoproc(curthread)->p_pid); 2297 } else 2298 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2299 2300 if (e.error) { 2301 struct lm_sysid *lmsid; 2302 lmsid = nfs4_find_sysid(VTOMI4(vp)); 2303 if (lmsid == NULL) { 2304 DTRACE_PROBE2(unknown__sysid, int, e.error, 2305 vnode_t *, vp); 2306 } else { 2307 cleanlocks(vp, ttoproc(curthread)->p_pid, 2308 (lm_sysidt(lmsid) | LM_SYSID_CLIENT)); 2309 2310 lm_rel_sysid(lmsid); 2311 } 2312 return (e.error); 2313 } 2314 2315 if (count > 1) 2316 return (0); 2317 2318 /* 2319 * If the file has been `unlinked', then purge the 2320 * DNLC so that this vnode will get reycled quicker 2321 * and the .nfs* file on the server will get removed. 2322 */ 2323 if (rp->r_unldvp != NULL) 2324 dnlc_purge_vp(vp); 2325 2326 /* 2327 * If the file was open for write and there are pages, 2328 * do a synchronous flush and commit of all of the 2329 * dirty and uncommitted pages. 2330 */ 2331 ASSERT(!e.error); 2332 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2333 error = nfs4_putpage_commit(vp, 0, 0, cr); 2334 2335 mutex_enter(&rp->r_statelock); 2336 r_error = rp->r_error; 2337 rp->r_error = 0; 2338 mutex_exit(&rp->r_statelock); 2339 2340 /* 2341 * If this file type is one for which no explicit 'open' was 2342 * done, then bail now (ie. no need for protocol 'close'). If 2343 * there was an error w/the vm subsystem, return _that_ error, 2344 * otherwise, return any errors that may've been reported via 2345 * the rnode. 2346 */ 2347 if (vp->v_type != VREG) 2348 return (error ? error : r_error); 2349 2350 /* 2351 * The sync putpage commit may have failed above, but since 2352 * we're working w/a regular file, we need to do the protocol 2353 * 'close' (nfs4close_one will figure out if an otw close is 2354 * needed or not). Report any errors _after_ doing the protocol 2355 * 'close'. 2356 */ 2357 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2358 n4error = e.error ? e.error : geterrno4(e.stat); 2359 2360 /* 2361 * Error reporting prio (Hi -> Lo) 2362 * 2363 * i) nfs4_putpage_commit (error) 2364 * ii) rnode's (r_error) 2365 * iii) nfs4close_one (n4error) 2366 */ 2367 return (error ? error : (r_error ? r_error : n4error)); 2368 } 2369 2370 /* 2371 * Initialize *lost_rqstp. 2372 */ 2373 2374 static void 2375 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2376 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2377 vnode_t *vp) 2378 { 2379 if (error != ETIMEDOUT && error != EINTR && 2380 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2381 lost_rqstp->lr_op = 0; 2382 return; 2383 } 2384 2385 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2386 "nfs4close_save_lost_rqst: error %d", error)); 2387 2388 lost_rqstp->lr_op = OP_CLOSE; 2389 /* 2390 * The vp is held and rele'd via the recovery code. 2391 * See nfs4_save_lost_rqst. 2392 */ 2393 lost_rqstp->lr_vp = vp; 2394 lost_rqstp->lr_dvp = NULL; 2395 lost_rqstp->lr_oop = oop; 2396 lost_rqstp->lr_osp = osp; 2397 ASSERT(osp != NULL); 2398 ASSERT(mutex_owned(&osp->os_sync_lock)); 2399 osp->os_pending_close = 1; 2400 lost_rqstp->lr_lop = NULL; 2401 lost_rqstp->lr_cr = cr; 2402 lost_rqstp->lr_flk = NULL; 2403 lost_rqstp->lr_putfirst = FALSE; 2404 } 2405 2406 /* 2407 * Assumes you already have the open seqid sync grabbed as well as the 2408 * 'os_sync_lock'. Note: this will release the open seqid sync and 2409 * 'os_sync_lock' if client recovery starts. Calling functions have to 2410 * be prepared to handle this. 2411 * 2412 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2413 * was needed and was started, and that the calling function should retry 2414 * this function; otherwise it is returned as 0. 2415 * 2416 * Errors are returned via the nfs4_error_t parameter. 2417 */ 2418 static void 2419 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2420 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2421 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2422 { 2423 COMPOUND4args_clnt args; 2424 COMPOUND4res_clnt res; 2425 CLOSE4args *close_args; 2426 nfs_resop4 *resop; 2427 nfs_argop4 argop[3]; 2428 int doqueue = 1; 2429 mntinfo4_t *mi; 2430 seqid4 seqid; 2431 vnode_t *vp; 2432 bool_t needrecov = FALSE; 2433 nfs4_lost_rqst_t lost_rqst; 2434 hrtime_t t; 2435 2436 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2437 2438 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2439 2440 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2441 2442 /* Only set this to 1 if recovery is started */ 2443 *recov = 0; 2444 2445 /* do the OTW call to close the file */ 2446 2447 if (close_type == CLOSE_RESEND) 2448 args.ctag = TAG_CLOSE_LOST; 2449 else if (close_type == CLOSE_AFTER_RESEND) 2450 args.ctag = TAG_CLOSE_UNDO; 2451 else 2452 args.ctag = TAG_CLOSE; 2453 2454 args.array_len = 3; 2455 args.array = argop; 2456 2457 vp = RTOV4(rp); 2458 2459 mi = VTOMI4(vp); 2460 2461 /* putfh target fh */ 2462 argop[0].argop = OP_CPUTFH; 2463 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2464 2465 argop[1].argop = OP_GETATTR; 2466 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2467 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2468 2469 argop[2].argop = OP_CLOSE; 2470 close_args = &argop[2].nfs_argop4_u.opclose; 2471 2472 seqid = nfs4_get_open_seqid(oop) + 1; 2473 2474 close_args->seqid = seqid; 2475 close_args->open_stateid = osp->open_stateid; 2476 2477 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2478 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2479 rnode4info(rp))); 2480 2481 t = gethrtime(); 2482 2483 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2484 2485 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2486 nfs4_set_open_seqid(seqid, oop, args.ctag); 2487 } 2488 2489 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2490 if (ep->error && !needrecov) { 2491 /* 2492 * if there was an error and no recovery is to be done 2493 * then then set up the file to flush its cache if 2494 * needed for the next caller. 2495 */ 2496 mutex_enter(&rp->r_statelock); 2497 PURGE_ATTRCACHE4_LOCKED(rp); 2498 rp->r_flags &= ~R4WRITEMODIFIED; 2499 mutex_exit(&rp->r_statelock); 2500 return; 2501 } 2502 2503 if (needrecov) { 2504 bool_t abort; 2505 nfs4_bseqid_entry_t *bsep = NULL; 2506 2507 if (close_type != CLOSE_RESEND) 2508 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2509 osp, cred_otw, vp); 2510 2511 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2512 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2513 0, args.ctag, close_args->seqid); 2514 2515 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2516 "nfs4close_otw: initiating recovery. error %d " 2517 "res.status %d", ep->error, res.status)); 2518 2519 /* 2520 * Drop the 'os_sync_lock' here so we don't hit 2521 * a potential recursive mutex_enter via an 2522 * 'open_stream_hold()'. 2523 */ 2524 mutex_exit(&osp->os_sync_lock); 2525 *have_sync_lockp = 0; 2526 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2527 (close_type != CLOSE_RESEND && 2528 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2529 OP_CLOSE, bsep, NULL, NULL); 2530 2531 /* drop open seq sync, and let the calling function regrab it */ 2532 nfs4_end_open_seqid_sync(oop); 2533 *did_start_seqid_syncp = 0; 2534 2535 if (bsep) 2536 kmem_free(bsep, sizeof (*bsep)); 2537 /* 2538 * For signals, the caller wants to quit, so don't say to 2539 * retry. For forced unmount, if it's a user thread, it 2540 * wants to quit. If it's a recovery thread, the retry 2541 * will happen higher-up on the call stack. Either way, 2542 * don't say to retry. 2543 */ 2544 if (abort == FALSE && ep->error != EINTR && 2545 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2546 close_type != CLOSE_RESEND && 2547 close_type != CLOSE_AFTER_RESEND) 2548 *recov = 1; 2549 else 2550 *recov = 0; 2551 2552 if (!ep->error) 2553 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2554 return; 2555 } 2556 2557 if (res.status) { 2558 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2559 return; 2560 } 2561 2562 mutex_enter(&rp->r_statev4_lock); 2563 rp->created_v4 = 0; 2564 mutex_exit(&rp->r_statev4_lock); 2565 2566 resop = &res.array[2]; 2567 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2568 osp->os_valid = 0; 2569 2570 /* 2571 * This removes the reference obtained at OPEN; ie, when the 2572 * open stream structure was created. 2573 * 2574 * We don't have to worry about calling 'open_stream_rele' 2575 * since we our currently holding a reference to the open 2576 * stream which means the count cannot go to 0 with this 2577 * decrement. 2578 */ 2579 ASSERT(osp->os_ref_count >= 2); 2580 osp->os_ref_count--; 2581 2582 if (ep->error == 0) { 2583 /* 2584 * Avoid a deadlock with the r_serial thread waiting for 2585 * os_sync_lock in nfs4_get_otw_cred_by_osp() which might be 2586 * held by us. We will wait in nfs4_attr_cache() for the 2587 * completion of the r_serial thread. 2588 */ 2589 mutex_exit(&osp->os_sync_lock); 2590 *have_sync_lockp = 0; 2591 2592 nfs4_attr_cache(vp, 2593 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2594 t, cred_otw, TRUE, NULL); 2595 } 2596 2597 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2598 " returning %d", ep->error)); 2599 2600 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2601 } 2602 2603 /* ARGSUSED */ 2604 static int 2605 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2606 caller_context_t *ct) 2607 { 2608 rnode4_t *rp; 2609 u_offset_t off; 2610 offset_t diff; 2611 uint_t on; 2612 uint_t n; 2613 caddr_t base; 2614 uint_t flags; 2615 int error; 2616 mntinfo4_t *mi; 2617 2618 rp = VTOR4(vp); 2619 2620 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2621 2622 if (IS_SHADOW(vp, rp)) 2623 vp = RTOV4(rp); 2624 2625 if (vp->v_type != VREG) 2626 return (EISDIR); 2627 2628 mi = VTOMI4(vp); 2629 2630 if (nfs_zone() != mi->mi_zone) 2631 return (EIO); 2632 2633 if (uiop->uio_resid == 0) 2634 return (0); 2635 2636 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2637 return (EINVAL); 2638 2639 mutex_enter(&rp->r_statelock); 2640 if (rp->r_flags & R4RECOVERRP) 2641 error = (rp->r_error ? rp->r_error : EIO); 2642 else 2643 error = 0; 2644 mutex_exit(&rp->r_statelock); 2645 if (error) 2646 return (error); 2647 2648 /* 2649 * Bypass VM if caching has been disabled (e.g., locking) or if 2650 * using client-side direct I/O and the file is not mmap'd and 2651 * there are no cached pages. 2652 */ 2653 if ((vp->v_flag & VNOCACHE) || 2654 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2655 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2656 size_t resid = 0; 2657 2658 return (nfs4read(vp, NULL, uiop->uio_loffset, 2659 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2660 } 2661 2662 error = 0; 2663 2664 do { 2665 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2666 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2667 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2668 2669 if (error = nfs4_validate_caches(vp, cr)) 2670 break; 2671 2672 mutex_enter(&rp->r_statelock); 2673 while (rp->r_flags & R4INCACHEPURGE) { 2674 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2675 mutex_exit(&rp->r_statelock); 2676 return (EINTR); 2677 } 2678 } 2679 diff = rp->r_size - uiop->uio_loffset; 2680 mutex_exit(&rp->r_statelock); 2681 if (diff <= 0) 2682 break; 2683 if (diff < n) 2684 n = (uint_t)diff; 2685 2686 if (vpm_enable) { 2687 /* 2688 * Copy data. 2689 */ 2690 error = vpm_data_copy(vp, off + on, n, uiop, 2691 1, NULL, 0, S_READ); 2692 } else { 2693 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2694 S_READ); 2695 2696 error = uiomove(base + on, n, UIO_READ, uiop); 2697 } 2698 2699 if (!error) { 2700 /* 2701 * If read a whole block or read to eof, 2702 * won't need this buffer again soon. 2703 */ 2704 mutex_enter(&rp->r_statelock); 2705 if (n + on == MAXBSIZE || 2706 uiop->uio_loffset == rp->r_size) 2707 flags = SM_DONTNEED; 2708 else 2709 flags = 0; 2710 mutex_exit(&rp->r_statelock); 2711 if (vpm_enable) { 2712 error = vpm_sync_pages(vp, off, n, flags); 2713 } else { 2714 error = segmap_release(segkmap, base, flags); 2715 } 2716 } else { 2717 if (vpm_enable) { 2718 (void) vpm_sync_pages(vp, off, n, 0); 2719 } else { 2720 (void) segmap_release(segkmap, base, 0); 2721 } 2722 } 2723 } while (!error && uiop->uio_resid > 0); 2724 2725 return (error); 2726 } 2727 2728 /* ARGSUSED */ 2729 static int 2730 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2731 caller_context_t *ct) 2732 { 2733 rlim64_t limit = uiop->uio_llimit; 2734 rnode4_t *rp; 2735 u_offset_t off; 2736 caddr_t base; 2737 uint_t flags; 2738 int remainder; 2739 size_t n; 2740 int on; 2741 int error; 2742 int resid; 2743 u_offset_t offset; 2744 mntinfo4_t *mi; 2745 uint_t bsize; 2746 2747 rp = VTOR4(vp); 2748 2749 if (IS_SHADOW(vp, rp)) 2750 vp = RTOV4(rp); 2751 2752 if (vp->v_type != VREG) 2753 return (EISDIR); 2754 2755 mi = VTOMI4(vp); 2756 2757 if (nfs_zone() != mi->mi_zone) 2758 return (EIO); 2759 2760 if (uiop->uio_resid == 0) 2761 return (0); 2762 2763 mutex_enter(&rp->r_statelock); 2764 if (rp->r_flags & R4RECOVERRP) 2765 error = (rp->r_error ? rp->r_error : EIO); 2766 else 2767 error = 0; 2768 mutex_exit(&rp->r_statelock); 2769 if (error) 2770 return (error); 2771 2772 if (ioflag & FAPPEND) { 2773 struct vattr va; 2774 2775 /* 2776 * Must serialize if appending. 2777 */ 2778 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2779 nfs_rw_exit(&rp->r_rwlock); 2780 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2781 INTR4(vp))) 2782 return (EINTR); 2783 } 2784 2785 va.va_mask = AT_SIZE; 2786 error = nfs4getattr(vp, &va, cr); 2787 if (error) 2788 return (error); 2789 uiop->uio_loffset = va.va_size; 2790 } 2791 2792 offset = uiop->uio_loffset + uiop->uio_resid; 2793 2794 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2795 return (EINVAL); 2796 2797 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2798 limit = MAXOFFSET_T; 2799 2800 /* 2801 * Check to make sure that the process will not exceed 2802 * its limit on file size. It is okay to write up to 2803 * the limit, but not beyond. Thus, the write which 2804 * reaches the limit will be short and the next write 2805 * will return an error. 2806 */ 2807 remainder = 0; 2808 if (offset > uiop->uio_llimit) { 2809 remainder = offset - uiop->uio_llimit; 2810 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2811 if (uiop->uio_resid <= 0) { 2812 proc_t *p = ttoproc(curthread); 2813 2814 uiop->uio_resid += remainder; 2815 mutex_enter(&p->p_lock); 2816 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2817 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2818 mutex_exit(&p->p_lock); 2819 return (EFBIG); 2820 } 2821 } 2822 2823 /* update the change attribute, if we have a write delegation */ 2824 2825 mutex_enter(&rp->r_statev4_lock); 2826 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2827 rp->r_deleg_change++; 2828 2829 mutex_exit(&rp->r_statev4_lock); 2830 2831 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2832 return (EINTR); 2833 2834 /* 2835 * Bypass VM if caching has been disabled (e.g., locking) or if 2836 * using client-side direct I/O and the file is not mmap'd and 2837 * there are no cached pages. 2838 */ 2839 if ((vp->v_flag & VNOCACHE) || 2840 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2841 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2842 size_t bufsize; 2843 int count; 2844 u_offset_t org_offset; 2845 stable_how4 stab_comm; 2846 nfs4_fwrite: 2847 if (rp->r_flags & R4STALE) { 2848 resid = uiop->uio_resid; 2849 offset = uiop->uio_loffset; 2850 error = rp->r_error; 2851 /* 2852 * A close may have cleared r_error, if so, 2853 * propagate ESTALE error return properly 2854 */ 2855 if (error == 0) 2856 error = ESTALE; 2857 goto bottom; 2858 } 2859 2860 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2861 base = kmem_alloc(bufsize, KM_SLEEP); 2862 do { 2863 if (ioflag & FDSYNC) 2864 stab_comm = DATA_SYNC4; 2865 else 2866 stab_comm = FILE_SYNC4; 2867 resid = uiop->uio_resid; 2868 offset = uiop->uio_loffset; 2869 count = MIN(uiop->uio_resid, bufsize); 2870 org_offset = uiop->uio_loffset; 2871 error = uiomove(base, count, UIO_WRITE, uiop); 2872 if (!error) { 2873 error = nfs4write(vp, base, org_offset, 2874 count, cr, &stab_comm); 2875 if (!error) { 2876 mutex_enter(&rp->r_statelock); 2877 if (rp->r_size < uiop->uio_loffset) 2878 rp->r_size = uiop->uio_loffset; 2879 mutex_exit(&rp->r_statelock); 2880 } 2881 } 2882 } while (!error && uiop->uio_resid > 0); 2883 kmem_free(base, bufsize); 2884 goto bottom; 2885 } 2886 2887 bsize = vp->v_vfsp->vfs_bsize; 2888 2889 do { 2890 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2891 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2892 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2893 2894 resid = uiop->uio_resid; 2895 offset = uiop->uio_loffset; 2896 2897 if (rp->r_flags & R4STALE) { 2898 error = rp->r_error; 2899 /* 2900 * A close may have cleared r_error, if so, 2901 * propagate ESTALE error return properly 2902 */ 2903 if (error == 0) 2904 error = ESTALE; 2905 break; 2906 } 2907 2908 /* 2909 * Don't create dirty pages faster than they 2910 * can be cleaned so that the system doesn't 2911 * get imbalanced. If the async queue is 2912 * maxed out, then wait for it to drain before 2913 * creating more dirty pages. Also, wait for 2914 * any threads doing pagewalks in the vop_getattr 2915 * entry points so that they don't block for 2916 * long periods. 2917 */ 2918 mutex_enter(&rp->r_statelock); 2919 while ((mi->mi_max_threads != 0 && 2920 rp->r_awcount > 2 * mi->mi_max_threads) || 2921 rp->r_gcount > 0) { 2922 if (INTR4(vp)) { 2923 klwp_t *lwp = ttolwp(curthread); 2924 2925 if (lwp != NULL) 2926 lwp->lwp_nostop++; 2927 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2928 mutex_exit(&rp->r_statelock); 2929 if (lwp != NULL) 2930 lwp->lwp_nostop--; 2931 error = EINTR; 2932 goto bottom; 2933 } 2934 if (lwp != NULL) 2935 lwp->lwp_nostop--; 2936 } else 2937 cv_wait(&rp->r_cv, &rp->r_statelock); 2938 } 2939 mutex_exit(&rp->r_statelock); 2940 2941 /* 2942 * Touch the page and fault it in if it is not in core 2943 * before segmap_getmapflt or vpm_data_copy can lock it. 2944 * This is to avoid the deadlock if the buffer is mapped 2945 * to the same file through mmap which we want to write. 2946 */ 2947 uio_prefaultpages((long)n, uiop); 2948 2949 if (vpm_enable) { 2950 /* 2951 * It will use kpm mappings, so no need to 2952 * pass an address. 2953 */ 2954 error = writerp4(rp, NULL, n, uiop, 0); 2955 } else { 2956 if (segmap_kpm) { 2957 int pon = uiop->uio_loffset & PAGEOFFSET; 2958 size_t pn = MIN(PAGESIZE - pon, 2959 uiop->uio_resid); 2960 int pagecreate; 2961 2962 mutex_enter(&rp->r_statelock); 2963 pagecreate = (pon == 0) && (pn == PAGESIZE || 2964 uiop->uio_loffset + pn >= rp->r_size); 2965 mutex_exit(&rp->r_statelock); 2966 2967 base = segmap_getmapflt(segkmap, vp, off + on, 2968 pn, !pagecreate, S_WRITE); 2969 2970 error = writerp4(rp, base + pon, n, uiop, 2971 pagecreate); 2972 2973 } else { 2974 base = segmap_getmapflt(segkmap, vp, off + on, 2975 n, 0, S_READ); 2976 error = writerp4(rp, base + on, n, uiop, 0); 2977 } 2978 } 2979 2980 if (!error) { 2981 if (mi->mi_flags & MI4_NOAC) 2982 flags = SM_WRITE; 2983 else if ((uiop->uio_loffset % bsize) == 0 || 2984 IS_SWAPVP(vp)) { 2985 /* 2986 * Have written a whole block. 2987 * Start an asynchronous write 2988 * and mark the buffer to 2989 * indicate that it won't be 2990 * needed again soon. 2991 */ 2992 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2993 } else 2994 flags = 0; 2995 if ((ioflag & (FSYNC|FDSYNC)) || 2996 (rp->r_flags & R4OUTOFSPACE)) { 2997 flags &= ~SM_ASYNC; 2998 flags |= SM_WRITE; 2999 } 3000 if (vpm_enable) { 3001 error = vpm_sync_pages(vp, off, n, flags); 3002 } else { 3003 error = segmap_release(segkmap, base, flags); 3004 } 3005 } else { 3006 if (vpm_enable) { 3007 (void) vpm_sync_pages(vp, off, n, 0); 3008 } else { 3009 (void) segmap_release(segkmap, base, 0); 3010 } 3011 /* 3012 * In the event that we got an access error while 3013 * faulting in a page for a write-only file just 3014 * force a write. 3015 */ 3016 if (error == EACCES) 3017 goto nfs4_fwrite; 3018 } 3019 } while (!error && uiop->uio_resid > 0); 3020 3021 bottom: 3022 if (error) { 3023 uiop->uio_resid = resid + remainder; 3024 uiop->uio_loffset = offset; 3025 } else { 3026 uiop->uio_resid += remainder; 3027 3028 mutex_enter(&rp->r_statev4_lock); 3029 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 3030 gethrestime(&rp->r_attr.va_mtime); 3031 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3032 } 3033 mutex_exit(&rp->r_statev4_lock); 3034 } 3035 3036 nfs_rw_exit(&rp->r_lkserlock); 3037 3038 return (error); 3039 } 3040 3041 /* 3042 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 3043 */ 3044 static int 3045 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 3046 int flags, cred_t *cr) 3047 { 3048 struct buf *bp; 3049 int error; 3050 page_t *savepp; 3051 uchar_t fsdata; 3052 stable_how4 stab_comm; 3053 3054 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3055 bp = pageio_setup(pp, len, vp, flags); 3056 ASSERT(bp != NULL); 3057 3058 /* 3059 * pageio_setup should have set b_addr to 0. This 3060 * is correct since we want to do I/O on a page 3061 * boundary. bp_mapin will use this addr to calculate 3062 * an offset, and then set b_addr to the kernel virtual 3063 * address it allocated for us. 3064 */ 3065 ASSERT(bp->b_un.b_addr == 0); 3066 3067 bp->b_edev = 0; 3068 bp->b_dev = 0; 3069 bp->b_lblkno = lbtodb(off); 3070 bp->b_file = vp; 3071 bp->b_offset = (offset_t)off; 3072 bp_mapin(bp); 3073 3074 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 3075 freemem > desfree) 3076 stab_comm = UNSTABLE4; 3077 else 3078 stab_comm = FILE_SYNC4; 3079 3080 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 3081 3082 bp_mapout(bp); 3083 pageio_done(bp); 3084 3085 if (stab_comm == UNSTABLE4) 3086 fsdata = C_DELAYCOMMIT; 3087 else 3088 fsdata = C_NOCOMMIT; 3089 3090 savepp = pp; 3091 do { 3092 pp->p_fsdata = fsdata; 3093 } while ((pp = pp->p_next) != savepp); 3094 3095 return (error); 3096 } 3097 3098 /* 3099 */ 3100 static int 3101 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3102 { 3103 nfs4_open_owner_t *oop; 3104 nfs4_open_stream_t *osp; 3105 rnode4_t *rp = VTOR4(vp); 3106 mntinfo4_t *mi = VTOMI4(vp); 3107 int reopen_needed; 3108 3109 ASSERT(nfs_zone() == mi->mi_zone); 3110 3111 3112 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3113 if (!oop) 3114 return (EIO); 3115 3116 /* returns with 'os_sync_lock' held */ 3117 osp = find_open_stream(oop, rp); 3118 if (!osp) { 3119 open_owner_rele(oop); 3120 return (EIO); 3121 } 3122 3123 if (osp->os_failed_reopen) { 3124 mutex_exit(&osp->os_sync_lock); 3125 open_stream_rele(osp, rp); 3126 open_owner_rele(oop); 3127 return (EIO); 3128 } 3129 3130 /* 3131 * Determine whether a reopen is needed. If this 3132 * is a delegation open stream, then the os_delegation bit 3133 * should be set. 3134 */ 3135 3136 reopen_needed = osp->os_delegation; 3137 3138 mutex_exit(&osp->os_sync_lock); 3139 open_owner_rele(oop); 3140 3141 if (reopen_needed) { 3142 nfs4_error_zinit(ep); 3143 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3144 mutex_enter(&osp->os_sync_lock); 3145 if (ep->error || ep->stat || osp->os_failed_reopen) { 3146 mutex_exit(&osp->os_sync_lock); 3147 open_stream_rele(osp, rp); 3148 return (EIO); 3149 } 3150 mutex_exit(&osp->os_sync_lock); 3151 } 3152 open_stream_rele(osp, rp); 3153 3154 return (0); 3155 } 3156 3157 /* 3158 * Write to file. Writes to remote server in largest size 3159 * chunks that the server can handle. Write is synchronous. 3160 */ 3161 static int 3162 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3163 stable_how4 *stab_comm) 3164 { 3165 mntinfo4_t *mi; 3166 COMPOUND4args_clnt args; 3167 COMPOUND4res_clnt res; 3168 WRITE4args *wargs; 3169 WRITE4res *wres; 3170 nfs_argop4 argop[2]; 3171 nfs_resop4 *resop; 3172 int tsize; 3173 stable_how4 stable; 3174 rnode4_t *rp; 3175 int doqueue = 1; 3176 bool_t needrecov; 3177 nfs4_recov_state_t recov_state; 3178 nfs4_stateid_types_t sid_types; 3179 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3180 int recov; 3181 3182 rp = VTOR4(vp); 3183 mi = VTOMI4(vp); 3184 3185 ASSERT(nfs_zone() == mi->mi_zone); 3186 3187 stable = *stab_comm; 3188 *stab_comm = FILE_SYNC4; 3189 3190 needrecov = FALSE; 3191 recov_state.rs_flags = 0; 3192 recov_state.rs_num_retry_despite_err = 0; 3193 nfs4_init_stateid_types(&sid_types); 3194 3195 /* Is curthread the recovery thread? */ 3196 mutex_enter(&mi->mi_lock); 3197 recov = (mi->mi_recovthread == curthread); 3198 mutex_exit(&mi->mi_lock); 3199 3200 recov_retry: 3201 args.ctag = TAG_WRITE; 3202 args.array_len = 2; 3203 args.array = argop; 3204 3205 if (!recov) { 3206 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3207 &recov_state, NULL); 3208 if (e.error) 3209 return (e.error); 3210 } 3211 3212 /* 0. putfh target fh */ 3213 argop[0].argop = OP_CPUTFH; 3214 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3215 3216 /* 1. write */ 3217 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3218 3219 do { 3220 3221 wargs->offset = (offset4)offset; 3222 wargs->data_val = base; 3223 3224 if (mi->mi_io_kstats) { 3225 mutex_enter(&mi->mi_lock); 3226 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3227 mutex_exit(&mi->mi_lock); 3228 } 3229 3230 if ((vp->v_flag & VNOCACHE) || 3231 (rp->r_flags & R4DIRECTIO) || 3232 (mi->mi_flags & MI4_DIRECTIO)) 3233 tsize = MIN(mi->mi_stsize, count); 3234 else 3235 tsize = MIN(mi->mi_curwrite, count); 3236 wargs->data_len = (uint_t)tsize; 3237 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3238 3239 if (mi->mi_io_kstats) { 3240 mutex_enter(&mi->mi_lock); 3241 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3242 mutex_exit(&mi->mi_lock); 3243 } 3244 3245 if (!recov) { 3246 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3247 if (e.error && !needrecov) { 3248 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3249 &recov_state, needrecov); 3250 return (e.error); 3251 } 3252 } else { 3253 if (e.error) 3254 return (e.error); 3255 } 3256 3257 /* 3258 * Do handling of OLD_STATEID outside 3259 * of the normal recovery framework. 3260 * 3261 * If write receives a BAD stateid error while using a 3262 * delegation stateid, retry using the open stateid (if it 3263 * exists). If it doesn't have an open stateid, reopen the 3264 * file first, then retry. 3265 */ 3266 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3267 sid_types.cur_sid_type != SPEC_SID) { 3268 nfs4_save_stateid(&wargs->stateid, &sid_types); 3269 if (!recov) 3270 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3271 &recov_state, needrecov); 3272 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3273 goto recov_retry; 3274 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3275 sid_types.cur_sid_type == DEL_SID) { 3276 nfs4_save_stateid(&wargs->stateid, &sid_types); 3277 mutex_enter(&rp->r_statev4_lock); 3278 rp->r_deleg_return_pending = TRUE; 3279 mutex_exit(&rp->r_statev4_lock); 3280 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3281 if (!recov) 3282 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3283 &recov_state, needrecov); 3284 (void) xdr_free(xdr_COMPOUND4res_clnt, 3285 (caddr_t)&res); 3286 return (EIO); 3287 } 3288 if (!recov) 3289 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3290 &recov_state, needrecov); 3291 /* hold needed for nfs4delegreturn_thread */ 3292 VN_HOLD(vp); 3293 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3294 NFS4_DR_DISCARD), FALSE); 3295 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3296 goto recov_retry; 3297 } 3298 3299 if (needrecov) { 3300 bool_t abort; 3301 3302 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3303 "nfs4write: client got error %d, res.status %d" 3304 ", so start recovery", e.error, res.status)); 3305 3306 abort = nfs4_start_recovery(&e, 3307 VTOMI4(vp), vp, NULL, &wargs->stateid, 3308 NULL, OP_WRITE, NULL, NULL, NULL); 3309 if (!e.error) { 3310 e.error = geterrno4(res.status); 3311 (void) xdr_free(xdr_COMPOUND4res_clnt, 3312 (caddr_t)&res); 3313 } 3314 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3315 &recov_state, needrecov); 3316 if (abort == FALSE) 3317 goto recov_retry; 3318 return (e.error); 3319 } 3320 3321 if (res.status) { 3322 e.error = geterrno4(res.status); 3323 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3324 if (!recov) 3325 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3326 &recov_state, needrecov); 3327 return (e.error); 3328 } 3329 3330 resop = &res.array[1]; /* write res */ 3331 wres = &resop->nfs_resop4_u.opwrite; 3332 3333 if ((int)wres->count > tsize) { 3334 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3335 3336 zcmn_err(getzoneid(), CE_WARN, 3337 "nfs4write: server wrote %u, requested was %u", 3338 (int)wres->count, tsize); 3339 if (!recov) 3340 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3341 &recov_state, needrecov); 3342 return (EIO); 3343 } 3344 if (wres->committed == UNSTABLE4) { 3345 *stab_comm = UNSTABLE4; 3346 if (wargs->stable == DATA_SYNC4 || 3347 wargs->stable == FILE_SYNC4) { 3348 (void) xdr_free(xdr_COMPOUND4res_clnt, 3349 (caddr_t)&res); 3350 zcmn_err(getzoneid(), CE_WARN, 3351 "nfs4write: server %s did not commit " 3352 "to stable storage", 3353 rp->r_server->sv_hostname); 3354 if (!recov) 3355 nfs4_end_fop(VTOMI4(vp), vp, NULL, 3356 OH_WRITE, &recov_state, needrecov); 3357 return (EIO); 3358 } 3359 } 3360 3361 tsize = (int)wres->count; 3362 count -= tsize; 3363 base += tsize; 3364 offset += tsize; 3365 if (mi->mi_io_kstats) { 3366 mutex_enter(&mi->mi_lock); 3367 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3368 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3369 tsize; 3370 mutex_exit(&mi->mi_lock); 3371 } 3372 lwp_stat_update(LWP_STAT_OUBLK, 1); 3373 mutex_enter(&rp->r_statelock); 3374 if (rp->r_flags & R4HAVEVERF) { 3375 if (rp->r_writeverf != wres->writeverf) { 3376 nfs4_set_mod(vp); 3377 rp->r_writeverf = wres->writeverf; 3378 } 3379 } else { 3380 rp->r_writeverf = wres->writeverf; 3381 rp->r_flags |= R4HAVEVERF; 3382 } 3383 PURGE_ATTRCACHE4_LOCKED(rp); 3384 rp->r_flags |= R4WRITEMODIFIED; 3385 gethrestime(&rp->r_attr.va_mtime); 3386 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3387 mutex_exit(&rp->r_statelock); 3388 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3389 } while (count); 3390 3391 if (!recov) 3392 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, 3393 needrecov); 3394 3395 return (e.error); 3396 } 3397 3398 /* 3399 * Read from a file. Reads data in largest chunks our interface can handle. 3400 */ 3401 static int 3402 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3403 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3404 { 3405 mntinfo4_t *mi; 3406 COMPOUND4args_clnt args; 3407 COMPOUND4res_clnt res; 3408 READ4args *rargs; 3409 nfs_argop4 argop[2]; 3410 int tsize; 3411 int doqueue; 3412 rnode4_t *rp; 3413 int data_len; 3414 bool_t is_eof; 3415 bool_t needrecov = FALSE; 3416 nfs4_recov_state_t recov_state; 3417 nfs4_stateid_types_t sid_types; 3418 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3419 3420 rp = VTOR4(vp); 3421 mi = VTOMI4(vp); 3422 doqueue = 1; 3423 3424 ASSERT(nfs_zone() == mi->mi_zone); 3425 3426 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3427 3428 args.array_len = 2; 3429 args.array = argop; 3430 3431 nfs4_init_stateid_types(&sid_types); 3432 3433 recov_state.rs_flags = 0; 3434 recov_state.rs_num_retry_despite_err = 0; 3435 3436 recov_retry: 3437 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3438 &recov_state, NULL); 3439 if (e.error) 3440 return (e.error); 3441 3442 /* putfh target fh */ 3443 argop[0].argop = OP_CPUTFH; 3444 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3445 3446 /* read */ 3447 argop[1].argop = OP_READ; 3448 rargs = &argop[1].nfs_argop4_u.opread; 3449 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3450 OP_READ, &sid_types, async); 3451 3452 do { 3453 if (mi->mi_io_kstats) { 3454 mutex_enter(&mi->mi_lock); 3455 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3456 mutex_exit(&mi->mi_lock); 3457 } 3458 3459 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3460 "nfs4read: %s call, rp %s", 3461 needrecov ? "recov" : "first", 3462 rnode4info(rp))); 3463 3464 if ((vp->v_flag & VNOCACHE) || 3465 (rp->r_flags & R4DIRECTIO) || 3466 (mi->mi_flags & MI4_DIRECTIO)) 3467 tsize = MIN(mi->mi_tsize, count); 3468 else 3469 tsize = MIN(mi->mi_curread, count); 3470 3471 rargs->offset = (offset4)offset; 3472 rargs->count = (count4)tsize; 3473 rargs->res_data_val_alt = NULL; 3474 rargs->res_mblk = NULL; 3475 rargs->res_uiop = NULL; 3476 rargs->res_maxsize = 0; 3477 rargs->wlist = NULL; 3478 3479 if (uiop) 3480 rargs->res_uiop = uiop; 3481 else 3482 rargs->res_data_val_alt = base; 3483 rargs->res_maxsize = tsize; 3484 3485 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3486 #ifdef DEBUG 3487 if (nfs4read_error_inject) { 3488 res.status = nfs4read_error_inject; 3489 nfs4read_error_inject = 0; 3490 } 3491 #endif 3492 3493 if (mi->mi_io_kstats) { 3494 mutex_enter(&mi->mi_lock); 3495 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3496 mutex_exit(&mi->mi_lock); 3497 } 3498 3499 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3500 if (e.error != 0 && !needrecov) { 3501 nfs4_end_fop(mi, vp, NULL, OH_READ, 3502 &recov_state, needrecov); 3503 return (e.error); 3504 } 3505 3506 /* 3507 * Do proper retry for OLD and BAD stateid errors outside 3508 * of the normal recovery framework. There are two differences 3509 * between async and sync reads. The first is that we allow 3510 * retry on BAD_STATEID for async reads, but not sync reads. 3511 * The second is that we mark the file dead for a failed 3512 * attempt with a special stateid for sync reads, but just 3513 * return EIO for async reads. 3514 * 3515 * If a sync read receives a BAD stateid error while using a 3516 * delegation stateid, retry using the open stateid (if it 3517 * exists). If it doesn't have an open stateid, reopen the 3518 * file first, then retry. 3519 */ 3520 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3521 res.status == NFS4ERR_BAD_STATEID) && async) { 3522 nfs4_end_fop(mi, vp, NULL, OH_READ, 3523 &recov_state, needrecov); 3524 if (sid_types.cur_sid_type == SPEC_SID) { 3525 (void) xdr_free(xdr_COMPOUND4res_clnt, 3526 (caddr_t)&res); 3527 return (EIO); 3528 } 3529 nfs4_save_stateid(&rargs->stateid, &sid_types); 3530 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3531 goto recov_retry; 3532 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3533 !async && sid_types.cur_sid_type != SPEC_SID) { 3534 nfs4_save_stateid(&rargs->stateid, &sid_types); 3535 nfs4_end_fop(mi, vp, NULL, OH_READ, 3536 &recov_state, needrecov); 3537 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3538 goto recov_retry; 3539 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3540 sid_types.cur_sid_type == DEL_SID) { 3541 nfs4_save_stateid(&rargs->stateid, &sid_types); 3542 mutex_enter(&rp->r_statev4_lock); 3543 rp->r_deleg_return_pending = TRUE; 3544 mutex_exit(&rp->r_statev4_lock); 3545 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3546 nfs4_end_fop(mi, vp, NULL, OH_READ, 3547 &recov_state, needrecov); 3548 (void) xdr_free(xdr_COMPOUND4res_clnt, 3549 (caddr_t)&res); 3550 return (EIO); 3551 } 3552 nfs4_end_fop(mi, vp, NULL, OH_READ, 3553 &recov_state, needrecov); 3554 /* hold needed for nfs4delegreturn_thread */ 3555 VN_HOLD(vp); 3556 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3557 NFS4_DR_DISCARD), FALSE); 3558 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3559 goto recov_retry; 3560 } 3561 if (needrecov) { 3562 bool_t abort; 3563 3564 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3565 "nfs4read: initiating recovery\n")); 3566 abort = nfs4_start_recovery(&e, 3567 mi, vp, NULL, &rargs->stateid, 3568 NULL, OP_READ, NULL, NULL, NULL); 3569 nfs4_end_fop(mi, vp, NULL, OH_READ, 3570 &recov_state, needrecov); 3571 /* 3572 * Do not retry if we got OLD_STATEID using a special 3573 * stateid. This avoids looping with a broken server. 3574 */ 3575 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3576 sid_types.cur_sid_type == SPEC_SID) 3577 abort = TRUE; 3578 3579 if (abort == FALSE) { 3580 /* 3581 * Need to retry all possible stateids in 3582 * case the recovery error wasn't stateid 3583 * related or the stateids have become 3584 * stale (server reboot). 3585 */ 3586 nfs4_init_stateid_types(&sid_types); 3587 (void) xdr_free(xdr_COMPOUND4res_clnt, 3588 (caddr_t)&res); 3589 goto recov_retry; 3590 } 3591 3592 if (!e.error) { 3593 e.error = geterrno4(res.status); 3594 (void) xdr_free(xdr_COMPOUND4res_clnt, 3595 (caddr_t)&res); 3596 } 3597 return (e.error); 3598 } 3599 3600 if (res.status) { 3601 e.error = geterrno4(res.status); 3602 nfs4_end_fop(mi, vp, NULL, OH_READ, 3603 &recov_state, needrecov); 3604 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3605 return (e.error); 3606 } 3607 3608 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3609 count -= data_len; 3610 if (base) 3611 base += data_len; 3612 offset += data_len; 3613 if (mi->mi_io_kstats) { 3614 mutex_enter(&mi->mi_lock); 3615 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3616 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3617 mutex_exit(&mi->mi_lock); 3618 } 3619 lwp_stat_update(LWP_STAT_INBLK, 1); 3620 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3621 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3622 3623 } while (count && !is_eof); 3624 3625 *residp = count; 3626 3627 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3628 3629 return (e.error); 3630 } 3631 3632 /* ARGSUSED */ 3633 static int 3634 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp, 3635 caller_context_t *ct) 3636 { 3637 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3638 return (EIO); 3639 switch (cmd) { 3640 case _FIODIRECTIO: 3641 return (nfs4_directio(vp, (int)arg, cr)); 3642 default: 3643 return (ENOTTY); 3644 } 3645 } 3646 3647 /* ARGSUSED */ 3648 int 3649 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3650 caller_context_t *ct) 3651 { 3652 int error; 3653 rnode4_t *rp = VTOR4(vp); 3654 3655 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3656 return (EIO); 3657 /* 3658 * If it has been specified that the return value will 3659 * just be used as a hint, and we are only being asked 3660 * for size, fsid or rdevid, then return the client's 3661 * notion of these values without checking to make sure 3662 * that the attribute cache is up to date. 3663 * The whole point is to avoid an over the wire GETATTR 3664 * call. 3665 */ 3666 if (flags & ATTR_HINT) { 3667 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) { 3668 mutex_enter(&rp->r_statelock); 3669 if (vap->va_mask & AT_SIZE) 3670 vap->va_size = rp->r_size; 3671 if (vap->va_mask & AT_FSID) 3672 vap->va_fsid = rp->r_attr.va_fsid; 3673 if (vap->va_mask & AT_RDEV) 3674 vap->va_rdev = rp->r_attr.va_rdev; 3675 mutex_exit(&rp->r_statelock); 3676 return (0); 3677 } 3678 } 3679 3680 /* 3681 * Only need to flush pages if asking for the mtime 3682 * and if there any dirty pages or any outstanding 3683 * asynchronous (write) requests for this file. 3684 */ 3685 if (vap->va_mask & AT_MTIME) { 3686 rp = VTOR4(vp); 3687 if (nfs4_has_pages(vp)) { 3688 mutex_enter(&rp->r_statev4_lock); 3689 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3690 mutex_exit(&rp->r_statev4_lock); 3691 if (rp->r_flags & R4DIRTY || 3692 rp->r_awcount > 0) { 3693 mutex_enter(&rp->r_statelock); 3694 rp->r_gcount++; 3695 mutex_exit(&rp->r_statelock); 3696 error = 3697 nfs4_putpage(vp, (u_offset_t)0, 3698 0, 0, cr, NULL); 3699 mutex_enter(&rp->r_statelock); 3700 if (error && (error == ENOSPC || 3701 error == EDQUOT)) { 3702 if (!rp->r_error) 3703 rp->r_error = error; 3704 } 3705 if (--rp->r_gcount == 0) 3706 cv_broadcast(&rp->r_cv); 3707 mutex_exit(&rp->r_statelock); 3708 } 3709 } else { 3710 mutex_exit(&rp->r_statev4_lock); 3711 } 3712 } 3713 } 3714 return (nfs4getattr(vp, vap, cr)); 3715 } 3716 3717 int 3718 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3719 { 3720 /* 3721 * If these are the only two bits cleared 3722 * on the server then return 0 (OK) else 3723 * return 1 (BAD). 3724 */ 3725 on_client &= ~(S_ISUID|S_ISGID); 3726 if (on_client == from_server) 3727 return (0); 3728 else 3729 return (1); 3730 } 3731 3732 /*ARGSUSED4*/ 3733 static int 3734 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3735 caller_context_t *ct) 3736 { 3737 int error; 3738 3739 if (vap->va_mask & AT_NOSET) 3740 return (EINVAL); 3741 3742 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3743 return (EIO); 3744 3745 /* 3746 * Don't call secpolicy_vnode_setattr, the client cannot 3747 * use its cached attributes to make security decisions 3748 * as the server may be faking mode bits or mapping uid/gid. 3749 * Always just let the server to the checking. 3750 * If we provide the ability to remove basic priviledges 3751 * to setattr (e.g. basic without chmod) then we will 3752 * need to add a check here before calling the server. 3753 */ 3754 error = nfs4setattr(vp, vap, flags, cr, NULL); 3755 3756 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0) 3757 vnevent_truncate(vp, ct); 3758 3759 return (error); 3760 } 3761 3762 /* 3763 * To replace the "guarded" version 3 setattr, we use two types of compound 3764 * setattr requests: 3765 * 1. The "normal" setattr, used when the size of the file isn't being 3766 * changed - { Putfh <fh>; Setattr; Getattr }/ 3767 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3768 * with only ctime as the argument. If the server ctime differs from 3769 * what is cached on the client, the verify will fail, but we would 3770 * already have the ctime from the preceding getattr, so just set it 3771 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3772 * Setattr; Getattr }. 3773 * 3774 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3775 * this setattr and NULL if they are not. 3776 */ 3777 static int 3778 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3779 vsecattr_t *vsap) 3780 { 3781 COMPOUND4args_clnt args; 3782 COMPOUND4res_clnt res, *resp = NULL; 3783 nfs4_ga_res_t *garp = NULL; 3784 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3785 nfs_argop4 argop[5]; 3786 int verify_argop = -1; 3787 int setattr_argop = 1; 3788 nfs_resop4 *resop; 3789 vattr_t va; 3790 rnode4_t *rp; 3791 int doqueue = 1; 3792 uint_t mask = vap->va_mask; 3793 mode_t omode; 3794 vsecattr_t *vsp; 3795 timestruc_t ctime; 3796 bool_t needrecov = FALSE; 3797 nfs4_recov_state_t recov_state; 3798 nfs4_stateid_types_t sid_types; 3799 stateid4 stateid; 3800 hrtime_t t; 3801 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3802 servinfo4_t *svp; 3803 bitmap4 supp_attrs; 3804 3805 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3806 rp = VTOR4(vp); 3807 nfs4_init_stateid_types(&sid_types); 3808 3809 /* 3810 * Only need to flush pages if there are any pages and 3811 * if the file is marked as dirty in some fashion. The 3812 * file must be flushed so that we can accurately 3813 * determine the size of the file and the cached data 3814 * after the SETATTR returns. A file is considered to 3815 * be dirty if it is either marked with R4DIRTY, has 3816 * outstanding i/o's active, or is mmap'd. In this 3817 * last case, we can't tell whether there are dirty 3818 * pages, so we flush just to be sure. 3819 */ 3820 if (nfs4_has_pages(vp) && 3821 ((rp->r_flags & R4DIRTY) || 3822 rp->r_count > 0 || 3823 rp->r_mapcnt > 0)) { 3824 ASSERT(vp->v_type != VCHR); 3825 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL); 3826 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3827 mutex_enter(&rp->r_statelock); 3828 if (!rp->r_error) 3829 rp->r_error = e.error; 3830 mutex_exit(&rp->r_statelock); 3831 } 3832 } 3833 3834 if (mask & AT_SIZE) { 3835 /* 3836 * Verification setattr compound for non-deleg AT_SIZE: 3837 * { Putfh; Getattr; Verify; Setattr; Getattr } 3838 * Set ctime local here (outside the do_again label) 3839 * so that subsequent retries (after failed VERIFY) 3840 * will use ctime from GETATTR results (from failed 3841 * verify compound) as VERIFY arg. 3842 * If file has delegation, then VERIFY(time_metadata) 3843 * is of little added value, so don't bother. 3844 */ 3845 mutex_enter(&rp->r_statev4_lock); 3846 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3847 rp->r_deleg_return_pending) { 3848 numops = 5; 3849 ctime = rp->r_attr.va_ctime; 3850 } 3851 mutex_exit(&rp->r_statev4_lock); 3852 } 3853 3854 recov_state.rs_flags = 0; 3855 recov_state.rs_num_retry_despite_err = 0; 3856 3857 args.ctag = TAG_SETATTR; 3858 do_again: 3859 recov_retry: 3860 setattr_argop = numops - 2; 3861 3862 args.array = argop; 3863 args.array_len = numops; 3864 3865 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3866 if (e.error) 3867 return (e.error); 3868 3869 3870 /* putfh target fh */ 3871 argop[0].argop = OP_CPUTFH; 3872 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3873 3874 if (numops == 5) { 3875 /* 3876 * We only care about the ctime, but need to get mtime 3877 * and size for proper cache update. 3878 */ 3879 /* getattr */ 3880 argop[1].argop = OP_GETATTR; 3881 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3882 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3883 3884 /* verify - set later in loop */ 3885 verify_argop = 2; 3886 } 3887 3888 /* setattr */ 3889 svp = rp->r_server; 3890 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3891 supp_attrs = svp->sv_supp_attrs; 3892 nfs_rw_exit(&svp->sv_lock); 3893 3894 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3895 supp_attrs, &e.error, &sid_types); 3896 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3897 if (e.error) { 3898 /* req time field(s) overflow - return immediately */ 3899 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3900 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3901 opsetattr.obj_attributes); 3902 return (e.error); 3903 } 3904 omode = rp->r_attr.va_mode; 3905 3906 /* getattr */ 3907 argop[numops-1].argop = OP_GETATTR; 3908 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3909 /* 3910 * If we are setting the ACL (indicated only by vsap != NULL), request 3911 * the ACL in this getattr. The ACL returned from this getattr will be 3912 * used in updating the ACL cache. 3913 */ 3914 if (vsap != NULL) 3915 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3916 FATTR4_ACL_MASK; 3917 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3918 3919 /* 3920 * setattr iterates if the object size is set and the cached ctime 3921 * does not match the file ctime. In that case, verify the ctime first. 3922 */ 3923 3924 do { 3925 if (verify_argop != -1) { 3926 /* 3927 * Verify that the ctime match before doing setattr. 3928 */ 3929 va.va_mask = AT_CTIME; 3930 va.va_ctime = ctime; 3931 svp = rp->r_server; 3932 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3933 supp_attrs = svp->sv_supp_attrs; 3934 nfs_rw_exit(&svp->sv_lock); 3935 e.error = nfs4args_verify(&argop[verify_argop], &va, 3936 OP_VERIFY, supp_attrs); 3937 if (e.error) { 3938 /* req time field(s) overflow - return */ 3939 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3940 needrecov); 3941 break; 3942 } 3943 } 3944 3945 doqueue = 1; 3946 3947 t = gethrtime(); 3948 3949 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3950 3951 /* 3952 * Purge the access cache and ACL cache if changing either the 3953 * owner of the file, the group owner, or the mode. These may 3954 * change the access permissions of the file, so purge old 3955 * information and start over again. 3956 */ 3957 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3958 (void) nfs4_access_purge_rp(rp); 3959 if (rp->r_secattr != NULL) { 3960 mutex_enter(&rp->r_statelock); 3961 vsp = rp->r_secattr; 3962 rp->r_secattr = NULL; 3963 mutex_exit(&rp->r_statelock); 3964 if (vsp != NULL) 3965 nfs4_acl_free_cache(vsp); 3966 } 3967 } 3968 3969 /* 3970 * If res.array_len == numops, then everything succeeded, 3971 * except for possibly the final getattr. If only the 3972 * last getattr failed, give up, and don't try recovery. 3973 */ 3974 if (res.array_len == numops) { 3975 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3976 needrecov); 3977 if (! e.error) 3978 resp = &res; 3979 break; 3980 } 3981 3982 /* 3983 * if either rpc call failed or completely succeeded - done 3984 */ 3985 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3986 if (e.error) { 3987 PURGE_ATTRCACHE4(vp); 3988 if (!needrecov) { 3989 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3990 needrecov); 3991 break; 3992 } 3993 } 3994 3995 /* 3996 * Do proper retry for OLD_STATEID outside of the normal 3997 * recovery framework. 3998 */ 3999 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 4000 sid_types.cur_sid_type != SPEC_SID && 4001 sid_types.cur_sid_type != NO_SID) { 4002 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4003 needrecov); 4004 nfs4_save_stateid(&stateid, &sid_types); 4005 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4006 opsetattr.obj_attributes); 4007 if (verify_argop != -1) { 4008 nfs4args_verify_free(&argop[verify_argop]); 4009 verify_argop = -1; 4010 } 4011 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4012 goto recov_retry; 4013 } 4014 4015 if (needrecov) { 4016 bool_t abort; 4017 4018 abort = nfs4_start_recovery(&e, 4019 VTOMI4(vp), vp, NULL, NULL, NULL, 4020 OP_SETATTR, NULL, NULL, NULL); 4021 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4022 needrecov); 4023 /* 4024 * Do not retry if we failed with OLD_STATEID using 4025 * a special stateid. This is done to avoid looping 4026 * with a broken server. 4027 */ 4028 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 4029 (sid_types.cur_sid_type == SPEC_SID || 4030 sid_types.cur_sid_type == NO_SID)) 4031 abort = TRUE; 4032 if (!e.error) { 4033 if (res.status == NFS4ERR_BADOWNER) 4034 nfs4_log_badowner(VTOMI4(vp), 4035 OP_SETATTR); 4036 4037 e.error = geterrno4(res.status); 4038 (void) xdr_free(xdr_COMPOUND4res_clnt, 4039 (caddr_t)&res); 4040 } 4041 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4042 opsetattr.obj_attributes); 4043 if (verify_argop != -1) { 4044 nfs4args_verify_free(&argop[verify_argop]); 4045 verify_argop = -1; 4046 } 4047 if (abort == FALSE) { 4048 /* 4049 * Need to retry all possible stateids in 4050 * case the recovery error wasn't stateid 4051 * related or the stateids have become 4052 * stale (server reboot). 4053 */ 4054 nfs4_init_stateid_types(&sid_types); 4055 goto recov_retry; 4056 } 4057 return (e.error); 4058 } 4059 4060 /* 4061 * Need to call nfs4_end_op before nfs4getattr to 4062 * avoid potential nfs4_start_op deadlock. See RFE 4063 * 4777612. Calls to nfs4_invalidate_pages() and 4064 * nfs4_purge_stale_fh() might also generate over the 4065 * wire calls which my cause nfs4_start_op() deadlock. 4066 */ 4067 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4068 4069 /* 4070 * Check to update lease. 4071 */ 4072 resp = &res; 4073 if (res.status == NFS4_OK) { 4074 break; 4075 } 4076 4077 /* 4078 * Check if verify failed to see if try again 4079 */ 4080 if ((verify_argop == -1) || (res.array_len != 3)) { 4081 /* 4082 * can't continue... 4083 */ 4084 if (res.status == NFS4ERR_BADOWNER) 4085 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 4086 4087 e.error = geterrno4(res.status); 4088 } else { 4089 /* 4090 * When the verify request fails, the client ctime is 4091 * not in sync with the server. This is the same as 4092 * the version 3 "not synchronized" error, and we 4093 * handle it in a similar manner (XXX do we need to???). 4094 * Use the ctime returned in the first getattr for 4095 * the input to the next verify. 4096 * If we couldn't get the attributes, then we give up 4097 * because we can't complete the operation as required. 4098 */ 4099 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 4100 } 4101 if (e.error) { 4102 PURGE_ATTRCACHE4(vp); 4103 nfs4_purge_stale_fh(e.error, vp, cr); 4104 } else { 4105 /* 4106 * retry with a new verify value 4107 */ 4108 ctime = garp->n4g_va.va_ctime; 4109 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4110 resp = NULL; 4111 } 4112 if (!e.error) { 4113 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4114 opsetattr.obj_attributes); 4115 if (verify_argop != -1) { 4116 nfs4args_verify_free(&argop[verify_argop]); 4117 verify_argop = -1; 4118 } 4119 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4120 goto do_again; 4121 } 4122 } while (!e.error); 4123 4124 if (e.error) { 4125 /* 4126 * If we are here, rfs4call has an irrecoverable error - return 4127 */ 4128 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4129 opsetattr.obj_attributes); 4130 if (verify_argop != -1) { 4131 nfs4args_verify_free(&argop[verify_argop]); 4132 verify_argop = -1; 4133 } 4134 if (resp) 4135 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4136 return (e.error); 4137 } 4138 4139 4140 4141 /* 4142 * If changing the size of the file, invalidate 4143 * any local cached data which is no longer part 4144 * of the file. We also possibly invalidate the 4145 * last page in the file. We could use 4146 * pvn_vpzero(), but this would mark the page as 4147 * modified and require it to be written back to 4148 * the server for no particularly good reason. 4149 * This way, if we access it, then we bring it 4150 * back in. A read should be cheaper than a 4151 * write. 4152 */ 4153 if (mask & AT_SIZE) { 4154 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4155 } 4156 4157 /* either no error or one of the postop getattr failed */ 4158 4159 /* 4160 * XXX Perform a simplified version of wcc checking. Instead of 4161 * have another getattr to get pre-op, just purge cache if 4162 * any of the ops prior to and including the getattr failed. 4163 * If the getattr succeeded then update the attrcache accordingly. 4164 */ 4165 4166 garp = NULL; 4167 if (res.status == NFS4_OK) { 4168 /* 4169 * Last getattr 4170 */ 4171 resop = &res.array[numops - 1]; 4172 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4173 } 4174 /* 4175 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4176 * rather than filling it. See the function itself for details. 4177 */ 4178 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4179 if (garp != NULL) { 4180 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4181 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4182 vs_ace4_destroy(&garp->n4g_vsa); 4183 } else { 4184 if (vsap != NULL) { 4185 /* 4186 * The ACL was supposed to be set and to be 4187 * returned in the last getattr of this 4188 * compound, but for some reason the getattr 4189 * result doesn't contain the ACL. In this 4190 * case, purge the ACL cache. 4191 */ 4192 if (rp->r_secattr != NULL) { 4193 mutex_enter(&rp->r_statelock); 4194 vsp = rp->r_secattr; 4195 rp->r_secattr = NULL; 4196 mutex_exit(&rp->r_statelock); 4197 if (vsp != NULL) 4198 nfs4_acl_free_cache(vsp); 4199 } 4200 } 4201 } 4202 } 4203 4204 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4205 /* 4206 * Set the size, rather than relying on getting it updated 4207 * via a GETATTR. With delegations the client tries to 4208 * suppress GETATTR calls. 4209 */ 4210 mutex_enter(&rp->r_statelock); 4211 rp->r_size = vap->va_size; 4212 mutex_exit(&rp->r_statelock); 4213 } 4214 4215 /* 4216 * Can free up request args and res 4217 */ 4218 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4219 opsetattr.obj_attributes); 4220 if (verify_argop != -1) { 4221 nfs4args_verify_free(&argop[verify_argop]); 4222 verify_argop = -1; 4223 } 4224 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4225 4226 /* 4227 * Some servers will change the mode to clear the setuid 4228 * and setgid bits when changing the uid or gid. The 4229 * client needs to compensate appropriately. 4230 */ 4231 if (mask & (AT_UID | AT_GID)) { 4232 int terror, do_setattr; 4233 4234 do_setattr = 0; 4235 va.va_mask = AT_MODE; 4236 terror = nfs4getattr(vp, &va, cr); 4237 if (!terror && 4238 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4239 (!(mask & AT_MODE) && va.va_mode != omode))) { 4240 va.va_mask = AT_MODE; 4241 if (mask & AT_MODE) { 4242 /* 4243 * We asked the mode to be changed and what 4244 * we just got from the server in getattr is 4245 * not what we wanted it to be, so set it now. 4246 */ 4247 va.va_mode = vap->va_mode; 4248 do_setattr = 1; 4249 } else { 4250 /* 4251 * We did not ask the mode to be changed, 4252 * Check to see that the server just cleared 4253 * I_SUID and I_GUID from it. If not then 4254 * set mode to omode with UID/GID cleared. 4255 */ 4256 if (nfs4_compare_modes(va.va_mode, omode)) { 4257 omode &= ~(S_ISUID|S_ISGID); 4258 va.va_mode = omode; 4259 do_setattr = 1; 4260 } 4261 } 4262 4263 if (do_setattr) 4264 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4265 } 4266 } 4267 4268 return (e.error); 4269 } 4270 4271 /* ARGSUSED */ 4272 static int 4273 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct) 4274 { 4275 COMPOUND4args_clnt args; 4276 COMPOUND4res_clnt res; 4277 int doqueue; 4278 uint32_t acc, resacc, argacc; 4279 rnode4_t *rp; 4280 cred_t *cred, *ncr, *ncrfree = NULL; 4281 nfs4_access_type_t cacc; 4282 int num_ops; 4283 nfs_argop4 argop[3]; 4284 nfs_resop4 *resop; 4285 bool_t needrecov = FALSE, do_getattr; 4286 nfs4_recov_state_t recov_state; 4287 int rpc_error; 4288 hrtime_t t; 4289 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4290 mntinfo4_t *mi = VTOMI4(vp); 4291 4292 if (nfs_zone() != mi->mi_zone) 4293 return (EIO); 4294 4295 acc = 0; 4296 if (mode & VREAD) 4297 acc |= ACCESS4_READ; 4298 if (mode & VWRITE) { 4299 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4300 return (EROFS); 4301 if (vp->v_type == VDIR) 4302 acc |= ACCESS4_DELETE; 4303 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4304 } 4305 if (mode & VEXEC) { 4306 if (vp->v_type == VDIR) 4307 acc |= ACCESS4_LOOKUP; 4308 else 4309 acc |= ACCESS4_EXECUTE; 4310 } 4311 4312 if (VTOR4(vp)->r_acache != NULL) { 4313 e.error = nfs4_validate_caches(vp, cr); 4314 if (e.error) 4315 return (e.error); 4316 } 4317 4318 rp = VTOR4(vp); 4319 if (vp->v_type == VDIR) 4320 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4321 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4322 else 4323 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4324 ACCESS4_EXECUTE; 4325 recov_state.rs_flags = 0; 4326 recov_state.rs_num_retry_despite_err = 0; 4327 4328 cred = cr; 4329 /* 4330 * ncr and ncrfree both initially 4331 * point to the memory area returned 4332 * by crnetadjust(); 4333 * ncrfree not NULL when exiting means 4334 * that we need to release it 4335 */ 4336 ncr = crnetadjust(cred); 4337 ncrfree = ncr; 4338 4339 tryagain: 4340 cacc = nfs4_access_check(rp, acc, cred); 4341 if (cacc == NFS4_ACCESS_ALLOWED) { 4342 if (ncrfree != NULL) 4343 crfree(ncrfree); 4344 return (0); 4345 } 4346 if (cacc == NFS4_ACCESS_DENIED) { 4347 /* 4348 * If the cred can be adjusted, try again 4349 * with the new cred. 4350 */ 4351 if (ncr != NULL) { 4352 cred = ncr; 4353 ncr = NULL; 4354 goto tryagain; 4355 } 4356 if (ncrfree != NULL) 4357 crfree(ncrfree); 4358 return (EACCES); 4359 } 4360 4361 recov_retry: 4362 /* 4363 * Don't take with r_statev4_lock here. r_deleg_type could 4364 * change as soon as lock is released. Since it is an int, 4365 * there is no atomicity issue. 4366 */ 4367 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4368 num_ops = do_getattr ? 3 : 2; 4369 4370 args.ctag = TAG_ACCESS; 4371 4372 args.array_len = num_ops; 4373 args.array = argop; 4374 4375 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4376 &recov_state, NULL)) { 4377 if (ncrfree != NULL) 4378 crfree(ncrfree); 4379 return (e.error); 4380 } 4381 4382 /* putfh target fh */ 4383 argop[0].argop = OP_CPUTFH; 4384 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4385 4386 /* access */ 4387 argop[1].argop = OP_ACCESS; 4388 argop[1].nfs_argop4_u.opaccess.access = argacc; 4389 4390 /* getattr */ 4391 if (do_getattr) { 4392 argop[2].argop = OP_GETATTR; 4393 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4394 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4395 } 4396 4397 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4398 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4399 rnode4info(VTOR4(vp)))); 4400 4401 doqueue = 1; 4402 t = gethrtime(); 4403 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4404 rpc_error = e.error; 4405 4406 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4407 if (needrecov) { 4408 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4409 "nfs4_access: initiating recovery\n")); 4410 4411 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4412 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) { 4413 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4414 &recov_state, needrecov); 4415 if (!e.error) 4416 (void) xdr_free(xdr_COMPOUND4res_clnt, 4417 (caddr_t)&res); 4418 goto recov_retry; 4419 } 4420 } 4421 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4422 4423 if (e.error) 4424 goto out; 4425 4426 if (res.status) { 4427 e.error = geterrno4(res.status); 4428 /* 4429 * This might generate over the wire calls throught 4430 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4431 * here to avoid a deadlock. 4432 */ 4433 nfs4_purge_stale_fh(e.error, vp, cr); 4434 goto out; 4435 } 4436 resop = &res.array[1]; /* access res */ 4437 4438 resacc = resop->nfs_resop4_u.opaccess.access; 4439 4440 if (do_getattr) { 4441 resop++; /* getattr res */ 4442 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4443 t, cr, FALSE, NULL); 4444 } 4445 4446 if (!e.error) { 4447 nfs4_access_cache(rp, argacc, resacc, cred); 4448 /* 4449 * we just cached results with cred; if cred is the 4450 * adjusted credentials from crnetadjust, we do not want 4451 * to release them before exiting: hence setting ncrfree 4452 * to NULL 4453 */ 4454 if (cred != cr) 4455 ncrfree = NULL; 4456 /* XXX check the supported bits too? */ 4457 if ((acc & resacc) != acc) { 4458 /* 4459 * The following code implements the semantic 4460 * that a setuid root program has *at least* the 4461 * permissions of the user that is running the 4462 * program. See rfs3call() for more portions 4463 * of the implementation of this functionality. 4464 */ 4465 /* XXX-LP */ 4466 if (ncr != NULL) { 4467 (void) xdr_free(xdr_COMPOUND4res_clnt, 4468 (caddr_t)&res); 4469 cred = ncr; 4470 ncr = NULL; 4471 goto tryagain; 4472 } 4473 e.error = EACCES; 4474 } 4475 } 4476 4477 out: 4478 if (!rpc_error) 4479 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4480 4481 if (ncrfree != NULL) 4482 crfree(ncrfree); 4483 4484 return (e.error); 4485 } 4486 4487 /* ARGSUSED */ 4488 static int 4489 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct) 4490 { 4491 COMPOUND4args_clnt args; 4492 COMPOUND4res_clnt res; 4493 int doqueue; 4494 rnode4_t *rp; 4495 nfs_argop4 argop[3]; 4496 nfs_resop4 *resop; 4497 READLINK4res *lr_res; 4498 nfs4_ga_res_t *garp; 4499 uint_t len; 4500 char *linkdata; 4501 bool_t needrecov = FALSE; 4502 nfs4_recov_state_t recov_state; 4503 hrtime_t t; 4504 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4505 4506 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4507 return (EIO); 4508 /* 4509 * Can't readlink anything other than a symbolic link. 4510 */ 4511 if (vp->v_type != VLNK) 4512 return (EINVAL); 4513 4514 rp = VTOR4(vp); 4515 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4516 e.error = nfs4_validate_caches(vp, cr); 4517 if (e.error) 4518 return (e.error); 4519 mutex_enter(&rp->r_statelock); 4520 if (rp->r_symlink.contents != NULL) { 4521 e.error = uiomove(rp->r_symlink.contents, 4522 rp->r_symlink.len, UIO_READ, uiop); 4523 mutex_exit(&rp->r_statelock); 4524 return (e.error); 4525 } 4526 mutex_exit(&rp->r_statelock); 4527 } 4528 recov_state.rs_flags = 0; 4529 recov_state.rs_num_retry_despite_err = 0; 4530 4531 recov_retry: 4532 args.array_len = 3; 4533 args.array = argop; 4534 args.ctag = TAG_READLINK; 4535 4536 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4537 if (e.error) { 4538 return (e.error); 4539 } 4540 4541 /* 0. putfh symlink fh */ 4542 argop[0].argop = OP_CPUTFH; 4543 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4544 4545 /* 1. readlink */ 4546 argop[1].argop = OP_READLINK; 4547 4548 /* 2. getattr */ 4549 argop[2].argop = OP_GETATTR; 4550 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4551 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4552 4553 doqueue = 1; 4554 4555 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4556 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4557 rnode4info(VTOR4(vp)))); 4558 4559 t = gethrtime(); 4560 4561 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4562 4563 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4564 if (needrecov) { 4565 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4566 "nfs4_readlink: initiating recovery\n")); 4567 4568 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4569 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) { 4570 if (!e.error) 4571 (void) xdr_free(xdr_COMPOUND4res_clnt, 4572 (caddr_t)&res); 4573 4574 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4575 needrecov); 4576 goto recov_retry; 4577 } 4578 } 4579 4580 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4581 4582 if (e.error) 4583 return (e.error); 4584 4585 /* 4586 * There is an path in the code below which calls 4587 * nfs4_purge_stale_fh(), which may generate otw calls through 4588 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4589 * here to avoid nfs4_start_op() deadlock. 4590 */ 4591 4592 if (res.status && (res.array_len < args.array_len)) { 4593 /* 4594 * either Putfh or Link failed 4595 */ 4596 e.error = geterrno4(res.status); 4597 nfs4_purge_stale_fh(e.error, vp, cr); 4598 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4599 return (e.error); 4600 } 4601 4602 resop = &res.array[1]; /* readlink res */ 4603 lr_res = &resop->nfs_resop4_u.opreadlink; 4604 4605 /* 4606 * treat symlink names as data 4607 */ 4608 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL); 4609 if (linkdata != NULL) { 4610 int uio_len = len - 1; 4611 /* len includes null byte, which we won't uiomove */ 4612 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4613 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4614 mutex_enter(&rp->r_statelock); 4615 if (rp->r_symlink.contents == NULL) { 4616 rp->r_symlink.contents = linkdata; 4617 rp->r_symlink.len = uio_len; 4618 rp->r_symlink.size = len; 4619 mutex_exit(&rp->r_statelock); 4620 } else { 4621 mutex_exit(&rp->r_statelock); 4622 kmem_free(linkdata, len); 4623 } 4624 } else { 4625 kmem_free(linkdata, len); 4626 } 4627 } 4628 if (res.status == NFS4_OK) { 4629 resop++; /* getattr res */ 4630 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4631 } 4632 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4633 4634 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4635 4636 /* 4637 * The over the wire error for attempting to readlink something 4638 * other than a symbolic link is ENXIO. However, we need to 4639 * return EINVAL instead of ENXIO, so we map it here. 4640 */ 4641 return (e.error == ENXIO ? EINVAL : e.error); 4642 } 4643 4644 /* 4645 * Flush local dirty pages to stable storage on the server. 4646 * 4647 * If FNODSYNC is specified, then there is nothing to do because 4648 * metadata changes are not cached on the client before being 4649 * sent to the server. 4650 */ 4651 /* ARGSUSED */ 4652 static int 4653 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 4654 { 4655 int error; 4656 4657 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4658 return (0); 4659 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4660 return (EIO); 4661 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4662 if (!error) 4663 error = VTOR4(vp)->r_error; 4664 return (error); 4665 } 4666 4667 /* 4668 * Weirdness: if the file was removed or the target of a rename 4669 * operation while it was open, it got renamed instead. Here we 4670 * remove the renamed file. 4671 */ 4672 /* ARGSUSED */ 4673 void 4674 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4675 { 4676 rnode4_t *rp; 4677 4678 ASSERT(vp != DNLC_NO_VNODE); 4679 4680 rp = VTOR4(vp); 4681 4682 if (IS_SHADOW(vp, rp)) { 4683 sv_inactive(vp); 4684 return; 4685 } 4686 4687 /* 4688 * If this is coming from the wrong zone, we let someone in the right 4689 * zone take care of it asynchronously. We can get here due to 4690 * VN_RELE() being called from pageout() or fsflush(). This call may 4691 * potentially turn into an expensive no-op if, for instance, v_count 4692 * gets incremented in the meantime, but it's still correct. 4693 */ 4694 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4695 nfs4_async_inactive(vp, cr); 4696 return; 4697 } 4698 4699 /* 4700 * Some of the cleanup steps might require over-the-wire 4701 * operations. Since VOP_INACTIVE can get called as a result of 4702 * other over-the-wire operations (e.g., an attribute cache update 4703 * can lead to a DNLC purge), doing those steps now would lead to a 4704 * nested call to the recovery framework, which can deadlock. So 4705 * do any over-the-wire cleanups asynchronously, in a separate 4706 * thread. 4707 */ 4708 4709 mutex_enter(&rp->r_os_lock); 4710 mutex_enter(&rp->r_statelock); 4711 mutex_enter(&rp->r_statev4_lock); 4712 4713 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4714 mutex_exit(&rp->r_statev4_lock); 4715 mutex_exit(&rp->r_statelock); 4716 mutex_exit(&rp->r_os_lock); 4717 nfs4_async_inactive(vp, cr); 4718 return; 4719 } 4720 4721 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4722 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4723 mutex_exit(&rp->r_statev4_lock); 4724 mutex_exit(&rp->r_statelock); 4725 mutex_exit(&rp->r_os_lock); 4726 nfs4_async_inactive(vp, cr); 4727 return; 4728 } 4729 4730 if (rp->r_unldvp != NULL) { 4731 mutex_exit(&rp->r_statev4_lock); 4732 mutex_exit(&rp->r_statelock); 4733 mutex_exit(&rp->r_os_lock); 4734 nfs4_async_inactive(vp, cr); 4735 return; 4736 } 4737 mutex_exit(&rp->r_statev4_lock); 4738 mutex_exit(&rp->r_statelock); 4739 mutex_exit(&rp->r_os_lock); 4740 4741 rp4_addfree(rp, cr); 4742 } 4743 4744 /* 4745 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4746 * various bits of state. The caller must not refer to vp after this call. 4747 */ 4748 4749 void 4750 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4751 { 4752 rnode4_t *rp = VTOR4(vp); 4753 nfs4_recov_state_t recov_state; 4754 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4755 vnode_t *unldvp; 4756 char *unlname; 4757 cred_t *unlcred; 4758 COMPOUND4args_clnt args; 4759 COMPOUND4res_clnt res, *resp; 4760 nfs_argop4 argop[2]; 4761 int doqueue; 4762 #ifdef DEBUG 4763 char *name; 4764 #endif 4765 4766 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4767 ASSERT(!IS_SHADOW(vp, rp)); 4768 4769 #ifdef DEBUG 4770 name = fn_name(VTOSV(vp)->sv_name); 4771 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4772 "release vnode %s", name)); 4773 kmem_free(name, MAXNAMELEN); 4774 #endif 4775 4776 if (vp->v_type == VREG) { 4777 bool_t recov_failed = FALSE; 4778 4779 e.error = nfs4close_all(vp, cr); 4780 if (e.error) { 4781 /* Check to see if recovery failed */ 4782 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4783 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4784 recov_failed = TRUE; 4785 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4786 if (!recov_failed) { 4787 mutex_enter(&rp->r_statelock); 4788 if (rp->r_flags & R4RECOVERR) 4789 recov_failed = TRUE; 4790 mutex_exit(&rp->r_statelock); 4791 } 4792 if (recov_failed) { 4793 NFS4_DEBUG(nfs4_client_recov_debug, 4794 (CE_NOTE, "nfs4_inactive_otw: " 4795 "close failed (recovery failure)")); 4796 } 4797 } 4798 } 4799 4800 redo: 4801 if (rp->r_unldvp == NULL) { 4802 rp4_addfree(rp, cr); 4803 return; 4804 } 4805 4806 /* 4807 * Save the vnode pointer for the directory where the 4808 * unlinked-open file got renamed, then set it to NULL 4809 * to prevent another thread from getting here before 4810 * we're done with the remove. While we have the 4811 * statelock, make local copies of the pertinent rnode 4812 * fields. If we weren't to do this in an atomic way, the 4813 * the unl* fields could become inconsistent with respect 4814 * to each other due to a race condition between this 4815 * code and nfs_remove(). See bug report 1034328. 4816 */ 4817 mutex_enter(&rp->r_statelock); 4818 if (rp->r_unldvp == NULL) { 4819 mutex_exit(&rp->r_statelock); 4820 rp4_addfree(rp, cr); 4821 return; 4822 } 4823 4824 unldvp = rp->r_unldvp; 4825 rp->r_unldvp = NULL; 4826 unlname = rp->r_unlname; 4827 rp->r_unlname = NULL; 4828 unlcred = rp->r_unlcred; 4829 rp->r_unlcred = NULL; 4830 mutex_exit(&rp->r_statelock); 4831 4832 /* 4833 * If there are any dirty pages left, then flush 4834 * them. This is unfortunate because they just 4835 * may get thrown away during the remove operation, 4836 * but we have to do this for correctness. 4837 */ 4838 if (nfs4_has_pages(vp) && 4839 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4840 ASSERT(vp->v_type != VCHR); 4841 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL); 4842 if (e.error) { 4843 mutex_enter(&rp->r_statelock); 4844 if (!rp->r_error) 4845 rp->r_error = e.error; 4846 mutex_exit(&rp->r_statelock); 4847 } 4848 } 4849 4850 recov_state.rs_flags = 0; 4851 recov_state.rs_num_retry_despite_err = 0; 4852 recov_retry_remove: 4853 /* 4854 * Do the remove operation on the renamed file 4855 */ 4856 args.ctag = TAG_INACTIVE; 4857 4858 /* 4859 * Remove ops: putfh dir; remove 4860 */ 4861 args.array_len = 2; 4862 args.array = argop; 4863 4864 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4865 if (e.error) { 4866 kmem_free(unlname, MAXNAMELEN); 4867 crfree(unlcred); 4868 VN_RELE(unldvp); 4869 /* 4870 * Try again; this time around r_unldvp will be NULL, so we'll 4871 * just call rp4_addfree() and return. 4872 */ 4873 goto redo; 4874 } 4875 4876 /* putfh directory */ 4877 argop[0].argop = OP_CPUTFH; 4878 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4879 4880 /* remove */ 4881 argop[1].argop = OP_CREMOVE; 4882 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4883 4884 doqueue = 1; 4885 resp = &res; 4886 4887 #if 0 /* notyet */ 4888 /* 4889 * Can't do this yet. We may be being called from 4890 * dnlc_purge_XXX while that routine is holding a 4891 * mutex lock to the nc_rele list. The calls to 4892 * nfs3_cache_wcc_data may result in calls to 4893 * dnlc_purge_XXX. This will result in a deadlock. 4894 */ 4895 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4896 if (e.error) { 4897 PURGE_ATTRCACHE4(unldvp); 4898 resp = NULL; 4899 } else if (res.status) { 4900 e.error = geterrno4(res.status); 4901 PURGE_ATTRCACHE4(unldvp); 4902 /* 4903 * This code is inactive right now 4904 * but if made active there should 4905 * be a nfs4_end_op() call before 4906 * nfs4_purge_stale_fh to avoid start_op() 4907 * deadlock. See BugId: 4948726 4908 */ 4909 nfs4_purge_stale_fh(error, unldvp, cr); 4910 } else { 4911 nfs_resop4 *resop; 4912 REMOVE4res *rm_res; 4913 4914 resop = &res.array[1]; 4915 rm_res = &resop->nfs_resop4_u.opremove; 4916 /* 4917 * Update directory cache attribute, 4918 * readdir and dnlc caches. 4919 */ 4920 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4921 } 4922 #else 4923 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4924 4925 PURGE_ATTRCACHE4(unldvp); 4926 #endif 4927 4928 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4929 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4930 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 4931 if (!e.error) 4932 (void) xdr_free(xdr_COMPOUND4res_clnt, 4933 (caddr_t)&res); 4934 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4935 &recov_state, TRUE); 4936 goto recov_retry_remove; 4937 } 4938 } 4939 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4940 4941 /* 4942 * Release stuff held for the remove 4943 */ 4944 VN_RELE(unldvp); 4945 if (!e.error && resp) 4946 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4947 4948 kmem_free(unlname, MAXNAMELEN); 4949 crfree(unlcred); 4950 goto redo; 4951 } 4952 4953 /* 4954 * Remote file system operations having to do with directory manipulation. 4955 */ 4956 /* ARGSUSED3 */ 4957 int 4958 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4959 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 4960 int *direntflags, pathname_t *realpnp) 4961 { 4962 int error; 4963 vnode_t *vp, *avp = NULL; 4964 rnode4_t *drp; 4965 4966 *vpp = NULL; 4967 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4968 return (EPERM); 4969 /* 4970 * if LOOKUP_XATTR, must replace dvp (object) with 4971 * object's attrdir before continuing with lookup 4972 */ 4973 if (flags & LOOKUP_XATTR) { 4974 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4975 if (error) 4976 return (error); 4977 4978 dvp = avp; 4979 4980 /* 4981 * If lookup is for "", just return dvp now. The attrdir 4982 * has already been activated (from nfs4lookup_xattr), and 4983 * the caller will RELE the original dvp -- not 4984 * the attrdir. So, set vpp and return. 4985 * Currently, when the LOOKUP_XATTR flag is 4986 * passed to VOP_LOOKUP, the name is always empty, and 4987 * shortcircuiting here avoids 3 unneeded lock/unlock 4988 * pairs. 4989 * 4990 * If a non-empty name was provided, then it is the 4991 * attribute name, and it will be looked up below. 4992 */ 4993 if (*nm == '\0') { 4994 *vpp = dvp; 4995 return (0); 4996 } 4997 4998 /* 4999 * The vfs layer never sends a name when asking for the 5000 * attrdir, so we should never get here (unless of course 5001 * name is passed at some time in future -- at which time 5002 * we'll blow up here). 5003 */ 5004 ASSERT(0); 5005 } 5006 5007 drp = VTOR4(dvp); 5008 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5009 return (EINTR); 5010 5011 error = nfs4lookup(dvp, nm, vpp, cr, 0); 5012 nfs_rw_exit(&drp->r_rwlock); 5013 5014 /* 5015 * If vnode is a device, create special vnode. 5016 */ 5017 if (!error && ISVDEV((*vpp)->v_type)) { 5018 vp = *vpp; 5019 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 5020 VN_RELE(vp); 5021 } 5022 5023 return (error); 5024 } 5025 5026 /* ARGSUSED */ 5027 static int 5028 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 5029 { 5030 int error; 5031 rnode4_t *drp; 5032 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 5033 mntinfo4_t *mi; 5034 5035 mi = VTOMI4(dvp); 5036 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) && 5037 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS)) 5038 return (EINVAL); 5039 5040 drp = VTOR4(dvp); 5041 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5042 return (EINTR); 5043 5044 mutex_enter(&drp->r_statelock); 5045 /* 5046 * If the server doesn't support xattrs just return EINVAL 5047 */ 5048 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 5049 mutex_exit(&drp->r_statelock); 5050 nfs_rw_exit(&drp->r_rwlock); 5051 return (EINVAL); 5052 } 5053 5054 /* 5055 * If there is a cached xattr directory entry, 5056 * use it as long as the attributes are valid. If the 5057 * attributes are not valid, take the simple approach and 5058 * free the cached value and re-fetch a new value. 5059 * 5060 * We don't negative entry cache for now, if we did we 5061 * would need to check if the file has changed on every 5062 * lookup. But xattrs don't exist very often and failing 5063 * an openattr is not much more expensive than and NVERIFY or GETATTR 5064 * so do an openattr over the wire for now. 5065 */ 5066 if (drp->r_xattr_dir != NULL) { 5067 if (ATTRCACHE4_VALID(dvp)) { 5068 VN_HOLD(drp->r_xattr_dir); 5069 *vpp = drp->r_xattr_dir; 5070 mutex_exit(&drp->r_statelock); 5071 nfs_rw_exit(&drp->r_rwlock); 5072 return (0); 5073 } 5074 VN_RELE(drp->r_xattr_dir); 5075 drp->r_xattr_dir = NULL; 5076 } 5077 mutex_exit(&drp->r_statelock); 5078 5079 error = nfs4openattr(dvp, vpp, cflag, cr); 5080 5081 nfs_rw_exit(&drp->r_rwlock); 5082 5083 return (error); 5084 } 5085 5086 static int 5087 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 5088 { 5089 int error; 5090 rnode4_t *drp; 5091 5092 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5093 5094 /* 5095 * If lookup is for "", just return dvp. Don't need 5096 * to send it over the wire, look it up in the dnlc, 5097 * or perform any access checks. 5098 */ 5099 if (*nm == '\0') { 5100 VN_HOLD(dvp); 5101 *vpp = dvp; 5102 return (0); 5103 } 5104 5105 /* 5106 * Can't do lookups in non-directories. 5107 */ 5108 if (dvp->v_type != VDIR) 5109 return (ENOTDIR); 5110 5111 /* 5112 * If lookup is for ".", just return dvp. Don't need 5113 * to send it over the wire or look it up in the dnlc, 5114 * just need to check access. 5115 */ 5116 if (nm[0] == '.' && nm[1] == '\0') { 5117 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5118 if (error) 5119 return (error); 5120 VN_HOLD(dvp); 5121 *vpp = dvp; 5122 return (0); 5123 } 5124 5125 drp = VTOR4(dvp); 5126 if (!(drp->r_flags & R4LOOKUP)) { 5127 mutex_enter(&drp->r_statelock); 5128 drp->r_flags |= R4LOOKUP; 5129 mutex_exit(&drp->r_statelock); 5130 } 5131 5132 *vpp = NULL; 5133 /* 5134 * Lookup this name in the DNLC. If there is no entry 5135 * lookup over the wire. 5136 */ 5137 if (!skipdnlc) 5138 *vpp = dnlc_lookup(dvp, nm); 5139 if (*vpp == NULL) { 5140 /* 5141 * We need to go over the wire to lookup the name. 5142 */ 5143 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5144 } 5145 5146 /* 5147 * We hit on the dnlc 5148 */ 5149 if (*vpp != DNLC_NO_VNODE || 5150 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5151 /* 5152 * But our attrs may not be valid. 5153 */ 5154 if (ATTRCACHE4_VALID(dvp)) { 5155 error = nfs4_waitfor_purge_complete(dvp); 5156 if (error) { 5157 VN_RELE(*vpp); 5158 *vpp = NULL; 5159 return (error); 5160 } 5161 5162 /* 5163 * If after the purge completes, check to make sure 5164 * our attrs are still valid. 5165 */ 5166 if (ATTRCACHE4_VALID(dvp)) { 5167 /* 5168 * If we waited for a purge we may have 5169 * lost our vnode so look it up again. 5170 */ 5171 VN_RELE(*vpp); 5172 *vpp = dnlc_lookup(dvp, nm); 5173 if (*vpp == NULL) 5174 return (nfs4lookupnew_otw(dvp, 5175 nm, vpp, cr)); 5176 5177 /* 5178 * The access cache should almost always hit 5179 */ 5180 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5181 5182 if (error) { 5183 VN_RELE(*vpp); 5184 *vpp = NULL; 5185 return (error); 5186 } 5187 if (*vpp == DNLC_NO_VNODE) { 5188 VN_RELE(*vpp); 5189 *vpp = NULL; 5190 return (ENOENT); 5191 } 5192 return (0); 5193 } 5194 } 5195 } 5196 5197 ASSERT(*vpp != NULL); 5198 5199 /* 5200 * We may have gotten here we have one of the following cases: 5201 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5202 * need to validate them. 5203 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5204 * must validate. 5205 * 5206 * Go to the server and check if the directory has changed, if 5207 * it hasn't we are done and can use the dnlc entry. 5208 */ 5209 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5210 } 5211 5212 /* 5213 * Go to the server and check if the directory has changed, if 5214 * it hasn't we are done and can use the dnlc entry. If it 5215 * has changed we get a new copy of its attributes and check 5216 * the access for VEXEC, then relookup the filename and 5217 * get its filehandle and attributes. 5218 * 5219 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5220 * if the NVERIFY failed we must 5221 * purge the caches 5222 * cache new attributes (will set r_time_attr_inval) 5223 * cache new access 5224 * recheck VEXEC access 5225 * add name to dnlc, possibly negative 5226 * if LOOKUP succeeded 5227 * cache new attributes 5228 * else 5229 * set a new r_time_attr_inval for dvp 5230 * check to make sure we have access 5231 * 5232 * The vpp returned is the vnode passed in if the directory is valid, 5233 * a new vnode if successful lookup, or NULL on error. 5234 */ 5235 static int 5236 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5237 { 5238 COMPOUND4args_clnt args; 5239 COMPOUND4res_clnt res; 5240 fattr4 *ver_fattr; 5241 fattr4_change dchange; 5242 int32_t *ptr; 5243 int argoplist_size = 7 * sizeof (nfs_argop4); 5244 nfs_argop4 *argop; 5245 int doqueue; 5246 mntinfo4_t *mi; 5247 nfs4_recov_state_t recov_state; 5248 hrtime_t t; 5249 int isdotdot; 5250 vnode_t *nvp; 5251 nfs_fh4 *fhp; 5252 nfs4_sharedfh_t *sfhp; 5253 nfs4_access_type_t cacc; 5254 rnode4_t *nrp; 5255 rnode4_t *drp = VTOR4(dvp); 5256 nfs4_ga_res_t *garp = NULL; 5257 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5258 5259 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5260 ASSERT(nm != NULL); 5261 ASSERT(nm[0] != '\0'); 5262 ASSERT(dvp->v_type == VDIR); 5263 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5264 ASSERT(*vpp != NULL); 5265 5266 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5267 isdotdot = 1; 5268 args.ctag = TAG_LOOKUP_VPARENT; 5269 } else { 5270 /* 5271 * If dvp were a stub, it should have triggered and caused 5272 * a mount for us to get this far. 5273 */ 5274 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5275 5276 isdotdot = 0; 5277 args.ctag = TAG_LOOKUP_VALID; 5278 } 5279 5280 mi = VTOMI4(dvp); 5281 recov_state.rs_flags = 0; 5282 recov_state.rs_num_retry_despite_err = 0; 5283 5284 nvp = NULL; 5285 5286 /* Save the original mount point security information */ 5287 (void) save_mnt_secinfo(mi->mi_curr_serv); 5288 5289 recov_retry: 5290 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5291 &recov_state, NULL); 5292 if (e.error) { 5293 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5294 VN_RELE(*vpp); 5295 *vpp = NULL; 5296 return (e.error); 5297 } 5298 5299 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5300 5301 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5302 args.array_len = 7; 5303 args.array = argop; 5304 5305 /* 0. putfh file */ 5306 argop[0].argop = OP_CPUTFH; 5307 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5308 5309 /* 1. nverify the change info */ 5310 argop[1].argop = OP_NVERIFY; 5311 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5312 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5313 ver_fattr->attrlist4 = (char *)&dchange; 5314 ptr = (int32_t *)&dchange; 5315 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5316 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5317 5318 /* 2. getattr directory */ 5319 argop[2].argop = OP_GETATTR; 5320 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5321 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5322 5323 /* 3. access directory */ 5324 argop[3].argop = OP_ACCESS; 5325 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5326 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5327 5328 /* 4. lookup name */ 5329 if (isdotdot) { 5330 argop[4].argop = OP_LOOKUPP; 5331 } else { 5332 argop[4].argop = OP_CLOOKUP; 5333 argop[4].nfs_argop4_u.opclookup.cname = nm; 5334 } 5335 5336 /* 5. resulting file handle */ 5337 argop[5].argop = OP_GETFH; 5338 5339 /* 6. resulting file attributes */ 5340 argop[6].argop = OP_GETATTR; 5341 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5342 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5343 5344 doqueue = 1; 5345 t = gethrtime(); 5346 5347 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5348 5349 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5350 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5351 if (e.error != 0 && *vpp != NULL) 5352 VN_RELE(*vpp); 5353 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5354 &recov_state, FALSE); 5355 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5356 kmem_free(argop, argoplist_size); 5357 return (e.error); 5358 } 5359 5360 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5361 /* 5362 * For WRONGSEC of a non-dotdot case, send secinfo directly 5363 * from this thread, do not go thru the recovery thread since 5364 * we need the nm information. 5365 * 5366 * Not doing dotdot case because there is no specification 5367 * for (PUTFH, SECINFO "..") yet. 5368 */ 5369 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5370 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5371 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5372 &recov_state, FALSE); 5373 else 5374 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5375 &recov_state, TRUE); 5376 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5377 kmem_free(argop, argoplist_size); 5378 if (!e.error) 5379 goto recov_retry; 5380 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5381 VN_RELE(*vpp); 5382 *vpp = NULL; 5383 return (e.error); 5384 } 5385 5386 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5387 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5388 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5389 &recov_state, TRUE); 5390 5391 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5392 kmem_free(argop, argoplist_size); 5393 goto recov_retry; 5394 } 5395 } 5396 5397 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5398 5399 if (e.error || res.array_len == 0) { 5400 /* 5401 * If e.error isn't set, then reply has no ops (or we couldn't 5402 * be here). The only legal way to reply without an op array 5403 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5404 * be in the reply for all other status values. 5405 * 5406 * For valid replies without an ops array, return ENOTSUP 5407 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5408 * return EIO -- don't trust status. 5409 */ 5410 if (e.error == 0) 5411 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5412 ENOTSUP : EIO; 5413 VN_RELE(*vpp); 5414 *vpp = NULL; 5415 kmem_free(argop, argoplist_size); 5416 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5417 return (e.error); 5418 } 5419 5420 if (res.status != NFS4ERR_SAME) { 5421 e.error = geterrno4(res.status); 5422 5423 /* 5424 * The NVERIFY "failed" so the directory has changed 5425 * First make sure PUTFH succeeded and NVERIFY "failed" 5426 * cleanly. 5427 */ 5428 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5429 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5430 nfs4_purge_stale_fh(e.error, dvp, cr); 5431 VN_RELE(*vpp); 5432 *vpp = NULL; 5433 goto exit; 5434 } 5435 5436 /* 5437 * We know the NVERIFY "failed" so we must: 5438 * purge the caches (access and indirectly dnlc if needed) 5439 */ 5440 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5441 5442 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5443 nfs4_purge_stale_fh(e.error, dvp, cr); 5444 VN_RELE(*vpp); 5445 *vpp = NULL; 5446 goto exit; 5447 } 5448 5449 /* 5450 * Install new cached attributes for the directory 5451 */ 5452 nfs4_attr_cache(dvp, 5453 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5454 t, cr, FALSE, NULL); 5455 5456 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5457 nfs4_purge_stale_fh(e.error, dvp, cr); 5458 VN_RELE(*vpp); 5459 *vpp = NULL; 5460 e.error = geterrno4(res.status); 5461 goto exit; 5462 } 5463 5464 /* 5465 * Now we know the directory is valid, 5466 * cache new directory access 5467 */ 5468 nfs4_access_cache(drp, 5469 args.array[3].nfs_argop4_u.opaccess.access, 5470 res.array[3].nfs_resop4_u.opaccess.access, cr); 5471 5472 /* 5473 * recheck VEXEC access 5474 */ 5475 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5476 if (cacc != NFS4_ACCESS_ALLOWED) { 5477 /* 5478 * Directory permissions might have been revoked 5479 */ 5480 if (cacc == NFS4_ACCESS_DENIED) { 5481 e.error = EACCES; 5482 VN_RELE(*vpp); 5483 *vpp = NULL; 5484 goto exit; 5485 } 5486 5487 /* 5488 * Somehow we must not have asked for enough 5489 * so try a singleton ACCESS, should never happen. 5490 */ 5491 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5492 if (e.error) { 5493 VN_RELE(*vpp); 5494 *vpp = NULL; 5495 goto exit; 5496 } 5497 } 5498 5499 e.error = geterrno4(res.status); 5500 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5501 /* 5502 * The lookup failed, probably no entry 5503 */ 5504 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5505 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5506 } else { 5507 /* 5508 * Might be some other error, so remove 5509 * the dnlc entry to make sure we start all 5510 * over again, next time. 5511 */ 5512 dnlc_remove(dvp, nm); 5513 } 5514 VN_RELE(*vpp); 5515 *vpp = NULL; 5516 goto exit; 5517 } 5518 5519 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5520 /* 5521 * The file exists but we can't get its fh for 5522 * some unknown reason. Remove it from the dnlc 5523 * and error out to be safe. 5524 */ 5525 dnlc_remove(dvp, nm); 5526 VN_RELE(*vpp); 5527 *vpp = NULL; 5528 goto exit; 5529 } 5530 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5531 if (fhp->nfs_fh4_len == 0) { 5532 /* 5533 * The file exists but a bogus fh 5534 * some unknown reason. Remove it from the dnlc 5535 * and error out to be safe. 5536 */ 5537 e.error = ENOENT; 5538 dnlc_remove(dvp, nm); 5539 VN_RELE(*vpp); 5540 *vpp = NULL; 5541 goto exit; 5542 } 5543 sfhp = sfh4_get(fhp, mi); 5544 5545 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5546 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5547 5548 /* 5549 * Make the new rnode 5550 */ 5551 if (isdotdot) { 5552 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5553 if (e.error) { 5554 sfh4_rele(&sfhp); 5555 VN_RELE(*vpp); 5556 *vpp = NULL; 5557 goto exit; 5558 } 5559 /* 5560 * XXX if nfs4_make_dotdot uses an existing rnode 5561 * XXX it doesn't update the attributes. 5562 * XXX for now just save them again to save an OTW 5563 */ 5564 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5565 } else { 5566 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5567 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 5568 /* 5569 * If v_type == VNON, then garp was NULL because 5570 * the last op in the compound failed and makenfs4node 5571 * could not find the vnode for sfhp. It created 5572 * a new vnode, so we have nothing to purge here. 5573 */ 5574 if (nvp->v_type == VNON) { 5575 vattr_t vattr; 5576 5577 vattr.va_mask = AT_TYPE; 5578 /* 5579 * N.B. We've already called nfs4_end_fop above. 5580 */ 5581 e.error = nfs4getattr(nvp, &vattr, cr); 5582 if (e.error) { 5583 sfh4_rele(&sfhp); 5584 VN_RELE(*vpp); 5585 *vpp = NULL; 5586 VN_RELE(nvp); 5587 goto exit; 5588 } 5589 nvp->v_type = vattr.va_type; 5590 } 5591 } 5592 sfh4_rele(&sfhp); 5593 5594 nrp = VTOR4(nvp); 5595 mutex_enter(&nrp->r_statev4_lock); 5596 if (!nrp->created_v4) { 5597 mutex_exit(&nrp->r_statev4_lock); 5598 dnlc_update(dvp, nm, nvp); 5599 } else 5600 mutex_exit(&nrp->r_statev4_lock); 5601 5602 VN_RELE(*vpp); 5603 *vpp = nvp; 5604 } else { 5605 hrtime_t now; 5606 hrtime_t delta = 0; 5607 5608 e.error = 0; 5609 5610 /* 5611 * Because the NVERIFY "succeeded" we know that the 5612 * directory attributes are still valid 5613 * so update r_time_attr_inval 5614 */ 5615 now = gethrtime(); 5616 mutex_enter(&drp->r_statelock); 5617 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5618 delta = now - drp->r_time_attr_saved; 5619 if (delta < mi->mi_acdirmin) 5620 delta = mi->mi_acdirmin; 5621 else if (delta > mi->mi_acdirmax) 5622 delta = mi->mi_acdirmax; 5623 } 5624 drp->r_time_attr_inval = now + delta; 5625 mutex_exit(&drp->r_statelock); 5626 dnlc_update(dvp, nm, *vpp); 5627 5628 /* 5629 * Even though we have a valid directory attr cache 5630 * and dnlc entry, we may not have access. 5631 * This should almost always hit the cache. 5632 */ 5633 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5634 if (e.error) { 5635 VN_RELE(*vpp); 5636 *vpp = NULL; 5637 } 5638 5639 if (*vpp == DNLC_NO_VNODE) { 5640 VN_RELE(*vpp); 5641 *vpp = NULL; 5642 e.error = ENOENT; 5643 } 5644 } 5645 5646 exit: 5647 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5648 kmem_free(argop, argoplist_size); 5649 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5650 return (e.error); 5651 } 5652 5653 /* 5654 * We need to go over the wire to lookup the name, but 5655 * while we are there verify the directory has not 5656 * changed but if it has, get new attributes and check access 5657 * 5658 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5659 * NVERIFY GETATTR ACCESS 5660 * 5661 * With the results: 5662 * if the NVERIFY failed we must purge the caches, add new attributes, 5663 * and cache new access. 5664 * set a new r_time_attr_inval 5665 * add name to dnlc, possibly negative 5666 * if LOOKUP succeeded 5667 * cache new attributes 5668 */ 5669 static int 5670 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5671 { 5672 COMPOUND4args_clnt args; 5673 COMPOUND4res_clnt res; 5674 fattr4 *ver_fattr; 5675 fattr4_change dchange; 5676 int32_t *ptr; 5677 nfs4_ga_res_t *garp = NULL; 5678 int argoplist_size = 9 * sizeof (nfs_argop4); 5679 nfs_argop4 *argop; 5680 int doqueue; 5681 mntinfo4_t *mi; 5682 nfs4_recov_state_t recov_state; 5683 hrtime_t t; 5684 int isdotdot; 5685 vnode_t *nvp; 5686 nfs_fh4 *fhp; 5687 nfs4_sharedfh_t *sfhp; 5688 nfs4_access_type_t cacc; 5689 rnode4_t *nrp; 5690 rnode4_t *drp = VTOR4(dvp); 5691 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5692 5693 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5694 ASSERT(nm != NULL); 5695 ASSERT(nm[0] != '\0'); 5696 ASSERT(dvp->v_type == VDIR); 5697 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5698 ASSERT(*vpp == NULL); 5699 5700 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5701 isdotdot = 1; 5702 args.ctag = TAG_LOOKUP_PARENT; 5703 } else { 5704 /* 5705 * If dvp were a stub, it should have triggered and caused 5706 * a mount for us to get this far. 5707 */ 5708 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5709 5710 isdotdot = 0; 5711 args.ctag = TAG_LOOKUP; 5712 } 5713 5714 mi = VTOMI4(dvp); 5715 recov_state.rs_flags = 0; 5716 recov_state.rs_num_retry_despite_err = 0; 5717 5718 nvp = NULL; 5719 5720 /* Save the original mount point security information */ 5721 (void) save_mnt_secinfo(mi->mi_curr_serv); 5722 5723 recov_retry: 5724 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5725 &recov_state, NULL); 5726 if (e.error) { 5727 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5728 return (e.error); 5729 } 5730 5731 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5732 5733 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5734 args.array_len = 9; 5735 args.array = argop; 5736 5737 /* 0. putfh file */ 5738 argop[0].argop = OP_CPUTFH; 5739 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5740 5741 /* 1. savefh for the nverify */ 5742 argop[1].argop = OP_SAVEFH; 5743 5744 /* 2. lookup name */ 5745 if (isdotdot) { 5746 argop[2].argop = OP_LOOKUPP; 5747 } else { 5748 argop[2].argop = OP_CLOOKUP; 5749 argop[2].nfs_argop4_u.opclookup.cname = nm; 5750 } 5751 5752 /* 3. resulting file handle */ 5753 argop[3].argop = OP_GETFH; 5754 5755 /* 4. resulting file attributes */ 5756 argop[4].argop = OP_GETATTR; 5757 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5758 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5759 5760 /* 5. restorefh back the directory for the nverify */ 5761 argop[5].argop = OP_RESTOREFH; 5762 5763 /* 6. nverify the change info */ 5764 argop[6].argop = OP_NVERIFY; 5765 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5766 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5767 ver_fattr->attrlist4 = (char *)&dchange; 5768 ptr = (int32_t *)&dchange; 5769 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5770 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5771 5772 /* 7. getattr directory */ 5773 argop[7].argop = OP_GETATTR; 5774 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5775 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5776 5777 /* 8. access directory */ 5778 argop[8].argop = OP_ACCESS; 5779 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5780 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5781 5782 doqueue = 1; 5783 t = gethrtime(); 5784 5785 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5786 5787 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5788 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5789 if (e.error != 0 && *vpp != NULL) 5790 VN_RELE(*vpp); 5791 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5792 &recov_state, FALSE); 5793 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5794 kmem_free(argop, argoplist_size); 5795 return (e.error); 5796 } 5797 5798 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5799 /* 5800 * For WRONGSEC of a non-dotdot case, send secinfo directly 5801 * from this thread, do not go thru the recovery thread since 5802 * we need the nm information. 5803 * 5804 * Not doing dotdot case because there is no specification 5805 * for (PUTFH, SECINFO "..") yet. 5806 */ 5807 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5808 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5809 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5810 &recov_state, FALSE); 5811 else 5812 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5813 &recov_state, TRUE); 5814 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5815 kmem_free(argop, argoplist_size); 5816 if (!e.error) 5817 goto recov_retry; 5818 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5819 return (e.error); 5820 } 5821 5822 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5823 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5824 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5825 &recov_state, TRUE); 5826 5827 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5828 kmem_free(argop, argoplist_size); 5829 goto recov_retry; 5830 } 5831 } 5832 5833 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5834 5835 if (e.error || res.array_len == 0) { 5836 /* 5837 * If e.error isn't set, then reply has no ops (or we couldn't 5838 * be here). The only legal way to reply without an op array 5839 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5840 * be in the reply for all other status values. 5841 * 5842 * For valid replies without an ops array, return ENOTSUP 5843 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5844 * return EIO -- don't trust status. 5845 */ 5846 if (e.error == 0) 5847 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5848 ENOTSUP : EIO; 5849 5850 kmem_free(argop, argoplist_size); 5851 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5852 return (e.error); 5853 } 5854 5855 e.error = geterrno4(res.status); 5856 5857 /* 5858 * The PUTFH and SAVEFH may have failed. 5859 */ 5860 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5861 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5862 nfs4_purge_stale_fh(e.error, dvp, cr); 5863 goto exit; 5864 } 5865 5866 /* 5867 * Check if the file exists, if it does delay entering 5868 * into the dnlc until after we update the directory 5869 * attributes so we don't cause it to get purged immediately. 5870 */ 5871 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5872 /* 5873 * The lookup failed, probably no entry 5874 */ 5875 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5876 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5877 goto exit; 5878 } 5879 5880 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5881 /* 5882 * The file exists but we can't get its fh for 5883 * some unknown reason. Error out to be safe. 5884 */ 5885 goto exit; 5886 } 5887 5888 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5889 if (fhp->nfs_fh4_len == 0) { 5890 /* 5891 * The file exists but a bogus fh 5892 * some unknown reason. Error out to be safe. 5893 */ 5894 e.error = EIO; 5895 goto exit; 5896 } 5897 sfhp = sfh4_get(fhp, mi); 5898 5899 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5900 sfh4_rele(&sfhp); 5901 goto exit; 5902 } 5903 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5904 5905 /* 5906 * The RESTOREFH may have failed 5907 */ 5908 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5909 sfh4_rele(&sfhp); 5910 e.error = EIO; 5911 goto exit; 5912 } 5913 5914 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5915 /* 5916 * First make sure the NVERIFY failed as we expected, 5917 * if it didn't then be conservative and error out 5918 * as we can't trust the directory. 5919 */ 5920 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5921 sfh4_rele(&sfhp); 5922 e.error = EIO; 5923 goto exit; 5924 } 5925 5926 /* 5927 * We know the NVERIFY "failed" so the directory has changed, 5928 * so we must: 5929 * purge the caches (access and indirectly dnlc if needed) 5930 */ 5931 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5932 5933 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5934 sfh4_rele(&sfhp); 5935 goto exit; 5936 } 5937 nfs4_attr_cache(dvp, 5938 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5939 t, cr, FALSE, NULL); 5940 5941 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5942 nfs4_purge_stale_fh(e.error, dvp, cr); 5943 sfh4_rele(&sfhp); 5944 e.error = geterrno4(res.status); 5945 goto exit; 5946 } 5947 5948 /* 5949 * Now we know the directory is valid, 5950 * cache new directory access 5951 */ 5952 nfs4_access_cache(drp, 5953 args.array[8].nfs_argop4_u.opaccess.access, 5954 res.array[8].nfs_resop4_u.opaccess.access, cr); 5955 5956 /* 5957 * recheck VEXEC access 5958 */ 5959 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5960 if (cacc != NFS4_ACCESS_ALLOWED) { 5961 /* 5962 * Directory permissions might have been revoked 5963 */ 5964 if (cacc == NFS4_ACCESS_DENIED) { 5965 sfh4_rele(&sfhp); 5966 e.error = EACCES; 5967 goto exit; 5968 } 5969 5970 /* 5971 * Somehow we must not have asked for enough 5972 * so try a singleton ACCESS should never happen 5973 */ 5974 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5975 if (e.error) { 5976 sfh4_rele(&sfhp); 5977 goto exit; 5978 } 5979 } 5980 5981 e.error = geterrno4(res.status); 5982 } else { 5983 hrtime_t now; 5984 hrtime_t delta = 0; 5985 5986 e.error = 0; 5987 5988 /* 5989 * Because the NVERIFY "succeeded" we know that the 5990 * directory attributes are still valid 5991 * so update r_time_attr_inval 5992 */ 5993 now = gethrtime(); 5994 mutex_enter(&drp->r_statelock); 5995 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5996 delta = now - drp->r_time_attr_saved; 5997 if (delta < mi->mi_acdirmin) 5998 delta = mi->mi_acdirmin; 5999 else if (delta > mi->mi_acdirmax) 6000 delta = mi->mi_acdirmax; 6001 } 6002 drp->r_time_attr_inval = now + delta; 6003 mutex_exit(&drp->r_statelock); 6004 6005 /* 6006 * Even though we have a valid directory attr cache, 6007 * we may not have access. 6008 * This should almost always hit the cache. 6009 */ 6010 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 6011 if (e.error) { 6012 sfh4_rele(&sfhp); 6013 goto exit; 6014 } 6015 } 6016 6017 /* 6018 * Now we have successfully completed the lookup, if the 6019 * directory has changed we now have the valid attributes. 6020 * We also know we have directory access. 6021 * Create the new rnode and insert it in the dnlc. 6022 */ 6023 if (isdotdot) { 6024 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 6025 if (e.error) { 6026 sfh4_rele(&sfhp); 6027 goto exit; 6028 } 6029 /* 6030 * XXX if nfs4_make_dotdot uses an existing rnode 6031 * XXX it doesn't update the attributes. 6032 * XXX for now just save them again to save an OTW 6033 */ 6034 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 6035 } else { 6036 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 6037 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 6038 } 6039 sfh4_rele(&sfhp); 6040 6041 nrp = VTOR4(nvp); 6042 mutex_enter(&nrp->r_statev4_lock); 6043 if (!nrp->created_v4) { 6044 mutex_exit(&nrp->r_statev4_lock); 6045 dnlc_update(dvp, nm, nvp); 6046 } else 6047 mutex_exit(&nrp->r_statev4_lock); 6048 6049 *vpp = nvp; 6050 6051 exit: 6052 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6053 kmem_free(argop, argoplist_size); 6054 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 6055 return (e.error); 6056 } 6057 6058 #ifdef DEBUG 6059 void 6060 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 6061 { 6062 uint_t i, len; 6063 zoneid_t zoneid = getzoneid(); 6064 char *s; 6065 6066 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 6067 for (i = 0; i < argcnt; i++) { 6068 nfs_argop4 *op = &argbase[i]; 6069 switch (op->argop) { 6070 case OP_CPUTFH: 6071 case OP_PUTFH: 6072 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 6073 break; 6074 case OP_PUTROOTFH: 6075 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 6076 break; 6077 case OP_CLOOKUP: 6078 s = op->nfs_argop4_u.opclookup.cname; 6079 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6080 break; 6081 case OP_LOOKUP: 6082 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 6083 &len, NULL); 6084 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6085 kmem_free(s, len); 6086 break; 6087 case OP_LOOKUPP: 6088 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 6089 break; 6090 case OP_GETFH: 6091 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 6092 break; 6093 case OP_GETATTR: 6094 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 6095 break; 6096 case OP_OPENATTR: 6097 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 6098 break; 6099 default: 6100 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 6101 op->argop); 6102 break; 6103 } 6104 } 6105 } 6106 #endif 6107 6108 /* 6109 * nfs4lookup_setup - constructs a multi-lookup compound request. 6110 * 6111 * Given the path "nm1/nm2/.../nmn", the following compound requests 6112 * may be created: 6113 * 6114 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 6115 * is faster, for now. 6116 * 6117 * l4_getattrs indicates the type of compound requested. 6118 * 6119 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 6120 * 6121 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 6122 * 6123 * total number of ops is n + 1. 6124 * 6125 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 6126 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 6127 * before the last component, and only get attributes 6128 * for the last component. Note that the second-to-last 6129 * pathname component is XATTR_RPATH, which does NOT go 6130 * over-the-wire as a lookup. 6131 * 6132 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 6133 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 6134 * 6135 * and total number of ops is n + 5. 6136 * 6137 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 6138 * attribute directory: create lookups plus an OPENATTR 6139 * replacing the last lookup. Note that the last pathname 6140 * component is XATTR_RPATH, which does NOT go over-the-wire 6141 * as a lookup. 6142 * 6143 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 6144 * Openattr; Getfh; Getattr } 6145 * 6146 * and total number of ops is n + 5. 6147 * 6148 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6149 * nodes too. 6150 * 6151 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6152 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6153 * 6154 * and total number of ops is 3*n + 1. 6155 * 6156 * All cases: returns the index in the arg array of the final LOOKUP op, or 6157 * -1 if no LOOKUPs were used. 6158 */ 6159 int 6160 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6161 { 6162 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6163 nfs_argop4 *argbase, *argop; 6164 int arglen, argcnt; 6165 int n = 1; /* number of components */ 6166 int nga = 1; /* number of Getattr's in request */ 6167 char c = '\0', *s, *p; 6168 int lookup_idx = -1; 6169 int argoplist_size; 6170 6171 /* set lookuparg response result to 0 */ 6172 lookupargp->resp->status = NFS4_OK; 6173 6174 /* skip leading "/" or "." e.g. ".//./" if there is */ 6175 for (; ; nm++) { 6176 if (*nm != '/' && *nm != '.') 6177 break; 6178 6179 /* ".." is counted as 1 component */ 6180 if (*nm == '.' && *(nm + 1) != '/') 6181 break; 6182 } 6183 6184 /* 6185 * Find n = number of components - nm must be null terminated 6186 * Skip "." components. 6187 */ 6188 if (*nm != '\0') 6189 for (n = 1, s = nm; *s != '\0'; s++) { 6190 if ((*s == '/') && (*(s + 1) != '/') && 6191 (*(s + 1) != '\0') && 6192 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6193 *(s + 2) == '\0'))) 6194 n++; 6195 } 6196 else 6197 n = 0; 6198 6199 /* 6200 * nga is number of components that need Getfh+Getattr 6201 */ 6202 switch (l4_getattrs) { 6203 case LKP4_NO_ATTRIBUTES: 6204 nga = 0; 6205 break; 6206 case LKP4_ALL_ATTRIBUTES: 6207 nga = n; 6208 /* 6209 * Always have at least 1 getfh, getattr pair 6210 */ 6211 if (nga == 0) 6212 nga++; 6213 break; 6214 case LKP4_LAST_ATTRDIR: 6215 case LKP4_LAST_NAMED_ATTR: 6216 nga = n+1; 6217 break; 6218 } 6219 6220 /* 6221 * If change to use the filehandle attr instead of getfh 6222 * the following line can be deleted. 6223 */ 6224 nga *= 2; 6225 6226 /* 6227 * calculate number of ops in request as 6228 * header + trailer + lookups + getattrs 6229 */ 6230 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6231 6232 argoplist_size = arglen * sizeof (nfs_argop4); 6233 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6234 lookupargp->argsp->array = argop; 6235 6236 argcnt = lookupargp->header_len; 6237 argop += argcnt; 6238 6239 /* 6240 * loop and create a lookup op and possibly getattr/getfh for 6241 * each component. Skip "." components. 6242 */ 6243 for (s = nm; *s != '\0'; s = p) { 6244 /* 6245 * Set up a pathname struct for each component if needed 6246 */ 6247 while (*s == '/') 6248 s++; 6249 if (*s == '\0') 6250 break; 6251 6252 for (p = s; (*p != '/') && (*p != '\0'); p++) 6253 ; 6254 c = *p; 6255 *p = '\0'; 6256 6257 if (s[0] == '.' && s[1] == '\0') { 6258 *p = c; 6259 continue; 6260 } 6261 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6262 strcmp(s, XATTR_RPATH) == 0) { 6263 /* getfh XXX may not be needed in future */ 6264 argop->argop = OP_GETFH; 6265 argop++; 6266 argcnt++; 6267 6268 /* getattr */ 6269 argop->argop = OP_GETATTR; 6270 argop->nfs_argop4_u.opgetattr.attr_request = 6271 lookupargp->ga_bits; 6272 argop->nfs_argop4_u.opgetattr.mi = 6273 lookupargp->mi; 6274 argop++; 6275 argcnt++; 6276 6277 /* openattr */ 6278 argop->argop = OP_OPENATTR; 6279 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6280 strcmp(s, XATTR_RPATH) == 0) { 6281 /* openattr */ 6282 argop->argop = OP_OPENATTR; 6283 argop++; 6284 argcnt++; 6285 6286 /* getfh XXX may not be needed in future */ 6287 argop->argop = OP_GETFH; 6288 argop++; 6289 argcnt++; 6290 6291 /* getattr */ 6292 argop->argop = OP_GETATTR; 6293 argop->nfs_argop4_u.opgetattr.attr_request = 6294 lookupargp->ga_bits; 6295 argop->nfs_argop4_u.opgetattr.mi = 6296 lookupargp->mi; 6297 argop++; 6298 argcnt++; 6299 *p = c; 6300 continue; 6301 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6302 /* lookupp */ 6303 argop->argop = OP_LOOKUPP; 6304 } else { 6305 /* lookup */ 6306 argop->argop = OP_LOOKUP; 6307 (void) str_to_utf8(s, 6308 &argop->nfs_argop4_u.oplookup.objname); 6309 } 6310 lookup_idx = argcnt; 6311 argop++; 6312 argcnt++; 6313 6314 *p = c; 6315 6316 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6317 /* getfh XXX may not be needed in future */ 6318 argop->argop = OP_GETFH; 6319 argop++; 6320 argcnt++; 6321 6322 /* getattr */ 6323 argop->argop = OP_GETATTR; 6324 argop->nfs_argop4_u.opgetattr.attr_request = 6325 lookupargp->ga_bits; 6326 argop->nfs_argop4_u.opgetattr.mi = 6327 lookupargp->mi; 6328 argop++; 6329 argcnt++; 6330 } 6331 } 6332 6333 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6334 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6335 if (needgetfh) { 6336 /* stick in a post-lookup getfh */ 6337 argop->argop = OP_GETFH; 6338 argcnt++; 6339 argop++; 6340 } 6341 /* post-lookup getattr */ 6342 argop->argop = OP_GETATTR; 6343 argop->nfs_argop4_u.opgetattr.attr_request = 6344 lookupargp->ga_bits; 6345 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6346 argcnt++; 6347 } 6348 argcnt += lookupargp->trailer_len; /* actual op count */ 6349 lookupargp->argsp->array_len = argcnt; 6350 lookupargp->arglen = arglen; 6351 6352 #ifdef DEBUG 6353 if (nfs4_client_lookup_debug) 6354 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6355 #endif 6356 6357 return (lookup_idx); 6358 } 6359 6360 static int 6361 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6362 { 6363 COMPOUND4args_clnt args; 6364 COMPOUND4res_clnt res; 6365 GETFH4res *gf_res = NULL; 6366 nfs_argop4 argop[4]; 6367 nfs_resop4 *resop = NULL; 6368 nfs4_sharedfh_t *sfhp; 6369 hrtime_t t; 6370 nfs4_error_t e; 6371 6372 rnode4_t *drp; 6373 int doqueue = 1; 6374 vnode_t *vp; 6375 int needrecov = 0; 6376 nfs4_recov_state_t recov_state; 6377 6378 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6379 6380 *avp = NULL; 6381 recov_state.rs_flags = 0; 6382 recov_state.rs_num_retry_despite_err = 0; 6383 6384 recov_retry: 6385 /* COMPOUND: putfh, openattr, getfh, getattr */ 6386 args.array_len = 4; 6387 args.array = argop; 6388 args.ctag = TAG_OPENATTR; 6389 6390 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6391 if (e.error) 6392 return (e.error); 6393 6394 drp = VTOR4(dvp); 6395 6396 /* putfh */ 6397 argop[0].argop = OP_CPUTFH; 6398 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6399 6400 /* openattr */ 6401 argop[1].argop = OP_OPENATTR; 6402 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6403 6404 /* getfh */ 6405 argop[2].argop = OP_GETFH; 6406 6407 /* getattr */ 6408 argop[3].argop = OP_GETATTR; 6409 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6410 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6411 6412 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6413 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6414 rnode4info(drp))); 6415 6416 t = gethrtime(); 6417 6418 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6419 6420 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6421 if (needrecov) { 6422 bool_t abort; 6423 6424 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6425 "nfs4openattr: initiating recovery\n")); 6426 6427 abort = nfs4_start_recovery(&e, 6428 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6429 OP_OPENATTR, NULL, NULL, NULL); 6430 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6431 if (!e.error) { 6432 e.error = geterrno4(res.status); 6433 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6434 } 6435 if (abort == FALSE) 6436 goto recov_retry; 6437 return (e.error); 6438 } 6439 6440 if (e.error) { 6441 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6442 return (e.error); 6443 } 6444 6445 if (res.status) { 6446 /* 6447 * If OTW errro is NOTSUPP, then it should be 6448 * translated to EINVAL. All Solaris file system 6449 * implementations return EINVAL to the syscall layer 6450 * when the attrdir cannot be created due to an 6451 * implementation restriction or noxattr mount option. 6452 */ 6453 if (res.status == NFS4ERR_NOTSUPP) { 6454 mutex_enter(&drp->r_statelock); 6455 if (drp->r_xattr_dir) 6456 VN_RELE(drp->r_xattr_dir); 6457 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6458 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6459 mutex_exit(&drp->r_statelock); 6460 6461 e.error = EINVAL; 6462 } else { 6463 e.error = geterrno4(res.status); 6464 } 6465 6466 if (e.error) { 6467 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6468 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6469 needrecov); 6470 return (e.error); 6471 } 6472 } 6473 6474 resop = &res.array[0]; /* putfh res */ 6475 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6476 6477 resop = &res.array[1]; /* openattr res */ 6478 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6479 6480 resop = &res.array[2]; /* getfh res */ 6481 gf_res = &resop->nfs_resop4_u.opgetfh; 6482 if (gf_res->object.nfs_fh4_len == 0) { 6483 *avp = NULL; 6484 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6485 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6486 return (ENOENT); 6487 } 6488 6489 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6490 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6491 dvp->v_vfsp, t, cr, dvp, 6492 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp)); 6493 sfh4_rele(&sfhp); 6494 6495 if (e.error) 6496 PURGE_ATTRCACHE4(vp); 6497 6498 mutex_enter(&vp->v_lock); 6499 vp->v_flag |= V_XATTRDIR; 6500 mutex_exit(&vp->v_lock); 6501 6502 *avp = vp; 6503 6504 mutex_enter(&drp->r_statelock); 6505 if (drp->r_xattr_dir) 6506 VN_RELE(drp->r_xattr_dir); 6507 VN_HOLD(vp); 6508 drp->r_xattr_dir = vp; 6509 6510 /* 6511 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6512 * NULL. xattrs could be created at any time, and we have no 6513 * way to update pc4_xattr_exists in the base object if/when 6514 * it happens. 6515 */ 6516 drp->r_pathconf.pc4_xattr_valid = 0; 6517 6518 mutex_exit(&drp->r_statelock); 6519 6520 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6521 6522 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6523 6524 return (0); 6525 } 6526 6527 /* ARGSUSED */ 6528 static int 6529 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6530 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct, 6531 vsecattr_t *vsecp) 6532 { 6533 int error; 6534 vnode_t *vp = NULL; 6535 rnode4_t *rp; 6536 struct vattr vattr; 6537 rnode4_t *drp; 6538 vnode_t *tempvp; 6539 enum createmode4 createmode; 6540 bool_t must_trunc = FALSE; 6541 int truncating = 0; 6542 6543 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6544 return (EPERM); 6545 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6546 return (EINVAL); 6547 } 6548 6549 /* . and .. have special meaning in the protocol, reject them. */ 6550 6551 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6552 return (EISDIR); 6553 6554 drp = VTOR4(dvp); 6555 6556 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6557 return (EINTR); 6558 6559 top: 6560 /* 6561 * We make a copy of the attributes because the caller does not 6562 * expect us to change what va points to. 6563 */ 6564 vattr = *va; 6565 6566 /* 6567 * If the pathname is "", then dvp is the root vnode of 6568 * a remote file mounted over a local directory. 6569 * All that needs to be done is access 6570 * checking and truncation. Note that we avoid doing 6571 * open w/ create because the parent directory might 6572 * be in pseudo-fs and the open would fail. 6573 */ 6574 if (*nm == '\0') { 6575 error = 0; 6576 VN_HOLD(dvp); 6577 vp = dvp; 6578 must_trunc = TRUE; 6579 } else { 6580 /* 6581 * We need to go over the wire, just to be sure whether the 6582 * file exists or not. Using the DNLC can be dangerous in 6583 * this case when making a decision regarding existence. 6584 */ 6585 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6586 } 6587 6588 if (exclusive) 6589 createmode = EXCLUSIVE4; 6590 else 6591 createmode = GUARDED4; 6592 6593 /* 6594 * error would be set if the file does not exist on the 6595 * server, so lets go create it. 6596 */ 6597 if (error) { 6598 goto create_otw; 6599 } 6600 6601 /* 6602 * File does exist on the server 6603 */ 6604 if (exclusive == EXCL) 6605 error = EEXIST; 6606 else if (vp->v_type == VDIR && (mode & VWRITE)) 6607 error = EISDIR; 6608 else { 6609 /* 6610 * If vnode is a device, create special vnode. 6611 */ 6612 if (ISVDEV(vp->v_type)) { 6613 tempvp = vp; 6614 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6615 VN_RELE(tempvp); 6616 } 6617 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) { 6618 if ((vattr.va_mask & AT_SIZE) && 6619 vp->v_type == VREG) { 6620 rp = VTOR4(vp); 6621 /* 6622 * Check here for large file handled 6623 * by LF-unaware process (as 6624 * ufs_create() does) 6625 */ 6626 if (!(flags & FOFFMAX)) { 6627 mutex_enter(&rp->r_statelock); 6628 if (rp->r_size > MAXOFF32_T) 6629 error = EOVERFLOW; 6630 mutex_exit(&rp->r_statelock); 6631 } 6632 6633 /* if error is set then we need to return */ 6634 if (error) { 6635 nfs_rw_exit(&drp->r_rwlock); 6636 VN_RELE(vp); 6637 return (error); 6638 } 6639 6640 if (must_trunc) { 6641 vattr.va_mask = AT_SIZE; 6642 error = nfs4setattr(vp, &vattr, 0, cr, 6643 NULL); 6644 } else { 6645 /* 6646 * we know we have a regular file that already 6647 * exists and we may end up truncating the file 6648 * as a result of the open_otw, so flush out 6649 * any dirty pages for this file first. 6650 */ 6651 if (nfs4_has_pages(vp) && 6652 ((rp->r_flags & R4DIRTY) || 6653 rp->r_count > 0 || 6654 rp->r_mapcnt > 0)) { 6655 error = nfs4_putpage(vp, 6656 (offset_t)0, 0, 0, cr, ct); 6657 if (error && (error == ENOSPC || 6658 error == EDQUOT)) { 6659 mutex_enter( 6660 &rp->r_statelock); 6661 if (!rp->r_error) 6662 rp->r_error = 6663 error; 6664 mutex_exit( 6665 &rp->r_statelock); 6666 } 6667 } 6668 vattr.va_mask = (AT_SIZE | 6669 AT_TYPE | AT_MODE); 6670 vattr.va_type = VREG; 6671 createmode = UNCHECKED4; 6672 truncating = 1; 6673 goto create_otw; 6674 } 6675 } 6676 } 6677 } 6678 nfs_rw_exit(&drp->r_rwlock); 6679 if (error) { 6680 VN_RELE(vp); 6681 } else { 6682 vnode_t *tvp; 6683 rnode4_t *trp; 6684 tvp = vp; 6685 if (vp->v_type == VREG) { 6686 trp = VTOR4(vp); 6687 if (IS_SHADOW(vp, trp)) 6688 tvp = RTOV4(trp); 6689 } 6690 6691 if (must_trunc) { 6692 /* 6693 * existing file got truncated, notify. 6694 */ 6695 vnevent_create(tvp, ct); 6696 } 6697 6698 *vpp = vp; 6699 } 6700 return (error); 6701 6702 create_otw: 6703 dnlc_remove(dvp, nm); 6704 6705 ASSERT(vattr.va_mask & AT_TYPE); 6706 6707 /* 6708 * If not a regular file let nfs4mknod() handle it. 6709 */ 6710 if (vattr.va_type != VREG) { 6711 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6712 nfs_rw_exit(&drp->r_rwlock); 6713 return (error); 6714 } 6715 6716 /* 6717 * It _is_ a regular file. 6718 */ 6719 ASSERT(vattr.va_mask & AT_MODE); 6720 if (MANDMODE(vattr.va_mode)) { 6721 nfs_rw_exit(&drp->r_rwlock); 6722 return (EACCES); 6723 } 6724 6725 /* 6726 * If this happens to be a mknod of a regular file, then flags will 6727 * have neither FREAD or FWRITE. However, we must set at least one 6728 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6729 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6730 * set (based on openmode specified by app). 6731 */ 6732 if ((flags & (FREAD|FWRITE)) == 0) 6733 flags |= (FREAD|FWRITE); 6734 6735 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6736 6737 if (vp != NULL) { 6738 /* if create was successful, throw away the file's pages */ 6739 if (!error && (vattr.va_mask & AT_SIZE)) 6740 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6741 cr); 6742 /* release the lookup hold */ 6743 VN_RELE(vp); 6744 vp = NULL; 6745 } 6746 6747 /* 6748 * validate that we opened a regular file. This handles a misbehaving 6749 * server that returns an incorrect FH. 6750 */ 6751 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6752 error = EISDIR; 6753 VN_RELE(*vpp); 6754 } 6755 6756 /* 6757 * If this is not an exclusive create, then the CREATE 6758 * request will be made with the GUARDED mode set. This 6759 * means that the server will return EEXIST if the file 6760 * exists. The file could exist because of a retransmitted 6761 * request. In this case, we recover by starting over and 6762 * checking to see whether the file exists. This second 6763 * time through it should and a CREATE request will not be 6764 * sent. 6765 * 6766 * This handles the problem of a dangling CREATE request 6767 * which contains attributes which indicate that the file 6768 * should be truncated. This retransmitted request could 6769 * possibly truncate valid data in the file if not caught 6770 * by the duplicate request mechanism on the server or if 6771 * not caught by other means. The scenario is: 6772 * 6773 * Client transmits CREATE request with size = 0 6774 * Client times out, retransmits request. 6775 * Response to the first request arrives from the server 6776 * and the client proceeds on. 6777 * Client writes data to the file. 6778 * The server now processes retransmitted CREATE request 6779 * and truncates file. 6780 * 6781 * The use of the GUARDED CREATE request prevents this from 6782 * happening because the retransmitted CREATE would fail 6783 * with EEXIST and would not truncate the file. 6784 */ 6785 if (error == EEXIST && exclusive == NONEXCL) { 6786 #ifdef DEBUG 6787 nfs4_create_misses++; 6788 #endif 6789 goto top; 6790 } 6791 nfs_rw_exit(&drp->r_rwlock); 6792 if (truncating && !error && *vpp) { 6793 vnode_t *tvp; 6794 rnode4_t *trp; 6795 /* 6796 * existing file got truncated, notify. 6797 */ 6798 tvp = *vpp; 6799 trp = VTOR4(tvp); 6800 if (IS_SHADOW(tvp, trp)) 6801 tvp = RTOV4(trp); 6802 vnevent_create(tvp, ct); 6803 } 6804 return (error); 6805 } 6806 6807 /* 6808 * Create compound (for mkdir, mknod, symlink): 6809 * { Putfh <dfh>; Create; Getfh; Getattr } 6810 * It's okay if setattr failed to set gid - this is not considered 6811 * an error, but purge attrs in that case. 6812 */ 6813 static int 6814 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6815 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6816 { 6817 int need_end_op = FALSE; 6818 COMPOUND4args_clnt args; 6819 COMPOUND4res_clnt res, *resp = NULL; 6820 nfs_argop4 *argop; 6821 nfs_resop4 *resop; 6822 int doqueue; 6823 mntinfo4_t *mi; 6824 rnode4_t *drp = VTOR4(dvp); 6825 change_info4 *cinfo; 6826 GETFH4res *gf_res; 6827 struct vattr vattr; 6828 vnode_t *vp; 6829 fattr4 *crattr; 6830 bool_t needrecov = FALSE; 6831 nfs4_recov_state_t recov_state; 6832 nfs4_sharedfh_t *sfhp = NULL; 6833 hrtime_t t; 6834 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6835 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6836 dirattr_info_t dinfo, *dinfop; 6837 servinfo4_t *svp; 6838 bitmap4 supp_attrs; 6839 6840 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6841 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6842 6843 mi = VTOMI4(dvp); 6844 6845 /* 6846 * Make sure we properly deal with setting the right gid 6847 * on a new directory to reflect the parent's setgid bit 6848 */ 6849 setgid_flag = 0; 6850 if (type == NF4DIR) { 6851 struct vattr dva; 6852 6853 va->va_mode &= ~VSGID; 6854 dva.va_mask = AT_MODE | AT_GID; 6855 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) { 6856 6857 /* 6858 * If the parent's directory has the setgid bit set 6859 * _and_ the client was able to get a valid mapping 6860 * for the parent dir's owner_group, we want to 6861 * append NVERIFY(owner_group == dva.va_gid) and 6862 * SETTATTR to the CREATE compound. 6863 */ 6864 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6865 setgid_flag = 1; 6866 va->va_mode |= VSGID; 6867 if (dva.va_gid != GID_NOBODY) { 6868 va->va_mask |= AT_GID; 6869 va->va_gid = dva.va_gid; 6870 } 6871 } 6872 } 6873 } 6874 6875 /* 6876 * Create ops: 6877 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6878 * 5:restorefh(dir) 6:getattr(dir) 6879 * 6880 * if (setgid) 6881 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6882 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6883 * 8:nverify 9:setattr 6884 */ 6885 if (setgid_flag) { 6886 numops = 10; 6887 idx_create = 1; 6888 idx_fattr = 3; 6889 } else { 6890 numops = 7; 6891 idx_create = 2; 6892 idx_fattr = 4; 6893 } 6894 6895 ASSERT(nfs_zone() == mi->mi_zone); 6896 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6897 return (EINTR); 6898 } 6899 recov_state.rs_flags = 0; 6900 recov_state.rs_num_retry_despite_err = 0; 6901 6902 argoplist_size = numops * sizeof (nfs_argop4); 6903 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6904 6905 recov_retry: 6906 if (type == NF4LNK) 6907 args.ctag = TAG_SYMLINK; 6908 else if (type == NF4DIR) 6909 args.ctag = TAG_MKDIR; 6910 else 6911 args.ctag = TAG_MKNOD; 6912 6913 args.array_len = numops; 6914 args.array = argop; 6915 6916 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6917 nfs_rw_exit(&drp->r_rwlock); 6918 kmem_free(argop, argoplist_size); 6919 return (e.error); 6920 } 6921 need_end_op = TRUE; 6922 6923 6924 /* 0: putfh directory */ 6925 argop[0].argop = OP_CPUTFH; 6926 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6927 6928 /* 1/2: Create object */ 6929 argop[idx_create].argop = OP_CCREATE; 6930 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6931 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6932 if (type == NF4LNK) { 6933 /* 6934 * symlink, treat name as data 6935 */ 6936 ASSERT(data != NULL); 6937 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6938 (char *)data; 6939 } 6940 if (type == NF4BLK || type == NF4CHR) { 6941 ASSERT(data != NULL); 6942 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6943 *((specdata4 *)data); 6944 } 6945 6946 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6947 6948 svp = drp->r_server; 6949 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6950 supp_attrs = svp->sv_supp_attrs; 6951 nfs_rw_exit(&svp->sv_lock); 6952 6953 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6954 nfs_rw_exit(&drp->r_rwlock); 6955 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6956 e.error = EINVAL; 6957 kmem_free(argop, argoplist_size); 6958 return (e.error); 6959 } 6960 6961 /* 2/3: getfh fh of created object */ 6962 ASSERT(idx_create + 1 == idx_fattr - 1); 6963 argop[idx_create + 1].argop = OP_GETFH; 6964 6965 /* 3/4: getattr of new object */ 6966 argop[idx_fattr].argop = OP_GETATTR; 6967 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6968 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6969 6970 if (setgid_flag) { 6971 vattr_t _v; 6972 6973 argop[4].argop = OP_SAVEFH; 6974 6975 argop[5].argop = OP_CPUTFH; 6976 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6977 6978 argop[6].argop = OP_GETATTR; 6979 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6980 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6981 6982 argop[7].argop = OP_RESTOREFH; 6983 6984 /* 6985 * nverify 6986 * 6987 * XXX - Revisit the last argument to nfs4_end_op() 6988 * once 5020486 is fixed. 6989 */ 6990 _v.va_mask = AT_GID; 6991 _v.va_gid = va->va_gid; 6992 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6993 supp_attrs)) { 6994 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6995 nfs_rw_exit(&drp->r_rwlock); 6996 nfs4_fattr4_free(crattr); 6997 kmem_free(argop, argoplist_size); 6998 return (e.error); 6999 } 7000 7001 /* 7002 * setattr 7003 * 7004 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 7005 * so no need for stateid or flags. Also we specify NULL 7006 * rp since we're only interested in setting owner_group 7007 * attributes. 7008 */ 7009 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 7010 &e.error, 0); 7011 7012 if (e.error) { 7013 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 7014 nfs_rw_exit(&drp->r_rwlock); 7015 nfs4_fattr4_free(crattr); 7016 nfs4args_verify_free(&argop[8]); 7017 kmem_free(argop, argoplist_size); 7018 return (e.error); 7019 } 7020 } else { 7021 argop[1].argop = OP_SAVEFH; 7022 7023 argop[5].argop = OP_RESTOREFH; 7024 7025 argop[6].argop = OP_GETATTR; 7026 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7027 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7028 } 7029 7030 dnlc_remove(dvp, nm); 7031 7032 doqueue = 1; 7033 t = gethrtime(); 7034 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7035 7036 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7037 if (e.error) { 7038 PURGE_ATTRCACHE4(dvp); 7039 if (!needrecov) 7040 goto out; 7041 } 7042 7043 if (needrecov) { 7044 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 7045 OP_CREATE, NULL, NULL, NULL) == FALSE) { 7046 nfs4_end_op(mi, dvp, NULL, &recov_state, 7047 needrecov); 7048 need_end_op = FALSE; 7049 nfs4_fattr4_free(crattr); 7050 if (setgid_flag) { 7051 nfs4args_verify_free(&argop[8]); 7052 nfs4args_setattr_free(&argop[9]); 7053 } 7054 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 7055 goto recov_retry; 7056 } 7057 } 7058 7059 resp = &res; 7060 7061 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 7062 7063 if (res.status == NFS4ERR_BADOWNER) 7064 nfs4_log_badowner(mi, OP_CREATE); 7065 7066 e.error = geterrno4(res.status); 7067 7068 /* 7069 * This check is left over from when create was implemented 7070 * using a setattr op (instead of createattrs). If the 7071 * putfh/create/getfh failed, the error was returned. If 7072 * setattr/getattr failed, we keep going. 7073 * 7074 * It might be better to get rid of the GETFH also, and just 7075 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 7076 * Then if any of the operations failed, we could return the 7077 * error now, and remove much of the error code below. 7078 */ 7079 if (res.array_len <= idx_fattr) { 7080 /* 7081 * Either Putfh, Create or Getfh failed. 7082 */ 7083 PURGE_ATTRCACHE4(dvp); 7084 /* 7085 * nfs4_purge_stale_fh() may generate otw calls through 7086 * nfs4_invalidate_pages. Hence the need to call 7087 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 7088 */ 7089 nfs4_end_op(mi, dvp, NULL, &recov_state, 7090 needrecov); 7091 need_end_op = FALSE; 7092 nfs4_purge_stale_fh(e.error, dvp, cr); 7093 goto out; 7094 } 7095 } 7096 7097 resop = &res.array[idx_create]; /* create res */ 7098 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 7099 7100 resop = &res.array[idx_create + 1]; /* getfh res */ 7101 gf_res = &resop->nfs_resop4_u.opgetfh; 7102 7103 sfhp = sfh4_get(&gf_res->object, mi); 7104 if (e.error) { 7105 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 7106 fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7107 if (vp->v_type == VNON) { 7108 vattr.va_mask = AT_TYPE; 7109 /* 7110 * Need to call nfs4_end_op before nfs4getattr to avoid 7111 * potential nfs4_start_op deadlock. See RFE 4777612. 7112 */ 7113 nfs4_end_op(mi, dvp, NULL, &recov_state, 7114 needrecov); 7115 need_end_op = FALSE; 7116 e.error = nfs4getattr(vp, &vattr, cr); 7117 if (e.error) { 7118 VN_RELE(vp); 7119 *vpp = NULL; 7120 goto out; 7121 } 7122 vp->v_type = vattr.va_type; 7123 } 7124 e.error = 0; 7125 } else { 7126 *vpp = vp = makenfs4node(sfhp, 7127 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 7128 dvp->v_vfsp, t, cr, 7129 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7130 } 7131 7132 /* 7133 * If compound succeeded, then update dir attrs 7134 */ 7135 if (res.status == NFS4_OK) { 7136 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 7137 dinfo.di_cred = cr; 7138 dinfo.di_time_call = t; 7139 dinfop = &dinfo; 7140 } else 7141 dinfop = NULL; 7142 7143 /* Update directory cache attribute, readdir and dnlc caches */ 7144 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 7145 7146 out: 7147 if (sfhp != NULL) 7148 sfh4_rele(&sfhp); 7149 nfs_rw_exit(&drp->r_rwlock); 7150 nfs4_fattr4_free(crattr); 7151 if (setgid_flag) { 7152 nfs4args_verify_free(&argop[8]); 7153 nfs4args_setattr_free(&argop[9]); 7154 } 7155 if (resp) 7156 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7157 if (need_end_op) 7158 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7159 7160 kmem_free(argop, argoplist_size); 7161 return (e.error); 7162 } 7163 7164 /* ARGSUSED */ 7165 static int 7166 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7167 int mode, vnode_t **vpp, cred_t *cr) 7168 { 7169 int error; 7170 vnode_t *vp; 7171 nfs_ftype4 type; 7172 specdata4 spec, *specp = NULL; 7173 7174 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7175 7176 switch (va->va_type) { 7177 case VCHR: 7178 case VBLK: 7179 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7180 spec.specdata1 = getmajor(va->va_rdev); 7181 spec.specdata2 = getminor(va->va_rdev); 7182 specp = &spec; 7183 break; 7184 7185 case VFIFO: 7186 type = NF4FIFO; 7187 break; 7188 case VSOCK: 7189 type = NF4SOCK; 7190 break; 7191 7192 default: 7193 return (EINVAL); 7194 } 7195 7196 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7197 if (error) { 7198 return (error); 7199 } 7200 7201 /* 7202 * This might not be needed any more; special case to deal 7203 * with problematic v2/v3 servers. Since create was unable 7204 * to set group correctly, not sure what hope setattr has. 7205 */ 7206 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7207 va->va_mask = AT_GID; 7208 (void) nfs4setattr(vp, va, 0, cr, NULL); 7209 } 7210 7211 /* 7212 * If vnode is a device create special vnode 7213 */ 7214 if (ISVDEV(vp->v_type)) { 7215 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7216 VN_RELE(vp); 7217 } else { 7218 *vpp = vp; 7219 } 7220 return (error); 7221 } 7222 7223 /* 7224 * Remove requires that the current fh be the target directory. 7225 * After the operation, the current fh is unchanged. 7226 * The compound op structure is: 7227 * PUTFH(targetdir), REMOVE 7228 * 7229 * Weirdness: if the vnode to be removed is open 7230 * we rename it instead of removing it and nfs_inactive 7231 * will remove the new name. 7232 */ 7233 /* ARGSUSED */ 7234 static int 7235 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags) 7236 { 7237 COMPOUND4args_clnt args; 7238 COMPOUND4res_clnt res, *resp = NULL; 7239 REMOVE4res *rm_res; 7240 nfs_argop4 argop[3]; 7241 nfs_resop4 *resop; 7242 vnode_t *vp; 7243 char *tmpname; 7244 int doqueue; 7245 mntinfo4_t *mi; 7246 rnode4_t *rp; 7247 rnode4_t *drp; 7248 int needrecov = 0; 7249 nfs4_recov_state_t recov_state; 7250 int isopen; 7251 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7252 dirattr_info_t dinfo; 7253 7254 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7255 return (EPERM); 7256 drp = VTOR4(dvp); 7257 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7258 return (EINTR); 7259 7260 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7261 if (e.error) { 7262 nfs_rw_exit(&drp->r_rwlock); 7263 return (e.error); 7264 } 7265 7266 if (vp->v_type == VDIR) { 7267 VN_RELE(vp); 7268 nfs_rw_exit(&drp->r_rwlock); 7269 return (EISDIR); 7270 } 7271 7272 /* 7273 * First just remove the entry from the name cache, as it 7274 * is most likely the only entry for this vp. 7275 */ 7276 dnlc_remove(dvp, nm); 7277 7278 rp = VTOR4(vp); 7279 7280 /* 7281 * For regular file types, check to see if the file is open by looking 7282 * at the open streams. 7283 * For all other types, check the reference count on the vnode. Since 7284 * they are not opened OTW they never have an open stream. 7285 * 7286 * If the file is open, rename it to .nfsXXXX. 7287 */ 7288 if (vp->v_type != VREG) { 7289 /* 7290 * If the file has a v_count > 1 then there may be more than one 7291 * entry in the name cache due multiple links or an open file, 7292 * but we don't have the real reference count so flush all 7293 * possible entries. 7294 */ 7295 if (vp->v_count > 1) 7296 dnlc_purge_vp(vp); 7297 7298 /* 7299 * Now we have the real reference count. 7300 */ 7301 isopen = vp->v_count > 1; 7302 } else { 7303 mutex_enter(&rp->r_os_lock); 7304 isopen = list_head(&rp->r_open_streams) != NULL; 7305 mutex_exit(&rp->r_os_lock); 7306 } 7307 7308 mutex_enter(&rp->r_statelock); 7309 if (isopen && 7310 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7311 mutex_exit(&rp->r_statelock); 7312 tmpname = newname(); 7313 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct); 7314 if (e.error) 7315 kmem_free(tmpname, MAXNAMELEN); 7316 else { 7317 mutex_enter(&rp->r_statelock); 7318 if (rp->r_unldvp == NULL) { 7319 VN_HOLD(dvp); 7320 rp->r_unldvp = dvp; 7321 if (rp->r_unlcred != NULL) 7322 crfree(rp->r_unlcred); 7323 crhold(cr); 7324 rp->r_unlcred = cr; 7325 rp->r_unlname = tmpname; 7326 } else { 7327 kmem_free(rp->r_unlname, MAXNAMELEN); 7328 rp->r_unlname = tmpname; 7329 } 7330 mutex_exit(&rp->r_statelock); 7331 } 7332 VN_RELE(vp); 7333 nfs_rw_exit(&drp->r_rwlock); 7334 return (e.error); 7335 } 7336 /* 7337 * Actually remove the file/dir 7338 */ 7339 mutex_exit(&rp->r_statelock); 7340 7341 /* 7342 * We need to flush any dirty pages which happen to 7343 * be hanging around before removing the file. 7344 * This shouldn't happen very often since in NFSv4 7345 * we should be close to open consistent. 7346 */ 7347 if (nfs4_has_pages(vp) && 7348 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7349 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct); 7350 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7351 mutex_enter(&rp->r_statelock); 7352 if (!rp->r_error) 7353 rp->r_error = e.error; 7354 mutex_exit(&rp->r_statelock); 7355 } 7356 } 7357 7358 mi = VTOMI4(dvp); 7359 7360 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7361 recov_state.rs_flags = 0; 7362 recov_state.rs_num_retry_despite_err = 0; 7363 7364 recov_retry: 7365 /* 7366 * Remove ops: putfh dir; remove 7367 */ 7368 args.ctag = TAG_REMOVE; 7369 args.array_len = 3; 7370 args.array = argop; 7371 7372 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7373 if (e.error) { 7374 nfs_rw_exit(&drp->r_rwlock); 7375 VN_RELE(vp); 7376 return (e.error); 7377 } 7378 7379 /* putfh directory */ 7380 argop[0].argop = OP_CPUTFH; 7381 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7382 7383 /* remove */ 7384 argop[1].argop = OP_CREMOVE; 7385 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7386 7387 /* getattr dir */ 7388 argop[2].argop = OP_GETATTR; 7389 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7390 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7391 7392 doqueue = 1; 7393 dinfo.di_time_call = gethrtime(); 7394 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7395 7396 PURGE_ATTRCACHE4(vp); 7397 7398 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7399 if (e.error) 7400 PURGE_ATTRCACHE4(dvp); 7401 7402 if (needrecov) { 7403 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7404 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 7405 if (!e.error) 7406 (void) xdr_free(xdr_COMPOUND4res_clnt, 7407 (caddr_t)&res); 7408 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7409 needrecov); 7410 goto recov_retry; 7411 } 7412 } 7413 7414 /* 7415 * Matching nfs4_end_op() for start_op() above. 7416 * There is a path in the code below which calls 7417 * nfs4_purge_stale_fh(), which may generate otw calls through 7418 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7419 * here to avoid nfs4_start_op() deadlock. 7420 */ 7421 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7422 7423 if (!e.error) { 7424 resp = &res; 7425 7426 if (res.status) { 7427 e.error = geterrno4(res.status); 7428 PURGE_ATTRCACHE4(dvp); 7429 nfs4_purge_stale_fh(e.error, dvp, cr); 7430 } else { 7431 resop = &res.array[1]; /* remove res */ 7432 rm_res = &resop->nfs_resop4_u.opremove; 7433 7434 dinfo.di_garp = 7435 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7436 dinfo.di_cred = cr; 7437 7438 /* Update directory attr, readdir and dnlc caches */ 7439 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7440 &dinfo); 7441 } 7442 } 7443 nfs_rw_exit(&drp->r_rwlock); 7444 if (resp) 7445 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7446 7447 if (e.error == 0) { 7448 vnode_t *tvp; 7449 rnode4_t *trp; 7450 trp = VTOR4(vp); 7451 tvp = vp; 7452 if (IS_SHADOW(vp, trp)) 7453 tvp = RTOV4(trp); 7454 vnevent_remove(tvp, dvp, nm, ct); 7455 } 7456 VN_RELE(vp); 7457 return (e.error); 7458 } 7459 7460 /* 7461 * Link requires that the current fh be the target directory and the 7462 * saved fh be the source fh. After the operation, the current fh is unchanged. 7463 * Thus the compound op structure is: 7464 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7465 * GETATTR(file) 7466 */ 7467 /* ARGSUSED */ 7468 static int 7469 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr, 7470 caller_context_t *ct, int flags) 7471 { 7472 COMPOUND4args_clnt args; 7473 COMPOUND4res_clnt res, *resp = NULL; 7474 LINK4res *ln_res; 7475 int argoplist_size = 7 * sizeof (nfs_argop4); 7476 nfs_argop4 *argop; 7477 nfs_resop4 *resop; 7478 vnode_t *realvp, *nvp; 7479 int doqueue; 7480 mntinfo4_t *mi; 7481 rnode4_t *tdrp; 7482 bool_t needrecov = FALSE; 7483 nfs4_recov_state_t recov_state; 7484 hrtime_t t; 7485 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7486 dirattr_info_t dinfo; 7487 7488 ASSERT(*tnm != '\0'); 7489 ASSERT(tdvp->v_type == VDIR); 7490 ASSERT(nfs4_consistent_type(tdvp)); 7491 ASSERT(nfs4_consistent_type(svp)); 7492 7493 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7494 return (EPERM); 7495 if (VOP_REALVP(svp, &realvp, ct) == 0) { 7496 svp = realvp; 7497 ASSERT(nfs4_consistent_type(svp)); 7498 } 7499 7500 tdrp = VTOR4(tdvp); 7501 mi = VTOMI4(svp); 7502 7503 if (!(mi->mi_flags & MI4_LINK)) { 7504 return (EOPNOTSUPP); 7505 } 7506 recov_state.rs_flags = 0; 7507 recov_state.rs_num_retry_despite_err = 0; 7508 7509 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7510 return (EINTR); 7511 7512 recov_retry: 7513 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7514 7515 args.ctag = TAG_LINK; 7516 7517 /* 7518 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7519 * restorefh; getattr(fl) 7520 */ 7521 args.array_len = 7; 7522 args.array = argop; 7523 7524 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7525 if (e.error) { 7526 kmem_free(argop, argoplist_size); 7527 nfs_rw_exit(&tdrp->r_rwlock); 7528 return (e.error); 7529 } 7530 7531 /* 0. putfh file */ 7532 argop[0].argop = OP_CPUTFH; 7533 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7534 7535 /* 1. save current fh to free up the space for the dir */ 7536 argop[1].argop = OP_SAVEFH; 7537 7538 /* 2. putfh targetdir */ 7539 argop[2].argop = OP_CPUTFH; 7540 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7541 7542 /* 3. link: current_fh is targetdir, saved_fh is source */ 7543 argop[3].argop = OP_CLINK; 7544 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7545 7546 /* 4. Get attributes of dir */ 7547 argop[4].argop = OP_GETATTR; 7548 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7549 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7550 7551 /* 5. If link was successful, restore current vp to file */ 7552 argop[5].argop = OP_RESTOREFH; 7553 7554 /* 6. Get attributes of linked object */ 7555 argop[6].argop = OP_GETATTR; 7556 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7557 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7558 7559 dnlc_remove(tdvp, tnm); 7560 7561 doqueue = 1; 7562 t = gethrtime(); 7563 7564 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7565 7566 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7567 if (e.error != 0 && !needrecov) { 7568 PURGE_ATTRCACHE4(tdvp); 7569 PURGE_ATTRCACHE4(svp); 7570 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7571 goto out; 7572 } 7573 7574 if (needrecov) { 7575 bool_t abort; 7576 7577 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7578 NULL, NULL, OP_LINK, NULL, NULL, NULL); 7579 if (abort == FALSE) { 7580 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7581 needrecov); 7582 kmem_free(argop, argoplist_size); 7583 if (!e.error) 7584 (void) xdr_free(xdr_COMPOUND4res_clnt, 7585 (caddr_t)&res); 7586 goto recov_retry; 7587 } else { 7588 if (e.error != 0) { 7589 PURGE_ATTRCACHE4(tdvp); 7590 PURGE_ATTRCACHE4(svp); 7591 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7592 &recov_state, needrecov); 7593 goto out; 7594 } 7595 /* fall through for res.status case */ 7596 } 7597 } 7598 7599 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7600 7601 resp = &res; 7602 if (res.status) { 7603 /* If link succeeded, then don't return error */ 7604 e.error = geterrno4(res.status); 7605 if (res.array_len <= 4) { 7606 /* 7607 * Either Putfh, Savefh, Putfh dir, or Link failed 7608 */ 7609 PURGE_ATTRCACHE4(svp); 7610 PURGE_ATTRCACHE4(tdvp); 7611 if (e.error == EOPNOTSUPP) { 7612 mutex_enter(&mi->mi_lock); 7613 mi->mi_flags &= ~MI4_LINK; 7614 mutex_exit(&mi->mi_lock); 7615 } 7616 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7617 /* XXX-LP */ 7618 if (e.error == EISDIR && crgetuid(cr) != 0) 7619 e.error = EPERM; 7620 goto out; 7621 } 7622 } 7623 7624 /* either no error or one of the postop getattr failed */ 7625 7626 /* 7627 * XXX - if LINK succeeded, but no attrs were returned for link 7628 * file, purge its cache. 7629 * 7630 * XXX Perform a simplified version of wcc checking. Instead of 7631 * have another getattr to get pre-op, just purge cache if 7632 * any of the ops prior to and including the getattr failed. 7633 * If the getattr succeeded then update the attrcache accordingly. 7634 */ 7635 7636 /* 7637 * update cache with link file postattrs. 7638 * Note: at this point resop points to link res. 7639 */ 7640 resop = &res.array[3]; /* link res */ 7641 ln_res = &resop->nfs_resop4_u.oplink; 7642 if (res.status == NFS4_OK) 7643 e.error = nfs4_update_attrcache(res.status, 7644 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7645 t, svp, cr); 7646 7647 /* 7648 * Call makenfs4node to create the new shadow vp for tnm. 7649 * We pass NULL attrs because we just cached attrs for 7650 * the src object. All we're trying to accomplish is to 7651 * to create the new shadow vnode. 7652 */ 7653 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7654 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh)); 7655 7656 /* Update target cache attribute, readdir and dnlc caches */ 7657 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7658 dinfo.di_time_call = t; 7659 dinfo.di_cred = cr; 7660 7661 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7662 ASSERT(nfs4_consistent_type(tdvp)); 7663 ASSERT(nfs4_consistent_type(svp)); 7664 ASSERT(nfs4_consistent_type(nvp)); 7665 VN_RELE(nvp); 7666 7667 if (!e.error) { 7668 vnode_t *tvp; 7669 rnode4_t *trp; 7670 /* 7671 * Notify the source file of this link operation. 7672 */ 7673 trp = VTOR4(svp); 7674 tvp = svp; 7675 if (IS_SHADOW(svp, trp)) 7676 tvp = RTOV4(trp); 7677 vnevent_link(tvp, ct); 7678 } 7679 out: 7680 kmem_free(argop, argoplist_size); 7681 if (resp) 7682 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7683 7684 nfs_rw_exit(&tdrp->r_rwlock); 7685 7686 return (e.error); 7687 } 7688 7689 /* ARGSUSED */ 7690 static int 7691 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7692 caller_context_t *ct, int flags) 7693 { 7694 vnode_t *realvp; 7695 7696 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7697 return (EPERM); 7698 if (VOP_REALVP(ndvp, &realvp, ct) == 0) 7699 ndvp = realvp; 7700 7701 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct)); 7702 } 7703 7704 /* 7705 * nfs4rename does the real work of renaming in NFS Version 4. 7706 * 7707 * A file handle is considered volatile for renaming purposes if either 7708 * of the volatile bits are turned on. However, the compound may differ 7709 * based on the likelihood of the filehandle to change during rename. 7710 */ 7711 static int 7712 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7713 caller_context_t *ct) 7714 { 7715 int error; 7716 mntinfo4_t *mi; 7717 vnode_t *nvp = NULL; 7718 vnode_t *ovp = NULL; 7719 char *tmpname = NULL; 7720 rnode4_t *rp; 7721 rnode4_t *odrp; 7722 rnode4_t *ndrp; 7723 int did_link = 0; 7724 int do_link = 1; 7725 nfsstat4 stat = NFS4_OK; 7726 7727 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7728 ASSERT(nfs4_consistent_type(odvp)); 7729 ASSERT(nfs4_consistent_type(ndvp)); 7730 7731 if (onm[0] == '.' && (onm[1] == '\0' || 7732 (onm[1] == '.' && onm[2] == '\0'))) 7733 return (EINVAL); 7734 7735 if (nnm[0] == '.' && (nnm[1] == '\0' || 7736 (nnm[1] == '.' && nnm[2] == '\0'))) 7737 return (EINVAL); 7738 7739 odrp = VTOR4(odvp); 7740 ndrp = VTOR4(ndvp); 7741 if ((intptr_t)odrp < (intptr_t)ndrp) { 7742 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7743 return (EINTR); 7744 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7745 nfs_rw_exit(&odrp->r_rwlock); 7746 return (EINTR); 7747 } 7748 } else { 7749 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7750 return (EINTR); 7751 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7752 nfs_rw_exit(&ndrp->r_rwlock); 7753 return (EINTR); 7754 } 7755 } 7756 7757 /* 7758 * Lookup the target file. If it exists, it needs to be 7759 * checked to see whether it is a mount point and whether 7760 * it is active (open). 7761 */ 7762 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7763 if (!error) { 7764 int isactive; 7765 7766 ASSERT(nfs4_consistent_type(nvp)); 7767 /* 7768 * If this file has been mounted on, then just 7769 * return busy because renaming to it would remove 7770 * the mounted file system from the name space. 7771 */ 7772 if (vn_ismntpt(nvp)) { 7773 VN_RELE(nvp); 7774 nfs_rw_exit(&odrp->r_rwlock); 7775 nfs_rw_exit(&ndrp->r_rwlock); 7776 return (EBUSY); 7777 } 7778 7779 /* 7780 * First just remove the entry from the name cache, as it 7781 * is most likely the only entry for this vp. 7782 */ 7783 dnlc_remove(ndvp, nnm); 7784 7785 rp = VTOR4(nvp); 7786 7787 if (nvp->v_type != VREG) { 7788 /* 7789 * Purge the name cache of all references to this vnode 7790 * so that we can check the reference count to infer 7791 * whether it is active or not. 7792 */ 7793 if (nvp->v_count > 1) 7794 dnlc_purge_vp(nvp); 7795 7796 isactive = nvp->v_count > 1; 7797 } else { 7798 mutex_enter(&rp->r_os_lock); 7799 isactive = list_head(&rp->r_open_streams) != NULL; 7800 mutex_exit(&rp->r_os_lock); 7801 } 7802 7803 /* 7804 * If the vnode is active and is not a directory, 7805 * arrange to rename it to a 7806 * temporary file so that it will continue to be 7807 * accessible. This implements the "unlink-open-file" 7808 * semantics for the target of a rename operation. 7809 * Before doing this though, make sure that the 7810 * source and target files are not already the same. 7811 */ 7812 if (isactive && nvp->v_type != VDIR) { 7813 /* 7814 * Lookup the source name. 7815 */ 7816 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7817 7818 /* 7819 * The source name *should* already exist. 7820 */ 7821 if (error) { 7822 VN_RELE(nvp); 7823 nfs_rw_exit(&odrp->r_rwlock); 7824 nfs_rw_exit(&ndrp->r_rwlock); 7825 return (error); 7826 } 7827 7828 ASSERT(nfs4_consistent_type(ovp)); 7829 7830 /* 7831 * Compare the two vnodes. If they are the same, 7832 * just release all held vnodes and return success. 7833 */ 7834 if (VN_CMP(ovp, nvp)) { 7835 VN_RELE(ovp); 7836 VN_RELE(nvp); 7837 nfs_rw_exit(&odrp->r_rwlock); 7838 nfs_rw_exit(&ndrp->r_rwlock); 7839 return (0); 7840 } 7841 7842 /* 7843 * Can't mix and match directories and non- 7844 * directories in rename operations. We already 7845 * know that the target is not a directory. If 7846 * the source is a directory, return an error. 7847 */ 7848 if (ovp->v_type == VDIR) { 7849 VN_RELE(ovp); 7850 VN_RELE(nvp); 7851 nfs_rw_exit(&odrp->r_rwlock); 7852 nfs_rw_exit(&ndrp->r_rwlock); 7853 return (ENOTDIR); 7854 } 7855 link_call: 7856 /* 7857 * The target file exists, is not the same as 7858 * the source file, and is active. We first 7859 * try to Link it to a temporary filename to 7860 * avoid having the server removing the file 7861 * completely (which could cause data loss to 7862 * the user's POV in the event the Rename fails 7863 * -- see bug 1165874). 7864 */ 7865 /* 7866 * The do_link and did_link booleans are 7867 * introduced in the event we get NFS4ERR_FILE_OPEN 7868 * returned for the Rename. Some servers can 7869 * not Rename over an Open file, so they return 7870 * this error. The client needs to Remove the 7871 * newly created Link and do two Renames, just 7872 * as if the server didn't support LINK. 7873 */ 7874 tmpname = newname(); 7875 error = 0; 7876 7877 if (do_link) { 7878 error = nfs4_link(ndvp, nvp, tmpname, cr, 7879 NULL, 0); 7880 } 7881 if (error == EOPNOTSUPP || !do_link) { 7882 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7883 cr, NULL, 0); 7884 did_link = 0; 7885 } else { 7886 did_link = 1; 7887 } 7888 if (error) { 7889 kmem_free(tmpname, MAXNAMELEN); 7890 VN_RELE(ovp); 7891 VN_RELE(nvp); 7892 nfs_rw_exit(&odrp->r_rwlock); 7893 nfs_rw_exit(&ndrp->r_rwlock); 7894 return (error); 7895 } 7896 7897 mutex_enter(&rp->r_statelock); 7898 if (rp->r_unldvp == NULL) { 7899 VN_HOLD(ndvp); 7900 rp->r_unldvp = ndvp; 7901 if (rp->r_unlcred != NULL) 7902 crfree(rp->r_unlcred); 7903 crhold(cr); 7904 rp->r_unlcred = cr; 7905 rp->r_unlname = tmpname; 7906 } else { 7907 if (rp->r_unlname) 7908 kmem_free(rp->r_unlname, MAXNAMELEN); 7909 rp->r_unlname = tmpname; 7910 } 7911 mutex_exit(&rp->r_statelock); 7912 } 7913 7914 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7915 7916 ASSERT(nfs4_consistent_type(nvp)); 7917 } 7918 7919 if (ovp == NULL) { 7920 /* 7921 * When renaming directories to be a subdirectory of a 7922 * different parent, the dnlc entry for ".." will no 7923 * longer be valid, so it must be removed. 7924 * 7925 * We do a lookup here to determine whether we are renaming 7926 * a directory and we need to check if we are renaming 7927 * an unlinked file. This might have already been done 7928 * in previous code, so we check ovp == NULL to avoid 7929 * doing it twice. 7930 */ 7931 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7932 /* 7933 * The source name *should* already exist. 7934 */ 7935 if (error) { 7936 nfs_rw_exit(&odrp->r_rwlock); 7937 nfs_rw_exit(&ndrp->r_rwlock); 7938 if (nvp) { 7939 VN_RELE(nvp); 7940 } 7941 return (error); 7942 } 7943 ASSERT(ovp != NULL); 7944 ASSERT(nfs4_consistent_type(ovp)); 7945 } 7946 7947 /* 7948 * Is the object being renamed a dir, and if so, is 7949 * it being renamed to a child of itself? The underlying 7950 * fs should ultimately return EINVAL for this case; 7951 * however, buggy beta non-Solaris NFSv4 servers at 7952 * interop testing events have allowed this behavior, 7953 * and it caused our client to panic due to a recursive 7954 * mutex_enter in fn_move. 7955 * 7956 * The tedious locking in fn_move could be changed to 7957 * deal with this case, and the client could avoid the 7958 * panic; however, the client would just confuse itself 7959 * later and misbehave. A better way to handle the broken 7960 * server is to detect this condition and return EINVAL 7961 * without ever sending the the bogus rename to the server. 7962 * We know the rename is invalid -- just fail it now. 7963 */ 7964 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7965 VN_RELE(ovp); 7966 nfs_rw_exit(&odrp->r_rwlock); 7967 nfs_rw_exit(&ndrp->r_rwlock); 7968 if (nvp) { 7969 VN_RELE(nvp); 7970 } 7971 return (EINVAL); 7972 } 7973 7974 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7975 7976 /* 7977 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7978 * possible for the filehandle to change due to the rename. 7979 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7980 * the fh will not change because of the rename, but we still need 7981 * to update its rnode entry with the new name for 7982 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7983 * has no effect on these for now, but for future improvements, 7984 * we might want to use it too to simplify handling of files 7985 * that are open with that flag on. (XXX) 7986 */ 7987 mi = VTOMI4(odvp); 7988 if (NFS4_VOLATILE_FH(mi)) 7989 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7990 &stat); 7991 else 7992 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7993 &stat); 7994 7995 ASSERT(nfs4_consistent_type(odvp)); 7996 ASSERT(nfs4_consistent_type(ndvp)); 7997 ASSERT(nfs4_consistent_type(ovp)); 7998 7999 if (stat == NFS4ERR_FILE_OPEN && did_link) { 8000 do_link = 0; 8001 /* 8002 * Before the 'link_call' code, we did a nfs4_lookup 8003 * that puts a VN_HOLD on nvp. After the nfs4_link 8004 * call we call VN_RELE to match that hold. We need 8005 * to place an additional VN_HOLD here since we will 8006 * be hitting that VN_RELE again. 8007 */ 8008 VN_HOLD(nvp); 8009 8010 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0); 8011 8012 /* Undo the unlinked file naming stuff we just did */ 8013 mutex_enter(&rp->r_statelock); 8014 if (rp->r_unldvp) { 8015 VN_RELE(ndvp); 8016 rp->r_unldvp = NULL; 8017 if (rp->r_unlcred != NULL) 8018 crfree(rp->r_unlcred); 8019 rp->r_unlcred = NULL; 8020 /* rp->r_unlanme points to tmpname */ 8021 if (rp->r_unlname) 8022 kmem_free(rp->r_unlname, MAXNAMELEN); 8023 rp->r_unlname = NULL; 8024 } 8025 mutex_exit(&rp->r_statelock); 8026 8027 if (nvp) { 8028 VN_RELE(nvp); 8029 } 8030 goto link_call; 8031 } 8032 8033 if (error) { 8034 VN_RELE(ovp); 8035 nfs_rw_exit(&odrp->r_rwlock); 8036 nfs_rw_exit(&ndrp->r_rwlock); 8037 if (nvp) { 8038 VN_RELE(nvp); 8039 } 8040 return (error); 8041 } 8042 8043 /* 8044 * when renaming directories to be a subdirectory of a 8045 * different parent, the dnlc entry for ".." will no 8046 * longer be valid, so it must be removed 8047 */ 8048 rp = VTOR4(ovp); 8049 if (ndvp != odvp) { 8050 if (ovp->v_type == VDIR) { 8051 dnlc_remove(ovp, ".."); 8052 if (rp->r_dir != NULL) 8053 nfs4_purge_rddir_cache(ovp); 8054 } 8055 } 8056 8057 /* 8058 * If we are renaming the unlinked file, update the 8059 * r_unldvp and r_unlname as needed. 8060 */ 8061 mutex_enter(&rp->r_statelock); 8062 if (rp->r_unldvp != NULL) { 8063 if (strcmp(rp->r_unlname, onm) == 0) { 8064 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 8065 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 8066 if (ndvp != rp->r_unldvp) { 8067 VN_RELE(rp->r_unldvp); 8068 rp->r_unldvp = ndvp; 8069 VN_HOLD(ndvp); 8070 } 8071 } 8072 } 8073 mutex_exit(&rp->r_statelock); 8074 8075 /* 8076 * Notify the rename vnevents to source vnode, and to the target 8077 * vnode if it already existed. 8078 */ 8079 if (error == 0) { 8080 vnode_t *tvp; 8081 rnode4_t *trp; 8082 /* 8083 * Notify the vnode. Each links is represented by 8084 * a different vnode, in nfsv4. 8085 */ 8086 if (nvp) { 8087 trp = VTOR4(nvp); 8088 tvp = nvp; 8089 if (IS_SHADOW(nvp, trp)) 8090 tvp = RTOV4(trp); 8091 vnevent_rename_dest(tvp, ndvp, nnm, ct); 8092 } 8093 8094 /* 8095 * if the source and destination directory are not the 8096 * same notify the destination directory. 8097 */ 8098 if (VTOR4(odvp) != VTOR4(ndvp)) { 8099 trp = VTOR4(ndvp); 8100 tvp = ndvp; 8101 if (IS_SHADOW(ndvp, trp)) 8102 tvp = RTOV4(trp); 8103 vnevent_rename_dest_dir(tvp, ct); 8104 } 8105 8106 trp = VTOR4(ovp); 8107 tvp = ovp; 8108 if (IS_SHADOW(ovp, trp)) 8109 tvp = RTOV4(trp); 8110 vnevent_rename_src(tvp, odvp, onm, ct); 8111 } 8112 8113 if (nvp) { 8114 VN_RELE(nvp); 8115 } 8116 VN_RELE(ovp); 8117 8118 nfs_rw_exit(&odrp->r_rwlock); 8119 nfs_rw_exit(&ndrp->r_rwlock); 8120 8121 return (error); 8122 } 8123 8124 /* 8125 * When the parent directory has changed, sv_dfh must be updated 8126 */ 8127 static void 8128 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp) 8129 { 8130 svnode_t *sv = VTOSV(vp); 8131 nfs4_sharedfh_t *old_dfh = sv->sv_dfh; 8132 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh; 8133 8134 sfh4_hold(new_dfh); 8135 sv->sv_dfh = new_dfh; 8136 sfh4_rele(&old_dfh); 8137 } 8138 8139 /* 8140 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 8141 * when it is known that the filehandle is persistent through rename. 8142 * 8143 * Rename requires that the current fh be the target directory and the 8144 * saved fh be the source directory. After the operation, the current fh 8145 * is unchanged. 8146 * The compound op structure for persistent fh rename is: 8147 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 8148 * Rather than bother with the directory postop args, we'll simply 8149 * update that a change occurred in the cache, so no post-op getattrs. 8150 */ 8151 static int 8152 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 8153 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8154 { 8155 COMPOUND4args_clnt args; 8156 COMPOUND4res_clnt res, *resp = NULL; 8157 nfs_argop4 *argop; 8158 nfs_resop4 *resop; 8159 int doqueue, argoplist_size; 8160 mntinfo4_t *mi; 8161 rnode4_t *odrp = VTOR4(odvp); 8162 rnode4_t *ndrp = VTOR4(ndvp); 8163 RENAME4res *rn_res; 8164 bool_t needrecov; 8165 nfs4_recov_state_t recov_state; 8166 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8167 dirattr_info_t dinfo, *dinfop; 8168 8169 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8170 8171 recov_state.rs_flags = 0; 8172 recov_state.rs_num_retry_despite_err = 0; 8173 8174 /* 8175 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 8176 * 8177 * If source/target are different dirs, then append putfh(src); getattr 8178 */ 8179 args.array_len = (odvp == ndvp) ? 5 : 7; 8180 argoplist_size = args.array_len * sizeof (nfs_argop4); 8181 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8182 8183 recov_retry: 8184 *statp = NFS4_OK; 8185 8186 /* No need to Lookup the file, persistent fh */ 8187 args.ctag = TAG_RENAME; 8188 8189 mi = VTOMI4(odvp); 8190 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8191 if (e.error) { 8192 kmem_free(argop, argoplist_size); 8193 return (e.error); 8194 } 8195 8196 /* 0: putfh source directory */ 8197 argop[0].argop = OP_CPUTFH; 8198 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8199 8200 /* 1: Save source fh to free up current for target */ 8201 argop[1].argop = OP_SAVEFH; 8202 8203 /* 2: putfh targetdir */ 8204 argop[2].argop = OP_CPUTFH; 8205 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8206 8207 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8208 argop[3].argop = OP_CRENAME; 8209 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8210 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8211 8212 /* 4: getattr (targetdir) */ 8213 argop[4].argop = OP_GETATTR; 8214 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8215 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8216 8217 if (ndvp != odvp) { 8218 8219 /* 5: putfh (sourcedir) */ 8220 argop[5].argop = OP_CPUTFH; 8221 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8222 8223 /* 6: getattr (sourcedir) */ 8224 argop[6].argop = OP_GETATTR; 8225 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8226 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8227 } 8228 8229 dnlc_remove(odvp, onm); 8230 dnlc_remove(ndvp, nnm); 8231 8232 doqueue = 1; 8233 dinfo.di_time_call = gethrtime(); 8234 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8235 8236 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8237 if (e.error) { 8238 PURGE_ATTRCACHE4(odvp); 8239 PURGE_ATTRCACHE4(ndvp); 8240 } else { 8241 *statp = res.status; 8242 } 8243 8244 if (needrecov) { 8245 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8246 OP_RENAME, NULL, NULL, NULL) == FALSE) { 8247 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8248 if (!e.error) 8249 (void) xdr_free(xdr_COMPOUND4res_clnt, 8250 (caddr_t)&res); 8251 goto recov_retry; 8252 } 8253 } 8254 8255 if (!e.error) { 8256 resp = &res; 8257 /* 8258 * as long as OP_RENAME 8259 */ 8260 if (res.status != NFS4_OK && res.array_len <= 4) { 8261 e.error = geterrno4(res.status); 8262 PURGE_ATTRCACHE4(odvp); 8263 PURGE_ATTRCACHE4(ndvp); 8264 /* 8265 * System V defines rename to return EEXIST, not 8266 * ENOTEMPTY if the target directory is not empty. 8267 * Over the wire, the error is NFSERR_ENOTEMPTY 8268 * which geterrno4 maps to ENOTEMPTY. 8269 */ 8270 if (e.error == ENOTEMPTY) 8271 e.error = EEXIST; 8272 } else { 8273 8274 resop = &res.array[3]; /* rename res */ 8275 rn_res = &resop->nfs_resop4_u.oprename; 8276 8277 if (res.status == NFS4_OK) { 8278 /* 8279 * Update target attribute, readdir and dnlc 8280 * caches. 8281 */ 8282 dinfo.di_garp = 8283 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8284 dinfo.di_cred = cr; 8285 dinfop = &dinfo; 8286 } else 8287 dinfop = NULL; 8288 8289 nfs4_update_dircaches(&rn_res->target_cinfo, 8290 ndvp, NULL, NULL, dinfop); 8291 8292 /* 8293 * Update source attribute, readdir and dnlc caches 8294 * 8295 */ 8296 if (ndvp != odvp) { 8297 update_parentdir_sfh(renvp, ndvp); 8298 8299 if (dinfop) 8300 dinfo.di_garp = 8301 &(res.array[6].nfs_resop4_u. 8302 opgetattr.ga_res); 8303 8304 nfs4_update_dircaches(&rn_res->source_cinfo, 8305 odvp, NULL, NULL, dinfop); 8306 } 8307 8308 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8309 nnm); 8310 } 8311 } 8312 8313 if (resp) 8314 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8315 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8316 kmem_free(argop, argoplist_size); 8317 8318 return (e.error); 8319 } 8320 8321 /* 8322 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8323 * it is possible for the filehandle to change due to the rename. 8324 * 8325 * The compound req in this case includes a post-rename lookup and getattr 8326 * to ensure that we have the correct fh and attributes for the object. 8327 * 8328 * Rename requires that the current fh be the target directory and the 8329 * saved fh be the source directory. After the operation, the current fh 8330 * is unchanged. 8331 * 8332 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8333 * update the filehandle for the renamed object. We also get the old 8334 * filehandle for historical reasons; this should be taken out sometime. 8335 * This results in a rather cumbersome compound... 8336 * 8337 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8338 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8339 * 8340 */ 8341 static int 8342 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8343 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8344 { 8345 COMPOUND4args_clnt args; 8346 COMPOUND4res_clnt res, *resp = NULL; 8347 int argoplist_size; 8348 nfs_argop4 *argop; 8349 nfs_resop4 *resop; 8350 int doqueue; 8351 mntinfo4_t *mi; 8352 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8353 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8354 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8355 RENAME4res *rn_res; 8356 GETFH4res *ngf_res; 8357 bool_t needrecov; 8358 nfs4_recov_state_t recov_state; 8359 hrtime_t t; 8360 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8361 dirattr_info_t dinfo, *dinfop = &dinfo; 8362 8363 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8364 8365 recov_state.rs_flags = 0; 8366 recov_state.rs_num_retry_despite_err = 0; 8367 8368 recov_retry: 8369 *statp = NFS4_OK; 8370 8371 /* 8372 * There is a window between the RPC and updating the path and 8373 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8374 * code, so that it doesn't try to use the old path during that 8375 * window. 8376 */ 8377 mutex_enter(&orp->r_statelock); 8378 while (orp->r_flags & R4RECEXPFH) { 8379 klwp_t *lwp = ttolwp(curthread); 8380 8381 if (lwp != NULL) 8382 lwp->lwp_nostop++; 8383 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8384 mutex_exit(&orp->r_statelock); 8385 if (lwp != NULL) 8386 lwp->lwp_nostop--; 8387 return (EINTR); 8388 } 8389 if (lwp != NULL) 8390 lwp->lwp_nostop--; 8391 } 8392 orp->r_flags |= R4RECEXPFH; 8393 mutex_exit(&orp->r_statelock); 8394 8395 mi = VTOMI4(odvp); 8396 8397 args.ctag = TAG_RENAME_VFH; 8398 args.array_len = (odvp == ndvp) ? 10 : 12; 8399 argoplist_size = args.array_len * sizeof (nfs_argop4); 8400 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8401 8402 /* 8403 * Rename ops: 8404 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8405 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8406 * LOOKUP(trgt), GETFH(new), GETATTR, 8407 * 8408 * if (odvp != ndvp) 8409 * add putfh(sourcedir), getattr(sourcedir) } 8410 */ 8411 args.array = argop; 8412 8413 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8414 &recov_state, NULL); 8415 if (e.error) { 8416 kmem_free(argop, argoplist_size); 8417 mutex_enter(&orp->r_statelock); 8418 orp->r_flags &= ~R4RECEXPFH; 8419 cv_broadcast(&orp->r_cv); 8420 mutex_exit(&orp->r_statelock); 8421 return (e.error); 8422 } 8423 8424 /* 0: putfh source directory */ 8425 argop[0].argop = OP_CPUTFH; 8426 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8427 8428 /* 1: Save source fh to free up current for target */ 8429 argop[1].argop = OP_SAVEFH; 8430 8431 /* 2: Lookup pre-rename fh of renamed object */ 8432 argop[2].argop = OP_CLOOKUP; 8433 argop[2].nfs_argop4_u.opclookup.cname = onm; 8434 8435 /* 3: getfh fh of renamed object (before rename) */ 8436 argop[3].argop = OP_GETFH; 8437 8438 /* 4: putfh targetdir */ 8439 argop[4].argop = OP_CPUTFH; 8440 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8441 8442 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8443 argop[5].argop = OP_CRENAME; 8444 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8445 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8446 8447 /* 6: getattr of target dir (post op attrs) */ 8448 argop[6].argop = OP_GETATTR; 8449 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8450 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8451 8452 /* 7: Lookup post-rename fh of renamed object */ 8453 argop[7].argop = OP_CLOOKUP; 8454 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8455 8456 /* 8: getfh fh of renamed object (after rename) */ 8457 argop[8].argop = OP_GETFH; 8458 8459 /* 9: getattr of renamed object */ 8460 argop[9].argop = OP_GETATTR; 8461 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8462 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8463 8464 /* 8465 * If source/target dirs are different, then get new post-op 8466 * attrs for source dir also. 8467 */ 8468 if (ndvp != odvp) { 8469 /* 10: putfh (sourcedir) */ 8470 argop[10].argop = OP_CPUTFH; 8471 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8472 8473 /* 11: getattr (sourcedir) */ 8474 argop[11].argop = OP_GETATTR; 8475 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8476 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8477 } 8478 8479 dnlc_remove(odvp, onm); 8480 dnlc_remove(ndvp, nnm); 8481 8482 doqueue = 1; 8483 t = gethrtime(); 8484 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8485 8486 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8487 if (e.error) { 8488 PURGE_ATTRCACHE4(odvp); 8489 PURGE_ATTRCACHE4(ndvp); 8490 if (!needrecov) { 8491 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8492 &recov_state, needrecov); 8493 goto out; 8494 } 8495 } else { 8496 *statp = res.status; 8497 } 8498 8499 if (needrecov) { 8500 bool_t abort; 8501 8502 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8503 OP_RENAME, NULL, NULL, NULL); 8504 if (abort == FALSE) { 8505 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8506 &recov_state, needrecov); 8507 kmem_free(argop, argoplist_size); 8508 if (!e.error) 8509 (void) xdr_free(xdr_COMPOUND4res_clnt, 8510 (caddr_t)&res); 8511 mutex_enter(&orp->r_statelock); 8512 orp->r_flags &= ~R4RECEXPFH; 8513 cv_broadcast(&orp->r_cv); 8514 mutex_exit(&orp->r_statelock); 8515 goto recov_retry; 8516 } else { 8517 if (e.error != 0) { 8518 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8519 &recov_state, needrecov); 8520 goto out; 8521 } 8522 /* fall through for res.status case */ 8523 } 8524 } 8525 8526 resp = &res; 8527 /* 8528 * If OP_RENAME (or any prev op) failed, then return an error. 8529 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8530 */ 8531 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8532 /* 8533 * Error in an op other than last Getattr 8534 */ 8535 e.error = geterrno4(res.status); 8536 PURGE_ATTRCACHE4(odvp); 8537 PURGE_ATTRCACHE4(ndvp); 8538 /* 8539 * System V defines rename to return EEXIST, not 8540 * ENOTEMPTY if the target directory is not empty. 8541 * Over the wire, the error is NFSERR_ENOTEMPTY 8542 * which geterrno4 maps to ENOTEMPTY. 8543 */ 8544 if (e.error == ENOTEMPTY) 8545 e.error = EEXIST; 8546 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8547 needrecov); 8548 goto out; 8549 } 8550 8551 /* rename results */ 8552 rn_res = &res.array[5].nfs_resop4_u.oprename; 8553 8554 if (res.status == NFS4_OK) { 8555 /* Update target attribute, readdir and dnlc caches */ 8556 dinfo.di_garp = 8557 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8558 dinfo.di_cred = cr; 8559 dinfo.di_time_call = t; 8560 } else 8561 dinfop = NULL; 8562 8563 /* Update source cache attribute, readdir and dnlc caches */ 8564 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8565 8566 /* Update source cache attribute, readdir and dnlc caches */ 8567 if (ndvp != odvp) { 8568 update_parentdir_sfh(ovp, ndvp); 8569 8570 /* 8571 * If dinfop is non-NULL, then compound succeded, so 8572 * set di_garp to attrs for source dir. dinfop is only 8573 * set to NULL when compound fails. 8574 */ 8575 if (dinfop) 8576 dinfo.di_garp = 8577 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8578 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8579 dinfop); 8580 } 8581 8582 /* 8583 * Update the rnode with the new component name and args, 8584 * and if the file handle changed, also update it with the new fh. 8585 * This is only necessary if the target object has an rnode 8586 * entry and there is no need to create one for it. 8587 */ 8588 resop = &res.array[8]; /* getfh new res */ 8589 ngf_res = &resop->nfs_resop4_u.opgetfh; 8590 8591 /* 8592 * Update the path and filehandle for the renamed object. 8593 */ 8594 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8595 8596 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8597 8598 if (res.status == NFS4_OK) { 8599 resop++; /* getattr res */ 8600 e.error = nfs4_update_attrcache(res.status, 8601 &resop->nfs_resop4_u.opgetattr.ga_res, 8602 t, ovp, cr); 8603 } 8604 8605 out: 8606 kmem_free(argop, argoplist_size); 8607 if (resp) 8608 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8609 mutex_enter(&orp->r_statelock); 8610 orp->r_flags &= ~R4RECEXPFH; 8611 cv_broadcast(&orp->r_cv); 8612 mutex_exit(&orp->r_statelock); 8613 8614 return (e.error); 8615 } 8616 8617 /* ARGSUSED */ 8618 static int 8619 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr, 8620 caller_context_t *ct, int flags, vsecattr_t *vsecp) 8621 { 8622 int error; 8623 vnode_t *vp; 8624 8625 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8626 return (EPERM); 8627 /* 8628 * As ".." has special meaning and rather than send a mkdir 8629 * over the wire to just let the server freak out, we just 8630 * short circuit it here and return EEXIST 8631 */ 8632 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8633 return (EEXIST); 8634 8635 /* 8636 * Decision to get the right gid and setgid bit of the 8637 * new directory is now made in call_nfs4_create_req. 8638 */ 8639 va->va_mask |= AT_MODE; 8640 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8641 if (error) 8642 return (error); 8643 8644 *vpp = vp; 8645 return (0); 8646 } 8647 8648 8649 /* 8650 * rmdir is using the same remove v4 op as does remove. 8651 * Remove requires that the current fh be the target directory. 8652 * After the operation, the current fh is unchanged. 8653 * The compound op structure is: 8654 * PUTFH(targetdir), REMOVE 8655 */ 8656 /*ARGSUSED4*/ 8657 static int 8658 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr, 8659 caller_context_t *ct, int flags) 8660 { 8661 int need_end_op = FALSE; 8662 COMPOUND4args_clnt args; 8663 COMPOUND4res_clnt res, *resp = NULL; 8664 REMOVE4res *rm_res; 8665 nfs_argop4 argop[3]; 8666 nfs_resop4 *resop; 8667 vnode_t *vp; 8668 int doqueue; 8669 mntinfo4_t *mi; 8670 rnode4_t *drp; 8671 bool_t needrecov = FALSE; 8672 nfs4_recov_state_t recov_state; 8673 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8674 dirattr_info_t dinfo, *dinfop; 8675 8676 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8677 return (EPERM); 8678 /* 8679 * As ".." has special meaning and rather than send a rmdir 8680 * over the wire to just let the server freak out, we just 8681 * short circuit it here and return EEXIST 8682 */ 8683 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8684 return (EEXIST); 8685 8686 drp = VTOR4(dvp); 8687 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8688 return (EINTR); 8689 8690 /* 8691 * Attempt to prevent a rmdir(".") from succeeding. 8692 */ 8693 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8694 if (e.error) { 8695 nfs_rw_exit(&drp->r_rwlock); 8696 return (e.error); 8697 } 8698 if (vp == cdir) { 8699 VN_RELE(vp); 8700 nfs_rw_exit(&drp->r_rwlock); 8701 return (EINVAL); 8702 } 8703 8704 /* 8705 * Since nfsv4 remove op works on both files and directories, 8706 * check that the removed object is indeed a directory. 8707 */ 8708 if (vp->v_type != VDIR) { 8709 VN_RELE(vp); 8710 nfs_rw_exit(&drp->r_rwlock); 8711 return (ENOTDIR); 8712 } 8713 8714 /* 8715 * First just remove the entry from the name cache, as it 8716 * is most likely an entry for this vp. 8717 */ 8718 dnlc_remove(dvp, nm); 8719 8720 /* 8721 * If there vnode reference count is greater than one, then 8722 * there may be additional references in the DNLC which will 8723 * need to be purged. First, trying removing the entry for 8724 * the parent directory and see if that removes the additional 8725 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8726 * to completely remove any references to the directory which 8727 * might still exist in the DNLC. 8728 */ 8729 if (vp->v_count > 1) { 8730 dnlc_remove(vp, ".."); 8731 if (vp->v_count > 1) 8732 dnlc_purge_vp(vp); 8733 } 8734 8735 mi = VTOMI4(dvp); 8736 recov_state.rs_flags = 0; 8737 recov_state.rs_num_retry_despite_err = 0; 8738 8739 recov_retry: 8740 args.ctag = TAG_RMDIR; 8741 8742 /* 8743 * Rmdir ops: putfh dir; remove 8744 */ 8745 args.array_len = 3; 8746 args.array = argop; 8747 8748 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8749 if (e.error) { 8750 nfs_rw_exit(&drp->r_rwlock); 8751 return (e.error); 8752 } 8753 need_end_op = TRUE; 8754 8755 /* putfh directory */ 8756 argop[0].argop = OP_CPUTFH; 8757 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8758 8759 /* remove */ 8760 argop[1].argop = OP_CREMOVE; 8761 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8762 8763 /* getattr (postop attrs for dir that contained removed dir) */ 8764 argop[2].argop = OP_GETATTR; 8765 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8766 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8767 8768 dinfo.di_time_call = gethrtime(); 8769 doqueue = 1; 8770 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8771 8772 PURGE_ATTRCACHE4(vp); 8773 8774 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8775 if (e.error) { 8776 PURGE_ATTRCACHE4(dvp); 8777 } 8778 8779 if (needrecov) { 8780 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8781 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 8782 if (!e.error) 8783 (void) xdr_free(xdr_COMPOUND4res_clnt, 8784 (caddr_t)&res); 8785 8786 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8787 needrecov); 8788 need_end_op = FALSE; 8789 goto recov_retry; 8790 } 8791 } 8792 8793 if (!e.error) { 8794 resp = &res; 8795 8796 /* 8797 * Only return error if first 2 ops (OP_REMOVE or earlier) 8798 * failed. 8799 */ 8800 if (res.status != NFS4_OK && res.array_len <= 2) { 8801 e.error = geterrno4(res.status); 8802 PURGE_ATTRCACHE4(dvp); 8803 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8804 &recov_state, needrecov); 8805 need_end_op = FALSE; 8806 nfs4_purge_stale_fh(e.error, dvp, cr); 8807 /* 8808 * System V defines rmdir to return EEXIST, not 8809 * ENOTEMPTY if the directory is not empty. Over 8810 * the wire, the error is NFSERR_ENOTEMPTY which 8811 * geterrno4 maps to ENOTEMPTY. 8812 */ 8813 if (e.error == ENOTEMPTY) 8814 e.error = EEXIST; 8815 } else { 8816 resop = &res.array[1]; /* remove res */ 8817 rm_res = &resop->nfs_resop4_u.opremove; 8818 8819 if (res.status == NFS4_OK) { 8820 resop = &res.array[2]; /* dir attrs */ 8821 dinfo.di_garp = 8822 &resop->nfs_resop4_u.opgetattr.ga_res; 8823 dinfo.di_cred = cr; 8824 dinfop = &dinfo; 8825 } else 8826 dinfop = NULL; 8827 8828 /* Update dir attribute, readdir and dnlc caches */ 8829 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8830 dinfop); 8831 8832 /* destroy rddir cache for dir that was removed */ 8833 if (VTOR4(vp)->r_dir != NULL) 8834 nfs4_purge_rddir_cache(vp); 8835 } 8836 } 8837 8838 if (need_end_op) 8839 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8840 8841 nfs_rw_exit(&drp->r_rwlock); 8842 8843 if (resp) 8844 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8845 8846 if (e.error == 0) { 8847 vnode_t *tvp; 8848 rnode4_t *trp; 8849 trp = VTOR4(vp); 8850 tvp = vp; 8851 if (IS_SHADOW(vp, trp)) 8852 tvp = RTOV4(trp); 8853 vnevent_rmdir(tvp, dvp, nm, ct); 8854 } 8855 8856 VN_RELE(vp); 8857 8858 return (e.error); 8859 } 8860 8861 /* ARGSUSED */ 8862 static int 8863 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr, 8864 caller_context_t *ct, int flags) 8865 { 8866 int error; 8867 vnode_t *vp; 8868 rnode4_t *rp; 8869 char *contents; 8870 mntinfo4_t *mi = VTOMI4(dvp); 8871 8872 if (nfs_zone() != mi->mi_zone) 8873 return (EPERM); 8874 if (!(mi->mi_flags & MI4_SYMLINK)) 8875 return (EOPNOTSUPP); 8876 8877 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8878 if (error) 8879 return (error); 8880 8881 ASSERT(nfs4_consistent_type(vp)); 8882 rp = VTOR4(vp); 8883 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8884 8885 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8886 8887 if (contents != NULL) { 8888 mutex_enter(&rp->r_statelock); 8889 if (rp->r_symlink.contents == NULL) { 8890 rp->r_symlink.len = strlen(tnm); 8891 bcopy(tnm, contents, rp->r_symlink.len); 8892 rp->r_symlink.contents = contents; 8893 rp->r_symlink.size = MAXPATHLEN; 8894 mutex_exit(&rp->r_statelock); 8895 } else { 8896 mutex_exit(&rp->r_statelock); 8897 kmem_free((void *)contents, MAXPATHLEN); 8898 } 8899 } 8900 } 8901 VN_RELE(vp); 8902 8903 return (error); 8904 } 8905 8906 8907 /* 8908 * Read directory entries. 8909 * There are some weird things to look out for here. The uio_loffset 8910 * field is either 0 or it is the offset returned from a previous 8911 * readdir. It is an opaque value used by the server to find the 8912 * correct directory block to read. The count field is the number 8913 * of blocks to read on the server. This is advisory only, the server 8914 * may return only one block's worth of entries. Entries may be compressed 8915 * on the server. 8916 */ 8917 /* ARGSUSED */ 8918 static int 8919 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp, 8920 caller_context_t *ct, int flags) 8921 { 8922 int error; 8923 uint_t count; 8924 rnode4_t *rp; 8925 rddir4_cache *rdc; 8926 rddir4_cache *rrdc; 8927 8928 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8929 return (EIO); 8930 rp = VTOR4(vp); 8931 8932 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8933 8934 /* 8935 * Make sure that the directory cache is valid. 8936 */ 8937 if (rp->r_dir != NULL) { 8938 if (nfs_disable_rddir_cache != 0) { 8939 /* 8940 * Setting nfs_disable_rddir_cache in /etc/system 8941 * allows interoperability with servers that do not 8942 * properly update the attributes of directories. 8943 * Any cached information gets purged before an 8944 * access is made to it. 8945 */ 8946 nfs4_purge_rddir_cache(vp); 8947 } 8948 8949 error = nfs4_validate_caches(vp, cr); 8950 if (error) 8951 return (error); 8952 } 8953 8954 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8955 8956 /* 8957 * Short circuit last readdir which always returns 0 bytes. 8958 * This can be done after the directory has been read through 8959 * completely at least once. This will set r_direof which 8960 * can be used to find the value of the last cookie. 8961 */ 8962 mutex_enter(&rp->r_statelock); 8963 if (rp->r_direof != NULL && 8964 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8965 mutex_exit(&rp->r_statelock); 8966 #ifdef DEBUG 8967 nfs4_readdir_cache_shorts++; 8968 #endif 8969 if (eofp) 8970 *eofp = 1; 8971 return (0); 8972 } 8973 8974 /* 8975 * Look for a cache entry. Cache entries are identified 8976 * by the NFS cookie value and the byte count requested. 8977 */ 8978 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8979 8980 /* 8981 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8982 */ 8983 if (rdc == NULL) { 8984 mutex_exit(&rp->r_statelock); 8985 return (EINTR); 8986 } 8987 8988 /* 8989 * Check to see if we need to fill this entry in. 8990 */ 8991 if (rdc->flags & RDDIRREQ) { 8992 rdc->flags &= ~RDDIRREQ; 8993 rdc->flags |= RDDIR; 8994 mutex_exit(&rp->r_statelock); 8995 8996 /* 8997 * Do the readdir. 8998 */ 8999 nfs4readdir(vp, rdc, cr); 9000 9001 /* 9002 * Reacquire the lock, so that we can continue 9003 */ 9004 mutex_enter(&rp->r_statelock); 9005 /* 9006 * The entry is now complete 9007 */ 9008 rdc->flags &= ~RDDIR; 9009 } 9010 9011 ASSERT(!(rdc->flags & RDDIR)); 9012 9013 /* 9014 * If an error occurred while attempting 9015 * to fill the cache entry, mark the entry invalid and 9016 * just return the error. 9017 */ 9018 if (rdc->error) { 9019 error = rdc->error; 9020 rdc->flags |= RDDIRREQ; 9021 rddir4_cache_rele(rp, rdc); 9022 mutex_exit(&rp->r_statelock); 9023 return (error); 9024 } 9025 9026 /* 9027 * The cache entry is complete and good, 9028 * copyout the dirent structs to the calling 9029 * thread. 9030 */ 9031 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 9032 9033 /* 9034 * If no error occurred during the copyout, 9035 * update the offset in the uio struct to 9036 * contain the value of the next NFS 4 cookie 9037 * and set the eof value appropriately. 9038 */ 9039 if (!error) { 9040 uiop->uio_loffset = rdc->nfs4_ncookie; 9041 if (eofp) 9042 *eofp = rdc->eof; 9043 } 9044 9045 /* 9046 * Decide whether to do readahead. Don't if we 9047 * have already read to the end of directory. 9048 */ 9049 if (rdc->eof) { 9050 /* 9051 * Make the entry the direof only if it is cached 9052 */ 9053 if (rdc->flags & RDDIRCACHED) 9054 rp->r_direof = rdc; 9055 rddir4_cache_rele(rp, rdc); 9056 mutex_exit(&rp->r_statelock); 9057 return (error); 9058 } 9059 9060 /* Determine if a readdir readahead should be done */ 9061 if (!(rp->r_flags & R4LOOKUP)) { 9062 rddir4_cache_rele(rp, rdc); 9063 mutex_exit(&rp->r_statelock); 9064 return (error); 9065 } 9066 9067 /* 9068 * Now look for a readahead entry. 9069 * 9070 * Check to see whether we found an entry for the readahead. 9071 * If so, we don't need to do anything further, so free the new 9072 * entry if one was allocated. Otherwise, allocate a new entry, add 9073 * it to the cache, and then initiate an asynchronous readdir 9074 * operation to fill it. 9075 */ 9076 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 9077 9078 /* 9079 * A readdir cache entry could not be obtained for the readahead. In 9080 * this case we skip the readahead and return. 9081 */ 9082 if (rrdc == NULL) { 9083 rddir4_cache_rele(rp, rdc); 9084 mutex_exit(&rp->r_statelock); 9085 return (error); 9086 } 9087 9088 /* 9089 * Check to see if we need to fill this entry in. 9090 */ 9091 if (rrdc->flags & RDDIRREQ) { 9092 rrdc->flags &= ~RDDIRREQ; 9093 rrdc->flags |= RDDIR; 9094 rddir4_cache_rele(rp, rdc); 9095 mutex_exit(&rp->r_statelock); 9096 #ifdef DEBUG 9097 nfs4_readdir_readahead++; 9098 #endif 9099 /* 9100 * Do the readdir. 9101 */ 9102 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 9103 return (error); 9104 } 9105 9106 rddir4_cache_rele(rp, rrdc); 9107 rddir4_cache_rele(rp, rdc); 9108 mutex_exit(&rp->r_statelock); 9109 return (error); 9110 } 9111 9112 static int 9113 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9114 { 9115 int error; 9116 rnode4_t *rp; 9117 9118 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9119 9120 rp = VTOR4(vp); 9121 9122 /* 9123 * Obtain the readdir results for the caller. 9124 */ 9125 nfs4readdir(vp, rdc, cr); 9126 9127 mutex_enter(&rp->r_statelock); 9128 /* 9129 * The entry is now complete 9130 */ 9131 rdc->flags &= ~RDDIR; 9132 9133 error = rdc->error; 9134 if (error) 9135 rdc->flags |= RDDIRREQ; 9136 rddir4_cache_rele(rp, rdc); 9137 mutex_exit(&rp->r_statelock); 9138 9139 return (error); 9140 } 9141 9142 /* 9143 * Read directory entries. 9144 * There are some weird things to look out for here. The uio_loffset 9145 * field is either 0 or it is the offset returned from a previous 9146 * readdir. It is an opaque value used by the server to find the 9147 * correct directory block to read. The count field is the number 9148 * of blocks to read on the server. This is advisory only, the server 9149 * may return only one block's worth of entries. Entries may be compressed 9150 * on the server. 9151 * 9152 * Generates the following compound request: 9153 * 1. If readdir offset is zero and no dnlc entry for parent exists, 9154 * must include a Lookupp as well. In this case, send: 9155 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 9156 * 2. Otherwise just do: { Putfh <fh>; Readdir } 9157 * 9158 * Get complete attributes and filehandles for entries if this is the 9159 * first read of the directory. Otherwise, just get fileid's. 9160 */ 9161 static void 9162 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9163 { 9164 COMPOUND4args_clnt args; 9165 COMPOUND4res_clnt res; 9166 READDIR4args *rargs; 9167 READDIR4res_clnt *rd_res; 9168 bitmap4 rd_bitsval; 9169 nfs_argop4 argop[5]; 9170 nfs_resop4 *resop; 9171 rnode4_t *rp = VTOR4(vp); 9172 mntinfo4_t *mi = VTOMI4(vp); 9173 int doqueue; 9174 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 9175 vnode_t *dvp; 9176 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 9177 int num_ops, res_opcnt; 9178 bool_t needrecov = FALSE; 9179 nfs4_recov_state_t recov_state; 9180 hrtime_t t; 9181 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 9182 9183 ASSERT(nfs_zone() == mi->mi_zone); 9184 ASSERT(rdc->flags & RDDIR); 9185 ASSERT(rdc->entries == NULL); 9186 9187 /* 9188 * If rp were a stub, it should have triggered and caused 9189 * a mount for us to get this far. 9190 */ 9191 ASSERT(!RP_ISSTUB(rp)); 9192 9193 num_ops = 2; 9194 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9195 /* 9196 * Since nfsv4 readdir may not return entries for "." and "..", 9197 * the client must recreate them: 9198 * To find the correct nodeid, do the following: 9199 * For current node, get nodeid from dnlc. 9200 * - if current node is rootvp, set pnodeid to nodeid. 9201 * - else if parent is in the dnlc, get its nodeid from there. 9202 * - else add LOOKUPP+GETATTR to compound. 9203 */ 9204 nodeid = rp->r_attr.va_nodeid; 9205 if (vp->v_flag & VROOT) { 9206 pnodeid = nodeid; /* root of mount point */ 9207 } else { 9208 dvp = dnlc_lookup(vp, ".."); 9209 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9210 /* parent in dnlc cache - no need for otw */ 9211 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9212 } else { 9213 /* 9214 * parent not in dnlc cache, 9215 * do lookupp to get its id 9216 */ 9217 num_ops = 5; 9218 pnodeid = 0; /* set later by getattr parent */ 9219 } 9220 if (dvp) 9221 VN_RELE(dvp); 9222 } 9223 } 9224 recov_state.rs_flags = 0; 9225 recov_state.rs_num_retry_despite_err = 0; 9226 9227 /* Save the original mount point security flavor */ 9228 (void) save_mnt_secinfo(mi->mi_curr_serv); 9229 9230 recov_retry: 9231 args.ctag = TAG_READDIR; 9232 9233 args.array = argop; 9234 args.array_len = num_ops; 9235 9236 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9237 &recov_state, NULL)) { 9238 /* 9239 * If readdir a node that is a stub for a crossed mount point, 9240 * keep the original secinfo flavor for the current file 9241 * system, not the crossed one. 9242 */ 9243 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9244 rdc->error = e.error; 9245 return; 9246 } 9247 9248 /* 9249 * Determine which attrs to request for dirents. This code 9250 * must be protected by nfs4_start/end_fop because of r_server 9251 * (which will change during failover recovery). 9252 * 9253 */ 9254 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9255 /* 9256 * Get all vattr attrs plus filehandle and rdattr_error 9257 */ 9258 rd_bitsval = NFS4_VATTR_MASK | 9259 FATTR4_RDATTR_ERROR_MASK | 9260 FATTR4_FILEHANDLE_MASK; 9261 9262 if (rp->r_flags & R4READDIRWATTR) { 9263 mutex_enter(&rp->r_statelock); 9264 rp->r_flags &= ~R4READDIRWATTR; 9265 mutex_exit(&rp->r_statelock); 9266 } 9267 } else { 9268 servinfo4_t *svp = rp->r_server; 9269 9270 /* 9271 * Already read directory. Use readdir with 9272 * no attrs (except for mounted_on_fileid) for updates. 9273 */ 9274 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9275 9276 /* 9277 * request mounted on fileid if supported, else request 9278 * fileid. maybe we should verify that fileid is supported 9279 * and request something else if not. 9280 */ 9281 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9282 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9283 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9284 nfs_rw_exit(&svp->sv_lock); 9285 } 9286 9287 /* putfh directory fh */ 9288 argop[0].argop = OP_CPUTFH; 9289 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9290 9291 argop[1].argop = OP_READDIR; 9292 rargs = &argop[1].nfs_argop4_u.opreaddir; 9293 /* 9294 * 1 and 2 are reserved for client "." and ".." entry offset. 9295 * cookie 0 should be used over-the-wire to start reading at 9296 * the beginning of the directory excluding "." and "..". 9297 */ 9298 if (rdc->nfs4_cookie == 0 || 9299 rdc->nfs4_cookie == 1 || 9300 rdc->nfs4_cookie == 2) { 9301 rargs->cookie = (nfs_cookie4)0; 9302 rargs->cookieverf = 0; 9303 } else { 9304 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9305 mutex_enter(&rp->r_statelock); 9306 rargs->cookieverf = rp->r_cookieverf4; 9307 mutex_exit(&rp->r_statelock); 9308 } 9309 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9310 rargs->maxcount = mi->mi_tsize; 9311 rargs->attr_request = rd_bitsval; 9312 rargs->rdc = rdc; 9313 rargs->dvp = vp; 9314 rargs->mi = mi; 9315 rargs->cr = cr; 9316 9317 9318 /* 9319 * If count < than the minimum required, we return no entries 9320 * and fail with EINVAL 9321 */ 9322 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9323 rdc->error = EINVAL; 9324 goto out; 9325 } 9326 9327 if (args.array_len == 5) { 9328 /* 9329 * Add lookupp and getattr for parent nodeid. 9330 */ 9331 argop[2].argop = OP_LOOKUPP; 9332 9333 argop[3].argop = OP_GETFH; 9334 9335 /* getattr parent */ 9336 argop[4].argop = OP_GETATTR; 9337 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9338 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9339 } 9340 9341 doqueue = 1; 9342 9343 if (mi->mi_io_kstats) { 9344 mutex_enter(&mi->mi_lock); 9345 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9346 mutex_exit(&mi->mi_lock); 9347 } 9348 9349 /* capture the time of this call */ 9350 rargs->t = t = gethrtime(); 9351 9352 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9353 9354 if (mi->mi_io_kstats) { 9355 mutex_enter(&mi->mi_lock); 9356 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9357 mutex_exit(&mi->mi_lock); 9358 } 9359 9360 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9361 9362 /* 9363 * If RPC error occurred and it isn't an error that 9364 * triggers recovery, then go ahead and fail now. 9365 */ 9366 if (e.error != 0 && !needrecov) { 9367 rdc->error = e.error; 9368 goto out; 9369 } 9370 9371 if (needrecov) { 9372 bool_t abort; 9373 9374 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9375 "nfs4readdir: initiating recovery.\n")); 9376 9377 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9378 NULL, OP_READDIR, NULL, NULL, NULL); 9379 if (abort == FALSE) { 9380 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9381 &recov_state, needrecov); 9382 if (!e.error) 9383 (void) xdr_free(xdr_COMPOUND4res_clnt, 9384 (caddr_t)&res); 9385 if (rdc->entries != NULL) { 9386 kmem_free(rdc->entries, rdc->entlen); 9387 rdc->entries = NULL; 9388 } 9389 goto recov_retry; 9390 } 9391 9392 if (e.error != 0) { 9393 rdc->error = e.error; 9394 goto out; 9395 } 9396 9397 /* fall through for res.status case */ 9398 } 9399 9400 res_opcnt = res.array_len; 9401 9402 /* 9403 * If compound failed first 2 ops (PUTFH+READDIR), then return 9404 * failure here. Subsequent ops are for filling out dot-dot 9405 * dirent, and if they fail, we still want to give the caller 9406 * the dirents returned by (the successful) READDIR op, so we need 9407 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9408 * 9409 * One example where PUTFH+READDIR ops would succeed but 9410 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9411 * but lacks x. In this case, a POSIX server's VOP_READDIR 9412 * would succeed; however, VOP_LOOKUP(..) would fail since no 9413 * x perm. We need to come up with a non-vendor-specific way 9414 * for a POSIX server to return d_ino from dotdot's dirent if 9415 * client only requests mounted_on_fileid, and just say the 9416 * LOOKUPP succeeded and fill out the GETATTR. However, if 9417 * client requested any mandatory attrs, server would be required 9418 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9419 * for dotdot. 9420 */ 9421 9422 if (res.status) { 9423 if (res_opcnt <= 2) { 9424 e.error = geterrno4(res.status); 9425 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9426 &recov_state, needrecov); 9427 nfs4_purge_stale_fh(e.error, vp, cr); 9428 rdc->error = e.error; 9429 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9430 if (rdc->entries != NULL) { 9431 kmem_free(rdc->entries, rdc->entlen); 9432 rdc->entries = NULL; 9433 } 9434 /* 9435 * If readdir a node that is a stub for a 9436 * crossed mount point, keep the original 9437 * secinfo flavor for the current file system, 9438 * not the crossed one. 9439 */ 9440 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9441 return; 9442 } 9443 } 9444 9445 resop = &res.array[1]; /* readdir res */ 9446 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9447 9448 mutex_enter(&rp->r_statelock); 9449 rp->r_cookieverf4 = rd_res->cookieverf; 9450 mutex_exit(&rp->r_statelock); 9451 9452 /* 9453 * For "." and ".." entries 9454 * e.g. 9455 * seek(cookie=0) -> "." entry with d_off = 1 9456 * seek(cookie=1) -> ".." entry with d_off = 2 9457 */ 9458 if (cookie == (nfs_cookie4) 0) { 9459 if (rd_res->dotp) 9460 rd_res->dotp->d_ino = nodeid; 9461 if (rd_res->dotdotp) 9462 rd_res->dotdotp->d_ino = pnodeid; 9463 } 9464 if (cookie == (nfs_cookie4) 1) { 9465 if (rd_res->dotdotp) 9466 rd_res->dotdotp->d_ino = pnodeid; 9467 } 9468 9469 9470 /* LOOKUPP+GETATTR attemped */ 9471 if (args.array_len == 5 && rd_res->dotdotp) { 9472 if (res.status == NFS4_OK && res_opcnt == 5) { 9473 nfs_fh4 *fhp; 9474 nfs4_sharedfh_t *sfhp; 9475 vnode_t *pvp; 9476 nfs4_ga_res_t *garp; 9477 9478 resop++; /* lookupp */ 9479 resop++; /* getfh */ 9480 fhp = &resop->nfs_resop4_u.opgetfh.object; 9481 9482 resop++; /* getattr of parent */ 9483 9484 /* 9485 * First, take care of finishing the 9486 * readdir results. 9487 */ 9488 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9489 /* 9490 * The d_ino of .. must be the inode number 9491 * of the mounted filesystem. 9492 */ 9493 if (garp->n4g_va.va_mask & AT_NODEID) 9494 rd_res->dotdotp->d_ino = 9495 garp->n4g_va.va_nodeid; 9496 9497 9498 /* 9499 * Next, create the ".." dnlc entry 9500 */ 9501 sfhp = sfh4_get(fhp, mi); 9502 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9503 dnlc_update(vp, "..", pvp); 9504 VN_RELE(pvp); 9505 } 9506 sfh4_rele(&sfhp); 9507 } 9508 } 9509 9510 if (mi->mi_io_kstats) { 9511 mutex_enter(&mi->mi_lock); 9512 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9513 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9514 mutex_exit(&mi->mi_lock); 9515 } 9516 9517 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9518 9519 out: 9520 /* 9521 * If readdir a node that is a stub for a crossed mount point, 9522 * keep the original secinfo flavor for the current file system, 9523 * not the crossed one. 9524 */ 9525 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9526 9527 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9528 } 9529 9530 9531 static int 9532 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9533 { 9534 rnode4_t *rp = VTOR4(bp->b_vp); 9535 int count; 9536 int error; 9537 cred_t *cred_otw = NULL; 9538 offset_t offset; 9539 nfs4_open_stream_t *osp = NULL; 9540 bool_t first_time = TRUE; /* first time getting otw cred */ 9541 bool_t last_time = FALSE; /* last time getting otw cred */ 9542 9543 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9544 9545 DTRACE_IO1(start, struct buf *, bp); 9546 offset = ldbtob(bp->b_lblkno); 9547 9548 if (bp->b_flags & B_READ) { 9549 read_again: 9550 /* 9551 * Releases the osp, if it is provided. 9552 * Puts a hold on the cred_otw and the new osp (if found). 9553 */ 9554 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9555 &first_time, &last_time); 9556 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9557 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9558 readahead, NULL); 9559 crfree(cred_otw); 9560 if (!error) { 9561 if (bp->b_resid) { 9562 /* 9563 * Didn't get it all because we hit EOF, 9564 * zero all the memory beyond the EOF. 9565 */ 9566 /* bzero(rdaddr + */ 9567 bzero(bp->b_un.b_addr + 9568 bp->b_bcount - bp->b_resid, bp->b_resid); 9569 } 9570 mutex_enter(&rp->r_statelock); 9571 if (bp->b_resid == bp->b_bcount && 9572 offset >= rp->r_size) { 9573 /* 9574 * We didn't read anything at all as we are 9575 * past EOF. Return an error indicator back 9576 * but don't destroy the pages (yet). 9577 */ 9578 error = NFS_EOF; 9579 } 9580 mutex_exit(&rp->r_statelock); 9581 } else if (error == EACCES && last_time == FALSE) { 9582 goto read_again; 9583 } 9584 } else { 9585 if (!(rp->r_flags & R4STALE)) { 9586 write_again: 9587 /* 9588 * Releases the osp, if it is provided. 9589 * Puts a hold on the cred_otw and the new 9590 * osp (if found). 9591 */ 9592 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9593 &first_time, &last_time); 9594 mutex_enter(&rp->r_statelock); 9595 count = MIN(bp->b_bcount, rp->r_size - offset); 9596 mutex_exit(&rp->r_statelock); 9597 if (count < 0) 9598 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9599 #ifdef DEBUG 9600 if (count == 0) { 9601 zoneid_t zoneid = getzoneid(); 9602 9603 zcmn_err(zoneid, CE_WARN, 9604 "nfs4_bio: zero length write at %lld", 9605 offset); 9606 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9607 "b_bcount=%ld, file size=%lld", 9608 rp->r_flags, (long)bp->b_bcount, 9609 rp->r_size); 9610 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9611 if (nfs4_bio_do_stop) 9612 debug_enter("nfs4_bio"); 9613 } 9614 #endif 9615 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9616 count, cred_otw, stab_comm); 9617 if (error == EACCES && last_time == FALSE) { 9618 crfree(cred_otw); 9619 goto write_again; 9620 } 9621 bp->b_error = error; 9622 if (error && error != EINTR && 9623 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9624 /* 9625 * Don't print EDQUOT errors on the console. 9626 * Don't print asynchronous EACCES errors. 9627 * Don't print EFBIG errors. 9628 * Print all other write errors. 9629 */ 9630 if (error != EDQUOT && error != EFBIG && 9631 (error != EACCES || 9632 !(bp->b_flags & B_ASYNC))) 9633 nfs4_write_error(bp->b_vp, 9634 error, cred_otw); 9635 /* 9636 * Update r_error and r_flags as appropriate. 9637 * If the error was ESTALE, then mark the 9638 * rnode as not being writeable and save 9639 * the error status. Otherwise, save any 9640 * errors which occur from asynchronous 9641 * page invalidations. Any errors occurring 9642 * from other operations should be saved 9643 * by the caller. 9644 */ 9645 mutex_enter(&rp->r_statelock); 9646 if (error == ESTALE) { 9647 rp->r_flags |= R4STALE; 9648 if (!rp->r_error) 9649 rp->r_error = error; 9650 } else if (!rp->r_error && 9651 (bp->b_flags & 9652 (B_INVAL|B_FORCE|B_ASYNC)) == 9653 (B_INVAL|B_FORCE|B_ASYNC)) { 9654 rp->r_error = error; 9655 } 9656 mutex_exit(&rp->r_statelock); 9657 } 9658 crfree(cred_otw); 9659 } else { 9660 error = rp->r_error; 9661 /* 9662 * A close may have cleared r_error, if so, 9663 * propagate ESTALE error return properly 9664 */ 9665 if (error == 0) 9666 error = ESTALE; 9667 } 9668 } 9669 9670 if (error != 0 && error != NFS_EOF) 9671 bp->b_flags |= B_ERROR; 9672 9673 if (osp) 9674 open_stream_rele(osp, rp); 9675 9676 DTRACE_IO1(done, struct buf *, bp); 9677 9678 return (error); 9679 } 9680 9681 /* ARGSUSED */ 9682 int 9683 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 9684 { 9685 return (EREMOTE); 9686 } 9687 9688 /* ARGSUSED2 */ 9689 int 9690 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9691 { 9692 rnode4_t *rp = VTOR4(vp); 9693 9694 if (!write_lock) { 9695 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9696 return (V_WRITELOCK_FALSE); 9697 } 9698 9699 if ((rp->r_flags & R4DIRECTIO) || 9700 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9701 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9702 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9703 return (V_WRITELOCK_FALSE); 9704 nfs_rw_exit(&rp->r_rwlock); 9705 } 9706 9707 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9708 return (V_WRITELOCK_TRUE); 9709 } 9710 9711 /* ARGSUSED */ 9712 void 9713 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9714 { 9715 rnode4_t *rp = VTOR4(vp); 9716 9717 nfs_rw_exit(&rp->r_rwlock); 9718 } 9719 9720 /* ARGSUSED */ 9721 static int 9722 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 9723 { 9724 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9725 return (EIO); 9726 9727 /* 9728 * Because we stuff the readdir cookie into the offset field 9729 * someone may attempt to do an lseek with the cookie which 9730 * we want to succeed. 9731 */ 9732 if (vp->v_type == VDIR) 9733 return (0); 9734 if (*noffp < 0) 9735 return (EINVAL); 9736 return (0); 9737 } 9738 9739 9740 /* 9741 * Return all the pages from [off..off+len) in file 9742 */ 9743 /* ARGSUSED */ 9744 static int 9745 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9746 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9747 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 9748 { 9749 rnode4_t *rp; 9750 int error; 9751 mntinfo4_t *mi; 9752 9753 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9754 return (EIO); 9755 rp = VTOR4(vp); 9756 if (IS_SHADOW(vp, rp)) 9757 vp = RTOV4(rp); 9758 9759 if (vp->v_flag & VNOMAP) 9760 return (ENOSYS); 9761 9762 if (protp != NULL) 9763 *protp = PROT_ALL; 9764 9765 /* 9766 * Now validate that the caches are up to date. 9767 */ 9768 if (error = nfs4_validate_caches(vp, cr)) 9769 return (error); 9770 9771 mi = VTOMI4(vp); 9772 retry: 9773 mutex_enter(&rp->r_statelock); 9774 9775 /* 9776 * Don't create dirty pages faster than they 9777 * can be cleaned so that the system doesn't 9778 * get imbalanced. If the async queue is 9779 * maxed out, then wait for it to drain before 9780 * creating more dirty pages. Also, wait for 9781 * any threads doing pagewalks in the vop_getattr 9782 * entry points so that they don't block for 9783 * long periods. 9784 */ 9785 if (rw == S_CREATE) { 9786 while ((mi->mi_max_threads != 0 && 9787 rp->r_awcount > 2 * mi->mi_max_threads) || 9788 rp->r_gcount > 0) 9789 cv_wait(&rp->r_cv, &rp->r_statelock); 9790 } 9791 9792 /* 9793 * If we are getting called as a side effect of an nfs_write() 9794 * operation the local file size might not be extended yet. 9795 * In this case we want to be able to return pages of zeroes. 9796 */ 9797 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9798 NFS4_DEBUG(nfs4_pageio_debug, 9799 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9800 "len=%llu, size=%llu, attrsize =%llu", off, 9801 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9802 mutex_exit(&rp->r_statelock); 9803 return (EFAULT); /* beyond EOF */ 9804 } 9805 9806 mutex_exit(&rp->r_statelock); 9807 9808 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9809 pl, plsz, seg, addr, rw, cr); 9810 NFS4_DEBUG(nfs4_pageio_debug && error, 9811 (CE_NOTE, "getpages error %d; off=%lld, len=%lld", 9812 error, off, (u_longlong_t)len)); 9813 9814 switch (error) { 9815 case NFS_EOF: 9816 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9817 goto retry; 9818 case ESTALE: 9819 nfs4_purge_stale_fh(error, vp, cr); 9820 } 9821 9822 return (error); 9823 } 9824 9825 /* 9826 * Called from pvn_getpages to get a particular page. 9827 */ 9828 /* ARGSUSED */ 9829 static int 9830 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9831 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9832 enum seg_rw rw, cred_t *cr) 9833 { 9834 rnode4_t *rp; 9835 uint_t bsize; 9836 struct buf *bp; 9837 page_t *pp; 9838 u_offset_t lbn; 9839 u_offset_t io_off; 9840 u_offset_t blkoff; 9841 u_offset_t rablkoff; 9842 size_t io_len; 9843 uint_t blksize; 9844 int error; 9845 int readahead; 9846 int readahead_issued = 0; 9847 int ra_window; /* readahead window */ 9848 page_t *pagefound; 9849 page_t *savepp; 9850 9851 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9852 return (EIO); 9853 9854 rp = VTOR4(vp); 9855 ASSERT(!IS_SHADOW(vp, rp)); 9856 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9857 9858 reread: 9859 bp = NULL; 9860 pp = NULL; 9861 pagefound = NULL; 9862 9863 if (pl != NULL) 9864 pl[0] = NULL; 9865 9866 error = 0; 9867 lbn = off / bsize; 9868 blkoff = lbn * bsize; 9869 9870 /* 9871 * Queueing up the readahead before doing the synchronous read 9872 * results in a significant increase in read throughput because 9873 * of the increased parallelism between the async threads and 9874 * the process context. 9875 */ 9876 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9877 rw != S_CREATE && 9878 !(vp->v_flag & VNOCACHE)) { 9879 mutex_enter(&rp->r_statelock); 9880 9881 /* 9882 * Calculate the number of readaheads to do. 9883 * a) No readaheads at offset = 0. 9884 * b) Do maximum(nfs4_nra) readaheads when the readahead 9885 * window is closed. 9886 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9887 * upon how far the readahead window is open or close. 9888 * d) No readaheads if rp->r_nextr is not within the scope 9889 * of the readahead window (random i/o). 9890 */ 9891 9892 if (off == 0) 9893 readahead = 0; 9894 else if (blkoff == rp->r_nextr) 9895 readahead = nfs4_nra; 9896 else if (rp->r_nextr > blkoff && 9897 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9898 <= (nfs4_nra - 1))) 9899 readahead = nfs4_nra - ra_window; 9900 else 9901 readahead = 0; 9902 9903 rablkoff = rp->r_nextr; 9904 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9905 mutex_exit(&rp->r_statelock); 9906 if (nfs4_async_readahead(vp, rablkoff + bsize, 9907 addr + (rablkoff + bsize - off), 9908 seg, cr, nfs4_readahead) < 0) { 9909 mutex_enter(&rp->r_statelock); 9910 break; 9911 } 9912 readahead--; 9913 rablkoff += bsize; 9914 /* 9915 * Indicate that we did a readahead so 9916 * readahead offset is not updated 9917 * by the synchronous read below. 9918 */ 9919 readahead_issued = 1; 9920 mutex_enter(&rp->r_statelock); 9921 /* 9922 * set readahead offset to 9923 * offset of last async readahead 9924 * request. 9925 */ 9926 rp->r_nextr = rablkoff; 9927 } 9928 mutex_exit(&rp->r_statelock); 9929 } 9930 9931 again: 9932 if ((pagefound = page_exists(vp, off)) == NULL) { 9933 if (pl == NULL) { 9934 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9935 nfs4_readahead); 9936 } else if (rw == S_CREATE) { 9937 /* 9938 * Block for this page is not allocated, or the offset 9939 * is beyond the current allocation size, or we're 9940 * allocating a swap slot and the page was not found, 9941 * so allocate it and return a zero page. 9942 */ 9943 if ((pp = page_create_va(vp, off, 9944 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9945 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9946 io_len = PAGESIZE; 9947 mutex_enter(&rp->r_statelock); 9948 rp->r_nextr = off + PAGESIZE; 9949 mutex_exit(&rp->r_statelock); 9950 } else { 9951 /* 9952 * Need to go to server to get a block 9953 */ 9954 mutex_enter(&rp->r_statelock); 9955 if (blkoff < rp->r_size && 9956 blkoff + bsize > rp->r_size) { 9957 /* 9958 * If less than a block left in 9959 * file read less than a block. 9960 */ 9961 if (rp->r_size <= off) { 9962 /* 9963 * Trying to access beyond EOF, 9964 * set up to get at least one page. 9965 */ 9966 blksize = off + PAGESIZE - blkoff; 9967 } else 9968 blksize = rp->r_size - blkoff; 9969 } else if ((off == 0) || 9970 (off != rp->r_nextr && !readahead_issued)) { 9971 blksize = PAGESIZE; 9972 blkoff = off; /* block = page here */ 9973 } else 9974 blksize = bsize; 9975 mutex_exit(&rp->r_statelock); 9976 9977 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9978 &io_len, blkoff, blksize, 0); 9979 9980 /* 9981 * Some other thread has entered the page, 9982 * so just use it. 9983 */ 9984 if (pp == NULL) 9985 goto again; 9986 9987 /* 9988 * Now round the request size up to page boundaries. 9989 * This ensures that the entire page will be 9990 * initialized to zeroes if EOF is encountered. 9991 */ 9992 io_len = ptob(btopr(io_len)); 9993 9994 bp = pageio_setup(pp, io_len, vp, B_READ); 9995 ASSERT(bp != NULL); 9996 9997 /* 9998 * pageio_setup should have set b_addr to 0. This 9999 * is correct since we want to do I/O on a page 10000 * boundary. bp_mapin will use this addr to calculate 10001 * an offset, and then set b_addr to the kernel virtual 10002 * address it allocated for us. 10003 */ 10004 ASSERT(bp->b_un.b_addr == 0); 10005 10006 bp->b_edev = 0; 10007 bp->b_dev = 0; 10008 bp->b_lblkno = lbtodb(io_off); 10009 bp->b_file = vp; 10010 bp->b_offset = (offset_t)off; 10011 bp_mapin(bp); 10012 10013 /* 10014 * If doing a write beyond what we believe is EOF, 10015 * don't bother trying to read the pages from the 10016 * server, we'll just zero the pages here. We 10017 * don't check that the rw flag is S_WRITE here 10018 * because some implementations may attempt a 10019 * read access to the buffer before copying data. 10020 */ 10021 mutex_enter(&rp->r_statelock); 10022 if (io_off >= rp->r_size && seg == segkmap) { 10023 mutex_exit(&rp->r_statelock); 10024 bzero(bp->b_un.b_addr, io_len); 10025 } else { 10026 mutex_exit(&rp->r_statelock); 10027 error = nfs4_bio(bp, NULL, cr, FALSE); 10028 } 10029 10030 /* 10031 * Unmap the buffer before freeing it. 10032 */ 10033 bp_mapout(bp); 10034 pageio_done(bp); 10035 10036 savepp = pp; 10037 do { 10038 pp->p_fsdata = C_NOCOMMIT; 10039 } while ((pp = pp->p_next) != savepp); 10040 10041 if (error == NFS_EOF) { 10042 /* 10043 * If doing a write system call just return 10044 * zeroed pages, else user tried to get pages 10045 * beyond EOF, return error. We don't check 10046 * that the rw flag is S_WRITE here because 10047 * some implementations may attempt a read 10048 * access to the buffer before copying data. 10049 */ 10050 if (seg == segkmap) 10051 error = 0; 10052 else 10053 error = EFAULT; 10054 } 10055 10056 if (!readahead_issued && !error) { 10057 mutex_enter(&rp->r_statelock); 10058 rp->r_nextr = io_off + io_len; 10059 mutex_exit(&rp->r_statelock); 10060 } 10061 } 10062 } 10063 10064 out: 10065 if (pl == NULL) 10066 return (error); 10067 10068 if (error) { 10069 if (pp != NULL) 10070 pvn_read_done(pp, B_ERROR); 10071 return (error); 10072 } 10073 10074 if (pagefound) { 10075 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 10076 10077 /* 10078 * Page exists in the cache, acquire the appropriate lock. 10079 * If this fails, start all over again. 10080 */ 10081 if ((pp = page_lookup(vp, off, se)) == NULL) { 10082 #ifdef DEBUG 10083 nfs4_lostpage++; 10084 #endif 10085 goto reread; 10086 } 10087 pl[0] = pp; 10088 pl[1] = NULL; 10089 return (0); 10090 } 10091 10092 if (pp != NULL) 10093 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 10094 10095 return (error); 10096 } 10097 10098 static void 10099 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 10100 cred_t *cr) 10101 { 10102 int error; 10103 page_t *pp; 10104 u_offset_t io_off; 10105 size_t io_len; 10106 struct buf *bp; 10107 uint_t bsize, blksize; 10108 rnode4_t *rp = VTOR4(vp); 10109 page_t *savepp; 10110 10111 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10112 10113 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10114 10115 mutex_enter(&rp->r_statelock); 10116 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 10117 /* 10118 * If less than a block left in file read less 10119 * than a block. 10120 */ 10121 blksize = rp->r_size - blkoff; 10122 } else 10123 blksize = bsize; 10124 mutex_exit(&rp->r_statelock); 10125 10126 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 10127 &io_off, &io_len, blkoff, blksize, 1); 10128 /* 10129 * The isra flag passed to the kluster function is 1, we may have 10130 * gotten a return value of NULL for a variety of reasons (# of free 10131 * pages < minfree, someone entered the page on the vnode etc). In all 10132 * cases, we want to punt on the readahead. 10133 */ 10134 if (pp == NULL) 10135 return; 10136 10137 /* 10138 * Now round the request size up to page boundaries. 10139 * This ensures that the entire page will be 10140 * initialized to zeroes if EOF is encountered. 10141 */ 10142 io_len = ptob(btopr(io_len)); 10143 10144 bp = pageio_setup(pp, io_len, vp, B_READ); 10145 ASSERT(bp != NULL); 10146 10147 /* 10148 * pageio_setup should have set b_addr to 0. This is correct since 10149 * we want to do I/O on a page boundary. bp_mapin() will use this addr 10150 * to calculate an offset, and then set b_addr to the kernel virtual 10151 * address it allocated for us. 10152 */ 10153 ASSERT(bp->b_un.b_addr == 0); 10154 10155 bp->b_edev = 0; 10156 bp->b_dev = 0; 10157 bp->b_lblkno = lbtodb(io_off); 10158 bp->b_file = vp; 10159 bp->b_offset = (offset_t)blkoff; 10160 bp_mapin(bp); 10161 10162 /* 10163 * If doing a write beyond what we believe is EOF, don't bother trying 10164 * to read the pages from the server, we'll just zero the pages here. 10165 * We don't check that the rw flag is S_WRITE here because some 10166 * implementations may attempt a read access to the buffer before 10167 * copying data. 10168 */ 10169 mutex_enter(&rp->r_statelock); 10170 if (io_off >= rp->r_size && seg == segkmap) { 10171 mutex_exit(&rp->r_statelock); 10172 bzero(bp->b_un.b_addr, io_len); 10173 error = 0; 10174 } else { 10175 mutex_exit(&rp->r_statelock); 10176 error = nfs4_bio(bp, NULL, cr, TRUE); 10177 if (error == NFS_EOF) 10178 error = 0; 10179 } 10180 10181 /* 10182 * Unmap the buffer before freeing it. 10183 */ 10184 bp_mapout(bp); 10185 pageio_done(bp); 10186 10187 savepp = pp; 10188 do { 10189 pp->p_fsdata = C_NOCOMMIT; 10190 } while ((pp = pp->p_next) != savepp); 10191 10192 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10193 10194 /* 10195 * In case of error set readahead offset 10196 * to the lowest offset. 10197 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10198 */ 10199 if (error && rp->r_nextr > io_off) { 10200 mutex_enter(&rp->r_statelock); 10201 if (rp->r_nextr > io_off) 10202 rp->r_nextr = io_off; 10203 mutex_exit(&rp->r_statelock); 10204 } 10205 } 10206 10207 /* 10208 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10209 * If len == 0, do from off to EOF. 10210 * 10211 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10212 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10213 * (from pageout). 10214 */ 10215 /* ARGSUSED */ 10216 static int 10217 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 10218 caller_context_t *ct) 10219 { 10220 int error; 10221 rnode4_t *rp; 10222 10223 ASSERT(cr != NULL); 10224 10225 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10226 return (EIO); 10227 10228 rp = VTOR4(vp); 10229 if (IS_SHADOW(vp, rp)) 10230 vp = RTOV4(rp); 10231 10232 /* 10233 * XXX - Why should this check be made here? 10234 */ 10235 if (vp->v_flag & VNOMAP) 10236 return (ENOSYS); 10237 10238 if (len == 0 && !(flags & B_INVAL) && 10239 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10240 return (0); 10241 10242 mutex_enter(&rp->r_statelock); 10243 rp->r_count++; 10244 mutex_exit(&rp->r_statelock); 10245 error = nfs4_putpages(vp, off, len, flags, cr); 10246 mutex_enter(&rp->r_statelock); 10247 rp->r_count--; 10248 cv_broadcast(&rp->r_cv); 10249 mutex_exit(&rp->r_statelock); 10250 10251 return (error); 10252 } 10253 10254 /* 10255 * Write out a single page, possibly klustering adjacent dirty pages. 10256 */ 10257 int 10258 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10259 int flags, cred_t *cr) 10260 { 10261 u_offset_t io_off; 10262 u_offset_t lbn_off; 10263 u_offset_t lbn; 10264 size_t io_len; 10265 uint_t bsize; 10266 int error; 10267 rnode4_t *rp; 10268 10269 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10270 ASSERT(pp != NULL); 10271 ASSERT(cr != NULL); 10272 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10273 10274 rp = VTOR4(vp); 10275 ASSERT(rp->r_count > 0); 10276 ASSERT(!IS_SHADOW(vp, rp)); 10277 10278 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10279 lbn = pp->p_offset / bsize; 10280 lbn_off = lbn * bsize; 10281 10282 /* 10283 * Find a kluster that fits in one block, or in 10284 * one page if pages are bigger than blocks. If 10285 * there is less file space allocated than a whole 10286 * page, we'll shorten the i/o request below. 10287 */ 10288 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10289 roundup(bsize, PAGESIZE), flags); 10290 10291 /* 10292 * pvn_write_kluster shouldn't have returned a page with offset 10293 * behind the original page we were given. Verify that. 10294 */ 10295 ASSERT((pp->p_offset / bsize) >= lbn); 10296 10297 /* 10298 * Now pp will have the list of kept dirty pages marked for 10299 * write back. It will also handle invalidation and freeing 10300 * of pages that are not dirty. Check for page length rounding 10301 * problems. 10302 */ 10303 if (io_off + io_len > lbn_off + bsize) { 10304 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10305 io_len = lbn_off + bsize - io_off; 10306 } 10307 /* 10308 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10309 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10310 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10311 * progress and the r_size has not been made consistent with the 10312 * new size of the file. When the uiomove() completes the r_size is 10313 * updated and the R4MODINPROGRESS flag is cleared. 10314 * 10315 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10316 * consistent value of r_size. Without this handshaking, it is 10317 * possible that nfs4_bio() picks up the old value of r_size 10318 * before the uiomove() in writerp4() completes. This will result 10319 * in the write through nfs4_bio() being dropped. 10320 * 10321 * More precisely, there is a window between the time the uiomove() 10322 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10323 * operation intervenes in this window, the page will be picked up, 10324 * because it is dirty (it will be unlocked, unless it was 10325 * pagecreate'd). When the page is picked up as dirty, the dirty 10326 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10327 * checked. This will still be the old size. Therefore the page will 10328 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10329 * the page will be found to be clean and the write will be dropped. 10330 */ 10331 if (rp->r_flags & R4MODINPROGRESS) { 10332 mutex_enter(&rp->r_statelock); 10333 if ((rp->r_flags & R4MODINPROGRESS) && 10334 rp->r_modaddr + MAXBSIZE > io_off && 10335 rp->r_modaddr < io_off + io_len) { 10336 page_t *plist; 10337 /* 10338 * A write is in progress for this region of the file. 10339 * If we did not detect R4MODINPROGRESS here then this 10340 * path through nfs_putapage() would eventually go to 10341 * nfs4_bio() and may not write out all of the data 10342 * in the pages. We end up losing data. So we decide 10343 * to set the modified bit on each page in the page 10344 * list and mark the rnode with R4DIRTY. This write 10345 * will be restarted at some later time. 10346 */ 10347 plist = pp; 10348 while (plist != NULL) { 10349 pp = plist; 10350 page_sub(&plist, pp); 10351 hat_setmod(pp); 10352 page_io_unlock(pp); 10353 page_unlock(pp); 10354 } 10355 rp->r_flags |= R4DIRTY; 10356 mutex_exit(&rp->r_statelock); 10357 if (offp) 10358 *offp = io_off; 10359 if (lenp) 10360 *lenp = io_len; 10361 return (0); 10362 } 10363 mutex_exit(&rp->r_statelock); 10364 } 10365 10366 if (flags & B_ASYNC) { 10367 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10368 nfs4_sync_putapage); 10369 } else 10370 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10371 10372 if (offp) 10373 *offp = io_off; 10374 if (lenp) 10375 *lenp = io_len; 10376 return (error); 10377 } 10378 10379 static int 10380 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10381 int flags, cred_t *cr) 10382 { 10383 int error; 10384 rnode4_t *rp; 10385 10386 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10387 10388 flags |= B_WRITE; 10389 10390 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10391 10392 rp = VTOR4(vp); 10393 10394 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10395 error == EACCES) && 10396 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10397 if (!(rp->r_flags & R4OUTOFSPACE)) { 10398 mutex_enter(&rp->r_statelock); 10399 rp->r_flags |= R4OUTOFSPACE; 10400 mutex_exit(&rp->r_statelock); 10401 } 10402 flags |= B_ERROR; 10403 pvn_write_done(pp, flags); 10404 /* 10405 * If this was not an async thread, then try again to 10406 * write out the pages, but this time, also destroy 10407 * them whether or not the write is successful. This 10408 * will prevent memory from filling up with these 10409 * pages and destroying them is the only alternative 10410 * if they can't be written out. 10411 * 10412 * Don't do this if this is an async thread because 10413 * when the pages are unlocked in pvn_write_done, 10414 * some other thread could have come along, locked 10415 * them, and queued for an async thread. It would be 10416 * possible for all of the async threads to be tied 10417 * up waiting to lock the pages again and they would 10418 * all already be locked and waiting for an async 10419 * thread to handle them. Deadlock. 10420 */ 10421 if (!(flags & B_ASYNC)) { 10422 error = nfs4_putpage(vp, io_off, io_len, 10423 B_INVAL | B_FORCE, cr, NULL); 10424 } 10425 } else { 10426 if (error) 10427 flags |= B_ERROR; 10428 else if (rp->r_flags & R4OUTOFSPACE) { 10429 mutex_enter(&rp->r_statelock); 10430 rp->r_flags &= ~R4OUTOFSPACE; 10431 mutex_exit(&rp->r_statelock); 10432 } 10433 pvn_write_done(pp, flags); 10434 if (freemem < desfree) 10435 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10436 NFS4_WRITE_NOWAIT); 10437 } 10438 10439 return (error); 10440 } 10441 10442 #ifdef DEBUG 10443 int nfs4_force_open_before_mmap = 0; 10444 #endif 10445 10446 /* ARGSUSED */ 10447 static int 10448 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10449 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10450 caller_context_t *ct) 10451 { 10452 struct segvn_crargs vn_a; 10453 int error = 0; 10454 rnode4_t *rp = VTOR4(vp); 10455 mntinfo4_t *mi = VTOMI4(vp); 10456 10457 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10458 return (EIO); 10459 10460 if (vp->v_flag & VNOMAP) 10461 return (ENOSYS); 10462 10463 if (off < 0 || (off + len) < 0) 10464 return (ENXIO); 10465 10466 if (vp->v_type != VREG) 10467 return (ENODEV); 10468 10469 /* 10470 * If the file is delegated to the client don't do anything. 10471 * If the file is not delegated, then validate the data cache. 10472 */ 10473 mutex_enter(&rp->r_statev4_lock); 10474 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10475 mutex_exit(&rp->r_statev4_lock); 10476 error = nfs4_validate_caches(vp, cr); 10477 if (error) 10478 return (error); 10479 } else { 10480 mutex_exit(&rp->r_statev4_lock); 10481 } 10482 10483 /* 10484 * Check to see if the vnode is currently marked as not cachable. 10485 * This means portions of the file are locked (through VOP_FRLOCK). 10486 * In this case the map request must be refused. We use 10487 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10488 * 10489 * Atomically increment r_inmap after acquiring r_rwlock. The 10490 * idea here is to acquire r_rwlock to block read/write and 10491 * not to protect r_inmap. r_inmap will inform nfs4_read/write() 10492 * that we are in nfs4_map(). Now, r_rwlock is acquired in order 10493 * and we can prevent the deadlock that would have occurred 10494 * when nfs4_addmap() would have acquired it out of order. 10495 * 10496 * Since we are not protecting r_inmap by any lock, we do not 10497 * hold any lock when we decrement it. We atomically decrement 10498 * r_inmap after we release r_lkserlock. 10499 */ 10500 10501 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp))) 10502 return (EINTR); 10503 atomic_inc_uint(&rp->r_inmap); 10504 nfs_rw_exit(&rp->r_rwlock); 10505 10506 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) { 10507 atomic_dec_uint(&rp->r_inmap); 10508 return (EINTR); 10509 } 10510 10511 10512 if (vp->v_flag & VNOCACHE) { 10513 error = EAGAIN; 10514 goto done; 10515 } 10516 10517 /* 10518 * Don't allow concurrent locks and mapping if mandatory locking is 10519 * enabled. 10520 */ 10521 if (flk_has_remote_locks(vp)) { 10522 struct vattr va; 10523 va.va_mask = AT_MODE; 10524 error = nfs4getattr(vp, &va, cr); 10525 if (error != 0) 10526 goto done; 10527 if (MANDLOCK(vp, va.va_mode)) { 10528 error = EAGAIN; 10529 goto done; 10530 } 10531 } 10532 10533 /* 10534 * It is possible that the rnode has a lost lock request that we 10535 * are still trying to recover, and that the request conflicts with 10536 * this map request. 10537 * 10538 * An alternative approach would be for nfs4_safemap() to consider 10539 * queued lock requests when deciding whether to set or clear 10540 * VNOCACHE. This would require the frlock code path to call 10541 * nfs4_safemap() after enqueing a lost request. 10542 */ 10543 if (nfs4_map_lost_lock_conflict(vp)) { 10544 error = EAGAIN; 10545 goto done; 10546 } 10547 10548 as_rangelock(as); 10549 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 10550 if (error != 0) { 10551 as_rangeunlock(as); 10552 goto done; 10553 } 10554 10555 if (vp->v_type == VREG) { 10556 /* 10557 * We need to retrieve the open stream 10558 */ 10559 nfs4_open_stream_t *osp = NULL; 10560 nfs4_open_owner_t *oop = NULL; 10561 10562 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10563 if (oop != NULL) { 10564 /* returns with 'os_sync_lock' held */ 10565 osp = find_open_stream(oop, rp); 10566 open_owner_rele(oop); 10567 } 10568 if (osp == NULL) { 10569 #ifdef DEBUG 10570 if (nfs4_force_open_before_mmap) { 10571 error = EIO; 10572 goto done; 10573 } 10574 #endif 10575 /* returns with 'os_sync_lock' held */ 10576 error = open_and_get_osp(vp, cr, &osp); 10577 if (osp == NULL) { 10578 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10579 "nfs4_map: we tried to OPEN the file " 10580 "but again no osp, so fail with EIO")); 10581 goto done; 10582 } 10583 } 10584 10585 if (osp->os_failed_reopen) { 10586 mutex_exit(&osp->os_sync_lock); 10587 open_stream_rele(osp, rp); 10588 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10589 "nfs4_map: os_failed_reopen set on " 10590 "osp %p, cr %p, rp %s", (void *)osp, 10591 (void *)cr, rnode4info(rp))); 10592 error = EIO; 10593 goto done; 10594 } 10595 mutex_exit(&osp->os_sync_lock); 10596 open_stream_rele(osp, rp); 10597 } 10598 10599 vn_a.vp = vp; 10600 vn_a.offset = off; 10601 vn_a.type = (flags & MAP_TYPE); 10602 vn_a.prot = (uchar_t)prot; 10603 vn_a.maxprot = (uchar_t)maxprot; 10604 vn_a.flags = (flags & ~MAP_TYPE); 10605 vn_a.cred = cr; 10606 vn_a.amp = NULL; 10607 vn_a.szc = 0; 10608 vn_a.lgrp_mem_policy_flags = 0; 10609 10610 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10611 as_rangeunlock(as); 10612 10613 done: 10614 nfs_rw_exit(&rp->r_lkserlock); 10615 atomic_dec_uint(&rp->r_inmap); 10616 return (error); 10617 } 10618 10619 /* 10620 * We're most likely dealing with a kernel module that likes to READ 10621 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10622 * officially OPEN the file to create the necessary client state 10623 * for bookkeeping of os_mmap_read/write counts. 10624 * 10625 * Since VOP_MAP only passes in a pointer to the vnode rather than 10626 * a double pointer, we can't handle the case where nfs4open_otw() 10627 * returns a different vnode than the one passed into VOP_MAP (since 10628 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10629 * we return NULL and let nfs4_map() fail. Note: the only case where 10630 * this should happen is if the file got removed and replaced with the 10631 * same name on the server (in addition to the fact that we're trying 10632 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10633 */ 10634 static int 10635 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10636 { 10637 rnode4_t *rp, *drp; 10638 vnode_t *dvp, *open_vp; 10639 char file_name[MAXNAMELEN]; 10640 int just_created; 10641 nfs4_open_stream_t *osp; 10642 nfs4_open_owner_t *oop; 10643 int error; 10644 10645 *ospp = NULL; 10646 open_vp = map_vp; 10647 10648 rp = VTOR4(open_vp); 10649 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10650 return (error); 10651 drp = VTOR4(dvp); 10652 10653 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10654 VN_RELE(dvp); 10655 return (EINTR); 10656 } 10657 10658 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10659 nfs_rw_exit(&drp->r_rwlock); 10660 VN_RELE(dvp); 10661 return (error); 10662 } 10663 10664 mutex_enter(&rp->r_statev4_lock); 10665 if (rp->created_v4) { 10666 rp->created_v4 = 0; 10667 mutex_exit(&rp->r_statev4_lock); 10668 10669 dnlc_update(dvp, file_name, open_vp); 10670 /* This is needed so we don't bump the open ref count */ 10671 just_created = 1; 10672 } else { 10673 mutex_exit(&rp->r_statev4_lock); 10674 just_created = 0; 10675 } 10676 10677 VN_HOLD(map_vp); 10678 10679 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10680 just_created); 10681 if (error) { 10682 nfs_rw_exit(&drp->r_rwlock); 10683 VN_RELE(dvp); 10684 VN_RELE(map_vp); 10685 return (error); 10686 } 10687 10688 nfs_rw_exit(&drp->r_rwlock); 10689 VN_RELE(dvp); 10690 10691 /* 10692 * If nfs4open_otw() returned a different vnode then "undo" 10693 * the open and return failure to the caller. 10694 */ 10695 if (!VN_CMP(open_vp, map_vp)) { 10696 nfs4_error_t e; 10697 10698 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10699 "open returned a different vnode")); 10700 /* 10701 * If there's an error, ignore it, 10702 * and let VOP_INACTIVE handle it. 10703 */ 10704 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10705 CLOSE_NORM, 0, 0, 0); 10706 VN_RELE(map_vp); 10707 return (EIO); 10708 } 10709 10710 VN_RELE(map_vp); 10711 10712 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10713 if (!oop) { 10714 nfs4_error_t e; 10715 10716 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10717 "no open owner")); 10718 /* 10719 * If there's an error, ignore it, 10720 * and let VOP_INACTIVE handle it. 10721 */ 10722 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10723 CLOSE_NORM, 0, 0, 0); 10724 return (EIO); 10725 } 10726 osp = find_open_stream(oop, rp); 10727 open_owner_rele(oop); 10728 *ospp = osp; 10729 return (0); 10730 } 10731 10732 /* 10733 * Please be aware that when this function is called, the address space write 10734 * a_lock is held. Do not put over the wire calls in this function. 10735 */ 10736 /* ARGSUSED */ 10737 static int 10738 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10739 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10740 caller_context_t *ct) 10741 { 10742 rnode4_t *rp; 10743 int error = 0; 10744 mntinfo4_t *mi; 10745 10746 mi = VTOMI4(vp); 10747 rp = VTOR4(vp); 10748 10749 if (nfs_zone() != mi->mi_zone) 10750 return (EIO); 10751 if (vp->v_flag & VNOMAP) 10752 return (ENOSYS); 10753 10754 /* 10755 * Don't need to update the open stream first, since this 10756 * mmap can't add any additional share access that isn't 10757 * already contained in the open stream (for the case where we 10758 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10759 * take into account os_mmap_read[write] counts). 10760 */ 10761 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10762 10763 if (vp->v_type == VREG) { 10764 /* 10765 * We need to retrieve the open stream and update the counts. 10766 * If there is no open stream here, something is wrong. 10767 */ 10768 nfs4_open_stream_t *osp = NULL; 10769 nfs4_open_owner_t *oop = NULL; 10770 10771 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10772 if (oop != NULL) { 10773 /* returns with 'os_sync_lock' held */ 10774 osp = find_open_stream(oop, rp); 10775 open_owner_rele(oop); 10776 } 10777 if (osp == NULL) { 10778 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10779 "nfs4_addmap: we should have an osp" 10780 "but we don't, so fail with EIO")); 10781 error = EIO; 10782 goto out; 10783 } 10784 10785 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10786 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10787 10788 /* 10789 * Update the map count in the open stream. 10790 * This is necessary in the case where we 10791 * open/mmap/close/, then the server reboots, and we 10792 * attempt to reopen. If the mmap doesn't add share 10793 * access then we send an invalid reopen with 10794 * access = NONE. 10795 * 10796 * We need to specifically check each PROT_* so a mmap 10797 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10798 * read and write access. A simple comparison of prot 10799 * to ~PROT_WRITE to determine read access is insufficient 10800 * since prot can be |= with PROT_USER, etc. 10801 */ 10802 10803 /* 10804 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10805 */ 10806 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10807 osp->os_mmap_write += btopr(len); 10808 if (maxprot & PROT_READ) 10809 osp->os_mmap_read += btopr(len); 10810 if (maxprot & PROT_EXEC) 10811 osp->os_mmap_read += btopr(len); 10812 /* 10813 * Ensure that os_mmap_read gets incremented, even if 10814 * maxprot were to look like PROT_NONE. 10815 */ 10816 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10817 !(maxprot & PROT_EXEC)) 10818 osp->os_mmap_read += btopr(len); 10819 osp->os_mapcnt += btopr(len); 10820 mutex_exit(&osp->os_sync_lock); 10821 open_stream_rele(osp, rp); 10822 } 10823 10824 out: 10825 /* 10826 * If we got an error, then undo our 10827 * incrementing of 'r_mapcnt'. 10828 */ 10829 10830 if (error) { 10831 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10832 ASSERT(rp->r_mapcnt >= 0); 10833 } 10834 return (error); 10835 } 10836 10837 /* ARGSUSED */ 10838 static int 10839 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct) 10840 { 10841 10842 return (VTOR4(vp1) == VTOR4(vp2)); 10843 } 10844 10845 /* ARGSUSED */ 10846 static int 10847 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10848 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr, 10849 caller_context_t *ct) 10850 { 10851 int rc; 10852 u_offset_t start, end; 10853 rnode4_t *rp; 10854 int error = 0, intr = INTR4(vp); 10855 nfs4_error_t e; 10856 10857 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10858 return (EIO); 10859 10860 /* check for valid cmd parameter */ 10861 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10862 return (EINVAL); 10863 10864 /* Verify l_type. */ 10865 switch (bfp->l_type) { 10866 case F_RDLCK: 10867 if (cmd != F_GETLK && !(flag & FREAD)) 10868 return (EBADF); 10869 break; 10870 case F_WRLCK: 10871 if (cmd != F_GETLK && !(flag & FWRITE)) 10872 return (EBADF); 10873 break; 10874 case F_UNLCK: 10875 intr = 0; 10876 break; 10877 10878 default: 10879 return (EINVAL); 10880 } 10881 10882 /* check the validity of the lock range */ 10883 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10884 return (rc); 10885 if (rc = flk_check_lock_data(start, end, MAXEND)) 10886 return (rc); 10887 10888 /* 10889 * If the filesystem is mounted using local locking, pass the 10890 * request off to the local locking code. 10891 */ 10892 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10893 if (cmd == F_SETLK || cmd == F_SETLKW) { 10894 /* 10895 * For complete safety, we should be holding 10896 * r_lkserlock. However, we can't call 10897 * nfs4_safelock and then fs_frlock while 10898 * holding r_lkserlock, so just invoke 10899 * nfs4_safelock and expect that this will 10900 * catch enough of the cases. 10901 */ 10902 if (!nfs4_safelock(vp, bfp, cr)) 10903 return (EAGAIN); 10904 } 10905 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 10906 } 10907 10908 rp = VTOR4(vp); 10909 10910 /* 10911 * Check whether the given lock request can proceed, given the 10912 * current file mappings. 10913 */ 10914 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10915 return (EINTR); 10916 if (cmd == F_SETLK || cmd == F_SETLKW) { 10917 if (!nfs4_safelock(vp, bfp, cr)) { 10918 rc = EAGAIN; 10919 goto done; 10920 } 10921 } 10922 10923 /* 10924 * Flush the cache after waiting for async I/O to finish. For new 10925 * locks, this is so that the process gets the latest bits from the 10926 * server. For unlocks, this is so that other clients see the 10927 * latest bits once the file has been unlocked. If currently dirty 10928 * pages can't be flushed, then don't allow a lock to be set. But 10929 * allow unlocks to succeed, to avoid having orphan locks on the 10930 * server. 10931 */ 10932 if (cmd != F_GETLK) { 10933 mutex_enter(&rp->r_statelock); 10934 while (rp->r_count > 0) { 10935 if (intr) { 10936 klwp_t *lwp = ttolwp(curthread); 10937 10938 if (lwp != NULL) 10939 lwp->lwp_nostop++; 10940 if (cv_wait_sig(&rp->r_cv, 10941 &rp->r_statelock) == 0) { 10942 if (lwp != NULL) 10943 lwp->lwp_nostop--; 10944 rc = EINTR; 10945 break; 10946 } 10947 if (lwp != NULL) 10948 lwp->lwp_nostop--; 10949 } else 10950 cv_wait(&rp->r_cv, &rp->r_statelock); 10951 } 10952 mutex_exit(&rp->r_statelock); 10953 if (rc != 0) 10954 goto done; 10955 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct); 10956 if (error) { 10957 if (error == ENOSPC || error == EDQUOT) { 10958 mutex_enter(&rp->r_statelock); 10959 if (!rp->r_error) 10960 rp->r_error = error; 10961 mutex_exit(&rp->r_statelock); 10962 } 10963 if (bfp->l_type != F_UNLCK) { 10964 rc = ENOLCK; 10965 goto done; 10966 } 10967 } 10968 } 10969 10970 /* 10971 * Call the lock manager to do the real work of contacting 10972 * the server and obtaining the lock. 10973 */ 10974 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10975 cr, &e, NULL, NULL); 10976 rc = e.error; 10977 10978 if (rc == 0) 10979 nfs4_lockcompletion(vp, cmd); 10980 10981 done: 10982 nfs_rw_exit(&rp->r_lkserlock); 10983 10984 return (rc); 10985 } 10986 10987 /* 10988 * Free storage space associated with the specified vnode. The portion 10989 * to be freed is specified by bfp->l_start and bfp->l_len (already 10990 * normalized to a "whence" of 0). 10991 * 10992 * This is an experimental facility whose continued existence is not 10993 * guaranteed. Currently, we only support the special case 10994 * of l_len == 0, meaning free to end of file. 10995 */ 10996 /* ARGSUSED */ 10997 static int 10998 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10999 offset_t offset, cred_t *cr, caller_context_t *ct) 11000 { 11001 int error; 11002 11003 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11004 return (EIO); 11005 ASSERT(vp->v_type == VREG); 11006 if (cmd != F_FREESP) 11007 return (EINVAL); 11008 11009 error = convoff(vp, bfp, 0, offset); 11010 if (!error) { 11011 ASSERT(bfp->l_start >= 0); 11012 if (bfp->l_len == 0) { 11013 struct vattr va; 11014 11015 va.va_mask = AT_SIZE; 11016 va.va_size = bfp->l_start; 11017 error = nfs4setattr(vp, &va, 0, cr, NULL); 11018 11019 if (error == 0 && bfp->l_start == 0) 11020 vnevent_truncate(vp, ct); 11021 } else 11022 error = EINVAL; 11023 } 11024 11025 return (error); 11026 } 11027 11028 /* ARGSUSED */ 11029 int 11030 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 11031 { 11032 rnode4_t *rp; 11033 rp = VTOR4(vp); 11034 11035 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 11036 vp = RTOV4(rp); 11037 } 11038 *vpp = vp; 11039 return (0); 11040 } 11041 11042 /* 11043 * Setup and add an address space callback to do the work of the delmap call. 11044 * The callback will (and must be) deleted in the actual callback function. 11045 * 11046 * This is done in order to take care of the problem that we have with holding 11047 * the address space's a_lock for a long period of time (e.g. if the NFS server 11048 * is down). Callbacks will be executed in the address space code while the 11049 * a_lock is not held. Holding the address space's a_lock causes things such 11050 * as ps and fork to hang because they are trying to acquire this lock as well. 11051 */ 11052 /* ARGSUSED */ 11053 static int 11054 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 11055 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 11056 caller_context_t *ct) 11057 { 11058 int caller_found; 11059 int error; 11060 rnode4_t *rp; 11061 nfs4_delmap_args_t *dmapp; 11062 nfs4_delmapcall_t *delmap_call; 11063 11064 if (vp->v_flag & VNOMAP) 11065 return (ENOSYS); 11066 11067 /* 11068 * A process may not change zones if it has NFS pages mmap'ed 11069 * in, so we can't legitimately get here from the wrong zone. 11070 */ 11071 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11072 11073 rp = VTOR4(vp); 11074 11075 /* 11076 * The way that the address space of this process deletes its mapping 11077 * of this file is via the following call chains: 11078 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11079 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11080 * 11081 * With the use of address space callbacks we are allowed to drop the 11082 * address space lock, a_lock, while executing the NFS operations that 11083 * need to go over the wire. Returning EAGAIN to the caller of this 11084 * function is what drives the execution of the callback that we add 11085 * below. The callback will be executed by the address space code 11086 * after dropping the a_lock. When the callback is finished, since 11087 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 11088 * is called again on the same segment to finish the rest of the work 11089 * that needs to happen during unmapping. 11090 * 11091 * This action of calling back into the segment driver causes 11092 * nfs4_delmap() to get called again, but since the callback was 11093 * already executed at this point, it already did the work and there 11094 * is nothing left for us to do. 11095 * 11096 * To Summarize: 11097 * - The first time nfs4_delmap is called by the current thread is when 11098 * we add the caller associated with this delmap to the delmap caller 11099 * list, add the callback, and return EAGAIN. 11100 * - The second time in this call chain when nfs4_delmap is called we 11101 * will find this caller in the delmap caller list and realize there 11102 * is no more work to do thus removing this caller from the list and 11103 * returning the error that was set in the callback execution. 11104 */ 11105 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 11106 if (caller_found) { 11107 /* 11108 * 'error' is from the actual delmap operations. To avoid 11109 * hangs, we need to handle the return of EAGAIN differently 11110 * since this is what drives the callback execution. 11111 * In this case, we don't want to return EAGAIN and do the 11112 * callback execution because there are none to execute. 11113 */ 11114 if (error == EAGAIN) 11115 return (0); 11116 else 11117 return (error); 11118 } 11119 11120 /* current caller was not in the list */ 11121 delmap_call = nfs4_init_delmapcall(); 11122 11123 mutex_enter(&rp->r_statelock); 11124 list_insert_tail(&rp->r_indelmap, delmap_call); 11125 mutex_exit(&rp->r_statelock); 11126 11127 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 11128 11129 dmapp->vp = vp; 11130 dmapp->off = off; 11131 dmapp->addr = addr; 11132 dmapp->len = len; 11133 dmapp->prot = prot; 11134 dmapp->maxprot = maxprot; 11135 dmapp->flags = flags; 11136 dmapp->cr = cr; 11137 dmapp->caller = delmap_call; 11138 11139 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 11140 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 11141 11142 return (error ? error : EAGAIN); 11143 } 11144 11145 static nfs4_delmapcall_t * 11146 nfs4_init_delmapcall() 11147 { 11148 nfs4_delmapcall_t *delmap_call; 11149 11150 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 11151 delmap_call->call_id = curthread; 11152 delmap_call->error = 0; 11153 11154 return (delmap_call); 11155 } 11156 11157 static void 11158 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 11159 { 11160 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 11161 } 11162 11163 /* 11164 * Searches for the current delmap caller (based on curthread) in the list of 11165 * callers. If it is found, we remove it and free the delmap caller. 11166 * Returns: 11167 * 0 if the caller wasn't found 11168 * 1 if the caller was found, removed and freed. *errp will be set 11169 * to what the result of the delmap was. 11170 */ 11171 static int 11172 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 11173 { 11174 nfs4_delmapcall_t *delmap_call; 11175 11176 /* 11177 * If the list doesn't exist yet, we create it and return 11178 * that the caller wasn't found. No list = no callers. 11179 */ 11180 mutex_enter(&rp->r_statelock); 11181 if (!(rp->r_flags & R4DELMAPLIST)) { 11182 /* The list does not exist */ 11183 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 11184 offsetof(nfs4_delmapcall_t, call_node)); 11185 rp->r_flags |= R4DELMAPLIST; 11186 mutex_exit(&rp->r_statelock); 11187 return (0); 11188 } else { 11189 /* The list exists so search it */ 11190 for (delmap_call = list_head(&rp->r_indelmap); 11191 delmap_call != NULL; 11192 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 11193 if (delmap_call->call_id == curthread) { 11194 /* current caller is in the list */ 11195 *errp = delmap_call->error; 11196 list_remove(&rp->r_indelmap, delmap_call); 11197 mutex_exit(&rp->r_statelock); 11198 nfs4_free_delmapcall(delmap_call); 11199 return (1); 11200 } 11201 } 11202 } 11203 mutex_exit(&rp->r_statelock); 11204 return (0); 11205 } 11206 11207 /* 11208 * Remove some pages from an mmap'd vnode. Just update the 11209 * count of pages. If doing close-to-open, then flush and 11210 * commit all of the pages associated with this file. 11211 * Otherwise, start an asynchronous page flush to write out 11212 * any dirty pages. This will also associate a credential 11213 * with the rnode which can be used to write the pages. 11214 */ 11215 /* ARGSUSED */ 11216 static void 11217 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11218 { 11219 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11220 rnode4_t *rp; 11221 mntinfo4_t *mi; 11222 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11223 11224 rp = VTOR4(dmapp->vp); 11225 mi = VTOMI4(dmapp->vp); 11226 11227 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11228 ASSERT(rp->r_mapcnt >= 0); 11229 11230 /* 11231 * Initiate a page flush and potential commit if there are 11232 * pages, the file system was not mounted readonly, the segment 11233 * was mapped shared, and the pages themselves were writeable. 11234 */ 11235 if (nfs4_has_pages(dmapp->vp) && 11236 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11237 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11238 mutex_enter(&rp->r_statelock); 11239 rp->r_flags |= R4DIRTY; 11240 mutex_exit(&rp->r_statelock); 11241 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11242 dmapp->len, dmapp->cr); 11243 if (!e.error) { 11244 mutex_enter(&rp->r_statelock); 11245 e.error = rp->r_error; 11246 rp->r_error = 0; 11247 mutex_exit(&rp->r_statelock); 11248 } 11249 } else 11250 e.error = 0; 11251 11252 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11253 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11254 B_INVAL, dmapp->cr, NULL); 11255 11256 if (e.error) { 11257 e.stat = puterrno4(e.error); 11258 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11259 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11260 dmapp->caller->error = e.error; 11261 } 11262 11263 /* Check to see if we need to close the file */ 11264 11265 if (dmapp->vp->v_type == VREG) { 11266 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11267 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11268 11269 if (e.error != 0 || e.stat != NFS4_OK) { 11270 /* 11271 * Since it is possible that e.error == 0 and 11272 * e.stat != NFS4_OK (and vice versa), 11273 * we do the proper checking in order to get both 11274 * e.error and e.stat reporting the correct info. 11275 */ 11276 if (e.stat == NFS4_OK) 11277 e.stat = puterrno4(e.error); 11278 if (e.error == 0) 11279 e.error = geterrno4(e.stat); 11280 11281 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11282 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11283 dmapp->caller->error = e.error; 11284 } 11285 } 11286 11287 (void) as_delete_callback(as, arg); 11288 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11289 } 11290 11291 11292 static uint_t 11293 fattr4_maxfilesize_to_bits(uint64_t ll) 11294 { 11295 uint_t l = 1; 11296 11297 if (ll == 0) { 11298 return (0); 11299 } 11300 11301 if (ll & 0xffffffff00000000) { 11302 l += 32; ll >>= 32; 11303 } 11304 if (ll & 0xffff0000) { 11305 l += 16; ll >>= 16; 11306 } 11307 if (ll & 0xff00) { 11308 l += 8; ll >>= 8; 11309 } 11310 if (ll & 0xf0) { 11311 l += 4; ll >>= 4; 11312 } 11313 if (ll & 0xc) { 11314 l += 2; ll >>= 2; 11315 } 11316 if (ll & 0x2) { 11317 l += 1; 11318 } 11319 return (l); 11320 } 11321 11322 static int 11323 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr) 11324 { 11325 vnode_t *avp = NULL; 11326 int error; 11327 11328 if ((error = nfs4lookup_xattr(vp, "", &avp, 11329 LOOKUP_XATTR, cr)) == 0) 11330 error = do_xattr_exists_check(avp, valp, cr); 11331 if (avp) 11332 VN_RELE(avp); 11333 11334 return (error); 11335 } 11336 11337 /* ARGSUSED */ 11338 int 11339 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 11340 caller_context_t *ct) 11341 { 11342 int error; 11343 hrtime_t t; 11344 rnode4_t *rp; 11345 nfs4_ga_res_t gar; 11346 nfs4_ga_ext_res_t ger; 11347 11348 gar.n4g_ext_res = &ger; 11349 11350 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11351 return (EIO); 11352 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11353 *valp = MAXPATHLEN; 11354 return (0); 11355 } 11356 if (cmd == _PC_ACL_ENABLED) { 11357 *valp = _ACL_ACE_ENABLED; 11358 return (0); 11359 } 11360 11361 rp = VTOR4(vp); 11362 if (cmd == _PC_XATTR_EXISTS) { 11363 /* 11364 * The existence of the xattr directory is not sufficient 11365 * for determining whether generic user attributes exists. 11366 * The attribute directory could only be a transient directory 11367 * used for Solaris sysattr support. Do a small readdir 11368 * to verify if the only entries are sysattrs or not. 11369 * 11370 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11371 * is NULL. Once the xadir vp exists, we can create xattrs, 11372 * and we don't have any way to update the "base" object's 11373 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11374 * could help out. 11375 */ 11376 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11377 rp->r_xattr_dir == NULL) { 11378 return (nfs4_have_xattrs(vp, valp, cr)); 11379 } 11380 } else { /* OLD CODE */ 11381 if (ATTRCACHE4_VALID(vp)) { 11382 mutex_enter(&rp->r_statelock); 11383 if (rp->r_pathconf.pc4_cache_valid) { 11384 error = 0; 11385 switch (cmd) { 11386 case _PC_FILESIZEBITS: 11387 *valp = 11388 rp->r_pathconf.pc4_filesizebits; 11389 break; 11390 case _PC_LINK_MAX: 11391 *valp = 11392 rp->r_pathconf.pc4_link_max; 11393 break; 11394 case _PC_NAME_MAX: 11395 *valp = 11396 rp->r_pathconf.pc4_name_max; 11397 break; 11398 case _PC_CHOWN_RESTRICTED: 11399 *valp = 11400 rp->r_pathconf.pc4_chown_restricted; 11401 break; 11402 case _PC_NO_TRUNC: 11403 *valp = 11404 rp->r_pathconf.pc4_no_trunc; 11405 break; 11406 default: 11407 error = EINVAL; 11408 break; 11409 } 11410 mutex_exit(&rp->r_statelock); 11411 #ifdef DEBUG 11412 nfs4_pathconf_cache_hits++; 11413 #endif 11414 return (error); 11415 } 11416 mutex_exit(&rp->r_statelock); 11417 } 11418 } 11419 #ifdef DEBUG 11420 nfs4_pathconf_cache_misses++; 11421 #endif 11422 11423 t = gethrtime(); 11424 11425 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11426 11427 if (error) { 11428 mutex_enter(&rp->r_statelock); 11429 rp->r_pathconf.pc4_cache_valid = FALSE; 11430 rp->r_pathconf.pc4_xattr_valid = FALSE; 11431 mutex_exit(&rp->r_statelock); 11432 return (error); 11433 } 11434 11435 /* interpret the max filesize */ 11436 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11437 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11438 11439 /* Store the attributes we just received */ 11440 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11441 11442 switch (cmd) { 11443 case _PC_FILESIZEBITS: 11444 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11445 break; 11446 case _PC_LINK_MAX: 11447 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11448 break; 11449 case _PC_NAME_MAX: 11450 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11451 break; 11452 case _PC_CHOWN_RESTRICTED: 11453 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11454 break; 11455 case _PC_NO_TRUNC: 11456 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11457 break; 11458 case _PC_XATTR_EXISTS: 11459 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) { 11460 if (error = nfs4_have_xattrs(vp, valp, cr)) 11461 return (error); 11462 } 11463 break; 11464 default: 11465 return (EINVAL); 11466 } 11467 11468 return (0); 11469 } 11470 11471 /* 11472 * Called by async thread to do synchronous pageio. Do the i/o, wait 11473 * for it to complete, and cleanup the page list when done. 11474 */ 11475 static int 11476 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11477 int flags, cred_t *cr) 11478 { 11479 int error; 11480 11481 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11482 11483 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11484 if (flags & B_READ) 11485 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11486 else 11487 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11488 return (error); 11489 } 11490 11491 /* ARGSUSED */ 11492 static int 11493 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11494 int flags, cred_t *cr, caller_context_t *ct) 11495 { 11496 int error; 11497 rnode4_t *rp; 11498 11499 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11500 return (EIO); 11501 11502 if (pp == NULL) 11503 return (EINVAL); 11504 11505 rp = VTOR4(vp); 11506 mutex_enter(&rp->r_statelock); 11507 rp->r_count++; 11508 mutex_exit(&rp->r_statelock); 11509 11510 if (flags & B_ASYNC) { 11511 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11512 nfs4_sync_pageio); 11513 } else 11514 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11515 mutex_enter(&rp->r_statelock); 11516 rp->r_count--; 11517 cv_broadcast(&rp->r_cv); 11518 mutex_exit(&rp->r_statelock); 11519 return (error); 11520 } 11521 11522 /* ARGSUSED */ 11523 static void 11524 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr, 11525 caller_context_t *ct) 11526 { 11527 int error; 11528 rnode4_t *rp; 11529 page_t *plist; 11530 page_t *pptr; 11531 offset3 offset; 11532 count3 len; 11533 k_sigset_t smask; 11534 11535 /* 11536 * We should get called with fl equal to either B_FREE or 11537 * B_INVAL. Any other value is illegal. 11538 * 11539 * The page that we are either supposed to free or destroy 11540 * should be exclusive locked and its io lock should not 11541 * be held. 11542 */ 11543 ASSERT(fl == B_FREE || fl == B_INVAL); 11544 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11545 11546 rp = VTOR4(vp); 11547 11548 /* 11549 * If the page doesn't need to be committed or we shouldn't 11550 * even bother attempting to commit it, then just make sure 11551 * that the p_fsdata byte is clear and then either free or 11552 * destroy the page as appropriate. 11553 */ 11554 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11555 pp->p_fsdata = C_NOCOMMIT; 11556 if (fl == B_FREE) 11557 page_free(pp, dn); 11558 else 11559 page_destroy(pp, dn); 11560 return; 11561 } 11562 11563 /* 11564 * If there is a page invalidation operation going on, then 11565 * if this is one of the pages being destroyed, then just 11566 * clear the p_fsdata byte and then either free or destroy 11567 * the page as appropriate. 11568 */ 11569 mutex_enter(&rp->r_statelock); 11570 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11571 mutex_exit(&rp->r_statelock); 11572 pp->p_fsdata = C_NOCOMMIT; 11573 if (fl == B_FREE) 11574 page_free(pp, dn); 11575 else 11576 page_destroy(pp, dn); 11577 return; 11578 } 11579 11580 /* 11581 * If we are freeing this page and someone else is already 11582 * waiting to do a commit, then just unlock the page and 11583 * return. That other thread will take care of commiting 11584 * this page. The page can be freed sometime after the 11585 * commit has finished. Otherwise, if the page is marked 11586 * as delay commit, then we may be getting called from 11587 * pvn_write_done, one page at a time. This could result 11588 * in one commit per page, so we end up doing lots of small 11589 * commits instead of fewer larger commits. This is bad, 11590 * we want do as few commits as possible. 11591 */ 11592 if (fl == B_FREE) { 11593 if (rp->r_flags & R4COMMITWAIT) { 11594 page_unlock(pp); 11595 mutex_exit(&rp->r_statelock); 11596 return; 11597 } 11598 if (pp->p_fsdata == C_DELAYCOMMIT) { 11599 pp->p_fsdata = C_COMMIT; 11600 page_unlock(pp); 11601 mutex_exit(&rp->r_statelock); 11602 return; 11603 } 11604 } 11605 11606 /* 11607 * Check to see if there is a signal which would prevent an 11608 * attempt to commit the pages from being successful. If so, 11609 * then don't bother with all of the work to gather pages and 11610 * generate the unsuccessful RPC. Just return from here and 11611 * let the page be committed at some later time. 11612 */ 11613 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11614 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11615 sigunintr(&smask); 11616 page_unlock(pp); 11617 mutex_exit(&rp->r_statelock); 11618 return; 11619 } 11620 sigunintr(&smask); 11621 11622 /* 11623 * We are starting to need to commit pages, so let's try 11624 * to commit as many as possible at once to reduce the 11625 * overhead. 11626 * 11627 * Set the `commit inprogress' state bit. We must 11628 * first wait until any current one finishes. Then 11629 * we initialize the c_pages list with this page. 11630 */ 11631 while (rp->r_flags & R4COMMIT) { 11632 rp->r_flags |= R4COMMITWAIT; 11633 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11634 rp->r_flags &= ~R4COMMITWAIT; 11635 } 11636 rp->r_flags |= R4COMMIT; 11637 mutex_exit(&rp->r_statelock); 11638 ASSERT(rp->r_commit.c_pages == NULL); 11639 rp->r_commit.c_pages = pp; 11640 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11641 rp->r_commit.c_commlen = PAGESIZE; 11642 11643 /* 11644 * Gather together all other pages which can be committed. 11645 * They will all be chained off r_commit.c_pages. 11646 */ 11647 nfs4_get_commit(vp); 11648 11649 /* 11650 * Clear the `commit inprogress' status and disconnect 11651 * the list of pages to be committed from the rnode. 11652 * At this same time, we also save the starting offset 11653 * and length of data to be committed on the server. 11654 */ 11655 plist = rp->r_commit.c_pages; 11656 rp->r_commit.c_pages = NULL; 11657 offset = rp->r_commit.c_commbase; 11658 len = rp->r_commit.c_commlen; 11659 mutex_enter(&rp->r_statelock); 11660 rp->r_flags &= ~R4COMMIT; 11661 cv_broadcast(&rp->r_commit.c_cv); 11662 mutex_exit(&rp->r_statelock); 11663 11664 if (curproc == proc_pageout || curproc == proc_fsflush || 11665 nfs_zone() != VTOMI4(vp)->mi_zone) { 11666 nfs4_async_commit(vp, plist, offset, len, 11667 cr, do_nfs4_async_commit); 11668 return; 11669 } 11670 11671 /* 11672 * Actually generate the COMMIT op over the wire operation. 11673 */ 11674 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11675 11676 /* 11677 * If we got an error during the commit, just unlock all 11678 * of the pages. The pages will get retransmitted to the 11679 * server during a putpage operation. 11680 */ 11681 if (error) { 11682 while (plist != NULL) { 11683 pptr = plist; 11684 page_sub(&plist, pptr); 11685 page_unlock(pptr); 11686 } 11687 return; 11688 } 11689 11690 /* 11691 * We've tried as hard as we can to commit the data to stable 11692 * storage on the server. We just unlock the rest of the pages 11693 * and clear the commit required state. They will be put 11694 * onto the tail of the cachelist if they are nolonger 11695 * mapped. 11696 */ 11697 while (plist != pp) { 11698 pptr = plist; 11699 page_sub(&plist, pptr); 11700 pptr->p_fsdata = C_NOCOMMIT; 11701 page_unlock(pptr); 11702 } 11703 11704 /* 11705 * It is possible that nfs4_commit didn't return error but 11706 * some other thread has modified the page we are going 11707 * to free/destroy. 11708 * In this case we need to rewrite the page. Do an explicit check 11709 * before attempting to free/destroy the page. If modified, needs to 11710 * be rewritten so unlock the page and return. 11711 */ 11712 if (hat_ismod(pp)) { 11713 pp->p_fsdata = C_NOCOMMIT; 11714 page_unlock(pp); 11715 return; 11716 } 11717 11718 /* 11719 * Now, as appropriate, either free or destroy the page 11720 * that we were called with. 11721 */ 11722 pp->p_fsdata = C_NOCOMMIT; 11723 if (fl == B_FREE) 11724 page_free(pp, dn); 11725 else 11726 page_destroy(pp, dn); 11727 } 11728 11729 /* 11730 * Commit requires that the current fh be the file written to. 11731 * The compound op structure is: 11732 * PUTFH(file), COMMIT 11733 */ 11734 static int 11735 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11736 { 11737 COMPOUND4args_clnt args; 11738 COMPOUND4res_clnt res; 11739 COMMIT4res *cm_res; 11740 nfs_argop4 argop[2]; 11741 nfs_resop4 *resop; 11742 int doqueue; 11743 mntinfo4_t *mi; 11744 rnode4_t *rp; 11745 cred_t *cred_otw = NULL; 11746 bool_t needrecov = FALSE; 11747 nfs4_recov_state_t recov_state; 11748 nfs4_open_stream_t *osp = NULL; 11749 bool_t first_time = TRUE; /* first time getting OTW cred */ 11750 bool_t last_time = FALSE; /* last time getting OTW cred */ 11751 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11752 11753 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11754 11755 rp = VTOR4(vp); 11756 11757 mi = VTOMI4(vp); 11758 recov_state.rs_flags = 0; 11759 recov_state.rs_num_retry_despite_err = 0; 11760 get_commit_cred: 11761 /* 11762 * Releases the osp, if a valid open stream is provided. 11763 * Puts a hold on the cred_otw and the new osp (if found). 11764 */ 11765 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11766 &first_time, &last_time); 11767 args.ctag = TAG_COMMIT; 11768 recov_retry: 11769 /* 11770 * Commit ops: putfh file; commit 11771 */ 11772 args.array_len = 2; 11773 args.array = argop; 11774 11775 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11776 &recov_state, NULL); 11777 if (e.error) { 11778 crfree(cred_otw); 11779 if (osp != NULL) 11780 open_stream_rele(osp, rp); 11781 return (e.error); 11782 } 11783 11784 /* putfh directory */ 11785 argop[0].argop = OP_CPUTFH; 11786 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11787 11788 /* commit */ 11789 argop[1].argop = OP_COMMIT; 11790 argop[1].nfs_argop4_u.opcommit.offset = offset; 11791 argop[1].nfs_argop4_u.opcommit.count = count; 11792 11793 doqueue = 1; 11794 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11795 11796 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11797 if (!needrecov && e.error) { 11798 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11799 needrecov); 11800 crfree(cred_otw); 11801 if (e.error == EACCES && last_time == FALSE) 11802 goto get_commit_cred; 11803 if (osp != NULL) 11804 open_stream_rele(osp, rp); 11805 return (e.error); 11806 } 11807 11808 if (needrecov) { 11809 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11810 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) { 11811 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11812 &recov_state, needrecov); 11813 if (!e.error) 11814 (void) xdr_free(xdr_COMPOUND4res_clnt, 11815 (caddr_t)&res); 11816 goto recov_retry; 11817 } 11818 if (e.error) { 11819 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11820 &recov_state, needrecov); 11821 crfree(cred_otw); 11822 if (osp != NULL) 11823 open_stream_rele(osp, rp); 11824 return (e.error); 11825 } 11826 /* fall through for res.status case */ 11827 } 11828 11829 if (res.status) { 11830 e.error = geterrno4(res.status); 11831 if (e.error == EACCES && last_time == FALSE) { 11832 crfree(cred_otw); 11833 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11834 &recov_state, needrecov); 11835 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11836 goto get_commit_cred; 11837 } 11838 /* 11839 * Can't do a nfs4_purge_stale_fh here because this 11840 * can cause a deadlock. nfs4_commit can 11841 * be called from nfs4_dispose which can be called 11842 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11843 * can call back to pvn_vplist_dirty. 11844 */ 11845 if (e.error == ESTALE) { 11846 mutex_enter(&rp->r_statelock); 11847 rp->r_flags |= R4STALE; 11848 if (!rp->r_error) 11849 rp->r_error = e.error; 11850 mutex_exit(&rp->r_statelock); 11851 PURGE_ATTRCACHE4(vp); 11852 } else { 11853 mutex_enter(&rp->r_statelock); 11854 if (!rp->r_error) 11855 rp->r_error = e.error; 11856 mutex_exit(&rp->r_statelock); 11857 } 11858 } else { 11859 ASSERT(rp->r_flags & R4HAVEVERF); 11860 resop = &res.array[1]; /* commit res */ 11861 cm_res = &resop->nfs_resop4_u.opcommit; 11862 mutex_enter(&rp->r_statelock); 11863 if (cm_res->writeverf == rp->r_writeverf) { 11864 mutex_exit(&rp->r_statelock); 11865 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11866 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11867 &recov_state, needrecov); 11868 crfree(cred_otw); 11869 if (osp != NULL) 11870 open_stream_rele(osp, rp); 11871 return (0); 11872 } 11873 nfs4_set_mod(vp); 11874 rp->r_writeverf = cm_res->writeverf; 11875 mutex_exit(&rp->r_statelock); 11876 e.error = NFS_VERF_MISMATCH; 11877 } 11878 11879 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11880 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11881 crfree(cred_otw); 11882 if (osp != NULL) 11883 open_stream_rele(osp, rp); 11884 11885 return (e.error); 11886 } 11887 11888 static void 11889 nfs4_set_mod(vnode_t *vp) 11890 { 11891 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11892 11893 /* make sure we're looking at the master vnode, not a shadow */ 11894 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check); 11895 } 11896 11897 /* 11898 * This function is used to gather a page list of the pages which 11899 * can be committed on the server. 11900 * 11901 * The calling thread must have set R4COMMIT. This bit is used to 11902 * serialize access to the commit structure in the rnode. As long 11903 * as the thread has set R4COMMIT, then it can manipulate the commit 11904 * structure without requiring any other locks. 11905 * 11906 * When this function is called from nfs4_dispose() the page passed 11907 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11908 * will skip it. This is not a problem since we initially add the 11909 * page to the r_commit page list. 11910 * 11911 */ 11912 static void 11913 nfs4_get_commit(vnode_t *vp) 11914 { 11915 rnode4_t *rp; 11916 page_t *pp; 11917 kmutex_t *vphm; 11918 11919 rp = VTOR4(vp); 11920 11921 ASSERT(rp->r_flags & R4COMMIT); 11922 11923 /* make sure we're looking at the master vnode, not a shadow */ 11924 11925 if (IS_SHADOW(vp, rp)) 11926 vp = RTOV4(rp); 11927 11928 vphm = page_vnode_mutex(vp); 11929 mutex_enter(vphm); 11930 11931 /* 11932 * If there are no pages associated with this vnode, then 11933 * just return. 11934 */ 11935 if ((pp = vp->v_pages) == NULL) { 11936 mutex_exit(vphm); 11937 return; 11938 } 11939 11940 /* 11941 * Step through all of the pages associated with this vnode 11942 * looking for pages which need to be committed. 11943 */ 11944 do { 11945 /* Skip marker pages. */ 11946 if (pp->p_hash == PVN_VPLIST_HASH_TAG) 11947 continue; 11948 11949 /* 11950 * First short-cut everything (without the page_lock) 11951 * and see if this page does not need to be committed 11952 * or is modified if so then we'll just skip it. 11953 */ 11954 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11955 continue; 11956 11957 /* 11958 * Attempt to lock the page. If we can't, then 11959 * someone else is messing with it or we have been 11960 * called from nfs4_dispose and this is the page that 11961 * nfs4_dispose was called with.. anyway just skip it. 11962 */ 11963 if (!page_trylock(pp, SE_EXCL)) 11964 continue; 11965 11966 /* 11967 * Lets check again now that we have the page lock. 11968 */ 11969 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11970 page_unlock(pp); 11971 continue; 11972 } 11973 11974 /* this had better not be a free page */ 11975 ASSERT(PP_ISFREE(pp) == 0); 11976 11977 /* 11978 * The page needs to be committed and we locked it. 11979 * Update the base and length parameters and add it 11980 * to r_pages. 11981 */ 11982 if (rp->r_commit.c_pages == NULL) { 11983 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11984 rp->r_commit.c_commlen = PAGESIZE; 11985 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11986 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11987 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11988 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11989 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11990 <= pp->p_offset) { 11991 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11992 rp->r_commit.c_commbase + PAGESIZE; 11993 } 11994 page_add(&rp->r_commit.c_pages, pp); 11995 } while ((pp = pp->p_vpnext) != vp->v_pages); 11996 11997 mutex_exit(vphm); 11998 } 11999 12000 /* 12001 * This routine is used to gather together a page list of the pages 12002 * which are to be committed on the server. This routine must not 12003 * be called if the calling thread holds any locked pages. 12004 * 12005 * The calling thread must have set R4COMMIT. This bit is used to 12006 * serialize access to the commit structure in the rnode. As long 12007 * as the thread has set R4COMMIT, then it can manipulate the commit 12008 * structure without requiring any other locks. 12009 */ 12010 static void 12011 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 12012 { 12013 12014 rnode4_t *rp; 12015 page_t *pp; 12016 u_offset_t end; 12017 u_offset_t off; 12018 ASSERT(len != 0); 12019 rp = VTOR4(vp); 12020 ASSERT(rp->r_flags & R4COMMIT); 12021 12022 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12023 12024 /* make sure we're looking at the master vnode, not a shadow */ 12025 12026 if (IS_SHADOW(vp, rp)) 12027 vp = RTOV4(rp); 12028 12029 /* 12030 * If there are no pages associated with this vnode, then 12031 * just return. 12032 */ 12033 if ((pp = vp->v_pages) == NULL) 12034 return; 12035 /* 12036 * Calculate the ending offset. 12037 */ 12038 end = soff + len; 12039 for (off = soff; off < end; off += PAGESIZE) { 12040 /* 12041 * Lookup each page by vp, offset. 12042 */ 12043 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 12044 continue; 12045 /* 12046 * If this page does not need to be committed or is 12047 * modified, then just skip it. 12048 */ 12049 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 12050 page_unlock(pp); 12051 continue; 12052 } 12053 12054 ASSERT(PP_ISFREE(pp) == 0); 12055 /* 12056 * The page needs to be committed and we locked it. 12057 * Update the base and length parameters and add it 12058 * to r_pages. 12059 */ 12060 if (rp->r_commit.c_pages == NULL) { 12061 rp->r_commit.c_commbase = (offset3)pp->p_offset; 12062 rp->r_commit.c_commlen = PAGESIZE; 12063 } else { 12064 rp->r_commit.c_commlen = (offset3)pp->p_offset - 12065 rp->r_commit.c_commbase + PAGESIZE; 12066 } 12067 page_add(&rp->r_commit.c_pages, pp); 12068 } 12069 } 12070 12071 /* 12072 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 12073 * Flushes and commits data to the server. 12074 */ 12075 static int 12076 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 12077 { 12078 int error; 12079 verifier4 write_verf; 12080 rnode4_t *rp = VTOR4(vp); 12081 12082 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12083 12084 /* 12085 * Flush the data portion of the file and then commit any 12086 * portions which need to be committed. This may need to 12087 * be done twice if the server has changed state since 12088 * data was last written. The data will need to be 12089 * rewritten to the server and then a new commit done. 12090 * 12091 * In fact, this may need to be done several times if the 12092 * server is having problems and crashing while we are 12093 * attempting to do this. 12094 */ 12095 12096 top: 12097 /* 12098 * Do a flush based on the poff and plen arguments. This 12099 * will synchronously write out any modified pages in the 12100 * range specified by (poff, plen). This starts all of the 12101 * i/o operations which will be waited for in the next 12102 * call to nfs4_putpage 12103 */ 12104 12105 mutex_enter(&rp->r_statelock); 12106 write_verf = rp->r_writeverf; 12107 mutex_exit(&rp->r_statelock); 12108 12109 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL); 12110 if (error == EAGAIN) 12111 error = 0; 12112 12113 /* 12114 * Do a flush based on the poff and plen arguments. This 12115 * will synchronously write out any modified pages in the 12116 * range specified by (poff, plen) and wait until all of 12117 * the asynchronous i/o's in that range are done as well. 12118 */ 12119 if (!error) 12120 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL); 12121 12122 if (error) 12123 return (error); 12124 12125 mutex_enter(&rp->r_statelock); 12126 if (rp->r_writeverf != write_verf) { 12127 mutex_exit(&rp->r_statelock); 12128 goto top; 12129 } 12130 mutex_exit(&rp->r_statelock); 12131 12132 /* 12133 * Now commit any pages which might need to be committed. 12134 * If the error, NFS_VERF_MISMATCH, is returned, then 12135 * start over with the flush operation. 12136 */ 12137 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 12138 12139 if (error == NFS_VERF_MISMATCH) 12140 goto top; 12141 12142 return (error); 12143 } 12144 12145 /* 12146 * nfs4_commit_vp() will wait for other pending commits and 12147 * will either commit the whole file or a range, plen dictates 12148 * if we commit whole file. a value of zero indicates the whole 12149 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 12150 */ 12151 static int 12152 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 12153 cred_t *cr, int wait_on_writes) 12154 { 12155 rnode4_t *rp; 12156 page_t *plist; 12157 offset3 offset; 12158 count3 len; 12159 12160 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12161 12162 rp = VTOR4(vp); 12163 12164 /* 12165 * before we gather commitable pages make 12166 * sure there are no outstanding async writes 12167 */ 12168 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 12169 mutex_enter(&rp->r_statelock); 12170 while (rp->r_count > 0) { 12171 cv_wait(&rp->r_cv, &rp->r_statelock); 12172 } 12173 mutex_exit(&rp->r_statelock); 12174 } 12175 12176 /* 12177 * Set the `commit inprogress' state bit. We must 12178 * first wait until any current one finishes. 12179 */ 12180 mutex_enter(&rp->r_statelock); 12181 while (rp->r_flags & R4COMMIT) { 12182 rp->r_flags |= R4COMMITWAIT; 12183 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 12184 rp->r_flags &= ~R4COMMITWAIT; 12185 } 12186 rp->r_flags |= R4COMMIT; 12187 mutex_exit(&rp->r_statelock); 12188 12189 /* 12190 * Gather all of the pages which need to be 12191 * committed. 12192 */ 12193 if (plen == 0) 12194 nfs4_get_commit(vp); 12195 else 12196 nfs4_get_commit_range(vp, poff, plen); 12197 12198 /* 12199 * Clear the `commit inprogress' bit and disconnect the 12200 * page list which was gathered by nfs4_get_commit. 12201 */ 12202 plist = rp->r_commit.c_pages; 12203 rp->r_commit.c_pages = NULL; 12204 offset = rp->r_commit.c_commbase; 12205 len = rp->r_commit.c_commlen; 12206 mutex_enter(&rp->r_statelock); 12207 rp->r_flags &= ~R4COMMIT; 12208 cv_broadcast(&rp->r_commit.c_cv); 12209 mutex_exit(&rp->r_statelock); 12210 12211 /* 12212 * If any pages need to be committed, commit them and 12213 * then unlock them so that they can be freed some 12214 * time later. 12215 */ 12216 if (plist == NULL) 12217 return (0); 12218 12219 /* 12220 * No error occurred during the flush portion 12221 * of this operation, so now attempt to commit 12222 * the data to stable storage on the server. 12223 * 12224 * This will unlock all of the pages on the list. 12225 */ 12226 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12227 } 12228 12229 static int 12230 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12231 cred_t *cr) 12232 { 12233 int error; 12234 page_t *pp; 12235 12236 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12237 12238 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12239 12240 /* 12241 * If we got an error, then just unlock all of the pages 12242 * on the list. 12243 */ 12244 if (error) { 12245 while (plist != NULL) { 12246 pp = plist; 12247 page_sub(&plist, pp); 12248 page_unlock(pp); 12249 } 12250 return (error); 12251 } 12252 /* 12253 * We've tried as hard as we can to commit the data to stable 12254 * storage on the server. We just unlock the pages and clear 12255 * the commit required state. They will get freed later. 12256 */ 12257 while (plist != NULL) { 12258 pp = plist; 12259 page_sub(&plist, pp); 12260 pp->p_fsdata = C_NOCOMMIT; 12261 page_unlock(pp); 12262 } 12263 12264 return (error); 12265 } 12266 12267 static void 12268 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12269 cred_t *cr) 12270 { 12271 12272 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12273 } 12274 12275 /*ARGSUSED*/ 12276 static int 12277 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12278 caller_context_t *ct) 12279 { 12280 int error = 0; 12281 mntinfo4_t *mi; 12282 vattr_t va; 12283 vsecattr_t nfsace4_vsap; 12284 12285 mi = VTOMI4(vp); 12286 if (nfs_zone() != mi->mi_zone) 12287 return (EIO); 12288 if (mi->mi_flags & MI4_ACL) { 12289 /* if we have a delegation, return it */ 12290 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12291 (void) nfs4delegreturn(VTOR4(vp), 12292 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12293 12294 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12295 NFS4_ACL_SET); 12296 if (error) /* EINVAL */ 12297 return (error); 12298 12299 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12300 /* 12301 * These are aclent_t type entries. 12302 */ 12303 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12304 vp->v_type == VDIR, FALSE); 12305 if (error) 12306 return (error); 12307 } else { 12308 /* 12309 * These are ace_t type entries. 12310 */ 12311 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12312 FALSE); 12313 if (error) 12314 return (error); 12315 } 12316 bzero(&va, sizeof (va)); 12317 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12318 vs_ace4_destroy(&nfsace4_vsap); 12319 return (error); 12320 } 12321 return (ENOSYS); 12322 } 12323 12324 /* ARGSUSED */ 12325 int 12326 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12327 caller_context_t *ct) 12328 { 12329 int error; 12330 mntinfo4_t *mi; 12331 nfs4_ga_res_t gar; 12332 rnode4_t *rp = VTOR4(vp); 12333 12334 mi = VTOMI4(vp); 12335 if (nfs_zone() != mi->mi_zone) 12336 return (EIO); 12337 12338 bzero(&gar, sizeof (gar)); 12339 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12340 12341 /* 12342 * vsecattr->vsa_mask holds the original acl request mask. 12343 * This is needed when determining what to return. 12344 * (See: nfs4_create_getsecattr_return()) 12345 */ 12346 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12347 if (error) /* EINVAL */ 12348 return (error); 12349 12350 /* 12351 * If this is a referral stub, don't try to go OTW for an ACL 12352 */ 12353 if (RP_ISSTUB_REFERRAL(VTOR4(vp))) 12354 return (fs_fab_acl(vp, vsecattr, flag, cr, ct)); 12355 12356 if (mi->mi_flags & MI4_ACL) { 12357 /* 12358 * Check if the data is cached and the cache is valid. If it 12359 * is we don't go over the wire. 12360 */ 12361 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12362 mutex_enter(&rp->r_statelock); 12363 if (rp->r_secattr != NULL) { 12364 error = nfs4_create_getsecattr_return( 12365 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12366 rp->r_attr.va_gid, 12367 vp->v_type == VDIR); 12368 if (!error) { /* error == 0 - Success! */ 12369 mutex_exit(&rp->r_statelock); 12370 return (error); 12371 } 12372 } 12373 mutex_exit(&rp->r_statelock); 12374 } 12375 12376 /* 12377 * The getattr otw call will always get both the acl, in 12378 * the form of a list of nfsace4's, and the number of acl 12379 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12380 */ 12381 gar.n4g_va.va_mask = AT_ALL; 12382 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12383 if (error) { 12384 vs_ace4_destroy(&gar.n4g_vsa); 12385 if (error == ENOTSUP || error == EOPNOTSUPP) 12386 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12387 return (error); 12388 } 12389 12390 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12391 /* 12392 * No error was returned, but according to the response 12393 * bitmap, neither was an acl. 12394 */ 12395 vs_ace4_destroy(&gar.n4g_vsa); 12396 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12397 return (error); 12398 } 12399 12400 /* 12401 * Update the cache with the ACL. 12402 */ 12403 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12404 12405 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12406 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12407 vp->v_type == VDIR); 12408 vs_ace4_destroy(&gar.n4g_vsa); 12409 if ((error) && (vsecattr->vsa_mask & 12410 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12411 (error != EACCES)) { 12412 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12413 } 12414 return (error); 12415 } 12416 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12417 return (error); 12418 } 12419 12420 /* 12421 * The function returns: 12422 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12423 * - EINVAL if the passed in "acl_mask" is an invalid request. 12424 * 12425 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12426 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12427 * 12428 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12429 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12430 * - We have a count field set without the corresponding acl field set. (e.g. - 12431 * VSA_ACECNT is set, but VSA_ACE is not) 12432 */ 12433 static int 12434 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12435 { 12436 /* Shortcut the masks that are always valid. */ 12437 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12438 return (0); 12439 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12440 return (0); 12441 12442 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12443 /* 12444 * We can't have any VSA_ACL type stuff in the mask now. 12445 */ 12446 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12447 VSA_DFACLCNT)) 12448 return (EINVAL); 12449 12450 if (op == NFS4_ACL_SET) { 12451 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12452 return (EINVAL); 12453 } 12454 } 12455 12456 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12457 /* 12458 * We can't have any VSA_ACE type stuff in the mask now. 12459 */ 12460 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12461 return (EINVAL); 12462 12463 if (op == NFS4_ACL_SET) { 12464 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12465 return (EINVAL); 12466 12467 if ((acl_mask & VSA_DFACLCNT) && 12468 !(acl_mask & VSA_DFACL)) 12469 return (EINVAL); 12470 } 12471 } 12472 return (0); 12473 } 12474 12475 /* 12476 * The theory behind creating the correct getsecattr return is simply this: 12477 * "Don't return anything that the caller is not expecting to have to free." 12478 */ 12479 static int 12480 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12481 uid_t uid, gid_t gid, int isdir) 12482 { 12483 int error = 0; 12484 /* Save the mask since the translators modify it. */ 12485 uint_t orig_mask = vsap->vsa_mask; 12486 12487 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12488 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE); 12489 12490 if (error) 12491 return (error); 12492 12493 /* 12494 * If the caller only asked for the ace count (VSA_ACECNT) 12495 * don't give them the full acl (VSA_ACE), free it. 12496 */ 12497 if (!orig_mask & VSA_ACE) { 12498 if (vsap->vsa_aclentp != NULL) { 12499 kmem_free(vsap->vsa_aclentp, 12500 vsap->vsa_aclcnt * sizeof (ace_t)); 12501 vsap->vsa_aclentp = NULL; 12502 } 12503 } 12504 vsap->vsa_mask = orig_mask; 12505 12506 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12507 VSA_DFACLCNT)) { 12508 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12509 isdir, FALSE); 12510 12511 if (error) 12512 return (error); 12513 12514 /* 12515 * If the caller only asked for the acl count (VSA_ACLCNT) 12516 * and/or the default acl count (VSA_DFACLCNT) don't give them 12517 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12518 */ 12519 if (!orig_mask & VSA_ACL) { 12520 if (vsap->vsa_aclentp != NULL) { 12521 kmem_free(vsap->vsa_aclentp, 12522 vsap->vsa_aclcnt * sizeof (aclent_t)); 12523 vsap->vsa_aclentp = NULL; 12524 } 12525 } 12526 12527 if (!orig_mask & VSA_DFACL) { 12528 if (vsap->vsa_dfaclentp != NULL) { 12529 kmem_free(vsap->vsa_dfaclentp, 12530 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12531 vsap->vsa_dfaclentp = NULL; 12532 } 12533 } 12534 vsap->vsa_mask = orig_mask; 12535 } 12536 return (0); 12537 } 12538 12539 /* ARGSUSED */ 12540 int 12541 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr, 12542 caller_context_t *ct) 12543 { 12544 int error; 12545 12546 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12547 return (EIO); 12548 /* 12549 * check for valid cmd parameter 12550 */ 12551 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12552 return (EINVAL); 12553 12554 /* 12555 * Check access permissions 12556 */ 12557 if ((cmd & F_SHARE) && 12558 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12559 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12560 return (EBADF); 12561 12562 /* 12563 * If the filesystem is mounted using local locking, pass the 12564 * request off to the local share code. 12565 */ 12566 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12567 return (fs_shrlock(vp, cmd, shr, flag, cr, ct)); 12568 12569 switch (cmd) { 12570 case F_SHARE: 12571 case F_UNSHARE: 12572 /* 12573 * This will be properly implemented later, 12574 * see RFE: 4823948 . 12575 */ 12576 error = EAGAIN; 12577 break; 12578 12579 case F_HASREMOTELOCKS: 12580 /* 12581 * NFS client can't store remote locks itself 12582 */ 12583 shr->s_access = 0; 12584 error = 0; 12585 break; 12586 12587 default: 12588 error = EINVAL; 12589 break; 12590 } 12591 12592 return (error); 12593 } 12594 12595 /* 12596 * Common code called by directory ops to update the attrcache 12597 */ 12598 static int 12599 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12600 hrtime_t t, vnode_t *vp, cred_t *cr) 12601 { 12602 int error = 0; 12603 12604 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12605 12606 if (status != NFS4_OK) { 12607 /* getattr not done or failed */ 12608 PURGE_ATTRCACHE4(vp); 12609 return (error); 12610 } 12611 12612 if (garp) { 12613 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12614 } else { 12615 PURGE_ATTRCACHE4(vp); 12616 } 12617 return (error); 12618 } 12619 12620 /* 12621 * Update directory caches for directory modification ops (link, rename, etc.) 12622 * When dinfo is NULL, manage dircaches in the old way. 12623 */ 12624 static void 12625 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12626 dirattr_info_t *dinfo) 12627 { 12628 rnode4_t *drp = VTOR4(dvp); 12629 12630 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12631 12632 /* Purge rddir cache for dir since it changed */ 12633 if (drp->r_dir != NULL) 12634 nfs4_purge_rddir_cache(dvp); 12635 12636 /* 12637 * If caller provided dinfo, then use it to manage dir caches. 12638 */ 12639 if (dinfo != NULL) { 12640 if (vp != NULL) { 12641 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12642 if (!VTOR4(vp)->created_v4) { 12643 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12644 dnlc_update(dvp, nm, vp); 12645 } else { 12646 /* 12647 * XXX don't update if the created_v4 flag is 12648 * set 12649 */ 12650 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12651 NFS4_DEBUG(nfs4_client_state_debug, 12652 (CE_NOTE, "nfs4_update_dircaches: " 12653 "don't update dnlc: created_v4 flag")); 12654 } 12655 } 12656 12657 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12658 dinfo->di_cred, FALSE, cinfo); 12659 12660 return; 12661 } 12662 12663 /* 12664 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12665 * Since caller modified dir but didn't receive post-dirmod-op dir 12666 * attrs, the dir's attrs must be purged. 12667 * 12668 * XXX this check and dnlc update/purge should really be atomic, 12669 * XXX but can't use rnode statelock because it'll deadlock in 12670 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12671 * XXX does occur. 12672 * 12673 * XXX We also may want to check that atomic is true in the 12674 * XXX change_info struct. If it is not, the change_info may 12675 * XXX reflect changes by more than one clients which means that 12676 * XXX our cache may not be valid. 12677 */ 12678 PURGE_ATTRCACHE4(dvp); 12679 if (drp->r_change == cinfo->before) { 12680 /* no changes took place in the directory prior to our link */ 12681 if (vp != NULL) { 12682 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12683 if (!VTOR4(vp)->created_v4) { 12684 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12685 dnlc_update(dvp, nm, vp); 12686 } else { 12687 /* 12688 * XXX dont' update if the created_v4 flag 12689 * is set 12690 */ 12691 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12692 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12693 "nfs4_update_dircaches: don't" 12694 " update dnlc: created_v4 flag")); 12695 } 12696 } 12697 } else { 12698 /* Another client modified directory - purge its dnlc cache */ 12699 dnlc_purge_vp(dvp); 12700 } 12701 } 12702 12703 /* 12704 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12705 * file. 12706 * 12707 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12708 * file (ie: client recovery) and otherwise set to FALSE. 12709 * 12710 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12711 * initiated) calling functions. 12712 * 12713 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12714 * of resending a 'lost' open request. 12715 * 12716 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12717 * server that hands out BAD_SEQID on open confirm. 12718 * 12719 * Errors are returned via the nfs4_error_t parameter. 12720 */ 12721 void 12722 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12723 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12724 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12725 { 12726 COMPOUND4args_clnt args; 12727 COMPOUND4res_clnt res; 12728 nfs_argop4 argop[2]; 12729 nfs_resop4 *resop; 12730 int doqueue = 1; 12731 mntinfo4_t *mi; 12732 OPEN_CONFIRM4args *open_confirm_args; 12733 int needrecov; 12734 12735 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12736 #if DEBUG 12737 mutex_enter(&oop->oo_lock); 12738 ASSERT(oop->oo_seqid_inuse); 12739 mutex_exit(&oop->oo_lock); 12740 #endif 12741 12742 recov_retry_confirm: 12743 nfs4_error_zinit(ep); 12744 *retry_open = FALSE; 12745 12746 if (resend) 12747 args.ctag = TAG_OPEN_CONFIRM_LOST; 12748 else 12749 args.ctag = TAG_OPEN_CONFIRM; 12750 12751 args.array_len = 2; 12752 args.array = argop; 12753 12754 /* putfh target fh */ 12755 argop[0].argop = OP_CPUTFH; 12756 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12757 12758 argop[1].argop = OP_OPEN_CONFIRM; 12759 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12760 12761 (*seqid) += 1; 12762 open_confirm_args->seqid = *seqid; 12763 open_confirm_args->open_stateid = *stateid; 12764 12765 mi = VTOMI4(vp); 12766 12767 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12768 12769 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12770 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12771 } 12772 12773 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12774 if (!needrecov && ep->error) 12775 return; 12776 12777 if (needrecov) { 12778 bool_t abort = FALSE; 12779 12780 if (reopening_file == FALSE) { 12781 nfs4_bseqid_entry_t *bsep = NULL; 12782 12783 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12784 bsep = nfs4_create_bseqid_entry(oop, NULL, 12785 vp, 0, args.ctag, 12786 open_confirm_args->seqid); 12787 12788 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 12789 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL); 12790 if (bsep) { 12791 kmem_free(bsep, sizeof (*bsep)); 12792 if (num_bseqid_retryp && 12793 --(*num_bseqid_retryp) == 0) 12794 abort = TRUE; 12795 } 12796 } 12797 if ((ep->error == ETIMEDOUT || 12798 res.status == NFS4ERR_RESOURCE) && 12799 abort == FALSE && resend == FALSE) { 12800 if (!ep->error) 12801 (void) xdr_free(xdr_COMPOUND4res_clnt, 12802 (caddr_t)&res); 12803 12804 delay(SEC_TO_TICK(confirm_retry_sec)); 12805 goto recov_retry_confirm; 12806 } 12807 /* State may have changed so retry the entire OPEN op */ 12808 if (abort == FALSE) 12809 *retry_open = TRUE; 12810 else 12811 *retry_open = FALSE; 12812 if (!ep->error) 12813 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12814 return; 12815 } 12816 12817 if (res.status) { 12818 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12819 return; 12820 } 12821 12822 resop = &res.array[1]; /* open confirm res */ 12823 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12824 stateid, sizeof (*stateid)); 12825 12826 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12827 } 12828 12829 /* 12830 * Return the credentials associated with a client state object. The 12831 * caller is responsible for freeing the credentials. 12832 */ 12833 12834 static cred_t * 12835 state_to_cred(nfs4_open_stream_t *osp) 12836 { 12837 cred_t *cr; 12838 12839 /* 12840 * It's ok to not lock the open stream and open owner to get 12841 * the oo_cred since this is only written once (upon creation) 12842 * and will not change. 12843 */ 12844 cr = osp->os_open_owner->oo_cred; 12845 crhold(cr); 12846 12847 return (cr); 12848 } 12849 12850 /* 12851 * nfs4_find_sysid 12852 * 12853 * Find the sysid for the knetconfig associated with the given mi. 12854 */ 12855 static struct lm_sysid * 12856 nfs4_find_sysid(mntinfo4_t *mi) 12857 { 12858 ASSERT(nfs_zone() == mi->mi_zone); 12859 12860 /* 12861 * Switch from RDMA knconf to original mount knconf 12862 */ 12863 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12864 mi->mi_curr_serv->sv_hostname, NULL)); 12865 } 12866 12867 #ifdef DEBUG 12868 /* 12869 * Return a string version of the call type for easy reading. 12870 */ 12871 static char * 12872 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12873 { 12874 switch (ctype) { 12875 case NFS4_LCK_CTYPE_NORM: 12876 return ("NORMAL"); 12877 case NFS4_LCK_CTYPE_RECLAIM: 12878 return ("RECLAIM"); 12879 case NFS4_LCK_CTYPE_RESEND: 12880 return ("RESEND"); 12881 case NFS4_LCK_CTYPE_REINSTATE: 12882 return ("REINSTATE"); 12883 default: 12884 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12885 "type %d", ctype); 12886 return (""); 12887 } 12888 } 12889 #endif 12890 12891 /* 12892 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12893 * Unlock requests don't have an over-the-wire locktype, so we just return 12894 * something non-threatening. 12895 */ 12896 12897 static nfs_lock_type4 12898 flk_to_locktype(int cmd, int l_type) 12899 { 12900 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12901 12902 switch (l_type) { 12903 case F_UNLCK: 12904 return (READ_LT); 12905 case F_RDLCK: 12906 if (cmd == F_SETLK) 12907 return (READ_LT); 12908 else 12909 return (READW_LT); 12910 case F_WRLCK: 12911 if (cmd == F_SETLK) 12912 return (WRITE_LT); 12913 else 12914 return (WRITEW_LT); 12915 } 12916 panic("flk_to_locktype"); 12917 /*NOTREACHED*/ 12918 } 12919 12920 /* 12921 * Do some preliminary checks for nfs4frlock. 12922 */ 12923 static int 12924 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12925 u_offset_t offset) 12926 { 12927 int error = 0; 12928 12929 /* 12930 * If we are setting a lock, check that the file is opened 12931 * with the correct mode. 12932 */ 12933 if (cmd == F_SETLK || cmd == F_SETLKW) { 12934 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12935 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12936 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12937 "nfs4frlock_validate_args: file was opened with " 12938 "incorrect mode")); 12939 return (EBADF); 12940 } 12941 } 12942 12943 /* Convert the offset. It may need to be restored before returning. */ 12944 if (error = convoff(vp, flk, 0, offset)) { 12945 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12946 "nfs4frlock_validate_args: convoff => error= %d\n", 12947 error)); 12948 return (error); 12949 } 12950 12951 return (error); 12952 } 12953 12954 /* 12955 * Set the flock64's lm_sysid for nfs4frlock. 12956 */ 12957 static int 12958 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12959 { 12960 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12961 12962 /* Find the lm_sysid */ 12963 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12964 12965 if (*lspp == NULL) { 12966 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12967 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12968 return (ENOLCK); 12969 } 12970 12971 flk->l_sysid = lm_sysidt(*lspp); 12972 12973 return (0); 12974 } 12975 12976 /* 12977 * Do the remaining preliminary setup for nfs4frlock. 12978 */ 12979 static void 12980 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12981 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12982 cred_t **cred_otw) 12983 { 12984 /* 12985 * set tick_delay to the base delay time. 12986 * (NFS4_BASE_WAIT_TIME is in secs) 12987 */ 12988 12989 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000); 12990 12991 /* 12992 * If lock is relative to EOF, we need the newest length of the 12993 * file. Therefore invalidate the ATTR_CACHE. 12994 */ 12995 12996 *whencep = flk->l_whence; 12997 12998 if (*whencep == 2) /* SEEK_END */ 12999 PURGE_ATTRCACHE4(vp); 13000 13001 recov_statep->rs_flags = 0; 13002 recov_statep->rs_num_retry_despite_err = 0; 13003 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 13004 } 13005 13006 /* 13007 * Initialize and allocate the data structures necessary for 13008 * the nfs4frlock call. 13009 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 13010 */ 13011 static void 13012 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 13013 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 13014 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 13015 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 13016 { 13017 int argoplist_size; 13018 int num_ops = 2; 13019 13020 *retry = FALSE; 13021 *did_start_fop = FALSE; 13022 *skip_get_err = FALSE; 13023 lost_rqstp->lr_op = 0; 13024 argoplist_size = num_ops * sizeof (nfs_argop4); 13025 /* fill array with zero */ 13026 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 13027 13028 *argspp = argsp; 13029 *respp = NULL; 13030 13031 argsp->array_len = num_ops; 13032 argsp->array = *argopp; 13033 13034 /* initialize in case of error; will get real value down below */ 13035 argsp->ctag = TAG_NONE; 13036 13037 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 13038 *op_hintp = OH_LOCKU; 13039 else 13040 *op_hintp = OH_OTHER; 13041 } 13042 13043 /* 13044 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 13045 * the proper nfs4_server_t for this instance of nfs4frlock. 13046 * Returns 0 (success) or an errno value. 13047 */ 13048 static int 13049 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 13050 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 13051 bool_t *did_start_fop, bool_t *startrecovp) 13052 { 13053 int error = 0; 13054 rnode4_t *rp; 13055 13056 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13057 13058 if (ctype == NFS4_LCK_CTYPE_NORM) { 13059 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 13060 recov_statep, startrecovp); 13061 if (error) 13062 return (error); 13063 *did_start_fop = TRUE; 13064 } else { 13065 *did_start_fop = FALSE; 13066 *startrecovp = FALSE; 13067 } 13068 13069 if (!error) { 13070 rp = VTOR4(vp); 13071 13072 /* If the file failed recovery, just quit. */ 13073 mutex_enter(&rp->r_statelock); 13074 if (rp->r_flags & R4RECOVERR) { 13075 error = EIO; 13076 } 13077 mutex_exit(&rp->r_statelock); 13078 } 13079 13080 return (error); 13081 } 13082 13083 /* 13084 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 13085 * resend nfs4frlock call is initiated by the recovery framework. 13086 * Acquires the lop and oop seqid synchronization. 13087 */ 13088 static void 13089 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 13090 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 13091 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13092 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 13093 { 13094 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 13095 int error; 13096 13097 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 13098 (CE_NOTE, 13099 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 13100 ASSERT(resend_rqstp != NULL); 13101 ASSERT(resend_rqstp->lr_op == OP_LOCK || 13102 resend_rqstp->lr_op == OP_LOCKU); 13103 13104 *oopp = resend_rqstp->lr_oop; 13105 if (resend_rqstp->lr_oop) { 13106 open_owner_hold(resend_rqstp->lr_oop); 13107 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 13108 ASSERT(error == 0); /* recov thread always succeeds */ 13109 } 13110 13111 /* Must resend this lost lock/locku request. */ 13112 ASSERT(resend_rqstp->lr_lop != NULL); 13113 *lopp = resend_rqstp->lr_lop; 13114 lock_owner_hold(resend_rqstp->lr_lop); 13115 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 13116 ASSERT(error == 0); /* recov thread always succeeds */ 13117 13118 *ospp = resend_rqstp->lr_osp; 13119 if (*ospp) 13120 open_stream_hold(resend_rqstp->lr_osp); 13121 13122 if (resend_rqstp->lr_op == OP_LOCK) { 13123 LOCK4args *lock_args; 13124 13125 argop->argop = OP_LOCK; 13126 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 13127 lock_args->locktype = resend_rqstp->lr_locktype; 13128 lock_args->reclaim = 13129 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 13130 lock_args->offset = resend_rqstp->lr_flk->l_start; 13131 lock_args->length = resend_rqstp->lr_flk->l_len; 13132 if (lock_args->length == 0) 13133 lock_args->length = ~lock_args->length; 13134 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 13135 mi2clientid(mi), &lock_args->locker); 13136 13137 switch (resend_rqstp->lr_ctype) { 13138 case NFS4_LCK_CTYPE_RESEND: 13139 argsp->ctag = TAG_LOCK_RESEND; 13140 break; 13141 case NFS4_LCK_CTYPE_REINSTATE: 13142 argsp->ctag = TAG_LOCK_REINSTATE; 13143 break; 13144 case NFS4_LCK_CTYPE_RECLAIM: 13145 argsp->ctag = TAG_LOCK_RECLAIM; 13146 break; 13147 default: 13148 argsp->ctag = TAG_LOCK_UNKNOWN; 13149 break; 13150 } 13151 } else { 13152 LOCKU4args *locku_args; 13153 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 13154 13155 argop->argop = OP_LOCKU; 13156 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 13157 locku_args->locktype = READ_LT; 13158 locku_args->seqid = lop->lock_seqid + 1; 13159 mutex_enter(&lop->lo_lock); 13160 locku_args->lock_stateid = lop->lock_stateid; 13161 mutex_exit(&lop->lo_lock); 13162 locku_args->offset = resend_rqstp->lr_flk->l_start; 13163 locku_args->length = resend_rqstp->lr_flk->l_len; 13164 if (locku_args->length == 0) 13165 locku_args->length = ~locku_args->length; 13166 13167 switch (resend_rqstp->lr_ctype) { 13168 case NFS4_LCK_CTYPE_RESEND: 13169 argsp->ctag = TAG_LOCKU_RESEND; 13170 break; 13171 case NFS4_LCK_CTYPE_REINSTATE: 13172 argsp->ctag = TAG_LOCKU_REINSTATE; 13173 break; 13174 default: 13175 argsp->ctag = TAG_LOCK_UNKNOWN; 13176 break; 13177 } 13178 } 13179 } 13180 13181 /* 13182 * Setup the LOCKT4 arguments. 13183 */ 13184 static void 13185 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13186 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 13187 rnode4_t *rp) 13188 { 13189 LOCKT4args *lockt_args; 13190 13191 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 13192 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13193 argop->argop = OP_LOCKT; 13194 argsp->ctag = TAG_LOCKT; 13195 lockt_args = &argop->nfs_argop4_u.oplockt; 13196 13197 /* 13198 * The locktype will be READ_LT unless it's 13199 * a write lock. We do this because the Solaris 13200 * system call allows the combination of 13201 * F_UNLCK and F_GETLK* and so in that case the 13202 * unlock is mapped to a read. 13203 */ 13204 if (flk->l_type == F_WRLCK) 13205 lockt_args->locktype = WRITE_LT; 13206 else 13207 lockt_args->locktype = READ_LT; 13208 13209 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 13210 /* set the lock owner4 args */ 13211 nfs4_setlockowner_args(&lockt_args->owner, rp, 13212 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13213 flk->l_pid); 13214 lockt_args->offset = flk->l_start; 13215 lockt_args->length = flk->l_len; 13216 if (flk->l_len == 0) 13217 lockt_args->length = ~lockt_args->length; 13218 13219 *lockt_argsp = lockt_args; 13220 } 13221 13222 /* 13223 * If the client is holding a delegation, and the open stream to be used 13224 * with this lock request is a delegation open stream, then re-open the stream. 13225 * Sets the nfs4_error_t to all zeros unless the open stream has already 13226 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13227 * means the caller should retry (like a recovery retry). 13228 */ 13229 static void 13230 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13231 { 13232 open_delegation_type4 dt; 13233 bool_t reopen_needed, force; 13234 nfs4_open_stream_t *osp; 13235 open_claim_type4 oclaim; 13236 rnode4_t *rp = VTOR4(vp); 13237 mntinfo4_t *mi = VTOMI4(vp); 13238 13239 ASSERT(nfs_zone() == mi->mi_zone); 13240 13241 nfs4_error_zinit(ep); 13242 13243 mutex_enter(&rp->r_statev4_lock); 13244 dt = rp->r_deleg_type; 13245 mutex_exit(&rp->r_statev4_lock); 13246 13247 if (dt != OPEN_DELEGATE_NONE) { 13248 nfs4_open_owner_t *oop; 13249 13250 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13251 if (!oop) { 13252 ep->stat = NFS4ERR_IO; 13253 return; 13254 } 13255 /* returns with 'os_sync_lock' held */ 13256 osp = find_open_stream(oop, rp); 13257 if (!osp) { 13258 open_owner_rele(oop); 13259 ep->stat = NFS4ERR_IO; 13260 return; 13261 } 13262 13263 if (osp->os_failed_reopen) { 13264 NFS4_DEBUG((nfs4_open_stream_debug || 13265 nfs4_client_lock_debug), (CE_NOTE, 13266 "nfs4frlock_check_deleg: os_failed_reopen set " 13267 "for osp %p, cr %p, rp %s", (void *)osp, 13268 (void *)cr, rnode4info(rp))); 13269 mutex_exit(&osp->os_sync_lock); 13270 open_stream_rele(osp, rp); 13271 open_owner_rele(oop); 13272 ep->stat = NFS4ERR_IO; 13273 return; 13274 } 13275 13276 /* 13277 * Determine whether a reopen is needed. If this 13278 * is a delegation open stream, then send the open 13279 * to the server to give visibility to the open owner. 13280 * Even if it isn't a delegation open stream, we need 13281 * to check if the previous open CLAIM_DELEGATE_CUR 13282 * was sufficient. 13283 */ 13284 13285 reopen_needed = osp->os_delegation || 13286 ((lt == F_RDLCK && 13287 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13288 (lt == F_WRLCK && 13289 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13290 13291 mutex_exit(&osp->os_sync_lock); 13292 open_owner_rele(oop); 13293 13294 if (reopen_needed) { 13295 /* 13296 * Always use CLAIM_PREVIOUS after server reboot. 13297 * The server will reject CLAIM_DELEGATE_CUR if 13298 * it is used during the grace period. 13299 */ 13300 mutex_enter(&mi->mi_lock); 13301 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13302 oclaim = CLAIM_PREVIOUS; 13303 force = TRUE; 13304 } else { 13305 oclaim = CLAIM_DELEGATE_CUR; 13306 force = FALSE; 13307 } 13308 mutex_exit(&mi->mi_lock); 13309 13310 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13311 if (ep->error == EAGAIN) { 13312 nfs4_error_zinit(ep); 13313 ep->stat = NFS4ERR_DELAY; 13314 } 13315 } 13316 open_stream_rele(osp, rp); 13317 osp = NULL; 13318 } 13319 } 13320 13321 /* 13322 * Setup the LOCKU4 arguments. 13323 * Returns errors via the nfs4_error_t. 13324 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13325 * over-the-wire. The caller must release the 13326 * reference on *lopp. 13327 * NFS4ERR_DELAY caller should retry (like recovery retry) 13328 * (other) unrecoverable error. 13329 */ 13330 static void 13331 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13332 LOCKU4args **locku_argsp, flock64_t *flk, 13333 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13334 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13335 bool_t *skip_get_err, bool_t *go_otwp) 13336 { 13337 nfs4_lock_owner_t *lop = NULL; 13338 LOCKU4args *locku_args; 13339 pid_t pid; 13340 bool_t is_spec = FALSE; 13341 rnode4_t *rp = VTOR4(vp); 13342 13343 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13344 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13345 13346 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13347 if (ep->error || ep->stat) 13348 return; 13349 13350 argop->argop = OP_LOCKU; 13351 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13352 argsp->ctag = TAG_LOCKU_REINSTATE; 13353 else 13354 argsp->ctag = TAG_LOCKU; 13355 locku_args = &argop->nfs_argop4_u.oplocku; 13356 *locku_argsp = locku_args; 13357 13358 /* 13359 * XXX what should locku_args->locktype be? 13360 * setting to ALWAYS be READ_LT so at least 13361 * it is a valid locktype. 13362 */ 13363 13364 locku_args->locktype = READ_LT; 13365 13366 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13367 flk->l_pid; 13368 13369 /* 13370 * Get the lock owner stateid. If no lock owner 13371 * exists, return success. 13372 */ 13373 lop = find_lock_owner(rp, pid, LOWN_ANY); 13374 *lopp = lop; 13375 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13376 is_spec = TRUE; 13377 if (!lop || is_spec) { 13378 /* 13379 * No lock owner so no locks to unlock. 13380 * Return success. If there was a failed 13381 * reclaim earlier, the lock might still be 13382 * registered with the local locking code, 13383 * so notify it of the unlock. 13384 * 13385 * If the lockowner is using a special stateid, 13386 * then the original lock request (that created 13387 * this lockowner) was never successful, so we 13388 * have no lock to undo OTW. 13389 */ 13390 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13391 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13392 "(%ld) so return success", (long)pid)); 13393 13394 if (ctype == NFS4_LCK_CTYPE_NORM) 13395 flk->l_pid = curproc->p_pid; 13396 nfs4_register_lock_locally(vp, flk, flag, offset); 13397 /* 13398 * Release our hold and NULL out so final_cleanup 13399 * doesn't try to end a lock seqid sync we 13400 * never started. 13401 */ 13402 if (is_spec) { 13403 lock_owner_rele(lop); 13404 *lopp = NULL; 13405 } 13406 *skip_get_err = TRUE; 13407 *go_otwp = FALSE; 13408 return; 13409 } 13410 13411 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13412 if (ep->error == EAGAIN) { 13413 lock_owner_rele(lop); 13414 *lopp = NULL; 13415 return; 13416 } 13417 13418 mutex_enter(&lop->lo_lock); 13419 locku_args->lock_stateid = lop->lock_stateid; 13420 mutex_exit(&lop->lo_lock); 13421 locku_args->seqid = lop->lock_seqid + 1; 13422 13423 /* leave the ref count on lop, rele after RPC call */ 13424 13425 locku_args->offset = flk->l_start; 13426 locku_args->length = flk->l_len; 13427 if (flk->l_len == 0) 13428 locku_args->length = ~locku_args->length; 13429 13430 *go_otwp = TRUE; 13431 } 13432 13433 /* 13434 * Setup the LOCK4 arguments. 13435 * 13436 * Returns errors via the nfs4_error_t. 13437 * NFS4_OK no problems 13438 * NFS4ERR_DELAY caller should retry (like recovery retry) 13439 * (other) unrecoverable error 13440 */ 13441 static void 13442 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13443 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13444 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13445 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13446 { 13447 LOCK4args *lock_args; 13448 nfs4_open_owner_t *oop = NULL; 13449 nfs4_open_stream_t *osp = NULL; 13450 nfs4_lock_owner_t *lop = NULL; 13451 pid_t pid; 13452 rnode4_t *rp = VTOR4(vp); 13453 13454 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13455 13456 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13457 if (ep->error || ep->stat != NFS4_OK) 13458 return; 13459 13460 argop->argop = OP_LOCK; 13461 if (ctype == NFS4_LCK_CTYPE_NORM) 13462 argsp->ctag = TAG_LOCK; 13463 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13464 argsp->ctag = TAG_RELOCK; 13465 else 13466 argsp->ctag = TAG_LOCK_REINSTATE; 13467 lock_args = &argop->nfs_argop4_u.oplock; 13468 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13469 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13470 /* 13471 * Get the lock owner. If no lock owner exists, 13472 * create a 'temporary' one and grab the open seqid 13473 * synchronization (which puts a hold on the open 13474 * owner and open stream). 13475 * This also grabs the lock seqid synchronization. 13476 */ 13477 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13478 ep->stat = 13479 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13480 13481 if (ep->stat != NFS4_OK) 13482 goto out; 13483 13484 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13485 &lock_args->locker); 13486 13487 lock_args->offset = flk->l_start; 13488 lock_args->length = flk->l_len; 13489 if (flk->l_len == 0) 13490 lock_args->length = ~lock_args->length; 13491 *lock_argsp = lock_args; 13492 out: 13493 *oopp = oop; 13494 *ospp = osp; 13495 *lopp = lop; 13496 } 13497 13498 /* 13499 * After we get the reply from the server, record the proper information 13500 * for possible resend lock requests. 13501 * 13502 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13503 */ 13504 static void 13505 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13506 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13507 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13508 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13509 { 13510 bool_t unlock = (flk->l_type == F_UNLCK); 13511 13512 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13513 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13514 ctype == NFS4_LCK_CTYPE_REINSTATE); 13515 13516 if (error != 0 && !unlock) { 13517 NFS4_DEBUG((nfs4_lost_rqst_debug || 13518 nfs4_client_lock_debug), (CE_NOTE, 13519 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13520 " for lop %p", (void *)lop)); 13521 ASSERT(lop != NULL); 13522 mutex_enter(&lop->lo_lock); 13523 lop->lo_pending_rqsts = 1; 13524 mutex_exit(&lop->lo_lock); 13525 } 13526 13527 lost_rqstp->lr_putfirst = FALSE; 13528 lost_rqstp->lr_op = 0; 13529 13530 /* 13531 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13532 * recovery purposes so that the lock request that was sent 13533 * can be saved and re-issued later. Ditto for EIO from a forced 13534 * unmount. This is done to have the client's local locking state 13535 * match the v4 server's state; that is, the request was 13536 * potentially received and accepted by the server but the client 13537 * thinks it was not. 13538 */ 13539 if (error == ETIMEDOUT || error == EINTR || 13540 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13541 NFS4_DEBUG((nfs4_lost_rqst_debug || 13542 nfs4_client_lock_debug), (CE_NOTE, 13543 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13544 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13545 (void *)lop, (void *)oop, (void *)osp)); 13546 if (unlock) 13547 lost_rqstp->lr_op = OP_LOCKU; 13548 else { 13549 lost_rqstp->lr_op = OP_LOCK; 13550 lost_rqstp->lr_locktype = locktype; 13551 } 13552 /* 13553 * Objects are held and rele'd via the recovery code. 13554 * See nfs4_save_lost_rqst. 13555 */ 13556 lost_rqstp->lr_vp = vp; 13557 lost_rqstp->lr_dvp = NULL; 13558 lost_rqstp->lr_oop = oop; 13559 lost_rqstp->lr_osp = osp; 13560 lost_rqstp->lr_lop = lop; 13561 lost_rqstp->lr_cr = cr; 13562 switch (ctype) { 13563 case NFS4_LCK_CTYPE_NORM: 13564 flk->l_pid = ttoproc(curthread)->p_pid; 13565 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13566 break; 13567 case NFS4_LCK_CTYPE_REINSTATE: 13568 lost_rqstp->lr_putfirst = TRUE; 13569 lost_rqstp->lr_ctype = ctype; 13570 break; 13571 default: 13572 break; 13573 } 13574 lost_rqstp->lr_flk = flk; 13575 } 13576 } 13577 13578 /* 13579 * Update lop's seqid. Also update the seqid stored in a resend request, 13580 * if any. (Some recovery errors increment the seqid, and we may have to 13581 * send the resend request again.) 13582 */ 13583 13584 static void 13585 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13586 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13587 { 13588 if (lock_args) { 13589 if (lock_args->locker.new_lock_owner == TRUE) 13590 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13591 else { 13592 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13593 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13594 } 13595 } else if (locku_args) { 13596 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13597 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13598 } 13599 } 13600 13601 /* 13602 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13603 * COMPOUND4 args/res for calls that need to retry. 13604 * Switches the *cred_otwp to base_cr. 13605 */ 13606 static void 13607 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13608 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13609 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13610 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13611 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13612 { 13613 nfs4_open_owner_t *oop = *oopp; 13614 nfs4_open_stream_t *osp = *ospp; 13615 nfs4_lock_owner_t *lop = *lopp; 13616 nfs_argop4 *argop = (*argspp)->array; 13617 13618 if (*did_start_fop) { 13619 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13620 needrecov); 13621 *did_start_fop = FALSE; 13622 } 13623 ASSERT((*argspp)->array_len == 2); 13624 if (argop[1].argop == OP_LOCK) 13625 nfs4args_lock_free(&argop[1]); 13626 else if (argop[1].argop == OP_LOCKT) 13627 nfs4args_lockt_free(&argop[1]); 13628 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13629 if (!error) 13630 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13631 *argspp = NULL; 13632 *respp = NULL; 13633 13634 if (lop) { 13635 nfs4_end_lock_seqid_sync(lop); 13636 lock_owner_rele(lop); 13637 *lopp = NULL; 13638 } 13639 13640 /* need to free up the reference on osp for lock args */ 13641 if (osp != NULL) { 13642 open_stream_rele(osp, VTOR4(vp)); 13643 *ospp = NULL; 13644 } 13645 13646 /* need to free up the reference on oop for lock args */ 13647 if (oop != NULL) { 13648 nfs4_end_open_seqid_sync(oop); 13649 open_owner_rele(oop); 13650 *oopp = NULL; 13651 } 13652 13653 crfree(*cred_otwp); 13654 *cred_otwp = base_cr; 13655 crhold(*cred_otwp); 13656 } 13657 13658 /* 13659 * Function to process the client's recovery for nfs4frlock. 13660 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13661 * 13662 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13663 * COMPOUND4 args/res for calls that need to retry. 13664 * 13665 * Note: the rp's r_lkserlock is *not* dropped during this path. 13666 */ 13667 static bool_t 13668 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13669 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13670 LOCK4args *lock_args, LOCKU4args *locku_args, 13671 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13672 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13673 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13674 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13675 { 13676 nfs4_open_owner_t *oop = *oopp; 13677 nfs4_open_stream_t *osp = *ospp; 13678 nfs4_lock_owner_t *lop = *lopp; 13679 13680 bool_t abort, retry; 13681 13682 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13683 ASSERT((*argspp) != NULL); 13684 ASSERT((*respp) != NULL); 13685 if (lock_args || locku_args) 13686 ASSERT(lop != NULL); 13687 13688 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13689 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13690 13691 retry = TRUE; 13692 abort = FALSE; 13693 if (needrecov) { 13694 nfs4_bseqid_entry_t *bsep = NULL; 13695 nfs_opnum4 op; 13696 13697 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13698 13699 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13700 seqid4 seqid; 13701 13702 if (lock_args) { 13703 if (lock_args->locker.new_lock_owner == TRUE) 13704 seqid = lock_args->locker.locker4_u. 13705 open_owner.open_seqid; 13706 else 13707 seqid = lock_args->locker.locker4_u. 13708 lock_owner.lock_seqid; 13709 } else if (locku_args) { 13710 seqid = locku_args->seqid; 13711 } else { 13712 seqid = 0; 13713 } 13714 13715 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13716 flk->l_pid, (*argspp)->ctag, seqid); 13717 } 13718 13719 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13720 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13721 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13722 NULL, op, bsep, NULL, NULL); 13723 13724 if (bsep) 13725 kmem_free(bsep, sizeof (*bsep)); 13726 } 13727 13728 /* 13729 * Return that we do not want to retry the request for 3 cases: 13730 * 1. If we received EINTR or are bailing out because of a forced 13731 * unmount, we came into this code path just for the sake of 13732 * initiating recovery, we now need to return the error. 13733 * 2. If we have aborted recovery. 13734 * 3. We received NFS4ERR_BAD_SEQID. 13735 */ 13736 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13737 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13738 retry = FALSE; 13739 13740 if (*did_start_fop == TRUE) { 13741 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13742 needrecov); 13743 *did_start_fop = FALSE; 13744 } 13745 13746 if (retry == TRUE) { 13747 nfs_argop4 *argop; 13748 13749 argop = (*argspp)->array; 13750 ASSERT((*argspp)->array_len == 2); 13751 13752 if (argop[1].argop == OP_LOCK) 13753 nfs4args_lock_free(&argop[1]); 13754 else if (argop[1].argop == OP_LOCKT) 13755 nfs4args_lockt_free(&argop[1]); 13756 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13757 if (!ep->error) 13758 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13759 *respp = NULL; 13760 *argspp = NULL; 13761 } 13762 13763 if (lop != NULL) { 13764 nfs4_end_lock_seqid_sync(lop); 13765 lock_owner_rele(lop); 13766 } 13767 13768 *lopp = NULL; 13769 13770 /* need to free up the reference on osp for lock args */ 13771 if (osp != NULL) { 13772 open_stream_rele(osp, rp); 13773 *ospp = NULL; 13774 } 13775 13776 /* need to free up the reference on oop for lock args */ 13777 if (oop != NULL) { 13778 nfs4_end_open_seqid_sync(oop); 13779 open_owner_rele(oop); 13780 *oopp = NULL; 13781 } 13782 13783 return (retry); 13784 } 13785 13786 /* 13787 * Handles the successful reply from the server for nfs4frlock. 13788 */ 13789 static void 13790 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13791 vnode_t *vp, int flag, u_offset_t offset, 13792 nfs4_lost_rqst_t *resend_rqstp) 13793 { 13794 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13795 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13796 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13797 if (ctype == NFS4_LCK_CTYPE_NORM) { 13798 flk->l_pid = ttoproc(curthread)->p_pid; 13799 /* 13800 * We do not register lost locks locally in 13801 * the 'resend' case since the user/application 13802 * doesn't think we have the lock. 13803 */ 13804 ASSERT(!resend_rqstp); 13805 nfs4_register_lock_locally(vp, flk, flag, offset); 13806 } 13807 } 13808 } 13809 13810 /* 13811 * Handle the DENIED reply from the server for nfs4frlock. 13812 * Returns TRUE if we should retry the request; FALSE otherwise. 13813 * 13814 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13815 * COMPOUND4 args/res for calls that need to retry. Can also 13816 * drop and regrab the r_lkserlock. 13817 */ 13818 static bool_t 13819 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13820 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13821 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13822 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13823 nfs4_recov_state_t *recov_statep, int needrecov, 13824 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13825 clock_t *tick_delayp, short *whencep, int *errorp, 13826 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13827 bool_t *skip_get_err) 13828 { 13829 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13830 13831 if (lock_args) { 13832 nfs4_open_owner_t *oop = *oopp; 13833 nfs4_open_stream_t *osp = *ospp; 13834 nfs4_lock_owner_t *lop = *lopp; 13835 int intr; 13836 13837 /* 13838 * Blocking lock needs to sleep and retry from the request. 13839 * 13840 * Do not block and wait for 'resend' or 'reinstate' 13841 * lock requests, just return the error. 13842 * 13843 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13844 */ 13845 if (cmd == F_SETLKW) { 13846 rnode4_t *rp = VTOR4(vp); 13847 nfs_argop4 *argop = (*argspp)->array; 13848 13849 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13850 13851 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13852 recov_statep, needrecov); 13853 *did_start_fop = FALSE; 13854 ASSERT((*argspp)->array_len == 2); 13855 if (argop[1].argop == OP_LOCK) 13856 nfs4args_lock_free(&argop[1]); 13857 else if (argop[1].argop == OP_LOCKT) 13858 nfs4args_lockt_free(&argop[1]); 13859 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13860 if (*respp) 13861 (void) xdr_free(xdr_COMPOUND4res_clnt, 13862 (caddr_t)*respp); 13863 *argspp = NULL; 13864 *respp = NULL; 13865 nfs4_end_lock_seqid_sync(lop); 13866 lock_owner_rele(lop); 13867 *lopp = NULL; 13868 if (osp != NULL) { 13869 open_stream_rele(osp, rp); 13870 *ospp = NULL; 13871 } 13872 if (oop != NULL) { 13873 nfs4_end_open_seqid_sync(oop); 13874 open_owner_rele(oop); 13875 *oopp = NULL; 13876 } 13877 13878 nfs_rw_exit(&rp->r_lkserlock); 13879 13880 intr = nfs4_block_and_wait(tick_delayp, rp); 13881 13882 if (intr) { 13883 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13884 RW_WRITER, FALSE); 13885 *errorp = EINTR; 13886 return (FALSE); 13887 } 13888 13889 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13890 RW_WRITER, FALSE); 13891 13892 /* 13893 * Make sure we are still safe to lock with 13894 * regards to mmapping. 13895 */ 13896 if (!nfs4_safelock(vp, flk, cr)) { 13897 *errorp = EAGAIN; 13898 return (FALSE); 13899 } 13900 13901 return (TRUE); 13902 } 13903 if (ctype == NFS4_LCK_CTYPE_NORM) 13904 *errorp = EAGAIN; 13905 *skip_get_err = TRUE; 13906 flk->l_whence = 0; 13907 *whencep = 0; 13908 return (FALSE); 13909 } else if (lockt_args) { 13910 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13911 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13912 13913 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13914 flk, lockt_args); 13915 13916 /* according to NLM code */ 13917 *errorp = 0; 13918 *whencep = 0; 13919 *skip_get_err = TRUE; 13920 return (FALSE); 13921 } 13922 return (FALSE); 13923 } 13924 13925 /* 13926 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13927 */ 13928 static void 13929 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13930 { 13931 switch (resp->status) { 13932 case NFS4ERR_ACCESS: 13933 case NFS4ERR_ADMIN_REVOKED: 13934 case NFS4ERR_BADHANDLE: 13935 case NFS4ERR_BAD_RANGE: 13936 case NFS4ERR_BAD_SEQID: 13937 case NFS4ERR_BAD_STATEID: 13938 case NFS4ERR_BADXDR: 13939 case NFS4ERR_DEADLOCK: 13940 case NFS4ERR_DELAY: 13941 case NFS4ERR_EXPIRED: 13942 case NFS4ERR_FHEXPIRED: 13943 case NFS4ERR_GRACE: 13944 case NFS4ERR_INVAL: 13945 case NFS4ERR_ISDIR: 13946 case NFS4ERR_LEASE_MOVED: 13947 case NFS4ERR_LOCK_NOTSUPP: 13948 case NFS4ERR_LOCK_RANGE: 13949 case NFS4ERR_MOVED: 13950 case NFS4ERR_NOFILEHANDLE: 13951 case NFS4ERR_NO_GRACE: 13952 case NFS4ERR_OLD_STATEID: 13953 case NFS4ERR_OPENMODE: 13954 case NFS4ERR_RECLAIM_BAD: 13955 case NFS4ERR_RECLAIM_CONFLICT: 13956 case NFS4ERR_RESOURCE: 13957 case NFS4ERR_SERVERFAULT: 13958 case NFS4ERR_STALE: 13959 case NFS4ERR_STALE_CLIENTID: 13960 case NFS4ERR_STALE_STATEID: 13961 return; 13962 default: 13963 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13964 "nfs4frlock_results_default: got unrecognizable " 13965 "res.status %d", resp->status)); 13966 *errorp = NFS4ERR_INVAL; 13967 } 13968 } 13969 13970 /* 13971 * The lock request was successful, so update the client's state. 13972 */ 13973 static void 13974 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13975 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13976 vnode_t *vp, flock64_t *flk, cred_t *cr, 13977 nfs4_lost_rqst_t *resend_rqstp) 13978 { 13979 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13980 13981 if (lock_args) { 13982 LOCK4res *lock_res; 13983 13984 lock_res = &resop->nfs_resop4_u.oplock; 13985 /* update the stateid with server's response */ 13986 13987 if (lock_args->locker.new_lock_owner == TRUE) { 13988 mutex_enter(&lop->lo_lock); 13989 lop->lo_just_created = NFS4_PERM_CREATED; 13990 mutex_exit(&lop->lo_lock); 13991 } 13992 13993 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13994 13995 /* 13996 * If the lock was the result of a resending a lost 13997 * request, we've synched up the stateid and seqid 13998 * with the server, but now the server might be out of sync 13999 * with what the application thinks it has for locks. 14000 * Clean that up here. It's unclear whether we should do 14001 * this even if the filesystem has been forcibly unmounted. 14002 * For most servers, it's probably wasted effort, but 14003 * RFC3530 lets servers require that unlocks exactly match 14004 * the locks that are held. 14005 */ 14006 if (resend_rqstp != NULL && 14007 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 14008 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 14009 } else { 14010 flk->l_whence = 0; 14011 } 14012 } else if (locku_args) { 14013 LOCKU4res *locku_res; 14014 14015 locku_res = &resop->nfs_resop4_u.oplocku; 14016 14017 /* Update the stateid with the server's response */ 14018 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 14019 } else if (lockt_args) { 14020 /* Switch the lock type to express success, see fcntl */ 14021 flk->l_type = F_UNLCK; 14022 flk->l_whence = 0; 14023 } 14024 } 14025 14026 /* 14027 * Do final cleanup before exiting nfs4frlock. 14028 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 14029 * COMPOUND4 args/res for calls that haven't already. 14030 */ 14031 static void 14032 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 14033 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 14034 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 14035 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 14036 short whence, u_offset_t offset, struct lm_sysid *ls, 14037 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 14038 bool_t did_start_fop, bool_t skip_get_err, 14039 cred_t *cred_otw, cred_t *cred) 14040 { 14041 mntinfo4_t *mi = VTOMI4(vp); 14042 rnode4_t *rp = VTOR4(vp); 14043 int error = *errorp; 14044 nfs_argop4 *argop; 14045 int do_flush_pages = 0; 14046 14047 ASSERT(nfs_zone() == mi->mi_zone); 14048 /* 14049 * The client recovery code wants the raw status information, 14050 * so don't map the NFS status code to an errno value for 14051 * non-normal call types. 14052 */ 14053 if (ctype == NFS4_LCK_CTYPE_NORM) { 14054 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 14055 *errorp = geterrno4(resp->status); 14056 if (did_start_fop == TRUE) 14057 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 14058 needrecov); 14059 14060 /* 14061 * We've established a new lock on the server, so invalidate 14062 * the pages associated with the vnode to get the most up to 14063 * date pages from the server after acquiring the lock. We 14064 * want to be sure that the read operation gets the newest data. 14065 * N.B. 14066 * We used to do this in nfs4frlock_results_ok but that doesn't 14067 * work since VOP_PUTPAGE can call nfs4_commit which calls 14068 * nfs4_start_fop. We flush the pages below after calling 14069 * nfs4_end_fop above 14070 * The flush of the page cache must be done after 14071 * nfs4_end_open_seqid_sync() to avoid a 4-way hang. 14072 */ 14073 if (!error && resp && resp->status == NFS4_OK) 14074 do_flush_pages = 1; 14075 } 14076 if (argsp) { 14077 ASSERT(argsp->array_len == 2); 14078 argop = argsp->array; 14079 if (argop[1].argop == OP_LOCK) 14080 nfs4args_lock_free(&argop[1]); 14081 else if (argop[1].argop == OP_LOCKT) 14082 nfs4args_lockt_free(&argop[1]); 14083 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14084 if (resp) 14085 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 14086 } 14087 14088 /* free the reference on the lock owner */ 14089 if (lop != NULL) { 14090 nfs4_end_lock_seqid_sync(lop); 14091 lock_owner_rele(lop); 14092 } 14093 14094 /* need to free up the reference on osp for lock args */ 14095 if (osp != NULL) 14096 open_stream_rele(osp, rp); 14097 14098 /* need to free up the reference on oop for lock args */ 14099 if (oop != NULL) { 14100 nfs4_end_open_seqid_sync(oop); 14101 open_owner_rele(oop); 14102 } 14103 14104 if (do_flush_pages) 14105 nfs4_flush_pages(vp, cred); 14106 14107 (void) convoff(vp, flk, whence, offset); 14108 14109 lm_rel_sysid(ls); 14110 14111 /* 14112 * Record debug information in the event we get EINVAL. 14113 */ 14114 mutex_enter(&mi->mi_lock); 14115 if (*errorp == EINVAL && (lock_args || locku_args) && 14116 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 14117 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 14118 zcmn_err(getzoneid(), CE_NOTE, 14119 "%s operation failed with " 14120 "EINVAL probably since the server, %s," 14121 " doesn't support POSIX style locking", 14122 lock_args ? "LOCK" : "LOCKU", 14123 mi->mi_curr_serv->sv_hostname); 14124 mi->mi_flags |= MI4_LOCK_DEBUG; 14125 } 14126 } 14127 mutex_exit(&mi->mi_lock); 14128 14129 if (cred_otw) 14130 crfree(cred_otw); 14131 } 14132 14133 /* 14134 * This calls the server and the local locking code. 14135 * 14136 * Client locks are registerred locally by oring the sysid with 14137 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 14138 * We need to distinguish between the two to avoid collision in case one 14139 * machine is used as both client and server. 14140 * 14141 * Blocking lock requests will continually retry to acquire the lock 14142 * forever. 14143 * 14144 * The ctype is defined as follows: 14145 * NFS4_LCK_CTYPE_NORM: normal lock request. 14146 * 14147 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 14148 * recovery, get the pid from flk instead of curproc, and don't reregister 14149 * the lock locally. 14150 * 14151 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 14152 * that we will use the information passed in via resend_rqstp to setup the 14153 * lock/locku request. This resend is the exact same request as the 'lost 14154 * lock', and is initiated by the recovery framework. A successful resend 14155 * request can initiate one or more reinstate requests. 14156 * 14157 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 14158 * does not trigger additional reinstate requests. This lock call type is 14159 * set for setting the v4 server's locking state back to match what the 14160 * client's local locking state is in the event of a received 'lost lock'. 14161 * 14162 * Errors are returned via the nfs4_error_t parameter. 14163 */ 14164 void 14165 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 14166 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 14167 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 14168 { 14169 COMPOUND4args_clnt args, *argsp = NULL; 14170 COMPOUND4res_clnt res, *resp = NULL; 14171 nfs_argop4 *argop; 14172 nfs_resop4 *resop; 14173 rnode4_t *rp; 14174 int doqueue = 1; 14175 clock_t tick_delay; /* delay in clock ticks */ 14176 struct lm_sysid *ls; 14177 LOCK4args *lock_args = NULL; 14178 LOCKU4args *locku_args = NULL; 14179 LOCKT4args *lockt_args = NULL; 14180 nfs4_open_owner_t *oop = NULL; 14181 nfs4_open_stream_t *osp = NULL; 14182 nfs4_lock_owner_t *lop = NULL; 14183 bool_t needrecov = FALSE; 14184 nfs4_recov_state_t recov_state; 14185 short whence; 14186 nfs4_op_hint_t op_hint; 14187 nfs4_lost_rqst_t lost_rqst; 14188 bool_t retry = FALSE; 14189 bool_t did_start_fop = FALSE; 14190 bool_t skip_get_err = FALSE; 14191 cred_t *cred_otw = NULL; 14192 bool_t recovonly; /* just queue request */ 14193 int frc_no_reclaim = 0; 14194 #ifdef DEBUG 14195 char *name; 14196 #endif 14197 14198 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14199 14200 #ifdef DEBUG 14201 name = fn_name(VTOSV(vp)->sv_name); 14202 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 14203 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 14204 "length %"PRIu64", pid %d, sysid %d, call type %s, " 14205 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 14206 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 14207 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 14208 resend_rqstp ? "TRUE" : "FALSE")); 14209 kmem_free(name, MAXNAMELEN); 14210 #endif 14211 14212 nfs4_error_zinit(ep); 14213 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14214 if (ep->error) 14215 return; 14216 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14217 if (ep->error) 14218 return; 14219 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14220 vp, cr, &cred_otw); 14221 14222 recov_retry: 14223 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14224 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14225 rp = VTOR4(vp); 14226 14227 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14228 &did_start_fop, &recovonly); 14229 14230 if (ep->error) 14231 goto out; 14232 14233 if (recovonly) { 14234 /* 14235 * Leave the request for the recovery system to deal with. 14236 */ 14237 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14238 ASSERT(cmd != F_GETLK); 14239 ASSERT(flk->l_type == F_UNLCK); 14240 14241 nfs4_error_init(ep, EINTR); 14242 needrecov = TRUE; 14243 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14244 if (lop != NULL) { 14245 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14246 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14247 (void) nfs4_start_recovery(ep, 14248 VTOMI4(vp), vp, NULL, NULL, 14249 (lost_rqst.lr_op == OP_LOCK || 14250 lost_rqst.lr_op == OP_LOCKU) ? 14251 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL); 14252 lock_owner_rele(lop); 14253 lop = NULL; 14254 } 14255 flk->l_pid = curproc->p_pid; 14256 nfs4_register_lock_locally(vp, flk, flag, offset); 14257 goto out; 14258 } 14259 14260 /* putfh directory fh */ 14261 argop[0].argop = OP_CPUTFH; 14262 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14263 14264 /* 14265 * Set up the over-the-wire arguments and get references to the 14266 * open owner, etc. 14267 */ 14268 14269 if (ctype == NFS4_LCK_CTYPE_RESEND || 14270 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14271 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14272 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14273 } else { 14274 bool_t go_otw = TRUE; 14275 14276 ASSERT(resend_rqstp == NULL); 14277 14278 switch (cmd) { 14279 case F_GETLK: 14280 case F_O_GETLK: 14281 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14282 &lockt_args, argsp, flk, rp); 14283 break; 14284 case F_SETLKW: 14285 case F_SETLK: 14286 if (flk->l_type == F_UNLCK) 14287 nfs4frlock_setup_locku_args(ctype, 14288 &argop[1], &locku_args, flk, 14289 &lop, ep, argsp, 14290 vp, flag, offset, cr, 14291 &skip_get_err, &go_otw); 14292 else 14293 nfs4frlock_setup_lock_args(ctype, 14294 &lock_args, &oop, &osp, &lop, &argop[1], 14295 argsp, flk, cmd, vp, cr, ep); 14296 14297 if (ep->error) 14298 goto out; 14299 14300 switch (ep->stat) { 14301 case NFS4_OK: 14302 break; 14303 case NFS4ERR_DELAY: 14304 /* recov thread never gets this error */ 14305 ASSERT(resend_rqstp == NULL); 14306 ASSERT(did_start_fop); 14307 14308 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14309 &recov_state, TRUE); 14310 did_start_fop = FALSE; 14311 if (argop[1].argop == OP_LOCK) 14312 nfs4args_lock_free(&argop[1]); 14313 else if (argop[1].argop == OP_LOCKT) 14314 nfs4args_lockt_free(&argop[1]); 14315 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14316 argsp = NULL; 14317 goto recov_retry; 14318 default: 14319 ep->error = EIO; 14320 goto out; 14321 } 14322 break; 14323 default: 14324 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14325 "nfs4_frlock: invalid cmd %d", cmd)); 14326 ep->error = EINVAL; 14327 goto out; 14328 } 14329 14330 if (!go_otw) 14331 goto out; 14332 } 14333 14334 /* XXX should we use the local reclock as a cache ? */ 14335 /* 14336 * Unregister the lock with the local locking code before 14337 * contacting the server. This avoids a potential race where 14338 * another process gets notified that it has been granted a lock 14339 * before we can unregister ourselves locally. 14340 */ 14341 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14342 if (ctype == NFS4_LCK_CTYPE_NORM) 14343 flk->l_pid = ttoproc(curthread)->p_pid; 14344 nfs4_register_lock_locally(vp, flk, flag, offset); 14345 } 14346 14347 /* 14348 * Send the server the lock request. Continually loop with a delay 14349 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14350 */ 14351 resp = &res; 14352 14353 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14354 (CE_NOTE, 14355 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14356 rnode4info(rp))); 14357 14358 if (lock_args && frc_no_reclaim) { 14359 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14360 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14361 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14362 lock_args->reclaim = FALSE; 14363 if (did_reclaimp) 14364 *did_reclaimp = 0; 14365 } 14366 14367 /* 14368 * Do the OTW call. 14369 */ 14370 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14371 14372 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14373 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14374 14375 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14376 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14377 "nfs4frlock: needrecov %d", needrecov)); 14378 14379 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14380 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14381 args.ctag); 14382 14383 /* 14384 * Check if one of these mutually exclusive error cases has 14385 * happened: 14386 * need to swap credentials due to access error 14387 * recovery is needed 14388 * different error (only known case is missing Kerberos ticket) 14389 */ 14390 14391 if ((ep->error == EACCES || 14392 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14393 cred_otw != cr) { 14394 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14395 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14396 cr, &cred_otw); 14397 goto recov_retry; 14398 } 14399 14400 if (needrecov) { 14401 /* 14402 * LOCKT requests don't need to recover from lost 14403 * requests since they don't create/modify state. 14404 */ 14405 if ((ep->error == EINTR || 14406 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14407 lockt_args) 14408 goto out; 14409 /* 14410 * Do not attempt recovery for requests initiated by 14411 * the recovery framework. Let the framework redrive them. 14412 */ 14413 if (ctype != NFS4_LCK_CTYPE_NORM) 14414 goto out; 14415 else { 14416 ASSERT(resend_rqstp == NULL); 14417 } 14418 14419 nfs4frlock_save_lost_rqst(ctype, ep->error, 14420 flk_to_locktype(cmd, flk->l_type), 14421 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14422 14423 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14424 &resp, lock_args, locku_args, &oop, &osp, &lop, 14425 rp, vp, &recov_state, op_hint, &did_start_fop, 14426 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14427 14428 if (retry) { 14429 ASSERT(oop == NULL); 14430 ASSERT(osp == NULL); 14431 ASSERT(lop == NULL); 14432 goto recov_retry; 14433 } 14434 goto out; 14435 } 14436 14437 /* 14438 * Bail out if have reached this point with ep->error set. Can 14439 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14440 * This happens if Kerberos ticket has expired or has been 14441 * destroyed. 14442 */ 14443 if (ep->error != 0) 14444 goto out; 14445 14446 /* 14447 * Process the reply. 14448 */ 14449 switch (resp->status) { 14450 case NFS4_OK: 14451 resop = &resp->array[1]; 14452 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14453 resend_rqstp); 14454 /* 14455 * Have a successful lock operation, now update state. 14456 */ 14457 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14458 resop, lop, vp, flk, cr, resend_rqstp); 14459 break; 14460 14461 case NFS4ERR_DENIED: 14462 resop = &resp->array[1]; 14463 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14464 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14465 &recov_state, needrecov, &argsp, &resp, 14466 &tick_delay, &whence, &ep->error, resop, cr, 14467 &did_start_fop, &skip_get_err); 14468 14469 if (retry) { 14470 ASSERT(oop == NULL); 14471 ASSERT(osp == NULL); 14472 ASSERT(lop == NULL); 14473 goto recov_retry; 14474 } 14475 break; 14476 /* 14477 * If the server won't let us reclaim, fall-back to trying to lock 14478 * the file from scratch. Code elsewhere will check the changeinfo 14479 * to ensure the file hasn't been changed. 14480 */ 14481 case NFS4ERR_NO_GRACE: 14482 if (lock_args && lock_args->reclaim == TRUE) { 14483 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14484 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14485 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14486 frc_no_reclaim = 1; 14487 /* clean up before retrying */ 14488 needrecov = 0; 14489 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14490 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14491 &recov_state, op_hint, &did_start_fop, NULL, flk); 14492 goto recov_retry; 14493 } 14494 /* FALLTHROUGH */ 14495 14496 default: 14497 nfs4frlock_results_default(resp, &ep->error); 14498 break; 14499 } 14500 out: 14501 /* 14502 * Process and cleanup from error. Make interrupted unlock 14503 * requests look successful, since they will be handled by the 14504 * client recovery code. 14505 */ 14506 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14507 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14508 lock_args, locku_args, did_start_fop, 14509 skip_get_err, cred_otw, cr); 14510 14511 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14512 (cmd == F_SETLK || cmd == F_SETLKW)) 14513 ep->error = 0; 14514 } 14515 14516 /* 14517 * nfs4_safelock: 14518 * 14519 * Return non-zero if the given lock request can be handled without 14520 * violating the constraints on concurrent mapping and locking. 14521 */ 14522 14523 static int 14524 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14525 { 14526 rnode4_t *rp = VTOR4(vp); 14527 struct vattr va; 14528 int error; 14529 14530 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14531 ASSERT(rp->r_mapcnt >= 0); 14532 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14533 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14534 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14535 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14536 14537 if (rp->r_mapcnt == 0) 14538 return (1); /* always safe if not mapped */ 14539 14540 /* 14541 * If the file is already mapped and there are locks, then they 14542 * should be all safe locks. So adding or removing a lock is safe 14543 * as long as the new request is safe (i.e., whole-file, meaning 14544 * length and starting offset are both zero). 14545 */ 14546 14547 if (bfp->l_start != 0 || bfp->l_len != 0) { 14548 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14549 "cannot lock a memory mapped file unless locking the " 14550 "entire file: start %"PRIx64", len %"PRIx64, 14551 bfp->l_start, bfp->l_len)); 14552 return (0); 14553 } 14554 14555 /* mandatory locking and mapping don't mix */ 14556 va.va_mask = AT_MODE; 14557 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 14558 if (error != 0) { 14559 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14560 "getattr error %d", error)); 14561 return (0); /* treat errors conservatively */ 14562 } 14563 if (MANDLOCK(vp, va.va_mode)) { 14564 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14565 "cannot mandatory lock and mmap a file")); 14566 return (0); 14567 } 14568 14569 return (1); 14570 } 14571 14572 14573 /* 14574 * Register the lock locally within Solaris. 14575 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14576 * recording locks locally. 14577 * 14578 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14579 * are registered locally. 14580 */ 14581 void 14582 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14583 u_offset_t offset) 14584 { 14585 int oldsysid; 14586 int error; 14587 #ifdef DEBUG 14588 char *name; 14589 #endif 14590 14591 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14592 14593 #ifdef DEBUG 14594 name = fn_name(VTOSV(vp)->sv_name); 14595 NFS4_DEBUG(nfs4_client_lock_debug, 14596 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14597 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14598 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14599 flk->l_sysid)); 14600 kmem_free(name, MAXNAMELEN); 14601 #endif 14602 14603 /* register the lock with local locking */ 14604 oldsysid = flk->l_sysid; 14605 flk->l_sysid |= LM_SYSID_CLIENT; 14606 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14607 #ifdef DEBUG 14608 if (error != 0) { 14609 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14610 "nfs4_register_lock_locally: could not register with" 14611 " local locking")); 14612 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14613 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14614 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14615 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14616 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14617 flk->l_type, flk->l_start, flk->l_len)); 14618 (void) reclock(vp, flk, 0, flag, offset, NULL); 14619 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14620 "blocked by pid %d sysid 0x%x type %d " 14621 "off 0x%" PRIx64 " len 0x%" PRIx64, 14622 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14623 flk->l_len)); 14624 } 14625 #endif 14626 flk->l_sysid = oldsysid; 14627 } 14628 14629 /* 14630 * nfs4_lockrelease: 14631 * 14632 * Release any locks on the given vnode that are held by the current 14633 * process. Also removes the lock owner (if one exists) from the rnode's 14634 * list. 14635 */ 14636 static int 14637 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14638 { 14639 flock64_t ld; 14640 int ret, error; 14641 rnode4_t *rp; 14642 nfs4_lock_owner_t *lop; 14643 nfs4_recov_state_t recov_state; 14644 mntinfo4_t *mi; 14645 bool_t possible_orphan = FALSE; 14646 bool_t recovonly; 14647 14648 ASSERT((uintptr_t)vp > KERNELBASE); 14649 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14650 14651 rp = VTOR4(vp); 14652 mi = VTOMI4(vp); 14653 14654 /* 14655 * If we have not locked anything then we can 14656 * just return since we have no work to do. 14657 */ 14658 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14659 return (0); 14660 } 14661 14662 /* 14663 * We need to comprehend that another thread may 14664 * kick off recovery and the lock_owner we have stashed 14665 * in lop might be invalid so we should NOT cache it 14666 * locally! 14667 */ 14668 recov_state.rs_flags = 0; 14669 recov_state.rs_num_retry_despite_err = 0; 14670 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14671 &recovonly); 14672 if (error) { 14673 mutex_enter(&rp->r_statelock); 14674 rp->r_flags |= R4LODANGLERS; 14675 mutex_exit(&rp->r_statelock); 14676 return (error); 14677 } 14678 14679 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14680 14681 /* 14682 * Check if the lock owner might have a lock (request was sent but 14683 * no response was received). Also check if there are any remote 14684 * locks on the file. (In theory we shouldn't have to make this 14685 * second check if there's no lock owner, but for now we'll be 14686 * conservative and do it anyway.) If either condition is true, 14687 * send an unlock for the entire file to the server. 14688 * 14689 * Note that no explicit synchronization is needed here. At worst, 14690 * flk_has_remote_locks() will return a false positive, in which case 14691 * the unlock call wastes time but doesn't harm correctness. 14692 */ 14693 14694 if (lop) { 14695 mutex_enter(&lop->lo_lock); 14696 possible_orphan = lop->lo_pending_rqsts; 14697 mutex_exit(&lop->lo_lock); 14698 lock_owner_rele(lop); 14699 } 14700 14701 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14702 14703 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14704 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14705 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14706 (void *)lop)); 14707 14708 if (possible_orphan || flk_has_remote_locks(vp)) { 14709 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14710 ld.l_whence = 0; /* unlock from start of file */ 14711 ld.l_start = 0; 14712 ld.l_len = 0; /* do entire file */ 14713 14714 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, 14715 cr, NULL); 14716 14717 if (ret != 0) { 14718 /* 14719 * If VOP_FRLOCK fails, make sure we unregister 14720 * local locks before we continue. 14721 */ 14722 ld.l_pid = ttoproc(curthread)->p_pid; 14723 nfs4_register_lock_locally(vp, &ld, flag, offset); 14724 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14725 "nfs4_lockrelease: lock release error on vp" 14726 " %p: error %d.\n", (void *)vp, ret)); 14727 } 14728 } 14729 14730 recov_state.rs_flags = 0; 14731 recov_state.rs_num_retry_despite_err = 0; 14732 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14733 &recovonly); 14734 if (error) { 14735 mutex_enter(&rp->r_statelock); 14736 rp->r_flags |= R4LODANGLERS; 14737 mutex_exit(&rp->r_statelock); 14738 return (error); 14739 } 14740 14741 /* 14742 * So, here we're going to need to retrieve the lock-owner 14743 * again (in case recovery has done a switch-a-roo) and 14744 * remove it because we can. 14745 */ 14746 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14747 14748 if (lop) { 14749 nfs4_rnode_remove_lock_owner(rp, lop); 14750 lock_owner_rele(lop); 14751 } 14752 14753 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14754 return (0); 14755 } 14756 14757 /* 14758 * Wait for 'tick_delay' clock ticks. 14759 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14760 * NOTE: lock_lease_time is in seconds. 14761 * 14762 * XXX For future improvements, should implement a waiting queue scheme. 14763 */ 14764 static int 14765 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14766 { 14767 long milliseconds_delay; 14768 time_t lock_lease_time; 14769 14770 /* wait tick_delay clock ticks or siginteruptus */ 14771 if (delay_sig(*tick_delay)) { 14772 return (EINTR); 14773 } 14774 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14775 "reissue the lock request: blocked for %ld clock ticks: %ld " 14776 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14777 14778 /* get the lease time */ 14779 lock_lease_time = r2lease_time(rp); 14780 14781 /* drv_hztousec converts ticks to microseconds */ 14782 milliseconds_delay = drv_hztousec(*tick_delay) / 1000; 14783 if (milliseconds_delay < lock_lease_time * 1000) { 14784 *tick_delay = 2 * *tick_delay; 14785 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000) 14786 *tick_delay = drv_usectohz(lock_lease_time*1000*1000); 14787 } 14788 return (0); 14789 } 14790 14791 14792 void 14793 nfs4_vnops_init(void) 14794 { 14795 } 14796 14797 void 14798 nfs4_vnops_fini(void) 14799 { 14800 } 14801 14802 /* 14803 * Return a reference to the directory (parent) vnode for a given vnode, 14804 * using the saved pathname information and the directory file handle. The 14805 * caller is responsible for disposing of the reference. 14806 * Returns zero or an errno value. 14807 * 14808 * Caller should set need_start_op to FALSE if it is the recovery 14809 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14810 */ 14811 int 14812 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14813 { 14814 svnode_t *svnp; 14815 vnode_t *dvp = NULL; 14816 servinfo4_t *svp; 14817 nfs4_fname_t *mfname; 14818 int error; 14819 14820 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14821 14822 if (vp->v_flag & VROOT) { 14823 nfs4_sharedfh_t *sfh; 14824 nfs_fh4 fh; 14825 mntinfo4_t *mi; 14826 14827 ASSERT(vp->v_type == VREG); 14828 14829 mi = VTOMI4(vp); 14830 svp = mi->mi_curr_serv; 14831 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14832 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14833 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14834 sfh = sfh4_get(&fh, VTOMI4(vp)); 14835 nfs_rw_exit(&svp->sv_lock); 14836 mfname = mi->mi_fname; 14837 fn_hold(mfname); 14838 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14839 sfh4_rele(&sfh); 14840 14841 if (dvp->v_type == VNON) 14842 dvp->v_type = VDIR; 14843 *dvpp = dvp; 14844 return (0); 14845 } 14846 14847 svnp = VTOSV(vp); 14848 14849 if (svnp == NULL) { 14850 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14851 "shadow node is NULL")); 14852 return (EINVAL); 14853 } 14854 14855 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14856 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14857 "shadow node name or dfh val == NULL")); 14858 return (EINVAL); 14859 } 14860 14861 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14862 (int)need_start_op); 14863 if (error != 0) { 14864 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14865 "nfs4_make_dotdot returned %d", error)); 14866 return (error); 14867 } 14868 if (!dvp) { 14869 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14870 "nfs4_make_dotdot returned a NULL dvp")); 14871 return (EIO); 14872 } 14873 if (dvp->v_type == VNON) 14874 dvp->v_type = VDIR; 14875 ASSERT(dvp->v_type == VDIR); 14876 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14877 mutex_enter(&dvp->v_lock); 14878 dvp->v_flag |= V_XATTRDIR; 14879 mutex_exit(&dvp->v_lock); 14880 } 14881 *dvpp = dvp; 14882 return (0); 14883 } 14884 14885 /* 14886 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14887 * length that fnamep can accept, including the trailing null. 14888 * Returns 0 if okay, returns an errno value if there was a problem. 14889 */ 14890 14891 int 14892 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14893 { 14894 char *fn; 14895 int err = 0; 14896 servinfo4_t *svp; 14897 svnode_t *shvp; 14898 14899 /* 14900 * If the file being opened has VROOT set, then this is 14901 * a "file" mount. sv_name will not be interesting, so 14902 * go back to the servinfo4 to get the original mount 14903 * path and strip off all but the final edge. Otherwise 14904 * just return the name from the shadow vnode. 14905 */ 14906 14907 if (vp->v_flag & VROOT) { 14908 14909 svp = VTOMI4(vp)->mi_curr_serv; 14910 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14911 14912 fn = strrchr(svp->sv_path, '/'); 14913 if (fn == NULL) 14914 err = EINVAL; 14915 else 14916 fn++; 14917 } else { 14918 shvp = VTOSV(vp); 14919 fn = fn_name(shvp->sv_name); 14920 } 14921 14922 if (err == 0) 14923 if (strlen(fn) < maxlen) 14924 (void) strcpy(fnamep, fn); 14925 else 14926 err = ENAMETOOLONG; 14927 14928 if (vp->v_flag & VROOT) 14929 nfs_rw_exit(&svp->sv_lock); 14930 else 14931 kmem_free(fn, MAXNAMELEN); 14932 14933 return (err); 14934 } 14935 14936 /* 14937 * Bookkeeping for a close that doesn't need to go over the wire. 14938 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14939 * it is left at 1. 14940 */ 14941 void 14942 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14943 { 14944 rnode4_t *rp; 14945 mntinfo4_t *mi; 14946 14947 mi = VTOMI4(vp); 14948 rp = VTOR4(vp); 14949 14950 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14951 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14952 ASSERT(nfs_zone() == mi->mi_zone); 14953 ASSERT(mutex_owned(&osp->os_sync_lock)); 14954 ASSERT(*have_lockp); 14955 14956 if (!osp->os_valid || 14957 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14958 return; 14959 } 14960 14961 /* 14962 * This removes the reference obtained at OPEN; ie, 14963 * when the open stream structure was created. 14964 * 14965 * We don't have to worry about calling 'open_stream_rele' 14966 * since we our currently holding a reference to this 14967 * open stream which means the count can not go to 0 with 14968 * this decrement. 14969 */ 14970 ASSERT(osp->os_ref_count >= 2); 14971 osp->os_ref_count--; 14972 osp->os_valid = 0; 14973 mutex_exit(&osp->os_sync_lock); 14974 *have_lockp = 0; 14975 14976 nfs4_dec_state_ref_count(mi); 14977 } 14978 14979 /* 14980 * Close all remaining open streams on the rnode. These open streams 14981 * could be here because: 14982 * - The close attempted at either close or delmap failed 14983 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14984 * - Someone did mknod on a regular file but never opened it 14985 */ 14986 int 14987 nfs4close_all(vnode_t *vp, cred_t *cr) 14988 { 14989 nfs4_open_stream_t *osp; 14990 int error; 14991 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14992 rnode4_t *rp; 14993 14994 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14995 14996 error = 0; 14997 rp = VTOR4(vp); 14998 14999 /* 15000 * At this point, all we know is that the last time 15001 * someone called vn_rele, the count was 1. Since then, 15002 * the vnode could have been re-activated. We want to 15003 * loop through the open streams and close each one, but 15004 * we have to be careful since once we release the rnode 15005 * hash bucket lock, someone else is free to come in and 15006 * re-activate the rnode and add new open streams. The 15007 * strategy is take the rnode hash bucket lock, verify that 15008 * the count is still 1, grab the open stream off the 15009 * head of the list and mark it invalid, then release the 15010 * rnode hash bucket lock and proceed with that open stream. 15011 * This is ok because nfs4close_one() will acquire the proper 15012 * open/create to close/destroy synchronization for open 15013 * streams, and will ensure that if someone has reopened 15014 * the open stream after we've dropped the hash bucket lock 15015 * then we'll just simply return without destroying the 15016 * open stream. 15017 * Repeat until the list is empty. 15018 */ 15019 15020 for (;;) { 15021 15022 /* make sure vnode hasn't been reactivated */ 15023 rw_enter(&rp->r_hashq->r_lock, RW_READER); 15024 mutex_enter(&vp->v_lock); 15025 if (vp->v_count > 1) { 15026 mutex_exit(&vp->v_lock); 15027 rw_exit(&rp->r_hashq->r_lock); 15028 break; 15029 } 15030 /* 15031 * Grabbing r_os_lock before releasing v_lock prevents 15032 * a window where the rnode/open stream could get 15033 * reactivated (and os_force_close set to 0) before we 15034 * had a chance to set os_force_close to 1. 15035 */ 15036 mutex_enter(&rp->r_os_lock); 15037 mutex_exit(&vp->v_lock); 15038 15039 osp = list_head(&rp->r_open_streams); 15040 if (!osp) { 15041 /* nothing left to CLOSE OTW, so return */ 15042 mutex_exit(&rp->r_os_lock); 15043 rw_exit(&rp->r_hashq->r_lock); 15044 break; 15045 } 15046 15047 mutex_enter(&rp->r_statev4_lock); 15048 /* the file can't still be mem mapped */ 15049 ASSERT(rp->r_mapcnt == 0); 15050 if (rp->created_v4) 15051 rp->created_v4 = 0; 15052 mutex_exit(&rp->r_statev4_lock); 15053 15054 /* 15055 * Grab a ref on this open stream; nfs4close_one 15056 * will mark it as invalid 15057 */ 15058 mutex_enter(&osp->os_sync_lock); 15059 osp->os_ref_count++; 15060 osp->os_force_close = 1; 15061 mutex_exit(&osp->os_sync_lock); 15062 mutex_exit(&rp->r_os_lock); 15063 rw_exit(&rp->r_hashq->r_lock); 15064 15065 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 15066 15067 /* Update error if it isn't already non-zero */ 15068 if (error == 0) { 15069 if (e.error) 15070 error = e.error; 15071 else if (e.stat) 15072 error = geterrno4(e.stat); 15073 } 15074 15075 #ifdef DEBUG 15076 nfs4close_all_cnt++; 15077 #endif 15078 /* Release the ref on osp acquired above. */ 15079 open_stream_rele(osp, rp); 15080 15081 /* Proceed to the next open stream, if any */ 15082 } 15083 return (error); 15084 } 15085 15086 /* 15087 * nfs4close_one - close one open stream for a file if needed. 15088 * 15089 * "close_type" indicates which close path this is: 15090 * CLOSE_NORM: close initiated via VOP_CLOSE. 15091 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 15092 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 15093 * the close and release of client state for this open stream 15094 * (unless someone else has the open stream open). 15095 * CLOSE_RESEND: indicates the request is a replay of an earlier request 15096 * (e.g., due to abort because of a signal). 15097 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 15098 * 15099 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 15100 * recovery. Instead, the caller is expected to deal with retries. 15101 * 15102 * The caller can either pass in the osp ('provided_osp') or not. 15103 * 15104 * 'access_bits' represents the access we are closing/downgrading. 15105 * 15106 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 15107 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 15108 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 15109 * 15110 * Errors are returned via the nfs4_error_t. 15111 */ 15112 void 15113 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 15114 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 15115 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 15116 uint_t mmap_flags) 15117 { 15118 nfs4_open_owner_t *oop; 15119 nfs4_open_stream_t *osp = NULL; 15120 int retry = 0; 15121 int num_retries = NFS4_NUM_RECOV_RETRIES; 15122 rnode4_t *rp; 15123 mntinfo4_t *mi; 15124 nfs4_recov_state_t recov_state; 15125 cred_t *cred_otw = NULL; 15126 bool_t recovonly = FALSE; 15127 int isrecov; 15128 int force_close; 15129 int close_failed = 0; 15130 int did_dec_count = 0; 15131 int did_start_op = 0; 15132 int did_force_recovlock = 0; 15133 int did_start_seqid_sync = 0; 15134 int have_sync_lock = 0; 15135 15136 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15137 15138 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 15139 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 15140 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 15141 len, maxprot, mmap_flags, access_bits)); 15142 15143 nfs4_error_zinit(ep); 15144 rp = VTOR4(vp); 15145 mi = VTOMI4(vp); 15146 isrecov = (close_type == CLOSE_RESEND || 15147 close_type == CLOSE_AFTER_RESEND); 15148 15149 /* 15150 * First get the open owner. 15151 */ 15152 if (!provided_osp) { 15153 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 15154 } else { 15155 oop = provided_osp->os_open_owner; 15156 ASSERT(oop != NULL); 15157 open_owner_hold(oop); 15158 } 15159 15160 if (!oop) { 15161 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15162 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 15163 "close type %d", (void *)rp, (void *)mi, (void *)cr, 15164 (void *)provided_osp, close_type)); 15165 ep->error = EIO; 15166 goto out; 15167 } 15168 15169 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 15170 recov_retry: 15171 osp = NULL; 15172 close_failed = 0; 15173 force_close = (close_type == CLOSE_FORCE); 15174 retry = 0; 15175 did_start_op = 0; 15176 did_force_recovlock = 0; 15177 did_start_seqid_sync = 0; 15178 have_sync_lock = 0; 15179 recovonly = FALSE; 15180 recov_state.rs_flags = 0; 15181 recov_state.rs_num_retry_despite_err = 0; 15182 15183 /* 15184 * Second synchronize with recovery. 15185 */ 15186 if (!isrecov) { 15187 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 15188 &recov_state, &recovonly); 15189 if (!ep->error) { 15190 did_start_op = 1; 15191 } else { 15192 close_failed = 1; 15193 /* 15194 * If we couldn't get start_fop, but have to 15195 * cleanup state, then at least acquire the 15196 * mi_recovlock so we can synchronize with 15197 * recovery. 15198 */ 15199 if (close_type == CLOSE_FORCE) { 15200 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 15201 RW_READER, FALSE); 15202 did_force_recovlock = 1; 15203 } else 15204 goto out; 15205 } 15206 } 15207 15208 /* 15209 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15210 * set 'recovonly' to TRUE since most likely this is due to 15211 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15212 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15213 * to retry, causing us to loop until recovery finishes. Plus we 15214 * don't need protection over the open seqid since we're not going 15215 * OTW, hence don't need to use the seqid. 15216 */ 15217 if (recovonly == FALSE) { 15218 /* need to grab the open owner sync before 'os_sync_lock' */ 15219 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15220 if (ep->error == EAGAIN) { 15221 ASSERT(!isrecov); 15222 if (did_start_op) 15223 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15224 &recov_state, TRUE); 15225 if (did_force_recovlock) 15226 nfs_rw_exit(&mi->mi_recovlock); 15227 goto recov_retry; 15228 } 15229 did_start_seqid_sync = 1; 15230 } 15231 15232 /* 15233 * Third get an open stream and acquire 'os_sync_lock' to 15234 * sychronize the opening/creating of an open stream with the 15235 * closing/destroying of an open stream. 15236 */ 15237 if (!provided_osp) { 15238 /* returns with 'os_sync_lock' held */ 15239 osp = find_open_stream(oop, rp); 15240 if (!osp) { 15241 ep->error = EIO; 15242 goto out; 15243 } 15244 } else { 15245 osp = provided_osp; 15246 open_stream_hold(osp); 15247 mutex_enter(&osp->os_sync_lock); 15248 } 15249 have_sync_lock = 1; 15250 15251 ASSERT(oop == osp->os_open_owner); 15252 15253 /* 15254 * Fourth, do any special pre-OTW CLOSE processing 15255 * based on the specific close type. 15256 */ 15257 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15258 !did_dec_count) { 15259 ASSERT(osp->os_open_ref_count > 0); 15260 osp->os_open_ref_count--; 15261 did_dec_count = 1; 15262 if (osp->os_open_ref_count == 0) 15263 osp->os_final_close = 1; 15264 } 15265 15266 if (close_type == CLOSE_FORCE) { 15267 /* see if somebody reopened the open stream. */ 15268 if (!osp->os_force_close) { 15269 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15270 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15271 "was reopened, vp %p", (void *)osp, (void *)vp)); 15272 ep->error = 0; 15273 ep->stat = NFS4_OK; 15274 goto out; 15275 } 15276 15277 if (!osp->os_final_close && !did_dec_count) { 15278 osp->os_open_ref_count--; 15279 did_dec_count = 1; 15280 } 15281 15282 /* 15283 * We can't depend on os_open_ref_count being 0 due to the 15284 * way executables are opened (VN_RELE to match a VOP_OPEN). 15285 */ 15286 #ifdef NOTYET 15287 ASSERT(osp->os_open_ref_count == 0); 15288 #endif 15289 if (osp->os_open_ref_count != 0) { 15290 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15291 "nfs4close_one: should panic here on an " 15292 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15293 "since this is probably the exec problem.")); 15294 15295 osp->os_open_ref_count = 0; 15296 } 15297 15298 /* 15299 * There is the possibility that nfs4close_one() 15300 * for close_type == CLOSE_DELMAP couldn't find the 15301 * open stream, thus couldn't decrement its os_mapcnt; 15302 * therefore we can't use this ASSERT yet. 15303 */ 15304 #ifdef NOTYET 15305 ASSERT(osp->os_mapcnt == 0); 15306 #endif 15307 osp->os_mapcnt = 0; 15308 } 15309 15310 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15311 ASSERT(osp->os_mapcnt >= btopr(len)); 15312 15313 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15314 osp->os_mmap_write -= btopr(len); 15315 if (maxprot & PROT_READ) 15316 osp->os_mmap_read -= btopr(len); 15317 if (maxprot & PROT_EXEC) 15318 osp->os_mmap_read -= btopr(len); 15319 /* mirror the PROT_NONE check in nfs4_addmap() */ 15320 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15321 !(maxprot & PROT_EXEC)) 15322 osp->os_mmap_read -= btopr(len); 15323 osp->os_mapcnt -= btopr(len); 15324 did_dec_count = 1; 15325 } 15326 15327 if (recovonly) { 15328 nfs4_lost_rqst_t lost_rqst; 15329 15330 /* request should not already be in recovery queue */ 15331 ASSERT(lrp == NULL); 15332 nfs4_error_init(ep, EINTR); 15333 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15334 osp, cred_otw, vp); 15335 mutex_exit(&osp->os_sync_lock); 15336 have_sync_lock = 0; 15337 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15338 lost_rqst.lr_op == OP_CLOSE ? 15339 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL); 15340 close_failed = 1; 15341 force_close = 0; 15342 goto close_cleanup; 15343 } 15344 15345 /* 15346 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15347 * we stopped operating on the open owner's <old oo_name, old seqid> 15348 * space, which means we stopped operating on the open stream 15349 * too. So don't go OTW (as the seqid is likely bad, and the 15350 * stateid could be stale, potentially triggering a false 15351 * setclientid), and just clean up the client's internal state. 15352 */ 15353 if (osp->os_orig_oo_name != oop->oo_name) { 15354 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15355 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15356 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15357 "oo_name %" PRIx64")", 15358 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15359 oop->oo_name)); 15360 close_failed = 1; 15361 } 15362 15363 /* If the file failed recovery, just quit. */ 15364 mutex_enter(&rp->r_statelock); 15365 if (rp->r_flags & R4RECOVERR) { 15366 close_failed = 1; 15367 } 15368 mutex_exit(&rp->r_statelock); 15369 15370 /* 15371 * If the force close path failed to obtain start_fop 15372 * then skip the OTW close and just remove the state. 15373 */ 15374 if (close_failed) 15375 goto close_cleanup; 15376 15377 /* 15378 * Fifth, check to see if there are still mapped pages or other 15379 * opens using this open stream. If there are then we can't 15380 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15381 */ 15382 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15383 nfs4_lost_rqst_t new_lost_rqst; 15384 bool_t needrecov = FALSE; 15385 cred_t *odg_cred_otw = NULL; 15386 seqid4 open_dg_seqid = 0; 15387 15388 if (osp->os_delegation) { 15389 /* 15390 * If this open stream was never OPENed OTW then we 15391 * surely can't DOWNGRADE it (especially since the 15392 * osp->open_stateid is really a delegation stateid 15393 * when os_delegation is 1). 15394 */ 15395 if (access_bits & FREAD) 15396 osp->os_share_acc_read--; 15397 if (access_bits & FWRITE) 15398 osp->os_share_acc_write--; 15399 osp->os_share_deny_none--; 15400 nfs4_error_zinit(ep); 15401 goto out; 15402 } 15403 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15404 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15405 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15406 if (needrecov && !isrecov) { 15407 bool_t abort; 15408 nfs4_bseqid_entry_t *bsep = NULL; 15409 15410 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15411 bsep = nfs4_create_bseqid_entry(oop, NULL, 15412 vp, 0, 15413 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15414 open_dg_seqid); 15415 15416 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15417 oop, osp, odg_cred_otw, vp, access_bits, 0); 15418 mutex_exit(&osp->os_sync_lock); 15419 have_sync_lock = 0; 15420 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15421 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15422 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15423 bsep, NULL, NULL); 15424 if (odg_cred_otw) 15425 crfree(odg_cred_otw); 15426 if (bsep) 15427 kmem_free(bsep, sizeof (*bsep)); 15428 15429 if (abort == TRUE) 15430 goto out; 15431 15432 if (did_start_seqid_sync) { 15433 nfs4_end_open_seqid_sync(oop); 15434 did_start_seqid_sync = 0; 15435 } 15436 open_stream_rele(osp, rp); 15437 15438 if (did_start_op) 15439 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15440 &recov_state, FALSE); 15441 if (did_force_recovlock) 15442 nfs_rw_exit(&mi->mi_recovlock); 15443 15444 goto recov_retry; 15445 } else { 15446 if (odg_cred_otw) 15447 crfree(odg_cred_otw); 15448 } 15449 goto out; 15450 } 15451 15452 /* 15453 * If this open stream was created as the results of an open 15454 * while holding a delegation, then just release it; no need 15455 * to do an OTW close. Otherwise do a "normal" OTW close. 15456 */ 15457 if (osp->os_delegation) { 15458 nfs4close_notw(vp, osp, &have_sync_lock); 15459 nfs4_error_zinit(ep); 15460 goto out; 15461 } 15462 15463 /* 15464 * If this stream is not valid, we're done. 15465 */ 15466 if (!osp->os_valid) { 15467 nfs4_error_zinit(ep); 15468 goto out; 15469 } 15470 15471 /* 15472 * Last open or mmap ref has vanished, need to do an OTW close. 15473 * First check to see if a close is still necessary. 15474 */ 15475 if (osp->os_failed_reopen) { 15476 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15477 "don't close OTW osp %p since reopen failed.", 15478 (void *)osp)); 15479 /* 15480 * Reopen of the open stream failed, hence the 15481 * stateid of the open stream is invalid/stale, and 15482 * sending this OTW would incorrectly cause another 15483 * round of recovery. In this case, we need to set 15484 * the 'os_valid' bit to 0 so another thread doesn't 15485 * come in and re-open this open stream before 15486 * this "closing" thread cleans up state (decrementing 15487 * the nfs4_server_t's state_ref_count and decrementing 15488 * the os_ref_count). 15489 */ 15490 osp->os_valid = 0; 15491 /* 15492 * This removes the reference obtained at OPEN; ie, 15493 * when the open stream structure was created. 15494 * 15495 * We don't have to worry about calling 'open_stream_rele' 15496 * since we our currently holding a reference to this 15497 * open stream which means the count can not go to 0 with 15498 * this decrement. 15499 */ 15500 ASSERT(osp->os_ref_count >= 2); 15501 osp->os_ref_count--; 15502 nfs4_error_zinit(ep); 15503 close_failed = 0; 15504 goto close_cleanup; 15505 } 15506 15507 ASSERT(osp->os_ref_count > 1); 15508 15509 /* 15510 * Sixth, try the CLOSE OTW. 15511 */ 15512 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15513 close_type, ep, &have_sync_lock); 15514 15515 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15516 /* 15517 * Let the recovery thread be responsible for 15518 * removing the state for CLOSE. 15519 */ 15520 close_failed = 1; 15521 force_close = 0; 15522 retry = 0; 15523 } 15524 15525 /* See if we need to retry with a different cred */ 15526 if ((ep->error == EACCES || 15527 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15528 cred_otw != cr) { 15529 crfree(cred_otw); 15530 cred_otw = cr; 15531 crhold(cred_otw); 15532 retry = 1; 15533 } 15534 15535 if (ep->error || ep->stat) 15536 close_failed = 1; 15537 15538 if (retry && !isrecov && num_retries-- > 0) { 15539 if (have_sync_lock) { 15540 mutex_exit(&osp->os_sync_lock); 15541 have_sync_lock = 0; 15542 } 15543 if (did_start_seqid_sync) { 15544 nfs4_end_open_seqid_sync(oop); 15545 did_start_seqid_sync = 0; 15546 } 15547 open_stream_rele(osp, rp); 15548 15549 if (did_start_op) 15550 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15551 &recov_state, FALSE); 15552 if (did_force_recovlock) 15553 nfs_rw_exit(&mi->mi_recovlock); 15554 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15555 "nfs4close_one: need to retry the close " 15556 "operation")); 15557 goto recov_retry; 15558 } 15559 close_cleanup: 15560 /* 15561 * Seventh and lastly, process our results. 15562 */ 15563 if (close_failed && force_close) { 15564 /* 15565 * It's ok to drop and regrab the 'os_sync_lock' since 15566 * nfs4close_notw() will recheck to make sure the 15567 * "close"/removal of state should happen. 15568 */ 15569 if (!have_sync_lock) { 15570 mutex_enter(&osp->os_sync_lock); 15571 have_sync_lock = 1; 15572 } 15573 /* 15574 * This is last call, remove the ref on the open 15575 * stream created by open and clean everything up. 15576 */ 15577 osp->os_pending_close = 0; 15578 nfs4close_notw(vp, osp, &have_sync_lock); 15579 nfs4_error_zinit(ep); 15580 } 15581 15582 if (!close_failed) { 15583 if (have_sync_lock) { 15584 osp->os_pending_close = 0; 15585 mutex_exit(&osp->os_sync_lock); 15586 have_sync_lock = 0; 15587 } else { 15588 mutex_enter(&osp->os_sync_lock); 15589 osp->os_pending_close = 0; 15590 mutex_exit(&osp->os_sync_lock); 15591 } 15592 if (did_start_op && recov_state.rs_sp != NULL) { 15593 mutex_enter(&recov_state.rs_sp->s_lock); 15594 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15595 mutex_exit(&recov_state.rs_sp->s_lock); 15596 } else { 15597 nfs4_dec_state_ref_count(mi); 15598 } 15599 nfs4_error_zinit(ep); 15600 } 15601 15602 out: 15603 if (have_sync_lock) 15604 mutex_exit(&osp->os_sync_lock); 15605 if (did_start_op) 15606 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15607 recovonly ? TRUE : FALSE); 15608 if (did_force_recovlock) 15609 nfs_rw_exit(&mi->mi_recovlock); 15610 if (cred_otw) 15611 crfree(cred_otw); 15612 if (osp) 15613 open_stream_rele(osp, rp); 15614 if (oop) { 15615 if (did_start_seqid_sync) 15616 nfs4_end_open_seqid_sync(oop); 15617 open_owner_rele(oop); 15618 } 15619 } 15620 15621 /* 15622 * Convert information returned by the server in the LOCK4denied 15623 * structure to the form required by fcntl. 15624 */ 15625 static void 15626 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15627 { 15628 nfs4_lo_name_t *lo; 15629 15630 #ifdef DEBUG 15631 if (denied_to_flk_debug) { 15632 lockt_denied_debug = lockt_denied; 15633 debug_enter("lockt_denied"); 15634 } 15635 #endif 15636 15637 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15638 flk->l_whence = 0; /* aka SEEK_SET */ 15639 flk->l_start = lockt_denied->offset; 15640 flk->l_len = lockt_denied->length; 15641 15642 /* 15643 * If the blocking clientid matches our client id, then we can 15644 * interpret the lockowner (since we built it). If not, then 15645 * fabricate a sysid and pid. Note that the l_sysid field 15646 * in *flk already has the local sysid. 15647 */ 15648 15649 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15650 15651 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15652 lo = (nfs4_lo_name_t *) 15653 lockt_denied->owner.owner_val; 15654 15655 flk->l_pid = lo->ln_pid; 15656 } else { 15657 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15658 "denied_to_flk: bad lock owner length\n")); 15659 15660 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15661 } 15662 } else { 15663 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15664 "denied_to_flk: foreign clientid\n")); 15665 15666 /* 15667 * Construct a new sysid which should be different from 15668 * sysids of other systems. 15669 */ 15670 15671 flk->l_sysid++; 15672 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15673 } 15674 } 15675 15676 static pid_t 15677 lo_to_pid(lock_owner4 *lop) 15678 { 15679 pid_t pid = 0; 15680 uchar_t *cp; 15681 int i; 15682 15683 cp = (uchar_t *)&lop->clientid; 15684 15685 for (i = 0; i < sizeof (lop->clientid); i++) 15686 pid += (pid_t)*cp++; 15687 15688 cp = (uchar_t *)lop->owner_val; 15689 15690 for (i = 0; i < lop->owner_len; i++) 15691 pid += (pid_t)*cp++; 15692 15693 return (pid); 15694 } 15695 15696 /* 15697 * Given a lock pointer, returns the length of that lock. 15698 * "end" is the last locked offset the "l_len" covers from 15699 * the start of the lock. 15700 */ 15701 static off64_t 15702 lock_to_end(flock64_t *lock) 15703 { 15704 off64_t lock_end; 15705 15706 if (lock->l_len == 0) 15707 lock_end = (off64_t)MAXEND; 15708 else 15709 lock_end = lock->l_start + lock->l_len - 1; 15710 15711 return (lock_end); 15712 } 15713 15714 /* 15715 * Given the end of a lock, it will return you the length "l_len" for that lock. 15716 */ 15717 static off64_t 15718 end_to_len(off64_t start, off64_t end) 15719 { 15720 off64_t lock_len; 15721 15722 ASSERT(end >= start); 15723 if (end == MAXEND) 15724 lock_len = 0; 15725 else 15726 lock_len = end - start + 1; 15727 15728 return (lock_len); 15729 } 15730 15731 /* 15732 * On given end for a lock it determines if it is the last locked offset 15733 * or not, if so keeps it as is, else adds one to return the length for 15734 * valid start. 15735 */ 15736 static off64_t 15737 start_check(off64_t x) 15738 { 15739 if (x == MAXEND) 15740 return (x); 15741 else 15742 return (x + 1); 15743 } 15744 15745 /* 15746 * See if these two locks overlap, and if so return 1; 15747 * otherwise, return 0. 15748 */ 15749 static int 15750 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15751 { 15752 off64_t llfp_end, curfp_end; 15753 15754 llfp_end = lock_to_end(llfp); 15755 curfp_end = lock_to_end(curfp); 15756 15757 if (((llfp_end >= curfp->l_start) && 15758 (llfp->l_start <= curfp->l_start)) || 15759 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15760 return (1); 15761 return (0); 15762 } 15763 15764 /* 15765 * Determine what the intersecting lock region is, and add that to the 15766 * 'nl_llpp' locklist in increasing order (by l_start). 15767 */ 15768 static void 15769 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15770 locklist_t **nl_llpp, vnode_t *vp) 15771 { 15772 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15773 off64_t lost_flp_end, local_flp_end, len, start; 15774 15775 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15776 15777 if (!locks_intersect(lost_flp, local_flp)) 15778 return; 15779 15780 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15781 "locks intersect")); 15782 15783 lost_flp_end = lock_to_end(lost_flp); 15784 local_flp_end = lock_to_end(local_flp); 15785 15786 /* Find the starting point of the intersecting region */ 15787 if (local_flp->l_start > lost_flp->l_start) 15788 start = local_flp->l_start; 15789 else 15790 start = lost_flp->l_start; 15791 15792 /* Find the lenght of the intersecting region */ 15793 if (lost_flp_end < local_flp_end) 15794 len = end_to_len(start, lost_flp_end); 15795 else 15796 len = end_to_len(start, local_flp_end); 15797 15798 /* 15799 * Prepare the flock structure for the intersection found and insert 15800 * it into the new list in increasing l_start order. This list contains 15801 * intersections of locks registered by the client with the local host 15802 * and the lost lock. 15803 * The lock type of this lock is the same as that of the local_flp. 15804 */ 15805 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15806 intersect_llp->ll_flock.l_start = start; 15807 intersect_llp->ll_flock.l_len = len; 15808 intersect_llp->ll_flock.l_type = local_flp->l_type; 15809 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15810 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15811 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15812 intersect_llp->ll_vp = vp; 15813 15814 tmp_fllp = *nl_llpp; 15815 cur_fllp = NULL; 15816 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15817 intersect_llp->ll_flock.l_start) { 15818 cur_fllp = tmp_fllp; 15819 tmp_fllp = tmp_fllp->ll_next; 15820 } 15821 if (cur_fllp == NULL) { 15822 /* first on the list */ 15823 intersect_llp->ll_next = *nl_llpp; 15824 *nl_llpp = intersect_llp; 15825 } else { 15826 intersect_llp->ll_next = cur_fllp->ll_next; 15827 cur_fllp->ll_next = intersect_llp; 15828 } 15829 15830 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15831 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15832 intersect_llp->ll_flock.l_start, 15833 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15834 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15835 } 15836 15837 /* 15838 * Our local locking current state is potentially different than 15839 * what the NFSv4 server thinks we have due to a lost lock that was 15840 * resent and then received. We need to reset our "NFSv4" locking 15841 * state to match the current local locking state for this pid since 15842 * that is what the user/application sees as what the world is. 15843 * 15844 * We cannot afford to drop the open/lock seqid sync since then we can 15845 * get confused about what the current local locking state "is" versus 15846 * "was". 15847 * 15848 * If we are unable to fix up the locks, we send SIGLOST to the affected 15849 * process. This is not done if the filesystem has been forcibly 15850 * unmounted, in case the process has already exited and a new process 15851 * exists with the same pid. 15852 */ 15853 static void 15854 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15855 nfs4_lock_owner_t *lop) 15856 { 15857 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15858 mntinfo4_t *mi = VTOMI4(vp); 15859 const int cmd = F_SETLK; 15860 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15861 flock64_t ul_fl; 15862 15863 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15864 "nfs4_reinstitute_local_lock_state")); 15865 15866 /* 15867 * Find active locks for this vp from the local locking code. 15868 * Scan through this list and find out the locks that intersect with 15869 * the lost lock. Once we find the lock that intersects, add the 15870 * intersection area as a new lock to a new list "ri_llp". The lock 15871 * type of the intersection region lock added to ri_llp is the same 15872 * as that found in the active lock list, "list". The intersecting 15873 * region locks are added to ri_llp in increasing l_start order. 15874 */ 15875 ASSERT(nfs_zone() == mi->mi_zone); 15876 15877 locks = flk_active_locks_for_vp(vp); 15878 ri_llp = NULL; 15879 15880 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15881 ASSERT(llp->ll_vp == vp); 15882 /* 15883 * Pick locks that belong to this pid/lockowner 15884 */ 15885 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15886 continue; 15887 15888 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15889 } 15890 15891 /* 15892 * Now we have the list of intersections with the lost lock. These are 15893 * the locks that were/are active before the server replied to the 15894 * last/lost lock. Issue these locks to the server here. Playing these 15895 * locks to the server will re-establish aur current local locking state 15896 * with the v4 server. 15897 * If we get an error, send SIGLOST to the application for that lock. 15898 */ 15899 15900 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15901 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15902 "nfs4_reinstitute_local_lock_state: need to issue " 15903 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15904 llp->ll_flock.l_start, 15905 llp->ll_flock.l_start + llp->ll_flock.l_len, 15906 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15907 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15908 /* 15909 * No need to relock what we already have 15910 */ 15911 if (llp->ll_flock.l_type == lost_flp->l_type) 15912 continue; 15913 15914 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15915 } 15916 15917 /* 15918 * Now keeping the start of the lost lock as our reference parse the 15919 * newly created ri_llp locklist to find the ranges that we have locked 15920 * with the v4 server but not in the current local locking. We need 15921 * to unlock these ranges. 15922 * These ranges can also be reffered to as those ranges, where the lost 15923 * lock does not overlap with the locks in the ri_llp but are locked 15924 * since the server replied to the lost lock. 15925 */ 15926 cur_start = lost_flp->l_start; 15927 lost_flp_end = lock_to_end(lost_flp); 15928 15929 ul_fl.l_type = F_UNLCK; 15930 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15931 ul_fl.l_sysid = lost_flp->l_sysid; 15932 ul_fl.l_pid = lost_flp->l_pid; 15933 15934 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15935 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15936 15937 if (llp->ll_flock.l_start <= cur_start) { 15938 cur_start = start_check(llp_ll_flock_end); 15939 continue; 15940 } 15941 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15942 "nfs4_reinstitute_local_lock_state: " 15943 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15944 cur_start, llp->ll_flock.l_start)); 15945 15946 ul_fl.l_start = cur_start; 15947 ul_fl.l_len = end_to_len(cur_start, 15948 (llp->ll_flock.l_start - 1)); 15949 15950 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15951 cur_start = start_check(llp_ll_flock_end); 15952 } 15953 15954 /* 15955 * In the case where the lost lock ends after all intersecting locks, 15956 * unlock the last part of the lost lock range. 15957 */ 15958 if (cur_start != start_check(lost_flp_end)) { 15959 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15960 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15961 "lost lock region [%"PRIx64" - %"PRIx64"]", 15962 cur_start, lost_flp->l_start + lost_flp->l_len)); 15963 15964 ul_fl.l_start = cur_start; 15965 /* 15966 * Is it an to-EOF lock? if so unlock till the end 15967 */ 15968 if (lost_flp->l_len == 0) 15969 ul_fl.l_len = 0; 15970 else 15971 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15972 15973 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15974 } 15975 15976 if (locks != NULL) 15977 flk_free_locklist(locks); 15978 15979 /* Free up our newly created locklist */ 15980 for (llp = ri_llp; llp != NULL; ) { 15981 tmp_llp = llp->ll_next; 15982 kmem_free(llp, sizeof (locklist_t)); 15983 llp = tmp_llp; 15984 } 15985 15986 /* 15987 * Now return back to the original calling nfs4frlock() 15988 * and let us naturally drop our seqid syncs. 15989 */ 15990 } 15991 15992 /* 15993 * Create a lost state record for the given lock reinstantiation request 15994 * and push it onto the lost state queue. 15995 */ 15996 static void 15997 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15998 nfs4_lock_owner_t *lop) 15999 { 16000 nfs4_lost_rqst_t req; 16001 nfs_lock_type4 locktype; 16002 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 16003 16004 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 16005 16006 locktype = flk_to_locktype(cmd, flk->l_type); 16007 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 16008 NULL, NULL, lop, flk, &req, cr, vp); 16009 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 16010 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 16011 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 16012 NULL, NULL, NULL); 16013 } 16014