1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* 26 * Copyright 2012 Nexenta Systems, Inc. All rights reserved. 27 */ 28 29 /* 30 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 31 * All Rights Reserved 32 */ 33 34 /* 35 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 36 */ 37 38 /* 39 * Copyright (c) 2014, STRATO AG. All rights reserved. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/types.h> 44 #include <sys/systm.h> 45 #include <sys/cred.h> 46 #include <sys/time.h> 47 #include <sys/vnode.h> 48 #include <sys/vfs.h> 49 #include <sys/vfs_opreg.h> 50 #include <sys/file.h> 51 #include <sys/filio.h> 52 #include <sys/uio.h> 53 #include <sys/buf.h> 54 #include <sys/mman.h> 55 #include <sys/pathname.h> 56 #include <sys/dirent.h> 57 #include <sys/debug.h> 58 #include <sys/vmsystm.h> 59 #include <sys/fcntl.h> 60 #include <sys/flock.h> 61 #include <sys/swap.h> 62 #include <sys/errno.h> 63 #include <sys/strsubr.h> 64 #include <sys/sysmacros.h> 65 #include <sys/kmem.h> 66 #include <sys/cmn_err.h> 67 #include <sys/pathconf.h> 68 #include <sys/utsname.h> 69 #include <sys/dnlc.h> 70 #include <sys/acl.h> 71 #include <sys/systeminfo.h> 72 #include <sys/policy.h> 73 #include <sys/sdt.h> 74 #include <sys/list.h> 75 #include <sys/stat.h> 76 #include <sys/zone.h> 77 78 #include <rpc/types.h> 79 #include <rpc/auth.h> 80 #include <rpc/clnt.h> 81 82 #include <nfs/nfs.h> 83 #include <nfs/nfs_clnt.h> 84 #include <nfs/nfs_acl.h> 85 #include <nfs/lm.h> 86 #include <nfs/nfs4.h> 87 #include <nfs/nfs4_kprot.h> 88 #include <nfs/rnode4.h> 89 #include <nfs/nfs4_clnt.h> 90 91 #include <vm/hat.h> 92 #include <vm/as.h> 93 #include <vm/page.h> 94 #include <vm/pvn.h> 95 #include <vm/seg.h> 96 #include <vm/seg_map.h> 97 #include <vm/seg_kpm.h> 98 #include <vm/seg_vn.h> 99 100 #include <fs/fs_subr.h> 101 102 #include <sys/ddi.h> 103 #include <sys/int_fmtio.h> 104 #include <sys/fs/autofs.h> 105 106 typedef struct { 107 nfs4_ga_res_t *di_garp; 108 cred_t *di_cred; 109 hrtime_t di_time_call; 110 } dirattr_info_t; 111 112 typedef enum nfs4_acl_op { 113 NFS4_ACL_GET, 114 NFS4_ACL_SET 115 } nfs4_acl_op_t; 116 117 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi); 118 119 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 120 char *, dirattr_info_t *); 121 122 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 123 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 124 nfs4_error_t *, int *); 125 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 126 cred_t *); 127 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 128 stable_how4 *); 129 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 130 cred_t *, bool_t, struct uio *); 131 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 132 vsecattr_t *); 133 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 134 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 135 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 136 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 137 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 138 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 139 int, vnode_t **, cred_t *); 140 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 141 cred_t *, int, int, enum createmode4, int); 142 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 143 caller_context_t *); 144 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 145 vnode_t *, char *, cred_t *, nfsstat4 *); 146 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 147 vnode_t *, char *, cred_t *, nfsstat4 *); 148 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 149 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 150 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 151 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 152 page_t *[], size_t, struct seg *, caddr_t, 153 enum seg_rw, cred_t *); 154 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 155 cred_t *); 156 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 157 int, cred_t *); 158 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 159 int, cred_t *); 160 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 161 static void nfs4_set_mod(vnode_t *); 162 static void nfs4_get_commit(vnode_t *); 163 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 164 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 165 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 166 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 167 cred_t *); 168 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 169 cred_t *); 170 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 171 hrtime_t, vnode_t *, cred_t *); 172 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 173 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 174 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 175 u_offset_t); 176 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 177 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 178 static cred_t *state_to_cred(nfs4_open_stream_t *); 179 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 180 static pid_t lo_to_pid(lock_owner4 *); 181 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 182 cred_t *, nfs4_lock_owner_t *); 183 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 184 nfs4_lock_owner_t *); 185 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **); 186 static void nfs4_delmap_callback(struct as *, void *, uint_t); 187 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 188 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 189 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 190 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 191 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 192 uid_t, gid_t, int); 193 194 /* 195 * Routines that implement the setting of v4 args for the misc. ops 196 */ 197 static void nfs4args_lock_free(nfs_argop4 *); 198 static void nfs4args_lockt_free(nfs_argop4 *); 199 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 200 int, rnode4_t *, cred_t *, bitmap4, int *, 201 nfs4_stateid_types_t *); 202 static void nfs4args_setattr_free(nfs_argop4 *); 203 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 204 bitmap4); 205 static void nfs4args_verify_free(nfs_argop4 *); 206 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 207 WRITE4args **, nfs4_stateid_types_t *); 208 209 /* 210 * These are the vnode ops functions that implement the vnode interface to 211 * the networked file system. See more comments below at nfs4_vnodeops. 212 */ 213 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *); 214 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *, 215 caller_context_t *); 216 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 217 caller_context_t *); 218 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 219 caller_context_t *); 220 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *, 221 caller_context_t *); 222 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 223 caller_context_t *); 224 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *); 225 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *, 226 caller_context_t *); 227 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *); 228 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 229 int, vnode_t **, cred_t *, int, caller_context_t *, 230 vsecattr_t *); 231 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *, 232 int); 233 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *, 234 caller_context_t *, int); 235 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 236 caller_context_t *, int); 237 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **, 238 cred_t *, caller_context_t *, int, vsecattr_t *); 239 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *, 240 caller_context_t *, int); 241 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 242 cred_t *, caller_context_t *, int); 243 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *, 244 caller_context_t *, int); 245 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *); 246 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 247 page_t *[], size_t, struct seg *, caddr_t, 248 enum seg_rw, cred_t *, caller_context_t *); 249 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *, 250 caller_context_t *); 251 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t, 252 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 253 static int nfs4_addmap(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_cmp(vnode_t *, vnode_t *, caller_context_t *); 256 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 257 struct flk_callback *, cred_t *, caller_context_t *); 258 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 259 cred_t *, caller_context_t *); 260 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 261 uint_t, uint_t, uint_t, cred_t *, caller_context_t *); 262 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 263 cred_t *, caller_context_t *); 264 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *, 265 caller_context_t *); 266 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 267 caller_context_t *); 268 /* 269 * These vnode ops are required to be called from outside this source file, 270 * e.g. by ephemeral mount stub vnode ops, and so may not be declared 271 * as static. 272 */ 273 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *, 274 caller_context_t *); 275 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *); 276 int nfs4_lookup(vnode_t *, char *, vnode_t **, 277 struct pathname *, int, vnode_t *, cred_t *, 278 caller_context_t *, int *, pathname_t *); 279 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *); 280 int nfs4_rwlock(vnode_t *, int, caller_context_t *); 281 void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 282 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *); 283 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *, 284 caller_context_t *); 285 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 286 caller_context_t *); 287 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *, 288 caller_context_t *); 289 290 /* 291 * Used for nfs4_commit_vp() to indicate if we should 292 * wait on pending writes. 293 */ 294 #define NFS4_WRITE_NOWAIT 0 295 #define NFS4_WRITE_WAIT 1 296 297 /* 298 * Error flags used to pass information about certain special errors 299 * which need to be handled specially. 300 */ 301 #define NFS_EOF -98 302 #define NFS_VERF_MISMATCH -97 303 304 /* 305 * Flags used to differentiate between which operation drove the 306 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 307 */ 308 #define NFS4_CLOSE_OP 0x1 309 #define NFS4_DELMAP_OP 0x2 310 #define NFS4_INACTIVE_OP 0x3 311 312 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 313 314 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 315 #define ALIGN64(x, ptr, sz) \ 316 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 317 if (x) { \ 318 x = sizeof (uint64_t) - (x); \ 319 sz -= (x); \ 320 ptr += (x); \ 321 } 322 323 #ifdef DEBUG 324 int nfs4_client_attr_debug = 0; 325 int nfs4_client_state_debug = 0; 326 int nfs4_client_shadow_debug = 0; 327 int nfs4_client_lock_debug = 0; 328 int nfs4_seqid_sync = 0; 329 int nfs4_client_map_debug = 0; 330 static int nfs4_pageio_debug = 0; 331 int nfs4_client_inactive_debug = 0; 332 int nfs4_client_recov_debug = 0; 333 int nfs4_client_failover_debug = 0; 334 int nfs4_client_call_debug = 0; 335 int nfs4_client_lookup_debug = 0; 336 int nfs4_client_zone_debug = 0; 337 int nfs4_lost_rqst_debug = 0; 338 int nfs4_rdattrerr_debug = 0; 339 int nfs4_open_stream_debug = 0; 340 341 int nfs4read_error_inject; 342 343 static int nfs4_create_misses = 0; 344 345 static int nfs4_readdir_cache_shorts = 0; 346 static int nfs4_readdir_readahead = 0; 347 348 static int nfs4_bio_do_stop = 0; 349 350 static int nfs4_lostpage = 0; /* number of times we lost original page */ 351 352 int nfs4_mmap_debug = 0; 353 354 static int nfs4_pathconf_cache_hits = 0; 355 static int nfs4_pathconf_cache_misses = 0; 356 357 int nfs4close_all_cnt; 358 int nfs4close_one_debug = 0; 359 int nfs4close_notw_debug = 0; 360 361 int denied_to_flk_debug = 0; 362 void *lockt_denied_debug; 363 364 #endif 365 366 /* 367 * In milliseconds. Should be less than half of the lease time or better, 368 * less than one second. 369 */ 370 int nfs4_base_wait_time = 20; 371 int nfs4_max_base_wait_time = 1 * 1000; /* 1 sec */ 372 373 /* 374 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 375 * or NFS4ERR_RESOURCE. 376 */ 377 static int confirm_retry_sec = 30; 378 379 static int nfs4_lookup_neg_cache = 1; 380 381 /* 382 * number of pages to read ahead 383 * optimized for 100 base-T. 384 */ 385 static int nfs4_nra = 4; 386 387 static int nfs4_do_symlink_cache = 1; 388 389 static int nfs4_pathconf_disable_cache = 0; 390 391 /* 392 * These are the vnode ops routines which implement the vnode interface to 393 * the networked file system. These routines just take their parameters, 394 * make them look networkish by putting the right info into interface structs, 395 * and then calling the appropriate remote routine(s) to do the work. 396 * 397 * Note on directory name lookup cacheing: If we detect a stale fhandle, 398 * we purge the directory cache relative to that vnode. This way, the 399 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 400 * more details on rnode locking. 401 */ 402 403 struct vnodeops *nfs4_vnodeops; 404 405 const fs_operation_def_t nfs4_vnodeops_template[] = { 406 VOPNAME_OPEN, { .vop_open = nfs4_open }, 407 VOPNAME_CLOSE, { .vop_close = nfs4_close }, 408 VOPNAME_READ, { .vop_read = nfs4_read }, 409 VOPNAME_WRITE, { .vop_write = nfs4_write }, 410 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl }, 411 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr }, 412 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr }, 413 VOPNAME_ACCESS, { .vop_access = nfs4_access }, 414 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup }, 415 VOPNAME_CREATE, { .vop_create = nfs4_create }, 416 VOPNAME_REMOVE, { .vop_remove = nfs4_remove }, 417 VOPNAME_LINK, { .vop_link = nfs4_link }, 418 VOPNAME_RENAME, { .vop_rename = nfs4_rename }, 419 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir }, 420 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir }, 421 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir }, 422 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink }, 423 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink }, 424 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync }, 425 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive }, 426 VOPNAME_FID, { .vop_fid = nfs4_fid }, 427 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock }, 428 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock }, 429 VOPNAME_SEEK, { .vop_seek = nfs4_seek }, 430 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock }, 431 VOPNAME_SPACE, { .vop_space = nfs4_space }, 432 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp }, 433 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage }, 434 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage }, 435 VOPNAME_MAP, { .vop_map = nfs4_map }, 436 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap }, 437 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap }, 438 /* no separate nfs4_dump */ 439 VOPNAME_DUMP, { .vop_dump = nfs_dump }, 440 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf }, 441 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio }, 442 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose }, 443 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr }, 444 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr }, 445 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock }, 446 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 447 NULL, NULL 448 }; 449 450 /* 451 * The following are subroutines and definitions to set args or get res 452 * for the different nfsv4 ops 453 */ 454 455 void 456 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 457 { 458 int i; 459 460 for (i = 0; i < arglen; i++) { 461 if (argop[i].argop == OP_LOOKUP) { 462 kmem_free( 463 argop[i].nfs_argop4_u.oplookup. 464 objname.utf8string_val, 465 argop[i].nfs_argop4_u.oplookup. 466 objname.utf8string_len); 467 } 468 } 469 } 470 471 static void 472 nfs4args_lock_free(nfs_argop4 *argop) 473 { 474 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 475 476 if (locker->new_lock_owner == TRUE) { 477 open_to_lock_owner4 *open_owner; 478 479 open_owner = &locker->locker4_u.open_owner; 480 if (open_owner->lock_owner.owner_val != NULL) { 481 kmem_free(open_owner->lock_owner.owner_val, 482 open_owner->lock_owner.owner_len); 483 } 484 } 485 } 486 487 static void 488 nfs4args_lockt_free(nfs_argop4 *argop) 489 { 490 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 491 492 if (lowner->owner_val != NULL) { 493 kmem_free(lowner->owner_val, lowner->owner_len); 494 } 495 } 496 497 static void 498 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 499 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 500 nfs4_stateid_types_t *sid_types) 501 { 502 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 503 mntinfo4_t *mi; 504 505 argop->argop = OP_SETATTR; 506 /* 507 * The stateid is set to 0 if client is not modifying the size 508 * and otherwise to whatever nfs4_get_stateid() returns. 509 * 510 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 511 * state struct could be found for the process/file pair. We may 512 * want to change this in the future (by OPENing the file). See 513 * bug # 4474852. 514 */ 515 if (vap->va_mask & AT_SIZE) { 516 517 ASSERT(rp != NULL); 518 mi = VTOMI4(RTOV4(rp)); 519 520 argop->nfs_argop4_u.opsetattr.stateid = 521 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 522 OP_SETATTR, sid_types, FALSE); 523 } else { 524 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 525 sizeof (stateid4)); 526 } 527 528 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 529 if (*error) 530 bzero(attr, sizeof (*attr)); 531 } 532 533 static void 534 nfs4args_setattr_free(nfs_argop4 *argop) 535 { 536 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 537 } 538 539 static int 540 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 541 bitmap4 supp) 542 { 543 fattr4 *attr; 544 int error = 0; 545 546 argop->argop = op; 547 switch (op) { 548 case OP_VERIFY: 549 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 550 break; 551 case OP_NVERIFY: 552 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 553 break; 554 default: 555 return (EINVAL); 556 } 557 if (!error) 558 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 559 if (error) 560 bzero(attr, sizeof (*attr)); 561 return (error); 562 } 563 564 static void 565 nfs4args_verify_free(nfs_argop4 *argop) 566 { 567 switch (argop->argop) { 568 case OP_VERIFY: 569 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 570 break; 571 case OP_NVERIFY: 572 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 573 break; 574 default: 575 break; 576 } 577 } 578 579 static void 580 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 581 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 582 { 583 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 584 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 585 586 argop->argop = OP_WRITE; 587 wargs->stable = stable; 588 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 589 mi, OP_WRITE, sid_tp); 590 wargs->mblk = NULL; 591 *wargs_pp = wargs; 592 } 593 594 void 595 nfs4args_copen_free(OPEN4cargs *open_args) 596 { 597 if (open_args->owner.owner_val) { 598 kmem_free(open_args->owner.owner_val, 599 open_args->owner.owner_len); 600 } 601 if ((open_args->opentype == OPEN4_CREATE) && 602 (open_args->mode != EXCLUSIVE4)) { 603 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 604 } 605 } 606 607 /* 608 * XXX: This is referenced in modstubs.s 609 */ 610 struct vnodeops * 611 nfs4_getvnodeops(void) 612 { 613 return (nfs4_vnodeops); 614 } 615 616 /* 617 * The OPEN operation opens a regular file. 618 */ 619 /*ARGSUSED3*/ 620 static int 621 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 622 { 623 vnode_t *dvp = NULL; 624 rnode4_t *rp, *drp; 625 int error; 626 int just_been_created; 627 char fn[MAXNAMELEN]; 628 629 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 630 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 631 return (EIO); 632 rp = VTOR4(*vpp); 633 634 /* 635 * Check to see if opening something besides a regular file; 636 * if so skip the OTW call 637 */ 638 if ((*vpp)->v_type != VREG) { 639 error = nfs4_open_non_reg_file(vpp, flag, cr); 640 return (error); 641 } 642 643 /* 644 * XXX - would like a check right here to know if the file is 645 * executable or not, so as to skip OTW 646 */ 647 648 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 649 return (error); 650 651 drp = VTOR4(dvp); 652 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 653 return (EINTR); 654 655 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) { 656 nfs_rw_exit(&drp->r_rwlock); 657 return (error); 658 } 659 660 /* 661 * See if this file has just been CREATEd. 662 * If so, clear the flag and update the dnlc, which was previously 663 * skipped in nfs4_create. 664 * XXX need better serilization on this. 665 * XXX move this into the nf4open_otw call, after we have 666 * XXX acquired the open owner seqid sync. 667 */ 668 mutex_enter(&rp->r_statev4_lock); 669 if (rp->created_v4) { 670 rp->created_v4 = 0; 671 mutex_exit(&rp->r_statev4_lock); 672 673 dnlc_update(dvp, fn, *vpp); 674 /* This is needed so we don't bump the open ref count */ 675 just_been_created = 1; 676 } else { 677 mutex_exit(&rp->r_statev4_lock); 678 just_been_created = 0; 679 } 680 681 /* 682 * If caller specified O_TRUNC/FTRUNC, then be sure to set 683 * FWRITE (to drive successful setattr(size=0) after open) 684 */ 685 if (flag & FTRUNC) 686 flag |= FWRITE; 687 688 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 689 just_been_created); 690 691 if (!error && !((*vpp)->v_flag & VROOT)) 692 dnlc_update(dvp, fn, *vpp); 693 694 nfs_rw_exit(&drp->r_rwlock); 695 696 /* release the hold from vtodv */ 697 VN_RELE(dvp); 698 699 /* exchange the shadow for the master vnode, if needed */ 700 701 if (error == 0 && IS_SHADOW(*vpp, rp)) 702 sv_exchange(vpp); 703 704 return (error); 705 } 706 707 /* 708 * See if there's a "lost open" request to be saved and recovered. 709 */ 710 static void 711 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 712 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 713 vnode_t *dvp, OPEN4cargs *open_args) 714 { 715 vfs_t *vfsp; 716 char *srccfp; 717 718 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 719 720 if (error != ETIMEDOUT && error != EINTR && 721 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 722 lost_rqstp->lr_op = 0; 723 return; 724 } 725 726 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 727 "nfs4open_save_lost_rqst: error %d", error)); 728 729 lost_rqstp->lr_op = OP_OPEN; 730 731 /* 732 * The vp (if it is not NULL) and dvp are held and rele'd via 733 * the recovery code. See nfs4_save_lost_rqst. 734 */ 735 lost_rqstp->lr_vp = vp; 736 lost_rqstp->lr_dvp = dvp; 737 lost_rqstp->lr_oop = oop; 738 lost_rqstp->lr_osp = NULL; 739 lost_rqstp->lr_lop = NULL; 740 lost_rqstp->lr_cr = cr; 741 lost_rqstp->lr_flk = NULL; 742 lost_rqstp->lr_oacc = open_args->share_access; 743 lost_rqstp->lr_odeny = open_args->share_deny; 744 lost_rqstp->lr_oclaim = open_args->claim; 745 if (open_args->claim == CLAIM_DELEGATE_CUR) { 746 lost_rqstp->lr_ostateid = 747 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 748 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 749 } else { 750 srccfp = open_args->open_claim4_u.cfile; 751 } 752 lost_rqstp->lr_ofile.utf8string_len = 0; 753 lost_rqstp->lr_ofile.utf8string_val = NULL; 754 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 755 lost_rqstp->lr_putfirst = FALSE; 756 } 757 758 struct nfs4_excl_time { 759 uint32 seconds; 760 uint32 nseconds; 761 }; 762 763 /* 764 * The OPEN operation creates and/or opens a regular file 765 * 766 * ARGSUSED 767 */ 768 static int 769 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 770 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 771 enum createmode4 createmode, int file_just_been_created) 772 { 773 rnode4_t *rp; 774 rnode4_t *drp = VTOR4(dvp); 775 vnode_t *vp = NULL; 776 vnode_t *vpi = *vpp; 777 bool_t needrecov = FALSE; 778 779 int doqueue = 1; 780 781 COMPOUND4args_clnt args; 782 COMPOUND4res_clnt res; 783 nfs_argop4 *argop; 784 nfs_resop4 *resop; 785 int argoplist_size; 786 int idx_open, idx_fattr; 787 788 GETFH4res *gf_res = NULL; 789 OPEN4res *op_res = NULL; 790 nfs4_ga_res_t *garp; 791 fattr4 *attr = NULL; 792 struct nfs4_excl_time verf; 793 bool_t did_excl_setup = FALSE; 794 int created_osp; 795 796 OPEN4cargs *open_args; 797 nfs4_open_owner_t *oop = NULL; 798 nfs4_open_stream_t *osp = NULL; 799 seqid4 seqid = 0; 800 bool_t retry_open = FALSE; 801 nfs4_recov_state_t recov_state; 802 nfs4_lost_rqst_t lost_rqst; 803 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 804 hrtime_t t; 805 int acc = 0; 806 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 807 cred_t *ncr = NULL; 808 809 nfs4_sharedfh_t *otw_sfh; 810 nfs4_sharedfh_t *orig_sfh; 811 int fh_differs = 0; 812 int numops, setgid_flag; 813 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 814 815 /* 816 * Make sure we properly deal with setting the right gid on 817 * a newly created file to reflect the parent's setgid bit 818 */ 819 setgid_flag = 0; 820 if (create_flag && in_va) { 821 822 /* 823 * If there is grpid mount flag used or 824 * the parent's directory has the setgid bit set 825 * _and_ the client was able to get a valid mapping 826 * for the parent dir's owner_group, we want to 827 * append NVERIFY(owner_group == dva.va_gid) and 828 * SETATTR to the CREATE compound. 829 */ 830 mutex_enter(&drp->r_statelock); 831 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID || 832 drp->r_attr.va_mode & VSGID) && 833 drp->r_attr.va_gid != GID_NOBODY) { 834 in_va->va_mask |= AT_GID; 835 in_va->va_gid = drp->r_attr.va_gid; 836 setgid_flag = 1; 837 } 838 mutex_exit(&drp->r_statelock); 839 } 840 841 /* 842 * Normal/non-create compound: 843 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 844 * 845 * Open(create) compound no setgid: 846 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 847 * RESTOREFH + GETATTR 848 * 849 * Open(create) setgid: 850 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 851 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 852 * NVERIFY(grp) + SETATTR 853 */ 854 if (setgid_flag) { 855 numops = 10; 856 idx_open = 1; 857 idx_fattr = 3; 858 } else if (create_flag) { 859 numops = 7; 860 idx_open = 2; 861 idx_fattr = 4; 862 } else { 863 numops = 4; 864 idx_open = 1; 865 idx_fattr = 3; 866 } 867 868 args.array_len = numops; 869 argoplist_size = numops * sizeof (nfs_argop4); 870 argop = kmem_alloc(argoplist_size, KM_SLEEP); 871 872 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 873 "open %s open flag 0x%x cred %p", file_name, open_flag, 874 (void *)cr)); 875 876 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 877 if (create_flag) { 878 /* 879 * We are to create a file. Initialize the passed in vnode 880 * pointer. 881 */ 882 vpi = NULL; 883 } else { 884 /* 885 * Check to see if the client owns a read delegation and is 886 * trying to open for write. If so, then return the delegation 887 * to avoid the server doing a cb_recall and returning DELAY. 888 * NB - we don't use the statev4_lock here because we'd have 889 * to drop the lock anyway and the result would be stale. 890 */ 891 if ((open_flag & FWRITE) && 892 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 893 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 894 895 /* 896 * If the file has a delegation, then do an access check up 897 * front. This avoids having to an access check later after 898 * we've already done start_op, which could deadlock. 899 */ 900 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 901 if (open_flag & FREAD && 902 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0) 903 acc |= VREAD; 904 if (open_flag & FWRITE && 905 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0) 906 acc |= VWRITE; 907 } 908 } 909 910 drp = VTOR4(dvp); 911 912 recov_state.rs_flags = 0; 913 recov_state.rs_num_retry_despite_err = 0; 914 cred_otw = cr; 915 916 recov_retry: 917 fh_differs = 0; 918 nfs4_error_zinit(&e); 919 920 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 921 if (e.error) { 922 if (ncr != NULL) 923 crfree(ncr); 924 kmem_free(argop, argoplist_size); 925 return (e.error); 926 } 927 928 args.ctag = TAG_OPEN; 929 args.array_len = numops; 930 args.array = argop; 931 932 /* putfh directory fh */ 933 argop[0].argop = OP_CPUTFH; 934 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 935 936 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 937 argop[idx_open].argop = OP_COPEN; 938 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 939 open_args->claim = CLAIM_NULL; 940 941 /* name of file */ 942 open_args->open_claim4_u.cfile = file_name; 943 open_args->owner.owner_len = 0; 944 open_args->owner.owner_val = NULL; 945 946 if (create_flag) { 947 /* CREATE a file */ 948 open_args->opentype = OPEN4_CREATE; 949 open_args->mode = createmode; 950 if (createmode == EXCLUSIVE4) { 951 if (did_excl_setup == FALSE) { 952 verf.seconds = zone_get_hostid(NULL); 953 if (verf.seconds != 0) 954 verf.nseconds = newnum(); 955 else { 956 timestruc_t now; 957 958 gethrestime(&now); 959 verf.seconds = now.tv_sec; 960 verf.nseconds = now.tv_nsec; 961 } 962 /* 963 * Since the server will use this value for the 964 * mtime, make sure that it can't overflow. Zero 965 * out the MSB. The actual value does not matter 966 * here, only its uniqeness. 967 */ 968 verf.seconds &= INT32_MAX; 969 did_excl_setup = TRUE; 970 } 971 972 /* Now copy over verifier to OPEN4args. */ 973 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 974 } else { 975 int v_error; 976 bitmap4 supp_attrs; 977 servinfo4_t *svp; 978 979 attr = &open_args->createhow4_u.createattrs; 980 981 svp = drp->r_server; 982 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 983 supp_attrs = svp->sv_supp_attrs; 984 nfs_rw_exit(&svp->sv_lock); 985 986 /* GUARDED4 or UNCHECKED4 */ 987 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 988 supp_attrs); 989 if (v_error) { 990 bzero(attr, sizeof (*attr)); 991 nfs4args_copen_free(open_args); 992 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 993 &recov_state, FALSE); 994 if (ncr != NULL) 995 crfree(ncr); 996 kmem_free(argop, argoplist_size); 997 return (v_error); 998 } 999 } 1000 } else { 1001 /* NO CREATE */ 1002 open_args->opentype = OPEN4_NOCREATE; 1003 } 1004 1005 if (recov_state.rs_sp != NULL) { 1006 mutex_enter(&recov_state.rs_sp->s_lock); 1007 open_args->owner.clientid = recov_state.rs_sp->clientid; 1008 mutex_exit(&recov_state.rs_sp->s_lock); 1009 } else { 1010 /* XXX should we just fail here? */ 1011 open_args->owner.clientid = 0; 1012 } 1013 1014 /* 1015 * This increments oop's ref count or creates a temporary 'just_created' 1016 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 1017 * completes. 1018 */ 1019 mutex_enter(&VTOMI4(dvp)->mi_lock); 1020 1021 /* See if a permanent or just created open owner exists */ 1022 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 1023 if (!oop) { 1024 /* 1025 * This open owner does not exist so create a temporary 1026 * just created one. 1027 */ 1028 oop = create_open_owner(cr, VTOMI4(dvp)); 1029 ASSERT(oop != NULL); 1030 } 1031 mutex_exit(&VTOMI4(dvp)->mi_lock); 1032 1033 /* this length never changes, do alloc before seqid sync */ 1034 open_args->owner.owner_len = sizeof (oop->oo_name); 1035 open_args->owner.owner_val = 1036 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1037 1038 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 1039 if (e.error == EAGAIN) { 1040 open_owner_rele(oop); 1041 nfs4args_copen_free(open_args); 1042 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1043 if (ncr != NULL) { 1044 crfree(ncr); 1045 ncr = NULL; 1046 } 1047 goto recov_retry; 1048 } 1049 1050 /* Check to see if we need to do the OTW call */ 1051 if (!create_flag) { 1052 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1053 file_just_been_created, &e.error, acc, &recov_state)) { 1054 1055 /* 1056 * The OTW open is not necessary. Either 1057 * the open can succeed without it (eg. 1058 * delegation, error == 0) or the open 1059 * must fail due to an access failure 1060 * (error != 0). In either case, tidy 1061 * up and return. 1062 */ 1063 1064 nfs4_end_open_seqid_sync(oop); 1065 open_owner_rele(oop); 1066 nfs4args_copen_free(open_args); 1067 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1068 if (ncr != NULL) 1069 crfree(ncr); 1070 kmem_free(argop, argoplist_size); 1071 return (e.error); 1072 } 1073 } 1074 1075 bcopy(&oop->oo_name, open_args->owner.owner_val, 1076 open_args->owner.owner_len); 1077 1078 seqid = nfs4_get_open_seqid(oop) + 1; 1079 open_args->seqid = seqid; 1080 open_args->share_access = 0; 1081 if (open_flag & FREAD) 1082 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1083 if (open_flag & FWRITE) 1084 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1085 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1086 1087 1088 1089 /* 1090 * getfh w/sanity check for idx_open/idx_fattr 1091 */ 1092 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1093 argop[idx_open + 1].argop = OP_GETFH; 1094 1095 /* getattr */ 1096 argop[idx_fattr].argop = OP_GETATTR; 1097 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1098 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1099 1100 if (setgid_flag) { 1101 vattr_t _v; 1102 servinfo4_t *svp; 1103 bitmap4 supp_attrs; 1104 1105 svp = drp->r_server; 1106 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1107 supp_attrs = svp->sv_supp_attrs; 1108 nfs_rw_exit(&svp->sv_lock); 1109 1110 /* 1111 * For setgid case, we need to: 1112 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1113 */ 1114 argop[4].argop = OP_SAVEFH; 1115 1116 argop[5].argop = OP_CPUTFH; 1117 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1118 1119 argop[6].argop = OP_GETATTR; 1120 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1121 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1122 1123 argop[7].argop = OP_RESTOREFH; 1124 1125 /* 1126 * nverify 1127 */ 1128 _v.va_mask = AT_GID; 1129 _v.va_gid = in_va->va_gid; 1130 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1131 supp_attrs))) { 1132 1133 /* 1134 * setattr 1135 * 1136 * We _know_ we're not messing with AT_SIZE or 1137 * AT_XTIME, so no need for stateid or flags. 1138 * Also we specify NULL rp since we're only 1139 * interested in setting owner_group attributes. 1140 */ 1141 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1142 supp_attrs, &e.error, 0); 1143 if (e.error) 1144 nfs4args_verify_free(&argop[8]); 1145 } 1146 1147 if (e.error) { 1148 /* 1149 * XXX - Revisit the last argument to nfs4_end_op() 1150 * once 5020486 is fixed. 1151 */ 1152 nfs4_end_open_seqid_sync(oop); 1153 open_owner_rele(oop); 1154 nfs4args_copen_free(open_args); 1155 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1156 if (ncr != NULL) 1157 crfree(ncr); 1158 kmem_free(argop, argoplist_size); 1159 return (e.error); 1160 } 1161 } else if (create_flag) { 1162 argop[1].argop = OP_SAVEFH; 1163 1164 argop[5].argop = OP_RESTOREFH; 1165 1166 argop[6].argop = OP_GETATTR; 1167 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1168 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1169 } 1170 1171 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1172 "nfs4open_otw: %s call, nm %s, rp %s", 1173 needrecov ? "recov" : "first", file_name, 1174 rnode4info(VTOR4(dvp)))); 1175 1176 t = gethrtime(); 1177 1178 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1179 1180 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1181 nfs4_set_open_seqid(seqid, oop, args.ctag); 1182 1183 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1184 1185 if (e.error || needrecov) { 1186 bool_t abort = FALSE; 1187 1188 if (needrecov) { 1189 nfs4_bseqid_entry_t *bsep = NULL; 1190 1191 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1192 cred_otw, vpi, dvp, open_args); 1193 1194 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1195 bsep = nfs4_create_bseqid_entry(oop, NULL, 1196 vpi, 0, args.ctag, open_args->seqid); 1197 num_bseqid_retry--; 1198 } 1199 1200 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1201 NULL, lost_rqst.lr_op == OP_OPEN ? 1202 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL); 1203 1204 if (bsep) 1205 kmem_free(bsep, sizeof (*bsep)); 1206 /* give up if we keep getting BAD_SEQID */ 1207 if (num_bseqid_retry == 0) 1208 abort = TRUE; 1209 if (abort == TRUE && e.error == 0) 1210 e.error = geterrno4(res.status); 1211 } 1212 nfs4_end_open_seqid_sync(oop); 1213 open_owner_rele(oop); 1214 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1215 nfs4args_copen_free(open_args); 1216 if (setgid_flag) { 1217 nfs4args_verify_free(&argop[8]); 1218 nfs4args_setattr_free(&argop[9]); 1219 } 1220 if (!e.error) 1221 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1222 if (ncr != NULL) { 1223 crfree(ncr); 1224 ncr = NULL; 1225 } 1226 if (!needrecov || abort == TRUE || e.error == EINTR || 1227 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1228 kmem_free(argop, argoplist_size); 1229 return (e.error); 1230 } 1231 goto recov_retry; 1232 } 1233 1234 /* 1235 * Will check and update lease after checking the rflag for 1236 * OPEN_CONFIRM in the successful OPEN call. 1237 */ 1238 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1239 1240 /* 1241 * XXX what if we're crossing mount points from server1:/drp 1242 * to server2:/drp/rp. 1243 */ 1244 1245 /* Signal our end of use of the open seqid */ 1246 nfs4_end_open_seqid_sync(oop); 1247 1248 /* 1249 * This will destroy the open owner if it was just created, 1250 * and no one else has put a reference on it. 1251 */ 1252 open_owner_rele(oop); 1253 if (create_flag && (createmode != EXCLUSIVE4) && 1254 res.status == NFS4ERR_BADOWNER) 1255 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1256 1257 e.error = geterrno4(res.status); 1258 nfs4args_copen_free(open_args); 1259 if (setgid_flag) { 1260 nfs4args_verify_free(&argop[8]); 1261 nfs4args_setattr_free(&argop[9]); 1262 } 1263 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1264 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1265 /* 1266 * If the reply is NFS4ERR_ACCESS, it may be because 1267 * we are root (no root net access). If the real uid 1268 * is not root, then retry with the real uid instead. 1269 */ 1270 if (ncr != NULL) { 1271 crfree(ncr); 1272 ncr = NULL; 1273 } 1274 if (res.status == NFS4ERR_ACCESS && 1275 (ncr = crnetadjust(cred_otw)) != NULL) { 1276 cred_otw = ncr; 1277 goto recov_retry; 1278 } 1279 kmem_free(argop, argoplist_size); 1280 return (e.error); 1281 } 1282 1283 resop = &res.array[idx_open]; /* open res */ 1284 op_res = &resop->nfs_resop4_u.opopen; 1285 1286 #ifdef DEBUG 1287 /* 1288 * verify attrset bitmap 1289 */ 1290 if (create_flag && 1291 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1292 /* make sure attrset returned is what we asked for */ 1293 /* XXX Ignore this 'error' for now */ 1294 if (attr->attrmask != op_res->attrset) 1295 /* EMPTY */; 1296 } 1297 #endif 1298 1299 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1300 mutex_enter(&VTOMI4(dvp)->mi_lock); 1301 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1302 mutex_exit(&VTOMI4(dvp)->mi_lock); 1303 } 1304 1305 resop = &res.array[idx_open + 1]; /* getfh res */ 1306 gf_res = &resop->nfs_resop4_u.opgetfh; 1307 1308 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1309 1310 /* 1311 * The open stateid has been updated on the server but not 1312 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1313 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1314 * WRITE call. That, however, will use the old stateid, so go ahead 1315 * and upate the open stateid now, before any call to makenfs4node. 1316 */ 1317 if (vpi) { 1318 nfs4_open_stream_t *tmp_osp; 1319 rnode4_t *tmp_rp = VTOR4(vpi); 1320 1321 tmp_osp = find_open_stream(oop, tmp_rp); 1322 if (tmp_osp) { 1323 tmp_osp->open_stateid = op_res->stateid; 1324 mutex_exit(&tmp_osp->os_sync_lock); 1325 open_stream_rele(tmp_osp, tmp_rp); 1326 } 1327 1328 /* 1329 * We must determine if the file handle given by the otw open 1330 * is the same as the file handle which was passed in with 1331 * *vpp. This case can be reached if the file we are trying 1332 * to open has been removed and another file has been created 1333 * having the same file name. The passed in vnode is released 1334 * later. 1335 */ 1336 orig_sfh = VTOR4(vpi)->r_fh; 1337 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1338 } 1339 1340 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1341 1342 if (create_flag || fh_differs) { 1343 int rnode_err = 0; 1344 1345 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1346 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh)); 1347 1348 if (e.error) 1349 PURGE_ATTRCACHE4(vp); 1350 /* 1351 * For the newly created vp case, make sure the rnode 1352 * isn't bad before using it. 1353 */ 1354 mutex_enter(&(VTOR4(vp))->r_statelock); 1355 if (VTOR4(vp)->r_flags & R4RECOVERR) 1356 rnode_err = EIO; 1357 mutex_exit(&(VTOR4(vp))->r_statelock); 1358 1359 if (rnode_err) { 1360 nfs4_end_open_seqid_sync(oop); 1361 nfs4args_copen_free(open_args); 1362 if (setgid_flag) { 1363 nfs4args_verify_free(&argop[8]); 1364 nfs4args_setattr_free(&argop[9]); 1365 } 1366 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1367 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1368 needrecov); 1369 open_owner_rele(oop); 1370 VN_RELE(vp); 1371 if (ncr != NULL) 1372 crfree(ncr); 1373 sfh4_rele(&otw_sfh); 1374 kmem_free(argop, argoplist_size); 1375 return (EIO); 1376 } 1377 } else { 1378 vp = vpi; 1379 } 1380 sfh4_rele(&otw_sfh); 1381 1382 /* 1383 * It seems odd to get a full set of attrs and then not update 1384 * the object's attrcache in the non-create case. Create case uses 1385 * the attrs since makenfs4node checks to see if the attrs need to 1386 * be updated (and then updates them). The non-create case should 1387 * update attrs also. 1388 */ 1389 if (! create_flag && ! fh_differs && !e.error) { 1390 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1391 } 1392 1393 nfs4_error_zinit(&e); 1394 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1395 /* This does not do recovery for vp explicitly. */ 1396 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1397 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1398 1399 if (e.error || e.stat) { 1400 nfs4_end_open_seqid_sync(oop); 1401 nfs4args_copen_free(open_args); 1402 if (setgid_flag) { 1403 nfs4args_verify_free(&argop[8]); 1404 nfs4args_setattr_free(&argop[9]); 1405 } 1406 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1407 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1408 needrecov); 1409 open_owner_rele(oop); 1410 if (create_flag || fh_differs) { 1411 /* rele the makenfs4node */ 1412 VN_RELE(vp); 1413 } 1414 if (ncr != NULL) { 1415 crfree(ncr); 1416 ncr = NULL; 1417 } 1418 if (retry_open == TRUE) { 1419 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1420 "nfs4open_otw: retry the open since OPEN " 1421 "CONFIRM failed with error %d stat %d", 1422 e.error, e.stat)); 1423 if (create_flag && createmode == GUARDED4) { 1424 NFS4_DEBUG(nfs4_client_recov_debug, 1425 (CE_NOTE, "nfs4open_otw: switch " 1426 "createmode from GUARDED4 to " 1427 "UNCHECKED4")); 1428 createmode = UNCHECKED4; 1429 } 1430 goto recov_retry; 1431 } 1432 if (!e.error) { 1433 if (create_flag && (createmode != EXCLUSIVE4) && 1434 e.stat == NFS4ERR_BADOWNER) 1435 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1436 1437 e.error = geterrno4(e.stat); 1438 } 1439 kmem_free(argop, argoplist_size); 1440 return (e.error); 1441 } 1442 } 1443 1444 rp = VTOR4(vp); 1445 1446 mutex_enter(&rp->r_statev4_lock); 1447 if (create_flag) 1448 rp->created_v4 = 1; 1449 mutex_exit(&rp->r_statev4_lock); 1450 1451 mutex_enter(&oop->oo_lock); 1452 /* Doesn't matter if 'oo_just_created' already was set as this */ 1453 oop->oo_just_created = NFS4_PERM_CREATED; 1454 if (oop->oo_cred_otw) 1455 crfree(oop->oo_cred_otw); 1456 oop->oo_cred_otw = cred_otw; 1457 crhold(oop->oo_cred_otw); 1458 mutex_exit(&oop->oo_lock); 1459 1460 /* returns with 'os_sync_lock' held */ 1461 osp = find_or_create_open_stream(oop, rp, &created_osp); 1462 if (!osp) { 1463 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1464 "nfs4open_otw: failed to create an open stream")); 1465 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1466 "signal our end of use of the open seqid")); 1467 1468 nfs4_end_open_seqid_sync(oop); 1469 open_owner_rele(oop); 1470 nfs4args_copen_free(open_args); 1471 if (setgid_flag) { 1472 nfs4args_verify_free(&argop[8]); 1473 nfs4args_setattr_free(&argop[9]); 1474 } 1475 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1476 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1477 if (create_flag || fh_differs) 1478 VN_RELE(vp); 1479 if (ncr != NULL) 1480 crfree(ncr); 1481 1482 kmem_free(argop, argoplist_size); 1483 return (EINVAL); 1484 1485 } 1486 1487 osp->open_stateid = op_res->stateid; 1488 1489 if (open_flag & FREAD) 1490 osp->os_share_acc_read++; 1491 if (open_flag & FWRITE) 1492 osp->os_share_acc_write++; 1493 osp->os_share_deny_none++; 1494 1495 /* 1496 * Need to reset this bitfield for the possible case where we were 1497 * going to OTW CLOSE the file, got a non-recoverable error, and before 1498 * we could retry the CLOSE, OPENed the file again. 1499 */ 1500 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1501 osp->os_final_close = 0; 1502 osp->os_force_close = 0; 1503 #ifdef DEBUG 1504 if (osp->os_failed_reopen) 1505 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1506 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1507 (void *)osp, (void *)cr, rnode4info(rp))); 1508 #endif 1509 osp->os_failed_reopen = 0; 1510 1511 mutex_exit(&osp->os_sync_lock); 1512 1513 nfs4_end_open_seqid_sync(oop); 1514 1515 if (created_osp && recov_state.rs_sp != NULL) { 1516 mutex_enter(&recov_state.rs_sp->s_lock); 1517 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1518 mutex_exit(&recov_state.rs_sp->s_lock); 1519 } 1520 1521 /* get rid of our reference to find oop */ 1522 open_owner_rele(oop); 1523 1524 open_stream_rele(osp, rp); 1525 1526 /* accept delegation, if any */ 1527 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1528 1529 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1530 1531 if (createmode == EXCLUSIVE4 && 1532 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1533 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1534 " EXCLUSIVE4: sending a SETATTR")); 1535 /* 1536 * If doing an exclusive create, then generate 1537 * a SETATTR to set the initial attributes. 1538 * Try to set the mtime and the atime to the 1539 * server's current time. It is somewhat 1540 * expected that these fields will be used to 1541 * store the exclusive create cookie. If not, 1542 * server implementors will need to know that 1543 * a SETATTR will follow an exclusive create 1544 * and the cookie should be destroyed if 1545 * appropriate. 1546 * 1547 * The AT_GID and AT_SIZE bits are turned off 1548 * so that the SETATTR request will not attempt 1549 * to process these. The gid will be set 1550 * separately if appropriate. The size is turned 1551 * off because it is assumed that a new file will 1552 * be created empty and if the file wasn't empty, 1553 * then the exclusive create will have failed 1554 * because the file must have existed already. 1555 * Therefore, no truncate operation is needed. 1556 */ 1557 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1558 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1559 1560 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1561 if (e.error) { 1562 /* 1563 * Couldn't correct the attributes of 1564 * the newly created file and the 1565 * attributes are wrong. Remove the 1566 * file and return an error to the 1567 * application. 1568 */ 1569 /* XXX will this take care of client state ? */ 1570 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1571 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1572 " remove file", e.error)); 1573 VN_RELE(vp); 1574 (void) nfs4_remove(dvp, file_name, cr, NULL, 0); 1575 /* 1576 * Since we've reled the vnode and removed 1577 * the file we now need to return the error. 1578 * At this point we don't want to update the 1579 * dircaches, call nfs4_waitfor_purge_complete 1580 * or set vpp to vp so we need to skip these 1581 * as well. 1582 */ 1583 goto skip_update_dircaches; 1584 } 1585 } 1586 1587 /* 1588 * If we created or found the correct vnode, due to create_flag or 1589 * fh_differs being set, then update directory cache attribute, readdir 1590 * and dnlc caches. 1591 */ 1592 if (create_flag || fh_differs) { 1593 dirattr_info_t dinfo, *dinfop; 1594 1595 /* 1596 * Make sure getattr succeeded before using results. 1597 * note: op 7 is getattr(dir) for both flavors of 1598 * open(create). 1599 */ 1600 if (create_flag && res.status == NFS4_OK) { 1601 dinfo.di_time_call = t; 1602 dinfo.di_cred = cr; 1603 dinfo.di_garp = 1604 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1605 dinfop = &dinfo; 1606 } else { 1607 dinfop = NULL; 1608 } 1609 1610 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1611 dinfop); 1612 } 1613 1614 /* 1615 * If the page cache for this file was flushed from actions 1616 * above, it was done asynchronously and if that is true, 1617 * there is a need to wait here for it to complete. This must 1618 * be done outside of start_fop/end_fop. 1619 */ 1620 (void) nfs4_waitfor_purge_complete(vp); 1621 1622 /* 1623 * It is implicit that we are in the open case (create_flag == 0) since 1624 * fh_differs can only be set to a non-zero value in the open case. 1625 */ 1626 if (fh_differs != 0 && vpi != NULL) 1627 VN_RELE(vpi); 1628 1629 /* 1630 * Be sure to set *vpp to the correct value before returning. 1631 */ 1632 *vpp = vp; 1633 1634 skip_update_dircaches: 1635 1636 nfs4args_copen_free(open_args); 1637 if (setgid_flag) { 1638 nfs4args_verify_free(&argop[8]); 1639 nfs4args_setattr_free(&argop[9]); 1640 } 1641 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1642 1643 if (ncr) 1644 crfree(ncr); 1645 kmem_free(argop, argoplist_size); 1646 return (e.error); 1647 } 1648 1649 /* 1650 * Reopen an open instance. cf. nfs4open_otw(). 1651 * 1652 * Errors are returned by the nfs4_error_t parameter. 1653 * - ep->error contains an errno value or zero. 1654 * - if it is zero, ep->stat is set to an NFS status code, if any. 1655 * If the file could not be reopened, but the caller should continue, the 1656 * file is marked dead and no error values are returned. If the caller 1657 * should stop recovering open files and start over, either the ep->error 1658 * value or ep->stat will indicate an error (either something that requires 1659 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1660 * filehandles) may be handled silently by this routine. 1661 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1662 * will be started, so the caller should not do it. 1663 * 1664 * Gotos: 1665 * - kill_file : reopen failed in such a fashion to constitute marking the 1666 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1667 * is for cases where recovery is not possible. 1668 * - failed_reopen : same as above, except that the file has already been 1669 * marked dead, so no need to do it again. 1670 * - bailout : reopen failed but we are able to recover and retry the reopen - 1671 * either within this function immediately or via the calling function. 1672 */ 1673 1674 void 1675 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1676 open_claim_type4 claim, bool_t frc_use_claim_previous, 1677 bool_t is_recov) 1678 { 1679 COMPOUND4args_clnt args; 1680 COMPOUND4res_clnt res; 1681 nfs_argop4 argop[4]; 1682 nfs_resop4 *resop; 1683 OPEN4res *op_res = NULL; 1684 OPEN4cargs *open_args; 1685 GETFH4res *gf_res; 1686 rnode4_t *rp = VTOR4(vp); 1687 int doqueue = 1; 1688 cred_t *cr = NULL, *cred_otw = NULL; 1689 nfs4_open_owner_t *oop = NULL; 1690 seqid4 seqid; 1691 nfs4_ga_res_t *garp; 1692 char fn[MAXNAMELEN]; 1693 nfs4_recov_state_t recov = {NULL, 0}; 1694 nfs4_lost_rqst_t lost_rqst; 1695 mntinfo4_t *mi = VTOMI4(vp); 1696 bool_t abort; 1697 char *failed_msg = ""; 1698 int fh_different; 1699 hrtime_t t; 1700 nfs4_bseqid_entry_t *bsep = NULL; 1701 1702 ASSERT(nfs4_consistent_type(vp)); 1703 ASSERT(nfs_zone() == mi->mi_zone); 1704 1705 nfs4_error_zinit(ep); 1706 1707 /* this is the cred used to find the open owner */ 1708 cr = state_to_cred(osp); 1709 if (cr == NULL) { 1710 failed_msg = "Couldn't reopen: no cred"; 1711 goto kill_file; 1712 } 1713 /* use this cred for OTW operations */ 1714 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1715 1716 top: 1717 nfs4_error_zinit(ep); 1718 1719 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1720 /* File system has been unmounted, quit */ 1721 ep->error = EIO; 1722 failed_msg = "Couldn't reopen: file system has been unmounted"; 1723 goto kill_file; 1724 } 1725 1726 oop = osp->os_open_owner; 1727 1728 ASSERT(oop != NULL); 1729 if (oop == NULL) { /* be defensive in non-DEBUG */ 1730 failed_msg = "can't reopen: no open owner"; 1731 goto kill_file; 1732 } 1733 open_owner_hold(oop); 1734 1735 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1736 if (ep->error) { 1737 open_owner_rele(oop); 1738 oop = NULL; 1739 goto bailout; 1740 } 1741 1742 /* 1743 * If the rnode has a delegation and the delegation has been 1744 * recovered and the server didn't request a recall and the caller 1745 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1746 * recovery) and the rnode hasn't been marked dead, then install 1747 * the delegation stateid in the open stream. Otherwise, proceed 1748 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1749 */ 1750 mutex_enter(&rp->r_statev4_lock); 1751 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1752 !rp->r_deleg_return_pending && 1753 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1754 !rp->r_deleg_needs_recall && 1755 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1756 !(rp->r_flags & R4RECOVERR)) { 1757 mutex_enter(&osp->os_sync_lock); 1758 osp->os_delegation = 1; 1759 osp->open_stateid = rp->r_deleg_stateid; 1760 mutex_exit(&osp->os_sync_lock); 1761 mutex_exit(&rp->r_statev4_lock); 1762 goto bailout; 1763 } 1764 mutex_exit(&rp->r_statev4_lock); 1765 1766 /* 1767 * If the file failed recovery, just quit. This failure need not 1768 * affect other reopens, so don't return an error. 1769 */ 1770 mutex_enter(&rp->r_statelock); 1771 if (rp->r_flags & R4RECOVERR) { 1772 mutex_exit(&rp->r_statelock); 1773 ep->error = 0; 1774 goto failed_reopen; 1775 } 1776 mutex_exit(&rp->r_statelock); 1777 1778 /* 1779 * argop is empty here 1780 * 1781 * PUTFH, OPEN, GETATTR 1782 */ 1783 args.ctag = TAG_REOPEN; 1784 args.array_len = 4; 1785 args.array = argop; 1786 1787 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1788 "nfs4_reopen: file is type %d, id %s", 1789 vp->v_type, rnode4info(VTOR4(vp)))); 1790 1791 argop[0].argop = OP_CPUTFH; 1792 1793 if (claim != CLAIM_PREVIOUS) { 1794 /* 1795 * if this is a file mount then 1796 * use the mntinfo parentfh 1797 */ 1798 argop[0].nfs_argop4_u.opcputfh.sfh = 1799 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1800 VTOSV(vp)->sv_dfh; 1801 } else { 1802 /* putfh fh to reopen */ 1803 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1804 } 1805 1806 argop[1].argop = OP_COPEN; 1807 open_args = &argop[1].nfs_argop4_u.opcopen; 1808 open_args->claim = claim; 1809 1810 if (claim == CLAIM_NULL) { 1811 1812 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1813 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1814 "failed for vp 0x%p for CLAIM_NULL with %m", 1815 (void *)vp); 1816 failed_msg = "Couldn't reopen: vtoname failed for " 1817 "CLAIM_NULL"; 1818 /* nothing allocated yet */ 1819 goto kill_file; 1820 } 1821 1822 open_args->open_claim4_u.cfile = fn; 1823 } else if (claim == CLAIM_PREVIOUS) { 1824 1825 /* 1826 * We have two cases to deal with here: 1827 * 1) We're being called to reopen files in order to satisfy 1828 * a lock operation request which requires us to explicitly 1829 * reopen files which were opened under a delegation. If 1830 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1831 * that case, frc_use_claim_previous is TRUE and we must 1832 * use the rnode's current delegation type (r_deleg_type). 1833 * 2) We're reopening files during some form of recovery. 1834 * In this case, frc_use_claim_previous is FALSE and we 1835 * use the delegation type appropriate for recovery 1836 * (r_deleg_needs_recovery). 1837 */ 1838 mutex_enter(&rp->r_statev4_lock); 1839 open_args->open_claim4_u.delegate_type = 1840 frc_use_claim_previous ? 1841 rp->r_deleg_type : 1842 rp->r_deleg_needs_recovery; 1843 mutex_exit(&rp->r_statev4_lock); 1844 1845 } else if (claim == CLAIM_DELEGATE_CUR) { 1846 1847 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1848 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1849 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1850 "with %m", (void *)vp); 1851 failed_msg = "Couldn't reopen: vtoname failed for " 1852 "CLAIM_DELEGATE_CUR"; 1853 /* nothing allocated yet */ 1854 goto kill_file; 1855 } 1856 1857 mutex_enter(&rp->r_statev4_lock); 1858 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1859 rp->r_deleg_stateid; 1860 mutex_exit(&rp->r_statev4_lock); 1861 1862 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1863 } 1864 open_args->opentype = OPEN4_NOCREATE; 1865 open_args->owner.clientid = mi2clientid(mi); 1866 open_args->owner.owner_len = sizeof (oop->oo_name); 1867 open_args->owner.owner_val = 1868 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1869 bcopy(&oop->oo_name, open_args->owner.owner_val, 1870 open_args->owner.owner_len); 1871 open_args->share_access = 0; 1872 open_args->share_deny = 0; 1873 1874 mutex_enter(&osp->os_sync_lock); 1875 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1876 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1877 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1878 (void *)osp, (void *)rp, osp->os_share_acc_read, 1879 osp->os_share_acc_write, osp->os_open_ref_count, 1880 osp->os_mmap_read, osp->os_mmap_write, claim)); 1881 1882 if (osp->os_share_acc_read || osp->os_mmap_read) 1883 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1884 if (osp->os_share_acc_write || osp->os_mmap_write) 1885 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1886 if (osp->os_share_deny_read) 1887 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1888 if (osp->os_share_deny_write) 1889 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1890 mutex_exit(&osp->os_sync_lock); 1891 1892 seqid = nfs4_get_open_seqid(oop) + 1; 1893 open_args->seqid = seqid; 1894 1895 /* Construct the getfh part of the compound */ 1896 argop[2].argop = OP_GETFH; 1897 1898 /* Construct the getattr part of the compound */ 1899 argop[3].argop = OP_GETATTR; 1900 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1901 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1902 1903 t = gethrtime(); 1904 1905 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1906 1907 if (ep->error) { 1908 if (!is_recov && !frc_use_claim_previous && 1909 (ep->error == EINTR || ep->error == ETIMEDOUT || 1910 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1911 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1912 cred_otw, vp, NULL, open_args); 1913 abort = nfs4_start_recovery(ep, 1914 VTOMI4(vp), vp, NULL, NULL, 1915 lost_rqst.lr_op == OP_OPEN ? 1916 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL); 1917 nfs4args_copen_free(open_args); 1918 goto bailout; 1919 } 1920 1921 nfs4args_copen_free(open_args); 1922 1923 if (ep->error == EACCES && cred_otw != cr) { 1924 crfree(cred_otw); 1925 cred_otw = cr; 1926 crhold(cred_otw); 1927 nfs4_end_open_seqid_sync(oop); 1928 open_owner_rele(oop); 1929 oop = NULL; 1930 goto top; 1931 } 1932 if (ep->error == ETIMEDOUT) 1933 goto bailout; 1934 failed_msg = "Couldn't reopen: rpc error"; 1935 goto kill_file; 1936 } 1937 1938 if (nfs4_need_to_bump_seqid(&res)) 1939 nfs4_set_open_seqid(seqid, oop, args.ctag); 1940 1941 switch (res.status) { 1942 case NFS4_OK: 1943 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1944 mutex_enter(&rp->r_statelock); 1945 rp->r_delay_interval = 0; 1946 mutex_exit(&rp->r_statelock); 1947 } 1948 break; 1949 case NFS4ERR_BAD_SEQID: 1950 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1951 args.ctag, open_args->seqid); 1952 1953 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1954 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1955 NULL, OP_OPEN, bsep, NULL, NULL); 1956 1957 nfs4args_copen_free(open_args); 1958 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1959 nfs4_end_open_seqid_sync(oop); 1960 open_owner_rele(oop); 1961 oop = NULL; 1962 kmem_free(bsep, sizeof (*bsep)); 1963 1964 goto kill_file; 1965 case NFS4ERR_NO_GRACE: 1966 nfs4args_copen_free(open_args); 1967 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1968 nfs4_end_open_seqid_sync(oop); 1969 open_owner_rele(oop); 1970 oop = NULL; 1971 if (claim == CLAIM_PREVIOUS) { 1972 /* 1973 * Retry as a plain open. We don't need to worry about 1974 * checking the changeinfo: it is acceptable for a 1975 * client to re-open a file and continue processing 1976 * (in the absence of locks). 1977 */ 1978 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1979 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1980 "will retry as CLAIM_NULL")); 1981 claim = CLAIM_NULL; 1982 nfs4_mi_kstat_inc_no_grace(mi); 1983 goto top; 1984 } 1985 failed_msg = 1986 "Couldn't reopen: tried reclaim outside grace period. "; 1987 goto kill_file; 1988 case NFS4ERR_GRACE: 1989 nfs4_set_grace_wait(mi); 1990 nfs4args_copen_free(open_args); 1991 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1992 nfs4_end_open_seqid_sync(oop); 1993 open_owner_rele(oop); 1994 oop = NULL; 1995 ep->error = nfs4_wait_for_grace(mi, &recov); 1996 if (ep->error != 0) 1997 goto bailout; 1998 goto top; 1999 case NFS4ERR_DELAY: 2000 nfs4_set_delay_wait(vp); 2001 nfs4args_copen_free(open_args); 2002 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2003 nfs4_end_open_seqid_sync(oop); 2004 open_owner_rele(oop); 2005 oop = NULL; 2006 ep->error = nfs4_wait_for_delay(vp, &recov); 2007 nfs4_mi_kstat_inc_delay(mi); 2008 if (ep->error != 0) 2009 goto bailout; 2010 goto top; 2011 case NFS4ERR_FHEXPIRED: 2012 /* recover filehandle and retry */ 2013 abort = nfs4_start_recovery(ep, 2014 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL); 2015 nfs4args_copen_free(open_args); 2016 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2017 nfs4_end_open_seqid_sync(oop); 2018 open_owner_rele(oop); 2019 oop = NULL; 2020 if (abort == FALSE) 2021 goto top; 2022 failed_msg = "Couldn't reopen: recovery aborted"; 2023 goto kill_file; 2024 case NFS4ERR_RESOURCE: 2025 case NFS4ERR_STALE_CLIENTID: 2026 case NFS4ERR_WRONGSEC: 2027 case NFS4ERR_EXPIRED: 2028 /* 2029 * Do not mark the file dead and let the calling 2030 * function initiate recovery. 2031 */ 2032 nfs4args_copen_free(open_args); 2033 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2034 nfs4_end_open_seqid_sync(oop); 2035 open_owner_rele(oop); 2036 oop = NULL; 2037 goto bailout; 2038 case NFS4ERR_ACCESS: 2039 if (cred_otw != cr) { 2040 crfree(cred_otw); 2041 cred_otw = cr; 2042 crhold(cred_otw); 2043 nfs4args_copen_free(open_args); 2044 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2045 nfs4_end_open_seqid_sync(oop); 2046 open_owner_rele(oop); 2047 oop = NULL; 2048 goto top; 2049 } 2050 /* fall through */ 2051 default: 2052 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2053 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2054 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2055 rnode4info(VTOR4(vp)))); 2056 failed_msg = "Couldn't reopen: NFSv4 error"; 2057 nfs4args_copen_free(open_args); 2058 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2059 goto kill_file; 2060 } 2061 2062 resop = &res.array[1]; /* open res */ 2063 op_res = &resop->nfs_resop4_u.opopen; 2064 2065 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2066 2067 /* 2068 * Check if the path we reopened really is the same 2069 * file. We could end up in a situation where the file 2070 * was removed and a new file created with the same name. 2071 */ 2072 resop = &res.array[2]; 2073 gf_res = &resop->nfs_resop4_u.opgetfh; 2074 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2075 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2076 if (fh_different) { 2077 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2078 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2079 /* Oops, we don't have the same file */ 2080 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2081 failed_msg = "Couldn't reopen: Persistent " 2082 "file handle changed"; 2083 else 2084 failed_msg = "Couldn't reopen: Volatile " 2085 "(no expire on open) file handle changed"; 2086 2087 nfs4args_copen_free(open_args); 2088 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2089 nfs_rw_exit(&mi->mi_fh_lock); 2090 goto kill_file; 2091 2092 } else { 2093 /* 2094 * We have volatile file handles that don't compare. 2095 * If the fids are the same then we assume that the 2096 * file handle expired but the rnode still refers to 2097 * the same file object. 2098 * 2099 * First check that we have fids or not. 2100 * If we don't we have a dumb server so we will 2101 * just assume every thing is ok for now. 2102 */ 2103 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2104 rp->r_attr.va_mask & AT_NODEID && 2105 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2106 /* 2107 * We have fids, but they don't 2108 * compare. So kill the file. 2109 */ 2110 failed_msg = 2111 "Couldn't reopen: file handle changed" 2112 " due to mismatched fids"; 2113 nfs4args_copen_free(open_args); 2114 (void) xdr_free(xdr_COMPOUND4res_clnt, 2115 (caddr_t)&res); 2116 nfs_rw_exit(&mi->mi_fh_lock); 2117 goto kill_file; 2118 } else { 2119 /* 2120 * We have volatile file handles that refers 2121 * to the same file (at least they have the 2122 * same fid) or we don't have fids so we 2123 * can't tell. :(. We'll be a kind and accepting 2124 * client so we'll update the rnode's file 2125 * handle with the otw handle. 2126 * 2127 * We need to drop mi->mi_fh_lock since 2128 * sh4_update acquires it. Since there is 2129 * only one recovery thread there is no 2130 * race. 2131 */ 2132 nfs_rw_exit(&mi->mi_fh_lock); 2133 sfh4_update(rp->r_fh, &gf_res->object); 2134 } 2135 } 2136 } else { 2137 nfs_rw_exit(&mi->mi_fh_lock); 2138 } 2139 2140 ASSERT(nfs4_consistent_type(vp)); 2141 2142 /* 2143 * If the server wanted an OPEN_CONFIRM but that fails, just start 2144 * over. Presumably if there is a persistent error it will show up 2145 * when we resend the OPEN. 2146 */ 2147 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2148 bool_t retry_open = FALSE; 2149 2150 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2151 cred_otw, is_recov, &retry_open, 2152 oop, FALSE, ep, NULL); 2153 if (ep->error || ep->stat) { 2154 nfs4args_copen_free(open_args); 2155 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2156 nfs4_end_open_seqid_sync(oop); 2157 open_owner_rele(oop); 2158 oop = NULL; 2159 goto top; 2160 } 2161 } 2162 2163 mutex_enter(&osp->os_sync_lock); 2164 osp->open_stateid = op_res->stateid; 2165 osp->os_delegation = 0; 2166 /* 2167 * Need to reset this bitfield for the possible case where we were 2168 * going to OTW CLOSE the file, got a non-recoverable error, and before 2169 * we could retry the CLOSE, OPENed the file again. 2170 */ 2171 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2172 osp->os_final_close = 0; 2173 osp->os_force_close = 0; 2174 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2175 osp->os_dc_openacc = open_args->share_access; 2176 mutex_exit(&osp->os_sync_lock); 2177 2178 nfs4_end_open_seqid_sync(oop); 2179 2180 /* accept delegation, if any */ 2181 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2182 2183 nfs4args_copen_free(open_args); 2184 2185 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2186 2187 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2188 2189 ASSERT(nfs4_consistent_type(vp)); 2190 2191 open_owner_rele(oop); 2192 crfree(cr); 2193 crfree(cred_otw); 2194 return; 2195 2196 kill_file: 2197 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2198 failed_reopen: 2199 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2200 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2201 (void *)osp, (void *)cr, rnode4info(rp))); 2202 mutex_enter(&osp->os_sync_lock); 2203 osp->os_failed_reopen = 1; 2204 mutex_exit(&osp->os_sync_lock); 2205 bailout: 2206 if (oop != NULL) { 2207 nfs4_end_open_seqid_sync(oop); 2208 open_owner_rele(oop); 2209 } 2210 if (cr != NULL) 2211 crfree(cr); 2212 if (cred_otw != NULL) 2213 crfree(cred_otw); 2214 } 2215 2216 /* for . and .. OPENs */ 2217 /* ARGSUSED */ 2218 static int 2219 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2220 { 2221 rnode4_t *rp; 2222 nfs4_ga_res_t gar; 2223 2224 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2225 2226 /* 2227 * If close-to-open consistency checking is turned off or 2228 * if there is no cached data, we can avoid 2229 * the over the wire getattr. Otherwise, force a 2230 * call to the server to get fresh attributes and to 2231 * check caches. This is required for close-to-open 2232 * consistency. 2233 */ 2234 rp = VTOR4(*vpp); 2235 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2236 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2237 return (0); 2238 2239 gar.n4g_va.va_mask = AT_ALL; 2240 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2241 } 2242 2243 /* 2244 * CLOSE a file 2245 */ 2246 /* ARGSUSED */ 2247 static int 2248 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2249 caller_context_t *ct) 2250 { 2251 rnode4_t *rp; 2252 int error = 0; 2253 int r_error = 0; 2254 int n4error = 0; 2255 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2256 2257 /* 2258 * Remove client state for this (lockowner, file) pair. 2259 * Issue otw v4 call to have the server do the same. 2260 */ 2261 2262 rp = VTOR4(vp); 2263 2264 /* 2265 * zone_enter(2) prevents processes from changing zones with NFS files 2266 * open; if we happen to get here from the wrong zone we can't do 2267 * anything over the wire. 2268 */ 2269 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2270 /* 2271 * We could attempt to clean up locks, except we're sure 2272 * that the current process didn't acquire any locks on 2273 * the file: any attempt to lock a file belong to another zone 2274 * will fail, and one can't lock an NFS file and then change 2275 * zones, as that fails too. 2276 * 2277 * Returning an error here is the sane thing to do. A 2278 * subsequent call to VN_RELE() which translates to a 2279 * nfs4_inactive() will clean up state: if the zone of the 2280 * vnode's origin is still alive and kicking, the inactive 2281 * thread will handle the request (from the correct zone), and 2282 * everything (minus the OTW close call) should be OK. If the 2283 * zone is going away nfs4_async_inactive() will throw away 2284 * delegations, open streams and cached pages inline. 2285 */ 2286 return (EIO); 2287 } 2288 2289 /* 2290 * If we are using local locking for this filesystem, then 2291 * release all of the SYSV style record locks. Otherwise, 2292 * we are doing network locking and we need to release all 2293 * of the network locks. All of the locks held by this 2294 * process on this file are released no matter what the 2295 * incoming reference count is. 2296 */ 2297 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2298 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2299 cleanshares(vp, ttoproc(curthread)->p_pid); 2300 } else 2301 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2302 2303 if (e.error) { 2304 struct lm_sysid *lmsid; 2305 lmsid = nfs4_find_sysid(VTOMI4(vp)); 2306 if (lmsid == NULL) { 2307 DTRACE_PROBE2(unknown__sysid, int, e.error, 2308 vnode_t *, vp); 2309 } else { 2310 cleanlocks(vp, ttoproc(curthread)->p_pid, 2311 (lm_sysidt(lmsid) | LM_SYSID_CLIENT)); 2312 } 2313 return (e.error); 2314 } 2315 2316 if (count > 1) 2317 return (0); 2318 2319 /* 2320 * If the file has been `unlinked', then purge the 2321 * DNLC so that this vnode will get reycled quicker 2322 * and the .nfs* file on the server will get removed. 2323 */ 2324 if (rp->r_unldvp != NULL) 2325 dnlc_purge_vp(vp); 2326 2327 /* 2328 * If the file was open for write and there are pages, 2329 * do a synchronous flush and commit of all of the 2330 * dirty and uncommitted pages. 2331 */ 2332 ASSERT(!e.error); 2333 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2334 error = nfs4_putpage_commit(vp, 0, 0, cr); 2335 2336 mutex_enter(&rp->r_statelock); 2337 r_error = rp->r_error; 2338 rp->r_error = 0; 2339 mutex_exit(&rp->r_statelock); 2340 2341 /* 2342 * If this file type is one for which no explicit 'open' was 2343 * done, then bail now (ie. no need for protocol 'close'). If 2344 * there was an error w/the vm subsystem, return _that_ error, 2345 * otherwise, return any errors that may've been reported via 2346 * the rnode. 2347 */ 2348 if (vp->v_type != VREG) 2349 return (error ? error : r_error); 2350 2351 /* 2352 * The sync putpage commit may have failed above, but since 2353 * we're working w/a regular file, we need to do the protocol 2354 * 'close' (nfs4close_one will figure out if an otw close is 2355 * needed or not). Report any errors _after_ doing the protocol 2356 * 'close'. 2357 */ 2358 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2359 n4error = e.error ? e.error : geterrno4(e.stat); 2360 2361 /* 2362 * Error reporting prio (Hi -> Lo) 2363 * 2364 * i) nfs4_putpage_commit (error) 2365 * ii) rnode's (r_error) 2366 * iii) nfs4close_one (n4error) 2367 */ 2368 return (error ? error : (r_error ? r_error : n4error)); 2369 } 2370 2371 /* 2372 * Initialize *lost_rqstp. 2373 */ 2374 2375 static void 2376 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2377 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2378 vnode_t *vp) 2379 { 2380 if (error != ETIMEDOUT && error != EINTR && 2381 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2382 lost_rqstp->lr_op = 0; 2383 return; 2384 } 2385 2386 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2387 "nfs4close_save_lost_rqst: error %d", error)); 2388 2389 lost_rqstp->lr_op = OP_CLOSE; 2390 /* 2391 * The vp is held and rele'd via the recovery code. 2392 * See nfs4_save_lost_rqst. 2393 */ 2394 lost_rqstp->lr_vp = vp; 2395 lost_rqstp->lr_dvp = NULL; 2396 lost_rqstp->lr_oop = oop; 2397 lost_rqstp->lr_osp = osp; 2398 ASSERT(osp != NULL); 2399 ASSERT(mutex_owned(&osp->os_sync_lock)); 2400 osp->os_pending_close = 1; 2401 lost_rqstp->lr_lop = NULL; 2402 lost_rqstp->lr_cr = cr; 2403 lost_rqstp->lr_flk = NULL; 2404 lost_rqstp->lr_putfirst = FALSE; 2405 } 2406 2407 /* 2408 * Assumes you already have the open seqid sync grabbed as well as the 2409 * 'os_sync_lock'. Note: this will release the open seqid sync and 2410 * 'os_sync_lock' if client recovery starts. Calling functions have to 2411 * be prepared to handle this. 2412 * 2413 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2414 * was needed and was started, and that the calling function should retry 2415 * this function; otherwise it is returned as 0. 2416 * 2417 * Errors are returned via the nfs4_error_t parameter. 2418 */ 2419 static void 2420 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2421 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2422 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2423 { 2424 COMPOUND4args_clnt args; 2425 COMPOUND4res_clnt res; 2426 CLOSE4args *close_args; 2427 nfs_resop4 *resop; 2428 nfs_argop4 argop[3]; 2429 int doqueue = 1; 2430 mntinfo4_t *mi; 2431 seqid4 seqid; 2432 vnode_t *vp; 2433 bool_t needrecov = FALSE; 2434 nfs4_lost_rqst_t lost_rqst; 2435 hrtime_t t; 2436 2437 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2438 2439 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2440 2441 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2442 2443 /* Only set this to 1 if recovery is started */ 2444 *recov = 0; 2445 2446 /* do the OTW call to close the file */ 2447 2448 if (close_type == CLOSE_RESEND) 2449 args.ctag = TAG_CLOSE_LOST; 2450 else if (close_type == CLOSE_AFTER_RESEND) 2451 args.ctag = TAG_CLOSE_UNDO; 2452 else 2453 args.ctag = TAG_CLOSE; 2454 2455 args.array_len = 3; 2456 args.array = argop; 2457 2458 vp = RTOV4(rp); 2459 2460 mi = VTOMI4(vp); 2461 2462 /* putfh target fh */ 2463 argop[0].argop = OP_CPUTFH; 2464 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2465 2466 argop[1].argop = OP_GETATTR; 2467 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2468 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2469 2470 argop[2].argop = OP_CLOSE; 2471 close_args = &argop[2].nfs_argop4_u.opclose; 2472 2473 seqid = nfs4_get_open_seqid(oop) + 1; 2474 2475 close_args->seqid = seqid; 2476 close_args->open_stateid = osp->open_stateid; 2477 2478 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2479 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2480 rnode4info(rp))); 2481 2482 t = gethrtime(); 2483 2484 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2485 2486 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2487 nfs4_set_open_seqid(seqid, oop, args.ctag); 2488 } 2489 2490 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2491 if (ep->error && !needrecov) { 2492 /* 2493 * if there was an error and no recovery is to be done 2494 * then then set up the file to flush its cache if 2495 * needed for the next caller. 2496 */ 2497 mutex_enter(&rp->r_statelock); 2498 PURGE_ATTRCACHE4_LOCKED(rp); 2499 rp->r_flags &= ~R4WRITEMODIFIED; 2500 mutex_exit(&rp->r_statelock); 2501 return; 2502 } 2503 2504 if (needrecov) { 2505 bool_t abort; 2506 nfs4_bseqid_entry_t *bsep = NULL; 2507 2508 if (close_type != CLOSE_RESEND) 2509 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2510 osp, cred_otw, vp); 2511 2512 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2513 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2514 0, args.ctag, close_args->seqid); 2515 2516 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2517 "nfs4close_otw: initiating recovery. error %d " 2518 "res.status %d", ep->error, res.status)); 2519 2520 /* 2521 * Drop the 'os_sync_lock' here so we don't hit 2522 * a potential recursive mutex_enter via an 2523 * 'open_stream_hold()'. 2524 */ 2525 mutex_exit(&osp->os_sync_lock); 2526 *have_sync_lockp = 0; 2527 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2528 (close_type != CLOSE_RESEND && 2529 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2530 OP_CLOSE, bsep, NULL, NULL); 2531 2532 /* drop open seq sync, and let the calling function regrab it */ 2533 nfs4_end_open_seqid_sync(oop); 2534 *did_start_seqid_syncp = 0; 2535 2536 if (bsep) 2537 kmem_free(bsep, sizeof (*bsep)); 2538 /* 2539 * For signals, the caller wants to quit, so don't say to 2540 * retry. For forced unmount, if it's a user thread, it 2541 * wants to quit. If it's a recovery thread, the retry 2542 * will happen higher-up on the call stack. Either way, 2543 * don't say to retry. 2544 */ 2545 if (abort == FALSE && ep->error != EINTR && 2546 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2547 close_type != CLOSE_RESEND && 2548 close_type != CLOSE_AFTER_RESEND) 2549 *recov = 1; 2550 else 2551 *recov = 0; 2552 2553 if (!ep->error) 2554 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2555 return; 2556 } 2557 2558 if (res.status) { 2559 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2560 return; 2561 } 2562 2563 mutex_enter(&rp->r_statev4_lock); 2564 rp->created_v4 = 0; 2565 mutex_exit(&rp->r_statev4_lock); 2566 2567 resop = &res.array[2]; 2568 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2569 osp->os_valid = 0; 2570 2571 /* 2572 * This removes the reference obtained at OPEN; ie, when the 2573 * open stream structure was created. 2574 * 2575 * We don't have to worry about calling 'open_stream_rele' 2576 * since we our currently holding a reference to the open 2577 * stream which means the count cannot go to 0 with this 2578 * decrement. 2579 */ 2580 ASSERT(osp->os_ref_count >= 2); 2581 osp->os_ref_count--; 2582 2583 if (!ep->error) 2584 nfs4_attr_cache(vp, 2585 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2586 t, cred_otw, TRUE, NULL); 2587 2588 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2589 " returning %d", ep->error)); 2590 2591 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2592 } 2593 2594 /* ARGSUSED */ 2595 static int 2596 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2597 caller_context_t *ct) 2598 { 2599 rnode4_t *rp; 2600 u_offset_t off; 2601 offset_t diff; 2602 uint_t on; 2603 uint_t n; 2604 caddr_t base; 2605 uint_t flags; 2606 int error; 2607 mntinfo4_t *mi; 2608 2609 rp = VTOR4(vp); 2610 2611 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2612 2613 if (IS_SHADOW(vp, rp)) 2614 vp = RTOV4(rp); 2615 2616 if (vp->v_type != VREG) 2617 return (EISDIR); 2618 2619 mi = VTOMI4(vp); 2620 2621 if (nfs_zone() != mi->mi_zone) 2622 return (EIO); 2623 2624 if (uiop->uio_resid == 0) 2625 return (0); 2626 2627 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2628 return (EINVAL); 2629 2630 mutex_enter(&rp->r_statelock); 2631 if (rp->r_flags & R4RECOVERRP) 2632 error = (rp->r_error ? rp->r_error : EIO); 2633 else 2634 error = 0; 2635 mutex_exit(&rp->r_statelock); 2636 if (error) 2637 return (error); 2638 2639 /* 2640 * Bypass VM if caching has been disabled (e.g., locking) or if 2641 * using client-side direct I/O and the file is not mmap'd and 2642 * there are no cached pages. 2643 */ 2644 if ((vp->v_flag & VNOCACHE) || 2645 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2646 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2647 size_t resid = 0; 2648 2649 return (nfs4read(vp, NULL, uiop->uio_loffset, 2650 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2651 } 2652 2653 error = 0; 2654 2655 do { 2656 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2657 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2658 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2659 2660 if (error = nfs4_validate_caches(vp, cr)) 2661 break; 2662 2663 mutex_enter(&rp->r_statelock); 2664 while (rp->r_flags & R4INCACHEPURGE) { 2665 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2666 mutex_exit(&rp->r_statelock); 2667 return (EINTR); 2668 } 2669 } 2670 diff = rp->r_size - uiop->uio_loffset; 2671 mutex_exit(&rp->r_statelock); 2672 if (diff <= 0) 2673 break; 2674 if (diff < n) 2675 n = (uint_t)diff; 2676 2677 if (vpm_enable) { 2678 /* 2679 * Copy data. 2680 */ 2681 error = vpm_data_copy(vp, off + on, n, uiop, 2682 1, NULL, 0, S_READ); 2683 } else { 2684 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2685 S_READ); 2686 2687 error = uiomove(base + on, n, UIO_READ, uiop); 2688 } 2689 2690 if (!error) { 2691 /* 2692 * If read a whole block or read to eof, 2693 * won't need this buffer again soon. 2694 */ 2695 mutex_enter(&rp->r_statelock); 2696 if (n + on == MAXBSIZE || 2697 uiop->uio_loffset == rp->r_size) 2698 flags = SM_DONTNEED; 2699 else 2700 flags = 0; 2701 mutex_exit(&rp->r_statelock); 2702 if (vpm_enable) { 2703 error = vpm_sync_pages(vp, off, n, flags); 2704 } else { 2705 error = segmap_release(segkmap, base, flags); 2706 } 2707 } else { 2708 if (vpm_enable) { 2709 (void) vpm_sync_pages(vp, off, n, 0); 2710 } else { 2711 (void) segmap_release(segkmap, base, 0); 2712 } 2713 } 2714 } while (!error && uiop->uio_resid > 0); 2715 2716 return (error); 2717 } 2718 2719 /* ARGSUSED */ 2720 static int 2721 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2722 caller_context_t *ct) 2723 { 2724 rlim64_t limit = uiop->uio_llimit; 2725 rnode4_t *rp; 2726 u_offset_t off; 2727 caddr_t base; 2728 uint_t flags; 2729 int remainder; 2730 size_t n; 2731 int on; 2732 int error; 2733 int resid; 2734 u_offset_t offset; 2735 mntinfo4_t *mi; 2736 uint_t bsize; 2737 2738 rp = VTOR4(vp); 2739 2740 if (IS_SHADOW(vp, rp)) 2741 vp = RTOV4(rp); 2742 2743 if (vp->v_type != VREG) 2744 return (EISDIR); 2745 2746 mi = VTOMI4(vp); 2747 2748 if (nfs_zone() != mi->mi_zone) 2749 return (EIO); 2750 2751 if (uiop->uio_resid == 0) 2752 return (0); 2753 2754 mutex_enter(&rp->r_statelock); 2755 if (rp->r_flags & R4RECOVERRP) 2756 error = (rp->r_error ? rp->r_error : EIO); 2757 else 2758 error = 0; 2759 mutex_exit(&rp->r_statelock); 2760 if (error) 2761 return (error); 2762 2763 if (ioflag & FAPPEND) { 2764 struct vattr va; 2765 2766 /* 2767 * Must serialize if appending. 2768 */ 2769 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2770 nfs_rw_exit(&rp->r_rwlock); 2771 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2772 INTR4(vp))) 2773 return (EINTR); 2774 } 2775 2776 va.va_mask = AT_SIZE; 2777 error = nfs4getattr(vp, &va, cr); 2778 if (error) 2779 return (error); 2780 uiop->uio_loffset = va.va_size; 2781 } 2782 2783 offset = uiop->uio_loffset + uiop->uio_resid; 2784 2785 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2786 return (EINVAL); 2787 2788 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2789 limit = MAXOFFSET_T; 2790 2791 /* 2792 * Check to make sure that the process will not exceed 2793 * its limit on file size. It is okay to write up to 2794 * the limit, but not beyond. Thus, the write which 2795 * reaches the limit will be short and the next write 2796 * will return an error. 2797 */ 2798 remainder = 0; 2799 if (offset > uiop->uio_llimit) { 2800 remainder = offset - uiop->uio_llimit; 2801 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2802 if (uiop->uio_resid <= 0) { 2803 proc_t *p = ttoproc(curthread); 2804 2805 uiop->uio_resid += remainder; 2806 mutex_enter(&p->p_lock); 2807 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2808 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2809 mutex_exit(&p->p_lock); 2810 return (EFBIG); 2811 } 2812 } 2813 2814 /* update the change attribute, if we have a write delegation */ 2815 2816 mutex_enter(&rp->r_statev4_lock); 2817 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2818 rp->r_deleg_change++; 2819 2820 mutex_exit(&rp->r_statev4_lock); 2821 2822 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2823 return (EINTR); 2824 2825 /* 2826 * Bypass VM if caching has been disabled (e.g., locking) or if 2827 * using client-side direct I/O and the file is not mmap'd and 2828 * there are no cached pages. 2829 */ 2830 if ((vp->v_flag & VNOCACHE) || 2831 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2832 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2833 size_t bufsize; 2834 int count; 2835 u_offset_t org_offset; 2836 stable_how4 stab_comm; 2837 nfs4_fwrite: 2838 if (rp->r_flags & R4STALE) { 2839 resid = uiop->uio_resid; 2840 offset = uiop->uio_loffset; 2841 error = rp->r_error; 2842 /* 2843 * A close may have cleared r_error, if so, 2844 * propagate ESTALE error return properly 2845 */ 2846 if (error == 0) 2847 error = ESTALE; 2848 goto bottom; 2849 } 2850 2851 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2852 base = kmem_alloc(bufsize, KM_SLEEP); 2853 do { 2854 if (ioflag & FDSYNC) 2855 stab_comm = DATA_SYNC4; 2856 else 2857 stab_comm = FILE_SYNC4; 2858 resid = uiop->uio_resid; 2859 offset = uiop->uio_loffset; 2860 count = MIN(uiop->uio_resid, bufsize); 2861 org_offset = uiop->uio_loffset; 2862 error = uiomove(base, count, UIO_WRITE, uiop); 2863 if (!error) { 2864 error = nfs4write(vp, base, org_offset, 2865 count, cr, &stab_comm); 2866 if (!error) { 2867 mutex_enter(&rp->r_statelock); 2868 if (rp->r_size < uiop->uio_loffset) 2869 rp->r_size = uiop->uio_loffset; 2870 mutex_exit(&rp->r_statelock); 2871 } 2872 } 2873 } while (!error && uiop->uio_resid > 0); 2874 kmem_free(base, bufsize); 2875 goto bottom; 2876 } 2877 2878 bsize = vp->v_vfsp->vfs_bsize; 2879 2880 do { 2881 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2882 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2883 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2884 2885 resid = uiop->uio_resid; 2886 offset = uiop->uio_loffset; 2887 2888 if (rp->r_flags & R4STALE) { 2889 error = rp->r_error; 2890 /* 2891 * A close may have cleared r_error, if so, 2892 * propagate ESTALE error return properly 2893 */ 2894 if (error == 0) 2895 error = ESTALE; 2896 break; 2897 } 2898 2899 /* 2900 * Don't create dirty pages faster than they 2901 * can be cleaned so that the system doesn't 2902 * get imbalanced. If the async queue is 2903 * maxed out, then wait for it to drain before 2904 * creating more dirty pages. Also, wait for 2905 * any threads doing pagewalks in the vop_getattr 2906 * entry points so that they don't block for 2907 * long periods. 2908 */ 2909 mutex_enter(&rp->r_statelock); 2910 while ((mi->mi_max_threads != 0 && 2911 rp->r_awcount > 2 * mi->mi_max_threads) || 2912 rp->r_gcount > 0) { 2913 if (INTR4(vp)) { 2914 klwp_t *lwp = ttolwp(curthread); 2915 2916 if (lwp != NULL) 2917 lwp->lwp_nostop++; 2918 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2919 mutex_exit(&rp->r_statelock); 2920 if (lwp != NULL) 2921 lwp->lwp_nostop--; 2922 error = EINTR; 2923 goto bottom; 2924 } 2925 if (lwp != NULL) 2926 lwp->lwp_nostop--; 2927 } else 2928 cv_wait(&rp->r_cv, &rp->r_statelock); 2929 } 2930 mutex_exit(&rp->r_statelock); 2931 2932 /* 2933 * Touch the page and fault it in if it is not in core 2934 * before segmap_getmapflt or vpm_data_copy can lock it. 2935 * This is to avoid the deadlock if the buffer is mapped 2936 * to the same file through mmap which we want to write. 2937 */ 2938 uio_prefaultpages((long)n, uiop); 2939 2940 if (vpm_enable) { 2941 /* 2942 * It will use kpm mappings, so no need to 2943 * pass an address. 2944 */ 2945 error = writerp4(rp, NULL, n, uiop, 0); 2946 } else { 2947 if (segmap_kpm) { 2948 int pon = uiop->uio_loffset & PAGEOFFSET; 2949 size_t pn = MIN(PAGESIZE - pon, 2950 uiop->uio_resid); 2951 int pagecreate; 2952 2953 mutex_enter(&rp->r_statelock); 2954 pagecreate = (pon == 0) && (pn == PAGESIZE || 2955 uiop->uio_loffset + pn >= rp->r_size); 2956 mutex_exit(&rp->r_statelock); 2957 2958 base = segmap_getmapflt(segkmap, vp, off + on, 2959 pn, !pagecreate, S_WRITE); 2960 2961 error = writerp4(rp, base + pon, n, uiop, 2962 pagecreate); 2963 2964 } else { 2965 base = segmap_getmapflt(segkmap, vp, off + on, 2966 n, 0, S_READ); 2967 error = writerp4(rp, base + on, n, uiop, 0); 2968 } 2969 } 2970 2971 if (!error) { 2972 if (mi->mi_flags & MI4_NOAC) 2973 flags = SM_WRITE; 2974 else if ((uiop->uio_loffset % bsize) == 0 || 2975 IS_SWAPVP(vp)) { 2976 /* 2977 * Have written a whole block. 2978 * Start an asynchronous write 2979 * and mark the buffer to 2980 * indicate that it won't be 2981 * needed again soon. 2982 */ 2983 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2984 } else 2985 flags = 0; 2986 if ((ioflag & (FSYNC|FDSYNC)) || 2987 (rp->r_flags & R4OUTOFSPACE)) { 2988 flags &= ~SM_ASYNC; 2989 flags |= SM_WRITE; 2990 } 2991 if (vpm_enable) { 2992 error = vpm_sync_pages(vp, off, n, flags); 2993 } else { 2994 error = segmap_release(segkmap, base, flags); 2995 } 2996 } else { 2997 if (vpm_enable) { 2998 (void) vpm_sync_pages(vp, off, n, 0); 2999 } else { 3000 (void) segmap_release(segkmap, base, 0); 3001 } 3002 /* 3003 * In the event that we got an access error while 3004 * faulting in a page for a write-only file just 3005 * force a write. 3006 */ 3007 if (error == EACCES) 3008 goto nfs4_fwrite; 3009 } 3010 } while (!error && uiop->uio_resid > 0); 3011 3012 bottom: 3013 if (error) { 3014 uiop->uio_resid = resid + remainder; 3015 uiop->uio_loffset = offset; 3016 } else { 3017 uiop->uio_resid += remainder; 3018 3019 mutex_enter(&rp->r_statev4_lock); 3020 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 3021 gethrestime(&rp->r_attr.va_mtime); 3022 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3023 } 3024 mutex_exit(&rp->r_statev4_lock); 3025 } 3026 3027 nfs_rw_exit(&rp->r_lkserlock); 3028 3029 return (error); 3030 } 3031 3032 /* 3033 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 3034 */ 3035 static int 3036 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 3037 int flags, cred_t *cr) 3038 { 3039 struct buf *bp; 3040 int error; 3041 page_t *savepp; 3042 uchar_t fsdata; 3043 stable_how4 stab_comm; 3044 3045 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3046 bp = pageio_setup(pp, len, vp, flags); 3047 ASSERT(bp != NULL); 3048 3049 /* 3050 * pageio_setup should have set b_addr to 0. This 3051 * is correct since we want to do I/O on a page 3052 * boundary. bp_mapin will use this addr to calculate 3053 * an offset, and then set b_addr to the kernel virtual 3054 * address it allocated for us. 3055 */ 3056 ASSERT(bp->b_un.b_addr == 0); 3057 3058 bp->b_edev = 0; 3059 bp->b_dev = 0; 3060 bp->b_lblkno = lbtodb(off); 3061 bp->b_file = vp; 3062 bp->b_offset = (offset_t)off; 3063 bp_mapin(bp); 3064 3065 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 3066 freemem > desfree) 3067 stab_comm = UNSTABLE4; 3068 else 3069 stab_comm = FILE_SYNC4; 3070 3071 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 3072 3073 bp_mapout(bp); 3074 pageio_done(bp); 3075 3076 if (stab_comm == UNSTABLE4) 3077 fsdata = C_DELAYCOMMIT; 3078 else 3079 fsdata = C_NOCOMMIT; 3080 3081 savepp = pp; 3082 do { 3083 pp->p_fsdata = fsdata; 3084 } while ((pp = pp->p_next) != savepp); 3085 3086 return (error); 3087 } 3088 3089 /* 3090 */ 3091 static int 3092 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3093 { 3094 nfs4_open_owner_t *oop; 3095 nfs4_open_stream_t *osp; 3096 rnode4_t *rp = VTOR4(vp); 3097 mntinfo4_t *mi = VTOMI4(vp); 3098 int reopen_needed; 3099 3100 ASSERT(nfs_zone() == mi->mi_zone); 3101 3102 3103 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3104 if (!oop) 3105 return (EIO); 3106 3107 /* returns with 'os_sync_lock' held */ 3108 osp = find_open_stream(oop, rp); 3109 if (!osp) { 3110 open_owner_rele(oop); 3111 return (EIO); 3112 } 3113 3114 if (osp->os_failed_reopen) { 3115 mutex_exit(&osp->os_sync_lock); 3116 open_stream_rele(osp, rp); 3117 open_owner_rele(oop); 3118 return (EIO); 3119 } 3120 3121 /* 3122 * Determine whether a reopen is needed. If this 3123 * is a delegation open stream, then the os_delegation bit 3124 * should be set. 3125 */ 3126 3127 reopen_needed = osp->os_delegation; 3128 3129 mutex_exit(&osp->os_sync_lock); 3130 open_owner_rele(oop); 3131 3132 if (reopen_needed) { 3133 nfs4_error_zinit(ep); 3134 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3135 mutex_enter(&osp->os_sync_lock); 3136 if (ep->error || ep->stat || osp->os_failed_reopen) { 3137 mutex_exit(&osp->os_sync_lock); 3138 open_stream_rele(osp, rp); 3139 return (EIO); 3140 } 3141 mutex_exit(&osp->os_sync_lock); 3142 } 3143 open_stream_rele(osp, rp); 3144 3145 return (0); 3146 } 3147 3148 /* 3149 * Write to file. Writes to remote server in largest size 3150 * chunks that the server can handle. Write is synchronous. 3151 */ 3152 static int 3153 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3154 stable_how4 *stab_comm) 3155 { 3156 mntinfo4_t *mi; 3157 COMPOUND4args_clnt args; 3158 COMPOUND4res_clnt res; 3159 WRITE4args *wargs; 3160 WRITE4res *wres; 3161 nfs_argop4 argop[2]; 3162 nfs_resop4 *resop; 3163 int tsize; 3164 stable_how4 stable; 3165 rnode4_t *rp; 3166 int doqueue = 1; 3167 bool_t needrecov; 3168 nfs4_recov_state_t recov_state; 3169 nfs4_stateid_types_t sid_types; 3170 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3171 int recov; 3172 3173 rp = VTOR4(vp); 3174 mi = VTOMI4(vp); 3175 3176 ASSERT(nfs_zone() == mi->mi_zone); 3177 3178 stable = *stab_comm; 3179 *stab_comm = FILE_SYNC4; 3180 3181 needrecov = FALSE; 3182 recov_state.rs_flags = 0; 3183 recov_state.rs_num_retry_despite_err = 0; 3184 nfs4_init_stateid_types(&sid_types); 3185 3186 /* Is curthread the recovery thread? */ 3187 mutex_enter(&mi->mi_lock); 3188 recov = (mi->mi_recovthread == curthread); 3189 mutex_exit(&mi->mi_lock); 3190 3191 recov_retry: 3192 args.ctag = TAG_WRITE; 3193 args.array_len = 2; 3194 args.array = argop; 3195 3196 if (!recov) { 3197 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3198 &recov_state, NULL); 3199 if (e.error) 3200 return (e.error); 3201 } 3202 3203 /* 0. putfh target fh */ 3204 argop[0].argop = OP_CPUTFH; 3205 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3206 3207 /* 1. write */ 3208 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3209 3210 do { 3211 3212 wargs->offset = (offset4)offset; 3213 wargs->data_val = base; 3214 3215 if (mi->mi_io_kstats) { 3216 mutex_enter(&mi->mi_lock); 3217 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3218 mutex_exit(&mi->mi_lock); 3219 } 3220 3221 if ((vp->v_flag & VNOCACHE) || 3222 (rp->r_flags & R4DIRECTIO) || 3223 (mi->mi_flags & MI4_DIRECTIO)) 3224 tsize = MIN(mi->mi_stsize, count); 3225 else 3226 tsize = MIN(mi->mi_curwrite, count); 3227 wargs->data_len = (uint_t)tsize; 3228 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3229 3230 if (mi->mi_io_kstats) { 3231 mutex_enter(&mi->mi_lock); 3232 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3233 mutex_exit(&mi->mi_lock); 3234 } 3235 3236 if (!recov) { 3237 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3238 if (e.error && !needrecov) { 3239 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3240 &recov_state, needrecov); 3241 return (e.error); 3242 } 3243 } else { 3244 if (e.error) 3245 return (e.error); 3246 } 3247 3248 /* 3249 * Do handling of OLD_STATEID outside 3250 * of the normal recovery framework. 3251 * 3252 * If write receives a BAD stateid error while using a 3253 * delegation stateid, retry using the open stateid (if it 3254 * exists). If it doesn't have an open stateid, reopen the 3255 * file first, then retry. 3256 */ 3257 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3258 sid_types.cur_sid_type != SPEC_SID) { 3259 nfs4_save_stateid(&wargs->stateid, &sid_types); 3260 if (!recov) 3261 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3262 &recov_state, needrecov); 3263 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3264 goto recov_retry; 3265 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3266 sid_types.cur_sid_type == DEL_SID) { 3267 nfs4_save_stateid(&wargs->stateid, &sid_types); 3268 mutex_enter(&rp->r_statev4_lock); 3269 rp->r_deleg_return_pending = TRUE; 3270 mutex_exit(&rp->r_statev4_lock); 3271 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3272 if (!recov) 3273 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3274 &recov_state, needrecov); 3275 (void) xdr_free(xdr_COMPOUND4res_clnt, 3276 (caddr_t)&res); 3277 return (EIO); 3278 } 3279 if (!recov) 3280 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3281 &recov_state, needrecov); 3282 /* hold needed for nfs4delegreturn_thread */ 3283 VN_HOLD(vp); 3284 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3285 NFS4_DR_DISCARD), FALSE); 3286 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3287 goto recov_retry; 3288 } 3289 3290 if (needrecov) { 3291 bool_t abort; 3292 3293 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3294 "nfs4write: client got error %d, res.status %d" 3295 ", so start recovery", e.error, res.status)); 3296 3297 abort = nfs4_start_recovery(&e, 3298 VTOMI4(vp), vp, NULL, &wargs->stateid, 3299 NULL, OP_WRITE, NULL, NULL, NULL); 3300 if (!e.error) { 3301 e.error = geterrno4(res.status); 3302 (void) xdr_free(xdr_COMPOUND4res_clnt, 3303 (caddr_t)&res); 3304 } 3305 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3306 &recov_state, needrecov); 3307 if (abort == FALSE) 3308 goto recov_retry; 3309 return (e.error); 3310 } 3311 3312 if (res.status) { 3313 e.error = geterrno4(res.status); 3314 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3315 if (!recov) 3316 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3317 &recov_state, needrecov); 3318 return (e.error); 3319 } 3320 3321 resop = &res.array[1]; /* write res */ 3322 wres = &resop->nfs_resop4_u.opwrite; 3323 3324 if ((int)wres->count > tsize) { 3325 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3326 3327 zcmn_err(getzoneid(), CE_WARN, 3328 "nfs4write: server wrote %u, requested was %u", 3329 (int)wres->count, tsize); 3330 if (!recov) 3331 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3332 &recov_state, needrecov); 3333 return (EIO); 3334 } 3335 if (wres->committed == UNSTABLE4) { 3336 *stab_comm = UNSTABLE4; 3337 if (wargs->stable == DATA_SYNC4 || 3338 wargs->stable == FILE_SYNC4) { 3339 (void) xdr_free(xdr_COMPOUND4res_clnt, 3340 (caddr_t)&res); 3341 zcmn_err(getzoneid(), CE_WARN, 3342 "nfs4write: server %s did not commit " 3343 "to stable storage", 3344 rp->r_server->sv_hostname); 3345 if (!recov) 3346 nfs4_end_fop(VTOMI4(vp), vp, NULL, 3347 OH_WRITE, &recov_state, needrecov); 3348 return (EIO); 3349 } 3350 } 3351 3352 tsize = (int)wres->count; 3353 count -= tsize; 3354 base += tsize; 3355 offset += tsize; 3356 if (mi->mi_io_kstats) { 3357 mutex_enter(&mi->mi_lock); 3358 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3359 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3360 tsize; 3361 mutex_exit(&mi->mi_lock); 3362 } 3363 lwp_stat_update(LWP_STAT_OUBLK, 1); 3364 mutex_enter(&rp->r_statelock); 3365 if (rp->r_flags & R4HAVEVERF) { 3366 if (rp->r_writeverf != wres->writeverf) { 3367 nfs4_set_mod(vp); 3368 rp->r_writeverf = wres->writeverf; 3369 } 3370 } else { 3371 rp->r_writeverf = wres->writeverf; 3372 rp->r_flags |= R4HAVEVERF; 3373 } 3374 PURGE_ATTRCACHE4_LOCKED(rp); 3375 rp->r_flags |= R4WRITEMODIFIED; 3376 gethrestime(&rp->r_attr.va_mtime); 3377 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3378 mutex_exit(&rp->r_statelock); 3379 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3380 } while (count); 3381 3382 if (!recov) 3383 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, 3384 needrecov); 3385 3386 return (e.error); 3387 } 3388 3389 /* 3390 * Read from a file. Reads data in largest chunks our interface can handle. 3391 */ 3392 static int 3393 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3394 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3395 { 3396 mntinfo4_t *mi; 3397 COMPOUND4args_clnt args; 3398 COMPOUND4res_clnt res; 3399 READ4args *rargs; 3400 nfs_argop4 argop[2]; 3401 int tsize; 3402 int doqueue; 3403 rnode4_t *rp; 3404 int data_len; 3405 bool_t is_eof; 3406 bool_t needrecov = FALSE; 3407 nfs4_recov_state_t recov_state; 3408 nfs4_stateid_types_t sid_types; 3409 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3410 3411 rp = VTOR4(vp); 3412 mi = VTOMI4(vp); 3413 doqueue = 1; 3414 3415 ASSERT(nfs_zone() == mi->mi_zone); 3416 3417 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3418 3419 args.array_len = 2; 3420 args.array = argop; 3421 3422 nfs4_init_stateid_types(&sid_types); 3423 3424 recov_state.rs_flags = 0; 3425 recov_state.rs_num_retry_despite_err = 0; 3426 3427 recov_retry: 3428 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3429 &recov_state, NULL); 3430 if (e.error) 3431 return (e.error); 3432 3433 /* putfh target fh */ 3434 argop[0].argop = OP_CPUTFH; 3435 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3436 3437 /* read */ 3438 argop[1].argop = OP_READ; 3439 rargs = &argop[1].nfs_argop4_u.opread; 3440 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3441 OP_READ, &sid_types, async); 3442 3443 do { 3444 if (mi->mi_io_kstats) { 3445 mutex_enter(&mi->mi_lock); 3446 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3447 mutex_exit(&mi->mi_lock); 3448 } 3449 3450 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3451 "nfs4read: %s call, rp %s", 3452 needrecov ? "recov" : "first", 3453 rnode4info(rp))); 3454 3455 if ((vp->v_flag & VNOCACHE) || 3456 (rp->r_flags & R4DIRECTIO) || 3457 (mi->mi_flags & MI4_DIRECTIO)) 3458 tsize = MIN(mi->mi_tsize, count); 3459 else 3460 tsize = MIN(mi->mi_curread, count); 3461 3462 rargs->offset = (offset4)offset; 3463 rargs->count = (count4)tsize; 3464 rargs->res_data_val_alt = NULL; 3465 rargs->res_mblk = NULL; 3466 rargs->res_uiop = NULL; 3467 rargs->res_maxsize = 0; 3468 rargs->wlist = NULL; 3469 3470 if (uiop) 3471 rargs->res_uiop = uiop; 3472 else 3473 rargs->res_data_val_alt = base; 3474 rargs->res_maxsize = tsize; 3475 3476 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3477 #ifdef DEBUG 3478 if (nfs4read_error_inject) { 3479 res.status = nfs4read_error_inject; 3480 nfs4read_error_inject = 0; 3481 } 3482 #endif 3483 3484 if (mi->mi_io_kstats) { 3485 mutex_enter(&mi->mi_lock); 3486 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3487 mutex_exit(&mi->mi_lock); 3488 } 3489 3490 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3491 if (e.error != 0 && !needrecov) { 3492 nfs4_end_fop(mi, vp, NULL, OH_READ, 3493 &recov_state, needrecov); 3494 return (e.error); 3495 } 3496 3497 /* 3498 * Do proper retry for OLD and BAD stateid errors outside 3499 * of the normal recovery framework. There are two differences 3500 * between async and sync reads. The first is that we allow 3501 * retry on BAD_STATEID for async reads, but not sync reads. 3502 * The second is that we mark the file dead for a failed 3503 * attempt with a special stateid for sync reads, but just 3504 * return EIO for async reads. 3505 * 3506 * If a sync read receives a BAD stateid error while using a 3507 * delegation stateid, retry using the open stateid (if it 3508 * exists). If it doesn't have an open stateid, reopen the 3509 * file first, then retry. 3510 */ 3511 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3512 res.status == NFS4ERR_BAD_STATEID) && async) { 3513 nfs4_end_fop(mi, vp, NULL, OH_READ, 3514 &recov_state, needrecov); 3515 if (sid_types.cur_sid_type == SPEC_SID) { 3516 (void) xdr_free(xdr_COMPOUND4res_clnt, 3517 (caddr_t)&res); 3518 return (EIO); 3519 } 3520 nfs4_save_stateid(&rargs->stateid, &sid_types); 3521 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3522 goto recov_retry; 3523 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3524 !async && sid_types.cur_sid_type != SPEC_SID) { 3525 nfs4_save_stateid(&rargs->stateid, &sid_types); 3526 nfs4_end_fop(mi, vp, NULL, OH_READ, 3527 &recov_state, needrecov); 3528 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3529 goto recov_retry; 3530 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3531 sid_types.cur_sid_type == DEL_SID) { 3532 nfs4_save_stateid(&rargs->stateid, &sid_types); 3533 mutex_enter(&rp->r_statev4_lock); 3534 rp->r_deleg_return_pending = TRUE; 3535 mutex_exit(&rp->r_statev4_lock); 3536 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3537 nfs4_end_fop(mi, vp, NULL, OH_READ, 3538 &recov_state, needrecov); 3539 (void) xdr_free(xdr_COMPOUND4res_clnt, 3540 (caddr_t)&res); 3541 return (EIO); 3542 } 3543 nfs4_end_fop(mi, vp, NULL, OH_READ, 3544 &recov_state, needrecov); 3545 /* hold needed for nfs4delegreturn_thread */ 3546 VN_HOLD(vp); 3547 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3548 NFS4_DR_DISCARD), FALSE); 3549 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3550 goto recov_retry; 3551 } 3552 if (needrecov) { 3553 bool_t abort; 3554 3555 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3556 "nfs4read: initiating recovery\n")); 3557 abort = nfs4_start_recovery(&e, 3558 mi, vp, NULL, &rargs->stateid, 3559 NULL, OP_READ, NULL, NULL, NULL); 3560 nfs4_end_fop(mi, vp, NULL, OH_READ, 3561 &recov_state, needrecov); 3562 /* 3563 * Do not retry if we got OLD_STATEID using a special 3564 * stateid. This avoids looping with a broken server. 3565 */ 3566 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3567 sid_types.cur_sid_type == SPEC_SID) 3568 abort = TRUE; 3569 3570 if (abort == FALSE) { 3571 /* 3572 * Need to retry all possible stateids in 3573 * case the recovery error wasn't stateid 3574 * related or the stateids have become 3575 * stale (server reboot). 3576 */ 3577 nfs4_init_stateid_types(&sid_types); 3578 (void) xdr_free(xdr_COMPOUND4res_clnt, 3579 (caddr_t)&res); 3580 goto recov_retry; 3581 } 3582 3583 if (!e.error) { 3584 e.error = geterrno4(res.status); 3585 (void) xdr_free(xdr_COMPOUND4res_clnt, 3586 (caddr_t)&res); 3587 } 3588 return (e.error); 3589 } 3590 3591 if (res.status) { 3592 e.error = geterrno4(res.status); 3593 nfs4_end_fop(mi, vp, NULL, OH_READ, 3594 &recov_state, needrecov); 3595 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3596 return (e.error); 3597 } 3598 3599 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3600 count -= data_len; 3601 if (base) 3602 base += data_len; 3603 offset += data_len; 3604 if (mi->mi_io_kstats) { 3605 mutex_enter(&mi->mi_lock); 3606 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3607 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3608 mutex_exit(&mi->mi_lock); 3609 } 3610 lwp_stat_update(LWP_STAT_INBLK, 1); 3611 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3612 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3613 3614 } while (count && !is_eof); 3615 3616 *residp = count; 3617 3618 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3619 3620 return (e.error); 3621 } 3622 3623 /* ARGSUSED */ 3624 static int 3625 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp, 3626 caller_context_t *ct) 3627 { 3628 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3629 return (EIO); 3630 switch (cmd) { 3631 case _FIODIRECTIO: 3632 return (nfs4_directio(vp, (int)arg, cr)); 3633 default: 3634 return (ENOTTY); 3635 } 3636 } 3637 3638 /* ARGSUSED */ 3639 int 3640 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3641 caller_context_t *ct) 3642 { 3643 int error; 3644 rnode4_t *rp = VTOR4(vp); 3645 3646 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3647 return (EIO); 3648 /* 3649 * If it has been specified that the return value will 3650 * just be used as a hint, and we are only being asked 3651 * for size, fsid or rdevid, then return the client's 3652 * notion of these values without checking to make sure 3653 * that the attribute cache is up to date. 3654 * The whole point is to avoid an over the wire GETATTR 3655 * call. 3656 */ 3657 if (flags & ATTR_HINT) { 3658 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) { 3659 mutex_enter(&rp->r_statelock); 3660 if (vap->va_mask & AT_SIZE) 3661 vap->va_size = rp->r_size; 3662 if (vap->va_mask & AT_FSID) 3663 vap->va_fsid = rp->r_attr.va_fsid; 3664 if (vap->va_mask & AT_RDEV) 3665 vap->va_rdev = rp->r_attr.va_rdev; 3666 mutex_exit(&rp->r_statelock); 3667 return (0); 3668 } 3669 } 3670 3671 /* 3672 * Only need to flush pages if asking for the mtime 3673 * and if there any dirty pages or any outstanding 3674 * asynchronous (write) requests for this file. 3675 */ 3676 if (vap->va_mask & AT_MTIME) { 3677 rp = VTOR4(vp); 3678 if (nfs4_has_pages(vp)) { 3679 mutex_enter(&rp->r_statev4_lock); 3680 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3681 mutex_exit(&rp->r_statev4_lock); 3682 if (rp->r_flags & R4DIRTY || 3683 rp->r_awcount > 0) { 3684 mutex_enter(&rp->r_statelock); 3685 rp->r_gcount++; 3686 mutex_exit(&rp->r_statelock); 3687 error = 3688 nfs4_putpage(vp, (u_offset_t)0, 3689 0, 0, cr, NULL); 3690 mutex_enter(&rp->r_statelock); 3691 if (error && (error == ENOSPC || 3692 error == EDQUOT)) { 3693 if (!rp->r_error) 3694 rp->r_error = error; 3695 } 3696 if (--rp->r_gcount == 0) 3697 cv_broadcast(&rp->r_cv); 3698 mutex_exit(&rp->r_statelock); 3699 } 3700 } else { 3701 mutex_exit(&rp->r_statev4_lock); 3702 } 3703 } 3704 } 3705 return (nfs4getattr(vp, vap, cr)); 3706 } 3707 3708 int 3709 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3710 { 3711 /* 3712 * If these are the only two bits cleared 3713 * on the server then return 0 (OK) else 3714 * return 1 (BAD). 3715 */ 3716 on_client &= ~(S_ISUID|S_ISGID); 3717 if (on_client == from_server) 3718 return (0); 3719 else 3720 return (1); 3721 } 3722 3723 /*ARGSUSED4*/ 3724 static int 3725 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3726 caller_context_t *ct) 3727 { 3728 int error; 3729 3730 if (vap->va_mask & AT_NOSET) 3731 return (EINVAL); 3732 3733 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3734 return (EIO); 3735 3736 /* 3737 * Don't call secpolicy_vnode_setattr, the client cannot 3738 * use its cached attributes to make security decisions 3739 * as the server may be faking mode bits or mapping uid/gid. 3740 * Always just let the server to the checking. 3741 * If we provide the ability to remove basic priviledges 3742 * to setattr (e.g. basic without chmod) then we will 3743 * need to add a check here before calling the server. 3744 */ 3745 error = nfs4setattr(vp, vap, flags, cr, NULL); 3746 3747 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0) 3748 vnevent_truncate(vp, ct); 3749 3750 return (error); 3751 } 3752 3753 /* 3754 * To replace the "guarded" version 3 setattr, we use two types of compound 3755 * setattr requests: 3756 * 1. The "normal" setattr, used when the size of the file isn't being 3757 * changed - { Putfh <fh>; Setattr; Getattr }/ 3758 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3759 * with only ctime as the argument. If the server ctime differs from 3760 * what is cached on the client, the verify will fail, but we would 3761 * already have the ctime from the preceding getattr, so just set it 3762 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3763 * Setattr; Getattr }. 3764 * 3765 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3766 * this setattr and NULL if they are not. 3767 */ 3768 static int 3769 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3770 vsecattr_t *vsap) 3771 { 3772 COMPOUND4args_clnt args; 3773 COMPOUND4res_clnt res, *resp = NULL; 3774 nfs4_ga_res_t *garp = NULL; 3775 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3776 nfs_argop4 argop[5]; 3777 int verify_argop = -1; 3778 int setattr_argop = 1; 3779 nfs_resop4 *resop; 3780 vattr_t va; 3781 rnode4_t *rp; 3782 int doqueue = 1; 3783 uint_t mask = vap->va_mask; 3784 mode_t omode; 3785 vsecattr_t *vsp; 3786 timestruc_t ctime; 3787 bool_t needrecov = FALSE; 3788 nfs4_recov_state_t recov_state; 3789 nfs4_stateid_types_t sid_types; 3790 stateid4 stateid; 3791 hrtime_t t; 3792 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3793 servinfo4_t *svp; 3794 bitmap4 supp_attrs; 3795 3796 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3797 rp = VTOR4(vp); 3798 nfs4_init_stateid_types(&sid_types); 3799 3800 /* 3801 * Only need to flush pages if there are any pages and 3802 * if the file is marked as dirty in some fashion. The 3803 * file must be flushed so that we can accurately 3804 * determine the size of the file and the cached data 3805 * after the SETATTR returns. A file is considered to 3806 * be dirty if it is either marked with R4DIRTY, has 3807 * outstanding i/o's active, or is mmap'd. In this 3808 * last case, we can't tell whether there are dirty 3809 * pages, so we flush just to be sure. 3810 */ 3811 if (nfs4_has_pages(vp) && 3812 ((rp->r_flags & R4DIRTY) || 3813 rp->r_count > 0 || 3814 rp->r_mapcnt > 0)) { 3815 ASSERT(vp->v_type != VCHR); 3816 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL); 3817 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3818 mutex_enter(&rp->r_statelock); 3819 if (!rp->r_error) 3820 rp->r_error = e.error; 3821 mutex_exit(&rp->r_statelock); 3822 } 3823 } 3824 3825 if (mask & AT_SIZE) { 3826 /* 3827 * Verification setattr compound for non-deleg AT_SIZE: 3828 * { Putfh; Getattr; Verify; Setattr; Getattr } 3829 * Set ctime local here (outside the do_again label) 3830 * so that subsequent retries (after failed VERIFY) 3831 * will use ctime from GETATTR results (from failed 3832 * verify compound) as VERIFY arg. 3833 * If file has delegation, then VERIFY(time_metadata) 3834 * is of little added value, so don't bother. 3835 */ 3836 mutex_enter(&rp->r_statev4_lock); 3837 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3838 rp->r_deleg_return_pending) { 3839 numops = 5; 3840 ctime = rp->r_attr.va_ctime; 3841 } 3842 mutex_exit(&rp->r_statev4_lock); 3843 } 3844 3845 recov_state.rs_flags = 0; 3846 recov_state.rs_num_retry_despite_err = 0; 3847 3848 args.ctag = TAG_SETATTR; 3849 do_again: 3850 recov_retry: 3851 setattr_argop = numops - 2; 3852 3853 args.array = argop; 3854 args.array_len = numops; 3855 3856 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3857 if (e.error) 3858 return (e.error); 3859 3860 3861 /* putfh target fh */ 3862 argop[0].argop = OP_CPUTFH; 3863 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3864 3865 if (numops == 5) { 3866 /* 3867 * We only care about the ctime, but need to get mtime 3868 * and size for proper cache update. 3869 */ 3870 /* getattr */ 3871 argop[1].argop = OP_GETATTR; 3872 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3873 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3874 3875 /* verify - set later in loop */ 3876 verify_argop = 2; 3877 } 3878 3879 /* setattr */ 3880 svp = rp->r_server; 3881 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3882 supp_attrs = svp->sv_supp_attrs; 3883 nfs_rw_exit(&svp->sv_lock); 3884 3885 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3886 supp_attrs, &e.error, &sid_types); 3887 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3888 if (e.error) { 3889 /* req time field(s) overflow - return immediately */ 3890 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3891 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3892 opsetattr.obj_attributes); 3893 return (e.error); 3894 } 3895 omode = rp->r_attr.va_mode; 3896 3897 /* getattr */ 3898 argop[numops-1].argop = OP_GETATTR; 3899 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3900 /* 3901 * If we are setting the ACL (indicated only by vsap != NULL), request 3902 * the ACL in this getattr. The ACL returned from this getattr will be 3903 * used in updating the ACL cache. 3904 */ 3905 if (vsap != NULL) 3906 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3907 FATTR4_ACL_MASK; 3908 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3909 3910 /* 3911 * setattr iterates if the object size is set and the cached ctime 3912 * does not match the file ctime. In that case, verify the ctime first. 3913 */ 3914 3915 do { 3916 if (verify_argop != -1) { 3917 /* 3918 * Verify that the ctime match before doing setattr. 3919 */ 3920 va.va_mask = AT_CTIME; 3921 va.va_ctime = ctime; 3922 svp = rp->r_server; 3923 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3924 supp_attrs = svp->sv_supp_attrs; 3925 nfs_rw_exit(&svp->sv_lock); 3926 e.error = nfs4args_verify(&argop[verify_argop], &va, 3927 OP_VERIFY, supp_attrs); 3928 if (e.error) { 3929 /* req time field(s) overflow - return */ 3930 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3931 needrecov); 3932 break; 3933 } 3934 } 3935 3936 doqueue = 1; 3937 3938 t = gethrtime(); 3939 3940 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3941 3942 /* 3943 * Purge the access cache and ACL cache if changing either the 3944 * owner of the file, the group owner, or the mode. These may 3945 * change the access permissions of the file, so purge old 3946 * information and start over again. 3947 */ 3948 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3949 (void) nfs4_access_purge_rp(rp); 3950 if (rp->r_secattr != NULL) { 3951 mutex_enter(&rp->r_statelock); 3952 vsp = rp->r_secattr; 3953 rp->r_secattr = NULL; 3954 mutex_exit(&rp->r_statelock); 3955 if (vsp != NULL) 3956 nfs4_acl_free_cache(vsp); 3957 } 3958 } 3959 3960 /* 3961 * If res.array_len == numops, then everything succeeded, 3962 * except for possibly the final getattr. If only the 3963 * last getattr failed, give up, and don't try recovery. 3964 */ 3965 if (res.array_len == numops) { 3966 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3967 needrecov); 3968 if (! e.error) 3969 resp = &res; 3970 break; 3971 } 3972 3973 /* 3974 * if either rpc call failed or completely succeeded - done 3975 */ 3976 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3977 if (e.error) { 3978 PURGE_ATTRCACHE4(vp); 3979 if (!needrecov) { 3980 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3981 needrecov); 3982 break; 3983 } 3984 } 3985 3986 /* 3987 * Do proper retry for OLD_STATEID outside of the normal 3988 * recovery framework. 3989 */ 3990 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3991 sid_types.cur_sid_type != SPEC_SID && 3992 sid_types.cur_sid_type != NO_SID) { 3993 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3994 needrecov); 3995 nfs4_save_stateid(&stateid, &sid_types); 3996 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3997 opsetattr.obj_attributes); 3998 if (verify_argop != -1) { 3999 nfs4args_verify_free(&argop[verify_argop]); 4000 verify_argop = -1; 4001 } 4002 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4003 goto recov_retry; 4004 } 4005 4006 if (needrecov) { 4007 bool_t abort; 4008 4009 abort = nfs4_start_recovery(&e, 4010 VTOMI4(vp), vp, NULL, NULL, NULL, 4011 OP_SETATTR, NULL, NULL, NULL); 4012 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4013 needrecov); 4014 /* 4015 * Do not retry if we failed with OLD_STATEID using 4016 * a special stateid. This is done to avoid looping 4017 * with a broken server. 4018 */ 4019 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 4020 (sid_types.cur_sid_type == SPEC_SID || 4021 sid_types.cur_sid_type == NO_SID)) 4022 abort = TRUE; 4023 if (!e.error) { 4024 if (res.status == NFS4ERR_BADOWNER) 4025 nfs4_log_badowner(VTOMI4(vp), 4026 OP_SETATTR); 4027 4028 e.error = geterrno4(res.status); 4029 (void) xdr_free(xdr_COMPOUND4res_clnt, 4030 (caddr_t)&res); 4031 } 4032 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4033 opsetattr.obj_attributes); 4034 if (verify_argop != -1) { 4035 nfs4args_verify_free(&argop[verify_argop]); 4036 verify_argop = -1; 4037 } 4038 if (abort == FALSE) { 4039 /* 4040 * Need to retry all possible stateids in 4041 * case the recovery error wasn't stateid 4042 * related or the stateids have become 4043 * stale (server reboot). 4044 */ 4045 nfs4_init_stateid_types(&sid_types); 4046 goto recov_retry; 4047 } 4048 return (e.error); 4049 } 4050 4051 /* 4052 * Need to call nfs4_end_op before nfs4getattr to 4053 * avoid potential nfs4_start_op deadlock. See RFE 4054 * 4777612. Calls to nfs4_invalidate_pages() and 4055 * nfs4_purge_stale_fh() might also generate over the 4056 * wire calls which my cause nfs4_start_op() deadlock. 4057 */ 4058 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4059 4060 /* 4061 * Check to update lease. 4062 */ 4063 resp = &res; 4064 if (res.status == NFS4_OK) { 4065 break; 4066 } 4067 4068 /* 4069 * Check if verify failed to see if try again 4070 */ 4071 if ((verify_argop == -1) || (res.array_len != 3)) { 4072 /* 4073 * can't continue... 4074 */ 4075 if (res.status == NFS4ERR_BADOWNER) 4076 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 4077 4078 e.error = geterrno4(res.status); 4079 } else { 4080 /* 4081 * When the verify request fails, the client ctime is 4082 * not in sync with the server. This is the same as 4083 * the version 3 "not synchronized" error, and we 4084 * handle it in a similar manner (XXX do we need to???). 4085 * Use the ctime returned in the first getattr for 4086 * the input to the next verify. 4087 * If we couldn't get the attributes, then we give up 4088 * because we can't complete the operation as required. 4089 */ 4090 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 4091 } 4092 if (e.error) { 4093 PURGE_ATTRCACHE4(vp); 4094 nfs4_purge_stale_fh(e.error, vp, cr); 4095 } else { 4096 /* 4097 * retry with a new verify value 4098 */ 4099 ctime = garp->n4g_va.va_ctime; 4100 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4101 resp = NULL; 4102 } 4103 if (!e.error) { 4104 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4105 opsetattr.obj_attributes); 4106 if (verify_argop != -1) { 4107 nfs4args_verify_free(&argop[verify_argop]); 4108 verify_argop = -1; 4109 } 4110 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4111 goto do_again; 4112 } 4113 } while (!e.error); 4114 4115 if (e.error) { 4116 /* 4117 * If we are here, rfs4call has an irrecoverable error - return 4118 */ 4119 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4120 opsetattr.obj_attributes); 4121 if (verify_argop != -1) { 4122 nfs4args_verify_free(&argop[verify_argop]); 4123 verify_argop = -1; 4124 } 4125 if (resp) 4126 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4127 return (e.error); 4128 } 4129 4130 4131 4132 /* 4133 * If changing the size of the file, invalidate 4134 * any local cached data which is no longer part 4135 * of the file. We also possibly invalidate the 4136 * last page in the file. We could use 4137 * pvn_vpzero(), but this would mark the page as 4138 * modified and require it to be written back to 4139 * the server for no particularly good reason. 4140 * This way, if we access it, then we bring it 4141 * back in. A read should be cheaper than a 4142 * write. 4143 */ 4144 if (mask & AT_SIZE) { 4145 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4146 } 4147 4148 /* either no error or one of the postop getattr failed */ 4149 4150 /* 4151 * XXX Perform a simplified version of wcc checking. Instead of 4152 * have another getattr to get pre-op, just purge cache if 4153 * any of the ops prior to and including the getattr failed. 4154 * If the getattr succeeded then update the attrcache accordingly. 4155 */ 4156 4157 garp = NULL; 4158 if (res.status == NFS4_OK) { 4159 /* 4160 * Last getattr 4161 */ 4162 resop = &res.array[numops - 1]; 4163 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4164 } 4165 /* 4166 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4167 * rather than filling it. See the function itself for details. 4168 */ 4169 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4170 if (garp != NULL) { 4171 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4172 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4173 vs_ace4_destroy(&garp->n4g_vsa); 4174 } else { 4175 if (vsap != NULL) { 4176 /* 4177 * The ACL was supposed to be set and to be 4178 * returned in the last getattr of this 4179 * compound, but for some reason the getattr 4180 * result doesn't contain the ACL. In this 4181 * case, purge the ACL cache. 4182 */ 4183 if (rp->r_secattr != NULL) { 4184 mutex_enter(&rp->r_statelock); 4185 vsp = rp->r_secattr; 4186 rp->r_secattr = NULL; 4187 mutex_exit(&rp->r_statelock); 4188 if (vsp != NULL) 4189 nfs4_acl_free_cache(vsp); 4190 } 4191 } 4192 } 4193 } 4194 4195 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4196 /* 4197 * Set the size, rather than relying on getting it updated 4198 * via a GETATTR. With delegations the client tries to 4199 * suppress GETATTR calls. 4200 */ 4201 mutex_enter(&rp->r_statelock); 4202 rp->r_size = vap->va_size; 4203 mutex_exit(&rp->r_statelock); 4204 } 4205 4206 /* 4207 * Can free up request args and res 4208 */ 4209 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4210 opsetattr.obj_attributes); 4211 if (verify_argop != -1) { 4212 nfs4args_verify_free(&argop[verify_argop]); 4213 verify_argop = -1; 4214 } 4215 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4216 4217 /* 4218 * Some servers will change the mode to clear the setuid 4219 * and setgid bits when changing the uid or gid. The 4220 * client needs to compensate appropriately. 4221 */ 4222 if (mask & (AT_UID | AT_GID)) { 4223 int terror, do_setattr; 4224 4225 do_setattr = 0; 4226 va.va_mask = AT_MODE; 4227 terror = nfs4getattr(vp, &va, cr); 4228 if (!terror && 4229 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4230 (!(mask & AT_MODE) && va.va_mode != omode))) { 4231 va.va_mask = AT_MODE; 4232 if (mask & AT_MODE) { 4233 /* 4234 * We asked the mode to be changed and what 4235 * we just got from the server in getattr is 4236 * not what we wanted it to be, so set it now. 4237 */ 4238 va.va_mode = vap->va_mode; 4239 do_setattr = 1; 4240 } else { 4241 /* 4242 * We did not ask the mode to be changed, 4243 * Check to see that the server just cleared 4244 * I_SUID and I_GUID from it. If not then 4245 * set mode to omode with UID/GID cleared. 4246 */ 4247 if (nfs4_compare_modes(va.va_mode, omode)) { 4248 omode &= ~(S_ISUID|S_ISGID); 4249 va.va_mode = omode; 4250 do_setattr = 1; 4251 } 4252 } 4253 4254 if (do_setattr) 4255 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4256 } 4257 } 4258 4259 return (e.error); 4260 } 4261 4262 /* ARGSUSED */ 4263 static int 4264 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct) 4265 { 4266 COMPOUND4args_clnt args; 4267 COMPOUND4res_clnt res; 4268 int doqueue; 4269 uint32_t acc, resacc, argacc; 4270 rnode4_t *rp; 4271 cred_t *cred, *ncr, *ncrfree = NULL; 4272 nfs4_access_type_t cacc; 4273 int num_ops; 4274 nfs_argop4 argop[3]; 4275 nfs_resop4 *resop; 4276 bool_t needrecov = FALSE, do_getattr; 4277 nfs4_recov_state_t recov_state; 4278 int rpc_error; 4279 hrtime_t t; 4280 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4281 mntinfo4_t *mi = VTOMI4(vp); 4282 4283 if (nfs_zone() != mi->mi_zone) 4284 return (EIO); 4285 4286 acc = 0; 4287 if (mode & VREAD) 4288 acc |= ACCESS4_READ; 4289 if (mode & VWRITE) { 4290 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4291 return (EROFS); 4292 if (vp->v_type == VDIR) 4293 acc |= ACCESS4_DELETE; 4294 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4295 } 4296 if (mode & VEXEC) { 4297 if (vp->v_type == VDIR) 4298 acc |= ACCESS4_LOOKUP; 4299 else 4300 acc |= ACCESS4_EXECUTE; 4301 } 4302 4303 if (VTOR4(vp)->r_acache != NULL) { 4304 e.error = nfs4_validate_caches(vp, cr); 4305 if (e.error) 4306 return (e.error); 4307 } 4308 4309 rp = VTOR4(vp); 4310 if (vp->v_type == VDIR) 4311 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4312 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4313 else 4314 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4315 ACCESS4_EXECUTE; 4316 recov_state.rs_flags = 0; 4317 recov_state.rs_num_retry_despite_err = 0; 4318 4319 cred = cr; 4320 /* 4321 * ncr and ncrfree both initially 4322 * point to the memory area returned 4323 * by crnetadjust(); 4324 * ncrfree not NULL when exiting means 4325 * that we need to release it 4326 */ 4327 ncr = crnetadjust(cred); 4328 ncrfree = ncr; 4329 4330 tryagain: 4331 cacc = nfs4_access_check(rp, acc, cred); 4332 if (cacc == NFS4_ACCESS_ALLOWED) { 4333 if (ncrfree != NULL) 4334 crfree(ncrfree); 4335 return (0); 4336 } 4337 if (cacc == NFS4_ACCESS_DENIED) { 4338 /* 4339 * If the cred can be adjusted, try again 4340 * with the new cred. 4341 */ 4342 if (ncr != NULL) { 4343 cred = ncr; 4344 ncr = NULL; 4345 goto tryagain; 4346 } 4347 if (ncrfree != NULL) 4348 crfree(ncrfree); 4349 return (EACCES); 4350 } 4351 4352 recov_retry: 4353 /* 4354 * Don't take with r_statev4_lock here. r_deleg_type could 4355 * change as soon as lock is released. Since it is an int, 4356 * there is no atomicity issue. 4357 */ 4358 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4359 num_ops = do_getattr ? 3 : 2; 4360 4361 args.ctag = TAG_ACCESS; 4362 4363 args.array_len = num_ops; 4364 args.array = argop; 4365 4366 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4367 &recov_state, NULL)) { 4368 if (ncrfree != NULL) 4369 crfree(ncrfree); 4370 return (e.error); 4371 } 4372 4373 /* putfh target fh */ 4374 argop[0].argop = OP_CPUTFH; 4375 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4376 4377 /* access */ 4378 argop[1].argop = OP_ACCESS; 4379 argop[1].nfs_argop4_u.opaccess.access = argacc; 4380 4381 /* getattr */ 4382 if (do_getattr) { 4383 argop[2].argop = OP_GETATTR; 4384 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4385 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4386 } 4387 4388 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4389 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4390 rnode4info(VTOR4(vp)))); 4391 4392 doqueue = 1; 4393 t = gethrtime(); 4394 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4395 rpc_error = e.error; 4396 4397 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4398 if (needrecov) { 4399 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4400 "nfs4_access: initiating recovery\n")); 4401 4402 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4403 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) { 4404 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4405 &recov_state, needrecov); 4406 if (!e.error) 4407 (void) xdr_free(xdr_COMPOUND4res_clnt, 4408 (caddr_t)&res); 4409 goto recov_retry; 4410 } 4411 } 4412 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4413 4414 if (e.error) 4415 goto out; 4416 4417 if (res.status) { 4418 e.error = geterrno4(res.status); 4419 /* 4420 * This might generate over the wire calls throught 4421 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4422 * here to avoid a deadlock. 4423 */ 4424 nfs4_purge_stale_fh(e.error, vp, cr); 4425 goto out; 4426 } 4427 resop = &res.array[1]; /* access res */ 4428 4429 resacc = resop->nfs_resop4_u.opaccess.access; 4430 4431 if (do_getattr) { 4432 resop++; /* getattr res */ 4433 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4434 t, cr, FALSE, NULL); 4435 } 4436 4437 if (!e.error) { 4438 nfs4_access_cache(rp, argacc, resacc, cred); 4439 /* 4440 * we just cached results with cred; if cred is the 4441 * adjusted credentials from crnetadjust, we do not want 4442 * to release them before exiting: hence setting ncrfree 4443 * to NULL 4444 */ 4445 if (cred != cr) 4446 ncrfree = NULL; 4447 /* XXX check the supported bits too? */ 4448 if ((acc & resacc) != acc) { 4449 /* 4450 * The following code implements the semantic 4451 * that a setuid root program has *at least* the 4452 * permissions of the user that is running the 4453 * program. See rfs3call() for more portions 4454 * of the implementation of this functionality. 4455 */ 4456 /* XXX-LP */ 4457 if (ncr != NULL) { 4458 (void) xdr_free(xdr_COMPOUND4res_clnt, 4459 (caddr_t)&res); 4460 cred = ncr; 4461 ncr = NULL; 4462 goto tryagain; 4463 } 4464 e.error = EACCES; 4465 } 4466 } 4467 4468 out: 4469 if (!rpc_error) 4470 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4471 4472 if (ncrfree != NULL) 4473 crfree(ncrfree); 4474 4475 return (e.error); 4476 } 4477 4478 /* ARGSUSED */ 4479 static int 4480 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct) 4481 { 4482 COMPOUND4args_clnt args; 4483 COMPOUND4res_clnt res; 4484 int doqueue; 4485 rnode4_t *rp; 4486 nfs_argop4 argop[3]; 4487 nfs_resop4 *resop; 4488 READLINK4res *lr_res; 4489 nfs4_ga_res_t *garp; 4490 uint_t len; 4491 char *linkdata; 4492 bool_t needrecov = FALSE; 4493 nfs4_recov_state_t recov_state; 4494 hrtime_t t; 4495 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4496 4497 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4498 return (EIO); 4499 /* 4500 * Can't readlink anything other than a symbolic link. 4501 */ 4502 if (vp->v_type != VLNK) 4503 return (EINVAL); 4504 4505 rp = VTOR4(vp); 4506 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4507 e.error = nfs4_validate_caches(vp, cr); 4508 if (e.error) 4509 return (e.error); 4510 mutex_enter(&rp->r_statelock); 4511 if (rp->r_symlink.contents != NULL) { 4512 e.error = uiomove(rp->r_symlink.contents, 4513 rp->r_symlink.len, UIO_READ, uiop); 4514 mutex_exit(&rp->r_statelock); 4515 return (e.error); 4516 } 4517 mutex_exit(&rp->r_statelock); 4518 } 4519 recov_state.rs_flags = 0; 4520 recov_state.rs_num_retry_despite_err = 0; 4521 4522 recov_retry: 4523 args.array_len = 3; 4524 args.array = argop; 4525 args.ctag = TAG_READLINK; 4526 4527 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4528 if (e.error) { 4529 return (e.error); 4530 } 4531 4532 /* 0. putfh symlink fh */ 4533 argop[0].argop = OP_CPUTFH; 4534 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4535 4536 /* 1. readlink */ 4537 argop[1].argop = OP_READLINK; 4538 4539 /* 2. getattr */ 4540 argop[2].argop = OP_GETATTR; 4541 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4542 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4543 4544 doqueue = 1; 4545 4546 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4547 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4548 rnode4info(VTOR4(vp)))); 4549 4550 t = gethrtime(); 4551 4552 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4553 4554 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4555 if (needrecov) { 4556 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4557 "nfs4_readlink: initiating recovery\n")); 4558 4559 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4560 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) { 4561 if (!e.error) 4562 (void) xdr_free(xdr_COMPOUND4res_clnt, 4563 (caddr_t)&res); 4564 4565 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4566 needrecov); 4567 goto recov_retry; 4568 } 4569 } 4570 4571 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4572 4573 if (e.error) 4574 return (e.error); 4575 4576 /* 4577 * There is an path in the code below which calls 4578 * nfs4_purge_stale_fh(), which may generate otw calls through 4579 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4580 * here to avoid nfs4_start_op() deadlock. 4581 */ 4582 4583 if (res.status && (res.array_len < args.array_len)) { 4584 /* 4585 * either Putfh or Link failed 4586 */ 4587 e.error = geterrno4(res.status); 4588 nfs4_purge_stale_fh(e.error, vp, cr); 4589 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4590 return (e.error); 4591 } 4592 4593 resop = &res.array[1]; /* readlink res */ 4594 lr_res = &resop->nfs_resop4_u.opreadlink; 4595 4596 /* 4597 * treat symlink names as data 4598 */ 4599 linkdata = utf8_to_str(&lr_res->link, &len, NULL); 4600 if (linkdata != NULL) { 4601 int uio_len = len - 1; 4602 /* len includes null byte, which we won't uiomove */ 4603 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4604 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4605 mutex_enter(&rp->r_statelock); 4606 if (rp->r_symlink.contents == NULL) { 4607 rp->r_symlink.contents = linkdata; 4608 rp->r_symlink.len = uio_len; 4609 rp->r_symlink.size = len; 4610 mutex_exit(&rp->r_statelock); 4611 } else { 4612 mutex_exit(&rp->r_statelock); 4613 kmem_free(linkdata, len); 4614 } 4615 } else { 4616 kmem_free(linkdata, len); 4617 } 4618 } 4619 if (res.status == NFS4_OK) { 4620 resop++; /* getattr res */ 4621 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4622 } 4623 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4624 4625 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4626 4627 /* 4628 * The over the wire error for attempting to readlink something 4629 * other than a symbolic link is ENXIO. However, we need to 4630 * return EINVAL instead of ENXIO, so we map it here. 4631 */ 4632 return (e.error == ENXIO ? EINVAL : e.error); 4633 } 4634 4635 /* 4636 * Flush local dirty pages to stable storage on the server. 4637 * 4638 * If FNODSYNC is specified, then there is nothing to do because 4639 * metadata changes are not cached on the client before being 4640 * sent to the server. 4641 */ 4642 /* ARGSUSED */ 4643 static int 4644 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 4645 { 4646 int error; 4647 4648 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4649 return (0); 4650 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4651 return (EIO); 4652 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4653 if (!error) 4654 error = VTOR4(vp)->r_error; 4655 return (error); 4656 } 4657 4658 /* 4659 * Weirdness: if the file was removed or the target of a rename 4660 * operation while it was open, it got renamed instead. Here we 4661 * remove the renamed file. 4662 */ 4663 /* ARGSUSED */ 4664 void 4665 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4666 { 4667 rnode4_t *rp; 4668 4669 ASSERT(vp != DNLC_NO_VNODE); 4670 4671 rp = VTOR4(vp); 4672 4673 if (IS_SHADOW(vp, rp)) { 4674 sv_inactive(vp); 4675 return; 4676 } 4677 4678 /* 4679 * If this is coming from the wrong zone, we let someone in the right 4680 * zone take care of it asynchronously. We can get here due to 4681 * VN_RELE() being called from pageout() or fsflush(). This call may 4682 * potentially turn into an expensive no-op if, for instance, v_count 4683 * gets incremented in the meantime, but it's still correct. 4684 */ 4685 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4686 nfs4_async_inactive(vp, cr); 4687 return; 4688 } 4689 4690 /* 4691 * Some of the cleanup steps might require over-the-wire 4692 * operations. Since VOP_INACTIVE can get called as a result of 4693 * other over-the-wire operations (e.g., an attribute cache update 4694 * can lead to a DNLC purge), doing those steps now would lead to a 4695 * nested call to the recovery framework, which can deadlock. So 4696 * do any over-the-wire cleanups asynchronously, in a separate 4697 * thread. 4698 */ 4699 4700 mutex_enter(&rp->r_os_lock); 4701 mutex_enter(&rp->r_statelock); 4702 mutex_enter(&rp->r_statev4_lock); 4703 4704 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4705 mutex_exit(&rp->r_statev4_lock); 4706 mutex_exit(&rp->r_statelock); 4707 mutex_exit(&rp->r_os_lock); 4708 nfs4_async_inactive(vp, cr); 4709 return; 4710 } 4711 4712 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4713 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 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_unldvp != NULL) { 4722 mutex_exit(&rp->r_statev4_lock); 4723 mutex_exit(&rp->r_statelock); 4724 mutex_exit(&rp->r_os_lock); 4725 nfs4_async_inactive(vp, cr); 4726 return; 4727 } 4728 mutex_exit(&rp->r_statev4_lock); 4729 mutex_exit(&rp->r_statelock); 4730 mutex_exit(&rp->r_os_lock); 4731 4732 rp4_addfree(rp, cr); 4733 } 4734 4735 /* 4736 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4737 * various bits of state. The caller must not refer to vp after this call. 4738 */ 4739 4740 void 4741 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4742 { 4743 rnode4_t *rp = VTOR4(vp); 4744 nfs4_recov_state_t recov_state; 4745 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4746 vnode_t *unldvp; 4747 char *unlname; 4748 cred_t *unlcred; 4749 COMPOUND4args_clnt args; 4750 COMPOUND4res_clnt res, *resp; 4751 nfs_argop4 argop[2]; 4752 int doqueue; 4753 #ifdef DEBUG 4754 char *name; 4755 #endif 4756 4757 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4758 ASSERT(!IS_SHADOW(vp, rp)); 4759 4760 #ifdef DEBUG 4761 name = fn_name(VTOSV(vp)->sv_name); 4762 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4763 "release vnode %s", name)); 4764 kmem_free(name, MAXNAMELEN); 4765 #endif 4766 4767 if (vp->v_type == VREG) { 4768 bool_t recov_failed = FALSE; 4769 4770 e.error = nfs4close_all(vp, cr); 4771 if (e.error) { 4772 /* Check to see if recovery failed */ 4773 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4774 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4775 recov_failed = TRUE; 4776 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4777 if (!recov_failed) { 4778 mutex_enter(&rp->r_statelock); 4779 if (rp->r_flags & R4RECOVERR) 4780 recov_failed = TRUE; 4781 mutex_exit(&rp->r_statelock); 4782 } 4783 if (recov_failed) { 4784 NFS4_DEBUG(nfs4_client_recov_debug, 4785 (CE_NOTE, "nfs4_inactive_otw: " 4786 "close failed (recovery failure)")); 4787 } 4788 } 4789 } 4790 4791 redo: 4792 if (rp->r_unldvp == NULL) { 4793 rp4_addfree(rp, cr); 4794 return; 4795 } 4796 4797 /* 4798 * Save the vnode pointer for the directory where the 4799 * unlinked-open file got renamed, then set it to NULL 4800 * to prevent another thread from getting here before 4801 * we're done with the remove. While we have the 4802 * statelock, make local copies of the pertinent rnode 4803 * fields. If we weren't to do this in an atomic way, the 4804 * the unl* fields could become inconsistent with respect 4805 * to each other due to a race condition between this 4806 * code and nfs_remove(). See bug report 1034328. 4807 */ 4808 mutex_enter(&rp->r_statelock); 4809 if (rp->r_unldvp == NULL) { 4810 mutex_exit(&rp->r_statelock); 4811 rp4_addfree(rp, cr); 4812 return; 4813 } 4814 4815 unldvp = rp->r_unldvp; 4816 rp->r_unldvp = NULL; 4817 unlname = rp->r_unlname; 4818 rp->r_unlname = NULL; 4819 unlcred = rp->r_unlcred; 4820 rp->r_unlcred = NULL; 4821 mutex_exit(&rp->r_statelock); 4822 4823 /* 4824 * If there are any dirty pages left, then flush 4825 * them. This is unfortunate because they just 4826 * may get thrown away during the remove operation, 4827 * but we have to do this for correctness. 4828 */ 4829 if (nfs4_has_pages(vp) && 4830 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4831 ASSERT(vp->v_type != VCHR); 4832 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL); 4833 if (e.error) { 4834 mutex_enter(&rp->r_statelock); 4835 if (!rp->r_error) 4836 rp->r_error = e.error; 4837 mutex_exit(&rp->r_statelock); 4838 } 4839 } 4840 4841 recov_state.rs_flags = 0; 4842 recov_state.rs_num_retry_despite_err = 0; 4843 recov_retry_remove: 4844 /* 4845 * Do the remove operation on the renamed file 4846 */ 4847 args.ctag = TAG_INACTIVE; 4848 4849 /* 4850 * Remove ops: putfh dir; remove 4851 */ 4852 args.array_len = 2; 4853 args.array = argop; 4854 4855 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4856 if (e.error) { 4857 kmem_free(unlname, MAXNAMELEN); 4858 crfree(unlcred); 4859 VN_RELE(unldvp); 4860 /* 4861 * Try again; this time around r_unldvp will be NULL, so we'll 4862 * just call rp4_addfree() and return. 4863 */ 4864 goto redo; 4865 } 4866 4867 /* putfh directory */ 4868 argop[0].argop = OP_CPUTFH; 4869 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4870 4871 /* remove */ 4872 argop[1].argop = OP_CREMOVE; 4873 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4874 4875 doqueue = 1; 4876 resp = &res; 4877 4878 #if 0 /* notyet */ 4879 /* 4880 * Can't do this yet. We may be being called from 4881 * dnlc_purge_XXX while that routine is holding a 4882 * mutex lock to the nc_rele list. The calls to 4883 * nfs3_cache_wcc_data may result in calls to 4884 * dnlc_purge_XXX. This will result in a deadlock. 4885 */ 4886 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4887 if (e.error) { 4888 PURGE_ATTRCACHE4(unldvp); 4889 resp = NULL; 4890 } else if (res.status) { 4891 e.error = geterrno4(res.status); 4892 PURGE_ATTRCACHE4(unldvp); 4893 /* 4894 * This code is inactive right now 4895 * but if made active there should 4896 * be a nfs4_end_op() call before 4897 * nfs4_purge_stale_fh to avoid start_op() 4898 * deadlock. See BugId: 4948726 4899 */ 4900 nfs4_purge_stale_fh(error, unldvp, cr); 4901 } else { 4902 nfs_resop4 *resop; 4903 REMOVE4res *rm_res; 4904 4905 resop = &res.array[1]; 4906 rm_res = &resop->nfs_resop4_u.opremove; 4907 /* 4908 * Update directory cache attribute, 4909 * readdir and dnlc caches. 4910 */ 4911 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4912 } 4913 #else 4914 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4915 4916 PURGE_ATTRCACHE4(unldvp); 4917 #endif 4918 4919 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4920 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4921 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 4922 if (!e.error) 4923 (void) xdr_free(xdr_COMPOUND4res_clnt, 4924 (caddr_t)&res); 4925 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4926 &recov_state, TRUE); 4927 goto recov_retry_remove; 4928 } 4929 } 4930 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4931 4932 /* 4933 * Release stuff held for the remove 4934 */ 4935 VN_RELE(unldvp); 4936 if (!e.error && resp) 4937 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4938 4939 kmem_free(unlname, MAXNAMELEN); 4940 crfree(unlcred); 4941 goto redo; 4942 } 4943 4944 /* 4945 * Remote file system operations having to do with directory manipulation. 4946 */ 4947 /* ARGSUSED3 */ 4948 int 4949 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4950 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 4951 int *direntflags, pathname_t *realpnp) 4952 { 4953 int error; 4954 vnode_t *vp, *avp = NULL; 4955 rnode4_t *drp; 4956 4957 *vpp = NULL; 4958 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4959 return (EPERM); 4960 /* 4961 * if LOOKUP_XATTR, must replace dvp (object) with 4962 * object's attrdir before continuing with lookup 4963 */ 4964 if (flags & LOOKUP_XATTR) { 4965 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4966 if (error) 4967 return (error); 4968 4969 dvp = avp; 4970 4971 /* 4972 * If lookup is for "", just return dvp now. The attrdir 4973 * has already been activated (from nfs4lookup_xattr), and 4974 * the caller will RELE the original dvp -- not 4975 * the attrdir. So, set vpp and return. 4976 * Currently, when the LOOKUP_XATTR flag is 4977 * passed to VOP_LOOKUP, the name is always empty, and 4978 * shortcircuiting here avoids 3 unneeded lock/unlock 4979 * pairs. 4980 * 4981 * If a non-empty name was provided, then it is the 4982 * attribute name, and it will be looked up below. 4983 */ 4984 if (*nm == '\0') { 4985 *vpp = dvp; 4986 return (0); 4987 } 4988 4989 /* 4990 * The vfs layer never sends a name when asking for the 4991 * attrdir, so we should never get here (unless of course 4992 * name is passed at some time in future -- at which time 4993 * we'll blow up here). 4994 */ 4995 ASSERT(0); 4996 } 4997 4998 drp = VTOR4(dvp); 4999 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5000 return (EINTR); 5001 5002 error = nfs4lookup(dvp, nm, vpp, cr, 0); 5003 nfs_rw_exit(&drp->r_rwlock); 5004 5005 /* 5006 * If vnode is a device, create special vnode. 5007 */ 5008 if (!error && ISVDEV((*vpp)->v_type)) { 5009 vp = *vpp; 5010 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 5011 VN_RELE(vp); 5012 } 5013 5014 return (error); 5015 } 5016 5017 /* ARGSUSED */ 5018 static int 5019 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 5020 { 5021 int error; 5022 rnode4_t *drp; 5023 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 5024 mntinfo4_t *mi; 5025 5026 mi = VTOMI4(dvp); 5027 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) && 5028 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS)) 5029 return (EINVAL); 5030 5031 drp = VTOR4(dvp); 5032 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5033 return (EINTR); 5034 5035 mutex_enter(&drp->r_statelock); 5036 /* 5037 * If the server doesn't support xattrs just return EINVAL 5038 */ 5039 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 5040 mutex_exit(&drp->r_statelock); 5041 nfs_rw_exit(&drp->r_rwlock); 5042 return (EINVAL); 5043 } 5044 5045 /* 5046 * If there is a cached xattr directory entry, 5047 * use it as long as the attributes are valid. If the 5048 * attributes are not valid, take the simple approach and 5049 * free the cached value and re-fetch a new value. 5050 * 5051 * We don't negative entry cache for now, if we did we 5052 * would need to check if the file has changed on every 5053 * lookup. But xattrs don't exist very often and failing 5054 * an openattr is not much more expensive than and NVERIFY or GETATTR 5055 * so do an openattr over the wire for now. 5056 */ 5057 if (drp->r_xattr_dir != NULL) { 5058 if (ATTRCACHE4_VALID(dvp)) { 5059 VN_HOLD(drp->r_xattr_dir); 5060 *vpp = drp->r_xattr_dir; 5061 mutex_exit(&drp->r_statelock); 5062 nfs_rw_exit(&drp->r_rwlock); 5063 return (0); 5064 } 5065 VN_RELE(drp->r_xattr_dir); 5066 drp->r_xattr_dir = NULL; 5067 } 5068 mutex_exit(&drp->r_statelock); 5069 5070 error = nfs4openattr(dvp, vpp, cflag, cr); 5071 5072 nfs_rw_exit(&drp->r_rwlock); 5073 5074 return (error); 5075 } 5076 5077 static int 5078 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 5079 { 5080 int error; 5081 rnode4_t *drp; 5082 5083 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5084 5085 /* 5086 * If lookup is for "", just return dvp. Don't need 5087 * to send it over the wire, look it up in the dnlc, 5088 * or perform any access checks. 5089 */ 5090 if (*nm == '\0') { 5091 VN_HOLD(dvp); 5092 *vpp = dvp; 5093 return (0); 5094 } 5095 5096 /* 5097 * Can't do lookups in non-directories. 5098 */ 5099 if (dvp->v_type != VDIR) 5100 return (ENOTDIR); 5101 5102 /* 5103 * If lookup is for ".", just return dvp. Don't need 5104 * to send it over the wire or look it up in the dnlc, 5105 * just need to check access. 5106 */ 5107 if (nm[0] == '.' && nm[1] == '\0') { 5108 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5109 if (error) 5110 return (error); 5111 VN_HOLD(dvp); 5112 *vpp = dvp; 5113 return (0); 5114 } 5115 5116 drp = VTOR4(dvp); 5117 if (!(drp->r_flags & R4LOOKUP)) { 5118 mutex_enter(&drp->r_statelock); 5119 drp->r_flags |= R4LOOKUP; 5120 mutex_exit(&drp->r_statelock); 5121 } 5122 5123 *vpp = NULL; 5124 /* 5125 * Lookup this name in the DNLC. If there is no entry 5126 * lookup over the wire. 5127 */ 5128 if (!skipdnlc) 5129 *vpp = dnlc_lookup(dvp, nm); 5130 if (*vpp == NULL) { 5131 /* 5132 * We need to go over the wire to lookup the name. 5133 */ 5134 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5135 } 5136 5137 /* 5138 * We hit on the dnlc 5139 */ 5140 if (*vpp != DNLC_NO_VNODE || 5141 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5142 /* 5143 * But our attrs may not be valid. 5144 */ 5145 if (ATTRCACHE4_VALID(dvp)) { 5146 error = nfs4_waitfor_purge_complete(dvp); 5147 if (error) { 5148 VN_RELE(*vpp); 5149 *vpp = NULL; 5150 return (error); 5151 } 5152 5153 /* 5154 * If after the purge completes, check to make sure 5155 * our attrs are still valid. 5156 */ 5157 if (ATTRCACHE4_VALID(dvp)) { 5158 /* 5159 * If we waited for a purge we may have 5160 * lost our vnode so look it up again. 5161 */ 5162 VN_RELE(*vpp); 5163 *vpp = dnlc_lookup(dvp, nm); 5164 if (*vpp == NULL) 5165 return (nfs4lookupnew_otw(dvp, 5166 nm, vpp, cr)); 5167 5168 /* 5169 * The access cache should almost always hit 5170 */ 5171 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5172 5173 if (error) { 5174 VN_RELE(*vpp); 5175 *vpp = NULL; 5176 return (error); 5177 } 5178 if (*vpp == DNLC_NO_VNODE) { 5179 VN_RELE(*vpp); 5180 *vpp = NULL; 5181 return (ENOENT); 5182 } 5183 return (0); 5184 } 5185 } 5186 } 5187 5188 ASSERT(*vpp != NULL); 5189 5190 /* 5191 * We may have gotten here we have one of the following cases: 5192 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5193 * need to validate them. 5194 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5195 * must validate. 5196 * 5197 * Go to the server and check if the directory has changed, if 5198 * it hasn't we are done and can use the dnlc entry. 5199 */ 5200 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5201 } 5202 5203 /* 5204 * Go to the server and check if the directory has changed, if 5205 * it hasn't we are done and can use the dnlc entry. If it 5206 * has changed we get a new copy of its attributes and check 5207 * the access for VEXEC, then relookup the filename and 5208 * get its filehandle and attributes. 5209 * 5210 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5211 * if the NVERIFY failed we must 5212 * purge the caches 5213 * cache new attributes (will set r_time_attr_inval) 5214 * cache new access 5215 * recheck VEXEC access 5216 * add name to dnlc, possibly negative 5217 * if LOOKUP succeeded 5218 * cache new attributes 5219 * else 5220 * set a new r_time_attr_inval for dvp 5221 * check to make sure we have access 5222 * 5223 * The vpp returned is the vnode passed in if the directory is valid, 5224 * a new vnode if successful lookup, or NULL on error. 5225 */ 5226 static int 5227 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5228 { 5229 COMPOUND4args_clnt args; 5230 COMPOUND4res_clnt res; 5231 fattr4 *ver_fattr; 5232 fattr4_change dchange; 5233 int32_t *ptr; 5234 int argoplist_size = 7 * sizeof (nfs_argop4); 5235 nfs_argop4 *argop; 5236 int doqueue; 5237 mntinfo4_t *mi; 5238 nfs4_recov_state_t recov_state; 5239 hrtime_t t; 5240 int isdotdot; 5241 vnode_t *nvp; 5242 nfs_fh4 *fhp; 5243 nfs4_sharedfh_t *sfhp; 5244 nfs4_access_type_t cacc; 5245 rnode4_t *nrp; 5246 rnode4_t *drp = VTOR4(dvp); 5247 nfs4_ga_res_t *garp = NULL; 5248 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5249 5250 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5251 ASSERT(nm != NULL); 5252 ASSERT(nm[0] != '\0'); 5253 ASSERT(dvp->v_type == VDIR); 5254 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5255 ASSERT(*vpp != NULL); 5256 5257 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5258 isdotdot = 1; 5259 args.ctag = TAG_LOOKUP_VPARENT; 5260 } else { 5261 /* 5262 * If dvp were a stub, it should have triggered and caused 5263 * a mount for us to get this far. 5264 */ 5265 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5266 5267 isdotdot = 0; 5268 args.ctag = TAG_LOOKUP_VALID; 5269 } 5270 5271 mi = VTOMI4(dvp); 5272 recov_state.rs_flags = 0; 5273 recov_state.rs_num_retry_despite_err = 0; 5274 5275 nvp = NULL; 5276 5277 /* Save the original mount point security information */ 5278 (void) save_mnt_secinfo(mi->mi_curr_serv); 5279 5280 recov_retry: 5281 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5282 &recov_state, NULL); 5283 if (e.error) { 5284 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5285 VN_RELE(*vpp); 5286 *vpp = NULL; 5287 return (e.error); 5288 } 5289 5290 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5291 5292 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5293 args.array_len = 7; 5294 args.array = argop; 5295 5296 /* 0. putfh file */ 5297 argop[0].argop = OP_CPUTFH; 5298 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5299 5300 /* 1. nverify the change info */ 5301 argop[1].argop = OP_NVERIFY; 5302 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5303 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5304 ver_fattr->attrlist4 = (char *)&dchange; 5305 ptr = (int32_t *)&dchange; 5306 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5307 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5308 5309 /* 2. getattr directory */ 5310 argop[2].argop = OP_GETATTR; 5311 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5312 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5313 5314 /* 3. access directory */ 5315 argop[3].argop = OP_ACCESS; 5316 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5317 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5318 5319 /* 4. lookup name */ 5320 if (isdotdot) { 5321 argop[4].argop = OP_LOOKUPP; 5322 } else { 5323 argop[4].argop = OP_CLOOKUP; 5324 argop[4].nfs_argop4_u.opclookup.cname = nm; 5325 } 5326 5327 /* 5. resulting file handle */ 5328 argop[5].argop = OP_GETFH; 5329 5330 /* 6. resulting file attributes */ 5331 argop[6].argop = OP_GETATTR; 5332 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5333 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5334 5335 doqueue = 1; 5336 t = gethrtime(); 5337 5338 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5339 5340 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5341 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5342 if (e.error != 0 && *vpp != NULL) 5343 VN_RELE(*vpp); 5344 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5345 &recov_state, FALSE); 5346 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5347 kmem_free(argop, argoplist_size); 5348 return (e.error); 5349 } 5350 5351 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5352 /* 5353 * For WRONGSEC of a non-dotdot case, send secinfo directly 5354 * from this thread, do not go thru the recovery thread since 5355 * we need the nm information. 5356 * 5357 * Not doing dotdot case because there is no specification 5358 * for (PUTFH, SECINFO "..") yet. 5359 */ 5360 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5361 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5362 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5363 &recov_state, FALSE); 5364 else 5365 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5366 &recov_state, TRUE); 5367 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5368 kmem_free(argop, argoplist_size); 5369 if (!e.error) 5370 goto recov_retry; 5371 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5372 VN_RELE(*vpp); 5373 *vpp = NULL; 5374 return (e.error); 5375 } 5376 5377 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5378 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5379 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5380 &recov_state, TRUE); 5381 5382 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5383 kmem_free(argop, argoplist_size); 5384 goto recov_retry; 5385 } 5386 } 5387 5388 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5389 5390 if (e.error || res.array_len == 0) { 5391 /* 5392 * If e.error isn't set, then reply has no ops (or we couldn't 5393 * be here). The only legal way to reply without an op array 5394 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5395 * be in the reply for all other status values. 5396 * 5397 * For valid replies without an ops array, return ENOTSUP 5398 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5399 * return EIO -- don't trust status. 5400 */ 5401 if (e.error == 0) 5402 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5403 ENOTSUP : EIO; 5404 VN_RELE(*vpp); 5405 *vpp = NULL; 5406 kmem_free(argop, argoplist_size); 5407 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5408 return (e.error); 5409 } 5410 5411 if (res.status != NFS4ERR_SAME) { 5412 e.error = geterrno4(res.status); 5413 5414 /* 5415 * The NVERIFY "failed" so the directory has changed 5416 * First make sure PUTFH succeeded and NVERIFY "failed" 5417 * cleanly. 5418 */ 5419 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5420 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5421 nfs4_purge_stale_fh(e.error, dvp, cr); 5422 VN_RELE(*vpp); 5423 *vpp = NULL; 5424 goto exit; 5425 } 5426 5427 /* 5428 * We know the NVERIFY "failed" so we must: 5429 * purge the caches (access and indirectly dnlc if needed) 5430 */ 5431 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5432 5433 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5434 nfs4_purge_stale_fh(e.error, dvp, cr); 5435 VN_RELE(*vpp); 5436 *vpp = NULL; 5437 goto exit; 5438 } 5439 5440 /* 5441 * Install new cached attributes for the directory 5442 */ 5443 nfs4_attr_cache(dvp, 5444 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5445 t, cr, FALSE, NULL); 5446 5447 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5448 nfs4_purge_stale_fh(e.error, dvp, cr); 5449 VN_RELE(*vpp); 5450 *vpp = NULL; 5451 e.error = geterrno4(res.status); 5452 goto exit; 5453 } 5454 5455 /* 5456 * Now we know the directory is valid, 5457 * cache new directory access 5458 */ 5459 nfs4_access_cache(drp, 5460 args.array[3].nfs_argop4_u.opaccess.access, 5461 res.array[3].nfs_resop4_u.opaccess.access, cr); 5462 5463 /* 5464 * recheck VEXEC access 5465 */ 5466 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5467 if (cacc != NFS4_ACCESS_ALLOWED) { 5468 /* 5469 * Directory permissions might have been revoked 5470 */ 5471 if (cacc == NFS4_ACCESS_DENIED) { 5472 e.error = EACCES; 5473 VN_RELE(*vpp); 5474 *vpp = NULL; 5475 goto exit; 5476 } 5477 5478 /* 5479 * Somehow we must not have asked for enough 5480 * so try a singleton ACCESS, should never happen. 5481 */ 5482 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5483 if (e.error) { 5484 VN_RELE(*vpp); 5485 *vpp = NULL; 5486 goto exit; 5487 } 5488 } 5489 5490 e.error = geterrno4(res.status); 5491 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5492 /* 5493 * The lookup failed, probably no entry 5494 */ 5495 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5496 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5497 } else { 5498 /* 5499 * Might be some other error, so remove 5500 * the dnlc entry to make sure we start all 5501 * over again, next time. 5502 */ 5503 dnlc_remove(dvp, nm); 5504 } 5505 VN_RELE(*vpp); 5506 *vpp = NULL; 5507 goto exit; 5508 } 5509 5510 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5511 /* 5512 * The file exists but we can't get its fh for 5513 * some unknown reason. Remove it from the dnlc 5514 * and error out to be safe. 5515 */ 5516 dnlc_remove(dvp, nm); 5517 VN_RELE(*vpp); 5518 *vpp = NULL; 5519 goto exit; 5520 } 5521 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5522 if (fhp->nfs_fh4_len == 0) { 5523 /* 5524 * The file exists but a bogus fh 5525 * some unknown reason. Remove it from the dnlc 5526 * and error out to be safe. 5527 */ 5528 e.error = ENOENT; 5529 dnlc_remove(dvp, nm); 5530 VN_RELE(*vpp); 5531 *vpp = NULL; 5532 goto exit; 5533 } 5534 sfhp = sfh4_get(fhp, mi); 5535 5536 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5537 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5538 5539 /* 5540 * Make the new rnode 5541 */ 5542 if (isdotdot) { 5543 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5544 if (e.error) { 5545 sfh4_rele(&sfhp); 5546 VN_RELE(*vpp); 5547 *vpp = NULL; 5548 goto exit; 5549 } 5550 /* 5551 * XXX if nfs4_make_dotdot uses an existing rnode 5552 * XXX it doesn't update the attributes. 5553 * XXX for now just save them again to save an OTW 5554 */ 5555 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5556 } else { 5557 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5558 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 5559 /* 5560 * If v_type == VNON, then garp was NULL because 5561 * the last op in the compound failed and makenfs4node 5562 * could not find the vnode for sfhp. It created 5563 * a new vnode, so we have nothing to purge here. 5564 */ 5565 if (nvp->v_type == VNON) { 5566 vattr_t vattr; 5567 5568 vattr.va_mask = AT_TYPE; 5569 /* 5570 * N.B. We've already called nfs4_end_fop above. 5571 */ 5572 e.error = nfs4getattr(nvp, &vattr, cr); 5573 if (e.error) { 5574 sfh4_rele(&sfhp); 5575 VN_RELE(*vpp); 5576 *vpp = NULL; 5577 VN_RELE(nvp); 5578 goto exit; 5579 } 5580 nvp->v_type = vattr.va_type; 5581 } 5582 } 5583 sfh4_rele(&sfhp); 5584 5585 nrp = VTOR4(nvp); 5586 mutex_enter(&nrp->r_statev4_lock); 5587 if (!nrp->created_v4) { 5588 mutex_exit(&nrp->r_statev4_lock); 5589 dnlc_update(dvp, nm, nvp); 5590 } else 5591 mutex_exit(&nrp->r_statev4_lock); 5592 5593 VN_RELE(*vpp); 5594 *vpp = nvp; 5595 } else { 5596 hrtime_t now; 5597 hrtime_t delta = 0; 5598 5599 e.error = 0; 5600 5601 /* 5602 * Because the NVERIFY "succeeded" we know that the 5603 * directory attributes are still valid 5604 * so update r_time_attr_inval 5605 */ 5606 now = gethrtime(); 5607 mutex_enter(&drp->r_statelock); 5608 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5609 delta = now - drp->r_time_attr_saved; 5610 if (delta < mi->mi_acdirmin) 5611 delta = mi->mi_acdirmin; 5612 else if (delta > mi->mi_acdirmax) 5613 delta = mi->mi_acdirmax; 5614 } 5615 drp->r_time_attr_inval = now + delta; 5616 mutex_exit(&drp->r_statelock); 5617 dnlc_update(dvp, nm, *vpp); 5618 5619 /* 5620 * Even though we have a valid directory attr cache 5621 * and dnlc entry, we may not have access. 5622 * This should almost always hit the cache. 5623 */ 5624 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5625 if (e.error) { 5626 VN_RELE(*vpp); 5627 *vpp = NULL; 5628 } 5629 5630 if (*vpp == DNLC_NO_VNODE) { 5631 VN_RELE(*vpp); 5632 *vpp = NULL; 5633 e.error = ENOENT; 5634 } 5635 } 5636 5637 exit: 5638 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5639 kmem_free(argop, argoplist_size); 5640 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5641 return (e.error); 5642 } 5643 5644 /* 5645 * We need to go over the wire to lookup the name, but 5646 * while we are there verify the directory has not 5647 * changed but if it has, get new attributes and check access 5648 * 5649 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5650 * NVERIFY GETATTR ACCESS 5651 * 5652 * With the results: 5653 * if the NVERIFY failed we must purge the caches, add new attributes, 5654 * and cache new access. 5655 * set a new r_time_attr_inval 5656 * add name to dnlc, possibly negative 5657 * if LOOKUP succeeded 5658 * cache new attributes 5659 */ 5660 static int 5661 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5662 { 5663 COMPOUND4args_clnt args; 5664 COMPOUND4res_clnt res; 5665 fattr4 *ver_fattr; 5666 fattr4_change dchange; 5667 int32_t *ptr; 5668 nfs4_ga_res_t *garp = NULL; 5669 int argoplist_size = 9 * sizeof (nfs_argop4); 5670 nfs_argop4 *argop; 5671 int doqueue; 5672 mntinfo4_t *mi; 5673 nfs4_recov_state_t recov_state; 5674 hrtime_t t; 5675 int isdotdot; 5676 vnode_t *nvp; 5677 nfs_fh4 *fhp; 5678 nfs4_sharedfh_t *sfhp; 5679 nfs4_access_type_t cacc; 5680 rnode4_t *nrp; 5681 rnode4_t *drp = VTOR4(dvp); 5682 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5683 5684 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5685 ASSERT(nm != NULL); 5686 ASSERT(nm[0] != '\0'); 5687 ASSERT(dvp->v_type == VDIR); 5688 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5689 ASSERT(*vpp == NULL); 5690 5691 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5692 isdotdot = 1; 5693 args.ctag = TAG_LOOKUP_PARENT; 5694 } else { 5695 /* 5696 * If dvp were a stub, it should have triggered and caused 5697 * a mount for us to get this far. 5698 */ 5699 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5700 5701 isdotdot = 0; 5702 args.ctag = TAG_LOOKUP; 5703 } 5704 5705 mi = VTOMI4(dvp); 5706 recov_state.rs_flags = 0; 5707 recov_state.rs_num_retry_despite_err = 0; 5708 5709 nvp = NULL; 5710 5711 /* Save the original mount point security information */ 5712 (void) save_mnt_secinfo(mi->mi_curr_serv); 5713 5714 recov_retry: 5715 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5716 &recov_state, NULL); 5717 if (e.error) { 5718 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5719 return (e.error); 5720 } 5721 5722 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5723 5724 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5725 args.array_len = 9; 5726 args.array = argop; 5727 5728 /* 0. putfh file */ 5729 argop[0].argop = OP_CPUTFH; 5730 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5731 5732 /* 1. savefh for the nverify */ 5733 argop[1].argop = OP_SAVEFH; 5734 5735 /* 2. lookup name */ 5736 if (isdotdot) { 5737 argop[2].argop = OP_LOOKUPP; 5738 } else { 5739 argop[2].argop = OP_CLOOKUP; 5740 argop[2].nfs_argop4_u.opclookup.cname = nm; 5741 } 5742 5743 /* 3. resulting file handle */ 5744 argop[3].argop = OP_GETFH; 5745 5746 /* 4. resulting file attributes */ 5747 argop[4].argop = OP_GETATTR; 5748 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5749 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5750 5751 /* 5. restorefh back the directory for the nverify */ 5752 argop[5].argop = OP_RESTOREFH; 5753 5754 /* 6. nverify the change info */ 5755 argop[6].argop = OP_NVERIFY; 5756 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5757 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5758 ver_fattr->attrlist4 = (char *)&dchange; 5759 ptr = (int32_t *)&dchange; 5760 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5761 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5762 5763 /* 7. getattr directory */ 5764 argop[7].argop = OP_GETATTR; 5765 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5766 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5767 5768 /* 8. access directory */ 5769 argop[8].argop = OP_ACCESS; 5770 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5771 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5772 5773 doqueue = 1; 5774 t = gethrtime(); 5775 5776 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5777 5778 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5779 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5780 if (e.error != 0 && *vpp != NULL) 5781 VN_RELE(*vpp); 5782 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5783 &recov_state, FALSE); 5784 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5785 kmem_free(argop, argoplist_size); 5786 return (e.error); 5787 } 5788 5789 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5790 /* 5791 * For WRONGSEC of a non-dotdot case, send secinfo directly 5792 * from this thread, do not go thru the recovery thread since 5793 * we need the nm information. 5794 * 5795 * Not doing dotdot case because there is no specification 5796 * for (PUTFH, SECINFO "..") yet. 5797 */ 5798 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5799 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5800 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5801 &recov_state, FALSE); 5802 else 5803 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5804 &recov_state, TRUE); 5805 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5806 kmem_free(argop, argoplist_size); 5807 if (!e.error) 5808 goto recov_retry; 5809 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5810 return (e.error); 5811 } 5812 5813 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5814 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5815 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5816 &recov_state, TRUE); 5817 5818 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5819 kmem_free(argop, argoplist_size); 5820 goto recov_retry; 5821 } 5822 } 5823 5824 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5825 5826 if (e.error || res.array_len == 0) { 5827 /* 5828 * If e.error isn't set, then reply has no ops (or we couldn't 5829 * be here). The only legal way to reply without an op array 5830 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5831 * be in the reply for all other status values. 5832 * 5833 * For valid replies without an ops array, return ENOTSUP 5834 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5835 * return EIO -- don't trust status. 5836 */ 5837 if (e.error == 0) 5838 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5839 ENOTSUP : EIO; 5840 5841 kmem_free(argop, argoplist_size); 5842 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5843 return (e.error); 5844 } 5845 5846 e.error = geterrno4(res.status); 5847 5848 /* 5849 * The PUTFH and SAVEFH may have failed. 5850 */ 5851 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5852 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5853 nfs4_purge_stale_fh(e.error, dvp, cr); 5854 goto exit; 5855 } 5856 5857 /* 5858 * Check if the file exists, if it does delay entering 5859 * into the dnlc until after we update the directory 5860 * attributes so we don't cause it to get purged immediately. 5861 */ 5862 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5863 /* 5864 * The lookup failed, probably no entry 5865 */ 5866 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5867 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5868 goto exit; 5869 } 5870 5871 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5872 /* 5873 * The file exists but we can't get its fh for 5874 * some unknown reason. Error out to be safe. 5875 */ 5876 goto exit; 5877 } 5878 5879 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5880 if (fhp->nfs_fh4_len == 0) { 5881 /* 5882 * The file exists but a bogus fh 5883 * some unknown reason. Error out to be safe. 5884 */ 5885 e.error = EIO; 5886 goto exit; 5887 } 5888 sfhp = sfh4_get(fhp, mi); 5889 5890 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5891 sfh4_rele(&sfhp); 5892 goto exit; 5893 } 5894 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5895 5896 /* 5897 * The RESTOREFH may have failed 5898 */ 5899 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5900 sfh4_rele(&sfhp); 5901 e.error = EIO; 5902 goto exit; 5903 } 5904 5905 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5906 /* 5907 * First make sure the NVERIFY failed as we expected, 5908 * if it didn't then be conservative and error out 5909 * as we can't trust the directory. 5910 */ 5911 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5912 sfh4_rele(&sfhp); 5913 e.error = EIO; 5914 goto exit; 5915 } 5916 5917 /* 5918 * We know the NVERIFY "failed" so the directory has changed, 5919 * so we must: 5920 * purge the caches (access and indirectly dnlc if needed) 5921 */ 5922 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5923 5924 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5925 sfh4_rele(&sfhp); 5926 goto exit; 5927 } 5928 nfs4_attr_cache(dvp, 5929 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5930 t, cr, FALSE, NULL); 5931 5932 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5933 nfs4_purge_stale_fh(e.error, dvp, cr); 5934 sfh4_rele(&sfhp); 5935 e.error = geterrno4(res.status); 5936 goto exit; 5937 } 5938 5939 /* 5940 * Now we know the directory is valid, 5941 * cache new directory access 5942 */ 5943 nfs4_access_cache(drp, 5944 args.array[8].nfs_argop4_u.opaccess.access, 5945 res.array[8].nfs_resop4_u.opaccess.access, cr); 5946 5947 /* 5948 * recheck VEXEC access 5949 */ 5950 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5951 if (cacc != NFS4_ACCESS_ALLOWED) { 5952 /* 5953 * Directory permissions might have been revoked 5954 */ 5955 if (cacc == NFS4_ACCESS_DENIED) { 5956 sfh4_rele(&sfhp); 5957 e.error = EACCES; 5958 goto exit; 5959 } 5960 5961 /* 5962 * Somehow we must not have asked for enough 5963 * so try a singleton ACCESS should never happen 5964 */ 5965 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5966 if (e.error) { 5967 sfh4_rele(&sfhp); 5968 goto exit; 5969 } 5970 } 5971 5972 e.error = geterrno4(res.status); 5973 } else { 5974 hrtime_t now; 5975 hrtime_t delta = 0; 5976 5977 e.error = 0; 5978 5979 /* 5980 * Because the NVERIFY "succeeded" we know that the 5981 * directory attributes are still valid 5982 * so update r_time_attr_inval 5983 */ 5984 now = gethrtime(); 5985 mutex_enter(&drp->r_statelock); 5986 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5987 delta = now - drp->r_time_attr_saved; 5988 if (delta < mi->mi_acdirmin) 5989 delta = mi->mi_acdirmin; 5990 else if (delta > mi->mi_acdirmax) 5991 delta = mi->mi_acdirmax; 5992 } 5993 drp->r_time_attr_inval = now + delta; 5994 mutex_exit(&drp->r_statelock); 5995 5996 /* 5997 * Even though we have a valid directory attr cache, 5998 * we may not have access. 5999 * This should almost always hit the cache. 6000 */ 6001 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 6002 if (e.error) { 6003 sfh4_rele(&sfhp); 6004 goto exit; 6005 } 6006 } 6007 6008 /* 6009 * Now we have successfully completed the lookup, if the 6010 * directory has changed we now have the valid attributes. 6011 * We also know we have directory access. 6012 * Create the new rnode and insert it in the dnlc. 6013 */ 6014 if (isdotdot) { 6015 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 6016 if (e.error) { 6017 sfh4_rele(&sfhp); 6018 goto exit; 6019 } 6020 /* 6021 * XXX if nfs4_make_dotdot uses an existing rnode 6022 * XXX it doesn't update the attributes. 6023 * XXX for now just save them again to save an OTW 6024 */ 6025 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 6026 } else { 6027 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 6028 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 6029 } 6030 sfh4_rele(&sfhp); 6031 6032 nrp = VTOR4(nvp); 6033 mutex_enter(&nrp->r_statev4_lock); 6034 if (!nrp->created_v4) { 6035 mutex_exit(&nrp->r_statev4_lock); 6036 dnlc_update(dvp, nm, nvp); 6037 } else 6038 mutex_exit(&nrp->r_statev4_lock); 6039 6040 *vpp = nvp; 6041 6042 exit: 6043 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6044 kmem_free(argop, argoplist_size); 6045 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 6046 return (e.error); 6047 } 6048 6049 #ifdef DEBUG 6050 void 6051 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 6052 { 6053 uint_t i, len; 6054 zoneid_t zoneid = getzoneid(); 6055 char *s; 6056 6057 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 6058 for (i = 0; i < argcnt; i++) { 6059 nfs_argop4 *op = &argbase[i]; 6060 switch (op->argop) { 6061 case OP_CPUTFH: 6062 case OP_PUTFH: 6063 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 6064 break; 6065 case OP_PUTROOTFH: 6066 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 6067 break; 6068 case OP_CLOOKUP: 6069 s = op->nfs_argop4_u.opclookup.cname; 6070 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6071 break; 6072 case OP_LOOKUP: 6073 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 6074 &len, NULL); 6075 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6076 kmem_free(s, len); 6077 break; 6078 case OP_LOOKUPP: 6079 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 6080 break; 6081 case OP_GETFH: 6082 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 6083 break; 6084 case OP_GETATTR: 6085 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 6086 break; 6087 case OP_OPENATTR: 6088 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 6089 break; 6090 default: 6091 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 6092 op->argop); 6093 break; 6094 } 6095 } 6096 } 6097 #endif 6098 6099 /* 6100 * nfs4lookup_setup - constructs a multi-lookup compound request. 6101 * 6102 * Given the path "nm1/nm2/.../nmn", the following compound requests 6103 * may be created: 6104 * 6105 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 6106 * is faster, for now. 6107 * 6108 * l4_getattrs indicates the type of compound requested. 6109 * 6110 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 6111 * 6112 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 6113 * 6114 * total number of ops is n + 1. 6115 * 6116 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 6117 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 6118 * before the last component, and only get attributes 6119 * for the last component. Note that the second-to-last 6120 * pathname component is XATTR_RPATH, which does NOT go 6121 * over-the-wire as a lookup. 6122 * 6123 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 6124 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 6125 * 6126 * and total number of ops is n + 5. 6127 * 6128 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 6129 * attribute directory: create lookups plus an OPENATTR 6130 * replacing the last lookup. Note that the last pathname 6131 * component is XATTR_RPATH, which does NOT go over-the-wire 6132 * as a lookup. 6133 * 6134 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 6135 * Openattr; Getfh; Getattr } 6136 * 6137 * and total number of ops is n + 5. 6138 * 6139 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6140 * nodes too. 6141 * 6142 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6143 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6144 * 6145 * and total number of ops is 3*n + 1. 6146 * 6147 * All cases: returns the index in the arg array of the final LOOKUP op, or 6148 * -1 if no LOOKUPs were used. 6149 */ 6150 int 6151 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6152 { 6153 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6154 nfs_argop4 *argbase, *argop; 6155 int arglen, argcnt; 6156 int n = 1; /* number of components */ 6157 int nga = 1; /* number of Getattr's in request */ 6158 char c = '\0', *s, *p; 6159 int lookup_idx = -1; 6160 int argoplist_size; 6161 6162 /* set lookuparg response result to 0 */ 6163 lookupargp->resp->status = NFS4_OK; 6164 6165 /* skip leading "/" or "." e.g. ".//./" if there is */ 6166 for (; ; nm++) { 6167 if (*nm != '/' && *nm != '.') 6168 break; 6169 6170 /* ".." is counted as 1 component */ 6171 if (*nm == '.' && *(nm + 1) != '/') 6172 break; 6173 } 6174 6175 /* 6176 * Find n = number of components - nm must be null terminated 6177 * Skip "." components. 6178 */ 6179 if (*nm != '\0') 6180 for (n = 1, s = nm; *s != '\0'; s++) { 6181 if ((*s == '/') && (*(s + 1) != '/') && 6182 (*(s + 1) != '\0') && 6183 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6184 *(s + 2) == '\0'))) 6185 n++; 6186 } 6187 else 6188 n = 0; 6189 6190 /* 6191 * nga is number of components that need Getfh+Getattr 6192 */ 6193 switch (l4_getattrs) { 6194 case LKP4_NO_ATTRIBUTES: 6195 nga = 0; 6196 break; 6197 case LKP4_ALL_ATTRIBUTES: 6198 nga = n; 6199 /* 6200 * Always have at least 1 getfh, getattr pair 6201 */ 6202 if (nga == 0) 6203 nga++; 6204 break; 6205 case LKP4_LAST_ATTRDIR: 6206 case LKP4_LAST_NAMED_ATTR: 6207 nga = n+1; 6208 break; 6209 } 6210 6211 /* 6212 * If change to use the filehandle attr instead of getfh 6213 * the following line can be deleted. 6214 */ 6215 nga *= 2; 6216 6217 /* 6218 * calculate number of ops in request as 6219 * header + trailer + lookups + getattrs 6220 */ 6221 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6222 6223 argoplist_size = arglen * sizeof (nfs_argop4); 6224 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6225 lookupargp->argsp->array = argop; 6226 6227 argcnt = lookupargp->header_len; 6228 argop += argcnt; 6229 6230 /* 6231 * loop and create a lookup op and possibly getattr/getfh for 6232 * each component. Skip "." components. 6233 */ 6234 for (s = nm; *s != '\0'; s = p) { 6235 /* 6236 * Set up a pathname struct for each component if needed 6237 */ 6238 while (*s == '/') 6239 s++; 6240 if (*s == '\0') 6241 break; 6242 6243 for (p = s; (*p != '/') && (*p != '\0'); p++) 6244 ; 6245 c = *p; 6246 *p = '\0'; 6247 6248 if (s[0] == '.' && s[1] == '\0') { 6249 *p = c; 6250 continue; 6251 } 6252 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6253 strcmp(s, XATTR_RPATH) == 0) { 6254 /* getfh XXX may not be needed in future */ 6255 argop->argop = OP_GETFH; 6256 argop++; 6257 argcnt++; 6258 6259 /* getattr */ 6260 argop->argop = OP_GETATTR; 6261 argop->nfs_argop4_u.opgetattr.attr_request = 6262 lookupargp->ga_bits; 6263 argop->nfs_argop4_u.opgetattr.mi = 6264 lookupargp->mi; 6265 argop++; 6266 argcnt++; 6267 6268 /* openattr */ 6269 argop->argop = OP_OPENATTR; 6270 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6271 strcmp(s, XATTR_RPATH) == 0) { 6272 /* openattr */ 6273 argop->argop = OP_OPENATTR; 6274 argop++; 6275 argcnt++; 6276 6277 /* getfh XXX may not be needed in future */ 6278 argop->argop = OP_GETFH; 6279 argop++; 6280 argcnt++; 6281 6282 /* getattr */ 6283 argop->argop = OP_GETATTR; 6284 argop->nfs_argop4_u.opgetattr.attr_request = 6285 lookupargp->ga_bits; 6286 argop->nfs_argop4_u.opgetattr.mi = 6287 lookupargp->mi; 6288 argop++; 6289 argcnt++; 6290 *p = c; 6291 continue; 6292 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6293 /* lookupp */ 6294 argop->argop = OP_LOOKUPP; 6295 } else { 6296 /* lookup */ 6297 argop->argop = OP_LOOKUP; 6298 (void) str_to_utf8(s, 6299 &argop->nfs_argop4_u.oplookup.objname); 6300 } 6301 lookup_idx = argcnt; 6302 argop++; 6303 argcnt++; 6304 6305 *p = c; 6306 6307 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6308 /* getfh XXX may not be needed in future */ 6309 argop->argop = OP_GETFH; 6310 argop++; 6311 argcnt++; 6312 6313 /* getattr */ 6314 argop->argop = OP_GETATTR; 6315 argop->nfs_argop4_u.opgetattr.attr_request = 6316 lookupargp->ga_bits; 6317 argop->nfs_argop4_u.opgetattr.mi = 6318 lookupargp->mi; 6319 argop++; 6320 argcnt++; 6321 } 6322 } 6323 6324 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6325 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6326 if (needgetfh) { 6327 /* stick in a post-lookup getfh */ 6328 argop->argop = OP_GETFH; 6329 argcnt++; 6330 argop++; 6331 } 6332 /* post-lookup getattr */ 6333 argop->argop = OP_GETATTR; 6334 argop->nfs_argop4_u.opgetattr.attr_request = 6335 lookupargp->ga_bits; 6336 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6337 argcnt++; 6338 } 6339 argcnt += lookupargp->trailer_len; /* actual op count */ 6340 lookupargp->argsp->array_len = argcnt; 6341 lookupargp->arglen = arglen; 6342 6343 #ifdef DEBUG 6344 if (nfs4_client_lookup_debug) 6345 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6346 #endif 6347 6348 return (lookup_idx); 6349 } 6350 6351 static int 6352 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6353 { 6354 COMPOUND4args_clnt args; 6355 COMPOUND4res_clnt res; 6356 GETFH4res *gf_res = NULL; 6357 nfs_argop4 argop[4]; 6358 nfs_resop4 *resop = NULL; 6359 nfs4_sharedfh_t *sfhp; 6360 hrtime_t t; 6361 nfs4_error_t e; 6362 6363 rnode4_t *drp; 6364 int doqueue = 1; 6365 vnode_t *vp; 6366 int needrecov = 0; 6367 nfs4_recov_state_t recov_state; 6368 6369 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6370 6371 *avp = NULL; 6372 recov_state.rs_flags = 0; 6373 recov_state.rs_num_retry_despite_err = 0; 6374 6375 recov_retry: 6376 /* COMPOUND: putfh, openattr, getfh, getattr */ 6377 args.array_len = 4; 6378 args.array = argop; 6379 args.ctag = TAG_OPENATTR; 6380 6381 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6382 if (e.error) 6383 return (e.error); 6384 6385 drp = VTOR4(dvp); 6386 6387 /* putfh */ 6388 argop[0].argop = OP_CPUTFH; 6389 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6390 6391 /* openattr */ 6392 argop[1].argop = OP_OPENATTR; 6393 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6394 6395 /* getfh */ 6396 argop[2].argop = OP_GETFH; 6397 6398 /* getattr */ 6399 argop[3].argop = OP_GETATTR; 6400 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6401 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6402 6403 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6404 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6405 rnode4info(drp))); 6406 6407 t = gethrtime(); 6408 6409 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6410 6411 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6412 if (needrecov) { 6413 bool_t abort; 6414 6415 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6416 "nfs4openattr: initiating recovery\n")); 6417 6418 abort = nfs4_start_recovery(&e, 6419 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6420 OP_OPENATTR, NULL, NULL, NULL); 6421 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6422 if (!e.error) { 6423 e.error = geterrno4(res.status); 6424 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6425 } 6426 if (abort == FALSE) 6427 goto recov_retry; 6428 return (e.error); 6429 } 6430 6431 if (e.error) { 6432 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6433 return (e.error); 6434 } 6435 6436 if (res.status) { 6437 /* 6438 * If OTW errro is NOTSUPP, then it should be 6439 * translated to EINVAL. All Solaris file system 6440 * implementations return EINVAL to the syscall layer 6441 * when the attrdir cannot be created due to an 6442 * implementation restriction or noxattr mount option. 6443 */ 6444 if (res.status == NFS4ERR_NOTSUPP) { 6445 mutex_enter(&drp->r_statelock); 6446 if (drp->r_xattr_dir) 6447 VN_RELE(drp->r_xattr_dir); 6448 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6449 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6450 mutex_exit(&drp->r_statelock); 6451 6452 e.error = EINVAL; 6453 } else { 6454 e.error = geterrno4(res.status); 6455 } 6456 6457 if (e.error) { 6458 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6459 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6460 needrecov); 6461 return (e.error); 6462 } 6463 } 6464 6465 resop = &res.array[0]; /* putfh res */ 6466 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6467 6468 resop = &res.array[1]; /* openattr res */ 6469 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6470 6471 resop = &res.array[2]; /* getfh res */ 6472 gf_res = &resop->nfs_resop4_u.opgetfh; 6473 if (gf_res->object.nfs_fh4_len == 0) { 6474 *avp = NULL; 6475 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6476 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6477 return (ENOENT); 6478 } 6479 6480 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6481 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6482 dvp->v_vfsp, t, cr, dvp, 6483 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp)); 6484 sfh4_rele(&sfhp); 6485 6486 if (e.error) 6487 PURGE_ATTRCACHE4(vp); 6488 6489 mutex_enter(&vp->v_lock); 6490 vp->v_flag |= V_XATTRDIR; 6491 mutex_exit(&vp->v_lock); 6492 6493 *avp = vp; 6494 6495 mutex_enter(&drp->r_statelock); 6496 if (drp->r_xattr_dir) 6497 VN_RELE(drp->r_xattr_dir); 6498 VN_HOLD(vp); 6499 drp->r_xattr_dir = vp; 6500 6501 /* 6502 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6503 * NULL. xattrs could be created at any time, and we have no 6504 * way to update pc4_xattr_exists in the base object if/when 6505 * it happens. 6506 */ 6507 drp->r_pathconf.pc4_xattr_valid = 0; 6508 6509 mutex_exit(&drp->r_statelock); 6510 6511 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6512 6513 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6514 6515 return (0); 6516 } 6517 6518 /* ARGSUSED */ 6519 static int 6520 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6521 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct, 6522 vsecattr_t *vsecp) 6523 { 6524 int error; 6525 vnode_t *vp = NULL; 6526 rnode4_t *rp; 6527 struct vattr vattr; 6528 rnode4_t *drp; 6529 vnode_t *tempvp; 6530 enum createmode4 createmode; 6531 bool_t must_trunc = FALSE; 6532 int truncating = 0; 6533 6534 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6535 return (EPERM); 6536 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6537 return (EINVAL); 6538 } 6539 6540 /* . and .. have special meaning in the protocol, reject them. */ 6541 6542 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6543 return (EISDIR); 6544 6545 drp = VTOR4(dvp); 6546 6547 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6548 return (EINTR); 6549 6550 top: 6551 /* 6552 * We make a copy of the attributes because the caller does not 6553 * expect us to change what va points to. 6554 */ 6555 vattr = *va; 6556 6557 /* 6558 * If the pathname is "", then dvp is the root vnode of 6559 * a remote file mounted over a local directory. 6560 * All that needs to be done is access 6561 * checking and truncation. Note that we avoid doing 6562 * open w/ create because the parent directory might 6563 * be in pseudo-fs and the open would fail. 6564 */ 6565 if (*nm == '\0') { 6566 error = 0; 6567 VN_HOLD(dvp); 6568 vp = dvp; 6569 must_trunc = TRUE; 6570 } else { 6571 /* 6572 * We need to go over the wire, just to be sure whether the 6573 * file exists or not. Using the DNLC can be dangerous in 6574 * this case when making a decision regarding existence. 6575 */ 6576 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6577 } 6578 6579 if (exclusive) 6580 createmode = EXCLUSIVE4; 6581 else 6582 createmode = GUARDED4; 6583 6584 /* 6585 * error would be set if the file does not exist on the 6586 * server, so lets go create it. 6587 */ 6588 if (error) { 6589 goto create_otw; 6590 } 6591 6592 /* 6593 * File does exist on the server 6594 */ 6595 if (exclusive == EXCL) 6596 error = EEXIST; 6597 else if (vp->v_type == VDIR && (mode & VWRITE)) 6598 error = EISDIR; 6599 else { 6600 /* 6601 * If vnode is a device, create special vnode. 6602 */ 6603 if (ISVDEV(vp->v_type)) { 6604 tempvp = vp; 6605 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6606 VN_RELE(tempvp); 6607 } 6608 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) { 6609 if ((vattr.va_mask & AT_SIZE) && 6610 vp->v_type == VREG) { 6611 rp = VTOR4(vp); 6612 /* 6613 * Check here for large file handled 6614 * by LF-unaware process (as 6615 * ufs_create() does) 6616 */ 6617 if (!(flags & FOFFMAX)) { 6618 mutex_enter(&rp->r_statelock); 6619 if (rp->r_size > MAXOFF32_T) 6620 error = EOVERFLOW; 6621 mutex_exit(&rp->r_statelock); 6622 } 6623 6624 /* if error is set then we need to return */ 6625 if (error) { 6626 nfs_rw_exit(&drp->r_rwlock); 6627 VN_RELE(vp); 6628 return (error); 6629 } 6630 6631 if (must_trunc) { 6632 vattr.va_mask = AT_SIZE; 6633 error = nfs4setattr(vp, &vattr, 0, cr, 6634 NULL); 6635 } else { 6636 /* 6637 * we know we have a regular file that already 6638 * exists and we may end up truncating the file 6639 * as a result of the open_otw, so flush out 6640 * any dirty pages for this file first. 6641 */ 6642 if (nfs4_has_pages(vp) && 6643 ((rp->r_flags & R4DIRTY) || 6644 rp->r_count > 0 || 6645 rp->r_mapcnt > 0)) { 6646 error = nfs4_putpage(vp, 6647 (offset_t)0, 0, 0, cr, ct); 6648 if (error && (error == ENOSPC || 6649 error == EDQUOT)) { 6650 mutex_enter( 6651 &rp->r_statelock); 6652 if (!rp->r_error) 6653 rp->r_error = 6654 error; 6655 mutex_exit( 6656 &rp->r_statelock); 6657 } 6658 } 6659 vattr.va_mask = (AT_SIZE | 6660 AT_TYPE | AT_MODE); 6661 vattr.va_type = VREG; 6662 createmode = UNCHECKED4; 6663 truncating = 1; 6664 goto create_otw; 6665 } 6666 } 6667 } 6668 } 6669 nfs_rw_exit(&drp->r_rwlock); 6670 if (error) { 6671 VN_RELE(vp); 6672 } else { 6673 vnode_t *tvp; 6674 rnode4_t *trp; 6675 tvp = vp; 6676 if (vp->v_type == VREG) { 6677 trp = VTOR4(vp); 6678 if (IS_SHADOW(vp, trp)) 6679 tvp = RTOV4(trp); 6680 } 6681 6682 if (must_trunc) { 6683 /* 6684 * existing file got truncated, notify. 6685 */ 6686 vnevent_create(tvp, ct); 6687 } 6688 6689 *vpp = vp; 6690 } 6691 return (error); 6692 6693 create_otw: 6694 dnlc_remove(dvp, nm); 6695 6696 ASSERT(vattr.va_mask & AT_TYPE); 6697 6698 /* 6699 * If not a regular file let nfs4mknod() handle it. 6700 */ 6701 if (vattr.va_type != VREG) { 6702 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6703 nfs_rw_exit(&drp->r_rwlock); 6704 return (error); 6705 } 6706 6707 /* 6708 * It _is_ a regular file. 6709 */ 6710 ASSERT(vattr.va_mask & AT_MODE); 6711 if (MANDMODE(vattr.va_mode)) { 6712 nfs_rw_exit(&drp->r_rwlock); 6713 return (EACCES); 6714 } 6715 6716 /* 6717 * If this happens to be a mknod of a regular file, then flags will 6718 * have neither FREAD or FWRITE. However, we must set at least one 6719 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6720 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6721 * set (based on openmode specified by app). 6722 */ 6723 if ((flags & (FREAD|FWRITE)) == 0) 6724 flags |= (FREAD|FWRITE); 6725 6726 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6727 6728 if (vp != NULL) { 6729 /* if create was successful, throw away the file's pages */ 6730 if (!error && (vattr.va_mask & AT_SIZE)) 6731 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6732 cr); 6733 /* release the lookup hold */ 6734 VN_RELE(vp); 6735 vp = NULL; 6736 } 6737 6738 /* 6739 * validate that we opened a regular file. This handles a misbehaving 6740 * server that returns an incorrect FH. 6741 */ 6742 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6743 error = EISDIR; 6744 VN_RELE(*vpp); 6745 } 6746 6747 /* 6748 * If this is not an exclusive create, then the CREATE 6749 * request will be made with the GUARDED mode set. This 6750 * means that the server will return EEXIST if the file 6751 * exists. The file could exist because of a retransmitted 6752 * request. In this case, we recover by starting over and 6753 * checking to see whether the file exists. This second 6754 * time through it should and a CREATE request will not be 6755 * sent. 6756 * 6757 * This handles the problem of a dangling CREATE request 6758 * which contains attributes which indicate that the file 6759 * should be truncated. This retransmitted request could 6760 * possibly truncate valid data in the file if not caught 6761 * by the duplicate request mechanism on the server or if 6762 * not caught by other means. The scenario is: 6763 * 6764 * Client transmits CREATE request with size = 0 6765 * Client times out, retransmits request. 6766 * Response to the first request arrives from the server 6767 * and the client proceeds on. 6768 * Client writes data to the file. 6769 * The server now processes retransmitted CREATE request 6770 * and truncates file. 6771 * 6772 * The use of the GUARDED CREATE request prevents this from 6773 * happening because the retransmitted CREATE would fail 6774 * with EEXIST and would not truncate the file. 6775 */ 6776 if (error == EEXIST && exclusive == NONEXCL) { 6777 #ifdef DEBUG 6778 nfs4_create_misses++; 6779 #endif 6780 goto top; 6781 } 6782 nfs_rw_exit(&drp->r_rwlock); 6783 if (truncating && !error && *vpp) { 6784 vnode_t *tvp; 6785 rnode4_t *trp; 6786 /* 6787 * existing file got truncated, notify. 6788 */ 6789 tvp = *vpp; 6790 trp = VTOR4(tvp); 6791 if (IS_SHADOW(tvp, trp)) 6792 tvp = RTOV4(trp); 6793 vnevent_create(tvp, ct); 6794 } 6795 return (error); 6796 } 6797 6798 /* 6799 * Create compound (for mkdir, mknod, symlink): 6800 * { Putfh <dfh>; Create; Getfh; Getattr } 6801 * It's okay if setattr failed to set gid - this is not considered 6802 * an error, but purge attrs in that case. 6803 */ 6804 static int 6805 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6806 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6807 { 6808 int need_end_op = FALSE; 6809 COMPOUND4args_clnt args; 6810 COMPOUND4res_clnt res, *resp = NULL; 6811 nfs_argop4 *argop; 6812 nfs_resop4 *resop; 6813 int doqueue; 6814 mntinfo4_t *mi; 6815 rnode4_t *drp = VTOR4(dvp); 6816 change_info4 *cinfo; 6817 GETFH4res *gf_res; 6818 struct vattr vattr; 6819 vnode_t *vp; 6820 fattr4 *crattr; 6821 bool_t needrecov = FALSE; 6822 nfs4_recov_state_t recov_state; 6823 nfs4_sharedfh_t *sfhp = NULL; 6824 hrtime_t t; 6825 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6826 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6827 dirattr_info_t dinfo, *dinfop; 6828 servinfo4_t *svp; 6829 bitmap4 supp_attrs; 6830 6831 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6832 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6833 6834 mi = VTOMI4(dvp); 6835 6836 /* 6837 * Make sure we properly deal with setting the right gid 6838 * on a new directory to reflect the parent's setgid bit 6839 */ 6840 setgid_flag = 0; 6841 if (type == NF4DIR) { 6842 struct vattr dva; 6843 6844 va->va_mode &= ~VSGID; 6845 dva.va_mask = AT_MODE | AT_GID; 6846 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) { 6847 6848 /* 6849 * If the parent's directory has the setgid bit set 6850 * _and_ the client was able to get a valid mapping 6851 * for the parent dir's owner_group, we want to 6852 * append NVERIFY(owner_group == dva.va_gid) and 6853 * SETTATTR to the CREATE compound. 6854 */ 6855 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6856 setgid_flag = 1; 6857 va->va_mode |= VSGID; 6858 if (dva.va_gid != GID_NOBODY) { 6859 va->va_mask |= AT_GID; 6860 va->va_gid = dva.va_gid; 6861 } 6862 } 6863 } 6864 } 6865 6866 /* 6867 * Create ops: 6868 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6869 * 5:restorefh(dir) 6:getattr(dir) 6870 * 6871 * if (setgid) 6872 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6873 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6874 * 8:nverify 9:setattr 6875 */ 6876 if (setgid_flag) { 6877 numops = 10; 6878 idx_create = 1; 6879 idx_fattr = 3; 6880 } else { 6881 numops = 7; 6882 idx_create = 2; 6883 idx_fattr = 4; 6884 } 6885 6886 ASSERT(nfs_zone() == mi->mi_zone); 6887 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6888 return (EINTR); 6889 } 6890 recov_state.rs_flags = 0; 6891 recov_state.rs_num_retry_despite_err = 0; 6892 6893 argoplist_size = numops * sizeof (nfs_argop4); 6894 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6895 6896 recov_retry: 6897 if (type == NF4LNK) 6898 args.ctag = TAG_SYMLINK; 6899 else if (type == NF4DIR) 6900 args.ctag = TAG_MKDIR; 6901 else 6902 args.ctag = TAG_MKNOD; 6903 6904 args.array_len = numops; 6905 args.array = argop; 6906 6907 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6908 nfs_rw_exit(&drp->r_rwlock); 6909 kmem_free(argop, argoplist_size); 6910 return (e.error); 6911 } 6912 need_end_op = TRUE; 6913 6914 6915 /* 0: putfh directory */ 6916 argop[0].argop = OP_CPUTFH; 6917 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6918 6919 /* 1/2: Create object */ 6920 argop[idx_create].argop = OP_CCREATE; 6921 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6922 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6923 if (type == NF4LNK) { 6924 /* 6925 * symlink, treat name as data 6926 */ 6927 ASSERT(data != NULL); 6928 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6929 (char *)data; 6930 } 6931 if (type == NF4BLK || type == NF4CHR) { 6932 ASSERT(data != NULL); 6933 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6934 *((specdata4 *)data); 6935 } 6936 6937 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6938 6939 svp = drp->r_server; 6940 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6941 supp_attrs = svp->sv_supp_attrs; 6942 nfs_rw_exit(&svp->sv_lock); 6943 6944 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6945 nfs_rw_exit(&drp->r_rwlock); 6946 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6947 e.error = EINVAL; 6948 kmem_free(argop, argoplist_size); 6949 return (e.error); 6950 } 6951 6952 /* 2/3: getfh fh of created object */ 6953 ASSERT(idx_create + 1 == idx_fattr - 1); 6954 argop[idx_create + 1].argop = OP_GETFH; 6955 6956 /* 3/4: getattr of new object */ 6957 argop[idx_fattr].argop = OP_GETATTR; 6958 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6959 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6960 6961 if (setgid_flag) { 6962 vattr_t _v; 6963 6964 argop[4].argop = OP_SAVEFH; 6965 6966 argop[5].argop = OP_CPUTFH; 6967 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6968 6969 argop[6].argop = OP_GETATTR; 6970 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6971 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6972 6973 argop[7].argop = OP_RESTOREFH; 6974 6975 /* 6976 * nverify 6977 * 6978 * XXX - Revisit the last argument to nfs4_end_op() 6979 * once 5020486 is fixed. 6980 */ 6981 _v.va_mask = AT_GID; 6982 _v.va_gid = va->va_gid; 6983 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6984 supp_attrs)) { 6985 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6986 nfs_rw_exit(&drp->r_rwlock); 6987 nfs4_fattr4_free(crattr); 6988 kmem_free(argop, argoplist_size); 6989 return (e.error); 6990 } 6991 6992 /* 6993 * setattr 6994 * 6995 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6996 * so no need for stateid or flags. Also we specify NULL 6997 * rp since we're only interested in setting owner_group 6998 * attributes. 6999 */ 7000 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 7001 &e.error, 0); 7002 7003 if (e.error) { 7004 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 7005 nfs_rw_exit(&drp->r_rwlock); 7006 nfs4_fattr4_free(crattr); 7007 nfs4args_verify_free(&argop[8]); 7008 kmem_free(argop, argoplist_size); 7009 return (e.error); 7010 } 7011 } else { 7012 argop[1].argop = OP_SAVEFH; 7013 7014 argop[5].argop = OP_RESTOREFH; 7015 7016 argop[6].argop = OP_GETATTR; 7017 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7018 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7019 } 7020 7021 dnlc_remove(dvp, nm); 7022 7023 doqueue = 1; 7024 t = gethrtime(); 7025 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7026 7027 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7028 if (e.error) { 7029 PURGE_ATTRCACHE4(dvp); 7030 if (!needrecov) 7031 goto out; 7032 } 7033 7034 if (needrecov) { 7035 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 7036 OP_CREATE, NULL, NULL, NULL) == FALSE) { 7037 nfs4_end_op(mi, dvp, NULL, &recov_state, 7038 needrecov); 7039 need_end_op = FALSE; 7040 nfs4_fattr4_free(crattr); 7041 if (setgid_flag) { 7042 nfs4args_verify_free(&argop[8]); 7043 nfs4args_setattr_free(&argop[9]); 7044 } 7045 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 7046 goto recov_retry; 7047 } 7048 } 7049 7050 resp = &res; 7051 7052 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 7053 7054 if (res.status == NFS4ERR_BADOWNER) 7055 nfs4_log_badowner(mi, OP_CREATE); 7056 7057 e.error = geterrno4(res.status); 7058 7059 /* 7060 * This check is left over from when create was implemented 7061 * using a setattr op (instead of createattrs). If the 7062 * putfh/create/getfh failed, the error was returned. If 7063 * setattr/getattr failed, we keep going. 7064 * 7065 * It might be better to get rid of the GETFH also, and just 7066 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 7067 * Then if any of the operations failed, we could return the 7068 * error now, and remove much of the error code below. 7069 */ 7070 if (res.array_len <= idx_fattr) { 7071 /* 7072 * Either Putfh, Create or Getfh failed. 7073 */ 7074 PURGE_ATTRCACHE4(dvp); 7075 /* 7076 * nfs4_purge_stale_fh() may generate otw calls through 7077 * nfs4_invalidate_pages. Hence the need to call 7078 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 7079 */ 7080 nfs4_end_op(mi, dvp, NULL, &recov_state, 7081 needrecov); 7082 need_end_op = FALSE; 7083 nfs4_purge_stale_fh(e.error, dvp, cr); 7084 goto out; 7085 } 7086 } 7087 7088 resop = &res.array[idx_create]; /* create res */ 7089 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 7090 7091 resop = &res.array[idx_create + 1]; /* getfh res */ 7092 gf_res = &resop->nfs_resop4_u.opgetfh; 7093 7094 sfhp = sfh4_get(&gf_res->object, mi); 7095 if (e.error) { 7096 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 7097 fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7098 if (vp->v_type == VNON) { 7099 vattr.va_mask = AT_TYPE; 7100 /* 7101 * Need to call nfs4_end_op before nfs4getattr to avoid 7102 * potential nfs4_start_op deadlock. See RFE 4777612. 7103 */ 7104 nfs4_end_op(mi, dvp, NULL, &recov_state, 7105 needrecov); 7106 need_end_op = FALSE; 7107 e.error = nfs4getattr(vp, &vattr, cr); 7108 if (e.error) { 7109 VN_RELE(vp); 7110 *vpp = NULL; 7111 goto out; 7112 } 7113 vp->v_type = vattr.va_type; 7114 } 7115 e.error = 0; 7116 } else { 7117 *vpp = vp = makenfs4node(sfhp, 7118 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 7119 dvp->v_vfsp, t, cr, 7120 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7121 } 7122 7123 /* 7124 * If compound succeeded, then update dir attrs 7125 */ 7126 if (res.status == NFS4_OK) { 7127 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 7128 dinfo.di_cred = cr; 7129 dinfo.di_time_call = t; 7130 dinfop = &dinfo; 7131 } else 7132 dinfop = NULL; 7133 7134 /* Update directory cache attribute, readdir and dnlc caches */ 7135 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 7136 7137 out: 7138 if (sfhp != NULL) 7139 sfh4_rele(&sfhp); 7140 nfs_rw_exit(&drp->r_rwlock); 7141 nfs4_fattr4_free(crattr); 7142 if (setgid_flag) { 7143 nfs4args_verify_free(&argop[8]); 7144 nfs4args_setattr_free(&argop[9]); 7145 } 7146 if (resp) 7147 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7148 if (need_end_op) 7149 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7150 7151 kmem_free(argop, argoplist_size); 7152 return (e.error); 7153 } 7154 7155 /* ARGSUSED */ 7156 static int 7157 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7158 int mode, vnode_t **vpp, cred_t *cr) 7159 { 7160 int error; 7161 vnode_t *vp; 7162 nfs_ftype4 type; 7163 specdata4 spec, *specp = NULL; 7164 7165 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7166 7167 switch (va->va_type) { 7168 case VCHR: 7169 case VBLK: 7170 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7171 spec.specdata1 = getmajor(va->va_rdev); 7172 spec.specdata2 = getminor(va->va_rdev); 7173 specp = &spec; 7174 break; 7175 7176 case VFIFO: 7177 type = NF4FIFO; 7178 break; 7179 case VSOCK: 7180 type = NF4SOCK; 7181 break; 7182 7183 default: 7184 return (EINVAL); 7185 } 7186 7187 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7188 if (error) { 7189 return (error); 7190 } 7191 7192 /* 7193 * This might not be needed any more; special case to deal 7194 * with problematic v2/v3 servers. Since create was unable 7195 * to set group correctly, not sure what hope setattr has. 7196 */ 7197 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7198 va->va_mask = AT_GID; 7199 (void) nfs4setattr(vp, va, 0, cr, NULL); 7200 } 7201 7202 /* 7203 * If vnode is a device create special vnode 7204 */ 7205 if (ISVDEV(vp->v_type)) { 7206 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7207 VN_RELE(vp); 7208 } else { 7209 *vpp = vp; 7210 } 7211 return (error); 7212 } 7213 7214 /* 7215 * Remove requires that the current fh be the target directory. 7216 * After the operation, the current fh is unchanged. 7217 * The compound op structure is: 7218 * PUTFH(targetdir), REMOVE 7219 * 7220 * Weirdness: if the vnode to be removed is open 7221 * we rename it instead of removing it and nfs_inactive 7222 * will remove the new name. 7223 */ 7224 /* ARGSUSED */ 7225 static int 7226 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags) 7227 { 7228 COMPOUND4args_clnt args; 7229 COMPOUND4res_clnt res, *resp = NULL; 7230 REMOVE4res *rm_res; 7231 nfs_argop4 argop[3]; 7232 nfs_resop4 *resop; 7233 vnode_t *vp; 7234 char *tmpname; 7235 int doqueue; 7236 mntinfo4_t *mi; 7237 rnode4_t *rp; 7238 rnode4_t *drp; 7239 int needrecov = 0; 7240 nfs4_recov_state_t recov_state; 7241 int isopen; 7242 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7243 dirattr_info_t dinfo; 7244 7245 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7246 return (EPERM); 7247 drp = VTOR4(dvp); 7248 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7249 return (EINTR); 7250 7251 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7252 if (e.error) { 7253 nfs_rw_exit(&drp->r_rwlock); 7254 return (e.error); 7255 } 7256 7257 if (vp->v_type == VDIR) { 7258 VN_RELE(vp); 7259 nfs_rw_exit(&drp->r_rwlock); 7260 return (EISDIR); 7261 } 7262 7263 /* 7264 * First just remove the entry from the name cache, as it 7265 * is most likely the only entry for this vp. 7266 */ 7267 dnlc_remove(dvp, nm); 7268 7269 rp = VTOR4(vp); 7270 7271 /* 7272 * For regular file types, check to see if the file is open by looking 7273 * at the open streams. 7274 * For all other types, check the reference count on the vnode. Since 7275 * they are not opened OTW they never have an open stream. 7276 * 7277 * If the file is open, rename it to .nfsXXXX. 7278 */ 7279 if (vp->v_type != VREG) { 7280 /* 7281 * If the file has a v_count > 1 then there may be more than one 7282 * entry in the name cache due multiple links or an open file, 7283 * but we don't have the real reference count so flush all 7284 * possible entries. 7285 */ 7286 if (vp->v_count > 1) 7287 dnlc_purge_vp(vp); 7288 7289 /* 7290 * Now we have the real reference count. 7291 */ 7292 isopen = vp->v_count > 1; 7293 } else { 7294 mutex_enter(&rp->r_os_lock); 7295 isopen = list_head(&rp->r_open_streams) != NULL; 7296 mutex_exit(&rp->r_os_lock); 7297 } 7298 7299 mutex_enter(&rp->r_statelock); 7300 if (isopen && 7301 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7302 mutex_exit(&rp->r_statelock); 7303 tmpname = newname(); 7304 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct); 7305 if (e.error) 7306 kmem_free(tmpname, MAXNAMELEN); 7307 else { 7308 mutex_enter(&rp->r_statelock); 7309 if (rp->r_unldvp == NULL) { 7310 VN_HOLD(dvp); 7311 rp->r_unldvp = dvp; 7312 if (rp->r_unlcred != NULL) 7313 crfree(rp->r_unlcred); 7314 crhold(cr); 7315 rp->r_unlcred = cr; 7316 rp->r_unlname = tmpname; 7317 } else { 7318 kmem_free(rp->r_unlname, MAXNAMELEN); 7319 rp->r_unlname = tmpname; 7320 } 7321 mutex_exit(&rp->r_statelock); 7322 } 7323 VN_RELE(vp); 7324 nfs_rw_exit(&drp->r_rwlock); 7325 return (e.error); 7326 } 7327 /* 7328 * Actually remove the file/dir 7329 */ 7330 mutex_exit(&rp->r_statelock); 7331 7332 /* 7333 * We need to flush any dirty pages which happen to 7334 * be hanging around before removing the file. 7335 * This shouldn't happen very often since in NFSv4 7336 * we should be close to open consistent. 7337 */ 7338 if (nfs4_has_pages(vp) && 7339 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7340 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct); 7341 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7342 mutex_enter(&rp->r_statelock); 7343 if (!rp->r_error) 7344 rp->r_error = e.error; 7345 mutex_exit(&rp->r_statelock); 7346 } 7347 } 7348 7349 mi = VTOMI4(dvp); 7350 7351 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7352 recov_state.rs_flags = 0; 7353 recov_state.rs_num_retry_despite_err = 0; 7354 7355 recov_retry: 7356 /* 7357 * Remove ops: putfh dir; remove 7358 */ 7359 args.ctag = TAG_REMOVE; 7360 args.array_len = 3; 7361 args.array = argop; 7362 7363 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7364 if (e.error) { 7365 nfs_rw_exit(&drp->r_rwlock); 7366 VN_RELE(vp); 7367 return (e.error); 7368 } 7369 7370 /* putfh directory */ 7371 argop[0].argop = OP_CPUTFH; 7372 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7373 7374 /* remove */ 7375 argop[1].argop = OP_CREMOVE; 7376 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7377 7378 /* getattr dir */ 7379 argop[2].argop = OP_GETATTR; 7380 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7381 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7382 7383 doqueue = 1; 7384 dinfo.di_time_call = gethrtime(); 7385 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7386 7387 PURGE_ATTRCACHE4(vp); 7388 7389 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7390 if (e.error) 7391 PURGE_ATTRCACHE4(dvp); 7392 7393 if (needrecov) { 7394 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7395 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 7396 if (!e.error) 7397 (void) xdr_free(xdr_COMPOUND4res_clnt, 7398 (caddr_t)&res); 7399 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7400 needrecov); 7401 goto recov_retry; 7402 } 7403 } 7404 7405 /* 7406 * Matching nfs4_end_op() for start_op() above. 7407 * There is a path in the code below which calls 7408 * nfs4_purge_stale_fh(), which may generate otw calls through 7409 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7410 * here to avoid nfs4_start_op() deadlock. 7411 */ 7412 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7413 7414 if (!e.error) { 7415 resp = &res; 7416 7417 if (res.status) { 7418 e.error = geterrno4(res.status); 7419 PURGE_ATTRCACHE4(dvp); 7420 nfs4_purge_stale_fh(e.error, dvp, cr); 7421 } else { 7422 resop = &res.array[1]; /* remove res */ 7423 rm_res = &resop->nfs_resop4_u.opremove; 7424 7425 dinfo.di_garp = 7426 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7427 dinfo.di_cred = cr; 7428 7429 /* Update directory attr, readdir and dnlc caches */ 7430 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7431 &dinfo); 7432 } 7433 } 7434 nfs_rw_exit(&drp->r_rwlock); 7435 if (resp) 7436 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7437 7438 if (e.error == 0) { 7439 vnode_t *tvp; 7440 rnode4_t *trp; 7441 trp = VTOR4(vp); 7442 tvp = vp; 7443 if (IS_SHADOW(vp, trp)) 7444 tvp = RTOV4(trp); 7445 vnevent_remove(tvp, dvp, nm, ct); 7446 } 7447 VN_RELE(vp); 7448 return (e.error); 7449 } 7450 7451 /* 7452 * Link requires that the current fh be the target directory and the 7453 * saved fh be the source fh. After the operation, the current fh is unchanged. 7454 * Thus the compound op structure is: 7455 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7456 * GETATTR(file) 7457 */ 7458 /* ARGSUSED */ 7459 static int 7460 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr, 7461 caller_context_t *ct, int flags) 7462 { 7463 COMPOUND4args_clnt args; 7464 COMPOUND4res_clnt res, *resp = NULL; 7465 LINK4res *ln_res; 7466 int argoplist_size = 7 * sizeof (nfs_argop4); 7467 nfs_argop4 *argop; 7468 nfs_resop4 *resop; 7469 vnode_t *realvp, *nvp; 7470 int doqueue; 7471 mntinfo4_t *mi; 7472 rnode4_t *tdrp; 7473 bool_t needrecov = FALSE; 7474 nfs4_recov_state_t recov_state; 7475 hrtime_t t; 7476 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7477 dirattr_info_t dinfo; 7478 7479 ASSERT(*tnm != '\0'); 7480 ASSERT(tdvp->v_type == VDIR); 7481 ASSERT(nfs4_consistent_type(tdvp)); 7482 ASSERT(nfs4_consistent_type(svp)); 7483 7484 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7485 return (EPERM); 7486 if (VOP_REALVP(svp, &realvp, ct) == 0) { 7487 svp = realvp; 7488 ASSERT(nfs4_consistent_type(svp)); 7489 } 7490 7491 tdrp = VTOR4(tdvp); 7492 mi = VTOMI4(svp); 7493 7494 if (!(mi->mi_flags & MI4_LINK)) { 7495 return (EOPNOTSUPP); 7496 } 7497 recov_state.rs_flags = 0; 7498 recov_state.rs_num_retry_despite_err = 0; 7499 7500 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7501 return (EINTR); 7502 7503 recov_retry: 7504 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7505 7506 args.ctag = TAG_LINK; 7507 7508 /* 7509 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7510 * restorefh; getattr(fl) 7511 */ 7512 args.array_len = 7; 7513 args.array = argop; 7514 7515 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7516 if (e.error) { 7517 kmem_free(argop, argoplist_size); 7518 nfs_rw_exit(&tdrp->r_rwlock); 7519 return (e.error); 7520 } 7521 7522 /* 0. putfh file */ 7523 argop[0].argop = OP_CPUTFH; 7524 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7525 7526 /* 1. save current fh to free up the space for the dir */ 7527 argop[1].argop = OP_SAVEFH; 7528 7529 /* 2. putfh targetdir */ 7530 argop[2].argop = OP_CPUTFH; 7531 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7532 7533 /* 3. link: current_fh is targetdir, saved_fh is source */ 7534 argop[3].argop = OP_CLINK; 7535 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7536 7537 /* 4. Get attributes of dir */ 7538 argop[4].argop = OP_GETATTR; 7539 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7540 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7541 7542 /* 5. If link was successful, restore current vp to file */ 7543 argop[5].argop = OP_RESTOREFH; 7544 7545 /* 6. Get attributes of linked object */ 7546 argop[6].argop = OP_GETATTR; 7547 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7548 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7549 7550 dnlc_remove(tdvp, tnm); 7551 7552 doqueue = 1; 7553 t = gethrtime(); 7554 7555 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7556 7557 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7558 if (e.error != 0 && !needrecov) { 7559 PURGE_ATTRCACHE4(tdvp); 7560 PURGE_ATTRCACHE4(svp); 7561 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7562 goto out; 7563 } 7564 7565 if (needrecov) { 7566 bool_t abort; 7567 7568 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7569 NULL, NULL, OP_LINK, NULL, NULL, NULL); 7570 if (abort == FALSE) { 7571 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7572 needrecov); 7573 kmem_free(argop, argoplist_size); 7574 if (!e.error) 7575 (void) xdr_free(xdr_COMPOUND4res_clnt, 7576 (caddr_t)&res); 7577 goto recov_retry; 7578 } else { 7579 if (e.error != 0) { 7580 PURGE_ATTRCACHE4(tdvp); 7581 PURGE_ATTRCACHE4(svp); 7582 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7583 &recov_state, needrecov); 7584 goto out; 7585 } 7586 /* fall through for res.status case */ 7587 } 7588 } 7589 7590 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7591 7592 resp = &res; 7593 if (res.status) { 7594 /* If link succeeded, then don't return error */ 7595 e.error = geterrno4(res.status); 7596 if (res.array_len <= 4) { 7597 /* 7598 * Either Putfh, Savefh, Putfh dir, or Link failed 7599 */ 7600 PURGE_ATTRCACHE4(svp); 7601 PURGE_ATTRCACHE4(tdvp); 7602 if (e.error == EOPNOTSUPP) { 7603 mutex_enter(&mi->mi_lock); 7604 mi->mi_flags &= ~MI4_LINK; 7605 mutex_exit(&mi->mi_lock); 7606 } 7607 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7608 /* XXX-LP */ 7609 if (e.error == EISDIR && crgetuid(cr) != 0) 7610 e.error = EPERM; 7611 goto out; 7612 } 7613 } 7614 7615 /* either no error or one of the postop getattr failed */ 7616 7617 /* 7618 * XXX - if LINK succeeded, but no attrs were returned for link 7619 * file, purge its cache. 7620 * 7621 * XXX Perform a simplified version of wcc checking. Instead of 7622 * have another getattr to get pre-op, just purge cache if 7623 * any of the ops prior to and including the getattr failed. 7624 * If the getattr succeeded then update the attrcache accordingly. 7625 */ 7626 7627 /* 7628 * update cache with link file postattrs. 7629 * Note: at this point resop points to link res. 7630 */ 7631 resop = &res.array[3]; /* link res */ 7632 ln_res = &resop->nfs_resop4_u.oplink; 7633 if (res.status == NFS4_OK) 7634 e.error = nfs4_update_attrcache(res.status, 7635 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7636 t, svp, cr); 7637 7638 /* 7639 * Call makenfs4node to create the new shadow vp for tnm. 7640 * We pass NULL attrs because we just cached attrs for 7641 * the src object. All we're trying to accomplish is to 7642 * to create the new shadow vnode. 7643 */ 7644 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7645 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh)); 7646 7647 /* Update target cache attribute, readdir and dnlc caches */ 7648 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7649 dinfo.di_time_call = t; 7650 dinfo.di_cred = cr; 7651 7652 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7653 ASSERT(nfs4_consistent_type(tdvp)); 7654 ASSERT(nfs4_consistent_type(svp)); 7655 ASSERT(nfs4_consistent_type(nvp)); 7656 VN_RELE(nvp); 7657 7658 if (!e.error) { 7659 vnode_t *tvp; 7660 rnode4_t *trp; 7661 /* 7662 * Notify the source file of this link operation. 7663 */ 7664 trp = VTOR4(svp); 7665 tvp = svp; 7666 if (IS_SHADOW(svp, trp)) 7667 tvp = RTOV4(trp); 7668 vnevent_link(tvp, ct); 7669 } 7670 out: 7671 kmem_free(argop, argoplist_size); 7672 if (resp) 7673 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7674 7675 nfs_rw_exit(&tdrp->r_rwlock); 7676 7677 return (e.error); 7678 } 7679 7680 /* ARGSUSED */ 7681 static int 7682 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7683 caller_context_t *ct, int flags) 7684 { 7685 vnode_t *realvp; 7686 7687 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7688 return (EPERM); 7689 if (VOP_REALVP(ndvp, &realvp, ct) == 0) 7690 ndvp = realvp; 7691 7692 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct)); 7693 } 7694 7695 /* 7696 * nfs4rename does the real work of renaming in NFS Version 4. 7697 * 7698 * A file handle is considered volatile for renaming purposes if either 7699 * of the volatile bits are turned on. However, the compound may differ 7700 * based on the likelihood of the filehandle to change during rename. 7701 */ 7702 static int 7703 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7704 caller_context_t *ct) 7705 { 7706 int error; 7707 mntinfo4_t *mi; 7708 vnode_t *nvp = NULL; 7709 vnode_t *ovp = NULL; 7710 char *tmpname = NULL; 7711 rnode4_t *rp; 7712 rnode4_t *odrp; 7713 rnode4_t *ndrp; 7714 int did_link = 0; 7715 int do_link = 1; 7716 nfsstat4 stat = NFS4_OK; 7717 7718 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7719 ASSERT(nfs4_consistent_type(odvp)); 7720 ASSERT(nfs4_consistent_type(ndvp)); 7721 7722 if (onm[0] == '.' && (onm[1] == '\0' || 7723 (onm[1] == '.' && onm[2] == '\0'))) 7724 return (EINVAL); 7725 7726 if (nnm[0] == '.' && (nnm[1] == '\0' || 7727 (nnm[1] == '.' && nnm[2] == '\0'))) 7728 return (EINVAL); 7729 7730 odrp = VTOR4(odvp); 7731 ndrp = VTOR4(ndvp); 7732 if ((intptr_t)odrp < (intptr_t)ndrp) { 7733 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7734 return (EINTR); 7735 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7736 nfs_rw_exit(&odrp->r_rwlock); 7737 return (EINTR); 7738 } 7739 } else { 7740 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7741 return (EINTR); 7742 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7743 nfs_rw_exit(&ndrp->r_rwlock); 7744 return (EINTR); 7745 } 7746 } 7747 7748 /* 7749 * Lookup the target file. If it exists, it needs to be 7750 * checked to see whether it is a mount point and whether 7751 * it is active (open). 7752 */ 7753 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7754 if (!error) { 7755 int isactive; 7756 7757 ASSERT(nfs4_consistent_type(nvp)); 7758 /* 7759 * If this file has been mounted on, then just 7760 * return busy because renaming to it would remove 7761 * the mounted file system from the name space. 7762 */ 7763 if (vn_ismntpt(nvp)) { 7764 VN_RELE(nvp); 7765 nfs_rw_exit(&odrp->r_rwlock); 7766 nfs_rw_exit(&ndrp->r_rwlock); 7767 return (EBUSY); 7768 } 7769 7770 /* 7771 * First just remove the entry from the name cache, as it 7772 * is most likely the only entry for this vp. 7773 */ 7774 dnlc_remove(ndvp, nnm); 7775 7776 rp = VTOR4(nvp); 7777 7778 if (nvp->v_type != VREG) { 7779 /* 7780 * Purge the name cache of all references to this vnode 7781 * so that we can check the reference count to infer 7782 * whether it is active or not. 7783 */ 7784 if (nvp->v_count > 1) 7785 dnlc_purge_vp(nvp); 7786 7787 isactive = nvp->v_count > 1; 7788 } else { 7789 mutex_enter(&rp->r_os_lock); 7790 isactive = list_head(&rp->r_open_streams) != NULL; 7791 mutex_exit(&rp->r_os_lock); 7792 } 7793 7794 /* 7795 * If the vnode is active and is not a directory, 7796 * arrange to rename it to a 7797 * temporary file so that it will continue to be 7798 * accessible. This implements the "unlink-open-file" 7799 * semantics for the target of a rename operation. 7800 * Before doing this though, make sure that the 7801 * source and target files are not already the same. 7802 */ 7803 if (isactive && nvp->v_type != VDIR) { 7804 /* 7805 * Lookup the source name. 7806 */ 7807 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7808 7809 /* 7810 * The source name *should* already exist. 7811 */ 7812 if (error) { 7813 VN_RELE(nvp); 7814 nfs_rw_exit(&odrp->r_rwlock); 7815 nfs_rw_exit(&ndrp->r_rwlock); 7816 return (error); 7817 } 7818 7819 ASSERT(nfs4_consistent_type(ovp)); 7820 7821 /* 7822 * Compare the two vnodes. If they are the same, 7823 * just release all held vnodes and return success. 7824 */ 7825 if (VN_CMP(ovp, nvp)) { 7826 VN_RELE(ovp); 7827 VN_RELE(nvp); 7828 nfs_rw_exit(&odrp->r_rwlock); 7829 nfs_rw_exit(&ndrp->r_rwlock); 7830 return (0); 7831 } 7832 7833 /* 7834 * Can't mix and match directories and non- 7835 * directories in rename operations. We already 7836 * know that the target is not a directory. If 7837 * the source is a directory, return an error. 7838 */ 7839 if (ovp->v_type == VDIR) { 7840 VN_RELE(ovp); 7841 VN_RELE(nvp); 7842 nfs_rw_exit(&odrp->r_rwlock); 7843 nfs_rw_exit(&ndrp->r_rwlock); 7844 return (ENOTDIR); 7845 } 7846 link_call: 7847 /* 7848 * The target file exists, is not the same as 7849 * the source file, and is active. We first 7850 * try to Link it to a temporary filename to 7851 * avoid having the server removing the file 7852 * completely (which could cause data loss to 7853 * the user's POV in the event the Rename fails 7854 * -- see bug 1165874). 7855 */ 7856 /* 7857 * The do_link and did_link booleans are 7858 * introduced in the event we get NFS4ERR_FILE_OPEN 7859 * returned for the Rename. Some servers can 7860 * not Rename over an Open file, so they return 7861 * this error. The client needs to Remove the 7862 * newly created Link and do two Renames, just 7863 * as if the server didn't support LINK. 7864 */ 7865 tmpname = newname(); 7866 error = 0; 7867 7868 if (do_link) { 7869 error = nfs4_link(ndvp, nvp, tmpname, cr, 7870 NULL, 0); 7871 } 7872 if (error == EOPNOTSUPP || !do_link) { 7873 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7874 cr, NULL, 0); 7875 did_link = 0; 7876 } else { 7877 did_link = 1; 7878 } 7879 if (error) { 7880 kmem_free(tmpname, MAXNAMELEN); 7881 VN_RELE(ovp); 7882 VN_RELE(nvp); 7883 nfs_rw_exit(&odrp->r_rwlock); 7884 nfs_rw_exit(&ndrp->r_rwlock); 7885 return (error); 7886 } 7887 7888 mutex_enter(&rp->r_statelock); 7889 if (rp->r_unldvp == NULL) { 7890 VN_HOLD(ndvp); 7891 rp->r_unldvp = ndvp; 7892 if (rp->r_unlcred != NULL) 7893 crfree(rp->r_unlcred); 7894 crhold(cr); 7895 rp->r_unlcred = cr; 7896 rp->r_unlname = tmpname; 7897 } else { 7898 if (rp->r_unlname) 7899 kmem_free(rp->r_unlname, MAXNAMELEN); 7900 rp->r_unlname = tmpname; 7901 } 7902 mutex_exit(&rp->r_statelock); 7903 } 7904 7905 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7906 7907 ASSERT(nfs4_consistent_type(nvp)); 7908 } 7909 7910 if (ovp == NULL) { 7911 /* 7912 * When renaming directories to be a subdirectory of a 7913 * different parent, the dnlc entry for ".." will no 7914 * longer be valid, so it must be removed. 7915 * 7916 * We do a lookup here to determine whether we are renaming 7917 * a directory and we need to check if we are renaming 7918 * an unlinked file. This might have already been done 7919 * in previous code, so we check ovp == NULL to avoid 7920 * doing it twice. 7921 */ 7922 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7923 /* 7924 * The source name *should* already exist. 7925 */ 7926 if (error) { 7927 nfs_rw_exit(&odrp->r_rwlock); 7928 nfs_rw_exit(&ndrp->r_rwlock); 7929 if (nvp) { 7930 VN_RELE(nvp); 7931 } 7932 return (error); 7933 } 7934 ASSERT(ovp != NULL); 7935 ASSERT(nfs4_consistent_type(ovp)); 7936 } 7937 7938 /* 7939 * Is the object being renamed a dir, and if so, is 7940 * it being renamed to a child of itself? The underlying 7941 * fs should ultimately return EINVAL for this case; 7942 * however, buggy beta non-Solaris NFSv4 servers at 7943 * interop testing events have allowed this behavior, 7944 * and it caused our client to panic due to a recursive 7945 * mutex_enter in fn_move. 7946 * 7947 * The tedious locking in fn_move could be changed to 7948 * deal with this case, and the client could avoid the 7949 * panic; however, the client would just confuse itself 7950 * later and misbehave. A better way to handle the broken 7951 * server is to detect this condition and return EINVAL 7952 * without ever sending the the bogus rename to the server. 7953 * We know the rename is invalid -- just fail it now. 7954 */ 7955 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7956 VN_RELE(ovp); 7957 nfs_rw_exit(&odrp->r_rwlock); 7958 nfs_rw_exit(&ndrp->r_rwlock); 7959 if (nvp) { 7960 VN_RELE(nvp); 7961 } 7962 return (EINVAL); 7963 } 7964 7965 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7966 7967 /* 7968 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7969 * possible for the filehandle to change due to the rename. 7970 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7971 * the fh will not change because of the rename, but we still need 7972 * to update its rnode entry with the new name for 7973 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7974 * has no effect on these for now, but for future improvements, 7975 * we might want to use it too to simplify handling of files 7976 * that are open with that flag on. (XXX) 7977 */ 7978 mi = VTOMI4(odvp); 7979 if (NFS4_VOLATILE_FH(mi)) 7980 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7981 &stat); 7982 else 7983 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7984 &stat); 7985 7986 ASSERT(nfs4_consistent_type(odvp)); 7987 ASSERT(nfs4_consistent_type(ndvp)); 7988 ASSERT(nfs4_consistent_type(ovp)); 7989 7990 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7991 do_link = 0; 7992 /* 7993 * Before the 'link_call' code, we did a nfs4_lookup 7994 * that puts a VN_HOLD on nvp. After the nfs4_link 7995 * call we call VN_RELE to match that hold. We need 7996 * to place an additional VN_HOLD here since we will 7997 * be hitting that VN_RELE again. 7998 */ 7999 VN_HOLD(nvp); 8000 8001 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0); 8002 8003 /* Undo the unlinked file naming stuff we just did */ 8004 mutex_enter(&rp->r_statelock); 8005 if (rp->r_unldvp) { 8006 VN_RELE(ndvp); 8007 rp->r_unldvp = NULL; 8008 if (rp->r_unlcred != NULL) 8009 crfree(rp->r_unlcred); 8010 rp->r_unlcred = NULL; 8011 /* rp->r_unlanme points to tmpname */ 8012 if (rp->r_unlname) 8013 kmem_free(rp->r_unlname, MAXNAMELEN); 8014 rp->r_unlname = NULL; 8015 } 8016 mutex_exit(&rp->r_statelock); 8017 8018 if (nvp) { 8019 VN_RELE(nvp); 8020 } 8021 goto link_call; 8022 } 8023 8024 if (error) { 8025 VN_RELE(ovp); 8026 nfs_rw_exit(&odrp->r_rwlock); 8027 nfs_rw_exit(&ndrp->r_rwlock); 8028 if (nvp) { 8029 VN_RELE(nvp); 8030 } 8031 return (error); 8032 } 8033 8034 /* 8035 * when renaming directories to be a subdirectory of a 8036 * different parent, the dnlc entry for ".." will no 8037 * longer be valid, so it must be removed 8038 */ 8039 rp = VTOR4(ovp); 8040 if (ndvp != odvp) { 8041 if (ovp->v_type == VDIR) { 8042 dnlc_remove(ovp, ".."); 8043 if (rp->r_dir != NULL) 8044 nfs4_purge_rddir_cache(ovp); 8045 } 8046 } 8047 8048 /* 8049 * If we are renaming the unlinked file, update the 8050 * r_unldvp and r_unlname as needed. 8051 */ 8052 mutex_enter(&rp->r_statelock); 8053 if (rp->r_unldvp != NULL) { 8054 if (strcmp(rp->r_unlname, onm) == 0) { 8055 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 8056 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 8057 if (ndvp != rp->r_unldvp) { 8058 VN_RELE(rp->r_unldvp); 8059 rp->r_unldvp = ndvp; 8060 VN_HOLD(ndvp); 8061 } 8062 } 8063 } 8064 mutex_exit(&rp->r_statelock); 8065 8066 /* 8067 * Notify the rename vnevents to source vnode, and to the target 8068 * vnode if it already existed. 8069 */ 8070 if (error == 0) { 8071 vnode_t *tvp; 8072 rnode4_t *trp; 8073 /* 8074 * Notify the vnode. Each links is represented by 8075 * a different vnode, in nfsv4. 8076 */ 8077 if (nvp) { 8078 trp = VTOR4(nvp); 8079 tvp = nvp; 8080 if (IS_SHADOW(nvp, trp)) 8081 tvp = RTOV4(trp); 8082 vnevent_rename_dest(tvp, ndvp, nnm, ct); 8083 } 8084 8085 /* 8086 * if the source and destination directory are not the 8087 * same notify the destination directory. 8088 */ 8089 if (VTOR4(odvp) != VTOR4(ndvp)) { 8090 trp = VTOR4(ndvp); 8091 tvp = ndvp; 8092 if (IS_SHADOW(ndvp, trp)) 8093 tvp = RTOV4(trp); 8094 vnevent_rename_dest_dir(tvp, ct); 8095 } 8096 8097 trp = VTOR4(ovp); 8098 tvp = ovp; 8099 if (IS_SHADOW(ovp, trp)) 8100 tvp = RTOV4(trp); 8101 vnevent_rename_src(tvp, odvp, onm, ct); 8102 } 8103 8104 if (nvp) { 8105 VN_RELE(nvp); 8106 } 8107 VN_RELE(ovp); 8108 8109 nfs_rw_exit(&odrp->r_rwlock); 8110 nfs_rw_exit(&ndrp->r_rwlock); 8111 8112 return (error); 8113 } 8114 8115 /* 8116 * When the parent directory has changed, sv_dfh must be updated 8117 */ 8118 static void 8119 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp) 8120 { 8121 svnode_t *sv = VTOSV(vp); 8122 nfs4_sharedfh_t *old_dfh = sv->sv_dfh; 8123 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh; 8124 8125 sfh4_hold(new_dfh); 8126 sv->sv_dfh = new_dfh; 8127 sfh4_rele(&old_dfh); 8128 } 8129 8130 /* 8131 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 8132 * when it is known that the filehandle is persistent through rename. 8133 * 8134 * Rename requires that the current fh be the target directory and the 8135 * saved fh be the source directory. After the operation, the current fh 8136 * is unchanged. 8137 * The compound op structure for persistent fh rename is: 8138 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 8139 * Rather than bother with the directory postop args, we'll simply 8140 * update that a change occurred in the cache, so no post-op getattrs. 8141 */ 8142 static int 8143 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 8144 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8145 { 8146 COMPOUND4args_clnt args; 8147 COMPOUND4res_clnt res, *resp = NULL; 8148 nfs_argop4 *argop; 8149 nfs_resop4 *resop; 8150 int doqueue, argoplist_size; 8151 mntinfo4_t *mi; 8152 rnode4_t *odrp = VTOR4(odvp); 8153 rnode4_t *ndrp = VTOR4(ndvp); 8154 RENAME4res *rn_res; 8155 bool_t needrecov; 8156 nfs4_recov_state_t recov_state; 8157 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8158 dirattr_info_t dinfo, *dinfop; 8159 8160 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8161 8162 recov_state.rs_flags = 0; 8163 recov_state.rs_num_retry_despite_err = 0; 8164 8165 /* 8166 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 8167 * 8168 * If source/target are different dirs, then append putfh(src); getattr 8169 */ 8170 args.array_len = (odvp == ndvp) ? 5 : 7; 8171 argoplist_size = args.array_len * sizeof (nfs_argop4); 8172 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8173 8174 recov_retry: 8175 *statp = NFS4_OK; 8176 8177 /* No need to Lookup the file, persistent fh */ 8178 args.ctag = TAG_RENAME; 8179 8180 mi = VTOMI4(odvp); 8181 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8182 if (e.error) { 8183 kmem_free(argop, argoplist_size); 8184 return (e.error); 8185 } 8186 8187 /* 0: putfh source directory */ 8188 argop[0].argop = OP_CPUTFH; 8189 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8190 8191 /* 1: Save source fh to free up current for target */ 8192 argop[1].argop = OP_SAVEFH; 8193 8194 /* 2: putfh targetdir */ 8195 argop[2].argop = OP_CPUTFH; 8196 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8197 8198 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8199 argop[3].argop = OP_CRENAME; 8200 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8201 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8202 8203 /* 4: getattr (targetdir) */ 8204 argop[4].argop = OP_GETATTR; 8205 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8206 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8207 8208 if (ndvp != odvp) { 8209 8210 /* 5: putfh (sourcedir) */ 8211 argop[5].argop = OP_CPUTFH; 8212 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8213 8214 /* 6: getattr (sourcedir) */ 8215 argop[6].argop = OP_GETATTR; 8216 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8217 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8218 } 8219 8220 dnlc_remove(odvp, onm); 8221 dnlc_remove(ndvp, nnm); 8222 8223 doqueue = 1; 8224 dinfo.di_time_call = gethrtime(); 8225 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8226 8227 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8228 if (e.error) { 8229 PURGE_ATTRCACHE4(odvp); 8230 PURGE_ATTRCACHE4(ndvp); 8231 } else { 8232 *statp = res.status; 8233 } 8234 8235 if (needrecov) { 8236 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8237 OP_RENAME, NULL, NULL, NULL) == FALSE) { 8238 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8239 if (!e.error) 8240 (void) xdr_free(xdr_COMPOUND4res_clnt, 8241 (caddr_t)&res); 8242 goto recov_retry; 8243 } 8244 } 8245 8246 if (!e.error) { 8247 resp = &res; 8248 /* 8249 * as long as OP_RENAME 8250 */ 8251 if (res.status != NFS4_OK && res.array_len <= 4) { 8252 e.error = geterrno4(res.status); 8253 PURGE_ATTRCACHE4(odvp); 8254 PURGE_ATTRCACHE4(ndvp); 8255 /* 8256 * System V defines rename to return EEXIST, not 8257 * ENOTEMPTY if the target directory is not empty. 8258 * Over the wire, the error is NFSERR_ENOTEMPTY 8259 * which geterrno4 maps to ENOTEMPTY. 8260 */ 8261 if (e.error == ENOTEMPTY) 8262 e.error = EEXIST; 8263 } else { 8264 8265 resop = &res.array[3]; /* rename res */ 8266 rn_res = &resop->nfs_resop4_u.oprename; 8267 8268 if (res.status == NFS4_OK) { 8269 /* 8270 * Update target attribute, readdir and dnlc 8271 * caches. 8272 */ 8273 dinfo.di_garp = 8274 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8275 dinfo.di_cred = cr; 8276 dinfop = &dinfo; 8277 } else 8278 dinfop = NULL; 8279 8280 nfs4_update_dircaches(&rn_res->target_cinfo, 8281 ndvp, NULL, NULL, dinfop); 8282 8283 /* 8284 * Update source attribute, readdir and dnlc caches 8285 * 8286 */ 8287 if (ndvp != odvp) { 8288 update_parentdir_sfh(renvp, ndvp); 8289 8290 if (dinfop) 8291 dinfo.di_garp = 8292 &(res.array[6].nfs_resop4_u. 8293 opgetattr.ga_res); 8294 8295 nfs4_update_dircaches(&rn_res->source_cinfo, 8296 odvp, NULL, NULL, dinfop); 8297 } 8298 8299 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8300 nnm); 8301 } 8302 } 8303 8304 if (resp) 8305 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8306 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8307 kmem_free(argop, argoplist_size); 8308 8309 return (e.error); 8310 } 8311 8312 /* 8313 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8314 * it is possible for the filehandle to change due to the rename. 8315 * 8316 * The compound req in this case includes a post-rename lookup and getattr 8317 * to ensure that we have the correct fh and attributes for the object. 8318 * 8319 * Rename requires that the current fh be the target directory and the 8320 * saved fh be the source directory. After the operation, the current fh 8321 * is unchanged. 8322 * 8323 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8324 * update the filehandle for the renamed object. We also get the old 8325 * filehandle for historical reasons; this should be taken out sometime. 8326 * This results in a rather cumbersome compound... 8327 * 8328 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8329 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8330 * 8331 */ 8332 static int 8333 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8334 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8335 { 8336 COMPOUND4args_clnt args; 8337 COMPOUND4res_clnt res, *resp = NULL; 8338 int argoplist_size; 8339 nfs_argop4 *argop; 8340 nfs_resop4 *resop; 8341 int doqueue; 8342 mntinfo4_t *mi; 8343 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8344 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8345 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8346 RENAME4res *rn_res; 8347 GETFH4res *ngf_res; 8348 bool_t needrecov; 8349 nfs4_recov_state_t recov_state; 8350 hrtime_t t; 8351 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8352 dirattr_info_t dinfo, *dinfop = &dinfo; 8353 8354 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8355 8356 recov_state.rs_flags = 0; 8357 recov_state.rs_num_retry_despite_err = 0; 8358 8359 recov_retry: 8360 *statp = NFS4_OK; 8361 8362 /* 8363 * There is a window between the RPC and updating the path and 8364 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8365 * code, so that it doesn't try to use the old path during that 8366 * window. 8367 */ 8368 mutex_enter(&orp->r_statelock); 8369 while (orp->r_flags & R4RECEXPFH) { 8370 klwp_t *lwp = ttolwp(curthread); 8371 8372 if (lwp != NULL) 8373 lwp->lwp_nostop++; 8374 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8375 mutex_exit(&orp->r_statelock); 8376 if (lwp != NULL) 8377 lwp->lwp_nostop--; 8378 return (EINTR); 8379 } 8380 if (lwp != NULL) 8381 lwp->lwp_nostop--; 8382 } 8383 orp->r_flags |= R4RECEXPFH; 8384 mutex_exit(&orp->r_statelock); 8385 8386 mi = VTOMI4(odvp); 8387 8388 args.ctag = TAG_RENAME_VFH; 8389 args.array_len = (odvp == ndvp) ? 10 : 12; 8390 argoplist_size = args.array_len * sizeof (nfs_argop4); 8391 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8392 8393 /* 8394 * Rename ops: 8395 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8396 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8397 * LOOKUP(trgt), GETFH(new), GETATTR, 8398 * 8399 * if (odvp != ndvp) 8400 * add putfh(sourcedir), getattr(sourcedir) } 8401 */ 8402 args.array = argop; 8403 8404 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8405 &recov_state, NULL); 8406 if (e.error) { 8407 kmem_free(argop, argoplist_size); 8408 mutex_enter(&orp->r_statelock); 8409 orp->r_flags &= ~R4RECEXPFH; 8410 cv_broadcast(&orp->r_cv); 8411 mutex_exit(&orp->r_statelock); 8412 return (e.error); 8413 } 8414 8415 /* 0: putfh source directory */ 8416 argop[0].argop = OP_CPUTFH; 8417 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8418 8419 /* 1: Save source fh to free up current for target */ 8420 argop[1].argop = OP_SAVEFH; 8421 8422 /* 2: Lookup pre-rename fh of renamed object */ 8423 argop[2].argop = OP_CLOOKUP; 8424 argop[2].nfs_argop4_u.opclookup.cname = onm; 8425 8426 /* 3: getfh fh of renamed object (before rename) */ 8427 argop[3].argop = OP_GETFH; 8428 8429 /* 4: putfh targetdir */ 8430 argop[4].argop = OP_CPUTFH; 8431 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8432 8433 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8434 argop[5].argop = OP_CRENAME; 8435 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8436 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8437 8438 /* 6: getattr of target dir (post op attrs) */ 8439 argop[6].argop = OP_GETATTR; 8440 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8441 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8442 8443 /* 7: Lookup post-rename fh of renamed object */ 8444 argop[7].argop = OP_CLOOKUP; 8445 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8446 8447 /* 8: getfh fh of renamed object (after rename) */ 8448 argop[8].argop = OP_GETFH; 8449 8450 /* 9: getattr of renamed object */ 8451 argop[9].argop = OP_GETATTR; 8452 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8453 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8454 8455 /* 8456 * If source/target dirs are different, then get new post-op 8457 * attrs for source dir also. 8458 */ 8459 if (ndvp != odvp) { 8460 /* 10: putfh (sourcedir) */ 8461 argop[10].argop = OP_CPUTFH; 8462 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8463 8464 /* 11: getattr (sourcedir) */ 8465 argop[11].argop = OP_GETATTR; 8466 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8467 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8468 } 8469 8470 dnlc_remove(odvp, onm); 8471 dnlc_remove(ndvp, nnm); 8472 8473 doqueue = 1; 8474 t = gethrtime(); 8475 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8476 8477 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8478 if (e.error) { 8479 PURGE_ATTRCACHE4(odvp); 8480 PURGE_ATTRCACHE4(ndvp); 8481 if (!needrecov) { 8482 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8483 &recov_state, needrecov); 8484 goto out; 8485 } 8486 } else { 8487 *statp = res.status; 8488 } 8489 8490 if (needrecov) { 8491 bool_t abort; 8492 8493 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8494 OP_RENAME, NULL, NULL, NULL); 8495 if (abort == FALSE) { 8496 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8497 &recov_state, needrecov); 8498 kmem_free(argop, argoplist_size); 8499 if (!e.error) 8500 (void) xdr_free(xdr_COMPOUND4res_clnt, 8501 (caddr_t)&res); 8502 mutex_enter(&orp->r_statelock); 8503 orp->r_flags &= ~R4RECEXPFH; 8504 cv_broadcast(&orp->r_cv); 8505 mutex_exit(&orp->r_statelock); 8506 goto recov_retry; 8507 } else { 8508 if (e.error != 0) { 8509 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8510 &recov_state, needrecov); 8511 goto out; 8512 } 8513 /* fall through for res.status case */ 8514 } 8515 } 8516 8517 resp = &res; 8518 /* 8519 * If OP_RENAME (or any prev op) failed, then return an error. 8520 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8521 */ 8522 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8523 /* 8524 * Error in an op other than last Getattr 8525 */ 8526 e.error = geterrno4(res.status); 8527 PURGE_ATTRCACHE4(odvp); 8528 PURGE_ATTRCACHE4(ndvp); 8529 /* 8530 * System V defines rename to return EEXIST, not 8531 * ENOTEMPTY if the target directory is not empty. 8532 * Over the wire, the error is NFSERR_ENOTEMPTY 8533 * which geterrno4 maps to ENOTEMPTY. 8534 */ 8535 if (e.error == ENOTEMPTY) 8536 e.error = EEXIST; 8537 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8538 needrecov); 8539 goto out; 8540 } 8541 8542 /* rename results */ 8543 rn_res = &res.array[5].nfs_resop4_u.oprename; 8544 8545 if (res.status == NFS4_OK) { 8546 /* Update target attribute, readdir and dnlc caches */ 8547 dinfo.di_garp = 8548 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8549 dinfo.di_cred = cr; 8550 dinfo.di_time_call = t; 8551 } else 8552 dinfop = NULL; 8553 8554 /* Update source cache attribute, readdir and dnlc caches */ 8555 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8556 8557 /* Update source cache attribute, readdir and dnlc caches */ 8558 if (ndvp != odvp) { 8559 update_parentdir_sfh(ovp, ndvp); 8560 8561 /* 8562 * If dinfop is non-NULL, then compound succeded, so 8563 * set di_garp to attrs for source dir. dinfop is only 8564 * set to NULL when compound fails. 8565 */ 8566 if (dinfop) 8567 dinfo.di_garp = 8568 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8569 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8570 dinfop); 8571 } 8572 8573 /* 8574 * Update the rnode with the new component name and args, 8575 * and if the file handle changed, also update it with the new fh. 8576 * This is only necessary if the target object has an rnode 8577 * entry and there is no need to create one for it. 8578 */ 8579 resop = &res.array[8]; /* getfh new res */ 8580 ngf_res = &resop->nfs_resop4_u.opgetfh; 8581 8582 /* 8583 * Update the path and filehandle for the renamed object. 8584 */ 8585 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8586 8587 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8588 8589 if (res.status == NFS4_OK) { 8590 resop++; /* getattr res */ 8591 e.error = nfs4_update_attrcache(res.status, 8592 &resop->nfs_resop4_u.opgetattr.ga_res, 8593 t, ovp, cr); 8594 } 8595 8596 out: 8597 kmem_free(argop, argoplist_size); 8598 if (resp) 8599 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8600 mutex_enter(&orp->r_statelock); 8601 orp->r_flags &= ~R4RECEXPFH; 8602 cv_broadcast(&orp->r_cv); 8603 mutex_exit(&orp->r_statelock); 8604 8605 return (e.error); 8606 } 8607 8608 /* ARGSUSED */ 8609 static int 8610 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr, 8611 caller_context_t *ct, int flags, vsecattr_t *vsecp) 8612 { 8613 int error; 8614 vnode_t *vp; 8615 8616 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8617 return (EPERM); 8618 /* 8619 * As ".." has special meaning and rather than send a mkdir 8620 * over the wire to just let the server freak out, we just 8621 * short circuit it here and return EEXIST 8622 */ 8623 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8624 return (EEXIST); 8625 8626 /* 8627 * Decision to get the right gid and setgid bit of the 8628 * new directory is now made in call_nfs4_create_req. 8629 */ 8630 va->va_mask |= AT_MODE; 8631 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8632 if (error) 8633 return (error); 8634 8635 *vpp = vp; 8636 return (0); 8637 } 8638 8639 8640 /* 8641 * rmdir is using the same remove v4 op as does remove. 8642 * Remove requires that the current fh be the target directory. 8643 * After the operation, the current fh is unchanged. 8644 * The compound op structure is: 8645 * PUTFH(targetdir), REMOVE 8646 */ 8647 /*ARGSUSED4*/ 8648 static int 8649 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr, 8650 caller_context_t *ct, int flags) 8651 { 8652 int need_end_op = FALSE; 8653 COMPOUND4args_clnt args; 8654 COMPOUND4res_clnt res, *resp = NULL; 8655 REMOVE4res *rm_res; 8656 nfs_argop4 argop[3]; 8657 nfs_resop4 *resop; 8658 vnode_t *vp; 8659 int doqueue; 8660 mntinfo4_t *mi; 8661 rnode4_t *drp; 8662 bool_t needrecov = FALSE; 8663 nfs4_recov_state_t recov_state; 8664 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8665 dirattr_info_t dinfo, *dinfop; 8666 8667 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8668 return (EPERM); 8669 /* 8670 * As ".." has special meaning and rather than send a rmdir 8671 * over the wire to just let the server freak out, we just 8672 * short circuit it here and return EEXIST 8673 */ 8674 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8675 return (EEXIST); 8676 8677 drp = VTOR4(dvp); 8678 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8679 return (EINTR); 8680 8681 /* 8682 * Attempt to prevent a rmdir(".") from succeeding. 8683 */ 8684 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8685 if (e.error) { 8686 nfs_rw_exit(&drp->r_rwlock); 8687 return (e.error); 8688 } 8689 if (vp == cdir) { 8690 VN_RELE(vp); 8691 nfs_rw_exit(&drp->r_rwlock); 8692 return (EINVAL); 8693 } 8694 8695 /* 8696 * Since nfsv4 remove op works on both files and directories, 8697 * check that the removed object is indeed a directory. 8698 */ 8699 if (vp->v_type != VDIR) { 8700 VN_RELE(vp); 8701 nfs_rw_exit(&drp->r_rwlock); 8702 return (ENOTDIR); 8703 } 8704 8705 /* 8706 * First just remove the entry from the name cache, as it 8707 * is most likely an entry for this vp. 8708 */ 8709 dnlc_remove(dvp, nm); 8710 8711 /* 8712 * If there vnode reference count is greater than one, then 8713 * there may be additional references in the DNLC which will 8714 * need to be purged. First, trying removing the entry for 8715 * the parent directory and see if that removes the additional 8716 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8717 * to completely remove any references to the directory which 8718 * might still exist in the DNLC. 8719 */ 8720 if (vp->v_count > 1) { 8721 dnlc_remove(vp, ".."); 8722 if (vp->v_count > 1) 8723 dnlc_purge_vp(vp); 8724 } 8725 8726 mi = VTOMI4(dvp); 8727 recov_state.rs_flags = 0; 8728 recov_state.rs_num_retry_despite_err = 0; 8729 8730 recov_retry: 8731 args.ctag = TAG_RMDIR; 8732 8733 /* 8734 * Rmdir ops: putfh dir; remove 8735 */ 8736 args.array_len = 3; 8737 args.array = argop; 8738 8739 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8740 if (e.error) { 8741 nfs_rw_exit(&drp->r_rwlock); 8742 return (e.error); 8743 } 8744 need_end_op = TRUE; 8745 8746 /* putfh directory */ 8747 argop[0].argop = OP_CPUTFH; 8748 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8749 8750 /* remove */ 8751 argop[1].argop = OP_CREMOVE; 8752 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8753 8754 /* getattr (postop attrs for dir that contained removed dir) */ 8755 argop[2].argop = OP_GETATTR; 8756 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8757 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8758 8759 dinfo.di_time_call = gethrtime(); 8760 doqueue = 1; 8761 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8762 8763 PURGE_ATTRCACHE4(vp); 8764 8765 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8766 if (e.error) { 8767 PURGE_ATTRCACHE4(dvp); 8768 } 8769 8770 if (needrecov) { 8771 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8772 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 8773 if (!e.error) 8774 (void) xdr_free(xdr_COMPOUND4res_clnt, 8775 (caddr_t)&res); 8776 8777 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8778 needrecov); 8779 need_end_op = FALSE; 8780 goto recov_retry; 8781 } 8782 } 8783 8784 if (!e.error) { 8785 resp = &res; 8786 8787 /* 8788 * Only return error if first 2 ops (OP_REMOVE or earlier) 8789 * failed. 8790 */ 8791 if (res.status != NFS4_OK && res.array_len <= 2) { 8792 e.error = geterrno4(res.status); 8793 PURGE_ATTRCACHE4(dvp); 8794 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8795 &recov_state, needrecov); 8796 need_end_op = FALSE; 8797 nfs4_purge_stale_fh(e.error, dvp, cr); 8798 /* 8799 * System V defines rmdir to return EEXIST, not 8800 * ENOTEMPTY if the directory is not empty. Over 8801 * the wire, the error is NFSERR_ENOTEMPTY which 8802 * geterrno4 maps to ENOTEMPTY. 8803 */ 8804 if (e.error == ENOTEMPTY) 8805 e.error = EEXIST; 8806 } else { 8807 resop = &res.array[1]; /* remove res */ 8808 rm_res = &resop->nfs_resop4_u.opremove; 8809 8810 if (res.status == NFS4_OK) { 8811 resop = &res.array[2]; /* dir attrs */ 8812 dinfo.di_garp = 8813 &resop->nfs_resop4_u.opgetattr.ga_res; 8814 dinfo.di_cred = cr; 8815 dinfop = &dinfo; 8816 } else 8817 dinfop = NULL; 8818 8819 /* Update dir attribute, readdir and dnlc caches */ 8820 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8821 dinfop); 8822 8823 /* destroy rddir cache for dir that was removed */ 8824 if (VTOR4(vp)->r_dir != NULL) 8825 nfs4_purge_rddir_cache(vp); 8826 } 8827 } 8828 8829 if (need_end_op) 8830 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8831 8832 nfs_rw_exit(&drp->r_rwlock); 8833 8834 if (resp) 8835 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8836 8837 if (e.error == 0) { 8838 vnode_t *tvp; 8839 rnode4_t *trp; 8840 trp = VTOR4(vp); 8841 tvp = vp; 8842 if (IS_SHADOW(vp, trp)) 8843 tvp = RTOV4(trp); 8844 vnevent_rmdir(tvp, dvp, nm, ct); 8845 } 8846 8847 VN_RELE(vp); 8848 8849 return (e.error); 8850 } 8851 8852 /* ARGSUSED */ 8853 static int 8854 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr, 8855 caller_context_t *ct, int flags) 8856 { 8857 int error; 8858 vnode_t *vp; 8859 rnode4_t *rp; 8860 char *contents; 8861 mntinfo4_t *mi = VTOMI4(dvp); 8862 8863 if (nfs_zone() != mi->mi_zone) 8864 return (EPERM); 8865 if (!(mi->mi_flags & MI4_SYMLINK)) 8866 return (EOPNOTSUPP); 8867 8868 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8869 if (error) 8870 return (error); 8871 8872 ASSERT(nfs4_consistent_type(vp)); 8873 rp = VTOR4(vp); 8874 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8875 8876 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8877 8878 if (contents != NULL) { 8879 mutex_enter(&rp->r_statelock); 8880 if (rp->r_symlink.contents == NULL) { 8881 rp->r_symlink.len = strlen(tnm); 8882 bcopy(tnm, contents, rp->r_symlink.len); 8883 rp->r_symlink.contents = contents; 8884 rp->r_symlink.size = MAXPATHLEN; 8885 mutex_exit(&rp->r_statelock); 8886 } else { 8887 mutex_exit(&rp->r_statelock); 8888 kmem_free((void *)contents, MAXPATHLEN); 8889 } 8890 } 8891 } 8892 VN_RELE(vp); 8893 8894 return (error); 8895 } 8896 8897 8898 /* 8899 * Read directory entries. 8900 * There are some weird things to look out for here. The uio_loffset 8901 * field is either 0 or it is the offset returned from a previous 8902 * readdir. It is an opaque value used by the server to find the 8903 * correct directory block to read. The count field is the number 8904 * of blocks to read on the server. This is advisory only, the server 8905 * may return only one block's worth of entries. Entries may be compressed 8906 * on the server. 8907 */ 8908 /* ARGSUSED */ 8909 static int 8910 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp, 8911 caller_context_t *ct, int flags) 8912 { 8913 int error; 8914 uint_t count; 8915 rnode4_t *rp; 8916 rddir4_cache *rdc; 8917 rddir4_cache *rrdc; 8918 8919 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8920 return (EIO); 8921 rp = VTOR4(vp); 8922 8923 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8924 8925 /* 8926 * Make sure that the directory cache is valid. 8927 */ 8928 if (rp->r_dir != NULL) { 8929 if (nfs_disable_rddir_cache != 0) { 8930 /* 8931 * Setting nfs_disable_rddir_cache in /etc/system 8932 * allows interoperability with servers that do not 8933 * properly update the attributes of directories. 8934 * Any cached information gets purged before an 8935 * access is made to it. 8936 */ 8937 nfs4_purge_rddir_cache(vp); 8938 } 8939 8940 error = nfs4_validate_caches(vp, cr); 8941 if (error) 8942 return (error); 8943 } 8944 8945 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8946 8947 /* 8948 * Short circuit last readdir which always returns 0 bytes. 8949 * This can be done after the directory has been read through 8950 * completely at least once. This will set r_direof which 8951 * can be used to find the value of the last cookie. 8952 */ 8953 mutex_enter(&rp->r_statelock); 8954 if (rp->r_direof != NULL && 8955 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8956 mutex_exit(&rp->r_statelock); 8957 #ifdef DEBUG 8958 nfs4_readdir_cache_shorts++; 8959 #endif 8960 if (eofp) 8961 *eofp = 1; 8962 return (0); 8963 } 8964 8965 /* 8966 * Look for a cache entry. Cache entries are identified 8967 * by the NFS cookie value and the byte count requested. 8968 */ 8969 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8970 8971 /* 8972 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8973 */ 8974 if (rdc == NULL) { 8975 mutex_exit(&rp->r_statelock); 8976 return (EINTR); 8977 } 8978 8979 /* 8980 * Check to see if we need to fill this entry in. 8981 */ 8982 if (rdc->flags & RDDIRREQ) { 8983 rdc->flags &= ~RDDIRREQ; 8984 rdc->flags |= RDDIR; 8985 mutex_exit(&rp->r_statelock); 8986 8987 /* 8988 * Do the readdir. 8989 */ 8990 nfs4readdir(vp, rdc, cr); 8991 8992 /* 8993 * Reacquire the lock, so that we can continue 8994 */ 8995 mutex_enter(&rp->r_statelock); 8996 /* 8997 * The entry is now complete 8998 */ 8999 rdc->flags &= ~RDDIR; 9000 } 9001 9002 ASSERT(!(rdc->flags & RDDIR)); 9003 9004 /* 9005 * If an error occurred while attempting 9006 * to fill the cache entry, mark the entry invalid and 9007 * just return the error. 9008 */ 9009 if (rdc->error) { 9010 error = rdc->error; 9011 rdc->flags |= RDDIRREQ; 9012 rddir4_cache_rele(rp, rdc); 9013 mutex_exit(&rp->r_statelock); 9014 return (error); 9015 } 9016 9017 /* 9018 * The cache entry is complete and good, 9019 * copyout the dirent structs to the calling 9020 * thread. 9021 */ 9022 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 9023 9024 /* 9025 * If no error occurred during the copyout, 9026 * update the offset in the uio struct to 9027 * contain the value of the next NFS 4 cookie 9028 * and set the eof value appropriately. 9029 */ 9030 if (!error) { 9031 uiop->uio_loffset = rdc->nfs4_ncookie; 9032 if (eofp) 9033 *eofp = rdc->eof; 9034 } 9035 9036 /* 9037 * Decide whether to do readahead. Don't if we 9038 * have already read to the end of directory. 9039 */ 9040 if (rdc->eof) { 9041 /* 9042 * Make the entry the direof only if it is cached 9043 */ 9044 if (rdc->flags & RDDIRCACHED) 9045 rp->r_direof = rdc; 9046 rddir4_cache_rele(rp, rdc); 9047 mutex_exit(&rp->r_statelock); 9048 return (error); 9049 } 9050 9051 /* Determine if a readdir readahead should be done */ 9052 if (!(rp->r_flags & R4LOOKUP)) { 9053 rddir4_cache_rele(rp, rdc); 9054 mutex_exit(&rp->r_statelock); 9055 return (error); 9056 } 9057 9058 /* 9059 * Now look for a readahead entry. 9060 * 9061 * Check to see whether we found an entry for the readahead. 9062 * If so, we don't need to do anything further, so free the new 9063 * entry if one was allocated. Otherwise, allocate a new entry, add 9064 * it to the cache, and then initiate an asynchronous readdir 9065 * operation to fill it. 9066 */ 9067 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 9068 9069 /* 9070 * A readdir cache entry could not be obtained for the readahead. In 9071 * this case we skip the readahead and return. 9072 */ 9073 if (rrdc == NULL) { 9074 rddir4_cache_rele(rp, rdc); 9075 mutex_exit(&rp->r_statelock); 9076 return (error); 9077 } 9078 9079 /* 9080 * Check to see if we need to fill this entry in. 9081 */ 9082 if (rrdc->flags & RDDIRREQ) { 9083 rrdc->flags &= ~RDDIRREQ; 9084 rrdc->flags |= RDDIR; 9085 rddir4_cache_rele(rp, rdc); 9086 mutex_exit(&rp->r_statelock); 9087 #ifdef DEBUG 9088 nfs4_readdir_readahead++; 9089 #endif 9090 /* 9091 * Do the readdir. 9092 */ 9093 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 9094 return (error); 9095 } 9096 9097 rddir4_cache_rele(rp, rrdc); 9098 rddir4_cache_rele(rp, rdc); 9099 mutex_exit(&rp->r_statelock); 9100 return (error); 9101 } 9102 9103 static int 9104 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9105 { 9106 int error; 9107 rnode4_t *rp; 9108 9109 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9110 9111 rp = VTOR4(vp); 9112 9113 /* 9114 * Obtain the readdir results for the caller. 9115 */ 9116 nfs4readdir(vp, rdc, cr); 9117 9118 mutex_enter(&rp->r_statelock); 9119 /* 9120 * The entry is now complete 9121 */ 9122 rdc->flags &= ~RDDIR; 9123 9124 error = rdc->error; 9125 if (error) 9126 rdc->flags |= RDDIRREQ; 9127 rddir4_cache_rele(rp, rdc); 9128 mutex_exit(&rp->r_statelock); 9129 9130 return (error); 9131 } 9132 9133 /* 9134 * Read directory entries. 9135 * There are some weird things to look out for here. The uio_loffset 9136 * field is either 0 or it is the offset returned from a previous 9137 * readdir. It is an opaque value used by the server to find the 9138 * correct directory block to read. The count field is the number 9139 * of blocks to read on the server. This is advisory only, the server 9140 * may return only one block's worth of entries. Entries may be compressed 9141 * on the server. 9142 * 9143 * Generates the following compound request: 9144 * 1. If readdir offset is zero and no dnlc entry for parent exists, 9145 * must include a Lookupp as well. In this case, send: 9146 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 9147 * 2. Otherwise just do: { Putfh <fh>; Readdir } 9148 * 9149 * Get complete attributes and filehandles for entries if this is the 9150 * first read of the directory. Otherwise, just get fileid's. 9151 */ 9152 static void 9153 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9154 { 9155 COMPOUND4args_clnt args; 9156 COMPOUND4res_clnt res; 9157 READDIR4args *rargs; 9158 READDIR4res_clnt *rd_res; 9159 bitmap4 rd_bitsval; 9160 nfs_argop4 argop[5]; 9161 nfs_resop4 *resop; 9162 rnode4_t *rp = VTOR4(vp); 9163 mntinfo4_t *mi = VTOMI4(vp); 9164 int doqueue; 9165 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 9166 vnode_t *dvp; 9167 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 9168 int num_ops, res_opcnt; 9169 bool_t needrecov = FALSE; 9170 nfs4_recov_state_t recov_state; 9171 hrtime_t t; 9172 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 9173 9174 ASSERT(nfs_zone() == mi->mi_zone); 9175 ASSERT(rdc->flags & RDDIR); 9176 ASSERT(rdc->entries == NULL); 9177 9178 /* 9179 * If rp were a stub, it should have triggered and caused 9180 * a mount for us to get this far. 9181 */ 9182 ASSERT(!RP_ISSTUB(rp)); 9183 9184 num_ops = 2; 9185 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9186 /* 9187 * Since nfsv4 readdir may not return entries for "." and "..", 9188 * the client must recreate them: 9189 * To find the correct nodeid, do the following: 9190 * For current node, get nodeid from dnlc. 9191 * - if current node is rootvp, set pnodeid to nodeid. 9192 * - else if parent is in the dnlc, get its nodeid from there. 9193 * - else add LOOKUPP+GETATTR to compound. 9194 */ 9195 nodeid = rp->r_attr.va_nodeid; 9196 if (vp->v_flag & VROOT) { 9197 pnodeid = nodeid; /* root of mount point */ 9198 } else { 9199 dvp = dnlc_lookup(vp, ".."); 9200 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9201 /* parent in dnlc cache - no need for otw */ 9202 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9203 } else { 9204 /* 9205 * parent not in dnlc cache, 9206 * do lookupp to get its id 9207 */ 9208 num_ops = 5; 9209 pnodeid = 0; /* set later by getattr parent */ 9210 } 9211 if (dvp) 9212 VN_RELE(dvp); 9213 } 9214 } 9215 recov_state.rs_flags = 0; 9216 recov_state.rs_num_retry_despite_err = 0; 9217 9218 /* Save the original mount point security flavor */ 9219 (void) save_mnt_secinfo(mi->mi_curr_serv); 9220 9221 recov_retry: 9222 args.ctag = TAG_READDIR; 9223 9224 args.array = argop; 9225 args.array_len = num_ops; 9226 9227 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9228 &recov_state, NULL)) { 9229 /* 9230 * If readdir a node that is a stub for a crossed mount point, 9231 * keep the original secinfo flavor for the current file 9232 * system, not the crossed one. 9233 */ 9234 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9235 rdc->error = e.error; 9236 return; 9237 } 9238 9239 /* 9240 * Determine which attrs to request for dirents. This code 9241 * must be protected by nfs4_start/end_fop because of r_server 9242 * (which will change during failover recovery). 9243 * 9244 */ 9245 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9246 /* 9247 * Get all vattr attrs plus filehandle and rdattr_error 9248 */ 9249 rd_bitsval = NFS4_VATTR_MASK | 9250 FATTR4_RDATTR_ERROR_MASK | 9251 FATTR4_FILEHANDLE_MASK; 9252 9253 if (rp->r_flags & R4READDIRWATTR) { 9254 mutex_enter(&rp->r_statelock); 9255 rp->r_flags &= ~R4READDIRWATTR; 9256 mutex_exit(&rp->r_statelock); 9257 } 9258 } else { 9259 servinfo4_t *svp = rp->r_server; 9260 9261 /* 9262 * Already read directory. Use readdir with 9263 * no attrs (except for mounted_on_fileid) for updates. 9264 */ 9265 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9266 9267 /* 9268 * request mounted on fileid if supported, else request 9269 * fileid. maybe we should verify that fileid is supported 9270 * and request something else if not. 9271 */ 9272 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9273 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9274 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9275 nfs_rw_exit(&svp->sv_lock); 9276 } 9277 9278 /* putfh directory fh */ 9279 argop[0].argop = OP_CPUTFH; 9280 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9281 9282 argop[1].argop = OP_READDIR; 9283 rargs = &argop[1].nfs_argop4_u.opreaddir; 9284 /* 9285 * 1 and 2 are reserved for client "." and ".." entry offset. 9286 * cookie 0 should be used over-the-wire to start reading at 9287 * the beginning of the directory excluding "." and "..". 9288 */ 9289 if (rdc->nfs4_cookie == 0 || 9290 rdc->nfs4_cookie == 1 || 9291 rdc->nfs4_cookie == 2) { 9292 rargs->cookie = (nfs_cookie4)0; 9293 rargs->cookieverf = 0; 9294 } else { 9295 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9296 mutex_enter(&rp->r_statelock); 9297 rargs->cookieverf = rp->r_cookieverf4; 9298 mutex_exit(&rp->r_statelock); 9299 } 9300 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9301 rargs->maxcount = mi->mi_tsize; 9302 rargs->attr_request = rd_bitsval; 9303 rargs->rdc = rdc; 9304 rargs->dvp = vp; 9305 rargs->mi = mi; 9306 rargs->cr = cr; 9307 9308 9309 /* 9310 * If count < than the minimum required, we return no entries 9311 * and fail with EINVAL 9312 */ 9313 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9314 rdc->error = EINVAL; 9315 goto out; 9316 } 9317 9318 if (args.array_len == 5) { 9319 /* 9320 * Add lookupp and getattr for parent nodeid. 9321 */ 9322 argop[2].argop = OP_LOOKUPP; 9323 9324 argop[3].argop = OP_GETFH; 9325 9326 /* getattr parent */ 9327 argop[4].argop = OP_GETATTR; 9328 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9329 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9330 } 9331 9332 doqueue = 1; 9333 9334 if (mi->mi_io_kstats) { 9335 mutex_enter(&mi->mi_lock); 9336 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9337 mutex_exit(&mi->mi_lock); 9338 } 9339 9340 /* capture the time of this call */ 9341 rargs->t = t = gethrtime(); 9342 9343 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9344 9345 if (mi->mi_io_kstats) { 9346 mutex_enter(&mi->mi_lock); 9347 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9348 mutex_exit(&mi->mi_lock); 9349 } 9350 9351 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9352 9353 /* 9354 * If RPC error occurred and it isn't an error that 9355 * triggers recovery, then go ahead and fail now. 9356 */ 9357 if (e.error != 0 && !needrecov) { 9358 rdc->error = e.error; 9359 goto out; 9360 } 9361 9362 if (needrecov) { 9363 bool_t abort; 9364 9365 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9366 "nfs4readdir: initiating recovery.\n")); 9367 9368 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9369 NULL, OP_READDIR, NULL, NULL, NULL); 9370 if (abort == FALSE) { 9371 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9372 &recov_state, needrecov); 9373 if (!e.error) 9374 (void) xdr_free(xdr_COMPOUND4res_clnt, 9375 (caddr_t)&res); 9376 if (rdc->entries != NULL) { 9377 kmem_free(rdc->entries, rdc->entlen); 9378 rdc->entries = NULL; 9379 } 9380 goto recov_retry; 9381 } 9382 9383 if (e.error != 0) { 9384 rdc->error = e.error; 9385 goto out; 9386 } 9387 9388 /* fall through for res.status case */ 9389 } 9390 9391 res_opcnt = res.array_len; 9392 9393 /* 9394 * If compound failed first 2 ops (PUTFH+READDIR), then return 9395 * failure here. Subsequent ops are for filling out dot-dot 9396 * dirent, and if they fail, we still want to give the caller 9397 * the dirents returned by (the successful) READDIR op, so we need 9398 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9399 * 9400 * One example where PUTFH+READDIR ops would succeed but 9401 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9402 * but lacks x. In this case, a POSIX server's VOP_READDIR 9403 * would succeed; however, VOP_LOOKUP(..) would fail since no 9404 * x perm. We need to come up with a non-vendor-specific way 9405 * for a POSIX server to return d_ino from dotdot's dirent if 9406 * client only requests mounted_on_fileid, and just say the 9407 * LOOKUPP succeeded and fill out the GETATTR. However, if 9408 * client requested any mandatory attrs, server would be required 9409 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9410 * for dotdot. 9411 */ 9412 9413 if (res.status) { 9414 if (res_opcnt <= 2) { 9415 e.error = geterrno4(res.status); 9416 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9417 &recov_state, needrecov); 9418 nfs4_purge_stale_fh(e.error, vp, cr); 9419 rdc->error = e.error; 9420 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9421 if (rdc->entries != NULL) { 9422 kmem_free(rdc->entries, rdc->entlen); 9423 rdc->entries = NULL; 9424 } 9425 /* 9426 * If readdir a node that is a stub for a 9427 * crossed mount point, keep the original 9428 * secinfo flavor for the current file system, 9429 * not the crossed one. 9430 */ 9431 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9432 return; 9433 } 9434 } 9435 9436 resop = &res.array[1]; /* readdir res */ 9437 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9438 9439 mutex_enter(&rp->r_statelock); 9440 rp->r_cookieverf4 = rd_res->cookieverf; 9441 mutex_exit(&rp->r_statelock); 9442 9443 /* 9444 * For "." and ".." entries 9445 * e.g. 9446 * seek(cookie=0) -> "." entry with d_off = 1 9447 * seek(cookie=1) -> ".." entry with d_off = 2 9448 */ 9449 if (cookie == (nfs_cookie4) 0) { 9450 if (rd_res->dotp) 9451 rd_res->dotp->d_ino = nodeid; 9452 if (rd_res->dotdotp) 9453 rd_res->dotdotp->d_ino = pnodeid; 9454 } 9455 if (cookie == (nfs_cookie4) 1) { 9456 if (rd_res->dotdotp) 9457 rd_res->dotdotp->d_ino = pnodeid; 9458 } 9459 9460 9461 /* LOOKUPP+GETATTR attemped */ 9462 if (args.array_len == 5 && rd_res->dotdotp) { 9463 if (res.status == NFS4_OK && res_opcnt == 5) { 9464 nfs_fh4 *fhp; 9465 nfs4_sharedfh_t *sfhp; 9466 vnode_t *pvp; 9467 nfs4_ga_res_t *garp; 9468 9469 resop++; /* lookupp */ 9470 resop++; /* getfh */ 9471 fhp = &resop->nfs_resop4_u.opgetfh.object; 9472 9473 resop++; /* getattr of parent */ 9474 9475 /* 9476 * First, take care of finishing the 9477 * readdir results. 9478 */ 9479 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9480 /* 9481 * The d_ino of .. must be the inode number 9482 * of the mounted filesystem. 9483 */ 9484 if (garp->n4g_va.va_mask & AT_NODEID) 9485 rd_res->dotdotp->d_ino = 9486 garp->n4g_va.va_nodeid; 9487 9488 9489 /* 9490 * Next, create the ".." dnlc entry 9491 */ 9492 sfhp = sfh4_get(fhp, mi); 9493 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9494 dnlc_update(vp, "..", pvp); 9495 VN_RELE(pvp); 9496 } 9497 sfh4_rele(&sfhp); 9498 } 9499 } 9500 9501 if (mi->mi_io_kstats) { 9502 mutex_enter(&mi->mi_lock); 9503 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9504 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9505 mutex_exit(&mi->mi_lock); 9506 } 9507 9508 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9509 9510 out: 9511 /* 9512 * If readdir a node that is a stub for a crossed mount point, 9513 * keep the original secinfo flavor for the current file system, 9514 * not the crossed one. 9515 */ 9516 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9517 9518 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9519 } 9520 9521 9522 static int 9523 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9524 { 9525 rnode4_t *rp = VTOR4(bp->b_vp); 9526 int count; 9527 int error; 9528 cred_t *cred_otw = NULL; 9529 offset_t offset; 9530 nfs4_open_stream_t *osp = NULL; 9531 bool_t first_time = TRUE; /* first time getting otw cred */ 9532 bool_t last_time = FALSE; /* last time getting otw cred */ 9533 9534 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9535 9536 DTRACE_IO1(start, struct buf *, bp); 9537 offset = ldbtob(bp->b_lblkno); 9538 9539 if (bp->b_flags & B_READ) { 9540 read_again: 9541 /* 9542 * Releases the osp, if it is provided. 9543 * Puts a hold on the cred_otw and the new osp (if found). 9544 */ 9545 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9546 &first_time, &last_time); 9547 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9548 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9549 readahead, NULL); 9550 crfree(cred_otw); 9551 if (!error) { 9552 if (bp->b_resid) { 9553 /* 9554 * Didn't get it all because we hit EOF, 9555 * zero all the memory beyond the EOF. 9556 */ 9557 /* bzero(rdaddr + */ 9558 bzero(bp->b_un.b_addr + 9559 bp->b_bcount - bp->b_resid, bp->b_resid); 9560 } 9561 mutex_enter(&rp->r_statelock); 9562 if (bp->b_resid == bp->b_bcount && 9563 offset >= rp->r_size) { 9564 /* 9565 * We didn't read anything at all as we are 9566 * past EOF. Return an error indicator back 9567 * but don't destroy the pages (yet). 9568 */ 9569 error = NFS_EOF; 9570 } 9571 mutex_exit(&rp->r_statelock); 9572 } else if (error == EACCES && last_time == FALSE) { 9573 goto read_again; 9574 } 9575 } else { 9576 if (!(rp->r_flags & R4STALE)) { 9577 write_again: 9578 /* 9579 * Releases the osp, if it is provided. 9580 * Puts a hold on the cred_otw and the new 9581 * osp (if found). 9582 */ 9583 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9584 &first_time, &last_time); 9585 mutex_enter(&rp->r_statelock); 9586 count = MIN(bp->b_bcount, rp->r_size - offset); 9587 mutex_exit(&rp->r_statelock); 9588 if (count < 0) 9589 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9590 #ifdef DEBUG 9591 if (count == 0) { 9592 zoneid_t zoneid = getzoneid(); 9593 9594 zcmn_err(zoneid, CE_WARN, 9595 "nfs4_bio: zero length write at %lld", 9596 offset); 9597 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9598 "b_bcount=%ld, file size=%lld", 9599 rp->r_flags, (long)bp->b_bcount, 9600 rp->r_size); 9601 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9602 if (nfs4_bio_do_stop) 9603 debug_enter("nfs4_bio"); 9604 } 9605 #endif 9606 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9607 count, cred_otw, stab_comm); 9608 if (error == EACCES && last_time == FALSE) { 9609 crfree(cred_otw); 9610 goto write_again; 9611 } 9612 bp->b_error = error; 9613 if (error && error != EINTR && 9614 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9615 /* 9616 * Don't print EDQUOT errors on the console. 9617 * Don't print asynchronous EACCES errors. 9618 * Don't print EFBIG errors. 9619 * Print all other write errors. 9620 */ 9621 if (error != EDQUOT && error != EFBIG && 9622 (error != EACCES || 9623 !(bp->b_flags & B_ASYNC))) 9624 nfs4_write_error(bp->b_vp, 9625 error, cred_otw); 9626 /* 9627 * Update r_error and r_flags as appropriate. 9628 * If the error was ESTALE, then mark the 9629 * rnode as not being writeable and save 9630 * the error status. Otherwise, save any 9631 * errors which occur from asynchronous 9632 * page invalidations. Any errors occurring 9633 * from other operations should be saved 9634 * by the caller. 9635 */ 9636 mutex_enter(&rp->r_statelock); 9637 if (error == ESTALE) { 9638 rp->r_flags |= R4STALE; 9639 if (!rp->r_error) 9640 rp->r_error = error; 9641 } else if (!rp->r_error && 9642 (bp->b_flags & 9643 (B_INVAL|B_FORCE|B_ASYNC)) == 9644 (B_INVAL|B_FORCE|B_ASYNC)) { 9645 rp->r_error = error; 9646 } 9647 mutex_exit(&rp->r_statelock); 9648 } 9649 crfree(cred_otw); 9650 } else { 9651 error = rp->r_error; 9652 /* 9653 * A close may have cleared r_error, if so, 9654 * propagate ESTALE error return properly 9655 */ 9656 if (error == 0) 9657 error = ESTALE; 9658 } 9659 } 9660 9661 if (error != 0 && error != NFS_EOF) 9662 bp->b_flags |= B_ERROR; 9663 9664 if (osp) 9665 open_stream_rele(osp, rp); 9666 9667 DTRACE_IO1(done, struct buf *, bp); 9668 9669 return (error); 9670 } 9671 9672 /* ARGSUSED */ 9673 int 9674 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 9675 { 9676 return (EREMOTE); 9677 } 9678 9679 /* ARGSUSED2 */ 9680 int 9681 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9682 { 9683 rnode4_t *rp = VTOR4(vp); 9684 9685 if (!write_lock) { 9686 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9687 return (V_WRITELOCK_FALSE); 9688 } 9689 9690 if ((rp->r_flags & R4DIRECTIO) || 9691 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9692 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9693 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9694 return (V_WRITELOCK_FALSE); 9695 nfs_rw_exit(&rp->r_rwlock); 9696 } 9697 9698 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9699 return (V_WRITELOCK_TRUE); 9700 } 9701 9702 /* ARGSUSED */ 9703 void 9704 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9705 { 9706 rnode4_t *rp = VTOR4(vp); 9707 9708 nfs_rw_exit(&rp->r_rwlock); 9709 } 9710 9711 /* ARGSUSED */ 9712 static int 9713 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 9714 { 9715 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9716 return (EIO); 9717 9718 /* 9719 * Because we stuff the readdir cookie into the offset field 9720 * someone may attempt to do an lseek with the cookie which 9721 * we want to succeed. 9722 */ 9723 if (vp->v_type == VDIR) 9724 return (0); 9725 if (*noffp < 0) 9726 return (EINVAL); 9727 return (0); 9728 } 9729 9730 9731 /* 9732 * Return all the pages from [off..off+len) in file 9733 */ 9734 /* ARGSUSED */ 9735 static int 9736 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9737 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9738 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 9739 { 9740 rnode4_t *rp; 9741 int error; 9742 mntinfo4_t *mi; 9743 9744 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9745 return (EIO); 9746 rp = VTOR4(vp); 9747 if (IS_SHADOW(vp, rp)) 9748 vp = RTOV4(rp); 9749 9750 if (vp->v_flag & VNOMAP) 9751 return (ENOSYS); 9752 9753 if (protp != NULL) 9754 *protp = PROT_ALL; 9755 9756 /* 9757 * Now validate that the caches are up to date. 9758 */ 9759 if (error = nfs4_validate_caches(vp, cr)) 9760 return (error); 9761 9762 mi = VTOMI4(vp); 9763 retry: 9764 mutex_enter(&rp->r_statelock); 9765 9766 /* 9767 * Don't create dirty pages faster than they 9768 * can be cleaned so that the system doesn't 9769 * get imbalanced. If the async queue is 9770 * maxed out, then wait for it to drain before 9771 * creating more dirty pages. Also, wait for 9772 * any threads doing pagewalks in the vop_getattr 9773 * entry points so that they don't block for 9774 * long periods. 9775 */ 9776 if (rw == S_CREATE) { 9777 while ((mi->mi_max_threads != 0 && 9778 rp->r_awcount > 2 * mi->mi_max_threads) || 9779 rp->r_gcount > 0) 9780 cv_wait(&rp->r_cv, &rp->r_statelock); 9781 } 9782 9783 /* 9784 * If we are getting called as a side effect of an nfs_write() 9785 * operation the local file size might not be extended yet. 9786 * In this case we want to be able to return pages of zeroes. 9787 */ 9788 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9789 NFS4_DEBUG(nfs4_pageio_debug, 9790 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9791 "len=%llu, size=%llu, attrsize =%llu", off, 9792 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9793 mutex_exit(&rp->r_statelock); 9794 return (EFAULT); /* beyond EOF */ 9795 } 9796 9797 mutex_exit(&rp->r_statelock); 9798 9799 if (len <= PAGESIZE) { 9800 error = nfs4_getapage(vp, off, len, protp, pl, plsz, 9801 seg, addr, rw, cr); 9802 NFS4_DEBUG(nfs4_pageio_debug && error, 9803 (CE_NOTE, "getpage error %d; off=%lld, " 9804 "len=%lld", error, off, (u_longlong_t)len)); 9805 } else { 9806 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9807 pl, plsz, seg, addr, rw, cr); 9808 NFS4_DEBUG(nfs4_pageio_debug && error, 9809 (CE_NOTE, "getpages error %d; off=%lld, " 9810 "len=%lld", error, off, (u_longlong_t)len)); 9811 } 9812 9813 switch (error) { 9814 case NFS_EOF: 9815 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9816 goto retry; 9817 case ESTALE: 9818 nfs4_purge_stale_fh(error, vp, cr); 9819 } 9820 9821 return (error); 9822 } 9823 9824 /* 9825 * Called from pvn_getpages or nfs4_getpage to get a particular page. 9826 */ 9827 /* ARGSUSED */ 9828 static int 9829 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9830 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9831 enum seg_rw rw, cred_t *cr) 9832 { 9833 rnode4_t *rp; 9834 uint_t bsize; 9835 struct buf *bp; 9836 page_t *pp; 9837 u_offset_t lbn; 9838 u_offset_t io_off; 9839 u_offset_t blkoff; 9840 u_offset_t rablkoff; 9841 size_t io_len; 9842 uint_t blksize; 9843 int error; 9844 int readahead; 9845 int readahead_issued = 0; 9846 int ra_window; /* readahead window */ 9847 page_t *pagefound; 9848 page_t *savepp; 9849 9850 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9851 return (EIO); 9852 9853 rp = VTOR4(vp); 9854 ASSERT(!IS_SHADOW(vp, rp)); 9855 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9856 9857 reread: 9858 bp = NULL; 9859 pp = NULL; 9860 pagefound = NULL; 9861 9862 if (pl != NULL) 9863 pl[0] = NULL; 9864 9865 error = 0; 9866 lbn = off / bsize; 9867 blkoff = lbn * bsize; 9868 9869 /* 9870 * Queueing up the readahead before doing the synchronous read 9871 * results in a significant increase in read throughput because 9872 * of the increased parallelism between the async threads and 9873 * the process context. 9874 */ 9875 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9876 rw != S_CREATE && 9877 !(vp->v_flag & VNOCACHE)) { 9878 mutex_enter(&rp->r_statelock); 9879 9880 /* 9881 * Calculate the number of readaheads to do. 9882 * a) No readaheads at offset = 0. 9883 * b) Do maximum(nfs4_nra) readaheads when the readahead 9884 * window is closed. 9885 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9886 * upon how far the readahead window is open or close. 9887 * d) No readaheads if rp->r_nextr is not within the scope 9888 * of the readahead window (random i/o). 9889 */ 9890 9891 if (off == 0) 9892 readahead = 0; 9893 else if (blkoff == rp->r_nextr) 9894 readahead = nfs4_nra; 9895 else if (rp->r_nextr > blkoff && 9896 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9897 <= (nfs4_nra - 1))) 9898 readahead = nfs4_nra - ra_window; 9899 else 9900 readahead = 0; 9901 9902 rablkoff = rp->r_nextr; 9903 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9904 mutex_exit(&rp->r_statelock); 9905 if (nfs4_async_readahead(vp, rablkoff + bsize, 9906 addr + (rablkoff + bsize - off), 9907 seg, cr, nfs4_readahead) < 0) { 9908 mutex_enter(&rp->r_statelock); 9909 break; 9910 } 9911 readahead--; 9912 rablkoff += bsize; 9913 /* 9914 * Indicate that we did a readahead so 9915 * readahead offset is not updated 9916 * by the synchronous read below. 9917 */ 9918 readahead_issued = 1; 9919 mutex_enter(&rp->r_statelock); 9920 /* 9921 * set readahead offset to 9922 * offset of last async readahead 9923 * request. 9924 */ 9925 rp->r_nextr = rablkoff; 9926 } 9927 mutex_exit(&rp->r_statelock); 9928 } 9929 9930 again: 9931 if ((pagefound = page_exists(vp, off)) == NULL) { 9932 if (pl == NULL) { 9933 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9934 nfs4_readahead); 9935 } else if (rw == S_CREATE) { 9936 /* 9937 * Block for this page is not allocated, or the offset 9938 * is beyond the current allocation size, or we're 9939 * allocating a swap slot and the page was not found, 9940 * so allocate it and return a zero page. 9941 */ 9942 if ((pp = page_create_va(vp, off, 9943 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9944 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9945 io_len = PAGESIZE; 9946 mutex_enter(&rp->r_statelock); 9947 rp->r_nextr = off + PAGESIZE; 9948 mutex_exit(&rp->r_statelock); 9949 } else { 9950 /* 9951 * Need to go to server to get a block 9952 */ 9953 mutex_enter(&rp->r_statelock); 9954 if (blkoff < rp->r_size && 9955 blkoff + bsize > rp->r_size) { 9956 /* 9957 * If less than a block left in 9958 * file read less than a block. 9959 */ 9960 if (rp->r_size <= off) { 9961 /* 9962 * Trying to access beyond EOF, 9963 * set up to get at least one page. 9964 */ 9965 blksize = off + PAGESIZE - blkoff; 9966 } else 9967 blksize = rp->r_size - blkoff; 9968 } else if ((off == 0) || 9969 (off != rp->r_nextr && !readahead_issued)) { 9970 blksize = PAGESIZE; 9971 blkoff = off; /* block = page here */ 9972 } else 9973 blksize = bsize; 9974 mutex_exit(&rp->r_statelock); 9975 9976 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9977 &io_len, blkoff, blksize, 0); 9978 9979 /* 9980 * Some other thread has entered the page, 9981 * so just use it. 9982 */ 9983 if (pp == NULL) 9984 goto again; 9985 9986 /* 9987 * Now round the request size up to page boundaries. 9988 * This ensures that the entire page will be 9989 * initialized to zeroes if EOF is encountered. 9990 */ 9991 io_len = ptob(btopr(io_len)); 9992 9993 bp = pageio_setup(pp, io_len, vp, B_READ); 9994 ASSERT(bp != NULL); 9995 9996 /* 9997 * pageio_setup should have set b_addr to 0. This 9998 * is correct since we want to do I/O on a page 9999 * boundary. bp_mapin will use this addr to calculate 10000 * an offset, and then set b_addr to the kernel virtual 10001 * address it allocated for us. 10002 */ 10003 ASSERT(bp->b_un.b_addr == 0); 10004 10005 bp->b_edev = 0; 10006 bp->b_dev = 0; 10007 bp->b_lblkno = lbtodb(io_off); 10008 bp->b_file = vp; 10009 bp->b_offset = (offset_t)off; 10010 bp_mapin(bp); 10011 10012 /* 10013 * If doing a write beyond what we believe is EOF, 10014 * don't bother trying to read the pages from the 10015 * server, we'll just zero the pages here. We 10016 * don't check that the rw flag is S_WRITE here 10017 * because some implementations may attempt a 10018 * read access to the buffer before copying data. 10019 */ 10020 mutex_enter(&rp->r_statelock); 10021 if (io_off >= rp->r_size && seg == segkmap) { 10022 mutex_exit(&rp->r_statelock); 10023 bzero(bp->b_un.b_addr, io_len); 10024 } else { 10025 mutex_exit(&rp->r_statelock); 10026 error = nfs4_bio(bp, NULL, cr, FALSE); 10027 } 10028 10029 /* 10030 * Unmap the buffer before freeing it. 10031 */ 10032 bp_mapout(bp); 10033 pageio_done(bp); 10034 10035 savepp = pp; 10036 do { 10037 pp->p_fsdata = C_NOCOMMIT; 10038 } while ((pp = pp->p_next) != savepp); 10039 10040 if (error == NFS_EOF) { 10041 /* 10042 * If doing a write system call just return 10043 * zeroed pages, else user tried to get pages 10044 * beyond EOF, return error. We don't check 10045 * that the rw flag is S_WRITE here because 10046 * some implementations may attempt a read 10047 * access to the buffer before copying data. 10048 */ 10049 if (seg == segkmap) 10050 error = 0; 10051 else 10052 error = EFAULT; 10053 } 10054 10055 if (!readahead_issued && !error) { 10056 mutex_enter(&rp->r_statelock); 10057 rp->r_nextr = io_off + io_len; 10058 mutex_exit(&rp->r_statelock); 10059 } 10060 } 10061 } 10062 10063 out: 10064 if (pl == NULL) 10065 return (error); 10066 10067 if (error) { 10068 if (pp != NULL) 10069 pvn_read_done(pp, B_ERROR); 10070 return (error); 10071 } 10072 10073 if (pagefound) { 10074 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 10075 10076 /* 10077 * Page exists in the cache, acquire the appropriate lock. 10078 * If this fails, start all over again. 10079 */ 10080 if ((pp = page_lookup(vp, off, se)) == NULL) { 10081 #ifdef DEBUG 10082 nfs4_lostpage++; 10083 #endif 10084 goto reread; 10085 } 10086 pl[0] = pp; 10087 pl[1] = NULL; 10088 return (0); 10089 } 10090 10091 if (pp != NULL) 10092 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 10093 10094 return (error); 10095 } 10096 10097 static void 10098 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 10099 cred_t *cr) 10100 { 10101 int error; 10102 page_t *pp; 10103 u_offset_t io_off; 10104 size_t io_len; 10105 struct buf *bp; 10106 uint_t bsize, blksize; 10107 rnode4_t *rp = VTOR4(vp); 10108 page_t *savepp; 10109 10110 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10111 10112 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10113 10114 mutex_enter(&rp->r_statelock); 10115 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 10116 /* 10117 * If less than a block left in file read less 10118 * than a block. 10119 */ 10120 blksize = rp->r_size - blkoff; 10121 } else 10122 blksize = bsize; 10123 mutex_exit(&rp->r_statelock); 10124 10125 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 10126 &io_off, &io_len, blkoff, blksize, 1); 10127 /* 10128 * The isra flag passed to the kluster function is 1, we may have 10129 * gotten a return value of NULL for a variety of reasons (# of free 10130 * pages < minfree, someone entered the page on the vnode etc). In all 10131 * cases, we want to punt on the readahead. 10132 */ 10133 if (pp == NULL) 10134 return; 10135 10136 /* 10137 * Now round the request size up to page boundaries. 10138 * This ensures that the entire page will be 10139 * initialized to zeroes if EOF is encountered. 10140 */ 10141 io_len = ptob(btopr(io_len)); 10142 10143 bp = pageio_setup(pp, io_len, vp, B_READ); 10144 ASSERT(bp != NULL); 10145 10146 /* 10147 * pageio_setup should have set b_addr to 0. This is correct since 10148 * we want to do I/O on a page boundary. bp_mapin() will use this addr 10149 * to calculate an offset, and then set b_addr to the kernel virtual 10150 * address it allocated for us. 10151 */ 10152 ASSERT(bp->b_un.b_addr == 0); 10153 10154 bp->b_edev = 0; 10155 bp->b_dev = 0; 10156 bp->b_lblkno = lbtodb(io_off); 10157 bp->b_file = vp; 10158 bp->b_offset = (offset_t)blkoff; 10159 bp_mapin(bp); 10160 10161 /* 10162 * If doing a write beyond what we believe is EOF, don't bother trying 10163 * to read the pages from the server, we'll just zero the pages here. 10164 * We don't check that the rw flag is S_WRITE here because some 10165 * implementations may attempt a read access to the buffer before 10166 * copying data. 10167 */ 10168 mutex_enter(&rp->r_statelock); 10169 if (io_off >= rp->r_size && seg == segkmap) { 10170 mutex_exit(&rp->r_statelock); 10171 bzero(bp->b_un.b_addr, io_len); 10172 error = 0; 10173 } else { 10174 mutex_exit(&rp->r_statelock); 10175 error = nfs4_bio(bp, NULL, cr, TRUE); 10176 if (error == NFS_EOF) 10177 error = 0; 10178 } 10179 10180 /* 10181 * Unmap the buffer before freeing it. 10182 */ 10183 bp_mapout(bp); 10184 pageio_done(bp); 10185 10186 savepp = pp; 10187 do { 10188 pp->p_fsdata = C_NOCOMMIT; 10189 } while ((pp = pp->p_next) != savepp); 10190 10191 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10192 10193 /* 10194 * In case of error set readahead offset 10195 * to the lowest offset. 10196 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10197 */ 10198 if (error && rp->r_nextr > io_off) { 10199 mutex_enter(&rp->r_statelock); 10200 if (rp->r_nextr > io_off) 10201 rp->r_nextr = io_off; 10202 mutex_exit(&rp->r_statelock); 10203 } 10204 } 10205 10206 /* 10207 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10208 * If len == 0, do from off to EOF. 10209 * 10210 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10211 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10212 * (from pageout). 10213 */ 10214 /* ARGSUSED */ 10215 static int 10216 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 10217 caller_context_t *ct) 10218 { 10219 int error; 10220 rnode4_t *rp; 10221 10222 ASSERT(cr != NULL); 10223 10224 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10225 return (EIO); 10226 10227 rp = VTOR4(vp); 10228 if (IS_SHADOW(vp, rp)) 10229 vp = RTOV4(rp); 10230 10231 /* 10232 * XXX - Why should this check be made here? 10233 */ 10234 if (vp->v_flag & VNOMAP) 10235 return (ENOSYS); 10236 10237 if (len == 0 && !(flags & B_INVAL) && 10238 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10239 return (0); 10240 10241 mutex_enter(&rp->r_statelock); 10242 rp->r_count++; 10243 mutex_exit(&rp->r_statelock); 10244 error = nfs4_putpages(vp, off, len, flags, cr); 10245 mutex_enter(&rp->r_statelock); 10246 rp->r_count--; 10247 cv_broadcast(&rp->r_cv); 10248 mutex_exit(&rp->r_statelock); 10249 10250 return (error); 10251 } 10252 10253 /* 10254 * Write out a single page, possibly klustering adjacent dirty pages. 10255 */ 10256 int 10257 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10258 int flags, cred_t *cr) 10259 { 10260 u_offset_t io_off; 10261 u_offset_t lbn_off; 10262 u_offset_t lbn; 10263 size_t io_len; 10264 uint_t bsize; 10265 int error; 10266 rnode4_t *rp; 10267 10268 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10269 ASSERT(pp != NULL); 10270 ASSERT(cr != NULL); 10271 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10272 10273 rp = VTOR4(vp); 10274 ASSERT(rp->r_count > 0); 10275 ASSERT(!IS_SHADOW(vp, rp)); 10276 10277 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10278 lbn = pp->p_offset / bsize; 10279 lbn_off = lbn * bsize; 10280 10281 /* 10282 * Find a kluster that fits in one block, or in 10283 * one page if pages are bigger than blocks. If 10284 * there is less file space allocated than a whole 10285 * page, we'll shorten the i/o request below. 10286 */ 10287 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10288 roundup(bsize, PAGESIZE), flags); 10289 10290 /* 10291 * pvn_write_kluster shouldn't have returned a page with offset 10292 * behind the original page we were given. Verify that. 10293 */ 10294 ASSERT((pp->p_offset / bsize) >= lbn); 10295 10296 /* 10297 * Now pp will have the list of kept dirty pages marked for 10298 * write back. It will also handle invalidation and freeing 10299 * of pages that are not dirty. Check for page length rounding 10300 * problems. 10301 */ 10302 if (io_off + io_len > lbn_off + bsize) { 10303 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10304 io_len = lbn_off + bsize - io_off; 10305 } 10306 /* 10307 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10308 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10309 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10310 * progress and the r_size has not been made consistent with the 10311 * new size of the file. When the uiomove() completes the r_size is 10312 * updated and the R4MODINPROGRESS flag is cleared. 10313 * 10314 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10315 * consistent value of r_size. Without this handshaking, it is 10316 * possible that nfs4_bio() picks up the old value of r_size 10317 * before the uiomove() in writerp4() completes. This will result 10318 * in the write through nfs4_bio() being dropped. 10319 * 10320 * More precisely, there is a window between the time the uiomove() 10321 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10322 * operation intervenes in this window, the page will be picked up, 10323 * because it is dirty (it will be unlocked, unless it was 10324 * pagecreate'd). When the page is picked up as dirty, the dirty 10325 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10326 * checked. This will still be the old size. Therefore the page will 10327 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10328 * the page will be found to be clean and the write will be dropped. 10329 */ 10330 if (rp->r_flags & R4MODINPROGRESS) { 10331 mutex_enter(&rp->r_statelock); 10332 if ((rp->r_flags & R4MODINPROGRESS) && 10333 rp->r_modaddr + MAXBSIZE > io_off && 10334 rp->r_modaddr < io_off + io_len) { 10335 page_t *plist; 10336 /* 10337 * A write is in progress for this region of the file. 10338 * If we did not detect R4MODINPROGRESS here then this 10339 * path through nfs_putapage() would eventually go to 10340 * nfs4_bio() and may not write out all of the data 10341 * in the pages. We end up losing data. So we decide 10342 * to set the modified bit on each page in the page 10343 * list and mark the rnode with R4DIRTY. This write 10344 * will be restarted at some later time. 10345 */ 10346 plist = pp; 10347 while (plist != NULL) { 10348 pp = plist; 10349 page_sub(&plist, pp); 10350 hat_setmod(pp); 10351 page_io_unlock(pp); 10352 page_unlock(pp); 10353 } 10354 rp->r_flags |= R4DIRTY; 10355 mutex_exit(&rp->r_statelock); 10356 if (offp) 10357 *offp = io_off; 10358 if (lenp) 10359 *lenp = io_len; 10360 return (0); 10361 } 10362 mutex_exit(&rp->r_statelock); 10363 } 10364 10365 if (flags & B_ASYNC) { 10366 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10367 nfs4_sync_putapage); 10368 } else 10369 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10370 10371 if (offp) 10372 *offp = io_off; 10373 if (lenp) 10374 *lenp = io_len; 10375 return (error); 10376 } 10377 10378 static int 10379 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10380 int flags, cred_t *cr) 10381 { 10382 int error; 10383 rnode4_t *rp; 10384 10385 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10386 10387 flags |= B_WRITE; 10388 10389 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10390 10391 rp = VTOR4(vp); 10392 10393 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10394 error == EACCES) && 10395 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10396 if (!(rp->r_flags & R4OUTOFSPACE)) { 10397 mutex_enter(&rp->r_statelock); 10398 rp->r_flags |= R4OUTOFSPACE; 10399 mutex_exit(&rp->r_statelock); 10400 } 10401 flags |= B_ERROR; 10402 pvn_write_done(pp, flags); 10403 /* 10404 * If this was not an async thread, then try again to 10405 * write out the pages, but this time, also destroy 10406 * them whether or not the write is successful. This 10407 * will prevent memory from filling up with these 10408 * pages and destroying them is the only alternative 10409 * if they can't be written out. 10410 * 10411 * Don't do this if this is an async thread because 10412 * when the pages are unlocked in pvn_write_done, 10413 * some other thread could have come along, locked 10414 * them, and queued for an async thread. It would be 10415 * possible for all of the async threads to be tied 10416 * up waiting to lock the pages again and they would 10417 * all already be locked and waiting for an async 10418 * thread to handle them. Deadlock. 10419 */ 10420 if (!(flags & B_ASYNC)) { 10421 error = nfs4_putpage(vp, io_off, io_len, 10422 B_INVAL | B_FORCE, cr, NULL); 10423 } 10424 } else { 10425 if (error) 10426 flags |= B_ERROR; 10427 else if (rp->r_flags & R4OUTOFSPACE) { 10428 mutex_enter(&rp->r_statelock); 10429 rp->r_flags &= ~R4OUTOFSPACE; 10430 mutex_exit(&rp->r_statelock); 10431 } 10432 pvn_write_done(pp, flags); 10433 if (freemem < desfree) 10434 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10435 NFS4_WRITE_NOWAIT); 10436 } 10437 10438 return (error); 10439 } 10440 10441 #ifdef DEBUG 10442 int nfs4_force_open_before_mmap = 0; 10443 #endif 10444 10445 /* ARGSUSED */ 10446 static int 10447 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10448 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10449 caller_context_t *ct) 10450 { 10451 struct segvn_crargs vn_a; 10452 int error = 0; 10453 rnode4_t *rp = VTOR4(vp); 10454 mntinfo4_t *mi = VTOMI4(vp); 10455 10456 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10457 return (EIO); 10458 10459 if (vp->v_flag & VNOMAP) 10460 return (ENOSYS); 10461 10462 if (off < 0 || (off + len) < 0) 10463 return (ENXIO); 10464 10465 if (vp->v_type != VREG) 10466 return (ENODEV); 10467 10468 /* 10469 * If the file is delegated to the client don't do anything. 10470 * If the file is not delegated, then validate the data cache. 10471 */ 10472 mutex_enter(&rp->r_statev4_lock); 10473 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10474 mutex_exit(&rp->r_statev4_lock); 10475 error = nfs4_validate_caches(vp, cr); 10476 if (error) 10477 return (error); 10478 } else { 10479 mutex_exit(&rp->r_statev4_lock); 10480 } 10481 10482 /* 10483 * Check to see if the vnode is currently marked as not cachable. 10484 * This means portions of the file are locked (through VOP_FRLOCK). 10485 * In this case the map request must be refused. We use 10486 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10487 * 10488 * Atomically increment r_inmap after acquiring r_rwlock. The 10489 * idea here is to acquire r_rwlock to block read/write and 10490 * not to protect r_inmap. r_inmap will inform nfs4_read/write() 10491 * that we are in nfs4_map(). Now, r_rwlock is acquired in order 10492 * and we can prevent the deadlock that would have occurred 10493 * when nfs4_addmap() would have acquired it out of order. 10494 * 10495 * Since we are not protecting r_inmap by any lock, we do not 10496 * hold any lock when we decrement it. We atomically decrement 10497 * r_inmap after we release r_lkserlock. 10498 */ 10499 10500 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp))) 10501 return (EINTR); 10502 atomic_add_int(&rp->r_inmap, 1); 10503 nfs_rw_exit(&rp->r_rwlock); 10504 10505 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) { 10506 atomic_add_int(&rp->r_inmap, -1); 10507 return (EINTR); 10508 } 10509 10510 10511 if (vp->v_flag & VNOCACHE) { 10512 error = EAGAIN; 10513 goto done; 10514 } 10515 10516 /* 10517 * Don't allow concurrent locks and mapping if mandatory locking is 10518 * enabled. 10519 */ 10520 if (flk_has_remote_locks(vp)) { 10521 struct vattr va; 10522 va.va_mask = AT_MODE; 10523 error = nfs4getattr(vp, &va, cr); 10524 if (error != 0) 10525 goto done; 10526 if (MANDLOCK(vp, va.va_mode)) { 10527 error = EAGAIN; 10528 goto done; 10529 } 10530 } 10531 10532 /* 10533 * It is possible that the rnode has a lost lock request that we 10534 * are still trying to recover, and that the request conflicts with 10535 * this map request. 10536 * 10537 * An alternative approach would be for nfs4_safemap() to consider 10538 * queued lock requests when deciding whether to set or clear 10539 * VNOCACHE. This would require the frlock code path to call 10540 * nfs4_safemap() after enqueing a lost request. 10541 */ 10542 if (nfs4_map_lost_lock_conflict(vp)) { 10543 error = EAGAIN; 10544 goto done; 10545 } 10546 10547 as_rangelock(as); 10548 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 10549 if (error != 0) { 10550 as_rangeunlock(as); 10551 goto done; 10552 } 10553 10554 if (vp->v_type == VREG) { 10555 /* 10556 * We need to retrieve the open stream 10557 */ 10558 nfs4_open_stream_t *osp = NULL; 10559 nfs4_open_owner_t *oop = NULL; 10560 10561 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10562 if (oop != NULL) { 10563 /* returns with 'os_sync_lock' held */ 10564 osp = find_open_stream(oop, rp); 10565 open_owner_rele(oop); 10566 } 10567 if (osp == NULL) { 10568 #ifdef DEBUG 10569 if (nfs4_force_open_before_mmap) { 10570 error = EIO; 10571 goto done; 10572 } 10573 #endif 10574 /* returns with 'os_sync_lock' held */ 10575 error = open_and_get_osp(vp, cr, &osp); 10576 if (osp == NULL) { 10577 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10578 "nfs4_map: we tried to OPEN the file " 10579 "but again no osp, so fail with EIO")); 10580 goto done; 10581 } 10582 } 10583 10584 if (osp->os_failed_reopen) { 10585 mutex_exit(&osp->os_sync_lock); 10586 open_stream_rele(osp, rp); 10587 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10588 "nfs4_map: os_failed_reopen set on " 10589 "osp %p, cr %p, rp %s", (void *)osp, 10590 (void *)cr, rnode4info(rp))); 10591 error = EIO; 10592 goto done; 10593 } 10594 mutex_exit(&osp->os_sync_lock); 10595 open_stream_rele(osp, rp); 10596 } 10597 10598 vn_a.vp = vp; 10599 vn_a.offset = off; 10600 vn_a.type = (flags & MAP_TYPE); 10601 vn_a.prot = (uchar_t)prot; 10602 vn_a.maxprot = (uchar_t)maxprot; 10603 vn_a.flags = (flags & ~MAP_TYPE); 10604 vn_a.cred = cr; 10605 vn_a.amp = NULL; 10606 vn_a.szc = 0; 10607 vn_a.lgrp_mem_policy_flags = 0; 10608 10609 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10610 as_rangeunlock(as); 10611 10612 done: 10613 nfs_rw_exit(&rp->r_lkserlock); 10614 atomic_add_int(&rp->r_inmap, -1); 10615 return (error); 10616 } 10617 10618 /* 10619 * We're most likely dealing with a kernel module that likes to READ 10620 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10621 * officially OPEN the file to create the necessary client state 10622 * for bookkeeping of os_mmap_read/write counts. 10623 * 10624 * Since VOP_MAP only passes in a pointer to the vnode rather than 10625 * a double pointer, we can't handle the case where nfs4open_otw() 10626 * returns a different vnode than the one passed into VOP_MAP (since 10627 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10628 * we return NULL and let nfs4_map() fail. Note: the only case where 10629 * this should happen is if the file got removed and replaced with the 10630 * same name on the server (in addition to the fact that we're trying 10631 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10632 */ 10633 static int 10634 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10635 { 10636 rnode4_t *rp, *drp; 10637 vnode_t *dvp, *open_vp; 10638 char file_name[MAXNAMELEN]; 10639 int just_created; 10640 nfs4_open_stream_t *osp; 10641 nfs4_open_owner_t *oop; 10642 int error; 10643 10644 *ospp = NULL; 10645 open_vp = map_vp; 10646 10647 rp = VTOR4(open_vp); 10648 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10649 return (error); 10650 drp = VTOR4(dvp); 10651 10652 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10653 VN_RELE(dvp); 10654 return (EINTR); 10655 } 10656 10657 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10658 nfs_rw_exit(&drp->r_rwlock); 10659 VN_RELE(dvp); 10660 return (error); 10661 } 10662 10663 mutex_enter(&rp->r_statev4_lock); 10664 if (rp->created_v4) { 10665 rp->created_v4 = 0; 10666 mutex_exit(&rp->r_statev4_lock); 10667 10668 dnlc_update(dvp, file_name, open_vp); 10669 /* This is needed so we don't bump the open ref count */ 10670 just_created = 1; 10671 } else { 10672 mutex_exit(&rp->r_statev4_lock); 10673 just_created = 0; 10674 } 10675 10676 VN_HOLD(map_vp); 10677 10678 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10679 just_created); 10680 if (error) { 10681 nfs_rw_exit(&drp->r_rwlock); 10682 VN_RELE(dvp); 10683 VN_RELE(map_vp); 10684 return (error); 10685 } 10686 10687 nfs_rw_exit(&drp->r_rwlock); 10688 VN_RELE(dvp); 10689 10690 /* 10691 * If nfs4open_otw() returned a different vnode then "undo" 10692 * the open and return failure to the caller. 10693 */ 10694 if (!VN_CMP(open_vp, map_vp)) { 10695 nfs4_error_t e; 10696 10697 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10698 "open returned a different vnode")); 10699 /* 10700 * If there's an error, ignore it, 10701 * and let VOP_INACTIVE handle it. 10702 */ 10703 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10704 CLOSE_NORM, 0, 0, 0); 10705 VN_RELE(map_vp); 10706 return (EIO); 10707 } 10708 10709 VN_RELE(map_vp); 10710 10711 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10712 if (!oop) { 10713 nfs4_error_t e; 10714 10715 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10716 "no open owner")); 10717 /* 10718 * If there's an error, ignore it, 10719 * and let VOP_INACTIVE handle it. 10720 */ 10721 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10722 CLOSE_NORM, 0, 0, 0); 10723 return (EIO); 10724 } 10725 osp = find_open_stream(oop, rp); 10726 open_owner_rele(oop); 10727 *ospp = osp; 10728 return (0); 10729 } 10730 10731 /* 10732 * Please be aware that when this function is called, the address space write 10733 * a_lock is held. Do not put over the wire calls in this function. 10734 */ 10735 /* ARGSUSED */ 10736 static int 10737 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10738 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10739 caller_context_t *ct) 10740 { 10741 rnode4_t *rp; 10742 int error = 0; 10743 mntinfo4_t *mi; 10744 10745 mi = VTOMI4(vp); 10746 rp = VTOR4(vp); 10747 10748 if (nfs_zone() != mi->mi_zone) 10749 return (EIO); 10750 if (vp->v_flag & VNOMAP) 10751 return (ENOSYS); 10752 10753 /* 10754 * Don't need to update the open stream first, since this 10755 * mmap can't add any additional share access that isn't 10756 * already contained in the open stream (for the case where we 10757 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10758 * take into account os_mmap_read[write] counts). 10759 */ 10760 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10761 10762 if (vp->v_type == VREG) { 10763 /* 10764 * We need to retrieve the open stream and update the counts. 10765 * If there is no open stream here, something is wrong. 10766 */ 10767 nfs4_open_stream_t *osp = NULL; 10768 nfs4_open_owner_t *oop = NULL; 10769 10770 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10771 if (oop != NULL) { 10772 /* returns with 'os_sync_lock' held */ 10773 osp = find_open_stream(oop, rp); 10774 open_owner_rele(oop); 10775 } 10776 if (osp == NULL) { 10777 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10778 "nfs4_addmap: we should have an osp" 10779 "but we don't, so fail with EIO")); 10780 error = EIO; 10781 goto out; 10782 } 10783 10784 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10785 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10786 10787 /* 10788 * Update the map count in the open stream. 10789 * This is necessary in the case where we 10790 * open/mmap/close/, then the server reboots, and we 10791 * attempt to reopen. If the mmap doesn't add share 10792 * access then we send an invalid reopen with 10793 * access = NONE. 10794 * 10795 * We need to specifically check each PROT_* so a mmap 10796 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10797 * read and write access. A simple comparison of prot 10798 * to ~PROT_WRITE to determine read access is insufficient 10799 * since prot can be |= with PROT_USER, etc. 10800 */ 10801 10802 /* 10803 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10804 */ 10805 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10806 osp->os_mmap_write += btopr(len); 10807 if (maxprot & PROT_READ) 10808 osp->os_mmap_read += btopr(len); 10809 if (maxprot & PROT_EXEC) 10810 osp->os_mmap_read += btopr(len); 10811 /* 10812 * Ensure that os_mmap_read gets incremented, even if 10813 * maxprot were to look like PROT_NONE. 10814 */ 10815 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10816 !(maxprot & PROT_EXEC)) 10817 osp->os_mmap_read += btopr(len); 10818 osp->os_mapcnt += btopr(len); 10819 mutex_exit(&osp->os_sync_lock); 10820 open_stream_rele(osp, rp); 10821 } 10822 10823 out: 10824 /* 10825 * If we got an error, then undo our 10826 * incrementing of 'r_mapcnt'. 10827 */ 10828 10829 if (error) { 10830 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10831 ASSERT(rp->r_mapcnt >= 0); 10832 } 10833 return (error); 10834 } 10835 10836 /* ARGSUSED */ 10837 static int 10838 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct) 10839 { 10840 10841 return (VTOR4(vp1) == VTOR4(vp2)); 10842 } 10843 10844 /* ARGSUSED */ 10845 static int 10846 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10847 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr, 10848 caller_context_t *ct) 10849 { 10850 int rc; 10851 u_offset_t start, end; 10852 rnode4_t *rp; 10853 int error = 0, intr = INTR4(vp); 10854 nfs4_error_t e; 10855 10856 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10857 return (EIO); 10858 10859 /* check for valid cmd parameter */ 10860 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10861 return (EINVAL); 10862 10863 /* Verify l_type. */ 10864 switch (bfp->l_type) { 10865 case F_RDLCK: 10866 if (cmd != F_GETLK && !(flag & FREAD)) 10867 return (EBADF); 10868 break; 10869 case F_WRLCK: 10870 if (cmd != F_GETLK && !(flag & FWRITE)) 10871 return (EBADF); 10872 break; 10873 case F_UNLCK: 10874 intr = 0; 10875 break; 10876 10877 default: 10878 return (EINVAL); 10879 } 10880 10881 /* check the validity of the lock range */ 10882 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10883 return (rc); 10884 if (rc = flk_check_lock_data(start, end, MAXEND)) 10885 return (rc); 10886 10887 /* 10888 * If the filesystem is mounted using local locking, pass the 10889 * request off to the local locking code. 10890 */ 10891 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10892 if (cmd == F_SETLK || cmd == F_SETLKW) { 10893 /* 10894 * For complete safety, we should be holding 10895 * r_lkserlock. However, we can't call 10896 * nfs4_safelock and then fs_frlock while 10897 * holding r_lkserlock, so just invoke 10898 * nfs4_safelock and expect that this will 10899 * catch enough of the cases. 10900 */ 10901 if (!nfs4_safelock(vp, bfp, cr)) 10902 return (EAGAIN); 10903 } 10904 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 10905 } 10906 10907 rp = VTOR4(vp); 10908 10909 /* 10910 * Check whether the given lock request can proceed, given the 10911 * current file mappings. 10912 */ 10913 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10914 return (EINTR); 10915 if (cmd == F_SETLK || cmd == F_SETLKW) { 10916 if (!nfs4_safelock(vp, bfp, cr)) { 10917 rc = EAGAIN; 10918 goto done; 10919 } 10920 } 10921 10922 /* 10923 * Flush the cache after waiting for async I/O to finish. For new 10924 * locks, this is so that the process gets the latest bits from the 10925 * server. For unlocks, this is so that other clients see the 10926 * latest bits once the file has been unlocked. If currently dirty 10927 * pages can't be flushed, then don't allow a lock to be set. But 10928 * allow unlocks to succeed, to avoid having orphan locks on the 10929 * server. 10930 */ 10931 if (cmd != F_GETLK) { 10932 mutex_enter(&rp->r_statelock); 10933 while (rp->r_count > 0) { 10934 if (intr) { 10935 klwp_t *lwp = ttolwp(curthread); 10936 10937 if (lwp != NULL) 10938 lwp->lwp_nostop++; 10939 if (cv_wait_sig(&rp->r_cv, 10940 &rp->r_statelock) == 0) { 10941 if (lwp != NULL) 10942 lwp->lwp_nostop--; 10943 rc = EINTR; 10944 break; 10945 } 10946 if (lwp != NULL) 10947 lwp->lwp_nostop--; 10948 } else 10949 cv_wait(&rp->r_cv, &rp->r_statelock); 10950 } 10951 mutex_exit(&rp->r_statelock); 10952 if (rc != 0) 10953 goto done; 10954 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct); 10955 if (error) { 10956 if (error == ENOSPC || error == EDQUOT) { 10957 mutex_enter(&rp->r_statelock); 10958 if (!rp->r_error) 10959 rp->r_error = error; 10960 mutex_exit(&rp->r_statelock); 10961 } 10962 if (bfp->l_type != F_UNLCK) { 10963 rc = ENOLCK; 10964 goto done; 10965 } 10966 } 10967 } 10968 10969 /* 10970 * Call the lock manager to do the real work of contacting 10971 * the server and obtaining the lock. 10972 */ 10973 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10974 cr, &e, NULL, NULL); 10975 rc = e.error; 10976 10977 if (rc == 0) 10978 nfs4_lockcompletion(vp, cmd); 10979 10980 done: 10981 nfs_rw_exit(&rp->r_lkserlock); 10982 10983 return (rc); 10984 } 10985 10986 /* 10987 * Free storage space associated with the specified vnode. The portion 10988 * to be freed is specified by bfp->l_start and bfp->l_len (already 10989 * normalized to a "whence" of 0). 10990 * 10991 * This is an experimental facility whose continued existence is not 10992 * guaranteed. Currently, we only support the special case 10993 * of l_len == 0, meaning free to end of file. 10994 */ 10995 /* ARGSUSED */ 10996 static int 10997 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10998 offset_t offset, cred_t *cr, caller_context_t *ct) 10999 { 11000 int error; 11001 11002 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11003 return (EIO); 11004 ASSERT(vp->v_type == VREG); 11005 if (cmd != F_FREESP) 11006 return (EINVAL); 11007 11008 error = convoff(vp, bfp, 0, offset); 11009 if (!error) { 11010 ASSERT(bfp->l_start >= 0); 11011 if (bfp->l_len == 0) { 11012 struct vattr va; 11013 11014 va.va_mask = AT_SIZE; 11015 va.va_size = bfp->l_start; 11016 error = nfs4setattr(vp, &va, 0, cr, NULL); 11017 11018 if (error == 0 && bfp->l_start == 0) 11019 vnevent_truncate(vp, ct); 11020 } else 11021 error = EINVAL; 11022 } 11023 11024 return (error); 11025 } 11026 11027 /* ARGSUSED */ 11028 int 11029 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 11030 { 11031 rnode4_t *rp; 11032 rp = VTOR4(vp); 11033 11034 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 11035 vp = RTOV4(rp); 11036 } 11037 *vpp = vp; 11038 return (0); 11039 } 11040 11041 /* 11042 * Setup and add an address space callback to do the work of the delmap call. 11043 * The callback will (and must be) deleted in the actual callback function. 11044 * 11045 * This is done in order to take care of the problem that we have with holding 11046 * the address space's a_lock for a long period of time (e.g. if the NFS server 11047 * is down). Callbacks will be executed in the address space code while the 11048 * a_lock is not held. Holding the address space's a_lock causes things such 11049 * as ps and fork to hang because they are trying to acquire this lock as well. 11050 */ 11051 /* ARGSUSED */ 11052 static int 11053 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 11054 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 11055 caller_context_t *ct) 11056 { 11057 int caller_found; 11058 int error; 11059 rnode4_t *rp; 11060 nfs4_delmap_args_t *dmapp; 11061 nfs4_delmapcall_t *delmap_call; 11062 11063 if (vp->v_flag & VNOMAP) 11064 return (ENOSYS); 11065 11066 /* 11067 * A process may not change zones if it has NFS pages mmap'ed 11068 * in, so we can't legitimately get here from the wrong zone. 11069 */ 11070 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11071 11072 rp = VTOR4(vp); 11073 11074 /* 11075 * The way that the address space of this process deletes its mapping 11076 * of this file is via the following call chains: 11077 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11078 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11079 * 11080 * With the use of address space callbacks we are allowed to drop the 11081 * address space lock, a_lock, while executing the NFS operations that 11082 * need to go over the wire. Returning EAGAIN to the caller of this 11083 * function is what drives the execution of the callback that we add 11084 * below. The callback will be executed by the address space code 11085 * after dropping the a_lock. When the callback is finished, since 11086 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 11087 * is called again on the same segment to finish the rest of the work 11088 * that needs to happen during unmapping. 11089 * 11090 * This action of calling back into the segment driver causes 11091 * nfs4_delmap() to get called again, but since the callback was 11092 * already executed at this point, it already did the work and there 11093 * is nothing left for us to do. 11094 * 11095 * To Summarize: 11096 * - The first time nfs4_delmap is called by the current thread is when 11097 * we add the caller associated with this delmap to the delmap caller 11098 * list, add the callback, and return EAGAIN. 11099 * - The second time in this call chain when nfs4_delmap is called we 11100 * will find this caller in the delmap caller list and realize there 11101 * is no more work to do thus removing this caller from the list and 11102 * returning the error that was set in the callback execution. 11103 */ 11104 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 11105 if (caller_found) { 11106 /* 11107 * 'error' is from the actual delmap operations. To avoid 11108 * hangs, we need to handle the return of EAGAIN differently 11109 * since this is what drives the callback execution. 11110 * In this case, we don't want to return EAGAIN and do the 11111 * callback execution because there are none to execute. 11112 */ 11113 if (error == EAGAIN) 11114 return (0); 11115 else 11116 return (error); 11117 } 11118 11119 /* current caller was not in the list */ 11120 delmap_call = nfs4_init_delmapcall(); 11121 11122 mutex_enter(&rp->r_statelock); 11123 list_insert_tail(&rp->r_indelmap, delmap_call); 11124 mutex_exit(&rp->r_statelock); 11125 11126 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 11127 11128 dmapp->vp = vp; 11129 dmapp->off = off; 11130 dmapp->addr = addr; 11131 dmapp->len = len; 11132 dmapp->prot = prot; 11133 dmapp->maxprot = maxprot; 11134 dmapp->flags = flags; 11135 dmapp->cr = cr; 11136 dmapp->caller = delmap_call; 11137 11138 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 11139 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 11140 11141 return (error ? error : EAGAIN); 11142 } 11143 11144 static nfs4_delmapcall_t * 11145 nfs4_init_delmapcall() 11146 { 11147 nfs4_delmapcall_t *delmap_call; 11148 11149 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 11150 delmap_call->call_id = curthread; 11151 delmap_call->error = 0; 11152 11153 return (delmap_call); 11154 } 11155 11156 static void 11157 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 11158 { 11159 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 11160 } 11161 11162 /* 11163 * Searches for the current delmap caller (based on curthread) in the list of 11164 * callers. If it is found, we remove it and free the delmap caller. 11165 * Returns: 11166 * 0 if the caller wasn't found 11167 * 1 if the caller was found, removed and freed. *errp will be set 11168 * to what the result of the delmap was. 11169 */ 11170 static int 11171 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 11172 { 11173 nfs4_delmapcall_t *delmap_call; 11174 11175 /* 11176 * If the list doesn't exist yet, we create it and return 11177 * that the caller wasn't found. No list = no callers. 11178 */ 11179 mutex_enter(&rp->r_statelock); 11180 if (!(rp->r_flags & R4DELMAPLIST)) { 11181 /* The list does not exist */ 11182 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 11183 offsetof(nfs4_delmapcall_t, call_node)); 11184 rp->r_flags |= R4DELMAPLIST; 11185 mutex_exit(&rp->r_statelock); 11186 return (0); 11187 } else { 11188 /* The list exists so search it */ 11189 for (delmap_call = list_head(&rp->r_indelmap); 11190 delmap_call != NULL; 11191 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 11192 if (delmap_call->call_id == curthread) { 11193 /* current caller is in the list */ 11194 *errp = delmap_call->error; 11195 list_remove(&rp->r_indelmap, delmap_call); 11196 mutex_exit(&rp->r_statelock); 11197 nfs4_free_delmapcall(delmap_call); 11198 return (1); 11199 } 11200 } 11201 } 11202 mutex_exit(&rp->r_statelock); 11203 return (0); 11204 } 11205 11206 /* 11207 * Remove some pages from an mmap'd vnode. Just update the 11208 * count of pages. If doing close-to-open, then flush and 11209 * commit all of the pages associated with this file. 11210 * Otherwise, start an asynchronous page flush to write out 11211 * any dirty pages. This will also associate a credential 11212 * with the rnode which can be used to write the pages. 11213 */ 11214 /* ARGSUSED */ 11215 static void 11216 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11217 { 11218 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11219 rnode4_t *rp; 11220 mntinfo4_t *mi; 11221 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11222 11223 rp = VTOR4(dmapp->vp); 11224 mi = VTOMI4(dmapp->vp); 11225 11226 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11227 ASSERT(rp->r_mapcnt >= 0); 11228 11229 /* 11230 * Initiate a page flush and potential commit if there are 11231 * pages, the file system was not mounted readonly, the segment 11232 * was mapped shared, and the pages themselves were writeable. 11233 */ 11234 if (nfs4_has_pages(dmapp->vp) && 11235 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11236 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11237 mutex_enter(&rp->r_statelock); 11238 rp->r_flags |= R4DIRTY; 11239 mutex_exit(&rp->r_statelock); 11240 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11241 dmapp->len, dmapp->cr); 11242 if (!e.error) { 11243 mutex_enter(&rp->r_statelock); 11244 e.error = rp->r_error; 11245 rp->r_error = 0; 11246 mutex_exit(&rp->r_statelock); 11247 } 11248 } else 11249 e.error = 0; 11250 11251 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11252 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11253 B_INVAL, dmapp->cr, NULL); 11254 11255 if (e.error) { 11256 e.stat = puterrno4(e.error); 11257 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11258 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11259 dmapp->caller->error = e.error; 11260 } 11261 11262 /* Check to see if we need to close the file */ 11263 11264 if (dmapp->vp->v_type == VREG) { 11265 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11266 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11267 11268 if (e.error != 0 || e.stat != NFS4_OK) { 11269 /* 11270 * Since it is possible that e.error == 0 and 11271 * e.stat != NFS4_OK (and vice versa), 11272 * we do the proper checking in order to get both 11273 * e.error and e.stat reporting the correct info. 11274 */ 11275 if (e.stat == NFS4_OK) 11276 e.stat = puterrno4(e.error); 11277 if (e.error == 0) 11278 e.error = geterrno4(e.stat); 11279 11280 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11281 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11282 dmapp->caller->error = e.error; 11283 } 11284 } 11285 11286 (void) as_delete_callback(as, arg); 11287 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11288 } 11289 11290 11291 static uint_t 11292 fattr4_maxfilesize_to_bits(uint64_t ll) 11293 { 11294 uint_t l = 1; 11295 11296 if (ll == 0) { 11297 return (0); 11298 } 11299 11300 if (ll & 0xffffffff00000000) { 11301 l += 32; ll >>= 32; 11302 } 11303 if (ll & 0xffff0000) { 11304 l += 16; ll >>= 16; 11305 } 11306 if (ll & 0xff00) { 11307 l += 8; ll >>= 8; 11308 } 11309 if (ll & 0xf0) { 11310 l += 4; ll >>= 4; 11311 } 11312 if (ll & 0xc) { 11313 l += 2; ll >>= 2; 11314 } 11315 if (ll & 0x2) { 11316 l += 1; 11317 } 11318 return (l); 11319 } 11320 11321 static int 11322 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr) 11323 { 11324 vnode_t *avp = NULL; 11325 int error; 11326 11327 if ((error = nfs4lookup_xattr(vp, "", &avp, 11328 LOOKUP_XATTR, cr)) == 0) 11329 error = do_xattr_exists_check(avp, valp, cr); 11330 if (avp) 11331 VN_RELE(avp); 11332 11333 return (error); 11334 } 11335 11336 /* ARGSUSED */ 11337 int 11338 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 11339 caller_context_t *ct) 11340 { 11341 int error; 11342 hrtime_t t; 11343 rnode4_t *rp; 11344 nfs4_ga_res_t gar; 11345 nfs4_ga_ext_res_t ger; 11346 11347 gar.n4g_ext_res = &ger; 11348 11349 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11350 return (EIO); 11351 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11352 *valp = MAXPATHLEN; 11353 return (0); 11354 } 11355 if (cmd == _PC_ACL_ENABLED) { 11356 *valp = _ACL_ACE_ENABLED; 11357 return (0); 11358 } 11359 11360 rp = VTOR4(vp); 11361 if (cmd == _PC_XATTR_EXISTS) { 11362 /* 11363 * The existence of the xattr directory is not sufficient 11364 * for determining whether generic user attributes exists. 11365 * The attribute directory could only be a transient directory 11366 * used for Solaris sysattr support. Do a small readdir 11367 * to verify if the only entries are sysattrs or not. 11368 * 11369 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11370 * is NULL. Once the xadir vp exists, we can create xattrs, 11371 * and we don't have any way to update the "base" object's 11372 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11373 * could help out. 11374 */ 11375 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11376 rp->r_xattr_dir == NULL) { 11377 return (nfs4_have_xattrs(vp, valp, cr)); 11378 } 11379 } else { /* OLD CODE */ 11380 if (ATTRCACHE4_VALID(vp)) { 11381 mutex_enter(&rp->r_statelock); 11382 if (rp->r_pathconf.pc4_cache_valid) { 11383 error = 0; 11384 switch (cmd) { 11385 case _PC_FILESIZEBITS: 11386 *valp = 11387 rp->r_pathconf.pc4_filesizebits; 11388 break; 11389 case _PC_LINK_MAX: 11390 *valp = 11391 rp->r_pathconf.pc4_link_max; 11392 break; 11393 case _PC_NAME_MAX: 11394 *valp = 11395 rp->r_pathconf.pc4_name_max; 11396 break; 11397 case _PC_CHOWN_RESTRICTED: 11398 *valp = 11399 rp->r_pathconf.pc4_chown_restricted; 11400 break; 11401 case _PC_NO_TRUNC: 11402 *valp = 11403 rp->r_pathconf.pc4_no_trunc; 11404 break; 11405 default: 11406 error = EINVAL; 11407 break; 11408 } 11409 mutex_exit(&rp->r_statelock); 11410 #ifdef DEBUG 11411 nfs4_pathconf_cache_hits++; 11412 #endif 11413 return (error); 11414 } 11415 mutex_exit(&rp->r_statelock); 11416 } 11417 } 11418 #ifdef DEBUG 11419 nfs4_pathconf_cache_misses++; 11420 #endif 11421 11422 t = gethrtime(); 11423 11424 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11425 11426 if (error) { 11427 mutex_enter(&rp->r_statelock); 11428 rp->r_pathconf.pc4_cache_valid = FALSE; 11429 rp->r_pathconf.pc4_xattr_valid = FALSE; 11430 mutex_exit(&rp->r_statelock); 11431 return (error); 11432 } 11433 11434 /* interpret the max filesize */ 11435 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11436 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11437 11438 /* Store the attributes we just received */ 11439 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11440 11441 switch (cmd) { 11442 case _PC_FILESIZEBITS: 11443 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11444 break; 11445 case _PC_LINK_MAX: 11446 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11447 break; 11448 case _PC_NAME_MAX: 11449 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11450 break; 11451 case _PC_CHOWN_RESTRICTED: 11452 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11453 break; 11454 case _PC_NO_TRUNC: 11455 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11456 break; 11457 case _PC_XATTR_EXISTS: 11458 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) { 11459 if (error = nfs4_have_xattrs(vp, valp, cr)) 11460 return (error); 11461 } 11462 break; 11463 default: 11464 return (EINVAL); 11465 } 11466 11467 return (0); 11468 } 11469 11470 /* 11471 * Called by async thread to do synchronous pageio. Do the i/o, wait 11472 * for it to complete, and cleanup the page list when done. 11473 */ 11474 static int 11475 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11476 int flags, cred_t *cr) 11477 { 11478 int error; 11479 11480 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11481 11482 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11483 if (flags & B_READ) 11484 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11485 else 11486 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11487 return (error); 11488 } 11489 11490 /* ARGSUSED */ 11491 static int 11492 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11493 int flags, cred_t *cr, caller_context_t *ct) 11494 { 11495 int error; 11496 rnode4_t *rp; 11497 11498 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11499 return (EIO); 11500 11501 if (pp == NULL) 11502 return (EINVAL); 11503 11504 rp = VTOR4(vp); 11505 mutex_enter(&rp->r_statelock); 11506 rp->r_count++; 11507 mutex_exit(&rp->r_statelock); 11508 11509 if (flags & B_ASYNC) { 11510 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11511 nfs4_sync_pageio); 11512 } else 11513 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11514 mutex_enter(&rp->r_statelock); 11515 rp->r_count--; 11516 cv_broadcast(&rp->r_cv); 11517 mutex_exit(&rp->r_statelock); 11518 return (error); 11519 } 11520 11521 /* ARGSUSED */ 11522 static void 11523 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr, 11524 caller_context_t *ct) 11525 { 11526 int error; 11527 rnode4_t *rp; 11528 page_t *plist; 11529 page_t *pptr; 11530 offset3 offset; 11531 count3 len; 11532 k_sigset_t smask; 11533 11534 /* 11535 * We should get called with fl equal to either B_FREE or 11536 * B_INVAL. Any other value is illegal. 11537 * 11538 * The page that we are either supposed to free or destroy 11539 * should be exclusive locked and its io lock should not 11540 * be held. 11541 */ 11542 ASSERT(fl == B_FREE || fl == B_INVAL); 11543 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11544 11545 rp = VTOR4(vp); 11546 11547 /* 11548 * If the page doesn't need to be committed or we shouldn't 11549 * even bother attempting to commit it, then just make sure 11550 * that the p_fsdata byte is clear and then either free or 11551 * destroy the page as appropriate. 11552 */ 11553 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11554 pp->p_fsdata = C_NOCOMMIT; 11555 if (fl == B_FREE) 11556 page_free(pp, dn); 11557 else 11558 page_destroy(pp, dn); 11559 return; 11560 } 11561 11562 /* 11563 * If there is a page invalidation operation going on, then 11564 * if this is one of the pages being destroyed, then just 11565 * clear the p_fsdata byte and then either free or destroy 11566 * the page as appropriate. 11567 */ 11568 mutex_enter(&rp->r_statelock); 11569 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11570 mutex_exit(&rp->r_statelock); 11571 pp->p_fsdata = C_NOCOMMIT; 11572 if (fl == B_FREE) 11573 page_free(pp, dn); 11574 else 11575 page_destroy(pp, dn); 11576 return; 11577 } 11578 11579 /* 11580 * If we are freeing this page and someone else is already 11581 * waiting to do a commit, then just unlock the page and 11582 * return. That other thread will take care of commiting 11583 * this page. The page can be freed sometime after the 11584 * commit has finished. Otherwise, if the page is marked 11585 * as delay commit, then we may be getting called from 11586 * pvn_write_done, one page at a time. This could result 11587 * in one commit per page, so we end up doing lots of small 11588 * commits instead of fewer larger commits. This is bad, 11589 * we want do as few commits as possible. 11590 */ 11591 if (fl == B_FREE) { 11592 if (rp->r_flags & R4COMMITWAIT) { 11593 page_unlock(pp); 11594 mutex_exit(&rp->r_statelock); 11595 return; 11596 } 11597 if (pp->p_fsdata == C_DELAYCOMMIT) { 11598 pp->p_fsdata = C_COMMIT; 11599 page_unlock(pp); 11600 mutex_exit(&rp->r_statelock); 11601 return; 11602 } 11603 } 11604 11605 /* 11606 * Check to see if there is a signal which would prevent an 11607 * attempt to commit the pages from being successful. If so, 11608 * then don't bother with all of the work to gather pages and 11609 * generate the unsuccessful RPC. Just return from here and 11610 * let the page be committed at some later time. 11611 */ 11612 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11613 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11614 sigunintr(&smask); 11615 page_unlock(pp); 11616 mutex_exit(&rp->r_statelock); 11617 return; 11618 } 11619 sigunintr(&smask); 11620 11621 /* 11622 * We are starting to need to commit pages, so let's try 11623 * to commit as many as possible at once to reduce the 11624 * overhead. 11625 * 11626 * Set the `commit inprogress' state bit. We must 11627 * first wait until any current one finishes. Then 11628 * we initialize the c_pages list with this page. 11629 */ 11630 while (rp->r_flags & R4COMMIT) { 11631 rp->r_flags |= R4COMMITWAIT; 11632 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11633 rp->r_flags &= ~R4COMMITWAIT; 11634 } 11635 rp->r_flags |= R4COMMIT; 11636 mutex_exit(&rp->r_statelock); 11637 ASSERT(rp->r_commit.c_pages == NULL); 11638 rp->r_commit.c_pages = pp; 11639 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11640 rp->r_commit.c_commlen = PAGESIZE; 11641 11642 /* 11643 * Gather together all other pages which can be committed. 11644 * They will all be chained off r_commit.c_pages. 11645 */ 11646 nfs4_get_commit(vp); 11647 11648 /* 11649 * Clear the `commit inprogress' status and disconnect 11650 * the list of pages to be committed from the rnode. 11651 * At this same time, we also save the starting offset 11652 * and length of data to be committed on the server. 11653 */ 11654 plist = rp->r_commit.c_pages; 11655 rp->r_commit.c_pages = NULL; 11656 offset = rp->r_commit.c_commbase; 11657 len = rp->r_commit.c_commlen; 11658 mutex_enter(&rp->r_statelock); 11659 rp->r_flags &= ~R4COMMIT; 11660 cv_broadcast(&rp->r_commit.c_cv); 11661 mutex_exit(&rp->r_statelock); 11662 11663 if (curproc == proc_pageout || curproc == proc_fsflush || 11664 nfs_zone() != VTOMI4(vp)->mi_zone) { 11665 nfs4_async_commit(vp, plist, offset, len, 11666 cr, do_nfs4_async_commit); 11667 return; 11668 } 11669 11670 /* 11671 * Actually generate the COMMIT op over the wire operation. 11672 */ 11673 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11674 11675 /* 11676 * If we got an error during the commit, just unlock all 11677 * of the pages. The pages will get retransmitted to the 11678 * server during a putpage operation. 11679 */ 11680 if (error) { 11681 while (plist != NULL) { 11682 pptr = plist; 11683 page_sub(&plist, pptr); 11684 page_unlock(pptr); 11685 } 11686 return; 11687 } 11688 11689 /* 11690 * We've tried as hard as we can to commit the data to stable 11691 * storage on the server. We just unlock the rest of the pages 11692 * and clear the commit required state. They will be put 11693 * onto the tail of the cachelist if they are nolonger 11694 * mapped. 11695 */ 11696 while (plist != pp) { 11697 pptr = plist; 11698 page_sub(&plist, pptr); 11699 pptr->p_fsdata = C_NOCOMMIT; 11700 page_unlock(pptr); 11701 } 11702 11703 /* 11704 * It is possible that nfs4_commit didn't return error but 11705 * some other thread has modified the page we are going 11706 * to free/destroy. 11707 * In this case we need to rewrite the page. Do an explicit check 11708 * before attempting to free/destroy the page. If modified, needs to 11709 * be rewritten so unlock the page and return. 11710 */ 11711 if (hat_ismod(pp)) { 11712 pp->p_fsdata = C_NOCOMMIT; 11713 page_unlock(pp); 11714 return; 11715 } 11716 11717 /* 11718 * Now, as appropriate, either free or destroy the page 11719 * that we were called with. 11720 */ 11721 pp->p_fsdata = C_NOCOMMIT; 11722 if (fl == B_FREE) 11723 page_free(pp, dn); 11724 else 11725 page_destroy(pp, dn); 11726 } 11727 11728 /* 11729 * Commit requires that the current fh be the file written to. 11730 * The compound op structure is: 11731 * PUTFH(file), COMMIT 11732 */ 11733 static int 11734 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11735 { 11736 COMPOUND4args_clnt args; 11737 COMPOUND4res_clnt res; 11738 COMMIT4res *cm_res; 11739 nfs_argop4 argop[2]; 11740 nfs_resop4 *resop; 11741 int doqueue; 11742 mntinfo4_t *mi; 11743 rnode4_t *rp; 11744 cred_t *cred_otw = NULL; 11745 bool_t needrecov = FALSE; 11746 nfs4_recov_state_t recov_state; 11747 nfs4_open_stream_t *osp = NULL; 11748 bool_t first_time = TRUE; /* first time getting OTW cred */ 11749 bool_t last_time = FALSE; /* last time getting OTW cred */ 11750 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11751 11752 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11753 11754 rp = VTOR4(vp); 11755 11756 mi = VTOMI4(vp); 11757 recov_state.rs_flags = 0; 11758 recov_state.rs_num_retry_despite_err = 0; 11759 get_commit_cred: 11760 /* 11761 * Releases the osp, if a valid open stream is provided. 11762 * Puts a hold on the cred_otw and the new osp (if found). 11763 */ 11764 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11765 &first_time, &last_time); 11766 args.ctag = TAG_COMMIT; 11767 recov_retry: 11768 /* 11769 * Commit ops: putfh file; commit 11770 */ 11771 args.array_len = 2; 11772 args.array = argop; 11773 11774 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11775 &recov_state, NULL); 11776 if (e.error) { 11777 crfree(cred_otw); 11778 if (osp != NULL) 11779 open_stream_rele(osp, rp); 11780 return (e.error); 11781 } 11782 11783 /* putfh directory */ 11784 argop[0].argop = OP_CPUTFH; 11785 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11786 11787 /* commit */ 11788 argop[1].argop = OP_COMMIT; 11789 argop[1].nfs_argop4_u.opcommit.offset = offset; 11790 argop[1].nfs_argop4_u.opcommit.count = count; 11791 11792 doqueue = 1; 11793 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11794 11795 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11796 if (!needrecov && e.error) { 11797 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11798 needrecov); 11799 crfree(cred_otw); 11800 if (e.error == EACCES && last_time == FALSE) 11801 goto get_commit_cred; 11802 if (osp != NULL) 11803 open_stream_rele(osp, rp); 11804 return (e.error); 11805 } 11806 11807 if (needrecov) { 11808 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11809 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) { 11810 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11811 &recov_state, needrecov); 11812 if (!e.error) 11813 (void) xdr_free(xdr_COMPOUND4res_clnt, 11814 (caddr_t)&res); 11815 goto recov_retry; 11816 } 11817 if (e.error) { 11818 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11819 &recov_state, needrecov); 11820 crfree(cred_otw); 11821 if (osp != NULL) 11822 open_stream_rele(osp, rp); 11823 return (e.error); 11824 } 11825 /* fall through for res.status case */ 11826 } 11827 11828 if (res.status) { 11829 e.error = geterrno4(res.status); 11830 if (e.error == EACCES && last_time == FALSE) { 11831 crfree(cred_otw); 11832 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11833 &recov_state, needrecov); 11834 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11835 goto get_commit_cred; 11836 } 11837 /* 11838 * Can't do a nfs4_purge_stale_fh here because this 11839 * can cause a deadlock. nfs4_commit can 11840 * be called from nfs4_dispose which can be called 11841 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11842 * can call back to pvn_vplist_dirty. 11843 */ 11844 if (e.error == ESTALE) { 11845 mutex_enter(&rp->r_statelock); 11846 rp->r_flags |= R4STALE; 11847 if (!rp->r_error) 11848 rp->r_error = e.error; 11849 mutex_exit(&rp->r_statelock); 11850 PURGE_ATTRCACHE4(vp); 11851 } else { 11852 mutex_enter(&rp->r_statelock); 11853 if (!rp->r_error) 11854 rp->r_error = e.error; 11855 mutex_exit(&rp->r_statelock); 11856 } 11857 } else { 11858 ASSERT(rp->r_flags & R4HAVEVERF); 11859 resop = &res.array[1]; /* commit res */ 11860 cm_res = &resop->nfs_resop4_u.opcommit; 11861 mutex_enter(&rp->r_statelock); 11862 if (cm_res->writeverf == rp->r_writeverf) { 11863 mutex_exit(&rp->r_statelock); 11864 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11865 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11866 &recov_state, needrecov); 11867 crfree(cred_otw); 11868 if (osp != NULL) 11869 open_stream_rele(osp, rp); 11870 return (0); 11871 } 11872 nfs4_set_mod(vp); 11873 rp->r_writeverf = cm_res->writeverf; 11874 mutex_exit(&rp->r_statelock); 11875 e.error = NFS_VERF_MISMATCH; 11876 } 11877 11878 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11879 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11880 crfree(cred_otw); 11881 if (osp != NULL) 11882 open_stream_rele(osp, rp); 11883 11884 return (e.error); 11885 } 11886 11887 static void 11888 nfs4_set_mod(vnode_t *vp) 11889 { 11890 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11891 11892 /* make sure we're looking at the master vnode, not a shadow */ 11893 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check); 11894 } 11895 11896 /* 11897 * This function is used to gather a page list of the pages which 11898 * can be committed on the server. 11899 * 11900 * The calling thread must have set R4COMMIT. This bit is used to 11901 * serialize access to the commit structure in the rnode. As long 11902 * as the thread has set R4COMMIT, then it can manipulate the commit 11903 * structure without requiring any other locks. 11904 * 11905 * When this function is called from nfs4_dispose() the page passed 11906 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11907 * will skip it. This is not a problem since we initially add the 11908 * page to the r_commit page list. 11909 * 11910 */ 11911 static void 11912 nfs4_get_commit(vnode_t *vp) 11913 { 11914 rnode4_t *rp; 11915 page_t *pp; 11916 kmutex_t *vphm; 11917 11918 rp = VTOR4(vp); 11919 11920 ASSERT(rp->r_flags & R4COMMIT); 11921 11922 /* make sure we're looking at the master vnode, not a shadow */ 11923 11924 if (IS_SHADOW(vp, rp)) 11925 vp = RTOV4(rp); 11926 11927 vphm = page_vnode_mutex(vp); 11928 mutex_enter(vphm); 11929 11930 /* 11931 * If there are no pages associated with this vnode, then 11932 * just return. 11933 */ 11934 if ((pp = vp->v_pages) == NULL) { 11935 mutex_exit(vphm); 11936 return; 11937 } 11938 11939 /* 11940 * Step through all of the pages associated with this vnode 11941 * looking for pages which need to be committed. 11942 */ 11943 do { 11944 /* Skip marker pages. */ 11945 if (pp->p_hash == PVN_VPLIST_HASH_TAG) 11946 continue; 11947 11948 /* 11949 * First short-cut everything (without the page_lock) 11950 * and see if this page does not need to be committed 11951 * or is modified if so then we'll just skip it. 11952 */ 11953 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11954 continue; 11955 11956 /* 11957 * Attempt to lock the page. If we can't, then 11958 * someone else is messing with it or we have been 11959 * called from nfs4_dispose and this is the page that 11960 * nfs4_dispose was called with.. anyway just skip it. 11961 */ 11962 if (!page_trylock(pp, SE_EXCL)) 11963 continue; 11964 11965 /* 11966 * Lets check again now that we have the page lock. 11967 */ 11968 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11969 page_unlock(pp); 11970 continue; 11971 } 11972 11973 /* this had better not be a free page */ 11974 ASSERT(PP_ISFREE(pp) == 0); 11975 11976 /* 11977 * The page needs to be committed and we locked it. 11978 * Update the base and length parameters and add it 11979 * to r_pages. 11980 */ 11981 if (rp->r_commit.c_pages == NULL) { 11982 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11983 rp->r_commit.c_commlen = PAGESIZE; 11984 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11985 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11986 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11987 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11988 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11989 <= pp->p_offset) { 11990 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11991 rp->r_commit.c_commbase + PAGESIZE; 11992 } 11993 page_add(&rp->r_commit.c_pages, pp); 11994 } while ((pp = pp->p_vpnext) != vp->v_pages); 11995 11996 mutex_exit(vphm); 11997 } 11998 11999 /* 12000 * This routine is used to gather together a page list of the pages 12001 * which are to be committed on the server. This routine must not 12002 * be called if the calling thread holds any locked pages. 12003 * 12004 * The calling thread must have set R4COMMIT. This bit is used to 12005 * serialize access to the commit structure in the rnode. As long 12006 * as the thread has set R4COMMIT, then it can manipulate the commit 12007 * structure without requiring any other locks. 12008 */ 12009 static void 12010 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 12011 { 12012 12013 rnode4_t *rp; 12014 page_t *pp; 12015 u_offset_t end; 12016 u_offset_t off; 12017 ASSERT(len != 0); 12018 rp = VTOR4(vp); 12019 ASSERT(rp->r_flags & R4COMMIT); 12020 12021 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12022 12023 /* make sure we're looking at the master vnode, not a shadow */ 12024 12025 if (IS_SHADOW(vp, rp)) 12026 vp = RTOV4(rp); 12027 12028 /* 12029 * If there are no pages associated with this vnode, then 12030 * just return. 12031 */ 12032 if ((pp = vp->v_pages) == NULL) 12033 return; 12034 /* 12035 * Calculate the ending offset. 12036 */ 12037 end = soff + len; 12038 for (off = soff; off < end; off += PAGESIZE) { 12039 /* 12040 * Lookup each page by vp, offset. 12041 */ 12042 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 12043 continue; 12044 /* 12045 * If this page does not need to be committed or is 12046 * modified, then just skip it. 12047 */ 12048 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 12049 page_unlock(pp); 12050 continue; 12051 } 12052 12053 ASSERT(PP_ISFREE(pp) == 0); 12054 /* 12055 * The page needs to be committed and we locked it. 12056 * Update the base and length parameters and add it 12057 * to r_pages. 12058 */ 12059 if (rp->r_commit.c_pages == NULL) { 12060 rp->r_commit.c_commbase = (offset3)pp->p_offset; 12061 rp->r_commit.c_commlen = PAGESIZE; 12062 } else { 12063 rp->r_commit.c_commlen = (offset3)pp->p_offset - 12064 rp->r_commit.c_commbase + PAGESIZE; 12065 } 12066 page_add(&rp->r_commit.c_pages, pp); 12067 } 12068 } 12069 12070 /* 12071 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 12072 * Flushes and commits data to the server. 12073 */ 12074 static int 12075 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 12076 { 12077 int error; 12078 verifier4 write_verf; 12079 rnode4_t *rp = VTOR4(vp); 12080 12081 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12082 12083 /* 12084 * Flush the data portion of the file and then commit any 12085 * portions which need to be committed. This may need to 12086 * be done twice if the server has changed state since 12087 * data was last written. The data will need to be 12088 * rewritten to the server and then a new commit done. 12089 * 12090 * In fact, this may need to be done several times if the 12091 * server is having problems and crashing while we are 12092 * attempting to do this. 12093 */ 12094 12095 top: 12096 /* 12097 * Do a flush based on the poff and plen arguments. This 12098 * will synchronously write out any modified pages in the 12099 * range specified by (poff, plen). This starts all of the 12100 * i/o operations which will be waited for in the next 12101 * call to nfs4_putpage 12102 */ 12103 12104 mutex_enter(&rp->r_statelock); 12105 write_verf = rp->r_writeverf; 12106 mutex_exit(&rp->r_statelock); 12107 12108 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL); 12109 if (error == EAGAIN) 12110 error = 0; 12111 12112 /* 12113 * Do a flush based on the poff and plen arguments. This 12114 * will synchronously write out any modified pages in the 12115 * range specified by (poff, plen) and wait until all of 12116 * the asynchronous i/o's in that range are done as well. 12117 */ 12118 if (!error) 12119 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL); 12120 12121 if (error) 12122 return (error); 12123 12124 mutex_enter(&rp->r_statelock); 12125 if (rp->r_writeverf != write_verf) { 12126 mutex_exit(&rp->r_statelock); 12127 goto top; 12128 } 12129 mutex_exit(&rp->r_statelock); 12130 12131 /* 12132 * Now commit any pages which might need to be committed. 12133 * If the error, NFS_VERF_MISMATCH, is returned, then 12134 * start over with the flush operation. 12135 */ 12136 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 12137 12138 if (error == NFS_VERF_MISMATCH) 12139 goto top; 12140 12141 return (error); 12142 } 12143 12144 /* 12145 * nfs4_commit_vp() will wait for other pending commits and 12146 * will either commit the whole file or a range, plen dictates 12147 * if we commit whole file. a value of zero indicates the whole 12148 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 12149 */ 12150 static int 12151 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 12152 cred_t *cr, int wait_on_writes) 12153 { 12154 rnode4_t *rp; 12155 page_t *plist; 12156 offset3 offset; 12157 count3 len; 12158 12159 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12160 12161 rp = VTOR4(vp); 12162 12163 /* 12164 * before we gather commitable pages make 12165 * sure there are no outstanding async writes 12166 */ 12167 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 12168 mutex_enter(&rp->r_statelock); 12169 while (rp->r_count > 0) { 12170 cv_wait(&rp->r_cv, &rp->r_statelock); 12171 } 12172 mutex_exit(&rp->r_statelock); 12173 } 12174 12175 /* 12176 * Set the `commit inprogress' state bit. We must 12177 * first wait until any current one finishes. 12178 */ 12179 mutex_enter(&rp->r_statelock); 12180 while (rp->r_flags & R4COMMIT) { 12181 rp->r_flags |= R4COMMITWAIT; 12182 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 12183 rp->r_flags &= ~R4COMMITWAIT; 12184 } 12185 rp->r_flags |= R4COMMIT; 12186 mutex_exit(&rp->r_statelock); 12187 12188 /* 12189 * Gather all of the pages which need to be 12190 * committed. 12191 */ 12192 if (plen == 0) 12193 nfs4_get_commit(vp); 12194 else 12195 nfs4_get_commit_range(vp, poff, plen); 12196 12197 /* 12198 * Clear the `commit inprogress' bit and disconnect the 12199 * page list which was gathered by nfs4_get_commit. 12200 */ 12201 plist = rp->r_commit.c_pages; 12202 rp->r_commit.c_pages = NULL; 12203 offset = rp->r_commit.c_commbase; 12204 len = rp->r_commit.c_commlen; 12205 mutex_enter(&rp->r_statelock); 12206 rp->r_flags &= ~R4COMMIT; 12207 cv_broadcast(&rp->r_commit.c_cv); 12208 mutex_exit(&rp->r_statelock); 12209 12210 /* 12211 * If any pages need to be committed, commit them and 12212 * then unlock them so that they can be freed some 12213 * time later. 12214 */ 12215 if (plist == NULL) 12216 return (0); 12217 12218 /* 12219 * No error occurred during the flush portion 12220 * of this operation, so now attempt to commit 12221 * the data to stable storage on the server. 12222 * 12223 * This will unlock all of the pages on the list. 12224 */ 12225 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12226 } 12227 12228 static int 12229 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12230 cred_t *cr) 12231 { 12232 int error; 12233 page_t *pp; 12234 12235 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12236 12237 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12238 12239 /* 12240 * If we got an error, then just unlock all of the pages 12241 * on the list. 12242 */ 12243 if (error) { 12244 while (plist != NULL) { 12245 pp = plist; 12246 page_sub(&plist, pp); 12247 page_unlock(pp); 12248 } 12249 return (error); 12250 } 12251 /* 12252 * We've tried as hard as we can to commit the data to stable 12253 * storage on the server. We just unlock the pages and clear 12254 * the commit required state. They will get freed later. 12255 */ 12256 while (plist != NULL) { 12257 pp = plist; 12258 page_sub(&plist, pp); 12259 pp->p_fsdata = C_NOCOMMIT; 12260 page_unlock(pp); 12261 } 12262 12263 return (error); 12264 } 12265 12266 static void 12267 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12268 cred_t *cr) 12269 { 12270 12271 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12272 } 12273 12274 /*ARGSUSED*/ 12275 static int 12276 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12277 caller_context_t *ct) 12278 { 12279 int error = 0; 12280 mntinfo4_t *mi; 12281 vattr_t va; 12282 vsecattr_t nfsace4_vsap; 12283 12284 mi = VTOMI4(vp); 12285 if (nfs_zone() != mi->mi_zone) 12286 return (EIO); 12287 if (mi->mi_flags & MI4_ACL) { 12288 /* if we have a delegation, return it */ 12289 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12290 (void) nfs4delegreturn(VTOR4(vp), 12291 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12292 12293 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12294 NFS4_ACL_SET); 12295 if (error) /* EINVAL */ 12296 return (error); 12297 12298 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12299 /* 12300 * These are aclent_t type entries. 12301 */ 12302 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12303 vp->v_type == VDIR, FALSE); 12304 if (error) 12305 return (error); 12306 } else { 12307 /* 12308 * These are ace_t type entries. 12309 */ 12310 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12311 FALSE); 12312 if (error) 12313 return (error); 12314 } 12315 bzero(&va, sizeof (va)); 12316 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12317 vs_ace4_destroy(&nfsace4_vsap); 12318 return (error); 12319 } 12320 return (ENOSYS); 12321 } 12322 12323 /* ARGSUSED */ 12324 int 12325 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12326 caller_context_t *ct) 12327 { 12328 int error; 12329 mntinfo4_t *mi; 12330 nfs4_ga_res_t gar; 12331 rnode4_t *rp = VTOR4(vp); 12332 12333 mi = VTOMI4(vp); 12334 if (nfs_zone() != mi->mi_zone) 12335 return (EIO); 12336 12337 bzero(&gar, sizeof (gar)); 12338 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12339 12340 /* 12341 * vsecattr->vsa_mask holds the original acl request mask. 12342 * This is needed when determining what to return. 12343 * (See: nfs4_create_getsecattr_return()) 12344 */ 12345 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12346 if (error) /* EINVAL */ 12347 return (error); 12348 12349 /* 12350 * If this is a referral stub, don't try to go OTW for an ACL 12351 */ 12352 if (RP_ISSTUB_REFERRAL(VTOR4(vp))) 12353 return (fs_fab_acl(vp, vsecattr, flag, cr, ct)); 12354 12355 if (mi->mi_flags & MI4_ACL) { 12356 /* 12357 * Check if the data is cached and the cache is valid. If it 12358 * is we don't go over the wire. 12359 */ 12360 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12361 mutex_enter(&rp->r_statelock); 12362 if (rp->r_secattr != NULL) { 12363 error = nfs4_create_getsecattr_return( 12364 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12365 rp->r_attr.va_gid, 12366 vp->v_type == VDIR); 12367 if (!error) { /* error == 0 - Success! */ 12368 mutex_exit(&rp->r_statelock); 12369 return (error); 12370 } 12371 } 12372 mutex_exit(&rp->r_statelock); 12373 } 12374 12375 /* 12376 * The getattr otw call will always get both the acl, in 12377 * the form of a list of nfsace4's, and the number of acl 12378 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12379 */ 12380 gar.n4g_va.va_mask = AT_ALL; 12381 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12382 if (error) { 12383 vs_ace4_destroy(&gar.n4g_vsa); 12384 if (error == ENOTSUP || error == EOPNOTSUPP) 12385 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12386 return (error); 12387 } 12388 12389 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12390 /* 12391 * No error was returned, but according to the response 12392 * bitmap, neither was an acl. 12393 */ 12394 vs_ace4_destroy(&gar.n4g_vsa); 12395 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12396 return (error); 12397 } 12398 12399 /* 12400 * Update the cache with the ACL. 12401 */ 12402 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12403 12404 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12405 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12406 vp->v_type == VDIR); 12407 vs_ace4_destroy(&gar.n4g_vsa); 12408 if ((error) && (vsecattr->vsa_mask & 12409 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12410 (error != EACCES)) { 12411 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12412 } 12413 return (error); 12414 } 12415 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12416 return (error); 12417 } 12418 12419 /* 12420 * The function returns: 12421 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12422 * - EINVAL if the passed in "acl_mask" is an invalid request. 12423 * 12424 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12425 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12426 * 12427 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12428 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12429 * - We have a count field set without the corresponding acl field set. (e.g. - 12430 * VSA_ACECNT is set, but VSA_ACE is not) 12431 */ 12432 static int 12433 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12434 { 12435 /* Shortcut the masks that are always valid. */ 12436 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12437 return (0); 12438 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12439 return (0); 12440 12441 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12442 /* 12443 * We can't have any VSA_ACL type stuff in the mask now. 12444 */ 12445 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12446 VSA_DFACLCNT)) 12447 return (EINVAL); 12448 12449 if (op == NFS4_ACL_SET) { 12450 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12451 return (EINVAL); 12452 } 12453 } 12454 12455 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12456 /* 12457 * We can't have any VSA_ACE type stuff in the mask now. 12458 */ 12459 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12460 return (EINVAL); 12461 12462 if (op == NFS4_ACL_SET) { 12463 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12464 return (EINVAL); 12465 12466 if ((acl_mask & VSA_DFACLCNT) && 12467 !(acl_mask & VSA_DFACL)) 12468 return (EINVAL); 12469 } 12470 } 12471 return (0); 12472 } 12473 12474 /* 12475 * The theory behind creating the correct getsecattr return is simply this: 12476 * "Don't return anything that the caller is not expecting to have to free." 12477 */ 12478 static int 12479 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12480 uid_t uid, gid_t gid, int isdir) 12481 { 12482 int error = 0; 12483 /* Save the mask since the translators modify it. */ 12484 uint_t orig_mask = vsap->vsa_mask; 12485 12486 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12487 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE); 12488 12489 if (error) 12490 return (error); 12491 12492 /* 12493 * If the caller only asked for the ace count (VSA_ACECNT) 12494 * don't give them the full acl (VSA_ACE), free it. 12495 */ 12496 if (!orig_mask & VSA_ACE) { 12497 if (vsap->vsa_aclentp != NULL) { 12498 kmem_free(vsap->vsa_aclentp, 12499 vsap->vsa_aclcnt * sizeof (ace_t)); 12500 vsap->vsa_aclentp = NULL; 12501 } 12502 } 12503 vsap->vsa_mask = orig_mask; 12504 12505 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12506 VSA_DFACLCNT)) { 12507 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12508 isdir, FALSE); 12509 12510 if (error) 12511 return (error); 12512 12513 /* 12514 * If the caller only asked for the acl count (VSA_ACLCNT) 12515 * and/or the default acl count (VSA_DFACLCNT) don't give them 12516 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12517 */ 12518 if (!orig_mask & VSA_ACL) { 12519 if (vsap->vsa_aclentp != NULL) { 12520 kmem_free(vsap->vsa_aclentp, 12521 vsap->vsa_aclcnt * sizeof (aclent_t)); 12522 vsap->vsa_aclentp = NULL; 12523 } 12524 } 12525 12526 if (!orig_mask & VSA_DFACL) { 12527 if (vsap->vsa_dfaclentp != NULL) { 12528 kmem_free(vsap->vsa_dfaclentp, 12529 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12530 vsap->vsa_dfaclentp = NULL; 12531 } 12532 } 12533 vsap->vsa_mask = orig_mask; 12534 } 12535 return (0); 12536 } 12537 12538 /* ARGSUSED */ 12539 int 12540 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr, 12541 caller_context_t *ct) 12542 { 12543 int error; 12544 12545 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12546 return (EIO); 12547 /* 12548 * check for valid cmd parameter 12549 */ 12550 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12551 return (EINVAL); 12552 12553 /* 12554 * Check access permissions 12555 */ 12556 if ((cmd & F_SHARE) && 12557 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12558 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12559 return (EBADF); 12560 12561 /* 12562 * If the filesystem is mounted using local locking, pass the 12563 * request off to the local share code. 12564 */ 12565 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12566 return (fs_shrlock(vp, cmd, shr, flag, cr, ct)); 12567 12568 switch (cmd) { 12569 case F_SHARE: 12570 case F_UNSHARE: 12571 /* 12572 * This will be properly implemented later, 12573 * see RFE: 4823948 . 12574 */ 12575 error = EAGAIN; 12576 break; 12577 12578 case F_HASREMOTELOCKS: 12579 /* 12580 * NFS client can't store remote locks itself 12581 */ 12582 shr->s_access = 0; 12583 error = 0; 12584 break; 12585 12586 default: 12587 error = EINVAL; 12588 break; 12589 } 12590 12591 return (error); 12592 } 12593 12594 /* 12595 * Common code called by directory ops to update the attrcache 12596 */ 12597 static int 12598 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12599 hrtime_t t, vnode_t *vp, cred_t *cr) 12600 { 12601 int error = 0; 12602 12603 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12604 12605 if (status != NFS4_OK) { 12606 /* getattr not done or failed */ 12607 PURGE_ATTRCACHE4(vp); 12608 return (error); 12609 } 12610 12611 if (garp) { 12612 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12613 } else { 12614 PURGE_ATTRCACHE4(vp); 12615 } 12616 return (error); 12617 } 12618 12619 /* 12620 * Update directory caches for directory modification ops (link, rename, etc.) 12621 * When dinfo is NULL, manage dircaches in the old way. 12622 */ 12623 static void 12624 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12625 dirattr_info_t *dinfo) 12626 { 12627 rnode4_t *drp = VTOR4(dvp); 12628 12629 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12630 12631 /* Purge rddir cache for dir since it changed */ 12632 if (drp->r_dir != NULL) 12633 nfs4_purge_rddir_cache(dvp); 12634 12635 /* 12636 * If caller provided dinfo, then use it to manage dir caches. 12637 */ 12638 if (dinfo != NULL) { 12639 if (vp != NULL) { 12640 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12641 if (!VTOR4(vp)->created_v4) { 12642 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12643 dnlc_update(dvp, nm, vp); 12644 } else { 12645 /* 12646 * XXX don't update if the created_v4 flag is 12647 * set 12648 */ 12649 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12650 NFS4_DEBUG(nfs4_client_state_debug, 12651 (CE_NOTE, "nfs4_update_dircaches: " 12652 "don't update dnlc: created_v4 flag")); 12653 } 12654 } 12655 12656 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12657 dinfo->di_cred, FALSE, cinfo); 12658 12659 return; 12660 } 12661 12662 /* 12663 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12664 * Since caller modified dir but didn't receive post-dirmod-op dir 12665 * attrs, the dir's attrs must be purged. 12666 * 12667 * XXX this check and dnlc update/purge should really be atomic, 12668 * XXX but can't use rnode statelock because it'll deadlock in 12669 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12670 * XXX does occur. 12671 * 12672 * XXX We also may want to check that atomic is true in the 12673 * XXX change_info struct. If it is not, the change_info may 12674 * XXX reflect changes by more than one clients which means that 12675 * XXX our cache may not be valid. 12676 */ 12677 PURGE_ATTRCACHE4(dvp); 12678 if (drp->r_change == cinfo->before) { 12679 /* no changes took place in the directory prior to our link */ 12680 if (vp != NULL) { 12681 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12682 if (!VTOR4(vp)->created_v4) { 12683 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12684 dnlc_update(dvp, nm, vp); 12685 } else { 12686 /* 12687 * XXX dont' update if the created_v4 flag 12688 * is set 12689 */ 12690 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12691 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12692 "nfs4_update_dircaches: don't" 12693 " update dnlc: created_v4 flag")); 12694 } 12695 } 12696 } else { 12697 /* Another client modified directory - purge its dnlc cache */ 12698 dnlc_purge_vp(dvp); 12699 } 12700 } 12701 12702 /* 12703 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12704 * file. 12705 * 12706 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12707 * file (ie: client recovery) and otherwise set to FALSE. 12708 * 12709 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12710 * initiated) calling functions. 12711 * 12712 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12713 * of resending a 'lost' open request. 12714 * 12715 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12716 * server that hands out BAD_SEQID on open confirm. 12717 * 12718 * Errors are returned via the nfs4_error_t parameter. 12719 */ 12720 void 12721 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12722 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12723 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12724 { 12725 COMPOUND4args_clnt args; 12726 COMPOUND4res_clnt res; 12727 nfs_argop4 argop[2]; 12728 nfs_resop4 *resop; 12729 int doqueue = 1; 12730 mntinfo4_t *mi; 12731 OPEN_CONFIRM4args *open_confirm_args; 12732 int needrecov; 12733 12734 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12735 #if DEBUG 12736 mutex_enter(&oop->oo_lock); 12737 ASSERT(oop->oo_seqid_inuse); 12738 mutex_exit(&oop->oo_lock); 12739 #endif 12740 12741 recov_retry_confirm: 12742 nfs4_error_zinit(ep); 12743 *retry_open = FALSE; 12744 12745 if (resend) 12746 args.ctag = TAG_OPEN_CONFIRM_LOST; 12747 else 12748 args.ctag = TAG_OPEN_CONFIRM; 12749 12750 args.array_len = 2; 12751 args.array = argop; 12752 12753 /* putfh target fh */ 12754 argop[0].argop = OP_CPUTFH; 12755 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12756 12757 argop[1].argop = OP_OPEN_CONFIRM; 12758 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12759 12760 (*seqid) += 1; 12761 open_confirm_args->seqid = *seqid; 12762 open_confirm_args->open_stateid = *stateid; 12763 12764 mi = VTOMI4(vp); 12765 12766 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12767 12768 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12769 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12770 } 12771 12772 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12773 if (!needrecov && ep->error) 12774 return; 12775 12776 if (needrecov) { 12777 bool_t abort = FALSE; 12778 12779 if (reopening_file == FALSE) { 12780 nfs4_bseqid_entry_t *bsep = NULL; 12781 12782 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12783 bsep = nfs4_create_bseqid_entry(oop, NULL, 12784 vp, 0, args.ctag, 12785 open_confirm_args->seqid); 12786 12787 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 12788 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL); 12789 if (bsep) { 12790 kmem_free(bsep, sizeof (*bsep)); 12791 if (num_bseqid_retryp && 12792 --(*num_bseqid_retryp) == 0) 12793 abort = TRUE; 12794 } 12795 } 12796 if ((ep->error == ETIMEDOUT || 12797 res.status == NFS4ERR_RESOURCE) && 12798 abort == FALSE && resend == FALSE) { 12799 if (!ep->error) 12800 (void) xdr_free(xdr_COMPOUND4res_clnt, 12801 (caddr_t)&res); 12802 12803 delay(SEC_TO_TICK(confirm_retry_sec)); 12804 goto recov_retry_confirm; 12805 } 12806 /* State may have changed so retry the entire OPEN op */ 12807 if (abort == FALSE) 12808 *retry_open = TRUE; 12809 else 12810 *retry_open = FALSE; 12811 if (!ep->error) 12812 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12813 return; 12814 } 12815 12816 if (res.status) { 12817 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12818 return; 12819 } 12820 12821 resop = &res.array[1]; /* open confirm res */ 12822 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12823 stateid, sizeof (*stateid)); 12824 12825 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12826 } 12827 12828 /* 12829 * Return the credentials associated with a client state object. The 12830 * caller is responsible for freeing the credentials. 12831 */ 12832 12833 static cred_t * 12834 state_to_cred(nfs4_open_stream_t *osp) 12835 { 12836 cred_t *cr; 12837 12838 /* 12839 * It's ok to not lock the open stream and open owner to get 12840 * the oo_cred since this is only written once (upon creation) 12841 * and will not change. 12842 */ 12843 cr = osp->os_open_owner->oo_cred; 12844 crhold(cr); 12845 12846 return (cr); 12847 } 12848 12849 /* 12850 * nfs4_find_sysid 12851 * 12852 * Find the sysid for the knetconfig associated with the given mi. 12853 */ 12854 static struct lm_sysid * 12855 nfs4_find_sysid(mntinfo4_t *mi) 12856 { 12857 ASSERT(nfs_zone() == mi->mi_zone); 12858 12859 /* 12860 * Switch from RDMA knconf to original mount knconf 12861 */ 12862 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12863 mi->mi_curr_serv->sv_hostname, NULL)); 12864 } 12865 12866 #ifdef DEBUG 12867 /* 12868 * Return a string version of the call type for easy reading. 12869 */ 12870 static char * 12871 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12872 { 12873 switch (ctype) { 12874 case NFS4_LCK_CTYPE_NORM: 12875 return ("NORMAL"); 12876 case NFS4_LCK_CTYPE_RECLAIM: 12877 return ("RECLAIM"); 12878 case NFS4_LCK_CTYPE_RESEND: 12879 return ("RESEND"); 12880 case NFS4_LCK_CTYPE_REINSTATE: 12881 return ("REINSTATE"); 12882 default: 12883 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12884 "type %d", ctype); 12885 return (""); 12886 } 12887 } 12888 #endif 12889 12890 /* 12891 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12892 * Unlock requests don't have an over-the-wire locktype, so we just return 12893 * something non-threatening. 12894 */ 12895 12896 static nfs_lock_type4 12897 flk_to_locktype(int cmd, int l_type) 12898 { 12899 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12900 12901 switch (l_type) { 12902 case F_UNLCK: 12903 return (READ_LT); 12904 case F_RDLCK: 12905 if (cmd == F_SETLK) 12906 return (READ_LT); 12907 else 12908 return (READW_LT); 12909 case F_WRLCK: 12910 if (cmd == F_SETLK) 12911 return (WRITE_LT); 12912 else 12913 return (WRITEW_LT); 12914 } 12915 panic("flk_to_locktype"); 12916 /*NOTREACHED*/ 12917 } 12918 12919 /* 12920 * Do some preliminary checks for nfs4frlock. 12921 */ 12922 static int 12923 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12924 u_offset_t offset) 12925 { 12926 int error = 0; 12927 12928 /* 12929 * If we are setting a lock, check that the file is opened 12930 * with the correct mode. 12931 */ 12932 if (cmd == F_SETLK || cmd == F_SETLKW) { 12933 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12934 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12935 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12936 "nfs4frlock_validate_args: file was opened with " 12937 "incorrect mode")); 12938 return (EBADF); 12939 } 12940 } 12941 12942 /* Convert the offset. It may need to be restored before returning. */ 12943 if (error = convoff(vp, flk, 0, offset)) { 12944 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12945 "nfs4frlock_validate_args: convoff => error= %d\n", 12946 error)); 12947 return (error); 12948 } 12949 12950 return (error); 12951 } 12952 12953 /* 12954 * Set the flock64's lm_sysid for nfs4frlock. 12955 */ 12956 static int 12957 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12958 { 12959 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12960 12961 /* Find the lm_sysid */ 12962 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12963 12964 if (*lspp == NULL) { 12965 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12966 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12967 return (ENOLCK); 12968 } 12969 12970 flk->l_sysid = lm_sysidt(*lspp); 12971 12972 return (0); 12973 } 12974 12975 /* 12976 * Do the remaining preliminary setup for nfs4frlock. 12977 */ 12978 static void 12979 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12980 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12981 cred_t **cred_otw) 12982 { 12983 /* 12984 * set tick_delay to the base delay time. 12985 * (nfs4_base_wait_time is in msecs) 12986 */ 12987 12988 *tick_delayp = drv_usectohz(nfs4_base_wait_time * 1000); 12989 12990 /* 12991 * If lock is relative to EOF, we need the newest length of the 12992 * file. Therefore invalidate the ATTR_CACHE. 12993 */ 12994 12995 *whencep = flk->l_whence; 12996 12997 if (*whencep == 2) /* SEEK_END */ 12998 PURGE_ATTRCACHE4(vp); 12999 13000 recov_statep->rs_flags = 0; 13001 recov_statep->rs_num_retry_despite_err = 0; 13002 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 13003 } 13004 13005 /* 13006 * Initialize and allocate the data structures necessary for 13007 * the nfs4frlock call. 13008 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 13009 */ 13010 static void 13011 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 13012 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 13013 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 13014 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 13015 { 13016 int argoplist_size; 13017 int num_ops = 2; 13018 13019 *retry = FALSE; 13020 *did_start_fop = FALSE; 13021 *skip_get_err = FALSE; 13022 lost_rqstp->lr_op = 0; 13023 argoplist_size = num_ops * sizeof (nfs_argop4); 13024 /* fill array with zero */ 13025 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 13026 13027 *argspp = argsp; 13028 *respp = NULL; 13029 13030 argsp->array_len = num_ops; 13031 argsp->array = *argopp; 13032 13033 /* initialize in case of error; will get real value down below */ 13034 argsp->ctag = TAG_NONE; 13035 13036 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 13037 *op_hintp = OH_LOCKU; 13038 else 13039 *op_hintp = OH_OTHER; 13040 } 13041 13042 /* 13043 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 13044 * the proper nfs4_server_t for this instance of nfs4frlock. 13045 * Returns 0 (success) or an errno value. 13046 */ 13047 static int 13048 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 13049 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 13050 bool_t *did_start_fop, bool_t *startrecovp) 13051 { 13052 int error = 0; 13053 rnode4_t *rp; 13054 13055 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13056 13057 if (ctype == NFS4_LCK_CTYPE_NORM) { 13058 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 13059 recov_statep, startrecovp); 13060 if (error) 13061 return (error); 13062 *did_start_fop = TRUE; 13063 } else { 13064 *did_start_fop = FALSE; 13065 *startrecovp = FALSE; 13066 } 13067 13068 if (!error) { 13069 rp = VTOR4(vp); 13070 13071 /* If the file failed recovery, just quit. */ 13072 mutex_enter(&rp->r_statelock); 13073 if (rp->r_flags & R4RECOVERR) { 13074 error = EIO; 13075 } 13076 mutex_exit(&rp->r_statelock); 13077 } 13078 13079 return (error); 13080 } 13081 13082 /* 13083 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 13084 * resend nfs4frlock call is initiated by the recovery framework. 13085 * Acquires the lop and oop seqid synchronization. 13086 */ 13087 static void 13088 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 13089 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 13090 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13091 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 13092 { 13093 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 13094 int error; 13095 13096 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 13097 (CE_NOTE, 13098 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 13099 ASSERT(resend_rqstp != NULL); 13100 ASSERT(resend_rqstp->lr_op == OP_LOCK || 13101 resend_rqstp->lr_op == OP_LOCKU); 13102 13103 *oopp = resend_rqstp->lr_oop; 13104 if (resend_rqstp->lr_oop) { 13105 open_owner_hold(resend_rqstp->lr_oop); 13106 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 13107 ASSERT(error == 0); /* recov thread always succeeds */ 13108 } 13109 13110 /* Must resend this lost lock/locku request. */ 13111 ASSERT(resend_rqstp->lr_lop != NULL); 13112 *lopp = resend_rqstp->lr_lop; 13113 lock_owner_hold(resend_rqstp->lr_lop); 13114 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 13115 ASSERT(error == 0); /* recov thread always succeeds */ 13116 13117 *ospp = resend_rqstp->lr_osp; 13118 if (*ospp) 13119 open_stream_hold(resend_rqstp->lr_osp); 13120 13121 if (resend_rqstp->lr_op == OP_LOCK) { 13122 LOCK4args *lock_args; 13123 13124 argop->argop = OP_LOCK; 13125 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 13126 lock_args->locktype = resend_rqstp->lr_locktype; 13127 lock_args->reclaim = 13128 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 13129 lock_args->offset = resend_rqstp->lr_flk->l_start; 13130 lock_args->length = resend_rqstp->lr_flk->l_len; 13131 if (lock_args->length == 0) 13132 lock_args->length = ~lock_args->length; 13133 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 13134 mi2clientid(mi), &lock_args->locker); 13135 13136 switch (resend_rqstp->lr_ctype) { 13137 case NFS4_LCK_CTYPE_RESEND: 13138 argsp->ctag = TAG_LOCK_RESEND; 13139 break; 13140 case NFS4_LCK_CTYPE_REINSTATE: 13141 argsp->ctag = TAG_LOCK_REINSTATE; 13142 break; 13143 case NFS4_LCK_CTYPE_RECLAIM: 13144 argsp->ctag = TAG_LOCK_RECLAIM; 13145 break; 13146 default: 13147 argsp->ctag = TAG_LOCK_UNKNOWN; 13148 break; 13149 } 13150 } else { 13151 LOCKU4args *locku_args; 13152 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 13153 13154 argop->argop = OP_LOCKU; 13155 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 13156 locku_args->locktype = READ_LT; 13157 locku_args->seqid = lop->lock_seqid + 1; 13158 mutex_enter(&lop->lo_lock); 13159 locku_args->lock_stateid = lop->lock_stateid; 13160 mutex_exit(&lop->lo_lock); 13161 locku_args->offset = resend_rqstp->lr_flk->l_start; 13162 locku_args->length = resend_rqstp->lr_flk->l_len; 13163 if (locku_args->length == 0) 13164 locku_args->length = ~locku_args->length; 13165 13166 switch (resend_rqstp->lr_ctype) { 13167 case NFS4_LCK_CTYPE_RESEND: 13168 argsp->ctag = TAG_LOCKU_RESEND; 13169 break; 13170 case NFS4_LCK_CTYPE_REINSTATE: 13171 argsp->ctag = TAG_LOCKU_REINSTATE; 13172 break; 13173 default: 13174 argsp->ctag = TAG_LOCK_UNKNOWN; 13175 break; 13176 } 13177 } 13178 } 13179 13180 /* 13181 * Setup the LOCKT4 arguments. 13182 */ 13183 static void 13184 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13185 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 13186 rnode4_t *rp) 13187 { 13188 LOCKT4args *lockt_args; 13189 13190 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 13191 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13192 argop->argop = OP_LOCKT; 13193 argsp->ctag = TAG_LOCKT; 13194 lockt_args = &argop->nfs_argop4_u.oplockt; 13195 13196 /* 13197 * The locktype will be READ_LT unless it's 13198 * a write lock. We do this because the Solaris 13199 * system call allows the combination of 13200 * F_UNLCK and F_GETLK* and so in that case the 13201 * unlock is mapped to a read. 13202 */ 13203 if (flk->l_type == F_WRLCK) 13204 lockt_args->locktype = WRITE_LT; 13205 else 13206 lockt_args->locktype = READ_LT; 13207 13208 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 13209 /* set the lock owner4 args */ 13210 nfs4_setlockowner_args(&lockt_args->owner, rp, 13211 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13212 flk->l_pid); 13213 lockt_args->offset = flk->l_start; 13214 lockt_args->length = flk->l_len; 13215 if (flk->l_len == 0) 13216 lockt_args->length = ~lockt_args->length; 13217 13218 *lockt_argsp = lockt_args; 13219 } 13220 13221 /* 13222 * If the client is holding a delegation, and the open stream to be used 13223 * with this lock request is a delegation open stream, then re-open the stream. 13224 * Sets the nfs4_error_t to all zeros unless the open stream has already 13225 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13226 * means the caller should retry (like a recovery retry). 13227 */ 13228 static void 13229 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13230 { 13231 open_delegation_type4 dt; 13232 bool_t reopen_needed, force; 13233 nfs4_open_stream_t *osp; 13234 open_claim_type4 oclaim; 13235 rnode4_t *rp = VTOR4(vp); 13236 mntinfo4_t *mi = VTOMI4(vp); 13237 13238 ASSERT(nfs_zone() == mi->mi_zone); 13239 13240 nfs4_error_zinit(ep); 13241 13242 mutex_enter(&rp->r_statev4_lock); 13243 dt = rp->r_deleg_type; 13244 mutex_exit(&rp->r_statev4_lock); 13245 13246 if (dt != OPEN_DELEGATE_NONE) { 13247 nfs4_open_owner_t *oop; 13248 13249 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13250 if (!oop) { 13251 ep->stat = NFS4ERR_IO; 13252 return; 13253 } 13254 /* returns with 'os_sync_lock' held */ 13255 osp = find_open_stream(oop, rp); 13256 if (!osp) { 13257 open_owner_rele(oop); 13258 ep->stat = NFS4ERR_IO; 13259 return; 13260 } 13261 13262 if (osp->os_failed_reopen) { 13263 NFS4_DEBUG((nfs4_open_stream_debug || 13264 nfs4_client_lock_debug), (CE_NOTE, 13265 "nfs4frlock_check_deleg: os_failed_reopen set " 13266 "for osp %p, cr %p, rp %s", (void *)osp, 13267 (void *)cr, rnode4info(rp))); 13268 mutex_exit(&osp->os_sync_lock); 13269 open_stream_rele(osp, rp); 13270 open_owner_rele(oop); 13271 ep->stat = NFS4ERR_IO; 13272 return; 13273 } 13274 13275 /* 13276 * Determine whether a reopen is needed. If this 13277 * is a delegation open stream, then send the open 13278 * to the server to give visibility to the open owner. 13279 * Even if it isn't a delegation open stream, we need 13280 * to check if the previous open CLAIM_DELEGATE_CUR 13281 * was sufficient. 13282 */ 13283 13284 reopen_needed = osp->os_delegation || 13285 ((lt == F_RDLCK && 13286 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13287 (lt == F_WRLCK && 13288 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13289 13290 mutex_exit(&osp->os_sync_lock); 13291 open_owner_rele(oop); 13292 13293 if (reopen_needed) { 13294 /* 13295 * Always use CLAIM_PREVIOUS after server reboot. 13296 * The server will reject CLAIM_DELEGATE_CUR if 13297 * it is used during the grace period. 13298 */ 13299 mutex_enter(&mi->mi_lock); 13300 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13301 oclaim = CLAIM_PREVIOUS; 13302 force = TRUE; 13303 } else { 13304 oclaim = CLAIM_DELEGATE_CUR; 13305 force = FALSE; 13306 } 13307 mutex_exit(&mi->mi_lock); 13308 13309 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13310 if (ep->error == EAGAIN) { 13311 nfs4_error_zinit(ep); 13312 ep->stat = NFS4ERR_DELAY; 13313 } 13314 } 13315 open_stream_rele(osp, rp); 13316 osp = NULL; 13317 } 13318 } 13319 13320 /* 13321 * Setup the LOCKU4 arguments. 13322 * Returns errors via the nfs4_error_t. 13323 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13324 * over-the-wire. The caller must release the 13325 * reference on *lopp. 13326 * NFS4ERR_DELAY caller should retry (like recovery retry) 13327 * (other) unrecoverable error. 13328 */ 13329 static void 13330 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13331 LOCKU4args **locku_argsp, flock64_t *flk, 13332 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13333 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13334 bool_t *skip_get_err, bool_t *go_otwp) 13335 { 13336 nfs4_lock_owner_t *lop = NULL; 13337 LOCKU4args *locku_args; 13338 pid_t pid; 13339 bool_t is_spec = FALSE; 13340 rnode4_t *rp = VTOR4(vp); 13341 13342 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13343 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13344 13345 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13346 if (ep->error || ep->stat) 13347 return; 13348 13349 argop->argop = OP_LOCKU; 13350 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13351 argsp->ctag = TAG_LOCKU_REINSTATE; 13352 else 13353 argsp->ctag = TAG_LOCKU; 13354 locku_args = &argop->nfs_argop4_u.oplocku; 13355 *locku_argsp = locku_args; 13356 13357 /* 13358 * XXX what should locku_args->locktype be? 13359 * setting to ALWAYS be READ_LT so at least 13360 * it is a valid locktype. 13361 */ 13362 13363 locku_args->locktype = READ_LT; 13364 13365 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13366 flk->l_pid; 13367 13368 /* 13369 * Get the lock owner stateid. If no lock owner 13370 * exists, return success. 13371 */ 13372 lop = find_lock_owner(rp, pid, LOWN_ANY); 13373 *lopp = lop; 13374 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13375 is_spec = TRUE; 13376 if (!lop || is_spec) { 13377 /* 13378 * No lock owner so no locks to unlock. 13379 * Return success. If there was a failed 13380 * reclaim earlier, the lock might still be 13381 * registered with the local locking code, 13382 * so notify it of the unlock. 13383 * 13384 * If the lockowner is using a special stateid, 13385 * then the original lock request (that created 13386 * this lockowner) was never successful, so we 13387 * have no lock to undo OTW. 13388 */ 13389 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13390 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13391 "(%ld) so return success", (long)pid)); 13392 13393 if (ctype == NFS4_LCK_CTYPE_NORM) 13394 flk->l_pid = curproc->p_pid; 13395 nfs4_register_lock_locally(vp, flk, flag, offset); 13396 /* 13397 * Release our hold and NULL out so final_cleanup 13398 * doesn't try to end a lock seqid sync we 13399 * never started. 13400 */ 13401 if (is_spec) { 13402 lock_owner_rele(lop); 13403 *lopp = NULL; 13404 } 13405 *skip_get_err = TRUE; 13406 *go_otwp = FALSE; 13407 return; 13408 } 13409 13410 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13411 if (ep->error == EAGAIN) { 13412 lock_owner_rele(lop); 13413 *lopp = NULL; 13414 return; 13415 } 13416 13417 mutex_enter(&lop->lo_lock); 13418 locku_args->lock_stateid = lop->lock_stateid; 13419 mutex_exit(&lop->lo_lock); 13420 locku_args->seqid = lop->lock_seqid + 1; 13421 13422 /* leave the ref count on lop, rele after RPC call */ 13423 13424 locku_args->offset = flk->l_start; 13425 locku_args->length = flk->l_len; 13426 if (flk->l_len == 0) 13427 locku_args->length = ~locku_args->length; 13428 13429 *go_otwp = TRUE; 13430 } 13431 13432 /* 13433 * Setup the LOCK4 arguments. 13434 * 13435 * Returns errors via the nfs4_error_t. 13436 * NFS4_OK no problems 13437 * NFS4ERR_DELAY caller should retry (like recovery retry) 13438 * (other) unrecoverable error 13439 */ 13440 static void 13441 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13442 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13443 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13444 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13445 { 13446 LOCK4args *lock_args; 13447 nfs4_open_owner_t *oop = NULL; 13448 nfs4_open_stream_t *osp = NULL; 13449 nfs4_lock_owner_t *lop = NULL; 13450 pid_t pid; 13451 rnode4_t *rp = VTOR4(vp); 13452 13453 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13454 13455 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13456 if (ep->error || ep->stat != NFS4_OK) 13457 return; 13458 13459 argop->argop = OP_LOCK; 13460 if (ctype == NFS4_LCK_CTYPE_NORM) 13461 argsp->ctag = TAG_LOCK; 13462 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13463 argsp->ctag = TAG_RELOCK; 13464 else 13465 argsp->ctag = TAG_LOCK_REINSTATE; 13466 lock_args = &argop->nfs_argop4_u.oplock; 13467 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13468 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13469 /* 13470 * Get the lock owner. If no lock owner exists, 13471 * create a 'temporary' one and grab the open seqid 13472 * synchronization (which puts a hold on the open 13473 * owner and open stream). 13474 * This also grabs the lock seqid synchronization. 13475 */ 13476 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13477 ep->stat = 13478 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13479 13480 if (ep->stat != NFS4_OK) 13481 goto out; 13482 13483 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13484 &lock_args->locker); 13485 13486 lock_args->offset = flk->l_start; 13487 lock_args->length = flk->l_len; 13488 if (flk->l_len == 0) 13489 lock_args->length = ~lock_args->length; 13490 *lock_argsp = lock_args; 13491 out: 13492 *oopp = oop; 13493 *ospp = osp; 13494 *lopp = lop; 13495 } 13496 13497 /* 13498 * After we get the reply from the server, record the proper information 13499 * for possible resend lock requests. 13500 * 13501 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13502 */ 13503 static void 13504 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13505 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13506 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13507 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13508 { 13509 bool_t unlock = (flk->l_type == F_UNLCK); 13510 13511 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13512 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13513 ctype == NFS4_LCK_CTYPE_REINSTATE); 13514 13515 if (error != 0 && !unlock) { 13516 NFS4_DEBUG((nfs4_lost_rqst_debug || 13517 nfs4_client_lock_debug), (CE_NOTE, 13518 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13519 " for lop %p", (void *)lop)); 13520 ASSERT(lop != NULL); 13521 mutex_enter(&lop->lo_lock); 13522 lop->lo_pending_rqsts = 1; 13523 mutex_exit(&lop->lo_lock); 13524 } 13525 13526 lost_rqstp->lr_putfirst = FALSE; 13527 lost_rqstp->lr_op = 0; 13528 13529 /* 13530 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13531 * recovery purposes so that the lock request that was sent 13532 * can be saved and re-issued later. Ditto for EIO from a forced 13533 * unmount. This is done to have the client's local locking state 13534 * match the v4 server's state; that is, the request was 13535 * potentially received and accepted by the server but the client 13536 * thinks it was not. 13537 */ 13538 if (error == ETIMEDOUT || error == EINTR || 13539 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13540 NFS4_DEBUG((nfs4_lost_rqst_debug || 13541 nfs4_client_lock_debug), (CE_NOTE, 13542 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13543 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13544 (void *)lop, (void *)oop, (void *)osp)); 13545 if (unlock) 13546 lost_rqstp->lr_op = OP_LOCKU; 13547 else { 13548 lost_rqstp->lr_op = OP_LOCK; 13549 lost_rqstp->lr_locktype = locktype; 13550 } 13551 /* 13552 * Objects are held and rele'd via the recovery code. 13553 * See nfs4_save_lost_rqst. 13554 */ 13555 lost_rqstp->lr_vp = vp; 13556 lost_rqstp->lr_dvp = NULL; 13557 lost_rqstp->lr_oop = oop; 13558 lost_rqstp->lr_osp = osp; 13559 lost_rqstp->lr_lop = lop; 13560 lost_rqstp->lr_cr = cr; 13561 switch (ctype) { 13562 case NFS4_LCK_CTYPE_NORM: 13563 flk->l_pid = ttoproc(curthread)->p_pid; 13564 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13565 break; 13566 case NFS4_LCK_CTYPE_REINSTATE: 13567 lost_rqstp->lr_putfirst = TRUE; 13568 lost_rqstp->lr_ctype = ctype; 13569 break; 13570 default: 13571 break; 13572 } 13573 lost_rqstp->lr_flk = flk; 13574 } 13575 } 13576 13577 /* 13578 * Update lop's seqid. Also update the seqid stored in a resend request, 13579 * if any. (Some recovery errors increment the seqid, and we may have to 13580 * send the resend request again.) 13581 */ 13582 13583 static void 13584 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13585 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13586 { 13587 if (lock_args) { 13588 if (lock_args->locker.new_lock_owner == TRUE) 13589 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13590 else { 13591 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13592 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13593 } 13594 } else if (locku_args) { 13595 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13596 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13597 } 13598 } 13599 13600 /* 13601 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13602 * COMPOUND4 args/res for calls that need to retry. 13603 * Switches the *cred_otwp to base_cr. 13604 */ 13605 static void 13606 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13607 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13608 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13609 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13610 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13611 { 13612 nfs4_open_owner_t *oop = *oopp; 13613 nfs4_open_stream_t *osp = *ospp; 13614 nfs4_lock_owner_t *lop = *lopp; 13615 nfs_argop4 *argop = (*argspp)->array; 13616 13617 if (*did_start_fop) { 13618 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13619 needrecov); 13620 *did_start_fop = FALSE; 13621 } 13622 ASSERT((*argspp)->array_len == 2); 13623 if (argop[1].argop == OP_LOCK) 13624 nfs4args_lock_free(&argop[1]); 13625 else if (argop[1].argop == OP_LOCKT) 13626 nfs4args_lockt_free(&argop[1]); 13627 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13628 if (!error) 13629 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13630 *argspp = NULL; 13631 *respp = NULL; 13632 13633 if (lop) { 13634 nfs4_end_lock_seqid_sync(lop); 13635 lock_owner_rele(lop); 13636 *lopp = NULL; 13637 } 13638 13639 /* need to free up the reference on osp for lock args */ 13640 if (osp != NULL) { 13641 open_stream_rele(osp, VTOR4(vp)); 13642 *ospp = NULL; 13643 } 13644 13645 /* need to free up the reference on oop for lock args */ 13646 if (oop != NULL) { 13647 nfs4_end_open_seqid_sync(oop); 13648 open_owner_rele(oop); 13649 *oopp = NULL; 13650 } 13651 13652 crfree(*cred_otwp); 13653 *cred_otwp = base_cr; 13654 crhold(*cred_otwp); 13655 } 13656 13657 /* 13658 * Function to process the client's recovery for nfs4frlock. 13659 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13660 * 13661 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13662 * COMPOUND4 args/res for calls that need to retry. 13663 * 13664 * Note: the rp's r_lkserlock is *not* dropped during this path. 13665 */ 13666 static bool_t 13667 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13668 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13669 LOCK4args *lock_args, LOCKU4args *locku_args, 13670 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13671 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13672 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13673 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13674 { 13675 nfs4_open_owner_t *oop = *oopp; 13676 nfs4_open_stream_t *osp = *ospp; 13677 nfs4_lock_owner_t *lop = *lopp; 13678 13679 bool_t abort, retry; 13680 13681 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13682 ASSERT((*argspp) != NULL); 13683 ASSERT((*respp) != NULL); 13684 if (lock_args || locku_args) 13685 ASSERT(lop != NULL); 13686 13687 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13688 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13689 13690 retry = TRUE; 13691 abort = FALSE; 13692 if (needrecov) { 13693 nfs4_bseqid_entry_t *bsep = NULL; 13694 nfs_opnum4 op; 13695 13696 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13697 13698 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13699 seqid4 seqid; 13700 13701 if (lock_args) { 13702 if (lock_args->locker.new_lock_owner == TRUE) 13703 seqid = lock_args->locker.locker4_u. 13704 open_owner.open_seqid; 13705 else 13706 seqid = lock_args->locker.locker4_u. 13707 lock_owner.lock_seqid; 13708 } else if (locku_args) { 13709 seqid = locku_args->seqid; 13710 } else { 13711 seqid = 0; 13712 } 13713 13714 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13715 flk->l_pid, (*argspp)->ctag, seqid); 13716 } 13717 13718 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13719 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13720 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13721 NULL, op, bsep, NULL, NULL); 13722 13723 if (bsep) 13724 kmem_free(bsep, sizeof (*bsep)); 13725 } 13726 13727 /* 13728 * Return that we do not want to retry the request for 3 cases: 13729 * 1. If we received EINTR or are bailing out because of a forced 13730 * unmount, we came into this code path just for the sake of 13731 * initiating recovery, we now need to return the error. 13732 * 2. If we have aborted recovery. 13733 * 3. We received NFS4ERR_BAD_SEQID. 13734 */ 13735 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13736 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13737 retry = FALSE; 13738 13739 if (*did_start_fop == TRUE) { 13740 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13741 needrecov); 13742 *did_start_fop = FALSE; 13743 } 13744 13745 if (retry == TRUE) { 13746 nfs_argop4 *argop; 13747 13748 argop = (*argspp)->array; 13749 ASSERT((*argspp)->array_len == 2); 13750 13751 if (argop[1].argop == OP_LOCK) 13752 nfs4args_lock_free(&argop[1]); 13753 else if (argop[1].argop == OP_LOCKT) 13754 nfs4args_lockt_free(&argop[1]); 13755 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13756 if (!ep->error) 13757 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13758 *respp = NULL; 13759 *argspp = NULL; 13760 } 13761 13762 if (lop != NULL) { 13763 nfs4_end_lock_seqid_sync(lop); 13764 lock_owner_rele(lop); 13765 } 13766 13767 *lopp = NULL; 13768 13769 /* need to free up the reference on osp for lock args */ 13770 if (osp != NULL) { 13771 open_stream_rele(osp, rp); 13772 *ospp = NULL; 13773 } 13774 13775 /* need to free up the reference on oop for lock args */ 13776 if (oop != NULL) { 13777 nfs4_end_open_seqid_sync(oop); 13778 open_owner_rele(oop); 13779 *oopp = NULL; 13780 } 13781 13782 return (retry); 13783 } 13784 13785 /* 13786 * Handles the successful reply from the server for nfs4frlock. 13787 */ 13788 static void 13789 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13790 vnode_t *vp, int flag, u_offset_t offset, 13791 nfs4_lost_rqst_t *resend_rqstp) 13792 { 13793 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13794 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13795 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13796 if (ctype == NFS4_LCK_CTYPE_NORM) { 13797 flk->l_pid = ttoproc(curthread)->p_pid; 13798 /* 13799 * We do not register lost locks locally in 13800 * the 'resend' case since the user/application 13801 * doesn't think we have the lock. 13802 */ 13803 ASSERT(!resend_rqstp); 13804 nfs4_register_lock_locally(vp, flk, flag, offset); 13805 } 13806 } 13807 } 13808 13809 /* 13810 * Handle the DENIED reply from the server for nfs4frlock. 13811 * Returns TRUE if we should retry the request; FALSE otherwise. 13812 * 13813 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13814 * COMPOUND4 args/res for calls that need to retry. Can also 13815 * drop and regrab the r_lkserlock. 13816 */ 13817 static bool_t 13818 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13819 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13820 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13821 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13822 nfs4_recov_state_t *recov_statep, int needrecov, 13823 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13824 clock_t *tick_delayp, short *whencep, int *errorp, 13825 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13826 bool_t *skip_get_err) 13827 { 13828 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13829 13830 if (lock_args) { 13831 nfs4_open_owner_t *oop = *oopp; 13832 nfs4_open_stream_t *osp = *ospp; 13833 nfs4_lock_owner_t *lop = *lopp; 13834 int intr; 13835 13836 /* 13837 * Blocking lock needs to sleep and retry from the request. 13838 * 13839 * Do not block and wait for 'resend' or 'reinstate' 13840 * lock requests, just return the error. 13841 * 13842 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13843 */ 13844 if (cmd == F_SETLKW) { 13845 rnode4_t *rp = VTOR4(vp); 13846 nfs_argop4 *argop = (*argspp)->array; 13847 13848 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13849 13850 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13851 recov_statep, needrecov); 13852 *did_start_fop = FALSE; 13853 ASSERT((*argspp)->array_len == 2); 13854 if (argop[1].argop == OP_LOCK) 13855 nfs4args_lock_free(&argop[1]); 13856 else if (argop[1].argop == OP_LOCKT) 13857 nfs4args_lockt_free(&argop[1]); 13858 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13859 if (*respp) 13860 (void) xdr_free(xdr_COMPOUND4res_clnt, 13861 (caddr_t)*respp); 13862 *argspp = NULL; 13863 *respp = NULL; 13864 nfs4_end_lock_seqid_sync(lop); 13865 lock_owner_rele(lop); 13866 *lopp = NULL; 13867 if (osp != NULL) { 13868 open_stream_rele(osp, rp); 13869 *ospp = NULL; 13870 } 13871 if (oop != NULL) { 13872 nfs4_end_open_seqid_sync(oop); 13873 open_owner_rele(oop); 13874 *oopp = NULL; 13875 } 13876 13877 nfs_rw_exit(&rp->r_lkserlock); 13878 13879 intr = nfs4_block_and_wait(tick_delayp, rp); 13880 13881 if (intr) { 13882 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13883 RW_WRITER, FALSE); 13884 *errorp = EINTR; 13885 return (FALSE); 13886 } 13887 13888 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13889 RW_WRITER, FALSE); 13890 13891 /* 13892 * Make sure we are still safe to lock with 13893 * regards to mmapping. 13894 */ 13895 if (!nfs4_safelock(vp, flk, cr)) { 13896 *errorp = EAGAIN; 13897 return (FALSE); 13898 } 13899 13900 return (TRUE); 13901 } 13902 if (ctype == NFS4_LCK_CTYPE_NORM) 13903 *errorp = EAGAIN; 13904 *skip_get_err = TRUE; 13905 flk->l_whence = 0; 13906 *whencep = 0; 13907 return (FALSE); 13908 } else if (lockt_args) { 13909 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13910 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13911 13912 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13913 flk, lockt_args); 13914 13915 /* according to NLM code */ 13916 *errorp = 0; 13917 *whencep = 0; 13918 *skip_get_err = TRUE; 13919 return (FALSE); 13920 } 13921 return (FALSE); 13922 } 13923 13924 /* 13925 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13926 */ 13927 static void 13928 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13929 { 13930 switch (resp->status) { 13931 case NFS4ERR_ACCESS: 13932 case NFS4ERR_ADMIN_REVOKED: 13933 case NFS4ERR_BADHANDLE: 13934 case NFS4ERR_BAD_RANGE: 13935 case NFS4ERR_BAD_SEQID: 13936 case NFS4ERR_BAD_STATEID: 13937 case NFS4ERR_BADXDR: 13938 case NFS4ERR_DEADLOCK: 13939 case NFS4ERR_DELAY: 13940 case NFS4ERR_EXPIRED: 13941 case NFS4ERR_FHEXPIRED: 13942 case NFS4ERR_GRACE: 13943 case NFS4ERR_INVAL: 13944 case NFS4ERR_ISDIR: 13945 case NFS4ERR_LEASE_MOVED: 13946 case NFS4ERR_LOCK_NOTSUPP: 13947 case NFS4ERR_LOCK_RANGE: 13948 case NFS4ERR_MOVED: 13949 case NFS4ERR_NOFILEHANDLE: 13950 case NFS4ERR_NO_GRACE: 13951 case NFS4ERR_OLD_STATEID: 13952 case NFS4ERR_OPENMODE: 13953 case NFS4ERR_RECLAIM_BAD: 13954 case NFS4ERR_RECLAIM_CONFLICT: 13955 case NFS4ERR_RESOURCE: 13956 case NFS4ERR_SERVERFAULT: 13957 case NFS4ERR_STALE: 13958 case NFS4ERR_STALE_CLIENTID: 13959 case NFS4ERR_STALE_STATEID: 13960 return; 13961 default: 13962 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13963 "nfs4frlock_results_default: got unrecognizable " 13964 "res.status %d", resp->status)); 13965 *errorp = NFS4ERR_INVAL; 13966 } 13967 } 13968 13969 /* 13970 * The lock request was successful, so update the client's state. 13971 */ 13972 static void 13973 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13974 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13975 vnode_t *vp, flock64_t *flk, cred_t *cr, 13976 nfs4_lost_rqst_t *resend_rqstp) 13977 { 13978 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13979 13980 if (lock_args) { 13981 LOCK4res *lock_res; 13982 13983 lock_res = &resop->nfs_resop4_u.oplock; 13984 /* update the stateid with server's response */ 13985 13986 if (lock_args->locker.new_lock_owner == TRUE) { 13987 mutex_enter(&lop->lo_lock); 13988 lop->lo_just_created = NFS4_PERM_CREATED; 13989 mutex_exit(&lop->lo_lock); 13990 } 13991 13992 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13993 13994 /* 13995 * If the lock was the result of a resending a lost 13996 * request, we've synched up the stateid and seqid 13997 * with the server, but now the server might be out of sync 13998 * with what the application thinks it has for locks. 13999 * Clean that up here. It's unclear whether we should do 14000 * this even if the filesystem has been forcibly unmounted. 14001 * For most servers, it's probably wasted effort, but 14002 * RFC3530 lets servers require that unlocks exactly match 14003 * the locks that are held. 14004 */ 14005 if (resend_rqstp != NULL && 14006 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 14007 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 14008 } else { 14009 flk->l_whence = 0; 14010 } 14011 } else if (locku_args) { 14012 LOCKU4res *locku_res; 14013 14014 locku_res = &resop->nfs_resop4_u.oplocku; 14015 14016 /* Update the stateid with the server's response */ 14017 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 14018 } else if (lockt_args) { 14019 /* Switch the lock type to express success, see fcntl */ 14020 flk->l_type = F_UNLCK; 14021 flk->l_whence = 0; 14022 } 14023 } 14024 14025 /* 14026 * Do final cleanup before exiting nfs4frlock. 14027 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 14028 * COMPOUND4 args/res for calls that haven't already. 14029 */ 14030 static void 14031 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 14032 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 14033 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 14034 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 14035 short whence, u_offset_t offset, struct lm_sysid *ls, 14036 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 14037 bool_t did_start_fop, bool_t skip_get_err, 14038 cred_t *cred_otw, cred_t *cred) 14039 { 14040 mntinfo4_t *mi = VTOMI4(vp); 14041 rnode4_t *rp = VTOR4(vp); 14042 int error = *errorp; 14043 nfs_argop4 *argop; 14044 int do_flush_pages = 0; 14045 14046 ASSERT(nfs_zone() == mi->mi_zone); 14047 /* 14048 * The client recovery code wants the raw status information, 14049 * so don't map the NFS status code to an errno value for 14050 * non-normal call types. 14051 */ 14052 if (ctype == NFS4_LCK_CTYPE_NORM) { 14053 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 14054 *errorp = geterrno4(resp->status); 14055 if (did_start_fop == TRUE) 14056 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 14057 needrecov); 14058 14059 /* 14060 * We've established a new lock on the server, so invalidate 14061 * the pages associated with the vnode to get the most up to 14062 * date pages from the server after acquiring the lock. We 14063 * want to be sure that the read operation gets the newest data. 14064 * N.B. 14065 * We used to do this in nfs4frlock_results_ok but that doesn't 14066 * work since VOP_PUTPAGE can call nfs4_commit which calls 14067 * nfs4_start_fop. We flush the pages below after calling 14068 * nfs4_end_fop above 14069 * The flush of the page cache must be done after 14070 * nfs4_end_open_seqid_sync() to avoid a 4-way hang. 14071 */ 14072 if (!error && resp && resp->status == NFS4_OK) 14073 do_flush_pages = 1; 14074 } 14075 if (argsp) { 14076 ASSERT(argsp->array_len == 2); 14077 argop = argsp->array; 14078 if (argop[1].argop == OP_LOCK) 14079 nfs4args_lock_free(&argop[1]); 14080 else if (argop[1].argop == OP_LOCKT) 14081 nfs4args_lockt_free(&argop[1]); 14082 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14083 if (resp) 14084 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 14085 } 14086 14087 /* free the reference on the lock owner */ 14088 if (lop != NULL) { 14089 nfs4_end_lock_seqid_sync(lop); 14090 lock_owner_rele(lop); 14091 } 14092 14093 /* need to free up the reference on osp for lock args */ 14094 if (osp != NULL) 14095 open_stream_rele(osp, rp); 14096 14097 /* need to free up the reference on oop for lock args */ 14098 if (oop != NULL) { 14099 nfs4_end_open_seqid_sync(oop); 14100 open_owner_rele(oop); 14101 } 14102 14103 if (do_flush_pages) 14104 nfs4_flush_pages(vp, cred); 14105 14106 (void) convoff(vp, flk, whence, offset); 14107 14108 lm_rel_sysid(ls); 14109 14110 /* 14111 * Record debug information in the event we get EINVAL. 14112 */ 14113 mutex_enter(&mi->mi_lock); 14114 if (*errorp == EINVAL && (lock_args || locku_args) && 14115 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 14116 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 14117 zcmn_err(getzoneid(), CE_NOTE, 14118 "%s operation failed with " 14119 "EINVAL probably since the server, %s," 14120 " doesn't support POSIX style locking", 14121 lock_args ? "LOCK" : "LOCKU", 14122 mi->mi_curr_serv->sv_hostname); 14123 mi->mi_flags |= MI4_LOCK_DEBUG; 14124 } 14125 } 14126 mutex_exit(&mi->mi_lock); 14127 14128 if (cred_otw) 14129 crfree(cred_otw); 14130 } 14131 14132 /* 14133 * This calls the server and the local locking code. 14134 * 14135 * Client locks are registerred locally by oring the sysid with 14136 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 14137 * We need to distinguish between the two to avoid collision in case one 14138 * machine is used as both client and server. 14139 * 14140 * Blocking lock requests will continually retry to acquire the lock 14141 * forever. 14142 * 14143 * The ctype is defined as follows: 14144 * NFS4_LCK_CTYPE_NORM: normal lock request. 14145 * 14146 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 14147 * recovery, get the pid from flk instead of curproc, and don't reregister 14148 * the lock locally. 14149 * 14150 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 14151 * that we will use the information passed in via resend_rqstp to setup the 14152 * lock/locku request. This resend is the exact same request as the 'lost 14153 * lock', and is initiated by the recovery framework. A successful resend 14154 * request can initiate one or more reinstate requests. 14155 * 14156 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 14157 * does not trigger additional reinstate requests. This lock call type is 14158 * set for setting the v4 server's locking state back to match what the 14159 * client's local locking state is in the event of a received 'lost lock'. 14160 * 14161 * Errors are returned via the nfs4_error_t parameter. 14162 */ 14163 void 14164 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 14165 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 14166 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 14167 { 14168 COMPOUND4args_clnt args, *argsp = NULL; 14169 COMPOUND4res_clnt res, *resp = NULL; 14170 nfs_argop4 *argop; 14171 nfs_resop4 *resop; 14172 rnode4_t *rp; 14173 int doqueue = 1; 14174 clock_t tick_delay; /* delay in clock ticks */ 14175 struct lm_sysid *ls; 14176 LOCK4args *lock_args = NULL; 14177 LOCKU4args *locku_args = NULL; 14178 LOCKT4args *lockt_args = NULL; 14179 nfs4_open_owner_t *oop = NULL; 14180 nfs4_open_stream_t *osp = NULL; 14181 nfs4_lock_owner_t *lop = NULL; 14182 bool_t needrecov = FALSE; 14183 nfs4_recov_state_t recov_state; 14184 short whence; 14185 nfs4_op_hint_t op_hint; 14186 nfs4_lost_rqst_t lost_rqst; 14187 bool_t retry = FALSE; 14188 bool_t did_start_fop = FALSE; 14189 bool_t skip_get_err = FALSE; 14190 cred_t *cred_otw = NULL; 14191 bool_t recovonly; /* just queue request */ 14192 int frc_no_reclaim = 0; 14193 #ifdef DEBUG 14194 char *name; 14195 #endif 14196 14197 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14198 14199 #ifdef DEBUG 14200 name = fn_name(VTOSV(vp)->sv_name); 14201 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 14202 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 14203 "length %"PRIu64", pid %d, sysid %d, call type %s, " 14204 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 14205 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 14206 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 14207 resend_rqstp ? "TRUE" : "FALSE")); 14208 kmem_free(name, MAXNAMELEN); 14209 #endif 14210 14211 nfs4_error_zinit(ep); 14212 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14213 if (ep->error) 14214 return; 14215 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14216 if (ep->error) 14217 return; 14218 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14219 vp, cr, &cred_otw); 14220 14221 recov_retry: 14222 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14223 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14224 rp = VTOR4(vp); 14225 14226 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14227 &did_start_fop, &recovonly); 14228 14229 if (ep->error) 14230 goto out; 14231 14232 if (recovonly) { 14233 /* 14234 * Leave the request for the recovery system to deal with. 14235 */ 14236 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14237 ASSERT(cmd != F_GETLK); 14238 ASSERT(flk->l_type == F_UNLCK); 14239 14240 nfs4_error_init(ep, EINTR); 14241 needrecov = TRUE; 14242 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14243 if (lop != NULL) { 14244 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14245 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14246 (void) nfs4_start_recovery(ep, 14247 VTOMI4(vp), vp, NULL, NULL, 14248 (lost_rqst.lr_op == OP_LOCK || 14249 lost_rqst.lr_op == OP_LOCKU) ? 14250 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL); 14251 lock_owner_rele(lop); 14252 lop = NULL; 14253 } 14254 flk->l_pid = curproc->p_pid; 14255 nfs4_register_lock_locally(vp, flk, flag, offset); 14256 goto out; 14257 } 14258 14259 /* putfh directory fh */ 14260 argop[0].argop = OP_CPUTFH; 14261 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14262 14263 /* 14264 * Set up the over-the-wire arguments and get references to the 14265 * open owner, etc. 14266 */ 14267 14268 if (ctype == NFS4_LCK_CTYPE_RESEND || 14269 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14270 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14271 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14272 } else { 14273 bool_t go_otw = TRUE; 14274 14275 ASSERT(resend_rqstp == NULL); 14276 14277 switch (cmd) { 14278 case F_GETLK: 14279 case F_O_GETLK: 14280 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14281 &lockt_args, argsp, flk, rp); 14282 break; 14283 case F_SETLKW: 14284 case F_SETLK: 14285 if (flk->l_type == F_UNLCK) 14286 nfs4frlock_setup_locku_args(ctype, 14287 &argop[1], &locku_args, flk, 14288 &lop, ep, argsp, 14289 vp, flag, offset, cr, 14290 &skip_get_err, &go_otw); 14291 else 14292 nfs4frlock_setup_lock_args(ctype, 14293 &lock_args, &oop, &osp, &lop, &argop[1], 14294 argsp, flk, cmd, vp, cr, ep); 14295 14296 if (ep->error) 14297 goto out; 14298 14299 switch (ep->stat) { 14300 case NFS4_OK: 14301 break; 14302 case NFS4ERR_DELAY: 14303 /* recov thread never gets this error */ 14304 ASSERT(resend_rqstp == NULL); 14305 ASSERT(did_start_fop); 14306 14307 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14308 &recov_state, TRUE); 14309 did_start_fop = FALSE; 14310 if (argop[1].argop == OP_LOCK) 14311 nfs4args_lock_free(&argop[1]); 14312 else if (argop[1].argop == OP_LOCKT) 14313 nfs4args_lockt_free(&argop[1]); 14314 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14315 argsp = NULL; 14316 goto recov_retry; 14317 default: 14318 ep->error = EIO; 14319 goto out; 14320 } 14321 break; 14322 default: 14323 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14324 "nfs4_frlock: invalid cmd %d", cmd)); 14325 ep->error = EINVAL; 14326 goto out; 14327 } 14328 14329 if (!go_otw) 14330 goto out; 14331 } 14332 14333 /* XXX should we use the local reclock as a cache ? */ 14334 /* 14335 * Unregister the lock with the local locking code before 14336 * contacting the server. This avoids a potential race where 14337 * another process gets notified that it has been granted a lock 14338 * before we can unregister ourselves locally. 14339 */ 14340 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14341 if (ctype == NFS4_LCK_CTYPE_NORM) 14342 flk->l_pid = ttoproc(curthread)->p_pid; 14343 nfs4_register_lock_locally(vp, flk, flag, offset); 14344 } 14345 14346 /* 14347 * Send the server the lock request. Continually loop with a delay 14348 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14349 */ 14350 resp = &res; 14351 14352 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14353 (CE_NOTE, 14354 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14355 rnode4info(rp))); 14356 14357 if (lock_args && frc_no_reclaim) { 14358 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14359 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14360 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14361 lock_args->reclaim = FALSE; 14362 if (did_reclaimp) 14363 *did_reclaimp = 0; 14364 } 14365 14366 /* 14367 * Do the OTW call. 14368 */ 14369 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14370 14371 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14372 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14373 14374 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14375 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14376 "nfs4frlock: needrecov %d", needrecov)); 14377 14378 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14379 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14380 args.ctag); 14381 14382 /* 14383 * Check if one of these mutually exclusive error cases has 14384 * happened: 14385 * need to swap credentials due to access error 14386 * recovery is needed 14387 * different error (only known case is missing Kerberos ticket) 14388 */ 14389 14390 if ((ep->error == EACCES || 14391 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14392 cred_otw != cr) { 14393 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14394 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14395 cr, &cred_otw); 14396 goto recov_retry; 14397 } 14398 14399 if (needrecov) { 14400 /* 14401 * LOCKT requests don't need to recover from lost 14402 * requests since they don't create/modify state. 14403 */ 14404 if ((ep->error == EINTR || 14405 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14406 lockt_args) 14407 goto out; 14408 /* 14409 * Do not attempt recovery for requests initiated by 14410 * the recovery framework. Let the framework redrive them. 14411 */ 14412 if (ctype != NFS4_LCK_CTYPE_NORM) 14413 goto out; 14414 else { 14415 ASSERT(resend_rqstp == NULL); 14416 } 14417 14418 nfs4frlock_save_lost_rqst(ctype, ep->error, 14419 flk_to_locktype(cmd, flk->l_type), 14420 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14421 14422 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14423 &resp, lock_args, locku_args, &oop, &osp, &lop, 14424 rp, vp, &recov_state, op_hint, &did_start_fop, 14425 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14426 14427 if (retry) { 14428 ASSERT(oop == NULL); 14429 ASSERT(osp == NULL); 14430 ASSERT(lop == NULL); 14431 goto recov_retry; 14432 } 14433 goto out; 14434 } 14435 14436 /* 14437 * Bail out if have reached this point with ep->error set. Can 14438 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14439 * This happens if Kerberos ticket has expired or has been 14440 * destroyed. 14441 */ 14442 if (ep->error != 0) 14443 goto out; 14444 14445 /* 14446 * Process the reply. 14447 */ 14448 switch (resp->status) { 14449 case NFS4_OK: 14450 resop = &resp->array[1]; 14451 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14452 resend_rqstp); 14453 /* 14454 * Have a successful lock operation, now update state. 14455 */ 14456 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14457 resop, lop, vp, flk, cr, resend_rqstp); 14458 break; 14459 14460 case NFS4ERR_DENIED: 14461 resop = &resp->array[1]; 14462 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14463 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14464 &recov_state, needrecov, &argsp, &resp, 14465 &tick_delay, &whence, &ep->error, resop, cr, 14466 &did_start_fop, &skip_get_err); 14467 14468 if (retry) { 14469 ASSERT(oop == NULL); 14470 ASSERT(osp == NULL); 14471 ASSERT(lop == NULL); 14472 goto recov_retry; 14473 } 14474 break; 14475 /* 14476 * If the server won't let us reclaim, fall-back to trying to lock 14477 * the file from scratch. Code elsewhere will check the changeinfo 14478 * to ensure the file hasn't been changed. 14479 */ 14480 case NFS4ERR_NO_GRACE: 14481 if (lock_args && lock_args->reclaim == TRUE) { 14482 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14483 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14484 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14485 frc_no_reclaim = 1; 14486 /* clean up before retrying */ 14487 needrecov = 0; 14488 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14489 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14490 &recov_state, op_hint, &did_start_fop, NULL, flk); 14491 goto recov_retry; 14492 } 14493 /* FALLTHROUGH */ 14494 14495 default: 14496 nfs4frlock_results_default(resp, &ep->error); 14497 break; 14498 } 14499 out: 14500 /* 14501 * Process and cleanup from error. Make interrupted unlock 14502 * requests look successful, since they will be handled by the 14503 * client recovery code. 14504 */ 14505 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14506 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14507 lock_args, locku_args, did_start_fop, 14508 skip_get_err, cred_otw, cr); 14509 14510 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14511 (cmd == F_SETLK || cmd == F_SETLKW)) 14512 ep->error = 0; 14513 } 14514 14515 /* 14516 * nfs4_safelock: 14517 * 14518 * Return non-zero if the given lock request can be handled without 14519 * violating the constraints on concurrent mapping and locking. 14520 */ 14521 14522 static int 14523 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14524 { 14525 rnode4_t *rp = VTOR4(vp); 14526 struct vattr va; 14527 int error; 14528 14529 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14530 ASSERT(rp->r_mapcnt >= 0); 14531 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14532 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14533 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14534 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14535 14536 if (rp->r_mapcnt == 0) 14537 return (1); /* always safe if not mapped */ 14538 14539 /* 14540 * If the file is already mapped and there are locks, then they 14541 * should be all safe locks. So adding or removing a lock is safe 14542 * as long as the new request is safe (i.e., whole-file, meaning 14543 * length and starting offset are both zero). 14544 */ 14545 14546 if (bfp->l_start != 0 || bfp->l_len != 0) { 14547 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14548 "cannot lock a memory mapped file unless locking the " 14549 "entire file: start %"PRIx64", len %"PRIx64, 14550 bfp->l_start, bfp->l_len)); 14551 return (0); 14552 } 14553 14554 /* mandatory locking and mapping don't mix */ 14555 va.va_mask = AT_MODE; 14556 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 14557 if (error != 0) { 14558 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14559 "getattr error %d", error)); 14560 return (0); /* treat errors conservatively */ 14561 } 14562 if (MANDLOCK(vp, va.va_mode)) { 14563 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14564 "cannot mandatory lock and mmap a file")); 14565 return (0); 14566 } 14567 14568 return (1); 14569 } 14570 14571 14572 /* 14573 * Register the lock locally within Solaris. 14574 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14575 * recording locks locally. 14576 * 14577 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14578 * are registered locally. 14579 */ 14580 void 14581 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14582 u_offset_t offset) 14583 { 14584 int oldsysid; 14585 int error; 14586 #ifdef DEBUG 14587 char *name; 14588 #endif 14589 14590 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14591 14592 #ifdef DEBUG 14593 name = fn_name(VTOSV(vp)->sv_name); 14594 NFS4_DEBUG(nfs4_client_lock_debug, 14595 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14596 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14597 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14598 flk->l_sysid)); 14599 kmem_free(name, MAXNAMELEN); 14600 #endif 14601 14602 /* register the lock with local locking */ 14603 oldsysid = flk->l_sysid; 14604 flk->l_sysid |= LM_SYSID_CLIENT; 14605 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14606 #ifdef DEBUG 14607 if (error != 0) { 14608 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14609 "nfs4_register_lock_locally: could not register with" 14610 " local locking")); 14611 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14612 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14613 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14614 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14615 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14616 flk->l_type, flk->l_start, flk->l_len)); 14617 (void) reclock(vp, flk, 0, flag, offset, NULL); 14618 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14619 "blocked by pid %d sysid 0x%x type %d " 14620 "off 0x%" PRIx64 " len 0x%" PRIx64, 14621 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14622 flk->l_len)); 14623 } 14624 #endif 14625 flk->l_sysid = oldsysid; 14626 } 14627 14628 /* 14629 * nfs4_lockrelease: 14630 * 14631 * Release any locks on the given vnode that are held by the current 14632 * process. Also removes the lock owner (if one exists) from the rnode's 14633 * list. 14634 */ 14635 static int 14636 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14637 { 14638 flock64_t ld; 14639 int ret, error; 14640 rnode4_t *rp; 14641 nfs4_lock_owner_t *lop; 14642 nfs4_recov_state_t recov_state; 14643 mntinfo4_t *mi; 14644 bool_t possible_orphan = FALSE; 14645 bool_t recovonly; 14646 14647 ASSERT((uintptr_t)vp > KERNELBASE); 14648 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14649 14650 rp = VTOR4(vp); 14651 mi = VTOMI4(vp); 14652 14653 /* 14654 * If we have not locked anything then we can 14655 * just return since we have no work to do. 14656 */ 14657 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14658 return (0); 14659 } 14660 14661 /* 14662 * We need to comprehend that another thread may 14663 * kick off recovery and the lock_owner we have stashed 14664 * in lop might be invalid so we should NOT cache it 14665 * locally! 14666 */ 14667 recov_state.rs_flags = 0; 14668 recov_state.rs_num_retry_despite_err = 0; 14669 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14670 &recovonly); 14671 if (error) { 14672 mutex_enter(&rp->r_statelock); 14673 rp->r_flags |= R4LODANGLERS; 14674 mutex_exit(&rp->r_statelock); 14675 return (error); 14676 } 14677 14678 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14679 14680 /* 14681 * Check if the lock owner might have a lock (request was sent but 14682 * no response was received). Also check if there are any remote 14683 * locks on the file. (In theory we shouldn't have to make this 14684 * second check if there's no lock owner, but for now we'll be 14685 * conservative and do it anyway.) If either condition is true, 14686 * send an unlock for the entire file to the server. 14687 * 14688 * Note that no explicit synchronization is needed here. At worst, 14689 * flk_has_remote_locks() will return a false positive, in which case 14690 * the unlock call wastes time but doesn't harm correctness. 14691 */ 14692 14693 if (lop) { 14694 mutex_enter(&lop->lo_lock); 14695 possible_orphan = lop->lo_pending_rqsts; 14696 mutex_exit(&lop->lo_lock); 14697 lock_owner_rele(lop); 14698 } 14699 14700 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14701 14702 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14703 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14704 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14705 (void *)lop)); 14706 14707 if (possible_orphan || flk_has_remote_locks(vp)) { 14708 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14709 ld.l_whence = 0; /* unlock from start of file */ 14710 ld.l_start = 0; 14711 ld.l_len = 0; /* do entire file */ 14712 14713 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, 14714 cr, NULL); 14715 14716 if (ret != 0) { 14717 /* 14718 * If VOP_FRLOCK fails, make sure we unregister 14719 * local locks before we continue. 14720 */ 14721 ld.l_pid = ttoproc(curthread)->p_pid; 14722 nfs4_register_lock_locally(vp, &ld, flag, offset); 14723 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14724 "nfs4_lockrelease: lock release error on vp" 14725 " %p: error %d.\n", (void *)vp, ret)); 14726 } 14727 } 14728 14729 recov_state.rs_flags = 0; 14730 recov_state.rs_num_retry_despite_err = 0; 14731 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14732 &recovonly); 14733 if (error) { 14734 mutex_enter(&rp->r_statelock); 14735 rp->r_flags |= R4LODANGLERS; 14736 mutex_exit(&rp->r_statelock); 14737 return (error); 14738 } 14739 14740 /* 14741 * So, here we're going to need to retrieve the lock-owner 14742 * again (in case recovery has done a switch-a-roo) and 14743 * remove it because we can. 14744 */ 14745 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14746 14747 if (lop) { 14748 nfs4_rnode_remove_lock_owner(rp, lop); 14749 lock_owner_rele(lop); 14750 } 14751 14752 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14753 return (0); 14754 } 14755 14756 /* 14757 * Wait for 'tick_delay' clock ticks. 14758 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14759 * 14760 * The client should retry to acquire the lock faster than the lease period. 14761 * We use roughly half of the lease time to use a similar calculation as it is 14762 * used in nfs4_renew_lease_thread(). 14763 * 14764 * XXX For future improvements, should implement a waiting queue scheme. 14765 */ 14766 static int 14767 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14768 { 14769 /* wait tick_delay clock ticks or siginteruptus */ 14770 if (delay_sig(*tick_delay)) { 14771 return (EINTR); 14772 } 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 *tick_delay = MIN(drv_usectohz(nfs4_max_base_wait_time * 1000), 14779 *tick_delay * 1.5); 14780 return (0); 14781 } 14782 14783 void 14784 nfs4_vnops_init(void) 14785 { 14786 } 14787 14788 void 14789 nfs4_vnops_fini(void) 14790 { 14791 } 14792 14793 /* 14794 * Return a reference to the directory (parent) vnode for a given vnode, 14795 * using the saved pathname information and the directory file handle. The 14796 * caller is responsible for disposing of the reference. 14797 * Returns zero or an errno value. 14798 * 14799 * Caller should set need_start_op to FALSE if it is the recovery 14800 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14801 */ 14802 int 14803 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14804 { 14805 svnode_t *svnp; 14806 vnode_t *dvp = NULL; 14807 servinfo4_t *svp; 14808 nfs4_fname_t *mfname; 14809 int error; 14810 14811 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14812 14813 if (vp->v_flag & VROOT) { 14814 nfs4_sharedfh_t *sfh; 14815 nfs_fh4 fh; 14816 mntinfo4_t *mi; 14817 14818 ASSERT(vp->v_type == VREG); 14819 14820 mi = VTOMI4(vp); 14821 svp = mi->mi_curr_serv; 14822 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14823 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14824 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14825 sfh = sfh4_get(&fh, VTOMI4(vp)); 14826 nfs_rw_exit(&svp->sv_lock); 14827 mfname = mi->mi_fname; 14828 fn_hold(mfname); 14829 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14830 sfh4_rele(&sfh); 14831 14832 if (dvp->v_type == VNON) 14833 dvp->v_type = VDIR; 14834 *dvpp = dvp; 14835 return (0); 14836 } 14837 14838 svnp = VTOSV(vp); 14839 14840 if (svnp == NULL) { 14841 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14842 "shadow node is NULL")); 14843 return (EINVAL); 14844 } 14845 14846 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14847 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14848 "shadow node name or dfh val == NULL")); 14849 return (EINVAL); 14850 } 14851 14852 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14853 (int)need_start_op); 14854 if (error != 0) { 14855 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14856 "nfs4_make_dotdot returned %d", error)); 14857 return (error); 14858 } 14859 if (!dvp) { 14860 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14861 "nfs4_make_dotdot returned a NULL dvp")); 14862 return (EIO); 14863 } 14864 if (dvp->v_type == VNON) 14865 dvp->v_type = VDIR; 14866 ASSERT(dvp->v_type == VDIR); 14867 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14868 mutex_enter(&dvp->v_lock); 14869 dvp->v_flag |= V_XATTRDIR; 14870 mutex_exit(&dvp->v_lock); 14871 } 14872 *dvpp = dvp; 14873 return (0); 14874 } 14875 14876 /* 14877 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14878 * length that fnamep can accept, including the trailing null. 14879 * Returns 0 if okay, returns an errno value if there was a problem. 14880 */ 14881 14882 int 14883 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14884 { 14885 char *fn; 14886 int err = 0; 14887 servinfo4_t *svp; 14888 svnode_t *shvp; 14889 14890 /* 14891 * If the file being opened has VROOT set, then this is 14892 * a "file" mount. sv_name will not be interesting, so 14893 * go back to the servinfo4 to get the original mount 14894 * path and strip off all but the final edge. Otherwise 14895 * just return the name from the shadow vnode. 14896 */ 14897 14898 if (vp->v_flag & VROOT) { 14899 14900 svp = VTOMI4(vp)->mi_curr_serv; 14901 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14902 14903 fn = strrchr(svp->sv_path, '/'); 14904 if (fn == NULL) 14905 err = EINVAL; 14906 else 14907 fn++; 14908 } else { 14909 shvp = VTOSV(vp); 14910 fn = fn_name(shvp->sv_name); 14911 } 14912 14913 if (err == 0) 14914 if (strlen(fn) < maxlen) 14915 (void) strcpy(fnamep, fn); 14916 else 14917 err = ENAMETOOLONG; 14918 14919 if (vp->v_flag & VROOT) 14920 nfs_rw_exit(&svp->sv_lock); 14921 else 14922 kmem_free(fn, MAXNAMELEN); 14923 14924 return (err); 14925 } 14926 14927 /* 14928 * Bookkeeping for a close that doesn't need to go over the wire. 14929 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14930 * it is left at 1. 14931 */ 14932 void 14933 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14934 { 14935 rnode4_t *rp; 14936 mntinfo4_t *mi; 14937 14938 mi = VTOMI4(vp); 14939 rp = VTOR4(vp); 14940 14941 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14942 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14943 ASSERT(nfs_zone() == mi->mi_zone); 14944 ASSERT(mutex_owned(&osp->os_sync_lock)); 14945 ASSERT(*have_lockp); 14946 14947 if (!osp->os_valid || 14948 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14949 return; 14950 } 14951 14952 /* 14953 * This removes the reference obtained at OPEN; ie, 14954 * when the open stream structure was created. 14955 * 14956 * We don't have to worry about calling 'open_stream_rele' 14957 * since we our currently holding a reference to this 14958 * open stream which means the count can not go to 0 with 14959 * this decrement. 14960 */ 14961 ASSERT(osp->os_ref_count >= 2); 14962 osp->os_ref_count--; 14963 osp->os_valid = 0; 14964 mutex_exit(&osp->os_sync_lock); 14965 *have_lockp = 0; 14966 14967 nfs4_dec_state_ref_count(mi); 14968 } 14969 14970 /* 14971 * Close all remaining open streams on the rnode. These open streams 14972 * could be here because: 14973 * - The close attempted at either close or delmap failed 14974 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14975 * - Someone did mknod on a regular file but never opened it 14976 */ 14977 int 14978 nfs4close_all(vnode_t *vp, cred_t *cr) 14979 { 14980 nfs4_open_stream_t *osp; 14981 int error; 14982 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14983 rnode4_t *rp; 14984 14985 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14986 14987 error = 0; 14988 rp = VTOR4(vp); 14989 14990 /* 14991 * At this point, all we know is that the last time 14992 * someone called vn_rele, the count was 1. Since then, 14993 * the vnode could have been re-activated. We want to 14994 * loop through the open streams and close each one, but 14995 * we have to be careful since once we release the rnode 14996 * hash bucket lock, someone else is free to come in and 14997 * re-activate the rnode and add new open streams. The 14998 * strategy is take the rnode hash bucket lock, verify that 14999 * the count is still 1, grab the open stream off the 15000 * head of the list and mark it invalid, then release the 15001 * rnode hash bucket lock and proceed with that open stream. 15002 * This is ok because nfs4close_one() will acquire the proper 15003 * open/create to close/destroy synchronization for open 15004 * streams, and will ensure that if someone has reopened 15005 * the open stream after we've dropped the hash bucket lock 15006 * then we'll just simply return without destroying the 15007 * open stream. 15008 * Repeat until the list is empty. 15009 */ 15010 15011 for (;;) { 15012 15013 /* make sure vnode hasn't been reactivated */ 15014 rw_enter(&rp->r_hashq->r_lock, RW_READER); 15015 mutex_enter(&vp->v_lock); 15016 if (vp->v_count > 1) { 15017 mutex_exit(&vp->v_lock); 15018 rw_exit(&rp->r_hashq->r_lock); 15019 break; 15020 } 15021 /* 15022 * Grabbing r_os_lock before releasing v_lock prevents 15023 * a window where the rnode/open stream could get 15024 * reactivated (and os_force_close set to 0) before we 15025 * had a chance to set os_force_close to 1. 15026 */ 15027 mutex_enter(&rp->r_os_lock); 15028 mutex_exit(&vp->v_lock); 15029 15030 osp = list_head(&rp->r_open_streams); 15031 if (!osp) { 15032 /* nothing left to CLOSE OTW, so return */ 15033 mutex_exit(&rp->r_os_lock); 15034 rw_exit(&rp->r_hashq->r_lock); 15035 break; 15036 } 15037 15038 mutex_enter(&rp->r_statev4_lock); 15039 /* the file can't still be mem mapped */ 15040 ASSERT(rp->r_mapcnt == 0); 15041 if (rp->created_v4) 15042 rp->created_v4 = 0; 15043 mutex_exit(&rp->r_statev4_lock); 15044 15045 /* 15046 * Grab a ref on this open stream; nfs4close_one 15047 * will mark it as invalid 15048 */ 15049 mutex_enter(&osp->os_sync_lock); 15050 osp->os_ref_count++; 15051 osp->os_force_close = 1; 15052 mutex_exit(&osp->os_sync_lock); 15053 mutex_exit(&rp->r_os_lock); 15054 rw_exit(&rp->r_hashq->r_lock); 15055 15056 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 15057 15058 /* Update error if it isn't already non-zero */ 15059 if (error == 0) { 15060 if (e.error) 15061 error = e.error; 15062 else if (e.stat) 15063 error = geterrno4(e.stat); 15064 } 15065 15066 #ifdef DEBUG 15067 nfs4close_all_cnt++; 15068 #endif 15069 /* Release the ref on osp acquired above. */ 15070 open_stream_rele(osp, rp); 15071 15072 /* Proceed to the next open stream, if any */ 15073 } 15074 return (error); 15075 } 15076 15077 /* 15078 * nfs4close_one - close one open stream for a file if needed. 15079 * 15080 * "close_type" indicates which close path this is: 15081 * CLOSE_NORM: close initiated via VOP_CLOSE. 15082 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 15083 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 15084 * the close and release of client state for this open stream 15085 * (unless someone else has the open stream open). 15086 * CLOSE_RESEND: indicates the request is a replay of an earlier request 15087 * (e.g., due to abort because of a signal). 15088 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 15089 * 15090 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 15091 * recovery. Instead, the caller is expected to deal with retries. 15092 * 15093 * The caller can either pass in the osp ('provided_osp') or not. 15094 * 15095 * 'access_bits' represents the access we are closing/downgrading. 15096 * 15097 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 15098 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 15099 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 15100 * 15101 * Errors are returned via the nfs4_error_t. 15102 */ 15103 void 15104 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 15105 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 15106 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 15107 uint_t mmap_flags) 15108 { 15109 nfs4_open_owner_t *oop; 15110 nfs4_open_stream_t *osp = NULL; 15111 int retry = 0; 15112 int num_retries = NFS4_NUM_RECOV_RETRIES; 15113 rnode4_t *rp; 15114 mntinfo4_t *mi; 15115 nfs4_recov_state_t recov_state; 15116 cred_t *cred_otw = NULL; 15117 bool_t recovonly = FALSE; 15118 int isrecov; 15119 int force_close; 15120 int close_failed = 0; 15121 int did_dec_count = 0; 15122 int did_start_op = 0; 15123 int did_force_recovlock = 0; 15124 int did_start_seqid_sync = 0; 15125 int have_sync_lock = 0; 15126 15127 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15128 15129 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 15130 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 15131 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 15132 len, maxprot, mmap_flags, access_bits)); 15133 15134 nfs4_error_zinit(ep); 15135 rp = VTOR4(vp); 15136 mi = VTOMI4(vp); 15137 isrecov = (close_type == CLOSE_RESEND || 15138 close_type == CLOSE_AFTER_RESEND); 15139 15140 /* 15141 * First get the open owner. 15142 */ 15143 if (!provided_osp) { 15144 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 15145 } else { 15146 oop = provided_osp->os_open_owner; 15147 ASSERT(oop != NULL); 15148 open_owner_hold(oop); 15149 } 15150 15151 if (!oop) { 15152 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15153 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 15154 "close type %d", (void *)rp, (void *)mi, (void *)cr, 15155 (void *)provided_osp, close_type)); 15156 ep->error = EIO; 15157 goto out; 15158 } 15159 15160 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 15161 recov_retry: 15162 osp = NULL; 15163 close_failed = 0; 15164 force_close = (close_type == CLOSE_FORCE); 15165 retry = 0; 15166 did_start_op = 0; 15167 did_force_recovlock = 0; 15168 did_start_seqid_sync = 0; 15169 have_sync_lock = 0; 15170 recovonly = FALSE; 15171 recov_state.rs_flags = 0; 15172 recov_state.rs_num_retry_despite_err = 0; 15173 15174 /* 15175 * Second synchronize with recovery. 15176 */ 15177 if (!isrecov) { 15178 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 15179 &recov_state, &recovonly); 15180 if (!ep->error) { 15181 did_start_op = 1; 15182 } else { 15183 close_failed = 1; 15184 /* 15185 * If we couldn't get start_fop, but have to 15186 * cleanup state, then at least acquire the 15187 * mi_recovlock so we can synchronize with 15188 * recovery. 15189 */ 15190 if (close_type == CLOSE_FORCE) { 15191 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 15192 RW_READER, FALSE); 15193 did_force_recovlock = 1; 15194 } else 15195 goto out; 15196 } 15197 } 15198 15199 /* 15200 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15201 * set 'recovonly' to TRUE since most likely this is due to 15202 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15203 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15204 * to retry, causing us to loop until recovery finishes. Plus we 15205 * don't need protection over the open seqid since we're not going 15206 * OTW, hence don't need to use the seqid. 15207 */ 15208 if (recovonly == FALSE) { 15209 /* need to grab the open owner sync before 'os_sync_lock' */ 15210 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15211 if (ep->error == EAGAIN) { 15212 ASSERT(!isrecov); 15213 if (did_start_op) 15214 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15215 &recov_state, TRUE); 15216 if (did_force_recovlock) 15217 nfs_rw_exit(&mi->mi_recovlock); 15218 goto recov_retry; 15219 } 15220 did_start_seqid_sync = 1; 15221 } 15222 15223 /* 15224 * Third get an open stream and acquire 'os_sync_lock' to 15225 * sychronize the opening/creating of an open stream with the 15226 * closing/destroying of an open stream. 15227 */ 15228 if (!provided_osp) { 15229 /* returns with 'os_sync_lock' held */ 15230 osp = find_open_stream(oop, rp); 15231 if (!osp) { 15232 ep->error = EIO; 15233 goto out; 15234 } 15235 } else { 15236 osp = provided_osp; 15237 open_stream_hold(osp); 15238 mutex_enter(&osp->os_sync_lock); 15239 } 15240 have_sync_lock = 1; 15241 15242 ASSERT(oop == osp->os_open_owner); 15243 15244 /* 15245 * Fourth, do any special pre-OTW CLOSE processing 15246 * based on the specific close type. 15247 */ 15248 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15249 !did_dec_count) { 15250 ASSERT(osp->os_open_ref_count > 0); 15251 osp->os_open_ref_count--; 15252 did_dec_count = 1; 15253 if (osp->os_open_ref_count == 0) 15254 osp->os_final_close = 1; 15255 } 15256 15257 if (close_type == CLOSE_FORCE) { 15258 /* see if somebody reopened the open stream. */ 15259 if (!osp->os_force_close) { 15260 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15261 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15262 "was reopened, vp %p", (void *)osp, (void *)vp)); 15263 ep->error = 0; 15264 ep->stat = NFS4_OK; 15265 goto out; 15266 } 15267 15268 if (!osp->os_final_close && !did_dec_count) { 15269 osp->os_open_ref_count--; 15270 did_dec_count = 1; 15271 } 15272 15273 /* 15274 * We can't depend on os_open_ref_count being 0 due to the 15275 * way executables are opened (VN_RELE to match a VOP_OPEN). 15276 */ 15277 #ifdef NOTYET 15278 ASSERT(osp->os_open_ref_count == 0); 15279 #endif 15280 if (osp->os_open_ref_count != 0) { 15281 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15282 "nfs4close_one: should panic here on an " 15283 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15284 "since this is probably the exec problem.")); 15285 15286 osp->os_open_ref_count = 0; 15287 } 15288 15289 /* 15290 * There is the possibility that nfs4close_one() 15291 * for close_type == CLOSE_DELMAP couldn't find the 15292 * open stream, thus couldn't decrement its os_mapcnt; 15293 * therefore we can't use this ASSERT yet. 15294 */ 15295 #ifdef NOTYET 15296 ASSERT(osp->os_mapcnt == 0); 15297 #endif 15298 osp->os_mapcnt = 0; 15299 } 15300 15301 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15302 ASSERT(osp->os_mapcnt >= btopr(len)); 15303 15304 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15305 osp->os_mmap_write -= btopr(len); 15306 if (maxprot & PROT_READ) 15307 osp->os_mmap_read -= btopr(len); 15308 if (maxprot & PROT_EXEC) 15309 osp->os_mmap_read -= btopr(len); 15310 /* mirror the PROT_NONE check in nfs4_addmap() */ 15311 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15312 !(maxprot & PROT_EXEC)) 15313 osp->os_mmap_read -= btopr(len); 15314 osp->os_mapcnt -= btopr(len); 15315 did_dec_count = 1; 15316 } 15317 15318 if (recovonly) { 15319 nfs4_lost_rqst_t lost_rqst; 15320 15321 /* request should not already be in recovery queue */ 15322 ASSERT(lrp == NULL); 15323 nfs4_error_init(ep, EINTR); 15324 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15325 osp, cred_otw, vp); 15326 mutex_exit(&osp->os_sync_lock); 15327 have_sync_lock = 0; 15328 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15329 lost_rqst.lr_op == OP_CLOSE ? 15330 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL); 15331 close_failed = 1; 15332 force_close = 0; 15333 goto close_cleanup; 15334 } 15335 15336 /* 15337 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15338 * we stopped operating on the open owner's <old oo_name, old seqid> 15339 * space, which means we stopped operating on the open stream 15340 * too. So don't go OTW (as the seqid is likely bad, and the 15341 * stateid could be stale, potentially triggering a false 15342 * setclientid), and just clean up the client's internal state. 15343 */ 15344 if (osp->os_orig_oo_name != oop->oo_name) { 15345 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15346 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15347 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15348 "oo_name %" PRIx64")", 15349 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15350 oop->oo_name)); 15351 close_failed = 1; 15352 } 15353 15354 /* If the file failed recovery, just quit. */ 15355 mutex_enter(&rp->r_statelock); 15356 if (rp->r_flags & R4RECOVERR) { 15357 close_failed = 1; 15358 } 15359 mutex_exit(&rp->r_statelock); 15360 15361 /* 15362 * If the force close path failed to obtain start_fop 15363 * then skip the OTW close and just remove the state. 15364 */ 15365 if (close_failed) 15366 goto close_cleanup; 15367 15368 /* 15369 * Fifth, check to see if there are still mapped pages or other 15370 * opens using this open stream. If there are then we can't 15371 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15372 */ 15373 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15374 nfs4_lost_rqst_t new_lost_rqst; 15375 bool_t needrecov = FALSE; 15376 cred_t *odg_cred_otw = NULL; 15377 seqid4 open_dg_seqid = 0; 15378 15379 if (osp->os_delegation) { 15380 /* 15381 * If this open stream was never OPENed OTW then we 15382 * surely can't DOWNGRADE it (especially since the 15383 * osp->open_stateid is really a delegation stateid 15384 * when os_delegation is 1). 15385 */ 15386 if (access_bits & FREAD) 15387 osp->os_share_acc_read--; 15388 if (access_bits & FWRITE) 15389 osp->os_share_acc_write--; 15390 osp->os_share_deny_none--; 15391 nfs4_error_zinit(ep); 15392 goto out; 15393 } 15394 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15395 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15396 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15397 if (needrecov && !isrecov) { 15398 bool_t abort; 15399 nfs4_bseqid_entry_t *bsep = NULL; 15400 15401 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15402 bsep = nfs4_create_bseqid_entry(oop, NULL, 15403 vp, 0, 15404 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15405 open_dg_seqid); 15406 15407 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15408 oop, osp, odg_cred_otw, vp, access_bits, 0); 15409 mutex_exit(&osp->os_sync_lock); 15410 have_sync_lock = 0; 15411 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15412 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15413 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15414 bsep, NULL, NULL); 15415 if (odg_cred_otw) 15416 crfree(odg_cred_otw); 15417 if (bsep) 15418 kmem_free(bsep, sizeof (*bsep)); 15419 15420 if (abort == TRUE) 15421 goto out; 15422 15423 if (did_start_seqid_sync) { 15424 nfs4_end_open_seqid_sync(oop); 15425 did_start_seqid_sync = 0; 15426 } 15427 open_stream_rele(osp, rp); 15428 15429 if (did_start_op) 15430 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15431 &recov_state, FALSE); 15432 if (did_force_recovlock) 15433 nfs_rw_exit(&mi->mi_recovlock); 15434 15435 goto recov_retry; 15436 } else { 15437 if (odg_cred_otw) 15438 crfree(odg_cred_otw); 15439 } 15440 goto out; 15441 } 15442 15443 /* 15444 * If this open stream was created as the results of an open 15445 * while holding a delegation, then just release it; no need 15446 * to do an OTW close. Otherwise do a "normal" OTW close. 15447 */ 15448 if (osp->os_delegation) { 15449 nfs4close_notw(vp, osp, &have_sync_lock); 15450 nfs4_error_zinit(ep); 15451 goto out; 15452 } 15453 15454 /* 15455 * If this stream is not valid, we're done. 15456 */ 15457 if (!osp->os_valid) { 15458 nfs4_error_zinit(ep); 15459 goto out; 15460 } 15461 15462 /* 15463 * Last open or mmap ref has vanished, need to do an OTW close. 15464 * First check to see if a close is still necessary. 15465 */ 15466 if (osp->os_failed_reopen) { 15467 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15468 "don't close OTW osp %p since reopen failed.", 15469 (void *)osp)); 15470 /* 15471 * Reopen of the open stream failed, hence the 15472 * stateid of the open stream is invalid/stale, and 15473 * sending this OTW would incorrectly cause another 15474 * round of recovery. In this case, we need to set 15475 * the 'os_valid' bit to 0 so another thread doesn't 15476 * come in and re-open this open stream before 15477 * this "closing" thread cleans up state (decrementing 15478 * the nfs4_server_t's state_ref_count and decrementing 15479 * the os_ref_count). 15480 */ 15481 osp->os_valid = 0; 15482 /* 15483 * This removes the reference obtained at OPEN; ie, 15484 * when the open stream structure was created. 15485 * 15486 * We don't have to worry about calling 'open_stream_rele' 15487 * since we our currently holding a reference to this 15488 * open stream which means the count can not go to 0 with 15489 * this decrement. 15490 */ 15491 ASSERT(osp->os_ref_count >= 2); 15492 osp->os_ref_count--; 15493 nfs4_error_zinit(ep); 15494 close_failed = 0; 15495 goto close_cleanup; 15496 } 15497 15498 ASSERT(osp->os_ref_count > 1); 15499 15500 /* 15501 * Sixth, try the CLOSE OTW. 15502 */ 15503 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15504 close_type, ep, &have_sync_lock); 15505 15506 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15507 /* 15508 * Let the recovery thread be responsible for 15509 * removing the state for CLOSE. 15510 */ 15511 close_failed = 1; 15512 force_close = 0; 15513 retry = 0; 15514 } 15515 15516 /* See if we need to retry with a different cred */ 15517 if ((ep->error == EACCES || 15518 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15519 cred_otw != cr) { 15520 crfree(cred_otw); 15521 cred_otw = cr; 15522 crhold(cred_otw); 15523 retry = 1; 15524 } 15525 15526 if (ep->error || ep->stat) 15527 close_failed = 1; 15528 15529 if (retry && !isrecov && num_retries-- > 0) { 15530 if (have_sync_lock) { 15531 mutex_exit(&osp->os_sync_lock); 15532 have_sync_lock = 0; 15533 } 15534 if (did_start_seqid_sync) { 15535 nfs4_end_open_seqid_sync(oop); 15536 did_start_seqid_sync = 0; 15537 } 15538 open_stream_rele(osp, rp); 15539 15540 if (did_start_op) 15541 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15542 &recov_state, FALSE); 15543 if (did_force_recovlock) 15544 nfs_rw_exit(&mi->mi_recovlock); 15545 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15546 "nfs4close_one: need to retry the close " 15547 "operation")); 15548 goto recov_retry; 15549 } 15550 close_cleanup: 15551 /* 15552 * Seventh and lastly, process our results. 15553 */ 15554 if (close_failed && force_close) { 15555 /* 15556 * It's ok to drop and regrab the 'os_sync_lock' since 15557 * nfs4close_notw() will recheck to make sure the 15558 * "close"/removal of state should happen. 15559 */ 15560 if (!have_sync_lock) { 15561 mutex_enter(&osp->os_sync_lock); 15562 have_sync_lock = 1; 15563 } 15564 /* 15565 * This is last call, remove the ref on the open 15566 * stream created by open and clean everything up. 15567 */ 15568 osp->os_pending_close = 0; 15569 nfs4close_notw(vp, osp, &have_sync_lock); 15570 nfs4_error_zinit(ep); 15571 } 15572 15573 if (!close_failed) { 15574 if (have_sync_lock) { 15575 osp->os_pending_close = 0; 15576 mutex_exit(&osp->os_sync_lock); 15577 have_sync_lock = 0; 15578 } else { 15579 mutex_enter(&osp->os_sync_lock); 15580 osp->os_pending_close = 0; 15581 mutex_exit(&osp->os_sync_lock); 15582 } 15583 if (did_start_op && recov_state.rs_sp != NULL) { 15584 mutex_enter(&recov_state.rs_sp->s_lock); 15585 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15586 mutex_exit(&recov_state.rs_sp->s_lock); 15587 } else { 15588 nfs4_dec_state_ref_count(mi); 15589 } 15590 nfs4_error_zinit(ep); 15591 } 15592 15593 out: 15594 if (have_sync_lock) 15595 mutex_exit(&osp->os_sync_lock); 15596 if (did_start_op) 15597 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15598 recovonly ? TRUE : FALSE); 15599 if (did_force_recovlock) 15600 nfs_rw_exit(&mi->mi_recovlock); 15601 if (cred_otw) 15602 crfree(cred_otw); 15603 if (osp) 15604 open_stream_rele(osp, rp); 15605 if (oop) { 15606 if (did_start_seqid_sync) 15607 nfs4_end_open_seqid_sync(oop); 15608 open_owner_rele(oop); 15609 } 15610 } 15611 15612 /* 15613 * Convert information returned by the server in the LOCK4denied 15614 * structure to the form required by fcntl. 15615 */ 15616 static void 15617 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15618 { 15619 nfs4_lo_name_t *lo; 15620 15621 #ifdef DEBUG 15622 if (denied_to_flk_debug) { 15623 lockt_denied_debug = lockt_denied; 15624 debug_enter("lockt_denied"); 15625 } 15626 #endif 15627 15628 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15629 flk->l_whence = 0; /* aka SEEK_SET */ 15630 flk->l_start = lockt_denied->offset; 15631 flk->l_len = lockt_denied->length; 15632 15633 /* 15634 * If the blocking clientid matches our client id, then we can 15635 * interpret the lockowner (since we built it). If not, then 15636 * fabricate a sysid and pid. Note that the l_sysid field 15637 * in *flk already has the local sysid. 15638 */ 15639 15640 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15641 15642 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15643 lo = (nfs4_lo_name_t *) 15644 lockt_denied->owner.owner_val; 15645 15646 flk->l_pid = lo->ln_pid; 15647 } else { 15648 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15649 "denied_to_flk: bad lock owner length\n")); 15650 15651 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15652 } 15653 } else { 15654 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15655 "denied_to_flk: foreign clientid\n")); 15656 15657 /* 15658 * Construct a new sysid which should be different from 15659 * sysids of other systems. 15660 */ 15661 15662 flk->l_sysid++; 15663 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15664 } 15665 } 15666 15667 static pid_t 15668 lo_to_pid(lock_owner4 *lop) 15669 { 15670 pid_t pid = 0; 15671 uchar_t *cp; 15672 int i; 15673 15674 cp = (uchar_t *)&lop->clientid; 15675 15676 for (i = 0; i < sizeof (lop->clientid); i++) 15677 pid += (pid_t)*cp++; 15678 15679 cp = (uchar_t *)lop->owner_val; 15680 15681 for (i = 0; i < lop->owner_len; i++) 15682 pid += (pid_t)*cp++; 15683 15684 return (pid); 15685 } 15686 15687 /* 15688 * Given a lock pointer, returns the length of that lock. 15689 * "end" is the last locked offset the "l_len" covers from 15690 * the start of the lock. 15691 */ 15692 static off64_t 15693 lock_to_end(flock64_t *lock) 15694 { 15695 off64_t lock_end; 15696 15697 if (lock->l_len == 0) 15698 lock_end = (off64_t)MAXEND; 15699 else 15700 lock_end = lock->l_start + lock->l_len - 1; 15701 15702 return (lock_end); 15703 } 15704 15705 /* 15706 * Given the end of a lock, it will return you the length "l_len" for that lock. 15707 */ 15708 static off64_t 15709 end_to_len(off64_t start, off64_t end) 15710 { 15711 off64_t lock_len; 15712 15713 ASSERT(end >= start); 15714 if (end == MAXEND) 15715 lock_len = 0; 15716 else 15717 lock_len = end - start + 1; 15718 15719 return (lock_len); 15720 } 15721 15722 /* 15723 * On given end for a lock it determines if it is the last locked offset 15724 * or not, if so keeps it as is, else adds one to return the length for 15725 * valid start. 15726 */ 15727 static off64_t 15728 start_check(off64_t x) 15729 { 15730 if (x == MAXEND) 15731 return (x); 15732 else 15733 return (x + 1); 15734 } 15735 15736 /* 15737 * See if these two locks overlap, and if so return 1; 15738 * otherwise, return 0. 15739 */ 15740 static int 15741 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15742 { 15743 off64_t llfp_end, curfp_end; 15744 15745 llfp_end = lock_to_end(llfp); 15746 curfp_end = lock_to_end(curfp); 15747 15748 if (((llfp_end >= curfp->l_start) && 15749 (llfp->l_start <= curfp->l_start)) || 15750 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15751 return (1); 15752 return (0); 15753 } 15754 15755 /* 15756 * Determine what the intersecting lock region is, and add that to the 15757 * 'nl_llpp' locklist in increasing order (by l_start). 15758 */ 15759 static void 15760 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15761 locklist_t **nl_llpp, vnode_t *vp) 15762 { 15763 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15764 off64_t lost_flp_end, local_flp_end, len, start; 15765 15766 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15767 15768 if (!locks_intersect(lost_flp, local_flp)) 15769 return; 15770 15771 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15772 "locks intersect")); 15773 15774 lost_flp_end = lock_to_end(lost_flp); 15775 local_flp_end = lock_to_end(local_flp); 15776 15777 /* Find the starting point of the intersecting region */ 15778 if (local_flp->l_start > lost_flp->l_start) 15779 start = local_flp->l_start; 15780 else 15781 start = lost_flp->l_start; 15782 15783 /* Find the lenght of the intersecting region */ 15784 if (lost_flp_end < local_flp_end) 15785 len = end_to_len(start, lost_flp_end); 15786 else 15787 len = end_to_len(start, local_flp_end); 15788 15789 /* 15790 * Prepare the flock structure for the intersection found and insert 15791 * it into the new list in increasing l_start order. This list contains 15792 * intersections of locks registered by the client with the local host 15793 * and the lost lock. 15794 * The lock type of this lock is the same as that of the local_flp. 15795 */ 15796 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15797 intersect_llp->ll_flock.l_start = start; 15798 intersect_llp->ll_flock.l_len = len; 15799 intersect_llp->ll_flock.l_type = local_flp->l_type; 15800 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15801 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15802 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15803 intersect_llp->ll_vp = vp; 15804 15805 tmp_fllp = *nl_llpp; 15806 cur_fllp = NULL; 15807 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15808 intersect_llp->ll_flock.l_start) { 15809 cur_fllp = tmp_fllp; 15810 tmp_fllp = tmp_fllp->ll_next; 15811 } 15812 if (cur_fllp == NULL) { 15813 /* first on the list */ 15814 intersect_llp->ll_next = *nl_llpp; 15815 *nl_llpp = intersect_llp; 15816 } else { 15817 intersect_llp->ll_next = cur_fllp->ll_next; 15818 cur_fllp->ll_next = intersect_llp; 15819 } 15820 15821 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15822 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15823 intersect_llp->ll_flock.l_start, 15824 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15825 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15826 } 15827 15828 /* 15829 * Our local locking current state is potentially different than 15830 * what the NFSv4 server thinks we have due to a lost lock that was 15831 * resent and then received. We need to reset our "NFSv4" locking 15832 * state to match the current local locking state for this pid since 15833 * that is what the user/application sees as what the world is. 15834 * 15835 * We cannot afford to drop the open/lock seqid sync since then we can 15836 * get confused about what the current local locking state "is" versus 15837 * "was". 15838 * 15839 * If we are unable to fix up the locks, we send SIGLOST to the affected 15840 * process. This is not done if the filesystem has been forcibly 15841 * unmounted, in case the process has already exited and a new process 15842 * exists with the same pid. 15843 */ 15844 static void 15845 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15846 nfs4_lock_owner_t *lop) 15847 { 15848 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15849 mntinfo4_t *mi = VTOMI4(vp); 15850 const int cmd = F_SETLK; 15851 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15852 flock64_t ul_fl; 15853 15854 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15855 "nfs4_reinstitute_local_lock_state")); 15856 15857 /* 15858 * Find active locks for this vp from the local locking code. 15859 * Scan through this list and find out the locks that intersect with 15860 * the lost lock. Once we find the lock that intersects, add the 15861 * intersection area as a new lock to a new list "ri_llp". The lock 15862 * type of the intersection region lock added to ri_llp is the same 15863 * as that found in the active lock list, "list". The intersecting 15864 * region locks are added to ri_llp in increasing l_start order. 15865 */ 15866 ASSERT(nfs_zone() == mi->mi_zone); 15867 15868 locks = flk_active_locks_for_vp(vp); 15869 ri_llp = NULL; 15870 15871 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15872 ASSERT(llp->ll_vp == vp); 15873 /* 15874 * Pick locks that belong to this pid/lockowner 15875 */ 15876 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15877 continue; 15878 15879 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15880 } 15881 15882 /* 15883 * Now we have the list of intersections with the lost lock. These are 15884 * the locks that were/are active before the server replied to the 15885 * last/lost lock. Issue these locks to the server here. Playing these 15886 * locks to the server will re-establish aur current local locking state 15887 * with the v4 server. 15888 * If we get an error, send SIGLOST to the application for that lock. 15889 */ 15890 15891 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15892 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15893 "nfs4_reinstitute_local_lock_state: need to issue " 15894 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15895 llp->ll_flock.l_start, 15896 llp->ll_flock.l_start + llp->ll_flock.l_len, 15897 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15898 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15899 /* 15900 * No need to relock what we already have 15901 */ 15902 if (llp->ll_flock.l_type == lost_flp->l_type) 15903 continue; 15904 15905 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15906 } 15907 15908 /* 15909 * Now keeping the start of the lost lock as our reference parse the 15910 * newly created ri_llp locklist to find the ranges that we have locked 15911 * with the v4 server but not in the current local locking. We need 15912 * to unlock these ranges. 15913 * These ranges can also be reffered to as those ranges, where the lost 15914 * lock does not overlap with the locks in the ri_llp but are locked 15915 * since the server replied to the lost lock. 15916 */ 15917 cur_start = lost_flp->l_start; 15918 lost_flp_end = lock_to_end(lost_flp); 15919 15920 ul_fl.l_type = F_UNLCK; 15921 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15922 ul_fl.l_sysid = lost_flp->l_sysid; 15923 ul_fl.l_pid = lost_flp->l_pid; 15924 15925 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15926 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15927 15928 if (llp->ll_flock.l_start <= cur_start) { 15929 cur_start = start_check(llp_ll_flock_end); 15930 continue; 15931 } 15932 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15933 "nfs4_reinstitute_local_lock_state: " 15934 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15935 cur_start, llp->ll_flock.l_start)); 15936 15937 ul_fl.l_start = cur_start; 15938 ul_fl.l_len = end_to_len(cur_start, 15939 (llp->ll_flock.l_start - 1)); 15940 15941 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15942 cur_start = start_check(llp_ll_flock_end); 15943 } 15944 15945 /* 15946 * In the case where the lost lock ends after all intersecting locks, 15947 * unlock the last part of the lost lock range. 15948 */ 15949 if (cur_start != start_check(lost_flp_end)) { 15950 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15951 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15952 "lost lock region [%"PRIx64" - %"PRIx64"]", 15953 cur_start, lost_flp->l_start + lost_flp->l_len)); 15954 15955 ul_fl.l_start = cur_start; 15956 /* 15957 * Is it an to-EOF lock? if so unlock till the end 15958 */ 15959 if (lost_flp->l_len == 0) 15960 ul_fl.l_len = 0; 15961 else 15962 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15963 15964 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15965 } 15966 15967 if (locks != NULL) 15968 flk_free_locklist(locks); 15969 15970 /* Free up our newly created locklist */ 15971 for (llp = ri_llp; llp != NULL; ) { 15972 tmp_llp = llp->ll_next; 15973 kmem_free(llp, sizeof (locklist_t)); 15974 llp = tmp_llp; 15975 } 15976 15977 /* 15978 * Now return back to the original calling nfs4frlock() 15979 * and let us naturally drop our seqid syncs. 15980 */ 15981 } 15982 15983 /* 15984 * Create a lost state record for the given lock reinstantiation request 15985 * and push it onto the lost state queue. 15986 */ 15987 static void 15988 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15989 nfs4_lock_owner_t *lop) 15990 { 15991 nfs4_lost_rqst_t req; 15992 nfs_lock_type4 locktype; 15993 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15994 15995 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15996 15997 locktype = flk_to_locktype(cmd, flk->l_type); 15998 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 15999 NULL, NULL, lop, flk, &req, cr, vp); 16000 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 16001 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 16002 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 16003 NULL, NULL, NULL); 16004 } 16005