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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 28 * All Rights Reserved 29 */ 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/param.h> 34 #include <sys/types.h> 35 #include <sys/systm.h> 36 #include <sys/cred.h> 37 #include <sys/time.h> 38 #include <sys/vnode.h> 39 #include <sys/vfs.h> 40 #include <sys/vfs_opreg.h> 41 #include <sys/file.h> 42 #include <sys/filio.h> 43 #include <sys/uio.h> 44 #include <sys/buf.h> 45 #include <sys/mman.h> 46 #include <sys/pathname.h> 47 #include <sys/dirent.h> 48 #include <sys/debug.h> 49 #include <sys/vmsystm.h> 50 #include <sys/fcntl.h> 51 #include <sys/flock.h> 52 #include <sys/swap.h> 53 #include <sys/errno.h> 54 #include <sys/strsubr.h> 55 #include <sys/sysmacros.h> 56 #include <sys/kmem.h> 57 #include <sys/cmn_err.h> 58 #include <sys/pathconf.h> 59 #include <sys/utsname.h> 60 #include <sys/dnlc.h> 61 #include <sys/acl.h> 62 #include <sys/systeminfo.h> 63 #include <sys/policy.h> 64 #include <sys/sdt.h> 65 #include <sys/list.h> 66 #include <sys/stat.h> 67 68 #include <rpc/types.h> 69 #include <rpc/auth.h> 70 #include <rpc/clnt.h> 71 72 #include <nfs/nfs.h> 73 #include <nfs/nfs_clnt.h> 74 #include <nfs/nfs_acl.h> 75 #include <nfs/lm.h> 76 #include <nfs/nfs4.h> 77 #include <nfs/nfs4_kprot.h> 78 #include <nfs/rnode4.h> 79 #include <nfs/nfs4_clnt.h> 80 81 #include <vm/hat.h> 82 #include <vm/as.h> 83 #include <vm/page.h> 84 #include <vm/pvn.h> 85 #include <vm/seg.h> 86 #include <vm/seg_map.h> 87 #include <vm/seg_kpm.h> 88 #include <vm/seg_vn.h> 89 90 #include <fs/fs_subr.h> 91 92 #include <sys/ddi.h> 93 #include <sys/int_fmtio.h> 94 95 typedef struct { 96 nfs4_ga_res_t *di_garp; 97 cred_t *di_cred; 98 hrtime_t di_time_call; 99 } dirattr_info_t; 100 101 typedef enum nfs4_acl_op { 102 NFS4_ACL_GET, 103 NFS4_ACL_SET 104 } nfs4_acl_op_t; 105 106 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 107 char *, dirattr_info_t *); 108 109 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 110 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 111 nfs4_error_t *, int *); 112 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 113 cred_t *); 114 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 115 stable_how4 *); 116 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 117 cred_t *, bool_t, struct uio *); 118 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 119 vsecattr_t *); 120 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 121 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 122 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 123 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 124 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 125 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 126 int, vnode_t **, cred_t *); 127 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 128 cred_t *, int, int, enum createmode4, int); 129 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 130 caller_context_t *); 131 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 132 vnode_t *, char *, cred_t *, nfsstat4 *); 133 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 134 vnode_t *, char *, cred_t *, nfsstat4 *); 135 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 136 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 137 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 138 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 139 page_t *[], size_t, struct seg *, caddr_t, 140 enum seg_rw, cred_t *); 141 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 142 cred_t *); 143 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 144 int, cred_t *); 145 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 146 int, cred_t *); 147 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 148 static void nfs4_set_mod(vnode_t *); 149 static void nfs4_get_commit(vnode_t *); 150 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 151 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 152 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 153 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 154 cred_t *); 155 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 156 cred_t *); 157 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 158 hrtime_t, vnode_t *, cred_t *); 159 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 160 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 161 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 162 u_offset_t); 163 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 164 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 165 static cred_t *state_to_cred(nfs4_open_stream_t *); 166 static int vtoname(vnode_t *, char *, ssize_t); 167 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 168 static pid_t lo_to_pid(lock_owner4 *); 169 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 170 cred_t *, nfs4_lock_owner_t *); 171 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 172 nfs4_lock_owner_t *); 173 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **); 174 static void nfs4_delmap_callback(struct as *, void *, uint_t); 175 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 176 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 177 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 178 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 179 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 180 uid_t, gid_t, int); 181 182 /* 183 * Routines that implement the setting of v4 args for the misc. ops 184 */ 185 static void nfs4args_lock_free(nfs_argop4 *); 186 static void nfs4args_lockt_free(nfs_argop4 *); 187 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 188 int, rnode4_t *, cred_t *, bitmap4, int *, 189 nfs4_stateid_types_t *); 190 static void nfs4args_setattr_free(nfs_argop4 *); 191 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 192 bitmap4); 193 static void nfs4args_verify_free(nfs_argop4 *); 194 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 195 WRITE4args **, nfs4_stateid_types_t *); 196 197 /* 198 * These are the vnode ops functions that implement the vnode interface to 199 * the networked file system. See more comments below at nfs4_vnodeops. 200 */ 201 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *); 202 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *, 203 caller_context_t *); 204 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 205 caller_context_t *); 206 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 207 caller_context_t *); 208 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *, 209 caller_context_t *); 210 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 211 caller_context_t *); 212 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *); 213 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *, 214 caller_context_t *); 215 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *); 216 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 217 int, vnode_t **, cred_t *, int, caller_context_t *, 218 vsecattr_t *); 219 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *, 220 int); 221 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *, 222 caller_context_t *, int); 223 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 224 caller_context_t *, int); 225 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **, 226 cred_t *, caller_context_t *, int, vsecattr_t *); 227 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *, 228 caller_context_t *, int); 229 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 230 cred_t *, caller_context_t *, int); 231 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *, 232 caller_context_t *, int); 233 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *); 234 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 235 page_t *[], size_t, struct seg *, caddr_t, 236 enum seg_rw, cred_t *, caller_context_t *); 237 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *, 238 caller_context_t *); 239 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t, 240 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 241 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 242 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 243 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *); 244 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 245 struct flk_callback *, cred_t *, caller_context_t *); 246 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 247 cred_t *, caller_context_t *); 248 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 249 uint_t, uint_t, uint_t, cred_t *, caller_context_t *); 250 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 251 cred_t *, caller_context_t *); 252 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *, 253 caller_context_t *); 254 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 255 caller_context_t *); 256 /* 257 * These vnode ops are required to be called from outside this source file, 258 * e.g. by ephemeral mount stub vnode ops, and so may not be declared 259 * as static. 260 */ 261 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *, 262 caller_context_t *); 263 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *); 264 int nfs4_lookup(vnode_t *, char *, vnode_t **, 265 struct pathname *, int, vnode_t *, cred_t *, 266 caller_context_t *, int *, pathname_t *); 267 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *); 268 int nfs4_rwlock(vnode_t *, int, caller_context_t *); 269 void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 270 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *); 271 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *, 272 caller_context_t *); 273 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 274 caller_context_t *); 275 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *, 276 caller_context_t *); 277 278 /* 279 * Used for nfs4_commit_vp() to indicate if we should 280 * wait on pending writes. 281 */ 282 #define NFS4_WRITE_NOWAIT 0 283 #define NFS4_WRITE_WAIT 1 284 285 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */ 286 287 /* 288 * Error flags used to pass information about certain special errors 289 * which need to be handled specially. 290 */ 291 #define NFS_EOF -98 292 #define NFS_VERF_MISMATCH -97 293 294 /* 295 * Flags used to differentiate between which operation drove the 296 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 297 */ 298 #define NFS4_CLOSE_OP 0x1 299 #define NFS4_DELMAP_OP 0x2 300 #define NFS4_INACTIVE_OP 0x3 301 302 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 303 304 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 305 #define ALIGN64(x, ptr, sz) \ 306 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 307 if (x) { \ 308 x = sizeof (uint64_t) - (x); \ 309 sz -= (x); \ 310 ptr += (x); \ 311 } 312 313 #ifdef DEBUG 314 int nfs4_client_attr_debug = 0; 315 int nfs4_client_state_debug = 0; 316 int nfs4_client_shadow_debug = 0; 317 int nfs4_client_lock_debug = 0; 318 int nfs4_seqid_sync = 0; 319 int nfs4_client_map_debug = 0; 320 static int nfs4_pageio_debug = 0; 321 int nfs4_client_inactive_debug = 0; 322 int nfs4_client_recov_debug = 0; 323 int nfs4_client_failover_debug = 0; 324 int nfs4_client_call_debug = 0; 325 int nfs4_client_lookup_debug = 0; 326 int nfs4_client_zone_debug = 0; 327 int nfs4_lost_rqst_debug = 0; 328 int nfs4_rdattrerr_debug = 0; 329 int nfs4_open_stream_debug = 0; 330 331 int nfs4read_error_inject; 332 333 static int nfs4_create_misses = 0; 334 335 static int nfs4_readdir_cache_shorts = 0; 336 static int nfs4_readdir_readahead = 0; 337 338 static int nfs4_bio_do_stop = 0; 339 340 static int nfs4_lostpage = 0; /* number of times we lost original page */ 341 342 int nfs4_mmap_debug = 0; 343 344 static int nfs4_pathconf_cache_hits = 0; 345 static int nfs4_pathconf_cache_misses = 0; 346 347 int nfs4close_all_cnt; 348 int nfs4close_one_debug = 0; 349 int nfs4close_notw_debug = 0; 350 351 int denied_to_flk_debug = 0; 352 void *lockt_denied_debug; 353 354 #endif 355 356 /* 357 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 358 * or NFS4ERR_RESOURCE. 359 */ 360 static int confirm_retry_sec = 30; 361 362 static int nfs4_lookup_neg_cache = 1; 363 364 /* 365 * number of pages to read ahead 366 * optimized for 100 base-T. 367 */ 368 static int nfs4_nra = 4; 369 370 static int nfs4_do_symlink_cache = 1; 371 372 static int nfs4_pathconf_disable_cache = 0; 373 374 /* 375 * These are the vnode ops routines which implement the vnode interface to 376 * the networked file system. These routines just take their parameters, 377 * make them look networkish by putting the right info into interface structs, 378 * and then calling the appropriate remote routine(s) to do the work. 379 * 380 * Note on directory name lookup cacheing: If we detect a stale fhandle, 381 * we purge the directory cache relative to that vnode. This way, the 382 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 383 * more details on rnode locking. 384 */ 385 386 struct vnodeops *nfs4_vnodeops; 387 388 const fs_operation_def_t nfs4_vnodeops_template[] = { 389 VOPNAME_OPEN, { .vop_open = nfs4_open }, 390 VOPNAME_CLOSE, { .vop_close = nfs4_close }, 391 VOPNAME_READ, { .vop_read = nfs4_read }, 392 VOPNAME_WRITE, { .vop_write = nfs4_write }, 393 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl }, 394 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr }, 395 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr }, 396 VOPNAME_ACCESS, { .vop_access = nfs4_access }, 397 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup }, 398 VOPNAME_CREATE, { .vop_create = nfs4_create }, 399 VOPNAME_REMOVE, { .vop_remove = nfs4_remove }, 400 VOPNAME_LINK, { .vop_link = nfs4_link }, 401 VOPNAME_RENAME, { .vop_rename = nfs4_rename }, 402 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir }, 403 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir }, 404 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir }, 405 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink }, 406 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink }, 407 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync }, 408 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive }, 409 VOPNAME_FID, { .vop_fid = nfs4_fid }, 410 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock }, 411 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock }, 412 VOPNAME_SEEK, { .vop_seek = nfs4_seek }, 413 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock }, 414 VOPNAME_SPACE, { .vop_space = nfs4_space }, 415 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp }, 416 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage }, 417 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage }, 418 VOPNAME_MAP, { .vop_map = nfs4_map }, 419 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap }, 420 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap }, 421 /* no separate nfs4_dump */ 422 VOPNAME_DUMP, { .vop_dump = nfs_dump }, 423 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf }, 424 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio }, 425 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose }, 426 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr }, 427 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr }, 428 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock }, 429 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 430 NULL, NULL 431 }; 432 433 /* 434 * The following are subroutines and definitions to set args or get res 435 * for the different nfsv4 ops 436 */ 437 438 void 439 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 440 { 441 int i; 442 443 for (i = 0; i < arglen; i++) { 444 if (argop[i].argop == OP_LOOKUP) { 445 kmem_free( 446 argop[i].nfs_argop4_u.oplookup. 447 objname.utf8string_val, 448 argop[i].nfs_argop4_u.oplookup. 449 objname.utf8string_len); 450 } 451 } 452 } 453 454 static void 455 nfs4args_lock_free(nfs_argop4 *argop) 456 { 457 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 458 459 if (locker->new_lock_owner == TRUE) { 460 open_to_lock_owner4 *open_owner; 461 462 open_owner = &locker->locker4_u.open_owner; 463 if (open_owner->lock_owner.owner_val != NULL) { 464 kmem_free(open_owner->lock_owner.owner_val, 465 open_owner->lock_owner.owner_len); 466 } 467 } 468 } 469 470 static void 471 nfs4args_lockt_free(nfs_argop4 *argop) 472 { 473 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 474 475 if (lowner->owner_val != NULL) { 476 kmem_free(lowner->owner_val, lowner->owner_len); 477 } 478 } 479 480 static void 481 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 482 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 483 nfs4_stateid_types_t *sid_types) 484 { 485 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 486 mntinfo4_t *mi; 487 488 argop->argop = OP_SETATTR; 489 /* 490 * The stateid is set to 0 if client is not modifying the size 491 * and otherwise to whatever nfs4_get_stateid() returns. 492 * 493 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 494 * state struct could be found for the process/file pair. We may 495 * want to change this in the future (by OPENing the file). See 496 * bug # 4474852. 497 */ 498 if (vap->va_mask & AT_SIZE) { 499 500 ASSERT(rp != NULL); 501 mi = VTOMI4(RTOV4(rp)); 502 503 argop->nfs_argop4_u.opsetattr.stateid = 504 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 505 OP_SETATTR, sid_types, FALSE); 506 } else { 507 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 508 sizeof (stateid4)); 509 } 510 511 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 512 if (*error) 513 bzero(attr, sizeof (*attr)); 514 } 515 516 static void 517 nfs4args_setattr_free(nfs_argop4 *argop) 518 { 519 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 520 } 521 522 static int 523 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 524 bitmap4 supp) 525 { 526 fattr4 *attr; 527 int error = 0; 528 529 argop->argop = op; 530 switch (op) { 531 case OP_VERIFY: 532 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 533 break; 534 case OP_NVERIFY: 535 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 536 break; 537 default: 538 return (EINVAL); 539 } 540 if (!error) 541 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 542 if (error) 543 bzero(attr, sizeof (*attr)); 544 return (error); 545 } 546 547 static void 548 nfs4args_verify_free(nfs_argop4 *argop) 549 { 550 switch (argop->argop) { 551 case OP_VERIFY: 552 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 553 break; 554 case OP_NVERIFY: 555 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 556 break; 557 default: 558 break; 559 } 560 } 561 562 static void 563 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 564 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 565 { 566 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 567 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 568 569 argop->argop = OP_WRITE; 570 wargs->stable = stable; 571 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 572 mi, OP_WRITE, sid_tp); 573 wargs->mblk = NULL; 574 *wargs_pp = wargs; 575 } 576 577 void 578 nfs4args_copen_free(OPEN4cargs *open_args) 579 { 580 if (open_args->owner.owner_val) { 581 kmem_free(open_args->owner.owner_val, 582 open_args->owner.owner_len); 583 } 584 if ((open_args->opentype == OPEN4_CREATE) && 585 (open_args->mode != EXCLUSIVE4)) { 586 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 587 } 588 } 589 590 /* 591 * XXX: This is referenced in modstubs.s 592 */ 593 struct vnodeops * 594 nfs4_getvnodeops(void) 595 { 596 return (nfs4_vnodeops); 597 } 598 599 /* 600 * The OPEN operation opens a regular file. 601 */ 602 /*ARGSUSED3*/ 603 static int 604 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 605 { 606 vnode_t *dvp = NULL; 607 rnode4_t *rp, *drp; 608 int error; 609 int just_been_created; 610 char fn[MAXNAMELEN]; 611 612 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 613 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 614 return (EIO); 615 rp = VTOR4(*vpp); 616 617 /* 618 * Check to see if opening something besides a regular file; 619 * if so skip the OTW call 620 */ 621 if ((*vpp)->v_type != VREG) { 622 error = nfs4_open_non_reg_file(vpp, flag, cr); 623 return (error); 624 } 625 626 /* 627 * XXX - would like a check right here to know if the file is 628 * executable or not, so as to skip OTW 629 */ 630 631 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 632 return (error); 633 634 drp = VTOR4(dvp); 635 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 636 return (EINTR); 637 638 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) { 639 nfs_rw_exit(&drp->r_rwlock); 640 return (error); 641 } 642 643 /* 644 * See if this file has just been CREATEd. 645 * If so, clear the flag and update the dnlc, which was previously 646 * skipped in nfs4_create. 647 * XXX need better serilization on this. 648 * XXX move this into the nf4open_otw call, after we have 649 * XXX acquired the open owner seqid sync. 650 */ 651 mutex_enter(&rp->r_statev4_lock); 652 if (rp->created_v4) { 653 rp->created_v4 = 0; 654 mutex_exit(&rp->r_statev4_lock); 655 656 dnlc_update(dvp, fn, *vpp); 657 /* This is needed so we don't bump the open ref count */ 658 just_been_created = 1; 659 } else { 660 mutex_exit(&rp->r_statev4_lock); 661 just_been_created = 0; 662 } 663 664 /* 665 * If caller specified O_TRUNC/FTRUNC, then be sure to set 666 * FWRITE (to drive successful setattr(size=0) after open) 667 */ 668 if (flag & FTRUNC) 669 flag |= FWRITE; 670 671 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 672 just_been_created); 673 674 if (!error && !((*vpp)->v_flag & VROOT)) 675 dnlc_update(dvp, fn, *vpp); 676 677 nfs_rw_exit(&drp->r_rwlock); 678 679 /* release the hold from vtodv */ 680 VN_RELE(dvp); 681 682 /* exchange the shadow for the master vnode, if needed */ 683 684 if (error == 0 && IS_SHADOW(*vpp, rp)) 685 sv_exchange(vpp); 686 687 return (error); 688 } 689 690 /* 691 * See if there's a "lost open" request to be saved and recovered. 692 */ 693 static void 694 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 695 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 696 vnode_t *dvp, OPEN4cargs *open_args) 697 { 698 vfs_t *vfsp; 699 char *srccfp; 700 701 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 702 703 if (error != ETIMEDOUT && error != EINTR && 704 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 705 lost_rqstp->lr_op = 0; 706 return; 707 } 708 709 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 710 "nfs4open_save_lost_rqst: error %d", error)); 711 712 lost_rqstp->lr_op = OP_OPEN; 713 714 /* 715 * The vp (if it is not NULL) and dvp are held and rele'd via 716 * the recovery code. See nfs4_save_lost_rqst. 717 */ 718 lost_rqstp->lr_vp = vp; 719 lost_rqstp->lr_dvp = dvp; 720 lost_rqstp->lr_oop = oop; 721 lost_rqstp->lr_osp = NULL; 722 lost_rqstp->lr_lop = NULL; 723 lost_rqstp->lr_cr = cr; 724 lost_rqstp->lr_flk = NULL; 725 lost_rqstp->lr_oacc = open_args->share_access; 726 lost_rqstp->lr_odeny = open_args->share_deny; 727 lost_rqstp->lr_oclaim = open_args->claim; 728 if (open_args->claim == CLAIM_DELEGATE_CUR) { 729 lost_rqstp->lr_ostateid = 730 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 731 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 732 } else { 733 srccfp = open_args->open_claim4_u.cfile; 734 } 735 lost_rqstp->lr_ofile.utf8string_len = 0; 736 lost_rqstp->lr_ofile.utf8string_val = NULL; 737 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 738 lost_rqstp->lr_putfirst = FALSE; 739 } 740 741 struct nfs4_excl_time { 742 uint32 seconds; 743 uint32 nseconds; 744 }; 745 746 /* 747 * The OPEN operation creates and/or opens a regular file 748 * 749 * ARGSUSED 750 */ 751 static int 752 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 753 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 754 enum createmode4 createmode, int file_just_been_created) 755 { 756 rnode4_t *rp; 757 rnode4_t *drp = VTOR4(dvp); 758 vnode_t *vp = NULL; 759 vnode_t *vpi = *vpp; 760 bool_t needrecov = FALSE; 761 762 int doqueue = 1; 763 764 COMPOUND4args_clnt args; 765 COMPOUND4res_clnt res; 766 nfs_argop4 *argop; 767 nfs_resop4 *resop; 768 int argoplist_size; 769 int idx_open, idx_fattr; 770 771 GETFH4res *gf_res = NULL; 772 OPEN4res *op_res = NULL; 773 nfs4_ga_res_t *garp; 774 fattr4 *attr = NULL; 775 struct nfs4_excl_time verf; 776 bool_t did_excl_setup = FALSE; 777 int created_osp; 778 779 OPEN4cargs *open_args; 780 nfs4_open_owner_t *oop = NULL; 781 nfs4_open_stream_t *osp = NULL; 782 seqid4 seqid = 0; 783 bool_t retry_open = FALSE; 784 nfs4_recov_state_t recov_state; 785 nfs4_lost_rqst_t lost_rqst; 786 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 787 hrtime_t t; 788 int acc = 0; 789 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 790 cred_t *ncr = NULL; 791 792 nfs4_sharedfh_t *otw_sfh; 793 nfs4_sharedfh_t *orig_sfh; 794 int fh_differs = 0; 795 int numops, setgid_flag; 796 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 797 798 /* 799 * Make sure we properly deal with setting the right gid on 800 * a newly created file to reflect the parent's setgid bit 801 */ 802 setgid_flag = 0; 803 if (create_flag && in_va) { 804 805 /* 806 * If the parent's directory has the setgid bit set 807 * _and_ the client was able to get a valid mapping 808 * for the parent dir's owner_group, we want to 809 * append NVERIFY(owner_group == dva.va_gid) and 810 * SETATTR to the CREATE compound. 811 */ 812 mutex_enter(&drp->r_statelock); 813 if (drp->r_attr.va_mode & VSGID && 814 drp->r_attr.va_gid != GID_NOBODY) { 815 in_va->va_gid = drp->r_attr.va_gid; 816 setgid_flag = 1; 817 } 818 mutex_exit(&drp->r_statelock); 819 } 820 821 /* 822 * Normal/non-create compound: 823 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 824 * 825 * Open(create) compound no setgid: 826 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 827 * RESTOREFH + GETATTR 828 * 829 * Open(create) setgid: 830 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 831 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 832 * NVERIFY(grp) + SETATTR 833 */ 834 if (setgid_flag) { 835 numops = 10; 836 idx_open = 1; 837 idx_fattr = 3; 838 } else if (create_flag) { 839 numops = 7; 840 idx_open = 2; 841 idx_fattr = 4; 842 } else { 843 numops = 4; 844 idx_open = 1; 845 idx_fattr = 3; 846 } 847 848 args.array_len = numops; 849 argoplist_size = numops * sizeof (nfs_argop4); 850 argop = kmem_alloc(argoplist_size, KM_SLEEP); 851 852 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 853 "open %s open flag 0x%x cred %p", file_name, open_flag, 854 (void *)cr)); 855 856 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 857 if (create_flag) { 858 /* 859 * We are to create a file. Initialize the passed in vnode 860 * pointer. 861 */ 862 vpi = NULL; 863 } else { 864 /* 865 * Check to see if the client owns a read delegation and is 866 * trying to open for write. If so, then return the delegation 867 * to avoid the server doing a cb_recall and returning DELAY. 868 * NB - we don't use the statev4_lock here because we'd have 869 * to drop the lock anyway and the result would be stale. 870 */ 871 if ((open_flag & FWRITE) && 872 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 873 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 874 875 /* 876 * If the file has a delegation, then do an access check up 877 * front. This avoids having to an access check later after 878 * we've already done start_op, which could deadlock. 879 */ 880 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 881 if (open_flag & FREAD && 882 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0) 883 acc |= VREAD; 884 if (open_flag & FWRITE && 885 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0) 886 acc |= VWRITE; 887 } 888 } 889 890 drp = VTOR4(dvp); 891 892 recov_state.rs_flags = 0; 893 recov_state.rs_num_retry_despite_err = 0; 894 cred_otw = cr; 895 896 recov_retry: 897 fh_differs = 0; 898 nfs4_error_zinit(&e); 899 900 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 901 if (e.error) { 902 if (ncr != NULL) 903 crfree(ncr); 904 kmem_free(argop, argoplist_size); 905 return (e.error); 906 } 907 908 args.ctag = TAG_OPEN; 909 args.array_len = numops; 910 args.array = argop; 911 912 /* putfh directory fh */ 913 argop[0].argop = OP_CPUTFH; 914 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 915 916 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 917 argop[idx_open].argop = OP_COPEN; 918 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 919 open_args->claim = CLAIM_NULL; 920 921 /* name of file */ 922 open_args->open_claim4_u.cfile = file_name; 923 open_args->owner.owner_len = 0; 924 open_args->owner.owner_val = NULL; 925 926 if (create_flag) { 927 /* CREATE a file */ 928 open_args->opentype = OPEN4_CREATE; 929 open_args->mode = createmode; 930 if (createmode == EXCLUSIVE4) { 931 if (did_excl_setup == FALSE) { 932 verf.seconds = nfs_atoi(hw_serial); 933 if (verf.seconds != 0) 934 verf.nseconds = newnum(); 935 else { 936 timestruc_t now; 937 938 gethrestime(&now); 939 verf.seconds = now.tv_sec; 940 verf.nseconds = now.tv_nsec; 941 } 942 /* 943 * Since the server will use this value for the 944 * mtime, make sure that it can't overflow. Zero 945 * out the MSB. The actual value does not matter 946 * here, only its uniqeness. 947 */ 948 verf.seconds &= INT32_MAX; 949 did_excl_setup = TRUE; 950 } 951 952 /* Now copy over verifier to OPEN4args. */ 953 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 954 } else { 955 int v_error; 956 bitmap4 supp_attrs; 957 servinfo4_t *svp; 958 959 attr = &open_args->createhow4_u.createattrs; 960 961 svp = drp->r_server; 962 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 963 supp_attrs = svp->sv_supp_attrs; 964 nfs_rw_exit(&svp->sv_lock); 965 966 /* GUARDED4 or UNCHECKED4 */ 967 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 968 supp_attrs); 969 if (v_error) { 970 bzero(attr, sizeof (*attr)); 971 nfs4args_copen_free(open_args); 972 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 973 &recov_state, FALSE); 974 if (ncr != NULL) 975 crfree(ncr); 976 kmem_free(argop, argoplist_size); 977 return (v_error); 978 } 979 } 980 } else { 981 /* NO CREATE */ 982 open_args->opentype = OPEN4_NOCREATE; 983 } 984 985 if (recov_state.rs_sp != NULL) { 986 mutex_enter(&recov_state.rs_sp->s_lock); 987 open_args->owner.clientid = recov_state.rs_sp->clientid; 988 mutex_exit(&recov_state.rs_sp->s_lock); 989 } else { 990 /* XXX should we just fail here? */ 991 open_args->owner.clientid = 0; 992 } 993 994 /* 995 * This increments oop's ref count or creates a temporary 'just_created' 996 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 997 * completes. 998 */ 999 mutex_enter(&VTOMI4(dvp)->mi_lock); 1000 1001 /* See if a permanent or just created open owner exists */ 1002 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 1003 if (!oop) { 1004 /* 1005 * This open owner does not exist so create a temporary 1006 * just created one. 1007 */ 1008 oop = create_open_owner(cr, VTOMI4(dvp)); 1009 ASSERT(oop != NULL); 1010 } 1011 mutex_exit(&VTOMI4(dvp)->mi_lock); 1012 1013 /* this length never changes, do alloc before seqid sync */ 1014 open_args->owner.owner_len = sizeof (oop->oo_name); 1015 open_args->owner.owner_val = 1016 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1017 1018 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 1019 if (e.error == EAGAIN) { 1020 open_owner_rele(oop); 1021 nfs4args_copen_free(open_args); 1022 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1023 if (ncr != NULL) { 1024 crfree(ncr); 1025 ncr = NULL; 1026 } 1027 goto recov_retry; 1028 } 1029 1030 /* Check to see if we need to do the OTW call */ 1031 if (!create_flag) { 1032 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1033 file_just_been_created, &e.error, acc, &recov_state)) { 1034 1035 /* 1036 * The OTW open is not necessary. Either 1037 * the open can succeed without it (eg. 1038 * delegation, error == 0) or the open 1039 * must fail due to an access failure 1040 * (error != 0). In either case, tidy 1041 * up and return. 1042 */ 1043 1044 nfs4_end_open_seqid_sync(oop); 1045 open_owner_rele(oop); 1046 nfs4args_copen_free(open_args); 1047 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1048 if (ncr != NULL) 1049 crfree(ncr); 1050 kmem_free(argop, argoplist_size); 1051 return (e.error); 1052 } 1053 } 1054 1055 bcopy(&oop->oo_name, open_args->owner.owner_val, 1056 open_args->owner.owner_len); 1057 1058 seqid = nfs4_get_open_seqid(oop) + 1; 1059 open_args->seqid = seqid; 1060 open_args->share_access = 0; 1061 if (open_flag & FREAD) 1062 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1063 if (open_flag & FWRITE) 1064 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1065 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1066 1067 1068 1069 /* 1070 * getfh w/sanity check for idx_open/idx_fattr 1071 */ 1072 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1073 argop[idx_open + 1].argop = OP_GETFH; 1074 1075 /* getattr */ 1076 argop[idx_fattr].argop = OP_GETATTR; 1077 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1078 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1079 1080 if (setgid_flag) { 1081 vattr_t _v; 1082 servinfo4_t *svp; 1083 bitmap4 supp_attrs; 1084 1085 svp = drp->r_server; 1086 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1087 supp_attrs = svp->sv_supp_attrs; 1088 nfs_rw_exit(&svp->sv_lock); 1089 1090 /* 1091 * For setgid case, we need to: 1092 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1093 */ 1094 argop[4].argop = OP_SAVEFH; 1095 1096 argop[5].argop = OP_CPUTFH; 1097 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1098 1099 argop[6].argop = OP_GETATTR; 1100 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1101 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1102 1103 argop[7].argop = OP_RESTOREFH; 1104 1105 /* 1106 * nverify 1107 */ 1108 _v.va_mask = AT_GID; 1109 _v.va_gid = in_va->va_gid; 1110 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1111 supp_attrs))) { 1112 1113 /* 1114 * setattr 1115 * 1116 * We _know_ we're not messing with AT_SIZE or 1117 * AT_XTIME, so no need for stateid or flags. 1118 * Also we specify NULL rp since we're only 1119 * interested in setting owner_group attributes. 1120 */ 1121 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1122 supp_attrs, &e.error, 0); 1123 if (e.error) 1124 nfs4args_verify_free(&argop[8]); 1125 } 1126 1127 if (e.error) { 1128 /* 1129 * XXX - Revisit the last argument to nfs4_end_op() 1130 * once 5020486 is fixed. 1131 */ 1132 nfs4_end_open_seqid_sync(oop); 1133 open_owner_rele(oop); 1134 nfs4args_copen_free(open_args); 1135 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1136 if (ncr != NULL) 1137 crfree(ncr); 1138 kmem_free(argop, argoplist_size); 1139 return (e.error); 1140 } 1141 } else if (create_flag) { 1142 /* 1143 * For setgid case, we need to: 1144 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1145 */ 1146 argop[1].argop = OP_SAVEFH; 1147 1148 argop[5].argop = OP_RESTOREFH; 1149 1150 argop[6].argop = OP_GETATTR; 1151 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1152 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1153 } 1154 1155 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1156 "nfs4open_otw: %s call, nm %s, rp %s", 1157 needrecov ? "recov" : "first", file_name, 1158 rnode4info(VTOR4(dvp)))); 1159 1160 t = gethrtime(); 1161 1162 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1163 1164 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1165 nfs4_set_open_seqid(seqid, oop, args.ctag); 1166 1167 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1168 1169 if (e.error || needrecov) { 1170 bool_t abort = FALSE; 1171 1172 if (needrecov) { 1173 nfs4_bseqid_entry_t *bsep = NULL; 1174 1175 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1176 cred_otw, vpi, dvp, open_args); 1177 1178 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1179 bsep = nfs4_create_bseqid_entry(oop, NULL, 1180 vpi, 0, args.ctag, open_args->seqid); 1181 num_bseqid_retry--; 1182 } 1183 1184 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1185 NULL, lost_rqst.lr_op == OP_OPEN ? 1186 &lost_rqst : NULL, OP_OPEN, bsep); 1187 1188 if (bsep) 1189 kmem_free(bsep, sizeof (*bsep)); 1190 /* give up if we keep getting BAD_SEQID */ 1191 if (num_bseqid_retry == 0) 1192 abort = TRUE; 1193 if (abort == TRUE && e.error == 0) 1194 e.error = geterrno4(res.status); 1195 } 1196 nfs4_end_open_seqid_sync(oop); 1197 open_owner_rele(oop); 1198 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1199 nfs4args_copen_free(open_args); 1200 if (setgid_flag) { 1201 nfs4args_verify_free(&argop[8]); 1202 nfs4args_setattr_free(&argop[9]); 1203 } 1204 if (!e.error) 1205 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1206 if (ncr != NULL) { 1207 crfree(ncr); 1208 ncr = NULL; 1209 } 1210 if (!needrecov || abort == TRUE || e.error == EINTR || 1211 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1212 kmem_free(argop, argoplist_size); 1213 return (e.error); 1214 } 1215 goto recov_retry; 1216 } 1217 1218 /* 1219 * Will check and update lease after checking the rflag for 1220 * OPEN_CONFIRM in the successful OPEN call. 1221 */ 1222 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1223 1224 /* 1225 * XXX what if we're crossing mount points from server1:/drp 1226 * to server2:/drp/rp. 1227 */ 1228 1229 /* Signal our end of use of the open seqid */ 1230 nfs4_end_open_seqid_sync(oop); 1231 1232 /* 1233 * This will destroy the open owner if it was just created, 1234 * and no one else has put a reference on it. 1235 */ 1236 open_owner_rele(oop); 1237 if (create_flag && (createmode != EXCLUSIVE4) && 1238 res.status == NFS4ERR_BADOWNER) 1239 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1240 1241 e.error = geterrno4(res.status); 1242 nfs4args_copen_free(open_args); 1243 if (setgid_flag) { 1244 nfs4args_verify_free(&argop[8]); 1245 nfs4args_setattr_free(&argop[9]); 1246 } 1247 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1248 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1249 /* 1250 * If the reply is NFS4ERR_ACCESS, it may be because 1251 * we are root (no root net access). If the real uid 1252 * is not root, then retry with the real uid instead. 1253 */ 1254 if (ncr != NULL) { 1255 crfree(ncr); 1256 ncr = NULL; 1257 } 1258 if (res.status == NFS4ERR_ACCESS && 1259 (ncr = crnetadjust(cred_otw)) != NULL) { 1260 cred_otw = ncr; 1261 goto recov_retry; 1262 } 1263 kmem_free(argop, argoplist_size); 1264 return (e.error); 1265 } 1266 1267 resop = &res.array[idx_open]; /* open res */ 1268 op_res = &resop->nfs_resop4_u.opopen; 1269 1270 #ifdef DEBUG 1271 /* 1272 * verify attrset bitmap 1273 */ 1274 if (create_flag && 1275 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1276 /* make sure attrset returned is what we asked for */ 1277 /* XXX Ignore this 'error' for now */ 1278 if (attr->attrmask != op_res->attrset) 1279 /* EMPTY */; 1280 } 1281 #endif 1282 1283 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1284 mutex_enter(&VTOMI4(dvp)->mi_lock); 1285 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1286 mutex_exit(&VTOMI4(dvp)->mi_lock); 1287 } 1288 1289 resop = &res.array[idx_open + 1]; /* getfh res */ 1290 gf_res = &resop->nfs_resop4_u.opgetfh; 1291 1292 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1293 1294 /* 1295 * The open stateid has been updated on the server but not 1296 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1297 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1298 * WRITE call. That, however, will use the old stateid, so go ahead 1299 * and upate the open stateid now, before any call to makenfs4node. 1300 */ 1301 if (vpi) { 1302 nfs4_open_stream_t *tmp_osp; 1303 rnode4_t *tmp_rp = VTOR4(vpi); 1304 1305 tmp_osp = find_open_stream(oop, tmp_rp); 1306 if (tmp_osp) { 1307 tmp_osp->open_stateid = op_res->stateid; 1308 mutex_exit(&tmp_osp->os_sync_lock); 1309 open_stream_rele(tmp_osp, tmp_rp); 1310 } 1311 1312 /* 1313 * We must determine if the file handle given by the otw open 1314 * is the same as the file handle which was passed in with 1315 * *vpp. This case can be reached if the file we are trying 1316 * to open has been removed and another file has been created 1317 * having the same file name. The passed in vnode is released 1318 * later. 1319 */ 1320 orig_sfh = VTOR4(vpi)->r_fh; 1321 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1322 } 1323 1324 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1325 1326 if (create_flag || fh_differs) { 1327 int rnode_err = 0; 1328 1329 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1330 dvp, fn_get(VTOSV(dvp)->sv_name, file_name)); 1331 1332 if (e.error) 1333 PURGE_ATTRCACHE4(vp); 1334 /* 1335 * For the newly created vp case, make sure the rnode 1336 * isn't bad before using it. 1337 */ 1338 mutex_enter(&(VTOR4(vp))->r_statelock); 1339 if (VTOR4(vp)->r_flags & R4RECOVERR) 1340 rnode_err = EIO; 1341 mutex_exit(&(VTOR4(vp))->r_statelock); 1342 1343 if (rnode_err) { 1344 nfs4_end_open_seqid_sync(oop); 1345 nfs4args_copen_free(open_args); 1346 if (setgid_flag) { 1347 nfs4args_verify_free(&argop[8]); 1348 nfs4args_setattr_free(&argop[9]); 1349 } 1350 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1351 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1352 needrecov); 1353 open_owner_rele(oop); 1354 VN_RELE(vp); 1355 if (ncr != NULL) 1356 crfree(ncr); 1357 sfh4_rele(&otw_sfh); 1358 kmem_free(argop, argoplist_size); 1359 return (EIO); 1360 } 1361 } else { 1362 vp = vpi; 1363 } 1364 sfh4_rele(&otw_sfh); 1365 1366 /* 1367 * It seems odd to get a full set of attrs and then not update 1368 * the object's attrcache in the non-create case. Create case uses 1369 * the attrs since makenfs4node checks to see if the attrs need to 1370 * be updated (and then updates them). The non-create case should 1371 * update attrs also. 1372 */ 1373 if (! create_flag && ! fh_differs && !e.error) { 1374 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1375 } 1376 1377 nfs4_error_zinit(&e); 1378 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1379 /* This does not do recovery for vp explicitly. */ 1380 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1381 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1382 1383 if (e.error || e.stat) { 1384 nfs4_end_open_seqid_sync(oop); 1385 nfs4args_copen_free(open_args); 1386 if (setgid_flag) { 1387 nfs4args_verify_free(&argop[8]); 1388 nfs4args_setattr_free(&argop[9]); 1389 } 1390 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1391 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1392 needrecov); 1393 open_owner_rele(oop); 1394 if (create_flag || fh_differs) { 1395 /* rele the makenfs4node */ 1396 VN_RELE(vp); 1397 } 1398 if (ncr != NULL) { 1399 crfree(ncr); 1400 ncr = NULL; 1401 } 1402 if (retry_open == TRUE) { 1403 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1404 "nfs4open_otw: retry the open since OPEN " 1405 "CONFIRM failed with error %d stat %d", 1406 e.error, e.stat)); 1407 if (create_flag && createmode == GUARDED4) { 1408 NFS4_DEBUG(nfs4_client_recov_debug, 1409 (CE_NOTE, "nfs4open_otw: switch " 1410 "createmode from GUARDED4 to " 1411 "UNCHECKED4")); 1412 createmode = UNCHECKED4; 1413 } 1414 goto recov_retry; 1415 } 1416 if (!e.error) { 1417 if (create_flag && (createmode != EXCLUSIVE4) && 1418 e.stat == NFS4ERR_BADOWNER) 1419 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1420 1421 e.error = geterrno4(e.stat); 1422 } 1423 kmem_free(argop, argoplist_size); 1424 return (e.error); 1425 } 1426 } 1427 1428 rp = VTOR4(vp); 1429 1430 mutex_enter(&rp->r_statev4_lock); 1431 if (create_flag) 1432 rp->created_v4 = 1; 1433 mutex_exit(&rp->r_statev4_lock); 1434 1435 mutex_enter(&oop->oo_lock); 1436 /* Doesn't matter if 'oo_just_created' already was set as this */ 1437 oop->oo_just_created = NFS4_PERM_CREATED; 1438 if (oop->oo_cred_otw) 1439 crfree(oop->oo_cred_otw); 1440 oop->oo_cred_otw = cred_otw; 1441 crhold(oop->oo_cred_otw); 1442 mutex_exit(&oop->oo_lock); 1443 1444 /* returns with 'os_sync_lock' held */ 1445 osp = find_or_create_open_stream(oop, rp, &created_osp); 1446 if (!osp) { 1447 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1448 "nfs4open_otw: failed to create an open stream")); 1449 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1450 "signal our end of use of the open seqid")); 1451 1452 nfs4_end_open_seqid_sync(oop); 1453 open_owner_rele(oop); 1454 nfs4args_copen_free(open_args); 1455 if (setgid_flag) { 1456 nfs4args_verify_free(&argop[8]); 1457 nfs4args_setattr_free(&argop[9]); 1458 } 1459 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1460 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1461 if (create_flag || fh_differs) 1462 VN_RELE(vp); 1463 if (ncr != NULL) 1464 crfree(ncr); 1465 1466 kmem_free(argop, argoplist_size); 1467 return (EINVAL); 1468 1469 } 1470 1471 osp->open_stateid = op_res->stateid; 1472 1473 if (open_flag & FREAD) 1474 osp->os_share_acc_read++; 1475 if (open_flag & FWRITE) 1476 osp->os_share_acc_write++; 1477 osp->os_share_deny_none++; 1478 1479 /* 1480 * Need to reset this bitfield for the possible case where we were 1481 * going to OTW CLOSE the file, got a non-recoverable error, and before 1482 * we could retry the CLOSE, OPENed the file again. 1483 */ 1484 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1485 osp->os_final_close = 0; 1486 osp->os_force_close = 0; 1487 #ifdef DEBUG 1488 if (osp->os_failed_reopen) 1489 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1490 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1491 (void *)osp, (void *)cr, rnode4info(rp))); 1492 #endif 1493 osp->os_failed_reopen = 0; 1494 1495 mutex_exit(&osp->os_sync_lock); 1496 1497 nfs4_end_open_seqid_sync(oop); 1498 1499 if (created_osp && recov_state.rs_sp != NULL) { 1500 mutex_enter(&recov_state.rs_sp->s_lock); 1501 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1502 mutex_exit(&recov_state.rs_sp->s_lock); 1503 } 1504 1505 /* get rid of our reference to find oop */ 1506 open_owner_rele(oop); 1507 1508 open_stream_rele(osp, rp); 1509 1510 /* accept delegation, if any */ 1511 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1512 1513 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1514 1515 if (createmode == EXCLUSIVE4 && 1516 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1517 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1518 " EXCLUSIVE4: sending a SETATTR")); 1519 /* 1520 * If doing an exclusive create, then generate 1521 * a SETATTR to set the initial attributes. 1522 * Try to set the mtime and the atime to the 1523 * server's current time. It is somewhat 1524 * expected that these fields will be used to 1525 * store the exclusive create cookie. If not, 1526 * server implementors will need to know that 1527 * a SETATTR will follow an exclusive create 1528 * and the cookie should be destroyed if 1529 * appropriate. 1530 * 1531 * The AT_GID and AT_SIZE bits are turned off 1532 * so that the SETATTR request will not attempt 1533 * to process these. The gid will be set 1534 * separately if appropriate. The size is turned 1535 * off because it is assumed that a new file will 1536 * be created empty and if the file wasn't empty, 1537 * then the exclusive create will have failed 1538 * because the file must have existed already. 1539 * Therefore, no truncate operation is needed. 1540 */ 1541 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1542 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1543 1544 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1545 if (e.error) { 1546 /* 1547 * Couldn't correct the attributes of 1548 * the newly created file and the 1549 * attributes are wrong. Remove the 1550 * file and return an error to the 1551 * application. 1552 */ 1553 /* XXX will this take care of client state ? */ 1554 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1555 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1556 " remove file", e.error)); 1557 VN_RELE(vp); 1558 (void) nfs4_remove(dvp, file_name, cr, NULL, 0); 1559 /* 1560 * Since we've reled the vnode and removed 1561 * the file we now need to return the error. 1562 * At this point we don't want to update the 1563 * dircaches, call nfs4_waitfor_purge_complete 1564 * or set vpp to vp so we need to skip these 1565 * as well. 1566 */ 1567 goto skip_update_dircaches; 1568 } 1569 } 1570 1571 /* 1572 * If we created or found the correct vnode, due to create_flag or 1573 * fh_differs being set, then update directory cache attribute, readdir 1574 * and dnlc caches. 1575 */ 1576 if (create_flag || fh_differs) { 1577 dirattr_info_t dinfo, *dinfop; 1578 1579 /* 1580 * Make sure getattr succeeded before using results. 1581 * note: op 7 is getattr(dir) for both flavors of 1582 * open(create). 1583 */ 1584 if (create_flag && res.status == NFS4_OK) { 1585 dinfo.di_time_call = t; 1586 dinfo.di_cred = cr; 1587 dinfo.di_garp = 1588 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1589 dinfop = &dinfo; 1590 } else { 1591 dinfop = NULL; 1592 } 1593 1594 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1595 dinfop); 1596 } 1597 1598 /* 1599 * If the page cache for this file was flushed from actions 1600 * above, it was done asynchronously and if that is true, 1601 * there is a need to wait here for it to complete. This must 1602 * be done outside of start_fop/end_fop. 1603 */ 1604 (void) nfs4_waitfor_purge_complete(vp); 1605 1606 /* 1607 * It is implicit that we are in the open case (create_flag == 0) since 1608 * fh_differs can only be set to a non-zero value in the open case. 1609 */ 1610 if (fh_differs != 0 && vpi != NULL) 1611 VN_RELE(vpi); 1612 1613 /* 1614 * Be sure to set *vpp to the correct value before returning. 1615 */ 1616 *vpp = vp; 1617 1618 skip_update_dircaches: 1619 1620 nfs4args_copen_free(open_args); 1621 if (setgid_flag) { 1622 nfs4args_verify_free(&argop[8]); 1623 nfs4args_setattr_free(&argop[9]); 1624 } 1625 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1626 1627 if (ncr) 1628 crfree(ncr); 1629 kmem_free(argop, argoplist_size); 1630 return (e.error); 1631 } 1632 1633 /* 1634 * Reopen an open instance. cf. nfs4open_otw(). 1635 * 1636 * Errors are returned by the nfs4_error_t parameter. 1637 * - ep->error contains an errno value or zero. 1638 * - if it is zero, ep->stat is set to an NFS status code, if any. 1639 * If the file could not be reopened, but the caller should continue, the 1640 * file is marked dead and no error values are returned. If the caller 1641 * should stop recovering open files and start over, either the ep->error 1642 * value or ep->stat will indicate an error (either something that requires 1643 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1644 * filehandles) may be handled silently by this routine. 1645 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1646 * will be started, so the caller should not do it. 1647 * 1648 * Gotos: 1649 * - kill_file : reopen failed in such a fashion to constitute marking the 1650 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1651 * is for cases where recovery is not possible. 1652 * - failed_reopen : same as above, except that the file has already been 1653 * marked dead, so no need to do it again. 1654 * - bailout : reopen failed but we are able to recover and retry the reopen - 1655 * either within this function immediately or via the calling function. 1656 */ 1657 1658 void 1659 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1660 open_claim_type4 claim, bool_t frc_use_claim_previous, 1661 bool_t is_recov) 1662 { 1663 COMPOUND4args_clnt args; 1664 COMPOUND4res_clnt res; 1665 nfs_argop4 argop[4]; 1666 nfs_resop4 *resop; 1667 OPEN4res *op_res = NULL; 1668 OPEN4cargs *open_args; 1669 GETFH4res *gf_res; 1670 rnode4_t *rp = VTOR4(vp); 1671 int doqueue = 1; 1672 cred_t *cr = NULL, *cred_otw = NULL; 1673 nfs4_open_owner_t *oop = NULL; 1674 seqid4 seqid; 1675 nfs4_ga_res_t *garp; 1676 char fn[MAXNAMELEN]; 1677 nfs4_recov_state_t recov = {NULL, 0}; 1678 nfs4_lost_rqst_t lost_rqst; 1679 mntinfo4_t *mi = VTOMI4(vp); 1680 bool_t abort; 1681 char *failed_msg = ""; 1682 int fh_different; 1683 hrtime_t t; 1684 nfs4_bseqid_entry_t *bsep = NULL; 1685 1686 ASSERT(nfs4_consistent_type(vp)); 1687 ASSERT(nfs_zone() == mi->mi_zone); 1688 1689 nfs4_error_zinit(ep); 1690 1691 /* this is the cred used to find the open owner */ 1692 cr = state_to_cred(osp); 1693 if (cr == NULL) { 1694 failed_msg = "Couldn't reopen: no cred"; 1695 goto kill_file; 1696 } 1697 /* use this cred for OTW operations */ 1698 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1699 1700 top: 1701 nfs4_error_zinit(ep); 1702 1703 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1704 /* File system has been unmounted, quit */ 1705 ep->error = EIO; 1706 failed_msg = "Couldn't reopen: file system has been unmounted"; 1707 goto kill_file; 1708 } 1709 1710 oop = osp->os_open_owner; 1711 1712 ASSERT(oop != NULL); 1713 if (oop == NULL) { /* be defensive in non-DEBUG */ 1714 failed_msg = "can't reopen: no open owner"; 1715 goto kill_file; 1716 } 1717 open_owner_hold(oop); 1718 1719 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1720 if (ep->error) { 1721 open_owner_rele(oop); 1722 oop = NULL; 1723 goto bailout; 1724 } 1725 1726 /* 1727 * If the rnode has a delegation and the delegation has been 1728 * recovered and the server didn't request a recall and the caller 1729 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1730 * recovery) and the rnode hasn't been marked dead, then install 1731 * the delegation stateid in the open stream. Otherwise, proceed 1732 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1733 */ 1734 mutex_enter(&rp->r_statev4_lock); 1735 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1736 !rp->r_deleg_return_pending && 1737 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1738 !rp->r_deleg_needs_recall && 1739 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1740 !(rp->r_flags & R4RECOVERR)) { 1741 mutex_enter(&osp->os_sync_lock); 1742 osp->os_delegation = 1; 1743 osp->open_stateid = rp->r_deleg_stateid; 1744 mutex_exit(&osp->os_sync_lock); 1745 mutex_exit(&rp->r_statev4_lock); 1746 goto bailout; 1747 } 1748 mutex_exit(&rp->r_statev4_lock); 1749 1750 /* 1751 * If the file failed recovery, just quit. This failure need not 1752 * affect other reopens, so don't return an error. 1753 */ 1754 mutex_enter(&rp->r_statelock); 1755 if (rp->r_flags & R4RECOVERR) { 1756 mutex_exit(&rp->r_statelock); 1757 ep->error = 0; 1758 goto failed_reopen; 1759 } 1760 mutex_exit(&rp->r_statelock); 1761 1762 /* 1763 * argop is empty here 1764 * 1765 * PUTFH, OPEN, GETATTR 1766 */ 1767 args.ctag = TAG_REOPEN; 1768 args.array_len = 4; 1769 args.array = argop; 1770 1771 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1772 "nfs4_reopen: file is type %d, id %s", 1773 vp->v_type, rnode4info(VTOR4(vp)))); 1774 1775 argop[0].argop = OP_CPUTFH; 1776 1777 if (claim != CLAIM_PREVIOUS) { 1778 /* 1779 * if this is a file mount then 1780 * use the mntinfo parentfh 1781 */ 1782 argop[0].nfs_argop4_u.opcputfh.sfh = 1783 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1784 VTOSV(vp)->sv_dfh; 1785 } else { 1786 /* putfh fh to reopen */ 1787 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1788 } 1789 1790 argop[1].argop = OP_COPEN; 1791 open_args = &argop[1].nfs_argop4_u.opcopen; 1792 open_args->claim = claim; 1793 1794 if (claim == CLAIM_NULL) { 1795 1796 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1797 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1798 "failed for vp 0x%p for CLAIM_NULL with %m", 1799 (void *)vp); 1800 failed_msg = "Couldn't reopen: vtoname failed for " 1801 "CLAIM_NULL"; 1802 /* nothing allocated yet */ 1803 goto kill_file; 1804 } 1805 1806 open_args->open_claim4_u.cfile = fn; 1807 } else if (claim == CLAIM_PREVIOUS) { 1808 1809 /* 1810 * We have two cases to deal with here: 1811 * 1) We're being called to reopen files in order to satisfy 1812 * a lock operation request which requires us to explicitly 1813 * reopen files which were opened under a delegation. If 1814 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1815 * that case, frc_use_claim_previous is TRUE and we must 1816 * use the rnode's current delegation type (r_deleg_type). 1817 * 2) We're reopening files during some form of recovery. 1818 * In this case, frc_use_claim_previous is FALSE and we 1819 * use the delegation type appropriate for recovery 1820 * (r_deleg_needs_recovery). 1821 */ 1822 mutex_enter(&rp->r_statev4_lock); 1823 open_args->open_claim4_u.delegate_type = 1824 frc_use_claim_previous ? 1825 rp->r_deleg_type : 1826 rp->r_deleg_needs_recovery; 1827 mutex_exit(&rp->r_statev4_lock); 1828 1829 } else if (claim == CLAIM_DELEGATE_CUR) { 1830 1831 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1832 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1833 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1834 "with %m", (void *)vp); 1835 failed_msg = "Couldn't reopen: vtoname failed for " 1836 "CLAIM_DELEGATE_CUR"; 1837 /* nothing allocated yet */ 1838 goto kill_file; 1839 } 1840 1841 mutex_enter(&rp->r_statev4_lock); 1842 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1843 rp->r_deleg_stateid; 1844 mutex_exit(&rp->r_statev4_lock); 1845 1846 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1847 } 1848 open_args->opentype = OPEN4_NOCREATE; 1849 open_args->owner.clientid = mi2clientid(mi); 1850 open_args->owner.owner_len = sizeof (oop->oo_name); 1851 open_args->owner.owner_val = 1852 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1853 bcopy(&oop->oo_name, open_args->owner.owner_val, 1854 open_args->owner.owner_len); 1855 open_args->share_access = 0; 1856 open_args->share_deny = 0; 1857 1858 mutex_enter(&osp->os_sync_lock); 1859 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1860 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1861 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1862 (void *)osp, (void *)rp, osp->os_share_acc_read, 1863 osp->os_share_acc_write, osp->os_open_ref_count, 1864 osp->os_mmap_read, osp->os_mmap_write, claim)); 1865 1866 if (osp->os_share_acc_read || osp->os_mmap_read) 1867 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1868 if (osp->os_share_acc_write || osp->os_mmap_write) 1869 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1870 if (osp->os_share_deny_read) 1871 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1872 if (osp->os_share_deny_write) 1873 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1874 mutex_exit(&osp->os_sync_lock); 1875 1876 seqid = nfs4_get_open_seqid(oop) + 1; 1877 open_args->seqid = seqid; 1878 1879 /* Construct the getfh part of the compound */ 1880 argop[2].argop = OP_GETFH; 1881 1882 /* Construct the getattr part of the compound */ 1883 argop[3].argop = OP_GETATTR; 1884 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1885 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1886 1887 t = gethrtime(); 1888 1889 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1890 1891 if (ep->error) { 1892 if (!is_recov && !frc_use_claim_previous && 1893 (ep->error == EINTR || ep->error == ETIMEDOUT || 1894 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1895 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1896 cred_otw, vp, NULL, open_args); 1897 abort = nfs4_start_recovery(ep, 1898 VTOMI4(vp), vp, NULL, NULL, 1899 lost_rqst.lr_op == OP_OPEN ? 1900 &lost_rqst : NULL, OP_OPEN, NULL); 1901 nfs4args_copen_free(open_args); 1902 goto bailout; 1903 } 1904 1905 nfs4args_copen_free(open_args); 1906 1907 if (ep->error == EACCES && cred_otw != cr) { 1908 crfree(cred_otw); 1909 cred_otw = cr; 1910 crhold(cred_otw); 1911 nfs4_end_open_seqid_sync(oop); 1912 open_owner_rele(oop); 1913 oop = NULL; 1914 goto top; 1915 } 1916 if (ep->error == ETIMEDOUT) 1917 goto bailout; 1918 failed_msg = "Couldn't reopen: rpc error"; 1919 goto kill_file; 1920 } 1921 1922 if (nfs4_need_to_bump_seqid(&res)) 1923 nfs4_set_open_seqid(seqid, oop, args.ctag); 1924 1925 switch (res.status) { 1926 case NFS4_OK: 1927 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1928 mutex_enter(&rp->r_statelock); 1929 rp->r_delay_interval = 0; 1930 mutex_exit(&rp->r_statelock); 1931 } 1932 break; 1933 case NFS4ERR_BAD_SEQID: 1934 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1935 args.ctag, open_args->seqid); 1936 1937 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1938 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1939 NULL, OP_OPEN, bsep); 1940 1941 nfs4args_copen_free(open_args); 1942 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1943 nfs4_end_open_seqid_sync(oop); 1944 open_owner_rele(oop); 1945 oop = NULL; 1946 kmem_free(bsep, sizeof (*bsep)); 1947 1948 goto kill_file; 1949 case NFS4ERR_NO_GRACE: 1950 nfs4args_copen_free(open_args); 1951 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1952 nfs4_end_open_seqid_sync(oop); 1953 open_owner_rele(oop); 1954 oop = NULL; 1955 if (claim == CLAIM_PREVIOUS) { 1956 /* 1957 * Retry as a plain open. We don't need to worry about 1958 * checking the changeinfo: it is acceptable for a 1959 * client to re-open a file and continue processing 1960 * (in the absence of locks). 1961 */ 1962 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1963 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1964 "will retry as CLAIM_NULL")); 1965 claim = CLAIM_NULL; 1966 nfs4_mi_kstat_inc_no_grace(mi); 1967 goto top; 1968 } 1969 failed_msg = 1970 "Couldn't reopen: tried reclaim outside grace period. "; 1971 goto kill_file; 1972 case NFS4ERR_GRACE: 1973 nfs4_set_grace_wait(mi); 1974 nfs4args_copen_free(open_args); 1975 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1976 nfs4_end_open_seqid_sync(oop); 1977 open_owner_rele(oop); 1978 oop = NULL; 1979 ep->error = nfs4_wait_for_grace(mi, &recov); 1980 if (ep->error != 0) 1981 goto bailout; 1982 goto top; 1983 case NFS4ERR_DELAY: 1984 nfs4_set_delay_wait(vp); 1985 nfs4args_copen_free(open_args); 1986 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1987 nfs4_end_open_seqid_sync(oop); 1988 open_owner_rele(oop); 1989 oop = NULL; 1990 ep->error = nfs4_wait_for_delay(vp, &recov); 1991 nfs4_mi_kstat_inc_delay(mi); 1992 if (ep->error != 0) 1993 goto bailout; 1994 goto top; 1995 case NFS4ERR_FHEXPIRED: 1996 /* recover filehandle and retry */ 1997 abort = nfs4_start_recovery(ep, 1998 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL); 1999 nfs4args_copen_free(open_args); 2000 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2001 nfs4_end_open_seqid_sync(oop); 2002 open_owner_rele(oop); 2003 oop = NULL; 2004 if (abort == FALSE) 2005 goto top; 2006 failed_msg = "Couldn't reopen: recovery aborted"; 2007 goto kill_file; 2008 case NFS4ERR_RESOURCE: 2009 case NFS4ERR_STALE_CLIENTID: 2010 case NFS4ERR_WRONGSEC: 2011 case NFS4ERR_EXPIRED: 2012 /* 2013 * Do not mark the file dead and let the calling 2014 * function initiate recovery. 2015 */ 2016 nfs4args_copen_free(open_args); 2017 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2018 nfs4_end_open_seqid_sync(oop); 2019 open_owner_rele(oop); 2020 oop = NULL; 2021 goto bailout; 2022 case NFS4ERR_ACCESS: 2023 if (cred_otw != cr) { 2024 crfree(cred_otw); 2025 cred_otw = cr; 2026 crhold(cred_otw); 2027 nfs4args_copen_free(open_args); 2028 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2029 nfs4_end_open_seqid_sync(oop); 2030 open_owner_rele(oop); 2031 oop = NULL; 2032 goto top; 2033 } 2034 /* fall through */ 2035 default: 2036 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2037 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2038 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2039 rnode4info(VTOR4(vp)))); 2040 failed_msg = "Couldn't reopen: NFSv4 error"; 2041 nfs4args_copen_free(open_args); 2042 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2043 goto kill_file; 2044 } 2045 2046 resop = &res.array[1]; /* open res */ 2047 op_res = &resop->nfs_resop4_u.opopen; 2048 2049 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2050 2051 /* 2052 * Check if the path we reopened really is the same 2053 * file. We could end up in a situation where the file 2054 * was removed and a new file created with the same name. 2055 */ 2056 resop = &res.array[2]; 2057 gf_res = &resop->nfs_resop4_u.opgetfh; 2058 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2059 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2060 if (fh_different) { 2061 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2062 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2063 /* Oops, we don't have the same file */ 2064 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2065 failed_msg = "Couldn't reopen: Persistent " 2066 "file handle changed"; 2067 else 2068 failed_msg = "Couldn't reopen: Volatile " 2069 "(no expire on open) file handle changed"; 2070 2071 nfs4args_copen_free(open_args); 2072 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2073 nfs_rw_exit(&mi->mi_fh_lock); 2074 goto kill_file; 2075 2076 } else { 2077 /* 2078 * We have volatile file handles that don't compare. 2079 * If the fids are the same then we assume that the 2080 * file handle expired but the rnode still refers to 2081 * the same file object. 2082 * 2083 * First check that we have fids or not. 2084 * If we don't we have a dumb server so we will 2085 * just assume every thing is ok for now. 2086 */ 2087 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2088 rp->r_attr.va_mask & AT_NODEID && 2089 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2090 /* 2091 * We have fids, but they don't 2092 * compare. So kill the file. 2093 */ 2094 failed_msg = 2095 "Couldn't reopen: file handle changed" 2096 " due to mismatched fids"; 2097 nfs4args_copen_free(open_args); 2098 (void) xdr_free(xdr_COMPOUND4res_clnt, 2099 (caddr_t)&res); 2100 nfs_rw_exit(&mi->mi_fh_lock); 2101 goto kill_file; 2102 } else { 2103 /* 2104 * We have volatile file handles that refers 2105 * to the same file (at least they have the 2106 * same fid) or we don't have fids so we 2107 * can't tell. :(. We'll be a kind and accepting 2108 * client so we'll update the rnode's file 2109 * handle with the otw handle. 2110 * 2111 * We need to drop mi->mi_fh_lock since 2112 * sh4_update acquires it. Since there is 2113 * only one recovery thread there is no 2114 * race. 2115 */ 2116 nfs_rw_exit(&mi->mi_fh_lock); 2117 sfh4_update(rp->r_fh, &gf_res->object); 2118 } 2119 } 2120 } else { 2121 nfs_rw_exit(&mi->mi_fh_lock); 2122 } 2123 2124 ASSERT(nfs4_consistent_type(vp)); 2125 2126 /* 2127 * If the server wanted an OPEN_CONFIRM but that fails, just start 2128 * over. Presumably if there is a persistent error it will show up 2129 * when we resend the OPEN. 2130 */ 2131 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2132 bool_t retry_open = FALSE; 2133 2134 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2135 cred_otw, is_recov, &retry_open, 2136 oop, FALSE, ep, NULL); 2137 if (ep->error || ep->stat) { 2138 nfs4args_copen_free(open_args); 2139 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2140 nfs4_end_open_seqid_sync(oop); 2141 open_owner_rele(oop); 2142 oop = NULL; 2143 goto top; 2144 } 2145 } 2146 2147 mutex_enter(&osp->os_sync_lock); 2148 osp->open_stateid = op_res->stateid; 2149 osp->os_delegation = 0; 2150 /* 2151 * Need to reset this bitfield for the possible case where we were 2152 * going to OTW CLOSE the file, got a non-recoverable error, and before 2153 * we could retry the CLOSE, OPENed the file again. 2154 */ 2155 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2156 osp->os_final_close = 0; 2157 osp->os_force_close = 0; 2158 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2159 osp->os_dc_openacc = open_args->share_access; 2160 mutex_exit(&osp->os_sync_lock); 2161 2162 nfs4_end_open_seqid_sync(oop); 2163 2164 /* accept delegation, if any */ 2165 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2166 2167 nfs4args_copen_free(open_args); 2168 2169 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2170 2171 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2172 2173 ASSERT(nfs4_consistent_type(vp)); 2174 2175 open_owner_rele(oop); 2176 crfree(cr); 2177 crfree(cred_otw); 2178 return; 2179 2180 kill_file: 2181 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2182 failed_reopen: 2183 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2184 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2185 (void *)osp, (void *)cr, rnode4info(rp))); 2186 mutex_enter(&osp->os_sync_lock); 2187 osp->os_failed_reopen = 1; 2188 mutex_exit(&osp->os_sync_lock); 2189 bailout: 2190 if (oop != NULL) { 2191 nfs4_end_open_seqid_sync(oop); 2192 open_owner_rele(oop); 2193 } 2194 if (cr != NULL) 2195 crfree(cr); 2196 if (cred_otw != NULL) 2197 crfree(cred_otw); 2198 } 2199 2200 /* for . and .. OPENs */ 2201 /* ARGSUSED */ 2202 static int 2203 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2204 { 2205 rnode4_t *rp; 2206 nfs4_ga_res_t gar; 2207 2208 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2209 2210 /* 2211 * If close-to-open consistency checking is turned off or 2212 * if there is no cached data, we can avoid 2213 * the over the wire getattr. Otherwise, force a 2214 * call to the server to get fresh attributes and to 2215 * check caches. This is required for close-to-open 2216 * consistency. 2217 */ 2218 rp = VTOR4(*vpp); 2219 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2220 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2221 return (0); 2222 2223 gar.n4g_va.va_mask = AT_ALL; 2224 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2225 } 2226 2227 /* 2228 * CLOSE a file 2229 */ 2230 /* ARGSUSED */ 2231 static int 2232 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2233 caller_context_t *ct) 2234 { 2235 rnode4_t *rp; 2236 int error = 0; 2237 int r_error = 0; 2238 int n4error = 0; 2239 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2240 2241 /* 2242 * Remove client state for this (lockowner, file) pair. 2243 * Issue otw v4 call to have the server do the same. 2244 */ 2245 2246 rp = VTOR4(vp); 2247 2248 /* 2249 * zone_enter(2) prevents processes from changing zones with NFS files 2250 * open; if we happen to get here from the wrong zone we can't do 2251 * anything over the wire. 2252 */ 2253 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2254 /* 2255 * We could attempt to clean up locks, except we're sure 2256 * that the current process didn't acquire any locks on 2257 * the file: any attempt to lock a file belong to another zone 2258 * will fail, and one can't lock an NFS file and then change 2259 * zones, as that fails too. 2260 * 2261 * Returning an error here is the sane thing to do. A 2262 * subsequent call to VN_RELE() which translates to a 2263 * nfs4_inactive() will clean up state: if the zone of the 2264 * vnode's origin is still alive and kicking, the inactive 2265 * thread will handle the request (from the correct zone), and 2266 * everything (minus the OTW close call) should be OK. If the 2267 * zone is going away nfs4_async_inactive() will throw away 2268 * delegations, open streams and cached pages inline. 2269 */ 2270 return (EIO); 2271 } 2272 2273 /* 2274 * If we are using local locking for this filesystem, then 2275 * release all of the SYSV style record locks. Otherwise, 2276 * we are doing network locking and we need to release all 2277 * of the network locks. All of the locks held by this 2278 * process on this file are released no matter what the 2279 * incoming reference count is. 2280 */ 2281 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2282 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2283 cleanshares(vp, ttoproc(curthread)->p_pid); 2284 } else 2285 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2286 2287 if (e.error) 2288 return (e.error); 2289 2290 if (count > 1) 2291 return (0); 2292 2293 /* 2294 * If the file has been `unlinked', then purge the 2295 * DNLC so that this vnode will get reycled quicker 2296 * and the .nfs* file on the server will get removed. 2297 */ 2298 if (rp->r_unldvp != NULL) 2299 dnlc_purge_vp(vp); 2300 2301 /* 2302 * If the file was open for write and there are pages, 2303 * do a synchronous flush and commit of all of the 2304 * dirty and uncommitted pages. 2305 */ 2306 ASSERT(!e.error); 2307 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2308 error = nfs4_putpage_commit(vp, 0, 0, cr); 2309 2310 mutex_enter(&rp->r_statelock); 2311 r_error = rp->r_error; 2312 rp->r_error = 0; 2313 mutex_exit(&rp->r_statelock); 2314 2315 /* 2316 * If this file type is one for which no explicit 'open' was 2317 * done, then bail now (ie. no need for protocol 'close'). If 2318 * there was an error w/the vm subsystem, return _that_ error, 2319 * otherwise, return any errors that may've been reported via 2320 * the rnode. 2321 */ 2322 if (vp->v_type != VREG) 2323 return (error ? error : r_error); 2324 2325 /* 2326 * The sync putpage commit may have failed above, but since 2327 * we're working w/a regular file, we need to do the protocol 2328 * 'close' (nfs4close_one will figure out if an otw close is 2329 * needed or not). Report any errors _after_ doing the protocol 2330 * 'close'. 2331 */ 2332 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2333 n4error = e.error ? e.error : geterrno4(e.stat); 2334 2335 /* 2336 * Error reporting prio (Hi -> Lo) 2337 * 2338 * i) nfs4_putpage_commit (error) 2339 * ii) rnode's (r_error) 2340 * iii) nfs4close_one (n4error) 2341 */ 2342 return (error ? error : (r_error ? r_error : n4error)); 2343 } 2344 2345 /* 2346 * Initialize *lost_rqstp. 2347 */ 2348 2349 static void 2350 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2351 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2352 vnode_t *vp) 2353 { 2354 if (error != ETIMEDOUT && error != EINTR && 2355 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2356 lost_rqstp->lr_op = 0; 2357 return; 2358 } 2359 2360 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2361 "nfs4close_save_lost_rqst: error %d", error)); 2362 2363 lost_rqstp->lr_op = OP_CLOSE; 2364 /* 2365 * The vp is held and rele'd via the recovery code. 2366 * See nfs4_save_lost_rqst. 2367 */ 2368 lost_rqstp->lr_vp = vp; 2369 lost_rqstp->lr_dvp = NULL; 2370 lost_rqstp->lr_oop = oop; 2371 lost_rqstp->lr_osp = osp; 2372 ASSERT(osp != NULL); 2373 ASSERT(mutex_owned(&osp->os_sync_lock)); 2374 osp->os_pending_close = 1; 2375 lost_rqstp->lr_lop = NULL; 2376 lost_rqstp->lr_cr = cr; 2377 lost_rqstp->lr_flk = NULL; 2378 lost_rqstp->lr_putfirst = FALSE; 2379 } 2380 2381 /* 2382 * Assumes you already have the open seqid sync grabbed as well as the 2383 * 'os_sync_lock'. Note: this will release the open seqid sync and 2384 * 'os_sync_lock' if client recovery starts. Calling functions have to 2385 * be prepared to handle this. 2386 * 2387 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2388 * was needed and was started, and that the calling function should retry 2389 * this function; otherwise it is returned as 0. 2390 * 2391 * Errors are returned via the nfs4_error_t parameter. 2392 */ 2393 static void 2394 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2395 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2396 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2397 { 2398 COMPOUND4args_clnt args; 2399 COMPOUND4res_clnt res; 2400 CLOSE4args *close_args; 2401 nfs_resop4 *resop; 2402 nfs_argop4 argop[3]; 2403 int doqueue = 1; 2404 mntinfo4_t *mi; 2405 seqid4 seqid; 2406 vnode_t *vp; 2407 bool_t needrecov = FALSE; 2408 nfs4_lost_rqst_t lost_rqst; 2409 hrtime_t t; 2410 2411 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2412 2413 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2414 2415 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2416 2417 /* Only set this to 1 if recovery is started */ 2418 *recov = 0; 2419 2420 /* do the OTW call to close the file */ 2421 2422 if (close_type == CLOSE_RESEND) 2423 args.ctag = TAG_CLOSE_LOST; 2424 else if (close_type == CLOSE_AFTER_RESEND) 2425 args.ctag = TAG_CLOSE_UNDO; 2426 else 2427 args.ctag = TAG_CLOSE; 2428 2429 args.array_len = 3; 2430 args.array = argop; 2431 2432 vp = RTOV4(rp); 2433 2434 mi = VTOMI4(vp); 2435 2436 /* putfh target fh */ 2437 argop[0].argop = OP_CPUTFH; 2438 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2439 2440 argop[1].argop = OP_GETATTR; 2441 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2442 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2443 2444 argop[2].argop = OP_CLOSE; 2445 close_args = &argop[2].nfs_argop4_u.opclose; 2446 2447 seqid = nfs4_get_open_seqid(oop) + 1; 2448 2449 close_args->seqid = seqid; 2450 close_args->open_stateid = osp->open_stateid; 2451 2452 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2453 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2454 rnode4info(rp))); 2455 2456 t = gethrtime(); 2457 2458 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2459 2460 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2461 nfs4_set_open_seqid(seqid, oop, args.ctag); 2462 } 2463 2464 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2465 if (ep->error && !needrecov) { 2466 /* 2467 * if there was an error and no recovery is to be done 2468 * then then set up the file to flush its cache if 2469 * needed for the next caller. 2470 */ 2471 mutex_enter(&rp->r_statelock); 2472 PURGE_ATTRCACHE4_LOCKED(rp); 2473 rp->r_flags &= ~R4WRITEMODIFIED; 2474 mutex_exit(&rp->r_statelock); 2475 return; 2476 } 2477 2478 if (needrecov) { 2479 bool_t abort; 2480 nfs4_bseqid_entry_t *bsep = NULL; 2481 2482 if (close_type != CLOSE_RESEND) 2483 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2484 osp, cred_otw, vp); 2485 2486 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2487 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2488 0, args.ctag, close_args->seqid); 2489 2490 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2491 "nfs4close_otw: initiating recovery. error %d " 2492 "res.status %d", ep->error, res.status)); 2493 2494 /* 2495 * Drop the 'os_sync_lock' here so we don't hit 2496 * a potential recursive mutex_enter via an 2497 * 'open_stream_hold()'. 2498 */ 2499 mutex_exit(&osp->os_sync_lock); 2500 *have_sync_lockp = 0; 2501 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2502 (close_type != CLOSE_RESEND && 2503 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2504 OP_CLOSE, bsep); 2505 2506 /* drop open seq sync, and let the calling function regrab it */ 2507 nfs4_end_open_seqid_sync(oop); 2508 *did_start_seqid_syncp = 0; 2509 2510 if (bsep) 2511 kmem_free(bsep, sizeof (*bsep)); 2512 /* 2513 * For signals, the caller wants to quit, so don't say to 2514 * retry. For forced unmount, if it's a user thread, it 2515 * wants to quit. If it's a recovery thread, the retry 2516 * will happen higher-up on the call stack. Either way, 2517 * don't say to retry. 2518 */ 2519 if (abort == FALSE && ep->error != EINTR && 2520 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2521 close_type != CLOSE_RESEND && 2522 close_type != CLOSE_AFTER_RESEND) 2523 *recov = 1; 2524 else 2525 *recov = 0; 2526 2527 if (!ep->error) 2528 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2529 return; 2530 } 2531 2532 if (res.status) { 2533 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2534 return; 2535 } 2536 2537 mutex_enter(&rp->r_statev4_lock); 2538 rp->created_v4 = 0; 2539 mutex_exit(&rp->r_statev4_lock); 2540 2541 resop = &res.array[2]; 2542 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2543 osp->os_valid = 0; 2544 2545 /* 2546 * This removes the reference obtained at OPEN; ie, when the 2547 * open stream structure was created. 2548 * 2549 * We don't have to worry about calling 'open_stream_rele' 2550 * since we our currently holding a reference to the open 2551 * stream which means the count cannot go to 0 with this 2552 * decrement. 2553 */ 2554 ASSERT(osp->os_ref_count >= 2); 2555 osp->os_ref_count--; 2556 2557 if (!ep->error) 2558 nfs4_attr_cache(vp, 2559 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2560 t, cred_otw, TRUE, NULL); 2561 2562 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2563 " returning %d", ep->error)); 2564 2565 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2566 } 2567 2568 /* ARGSUSED */ 2569 static int 2570 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2571 caller_context_t *ct) 2572 { 2573 rnode4_t *rp; 2574 u_offset_t off; 2575 offset_t diff; 2576 uint_t on; 2577 uint_t n; 2578 caddr_t base; 2579 uint_t flags; 2580 int error; 2581 mntinfo4_t *mi; 2582 2583 rp = VTOR4(vp); 2584 2585 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2586 2587 if (IS_SHADOW(vp, rp)) 2588 vp = RTOV4(rp); 2589 2590 if (vp->v_type != VREG) 2591 return (EISDIR); 2592 2593 mi = VTOMI4(vp); 2594 2595 if (nfs_zone() != mi->mi_zone) 2596 return (EIO); 2597 2598 if (uiop->uio_resid == 0) 2599 return (0); 2600 2601 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2602 return (EINVAL); 2603 2604 mutex_enter(&rp->r_statelock); 2605 if (rp->r_flags & R4RECOVERRP) 2606 error = (rp->r_error ? rp->r_error : EIO); 2607 else 2608 error = 0; 2609 mutex_exit(&rp->r_statelock); 2610 if (error) 2611 return (error); 2612 2613 /* 2614 * Bypass VM if caching has been disabled (e.g., locking) or if 2615 * using client-side direct I/O and the file is not mmap'd and 2616 * there are no cached pages. 2617 */ 2618 if ((vp->v_flag & VNOCACHE) || 2619 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2620 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2621 size_t resid = 0; 2622 2623 return (nfs4read(vp, NULL, uiop->uio_loffset, 2624 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2625 } 2626 2627 error = 0; 2628 2629 do { 2630 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2631 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2632 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2633 2634 if (error = nfs4_validate_caches(vp, cr)) 2635 break; 2636 2637 mutex_enter(&rp->r_statelock); 2638 diff = rp->r_size - uiop->uio_loffset; 2639 mutex_exit(&rp->r_statelock); 2640 if (diff <= 0) 2641 break; 2642 if (diff < n) 2643 n = (uint_t)diff; 2644 2645 if (vpm_enable) { 2646 /* 2647 * Copy data. 2648 */ 2649 error = vpm_data_copy(vp, off + on, n, uiop, 2650 1, NULL, 0, S_READ); 2651 } else { 2652 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2653 S_READ); 2654 2655 error = uiomove(base + on, n, UIO_READ, uiop); 2656 } 2657 2658 if (!error) { 2659 /* 2660 * If read a whole block or read to eof, 2661 * won't need this buffer again soon. 2662 */ 2663 mutex_enter(&rp->r_statelock); 2664 if (n + on == MAXBSIZE || 2665 uiop->uio_loffset == rp->r_size) 2666 flags = SM_DONTNEED; 2667 else 2668 flags = 0; 2669 mutex_exit(&rp->r_statelock); 2670 if (vpm_enable) { 2671 error = vpm_sync_pages(vp, off, n, flags); 2672 } else { 2673 error = segmap_release(segkmap, base, flags); 2674 } 2675 } else { 2676 if (vpm_enable) { 2677 (void) vpm_sync_pages(vp, off, n, 0); 2678 } else { 2679 (void) segmap_release(segkmap, base, 0); 2680 } 2681 } 2682 } while (!error && uiop->uio_resid > 0); 2683 2684 return (error); 2685 } 2686 2687 /* ARGSUSED */ 2688 static int 2689 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2690 caller_context_t *ct) 2691 { 2692 rlim64_t limit = uiop->uio_llimit; 2693 rnode4_t *rp; 2694 u_offset_t off; 2695 caddr_t base; 2696 uint_t flags; 2697 int remainder; 2698 size_t n; 2699 int on; 2700 int error; 2701 int resid; 2702 u_offset_t offset; 2703 mntinfo4_t *mi; 2704 uint_t bsize; 2705 2706 rp = VTOR4(vp); 2707 2708 if (IS_SHADOW(vp, rp)) 2709 vp = RTOV4(rp); 2710 2711 if (vp->v_type != VREG) 2712 return (EISDIR); 2713 2714 mi = VTOMI4(vp); 2715 2716 if (nfs_zone() != mi->mi_zone) 2717 return (EIO); 2718 2719 if (uiop->uio_resid == 0) 2720 return (0); 2721 2722 mutex_enter(&rp->r_statelock); 2723 if (rp->r_flags & R4RECOVERRP) 2724 error = (rp->r_error ? rp->r_error : EIO); 2725 else 2726 error = 0; 2727 mutex_exit(&rp->r_statelock); 2728 if (error) 2729 return (error); 2730 2731 if (ioflag & FAPPEND) { 2732 struct vattr va; 2733 2734 /* 2735 * Must serialize if appending. 2736 */ 2737 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2738 nfs_rw_exit(&rp->r_rwlock); 2739 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2740 INTR(vp))) 2741 return (EINTR); 2742 } 2743 2744 va.va_mask = AT_SIZE; 2745 error = nfs4getattr(vp, &va, cr); 2746 if (error) 2747 return (error); 2748 uiop->uio_loffset = va.va_size; 2749 } 2750 2751 offset = uiop->uio_loffset + uiop->uio_resid; 2752 2753 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2754 return (EINVAL); 2755 2756 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2757 limit = MAXOFFSET_T; 2758 2759 /* 2760 * Check to make sure that the process will not exceed 2761 * its limit on file size. It is okay to write up to 2762 * the limit, but not beyond. Thus, the write which 2763 * reaches the limit will be short and the next write 2764 * will return an error. 2765 */ 2766 remainder = 0; 2767 if (offset > uiop->uio_llimit) { 2768 remainder = offset - uiop->uio_llimit; 2769 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2770 if (uiop->uio_resid <= 0) { 2771 proc_t *p = ttoproc(curthread); 2772 2773 uiop->uio_resid += remainder; 2774 mutex_enter(&p->p_lock); 2775 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2776 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2777 mutex_exit(&p->p_lock); 2778 return (EFBIG); 2779 } 2780 } 2781 2782 /* update the change attribute, if we have a write delegation */ 2783 2784 mutex_enter(&rp->r_statev4_lock); 2785 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2786 rp->r_deleg_change++; 2787 2788 mutex_exit(&rp->r_statev4_lock); 2789 2790 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2791 return (EINTR); 2792 2793 /* 2794 * Bypass VM if caching has been disabled (e.g., locking) or if 2795 * using client-side direct I/O and the file is not mmap'd and 2796 * there are no cached pages. 2797 */ 2798 if ((vp->v_flag & VNOCACHE) || 2799 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2800 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2801 size_t bufsize; 2802 int count; 2803 u_offset_t org_offset; 2804 stable_how4 stab_comm; 2805 nfs4_fwrite: 2806 if (rp->r_flags & R4STALE) { 2807 resid = uiop->uio_resid; 2808 offset = uiop->uio_loffset; 2809 error = rp->r_error; 2810 goto bottom; 2811 } 2812 2813 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2814 base = kmem_alloc(bufsize, KM_SLEEP); 2815 do { 2816 if (ioflag & FDSYNC) 2817 stab_comm = DATA_SYNC4; 2818 else 2819 stab_comm = FILE_SYNC4; 2820 resid = uiop->uio_resid; 2821 offset = uiop->uio_loffset; 2822 count = MIN(uiop->uio_resid, bufsize); 2823 org_offset = uiop->uio_loffset; 2824 error = uiomove(base, count, UIO_WRITE, uiop); 2825 if (!error) { 2826 error = nfs4write(vp, base, org_offset, 2827 count, cr, &stab_comm); 2828 if (!error) { 2829 mutex_enter(&rp->r_statelock); 2830 if (rp->r_size < uiop->uio_loffset) 2831 rp->r_size = uiop->uio_loffset; 2832 mutex_exit(&rp->r_statelock); 2833 } 2834 } 2835 } while (!error && uiop->uio_resid > 0); 2836 kmem_free(base, bufsize); 2837 goto bottom; 2838 } 2839 2840 bsize = vp->v_vfsp->vfs_bsize; 2841 2842 do { 2843 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2844 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2845 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2846 2847 resid = uiop->uio_resid; 2848 offset = uiop->uio_loffset; 2849 2850 if (rp->r_flags & R4STALE) { 2851 error = rp->r_error; 2852 break; 2853 } 2854 2855 /* 2856 * Don't create dirty pages faster than they 2857 * can be cleaned so that the system doesn't 2858 * get imbalanced. If the async queue is 2859 * maxed out, then wait for it to drain before 2860 * creating more dirty pages. Also, wait for 2861 * any threads doing pagewalks in the vop_getattr 2862 * entry points so that they don't block for 2863 * long periods. 2864 */ 2865 mutex_enter(&rp->r_statelock); 2866 while ((mi->mi_max_threads != 0 && 2867 rp->r_awcount > 2 * mi->mi_max_threads) || 2868 rp->r_gcount > 0) 2869 cv_wait(&rp->r_cv, &rp->r_statelock); 2870 mutex_exit(&rp->r_statelock); 2871 2872 if (vpm_enable) { 2873 /* 2874 * It will use kpm mappings, so no need to 2875 * pass an address. 2876 */ 2877 error = writerp4(rp, NULL, n, uiop, 0); 2878 } else { 2879 if (segmap_kpm) { 2880 int pon = uiop->uio_loffset & PAGEOFFSET; 2881 size_t pn = MIN(PAGESIZE - pon, 2882 uiop->uio_resid); 2883 int pagecreate; 2884 2885 mutex_enter(&rp->r_statelock); 2886 pagecreate = (pon == 0) && (pn == PAGESIZE || 2887 uiop->uio_loffset + pn >= rp->r_size); 2888 mutex_exit(&rp->r_statelock); 2889 2890 base = segmap_getmapflt(segkmap, vp, off + on, 2891 pn, !pagecreate, S_WRITE); 2892 2893 error = writerp4(rp, base + pon, n, uiop, 2894 pagecreate); 2895 2896 } else { 2897 base = segmap_getmapflt(segkmap, vp, off + on, 2898 n, 0, S_READ); 2899 error = writerp4(rp, base + on, n, uiop, 0); 2900 } 2901 } 2902 2903 if (!error) { 2904 if (mi->mi_flags & MI4_NOAC) 2905 flags = SM_WRITE; 2906 else if ((uiop->uio_loffset % bsize) == 0 || 2907 IS_SWAPVP(vp)) { 2908 /* 2909 * Have written a whole block. 2910 * Start an asynchronous write 2911 * and mark the buffer to 2912 * indicate that it won't be 2913 * needed again soon. 2914 */ 2915 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2916 } else 2917 flags = 0; 2918 if ((ioflag & (FSYNC|FDSYNC)) || 2919 (rp->r_flags & R4OUTOFSPACE)) { 2920 flags &= ~SM_ASYNC; 2921 flags |= SM_WRITE; 2922 } 2923 if (vpm_enable) { 2924 error = vpm_sync_pages(vp, off, n, flags); 2925 } else { 2926 error = segmap_release(segkmap, base, flags); 2927 } 2928 } else { 2929 if (vpm_enable) { 2930 (void) vpm_sync_pages(vp, off, n, 0); 2931 } else { 2932 (void) segmap_release(segkmap, base, 0); 2933 } 2934 /* 2935 * In the event that we got an access error while 2936 * faulting in a page for a write-only file just 2937 * force a write. 2938 */ 2939 if (error == EACCES) 2940 goto nfs4_fwrite; 2941 } 2942 } while (!error && uiop->uio_resid > 0); 2943 2944 bottom: 2945 if (error) { 2946 uiop->uio_resid = resid + remainder; 2947 uiop->uio_loffset = offset; 2948 } else { 2949 uiop->uio_resid += remainder; 2950 2951 mutex_enter(&rp->r_statev4_lock); 2952 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 2953 gethrestime(&rp->r_attr.va_mtime); 2954 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 2955 } 2956 mutex_exit(&rp->r_statev4_lock); 2957 } 2958 2959 nfs_rw_exit(&rp->r_lkserlock); 2960 2961 return (error); 2962 } 2963 2964 /* 2965 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 2966 */ 2967 static int 2968 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 2969 int flags, cred_t *cr) 2970 { 2971 struct buf *bp; 2972 int error; 2973 page_t *savepp; 2974 uchar_t fsdata; 2975 stable_how4 stab_comm; 2976 2977 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 2978 bp = pageio_setup(pp, len, vp, flags); 2979 ASSERT(bp != NULL); 2980 2981 /* 2982 * pageio_setup should have set b_addr to 0. This 2983 * is correct since we want to do I/O on a page 2984 * boundary. bp_mapin will use this addr to calculate 2985 * an offset, and then set b_addr to the kernel virtual 2986 * address it allocated for us. 2987 */ 2988 ASSERT(bp->b_un.b_addr == 0); 2989 2990 bp->b_edev = 0; 2991 bp->b_dev = 0; 2992 bp->b_lblkno = lbtodb(off); 2993 bp->b_file = vp; 2994 bp->b_offset = (offset_t)off; 2995 bp_mapin(bp); 2996 2997 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 2998 freemem > desfree) 2999 stab_comm = UNSTABLE4; 3000 else 3001 stab_comm = FILE_SYNC4; 3002 3003 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 3004 3005 bp_mapout(bp); 3006 pageio_done(bp); 3007 3008 if (stab_comm == UNSTABLE4) 3009 fsdata = C_DELAYCOMMIT; 3010 else 3011 fsdata = C_NOCOMMIT; 3012 3013 savepp = pp; 3014 do { 3015 pp->p_fsdata = fsdata; 3016 } while ((pp = pp->p_next) != savepp); 3017 3018 return (error); 3019 } 3020 3021 /* 3022 */ 3023 static int 3024 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3025 { 3026 nfs4_open_owner_t *oop; 3027 nfs4_open_stream_t *osp; 3028 rnode4_t *rp = VTOR4(vp); 3029 mntinfo4_t *mi = VTOMI4(vp); 3030 int reopen_needed; 3031 3032 ASSERT(nfs_zone() == mi->mi_zone); 3033 3034 3035 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3036 if (!oop) 3037 return (EIO); 3038 3039 /* returns with 'os_sync_lock' held */ 3040 osp = find_open_stream(oop, rp); 3041 if (!osp) { 3042 open_owner_rele(oop); 3043 return (EIO); 3044 } 3045 3046 if (osp->os_failed_reopen) { 3047 mutex_exit(&osp->os_sync_lock); 3048 open_stream_rele(osp, rp); 3049 open_owner_rele(oop); 3050 return (EIO); 3051 } 3052 3053 /* 3054 * Determine whether a reopen is needed. If this 3055 * is a delegation open stream, then the os_delegation bit 3056 * should be set. 3057 */ 3058 3059 reopen_needed = osp->os_delegation; 3060 3061 mutex_exit(&osp->os_sync_lock); 3062 open_owner_rele(oop); 3063 3064 if (reopen_needed) { 3065 nfs4_error_zinit(ep); 3066 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3067 mutex_enter(&osp->os_sync_lock); 3068 if (ep->error || ep->stat || osp->os_failed_reopen) { 3069 mutex_exit(&osp->os_sync_lock); 3070 open_stream_rele(osp, rp); 3071 return (EIO); 3072 } 3073 mutex_exit(&osp->os_sync_lock); 3074 } 3075 open_stream_rele(osp, rp); 3076 3077 return (0); 3078 } 3079 3080 /* 3081 * Write to file. Writes to remote server in largest size 3082 * chunks that the server can handle. Write is synchronous. 3083 */ 3084 static int 3085 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3086 stable_how4 *stab_comm) 3087 { 3088 mntinfo4_t *mi; 3089 COMPOUND4args_clnt args; 3090 COMPOUND4res_clnt res; 3091 WRITE4args *wargs; 3092 WRITE4res *wres; 3093 nfs_argop4 argop[2]; 3094 nfs_resop4 *resop; 3095 int tsize; 3096 stable_how4 stable; 3097 rnode4_t *rp; 3098 int doqueue = 1; 3099 bool_t needrecov; 3100 nfs4_recov_state_t recov_state; 3101 nfs4_stateid_types_t sid_types; 3102 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3103 3104 rp = VTOR4(vp); 3105 mi = VTOMI4(vp); 3106 3107 ASSERT(nfs_zone() == mi->mi_zone); 3108 3109 stable = *stab_comm; 3110 *stab_comm = FILE_SYNC4; 3111 3112 needrecov = FALSE; 3113 recov_state.rs_flags = 0; 3114 recov_state.rs_num_retry_despite_err = 0; 3115 nfs4_init_stateid_types(&sid_types); 3116 3117 recov_retry: 3118 args.ctag = TAG_WRITE; 3119 args.array_len = 2; 3120 args.array = argop; 3121 3122 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3123 &recov_state, NULL); 3124 if (e.error) 3125 return (e.error); 3126 3127 /* 0. putfh target fh */ 3128 argop[0].argop = OP_CPUTFH; 3129 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3130 3131 /* 1. write */ 3132 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3133 3134 do { 3135 3136 wargs->offset = (offset4)offset; 3137 wargs->data_val = base; 3138 3139 if (mi->mi_io_kstats) { 3140 mutex_enter(&mi->mi_lock); 3141 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3142 mutex_exit(&mi->mi_lock); 3143 } 3144 3145 if ((vp->v_flag & VNOCACHE) || 3146 (rp->r_flags & R4DIRECTIO) || 3147 (mi->mi_flags & MI4_DIRECTIO)) 3148 tsize = MIN(mi->mi_stsize, count); 3149 else 3150 tsize = MIN(mi->mi_curwrite, count); 3151 wargs->data_len = (uint_t)tsize; 3152 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3153 3154 if (mi->mi_io_kstats) { 3155 mutex_enter(&mi->mi_lock); 3156 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3157 mutex_exit(&mi->mi_lock); 3158 } 3159 3160 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3161 if (e.error && !needrecov) { 3162 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3163 &recov_state, needrecov); 3164 return (e.error); 3165 } 3166 3167 3168 /* 3169 * Do handling of OLD_STATEID outside 3170 * of the normal recovery framework. 3171 * 3172 * If write receives a BAD stateid error while using a 3173 * delegation stateid, retry using the open stateid (if it 3174 * exists). If it doesn't have an open stateid, reopen the 3175 * file first, then retry. 3176 */ 3177 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3178 sid_types.cur_sid_type != SPEC_SID) { 3179 nfs4_save_stateid(&wargs->stateid, &sid_types); 3180 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3181 &recov_state, needrecov); 3182 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3183 goto recov_retry; 3184 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3185 sid_types.cur_sid_type == DEL_SID) { 3186 nfs4_save_stateid(&wargs->stateid, &sid_types); 3187 mutex_enter(&rp->r_statev4_lock); 3188 rp->r_deleg_return_pending = TRUE; 3189 mutex_exit(&rp->r_statev4_lock); 3190 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3191 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3192 &recov_state, needrecov); 3193 (void) xdr_free(xdr_COMPOUND4res_clnt, 3194 (caddr_t)&res); 3195 return (EIO); 3196 } 3197 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3198 &recov_state, needrecov); 3199 /* hold needed for nfs4delegreturn_thread */ 3200 VN_HOLD(vp); 3201 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3202 NFS4_DR_DISCARD), FALSE); 3203 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3204 goto recov_retry; 3205 } 3206 3207 if (needrecov) { 3208 bool_t abort; 3209 3210 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3211 "nfs4write: client got error %d, res.status %d" 3212 ", so start recovery", e.error, res.status)); 3213 3214 abort = nfs4_start_recovery(&e, 3215 VTOMI4(vp), vp, NULL, &wargs->stateid, 3216 NULL, OP_WRITE, NULL); 3217 if (!e.error) { 3218 e.error = geterrno4(res.status); 3219 (void) xdr_free(xdr_COMPOUND4res_clnt, 3220 (caddr_t)&res); 3221 } 3222 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3223 &recov_state, needrecov); 3224 if (abort == FALSE) 3225 goto recov_retry; 3226 return (e.error); 3227 } 3228 3229 if (res.status) { 3230 e.error = geterrno4(res.status); 3231 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3232 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3233 &recov_state, needrecov); 3234 return (e.error); 3235 } 3236 3237 resop = &res.array[1]; /* write res */ 3238 wres = &resop->nfs_resop4_u.opwrite; 3239 3240 if ((int)wres->count > tsize) { 3241 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3242 3243 zcmn_err(getzoneid(), CE_WARN, 3244 "nfs4write: server wrote %u, requested was %u", 3245 (int)wres->count, tsize); 3246 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3247 &recov_state, needrecov); 3248 return (EIO); 3249 } 3250 if (wres->committed == UNSTABLE4) { 3251 *stab_comm = UNSTABLE4; 3252 if (wargs->stable == DATA_SYNC4 || 3253 wargs->stable == FILE_SYNC4) { 3254 (void) xdr_free(xdr_COMPOUND4res_clnt, 3255 (caddr_t)&res); 3256 zcmn_err(getzoneid(), CE_WARN, 3257 "nfs4write: server %s did not commit " 3258 "to stable storage", 3259 rp->r_server->sv_hostname); 3260 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3261 &recov_state, needrecov); 3262 return (EIO); 3263 } 3264 } 3265 3266 tsize = (int)wres->count; 3267 count -= tsize; 3268 base += tsize; 3269 offset += tsize; 3270 if (mi->mi_io_kstats) { 3271 mutex_enter(&mi->mi_lock); 3272 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3273 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3274 tsize; 3275 mutex_exit(&mi->mi_lock); 3276 } 3277 lwp_stat_update(LWP_STAT_OUBLK, 1); 3278 mutex_enter(&rp->r_statelock); 3279 if (rp->r_flags & R4HAVEVERF) { 3280 if (rp->r_writeverf != wres->writeverf) { 3281 nfs4_set_mod(vp); 3282 rp->r_writeverf = wres->writeverf; 3283 } 3284 } else { 3285 rp->r_writeverf = wres->writeverf; 3286 rp->r_flags |= R4HAVEVERF; 3287 } 3288 PURGE_ATTRCACHE4_LOCKED(rp); 3289 rp->r_flags |= R4WRITEMODIFIED; 3290 gethrestime(&rp->r_attr.va_mtime); 3291 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3292 mutex_exit(&rp->r_statelock); 3293 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3294 } while (count); 3295 3296 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, needrecov); 3297 3298 return (e.error); 3299 } 3300 3301 /* 3302 * Read from a file. Reads data in largest chunks our interface can handle. 3303 */ 3304 static int 3305 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3306 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3307 { 3308 mntinfo4_t *mi; 3309 COMPOUND4args_clnt args; 3310 COMPOUND4res_clnt res; 3311 READ4args *rargs; 3312 nfs_argop4 argop[2]; 3313 int tsize; 3314 int doqueue; 3315 rnode4_t *rp; 3316 int data_len; 3317 bool_t is_eof; 3318 bool_t needrecov = FALSE; 3319 nfs4_recov_state_t recov_state; 3320 nfs4_stateid_types_t sid_types; 3321 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3322 3323 rp = VTOR4(vp); 3324 mi = VTOMI4(vp); 3325 doqueue = 1; 3326 3327 ASSERT(nfs_zone() == mi->mi_zone); 3328 3329 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3330 3331 args.array_len = 2; 3332 args.array = argop; 3333 3334 nfs4_init_stateid_types(&sid_types); 3335 3336 recov_state.rs_flags = 0; 3337 recov_state.rs_num_retry_despite_err = 0; 3338 3339 recov_retry: 3340 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3341 &recov_state, NULL); 3342 if (e.error) 3343 return (e.error); 3344 3345 /* putfh target fh */ 3346 argop[0].argop = OP_CPUTFH; 3347 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3348 3349 /* read */ 3350 argop[1].argop = OP_READ; 3351 rargs = &argop[1].nfs_argop4_u.opread; 3352 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3353 OP_READ, &sid_types, async); 3354 3355 do { 3356 if (mi->mi_io_kstats) { 3357 mutex_enter(&mi->mi_lock); 3358 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3359 mutex_exit(&mi->mi_lock); 3360 } 3361 3362 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3363 "nfs4read: %s call, rp %s", 3364 needrecov ? "recov" : "first", 3365 rnode4info(rp))); 3366 3367 if ((vp->v_flag & VNOCACHE) || 3368 (rp->r_flags & R4DIRECTIO) || 3369 (mi->mi_flags & MI4_DIRECTIO)) 3370 tsize = MIN(mi->mi_tsize, count); 3371 else 3372 tsize = MIN(mi->mi_curread, count); 3373 rargs->offset = (offset4)offset; 3374 rargs->count = (count4)tsize; 3375 rargs->res_data_val_alt = NULL; 3376 rargs->res_mblk = NULL; 3377 rargs->res_uiop = NULL; 3378 rargs->res_maxsize = 0; 3379 if (uiop) 3380 rargs->res_uiop = uiop; 3381 else 3382 rargs->res_data_val_alt = base; 3383 rargs->res_maxsize = tsize; 3384 3385 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3386 #ifdef DEBUG 3387 if (nfs4read_error_inject) { 3388 res.status = nfs4read_error_inject; 3389 nfs4read_error_inject = 0; 3390 } 3391 #endif 3392 3393 if (mi->mi_io_kstats) { 3394 mutex_enter(&mi->mi_lock); 3395 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3396 mutex_exit(&mi->mi_lock); 3397 } 3398 3399 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3400 if (e.error != 0 && !needrecov) { 3401 nfs4_end_fop(mi, vp, NULL, OH_READ, 3402 &recov_state, needrecov); 3403 return (e.error); 3404 } 3405 3406 /* 3407 * Do proper retry for OLD and BAD stateid errors outside 3408 * of the normal recovery framework. There are two differences 3409 * between async and sync reads. The first is that we allow 3410 * retry on BAD_STATEID for async reads, but not sync reads. 3411 * The second is that we mark the file dead for a failed 3412 * attempt with a special stateid for sync reads, but just 3413 * return EIO for async reads. 3414 * 3415 * If a sync read receives a BAD stateid error while using a 3416 * delegation stateid, retry using the open stateid (if it 3417 * exists). If it doesn't have an open stateid, reopen the 3418 * file first, then retry. 3419 */ 3420 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3421 res.status == NFS4ERR_BAD_STATEID) && async) { 3422 nfs4_end_fop(mi, vp, NULL, OH_READ, 3423 &recov_state, needrecov); 3424 if (sid_types.cur_sid_type == SPEC_SID) { 3425 (void) xdr_free(xdr_COMPOUND4res_clnt, 3426 (caddr_t)&res); 3427 return (EIO); 3428 } 3429 nfs4_save_stateid(&rargs->stateid, &sid_types); 3430 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3431 goto recov_retry; 3432 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3433 !async && sid_types.cur_sid_type != SPEC_SID) { 3434 nfs4_save_stateid(&rargs->stateid, &sid_types); 3435 nfs4_end_fop(mi, vp, NULL, OH_READ, 3436 &recov_state, needrecov); 3437 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3438 goto recov_retry; 3439 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3440 sid_types.cur_sid_type == DEL_SID) { 3441 nfs4_save_stateid(&rargs->stateid, &sid_types); 3442 mutex_enter(&rp->r_statev4_lock); 3443 rp->r_deleg_return_pending = TRUE; 3444 mutex_exit(&rp->r_statev4_lock); 3445 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3446 nfs4_end_fop(mi, vp, NULL, OH_READ, 3447 &recov_state, needrecov); 3448 (void) xdr_free(xdr_COMPOUND4res_clnt, 3449 (caddr_t)&res); 3450 return (EIO); 3451 } 3452 nfs4_end_fop(mi, vp, NULL, OH_READ, 3453 &recov_state, needrecov); 3454 /* hold needed for nfs4delegreturn_thread */ 3455 VN_HOLD(vp); 3456 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3457 NFS4_DR_DISCARD), FALSE); 3458 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3459 goto recov_retry; 3460 } 3461 if (needrecov) { 3462 bool_t abort; 3463 3464 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3465 "nfs4read: initiating recovery\n")); 3466 3467 abort = nfs4_start_recovery(&e, 3468 mi, vp, NULL, &rargs->stateid, 3469 NULL, OP_READ, NULL); 3470 nfs4_end_fop(mi, vp, NULL, OH_READ, 3471 &recov_state, needrecov); 3472 /* 3473 * Do not retry if we got OLD_STATEID using a special 3474 * stateid. This avoids looping with a broken server. 3475 */ 3476 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3477 sid_types.cur_sid_type == SPEC_SID) 3478 abort = TRUE; 3479 3480 if (abort == FALSE) { 3481 /* 3482 * Need to retry all possible stateids in 3483 * case the recovery error wasn't stateid 3484 * related or the stateids have become 3485 * stale (server reboot). 3486 */ 3487 nfs4_init_stateid_types(&sid_types); 3488 (void) xdr_free(xdr_COMPOUND4res_clnt, 3489 (caddr_t)&res); 3490 goto recov_retry; 3491 } 3492 3493 if (!e.error) { 3494 e.error = geterrno4(res.status); 3495 (void) xdr_free(xdr_COMPOUND4res_clnt, 3496 (caddr_t)&res); 3497 } 3498 return (e.error); 3499 } 3500 3501 if (res.status) { 3502 e.error = geterrno4(res.status); 3503 nfs4_end_fop(mi, vp, NULL, OH_READ, 3504 &recov_state, needrecov); 3505 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3506 return (e.error); 3507 } 3508 3509 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3510 count -= data_len; 3511 if (base) 3512 base += data_len; 3513 offset += data_len; 3514 if (mi->mi_io_kstats) { 3515 mutex_enter(&mi->mi_lock); 3516 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3517 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3518 mutex_exit(&mi->mi_lock); 3519 } 3520 lwp_stat_update(LWP_STAT_INBLK, 1); 3521 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3522 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3523 3524 } while (count && !is_eof); 3525 3526 *residp = count; 3527 3528 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3529 3530 return (e.error); 3531 } 3532 3533 /* ARGSUSED */ 3534 static int 3535 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp, 3536 caller_context_t *ct) 3537 { 3538 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3539 return (EIO); 3540 switch (cmd) { 3541 case _FIODIRECTIO: 3542 return (nfs4_directio(vp, (int)arg, cr)); 3543 default: 3544 return (ENOTTY); 3545 } 3546 } 3547 3548 /* ARGSUSED */ 3549 int 3550 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3551 caller_context_t *ct) 3552 { 3553 int error; 3554 rnode4_t *rp = VTOR4(vp); 3555 3556 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3557 return (EIO); 3558 /* 3559 * If it has been specified that the return value will 3560 * just be used as a hint, and we are only being asked 3561 * for size, fsid or rdevid, then return the client's 3562 * notion of these values without checking to make sure 3563 * that the attribute cache is up to date. 3564 * The whole point is to avoid an over the wire GETATTR 3565 * call. 3566 */ 3567 if (flags & ATTR_HINT) { 3568 if (vap->va_mask == 3569 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) { 3570 mutex_enter(&rp->r_statelock); 3571 if (vap->va_mask | AT_SIZE) 3572 vap->va_size = rp->r_size; 3573 if (vap->va_mask | AT_FSID) 3574 vap->va_fsid = rp->r_attr.va_fsid; 3575 if (vap->va_mask | AT_RDEV) 3576 vap->va_rdev = rp->r_attr.va_rdev; 3577 mutex_exit(&rp->r_statelock); 3578 return (0); 3579 } 3580 } 3581 3582 /* 3583 * Only need to flush pages if asking for the mtime 3584 * and if there any dirty pages or any outstanding 3585 * asynchronous (write) requests for this file. 3586 */ 3587 if (vap->va_mask & AT_MTIME) { 3588 rp = VTOR4(vp); 3589 if (nfs4_has_pages(vp)) { 3590 mutex_enter(&rp->r_statev4_lock); 3591 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3592 mutex_exit(&rp->r_statev4_lock); 3593 if (rp->r_flags & R4DIRTY || 3594 rp->r_awcount > 0) { 3595 mutex_enter(&rp->r_statelock); 3596 rp->r_gcount++; 3597 mutex_exit(&rp->r_statelock); 3598 error = 3599 nfs4_putpage(vp, (u_offset_t)0, 3600 0, 0, cr, NULL); 3601 mutex_enter(&rp->r_statelock); 3602 if (error && (error == ENOSPC || 3603 error == EDQUOT)) { 3604 if (!rp->r_error) 3605 rp->r_error = error; 3606 } 3607 if (--rp->r_gcount == 0) 3608 cv_broadcast(&rp->r_cv); 3609 mutex_exit(&rp->r_statelock); 3610 } 3611 } else { 3612 mutex_exit(&rp->r_statev4_lock); 3613 } 3614 } 3615 } 3616 return (nfs4getattr(vp, vap, cr)); 3617 } 3618 3619 int 3620 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3621 { 3622 /* 3623 * If these are the only two bits cleared 3624 * on the server then return 0 (OK) else 3625 * return 1 (BAD). 3626 */ 3627 on_client &= ~(S_ISUID|S_ISGID); 3628 if (on_client == from_server) 3629 return (0); 3630 else 3631 return (1); 3632 } 3633 3634 /*ARGSUSED4*/ 3635 static int 3636 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3637 caller_context_t *ct) 3638 { 3639 if (vap->va_mask & AT_NOSET) 3640 return (EINVAL); 3641 3642 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3643 return (EIO); 3644 3645 /* 3646 * Don't call secpolicy_vnode_setattr, the client cannot 3647 * use its cached attributes to make security decisions 3648 * as the server may be faking mode bits or mapping uid/gid. 3649 * Always just let the server to the checking. 3650 * If we provide the ability to remove basic priviledges 3651 * to setattr (e.g. basic without chmod) then we will 3652 * need to add a check here before calling the server. 3653 */ 3654 3655 return (nfs4setattr(vp, vap, flags, cr, NULL)); 3656 } 3657 3658 /* 3659 * To replace the "guarded" version 3 setattr, we use two types of compound 3660 * setattr requests: 3661 * 1. The "normal" setattr, used when the size of the file isn't being 3662 * changed - { Putfh <fh>; Setattr; Getattr }/ 3663 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3664 * with only ctime as the argument. If the server ctime differs from 3665 * what is cached on the client, the verify will fail, but we would 3666 * already have the ctime from the preceding getattr, so just set it 3667 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3668 * Setattr; Getattr }. 3669 * 3670 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3671 * this setattr and NULL if they are not. 3672 */ 3673 static int 3674 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3675 vsecattr_t *vsap) 3676 { 3677 COMPOUND4args_clnt args; 3678 COMPOUND4res_clnt res, *resp = NULL; 3679 nfs4_ga_res_t *garp = NULL; 3680 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3681 nfs_argop4 argop[5]; 3682 int verify_argop = -1; 3683 int setattr_argop = 1; 3684 nfs_resop4 *resop; 3685 vattr_t va; 3686 rnode4_t *rp; 3687 int doqueue = 1; 3688 uint_t mask = vap->va_mask; 3689 mode_t omode; 3690 vsecattr_t *vsp; 3691 timestruc_t ctime; 3692 bool_t needrecov = FALSE; 3693 nfs4_recov_state_t recov_state; 3694 nfs4_stateid_types_t sid_types; 3695 stateid4 stateid; 3696 hrtime_t t; 3697 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3698 servinfo4_t *svp; 3699 bitmap4 supp_attrs; 3700 3701 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3702 rp = VTOR4(vp); 3703 nfs4_init_stateid_types(&sid_types); 3704 3705 /* 3706 * Only need to flush pages if there are any pages and 3707 * if the file is marked as dirty in some fashion. The 3708 * file must be flushed so that we can accurately 3709 * determine the size of the file and the cached data 3710 * after the SETATTR returns. A file is considered to 3711 * be dirty if it is either marked with R4DIRTY, has 3712 * outstanding i/o's active, or is mmap'd. In this 3713 * last case, we can't tell whether there are dirty 3714 * pages, so we flush just to be sure. 3715 */ 3716 if (nfs4_has_pages(vp) && 3717 ((rp->r_flags & R4DIRTY) || 3718 rp->r_count > 0 || 3719 rp->r_mapcnt > 0)) { 3720 ASSERT(vp->v_type != VCHR); 3721 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL); 3722 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3723 mutex_enter(&rp->r_statelock); 3724 if (!rp->r_error) 3725 rp->r_error = e.error; 3726 mutex_exit(&rp->r_statelock); 3727 } 3728 } 3729 3730 if (mask & AT_SIZE) { 3731 /* 3732 * Verification setattr compound for non-deleg AT_SIZE: 3733 * { Putfh; Getattr; Verify; Setattr; Getattr } 3734 * Set ctime local here (outside the do_again label) 3735 * so that subsequent retries (after failed VERIFY) 3736 * will use ctime from GETATTR results (from failed 3737 * verify compound) as VERIFY arg. 3738 * If file has delegation, then VERIFY(time_metadata) 3739 * is of little added value, so don't bother. 3740 */ 3741 mutex_enter(&rp->r_statev4_lock); 3742 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3743 rp->r_deleg_return_pending) { 3744 numops = 5; 3745 ctime = rp->r_attr.va_ctime; 3746 } 3747 mutex_exit(&rp->r_statev4_lock); 3748 } 3749 3750 recov_state.rs_flags = 0; 3751 recov_state.rs_num_retry_despite_err = 0; 3752 3753 args.ctag = TAG_SETATTR; 3754 do_again: 3755 recov_retry: 3756 setattr_argop = numops - 2; 3757 3758 args.array = argop; 3759 args.array_len = numops; 3760 3761 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3762 if (e.error) 3763 return (e.error); 3764 3765 3766 /* putfh target fh */ 3767 argop[0].argop = OP_CPUTFH; 3768 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3769 3770 if (numops == 5) { 3771 /* 3772 * We only care about the ctime, but need to get mtime 3773 * and size for proper cache update. 3774 */ 3775 /* getattr */ 3776 argop[1].argop = OP_GETATTR; 3777 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3778 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3779 3780 /* verify - set later in loop */ 3781 verify_argop = 2; 3782 } 3783 3784 /* setattr */ 3785 svp = rp->r_server; 3786 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3787 supp_attrs = svp->sv_supp_attrs; 3788 nfs_rw_exit(&svp->sv_lock); 3789 3790 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3791 supp_attrs, &e.error, &sid_types); 3792 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3793 if (e.error) { 3794 /* req time field(s) overflow - return immediately */ 3795 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3796 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3797 opsetattr.obj_attributes); 3798 return (e.error); 3799 } 3800 omode = rp->r_attr.va_mode; 3801 3802 /* getattr */ 3803 argop[numops-1].argop = OP_GETATTR; 3804 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3805 /* 3806 * If we are setting the ACL (indicated only by vsap != NULL), request 3807 * the ACL in this getattr. The ACL returned from this getattr will be 3808 * used in updating the ACL cache. 3809 */ 3810 if (vsap != NULL) 3811 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3812 FATTR4_ACL_MASK; 3813 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3814 3815 /* 3816 * setattr iterates if the object size is set and the cached ctime 3817 * does not match the file ctime. In that case, verify the ctime first. 3818 */ 3819 3820 do { 3821 if (verify_argop != -1) { 3822 /* 3823 * Verify that the ctime match before doing setattr. 3824 */ 3825 va.va_mask = AT_CTIME; 3826 va.va_ctime = ctime; 3827 svp = rp->r_server; 3828 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3829 supp_attrs = svp->sv_supp_attrs; 3830 nfs_rw_exit(&svp->sv_lock); 3831 e.error = nfs4args_verify(&argop[verify_argop], &va, 3832 OP_VERIFY, supp_attrs); 3833 if (e.error) { 3834 /* req time field(s) overflow - return */ 3835 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3836 needrecov); 3837 break; 3838 } 3839 } 3840 3841 doqueue = 1; 3842 3843 t = gethrtime(); 3844 3845 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3846 3847 /* 3848 * Purge the access cache and ACL cache if changing either the 3849 * owner of the file, the group owner, or the mode. These may 3850 * change the access permissions of the file, so purge old 3851 * information and start over again. 3852 */ 3853 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3854 (void) nfs4_access_purge_rp(rp); 3855 if (rp->r_secattr != NULL) { 3856 mutex_enter(&rp->r_statelock); 3857 vsp = rp->r_secattr; 3858 rp->r_secattr = NULL; 3859 mutex_exit(&rp->r_statelock); 3860 if (vsp != NULL) 3861 nfs4_acl_free_cache(vsp); 3862 } 3863 } 3864 3865 /* 3866 * If res.array_len == numops, then everything succeeded, 3867 * except for possibly the final getattr. If only the 3868 * last getattr failed, give up, and don't try recovery. 3869 */ 3870 if (res.array_len == numops) { 3871 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3872 needrecov); 3873 if (! e.error) 3874 resp = &res; 3875 break; 3876 } 3877 3878 /* 3879 * if either rpc call failed or completely succeeded - done 3880 */ 3881 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3882 if (e.error) { 3883 PURGE_ATTRCACHE4(vp); 3884 if (!needrecov) { 3885 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3886 needrecov); 3887 break; 3888 } 3889 } 3890 3891 /* 3892 * Do proper retry for OLD_STATEID outside of the normal 3893 * recovery framework. 3894 */ 3895 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3896 sid_types.cur_sid_type != SPEC_SID && 3897 sid_types.cur_sid_type != NO_SID) { 3898 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3899 needrecov); 3900 nfs4_save_stateid(&stateid, &sid_types); 3901 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3902 opsetattr.obj_attributes); 3903 if (verify_argop != -1) { 3904 nfs4args_verify_free(&argop[verify_argop]); 3905 verify_argop = -1; 3906 } 3907 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3908 goto recov_retry; 3909 } 3910 3911 if (needrecov) { 3912 bool_t abort; 3913 3914 abort = nfs4_start_recovery(&e, 3915 VTOMI4(vp), vp, NULL, NULL, NULL, 3916 OP_SETATTR, NULL); 3917 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3918 needrecov); 3919 /* 3920 * Do not retry if we failed with OLD_STATEID using 3921 * a special stateid. This is done to avoid looping 3922 * with a broken server. 3923 */ 3924 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3925 (sid_types.cur_sid_type == SPEC_SID || 3926 sid_types.cur_sid_type == NO_SID)) 3927 abort = TRUE; 3928 if (!e.error) { 3929 if (res.status == NFS4ERR_BADOWNER) 3930 nfs4_log_badowner(VTOMI4(vp), 3931 OP_SETATTR); 3932 3933 e.error = geterrno4(res.status); 3934 (void) xdr_free(xdr_COMPOUND4res_clnt, 3935 (caddr_t)&res); 3936 } 3937 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3938 opsetattr.obj_attributes); 3939 if (verify_argop != -1) { 3940 nfs4args_verify_free(&argop[verify_argop]); 3941 verify_argop = -1; 3942 } 3943 if (abort == FALSE) { 3944 /* 3945 * Need to retry all possible stateids in 3946 * case the recovery error wasn't stateid 3947 * related or the stateids have become 3948 * stale (server reboot). 3949 */ 3950 nfs4_init_stateid_types(&sid_types); 3951 goto recov_retry; 3952 } 3953 return (e.error); 3954 } 3955 3956 /* 3957 * Need to call nfs4_end_op before nfs4getattr to 3958 * avoid potential nfs4_start_op deadlock. See RFE 3959 * 4777612. Calls to nfs4_invalidate_pages() and 3960 * nfs4_purge_stale_fh() might also generate over the 3961 * wire calls which my cause nfs4_start_op() deadlock. 3962 */ 3963 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3964 3965 /* 3966 * Check to update lease. 3967 */ 3968 resp = &res; 3969 if (res.status == NFS4_OK) { 3970 break; 3971 } 3972 3973 /* 3974 * Check if verify failed to see if try again 3975 */ 3976 if ((verify_argop == -1) || (res.array_len != 3)) { 3977 /* 3978 * can't continue... 3979 */ 3980 if (res.status == NFS4ERR_BADOWNER) 3981 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 3982 3983 e.error = geterrno4(res.status); 3984 } else { 3985 /* 3986 * When the verify request fails, the client ctime is 3987 * not in sync with the server. This is the same as 3988 * the version 3 "not synchronized" error, and we 3989 * handle it in a similar manner (XXX do we need to???). 3990 * Use the ctime returned in the first getattr for 3991 * the input to the next verify. 3992 * If we couldn't get the attributes, then we give up 3993 * because we can't complete the operation as required. 3994 */ 3995 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 3996 } 3997 if (e.error) { 3998 PURGE_ATTRCACHE4(vp); 3999 nfs4_purge_stale_fh(e.error, vp, cr); 4000 } else { 4001 /* 4002 * retry with a new verify value 4003 */ 4004 ctime = garp->n4g_va.va_ctime; 4005 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4006 resp = NULL; 4007 } 4008 if (!e.error) { 4009 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4010 opsetattr.obj_attributes); 4011 if (verify_argop != -1) { 4012 nfs4args_verify_free(&argop[verify_argop]); 4013 verify_argop = -1; 4014 } 4015 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4016 goto do_again; 4017 } 4018 } while (!e.error); 4019 4020 if (e.error) { 4021 /* 4022 * If we are here, rfs4call has an irrecoverable error - return 4023 */ 4024 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4025 opsetattr.obj_attributes); 4026 if (verify_argop != -1) { 4027 nfs4args_verify_free(&argop[verify_argop]); 4028 verify_argop = -1; 4029 } 4030 if (resp) 4031 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4032 return (e.error); 4033 } 4034 4035 4036 4037 /* 4038 * If changing the size of the file, invalidate 4039 * any local cached data which is no longer part 4040 * of the file. We also possibly invalidate the 4041 * last page in the file. We could use 4042 * pvn_vpzero(), but this would mark the page as 4043 * modified and require it to be written back to 4044 * the server for no particularly good reason. 4045 * This way, if we access it, then we bring it 4046 * back in. A read should be cheaper than a 4047 * write. 4048 */ 4049 if (mask & AT_SIZE) { 4050 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4051 } 4052 4053 /* either no error or one of the postop getattr failed */ 4054 4055 /* 4056 * XXX Perform a simplified version of wcc checking. Instead of 4057 * have another getattr to get pre-op, just purge cache if 4058 * any of the ops prior to and including the getattr failed. 4059 * If the getattr succeeded then update the attrcache accordingly. 4060 */ 4061 4062 garp = NULL; 4063 if (res.status == NFS4_OK) { 4064 /* 4065 * Last getattr 4066 */ 4067 resop = &res.array[numops - 1]; 4068 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4069 } 4070 /* 4071 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4072 * rather than filling it. See the function itself for details. 4073 */ 4074 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4075 if (garp != NULL) { 4076 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4077 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4078 vs_ace4_destroy(&garp->n4g_vsa); 4079 } else { 4080 if (vsap != NULL) { 4081 /* 4082 * The ACL was supposed to be set and to be 4083 * returned in the last getattr of this 4084 * compound, but for some reason the getattr 4085 * result doesn't contain the ACL. In this 4086 * case, purge the ACL cache. 4087 */ 4088 if (rp->r_secattr != NULL) { 4089 mutex_enter(&rp->r_statelock); 4090 vsp = rp->r_secattr; 4091 rp->r_secattr = NULL; 4092 mutex_exit(&rp->r_statelock); 4093 if (vsp != NULL) 4094 nfs4_acl_free_cache(vsp); 4095 } 4096 } 4097 } 4098 } 4099 4100 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4101 /* 4102 * Set the size, rather than relying on getting it updated 4103 * via a GETATTR. With delegations the client tries to 4104 * suppress GETATTR calls. 4105 */ 4106 mutex_enter(&rp->r_statelock); 4107 rp->r_size = vap->va_size; 4108 mutex_exit(&rp->r_statelock); 4109 } 4110 4111 /* 4112 * Can free up request args and res 4113 */ 4114 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4115 opsetattr.obj_attributes); 4116 if (verify_argop != -1) { 4117 nfs4args_verify_free(&argop[verify_argop]); 4118 verify_argop = -1; 4119 } 4120 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4121 4122 /* 4123 * Some servers will change the mode to clear the setuid 4124 * and setgid bits when changing the uid or gid. The 4125 * client needs to compensate appropriately. 4126 */ 4127 if (mask & (AT_UID | AT_GID)) { 4128 int terror, do_setattr; 4129 4130 do_setattr = 0; 4131 va.va_mask = AT_MODE; 4132 terror = nfs4getattr(vp, &va, cr); 4133 if (!terror && 4134 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4135 (!(mask & AT_MODE) && va.va_mode != omode))) { 4136 va.va_mask = AT_MODE; 4137 if (mask & AT_MODE) { 4138 /* 4139 * We asked the mode to be changed and what 4140 * we just got from the server in getattr is 4141 * not what we wanted it to be, so set it now. 4142 */ 4143 va.va_mode = vap->va_mode; 4144 do_setattr = 1; 4145 } else { 4146 /* 4147 * We did not ask the mode to be changed, 4148 * Check to see that the server just cleared 4149 * I_SUID and I_GUID from it. If not then 4150 * set mode to omode with UID/GID cleared. 4151 */ 4152 if (nfs4_compare_modes(va.va_mode, omode)) { 4153 omode &= ~(S_ISUID|S_ISGID); 4154 va.va_mode = omode; 4155 do_setattr = 1; 4156 } 4157 } 4158 4159 if (do_setattr) 4160 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4161 } 4162 } 4163 4164 return (e.error); 4165 } 4166 4167 /* ARGSUSED */ 4168 static int 4169 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct) 4170 { 4171 COMPOUND4args_clnt args; 4172 COMPOUND4res_clnt res; 4173 int doqueue; 4174 uint32_t acc, resacc, argacc; 4175 rnode4_t *rp; 4176 cred_t *cred, *ncr, *ncrfree = NULL; 4177 nfs4_access_type_t cacc; 4178 int num_ops; 4179 nfs_argop4 argop[3]; 4180 nfs_resop4 *resop; 4181 bool_t needrecov = FALSE, do_getattr; 4182 nfs4_recov_state_t recov_state; 4183 int rpc_error; 4184 hrtime_t t; 4185 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4186 mntinfo4_t *mi = VTOMI4(vp); 4187 4188 if (nfs_zone() != mi->mi_zone) 4189 return (EIO); 4190 4191 acc = 0; 4192 if (mode & VREAD) 4193 acc |= ACCESS4_READ; 4194 if (mode & VWRITE) { 4195 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4196 return (EROFS); 4197 if (vp->v_type == VDIR) 4198 acc |= ACCESS4_DELETE; 4199 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4200 } 4201 if (mode & VEXEC) { 4202 if (vp->v_type == VDIR) 4203 acc |= ACCESS4_LOOKUP; 4204 else 4205 acc |= ACCESS4_EXECUTE; 4206 } 4207 4208 if (VTOR4(vp)->r_acache != NULL) { 4209 e.error = nfs4_validate_caches(vp, cr); 4210 if (e.error) 4211 return (e.error); 4212 } 4213 4214 rp = VTOR4(vp); 4215 if (vp->v_type == VDIR) 4216 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4217 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4218 else 4219 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4220 ACCESS4_EXECUTE; 4221 recov_state.rs_flags = 0; 4222 recov_state.rs_num_retry_despite_err = 0; 4223 4224 cred = cr; 4225 /* 4226 * ncr and ncrfree both initially 4227 * point to the memory area returned 4228 * by crnetadjust(); 4229 * ncrfree not NULL when exiting means 4230 * that we need to release it 4231 */ 4232 ncr = crnetadjust(cred); 4233 ncrfree = ncr; 4234 4235 tryagain: 4236 cacc = nfs4_access_check(rp, acc, cred); 4237 if (cacc == NFS4_ACCESS_ALLOWED) { 4238 if (ncrfree != NULL) 4239 crfree(ncrfree); 4240 return (0); 4241 } 4242 if (cacc == NFS4_ACCESS_DENIED) { 4243 /* 4244 * If the cred can be adjusted, try again 4245 * with the new cred. 4246 */ 4247 if (ncr != NULL) { 4248 cred = ncr; 4249 ncr = NULL; 4250 goto tryagain; 4251 } 4252 if (ncrfree != NULL) 4253 crfree(ncrfree); 4254 return (EACCES); 4255 } 4256 4257 recov_retry: 4258 /* 4259 * Don't take with r_statev4_lock here. r_deleg_type could 4260 * change as soon as lock is released. Since it is an int, 4261 * there is no atomicity issue. 4262 */ 4263 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4264 num_ops = do_getattr ? 3 : 2; 4265 4266 args.ctag = TAG_ACCESS; 4267 4268 args.array_len = num_ops; 4269 args.array = argop; 4270 4271 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4272 &recov_state, NULL)) { 4273 if (ncrfree != NULL) 4274 crfree(ncrfree); 4275 return (e.error); 4276 } 4277 4278 /* putfh target fh */ 4279 argop[0].argop = OP_CPUTFH; 4280 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4281 4282 /* access */ 4283 argop[1].argop = OP_ACCESS; 4284 argop[1].nfs_argop4_u.opaccess.access = argacc; 4285 4286 /* getattr */ 4287 if (do_getattr) { 4288 argop[2].argop = OP_GETATTR; 4289 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4290 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4291 } 4292 4293 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4294 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4295 rnode4info(VTOR4(vp)))); 4296 4297 doqueue = 1; 4298 t = gethrtime(); 4299 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4300 rpc_error = e.error; 4301 4302 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4303 if (needrecov) { 4304 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4305 "nfs4_access: initiating recovery\n")); 4306 4307 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4308 NULL, OP_ACCESS, NULL) == FALSE) { 4309 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4310 &recov_state, needrecov); 4311 if (!e.error) 4312 (void) xdr_free(xdr_COMPOUND4res_clnt, 4313 (caddr_t)&res); 4314 goto recov_retry; 4315 } 4316 } 4317 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4318 4319 if (e.error) 4320 goto out; 4321 4322 if (res.status) { 4323 e.error = geterrno4(res.status); 4324 /* 4325 * This might generate over the wire calls throught 4326 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4327 * here to avoid a deadlock. 4328 */ 4329 nfs4_purge_stale_fh(e.error, vp, cr); 4330 goto out; 4331 } 4332 resop = &res.array[1]; /* access res */ 4333 4334 resacc = resop->nfs_resop4_u.opaccess.access; 4335 4336 if (do_getattr) { 4337 resop++; /* getattr res */ 4338 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4339 t, cr, FALSE, NULL); 4340 } 4341 4342 if (!e.error) { 4343 nfs4_access_cache(rp, argacc, resacc, cred); 4344 /* 4345 * we just cached results with cred; if cred is the 4346 * adjusted credentials from crnetadjust, we do not want 4347 * to release them before exiting: hence setting ncrfree 4348 * to NULL 4349 */ 4350 if (cred != cr) 4351 ncrfree = NULL; 4352 /* XXX check the supported bits too? */ 4353 if ((acc & resacc) != acc) { 4354 /* 4355 * The following code implements the semantic 4356 * that a setuid root program has *at least* the 4357 * permissions of the user that is running the 4358 * program. See rfs3call() for more portions 4359 * of the implementation of this functionality. 4360 */ 4361 /* XXX-LP */ 4362 if (ncr != NULL) { 4363 (void) xdr_free(xdr_COMPOUND4res_clnt, 4364 (caddr_t)&res); 4365 cred = ncr; 4366 ncr = NULL; 4367 goto tryagain; 4368 } 4369 e.error = EACCES; 4370 } 4371 } 4372 4373 out: 4374 if (!rpc_error) 4375 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4376 4377 if (ncrfree != NULL) 4378 crfree(ncrfree); 4379 4380 return (e.error); 4381 } 4382 4383 /* ARGSUSED */ 4384 static int 4385 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct) 4386 { 4387 COMPOUND4args_clnt args; 4388 COMPOUND4res_clnt res; 4389 int doqueue; 4390 rnode4_t *rp; 4391 nfs_argop4 argop[3]; 4392 nfs_resop4 *resop; 4393 READLINK4res *lr_res; 4394 nfs4_ga_res_t *garp; 4395 uint_t len; 4396 char *linkdata; 4397 bool_t needrecov = FALSE; 4398 nfs4_recov_state_t recov_state; 4399 hrtime_t t; 4400 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4401 4402 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4403 return (EIO); 4404 /* 4405 * Can't readlink anything other than a symbolic link. 4406 */ 4407 if (vp->v_type != VLNK) 4408 return (EINVAL); 4409 4410 rp = VTOR4(vp); 4411 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4412 e.error = nfs4_validate_caches(vp, cr); 4413 if (e.error) 4414 return (e.error); 4415 mutex_enter(&rp->r_statelock); 4416 if (rp->r_symlink.contents != NULL) { 4417 e.error = uiomove(rp->r_symlink.contents, 4418 rp->r_symlink.len, UIO_READ, uiop); 4419 mutex_exit(&rp->r_statelock); 4420 return (e.error); 4421 } 4422 mutex_exit(&rp->r_statelock); 4423 } 4424 recov_state.rs_flags = 0; 4425 recov_state.rs_num_retry_despite_err = 0; 4426 4427 recov_retry: 4428 args.array_len = 3; 4429 args.array = argop; 4430 args.ctag = TAG_READLINK; 4431 4432 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4433 if (e.error) { 4434 return (e.error); 4435 } 4436 4437 /* 0. putfh symlink fh */ 4438 argop[0].argop = OP_CPUTFH; 4439 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4440 4441 /* 1. readlink */ 4442 argop[1].argop = OP_READLINK; 4443 4444 /* 2. getattr */ 4445 argop[2].argop = OP_GETATTR; 4446 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4447 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4448 4449 doqueue = 1; 4450 4451 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4452 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4453 rnode4info(VTOR4(vp)))); 4454 4455 t = gethrtime(); 4456 4457 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4458 4459 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4460 if (needrecov) { 4461 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4462 "nfs4_readlink: initiating recovery\n")); 4463 4464 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4465 NULL, OP_READLINK, NULL) == FALSE) { 4466 if (!e.error) 4467 (void) xdr_free(xdr_COMPOUND4res_clnt, 4468 (caddr_t)&res); 4469 4470 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4471 needrecov); 4472 goto recov_retry; 4473 } 4474 } 4475 4476 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4477 4478 if (e.error) 4479 return (e.error); 4480 4481 /* 4482 * There is an path in the code below which calls 4483 * nfs4_purge_stale_fh(), which may generate otw calls through 4484 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4485 * here to avoid nfs4_start_op() deadlock. 4486 */ 4487 4488 if (res.status && (res.array_len < args.array_len)) { 4489 /* 4490 * either Putfh or Link failed 4491 */ 4492 e.error = geterrno4(res.status); 4493 nfs4_purge_stale_fh(e.error, vp, cr); 4494 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4495 return (e.error); 4496 } 4497 4498 resop = &res.array[1]; /* readlink res */ 4499 lr_res = &resop->nfs_resop4_u.opreadlink; 4500 4501 /* 4502 * treat symlink names as data 4503 */ 4504 linkdata = utf8_to_str(&lr_res->link, &len, NULL); 4505 if (linkdata != NULL) { 4506 int uio_len = len - 1; 4507 /* len includes null byte, which we won't uiomove */ 4508 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4509 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4510 mutex_enter(&rp->r_statelock); 4511 if (rp->r_symlink.contents == NULL) { 4512 rp->r_symlink.contents = linkdata; 4513 rp->r_symlink.len = uio_len; 4514 rp->r_symlink.size = len; 4515 mutex_exit(&rp->r_statelock); 4516 } else { 4517 mutex_exit(&rp->r_statelock); 4518 kmem_free(linkdata, len); 4519 } 4520 } else { 4521 kmem_free(linkdata, len); 4522 } 4523 } 4524 if (res.status == NFS4_OK) { 4525 resop++; /* getattr res */ 4526 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4527 } 4528 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4529 4530 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4531 4532 /* 4533 * The over the wire error for attempting to readlink something 4534 * other than a symbolic link is ENXIO. However, we need to 4535 * return EINVAL instead of ENXIO, so we map it here. 4536 */ 4537 return (e.error == ENXIO ? EINVAL : e.error); 4538 } 4539 4540 /* 4541 * Flush local dirty pages to stable storage on the server. 4542 * 4543 * If FNODSYNC is specified, then there is nothing to do because 4544 * metadata changes are not cached on the client before being 4545 * sent to the server. 4546 */ 4547 /* ARGSUSED */ 4548 static int 4549 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 4550 { 4551 int error; 4552 4553 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4554 return (0); 4555 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4556 return (EIO); 4557 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4558 if (!error) 4559 error = VTOR4(vp)->r_error; 4560 return (error); 4561 } 4562 4563 /* 4564 * Weirdness: if the file was removed or the target of a rename 4565 * operation while it was open, it got renamed instead. Here we 4566 * remove the renamed file. 4567 */ 4568 /* ARGSUSED */ 4569 void 4570 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4571 { 4572 rnode4_t *rp; 4573 4574 ASSERT(vp != DNLC_NO_VNODE); 4575 4576 rp = VTOR4(vp); 4577 4578 if (IS_SHADOW(vp, rp)) { 4579 sv_inactive(vp); 4580 return; 4581 } 4582 4583 /* 4584 * If this is coming from the wrong zone, we let someone in the right 4585 * zone take care of it asynchronously. We can get here due to 4586 * VN_RELE() being called from pageout() or fsflush(). This call may 4587 * potentially turn into an expensive no-op if, for instance, v_count 4588 * gets incremented in the meantime, but it's still correct. 4589 */ 4590 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4591 nfs4_async_inactive(vp, cr); 4592 return; 4593 } 4594 4595 /* 4596 * Some of the cleanup steps might require over-the-wire 4597 * operations. Since VOP_INACTIVE can get called as a result of 4598 * other over-the-wire operations (e.g., an attribute cache update 4599 * can lead to a DNLC purge), doing those steps now would lead to a 4600 * nested call to the recovery framework, which can deadlock. So 4601 * do any over-the-wire cleanups asynchronously, in a separate 4602 * thread. 4603 */ 4604 4605 mutex_enter(&rp->r_os_lock); 4606 mutex_enter(&rp->r_statelock); 4607 mutex_enter(&rp->r_statev4_lock); 4608 4609 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4610 mutex_exit(&rp->r_statev4_lock); 4611 mutex_exit(&rp->r_statelock); 4612 mutex_exit(&rp->r_os_lock); 4613 nfs4_async_inactive(vp, cr); 4614 return; 4615 } 4616 4617 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4618 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4619 mutex_exit(&rp->r_statev4_lock); 4620 mutex_exit(&rp->r_statelock); 4621 mutex_exit(&rp->r_os_lock); 4622 nfs4_async_inactive(vp, cr); 4623 return; 4624 } 4625 4626 if (rp->r_unldvp != NULL) { 4627 mutex_exit(&rp->r_statev4_lock); 4628 mutex_exit(&rp->r_statelock); 4629 mutex_exit(&rp->r_os_lock); 4630 nfs4_async_inactive(vp, cr); 4631 return; 4632 } 4633 mutex_exit(&rp->r_statev4_lock); 4634 mutex_exit(&rp->r_statelock); 4635 mutex_exit(&rp->r_os_lock); 4636 4637 rp4_addfree(rp, cr); 4638 } 4639 4640 /* 4641 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4642 * various bits of state. The caller must not refer to vp after this call. 4643 */ 4644 4645 void 4646 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4647 { 4648 rnode4_t *rp = VTOR4(vp); 4649 nfs4_recov_state_t recov_state; 4650 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4651 vnode_t *unldvp; 4652 char *unlname; 4653 cred_t *unlcred; 4654 COMPOUND4args_clnt args; 4655 COMPOUND4res_clnt res, *resp; 4656 nfs_argop4 argop[2]; 4657 int doqueue; 4658 #ifdef DEBUG 4659 char *name; 4660 #endif 4661 4662 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4663 ASSERT(!IS_SHADOW(vp, rp)); 4664 4665 #ifdef DEBUG 4666 name = fn_name(VTOSV(vp)->sv_name); 4667 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4668 "release vnode %s", name)); 4669 kmem_free(name, MAXNAMELEN); 4670 #endif 4671 4672 if (vp->v_type == VREG) { 4673 bool_t recov_failed = FALSE; 4674 4675 e.error = nfs4close_all(vp, cr); 4676 if (e.error) { 4677 /* Check to see if recovery failed */ 4678 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4679 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4680 recov_failed = TRUE; 4681 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4682 if (!recov_failed) { 4683 mutex_enter(&rp->r_statelock); 4684 if (rp->r_flags & R4RECOVERR) 4685 recov_failed = TRUE; 4686 mutex_exit(&rp->r_statelock); 4687 } 4688 if (recov_failed) { 4689 NFS4_DEBUG(nfs4_client_recov_debug, 4690 (CE_NOTE, "nfs4_inactive_otw: " 4691 "close failed (recovery failure)")); 4692 } 4693 } 4694 } 4695 4696 redo: 4697 if (rp->r_unldvp == NULL) { 4698 rp4_addfree(rp, cr); 4699 return; 4700 } 4701 4702 /* 4703 * Save the vnode pointer for the directory where the 4704 * unlinked-open file got renamed, then set it to NULL 4705 * to prevent another thread from getting here before 4706 * we're done with the remove. While we have the 4707 * statelock, make local copies of the pertinent rnode 4708 * fields. If we weren't to do this in an atomic way, the 4709 * the unl* fields could become inconsistent with respect 4710 * to each other due to a race condition between this 4711 * code and nfs_remove(). See bug report 1034328. 4712 */ 4713 mutex_enter(&rp->r_statelock); 4714 if (rp->r_unldvp == NULL) { 4715 mutex_exit(&rp->r_statelock); 4716 rp4_addfree(rp, cr); 4717 return; 4718 } 4719 4720 unldvp = rp->r_unldvp; 4721 rp->r_unldvp = NULL; 4722 unlname = rp->r_unlname; 4723 rp->r_unlname = NULL; 4724 unlcred = rp->r_unlcred; 4725 rp->r_unlcred = NULL; 4726 mutex_exit(&rp->r_statelock); 4727 4728 /* 4729 * If there are any dirty pages left, then flush 4730 * them. This is unfortunate because they just 4731 * may get thrown away during the remove operation, 4732 * but we have to do this for correctness. 4733 */ 4734 if (nfs4_has_pages(vp) && 4735 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4736 ASSERT(vp->v_type != VCHR); 4737 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL); 4738 if (e.error) { 4739 mutex_enter(&rp->r_statelock); 4740 if (!rp->r_error) 4741 rp->r_error = e.error; 4742 mutex_exit(&rp->r_statelock); 4743 } 4744 } 4745 4746 recov_state.rs_flags = 0; 4747 recov_state.rs_num_retry_despite_err = 0; 4748 recov_retry_remove: 4749 /* 4750 * Do the remove operation on the renamed file 4751 */ 4752 args.ctag = TAG_INACTIVE; 4753 4754 /* 4755 * Remove ops: putfh dir; remove 4756 */ 4757 args.array_len = 2; 4758 args.array = argop; 4759 4760 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4761 if (e.error) { 4762 kmem_free(unlname, MAXNAMELEN); 4763 crfree(unlcred); 4764 VN_RELE(unldvp); 4765 /* 4766 * Try again; this time around r_unldvp will be NULL, so we'll 4767 * just call rp4_addfree() and return. 4768 */ 4769 goto redo; 4770 } 4771 4772 /* putfh directory */ 4773 argop[0].argop = OP_CPUTFH; 4774 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4775 4776 /* remove */ 4777 argop[1].argop = OP_CREMOVE; 4778 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4779 4780 doqueue = 1; 4781 resp = &res; 4782 4783 #if 0 /* notyet */ 4784 /* 4785 * Can't do this yet. We may be being called from 4786 * dnlc_purge_XXX while that routine is holding a 4787 * mutex lock to the nc_rele list. The calls to 4788 * nfs3_cache_wcc_data may result in calls to 4789 * dnlc_purge_XXX. This will result in a deadlock. 4790 */ 4791 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4792 if (e.error) { 4793 PURGE_ATTRCACHE4(unldvp); 4794 resp = NULL; 4795 } else if (res.status) { 4796 e.error = geterrno4(res.status); 4797 PURGE_ATTRCACHE4(unldvp); 4798 /* 4799 * This code is inactive right now 4800 * but if made active there should 4801 * be a nfs4_end_op() call before 4802 * nfs4_purge_stale_fh to avoid start_op() 4803 * deadlock. See BugId: 4948726 4804 */ 4805 nfs4_purge_stale_fh(error, unldvp, cr); 4806 } else { 4807 nfs_resop4 *resop; 4808 REMOVE4res *rm_res; 4809 4810 resop = &res.array[1]; 4811 rm_res = &resop->nfs_resop4_u.opremove; 4812 /* 4813 * Update directory cache attribute, 4814 * readdir and dnlc caches. 4815 */ 4816 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4817 } 4818 #else 4819 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4820 4821 PURGE_ATTRCACHE4(unldvp); 4822 #endif 4823 4824 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4825 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4826 NULL, NULL, OP_REMOVE, NULL) == FALSE) { 4827 if (!e.error) 4828 (void) xdr_free(xdr_COMPOUND4res_clnt, 4829 (caddr_t)&res); 4830 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4831 &recov_state, TRUE); 4832 goto recov_retry_remove; 4833 } 4834 } 4835 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4836 4837 /* 4838 * Release stuff held for the remove 4839 */ 4840 VN_RELE(unldvp); 4841 if (!e.error && resp) 4842 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4843 4844 kmem_free(unlname, MAXNAMELEN); 4845 crfree(unlcred); 4846 goto redo; 4847 } 4848 4849 /* 4850 * Remote file system operations having to do with directory manipulation. 4851 */ 4852 /* ARGSUSED3 */ 4853 int 4854 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4855 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 4856 int *direntflags, pathname_t *realpnp) 4857 { 4858 int error; 4859 vnode_t *vp, *avp = NULL; 4860 rnode4_t *drp; 4861 4862 *vpp = NULL; 4863 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4864 return (EPERM); 4865 /* 4866 * if LOOKUP_XATTR, must replace dvp (object) with 4867 * object's attrdir before continuing with lookup 4868 */ 4869 if (flags & LOOKUP_XATTR) { 4870 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4871 if (error) 4872 return (error); 4873 4874 dvp = avp; 4875 4876 /* 4877 * If lookup is for "", just return dvp now. The attrdir 4878 * has already been activated (from nfs4lookup_xattr), and 4879 * the caller will RELE the original dvp -- not 4880 * the attrdir. So, set vpp and return. 4881 * Currently, when the LOOKUP_XATTR flag is 4882 * passed to VOP_LOOKUP, the name is always empty, and 4883 * shortcircuiting here avoids 3 unneeded lock/unlock 4884 * pairs. 4885 * 4886 * If a non-empty name was provided, then it is the 4887 * attribute name, and it will be looked up below. 4888 */ 4889 if (*nm == '\0') { 4890 *vpp = dvp; 4891 return (0); 4892 } 4893 4894 /* 4895 * The vfs layer never sends a name when asking for the 4896 * attrdir, so we should never get here (unless of course 4897 * name is passed at some time in future -- at which time 4898 * we'll blow up here). 4899 */ 4900 ASSERT(0); 4901 } 4902 4903 drp = VTOR4(dvp); 4904 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4905 return (EINTR); 4906 4907 error = nfs4lookup(dvp, nm, vpp, cr, 0); 4908 nfs_rw_exit(&drp->r_rwlock); 4909 4910 /* 4911 * If vnode is a device, create special vnode. 4912 */ 4913 if (!error && ISVDEV((*vpp)->v_type)) { 4914 vp = *vpp; 4915 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 4916 VN_RELE(vp); 4917 } 4918 4919 return (error); 4920 } 4921 4922 /* ARGSUSED */ 4923 static int 4924 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 4925 { 4926 int error; 4927 rnode4_t *drp; 4928 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 4929 mntinfo4_t *mi; 4930 4931 mi = VTOMI4(dvp); 4932 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) && 4933 !vfs_has_feature(mi->mi_vfsp, VFSFT_XVATTR)) 4934 return (EINVAL); 4935 4936 drp = VTOR4(dvp); 4937 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4938 return (EINTR); 4939 4940 mutex_enter(&drp->r_statelock); 4941 /* 4942 * If the server doesn't support xattrs just return EINVAL 4943 */ 4944 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 4945 mutex_exit(&drp->r_statelock); 4946 nfs_rw_exit(&drp->r_rwlock); 4947 return (EINVAL); 4948 } 4949 4950 /* 4951 * If there is a cached xattr directory entry, 4952 * use it as long as the attributes are valid. If the 4953 * attributes are not valid, take the simple approach and 4954 * free the cached value and re-fetch a new value. 4955 * 4956 * We don't negative entry cache for now, if we did we 4957 * would need to check if the file has changed on every 4958 * lookup. But xattrs don't exist very often and failing 4959 * an openattr is not much more expensive than and NVERIFY or GETATTR 4960 * so do an openattr over the wire for now. 4961 */ 4962 if (drp->r_xattr_dir != NULL) { 4963 if (ATTRCACHE4_VALID(dvp)) { 4964 VN_HOLD(drp->r_xattr_dir); 4965 *vpp = drp->r_xattr_dir; 4966 mutex_exit(&drp->r_statelock); 4967 nfs_rw_exit(&drp->r_rwlock); 4968 return (0); 4969 } 4970 VN_RELE(drp->r_xattr_dir); 4971 drp->r_xattr_dir = NULL; 4972 } 4973 mutex_exit(&drp->r_statelock); 4974 4975 error = nfs4openattr(dvp, vpp, cflag, cr); 4976 4977 nfs_rw_exit(&drp->r_rwlock); 4978 4979 return (error); 4980 } 4981 4982 static int 4983 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 4984 { 4985 int error; 4986 rnode4_t *drp; 4987 4988 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 4989 4990 /* 4991 * If lookup is for "", just return dvp. Don't need 4992 * to send it over the wire, look it up in the dnlc, 4993 * or perform any access checks. 4994 */ 4995 if (*nm == '\0') { 4996 VN_HOLD(dvp); 4997 *vpp = dvp; 4998 return (0); 4999 } 5000 5001 /* 5002 * Can't do lookups in non-directories. 5003 */ 5004 if (dvp->v_type != VDIR) 5005 return (ENOTDIR); 5006 5007 /* 5008 * If lookup is for ".", just return dvp. Don't need 5009 * to send it over the wire or look it up in the dnlc, 5010 * just need to check access. 5011 */ 5012 if (nm[0] == '.' && nm[1] == '\0') { 5013 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5014 if (error) 5015 return (error); 5016 VN_HOLD(dvp); 5017 *vpp = dvp; 5018 return (0); 5019 } 5020 5021 drp = VTOR4(dvp); 5022 if (!(drp->r_flags & R4LOOKUP)) { 5023 mutex_enter(&drp->r_statelock); 5024 drp->r_flags |= R4LOOKUP; 5025 mutex_exit(&drp->r_statelock); 5026 } 5027 5028 *vpp = NULL; 5029 /* 5030 * Lookup this name in the DNLC. If there is no entry 5031 * lookup over the wire. 5032 */ 5033 if (!skipdnlc) 5034 *vpp = dnlc_lookup(dvp, nm); 5035 if (*vpp == NULL) { 5036 /* 5037 * We need to go over the wire to lookup the name. 5038 */ 5039 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5040 } 5041 5042 /* 5043 * We hit on the dnlc 5044 */ 5045 if (*vpp != DNLC_NO_VNODE || 5046 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5047 /* 5048 * But our attrs may not be valid. 5049 */ 5050 if (ATTRCACHE4_VALID(dvp)) { 5051 error = nfs4_waitfor_purge_complete(dvp); 5052 if (error) { 5053 VN_RELE(*vpp); 5054 *vpp = NULL; 5055 return (error); 5056 } 5057 5058 /* 5059 * If after the purge completes, check to make sure 5060 * our attrs are still valid. 5061 */ 5062 if (ATTRCACHE4_VALID(dvp)) { 5063 /* 5064 * If we waited for a purge we may have 5065 * lost our vnode so look it up again. 5066 */ 5067 VN_RELE(*vpp); 5068 *vpp = dnlc_lookup(dvp, nm); 5069 if (*vpp == NULL) 5070 return (nfs4lookupnew_otw(dvp, 5071 nm, vpp, cr)); 5072 5073 /* 5074 * The access cache should almost always hit 5075 */ 5076 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5077 5078 if (error) { 5079 VN_RELE(*vpp); 5080 *vpp = NULL; 5081 return (error); 5082 } 5083 if (*vpp == DNLC_NO_VNODE) { 5084 VN_RELE(*vpp); 5085 *vpp = NULL; 5086 return (ENOENT); 5087 } 5088 return (0); 5089 } 5090 } 5091 } 5092 5093 ASSERT(*vpp != NULL); 5094 5095 /* 5096 * We may have gotten here we have one of the following cases: 5097 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5098 * need to validate them. 5099 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5100 * must validate. 5101 * 5102 * Go to the server and check if the directory has changed, if 5103 * it hasn't we are done and can use the dnlc entry. 5104 */ 5105 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5106 } 5107 5108 /* 5109 * Go to the server and check if the directory has changed, if 5110 * it hasn't we are done and can use the dnlc entry. If it 5111 * has changed we get a new copy of its attributes and check 5112 * the access for VEXEC, then relookup the filename and 5113 * get its filehandle and attributes. 5114 * 5115 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5116 * if the NVERIFY failed we must 5117 * purge the caches 5118 * cache new attributes (will set r_time_attr_inval) 5119 * cache new access 5120 * recheck VEXEC access 5121 * add name to dnlc, possibly negative 5122 * if LOOKUP succeeded 5123 * cache new attributes 5124 * else 5125 * set a new r_time_attr_inval for dvp 5126 * check to make sure we have access 5127 * 5128 * The vpp returned is the vnode passed in if the directory is valid, 5129 * a new vnode if successful lookup, or NULL on error. 5130 */ 5131 static int 5132 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5133 { 5134 COMPOUND4args_clnt args; 5135 COMPOUND4res_clnt res; 5136 fattr4 *ver_fattr; 5137 fattr4_change dchange; 5138 int32_t *ptr; 5139 int argoplist_size = 7 * sizeof (nfs_argop4); 5140 nfs_argop4 *argop; 5141 int doqueue; 5142 mntinfo4_t *mi; 5143 nfs4_recov_state_t recov_state; 5144 hrtime_t t; 5145 int isdotdot; 5146 vnode_t *nvp; 5147 nfs_fh4 *fhp; 5148 nfs4_sharedfh_t *sfhp; 5149 nfs4_access_type_t cacc; 5150 rnode4_t *nrp; 5151 rnode4_t *drp = VTOR4(dvp); 5152 nfs4_ga_res_t *garp = NULL; 5153 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5154 5155 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5156 ASSERT(nm != NULL); 5157 ASSERT(nm[0] != '\0'); 5158 ASSERT(dvp->v_type == VDIR); 5159 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5160 ASSERT(*vpp != NULL); 5161 5162 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5163 isdotdot = 1; 5164 args.ctag = TAG_LOOKUP_VPARENT; 5165 } else { 5166 /* 5167 * If dvp were a stub, it should have triggered and caused 5168 * a mount for us to get this far. 5169 */ 5170 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5171 5172 isdotdot = 0; 5173 args.ctag = TAG_LOOKUP_VALID; 5174 } 5175 5176 mi = VTOMI4(dvp); 5177 recov_state.rs_flags = 0; 5178 recov_state.rs_num_retry_despite_err = 0; 5179 5180 nvp = NULL; 5181 5182 /* Save the original mount point security information */ 5183 (void) save_mnt_secinfo(mi->mi_curr_serv); 5184 5185 recov_retry: 5186 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5187 &recov_state, NULL); 5188 if (e.error) { 5189 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5190 VN_RELE(*vpp); 5191 *vpp = NULL; 5192 return (e.error); 5193 } 5194 5195 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5196 5197 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5198 args.array_len = 7; 5199 args.array = argop; 5200 5201 /* 0. putfh file */ 5202 argop[0].argop = OP_CPUTFH; 5203 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5204 5205 /* 1. nverify the change info */ 5206 argop[1].argop = OP_NVERIFY; 5207 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5208 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5209 ver_fattr->attrlist4 = (char *)&dchange; 5210 ptr = (int32_t *)&dchange; 5211 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5212 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5213 5214 /* 2. getattr directory */ 5215 argop[2].argop = OP_GETATTR; 5216 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5217 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5218 5219 /* 3. access directory */ 5220 argop[3].argop = OP_ACCESS; 5221 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5222 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5223 5224 /* 4. lookup name */ 5225 if (isdotdot) { 5226 argop[4].argop = OP_LOOKUPP; 5227 } else { 5228 argop[4].argop = OP_CLOOKUP; 5229 argop[4].nfs_argop4_u.opclookup.cname = nm; 5230 } 5231 5232 /* 5. resulting file handle */ 5233 argop[5].argop = OP_GETFH; 5234 5235 /* 6. resulting file attributes */ 5236 argop[6].argop = OP_GETATTR; 5237 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5238 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5239 5240 doqueue = 1; 5241 t = gethrtime(); 5242 5243 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5244 5245 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5246 /* 5247 * For WRONGSEC of a non-dotdot case, send secinfo directly 5248 * from this thread, do not go thru the recovery thread since 5249 * we need the nm information. 5250 * 5251 * Not doing dotdot case because there is no specification 5252 * for (PUTFH, SECINFO "..") yet. 5253 */ 5254 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5255 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5256 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5257 &recov_state, FALSE); 5258 else 5259 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5260 &recov_state, TRUE); 5261 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5262 kmem_free(argop, argoplist_size); 5263 if (!e.error) 5264 goto recov_retry; 5265 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5266 VN_RELE(*vpp); 5267 *vpp = NULL; 5268 return (e.error); 5269 } 5270 5271 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5272 OP_LOOKUP, NULL) == FALSE) { 5273 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5274 &recov_state, TRUE); 5275 5276 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5277 kmem_free(argop, argoplist_size); 5278 goto recov_retry; 5279 } 5280 } 5281 5282 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5283 5284 if (e.error || res.array_len == 0) { 5285 /* 5286 * If e.error isn't set, then reply has no ops (or we couldn't 5287 * be here). The only legal way to reply without an op array 5288 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5289 * be in the reply for all other status values. 5290 * 5291 * For valid replies without an ops array, return ENOTSUP 5292 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5293 * return EIO -- don't trust status. 5294 */ 5295 if (e.error == 0) 5296 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5297 ENOTSUP : EIO; 5298 VN_RELE(*vpp); 5299 *vpp = NULL; 5300 kmem_free(argop, argoplist_size); 5301 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5302 return (e.error); 5303 } 5304 5305 if (res.status != NFS4ERR_SAME) { 5306 e.error = geterrno4(res.status); 5307 5308 /* 5309 * The NVERIFY "failed" so the directory has changed 5310 * First make sure PUTFH succeeded and NVERIFY "failed" 5311 * cleanly. 5312 */ 5313 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5314 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5315 nfs4_purge_stale_fh(e.error, dvp, cr); 5316 VN_RELE(*vpp); 5317 *vpp = NULL; 5318 goto exit; 5319 } 5320 5321 /* 5322 * We know the NVERIFY "failed" so we must: 5323 * purge the caches (access and indirectly dnlc if needed) 5324 */ 5325 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5326 5327 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5328 nfs4_purge_stale_fh(e.error, dvp, cr); 5329 VN_RELE(*vpp); 5330 *vpp = NULL; 5331 goto exit; 5332 } 5333 5334 /* 5335 * Install new cached attributes for the directory 5336 */ 5337 nfs4_attr_cache(dvp, 5338 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5339 t, cr, FALSE, NULL); 5340 5341 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5342 nfs4_purge_stale_fh(e.error, dvp, cr); 5343 VN_RELE(*vpp); 5344 *vpp = NULL; 5345 e.error = geterrno4(res.status); 5346 goto exit; 5347 } 5348 5349 /* 5350 * Now we know the directory is valid, 5351 * cache new directory access 5352 */ 5353 nfs4_access_cache(drp, 5354 args.array[3].nfs_argop4_u.opaccess.access, 5355 res.array[3].nfs_resop4_u.opaccess.access, cr); 5356 5357 /* 5358 * recheck VEXEC access 5359 */ 5360 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5361 if (cacc != NFS4_ACCESS_ALLOWED) { 5362 /* 5363 * Directory permissions might have been revoked 5364 */ 5365 if (cacc == NFS4_ACCESS_DENIED) { 5366 e.error = EACCES; 5367 VN_RELE(*vpp); 5368 *vpp = NULL; 5369 goto exit; 5370 } 5371 5372 /* 5373 * Somehow we must not have asked for enough 5374 * so try a singleton ACCESS, should never happen. 5375 */ 5376 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5377 if (e.error) { 5378 VN_RELE(*vpp); 5379 *vpp = NULL; 5380 goto exit; 5381 } 5382 } 5383 5384 e.error = geterrno4(res.status); 5385 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5386 /* 5387 * The lookup failed, probably no entry 5388 */ 5389 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5390 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5391 } else { 5392 /* 5393 * Might be some other error, so remove 5394 * the dnlc entry to make sure we start all 5395 * over again, next time. 5396 */ 5397 dnlc_remove(dvp, nm); 5398 } 5399 VN_RELE(*vpp); 5400 *vpp = NULL; 5401 goto exit; 5402 } 5403 5404 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5405 /* 5406 * The file exists but we can't get its fh for 5407 * some unknown reason. Remove it from the dnlc 5408 * and error out to be safe. 5409 */ 5410 dnlc_remove(dvp, nm); 5411 VN_RELE(*vpp); 5412 *vpp = NULL; 5413 goto exit; 5414 } 5415 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5416 if (fhp->nfs_fh4_len == 0) { 5417 /* 5418 * The file exists but a bogus fh 5419 * some unknown reason. Remove it from the dnlc 5420 * and error out to be safe. 5421 */ 5422 e.error = ENOENT; 5423 dnlc_remove(dvp, nm); 5424 VN_RELE(*vpp); 5425 *vpp = NULL; 5426 goto exit; 5427 } 5428 sfhp = sfh4_get(fhp, mi); 5429 5430 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5431 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5432 5433 /* 5434 * Make the new rnode 5435 */ 5436 if (isdotdot) { 5437 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5438 if (e.error) { 5439 sfh4_rele(&sfhp); 5440 VN_RELE(*vpp); 5441 *vpp = NULL; 5442 goto exit; 5443 } 5444 /* 5445 * XXX if nfs4_make_dotdot uses an existing rnode 5446 * XXX it doesn't update the attributes. 5447 * XXX for now just save them again to save an OTW 5448 */ 5449 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5450 } else { 5451 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5452 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5453 /* 5454 * If v_type == VNON, then garp was NULL because 5455 * the last op in the compound failed and makenfs4node 5456 * could not find the vnode for sfhp. It created 5457 * a new vnode, so we have nothing to purge here. 5458 */ 5459 if (nvp->v_type == VNON) { 5460 vattr_t vattr; 5461 5462 vattr.va_mask = AT_TYPE; 5463 /* 5464 * N.B. We've already called nfs4_end_fop above. 5465 */ 5466 e.error = nfs4getattr(nvp, &vattr, cr); 5467 if (e.error) { 5468 sfh4_rele(&sfhp); 5469 VN_RELE(*vpp); 5470 *vpp = NULL; 5471 VN_RELE(nvp); 5472 goto exit; 5473 } 5474 nvp->v_type = vattr.va_type; 5475 } 5476 } 5477 sfh4_rele(&sfhp); 5478 5479 nrp = VTOR4(nvp); 5480 mutex_enter(&nrp->r_statev4_lock); 5481 if (!nrp->created_v4) { 5482 mutex_exit(&nrp->r_statev4_lock); 5483 dnlc_update(dvp, nm, nvp); 5484 } else 5485 mutex_exit(&nrp->r_statev4_lock); 5486 5487 VN_RELE(*vpp); 5488 *vpp = nvp; 5489 } else { 5490 hrtime_t now; 5491 hrtime_t delta = 0; 5492 5493 e.error = 0; 5494 5495 /* 5496 * Because the NVERIFY "succeeded" we know that the 5497 * directory attributes are still valid 5498 * so update r_time_attr_inval 5499 */ 5500 now = gethrtime(); 5501 mutex_enter(&drp->r_statelock); 5502 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5503 delta = now - drp->r_time_attr_saved; 5504 if (delta < mi->mi_acdirmin) 5505 delta = mi->mi_acdirmin; 5506 else if (delta > mi->mi_acdirmax) 5507 delta = mi->mi_acdirmax; 5508 } 5509 drp->r_time_attr_inval = now + delta; 5510 mutex_exit(&drp->r_statelock); 5511 dnlc_update(dvp, nm, *vpp); 5512 5513 /* 5514 * Even though we have a valid directory attr cache 5515 * and dnlc entry, we may not have access. 5516 * This should almost always hit the cache. 5517 */ 5518 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5519 if (e.error) { 5520 VN_RELE(*vpp); 5521 *vpp = NULL; 5522 } 5523 5524 if (*vpp == DNLC_NO_VNODE) { 5525 VN_RELE(*vpp); 5526 *vpp = NULL; 5527 e.error = ENOENT; 5528 } 5529 } 5530 5531 exit: 5532 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5533 kmem_free(argop, argoplist_size); 5534 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5535 return (e.error); 5536 } 5537 5538 /* 5539 * We need to go over the wire to lookup the name, but 5540 * while we are there verify the directory has not 5541 * changed but if it has, get new attributes and check access 5542 * 5543 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5544 * NVERIFY GETATTR ACCESS 5545 * 5546 * With the results: 5547 * if the NVERIFY failed we must purge the caches, add new attributes, 5548 * and cache new access. 5549 * set a new r_time_attr_inval 5550 * add name to dnlc, possibly negative 5551 * if LOOKUP succeeded 5552 * cache new attributes 5553 */ 5554 static int 5555 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5556 { 5557 COMPOUND4args_clnt args; 5558 COMPOUND4res_clnt res; 5559 fattr4 *ver_fattr; 5560 fattr4_change dchange; 5561 int32_t *ptr; 5562 nfs4_ga_res_t *garp = NULL; 5563 int argoplist_size = 9 * sizeof (nfs_argop4); 5564 nfs_argop4 *argop; 5565 int doqueue; 5566 mntinfo4_t *mi; 5567 nfs4_recov_state_t recov_state; 5568 hrtime_t t; 5569 int isdotdot; 5570 vnode_t *nvp; 5571 nfs_fh4 *fhp; 5572 nfs4_sharedfh_t *sfhp; 5573 nfs4_access_type_t cacc; 5574 rnode4_t *nrp; 5575 rnode4_t *drp = VTOR4(dvp); 5576 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5577 5578 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5579 ASSERT(nm != NULL); 5580 ASSERT(nm[0] != '\0'); 5581 ASSERT(dvp->v_type == VDIR); 5582 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5583 ASSERT(*vpp == NULL); 5584 5585 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5586 isdotdot = 1; 5587 args.ctag = TAG_LOOKUP_PARENT; 5588 } else { 5589 /* 5590 * If dvp were a stub, it should have triggered and caused 5591 * a mount for us to get this far. 5592 */ 5593 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5594 5595 isdotdot = 0; 5596 args.ctag = TAG_LOOKUP; 5597 } 5598 5599 mi = VTOMI4(dvp); 5600 recov_state.rs_flags = 0; 5601 recov_state.rs_num_retry_despite_err = 0; 5602 5603 nvp = NULL; 5604 5605 /* Save the original mount point security information */ 5606 (void) save_mnt_secinfo(mi->mi_curr_serv); 5607 5608 recov_retry: 5609 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5610 &recov_state, NULL); 5611 if (e.error) { 5612 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5613 return (e.error); 5614 } 5615 5616 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5617 5618 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5619 args.array_len = 9; 5620 args.array = argop; 5621 5622 /* 0. putfh file */ 5623 argop[0].argop = OP_CPUTFH; 5624 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5625 5626 /* 1. savefh for the nverify */ 5627 argop[1].argop = OP_SAVEFH; 5628 5629 /* 2. lookup name */ 5630 if (isdotdot) { 5631 argop[2].argop = OP_LOOKUPP; 5632 } else { 5633 argop[2].argop = OP_CLOOKUP; 5634 argop[2].nfs_argop4_u.opclookup.cname = nm; 5635 } 5636 5637 /* 3. resulting file handle */ 5638 argop[3].argop = OP_GETFH; 5639 5640 /* 4. resulting file attributes */ 5641 argop[4].argop = OP_GETATTR; 5642 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5643 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5644 5645 /* 5. restorefh back the directory for the nverify */ 5646 argop[5].argop = OP_RESTOREFH; 5647 5648 /* 6. nverify the change info */ 5649 argop[6].argop = OP_NVERIFY; 5650 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5651 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5652 ver_fattr->attrlist4 = (char *)&dchange; 5653 ptr = (int32_t *)&dchange; 5654 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5655 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5656 5657 /* 7. getattr directory */ 5658 argop[7].argop = OP_GETATTR; 5659 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5660 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5661 5662 /* 8. access directory */ 5663 argop[8].argop = OP_ACCESS; 5664 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5665 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5666 5667 doqueue = 1; 5668 t = gethrtime(); 5669 5670 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5671 5672 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5673 /* 5674 * For WRONGSEC of a non-dotdot case, send secinfo directly 5675 * from this thread, do not go thru the recovery thread since 5676 * we need the nm information. 5677 * 5678 * Not doing dotdot case because there is no specification 5679 * for (PUTFH, SECINFO "..") yet. 5680 */ 5681 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5682 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5683 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5684 &recov_state, FALSE); 5685 else 5686 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5687 &recov_state, TRUE); 5688 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5689 kmem_free(argop, argoplist_size); 5690 if (!e.error) 5691 goto recov_retry; 5692 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5693 return (e.error); 5694 } 5695 5696 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5697 OP_LOOKUP, NULL) == FALSE) { 5698 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5699 &recov_state, TRUE); 5700 5701 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5702 kmem_free(argop, argoplist_size); 5703 goto recov_retry; 5704 } 5705 } 5706 5707 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5708 5709 if (e.error || res.array_len == 0) { 5710 /* 5711 * If e.error isn't set, then reply has no ops (or we couldn't 5712 * be here). The only legal way to reply without an op array 5713 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5714 * be in the reply for all other status values. 5715 * 5716 * For valid replies without an ops array, return ENOTSUP 5717 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5718 * return EIO -- don't trust status. 5719 */ 5720 if (e.error == 0) 5721 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5722 ENOTSUP : EIO; 5723 5724 kmem_free(argop, argoplist_size); 5725 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5726 return (e.error); 5727 } 5728 5729 e.error = geterrno4(res.status); 5730 5731 /* 5732 * The PUTFH and SAVEFH may have failed. 5733 */ 5734 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5735 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5736 nfs4_purge_stale_fh(e.error, dvp, cr); 5737 goto exit; 5738 } 5739 5740 /* 5741 * Check if the file exists, if it does delay entering 5742 * into the dnlc until after we update the directory 5743 * attributes so we don't cause it to get purged immediately. 5744 */ 5745 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5746 /* 5747 * The lookup failed, probably no entry 5748 */ 5749 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5750 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5751 goto exit; 5752 } 5753 5754 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5755 /* 5756 * The file exists but we can't get its fh for 5757 * some unknown reason. Error out to be safe. 5758 */ 5759 goto exit; 5760 } 5761 5762 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5763 if (fhp->nfs_fh4_len == 0) { 5764 /* 5765 * The file exists but a bogus fh 5766 * some unknown reason. Error out to be safe. 5767 */ 5768 e.error = EIO; 5769 goto exit; 5770 } 5771 sfhp = sfh4_get(fhp, mi); 5772 5773 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5774 sfh4_rele(&sfhp); 5775 e.error = EIO; 5776 goto exit; 5777 } 5778 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5779 5780 /* 5781 * The RESTOREFH may have failed 5782 */ 5783 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5784 sfh4_rele(&sfhp); 5785 e.error = EIO; 5786 goto exit; 5787 } 5788 5789 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5790 /* 5791 * First make sure the NVERIFY failed as we expected, 5792 * if it didn't then be conservative and error out 5793 * as we can't trust the directory. 5794 */ 5795 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5796 sfh4_rele(&sfhp); 5797 e.error = EIO; 5798 goto exit; 5799 } 5800 5801 /* 5802 * We know the NVERIFY "failed" so the directory has changed, 5803 * so we must: 5804 * purge the caches (access and indirectly dnlc if needed) 5805 */ 5806 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5807 5808 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5809 sfh4_rele(&sfhp); 5810 goto exit; 5811 } 5812 nfs4_attr_cache(dvp, 5813 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5814 t, cr, FALSE, NULL); 5815 5816 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5817 nfs4_purge_stale_fh(e.error, dvp, cr); 5818 sfh4_rele(&sfhp); 5819 e.error = geterrno4(res.status); 5820 goto exit; 5821 } 5822 5823 /* 5824 * Now we know the directory is valid, 5825 * cache new directory access 5826 */ 5827 nfs4_access_cache(drp, 5828 args.array[8].nfs_argop4_u.opaccess.access, 5829 res.array[8].nfs_resop4_u.opaccess.access, cr); 5830 5831 /* 5832 * recheck VEXEC access 5833 */ 5834 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5835 if (cacc != NFS4_ACCESS_ALLOWED) { 5836 /* 5837 * Directory permissions might have been revoked 5838 */ 5839 if (cacc == NFS4_ACCESS_DENIED) { 5840 sfh4_rele(&sfhp); 5841 e.error = EACCES; 5842 goto exit; 5843 } 5844 5845 /* 5846 * Somehow we must not have asked for enough 5847 * so try a singleton ACCESS should never happen 5848 */ 5849 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5850 if (e.error) { 5851 sfh4_rele(&sfhp); 5852 goto exit; 5853 } 5854 } 5855 5856 e.error = geterrno4(res.status); 5857 } else { 5858 hrtime_t now; 5859 hrtime_t delta = 0; 5860 5861 e.error = 0; 5862 5863 /* 5864 * Because the NVERIFY "succeeded" we know that the 5865 * directory attributes are still valid 5866 * so update r_time_attr_inval 5867 */ 5868 now = gethrtime(); 5869 mutex_enter(&drp->r_statelock); 5870 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5871 delta = now - drp->r_time_attr_saved; 5872 if (delta < mi->mi_acdirmin) 5873 delta = mi->mi_acdirmin; 5874 else if (delta > mi->mi_acdirmax) 5875 delta = mi->mi_acdirmax; 5876 } 5877 drp->r_time_attr_inval = now + delta; 5878 mutex_exit(&drp->r_statelock); 5879 5880 /* 5881 * Even though we have a valid directory attr cache, 5882 * we may not have access. 5883 * This should almost always hit the cache. 5884 */ 5885 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5886 if (e.error) { 5887 sfh4_rele(&sfhp); 5888 goto exit; 5889 } 5890 } 5891 5892 /* 5893 * Now we have successfully completed the lookup, if the 5894 * directory has changed we now have the valid attributes. 5895 * We also know we have directory access. 5896 * Create the new rnode and insert it in the dnlc. 5897 */ 5898 if (isdotdot) { 5899 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5900 if (e.error) { 5901 sfh4_rele(&sfhp); 5902 goto exit; 5903 } 5904 /* 5905 * XXX if nfs4_make_dotdot uses an existing rnode 5906 * XXX it doesn't update the attributes. 5907 * XXX for now just save them again to save an OTW 5908 */ 5909 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5910 } else { 5911 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5912 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5913 } 5914 sfh4_rele(&sfhp); 5915 5916 nrp = VTOR4(nvp); 5917 mutex_enter(&nrp->r_statev4_lock); 5918 if (!nrp->created_v4) { 5919 mutex_exit(&nrp->r_statev4_lock); 5920 dnlc_update(dvp, nm, nvp); 5921 } else 5922 mutex_exit(&nrp->r_statev4_lock); 5923 5924 *vpp = nvp; 5925 5926 exit: 5927 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5928 kmem_free(argop, argoplist_size); 5929 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5930 return (e.error); 5931 } 5932 5933 #ifdef DEBUG 5934 void 5935 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 5936 { 5937 uint_t i, len; 5938 zoneid_t zoneid = getzoneid(); 5939 char *s; 5940 5941 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 5942 for (i = 0; i < argcnt; i++) { 5943 nfs_argop4 *op = &argbase[i]; 5944 switch (op->argop) { 5945 case OP_CPUTFH: 5946 case OP_PUTFH: 5947 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 5948 break; 5949 case OP_PUTROOTFH: 5950 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 5951 break; 5952 case OP_CLOOKUP: 5953 s = op->nfs_argop4_u.opclookup.cname; 5954 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5955 break; 5956 case OP_LOOKUP: 5957 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 5958 &len, NULL); 5959 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5960 kmem_free(s, len); 5961 break; 5962 case OP_LOOKUPP: 5963 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 5964 break; 5965 case OP_GETFH: 5966 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 5967 break; 5968 case OP_GETATTR: 5969 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 5970 break; 5971 case OP_OPENATTR: 5972 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 5973 break; 5974 default: 5975 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 5976 op->argop); 5977 break; 5978 } 5979 } 5980 } 5981 #endif 5982 5983 /* 5984 * nfs4lookup_setup - constructs a multi-lookup compound request. 5985 * 5986 * Given the path "nm1/nm2/.../nmn", the following compound requests 5987 * may be created: 5988 * 5989 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 5990 * is faster, for now. 5991 * 5992 * l4_getattrs indicates the type of compound requested. 5993 * 5994 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 5995 * 5996 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 5997 * 5998 * total number of ops is n + 1. 5999 * 6000 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 6001 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 6002 * before the last component, and only get attributes 6003 * for the last component. Note that the second-to-last 6004 * pathname component is XATTR_RPATH, which does NOT go 6005 * over-the-wire as a lookup. 6006 * 6007 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 6008 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 6009 * 6010 * and total number of ops is n + 5. 6011 * 6012 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 6013 * attribute directory: create lookups plus an OPENATTR 6014 * replacing the last lookup. Note that the last pathname 6015 * component is XATTR_RPATH, which does NOT go over-the-wire 6016 * as a lookup. 6017 * 6018 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 6019 * Openattr; Getfh; Getattr } 6020 * 6021 * and total number of ops is n + 5. 6022 * 6023 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6024 * nodes too. 6025 * 6026 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6027 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6028 * 6029 * and total number of ops is 3*n + 1. 6030 * 6031 * All cases: returns the index in the arg array of the final LOOKUP op, or 6032 * -1 if no LOOKUPs were used. 6033 */ 6034 int 6035 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6036 { 6037 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6038 nfs_argop4 *argbase, *argop; 6039 int arglen, argcnt; 6040 int n = 1; /* number of components */ 6041 int nga = 1; /* number of Getattr's in request */ 6042 char c = '\0', *s, *p; 6043 int lookup_idx = -1; 6044 int argoplist_size; 6045 6046 /* set lookuparg response result to 0 */ 6047 lookupargp->resp->status = NFS4_OK; 6048 6049 /* skip leading "/" or "." e.g. ".//./" if there is */ 6050 for (; ; nm++) { 6051 if (*nm != '/' && *nm != '.') 6052 break; 6053 6054 /* ".." is counted as 1 component */ 6055 if (*nm == '.' && *(nm + 1) == '.') 6056 break; 6057 } 6058 6059 /* 6060 * Find n = number of components - nm must be null terminated 6061 * Skip "." components. 6062 */ 6063 if (*nm != '\0') 6064 for (n = 1, s = nm; *s != '\0'; s++) { 6065 if ((*s == '/') && (*(s + 1) != '/') && 6066 (*(s + 1) != '\0') && 6067 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6068 *(s + 2) == '\0'))) 6069 n++; 6070 } 6071 else 6072 n = 0; 6073 6074 /* 6075 * nga is number of components that need Getfh+Getattr 6076 */ 6077 switch (l4_getattrs) { 6078 case LKP4_NO_ATTRIBUTES: 6079 nga = 0; 6080 break; 6081 case LKP4_ALL_ATTRIBUTES: 6082 nga = n; 6083 /* 6084 * Always have at least 1 getfh, getattr pair 6085 */ 6086 if (nga == 0) 6087 nga++; 6088 break; 6089 case LKP4_LAST_ATTRDIR: 6090 case LKP4_LAST_NAMED_ATTR: 6091 nga = n+1; 6092 break; 6093 } 6094 6095 /* 6096 * If change to use the filehandle attr instead of getfh 6097 * the following line can be deleted. 6098 */ 6099 nga *= 2; 6100 6101 /* 6102 * calculate number of ops in request as 6103 * header + trailer + lookups + getattrs 6104 */ 6105 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6106 6107 argoplist_size = arglen * sizeof (nfs_argop4); 6108 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6109 lookupargp->argsp->array = argop; 6110 6111 argcnt = lookupargp->header_len; 6112 argop += argcnt; 6113 6114 /* 6115 * loop and create a lookup op and possibly getattr/getfh for 6116 * each component. Skip "." components. 6117 */ 6118 for (s = nm; *s != '\0'; s = p) { 6119 /* 6120 * Set up a pathname struct for each component if needed 6121 */ 6122 while (*s == '/') 6123 s++; 6124 if (*s == '\0') 6125 break; 6126 6127 for (p = s; (*p != '/') && (*p != '\0'); p++) 6128 ; 6129 c = *p; 6130 *p = '\0'; 6131 6132 if (s[0] == '.' && s[1] == '\0') { 6133 *p = c; 6134 continue; 6135 } 6136 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6137 strcmp(s, XATTR_RPATH) == 0) { 6138 /* getfh XXX may not be needed in future */ 6139 argop->argop = OP_GETFH; 6140 argop++; 6141 argcnt++; 6142 6143 /* getattr */ 6144 argop->argop = OP_GETATTR; 6145 argop->nfs_argop4_u.opgetattr.attr_request = 6146 lookupargp->ga_bits; 6147 argop->nfs_argop4_u.opgetattr.mi = 6148 lookupargp->mi; 6149 argop++; 6150 argcnt++; 6151 6152 /* openattr */ 6153 argop->argop = OP_OPENATTR; 6154 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6155 strcmp(s, XATTR_RPATH) == 0) { 6156 /* openattr */ 6157 argop->argop = OP_OPENATTR; 6158 argop++; 6159 argcnt++; 6160 6161 /* getfh XXX may not be needed in future */ 6162 argop->argop = OP_GETFH; 6163 argop++; 6164 argcnt++; 6165 6166 /* getattr */ 6167 argop->argop = OP_GETATTR; 6168 argop->nfs_argop4_u.opgetattr.attr_request = 6169 lookupargp->ga_bits; 6170 argop->nfs_argop4_u.opgetattr.mi = 6171 lookupargp->mi; 6172 argop++; 6173 argcnt++; 6174 *p = c; 6175 continue; 6176 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6177 /* lookupp */ 6178 argop->argop = OP_LOOKUPP; 6179 } else { 6180 /* lookup */ 6181 argop->argop = OP_LOOKUP; 6182 (void) str_to_utf8(s, 6183 &argop->nfs_argop4_u.oplookup.objname); 6184 } 6185 lookup_idx = argcnt; 6186 argop++; 6187 argcnt++; 6188 6189 *p = c; 6190 6191 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6192 /* getfh XXX may not be needed in future */ 6193 argop->argop = OP_GETFH; 6194 argop++; 6195 argcnt++; 6196 6197 /* getattr */ 6198 argop->argop = OP_GETATTR; 6199 argop->nfs_argop4_u.opgetattr.attr_request = 6200 lookupargp->ga_bits; 6201 argop->nfs_argop4_u.opgetattr.mi = 6202 lookupargp->mi; 6203 argop++; 6204 argcnt++; 6205 } 6206 } 6207 6208 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6209 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6210 if (needgetfh) { 6211 /* stick in a post-lookup getfh */ 6212 argop->argop = OP_GETFH; 6213 argcnt++; 6214 argop++; 6215 } 6216 /* post-lookup getattr */ 6217 argop->argop = OP_GETATTR; 6218 argop->nfs_argop4_u.opgetattr.attr_request = 6219 lookupargp->ga_bits; 6220 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6221 argcnt++; 6222 } 6223 argcnt += lookupargp->trailer_len; /* actual op count */ 6224 lookupargp->argsp->array_len = argcnt; 6225 lookupargp->arglen = arglen; 6226 6227 #ifdef DEBUG 6228 if (nfs4_client_lookup_debug) 6229 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6230 #endif 6231 6232 return (lookup_idx); 6233 } 6234 6235 static int 6236 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6237 { 6238 COMPOUND4args_clnt args; 6239 COMPOUND4res_clnt res; 6240 GETFH4res *gf_res = NULL; 6241 nfs_argop4 argop[4]; 6242 nfs_resop4 *resop = NULL; 6243 nfs4_sharedfh_t *sfhp; 6244 hrtime_t t; 6245 nfs4_error_t e; 6246 6247 rnode4_t *drp; 6248 int doqueue = 1; 6249 vnode_t *vp; 6250 int needrecov = 0; 6251 nfs4_recov_state_t recov_state; 6252 6253 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6254 6255 *avp = NULL; 6256 recov_state.rs_flags = 0; 6257 recov_state.rs_num_retry_despite_err = 0; 6258 6259 recov_retry: 6260 /* COMPOUND: putfh, openattr, getfh, getattr */ 6261 args.array_len = 4; 6262 args.array = argop; 6263 args.ctag = TAG_OPENATTR; 6264 6265 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6266 if (e.error) 6267 return (e.error); 6268 6269 drp = VTOR4(dvp); 6270 6271 /* putfh */ 6272 argop[0].argop = OP_CPUTFH; 6273 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6274 6275 /* openattr */ 6276 argop[1].argop = OP_OPENATTR; 6277 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6278 6279 /* getfh */ 6280 argop[2].argop = OP_GETFH; 6281 6282 /* getattr */ 6283 argop[3].argop = OP_GETATTR; 6284 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6285 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6286 6287 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6288 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6289 rnode4info(drp))); 6290 6291 t = gethrtime(); 6292 6293 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6294 6295 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6296 if (needrecov) { 6297 bool_t abort; 6298 6299 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6300 "nfs4openattr: initiating recovery\n")); 6301 6302 abort = nfs4_start_recovery(&e, 6303 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6304 OP_OPENATTR, NULL); 6305 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6306 if (!e.error) { 6307 e.error = geterrno4(res.status); 6308 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6309 } 6310 if (abort == FALSE) 6311 goto recov_retry; 6312 return (e.error); 6313 } 6314 6315 if (e.error) { 6316 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6317 return (e.error); 6318 } 6319 6320 if (res.status) { 6321 /* 6322 * If OTW errro is NOTSUPP, then it should be 6323 * translated to EINVAL. All Solaris file system 6324 * implementations return EINVAL to the syscall layer 6325 * when the attrdir cannot be created due to an 6326 * implementation restriction or noxattr mount option. 6327 */ 6328 if (res.status == NFS4ERR_NOTSUPP) { 6329 mutex_enter(&drp->r_statelock); 6330 if (drp->r_xattr_dir) 6331 VN_RELE(drp->r_xattr_dir); 6332 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6333 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6334 mutex_exit(&drp->r_statelock); 6335 6336 e.error = EINVAL; 6337 } else { 6338 e.error = geterrno4(res.status); 6339 } 6340 6341 if (e.error) { 6342 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6343 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6344 needrecov); 6345 return (e.error); 6346 } 6347 } 6348 6349 resop = &res.array[0]; /* putfh res */ 6350 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6351 6352 resop = &res.array[1]; /* openattr res */ 6353 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6354 6355 resop = &res.array[2]; /* getfh res */ 6356 gf_res = &resop->nfs_resop4_u.opgetfh; 6357 if (gf_res->object.nfs_fh4_len == 0) { 6358 *avp = NULL; 6359 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6360 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6361 return (ENOENT); 6362 } 6363 6364 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6365 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6366 dvp->v_vfsp, t, cr, dvp, 6367 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH)); 6368 sfh4_rele(&sfhp); 6369 6370 if (e.error) 6371 PURGE_ATTRCACHE4(vp); 6372 6373 mutex_enter(&vp->v_lock); 6374 vp->v_flag |= V_XATTRDIR; 6375 mutex_exit(&vp->v_lock); 6376 6377 *avp = vp; 6378 6379 mutex_enter(&drp->r_statelock); 6380 if (drp->r_xattr_dir) 6381 VN_RELE(drp->r_xattr_dir); 6382 VN_HOLD(vp); 6383 drp->r_xattr_dir = vp; 6384 6385 /* 6386 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6387 * NULL. xattrs could be created at any time, and we have no 6388 * way to update pc4_xattr_exists in the base object if/when 6389 * it happens. 6390 */ 6391 drp->r_pathconf.pc4_xattr_valid = 0; 6392 6393 mutex_exit(&drp->r_statelock); 6394 6395 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6396 6397 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6398 6399 return (0); 6400 } 6401 6402 /* ARGSUSED */ 6403 static int 6404 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6405 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct, 6406 vsecattr_t *vsecp) 6407 { 6408 int error; 6409 vnode_t *vp = NULL; 6410 rnode4_t *rp; 6411 struct vattr vattr; 6412 rnode4_t *drp; 6413 vnode_t *tempvp; 6414 enum createmode4 createmode; 6415 bool_t must_trunc = FALSE; 6416 int truncating = 0; 6417 6418 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6419 return (EPERM); 6420 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6421 return (EINVAL); 6422 } 6423 6424 /* . and .. have special meaning in the protocol, reject them. */ 6425 6426 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6427 return (EISDIR); 6428 6429 drp = VTOR4(dvp); 6430 6431 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6432 return (EINTR); 6433 6434 top: 6435 /* 6436 * We make a copy of the attributes because the caller does not 6437 * expect us to change what va points to. 6438 */ 6439 vattr = *va; 6440 6441 /* 6442 * If the pathname is "", then dvp is the root vnode of 6443 * a remote file mounted over a local directory. 6444 * All that needs to be done is access 6445 * checking and truncation. Note that we avoid doing 6446 * open w/ create because the parent directory might 6447 * be in pseudo-fs and the open would fail. 6448 */ 6449 if (*nm == '\0') { 6450 error = 0; 6451 VN_HOLD(dvp); 6452 vp = dvp; 6453 must_trunc = TRUE; 6454 } else { 6455 /* 6456 * We need to go over the wire, just to be sure whether the 6457 * file exists or not. Using the DNLC can be dangerous in 6458 * this case when making a decision regarding existence. 6459 */ 6460 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6461 } 6462 6463 if (exclusive) 6464 createmode = EXCLUSIVE4; 6465 else 6466 createmode = GUARDED4; 6467 6468 /* 6469 * error would be set if the file does not exist on the 6470 * server, so lets go create it. 6471 */ 6472 if (error) { 6473 goto create_otw; 6474 } 6475 6476 /* 6477 * File does exist on the server 6478 */ 6479 if (exclusive == EXCL) 6480 error = EEXIST; 6481 else if (vp->v_type == VDIR && (mode & VWRITE)) 6482 error = EISDIR; 6483 else { 6484 /* 6485 * If vnode is a device, create special vnode. 6486 */ 6487 if (ISVDEV(vp->v_type)) { 6488 tempvp = vp; 6489 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6490 VN_RELE(tempvp); 6491 } 6492 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) { 6493 if ((vattr.va_mask & AT_SIZE) && 6494 vp->v_type == VREG) { 6495 rp = VTOR4(vp); 6496 /* 6497 * Check here for large file handled 6498 * by LF-unaware process (as 6499 * ufs_create() does) 6500 */ 6501 if (!(flags & FOFFMAX)) { 6502 mutex_enter(&rp->r_statelock); 6503 if (rp->r_size > MAXOFF32_T) 6504 error = EOVERFLOW; 6505 mutex_exit(&rp->r_statelock); 6506 } 6507 6508 /* if error is set then we need to return */ 6509 if (error) { 6510 nfs_rw_exit(&drp->r_rwlock); 6511 VN_RELE(vp); 6512 return (error); 6513 } 6514 6515 if (must_trunc) { 6516 vattr.va_mask = AT_SIZE; 6517 error = nfs4setattr(vp, &vattr, 0, cr, 6518 NULL); 6519 } else { 6520 /* 6521 * we know we have a regular file that already 6522 * exists and we may end up truncating the file 6523 * as a result of the open_otw, so flush out 6524 * any dirty pages for this file first. 6525 */ 6526 if (nfs4_has_pages(vp) && 6527 ((rp->r_flags & R4DIRTY) || 6528 rp->r_count > 0 || 6529 rp->r_mapcnt > 0)) { 6530 error = nfs4_putpage(vp, 6531 (offset_t)0, 0, 0, cr, 6532 ct); 6533 if (error && (error == ENOSPC || 6534 error == EDQUOT)) { 6535 mutex_enter( 6536 &rp->r_statelock); 6537 if (!rp->r_error) 6538 rp->r_error = 6539 error; 6540 mutex_exit( 6541 &rp->r_statelock); 6542 } 6543 } 6544 vattr.va_mask = (AT_SIZE | 6545 AT_TYPE | AT_MODE); 6546 vattr.va_type = VREG; 6547 createmode = UNCHECKED4; 6548 truncating = 1; 6549 goto create_otw; 6550 } 6551 } 6552 } 6553 } 6554 nfs_rw_exit(&drp->r_rwlock); 6555 if (error) { 6556 VN_RELE(vp); 6557 } else { 6558 vnode_t *tvp; 6559 rnode4_t *trp; 6560 /* 6561 * existing file got truncated, notify. 6562 */ 6563 tvp = vp; 6564 if (vp->v_type == VREG) { 6565 trp = VTOR4(vp); 6566 if (IS_SHADOW(vp, trp)) 6567 tvp = RTOV4(trp); 6568 } 6569 vnevent_create(tvp, ct); 6570 *vpp = vp; 6571 } 6572 return (error); 6573 6574 create_otw: 6575 dnlc_remove(dvp, nm); 6576 6577 ASSERT(vattr.va_mask & AT_TYPE); 6578 6579 /* 6580 * If not a regular file let nfs4mknod() handle it. 6581 */ 6582 if (vattr.va_type != VREG) { 6583 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6584 nfs_rw_exit(&drp->r_rwlock); 6585 return (error); 6586 } 6587 6588 /* 6589 * It _is_ a regular file. 6590 */ 6591 ASSERT(vattr.va_mask & AT_MODE); 6592 if (MANDMODE(vattr.va_mode)) { 6593 nfs_rw_exit(&drp->r_rwlock); 6594 return (EACCES); 6595 } 6596 6597 /* 6598 * If this happens to be a mknod of a regular file, then flags will 6599 * have neither FREAD or FWRITE. However, we must set at least one 6600 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6601 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6602 * set (based on openmode specified by app). 6603 */ 6604 if ((flags & (FREAD|FWRITE)) == 0) 6605 flags |= (FREAD|FWRITE); 6606 6607 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6608 6609 if (vp != NULL) { 6610 /* if create was successful, throw away the file's pages */ 6611 if (!error && (vattr.va_mask & AT_SIZE)) 6612 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6613 cr); 6614 /* release the lookup hold */ 6615 VN_RELE(vp); 6616 vp = NULL; 6617 } 6618 6619 /* 6620 * validate that we opened a regular file. This handles a misbehaving 6621 * server that returns an incorrect FH. 6622 */ 6623 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6624 error = EISDIR; 6625 VN_RELE(*vpp); 6626 } 6627 6628 /* 6629 * If this is not an exclusive create, then the CREATE 6630 * request will be made with the GUARDED mode set. This 6631 * means that the server will return EEXIST if the file 6632 * exists. The file could exist because of a retransmitted 6633 * request. In this case, we recover by starting over and 6634 * checking to see whether the file exists. This second 6635 * time through it should and a CREATE request will not be 6636 * sent. 6637 * 6638 * This handles the problem of a dangling CREATE request 6639 * which contains attributes which indicate that the file 6640 * should be truncated. This retransmitted request could 6641 * possibly truncate valid data in the file if not caught 6642 * by the duplicate request mechanism on the server or if 6643 * not caught by other means. The scenario is: 6644 * 6645 * Client transmits CREATE request with size = 0 6646 * Client times out, retransmits request. 6647 * Response to the first request arrives from the server 6648 * and the client proceeds on. 6649 * Client writes data to the file. 6650 * The server now processes retransmitted CREATE request 6651 * and truncates file. 6652 * 6653 * The use of the GUARDED CREATE request prevents this from 6654 * happening because the retransmitted CREATE would fail 6655 * with EEXIST and would not truncate the file. 6656 */ 6657 if (error == EEXIST && exclusive == NONEXCL) { 6658 #ifdef DEBUG 6659 nfs4_create_misses++; 6660 #endif 6661 goto top; 6662 } 6663 nfs_rw_exit(&drp->r_rwlock); 6664 if (truncating && !error && *vpp) { 6665 vnode_t *tvp; 6666 rnode4_t *trp; 6667 /* 6668 * existing file got truncated, notify. 6669 */ 6670 tvp = *vpp; 6671 trp = VTOR4(tvp); 6672 if (IS_SHADOW(tvp, trp)) 6673 tvp = RTOV4(trp); 6674 vnevent_create(tvp, ct); 6675 } 6676 return (error); 6677 } 6678 6679 /* 6680 * Create compound (for mkdir, mknod, symlink): 6681 * { Putfh <dfh>; Create; Getfh; Getattr } 6682 * It's okay if setattr failed to set gid - this is not considered 6683 * an error, but purge attrs in that case. 6684 */ 6685 static int 6686 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6687 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6688 { 6689 int need_end_op = FALSE; 6690 COMPOUND4args_clnt args; 6691 COMPOUND4res_clnt res, *resp = NULL; 6692 nfs_argop4 *argop; 6693 nfs_resop4 *resop; 6694 int doqueue; 6695 mntinfo4_t *mi; 6696 rnode4_t *drp = VTOR4(dvp); 6697 change_info4 *cinfo; 6698 GETFH4res *gf_res; 6699 struct vattr vattr; 6700 vnode_t *vp; 6701 fattr4 *crattr; 6702 bool_t needrecov = FALSE; 6703 nfs4_recov_state_t recov_state; 6704 nfs4_sharedfh_t *sfhp = NULL; 6705 hrtime_t t; 6706 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6707 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6708 dirattr_info_t dinfo, *dinfop; 6709 servinfo4_t *svp; 6710 bitmap4 supp_attrs; 6711 6712 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6713 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6714 6715 mi = VTOMI4(dvp); 6716 6717 /* 6718 * Make sure we properly deal with setting the right gid 6719 * on a new directory to reflect the parent's setgid bit 6720 */ 6721 setgid_flag = 0; 6722 if (type == NF4DIR) { 6723 struct vattr dva; 6724 6725 va->va_mode &= ~VSGID; 6726 dva.va_mask = AT_MODE | AT_GID; 6727 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) { 6728 6729 /* 6730 * If the parent's directory has the setgid bit set 6731 * _and_ the client was able to get a valid mapping 6732 * for the parent dir's owner_group, we want to 6733 * append NVERIFY(owner_group == dva.va_gid) and 6734 * SETTATTR to the CREATE compound. 6735 */ 6736 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6737 setgid_flag = 1; 6738 va->va_mode |= VSGID; 6739 if (dva.va_gid != GID_NOBODY) { 6740 va->va_mask |= AT_GID; 6741 va->va_gid = dva.va_gid; 6742 } 6743 } 6744 } 6745 } 6746 6747 /* 6748 * Create ops: 6749 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6750 * 5:restorefh(dir) 6:getattr(dir) 6751 * 6752 * if (setgid) 6753 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6754 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6755 * 8:nverify 9:setattr 6756 */ 6757 if (setgid_flag) { 6758 numops = 10; 6759 idx_create = 1; 6760 idx_fattr = 3; 6761 } else { 6762 numops = 7; 6763 idx_create = 2; 6764 idx_fattr = 4; 6765 } 6766 6767 ASSERT(nfs_zone() == mi->mi_zone); 6768 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6769 return (EINTR); 6770 } 6771 recov_state.rs_flags = 0; 6772 recov_state.rs_num_retry_despite_err = 0; 6773 6774 argoplist_size = numops * sizeof (nfs_argop4); 6775 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6776 6777 recov_retry: 6778 if (type == NF4LNK) 6779 args.ctag = TAG_SYMLINK; 6780 else if (type == NF4DIR) 6781 args.ctag = TAG_MKDIR; 6782 else 6783 args.ctag = TAG_MKNOD; 6784 6785 args.array_len = numops; 6786 args.array = argop; 6787 6788 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6789 nfs_rw_exit(&drp->r_rwlock); 6790 kmem_free(argop, argoplist_size); 6791 return (e.error); 6792 } 6793 need_end_op = TRUE; 6794 6795 6796 /* 0: putfh directory */ 6797 argop[0].argop = OP_CPUTFH; 6798 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6799 6800 /* 1/2: Create object */ 6801 argop[idx_create].argop = OP_CCREATE; 6802 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6803 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6804 if (type == NF4LNK) { 6805 /* 6806 * symlink, treat name as data 6807 */ 6808 ASSERT(data != NULL); 6809 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6810 (char *)data; 6811 } 6812 if (type == NF4BLK || type == NF4CHR) { 6813 ASSERT(data != NULL); 6814 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6815 *((specdata4 *)data); 6816 } 6817 6818 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6819 6820 svp = drp->r_server; 6821 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6822 supp_attrs = svp->sv_supp_attrs; 6823 nfs_rw_exit(&svp->sv_lock); 6824 6825 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6826 nfs_rw_exit(&drp->r_rwlock); 6827 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6828 e.error = EINVAL; 6829 kmem_free(argop, argoplist_size); 6830 return (e.error); 6831 } 6832 6833 /* 2/3: getfh fh of created object */ 6834 ASSERT(idx_create + 1 == idx_fattr - 1); 6835 argop[idx_create + 1].argop = OP_GETFH; 6836 6837 /* 3/4: getattr of new object */ 6838 argop[idx_fattr].argop = OP_GETATTR; 6839 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6840 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6841 6842 if (setgid_flag) { 6843 vattr_t _v; 6844 6845 argop[4].argop = OP_SAVEFH; 6846 6847 argop[5].argop = OP_CPUTFH; 6848 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6849 6850 argop[6].argop = OP_GETATTR; 6851 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6852 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6853 6854 argop[7].argop = OP_RESTOREFH; 6855 6856 /* 6857 * nverify 6858 * 6859 * XXX - Revisit the last argument to nfs4_end_op() 6860 * once 5020486 is fixed. 6861 */ 6862 _v.va_mask = AT_GID; 6863 _v.va_gid = va->va_gid; 6864 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6865 supp_attrs)) { 6866 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6867 nfs_rw_exit(&drp->r_rwlock); 6868 nfs4_fattr4_free(crattr); 6869 kmem_free(argop, argoplist_size); 6870 return (e.error); 6871 } 6872 6873 /* 6874 * setattr 6875 * 6876 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6877 * so no need for stateid or flags. Also we specify NULL 6878 * rp since we're only interested in setting owner_group 6879 * attributes. 6880 */ 6881 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 6882 &e.error, 0); 6883 6884 if (e.error) { 6885 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6886 nfs_rw_exit(&drp->r_rwlock); 6887 nfs4_fattr4_free(crattr); 6888 nfs4args_verify_free(&argop[8]); 6889 kmem_free(argop, argoplist_size); 6890 return (e.error); 6891 } 6892 } else { 6893 argop[1].argop = OP_SAVEFH; 6894 6895 argop[5].argop = OP_RESTOREFH; 6896 6897 argop[6].argop = OP_GETATTR; 6898 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6899 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6900 } 6901 6902 dnlc_remove(dvp, nm); 6903 6904 doqueue = 1; 6905 t = gethrtime(); 6906 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 6907 6908 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 6909 if (e.error) { 6910 PURGE_ATTRCACHE4(dvp); 6911 if (!needrecov) 6912 goto out; 6913 } 6914 6915 if (needrecov) { 6916 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 6917 OP_CREATE, NULL) == FALSE) { 6918 nfs4_end_op(mi, dvp, NULL, &recov_state, 6919 needrecov); 6920 need_end_op = FALSE; 6921 nfs4_fattr4_free(crattr); 6922 if (setgid_flag) { 6923 nfs4args_verify_free(&argop[8]); 6924 nfs4args_setattr_free(&argop[9]); 6925 } 6926 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6927 goto recov_retry; 6928 } 6929 } 6930 6931 resp = &res; 6932 6933 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 6934 6935 if (res.status == NFS4ERR_BADOWNER) 6936 nfs4_log_badowner(mi, OP_CREATE); 6937 6938 e.error = geterrno4(res.status); 6939 6940 /* 6941 * This check is left over from when create was implemented 6942 * using a setattr op (instead of createattrs). If the 6943 * putfh/create/getfh failed, the error was returned. If 6944 * setattr/getattr failed, we keep going. 6945 * 6946 * It might be better to get rid of the GETFH also, and just 6947 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 6948 * Then if any of the operations failed, we could return the 6949 * error now, and remove much of the error code below. 6950 */ 6951 if (res.array_len <= idx_fattr) { 6952 /* 6953 * Either Putfh, Create or Getfh failed. 6954 */ 6955 PURGE_ATTRCACHE4(dvp); 6956 /* 6957 * nfs4_purge_stale_fh() may generate otw calls through 6958 * nfs4_invalidate_pages. Hence the need to call 6959 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 6960 */ 6961 nfs4_end_op(mi, dvp, NULL, &recov_state, 6962 needrecov); 6963 need_end_op = FALSE; 6964 nfs4_purge_stale_fh(e.error, dvp, cr); 6965 goto out; 6966 } 6967 } 6968 6969 resop = &res.array[idx_create]; /* create res */ 6970 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 6971 6972 resop = &res.array[idx_create + 1]; /* getfh res */ 6973 gf_res = &resop->nfs_resop4_u.opgetfh; 6974 6975 sfhp = sfh4_get(&gf_res->object, mi); 6976 if (e.error) { 6977 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 6978 fn_get(VTOSV(dvp)->sv_name, nm)); 6979 if (vp->v_type == VNON) { 6980 vattr.va_mask = AT_TYPE; 6981 /* 6982 * Need to call nfs4_end_op before nfs4getattr to avoid 6983 * potential nfs4_start_op deadlock. See RFE 4777612. 6984 */ 6985 nfs4_end_op(mi, dvp, NULL, &recov_state, 6986 needrecov); 6987 need_end_op = FALSE; 6988 e.error = nfs4getattr(vp, &vattr, cr); 6989 if (e.error) { 6990 VN_RELE(vp); 6991 *vpp = NULL; 6992 goto out; 6993 } 6994 vp->v_type = vattr.va_type; 6995 } 6996 e.error = 0; 6997 } else { 6998 *vpp = vp = makenfs4node(sfhp, 6999 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 7000 dvp->v_vfsp, t, cr, 7001 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 7002 } 7003 7004 /* 7005 * If compound succeeded, then update dir attrs 7006 */ 7007 if (res.status == NFS4_OK) { 7008 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 7009 dinfo.di_cred = cr; 7010 dinfo.di_time_call = t; 7011 dinfop = &dinfo; 7012 } else 7013 dinfop = NULL; 7014 7015 /* Update directory cache attribute, readdir and dnlc caches */ 7016 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 7017 7018 out: 7019 if (sfhp != NULL) 7020 sfh4_rele(&sfhp); 7021 nfs_rw_exit(&drp->r_rwlock); 7022 nfs4_fattr4_free(crattr); 7023 if (setgid_flag) { 7024 nfs4args_verify_free(&argop[8]); 7025 nfs4args_setattr_free(&argop[9]); 7026 } 7027 if (resp) 7028 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7029 if (need_end_op) 7030 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7031 7032 kmem_free(argop, argoplist_size); 7033 return (e.error); 7034 } 7035 7036 /* ARGSUSED */ 7037 static int 7038 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7039 int mode, vnode_t **vpp, cred_t *cr) 7040 { 7041 int error; 7042 vnode_t *vp; 7043 nfs_ftype4 type; 7044 specdata4 spec, *specp = NULL; 7045 7046 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7047 7048 switch (va->va_type) { 7049 case VCHR: 7050 case VBLK: 7051 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7052 spec.specdata1 = getmajor(va->va_rdev); 7053 spec.specdata2 = getminor(va->va_rdev); 7054 specp = &spec; 7055 break; 7056 7057 case VFIFO: 7058 type = NF4FIFO; 7059 break; 7060 case VSOCK: 7061 type = NF4SOCK; 7062 break; 7063 7064 default: 7065 return (EINVAL); 7066 } 7067 7068 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7069 if (error) { 7070 return (error); 7071 } 7072 7073 /* 7074 * This might not be needed any more; special case to deal 7075 * with problematic v2/v3 servers. Since create was unable 7076 * to set group correctly, not sure what hope setattr has. 7077 */ 7078 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7079 va->va_mask = AT_GID; 7080 (void) nfs4setattr(vp, va, 0, cr, NULL); 7081 } 7082 7083 /* 7084 * If vnode is a device create special vnode 7085 */ 7086 if (ISVDEV(vp->v_type)) { 7087 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7088 VN_RELE(vp); 7089 } else { 7090 *vpp = vp; 7091 } 7092 return (error); 7093 } 7094 7095 /* 7096 * Remove requires that the current fh be the target directory. 7097 * After the operation, the current fh is unchanged. 7098 * The compound op structure is: 7099 * PUTFH(targetdir), REMOVE 7100 * 7101 * Weirdness: if the vnode to be removed is open 7102 * we rename it instead of removing it and nfs_inactive 7103 * will remove the new name. 7104 */ 7105 /* ARGSUSED */ 7106 static int 7107 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags) 7108 { 7109 COMPOUND4args_clnt args; 7110 COMPOUND4res_clnt res, *resp = NULL; 7111 REMOVE4res *rm_res; 7112 nfs_argop4 argop[3]; 7113 nfs_resop4 *resop; 7114 vnode_t *vp; 7115 char *tmpname; 7116 int doqueue; 7117 mntinfo4_t *mi; 7118 rnode4_t *rp; 7119 rnode4_t *drp; 7120 int needrecov = 0; 7121 nfs4_recov_state_t recov_state; 7122 int isopen; 7123 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7124 dirattr_info_t dinfo; 7125 7126 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7127 return (EPERM); 7128 drp = VTOR4(dvp); 7129 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7130 return (EINTR); 7131 7132 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7133 if (e.error) { 7134 nfs_rw_exit(&drp->r_rwlock); 7135 return (e.error); 7136 } 7137 7138 if (vp->v_type == VDIR) { 7139 VN_RELE(vp); 7140 nfs_rw_exit(&drp->r_rwlock); 7141 return (EISDIR); 7142 } 7143 7144 /* 7145 * First just remove the entry from the name cache, as it 7146 * is most likely the only entry for this vp. 7147 */ 7148 dnlc_remove(dvp, nm); 7149 7150 rp = VTOR4(vp); 7151 7152 /* 7153 * For regular file types, check to see if the file is open by looking 7154 * at the open streams. 7155 * For all other types, check the reference count on the vnode. Since 7156 * they are not opened OTW they never have an open stream. 7157 * 7158 * If the file is open, rename it to .nfsXXXX. 7159 */ 7160 if (vp->v_type != VREG) { 7161 /* 7162 * If the file has a v_count > 1 then there may be more than one 7163 * entry in the name cache due multiple links or an open file, 7164 * but we don't have the real reference count so flush all 7165 * possible entries. 7166 */ 7167 if (vp->v_count > 1) 7168 dnlc_purge_vp(vp); 7169 7170 /* 7171 * Now we have the real reference count. 7172 */ 7173 isopen = vp->v_count > 1; 7174 } else { 7175 mutex_enter(&rp->r_os_lock); 7176 isopen = list_head(&rp->r_open_streams) != NULL; 7177 mutex_exit(&rp->r_os_lock); 7178 } 7179 7180 mutex_enter(&rp->r_statelock); 7181 if (isopen && 7182 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7183 mutex_exit(&rp->r_statelock); 7184 tmpname = newname(); 7185 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct); 7186 if (e.error) 7187 kmem_free(tmpname, MAXNAMELEN); 7188 else { 7189 mutex_enter(&rp->r_statelock); 7190 if (rp->r_unldvp == NULL) { 7191 VN_HOLD(dvp); 7192 rp->r_unldvp = dvp; 7193 if (rp->r_unlcred != NULL) 7194 crfree(rp->r_unlcred); 7195 crhold(cr); 7196 rp->r_unlcred = cr; 7197 rp->r_unlname = tmpname; 7198 } else { 7199 kmem_free(rp->r_unlname, MAXNAMELEN); 7200 rp->r_unlname = tmpname; 7201 } 7202 mutex_exit(&rp->r_statelock); 7203 } 7204 VN_RELE(vp); 7205 nfs_rw_exit(&drp->r_rwlock); 7206 return (e.error); 7207 } 7208 /* 7209 * Actually remove the file/dir 7210 */ 7211 mutex_exit(&rp->r_statelock); 7212 7213 /* 7214 * We need to flush any dirty pages which happen to 7215 * be hanging around before removing the file. 7216 * This shouldn't happen very often since in NFSv4 7217 * we should be close to open consistent. 7218 */ 7219 if (nfs4_has_pages(vp) && 7220 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7221 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct); 7222 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7223 mutex_enter(&rp->r_statelock); 7224 if (!rp->r_error) 7225 rp->r_error = e.error; 7226 mutex_exit(&rp->r_statelock); 7227 } 7228 } 7229 7230 mi = VTOMI4(dvp); 7231 7232 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7233 recov_state.rs_flags = 0; 7234 recov_state.rs_num_retry_despite_err = 0; 7235 7236 recov_retry: 7237 /* 7238 * Remove ops: putfh dir; remove 7239 */ 7240 args.ctag = TAG_REMOVE; 7241 args.array_len = 3; 7242 args.array = argop; 7243 7244 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7245 if (e.error) { 7246 nfs_rw_exit(&drp->r_rwlock); 7247 VN_RELE(vp); 7248 return (e.error); 7249 } 7250 7251 /* putfh directory */ 7252 argop[0].argop = OP_CPUTFH; 7253 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7254 7255 /* remove */ 7256 argop[1].argop = OP_CREMOVE; 7257 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7258 7259 /* getattr dir */ 7260 argop[2].argop = OP_GETATTR; 7261 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7262 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7263 7264 doqueue = 1; 7265 dinfo.di_time_call = gethrtime(); 7266 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7267 7268 PURGE_ATTRCACHE4(vp); 7269 7270 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7271 if (e.error) 7272 PURGE_ATTRCACHE4(dvp); 7273 7274 if (needrecov) { 7275 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7276 NULL, NULL, NULL, OP_REMOVE, NULL) == FALSE) { 7277 if (!e.error) 7278 (void) xdr_free(xdr_COMPOUND4res_clnt, 7279 (caddr_t)&res); 7280 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7281 needrecov); 7282 goto recov_retry; 7283 } 7284 } 7285 7286 /* 7287 * Matching nfs4_end_op() for start_op() above. 7288 * There is a path in the code below which calls 7289 * nfs4_purge_stale_fh(), which may generate otw calls through 7290 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7291 * here to avoid nfs4_start_op() deadlock. 7292 */ 7293 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7294 7295 if (!e.error) { 7296 resp = &res; 7297 7298 if (res.status) { 7299 e.error = geterrno4(res.status); 7300 PURGE_ATTRCACHE4(dvp); 7301 nfs4_purge_stale_fh(e.error, dvp, cr); 7302 } else { 7303 resop = &res.array[1]; /* remove res */ 7304 rm_res = &resop->nfs_resop4_u.opremove; 7305 7306 dinfo.di_garp = 7307 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7308 dinfo.di_cred = cr; 7309 7310 /* Update directory attr, readdir and dnlc caches */ 7311 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7312 &dinfo); 7313 } 7314 } 7315 nfs_rw_exit(&drp->r_rwlock); 7316 if (resp) 7317 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7318 7319 if (e.error == 0) { 7320 vnode_t *tvp; 7321 rnode4_t *trp; 7322 trp = VTOR4(vp); 7323 tvp = vp; 7324 if (IS_SHADOW(vp, trp)) 7325 tvp = RTOV4(trp); 7326 vnevent_remove(tvp, dvp, nm, ct); 7327 } 7328 VN_RELE(vp); 7329 return (e.error); 7330 } 7331 7332 /* 7333 * Link requires that the current fh be the target directory and the 7334 * saved fh be the source fh. After the operation, the current fh is unchanged. 7335 * Thus the compound op structure is: 7336 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7337 * GETATTR(file) 7338 */ 7339 /* ARGSUSED */ 7340 static int 7341 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr, 7342 caller_context_t *ct, int flags) 7343 { 7344 COMPOUND4args_clnt args; 7345 COMPOUND4res_clnt res, *resp = NULL; 7346 LINK4res *ln_res; 7347 int argoplist_size = 7 * sizeof (nfs_argop4); 7348 nfs_argop4 *argop; 7349 nfs_resop4 *resop; 7350 vnode_t *realvp, *nvp; 7351 int doqueue; 7352 mntinfo4_t *mi; 7353 rnode4_t *tdrp; 7354 bool_t needrecov = FALSE; 7355 nfs4_recov_state_t recov_state; 7356 hrtime_t t; 7357 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7358 dirattr_info_t dinfo; 7359 7360 ASSERT(*tnm != '\0'); 7361 ASSERT(tdvp->v_type == VDIR); 7362 ASSERT(nfs4_consistent_type(tdvp)); 7363 ASSERT(nfs4_consistent_type(svp)); 7364 7365 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7366 return (EPERM); 7367 if (VOP_REALVP(svp, &realvp, ct) == 0) { 7368 svp = realvp; 7369 ASSERT(nfs4_consistent_type(svp)); 7370 } 7371 7372 tdrp = VTOR4(tdvp); 7373 mi = VTOMI4(svp); 7374 7375 if (!(mi->mi_flags & MI4_LINK)) { 7376 return (EOPNOTSUPP); 7377 } 7378 recov_state.rs_flags = 0; 7379 recov_state.rs_num_retry_despite_err = 0; 7380 7381 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7382 return (EINTR); 7383 7384 recov_retry: 7385 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7386 7387 args.ctag = TAG_LINK; 7388 7389 /* 7390 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7391 * restorefh; getattr(fl) 7392 */ 7393 args.array_len = 7; 7394 args.array = argop; 7395 7396 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7397 if (e.error) { 7398 kmem_free(argop, argoplist_size); 7399 nfs_rw_exit(&tdrp->r_rwlock); 7400 return (e.error); 7401 } 7402 7403 /* 0. putfh file */ 7404 argop[0].argop = OP_CPUTFH; 7405 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7406 7407 /* 1. save current fh to free up the space for the dir */ 7408 argop[1].argop = OP_SAVEFH; 7409 7410 /* 2. putfh targetdir */ 7411 argop[2].argop = OP_CPUTFH; 7412 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7413 7414 /* 3. link: current_fh is targetdir, saved_fh is source */ 7415 argop[3].argop = OP_CLINK; 7416 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7417 7418 /* 4. Get attributes of dir */ 7419 argop[4].argop = OP_GETATTR; 7420 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7421 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7422 7423 /* 5. If link was successful, restore current vp to file */ 7424 argop[5].argop = OP_RESTOREFH; 7425 7426 /* 6. Get attributes of linked object */ 7427 argop[6].argop = OP_GETATTR; 7428 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7429 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7430 7431 dnlc_remove(tdvp, tnm); 7432 7433 doqueue = 1; 7434 t = gethrtime(); 7435 7436 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7437 7438 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7439 if (e.error != 0 && !needrecov) { 7440 PURGE_ATTRCACHE4(tdvp); 7441 PURGE_ATTRCACHE4(svp); 7442 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7443 goto out; 7444 } 7445 7446 if (needrecov) { 7447 bool_t abort; 7448 7449 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7450 NULL, NULL, OP_LINK, NULL); 7451 if (abort == FALSE) { 7452 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7453 needrecov); 7454 kmem_free(argop, argoplist_size); 7455 if (!e.error) 7456 (void) xdr_free(xdr_COMPOUND4res_clnt, 7457 (caddr_t)&res); 7458 goto recov_retry; 7459 } else { 7460 if (e.error != 0) { 7461 PURGE_ATTRCACHE4(tdvp); 7462 PURGE_ATTRCACHE4(svp); 7463 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7464 &recov_state, needrecov); 7465 goto out; 7466 } 7467 /* fall through for res.status case */ 7468 } 7469 } 7470 7471 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7472 7473 resp = &res; 7474 if (res.status) { 7475 /* If link succeeded, then don't return error */ 7476 e.error = geterrno4(res.status); 7477 if (res.array_len <= 4) { 7478 /* 7479 * Either Putfh, Savefh, Putfh dir, or Link failed 7480 */ 7481 PURGE_ATTRCACHE4(svp); 7482 PURGE_ATTRCACHE4(tdvp); 7483 if (e.error == EOPNOTSUPP) { 7484 mutex_enter(&mi->mi_lock); 7485 mi->mi_flags &= ~MI4_LINK; 7486 mutex_exit(&mi->mi_lock); 7487 } 7488 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7489 /* XXX-LP */ 7490 if (e.error == EISDIR && crgetuid(cr) != 0) 7491 e.error = EPERM; 7492 goto out; 7493 } 7494 } 7495 7496 /* either no error or one of the postop getattr failed */ 7497 7498 /* 7499 * XXX - if LINK succeeded, but no attrs were returned for link 7500 * file, purge its cache. 7501 * 7502 * XXX Perform a simplified version of wcc checking. Instead of 7503 * have another getattr to get pre-op, just purge cache if 7504 * any of the ops prior to and including the getattr failed. 7505 * If the getattr succeeded then update the attrcache accordingly. 7506 */ 7507 7508 /* 7509 * update cache with link file postattrs. 7510 * Note: at this point resop points to link res. 7511 */ 7512 resop = &res.array[3]; /* link res */ 7513 ln_res = &resop->nfs_resop4_u.oplink; 7514 if (res.status == NFS4_OK) 7515 e.error = nfs4_update_attrcache(res.status, 7516 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7517 t, svp, cr); 7518 7519 /* 7520 * Call makenfs4node to create the new shadow vp for tnm. 7521 * We pass NULL attrs because we just cached attrs for 7522 * the src object. All we're trying to accomplish is to 7523 * to create the new shadow vnode. 7524 */ 7525 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7526 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm)); 7527 7528 /* Update target cache attribute, readdir and dnlc caches */ 7529 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7530 dinfo.di_time_call = t; 7531 dinfo.di_cred = cr; 7532 7533 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7534 ASSERT(nfs4_consistent_type(tdvp)); 7535 ASSERT(nfs4_consistent_type(svp)); 7536 ASSERT(nfs4_consistent_type(nvp)); 7537 VN_RELE(nvp); 7538 7539 if (!e.error) { 7540 vnode_t *tvp; 7541 rnode4_t *trp; 7542 /* 7543 * Notify the source file of this link operation. 7544 */ 7545 trp = VTOR4(svp); 7546 tvp = svp; 7547 if (IS_SHADOW(svp, trp)) 7548 tvp = RTOV4(trp); 7549 vnevent_link(tvp, ct); 7550 } 7551 out: 7552 kmem_free(argop, argoplist_size); 7553 if (resp) 7554 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7555 7556 nfs_rw_exit(&tdrp->r_rwlock); 7557 7558 return (e.error); 7559 } 7560 7561 /* ARGSUSED */ 7562 static int 7563 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7564 caller_context_t *ct, int flags) 7565 { 7566 vnode_t *realvp; 7567 7568 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7569 return (EPERM); 7570 if (VOP_REALVP(ndvp, &realvp, ct) == 0) 7571 ndvp = realvp; 7572 7573 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct)); 7574 } 7575 7576 /* 7577 * nfs4rename does the real work of renaming in NFS Version 4. 7578 * 7579 * A file handle is considered volatile for renaming purposes if either 7580 * of the volatile bits are turned on. However, the compound may differ 7581 * based on the likelihood of the filehandle to change during rename. 7582 */ 7583 static int 7584 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7585 caller_context_t *ct) 7586 { 7587 int error; 7588 mntinfo4_t *mi; 7589 vnode_t *nvp = NULL; 7590 vnode_t *ovp = NULL; 7591 char *tmpname = NULL; 7592 rnode4_t *rp; 7593 rnode4_t *odrp; 7594 rnode4_t *ndrp; 7595 int did_link = 0; 7596 int do_link = 1; 7597 nfsstat4 stat = NFS4_OK; 7598 7599 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7600 ASSERT(nfs4_consistent_type(odvp)); 7601 ASSERT(nfs4_consistent_type(ndvp)); 7602 7603 if (onm[0] == '.' && (onm[1] == '\0' || 7604 (onm[1] == '.' && onm[2] == '\0'))) 7605 return (EINVAL); 7606 7607 if (nnm[0] == '.' && (nnm[1] == '\0' || 7608 (nnm[1] == '.' && nnm[2] == '\0'))) 7609 return (EINVAL); 7610 7611 odrp = VTOR4(odvp); 7612 ndrp = VTOR4(ndvp); 7613 if ((intptr_t)odrp < (intptr_t)ndrp) { 7614 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7615 return (EINTR); 7616 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7617 nfs_rw_exit(&odrp->r_rwlock); 7618 return (EINTR); 7619 } 7620 } else { 7621 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7622 return (EINTR); 7623 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7624 nfs_rw_exit(&ndrp->r_rwlock); 7625 return (EINTR); 7626 } 7627 } 7628 7629 /* 7630 * Lookup the target file. If it exists, it needs to be 7631 * checked to see whether it is a mount point and whether 7632 * it is active (open). 7633 */ 7634 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7635 if (!error) { 7636 int isactive; 7637 7638 ASSERT(nfs4_consistent_type(nvp)); 7639 /* 7640 * If this file has been mounted on, then just 7641 * return busy because renaming to it would remove 7642 * the mounted file system from the name space. 7643 */ 7644 if (vn_ismntpt(nvp)) { 7645 VN_RELE(nvp); 7646 nfs_rw_exit(&odrp->r_rwlock); 7647 nfs_rw_exit(&ndrp->r_rwlock); 7648 return (EBUSY); 7649 } 7650 7651 /* 7652 * First just remove the entry from the name cache, as it 7653 * is most likely the only entry for this vp. 7654 */ 7655 dnlc_remove(ndvp, nnm); 7656 7657 rp = VTOR4(nvp); 7658 7659 if (nvp->v_type != VREG) { 7660 /* 7661 * Purge the name cache of all references to this vnode 7662 * so that we can check the reference count to infer 7663 * whether it is active or not. 7664 */ 7665 if (nvp->v_count > 1) 7666 dnlc_purge_vp(nvp); 7667 7668 isactive = nvp->v_count > 1; 7669 } else { 7670 mutex_enter(&rp->r_os_lock); 7671 isactive = list_head(&rp->r_open_streams) != NULL; 7672 mutex_exit(&rp->r_os_lock); 7673 } 7674 7675 /* 7676 * If the vnode is active and is not a directory, 7677 * arrange to rename it to a 7678 * temporary file so that it will continue to be 7679 * accessible. This implements the "unlink-open-file" 7680 * semantics for the target of a rename operation. 7681 * Before doing this though, make sure that the 7682 * source and target files are not already the same. 7683 */ 7684 if (isactive && nvp->v_type != VDIR) { 7685 /* 7686 * Lookup the source name. 7687 */ 7688 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7689 7690 /* 7691 * The source name *should* already exist. 7692 */ 7693 if (error) { 7694 VN_RELE(nvp); 7695 nfs_rw_exit(&odrp->r_rwlock); 7696 nfs_rw_exit(&ndrp->r_rwlock); 7697 return (error); 7698 } 7699 7700 ASSERT(nfs4_consistent_type(ovp)); 7701 7702 /* 7703 * Compare the two vnodes. If they are the same, 7704 * just release all held vnodes and return success. 7705 */ 7706 if (VN_CMP(ovp, nvp)) { 7707 VN_RELE(ovp); 7708 VN_RELE(nvp); 7709 nfs_rw_exit(&odrp->r_rwlock); 7710 nfs_rw_exit(&ndrp->r_rwlock); 7711 return (0); 7712 } 7713 7714 /* 7715 * Can't mix and match directories and non- 7716 * directories in rename operations. We already 7717 * know that the target is not a directory. If 7718 * the source is a directory, return an error. 7719 */ 7720 if (ovp->v_type == VDIR) { 7721 VN_RELE(ovp); 7722 VN_RELE(nvp); 7723 nfs_rw_exit(&odrp->r_rwlock); 7724 nfs_rw_exit(&ndrp->r_rwlock); 7725 return (ENOTDIR); 7726 } 7727 link_call: 7728 /* 7729 * The target file exists, is not the same as 7730 * the source file, and is active. We first 7731 * try to Link it to a temporary filename to 7732 * avoid having the server removing the file 7733 * completely (which could cause data loss to 7734 * the user's POV in the event the Rename fails 7735 * -- see bug 1165874). 7736 */ 7737 /* 7738 * The do_link and did_link booleans are 7739 * introduced in the event we get NFS4ERR_FILE_OPEN 7740 * returned for the Rename. Some servers can 7741 * not Rename over an Open file, so they return 7742 * this error. The client needs to Remove the 7743 * newly created Link and do two Renames, just 7744 * as if the server didn't support LINK. 7745 */ 7746 tmpname = newname(); 7747 error = 0; 7748 7749 if (do_link) { 7750 error = nfs4_link(ndvp, nvp, tmpname, cr, 7751 NULL, 0); 7752 } 7753 if (error == EOPNOTSUPP || !do_link) { 7754 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7755 cr, NULL, 0); 7756 did_link = 0; 7757 } else { 7758 did_link = 1; 7759 } 7760 if (error) { 7761 kmem_free(tmpname, MAXNAMELEN); 7762 VN_RELE(ovp); 7763 VN_RELE(nvp); 7764 nfs_rw_exit(&odrp->r_rwlock); 7765 nfs_rw_exit(&ndrp->r_rwlock); 7766 return (error); 7767 } 7768 7769 mutex_enter(&rp->r_statelock); 7770 if (rp->r_unldvp == NULL) { 7771 VN_HOLD(ndvp); 7772 rp->r_unldvp = ndvp; 7773 if (rp->r_unlcred != NULL) 7774 crfree(rp->r_unlcred); 7775 crhold(cr); 7776 rp->r_unlcred = cr; 7777 rp->r_unlname = tmpname; 7778 } else { 7779 if (rp->r_unlname) 7780 kmem_free(rp->r_unlname, MAXNAMELEN); 7781 rp->r_unlname = tmpname; 7782 } 7783 mutex_exit(&rp->r_statelock); 7784 } 7785 7786 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7787 7788 ASSERT(nfs4_consistent_type(nvp)); 7789 } 7790 7791 if (ovp == NULL) { 7792 /* 7793 * When renaming directories to be a subdirectory of a 7794 * different parent, the dnlc entry for ".." will no 7795 * longer be valid, so it must be removed. 7796 * 7797 * We do a lookup here to determine whether we are renaming 7798 * a directory and we need to check if we are renaming 7799 * an unlinked file. This might have already been done 7800 * in previous code, so we check ovp == NULL to avoid 7801 * doing it twice. 7802 */ 7803 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7804 /* 7805 * The source name *should* already exist. 7806 */ 7807 if (error) { 7808 nfs_rw_exit(&odrp->r_rwlock); 7809 nfs_rw_exit(&ndrp->r_rwlock); 7810 if (nvp) { 7811 VN_RELE(nvp); 7812 } 7813 return (error); 7814 } 7815 ASSERT(ovp != NULL); 7816 ASSERT(nfs4_consistent_type(ovp)); 7817 } 7818 7819 /* 7820 * Is the object being renamed a dir, and if so, is 7821 * it being renamed to a child of itself? The underlying 7822 * fs should ultimately return EINVAL for this case; 7823 * however, buggy beta non-Solaris NFSv4 servers at 7824 * interop testing events have allowed this behavior, 7825 * and it caused our client to panic due to a recursive 7826 * mutex_enter in fn_move. 7827 * 7828 * The tedious locking in fn_move could be changed to 7829 * deal with this case, and the client could avoid the 7830 * panic; however, the client would just confuse itself 7831 * later and misbehave. A better way to handle the broken 7832 * server is to detect this condition and return EINVAL 7833 * without ever sending the the bogus rename to the server. 7834 * We know the rename is invalid -- just fail it now. 7835 */ 7836 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7837 VN_RELE(ovp); 7838 nfs_rw_exit(&odrp->r_rwlock); 7839 nfs_rw_exit(&ndrp->r_rwlock); 7840 if (nvp) { 7841 VN_RELE(nvp); 7842 } 7843 return (EINVAL); 7844 } 7845 7846 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7847 7848 /* 7849 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7850 * possible for the filehandle to change due to the rename. 7851 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7852 * the fh will not change because of the rename, but we still need 7853 * to update its rnode entry with the new name for 7854 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7855 * has no effect on these for now, but for future improvements, 7856 * we might want to use it too to simplify handling of files 7857 * that are open with that flag on. (XXX) 7858 */ 7859 mi = VTOMI4(odvp); 7860 if (NFS4_VOLATILE_FH(mi)) 7861 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7862 &stat); 7863 else 7864 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7865 &stat); 7866 7867 ASSERT(nfs4_consistent_type(odvp)); 7868 ASSERT(nfs4_consistent_type(ndvp)); 7869 ASSERT(nfs4_consistent_type(ovp)); 7870 7871 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7872 do_link = 0; 7873 /* 7874 * Before the 'link_call' code, we did a nfs4_lookup 7875 * that puts a VN_HOLD on nvp. After the nfs4_link 7876 * call we call VN_RELE to match that hold. We need 7877 * to place an additional VN_HOLD here since we will 7878 * be hitting that VN_RELE again. 7879 */ 7880 VN_HOLD(nvp); 7881 7882 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0); 7883 7884 /* Undo the unlinked file naming stuff we just did */ 7885 mutex_enter(&rp->r_statelock); 7886 if (rp->r_unldvp) { 7887 VN_RELE(ndvp); 7888 rp->r_unldvp = NULL; 7889 if (rp->r_unlcred != NULL) 7890 crfree(rp->r_unlcred); 7891 rp->r_unlcred = NULL; 7892 /* rp->r_unlanme points to tmpname */ 7893 if (rp->r_unlname) 7894 kmem_free(rp->r_unlname, MAXNAMELEN); 7895 rp->r_unlname = NULL; 7896 } 7897 mutex_exit(&rp->r_statelock); 7898 7899 if (nvp) { 7900 VN_RELE(nvp); 7901 } 7902 goto link_call; 7903 } 7904 7905 if (error) { 7906 VN_RELE(ovp); 7907 nfs_rw_exit(&odrp->r_rwlock); 7908 nfs_rw_exit(&ndrp->r_rwlock); 7909 if (nvp) { 7910 VN_RELE(nvp); 7911 } 7912 return (error); 7913 } 7914 7915 /* 7916 * when renaming directories to be a subdirectory of a 7917 * different parent, the dnlc entry for ".." will no 7918 * longer be valid, so it must be removed 7919 */ 7920 rp = VTOR4(ovp); 7921 if (ndvp != odvp) { 7922 if (ovp->v_type == VDIR) { 7923 dnlc_remove(ovp, ".."); 7924 if (rp->r_dir != NULL) 7925 nfs4_purge_rddir_cache(ovp); 7926 } 7927 } 7928 7929 /* 7930 * If we are renaming the unlinked file, update the 7931 * r_unldvp and r_unlname as needed. 7932 */ 7933 mutex_enter(&rp->r_statelock); 7934 if (rp->r_unldvp != NULL) { 7935 if (strcmp(rp->r_unlname, onm) == 0) { 7936 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 7937 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 7938 if (ndvp != rp->r_unldvp) { 7939 VN_RELE(rp->r_unldvp); 7940 rp->r_unldvp = ndvp; 7941 VN_HOLD(ndvp); 7942 } 7943 } 7944 } 7945 mutex_exit(&rp->r_statelock); 7946 7947 /* 7948 * Notify the rename vnevents to source vnode, and to the target 7949 * vnode if it already existed. 7950 */ 7951 if (error == 0) { 7952 vnode_t *tvp; 7953 rnode4_t *trp; 7954 /* 7955 * Notify the vnode. Each links is represented by 7956 * a different vnode, in nfsv4. 7957 */ 7958 if (nvp) { 7959 trp = VTOR4(nvp); 7960 tvp = nvp; 7961 if (IS_SHADOW(nvp, trp)) 7962 tvp = RTOV4(trp); 7963 vnevent_rename_dest(tvp, ndvp, nnm, ct); 7964 } 7965 7966 /* 7967 * if the source and destination directory are not the 7968 * same notify the destination directory. 7969 */ 7970 if (VTOR4(odvp) != VTOR4(ndvp)) { 7971 trp = VTOR4(ndvp); 7972 tvp = ndvp; 7973 if (IS_SHADOW(ndvp, trp)) 7974 tvp = RTOV4(trp); 7975 vnevent_rename_dest_dir(tvp, ct); 7976 } 7977 7978 trp = VTOR4(ovp); 7979 tvp = ovp; 7980 if (IS_SHADOW(ovp, trp)) 7981 tvp = RTOV4(trp); 7982 vnevent_rename_src(tvp, odvp, onm, ct); 7983 } 7984 7985 if (nvp) { 7986 VN_RELE(nvp); 7987 } 7988 VN_RELE(ovp); 7989 7990 nfs_rw_exit(&odrp->r_rwlock); 7991 nfs_rw_exit(&ndrp->r_rwlock); 7992 7993 return (error); 7994 } 7995 7996 /* 7997 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 7998 * when it is known that the filehandle is persistent through rename. 7999 * 8000 * Rename requires that the current fh be the target directory and the 8001 * saved fh be the source directory. After the operation, the current fh 8002 * is unchanged. 8003 * The compound op structure for persistent fh rename is: 8004 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 8005 * Rather than bother with the directory postop args, we'll simply 8006 * update that a change occurred in the cache, so no post-op getattrs. 8007 */ 8008 static int 8009 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 8010 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8011 { 8012 COMPOUND4args_clnt args; 8013 COMPOUND4res_clnt res, *resp = NULL; 8014 nfs_argop4 *argop; 8015 nfs_resop4 *resop; 8016 int doqueue, argoplist_size; 8017 mntinfo4_t *mi; 8018 rnode4_t *odrp = VTOR4(odvp); 8019 rnode4_t *ndrp = VTOR4(ndvp); 8020 RENAME4res *rn_res; 8021 bool_t needrecov; 8022 nfs4_recov_state_t recov_state; 8023 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8024 dirattr_info_t dinfo, *dinfop; 8025 8026 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8027 8028 recov_state.rs_flags = 0; 8029 recov_state.rs_num_retry_despite_err = 0; 8030 8031 /* 8032 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 8033 * 8034 * If source/target are different dirs, then append putfh(src); getattr 8035 */ 8036 args.array_len = (odvp == ndvp) ? 5 : 7; 8037 argoplist_size = args.array_len * sizeof (nfs_argop4); 8038 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8039 8040 recov_retry: 8041 *statp = NFS4_OK; 8042 8043 /* No need to Lookup the file, persistent fh */ 8044 args.ctag = TAG_RENAME; 8045 8046 mi = VTOMI4(odvp); 8047 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8048 if (e.error) { 8049 kmem_free(argop, argoplist_size); 8050 return (e.error); 8051 } 8052 8053 /* 0: putfh source directory */ 8054 argop[0].argop = OP_CPUTFH; 8055 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8056 8057 /* 1: Save source fh to free up current for target */ 8058 argop[1].argop = OP_SAVEFH; 8059 8060 /* 2: putfh targetdir */ 8061 argop[2].argop = OP_CPUTFH; 8062 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8063 8064 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8065 argop[3].argop = OP_CRENAME; 8066 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8067 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8068 8069 /* 4: getattr (targetdir) */ 8070 argop[4].argop = OP_GETATTR; 8071 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8072 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8073 8074 if (ndvp != odvp) { 8075 8076 /* 5: putfh (sourcedir) */ 8077 argop[5].argop = OP_CPUTFH; 8078 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8079 8080 /* 6: getattr (sourcedir) */ 8081 argop[6].argop = OP_GETATTR; 8082 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8083 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8084 } 8085 8086 dnlc_remove(odvp, onm); 8087 dnlc_remove(ndvp, nnm); 8088 8089 doqueue = 1; 8090 dinfo.di_time_call = gethrtime(); 8091 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8092 8093 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8094 if (e.error) { 8095 PURGE_ATTRCACHE4(odvp); 8096 PURGE_ATTRCACHE4(ndvp); 8097 } else { 8098 *statp = res.status; 8099 } 8100 8101 if (needrecov) { 8102 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8103 OP_RENAME, NULL) == FALSE) { 8104 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8105 if (!e.error) 8106 (void) xdr_free(xdr_COMPOUND4res_clnt, 8107 (caddr_t)&res); 8108 goto recov_retry; 8109 } 8110 } 8111 8112 if (!e.error) { 8113 resp = &res; 8114 /* 8115 * as long as OP_RENAME 8116 */ 8117 if (res.status != NFS4_OK && res.array_len <= 4) { 8118 e.error = geterrno4(res.status); 8119 PURGE_ATTRCACHE4(odvp); 8120 PURGE_ATTRCACHE4(ndvp); 8121 /* 8122 * System V defines rename to return EEXIST, not 8123 * ENOTEMPTY if the target directory is not empty. 8124 * Over the wire, the error is NFSERR_ENOTEMPTY 8125 * which geterrno4 maps to ENOTEMPTY. 8126 */ 8127 if (e.error == ENOTEMPTY) 8128 e.error = EEXIST; 8129 } else { 8130 8131 resop = &res.array[3]; /* rename res */ 8132 rn_res = &resop->nfs_resop4_u.oprename; 8133 8134 if (res.status == NFS4_OK) { 8135 /* 8136 * Update target attribute, readdir and dnlc 8137 * caches. 8138 */ 8139 dinfo.di_garp = 8140 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8141 dinfo.di_cred = cr; 8142 dinfop = &dinfo; 8143 } else 8144 dinfop = NULL; 8145 8146 nfs4_update_dircaches(&rn_res->target_cinfo, 8147 ndvp, NULL, NULL, dinfop); 8148 8149 /* 8150 * Update source attribute, readdir and dnlc caches 8151 * 8152 */ 8153 if (ndvp != odvp) { 8154 if (dinfop) 8155 dinfo.di_garp = 8156 &(res.array[6].nfs_resop4_u. 8157 opgetattr.ga_res); 8158 8159 nfs4_update_dircaches(&rn_res->source_cinfo, 8160 odvp, NULL, NULL, dinfop); 8161 } 8162 8163 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8164 nnm); 8165 } 8166 } 8167 8168 if (resp) 8169 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8170 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8171 kmem_free(argop, argoplist_size); 8172 8173 return (e.error); 8174 } 8175 8176 /* 8177 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8178 * it is possible for the filehandle to change due to the rename. 8179 * 8180 * The compound req in this case includes a post-rename lookup and getattr 8181 * to ensure that we have the correct fh and attributes for the object. 8182 * 8183 * Rename requires that the current fh be the target directory and the 8184 * saved fh be the source directory. After the operation, the current fh 8185 * is unchanged. 8186 * 8187 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8188 * update the filehandle for the renamed object. We also get the old 8189 * filehandle for historical reasons; this should be taken out sometime. 8190 * This results in a rather cumbersome compound... 8191 * 8192 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8193 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8194 * 8195 */ 8196 static int 8197 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8198 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8199 { 8200 COMPOUND4args_clnt args; 8201 COMPOUND4res_clnt res, *resp = NULL; 8202 int argoplist_size; 8203 nfs_argop4 *argop; 8204 nfs_resop4 *resop; 8205 int doqueue; 8206 mntinfo4_t *mi; 8207 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8208 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8209 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8210 RENAME4res *rn_res; 8211 GETFH4res *ngf_res; 8212 bool_t needrecov; 8213 nfs4_recov_state_t recov_state; 8214 hrtime_t t; 8215 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8216 dirattr_info_t dinfo, *dinfop = &dinfo; 8217 8218 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8219 8220 recov_state.rs_flags = 0; 8221 recov_state.rs_num_retry_despite_err = 0; 8222 8223 recov_retry: 8224 *statp = NFS4_OK; 8225 8226 /* 8227 * There is a window between the RPC and updating the path and 8228 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8229 * code, so that it doesn't try to use the old path during that 8230 * window. 8231 */ 8232 mutex_enter(&orp->r_statelock); 8233 while (orp->r_flags & R4RECEXPFH) { 8234 klwp_t *lwp = ttolwp(curthread); 8235 8236 if (lwp != NULL) 8237 lwp->lwp_nostop++; 8238 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8239 mutex_exit(&orp->r_statelock); 8240 if (lwp != NULL) 8241 lwp->lwp_nostop--; 8242 return (EINTR); 8243 } 8244 if (lwp != NULL) 8245 lwp->lwp_nostop--; 8246 } 8247 orp->r_flags |= R4RECEXPFH; 8248 mutex_exit(&orp->r_statelock); 8249 8250 mi = VTOMI4(odvp); 8251 8252 args.ctag = TAG_RENAME_VFH; 8253 args.array_len = (odvp == ndvp) ? 10 : 12; 8254 argoplist_size = args.array_len * sizeof (nfs_argop4); 8255 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8256 8257 /* 8258 * Rename ops: 8259 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8260 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8261 * LOOKUP(trgt), GETFH(new), GETATTR, 8262 * 8263 * if (odvp != ndvp) 8264 * add putfh(sourcedir), getattr(sourcedir) } 8265 */ 8266 args.array = argop; 8267 8268 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8269 &recov_state, NULL); 8270 if (e.error) { 8271 kmem_free(argop, argoplist_size); 8272 mutex_enter(&orp->r_statelock); 8273 orp->r_flags &= ~R4RECEXPFH; 8274 cv_broadcast(&orp->r_cv); 8275 mutex_exit(&orp->r_statelock); 8276 return (e.error); 8277 } 8278 8279 /* 0: putfh source directory */ 8280 argop[0].argop = OP_CPUTFH; 8281 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8282 8283 /* 1: Save source fh to free up current for target */ 8284 argop[1].argop = OP_SAVEFH; 8285 8286 /* 2: Lookup pre-rename fh of renamed object */ 8287 argop[2].argop = OP_CLOOKUP; 8288 argop[2].nfs_argop4_u.opclookup.cname = onm; 8289 8290 /* 3: getfh fh of renamed object (before rename) */ 8291 argop[3].argop = OP_GETFH; 8292 8293 /* 4: putfh targetdir */ 8294 argop[4].argop = OP_CPUTFH; 8295 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8296 8297 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8298 argop[5].argop = OP_CRENAME; 8299 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8300 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8301 8302 /* 6: getattr of target dir (post op attrs) */ 8303 argop[6].argop = OP_GETATTR; 8304 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8305 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8306 8307 /* 7: Lookup post-rename fh of renamed object */ 8308 argop[7].argop = OP_CLOOKUP; 8309 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8310 8311 /* 8: getfh fh of renamed object (after rename) */ 8312 argop[8].argop = OP_GETFH; 8313 8314 /* 9: getattr of renamed object */ 8315 argop[9].argop = OP_GETATTR; 8316 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8317 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8318 8319 /* 8320 * If source/target dirs are different, then get new post-op 8321 * attrs for source dir also. 8322 */ 8323 if (ndvp != odvp) { 8324 /* 10: putfh (sourcedir) */ 8325 argop[10].argop = OP_CPUTFH; 8326 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8327 8328 /* 11: getattr (sourcedir) */ 8329 argop[11].argop = OP_GETATTR; 8330 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8331 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8332 } 8333 8334 dnlc_remove(odvp, onm); 8335 dnlc_remove(ndvp, nnm); 8336 8337 doqueue = 1; 8338 t = gethrtime(); 8339 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8340 8341 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8342 if (e.error) { 8343 PURGE_ATTRCACHE4(odvp); 8344 PURGE_ATTRCACHE4(ndvp); 8345 if (!needrecov) { 8346 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8347 &recov_state, needrecov); 8348 goto out; 8349 } 8350 } else { 8351 *statp = res.status; 8352 } 8353 8354 if (needrecov) { 8355 bool_t abort; 8356 8357 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8358 OP_RENAME, NULL); 8359 if (abort == FALSE) { 8360 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8361 &recov_state, needrecov); 8362 kmem_free(argop, argoplist_size); 8363 if (!e.error) 8364 (void) xdr_free(xdr_COMPOUND4res_clnt, 8365 (caddr_t)&res); 8366 mutex_enter(&orp->r_statelock); 8367 orp->r_flags &= ~R4RECEXPFH; 8368 cv_broadcast(&orp->r_cv); 8369 mutex_exit(&orp->r_statelock); 8370 goto recov_retry; 8371 } else { 8372 if (e.error != 0) { 8373 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8374 &recov_state, needrecov); 8375 goto out; 8376 } 8377 /* fall through for res.status case */ 8378 } 8379 } 8380 8381 resp = &res; 8382 /* 8383 * If OP_RENAME (or any prev op) failed, then return an error. 8384 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8385 */ 8386 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8387 /* 8388 * Error in an op other than last Getattr 8389 */ 8390 e.error = geterrno4(res.status); 8391 PURGE_ATTRCACHE4(odvp); 8392 PURGE_ATTRCACHE4(ndvp); 8393 /* 8394 * System V defines rename to return EEXIST, not 8395 * ENOTEMPTY if the target directory is not empty. 8396 * Over the wire, the error is NFSERR_ENOTEMPTY 8397 * which geterrno4 maps to ENOTEMPTY. 8398 */ 8399 if (e.error == ENOTEMPTY) 8400 e.error = EEXIST; 8401 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8402 needrecov); 8403 goto out; 8404 } 8405 8406 /* rename results */ 8407 rn_res = &res.array[5].nfs_resop4_u.oprename; 8408 8409 if (res.status == NFS4_OK) { 8410 /* Update target attribute, readdir and dnlc caches */ 8411 dinfo.di_garp = 8412 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8413 dinfo.di_cred = cr; 8414 dinfo.di_time_call = t; 8415 } else 8416 dinfop = NULL; 8417 8418 /* Update source cache attribute, readdir and dnlc caches */ 8419 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8420 8421 /* Update source cache attribute, readdir and dnlc caches */ 8422 if (ndvp != odvp) { 8423 8424 /* 8425 * If dinfop is non-NULL, then compound succeded, so 8426 * set di_garp to attrs for source dir. dinfop is only 8427 * set to NULL when compound fails. 8428 */ 8429 if (dinfop) 8430 dinfo.di_garp = 8431 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8432 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8433 dinfop); 8434 } 8435 8436 /* 8437 * Update the rnode with the new component name and args, 8438 * and if the file handle changed, also update it with the new fh. 8439 * This is only necessary if the target object has an rnode 8440 * entry and there is no need to create one for it. 8441 */ 8442 resop = &res.array[8]; /* getfh new res */ 8443 ngf_res = &resop->nfs_resop4_u.opgetfh; 8444 8445 /* 8446 * Update the path and filehandle for the renamed object. 8447 */ 8448 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8449 8450 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8451 8452 if (res.status == NFS4_OK) { 8453 resop++; /* getattr res */ 8454 e.error = nfs4_update_attrcache(res.status, 8455 &resop->nfs_resop4_u.opgetattr.ga_res, 8456 t, ovp, cr); 8457 } 8458 8459 out: 8460 kmem_free(argop, argoplist_size); 8461 if (resp) 8462 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8463 mutex_enter(&orp->r_statelock); 8464 orp->r_flags &= ~R4RECEXPFH; 8465 cv_broadcast(&orp->r_cv); 8466 mutex_exit(&orp->r_statelock); 8467 8468 return (e.error); 8469 } 8470 8471 /* ARGSUSED */ 8472 static int 8473 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr, 8474 caller_context_t *ct, int flags, vsecattr_t *vsecp) 8475 { 8476 int error; 8477 vnode_t *vp; 8478 8479 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8480 return (EPERM); 8481 /* 8482 * As ".." has special meaning and rather than send a mkdir 8483 * over the wire to just let the server freak out, we just 8484 * short circuit it here and return EEXIST 8485 */ 8486 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8487 return (EEXIST); 8488 8489 /* 8490 * Decision to get the right gid and setgid bit of the 8491 * new directory is now made in call_nfs4_create_req. 8492 */ 8493 va->va_mask |= AT_MODE; 8494 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8495 if (error) 8496 return (error); 8497 8498 *vpp = vp; 8499 return (0); 8500 } 8501 8502 8503 /* 8504 * rmdir is using the same remove v4 op as does remove. 8505 * Remove requires that the current fh be the target directory. 8506 * After the operation, the current fh is unchanged. 8507 * The compound op structure is: 8508 * PUTFH(targetdir), REMOVE 8509 */ 8510 /*ARGSUSED4*/ 8511 static int 8512 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr, 8513 caller_context_t *ct, int flags) 8514 { 8515 int need_end_op = FALSE; 8516 COMPOUND4args_clnt args; 8517 COMPOUND4res_clnt res, *resp = NULL; 8518 REMOVE4res *rm_res; 8519 nfs_argop4 argop[3]; 8520 nfs_resop4 *resop; 8521 vnode_t *vp; 8522 int doqueue; 8523 mntinfo4_t *mi; 8524 rnode4_t *drp; 8525 bool_t needrecov = FALSE; 8526 nfs4_recov_state_t recov_state; 8527 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8528 dirattr_info_t dinfo, *dinfop; 8529 8530 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8531 return (EPERM); 8532 /* 8533 * As ".." has special meaning and rather than send a rmdir 8534 * over the wire to just let the server freak out, we just 8535 * short circuit it here and return EEXIST 8536 */ 8537 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8538 return (EEXIST); 8539 8540 drp = VTOR4(dvp); 8541 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8542 return (EINTR); 8543 8544 /* 8545 * Attempt to prevent a rmdir(".") from succeeding. 8546 */ 8547 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8548 if (e.error) { 8549 nfs_rw_exit(&drp->r_rwlock); 8550 return (e.error); 8551 } 8552 if (vp == cdir) { 8553 VN_RELE(vp); 8554 nfs_rw_exit(&drp->r_rwlock); 8555 return (EINVAL); 8556 } 8557 8558 /* 8559 * Since nfsv4 remove op works on both files and directories, 8560 * check that the removed object is indeed a directory. 8561 */ 8562 if (vp->v_type != VDIR) { 8563 VN_RELE(vp); 8564 nfs_rw_exit(&drp->r_rwlock); 8565 return (ENOTDIR); 8566 } 8567 8568 /* 8569 * First just remove the entry from the name cache, as it 8570 * is most likely an entry for this vp. 8571 */ 8572 dnlc_remove(dvp, nm); 8573 8574 /* 8575 * If there vnode reference count is greater than one, then 8576 * there may be additional references in the DNLC which will 8577 * need to be purged. First, trying removing the entry for 8578 * the parent directory and see if that removes the additional 8579 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8580 * to completely remove any references to the directory which 8581 * might still exist in the DNLC. 8582 */ 8583 if (vp->v_count > 1) { 8584 dnlc_remove(vp, ".."); 8585 if (vp->v_count > 1) 8586 dnlc_purge_vp(vp); 8587 } 8588 8589 mi = VTOMI4(dvp); 8590 recov_state.rs_flags = 0; 8591 recov_state.rs_num_retry_despite_err = 0; 8592 8593 recov_retry: 8594 args.ctag = TAG_RMDIR; 8595 8596 /* 8597 * Rmdir ops: putfh dir; remove 8598 */ 8599 args.array_len = 3; 8600 args.array = argop; 8601 8602 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8603 if (e.error) { 8604 nfs_rw_exit(&drp->r_rwlock); 8605 return (e.error); 8606 } 8607 need_end_op = TRUE; 8608 8609 /* putfh directory */ 8610 argop[0].argop = OP_CPUTFH; 8611 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8612 8613 /* remove */ 8614 argop[1].argop = OP_CREMOVE; 8615 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8616 8617 /* getattr (postop attrs for dir that contained removed dir) */ 8618 argop[2].argop = OP_GETATTR; 8619 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8620 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8621 8622 dinfo.di_time_call = gethrtime(); 8623 doqueue = 1; 8624 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8625 8626 PURGE_ATTRCACHE4(vp); 8627 8628 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8629 if (e.error) { 8630 PURGE_ATTRCACHE4(dvp); 8631 } 8632 8633 if (needrecov) { 8634 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8635 NULL, OP_REMOVE, NULL) == FALSE) { 8636 if (!e.error) 8637 (void) xdr_free(xdr_COMPOUND4res_clnt, 8638 (caddr_t)&res); 8639 8640 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8641 needrecov); 8642 need_end_op = FALSE; 8643 goto recov_retry; 8644 } 8645 } 8646 8647 if (!e.error) { 8648 resp = &res; 8649 8650 /* 8651 * Only return error if first 2 ops (OP_REMOVE or earlier) 8652 * failed. 8653 */ 8654 if (res.status != NFS4_OK && res.array_len <= 2) { 8655 e.error = geterrno4(res.status); 8656 PURGE_ATTRCACHE4(dvp); 8657 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8658 &recov_state, needrecov); 8659 need_end_op = FALSE; 8660 nfs4_purge_stale_fh(e.error, dvp, cr); 8661 /* 8662 * System V defines rmdir to return EEXIST, not 8663 * ENOTEMPTY if the directory is not empty. Over 8664 * the wire, the error is NFSERR_ENOTEMPTY which 8665 * geterrno4 maps to ENOTEMPTY. 8666 */ 8667 if (e.error == ENOTEMPTY) 8668 e.error = EEXIST; 8669 } else { 8670 resop = &res.array[1]; /* remove res */ 8671 rm_res = &resop->nfs_resop4_u.opremove; 8672 8673 if (res.status == NFS4_OK) { 8674 resop = &res.array[2]; /* dir attrs */ 8675 dinfo.di_garp = 8676 &resop->nfs_resop4_u.opgetattr.ga_res; 8677 dinfo.di_cred = cr; 8678 dinfop = &dinfo; 8679 } else 8680 dinfop = NULL; 8681 8682 /* Update dir attribute, readdir and dnlc caches */ 8683 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8684 dinfop); 8685 8686 /* destroy rddir cache for dir that was removed */ 8687 if (VTOR4(vp)->r_dir != NULL) 8688 nfs4_purge_rddir_cache(vp); 8689 } 8690 } 8691 8692 if (need_end_op) 8693 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8694 8695 nfs_rw_exit(&drp->r_rwlock); 8696 8697 if (resp) 8698 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8699 8700 if (e.error == 0) { 8701 vnode_t *tvp; 8702 rnode4_t *trp; 8703 trp = VTOR4(vp); 8704 tvp = vp; 8705 if (IS_SHADOW(vp, trp)) 8706 tvp = RTOV4(trp); 8707 vnevent_rmdir(tvp, dvp, nm, ct); 8708 } 8709 8710 VN_RELE(vp); 8711 8712 return (e.error); 8713 } 8714 8715 /* ARGSUSED */ 8716 static int 8717 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr, 8718 caller_context_t *ct, int flags) 8719 { 8720 int error; 8721 vnode_t *vp; 8722 rnode4_t *rp; 8723 char *contents; 8724 mntinfo4_t *mi = VTOMI4(dvp); 8725 8726 if (nfs_zone() != mi->mi_zone) 8727 return (EPERM); 8728 if (!(mi->mi_flags & MI4_SYMLINK)) 8729 return (EOPNOTSUPP); 8730 8731 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8732 if (error) 8733 return (error); 8734 8735 ASSERT(nfs4_consistent_type(vp)); 8736 rp = VTOR4(vp); 8737 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8738 8739 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8740 8741 if (contents != NULL) { 8742 mutex_enter(&rp->r_statelock); 8743 if (rp->r_symlink.contents == NULL) { 8744 rp->r_symlink.len = strlen(tnm); 8745 bcopy(tnm, contents, rp->r_symlink.len); 8746 rp->r_symlink.contents = contents; 8747 rp->r_symlink.size = MAXPATHLEN; 8748 mutex_exit(&rp->r_statelock); 8749 } else { 8750 mutex_exit(&rp->r_statelock); 8751 kmem_free((void *)contents, MAXPATHLEN); 8752 } 8753 } 8754 } 8755 VN_RELE(vp); 8756 8757 return (error); 8758 } 8759 8760 8761 /* 8762 * Read directory entries. 8763 * There are some weird things to look out for here. The uio_loffset 8764 * field is either 0 or it is the offset returned from a previous 8765 * readdir. It is an opaque value used by the server to find the 8766 * correct directory block to read. The count field is the number 8767 * of blocks to read on the server. This is advisory only, the server 8768 * may return only one block's worth of entries. Entries may be compressed 8769 * on the server. 8770 */ 8771 /* ARGSUSED */ 8772 static int 8773 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp, 8774 caller_context_t *ct, int flags) 8775 { 8776 int error; 8777 uint_t count; 8778 rnode4_t *rp; 8779 rddir4_cache *rdc; 8780 rddir4_cache *rrdc; 8781 8782 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8783 return (EIO); 8784 rp = VTOR4(vp); 8785 8786 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8787 8788 /* 8789 * Make sure that the directory cache is valid. 8790 */ 8791 if (rp->r_dir != NULL) { 8792 if (nfs_disable_rddir_cache != 0) { 8793 /* 8794 * Setting nfs_disable_rddir_cache in /etc/system 8795 * allows interoperability with servers that do not 8796 * properly update the attributes of directories. 8797 * Any cached information gets purged before an 8798 * access is made to it. 8799 */ 8800 nfs4_purge_rddir_cache(vp); 8801 } 8802 8803 error = nfs4_validate_caches(vp, cr); 8804 if (error) 8805 return (error); 8806 } 8807 8808 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8809 8810 /* 8811 * Short circuit last readdir which always returns 0 bytes. 8812 * This can be done after the directory has been read through 8813 * completely at least once. This will set r_direof which 8814 * can be used to find the value of the last cookie. 8815 */ 8816 mutex_enter(&rp->r_statelock); 8817 if (rp->r_direof != NULL && 8818 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8819 mutex_exit(&rp->r_statelock); 8820 #ifdef DEBUG 8821 nfs4_readdir_cache_shorts++; 8822 #endif 8823 if (eofp) 8824 *eofp = 1; 8825 return (0); 8826 } 8827 8828 /* 8829 * Look for a cache entry. Cache entries are identified 8830 * by the NFS cookie value and the byte count requested. 8831 */ 8832 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8833 8834 /* 8835 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8836 */ 8837 if (rdc == NULL) { 8838 mutex_exit(&rp->r_statelock); 8839 return (EINTR); 8840 } 8841 8842 /* 8843 * Check to see if we need to fill this entry in. 8844 */ 8845 if (rdc->flags & RDDIRREQ) { 8846 rdc->flags &= ~RDDIRREQ; 8847 rdc->flags |= RDDIR; 8848 mutex_exit(&rp->r_statelock); 8849 8850 /* 8851 * Do the readdir. 8852 */ 8853 nfs4readdir(vp, rdc, cr); 8854 8855 /* 8856 * Reacquire the lock, so that we can continue 8857 */ 8858 mutex_enter(&rp->r_statelock); 8859 /* 8860 * The entry is now complete 8861 */ 8862 rdc->flags &= ~RDDIR; 8863 } 8864 8865 ASSERT(!(rdc->flags & RDDIR)); 8866 8867 /* 8868 * If an error occurred while attempting 8869 * to fill the cache entry, mark the entry invalid and 8870 * just return the error. 8871 */ 8872 if (rdc->error) { 8873 error = rdc->error; 8874 rdc->flags |= RDDIRREQ; 8875 rddir4_cache_rele(rp, rdc); 8876 mutex_exit(&rp->r_statelock); 8877 return (error); 8878 } 8879 8880 /* 8881 * The cache entry is complete and good, 8882 * copyout the dirent structs to the calling 8883 * thread. 8884 */ 8885 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 8886 8887 /* 8888 * If no error occurred during the copyout, 8889 * update the offset in the uio struct to 8890 * contain the value of the next NFS 4 cookie 8891 * and set the eof value appropriately. 8892 */ 8893 if (!error) { 8894 uiop->uio_loffset = rdc->nfs4_ncookie; 8895 if (eofp) 8896 *eofp = rdc->eof; 8897 } 8898 8899 /* 8900 * Decide whether to do readahead. Don't if we 8901 * have already read to the end of directory. 8902 */ 8903 if (rdc->eof) { 8904 /* 8905 * Make the entry the direof only if it is cached 8906 */ 8907 if (rdc->flags & RDDIRCACHED) 8908 rp->r_direof = rdc; 8909 rddir4_cache_rele(rp, rdc); 8910 mutex_exit(&rp->r_statelock); 8911 return (error); 8912 } 8913 8914 /* Determine if a readdir readahead should be done */ 8915 if (!(rp->r_flags & R4LOOKUP)) { 8916 rddir4_cache_rele(rp, rdc); 8917 mutex_exit(&rp->r_statelock); 8918 return (error); 8919 } 8920 8921 /* 8922 * Now look for a readahead entry. 8923 * 8924 * Check to see whether we found an entry for the readahead. 8925 * If so, we don't need to do anything further, so free the new 8926 * entry if one was allocated. Otherwise, allocate a new entry, add 8927 * it to the cache, and then initiate an asynchronous readdir 8928 * operation to fill it. 8929 */ 8930 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 8931 8932 /* 8933 * A readdir cache entry could not be obtained for the readahead. In 8934 * this case we skip the readahead and return. 8935 */ 8936 if (rrdc == NULL) { 8937 rddir4_cache_rele(rp, rdc); 8938 mutex_exit(&rp->r_statelock); 8939 return (error); 8940 } 8941 8942 /* 8943 * Check to see if we need to fill this entry in. 8944 */ 8945 if (rrdc->flags & RDDIRREQ) { 8946 rrdc->flags &= ~RDDIRREQ; 8947 rrdc->flags |= RDDIR; 8948 rddir4_cache_rele(rp, rdc); 8949 mutex_exit(&rp->r_statelock); 8950 #ifdef DEBUG 8951 nfs4_readdir_readahead++; 8952 #endif 8953 /* 8954 * Do the readdir. 8955 */ 8956 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 8957 return (error); 8958 } 8959 8960 rddir4_cache_rele(rp, rrdc); 8961 rddir4_cache_rele(rp, rdc); 8962 mutex_exit(&rp->r_statelock); 8963 return (error); 8964 } 8965 8966 static int 8967 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8968 { 8969 int error; 8970 rnode4_t *rp; 8971 8972 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 8973 8974 rp = VTOR4(vp); 8975 8976 /* 8977 * Obtain the readdir results for the caller. 8978 */ 8979 nfs4readdir(vp, rdc, cr); 8980 8981 mutex_enter(&rp->r_statelock); 8982 /* 8983 * The entry is now complete 8984 */ 8985 rdc->flags &= ~RDDIR; 8986 8987 error = rdc->error; 8988 if (error) 8989 rdc->flags |= RDDIRREQ; 8990 rddir4_cache_rele(rp, rdc); 8991 mutex_exit(&rp->r_statelock); 8992 8993 return (error); 8994 } 8995 8996 /* 8997 * Read directory entries. 8998 * There are some weird things to look out for here. The uio_loffset 8999 * field is either 0 or it is the offset returned from a previous 9000 * readdir. It is an opaque value used by the server to find the 9001 * correct directory block to read. The count field is the number 9002 * of blocks to read on the server. This is advisory only, the server 9003 * may return only one block's worth of entries. Entries may be compressed 9004 * on the server. 9005 * 9006 * Generates the following compound request: 9007 * 1. If readdir offset is zero and no dnlc entry for parent exists, 9008 * must include a Lookupp as well. In this case, send: 9009 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 9010 * 2. Otherwise just do: { Putfh <fh>; Readdir } 9011 * 9012 * Get complete attributes and filehandles for entries if this is the 9013 * first read of the directory. Otherwise, just get fileid's. 9014 */ 9015 static void 9016 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9017 { 9018 COMPOUND4args_clnt args; 9019 COMPOUND4res_clnt res; 9020 READDIR4args *rargs; 9021 READDIR4res_clnt *rd_res; 9022 bitmap4 rd_bitsval; 9023 nfs_argop4 argop[5]; 9024 nfs_resop4 *resop; 9025 rnode4_t *rp = VTOR4(vp); 9026 mntinfo4_t *mi = VTOMI4(vp); 9027 int doqueue; 9028 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 9029 vnode_t *dvp; 9030 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 9031 int num_ops, res_opcnt; 9032 bool_t needrecov = FALSE; 9033 nfs4_recov_state_t recov_state; 9034 hrtime_t t; 9035 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 9036 9037 ASSERT(nfs_zone() == mi->mi_zone); 9038 ASSERT(rdc->flags & RDDIR); 9039 ASSERT(rdc->entries == NULL); 9040 9041 /* 9042 * If rp were a stub, it should have triggered and caused 9043 * a mount for us to get this far. 9044 */ 9045 ASSERT(!RP_ISSTUB(rp)); 9046 9047 num_ops = 2; 9048 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9049 /* 9050 * Since nfsv4 readdir may not return entries for "." and "..", 9051 * the client must recreate them: 9052 * To find the correct nodeid, do the following: 9053 * For current node, get nodeid from dnlc. 9054 * - if current node is rootvp, set pnodeid to nodeid. 9055 * - else if parent is in the dnlc, get its nodeid from there. 9056 * - else add LOOKUPP+GETATTR to compound. 9057 */ 9058 nodeid = rp->r_attr.va_nodeid; 9059 if (vp->v_flag & VROOT) { 9060 pnodeid = nodeid; /* root of mount point */ 9061 } else { 9062 dvp = dnlc_lookup(vp, ".."); 9063 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9064 /* parent in dnlc cache - no need for otw */ 9065 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9066 } else { 9067 /* 9068 * parent not in dnlc cache, 9069 * do lookupp to get its id 9070 */ 9071 num_ops = 5; 9072 pnodeid = 0; /* set later by getattr parent */ 9073 } 9074 if (dvp) 9075 VN_RELE(dvp); 9076 } 9077 } 9078 recov_state.rs_flags = 0; 9079 recov_state.rs_num_retry_despite_err = 0; 9080 9081 /* Save the original mount point security flavor */ 9082 (void) save_mnt_secinfo(mi->mi_curr_serv); 9083 9084 recov_retry: 9085 args.ctag = TAG_READDIR; 9086 9087 args.array = argop; 9088 args.array_len = num_ops; 9089 9090 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9091 &recov_state, NULL)) { 9092 /* 9093 * If readdir a node that is a stub for a crossed mount point, 9094 * keep the original secinfo flavor for the current file 9095 * system, not the crossed one. 9096 */ 9097 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9098 rdc->error = e.error; 9099 return; 9100 } 9101 9102 /* 9103 * Determine which attrs to request for dirents. This code 9104 * must be protected by nfs4_start/end_fop because of r_server 9105 * (which will change during failover recovery). 9106 * 9107 */ 9108 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9109 /* 9110 * Get all vattr attrs plus filehandle and rdattr_error 9111 */ 9112 rd_bitsval = NFS4_VATTR_MASK | 9113 FATTR4_RDATTR_ERROR_MASK | 9114 FATTR4_FILEHANDLE_MASK; 9115 9116 if (rp->r_flags & R4READDIRWATTR) { 9117 mutex_enter(&rp->r_statelock); 9118 rp->r_flags &= ~R4READDIRWATTR; 9119 mutex_exit(&rp->r_statelock); 9120 } 9121 } else { 9122 servinfo4_t *svp = rp->r_server; 9123 9124 /* 9125 * Already read directory. Use readdir with 9126 * no attrs (except for mounted_on_fileid) for updates. 9127 */ 9128 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9129 9130 /* 9131 * request mounted on fileid if supported, else request 9132 * fileid. maybe we should verify that fileid is supported 9133 * and request something else if not. 9134 */ 9135 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9136 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9137 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9138 nfs_rw_exit(&svp->sv_lock); 9139 } 9140 9141 /* putfh directory fh */ 9142 argop[0].argop = OP_CPUTFH; 9143 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9144 9145 argop[1].argop = OP_READDIR; 9146 rargs = &argop[1].nfs_argop4_u.opreaddir; 9147 /* 9148 * 1 and 2 are reserved for client "." and ".." entry offset. 9149 * cookie 0 should be used over-the-wire to start reading at 9150 * the beginning of the directory excluding "." and "..". 9151 */ 9152 if (rdc->nfs4_cookie == 0 || 9153 rdc->nfs4_cookie == 1 || 9154 rdc->nfs4_cookie == 2) { 9155 rargs->cookie = (nfs_cookie4)0; 9156 rargs->cookieverf = 0; 9157 } else { 9158 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9159 mutex_enter(&rp->r_statelock); 9160 rargs->cookieverf = rp->r_cookieverf4; 9161 mutex_exit(&rp->r_statelock); 9162 } 9163 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9164 rargs->maxcount = mi->mi_tsize; 9165 rargs->attr_request = rd_bitsval; 9166 rargs->rdc = rdc; 9167 rargs->dvp = vp; 9168 rargs->mi = mi; 9169 rargs->cr = cr; 9170 9171 9172 /* 9173 * If count < than the minimum required, we return no entries 9174 * and fail with EINVAL 9175 */ 9176 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9177 rdc->error = EINVAL; 9178 goto out; 9179 } 9180 9181 if (args.array_len == 5) { 9182 /* 9183 * Add lookupp and getattr for parent nodeid. 9184 */ 9185 argop[2].argop = OP_LOOKUPP; 9186 9187 argop[3].argop = OP_GETFH; 9188 9189 /* getattr parent */ 9190 argop[4].argop = OP_GETATTR; 9191 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9192 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9193 } 9194 9195 doqueue = 1; 9196 9197 if (mi->mi_io_kstats) { 9198 mutex_enter(&mi->mi_lock); 9199 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9200 mutex_exit(&mi->mi_lock); 9201 } 9202 9203 /* capture the time of this call */ 9204 rargs->t = t = gethrtime(); 9205 9206 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9207 9208 if (mi->mi_io_kstats) { 9209 mutex_enter(&mi->mi_lock); 9210 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9211 mutex_exit(&mi->mi_lock); 9212 } 9213 9214 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9215 9216 /* 9217 * If RPC error occurred and it isn't an error that 9218 * triggers recovery, then go ahead and fail now. 9219 */ 9220 if (e.error != 0 && !needrecov) { 9221 rdc->error = e.error; 9222 goto out; 9223 } 9224 9225 if (needrecov) { 9226 bool_t abort; 9227 9228 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9229 "nfs4readdir: initiating recovery.\n")); 9230 9231 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9232 NULL, OP_READDIR, NULL); 9233 if (abort == FALSE) { 9234 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9235 &recov_state, needrecov); 9236 if (!e.error) 9237 (void) xdr_free(xdr_COMPOUND4res_clnt, 9238 (caddr_t)&res); 9239 if (rdc->entries != NULL) { 9240 kmem_free(rdc->entries, rdc->entlen); 9241 rdc->entries = NULL; 9242 } 9243 goto recov_retry; 9244 } 9245 9246 if (e.error != 0) { 9247 rdc->error = e.error; 9248 goto out; 9249 } 9250 9251 /* fall through for res.status case */ 9252 } 9253 9254 res_opcnt = res.array_len; 9255 9256 /* 9257 * If compound failed first 2 ops (PUTFH+READDIR), then return 9258 * failure here. Subsequent ops are for filling out dot-dot 9259 * dirent, and if they fail, we still want to give the caller 9260 * the dirents returned by (the successful) READDIR op, so we need 9261 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9262 * 9263 * One example where PUTFH+READDIR ops would succeed but 9264 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9265 * but lacks x. In this case, a POSIX server's VOP_READDIR 9266 * would succeed; however, VOP_LOOKUP(..) would fail since no 9267 * x perm. We need to come up with a non-vendor-specific way 9268 * for a POSIX server to return d_ino from dotdot's dirent if 9269 * client only requests mounted_on_fileid, and just say the 9270 * LOOKUPP succeeded and fill out the GETATTR. However, if 9271 * client requested any mandatory attrs, server would be required 9272 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9273 * for dotdot. 9274 */ 9275 9276 if (res.status) { 9277 if (res_opcnt <= 2) { 9278 e.error = geterrno4(res.status); 9279 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9280 &recov_state, needrecov); 9281 nfs4_purge_stale_fh(e.error, vp, cr); 9282 rdc->error = e.error; 9283 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9284 if (rdc->entries != NULL) { 9285 kmem_free(rdc->entries, rdc->entlen); 9286 rdc->entries = NULL; 9287 } 9288 /* 9289 * If readdir a node that is a stub for a 9290 * crossed mount point, keep the original 9291 * secinfo flavor for the current file system, 9292 * not the crossed one. 9293 */ 9294 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9295 return; 9296 } 9297 } 9298 9299 resop = &res.array[1]; /* readdir res */ 9300 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9301 9302 mutex_enter(&rp->r_statelock); 9303 rp->r_cookieverf4 = rd_res->cookieverf; 9304 mutex_exit(&rp->r_statelock); 9305 9306 /* 9307 * For "." and ".." entries 9308 * e.g. 9309 * seek(cookie=0) -> "." entry with d_off = 1 9310 * seek(cookie=1) -> ".." entry with d_off = 2 9311 */ 9312 if (cookie == (nfs_cookie4) 0) { 9313 if (rd_res->dotp) 9314 rd_res->dotp->d_ino = nodeid; 9315 if (rd_res->dotdotp) 9316 rd_res->dotdotp->d_ino = pnodeid; 9317 } 9318 if (cookie == (nfs_cookie4) 1) { 9319 if (rd_res->dotdotp) 9320 rd_res->dotdotp->d_ino = pnodeid; 9321 } 9322 9323 9324 /* LOOKUPP+GETATTR attemped */ 9325 if (args.array_len == 5 && rd_res->dotdotp) { 9326 if (res.status == NFS4_OK && res_opcnt == 5) { 9327 nfs_fh4 *fhp; 9328 nfs4_sharedfh_t *sfhp; 9329 vnode_t *pvp; 9330 nfs4_ga_res_t *garp; 9331 9332 resop++; /* lookupp */ 9333 resop++; /* getfh */ 9334 fhp = &resop->nfs_resop4_u.opgetfh.object; 9335 9336 resop++; /* getattr of parent */ 9337 9338 /* 9339 * First, take care of finishing the 9340 * readdir results. 9341 */ 9342 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9343 /* 9344 * The d_ino of .. must be the inode number 9345 * of the mounted filesystem. 9346 */ 9347 if (garp->n4g_va.va_mask & AT_NODEID) 9348 rd_res->dotdotp->d_ino = 9349 garp->n4g_va.va_nodeid; 9350 9351 9352 /* 9353 * Next, create the ".." dnlc entry 9354 */ 9355 sfhp = sfh4_get(fhp, mi); 9356 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9357 dnlc_update(vp, "..", pvp); 9358 VN_RELE(pvp); 9359 } 9360 sfh4_rele(&sfhp); 9361 } 9362 } 9363 9364 if (mi->mi_io_kstats) { 9365 mutex_enter(&mi->mi_lock); 9366 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9367 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9368 mutex_exit(&mi->mi_lock); 9369 } 9370 9371 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9372 9373 out: 9374 /* 9375 * If readdir a node that is a stub for a crossed mount point, 9376 * keep the original secinfo flavor for the current file system, 9377 * not the crossed one. 9378 */ 9379 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9380 9381 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9382 } 9383 9384 9385 static int 9386 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9387 { 9388 rnode4_t *rp = VTOR4(bp->b_vp); 9389 int count; 9390 int error; 9391 cred_t *cred_otw = NULL; 9392 offset_t offset; 9393 nfs4_open_stream_t *osp = NULL; 9394 bool_t first_time = TRUE; /* first time getting otw cred */ 9395 bool_t last_time = FALSE; /* last time getting otw cred */ 9396 9397 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9398 9399 DTRACE_IO1(start, struct buf *, bp); 9400 offset = ldbtob(bp->b_lblkno); 9401 9402 if (bp->b_flags & B_READ) { 9403 read_again: 9404 /* 9405 * Releases the osp, if it is provided. 9406 * Puts a hold on the cred_otw and the new osp (if found). 9407 */ 9408 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9409 &first_time, &last_time); 9410 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9411 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9412 readahead, NULL); 9413 crfree(cred_otw); 9414 if (!error) { 9415 if (bp->b_resid) { 9416 /* 9417 * Didn't get it all because we hit EOF, 9418 * zero all the memory beyond the EOF. 9419 */ 9420 /* bzero(rdaddr + */ 9421 bzero(bp->b_un.b_addr + 9422 bp->b_bcount - bp->b_resid, bp->b_resid); 9423 } 9424 mutex_enter(&rp->r_statelock); 9425 if (bp->b_resid == bp->b_bcount && 9426 offset >= rp->r_size) { 9427 /* 9428 * We didn't read anything at all as we are 9429 * past EOF. Return an error indicator back 9430 * but don't destroy the pages (yet). 9431 */ 9432 error = NFS_EOF; 9433 } 9434 mutex_exit(&rp->r_statelock); 9435 } else if (error == EACCES && last_time == FALSE) { 9436 goto read_again; 9437 } 9438 } else { 9439 if (!(rp->r_flags & R4STALE)) { 9440 write_again: 9441 /* 9442 * Releases the osp, if it is provided. 9443 * Puts a hold on the cred_otw and the new 9444 * osp (if found). 9445 */ 9446 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9447 &first_time, &last_time); 9448 mutex_enter(&rp->r_statelock); 9449 count = MIN(bp->b_bcount, rp->r_size - offset); 9450 mutex_exit(&rp->r_statelock); 9451 if (count < 0) 9452 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9453 #ifdef DEBUG 9454 if (count == 0) { 9455 zoneid_t zoneid = getzoneid(); 9456 9457 zcmn_err(zoneid, CE_WARN, 9458 "nfs4_bio: zero length write at %lld", 9459 offset); 9460 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9461 "b_bcount=%ld, file size=%lld", 9462 rp->r_flags, (long)bp->b_bcount, 9463 rp->r_size); 9464 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9465 if (nfs4_bio_do_stop) 9466 debug_enter("nfs4_bio"); 9467 } 9468 #endif 9469 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9470 count, cred_otw, stab_comm); 9471 if (error == EACCES && last_time == FALSE) { 9472 crfree(cred_otw); 9473 goto write_again; 9474 } 9475 bp->b_error = error; 9476 if (error && error != EINTR && 9477 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9478 /* 9479 * Don't print EDQUOT errors on the console. 9480 * Don't print asynchronous EACCES errors. 9481 * Don't print EFBIG errors. 9482 * Print all other write errors. 9483 */ 9484 if (error != EDQUOT && error != EFBIG && 9485 (error != EACCES || 9486 !(bp->b_flags & B_ASYNC))) 9487 nfs4_write_error(bp->b_vp, 9488 error, cred_otw); 9489 /* 9490 * Update r_error and r_flags as appropriate. 9491 * If the error was ESTALE, then mark the 9492 * rnode as not being writeable and save 9493 * the error status. Otherwise, save any 9494 * errors which occur from asynchronous 9495 * page invalidations. Any errors occurring 9496 * from other operations should be saved 9497 * by the caller. 9498 */ 9499 mutex_enter(&rp->r_statelock); 9500 if (error == ESTALE) { 9501 rp->r_flags |= R4STALE; 9502 if (!rp->r_error) 9503 rp->r_error = error; 9504 } else if (!rp->r_error && 9505 (bp->b_flags & 9506 (B_INVAL|B_FORCE|B_ASYNC)) == 9507 (B_INVAL|B_FORCE|B_ASYNC)) { 9508 rp->r_error = error; 9509 } 9510 mutex_exit(&rp->r_statelock); 9511 } 9512 crfree(cred_otw); 9513 } else 9514 error = rp->r_error; 9515 } 9516 9517 if (error != 0 && error != NFS_EOF) 9518 bp->b_flags |= B_ERROR; 9519 9520 if (osp) 9521 open_stream_rele(osp, rp); 9522 9523 DTRACE_IO1(done, struct buf *, bp); 9524 9525 return (error); 9526 } 9527 9528 /* ARGSUSED */ 9529 int 9530 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 9531 { 9532 return (EREMOTE); 9533 } 9534 9535 /* ARGSUSED2 */ 9536 int 9537 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9538 { 9539 rnode4_t *rp = VTOR4(vp); 9540 9541 if (!write_lock) { 9542 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9543 return (V_WRITELOCK_FALSE); 9544 } 9545 9546 if ((rp->r_flags & R4DIRECTIO) || 9547 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9548 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9549 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9550 return (V_WRITELOCK_FALSE); 9551 nfs_rw_exit(&rp->r_rwlock); 9552 } 9553 9554 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9555 return (V_WRITELOCK_TRUE); 9556 } 9557 9558 /* ARGSUSED */ 9559 void 9560 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9561 { 9562 rnode4_t *rp = VTOR4(vp); 9563 9564 nfs_rw_exit(&rp->r_rwlock); 9565 } 9566 9567 /* ARGSUSED */ 9568 static int 9569 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 9570 { 9571 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9572 return (EIO); 9573 9574 /* 9575 * Because we stuff the readdir cookie into the offset field 9576 * someone may attempt to do an lseek with the cookie which 9577 * we want to succeed. 9578 */ 9579 if (vp->v_type == VDIR) 9580 return (0); 9581 if (*noffp < 0) 9582 return (EINVAL); 9583 return (0); 9584 } 9585 9586 9587 /* 9588 * Return all the pages from [off..off+len) in file 9589 */ 9590 /* ARGSUSED */ 9591 static int 9592 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9593 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9594 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 9595 { 9596 rnode4_t *rp; 9597 int error; 9598 mntinfo4_t *mi; 9599 9600 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9601 return (EIO); 9602 rp = VTOR4(vp); 9603 if (IS_SHADOW(vp, rp)) 9604 vp = RTOV4(rp); 9605 9606 if (vp->v_flag & VNOMAP) 9607 return (ENOSYS); 9608 9609 if (protp != NULL) 9610 *protp = PROT_ALL; 9611 9612 /* 9613 * Now validate that the caches are up to date. 9614 */ 9615 if (error = nfs4_validate_caches(vp, cr)) 9616 return (error); 9617 9618 mi = VTOMI4(vp); 9619 retry: 9620 mutex_enter(&rp->r_statelock); 9621 9622 /* 9623 * Don't create dirty pages faster than they 9624 * can be cleaned so that the system doesn't 9625 * get imbalanced. If the async queue is 9626 * maxed out, then wait for it to drain before 9627 * creating more dirty pages. Also, wait for 9628 * any threads doing pagewalks in the vop_getattr 9629 * entry points so that they don't block for 9630 * long periods. 9631 */ 9632 if (rw == S_CREATE) { 9633 while ((mi->mi_max_threads != 0 && 9634 rp->r_awcount > 2 * mi->mi_max_threads) || 9635 rp->r_gcount > 0) 9636 cv_wait(&rp->r_cv, &rp->r_statelock); 9637 } 9638 9639 /* 9640 * If we are getting called as a side effect of an nfs_write() 9641 * operation the local file size might not be extended yet. 9642 * In this case we want to be able to return pages of zeroes. 9643 */ 9644 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9645 NFS4_DEBUG(nfs4_pageio_debug, 9646 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9647 "len=%llu, size=%llu, attrsize =%llu", off, 9648 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9649 mutex_exit(&rp->r_statelock); 9650 return (EFAULT); /* beyond EOF */ 9651 } 9652 9653 mutex_exit(&rp->r_statelock); 9654 9655 if (len <= PAGESIZE) { 9656 error = nfs4_getapage(vp, off, len, protp, pl, plsz, 9657 seg, addr, rw, cr); 9658 NFS4_DEBUG(nfs4_pageio_debug && error, 9659 (CE_NOTE, "getpage error %d; off=%lld, " 9660 "len=%lld", error, off, (u_longlong_t)len)); 9661 } else { 9662 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9663 pl, plsz, seg, addr, rw, cr); 9664 NFS4_DEBUG(nfs4_pageio_debug && error, 9665 (CE_NOTE, "getpages error %d; off=%lld, " 9666 "len=%lld", error, off, (u_longlong_t)len)); 9667 } 9668 9669 switch (error) { 9670 case NFS_EOF: 9671 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9672 goto retry; 9673 case ESTALE: 9674 nfs4_purge_stale_fh(error, vp, cr); 9675 } 9676 9677 return (error); 9678 } 9679 9680 /* 9681 * Called from pvn_getpages or nfs4_getpage to get a particular page. 9682 */ 9683 /* ARGSUSED */ 9684 static int 9685 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9686 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9687 enum seg_rw rw, cred_t *cr) 9688 { 9689 rnode4_t *rp; 9690 uint_t bsize; 9691 struct buf *bp; 9692 page_t *pp; 9693 u_offset_t lbn; 9694 u_offset_t io_off; 9695 u_offset_t blkoff; 9696 u_offset_t rablkoff; 9697 size_t io_len; 9698 uint_t blksize; 9699 int error; 9700 int readahead; 9701 int readahead_issued = 0; 9702 int ra_window; /* readahead window */ 9703 page_t *pagefound; 9704 page_t *savepp; 9705 9706 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9707 return (EIO); 9708 9709 rp = VTOR4(vp); 9710 ASSERT(!IS_SHADOW(vp, rp)); 9711 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9712 9713 reread: 9714 bp = NULL; 9715 pp = NULL; 9716 pagefound = NULL; 9717 9718 if (pl != NULL) 9719 pl[0] = NULL; 9720 9721 error = 0; 9722 lbn = off / bsize; 9723 blkoff = lbn * bsize; 9724 9725 /* 9726 * Queueing up the readahead before doing the synchronous read 9727 * results in a significant increase in read throughput because 9728 * of the increased parallelism between the async threads and 9729 * the process context. 9730 */ 9731 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9732 rw != S_CREATE && 9733 !(vp->v_flag & VNOCACHE)) { 9734 mutex_enter(&rp->r_statelock); 9735 9736 /* 9737 * Calculate the number of readaheads to do. 9738 * a) No readaheads at offset = 0. 9739 * b) Do maximum(nfs4_nra) readaheads when the readahead 9740 * window is closed. 9741 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9742 * upon how far the readahead window is open or close. 9743 * d) No readaheads if rp->r_nextr is not within the scope 9744 * of the readahead window (random i/o). 9745 */ 9746 9747 if (off == 0) 9748 readahead = 0; 9749 else if (blkoff == rp->r_nextr) 9750 readahead = nfs4_nra; 9751 else if (rp->r_nextr > blkoff && 9752 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9753 <= (nfs4_nra - 1))) 9754 readahead = nfs4_nra - ra_window; 9755 else 9756 readahead = 0; 9757 9758 rablkoff = rp->r_nextr; 9759 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9760 mutex_exit(&rp->r_statelock); 9761 if (nfs4_async_readahead(vp, rablkoff + bsize, 9762 addr + (rablkoff + bsize - off), 9763 seg, cr, nfs4_readahead) < 0) { 9764 mutex_enter(&rp->r_statelock); 9765 break; 9766 } 9767 readahead--; 9768 rablkoff += bsize; 9769 /* 9770 * Indicate that we did a readahead so 9771 * readahead offset is not updated 9772 * by the synchronous read below. 9773 */ 9774 readahead_issued = 1; 9775 mutex_enter(&rp->r_statelock); 9776 /* 9777 * set readahead offset to 9778 * offset of last async readahead 9779 * request. 9780 */ 9781 rp->r_nextr = rablkoff; 9782 } 9783 mutex_exit(&rp->r_statelock); 9784 } 9785 9786 again: 9787 if ((pagefound = page_exists(vp, off)) == NULL) { 9788 if (pl == NULL) { 9789 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9790 nfs4_readahead); 9791 } else if (rw == S_CREATE) { 9792 /* 9793 * Block for this page is not allocated, or the offset 9794 * is beyond the current allocation size, or we're 9795 * allocating a swap slot and the page was not found, 9796 * so allocate it and return a zero page. 9797 */ 9798 if ((pp = page_create_va(vp, off, 9799 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9800 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9801 io_len = PAGESIZE; 9802 mutex_enter(&rp->r_statelock); 9803 rp->r_nextr = off + PAGESIZE; 9804 mutex_exit(&rp->r_statelock); 9805 } else { 9806 /* 9807 * Need to go to server to get a block 9808 */ 9809 mutex_enter(&rp->r_statelock); 9810 if (blkoff < rp->r_size && 9811 blkoff + bsize > rp->r_size) { 9812 /* 9813 * If less than a block left in 9814 * file read less than a block. 9815 */ 9816 if (rp->r_size <= off) { 9817 /* 9818 * Trying to access beyond EOF, 9819 * set up to get at least one page. 9820 */ 9821 blksize = off + PAGESIZE - blkoff; 9822 } else 9823 blksize = rp->r_size - blkoff; 9824 } else if ((off == 0) || 9825 (off != rp->r_nextr && !readahead_issued)) { 9826 blksize = PAGESIZE; 9827 blkoff = off; /* block = page here */ 9828 } else 9829 blksize = bsize; 9830 mutex_exit(&rp->r_statelock); 9831 9832 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9833 &io_len, blkoff, blksize, 0); 9834 9835 /* 9836 * Some other thread has entered the page, 9837 * so just use it. 9838 */ 9839 if (pp == NULL) 9840 goto again; 9841 9842 /* 9843 * Now round the request size up to page boundaries. 9844 * This ensures that the entire page will be 9845 * initialized to zeroes if EOF is encountered. 9846 */ 9847 io_len = ptob(btopr(io_len)); 9848 9849 bp = pageio_setup(pp, io_len, vp, B_READ); 9850 ASSERT(bp != NULL); 9851 9852 /* 9853 * pageio_setup should have set b_addr to 0. This 9854 * is correct since we want to do I/O on a page 9855 * boundary. bp_mapin will use this addr to calculate 9856 * an offset, and then set b_addr to the kernel virtual 9857 * address it allocated for us. 9858 */ 9859 ASSERT(bp->b_un.b_addr == 0); 9860 9861 bp->b_edev = 0; 9862 bp->b_dev = 0; 9863 bp->b_lblkno = lbtodb(io_off); 9864 bp->b_file = vp; 9865 bp->b_offset = (offset_t)off; 9866 bp_mapin(bp); 9867 9868 /* 9869 * If doing a write beyond what we believe is EOF, 9870 * don't bother trying to read the pages from the 9871 * server, we'll just zero the pages here. We 9872 * don't check that the rw flag is S_WRITE here 9873 * because some implementations may attempt a 9874 * read access to the buffer before copying data. 9875 */ 9876 mutex_enter(&rp->r_statelock); 9877 if (io_off >= rp->r_size && seg == segkmap) { 9878 mutex_exit(&rp->r_statelock); 9879 bzero(bp->b_un.b_addr, io_len); 9880 } else { 9881 mutex_exit(&rp->r_statelock); 9882 error = nfs4_bio(bp, NULL, cr, FALSE); 9883 } 9884 9885 /* 9886 * Unmap the buffer before freeing it. 9887 */ 9888 bp_mapout(bp); 9889 pageio_done(bp); 9890 9891 savepp = pp; 9892 do { 9893 pp->p_fsdata = C_NOCOMMIT; 9894 } while ((pp = pp->p_next) != savepp); 9895 9896 if (error == NFS_EOF) { 9897 /* 9898 * If doing a write system call just return 9899 * zeroed pages, else user tried to get pages 9900 * beyond EOF, return error. We don't check 9901 * that the rw flag is S_WRITE here because 9902 * some implementations may attempt a read 9903 * access to the buffer before copying data. 9904 */ 9905 if (seg == segkmap) 9906 error = 0; 9907 else 9908 error = EFAULT; 9909 } 9910 9911 if (!readahead_issued && !error) { 9912 mutex_enter(&rp->r_statelock); 9913 rp->r_nextr = io_off + io_len; 9914 mutex_exit(&rp->r_statelock); 9915 } 9916 } 9917 } 9918 9919 out: 9920 if (pl == NULL) 9921 return (error); 9922 9923 if (error) { 9924 if (pp != NULL) 9925 pvn_read_done(pp, B_ERROR); 9926 return (error); 9927 } 9928 9929 if (pagefound) { 9930 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 9931 9932 /* 9933 * Page exists in the cache, acquire the appropriate lock. 9934 * If this fails, start all over again. 9935 */ 9936 if ((pp = page_lookup(vp, off, se)) == NULL) { 9937 #ifdef DEBUG 9938 nfs4_lostpage++; 9939 #endif 9940 goto reread; 9941 } 9942 pl[0] = pp; 9943 pl[1] = NULL; 9944 return (0); 9945 } 9946 9947 if (pp != NULL) 9948 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 9949 9950 return (error); 9951 } 9952 9953 static void 9954 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 9955 cred_t *cr) 9956 { 9957 int error; 9958 page_t *pp; 9959 u_offset_t io_off; 9960 size_t io_len; 9961 struct buf *bp; 9962 uint_t bsize, blksize; 9963 rnode4_t *rp = VTOR4(vp); 9964 page_t *savepp; 9965 9966 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9967 9968 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9969 9970 mutex_enter(&rp->r_statelock); 9971 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 9972 /* 9973 * If less than a block left in file read less 9974 * than a block. 9975 */ 9976 blksize = rp->r_size - blkoff; 9977 } else 9978 blksize = bsize; 9979 mutex_exit(&rp->r_statelock); 9980 9981 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 9982 &io_off, &io_len, blkoff, blksize, 1); 9983 /* 9984 * The isra flag passed to the kluster function is 1, we may have 9985 * gotten a return value of NULL for a variety of reasons (# of free 9986 * pages < minfree, someone entered the page on the vnode etc). In all 9987 * cases, we want to punt on the readahead. 9988 */ 9989 if (pp == NULL) 9990 return; 9991 9992 /* 9993 * Now round the request size up to page boundaries. 9994 * This ensures that the entire page will be 9995 * initialized to zeroes if EOF is encountered. 9996 */ 9997 io_len = ptob(btopr(io_len)); 9998 9999 bp = pageio_setup(pp, io_len, vp, B_READ); 10000 ASSERT(bp != NULL); 10001 10002 /* 10003 * pageio_setup should have set b_addr to 0. This is correct since 10004 * we want to do I/O on a page boundary. bp_mapin() will use this addr 10005 * to calculate an offset, and then set b_addr to the kernel virtual 10006 * address it allocated for us. 10007 */ 10008 ASSERT(bp->b_un.b_addr == 0); 10009 10010 bp->b_edev = 0; 10011 bp->b_dev = 0; 10012 bp->b_lblkno = lbtodb(io_off); 10013 bp->b_file = vp; 10014 bp->b_offset = (offset_t)blkoff; 10015 bp_mapin(bp); 10016 10017 /* 10018 * If doing a write beyond what we believe is EOF, don't bother trying 10019 * to read the pages from the server, we'll just zero the pages here. 10020 * We don't check that the rw flag is S_WRITE here because some 10021 * implementations may attempt a read access to the buffer before 10022 * copying data. 10023 */ 10024 mutex_enter(&rp->r_statelock); 10025 if (io_off >= rp->r_size && seg == segkmap) { 10026 mutex_exit(&rp->r_statelock); 10027 bzero(bp->b_un.b_addr, io_len); 10028 error = 0; 10029 } else { 10030 mutex_exit(&rp->r_statelock); 10031 error = nfs4_bio(bp, NULL, cr, TRUE); 10032 if (error == NFS_EOF) 10033 error = 0; 10034 } 10035 10036 /* 10037 * Unmap the buffer before freeing it. 10038 */ 10039 bp_mapout(bp); 10040 pageio_done(bp); 10041 10042 savepp = pp; 10043 do { 10044 pp->p_fsdata = C_NOCOMMIT; 10045 } while ((pp = pp->p_next) != savepp); 10046 10047 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10048 10049 /* 10050 * In case of error set readahead offset 10051 * to the lowest offset. 10052 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10053 */ 10054 if (error && rp->r_nextr > io_off) { 10055 mutex_enter(&rp->r_statelock); 10056 if (rp->r_nextr > io_off) 10057 rp->r_nextr = io_off; 10058 mutex_exit(&rp->r_statelock); 10059 } 10060 } 10061 10062 /* 10063 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10064 * If len == 0, do from off to EOF. 10065 * 10066 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10067 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10068 * (from pageout). 10069 */ 10070 /* ARGSUSED */ 10071 static int 10072 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 10073 caller_context_t *ct) 10074 { 10075 int error; 10076 rnode4_t *rp; 10077 10078 ASSERT(cr != NULL); 10079 10080 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10081 return (EIO); 10082 10083 rp = VTOR4(vp); 10084 if (IS_SHADOW(vp, rp)) 10085 vp = RTOV4(rp); 10086 10087 /* 10088 * XXX - Why should this check be made here? 10089 */ 10090 if (vp->v_flag & VNOMAP) 10091 return (ENOSYS); 10092 10093 if (len == 0 && !(flags & B_INVAL) && 10094 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10095 return (0); 10096 10097 mutex_enter(&rp->r_statelock); 10098 rp->r_count++; 10099 mutex_exit(&rp->r_statelock); 10100 error = nfs4_putpages(vp, off, len, flags, cr); 10101 mutex_enter(&rp->r_statelock); 10102 rp->r_count--; 10103 cv_broadcast(&rp->r_cv); 10104 mutex_exit(&rp->r_statelock); 10105 10106 return (error); 10107 } 10108 10109 /* 10110 * Write out a single page, possibly klustering adjacent dirty pages. 10111 */ 10112 int 10113 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10114 int flags, cred_t *cr) 10115 { 10116 u_offset_t io_off; 10117 u_offset_t lbn_off; 10118 u_offset_t lbn; 10119 size_t io_len; 10120 uint_t bsize; 10121 int error; 10122 rnode4_t *rp; 10123 10124 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10125 ASSERT(pp != NULL); 10126 ASSERT(cr != NULL); 10127 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10128 10129 rp = VTOR4(vp); 10130 ASSERT(rp->r_count > 0); 10131 ASSERT(!IS_SHADOW(vp, rp)); 10132 10133 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10134 lbn = pp->p_offset / bsize; 10135 lbn_off = lbn * bsize; 10136 10137 /* 10138 * Find a kluster that fits in one block, or in 10139 * one page if pages are bigger than blocks. If 10140 * there is less file space allocated than a whole 10141 * page, we'll shorten the i/o request below. 10142 */ 10143 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10144 roundup(bsize, PAGESIZE), flags); 10145 10146 /* 10147 * pvn_write_kluster shouldn't have returned a page with offset 10148 * behind the original page we were given. Verify that. 10149 */ 10150 ASSERT((pp->p_offset / bsize) >= lbn); 10151 10152 /* 10153 * Now pp will have the list of kept dirty pages marked for 10154 * write back. It will also handle invalidation and freeing 10155 * of pages that are not dirty. Check for page length rounding 10156 * problems. 10157 */ 10158 if (io_off + io_len > lbn_off + bsize) { 10159 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10160 io_len = lbn_off + bsize - io_off; 10161 } 10162 /* 10163 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10164 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10165 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10166 * progress and the r_size has not been made consistent with the 10167 * new size of the file. When the uiomove() completes the r_size is 10168 * updated and the R4MODINPROGRESS flag is cleared. 10169 * 10170 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10171 * consistent value of r_size. Without this handshaking, it is 10172 * possible that nfs4_bio() picks up the old value of r_size 10173 * before the uiomove() in writerp4() completes. This will result 10174 * in the write through nfs4_bio() being dropped. 10175 * 10176 * More precisely, there is a window between the time the uiomove() 10177 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10178 * operation intervenes in this window, the page will be picked up, 10179 * because it is dirty (it will be unlocked, unless it was 10180 * pagecreate'd). When the page is picked up as dirty, the dirty 10181 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10182 * checked. This will still be the old size. Therefore the page will 10183 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10184 * the page will be found to be clean and the write will be dropped. 10185 */ 10186 if (rp->r_flags & R4MODINPROGRESS) { 10187 mutex_enter(&rp->r_statelock); 10188 if ((rp->r_flags & R4MODINPROGRESS) && 10189 rp->r_modaddr + MAXBSIZE > io_off && 10190 rp->r_modaddr < io_off + io_len) { 10191 page_t *plist; 10192 /* 10193 * A write is in progress for this region of the file. 10194 * If we did not detect R4MODINPROGRESS here then this 10195 * path through nfs_putapage() would eventually go to 10196 * nfs4_bio() and may not write out all of the data 10197 * in the pages. We end up losing data. So we decide 10198 * to set the modified bit on each page in the page 10199 * list and mark the rnode with R4DIRTY. This write 10200 * will be restarted at some later time. 10201 */ 10202 plist = pp; 10203 while (plist != NULL) { 10204 pp = plist; 10205 page_sub(&plist, pp); 10206 hat_setmod(pp); 10207 page_io_unlock(pp); 10208 page_unlock(pp); 10209 } 10210 rp->r_flags |= R4DIRTY; 10211 mutex_exit(&rp->r_statelock); 10212 if (offp) 10213 *offp = io_off; 10214 if (lenp) 10215 *lenp = io_len; 10216 return (0); 10217 } 10218 mutex_exit(&rp->r_statelock); 10219 } 10220 10221 if (flags & B_ASYNC) { 10222 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10223 nfs4_sync_putapage); 10224 } else 10225 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10226 10227 if (offp) 10228 *offp = io_off; 10229 if (lenp) 10230 *lenp = io_len; 10231 return (error); 10232 } 10233 10234 static int 10235 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10236 int flags, cred_t *cr) 10237 { 10238 int error; 10239 rnode4_t *rp; 10240 10241 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10242 10243 flags |= B_WRITE; 10244 10245 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10246 10247 rp = VTOR4(vp); 10248 10249 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10250 error == EACCES) && 10251 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10252 if (!(rp->r_flags & R4OUTOFSPACE)) { 10253 mutex_enter(&rp->r_statelock); 10254 rp->r_flags |= R4OUTOFSPACE; 10255 mutex_exit(&rp->r_statelock); 10256 } 10257 flags |= B_ERROR; 10258 pvn_write_done(pp, flags); 10259 /* 10260 * If this was not an async thread, then try again to 10261 * write out the pages, but this time, also destroy 10262 * them whether or not the write is successful. This 10263 * will prevent memory from filling up with these 10264 * pages and destroying them is the only alternative 10265 * if they can't be written out. 10266 * 10267 * Don't do this if this is an async thread because 10268 * when the pages are unlocked in pvn_write_done, 10269 * some other thread could have come along, locked 10270 * them, and queued for an async thread. It would be 10271 * possible for all of the async threads to be tied 10272 * up waiting to lock the pages again and they would 10273 * all already be locked and waiting for an async 10274 * thread to handle them. Deadlock. 10275 */ 10276 if (!(flags & B_ASYNC)) { 10277 error = nfs4_putpage(vp, io_off, io_len, 10278 B_INVAL | B_FORCE, cr, NULL); 10279 } 10280 } else { 10281 if (error) 10282 flags |= B_ERROR; 10283 else if (rp->r_flags & R4OUTOFSPACE) { 10284 mutex_enter(&rp->r_statelock); 10285 rp->r_flags &= ~R4OUTOFSPACE; 10286 mutex_exit(&rp->r_statelock); 10287 } 10288 pvn_write_done(pp, flags); 10289 if (freemem < desfree) 10290 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10291 NFS4_WRITE_NOWAIT); 10292 } 10293 10294 return (error); 10295 } 10296 10297 #ifdef DEBUG 10298 int nfs4_force_open_before_mmap = 0; 10299 #endif 10300 10301 /* ARGSUSED */ 10302 static int 10303 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10304 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10305 caller_context_t *ct) 10306 { 10307 struct segvn_crargs vn_a; 10308 int error = 0; 10309 rnode4_t *rp = VTOR4(vp); 10310 mntinfo4_t *mi = VTOMI4(vp); 10311 10312 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10313 return (EIO); 10314 10315 if (vp->v_flag & VNOMAP) 10316 return (ENOSYS); 10317 10318 if (off < 0 || (off + len) < 0) 10319 return (ENXIO); 10320 10321 if (vp->v_type != VREG) 10322 return (ENODEV); 10323 10324 /* 10325 * If the file is delegated to the client don't do anything. 10326 * If the file is not delegated, then validate the data cache. 10327 */ 10328 mutex_enter(&rp->r_statev4_lock); 10329 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10330 mutex_exit(&rp->r_statev4_lock); 10331 error = nfs4_validate_caches(vp, cr); 10332 if (error) 10333 return (error); 10334 } else { 10335 mutex_exit(&rp->r_statev4_lock); 10336 } 10337 10338 /* 10339 * Check to see if the vnode is currently marked as not cachable. 10340 * This means portions of the file are locked (through VOP_FRLOCK). 10341 * In this case the map request must be refused. We use 10342 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10343 */ 10344 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 10345 return (EINTR); 10346 10347 if (vp->v_flag & VNOCACHE) { 10348 error = EAGAIN; 10349 goto done; 10350 } 10351 10352 /* 10353 * Don't allow concurrent locks and mapping if mandatory locking is 10354 * enabled. 10355 */ 10356 if (flk_has_remote_locks(vp)) { 10357 struct vattr va; 10358 va.va_mask = AT_MODE; 10359 error = nfs4getattr(vp, &va, cr); 10360 if (error != 0) 10361 goto done; 10362 if (MANDLOCK(vp, va.va_mode)) { 10363 error = EAGAIN; 10364 goto done; 10365 } 10366 } 10367 10368 /* 10369 * It is possible that the rnode has a lost lock request that we 10370 * are still trying to recover, and that the request conflicts with 10371 * this map request. 10372 * 10373 * An alternative approach would be for nfs4_safemap() to consider 10374 * queued lock requests when deciding whether to set or clear 10375 * VNOCACHE. This would require the frlock code path to call 10376 * nfs4_safemap() after enqueing a lost request. 10377 */ 10378 if (nfs4_map_lost_lock_conflict(vp)) { 10379 error = EAGAIN; 10380 goto done; 10381 } 10382 10383 as_rangelock(as); 10384 if (!(flags & MAP_FIXED)) { 10385 map_addr(addrp, len, off, 1, flags); 10386 if (*addrp == NULL) { 10387 as_rangeunlock(as); 10388 error = ENOMEM; 10389 goto done; 10390 } 10391 } else { 10392 /* 10393 * User specified address - blow away any previous mappings 10394 */ 10395 (void) as_unmap(as, *addrp, len); 10396 } 10397 10398 if (vp->v_type == VREG) { 10399 /* 10400 * We need to retrieve the open stream 10401 */ 10402 nfs4_open_stream_t *osp = NULL; 10403 nfs4_open_owner_t *oop = NULL; 10404 10405 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10406 if (oop != NULL) { 10407 /* returns with 'os_sync_lock' held */ 10408 osp = find_open_stream(oop, rp); 10409 open_owner_rele(oop); 10410 } 10411 if (osp == NULL) { 10412 #ifdef DEBUG 10413 if (nfs4_force_open_before_mmap) { 10414 error = EIO; 10415 goto done; 10416 } 10417 #endif 10418 /* returns with 'os_sync_lock' held */ 10419 error = open_and_get_osp(vp, cr, &osp); 10420 if (osp == NULL) { 10421 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10422 "nfs4_map: we tried to OPEN the file " 10423 "but again no osp, so fail with EIO")); 10424 goto done; 10425 } 10426 } 10427 10428 if (osp->os_failed_reopen) { 10429 mutex_exit(&osp->os_sync_lock); 10430 open_stream_rele(osp, rp); 10431 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10432 "nfs4_map: os_failed_reopen set on " 10433 "osp %p, cr %p, rp %s", (void *)osp, 10434 (void *)cr, rnode4info(rp))); 10435 error = EIO; 10436 goto done; 10437 } 10438 mutex_exit(&osp->os_sync_lock); 10439 open_stream_rele(osp, rp); 10440 } 10441 10442 vn_a.vp = vp; 10443 vn_a.offset = off; 10444 vn_a.type = (flags & MAP_TYPE); 10445 vn_a.prot = (uchar_t)prot; 10446 vn_a.maxprot = (uchar_t)maxprot; 10447 vn_a.flags = (flags & ~MAP_TYPE); 10448 vn_a.cred = cr; 10449 vn_a.amp = NULL; 10450 vn_a.szc = 0; 10451 vn_a.lgrp_mem_policy_flags = 0; 10452 10453 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10454 as_rangeunlock(as); 10455 10456 done: 10457 nfs_rw_exit(&rp->r_lkserlock); 10458 return (error); 10459 } 10460 10461 /* 10462 * We're most likely dealing with a kernel module that likes to READ 10463 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10464 * officially OPEN the file to create the necessary client state 10465 * for bookkeeping of os_mmap_read/write counts. 10466 * 10467 * Since VOP_MAP only passes in a pointer to the vnode rather than 10468 * a double pointer, we can't handle the case where nfs4open_otw() 10469 * returns a different vnode than the one passed into VOP_MAP (since 10470 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10471 * we return NULL and let nfs4_map() fail. Note: the only case where 10472 * this should happen is if the file got removed and replaced with the 10473 * same name on the server (in addition to the fact that we're trying 10474 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10475 */ 10476 static int 10477 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10478 { 10479 rnode4_t *rp, *drp; 10480 vnode_t *dvp, *open_vp; 10481 char file_name[MAXNAMELEN]; 10482 int just_created; 10483 nfs4_open_stream_t *osp; 10484 nfs4_open_owner_t *oop; 10485 int error; 10486 10487 *ospp = NULL; 10488 open_vp = map_vp; 10489 10490 rp = VTOR4(open_vp); 10491 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10492 return (error); 10493 drp = VTOR4(dvp); 10494 10495 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10496 VN_RELE(dvp); 10497 return (EINTR); 10498 } 10499 10500 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10501 nfs_rw_exit(&drp->r_rwlock); 10502 VN_RELE(dvp); 10503 return (error); 10504 } 10505 10506 mutex_enter(&rp->r_statev4_lock); 10507 if (rp->created_v4) { 10508 rp->created_v4 = 0; 10509 mutex_exit(&rp->r_statev4_lock); 10510 10511 dnlc_update(dvp, file_name, open_vp); 10512 /* This is needed so we don't bump the open ref count */ 10513 just_created = 1; 10514 } else { 10515 mutex_exit(&rp->r_statev4_lock); 10516 just_created = 0; 10517 } 10518 10519 VN_HOLD(map_vp); 10520 10521 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10522 just_created); 10523 if (error) { 10524 nfs_rw_exit(&drp->r_rwlock); 10525 VN_RELE(dvp); 10526 VN_RELE(map_vp); 10527 return (error); 10528 } 10529 10530 nfs_rw_exit(&drp->r_rwlock); 10531 VN_RELE(dvp); 10532 10533 /* 10534 * If nfs4open_otw() returned a different vnode then "undo" 10535 * the open and return failure to the caller. 10536 */ 10537 if (!VN_CMP(open_vp, map_vp)) { 10538 nfs4_error_t e; 10539 10540 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10541 "open returned a different vnode")); 10542 /* 10543 * If there's an error, ignore it, 10544 * and let VOP_INACTIVE handle it. 10545 */ 10546 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10547 CLOSE_NORM, 0, 0, 0); 10548 VN_RELE(map_vp); 10549 return (EIO); 10550 } 10551 10552 VN_RELE(map_vp); 10553 10554 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10555 if (!oop) { 10556 nfs4_error_t e; 10557 10558 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10559 "no open owner")); 10560 /* 10561 * If there's an error, ignore it, 10562 * and let VOP_INACTIVE handle it. 10563 */ 10564 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10565 CLOSE_NORM, 0, 0, 0); 10566 return (EIO); 10567 } 10568 osp = find_open_stream(oop, rp); 10569 open_owner_rele(oop); 10570 *ospp = osp; 10571 return (0); 10572 } 10573 10574 /* 10575 * Please be aware that when this function is called, the address space write 10576 * a_lock is held. Do not put over the wire calls in this function. 10577 */ 10578 /* ARGSUSED */ 10579 static int 10580 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10581 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10582 caller_context_t *ct) 10583 { 10584 rnode4_t *rp; 10585 int error = 0; 10586 mntinfo4_t *mi; 10587 10588 mi = VTOMI4(vp); 10589 rp = VTOR4(vp); 10590 10591 if (nfs_zone() != mi->mi_zone) 10592 return (EIO); 10593 if (vp->v_flag & VNOMAP) 10594 return (ENOSYS); 10595 10596 /* 10597 * Need to hold rwlock while incrementing the mapcnt so that 10598 * mmap'ing can be serialized with writes so that the caching 10599 * can be handled correctly. 10600 * 10601 * Don't need to update the open stream first, since this 10602 * mmap can't add any additional share access that isn't 10603 * already contained in the open stream (for the case where we 10604 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10605 * take into account os_mmap_read[write] counts). 10606 */ 10607 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp))) 10608 return (EINTR); 10609 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10610 nfs_rw_exit(&rp->r_rwlock); 10611 10612 if (vp->v_type == VREG) { 10613 /* 10614 * We need to retrieve the open stream and update the counts. 10615 * If there is no open stream here, something is wrong. 10616 */ 10617 nfs4_open_stream_t *osp = NULL; 10618 nfs4_open_owner_t *oop = NULL; 10619 10620 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10621 if (oop != NULL) { 10622 /* returns with 'os_sync_lock' held */ 10623 osp = find_open_stream(oop, rp); 10624 open_owner_rele(oop); 10625 } 10626 if (osp == NULL) { 10627 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10628 "nfs4_addmap: we should have an osp" 10629 "but we don't, so fail with EIO")); 10630 error = EIO; 10631 goto out; 10632 } 10633 10634 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10635 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10636 10637 /* 10638 * Update the map count in the open stream. 10639 * This is necessary in the case where we 10640 * open/mmap/close/, then the server reboots, and we 10641 * attempt to reopen. If the mmap doesn't add share 10642 * access then we send an invalid reopen with 10643 * access = NONE. 10644 * 10645 * We need to specifically check each PROT_* so a mmap 10646 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10647 * read and write access. A simple comparison of prot 10648 * to ~PROT_WRITE to determine read access is insufficient 10649 * since prot can be |= with PROT_USER, etc. 10650 */ 10651 10652 /* 10653 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10654 */ 10655 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10656 osp->os_mmap_write += btopr(len); 10657 if (maxprot & PROT_READ) 10658 osp->os_mmap_read += btopr(len); 10659 if (maxprot & PROT_EXEC) 10660 osp->os_mmap_read += btopr(len); 10661 /* 10662 * Ensure that os_mmap_read gets incremented, even if 10663 * maxprot were to look like PROT_NONE. 10664 */ 10665 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10666 !(maxprot & PROT_EXEC)) 10667 osp->os_mmap_read += btopr(len); 10668 osp->os_mapcnt += btopr(len); 10669 mutex_exit(&osp->os_sync_lock); 10670 open_stream_rele(osp, rp); 10671 } 10672 10673 out: 10674 /* 10675 * If we got an error, then undo our 10676 * incrementing of 'r_mapcnt'. 10677 */ 10678 10679 if (error) { 10680 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10681 ASSERT(rp->r_mapcnt >= 0); 10682 } 10683 return (error); 10684 } 10685 10686 /* ARGSUSED */ 10687 static int 10688 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct) 10689 { 10690 10691 return (VTOR4(vp1) == VTOR4(vp2)); 10692 } 10693 10694 /* ARGSUSED */ 10695 static int 10696 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10697 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr, 10698 caller_context_t *ct) 10699 { 10700 int rc; 10701 u_offset_t start, end; 10702 rnode4_t *rp; 10703 int error = 0, intr = INTR4(vp); 10704 nfs4_error_t e; 10705 10706 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10707 return (EIO); 10708 10709 /* check for valid cmd parameter */ 10710 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10711 return (EINVAL); 10712 10713 /* Verify l_type. */ 10714 switch (bfp->l_type) { 10715 case F_RDLCK: 10716 if (cmd != F_GETLK && !(flag & FREAD)) 10717 return (EBADF); 10718 break; 10719 case F_WRLCK: 10720 if (cmd != F_GETLK && !(flag & FWRITE)) 10721 return (EBADF); 10722 break; 10723 case F_UNLCK: 10724 intr = 0; 10725 break; 10726 10727 default: 10728 return (EINVAL); 10729 } 10730 10731 /* check the validity of the lock range */ 10732 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10733 return (rc); 10734 if (rc = flk_check_lock_data(start, end, MAXEND)) 10735 return (rc); 10736 10737 /* 10738 * If the filesystem is mounted using local locking, pass the 10739 * request off to the local locking code. 10740 */ 10741 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10742 if (cmd == F_SETLK || cmd == F_SETLKW) { 10743 /* 10744 * For complete safety, we should be holding 10745 * r_lkserlock. However, we can't call 10746 * nfs4_safelock and then fs_frlock while 10747 * holding r_lkserlock, so just invoke 10748 * nfs4_safelock and expect that this will 10749 * catch enough of the cases. 10750 */ 10751 if (!nfs4_safelock(vp, bfp, cr)) 10752 return (EAGAIN); 10753 } 10754 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 10755 } 10756 10757 rp = VTOR4(vp); 10758 10759 /* 10760 * Check whether the given lock request can proceed, given the 10761 * current file mappings. 10762 */ 10763 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10764 return (EINTR); 10765 if (cmd == F_SETLK || cmd == F_SETLKW) { 10766 if (!nfs4_safelock(vp, bfp, cr)) { 10767 rc = EAGAIN; 10768 goto done; 10769 } 10770 } 10771 10772 /* 10773 * Flush the cache after waiting for async I/O to finish. For new 10774 * locks, this is so that the process gets the latest bits from the 10775 * server. For unlocks, this is so that other clients see the 10776 * latest bits once the file has been unlocked. If currently dirty 10777 * pages can't be flushed, then don't allow a lock to be set. But 10778 * allow unlocks to succeed, to avoid having orphan locks on the 10779 * server. 10780 */ 10781 if (cmd != F_GETLK) { 10782 mutex_enter(&rp->r_statelock); 10783 while (rp->r_count > 0) { 10784 if (intr) { 10785 klwp_t *lwp = ttolwp(curthread); 10786 10787 if (lwp != NULL) 10788 lwp->lwp_nostop++; 10789 if (cv_wait_sig(&rp->r_cv, 10790 &rp->r_statelock) == 0) { 10791 if (lwp != NULL) 10792 lwp->lwp_nostop--; 10793 rc = EINTR; 10794 break; 10795 } 10796 if (lwp != NULL) 10797 lwp->lwp_nostop--; 10798 } else 10799 cv_wait(&rp->r_cv, &rp->r_statelock); 10800 } 10801 mutex_exit(&rp->r_statelock); 10802 if (rc != 0) 10803 goto done; 10804 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct); 10805 if (error) { 10806 if (error == ENOSPC || error == EDQUOT) { 10807 mutex_enter(&rp->r_statelock); 10808 if (!rp->r_error) 10809 rp->r_error = error; 10810 mutex_exit(&rp->r_statelock); 10811 } 10812 if (bfp->l_type != F_UNLCK) { 10813 rc = ENOLCK; 10814 goto done; 10815 } 10816 } 10817 } 10818 10819 /* 10820 * Call the lock manager to do the real work of contacting 10821 * the server and obtaining the lock. 10822 */ 10823 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10824 cr, &e, NULL, NULL); 10825 rc = e.error; 10826 10827 if (rc == 0) 10828 nfs4_lockcompletion(vp, cmd); 10829 10830 done: 10831 nfs_rw_exit(&rp->r_lkserlock); 10832 10833 return (rc); 10834 } 10835 10836 /* 10837 * Free storage space associated with the specified vnode. The portion 10838 * to be freed is specified by bfp->l_start and bfp->l_len (already 10839 * normalized to a "whence" of 0). 10840 * 10841 * This is an experimental facility whose continued existence is not 10842 * guaranteed. Currently, we only support the special case 10843 * of l_len == 0, meaning free to end of file. 10844 */ 10845 /* ARGSUSED */ 10846 static int 10847 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10848 offset_t offset, cred_t *cr, caller_context_t *ct) 10849 { 10850 int error; 10851 10852 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10853 return (EIO); 10854 ASSERT(vp->v_type == VREG); 10855 if (cmd != F_FREESP) 10856 return (EINVAL); 10857 10858 error = convoff(vp, bfp, 0, offset); 10859 if (!error) { 10860 ASSERT(bfp->l_start >= 0); 10861 if (bfp->l_len == 0) { 10862 struct vattr va; 10863 10864 va.va_mask = AT_SIZE; 10865 va.va_size = bfp->l_start; 10866 error = nfs4setattr(vp, &va, 0, cr, NULL); 10867 } else 10868 error = EINVAL; 10869 } 10870 10871 return (error); 10872 } 10873 10874 /* ARGSUSED */ 10875 int 10876 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 10877 { 10878 rnode4_t *rp; 10879 rp = VTOR4(vp); 10880 10881 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 10882 vp = RTOV4(rp); 10883 } 10884 *vpp = vp; 10885 return (0); 10886 } 10887 10888 /* 10889 * Setup and add an address space callback to do the work of the delmap call. 10890 * The callback will (and must be) deleted in the actual callback function. 10891 * 10892 * This is done in order to take care of the problem that we have with holding 10893 * the address space's a_lock for a long period of time (e.g. if the NFS server 10894 * is down). Callbacks will be executed in the address space code while the 10895 * a_lock is not held. Holding the address space's a_lock causes things such 10896 * as ps and fork to hang because they are trying to acquire this lock as well. 10897 */ 10898 /* ARGSUSED */ 10899 static int 10900 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10901 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 10902 caller_context_t *ct) 10903 { 10904 int caller_found; 10905 int error; 10906 rnode4_t *rp; 10907 nfs4_delmap_args_t *dmapp; 10908 nfs4_delmapcall_t *delmap_call; 10909 10910 if (vp->v_flag & VNOMAP) 10911 return (ENOSYS); 10912 10913 /* 10914 * A process may not change zones if it has NFS pages mmap'ed 10915 * in, so we can't legitimately get here from the wrong zone. 10916 */ 10917 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10918 10919 rp = VTOR4(vp); 10920 10921 /* 10922 * The way that the address space of this process deletes its mapping 10923 * of this file is via the following call chains: 10924 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10925 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10926 * 10927 * With the use of address space callbacks we are allowed to drop the 10928 * address space lock, a_lock, while executing the NFS operations that 10929 * need to go over the wire. Returning EAGAIN to the caller of this 10930 * function is what drives the execution of the callback that we add 10931 * below. The callback will be executed by the address space code 10932 * after dropping the a_lock. When the callback is finished, since 10933 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 10934 * is called again on the same segment to finish the rest of the work 10935 * that needs to happen during unmapping. 10936 * 10937 * This action of calling back into the segment driver causes 10938 * nfs4_delmap() to get called again, but since the callback was 10939 * already executed at this point, it already did the work and there 10940 * is nothing left for us to do. 10941 * 10942 * To Summarize: 10943 * - The first time nfs4_delmap is called by the current thread is when 10944 * we add the caller associated with this delmap to the delmap caller 10945 * list, add the callback, and return EAGAIN. 10946 * - The second time in this call chain when nfs4_delmap is called we 10947 * will find this caller in the delmap caller list and realize there 10948 * is no more work to do thus removing this caller from the list and 10949 * returning the error that was set in the callback execution. 10950 */ 10951 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 10952 if (caller_found) { 10953 /* 10954 * 'error' is from the actual delmap operations. To avoid 10955 * hangs, we need to handle the return of EAGAIN differently 10956 * since this is what drives the callback execution. 10957 * In this case, we don't want to return EAGAIN and do the 10958 * callback execution because there are none to execute. 10959 */ 10960 if (error == EAGAIN) 10961 return (0); 10962 else 10963 return (error); 10964 } 10965 10966 /* current caller was not in the list */ 10967 delmap_call = nfs4_init_delmapcall(); 10968 10969 mutex_enter(&rp->r_statelock); 10970 list_insert_tail(&rp->r_indelmap, delmap_call); 10971 mutex_exit(&rp->r_statelock); 10972 10973 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 10974 10975 dmapp->vp = vp; 10976 dmapp->off = off; 10977 dmapp->addr = addr; 10978 dmapp->len = len; 10979 dmapp->prot = prot; 10980 dmapp->maxprot = maxprot; 10981 dmapp->flags = flags; 10982 dmapp->cr = cr; 10983 dmapp->caller = delmap_call; 10984 10985 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 10986 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 10987 10988 return (error ? error : EAGAIN); 10989 } 10990 10991 static nfs4_delmapcall_t * 10992 nfs4_init_delmapcall() 10993 { 10994 nfs4_delmapcall_t *delmap_call; 10995 10996 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 10997 delmap_call->call_id = curthread; 10998 delmap_call->error = 0; 10999 11000 return (delmap_call); 11001 } 11002 11003 static void 11004 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 11005 { 11006 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 11007 } 11008 11009 /* 11010 * Searches for the current delmap caller (based on curthread) in the list of 11011 * callers. If it is found, we remove it and free the delmap caller. 11012 * Returns: 11013 * 0 if the caller wasn't found 11014 * 1 if the caller was found, removed and freed. *errp will be set 11015 * to what the result of the delmap was. 11016 */ 11017 static int 11018 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 11019 { 11020 nfs4_delmapcall_t *delmap_call; 11021 11022 /* 11023 * If the list doesn't exist yet, we create it and return 11024 * that the caller wasn't found. No list = no callers. 11025 */ 11026 mutex_enter(&rp->r_statelock); 11027 if (!(rp->r_flags & R4DELMAPLIST)) { 11028 /* The list does not exist */ 11029 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 11030 offsetof(nfs4_delmapcall_t, call_node)); 11031 rp->r_flags |= R4DELMAPLIST; 11032 mutex_exit(&rp->r_statelock); 11033 return (0); 11034 } else { 11035 /* The list exists so search it */ 11036 for (delmap_call = list_head(&rp->r_indelmap); 11037 delmap_call != NULL; 11038 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 11039 if (delmap_call->call_id == curthread) { 11040 /* current caller is in the list */ 11041 *errp = delmap_call->error; 11042 list_remove(&rp->r_indelmap, delmap_call); 11043 mutex_exit(&rp->r_statelock); 11044 nfs4_free_delmapcall(delmap_call); 11045 return (1); 11046 } 11047 } 11048 } 11049 mutex_exit(&rp->r_statelock); 11050 return (0); 11051 } 11052 11053 /* 11054 * Remove some pages from an mmap'd vnode. Just update the 11055 * count of pages. If doing close-to-open, then flush and 11056 * commit all of the pages associated with this file. 11057 * Otherwise, start an asynchronous page flush to write out 11058 * any dirty pages. This will also associate a credential 11059 * with the rnode which can be used to write the pages. 11060 */ 11061 /* ARGSUSED */ 11062 static void 11063 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11064 { 11065 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11066 rnode4_t *rp; 11067 mntinfo4_t *mi; 11068 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11069 11070 rp = VTOR4(dmapp->vp); 11071 mi = VTOMI4(dmapp->vp); 11072 11073 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11074 ASSERT(rp->r_mapcnt >= 0); 11075 11076 /* 11077 * Initiate a page flush and potential commit if there are 11078 * pages, the file system was not mounted readonly, the segment 11079 * was mapped shared, and the pages themselves were writeable. 11080 */ 11081 if (nfs4_has_pages(dmapp->vp) && 11082 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11083 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11084 mutex_enter(&rp->r_statelock); 11085 rp->r_flags |= R4DIRTY; 11086 mutex_exit(&rp->r_statelock); 11087 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11088 dmapp->len, dmapp->cr); 11089 if (!e.error) { 11090 mutex_enter(&rp->r_statelock); 11091 e.error = rp->r_error; 11092 rp->r_error = 0; 11093 mutex_exit(&rp->r_statelock); 11094 } 11095 } else 11096 e.error = 0; 11097 11098 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11099 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11100 B_INVAL, dmapp->cr, NULL); 11101 11102 if (e.error) { 11103 e.stat = puterrno4(e.error); 11104 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11105 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11106 dmapp->caller->error = e.error; 11107 } 11108 11109 /* Check to see if we need to close the file */ 11110 11111 if (dmapp->vp->v_type == VREG) { 11112 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11113 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11114 11115 if (e.error != 0 || e.stat != NFS4_OK) { 11116 /* 11117 * Since it is possible that e.error == 0 and 11118 * e.stat != NFS4_OK (and vice versa), 11119 * we do the proper checking in order to get both 11120 * e.error and e.stat reporting the correct info. 11121 */ 11122 if (e.stat == NFS4_OK) 11123 e.stat = puterrno4(e.error); 11124 if (e.error == 0) 11125 e.error = geterrno4(e.stat); 11126 11127 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11128 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11129 dmapp->caller->error = e.error; 11130 } 11131 } 11132 11133 (void) as_delete_callback(as, arg); 11134 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11135 } 11136 11137 11138 static uint_t 11139 fattr4_maxfilesize_to_bits(uint64_t ll) 11140 { 11141 uint_t l = 1; 11142 11143 if (ll == 0) { 11144 return (0); 11145 } 11146 11147 if (ll & 0xffffffff00000000) { 11148 l += 32; ll >>= 32; 11149 } 11150 if (ll & 0xffff0000) { 11151 l += 16; ll >>= 16; 11152 } 11153 if (ll & 0xff00) { 11154 l += 8; ll >>= 8; 11155 } 11156 if (ll & 0xf0) { 11157 l += 4; ll >>= 4; 11158 } 11159 if (ll & 0xc) { 11160 l += 2; ll >>= 2; 11161 } 11162 if (ll & 0x2) { 11163 l += 1; 11164 } 11165 return (l); 11166 } 11167 11168 /* ARGSUSED */ 11169 int 11170 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 11171 caller_context_t *ct) 11172 { 11173 int error; 11174 hrtime_t t; 11175 rnode4_t *rp; 11176 nfs4_ga_res_t gar; 11177 nfs4_ga_ext_res_t ger; 11178 11179 gar.n4g_ext_res = &ger; 11180 11181 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11182 return (EIO); 11183 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11184 *valp = MAXPATHLEN; 11185 return (0); 11186 } 11187 if (cmd == _PC_ACL_ENABLED) { 11188 *valp = _ACL_ACE_ENABLED; 11189 return (0); 11190 } 11191 11192 rp = VTOR4(vp); 11193 if (cmd == _PC_XATTR_EXISTS) { 11194 /* 11195 * Eventually should attempt small client readdir before 11196 * going otw with GETATTR(FATTR4_NAMED_ATTR). For now 11197 * just drive the OTW getattr. This is required because 11198 * _PC_XATTR_EXISTS can only return true if attributes 11199 * exist -- simply checking for existence of the attrdir 11200 * is not sufficient. 11201 * 11202 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11203 * is NULL. Once the xadir vp exists, we can create xattrs, 11204 * and we don't have any way to update the "base" object's 11205 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11206 * could help out. 11207 */ 11208 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11209 rp->r_xattr_dir == NULL) { 11210 *valp = rp->r_pathconf.pc4_xattr_exists; 11211 return (0); 11212 } 11213 } else { /* OLD CODE */ 11214 if (ATTRCACHE4_VALID(vp)) { 11215 mutex_enter(&rp->r_statelock); 11216 if (rp->r_pathconf.pc4_cache_valid) { 11217 error = 0; 11218 switch (cmd) { 11219 case _PC_FILESIZEBITS: 11220 *valp = 11221 rp->r_pathconf.pc4_filesizebits; 11222 break; 11223 case _PC_LINK_MAX: 11224 *valp = 11225 rp->r_pathconf.pc4_link_max; 11226 break; 11227 case _PC_NAME_MAX: 11228 *valp = 11229 rp->r_pathconf.pc4_name_max; 11230 break; 11231 case _PC_CHOWN_RESTRICTED: 11232 *valp = 11233 rp->r_pathconf.pc4_chown_restricted; 11234 break; 11235 case _PC_NO_TRUNC: 11236 *valp = 11237 rp->r_pathconf.pc4_no_trunc; 11238 break; 11239 default: 11240 error = EINVAL; 11241 break; 11242 } 11243 mutex_exit(&rp->r_statelock); 11244 #ifdef DEBUG 11245 nfs4_pathconf_cache_hits++; 11246 #endif 11247 return (error); 11248 } 11249 mutex_exit(&rp->r_statelock); 11250 } 11251 } 11252 #ifdef DEBUG 11253 nfs4_pathconf_cache_misses++; 11254 #endif 11255 11256 t = gethrtime(); 11257 11258 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11259 11260 if (error) { 11261 mutex_enter(&rp->r_statelock); 11262 rp->r_pathconf.pc4_cache_valid = FALSE; 11263 rp->r_pathconf.pc4_xattr_valid = FALSE; 11264 mutex_exit(&rp->r_statelock); 11265 return (error); 11266 } 11267 11268 /* interpret the max filesize */ 11269 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11270 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11271 11272 /* Store the attributes we just received */ 11273 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11274 11275 switch (cmd) { 11276 case _PC_FILESIZEBITS: 11277 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11278 break; 11279 case _PC_LINK_MAX: 11280 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11281 break; 11282 case _PC_NAME_MAX: 11283 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11284 break; 11285 case _PC_CHOWN_RESTRICTED: 11286 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11287 break; 11288 case _PC_NO_TRUNC: 11289 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11290 break; 11291 case _PC_XATTR_EXISTS: 11292 *valp = gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists; 11293 break; 11294 default: 11295 return (EINVAL); 11296 } 11297 11298 return (0); 11299 } 11300 11301 /* 11302 * Called by async thread to do synchronous pageio. Do the i/o, wait 11303 * for it to complete, and cleanup the page list when done. 11304 */ 11305 static int 11306 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11307 int flags, cred_t *cr) 11308 { 11309 int error; 11310 11311 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11312 11313 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11314 if (flags & B_READ) 11315 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11316 else 11317 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11318 return (error); 11319 } 11320 11321 /* ARGSUSED */ 11322 static int 11323 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11324 int flags, cred_t *cr, caller_context_t *ct) 11325 { 11326 int error; 11327 rnode4_t *rp; 11328 11329 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11330 return (EIO); 11331 11332 if (pp == NULL) 11333 return (EINVAL); 11334 11335 rp = VTOR4(vp); 11336 mutex_enter(&rp->r_statelock); 11337 rp->r_count++; 11338 mutex_exit(&rp->r_statelock); 11339 11340 if (flags & B_ASYNC) { 11341 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11342 nfs4_sync_pageio); 11343 } else 11344 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11345 mutex_enter(&rp->r_statelock); 11346 rp->r_count--; 11347 cv_broadcast(&rp->r_cv); 11348 mutex_exit(&rp->r_statelock); 11349 return (error); 11350 } 11351 11352 /* ARGSUSED */ 11353 static void 11354 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr, 11355 caller_context_t *ct) 11356 { 11357 int error; 11358 rnode4_t *rp; 11359 page_t *plist; 11360 page_t *pptr; 11361 offset3 offset; 11362 count3 len; 11363 k_sigset_t smask; 11364 11365 /* 11366 * We should get called with fl equal to either B_FREE or 11367 * B_INVAL. Any other value is illegal. 11368 * 11369 * The page that we are either supposed to free or destroy 11370 * should be exclusive locked and its io lock should not 11371 * be held. 11372 */ 11373 ASSERT(fl == B_FREE || fl == B_INVAL); 11374 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11375 11376 rp = VTOR4(vp); 11377 11378 /* 11379 * If the page doesn't need to be committed or we shouldn't 11380 * even bother attempting to commit it, then just make sure 11381 * that the p_fsdata byte is clear and then either free or 11382 * destroy the page as appropriate. 11383 */ 11384 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11385 pp->p_fsdata = C_NOCOMMIT; 11386 if (fl == B_FREE) 11387 page_free(pp, dn); 11388 else 11389 page_destroy(pp, dn); 11390 return; 11391 } 11392 11393 /* 11394 * If there is a page invalidation operation going on, then 11395 * if this is one of the pages being destroyed, then just 11396 * clear the p_fsdata byte and then either free or destroy 11397 * the page as appropriate. 11398 */ 11399 mutex_enter(&rp->r_statelock); 11400 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11401 mutex_exit(&rp->r_statelock); 11402 pp->p_fsdata = C_NOCOMMIT; 11403 if (fl == B_FREE) 11404 page_free(pp, dn); 11405 else 11406 page_destroy(pp, dn); 11407 return; 11408 } 11409 11410 /* 11411 * If we are freeing this page and someone else is already 11412 * waiting to do a commit, then just unlock the page and 11413 * return. That other thread will take care of commiting 11414 * this page. The page can be freed sometime after the 11415 * commit has finished. Otherwise, if the page is marked 11416 * as delay commit, then we may be getting called from 11417 * pvn_write_done, one page at a time. This could result 11418 * in one commit per page, so we end up doing lots of small 11419 * commits instead of fewer larger commits. This is bad, 11420 * we want do as few commits as possible. 11421 */ 11422 if (fl == B_FREE) { 11423 if (rp->r_flags & R4COMMITWAIT) { 11424 page_unlock(pp); 11425 mutex_exit(&rp->r_statelock); 11426 return; 11427 } 11428 if (pp->p_fsdata == C_DELAYCOMMIT) { 11429 pp->p_fsdata = C_COMMIT; 11430 page_unlock(pp); 11431 mutex_exit(&rp->r_statelock); 11432 return; 11433 } 11434 } 11435 11436 /* 11437 * Check to see if there is a signal which would prevent an 11438 * attempt to commit the pages from being successful. If so, 11439 * then don't bother with all of the work to gather pages and 11440 * generate the unsuccessful RPC. Just return from here and 11441 * let the page be committed at some later time. 11442 */ 11443 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11444 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11445 sigunintr(&smask); 11446 page_unlock(pp); 11447 mutex_exit(&rp->r_statelock); 11448 return; 11449 } 11450 sigunintr(&smask); 11451 11452 /* 11453 * We are starting to need to commit pages, so let's try 11454 * to commit as many as possible at once to reduce the 11455 * overhead. 11456 * 11457 * Set the `commit inprogress' state bit. We must 11458 * first wait until any current one finishes. Then 11459 * we initialize the c_pages list with this page. 11460 */ 11461 while (rp->r_flags & R4COMMIT) { 11462 rp->r_flags |= R4COMMITWAIT; 11463 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11464 rp->r_flags &= ~R4COMMITWAIT; 11465 } 11466 rp->r_flags |= R4COMMIT; 11467 mutex_exit(&rp->r_statelock); 11468 ASSERT(rp->r_commit.c_pages == NULL); 11469 rp->r_commit.c_pages = pp; 11470 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11471 rp->r_commit.c_commlen = PAGESIZE; 11472 11473 /* 11474 * Gather together all other pages which can be committed. 11475 * They will all be chained off r_commit.c_pages. 11476 */ 11477 nfs4_get_commit(vp); 11478 11479 /* 11480 * Clear the `commit inprogress' status and disconnect 11481 * the list of pages to be committed from the rnode. 11482 * At this same time, we also save the starting offset 11483 * and length of data to be committed on the server. 11484 */ 11485 plist = rp->r_commit.c_pages; 11486 rp->r_commit.c_pages = NULL; 11487 offset = rp->r_commit.c_commbase; 11488 len = rp->r_commit.c_commlen; 11489 mutex_enter(&rp->r_statelock); 11490 rp->r_flags &= ~R4COMMIT; 11491 cv_broadcast(&rp->r_commit.c_cv); 11492 mutex_exit(&rp->r_statelock); 11493 11494 if (curproc == proc_pageout || curproc == proc_fsflush || 11495 nfs_zone() != VTOMI4(vp)->mi_zone) { 11496 nfs4_async_commit(vp, plist, offset, len, 11497 cr, do_nfs4_async_commit); 11498 return; 11499 } 11500 11501 /* 11502 * Actually generate the COMMIT op over the wire operation. 11503 */ 11504 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11505 11506 /* 11507 * If we got an error during the commit, just unlock all 11508 * of the pages. The pages will get retransmitted to the 11509 * server during a putpage operation. 11510 */ 11511 if (error) { 11512 while (plist != NULL) { 11513 pptr = plist; 11514 page_sub(&plist, pptr); 11515 page_unlock(pptr); 11516 } 11517 return; 11518 } 11519 11520 /* 11521 * We've tried as hard as we can to commit the data to stable 11522 * storage on the server. We just unlock the rest of the pages 11523 * and clear the commit required state. They will be put 11524 * onto the tail of the cachelist if they are nolonger 11525 * mapped. 11526 */ 11527 while (plist != pp) { 11528 pptr = plist; 11529 page_sub(&plist, pptr); 11530 pptr->p_fsdata = C_NOCOMMIT; 11531 page_unlock(pptr); 11532 } 11533 11534 /* 11535 * It is possible that nfs4_commit didn't return error but 11536 * some other thread has modified the page we are going 11537 * to free/destroy. 11538 * In this case we need to rewrite the page. Do an explicit check 11539 * before attempting to free/destroy the page. If modified, needs to 11540 * be rewritten so unlock the page and return. 11541 */ 11542 if (hat_ismod(pp)) { 11543 pp->p_fsdata = C_NOCOMMIT; 11544 page_unlock(pp); 11545 return; 11546 } 11547 11548 /* 11549 * Now, as appropriate, either free or destroy the page 11550 * that we were called with. 11551 */ 11552 pp->p_fsdata = C_NOCOMMIT; 11553 if (fl == B_FREE) 11554 page_free(pp, dn); 11555 else 11556 page_destroy(pp, dn); 11557 } 11558 11559 /* 11560 * Commit requires that the current fh be the file written to. 11561 * The compound op structure is: 11562 * PUTFH(file), COMMIT 11563 */ 11564 static int 11565 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11566 { 11567 COMPOUND4args_clnt args; 11568 COMPOUND4res_clnt res; 11569 COMMIT4res *cm_res; 11570 nfs_argop4 argop[2]; 11571 nfs_resop4 *resop; 11572 int doqueue; 11573 mntinfo4_t *mi; 11574 rnode4_t *rp; 11575 cred_t *cred_otw = NULL; 11576 bool_t needrecov = FALSE; 11577 nfs4_recov_state_t recov_state; 11578 nfs4_open_stream_t *osp = NULL; 11579 bool_t first_time = TRUE; /* first time getting OTW cred */ 11580 bool_t last_time = FALSE; /* last time getting OTW cred */ 11581 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11582 11583 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11584 11585 rp = VTOR4(vp); 11586 11587 mi = VTOMI4(vp); 11588 recov_state.rs_flags = 0; 11589 recov_state.rs_num_retry_despite_err = 0; 11590 get_commit_cred: 11591 /* 11592 * Releases the osp, if a valid open stream is provided. 11593 * Puts a hold on the cred_otw and the new osp (if found). 11594 */ 11595 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11596 &first_time, &last_time); 11597 args.ctag = TAG_COMMIT; 11598 recov_retry: 11599 /* 11600 * Commit ops: putfh file; commit 11601 */ 11602 args.array_len = 2; 11603 args.array = argop; 11604 11605 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11606 &recov_state, NULL); 11607 if (e.error) { 11608 crfree(cred_otw); 11609 if (osp != NULL) 11610 open_stream_rele(osp, rp); 11611 return (e.error); 11612 } 11613 11614 /* putfh directory */ 11615 argop[0].argop = OP_CPUTFH; 11616 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11617 11618 /* commit */ 11619 argop[1].argop = OP_COMMIT; 11620 argop[1].nfs_argop4_u.opcommit.offset = offset; 11621 argop[1].nfs_argop4_u.opcommit.count = count; 11622 11623 doqueue = 1; 11624 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11625 11626 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11627 if (!needrecov && e.error) { 11628 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11629 needrecov); 11630 crfree(cred_otw); 11631 if (e.error == EACCES && last_time == FALSE) 11632 goto get_commit_cred; 11633 if (osp != NULL) 11634 open_stream_rele(osp, rp); 11635 return (e.error); 11636 } 11637 11638 if (needrecov) { 11639 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11640 NULL, OP_COMMIT, NULL) == FALSE) { 11641 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11642 &recov_state, needrecov); 11643 if (!e.error) 11644 (void) xdr_free(xdr_COMPOUND4res_clnt, 11645 (caddr_t)&res); 11646 goto recov_retry; 11647 } 11648 if (e.error) { 11649 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11650 &recov_state, needrecov); 11651 crfree(cred_otw); 11652 if (osp != NULL) 11653 open_stream_rele(osp, rp); 11654 return (e.error); 11655 } 11656 /* fall through for res.status case */ 11657 } 11658 11659 if (res.status) { 11660 e.error = geterrno4(res.status); 11661 if (e.error == EACCES && last_time == FALSE) { 11662 crfree(cred_otw); 11663 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11664 &recov_state, needrecov); 11665 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11666 goto get_commit_cred; 11667 } 11668 /* 11669 * Can't do a nfs4_purge_stale_fh here because this 11670 * can cause a deadlock. nfs4_commit can 11671 * be called from nfs4_dispose which can be called 11672 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11673 * can call back to pvn_vplist_dirty. 11674 */ 11675 if (e.error == ESTALE) { 11676 mutex_enter(&rp->r_statelock); 11677 rp->r_flags |= R4STALE; 11678 if (!rp->r_error) 11679 rp->r_error = e.error; 11680 mutex_exit(&rp->r_statelock); 11681 PURGE_ATTRCACHE4(vp); 11682 } else { 11683 mutex_enter(&rp->r_statelock); 11684 if (!rp->r_error) 11685 rp->r_error = e.error; 11686 mutex_exit(&rp->r_statelock); 11687 } 11688 } else { 11689 ASSERT(rp->r_flags & R4HAVEVERF); 11690 resop = &res.array[1]; /* commit res */ 11691 cm_res = &resop->nfs_resop4_u.opcommit; 11692 mutex_enter(&rp->r_statelock); 11693 if (cm_res->writeverf == rp->r_writeverf) { 11694 mutex_exit(&rp->r_statelock); 11695 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11696 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11697 &recov_state, needrecov); 11698 crfree(cred_otw); 11699 if (osp != NULL) 11700 open_stream_rele(osp, rp); 11701 return (0); 11702 } 11703 nfs4_set_mod(vp); 11704 rp->r_writeverf = cm_res->writeverf; 11705 mutex_exit(&rp->r_statelock); 11706 e.error = NFS_VERF_MISMATCH; 11707 } 11708 11709 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11710 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11711 crfree(cred_otw); 11712 if (osp != NULL) 11713 open_stream_rele(osp, rp); 11714 11715 return (e.error); 11716 } 11717 11718 static void 11719 nfs4_set_mod(vnode_t *vp) 11720 { 11721 page_t *pp; 11722 kmutex_t *vphm; 11723 rnode4_t *rp; 11724 11725 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11726 11727 /* make sure we're looking at the master vnode, not a shadow */ 11728 11729 rp = VTOR4(vp); 11730 if (IS_SHADOW(vp, rp)) 11731 vp = RTOV4(rp); 11732 11733 vphm = page_vnode_mutex(vp); 11734 mutex_enter(vphm); 11735 /* 11736 * If there are no pages associated with this vnode, then 11737 * just return. 11738 */ 11739 if ((pp = vp->v_pages) == NULL) { 11740 mutex_exit(vphm); 11741 return; 11742 } 11743 11744 do { 11745 if (pp->p_fsdata != C_NOCOMMIT) { 11746 hat_setmod(pp); 11747 pp->p_fsdata = C_NOCOMMIT; 11748 } 11749 } while ((pp = pp->p_vpnext) != vp->v_pages); 11750 mutex_exit(vphm); 11751 } 11752 11753 /* 11754 * This function is used to gather a page list of the pages which 11755 * can be committed on the server. 11756 * 11757 * The calling thread must have set R4COMMIT. This bit is used to 11758 * serialize access to the commit structure in the rnode. As long 11759 * as the thread has set R4COMMIT, then it can manipulate the commit 11760 * structure without requiring any other locks. 11761 * 11762 * When this function is called from nfs4_dispose() the page passed 11763 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11764 * will skip it. This is not a problem since we initially add the 11765 * page to the r_commit page list. 11766 * 11767 */ 11768 static void 11769 nfs4_get_commit(vnode_t *vp) 11770 { 11771 rnode4_t *rp; 11772 page_t *pp; 11773 kmutex_t *vphm; 11774 11775 rp = VTOR4(vp); 11776 11777 ASSERT(rp->r_flags & R4COMMIT); 11778 11779 /* make sure we're looking at the master vnode, not a shadow */ 11780 11781 if (IS_SHADOW(vp, rp)) 11782 vp = RTOV4(rp); 11783 11784 vphm = page_vnode_mutex(vp); 11785 mutex_enter(vphm); 11786 11787 /* 11788 * If there are no pages associated with this vnode, then 11789 * just return. 11790 */ 11791 if ((pp = vp->v_pages) == NULL) { 11792 mutex_exit(vphm); 11793 return; 11794 } 11795 11796 /* 11797 * Step through all of the pages associated with this vnode 11798 * looking for pages which need to be committed. 11799 */ 11800 do { 11801 /* 11802 * First short-cut everything (without the page_lock) 11803 * and see if this page does not need to be committed 11804 * or is modified if so then we'll just skip it. 11805 */ 11806 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11807 continue; 11808 11809 /* 11810 * Attempt to lock the page. If we can't, then 11811 * someone else is messing with it or we have been 11812 * called from nfs4_dispose and this is the page that 11813 * nfs4_dispose was called with.. anyway just skip it. 11814 */ 11815 if (!page_trylock(pp, SE_EXCL)) 11816 continue; 11817 11818 /* 11819 * Lets check again now that we have the page lock. 11820 */ 11821 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11822 page_unlock(pp); 11823 continue; 11824 } 11825 11826 /* this had better not be a free page */ 11827 ASSERT(PP_ISFREE(pp) == 0); 11828 11829 /* 11830 * The page needs to be committed and we locked it. 11831 * Update the base and length parameters and add it 11832 * to r_pages. 11833 */ 11834 if (rp->r_commit.c_pages == NULL) { 11835 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11836 rp->r_commit.c_commlen = PAGESIZE; 11837 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11838 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11839 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11840 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11841 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11842 <= pp->p_offset) { 11843 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11844 rp->r_commit.c_commbase + PAGESIZE; 11845 } 11846 page_add(&rp->r_commit.c_pages, pp); 11847 } while ((pp = pp->p_vpnext) != vp->v_pages); 11848 11849 mutex_exit(vphm); 11850 } 11851 11852 /* 11853 * This routine is used to gather together a page list of the pages 11854 * which are to be committed on the server. This routine must not 11855 * be called if the calling thread holds any locked pages. 11856 * 11857 * The calling thread must have set R4COMMIT. This bit is used to 11858 * serialize access to the commit structure in the rnode. As long 11859 * as the thread has set R4COMMIT, then it can manipulate the commit 11860 * structure without requiring any other locks. 11861 */ 11862 static void 11863 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 11864 { 11865 11866 rnode4_t *rp; 11867 page_t *pp; 11868 u_offset_t end; 11869 u_offset_t off; 11870 ASSERT(len != 0); 11871 rp = VTOR4(vp); 11872 ASSERT(rp->r_flags & R4COMMIT); 11873 11874 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11875 11876 /* make sure we're looking at the master vnode, not a shadow */ 11877 11878 if (IS_SHADOW(vp, rp)) 11879 vp = RTOV4(rp); 11880 11881 /* 11882 * If there are no pages associated with this vnode, then 11883 * just return. 11884 */ 11885 if ((pp = vp->v_pages) == NULL) 11886 return; 11887 /* 11888 * Calculate the ending offset. 11889 */ 11890 end = soff + len; 11891 for (off = soff; off < end; off += PAGESIZE) { 11892 /* 11893 * Lookup each page by vp, offset. 11894 */ 11895 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 11896 continue; 11897 /* 11898 * If this page does not need to be committed or is 11899 * modified, then just skip it. 11900 */ 11901 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11902 page_unlock(pp); 11903 continue; 11904 } 11905 11906 ASSERT(PP_ISFREE(pp) == 0); 11907 /* 11908 * The page needs to be committed and we locked it. 11909 * Update the base and length parameters and add it 11910 * to r_pages. 11911 */ 11912 if (rp->r_commit.c_pages == NULL) { 11913 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11914 rp->r_commit.c_commlen = PAGESIZE; 11915 } else { 11916 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11917 rp->r_commit.c_commbase + PAGESIZE; 11918 } 11919 page_add(&rp->r_commit.c_pages, pp); 11920 } 11921 } 11922 11923 /* 11924 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 11925 * Flushes and commits data to the server. 11926 */ 11927 static int 11928 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 11929 { 11930 int error; 11931 verifier4 write_verf; 11932 rnode4_t *rp = VTOR4(vp); 11933 11934 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11935 11936 /* 11937 * Flush the data portion of the file and then commit any 11938 * portions which need to be committed. This may need to 11939 * be done twice if the server has changed state since 11940 * data was last written. The data will need to be 11941 * rewritten to the server and then a new commit done. 11942 * 11943 * In fact, this may need to be done several times if the 11944 * server is having problems and crashing while we are 11945 * attempting to do this. 11946 */ 11947 11948 top: 11949 /* 11950 * Do a flush based on the poff and plen arguments. This 11951 * will synchronously write out any modified pages in the 11952 * range specified by (poff, plen). This starts all of the 11953 * i/o operations which will be waited for in the next 11954 * call to nfs4_putpage 11955 */ 11956 11957 mutex_enter(&rp->r_statelock); 11958 write_verf = rp->r_writeverf; 11959 mutex_exit(&rp->r_statelock); 11960 11961 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL); 11962 if (error == EAGAIN) 11963 error = 0; 11964 11965 /* 11966 * Do a flush based on the poff and plen arguments. This 11967 * will synchronously write out any modified pages in the 11968 * range specified by (poff, plen) and wait until all of 11969 * the asynchronous i/o's in that range are done as well. 11970 */ 11971 if (!error) 11972 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL); 11973 11974 if (error) 11975 return (error); 11976 11977 mutex_enter(&rp->r_statelock); 11978 if (rp->r_writeverf != write_verf) { 11979 mutex_exit(&rp->r_statelock); 11980 goto top; 11981 } 11982 mutex_exit(&rp->r_statelock); 11983 11984 /* 11985 * Now commit any pages which might need to be committed. 11986 * If the error, NFS_VERF_MISMATCH, is returned, then 11987 * start over with the flush operation. 11988 */ 11989 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 11990 11991 if (error == NFS_VERF_MISMATCH) 11992 goto top; 11993 11994 return (error); 11995 } 11996 11997 /* 11998 * nfs4_commit_vp() will wait for other pending commits and 11999 * will either commit the whole file or a range, plen dictates 12000 * if we commit whole file. a value of zero indicates the whole 12001 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 12002 */ 12003 static int 12004 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 12005 cred_t *cr, int wait_on_writes) 12006 { 12007 rnode4_t *rp; 12008 page_t *plist; 12009 offset3 offset; 12010 count3 len; 12011 12012 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12013 12014 rp = VTOR4(vp); 12015 12016 /* 12017 * before we gather commitable pages make 12018 * sure there are no outstanding async writes 12019 */ 12020 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 12021 mutex_enter(&rp->r_statelock); 12022 while (rp->r_count > 0) { 12023 cv_wait(&rp->r_cv, &rp->r_statelock); 12024 } 12025 mutex_exit(&rp->r_statelock); 12026 } 12027 12028 /* 12029 * Set the `commit inprogress' state bit. We must 12030 * first wait until any current one finishes. 12031 */ 12032 mutex_enter(&rp->r_statelock); 12033 while (rp->r_flags & R4COMMIT) { 12034 rp->r_flags |= R4COMMITWAIT; 12035 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 12036 rp->r_flags &= ~R4COMMITWAIT; 12037 } 12038 rp->r_flags |= R4COMMIT; 12039 mutex_exit(&rp->r_statelock); 12040 12041 /* 12042 * Gather all of the pages which need to be 12043 * committed. 12044 */ 12045 if (plen == 0) 12046 nfs4_get_commit(vp); 12047 else 12048 nfs4_get_commit_range(vp, poff, plen); 12049 12050 /* 12051 * Clear the `commit inprogress' bit and disconnect the 12052 * page list which was gathered by nfs4_get_commit. 12053 */ 12054 plist = rp->r_commit.c_pages; 12055 rp->r_commit.c_pages = NULL; 12056 offset = rp->r_commit.c_commbase; 12057 len = rp->r_commit.c_commlen; 12058 mutex_enter(&rp->r_statelock); 12059 rp->r_flags &= ~R4COMMIT; 12060 cv_broadcast(&rp->r_commit.c_cv); 12061 mutex_exit(&rp->r_statelock); 12062 12063 /* 12064 * If any pages need to be committed, commit them and 12065 * then unlock them so that they can be freed some 12066 * time later. 12067 */ 12068 if (plist == NULL) 12069 return (0); 12070 12071 /* 12072 * No error occurred during the flush portion 12073 * of this operation, so now attempt to commit 12074 * the data to stable storage on the server. 12075 * 12076 * This will unlock all of the pages on the list. 12077 */ 12078 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12079 } 12080 12081 static int 12082 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12083 cred_t *cr) 12084 { 12085 int error; 12086 page_t *pp; 12087 12088 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12089 12090 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12091 12092 /* 12093 * If we got an error, then just unlock all of the pages 12094 * on the list. 12095 */ 12096 if (error) { 12097 while (plist != NULL) { 12098 pp = plist; 12099 page_sub(&plist, pp); 12100 page_unlock(pp); 12101 } 12102 return (error); 12103 } 12104 /* 12105 * We've tried as hard as we can to commit the data to stable 12106 * storage on the server. We just unlock the pages and clear 12107 * the commit required state. They will get freed later. 12108 */ 12109 while (plist != NULL) { 12110 pp = plist; 12111 page_sub(&plist, pp); 12112 pp->p_fsdata = C_NOCOMMIT; 12113 page_unlock(pp); 12114 } 12115 12116 return (error); 12117 } 12118 12119 static void 12120 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12121 cred_t *cr) 12122 { 12123 12124 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12125 } 12126 12127 /*ARGSUSED*/ 12128 static int 12129 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12130 caller_context_t *ct) 12131 { 12132 int error = 0; 12133 mntinfo4_t *mi; 12134 vattr_t va; 12135 vsecattr_t nfsace4_vsap; 12136 12137 mi = VTOMI4(vp); 12138 if (nfs_zone() != mi->mi_zone) 12139 return (EIO); 12140 if (mi->mi_flags & MI4_ACL) { 12141 /* if we have a delegation, return it */ 12142 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12143 (void) nfs4delegreturn(VTOR4(vp), 12144 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12145 12146 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12147 NFS4_ACL_SET); 12148 if (error) /* EINVAL */ 12149 return (error); 12150 12151 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12152 /* 12153 * These are aclent_t type entries. 12154 */ 12155 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12156 vp->v_type == VDIR, FALSE); 12157 if (error) 12158 return (error); 12159 } else { 12160 /* 12161 * These are ace_t type entries. 12162 */ 12163 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12164 FALSE); 12165 if (error) 12166 return (error); 12167 } 12168 bzero(&va, sizeof (va)); 12169 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12170 vs_ace4_destroy(&nfsace4_vsap); 12171 return (error); 12172 } 12173 return (ENOSYS); 12174 } 12175 12176 /* ARGSUSED */ 12177 int 12178 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12179 caller_context_t *ct) 12180 { 12181 int error; 12182 mntinfo4_t *mi; 12183 nfs4_ga_res_t gar; 12184 rnode4_t *rp = VTOR4(vp); 12185 12186 mi = VTOMI4(vp); 12187 if (nfs_zone() != mi->mi_zone) 12188 return (EIO); 12189 12190 bzero(&gar, sizeof (gar)); 12191 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12192 12193 /* 12194 * vsecattr->vsa_mask holds the original acl request mask. 12195 * This is needed when determining what to return. 12196 * (See: nfs4_create_getsecattr_return()) 12197 */ 12198 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12199 if (error) /* EINVAL */ 12200 return (error); 12201 12202 if (mi->mi_flags & MI4_ACL) { 12203 /* 12204 * Check if the data is cached and the cache is valid. If it 12205 * is we don't go over the wire. 12206 */ 12207 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12208 mutex_enter(&rp->r_statelock); 12209 if (rp->r_secattr != NULL) { 12210 error = nfs4_create_getsecattr_return( 12211 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12212 rp->r_attr.va_gid, 12213 vp->v_type == VDIR); 12214 if (!error) { /* error == 0 - Success! */ 12215 mutex_exit(&rp->r_statelock); 12216 return (error); 12217 } 12218 } 12219 mutex_exit(&rp->r_statelock); 12220 } 12221 12222 /* 12223 * The getattr otw call will always get both the acl, in 12224 * the form of a list of nfsace4's, and the number of acl 12225 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12226 */ 12227 gar.n4g_va.va_mask = AT_ALL; 12228 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12229 if (error) { 12230 vs_ace4_destroy(&gar.n4g_vsa); 12231 if (error == ENOTSUP || error == EOPNOTSUPP) 12232 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12233 return (error); 12234 } 12235 12236 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12237 /* 12238 * No error was returned, but according to the response 12239 * bitmap, neither was an acl. 12240 */ 12241 vs_ace4_destroy(&gar.n4g_vsa); 12242 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12243 return (error); 12244 } 12245 12246 /* 12247 * Update the cache with the ACL. 12248 */ 12249 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12250 12251 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12252 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12253 vp->v_type == VDIR); 12254 vs_ace4_destroy(&gar.n4g_vsa); 12255 if ((error) && (vsecattr->vsa_mask & 12256 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12257 (error != EACCES)) { 12258 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12259 } 12260 return (error); 12261 } 12262 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12263 return (error); 12264 } 12265 12266 /* 12267 * The function returns: 12268 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12269 * - EINVAL if the passed in "acl_mask" is an invalid request. 12270 * 12271 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12272 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12273 * 12274 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12275 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12276 * - We have a count field set without the corresponding acl field set. (e.g. - 12277 * VSA_ACECNT is set, but VSA_ACE is not) 12278 */ 12279 static int 12280 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12281 { 12282 /* Shortcut the masks that are always valid. */ 12283 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12284 return (0); 12285 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12286 return (0); 12287 12288 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12289 /* 12290 * We can't have any VSA_ACL type stuff in the mask now. 12291 */ 12292 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12293 VSA_DFACLCNT)) 12294 return (EINVAL); 12295 12296 if (op == NFS4_ACL_SET) { 12297 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12298 return (EINVAL); 12299 } 12300 } 12301 12302 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12303 /* 12304 * We can't have any VSA_ACE type stuff in the mask now. 12305 */ 12306 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12307 return (EINVAL); 12308 12309 if (op == NFS4_ACL_SET) { 12310 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12311 return (EINVAL); 12312 12313 if ((acl_mask & VSA_DFACLCNT) && 12314 !(acl_mask & VSA_DFACL)) 12315 return (EINVAL); 12316 } 12317 } 12318 return (0); 12319 } 12320 12321 /* 12322 * The theory behind creating the correct getsecattr return is simply this: 12323 * "Don't return anything that the caller is not expecting to have to free." 12324 */ 12325 static int 12326 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12327 uid_t uid, gid_t gid, int isdir) 12328 { 12329 int error = 0; 12330 /* Save the mask since the translators modify it. */ 12331 uint_t orig_mask = vsap->vsa_mask; 12332 12333 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12334 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, 12335 FALSE, ((orig_mask & VSA_ACE) ? FALSE : TRUE)); 12336 12337 if (error) 12338 return (error); 12339 12340 /* 12341 * If the caller only asked for the ace count (VSA_ACECNT) 12342 * don't give them the full acl (VSA_ACE), free it. 12343 */ 12344 if (!orig_mask & VSA_ACE) { 12345 if (vsap->vsa_aclentp != NULL) { 12346 kmem_free(vsap->vsa_aclentp, 12347 vsap->vsa_aclcnt * sizeof (ace_t)); 12348 vsap->vsa_aclentp = NULL; 12349 } 12350 } 12351 vsap->vsa_mask = orig_mask; 12352 12353 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12354 VSA_DFACLCNT)) { 12355 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12356 isdir, FALSE, 12357 ((orig_mask & (VSA_ACL | VSA_DFACL)) ? FALSE : TRUE)); 12358 12359 if (error) 12360 return (error); 12361 12362 /* 12363 * If the caller only asked for the acl count (VSA_ACLCNT) 12364 * and/or the default acl count (VSA_DFACLCNT) don't give them 12365 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12366 */ 12367 if (!orig_mask & VSA_ACL) { 12368 if (vsap->vsa_aclentp != NULL) { 12369 kmem_free(vsap->vsa_aclentp, 12370 vsap->vsa_aclcnt * sizeof (aclent_t)); 12371 vsap->vsa_aclentp = NULL; 12372 } 12373 } 12374 12375 if (!orig_mask & VSA_DFACL) { 12376 if (vsap->vsa_dfaclentp != NULL) { 12377 kmem_free(vsap->vsa_dfaclentp, 12378 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12379 vsap->vsa_dfaclentp = NULL; 12380 } 12381 } 12382 vsap->vsa_mask = orig_mask; 12383 } 12384 return (0); 12385 } 12386 12387 /* ARGSUSED */ 12388 int 12389 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr, 12390 caller_context_t *ct) 12391 { 12392 int error; 12393 12394 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12395 return (EIO); 12396 /* 12397 * check for valid cmd parameter 12398 */ 12399 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12400 return (EINVAL); 12401 12402 /* 12403 * Check access permissions 12404 */ 12405 if ((cmd & F_SHARE) && 12406 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12407 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12408 return (EBADF); 12409 12410 /* 12411 * If the filesystem is mounted using local locking, pass the 12412 * request off to the local share code. 12413 */ 12414 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12415 return (fs_shrlock(vp, cmd, shr, flag, cr, ct)); 12416 12417 switch (cmd) { 12418 case F_SHARE: 12419 case F_UNSHARE: 12420 /* 12421 * This will be properly implemented later, 12422 * see RFE: 4823948 . 12423 */ 12424 error = EAGAIN; 12425 break; 12426 12427 case F_HASREMOTELOCKS: 12428 /* 12429 * NFS client can't store remote locks itself 12430 */ 12431 shr->s_access = 0; 12432 error = 0; 12433 break; 12434 12435 default: 12436 error = EINVAL; 12437 break; 12438 } 12439 12440 return (error); 12441 } 12442 12443 /* 12444 * Common code called by directory ops to update the attrcache 12445 */ 12446 static int 12447 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12448 hrtime_t t, vnode_t *vp, cred_t *cr) 12449 { 12450 int error = 0; 12451 12452 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12453 12454 if (status != NFS4_OK) { 12455 /* getattr not done or failed */ 12456 PURGE_ATTRCACHE4(vp); 12457 return (error); 12458 } 12459 12460 if (garp) { 12461 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12462 } else { 12463 PURGE_ATTRCACHE4(vp); 12464 } 12465 return (error); 12466 } 12467 12468 /* 12469 * Update directory caches for directory modification ops (link, rename, etc.) 12470 * When dinfo is NULL, manage dircaches in the old way. 12471 */ 12472 static void 12473 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12474 dirattr_info_t *dinfo) 12475 { 12476 rnode4_t *drp = VTOR4(dvp); 12477 12478 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12479 12480 /* Purge rddir cache for dir since it changed */ 12481 if (drp->r_dir != NULL) 12482 nfs4_purge_rddir_cache(dvp); 12483 12484 /* 12485 * If caller provided dinfo, then use it to manage dir caches. 12486 */ 12487 if (dinfo != NULL) { 12488 if (vp != NULL) { 12489 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12490 if (!VTOR4(vp)->created_v4) { 12491 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12492 dnlc_update(dvp, nm, vp); 12493 } else { 12494 /* 12495 * XXX don't update if the created_v4 flag is 12496 * set 12497 */ 12498 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12499 NFS4_DEBUG(nfs4_client_state_debug, 12500 (CE_NOTE, "nfs4_update_dircaches: " 12501 "don't update dnlc: created_v4 flag")); 12502 } 12503 } 12504 12505 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12506 dinfo->di_cred, FALSE, cinfo); 12507 12508 return; 12509 } 12510 12511 /* 12512 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12513 * Since caller modified dir but didn't receive post-dirmod-op dir 12514 * attrs, the dir's attrs must be purged. 12515 * 12516 * XXX this check and dnlc update/purge should really be atomic, 12517 * XXX but can't use rnode statelock because it'll deadlock in 12518 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12519 * XXX does occur. 12520 * 12521 * XXX We also may want to check that atomic is true in the 12522 * XXX change_info struct. If it is not, the change_info may 12523 * XXX reflect changes by more than one clients which means that 12524 * XXX our cache may not be valid. 12525 */ 12526 PURGE_ATTRCACHE4(dvp); 12527 if (drp->r_change == cinfo->before) { 12528 /* no changes took place in the directory prior to our link */ 12529 if (vp != NULL) { 12530 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12531 if (!VTOR4(vp)->created_v4) { 12532 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12533 dnlc_update(dvp, nm, vp); 12534 } else { 12535 /* 12536 * XXX dont' update if the created_v4 flag 12537 * is set 12538 */ 12539 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12540 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12541 "nfs4_update_dircaches: don't" 12542 " update dnlc: created_v4 flag")); 12543 } 12544 } 12545 } else { 12546 /* Another client modified directory - purge its dnlc cache */ 12547 dnlc_purge_vp(dvp); 12548 } 12549 } 12550 12551 /* 12552 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12553 * file. 12554 * 12555 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12556 * file (ie: client recovery) and otherwise set to FALSE. 12557 * 12558 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12559 * initiated) calling functions. 12560 * 12561 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12562 * of resending a 'lost' open request. 12563 * 12564 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12565 * server that hands out BAD_SEQID on open confirm. 12566 * 12567 * Errors are returned via the nfs4_error_t parameter. 12568 */ 12569 void 12570 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12571 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12572 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12573 { 12574 COMPOUND4args_clnt args; 12575 COMPOUND4res_clnt res; 12576 nfs_argop4 argop[2]; 12577 nfs_resop4 *resop; 12578 int doqueue = 1; 12579 mntinfo4_t *mi; 12580 OPEN_CONFIRM4args *open_confirm_args; 12581 int needrecov; 12582 12583 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12584 #if DEBUG 12585 mutex_enter(&oop->oo_lock); 12586 ASSERT(oop->oo_seqid_inuse); 12587 mutex_exit(&oop->oo_lock); 12588 #endif 12589 12590 recov_retry_confirm: 12591 nfs4_error_zinit(ep); 12592 *retry_open = FALSE; 12593 12594 if (resend) 12595 args.ctag = TAG_OPEN_CONFIRM_LOST; 12596 else 12597 args.ctag = TAG_OPEN_CONFIRM; 12598 12599 args.array_len = 2; 12600 args.array = argop; 12601 12602 /* putfh target fh */ 12603 argop[0].argop = OP_CPUTFH; 12604 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12605 12606 argop[1].argop = OP_OPEN_CONFIRM; 12607 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12608 12609 (*seqid) += 1; 12610 open_confirm_args->seqid = *seqid; 12611 open_confirm_args->open_stateid = *stateid; 12612 12613 mi = VTOMI4(vp); 12614 12615 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12616 12617 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12618 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12619 } 12620 12621 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12622 if (!needrecov && ep->error) 12623 return; 12624 12625 if (needrecov) { 12626 bool_t abort = FALSE; 12627 12628 if (reopening_file == FALSE) { 12629 nfs4_bseqid_entry_t *bsep = NULL; 12630 12631 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12632 bsep = nfs4_create_bseqid_entry(oop, NULL, 12633 vp, 0, args.ctag, 12634 open_confirm_args->seqid); 12635 12636 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, 12637 NULL, NULL, NULL, OP_OPEN_CONFIRM, bsep); 12638 if (bsep) { 12639 kmem_free(bsep, sizeof (*bsep)); 12640 if (num_bseqid_retryp && 12641 --(*num_bseqid_retryp) == 0) 12642 abort = TRUE; 12643 } 12644 } 12645 if ((ep->error == ETIMEDOUT || 12646 res.status == NFS4ERR_RESOURCE) && 12647 abort == FALSE && resend == FALSE) { 12648 if (!ep->error) 12649 (void) xdr_free(xdr_COMPOUND4res_clnt, 12650 (caddr_t)&res); 12651 12652 delay(SEC_TO_TICK(confirm_retry_sec)); 12653 goto recov_retry_confirm; 12654 } 12655 /* State may have changed so retry the entire OPEN op */ 12656 if (abort == FALSE) 12657 *retry_open = TRUE; 12658 else 12659 *retry_open = FALSE; 12660 if (!ep->error) 12661 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12662 return; 12663 } 12664 12665 if (res.status) { 12666 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12667 return; 12668 } 12669 12670 resop = &res.array[1]; /* open confirm res */ 12671 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12672 stateid, sizeof (*stateid)); 12673 12674 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12675 } 12676 12677 /* 12678 * Return the credentials associated with a client state object. The 12679 * caller is responsible for freeing the credentials. 12680 */ 12681 12682 static cred_t * 12683 state_to_cred(nfs4_open_stream_t *osp) 12684 { 12685 cred_t *cr; 12686 12687 /* 12688 * It's ok to not lock the open stream and open owner to get 12689 * the oo_cred since this is only written once (upon creation) 12690 * and will not change. 12691 */ 12692 cr = osp->os_open_owner->oo_cred; 12693 crhold(cr); 12694 12695 return (cr); 12696 } 12697 12698 /* 12699 * nfs4_find_sysid 12700 * 12701 * Find the sysid for the knetconfig associated with the given mi. 12702 */ 12703 static struct lm_sysid * 12704 nfs4_find_sysid(mntinfo4_t *mi) 12705 { 12706 ASSERT(nfs_zone() == mi->mi_zone); 12707 12708 /* 12709 * Switch from RDMA knconf to original mount knconf 12710 */ 12711 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12712 mi->mi_curr_serv->sv_hostname, NULL)); 12713 } 12714 12715 #ifdef DEBUG 12716 /* 12717 * Return a string version of the call type for easy reading. 12718 */ 12719 static char * 12720 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12721 { 12722 switch (ctype) { 12723 case NFS4_LCK_CTYPE_NORM: 12724 return ("NORMAL"); 12725 case NFS4_LCK_CTYPE_RECLAIM: 12726 return ("RECLAIM"); 12727 case NFS4_LCK_CTYPE_RESEND: 12728 return ("RESEND"); 12729 case NFS4_LCK_CTYPE_REINSTATE: 12730 return ("REINSTATE"); 12731 default: 12732 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12733 "type %d", ctype); 12734 return (""); 12735 } 12736 } 12737 #endif 12738 12739 /* 12740 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12741 * Unlock requests don't have an over-the-wire locktype, so we just return 12742 * something non-threatening. 12743 */ 12744 12745 static nfs_lock_type4 12746 flk_to_locktype(int cmd, int l_type) 12747 { 12748 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12749 12750 switch (l_type) { 12751 case F_UNLCK: 12752 return (READ_LT); 12753 case F_RDLCK: 12754 if (cmd == F_SETLK) 12755 return (READ_LT); 12756 else 12757 return (READW_LT); 12758 case F_WRLCK: 12759 if (cmd == F_SETLK) 12760 return (WRITE_LT); 12761 else 12762 return (WRITEW_LT); 12763 } 12764 panic("flk_to_locktype"); 12765 /*NOTREACHED*/ 12766 } 12767 12768 /* 12769 * Do some preliminary checks for nfs4frlock. 12770 */ 12771 static int 12772 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12773 u_offset_t offset) 12774 { 12775 int error = 0; 12776 12777 /* 12778 * If we are setting a lock, check that the file is opened 12779 * with the correct mode. 12780 */ 12781 if (cmd == F_SETLK || cmd == F_SETLKW) { 12782 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12783 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12784 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12785 "nfs4frlock_validate_args: file was opened with " 12786 "incorrect mode")); 12787 return (EBADF); 12788 } 12789 } 12790 12791 /* Convert the offset. It may need to be restored before returning. */ 12792 if (error = convoff(vp, flk, 0, offset)) { 12793 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12794 "nfs4frlock_validate_args: convoff => error= %d\n", 12795 error)); 12796 return (error); 12797 } 12798 12799 return (error); 12800 } 12801 12802 /* 12803 * Set the flock64's lm_sysid for nfs4frlock. 12804 */ 12805 static int 12806 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12807 { 12808 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12809 12810 /* Find the lm_sysid */ 12811 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12812 12813 if (*lspp == NULL) { 12814 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12815 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12816 return (ENOLCK); 12817 } 12818 12819 flk->l_sysid = lm_sysidt(*lspp); 12820 12821 return (0); 12822 } 12823 12824 /* 12825 * Do the remaining preliminary setup for nfs4frlock. 12826 */ 12827 static void 12828 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12829 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12830 cred_t **cred_otw) 12831 { 12832 /* 12833 * set tick_delay to the base delay time. 12834 * (NFS4_BASE_WAIT_TIME is in secs) 12835 */ 12836 12837 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000); 12838 12839 /* 12840 * If lock is relative to EOF, we need the newest length of the 12841 * file. Therefore invalidate the ATTR_CACHE. 12842 */ 12843 12844 *whencep = flk->l_whence; 12845 12846 if (*whencep == 2) /* SEEK_END */ 12847 PURGE_ATTRCACHE4(vp); 12848 12849 recov_statep->rs_flags = 0; 12850 recov_statep->rs_num_retry_despite_err = 0; 12851 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 12852 } 12853 12854 /* 12855 * Initialize and allocate the data structures necessary for 12856 * the nfs4frlock call. 12857 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 12858 */ 12859 static void 12860 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 12861 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 12862 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 12863 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 12864 { 12865 int argoplist_size; 12866 int num_ops = 2; 12867 12868 *retry = FALSE; 12869 *did_start_fop = FALSE; 12870 *skip_get_err = FALSE; 12871 lost_rqstp->lr_op = 0; 12872 argoplist_size = num_ops * sizeof (nfs_argop4); 12873 /* fill array with zero */ 12874 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 12875 12876 *argspp = argsp; 12877 *respp = NULL; 12878 12879 argsp->array_len = num_ops; 12880 argsp->array = *argopp; 12881 12882 /* initialize in case of error; will get real value down below */ 12883 argsp->ctag = TAG_NONE; 12884 12885 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 12886 *op_hintp = OH_LOCKU; 12887 else 12888 *op_hintp = OH_OTHER; 12889 } 12890 12891 /* 12892 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 12893 * the proper nfs4_server_t for this instance of nfs4frlock. 12894 * Returns 0 (success) or an errno value. 12895 */ 12896 static int 12897 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 12898 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 12899 bool_t *did_start_fop, bool_t *startrecovp) 12900 { 12901 int error = 0; 12902 rnode4_t *rp; 12903 12904 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12905 12906 if (ctype == NFS4_LCK_CTYPE_NORM) { 12907 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 12908 recov_statep, startrecovp); 12909 if (error) 12910 return (error); 12911 *did_start_fop = TRUE; 12912 } else { 12913 *did_start_fop = FALSE; 12914 *startrecovp = FALSE; 12915 } 12916 12917 if (!error) { 12918 rp = VTOR4(vp); 12919 12920 /* If the file failed recovery, just quit. */ 12921 mutex_enter(&rp->r_statelock); 12922 if (rp->r_flags & R4RECOVERR) { 12923 error = EIO; 12924 } 12925 mutex_exit(&rp->r_statelock); 12926 } 12927 12928 return (error); 12929 } 12930 12931 /* 12932 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 12933 * resend nfs4frlock call is initiated by the recovery framework. 12934 * Acquires the lop and oop seqid synchronization. 12935 */ 12936 static void 12937 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 12938 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 12939 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 12940 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 12941 { 12942 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 12943 int error; 12944 12945 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 12946 (CE_NOTE, 12947 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 12948 ASSERT(resend_rqstp != NULL); 12949 ASSERT(resend_rqstp->lr_op == OP_LOCK || 12950 resend_rqstp->lr_op == OP_LOCKU); 12951 12952 *oopp = resend_rqstp->lr_oop; 12953 if (resend_rqstp->lr_oop) { 12954 open_owner_hold(resend_rqstp->lr_oop); 12955 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 12956 ASSERT(error == 0); /* recov thread always succeeds */ 12957 } 12958 12959 /* Must resend this lost lock/locku request. */ 12960 ASSERT(resend_rqstp->lr_lop != NULL); 12961 *lopp = resend_rqstp->lr_lop; 12962 lock_owner_hold(resend_rqstp->lr_lop); 12963 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 12964 ASSERT(error == 0); /* recov thread always succeeds */ 12965 12966 *ospp = resend_rqstp->lr_osp; 12967 if (*ospp) 12968 open_stream_hold(resend_rqstp->lr_osp); 12969 12970 if (resend_rqstp->lr_op == OP_LOCK) { 12971 LOCK4args *lock_args; 12972 12973 argop->argop = OP_LOCK; 12974 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 12975 lock_args->locktype = resend_rqstp->lr_locktype; 12976 lock_args->reclaim = 12977 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 12978 lock_args->offset = resend_rqstp->lr_flk->l_start; 12979 lock_args->length = resend_rqstp->lr_flk->l_len; 12980 if (lock_args->length == 0) 12981 lock_args->length = ~lock_args->length; 12982 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 12983 mi2clientid(mi), &lock_args->locker); 12984 12985 switch (resend_rqstp->lr_ctype) { 12986 case NFS4_LCK_CTYPE_RESEND: 12987 argsp->ctag = TAG_LOCK_RESEND; 12988 break; 12989 case NFS4_LCK_CTYPE_REINSTATE: 12990 argsp->ctag = TAG_LOCK_REINSTATE; 12991 break; 12992 case NFS4_LCK_CTYPE_RECLAIM: 12993 argsp->ctag = TAG_LOCK_RECLAIM; 12994 break; 12995 default: 12996 argsp->ctag = TAG_LOCK_UNKNOWN; 12997 break; 12998 } 12999 } else { 13000 LOCKU4args *locku_args; 13001 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 13002 13003 argop->argop = OP_LOCKU; 13004 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 13005 locku_args->locktype = READ_LT; 13006 locku_args->seqid = lop->lock_seqid + 1; 13007 mutex_enter(&lop->lo_lock); 13008 locku_args->lock_stateid = lop->lock_stateid; 13009 mutex_exit(&lop->lo_lock); 13010 locku_args->offset = resend_rqstp->lr_flk->l_start; 13011 locku_args->length = resend_rqstp->lr_flk->l_len; 13012 if (locku_args->length == 0) 13013 locku_args->length = ~locku_args->length; 13014 13015 switch (resend_rqstp->lr_ctype) { 13016 case NFS4_LCK_CTYPE_RESEND: 13017 argsp->ctag = TAG_LOCKU_RESEND; 13018 break; 13019 case NFS4_LCK_CTYPE_REINSTATE: 13020 argsp->ctag = TAG_LOCKU_REINSTATE; 13021 break; 13022 default: 13023 argsp->ctag = TAG_LOCK_UNKNOWN; 13024 break; 13025 } 13026 } 13027 } 13028 13029 /* 13030 * Setup the LOCKT4 arguments. 13031 */ 13032 static void 13033 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13034 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 13035 rnode4_t *rp) 13036 { 13037 LOCKT4args *lockt_args; 13038 13039 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 13040 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13041 argop->argop = OP_LOCKT; 13042 argsp->ctag = TAG_LOCKT; 13043 lockt_args = &argop->nfs_argop4_u.oplockt; 13044 13045 /* 13046 * The locktype will be READ_LT unless it's 13047 * a write lock. We do this because the Solaris 13048 * system call allows the combination of 13049 * F_UNLCK and F_GETLK* and so in that case the 13050 * unlock is mapped to a read. 13051 */ 13052 if (flk->l_type == F_WRLCK) 13053 lockt_args->locktype = WRITE_LT; 13054 else 13055 lockt_args->locktype = READ_LT; 13056 13057 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 13058 /* set the lock owner4 args */ 13059 nfs4_setlockowner_args(&lockt_args->owner, rp, 13060 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13061 flk->l_pid); 13062 lockt_args->offset = flk->l_start; 13063 lockt_args->length = flk->l_len; 13064 if (flk->l_len == 0) 13065 lockt_args->length = ~lockt_args->length; 13066 13067 *lockt_argsp = lockt_args; 13068 } 13069 13070 /* 13071 * If the client is holding a delegation, and the open stream to be used 13072 * with this lock request is a delegation open stream, then re-open the stream. 13073 * Sets the nfs4_error_t to all zeros unless the open stream has already 13074 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13075 * means the caller should retry (like a recovery retry). 13076 */ 13077 static void 13078 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13079 { 13080 open_delegation_type4 dt; 13081 bool_t reopen_needed, force; 13082 nfs4_open_stream_t *osp; 13083 open_claim_type4 oclaim; 13084 rnode4_t *rp = VTOR4(vp); 13085 mntinfo4_t *mi = VTOMI4(vp); 13086 13087 ASSERT(nfs_zone() == mi->mi_zone); 13088 13089 nfs4_error_zinit(ep); 13090 13091 mutex_enter(&rp->r_statev4_lock); 13092 dt = rp->r_deleg_type; 13093 mutex_exit(&rp->r_statev4_lock); 13094 13095 if (dt != OPEN_DELEGATE_NONE) { 13096 nfs4_open_owner_t *oop; 13097 13098 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13099 if (!oop) { 13100 ep->stat = NFS4ERR_IO; 13101 return; 13102 } 13103 /* returns with 'os_sync_lock' held */ 13104 osp = find_open_stream(oop, rp); 13105 if (!osp) { 13106 open_owner_rele(oop); 13107 ep->stat = NFS4ERR_IO; 13108 return; 13109 } 13110 13111 if (osp->os_failed_reopen) { 13112 NFS4_DEBUG((nfs4_open_stream_debug || 13113 nfs4_client_lock_debug), (CE_NOTE, 13114 "nfs4frlock_check_deleg: os_failed_reopen set " 13115 "for osp %p, cr %p, rp %s", (void *)osp, 13116 (void *)cr, rnode4info(rp))); 13117 mutex_exit(&osp->os_sync_lock); 13118 open_stream_rele(osp, rp); 13119 open_owner_rele(oop); 13120 ep->stat = NFS4ERR_IO; 13121 return; 13122 } 13123 13124 /* 13125 * Determine whether a reopen is needed. If this 13126 * is a delegation open stream, then send the open 13127 * to the server to give visibility to the open owner. 13128 * Even if it isn't a delegation open stream, we need 13129 * to check if the previous open CLAIM_DELEGATE_CUR 13130 * was sufficient. 13131 */ 13132 13133 reopen_needed = osp->os_delegation || 13134 ((lt == F_RDLCK && 13135 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13136 (lt == F_WRLCK && 13137 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13138 13139 mutex_exit(&osp->os_sync_lock); 13140 open_owner_rele(oop); 13141 13142 if (reopen_needed) { 13143 /* 13144 * Always use CLAIM_PREVIOUS after server reboot. 13145 * The server will reject CLAIM_DELEGATE_CUR if 13146 * it is used during the grace period. 13147 */ 13148 mutex_enter(&mi->mi_lock); 13149 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13150 oclaim = CLAIM_PREVIOUS; 13151 force = TRUE; 13152 } else { 13153 oclaim = CLAIM_DELEGATE_CUR; 13154 force = FALSE; 13155 } 13156 mutex_exit(&mi->mi_lock); 13157 13158 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13159 if (ep->error == EAGAIN) { 13160 nfs4_error_zinit(ep); 13161 ep->stat = NFS4ERR_DELAY; 13162 } 13163 } 13164 open_stream_rele(osp, rp); 13165 osp = NULL; 13166 } 13167 } 13168 13169 /* 13170 * Setup the LOCKU4 arguments. 13171 * Returns errors via the nfs4_error_t. 13172 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13173 * over-the-wire. The caller must release the 13174 * reference on *lopp. 13175 * NFS4ERR_DELAY caller should retry (like recovery retry) 13176 * (other) unrecoverable error. 13177 */ 13178 static void 13179 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13180 LOCKU4args **locku_argsp, flock64_t *flk, 13181 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13182 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13183 bool_t *skip_get_err, bool_t *go_otwp) 13184 { 13185 nfs4_lock_owner_t *lop = NULL; 13186 LOCKU4args *locku_args; 13187 pid_t pid; 13188 bool_t is_spec = FALSE; 13189 rnode4_t *rp = VTOR4(vp); 13190 13191 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13192 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13193 13194 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13195 if (ep->error || ep->stat) 13196 return; 13197 13198 argop->argop = OP_LOCKU; 13199 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13200 argsp->ctag = TAG_LOCKU_REINSTATE; 13201 else 13202 argsp->ctag = TAG_LOCKU; 13203 locku_args = &argop->nfs_argop4_u.oplocku; 13204 *locku_argsp = locku_args; 13205 13206 /* 13207 * XXX what should locku_args->locktype be? 13208 * setting to ALWAYS be READ_LT so at least 13209 * it is a valid locktype. 13210 */ 13211 13212 locku_args->locktype = READ_LT; 13213 13214 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13215 flk->l_pid; 13216 13217 /* 13218 * Get the lock owner stateid. If no lock owner 13219 * exists, return success. 13220 */ 13221 lop = find_lock_owner(rp, pid, LOWN_ANY); 13222 *lopp = lop; 13223 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13224 is_spec = TRUE; 13225 if (!lop || is_spec) { 13226 /* 13227 * No lock owner so no locks to unlock. 13228 * Return success. If there was a failed 13229 * reclaim earlier, the lock might still be 13230 * registered with the local locking code, 13231 * so notify it of the unlock. 13232 * 13233 * If the lockowner is using a special stateid, 13234 * then the original lock request (that created 13235 * this lockowner) was never successful, so we 13236 * have no lock to undo OTW. 13237 */ 13238 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13239 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13240 "(%ld) so return success", (long)pid)); 13241 13242 if (ctype == NFS4_LCK_CTYPE_NORM) 13243 flk->l_pid = curproc->p_pid; 13244 nfs4_register_lock_locally(vp, flk, flag, offset); 13245 /* 13246 * Release our hold and NULL out so final_cleanup 13247 * doesn't try to end a lock seqid sync we 13248 * never started. 13249 */ 13250 if (is_spec) { 13251 lock_owner_rele(lop); 13252 *lopp = NULL; 13253 } 13254 *skip_get_err = TRUE; 13255 *go_otwp = FALSE; 13256 return; 13257 } 13258 13259 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13260 if (ep->error == EAGAIN) { 13261 lock_owner_rele(lop); 13262 *lopp = NULL; 13263 return; 13264 } 13265 13266 mutex_enter(&lop->lo_lock); 13267 locku_args->lock_stateid = lop->lock_stateid; 13268 mutex_exit(&lop->lo_lock); 13269 locku_args->seqid = lop->lock_seqid + 1; 13270 13271 /* leave the ref count on lop, rele after RPC call */ 13272 13273 locku_args->offset = flk->l_start; 13274 locku_args->length = flk->l_len; 13275 if (flk->l_len == 0) 13276 locku_args->length = ~locku_args->length; 13277 13278 *go_otwp = TRUE; 13279 } 13280 13281 /* 13282 * Setup the LOCK4 arguments. 13283 * 13284 * Returns errors via the nfs4_error_t. 13285 * NFS4_OK no problems 13286 * NFS4ERR_DELAY caller should retry (like recovery retry) 13287 * (other) unrecoverable error 13288 */ 13289 static void 13290 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13291 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13292 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13293 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13294 { 13295 LOCK4args *lock_args; 13296 nfs4_open_owner_t *oop = NULL; 13297 nfs4_open_stream_t *osp = NULL; 13298 nfs4_lock_owner_t *lop = NULL; 13299 pid_t pid; 13300 rnode4_t *rp = VTOR4(vp); 13301 13302 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13303 13304 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13305 if (ep->error || ep->stat != NFS4_OK) 13306 return; 13307 13308 argop->argop = OP_LOCK; 13309 if (ctype == NFS4_LCK_CTYPE_NORM) 13310 argsp->ctag = TAG_LOCK; 13311 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13312 argsp->ctag = TAG_RELOCK; 13313 else 13314 argsp->ctag = TAG_LOCK_REINSTATE; 13315 lock_args = &argop->nfs_argop4_u.oplock; 13316 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13317 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13318 /* 13319 * Get the lock owner. If no lock owner exists, 13320 * create a 'temporary' one and grab the open seqid 13321 * synchronization (which puts a hold on the open 13322 * owner and open stream). 13323 * This also grabs the lock seqid synchronization. 13324 */ 13325 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13326 ep->stat = 13327 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13328 13329 if (ep->stat != NFS4_OK) 13330 goto out; 13331 13332 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13333 &lock_args->locker); 13334 13335 lock_args->offset = flk->l_start; 13336 lock_args->length = flk->l_len; 13337 if (flk->l_len == 0) 13338 lock_args->length = ~lock_args->length; 13339 *lock_argsp = lock_args; 13340 out: 13341 *oopp = oop; 13342 *ospp = osp; 13343 *lopp = lop; 13344 } 13345 13346 /* 13347 * After we get the reply from the server, record the proper information 13348 * for possible resend lock requests. 13349 * 13350 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13351 */ 13352 static void 13353 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13354 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13355 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13356 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13357 { 13358 bool_t unlock = (flk->l_type == F_UNLCK); 13359 13360 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13361 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13362 ctype == NFS4_LCK_CTYPE_REINSTATE); 13363 13364 if (error != 0 && !unlock) { 13365 NFS4_DEBUG((nfs4_lost_rqst_debug || 13366 nfs4_client_lock_debug), (CE_NOTE, 13367 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13368 " for lop %p", (void *)lop)); 13369 ASSERT(lop != NULL); 13370 mutex_enter(&lop->lo_lock); 13371 lop->lo_pending_rqsts = 1; 13372 mutex_exit(&lop->lo_lock); 13373 } 13374 13375 lost_rqstp->lr_putfirst = FALSE; 13376 lost_rqstp->lr_op = 0; 13377 13378 /* 13379 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13380 * recovery purposes so that the lock request that was sent 13381 * can be saved and re-issued later. Ditto for EIO from a forced 13382 * unmount. This is done to have the client's local locking state 13383 * match the v4 server's state; that is, the request was 13384 * potentially received and accepted by the server but the client 13385 * thinks it was not. 13386 */ 13387 if (error == ETIMEDOUT || error == EINTR || 13388 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13389 NFS4_DEBUG((nfs4_lost_rqst_debug || 13390 nfs4_client_lock_debug), (CE_NOTE, 13391 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13392 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13393 (void *)lop, (void *)oop, (void *)osp)); 13394 if (unlock) 13395 lost_rqstp->lr_op = OP_LOCKU; 13396 else { 13397 lost_rqstp->lr_op = OP_LOCK; 13398 lost_rqstp->lr_locktype = locktype; 13399 } 13400 /* 13401 * Objects are held and rele'd via the recovery code. 13402 * See nfs4_save_lost_rqst. 13403 */ 13404 lost_rqstp->lr_vp = vp; 13405 lost_rqstp->lr_dvp = NULL; 13406 lost_rqstp->lr_oop = oop; 13407 lost_rqstp->lr_osp = osp; 13408 lost_rqstp->lr_lop = lop; 13409 lost_rqstp->lr_cr = cr; 13410 switch (ctype) { 13411 case NFS4_LCK_CTYPE_NORM: 13412 flk->l_pid = ttoproc(curthread)->p_pid; 13413 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13414 break; 13415 case NFS4_LCK_CTYPE_REINSTATE: 13416 lost_rqstp->lr_putfirst = TRUE; 13417 lost_rqstp->lr_ctype = ctype; 13418 break; 13419 default: 13420 break; 13421 } 13422 lost_rqstp->lr_flk = flk; 13423 } 13424 } 13425 13426 /* 13427 * Update lop's seqid. Also update the seqid stored in a resend request, 13428 * if any. (Some recovery errors increment the seqid, and we may have to 13429 * send the resend request again.) 13430 */ 13431 13432 static void 13433 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13434 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13435 { 13436 if (lock_args) { 13437 if (lock_args->locker.new_lock_owner == TRUE) 13438 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13439 else { 13440 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13441 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13442 } 13443 } else if (locku_args) { 13444 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13445 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13446 } 13447 } 13448 13449 /* 13450 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13451 * COMPOUND4 args/res for calls that need to retry. 13452 * Switches the *cred_otwp to base_cr. 13453 */ 13454 static void 13455 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13456 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13457 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13458 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13459 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13460 { 13461 nfs4_open_owner_t *oop = *oopp; 13462 nfs4_open_stream_t *osp = *ospp; 13463 nfs4_lock_owner_t *lop = *lopp; 13464 nfs_argop4 *argop = (*argspp)->array; 13465 13466 if (*did_start_fop) { 13467 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13468 needrecov); 13469 *did_start_fop = FALSE; 13470 } 13471 ASSERT((*argspp)->array_len == 2); 13472 if (argop[1].argop == OP_LOCK) 13473 nfs4args_lock_free(&argop[1]); 13474 else if (argop[1].argop == OP_LOCKT) 13475 nfs4args_lockt_free(&argop[1]); 13476 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13477 if (!error) 13478 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13479 *argspp = NULL; 13480 *respp = NULL; 13481 13482 if (lop) { 13483 nfs4_end_lock_seqid_sync(lop); 13484 lock_owner_rele(lop); 13485 *lopp = NULL; 13486 } 13487 13488 /* need to free up the reference on osp for lock args */ 13489 if (osp != NULL) { 13490 open_stream_rele(osp, VTOR4(vp)); 13491 *ospp = NULL; 13492 } 13493 13494 /* need to free up the reference on oop for lock args */ 13495 if (oop != NULL) { 13496 nfs4_end_open_seqid_sync(oop); 13497 open_owner_rele(oop); 13498 *oopp = NULL; 13499 } 13500 13501 crfree(*cred_otwp); 13502 *cred_otwp = base_cr; 13503 crhold(*cred_otwp); 13504 } 13505 13506 /* 13507 * Function to process the client's recovery for nfs4frlock. 13508 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13509 * 13510 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13511 * COMPOUND4 args/res for calls that need to retry. 13512 * 13513 * Note: the rp's r_lkserlock is *not* dropped during this path. 13514 */ 13515 static bool_t 13516 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13517 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13518 LOCK4args *lock_args, LOCKU4args *locku_args, 13519 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13520 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13521 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13522 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13523 { 13524 nfs4_open_owner_t *oop = *oopp; 13525 nfs4_open_stream_t *osp = *ospp; 13526 nfs4_lock_owner_t *lop = *lopp; 13527 13528 bool_t abort, retry; 13529 13530 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13531 ASSERT((*argspp) != NULL); 13532 ASSERT((*respp) != NULL); 13533 if (lock_args || locku_args) 13534 ASSERT(lop != NULL); 13535 13536 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13537 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13538 13539 retry = TRUE; 13540 abort = FALSE; 13541 if (needrecov) { 13542 nfs4_bseqid_entry_t *bsep = NULL; 13543 nfs_opnum4 op; 13544 13545 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13546 13547 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13548 seqid4 seqid; 13549 13550 if (lock_args) { 13551 if (lock_args->locker.new_lock_owner == TRUE) 13552 seqid = lock_args->locker.locker4_u. 13553 open_owner.open_seqid; 13554 else 13555 seqid = lock_args->locker.locker4_u. 13556 lock_owner.lock_seqid; 13557 } else if (locku_args) { 13558 seqid = locku_args->seqid; 13559 } else { 13560 seqid = 0; 13561 } 13562 13563 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13564 flk->l_pid, (*argspp)->ctag, seqid); 13565 } 13566 13567 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13568 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13569 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13570 NULL, op, bsep); 13571 13572 if (bsep) 13573 kmem_free(bsep, sizeof (*bsep)); 13574 } 13575 13576 /* 13577 * Return that we do not want to retry the request for 3 cases: 13578 * 1. If we received EINTR or are bailing out because of a forced 13579 * unmount, we came into this code path just for the sake of 13580 * initiating recovery, we now need to return the error. 13581 * 2. If we have aborted recovery. 13582 * 3. We received NFS4ERR_BAD_SEQID. 13583 */ 13584 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13585 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13586 retry = FALSE; 13587 13588 if (*did_start_fop == TRUE) { 13589 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13590 needrecov); 13591 *did_start_fop = FALSE; 13592 } 13593 13594 if (retry == TRUE) { 13595 nfs_argop4 *argop; 13596 13597 argop = (*argspp)->array; 13598 ASSERT((*argspp)->array_len == 2); 13599 13600 if (argop[1].argop == OP_LOCK) 13601 nfs4args_lock_free(&argop[1]); 13602 else if (argop[1].argop == OP_LOCKT) 13603 nfs4args_lockt_free(&argop[1]); 13604 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13605 if (!ep->error) 13606 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13607 *respp = NULL; 13608 *argspp = NULL; 13609 } 13610 13611 if (lop != NULL) { 13612 nfs4_end_lock_seqid_sync(lop); 13613 lock_owner_rele(lop); 13614 } 13615 13616 *lopp = NULL; 13617 13618 /* need to free up the reference on osp for lock args */ 13619 if (osp != NULL) { 13620 open_stream_rele(osp, rp); 13621 *ospp = NULL; 13622 } 13623 13624 /* need to free up the reference on oop for lock args */ 13625 if (oop != NULL) { 13626 nfs4_end_open_seqid_sync(oop); 13627 open_owner_rele(oop); 13628 *oopp = NULL; 13629 } 13630 13631 return (retry); 13632 } 13633 13634 /* 13635 * Handles the successful reply from the server for nfs4frlock. 13636 */ 13637 static void 13638 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13639 vnode_t *vp, int flag, u_offset_t offset, 13640 nfs4_lost_rqst_t *resend_rqstp) 13641 { 13642 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13643 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13644 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13645 if (ctype == NFS4_LCK_CTYPE_NORM) { 13646 flk->l_pid = ttoproc(curthread)->p_pid; 13647 /* 13648 * We do not register lost locks locally in 13649 * the 'resend' case since the user/application 13650 * doesn't think we have the lock. 13651 */ 13652 ASSERT(!resend_rqstp); 13653 nfs4_register_lock_locally(vp, flk, flag, offset); 13654 } 13655 } 13656 } 13657 13658 /* 13659 * Handle the DENIED reply from the server for nfs4frlock. 13660 * Returns TRUE if we should retry the request; FALSE otherwise. 13661 * 13662 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13663 * COMPOUND4 args/res for calls that need to retry. Can also 13664 * drop and regrab the r_lkserlock. 13665 */ 13666 static bool_t 13667 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13668 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13669 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13670 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13671 nfs4_recov_state_t *recov_statep, int needrecov, 13672 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13673 clock_t *tick_delayp, short *whencep, int *errorp, 13674 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13675 bool_t *skip_get_err) 13676 { 13677 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13678 13679 if (lock_args) { 13680 nfs4_open_owner_t *oop = *oopp; 13681 nfs4_open_stream_t *osp = *ospp; 13682 nfs4_lock_owner_t *lop = *lopp; 13683 int intr; 13684 13685 /* 13686 * Blocking lock needs to sleep and retry from the request. 13687 * 13688 * Do not block and wait for 'resend' or 'reinstate' 13689 * lock requests, just return the error. 13690 * 13691 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13692 */ 13693 if (cmd == F_SETLKW) { 13694 rnode4_t *rp = VTOR4(vp); 13695 nfs_argop4 *argop = (*argspp)->array; 13696 13697 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13698 13699 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13700 recov_statep, needrecov); 13701 *did_start_fop = FALSE; 13702 ASSERT((*argspp)->array_len == 2); 13703 if (argop[1].argop == OP_LOCK) 13704 nfs4args_lock_free(&argop[1]); 13705 else if (argop[1].argop == OP_LOCKT) 13706 nfs4args_lockt_free(&argop[1]); 13707 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13708 if (*respp) 13709 (void) xdr_free(xdr_COMPOUND4res_clnt, 13710 (caddr_t)*respp); 13711 *argspp = NULL; 13712 *respp = NULL; 13713 nfs4_end_lock_seqid_sync(lop); 13714 lock_owner_rele(lop); 13715 *lopp = NULL; 13716 if (osp != NULL) { 13717 open_stream_rele(osp, rp); 13718 *ospp = NULL; 13719 } 13720 if (oop != NULL) { 13721 nfs4_end_open_seqid_sync(oop); 13722 open_owner_rele(oop); 13723 *oopp = NULL; 13724 } 13725 13726 nfs_rw_exit(&rp->r_lkserlock); 13727 13728 intr = nfs4_block_and_wait(tick_delayp, rp); 13729 13730 if (intr) { 13731 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13732 RW_WRITER, FALSE); 13733 *errorp = EINTR; 13734 return (FALSE); 13735 } 13736 13737 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13738 RW_WRITER, FALSE); 13739 13740 /* 13741 * Make sure we are still safe to lock with 13742 * regards to mmapping. 13743 */ 13744 if (!nfs4_safelock(vp, flk, cr)) { 13745 *errorp = EAGAIN; 13746 return (FALSE); 13747 } 13748 13749 return (TRUE); 13750 } 13751 if (ctype == NFS4_LCK_CTYPE_NORM) 13752 *errorp = EAGAIN; 13753 *skip_get_err = TRUE; 13754 flk->l_whence = 0; 13755 *whencep = 0; 13756 return (FALSE); 13757 } else if (lockt_args) { 13758 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13759 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13760 13761 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13762 flk, lockt_args); 13763 13764 /* according to NLM code */ 13765 *errorp = 0; 13766 *whencep = 0; 13767 *skip_get_err = TRUE; 13768 return (FALSE); 13769 } 13770 return (FALSE); 13771 } 13772 13773 /* 13774 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13775 */ 13776 static void 13777 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13778 { 13779 switch (resp->status) { 13780 case NFS4ERR_ACCESS: 13781 case NFS4ERR_ADMIN_REVOKED: 13782 case NFS4ERR_BADHANDLE: 13783 case NFS4ERR_BAD_RANGE: 13784 case NFS4ERR_BAD_SEQID: 13785 case NFS4ERR_BAD_STATEID: 13786 case NFS4ERR_BADXDR: 13787 case NFS4ERR_DEADLOCK: 13788 case NFS4ERR_DELAY: 13789 case NFS4ERR_EXPIRED: 13790 case NFS4ERR_FHEXPIRED: 13791 case NFS4ERR_GRACE: 13792 case NFS4ERR_INVAL: 13793 case NFS4ERR_ISDIR: 13794 case NFS4ERR_LEASE_MOVED: 13795 case NFS4ERR_LOCK_NOTSUPP: 13796 case NFS4ERR_LOCK_RANGE: 13797 case NFS4ERR_MOVED: 13798 case NFS4ERR_NOFILEHANDLE: 13799 case NFS4ERR_NO_GRACE: 13800 case NFS4ERR_OLD_STATEID: 13801 case NFS4ERR_OPENMODE: 13802 case NFS4ERR_RECLAIM_BAD: 13803 case NFS4ERR_RECLAIM_CONFLICT: 13804 case NFS4ERR_RESOURCE: 13805 case NFS4ERR_SERVERFAULT: 13806 case NFS4ERR_STALE: 13807 case NFS4ERR_STALE_CLIENTID: 13808 case NFS4ERR_STALE_STATEID: 13809 return; 13810 default: 13811 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13812 "nfs4frlock_results_default: got unrecognizable " 13813 "res.status %d", resp->status)); 13814 *errorp = NFS4ERR_INVAL; 13815 } 13816 } 13817 13818 /* 13819 * The lock request was successful, so update the client's state. 13820 */ 13821 static void 13822 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13823 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13824 vnode_t *vp, flock64_t *flk, cred_t *cr, 13825 nfs4_lost_rqst_t *resend_rqstp) 13826 { 13827 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13828 13829 if (lock_args) { 13830 LOCK4res *lock_res; 13831 13832 lock_res = &resop->nfs_resop4_u.oplock; 13833 /* update the stateid with server's response */ 13834 13835 if (lock_args->locker.new_lock_owner == TRUE) { 13836 mutex_enter(&lop->lo_lock); 13837 lop->lo_just_created = NFS4_PERM_CREATED; 13838 mutex_exit(&lop->lo_lock); 13839 } 13840 13841 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13842 13843 /* 13844 * If the lock was the result of a resending a lost 13845 * request, we've synched up the stateid and seqid 13846 * with the server, but now the server might be out of sync 13847 * with what the application thinks it has for locks. 13848 * Clean that up here. It's unclear whether we should do 13849 * this even if the filesystem has been forcibly unmounted. 13850 * For most servers, it's probably wasted effort, but 13851 * RFC3530 lets servers require that unlocks exactly match 13852 * the locks that are held. 13853 */ 13854 if (resend_rqstp != NULL && 13855 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 13856 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 13857 } else { 13858 flk->l_whence = 0; 13859 } 13860 } else if (locku_args) { 13861 LOCKU4res *locku_res; 13862 13863 locku_res = &resop->nfs_resop4_u.oplocku; 13864 13865 /* Update the stateid with the server's response */ 13866 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 13867 } else if (lockt_args) { 13868 /* Switch the lock type to express success, see fcntl */ 13869 flk->l_type = F_UNLCK; 13870 flk->l_whence = 0; 13871 } 13872 } 13873 13874 /* 13875 * Do final cleanup before exiting nfs4frlock. 13876 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13877 * COMPOUND4 args/res for calls that haven't already. 13878 */ 13879 static void 13880 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 13881 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 13882 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 13883 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13884 short whence, u_offset_t offset, struct lm_sysid *ls, 13885 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 13886 bool_t did_start_fop, bool_t skip_get_err, 13887 cred_t *cred_otw, cred_t *cred) 13888 { 13889 mntinfo4_t *mi = VTOMI4(vp); 13890 rnode4_t *rp = VTOR4(vp); 13891 int error = *errorp; 13892 nfs_argop4 *argop; 13893 13894 ASSERT(nfs_zone() == mi->mi_zone); 13895 /* 13896 * The client recovery code wants the raw status information, 13897 * so don't map the NFS status code to an errno value for 13898 * non-normal call types. 13899 */ 13900 if (ctype == NFS4_LCK_CTYPE_NORM) { 13901 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 13902 *errorp = geterrno4(resp->status); 13903 if (did_start_fop == TRUE) 13904 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 13905 needrecov); 13906 13907 /* 13908 * We've established a new lock on the server, so invalidate 13909 * the pages associated with the vnode to get the most up to 13910 * date pages from the server after acquiring the lock. We 13911 * want to be sure that the read operation gets the newest data. 13912 * N.B. 13913 * We used to do this in nfs4frlock_results_ok but that doesn't 13914 * work since VOP_PUTPAGE can call nfs4_commit which calls 13915 * nfs4_start_fop. We flush the pages below after calling 13916 * nfs4_end_fop above 13917 */ 13918 if (!error && resp && resp->status == NFS4_OK) { 13919 int error; 13920 13921 error = VOP_PUTPAGE(vp, (u_offset_t)0, 13922 0, B_INVAL, cred, NULL); 13923 13924 if (error && (error == ENOSPC || error == EDQUOT)) { 13925 rnode4_t *rp = VTOR4(vp); 13926 13927 mutex_enter(&rp->r_statelock); 13928 if (!rp->r_error) 13929 rp->r_error = error; 13930 mutex_exit(&rp->r_statelock); 13931 } 13932 } 13933 } 13934 if (argsp) { 13935 ASSERT(argsp->array_len == 2); 13936 argop = argsp->array; 13937 if (argop[1].argop == OP_LOCK) 13938 nfs4args_lock_free(&argop[1]); 13939 else if (argop[1].argop == OP_LOCKT) 13940 nfs4args_lockt_free(&argop[1]); 13941 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13942 if (resp) 13943 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 13944 } 13945 13946 /* free the reference on the lock owner */ 13947 if (lop != NULL) { 13948 nfs4_end_lock_seqid_sync(lop); 13949 lock_owner_rele(lop); 13950 } 13951 13952 /* need to free up the reference on osp for lock args */ 13953 if (osp != NULL) 13954 open_stream_rele(osp, rp); 13955 13956 /* need to free up the reference on oop for lock args */ 13957 if (oop != NULL) { 13958 nfs4_end_open_seqid_sync(oop); 13959 open_owner_rele(oop); 13960 } 13961 13962 (void) convoff(vp, flk, whence, offset); 13963 13964 lm_rel_sysid(ls); 13965 13966 /* 13967 * Record debug information in the event we get EINVAL. 13968 */ 13969 mutex_enter(&mi->mi_lock); 13970 if (*errorp == EINVAL && (lock_args || locku_args) && 13971 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 13972 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 13973 zcmn_err(getzoneid(), CE_NOTE, 13974 "%s operation failed with " 13975 "EINVAL probably since the server, %s," 13976 " doesn't support POSIX style locking", 13977 lock_args ? "LOCK" : "LOCKU", 13978 mi->mi_curr_serv->sv_hostname); 13979 mi->mi_flags |= MI4_LOCK_DEBUG; 13980 } 13981 } 13982 mutex_exit(&mi->mi_lock); 13983 13984 if (cred_otw) 13985 crfree(cred_otw); 13986 } 13987 13988 /* 13989 * This calls the server and the local locking code. 13990 * 13991 * Client locks are registerred locally by oring the sysid with 13992 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 13993 * We need to distinguish between the two to avoid collision in case one 13994 * machine is used as both client and server. 13995 * 13996 * Blocking lock requests will continually retry to acquire the lock 13997 * forever. 13998 * 13999 * The ctype is defined as follows: 14000 * NFS4_LCK_CTYPE_NORM: normal lock request. 14001 * 14002 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 14003 * recovery, get the pid from flk instead of curproc, and don't reregister 14004 * the lock locally. 14005 * 14006 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 14007 * that we will use the information passed in via resend_rqstp to setup the 14008 * lock/locku request. This resend is the exact same request as the 'lost 14009 * lock', and is initiated by the recovery framework. A successful resend 14010 * request can initiate one or more reinstate requests. 14011 * 14012 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 14013 * does not trigger additional reinstate requests. This lock call type is 14014 * set for setting the v4 server's locking state back to match what the 14015 * client's local locking state is in the event of a received 'lost lock'. 14016 * 14017 * Errors are returned via the nfs4_error_t parameter. 14018 */ 14019 void 14020 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 14021 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 14022 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 14023 { 14024 COMPOUND4args_clnt args, *argsp = NULL; 14025 COMPOUND4res_clnt res, *resp = NULL; 14026 nfs_argop4 *argop; 14027 nfs_resop4 *resop; 14028 rnode4_t *rp; 14029 int doqueue = 1; 14030 clock_t tick_delay; /* delay in clock ticks */ 14031 struct lm_sysid *ls; 14032 LOCK4args *lock_args = NULL; 14033 LOCKU4args *locku_args = NULL; 14034 LOCKT4args *lockt_args = NULL; 14035 nfs4_open_owner_t *oop = NULL; 14036 nfs4_open_stream_t *osp = NULL; 14037 nfs4_lock_owner_t *lop = NULL; 14038 bool_t needrecov = FALSE; 14039 nfs4_recov_state_t recov_state; 14040 short whence; 14041 nfs4_op_hint_t op_hint; 14042 nfs4_lost_rqst_t lost_rqst; 14043 bool_t retry = FALSE; 14044 bool_t did_start_fop = FALSE; 14045 bool_t skip_get_err = FALSE; 14046 cred_t *cred_otw = NULL; 14047 bool_t recovonly; /* just queue request */ 14048 int frc_no_reclaim = 0; 14049 #ifdef DEBUG 14050 char *name; 14051 #endif 14052 14053 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14054 14055 #ifdef DEBUG 14056 name = fn_name(VTOSV(vp)->sv_name); 14057 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 14058 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 14059 "length %"PRIu64", pid %d, sysid %d, call type %s, " 14060 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 14061 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 14062 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 14063 resend_rqstp ? "TRUE" : "FALSE")); 14064 kmem_free(name, MAXNAMELEN); 14065 #endif 14066 14067 nfs4_error_zinit(ep); 14068 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14069 if (ep->error) 14070 return; 14071 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14072 if (ep->error) 14073 return; 14074 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14075 vp, cr, &cred_otw); 14076 14077 recov_retry: 14078 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14079 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14080 rp = VTOR4(vp); 14081 14082 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14083 &did_start_fop, &recovonly); 14084 14085 if (ep->error) 14086 goto out; 14087 14088 if (recovonly) { 14089 /* 14090 * Leave the request for the recovery system to deal with. 14091 */ 14092 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14093 ASSERT(cmd != F_GETLK); 14094 ASSERT(flk->l_type == F_UNLCK); 14095 14096 nfs4_error_init(ep, EINTR); 14097 needrecov = TRUE; 14098 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14099 if (lop != NULL) { 14100 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14101 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14102 (void) nfs4_start_recovery(ep, 14103 VTOMI4(vp), vp, NULL, NULL, 14104 (lost_rqst.lr_op == OP_LOCK || 14105 lost_rqst.lr_op == OP_LOCKU) ? 14106 &lost_rqst : NULL, OP_LOCKU, NULL); 14107 lock_owner_rele(lop); 14108 lop = NULL; 14109 } 14110 flk->l_pid = curproc->p_pid; 14111 nfs4_register_lock_locally(vp, flk, flag, offset); 14112 goto out; 14113 } 14114 14115 /* putfh directory fh */ 14116 argop[0].argop = OP_CPUTFH; 14117 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14118 14119 /* 14120 * Set up the over-the-wire arguments and get references to the 14121 * open owner, etc. 14122 */ 14123 14124 if (ctype == NFS4_LCK_CTYPE_RESEND || 14125 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14126 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14127 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14128 } else { 14129 bool_t go_otw = TRUE; 14130 14131 ASSERT(resend_rqstp == NULL); 14132 14133 switch (cmd) { 14134 case F_GETLK: 14135 case F_O_GETLK: 14136 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14137 &lockt_args, argsp, flk, rp); 14138 break; 14139 case F_SETLKW: 14140 case F_SETLK: 14141 if (flk->l_type == F_UNLCK) 14142 nfs4frlock_setup_locku_args(ctype, 14143 &argop[1], &locku_args, flk, 14144 &lop, ep, argsp, 14145 vp, flag, offset, cr, 14146 &skip_get_err, &go_otw); 14147 else 14148 nfs4frlock_setup_lock_args(ctype, 14149 &lock_args, &oop, &osp, &lop, &argop[1], 14150 argsp, flk, cmd, vp, cr, ep); 14151 14152 if (ep->error) 14153 goto out; 14154 14155 switch (ep->stat) { 14156 case NFS4_OK: 14157 break; 14158 case NFS4ERR_DELAY: 14159 /* recov thread never gets this error */ 14160 ASSERT(resend_rqstp == NULL); 14161 ASSERT(did_start_fop); 14162 14163 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14164 &recov_state, TRUE); 14165 did_start_fop = FALSE; 14166 if (argop[1].argop == OP_LOCK) 14167 nfs4args_lock_free(&argop[1]); 14168 else if (argop[1].argop == OP_LOCKT) 14169 nfs4args_lockt_free(&argop[1]); 14170 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14171 argsp = NULL; 14172 goto recov_retry; 14173 default: 14174 ep->error = EIO; 14175 goto out; 14176 } 14177 break; 14178 default: 14179 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14180 "nfs4_frlock: invalid cmd %d", cmd)); 14181 ep->error = EINVAL; 14182 goto out; 14183 } 14184 14185 if (!go_otw) 14186 goto out; 14187 } 14188 14189 /* XXX should we use the local reclock as a cache ? */ 14190 /* 14191 * Unregister the lock with the local locking code before 14192 * contacting the server. This avoids a potential race where 14193 * another process gets notified that it has been granted a lock 14194 * before we can unregister ourselves locally. 14195 */ 14196 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14197 if (ctype == NFS4_LCK_CTYPE_NORM) 14198 flk->l_pid = ttoproc(curthread)->p_pid; 14199 nfs4_register_lock_locally(vp, flk, flag, offset); 14200 } 14201 14202 /* 14203 * Send the server the lock request. Continually loop with a delay 14204 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14205 */ 14206 resp = &res; 14207 14208 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14209 (CE_NOTE, 14210 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14211 rnode4info(rp))); 14212 14213 if (lock_args && frc_no_reclaim) { 14214 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14215 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14216 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14217 lock_args->reclaim = FALSE; 14218 if (did_reclaimp) 14219 *did_reclaimp = 0; 14220 } 14221 14222 /* 14223 * Do the OTW call. 14224 */ 14225 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14226 14227 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14228 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14229 14230 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14231 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14232 "nfs4frlock: needrecov %d", needrecov)); 14233 14234 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14235 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14236 args.ctag); 14237 14238 /* 14239 * Check if one of these mutually exclusive error cases has 14240 * happened: 14241 * need to swap credentials due to access error 14242 * recovery is needed 14243 * different error (only known case is missing Kerberos ticket) 14244 */ 14245 14246 if ((ep->error == EACCES || 14247 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14248 cred_otw != cr) { 14249 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14250 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14251 cr, &cred_otw); 14252 goto recov_retry; 14253 } 14254 14255 if (needrecov) { 14256 /* 14257 * LOCKT requests don't need to recover from lost 14258 * requests since they don't create/modify state. 14259 */ 14260 if ((ep->error == EINTR || 14261 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14262 lockt_args) 14263 goto out; 14264 /* 14265 * Do not attempt recovery for requests initiated by 14266 * the recovery framework. Let the framework redrive them. 14267 */ 14268 if (ctype != NFS4_LCK_CTYPE_NORM) 14269 goto out; 14270 else { 14271 ASSERT(resend_rqstp == NULL); 14272 } 14273 14274 nfs4frlock_save_lost_rqst(ctype, ep->error, 14275 flk_to_locktype(cmd, flk->l_type), 14276 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14277 14278 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14279 &resp, lock_args, locku_args, &oop, &osp, &lop, 14280 rp, vp, &recov_state, op_hint, &did_start_fop, 14281 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14282 14283 if (retry) { 14284 ASSERT(oop == NULL); 14285 ASSERT(osp == NULL); 14286 ASSERT(lop == NULL); 14287 goto recov_retry; 14288 } 14289 goto out; 14290 } 14291 14292 /* 14293 * Bail out if have reached this point with ep->error set. Can 14294 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14295 * This happens if Kerberos ticket has expired or has been 14296 * destroyed. 14297 */ 14298 if (ep->error != 0) 14299 goto out; 14300 14301 /* 14302 * Process the reply. 14303 */ 14304 switch (resp->status) { 14305 case NFS4_OK: 14306 resop = &resp->array[1]; 14307 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14308 resend_rqstp); 14309 /* 14310 * Have a successful lock operation, now update state. 14311 */ 14312 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14313 resop, lop, vp, flk, cr, resend_rqstp); 14314 break; 14315 14316 case NFS4ERR_DENIED: 14317 resop = &resp->array[1]; 14318 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14319 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14320 &recov_state, needrecov, &argsp, &resp, 14321 &tick_delay, &whence, &ep->error, resop, cr, 14322 &did_start_fop, &skip_get_err); 14323 14324 if (retry) { 14325 ASSERT(oop == NULL); 14326 ASSERT(osp == NULL); 14327 ASSERT(lop == NULL); 14328 goto recov_retry; 14329 } 14330 break; 14331 /* 14332 * If the server won't let us reclaim, fall-back to trying to lock 14333 * the file from scratch. Code elsewhere will check the changeinfo 14334 * to ensure the file hasn't been changed. 14335 */ 14336 case NFS4ERR_NO_GRACE: 14337 if (lock_args && lock_args->reclaim == TRUE) { 14338 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14339 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14340 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14341 frc_no_reclaim = 1; 14342 /* clean up before retrying */ 14343 needrecov = 0; 14344 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14345 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14346 &recov_state, op_hint, &did_start_fop, NULL, flk); 14347 goto recov_retry; 14348 } 14349 /* FALLTHROUGH */ 14350 14351 default: 14352 nfs4frlock_results_default(resp, &ep->error); 14353 break; 14354 } 14355 out: 14356 /* 14357 * Process and cleanup from error. Make interrupted unlock 14358 * requests look successful, since they will be handled by the 14359 * client recovery code. 14360 */ 14361 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14362 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14363 lock_args, locku_args, did_start_fop, 14364 skip_get_err, cred_otw, cr); 14365 14366 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14367 (cmd == F_SETLK || cmd == F_SETLKW)) 14368 ep->error = 0; 14369 } 14370 14371 /* 14372 * nfs4_safelock: 14373 * 14374 * Return non-zero if the given lock request can be handled without 14375 * violating the constraints on concurrent mapping and locking. 14376 */ 14377 14378 static int 14379 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14380 { 14381 rnode4_t *rp = VTOR4(vp); 14382 struct vattr va; 14383 int error; 14384 14385 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14386 ASSERT(rp->r_mapcnt >= 0); 14387 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14388 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14389 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14390 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14391 14392 if (rp->r_mapcnt == 0) 14393 return (1); /* always safe if not mapped */ 14394 14395 /* 14396 * If the file is already mapped and there are locks, then they 14397 * should be all safe locks. So adding or removing a lock is safe 14398 * as long as the new request is safe (i.e., whole-file, meaning 14399 * length and starting offset are both zero). 14400 */ 14401 14402 if (bfp->l_start != 0 || bfp->l_len != 0) { 14403 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14404 "cannot lock a memory mapped file unless locking the " 14405 "entire file: start %"PRIx64", len %"PRIx64, 14406 bfp->l_start, bfp->l_len)); 14407 return (0); 14408 } 14409 14410 /* mandatory locking and mapping don't mix */ 14411 va.va_mask = AT_MODE; 14412 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 14413 if (error != 0) { 14414 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14415 "getattr error %d", error)); 14416 return (0); /* treat errors conservatively */ 14417 } 14418 if (MANDLOCK(vp, va.va_mode)) { 14419 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14420 "cannot mandatory lock and mmap a file")); 14421 return (0); 14422 } 14423 14424 return (1); 14425 } 14426 14427 14428 /* 14429 * Register the lock locally within Solaris. 14430 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14431 * recording locks locally. 14432 * 14433 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14434 * are registered locally. 14435 */ 14436 void 14437 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14438 u_offset_t offset) 14439 { 14440 int oldsysid; 14441 int error; 14442 #ifdef DEBUG 14443 char *name; 14444 #endif 14445 14446 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14447 14448 #ifdef DEBUG 14449 name = fn_name(VTOSV(vp)->sv_name); 14450 NFS4_DEBUG(nfs4_client_lock_debug, 14451 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14452 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14453 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14454 flk->l_sysid)); 14455 kmem_free(name, MAXNAMELEN); 14456 #endif 14457 14458 /* register the lock with local locking */ 14459 oldsysid = flk->l_sysid; 14460 flk->l_sysid |= LM_SYSID_CLIENT; 14461 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14462 #ifdef DEBUG 14463 if (error != 0) { 14464 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14465 "nfs4_register_lock_locally: could not register with" 14466 " local locking")); 14467 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14468 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14469 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14470 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14471 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14472 flk->l_type, flk->l_start, flk->l_len)); 14473 (void) reclock(vp, flk, 0, flag, offset, NULL); 14474 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14475 "blocked by pid %d sysid 0x%x type %d " 14476 "off 0x%" PRIx64 " len 0x%" PRIx64, 14477 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14478 flk->l_len)); 14479 } 14480 #endif 14481 flk->l_sysid = oldsysid; 14482 } 14483 14484 /* 14485 * nfs4_lockrelease: 14486 * 14487 * Release any locks on the given vnode that are held by the current 14488 * process. Also removes the lock owner (if one exists) from the rnode's 14489 * list. 14490 */ 14491 static int 14492 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14493 { 14494 flock64_t ld; 14495 int ret, error; 14496 rnode4_t *rp; 14497 nfs4_lock_owner_t *lop; 14498 nfs4_recov_state_t recov_state; 14499 mntinfo4_t *mi; 14500 bool_t possible_orphan = FALSE; 14501 bool_t recovonly; 14502 14503 ASSERT((uintptr_t)vp > KERNELBASE); 14504 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14505 14506 rp = VTOR4(vp); 14507 mi = VTOMI4(vp); 14508 14509 /* 14510 * If we have not locked anything then we can 14511 * just return since we have no work to do. 14512 */ 14513 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14514 return (0); 14515 } 14516 14517 /* 14518 * We need to comprehend that another thread may 14519 * kick off recovery and the lock_owner we have stashed 14520 * in lop might be invalid so we should NOT cache it 14521 * locally! 14522 */ 14523 recov_state.rs_flags = 0; 14524 recov_state.rs_num_retry_despite_err = 0; 14525 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14526 &recovonly); 14527 if (error) { 14528 mutex_enter(&rp->r_statelock); 14529 rp->r_flags |= R4LODANGLERS; 14530 mutex_exit(&rp->r_statelock); 14531 return (error); 14532 } 14533 14534 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14535 14536 /* 14537 * Check if the lock owner might have a lock (request was sent but 14538 * no response was received). Also check if there are any remote 14539 * locks on the file. (In theory we shouldn't have to make this 14540 * second check if there's no lock owner, but for now we'll be 14541 * conservative and do it anyway.) If either condition is true, 14542 * send an unlock for the entire file to the server. 14543 * 14544 * Note that no explicit synchronization is needed here. At worst, 14545 * flk_has_remote_locks() will return a false positive, in which case 14546 * the unlock call wastes time but doesn't harm correctness. 14547 */ 14548 14549 if (lop) { 14550 mutex_enter(&lop->lo_lock); 14551 possible_orphan = lop->lo_pending_rqsts; 14552 mutex_exit(&lop->lo_lock); 14553 lock_owner_rele(lop); 14554 } 14555 14556 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14557 14558 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14559 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14560 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14561 (void *)lop)); 14562 14563 if (possible_orphan || flk_has_remote_locks(vp)) { 14564 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14565 ld.l_whence = 0; /* unlock from start of file */ 14566 ld.l_start = 0; 14567 ld.l_len = 0; /* do entire file */ 14568 14569 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, 14570 cr, NULL); 14571 14572 if (ret != 0) { 14573 /* 14574 * If VOP_FRLOCK fails, make sure we unregister 14575 * local locks before we continue. 14576 */ 14577 ld.l_pid = ttoproc(curthread)->p_pid; 14578 nfs4_register_lock_locally(vp, &ld, flag, offset); 14579 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14580 "nfs4_lockrelease: lock release error on vp" 14581 " %p: error %d.\n", (void *)vp, ret)); 14582 } 14583 } 14584 14585 recov_state.rs_flags = 0; 14586 recov_state.rs_num_retry_despite_err = 0; 14587 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14588 &recovonly); 14589 if (error) { 14590 mutex_enter(&rp->r_statelock); 14591 rp->r_flags |= R4LODANGLERS; 14592 mutex_exit(&rp->r_statelock); 14593 return (error); 14594 } 14595 14596 /* 14597 * So, here we're going to need to retrieve the lock-owner 14598 * again (in case recovery has done a switch-a-roo) and 14599 * remove it because we can. 14600 */ 14601 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14602 14603 if (lop) { 14604 nfs4_rnode_remove_lock_owner(rp, lop); 14605 lock_owner_rele(lop); 14606 } 14607 14608 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14609 return (0); 14610 } 14611 14612 /* 14613 * Wait for 'tick_delay' clock ticks. 14614 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14615 * NOTE: lock_lease_time is in seconds. 14616 * 14617 * XXX For future improvements, should implement a waiting queue scheme. 14618 */ 14619 static int 14620 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14621 { 14622 long milliseconds_delay; 14623 time_t lock_lease_time; 14624 14625 /* wait tick_delay clock ticks or siginteruptus */ 14626 if (delay_sig(*tick_delay)) { 14627 return (EINTR); 14628 } 14629 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14630 "reissue the lock request: blocked for %ld clock ticks: %ld " 14631 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14632 14633 /* get the lease time */ 14634 lock_lease_time = r2lease_time(rp); 14635 14636 /* drv_hztousec converts ticks to microseconds */ 14637 milliseconds_delay = drv_hztousec(*tick_delay) / 1000; 14638 if (milliseconds_delay < lock_lease_time * 1000) { 14639 *tick_delay = 2 * *tick_delay; 14640 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000) 14641 *tick_delay = drv_usectohz(lock_lease_time*1000*1000); 14642 } 14643 return (0); 14644 } 14645 14646 14647 void 14648 nfs4_vnops_init(void) 14649 { 14650 } 14651 14652 void 14653 nfs4_vnops_fini(void) 14654 { 14655 } 14656 14657 /* 14658 * Return a reference to the directory (parent) vnode for a given vnode, 14659 * using the saved pathname information and the directory file handle. The 14660 * caller is responsible for disposing of the reference. 14661 * Returns zero or an errno value. 14662 * 14663 * Caller should set need_start_op to FALSE if it is the recovery 14664 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14665 */ 14666 int 14667 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14668 { 14669 svnode_t *svnp; 14670 vnode_t *dvp = NULL; 14671 servinfo4_t *svp; 14672 nfs4_fname_t *mfname; 14673 int error; 14674 14675 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14676 14677 if (vp->v_flag & VROOT) { 14678 nfs4_sharedfh_t *sfh; 14679 nfs_fh4 fh; 14680 mntinfo4_t *mi; 14681 14682 ASSERT(vp->v_type == VREG); 14683 14684 mi = VTOMI4(vp); 14685 svp = mi->mi_curr_serv; 14686 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14687 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14688 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14689 sfh = sfh4_get(&fh, VTOMI4(vp)); 14690 nfs_rw_exit(&svp->sv_lock); 14691 mfname = mi->mi_fname; 14692 fn_hold(mfname); 14693 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14694 sfh4_rele(&sfh); 14695 14696 if (dvp->v_type == VNON) 14697 dvp->v_type = VDIR; 14698 *dvpp = dvp; 14699 return (0); 14700 } 14701 14702 svnp = VTOSV(vp); 14703 14704 if (svnp == NULL) { 14705 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14706 "shadow node is NULL")); 14707 return (EINVAL); 14708 } 14709 14710 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14711 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14712 "shadow node name or dfh val == NULL")); 14713 return (EINVAL); 14714 } 14715 14716 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14717 (int)need_start_op); 14718 if (error != 0) { 14719 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14720 "nfs4_make_dotdot returned %d", error)); 14721 return (error); 14722 } 14723 if (!dvp) { 14724 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14725 "nfs4_make_dotdot returned a NULL dvp")); 14726 return (EIO); 14727 } 14728 if (dvp->v_type == VNON) 14729 dvp->v_type = VDIR; 14730 ASSERT(dvp->v_type == VDIR); 14731 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14732 mutex_enter(&dvp->v_lock); 14733 dvp->v_flag |= V_XATTRDIR; 14734 mutex_exit(&dvp->v_lock); 14735 } 14736 *dvpp = dvp; 14737 return (0); 14738 } 14739 14740 /* 14741 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14742 * length that fnamep can accept, including the trailing null. 14743 * Returns 0 if okay, returns an errno value if there was a problem. 14744 */ 14745 14746 int 14747 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14748 { 14749 char *fn; 14750 int err = 0; 14751 servinfo4_t *svp; 14752 svnode_t *shvp; 14753 14754 /* 14755 * If the file being opened has VROOT set, then this is 14756 * a "file" mount. sv_name will not be interesting, so 14757 * go back to the servinfo4 to get the original mount 14758 * path and strip off all but the final edge. Otherwise 14759 * just return the name from the shadow vnode. 14760 */ 14761 14762 if (vp->v_flag & VROOT) { 14763 14764 svp = VTOMI4(vp)->mi_curr_serv; 14765 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14766 14767 fn = strrchr(svp->sv_path, '/'); 14768 if (fn == NULL) 14769 err = EINVAL; 14770 else 14771 fn++; 14772 } else { 14773 shvp = VTOSV(vp); 14774 fn = fn_name(shvp->sv_name); 14775 } 14776 14777 if (err == 0) 14778 if (strlen(fn) < maxlen) 14779 (void) strcpy(fnamep, fn); 14780 else 14781 err = ENAMETOOLONG; 14782 14783 if (vp->v_flag & VROOT) 14784 nfs_rw_exit(&svp->sv_lock); 14785 else 14786 kmem_free(fn, MAXNAMELEN); 14787 14788 return (err); 14789 } 14790 14791 /* 14792 * Bookkeeping for a close that doesn't need to go over the wire. 14793 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14794 * it is left at 1. 14795 */ 14796 void 14797 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14798 { 14799 rnode4_t *rp; 14800 mntinfo4_t *mi; 14801 14802 mi = VTOMI4(vp); 14803 rp = VTOR4(vp); 14804 14805 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14806 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14807 ASSERT(nfs_zone() == mi->mi_zone); 14808 ASSERT(mutex_owned(&osp->os_sync_lock)); 14809 ASSERT(*have_lockp); 14810 14811 if (!osp->os_valid || 14812 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14813 return; 14814 } 14815 14816 /* 14817 * This removes the reference obtained at OPEN; ie, 14818 * when the open stream structure was created. 14819 * 14820 * We don't have to worry about calling 'open_stream_rele' 14821 * since we our currently holding a reference to this 14822 * open stream which means the count can not go to 0 with 14823 * this decrement. 14824 */ 14825 ASSERT(osp->os_ref_count >= 2); 14826 osp->os_ref_count--; 14827 osp->os_valid = 0; 14828 mutex_exit(&osp->os_sync_lock); 14829 *have_lockp = 0; 14830 14831 nfs4_dec_state_ref_count(mi); 14832 } 14833 14834 /* 14835 * Close all remaining open streams on the rnode. These open streams 14836 * could be here because: 14837 * - The close attempted at either close or delmap failed 14838 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14839 * - Someone did mknod on a regular file but never opened it 14840 */ 14841 int 14842 nfs4close_all(vnode_t *vp, cred_t *cr) 14843 { 14844 nfs4_open_stream_t *osp; 14845 int error; 14846 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14847 rnode4_t *rp; 14848 14849 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14850 14851 error = 0; 14852 rp = VTOR4(vp); 14853 14854 /* 14855 * At this point, all we know is that the last time 14856 * someone called vn_rele, the count was 1. Since then, 14857 * the vnode could have been re-activated. We want to 14858 * loop through the open streams and close each one, but 14859 * we have to be careful since once we release the rnode 14860 * hash bucket lock, someone else is free to come in and 14861 * re-activate the rnode and add new open streams. The 14862 * strategy is take the rnode hash bucket lock, verify that 14863 * the count is still 1, grab the open stream off the 14864 * head of the list and mark it invalid, then release the 14865 * rnode hash bucket lock and proceed with that open stream. 14866 * This is ok because nfs4close_one() will acquire the proper 14867 * open/create to close/destroy synchronization for open 14868 * streams, and will ensure that if someone has reopened 14869 * the open stream after we've dropped the hash bucket lock 14870 * then we'll just simply return without destroying the 14871 * open stream. 14872 * Repeat until the list is empty. 14873 */ 14874 14875 for (;;) { 14876 14877 /* make sure vnode hasn't been reactivated */ 14878 rw_enter(&rp->r_hashq->r_lock, RW_READER); 14879 mutex_enter(&vp->v_lock); 14880 if (vp->v_count > 1) { 14881 mutex_exit(&vp->v_lock); 14882 rw_exit(&rp->r_hashq->r_lock); 14883 break; 14884 } 14885 /* 14886 * Grabbing r_os_lock before releasing v_lock prevents 14887 * a window where the rnode/open stream could get 14888 * reactivated (and os_force_close set to 0) before we 14889 * had a chance to set os_force_close to 1. 14890 */ 14891 mutex_enter(&rp->r_os_lock); 14892 mutex_exit(&vp->v_lock); 14893 14894 osp = list_head(&rp->r_open_streams); 14895 if (!osp) { 14896 /* nothing left to CLOSE OTW, so return */ 14897 mutex_exit(&rp->r_os_lock); 14898 rw_exit(&rp->r_hashq->r_lock); 14899 break; 14900 } 14901 14902 mutex_enter(&rp->r_statev4_lock); 14903 /* the file can't still be mem mapped */ 14904 ASSERT(rp->r_mapcnt == 0); 14905 if (rp->created_v4) 14906 rp->created_v4 = 0; 14907 mutex_exit(&rp->r_statev4_lock); 14908 14909 /* 14910 * Grab a ref on this open stream; nfs4close_one 14911 * will mark it as invalid 14912 */ 14913 mutex_enter(&osp->os_sync_lock); 14914 osp->os_ref_count++; 14915 osp->os_force_close = 1; 14916 mutex_exit(&osp->os_sync_lock); 14917 mutex_exit(&rp->r_os_lock); 14918 rw_exit(&rp->r_hashq->r_lock); 14919 14920 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 14921 14922 /* Update error if it isn't already non-zero */ 14923 if (error == 0) { 14924 if (e.error) 14925 error = e.error; 14926 else if (e.stat) 14927 error = geterrno4(e.stat); 14928 } 14929 14930 #ifdef DEBUG 14931 nfs4close_all_cnt++; 14932 #endif 14933 /* Release the ref on osp acquired above. */ 14934 open_stream_rele(osp, rp); 14935 14936 /* Proceed to the next open stream, if any */ 14937 } 14938 return (error); 14939 } 14940 14941 /* 14942 * nfs4close_one - close one open stream for a file if needed. 14943 * 14944 * "close_type" indicates which close path this is: 14945 * CLOSE_NORM: close initiated via VOP_CLOSE. 14946 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 14947 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 14948 * the close and release of client state for this open stream 14949 * (unless someone else has the open stream open). 14950 * CLOSE_RESEND: indicates the request is a replay of an earlier request 14951 * (e.g., due to abort because of a signal). 14952 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 14953 * 14954 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 14955 * recovery. Instead, the caller is expected to deal with retries. 14956 * 14957 * The caller can either pass in the osp ('provided_osp') or not. 14958 * 14959 * 'access_bits' represents the access we are closing/downgrading. 14960 * 14961 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 14962 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 14963 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 14964 * 14965 * Errors are returned via the nfs4_error_t. 14966 */ 14967 void 14968 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 14969 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 14970 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 14971 uint_t mmap_flags) 14972 { 14973 nfs4_open_owner_t *oop; 14974 nfs4_open_stream_t *osp = NULL; 14975 int retry = 0; 14976 int num_retries = NFS4_NUM_RECOV_RETRIES; 14977 rnode4_t *rp; 14978 mntinfo4_t *mi; 14979 nfs4_recov_state_t recov_state; 14980 cred_t *cred_otw = NULL; 14981 bool_t recovonly = FALSE; 14982 int isrecov; 14983 int force_close; 14984 int close_failed = 0; 14985 int did_dec_count = 0; 14986 int did_start_op = 0; 14987 int did_force_recovlock = 0; 14988 int did_start_seqid_sync = 0; 14989 int have_sync_lock = 0; 14990 14991 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14992 14993 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 14994 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 14995 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 14996 len, maxprot, mmap_flags, access_bits)); 14997 14998 nfs4_error_zinit(ep); 14999 rp = VTOR4(vp); 15000 mi = VTOMI4(vp); 15001 isrecov = (close_type == CLOSE_RESEND || 15002 close_type == CLOSE_AFTER_RESEND); 15003 15004 /* 15005 * First get the open owner. 15006 */ 15007 if (!provided_osp) { 15008 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 15009 } else { 15010 oop = provided_osp->os_open_owner; 15011 ASSERT(oop != NULL); 15012 open_owner_hold(oop); 15013 } 15014 15015 if (!oop) { 15016 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15017 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 15018 "close type %d", (void *)rp, (void *)mi, (void *)cr, 15019 (void *)provided_osp, close_type)); 15020 ep->error = EIO; 15021 goto out; 15022 } 15023 15024 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 15025 recov_retry: 15026 osp = NULL; 15027 close_failed = 0; 15028 force_close = (close_type == CLOSE_FORCE); 15029 retry = 0; 15030 did_start_op = 0; 15031 did_force_recovlock = 0; 15032 did_start_seqid_sync = 0; 15033 have_sync_lock = 0; 15034 recovonly = FALSE; 15035 recov_state.rs_flags = 0; 15036 recov_state.rs_num_retry_despite_err = 0; 15037 15038 /* 15039 * Second synchronize with recovery. 15040 */ 15041 if (!isrecov) { 15042 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 15043 &recov_state, &recovonly); 15044 if (!ep->error) { 15045 did_start_op = 1; 15046 } else { 15047 close_failed = 1; 15048 /* 15049 * If we couldn't get start_fop, but have to 15050 * cleanup state, then at least acquire the 15051 * mi_recovlock so we can synchronize with 15052 * recovery. 15053 */ 15054 if (close_type == CLOSE_FORCE) { 15055 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 15056 RW_READER, FALSE); 15057 did_force_recovlock = 1; 15058 } else 15059 goto out; 15060 } 15061 } 15062 15063 /* 15064 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15065 * set 'recovonly' to TRUE since most likely this is due to 15066 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15067 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15068 * to retry, causing us to loop until recovery finishes. Plus we 15069 * don't need protection over the open seqid since we're not going 15070 * OTW, hence don't need to use the seqid. 15071 */ 15072 if (recovonly == FALSE) { 15073 /* need to grab the open owner sync before 'os_sync_lock' */ 15074 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15075 if (ep->error == EAGAIN) { 15076 ASSERT(!isrecov); 15077 if (did_start_op) 15078 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15079 &recov_state, TRUE); 15080 if (did_force_recovlock) 15081 nfs_rw_exit(&mi->mi_recovlock); 15082 goto recov_retry; 15083 } 15084 did_start_seqid_sync = 1; 15085 } 15086 15087 /* 15088 * Third get an open stream and acquire 'os_sync_lock' to 15089 * sychronize the opening/creating of an open stream with the 15090 * closing/destroying of an open stream. 15091 */ 15092 if (!provided_osp) { 15093 /* returns with 'os_sync_lock' held */ 15094 osp = find_open_stream(oop, rp); 15095 if (!osp) { 15096 ep->error = EIO; 15097 goto out; 15098 } 15099 } else { 15100 osp = provided_osp; 15101 open_stream_hold(osp); 15102 mutex_enter(&osp->os_sync_lock); 15103 } 15104 have_sync_lock = 1; 15105 15106 ASSERT(oop == osp->os_open_owner); 15107 15108 /* 15109 * Fourth, do any special pre-OTW CLOSE processing 15110 * based on the specific close type. 15111 */ 15112 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15113 !did_dec_count) { 15114 ASSERT(osp->os_open_ref_count > 0); 15115 osp->os_open_ref_count--; 15116 did_dec_count = 1; 15117 if (osp->os_open_ref_count == 0) 15118 osp->os_final_close = 1; 15119 } 15120 15121 if (close_type == CLOSE_FORCE) { 15122 /* see if somebody reopened the open stream. */ 15123 if (!osp->os_force_close) { 15124 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15125 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15126 "was reopened, vp %p", (void *)osp, (void *)vp)); 15127 ep->error = 0; 15128 ep->stat = NFS4_OK; 15129 goto out; 15130 } 15131 15132 if (!osp->os_final_close && !did_dec_count) { 15133 osp->os_open_ref_count--; 15134 did_dec_count = 1; 15135 } 15136 15137 /* 15138 * We can't depend on os_open_ref_count being 0 due to the 15139 * way executables are opened (VN_RELE to match a VOP_OPEN). 15140 */ 15141 #ifdef NOTYET 15142 ASSERT(osp->os_open_ref_count == 0); 15143 #endif 15144 if (osp->os_open_ref_count != 0) { 15145 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15146 "nfs4close_one: should panic here on an " 15147 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15148 "since this is probably the exec problem.")); 15149 15150 osp->os_open_ref_count = 0; 15151 } 15152 15153 /* 15154 * There is the possibility that nfs4close_one() 15155 * for close_type == CLOSE_DELMAP couldn't find the 15156 * open stream, thus couldn't decrement its os_mapcnt; 15157 * therefore we can't use this ASSERT yet. 15158 */ 15159 #ifdef NOTYET 15160 ASSERT(osp->os_mapcnt == 0); 15161 #endif 15162 osp->os_mapcnt = 0; 15163 } 15164 15165 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15166 ASSERT(osp->os_mapcnt >= btopr(len)); 15167 15168 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15169 osp->os_mmap_write -= btopr(len); 15170 if (maxprot & PROT_READ) 15171 osp->os_mmap_read -= btopr(len); 15172 if (maxprot & PROT_EXEC) 15173 osp->os_mmap_read -= btopr(len); 15174 /* mirror the PROT_NONE check in nfs4_addmap() */ 15175 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15176 !(maxprot & PROT_EXEC)) 15177 osp->os_mmap_read -= btopr(len); 15178 osp->os_mapcnt -= btopr(len); 15179 did_dec_count = 1; 15180 } 15181 15182 if (recovonly) { 15183 nfs4_lost_rqst_t lost_rqst; 15184 15185 /* request should not already be in recovery queue */ 15186 ASSERT(lrp == NULL); 15187 nfs4_error_init(ep, EINTR); 15188 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15189 osp, cred_otw, vp); 15190 mutex_exit(&osp->os_sync_lock); 15191 have_sync_lock = 0; 15192 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15193 lost_rqst.lr_op == OP_CLOSE ? 15194 &lost_rqst : NULL, OP_CLOSE, NULL); 15195 close_failed = 1; 15196 force_close = 0; 15197 goto close_cleanup; 15198 } 15199 15200 /* 15201 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15202 * we stopped operating on the open owner's <old oo_name, old seqid> 15203 * space, which means we stopped operating on the open stream 15204 * too. So don't go OTW (as the seqid is likely bad, and the 15205 * stateid could be stale, potentially triggering a false 15206 * setclientid), and just clean up the client's internal state. 15207 */ 15208 if (osp->os_orig_oo_name != oop->oo_name) { 15209 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15210 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15211 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15212 "oo_name %" PRIx64")", 15213 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15214 oop->oo_name)); 15215 close_failed = 1; 15216 } 15217 15218 /* If the file failed recovery, just quit. */ 15219 mutex_enter(&rp->r_statelock); 15220 if (rp->r_flags & R4RECOVERR) { 15221 close_failed = 1; 15222 } 15223 mutex_exit(&rp->r_statelock); 15224 15225 /* 15226 * If the force close path failed to obtain start_fop 15227 * then skip the OTW close and just remove the state. 15228 */ 15229 if (close_failed) 15230 goto close_cleanup; 15231 15232 /* 15233 * Fifth, check to see if there are still mapped pages or other 15234 * opens using this open stream. If there are then we can't 15235 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15236 */ 15237 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15238 nfs4_lost_rqst_t new_lost_rqst; 15239 bool_t needrecov = FALSE; 15240 cred_t *odg_cred_otw = NULL; 15241 seqid4 open_dg_seqid = 0; 15242 15243 if (osp->os_delegation) { 15244 /* 15245 * If this open stream was never OPENed OTW then we 15246 * surely can't DOWNGRADE it (especially since the 15247 * osp->open_stateid is really a delegation stateid 15248 * when os_delegation is 1). 15249 */ 15250 if (access_bits & FREAD) 15251 osp->os_share_acc_read--; 15252 if (access_bits & FWRITE) 15253 osp->os_share_acc_write--; 15254 osp->os_share_deny_none--; 15255 nfs4_error_zinit(ep); 15256 goto out; 15257 } 15258 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15259 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15260 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15261 if (needrecov && !isrecov) { 15262 bool_t abort; 15263 nfs4_bseqid_entry_t *bsep = NULL; 15264 15265 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15266 bsep = nfs4_create_bseqid_entry(oop, NULL, 15267 vp, 0, 15268 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15269 open_dg_seqid); 15270 15271 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15272 oop, osp, odg_cred_otw, vp, access_bits, 0); 15273 mutex_exit(&osp->os_sync_lock); 15274 have_sync_lock = 0; 15275 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15276 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15277 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15278 bsep); 15279 if (odg_cred_otw) 15280 crfree(odg_cred_otw); 15281 if (bsep) 15282 kmem_free(bsep, sizeof (*bsep)); 15283 15284 if (abort == TRUE) 15285 goto out; 15286 15287 if (did_start_seqid_sync) { 15288 nfs4_end_open_seqid_sync(oop); 15289 did_start_seqid_sync = 0; 15290 } 15291 open_stream_rele(osp, rp); 15292 15293 if (did_start_op) 15294 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15295 &recov_state, FALSE); 15296 if (did_force_recovlock) 15297 nfs_rw_exit(&mi->mi_recovlock); 15298 15299 goto recov_retry; 15300 } else { 15301 if (odg_cred_otw) 15302 crfree(odg_cred_otw); 15303 } 15304 goto out; 15305 } 15306 15307 /* 15308 * If this open stream was created as the results of an open 15309 * while holding a delegation, then just release it; no need 15310 * to do an OTW close. Otherwise do a "normal" OTW close. 15311 */ 15312 if (osp->os_delegation) { 15313 nfs4close_notw(vp, osp, &have_sync_lock); 15314 nfs4_error_zinit(ep); 15315 goto out; 15316 } 15317 15318 /* 15319 * If this stream is not valid, we're done. 15320 */ 15321 if (!osp->os_valid) { 15322 nfs4_error_zinit(ep); 15323 goto out; 15324 } 15325 15326 /* 15327 * Last open or mmap ref has vanished, need to do an OTW close. 15328 * First check to see if a close is still necessary. 15329 */ 15330 if (osp->os_failed_reopen) { 15331 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15332 "don't close OTW osp %p since reopen failed.", 15333 (void *)osp)); 15334 /* 15335 * Reopen of the open stream failed, hence the 15336 * stateid of the open stream is invalid/stale, and 15337 * sending this OTW would incorrectly cause another 15338 * round of recovery. In this case, we need to set 15339 * the 'os_valid' bit to 0 so another thread doesn't 15340 * come in and re-open this open stream before 15341 * this "closing" thread cleans up state (decrementing 15342 * the nfs4_server_t's state_ref_count and decrementing 15343 * the os_ref_count). 15344 */ 15345 osp->os_valid = 0; 15346 /* 15347 * This removes the reference obtained at OPEN; ie, 15348 * when the open stream structure was created. 15349 * 15350 * We don't have to worry about calling 'open_stream_rele' 15351 * since we our currently holding a reference to this 15352 * open stream which means the count can not go to 0 with 15353 * this decrement. 15354 */ 15355 ASSERT(osp->os_ref_count >= 2); 15356 osp->os_ref_count--; 15357 nfs4_error_zinit(ep); 15358 close_failed = 0; 15359 goto close_cleanup; 15360 } 15361 15362 ASSERT(osp->os_ref_count > 1); 15363 15364 /* 15365 * Sixth, try the CLOSE OTW. 15366 */ 15367 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15368 close_type, ep, &have_sync_lock); 15369 15370 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15371 /* 15372 * Let the recovery thread be responsible for 15373 * removing the state for CLOSE. 15374 */ 15375 close_failed = 1; 15376 force_close = 0; 15377 retry = 0; 15378 } 15379 15380 /* See if we need to retry with a different cred */ 15381 if ((ep->error == EACCES || 15382 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15383 cred_otw != cr) { 15384 crfree(cred_otw); 15385 cred_otw = cr; 15386 crhold(cred_otw); 15387 retry = 1; 15388 } 15389 15390 if (ep->error || ep->stat) 15391 close_failed = 1; 15392 15393 if (retry && !isrecov && num_retries-- > 0) { 15394 if (have_sync_lock) { 15395 mutex_exit(&osp->os_sync_lock); 15396 have_sync_lock = 0; 15397 } 15398 if (did_start_seqid_sync) { 15399 nfs4_end_open_seqid_sync(oop); 15400 did_start_seqid_sync = 0; 15401 } 15402 open_stream_rele(osp, rp); 15403 15404 if (did_start_op) 15405 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15406 &recov_state, FALSE); 15407 if (did_force_recovlock) 15408 nfs_rw_exit(&mi->mi_recovlock); 15409 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15410 "nfs4close_one: need to retry the close " 15411 "operation")); 15412 goto recov_retry; 15413 } 15414 close_cleanup: 15415 /* 15416 * Seventh and lastly, process our results. 15417 */ 15418 if (close_failed && force_close) { 15419 /* 15420 * It's ok to drop and regrab the 'os_sync_lock' since 15421 * nfs4close_notw() will recheck to make sure the 15422 * "close"/removal of state should happen. 15423 */ 15424 if (!have_sync_lock) { 15425 mutex_enter(&osp->os_sync_lock); 15426 have_sync_lock = 1; 15427 } 15428 /* 15429 * This is last call, remove the ref on the open 15430 * stream created by open and clean everything up. 15431 */ 15432 osp->os_pending_close = 0; 15433 nfs4close_notw(vp, osp, &have_sync_lock); 15434 nfs4_error_zinit(ep); 15435 } 15436 15437 if (!close_failed) { 15438 if (have_sync_lock) { 15439 osp->os_pending_close = 0; 15440 mutex_exit(&osp->os_sync_lock); 15441 have_sync_lock = 0; 15442 } else { 15443 mutex_enter(&osp->os_sync_lock); 15444 osp->os_pending_close = 0; 15445 mutex_exit(&osp->os_sync_lock); 15446 } 15447 if (did_start_op && recov_state.rs_sp != NULL) { 15448 mutex_enter(&recov_state.rs_sp->s_lock); 15449 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15450 mutex_exit(&recov_state.rs_sp->s_lock); 15451 } else { 15452 nfs4_dec_state_ref_count(mi); 15453 } 15454 nfs4_error_zinit(ep); 15455 } 15456 15457 out: 15458 if (have_sync_lock) 15459 mutex_exit(&osp->os_sync_lock); 15460 if (did_start_op) 15461 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15462 recovonly ? TRUE : FALSE); 15463 if (did_force_recovlock) 15464 nfs_rw_exit(&mi->mi_recovlock); 15465 if (cred_otw) 15466 crfree(cred_otw); 15467 if (osp) 15468 open_stream_rele(osp, rp); 15469 if (oop) { 15470 if (did_start_seqid_sync) 15471 nfs4_end_open_seqid_sync(oop); 15472 open_owner_rele(oop); 15473 } 15474 } 15475 15476 /* 15477 * Convert information returned by the server in the LOCK4denied 15478 * structure to the form required by fcntl. 15479 */ 15480 static void 15481 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15482 { 15483 nfs4_lo_name_t *lo; 15484 15485 #ifdef DEBUG 15486 if (denied_to_flk_debug) { 15487 lockt_denied_debug = lockt_denied; 15488 debug_enter("lockt_denied"); 15489 } 15490 #endif 15491 15492 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15493 flk->l_whence = 0; /* aka SEEK_SET */ 15494 flk->l_start = lockt_denied->offset; 15495 flk->l_len = lockt_denied->length; 15496 15497 /* 15498 * If the blocking clientid matches our client id, then we can 15499 * interpret the lockowner (since we built it). If not, then 15500 * fabricate a sysid and pid. Note that the l_sysid field 15501 * in *flk already has the local sysid. 15502 */ 15503 15504 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15505 15506 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15507 lo = (nfs4_lo_name_t *) 15508 lockt_denied->owner.owner_val; 15509 15510 flk->l_pid = lo->ln_pid; 15511 } else { 15512 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15513 "denied_to_flk: bad lock owner length\n")); 15514 15515 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15516 } 15517 } else { 15518 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15519 "denied_to_flk: foreign clientid\n")); 15520 15521 /* 15522 * Construct a new sysid which should be different from 15523 * sysids of other systems. 15524 */ 15525 15526 flk->l_sysid++; 15527 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15528 } 15529 } 15530 15531 static pid_t 15532 lo_to_pid(lock_owner4 *lop) 15533 { 15534 pid_t pid = 0; 15535 uchar_t *cp; 15536 int i; 15537 15538 cp = (uchar_t *)&lop->clientid; 15539 15540 for (i = 0; i < sizeof (lop->clientid); i++) 15541 pid += (pid_t)*cp++; 15542 15543 cp = (uchar_t *)lop->owner_val; 15544 15545 for (i = 0; i < lop->owner_len; i++) 15546 pid += (pid_t)*cp++; 15547 15548 return (pid); 15549 } 15550 15551 /* 15552 * Given a lock pointer, returns the length of that lock. 15553 * "end" is the last locked offset the "l_len" covers from 15554 * the start of the lock. 15555 */ 15556 static off64_t 15557 lock_to_end(flock64_t *lock) 15558 { 15559 off64_t lock_end; 15560 15561 if (lock->l_len == 0) 15562 lock_end = (off64_t)MAXEND; 15563 else 15564 lock_end = lock->l_start + lock->l_len - 1; 15565 15566 return (lock_end); 15567 } 15568 15569 /* 15570 * Given the end of a lock, it will return you the length "l_len" for that lock. 15571 */ 15572 static off64_t 15573 end_to_len(off64_t start, off64_t end) 15574 { 15575 off64_t lock_len; 15576 15577 ASSERT(end >= start); 15578 if (end == MAXEND) 15579 lock_len = 0; 15580 else 15581 lock_len = end - start + 1; 15582 15583 return (lock_len); 15584 } 15585 15586 /* 15587 * On given end for a lock it determines if it is the last locked offset 15588 * or not, if so keeps it as is, else adds one to return the length for 15589 * valid start. 15590 */ 15591 static off64_t 15592 start_check(off64_t x) 15593 { 15594 if (x == MAXEND) 15595 return (x); 15596 else 15597 return (x + 1); 15598 } 15599 15600 /* 15601 * See if these two locks overlap, and if so return 1; 15602 * otherwise, return 0. 15603 */ 15604 static int 15605 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15606 { 15607 off64_t llfp_end, curfp_end; 15608 15609 llfp_end = lock_to_end(llfp); 15610 curfp_end = lock_to_end(curfp); 15611 15612 if (((llfp_end >= curfp->l_start) && 15613 (llfp->l_start <= curfp->l_start)) || 15614 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15615 return (1); 15616 return (0); 15617 } 15618 15619 /* 15620 * Determine what the intersecting lock region is, and add that to the 15621 * 'nl_llpp' locklist in increasing order (by l_start). 15622 */ 15623 static void 15624 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15625 locklist_t **nl_llpp, vnode_t *vp) 15626 { 15627 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15628 off64_t lost_flp_end, local_flp_end, len, start; 15629 15630 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15631 15632 if (!locks_intersect(lost_flp, local_flp)) 15633 return; 15634 15635 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15636 "locks intersect")); 15637 15638 lost_flp_end = lock_to_end(lost_flp); 15639 local_flp_end = lock_to_end(local_flp); 15640 15641 /* Find the starting point of the intersecting region */ 15642 if (local_flp->l_start > lost_flp->l_start) 15643 start = local_flp->l_start; 15644 else 15645 start = lost_flp->l_start; 15646 15647 /* Find the lenght of the intersecting region */ 15648 if (lost_flp_end < local_flp_end) 15649 len = end_to_len(start, lost_flp_end); 15650 else 15651 len = end_to_len(start, local_flp_end); 15652 15653 /* 15654 * Prepare the flock structure for the intersection found and insert 15655 * it into the new list in increasing l_start order. This list contains 15656 * intersections of locks registered by the client with the local host 15657 * and the lost lock. 15658 * The lock type of this lock is the same as that of the local_flp. 15659 */ 15660 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15661 intersect_llp->ll_flock.l_start = start; 15662 intersect_llp->ll_flock.l_len = len; 15663 intersect_llp->ll_flock.l_type = local_flp->l_type; 15664 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15665 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15666 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15667 intersect_llp->ll_vp = vp; 15668 15669 tmp_fllp = *nl_llpp; 15670 cur_fllp = NULL; 15671 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15672 intersect_llp->ll_flock.l_start) { 15673 cur_fllp = tmp_fllp; 15674 tmp_fllp = tmp_fllp->ll_next; 15675 } 15676 if (cur_fllp == NULL) { 15677 /* first on the list */ 15678 intersect_llp->ll_next = *nl_llpp; 15679 *nl_llpp = intersect_llp; 15680 } else { 15681 intersect_llp->ll_next = cur_fllp->ll_next; 15682 cur_fllp->ll_next = intersect_llp; 15683 } 15684 15685 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15686 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15687 intersect_llp->ll_flock.l_start, 15688 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15689 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15690 } 15691 15692 /* 15693 * Our local locking current state is potentially different than 15694 * what the NFSv4 server thinks we have due to a lost lock that was 15695 * resent and then received. We need to reset our "NFSv4" locking 15696 * state to match the current local locking state for this pid since 15697 * that is what the user/application sees as what the world is. 15698 * 15699 * We cannot afford to drop the open/lock seqid sync since then we can 15700 * get confused about what the current local locking state "is" versus 15701 * "was". 15702 * 15703 * If we are unable to fix up the locks, we send SIGLOST to the affected 15704 * process. This is not done if the filesystem has been forcibly 15705 * unmounted, in case the process has already exited and a new process 15706 * exists with the same pid. 15707 */ 15708 static void 15709 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15710 nfs4_lock_owner_t *lop) 15711 { 15712 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15713 mntinfo4_t *mi = VTOMI4(vp); 15714 const int cmd = F_SETLK; 15715 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15716 flock64_t ul_fl; 15717 15718 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15719 "nfs4_reinstitute_local_lock_state")); 15720 15721 /* 15722 * Find active locks for this vp from the local locking code. 15723 * Scan through this list and find out the locks that intersect with 15724 * the lost lock. Once we find the lock that intersects, add the 15725 * intersection area as a new lock to a new list "ri_llp". The lock 15726 * type of the intersection region lock added to ri_llp is the same 15727 * as that found in the active lock list, "list". The intersecting 15728 * region locks are added to ri_llp in increasing l_start order. 15729 */ 15730 ASSERT(nfs_zone() == mi->mi_zone); 15731 15732 locks = flk_active_locks_for_vp(vp); 15733 ri_llp = NULL; 15734 15735 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15736 ASSERT(llp->ll_vp == vp); 15737 /* 15738 * Pick locks that belong to this pid/lockowner 15739 */ 15740 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15741 continue; 15742 15743 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15744 } 15745 15746 /* 15747 * Now we have the list of intersections with the lost lock. These are 15748 * the locks that were/are active before the server replied to the 15749 * last/lost lock. Issue these locks to the server here. Playing these 15750 * locks to the server will re-establish aur current local locking state 15751 * with the v4 server. 15752 * If we get an error, send SIGLOST to the application for that lock. 15753 */ 15754 15755 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15756 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15757 "nfs4_reinstitute_local_lock_state: need to issue " 15758 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15759 llp->ll_flock.l_start, 15760 llp->ll_flock.l_start + llp->ll_flock.l_len, 15761 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15762 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15763 /* 15764 * No need to relock what we already have 15765 */ 15766 if (llp->ll_flock.l_type == lost_flp->l_type) 15767 continue; 15768 15769 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15770 } 15771 15772 /* 15773 * Now keeping the start of the lost lock as our reference parse the 15774 * newly created ri_llp locklist to find the ranges that we have locked 15775 * with the v4 server but not in the current local locking. We need 15776 * to unlock these ranges. 15777 * These ranges can also be reffered to as those ranges, where the lost 15778 * lock does not overlap with the locks in the ri_llp but are locked 15779 * since the server replied to the lost lock. 15780 */ 15781 cur_start = lost_flp->l_start; 15782 lost_flp_end = lock_to_end(lost_flp); 15783 15784 ul_fl.l_type = F_UNLCK; 15785 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15786 ul_fl.l_sysid = lost_flp->l_sysid; 15787 ul_fl.l_pid = lost_flp->l_pid; 15788 15789 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15790 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15791 15792 if (llp->ll_flock.l_start <= cur_start) { 15793 cur_start = start_check(llp_ll_flock_end); 15794 continue; 15795 } 15796 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15797 "nfs4_reinstitute_local_lock_state: " 15798 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15799 cur_start, llp->ll_flock.l_start)); 15800 15801 ul_fl.l_start = cur_start; 15802 ul_fl.l_len = end_to_len(cur_start, 15803 (llp->ll_flock.l_start - 1)); 15804 15805 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15806 cur_start = start_check(llp_ll_flock_end); 15807 } 15808 15809 /* 15810 * In the case where the lost lock ends after all intersecting locks, 15811 * unlock the last part of the lost lock range. 15812 */ 15813 if (cur_start != start_check(lost_flp_end)) { 15814 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15815 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15816 "lost lock region [%"PRIx64" - %"PRIx64"]", 15817 cur_start, lost_flp->l_start + lost_flp->l_len)); 15818 15819 ul_fl.l_start = cur_start; 15820 /* 15821 * Is it an to-EOF lock? if so unlock till the end 15822 */ 15823 if (lost_flp->l_len == 0) 15824 ul_fl.l_len = 0; 15825 else 15826 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15827 15828 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15829 } 15830 15831 if (locks != NULL) 15832 flk_free_locklist(locks); 15833 15834 /* Free up our newly created locklist */ 15835 for (llp = ri_llp; llp != NULL; ) { 15836 tmp_llp = llp->ll_next; 15837 kmem_free(llp, sizeof (locklist_t)); 15838 llp = tmp_llp; 15839 } 15840 15841 /* 15842 * Now return back to the original calling nfs4frlock() 15843 * and let us naturally drop our seqid syncs. 15844 */ 15845 } 15846 15847 /* 15848 * Create a lost state record for the given lock reinstantiation request 15849 * and push it onto the lost state queue. 15850 */ 15851 static void 15852 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15853 nfs4_lock_owner_t *lop) 15854 { 15855 nfs4_lost_rqst_t req; 15856 nfs_lock_type4 locktype; 15857 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15858 15859 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15860 15861 locktype = flk_to_locktype(cmd, flk->l_type); 15862 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 15863 NULL, NULL, lop, flk, &req, cr, vp); 15864 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 15865 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 15866 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 15867 NULL); 15868 } 15869