1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* 26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 27 */ 28 29 /* 30 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 31 * All Rights Reserved 32 */ 33 34 /* 35 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 36 */ 37 38 /* 39 * Copyright (c) 2014, STRATO AG. All rights reserved. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/types.h> 44 #include <sys/systm.h> 45 #include <sys/cred.h> 46 #include <sys/time.h> 47 #include <sys/vnode.h> 48 #include <sys/vfs.h> 49 #include <sys/vfs_opreg.h> 50 #include <sys/file.h> 51 #include <sys/filio.h> 52 #include <sys/uio.h> 53 #include <sys/buf.h> 54 #include <sys/mman.h> 55 #include <sys/pathname.h> 56 #include <sys/dirent.h> 57 #include <sys/debug.h> 58 #include <sys/vmsystm.h> 59 #include <sys/fcntl.h> 60 #include <sys/flock.h> 61 #include <sys/swap.h> 62 #include <sys/errno.h> 63 #include <sys/strsubr.h> 64 #include <sys/sysmacros.h> 65 #include <sys/kmem.h> 66 #include <sys/cmn_err.h> 67 #include <sys/pathconf.h> 68 #include <sys/utsname.h> 69 #include <sys/dnlc.h> 70 #include <sys/acl.h> 71 #include <sys/systeminfo.h> 72 #include <sys/policy.h> 73 #include <sys/sdt.h> 74 #include <sys/list.h> 75 #include <sys/stat.h> 76 #include <sys/zone.h> 77 78 #include <rpc/types.h> 79 #include <rpc/auth.h> 80 #include <rpc/clnt.h> 81 82 #include <nfs/nfs.h> 83 #include <nfs/nfs_clnt.h> 84 #include <nfs/nfs_acl.h> 85 #include <nfs/lm.h> 86 #include <nfs/nfs4.h> 87 #include <nfs/nfs4_kprot.h> 88 #include <nfs/rnode4.h> 89 #include <nfs/nfs4_clnt.h> 90 91 #include <vm/hat.h> 92 #include <vm/as.h> 93 #include <vm/page.h> 94 #include <vm/pvn.h> 95 #include <vm/seg.h> 96 #include <vm/seg_map.h> 97 #include <vm/seg_kpm.h> 98 #include <vm/seg_vn.h> 99 100 #include <fs/fs_subr.h> 101 102 #include <sys/ddi.h> 103 #include <sys/int_fmtio.h> 104 #include <sys/fs/autofs.h> 105 106 typedef struct { 107 nfs4_ga_res_t *di_garp; 108 cred_t *di_cred; 109 hrtime_t di_time_call; 110 } dirattr_info_t; 111 112 typedef enum nfs4_acl_op { 113 NFS4_ACL_GET, 114 NFS4_ACL_SET 115 } nfs4_acl_op_t; 116 117 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi); 118 119 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 120 char *, dirattr_info_t *); 121 122 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 123 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 124 nfs4_error_t *, int *); 125 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 126 cred_t *); 127 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 128 stable_how4 *); 129 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 130 cred_t *, bool_t, struct uio *); 131 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 132 vsecattr_t *); 133 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 134 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 135 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 136 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 137 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 138 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 139 int, vnode_t **, cred_t *); 140 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 141 cred_t *, int, int, enum createmode4, int); 142 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 143 caller_context_t *); 144 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 145 vnode_t *, char *, cred_t *, nfsstat4 *); 146 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 147 vnode_t *, char *, cred_t *, nfsstat4 *); 148 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 149 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 150 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 151 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 152 page_t *[], size_t, struct seg *, caddr_t, 153 enum seg_rw, cred_t *); 154 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 155 cred_t *); 156 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 157 int, cred_t *); 158 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 159 int, cred_t *); 160 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 161 static void nfs4_set_mod(vnode_t *); 162 static void nfs4_get_commit(vnode_t *); 163 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 164 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 165 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 166 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 167 cred_t *); 168 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 169 cred_t *); 170 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 171 hrtime_t, vnode_t *, cred_t *); 172 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 173 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 174 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 175 u_offset_t); 176 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 177 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 178 static cred_t *state_to_cred(nfs4_open_stream_t *); 179 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 180 static pid_t lo_to_pid(lock_owner4 *); 181 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 182 cred_t *, nfs4_lock_owner_t *); 183 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 184 nfs4_lock_owner_t *); 185 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **); 186 static void nfs4_delmap_callback(struct as *, void *, uint_t); 187 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 188 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 189 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 190 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 191 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 192 uid_t, gid_t, int); 193 194 /* 195 * Routines that implement the setting of v4 args for the misc. ops 196 */ 197 static void nfs4args_lock_free(nfs_argop4 *); 198 static void nfs4args_lockt_free(nfs_argop4 *); 199 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 200 int, rnode4_t *, cred_t *, bitmap4, int *, 201 nfs4_stateid_types_t *); 202 static void nfs4args_setattr_free(nfs_argop4 *); 203 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 204 bitmap4); 205 static void nfs4args_verify_free(nfs_argop4 *); 206 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 207 WRITE4args **, nfs4_stateid_types_t *); 208 209 /* 210 * These are the vnode ops functions that implement the vnode interface to 211 * the networked file system. See more comments below at nfs4_vnodeops. 212 */ 213 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *); 214 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *, 215 caller_context_t *); 216 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 217 caller_context_t *); 218 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 219 caller_context_t *); 220 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *, 221 caller_context_t *); 222 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 223 caller_context_t *); 224 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *); 225 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *, 226 caller_context_t *); 227 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *); 228 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 229 int, vnode_t **, cred_t *, int, caller_context_t *, 230 vsecattr_t *); 231 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *, 232 int); 233 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *, 234 caller_context_t *, int); 235 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *, 236 caller_context_t *, int); 237 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **, 238 cred_t *, caller_context_t *, int, vsecattr_t *); 239 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *, 240 caller_context_t *, int); 241 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 242 cred_t *, caller_context_t *, int); 243 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *, 244 caller_context_t *, int); 245 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *); 246 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 247 page_t *[], size_t, struct seg *, caddr_t, 248 enum seg_rw, cred_t *, caller_context_t *); 249 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *, 250 caller_context_t *); 251 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t, 252 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 253 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 254 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *); 255 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *); 256 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 257 struct flk_callback *, cred_t *, caller_context_t *); 258 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 259 cred_t *, caller_context_t *); 260 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t, 261 uint_t, uint_t, uint_t, cred_t *, caller_context_t *); 262 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 263 cred_t *, caller_context_t *); 264 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *, 265 caller_context_t *); 266 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 267 caller_context_t *); 268 /* 269 * These vnode ops are required to be called from outside this source file, 270 * e.g. by ephemeral mount stub vnode ops, and so may not be declared 271 * as static. 272 */ 273 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *, 274 caller_context_t *); 275 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *); 276 int nfs4_lookup(vnode_t *, char *, vnode_t **, 277 struct pathname *, int, vnode_t *, cred_t *, 278 caller_context_t *, int *, pathname_t *); 279 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *); 280 int nfs4_rwlock(vnode_t *, int, caller_context_t *); 281 void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 282 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *); 283 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *, 284 caller_context_t *); 285 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *, 286 caller_context_t *); 287 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *, 288 caller_context_t *); 289 290 /* 291 * Used for nfs4_commit_vp() to indicate if we should 292 * wait on pending writes. 293 */ 294 #define NFS4_WRITE_NOWAIT 0 295 #define NFS4_WRITE_WAIT 1 296 297 /* 298 * Error flags used to pass information about certain special errors 299 * which need to be handled specially. 300 */ 301 #define NFS_EOF -98 302 #define NFS_VERF_MISMATCH -97 303 304 /* 305 * Flags used to differentiate between which operation drove the 306 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 307 */ 308 #define NFS4_CLOSE_OP 0x1 309 #define NFS4_DELMAP_OP 0x2 310 #define NFS4_INACTIVE_OP 0x3 311 312 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 313 314 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 315 #define ALIGN64(x, ptr, sz) \ 316 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 317 if (x) { \ 318 x = sizeof (uint64_t) - (x); \ 319 sz -= (x); \ 320 ptr += (x); \ 321 } 322 323 #ifdef DEBUG 324 int nfs4_client_attr_debug = 0; 325 int nfs4_client_state_debug = 0; 326 int nfs4_client_shadow_debug = 0; 327 int nfs4_client_lock_debug = 0; 328 int nfs4_seqid_sync = 0; 329 int nfs4_client_map_debug = 0; 330 static int nfs4_pageio_debug = 0; 331 int nfs4_client_inactive_debug = 0; 332 int nfs4_client_recov_debug = 0; 333 int nfs4_client_failover_debug = 0; 334 int nfs4_client_call_debug = 0; 335 int nfs4_client_lookup_debug = 0; 336 int nfs4_client_zone_debug = 0; 337 int nfs4_lost_rqst_debug = 0; 338 int nfs4_rdattrerr_debug = 0; 339 int nfs4_open_stream_debug = 0; 340 341 int nfs4read_error_inject; 342 343 static int nfs4_create_misses = 0; 344 345 static int nfs4_readdir_cache_shorts = 0; 346 static int nfs4_readdir_readahead = 0; 347 348 static int nfs4_bio_do_stop = 0; 349 350 static int nfs4_lostpage = 0; /* number of times we lost original page */ 351 352 int nfs4_mmap_debug = 0; 353 354 static int nfs4_pathconf_cache_hits = 0; 355 static int nfs4_pathconf_cache_misses = 0; 356 357 int nfs4close_all_cnt; 358 int nfs4close_one_debug = 0; 359 int nfs4close_notw_debug = 0; 360 361 int denied_to_flk_debug = 0; 362 void *lockt_denied_debug; 363 364 #endif 365 366 /* 367 * In milliseconds. Should be less than half of the lease time or better, 368 * less than one second. 369 */ 370 int nfs4_base_wait_time = 20; 371 int nfs4_max_base_wait_time = 1 * 1000; /* 1 sec */ 372 373 /* 374 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 375 * or NFS4ERR_RESOURCE. 376 */ 377 static int confirm_retry_sec = 30; 378 379 static int nfs4_lookup_neg_cache = 1; 380 381 /* 382 * number of pages to read ahead 383 * optimized for 100 base-T. 384 */ 385 static int nfs4_nra = 4; 386 387 static int nfs4_do_symlink_cache = 1; 388 389 static int nfs4_pathconf_disable_cache = 0; 390 391 /* 392 * These are the vnode ops routines which implement the vnode interface to 393 * the networked file system. These routines just take their parameters, 394 * make them look networkish by putting the right info into interface structs, 395 * and then calling the appropriate remote routine(s) to do the work. 396 * 397 * Note on directory name lookup cacheing: If we detect a stale fhandle, 398 * we purge the directory cache relative to that vnode. This way, the 399 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 400 * more details on rnode locking. 401 */ 402 403 struct vnodeops *nfs4_vnodeops; 404 405 const fs_operation_def_t nfs4_vnodeops_template[] = { 406 VOPNAME_OPEN, { .vop_open = nfs4_open }, 407 VOPNAME_CLOSE, { .vop_close = nfs4_close }, 408 VOPNAME_READ, { .vop_read = nfs4_read }, 409 VOPNAME_WRITE, { .vop_write = nfs4_write }, 410 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl }, 411 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr }, 412 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr }, 413 VOPNAME_ACCESS, { .vop_access = nfs4_access }, 414 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup }, 415 VOPNAME_CREATE, { .vop_create = nfs4_create }, 416 VOPNAME_REMOVE, { .vop_remove = nfs4_remove }, 417 VOPNAME_LINK, { .vop_link = nfs4_link }, 418 VOPNAME_RENAME, { .vop_rename = nfs4_rename }, 419 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir }, 420 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir }, 421 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir }, 422 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink }, 423 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink }, 424 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync }, 425 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive }, 426 VOPNAME_FID, { .vop_fid = nfs4_fid }, 427 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock }, 428 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock }, 429 VOPNAME_SEEK, { .vop_seek = nfs4_seek }, 430 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock }, 431 VOPNAME_SPACE, { .vop_space = nfs4_space }, 432 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp }, 433 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage }, 434 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage }, 435 VOPNAME_MAP, { .vop_map = nfs4_map }, 436 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap }, 437 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap }, 438 /* no separate nfs4_dump */ 439 VOPNAME_DUMP, { .vop_dump = nfs_dump }, 440 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf }, 441 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio }, 442 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose }, 443 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr }, 444 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr }, 445 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock }, 446 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 447 NULL, NULL 448 }; 449 450 /* 451 * The following are subroutines and definitions to set args or get res 452 * for the different nfsv4 ops 453 */ 454 455 void 456 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 457 { 458 int i; 459 460 for (i = 0; i < arglen; i++) { 461 if (argop[i].argop == OP_LOOKUP) { 462 kmem_free( 463 argop[i].nfs_argop4_u.oplookup. 464 objname.utf8string_val, 465 argop[i].nfs_argop4_u.oplookup. 466 objname.utf8string_len); 467 } 468 } 469 } 470 471 static void 472 nfs4args_lock_free(nfs_argop4 *argop) 473 { 474 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 475 476 if (locker->new_lock_owner == TRUE) { 477 open_to_lock_owner4 *open_owner; 478 479 open_owner = &locker->locker4_u.open_owner; 480 if (open_owner->lock_owner.owner_val != NULL) { 481 kmem_free(open_owner->lock_owner.owner_val, 482 open_owner->lock_owner.owner_len); 483 } 484 } 485 } 486 487 static void 488 nfs4args_lockt_free(nfs_argop4 *argop) 489 { 490 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 491 492 if (lowner->owner_val != NULL) { 493 kmem_free(lowner->owner_val, lowner->owner_len); 494 } 495 } 496 497 static void 498 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 499 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 500 nfs4_stateid_types_t *sid_types) 501 { 502 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 503 mntinfo4_t *mi; 504 505 argop->argop = OP_SETATTR; 506 /* 507 * The stateid is set to 0 if client is not modifying the size 508 * and otherwise to whatever nfs4_get_stateid() returns. 509 * 510 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 511 * state struct could be found for the process/file pair. We may 512 * want to change this in the future (by OPENing the file). See 513 * bug # 4474852. 514 */ 515 if (vap->va_mask & AT_SIZE) { 516 517 ASSERT(rp != NULL); 518 mi = VTOMI4(RTOV4(rp)); 519 520 argop->nfs_argop4_u.opsetattr.stateid = 521 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 522 OP_SETATTR, sid_types, FALSE); 523 } else { 524 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 525 sizeof (stateid4)); 526 } 527 528 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 529 if (*error) 530 bzero(attr, sizeof (*attr)); 531 } 532 533 static void 534 nfs4args_setattr_free(nfs_argop4 *argop) 535 { 536 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 537 } 538 539 static int 540 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 541 bitmap4 supp) 542 { 543 fattr4 *attr; 544 int error = 0; 545 546 argop->argop = op; 547 switch (op) { 548 case OP_VERIFY: 549 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 550 break; 551 case OP_NVERIFY: 552 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 553 break; 554 default: 555 return (EINVAL); 556 } 557 if (!error) 558 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 559 if (error) 560 bzero(attr, sizeof (*attr)); 561 return (error); 562 } 563 564 static void 565 nfs4args_verify_free(nfs_argop4 *argop) 566 { 567 switch (argop->argop) { 568 case OP_VERIFY: 569 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 570 break; 571 case OP_NVERIFY: 572 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 573 break; 574 default: 575 break; 576 } 577 } 578 579 static void 580 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 581 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 582 { 583 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 584 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 585 586 argop->argop = OP_WRITE; 587 wargs->stable = stable; 588 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 589 mi, OP_WRITE, sid_tp); 590 wargs->mblk = NULL; 591 *wargs_pp = wargs; 592 } 593 594 void 595 nfs4args_copen_free(OPEN4cargs *open_args) 596 { 597 if (open_args->owner.owner_val) { 598 kmem_free(open_args->owner.owner_val, 599 open_args->owner.owner_len); 600 } 601 if ((open_args->opentype == OPEN4_CREATE) && 602 (open_args->mode != EXCLUSIVE4)) { 603 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 604 } 605 } 606 607 /* 608 * XXX: This is referenced in modstubs.s 609 */ 610 struct vnodeops * 611 nfs4_getvnodeops(void) 612 { 613 return (nfs4_vnodeops); 614 } 615 616 /* 617 * The OPEN operation opens a regular file. 618 */ 619 /*ARGSUSED3*/ 620 static int 621 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 622 { 623 vnode_t *dvp = NULL; 624 rnode4_t *rp, *drp; 625 int error; 626 int just_been_created; 627 char fn[MAXNAMELEN]; 628 629 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 630 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 631 return (EIO); 632 rp = VTOR4(*vpp); 633 634 /* 635 * Check to see if opening something besides a regular file; 636 * if so skip the OTW call 637 */ 638 if ((*vpp)->v_type != VREG) { 639 error = nfs4_open_non_reg_file(vpp, flag, cr); 640 return (error); 641 } 642 643 /* 644 * XXX - would like a check right here to know if the file is 645 * executable or not, so as to skip OTW 646 */ 647 648 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 649 return (error); 650 651 drp = VTOR4(dvp); 652 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 653 return (EINTR); 654 655 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) { 656 nfs_rw_exit(&drp->r_rwlock); 657 return (error); 658 } 659 660 /* 661 * See if this file has just been CREATEd. 662 * If so, clear the flag and update the dnlc, which was previously 663 * skipped in nfs4_create. 664 * XXX need better serilization on this. 665 * XXX move this into the nf4open_otw call, after we have 666 * XXX acquired the open owner seqid sync. 667 */ 668 mutex_enter(&rp->r_statev4_lock); 669 if (rp->created_v4) { 670 rp->created_v4 = 0; 671 mutex_exit(&rp->r_statev4_lock); 672 673 dnlc_update(dvp, fn, *vpp); 674 /* This is needed so we don't bump the open ref count */ 675 just_been_created = 1; 676 } else { 677 mutex_exit(&rp->r_statev4_lock); 678 just_been_created = 0; 679 } 680 681 /* 682 * If caller specified O_TRUNC/FTRUNC, then be sure to set 683 * FWRITE (to drive successful setattr(size=0) after open) 684 */ 685 if (flag & FTRUNC) 686 flag |= FWRITE; 687 688 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 689 just_been_created); 690 691 if (!error && !((*vpp)->v_flag & VROOT)) 692 dnlc_update(dvp, fn, *vpp); 693 694 nfs_rw_exit(&drp->r_rwlock); 695 696 /* release the hold from vtodv */ 697 VN_RELE(dvp); 698 699 /* exchange the shadow for the master vnode, if needed */ 700 701 if (error == 0 && IS_SHADOW(*vpp, rp)) 702 sv_exchange(vpp); 703 704 return (error); 705 } 706 707 /* 708 * See if there's a "lost open" request to be saved and recovered. 709 */ 710 static void 711 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 712 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 713 vnode_t *dvp, OPEN4cargs *open_args) 714 { 715 vfs_t *vfsp; 716 char *srccfp; 717 718 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 719 720 if (error != ETIMEDOUT && error != EINTR && 721 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 722 lost_rqstp->lr_op = 0; 723 return; 724 } 725 726 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 727 "nfs4open_save_lost_rqst: error %d", error)); 728 729 lost_rqstp->lr_op = OP_OPEN; 730 731 /* 732 * The vp (if it is not NULL) and dvp are held and rele'd via 733 * the recovery code. See nfs4_save_lost_rqst. 734 */ 735 lost_rqstp->lr_vp = vp; 736 lost_rqstp->lr_dvp = dvp; 737 lost_rqstp->lr_oop = oop; 738 lost_rqstp->lr_osp = NULL; 739 lost_rqstp->lr_lop = NULL; 740 lost_rqstp->lr_cr = cr; 741 lost_rqstp->lr_flk = NULL; 742 lost_rqstp->lr_oacc = open_args->share_access; 743 lost_rqstp->lr_odeny = open_args->share_deny; 744 lost_rqstp->lr_oclaim = open_args->claim; 745 if (open_args->claim == CLAIM_DELEGATE_CUR) { 746 lost_rqstp->lr_ostateid = 747 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 748 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 749 } else { 750 srccfp = open_args->open_claim4_u.cfile; 751 } 752 lost_rqstp->lr_ofile.utf8string_len = 0; 753 lost_rqstp->lr_ofile.utf8string_val = NULL; 754 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 755 lost_rqstp->lr_putfirst = FALSE; 756 } 757 758 struct nfs4_excl_time { 759 uint32 seconds; 760 uint32 nseconds; 761 }; 762 763 /* 764 * The OPEN operation creates and/or opens a regular file 765 * 766 * ARGSUSED 767 */ 768 static int 769 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 770 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 771 enum createmode4 createmode, int file_just_been_created) 772 { 773 rnode4_t *rp; 774 rnode4_t *drp = VTOR4(dvp); 775 vnode_t *vp = NULL; 776 vnode_t *vpi = *vpp; 777 bool_t needrecov = FALSE; 778 779 int doqueue = 1; 780 781 COMPOUND4args_clnt args; 782 COMPOUND4res_clnt res; 783 nfs_argop4 *argop; 784 nfs_resop4 *resop; 785 int argoplist_size; 786 int idx_open, idx_fattr; 787 788 GETFH4res *gf_res = NULL; 789 OPEN4res *op_res = NULL; 790 nfs4_ga_res_t *garp; 791 fattr4 *attr = NULL; 792 struct nfs4_excl_time verf; 793 bool_t did_excl_setup = FALSE; 794 int created_osp; 795 796 OPEN4cargs *open_args; 797 nfs4_open_owner_t *oop = NULL; 798 nfs4_open_stream_t *osp = NULL; 799 seqid4 seqid = 0; 800 bool_t retry_open = FALSE; 801 nfs4_recov_state_t recov_state; 802 nfs4_lost_rqst_t lost_rqst; 803 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 804 hrtime_t t; 805 int acc = 0; 806 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 807 cred_t *ncr = NULL; 808 809 nfs4_sharedfh_t *otw_sfh; 810 nfs4_sharedfh_t *orig_sfh; 811 int fh_differs = 0; 812 int numops, setgid_flag; 813 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 814 815 /* 816 * Make sure we properly deal with setting the right gid on 817 * a newly created file to reflect the parent's setgid bit 818 */ 819 setgid_flag = 0; 820 if (create_flag && in_va) { 821 822 /* 823 * If there is grpid mount flag used or 824 * the parent's directory has the setgid bit set 825 * _and_ the client was able to get a valid mapping 826 * for the parent dir's owner_group, we want to 827 * append NVERIFY(owner_group == dva.va_gid) and 828 * SETATTR to the CREATE compound. 829 */ 830 mutex_enter(&drp->r_statelock); 831 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID || 832 drp->r_attr.va_mode & VSGID) && 833 drp->r_attr.va_gid != GID_NOBODY) { 834 in_va->va_mask |= AT_GID; 835 in_va->va_gid = drp->r_attr.va_gid; 836 setgid_flag = 1; 837 } 838 mutex_exit(&drp->r_statelock); 839 } 840 841 /* 842 * Normal/non-create compound: 843 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 844 * 845 * Open(create) compound no setgid: 846 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 847 * RESTOREFH + GETATTR 848 * 849 * Open(create) setgid: 850 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 851 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 852 * NVERIFY(grp) + SETATTR 853 */ 854 if (setgid_flag) { 855 numops = 10; 856 idx_open = 1; 857 idx_fattr = 3; 858 } else if (create_flag) { 859 numops = 7; 860 idx_open = 2; 861 idx_fattr = 4; 862 } else { 863 numops = 4; 864 idx_open = 1; 865 idx_fattr = 3; 866 } 867 868 args.array_len = numops; 869 argoplist_size = numops * sizeof (nfs_argop4); 870 argop = kmem_alloc(argoplist_size, KM_SLEEP); 871 872 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 873 "open %s open flag 0x%x cred %p", file_name, open_flag, 874 (void *)cr)); 875 876 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 877 if (create_flag) { 878 /* 879 * We are to create a file. Initialize the passed in vnode 880 * pointer. 881 */ 882 vpi = NULL; 883 } else { 884 /* 885 * Check to see if the client owns a read delegation and is 886 * trying to open for write. If so, then return the delegation 887 * to avoid the server doing a cb_recall and returning DELAY. 888 * NB - we don't use the statev4_lock here because we'd have 889 * to drop the lock anyway and the result would be stale. 890 */ 891 if ((open_flag & FWRITE) && 892 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 893 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 894 895 /* 896 * If the file has a delegation, then do an access check up 897 * front. This avoids having to an access check later after 898 * we've already done start_op, which could deadlock. 899 */ 900 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 901 if (open_flag & FREAD && 902 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0) 903 acc |= VREAD; 904 if (open_flag & FWRITE && 905 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0) 906 acc |= VWRITE; 907 } 908 } 909 910 drp = VTOR4(dvp); 911 912 recov_state.rs_flags = 0; 913 recov_state.rs_num_retry_despite_err = 0; 914 cred_otw = cr; 915 916 recov_retry: 917 fh_differs = 0; 918 nfs4_error_zinit(&e); 919 920 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 921 if (e.error) { 922 if (ncr != NULL) 923 crfree(ncr); 924 kmem_free(argop, argoplist_size); 925 return (e.error); 926 } 927 928 args.ctag = TAG_OPEN; 929 args.array_len = numops; 930 args.array = argop; 931 932 /* putfh directory fh */ 933 argop[0].argop = OP_CPUTFH; 934 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 935 936 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 937 argop[idx_open].argop = OP_COPEN; 938 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 939 open_args->claim = CLAIM_NULL; 940 941 /* name of file */ 942 open_args->open_claim4_u.cfile = file_name; 943 open_args->owner.owner_len = 0; 944 open_args->owner.owner_val = NULL; 945 946 if (create_flag) { 947 /* CREATE a file */ 948 open_args->opentype = OPEN4_CREATE; 949 open_args->mode = createmode; 950 if (createmode == EXCLUSIVE4) { 951 if (did_excl_setup == FALSE) { 952 verf.seconds = zone_get_hostid(NULL); 953 if (verf.seconds != 0) 954 verf.nseconds = newnum(); 955 else { 956 timestruc_t now; 957 958 gethrestime(&now); 959 verf.seconds = now.tv_sec; 960 verf.nseconds = now.tv_nsec; 961 } 962 /* 963 * Since the server will use this value for the 964 * mtime, make sure that it can't overflow. Zero 965 * out the MSB. The actual value does not matter 966 * here, only its uniqeness. 967 */ 968 verf.seconds &= INT32_MAX; 969 did_excl_setup = TRUE; 970 } 971 972 /* Now copy over verifier to OPEN4args. */ 973 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 974 } else { 975 int v_error; 976 bitmap4 supp_attrs; 977 servinfo4_t *svp; 978 979 attr = &open_args->createhow4_u.createattrs; 980 981 svp = drp->r_server; 982 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 983 supp_attrs = svp->sv_supp_attrs; 984 nfs_rw_exit(&svp->sv_lock); 985 986 /* GUARDED4 or UNCHECKED4 */ 987 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 988 supp_attrs); 989 if (v_error) { 990 bzero(attr, sizeof (*attr)); 991 nfs4args_copen_free(open_args); 992 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 993 &recov_state, FALSE); 994 if (ncr != NULL) 995 crfree(ncr); 996 kmem_free(argop, argoplist_size); 997 return (v_error); 998 } 999 } 1000 } else { 1001 /* NO CREATE */ 1002 open_args->opentype = OPEN4_NOCREATE; 1003 } 1004 1005 if (recov_state.rs_sp != NULL) { 1006 mutex_enter(&recov_state.rs_sp->s_lock); 1007 open_args->owner.clientid = recov_state.rs_sp->clientid; 1008 mutex_exit(&recov_state.rs_sp->s_lock); 1009 } else { 1010 /* XXX should we just fail here? */ 1011 open_args->owner.clientid = 0; 1012 } 1013 1014 /* 1015 * This increments oop's ref count or creates a temporary 'just_created' 1016 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 1017 * completes. 1018 */ 1019 mutex_enter(&VTOMI4(dvp)->mi_lock); 1020 1021 /* See if a permanent or just created open owner exists */ 1022 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 1023 if (!oop) { 1024 /* 1025 * This open owner does not exist so create a temporary 1026 * just created one. 1027 */ 1028 oop = create_open_owner(cr, VTOMI4(dvp)); 1029 ASSERT(oop != NULL); 1030 } 1031 mutex_exit(&VTOMI4(dvp)->mi_lock); 1032 1033 /* this length never changes, do alloc before seqid sync */ 1034 open_args->owner.owner_len = sizeof (oop->oo_name); 1035 open_args->owner.owner_val = 1036 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1037 1038 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 1039 if (e.error == EAGAIN) { 1040 open_owner_rele(oop); 1041 nfs4args_copen_free(open_args); 1042 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1043 if (ncr != NULL) { 1044 crfree(ncr); 1045 ncr = NULL; 1046 } 1047 goto recov_retry; 1048 } 1049 1050 /* Check to see if we need to do the OTW call */ 1051 if (!create_flag) { 1052 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1053 file_just_been_created, &e.error, acc, &recov_state)) { 1054 1055 /* 1056 * The OTW open is not necessary. Either 1057 * the open can succeed without it (eg. 1058 * delegation, error == 0) or the open 1059 * must fail due to an access failure 1060 * (error != 0). In either case, tidy 1061 * up and return. 1062 */ 1063 1064 nfs4_end_open_seqid_sync(oop); 1065 open_owner_rele(oop); 1066 nfs4args_copen_free(open_args); 1067 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1068 if (ncr != NULL) 1069 crfree(ncr); 1070 kmem_free(argop, argoplist_size); 1071 return (e.error); 1072 } 1073 } 1074 1075 bcopy(&oop->oo_name, open_args->owner.owner_val, 1076 open_args->owner.owner_len); 1077 1078 seqid = nfs4_get_open_seqid(oop) + 1; 1079 open_args->seqid = seqid; 1080 open_args->share_access = 0; 1081 if (open_flag & FREAD) 1082 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1083 if (open_flag & FWRITE) 1084 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1085 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1086 1087 1088 1089 /* 1090 * getfh w/sanity check for idx_open/idx_fattr 1091 */ 1092 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1093 argop[idx_open + 1].argop = OP_GETFH; 1094 1095 /* getattr */ 1096 argop[idx_fattr].argop = OP_GETATTR; 1097 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1098 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1099 1100 if (setgid_flag) { 1101 vattr_t _v; 1102 servinfo4_t *svp; 1103 bitmap4 supp_attrs; 1104 1105 svp = drp->r_server; 1106 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1107 supp_attrs = svp->sv_supp_attrs; 1108 nfs_rw_exit(&svp->sv_lock); 1109 1110 /* 1111 * For setgid case, we need to: 1112 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1113 */ 1114 argop[4].argop = OP_SAVEFH; 1115 1116 argop[5].argop = OP_CPUTFH; 1117 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1118 1119 argop[6].argop = OP_GETATTR; 1120 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1121 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1122 1123 argop[7].argop = OP_RESTOREFH; 1124 1125 /* 1126 * nverify 1127 */ 1128 _v.va_mask = AT_GID; 1129 _v.va_gid = in_va->va_gid; 1130 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1131 supp_attrs))) { 1132 1133 /* 1134 * setattr 1135 * 1136 * We _know_ we're not messing with AT_SIZE or 1137 * AT_XTIME, so no need for stateid or flags. 1138 * Also we specify NULL rp since we're only 1139 * interested in setting owner_group attributes. 1140 */ 1141 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1142 supp_attrs, &e.error, 0); 1143 if (e.error) 1144 nfs4args_verify_free(&argop[8]); 1145 } 1146 1147 if (e.error) { 1148 /* 1149 * XXX - Revisit the last argument to nfs4_end_op() 1150 * once 5020486 is fixed. 1151 */ 1152 nfs4_end_open_seqid_sync(oop); 1153 open_owner_rele(oop); 1154 nfs4args_copen_free(open_args); 1155 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1156 if (ncr != NULL) 1157 crfree(ncr); 1158 kmem_free(argop, argoplist_size); 1159 return (e.error); 1160 } 1161 } else if (create_flag) { 1162 argop[1].argop = OP_SAVEFH; 1163 1164 argop[5].argop = OP_RESTOREFH; 1165 1166 argop[6].argop = OP_GETATTR; 1167 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1168 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1169 } 1170 1171 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1172 "nfs4open_otw: %s call, nm %s, rp %s", 1173 needrecov ? "recov" : "first", file_name, 1174 rnode4info(VTOR4(dvp)))); 1175 1176 t = gethrtime(); 1177 1178 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1179 1180 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1181 nfs4_set_open_seqid(seqid, oop, args.ctag); 1182 1183 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1184 1185 if (e.error || needrecov) { 1186 bool_t abort = FALSE; 1187 1188 if (needrecov) { 1189 nfs4_bseqid_entry_t *bsep = NULL; 1190 1191 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1192 cred_otw, vpi, dvp, open_args); 1193 1194 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1195 bsep = nfs4_create_bseqid_entry(oop, NULL, 1196 vpi, 0, args.ctag, open_args->seqid); 1197 num_bseqid_retry--; 1198 } 1199 1200 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1201 NULL, lost_rqst.lr_op == OP_OPEN ? 1202 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL); 1203 1204 if (bsep) 1205 kmem_free(bsep, sizeof (*bsep)); 1206 /* give up if we keep getting BAD_SEQID */ 1207 if (num_bseqid_retry == 0) 1208 abort = TRUE; 1209 if (abort == TRUE && e.error == 0) 1210 e.error = geterrno4(res.status); 1211 } 1212 nfs4_end_open_seqid_sync(oop); 1213 open_owner_rele(oop); 1214 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1215 nfs4args_copen_free(open_args); 1216 if (setgid_flag) { 1217 nfs4args_verify_free(&argop[8]); 1218 nfs4args_setattr_free(&argop[9]); 1219 } 1220 if (!e.error) 1221 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1222 if (ncr != NULL) { 1223 crfree(ncr); 1224 ncr = NULL; 1225 } 1226 if (!needrecov || abort == TRUE || e.error == EINTR || 1227 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1228 kmem_free(argop, argoplist_size); 1229 return (e.error); 1230 } 1231 goto recov_retry; 1232 } 1233 1234 /* 1235 * Will check and update lease after checking the rflag for 1236 * OPEN_CONFIRM in the successful OPEN call. 1237 */ 1238 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1239 1240 /* 1241 * XXX what if we're crossing mount points from server1:/drp 1242 * to server2:/drp/rp. 1243 */ 1244 1245 /* Signal our end of use of the open seqid */ 1246 nfs4_end_open_seqid_sync(oop); 1247 1248 /* 1249 * This will destroy the open owner if it was just created, 1250 * and no one else has put a reference on it. 1251 */ 1252 open_owner_rele(oop); 1253 if (create_flag && (createmode != EXCLUSIVE4) && 1254 res.status == NFS4ERR_BADOWNER) 1255 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1256 1257 e.error = geterrno4(res.status); 1258 nfs4args_copen_free(open_args); 1259 if (setgid_flag) { 1260 nfs4args_verify_free(&argop[8]); 1261 nfs4args_setattr_free(&argop[9]); 1262 } 1263 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1264 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1265 /* 1266 * If the reply is NFS4ERR_ACCESS, it may be because 1267 * we are root (no root net access). If the real uid 1268 * is not root, then retry with the real uid instead. 1269 */ 1270 if (ncr != NULL) { 1271 crfree(ncr); 1272 ncr = NULL; 1273 } 1274 if (res.status == NFS4ERR_ACCESS && 1275 (ncr = crnetadjust(cred_otw)) != NULL) { 1276 cred_otw = ncr; 1277 goto recov_retry; 1278 } 1279 kmem_free(argop, argoplist_size); 1280 return (e.error); 1281 } 1282 1283 resop = &res.array[idx_open]; /* open res */ 1284 op_res = &resop->nfs_resop4_u.opopen; 1285 1286 #ifdef DEBUG 1287 /* 1288 * verify attrset bitmap 1289 */ 1290 if (create_flag && 1291 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1292 /* make sure attrset returned is what we asked for */ 1293 /* XXX Ignore this 'error' for now */ 1294 if (attr->attrmask != op_res->attrset) 1295 /* EMPTY */; 1296 } 1297 #endif 1298 1299 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1300 mutex_enter(&VTOMI4(dvp)->mi_lock); 1301 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1302 mutex_exit(&VTOMI4(dvp)->mi_lock); 1303 } 1304 1305 resop = &res.array[idx_open + 1]; /* getfh res */ 1306 gf_res = &resop->nfs_resop4_u.opgetfh; 1307 1308 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1309 1310 /* 1311 * The open stateid has been updated on the server but not 1312 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1313 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1314 * WRITE call. That, however, will use the old stateid, so go ahead 1315 * and upate the open stateid now, before any call to makenfs4node. 1316 */ 1317 if (vpi) { 1318 nfs4_open_stream_t *tmp_osp; 1319 rnode4_t *tmp_rp = VTOR4(vpi); 1320 1321 tmp_osp = find_open_stream(oop, tmp_rp); 1322 if (tmp_osp) { 1323 tmp_osp->open_stateid = op_res->stateid; 1324 mutex_exit(&tmp_osp->os_sync_lock); 1325 open_stream_rele(tmp_osp, tmp_rp); 1326 } 1327 1328 /* 1329 * We must determine if the file handle given by the otw open 1330 * is the same as the file handle which was passed in with 1331 * *vpp. This case can be reached if the file we are trying 1332 * to open has been removed and another file has been created 1333 * having the same file name. The passed in vnode is released 1334 * later. 1335 */ 1336 orig_sfh = VTOR4(vpi)->r_fh; 1337 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1338 } 1339 1340 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1341 1342 if (create_flag || fh_differs) { 1343 int rnode_err = 0; 1344 1345 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1346 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh)); 1347 1348 if (e.error) 1349 PURGE_ATTRCACHE4(vp); 1350 /* 1351 * For the newly created vp case, make sure the rnode 1352 * isn't bad before using it. 1353 */ 1354 mutex_enter(&(VTOR4(vp))->r_statelock); 1355 if (VTOR4(vp)->r_flags & R4RECOVERR) 1356 rnode_err = EIO; 1357 mutex_exit(&(VTOR4(vp))->r_statelock); 1358 1359 if (rnode_err) { 1360 nfs4_end_open_seqid_sync(oop); 1361 nfs4args_copen_free(open_args); 1362 if (setgid_flag) { 1363 nfs4args_verify_free(&argop[8]); 1364 nfs4args_setattr_free(&argop[9]); 1365 } 1366 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1367 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1368 needrecov); 1369 open_owner_rele(oop); 1370 VN_RELE(vp); 1371 if (ncr != NULL) 1372 crfree(ncr); 1373 sfh4_rele(&otw_sfh); 1374 kmem_free(argop, argoplist_size); 1375 return (EIO); 1376 } 1377 } else { 1378 vp = vpi; 1379 } 1380 sfh4_rele(&otw_sfh); 1381 1382 /* 1383 * It seems odd to get a full set of attrs and then not update 1384 * the object's attrcache in the non-create case. Create case uses 1385 * the attrs since makenfs4node checks to see if the attrs need to 1386 * be updated (and then updates them). The non-create case should 1387 * update attrs also. 1388 */ 1389 if (! create_flag && ! fh_differs && !e.error) { 1390 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1391 } 1392 1393 nfs4_error_zinit(&e); 1394 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1395 /* This does not do recovery for vp explicitly. */ 1396 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1397 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1398 1399 if (e.error || e.stat) { 1400 nfs4_end_open_seqid_sync(oop); 1401 nfs4args_copen_free(open_args); 1402 if (setgid_flag) { 1403 nfs4args_verify_free(&argop[8]); 1404 nfs4args_setattr_free(&argop[9]); 1405 } 1406 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1407 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1408 needrecov); 1409 open_owner_rele(oop); 1410 if (create_flag || fh_differs) { 1411 /* rele the makenfs4node */ 1412 VN_RELE(vp); 1413 } 1414 if (ncr != NULL) { 1415 crfree(ncr); 1416 ncr = NULL; 1417 } 1418 if (retry_open == TRUE) { 1419 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1420 "nfs4open_otw: retry the open since OPEN " 1421 "CONFIRM failed with error %d stat %d", 1422 e.error, e.stat)); 1423 if (create_flag && createmode == GUARDED4) { 1424 NFS4_DEBUG(nfs4_client_recov_debug, 1425 (CE_NOTE, "nfs4open_otw: switch " 1426 "createmode from GUARDED4 to " 1427 "UNCHECKED4")); 1428 createmode = UNCHECKED4; 1429 } 1430 goto recov_retry; 1431 } 1432 if (!e.error) { 1433 if (create_flag && (createmode != EXCLUSIVE4) && 1434 e.stat == NFS4ERR_BADOWNER) 1435 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1436 1437 e.error = geterrno4(e.stat); 1438 } 1439 kmem_free(argop, argoplist_size); 1440 return (e.error); 1441 } 1442 } 1443 1444 rp = VTOR4(vp); 1445 1446 mutex_enter(&rp->r_statev4_lock); 1447 if (create_flag) 1448 rp->created_v4 = 1; 1449 mutex_exit(&rp->r_statev4_lock); 1450 1451 mutex_enter(&oop->oo_lock); 1452 /* Doesn't matter if 'oo_just_created' already was set as this */ 1453 oop->oo_just_created = NFS4_PERM_CREATED; 1454 if (oop->oo_cred_otw) 1455 crfree(oop->oo_cred_otw); 1456 oop->oo_cred_otw = cred_otw; 1457 crhold(oop->oo_cred_otw); 1458 mutex_exit(&oop->oo_lock); 1459 1460 /* returns with 'os_sync_lock' held */ 1461 osp = find_or_create_open_stream(oop, rp, &created_osp); 1462 if (!osp) { 1463 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1464 "nfs4open_otw: failed to create an open stream")); 1465 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1466 "signal our end of use of the open seqid")); 1467 1468 nfs4_end_open_seqid_sync(oop); 1469 open_owner_rele(oop); 1470 nfs4args_copen_free(open_args); 1471 if (setgid_flag) { 1472 nfs4args_verify_free(&argop[8]); 1473 nfs4args_setattr_free(&argop[9]); 1474 } 1475 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1476 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1477 if (create_flag || fh_differs) 1478 VN_RELE(vp); 1479 if (ncr != NULL) 1480 crfree(ncr); 1481 1482 kmem_free(argop, argoplist_size); 1483 return (EINVAL); 1484 1485 } 1486 1487 osp->open_stateid = op_res->stateid; 1488 1489 if (open_flag & FREAD) 1490 osp->os_share_acc_read++; 1491 if (open_flag & FWRITE) 1492 osp->os_share_acc_write++; 1493 osp->os_share_deny_none++; 1494 1495 /* 1496 * Need to reset this bitfield for the possible case where we were 1497 * going to OTW CLOSE the file, got a non-recoverable error, and before 1498 * we could retry the CLOSE, OPENed the file again. 1499 */ 1500 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1501 osp->os_final_close = 0; 1502 osp->os_force_close = 0; 1503 #ifdef DEBUG 1504 if (osp->os_failed_reopen) 1505 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1506 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1507 (void *)osp, (void *)cr, rnode4info(rp))); 1508 #endif 1509 osp->os_failed_reopen = 0; 1510 1511 mutex_exit(&osp->os_sync_lock); 1512 1513 nfs4_end_open_seqid_sync(oop); 1514 1515 if (created_osp && recov_state.rs_sp != NULL) { 1516 mutex_enter(&recov_state.rs_sp->s_lock); 1517 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1518 mutex_exit(&recov_state.rs_sp->s_lock); 1519 } 1520 1521 /* get rid of our reference to find oop */ 1522 open_owner_rele(oop); 1523 1524 open_stream_rele(osp, rp); 1525 1526 /* accept delegation, if any */ 1527 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1528 1529 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1530 1531 if (createmode == EXCLUSIVE4 && 1532 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1533 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1534 " EXCLUSIVE4: sending a SETATTR")); 1535 /* 1536 * If doing an exclusive create, then generate 1537 * a SETATTR to set the initial attributes. 1538 * Try to set the mtime and the atime to the 1539 * server's current time. It is somewhat 1540 * expected that these fields will be used to 1541 * store the exclusive create cookie. If not, 1542 * server implementors will need to know that 1543 * a SETATTR will follow an exclusive create 1544 * and the cookie should be destroyed if 1545 * appropriate. 1546 * 1547 * The AT_GID and AT_SIZE bits are turned off 1548 * so that the SETATTR request will not attempt 1549 * to process these. The gid will be set 1550 * separately if appropriate. The size is turned 1551 * off because it is assumed that a new file will 1552 * be created empty and if the file wasn't empty, 1553 * then the exclusive create will have failed 1554 * because the file must have existed already. 1555 * Therefore, no truncate operation is needed. 1556 */ 1557 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1558 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1559 1560 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1561 if (e.error) { 1562 /* 1563 * Couldn't correct the attributes of 1564 * the newly created file and the 1565 * attributes are wrong. Remove the 1566 * file and return an error to the 1567 * application. 1568 */ 1569 /* XXX will this take care of client state ? */ 1570 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1571 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1572 " remove file", e.error)); 1573 VN_RELE(vp); 1574 (void) nfs4_remove(dvp, file_name, cr, NULL, 0); 1575 /* 1576 * Since we've reled the vnode and removed 1577 * the file we now need to return the error. 1578 * At this point we don't want to update the 1579 * dircaches, call nfs4_waitfor_purge_complete 1580 * or set vpp to vp so we need to skip these 1581 * as well. 1582 */ 1583 goto skip_update_dircaches; 1584 } 1585 } 1586 1587 /* 1588 * If we created or found the correct vnode, due to create_flag or 1589 * fh_differs being set, then update directory cache attribute, readdir 1590 * and dnlc caches. 1591 */ 1592 if (create_flag || fh_differs) { 1593 dirattr_info_t dinfo, *dinfop; 1594 1595 /* 1596 * Make sure getattr succeeded before using results. 1597 * note: op 7 is getattr(dir) for both flavors of 1598 * open(create). 1599 */ 1600 if (create_flag && res.status == NFS4_OK) { 1601 dinfo.di_time_call = t; 1602 dinfo.di_cred = cr; 1603 dinfo.di_garp = 1604 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1605 dinfop = &dinfo; 1606 } else { 1607 dinfop = NULL; 1608 } 1609 1610 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1611 dinfop); 1612 } 1613 1614 /* 1615 * If the page cache for this file was flushed from actions 1616 * above, it was done asynchronously and if that is true, 1617 * there is a need to wait here for it to complete. This must 1618 * be done outside of start_fop/end_fop. 1619 */ 1620 (void) nfs4_waitfor_purge_complete(vp); 1621 1622 /* 1623 * It is implicit that we are in the open case (create_flag == 0) since 1624 * fh_differs can only be set to a non-zero value in the open case. 1625 */ 1626 if (fh_differs != 0 && vpi != NULL) 1627 VN_RELE(vpi); 1628 1629 /* 1630 * Be sure to set *vpp to the correct value before returning. 1631 */ 1632 *vpp = vp; 1633 1634 skip_update_dircaches: 1635 1636 nfs4args_copen_free(open_args); 1637 if (setgid_flag) { 1638 nfs4args_verify_free(&argop[8]); 1639 nfs4args_setattr_free(&argop[9]); 1640 } 1641 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1642 1643 if (ncr) 1644 crfree(ncr); 1645 kmem_free(argop, argoplist_size); 1646 return (e.error); 1647 } 1648 1649 /* 1650 * Reopen an open instance. cf. nfs4open_otw(). 1651 * 1652 * Errors are returned by the nfs4_error_t parameter. 1653 * - ep->error contains an errno value or zero. 1654 * - if it is zero, ep->stat is set to an NFS status code, if any. 1655 * If the file could not be reopened, but the caller should continue, the 1656 * file is marked dead and no error values are returned. If the caller 1657 * should stop recovering open files and start over, either the ep->error 1658 * value or ep->stat will indicate an error (either something that requires 1659 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1660 * filehandles) may be handled silently by this routine. 1661 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1662 * will be started, so the caller should not do it. 1663 * 1664 * Gotos: 1665 * - kill_file : reopen failed in such a fashion to constitute marking the 1666 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1667 * is for cases where recovery is not possible. 1668 * - failed_reopen : same as above, except that the file has already been 1669 * marked dead, so no need to do it again. 1670 * - bailout : reopen failed but we are able to recover and retry the reopen - 1671 * either within this function immediately or via the calling function. 1672 */ 1673 1674 void 1675 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1676 open_claim_type4 claim, bool_t frc_use_claim_previous, 1677 bool_t is_recov) 1678 { 1679 COMPOUND4args_clnt args; 1680 COMPOUND4res_clnt res; 1681 nfs_argop4 argop[4]; 1682 nfs_resop4 *resop; 1683 OPEN4res *op_res = NULL; 1684 OPEN4cargs *open_args; 1685 GETFH4res *gf_res; 1686 rnode4_t *rp = VTOR4(vp); 1687 int doqueue = 1; 1688 cred_t *cr = NULL, *cred_otw = NULL; 1689 nfs4_open_owner_t *oop = NULL; 1690 seqid4 seqid; 1691 nfs4_ga_res_t *garp; 1692 char fn[MAXNAMELEN]; 1693 nfs4_recov_state_t recov = {NULL, 0}; 1694 nfs4_lost_rqst_t lost_rqst; 1695 mntinfo4_t *mi = VTOMI4(vp); 1696 bool_t abort; 1697 char *failed_msg = ""; 1698 int fh_different; 1699 hrtime_t t; 1700 nfs4_bseqid_entry_t *bsep = NULL; 1701 1702 ASSERT(nfs4_consistent_type(vp)); 1703 ASSERT(nfs_zone() == mi->mi_zone); 1704 1705 nfs4_error_zinit(ep); 1706 1707 /* this is the cred used to find the open owner */ 1708 cr = state_to_cred(osp); 1709 if (cr == NULL) { 1710 failed_msg = "Couldn't reopen: no cred"; 1711 goto kill_file; 1712 } 1713 /* use this cred for OTW operations */ 1714 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1715 1716 top: 1717 nfs4_error_zinit(ep); 1718 1719 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1720 /* File system has been unmounted, quit */ 1721 ep->error = EIO; 1722 failed_msg = "Couldn't reopen: file system has been unmounted"; 1723 goto kill_file; 1724 } 1725 1726 oop = osp->os_open_owner; 1727 1728 ASSERT(oop != NULL); 1729 if (oop == NULL) { /* be defensive in non-DEBUG */ 1730 failed_msg = "can't reopen: no open owner"; 1731 goto kill_file; 1732 } 1733 open_owner_hold(oop); 1734 1735 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1736 if (ep->error) { 1737 open_owner_rele(oop); 1738 oop = NULL; 1739 goto bailout; 1740 } 1741 1742 /* 1743 * If the rnode has a delegation and the delegation has been 1744 * recovered and the server didn't request a recall and the caller 1745 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1746 * recovery) and the rnode hasn't been marked dead, then install 1747 * the delegation stateid in the open stream. Otherwise, proceed 1748 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1749 */ 1750 mutex_enter(&rp->r_statev4_lock); 1751 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1752 !rp->r_deleg_return_pending && 1753 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1754 !rp->r_deleg_needs_recall && 1755 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1756 !(rp->r_flags & R4RECOVERR)) { 1757 mutex_enter(&osp->os_sync_lock); 1758 osp->os_delegation = 1; 1759 osp->open_stateid = rp->r_deleg_stateid; 1760 mutex_exit(&osp->os_sync_lock); 1761 mutex_exit(&rp->r_statev4_lock); 1762 goto bailout; 1763 } 1764 mutex_exit(&rp->r_statev4_lock); 1765 1766 /* 1767 * If the file failed recovery, just quit. This failure need not 1768 * affect other reopens, so don't return an error. 1769 */ 1770 mutex_enter(&rp->r_statelock); 1771 if (rp->r_flags & R4RECOVERR) { 1772 mutex_exit(&rp->r_statelock); 1773 ep->error = 0; 1774 goto failed_reopen; 1775 } 1776 mutex_exit(&rp->r_statelock); 1777 1778 /* 1779 * argop is empty here 1780 * 1781 * PUTFH, OPEN, GETATTR 1782 */ 1783 args.ctag = TAG_REOPEN; 1784 args.array_len = 4; 1785 args.array = argop; 1786 1787 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1788 "nfs4_reopen: file is type %d, id %s", 1789 vp->v_type, rnode4info(VTOR4(vp)))); 1790 1791 argop[0].argop = OP_CPUTFH; 1792 1793 if (claim != CLAIM_PREVIOUS) { 1794 /* 1795 * if this is a file mount then 1796 * use the mntinfo parentfh 1797 */ 1798 argop[0].nfs_argop4_u.opcputfh.sfh = 1799 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1800 VTOSV(vp)->sv_dfh; 1801 } else { 1802 /* putfh fh to reopen */ 1803 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1804 } 1805 1806 argop[1].argop = OP_COPEN; 1807 open_args = &argop[1].nfs_argop4_u.opcopen; 1808 open_args->claim = claim; 1809 1810 if (claim == CLAIM_NULL) { 1811 1812 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1813 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1814 "failed for vp 0x%p for CLAIM_NULL with %m", 1815 (void *)vp); 1816 failed_msg = "Couldn't reopen: vtoname failed for " 1817 "CLAIM_NULL"; 1818 /* nothing allocated yet */ 1819 goto kill_file; 1820 } 1821 1822 open_args->open_claim4_u.cfile = fn; 1823 } else if (claim == CLAIM_PREVIOUS) { 1824 1825 /* 1826 * We have two cases to deal with here: 1827 * 1) We're being called to reopen files in order to satisfy 1828 * a lock operation request which requires us to explicitly 1829 * reopen files which were opened under a delegation. If 1830 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1831 * that case, frc_use_claim_previous is TRUE and we must 1832 * use the rnode's current delegation type (r_deleg_type). 1833 * 2) We're reopening files during some form of recovery. 1834 * In this case, frc_use_claim_previous is FALSE and we 1835 * use the delegation type appropriate for recovery 1836 * (r_deleg_needs_recovery). 1837 */ 1838 mutex_enter(&rp->r_statev4_lock); 1839 open_args->open_claim4_u.delegate_type = 1840 frc_use_claim_previous ? 1841 rp->r_deleg_type : 1842 rp->r_deleg_needs_recovery; 1843 mutex_exit(&rp->r_statev4_lock); 1844 1845 } else if (claim == CLAIM_DELEGATE_CUR) { 1846 1847 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1848 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1849 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1850 "with %m", (void *)vp); 1851 failed_msg = "Couldn't reopen: vtoname failed for " 1852 "CLAIM_DELEGATE_CUR"; 1853 /* nothing allocated yet */ 1854 goto kill_file; 1855 } 1856 1857 mutex_enter(&rp->r_statev4_lock); 1858 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1859 rp->r_deleg_stateid; 1860 mutex_exit(&rp->r_statev4_lock); 1861 1862 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1863 } 1864 open_args->opentype = OPEN4_NOCREATE; 1865 open_args->owner.clientid = mi2clientid(mi); 1866 open_args->owner.owner_len = sizeof (oop->oo_name); 1867 open_args->owner.owner_val = 1868 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1869 bcopy(&oop->oo_name, open_args->owner.owner_val, 1870 open_args->owner.owner_len); 1871 open_args->share_access = 0; 1872 open_args->share_deny = 0; 1873 1874 mutex_enter(&osp->os_sync_lock); 1875 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1876 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1877 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1878 (void *)osp, (void *)rp, osp->os_share_acc_read, 1879 osp->os_share_acc_write, osp->os_open_ref_count, 1880 osp->os_mmap_read, osp->os_mmap_write, claim)); 1881 1882 if (osp->os_share_acc_read || osp->os_mmap_read) 1883 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1884 if (osp->os_share_acc_write || osp->os_mmap_write) 1885 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1886 if (osp->os_share_deny_read) 1887 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1888 if (osp->os_share_deny_write) 1889 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1890 mutex_exit(&osp->os_sync_lock); 1891 1892 seqid = nfs4_get_open_seqid(oop) + 1; 1893 open_args->seqid = seqid; 1894 1895 /* Construct the getfh part of the compound */ 1896 argop[2].argop = OP_GETFH; 1897 1898 /* Construct the getattr part of the compound */ 1899 argop[3].argop = OP_GETATTR; 1900 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1901 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1902 1903 t = gethrtime(); 1904 1905 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1906 1907 if (ep->error) { 1908 if (!is_recov && !frc_use_claim_previous && 1909 (ep->error == EINTR || ep->error == ETIMEDOUT || 1910 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1911 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1912 cred_otw, vp, NULL, open_args); 1913 abort = nfs4_start_recovery(ep, 1914 VTOMI4(vp), vp, NULL, NULL, 1915 lost_rqst.lr_op == OP_OPEN ? 1916 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL); 1917 nfs4args_copen_free(open_args); 1918 goto bailout; 1919 } 1920 1921 nfs4args_copen_free(open_args); 1922 1923 if (ep->error == EACCES && cred_otw != cr) { 1924 crfree(cred_otw); 1925 cred_otw = cr; 1926 crhold(cred_otw); 1927 nfs4_end_open_seqid_sync(oop); 1928 open_owner_rele(oop); 1929 oop = NULL; 1930 goto top; 1931 } 1932 if (ep->error == ETIMEDOUT) 1933 goto bailout; 1934 failed_msg = "Couldn't reopen: rpc error"; 1935 goto kill_file; 1936 } 1937 1938 if (nfs4_need_to_bump_seqid(&res)) 1939 nfs4_set_open_seqid(seqid, oop, args.ctag); 1940 1941 switch (res.status) { 1942 case NFS4_OK: 1943 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1944 mutex_enter(&rp->r_statelock); 1945 rp->r_delay_interval = 0; 1946 mutex_exit(&rp->r_statelock); 1947 } 1948 break; 1949 case NFS4ERR_BAD_SEQID: 1950 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1951 args.ctag, open_args->seqid); 1952 1953 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1954 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1955 NULL, OP_OPEN, bsep, NULL, NULL); 1956 1957 nfs4args_copen_free(open_args); 1958 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1959 nfs4_end_open_seqid_sync(oop); 1960 open_owner_rele(oop); 1961 oop = NULL; 1962 kmem_free(bsep, sizeof (*bsep)); 1963 1964 goto kill_file; 1965 case NFS4ERR_NO_GRACE: 1966 nfs4args_copen_free(open_args); 1967 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1968 nfs4_end_open_seqid_sync(oop); 1969 open_owner_rele(oop); 1970 oop = NULL; 1971 if (claim == CLAIM_PREVIOUS) { 1972 /* 1973 * Retry as a plain open. We don't need to worry about 1974 * checking the changeinfo: it is acceptable for a 1975 * client to re-open a file and continue processing 1976 * (in the absence of locks). 1977 */ 1978 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1979 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1980 "will retry as CLAIM_NULL")); 1981 claim = CLAIM_NULL; 1982 nfs4_mi_kstat_inc_no_grace(mi); 1983 goto top; 1984 } 1985 failed_msg = 1986 "Couldn't reopen: tried reclaim outside grace period. "; 1987 goto kill_file; 1988 case NFS4ERR_GRACE: 1989 nfs4_set_grace_wait(mi); 1990 nfs4args_copen_free(open_args); 1991 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1992 nfs4_end_open_seqid_sync(oop); 1993 open_owner_rele(oop); 1994 oop = NULL; 1995 ep->error = nfs4_wait_for_grace(mi, &recov); 1996 if (ep->error != 0) 1997 goto bailout; 1998 goto top; 1999 case NFS4ERR_DELAY: 2000 nfs4_set_delay_wait(vp); 2001 nfs4args_copen_free(open_args); 2002 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2003 nfs4_end_open_seqid_sync(oop); 2004 open_owner_rele(oop); 2005 oop = NULL; 2006 ep->error = nfs4_wait_for_delay(vp, &recov); 2007 nfs4_mi_kstat_inc_delay(mi); 2008 if (ep->error != 0) 2009 goto bailout; 2010 goto top; 2011 case NFS4ERR_FHEXPIRED: 2012 /* recover filehandle and retry */ 2013 abort = nfs4_start_recovery(ep, 2014 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL); 2015 nfs4args_copen_free(open_args); 2016 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2017 nfs4_end_open_seqid_sync(oop); 2018 open_owner_rele(oop); 2019 oop = NULL; 2020 if (abort == FALSE) 2021 goto top; 2022 failed_msg = "Couldn't reopen: recovery aborted"; 2023 goto kill_file; 2024 case NFS4ERR_RESOURCE: 2025 case NFS4ERR_STALE_CLIENTID: 2026 case NFS4ERR_WRONGSEC: 2027 case NFS4ERR_EXPIRED: 2028 /* 2029 * Do not mark the file dead and let the calling 2030 * function initiate recovery. 2031 */ 2032 nfs4args_copen_free(open_args); 2033 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2034 nfs4_end_open_seqid_sync(oop); 2035 open_owner_rele(oop); 2036 oop = NULL; 2037 goto bailout; 2038 case NFS4ERR_ACCESS: 2039 if (cred_otw != cr) { 2040 crfree(cred_otw); 2041 cred_otw = cr; 2042 crhold(cred_otw); 2043 nfs4args_copen_free(open_args); 2044 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2045 nfs4_end_open_seqid_sync(oop); 2046 open_owner_rele(oop); 2047 oop = NULL; 2048 goto top; 2049 } 2050 /* fall through */ 2051 default: 2052 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2053 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2054 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2055 rnode4info(VTOR4(vp)))); 2056 failed_msg = "Couldn't reopen: NFSv4 error"; 2057 nfs4args_copen_free(open_args); 2058 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2059 goto kill_file; 2060 } 2061 2062 resop = &res.array[1]; /* open res */ 2063 op_res = &resop->nfs_resop4_u.opopen; 2064 2065 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2066 2067 /* 2068 * Check if the path we reopened really is the same 2069 * file. We could end up in a situation where the file 2070 * was removed and a new file created with the same name. 2071 */ 2072 resop = &res.array[2]; 2073 gf_res = &resop->nfs_resop4_u.opgetfh; 2074 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2075 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2076 if (fh_different) { 2077 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2078 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2079 /* Oops, we don't have the same file */ 2080 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2081 failed_msg = "Couldn't reopen: Persistent " 2082 "file handle changed"; 2083 else 2084 failed_msg = "Couldn't reopen: Volatile " 2085 "(no expire on open) file handle changed"; 2086 2087 nfs4args_copen_free(open_args); 2088 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2089 nfs_rw_exit(&mi->mi_fh_lock); 2090 goto kill_file; 2091 2092 } else { 2093 /* 2094 * We have volatile file handles that don't compare. 2095 * If the fids are the same then we assume that the 2096 * file handle expired but the rnode still refers to 2097 * the same file object. 2098 * 2099 * First check that we have fids or not. 2100 * If we don't we have a dumb server so we will 2101 * just assume every thing is ok for now. 2102 */ 2103 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2104 rp->r_attr.va_mask & AT_NODEID && 2105 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2106 /* 2107 * We have fids, but they don't 2108 * compare. So kill the file. 2109 */ 2110 failed_msg = 2111 "Couldn't reopen: file handle changed" 2112 " due to mismatched fids"; 2113 nfs4args_copen_free(open_args); 2114 (void) xdr_free(xdr_COMPOUND4res_clnt, 2115 (caddr_t)&res); 2116 nfs_rw_exit(&mi->mi_fh_lock); 2117 goto kill_file; 2118 } else { 2119 /* 2120 * We have volatile file handles that refers 2121 * to the same file (at least they have the 2122 * same fid) or we don't have fids so we 2123 * can't tell. :(. We'll be a kind and accepting 2124 * client so we'll update the rnode's file 2125 * handle with the otw handle. 2126 * 2127 * We need to drop mi->mi_fh_lock since 2128 * sh4_update acquires it. Since there is 2129 * only one recovery thread there is no 2130 * race. 2131 */ 2132 nfs_rw_exit(&mi->mi_fh_lock); 2133 sfh4_update(rp->r_fh, &gf_res->object); 2134 } 2135 } 2136 } else { 2137 nfs_rw_exit(&mi->mi_fh_lock); 2138 } 2139 2140 ASSERT(nfs4_consistent_type(vp)); 2141 2142 /* 2143 * If the server wanted an OPEN_CONFIRM but that fails, just start 2144 * over. Presumably if there is a persistent error it will show up 2145 * when we resend the OPEN. 2146 */ 2147 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2148 bool_t retry_open = FALSE; 2149 2150 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2151 cred_otw, is_recov, &retry_open, 2152 oop, FALSE, ep, NULL); 2153 if (ep->error || ep->stat) { 2154 nfs4args_copen_free(open_args); 2155 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2156 nfs4_end_open_seqid_sync(oop); 2157 open_owner_rele(oop); 2158 oop = NULL; 2159 goto top; 2160 } 2161 } 2162 2163 mutex_enter(&osp->os_sync_lock); 2164 osp->open_stateid = op_res->stateid; 2165 osp->os_delegation = 0; 2166 /* 2167 * Need to reset this bitfield for the possible case where we were 2168 * going to OTW CLOSE the file, got a non-recoverable error, and before 2169 * we could retry the CLOSE, OPENed the file again. 2170 */ 2171 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2172 osp->os_final_close = 0; 2173 osp->os_force_close = 0; 2174 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2175 osp->os_dc_openacc = open_args->share_access; 2176 mutex_exit(&osp->os_sync_lock); 2177 2178 nfs4_end_open_seqid_sync(oop); 2179 2180 /* accept delegation, if any */ 2181 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2182 2183 nfs4args_copen_free(open_args); 2184 2185 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2186 2187 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2188 2189 ASSERT(nfs4_consistent_type(vp)); 2190 2191 open_owner_rele(oop); 2192 crfree(cr); 2193 crfree(cred_otw); 2194 return; 2195 2196 kill_file: 2197 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2198 failed_reopen: 2199 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2200 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2201 (void *)osp, (void *)cr, rnode4info(rp))); 2202 mutex_enter(&osp->os_sync_lock); 2203 osp->os_failed_reopen = 1; 2204 mutex_exit(&osp->os_sync_lock); 2205 bailout: 2206 if (oop != NULL) { 2207 nfs4_end_open_seqid_sync(oop); 2208 open_owner_rele(oop); 2209 } 2210 if (cr != NULL) 2211 crfree(cr); 2212 if (cred_otw != NULL) 2213 crfree(cred_otw); 2214 } 2215 2216 /* for . and .. OPENs */ 2217 /* ARGSUSED */ 2218 static int 2219 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2220 { 2221 rnode4_t *rp; 2222 nfs4_ga_res_t gar; 2223 2224 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2225 2226 /* 2227 * If close-to-open consistency checking is turned off or 2228 * if there is no cached data, we can avoid 2229 * the over the wire getattr. Otherwise, force a 2230 * call to the server to get fresh attributes and to 2231 * check caches. This is required for close-to-open 2232 * consistency. 2233 */ 2234 rp = VTOR4(*vpp); 2235 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2236 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2237 return (0); 2238 2239 gar.n4g_va.va_mask = AT_ALL; 2240 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2241 } 2242 2243 /* 2244 * CLOSE a file 2245 */ 2246 /* ARGSUSED */ 2247 static int 2248 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2249 caller_context_t *ct) 2250 { 2251 rnode4_t *rp; 2252 int error = 0; 2253 int r_error = 0; 2254 int n4error = 0; 2255 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2256 2257 /* 2258 * Remove client state for this (lockowner, file) pair. 2259 * Issue otw v4 call to have the server do the same. 2260 */ 2261 2262 rp = VTOR4(vp); 2263 2264 /* 2265 * zone_enter(2) prevents processes from changing zones with NFS files 2266 * open; if we happen to get here from the wrong zone we can't do 2267 * anything over the wire. 2268 */ 2269 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2270 /* 2271 * We could attempt to clean up locks, except we're sure 2272 * that the current process didn't acquire any locks on 2273 * the file: any attempt to lock a file belong to another zone 2274 * will fail, and one can't lock an NFS file and then change 2275 * zones, as that fails too. 2276 * 2277 * Returning an error here is the sane thing to do. A 2278 * subsequent call to VN_RELE() which translates to a 2279 * nfs4_inactive() will clean up state: if the zone of the 2280 * vnode's origin is still alive and kicking, the inactive 2281 * thread will handle the request (from the correct zone), and 2282 * everything (minus the OTW close call) should be OK. If the 2283 * zone is going away nfs4_async_inactive() will throw away 2284 * delegations, open streams and cached pages inline. 2285 */ 2286 return (EIO); 2287 } 2288 2289 /* 2290 * If we are using local locking for this filesystem, then 2291 * release all of the SYSV style record locks. Otherwise, 2292 * we are doing network locking and we need to release all 2293 * of the network locks. All of the locks held by this 2294 * process on this file are released no matter what the 2295 * incoming reference count is. 2296 */ 2297 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2298 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2299 cleanshares(vp, ttoproc(curthread)->p_pid); 2300 } else 2301 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2302 2303 if (e.error) { 2304 struct lm_sysid *lmsid; 2305 lmsid = nfs4_find_sysid(VTOMI4(vp)); 2306 if (lmsid == NULL) { 2307 DTRACE_PROBE2(unknown__sysid, int, e.error, 2308 vnode_t *, vp); 2309 } else { 2310 cleanlocks(vp, ttoproc(curthread)->p_pid, 2311 (lm_sysidt(lmsid) | LM_SYSID_CLIENT)); 2312 } 2313 return (e.error); 2314 } 2315 2316 if (count > 1) 2317 return (0); 2318 2319 /* 2320 * If the file has been `unlinked', then purge the 2321 * DNLC so that this vnode will get reycled quicker 2322 * and the .nfs* file on the server will get removed. 2323 */ 2324 if (rp->r_unldvp != NULL) 2325 dnlc_purge_vp(vp); 2326 2327 /* 2328 * If the file was open for write and there are pages, 2329 * do a synchronous flush and commit of all of the 2330 * dirty and uncommitted pages. 2331 */ 2332 ASSERT(!e.error); 2333 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2334 error = nfs4_putpage_commit(vp, 0, 0, cr); 2335 2336 mutex_enter(&rp->r_statelock); 2337 r_error = rp->r_error; 2338 rp->r_error = 0; 2339 mutex_exit(&rp->r_statelock); 2340 2341 /* 2342 * If this file type is one for which no explicit 'open' was 2343 * done, then bail now (ie. no need for protocol 'close'). If 2344 * there was an error w/the vm subsystem, return _that_ error, 2345 * otherwise, return any errors that may've been reported via 2346 * the rnode. 2347 */ 2348 if (vp->v_type != VREG) 2349 return (error ? error : r_error); 2350 2351 /* 2352 * The sync putpage commit may have failed above, but since 2353 * we're working w/a regular file, we need to do the protocol 2354 * 'close' (nfs4close_one will figure out if an otw close is 2355 * needed or not). Report any errors _after_ doing the protocol 2356 * 'close'. 2357 */ 2358 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2359 n4error = e.error ? e.error : geterrno4(e.stat); 2360 2361 /* 2362 * Error reporting prio (Hi -> Lo) 2363 * 2364 * i) nfs4_putpage_commit (error) 2365 * ii) rnode's (r_error) 2366 * iii) nfs4close_one (n4error) 2367 */ 2368 return (error ? error : (r_error ? r_error : n4error)); 2369 } 2370 2371 /* 2372 * Initialize *lost_rqstp. 2373 */ 2374 2375 static void 2376 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2377 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2378 vnode_t *vp) 2379 { 2380 if (error != ETIMEDOUT && error != EINTR && 2381 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2382 lost_rqstp->lr_op = 0; 2383 return; 2384 } 2385 2386 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2387 "nfs4close_save_lost_rqst: error %d", error)); 2388 2389 lost_rqstp->lr_op = OP_CLOSE; 2390 /* 2391 * The vp is held and rele'd via the recovery code. 2392 * See nfs4_save_lost_rqst. 2393 */ 2394 lost_rqstp->lr_vp = vp; 2395 lost_rqstp->lr_dvp = NULL; 2396 lost_rqstp->lr_oop = oop; 2397 lost_rqstp->lr_osp = osp; 2398 ASSERT(osp != NULL); 2399 ASSERT(mutex_owned(&osp->os_sync_lock)); 2400 osp->os_pending_close = 1; 2401 lost_rqstp->lr_lop = NULL; 2402 lost_rqstp->lr_cr = cr; 2403 lost_rqstp->lr_flk = NULL; 2404 lost_rqstp->lr_putfirst = FALSE; 2405 } 2406 2407 /* 2408 * Assumes you already have the open seqid sync grabbed as well as the 2409 * 'os_sync_lock'. Note: this will release the open seqid sync and 2410 * 'os_sync_lock' if client recovery starts. Calling functions have to 2411 * be prepared to handle this. 2412 * 2413 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2414 * was needed and was started, and that the calling function should retry 2415 * this function; otherwise it is returned as 0. 2416 * 2417 * Errors are returned via the nfs4_error_t parameter. 2418 */ 2419 static void 2420 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2421 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2422 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2423 { 2424 COMPOUND4args_clnt args; 2425 COMPOUND4res_clnt res; 2426 CLOSE4args *close_args; 2427 nfs_resop4 *resop; 2428 nfs_argop4 argop[3]; 2429 int doqueue = 1; 2430 mntinfo4_t *mi; 2431 seqid4 seqid; 2432 vnode_t *vp; 2433 bool_t needrecov = FALSE; 2434 nfs4_lost_rqst_t lost_rqst; 2435 hrtime_t t; 2436 2437 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2438 2439 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2440 2441 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2442 2443 /* Only set this to 1 if recovery is started */ 2444 *recov = 0; 2445 2446 /* do the OTW call to close the file */ 2447 2448 if (close_type == CLOSE_RESEND) 2449 args.ctag = TAG_CLOSE_LOST; 2450 else if (close_type == CLOSE_AFTER_RESEND) 2451 args.ctag = TAG_CLOSE_UNDO; 2452 else 2453 args.ctag = TAG_CLOSE; 2454 2455 args.array_len = 3; 2456 args.array = argop; 2457 2458 vp = RTOV4(rp); 2459 2460 mi = VTOMI4(vp); 2461 2462 /* putfh target fh */ 2463 argop[0].argop = OP_CPUTFH; 2464 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2465 2466 argop[1].argop = OP_GETATTR; 2467 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2468 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2469 2470 argop[2].argop = OP_CLOSE; 2471 close_args = &argop[2].nfs_argop4_u.opclose; 2472 2473 seqid = nfs4_get_open_seqid(oop) + 1; 2474 2475 close_args->seqid = seqid; 2476 close_args->open_stateid = osp->open_stateid; 2477 2478 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2479 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2480 rnode4info(rp))); 2481 2482 t = gethrtime(); 2483 2484 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2485 2486 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2487 nfs4_set_open_seqid(seqid, oop, args.ctag); 2488 } 2489 2490 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2491 if (ep->error && !needrecov) { 2492 /* 2493 * if there was an error and no recovery is to be done 2494 * then then set up the file to flush its cache if 2495 * needed for the next caller. 2496 */ 2497 mutex_enter(&rp->r_statelock); 2498 PURGE_ATTRCACHE4_LOCKED(rp); 2499 rp->r_flags &= ~R4WRITEMODIFIED; 2500 mutex_exit(&rp->r_statelock); 2501 return; 2502 } 2503 2504 if (needrecov) { 2505 bool_t abort; 2506 nfs4_bseqid_entry_t *bsep = NULL; 2507 2508 if (close_type != CLOSE_RESEND) 2509 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2510 osp, cred_otw, vp); 2511 2512 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2513 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2514 0, args.ctag, close_args->seqid); 2515 2516 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2517 "nfs4close_otw: initiating recovery. error %d " 2518 "res.status %d", ep->error, res.status)); 2519 2520 /* 2521 * Drop the 'os_sync_lock' here so we don't hit 2522 * a potential recursive mutex_enter via an 2523 * 'open_stream_hold()'. 2524 */ 2525 mutex_exit(&osp->os_sync_lock); 2526 *have_sync_lockp = 0; 2527 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2528 (close_type != CLOSE_RESEND && 2529 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2530 OP_CLOSE, bsep, NULL, NULL); 2531 2532 /* drop open seq sync, and let the calling function regrab it */ 2533 nfs4_end_open_seqid_sync(oop); 2534 *did_start_seqid_syncp = 0; 2535 2536 if (bsep) 2537 kmem_free(bsep, sizeof (*bsep)); 2538 /* 2539 * For signals, the caller wants to quit, so don't say to 2540 * retry. For forced unmount, if it's a user thread, it 2541 * wants to quit. If it's a recovery thread, the retry 2542 * will happen higher-up on the call stack. Either way, 2543 * don't say to retry. 2544 */ 2545 if (abort == FALSE && ep->error != EINTR && 2546 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2547 close_type != CLOSE_RESEND && 2548 close_type != CLOSE_AFTER_RESEND) 2549 *recov = 1; 2550 else 2551 *recov = 0; 2552 2553 if (!ep->error) 2554 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2555 return; 2556 } 2557 2558 if (res.status) { 2559 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2560 return; 2561 } 2562 2563 mutex_enter(&rp->r_statev4_lock); 2564 rp->created_v4 = 0; 2565 mutex_exit(&rp->r_statev4_lock); 2566 2567 resop = &res.array[2]; 2568 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2569 osp->os_valid = 0; 2570 2571 /* 2572 * This removes the reference obtained at OPEN; ie, when the 2573 * open stream structure was created. 2574 * 2575 * We don't have to worry about calling 'open_stream_rele' 2576 * since we our currently holding a reference to the open 2577 * stream which means the count cannot go to 0 with this 2578 * decrement. 2579 */ 2580 ASSERT(osp->os_ref_count >= 2); 2581 osp->os_ref_count--; 2582 2583 if (!ep->error) 2584 nfs4_attr_cache(vp, 2585 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2586 t, cred_otw, TRUE, NULL); 2587 2588 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2589 " returning %d", ep->error)); 2590 2591 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2592 } 2593 2594 /* ARGSUSED */ 2595 static int 2596 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2597 caller_context_t *ct) 2598 { 2599 rnode4_t *rp; 2600 u_offset_t off; 2601 offset_t diff; 2602 uint_t on; 2603 uint_t n; 2604 caddr_t base; 2605 uint_t flags; 2606 int error; 2607 mntinfo4_t *mi; 2608 2609 rp = VTOR4(vp); 2610 2611 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2612 2613 if (IS_SHADOW(vp, rp)) 2614 vp = RTOV4(rp); 2615 2616 if (vp->v_type != VREG) 2617 return (EISDIR); 2618 2619 mi = VTOMI4(vp); 2620 2621 if (nfs_zone() != mi->mi_zone) 2622 return (EIO); 2623 2624 if (uiop->uio_resid == 0) 2625 return (0); 2626 2627 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2628 return (EINVAL); 2629 2630 mutex_enter(&rp->r_statelock); 2631 if (rp->r_flags & R4RECOVERRP) 2632 error = (rp->r_error ? rp->r_error : EIO); 2633 else 2634 error = 0; 2635 mutex_exit(&rp->r_statelock); 2636 if (error) 2637 return (error); 2638 2639 /* 2640 * Bypass VM if caching has been disabled (e.g., locking) or if 2641 * using client-side direct I/O and the file is not mmap'd and 2642 * there are no cached pages. 2643 */ 2644 if ((vp->v_flag & VNOCACHE) || 2645 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2646 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2647 size_t resid = 0; 2648 2649 return (nfs4read(vp, NULL, uiop->uio_loffset, 2650 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2651 } 2652 2653 error = 0; 2654 2655 do { 2656 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2657 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2658 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2659 2660 if (error = nfs4_validate_caches(vp, cr)) 2661 break; 2662 2663 mutex_enter(&rp->r_statelock); 2664 while (rp->r_flags & R4INCACHEPURGE) { 2665 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2666 mutex_exit(&rp->r_statelock); 2667 return (EINTR); 2668 } 2669 } 2670 diff = rp->r_size - uiop->uio_loffset; 2671 mutex_exit(&rp->r_statelock); 2672 if (diff <= 0) 2673 break; 2674 if (diff < n) 2675 n = (uint_t)diff; 2676 2677 if (vpm_enable) { 2678 /* 2679 * Copy data. 2680 */ 2681 error = vpm_data_copy(vp, off + on, n, uiop, 2682 1, NULL, 0, S_READ); 2683 } else { 2684 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2685 S_READ); 2686 2687 error = uiomove(base + on, n, UIO_READ, uiop); 2688 } 2689 2690 if (!error) { 2691 /* 2692 * If read a whole block or read to eof, 2693 * won't need this buffer again soon. 2694 */ 2695 mutex_enter(&rp->r_statelock); 2696 if (n + on == MAXBSIZE || 2697 uiop->uio_loffset == rp->r_size) 2698 flags = SM_DONTNEED; 2699 else 2700 flags = 0; 2701 mutex_exit(&rp->r_statelock); 2702 if (vpm_enable) { 2703 error = vpm_sync_pages(vp, off, n, flags); 2704 } else { 2705 error = segmap_release(segkmap, base, flags); 2706 } 2707 } else { 2708 if (vpm_enable) { 2709 (void) vpm_sync_pages(vp, off, n, 0); 2710 } else { 2711 (void) segmap_release(segkmap, base, 0); 2712 } 2713 } 2714 } while (!error && uiop->uio_resid > 0); 2715 2716 return (error); 2717 } 2718 2719 /* ARGSUSED */ 2720 static int 2721 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2722 caller_context_t *ct) 2723 { 2724 rlim64_t limit = uiop->uio_llimit; 2725 rnode4_t *rp; 2726 u_offset_t off; 2727 caddr_t base; 2728 uint_t flags; 2729 int remainder; 2730 size_t n; 2731 int on; 2732 int error; 2733 int resid; 2734 u_offset_t offset; 2735 mntinfo4_t *mi; 2736 uint_t bsize; 2737 2738 rp = VTOR4(vp); 2739 2740 if (IS_SHADOW(vp, rp)) 2741 vp = RTOV4(rp); 2742 2743 if (vp->v_type != VREG) 2744 return (EISDIR); 2745 2746 mi = VTOMI4(vp); 2747 2748 if (nfs_zone() != mi->mi_zone) 2749 return (EIO); 2750 2751 if (uiop->uio_resid == 0) 2752 return (0); 2753 2754 mutex_enter(&rp->r_statelock); 2755 if (rp->r_flags & R4RECOVERRP) 2756 error = (rp->r_error ? rp->r_error : EIO); 2757 else 2758 error = 0; 2759 mutex_exit(&rp->r_statelock); 2760 if (error) 2761 return (error); 2762 2763 if (ioflag & FAPPEND) { 2764 struct vattr va; 2765 2766 /* 2767 * Must serialize if appending. 2768 */ 2769 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2770 nfs_rw_exit(&rp->r_rwlock); 2771 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2772 INTR4(vp))) 2773 return (EINTR); 2774 } 2775 2776 va.va_mask = AT_SIZE; 2777 error = nfs4getattr(vp, &va, cr); 2778 if (error) 2779 return (error); 2780 uiop->uio_loffset = va.va_size; 2781 } 2782 2783 offset = uiop->uio_loffset + uiop->uio_resid; 2784 2785 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2786 return (EINVAL); 2787 2788 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2789 limit = MAXOFFSET_T; 2790 2791 /* 2792 * Check to make sure that the process will not exceed 2793 * its limit on file size. It is okay to write up to 2794 * the limit, but not beyond. Thus, the write which 2795 * reaches the limit will be short and the next write 2796 * will return an error. 2797 */ 2798 remainder = 0; 2799 if (offset > uiop->uio_llimit) { 2800 remainder = offset - uiop->uio_llimit; 2801 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2802 if (uiop->uio_resid <= 0) { 2803 proc_t *p = ttoproc(curthread); 2804 2805 uiop->uio_resid += remainder; 2806 mutex_enter(&p->p_lock); 2807 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2808 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2809 mutex_exit(&p->p_lock); 2810 return (EFBIG); 2811 } 2812 } 2813 2814 /* update the change attribute, if we have a write delegation */ 2815 2816 mutex_enter(&rp->r_statev4_lock); 2817 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2818 rp->r_deleg_change++; 2819 2820 mutex_exit(&rp->r_statev4_lock); 2821 2822 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2823 return (EINTR); 2824 2825 /* 2826 * Bypass VM if caching has been disabled (e.g., locking) or if 2827 * using client-side direct I/O and the file is not mmap'd and 2828 * there are no cached pages. 2829 */ 2830 if ((vp->v_flag & VNOCACHE) || 2831 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2832 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) { 2833 size_t bufsize; 2834 int count; 2835 u_offset_t org_offset; 2836 stable_how4 stab_comm; 2837 nfs4_fwrite: 2838 if (rp->r_flags & R4STALE) { 2839 resid = uiop->uio_resid; 2840 offset = uiop->uio_loffset; 2841 error = rp->r_error; 2842 /* 2843 * A close may have cleared r_error, if so, 2844 * propagate ESTALE error return properly 2845 */ 2846 if (error == 0) 2847 error = ESTALE; 2848 goto bottom; 2849 } 2850 2851 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2852 base = kmem_alloc(bufsize, KM_SLEEP); 2853 do { 2854 if (ioflag & FDSYNC) 2855 stab_comm = DATA_SYNC4; 2856 else 2857 stab_comm = FILE_SYNC4; 2858 resid = uiop->uio_resid; 2859 offset = uiop->uio_loffset; 2860 count = MIN(uiop->uio_resid, bufsize); 2861 org_offset = uiop->uio_loffset; 2862 error = uiomove(base, count, UIO_WRITE, uiop); 2863 if (!error) { 2864 error = nfs4write(vp, base, org_offset, 2865 count, cr, &stab_comm); 2866 if (!error) { 2867 mutex_enter(&rp->r_statelock); 2868 if (rp->r_size < uiop->uio_loffset) 2869 rp->r_size = uiop->uio_loffset; 2870 mutex_exit(&rp->r_statelock); 2871 } 2872 } 2873 } while (!error && uiop->uio_resid > 0); 2874 kmem_free(base, bufsize); 2875 goto bottom; 2876 } 2877 2878 bsize = vp->v_vfsp->vfs_bsize; 2879 2880 do { 2881 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2882 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2883 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2884 2885 resid = uiop->uio_resid; 2886 offset = uiop->uio_loffset; 2887 2888 if (rp->r_flags & R4STALE) { 2889 error = rp->r_error; 2890 /* 2891 * A close may have cleared r_error, if so, 2892 * propagate ESTALE error return properly 2893 */ 2894 if (error == 0) 2895 error = ESTALE; 2896 break; 2897 } 2898 2899 /* 2900 * Don't create dirty pages faster than they 2901 * can be cleaned so that the system doesn't 2902 * get imbalanced. If the async queue is 2903 * maxed out, then wait for it to drain before 2904 * creating more dirty pages. Also, wait for 2905 * any threads doing pagewalks in the vop_getattr 2906 * entry points so that they don't block for 2907 * long periods. 2908 */ 2909 mutex_enter(&rp->r_statelock); 2910 while ((mi->mi_max_threads != 0 && 2911 rp->r_awcount > 2 * mi->mi_max_threads) || 2912 rp->r_gcount > 0) { 2913 if (INTR4(vp)) { 2914 klwp_t *lwp = ttolwp(curthread); 2915 2916 if (lwp != NULL) 2917 lwp->lwp_nostop++; 2918 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) { 2919 mutex_exit(&rp->r_statelock); 2920 if (lwp != NULL) 2921 lwp->lwp_nostop--; 2922 error = EINTR; 2923 goto bottom; 2924 } 2925 if (lwp != NULL) 2926 lwp->lwp_nostop--; 2927 } else 2928 cv_wait(&rp->r_cv, &rp->r_statelock); 2929 } 2930 mutex_exit(&rp->r_statelock); 2931 2932 /* 2933 * Touch the page and fault it in if it is not in core 2934 * before segmap_getmapflt or vpm_data_copy can lock it. 2935 * This is to avoid the deadlock if the buffer is mapped 2936 * to the same file through mmap which we want to write. 2937 */ 2938 uio_prefaultpages((long)n, uiop); 2939 2940 if (vpm_enable) { 2941 /* 2942 * It will use kpm mappings, so no need to 2943 * pass an address. 2944 */ 2945 error = writerp4(rp, NULL, n, uiop, 0); 2946 } else { 2947 if (segmap_kpm) { 2948 int pon = uiop->uio_loffset & PAGEOFFSET; 2949 size_t pn = MIN(PAGESIZE - pon, 2950 uiop->uio_resid); 2951 int pagecreate; 2952 2953 mutex_enter(&rp->r_statelock); 2954 pagecreate = (pon == 0) && (pn == PAGESIZE || 2955 uiop->uio_loffset + pn >= rp->r_size); 2956 mutex_exit(&rp->r_statelock); 2957 2958 base = segmap_getmapflt(segkmap, vp, off + on, 2959 pn, !pagecreate, S_WRITE); 2960 2961 error = writerp4(rp, base + pon, n, uiop, 2962 pagecreate); 2963 2964 } else { 2965 base = segmap_getmapflt(segkmap, vp, off + on, 2966 n, 0, S_READ); 2967 error = writerp4(rp, base + on, n, uiop, 0); 2968 } 2969 } 2970 2971 if (!error) { 2972 if (mi->mi_flags & MI4_NOAC) 2973 flags = SM_WRITE; 2974 else if ((uiop->uio_loffset % bsize) == 0 || 2975 IS_SWAPVP(vp)) { 2976 /* 2977 * Have written a whole block. 2978 * Start an asynchronous write 2979 * and mark the buffer to 2980 * indicate that it won't be 2981 * needed again soon. 2982 */ 2983 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2984 } else 2985 flags = 0; 2986 if ((ioflag & (FSYNC|FDSYNC)) || 2987 (rp->r_flags & R4OUTOFSPACE)) { 2988 flags &= ~SM_ASYNC; 2989 flags |= SM_WRITE; 2990 } 2991 if (vpm_enable) { 2992 error = vpm_sync_pages(vp, off, n, flags); 2993 } else { 2994 error = segmap_release(segkmap, base, flags); 2995 } 2996 } else { 2997 if (vpm_enable) { 2998 (void) vpm_sync_pages(vp, off, n, 0); 2999 } else { 3000 (void) segmap_release(segkmap, base, 0); 3001 } 3002 /* 3003 * In the event that we got an access error while 3004 * faulting in a page for a write-only file just 3005 * force a write. 3006 */ 3007 if (error == EACCES) 3008 goto nfs4_fwrite; 3009 } 3010 } while (!error && uiop->uio_resid > 0); 3011 3012 bottom: 3013 if (error) { 3014 uiop->uio_resid = resid + remainder; 3015 uiop->uio_loffset = offset; 3016 } else { 3017 uiop->uio_resid += remainder; 3018 3019 mutex_enter(&rp->r_statev4_lock); 3020 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 3021 gethrestime(&rp->r_attr.va_mtime); 3022 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3023 } 3024 mutex_exit(&rp->r_statev4_lock); 3025 } 3026 3027 nfs_rw_exit(&rp->r_lkserlock); 3028 3029 return (error); 3030 } 3031 3032 /* 3033 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 3034 */ 3035 static int 3036 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 3037 int flags, cred_t *cr) 3038 { 3039 struct buf *bp; 3040 int error; 3041 page_t *savepp; 3042 uchar_t fsdata; 3043 stable_how4 stab_comm; 3044 3045 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3046 bp = pageio_setup(pp, len, vp, flags); 3047 ASSERT(bp != NULL); 3048 3049 /* 3050 * pageio_setup should have set b_addr to 0. This 3051 * is correct since we want to do I/O on a page 3052 * boundary. bp_mapin will use this addr to calculate 3053 * an offset, and then set b_addr to the kernel virtual 3054 * address it allocated for us. 3055 */ 3056 ASSERT(bp->b_un.b_addr == 0); 3057 3058 bp->b_edev = 0; 3059 bp->b_dev = 0; 3060 bp->b_lblkno = lbtodb(off); 3061 bp->b_file = vp; 3062 bp->b_offset = (offset_t)off; 3063 bp_mapin(bp); 3064 3065 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 3066 freemem > desfree) 3067 stab_comm = UNSTABLE4; 3068 else 3069 stab_comm = FILE_SYNC4; 3070 3071 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 3072 3073 bp_mapout(bp); 3074 pageio_done(bp); 3075 3076 if (stab_comm == UNSTABLE4) 3077 fsdata = C_DELAYCOMMIT; 3078 else 3079 fsdata = C_NOCOMMIT; 3080 3081 savepp = pp; 3082 do { 3083 pp->p_fsdata = fsdata; 3084 } while ((pp = pp->p_next) != savepp); 3085 3086 return (error); 3087 } 3088 3089 /* 3090 */ 3091 static int 3092 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3093 { 3094 nfs4_open_owner_t *oop; 3095 nfs4_open_stream_t *osp; 3096 rnode4_t *rp = VTOR4(vp); 3097 mntinfo4_t *mi = VTOMI4(vp); 3098 int reopen_needed; 3099 3100 ASSERT(nfs_zone() == mi->mi_zone); 3101 3102 3103 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3104 if (!oop) 3105 return (EIO); 3106 3107 /* returns with 'os_sync_lock' held */ 3108 osp = find_open_stream(oop, rp); 3109 if (!osp) { 3110 open_owner_rele(oop); 3111 return (EIO); 3112 } 3113 3114 if (osp->os_failed_reopen) { 3115 mutex_exit(&osp->os_sync_lock); 3116 open_stream_rele(osp, rp); 3117 open_owner_rele(oop); 3118 return (EIO); 3119 } 3120 3121 /* 3122 * Determine whether a reopen is needed. If this 3123 * is a delegation open stream, then the os_delegation bit 3124 * should be set. 3125 */ 3126 3127 reopen_needed = osp->os_delegation; 3128 3129 mutex_exit(&osp->os_sync_lock); 3130 open_owner_rele(oop); 3131 3132 if (reopen_needed) { 3133 nfs4_error_zinit(ep); 3134 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3135 mutex_enter(&osp->os_sync_lock); 3136 if (ep->error || ep->stat || osp->os_failed_reopen) { 3137 mutex_exit(&osp->os_sync_lock); 3138 open_stream_rele(osp, rp); 3139 return (EIO); 3140 } 3141 mutex_exit(&osp->os_sync_lock); 3142 } 3143 open_stream_rele(osp, rp); 3144 3145 return (0); 3146 } 3147 3148 /* 3149 * Write to file. Writes to remote server in largest size 3150 * chunks that the server can handle. Write is synchronous. 3151 */ 3152 static int 3153 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3154 stable_how4 *stab_comm) 3155 { 3156 mntinfo4_t *mi; 3157 COMPOUND4args_clnt args; 3158 COMPOUND4res_clnt res; 3159 WRITE4args *wargs; 3160 WRITE4res *wres; 3161 nfs_argop4 argop[2]; 3162 nfs_resop4 *resop; 3163 int tsize; 3164 stable_how4 stable; 3165 rnode4_t *rp; 3166 int doqueue = 1; 3167 bool_t needrecov; 3168 nfs4_recov_state_t recov_state; 3169 nfs4_stateid_types_t sid_types; 3170 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3171 int recov; 3172 3173 rp = VTOR4(vp); 3174 mi = VTOMI4(vp); 3175 3176 ASSERT(nfs_zone() == mi->mi_zone); 3177 3178 stable = *stab_comm; 3179 *stab_comm = FILE_SYNC4; 3180 3181 needrecov = FALSE; 3182 recov_state.rs_flags = 0; 3183 recov_state.rs_num_retry_despite_err = 0; 3184 nfs4_init_stateid_types(&sid_types); 3185 3186 /* Is curthread the recovery thread? */ 3187 mutex_enter(&mi->mi_lock); 3188 recov = (mi->mi_recovthread == curthread); 3189 mutex_exit(&mi->mi_lock); 3190 3191 recov_retry: 3192 args.ctag = TAG_WRITE; 3193 args.array_len = 2; 3194 args.array = argop; 3195 3196 if (!recov) { 3197 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3198 &recov_state, NULL); 3199 if (e.error) 3200 return (e.error); 3201 } 3202 3203 /* 0. putfh target fh */ 3204 argop[0].argop = OP_CPUTFH; 3205 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3206 3207 /* 1. write */ 3208 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3209 3210 do { 3211 3212 wargs->offset = (offset4)offset; 3213 wargs->data_val = base; 3214 3215 if (mi->mi_io_kstats) { 3216 mutex_enter(&mi->mi_lock); 3217 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3218 mutex_exit(&mi->mi_lock); 3219 } 3220 3221 if ((vp->v_flag & VNOCACHE) || 3222 (rp->r_flags & R4DIRECTIO) || 3223 (mi->mi_flags & MI4_DIRECTIO)) 3224 tsize = MIN(mi->mi_stsize, count); 3225 else 3226 tsize = MIN(mi->mi_curwrite, count); 3227 wargs->data_len = (uint_t)tsize; 3228 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3229 3230 if (mi->mi_io_kstats) { 3231 mutex_enter(&mi->mi_lock); 3232 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3233 mutex_exit(&mi->mi_lock); 3234 } 3235 3236 if (!recov) { 3237 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3238 if (e.error && !needrecov) { 3239 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3240 &recov_state, needrecov); 3241 return (e.error); 3242 } 3243 } else { 3244 if (e.error) 3245 return (e.error); 3246 } 3247 3248 /* 3249 * Do handling of OLD_STATEID outside 3250 * of the normal recovery framework. 3251 * 3252 * If write receives a BAD stateid error while using a 3253 * delegation stateid, retry using the open stateid (if it 3254 * exists). If it doesn't have an open stateid, reopen the 3255 * file first, then retry. 3256 */ 3257 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3258 sid_types.cur_sid_type != SPEC_SID) { 3259 nfs4_save_stateid(&wargs->stateid, &sid_types); 3260 if (!recov) 3261 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3262 &recov_state, needrecov); 3263 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3264 goto recov_retry; 3265 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3266 sid_types.cur_sid_type == DEL_SID) { 3267 nfs4_save_stateid(&wargs->stateid, &sid_types); 3268 mutex_enter(&rp->r_statev4_lock); 3269 rp->r_deleg_return_pending = TRUE; 3270 mutex_exit(&rp->r_statev4_lock); 3271 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3272 if (!recov) 3273 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3274 &recov_state, needrecov); 3275 (void) xdr_free(xdr_COMPOUND4res_clnt, 3276 (caddr_t)&res); 3277 return (EIO); 3278 } 3279 if (!recov) 3280 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3281 &recov_state, needrecov); 3282 /* hold needed for nfs4delegreturn_thread */ 3283 VN_HOLD(vp); 3284 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3285 NFS4_DR_DISCARD), FALSE); 3286 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3287 goto recov_retry; 3288 } 3289 3290 if (needrecov) { 3291 bool_t abort; 3292 3293 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3294 "nfs4write: client got error %d, res.status %d" 3295 ", so start recovery", e.error, res.status)); 3296 3297 abort = nfs4_start_recovery(&e, 3298 VTOMI4(vp), vp, NULL, &wargs->stateid, 3299 NULL, OP_WRITE, NULL, NULL, NULL); 3300 if (!e.error) { 3301 e.error = geterrno4(res.status); 3302 (void) xdr_free(xdr_COMPOUND4res_clnt, 3303 (caddr_t)&res); 3304 } 3305 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3306 &recov_state, needrecov); 3307 if (abort == FALSE) 3308 goto recov_retry; 3309 return (e.error); 3310 } 3311 3312 if (res.status) { 3313 e.error = geterrno4(res.status); 3314 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3315 if (!recov) 3316 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3317 &recov_state, needrecov); 3318 return (e.error); 3319 } 3320 3321 resop = &res.array[1]; /* write res */ 3322 wres = &resop->nfs_resop4_u.opwrite; 3323 3324 if ((int)wres->count > tsize) { 3325 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3326 3327 zcmn_err(getzoneid(), CE_WARN, 3328 "nfs4write: server wrote %u, requested was %u", 3329 (int)wres->count, tsize); 3330 if (!recov) 3331 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3332 &recov_state, needrecov); 3333 return (EIO); 3334 } 3335 if (wres->committed == UNSTABLE4) { 3336 *stab_comm = UNSTABLE4; 3337 if (wargs->stable == DATA_SYNC4 || 3338 wargs->stable == FILE_SYNC4) { 3339 (void) xdr_free(xdr_COMPOUND4res_clnt, 3340 (caddr_t)&res); 3341 zcmn_err(getzoneid(), CE_WARN, 3342 "nfs4write: server %s did not commit " 3343 "to stable storage", 3344 rp->r_server->sv_hostname); 3345 if (!recov) 3346 nfs4_end_fop(VTOMI4(vp), vp, NULL, 3347 OH_WRITE, &recov_state, needrecov); 3348 return (EIO); 3349 } 3350 } 3351 3352 tsize = (int)wres->count; 3353 count -= tsize; 3354 base += tsize; 3355 offset += tsize; 3356 if (mi->mi_io_kstats) { 3357 mutex_enter(&mi->mi_lock); 3358 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3359 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3360 tsize; 3361 mutex_exit(&mi->mi_lock); 3362 } 3363 lwp_stat_update(LWP_STAT_OUBLK, 1); 3364 mutex_enter(&rp->r_statelock); 3365 if (rp->r_flags & R4HAVEVERF) { 3366 if (rp->r_writeverf != wres->writeverf) { 3367 nfs4_set_mod(vp); 3368 rp->r_writeverf = wres->writeverf; 3369 } 3370 } else { 3371 rp->r_writeverf = wres->writeverf; 3372 rp->r_flags |= R4HAVEVERF; 3373 } 3374 PURGE_ATTRCACHE4_LOCKED(rp); 3375 rp->r_flags |= R4WRITEMODIFIED; 3376 gethrestime(&rp->r_attr.va_mtime); 3377 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3378 mutex_exit(&rp->r_statelock); 3379 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3380 } while (count); 3381 3382 if (!recov) 3383 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, 3384 needrecov); 3385 3386 return (e.error); 3387 } 3388 3389 /* 3390 * Read from a file. Reads data in largest chunks our interface can handle. 3391 */ 3392 static int 3393 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3394 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3395 { 3396 mntinfo4_t *mi; 3397 COMPOUND4args_clnt args; 3398 COMPOUND4res_clnt res; 3399 READ4args *rargs; 3400 nfs_argop4 argop[2]; 3401 int tsize; 3402 int doqueue; 3403 rnode4_t *rp; 3404 int data_len; 3405 bool_t is_eof; 3406 bool_t needrecov = FALSE; 3407 nfs4_recov_state_t recov_state; 3408 nfs4_stateid_types_t sid_types; 3409 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3410 3411 rp = VTOR4(vp); 3412 mi = VTOMI4(vp); 3413 doqueue = 1; 3414 3415 ASSERT(nfs_zone() == mi->mi_zone); 3416 3417 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3418 3419 args.array_len = 2; 3420 args.array = argop; 3421 3422 nfs4_init_stateid_types(&sid_types); 3423 3424 recov_state.rs_flags = 0; 3425 recov_state.rs_num_retry_despite_err = 0; 3426 3427 recov_retry: 3428 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3429 &recov_state, NULL); 3430 if (e.error) 3431 return (e.error); 3432 3433 /* putfh target fh */ 3434 argop[0].argop = OP_CPUTFH; 3435 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3436 3437 /* read */ 3438 argop[1].argop = OP_READ; 3439 rargs = &argop[1].nfs_argop4_u.opread; 3440 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3441 OP_READ, &sid_types, async); 3442 3443 do { 3444 if (mi->mi_io_kstats) { 3445 mutex_enter(&mi->mi_lock); 3446 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3447 mutex_exit(&mi->mi_lock); 3448 } 3449 3450 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3451 "nfs4read: %s call, rp %s", 3452 needrecov ? "recov" : "first", 3453 rnode4info(rp))); 3454 3455 if ((vp->v_flag & VNOCACHE) || 3456 (rp->r_flags & R4DIRECTIO) || 3457 (mi->mi_flags & MI4_DIRECTIO)) 3458 tsize = MIN(mi->mi_tsize, count); 3459 else 3460 tsize = MIN(mi->mi_curread, count); 3461 3462 rargs->offset = (offset4)offset; 3463 rargs->count = (count4)tsize; 3464 rargs->res_data_val_alt = NULL; 3465 rargs->res_mblk = NULL; 3466 rargs->res_uiop = NULL; 3467 rargs->res_maxsize = 0; 3468 rargs->wlist = NULL; 3469 3470 if (uiop) 3471 rargs->res_uiop = uiop; 3472 else 3473 rargs->res_data_val_alt = base; 3474 rargs->res_maxsize = tsize; 3475 3476 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3477 #ifdef DEBUG 3478 if (nfs4read_error_inject) { 3479 res.status = nfs4read_error_inject; 3480 nfs4read_error_inject = 0; 3481 } 3482 #endif 3483 3484 if (mi->mi_io_kstats) { 3485 mutex_enter(&mi->mi_lock); 3486 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3487 mutex_exit(&mi->mi_lock); 3488 } 3489 3490 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3491 if (e.error != 0 && !needrecov) { 3492 nfs4_end_fop(mi, vp, NULL, OH_READ, 3493 &recov_state, needrecov); 3494 return (e.error); 3495 } 3496 3497 /* 3498 * Do proper retry for OLD and BAD stateid errors outside 3499 * of the normal recovery framework. There are two differences 3500 * between async and sync reads. The first is that we allow 3501 * retry on BAD_STATEID for async reads, but not sync reads. 3502 * The second is that we mark the file dead for a failed 3503 * attempt with a special stateid for sync reads, but just 3504 * return EIO for async reads. 3505 * 3506 * If a sync read receives a BAD stateid error while using a 3507 * delegation stateid, retry using the open stateid (if it 3508 * exists). If it doesn't have an open stateid, reopen the 3509 * file first, then retry. 3510 */ 3511 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3512 res.status == NFS4ERR_BAD_STATEID) && async) { 3513 nfs4_end_fop(mi, vp, NULL, OH_READ, 3514 &recov_state, needrecov); 3515 if (sid_types.cur_sid_type == SPEC_SID) { 3516 (void) xdr_free(xdr_COMPOUND4res_clnt, 3517 (caddr_t)&res); 3518 return (EIO); 3519 } 3520 nfs4_save_stateid(&rargs->stateid, &sid_types); 3521 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3522 goto recov_retry; 3523 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3524 !async && sid_types.cur_sid_type != SPEC_SID) { 3525 nfs4_save_stateid(&rargs->stateid, &sid_types); 3526 nfs4_end_fop(mi, vp, NULL, OH_READ, 3527 &recov_state, needrecov); 3528 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3529 goto recov_retry; 3530 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3531 sid_types.cur_sid_type == DEL_SID) { 3532 nfs4_save_stateid(&rargs->stateid, &sid_types); 3533 mutex_enter(&rp->r_statev4_lock); 3534 rp->r_deleg_return_pending = TRUE; 3535 mutex_exit(&rp->r_statev4_lock); 3536 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3537 nfs4_end_fop(mi, vp, NULL, OH_READ, 3538 &recov_state, needrecov); 3539 (void) xdr_free(xdr_COMPOUND4res_clnt, 3540 (caddr_t)&res); 3541 return (EIO); 3542 } 3543 nfs4_end_fop(mi, vp, NULL, OH_READ, 3544 &recov_state, needrecov); 3545 /* hold needed for nfs4delegreturn_thread */ 3546 VN_HOLD(vp); 3547 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3548 NFS4_DR_DISCARD), FALSE); 3549 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3550 goto recov_retry; 3551 } 3552 if (needrecov) { 3553 bool_t abort; 3554 3555 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3556 "nfs4read: initiating recovery\n")); 3557 abort = nfs4_start_recovery(&e, 3558 mi, vp, NULL, &rargs->stateid, 3559 NULL, OP_READ, NULL, NULL, NULL); 3560 nfs4_end_fop(mi, vp, NULL, OH_READ, 3561 &recov_state, needrecov); 3562 /* 3563 * Do not retry if we got OLD_STATEID using a special 3564 * stateid. This avoids looping with a broken server. 3565 */ 3566 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3567 sid_types.cur_sid_type == SPEC_SID) 3568 abort = TRUE; 3569 3570 if (abort == FALSE) { 3571 /* 3572 * Need to retry all possible stateids in 3573 * case the recovery error wasn't stateid 3574 * related or the stateids have become 3575 * stale (server reboot). 3576 */ 3577 nfs4_init_stateid_types(&sid_types); 3578 (void) xdr_free(xdr_COMPOUND4res_clnt, 3579 (caddr_t)&res); 3580 goto recov_retry; 3581 } 3582 3583 if (!e.error) { 3584 e.error = geterrno4(res.status); 3585 (void) xdr_free(xdr_COMPOUND4res_clnt, 3586 (caddr_t)&res); 3587 } 3588 return (e.error); 3589 } 3590 3591 if (res.status) { 3592 e.error = geterrno4(res.status); 3593 nfs4_end_fop(mi, vp, NULL, OH_READ, 3594 &recov_state, needrecov); 3595 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3596 return (e.error); 3597 } 3598 3599 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3600 count -= data_len; 3601 if (base) 3602 base += data_len; 3603 offset += data_len; 3604 if (mi->mi_io_kstats) { 3605 mutex_enter(&mi->mi_lock); 3606 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3607 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3608 mutex_exit(&mi->mi_lock); 3609 } 3610 lwp_stat_update(LWP_STAT_INBLK, 1); 3611 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3612 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3613 3614 } while (count && !is_eof); 3615 3616 *residp = count; 3617 3618 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3619 3620 return (e.error); 3621 } 3622 3623 /* ARGSUSED */ 3624 static int 3625 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp, 3626 caller_context_t *ct) 3627 { 3628 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3629 return (EIO); 3630 switch (cmd) { 3631 case _FIODIRECTIO: 3632 return (nfs4_directio(vp, (int)arg, cr)); 3633 default: 3634 return (ENOTTY); 3635 } 3636 } 3637 3638 /* ARGSUSED */ 3639 int 3640 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3641 caller_context_t *ct) 3642 { 3643 int error; 3644 rnode4_t *rp = VTOR4(vp); 3645 3646 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3647 return (EIO); 3648 /* 3649 * If it has been specified that the return value will 3650 * just be used as a hint, and we are only being asked 3651 * for size, fsid or rdevid, then return the client's 3652 * notion of these values without checking to make sure 3653 * that the attribute cache is up to date. 3654 * The whole point is to avoid an over the wire GETATTR 3655 * call. 3656 */ 3657 if (flags & ATTR_HINT) { 3658 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) { 3659 mutex_enter(&rp->r_statelock); 3660 if (vap->va_mask & AT_SIZE) 3661 vap->va_size = rp->r_size; 3662 if (vap->va_mask & AT_FSID) 3663 vap->va_fsid = rp->r_attr.va_fsid; 3664 if (vap->va_mask & AT_RDEV) 3665 vap->va_rdev = rp->r_attr.va_rdev; 3666 mutex_exit(&rp->r_statelock); 3667 return (0); 3668 } 3669 } 3670 3671 /* 3672 * Only need to flush pages if asking for the mtime 3673 * and if there any dirty pages or any outstanding 3674 * asynchronous (write) requests for this file. 3675 */ 3676 if (vap->va_mask & AT_MTIME) { 3677 rp = VTOR4(vp); 3678 if (nfs4_has_pages(vp)) { 3679 mutex_enter(&rp->r_statev4_lock); 3680 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3681 mutex_exit(&rp->r_statev4_lock); 3682 if (rp->r_flags & R4DIRTY || 3683 rp->r_awcount > 0) { 3684 mutex_enter(&rp->r_statelock); 3685 rp->r_gcount++; 3686 mutex_exit(&rp->r_statelock); 3687 error = 3688 nfs4_putpage(vp, (u_offset_t)0, 3689 0, 0, cr, NULL); 3690 mutex_enter(&rp->r_statelock); 3691 if (error && (error == ENOSPC || 3692 error == EDQUOT)) { 3693 if (!rp->r_error) 3694 rp->r_error = error; 3695 } 3696 if (--rp->r_gcount == 0) 3697 cv_broadcast(&rp->r_cv); 3698 mutex_exit(&rp->r_statelock); 3699 } 3700 } else { 3701 mutex_exit(&rp->r_statev4_lock); 3702 } 3703 } 3704 } 3705 return (nfs4getattr(vp, vap, cr)); 3706 } 3707 3708 int 3709 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3710 { 3711 /* 3712 * If these are the only two bits cleared 3713 * on the server then return 0 (OK) else 3714 * return 1 (BAD). 3715 */ 3716 on_client &= ~(S_ISUID|S_ISGID); 3717 if (on_client == from_server) 3718 return (0); 3719 else 3720 return (1); 3721 } 3722 3723 /*ARGSUSED4*/ 3724 static int 3725 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3726 caller_context_t *ct) 3727 { 3728 int error; 3729 3730 if (vap->va_mask & AT_NOSET) 3731 return (EINVAL); 3732 3733 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3734 return (EIO); 3735 3736 /* 3737 * Don't call secpolicy_vnode_setattr, the client cannot 3738 * use its cached attributes to make security decisions 3739 * as the server may be faking mode bits or mapping uid/gid. 3740 * Always just let the server to the checking. 3741 * If we provide the ability to remove basic priviledges 3742 * to setattr (e.g. basic without chmod) then we will 3743 * need to add a check here before calling the server. 3744 */ 3745 error = nfs4setattr(vp, vap, flags, cr, NULL); 3746 3747 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0) 3748 vnevent_truncate(vp, ct); 3749 3750 return (error); 3751 } 3752 3753 /* 3754 * To replace the "guarded" version 3 setattr, we use two types of compound 3755 * setattr requests: 3756 * 1. The "normal" setattr, used when the size of the file isn't being 3757 * changed - { Putfh <fh>; Setattr; Getattr }/ 3758 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3759 * with only ctime as the argument. If the server ctime differs from 3760 * what is cached on the client, the verify will fail, but we would 3761 * already have the ctime from the preceding getattr, so just set it 3762 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3763 * Setattr; Getattr }. 3764 * 3765 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3766 * this setattr and NULL if they are not. 3767 */ 3768 static int 3769 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3770 vsecattr_t *vsap) 3771 { 3772 COMPOUND4args_clnt args; 3773 COMPOUND4res_clnt res, *resp = NULL; 3774 nfs4_ga_res_t *garp = NULL; 3775 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3776 nfs_argop4 argop[5]; 3777 int verify_argop = -1; 3778 int setattr_argop = 1; 3779 nfs_resop4 *resop; 3780 vattr_t va; 3781 rnode4_t *rp; 3782 int doqueue = 1; 3783 uint_t mask = vap->va_mask; 3784 mode_t omode; 3785 vsecattr_t *vsp; 3786 timestruc_t ctime; 3787 bool_t needrecov = FALSE; 3788 nfs4_recov_state_t recov_state; 3789 nfs4_stateid_types_t sid_types; 3790 stateid4 stateid; 3791 hrtime_t t; 3792 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3793 servinfo4_t *svp; 3794 bitmap4 supp_attrs; 3795 3796 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3797 rp = VTOR4(vp); 3798 nfs4_init_stateid_types(&sid_types); 3799 3800 /* 3801 * Only need to flush pages if there are any pages and 3802 * if the file is marked as dirty in some fashion. The 3803 * file must be flushed so that we can accurately 3804 * determine the size of the file and the cached data 3805 * after the SETATTR returns. A file is considered to 3806 * be dirty if it is either marked with R4DIRTY, has 3807 * outstanding i/o's active, or is mmap'd. In this 3808 * last case, we can't tell whether there are dirty 3809 * pages, so we flush just to be sure. 3810 */ 3811 if (nfs4_has_pages(vp) && 3812 ((rp->r_flags & R4DIRTY) || 3813 rp->r_count > 0 || 3814 rp->r_mapcnt > 0)) { 3815 ASSERT(vp->v_type != VCHR); 3816 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL); 3817 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3818 mutex_enter(&rp->r_statelock); 3819 if (!rp->r_error) 3820 rp->r_error = e.error; 3821 mutex_exit(&rp->r_statelock); 3822 } 3823 } 3824 3825 if (mask & AT_SIZE) { 3826 /* 3827 * Verification setattr compound for non-deleg AT_SIZE: 3828 * { Putfh; Getattr; Verify; Setattr; Getattr } 3829 * Set ctime local here (outside the do_again label) 3830 * so that subsequent retries (after failed VERIFY) 3831 * will use ctime from GETATTR results (from failed 3832 * verify compound) as VERIFY arg. 3833 * If file has delegation, then VERIFY(time_metadata) 3834 * is of little added value, so don't bother. 3835 */ 3836 mutex_enter(&rp->r_statev4_lock); 3837 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3838 rp->r_deleg_return_pending) { 3839 numops = 5; 3840 ctime = rp->r_attr.va_ctime; 3841 } 3842 mutex_exit(&rp->r_statev4_lock); 3843 } 3844 3845 recov_state.rs_flags = 0; 3846 recov_state.rs_num_retry_despite_err = 0; 3847 3848 args.ctag = TAG_SETATTR; 3849 do_again: 3850 recov_retry: 3851 setattr_argop = numops - 2; 3852 3853 args.array = argop; 3854 args.array_len = numops; 3855 3856 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3857 if (e.error) 3858 return (e.error); 3859 3860 3861 /* putfh target fh */ 3862 argop[0].argop = OP_CPUTFH; 3863 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3864 3865 if (numops == 5) { 3866 /* 3867 * We only care about the ctime, but need to get mtime 3868 * and size for proper cache update. 3869 */ 3870 /* getattr */ 3871 argop[1].argop = OP_GETATTR; 3872 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3873 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3874 3875 /* verify - set later in loop */ 3876 verify_argop = 2; 3877 } 3878 3879 /* setattr */ 3880 svp = rp->r_server; 3881 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3882 supp_attrs = svp->sv_supp_attrs; 3883 nfs_rw_exit(&svp->sv_lock); 3884 3885 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3886 supp_attrs, &e.error, &sid_types); 3887 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3888 if (e.error) { 3889 /* req time field(s) overflow - return immediately */ 3890 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3891 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3892 opsetattr.obj_attributes); 3893 return (e.error); 3894 } 3895 omode = rp->r_attr.va_mode; 3896 3897 /* getattr */ 3898 argop[numops-1].argop = OP_GETATTR; 3899 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3900 /* 3901 * If we are setting the ACL (indicated only by vsap != NULL), request 3902 * the ACL in this getattr. The ACL returned from this getattr will be 3903 * used in updating the ACL cache. 3904 */ 3905 if (vsap != NULL) 3906 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3907 FATTR4_ACL_MASK; 3908 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3909 3910 /* 3911 * setattr iterates if the object size is set and the cached ctime 3912 * does not match the file ctime. In that case, verify the ctime first. 3913 */ 3914 3915 do { 3916 if (verify_argop != -1) { 3917 /* 3918 * Verify that the ctime match before doing setattr. 3919 */ 3920 va.va_mask = AT_CTIME; 3921 va.va_ctime = ctime; 3922 svp = rp->r_server; 3923 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3924 supp_attrs = svp->sv_supp_attrs; 3925 nfs_rw_exit(&svp->sv_lock); 3926 e.error = nfs4args_verify(&argop[verify_argop], &va, 3927 OP_VERIFY, supp_attrs); 3928 if (e.error) { 3929 /* req time field(s) overflow - return */ 3930 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3931 needrecov); 3932 break; 3933 } 3934 } 3935 3936 doqueue = 1; 3937 3938 t = gethrtime(); 3939 3940 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3941 3942 /* 3943 * Purge the access cache and ACL cache if changing either the 3944 * owner of the file, the group owner, or the mode. These may 3945 * change the access permissions of the file, so purge old 3946 * information and start over again. 3947 */ 3948 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3949 (void) nfs4_access_purge_rp(rp); 3950 if (rp->r_secattr != NULL) { 3951 mutex_enter(&rp->r_statelock); 3952 vsp = rp->r_secattr; 3953 rp->r_secattr = NULL; 3954 mutex_exit(&rp->r_statelock); 3955 if (vsp != NULL) 3956 nfs4_acl_free_cache(vsp); 3957 } 3958 } 3959 3960 /* 3961 * If res.array_len == numops, then everything succeeded, 3962 * except for possibly the final getattr. If only the 3963 * last getattr failed, give up, and don't try recovery. 3964 */ 3965 if (res.array_len == numops) { 3966 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3967 needrecov); 3968 if (! e.error) 3969 resp = &res; 3970 break; 3971 } 3972 3973 /* 3974 * if either rpc call failed or completely succeeded - done 3975 */ 3976 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3977 if (e.error) { 3978 PURGE_ATTRCACHE4(vp); 3979 if (!needrecov) { 3980 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3981 needrecov); 3982 break; 3983 } 3984 } 3985 3986 /* 3987 * Do proper retry for OLD_STATEID outside of the normal 3988 * recovery framework. 3989 */ 3990 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3991 sid_types.cur_sid_type != SPEC_SID && 3992 sid_types.cur_sid_type != NO_SID) { 3993 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3994 needrecov); 3995 nfs4_save_stateid(&stateid, &sid_types); 3996 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3997 opsetattr.obj_attributes); 3998 if (verify_argop != -1) { 3999 nfs4args_verify_free(&argop[verify_argop]); 4000 verify_argop = -1; 4001 } 4002 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4003 goto recov_retry; 4004 } 4005 4006 if (needrecov) { 4007 bool_t abort; 4008 4009 abort = nfs4_start_recovery(&e, 4010 VTOMI4(vp), vp, NULL, NULL, NULL, 4011 OP_SETATTR, NULL, NULL, NULL); 4012 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4013 needrecov); 4014 /* 4015 * Do not retry if we failed with OLD_STATEID using 4016 * a special stateid. This is done to avoid looping 4017 * with a broken server. 4018 */ 4019 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 4020 (sid_types.cur_sid_type == SPEC_SID || 4021 sid_types.cur_sid_type == NO_SID)) 4022 abort = TRUE; 4023 if (!e.error) { 4024 if (res.status == NFS4ERR_BADOWNER) 4025 nfs4_log_badowner(VTOMI4(vp), 4026 OP_SETATTR); 4027 4028 e.error = geterrno4(res.status); 4029 (void) xdr_free(xdr_COMPOUND4res_clnt, 4030 (caddr_t)&res); 4031 } 4032 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4033 opsetattr.obj_attributes); 4034 if (verify_argop != -1) { 4035 nfs4args_verify_free(&argop[verify_argop]); 4036 verify_argop = -1; 4037 } 4038 if (abort == FALSE) { 4039 /* 4040 * Need to retry all possible stateids in 4041 * case the recovery error wasn't stateid 4042 * related or the stateids have become 4043 * stale (server reboot). 4044 */ 4045 nfs4_init_stateid_types(&sid_types); 4046 goto recov_retry; 4047 } 4048 return (e.error); 4049 } 4050 4051 /* 4052 * Need to call nfs4_end_op before nfs4getattr to 4053 * avoid potential nfs4_start_op deadlock. See RFE 4054 * 4777612. Calls to nfs4_invalidate_pages() and 4055 * nfs4_purge_stale_fh() might also generate over the 4056 * wire calls which my cause nfs4_start_op() deadlock. 4057 */ 4058 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4059 4060 /* 4061 * Check to update lease. 4062 */ 4063 resp = &res; 4064 if (res.status == NFS4_OK) { 4065 break; 4066 } 4067 4068 /* 4069 * Check if verify failed to see if try again 4070 */ 4071 if ((verify_argop == -1) || (res.array_len != 3)) { 4072 /* 4073 * can't continue... 4074 */ 4075 if (res.status == NFS4ERR_BADOWNER) 4076 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 4077 4078 e.error = geterrno4(res.status); 4079 } else { 4080 /* 4081 * When the verify request fails, the client ctime is 4082 * not in sync with the server. This is the same as 4083 * the version 3 "not synchronized" error, and we 4084 * handle it in a similar manner (XXX do we need to???). 4085 * Use the ctime returned in the first getattr for 4086 * the input to the next verify. 4087 * If we couldn't get the attributes, then we give up 4088 * because we can't complete the operation as required. 4089 */ 4090 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 4091 } 4092 if (e.error) { 4093 PURGE_ATTRCACHE4(vp); 4094 nfs4_purge_stale_fh(e.error, vp, cr); 4095 } else { 4096 /* 4097 * retry with a new verify value 4098 */ 4099 ctime = garp->n4g_va.va_ctime; 4100 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4101 resp = NULL; 4102 } 4103 if (!e.error) { 4104 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4105 opsetattr.obj_attributes); 4106 if (verify_argop != -1) { 4107 nfs4args_verify_free(&argop[verify_argop]); 4108 verify_argop = -1; 4109 } 4110 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4111 goto do_again; 4112 } 4113 } while (!e.error); 4114 4115 if (e.error) { 4116 /* 4117 * If we are here, rfs4call has an irrecoverable error - return 4118 */ 4119 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4120 opsetattr.obj_attributes); 4121 if (verify_argop != -1) { 4122 nfs4args_verify_free(&argop[verify_argop]); 4123 verify_argop = -1; 4124 } 4125 if (resp) 4126 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4127 return (e.error); 4128 } 4129 4130 4131 4132 /* 4133 * If changing the size of the file, invalidate 4134 * any local cached data which is no longer part 4135 * of the file. We also possibly invalidate the 4136 * last page in the file. We could use 4137 * pvn_vpzero(), but this would mark the page as 4138 * modified and require it to be written back to 4139 * the server for no particularly good reason. 4140 * This way, if we access it, then we bring it 4141 * back in. A read should be cheaper than a 4142 * write. 4143 */ 4144 if (mask & AT_SIZE) { 4145 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4146 } 4147 4148 /* either no error or one of the postop getattr failed */ 4149 4150 /* 4151 * XXX Perform a simplified version of wcc checking. Instead of 4152 * have another getattr to get pre-op, just purge cache if 4153 * any of the ops prior to and including the getattr failed. 4154 * If the getattr succeeded then update the attrcache accordingly. 4155 */ 4156 4157 garp = NULL; 4158 if (res.status == NFS4_OK) { 4159 /* 4160 * Last getattr 4161 */ 4162 resop = &res.array[numops - 1]; 4163 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4164 } 4165 /* 4166 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4167 * rather than filling it. See the function itself for details. 4168 */ 4169 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4170 if (garp != NULL) { 4171 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4172 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4173 vs_ace4_destroy(&garp->n4g_vsa); 4174 } else { 4175 if (vsap != NULL) { 4176 /* 4177 * The ACL was supposed to be set and to be 4178 * returned in the last getattr of this 4179 * compound, but for some reason the getattr 4180 * result doesn't contain the ACL. In this 4181 * case, purge the ACL cache. 4182 */ 4183 if (rp->r_secattr != NULL) { 4184 mutex_enter(&rp->r_statelock); 4185 vsp = rp->r_secattr; 4186 rp->r_secattr = NULL; 4187 mutex_exit(&rp->r_statelock); 4188 if (vsp != NULL) 4189 nfs4_acl_free_cache(vsp); 4190 } 4191 } 4192 } 4193 } 4194 4195 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4196 /* 4197 * Set the size, rather than relying on getting it updated 4198 * via a GETATTR. With delegations the client tries to 4199 * suppress GETATTR calls. 4200 */ 4201 mutex_enter(&rp->r_statelock); 4202 rp->r_size = vap->va_size; 4203 mutex_exit(&rp->r_statelock); 4204 } 4205 4206 /* 4207 * Can free up request args and res 4208 */ 4209 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4210 opsetattr.obj_attributes); 4211 if (verify_argop != -1) { 4212 nfs4args_verify_free(&argop[verify_argop]); 4213 verify_argop = -1; 4214 } 4215 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4216 4217 /* 4218 * Some servers will change the mode to clear the setuid 4219 * and setgid bits when changing the uid or gid. The 4220 * client needs to compensate appropriately. 4221 */ 4222 if (mask & (AT_UID | AT_GID)) { 4223 int terror, do_setattr; 4224 4225 do_setattr = 0; 4226 va.va_mask = AT_MODE; 4227 terror = nfs4getattr(vp, &va, cr); 4228 if (!terror && 4229 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4230 (!(mask & AT_MODE) && va.va_mode != omode))) { 4231 va.va_mask = AT_MODE; 4232 if (mask & AT_MODE) { 4233 /* 4234 * We asked the mode to be changed and what 4235 * we just got from the server in getattr is 4236 * not what we wanted it to be, so set it now. 4237 */ 4238 va.va_mode = vap->va_mode; 4239 do_setattr = 1; 4240 } else { 4241 /* 4242 * We did not ask the mode to be changed, 4243 * Check to see that the server just cleared 4244 * I_SUID and I_GUID from it. If not then 4245 * set mode to omode with UID/GID cleared. 4246 */ 4247 if (nfs4_compare_modes(va.va_mode, omode)) { 4248 omode &= ~(S_ISUID|S_ISGID); 4249 va.va_mode = omode; 4250 do_setattr = 1; 4251 } 4252 } 4253 4254 if (do_setattr) 4255 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4256 } 4257 } 4258 4259 return (e.error); 4260 } 4261 4262 /* ARGSUSED */ 4263 static int 4264 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct) 4265 { 4266 COMPOUND4args_clnt args; 4267 COMPOUND4res_clnt res; 4268 int doqueue; 4269 uint32_t acc, resacc, argacc; 4270 rnode4_t *rp; 4271 cred_t *cred, *ncr, *ncrfree = NULL; 4272 nfs4_access_type_t cacc; 4273 int num_ops; 4274 nfs_argop4 argop[3]; 4275 nfs_resop4 *resop; 4276 bool_t needrecov = FALSE, do_getattr; 4277 nfs4_recov_state_t recov_state; 4278 int rpc_error; 4279 hrtime_t t; 4280 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4281 mntinfo4_t *mi = VTOMI4(vp); 4282 4283 if (nfs_zone() != mi->mi_zone) 4284 return (EIO); 4285 4286 acc = 0; 4287 if (mode & VREAD) 4288 acc |= ACCESS4_READ; 4289 if (mode & VWRITE) { 4290 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4291 return (EROFS); 4292 if (vp->v_type == VDIR) 4293 acc |= ACCESS4_DELETE; 4294 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4295 } 4296 if (mode & VEXEC) { 4297 if (vp->v_type == VDIR) 4298 acc |= ACCESS4_LOOKUP; 4299 else 4300 acc |= ACCESS4_EXECUTE; 4301 } 4302 4303 if (VTOR4(vp)->r_acache != NULL) { 4304 e.error = nfs4_validate_caches(vp, cr); 4305 if (e.error) 4306 return (e.error); 4307 } 4308 4309 rp = VTOR4(vp); 4310 if (vp->v_type == VDIR) 4311 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4312 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4313 else 4314 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4315 ACCESS4_EXECUTE; 4316 recov_state.rs_flags = 0; 4317 recov_state.rs_num_retry_despite_err = 0; 4318 4319 cred = cr; 4320 /* 4321 * ncr and ncrfree both initially 4322 * point to the memory area returned 4323 * by crnetadjust(); 4324 * ncrfree not NULL when exiting means 4325 * that we need to release it 4326 */ 4327 ncr = crnetadjust(cred); 4328 ncrfree = ncr; 4329 4330 tryagain: 4331 cacc = nfs4_access_check(rp, acc, cred); 4332 if (cacc == NFS4_ACCESS_ALLOWED) { 4333 if (ncrfree != NULL) 4334 crfree(ncrfree); 4335 return (0); 4336 } 4337 if (cacc == NFS4_ACCESS_DENIED) { 4338 /* 4339 * If the cred can be adjusted, try again 4340 * with the new cred. 4341 */ 4342 if (ncr != NULL) { 4343 cred = ncr; 4344 ncr = NULL; 4345 goto tryagain; 4346 } 4347 if (ncrfree != NULL) 4348 crfree(ncrfree); 4349 return (EACCES); 4350 } 4351 4352 recov_retry: 4353 /* 4354 * Don't take with r_statev4_lock here. r_deleg_type could 4355 * change as soon as lock is released. Since it is an int, 4356 * there is no atomicity issue. 4357 */ 4358 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4359 num_ops = do_getattr ? 3 : 2; 4360 4361 args.ctag = TAG_ACCESS; 4362 4363 args.array_len = num_ops; 4364 args.array = argop; 4365 4366 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4367 &recov_state, NULL)) { 4368 if (ncrfree != NULL) 4369 crfree(ncrfree); 4370 return (e.error); 4371 } 4372 4373 /* putfh target fh */ 4374 argop[0].argop = OP_CPUTFH; 4375 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4376 4377 /* access */ 4378 argop[1].argop = OP_ACCESS; 4379 argop[1].nfs_argop4_u.opaccess.access = argacc; 4380 4381 /* getattr */ 4382 if (do_getattr) { 4383 argop[2].argop = OP_GETATTR; 4384 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4385 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4386 } 4387 4388 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4389 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4390 rnode4info(VTOR4(vp)))); 4391 4392 doqueue = 1; 4393 t = gethrtime(); 4394 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4395 rpc_error = e.error; 4396 4397 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4398 if (needrecov) { 4399 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4400 "nfs4_access: initiating recovery\n")); 4401 4402 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4403 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) { 4404 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4405 &recov_state, needrecov); 4406 if (!e.error) 4407 (void) xdr_free(xdr_COMPOUND4res_clnt, 4408 (caddr_t)&res); 4409 goto recov_retry; 4410 } 4411 } 4412 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4413 4414 if (e.error) 4415 goto out; 4416 4417 if (res.status) { 4418 e.error = geterrno4(res.status); 4419 /* 4420 * This might generate over the wire calls throught 4421 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4422 * here to avoid a deadlock. 4423 */ 4424 nfs4_purge_stale_fh(e.error, vp, cr); 4425 goto out; 4426 } 4427 resop = &res.array[1]; /* access res */ 4428 4429 resacc = resop->nfs_resop4_u.opaccess.access; 4430 4431 if (do_getattr) { 4432 resop++; /* getattr res */ 4433 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4434 t, cr, FALSE, NULL); 4435 } 4436 4437 if (!e.error) { 4438 nfs4_access_cache(rp, argacc, resacc, cred); 4439 /* 4440 * we just cached results with cred; if cred is the 4441 * adjusted credentials from crnetadjust, we do not want 4442 * to release them before exiting: hence setting ncrfree 4443 * to NULL 4444 */ 4445 if (cred != cr) 4446 ncrfree = NULL; 4447 /* XXX check the supported bits too? */ 4448 if ((acc & resacc) != acc) { 4449 /* 4450 * The following code implements the semantic 4451 * that a setuid root program has *at least* the 4452 * permissions of the user that is running the 4453 * program. See rfs3call() for more portions 4454 * of the implementation of this functionality. 4455 */ 4456 /* XXX-LP */ 4457 if (ncr != NULL) { 4458 (void) xdr_free(xdr_COMPOUND4res_clnt, 4459 (caddr_t)&res); 4460 cred = ncr; 4461 ncr = NULL; 4462 goto tryagain; 4463 } 4464 e.error = EACCES; 4465 } 4466 } 4467 4468 out: 4469 if (!rpc_error) 4470 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4471 4472 if (ncrfree != NULL) 4473 crfree(ncrfree); 4474 4475 return (e.error); 4476 } 4477 4478 /* ARGSUSED */ 4479 static int 4480 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct) 4481 { 4482 COMPOUND4args_clnt args; 4483 COMPOUND4res_clnt res; 4484 int doqueue; 4485 rnode4_t *rp; 4486 nfs_argop4 argop[3]; 4487 nfs_resop4 *resop; 4488 READLINK4res *lr_res; 4489 nfs4_ga_res_t *garp; 4490 uint_t len; 4491 char *linkdata; 4492 bool_t needrecov = FALSE; 4493 nfs4_recov_state_t recov_state; 4494 hrtime_t t; 4495 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4496 4497 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4498 return (EIO); 4499 /* 4500 * Can't readlink anything other than a symbolic link. 4501 */ 4502 if (vp->v_type != VLNK) 4503 return (EINVAL); 4504 4505 rp = VTOR4(vp); 4506 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4507 e.error = nfs4_validate_caches(vp, cr); 4508 if (e.error) 4509 return (e.error); 4510 mutex_enter(&rp->r_statelock); 4511 if (rp->r_symlink.contents != NULL) { 4512 e.error = uiomove(rp->r_symlink.contents, 4513 rp->r_symlink.len, UIO_READ, uiop); 4514 mutex_exit(&rp->r_statelock); 4515 return (e.error); 4516 } 4517 mutex_exit(&rp->r_statelock); 4518 } 4519 recov_state.rs_flags = 0; 4520 recov_state.rs_num_retry_despite_err = 0; 4521 4522 recov_retry: 4523 args.array_len = 3; 4524 args.array = argop; 4525 args.ctag = TAG_READLINK; 4526 4527 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4528 if (e.error) { 4529 return (e.error); 4530 } 4531 4532 /* 0. putfh symlink fh */ 4533 argop[0].argop = OP_CPUTFH; 4534 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4535 4536 /* 1. readlink */ 4537 argop[1].argop = OP_READLINK; 4538 4539 /* 2. getattr */ 4540 argop[2].argop = OP_GETATTR; 4541 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4542 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4543 4544 doqueue = 1; 4545 4546 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4547 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4548 rnode4info(VTOR4(vp)))); 4549 4550 t = gethrtime(); 4551 4552 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4553 4554 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4555 if (needrecov) { 4556 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4557 "nfs4_readlink: initiating recovery\n")); 4558 4559 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4560 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) { 4561 if (!e.error) 4562 (void) xdr_free(xdr_COMPOUND4res_clnt, 4563 (caddr_t)&res); 4564 4565 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4566 needrecov); 4567 goto recov_retry; 4568 } 4569 } 4570 4571 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4572 4573 if (e.error) 4574 return (e.error); 4575 4576 /* 4577 * There is an path in the code below which calls 4578 * nfs4_purge_stale_fh(), which may generate otw calls through 4579 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4580 * here to avoid nfs4_start_op() deadlock. 4581 */ 4582 4583 if (res.status && (res.array_len < args.array_len)) { 4584 /* 4585 * either Putfh or Link failed 4586 */ 4587 e.error = geterrno4(res.status); 4588 nfs4_purge_stale_fh(e.error, vp, cr); 4589 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4590 return (e.error); 4591 } 4592 4593 resop = &res.array[1]; /* readlink res */ 4594 lr_res = &resop->nfs_resop4_u.opreadlink; 4595 4596 /* 4597 * treat symlink names as data 4598 */ 4599 linkdata = utf8_to_str(&lr_res->link, &len, NULL); 4600 if (linkdata != NULL) { 4601 int uio_len = len - 1; 4602 /* len includes null byte, which we won't uiomove */ 4603 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4604 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4605 mutex_enter(&rp->r_statelock); 4606 if (rp->r_symlink.contents == NULL) { 4607 rp->r_symlink.contents = linkdata; 4608 rp->r_symlink.len = uio_len; 4609 rp->r_symlink.size = len; 4610 mutex_exit(&rp->r_statelock); 4611 } else { 4612 mutex_exit(&rp->r_statelock); 4613 kmem_free(linkdata, len); 4614 } 4615 } else { 4616 kmem_free(linkdata, len); 4617 } 4618 } 4619 if (res.status == NFS4_OK) { 4620 resop++; /* getattr res */ 4621 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4622 } 4623 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4624 4625 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4626 4627 /* 4628 * The over the wire error for attempting to readlink something 4629 * other than a symbolic link is ENXIO. However, we need to 4630 * return EINVAL instead of ENXIO, so we map it here. 4631 */ 4632 return (e.error == ENXIO ? EINVAL : e.error); 4633 } 4634 4635 /* 4636 * Flush local dirty pages to stable storage on the server. 4637 * 4638 * If FNODSYNC is specified, then there is nothing to do because 4639 * metadata changes are not cached on the client before being 4640 * sent to the server. 4641 */ 4642 /* ARGSUSED */ 4643 static int 4644 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 4645 { 4646 int error; 4647 4648 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4649 return (0); 4650 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4651 return (EIO); 4652 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4653 if (!error) 4654 error = VTOR4(vp)->r_error; 4655 return (error); 4656 } 4657 4658 /* 4659 * Weirdness: if the file was removed or the target of a rename 4660 * operation while it was open, it got renamed instead. Here we 4661 * remove the renamed file. 4662 */ 4663 /* ARGSUSED */ 4664 void 4665 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4666 { 4667 rnode4_t *rp; 4668 4669 ASSERT(vp != DNLC_NO_VNODE); 4670 4671 rp = VTOR4(vp); 4672 4673 if (IS_SHADOW(vp, rp)) { 4674 sv_inactive(vp); 4675 return; 4676 } 4677 4678 /* 4679 * If this is coming from the wrong zone, we let someone in the right 4680 * zone take care of it asynchronously. We can get here due to 4681 * VN_RELE() being called from pageout() or fsflush(). This call may 4682 * potentially turn into an expensive no-op if, for instance, v_count 4683 * gets incremented in the meantime, but it's still correct. 4684 */ 4685 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4686 nfs4_async_inactive(vp, cr); 4687 return; 4688 } 4689 4690 /* 4691 * Some of the cleanup steps might require over-the-wire 4692 * operations. Since VOP_INACTIVE can get called as a result of 4693 * other over-the-wire operations (e.g., an attribute cache update 4694 * can lead to a DNLC purge), doing those steps now would lead to a 4695 * nested call to the recovery framework, which can deadlock. So 4696 * do any over-the-wire cleanups asynchronously, in a separate 4697 * thread. 4698 */ 4699 4700 mutex_enter(&rp->r_os_lock); 4701 mutex_enter(&rp->r_statelock); 4702 mutex_enter(&rp->r_statev4_lock); 4703 4704 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4705 mutex_exit(&rp->r_statev4_lock); 4706 mutex_exit(&rp->r_statelock); 4707 mutex_exit(&rp->r_os_lock); 4708 nfs4_async_inactive(vp, cr); 4709 return; 4710 } 4711 4712 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4713 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4714 mutex_exit(&rp->r_statev4_lock); 4715 mutex_exit(&rp->r_statelock); 4716 mutex_exit(&rp->r_os_lock); 4717 nfs4_async_inactive(vp, cr); 4718 return; 4719 } 4720 4721 if (rp->r_unldvp != NULL) { 4722 mutex_exit(&rp->r_statev4_lock); 4723 mutex_exit(&rp->r_statelock); 4724 mutex_exit(&rp->r_os_lock); 4725 nfs4_async_inactive(vp, cr); 4726 return; 4727 } 4728 mutex_exit(&rp->r_statev4_lock); 4729 mutex_exit(&rp->r_statelock); 4730 mutex_exit(&rp->r_os_lock); 4731 4732 rp4_addfree(rp, cr); 4733 } 4734 4735 /* 4736 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4737 * various bits of state. The caller must not refer to vp after this call. 4738 */ 4739 4740 void 4741 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4742 { 4743 rnode4_t *rp = VTOR4(vp); 4744 nfs4_recov_state_t recov_state; 4745 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4746 vnode_t *unldvp; 4747 char *unlname; 4748 cred_t *unlcred; 4749 COMPOUND4args_clnt args; 4750 COMPOUND4res_clnt res, *resp; 4751 nfs_argop4 argop[2]; 4752 int doqueue; 4753 #ifdef DEBUG 4754 char *name; 4755 #endif 4756 4757 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4758 ASSERT(!IS_SHADOW(vp, rp)); 4759 4760 #ifdef DEBUG 4761 name = fn_name(VTOSV(vp)->sv_name); 4762 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4763 "release vnode %s", name)); 4764 kmem_free(name, MAXNAMELEN); 4765 #endif 4766 4767 if (vp->v_type == VREG) { 4768 bool_t recov_failed = FALSE; 4769 4770 e.error = nfs4close_all(vp, cr); 4771 if (e.error) { 4772 /* Check to see if recovery failed */ 4773 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4774 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4775 recov_failed = TRUE; 4776 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4777 if (!recov_failed) { 4778 mutex_enter(&rp->r_statelock); 4779 if (rp->r_flags & R4RECOVERR) 4780 recov_failed = TRUE; 4781 mutex_exit(&rp->r_statelock); 4782 } 4783 if (recov_failed) { 4784 NFS4_DEBUG(nfs4_client_recov_debug, 4785 (CE_NOTE, "nfs4_inactive_otw: " 4786 "close failed (recovery failure)")); 4787 } 4788 } 4789 } 4790 4791 redo: 4792 if (rp->r_unldvp == NULL) { 4793 rp4_addfree(rp, cr); 4794 return; 4795 } 4796 4797 /* 4798 * Save the vnode pointer for the directory where the 4799 * unlinked-open file got renamed, then set it to NULL 4800 * to prevent another thread from getting here before 4801 * we're done with the remove. While we have the 4802 * statelock, make local copies of the pertinent rnode 4803 * fields. If we weren't to do this in an atomic way, the 4804 * the unl* fields could become inconsistent with respect 4805 * to each other due to a race condition between this 4806 * code and nfs_remove(). See bug report 1034328. 4807 */ 4808 mutex_enter(&rp->r_statelock); 4809 if (rp->r_unldvp == NULL) { 4810 mutex_exit(&rp->r_statelock); 4811 rp4_addfree(rp, cr); 4812 return; 4813 } 4814 4815 unldvp = rp->r_unldvp; 4816 rp->r_unldvp = NULL; 4817 unlname = rp->r_unlname; 4818 rp->r_unlname = NULL; 4819 unlcred = rp->r_unlcred; 4820 rp->r_unlcred = NULL; 4821 mutex_exit(&rp->r_statelock); 4822 4823 /* 4824 * If there are any dirty pages left, then flush 4825 * them. This is unfortunate because they just 4826 * may get thrown away during the remove operation, 4827 * but we have to do this for correctness. 4828 */ 4829 if (nfs4_has_pages(vp) && 4830 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4831 ASSERT(vp->v_type != VCHR); 4832 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL); 4833 if (e.error) { 4834 mutex_enter(&rp->r_statelock); 4835 if (!rp->r_error) 4836 rp->r_error = e.error; 4837 mutex_exit(&rp->r_statelock); 4838 } 4839 } 4840 4841 recov_state.rs_flags = 0; 4842 recov_state.rs_num_retry_despite_err = 0; 4843 recov_retry_remove: 4844 /* 4845 * Do the remove operation on the renamed file 4846 */ 4847 args.ctag = TAG_INACTIVE; 4848 4849 /* 4850 * Remove ops: putfh dir; remove 4851 */ 4852 args.array_len = 2; 4853 args.array = argop; 4854 4855 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4856 if (e.error) { 4857 kmem_free(unlname, MAXNAMELEN); 4858 crfree(unlcred); 4859 VN_RELE(unldvp); 4860 /* 4861 * Try again; this time around r_unldvp will be NULL, so we'll 4862 * just call rp4_addfree() and return. 4863 */ 4864 goto redo; 4865 } 4866 4867 /* putfh directory */ 4868 argop[0].argop = OP_CPUTFH; 4869 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4870 4871 /* remove */ 4872 argop[1].argop = OP_CREMOVE; 4873 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4874 4875 doqueue = 1; 4876 resp = &res; 4877 4878 #if 0 /* notyet */ 4879 /* 4880 * Can't do this yet. We may be being called from 4881 * dnlc_purge_XXX while that routine is holding a 4882 * mutex lock to the nc_rele list. The calls to 4883 * nfs3_cache_wcc_data may result in calls to 4884 * dnlc_purge_XXX. This will result in a deadlock. 4885 */ 4886 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4887 if (e.error) { 4888 PURGE_ATTRCACHE4(unldvp); 4889 resp = NULL; 4890 } else if (res.status) { 4891 e.error = geterrno4(res.status); 4892 PURGE_ATTRCACHE4(unldvp); 4893 /* 4894 * This code is inactive right now 4895 * but if made active there should 4896 * be a nfs4_end_op() call before 4897 * nfs4_purge_stale_fh to avoid start_op() 4898 * deadlock. See BugId: 4948726 4899 */ 4900 nfs4_purge_stale_fh(error, unldvp, cr); 4901 } else { 4902 nfs_resop4 *resop; 4903 REMOVE4res *rm_res; 4904 4905 resop = &res.array[1]; 4906 rm_res = &resop->nfs_resop4_u.opremove; 4907 /* 4908 * Update directory cache attribute, 4909 * readdir and dnlc caches. 4910 */ 4911 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4912 } 4913 #else 4914 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4915 4916 PURGE_ATTRCACHE4(unldvp); 4917 #endif 4918 4919 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4920 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4921 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 4922 if (!e.error) 4923 (void) xdr_free(xdr_COMPOUND4res_clnt, 4924 (caddr_t)&res); 4925 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4926 &recov_state, TRUE); 4927 goto recov_retry_remove; 4928 } 4929 } 4930 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4931 4932 /* 4933 * Release stuff held for the remove 4934 */ 4935 VN_RELE(unldvp); 4936 if (!e.error && resp) 4937 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4938 4939 kmem_free(unlname, MAXNAMELEN); 4940 crfree(unlcred); 4941 goto redo; 4942 } 4943 4944 /* 4945 * Remote file system operations having to do with directory manipulation. 4946 */ 4947 /* ARGSUSED3 */ 4948 int 4949 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4950 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 4951 int *direntflags, pathname_t *realpnp) 4952 { 4953 int error; 4954 vnode_t *vp, *avp = NULL; 4955 rnode4_t *drp; 4956 4957 *vpp = NULL; 4958 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4959 return (EPERM); 4960 /* 4961 * if LOOKUP_XATTR, must replace dvp (object) with 4962 * object's attrdir before continuing with lookup 4963 */ 4964 if (flags & LOOKUP_XATTR) { 4965 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4966 if (error) 4967 return (error); 4968 4969 dvp = avp; 4970 4971 /* 4972 * If lookup is for "", just return dvp now. The attrdir 4973 * has already been activated (from nfs4lookup_xattr), and 4974 * the caller will RELE the original dvp -- not 4975 * the attrdir. So, set vpp and return. 4976 * Currently, when the LOOKUP_XATTR flag is 4977 * passed to VOP_LOOKUP, the name is always empty, and 4978 * shortcircuiting here avoids 3 unneeded lock/unlock 4979 * pairs. 4980 * 4981 * If a non-empty name was provided, then it is the 4982 * attribute name, and it will be looked up below. 4983 */ 4984 if (*nm == '\0') { 4985 *vpp = dvp; 4986 return (0); 4987 } 4988 4989 /* 4990 * The vfs layer never sends a name when asking for the 4991 * attrdir, so we should never get here (unless of course 4992 * name is passed at some time in future -- at which time 4993 * we'll blow up here). 4994 */ 4995 ASSERT(0); 4996 } 4997 4998 drp = VTOR4(dvp); 4999 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5000 return (EINTR); 5001 5002 error = nfs4lookup(dvp, nm, vpp, cr, 0); 5003 nfs_rw_exit(&drp->r_rwlock); 5004 5005 /* 5006 * If vnode is a device, create special vnode. 5007 */ 5008 if (!error && ISVDEV((*vpp)->v_type)) { 5009 vp = *vpp; 5010 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 5011 VN_RELE(vp); 5012 } 5013 5014 return (error); 5015 } 5016 5017 /* ARGSUSED */ 5018 static int 5019 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 5020 { 5021 int error; 5022 rnode4_t *drp; 5023 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 5024 mntinfo4_t *mi; 5025 5026 mi = VTOMI4(dvp); 5027 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) && 5028 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS)) 5029 return (EINVAL); 5030 5031 drp = VTOR4(dvp); 5032 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 5033 return (EINTR); 5034 5035 mutex_enter(&drp->r_statelock); 5036 /* 5037 * If the server doesn't support xattrs just return EINVAL 5038 */ 5039 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 5040 mutex_exit(&drp->r_statelock); 5041 nfs_rw_exit(&drp->r_rwlock); 5042 return (EINVAL); 5043 } 5044 5045 /* 5046 * If there is a cached xattr directory entry, 5047 * use it as long as the attributes are valid. If the 5048 * attributes are not valid, take the simple approach and 5049 * free the cached value and re-fetch a new value. 5050 * 5051 * We don't negative entry cache for now, if we did we 5052 * would need to check if the file has changed on every 5053 * lookup. But xattrs don't exist very often and failing 5054 * an openattr is not much more expensive than and NVERIFY or GETATTR 5055 * so do an openattr over the wire for now. 5056 */ 5057 if (drp->r_xattr_dir != NULL) { 5058 if (ATTRCACHE4_VALID(dvp)) { 5059 VN_HOLD(drp->r_xattr_dir); 5060 *vpp = drp->r_xattr_dir; 5061 mutex_exit(&drp->r_statelock); 5062 nfs_rw_exit(&drp->r_rwlock); 5063 return (0); 5064 } 5065 VN_RELE(drp->r_xattr_dir); 5066 drp->r_xattr_dir = NULL; 5067 } 5068 mutex_exit(&drp->r_statelock); 5069 5070 error = nfs4openattr(dvp, vpp, cflag, cr); 5071 5072 nfs_rw_exit(&drp->r_rwlock); 5073 5074 return (error); 5075 } 5076 5077 static int 5078 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 5079 { 5080 int error; 5081 rnode4_t *drp; 5082 5083 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5084 5085 /* 5086 * If lookup is for "", just return dvp. Don't need 5087 * to send it over the wire, look it up in the dnlc, 5088 * or perform any access checks. 5089 */ 5090 if (*nm == '\0') { 5091 VN_HOLD(dvp); 5092 *vpp = dvp; 5093 return (0); 5094 } 5095 5096 /* 5097 * Can't do lookups in non-directories. 5098 */ 5099 if (dvp->v_type != VDIR) 5100 return (ENOTDIR); 5101 5102 /* 5103 * If lookup is for ".", just return dvp. Don't need 5104 * to send it over the wire or look it up in the dnlc, 5105 * just need to check access. 5106 */ 5107 if (nm[0] == '.' && nm[1] == '\0') { 5108 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5109 if (error) 5110 return (error); 5111 VN_HOLD(dvp); 5112 *vpp = dvp; 5113 return (0); 5114 } 5115 5116 drp = VTOR4(dvp); 5117 if (!(drp->r_flags & R4LOOKUP)) { 5118 mutex_enter(&drp->r_statelock); 5119 drp->r_flags |= R4LOOKUP; 5120 mutex_exit(&drp->r_statelock); 5121 } 5122 5123 *vpp = NULL; 5124 /* 5125 * Lookup this name in the DNLC. If there is no entry 5126 * lookup over the wire. 5127 */ 5128 if (!skipdnlc) 5129 *vpp = dnlc_lookup(dvp, nm); 5130 if (*vpp == NULL) { 5131 /* 5132 * We need to go over the wire to lookup the name. 5133 */ 5134 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5135 } 5136 5137 /* 5138 * We hit on the dnlc 5139 */ 5140 if (*vpp != DNLC_NO_VNODE || 5141 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5142 /* 5143 * But our attrs may not be valid. 5144 */ 5145 if (ATTRCACHE4_VALID(dvp)) { 5146 error = nfs4_waitfor_purge_complete(dvp); 5147 if (error) { 5148 VN_RELE(*vpp); 5149 *vpp = NULL; 5150 return (error); 5151 } 5152 5153 /* 5154 * If after the purge completes, check to make sure 5155 * our attrs are still valid. 5156 */ 5157 if (ATTRCACHE4_VALID(dvp)) { 5158 /* 5159 * If we waited for a purge we may have 5160 * lost our vnode so look it up again. 5161 */ 5162 VN_RELE(*vpp); 5163 *vpp = dnlc_lookup(dvp, nm); 5164 if (*vpp == NULL) 5165 return (nfs4lookupnew_otw(dvp, 5166 nm, vpp, cr)); 5167 5168 /* 5169 * The access cache should almost always hit 5170 */ 5171 error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5172 5173 if (error) { 5174 VN_RELE(*vpp); 5175 *vpp = NULL; 5176 return (error); 5177 } 5178 if (*vpp == DNLC_NO_VNODE) { 5179 VN_RELE(*vpp); 5180 *vpp = NULL; 5181 return (ENOENT); 5182 } 5183 return (0); 5184 } 5185 } 5186 } 5187 5188 ASSERT(*vpp != NULL); 5189 5190 /* 5191 * We may have gotten here we have one of the following cases: 5192 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5193 * need to validate them. 5194 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5195 * must validate. 5196 * 5197 * Go to the server and check if the directory has changed, if 5198 * it hasn't we are done and can use the dnlc entry. 5199 */ 5200 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5201 } 5202 5203 /* 5204 * Go to the server and check if the directory has changed, if 5205 * it hasn't we are done and can use the dnlc entry. If it 5206 * has changed we get a new copy of its attributes and check 5207 * the access for VEXEC, then relookup the filename and 5208 * get its filehandle and attributes. 5209 * 5210 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5211 * if the NVERIFY failed we must 5212 * purge the caches 5213 * cache new attributes (will set r_time_attr_inval) 5214 * cache new access 5215 * recheck VEXEC access 5216 * add name to dnlc, possibly negative 5217 * if LOOKUP succeeded 5218 * cache new attributes 5219 * else 5220 * set a new r_time_attr_inval for dvp 5221 * check to make sure we have access 5222 * 5223 * The vpp returned is the vnode passed in if the directory is valid, 5224 * a new vnode if successful lookup, or NULL on error. 5225 */ 5226 static int 5227 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5228 { 5229 COMPOUND4args_clnt args; 5230 COMPOUND4res_clnt res; 5231 fattr4 *ver_fattr; 5232 fattr4_change dchange; 5233 int32_t *ptr; 5234 int argoplist_size = 7 * sizeof (nfs_argop4); 5235 nfs_argop4 *argop; 5236 int doqueue; 5237 mntinfo4_t *mi; 5238 nfs4_recov_state_t recov_state; 5239 hrtime_t t; 5240 int isdotdot; 5241 vnode_t *nvp; 5242 nfs_fh4 *fhp; 5243 nfs4_sharedfh_t *sfhp; 5244 nfs4_access_type_t cacc; 5245 rnode4_t *nrp; 5246 rnode4_t *drp = VTOR4(dvp); 5247 nfs4_ga_res_t *garp = NULL; 5248 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5249 5250 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5251 ASSERT(nm != NULL); 5252 ASSERT(nm[0] != '\0'); 5253 ASSERT(dvp->v_type == VDIR); 5254 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5255 ASSERT(*vpp != NULL); 5256 5257 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5258 isdotdot = 1; 5259 args.ctag = TAG_LOOKUP_VPARENT; 5260 } else { 5261 /* 5262 * If dvp were a stub, it should have triggered and caused 5263 * a mount for us to get this far. 5264 */ 5265 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5266 5267 isdotdot = 0; 5268 args.ctag = TAG_LOOKUP_VALID; 5269 } 5270 5271 mi = VTOMI4(dvp); 5272 recov_state.rs_flags = 0; 5273 recov_state.rs_num_retry_despite_err = 0; 5274 5275 nvp = NULL; 5276 5277 /* Save the original mount point security information */ 5278 (void) save_mnt_secinfo(mi->mi_curr_serv); 5279 5280 recov_retry: 5281 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5282 &recov_state, NULL); 5283 if (e.error) { 5284 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5285 VN_RELE(*vpp); 5286 *vpp = NULL; 5287 return (e.error); 5288 } 5289 5290 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5291 5292 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5293 args.array_len = 7; 5294 args.array = argop; 5295 5296 /* 0. putfh file */ 5297 argop[0].argop = OP_CPUTFH; 5298 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5299 5300 /* 1. nverify the change info */ 5301 argop[1].argop = OP_NVERIFY; 5302 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5303 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5304 ver_fattr->attrlist4 = (char *)&dchange; 5305 ptr = (int32_t *)&dchange; 5306 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5307 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5308 5309 /* 2. getattr directory */ 5310 argop[2].argop = OP_GETATTR; 5311 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5312 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5313 5314 /* 3. access directory */ 5315 argop[3].argop = OP_ACCESS; 5316 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5317 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5318 5319 /* 4. lookup name */ 5320 if (isdotdot) { 5321 argop[4].argop = OP_LOOKUPP; 5322 } else { 5323 argop[4].argop = OP_CLOOKUP; 5324 argop[4].nfs_argop4_u.opclookup.cname = nm; 5325 } 5326 5327 /* 5. resulting file handle */ 5328 argop[5].argop = OP_GETFH; 5329 5330 /* 6. resulting file attributes */ 5331 argop[6].argop = OP_GETATTR; 5332 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5333 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5334 5335 doqueue = 1; 5336 t = gethrtime(); 5337 5338 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5339 5340 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5341 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5342 if (e.error != 0 && *vpp != NULL) 5343 VN_RELE(*vpp); 5344 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5345 &recov_state, FALSE); 5346 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5347 kmem_free(argop, argoplist_size); 5348 return (e.error); 5349 } 5350 5351 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5352 /* 5353 * For WRONGSEC of a non-dotdot case, send secinfo directly 5354 * from this thread, do not go thru the recovery thread since 5355 * we need the nm information. 5356 * 5357 * Not doing dotdot case because there is no specification 5358 * for (PUTFH, SECINFO "..") yet. 5359 */ 5360 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5361 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5362 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5363 &recov_state, FALSE); 5364 else 5365 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5366 &recov_state, TRUE); 5367 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5368 kmem_free(argop, argoplist_size); 5369 if (!e.error) 5370 goto recov_retry; 5371 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5372 VN_RELE(*vpp); 5373 *vpp = NULL; 5374 return (e.error); 5375 } 5376 5377 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5378 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5379 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5380 &recov_state, TRUE); 5381 5382 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5383 kmem_free(argop, argoplist_size); 5384 goto recov_retry; 5385 } 5386 } 5387 5388 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5389 5390 if (e.error || res.array_len == 0) { 5391 /* 5392 * If e.error isn't set, then reply has no ops (or we couldn't 5393 * be here). The only legal way to reply without an op array 5394 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5395 * be in the reply for all other status values. 5396 * 5397 * For valid replies without an ops array, return ENOTSUP 5398 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5399 * return EIO -- don't trust status. 5400 */ 5401 if (e.error == 0) 5402 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5403 ENOTSUP : EIO; 5404 VN_RELE(*vpp); 5405 *vpp = NULL; 5406 kmem_free(argop, argoplist_size); 5407 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5408 return (e.error); 5409 } 5410 5411 if (res.status != NFS4ERR_SAME) { 5412 e.error = geterrno4(res.status); 5413 5414 /* 5415 * The NVERIFY "failed" so the directory has changed 5416 * First make sure PUTFH succeeded and NVERIFY "failed" 5417 * cleanly. 5418 */ 5419 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5420 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5421 nfs4_purge_stale_fh(e.error, dvp, cr); 5422 VN_RELE(*vpp); 5423 *vpp = NULL; 5424 goto exit; 5425 } 5426 5427 /* 5428 * We know the NVERIFY "failed" so we must: 5429 * purge the caches (access and indirectly dnlc if needed) 5430 */ 5431 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5432 5433 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5434 nfs4_purge_stale_fh(e.error, dvp, cr); 5435 VN_RELE(*vpp); 5436 *vpp = NULL; 5437 goto exit; 5438 } 5439 5440 /* 5441 * Install new cached attributes for the directory 5442 */ 5443 nfs4_attr_cache(dvp, 5444 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5445 t, cr, FALSE, NULL); 5446 5447 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5448 nfs4_purge_stale_fh(e.error, dvp, cr); 5449 VN_RELE(*vpp); 5450 *vpp = NULL; 5451 e.error = geterrno4(res.status); 5452 goto exit; 5453 } 5454 5455 /* 5456 * Now we know the directory is valid, 5457 * cache new directory access 5458 */ 5459 nfs4_access_cache(drp, 5460 args.array[3].nfs_argop4_u.opaccess.access, 5461 res.array[3].nfs_resop4_u.opaccess.access, cr); 5462 5463 /* 5464 * recheck VEXEC access 5465 */ 5466 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5467 if (cacc != NFS4_ACCESS_ALLOWED) { 5468 /* 5469 * Directory permissions might have been revoked 5470 */ 5471 if (cacc == NFS4_ACCESS_DENIED) { 5472 e.error = EACCES; 5473 VN_RELE(*vpp); 5474 *vpp = NULL; 5475 goto exit; 5476 } 5477 5478 /* 5479 * Somehow we must not have asked for enough 5480 * so try a singleton ACCESS, should never happen. 5481 */ 5482 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5483 if (e.error) { 5484 VN_RELE(*vpp); 5485 *vpp = NULL; 5486 goto exit; 5487 } 5488 } 5489 5490 e.error = geterrno4(res.status); 5491 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5492 /* 5493 * The lookup failed, probably no entry 5494 */ 5495 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5496 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5497 } else { 5498 /* 5499 * Might be some other error, so remove 5500 * the dnlc entry to make sure we start all 5501 * over again, next time. 5502 */ 5503 dnlc_remove(dvp, nm); 5504 } 5505 VN_RELE(*vpp); 5506 *vpp = NULL; 5507 goto exit; 5508 } 5509 5510 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5511 /* 5512 * The file exists but we can't get its fh for 5513 * some unknown reason. Remove it from the dnlc 5514 * and error out to be safe. 5515 */ 5516 dnlc_remove(dvp, nm); 5517 VN_RELE(*vpp); 5518 *vpp = NULL; 5519 goto exit; 5520 } 5521 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5522 if (fhp->nfs_fh4_len == 0) { 5523 /* 5524 * The file exists but a bogus fh 5525 * some unknown reason. Remove it from the dnlc 5526 * and error out to be safe. 5527 */ 5528 e.error = ENOENT; 5529 dnlc_remove(dvp, nm); 5530 VN_RELE(*vpp); 5531 *vpp = NULL; 5532 goto exit; 5533 } 5534 sfhp = sfh4_get(fhp, mi); 5535 5536 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5537 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5538 5539 /* 5540 * Make the new rnode 5541 */ 5542 if (isdotdot) { 5543 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5544 if (e.error) { 5545 sfh4_rele(&sfhp); 5546 VN_RELE(*vpp); 5547 *vpp = NULL; 5548 goto exit; 5549 } 5550 /* 5551 * XXX if nfs4_make_dotdot uses an existing rnode 5552 * XXX it doesn't update the attributes. 5553 * XXX for now just save them again to save an OTW 5554 */ 5555 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5556 } else { 5557 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5558 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 5559 /* 5560 * If v_type == VNON, then garp was NULL because 5561 * the last op in the compound failed and makenfs4node 5562 * could not find the vnode for sfhp. It created 5563 * a new vnode, so we have nothing to purge here. 5564 */ 5565 if (nvp->v_type == VNON) { 5566 vattr_t vattr; 5567 5568 vattr.va_mask = AT_TYPE; 5569 /* 5570 * N.B. We've already called nfs4_end_fop above. 5571 */ 5572 e.error = nfs4getattr(nvp, &vattr, cr); 5573 if (e.error) { 5574 sfh4_rele(&sfhp); 5575 VN_RELE(*vpp); 5576 *vpp = NULL; 5577 VN_RELE(nvp); 5578 goto exit; 5579 } 5580 nvp->v_type = vattr.va_type; 5581 } 5582 } 5583 sfh4_rele(&sfhp); 5584 5585 nrp = VTOR4(nvp); 5586 mutex_enter(&nrp->r_statev4_lock); 5587 if (!nrp->created_v4) { 5588 mutex_exit(&nrp->r_statev4_lock); 5589 dnlc_update(dvp, nm, nvp); 5590 } else 5591 mutex_exit(&nrp->r_statev4_lock); 5592 5593 VN_RELE(*vpp); 5594 *vpp = nvp; 5595 } else { 5596 hrtime_t now; 5597 hrtime_t delta = 0; 5598 5599 e.error = 0; 5600 5601 /* 5602 * Because the NVERIFY "succeeded" we know that the 5603 * directory attributes are still valid 5604 * so update r_time_attr_inval 5605 */ 5606 now = gethrtime(); 5607 mutex_enter(&drp->r_statelock); 5608 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5609 delta = now - drp->r_time_attr_saved; 5610 if (delta < mi->mi_acdirmin) 5611 delta = mi->mi_acdirmin; 5612 else if (delta > mi->mi_acdirmax) 5613 delta = mi->mi_acdirmax; 5614 } 5615 drp->r_time_attr_inval = now + delta; 5616 mutex_exit(&drp->r_statelock); 5617 dnlc_update(dvp, nm, *vpp); 5618 5619 /* 5620 * Even though we have a valid directory attr cache 5621 * and dnlc entry, we may not have access. 5622 * This should almost always hit the cache. 5623 */ 5624 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5625 if (e.error) { 5626 VN_RELE(*vpp); 5627 *vpp = NULL; 5628 } 5629 5630 if (*vpp == DNLC_NO_VNODE) { 5631 VN_RELE(*vpp); 5632 *vpp = NULL; 5633 e.error = ENOENT; 5634 } 5635 } 5636 5637 exit: 5638 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5639 kmem_free(argop, argoplist_size); 5640 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5641 return (e.error); 5642 } 5643 5644 /* 5645 * We need to go over the wire to lookup the name, but 5646 * while we are there verify the directory has not 5647 * changed but if it has, get new attributes and check access 5648 * 5649 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5650 * NVERIFY GETATTR ACCESS 5651 * 5652 * With the results: 5653 * if the NVERIFY failed we must purge the caches, add new attributes, 5654 * and cache new access. 5655 * set a new r_time_attr_inval 5656 * add name to dnlc, possibly negative 5657 * if LOOKUP succeeded 5658 * cache new attributes 5659 */ 5660 static int 5661 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5662 { 5663 COMPOUND4args_clnt args; 5664 COMPOUND4res_clnt res; 5665 fattr4 *ver_fattr; 5666 fattr4_change dchange; 5667 int32_t *ptr; 5668 nfs4_ga_res_t *garp = NULL; 5669 int argoplist_size = 9 * sizeof (nfs_argop4); 5670 nfs_argop4 *argop; 5671 int doqueue; 5672 mntinfo4_t *mi; 5673 nfs4_recov_state_t recov_state; 5674 hrtime_t t; 5675 int isdotdot; 5676 vnode_t *nvp; 5677 nfs_fh4 *fhp; 5678 nfs4_sharedfh_t *sfhp; 5679 nfs4_access_type_t cacc; 5680 rnode4_t *nrp; 5681 rnode4_t *drp = VTOR4(dvp); 5682 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5683 5684 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5685 ASSERT(nm != NULL); 5686 ASSERT(nm[0] != '\0'); 5687 ASSERT(dvp->v_type == VDIR); 5688 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5689 ASSERT(*vpp == NULL); 5690 5691 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5692 isdotdot = 1; 5693 args.ctag = TAG_LOOKUP_PARENT; 5694 } else { 5695 /* 5696 * If dvp were a stub, it should have triggered and caused 5697 * a mount for us to get this far. 5698 */ 5699 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5700 5701 isdotdot = 0; 5702 args.ctag = TAG_LOOKUP; 5703 } 5704 5705 mi = VTOMI4(dvp); 5706 recov_state.rs_flags = 0; 5707 recov_state.rs_num_retry_despite_err = 0; 5708 5709 nvp = NULL; 5710 5711 /* Save the original mount point security information */ 5712 (void) save_mnt_secinfo(mi->mi_curr_serv); 5713 5714 recov_retry: 5715 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5716 &recov_state, NULL); 5717 if (e.error) { 5718 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5719 return (e.error); 5720 } 5721 5722 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5723 5724 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5725 args.array_len = 9; 5726 args.array = argop; 5727 5728 /* 0. putfh file */ 5729 argop[0].argop = OP_CPUTFH; 5730 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5731 5732 /* 1. savefh for the nverify */ 5733 argop[1].argop = OP_SAVEFH; 5734 5735 /* 2. lookup name */ 5736 if (isdotdot) { 5737 argop[2].argop = OP_LOOKUPP; 5738 } else { 5739 argop[2].argop = OP_CLOOKUP; 5740 argop[2].nfs_argop4_u.opclookup.cname = nm; 5741 } 5742 5743 /* 3. resulting file handle */ 5744 argop[3].argop = OP_GETFH; 5745 5746 /* 4. resulting file attributes */ 5747 argop[4].argop = OP_GETATTR; 5748 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5749 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5750 5751 /* 5. restorefh back the directory for the nverify */ 5752 argop[5].argop = OP_RESTOREFH; 5753 5754 /* 6. nverify the change info */ 5755 argop[6].argop = OP_NVERIFY; 5756 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5757 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5758 ver_fattr->attrlist4 = (char *)&dchange; 5759 ptr = (int32_t *)&dchange; 5760 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5761 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5762 5763 /* 7. getattr directory */ 5764 argop[7].argop = OP_GETATTR; 5765 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5766 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5767 5768 /* 8. access directory */ 5769 argop[8].argop = OP_ACCESS; 5770 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5771 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5772 5773 doqueue = 1; 5774 t = gethrtime(); 5775 5776 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5777 5778 if (!isdotdot && res.status == NFS4ERR_MOVED) { 5779 e.error = nfs4_setup_referral(dvp, nm, vpp, cr); 5780 if (e.error != 0 && *vpp != NULL) 5781 VN_RELE(*vpp); 5782 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5783 &recov_state, FALSE); 5784 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5785 kmem_free(argop, argoplist_size); 5786 return (e.error); 5787 } 5788 5789 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5790 /* 5791 * For WRONGSEC of a non-dotdot case, send secinfo directly 5792 * from this thread, do not go thru the recovery thread since 5793 * we need the nm information. 5794 * 5795 * Not doing dotdot case because there is no specification 5796 * for (PUTFH, SECINFO "..") yet. 5797 */ 5798 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5799 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5800 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5801 &recov_state, FALSE); 5802 else 5803 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5804 &recov_state, TRUE); 5805 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5806 kmem_free(argop, argoplist_size); 5807 if (!e.error) 5808 goto recov_retry; 5809 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5810 return (e.error); 5811 } 5812 5813 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5814 OP_LOOKUP, NULL, NULL, NULL) == FALSE) { 5815 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5816 &recov_state, TRUE); 5817 5818 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5819 kmem_free(argop, argoplist_size); 5820 goto recov_retry; 5821 } 5822 } 5823 5824 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5825 5826 if (e.error || res.array_len == 0) { 5827 /* 5828 * If e.error isn't set, then reply has no ops (or we couldn't 5829 * be here). The only legal way to reply without an op array 5830 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5831 * be in the reply for all other status values. 5832 * 5833 * For valid replies without an ops array, return ENOTSUP 5834 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5835 * return EIO -- don't trust status. 5836 */ 5837 if (e.error == 0) 5838 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5839 ENOTSUP : EIO; 5840 5841 kmem_free(argop, argoplist_size); 5842 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5843 return (e.error); 5844 } 5845 5846 e.error = geterrno4(res.status); 5847 5848 /* 5849 * The PUTFH and SAVEFH may have failed. 5850 */ 5851 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5852 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5853 nfs4_purge_stale_fh(e.error, dvp, cr); 5854 goto exit; 5855 } 5856 5857 /* 5858 * Check if the file exists, if it does delay entering 5859 * into the dnlc until after we update the directory 5860 * attributes so we don't cause it to get purged immediately. 5861 */ 5862 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5863 /* 5864 * The lookup failed, probably no entry 5865 */ 5866 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5867 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5868 goto exit; 5869 } 5870 5871 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5872 /* 5873 * The file exists but we can't get its fh for 5874 * some unknown reason. Error out to be safe. 5875 */ 5876 goto exit; 5877 } 5878 5879 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5880 if (fhp->nfs_fh4_len == 0) { 5881 /* 5882 * The file exists but a bogus fh 5883 * some unknown reason. Error out to be safe. 5884 */ 5885 e.error = EIO; 5886 goto exit; 5887 } 5888 sfhp = sfh4_get(fhp, mi); 5889 5890 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5891 sfh4_rele(&sfhp); 5892 goto exit; 5893 } 5894 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5895 5896 /* 5897 * The RESTOREFH may have failed 5898 */ 5899 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5900 sfh4_rele(&sfhp); 5901 e.error = EIO; 5902 goto exit; 5903 } 5904 5905 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5906 /* 5907 * First make sure the NVERIFY failed as we expected, 5908 * if it didn't then be conservative and error out 5909 * as we can't trust the directory. 5910 */ 5911 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5912 sfh4_rele(&sfhp); 5913 e.error = EIO; 5914 goto exit; 5915 } 5916 5917 /* 5918 * We know the NVERIFY "failed" so the directory has changed, 5919 * so we must: 5920 * purge the caches (access and indirectly dnlc if needed) 5921 */ 5922 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5923 5924 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5925 sfh4_rele(&sfhp); 5926 goto exit; 5927 } 5928 nfs4_attr_cache(dvp, 5929 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5930 t, cr, FALSE, NULL); 5931 5932 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5933 nfs4_purge_stale_fh(e.error, dvp, cr); 5934 sfh4_rele(&sfhp); 5935 e.error = geterrno4(res.status); 5936 goto exit; 5937 } 5938 5939 /* 5940 * Now we know the directory is valid, 5941 * cache new directory access 5942 */ 5943 nfs4_access_cache(drp, 5944 args.array[8].nfs_argop4_u.opaccess.access, 5945 res.array[8].nfs_resop4_u.opaccess.access, cr); 5946 5947 /* 5948 * recheck VEXEC access 5949 */ 5950 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5951 if (cacc != NFS4_ACCESS_ALLOWED) { 5952 /* 5953 * Directory permissions might have been revoked 5954 */ 5955 if (cacc == NFS4_ACCESS_DENIED) { 5956 sfh4_rele(&sfhp); 5957 e.error = EACCES; 5958 goto exit; 5959 } 5960 5961 /* 5962 * Somehow we must not have asked for enough 5963 * so try a singleton ACCESS should never happen 5964 */ 5965 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 5966 if (e.error) { 5967 sfh4_rele(&sfhp); 5968 goto exit; 5969 } 5970 } 5971 5972 e.error = geterrno4(res.status); 5973 } else { 5974 hrtime_t now; 5975 hrtime_t delta = 0; 5976 5977 e.error = 0; 5978 5979 /* 5980 * Because the NVERIFY "succeeded" we know that the 5981 * directory attributes are still valid 5982 * so update r_time_attr_inval 5983 */ 5984 now = gethrtime(); 5985 mutex_enter(&drp->r_statelock); 5986 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5987 delta = now - drp->r_time_attr_saved; 5988 if (delta < mi->mi_acdirmin) 5989 delta = mi->mi_acdirmin; 5990 else if (delta > mi->mi_acdirmax) 5991 delta = mi->mi_acdirmax; 5992 } 5993 drp->r_time_attr_inval = now + delta; 5994 mutex_exit(&drp->r_statelock); 5995 5996 /* 5997 * Even though we have a valid directory attr cache, 5998 * we may not have access. 5999 * This should almost always hit the cache. 6000 */ 6001 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL); 6002 if (e.error) { 6003 sfh4_rele(&sfhp); 6004 goto exit; 6005 } 6006 } 6007 6008 /* 6009 * Now we have successfully completed the lookup, if the 6010 * directory has changed we now have the valid attributes. 6011 * We also know we have directory access. 6012 * Create the new rnode and insert it in the dnlc. 6013 */ 6014 if (isdotdot) { 6015 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 6016 if (e.error) { 6017 sfh4_rele(&sfhp); 6018 goto exit; 6019 } 6020 /* 6021 * XXX if nfs4_make_dotdot uses an existing rnode 6022 * XXX it doesn't update the attributes. 6023 * XXX for now just save them again to save an OTW 6024 */ 6025 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 6026 } else { 6027 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 6028 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 6029 } 6030 sfh4_rele(&sfhp); 6031 6032 nrp = VTOR4(nvp); 6033 mutex_enter(&nrp->r_statev4_lock); 6034 if (!nrp->created_v4) { 6035 mutex_exit(&nrp->r_statev4_lock); 6036 dnlc_update(dvp, nm, nvp); 6037 } else 6038 mutex_exit(&nrp->r_statev4_lock); 6039 6040 *vpp = nvp; 6041 6042 exit: 6043 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6044 kmem_free(argop, argoplist_size); 6045 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 6046 return (e.error); 6047 } 6048 6049 #ifdef DEBUG 6050 void 6051 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 6052 { 6053 uint_t i, len; 6054 zoneid_t zoneid = getzoneid(); 6055 char *s; 6056 6057 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 6058 for (i = 0; i < argcnt; i++) { 6059 nfs_argop4 *op = &argbase[i]; 6060 switch (op->argop) { 6061 case OP_CPUTFH: 6062 case OP_PUTFH: 6063 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 6064 break; 6065 case OP_PUTROOTFH: 6066 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 6067 break; 6068 case OP_CLOOKUP: 6069 s = op->nfs_argop4_u.opclookup.cname; 6070 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6071 break; 6072 case OP_LOOKUP: 6073 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 6074 &len, NULL); 6075 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 6076 kmem_free(s, len); 6077 break; 6078 case OP_LOOKUPP: 6079 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 6080 break; 6081 case OP_GETFH: 6082 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 6083 break; 6084 case OP_GETATTR: 6085 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 6086 break; 6087 case OP_OPENATTR: 6088 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 6089 break; 6090 default: 6091 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 6092 op->argop); 6093 break; 6094 } 6095 } 6096 } 6097 #endif 6098 6099 /* 6100 * nfs4lookup_setup - constructs a multi-lookup compound request. 6101 * 6102 * Given the path "nm1/nm2/.../nmn", the following compound requests 6103 * may be created: 6104 * 6105 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 6106 * is faster, for now. 6107 * 6108 * l4_getattrs indicates the type of compound requested. 6109 * 6110 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 6111 * 6112 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 6113 * 6114 * total number of ops is n + 1. 6115 * 6116 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 6117 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 6118 * before the last component, and only get attributes 6119 * for the last component. Note that the second-to-last 6120 * pathname component is XATTR_RPATH, which does NOT go 6121 * over-the-wire as a lookup. 6122 * 6123 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 6124 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 6125 * 6126 * and total number of ops is n + 5. 6127 * 6128 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 6129 * attribute directory: create lookups plus an OPENATTR 6130 * replacing the last lookup. Note that the last pathname 6131 * component is XATTR_RPATH, which does NOT go over-the-wire 6132 * as a lookup. 6133 * 6134 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 6135 * Openattr; Getfh; Getattr } 6136 * 6137 * and total number of ops is n + 5. 6138 * 6139 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 6140 * nodes too. 6141 * 6142 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6143 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6144 * 6145 * and total number of ops is 3*n + 1. 6146 * 6147 * All cases: returns the index in the arg array of the final LOOKUP op, or 6148 * -1 if no LOOKUPs were used. 6149 */ 6150 int 6151 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6152 { 6153 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6154 nfs_argop4 *argbase, *argop; 6155 int arglen, argcnt; 6156 int n = 1; /* number of components */ 6157 int nga = 1; /* number of Getattr's in request */ 6158 char c = '\0', *s, *p; 6159 int lookup_idx = -1; 6160 int argoplist_size; 6161 6162 /* set lookuparg response result to 0 */ 6163 lookupargp->resp->status = NFS4_OK; 6164 6165 /* skip leading "/" or "." e.g. ".//./" if there is */ 6166 for (; ; nm++) { 6167 if (*nm != '/' && *nm != '.') 6168 break; 6169 6170 /* ".." is counted as 1 component */ 6171 if (*nm == '.' && *(nm + 1) != '/') 6172 break; 6173 } 6174 6175 /* 6176 * Find n = number of components - nm must be null terminated 6177 * Skip "." components. 6178 */ 6179 if (*nm != '\0') 6180 for (n = 1, s = nm; *s != '\0'; s++) { 6181 if ((*s == '/') && (*(s + 1) != '/') && 6182 (*(s + 1) != '\0') && 6183 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6184 *(s + 2) == '\0'))) 6185 n++; 6186 } 6187 else 6188 n = 0; 6189 6190 /* 6191 * nga is number of components that need Getfh+Getattr 6192 */ 6193 switch (l4_getattrs) { 6194 case LKP4_NO_ATTRIBUTES: 6195 nga = 0; 6196 break; 6197 case LKP4_ALL_ATTRIBUTES: 6198 nga = n; 6199 /* 6200 * Always have at least 1 getfh, getattr pair 6201 */ 6202 if (nga == 0) 6203 nga++; 6204 break; 6205 case LKP4_LAST_ATTRDIR: 6206 case LKP4_LAST_NAMED_ATTR: 6207 nga = n+1; 6208 break; 6209 } 6210 6211 /* 6212 * If change to use the filehandle attr instead of getfh 6213 * the following line can be deleted. 6214 */ 6215 nga *= 2; 6216 6217 /* 6218 * calculate number of ops in request as 6219 * header + trailer + lookups + getattrs 6220 */ 6221 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6222 6223 argoplist_size = arglen * sizeof (nfs_argop4); 6224 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6225 lookupargp->argsp->array = argop; 6226 6227 argcnt = lookupargp->header_len; 6228 argop += argcnt; 6229 6230 /* 6231 * loop and create a lookup op and possibly getattr/getfh for 6232 * each component. Skip "." components. 6233 */ 6234 for (s = nm; *s != '\0'; s = p) { 6235 /* 6236 * Set up a pathname struct for each component if needed 6237 */ 6238 while (*s == '/') 6239 s++; 6240 if (*s == '\0') 6241 break; 6242 6243 for (p = s; (*p != '/') && (*p != '\0'); p++) 6244 ; 6245 c = *p; 6246 *p = '\0'; 6247 6248 if (s[0] == '.' && s[1] == '\0') { 6249 *p = c; 6250 continue; 6251 } 6252 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6253 strcmp(s, XATTR_RPATH) == 0) { 6254 /* getfh XXX may not be needed in future */ 6255 argop->argop = OP_GETFH; 6256 argop++; 6257 argcnt++; 6258 6259 /* getattr */ 6260 argop->argop = OP_GETATTR; 6261 argop->nfs_argop4_u.opgetattr.attr_request = 6262 lookupargp->ga_bits; 6263 argop->nfs_argop4_u.opgetattr.mi = 6264 lookupargp->mi; 6265 argop++; 6266 argcnt++; 6267 6268 /* openattr */ 6269 argop->argop = OP_OPENATTR; 6270 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6271 strcmp(s, XATTR_RPATH) == 0) { 6272 /* openattr */ 6273 argop->argop = OP_OPENATTR; 6274 argop++; 6275 argcnt++; 6276 6277 /* getfh XXX may not be needed in future */ 6278 argop->argop = OP_GETFH; 6279 argop++; 6280 argcnt++; 6281 6282 /* getattr */ 6283 argop->argop = OP_GETATTR; 6284 argop->nfs_argop4_u.opgetattr.attr_request = 6285 lookupargp->ga_bits; 6286 argop->nfs_argop4_u.opgetattr.mi = 6287 lookupargp->mi; 6288 argop++; 6289 argcnt++; 6290 *p = c; 6291 continue; 6292 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6293 /* lookupp */ 6294 argop->argop = OP_LOOKUPP; 6295 } else { 6296 /* lookup */ 6297 argop->argop = OP_LOOKUP; 6298 (void) str_to_utf8(s, 6299 &argop->nfs_argop4_u.oplookup.objname); 6300 } 6301 lookup_idx = argcnt; 6302 argop++; 6303 argcnt++; 6304 6305 *p = c; 6306 6307 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6308 /* getfh XXX may not be needed in future */ 6309 argop->argop = OP_GETFH; 6310 argop++; 6311 argcnt++; 6312 6313 /* getattr */ 6314 argop->argop = OP_GETATTR; 6315 argop->nfs_argop4_u.opgetattr.attr_request = 6316 lookupargp->ga_bits; 6317 argop->nfs_argop4_u.opgetattr.mi = 6318 lookupargp->mi; 6319 argop++; 6320 argcnt++; 6321 } 6322 } 6323 6324 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6325 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6326 if (needgetfh) { 6327 /* stick in a post-lookup getfh */ 6328 argop->argop = OP_GETFH; 6329 argcnt++; 6330 argop++; 6331 } 6332 /* post-lookup getattr */ 6333 argop->argop = OP_GETATTR; 6334 argop->nfs_argop4_u.opgetattr.attr_request = 6335 lookupargp->ga_bits; 6336 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6337 argcnt++; 6338 } 6339 argcnt += lookupargp->trailer_len; /* actual op count */ 6340 lookupargp->argsp->array_len = argcnt; 6341 lookupargp->arglen = arglen; 6342 6343 #ifdef DEBUG 6344 if (nfs4_client_lookup_debug) 6345 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6346 #endif 6347 6348 return (lookup_idx); 6349 } 6350 6351 static int 6352 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6353 { 6354 COMPOUND4args_clnt args; 6355 COMPOUND4res_clnt res; 6356 GETFH4res *gf_res = NULL; 6357 nfs_argop4 argop[4]; 6358 nfs_resop4 *resop = NULL; 6359 nfs4_sharedfh_t *sfhp; 6360 hrtime_t t; 6361 nfs4_error_t e; 6362 6363 rnode4_t *drp; 6364 int doqueue = 1; 6365 vnode_t *vp; 6366 int needrecov = 0; 6367 nfs4_recov_state_t recov_state; 6368 6369 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6370 6371 *avp = NULL; 6372 recov_state.rs_flags = 0; 6373 recov_state.rs_num_retry_despite_err = 0; 6374 6375 recov_retry: 6376 /* COMPOUND: putfh, openattr, getfh, getattr */ 6377 args.array_len = 4; 6378 args.array = argop; 6379 args.ctag = TAG_OPENATTR; 6380 6381 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6382 if (e.error) 6383 return (e.error); 6384 6385 drp = VTOR4(dvp); 6386 6387 /* putfh */ 6388 argop[0].argop = OP_CPUTFH; 6389 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6390 6391 /* openattr */ 6392 argop[1].argop = OP_OPENATTR; 6393 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6394 6395 /* getfh */ 6396 argop[2].argop = OP_GETFH; 6397 6398 /* getattr */ 6399 argop[3].argop = OP_GETATTR; 6400 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6401 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6402 6403 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6404 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6405 rnode4info(drp))); 6406 6407 t = gethrtime(); 6408 6409 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6410 6411 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6412 if (needrecov) { 6413 bool_t abort; 6414 6415 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6416 "nfs4openattr: initiating recovery\n")); 6417 6418 abort = nfs4_start_recovery(&e, 6419 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6420 OP_OPENATTR, NULL, NULL, NULL); 6421 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6422 if (!e.error) { 6423 e.error = geterrno4(res.status); 6424 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6425 } 6426 if (abort == FALSE) 6427 goto recov_retry; 6428 return (e.error); 6429 } 6430 6431 if (e.error) { 6432 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6433 return (e.error); 6434 } 6435 6436 if (res.status) { 6437 /* 6438 * If OTW errro is NOTSUPP, then it should be 6439 * translated to EINVAL. All Solaris file system 6440 * implementations return EINVAL to the syscall layer 6441 * when the attrdir cannot be created due to an 6442 * implementation restriction or noxattr mount option. 6443 */ 6444 if (res.status == NFS4ERR_NOTSUPP) { 6445 mutex_enter(&drp->r_statelock); 6446 if (drp->r_xattr_dir) 6447 VN_RELE(drp->r_xattr_dir); 6448 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6449 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6450 mutex_exit(&drp->r_statelock); 6451 6452 e.error = EINVAL; 6453 } else { 6454 e.error = geterrno4(res.status); 6455 } 6456 6457 if (e.error) { 6458 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6459 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6460 needrecov); 6461 return (e.error); 6462 } 6463 } 6464 6465 resop = &res.array[0]; /* putfh res */ 6466 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6467 6468 resop = &res.array[1]; /* openattr res */ 6469 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6470 6471 resop = &res.array[2]; /* getfh res */ 6472 gf_res = &resop->nfs_resop4_u.opgetfh; 6473 if (gf_res->object.nfs_fh4_len == 0) { 6474 *avp = NULL; 6475 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6476 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6477 return (ENOENT); 6478 } 6479 6480 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6481 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6482 dvp->v_vfsp, t, cr, dvp, 6483 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp)); 6484 sfh4_rele(&sfhp); 6485 6486 if (e.error) 6487 PURGE_ATTRCACHE4(vp); 6488 6489 mutex_enter(&vp->v_lock); 6490 vp->v_flag |= V_XATTRDIR; 6491 mutex_exit(&vp->v_lock); 6492 6493 *avp = vp; 6494 6495 mutex_enter(&drp->r_statelock); 6496 if (drp->r_xattr_dir) 6497 VN_RELE(drp->r_xattr_dir); 6498 VN_HOLD(vp); 6499 drp->r_xattr_dir = vp; 6500 6501 /* 6502 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6503 * NULL. xattrs could be created at any time, and we have no 6504 * way to update pc4_xattr_exists in the base object if/when 6505 * it happens. 6506 */ 6507 drp->r_pathconf.pc4_xattr_valid = 0; 6508 6509 mutex_exit(&drp->r_statelock); 6510 6511 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6512 6513 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6514 6515 return (0); 6516 } 6517 6518 /* ARGSUSED */ 6519 static int 6520 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6521 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct, 6522 vsecattr_t *vsecp) 6523 { 6524 int error; 6525 vnode_t *vp = NULL; 6526 rnode4_t *rp; 6527 struct vattr vattr; 6528 rnode4_t *drp; 6529 vnode_t *tempvp; 6530 enum createmode4 createmode; 6531 bool_t must_trunc = FALSE; 6532 int truncating = 0; 6533 6534 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6535 return (EPERM); 6536 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6537 return (EINVAL); 6538 } 6539 6540 /* . and .. have special meaning in the protocol, reject them. */ 6541 6542 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6543 return (EISDIR); 6544 6545 drp = VTOR4(dvp); 6546 6547 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6548 return (EINTR); 6549 6550 top: 6551 /* 6552 * We make a copy of the attributes because the caller does not 6553 * expect us to change what va points to. 6554 */ 6555 vattr = *va; 6556 6557 /* 6558 * If the pathname is "", then dvp is the root vnode of 6559 * a remote file mounted over a local directory. 6560 * All that needs to be done is access 6561 * checking and truncation. Note that we avoid doing 6562 * open w/ create because the parent directory might 6563 * be in pseudo-fs and the open would fail. 6564 */ 6565 if (*nm == '\0') { 6566 error = 0; 6567 VN_HOLD(dvp); 6568 vp = dvp; 6569 must_trunc = TRUE; 6570 } else { 6571 /* 6572 * We need to go over the wire, just to be sure whether the 6573 * file exists or not. Using the DNLC can be dangerous in 6574 * this case when making a decision regarding existence. 6575 */ 6576 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6577 } 6578 6579 if (exclusive) 6580 createmode = EXCLUSIVE4; 6581 else 6582 createmode = GUARDED4; 6583 6584 /* 6585 * error would be set if the file does not exist on the 6586 * server, so lets go create it. 6587 */ 6588 if (error) { 6589 goto create_otw; 6590 } 6591 6592 /* 6593 * File does exist on the server 6594 */ 6595 if (exclusive == EXCL) 6596 error = EEXIST; 6597 else if (vp->v_type == VDIR && (mode & VWRITE)) 6598 error = EISDIR; 6599 else { 6600 /* 6601 * If vnode is a device, create special vnode. 6602 */ 6603 if (ISVDEV(vp->v_type)) { 6604 tempvp = vp; 6605 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6606 VN_RELE(tempvp); 6607 } 6608 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) { 6609 if ((vattr.va_mask & AT_SIZE) && 6610 vp->v_type == VREG) { 6611 rp = VTOR4(vp); 6612 /* 6613 * Check here for large file handled 6614 * by LF-unaware process (as 6615 * ufs_create() does) 6616 */ 6617 if (!(flags & FOFFMAX)) { 6618 mutex_enter(&rp->r_statelock); 6619 if (rp->r_size > MAXOFF32_T) 6620 error = EOVERFLOW; 6621 mutex_exit(&rp->r_statelock); 6622 } 6623 6624 /* if error is set then we need to return */ 6625 if (error) { 6626 nfs_rw_exit(&drp->r_rwlock); 6627 VN_RELE(vp); 6628 return (error); 6629 } 6630 6631 if (must_trunc) { 6632 vattr.va_mask = AT_SIZE; 6633 error = nfs4setattr(vp, &vattr, 0, cr, 6634 NULL); 6635 } else { 6636 /* 6637 * we know we have a regular file that already 6638 * exists and we may end up truncating the file 6639 * as a result of the open_otw, so flush out 6640 * any dirty pages for this file first. 6641 */ 6642 if (nfs4_has_pages(vp) && 6643 ((rp->r_flags & R4DIRTY) || 6644 rp->r_count > 0 || 6645 rp->r_mapcnt > 0)) { 6646 error = nfs4_putpage(vp, 6647 (offset_t)0, 0, 0, cr, ct); 6648 if (error && (error == ENOSPC || 6649 error == EDQUOT)) { 6650 mutex_enter( 6651 &rp->r_statelock); 6652 if (!rp->r_error) 6653 rp->r_error = 6654 error; 6655 mutex_exit( 6656 &rp->r_statelock); 6657 } 6658 } 6659 vattr.va_mask = (AT_SIZE | 6660 AT_TYPE | AT_MODE); 6661 vattr.va_type = VREG; 6662 createmode = UNCHECKED4; 6663 truncating = 1; 6664 goto create_otw; 6665 } 6666 } 6667 } 6668 } 6669 nfs_rw_exit(&drp->r_rwlock); 6670 if (error) { 6671 VN_RELE(vp); 6672 } else { 6673 vnode_t *tvp; 6674 rnode4_t *trp; 6675 tvp = vp; 6676 if (vp->v_type == VREG) { 6677 trp = VTOR4(vp); 6678 if (IS_SHADOW(vp, trp)) 6679 tvp = RTOV4(trp); 6680 } 6681 6682 if (must_trunc) { 6683 /* 6684 * existing file got truncated, notify. 6685 */ 6686 vnevent_create(tvp, ct); 6687 } 6688 6689 *vpp = vp; 6690 } 6691 return (error); 6692 6693 create_otw: 6694 dnlc_remove(dvp, nm); 6695 6696 ASSERT(vattr.va_mask & AT_TYPE); 6697 6698 /* 6699 * If not a regular file let nfs4mknod() handle it. 6700 */ 6701 if (vattr.va_type != VREG) { 6702 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6703 nfs_rw_exit(&drp->r_rwlock); 6704 return (error); 6705 } 6706 6707 /* 6708 * It _is_ a regular file. 6709 */ 6710 ASSERT(vattr.va_mask & AT_MODE); 6711 if (MANDMODE(vattr.va_mode)) { 6712 nfs_rw_exit(&drp->r_rwlock); 6713 return (EACCES); 6714 } 6715 6716 /* 6717 * If this happens to be a mknod of a regular file, then flags will 6718 * have neither FREAD or FWRITE. However, we must set at least one 6719 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6720 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6721 * set (based on openmode specified by app). 6722 */ 6723 if ((flags & (FREAD|FWRITE)) == 0) 6724 flags |= (FREAD|FWRITE); 6725 6726 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6727 6728 if (vp != NULL) { 6729 /* if create was successful, throw away the file's pages */ 6730 if (!error && (vattr.va_mask & AT_SIZE)) 6731 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6732 cr); 6733 /* release the lookup hold */ 6734 VN_RELE(vp); 6735 vp = NULL; 6736 } 6737 6738 /* 6739 * validate that we opened a regular file. This handles a misbehaving 6740 * server that returns an incorrect FH. 6741 */ 6742 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6743 error = EISDIR; 6744 VN_RELE(*vpp); 6745 } 6746 6747 /* 6748 * If this is not an exclusive create, then the CREATE 6749 * request will be made with the GUARDED mode set. This 6750 * means that the server will return EEXIST if the file 6751 * exists. The file could exist because of a retransmitted 6752 * request. In this case, we recover by starting over and 6753 * checking to see whether the file exists. This second 6754 * time through it should and a CREATE request will not be 6755 * sent. 6756 * 6757 * This handles the problem of a dangling CREATE request 6758 * which contains attributes which indicate that the file 6759 * should be truncated. This retransmitted request could 6760 * possibly truncate valid data in the file if not caught 6761 * by the duplicate request mechanism on the server or if 6762 * not caught by other means. The scenario is: 6763 * 6764 * Client transmits CREATE request with size = 0 6765 * Client times out, retransmits request. 6766 * Response to the first request arrives from the server 6767 * and the client proceeds on. 6768 * Client writes data to the file. 6769 * The server now processes retransmitted CREATE request 6770 * and truncates file. 6771 * 6772 * The use of the GUARDED CREATE request prevents this from 6773 * happening because the retransmitted CREATE would fail 6774 * with EEXIST and would not truncate the file. 6775 */ 6776 if (error == EEXIST && exclusive == NONEXCL) { 6777 #ifdef DEBUG 6778 nfs4_create_misses++; 6779 #endif 6780 goto top; 6781 } 6782 nfs_rw_exit(&drp->r_rwlock); 6783 if (truncating && !error && *vpp) { 6784 vnode_t *tvp; 6785 rnode4_t *trp; 6786 /* 6787 * existing file got truncated, notify. 6788 */ 6789 tvp = *vpp; 6790 trp = VTOR4(tvp); 6791 if (IS_SHADOW(tvp, trp)) 6792 tvp = RTOV4(trp); 6793 vnevent_create(tvp, ct); 6794 } 6795 return (error); 6796 } 6797 6798 /* 6799 * Create compound (for mkdir, mknod, symlink): 6800 * { Putfh <dfh>; Create; Getfh; Getattr } 6801 * It's okay if setattr failed to set gid - this is not considered 6802 * an error, but purge attrs in that case. 6803 */ 6804 static int 6805 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6806 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6807 { 6808 int need_end_op = FALSE; 6809 COMPOUND4args_clnt args; 6810 COMPOUND4res_clnt res, *resp = NULL; 6811 nfs_argop4 *argop; 6812 nfs_resop4 *resop; 6813 int doqueue; 6814 mntinfo4_t *mi; 6815 rnode4_t *drp = VTOR4(dvp); 6816 change_info4 *cinfo; 6817 GETFH4res *gf_res; 6818 struct vattr vattr; 6819 vnode_t *vp; 6820 fattr4 *crattr; 6821 bool_t needrecov = FALSE; 6822 nfs4_recov_state_t recov_state; 6823 nfs4_sharedfh_t *sfhp = NULL; 6824 hrtime_t t; 6825 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6826 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6827 dirattr_info_t dinfo, *dinfop; 6828 servinfo4_t *svp; 6829 bitmap4 supp_attrs; 6830 6831 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6832 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6833 6834 mi = VTOMI4(dvp); 6835 6836 /* 6837 * Make sure we properly deal with setting the right gid 6838 * on a new directory to reflect the parent's setgid bit 6839 */ 6840 setgid_flag = 0; 6841 if (type == NF4DIR) { 6842 struct vattr dva; 6843 6844 va->va_mode &= ~VSGID; 6845 dva.va_mask = AT_MODE | AT_GID; 6846 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) { 6847 6848 /* 6849 * If the parent's directory has the setgid bit set 6850 * _and_ the client was able to get a valid mapping 6851 * for the parent dir's owner_group, we want to 6852 * append NVERIFY(owner_group == dva.va_gid) and 6853 * SETTATTR to the CREATE compound. 6854 */ 6855 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6856 setgid_flag = 1; 6857 va->va_mode |= VSGID; 6858 if (dva.va_gid != GID_NOBODY) { 6859 va->va_mask |= AT_GID; 6860 va->va_gid = dva.va_gid; 6861 } 6862 } 6863 } 6864 } 6865 6866 /* 6867 * Create ops: 6868 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6869 * 5:restorefh(dir) 6:getattr(dir) 6870 * 6871 * if (setgid) 6872 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6873 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6874 * 8:nverify 9:setattr 6875 */ 6876 if (setgid_flag) { 6877 numops = 10; 6878 idx_create = 1; 6879 idx_fattr = 3; 6880 } else { 6881 numops = 7; 6882 idx_create = 2; 6883 idx_fattr = 4; 6884 } 6885 6886 ASSERT(nfs_zone() == mi->mi_zone); 6887 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6888 return (EINTR); 6889 } 6890 recov_state.rs_flags = 0; 6891 recov_state.rs_num_retry_despite_err = 0; 6892 6893 argoplist_size = numops * sizeof (nfs_argop4); 6894 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6895 6896 recov_retry: 6897 if (type == NF4LNK) 6898 args.ctag = TAG_SYMLINK; 6899 else if (type == NF4DIR) 6900 args.ctag = TAG_MKDIR; 6901 else 6902 args.ctag = TAG_MKNOD; 6903 6904 args.array_len = numops; 6905 args.array = argop; 6906 6907 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6908 nfs_rw_exit(&drp->r_rwlock); 6909 kmem_free(argop, argoplist_size); 6910 return (e.error); 6911 } 6912 need_end_op = TRUE; 6913 6914 6915 /* 0: putfh directory */ 6916 argop[0].argop = OP_CPUTFH; 6917 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6918 6919 /* 1/2: Create object */ 6920 argop[idx_create].argop = OP_CCREATE; 6921 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6922 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6923 if (type == NF4LNK) { 6924 /* 6925 * symlink, treat name as data 6926 */ 6927 ASSERT(data != NULL); 6928 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6929 (char *)data; 6930 } 6931 if (type == NF4BLK || type == NF4CHR) { 6932 ASSERT(data != NULL); 6933 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6934 *((specdata4 *)data); 6935 } 6936 6937 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6938 6939 svp = drp->r_server; 6940 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6941 supp_attrs = svp->sv_supp_attrs; 6942 nfs_rw_exit(&svp->sv_lock); 6943 6944 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6945 nfs_rw_exit(&drp->r_rwlock); 6946 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6947 e.error = EINVAL; 6948 kmem_free(argop, argoplist_size); 6949 return (e.error); 6950 } 6951 6952 /* 2/3: getfh fh of created object */ 6953 ASSERT(idx_create + 1 == idx_fattr - 1); 6954 argop[idx_create + 1].argop = OP_GETFH; 6955 6956 /* 3/4: getattr of new object */ 6957 argop[idx_fattr].argop = OP_GETATTR; 6958 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6959 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6960 6961 if (setgid_flag) { 6962 vattr_t _v; 6963 6964 argop[4].argop = OP_SAVEFH; 6965 6966 argop[5].argop = OP_CPUTFH; 6967 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6968 6969 argop[6].argop = OP_GETATTR; 6970 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6971 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6972 6973 argop[7].argop = OP_RESTOREFH; 6974 6975 /* 6976 * nverify 6977 * 6978 * XXX - Revisit the last argument to nfs4_end_op() 6979 * once 5020486 is fixed. 6980 */ 6981 _v.va_mask = AT_GID; 6982 _v.va_gid = va->va_gid; 6983 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6984 supp_attrs)) { 6985 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6986 nfs_rw_exit(&drp->r_rwlock); 6987 nfs4_fattr4_free(crattr); 6988 kmem_free(argop, argoplist_size); 6989 return (e.error); 6990 } 6991 6992 /* 6993 * setattr 6994 * 6995 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6996 * so no need for stateid or flags. Also we specify NULL 6997 * rp since we're only interested in setting owner_group 6998 * attributes. 6999 */ 7000 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 7001 &e.error, 0); 7002 7003 if (e.error) { 7004 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 7005 nfs_rw_exit(&drp->r_rwlock); 7006 nfs4_fattr4_free(crattr); 7007 nfs4args_verify_free(&argop[8]); 7008 kmem_free(argop, argoplist_size); 7009 return (e.error); 7010 } 7011 } else { 7012 argop[1].argop = OP_SAVEFH; 7013 7014 argop[5].argop = OP_RESTOREFH; 7015 7016 argop[6].argop = OP_GETATTR; 7017 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7018 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7019 } 7020 7021 dnlc_remove(dvp, nm); 7022 7023 doqueue = 1; 7024 t = gethrtime(); 7025 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7026 7027 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7028 if (e.error) { 7029 PURGE_ATTRCACHE4(dvp); 7030 if (!needrecov) 7031 goto out; 7032 } 7033 7034 if (needrecov) { 7035 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 7036 OP_CREATE, NULL, NULL, NULL) == FALSE) { 7037 nfs4_end_op(mi, dvp, NULL, &recov_state, 7038 needrecov); 7039 need_end_op = FALSE; 7040 nfs4_fattr4_free(crattr); 7041 if (setgid_flag) { 7042 nfs4args_verify_free(&argop[8]); 7043 nfs4args_setattr_free(&argop[9]); 7044 } 7045 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 7046 goto recov_retry; 7047 } 7048 } 7049 7050 resp = &res; 7051 7052 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 7053 7054 if (res.status == NFS4ERR_BADOWNER) 7055 nfs4_log_badowner(mi, OP_CREATE); 7056 7057 e.error = geterrno4(res.status); 7058 7059 /* 7060 * This check is left over from when create was implemented 7061 * using a setattr op (instead of createattrs). If the 7062 * putfh/create/getfh failed, the error was returned. If 7063 * setattr/getattr failed, we keep going. 7064 * 7065 * It might be better to get rid of the GETFH also, and just 7066 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 7067 * Then if any of the operations failed, we could return the 7068 * error now, and remove much of the error code below. 7069 */ 7070 if (res.array_len <= idx_fattr) { 7071 /* 7072 * Either Putfh, Create or Getfh failed. 7073 */ 7074 PURGE_ATTRCACHE4(dvp); 7075 /* 7076 * nfs4_purge_stale_fh() may generate otw calls through 7077 * nfs4_invalidate_pages. Hence the need to call 7078 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 7079 */ 7080 nfs4_end_op(mi, dvp, NULL, &recov_state, 7081 needrecov); 7082 need_end_op = FALSE; 7083 nfs4_purge_stale_fh(e.error, dvp, cr); 7084 goto out; 7085 } 7086 } 7087 7088 resop = &res.array[idx_create]; /* create res */ 7089 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 7090 7091 resop = &res.array[idx_create + 1]; /* getfh res */ 7092 gf_res = &resop->nfs_resop4_u.opgetfh; 7093 7094 sfhp = sfh4_get(&gf_res->object, mi); 7095 if (e.error) { 7096 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 7097 fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7098 if (vp->v_type == VNON) { 7099 vattr.va_mask = AT_TYPE; 7100 /* 7101 * Need to call nfs4_end_op before nfs4getattr to avoid 7102 * potential nfs4_start_op deadlock. See RFE 4777612. 7103 */ 7104 nfs4_end_op(mi, dvp, NULL, &recov_state, 7105 needrecov); 7106 need_end_op = FALSE; 7107 e.error = nfs4getattr(vp, &vattr, cr); 7108 if (e.error) { 7109 VN_RELE(vp); 7110 *vpp = NULL; 7111 goto out; 7112 } 7113 vp->v_type = vattr.va_type; 7114 } 7115 e.error = 0; 7116 } else { 7117 *vpp = vp = makenfs4node(sfhp, 7118 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 7119 dvp->v_vfsp, t, cr, 7120 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp)); 7121 } 7122 7123 /* 7124 * If compound succeeded, then update dir attrs 7125 */ 7126 if (res.status == NFS4_OK) { 7127 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 7128 dinfo.di_cred = cr; 7129 dinfo.di_time_call = t; 7130 dinfop = &dinfo; 7131 } else 7132 dinfop = NULL; 7133 7134 /* Update directory cache attribute, readdir and dnlc caches */ 7135 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 7136 7137 out: 7138 if (sfhp != NULL) 7139 sfh4_rele(&sfhp); 7140 nfs_rw_exit(&drp->r_rwlock); 7141 nfs4_fattr4_free(crattr); 7142 if (setgid_flag) { 7143 nfs4args_verify_free(&argop[8]); 7144 nfs4args_setattr_free(&argop[9]); 7145 } 7146 if (resp) 7147 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7148 if (need_end_op) 7149 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7150 7151 kmem_free(argop, argoplist_size); 7152 return (e.error); 7153 } 7154 7155 /* ARGSUSED */ 7156 static int 7157 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7158 int mode, vnode_t **vpp, cred_t *cr) 7159 { 7160 int error; 7161 vnode_t *vp; 7162 nfs_ftype4 type; 7163 specdata4 spec, *specp = NULL; 7164 7165 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7166 7167 switch (va->va_type) { 7168 case VCHR: 7169 case VBLK: 7170 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7171 spec.specdata1 = getmajor(va->va_rdev); 7172 spec.specdata2 = getminor(va->va_rdev); 7173 specp = &spec; 7174 break; 7175 7176 case VFIFO: 7177 type = NF4FIFO; 7178 break; 7179 case VSOCK: 7180 type = NF4SOCK; 7181 break; 7182 7183 default: 7184 return (EINVAL); 7185 } 7186 7187 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7188 if (error) { 7189 return (error); 7190 } 7191 7192 /* 7193 * This might not be needed any more; special case to deal 7194 * with problematic v2/v3 servers. Since create was unable 7195 * to set group correctly, not sure what hope setattr has. 7196 */ 7197 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7198 va->va_mask = AT_GID; 7199 (void) nfs4setattr(vp, va, 0, cr, NULL); 7200 } 7201 7202 /* 7203 * If vnode is a device create special vnode 7204 */ 7205 if (ISVDEV(vp->v_type)) { 7206 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7207 VN_RELE(vp); 7208 } else { 7209 *vpp = vp; 7210 } 7211 return (error); 7212 } 7213 7214 /* 7215 * Remove requires that the current fh be the target directory. 7216 * After the operation, the current fh is unchanged. 7217 * The compound op structure is: 7218 * PUTFH(targetdir), REMOVE 7219 * 7220 * Weirdness: if the vnode to be removed is open 7221 * we rename it instead of removing it and nfs_inactive 7222 * will remove the new name. 7223 */ 7224 /* ARGSUSED */ 7225 static int 7226 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags) 7227 { 7228 COMPOUND4args_clnt args; 7229 COMPOUND4res_clnt res, *resp = NULL; 7230 REMOVE4res *rm_res; 7231 nfs_argop4 argop[3]; 7232 nfs_resop4 *resop; 7233 vnode_t *vp; 7234 char *tmpname; 7235 int doqueue; 7236 mntinfo4_t *mi; 7237 rnode4_t *rp; 7238 rnode4_t *drp; 7239 int needrecov = 0; 7240 nfs4_recov_state_t recov_state; 7241 int isopen; 7242 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7243 dirattr_info_t dinfo; 7244 7245 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7246 return (EPERM); 7247 drp = VTOR4(dvp); 7248 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7249 return (EINTR); 7250 7251 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7252 if (e.error) { 7253 nfs_rw_exit(&drp->r_rwlock); 7254 return (e.error); 7255 } 7256 7257 if (vp->v_type == VDIR) { 7258 VN_RELE(vp); 7259 nfs_rw_exit(&drp->r_rwlock); 7260 return (EISDIR); 7261 } 7262 7263 /* 7264 * First just remove the entry from the name cache, as it 7265 * is most likely the only entry for this vp. 7266 */ 7267 dnlc_remove(dvp, nm); 7268 7269 rp = VTOR4(vp); 7270 7271 /* 7272 * For regular file types, check to see if the file is open by looking 7273 * at the open streams. 7274 * For all other types, check the reference count on the vnode. Since 7275 * they are not opened OTW they never have an open stream. 7276 * 7277 * If the file is open, rename it to .nfsXXXX. 7278 */ 7279 if (vp->v_type != VREG) { 7280 /* 7281 * If the file has a v_count > 1 then there may be more than one 7282 * entry in the name cache due multiple links or an open file, 7283 * but we don't have the real reference count so flush all 7284 * possible entries. 7285 */ 7286 if (vp->v_count > 1) 7287 dnlc_purge_vp(vp); 7288 7289 /* 7290 * Now we have the real reference count. 7291 */ 7292 isopen = vp->v_count > 1; 7293 } else { 7294 mutex_enter(&rp->r_os_lock); 7295 isopen = list_head(&rp->r_open_streams) != NULL; 7296 mutex_exit(&rp->r_os_lock); 7297 } 7298 7299 mutex_enter(&rp->r_statelock); 7300 if (isopen && 7301 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7302 mutex_exit(&rp->r_statelock); 7303 tmpname = newname(); 7304 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct); 7305 if (e.error) 7306 kmem_free(tmpname, MAXNAMELEN); 7307 else { 7308 mutex_enter(&rp->r_statelock); 7309 if (rp->r_unldvp == NULL) { 7310 VN_HOLD(dvp); 7311 rp->r_unldvp = dvp; 7312 if (rp->r_unlcred != NULL) 7313 crfree(rp->r_unlcred); 7314 crhold(cr); 7315 rp->r_unlcred = cr; 7316 rp->r_unlname = tmpname; 7317 } else { 7318 kmem_free(rp->r_unlname, MAXNAMELEN); 7319 rp->r_unlname = tmpname; 7320 } 7321 mutex_exit(&rp->r_statelock); 7322 } 7323 VN_RELE(vp); 7324 nfs_rw_exit(&drp->r_rwlock); 7325 return (e.error); 7326 } 7327 /* 7328 * Actually remove the file/dir 7329 */ 7330 mutex_exit(&rp->r_statelock); 7331 7332 /* 7333 * We need to flush any dirty pages which happen to 7334 * be hanging around before removing the file. 7335 * This shouldn't happen very often since in NFSv4 7336 * we should be close to open consistent. 7337 */ 7338 if (nfs4_has_pages(vp) && 7339 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7340 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct); 7341 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7342 mutex_enter(&rp->r_statelock); 7343 if (!rp->r_error) 7344 rp->r_error = e.error; 7345 mutex_exit(&rp->r_statelock); 7346 } 7347 } 7348 7349 mi = VTOMI4(dvp); 7350 7351 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7352 recov_state.rs_flags = 0; 7353 recov_state.rs_num_retry_despite_err = 0; 7354 7355 recov_retry: 7356 /* 7357 * Remove ops: putfh dir; remove 7358 */ 7359 args.ctag = TAG_REMOVE; 7360 args.array_len = 3; 7361 args.array = argop; 7362 7363 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7364 if (e.error) { 7365 nfs_rw_exit(&drp->r_rwlock); 7366 VN_RELE(vp); 7367 return (e.error); 7368 } 7369 7370 /* putfh directory */ 7371 argop[0].argop = OP_CPUTFH; 7372 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7373 7374 /* remove */ 7375 argop[1].argop = OP_CREMOVE; 7376 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7377 7378 /* getattr dir */ 7379 argop[2].argop = OP_GETATTR; 7380 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7381 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7382 7383 doqueue = 1; 7384 dinfo.di_time_call = gethrtime(); 7385 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7386 7387 PURGE_ATTRCACHE4(vp); 7388 7389 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7390 if (e.error) 7391 PURGE_ATTRCACHE4(dvp); 7392 7393 if (needrecov) { 7394 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7395 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 7396 if (!e.error) 7397 (void) xdr_free(xdr_COMPOUND4res_clnt, 7398 (caddr_t)&res); 7399 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7400 needrecov); 7401 goto recov_retry; 7402 } 7403 } 7404 7405 /* 7406 * Matching nfs4_end_op() for start_op() above. 7407 * There is a path in the code below which calls 7408 * nfs4_purge_stale_fh(), which may generate otw calls through 7409 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7410 * here to avoid nfs4_start_op() deadlock. 7411 */ 7412 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7413 7414 if (!e.error) { 7415 resp = &res; 7416 7417 if (res.status) { 7418 e.error = geterrno4(res.status); 7419 PURGE_ATTRCACHE4(dvp); 7420 nfs4_purge_stale_fh(e.error, dvp, cr); 7421 } else { 7422 resop = &res.array[1]; /* remove res */ 7423 rm_res = &resop->nfs_resop4_u.opremove; 7424 7425 dinfo.di_garp = 7426 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7427 dinfo.di_cred = cr; 7428 7429 /* Update directory attr, readdir and dnlc caches */ 7430 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7431 &dinfo); 7432 } 7433 } 7434 nfs_rw_exit(&drp->r_rwlock); 7435 if (resp) 7436 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7437 7438 if (e.error == 0) { 7439 vnode_t *tvp; 7440 rnode4_t *trp; 7441 trp = VTOR4(vp); 7442 tvp = vp; 7443 if (IS_SHADOW(vp, trp)) 7444 tvp = RTOV4(trp); 7445 vnevent_remove(tvp, dvp, nm, ct); 7446 } 7447 VN_RELE(vp); 7448 return (e.error); 7449 } 7450 7451 /* 7452 * Link requires that the current fh be the target directory and the 7453 * saved fh be the source fh. After the operation, the current fh is unchanged. 7454 * Thus the compound op structure is: 7455 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7456 * GETATTR(file) 7457 */ 7458 /* ARGSUSED */ 7459 static int 7460 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr, 7461 caller_context_t *ct, int flags) 7462 { 7463 COMPOUND4args_clnt args; 7464 COMPOUND4res_clnt res, *resp = NULL; 7465 LINK4res *ln_res; 7466 int argoplist_size = 7 * sizeof (nfs_argop4); 7467 nfs_argop4 *argop; 7468 nfs_resop4 *resop; 7469 vnode_t *realvp, *nvp; 7470 int doqueue; 7471 mntinfo4_t *mi; 7472 rnode4_t *tdrp; 7473 bool_t needrecov = FALSE; 7474 nfs4_recov_state_t recov_state; 7475 hrtime_t t; 7476 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7477 dirattr_info_t dinfo; 7478 7479 ASSERT(*tnm != '\0'); 7480 ASSERT(tdvp->v_type == VDIR); 7481 ASSERT(nfs4_consistent_type(tdvp)); 7482 ASSERT(nfs4_consistent_type(svp)); 7483 7484 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7485 return (EPERM); 7486 if (VOP_REALVP(svp, &realvp, ct) == 0) { 7487 svp = realvp; 7488 ASSERT(nfs4_consistent_type(svp)); 7489 } 7490 7491 tdrp = VTOR4(tdvp); 7492 mi = VTOMI4(svp); 7493 7494 if (!(mi->mi_flags & MI4_LINK)) { 7495 return (EOPNOTSUPP); 7496 } 7497 recov_state.rs_flags = 0; 7498 recov_state.rs_num_retry_despite_err = 0; 7499 7500 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7501 return (EINTR); 7502 7503 recov_retry: 7504 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7505 7506 args.ctag = TAG_LINK; 7507 7508 /* 7509 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7510 * restorefh; getattr(fl) 7511 */ 7512 args.array_len = 7; 7513 args.array = argop; 7514 7515 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7516 if (e.error) { 7517 kmem_free(argop, argoplist_size); 7518 nfs_rw_exit(&tdrp->r_rwlock); 7519 return (e.error); 7520 } 7521 7522 /* 0. putfh file */ 7523 argop[0].argop = OP_CPUTFH; 7524 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7525 7526 /* 1. save current fh to free up the space for the dir */ 7527 argop[1].argop = OP_SAVEFH; 7528 7529 /* 2. putfh targetdir */ 7530 argop[2].argop = OP_CPUTFH; 7531 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7532 7533 /* 3. link: current_fh is targetdir, saved_fh is source */ 7534 argop[3].argop = OP_CLINK; 7535 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7536 7537 /* 4. Get attributes of dir */ 7538 argop[4].argop = OP_GETATTR; 7539 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7540 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7541 7542 /* 5. If link was successful, restore current vp to file */ 7543 argop[5].argop = OP_RESTOREFH; 7544 7545 /* 6. Get attributes of linked object */ 7546 argop[6].argop = OP_GETATTR; 7547 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7548 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7549 7550 dnlc_remove(tdvp, tnm); 7551 7552 doqueue = 1; 7553 t = gethrtime(); 7554 7555 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7556 7557 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7558 if (e.error != 0 && !needrecov) { 7559 PURGE_ATTRCACHE4(tdvp); 7560 PURGE_ATTRCACHE4(svp); 7561 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7562 goto out; 7563 } 7564 7565 if (needrecov) { 7566 bool_t abort; 7567 7568 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7569 NULL, NULL, OP_LINK, NULL, NULL, NULL); 7570 if (abort == FALSE) { 7571 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7572 needrecov); 7573 kmem_free(argop, argoplist_size); 7574 if (!e.error) 7575 (void) xdr_free(xdr_COMPOUND4res_clnt, 7576 (caddr_t)&res); 7577 goto recov_retry; 7578 } else { 7579 if (e.error != 0) { 7580 PURGE_ATTRCACHE4(tdvp); 7581 PURGE_ATTRCACHE4(svp); 7582 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7583 &recov_state, needrecov); 7584 goto out; 7585 } 7586 /* fall through for res.status case */ 7587 } 7588 } 7589 7590 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7591 7592 resp = &res; 7593 if (res.status) { 7594 /* If link succeeded, then don't return error */ 7595 e.error = geterrno4(res.status); 7596 if (res.array_len <= 4) { 7597 /* 7598 * Either Putfh, Savefh, Putfh dir, or Link failed 7599 */ 7600 PURGE_ATTRCACHE4(svp); 7601 PURGE_ATTRCACHE4(tdvp); 7602 if (e.error == EOPNOTSUPP) { 7603 mutex_enter(&mi->mi_lock); 7604 mi->mi_flags &= ~MI4_LINK; 7605 mutex_exit(&mi->mi_lock); 7606 } 7607 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7608 /* XXX-LP */ 7609 if (e.error == EISDIR && crgetuid(cr) != 0) 7610 e.error = EPERM; 7611 goto out; 7612 } 7613 } 7614 7615 /* either no error or one of the postop getattr failed */ 7616 7617 /* 7618 * XXX - if LINK succeeded, but no attrs were returned for link 7619 * file, purge its cache. 7620 * 7621 * XXX Perform a simplified version of wcc checking. Instead of 7622 * have another getattr to get pre-op, just purge cache if 7623 * any of the ops prior to and including the getattr failed. 7624 * If the getattr succeeded then update the attrcache accordingly. 7625 */ 7626 7627 /* 7628 * update cache with link file postattrs. 7629 * Note: at this point resop points to link res. 7630 */ 7631 resop = &res.array[3]; /* link res */ 7632 ln_res = &resop->nfs_resop4_u.oplink; 7633 if (res.status == NFS4_OK) 7634 e.error = nfs4_update_attrcache(res.status, 7635 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7636 t, svp, cr); 7637 7638 /* 7639 * Call makenfs4node to create the new shadow vp for tnm. 7640 * We pass NULL attrs because we just cached attrs for 7641 * the src object. All we're trying to accomplish is to 7642 * to create the new shadow vnode. 7643 */ 7644 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7645 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh)); 7646 7647 /* Update target cache attribute, readdir and dnlc caches */ 7648 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7649 dinfo.di_time_call = t; 7650 dinfo.di_cred = cr; 7651 7652 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7653 ASSERT(nfs4_consistent_type(tdvp)); 7654 ASSERT(nfs4_consistent_type(svp)); 7655 ASSERT(nfs4_consistent_type(nvp)); 7656 VN_RELE(nvp); 7657 7658 if (!e.error) { 7659 vnode_t *tvp; 7660 rnode4_t *trp; 7661 /* 7662 * Notify the source file of this link operation. 7663 */ 7664 trp = VTOR4(svp); 7665 tvp = svp; 7666 if (IS_SHADOW(svp, trp)) 7667 tvp = RTOV4(trp); 7668 vnevent_link(tvp, ct); 7669 } 7670 out: 7671 kmem_free(argop, argoplist_size); 7672 if (resp) 7673 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7674 7675 nfs_rw_exit(&tdrp->r_rwlock); 7676 7677 return (e.error); 7678 } 7679 7680 /* ARGSUSED */ 7681 static int 7682 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7683 caller_context_t *ct, int flags) 7684 { 7685 vnode_t *realvp; 7686 7687 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7688 return (EPERM); 7689 if (VOP_REALVP(ndvp, &realvp, ct) == 0) 7690 ndvp = realvp; 7691 7692 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct)); 7693 } 7694 7695 /* 7696 * nfs4rename does the real work of renaming in NFS Version 4. 7697 * 7698 * A file handle is considered volatile for renaming purposes if either 7699 * of the volatile bits are turned on. However, the compound may differ 7700 * based on the likelihood of the filehandle to change during rename. 7701 */ 7702 static int 7703 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr, 7704 caller_context_t *ct) 7705 { 7706 int error; 7707 mntinfo4_t *mi; 7708 vnode_t *nvp = NULL; 7709 vnode_t *ovp = NULL; 7710 char *tmpname = NULL; 7711 rnode4_t *rp; 7712 rnode4_t *odrp; 7713 rnode4_t *ndrp; 7714 int did_link = 0; 7715 int do_link = 1; 7716 nfsstat4 stat = NFS4_OK; 7717 7718 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7719 ASSERT(nfs4_consistent_type(odvp)); 7720 ASSERT(nfs4_consistent_type(ndvp)); 7721 7722 if (onm[0] == '.' && (onm[1] == '\0' || 7723 (onm[1] == '.' && onm[2] == '\0'))) 7724 return (EINVAL); 7725 7726 if (nnm[0] == '.' && (nnm[1] == '\0' || 7727 (nnm[1] == '.' && nnm[2] == '\0'))) 7728 return (EINVAL); 7729 7730 odrp = VTOR4(odvp); 7731 ndrp = VTOR4(ndvp); 7732 if ((intptr_t)odrp < (intptr_t)ndrp) { 7733 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7734 return (EINTR); 7735 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7736 nfs_rw_exit(&odrp->r_rwlock); 7737 return (EINTR); 7738 } 7739 } else { 7740 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7741 return (EINTR); 7742 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7743 nfs_rw_exit(&ndrp->r_rwlock); 7744 return (EINTR); 7745 } 7746 } 7747 7748 /* 7749 * Lookup the target file. If it exists, it needs to be 7750 * checked to see whether it is a mount point and whether 7751 * it is active (open). 7752 */ 7753 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7754 if (!error) { 7755 int isactive; 7756 7757 ASSERT(nfs4_consistent_type(nvp)); 7758 /* 7759 * If this file has been mounted on, then just 7760 * return busy because renaming to it would remove 7761 * the mounted file system from the name space. 7762 */ 7763 if (vn_ismntpt(nvp)) { 7764 VN_RELE(nvp); 7765 nfs_rw_exit(&odrp->r_rwlock); 7766 nfs_rw_exit(&ndrp->r_rwlock); 7767 return (EBUSY); 7768 } 7769 7770 /* 7771 * First just remove the entry from the name cache, as it 7772 * is most likely the only entry for this vp. 7773 */ 7774 dnlc_remove(ndvp, nnm); 7775 7776 rp = VTOR4(nvp); 7777 7778 if (nvp->v_type != VREG) { 7779 /* 7780 * Purge the name cache of all references to this vnode 7781 * so that we can check the reference count to infer 7782 * whether it is active or not. 7783 */ 7784 if (nvp->v_count > 1) 7785 dnlc_purge_vp(nvp); 7786 7787 isactive = nvp->v_count > 1; 7788 } else { 7789 mutex_enter(&rp->r_os_lock); 7790 isactive = list_head(&rp->r_open_streams) != NULL; 7791 mutex_exit(&rp->r_os_lock); 7792 } 7793 7794 /* 7795 * If the vnode is active and is not a directory, 7796 * arrange to rename it to a 7797 * temporary file so that it will continue to be 7798 * accessible. This implements the "unlink-open-file" 7799 * semantics for the target of a rename operation. 7800 * Before doing this though, make sure that the 7801 * source and target files are not already the same. 7802 */ 7803 if (isactive && nvp->v_type != VDIR) { 7804 /* 7805 * Lookup the source name. 7806 */ 7807 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7808 7809 /* 7810 * The source name *should* already exist. 7811 */ 7812 if (error) { 7813 VN_RELE(nvp); 7814 nfs_rw_exit(&odrp->r_rwlock); 7815 nfs_rw_exit(&ndrp->r_rwlock); 7816 return (error); 7817 } 7818 7819 ASSERT(nfs4_consistent_type(ovp)); 7820 7821 /* 7822 * Compare the two vnodes. If they are the same, 7823 * just release all held vnodes and return success. 7824 */ 7825 if (VN_CMP(ovp, nvp)) { 7826 VN_RELE(ovp); 7827 VN_RELE(nvp); 7828 nfs_rw_exit(&odrp->r_rwlock); 7829 nfs_rw_exit(&ndrp->r_rwlock); 7830 return (0); 7831 } 7832 7833 /* 7834 * Can't mix and match directories and non- 7835 * directories in rename operations. We already 7836 * know that the target is not a directory. If 7837 * the source is a directory, return an error. 7838 */ 7839 if (ovp->v_type == VDIR) { 7840 VN_RELE(ovp); 7841 VN_RELE(nvp); 7842 nfs_rw_exit(&odrp->r_rwlock); 7843 nfs_rw_exit(&ndrp->r_rwlock); 7844 return (ENOTDIR); 7845 } 7846 link_call: 7847 /* 7848 * The target file exists, is not the same as 7849 * the source file, and is active. We first 7850 * try to Link it to a temporary filename to 7851 * avoid having the server removing the file 7852 * completely (which could cause data loss to 7853 * the user's POV in the event the Rename fails 7854 * -- see bug 1165874). 7855 */ 7856 /* 7857 * The do_link and did_link booleans are 7858 * introduced in the event we get NFS4ERR_FILE_OPEN 7859 * returned for the Rename. Some servers can 7860 * not Rename over an Open file, so they return 7861 * this error. The client needs to Remove the 7862 * newly created Link and do two Renames, just 7863 * as if the server didn't support LINK. 7864 */ 7865 tmpname = newname(); 7866 error = 0; 7867 7868 if (do_link) { 7869 error = nfs4_link(ndvp, nvp, tmpname, cr, 7870 NULL, 0); 7871 } 7872 if (error == EOPNOTSUPP || !do_link) { 7873 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7874 cr, NULL, 0); 7875 did_link = 0; 7876 } else { 7877 did_link = 1; 7878 } 7879 if (error) { 7880 kmem_free(tmpname, MAXNAMELEN); 7881 VN_RELE(ovp); 7882 VN_RELE(nvp); 7883 nfs_rw_exit(&odrp->r_rwlock); 7884 nfs_rw_exit(&ndrp->r_rwlock); 7885 return (error); 7886 } 7887 7888 mutex_enter(&rp->r_statelock); 7889 if (rp->r_unldvp == NULL) { 7890 VN_HOLD(ndvp); 7891 rp->r_unldvp = ndvp; 7892 if (rp->r_unlcred != NULL) 7893 crfree(rp->r_unlcred); 7894 crhold(cr); 7895 rp->r_unlcred = cr; 7896 rp->r_unlname = tmpname; 7897 } else { 7898 if (rp->r_unlname) 7899 kmem_free(rp->r_unlname, MAXNAMELEN); 7900 rp->r_unlname = tmpname; 7901 } 7902 mutex_exit(&rp->r_statelock); 7903 } 7904 7905 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7906 7907 ASSERT(nfs4_consistent_type(nvp)); 7908 } 7909 7910 if (ovp == NULL) { 7911 /* 7912 * When renaming directories to be a subdirectory of a 7913 * different parent, the dnlc entry for ".." will no 7914 * longer be valid, so it must be removed. 7915 * 7916 * We do a lookup here to determine whether we are renaming 7917 * a directory and we need to check if we are renaming 7918 * an unlinked file. This might have already been done 7919 * in previous code, so we check ovp == NULL to avoid 7920 * doing it twice. 7921 */ 7922 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7923 /* 7924 * The source name *should* already exist. 7925 */ 7926 if (error) { 7927 nfs_rw_exit(&odrp->r_rwlock); 7928 nfs_rw_exit(&ndrp->r_rwlock); 7929 if (nvp) { 7930 VN_RELE(nvp); 7931 } 7932 return (error); 7933 } 7934 ASSERT(ovp != NULL); 7935 ASSERT(nfs4_consistent_type(ovp)); 7936 } 7937 7938 /* 7939 * Is the object being renamed a dir, and if so, is 7940 * it being renamed to a child of itself? The underlying 7941 * fs should ultimately return EINVAL for this case; 7942 * however, buggy beta non-Solaris NFSv4 servers at 7943 * interop testing events have allowed this behavior, 7944 * and it caused our client to panic due to a recursive 7945 * mutex_enter in fn_move. 7946 * 7947 * The tedious locking in fn_move could be changed to 7948 * deal with this case, and the client could avoid the 7949 * panic; however, the client would just confuse itself 7950 * later and misbehave. A better way to handle the broken 7951 * server is to detect this condition and return EINVAL 7952 * without ever sending the the bogus rename to the server. 7953 * We know the rename is invalid -- just fail it now. 7954 */ 7955 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7956 VN_RELE(ovp); 7957 nfs_rw_exit(&odrp->r_rwlock); 7958 nfs_rw_exit(&ndrp->r_rwlock); 7959 if (nvp) { 7960 VN_RELE(nvp); 7961 } 7962 return (EINVAL); 7963 } 7964 7965 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7966 7967 /* 7968 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7969 * possible for the filehandle to change due to the rename. 7970 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7971 * the fh will not change because of the rename, but we still need 7972 * to update its rnode entry with the new name for 7973 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7974 * has no effect on these for now, but for future improvements, 7975 * we might want to use it too to simplify handling of files 7976 * that are open with that flag on. (XXX) 7977 */ 7978 mi = VTOMI4(odvp); 7979 if (NFS4_VOLATILE_FH(mi)) 7980 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7981 &stat); 7982 else 7983 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7984 &stat); 7985 7986 ASSERT(nfs4_consistent_type(odvp)); 7987 ASSERT(nfs4_consistent_type(ndvp)); 7988 ASSERT(nfs4_consistent_type(ovp)); 7989 7990 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7991 do_link = 0; 7992 /* 7993 * Before the 'link_call' code, we did a nfs4_lookup 7994 * that puts a VN_HOLD on nvp. After the nfs4_link 7995 * call we call VN_RELE to match that hold. We need 7996 * to place an additional VN_HOLD here since we will 7997 * be hitting that VN_RELE again. 7998 */ 7999 VN_HOLD(nvp); 8000 8001 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0); 8002 8003 /* Undo the unlinked file naming stuff we just did */ 8004 mutex_enter(&rp->r_statelock); 8005 if (rp->r_unldvp) { 8006 VN_RELE(ndvp); 8007 rp->r_unldvp = NULL; 8008 if (rp->r_unlcred != NULL) 8009 crfree(rp->r_unlcred); 8010 rp->r_unlcred = NULL; 8011 /* rp->r_unlanme points to tmpname */ 8012 if (rp->r_unlname) 8013 kmem_free(rp->r_unlname, MAXNAMELEN); 8014 rp->r_unlname = NULL; 8015 } 8016 mutex_exit(&rp->r_statelock); 8017 8018 if (nvp) { 8019 VN_RELE(nvp); 8020 } 8021 goto link_call; 8022 } 8023 8024 if (error) { 8025 VN_RELE(ovp); 8026 nfs_rw_exit(&odrp->r_rwlock); 8027 nfs_rw_exit(&ndrp->r_rwlock); 8028 if (nvp) { 8029 VN_RELE(nvp); 8030 } 8031 return (error); 8032 } 8033 8034 /* 8035 * when renaming directories to be a subdirectory of a 8036 * different parent, the dnlc entry for ".." will no 8037 * longer be valid, so it must be removed 8038 */ 8039 rp = VTOR4(ovp); 8040 if (ndvp != odvp) { 8041 if (ovp->v_type == VDIR) { 8042 dnlc_remove(ovp, ".."); 8043 if (rp->r_dir != NULL) 8044 nfs4_purge_rddir_cache(ovp); 8045 } 8046 } 8047 8048 /* 8049 * If we are renaming the unlinked file, update the 8050 * r_unldvp and r_unlname as needed. 8051 */ 8052 mutex_enter(&rp->r_statelock); 8053 if (rp->r_unldvp != NULL) { 8054 if (strcmp(rp->r_unlname, onm) == 0) { 8055 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 8056 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 8057 if (ndvp != rp->r_unldvp) { 8058 VN_RELE(rp->r_unldvp); 8059 rp->r_unldvp = ndvp; 8060 VN_HOLD(ndvp); 8061 } 8062 } 8063 } 8064 mutex_exit(&rp->r_statelock); 8065 8066 /* 8067 * Notify the rename vnevents to source vnode, and to the target 8068 * vnode if it already existed. 8069 */ 8070 if (error == 0) { 8071 vnode_t *tvp; 8072 rnode4_t *trp; 8073 /* 8074 * Notify the vnode. Each links is represented by 8075 * a different vnode, in nfsv4. 8076 */ 8077 if (nvp) { 8078 trp = VTOR4(nvp); 8079 tvp = nvp; 8080 if (IS_SHADOW(nvp, trp)) 8081 tvp = RTOV4(trp); 8082 vnevent_rename_dest(tvp, ndvp, nnm, ct); 8083 } 8084 8085 /* 8086 * if the source and destination directory are not the 8087 * same notify the destination directory. 8088 */ 8089 if (VTOR4(odvp) != VTOR4(ndvp)) { 8090 trp = VTOR4(ndvp); 8091 tvp = ndvp; 8092 if (IS_SHADOW(ndvp, trp)) 8093 tvp = RTOV4(trp); 8094 vnevent_rename_dest_dir(tvp, ct); 8095 } 8096 8097 trp = VTOR4(ovp); 8098 tvp = ovp; 8099 if (IS_SHADOW(ovp, trp)) 8100 tvp = RTOV4(trp); 8101 vnevent_rename_src(tvp, odvp, onm, ct); 8102 } 8103 8104 if (nvp) { 8105 VN_RELE(nvp); 8106 } 8107 VN_RELE(ovp); 8108 8109 nfs_rw_exit(&odrp->r_rwlock); 8110 nfs_rw_exit(&ndrp->r_rwlock); 8111 8112 return (error); 8113 } 8114 8115 /* 8116 * When the parent directory has changed, sv_dfh must be updated 8117 */ 8118 static void 8119 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp) 8120 { 8121 svnode_t *sv = VTOSV(vp); 8122 nfs4_sharedfh_t *old_dfh = sv->sv_dfh; 8123 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh; 8124 8125 sfh4_hold(new_dfh); 8126 sv->sv_dfh = new_dfh; 8127 sfh4_rele(&old_dfh); 8128 } 8129 8130 /* 8131 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 8132 * when it is known that the filehandle is persistent through rename. 8133 * 8134 * Rename requires that the current fh be the target directory and the 8135 * saved fh be the source directory. After the operation, the current fh 8136 * is unchanged. 8137 * The compound op structure for persistent fh rename is: 8138 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 8139 * Rather than bother with the directory postop args, we'll simply 8140 * update that a change occurred in the cache, so no post-op getattrs. 8141 */ 8142 static int 8143 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 8144 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8145 { 8146 COMPOUND4args_clnt args; 8147 COMPOUND4res_clnt res, *resp = NULL; 8148 nfs_argop4 *argop; 8149 nfs_resop4 *resop; 8150 int doqueue, argoplist_size; 8151 mntinfo4_t *mi; 8152 rnode4_t *odrp = VTOR4(odvp); 8153 rnode4_t *ndrp = VTOR4(ndvp); 8154 RENAME4res *rn_res; 8155 bool_t needrecov; 8156 nfs4_recov_state_t recov_state; 8157 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8158 dirattr_info_t dinfo, *dinfop; 8159 8160 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8161 8162 recov_state.rs_flags = 0; 8163 recov_state.rs_num_retry_despite_err = 0; 8164 8165 /* 8166 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 8167 * 8168 * If source/target are different dirs, then append putfh(src); getattr 8169 */ 8170 args.array_len = (odvp == ndvp) ? 5 : 7; 8171 argoplist_size = args.array_len * sizeof (nfs_argop4); 8172 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8173 8174 recov_retry: 8175 *statp = NFS4_OK; 8176 8177 /* No need to Lookup the file, persistent fh */ 8178 args.ctag = TAG_RENAME; 8179 8180 mi = VTOMI4(odvp); 8181 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8182 if (e.error) { 8183 kmem_free(argop, argoplist_size); 8184 return (e.error); 8185 } 8186 8187 /* 0: putfh source directory */ 8188 argop[0].argop = OP_CPUTFH; 8189 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8190 8191 /* 1: Save source fh to free up current for target */ 8192 argop[1].argop = OP_SAVEFH; 8193 8194 /* 2: putfh targetdir */ 8195 argop[2].argop = OP_CPUTFH; 8196 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8197 8198 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8199 argop[3].argop = OP_CRENAME; 8200 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8201 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8202 8203 /* 4: getattr (targetdir) */ 8204 argop[4].argop = OP_GETATTR; 8205 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8206 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8207 8208 if (ndvp != odvp) { 8209 8210 /* 5: putfh (sourcedir) */ 8211 argop[5].argop = OP_CPUTFH; 8212 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8213 8214 /* 6: getattr (sourcedir) */ 8215 argop[6].argop = OP_GETATTR; 8216 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8217 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8218 } 8219 8220 dnlc_remove(odvp, onm); 8221 dnlc_remove(ndvp, nnm); 8222 8223 doqueue = 1; 8224 dinfo.di_time_call = gethrtime(); 8225 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8226 8227 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8228 if (e.error) { 8229 PURGE_ATTRCACHE4(odvp); 8230 PURGE_ATTRCACHE4(ndvp); 8231 } else { 8232 *statp = res.status; 8233 } 8234 8235 if (needrecov) { 8236 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8237 OP_RENAME, NULL, NULL, NULL) == FALSE) { 8238 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8239 if (!e.error) 8240 (void) xdr_free(xdr_COMPOUND4res_clnt, 8241 (caddr_t)&res); 8242 goto recov_retry; 8243 } 8244 } 8245 8246 if (!e.error) { 8247 resp = &res; 8248 /* 8249 * as long as OP_RENAME 8250 */ 8251 if (res.status != NFS4_OK && res.array_len <= 4) { 8252 e.error = geterrno4(res.status); 8253 PURGE_ATTRCACHE4(odvp); 8254 PURGE_ATTRCACHE4(ndvp); 8255 /* 8256 * System V defines rename to return EEXIST, not 8257 * ENOTEMPTY if the target directory is not empty. 8258 * Over the wire, the error is NFSERR_ENOTEMPTY 8259 * which geterrno4 maps to ENOTEMPTY. 8260 */ 8261 if (e.error == ENOTEMPTY) 8262 e.error = EEXIST; 8263 } else { 8264 8265 resop = &res.array[3]; /* rename res */ 8266 rn_res = &resop->nfs_resop4_u.oprename; 8267 8268 if (res.status == NFS4_OK) { 8269 /* 8270 * Update target attribute, readdir and dnlc 8271 * caches. 8272 */ 8273 dinfo.di_garp = 8274 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8275 dinfo.di_cred = cr; 8276 dinfop = &dinfo; 8277 } else 8278 dinfop = NULL; 8279 8280 nfs4_update_dircaches(&rn_res->target_cinfo, 8281 ndvp, NULL, NULL, dinfop); 8282 8283 /* 8284 * Update source attribute, readdir and dnlc caches 8285 * 8286 */ 8287 if (ndvp != odvp) { 8288 update_parentdir_sfh(renvp, ndvp); 8289 8290 if (dinfop) 8291 dinfo.di_garp = 8292 &(res.array[6].nfs_resop4_u. 8293 opgetattr.ga_res); 8294 8295 nfs4_update_dircaches(&rn_res->source_cinfo, 8296 odvp, NULL, NULL, dinfop); 8297 } 8298 8299 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8300 nnm); 8301 } 8302 } 8303 8304 if (resp) 8305 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8306 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8307 kmem_free(argop, argoplist_size); 8308 8309 return (e.error); 8310 } 8311 8312 /* 8313 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8314 * it is possible for the filehandle to change due to the rename. 8315 * 8316 * The compound req in this case includes a post-rename lookup and getattr 8317 * to ensure that we have the correct fh and attributes for the object. 8318 * 8319 * Rename requires that the current fh be the target directory and the 8320 * saved fh be the source directory. After the operation, the current fh 8321 * is unchanged. 8322 * 8323 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8324 * update the filehandle for the renamed object. We also get the old 8325 * filehandle for historical reasons; this should be taken out sometime. 8326 * This results in a rather cumbersome compound... 8327 * 8328 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8329 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8330 * 8331 */ 8332 static int 8333 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8334 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8335 { 8336 COMPOUND4args_clnt args; 8337 COMPOUND4res_clnt res, *resp = NULL; 8338 int argoplist_size; 8339 nfs_argop4 *argop; 8340 nfs_resop4 *resop; 8341 int doqueue; 8342 mntinfo4_t *mi; 8343 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8344 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8345 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8346 RENAME4res *rn_res; 8347 GETFH4res *ngf_res; 8348 bool_t needrecov; 8349 nfs4_recov_state_t recov_state; 8350 hrtime_t t; 8351 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8352 dirattr_info_t dinfo, *dinfop = &dinfo; 8353 8354 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8355 8356 recov_state.rs_flags = 0; 8357 recov_state.rs_num_retry_despite_err = 0; 8358 8359 recov_retry: 8360 *statp = NFS4_OK; 8361 8362 /* 8363 * There is a window between the RPC and updating the path and 8364 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8365 * code, so that it doesn't try to use the old path during that 8366 * window. 8367 */ 8368 mutex_enter(&orp->r_statelock); 8369 while (orp->r_flags & R4RECEXPFH) { 8370 klwp_t *lwp = ttolwp(curthread); 8371 8372 if (lwp != NULL) 8373 lwp->lwp_nostop++; 8374 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8375 mutex_exit(&orp->r_statelock); 8376 if (lwp != NULL) 8377 lwp->lwp_nostop--; 8378 return (EINTR); 8379 } 8380 if (lwp != NULL) 8381 lwp->lwp_nostop--; 8382 } 8383 orp->r_flags |= R4RECEXPFH; 8384 mutex_exit(&orp->r_statelock); 8385 8386 mi = VTOMI4(odvp); 8387 8388 args.ctag = TAG_RENAME_VFH; 8389 args.array_len = (odvp == ndvp) ? 10 : 12; 8390 argoplist_size = args.array_len * sizeof (nfs_argop4); 8391 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8392 8393 /* 8394 * Rename ops: 8395 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8396 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8397 * LOOKUP(trgt), GETFH(new), GETATTR, 8398 * 8399 * if (odvp != ndvp) 8400 * add putfh(sourcedir), getattr(sourcedir) } 8401 */ 8402 args.array = argop; 8403 8404 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8405 &recov_state, NULL); 8406 if (e.error) { 8407 kmem_free(argop, argoplist_size); 8408 mutex_enter(&orp->r_statelock); 8409 orp->r_flags &= ~R4RECEXPFH; 8410 cv_broadcast(&orp->r_cv); 8411 mutex_exit(&orp->r_statelock); 8412 return (e.error); 8413 } 8414 8415 /* 0: putfh source directory */ 8416 argop[0].argop = OP_CPUTFH; 8417 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8418 8419 /* 1: Save source fh to free up current for target */ 8420 argop[1].argop = OP_SAVEFH; 8421 8422 /* 2: Lookup pre-rename fh of renamed object */ 8423 argop[2].argop = OP_CLOOKUP; 8424 argop[2].nfs_argop4_u.opclookup.cname = onm; 8425 8426 /* 3: getfh fh of renamed object (before rename) */ 8427 argop[3].argop = OP_GETFH; 8428 8429 /* 4: putfh targetdir */ 8430 argop[4].argop = OP_CPUTFH; 8431 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8432 8433 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8434 argop[5].argop = OP_CRENAME; 8435 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8436 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8437 8438 /* 6: getattr of target dir (post op attrs) */ 8439 argop[6].argop = OP_GETATTR; 8440 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8441 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8442 8443 /* 7: Lookup post-rename fh of renamed object */ 8444 argop[7].argop = OP_CLOOKUP; 8445 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8446 8447 /* 8: getfh fh of renamed object (after rename) */ 8448 argop[8].argop = OP_GETFH; 8449 8450 /* 9: getattr of renamed object */ 8451 argop[9].argop = OP_GETATTR; 8452 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8453 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8454 8455 /* 8456 * If source/target dirs are different, then get new post-op 8457 * attrs for source dir also. 8458 */ 8459 if (ndvp != odvp) { 8460 /* 10: putfh (sourcedir) */ 8461 argop[10].argop = OP_CPUTFH; 8462 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8463 8464 /* 11: getattr (sourcedir) */ 8465 argop[11].argop = OP_GETATTR; 8466 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8467 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8468 } 8469 8470 dnlc_remove(odvp, onm); 8471 dnlc_remove(ndvp, nnm); 8472 8473 doqueue = 1; 8474 t = gethrtime(); 8475 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8476 8477 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8478 if (e.error) { 8479 PURGE_ATTRCACHE4(odvp); 8480 PURGE_ATTRCACHE4(ndvp); 8481 if (!needrecov) { 8482 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8483 &recov_state, needrecov); 8484 goto out; 8485 } 8486 } else { 8487 *statp = res.status; 8488 } 8489 8490 if (needrecov) { 8491 bool_t abort; 8492 8493 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8494 OP_RENAME, NULL, NULL, NULL); 8495 if (abort == FALSE) { 8496 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8497 &recov_state, needrecov); 8498 kmem_free(argop, argoplist_size); 8499 if (!e.error) 8500 (void) xdr_free(xdr_COMPOUND4res_clnt, 8501 (caddr_t)&res); 8502 mutex_enter(&orp->r_statelock); 8503 orp->r_flags &= ~R4RECEXPFH; 8504 cv_broadcast(&orp->r_cv); 8505 mutex_exit(&orp->r_statelock); 8506 goto recov_retry; 8507 } else { 8508 if (e.error != 0) { 8509 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8510 &recov_state, needrecov); 8511 goto out; 8512 } 8513 /* fall through for res.status case */ 8514 } 8515 } 8516 8517 resp = &res; 8518 /* 8519 * If OP_RENAME (or any prev op) failed, then return an error. 8520 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8521 */ 8522 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8523 /* 8524 * Error in an op other than last Getattr 8525 */ 8526 e.error = geterrno4(res.status); 8527 PURGE_ATTRCACHE4(odvp); 8528 PURGE_ATTRCACHE4(ndvp); 8529 /* 8530 * System V defines rename to return EEXIST, not 8531 * ENOTEMPTY if the target directory is not empty. 8532 * Over the wire, the error is NFSERR_ENOTEMPTY 8533 * which geterrno4 maps to ENOTEMPTY. 8534 */ 8535 if (e.error == ENOTEMPTY) 8536 e.error = EEXIST; 8537 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8538 needrecov); 8539 goto out; 8540 } 8541 8542 /* rename results */ 8543 rn_res = &res.array[5].nfs_resop4_u.oprename; 8544 8545 if (res.status == NFS4_OK) { 8546 /* Update target attribute, readdir and dnlc caches */ 8547 dinfo.di_garp = 8548 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8549 dinfo.di_cred = cr; 8550 dinfo.di_time_call = t; 8551 } else 8552 dinfop = NULL; 8553 8554 /* Update source cache attribute, readdir and dnlc caches */ 8555 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8556 8557 /* Update source cache attribute, readdir and dnlc caches */ 8558 if (ndvp != odvp) { 8559 update_parentdir_sfh(ovp, ndvp); 8560 8561 /* 8562 * If dinfop is non-NULL, then compound succeded, so 8563 * set di_garp to attrs for source dir. dinfop is only 8564 * set to NULL when compound fails. 8565 */ 8566 if (dinfop) 8567 dinfo.di_garp = 8568 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8569 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8570 dinfop); 8571 } 8572 8573 /* 8574 * Update the rnode with the new component name and args, 8575 * and if the file handle changed, also update it with the new fh. 8576 * This is only necessary if the target object has an rnode 8577 * entry and there is no need to create one for it. 8578 */ 8579 resop = &res.array[8]; /* getfh new res */ 8580 ngf_res = &resop->nfs_resop4_u.opgetfh; 8581 8582 /* 8583 * Update the path and filehandle for the renamed object. 8584 */ 8585 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8586 8587 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8588 8589 if (res.status == NFS4_OK) { 8590 resop++; /* getattr res */ 8591 e.error = nfs4_update_attrcache(res.status, 8592 &resop->nfs_resop4_u.opgetattr.ga_res, 8593 t, ovp, cr); 8594 } 8595 8596 out: 8597 kmem_free(argop, argoplist_size); 8598 if (resp) 8599 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8600 mutex_enter(&orp->r_statelock); 8601 orp->r_flags &= ~R4RECEXPFH; 8602 cv_broadcast(&orp->r_cv); 8603 mutex_exit(&orp->r_statelock); 8604 8605 return (e.error); 8606 } 8607 8608 /* ARGSUSED */ 8609 static int 8610 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr, 8611 caller_context_t *ct, int flags, vsecattr_t *vsecp) 8612 { 8613 int error; 8614 vnode_t *vp; 8615 8616 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8617 return (EPERM); 8618 /* 8619 * As ".." has special meaning and rather than send a mkdir 8620 * over the wire to just let the server freak out, we just 8621 * short circuit it here and return EEXIST 8622 */ 8623 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8624 return (EEXIST); 8625 8626 /* 8627 * Decision to get the right gid and setgid bit of the 8628 * new directory is now made in call_nfs4_create_req. 8629 */ 8630 va->va_mask |= AT_MODE; 8631 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8632 if (error) 8633 return (error); 8634 8635 *vpp = vp; 8636 return (0); 8637 } 8638 8639 8640 /* 8641 * rmdir is using the same remove v4 op as does remove. 8642 * Remove requires that the current fh be the target directory. 8643 * After the operation, the current fh is unchanged. 8644 * The compound op structure is: 8645 * PUTFH(targetdir), REMOVE 8646 */ 8647 /*ARGSUSED4*/ 8648 static int 8649 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr, 8650 caller_context_t *ct, int flags) 8651 { 8652 int need_end_op = FALSE; 8653 COMPOUND4args_clnt args; 8654 COMPOUND4res_clnt res, *resp = NULL; 8655 REMOVE4res *rm_res; 8656 nfs_argop4 argop[3]; 8657 nfs_resop4 *resop; 8658 vnode_t *vp; 8659 int doqueue; 8660 mntinfo4_t *mi; 8661 rnode4_t *drp; 8662 bool_t needrecov = FALSE; 8663 nfs4_recov_state_t recov_state; 8664 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8665 dirattr_info_t dinfo, *dinfop; 8666 8667 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8668 return (EPERM); 8669 /* 8670 * As ".." has special meaning and rather than send a rmdir 8671 * over the wire to just let the server freak out, we just 8672 * short circuit it here and return EEXIST 8673 */ 8674 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8675 return (EEXIST); 8676 8677 drp = VTOR4(dvp); 8678 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8679 return (EINTR); 8680 8681 /* 8682 * Attempt to prevent a rmdir(".") from succeeding. 8683 */ 8684 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8685 if (e.error) { 8686 nfs_rw_exit(&drp->r_rwlock); 8687 return (e.error); 8688 } 8689 if (vp == cdir) { 8690 VN_RELE(vp); 8691 nfs_rw_exit(&drp->r_rwlock); 8692 return (EINVAL); 8693 } 8694 8695 /* 8696 * Since nfsv4 remove op works on both files and directories, 8697 * check that the removed object is indeed a directory. 8698 */ 8699 if (vp->v_type != VDIR) { 8700 VN_RELE(vp); 8701 nfs_rw_exit(&drp->r_rwlock); 8702 return (ENOTDIR); 8703 } 8704 8705 /* 8706 * First just remove the entry from the name cache, as it 8707 * is most likely an entry for this vp. 8708 */ 8709 dnlc_remove(dvp, nm); 8710 8711 /* 8712 * If there vnode reference count is greater than one, then 8713 * there may be additional references in the DNLC which will 8714 * need to be purged. First, trying removing the entry for 8715 * the parent directory and see if that removes the additional 8716 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8717 * to completely remove any references to the directory which 8718 * might still exist in the DNLC. 8719 */ 8720 if (vp->v_count > 1) { 8721 dnlc_remove(vp, ".."); 8722 if (vp->v_count > 1) 8723 dnlc_purge_vp(vp); 8724 } 8725 8726 mi = VTOMI4(dvp); 8727 recov_state.rs_flags = 0; 8728 recov_state.rs_num_retry_despite_err = 0; 8729 8730 recov_retry: 8731 args.ctag = TAG_RMDIR; 8732 8733 /* 8734 * Rmdir ops: putfh dir; remove 8735 */ 8736 args.array_len = 3; 8737 args.array = argop; 8738 8739 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8740 if (e.error) { 8741 nfs_rw_exit(&drp->r_rwlock); 8742 return (e.error); 8743 } 8744 need_end_op = TRUE; 8745 8746 /* putfh directory */ 8747 argop[0].argop = OP_CPUTFH; 8748 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8749 8750 /* remove */ 8751 argop[1].argop = OP_CREMOVE; 8752 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8753 8754 /* getattr (postop attrs for dir that contained removed dir) */ 8755 argop[2].argop = OP_GETATTR; 8756 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8757 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8758 8759 dinfo.di_time_call = gethrtime(); 8760 doqueue = 1; 8761 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8762 8763 PURGE_ATTRCACHE4(vp); 8764 8765 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8766 if (e.error) { 8767 PURGE_ATTRCACHE4(dvp); 8768 } 8769 8770 if (needrecov) { 8771 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8772 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) { 8773 if (!e.error) 8774 (void) xdr_free(xdr_COMPOUND4res_clnt, 8775 (caddr_t)&res); 8776 8777 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8778 needrecov); 8779 need_end_op = FALSE; 8780 goto recov_retry; 8781 } 8782 } 8783 8784 if (!e.error) { 8785 resp = &res; 8786 8787 /* 8788 * Only return error if first 2 ops (OP_REMOVE or earlier) 8789 * failed. 8790 */ 8791 if (res.status != NFS4_OK && res.array_len <= 2) { 8792 e.error = geterrno4(res.status); 8793 PURGE_ATTRCACHE4(dvp); 8794 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8795 &recov_state, needrecov); 8796 need_end_op = FALSE; 8797 nfs4_purge_stale_fh(e.error, dvp, cr); 8798 /* 8799 * System V defines rmdir to return EEXIST, not 8800 * ENOTEMPTY if the directory is not empty. Over 8801 * the wire, the error is NFSERR_ENOTEMPTY which 8802 * geterrno4 maps to ENOTEMPTY. 8803 */ 8804 if (e.error == ENOTEMPTY) 8805 e.error = EEXIST; 8806 } else { 8807 resop = &res.array[1]; /* remove res */ 8808 rm_res = &resop->nfs_resop4_u.opremove; 8809 8810 if (res.status == NFS4_OK) { 8811 resop = &res.array[2]; /* dir attrs */ 8812 dinfo.di_garp = 8813 &resop->nfs_resop4_u.opgetattr.ga_res; 8814 dinfo.di_cred = cr; 8815 dinfop = &dinfo; 8816 } else 8817 dinfop = NULL; 8818 8819 /* Update dir attribute, readdir and dnlc caches */ 8820 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8821 dinfop); 8822 8823 /* destroy rddir cache for dir that was removed */ 8824 if (VTOR4(vp)->r_dir != NULL) 8825 nfs4_purge_rddir_cache(vp); 8826 } 8827 } 8828 8829 if (need_end_op) 8830 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8831 8832 nfs_rw_exit(&drp->r_rwlock); 8833 8834 if (resp) 8835 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8836 8837 if (e.error == 0) { 8838 vnode_t *tvp; 8839 rnode4_t *trp; 8840 trp = VTOR4(vp); 8841 tvp = vp; 8842 if (IS_SHADOW(vp, trp)) 8843 tvp = RTOV4(trp); 8844 vnevent_rmdir(tvp, dvp, nm, ct); 8845 } 8846 8847 VN_RELE(vp); 8848 8849 return (e.error); 8850 } 8851 8852 /* ARGSUSED */ 8853 static int 8854 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr, 8855 caller_context_t *ct, int flags) 8856 { 8857 int error; 8858 vnode_t *vp; 8859 rnode4_t *rp; 8860 char *contents; 8861 mntinfo4_t *mi = VTOMI4(dvp); 8862 8863 if (nfs_zone() != mi->mi_zone) 8864 return (EPERM); 8865 if (!(mi->mi_flags & MI4_SYMLINK)) 8866 return (EOPNOTSUPP); 8867 8868 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8869 if (error) 8870 return (error); 8871 8872 ASSERT(nfs4_consistent_type(vp)); 8873 rp = VTOR4(vp); 8874 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8875 8876 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8877 8878 if (contents != NULL) { 8879 mutex_enter(&rp->r_statelock); 8880 if (rp->r_symlink.contents == NULL) { 8881 rp->r_symlink.len = strlen(tnm); 8882 bcopy(tnm, contents, rp->r_symlink.len); 8883 rp->r_symlink.contents = contents; 8884 rp->r_symlink.size = MAXPATHLEN; 8885 mutex_exit(&rp->r_statelock); 8886 } else { 8887 mutex_exit(&rp->r_statelock); 8888 kmem_free((void *)contents, MAXPATHLEN); 8889 } 8890 } 8891 } 8892 VN_RELE(vp); 8893 8894 return (error); 8895 } 8896 8897 8898 /* 8899 * Read directory entries. 8900 * There are some weird things to look out for here. The uio_loffset 8901 * field is either 0 or it is the offset returned from a previous 8902 * readdir. It is an opaque value used by the server to find the 8903 * correct directory block to read. The count field is the number 8904 * of blocks to read on the server. This is advisory only, the server 8905 * may return only one block's worth of entries. Entries may be compressed 8906 * on the server. 8907 */ 8908 /* ARGSUSED */ 8909 static int 8910 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp, 8911 caller_context_t *ct, int flags) 8912 { 8913 int error; 8914 uint_t count; 8915 rnode4_t *rp; 8916 rddir4_cache *rdc; 8917 rddir4_cache *rrdc; 8918 8919 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8920 return (EIO); 8921 rp = VTOR4(vp); 8922 8923 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8924 8925 /* 8926 * Make sure that the directory cache is valid. 8927 */ 8928 if (rp->r_dir != NULL) { 8929 if (nfs_disable_rddir_cache != 0) { 8930 /* 8931 * Setting nfs_disable_rddir_cache in /etc/system 8932 * allows interoperability with servers that do not 8933 * properly update the attributes of directories. 8934 * Any cached information gets purged before an 8935 * access is made to it. 8936 */ 8937 nfs4_purge_rddir_cache(vp); 8938 } 8939 8940 error = nfs4_validate_caches(vp, cr); 8941 if (error) 8942 return (error); 8943 } 8944 8945 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8946 8947 /* 8948 * Short circuit last readdir which always returns 0 bytes. 8949 * This can be done after the directory has been read through 8950 * completely at least once. This will set r_direof which 8951 * can be used to find the value of the last cookie. 8952 */ 8953 mutex_enter(&rp->r_statelock); 8954 if (rp->r_direof != NULL && 8955 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8956 mutex_exit(&rp->r_statelock); 8957 #ifdef DEBUG 8958 nfs4_readdir_cache_shorts++; 8959 #endif 8960 if (eofp) 8961 *eofp = 1; 8962 return (0); 8963 } 8964 8965 /* 8966 * Look for a cache entry. Cache entries are identified 8967 * by the NFS cookie value and the byte count requested. 8968 */ 8969 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8970 8971 /* 8972 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8973 */ 8974 if (rdc == NULL) { 8975 mutex_exit(&rp->r_statelock); 8976 return (EINTR); 8977 } 8978 8979 /* 8980 * Check to see if we need to fill this entry in. 8981 */ 8982 if (rdc->flags & RDDIRREQ) { 8983 rdc->flags &= ~RDDIRREQ; 8984 rdc->flags |= RDDIR; 8985 mutex_exit(&rp->r_statelock); 8986 8987 /* 8988 * Do the readdir. 8989 */ 8990 nfs4readdir(vp, rdc, cr); 8991 8992 /* 8993 * Reacquire the lock, so that we can continue 8994 */ 8995 mutex_enter(&rp->r_statelock); 8996 /* 8997 * The entry is now complete 8998 */ 8999 rdc->flags &= ~RDDIR; 9000 } 9001 9002 ASSERT(!(rdc->flags & RDDIR)); 9003 9004 /* 9005 * If an error occurred while attempting 9006 * to fill the cache entry, mark the entry invalid and 9007 * just return the error. 9008 */ 9009 if (rdc->error) { 9010 error = rdc->error; 9011 rdc->flags |= RDDIRREQ; 9012 rddir4_cache_rele(rp, rdc); 9013 mutex_exit(&rp->r_statelock); 9014 return (error); 9015 } 9016 9017 /* 9018 * The cache entry is complete and good, 9019 * copyout the dirent structs to the calling 9020 * thread. 9021 */ 9022 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 9023 9024 /* 9025 * If no error occurred during the copyout, 9026 * update the offset in the uio struct to 9027 * contain the value of the next NFS 4 cookie 9028 * and set the eof value appropriately. 9029 */ 9030 if (!error) { 9031 uiop->uio_loffset = rdc->nfs4_ncookie; 9032 if (eofp) 9033 *eofp = rdc->eof; 9034 } 9035 9036 /* 9037 * Decide whether to do readahead. Don't if we 9038 * have already read to the end of directory. 9039 */ 9040 if (rdc->eof) { 9041 /* 9042 * Make the entry the direof only if it is cached 9043 */ 9044 if (rdc->flags & RDDIRCACHED) 9045 rp->r_direof = rdc; 9046 rddir4_cache_rele(rp, rdc); 9047 mutex_exit(&rp->r_statelock); 9048 return (error); 9049 } 9050 9051 /* Determine if a readdir readahead should be done */ 9052 if (!(rp->r_flags & R4LOOKUP)) { 9053 rddir4_cache_rele(rp, rdc); 9054 mutex_exit(&rp->r_statelock); 9055 return (error); 9056 } 9057 9058 /* 9059 * Now look for a readahead entry. 9060 * 9061 * Check to see whether we found an entry for the readahead. 9062 * If so, we don't need to do anything further, so free the new 9063 * entry if one was allocated. Otherwise, allocate a new entry, add 9064 * it to the cache, and then initiate an asynchronous readdir 9065 * operation to fill it. 9066 */ 9067 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 9068 9069 /* 9070 * A readdir cache entry could not be obtained for the readahead. In 9071 * this case we skip the readahead and return. 9072 */ 9073 if (rrdc == NULL) { 9074 rddir4_cache_rele(rp, rdc); 9075 mutex_exit(&rp->r_statelock); 9076 return (error); 9077 } 9078 9079 /* 9080 * Check to see if we need to fill this entry in. 9081 */ 9082 if (rrdc->flags & RDDIRREQ) { 9083 rrdc->flags &= ~RDDIRREQ; 9084 rrdc->flags |= RDDIR; 9085 rddir4_cache_rele(rp, rdc); 9086 mutex_exit(&rp->r_statelock); 9087 #ifdef DEBUG 9088 nfs4_readdir_readahead++; 9089 #endif 9090 /* 9091 * Do the readdir. 9092 */ 9093 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 9094 return (error); 9095 } 9096 9097 rddir4_cache_rele(rp, rrdc); 9098 rddir4_cache_rele(rp, rdc); 9099 mutex_exit(&rp->r_statelock); 9100 return (error); 9101 } 9102 9103 static int 9104 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9105 { 9106 int error; 9107 rnode4_t *rp; 9108 9109 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9110 9111 rp = VTOR4(vp); 9112 9113 /* 9114 * Obtain the readdir results for the caller. 9115 */ 9116 nfs4readdir(vp, rdc, cr); 9117 9118 mutex_enter(&rp->r_statelock); 9119 /* 9120 * The entry is now complete 9121 */ 9122 rdc->flags &= ~RDDIR; 9123 9124 error = rdc->error; 9125 if (error) 9126 rdc->flags |= RDDIRREQ; 9127 rddir4_cache_rele(rp, rdc); 9128 mutex_exit(&rp->r_statelock); 9129 9130 return (error); 9131 } 9132 9133 /* 9134 * Read directory entries. 9135 * There are some weird things to look out for here. The uio_loffset 9136 * field is either 0 or it is the offset returned from a previous 9137 * readdir. It is an opaque value used by the server to find the 9138 * correct directory block to read. The count field is the number 9139 * of blocks to read on the server. This is advisory only, the server 9140 * may return only one block's worth of entries. Entries may be compressed 9141 * on the server. 9142 * 9143 * Generates the following compound request: 9144 * 1. If readdir offset is zero and no dnlc entry for parent exists, 9145 * must include a Lookupp as well. In this case, send: 9146 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 9147 * 2. Otherwise just do: { Putfh <fh>; Readdir } 9148 * 9149 * Get complete attributes and filehandles for entries if this is the 9150 * first read of the directory. Otherwise, just get fileid's. 9151 */ 9152 static void 9153 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 9154 { 9155 COMPOUND4args_clnt args; 9156 COMPOUND4res_clnt res; 9157 READDIR4args *rargs; 9158 READDIR4res_clnt *rd_res; 9159 bitmap4 rd_bitsval; 9160 nfs_argop4 argop[5]; 9161 nfs_resop4 *resop; 9162 rnode4_t *rp = VTOR4(vp); 9163 mntinfo4_t *mi = VTOMI4(vp); 9164 int doqueue; 9165 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 9166 vnode_t *dvp; 9167 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 9168 int num_ops, res_opcnt; 9169 bool_t needrecov = FALSE; 9170 nfs4_recov_state_t recov_state; 9171 hrtime_t t; 9172 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 9173 9174 ASSERT(nfs_zone() == mi->mi_zone); 9175 ASSERT(rdc->flags & RDDIR); 9176 ASSERT(rdc->entries == NULL); 9177 9178 /* 9179 * If rp were a stub, it should have triggered and caused 9180 * a mount for us to get this far. 9181 */ 9182 ASSERT(!RP_ISSTUB(rp)); 9183 9184 num_ops = 2; 9185 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9186 /* 9187 * Since nfsv4 readdir may not return entries for "." and "..", 9188 * the client must recreate them: 9189 * To find the correct nodeid, do the following: 9190 * For current node, get nodeid from dnlc. 9191 * - if current node is rootvp, set pnodeid to nodeid. 9192 * - else if parent is in the dnlc, get its nodeid from there. 9193 * - else add LOOKUPP+GETATTR to compound. 9194 */ 9195 nodeid = rp->r_attr.va_nodeid; 9196 if (vp->v_flag & VROOT) { 9197 pnodeid = nodeid; /* root of mount point */ 9198 } else { 9199 dvp = dnlc_lookup(vp, ".."); 9200 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9201 /* parent in dnlc cache - no need for otw */ 9202 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9203 } else { 9204 /* 9205 * parent not in dnlc cache, 9206 * do lookupp to get its id 9207 */ 9208 num_ops = 5; 9209 pnodeid = 0; /* set later by getattr parent */ 9210 } 9211 if (dvp) 9212 VN_RELE(dvp); 9213 } 9214 } 9215 recov_state.rs_flags = 0; 9216 recov_state.rs_num_retry_despite_err = 0; 9217 9218 /* Save the original mount point security flavor */ 9219 (void) save_mnt_secinfo(mi->mi_curr_serv); 9220 9221 recov_retry: 9222 args.ctag = TAG_READDIR; 9223 9224 args.array = argop; 9225 args.array_len = num_ops; 9226 9227 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9228 &recov_state, NULL)) { 9229 /* 9230 * If readdir a node that is a stub for a crossed mount point, 9231 * keep the original secinfo flavor for the current file 9232 * system, not the crossed one. 9233 */ 9234 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9235 rdc->error = e.error; 9236 return; 9237 } 9238 9239 /* 9240 * Determine which attrs to request for dirents. This code 9241 * must be protected by nfs4_start/end_fop because of r_server 9242 * (which will change during failover recovery). 9243 * 9244 */ 9245 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9246 /* 9247 * Get all vattr attrs plus filehandle and rdattr_error 9248 */ 9249 rd_bitsval = NFS4_VATTR_MASK | 9250 FATTR4_RDATTR_ERROR_MASK | 9251 FATTR4_FILEHANDLE_MASK; 9252 9253 if (rp->r_flags & R4READDIRWATTR) { 9254 mutex_enter(&rp->r_statelock); 9255 rp->r_flags &= ~R4READDIRWATTR; 9256 mutex_exit(&rp->r_statelock); 9257 } 9258 } else { 9259 servinfo4_t *svp = rp->r_server; 9260 9261 /* 9262 * Already read directory. Use readdir with 9263 * no attrs (except for mounted_on_fileid) for updates. 9264 */ 9265 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9266 9267 /* 9268 * request mounted on fileid if supported, else request 9269 * fileid. maybe we should verify that fileid is supported 9270 * and request something else if not. 9271 */ 9272 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9273 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9274 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9275 nfs_rw_exit(&svp->sv_lock); 9276 } 9277 9278 /* putfh directory fh */ 9279 argop[0].argop = OP_CPUTFH; 9280 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9281 9282 argop[1].argop = OP_READDIR; 9283 rargs = &argop[1].nfs_argop4_u.opreaddir; 9284 /* 9285 * 1 and 2 are reserved for client "." and ".." entry offset. 9286 * cookie 0 should be used over-the-wire to start reading at 9287 * the beginning of the directory excluding "." and "..". 9288 */ 9289 if (rdc->nfs4_cookie == 0 || 9290 rdc->nfs4_cookie == 1 || 9291 rdc->nfs4_cookie == 2) { 9292 rargs->cookie = (nfs_cookie4)0; 9293 rargs->cookieverf = 0; 9294 } else { 9295 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9296 mutex_enter(&rp->r_statelock); 9297 rargs->cookieverf = rp->r_cookieverf4; 9298 mutex_exit(&rp->r_statelock); 9299 } 9300 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9301 rargs->maxcount = mi->mi_tsize; 9302 rargs->attr_request = rd_bitsval; 9303 rargs->rdc = rdc; 9304 rargs->dvp = vp; 9305 rargs->mi = mi; 9306 rargs->cr = cr; 9307 9308 9309 /* 9310 * If count < than the minimum required, we return no entries 9311 * and fail with EINVAL 9312 */ 9313 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9314 rdc->error = EINVAL; 9315 goto out; 9316 } 9317 9318 if (args.array_len == 5) { 9319 /* 9320 * Add lookupp and getattr for parent nodeid. 9321 */ 9322 argop[2].argop = OP_LOOKUPP; 9323 9324 argop[3].argop = OP_GETFH; 9325 9326 /* getattr parent */ 9327 argop[4].argop = OP_GETATTR; 9328 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9329 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9330 } 9331 9332 doqueue = 1; 9333 9334 if (mi->mi_io_kstats) { 9335 mutex_enter(&mi->mi_lock); 9336 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9337 mutex_exit(&mi->mi_lock); 9338 } 9339 9340 /* capture the time of this call */ 9341 rargs->t = t = gethrtime(); 9342 9343 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9344 9345 if (mi->mi_io_kstats) { 9346 mutex_enter(&mi->mi_lock); 9347 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9348 mutex_exit(&mi->mi_lock); 9349 } 9350 9351 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9352 9353 /* 9354 * If RPC error occurred and it isn't an error that 9355 * triggers recovery, then go ahead and fail now. 9356 */ 9357 if (e.error != 0 && !needrecov) { 9358 rdc->error = e.error; 9359 goto out; 9360 } 9361 9362 if (needrecov) { 9363 bool_t abort; 9364 9365 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9366 "nfs4readdir: initiating recovery.\n")); 9367 9368 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9369 NULL, OP_READDIR, NULL, NULL, NULL); 9370 if (abort == FALSE) { 9371 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9372 &recov_state, needrecov); 9373 if (!e.error) 9374 (void) xdr_free(xdr_COMPOUND4res_clnt, 9375 (caddr_t)&res); 9376 if (rdc->entries != NULL) { 9377 kmem_free(rdc->entries, rdc->entlen); 9378 rdc->entries = NULL; 9379 } 9380 goto recov_retry; 9381 } 9382 9383 if (e.error != 0) { 9384 rdc->error = e.error; 9385 goto out; 9386 } 9387 9388 /* fall through for res.status case */ 9389 } 9390 9391 res_opcnt = res.array_len; 9392 9393 /* 9394 * If compound failed first 2 ops (PUTFH+READDIR), then return 9395 * failure here. Subsequent ops are for filling out dot-dot 9396 * dirent, and if they fail, we still want to give the caller 9397 * the dirents returned by (the successful) READDIR op, so we need 9398 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9399 * 9400 * One example where PUTFH+READDIR ops would succeed but 9401 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9402 * but lacks x. In this case, a POSIX server's VOP_READDIR 9403 * would succeed; however, VOP_LOOKUP(..) would fail since no 9404 * x perm. We need to come up with a non-vendor-specific way 9405 * for a POSIX server to return d_ino from dotdot's dirent if 9406 * client only requests mounted_on_fileid, and just say the 9407 * LOOKUPP succeeded and fill out the GETATTR. However, if 9408 * client requested any mandatory attrs, server would be required 9409 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9410 * for dotdot. 9411 */ 9412 9413 if (res.status) { 9414 if (res_opcnt <= 2) { 9415 e.error = geterrno4(res.status); 9416 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9417 &recov_state, needrecov); 9418 nfs4_purge_stale_fh(e.error, vp, cr); 9419 rdc->error = e.error; 9420 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9421 if (rdc->entries != NULL) { 9422 kmem_free(rdc->entries, rdc->entlen); 9423 rdc->entries = NULL; 9424 } 9425 /* 9426 * If readdir a node that is a stub for a 9427 * crossed mount point, keep the original 9428 * secinfo flavor for the current file system, 9429 * not the crossed one. 9430 */ 9431 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9432 return; 9433 } 9434 } 9435 9436 resop = &res.array[1]; /* readdir res */ 9437 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9438 9439 mutex_enter(&rp->r_statelock); 9440 rp->r_cookieverf4 = rd_res->cookieverf; 9441 mutex_exit(&rp->r_statelock); 9442 9443 /* 9444 * For "." and ".." entries 9445 * e.g. 9446 * seek(cookie=0) -> "." entry with d_off = 1 9447 * seek(cookie=1) -> ".." entry with d_off = 2 9448 */ 9449 if (cookie == (nfs_cookie4) 0) { 9450 if (rd_res->dotp) 9451 rd_res->dotp->d_ino = nodeid; 9452 if (rd_res->dotdotp) 9453 rd_res->dotdotp->d_ino = pnodeid; 9454 } 9455 if (cookie == (nfs_cookie4) 1) { 9456 if (rd_res->dotdotp) 9457 rd_res->dotdotp->d_ino = pnodeid; 9458 } 9459 9460 9461 /* LOOKUPP+GETATTR attemped */ 9462 if (args.array_len == 5 && rd_res->dotdotp) { 9463 if (res.status == NFS4_OK && res_opcnt == 5) { 9464 nfs_fh4 *fhp; 9465 nfs4_sharedfh_t *sfhp; 9466 vnode_t *pvp; 9467 nfs4_ga_res_t *garp; 9468 9469 resop++; /* lookupp */ 9470 resop++; /* getfh */ 9471 fhp = &resop->nfs_resop4_u.opgetfh.object; 9472 9473 resop++; /* getattr of parent */ 9474 9475 /* 9476 * First, take care of finishing the 9477 * readdir results. 9478 */ 9479 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9480 /* 9481 * The d_ino of .. must be the inode number 9482 * of the mounted filesystem. 9483 */ 9484 if (garp->n4g_va.va_mask & AT_NODEID) 9485 rd_res->dotdotp->d_ino = 9486 garp->n4g_va.va_nodeid; 9487 9488 9489 /* 9490 * Next, create the ".." dnlc entry 9491 */ 9492 sfhp = sfh4_get(fhp, mi); 9493 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9494 dnlc_update(vp, "..", pvp); 9495 VN_RELE(pvp); 9496 } 9497 sfh4_rele(&sfhp); 9498 } 9499 } 9500 9501 if (mi->mi_io_kstats) { 9502 mutex_enter(&mi->mi_lock); 9503 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9504 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9505 mutex_exit(&mi->mi_lock); 9506 } 9507 9508 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9509 9510 out: 9511 /* 9512 * If readdir a node that is a stub for a crossed mount point, 9513 * keep the original secinfo flavor for the current file system, 9514 * not the crossed one. 9515 */ 9516 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9517 9518 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9519 } 9520 9521 9522 static int 9523 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9524 { 9525 rnode4_t *rp = VTOR4(bp->b_vp); 9526 int count; 9527 int error; 9528 cred_t *cred_otw = NULL; 9529 offset_t offset; 9530 nfs4_open_stream_t *osp = NULL; 9531 bool_t first_time = TRUE; /* first time getting otw cred */ 9532 bool_t last_time = FALSE; /* last time getting otw cred */ 9533 9534 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9535 9536 DTRACE_IO1(start, struct buf *, bp); 9537 offset = ldbtob(bp->b_lblkno); 9538 9539 if (bp->b_flags & B_READ) { 9540 read_again: 9541 /* 9542 * Releases the osp, if it is provided. 9543 * Puts a hold on the cred_otw and the new osp (if found). 9544 */ 9545 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9546 &first_time, &last_time); 9547 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9548 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9549 readahead, NULL); 9550 crfree(cred_otw); 9551 if (!error) { 9552 if (bp->b_resid) { 9553 /* 9554 * Didn't get it all because we hit EOF, 9555 * zero all the memory beyond the EOF. 9556 */ 9557 /* bzero(rdaddr + */ 9558 bzero(bp->b_un.b_addr + 9559 bp->b_bcount - bp->b_resid, bp->b_resid); 9560 } 9561 mutex_enter(&rp->r_statelock); 9562 if (bp->b_resid == bp->b_bcount && 9563 offset >= rp->r_size) { 9564 /* 9565 * We didn't read anything at all as we are 9566 * past EOF. Return an error indicator back 9567 * but don't destroy the pages (yet). 9568 */ 9569 error = NFS_EOF; 9570 } 9571 mutex_exit(&rp->r_statelock); 9572 } else if (error == EACCES && last_time == FALSE) { 9573 goto read_again; 9574 } 9575 } else { 9576 if (!(rp->r_flags & R4STALE)) { 9577 write_again: 9578 /* 9579 * Releases the osp, if it is provided. 9580 * Puts a hold on the cred_otw and the new 9581 * osp (if found). 9582 */ 9583 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9584 &first_time, &last_time); 9585 mutex_enter(&rp->r_statelock); 9586 count = MIN(bp->b_bcount, rp->r_size - offset); 9587 mutex_exit(&rp->r_statelock); 9588 if (count < 0) 9589 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9590 #ifdef DEBUG 9591 if (count == 0) { 9592 zoneid_t zoneid = getzoneid(); 9593 9594 zcmn_err(zoneid, CE_WARN, 9595 "nfs4_bio: zero length write at %lld", 9596 offset); 9597 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9598 "b_bcount=%ld, file size=%lld", 9599 rp->r_flags, (long)bp->b_bcount, 9600 rp->r_size); 9601 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9602 if (nfs4_bio_do_stop) 9603 debug_enter("nfs4_bio"); 9604 } 9605 #endif 9606 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9607 count, cred_otw, stab_comm); 9608 if (error == EACCES && last_time == FALSE) { 9609 crfree(cred_otw); 9610 goto write_again; 9611 } 9612 bp->b_error = error; 9613 if (error && error != EINTR && 9614 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9615 /* 9616 * Don't print EDQUOT errors on the console. 9617 * Don't print asynchronous EACCES errors. 9618 * Don't print EFBIG errors. 9619 * Print all other write errors. 9620 */ 9621 if (error != EDQUOT && error != EFBIG && 9622 (error != EACCES || 9623 !(bp->b_flags & B_ASYNC))) 9624 nfs4_write_error(bp->b_vp, 9625 error, cred_otw); 9626 /* 9627 * Update r_error and r_flags as appropriate. 9628 * If the error was ESTALE, then mark the 9629 * rnode as not being writeable and save 9630 * the error status. Otherwise, save any 9631 * errors which occur from asynchronous 9632 * page invalidations. Any errors occurring 9633 * from other operations should be saved 9634 * by the caller. 9635 */ 9636 mutex_enter(&rp->r_statelock); 9637 if (error == ESTALE) { 9638 rp->r_flags |= R4STALE; 9639 if (!rp->r_error) 9640 rp->r_error = error; 9641 } else if (!rp->r_error && 9642 (bp->b_flags & 9643 (B_INVAL|B_FORCE|B_ASYNC)) == 9644 (B_INVAL|B_FORCE|B_ASYNC)) { 9645 rp->r_error = error; 9646 } 9647 mutex_exit(&rp->r_statelock); 9648 } 9649 crfree(cred_otw); 9650 } else { 9651 error = rp->r_error; 9652 /* 9653 * A close may have cleared r_error, if so, 9654 * propagate ESTALE error return properly 9655 */ 9656 if (error == 0) 9657 error = ESTALE; 9658 } 9659 } 9660 9661 if (error != 0 && error != NFS_EOF) 9662 bp->b_flags |= B_ERROR; 9663 9664 if (osp) 9665 open_stream_rele(osp, rp); 9666 9667 DTRACE_IO1(done, struct buf *, bp); 9668 9669 return (error); 9670 } 9671 9672 /* ARGSUSED */ 9673 int 9674 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 9675 { 9676 return (EREMOTE); 9677 } 9678 9679 /* ARGSUSED2 */ 9680 int 9681 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9682 { 9683 rnode4_t *rp = VTOR4(vp); 9684 9685 if (!write_lock) { 9686 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9687 return (V_WRITELOCK_FALSE); 9688 } 9689 9690 if ((rp->r_flags & R4DIRECTIO) || 9691 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9692 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9693 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9694 return (V_WRITELOCK_FALSE); 9695 nfs_rw_exit(&rp->r_rwlock); 9696 } 9697 9698 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9699 return (V_WRITELOCK_TRUE); 9700 } 9701 9702 /* ARGSUSED */ 9703 void 9704 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9705 { 9706 rnode4_t *rp = VTOR4(vp); 9707 9708 nfs_rw_exit(&rp->r_rwlock); 9709 } 9710 9711 /* ARGSUSED */ 9712 static int 9713 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct) 9714 { 9715 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9716 return (EIO); 9717 9718 /* 9719 * Because we stuff the readdir cookie into the offset field 9720 * someone may attempt to do an lseek with the cookie which 9721 * we want to succeed. 9722 */ 9723 if (vp->v_type == VDIR) 9724 return (0); 9725 if (*noffp < 0) 9726 return (EINVAL); 9727 return (0); 9728 } 9729 9730 9731 /* 9732 * Return all the pages from [off..off+len) in file 9733 */ 9734 /* ARGSUSED */ 9735 static int 9736 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9737 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9738 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 9739 { 9740 rnode4_t *rp; 9741 int error; 9742 mntinfo4_t *mi; 9743 9744 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9745 return (EIO); 9746 rp = VTOR4(vp); 9747 if (IS_SHADOW(vp, rp)) 9748 vp = RTOV4(rp); 9749 9750 if (vp->v_flag & VNOMAP) 9751 return (ENOSYS); 9752 9753 if (protp != NULL) 9754 *protp = PROT_ALL; 9755 9756 /* 9757 * Now validate that the caches are up to date. 9758 */ 9759 if (error = nfs4_validate_caches(vp, cr)) 9760 return (error); 9761 9762 mi = VTOMI4(vp); 9763 retry: 9764 mutex_enter(&rp->r_statelock); 9765 9766 /* 9767 * Don't create dirty pages faster than they 9768 * can be cleaned so that the system doesn't 9769 * get imbalanced. If the async queue is 9770 * maxed out, then wait for it to drain before 9771 * creating more dirty pages. Also, wait for 9772 * any threads doing pagewalks in the vop_getattr 9773 * entry points so that they don't block for 9774 * long periods. 9775 */ 9776 if (rw == S_CREATE) { 9777 while ((mi->mi_max_threads != 0 && 9778 rp->r_awcount > 2 * mi->mi_max_threads) || 9779 rp->r_gcount > 0) 9780 cv_wait(&rp->r_cv, &rp->r_statelock); 9781 } 9782 9783 /* 9784 * If we are getting called as a side effect of an nfs_write() 9785 * operation the local file size might not be extended yet. 9786 * In this case we want to be able to return pages of zeroes. 9787 */ 9788 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9789 NFS4_DEBUG(nfs4_pageio_debug, 9790 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9791 "len=%llu, size=%llu, attrsize =%llu", off, 9792 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9793 mutex_exit(&rp->r_statelock); 9794 return (EFAULT); /* beyond EOF */ 9795 } 9796 9797 mutex_exit(&rp->r_statelock); 9798 9799 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9800 pl, plsz, seg, addr, rw, cr); 9801 NFS4_DEBUG(nfs4_pageio_debug && error, 9802 (CE_NOTE, "getpages error %d; off=%lld, len=%lld", 9803 error, off, (u_longlong_t)len)); 9804 9805 switch (error) { 9806 case NFS_EOF: 9807 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9808 goto retry; 9809 case ESTALE: 9810 nfs4_purge_stale_fh(error, vp, cr); 9811 } 9812 9813 return (error); 9814 } 9815 9816 /* 9817 * Called from pvn_getpages to get a particular page. 9818 */ 9819 /* ARGSUSED */ 9820 static int 9821 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9822 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9823 enum seg_rw rw, cred_t *cr) 9824 { 9825 rnode4_t *rp; 9826 uint_t bsize; 9827 struct buf *bp; 9828 page_t *pp; 9829 u_offset_t lbn; 9830 u_offset_t io_off; 9831 u_offset_t blkoff; 9832 u_offset_t rablkoff; 9833 size_t io_len; 9834 uint_t blksize; 9835 int error; 9836 int readahead; 9837 int readahead_issued = 0; 9838 int ra_window; /* readahead window */ 9839 page_t *pagefound; 9840 page_t *savepp; 9841 9842 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9843 return (EIO); 9844 9845 rp = VTOR4(vp); 9846 ASSERT(!IS_SHADOW(vp, rp)); 9847 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9848 9849 reread: 9850 bp = NULL; 9851 pp = NULL; 9852 pagefound = NULL; 9853 9854 if (pl != NULL) 9855 pl[0] = NULL; 9856 9857 error = 0; 9858 lbn = off / bsize; 9859 blkoff = lbn * bsize; 9860 9861 /* 9862 * Queueing up the readahead before doing the synchronous read 9863 * results in a significant increase in read throughput because 9864 * of the increased parallelism between the async threads and 9865 * the process context. 9866 */ 9867 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9868 rw != S_CREATE && 9869 !(vp->v_flag & VNOCACHE)) { 9870 mutex_enter(&rp->r_statelock); 9871 9872 /* 9873 * Calculate the number of readaheads to do. 9874 * a) No readaheads at offset = 0. 9875 * b) Do maximum(nfs4_nra) readaheads when the readahead 9876 * window is closed. 9877 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9878 * upon how far the readahead window is open or close. 9879 * d) No readaheads if rp->r_nextr is not within the scope 9880 * of the readahead window (random i/o). 9881 */ 9882 9883 if (off == 0) 9884 readahead = 0; 9885 else if (blkoff == rp->r_nextr) 9886 readahead = nfs4_nra; 9887 else if (rp->r_nextr > blkoff && 9888 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9889 <= (nfs4_nra - 1))) 9890 readahead = nfs4_nra - ra_window; 9891 else 9892 readahead = 0; 9893 9894 rablkoff = rp->r_nextr; 9895 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9896 mutex_exit(&rp->r_statelock); 9897 if (nfs4_async_readahead(vp, rablkoff + bsize, 9898 addr + (rablkoff + bsize - off), 9899 seg, cr, nfs4_readahead) < 0) { 9900 mutex_enter(&rp->r_statelock); 9901 break; 9902 } 9903 readahead--; 9904 rablkoff += bsize; 9905 /* 9906 * Indicate that we did a readahead so 9907 * readahead offset is not updated 9908 * by the synchronous read below. 9909 */ 9910 readahead_issued = 1; 9911 mutex_enter(&rp->r_statelock); 9912 /* 9913 * set readahead offset to 9914 * offset of last async readahead 9915 * request. 9916 */ 9917 rp->r_nextr = rablkoff; 9918 } 9919 mutex_exit(&rp->r_statelock); 9920 } 9921 9922 again: 9923 if ((pagefound = page_exists(vp, off)) == NULL) { 9924 if (pl == NULL) { 9925 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9926 nfs4_readahead); 9927 } else if (rw == S_CREATE) { 9928 /* 9929 * Block for this page is not allocated, or the offset 9930 * is beyond the current allocation size, or we're 9931 * allocating a swap slot and the page was not found, 9932 * so allocate it and return a zero page. 9933 */ 9934 if ((pp = page_create_va(vp, off, 9935 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9936 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9937 io_len = PAGESIZE; 9938 mutex_enter(&rp->r_statelock); 9939 rp->r_nextr = off + PAGESIZE; 9940 mutex_exit(&rp->r_statelock); 9941 } else { 9942 /* 9943 * Need to go to server to get a block 9944 */ 9945 mutex_enter(&rp->r_statelock); 9946 if (blkoff < rp->r_size && 9947 blkoff + bsize > rp->r_size) { 9948 /* 9949 * If less than a block left in 9950 * file read less than a block. 9951 */ 9952 if (rp->r_size <= off) { 9953 /* 9954 * Trying to access beyond EOF, 9955 * set up to get at least one page. 9956 */ 9957 blksize = off + PAGESIZE - blkoff; 9958 } else 9959 blksize = rp->r_size - blkoff; 9960 } else if ((off == 0) || 9961 (off != rp->r_nextr && !readahead_issued)) { 9962 blksize = PAGESIZE; 9963 blkoff = off; /* block = page here */ 9964 } else 9965 blksize = bsize; 9966 mutex_exit(&rp->r_statelock); 9967 9968 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9969 &io_len, blkoff, blksize, 0); 9970 9971 /* 9972 * Some other thread has entered the page, 9973 * so just use it. 9974 */ 9975 if (pp == NULL) 9976 goto again; 9977 9978 /* 9979 * Now round the request size up to page boundaries. 9980 * This ensures that the entire page will be 9981 * initialized to zeroes if EOF is encountered. 9982 */ 9983 io_len = ptob(btopr(io_len)); 9984 9985 bp = pageio_setup(pp, io_len, vp, B_READ); 9986 ASSERT(bp != NULL); 9987 9988 /* 9989 * pageio_setup should have set b_addr to 0. This 9990 * is correct since we want to do I/O on a page 9991 * boundary. bp_mapin will use this addr to calculate 9992 * an offset, and then set b_addr to the kernel virtual 9993 * address it allocated for us. 9994 */ 9995 ASSERT(bp->b_un.b_addr == 0); 9996 9997 bp->b_edev = 0; 9998 bp->b_dev = 0; 9999 bp->b_lblkno = lbtodb(io_off); 10000 bp->b_file = vp; 10001 bp->b_offset = (offset_t)off; 10002 bp_mapin(bp); 10003 10004 /* 10005 * If doing a write beyond what we believe is EOF, 10006 * don't bother trying to read the pages from the 10007 * server, we'll just zero the pages here. We 10008 * don't check that the rw flag is S_WRITE here 10009 * because some implementations may attempt a 10010 * read access to the buffer before copying data. 10011 */ 10012 mutex_enter(&rp->r_statelock); 10013 if (io_off >= rp->r_size && seg == segkmap) { 10014 mutex_exit(&rp->r_statelock); 10015 bzero(bp->b_un.b_addr, io_len); 10016 } else { 10017 mutex_exit(&rp->r_statelock); 10018 error = nfs4_bio(bp, NULL, cr, FALSE); 10019 } 10020 10021 /* 10022 * Unmap the buffer before freeing it. 10023 */ 10024 bp_mapout(bp); 10025 pageio_done(bp); 10026 10027 savepp = pp; 10028 do { 10029 pp->p_fsdata = C_NOCOMMIT; 10030 } while ((pp = pp->p_next) != savepp); 10031 10032 if (error == NFS_EOF) { 10033 /* 10034 * If doing a write system call just return 10035 * zeroed pages, else user tried to get pages 10036 * beyond EOF, return error. We don't check 10037 * that the rw flag is S_WRITE here because 10038 * some implementations may attempt a read 10039 * access to the buffer before copying data. 10040 */ 10041 if (seg == segkmap) 10042 error = 0; 10043 else 10044 error = EFAULT; 10045 } 10046 10047 if (!readahead_issued && !error) { 10048 mutex_enter(&rp->r_statelock); 10049 rp->r_nextr = io_off + io_len; 10050 mutex_exit(&rp->r_statelock); 10051 } 10052 } 10053 } 10054 10055 out: 10056 if (pl == NULL) 10057 return (error); 10058 10059 if (error) { 10060 if (pp != NULL) 10061 pvn_read_done(pp, B_ERROR); 10062 return (error); 10063 } 10064 10065 if (pagefound) { 10066 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 10067 10068 /* 10069 * Page exists in the cache, acquire the appropriate lock. 10070 * If this fails, start all over again. 10071 */ 10072 if ((pp = page_lookup(vp, off, se)) == NULL) { 10073 #ifdef DEBUG 10074 nfs4_lostpage++; 10075 #endif 10076 goto reread; 10077 } 10078 pl[0] = pp; 10079 pl[1] = NULL; 10080 return (0); 10081 } 10082 10083 if (pp != NULL) 10084 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 10085 10086 return (error); 10087 } 10088 10089 static void 10090 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 10091 cred_t *cr) 10092 { 10093 int error; 10094 page_t *pp; 10095 u_offset_t io_off; 10096 size_t io_len; 10097 struct buf *bp; 10098 uint_t bsize, blksize; 10099 rnode4_t *rp = VTOR4(vp); 10100 page_t *savepp; 10101 10102 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10103 10104 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10105 10106 mutex_enter(&rp->r_statelock); 10107 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 10108 /* 10109 * If less than a block left in file read less 10110 * than a block. 10111 */ 10112 blksize = rp->r_size - blkoff; 10113 } else 10114 blksize = bsize; 10115 mutex_exit(&rp->r_statelock); 10116 10117 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 10118 &io_off, &io_len, blkoff, blksize, 1); 10119 /* 10120 * The isra flag passed to the kluster function is 1, we may have 10121 * gotten a return value of NULL for a variety of reasons (# of free 10122 * pages < minfree, someone entered the page on the vnode etc). In all 10123 * cases, we want to punt on the readahead. 10124 */ 10125 if (pp == NULL) 10126 return; 10127 10128 /* 10129 * Now round the request size up to page boundaries. 10130 * This ensures that the entire page will be 10131 * initialized to zeroes if EOF is encountered. 10132 */ 10133 io_len = ptob(btopr(io_len)); 10134 10135 bp = pageio_setup(pp, io_len, vp, B_READ); 10136 ASSERT(bp != NULL); 10137 10138 /* 10139 * pageio_setup should have set b_addr to 0. This is correct since 10140 * we want to do I/O on a page boundary. bp_mapin() will use this addr 10141 * to calculate an offset, and then set b_addr to the kernel virtual 10142 * address it allocated for us. 10143 */ 10144 ASSERT(bp->b_un.b_addr == 0); 10145 10146 bp->b_edev = 0; 10147 bp->b_dev = 0; 10148 bp->b_lblkno = lbtodb(io_off); 10149 bp->b_file = vp; 10150 bp->b_offset = (offset_t)blkoff; 10151 bp_mapin(bp); 10152 10153 /* 10154 * If doing a write beyond what we believe is EOF, don't bother trying 10155 * to read the pages from the server, we'll just zero the pages here. 10156 * We don't check that the rw flag is S_WRITE here because some 10157 * implementations may attempt a read access to the buffer before 10158 * copying data. 10159 */ 10160 mutex_enter(&rp->r_statelock); 10161 if (io_off >= rp->r_size && seg == segkmap) { 10162 mutex_exit(&rp->r_statelock); 10163 bzero(bp->b_un.b_addr, io_len); 10164 error = 0; 10165 } else { 10166 mutex_exit(&rp->r_statelock); 10167 error = nfs4_bio(bp, NULL, cr, TRUE); 10168 if (error == NFS_EOF) 10169 error = 0; 10170 } 10171 10172 /* 10173 * Unmap the buffer before freeing it. 10174 */ 10175 bp_mapout(bp); 10176 pageio_done(bp); 10177 10178 savepp = pp; 10179 do { 10180 pp->p_fsdata = C_NOCOMMIT; 10181 } while ((pp = pp->p_next) != savepp); 10182 10183 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10184 10185 /* 10186 * In case of error set readahead offset 10187 * to the lowest offset. 10188 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10189 */ 10190 if (error && rp->r_nextr > io_off) { 10191 mutex_enter(&rp->r_statelock); 10192 if (rp->r_nextr > io_off) 10193 rp->r_nextr = io_off; 10194 mutex_exit(&rp->r_statelock); 10195 } 10196 } 10197 10198 /* 10199 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10200 * If len == 0, do from off to EOF. 10201 * 10202 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10203 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10204 * (from pageout). 10205 */ 10206 /* ARGSUSED */ 10207 static int 10208 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 10209 caller_context_t *ct) 10210 { 10211 int error; 10212 rnode4_t *rp; 10213 10214 ASSERT(cr != NULL); 10215 10216 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10217 return (EIO); 10218 10219 rp = VTOR4(vp); 10220 if (IS_SHADOW(vp, rp)) 10221 vp = RTOV4(rp); 10222 10223 /* 10224 * XXX - Why should this check be made here? 10225 */ 10226 if (vp->v_flag & VNOMAP) 10227 return (ENOSYS); 10228 10229 if (len == 0 && !(flags & B_INVAL) && 10230 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10231 return (0); 10232 10233 mutex_enter(&rp->r_statelock); 10234 rp->r_count++; 10235 mutex_exit(&rp->r_statelock); 10236 error = nfs4_putpages(vp, off, len, flags, cr); 10237 mutex_enter(&rp->r_statelock); 10238 rp->r_count--; 10239 cv_broadcast(&rp->r_cv); 10240 mutex_exit(&rp->r_statelock); 10241 10242 return (error); 10243 } 10244 10245 /* 10246 * Write out a single page, possibly klustering adjacent dirty pages. 10247 */ 10248 int 10249 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10250 int flags, cred_t *cr) 10251 { 10252 u_offset_t io_off; 10253 u_offset_t lbn_off; 10254 u_offset_t lbn; 10255 size_t io_len; 10256 uint_t bsize; 10257 int error; 10258 rnode4_t *rp; 10259 10260 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10261 ASSERT(pp != NULL); 10262 ASSERT(cr != NULL); 10263 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10264 10265 rp = VTOR4(vp); 10266 ASSERT(rp->r_count > 0); 10267 ASSERT(!IS_SHADOW(vp, rp)); 10268 10269 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10270 lbn = pp->p_offset / bsize; 10271 lbn_off = lbn * bsize; 10272 10273 /* 10274 * Find a kluster that fits in one block, or in 10275 * one page if pages are bigger than blocks. If 10276 * there is less file space allocated than a whole 10277 * page, we'll shorten the i/o request below. 10278 */ 10279 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10280 roundup(bsize, PAGESIZE), flags); 10281 10282 /* 10283 * pvn_write_kluster shouldn't have returned a page with offset 10284 * behind the original page we were given. Verify that. 10285 */ 10286 ASSERT((pp->p_offset / bsize) >= lbn); 10287 10288 /* 10289 * Now pp will have the list of kept dirty pages marked for 10290 * write back. It will also handle invalidation and freeing 10291 * of pages that are not dirty. Check for page length rounding 10292 * problems. 10293 */ 10294 if (io_off + io_len > lbn_off + bsize) { 10295 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10296 io_len = lbn_off + bsize - io_off; 10297 } 10298 /* 10299 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10300 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10301 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10302 * progress and the r_size has not been made consistent with the 10303 * new size of the file. When the uiomove() completes the r_size is 10304 * updated and the R4MODINPROGRESS flag is cleared. 10305 * 10306 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10307 * consistent value of r_size. Without this handshaking, it is 10308 * possible that nfs4_bio() picks up the old value of r_size 10309 * before the uiomove() in writerp4() completes. This will result 10310 * in the write through nfs4_bio() being dropped. 10311 * 10312 * More precisely, there is a window between the time the uiomove() 10313 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10314 * operation intervenes in this window, the page will be picked up, 10315 * because it is dirty (it will be unlocked, unless it was 10316 * pagecreate'd). When the page is picked up as dirty, the dirty 10317 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10318 * checked. This will still be the old size. Therefore the page will 10319 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10320 * the page will be found to be clean and the write will be dropped. 10321 */ 10322 if (rp->r_flags & R4MODINPROGRESS) { 10323 mutex_enter(&rp->r_statelock); 10324 if ((rp->r_flags & R4MODINPROGRESS) && 10325 rp->r_modaddr + MAXBSIZE > io_off && 10326 rp->r_modaddr < io_off + io_len) { 10327 page_t *plist; 10328 /* 10329 * A write is in progress for this region of the file. 10330 * If we did not detect R4MODINPROGRESS here then this 10331 * path through nfs_putapage() would eventually go to 10332 * nfs4_bio() and may not write out all of the data 10333 * in the pages. We end up losing data. So we decide 10334 * to set the modified bit on each page in the page 10335 * list and mark the rnode with R4DIRTY. This write 10336 * will be restarted at some later time. 10337 */ 10338 plist = pp; 10339 while (plist != NULL) { 10340 pp = plist; 10341 page_sub(&plist, pp); 10342 hat_setmod(pp); 10343 page_io_unlock(pp); 10344 page_unlock(pp); 10345 } 10346 rp->r_flags |= R4DIRTY; 10347 mutex_exit(&rp->r_statelock); 10348 if (offp) 10349 *offp = io_off; 10350 if (lenp) 10351 *lenp = io_len; 10352 return (0); 10353 } 10354 mutex_exit(&rp->r_statelock); 10355 } 10356 10357 if (flags & B_ASYNC) { 10358 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10359 nfs4_sync_putapage); 10360 } else 10361 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10362 10363 if (offp) 10364 *offp = io_off; 10365 if (lenp) 10366 *lenp = io_len; 10367 return (error); 10368 } 10369 10370 static int 10371 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10372 int flags, cred_t *cr) 10373 { 10374 int error; 10375 rnode4_t *rp; 10376 10377 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10378 10379 flags |= B_WRITE; 10380 10381 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10382 10383 rp = VTOR4(vp); 10384 10385 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10386 error == EACCES) && 10387 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10388 if (!(rp->r_flags & R4OUTOFSPACE)) { 10389 mutex_enter(&rp->r_statelock); 10390 rp->r_flags |= R4OUTOFSPACE; 10391 mutex_exit(&rp->r_statelock); 10392 } 10393 flags |= B_ERROR; 10394 pvn_write_done(pp, flags); 10395 /* 10396 * If this was not an async thread, then try again to 10397 * write out the pages, but this time, also destroy 10398 * them whether or not the write is successful. This 10399 * will prevent memory from filling up with these 10400 * pages and destroying them is the only alternative 10401 * if they can't be written out. 10402 * 10403 * Don't do this if this is an async thread because 10404 * when the pages are unlocked in pvn_write_done, 10405 * some other thread could have come along, locked 10406 * them, and queued for an async thread. It would be 10407 * possible for all of the async threads to be tied 10408 * up waiting to lock the pages again and they would 10409 * all already be locked and waiting for an async 10410 * thread to handle them. Deadlock. 10411 */ 10412 if (!(flags & B_ASYNC)) { 10413 error = nfs4_putpage(vp, io_off, io_len, 10414 B_INVAL | B_FORCE, cr, NULL); 10415 } 10416 } else { 10417 if (error) 10418 flags |= B_ERROR; 10419 else if (rp->r_flags & R4OUTOFSPACE) { 10420 mutex_enter(&rp->r_statelock); 10421 rp->r_flags &= ~R4OUTOFSPACE; 10422 mutex_exit(&rp->r_statelock); 10423 } 10424 pvn_write_done(pp, flags); 10425 if (freemem < desfree) 10426 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10427 NFS4_WRITE_NOWAIT); 10428 } 10429 10430 return (error); 10431 } 10432 10433 #ifdef DEBUG 10434 int nfs4_force_open_before_mmap = 0; 10435 #endif 10436 10437 /* ARGSUSED */ 10438 static int 10439 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10440 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10441 caller_context_t *ct) 10442 { 10443 struct segvn_crargs vn_a; 10444 int error = 0; 10445 rnode4_t *rp = VTOR4(vp); 10446 mntinfo4_t *mi = VTOMI4(vp); 10447 10448 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10449 return (EIO); 10450 10451 if (vp->v_flag & VNOMAP) 10452 return (ENOSYS); 10453 10454 if (off < 0 || (off + len) < 0) 10455 return (ENXIO); 10456 10457 if (vp->v_type != VREG) 10458 return (ENODEV); 10459 10460 /* 10461 * If the file is delegated to the client don't do anything. 10462 * If the file is not delegated, then validate the data cache. 10463 */ 10464 mutex_enter(&rp->r_statev4_lock); 10465 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10466 mutex_exit(&rp->r_statev4_lock); 10467 error = nfs4_validate_caches(vp, cr); 10468 if (error) 10469 return (error); 10470 } else { 10471 mutex_exit(&rp->r_statev4_lock); 10472 } 10473 10474 /* 10475 * Check to see if the vnode is currently marked as not cachable. 10476 * This means portions of the file are locked (through VOP_FRLOCK). 10477 * In this case the map request must be refused. We use 10478 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10479 * 10480 * Atomically increment r_inmap after acquiring r_rwlock. The 10481 * idea here is to acquire r_rwlock to block read/write and 10482 * not to protect r_inmap. r_inmap will inform nfs4_read/write() 10483 * that we are in nfs4_map(). Now, r_rwlock is acquired in order 10484 * and we can prevent the deadlock that would have occurred 10485 * when nfs4_addmap() would have acquired it out of order. 10486 * 10487 * Since we are not protecting r_inmap by any lock, we do not 10488 * hold any lock when we decrement it. We atomically decrement 10489 * r_inmap after we release r_lkserlock. 10490 */ 10491 10492 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp))) 10493 return (EINTR); 10494 atomic_inc_uint(&rp->r_inmap); 10495 nfs_rw_exit(&rp->r_rwlock); 10496 10497 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) { 10498 atomic_dec_uint(&rp->r_inmap); 10499 return (EINTR); 10500 } 10501 10502 10503 if (vp->v_flag & VNOCACHE) { 10504 error = EAGAIN; 10505 goto done; 10506 } 10507 10508 /* 10509 * Don't allow concurrent locks and mapping if mandatory locking is 10510 * enabled. 10511 */ 10512 if (flk_has_remote_locks(vp)) { 10513 struct vattr va; 10514 va.va_mask = AT_MODE; 10515 error = nfs4getattr(vp, &va, cr); 10516 if (error != 0) 10517 goto done; 10518 if (MANDLOCK(vp, va.va_mode)) { 10519 error = EAGAIN; 10520 goto done; 10521 } 10522 } 10523 10524 /* 10525 * It is possible that the rnode has a lost lock request that we 10526 * are still trying to recover, and that the request conflicts with 10527 * this map request. 10528 * 10529 * An alternative approach would be for nfs4_safemap() to consider 10530 * queued lock requests when deciding whether to set or clear 10531 * VNOCACHE. This would require the frlock code path to call 10532 * nfs4_safemap() after enqueing a lost request. 10533 */ 10534 if (nfs4_map_lost_lock_conflict(vp)) { 10535 error = EAGAIN; 10536 goto done; 10537 } 10538 10539 as_rangelock(as); 10540 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 10541 if (error != 0) { 10542 as_rangeunlock(as); 10543 goto done; 10544 } 10545 10546 if (vp->v_type == VREG) { 10547 /* 10548 * We need to retrieve the open stream 10549 */ 10550 nfs4_open_stream_t *osp = NULL; 10551 nfs4_open_owner_t *oop = NULL; 10552 10553 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10554 if (oop != NULL) { 10555 /* returns with 'os_sync_lock' held */ 10556 osp = find_open_stream(oop, rp); 10557 open_owner_rele(oop); 10558 } 10559 if (osp == NULL) { 10560 #ifdef DEBUG 10561 if (nfs4_force_open_before_mmap) { 10562 error = EIO; 10563 goto done; 10564 } 10565 #endif 10566 /* returns with 'os_sync_lock' held */ 10567 error = open_and_get_osp(vp, cr, &osp); 10568 if (osp == NULL) { 10569 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10570 "nfs4_map: we tried to OPEN the file " 10571 "but again no osp, so fail with EIO")); 10572 goto done; 10573 } 10574 } 10575 10576 if (osp->os_failed_reopen) { 10577 mutex_exit(&osp->os_sync_lock); 10578 open_stream_rele(osp, rp); 10579 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10580 "nfs4_map: os_failed_reopen set on " 10581 "osp %p, cr %p, rp %s", (void *)osp, 10582 (void *)cr, rnode4info(rp))); 10583 error = EIO; 10584 goto done; 10585 } 10586 mutex_exit(&osp->os_sync_lock); 10587 open_stream_rele(osp, rp); 10588 } 10589 10590 vn_a.vp = vp; 10591 vn_a.offset = off; 10592 vn_a.type = (flags & MAP_TYPE); 10593 vn_a.prot = (uchar_t)prot; 10594 vn_a.maxprot = (uchar_t)maxprot; 10595 vn_a.flags = (flags & ~MAP_TYPE); 10596 vn_a.cred = cr; 10597 vn_a.amp = NULL; 10598 vn_a.szc = 0; 10599 vn_a.lgrp_mem_policy_flags = 0; 10600 10601 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10602 as_rangeunlock(as); 10603 10604 done: 10605 nfs_rw_exit(&rp->r_lkserlock); 10606 atomic_dec_uint(&rp->r_inmap); 10607 return (error); 10608 } 10609 10610 /* 10611 * We're most likely dealing with a kernel module that likes to READ 10612 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10613 * officially OPEN the file to create the necessary client state 10614 * for bookkeeping of os_mmap_read/write counts. 10615 * 10616 * Since VOP_MAP only passes in a pointer to the vnode rather than 10617 * a double pointer, we can't handle the case where nfs4open_otw() 10618 * returns a different vnode than the one passed into VOP_MAP (since 10619 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10620 * we return NULL and let nfs4_map() fail. Note: the only case where 10621 * this should happen is if the file got removed and replaced with the 10622 * same name on the server (in addition to the fact that we're trying 10623 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10624 */ 10625 static int 10626 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10627 { 10628 rnode4_t *rp, *drp; 10629 vnode_t *dvp, *open_vp; 10630 char file_name[MAXNAMELEN]; 10631 int just_created; 10632 nfs4_open_stream_t *osp; 10633 nfs4_open_owner_t *oop; 10634 int error; 10635 10636 *ospp = NULL; 10637 open_vp = map_vp; 10638 10639 rp = VTOR4(open_vp); 10640 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10641 return (error); 10642 drp = VTOR4(dvp); 10643 10644 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10645 VN_RELE(dvp); 10646 return (EINTR); 10647 } 10648 10649 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10650 nfs_rw_exit(&drp->r_rwlock); 10651 VN_RELE(dvp); 10652 return (error); 10653 } 10654 10655 mutex_enter(&rp->r_statev4_lock); 10656 if (rp->created_v4) { 10657 rp->created_v4 = 0; 10658 mutex_exit(&rp->r_statev4_lock); 10659 10660 dnlc_update(dvp, file_name, open_vp); 10661 /* This is needed so we don't bump the open ref count */ 10662 just_created = 1; 10663 } else { 10664 mutex_exit(&rp->r_statev4_lock); 10665 just_created = 0; 10666 } 10667 10668 VN_HOLD(map_vp); 10669 10670 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10671 just_created); 10672 if (error) { 10673 nfs_rw_exit(&drp->r_rwlock); 10674 VN_RELE(dvp); 10675 VN_RELE(map_vp); 10676 return (error); 10677 } 10678 10679 nfs_rw_exit(&drp->r_rwlock); 10680 VN_RELE(dvp); 10681 10682 /* 10683 * If nfs4open_otw() returned a different vnode then "undo" 10684 * the open and return failure to the caller. 10685 */ 10686 if (!VN_CMP(open_vp, map_vp)) { 10687 nfs4_error_t e; 10688 10689 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10690 "open returned a different vnode")); 10691 /* 10692 * If there's an error, ignore it, 10693 * and let VOP_INACTIVE handle it. 10694 */ 10695 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10696 CLOSE_NORM, 0, 0, 0); 10697 VN_RELE(map_vp); 10698 return (EIO); 10699 } 10700 10701 VN_RELE(map_vp); 10702 10703 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10704 if (!oop) { 10705 nfs4_error_t e; 10706 10707 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10708 "no open owner")); 10709 /* 10710 * If there's an error, ignore it, 10711 * and let VOP_INACTIVE handle it. 10712 */ 10713 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10714 CLOSE_NORM, 0, 0, 0); 10715 return (EIO); 10716 } 10717 osp = find_open_stream(oop, rp); 10718 open_owner_rele(oop); 10719 *ospp = osp; 10720 return (0); 10721 } 10722 10723 /* 10724 * Please be aware that when this function is called, the address space write 10725 * a_lock is held. Do not put over the wire calls in this function. 10726 */ 10727 /* ARGSUSED */ 10728 static int 10729 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10730 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 10731 caller_context_t *ct) 10732 { 10733 rnode4_t *rp; 10734 int error = 0; 10735 mntinfo4_t *mi; 10736 10737 mi = VTOMI4(vp); 10738 rp = VTOR4(vp); 10739 10740 if (nfs_zone() != mi->mi_zone) 10741 return (EIO); 10742 if (vp->v_flag & VNOMAP) 10743 return (ENOSYS); 10744 10745 /* 10746 * Don't need to update the open stream first, since this 10747 * mmap can't add any additional share access that isn't 10748 * already contained in the open stream (for the case where we 10749 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10750 * take into account os_mmap_read[write] counts). 10751 */ 10752 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10753 10754 if (vp->v_type == VREG) { 10755 /* 10756 * We need to retrieve the open stream and update the counts. 10757 * If there is no open stream here, something is wrong. 10758 */ 10759 nfs4_open_stream_t *osp = NULL; 10760 nfs4_open_owner_t *oop = NULL; 10761 10762 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10763 if (oop != NULL) { 10764 /* returns with 'os_sync_lock' held */ 10765 osp = find_open_stream(oop, rp); 10766 open_owner_rele(oop); 10767 } 10768 if (osp == NULL) { 10769 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10770 "nfs4_addmap: we should have an osp" 10771 "but we don't, so fail with EIO")); 10772 error = EIO; 10773 goto out; 10774 } 10775 10776 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10777 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10778 10779 /* 10780 * Update the map count in the open stream. 10781 * This is necessary in the case where we 10782 * open/mmap/close/, then the server reboots, and we 10783 * attempt to reopen. If the mmap doesn't add share 10784 * access then we send an invalid reopen with 10785 * access = NONE. 10786 * 10787 * We need to specifically check each PROT_* so a mmap 10788 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10789 * read and write access. A simple comparison of prot 10790 * to ~PROT_WRITE to determine read access is insufficient 10791 * since prot can be |= with PROT_USER, etc. 10792 */ 10793 10794 /* 10795 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10796 */ 10797 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10798 osp->os_mmap_write += btopr(len); 10799 if (maxprot & PROT_READ) 10800 osp->os_mmap_read += btopr(len); 10801 if (maxprot & PROT_EXEC) 10802 osp->os_mmap_read += btopr(len); 10803 /* 10804 * Ensure that os_mmap_read gets incremented, even if 10805 * maxprot were to look like PROT_NONE. 10806 */ 10807 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10808 !(maxprot & PROT_EXEC)) 10809 osp->os_mmap_read += btopr(len); 10810 osp->os_mapcnt += btopr(len); 10811 mutex_exit(&osp->os_sync_lock); 10812 open_stream_rele(osp, rp); 10813 } 10814 10815 out: 10816 /* 10817 * If we got an error, then undo our 10818 * incrementing of 'r_mapcnt'. 10819 */ 10820 10821 if (error) { 10822 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10823 ASSERT(rp->r_mapcnt >= 0); 10824 } 10825 return (error); 10826 } 10827 10828 /* ARGSUSED */ 10829 static int 10830 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct) 10831 { 10832 10833 return (VTOR4(vp1) == VTOR4(vp2)); 10834 } 10835 10836 /* ARGSUSED */ 10837 static int 10838 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10839 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr, 10840 caller_context_t *ct) 10841 { 10842 int rc; 10843 u_offset_t start, end; 10844 rnode4_t *rp; 10845 int error = 0, intr = INTR4(vp); 10846 nfs4_error_t e; 10847 10848 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10849 return (EIO); 10850 10851 /* check for valid cmd parameter */ 10852 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10853 return (EINVAL); 10854 10855 /* Verify l_type. */ 10856 switch (bfp->l_type) { 10857 case F_RDLCK: 10858 if (cmd != F_GETLK && !(flag & FREAD)) 10859 return (EBADF); 10860 break; 10861 case F_WRLCK: 10862 if (cmd != F_GETLK && !(flag & FWRITE)) 10863 return (EBADF); 10864 break; 10865 case F_UNLCK: 10866 intr = 0; 10867 break; 10868 10869 default: 10870 return (EINVAL); 10871 } 10872 10873 /* check the validity of the lock range */ 10874 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10875 return (rc); 10876 if (rc = flk_check_lock_data(start, end, MAXEND)) 10877 return (rc); 10878 10879 /* 10880 * If the filesystem is mounted using local locking, pass the 10881 * request off to the local locking code. 10882 */ 10883 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10884 if (cmd == F_SETLK || cmd == F_SETLKW) { 10885 /* 10886 * For complete safety, we should be holding 10887 * r_lkserlock. However, we can't call 10888 * nfs4_safelock and then fs_frlock while 10889 * holding r_lkserlock, so just invoke 10890 * nfs4_safelock and expect that this will 10891 * catch enough of the cases. 10892 */ 10893 if (!nfs4_safelock(vp, bfp, cr)) 10894 return (EAGAIN); 10895 } 10896 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 10897 } 10898 10899 rp = VTOR4(vp); 10900 10901 /* 10902 * Check whether the given lock request can proceed, given the 10903 * current file mappings. 10904 */ 10905 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10906 return (EINTR); 10907 if (cmd == F_SETLK || cmd == F_SETLKW) { 10908 if (!nfs4_safelock(vp, bfp, cr)) { 10909 rc = EAGAIN; 10910 goto done; 10911 } 10912 } 10913 10914 /* 10915 * Flush the cache after waiting for async I/O to finish. For new 10916 * locks, this is so that the process gets the latest bits from the 10917 * server. For unlocks, this is so that other clients see the 10918 * latest bits once the file has been unlocked. If currently dirty 10919 * pages can't be flushed, then don't allow a lock to be set. But 10920 * allow unlocks to succeed, to avoid having orphan locks on the 10921 * server. 10922 */ 10923 if (cmd != F_GETLK) { 10924 mutex_enter(&rp->r_statelock); 10925 while (rp->r_count > 0) { 10926 if (intr) { 10927 klwp_t *lwp = ttolwp(curthread); 10928 10929 if (lwp != NULL) 10930 lwp->lwp_nostop++; 10931 if (cv_wait_sig(&rp->r_cv, 10932 &rp->r_statelock) == 0) { 10933 if (lwp != NULL) 10934 lwp->lwp_nostop--; 10935 rc = EINTR; 10936 break; 10937 } 10938 if (lwp != NULL) 10939 lwp->lwp_nostop--; 10940 } else 10941 cv_wait(&rp->r_cv, &rp->r_statelock); 10942 } 10943 mutex_exit(&rp->r_statelock); 10944 if (rc != 0) 10945 goto done; 10946 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct); 10947 if (error) { 10948 if (error == ENOSPC || error == EDQUOT) { 10949 mutex_enter(&rp->r_statelock); 10950 if (!rp->r_error) 10951 rp->r_error = error; 10952 mutex_exit(&rp->r_statelock); 10953 } 10954 if (bfp->l_type != F_UNLCK) { 10955 rc = ENOLCK; 10956 goto done; 10957 } 10958 } 10959 } 10960 10961 /* 10962 * Call the lock manager to do the real work of contacting 10963 * the server and obtaining the lock. 10964 */ 10965 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10966 cr, &e, NULL, NULL); 10967 rc = e.error; 10968 10969 if (rc == 0) 10970 nfs4_lockcompletion(vp, cmd); 10971 10972 done: 10973 nfs_rw_exit(&rp->r_lkserlock); 10974 10975 return (rc); 10976 } 10977 10978 /* 10979 * Free storage space associated with the specified vnode. The portion 10980 * to be freed is specified by bfp->l_start and bfp->l_len (already 10981 * normalized to a "whence" of 0). 10982 * 10983 * This is an experimental facility whose continued existence is not 10984 * guaranteed. Currently, we only support the special case 10985 * of l_len == 0, meaning free to end of file. 10986 */ 10987 /* ARGSUSED */ 10988 static int 10989 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10990 offset_t offset, cred_t *cr, caller_context_t *ct) 10991 { 10992 int error; 10993 10994 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10995 return (EIO); 10996 ASSERT(vp->v_type == VREG); 10997 if (cmd != F_FREESP) 10998 return (EINVAL); 10999 11000 error = convoff(vp, bfp, 0, offset); 11001 if (!error) { 11002 ASSERT(bfp->l_start >= 0); 11003 if (bfp->l_len == 0) { 11004 struct vattr va; 11005 11006 va.va_mask = AT_SIZE; 11007 va.va_size = bfp->l_start; 11008 error = nfs4setattr(vp, &va, 0, cr, NULL); 11009 11010 if (error == 0 && bfp->l_start == 0) 11011 vnevent_truncate(vp, ct); 11012 } else 11013 error = EINVAL; 11014 } 11015 11016 return (error); 11017 } 11018 11019 /* ARGSUSED */ 11020 int 11021 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct) 11022 { 11023 rnode4_t *rp; 11024 rp = VTOR4(vp); 11025 11026 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 11027 vp = RTOV4(rp); 11028 } 11029 *vpp = vp; 11030 return (0); 11031 } 11032 11033 /* 11034 * Setup and add an address space callback to do the work of the delmap call. 11035 * The callback will (and must be) deleted in the actual callback function. 11036 * 11037 * This is done in order to take care of the problem that we have with holding 11038 * the address space's a_lock for a long period of time (e.g. if the NFS server 11039 * is down). Callbacks will be executed in the address space code while the 11040 * a_lock is not held. Holding the address space's a_lock causes things such 11041 * as ps and fork to hang because they are trying to acquire this lock as well. 11042 */ 11043 /* ARGSUSED */ 11044 static int 11045 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 11046 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 11047 caller_context_t *ct) 11048 { 11049 int caller_found; 11050 int error; 11051 rnode4_t *rp; 11052 nfs4_delmap_args_t *dmapp; 11053 nfs4_delmapcall_t *delmap_call; 11054 11055 if (vp->v_flag & VNOMAP) 11056 return (ENOSYS); 11057 11058 /* 11059 * A process may not change zones if it has NFS pages mmap'ed 11060 * in, so we can't legitimately get here from the wrong zone. 11061 */ 11062 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11063 11064 rp = VTOR4(vp); 11065 11066 /* 11067 * The way that the address space of this process deletes its mapping 11068 * of this file is via the following call chains: 11069 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11070 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 11071 * 11072 * With the use of address space callbacks we are allowed to drop the 11073 * address space lock, a_lock, while executing the NFS operations that 11074 * need to go over the wire. Returning EAGAIN to the caller of this 11075 * function is what drives the execution of the callback that we add 11076 * below. The callback will be executed by the address space code 11077 * after dropping the a_lock. When the callback is finished, since 11078 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 11079 * is called again on the same segment to finish the rest of the work 11080 * that needs to happen during unmapping. 11081 * 11082 * This action of calling back into the segment driver causes 11083 * nfs4_delmap() to get called again, but since the callback was 11084 * already executed at this point, it already did the work and there 11085 * is nothing left for us to do. 11086 * 11087 * To Summarize: 11088 * - The first time nfs4_delmap is called by the current thread is when 11089 * we add the caller associated with this delmap to the delmap caller 11090 * list, add the callback, and return EAGAIN. 11091 * - The second time in this call chain when nfs4_delmap is called we 11092 * will find this caller in the delmap caller list and realize there 11093 * is no more work to do thus removing this caller from the list and 11094 * returning the error that was set in the callback execution. 11095 */ 11096 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 11097 if (caller_found) { 11098 /* 11099 * 'error' is from the actual delmap operations. To avoid 11100 * hangs, we need to handle the return of EAGAIN differently 11101 * since this is what drives the callback execution. 11102 * In this case, we don't want to return EAGAIN and do the 11103 * callback execution because there are none to execute. 11104 */ 11105 if (error == EAGAIN) 11106 return (0); 11107 else 11108 return (error); 11109 } 11110 11111 /* current caller was not in the list */ 11112 delmap_call = nfs4_init_delmapcall(); 11113 11114 mutex_enter(&rp->r_statelock); 11115 list_insert_tail(&rp->r_indelmap, delmap_call); 11116 mutex_exit(&rp->r_statelock); 11117 11118 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 11119 11120 dmapp->vp = vp; 11121 dmapp->off = off; 11122 dmapp->addr = addr; 11123 dmapp->len = len; 11124 dmapp->prot = prot; 11125 dmapp->maxprot = maxprot; 11126 dmapp->flags = flags; 11127 dmapp->cr = cr; 11128 dmapp->caller = delmap_call; 11129 11130 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 11131 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 11132 11133 return (error ? error : EAGAIN); 11134 } 11135 11136 static nfs4_delmapcall_t * 11137 nfs4_init_delmapcall() 11138 { 11139 nfs4_delmapcall_t *delmap_call; 11140 11141 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 11142 delmap_call->call_id = curthread; 11143 delmap_call->error = 0; 11144 11145 return (delmap_call); 11146 } 11147 11148 static void 11149 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 11150 { 11151 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 11152 } 11153 11154 /* 11155 * Searches for the current delmap caller (based on curthread) in the list of 11156 * callers. If it is found, we remove it and free the delmap caller. 11157 * Returns: 11158 * 0 if the caller wasn't found 11159 * 1 if the caller was found, removed and freed. *errp will be set 11160 * to what the result of the delmap was. 11161 */ 11162 static int 11163 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 11164 { 11165 nfs4_delmapcall_t *delmap_call; 11166 11167 /* 11168 * If the list doesn't exist yet, we create it and return 11169 * that the caller wasn't found. No list = no callers. 11170 */ 11171 mutex_enter(&rp->r_statelock); 11172 if (!(rp->r_flags & R4DELMAPLIST)) { 11173 /* The list does not exist */ 11174 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 11175 offsetof(nfs4_delmapcall_t, call_node)); 11176 rp->r_flags |= R4DELMAPLIST; 11177 mutex_exit(&rp->r_statelock); 11178 return (0); 11179 } else { 11180 /* The list exists so search it */ 11181 for (delmap_call = list_head(&rp->r_indelmap); 11182 delmap_call != NULL; 11183 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 11184 if (delmap_call->call_id == curthread) { 11185 /* current caller is in the list */ 11186 *errp = delmap_call->error; 11187 list_remove(&rp->r_indelmap, delmap_call); 11188 mutex_exit(&rp->r_statelock); 11189 nfs4_free_delmapcall(delmap_call); 11190 return (1); 11191 } 11192 } 11193 } 11194 mutex_exit(&rp->r_statelock); 11195 return (0); 11196 } 11197 11198 /* 11199 * Remove some pages from an mmap'd vnode. Just update the 11200 * count of pages. If doing close-to-open, then flush and 11201 * commit all of the pages associated with this file. 11202 * Otherwise, start an asynchronous page flush to write out 11203 * any dirty pages. This will also associate a credential 11204 * with the rnode which can be used to write the pages. 11205 */ 11206 /* ARGSUSED */ 11207 static void 11208 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11209 { 11210 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11211 rnode4_t *rp; 11212 mntinfo4_t *mi; 11213 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11214 11215 rp = VTOR4(dmapp->vp); 11216 mi = VTOMI4(dmapp->vp); 11217 11218 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11219 ASSERT(rp->r_mapcnt >= 0); 11220 11221 /* 11222 * Initiate a page flush and potential commit if there are 11223 * pages, the file system was not mounted readonly, the segment 11224 * was mapped shared, and the pages themselves were writeable. 11225 */ 11226 if (nfs4_has_pages(dmapp->vp) && 11227 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11228 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11229 mutex_enter(&rp->r_statelock); 11230 rp->r_flags |= R4DIRTY; 11231 mutex_exit(&rp->r_statelock); 11232 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11233 dmapp->len, dmapp->cr); 11234 if (!e.error) { 11235 mutex_enter(&rp->r_statelock); 11236 e.error = rp->r_error; 11237 rp->r_error = 0; 11238 mutex_exit(&rp->r_statelock); 11239 } 11240 } else 11241 e.error = 0; 11242 11243 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11244 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11245 B_INVAL, dmapp->cr, NULL); 11246 11247 if (e.error) { 11248 e.stat = puterrno4(e.error); 11249 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11250 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11251 dmapp->caller->error = e.error; 11252 } 11253 11254 /* Check to see if we need to close the file */ 11255 11256 if (dmapp->vp->v_type == VREG) { 11257 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11258 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11259 11260 if (e.error != 0 || e.stat != NFS4_OK) { 11261 /* 11262 * Since it is possible that e.error == 0 and 11263 * e.stat != NFS4_OK (and vice versa), 11264 * we do the proper checking in order to get both 11265 * e.error and e.stat reporting the correct info. 11266 */ 11267 if (e.stat == NFS4_OK) 11268 e.stat = puterrno4(e.error); 11269 if (e.error == 0) 11270 e.error = geterrno4(e.stat); 11271 11272 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11273 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11274 dmapp->caller->error = e.error; 11275 } 11276 } 11277 11278 (void) as_delete_callback(as, arg); 11279 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11280 } 11281 11282 11283 static uint_t 11284 fattr4_maxfilesize_to_bits(uint64_t ll) 11285 { 11286 uint_t l = 1; 11287 11288 if (ll == 0) { 11289 return (0); 11290 } 11291 11292 if (ll & 0xffffffff00000000) { 11293 l += 32; ll >>= 32; 11294 } 11295 if (ll & 0xffff0000) { 11296 l += 16; ll >>= 16; 11297 } 11298 if (ll & 0xff00) { 11299 l += 8; ll >>= 8; 11300 } 11301 if (ll & 0xf0) { 11302 l += 4; ll >>= 4; 11303 } 11304 if (ll & 0xc) { 11305 l += 2; ll >>= 2; 11306 } 11307 if (ll & 0x2) { 11308 l += 1; 11309 } 11310 return (l); 11311 } 11312 11313 static int 11314 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr) 11315 { 11316 vnode_t *avp = NULL; 11317 int error; 11318 11319 if ((error = nfs4lookup_xattr(vp, "", &avp, 11320 LOOKUP_XATTR, cr)) == 0) 11321 error = do_xattr_exists_check(avp, valp, cr); 11322 if (avp) 11323 VN_RELE(avp); 11324 11325 return (error); 11326 } 11327 11328 /* ARGSUSED */ 11329 int 11330 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 11331 caller_context_t *ct) 11332 { 11333 int error; 11334 hrtime_t t; 11335 rnode4_t *rp; 11336 nfs4_ga_res_t gar; 11337 nfs4_ga_ext_res_t ger; 11338 11339 gar.n4g_ext_res = &ger; 11340 11341 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11342 return (EIO); 11343 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11344 *valp = MAXPATHLEN; 11345 return (0); 11346 } 11347 if (cmd == _PC_ACL_ENABLED) { 11348 *valp = _ACL_ACE_ENABLED; 11349 return (0); 11350 } 11351 11352 rp = VTOR4(vp); 11353 if (cmd == _PC_XATTR_EXISTS) { 11354 /* 11355 * The existence of the xattr directory is not sufficient 11356 * for determining whether generic user attributes exists. 11357 * The attribute directory could only be a transient directory 11358 * used for Solaris sysattr support. Do a small readdir 11359 * to verify if the only entries are sysattrs or not. 11360 * 11361 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11362 * is NULL. Once the xadir vp exists, we can create xattrs, 11363 * and we don't have any way to update the "base" object's 11364 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11365 * could help out. 11366 */ 11367 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11368 rp->r_xattr_dir == NULL) { 11369 return (nfs4_have_xattrs(vp, valp, cr)); 11370 } 11371 } else { /* OLD CODE */ 11372 if (ATTRCACHE4_VALID(vp)) { 11373 mutex_enter(&rp->r_statelock); 11374 if (rp->r_pathconf.pc4_cache_valid) { 11375 error = 0; 11376 switch (cmd) { 11377 case _PC_FILESIZEBITS: 11378 *valp = 11379 rp->r_pathconf.pc4_filesizebits; 11380 break; 11381 case _PC_LINK_MAX: 11382 *valp = 11383 rp->r_pathconf.pc4_link_max; 11384 break; 11385 case _PC_NAME_MAX: 11386 *valp = 11387 rp->r_pathconf.pc4_name_max; 11388 break; 11389 case _PC_CHOWN_RESTRICTED: 11390 *valp = 11391 rp->r_pathconf.pc4_chown_restricted; 11392 break; 11393 case _PC_NO_TRUNC: 11394 *valp = 11395 rp->r_pathconf.pc4_no_trunc; 11396 break; 11397 default: 11398 error = EINVAL; 11399 break; 11400 } 11401 mutex_exit(&rp->r_statelock); 11402 #ifdef DEBUG 11403 nfs4_pathconf_cache_hits++; 11404 #endif 11405 return (error); 11406 } 11407 mutex_exit(&rp->r_statelock); 11408 } 11409 } 11410 #ifdef DEBUG 11411 nfs4_pathconf_cache_misses++; 11412 #endif 11413 11414 t = gethrtime(); 11415 11416 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11417 11418 if (error) { 11419 mutex_enter(&rp->r_statelock); 11420 rp->r_pathconf.pc4_cache_valid = FALSE; 11421 rp->r_pathconf.pc4_xattr_valid = FALSE; 11422 mutex_exit(&rp->r_statelock); 11423 return (error); 11424 } 11425 11426 /* interpret the max filesize */ 11427 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11428 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11429 11430 /* Store the attributes we just received */ 11431 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11432 11433 switch (cmd) { 11434 case _PC_FILESIZEBITS: 11435 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11436 break; 11437 case _PC_LINK_MAX: 11438 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11439 break; 11440 case _PC_NAME_MAX: 11441 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11442 break; 11443 case _PC_CHOWN_RESTRICTED: 11444 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11445 break; 11446 case _PC_NO_TRUNC: 11447 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11448 break; 11449 case _PC_XATTR_EXISTS: 11450 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) { 11451 if (error = nfs4_have_xattrs(vp, valp, cr)) 11452 return (error); 11453 } 11454 break; 11455 default: 11456 return (EINVAL); 11457 } 11458 11459 return (0); 11460 } 11461 11462 /* 11463 * Called by async thread to do synchronous pageio. Do the i/o, wait 11464 * for it to complete, and cleanup the page list when done. 11465 */ 11466 static int 11467 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11468 int flags, cred_t *cr) 11469 { 11470 int error; 11471 11472 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11473 11474 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11475 if (flags & B_READ) 11476 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11477 else 11478 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11479 return (error); 11480 } 11481 11482 /* ARGSUSED */ 11483 static int 11484 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11485 int flags, cred_t *cr, caller_context_t *ct) 11486 { 11487 int error; 11488 rnode4_t *rp; 11489 11490 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11491 return (EIO); 11492 11493 if (pp == NULL) 11494 return (EINVAL); 11495 11496 rp = VTOR4(vp); 11497 mutex_enter(&rp->r_statelock); 11498 rp->r_count++; 11499 mutex_exit(&rp->r_statelock); 11500 11501 if (flags & B_ASYNC) { 11502 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11503 nfs4_sync_pageio); 11504 } else 11505 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11506 mutex_enter(&rp->r_statelock); 11507 rp->r_count--; 11508 cv_broadcast(&rp->r_cv); 11509 mutex_exit(&rp->r_statelock); 11510 return (error); 11511 } 11512 11513 /* ARGSUSED */ 11514 static void 11515 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr, 11516 caller_context_t *ct) 11517 { 11518 int error; 11519 rnode4_t *rp; 11520 page_t *plist; 11521 page_t *pptr; 11522 offset3 offset; 11523 count3 len; 11524 k_sigset_t smask; 11525 11526 /* 11527 * We should get called with fl equal to either B_FREE or 11528 * B_INVAL. Any other value is illegal. 11529 * 11530 * The page that we are either supposed to free or destroy 11531 * should be exclusive locked and its io lock should not 11532 * be held. 11533 */ 11534 ASSERT(fl == B_FREE || fl == B_INVAL); 11535 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11536 11537 rp = VTOR4(vp); 11538 11539 /* 11540 * If the page doesn't need to be committed or we shouldn't 11541 * even bother attempting to commit it, then just make sure 11542 * that the p_fsdata byte is clear and then either free or 11543 * destroy the page as appropriate. 11544 */ 11545 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11546 pp->p_fsdata = C_NOCOMMIT; 11547 if (fl == B_FREE) 11548 page_free(pp, dn); 11549 else 11550 page_destroy(pp, dn); 11551 return; 11552 } 11553 11554 /* 11555 * If there is a page invalidation operation going on, then 11556 * if this is one of the pages being destroyed, then just 11557 * clear the p_fsdata byte and then either free or destroy 11558 * the page as appropriate. 11559 */ 11560 mutex_enter(&rp->r_statelock); 11561 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11562 mutex_exit(&rp->r_statelock); 11563 pp->p_fsdata = C_NOCOMMIT; 11564 if (fl == B_FREE) 11565 page_free(pp, dn); 11566 else 11567 page_destroy(pp, dn); 11568 return; 11569 } 11570 11571 /* 11572 * If we are freeing this page and someone else is already 11573 * waiting to do a commit, then just unlock the page and 11574 * return. That other thread will take care of commiting 11575 * this page. The page can be freed sometime after the 11576 * commit has finished. Otherwise, if the page is marked 11577 * as delay commit, then we may be getting called from 11578 * pvn_write_done, one page at a time. This could result 11579 * in one commit per page, so we end up doing lots of small 11580 * commits instead of fewer larger commits. This is bad, 11581 * we want do as few commits as possible. 11582 */ 11583 if (fl == B_FREE) { 11584 if (rp->r_flags & R4COMMITWAIT) { 11585 page_unlock(pp); 11586 mutex_exit(&rp->r_statelock); 11587 return; 11588 } 11589 if (pp->p_fsdata == C_DELAYCOMMIT) { 11590 pp->p_fsdata = C_COMMIT; 11591 page_unlock(pp); 11592 mutex_exit(&rp->r_statelock); 11593 return; 11594 } 11595 } 11596 11597 /* 11598 * Check to see if there is a signal which would prevent an 11599 * attempt to commit the pages from being successful. If so, 11600 * then don't bother with all of the work to gather pages and 11601 * generate the unsuccessful RPC. Just return from here and 11602 * let the page be committed at some later time. 11603 */ 11604 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11605 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11606 sigunintr(&smask); 11607 page_unlock(pp); 11608 mutex_exit(&rp->r_statelock); 11609 return; 11610 } 11611 sigunintr(&smask); 11612 11613 /* 11614 * We are starting to need to commit pages, so let's try 11615 * to commit as many as possible at once to reduce the 11616 * overhead. 11617 * 11618 * Set the `commit inprogress' state bit. We must 11619 * first wait until any current one finishes. Then 11620 * we initialize the c_pages list with this page. 11621 */ 11622 while (rp->r_flags & R4COMMIT) { 11623 rp->r_flags |= R4COMMITWAIT; 11624 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11625 rp->r_flags &= ~R4COMMITWAIT; 11626 } 11627 rp->r_flags |= R4COMMIT; 11628 mutex_exit(&rp->r_statelock); 11629 ASSERT(rp->r_commit.c_pages == NULL); 11630 rp->r_commit.c_pages = pp; 11631 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11632 rp->r_commit.c_commlen = PAGESIZE; 11633 11634 /* 11635 * Gather together all other pages which can be committed. 11636 * They will all be chained off r_commit.c_pages. 11637 */ 11638 nfs4_get_commit(vp); 11639 11640 /* 11641 * Clear the `commit inprogress' status and disconnect 11642 * the list of pages to be committed from the rnode. 11643 * At this same time, we also save the starting offset 11644 * and length of data to be committed on the server. 11645 */ 11646 plist = rp->r_commit.c_pages; 11647 rp->r_commit.c_pages = NULL; 11648 offset = rp->r_commit.c_commbase; 11649 len = rp->r_commit.c_commlen; 11650 mutex_enter(&rp->r_statelock); 11651 rp->r_flags &= ~R4COMMIT; 11652 cv_broadcast(&rp->r_commit.c_cv); 11653 mutex_exit(&rp->r_statelock); 11654 11655 if (curproc == proc_pageout || curproc == proc_fsflush || 11656 nfs_zone() != VTOMI4(vp)->mi_zone) { 11657 nfs4_async_commit(vp, plist, offset, len, 11658 cr, do_nfs4_async_commit); 11659 return; 11660 } 11661 11662 /* 11663 * Actually generate the COMMIT op over the wire operation. 11664 */ 11665 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11666 11667 /* 11668 * If we got an error during the commit, just unlock all 11669 * of the pages. The pages will get retransmitted to the 11670 * server during a putpage operation. 11671 */ 11672 if (error) { 11673 while (plist != NULL) { 11674 pptr = plist; 11675 page_sub(&plist, pptr); 11676 page_unlock(pptr); 11677 } 11678 return; 11679 } 11680 11681 /* 11682 * We've tried as hard as we can to commit the data to stable 11683 * storage on the server. We just unlock the rest of the pages 11684 * and clear the commit required state. They will be put 11685 * onto the tail of the cachelist if they are nolonger 11686 * mapped. 11687 */ 11688 while (plist != pp) { 11689 pptr = plist; 11690 page_sub(&plist, pptr); 11691 pptr->p_fsdata = C_NOCOMMIT; 11692 page_unlock(pptr); 11693 } 11694 11695 /* 11696 * It is possible that nfs4_commit didn't return error but 11697 * some other thread has modified the page we are going 11698 * to free/destroy. 11699 * In this case we need to rewrite the page. Do an explicit check 11700 * before attempting to free/destroy the page. If modified, needs to 11701 * be rewritten so unlock the page and return. 11702 */ 11703 if (hat_ismod(pp)) { 11704 pp->p_fsdata = C_NOCOMMIT; 11705 page_unlock(pp); 11706 return; 11707 } 11708 11709 /* 11710 * Now, as appropriate, either free or destroy the page 11711 * that we were called with. 11712 */ 11713 pp->p_fsdata = C_NOCOMMIT; 11714 if (fl == B_FREE) 11715 page_free(pp, dn); 11716 else 11717 page_destroy(pp, dn); 11718 } 11719 11720 /* 11721 * Commit requires that the current fh be the file written to. 11722 * The compound op structure is: 11723 * PUTFH(file), COMMIT 11724 */ 11725 static int 11726 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11727 { 11728 COMPOUND4args_clnt args; 11729 COMPOUND4res_clnt res; 11730 COMMIT4res *cm_res; 11731 nfs_argop4 argop[2]; 11732 nfs_resop4 *resop; 11733 int doqueue; 11734 mntinfo4_t *mi; 11735 rnode4_t *rp; 11736 cred_t *cred_otw = NULL; 11737 bool_t needrecov = FALSE; 11738 nfs4_recov_state_t recov_state; 11739 nfs4_open_stream_t *osp = NULL; 11740 bool_t first_time = TRUE; /* first time getting OTW cred */ 11741 bool_t last_time = FALSE; /* last time getting OTW cred */ 11742 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11743 11744 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11745 11746 rp = VTOR4(vp); 11747 11748 mi = VTOMI4(vp); 11749 recov_state.rs_flags = 0; 11750 recov_state.rs_num_retry_despite_err = 0; 11751 get_commit_cred: 11752 /* 11753 * Releases the osp, if a valid open stream is provided. 11754 * Puts a hold on the cred_otw and the new osp (if found). 11755 */ 11756 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11757 &first_time, &last_time); 11758 args.ctag = TAG_COMMIT; 11759 recov_retry: 11760 /* 11761 * Commit ops: putfh file; commit 11762 */ 11763 args.array_len = 2; 11764 args.array = argop; 11765 11766 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11767 &recov_state, NULL); 11768 if (e.error) { 11769 crfree(cred_otw); 11770 if (osp != NULL) 11771 open_stream_rele(osp, rp); 11772 return (e.error); 11773 } 11774 11775 /* putfh directory */ 11776 argop[0].argop = OP_CPUTFH; 11777 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11778 11779 /* commit */ 11780 argop[1].argop = OP_COMMIT; 11781 argop[1].nfs_argop4_u.opcommit.offset = offset; 11782 argop[1].nfs_argop4_u.opcommit.count = count; 11783 11784 doqueue = 1; 11785 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11786 11787 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11788 if (!needrecov && e.error) { 11789 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11790 needrecov); 11791 crfree(cred_otw); 11792 if (e.error == EACCES && last_time == FALSE) 11793 goto get_commit_cred; 11794 if (osp != NULL) 11795 open_stream_rele(osp, rp); 11796 return (e.error); 11797 } 11798 11799 if (needrecov) { 11800 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11801 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) { 11802 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11803 &recov_state, needrecov); 11804 if (!e.error) 11805 (void) xdr_free(xdr_COMPOUND4res_clnt, 11806 (caddr_t)&res); 11807 goto recov_retry; 11808 } 11809 if (e.error) { 11810 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11811 &recov_state, needrecov); 11812 crfree(cred_otw); 11813 if (osp != NULL) 11814 open_stream_rele(osp, rp); 11815 return (e.error); 11816 } 11817 /* fall through for res.status case */ 11818 } 11819 11820 if (res.status) { 11821 e.error = geterrno4(res.status); 11822 if (e.error == EACCES && last_time == FALSE) { 11823 crfree(cred_otw); 11824 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11825 &recov_state, needrecov); 11826 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11827 goto get_commit_cred; 11828 } 11829 /* 11830 * Can't do a nfs4_purge_stale_fh here because this 11831 * can cause a deadlock. nfs4_commit can 11832 * be called from nfs4_dispose which can be called 11833 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11834 * can call back to pvn_vplist_dirty. 11835 */ 11836 if (e.error == ESTALE) { 11837 mutex_enter(&rp->r_statelock); 11838 rp->r_flags |= R4STALE; 11839 if (!rp->r_error) 11840 rp->r_error = e.error; 11841 mutex_exit(&rp->r_statelock); 11842 PURGE_ATTRCACHE4(vp); 11843 } else { 11844 mutex_enter(&rp->r_statelock); 11845 if (!rp->r_error) 11846 rp->r_error = e.error; 11847 mutex_exit(&rp->r_statelock); 11848 } 11849 } else { 11850 ASSERT(rp->r_flags & R4HAVEVERF); 11851 resop = &res.array[1]; /* commit res */ 11852 cm_res = &resop->nfs_resop4_u.opcommit; 11853 mutex_enter(&rp->r_statelock); 11854 if (cm_res->writeverf == rp->r_writeverf) { 11855 mutex_exit(&rp->r_statelock); 11856 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11857 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11858 &recov_state, needrecov); 11859 crfree(cred_otw); 11860 if (osp != NULL) 11861 open_stream_rele(osp, rp); 11862 return (0); 11863 } 11864 nfs4_set_mod(vp); 11865 rp->r_writeverf = cm_res->writeverf; 11866 mutex_exit(&rp->r_statelock); 11867 e.error = NFS_VERF_MISMATCH; 11868 } 11869 11870 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11871 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11872 crfree(cred_otw); 11873 if (osp != NULL) 11874 open_stream_rele(osp, rp); 11875 11876 return (e.error); 11877 } 11878 11879 static void 11880 nfs4_set_mod(vnode_t *vp) 11881 { 11882 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11883 11884 /* make sure we're looking at the master vnode, not a shadow */ 11885 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check); 11886 } 11887 11888 /* 11889 * This function is used to gather a page list of the pages which 11890 * can be committed on the server. 11891 * 11892 * The calling thread must have set R4COMMIT. This bit is used to 11893 * serialize access to the commit structure in the rnode. As long 11894 * as the thread has set R4COMMIT, then it can manipulate the commit 11895 * structure without requiring any other locks. 11896 * 11897 * When this function is called from nfs4_dispose() the page passed 11898 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11899 * will skip it. This is not a problem since we initially add the 11900 * page to the r_commit page list. 11901 * 11902 */ 11903 static void 11904 nfs4_get_commit(vnode_t *vp) 11905 { 11906 rnode4_t *rp; 11907 page_t *pp; 11908 kmutex_t *vphm; 11909 11910 rp = VTOR4(vp); 11911 11912 ASSERT(rp->r_flags & R4COMMIT); 11913 11914 /* make sure we're looking at the master vnode, not a shadow */ 11915 11916 if (IS_SHADOW(vp, rp)) 11917 vp = RTOV4(rp); 11918 11919 vphm = page_vnode_mutex(vp); 11920 mutex_enter(vphm); 11921 11922 /* 11923 * If there are no pages associated with this vnode, then 11924 * just return. 11925 */ 11926 if ((pp = vp->v_pages) == NULL) { 11927 mutex_exit(vphm); 11928 return; 11929 } 11930 11931 /* 11932 * Step through all of the pages associated with this vnode 11933 * looking for pages which need to be committed. 11934 */ 11935 do { 11936 /* Skip marker pages. */ 11937 if (pp->p_hash == PVN_VPLIST_HASH_TAG) 11938 continue; 11939 11940 /* 11941 * First short-cut everything (without the page_lock) 11942 * and see if this page does not need to be committed 11943 * or is modified if so then we'll just skip it. 11944 */ 11945 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11946 continue; 11947 11948 /* 11949 * Attempt to lock the page. If we can't, then 11950 * someone else is messing with it or we have been 11951 * called from nfs4_dispose and this is the page that 11952 * nfs4_dispose was called with.. anyway just skip it. 11953 */ 11954 if (!page_trylock(pp, SE_EXCL)) 11955 continue; 11956 11957 /* 11958 * Lets check again now that we have the page lock. 11959 */ 11960 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11961 page_unlock(pp); 11962 continue; 11963 } 11964 11965 /* this had better not be a free page */ 11966 ASSERT(PP_ISFREE(pp) == 0); 11967 11968 /* 11969 * The page needs to be committed and we locked it. 11970 * Update the base and length parameters and add it 11971 * to r_pages. 11972 */ 11973 if (rp->r_commit.c_pages == NULL) { 11974 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11975 rp->r_commit.c_commlen = PAGESIZE; 11976 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11977 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11978 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11979 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11980 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11981 <= pp->p_offset) { 11982 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11983 rp->r_commit.c_commbase + PAGESIZE; 11984 } 11985 page_add(&rp->r_commit.c_pages, pp); 11986 } while ((pp = pp->p_vpnext) != vp->v_pages); 11987 11988 mutex_exit(vphm); 11989 } 11990 11991 /* 11992 * This routine is used to gather together a page list of the pages 11993 * which are to be committed on the server. This routine must not 11994 * be called if the calling thread holds any locked pages. 11995 * 11996 * The calling thread must have set R4COMMIT. This bit is used to 11997 * serialize access to the commit structure in the rnode. As long 11998 * as the thread has set R4COMMIT, then it can manipulate the commit 11999 * structure without requiring any other locks. 12000 */ 12001 static void 12002 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 12003 { 12004 12005 rnode4_t *rp; 12006 page_t *pp; 12007 u_offset_t end; 12008 u_offset_t off; 12009 ASSERT(len != 0); 12010 rp = VTOR4(vp); 12011 ASSERT(rp->r_flags & R4COMMIT); 12012 12013 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12014 12015 /* make sure we're looking at the master vnode, not a shadow */ 12016 12017 if (IS_SHADOW(vp, rp)) 12018 vp = RTOV4(rp); 12019 12020 /* 12021 * If there are no pages associated with this vnode, then 12022 * just return. 12023 */ 12024 if ((pp = vp->v_pages) == NULL) 12025 return; 12026 /* 12027 * Calculate the ending offset. 12028 */ 12029 end = soff + len; 12030 for (off = soff; off < end; off += PAGESIZE) { 12031 /* 12032 * Lookup each page by vp, offset. 12033 */ 12034 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 12035 continue; 12036 /* 12037 * If this page does not need to be committed or is 12038 * modified, then just skip it. 12039 */ 12040 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 12041 page_unlock(pp); 12042 continue; 12043 } 12044 12045 ASSERT(PP_ISFREE(pp) == 0); 12046 /* 12047 * The page needs to be committed and we locked it. 12048 * Update the base and length parameters and add it 12049 * to r_pages. 12050 */ 12051 if (rp->r_commit.c_pages == NULL) { 12052 rp->r_commit.c_commbase = (offset3)pp->p_offset; 12053 rp->r_commit.c_commlen = PAGESIZE; 12054 } else { 12055 rp->r_commit.c_commlen = (offset3)pp->p_offset - 12056 rp->r_commit.c_commbase + PAGESIZE; 12057 } 12058 page_add(&rp->r_commit.c_pages, pp); 12059 } 12060 } 12061 12062 /* 12063 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 12064 * Flushes and commits data to the server. 12065 */ 12066 static int 12067 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 12068 { 12069 int error; 12070 verifier4 write_verf; 12071 rnode4_t *rp = VTOR4(vp); 12072 12073 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12074 12075 /* 12076 * Flush the data portion of the file and then commit any 12077 * portions which need to be committed. This may need to 12078 * be done twice if the server has changed state since 12079 * data was last written. The data will need to be 12080 * rewritten to the server and then a new commit done. 12081 * 12082 * In fact, this may need to be done several times if the 12083 * server is having problems and crashing while we are 12084 * attempting to do this. 12085 */ 12086 12087 top: 12088 /* 12089 * Do a flush based on the poff and plen arguments. This 12090 * will synchronously write out any modified pages in the 12091 * range specified by (poff, plen). This starts all of the 12092 * i/o operations which will be waited for in the next 12093 * call to nfs4_putpage 12094 */ 12095 12096 mutex_enter(&rp->r_statelock); 12097 write_verf = rp->r_writeverf; 12098 mutex_exit(&rp->r_statelock); 12099 12100 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL); 12101 if (error == EAGAIN) 12102 error = 0; 12103 12104 /* 12105 * Do a flush based on the poff and plen arguments. This 12106 * will synchronously write out any modified pages in the 12107 * range specified by (poff, plen) and wait until all of 12108 * the asynchronous i/o's in that range are done as well. 12109 */ 12110 if (!error) 12111 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL); 12112 12113 if (error) 12114 return (error); 12115 12116 mutex_enter(&rp->r_statelock); 12117 if (rp->r_writeverf != write_verf) { 12118 mutex_exit(&rp->r_statelock); 12119 goto top; 12120 } 12121 mutex_exit(&rp->r_statelock); 12122 12123 /* 12124 * Now commit any pages which might need to be committed. 12125 * If the error, NFS_VERF_MISMATCH, is returned, then 12126 * start over with the flush operation. 12127 */ 12128 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 12129 12130 if (error == NFS_VERF_MISMATCH) 12131 goto top; 12132 12133 return (error); 12134 } 12135 12136 /* 12137 * nfs4_commit_vp() will wait for other pending commits and 12138 * will either commit the whole file or a range, plen dictates 12139 * if we commit whole file. a value of zero indicates the whole 12140 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 12141 */ 12142 static int 12143 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 12144 cred_t *cr, int wait_on_writes) 12145 { 12146 rnode4_t *rp; 12147 page_t *plist; 12148 offset3 offset; 12149 count3 len; 12150 12151 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12152 12153 rp = VTOR4(vp); 12154 12155 /* 12156 * before we gather commitable pages make 12157 * sure there are no outstanding async writes 12158 */ 12159 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 12160 mutex_enter(&rp->r_statelock); 12161 while (rp->r_count > 0) { 12162 cv_wait(&rp->r_cv, &rp->r_statelock); 12163 } 12164 mutex_exit(&rp->r_statelock); 12165 } 12166 12167 /* 12168 * Set the `commit inprogress' state bit. We must 12169 * first wait until any current one finishes. 12170 */ 12171 mutex_enter(&rp->r_statelock); 12172 while (rp->r_flags & R4COMMIT) { 12173 rp->r_flags |= R4COMMITWAIT; 12174 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 12175 rp->r_flags &= ~R4COMMITWAIT; 12176 } 12177 rp->r_flags |= R4COMMIT; 12178 mutex_exit(&rp->r_statelock); 12179 12180 /* 12181 * Gather all of the pages which need to be 12182 * committed. 12183 */ 12184 if (plen == 0) 12185 nfs4_get_commit(vp); 12186 else 12187 nfs4_get_commit_range(vp, poff, plen); 12188 12189 /* 12190 * Clear the `commit inprogress' bit and disconnect the 12191 * page list which was gathered by nfs4_get_commit. 12192 */ 12193 plist = rp->r_commit.c_pages; 12194 rp->r_commit.c_pages = NULL; 12195 offset = rp->r_commit.c_commbase; 12196 len = rp->r_commit.c_commlen; 12197 mutex_enter(&rp->r_statelock); 12198 rp->r_flags &= ~R4COMMIT; 12199 cv_broadcast(&rp->r_commit.c_cv); 12200 mutex_exit(&rp->r_statelock); 12201 12202 /* 12203 * If any pages need to be committed, commit them and 12204 * then unlock them so that they can be freed some 12205 * time later. 12206 */ 12207 if (plist == NULL) 12208 return (0); 12209 12210 /* 12211 * No error occurred during the flush portion 12212 * of this operation, so now attempt to commit 12213 * the data to stable storage on the server. 12214 * 12215 * This will unlock all of the pages on the list. 12216 */ 12217 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12218 } 12219 12220 static int 12221 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12222 cred_t *cr) 12223 { 12224 int error; 12225 page_t *pp; 12226 12227 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12228 12229 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12230 12231 /* 12232 * If we got an error, then just unlock all of the pages 12233 * on the list. 12234 */ 12235 if (error) { 12236 while (plist != NULL) { 12237 pp = plist; 12238 page_sub(&plist, pp); 12239 page_unlock(pp); 12240 } 12241 return (error); 12242 } 12243 /* 12244 * We've tried as hard as we can to commit the data to stable 12245 * storage on the server. We just unlock the pages and clear 12246 * the commit required state. They will get freed later. 12247 */ 12248 while (plist != NULL) { 12249 pp = plist; 12250 page_sub(&plist, pp); 12251 pp->p_fsdata = C_NOCOMMIT; 12252 page_unlock(pp); 12253 } 12254 12255 return (error); 12256 } 12257 12258 static void 12259 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12260 cred_t *cr) 12261 { 12262 12263 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12264 } 12265 12266 /*ARGSUSED*/ 12267 static int 12268 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12269 caller_context_t *ct) 12270 { 12271 int error = 0; 12272 mntinfo4_t *mi; 12273 vattr_t va; 12274 vsecattr_t nfsace4_vsap; 12275 12276 mi = VTOMI4(vp); 12277 if (nfs_zone() != mi->mi_zone) 12278 return (EIO); 12279 if (mi->mi_flags & MI4_ACL) { 12280 /* if we have a delegation, return it */ 12281 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12282 (void) nfs4delegreturn(VTOR4(vp), 12283 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12284 12285 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12286 NFS4_ACL_SET); 12287 if (error) /* EINVAL */ 12288 return (error); 12289 12290 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12291 /* 12292 * These are aclent_t type entries. 12293 */ 12294 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12295 vp->v_type == VDIR, FALSE); 12296 if (error) 12297 return (error); 12298 } else { 12299 /* 12300 * These are ace_t type entries. 12301 */ 12302 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12303 FALSE); 12304 if (error) 12305 return (error); 12306 } 12307 bzero(&va, sizeof (va)); 12308 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12309 vs_ace4_destroy(&nfsace4_vsap); 12310 return (error); 12311 } 12312 return (ENOSYS); 12313 } 12314 12315 /* ARGSUSED */ 12316 int 12317 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr, 12318 caller_context_t *ct) 12319 { 12320 int error; 12321 mntinfo4_t *mi; 12322 nfs4_ga_res_t gar; 12323 rnode4_t *rp = VTOR4(vp); 12324 12325 mi = VTOMI4(vp); 12326 if (nfs_zone() != mi->mi_zone) 12327 return (EIO); 12328 12329 bzero(&gar, sizeof (gar)); 12330 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12331 12332 /* 12333 * vsecattr->vsa_mask holds the original acl request mask. 12334 * This is needed when determining what to return. 12335 * (See: nfs4_create_getsecattr_return()) 12336 */ 12337 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12338 if (error) /* EINVAL */ 12339 return (error); 12340 12341 /* 12342 * If this is a referral stub, don't try to go OTW for an ACL 12343 */ 12344 if (RP_ISSTUB_REFERRAL(VTOR4(vp))) 12345 return (fs_fab_acl(vp, vsecattr, flag, cr, ct)); 12346 12347 if (mi->mi_flags & MI4_ACL) { 12348 /* 12349 * Check if the data is cached and the cache is valid. If it 12350 * is we don't go over the wire. 12351 */ 12352 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12353 mutex_enter(&rp->r_statelock); 12354 if (rp->r_secattr != NULL) { 12355 error = nfs4_create_getsecattr_return( 12356 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12357 rp->r_attr.va_gid, 12358 vp->v_type == VDIR); 12359 if (!error) { /* error == 0 - Success! */ 12360 mutex_exit(&rp->r_statelock); 12361 return (error); 12362 } 12363 } 12364 mutex_exit(&rp->r_statelock); 12365 } 12366 12367 /* 12368 * The getattr otw call will always get both the acl, in 12369 * the form of a list of nfsace4's, and the number of acl 12370 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12371 */ 12372 gar.n4g_va.va_mask = AT_ALL; 12373 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12374 if (error) { 12375 vs_ace4_destroy(&gar.n4g_vsa); 12376 if (error == ENOTSUP || error == EOPNOTSUPP) 12377 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12378 return (error); 12379 } 12380 12381 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12382 /* 12383 * No error was returned, but according to the response 12384 * bitmap, neither was an acl. 12385 */ 12386 vs_ace4_destroy(&gar.n4g_vsa); 12387 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12388 return (error); 12389 } 12390 12391 /* 12392 * Update the cache with the ACL. 12393 */ 12394 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12395 12396 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12397 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12398 vp->v_type == VDIR); 12399 vs_ace4_destroy(&gar.n4g_vsa); 12400 if ((error) && (vsecattr->vsa_mask & 12401 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12402 (error != EACCES)) { 12403 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12404 } 12405 return (error); 12406 } 12407 error = fs_fab_acl(vp, vsecattr, flag, cr, ct); 12408 return (error); 12409 } 12410 12411 /* 12412 * The function returns: 12413 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12414 * - EINVAL if the passed in "acl_mask" is an invalid request. 12415 * 12416 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12417 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12418 * 12419 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12420 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12421 * - We have a count field set without the corresponding acl field set. (e.g. - 12422 * VSA_ACECNT is set, but VSA_ACE is not) 12423 */ 12424 static int 12425 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12426 { 12427 /* Shortcut the masks that are always valid. */ 12428 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12429 return (0); 12430 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12431 return (0); 12432 12433 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12434 /* 12435 * We can't have any VSA_ACL type stuff in the mask now. 12436 */ 12437 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12438 VSA_DFACLCNT)) 12439 return (EINVAL); 12440 12441 if (op == NFS4_ACL_SET) { 12442 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12443 return (EINVAL); 12444 } 12445 } 12446 12447 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12448 /* 12449 * We can't have any VSA_ACE type stuff in the mask now. 12450 */ 12451 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12452 return (EINVAL); 12453 12454 if (op == NFS4_ACL_SET) { 12455 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12456 return (EINVAL); 12457 12458 if ((acl_mask & VSA_DFACLCNT) && 12459 !(acl_mask & VSA_DFACL)) 12460 return (EINVAL); 12461 } 12462 } 12463 return (0); 12464 } 12465 12466 /* 12467 * The theory behind creating the correct getsecattr return is simply this: 12468 * "Don't return anything that the caller is not expecting to have to free." 12469 */ 12470 static int 12471 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12472 uid_t uid, gid_t gid, int isdir) 12473 { 12474 int error = 0; 12475 /* Save the mask since the translators modify it. */ 12476 uint_t orig_mask = vsap->vsa_mask; 12477 12478 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12479 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE); 12480 12481 if (error) 12482 return (error); 12483 12484 /* 12485 * If the caller only asked for the ace count (VSA_ACECNT) 12486 * don't give them the full acl (VSA_ACE), free it. 12487 */ 12488 if (!orig_mask & VSA_ACE) { 12489 if (vsap->vsa_aclentp != NULL) { 12490 kmem_free(vsap->vsa_aclentp, 12491 vsap->vsa_aclcnt * sizeof (ace_t)); 12492 vsap->vsa_aclentp = NULL; 12493 } 12494 } 12495 vsap->vsa_mask = orig_mask; 12496 12497 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12498 VSA_DFACLCNT)) { 12499 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12500 isdir, FALSE); 12501 12502 if (error) 12503 return (error); 12504 12505 /* 12506 * If the caller only asked for the acl count (VSA_ACLCNT) 12507 * and/or the default acl count (VSA_DFACLCNT) don't give them 12508 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12509 */ 12510 if (!orig_mask & VSA_ACL) { 12511 if (vsap->vsa_aclentp != NULL) { 12512 kmem_free(vsap->vsa_aclentp, 12513 vsap->vsa_aclcnt * sizeof (aclent_t)); 12514 vsap->vsa_aclentp = NULL; 12515 } 12516 } 12517 12518 if (!orig_mask & VSA_DFACL) { 12519 if (vsap->vsa_dfaclentp != NULL) { 12520 kmem_free(vsap->vsa_dfaclentp, 12521 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12522 vsap->vsa_dfaclentp = NULL; 12523 } 12524 } 12525 vsap->vsa_mask = orig_mask; 12526 } 12527 return (0); 12528 } 12529 12530 /* ARGSUSED */ 12531 int 12532 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr, 12533 caller_context_t *ct) 12534 { 12535 int error; 12536 12537 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12538 return (EIO); 12539 /* 12540 * check for valid cmd parameter 12541 */ 12542 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12543 return (EINVAL); 12544 12545 /* 12546 * Check access permissions 12547 */ 12548 if ((cmd & F_SHARE) && 12549 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12550 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12551 return (EBADF); 12552 12553 /* 12554 * If the filesystem is mounted using local locking, pass the 12555 * request off to the local share code. 12556 */ 12557 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12558 return (fs_shrlock(vp, cmd, shr, flag, cr, ct)); 12559 12560 switch (cmd) { 12561 case F_SHARE: 12562 case F_UNSHARE: 12563 /* 12564 * This will be properly implemented later, 12565 * see RFE: 4823948 . 12566 */ 12567 error = EAGAIN; 12568 break; 12569 12570 case F_HASREMOTELOCKS: 12571 /* 12572 * NFS client can't store remote locks itself 12573 */ 12574 shr->s_access = 0; 12575 error = 0; 12576 break; 12577 12578 default: 12579 error = EINVAL; 12580 break; 12581 } 12582 12583 return (error); 12584 } 12585 12586 /* 12587 * Common code called by directory ops to update the attrcache 12588 */ 12589 static int 12590 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12591 hrtime_t t, vnode_t *vp, cred_t *cr) 12592 { 12593 int error = 0; 12594 12595 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12596 12597 if (status != NFS4_OK) { 12598 /* getattr not done or failed */ 12599 PURGE_ATTRCACHE4(vp); 12600 return (error); 12601 } 12602 12603 if (garp) { 12604 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12605 } else { 12606 PURGE_ATTRCACHE4(vp); 12607 } 12608 return (error); 12609 } 12610 12611 /* 12612 * Update directory caches for directory modification ops (link, rename, etc.) 12613 * When dinfo is NULL, manage dircaches in the old way. 12614 */ 12615 static void 12616 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12617 dirattr_info_t *dinfo) 12618 { 12619 rnode4_t *drp = VTOR4(dvp); 12620 12621 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12622 12623 /* Purge rddir cache for dir since it changed */ 12624 if (drp->r_dir != NULL) 12625 nfs4_purge_rddir_cache(dvp); 12626 12627 /* 12628 * If caller provided dinfo, then use it to manage dir caches. 12629 */ 12630 if (dinfo != NULL) { 12631 if (vp != NULL) { 12632 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12633 if (!VTOR4(vp)->created_v4) { 12634 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12635 dnlc_update(dvp, nm, vp); 12636 } else { 12637 /* 12638 * XXX don't update if the created_v4 flag is 12639 * set 12640 */ 12641 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12642 NFS4_DEBUG(nfs4_client_state_debug, 12643 (CE_NOTE, "nfs4_update_dircaches: " 12644 "don't update dnlc: created_v4 flag")); 12645 } 12646 } 12647 12648 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12649 dinfo->di_cred, FALSE, cinfo); 12650 12651 return; 12652 } 12653 12654 /* 12655 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12656 * Since caller modified dir but didn't receive post-dirmod-op dir 12657 * attrs, the dir's attrs must be purged. 12658 * 12659 * XXX this check and dnlc update/purge should really be atomic, 12660 * XXX but can't use rnode statelock because it'll deadlock in 12661 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12662 * XXX does occur. 12663 * 12664 * XXX We also may want to check that atomic is true in the 12665 * XXX change_info struct. If it is not, the change_info may 12666 * XXX reflect changes by more than one clients which means that 12667 * XXX our cache may not be valid. 12668 */ 12669 PURGE_ATTRCACHE4(dvp); 12670 if (drp->r_change == cinfo->before) { 12671 /* no changes took place in the directory prior to our link */ 12672 if (vp != NULL) { 12673 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12674 if (!VTOR4(vp)->created_v4) { 12675 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12676 dnlc_update(dvp, nm, vp); 12677 } else { 12678 /* 12679 * XXX dont' update if the created_v4 flag 12680 * is set 12681 */ 12682 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12683 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12684 "nfs4_update_dircaches: don't" 12685 " update dnlc: created_v4 flag")); 12686 } 12687 } 12688 } else { 12689 /* Another client modified directory - purge its dnlc cache */ 12690 dnlc_purge_vp(dvp); 12691 } 12692 } 12693 12694 /* 12695 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12696 * file. 12697 * 12698 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12699 * file (ie: client recovery) and otherwise set to FALSE. 12700 * 12701 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12702 * initiated) calling functions. 12703 * 12704 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12705 * of resending a 'lost' open request. 12706 * 12707 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12708 * server that hands out BAD_SEQID on open confirm. 12709 * 12710 * Errors are returned via the nfs4_error_t parameter. 12711 */ 12712 void 12713 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12714 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12715 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12716 { 12717 COMPOUND4args_clnt args; 12718 COMPOUND4res_clnt res; 12719 nfs_argop4 argop[2]; 12720 nfs_resop4 *resop; 12721 int doqueue = 1; 12722 mntinfo4_t *mi; 12723 OPEN_CONFIRM4args *open_confirm_args; 12724 int needrecov; 12725 12726 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12727 #if DEBUG 12728 mutex_enter(&oop->oo_lock); 12729 ASSERT(oop->oo_seqid_inuse); 12730 mutex_exit(&oop->oo_lock); 12731 #endif 12732 12733 recov_retry_confirm: 12734 nfs4_error_zinit(ep); 12735 *retry_open = FALSE; 12736 12737 if (resend) 12738 args.ctag = TAG_OPEN_CONFIRM_LOST; 12739 else 12740 args.ctag = TAG_OPEN_CONFIRM; 12741 12742 args.array_len = 2; 12743 args.array = argop; 12744 12745 /* putfh target fh */ 12746 argop[0].argop = OP_CPUTFH; 12747 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12748 12749 argop[1].argop = OP_OPEN_CONFIRM; 12750 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12751 12752 (*seqid) += 1; 12753 open_confirm_args->seqid = *seqid; 12754 open_confirm_args->open_stateid = *stateid; 12755 12756 mi = VTOMI4(vp); 12757 12758 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12759 12760 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12761 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12762 } 12763 12764 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12765 if (!needrecov && ep->error) 12766 return; 12767 12768 if (needrecov) { 12769 bool_t abort = FALSE; 12770 12771 if (reopening_file == FALSE) { 12772 nfs4_bseqid_entry_t *bsep = NULL; 12773 12774 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12775 bsep = nfs4_create_bseqid_entry(oop, NULL, 12776 vp, 0, args.ctag, 12777 open_confirm_args->seqid); 12778 12779 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 12780 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL); 12781 if (bsep) { 12782 kmem_free(bsep, sizeof (*bsep)); 12783 if (num_bseqid_retryp && 12784 --(*num_bseqid_retryp) == 0) 12785 abort = TRUE; 12786 } 12787 } 12788 if ((ep->error == ETIMEDOUT || 12789 res.status == NFS4ERR_RESOURCE) && 12790 abort == FALSE && resend == FALSE) { 12791 if (!ep->error) 12792 (void) xdr_free(xdr_COMPOUND4res_clnt, 12793 (caddr_t)&res); 12794 12795 delay(SEC_TO_TICK(confirm_retry_sec)); 12796 goto recov_retry_confirm; 12797 } 12798 /* State may have changed so retry the entire OPEN op */ 12799 if (abort == FALSE) 12800 *retry_open = TRUE; 12801 else 12802 *retry_open = FALSE; 12803 if (!ep->error) 12804 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12805 return; 12806 } 12807 12808 if (res.status) { 12809 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12810 return; 12811 } 12812 12813 resop = &res.array[1]; /* open confirm res */ 12814 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12815 stateid, sizeof (*stateid)); 12816 12817 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12818 } 12819 12820 /* 12821 * Return the credentials associated with a client state object. The 12822 * caller is responsible for freeing the credentials. 12823 */ 12824 12825 static cred_t * 12826 state_to_cred(nfs4_open_stream_t *osp) 12827 { 12828 cred_t *cr; 12829 12830 /* 12831 * It's ok to not lock the open stream and open owner to get 12832 * the oo_cred since this is only written once (upon creation) 12833 * and will not change. 12834 */ 12835 cr = osp->os_open_owner->oo_cred; 12836 crhold(cr); 12837 12838 return (cr); 12839 } 12840 12841 /* 12842 * nfs4_find_sysid 12843 * 12844 * Find the sysid for the knetconfig associated with the given mi. 12845 */ 12846 static struct lm_sysid * 12847 nfs4_find_sysid(mntinfo4_t *mi) 12848 { 12849 ASSERT(nfs_zone() == mi->mi_zone); 12850 12851 /* 12852 * Switch from RDMA knconf to original mount knconf 12853 */ 12854 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12855 mi->mi_curr_serv->sv_hostname, NULL)); 12856 } 12857 12858 #ifdef DEBUG 12859 /* 12860 * Return a string version of the call type for easy reading. 12861 */ 12862 static char * 12863 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12864 { 12865 switch (ctype) { 12866 case NFS4_LCK_CTYPE_NORM: 12867 return ("NORMAL"); 12868 case NFS4_LCK_CTYPE_RECLAIM: 12869 return ("RECLAIM"); 12870 case NFS4_LCK_CTYPE_RESEND: 12871 return ("RESEND"); 12872 case NFS4_LCK_CTYPE_REINSTATE: 12873 return ("REINSTATE"); 12874 default: 12875 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12876 "type %d", ctype); 12877 return (""); 12878 } 12879 } 12880 #endif 12881 12882 /* 12883 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12884 * Unlock requests don't have an over-the-wire locktype, so we just return 12885 * something non-threatening. 12886 */ 12887 12888 static nfs_lock_type4 12889 flk_to_locktype(int cmd, int l_type) 12890 { 12891 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12892 12893 switch (l_type) { 12894 case F_UNLCK: 12895 return (READ_LT); 12896 case F_RDLCK: 12897 if (cmd == F_SETLK) 12898 return (READ_LT); 12899 else 12900 return (READW_LT); 12901 case F_WRLCK: 12902 if (cmd == F_SETLK) 12903 return (WRITE_LT); 12904 else 12905 return (WRITEW_LT); 12906 } 12907 panic("flk_to_locktype"); 12908 /*NOTREACHED*/ 12909 } 12910 12911 /* 12912 * Do some preliminary checks for nfs4frlock. 12913 */ 12914 static int 12915 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12916 u_offset_t offset) 12917 { 12918 int error = 0; 12919 12920 /* 12921 * If we are setting a lock, check that the file is opened 12922 * with the correct mode. 12923 */ 12924 if (cmd == F_SETLK || cmd == F_SETLKW) { 12925 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12926 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12927 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12928 "nfs4frlock_validate_args: file was opened with " 12929 "incorrect mode")); 12930 return (EBADF); 12931 } 12932 } 12933 12934 /* Convert the offset. It may need to be restored before returning. */ 12935 if (error = convoff(vp, flk, 0, offset)) { 12936 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12937 "nfs4frlock_validate_args: convoff => error= %d\n", 12938 error)); 12939 return (error); 12940 } 12941 12942 return (error); 12943 } 12944 12945 /* 12946 * Set the flock64's lm_sysid for nfs4frlock. 12947 */ 12948 static int 12949 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12950 { 12951 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12952 12953 /* Find the lm_sysid */ 12954 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12955 12956 if (*lspp == NULL) { 12957 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12958 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12959 return (ENOLCK); 12960 } 12961 12962 flk->l_sysid = lm_sysidt(*lspp); 12963 12964 return (0); 12965 } 12966 12967 /* 12968 * Do the remaining preliminary setup for nfs4frlock. 12969 */ 12970 static void 12971 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12972 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12973 cred_t **cred_otw) 12974 { 12975 /* 12976 * set tick_delay to the base delay time. 12977 * (nfs4_base_wait_time is in msecs) 12978 */ 12979 12980 *tick_delayp = drv_usectohz(nfs4_base_wait_time * 1000); 12981 12982 /* 12983 * If lock is relative to EOF, we need the newest length of the 12984 * file. Therefore invalidate the ATTR_CACHE. 12985 */ 12986 12987 *whencep = flk->l_whence; 12988 12989 if (*whencep == 2) /* SEEK_END */ 12990 PURGE_ATTRCACHE4(vp); 12991 12992 recov_statep->rs_flags = 0; 12993 recov_statep->rs_num_retry_despite_err = 0; 12994 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 12995 } 12996 12997 /* 12998 * Initialize and allocate the data structures necessary for 12999 * the nfs4frlock call. 13000 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 13001 */ 13002 static void 13003 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 13004 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 13005 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 13006 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 13007 { 13008 int argoplist_size; 13009 int num_ops = 2; 13010 13011 *retry = FALSE; 13012 *did_start_fop = FALSE; 13013 *skip_get_err = FALSE; 13014 lost_rqstp->lr_op = 0; 13015 argoplist_size = num_ops * sizeof (nfs_argop4); 13016 /* fill array with zero */ 13017 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 13018 13019 *argspp = argsp; 13020 *respp = NULL; 13021 13022 argsp->array_len = num_ops; 13023 argsp->array = *argopp; 13024 13025 /* initialize in case of error; will get real value down below */ 13026 argsp->ctag = TAG_NONE; 13027 13028 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 13029 *op_hintp = OH_LOCKU; 13030 else 13031 *op_hintp = OH_OTHER; 13032 } 13033 13034 /* 13035 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 13036 * the proper nfs4_server_t for this instance of nfs4frlock. 13037 * Returns 0 (success) or an errno value. 13038 */ 13039 static int 13040 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 13041 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 13042 bool_t *did_start_fop, bool_t *startrecovp) 13043 { 13044 int error = 0; 13045 rnode4_t *rp; 13046 13047 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13048 13049 if (ctype == NFS4_LCK_CTYPE_NORM) { 13050 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 13051 recov_statep, startrecovp); 13052 if (error) 13053 return (error); 13054 *did_start_fop = TRUE; 13055 } else { 13056 *did_start_fop = FALSE; 13057 *startrecovp = FALSE; 13058 } 13059 13060 if (!error) { 13061 rp = VTOR4(vp); 13062 13063 /* If the file failed recovery, just quit. */ 13064 mutex_enter(&rp->r_statelock); 13065 if (rp->r_flags & R4RECOVERR) { 13066 error = EIO; 13067 } 13068 mutex_exit(&rp->r_statelock); 13069 } 13070 13071 return (error); 13072 } 13073 13074 /* 13075 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 13076 * resend nfs4frlock call is initiated by the recovery framework. 13077 * Acquires the lop and oop seqid synchronization. 13078 */ 13079 static void 13080 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 13081 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 13082 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13083 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 13084 { 13085 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 13086 int error; 13087 13088 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 13089 (CE_NOTE, 13090 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 13091 ASSERT(resend_rqstp != NULL); 13092 ASSERT(resend_rqstp->lr_op == OP_LOCK || 13093 resend_rqstp->lr_op == OP_LOCKU); 13094 13095 *oopp = resend_rqstp->lr_oop; 13096 if (resend_rqstp->lr_oop) { 13097 open_owner_hold(resend_rqstp->lr_oop); 13098 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 13099 ASSERT(error == 0); /* recov thread always succeeds */ 13100 } 13101 13102 /* Must resend this lost lock/locku request. */ 13103 ASSERT(resend_rqstp->lr_lop != NULL); 13104 *lopp = resend_rqstp->lr_lop; 13105 lock_owner_hold(resend_rqstp->lr_lop); 13106 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 13107 ASSERT(error == 0); /* recov thread always succeeds */ 13108 13109 *ospp = resend_rqstp->lr_osp; 13110 if (*ospp) 13111 open_stream_hold(resend_rqstp->lr_osp); 13112 13113 if (resend_rqstp->lr_op == OP_LOCK) { 13114 LOCK4args *lock_args; 13115 13116 argop->argop = OP_LOCK; 13117 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 13118 lock_args->locktype = resend_rqstp->lr_locktype; 13119 lock_args->reclaim = 13120 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 13121 lock_args->offset = resend_rqstp->lr_flk->l_start; 13122 lock_args->length = resend_rqstp->lr_flk->l_len; 13123 if (lock_args->length == 0) 13124 lock_args->length = ~lock_args->length; 13125 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 13126 mi2clientid(mi), &lock_args->locker); 13127 13128 switch (resend_rqstp->lr_ctype) { 13129 case NFS4_LCK_CTYPE_RESEND: 13130 argsp->ctag = TAG_LOCK_RESEND; 13131 break; 13132 case NFS4_LCK_CTYPE_REINSTATE: 13133 argsp->ctag = TAG_LOCK_REINSTATE; 13134 break; 13135 case NFS4_LCK_CTYPE_RECLAIM: 13136 argsp->ctag = TAG_LOCK_RECLAIM; 13137 break; 13138 default: 13139 argsp->ctag = TAG_LOCK_UNKNOWN; 13140 break; 13141 } 13142 } else { 13143 LOCKU4args *locku_args; 13144 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 13145 13146 argop->argop = OP_LOCKU; 13147 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 13148 locku_args->locktype = READ_LT; 13149 locku_args->seqid = lop->lock_seqid + 1; 13150 mutex_enter(&lop->lo_lock); 13151 locku_args->lock_stateid = lop->lock_stateid; 13152 mutex_exit(&lop->lo_lock); 13153 locku_args->offset = resend_rqstp->lr_flk->l_start; 13154 locku_args->length = resend_rqstp->lr_flk->l_len; 13155 if (locku_args->length == 0) 13156 locku_args->length = ~locku_args->length; 13157 13158 switch (resend_rqstp->lr_ctype) { 13159 case NFS4_LCK_CTYPE_RESEND: 13160 argsp->ctag = TAG_LOCKU_RESEND; 13161 break; 13162 case NFS4_LCK_CTYPE_REINSTATE: 13163 argsp->ctag = TAG_LOCKU_REINSTATE; 13164 break; 13165 default: 13166 argsp->ctag = TAG_LOCK_UNKNOWN; 13167 break; 13168 } 13169 } 13170 } 13171 13172 /* 13173 * Setup the LOCKT4 arguments. 13174 */ 13175 static void 13176 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13177 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 13178 rnode4_t *rp) 13179 { 13180 LOCKT4args *lockt_args; 13181 13182 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 13183 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13184 argop->argop = OP_LOCKT; 13185 argsp->ctag = TAG_LOCKT; 13186 lockt_args = &argop->nfs_argop4_u.oplockt; 13187 13188 /* 13189 * The locktype will be READ_LT unless it's 13190 * a write lock. We do this because the Solaris 13191 * system call allows the combination of 13192 * F_UNLCK and F_GETLK* and so in that case the 13193 * unlock is mapped to a read. 13194 */ 13195 if (flk->l_type == F_WRLCK) 13196 lockt_args->locktype = WRITE_LT; 13197 else 13198 lockt_args->locktype = READ_LT; 13199 13200 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 13201 /* set the lock owner4 args */ 13202 nfs4_setlockowner_args(&lockt_args->owner, rp, 13203 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13204 flk->l_pid); 13205 lockt_args->offset = flk->l_start; 13206 lockt_args->length = flk->l_len; 13207 if (flk->l_len == 0) 13208 lockt_args->length = ~lockt_args->length; 13209 13210 *lockt_argsp = lockt_args; 13211 } 13212 13213 /* 13214 * If the client is holding a delegation, and the open stream to be used 13215 * with this lock request is a delegation open stream, then re-open the stream. 13216 * Sets the nfs4_error_t to all zeros unless the open stream has already 13217 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13218 * means the caller should retry (like a recovery retry). 13219 */ 13220 static void 13221 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13222 { 13223 open_delegation_type4 dt; 13224 bool_t reopen_needed, force; 13225 nfs4_open_stream_t *osp; 13226 open_claim_type4 oclaim; 13227 rnode4_t *rp = VTOR4(vp); 13228 mntinfo4_t *mi = VTOMI4(vp); 13229 13230 ASSERT(nfs_zone() == mi->mi_zone); 13231 13232 nfs4_error_zinit(ep); 13233 13234 mutex_enter(&rp->r_statev4_lock); 13235 dt = rp->r_deleg_type; 13236 mutex_exit(&rp->r_statev4_lock); 13237 13238 if (dt != OPEN_DELEGATE_NONE) { 13239 nfs4_open_owner_t *oop; 13240 13241 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13242 if (!oop) { 13243 ep->stat = NFS4ERR_IO; 13244 return; 13245 } 13246 /* returns with 'os_sync_lock' held */ 13247 osp = find_open_stream(oop, rp); 13248 if (!osp) { 13249 open_owner_rele(oop); 13250 ep->stat = NFS4ERR_IO; 13251 return; 13252 } 13253 13254 if (osp->os_failed_reopen) { 13255 NFS4_DEBUG((nfs4_open_stream_debug || 13256 nfs4_client_lock_debug), (CE_NOTE, 13257 "nfs4frlock_check_deleg: os_failed_reopen set " 13258 "for osp %p, cr %p, rp %s", (void *)osp, 13259 (void *)cr, rnode4info(rp))); 13260 mutex_exit(&osp->os_sync_lock); 13261 open_stream_rele(osp, rp); 13262 open_owner_rele(oop); 13263 ep->stat = NFS4ERR_IO; 13264 return; 13265 } 13266 13267 /* 13268 * Determine whether a reopen is needed. If this 13269 * is a delegation open stream, then send the open 13270 * to the server to give visibility to the open owner. 13271 * Even if it isn't a delegation open stream, we need 13272 * to check if the previous open CLAIM_DELEGATE_CUR 13273 * was sufficient. 13274 */ 13275 13276 reopen_needed = osp->os_delegation || 13277 ((lt == F_RDLCK && 13278 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13279 (lt == F_WRLCK && 13280 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13281 13282 mutex_exit(&osp->os_sync_lock); 13283 open_owner_rele(oop); 13284 13285 if (reopen_needed) { 13286 /* 13287 * Always use CLAIM_PREVIOUS after server reboot. 13288 * The server will reject CLAIM_DELEGATE_CUR if 13289 * it is used during the grace period. 13290 */ 13291 mutex_enter(&mi->mi_lock); 13292 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13293 oclaim = CLAIM_PREVIOUS; 13294 force = TRUE; 13295 } else { 13296 oclaim = CLAIM_DELEGATE_CUR; 13297 force = FALSE; 13298 } 13299 mutex_exit(&mi->mi_lock); 13300 13301 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13302 if (ep->error == EAGAIN) { 13303 nfs4_error_zinit(ep); 13304 ep->stat = NFS4ERR_DELAY; 13305 } 13306 } 13307 open_stream_rele(osp, rp); 13308 osp = NULL; 13309 } 13310 } 13311 13312 /* 13313 * Setup the LOCKU4 arguments. 13314 * Returns errors via the nfs4_error_t. 13315 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13316 * over-the-wire. The caller must release the 13317 * reference on *lopp. 13318 * NFS4ERR_DELAY caller should retry (like recovery retry) 13319 * (other) unrecoverable error. 13320 */ 13321 static void 13322 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13323 LOCKU4args **locku_argsp, flock64_t *flk, 13324 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13325 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13326 bool_t *skip_get_err, bool_t *go_otwp) 13327 { 13328 nfs4_lock_owner_t *lop = NULL; 13329 LOCKU4args *locku_args; 13330 pid_t pid; 13331 bool_t is_spec = FALSE; 13332 rnode4_t *rp = VTOR4(vp); 13333 13334 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13335 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13336 13337 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13338 if (ep->error || ep->stat) 13339 return; 13340 13341 argop->argop = OP_LOCKU; 13342 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13343 argsp->ctag = TAG_LOCKU_REINSTATE; 13344 else 13345 argsp->ctag = TAG_LOCKU; 13346 locku_args = &argop->nfs_argop4_u.oplocku; 13347 *locku_argsp = locku_args; 13348 13349 /* 13350 * XXX what should locku_args->locktype be? 13351 * setting to ALWAYS be READ_LT so at least 13352 * it is a valid locktype. 13353 */ 13354 13355 locku_args->locktype = READ_LT; 13356 13357 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13358 flk->l_pid; 13359 13360 /* 13361 * Get the lock owner stateid. If no lock owner 13362 * exists, return success. 13363 */ 13364 lop = find_lock_owner(rp, pid, LOWN_ANY); 13365 *lopp = lop; 13366 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13367 is_spec = TRUE; 13368 if (!lop || is_spec) { 13369 /* 13370 * No lock owner so no locks to unlock. 13371 * Return success. If there was a failed 13372 * reclaim earlier, the lock might still be 13373 * registered with the local locking code, 13374 * so notify it of the unlock. 13375 * 13376 * If the lockowner is using a special stateid, 13377 * then the original lock request (that created 13378 * this lockowner) was never successful, so we 13379 * have no lock to undo OTW. 13380 */ 13381 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13382 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13383 "(%ld) so return success", (long)pid)); 13384 13385 if (ctype == NFS4_LCK_CTYPE_NORM) 13386 flk->l_pid = curproc->p_pid; 13387 nfs4_register_lock_locally(vp, flk, flag, offset); 13388 /* 13389 * Release our hold and NULL out so final_cleanup 13390 * doesn't try to end a lock seqid sync we 13391 * never started. 13392 */ 13393 if (is_spec) { 13394 lock_owner_rele(lop); 13395 *lopp = NULL; 13396 } 13397 *skip_get_err = TRUE; 13398 *go_otwp = FALSE; 13399 return; 13400 } 13401 13402 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13403 if (ep->error == EAGAIN) { 13404 lock_owner_rele(lop); 13405 *lopp = NULL; 13406 return; 13407 } 13408 13409 mutex_enter(&lop->lo_lock); 13410 locku_args->lock_stateid = lop->lock_stateid; 13411 mutex_exit(&lop->lo_lock); 13412 locku_args->seqid = lop->lock_seqid + 1; 13413 13414 /* leave the ref count on lop, rele after RPC call */ 13415 13416 locku_args->offset = flk->l_start; 13417 locku_args->length = flk->l_len; 13418 if (flk->l_len == 0) 13419 locku_args->length = ~locku_args->length; 13420 13421 *go_otwp = TRUE; 13422 } 13423 13424 /* 13425 * Setup the LOCK4 arguments. 13426 * 13427 * Returns errors via the nfs4_error_t. 13428 * NFS4_OK no problems 13429 * NFS4ERR_DELAY caller should retry (like recovery retry) 13430 * (other) unrecoverable error 13431 */ 13432 static void 13433 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13434 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13435 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13436 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13437 { 13438 LOCK4args *lock_args; 13439 nfs4_open_owner_t *oop = NULL; 13440 nfs4_open_stream_t *osp = NULL; 13441 nfs4_lock_owner_t *lop = NULL; 13442 pid_t pid; 13443 rnode4_t *rp = VTOR4(vp); 13444 13445 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13446 13447 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13448 if (ep->error || ep->stat != NFS4_OK) 13449 return; 13450 13451 argop->argop = OP_LOCK; 13452 if (ctype == NFS4_LCK_CTYPE_NORM) 13453 argsp->ctag = TAG_LOCK; 13454 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13455 argsp->ctag = TAG_RELOCK; 13456 else 13457 argsp->ctag = TAG_LOCK_REINSTATE; 13458 lock_args = &argop->nfs_argop4_u.oplock; 13459 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13460 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13461 /* 13462 * Get the lock owner. If no lock owner exists, 13463 * create a 'temporary' one and grab the open seqid 13464 * synchronization (which puts a hold on the open 13465 * owner and open stream). 13466 * This also grabs the lock seqid synchronization. 13467 */ 13468 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13469 ep->stat = 13470 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13471 13472 if (ep->stat != NFS4_OK) 13473 goto out; 13474 13475 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13476 &lock_args->locker); 13477 13478 lock_args->offset = flk->l_start; 13479 lock_args->length = flk->l_len; 13480 if (flk->l_len == 0) 13481 lock_args->length = ~lock_args->length; 13482 *lock_argsp = lock_args; 13483 out: 13484 *oopp = oop; 13485 *ospp = osp; 13486 *lopp = lop; 13487 } 13488 13489 /* 13490 * After we get the reply from the server, record the proper information 13491 * for possible resend lock requests. 13492 * 13493 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13494 */ 13495 static void 13496 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13497 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13498 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13499 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13500 { 13501 bool_t unlock = (flk->l_type == F_UNLCK); 13502 13503 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13504 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13505 ctype == NFS4_LCK_CTYPE_REINSTATE); 13506 13507 if (error != 0 && !unlock) { 13508 NFS4_DEBUG((nfs4_lost_rqst_debug || 13509 nfs4_client_lock_debug), (CE_NOTE, 13510 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13511 " for lop %p", (void *)lop)); 13512 ASSERT(lop != NULL); 13513 mutex_enter(&lop->lo_lock); 13514 lop->lo_pending_rqsts = 1; 13515 mutex_exit(&lop->lo_lock); 13516 } 13517 13518 lost_rqstp->lr_putfirst = FALSE; 13519 lost_rqstp->lr_op = 0; 13520 13521 /* 13522 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13523 * recovery purposes so that the lock request that was sent 13524 * can be saved and re-issued later. Ditto for EIO from a forced 13525 * unmount. This is done to have the client's local locking state 13526 * match the v4 server's state; that is, the request was 13527 * potentially received and accepted by the server but the client 13528 * thinks it was not. 13529 */ 13530 if (error == ETIMEDOUT || error == EINTR || 13531 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13532 NFS4_DEBUG((nfs4_lost_rqst_debug || 13533 nfs4_client_lock_debug), (CE_NOTE, 13534 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13535 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13536 (void *)lop, (void *)oop, (void *)osp)); 13537 if (unlock) 13538 lost_rqstp->lr_op = OP_LOCKU; 13539 else { 13540 lost_rqstp->lr_op = OP_LOCK; 13541 lost_rqstp->lr_locktype = locktype; 13542 } 13543 /* 13544 * Objects are held and rele'd via the recovery code. 13545 * See nfs4_save_lost_rqst. 13546 */ 13547 lost_rqstp->lr_vp = vp; 13548 lost_rqstp->lr_dvp = NULL; 13549 lost_rqstp->lr_oop = oop; 13550 lost_rqstp->lr_osp = osp; 13551 lost_rqstp->lr_lop = lop; 13552 lost_rqstp->lr_cr = cr; 13553 switch (ctype) { 13554 case NFS4_LCK_CTYPE_NORM: 13555 flk->l_pid = ttoproc(curthread)->p_pid; 13556 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13557 break; 13558 case NFS4_LCK_CTYPE_REINSTATE: 13559 lost_rqstp->lr_putfirst = TRUE; 13560 lost_rqstp->lr_ctype = ctype; 13561 break; 13562 default: 13563 break; 13564 } 13565 lost_rqstp->lr_flk = flk; 13566 } 13567 } 13568 13569 /* 13570 * Update lop's seqid. Also update the seqid stored in a resend request, 13571 * if any. (Some recovery errors increment the seqid, and we may have to 13572 * send the resend request again.) 13573 */ 13574 13575 static void 13576 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13577 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13578 { 13579 if (lock_args) { 13580 if (lock_args->locker.new_lock_owner == TRUE) 13581 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13582 else { 13583 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13584 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13585 } 13586 } else if (locku_args) { 13587 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13588 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13589 } 13590 } 13591 13592 /* 13593 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13594 * COMPOUND4 args/res for calls that need to retry. 13595 * Switches the *cred_otwp to base_cr. 13596 */ 13597 static void 13598 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13599 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13600 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13601 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13602 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13603 { 13604 nfs4_open_owner_t *oop = *oopp; 13605 nfs4_open_stream_t *osp = *ospp; 13606 nfs4_lock_owner_t *lop = *lopp; 13607 nfs_argop4 *argop = (*argspp)->array; 13608 13609 if (*did_start_fop) { 13610 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13611 needrecov); 13612 *did_start_fop = FALSE; 13613 } 13614 ASSERT((*argspp)->array_len == 2); 13615 if (argop[1].argop == OP_LOCK) 13616 nfs4args_lock_free(&argop[1]); 13617 else if (argop[1].argop == OP_LOCKT) 13618 nfs4args_lockt_free(&argop[1]); 13619 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13620 if (!error) 13621 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13622 *argspp = NULL; 13623 *respp = NULL; 13624 13625 if (lop) { 13626 nfs4_end_lock_seqid_sync(lop); 13627 lock_owner_rele(lop); 13628 *lopp = NULL; 13629 } 13630 13631 /* need to free up the reference on osp for lock args */ 13632 if (osp != NULL) { 13633 open_stream_rele(osp, VTOR4(vp)); 13634 *ospp = NULL; 13635 } 13636 13637 /* need to free up the reference on oop for lock args */ 13638 if (oop != NULL) { 13639 nfs4_end_open_seqid_sync(oop); 13640 open_owner_rele(oop); 13641 *oopp = NULL; 13642 } 13643 13644 crfree(*cred_otwp); 13645 *cred_otwp = base_cr; 13646 crhold(*cred_otwp); 13647 } 13648 13649 /* 13650 * Function to process the client's recovery for nfs4frlock. 13651 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13652 * 13653 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13654 * COMPOUND4 args/res for calls that need to retry. 13655 * 13656 * Note: the rp's r_lkserlock is *not* dropped during this path. 13657 */ 13658 static bool_t 13659 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13660 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13661 LOCK4args *lock_args, LOCKU4args *locku_args, 13662 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13663 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13664 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13665 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13666 { 13667 nfs4_open_owner_t *oop = *oopp; 13668 nfs4_open_stream_t *osp = *ospp; 13669 nfs4_lock_owner_t *lop = *lopp; 13670 13671 bool_t abort, retry; 13672 13673 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13674 ASSERT((*argspp) != NULL); 13675 ASSERT((*respp) != NULL); 13676 if (lock_args || locku_args) 13677 ASSERT(lop != NULL); 13678 13679 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13680 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13681 13682 retry = TRUE; 13683 abort = FALSE; 13684 if (needrecov) { 13685 nfs4_bseqid_entry_t *bsep = NULL; 13686 nfs_opnum4 op; 13687 13688 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13689 13690 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13691 seqid4 seqid; 13692 13693 if (lock_args) { 13694 if (lock_args->locker.new_lock_owner == TRUE) 13695 seqid = lock_args->locker.locker4_u. 13696 open_owner.open_seqid; 13697 else 13698 seqid = lock_args->locker.locker4_u. 13699 lock_owner.lock_seqid; 13700 } else if (locku_args) { 13701 seqid = locku_args->seqid; 13702 } else { 13703 seqid = 0; 13704 } 13705 13706 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13707 flk->l_pid, (*argspp)->ctag, seqid); 13708 } 13709 13710 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13711 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13712 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13713 NULL, op, bsep, NULL, NULL); 13714 13715 if (bsep) 13716 kmem_free(bsep, sizeof (*bsep)); 13717 } 13718 13719 /* 13720 * Return that we do not want to retry the request for 3 cases: 13721 * 1. If we received EINTR or are bailing out because of a forced 13722 * unmount, we came into this code path just for the sake of 13723 * initiating recovery, we now need to return the error. 13724 * 2. If we have aborted recovery. 13725 * 3. We received NFS4ERR_BAD_SEQID. 13726 */ 13727 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13728 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13729 retry = FALSE; 13730 13731 if (*did_start_fop == TRUE) { 13732 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13733 needrecov); 13734 *did_start_fop = FALSE; 13735 } 13736 13737 if (retry == TRUE) { 13738 nfs_argop4 *argop; 13739 13740 argop = (*argspp)->array; 13741 ASSERT((*argspp)->array_len == 2); 13742 13743 if (argop[1].argop == OP_LOCK) 13744 nfs4args_lock_free(&argop[1]); 13745 else if (argop[1].argop == OP_LOCKT) 13746 nfs4args_lockt_free(&argop[1]); 13747 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13748 if (!ep->error) 13749 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13750 *respp = NULL; 13751 *argspp = NULL; 13752 } 13753 13754 if (lop != NULL) { 13755 nfs4_end_lock_seqid_sync(lop); 13756 lock_owner_rele(lop); 13757 } 13758 13759 *lopp = NULL; 13760 13761 /* need to free up the reference on osp for lock args */ 13762 if (osp != NULL) { 13763 open_stream_rele(osp, rp); 13764 *ospp = NULL; 13765 } 13766 13767 /* need to free up the reference on oop for lock args */ 13768 if (oop != NULL) { 13769 nfs4_end_open_seqid_sync(oop); 13770 open_owner_rele(oop); 13771 *oopp = NULL; 13772 } 13773 13774 return (retry); 13775 } 13776 13777 /* 13778 * Handles the successful reply from the server for nfs4frlock. 13779 */ 13780 static void 13781 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13782 vnode_t *vp, int flag, u_offset_t offset, 13783 nfs4_lost_rqst_t *resend_rqstp) 13784 { 13785 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13786 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13787 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13788 if (ctype == NFS4_LCK_CTYPE_NORM) { 13789 flk->l_pid = ttoproc(curthread)->p_pid; 13790 /* 13791 * We do not register lost locks locally in 13792 * the 'resend' case since the user/application 13793 * doesn't think we have the lock. 13794 */ 13795 ASSERT(!resend_rqstp); 13796 nfs4_register_lock_locally(vp, flk, flag, offset); 13797 } 13798 } 13799 } 13800 13801 /* 13802 * Handle the DENIED reply from the server for nfs4frlock. 13803 * Returns TRUE if we should retry the request; FALSE otherwise. 13804 * 13805 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13806 * COMPOUND4 args/res for calls that need to retry. Can also 13807 * drop and regrab the r_lkserlock. 13808 */ 13809 static bool_t 13810 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13811 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13812 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13813 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13814 nfs4_recov_state_t *recov_statep, int needrecov, 13815 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13816 clock_t *tick_delayp, short *whencep, int *errorp, 13817 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13818 bool_t *skip_get_err) 13819 { 13820 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13821 13822 if (lock_args) { 13823 nfs4_open_owner_t *oop = *oopp; 13824 nfs4_open_stream_t *osp = *ospp; 13825 nfs4_lock_owner_t *lop = *lopp; 13826 int intr; 13827 13828 /* 13829 * Blocking lock needs to sleep and retry from the request. 13830 * 13831 * Do not block and wait for 'resend' or 'reinstate' 13832 * lock requests, just return the error. 13833 * 13834 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13835 */ 13836 if (cmd == F_SETLKW) { 13837 rnode4_t *rp = VTOR4(vp); 13838 nfs_argop4 *argop = (*argspp)->array; 13839 13840 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13841 13842 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13843 recov_statep, needrecov); 13844 *did_start_fop = FALSE; 13845 ASSERT((*argspp)->array_len == 2); 13846 if (argop[1].argop == OP_LOCK) 13847 nfs4args_lock_free(&argop[1]); 13848 else if (argop[1].argop == OP_LOCKT) 13849 nfs4args_lockt_free(&argop[1]); 13850 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13851 if (*respp) 13852 (void) xdr_free(xdr_COMPOUND4res_clnt, 13853 (caddr_t)*respp); 13854 *argspp = NULL; 13855 *respp = NULL; 13856 nfs4_end_lock_seqid_sync(lop); 13857 lock_owner_rele(lop); 13858 *lopp = NULL; 13859 if (osp != NULL) { 13860 open_stream_rele(osp, rp); 13861 *ospp = NULL; 13862 } 13863 if (oop != NULL) { 13864 nfs4_end_open_seqid_sync(oop); 13865 open_owner_rele(oop); 13866 *oopp = NULL; 13867 } 13868 13869 nfs_rw_exit(&rp->r_lkserlock); 13870 13871 intr = nfs4_block_and_wait(tick_delayp, rp); 13872 13873 if (intr) { 13874 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13875 RW_WRITER, FALSE); 13876 *errorp = EINTR; 13877 return (FALSE); 13878 } 13879 13880 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13881 RW_WRITER, FALSE); 13882 13883 /* 13884 * Make sure we are still safe to lock with 13885 * regards to mmapping. 13886 */ 13887 if (!nfs4_safelock(vp, flk, cr)) { 13888 *errorp = EAGAIN; 13889 return (FALSE); 13890 } 13891 13892 return (TRUE); 13893 } 13894 if (ctype == NFS4_LCK_CTYPE_NORM) 13895 *errorp = EAGAIN; 13896 *skip_get_err = TRUE; 13897 flk->l_whence = 0; 13898 *whencep = 0; 13899 return (FALSE); 13900 } else if (lockt_args) { 13901 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13902 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13903 13904 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13905 flk, lockt_args); 13906 13907 /* according to NLM code */ 13908 *errorp = 0; 13909 *whencep = 0; 13910 *skip_get_err = TRUE; 13911 return (FALSE); 13912 } 13913 return (FALSE); 13914 } 13915 13916 /* 13917 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13918 */ 13919 static void 13920 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13921 { 13922 switch (resp->status) { 13923 case NFS4ERR_ACCESS: 13924 case NFS4ERR_ADMIN_REVOKED: 13925 case NFS4ERR_BADHANDLE: 13926 case NFS4ERR_BAD_RANGE: 13927 case NFS4ERR_BAD_SEQID: 13928 case NFS4ERR_BAD_STATEID: 13929 case NFS4ERR_BADXDR: 13930 case NFS4ERR_DEADLOCK: 13931 case NFS4ERR_DELAY: 13932 case NFS4ERR_EXPIRED: 13933 case NFS4ERR_FHEXPIRED: 13934 case NFS4ERR_GRACE: 13935 case NFS4ERR_INVAL: 13936 case NFS4ERR_ISDIR: 13937 case NFS4ERR_LEASE_MOVED: 13938 case NFS4ERR_LOCK_NOTSUPP: 13939 case NFS4ERR_LOCK_RANGE: 13940 case NFS4ERR_MOVED: 13941 case NFS4ERR_NOFILEHANDLE: 13942 case NFS4ERR_NO_GRACE: 13943 case NFS4ERR_OLD_STATEID: 13944 case NFS4ERR_OPENMODE: 13945 case NFS4ERR_RECLAIM_BAD: 13946 case NFS4ERR_RECLAIM_CONFLICT: 13947 case NFS4ERR_RESOURCE: 13948 case NFS4ERR_SERVERFAULT: 13949 case NFS4ERR_STALE: 13950 case NFS4ERR_STALE_CLIENTID: 13951 case NFS4ERR_STALE_STATEID: 13952 return; 13953 default: 13954 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13955 "nfs4frlock_results_default: got unrecognizable " 13956 "res.status %d", resp->status)); 13957 *errorp = NFS4ERR_INVAL; 13958 } 13959 } 13960 13961 /* 13962 * The lock request was successful, so update the client's state. 13963 */ 13964 static void 13965 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13966 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13967 vnode_t *vp, flock64_t *flk, cred_t *cr, 13968 nfs4_lost_rqst_t *resend_rqstp) 13969 { 13970 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13971 13972 if (lock_args) { 13973 LOCK4res *lock_res; 13974 13975 lock_res = &resop->nfs_resop4_u.oplock; 13976 /* update the stateid with server's response */ 13977 13978 if (lock_args->locker.new_lock_owner == TRUE) { 13979 mutex_enter(&lop->lo_lock); 13980 lop->lo_just_created = NFS4_PERM_CREATED; 13981 mutex_exit(&lop->lo_lock); 13982 } 13983 13984 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13985 13986 /* 13987 * If the lock was the result of a resending a lost 13988 * request, we've synched up the stateid and seqid 13989 * with the server, but now the server might be out of sync 13990 * with what the application thinks it has for locks. 13991 * Clean that up here. It's unclear whether we should do 13992 * this even if the filesystem has been forcibly unmounted. 13993 * For most servers, it's probably wasted effort, but 13994 * RFC3530 lets servers require that unlocks exactly match 13995 * the locks that are held. 13996 */ 13997 if (resend_rqstp != NULL && 13998 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 13999 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 14000 } else { 14001 flk->l_whence = 0; 14002 } 14003 } else if (locku_args) { 14004 LOCKU4res *locku_res; 14005 14006 locku_res = &resop->nfs_resop4_u.oplocku; 14007 14008 /* Update the stateid with the server's response */ 14009 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 14010 } else if (lockt_args) { 14011 /* Switch the lock type to express success, see fcntl */ 14012 flk->l_type = F_UNLCK; 14013 flk->l_whence = 0; 14014 } 14015 } 14016 14017 /* 14018 * Do final cleanup before exiting nfs4frlock. 14019 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 14020 * COMPOUND4 args/res for calls that haven't already. 14021 */ 14022 static void 14023 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 14024 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 14025 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 14026 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 14027 short whence, u_offset_t offset, struct lm_sysid *ls, 14028 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 14029 bool_t did_start_fop, bool_t skip_get_err, 14030 cred_t *cred_otw, cred_t *cred) 14031 { 14032 mntinfo4_t *mi = VTOMI4(vp); 14033 rnode4_t *rp = VTOR4(vp); 14034 int error = *errorp; 14035 nfs_argop4 *argop; 14036 int do_flush_pages = 0; 14037 14038 ASSERT(nfs_zone() == mi->mi_zone); 14039 /* 14040 * The client recovery code wants the raw status information, 14041 * so don't map the NFS status code to an errno value for 14042 * non-normal call types. 14043 */ 14044 if (ctype == NFS4_LCK_CTYPE_NORM) { 14045 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 14046 *errorp = geterrno4(resp->status); 14047 if (did_start_fop == TRUE) 14048 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 14049 needrecov); 14050 14051 /* 14052 * We've established a new lock on the server, so invalidate 14053 * the pages associated with the vnode to get the most up to 14054 * date pages from the server after acquiring the lock. We 14055 * want to be sure that the read operation gets the newest data. 14056 * N.B. 14057 * We used to do this in nfs4frlock_results_ok but that doesn't 14058 * work since VOP_PUTPAGE can call nfs4_commit which calls 14059 * nfs4_start_fop. We flush the pages below after calling 14060 * nfs4_end_fop above 14061 * The flush of the page cache must be done after 14062 * nfs4_end_open_seqid_sync() to avoid a 4-way hang. 14063 */ 14064 if (!error && resp && resp->status == NFS4_OK) 14065 do_flush_pages = 1; 14066 } 14067 if (argsp) { 14068 ASSERT(argsp->array_len == 2); 14069 argop = argsp->array; 14070 if (argop[1].argop == OP_LOCK) 14071 nfs4args_lock_free(&argop[1]); 14072 else if (argop[1].argop == OP_LOCKT) 14073 nfs4args_lockt_free(&argop[1]); 14074 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14075 if (resp) 14076 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 14077 } 14078 14079 /* free the reference on the lock owner */ 14080 if (lop != NULL) { 14081 nfs4_end_lock_seqid_sync(lop); 14082 lock_owner_rele(lop); 14083 } 14084 14085 /* need to free up the reference on osp for lock args */ 14086 if (osp != NULL) 14087 open_stream_rele(osp, rp); 14088 14089 /* need to free up the reference on oop for lock args */ 14090 if (oop != NULL) { 14091 nfs4_end_open_seqid_sync(oop); 14092 open_owner_rele(oop); 14093 } 14094 14095 if (do_flush_pages) 14096 nfs4_flush_pages(vp, cred); 14097 14098 (void) convoff(vp, flk, whence, offset); 14099 14100 lm_rel_sysid(ls); 14101 14102 /* 14103 * Record debug information in the event we get EINVAL. 14104 */ 14105 mutex_enter(&mi->mi_lock); 14106 if (*errorp == EINVAL && (lock_args || locku_args) && 14107 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 14108 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 14109 zcmn_err(getzoneid(), CE_NOTE, 14110 "%s operation failed with " 14111 "EINVAL probably since the server, %s," 14112 " doesn't support POSIX style locking", 14113 lock_args ? "LOCK" : "LOCKU", 14114 mi->mi_curr_serv->sv_hostname); 14115 mi->mi_flags |= MI4_LOCK_DEBUG; 14116 } 14117 } 14118 mutex_exit(&mi->mi_lock); 14119 14120 if (cred_otw) 14121 crfree(cred_otw); 14122 } 14123 14124 /* 14125 * This calls the server and the local locking code. 14126 * 14127 * Client locks are registerred locally by oring the sysid with 14128 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 14129 * We need to distinguish between the two to avoid collision in case one 14130 * machine is used as both client and server. 14131 * 14132 * Blocking lock requests will continually retry to acquire the lock 14133 * forever. 14134 * 14135 * The ctype is defined as follows: 14136 * NFS4_LCK_CTYPE_NORM: normal lock request. 14137 * 14138 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 14139 * recovery, get the pid from flk instead of curproc, and don't reregister 14140 * the lock locally. 14141 * 14142 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 14143 * that we will use the information passed in via resend_rqstp to setup the 14144 * lock/locku request. This resend is the exact same request as the 'lost 14145 * lock', and is initiated by the recovery framework. A successful resend 14146 * request can initiate one or more reinstate requests. 14147 * 14148 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 14149 * does not trigger additional reinstate requests. This lock call type is 14150 * set for setting the v4 server's locking state back to match what the 14151 * client's local locking state is in the event of a received 'lost lock'. 14152 * 14153 * Errors are returned via the nfs4_error_t parameter. 14154 */ 14155 void 14156 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 14157 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 14158 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 14159 { 14160 COMPOUND4args_clnt args, *argsp = NULL; 14161 COMPOUND4res_clnt res, *resp = NULL; 14162 nfs_argop4 *argop; 14163 nfs_resop4 *resop; 14164 rnode4_t *rp; 14165 int doqueue = 1; 14166 clock_t tick_delay; /* delay in clock ticks */ 14167 struct lm_sysid *ls; 14168 LOCK4args *lock_args = NULL; 14169 LOCKU4args *locku_args = NULL; 14170 LOCKT4args *lockt_args = NULL; 14171 nfs4_open_owner_t *oop = NULL; 14172 nfs4_open_stream_t *osp = NULL; 14173 nfs4_lock_owner_t *lop = NULL; 14174 bool_t needrecov = FALSE; 14175 nfs4_recov_state_t recov_state; 14176 short whence; 14177 nfs4_op_hint_t op_hint; 14178 nfs4_lost_rqst_t lost_rqst; 14179 bool_t retry = FALSE; 14180 bool_t did_start_fop = FALSE; 14181 bool_t skip_get_err = FALSE; 14182 cred_t *cred_otw = NULL; 14183 bool_t recovonly; /* just queue request */ 14184 int frc_no_reclaim = 0; 14185 #ifdef DEBUG 14186 char *name; 14187 #endif 14188 14189 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14190 14191 #ifdef DEBUG 14192 name = fn_name(VTOSV(vp)->sv_name); 14193 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 14194 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 14195 "length %"PRIu64", pid %d, sysid %d, call type %s, " 14196 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 14197 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 14198 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 14199 resend_rqstp ? "TRUE" : "FALSE")); 14200 kmem_free(name, MAXNAMELEN); 14201 #endif 14202 14203 nfs4_error_zinit(ep); 14204 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14205 if (ep->error) 14206 return; 14207 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14208 if (ep->error) 14209 return; 14210 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14211 vp, cr, &cred_otw); 14212 14213 recov_retry: 14214 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14215 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14216 rp = VTOR4(vp); 14217 14218 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14219 &did_start_fop, &recovonly); 14220 14221 if (ep->error) 14222 goto out; 14223 14224 if (recovonly) { 14225 /* 14226 * Leave the request for the recovery system to deal with. 14227 */ 14228 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14229 ASSERT(cmd != F_GETLK); 14230 ASSERT(flk->l_type == F_UNLCK); 14231 14232 nfs4_error_init(ep, EINTR); 14233 needrecov = TRUE; 14234 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14235 if (lop != NULL) { 14236 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14237 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14238 (void) nfs4_start_recovery(ep, 14239 VTOMI4(vp), vp, NULL, NULL, 14240 (lost_rqst.lr_op == OP_LOCK || 14241 lost_rqst.lr_op == OP_LOCKU) ? 14242 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL); 14243 lock_owner_rele(lop); 14244 lop = NULL; 14245 } 14246 flk->l_pid = curproc->p_pid; 14247 nfs4_register_lock_locally(vp, flk, flag, offset); 14248 goto out; 14249 } 14250 14251 /* putfh directory fh */ 14252 argop[0].argop = OP_CPUTFH; 14253 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14254 14255 /* 14256 * Set up the over-the-wire arguments and get references to the 14257 * open owner, etc. 14258 */ 14259 14260 if (ctype == NFS4_LCK_CTYPE_RESEND || 14261 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14262 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14263 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14264 } else { 14265 bool_t go_otw = TRUE; 14266 14267 ASSERT(resend_rqstp == NULL); 14268 14269 switch (cmd) { 14270 case F_GETLK: 14271 case F_O_GETLK: 14272 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14273 &lockt_args, argsp, flk, rp); 14274 break; 14275 case F_SETLKW: 14276 case F_SETLK: 14277 if (flk->l_type == F_UNLCK) 14278 nfs4frlock_setup_locku_args(ctype, 14279 &argop[1], &locku_args, flk, 14280 &lop, ep, argsp, 14281 vp, flag, offset, cr, 14282 &skip_get_err, &go_otw); 14283 else 14284 nfs4frlock_setup_lock_args(ctype, 14285 &lock_args, &oop, &osp, &lop, &argop[1], 14286 argsp, flk, cmd, vp, cr, ep); 14287 14288 if (ep->error) 14289 goto out; 14290 14291 switch (ep->stat) { 14292 case NFS4_OK: 14293 break; 14294 case NFS4ERR_DELAY: 14295 /* recov thread never gets this error */ 14296 ASSERT(resend_rqstp == NULL); 14297 ASSERT(did_start_fop); 14298 14299 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14300 &recov_state, TRUE); 14301 did_start_fop = FALSE; 14302 if (argop[1].argop == OP_LOCK) 14303 nfs4args_lock_free(&argop[1]); 14304 else if (argop[1].argop == OP_LOCKT) 14305 nfs4args_lockt_free(&argop[1]); 14306 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14307 argsp = NULL; 14308 goto recov_retry; 14309 default: 14310 ep->error = EIO; 14311 goto out; 14312 } 14313 break; 14314 default: 14315 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14316 "nfs4_frlock: invalid cmd %d", cmd)); 14317 ep->error = EINVAL; 14318 goto out; 14319 } 14320 14321 if (!go_otw) 14322 goto out; 14323 } 14324 14325 /* XXX should we use the local reclock as a cache ? */ 14326 /* 14327 * Unregister the lock with the local locking code before 14328 * contacting the server. This avoids a potential race where 14329 * another process gets notified that it has been granted a lock 14330 * before we can unregister ourselves locally. 14331 */ 14332 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14333 if (ctype == NFS4_LCK_CTYPE_NORM) 14334 flk->l_pid = ttoproc(curthread)->p_pid; 14335 nfs4_register_lock_locally(vp, flk, flag, offset); 14336 } 14337 14338 /* 14339 * Send the server the lock request. Continually loop with a delay 14340 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14341 */ 14342 resp = &res; 14343 14344 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14345 (CE_NOTE, 14346 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14347 rnode4info(rp))); 14348 14349 if (lock_args && frc_no_reclaim) { 14350 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14351 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14352 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14353 lock_args->reclaim = FALSE; 14354 if (did_reclaimp) 14355 *did_reclaimp = 0; 14356 } 14357 14358 /* 14359 * Do the OTW call. 14360 */ 14361 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14362 14363 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14364 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14365 14366 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14367 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14368 "nfs4frlock: needrecov %d", needrecov)); 14369 14370 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14371 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14372 args.ctag); 14373 14374 /* 14375 * Check if one of these mutually exclusive error cases has 14376 * happened: 14377 * need to swap credentials due to access error 14378 * recovery is needed 14379 * different error (only known case is missing Kerberos ticket) 14380 */ 14381 14382 if ((ep->error == EACCES || 14383 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14384 cred_otw != cr) { 14385 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14386 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14387 cr, &cred_otw); 14388 goto recov_retry; 14389 } 14390 14391 if (needrecov) { 14392 /* 14393 * LOCKT requests don't need to recover from lost 14394 * requests since they don't create/modify state. 14395 */ 14396 if ((ep->error == EINTR || 14397 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14398 lockt_args) 14399 goto out; 14400 /* 14401 * Do not attempt recovery for requests initiated by 14402 * the recovery framework. Let the framework redrive them. 14403 */ 14404 if (ctype != NFS4_LCK_CTYPE_NORM) 14405 goto out; 14406 else { 14407 ASSERT(resend_rqstp == NULL); 14408 } 14409 14410 nfs4frlock_save_lost_rqst(ctype, ep->error, 14411 flk_to_locktype(cmd, flk->l_type), 14412 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14413 14414 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14415 &resp, lock_args, locku_args, &oop, &osp, &lop, 14416 rp, vp, &recov_state, op_hint, &did_start_fop, 14417 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14418 14419 if (retry) { 14420 ASSERT(oop == NULL); 14421 ASSERT(osp == NULL); 14422 ASSERT(lop == NULL); 14423 goto recov_retry; 14424 } 14425 goto out; 14426 } 14427 14428 /* 14429 * Bail out if have reached this point with ep->error set. Can 14430 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14431 * This happens if Kerberos ticket has expired or has been 14432 * destroyed. 14433 */ 14434 if (ep->error != 0) 14435 goto out; 14436 14437 /* 14438 * Process the reply. 14439 */ 14440 switch (resp->status) { 14441 case NFS4_OK: 14442 resop = &resp->array[1]; 14443 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14444 resend_rqstp); 14445 /* 14446 * Have a successful lock operation, now update state. 14447 */ 14448 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14449 resop, lop, vp, flk, cr, resend_rqstp); 14450 break; 14451 14452 case NFS4ERR_DENIED: 14453 resop = &resp->array[1]; 14454 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14455 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14456 &recov_state, needrecov, &argsp, &resp, 14457 &tick_delay, &whence, &ep->error, resop, cr, 14458 &did_start_fop, &skip_get_err); 14459 14460 if (retry) { 14461 ASSERT(oop == NULL); 14462 ASSERT(osp == NULL); 14463 ASSERT(lop == NULL); 14464 goto recov_retry; 14465 } 14466 break; 14467 /* 14468 * If the server won't let us reclaim, fall-back to trying to lock 14469 * the file from scratch. Code elsewhere will check the changeinfo 14470 * to ensure the file hasn't been changed. 14471 */ 14472 case NFS4ERR_NO_GRACE: 14473 if (lock_args && lock_args->reclaim == TRUE) { 14474 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14475 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14476 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14477 frc_no_reclaim = 1; 14478 /* clean up before retrying */ 14479 needrecov = 0; 14480 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14481 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14482 &recov_state, op_hint, &did_start_fop, NULL, flk); 14483 goto recov_retry; 14484 } 14485 /* FALLTHROUGH */ 14486 14487 default: 14488 nfs4frlock_results_default(resp, &ep->error); 14489 break; 14490 } 14491 out: 14492 /* 14493 * Process and cleanup from error. Make interrupted unlock 14494 * requests look successful, since they will be handled by the 14495 * client recovery code. 14496 */ 14497 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14498 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14499 lock_args, locku_args, did_start_fop, 14500 skip_get_err, cred_otw, cr); 14501 14502 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14503 (cmd == F_SETLK || cmd == F_SETLKW)) 14504 ep->error = 0; 14505 } 14506 14507 /* 14508 * nfs4_safelock: 14509 * 14510 * Return non-zero if the given lock request can be handled without 14511 * violating the constraints on concurrent mapping and locking. 14512 */ 14513 14514 static int 14515 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14516 { 14517 rnode4_t *rp = VTOR4(vp); 14518 struct vattr va; 14519 int error; 14520 14521 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14522 ASSERT(rp->r_mapcnt >= 0); 14523 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14524 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14525 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14526 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14527 14528 if (rp->r_mapcnt == 0) 14529 return (1); /* always safe if not mapped */ 14530 14531 /* 14532 * If the file is already mapped and there are locks, then they 14533 * should be all safe locks. So adding or removing a lock is safe 14534 * as long as the new request is safe (i.e., whole-file, meaning 14535 * length and starting offset are both zero). 14536 */ 14537 14538 if (bfp->l_start != 0 || bfp->l_len != 0) { 14539 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14540 "cannot lock a memory mapped file unless locking the " 14541 "entire file: start %"PRIx64", len %"PRIx64, 14542 bfp->l_start, bfp->l_len)); 14543 return (0); 14544 } 14545 14546 /* mandatory locking and mapping don't mix */ 14547 va.va_mask = AT_MODE; 14548 error = VOP_GETATTR(vp, &va, 0, cr, NULL); 14549 if (error != 0) { 14550 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14551 "getattr error %d", error)); 14552 return (0); /* treat errors conservatively */ 14553 } 14554 if (MANDLOCK(vp, va.va_mode)) { 14555 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14556 "cannot mandatory lock and mmap a file")); 14557 return (0); 14558 } 14559 14560 return (1); 14561 } 14562 14563 14564 /* 14565 * Register the lock locally within Solaris. 14566 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14567 * recording locks locally. 14568 * 14569 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14570 * are registered locally. 14571 */ 14572 void 14573 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14574 u_offset_t offset) 14575 { 14576 int oldsysid; 14577 int error; 14578 #ifdef DEBUG 14579 char *name; 14580 #endif 14581 14582 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14583 14584 #ifdef DEBUG 14585 name = fn_name(VTOSV(vp)->sv_name); 14586 NFS4_DEBUG(nfs4_client_lock_debug, 14587 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14588 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14589 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14590 flk->l_sysid)); 14591 kmem_free(name, MAXNAMELEN); 14592 #endif 14593 14594 /* register the lock with local locking */ 14595 oldsysid = flk->l_sysid; 14596 flk->l_sysid |= LM_SYSID_CLIENT; 14597 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14598 #ifdef DEBUG 14599 if (error != 0) { 14600 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14601 "nfs4_register_lock_locally: could not register with" 14602 " local locking")); 14603 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14604 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14605 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14606 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14607 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14608 flk->l_type, flk->l_start, flk->l_len)); 14609 (void) reclock(vp, flk, 0, flag, offset, NULL); 14610 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14611 "blocked by pid %d sysid 0x%x type %d " 14612 "off 0x%" PRIx64 " len 0x%" PRIx64, 14613 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14614 flk->l_len)); 14615 } 14616 #endif 14617 flk->l_sysid = oldsysid; 14618 } 14619 14620 /* 14621 * nfs4_lockrelease: 14622 * 14623 * Release any locks on the given vnode that are held by the current 14624 * process. Also removes the lock owner (if one exists) from the rnode's 14625 * list. 14626 */ 14627 static int 14628 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14629 { 14630 flock64_t ld; 14631 int ret, error; 14632 rnode4_t *rp; 14633 nfs4_lock_owner_t *lop; 14634 nfs4_recov_state_t recov_state; 14635 mntinfo4_t *mi; 14636 bool_t possible_orphan = FALSE; 14637 bool_t recovonly; 14638 14639 ASSERT((uintptr_t)vp > KERNELBASE); 14640 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14641 14642 rp = VTOR4(vp); 14643 mi = VTOMI4(vp); 14644 14645 /* 14646 * If we have not locked anything then we can 14647 * just return since we have no work to do. 14648 */ 14649 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14650 return (0); 14651 } 14652 14653 /* 14654 * We need to comprehend that another thread may 14655 * kick off recovery and the lock_owner we have stashed 14656 * in lop might be invalid so we should NOT cache it 14657 * locally! 14658 */ 14659 recov_state.rs_flags = 0; 14660 recov_state.rs_num_retry_despite_err = 0; 14661 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14662 &recovonly); 14663 if (error) { 14664 mutex_enter(&rp->r_statelock); 14665 rp->r_flags |= R4LODANGLERS; 14666 mutex_exit(&rp->r_statelock); 14667 return (error); 14668 } 14669 14670 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14671 14672 /* 14673 * Check if the lock owner might have a lock (request was sent but 14674 * no response was received). Also check if there are any remote 14675 * locks on the file. (In theory we shouldn't have to make this 14676 * second check if there's no lock owner, but for now we'll be 14677 * conservative and do it anyway.) If either condition is true, 14678 * send an unlock for the entire file to the server. 14679 * 14680 * Note that no explicit synchronization is needed here. At worst, 14681 * flk_has_remote_locks() will return a false positive, in which case 14682 * the unlock call wastes time but doesn't harm correctness. 14683 */ 14684 14685 if (lop) { 14686 mutex_enter(&lop->lo_lock); 14687 possible_orphan = lop->lo_pending_rqsts; 14688 mutex_exit(&lop->lo_lock); 14689 lock_owner_rele(lop); 14690 } 14691 14692 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14693 14694 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14695 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14696 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14697 (void *)lop)); 14698 14699 if (possible_orphan || flk_has_remote_locks(vp)) { 14700 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14701 ld.l_whence = 0; /* unlock from start of file */ 14702 ld.l_start = 0; 14703 ld.l_len = 0; /* do entire file */ 14704 14705 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, 14706 cr, NULL); 14707 14708 if (ret != 0) { 14709 /* 14710 * If VOP_FRLOCK fails, make sure we unregister 14711 * local locks before we continue. 14712 */ 14713 ld.l_pid = ttoproc(curthread)->p_pid; 14714 nfs4_register_lock_locally(vp, &ld, flag, offset); 14715 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14716 "nfs4_lockrelease: lock release error on vp" 14717 " %p: error %d.\n", (void *)vp, ret)); 14718 } 14719 } 14720 14721 recov_state.rs_flags = 0; 14722 recov_state.rs_num_retry_despite_err = 0; 14723 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14724 &recovonly); 14725 if (error) { 14726 mutex_enter(&rp->r_statelock); 14727 rp->r_flags |= R4LODANGLERS; 14728 mutex_exit(&rp->r_statelock); 14729 return (error); 14730 } 14731 14732 /* 14733 * So, here we're going to need to retrieve the lock-owner 14734 * again (in case recovery has done a switch-a-roo) and 14735 * remove it because we can. 14736 */ 14737 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14738 14739 if (lop) { 14740 nfs4_rnode_remove_lock_owner(rp, lop); 14741 lock_owner_rele(lop); 14742 } 14743 14744 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14745 return (0); 14746 } 14747 14748 /* 14749 * Wait for 'tick_delay' clock ticks. 14750 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14751 * 14752 * The client should retry to acquire the lock faster than the lease period. 14753 * We use roughly half of the lease time to use a similar calculation as it is 14754 * used in nfs4_renew_lease_thread(). 14755 * 14756 * XXX For future improvements, should implement a waiting queue scheme. 14757 */ 14758 static int 14759 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14760 { 14761 /* wait tick_delay clock ticks or siginteruptus */ 14762 if (delay_sig(*tick_delay)) { 14763 return (EINTR); 14764 } 14765 14766 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14767 "reissue the lock request: blocked for %ld clock ticks: %ld " 14768 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14769 14770 *tick_delay = MIN(drv_usectohz(nfs4_max_base_wait_time * 1000), 14771 *tick_delay * 1.5); 14772 return (0); 14773 } 14774 14775 void 14776 nfs4_vnops_init(void) 14777 { 14778 } 14779 14780 void 14781 nfs4_vnops_fini(void) 14782 { 14783 } 14784 14785 /* 14786 * Return a reference to the directory (parent) vnode for a given vnode, 14787 * using the saved pathname information and the directory file handle. The 14788 * caller is responsible for disposing of the reference. 14789 * Returns zero or an errno value. 14790 * 14791 * Caller should set need_start_op to FALSE if it is the recovery 14792 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14793 */ 14794 int 14795 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14796 { 14797 svnode_t *svnp; 14798 vnode_t *dvp = NULL; 14799 servinfo4_t *svp; 14800 nfs4_fname_t *mfname; 14801 int error; 14802 14803 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14804 14805 if (vp->v_flag & VROOT) { 14806 nfs4_sharedfh_t *sfh; 14807 nfs_fh4 fh; 14808 mntinfo4_t *mi; 14809 14810 ASSERT(vp->v_type == VREG); 14811 14812 mi = VTOMI4(vp); 14813 svp = mi->mi_curr_serv; 14814 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14815 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14816 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14817 sfh = sfh4_get(&fh, VTOMI4(vp)); 14818 nfs_rw_exit(&svp->sv_lock); 14819 mfname = mi->mi_fname; 14820 fn_hold(mfname); 14821 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14822 sfh4_rele(&sfh); 14823 14824 if (dvp->v_type == VNON) 14825 dvp->v_type = VDIR; 14826 *dvpp = dvp; 14827 return (0); 14828 } 14829 14830 svnp = VTOSV(vp); 14831 14832 if (svnp == NULL) { 14833 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14834 "shadow node is NULL")); 14835 return (EINVAL); 14836 } 14837 14838 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14839 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14840 "shadow node name or dfh val == NULL")); 14841 return (EINVAL); 14842 } 14843 14844 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14845 (int)need_start_op); 14846 if (error != 0) { 14847 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14848 "nfs4_make_dotdot returned %d", error)); 14849 return (error); 14850 } 14851 if (!dvp) { 14852 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14853 "nfs4_make_dotdot returned a NULL dvp")); 14854 return (EIO); 14855 } 14856 if (dvp->v_type == VNON) 14857 dvp->v_type = VDIR; 14858 ASSERT(dvp->v_type == VDIR); 14859 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14860 mutex_enter(&dvp->v_lock); 14861 dvp->v_flag |= V_XATTRDIR; 14862 mutex_exit(&dvp->v_lock); 14863 } 14864 *dvpp = dvp; 14865 return (0); 14866 } 14867 14868 /* 14869 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14870 * length that fnamep can accept, including the trailing null. 14871 * Returns 0 if okay, returns an errno value if there was a problem. 14872 */ 14873 14874 int 14875 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14876 { 14877 char *fn; 14878 int err = 0; 14879 servinfo4_t *svp; 14880 svnode_t *shvp; 14881 14882 /* 14883 * If the file being opened has VROOT set, then this is 14884 * a "file" mount. sv_name will not be interesting, so 14885 * go back to the servinfo4 to get the original mount 14886 * path and strip off all but the final edge. Otherwise 14887 * just return the name from the shadow vnode. 14888 */ 14889 14890 if (vp->v_flag & VROOT) { 14891 14892 svp = VTOMI4(vp)->mi_curr_serv; 14893 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14894 14895 fn = strrchr(svp->sv_path, '/'); 14896 if (fn == NULL) 14897 err = EINVAL; 14898 else 14899 fn++; 14900 } else { 14901 shvp = VTOSV(vp); 14902 fn = fn_name(shvp->sv_name); 14903 } 14904 14905 if (err == 0) 14906 if (strlen(fn) < maxlen) 14907 (void) strcpy(fnamep, fn); 14908 else 14909 err = ENAMETOOLONG; 14910 14911 if (vp->v_flag & VROOT) 14912 nfs_rw_exit(&svp->sv_lock); 14913 else 14914 kmem_free(fn, MAXNAMELEN); 14915 14916 return (err); 14917 } 14918 14919 /* 14920 * Bookkeeping for a close that doesn't need to go over the wire. 14921 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14922 * it is left at 1. 14923 */ 14924 void 14925 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14926 { 14927 rnode4_t *rp; 14928 mntinfo4_t *mi; 14929 14930 mi = VTOMI4(vp); 14931 rp = VTOR4(vp); 14932 14933 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14934 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14935 ASSERT(nfs_zone() == mi->mi_zone); 14936 ASSERT(mutex_owned(&osp->os_sync_lock)); 14937 ASSERT(*have_lockp); 14938 14939 if (!osp->os_valid || 14940 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14941 return; 14942 } 14943 14944 /* 14945 * This removes the reference obtained at OPEN; ie, 14946 * when the open stream structure was created. 14947 * 14948 * We don't have to worry about calling 'open_stream_rele' 14949 * since we our currently holding a reference to this 14950 * open stream which means the count can not go to 0 with 14951 * this decrement. 14952 */ 14953 ASSERT(osp->os_ref_count >= 2); 14954 osp->os_ref_count--; 14955 osp->os_valid = 0; 14956 mutex_exit(&osp->os_sync_lock); 14957 *have_lockp = 0; 14958 14959 nfs4_dec_state_ref_count(mi); 14960 } 14961 14962 /* 14963 * Close all remaining open streams on the rnode. These open streams 14964 * could be here because: 14965 * - The close attempted at either close or delmap failed 14966 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14967 * - Someone did mknod on a regular file but never opened it 14968 */ 14969 int 14970 nfs4close_all(vnode_t *vp, cred_t *cr) 14971 { 14972 nfs4_open_stream_t *osp; 14973 int error; 14974 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14975 rnode4_t *rp; 14976 14977 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14978 14979 error = 0; 14980 rp = VTOR4(vp); 14981 14982 /* 14983 * At this point, all we know is that the last time 14984 * someone called vn_rele, the count was 1. Since then, 14985 * the vnode could have been re-activated. We want to 14986 * loop through the open streams and close each one, but 14987 * we have to be careful since once we release the rnode 14988 * hash bucket lock, someone else is free to come in and 14989 * re-activate the rnode and add new open streams. The 14990 * strategy is take the rnode hash bucket lock, verify that 14991 * the count is still 1, grab the open stream off the 14992 * head of the list and mark it invalid, then release the 14993 * rnode hash bucket lock and proceed with that open stream. 14994 * This is ok because nfs4close_one() will acquire the proper 14995 * open/create to close/destroy synchronization for open 14996 * streams, and will ensure that if someone has reopened 14997 * the open stream after we've dropped the hash bucket lock 14998 * then we'll just simply return without destroying the 14999 * open stream. 15000 * Repeat until the list is empty. 15001 */ 15002 15003 for (;;) { 15004 15005 /* make sure vnode hasn't been reactivated */ 15006 rw_enter(&rp->r_hashq->r_lock, RW_READER); 15007 mutex_enter(&vp->v_lock); 15008 if (vp->v_count > 1) { 15009 mutex_exit(&vp->v_lock); 15010 rw_exit(&rp->r_hashq->r_lock); 15011 break; 15012 } 15013 /* 15014 * Grabbing r_os_lock before releasing v_lock prevents 15015 * a window where the rnode/open stream could get 15016 * reactivated (and os_force_close set to 0) before we 15017 * had a chance to set os_force_close to 1. 15018 */ 15019 mutex_enter(&rp->r_os_lock); 15020 mutex_exit(&vp->v_lock); 15021 15022 osp = list_head(&rp->r_open_streams); 15023 if (!osp) { 15024 /* nothing left to CLOSE OTW, so return */ 15025 mutex_exit(&rp->r_os_lock); 15026 rw_exit(&rp->r_hashq->r_lock); 15027 break; 15028 } 15029 15030 mutex_enter(&rp->r_statev4_lock); 15031 /* the file can't still be mem mapped */ 15032 ASSERT(rp->r_mapcnt == 0); 15033 if (rp->created_v4) 15034 rp->created_v4 = 0; 15035 mutex_exit(&rp->r_statev4_lock); 15036 15037 /* 15038 * Grab a ref on this open stream; nfs4close_one 15039 * will mark it as invalid 15040 */ 15041 mutex_enter(&osp->os_sync_lock); 15042 osp->os_ref_count++; 15043 osp->os_force_close = 1; 15044 mutex_exit(&osp->os_sync_lock); 15045 mutex_exit(&rp->r_os_lock); 15046 rw_exit(&rp->r_hashq->r_lock); 15047 15048 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 15049 15050 /* Update error if it isn't already non-zero */ 15051 if (error == 0) { 15052 if (e.error) 15053 error = e.error; 15054 else if (e.stat) 15055 error = geterrno4(e.stat); 15056 } 15057 15058 #ifdef DEBUG 15059 nfs4close_all_cnt++; 15060 #endif 15061 /* Release the ref on osp acquired above. */ 15062 open_stream_rele(osp, rp); 15063 15064 /* Proceed to the next open stream, if any */ 15065 } 15066 return (error); 15067 } 15068 15069 /* 15070 * nfs4close_one - close one open stream for a file if needed. 15071 * 15072 * "close_type" indicates which close path this is: 15073 * CLOSE_NORM: close initiated via VOP_CLOSE. 15074 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 15075 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 15076 * the close and release of client state for this open stream 15077 * (unless someone else has the open stream open). 15078 * CLOSE_RESEND: indicates the request is a replay of an earlier request 15079 * (e.g., due to abort because of a signal). 15080 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 15081 * 15082 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 15083 * recovery. Instead, the caller is expected to deal with retries. 15084 * 15085 * The caller can either pass in the osp ('provided_osp') or not. 15086 * 15087 * 'access_bits' represents the access we are closing/downgrading. 15088 * 15089 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 15090 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 15091 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 15092 * 15093 * Errors are returned via the nfs4_error_t. 15094 */ 15095 void 15096 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 15097 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 15098 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 15099 uint_t mmap_flags) 15100 { 15101 nfs4_open_owner_t *oop; 15102 nfs4_open_stream_t *osp = NULL; 15103 int retry = 0; 15104 int num_retries = NFS4_NUM_RECOV_RETRIES; 15105 rnode4_t *rp; 15106 mntinfo4_t *mi; 15107 nfs4_recov_state_t recov_state; 15108 cred_t *cred_otw = NULL; 15109 bool_t recovonly = FALSE; 15110 int isrecov; 15111 int force_close; 15112 int close_failed = 0; 15113 int did_dec_count = 0; 15114 int did_start_op = 0; 15115 int did_force_recovlock = 0; 15116 int did_start_seqid_sync = 0; 15117 int have_sync_lock = 0; 15118 15119 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15120 15121 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 15122 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 15123 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 15124 len, maxprot, mmap_flags, access_bits)); 15125 15126 nfs4_error_zinit(ep); 15127 rp = VTOR4(vp); 15128 mi = VTOMI4(vp); 15129 isrecov = (close_type == CLOSE_RESEND || 15130 close_type == CLOSE_AFTER_RESEND); 15131 15132 /* 15133 * First get the open owner. 15134 */ 15135 if (!provided_osp) { 15136 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 15137 } else { 15138 oop = provided_osp->os_open_owner; 15139 ASSERT(oop != NULL); 15140 open_owner_hold(oop); 15141 } 15142 15143 if (!oop) { 15144 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15145 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 15146 "close type %d", (void *)rp, (void *)mi, (void *)cr, 15147 (void *)provided_osp, close_type)); 15148 ep->error = EIO; 15149 goto out; 15150 } 15151 15152 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 15153 recov_retry: 15154 osp = NULL; 15155 close_failed = 0; 15156 force_close = (close_type == CLOSE_FORCE); 15157 retry = 0; 15158 did_start_op = 0; 15159 did_force_recovlock = 0; 15160 did_start_seqid_sync = 0; 15161 have_sync_lock = 0; 15162 recovonly = FALSE; 15163 recov_state.rs_flags = 0; 15164 recov_state.rs_num_retry_despite_err = 0; 15165 15166 /* 15167 * Second synchronize with recovery. 15168 */ 15169 if (!isrecov) { 15170 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 15171 &recov_state, &recovonly); 15172 if (!ep->error) { 15173 did_start_op = 1; 15174 } else { 15175 close_failed = 1; 15176 /* 15177 * If we couldn't get start_fop, but have to 15178 * cleanup state, then at least acquire the 15179 * mi_recovlock so we can synchronize with 15180 * recovery. 15181 */ 15182 if (close_type == CLOSE_FORCE) { 15183 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 15184 RW_READER, FALSE); 15185 did_force_recovlock = 1; 15186 } else 15187 goto out; 15188 } 15189 } 15190 15191 /* 15192 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15193 * set 'recovonly' to TRUE since most likely this is due to 15194 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15195 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15196 * to retry, causing us to loop until recovery finishes. Plus we 15197 * don't need protection over the open seqid since we're not going 15198 * OTW, hence don't need to use the seqid. 15199 */ 15200 if (recovonly == FALSE) { 15201 /* need to grab the open owner sync before 'os_sync_lock' */ 15202 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15203 if (ep->error == EAGAIN) { 15204 ASSERT(!isrecov); 15205 if (did_start_op) 15206 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15207 &recov_state, TRUE); 15208 if (did_force_recovlock) 15209 nfs_rw_exit(&mi->mi_recovlock); 15210 goto recov_retry; 15211 } 15212 did_start_seqid_sync = 1; 15213 } 15214 15215 /* 15216 * Third get an open stream and acquire 'os_sync_lock' to 15217 * sychronize the opening/creating of an open stream with the 15218 * closing/destroying of an open stream. 15219 */ 15220 if (!provided_osp) { 15221 /* returns with 'os_sync_lock' held */ 15222 osp = find_open_stream(oop, rp); 15223 if (!osp) { 15224 ep->error = EIO; 15225 goto out; 15226 } 15227 } else { 15228 osp = provided_osp; 15229 open_stream_hold(osp); 15230 mutex_enter(&osp->os_sync_lock); 15231 } 15232 have_sync_lock = 1; 15233 15234 ASSERT(oop == osp->os_open_owner); 15235 15236 /* 15237 * Fourth, do any special pre-OTW CLOSE processing 15238 * based on the specific close type. 15239 */ 15240 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15241 !did_dec_count) { 15242 ASSERT(osp->os_open_ref_count > 0); 15243 osp->os_open_ref_count--; 15244 did_dec_count = 1; 15245 if (osp->os_open_ref_count == 0) 15246 osp->os_final_close = 1; 15247 } 15248 15249 if (close_type == CLOSE_FORCE) { 15250 /* see if somebody reopened the open stream. */ 15251 if (!osp->os_force_close) { 15252 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15253 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15254 "was reopened, vp %p", (void *)osp, (void *)vp)); 15255 ep->error = 0; 15256 ep->stat = NFS4_OK; 15257 goto out; 15258 } 15259 15260 if (!osp->os_final_close && !did_dec_count) { 15261 osp->os_open_ref_count--; 15262 did_dec_count = 1; 15263 } 15264 15265 /* 15266 * We can't depend on os_open_ref_count being 0 due to the 15267 * way executables are opened (VN_RELE to match a VOP_OPEN). 15268 */ 15269 #ifdef NOTYET 15270 ASSERT(osp->os_open_ref_count == 0); 15271 #endif 15272 if (osp->os_open_ref_count != 0) { 15273 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15274 "nfs4close_one: should panic here on an " 15275 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15276 "since this is probably the exec problem.")); 15277 15278 osp->os_open_ref_count = 0; 15279 } 15280 15281 /* 15282 * There is the possibility that nfs4close_one() 15283 * for close_type == CLOSE_DELMAP couldn't find the 15284 * open stream, thus couldn't decrement its os_mapcnt; 15285 * therefore we can't use this ASSERT yet. 15286 */ 15287 #ifdef NOTYET 15288 ASSERT(osp->os_mapcnt == 0); 15289 #endif 15290 osp->os_mapcnt = 0; 15291 } 15292 15293 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15294 ASSERT(osp->os_mapcnt >= btopr(len)); 15295 15296 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15297 osp->os_mmap_write -= btopr(len); 15298 if (maxprot & PROT_READ) 15299 osp->os_mmap_read -= btopr(len); 15300 if (maxprot & PROT_EXEC) 15301 osp->os_mmap_read -= btopr(len); 15302 /* mirror the PROT_NONE check in nfs4_addmap() */ 15303 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15304 !(maxprot & PROT_EXEC)) 15305 osp->os_mmap_read -= btopr(len); 15306 osp->os_mapcnt -= btopr(len); 15307 did_dec_count = 1; 15308 } 15309 15310 if (recovonly) { 15311 nfs4_lost_rqst_t lost_rqst; 15312 15313 /* request should not already be in recovery queue */ 15314 ASSERT(lrp == NULL); 15315 nfs4_error_init(ep, EINTR); 15316 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15317 osp, cred_otw, vp); 15318 mutex_exit(&osp->os_sync_lock); 15319 have_sync_lock = 0; 15320 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15321 lost_rqst.lr_op == OP_CLOSE ? 15322 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL); 15323 close_failed = 1; 15324 force_close = 0; 15325 goto close_cleanup; 15326 } 15327 15328 /* 15329 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15330 * we stopped operating on the open owner's <old oo_name, old seqid> 15331 * space, which means we stopped operating on the open stream 15332 * too. So don't go OTW (as the seqid is likely bad, and the 15333 * stateid could be stale, potentially triggering a false 15334 * setclientid), and just clean up the client's internal state. 15335 */ 15336 if (osp->os_orig_oo_name != oop->oo_name) { 15337 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15338 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15339 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15340 "oo_name %" PRIx64")", 15341 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15342 oop->oo_name)); 15343 close_failed = 1; 15344 } 15345 15346 /* If the file failed recovery, just quit. */ 15347 mutex_enter(&rp->r_statelock); 15348 if (rp->r_flags & R4RECOVERR) { 15349 close_failed = 1; 15350 } 15351 mutex_exit(&rp->r_statelock); 15352 15353 /* 15354 * If the force close path failed to obtain start_fop 15355 * then skip the OTW close and just remove the state. 15356 */ 15357 if (close_failed) 15358 goto close_cleanup; 15359 15360 /* 15361 * Fifth, check to see if there are still mapped pages or other 15362 * opens using this open stream. If there are then we can't 15363 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15364 */ 15365 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15366 nfs4_lost_rqst_t new_lost_rqst; 15367 bool_t needrecov = FALSE; 15368 cred_t *odg_cred_otw = NULL; 15369 seqid4 open_dg_seqid = 0; 15370 15371 if (osp->os_delegation) { 15372 /* 15373 * If this open stream was never OPENed OTW then we 15374 * surely can't DOWNGRADE it (especially since the 15375 * osp->open_stateid is really a delegation stateid 15376 * when os_delegation is 1). 15377 */ 15378 if (access_bits & FREAD) 15379 osp->os_share_acc_read--; 15380 if (access_bits & FWRITE) 15381 osp->os_share_acc_write--; 15382 osp->os_share_deny_none--; 15383 nfs4_error_zinit(ep); 15384 goto out; 15385 } 15386 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15387 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15388 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15389 if (needrecov && !isrecov) { 15390 bool_t abort; 15391 nfs4_bseqid_entry_t *bsep = NULL; 15392 15393 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15394 bsep = nfs4_create_bseqid_entry(oop, NULL, 15395 vp, 0, 15396 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15397 open_dg_seqid); 15398 15399 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15400 oop, osp, odg_cred_otw, vp, access_bits, 0); 15401 mutex_exit(&osp->os_sync_lock); 15402 have_sync_lock = 0; 15403 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15404 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15405 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15406 bsep, NULL, NULL); 15407 if (odg_cred_otw) 15408 crfree(odg_cred_otw); 15409 if (bsep) 15410 kmem_free(bsep, sizeof (*bsep)); 15411 15412 if (abort == TRUE) 15413 goto out; 15414 15415 if (did_start_seqid_sync) { 15416 nfs4_end_open_seqid_sync(oop); 15417 did_start_seqid_sync = 0; 15418 } 15419 open_stream_rele(osp, rp); 15420 15421 if (did_start_op) 15422 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15423 &recov_state, FALSE); 15424 if (did_force_recovlock) 15425 nfs_rw_exit(&mi->mi_recovlock); 15426 15427 goto recov_retry; 15428 } else { 15429 if (odg_cred_otw) 15430 crfree(odg_cred_otw); 15431 } 15432 goto out; 15433 } 15434 15435 /* 15436 * If this open stream was created as the results of an open 15437 * while holding a delegation, then just release it; no need 15438 * to do an OTW close. Otherwise do a "normal" OTW close. 15439 */ 15440 if (osp->os_delegation) { 15441 nfs4close_notw(vp, osp, &have_sync_lock); 15442 nfs4_error_zinit(ep); 15443 goto out; 15444 } 15445 15446 /* 15447 * If this stream is not valid, we're done. 15448 */ 15449 if (!osp->os_valid) { 15450 nfs4_error_zinit(ep); 15451 goto out; 15452 } 15453 15454 /* 15455 * Last open or mmap ref has vanished, need to do an OTW close. 15456 * First check to see if a close is still necessary. 15457 */ 15458 if (osp->os_failed_reopen) { 15459 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15460 "don't close OTW osp %p since reopen failed.", 15461 (void *)osp)); 15462 /* 15463 * Reopen of the open stream failed, hence the 15464 * stateid of the open stream is invalid/stale, and 15465 * sending this OTW would incorrectly cause another 15466 * round of recovery. In this case, we need to set 15467 * the 'os_valid' bit to 0 so another thread doesn't 15468 * come in and re-open this open stream before 15469 * this "closing" thread cleans up state (decrementing 15470 * the nfs4_server_t's state_ref_count and decrementing 15471 * the os_ref_count). 15472 */ 15473 osp->os_valid = 0; 15474 /* 15475 * This removes the reference obtained at OPEN; ie, 15476 * when the open stream structure was created. 15477 * 15478 * We don't have to worry about calling 'open_stream_rele' 15479 * since we our currently holding a reference to this 15480 * open stream which means the count can not go to 0 with 15481 * this decrement. 15482 */ 15483 ASSERT(osp->os_ref_count >= 2); 15484 osp->os_ref_count--; 15485 nfs4_error_zinit(ep); 15486 close_failed = 0; 15487 goto close_cleanup; 15488 } 15489 15490 ASSERT(osp->os_ref_count > 1); 15491 15492 /* 15493 * Sixth, try the CLOSE OTW. 15494 */ 15495 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15496 close_type, ep, &have_sync_lock); 15497 15498 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15499 /* 15500 * Let the recovery thread be responsible for 15501 * removing the state for CLOSE. 15502 */ 15503 close_failed = 1; 15504 force_close = 0; 15505 retry = 0; 15506 } 15507 15508 /* See if we need to retry with a different cred */ 15509 if ((ep->error == EACCES || 15510 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15511 cred_otw != cr) { 15512 crfree(cred_otw); 15513 cred_otw = cr; 15514 crhold(cred_otw); 15515 retry = 1; 15516 } 15517 15518 if (ep->error || ep->stat) 15519 close_failed = 1; 15520 15521 if (retry && !isrecov && num_retries-- > 0) { 15522 if (have_sync_lock) { 15523 mutex_exit(&osp->os_sync_lock); 15524 have_sync_lock = 0; 15525 } 15526 if (did_start_seqid_sync) { 15527 nfs4_end_open_seqid_sync(oop); 15528 did_start_seqid_sync = 0; 15529 } 15530 open_stream_rele(osp, rp); 15531 15532 if (did_start_op) 15533 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15534 &recov_state, FALSE); 15535 if (did_force_recovlock) 15536 nfs_rw_exit(&mi->mi_recovlock); 15537 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15538 "nfs4close_one: need to retry the close " 15539 "operation")); 15540 goto recov_retry; 15541 } 15542 close_cleanup: 15543 /* 15544 * Seventh and lastly, process our results. 15545 */ 15546 if (close_failed && force_close) { 15547 /* 15548 * It's ok to drop and regrab the 'os_sync_lock' since 15549 * nfs4close_notw() will recheck to make sure the 15550 * "close"/removal of state should happen. 15551 */ 15552 if (!have_sync_lock) { 15553 mutex_enter(&osp->os_sync_lock); 15554 have_sync_lock = 1; 15555 } 15556 /* 15557 * This is last call, remove the ref on the open 15558 * stream created by open and clean everything up. 15559 */ 15560 osp->os_pending_close = 0; 15561 nfs4close_notw(vp, osp, &have_sync_lock); 15562 nfs4_error_zinit(ep); 15563 } 15564 15565 if (!close_failed) { 15566 if (have_sync_lock) { 15567 osp->os_pending_close = 0; 15568 mutex_exit(&osp->os_sync_lock); 15569 have_sync_lock = 0; 15570 } else { 15571 mutex_enter(&osp->os_sync_lock); 15572 osp->os_pending_close = 0; 15573 mutex_exit(&osp->os_sync_lock); 15574 } 15575 if (did_start_op && recov_state.rs_sp != NULL) { 15576 mutex_enter(&recov_state.rs_sp->s_lock); 15577 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15578 mutex_exit(&recov_state.rs_sp->s_lock); 15579 } else { 15580 nfs4_dec_state_ref_count(mi); 15581 } 15582 nfs4_error_zinit(ep); 15583 } 15584 15585 out: 15586 if (have_sync_lock) 15587 mutex_exit(&osp->os_sync_lock); 15588 if (did_start_op) 15589 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15590 recovonly ? TRUE : FALSE); 15591 if (did_force_recovlock) 15592 nfs_rw_exit(&mi->mi_recovlock); 15593 if (cred_otw) 15594 crfree(cred_otw); 15595 if (osp) 15596 open_stream_rele(osp, rp); 15597 if (oop) { 15598 if (did_start_seqid_sync) 15599 nfs4_end_open_seqid_sync(oop); 15600 open_owner_rele(oop); 15601 } 15602 } 15603 15604 /* 15605 * Convert information returned by the server in the LOCK4denied 15606 * structure to the form required by fcntl. 15607 */ 15608 static void 15609 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15610 { 15611 nfs4_lo_name_t *lo; 15612 15613 #ifdef DEBUG 15614 if (denied_to_flk_debug) { 15615 lockt_denied_debug = lockt_denied; 15616 debug_enter("lockt_denied"); 15617 } 15618 #endif 15619 15620 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15621 flk->l_whence = 0; /* aka SEEK_SET */ 15622 flk->l_start = lockt_denied->offset; 15623 flk->l_len = lockt_denied->length; 15624 15625 /* 15626 * If the blocking clientid matches our client id, then we can 15627 * interpret the lockowner (since we built it). If not, then 15628 * fabricate a sysid and pid. Note that the l_sysid field 15629 * in *flk already has the local sysid. 15630 */ 15631 15632 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15633 15634 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15635 lo = (nfs4_lo_name_t *) 15636 lockt_denied->owner.owner_val; 15637 15638 flk->l_pid = lo->ln_pid; 15639 } else { 15640 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15641 "denied_to_flk: bad lock owner length\n")); 15642 15643 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15644 } 15645 } else { 15646 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15647 "denied_to_flk: foreign clientid\n")); 15648 15649 /* 15650 * Construct a new sysid which should be different from 15651 * sysids of other systems. 15652 */ 15653 15654 flk->l_sysid++; 15655 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15656 } 15657 } 15658 15659 static pid_t 15660 lo_to_pid(lock_owner4 *lop) 15661 { 15662 pid_t pid = 0; 15663 uchar_t *cp; 15664 int i; 15665 15666 cp = (uchar_t *)&lop->clientid; 15667 15668 for (i = 0; i < sizeof (lop->clientid); i++) 15669 pid += (pid_t)*cp++; 15670 15671 cp = (uchar_t *)lop->owner_val; 15672 15673 for (i = 0; i < lop->owner_len; i++) 15674 pid += (pid_t)*cp++; 15675 15676 return (pid); 15677 } 15678 15679 /* 15680 * Given a lock pointer, returns the length of that lock. 15681 * "end" is the last locked offset the "l_len" covers from 15682 * the start of the lock. 15683 */ 15684 static off64_t 15685 lock_to_end(flock64_t *lock) 15686 { 15687 off64_t lock_end; 15688 15689 if (lock->l_len == 0) 15690 lock_end = (off64_t)MAXEND; 15691 else 15692 lock_end = lock->l_start + lock->l_len - 1; 15693 15694 return (lock_end); 15695 } 15696 15697 /* 15698 * Given the end of a lock, it will return you the length "l_len" for that lock. 15699 */ 15700 static off64_t 15701 end_to_len(off64_t start, off64_t end) 15702 { 15703 off64_t lock_len; 15704 15705 ASSERT(end >= start); 15706 if (end == MAXEND) 15707 lock_len = 0; 15708 else 15709 lock_len = end - start + 1; 15710 15711 return (lock_len); 15712 } 15713 15714 /* 15715 * On given end for a lock it determines if it is the last locked offset 15716 * or not, if so keeps it as is, else adds one to return the length for 15717 * valid start. 15718 */ 15719 static off64_t 15720 start_check(off64_t x) 15721 { 15722 if (x == MAXEND) 15723 return (x); 15724 else 15725 return (x + 1); 15726 } 15727 15728 /* 15729 * See if these two locks overlap, and if so return 1; 15730 * otherwise, return 0. 15731 */ 15732 static int 15733 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15734 { 15735 off64_t llfp_end, curfp_end; 15736 15737 llfp_end = lock_to_end(llfp); 15738 curfp_end = lock_to_end(curfp); 15739 15740 if (((llfp_end >= curfp->l_start) && 15741 (llfp->l_start <= curfp->l_start)) || 15742 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15743 return (1); 15744 return (0); 15745 } 15746 15747 /* 15748 * Determine what the intersecting lock region is, and add that to the 15749 * 'nl_llpp' locklist in increasing order (by l_start). 15750 */ 15751 static void 15752 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15753 locklist_t **nl_llpp, vnode_t *vp) 15754 { 15755 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15756 off64_t lost_flp_end, local_flp_end, len, start; 15757 15758 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15759 15760 if (!locks_intersect(lost_flp, local_flp)) 15761 return; 15762 15763 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15764 "locks intersect")); 15765 15766 lost_flp_end = lock_to_end(lost_flp); 15767 local_flp_end = lock_to_end(local_flp); 15768 15769 /* Find the starting point of the intersecting region */ 15770 if (local_flp->l_start > lost_flp->l_start) 15771 start = local_flp->l_start; 15772 else 15773 start = lost_flp->l_start; 15774 15775 /* Find the lenght of the intersecting region */ 15776 if (lost_flp_end < local_flp_end) 15777 len = end_to_len(start, lost_flp_end); 15778 else 15779 len = end_to_len(start, local_flp_end); 15780 15781 /* 15782 * Prepare the flock structure for the intersection found and insert 15783 * it into the new list in increasing l_start order. This list contains 15784 * intersections of locks registered by the client with the local host 15785 * and the lost lock. 15786 * The lock type of this lock is the same as that of the local_flp. 15787 */ 15788 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15789 intersect_llp->ll_flock.l_start = start; 15790 intersect_llp->ll_flock.l_len = len; 15791 intersect_llp->ll_flock.l_type = local_flp->l_type; 15792 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15793 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15794 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15795 intersect_llp->ll_vp = vp; 15796 15797 tmp_fllp = *nl_llpp; 15798 cur_fllp = NULL; 15799 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15800 intersect_llp->ll_flock.l_start) { 15801 cur_fllp = tmp_fllp; 15802 tmp_fllp = tmp_fllp->ll_next; 15803 } 15804 if (cur_fllp == NULL) { 15805 /* first on the list */ 15806 intersect_llp->ll_next = *nl_llpp; 15807 *nl_llpp = intersect_llp; 15808 } else { 15809 intersect_llp->ll_next = cur_fllp->ll_next; 15810 cur_fllp->ll_next = intersect_llp; 15811 } 15812 15813 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15814 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15815 intersect_llp->ll_flock.l_start, 15816 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15817 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15818 } 15819 15820 /* 15821 * Our local locking current state is potentially different than 15822 * what the NFSv4 server thinks we have due to a lost lock that was 15823 * resent and then received. We need to reset our "NFSv4" locking 15824 * state to match the current local locking state for this pid since 15825 * that is what the user/application sees as what the world is. 15826 * 15827 * We cannot afford to drop the open/lock seqid sync since then we can 15828 * get confused about what the current local locking state "is" versus 15829 * "was". 15830 * 15831 * If we are unable to fix up the locks, we send SIGLOST to the affected 15832 * process. This is not done if the filesystem has been forcibly 15833 * unmounted, in case the process has already exited and a new process 15834 * exists with the same pid. 15835 */ 15836 static void 15837 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15838 nfs4_lock_owner_t *lop) 15839 { 15840 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15841 mntinfo4_t *mi = VTOMI4(vp); 15842 const int cmd = F_SETLK; 15843 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15844 flock64_t ul_fl; 15845 15846 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15847 "nfs4_reinstitute_local_lock_state")); 15848 15849 /* 15850 * Find active locks for this vp from the local locking code. 15851 * Scan through this list and find out the locks that intersect with 15852 * the lost lock. Once we find the lock that intersects, add the 15853 * intersection area as a new lock to a new list "ri_llp". The lock 15854 * type of the intersection region lock added to ri_llp is the same 15855 * as that found in the active lock list, "list". The intersecting 15856 * region locks are added to ri_llp in increasing l_start order. 15857 */ 15858 ASSERT(nfs_zone() == mi->mi_zone); 15859 15860 locks = flk_active_locks_for_vp(vp); 15861 ri_llp = NULL; 15862 15863 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15864 ASSERT(llp->ll_vp == vp); 15865 /* 15866 * Pick locks that belong to this pid/lockowner 15867 */ 15868 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15869 continue; 15870 15871 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15872 } 15873 15874 /* 15875 * Now we have the list of intersections with the lost lock. These are 15876 * the locks that were/are active before the server replied to the 15877 * last/lost lock. Issue these locks to the server here. Playing these 15878 * locks to the server will re-establish aur current local locking state 15879 * with the v4 server. 15880 * If we get an error, send SIGLOST to the application for that lock. 15881 */ 15882 15883 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15884 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15885 "nfs4_reinstitute_local_lock_state: need to issue " 15886 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15887 llp->ll_flock.l_start, 15888 llp->ll_flock.l_start + llp->ll_flock.l_len, 15889 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15890 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15891 /* 15892 * No need to relock what we already have 15893 */ 15894 if (llp->ll_flock.l_type == lost_flp->l_type) 15895 continue; 15896 15897 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15898 } 15899 15900 /* 15901 * Now keeping the start of the lost lock as our reference parse the 15902 * newly created ri_llp locklist to find the ranges that we have locked 15903 * with the v4 server but not in the current local locking. We need 15904 * to unlock these ranges. 15905 * These ranges can also be reffered to as those ranges, where the lost 15906 * lock does not overlap with the locks in the ri_llp but are locked 15907 * since the server replied to the lost lock. 15908 */ 15909 cur_start = lost_flp->l_start; 15910 lost_flp_end = lock_to_end(lost_flp); 15911 15912 ul_fl.l_type = F_UNLCK; 15913 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15914 ul_fl.l_sysid = lost_flp->l_sysid; 15915 ul_fl.l_pid = lost_flp->l_pid; 15916 15917 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15918 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15919 15920 if (llp->ll_flock.l_start <= cur_start) { 15921 cur_start = start_check(llp_ll_flock_end); 15922 continue; 15923 } 15924 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15925 "nfs4_reinstitute_local_lock_state: " 15926 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15927 cur_start, llp->ll_flock.l_start)); 15928 15929 ul_fl.l_start = cur_start; 15930 ul_fl.l_len = end_to_len(cur_start, 15931 (llp->ll_flock.l_start - 1)); 15932 15933 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15934 cur_start = start_check(llp_ll_flock_end); 15935 } 15936 15937 /* 15938 * In the case where the lost lock ends after all intersecting locks, 15939 * unlock the last part of the lost lock range. 15940 */ 15941 if (cur_start != start_check(lost_flp_end)) { 15942 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15943 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15944 "lost lock region [%"PRIx64" - %"PRIx64"]", 15945 cur_start, lost_flp->l_start + lost_flp->l_len)); 15946 15947 ul_fl.l_start = cur_start; 15948 /* 15949 * Is it an to-EOF lock? if so unlock till the end 15950 */ 15951 if (lost_flp->l_len == 0) 15952 ul_fl.l_len = 0; 15953 else 15954 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15955 15956 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15957 } 15958 15959 if (locks != NULL) 15960 flk_free_locklist(locks); 15961 15962 /* Free up our newly created locklist */ 15963 for (llp = ri_llp; llp != NULL; ) { 15964 tmp_llp = llp->ll_next; 15965 kmem_free(llp, sizeof (locklist_t)); 15966 llp = tmp_llp; 15967 } 15968 15969 /* 15970 * Now return back to the original calling nfs4frlock() 15971 * and let us naturally drop our seqid syncs. 15972 */ 15973 } 15974 15975 /* 15976 * Create a lost state record for the given lock reinstantiation request 15977 * and push it onto the lost state queue. 15978 */ 15979 static void 15980 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15981 nfs4_lock_owner_t *lop) 15982 { 15983 nfs4_lost_rqst_t req; 15984 nfs_lock_type4 locktype; 15985 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15986 15987 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15988 15989 locktype = flk_to_locktype(cmd, flk->l_type); 15990 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 15991 NULL, NULL, lop, flk, &req, cr, vp); 15992 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 15993 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 15994 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 15995 NULL, NULL, NULL); 15996 } 15997