1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T. 28 * All Rights Reserved 29 */ 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/param.h> 34 #include <sys/types.h> 35 #include <sys/systm.h> 36 #include <sys/cred.h> 37 #include <sys/time.h> 38 #include <sys/vnode.h> 39 #include <sys/vfs.h> 40 #include <sys/vfs_opreg.h> 41 #include <sys/file.h> 42 #include <sys/filio.h> 43 #include <sys/uio.h> 44 #include <sys/buf.h> 45 #include <sys/mman.h> 46 #include <sys/pathname.h> 47 #include <sys/dirent.h> 48 #include <sys/debug.h> 49 #include <sys/vmsystm.h> 50 #include <sys/fcntl.h> 51 #include <sys/flock.h> 52 #include <sys/swap.h> 53 #include <sys/errno.h> 54 #include <sys/strsubr.h> 55 #include <sys/sysmacros.h> 56 #include <sys/kmem.h> 57 #include <sys/cmn_err.h> 58 #include <sys/pathconf.h> 59 #include <sys/utsname.h> 60 #include <sys/dnlc.h> 61 #include <sys/acl.h> 62 #include <sys/systeminfo.h> 63 #include <sys/policy.h> 64 #include <sys/sdt.h> 65 #include <sys/list.h> 66 #include <sys/stat.h> 67 68 #include <rpc/types.h> 69 #include <rpc/auth.h> 70 #include <rpc/clnt.h> 71 72 #include <nfs/nfs.h> 73 #include <nfs/nfs_clnt.h> 74 #include <nfs/nfs_acl.h> 75 #include <nfs/lm.h> 76 #include <nfs/nfs4.h> 77 #include <nfs/nfs4_kprot.h> 78 #include <nfs/rnode4.h> 79 #include <nfs/nfs4_clnt.h> 80 81 #include <vm/hat.h> 82 #include <vm/as.h> 83 #include <vm/page.h> 84 #include <vm/pvn.h> 85 #include <vm/seg.h> 86 #include <vm/seg_map.h> 87 #include <vm/seg_kpm.h> 88 #include <vm/seg_vn.h> 89 90 #include <fs/fs_subr.h> 91 92 #include <sys/ddi.h> 93 #include <sys/int_fmtio.h> 94 95 typedef struct { 96 nfs4_ga_res_t *di_garp; 97 cred_t *di_cred; 98 hrtime_t di_time_call; 99 } dirattr_info_t; 100 101 typedef enum nfs4_acl_op { 102 NFS4_ACL_GET, 103 NFS4_ACL_SET 104 } nfs4_acl_op_t; 105 106 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *, 107 char *, dirattr_info_t *); 108 109 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *, 110 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t, 111 nfs4_error_t *, int *); 112 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 113 cred_t *); 114 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 115 stable_how4 *); 116 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *, 117 cred_t *, bool_t, struct uio *); 118 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *, 119 vsecattr_t *); 120 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *); 121 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int); 122 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *); 123 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *); 124 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *); 125 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 126 int, vnode_t **, cred_t *); 127 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **, 128 cred_t *, int, int, enum createmode4, int); 129 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 130 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *, 131 vnode_t *, char *, cred_t *, nfsstat4 *); 132 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *, 133 vnode_t *, char *, cred_t *, nfsstat4 *); 134 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 135 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *); 136 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t); 137 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 138 page_t *[], size_t, struct seg *, caddr_t, 139 enum seg_rw, cred_t *); 140 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 141 cred_t *); 142 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 143 int, cred_t *); 144 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 145 int, cred_t *); 146 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *); 147 static void nfs4_set_mod(vnode_t *); 148 static void nfs4_get_commit(vnode_t *); 149 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t); 150 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 151 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int); 152 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3, 153 cred_t *); 154 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3, 155 cred_t *); 156 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *, 157 hrtime_t, vnode_t *, cred_t *); 158 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *); 159 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *); 160 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int, 161 u_offset_t); 162 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *); 163 static int nfs4_block_and_wait(clock_t *, rnode4_t *); 164 static cred_t *state_to_cred(nfs4_open_stream_t *); 165 static int vtoname(vnode_t *, char *, ssize_t); 166 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *); 167 static pid_t lo_to_pid(lock_owner4 *); 168 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *, 169 cred_t *, nfs4_lock_owner_t *); 170 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *, 171 nfs4_lock_owner_t *); 172 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **); 173 static void nfs4_delmap_callback(struct as *, void *, uint_t); 174 static void nfs4_free_delmapcall(nfs4_delmapcall_t *); 175 static nfs4_delmapcall_t *nfs4_init_delmapcall(); 176 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *); 177 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t); 178 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *, 179 uid_t, gid_t, int); 180 181 /* 182 * Routines that implement the setting of v4 args for the misc. ops 183 */ 184 static void nfs4args_lock_free(nfs_argop4 *); 185 static void nfs4args_lockt_free(nfs_argop4 *); 186 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *, 187 int, rnode4_t *, cred_t *, bitmap4, int *, 188 nfs4_stateid_types_t *); 189 static void nfs4args_setattr_free(nfs_argop4 *); 190 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4, 191 bitmap4); 192 static void nfs4args_verify_free(nfs_argop4 *); 193 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *, 194 WRITE4args **, nfs4_stateid_types_t *); 195 196 /* 197 * These are the vnode ops functions that implement the vnode interface to 198 * the networked file system. See more comments below at nfs4_vnodeops. 199 */ 200 static int nfs4_open(vnode_t **, int, cred_t *); 201 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *); 202 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *, 203 caller_context_t *); 204 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *, 205 caller_context_t *); 206 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *); 207 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *, 208 caller_context_t *); 209 static int nfs4_access(vnode_t *, int, int, cred_t *); 210 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *); 211 static int nfs4_fsync(vnode_t *, int, cred_t *); 212 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl, 213 int, vnode_t **, cred_t *, int); 214 static int nfs4_remove(vnode_t *, char *, cred_t *); 215 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *); 216 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 217 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, 218 vnode_t **, cred_t *); 219 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *); 220 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *, 221 cred_t *); 222 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *); 223 static int nfs4_seek(vnode_t *, offset_t, offset_t *); 224 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *, 225 page_t *[], size_t, struct seg *, caddr_t, 226 enum seg_rw, cred_t *); 227 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *); 228 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, 229 size_t, uchar_t, uchar_t, uint_t, cred_t *); 230 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, 231 size_t, uchar_t, uchar_t, uint_t, cred_t *); 232 static int nfs4_cmp(vnode_t *, vnode_t *); 233 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 234 struct flk_callback *, cred_t *); 235 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t, 236 cred_t *, caller_context_t *); 237 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, 238 size_t, uint_t, uint_t, uint_t, cred_t *); 239 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 240 cred_t *); 241 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *); 242 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 243 static int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *); 244 /* 245 * These vnode ops are required to be called from outside this source file, 246 * e.g. by ephemeral mount stub vnode ops, and so may not be declared 247 * as static. 248 */ 249 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *); 250 void nfs4_inactive(vnode_t *, cred_t *); 251 int nfs4_lookup(vnode_t *, char *, vnode_t **, 252 struct pathname *, int, vnode_t *, cred_t *); 253 int nfs4_fid(vnode_t *, fid_t *); 254 int nfs4_rwlock(vnode_t *, int, caller_context_t *); 255 void nfs4_rwunlock(vnode_t *, int, caller_context_t *); 256 int nfs4_realvp(vnode_t *, vnode_t **); 257 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *); 258 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 259 260 /* 261 * Used for nfs4_commit_vp() to indicate if we should 262 * wait on pending writes. 263 */ 264 #define NFS4_WRITE_NOWAIT 0 265 #define NFS4_WRITE_WAIT 1 266 267 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */ 268 269 /* 270 * Error flags used to pass information about certain special errors 271 * which need to be handled specially. 272 */ 273 #define NFS_EOF -98 274 #define NFS_VERF_MISMATCH -97 275 276 /* 277 * Flags used to differentiate between which operation drove the 278 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary) 279 */ 280 #define NFS4_CLOSE_OP 0x1 281 #define NFS4_DELMAP_OP 0x2 282 #define NFS4_INACTIVE_OP 0x3 283 284 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO)) 285 286 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 287 #define ALIGN64(x, ptr, sz) \ 288 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 289 if (x) { \ 290 x = sizeof (uint64_t) - (x); \ 291 sz -= (x); \ 292 ptr += (x); \ 293 } 294 295 #ifdef DEBUG 296 int nfs4_client_attr_debug = 0; 297 int nfs4_client_state_debug = 0; 298 int nfs4_client_shadow_debug = 0; 299 int nfs4_client_lock_debug = 0; 300 int nfs4_seqid_sync = 0; 301 int nfs4_client_map_debug = 0; 302 static int nfs4_pageio_debug = 0; 303 int nfs4_client_inactive_debug = 0; 304 int nfs4_client_recov_debug = 0; 305 int nfs4_client_failover_debug = 0; 306 int nfs4_client_call_debug = 0; 307 int nfs4_client_lookup_debug = 0; 308 int nfs4_client_zone_debug = 0; 309 int nfs4_lost_rqst_debug = 0; 310 int nfs4_rdattrerr_debug = 0; 311 int nfs4_open_stream_debug = 0; 312 313 int nfs4read_error_inject; 314 315 static int nfs4_create_misses = 0; 316 317 static int nfs4_readdir_cache_shorts = 0; 318 static int nfs4_readdir_readahead = 0; 319 320 static int nfs4_bio_do_stop = 0; 321 322 static int nfs4_lostpage = 0; /* number of times we lost original page */ 323 324 int nfs4_mmap_debug = 0; 325 326 static int nfs4_pathconf_cache_hits = 0; 327 static int nfs4_pathconf_cache_misses = 0; 328 329 int nfs4close_all_cnt; 330 int nfs4close_one_debug = 0; 331 int nfs4close_notw_debug = 0; 332 333 int denied_to_flk_debug = 0; 334 void *lockt_denied_debug; 335 336 #endif 337 338 /* 339 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT 340 * or NFS4ERR_RESOURCE. 341 */ 342 static int confirm_retry_sec = 30; 343 344 static int nfs4_lookup_neg_cache = 1; 345 346 /* 347 * number of pages to read ahead 348 * optimized for 100 base-T. 349 */ 350 static int nfs4_nra = 4; 351 352 static int nfs4_do_symlink_cache = 1; 353 354 static int nfs4_pathconf_disable_cache = 0; 355 356 /* 357 * These are the vnode ops routines which implement the vnode interface to 358 * the networked file system. These routines just take their parameters, 359 * make them look networkish by putting the right info into interface structs, 360 * and then calling the appropriate remote routine(s) to do the work. 361 * 362 * Note on directory name lookup cacheing: If we detect a stale fhandle, 363 * we purge the directory cache relative to that vnode. This way, the 364 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for 365 * more details on rnode locking. 366 */ 367 368 struct vnodeops *nfs4_vnodeops; 369 370 const fs_operation_def_t nfs4_vnodeops_template[] = { 371 VOPNAME_OPEN, { .vop_open = nfs4_open }, 372 VOPNAME_CLOSE, { .vop_close = nfs4_close }, 373 VOPNAME_READ, { .vop_read = nfs4_read }, 374 VOPNAME_WRITE, { .vop_write = nfs4_write }, 375 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl }, 376 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr }, 377 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr }, 378 VOPNAME_ACCESS, { .vop_access = nfs4_access }, 379 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup }, 380 VOPNAME_CREATE, { .vop_create = nfs4_create }, 381 VOPNAME_REMOVE, { .vop_remove = nfs4_remove }, 382 VOPNAME_LINK, { .vop_link = nfs4_link }, 383 VOPNAME_RENAME, { .vop_rename = nfs4_rename }, 384 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir }, 385 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir }, 386 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir }, 387 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink }, 388 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink }, 389 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync }, 390 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive }, 391 VOPNAME_FID, { .vop_fid = nfs4_fid }, 392 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock }, 393 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock }, 394 VOPNAME_SEEK, { .vop_seek = nfs4_seek }, 395 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock }, 396 VOPNAME_SPACE, { .vop_space = nfs4_space }, 397 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp }, 398 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage }, 399 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage }, 400 VOPNAME_MAP, { .vop_map = nfs4_map }, 401 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap }, 402 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap }, 403 /* no separate nfs4_dump */ 404 VOPNAME_DUMP, { .vop_dump = nfs_dump }, 405 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf }, 406 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio }, 407 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose }, 408 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr }, 409 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr }, 410 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock }, 411 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 412 NULL, NULL 413 }; 414 415 /* 416 * The following are subroutines and definitions to set args or get res 417 * for the different nfsv4 ops 418 */ 419 420 void 421 nfs4args_lookup_free(nfs_argop4 *argop, int arglen) 422 { 423 int i; 424 425 for (i = 0; i < arglen; i++) { 426 if (argop[i].argop == OP_LOOKUP) { 427 kmem_free( 428 argop[i].nfs_argop4_u.oplookup. 429 objname.utf8string_val, 430 argop[i].nfs_argop4_u.oplookup. 431 objname.utf8string_len); 432 } 433 } 434 } 435 436 static void 437 nfs4args_lock_free(nfs_argop4 *argop) 438 { 439 locker4 *locker = &argop->nfs_argop4_u.oplock.locker; 440 441 if (locker->new_lock_owner == TRUE) { 442 open_to_lock_owner4 *open_owner; 443 444 open_owner = &locker->locker4_u.open_owner; 445 if (open_owner->lock_owner.owner_val != NULL) { 446 kmem_free(open_owner->lock_owner.owner_val, 447 open_owner->lock_owner.owner_len); 448 } 449 } 450 } 451 452 static void 453 nfs4args_lockt_free(nfs_argop4 *argop) 454 { 455 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner; 456 457 if (lowner->owner_val != NULL) { 458 kmem_free(lowner->owner_val, lowner->owner_len); 459 } 460 } 461 462 static void 463 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags, 464 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error, 465 nfs4_stateid_types_t *sid_types) 466 { 467 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes; 468 mntinfo4_t *mi; 469 470 argop->argop = OP_SETATTR; 471 /* 472 * The stateid is set to 0 if client is not modifying the size 473 * and otherwise to whatever nfs4_get_stateid() returns. 474 * 475 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no 476 * state struct could be found for the process/file pair. We may 477 * want to change this in the future (by OPENing the file). See 478 * bug # 4474852. 479 */ 480 if (vap->va_mask & AT_SIZE) { 481 482 ASSERT(rp != NULL); 483 mi = VTOMI4(RTOV4(rp)); 484 485 argop->nfs_argop4_u.opsetattr.stateid = 486 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 487 OP_SETATTR, sid_types, FALSE); 488 } else { 489 bzero(&argop->nfs_argop4_u.opsetattr.stateid, 490 sizeof (stateid4)); 491 } 492 493 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp); 494 if (*error) 495 bzero(attr, sizeof (*attr)); 496 } 497 498 static void 499 nfs4args_setattr_free(nfs_argop4 *argop) 500 { 501 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes); 502 } 503 504 static int 505 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op, 506 bitmap4 supp) 507 { 508 fattr4 *attr; 509 int error = 0; 510 511 argop->argop = op; 512 switch (op) { 513 case OP_VERIFY: 514 attr = &argop->nfs_argop4_u.opverify.obj_attributes; 515 break; 516 case OP_NVERIFY: 517 attr = &argop->nfs_argop4_u.opnverify.obj_attributes; 518 break; 519 default: 520 return (EINVAL); 521 } 522 if (!error) 523 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp); 524 if (error) 525 bzero(attr, sizeof (*attr)); 526 return (error); 527 } 528 529 static void 530 nfs4args_verify_free(nfs_argop4 *argop) 531 { 532 switch (argop->argop) { 533 case OP_VERIFY: 534 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes); 535 break; 536 case OP_NVERIFY: 537 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes); 538 break; 539 default: 540 break; 541 } 542 } 543 544 static void 545 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr, 546 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp) 547 { 548 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite; 549 mntinfo4_t *mi = VTOMI4(RTOV4(rp)); 550 551 argop->argop = OP_WRITE; 552 wargs->stable = stable; 553 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id, 554 mi, OP_WRITE, sid_tp); 555 wargs->mblk = NULL; 556 *wargs_pp = wargs; 557 } 558 559 void 560 nfs4args_copen_free(OPEN4cargs *open_args) 561 { 562 if (open_args->owner.owner_val) { 563 kmem_free(open_args->owner.owner_val, 564 open_args->owner.owner_len); 565 } 566 if ((open_args->opentype == OPEN4_CREATE) && 567 (open_args->mode != EXCLUSIVE4)) { 568 nfs4_fattr4_free(&open_args->createhow4_u.createattrs); 569 } 570 } 571 572 /* 573 * XXX: This is referenced in modstubs.s 574 */ 575 struct vnodeops * 576 nfs4_getvnodeops(void) 577 { 578 return (nfs4_vnodeops); 579 } 580 581 /* 582 * The OPEN operation opens a regular file. 583 * 584 * ARGSUSED 585 */ 586 static int 587 nfs4_open(vnode_t **vpp, int flag, cred_t *cr) 588 { 589 vnode_t *dvp = NULL; 590 rnode4_t *rp, *drp; 591 int error; 592 int just_been_created; 593 char fn[MAXNAMELEN]; 594 595 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: ")); 596 if (nfs_zone() != VTOMI4(*vpp)->mi_zone) 597 return (EIO); 598 rp = VTOR4(*vpp); 599 600 /* 601 * Check to see if opening something besides a regular file; 602 * if so skip the OTW call 603 */ 604 if ((*vpp)->v_type != VREG) { 605 error = nfs4_open_non_reg_file(vpp, flag, cr); 606 return (error); 607 } 608 609 /* 610 * XXX - would like a check right here to know if the file is 611 * executable or not, so as to skip OTW 612 */ 613 614 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0) 615 return (error); 616 617 drp = VTOR4(dvp); 618 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 619 return (EINTR); 620 621 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) { 622 nfs_rw_exit(&drp->r_rwlock); 623 return (error); 624 } 625 626 /* 627 * See if this file has just been CREATEd. 628 * If so, clear the flag and update the dnlc, which was previously 629 * skipped in nfs4_create. 630 * XXX need better serilization on this. 631 * XXX move this into the nf4open_otw call, after we have 632 * XXX acquired the open owner seqid sync. 633 */ 634 mutex_enter(&rp->r_statev4_lock); 635 if (rp->created_v4) { 636 rp->created_v4 = 0; 637 mutex_exit(&rp->r_statev4_lock); 638 639 dnlc_update(dvp, fn, *vpp); 640 /* This is needed so we don't bump the open ref count */ 641 just_been_created = 1; 642 } else { 643 mutex_exit(&rp->r_statev4_lock); 644 just_been_created = 0; 645 } 646 647 /* 648 * If caller specified O_TRUNC/FTRUNC, then be sure to set 649 * FWRITE (to drive successful setattr(size=0) after open) 650 */ 651 if (flag & FTRUNC) 652 flag |= FWRITE; 653 654 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0, 655 just_been_created); 656 657 if (!error && !((*vpp)->v_flag & VROOT)) 658 dnlc_update(dvp, fn, *vpp); 659 660 nfs_rw_exit(&drp->r_rwlock); 661 662 /* release the hold from vtodv */ 663 VN_RELE(dvp); 664 665 /* exchange the shadow for the master vnode, if needed */ 666 667 if (error == 0 && IS_SHADOW(*vpp, rp)) 668 sv_exchange(vpp); 669 670 return (error); 671 } 672 673 /* 674 * See if there's a "lost open" request to be saved and recovered. 675 */ 676 static void 677 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 678 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp, 679 vnode_t *dvp, OPEN4cargs *open_args) 680 { 681 vfs_t *vfsp; 682 char *srccfp; 683 684 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp); 685 686 if (error != ETIMEDOUT && error != EINTR && 687 !NFS4_FRC_UNMT_ERR(error, vfsp)) { 688 lost_rqstp->lr_op = 0; 689 return; 690 } 691 692 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 693 "nfs4open_save_lost_rqst: error %d", error)); 694 695 lost_rqstp->lr_op = OP_OPEN; 696 697 /* 698 * The vp (if it is not NULL) and dvp are held and rele'd via 699 * the recovery code. See nfs4_save_lost_rqst. 700 */ 701 lost_rqstp->lr_vp = vp; 702 lost_rqstp->lr_dvp = dvp; 703 lost_rqstp->lr_oop = oop; 704 lost_rqstp->lr_osp = NULL; 705 lost_rqstp->lr_lop = NULL; 706 lost_rqstp->lr_cr = cr; 707 lost_rqstp->lr_flk = NULL; 708 lost_rqstp->lr_oacc = open_args->share_access; 709 lost_rqstp->lr_odeny = open_args->share_deny; 710 lost_rqstp->lr_oclaim = open_args->claim; 711 if (open_args->claim == CLAIM_DELEGATE_CUR) { 712 lost_rqstp->lr_ostateid = 713 open_args->open_claim4_u.delegate_cur_info.delegate_stateid; 714 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile; 715 } else { 716 srccfp = open_args->open_claim4_u.cfile; 717 } 718 lost_rqstp->lr_ofile.utf8string_len = 0; 719 lost_rqstp->lr_ofile.utf8string_val = NULL; 720 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile); 721 lost_rqstp->lr_putfirst = FALSE; 722 } 723 724 struct nfs4_excl_time { 725 uint32 seconds; 726 uint32 nseconds; 727 }; 728 729 /* 730 * The OPEN operation creates and/or opens a regular file 731 * 732 * ARGSUSED 733 */ 734 static int 735 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va, 736 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag, 737 enum createmode4 createmode, int file_just_been_created) 738 { 739 rnode4_t *rp; 740 rnode4_t *drp = VTOR4(dvp); 741 vnode_t *vp = NULL; 742 vnode_t *vpi = *vpp; 743 bool_t needrecov = FALSE; 744 745 int doqueue = 1; 746 747 COMPOUND4args_clnt args; 748 COMPOUND4res_clnt res; 749 nfs_argop4 *argop; 750 nfs_resop4 *resop; 751 int argoplist_size; 752 int idx_open, idx_fattr; 753 754 GETFH4res *gf_res = NULL; 755 OPEN4res *op_res = NULL; 756 nfs4_ga_res_t *garp; 757 fattr4 *attr = NULL; 758 struct nfs4_excl_time verf; 759 bool_t did_excl_setup = FALSE; 760 int created_osp; 761 762 OPEN4cargs *open_args; 763 nfs4_open_owner_t *oop = NULL; 764 nfs4_open_stream_t *osp = NULL; 765 seqid4 seqid = 0; 766 bool_t retry_open = FALSE; 767 nfs4_recov_state_t recov_state; 768 nfs4_lost_rqst_t lost_rqst; 769 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 770 hrtime_t t; 771 int acc = 0; 772 cred_t *cred_otw = NULL; /* cred used to do the RPC call */ 773 cred_t *ncr = NULL; 774 775 nfs4_sharedfh_t *otw_sfh; 776 nfs4_sharedfh_t *orig_sfh; 777 int fh_differs = 0; 778 int numops, setgid_flag; 779 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1; 780 781 /* 782 * Make sure we properly deal with setting the right gid on 783 * a newly created file to reflect the parent's setgid bit 784 */ 785 setgid_flag = 0; 786 if (create_flag && in_va) { 787 788 /* 789 * If the parent's directory has the setgid bit set 790 * _and_ the client was able to get a valid mapping 791 * for the parent dir's owner_group, we want to 792 * append NVERIFY(owner_group == dva.va_gid) and 793 * SETATTR to the CREATE compound. 794 */ 795 mutex_enter(&drp->r_statelock); 796 if (drp->r_attr.va_mode & VSGID && 797 drp->r_attr.va_gid != GID_NOBODY) { 798 in_va->va_gid = drp->r_attr.va_gid; 799 setgid_flag = 1; 800 } 801 mutex_exit(&drp->r_statelock); 802 } 803 804 /* 805 * Normal/non-create compound: 806 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) 807 * 808 * Open(create) compound no setgid: 809 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) + 810 * RESTOREFH + GETATTR 811 * 812 * Open(create) setgid: 813 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) + 814 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH + 815 * NVERIFY(grp) + SETATTR 816 */ 817 if (setgid_flag) { 818 numops = 10; 819 idx_open = 1; 820 idx_fattr = 3; 821 } else if (create_flag) { 822 numops = 7; 823 idx_open = 2; 824 idx_fattr = 4; 825 } else { 826 numops = 4; 827 idx_open = 1; 828 idx_fattr = 3; 829 } 830 831 args.array_len = numops; 832 argoplist_size = numops * sizeof (nfs_argop4); 833 argop = kmem_alloc(argoplist_size, KM_SLEEP); 834 835 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: " 836 "open %s open flag 0x%x cred %p", file_name, open_flag, 837 (void *)cr)); 838 839 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 840 if (create_flag) { 841 /* 842 * We are to create a file. Initialize the passed in vnode 843 * pointer. 844 */ 845 vpi = NULL; 846 } else { 847 /* 848 * Check to see if the client owns a read delegation and is 849 * trying to open for write. If so, then return the delegation 850 * to avoid the server doing a cb_recall and returning DELAY. 851 * NB - we don't use the statev4_lock here because we'd have 852 * to drop the lock anyway and the result would be stale. 853 */ 854 if ((open_flag & FWRITE) && 855 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ) 856 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN); 857 858 /* 859 * If the file has a delegation, then do an access check up 860 * front. This avoids having to an access check later after 861 * we've already done start_op, which could deadlock. 862 */ 863 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) { 864 if (open_flag & FREAD && 865 nfs4_access(vpi, VREAD, 0, cr) == 0) 866 acc |= VREAD; 867 if (open_flag & FWRITE && 868 nfs4_access(vpi, VWRITE, 0, cr) == 0) 869 acc |= VWRITE; 870 } 871 } 872 873 drp = VTOR4(dvp); 874 875 recov_state.rs_flags = 0; 876 recov_state.rs_num_retry_despite_err = 0; 877 cred_otw = cr; 878 879 recov_retry: 880 fh_differs = 0; 881 nfs4_error_zinit(&e); 882 883 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state); 884 if (e.error) { 885 if (ncr != NULL) 886 crfree(ncr); 887 kmem_free(argop, argoplist_size); 888 return (e.error); 889 } 890 891 args.ctag = TAG_OPEN; 892 args.array_len = numops; 893 args.array = argop; 894 895 /* putfh directory fh */ 896 argop[0].argop = OP_CPUTFH; 897 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 898 899 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */ 900 argop[idx_open].argop = OP_COPEN; 901 open_args = &argop[idx_open].nfs_argop4_u.opcopen; 902 open_args->claim = CLAIM_NULL; 903 904 /* name of file */ 905 open_args->open_claim4_u.cfile = file_name; 906 open_args->owner.owner_len = 0; 907 open_args->owner.owner_val = NULL; 908 909 if (create_flag) { 910 /* CREATE a file */ 911 open_args->opentype = OPEN4_CREATE; 912 open_args->mode = createmode; 913 if (createmode == EXCLUSIVE4) { 914 if (did_excl_setup == FALSE) { 915 verf.seconds = nfs_atoi(hw_serial); 916 if (verf.seconds != 0) 917 verf.nseconds = newnum(); 918 else { 919 timestruc_t now; 920 921 gethrestime(&now); 922 verf.seconds = now.tv_sec; 923 verf.nseconds = now.tv_nsec; 924 } 925 /* 926 * Since the server will use this value for the 927 * mtime, make sure that it can't overflow. Zero 928 * out the MSB. The actual value does not matter 929 * here, only its uniqeness. 930 */ 931 verf.seconds &= INT32_MAX; 932 did_excl_setup = TRUE; 933 } 934 935 /* Now copy over verifier to OPEN4args. */ 936 open_args->createhow4_u.createverf = *(uint64_t *)&verf; 937 } else { 938 int v_error; 939 bitmap4 supp_attrs; 940 servinfo4_t *svp; 941 942 attr = &open_args->createhow4_u.createattrs; 943 944 svp = drp->r_server; 945 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 946 supp_attrs = svp->sv_supp_attrs; 947 nfs_rw_exit(&svp->sv_lock); 948 949 /* GUARDED4 or UNCHECKED4 */ 950 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN, 951 supp_attrs); 952 if (v_error) { 953 bzero(attr, sizeof (*attr)); 954 nfs4args_copen_free(open_args); 955 nfs4_end_op(VTOMI4(dvp), dvp, vpi, 956 &recov_state, FALSE); 957 if (ncr != NULL) 958 crfree(ncr); 959 kmem_free(argop, argoplist_size); 960 return (v_error); 961 } 962 } 963 } else { 964 /* NO CREATE */ 965 open_args->opentype = OPEN4_NOCREATE; 966 } 967 968 if (recov_state.rs_sp != NULL) { 969 mutex_enter(&recov_state.rs_sp->s_lock); 970 open_args->owner.clientid = recov_state.rs_sp->clientid; 971 mutex_exit(&recov_state.rs_sp->s_lock); 972 } else { 973 /* XXX should we just fail here? */ 974 open_args->owner.clientid = 0; 975 } 976 977 /* 978 * This increments oop's ref count or creates a temporary 'just_created' 979 * open owner that will become valid when this OPEN/OPEN_CONFIRM call 980 * completes. 981 */ 982 mutex_enter(&VTOMI4(dvp)->mi_lock); 983 984 /* See if a permanent or just created open owner exists */ 985 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp)); 986 if (!oop) { 987 /* 988 * This open owner does not exist so create a temporary 989 * just created one. 990 */ 991 oop = create_open_owner(cr, VTOMI4(dvp)); 992 ASSERT(oop != NULL); 993 } 994 mutex_exit(&VTOMI4(dvp)->mi_lock); 995 996 /* this length never changes, do alloc before seqid sync */ 997 open_args->owner.owner_len = sizeof (oop->oo_name); 998 open_args->owner.owner_val = 999 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1000 1001 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp)); 1002 if (e.error == EAGAIN) { 1003 open_owner_rele(oop); 1004 nfs4args_copen_free(open_args); 1005 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1006 if (ncr != NULL) { 1007 crfree(ncr); 1008 ncr = NULL; 1009 } 1010 goto recov_retry; 1011 } 1012 1013 /* Check to see if we need to do the OTW call */ 1014 if (!create_flag) { 1015 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi, 1016 file_just_been_created, &e.error, acc, &recov_state)) { 1017 1018 /* 1019 * The OTW open is not necessary. Either 1020 * the open can succeed without it (eg. 1021 * delegation, error == 0) or the open 1022 * must fail due to an access failure 1023 * (error != 0). In either case, tidy 1024 * up and return. 1025 */ 1026 1027 nfs4_end_open_seqid_sync(oop); 1028 open_owner_rele(oop); 1029 nfs4args_copen_free(open_args); 1030 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE); 1031 if (ncr != NULL) 1032 crfree(ncr); 1033 kmem_free(argop, argoplist_size); 1034 return (e.error); 1035 } 1036 } 1037 1038 bcopy(&oop->oo_name, open_args->owner.owner_val, 1039 open_args->owner.owner_len); 1040 1041 seqid = nfs4_get_open_seqid(oop) + 1; 1042 open_args->seqid = seqid; 1043 open_args->share_access = 0; 1044 if (open_flag & FREAD) 1045 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1046 if (open_flag & FWRITE) 1047 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1048 open_args->share_deny = OPEN4_SHARE_DENY_NONE; 1049 1050 1051 1052 /* 1053 * getfh w/sanity check for idx_open/idx_fattr 1054 */ 1055 ASSERT((idx_open + 1) == (idx_fattr - 1)); 1056 argop[idx_open + 1].argop = OP_GETFH; 1057 1058 /* getattr */ 1059 argop[idx_fattr].argop = OP_GETATTR; 1060 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1061 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1062 1063 if (setgid_flag) { 1064 vattr_t _v; 1065 servinfo4_t *svp; 1066 bitmap4 supp_attrs; 1067 1068 svp = drp->r_server; 1069 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 1070 supp_attrs = svp->sv_supp_attrs; 1071 nfs_rw_exit(&svp->sv_lock); 1072 1073 /* 1074 * For setgid case, we need to: 1075 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1076 */ 1077 argop[4].argop = OP_SAVEFH; 1078 1079 argop[5].argop = OP_CPUTFH; 1080 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 1081 1082 argop[6].argop = OP_GETATTR; 1083 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1084 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1085 1086 argop[7].argop = OP_RESTOREFH; 1087 1088 /* 1089 * nverify 1090 */ 1091 _v.va_mask = AT_GID; 1092 _v.va_gid = in_va->va_gid; 1093 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 1094 supp_attrs))) { 1095 1096 /* 1097 * setattr 1098 * 1099 * We _know_ we're not messing with AT_SIZE or 1100 * AT_XTIME, so no need for stateid or flags. 1101 * Also we specify NULL rp since we're only 1102 * interested in setting owner_group attributes. 1103 */ 1104 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, 1105 supp_attrs, &e.error, 0); 1106 if (e.error) 1107 nfs4args_verify_free(&argop[8]); 1108 } 1109 1110 if (e.error) { 1111 /* 1112 * XXX - Revisit the last argument to nfs4_end_op() 1113 * once 5020486 is fixed. 1114 */ 1115 nfs4_end_open_seqid_sync(oop); 1116 open_owner_rele(oop); 1117 nfs4args_copen_free(open_args); 1118 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE); 1119 if (ncr != NULL) 1120 crfree(ncr); 1121 kmem_free(argop, argoplist_size); 1122 return (e.error); 1123 } 1124 } else if (create_flag) { 1125 /* 1126 * For setgid case, we need to: 1127 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 1128 */ 1129 argop[1].argop = OP_SAVEFH; 1130 1131 argop[5].argop = OP_RESTOREFH; 1132 1133 argop[6].argop = OP_GETATTR; 1134 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1135 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 1136 } 1137 1138 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 1139 "nfs4open_otw: %s call, nm %s, rp %s", 1140 needrecov ? "recov" : "first", file_name, 1141 rnode4info(VTOR4(dvp)))); 1142 1143 t = gethrtime(); 1144 1145 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e); 1146 1147 if (!e.error && nfs4_need_to_bump_seqid(&res)) 1148 nfs4_set_open_seqid(seqid, oop, args.ctag); 1149 1150 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp); 1151 1152 if (e.error || needrecov) { 1153 bool_t abort = FALSE; 1154 1155 if (needrecov) { 1156 nfs4_bseqid_entry_t *bsep = NULL; 1157 1158 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop, 1159 cred_otw, vpi, dvp, open_args); 1160 1161 if (!e.error && res.status == NFS4ERR_BAD_SEQID) { 1162 bsep = nfs4_create_bseqid_entry(oop, NULL, 1163 vpi, 0, args.ctag, open_args->seqid); 1164 num_bseqid_retry--; 1165 } 1166 1167 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi, 1168 NULL, lost_rqst.lr_op == OP_OPEN ? 1169 &lost_rqst : NULL, OP_OPEN, bsep); 1170 1171 if (bsep) 1172 kmem_free(bsep, sizeof (*bsep)); 1173 /* give up if we keep getting BAD_SEQID */ 1174 if (num_bseqid_retry == 0) 1175 abort = TRUE; 1176 if (abort == TRUE && e.error == 0) 1177 e.error = geterrno4(res.status); 1178 } 1179 nfs4_end_open_seqid_sync(oop); 1180 open_owner_rele(oop); 1181 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1182 nfs4args_copen_free(open_args); 1183 if (setgid_flag) { 1184 nfs4args_verify_free(&argop[8]); 1185 nfs4args_setattr_free(&argop[9]); 1186 } 1187 if (!e.error) 1188 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1189 if (ncr != NULL) { 1190 crfree(ncr); 1191 ncr = NULL; 1192 } 1193 if (!needrecov || abort == TRUE || e.error == EINTR || 1194 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) { 1195 kmem_free(argop, argoplist_size); 1196 return (e.error); 1197 } 1198 goto recov_retry; 1199 } 1200 1201 /* 1202 * Will check and update lease after checking the rflag for 1203 * OPEN_CONFIRM in the successful OPEN call. 1204 */ 1205 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 1206 1207 /* 1208 * XXX what if we're crossing mount points from server1:/drp 1209 * to server2:/drp/rp. 1210 */ 1211 1212 /* Signal our end of use of the open seqid */ 1213 nfs4_end_open_seqid_sync(oop); 1214 1215 /* 1216 * This will destroy the open owner if it was just created, 1217 * and no one else has put a reference on it. 1218 */ 1219 open_owner_rele(oop); 1220 if (create_flag && (createmode != EXCLUSIVE4) && 1221 res.status == NFS4ERR_BADOWNER) 1222 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1223 1224 e.error = geterrno4(res.status); 1225 nfs4args_copen_free(open_args); 1226 if (setgid_flag) { 1227 nfs4args_verify_free(&argop[8]); 1228 nfs4args_setattr_free(&argop[9]); 1229 } 1230 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1231 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1232 /* 1233 * If the reply is NFS4ERR_ACCESS, it may be because 1234 * we are root (no root net access). If the real uid 1235 * is not root, then retry with the real uid instead. 1236 */ 1237 if (ncr != NULL) { 1238 crfree(ncr); 1239 ncr = NULL; 1240 } 1241 if (res.status == NFS4ERR_ACCESS && 1242 (ncr = crnetadjust(cred_otw)) != NULL) { 1243 cred_otw = ncr; 1244 goto recov_retry; 1245 } 1246 kmem_free(argop, argoplist_size); 1247 return (e.error); 1248 } 1249 1250 resop = &res.array[idx_open]; /* open res */ 1251 op_res = &resop->nfs_resop4_u.opopen; 1252 1253 #ifdef DEBUG 1254 /* 1255 * verify attrset bitmap 1256 */ 1257 if (create_flag && 1258 (createmode == UNCHECKED4 || createmode == GUARDED4)) { 1259 /* make sure attrset returned is what we asked for */ 1260 /* XXX Ignore this 'error' for now */ 1261 if (attr->attrmask != op_res->attrset) 1262 /* EMPTY */; 1263 } 1264 #endif 1265 1266 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) { 1267 mutex_enter(&VTOMI4(dvp)->mi_lock); 1268 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK; 1269 mutex_exit(&VTOMI4(dvp)->mi_lock); 1270 } 1271 1272 resop = &res.array[idx_open + 1]; /* getfh res */ 1273 gf_res = &resop->nfs_resop4_u.opgetfh; 1274 1275 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp)); 1276 1277 /* 1278 * The open stateid has been updated on the server but not 1279 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache-> 1280 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW 1281 * WRITE call. That, however, will use the old stateid, so go ahead 1282 * and upate the open stateid now, before any call to makenfs4node. 1283 */ 1284 if (vpi) { 1285 nfs4_open_stream_t *tmp_osp; 1286 rnode4_t *tmp_rp = VTOR4(vpi); 1287 1288 tmp_osp = find_open_stream(oop, tmp_rp); 1289 if (tmp_osp) { 1290 tmp_osp->open_stateid = op_res->stateid; 1291 mutex_exit(&tmp_osp->os_sync_lock); 1292 open_stream_rele(tmp_osp, tmp_rp); 1293 } 1294 1295 /* 1296 * We must determine if the file handle given by the otw open 1297 * is the same as the file handle which was passed in with 1298 * *vpp. This case can be reached if the file we are trying 1299 * to open has been removed and another file has been created 1300 * having the same file name. The passed in vnode is released 1301 * later. 1302 */ 1303 orig_sfh = VTOR4(vpi)->r_fh; 1304 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh); 1305 } 1306 1307 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res; 1308 1309 if (create_flag || fh_differs) { 1310 int rnode_err = 0; 1311 1312 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr, 1313 dvp, fn_get(VTOSV(dvp)->sv_name, file_name)); 1314 1315 if (e.error) 1316 PURGE_ATTRCACHE4(vp); 1317 /* 1318 * For the newly created vp case, make sure the rnode 1319 * isn't bad before using it. 1320 */ 1321 mutex_enter(&(VTOR4(vp))->r_statelock); 1322 if (VTOR4(vp)->r_flags & R4RECOVERR) 1323 rnode_err = EIO; 1324 mutex_exit(&(VTOR4(vp))->r_statelock); 1325 1326 if (rnode_err) { 1327 nfs4_end_open_seqid_sync(oop); 1328 nfs4args_copen_free(open_args); 1329 if (setgid_flag) { 1330 nfs4args_verify_free(&argop[8]); 1331 nfs4args_setattr_free(&argop[9]); 1332 } 1333 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1334 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1335 needrecov); 1336 open_owner_rele(oop); 1337 VN_RELE(vp); 1338 if (ncr != NULL) 1339 crfree(ncr); 1340 sfh4_rele(&otw_sfh); 1341 kmem_free(argop, argoplist_size); 1342 return (EIO); 1343 } 1344 } else { 1345 vp = vpi; 1346 } 1347 sfh4_rele(&otw_sfh); 1348 1349 /* 1350 * It seems odd to get a full set of attrs and then not update 1351 * the object's attrcache in the non-create case. Create case uses 1352 * the attrs since makenfs4node checks to see if the attrs need to 1353 * be updated (and then updates them). The non-create case should 1354 * update attrs also. 1355 */ 1356 if (! create_flag && ! fh_differs && !e.error) { 1357 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 1358 } 1359 1360 nfs4_error_zinit(&e); 1361 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 1362 /* This does not do recovery for vp explicitly. */ 1363 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE, 1364 &retry_open, oop, FALSE, &e, &num_bseqid_retry); 1365 1366 if (e.error || e.stat) { 1367 nfs4_end_open_seqid_sync(oop); 1368 nfs4args_copen_free(open_args); 1369 if (setgid_flag) { 1370 nfs4args_verify_free(&argop[8]); 1371 nfs4args_setattr_free(&argop[9]); 1372 } 1373 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1374 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, 1375 needrecov); 1376 open_owner_rele(oop); 1377 if (create_flag || fh_differs) { 1378 /* rele the makenfs4node */ 1379 VN_RELE(vp); 1380 } 1381 if (ncr != NULL) { 1382 crfree(ncr); 1383 ncr = NULL; 1384 } 1385 if (retry_open == TRUE) { 1386 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1387 "nfs4open_otw: retry the open since OPEN " 1388 "CONFIRM failed with error %d stat %d", 1389 e.error, e.stat)); 1390 if (create_flag && createmode == GUARDED4) { 1391 NFS4_DEBUG(nfs4_client_recov_debug, 1392 (CE_NOTE, "nfs4open_otw: switch " 1393 "createmode from GUARDED4 to " 1394 "UNCHECKED4")); 1395 createmode = UNCHECKED4; 1396 } 1397 goto recov_retry; 1398 } 1399 if (!e.error) { 1400 if (create_flag && (createmode != EXCLUSIVE4) && 1401 e.stat == NFS4ERR_BADOWNER) 1402 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN); 1403 1404 e.error = geterrno4(e.stat); 1405 } 1406 kmem_free(argop, argoplist_size); 1407 return (e.error); 1408 } 1409 } 1410 1411 rp = VTOR4(vp); 1412 1413 mutex_enter(&rp->r_statev4_lock); 1414 if (create_flag) 1415 rp->created_v4 = 1; 1416 mutex_exit(&rp->r_statev4_lock); 1417 1418 mutex_enter(&oop->oo_lock); 1419 /* Doesn't matter if 'oo_just_created' already was set as this */ 1420 oop->oo_just_created = NFS4_PERM_CREATED; 1421 if (oop->oo_cred_otw) 1422 crfree(oop->oo_cred_otw); 1423 oop->oo_cred_otw = cred_otw; 1424 crhold(oop->oo_cred_otw); 1425 mutex_exit(&oop->oo_lock); 1426 1427 /* returns with 'os_sync_lock' held */ 1428 osp = find_or_create_open_stream(oop, rp, &created_osp); 1429 if (!osp) { 1430 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1431 "nfs4open_otw: failed to create an open stream")); 1432 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: " 1433 "signal our end of use of the open seqid")); 1434 1435 nfs4_end_open_seqid_sync(oop); 1436 open_owner_rele(oop); 1437 nfs4args_copen_free(open_args); 1438 if (setgid_flag) { 1439 nfs4args_verify_free(&argop[8]); 1440 nfs4args_setattr_free(&argop[9]); 1441 } 1442 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1443 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1444 if (create_flag || fh_differs) 1445 VN_RELE(vp); 1446 if (ncr != NULL) 1447 crfree(ncr); 1448 1449 kmem_free(argop, argoplist_size); 1450 return (EINVAL); 1451 1452 } 1453 1454 osp->open_stateid = op_res->stateid; 1455 1456 if (open_flag & FREAD) 1457 osp->os_share_acc_read++; 1458 if (open_flag & FWRITE) 1459 osp->os_share_acc_write++; 1460 osp->os_share_deny_none++; 1461 1462 /* 1463 * Need to reset this bitfield for the possible case where we were 1464 * going to OTW CLOSE the file, got a non-recoverable error, and before 1465 * we could retry the CLOSE, OPENed the file again. 1466 */ 1467 ASSERT(osp->os_open_owner->oo_seqid_inuse); 1468 osp->os_final_close = 0; 1469 osp->os_force_close = 0; 1470 #ifdef DEBUG 1471 if (osp->os_failed_reopen) 1472 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:" 1473 " clearing os_failed_reopen for osp %p, cr %p, rp %s", 1474 (void *)osp, (void *)cr, rnode4info(rp))); 1475 #endif 1476 osp->os_failed_reopen = 0; 1477 1478 mutex_exit(&osp->os_sync_lock); 1479 1480 nfs4_end_open_seqid_sync(oop); 1481 1482 if (created_osp && recov_state.rs_sp != NULL) { 1483 mutex_enter(&recov_state.rs_sp->s_lock); 1484 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp)); 1485 mutex_exit(&recov_state.rs_sp->s_lock); 1486 } 1487 1488 /* get rid of our reference to find oop */ 1489 open_owner_rele(oop); 1490 1491 open_stream_rele(osp, rp); 1492 1493 /* accept delegation, if any */ 1494 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw); 1495 1496 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov); 1497 1498 if (createmode == EXCLUSIVE4 && 1499 (in_va->va_mask & ~(AT_GID | AT_SIZE))) { 1500 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:" 1501 " EXCLUSIVE4: sending a SETATTR")); 1502 /* 1503 * If doing an exclusive create, then generate 1504 * a SETATTR to set the initial attributes. 1505 * Try to set the mtime and the atime to the 1506 * server's current time. It is somewhat 1507 * expected that these fields will be used to 1508 * store the exclusive create cookie. If not, 1509 * server implementors will need to know that 1510 * a SETATTR will follow an exclusive create 1511 * and the cookie should be destroyed if 1512 * appropriate. 1513 * 1514 * The AT_GID and AT_SIZE bits are turned off 1515 * so that the SETATTR request will not attempt 1516 * to process these. The gid will be set 1517 * separately if appropriate. The size is turned 1518 * off because it is assumed that a new file will 1519 * be created empty and if the file wasn't empty, 1520 * then the exclusive create will have failed 1521 * because the file must have existed already. 1522 * Therefore, no truncate operation is needed. 1523 */ 1524 in_va->va_mask &= ~(AT_GID | AT_SIZE); 1525 in_va->va_mask |= (AT_MTIME | AT_ATIME); 1526 1527 e.error = nfs4setattr(vp, in_va, 0, cr, NULL); 1528 if (e.error) { 1529 /* 1530 * Couldn't correct the attributes of 1531 * the newly created file and the 1532 * attributes are wrong. Remove the 1533 * file and return an error to the 1534 * application. 1535 */ 1536 /* XXX will this take care of client state ? */ 1537 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 1538 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:" 1539 " remove file", e.error)); 1540 VN_RELE(vp); 1541 (void) nfs4_remove(dvp, file_name, cr); 1542 /* 1543 * Since we've reled the vnode and removed 1544 * the file we now need to return the error. 1545 * At this point we don't want to update the 1546 * dircaches, call nfs4_waitfor_purge_complete 1547 * or set vpp to vp so we need to skip these 1548 * as well. 1549 */ 1550 goto skip_update_dircaches; 1551 } 1552 } 1553 1554 /* 1555 * If we created or found the correct vnode, due to create_flag or 1556 * fh_differs being set, then update directory cache attribute, readdir 1557 * and dnlc caches. 1558 */ 1559 if (create_flag || fh_differs) { 1560 dirattr_info_t dinfo, *dinfop; 1561 1562 /* 1563 * Make sure getattr succeeded before using results. 1564 * note: op 7 is getattr(dir) for both flavors of 1565 * open(create). 1566 */ 1567 if (create_flag && res.status == NFS4_OK) { 1568 dinfo.di_time_call = t; 1569 dinfo.di_cred = cr; 1570 dinfo.di_garp = 1571 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 1572 dinfop = &dinfo; 1573 } else { 1574 dinfop = NULL; 1575 } 1576 1577 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name, 1578 dinfop); 1579 } 1580 1581 /* 1582 * If the page cache for this file was flushed from actions 1583 * above, it was done asynchronously and if that is true, 1584 * there is a need to wait here for it to complete. This must 1585 * be done outside of start_fop/end_fop. 1586 */ 1587 (void) nfs4_waitfor_purge_complete(vp); 1588 1589 /* 1590 * It is implicit that we are in the open case (create_flag == 0) since 1591 * fh_differs can only be set to a non-zero value in the open case. 1592 */ 1593 if (fh_differs != 0 && vpi != NULL) 1594 VN_RELE(vpi); 1595 1596 /* 1597 * Be sure to set *vpp to the correct value before returning. 1598 */ 1599 *vpp = vp; 1600 1601 skip_update_dircaches: 1602 1603 nfs4args_copen_free(open_args); 1604 if (setgid_flag) { 1605 nfs4args_verify_free(&argop[8]); 1606 nfs4args_setattr_free(&argop[9]); 1607 } 1608 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1609 1610 if (ncr) 1611 crfree(ncr); 1612 kmem_free(argop, argoplist_size); 1613 return (e.error); 1614 } 1615 1616 /* 1617 * Reopen an open instance. cf. nfs4open_otw(). 1618 * 1619 * Errors are returned by the nfs4_error_t parameter. 1620 * - ep->error contains an errno value or zero. 1621 * - if it is zero, ep->stat is set to an NFS status code, if any. 1622 * If the file could not be reopened, but the caller should continue, the 1623 * file is marked dead and no error values are returned. If the caller 1624 * should stop recovering open files and start over, either the ep->error 1625 * value or ep->stat will indicate an error (either something that requires 1626 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile 1627 * filehandles) may be handled silently by this routine. 1628 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state 1629 * will be started, so the caller should not do it. 1630 * 1631 * Gotos: 1632 * - kill_file : reopen failed in such a fashion to constitute marking the 1633 * file dead and setting the open stream's 'os_failed_reopen' as 1. This 1634 * is for cases where recovery is not possible. 1635 * - failed_reopen : same as above, except that the file has already been 1636 * marked dead, so no need to do it again. 1637 * - bailout : reopen failed but we are able to recover and retry the reopen - 1638 * either within this function immediatley or via the calling function. 1639 */ 1640 1641 void 1642 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep, 1643 open_claim_type4 claim, bool_t frc_use_claim_previous, 1644 bool_t is_recov) 1645 { 1646 COMPOUND4args_clnt args; 1647 COMPOUND4res_clnt res; 1648 nfs_argop4 argop[4]; 1649 nfs_resop4 *resop; 1650 OPEN4res *op_res = NULL; 1651 OPEN4cargs *open_args; 1652 GETFH4res *gf_res; 1653 rnode4_t *rp = VTOR4(vp); 1654 int doqueue = 1; 1655 cred_t *cr = NULL, *cred_otw = NULL; 1656 nfs4_open_owner_t *oop = NULL; 1657 seqid4 seqid; 1658 nfs4_ga_res_t *garp; 1659 char fn[MAXNAMELEN]; 1660 nfs4_recov_state_t recov = {NULL, 0}; 1661 nfs4_lost_rqst_t lost_rqst; 1662 mntinfo4_t *mi = VTOMI4(vp); 1663 bool_t abort; 1664 char *failed_msg = ""; 1665 int fh_different; 1666 hrtime_t t; 1667 nfs4_bseqid_entry_t *bsep = NULL; 1668 1669 ASSERT(nfs4_consistent_type(vp)); 1670 ASSERT(nfs_zone() == mi->mi_zone); 1671 1672 nfs4_error_zinit(ep); 1673 1674 /* this is the cred used to find the open owner */ 1675 cr = state_to_cred(osp); 1676 if (cr == NULL) { 1677 failed_msg = "Couldn't reopen: no cred"; 1678 goto kill_file; 1679 } 1680 /* use this cred for OTW operations */ 1681 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner); 1682 1683 top: 1684 nfs4_error_zinit(ep); 1685 1686 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) { 1687 /* File system has been unmounted, quit */ 1688 ep->error = EIO; 1689 failed_msg = "Couldn't reopen: file system has been unmounted"; 1690 goto kill_file; 1691 } 1692 1693 oop = osp->os_open_owner; 1694 1695 ASSERT(oop != NULL); 1696 if (oop == NULL) { /* be defensive in non-DEBUG */ 1697 failed_msg = "can't reopen: no open owner"; 1698 goto kill_file; 1699 } 1700 open_owner_hold(oop); 1701 1702 ep->error = nfs4_start_open_seqid_sync(oop, mi); 1703 if (ep->error) { 1704 open_owner_rele(oop); 1705 oop = NULL; 1706 goto bailout; 1707 } 1708 1709 /* 1710 * If the rnode has a delegation and the delegation has been 1711 * recovered and the server didn't request a recall and the caller 1712 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during 1713 * recovery) and the rnode hasn't been marked dead, then install 1714 * the delegation stateid in the open stream. Otherwise, proceed 1715 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN. 1716 */ 1717 mutex_enter(&rp->r_statev4_lock); 1718 if (rp->r_deleg_type != OPEN_DELEGATE_NONE && 1719 !rp->r_deleg_return_pending && 1720 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) && 1721 !rp->r_deleg_needs_recall && 1722 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous && 1723 !(rp->r_flags & R4RECOVERR)) { 1724 mutex_enter(&osp->os_sync_lock); 1725 osp->os_delegation = 1; 1726 osp->open_stateid = rp->r_deleg_stateid; 1727 mutex_exit(&osp->os_sync_lock); 1728 mutex_exit(&rp->r_statev4_lock); 1729 goto bailout; 1730 } 1731 mutex_exit(&rp->r_statev4_lock); 1732 1733 /* 1734 * If the file failed recovery, just quit. This failure need not 1735 * affect other reopens, so don't return an error. 1736 */ 1737 mutex_enter(&rp->r_statelock); 1738 if (rp->r_flags & R4RECOVERR) { 1739 mutex_exit(&rp->r_statelock); 1740 ep->error = 0; 1741 goto failed_reopen; 1742 } 1743 mutex_exit(&rp->r_statelock); 1744 1745 /* 1746 * argop is empty here 1747 * 1748 * PUTFH, OPEN, GETATTR 1749 */ 1750 args.ctag = TAG_REOPEN; 1751 args.array_len = 4; 1752 args.array = argop; 1753 1754 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 1755 "nfs4_reopen: file is type %d, id %s", 1756 vp->v_type, rnode4info(VTOR4(vp)))); 1757 1758 argop[0].argop = OP_CPUTFH; 1759 1760 if (claim != CLAIM_PREVIOUS) { 1761 /* 1762 * if this is a file mount then 1763 * use the mntinfo parentfh 1764 */ 1765 argop[0].nfs_argop4_u.opcputfh.sfh = 1766 (vp->v_flag & VROOT) ? mi->mi_srvparentfh : 1767 VTOSV(vp)->sv_dfh; 1768 } else { 1769 /* putfh fh to reopen */ 1770 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 1771 } 1772 1773 argop[1].argop = OP_COPEN; 1774 open_args = &argop[1].nfs_argop4_u.opcopen; 1775 open_args->claim = claim; 1776 1777 if (claim == CLAIM_NULL) { 1778 1779 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1780 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1781 "failed for vp 0x%p for CLAIM_NULL with %m", 1782 (void *)vp); 1783 failed_msg = "Couldn't reopen: vtoname failed for " 1784 "CLAIM_NULL"; 1785 /* nothing allocated yet */ 1786 goto kill_file; 1787 } 1788 1789 open_args->open_claim4_u.cfile = fn; 1790 } else if (claim == CLAIM_PREVIOUS) { 1791 1792 /* 1793 * We have two cases to deal with here: 1794 * 1) We're being called to reopen files in order to satisfy 1795 * a lock operation request which requires us to explicitly 1796 * reopen files which were opened under a delegation. If 1797 * we're in recovery, we *must* use CLAIM_PREVIOUS. In 1798 * that case, frc_use_claim_previous is TRUE and we must 1799 * use the rnode's current delegation type (r_deleg_type). 1800 * 2) We're reopening files during some form of recovery. 1801 * In this case, frc_use_claim_previous is FALSE and we 1802 * use the delegation type appropriate for recovery 1803 * (r_deleg_needs_recovery). 1804 */ 1805 mutex_enter(&rp->r_statev4_lock); 1806 open_args->open_claim4_u.delegate_type = 1807 frc_use_claim_previous ? 1808 rp->r_deleg_type : 1809 rp->r_deleg_needs_recovery; 1810 mutex_exit(&rp->r_statev4_lock); 1811 1812 } else if (claim == CLAIM_DELEGATE_CUR) { 1813 1814 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) { 1815 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname " 1816 "failed for vp 0x%p for CLAIM_DELEGATE_CUR " 1817 "with %m", (void *)vp); 1818 failed_msg = "Couldn't reopen: vtoname failed for " 1819 "CLAIM_DELEGATE_CUR"; 1820 /* nothing allocated yet */ 1821 goto kill_file; 1822 } 1823 1824 mutex_enter(&rp->r_statev4_lock); 1825 open_args->open_claim4_u.delegate_cur_info.delegate_stateid = 1826 rp->r_deleg_stateid; 1827 mutex_exit(&rp->r_statev4_lock); 1828 1829 open_args->open_claim4_u.delegate_cur_info.cfile = fn; 1830 } 1831 open_args->opentype = OPEN4_NOCREATE; 1832 open_args->owner.clientid = mi2clientid(mi); 1833 open_args->owner.owner_len = sizeof (oop->oo_name); 1834 open_args->owner.owner_val = 1835 kmem_alloc(open_args->owner.owner_len, KM_SLEEP); 1836 bcopy(&oop->oo_name, open_args->owner.owner_val, 1837 open_args->owner.owner_len); 1838 open_args->share_access = 0; 1839 open_args->share_deny = 0; 1840 1841 mutex_enter(&osp->os_sync_lock); 1842 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp " 1843 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: " 1844 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ", 1845 (void *)osp, (void *)rp, osp->os_share_acc_read, 1846 osp->os_share_acc_write, osp->os_open_ref_count, 1847 osp->os_mmap_read, osp->os_mmap_write, claim)); 1848 1849 if (osp->os_share_acc_read || osp->os_mmap_read) 1850 open_args->share_access |= OPEN4_SHARE_ACCESS_READ; 1851 if (osp->os_share_acc_write || osp->os_mmap_write) 1852 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE; 1853 if (osp->os_share_deny_read) 1854 open_args->share_deny |= OPEN4_SHARE_DENY_READ; 1855 if (osp->os_share_deny_write) 1856 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE; 1857 mutex_exit(&osp->os_sync_lock); 1858 1859 seqid = nfs4_get_open_seqid(oop) + 1; 1860 open_args->seqid = seqid; 1861 1862 /* Construct the getfh part of the compound */ 1863 argop[2].argop = OP_GETFH; 1864 1865 /* Construct the getattr part of the compound */ 1866 argop[3].argop = OP_GETATTR; 1867 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 1868 argop[3].nfs_argop4_u.opgetattr.mi = mi; 1869 1870 t = gethrtime(); 1871 1872 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 1873 1874 if (ep->error) { 1875 if (!is_recov && !frc_use_claim_previous && 1876 (ep->error == EINTR || ep->error == ETIMEDOUT || 1877 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) { 1878 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop, 1879 cred_otw, vp, NULL, open_args); 1880 abort = nfs4_start_recovery(ep, 1881 VTOMI4(vp), vp, NULL, NULL, 1882 lost_rqst.lr_op == OP_OPEN ? 1883 &lost_rqst : NULL, OP_OPEN, NULL); 1884 nfs4args_copen_free(open_args); 1885 goto bailout; 1886 } 1887 1888 nfs4args_copen_free(open_args); 1889 1890 if (ep->error == EACCES && cred_otw != cr) { 1891 crfree(cred_otw); 1892 cred_otw = cr; 1893 crhold(cred_otw); 1894 nfs4_end_open_seqid_sync(oop); 1895 open_owner_rele(oop); 1896 oop = NULL; 1897 goto top; 1898 } 1899 if (ep->error == ETIMEDOUT) 1900 goto bailout; 1901 failed_msg = "Couldn't reopen: rpc error"; 1902 goto kill_file; 1903 } 1904 1905 if (nfs4_need_to_bump_seqid(&res)) 1906 nfs4_set_open_seqid(seqid, oop, args.ctag); 1907 1908 switch (res.status) { 1909 case NFS4_OK: 1910 if (recov.rs_flags & NFS4_RS_DELAY_MSG) { 1911 mutex_enter(&rp->r_statelock); 1912 rp->r_delay_interval = 0; 1913 mutex_exit(&rp->r_statelock); 1914 } 1915 break; 1916 case NFS4ERR_BAD_SEQID: 1917 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0, 1918 args.ctag, open_args->seqid); 1919 1920 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, 1921 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst : 1922 NULL, OP_OPEN, bsep); 1923 1924 nfs4args_copen_free(open_args); 1925 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1926 nfs4_end_open_seqid_sync(oop); 1927 open_owner_rele(oop); 1928 oop = NULL; 1929 kmem_free(bsep, sizeof (*bsep)); 1930 1931 goto kill_file; 1932 case NFS4ERR_NO_GRACE: 1933 nfs4args_copen_free(open_args); 1934 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1935 nfs4_end_open_seqid_sync(oop); 1936 open_owner_rele(oop); 1937 oop = NULL; 1938 if (claim == CLAIM_PREVIOUS) { 1939 /* 1940 * Retry as a plain open. We don't need to worry about 1941 * checking the changeinfo: it is acceptable for a 1942 * client to re-open a file and continue processing 1943 * (in the absence of locks). 1944 */ 1945 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 1946 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; " 1947 "will retry as CLAIM_NULL")); 1948 claim = CLAIM_NULL; 1949 nfs4_mi_kstat_inc_no_grace(mi); 1950 goto top; 1951 } 1952 failed_msg = 1953 "Couldn't reopen: tried reclaim outside grace period. "; 1954 goto kill_file; 1955 case NFS4ERR_GRACE: 1956 nfs4_set_grace_wait(mi); 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 ep->error = nfs4_wait_for_grace(mi, &recov); 1963 if (ep->error != 0) 1964 goto bailout; 1965 goto top; 1966 case NFS4ERR_DELAY: 1967 nfs4_set_delay_wait(vp); 1968 nfs4args_copen_free(open_args); 1969 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1970 nfs4_end_open_seqid_sync(oop); 1971 open_owner_rele(oop); 1972 oop = NULL; 1973 ep->error = nfs4_wait_for_delay(vp, &recov); 1974 nfs4_mi_kstat_inc_delay(mi); 1975 if (ep->error != 0) 1976 goto bailout; 1977 goto top; 1978 case NFS4ERR_FHEXPIRED: 1979 /* recover filehandle and retry */ 1980 abort = nfs4_start_recovery(ep, 1981 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL); 1982 nfs4args_copen_free(open_args); 1983 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 1984 nfs4_end_open_seqid_sync(oop); 1985 open_owner_rele(oop); 1986 oop = NULL; 1987 if (abort == FALSE) 1988 goto top; 1989 failed_msg = "Couldn't reopen: recovery aborted"; 1990 goto kill_file; 1991 case NFS4ERR_RESOURCE: 1992 case NFS4ERR_STALE_CLIENTID: 1993 case NFS4ERR_WRONGSEC: 1994 case NFS4ERR_EXPIRED: 1995 /* 1996 * Do not mark the file dead and let the calling 1997 * function initiate recovery. 1998 */ 1999 nfs4args_copen_free(open_args); 2000 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2001 nfs4_end_open_seqid_sync(oop); 2002 open_owner_rele(oop); 2003 oop = NULL; 2004 goto bailout; 2005 case NFS4ERR_ACCESS: 2006 if (cred_otw != cr) { 2007 crfree(cred_otw); 2008 cred_otw = cr; 2009 crhold(cred_otw); 2010 nfs4args_copen_free(open_args); 2011 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2012 nfs4_end_open_seqid_sync(oop); 2013 open_owner_rele(oop); 2014 oop = NULL; 2015 goto top; 2016 } 2017 /* fall through */ 2018 default: 2019 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE, 2020 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s", 2021 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv, 2022 rnode4info(VTOR4(vp)))); 2023 failed_msg = "Couldn't reopen: NFSv4 error"; 2024 nfs4args_copen_free(open_args); 2025 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2026 goto kill_file; 2027 } 2028 2029 resop = &res.array[1]; /* open res */ 2030 op_res = &resop->nfs_resop4_u.opopen; 2031 2032 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res; 2033 2034 /* 2035 * Check if the path we reopened really is the same 2036 * file. We could end up in a situation where the file 2037 * was removed and a new file created with the same name. 2038 */ 2039 resop = &res.array[2]; 2040 gf_res = &resop->nfs_resop4_u.opgetfh; 2041 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0); 2042 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0); 2043 if (fh_different) { 2044 if (mi->mi_fh_expire_type == FH4_PERSISTENT || 2045 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) { 2046 /* Oops, we don't have the same file */ 2047 if (mi->mi_fh_expire_type == FH4_PERSISTENT) 2048 failed_msg = "Couldn't reopen: Persistent " 2049 "file handle changed"; 2050 else 2051 failed_msg = "Couldn't reopen: Volatile " 2052 "(no expire on open) file handle changed"; 2053 2054 nfs4args_copen_free(open_args); 2055 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2056 nfs_rw_exit(&mi->mi_fh_lock); 2057 goto kill_file; 2058 2059 } else { 2060 /* 2061 * We have volatile file handles that don't compare. 2062 * If the fids are the same then we assume that the 2063 * file handle expired but the rnode still refers to 2064 * the same file object. 2065 * 2066 * First check that we have fids or not. 2067 * If we don't we have a dumb server so we will 2068 * just assume every thing is ok for now. 2069 */ 2070 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID && 2071 rp->r_attr.va_mask & AT_NODEID && 2072 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) { 2073 /* 2074 * We have fids, but they don't 2075 * compare. So kill the file. 2076 */ 2077 failed_msg = 2078 "Couldn't reopen: file handle changed" 2079 " due to mismatched fids"; 2080 nfs4args_copen_free(open_args); 2081 (void) xdr_free(xdr_COMPOUND4res_clnt, 2082 (caddr_t)&res); 2083 nfs_rw_exit(&mi->mi_fh_lock); 2084 goto kill_file; 2085 } else { 2086 /* 2087 * We have volatile file handles that refers 2088 * to the same file (at least they have the 2089 * same fid) or we don't have fids so we 2090 * can't tell. :(. We'll be a kind and accepting 2091 * client so we'll update the rnode's file 2092 * handle with the otw handle. 2093 * 2094 * We need to drop mi->mi_fh_lock since 2095 * sh4_update acquires it. Since there is 2096 * only one recovery thread there is no 2097 * race. 2098 */ 2099 nfs_rw_exit(&mi->mi_fh_lock); 2100 sfh4_update(rp->r_fh, &gf_res->object); 2101 } 2102 } 2103 } else { 2104 nfs_rw_exit(&mi->mi_fh_lock); 2105 } 2106 2107 ASSERT(nfs4_consistent_type(vp)); 2108 2109 /* 2110 * If the server wanted an OPEN_CONFIRM but that fails, just start 2111 * over. Presumably if there is a persistent error it will show up 2112 * when we resend the OPEN. 2113 */ 2114 if (op_res->rflags & OPEN4_RESULT_CONFIRM) { 2115 bool_t retry_open = FALSE; 2116 2117 nfs4open_confirm(vp, &seqid, &op_res->stateid, 2118 cred_otw, is_recov, &retry_open, 2119 oop, FALSE, ep, NULL); 2120 if (ep->error || ep->stat) { 2121 nfs4args_copen_free(open_args); 2122 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2123 nfs4_end_open_seqid_sync(oop); 2124 open_owner_rele(oop); 2125 oop = NULL; 2126 goto top; 2127 } 2128 } 2129 2130 mutex_enter(&osp->os_sync_lock); 2131 osp->open_stateid = op_res->stateid; 2132 osp->os_delegation = 0; 2133 /* 2134 * Need to reset this bitfield for the possible case where we were 2135 * going to OTW CLOSE the file, got a non-recoverable error, and before 2136 * we could retry the CLOSE, OPENed the file again. 2137 */ 2138 ASSERT(osp->os_open_owner->oo_seqid_inuse); 2139 osp->os_final_close = 0; 2140 osp->os_force_close = 0; 2141 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS) 2142 osp->os_dc_openacc = open_args->share_access; 2143 mutex_exit(&osp->os_sync_lock); 2144 2145 nfs4_end_open_seqid_sync(oop); 2146 2147 /* accept delegation, if any */ 2148 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw); 2149 2150 nfs4args_copen_free(open_args); 2151 2152 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL); 2153 2154 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2155 2156 ASSERT(nfs4_consistent_type(vp)); 2157 2158 open_owner_rele(oop); 2159 crfree(cr); 2160 crfree(cred_otw); 2161 return; 2162 2163 kill_file: 2164 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat); 2165 failed_reopen: 2166 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 2167 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s", 2168 (void *)osp, (void *)cr, rnode4info(rp))); 2169 mutex_enter(&osp->os_sync_lock); 2170 osp->os_failed_reopen = 1; 2171 mutex_exit(&osp->os_sync_lock); 2172 bailout: 2173 if (oop != NULL) { 2174 nfs4_end_open_seqid_sync(oop); 2175 open_owner_rele(oop); 2176 } 2177 if (cr != NULL) 2178 crfree(cr); 2179 if (cred_otw != NULL) 2180 crfree(cred_otw); 2181 } 2182 2183 /* for . and .. OPENs */ 2184 /* ARGSUSED */ 2185 static int 2186 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr) 2187 { 2188 rnode4_t *rp; 2189 nfs4_ga_res_t gar; 2190 2191 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone); 2192 2193 /* 2194 * If close-to-open consistency checking is turned off or 2195 * if there is no cached data, we can avoid 2196 * the over the wire getattr. Otherwise, force a 2197 * call to the server to get fresh attributes and to 2198 * check caches. This is required for close-to-open 2199 * consistency. 2200 */ 2201 rp = VTOR4(*vpp); 2202 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO || 2203 (rp->r_dir == NULL && !nfs4_has_pages(*vpp))) 2204 return (0); 2205 2206 gar.n4g_va.va_mask = AT_ALL; 2207 return (nfs4_getattr_otw(*vpp, &gar, cr, 0)); 2208 } 2209 2210 /* 2211 * CLOSE a file 2212 */ 2213 static int 2214 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 2215 { 2216 rnode4_t *rp; 2217 int error = 0; 2218 int r_error = 0; 2219 int n4error = 0; 2220 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 2221 2222 /* 2223 * Remove client state for this (lockowner, file) pair. 2224 * Issue otw v4 call to have the server do the same. 2225 */ 2226 2227 rp = VTOR4(vp); 2228 2229 /* 2230 * zone_enter(2) prevents processes from changing zones with NFS files 2231 * open; if we happen to get here from the wrong zone we can't do 2232 * anything over the wire. 2233 */ 2234 if (VTOMI4(vp)->mi_zone != nfs_zone()) { 2235 /* 2236 * We could attempt to clean up locks, except we're sure 2237 * that the current process didn't acquire any locks on 2238 * the file: any attempt to lock a file belong to another zone 2239 * will fail, and one can't lock an NFS file and then change 2240 * zones, as that fails too. 2241 * 2242 * Returning an error here is the sane thing to do. A 2243 * subsequent call to VN_RELE() which translates to a 2244 * nfs4_inactive() will clean up state: if the zone of the 2245 * vnode's origin is still alive and kicking, the inactive 2246 * thread will handle the request (from the correct zone), and 2247 * everything (minus the OTW close call) should be OK. If the 2248 * zone is going away nfs4_async_inactive() will throw away 2249 * delegations, open streams and cached pages inline. 2250 */ 2251 return (EIO); 2252 } 2253 2254 /* 2255 * If we are using local locking for this filesystem, then 2256 * release all of the SYSV style record locks. Otherwise, 2257 * we are doing network locking and we need to release all 2258 * of the network locks. All of the locks held by this 2259 * process on this file are released no matter what the 2260 * incoming reference count is. 2261 */ 2262 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) { 2263 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 2264 cleanshares(vp, ttoproc(curthread)->p_pid); 2265 } else 2266 e.error = nfs4_lockrelease(vp, flag, offset, cr); 2267 2268 if (e.error) 2269 return (e.error); 2270 2271 if (count > 1) 2272 return (0); 2273 2274 /* 2275 * If the file has been `unlinked', then purge the 2276 * DNLC so that this vnode will get reycled quicker 2277 * and the .nfs* file on the server will get removed. 2278 */ 2279 if (rp->r_unldvp != NULL) 2280 dnlc_purge_vp(vp); 2281 2282 /* 2283 * If the file was open for write and there are pages, 2284 * do a synchronous flush and commit of all of the 2285 * dirty and uncommitted pages. 2286 */ 2287 ASSERT(!e.error); 2288 if ((flag & FWRITE) && nfs4_has_pages(vp)) 2289 error = nfs4_putpage_commit(vp, 0, 0, cr); 2290 2291 mutex_enter(&rp->r_statelock); 2292 r_error = rp->r_error; 2293 rp->r_error = 0; 2294 mutex_exit(&rp->r_statelock); 2295 2296 /* 2297 * If this file type is one for which no explicit 'open' was 2298 * done, then bail now (ie. no need for protocol 'close'). If 2299 * there was an error w/the vm subsystem, return _that_ error, 2300 * otherwise, return any errors that may've been reported via 2301 * the rnode. 2302 */ 2303 if (vp->v_type != VREG) 2304 return (error ? error : r_error); 2305 2306 /* 2307 * The sync putpage commit may have failed above, but since 2308 * we're working w/a regular file, we need to do the protocol 2309 * 'close' (nfs4close_one will figure out if an otw close is 2310 * needed or not). Report any errors _after_ doing the protocol 2311 * 'close'. 2312 */ 2313 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0); 2314 n4error = e.error ? e.error : geterrno4(e.stat); 2315 2316 /* 2317 * Error reporting prio (Hi -> Lo) 2318 * 2319 * i) nfs4_putpage_commit (error) 2320 * ii) rnode's (r_error) 2321 * iii) nfs4close_one (n4error) 2322 */ 2323 return (error ? error : (r_error ? r_error : n4error)); 2324 } 2325 2326 /* 2327 * Initialize *lost_rqstp. 2328 */ 2329 2330 static void 2331 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp, 2332 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr, 2333 vnode_t *vp) 2334 { 2335 if (error != ETIMEDOUT && error != EINTR && 2336 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 2337 lost_rqstp->lr_op = 0; 2338 return; 2339 } 2340 2341 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 2342 "nfs4close_save_lost_rqst: error %d", error)); 2343 2344 lost_rqstp->lr_op = OP_CLOSE; 2345 /* 2346 * The vp is held and rele'd via the recovery code. 2347 * See nfs4_save_lost_rqst. 2348 */ 2349 lost_rqstp->lr_vp = vp; 2350 lost_rqstp->lr_dvp = NULL; 2351 lost_rqstp->lr_oop = oop; 2352 lost_rqstp->lr_osp = osp; 2353 ASSERT(osp != NULL); 2354 ASSERT(mutex_owned(&osp->os_sync_lock)); 2355 osp->os_pending_close = 1; 2356 lost_rqstp->lr_lop = NULL; 2357 lost_rqstp->lr_cr = cr; 2358 lost_rqstp->lr_flk = NULL; 2359 lost_rqstp->lr_putfirst = FALSE; 2360 } 2361 2362 /* 2363 * Assumes you already have the open seqid sync grabbed as well as the 2364 * 'os_sync_lock'. Note: this will release the open seqid sync and 2365 * 'os_sync_lock' if client recovery starts. Calling functions have to 2366 * be prepared to handle this. 2367 * 2368 * 'recov' is returned as 1 if the CLOSE operation detected client recovery 2369 * was needed and was started, and that the calling function should retry 2370 * this function; otherwise it is returned as 0. 2371 * 2372 * Errors are returned via the nfs4_error_t parameter. 2373 */ 2374 static void 2375 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop, 2376 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp, 2377 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp) 2378 { 2379 COMPOUND4args_clnt args; 2380 COMPOUND4res_clnt res; 2381 CLOSE4args *close_args; 2382 nfs_resop4 *resop; 2383 nfs_argop4 argop[3]; 2384 int doqueue = 1; 2385 mntinfo4_t *mi; 2386 seqid4 seqid; 2387 vnode_t *vp; 2388 bool_t needrecov = FALSE; 2389 nfs4_lost_rqst_t lost_rqst; 2390 hrtime_t t; 2391 2392 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 2393 2394 ASSERT(MUTEX_HELD(&osp->os_sync_lock)); 2395 2396 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw")); 2397 2398 /* Only set this to 1 if recovery is started */ 2399 *recov = 0; 2400 2401 /* do the OTW call to close the file */ 2402 2403 if (close_type == CLOSE_RESEND) 2404 args.ctag = TAG_CLOSE_LOST; 2405 else if (close_type == CLOSE_AFTER_RESEND) 2406 args.ctag = TAG_CLOSE_UNDO; 2407 else 2408 args.ctag = TAG_CLOSE; 2409 2410 args.array_len = 3; 2411 args.array = argop; 2412 2413 vp = RTOV4(rp); 2414 2415 mi = VTOMI4(vp); 2416 2417 /* putfh target fh */ 2418 argop[0].argop = OP_CPUTFH; 2419 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 2420 2421 argop[1].argop = OP_GETATTR; 2422 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 2423 argop[1].nfs_argop4_u.opgetattr.mi = mi; 2424 2425 argop[2].argop = OP_CLOSE; 2426 close_args = &argop[2].nfs_argop4_u.opclose; 2427 2428 seqid = nfs4_get_open_seqid(oop) + 1; 2429 2430 close_args->seqid = seqid; 2431 close_args->open_stateid = osp->open_stateid; 2432 2433 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 2434 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first", 2435 rnode4info(rp))); 2436 2437 t = gethrtime(); 2438 2439 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep); 2440 2441 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 2442 nfs4_set_open_seqid(seqid, oop, args.ctag); 2443 } 2444 2445 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 2446 if (ep->error && !needrecov) { 2447 /* 2448 * if there was an error and no recovery is to be done 2449 * then then set up the file to flush its cache if 2450 * needed for the next caller. 2451 */ 2452 mutex_enter(&rp->r_statelock); 2453 PURGE_ATTRCACHE4_LOCKED(rp); 2454 rp->r_flags &= ~R4WRITEMODIFIED; 2455 mutex_exit(&rp->r_statelock); 2456 return; 2457 } 2458 2459 if (needrecov) { 2460 bool_t abort; 2461 nfs4_bseqid_entry_t *bsep = NULL; 2462 2463 if (close_type != CLOSE_RESEND) 2464 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 2465 osp, cred_otw, vp); 2466 2467 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 2468 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 2469 0, args.ctag, close_args->seqid); 2470 2471 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 2472 "nfs4close_otw: initiating recovery. error %d " 2473 "res.status %d", ep->error, res.status)); 2474 2475 /* 2476 * Drop the 'os_sync_lock' here so we don't hit 2477 * a potential recursive mutex_enter via an 2478 * 'open_stream_hold()'. 2479 */ 2480 mutex_exit(&osp->os_sync_lock); 2481 *have_sync_lockp = 0; 2482 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 2483 (close_type != CLOSE_RESEND && 2484 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL, 2485 OP_CLOSE, bsep); 2486 2487 /* drop open seq sync, and let the calling function regrab it */ 2488 nfs4_end_open_seqid_sync(oop); 2489 *did_start_seqid_syncp = 0; 2490 2491 if (bsep) 2492 kmem_free(bsep, sizeof (*bsep)); 2493 /* 2494 * For signals, the caller wants to quit, so don't say to 2495 * retry. For forced unmount, if it's a user thread, it 2496 * wants to quit. If it's a recovery thread, the retry 2497 * will happen higher-up on the call stack. Either way, 2498 * don't say to retry. 2499 */ 2500 if (abort == FALSE && ep->error != EINTR && 2501 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) && 2502 close_type != CLOSE_RESEND && 2503 close_type != CLOSE_AFTER_RESEND) 2504 *recov = 1; 2505 else 2506 *recov = 0; 2507 2508 if (!ep->error) 2509 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2510 return; 2511 } 2512 2513 if (res.status) { 2514 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2515 return; 2516 } 2517 2518 mutex_enter(&rp->r_statev4_lock); 2519 rp->created_v4 = 0; 2520 mutex_exit(&rp->r_statev4_lock); 2521 2522 resop = &res.array[2]; 2523 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid; 2524 osp->os_valid = 0; 2525 2526 /* 2527 * This removes the reference obtained at OPEN; ie, when the 2528 * open stream structure was created. 2529 * 2530 * We don't have to worry about calling 'open_stream_rele' 2531 * since we our currently holding a reference to the open 2532 * stream which means the count cannot go to 0 with this 2533 * decrement. 2534 */ 2535 ASSERT(osp->os_ref_count >= 2); 2536 osp->os_ref_count--; 2537 2538 if (!ep->error) 2539 nfs4_attr_cache(vp, 2540 &res.array[1].nfs_resop4_u.opgetattr.ga_res, 2541 t, cred_otw, TRUE, NULL); 2542 2543 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:" 2544 " returning %d", ep->error)); 2545 2546 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 2547 } 2548 2549 /* ARGSUSED */ 2550 static int 2551 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2552 caller_context_t *ct) 2553 { 2554 rnode4_t *rp; 2555 u_offset_t off; 2556 offset_t diff; 2557 uint_t on; 2558 uint_t n; 2559 caddr_t base; 2560 uint_t flags; 2561 int error; 2562 mntinfo4_t *mi; 2563 2564 rp = VTOR4(vp); 2565 2566 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 2567 2568 if (IS_SHADOW(vp, rp)) 2569 vp = RTOV4(rp); 2570 2571 if (vp->v_type != VREG) 2572 return (EISDIR); 2573 2574 mi = VTOMI4(vp); 2575 2576 if (nfs_zone() != mi->mi_zone) 2577 return (EIO); 2578 2579 if (uiop->uio_resid == 0) 2580 return (0); 2581 2582 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 2583 return (EINVAL); 2584 2585 mutex_enter(&rp->r_statelock); 2586 if (rp->r_flags & R4RECOVERRP) 2587 error = (rp->r_error ? rp->r_error : EIO); 2588 else 2589 error = 0; 2590 mutex_exit(&rp->r_statelock); 2591 if (error) 2592 return (error); 2593 2594 /* 2595 * Bypass VM if caching has been disabled (e.g., locking) or if 2596 * using client-side direct I/O and the file is not mmap'd and 2597 * there are no cached pages. 2598 */ 2599 if ((vp->v_flag & VNOCACHE) || 2600 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2601 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2602 size_t resid = 0; 2603 2604 return (nfs4read(vp, NULL, uiop->uio_loffset, 2605 uiop->uio_resid, &resid, cr, FALSE, uiop)); 2606 } 2607 2608 error = 0; 2609 2610 do { 2611 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2612 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2613 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2614 2615 if (error = nfs4_validate_caches(vp, cr)) 2616 break; 2617 2618 mutex_enter(&rp->r_statelock); 2619 diff = rp->r_size - uiop->uio_loffset; 2620 mutex_exit(&rp->r_statelock); 2621 if (diff <= 0) 2622 break; 2623 if (diff < n) 2624 n = (uint_t)diff; 2625 2626 if (vpm_enable) { 2627 /* 2628 * Copy data. 2629 */ 2630 error = vpm_data_copy(vp, off + on, n, uiop, 2631 1, NULL, 0, S_READ); 2632 } else { 2633 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, 2634 S_READ); 2635 2636 error = uiomove(base + on, n, UIO_READ, uiop); 2637 } 2638 2639 if (!error) { 2640 /* 2641 * If read a whole block or read to eof, 2642 * won't need this buffer again soon. 2643 */ 2644 mutex_enter(&rp->r_statelock); 2645 if (n + on == MAXBSIZE || 2646 uiop->uio_loffset == rp->r_size) 2647 flags = SM_DONTNEED; 2648 else 2649 flags = 0; 2650 mutex_exit(&rp->r_statelock); 2651 if (vpm_enable) { 2652 error = vpm_sync_pages(vp, off, n, flags); 2653 } else { 2654 error = segmap_release(segkmap, base, flags); 2655 } 2656 } else { 2657 if (vpm_enable) { 2658 (void) vpm_sync_pages(vp, off, n, 0); 2659 } else { 2660 (void) segmap_release(segkmap, base, 0); 2661 } 2662 } 2663 } while (!error && uiop->uio_resid > 0); 2664 2665 return (error); 2666 } 2667 2668 /* ARGSUSED */ 2669 static int 2670 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 2671 caller_context_t *ct) 2672 { 2673 rlim64_t limit = uiop->uio_llimit; 2674 rnode4_t *rp; 2675 u_offset_t off; 2676 caddr_t base; 2677 uint_t flags; 2678 int remainder; 2679 size_t n; 2680 int on; 2681 int error; 2682 int resid; 2683 u_offset_t offset; 2684 mntinfo4_t *mi; 2685 uint_t bsize; 2686 2687 rp = VTOR4(vp); 2688 2689 if (IS_SHADOW(vp, rp)) 2690 vp = RTOV4(rp); 2691 2692 if (vp->v_type != VREG) 2693 return (EISDIR); 2694 2695 mi = VTOMI4(vp); 2696 2697 if (nfs_zone() != mi->mi_zone) 2698 return (EIO); 2699 2700 if (uiop->uio_resid == 0) 2701 return (0); 2702 2703 mutex_enter(&rp->r_statelock); 2704 if (rp->r_flags & R4RECOVERRP) 2705 error = (rp->r_error ? rp->r_error : EIO); 2706 else 2707 error = 0; 2708 mutex_exit(&rp->r_statelock); 2709 if (error) 2710 return (error); 2711 2712 if (ioflag & FAPPEND) { 2713 struct vattr va; 2714 2715 /* 2716 * Must serialize if appending. 2717 */ 2718 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 2719 nfs_rw_exit(&rp->r_rwlock); 2720 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 2721 INTR(vp))) 2722 return (EINTR); 2723 } 2724 2725 va.va_mask = AT_SIZE; 2726 error = nfs4getattr(vp, &va, cr); 2727 if (error) 2728 return (error); 2729 uiop->uio_loffset = va.va_size; 2730 } 2731 2732 offset = uiop->uio_loffset + uiop->uio_resid; 2733 2734 if (uiop->uio_loffset < (offset_t)0 || offset < 0) 2735 return (EINVAL); 2736 2737 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 2738 limit = MAXOFFSET_T; 2739 2740 /* 2741 * Check to make sure that the process will not exceed 2742 * its limit on file size. It is okay to write up to 2743 * the limit, but not beyond. Thus, the write which 2744 * reaches the limit will be short and the next write 2745 * will return an error. 2746 */ 2747 remainder = 0; 2748 if (offset > uiop->uio_llimit) { 2749 remainder = offset - uiop->uio_llimit; 2750 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset; 2751 if (uiop->uio_resid <= 0) { 2752 proc_t *p = ttoproc(curthread); 2753 2754 uiop->uio_resid += remainder; 2755 mutex_enter(&p->p_lock); 2756 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 2757 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 2758 mutex_exit(&p->p_lock); 2759 return (EFBIG); 2760 } 2761 } 2762 2763 /* update the change attribute, if we have a write delegation */ 2764 2765 mutex_enter(&rp->r_statev4_lock); 2766 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) 2767 rp->r_deleg_change++; 2768 2769 mutex_exit(&rp->r_statev4_lock); 2770 2771 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 2772 return (EINTR); 2773 2774 /* 2775 * Bypass VM if caching has been disabled (e.g., locking) or if 2776 * using client-side direct I/O and the file is not mmap'd and 2777 * there are no cached pages. 2778 */ 2779 if ((vp->v_flag & VNOCACHE) || 2780 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) && 2781 rp->r_mapcnt == 0 && !nfs4_has_pages(vp))) { 2782 size_t bufsize; 2783 int count; 2784 u_offset_t org_offset; 2785 stable_how4 stab_comm; 2786 nfs4_fwrite: 2787 if (rp->r_flags & R4STALE) { 2788 resid = uiop->uio_resid; 2789 offset = uiop->uio_loffset; 2790 error = rp->r_error; 2791 goto bottom; 2792 } 2793 2794 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 2795 base = kmem_alloc(bufsize, KM_SLEEP); 2796 do { 2797 if (ioflag & FDSYNC) 2798 stab_comm = DATA_SYNC4; 2799 else 2800 stab_comm = FILE_SYNC4; 2801 resid = uiop->uio_resid; 2802 offset = uiop->uio_loffset; 2803 count = MIN(uiop->uio_resid, bufsize); 2804 org_offset = uiop->uio_loffset; 2805 error = uiomove(base, count, UIO_WRITE, uiop); 2806 if (!error) { 2807 error = nfs4write(vp, base, org_offset, 2808 count, cr, &stab_comm); 2809 if (!error) { 2810 mutex_enter(&rp->r_statelock); 2811 if (rp->r_size < uiop->uio_loffset) 2812 rp->r_size = uiop->uio_loffset; 2813 mutex_exit(&rp->r_statelock); 2814 } 2815 } 2816 } while (!error && uiop->uio_resid > 0); 2817 kmem_free(base, bufsize); 2818 goto bottom; 2819 } 2820 2821 bsize = vp->v_vfsp->vfs_bsize; 2822 2823 do { 2824 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 2825 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 2826 n = MIN(MAXBSIZE - on, uiop->uio_resid); 2827 2828 resid = uiop->uio_resid; 2829 offset = uiop->uio_loffset; 2830 2831 if (rp->r_flags & R4STALE) { 2832 error = rp->r_error; 2833 break; 2834 } 2835 2836 /* 2837 * Don't create dirty pages faster than they 2838 * can be cleaned so that the system doesn't 2839 * get imbalanced. If the async queue is 2840 * maxed out, then wait for it to drain before 2841 * creating more dirty pages. Also, wait for 2842 * any threads doing pagewalks in the vop_getattr 2843 * entry points so that they don't block for 2844 * long periods. 2845 */ 2846 mutex_enter(&rp->r_statelock); 2847 while ((mi->mi_max_threads != 0 && 2848 rp->r_awcount > 2 * mi->mi_max_threads) || 2849 rp->r_gcount > 0) 2850 cv_wait(&rp->r_cv, &rp->r_statelock); 2851 mutex_exit(&rp->r_statelock); 2852 2853 if (vpm_enable) { 2854 /* 2855 * It will use kpm mappings, so no need to 2856 * pass an address. 2857 */ 2858 error = writerp4(rp, NULL, n, uiop, 0); 2859 } else { 2860 if (segmap_kpm) { 2861 int pon = uiop->uio_loffset & PAGEOFFSET; 2862 size_t pn = MIN(PAGESIZE - pon, 2863 uiop->uio_resid); 2864 int pagecreate; 2865 2866 mutex_enter(&rp->r_statelock); 2867 pagecreate = (pon == 0) && (pn == PAGESIZE || 2868 uiop->uio_loffset + pn >= rp->r_size); 2869 mutex_exit(&rp->r_statelock); 2870 2871 base = segmap_getmapflt(segkmap, vp, off + on, 2872 pn, !pagecreate, S_WRITE); 2873 2874 error = writerp4(rp, base + pon, n, uiop, 2875 pagecreate); 2876 2877 } else { 2878 base = segmap_getmapflt(segkmap, vp, off + on, 2879 n, 0, S_READ); 2880 error = writerp4(rp, base + on, n, uiop, 0); 2881 } 2882 } 2883 2884 if (!error) { 2885 if (mi->mi_flags & MI4_NOAC) 2886 flags = SM_WRITE; 2887 else if ((uiop->uio_loffset % bsize) == 0 || 2888 IS_SWAPVP(vp)) { 2889 /* 2890 * Have written a whole block. 2891 * Start an asynchronous write 2892 * and mark the buffer to 2893 * indicate that it won't be 2894 * needed again soon. 2895 */ 2896 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 2897 } else 2898 flags = 0; 2899 if ((ioflag & (FSYNC|FDSYNC)) || 2900 (rp->r_flags & R4OUTOFSPACE)) { 2901 flags &= ~SM_ASYNC; 2902 flags |= SM_WRITE; 2903 } 2904 if (vpm_enable) { 2905 error = vpm_sync_pages(vp, off, n, flags); 2906 } else { 2907 error = segmap_release(segkmap, base, flags); 2908 } 2909 } else { 2910 if (vpm_enable) { 2911 (void) vpm_sync_pages(vp, off, n, 0); 2912 } else { 2913 (void) segmap_release(segkmap, base, 0); 2914 } 2915 /* 2916 * In the event that we got an access error while 2917 * faulting in a page for a write-only file just 2918 * force a write. 2919 */ 2920 if (error == EACCES) 2921 goto nfs4_fwrite; 2922 } 2923 } while (!error && uiop->uio_resid > 0); 2924 2925 bottom: 2926 if (error) { 2927 uiop->uio_resid = resid + remainder; 2928 uiop->uio_loffset = offset; 2929 } else { 2930 uiop->uio_resid += remainder; 2931 2932 mutex_enter(&rp->r_statev4_lock); 2933 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 2934 gethrestime(&rp->r_attr.va_mtime); 2935 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 2936 } 2937 mutex_exit(&rp->r_statev4_lock); 2938 } 2939 2940 nfs_rw_exit(&rp->r_lkserlock); 2941 2942 return (error); 2943 } 2944 2945 /* 2946 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 2947 */ 2948 static int 2949 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 2950 int flags, cred_t *cr) 2951 { 2952 struct buf *bp; 2953 int error; 2954 page_t *savepp; 2955 uchar_t fsdata; 2956 stable_how4 stab_comm; 2957 2958 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 2959 bp = pageio_setup(pp, len, vp, flags); 2960 ASSERT(bp != NULL); 2961 2962 /* 2963 * pageio_setup should have set b_addr to 0. This 2964 * is correct since we want to do I/O on a page 2965 * boundary. bp_mapin will use this addr to calculate 2966 * an offset, and then set b_addr to the kernel virtual 2967 * address it allocated for us. 2968 */ 2969 ASSERT(bp->b_un.b_addr == 0); 2970 2971 bp->b_edev = 0; 2972 bp->b_dev = 0; 2973 bp->b_lblkno = lbtodb(off); 2974 bp->b_file = vp; 2975 bp->b_offset = (offset_t)off; 2976 bp_mapin(bp); 2977 2978 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 2979 freemem > desfree) 2980 stab_comm = UNSTABLE4; 2981 else 2982 stab_comm = FILE_SYNC4; 2983 2984 error = nfs4_bio(bp, &stab_comm, cr, FALSE); 2985 2986 bp_mapout(bp); 2987 pageio_done(bp); 2988 2989 if (stab_comm == UNSTABLE4) 2990 fsdata = C_DELAYCOMMIT; 2991 else 2992 fsdata = C_NOCOMMIT; 2993 2994 savepp = pp; 2995 do { 2996 pp->p_fsdata = fsdata; 2997 } while ((pp = pp->p_next) != savepp); 2998 2999 return (error); 3000 } 3001 3002 /* 3003 */ 3004 static int 3005 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr) 3006 { 3007 nfs4_open_owner_t *oop; 3008 nfs4_open_stream_t *osp; 3009 rnode4_t *rp = VTOR4(vp); 3010 mntinfo4_t *mi = VTOMI4(vp); 3011 int reopen_needed; 3012 3013 ASSERT(nfs_zone() == mi->mi_zone); 3014 3015 3016 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 3017 if (!oop) 3018 return (EIO); 3019 3020 /* returns with 'os_sync_lock' held */ 3021 osp = find_open_stream(oop, rp); 3022 if (!osp) { 3023 open_owner_rele(oop); 3024 return (EIO); 3025 } 3026 3027 if (osp->os_failed_reopen) { 3028 mutex_exit(&osp->os_sync_lock); 3029 open_stream_rele(osp, rp); 3030 open_owner_rele(oop); 3031 return (EIO); 3032 } 3033 3034 /* 3035 * Determine whether a reopen is needed. If this 3036 * is a delegation open stream, then the os_delegation bit 3037 * should be set. 3038 */ 3039 3040 reopen_needed = osp->os_delegation; 3041 3042 mutex_exit(&osp->os_sync_lock); 3043 open_owner_rele(oop); 3044 3045 if (reopen_needed) { 3046 nfs4_error_zinit(ep); 3047 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE); 3048 mutex_enter(&osp->os_sync_lock); 3049 if (ep->error || ep->stat || osp->os_failed_reopen) { 3050 mutex_exit(&osp->os_sync_lock); 3051 open_stream_rele(osp, rp); 3052 return (EIO); 3053 } 3054 mutex_exit(&osp->os_sync_lock); 3055 } 3056 open_stream_rele(osp, rp); 3057 3058 return (0); 3059 } 3060 3061 /* 3062 * Write to file. Writes to remote server in largest size 3063 * chunks that the server can handle. Write is synchronous. 3064 */ 3065 static int 3066 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 3067 stable_how4 *stab_comm) 3068 { 3069 mntinfo4_t *mi; 3070 COMPOUND4args_clnt args; 3071 COMPOUND4res_clnt res; 3072 WRITE4args *wargs; 3073 WRITE4res *wres; 3074 nfs_argop4 argop[2]; 3075 nfs_resop4 *resop; 3076 int tsize; 3077 stable_how4 stable; 3078 rnode4_t *rp; 3079 int doqueue = 1; 3080 bool_t needrecov; 3081 nfs4_recov_state_t recov_state; 3082 nfs4_stateid_types_t sid_types; 3083 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3084 3085 rp = VTOR4(vp); 3086 mi = VTOMI4(vp); 3087 3088 ASSERT(nfs_zone() == mi->mi_zone); 3089 3090 stable = *stab_comm; 3091 *stab_comm = FILE_SYNC4; 3092 3093 needrecov = FALSE; 3094 recov_state.rs_flags = 0; 3095 recov_state.rs_num_retry_despite_err = 0; 3096 nfs4_init_stateid_types(&sid_types); 3097 3098 recov_retry: 3099 args.ctag = TAG_WRITE; 3100 args.array_len = 2; 3101 args.array = argop; 3102 3103 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3104 &recov_state, NULL); 3105 if (e.error) 3106 return (e.error); 3107 3108 /* 0. putfh target fh */ 3109 argop[0].argop = OP_CPUTFH; 3110 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3111 3112 /* 1. write */ 3113 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types); 3114 3115 do { 3116 3117 wargs->offset = (offset4)offset; 3118 wargs->data_val = base; 3119 3120 if (mi->mi_io_kstats) { 3121 mutex_enter(&mi->mi_lock); 3122 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3123 mutex_exit(&mi->mi_lock); 3124 } 3125 3126 if ((vp->v_flag & VNOCACHE) || 3127 (rp->r_flags & R4DIRECTIO) || 3128 (mi->mi_flags & MI4_DIRECTIO)) 3129 tsize = MIN(mi->mi_stsize, count); 3130 else 3131 tsize = MIN(mi->mi_curwrite, count); 3132 wargs->data_len = (uint_t)tsize; 3133 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3134 3135 if (mi->mi_io_kstats) { 3136 mutex_enter(&mi->mi_lock); 3137 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3138 mutex_exit(&mi->mi_lock); 3139 } 3140 3141 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3142 if (e.error && !needrecov) { 3143 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3144 &recov_state, needrecov); 3145 return (e.error); 3146 } 3147 3148 3149 /* 3150 * Do handling of OLD_STATEID outside 3151 * of the normal recovery framework. 3152 * 3153 * If write receives a BAD stateid error while using a 3154 * delegation stateid, retry using the open stateid (if it 3155 * exists). If it doesn't have an open stateid, reopen the 3156 * file first, then retry. 3157 */ 3158 if (!e.error && res.status == NFS4ERR_OLD_STATEID && 3159 sid_types.cur_sid_type != SPEC_SID) { 3160 nfs4_save_stateid(&wargs->stateid, &sid_types); 3161 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3162 &recov_state, needrecov); 3163 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3164 goto recov_retry; 3165 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3166 sid_types.cur_sid_type == DEL_SID) { 3167 nfs4_save_stateid(&wargs->stateid, &sid_types); 3168 mutex_enter(&rp->r_statev4_lock); 3169 rp->r_deleg_return_pending = TRUE; 3170 mutex_exit(&rp->r_statev4_lock); 3171 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3172 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3173 &recov_state, needrecov); 3174 (void) xdr_free(xdr_COMPOUND4res_clnt, 3175 (caddr_t)&res); 3176 return (EIO); 3177 } 3178 nfs4_end_fop(mi, vp, NULL, OH_WRITE, 3179 &recov_state, needrecov); 3180 /* hold needed for nfs4delegreturn_thread */ 3181 VN_HOLD(vp); 3182 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3183 NFS4_DR_DISCARD), FALSE); 3184 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3185 goto recov_retry; 3186 } 3187 3188 if (needrecov) { 3189 bool_t abort; 3190 3191 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3192 "nfs4write: client got error %d, res.status %d" 3193 ", so start recovery", e.error, res.status)); 3194 3195 abort = nfs4_start_recovery(&e, 3196 VTOMI4(vp), vp, NULL, &wargs->stateid, 3197 NULL, OP_WRITE, NULL); 3198 if (!e.error) { 3199 e.error = geterrno4(res.status); 3200 (void) xdr_free(xdr_COMPOUND4res_clnt, 3201 (caddr_t)&res); 3202 } 3203 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3204 &recov_state, needrecov); 3205 if (abort == FALSE) 3206 goto recov_retry; 3207 return (e.error); 3208 } 3209 3210 if (res.status) { 3211 e.error = geterrno4(res.status); 3212 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3213 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3214 &recov_state, needrecov); 3215 return (e.error); 3216 } 3217 3218 resop = &res.array[1]; /* write res */ 3219 wres = &resop->nfs_resop4_u.opwrite; 3220 3221 if ((int)wres->count > tsize) { 3222 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3223 3224 zcmn_err(getzoneid(), CE_WARN, 3225 "nfs4write: server wrote %u, requested was %u", 3226 (int)wres->count, tsize); 3227 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3228 &recov_state, needrecov); 3229 return (EIO); 3230 } 3231 if (wres->committed == UNSTABLE4) { 3232 *stab_comm = UNSTABLE4; 3233 if (wargs->stable == DATA_SYNC4 || 3234 wargs->stable == FILE_SYNC4) { 3235 (void) xdr_free(xdr_COMPOUND4res_clnt, 3236 (caddr_t)&res); 3237 zcmn_err(getzoneid(), CE_WARN, 3238 "nfs4write: server %s did not commit " 3239 "to stable storage", 3240 rp->r_server->sv_hostname); 3241 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, 3242 &recov_state, needrecov); 3243 return (EIO); 3244 } 3245 } 3246 3247 tsize = (int)wres->count; 3248 count -= tsize; 3249 base += tsize; 3250 offset += tsize; 3251 if (mi->mi_io_kstats) { 3252 mutex_enter(&mi->mi_lock); 3253 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 3254 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 3255 tsize; 3256 mutex_exit(&mi->mi_lock); 3257 } 3258 lwp_stat_update(LWP_STAT_OUBLK, 1); 3259 mutex_enter(&rp->r_statelock); 3260 if (rp->r_flags & R4HAVEVERF) { 3261 if (rp->r_writeverf != wres->writeverf) { 3262 nfs4_set_mod(vp); 3263 rp->r_writeverf = wres->writeverf; 3264 } 3265 } else { 3266 rp->r_writeverf = wres->writeverf; 3267 rp->r_flags |= R4HAVEVERF; 3268 } 3269 PURGE_ATTRCACHE4_LOCKED(rp); 3270 rp->r_flags |= R4WRITEMODIFIED; 3271 gethrestime(&rp->r_attr.va_mtime); 3272 rp->r_attr.va_ctime = rp->r_attr.va_mtime; 3273 mutex_exit(&rp->r_statelock); 3274 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3275 } while (count); 3276 3277 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state, needrecov); 3278 3279 return (e.error); 3280 } 3281 3282 /* 3283 * Read from a file. Reads data in largest chunks our interface can handle. 3284 */ 3285 static int 3286 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count, 3287 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop) 3288 { 3289 mntinfo4_t *mi; 3290 COMPOUND4args_clnt args; 3291 COMPOUND4res_clnt res; 3292 READ4args *rargs; 3293 nfs_argop4 argop[2]; 3294 int tsize; 3295 int doqueue; 3296 rnode4_t *rp; 3297 int data_len; 3298 bool_t is_eof; 3299 bool_t needrecov = FALSE; 3300 nfs4_recov_state_t recov_state; 3301 nfs4_stateid_types_t sid_types; 3302 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3303 3304 rp = VTOR4(vp); 3305 mi = VTOMI4(vp); 3306 doqueue = 1; 3307 3308 ASSERT(nfs_zone() == mi->mi_zone); 3309 3310 args.ctag = async ? TAG_READAHEAD : TAG_READ; 3311 3312 args.array_len = 2; 3313 args.array = argop; 3314 3315 nfs4_init_stateid_types(&sid_types); 3316 3317 recov_state.rs_flags = 0; 3318 recov_state.rs_num_retry_despite_err = 0; 3319 3320 recov_retry: 3321 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ, 3322 &recov_state, NULL); 3323 if (e.error) 3324 return (e.error); 3325 3326 /* putfh target fh */ 3327 argop[0].argop = OP_CPUTFH; 3328 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3329 3330 /* read */ 3331 argop[1].argop = OP_READ; 3332 rargs = &argop[1].nfs_argop4_u.opread; 3333 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi, 3334 OP_READ, &sid_types, async); 3335 3336 do { 3337 if (mi->mi_io_kstats) { 3338 mutex_enter(&mi->mi_lock); 3339 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3340 mutex_exit(&mi->mi_lock); 3341 } 3342 3343 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 3344 "nfs4read: %s call, rp %s", 3345 needrecov ? "recov" : "first", 3346 rnode4info(rp))); 3347 3348 if ((vp->v_flag & VNOCACHE) || 3349 (rp->r_flags & R4DIRECTIO) || 3350 (mi->mi_flags & MI4_DIRECTIO)) 3351 tsize = MIN(mi->mi_tsize, count); 3352 else 3353 tsize = MIN(mi->mi_curread, count); 3354 rargs->offset = (offset4)offset; 3355 rargs->count = (count4)tsize; 3356 rargs->res_data_val_alt = NULL; 3357 rargs->res_mblk = NULL; 3358 rargs->res_uiop = NULL; 3359 rargs->res_maxsize = 0; 3360 if (uiop) 3361 rargs->res_uiop = uiop; 3362 else 3363 rargs->res_data_val_alt = base; 3364 rargs->res_maxsize = tsize; 3365 3366 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 3367 #ifdef DEBUG 3368 if (nfs4read_error_inject) { 3369 res.status = nfs4read_error_inject; 3370 nfs4read_error_inject = 0; 3371 } 3372 #endif 3373 3374 if (mi->mi_io_kstats) { 3375 mutex_enter(&mi->mi_lock); 3376 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3377 mutex_exit(&mi->mi_lock); 3378 } 3379 3380 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 3381 if (e.error != 0 && !needrecov) { 3382 nfs4_end_fop(mi, vp, NULL, OH_READ, 3383 &recov_state, needrecov); 3384 return (e.error); 3385 } 3386 3387 /* 3388 * Do proper retry for OLD and BAD stateid errors outside 3389 * of the normal recovery framework. There are two differences 3390 * between async and sync reads. The first is that we allow 3391 * retry on BAD_STATEID for async reads, but not sync reads. 3392 * The second is that we mark the file dead for a failed 3393 * attempt with a special stateid for sync reads, but just 3394 * return EIO for async reads. 3395 * 3396 * If a sync read receives a BAD stateid error while using a 3397 * delegation stateid, retry using the open stateid (if it 3398 * exists). If it doesn't have an open stateid, reopen the 3399 * file first, then retry. 3400 */ 3401 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID || 3402 res.status == NFS4ERR_BAD_STATEID) && async) { 3403 nfs4_end_fop(mi, vp, NULL, OH_READ, 3404 &recov_state, needrecov); 3405 if (sid_types.cur_sid_type == SPEC_SID) { 3406 (void) xdr_free(xdr_COMPOUND4res_clnt, 3407 (caddr_t)&res); 3408 return (EIO); 3409 } 3410 nfs4_save_stateid(&rargs->stateid, &sid_types); 3411 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3412 goto recov_retry; 3413 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3414 !async && sid_types.cur_sid_type != SPEC_SID) { 3415 nfs4_save_stateid(&rargs->stateid, &sid_types); 3416 nfs4_end_fop(mi, vp, NULL, OH_READ, 3417 &recov_state, needrecov); 3418 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3419 goto recov_retry; 3420 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID && 3421 sid_types.cur_sid_type == DEL_SID) { 3422 nfs4_save_stateid(&rargs->stateid, &sid_types); 3423 mutex_enter(&rp->r_statev4_lock); 3424 rp->r_deleg_return_pending = TRUE; 3425 mutex_exit(&rp->r_statev4_lock); 3426 if (nfs4rdwr_check_osid(vp, &e, cr)) { 3427 nfs4_end_fop(mi, vp, NULL, OH_READ, 3428 &recov_state, needrecov); 3429 (void) xdr_free(xdr_COMPOUND4res_clnt, 3430 (caddr_t)&res); 3431 return (EIO); 3432 } 3433 nfs4_end_fop(mi, vp, NULL, OH_READ, 3434 &recov_state, needrecov); 3435 /* hold needed for nfs4delegreturn_thread */ 3436 VN_HOLD(vp); 3437 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN| 3438 NFS4_DR_DISCARD), FALSE); 3439 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3440 goto recov_retry; 3441 } 3442 if (needrecov) { 3443 bool_t abort; 3444 3445 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 3446 "nfs4read: initiating recovery\n")); 3447 3448 abort = nfs4_start_recovery(&e, 3449 mi, vp, NULL, &rargs->stateid, 3450 NULL, OP_READ, NULL); 3451 nfs4_end_fop(mi, vp, NULL, OH_READ, 3452 &recov_state, needrecov); 3453 /* 3454 * Do not retry if we got OLD_STATEID using a special 3455 * stateid. This avoids looping with a broken server. 3456 */ 3457 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3458 sid_types.cur_sid_type == SPEC_SID) 3459 abort = TRUE; 3460 3461 if (abort == FALSE) { 3462 /* 3463 * Need to retry all possible stateids in 3464 * case the recovery error wasn't stateid 3465 * related or the stateids have become 3466 * stale (server reboot). 3467 */ 3468 nfs4_init_stateid_types(&sid_types); 3469 (void) xdr_free(xdr_COMPOUND4res_clnt, 3470 (caddr_t)&res); 3471 goto recov_retry; 3472 } 3473 3474 if (!e.error) { 3475 e.error = geterrno4(res.status); 3476 (void) xdr_free(xdr_COMPOUND4res_clnt, 3477 (caddr_t)&res); 3478 } 3479 return (e.error); 3480 } 3481 3482 if (res.status) { 3483 e.error = geterrno4(res.status); 3484 nfs4_end_fop(mi, vp, NULL, OH_READ, 3485 &recov_state, needrecov); 3486 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3487 return (e.error); 3488 } 3489 3490 data_len = res.array[1].nfs_resop4_u.opread.data_len; 3491 count -= data_len; 3492 if (base) 3493 base += data_len; 3494 offset += data_len; 3495 if (mi->mi_io_kstats) { 3496 mutex_enter(&mi->mi_lock); 3497 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3498 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len; 3499 mutex_exit(&mi->mi_lock); 3500 } 3501 lwp_stat_update(LWP_STAT_INBLK, 1); 3502 is_eof = res.array[1].nfs_resop4_u.opread.eof; 3503 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3504 3505 } while (count && !is_eof); 3506 3507 *residp = count; 3508 3509 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov); 3510 3511 return (e.error); 3512 } 3513 3514 /* ARGSUSED */ 3515 static int 3516 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 3517 { 3518 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3519 return (EIO); 3520 switch (cmd) { 3521 case _FIODIRECTIO: 3522 return (nfs4_directio(vp, (int)arg, cr)); 3523 default: 3524 return (ENOTTY); 3525 } 3526 } 3527 3528 int 3529 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr) 3530 { 3531 int error; 3532 rnode4_t *rp = VTOR4(vp); 3533 3534 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3535 return (EIO); 3536 /* 3537 * If it has been specified that the return value will 3538 * just be used as a hint, and we are only being asked 3539 * for size, fsid or rdevid, then return the client's 3540 * notion of these values without checking to make sure 3541 * that the attribute cache is up to date. 3542 * The whole point is to avoid an over the wire GETATTR 3543 * call. 3544 */ 3545 if (flags & ATTR_HINT) { 3546 if (vap->va_mask == 3547 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) { 3548 mutex_enter(&rp->r_statelock); 3549 if (vap->va_mask | AT_SIZE) 3550 vap->va_size = rp->r_size; 3551 if (vap->va_mask | AT_FSID) 3552 vap->va_fsid = rp->r_attr.va_fsid; 3553 if (vap->va_mask | AT_RDEV) 3554 vap->va_rdev = rp->r_attr.va_rdev; 3555 mutex_exit(&rp->r_statelock); 3556 return (0); 3557 } 3558 } 3559 3560 /* 3561 * Only need to flush pages if asking for the mtime 3562 * and if there any dirty pages or any outstanding 3563 * asynchronous (write) requests for this file. 3564 */ 3565 if (vap->va_mask & AT_MTIME) { 3566 rp = VTOR4(vp); 3567 if (nfs4_has_pages(vp)) { 3568 mutex_enter(&rp->r_statev4_lock); 3569 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) { 3570 mutex_exit(&rp->r_statev4_lock); 3571 if (rp->r_flags & R4DIRTY || 3572 rp->r_awcount > 0) { 3573 mutex_enter(&rp->r_statelock); 3574 rp->r_gcount++; 3575 mutex_exit(&rp->r_statelock); 3576 error = 3577 nfs4_putpage(vp, (u_offset_t)0, 3578 0, 0, cr); 3579 mutex_enter(&rp->r_statelock); 3580 if (error && (error == ENOSPC || 3581 error == EDQUOT)) { 3582 if (!rp->r_error) 3583 rp->r_error = error; 3584 } 3585 if (--rp->r_gcount == 0) 3586 cv_broadcast(&rp->r_cv); 3587 mutex_exit(&rp->r_statelock); 3588 } 3589 } else { 3590 mutex_exit(&rp->r_statev4_lock); 3591 } 3592 } 3593 } 3594 return (nfs4getattr(vp, vap, cr)); 3595 } 3596 3597 int 3598 nfs4_compare_modes(mode_t from_server, mode_t on_client) 3599 { 3600 /* 3601 * If these are the only two bits cleared 3602 * on the server then return 0 (OK) else 3603 * return 1 (BAD). 3604 */ 3605 on_client &= ~(S_ISUID|S_ISGID); 3606 if (on_client == from_server) 3607 return (0); 3608 else 3609 return (1); 3610 } 3611 3612 /*ARGSUSED4*/ 3613 static int 3614 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3615 caller_context_t *ct) 3616 { 3617 if (vap->va_mask & AT_NOSET) 3618 return (EINVAL); 3619 3620 if (nfs_zone() != VTOMI4(vp)->mi_zone) 3621 return (EIO); 3622 3623 /* 3624 * Don't call secpolicy_vnode_setattr, the client cannot 3625 * use its cached attributes to make security decisions 3626 * as the server may be faking mode bits or mapping uid/gid. 3627 * Always just let the server to the checking. 3628 * If we provide the ability to remove basic priviledges 3629 * to setattr (e.g. basic without chmod) then we will 3630 * need to add a check here before calling the server. 3631 */ 3632 3633 return (nfs4setattr(vp, vap, flags, cr, NULL)); 3634 } 3635 3636 /* 3637 * To replace the "guarded" version 3 setattr, we use two types of compound 3638 * setattr requests: 3639 * 1. The "normal" setattr, used when the size of the file isn't being 3640 * changed - { Putfh <fh>; Setattr; Getattr }/ 3641 * 2. If the size is changed, precede Setattr with: Getattr; Verify 3642 * with only ctime as the argument. If the server ctime differs from 3643 * what is cached on the client, the verify will fail, but we would 3644 * already have the ctime from the preceding getattr, so just set it 3645 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify; 3646 * Setattr; Getattr }. 3647 * 3648 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in 3649 * this setattr and NULL if they are not. 3650 */ 3651 static int 3652 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 3653 vsecattr_t *vsap) 3654 { 3655 COMPOUND4args_clnt args; 3656 COMPOUND4res_clnt res, *resp = NULL; 3657 nfs4_ga_res_t *garp = NULL; 3658 int numops = 3; /* { Putfh; Setattr; Getattr } */ 3659 nfs_argop4 argop[5]; 3660 int verify_argop = -1; 3661 int setattr_argop = 1; 3662 nfs_resop4 *resop; 3663 vattr_t va; 3664 rnode4_t *rp; 3665 int doqueue = 1; 3666 uint_t mask = vap->va_mask; 3667 mode_t omode; 3668 vsecattr_t *vsp; 3669 timestruc_t ctime; 3670 bool_t needrecov = FALSE; 3671 nfs4_recov_state_t recov_state; 3672 nfs4_stateid_types_t sid_types; 3673 stateid4 stateid; 3674 hrtime_t t; 3675 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 3676 servinfo4_t *svp; 3677 bitmap4 supp_attrs; 3678 3679 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 3680 rp = VTOR4(vp); 3681 nfs4_init_stateid_types(&sid_types); 3682 3683 /* 3684 * Only need to flush pages if there are any pages and 3685 * if the file is marked as dirty in some fashion. The 3686 * file must be flushed so that we can accurately 3687 * determine the size of the file and the cached data 3688 * after the SETATTR returns. A file is considered to 3689 * be dirty if it is either marked with R4DIRTY, has 3690 * outstanding i/o's active, or is mmap'd. In this 3691 * last case, we can't tell whether there are dirty 3692 * pages, so we flush just to be sure. 3693 */ 3694 if (nfs4_has_pages(vp) && 3695 ((rp->r_flags & R4DIRTY) || 3696 rp->r_count > 0 || 3697 rp->r_mapcnt > 0)) { 3698 ASSERT(vp->v_type != VCHR); 3699 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr); 3700 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 3701 mutex_enter(&rp->r_statelock); 3702 if (!rp->r_error) 3703 rp->r_error = e.error; 3704 mutex_exit(&rp->r_statelock); 3705 } 3706 } 3707 3708 if (mask & AT_SIZE) { 3709 /* 3710 * Verification setattr compound for non-deleg AT_SIZE: 3711 * { Putfh; Getattr; Verify; Setattr; Getattr } 3712 * Set ctime local here (outside the do_again label) 3713 * so that subsequent retries (after failed VERIFY) 3714 * will use ctime from GETATTR results (from failed 3715 * verify compound) as VERIFY arg. 3716 * If file has delegation, then VERIFY(time_metadata) 3717 * is of little added value, so don't bother. 3718 */ 3719 mutex_enter(&rp->r_statev4_lock); 3720 if (rp->r_deleg_type == OPEN_DELEGATE_NONE || 3721 rp->r_deleg_return_pending) { 3722 numops = 5; 3723 ctime = rp->r_attr.va_ctime; 3724 } 3725 mutex_exit(&rp->r_statev4_lock); 3726 } 3727 3728 recov_state.rs_flags = 0; 3729 recov_state.rs_num_retry_despite_err = 0; 3730 3731 args.ctag = TAG_SETATTR; 3732 do_again: 3733 recov_retry: 3734 setattr_argop = numops - 2; 3735 3736 args.array = argop; 3737 args.array_len = numops; 3738 3739 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 3740 if (e.error) 3741 return (e.error); 3742 3743 3744 /* putfh target fh */ 3745 argop[0].argop = OP_CPUTFH; 3746 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 3747 3748 if (numops == 5) { 3749 /* 3750 * We only care about the ctime, but need to get mtime 3751 * and size for proper cache update. 3752 */ 3753 /* getattr */ 3754 argop[1].argop = OP_GETATTR; 3755 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3756 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3757 3758 /* verify - set later in loop */ 3759 verify_argop = 2; 3760 } 3761 3762 /* setattr */ 3763 svp = rp->r_server; 3764 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3765 supp_attrs = svp->sv_supp_attrs; 3766 nfs_rw_exit(&svp->sv_lock); 3767 3768 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr, 3769 supp_attrs, &e.error, &sid_types); 3770 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid; 3771 if (e.error) { 3772 /* req time field(s) overflow - return immediately */ 3773 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3774 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3775 opsetattr.obj_attributes); 3776 return (e.error); 3777 } 3778 omode = rp->r_attr.va_mode; 3779 3780 /* getattr */ 3781 argop[numops-1].argop = OP_GETATTR; 3782 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 3783 /* 3784 * If we are setting the ACL (indicated only by vsap != NULL), request 3785 * the ACL in this getattr. The ACL returned from this getattr will be 3786 * used in updating the ACL cache. 3787 */ 3788 if (vsap != NULL) 3789 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |= 3790 FATTR4_ACL_MASK; 3791 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 3792 3793 /* 3794 * setattr iterates if the object size is set and the cached ctime 3795 * does not match the file ctime. In that case, verify the ctime first. 3796 */ 3797 3798 do { 3799 if (verify_argop != -1) { 3800 /* 3801 * Verify that the ctime match before doing setattr. 3802 */ 3803 va.va_mask = AT_CTIME; 3804 va.va_ctime = ctime; 3805 svp = rp->r_server; 3806 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 3807 supp_attrs = svp->sv_supp_attrs; 3808 nfs_rw_exit(&svp->sv_lock); 3809 e.error = nfs4args_verify(&argop[verify_argop], &va, 3810 OP_VERIFY, supp_attrs); 3811 if (e.error) { 3812 /* req time field(s) overflow - return */ 3813 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3814 needrecov); 3815 break; 3816 } 3817 } 3818 3819 doqueue = 1; 3820 3821 t = gethrtime(); 3822 3823 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 3824 3825 /* 3826 * Purge the access cache and ACL cache if changing either the 3827 * owner of the file, the group owner, or the mode. These may 3828 * change the access permissions of the file, so purge old 3829 * information and start over again. 3830 */ 3831 if (mask & (AT_UID | AT_GID | AT_MODE)) { 3832 (void) nfs4_access_purge_rp(rp); 3833 if (rp->r_secattr != NULL) { 3834 mutex_enter(&rp->r_statelock); 3835 vsp = rp->r_secattr; 3836 rp->r_secattr = NULL; 3837 mutex_exit(&rp->r_statelock); 3838 if (vsp != NULL) 3839 nfs4_acl_free_cache(vsp); 3840 } 3841 } 3842 3843 /* 3844 * If res.array_len == numops, then everything succeeded, 3845 * except for possibly the final getattr. If only the 3846 * last getattr failed, give up, and don't try recovery. 3847 */ 3848 if (res.array_len == numops) { 3849 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3850 needrecov); 3851 if (! e.error) 3852 resp = &res; 3853 break; 3854 } 3855 3856 /* 3857 * if either rpc call failed or completely succeeded - done 3858 */ 3859 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 3860 if (e.error) { 3861 PURGE_ATTRCACHE4(vp); 3862 if (!needrecov) { 3863 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3864 needrecov); 3865 break; 3866 } 3867 } 3868 3869 /* 3870 * Do proper retry for OLD_STATEID outside of the normal 3871 * recovery framework. 3872 */ 3873 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3874 sid_types.cur_sid_type != SPEC_SID && 3875 sid_types.cur_sid_type != NO_SID) { 3876 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3877 needrecov); 3878 nfs4_save_stateid(&stateid, &sid_types); 3879 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3880 opsetattr.obj_attributes); 3881 if (verify_argop != -1) { 3882 nfs4args_verify_free(&argop[verify_argop]); 3883 verify_argop = -1; 3884 } 3885 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3886 goto recov_retry; 3887 } 3888 3889 if (needrecov) { 3890 bool_t abort; 3891 3892 abort = nfs4_start_recovery(&e, 3893 VTOMI4(vp), vp, NULL, NULL, NULL, 3894 OP_SETATTR, NULL); 3895 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 3896 needrecov); 3897 /* 3898 * Do not retry if we failed with OLD_STATEID using 3899 * a special stateid. This is done to avoid looping 3900 * with a broken server. 3901 */ 3902 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID && 3903 (sid_types.cur_sid_type == SPEC_SID || 3904 sid_types.cur_sid_type == NO_SID)) 3905 abort = TRUE; 3906 if (!e.error) { 3907 if (res.status == NFS4ERR_BADOWNER) 3908 nfs4_log_badowner(VTOMI4(vp), 3909 OP_SETATTR); 3910 3911 e.error = geterrno4(res.status); 3912 (void) xdr_free(xdr_COMPOUND4res_clnt, 3913 (caddr_t)&res); 3914 } 3915 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3916 opsetattr.obj_attributes); 3917 if (verify_argop != -1) { 3918 nfs4args_verify_free(&argop[verify_argop]); 3919 verify_argop = -1; 3920 } 3921 if (abort == FALSE) { 3922 /* 3923 * Need to retry all possible stateids in 3924 * case the recovery error wasn't stateid 3925 * related or the stateids have become 3926 * stale (server reboot). 3927 */ 3928 nfs4_init_stateid_types(&sid_types); 3929 goto recov_retry; 3930 } 3931 return (e.error); 3932 } 3933 3934 /* 3935 * Need to call nfs4_end_op before nfs4getattr to 3936 * avoid potential nfs4_start_op deadlock. See RFE 3937 * 4777612. Calls to nfs4_invalidate_pages() and 3938 * nfs4_purge_stale_fh() might also generate over the 3939 * wire calls which my cause nfs4_start_op() deadlock. 3940 */ 3941 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 3942 3943 /* 3944 * Check to update lease. 3945 */ 3946 resp = &res; 3947 if (res.status == NFS4_OK) { 3948 break; 3949 } 3950 3951 /* 3952 * Check if verify failed to see if try again 3953 */ 3954 if ((verify_argop == -1) || (res.array_len != 3)) { 3955 /* 3956 * can't continue... 3957 */ 3958 if (res.status == NFS4ERR_BADOWNER) 3959 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR); 3960 3961 e.error = geterrno4(res.status); 3962 } else { 3963 /* 3964 * When the verify request fails, the client ctime is 3965 * not in sync with the server. This is the same as 3966 * the version 3 "not synchronized" error, and we 3967 * handle it in a similar manner (XXX do we need to???). 3968 * Use the ctime returned in the first getattr for 3969 * the input to the next verify. 3970 * If we couldn't get the attributes, then we give up 3971 * because we can't complete the operation as required. 3972 */ 3973 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res; 3974 } 3975 if (e.error) { 3976 PURGE_ATTRCACHE4(vp); 3977 nfs4_purge_stale_fh(e.error, vp, cr); 3978 } else { 3979 /* 3980 * retry with a new verify value 3981 */ 3982 ctime = garp->n4g_va.va_ctime; 3983 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3984 resp = NULL; 3985 } 3986 if (!e.error) { 3987 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 3988 opsetattr.obj_attributes); 3989 if (verify_argop != -1) { 3990 nfs4args_verify_free(&argop[verify_argop]); 3991 verify_argop = -1; 3992 } 3993 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 3994 goto do_again; 3995 } 3996 } while (!e.error); 3997 3998 if (e.error) { 3999 /* 4000 * If we are here, rfs4call has an irrecoverable error - return 4001 */ 4002 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4003 opsetattr.obj_attributes); 4004 if (verify_argop != -1) { 4005 nfs4args_verify_free(&argop[verify_argop]); 4006 verify_argop = -1; 4007 } 4008 if (resp) 4009 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4010 return (e.error); 4011 } 4012 4013 4014 4015 /* 4016 * If changing the size of the file, invalidate 4017 * any local cached data which is no longer part 4018 * of the file. We also possibly invalidate the 4019 * last page in the file. We could use 4020 * pvn_vpzero(), but this would mark the page as 4021 * modified and require it to be written back to 4022 * the server for no particularly good reason. 4023 * This way, if we access it, then we bring it 4024 * back in. A read should be cheaper than a 4025 * write. 4026 */ 4027 if (mask & AT_SIZE) { 4028 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr); 4029 } 4030 4031 /* either no error or one of the postop getattr failed */ 4032 4033 /* 4034 * XXX Perform a simplified version of wcc checking. Instead of 4035 * have another getattr to get pre-op, just purge cache if 4036 * any of the ops prior to and including the getattr failed. 4037 * If the getattr succeeded then update the attrcache accordingly. 4038 */ 4039 4040 garp = NULL; 4041 if (res.status == NFS4_OK) { 4042 /* 4043 * Last getattr 4044 */ 4045 resop = &res.array[numops - 1]; 4046 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4047 } 4048 /* 4049 * In certain cases, nfs4_update_attrcache() will purge the attrcache, 4050 * rather than filling it. See the function itself for details. 4051 */ 4052 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4053 if (garp != NULL) { 4054 if (garp->n4g_resbmap & FATTR4_ACL_MASK) { 4055 nfs4_acl_fill_cache(rp, &garp->n4g_vsa); 4056 vs_ace4_destroy(&garp->n4g_vsa); 4057 } else { 4058 if (vsap != NULL) { 4059 /* 4060 * The ACL was supposed to be set and to be 4061 * returned in the last getattr of this 4062 * compound, but for some reason the getattr 4063 * result doesn't contain the ACL. In this 4064 * case, purge the ACL cache. 4065 */ 4066 if (rp->r_secattr != NULL) { 4067 mutex_enter(&rp->r_statelock); 4068 vsp = rp->r_secattr; 4069 rp->r_secattr = NULL; 4070 mutex_exit(&rp->r_statelock); 4071 if (vsp != NULL) 4072 nfs4_acl_free_cache(vsp); 4073 } 4074 } 4075 } 4076 } 4077 4078 if (res.status == NFS4_OK && (mask & AT_SIZE)) { 4079 /* 4080 * Set the size, rather than relying on getting it updated 4081 * via a GETATTR. With delegations the client tries to 4082 * suppress GETATTR calls. 4083 */ 4084 mutex_enter(&rp->r_statelock); 4085 rp->r_size = vap->va_size; 4086 mutex_exit(&rp->r_statelock); 4087 } 4088 4089 /* 4090 * Can free up request args and res 4091 */ 4092 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u. 4093 opsetattr.obj_attributes); 4094 if (verify_argop != -1) { 4095 nfs4args_verify_free(&argop[verify_argop]); 4096 verify_argop = -1; 4097 } 4098 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4099 4100 /* 4101 * Some servers will change the mode to clear the setuid 4102 * and setgid bits when changing the uid or gid. The 4103 * client needs to compensate appropriately. 4104 */ 4105 if (mask & (AT_UID | AT_GID)) { 4106 int terror, do_setattr; 4107 4108 do_setattr = 0; 4109 va.va_mask = AT_MODE; 4110 terror = nfs4getattr(vp, &va, cr); 4111 if (!terror && 4112 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 4113 (!(mask & AT_MODE) && va.va_mode != omode))) { 4114 va.va_mask = AT_MODE; 4115 if (mask & AT_MODE) { 4116 /* 4117 * We asked the mode to be changed and what 4118 * we just got from the server in getattr is 4119 * not what we wanted it to be, so set it now. 4120 */ 4121 va.va_mode = vap->va_mode; 4122 do_setattr = 1; 4123 } else { 4124 /* 4125 * We did not ask the mode to be changed, 4126 * Check to see that the server just cleared 4127 * I_SUID and I_GUID from it. If not then 4128 * set mode to omode with UID/GID cleared. 4129 */ 4130 if (nfs4_compare_modes(va.va_mode, omode)) { 4131 omode &= ~(S_ISUID|S_ISGID); 4132 va.va_mode = omode; 4133 do_setattr = 1; 4134 } 4135 } 4136 4137 if (do_setattr) 4138 (void) nfs4setattr(vp, &va, 0, cr, NULL); 4139 } 4140 } 4141 4142 return (e.error); 4143 } 4144 4145 /* ARGSUSED */ 4146 static int 4147 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr) 4148 { 4149 COMPOUND4args_clnt args; 4150 COMPOUND4res_clnt res; 4151 int doqueue; 4152 uint32_t acc, resacc, argacc; 4153 rnode4_t *rp; 4154 cred_t *cred, *ncr, *ncrfree = NULL; 4155 nfs4_access_type_t cacc; 4156 int num_ops; 4157 nfs_argop4 argop[3]; 4158 nfs_resop4 *resop; 4159 bool_t needrecov = FALSE, do_getattr; 4160 nfs4_recov_state_t recov_state; 4161 int rpc_error; 4162 hrtime_t t; 4163 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4164 mntinfo4_t *mi = VTOMI4(vp); 4165 4166 if (nfs_zone() != mi->mi_zone) 4167 return (EIO); 4168 4169 acc = 0; 4170 if (mode & VREAD) 4171 acc |= ACCESS4_READ; 4172 if (mode & VWRITE) { 4173 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type)) 4174 return (EROFS); 4175 if (vp->v_type == VDIR) 4176 acc |= ACCESS4_DELETE; 4177 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND; 4178 } 4179 if (mode & VEXEC) { 4180 if (vp->v_type == VDIR) 4181 acc |= ACCESS4_LOOKUP; 4182 else 4183 acc |= ACCESS4_EXECUTE; 4184 } 4185 4186 if (VTOR4(vp)->r_acache != NULL) { 4187 e.error = nfs4_validate_caches(vp, cr); 4188 if (e.error) 4189 return (e.error); 4190 } 4191 4192 rp = VTOR4(vp); 4193 if (vp->v_type == VDIR) 4194 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY | 4195 ACCESS4_EXTEND | ACCESS4_LOOKUP; 4196 else 4197 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | 4198 ACCESS4_EXECUTE; 4199 recov_state.rs_flags = 0; 4200 recov_state.rs_num_retry_despite_err = 0; 4201 4202 cred = cr; 4203 /* 4204 * ncr and ncrfree both initially 4205 * point to the memory area returned 4206 * by crnetadjust(); 4207 * ncrfree not NULL when exiting means 4208 * that we need to release it 4209 */ 4210 ncr = crnetadjust(cred); 4211 ncrfree = ncr; 4212 4213 tryagain: 4214 cacc = nfs4_access_check(rp, acc, cred); 4215 if (cacc == NFS4_ACCESS_ALLOWED) { 4216 if (ncrfree != NULL) 4217 crfree(ncrfree); 4218 return (0); 4219 } 4220 if (cacc == NFS4_ACCESS_DENIED) { 4221 /* 4222 * If the cred can be adjusted, try again 4223 * with the new cred. 4224 */ 4225 if (ncr != NULL) { 4226 cred = ncr; 4227 ncr = NULL; 4228 goto tryagain; 4229 } 4230 if (ncrfree != NULL) 4231 crfree(ncrfree); 4232 return (EACCES); 4233 } 4234 4235 recov_retry: 4236 /* 4237 * Don't take with r_statev4_lock here. r_deleg_type could 4238 * change as soon as lock is released. Since it is an int, 4239 * there is no atomicity issue. 4240 */ 4241 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE); 4242 num_ops = do_getattr ? 3 : 2; 4243 4244 args.ctag = TAG_ACCESS; 4245 4246 args.array_len = num_ops; 4247 args.array = argop; 4248 4249 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS, 4250 &recov_state, NULL)) { 4251 if (ncrfree != NULL) 4252 crfree(ncrfree); 4253 return (e.error); 4254 } 4255 4256 /* putfh target fh */ 4257 argop[0].argop = OP_CPUTFH; 4258 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4259 4260 /* access */ 4261 argop[1].argop = OP_ACCESS; 4262 argop[1].nfs_argop4_u.opaccess.access = argacc; 4263 4264 /* getattr */ 4265 if (do_getattr) { 4266 argop[2].argop = OP_GETATTR; 4267 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4268 argop[2].nfs_argop4_u.opgetattr.mi = mi; 4269 } 4270 4271 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4272 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first", 4273 rnode4info(VTOR4(vp)))); 4274 4275 doqueue = 1; 4276 t = gethrtime(); 4277 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e); 4278 rpc_error = e.error; 4279 4280 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4281 if (needrecov) { 4282 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4283 "nfs4_access: initiating recovery\n")); 4284 4285 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4286 NULL, OP_ACCESS, NULL) == FALSE) { 4287 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS, 4288 &recov_state, needrecov); 4289 if (!e.error) 4290 (void) xdr_free(xdr_COMPOUND4res_clnt, 4291 (caddr_t)&res); 4292 goto recov_retry; 4293 } 4294 } 4295 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov); 4296 4297 if (e.error) 4298 goto out; 4299 4300 if (res.status) { 4301 e.error = geterrno4(res.status); 4302 /* 4303 * This might generate over the wire calls throught 4304 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4305 * here to avoid a deadlock. 4306 */ 4307 nfs4_purge_stale_fh(e.error, vp, cr); 4308 goto out; 4309 } 4310 resop = &res.array[1]; /* access res */ 4311 4312 resacc = resop->nfs_resop4_u.opaccess.access; 4313 4314 if (do_getattr) { 4315 resop++; /* getattr res */ 4316 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res, 4317 t, cr, FALSE, NULL); 4318 } 4319 4320 if (!e.error) { 4321 nfs4_access_cache(rp, argacc, resacc, cred); 4322 /* 4323 * we just cached results with cred; if cred is the 4324 * adjusted credentials from crnetadjust, we do not want 4325 * to release them before exiting: hence setting ncrfree 4326 * to NULL 4327 */ 4328 if (cred != cr) 4329 ncrfree = NULL; 4330 /* XXX check the supported bits too? */ 4331 if ((acc & resacc) != acc) { 4332 /* 4333 * The following code implements the semantic 4334 * that a setuid root program has *at least* the 4335 * permissions of the user that is running the 4336 * program. See rfs3call() for more portions 4337 * of the implementation of this functionality. 4338 */ 4339 /* XXX-LP */ 4340 if (ncr != NULL) { 4341 (void) xdr_free(xdr_COMPOUND4res_clnt, 4342 (caddr_t)&res); 4343 cred = ncr; 4344 ncr = NULL; 4345 goto tryagain; 4346 } 4347 e.error = EACCES; 4348 } 4349 } 4350 4351 out: 4352 if (!rpc_error) 4353 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4354 4355 if (ncrfree != NULL) 4356 crfree(ncrfree); 4357 4358 return (e.error); 4359 } 4360 4361 static int 4362 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr) 4363 { 4364 COMPOUND4args_clnt args; 4365 COMPOUND4res_clnt res; 4366 int doqueue; 4367 rnode4_t *rp; 4368 nfs_argop4 argop[3]; 4369 nfs_resop4 *resop; 4370 READLINK4res *lr_res; 4371 nfs4_ga_res_t *garp; 4372 uint_t len; 4373 char *linkdata; 4374 bool_t needrecov = FALSE; 4375 nfs4_recov_state_t recov_state; 4376 hrtime_t t; 4377 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4378 4379 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4380 return (EIO); 4381 /* 4382 * Can't readlink anything other than a symbolic link. 4383 */ 4384 if (vp->v_type != VLNK) 4385 return (EINVAL); 4386 4387 rp = VTOR4(vp); 4388 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) { 4389 e.error = nfs4_validate_caches(vp, cr); 4390 if (e.error) 4391 return (e.error); 4392 mutex_enter(&rp->r_statelock); 4393 if (rp->r_symlink.contents != NULL) { 4394 e.error = uiomove(rp->r_symlink.contents, 4395 rp->r_symlink.len, UIO_READ, uiop); 4396 mutex_exit(&rp->r_statelock); 4397 return (e.error); 4398 } 4399 mutex_exit(&rp->r_statelock); 4400 } 4401 recov_state.rs_flags = 0; 4402 recov_state.rs_num_retry_despite_err = 0; 4403 4404 recov_retry: 4405 args.array_len = 3; 4406 args.array = argop; 4407 args.ctag = TAG_READLINK; 4408 4409 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state); 4410 if (e.error) { 4411 return (e.error); 4412 } 4413 4414 /* 0. putfh symlink fh */ 4415 argop[0].argop = OP_CPUTFH; 4416 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 4417 4418 /* 1. readlink */ 4419 argop[1].argop = OP_READLINK; 4420 4421 /* 2. getattr */ 4422 argop[2].argop = OP_GETATTR; 4423 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 4424 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp); 4425 4426 doqueue = 1; 4427 4428 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 4429 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first", 4430 rnode4info(VTOR4(vp)))); 4431 4432 t = gethrtime(); 4433 4434 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e); 4435 4436 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp); 4437 if (needrecov) { 4438 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 4439 "nfs4_readlink: initiating recovery\n")); 4440 4441 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 4442 NULL, OP_READLINK, NULL) == FALSE) { 4443 if (!e.error) 4444 (void) xdr_free(xdr_COMPOUND4res_clnt, 4445 (caddr_t)&res); 4446 4447 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, 4448 needrecov); 4449 goto recov_retry; 4450 } 4451 } 4452 4453 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov); 4454 4455 if (e.error) 4456 return (e.error); 4457 4458 /* 4459 * There is an path in the code below which calls 4460 * nfs4_purge_stale_fh(), which may generate otw calls through 4461 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 4462 * here to avoid nfs4_start_op() deadlock. 4463 */ 4464 4465 if (res.status && (res.array_len < args.array_len)) { 4466 /* 4467 * either Putfh or Link failed 4468 */ 4469 e.error = geterrno4(res.status); 4470 nfs4_purge_stale_fh(e.error, vp, cr); 4471 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4472 return (e.error); 4473 } 4474 4475 resop = &res.array[1]; /* readlink res */ 4476 lr_res = &resop->nfs_resop4_u.opreadlink; 4477 4478 /* 4479 * treat symlink names as data 4480 */ 4481 linkdata = utf8_to_str(&lr_res->link, &len, NULL); 4482 if (linkdata != NULL) { 4483 int uio_len = len - 1; 4484 /* len includes null byte, which we won't uiomove */ 4485 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop); 4486 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 4487 mutex_enter(&rp->r_statelock); 4488 if (rp->r_symlink.contents == NULL) { 4489 rp->r_symlink.contents = linkdata; 4490 rp->r_symlink.len = uio_len; 4491 rp->r_symlink.size = len; 4492 mutex_exit(&rp->r_statelock); 4493 } else { 4494 mutex_exit(&rp->r_statelock); 4495 kmem_free(linkdata, len); 4496 } 4497 } else { 4498 kmem_free(linkdata, len); 4499 } 4500 } 4501 if (res.status == NFS4_OK) { 4502 resop++; /* getattr res */ 4503 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 4504 } 4505 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr); 4506 4507 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 4508 4509 /* 4510 * The over the wire error for attempting to readlink something 4511 * other than a symbolic link is ENXIO. However, we need to 4512 * return EINVAL instead of ENXIO, so we map it here. 4513 */ 4514 return (e.error == ENXIO ? EINVAL : e.error); 4515 } 4516 4517 /* 4518 * Flush local dirty pages to stable storage on the server. 4519 * 4520 * If FNODSYNC is specified, then there is nothing to do because 4521 * metadata changes are not cached on the client before being 4522 * sent to the server. 4523 */ 4524 static int 4525 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr) 4526 { 4527 int error; 4528 4529 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 4530 return (0); 4531 if (nfs_zone() != VTOMI4(vp)->mi_zone) 4532 return (EIO); 4533 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr); 4534 if (!error) 4535 error = VTOR4(vp)->r_error; 4536 return (error); 4537 } 4538 4539 /* 4540 * Weirdness: if the file was removed or the target of a rename 4541 * operation while it was open, it got renamed instead. Here we 4542 * remove the renamed file. 4543 */ 4544 void 4545 nfs4_inactive(vnode_t *vp, cred_t *cr) 4546 { 4547 rnode4_t *rp; 4548 4549 ASSERT(vp != DNLC_NO_VNODE); 4550 4551 rp = VTOR4(vp); 4552 4553 if (IS_SHADOW(vp, rp)) { 4554 sv_inactive(vp); 4555 return; 4556 } 4557 4558 /* 4559 * If this is coming from the wrong zone, we let someone in the right 4560 * zone take care of it asynchronously. We can get here due to 4561 * VN_RELE() being called from pageout() or fsflush(). This call may 4562 * potentially turn into an expensive no-op if, for instance, v_count 4563 * gets incremented in the meantime, but it's still correct. 4564 */ 4565 if (nfs_zone() != VTOMI4(vp)->mi_zone) { 4566 nfs4_async_inactive(vp, cr); 4567 return; 4568 } 4569 4570 /* 4571 * Some of the cleanup steps might require over-the-wire 4572 * operations. Since VOP_INACTIVE can get called as a result of 4573 * other over-the-wire operations (e.g., an attribute cache update 4574 * can lead to a DNLC purge), doing those steps now would lead to a 4575 * nested call to the recovery framework, which can deadlock. So 4576 * do any over-the-wire cleanups asynchronously, in a separate 4577 * thread. 4578 */ 4579 4580 mutex_enter(&rp->r_os_lock); 4581 mutex_enter(&rp->r_statelock); 4582 mutex_enter(&rp->r_statev4_lock); 4583 4584 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) { 4585 mutex_exit(&rp->r_statev4_lock); 4586 mutex_exit(&rp->r_statelock); 4587 mutex_exit(&rp->r_os_lock); 4588 nfs4_async_inactive(vp, cr); 4589 return; 4590 } 4591 4592 if (rp->r_deleg_type == OPEN_DELEGATE_READ || 4593 rp->r_deleg_type == OPEN_DELEGATE_WRITE) { 4594 mutex_exit(&rp->r_statev4_lock); 4595 mutex_exit(&rp->r_statelock); 4596 mutex_exit(&rp->r_os_lock); 4597 nfs4_async_inactive(vp, cr); 4598 return; 4599 } 4600 4601 if (rp->r_unldvp != NULL) { 4602 mutex_exit(&rp->r_statev4_lock); 4603 mutex_exit(&rp->r_statelock); 4604 mutex_exit(&rp->r_os_lock); 4605 nfs4_async_inactive(vp, cr); 4606 return; 4607 } 4608 mutex_exit(&rp->r_statev4_lock); 4609 mutex_exit(&rp->r_statelock); 4610 mutex_exit(&rp->r_os_lock); 4611 4612 rp4_addfree(rp, cr); 4613 } 4614 4615 /* 4616 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up 4617 * various bits of state. The caller must not refer to vp after this call. 4618 */ 4619 4620 void 4621 nfs4_inactive_otw(vnode_t *vp, cred_t *cr) 4622 { 4623 rnode4_t *rp = VTOR4(vp); 4624 nfs4_recov_state_t recov_state; 4625 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 4626 vnode_t *unldvp; 4627 char *unlname; 4628 cred_t *unlcred; 4629 COMPOUND4args_clnt args; 4630 COMPOUND4res_clnt res, *resp; 4631 nfs_argop4 argop[2]; 4632 int doqueue; 4633 #ifdef DEBUG 4634 char *name; 4635 #endif 4636 4637 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 4638 ASSERT(!IS_SHADOW(vp, rp)); 4639 4640 #ifdef DEBUG 4641 name = fn_name(VTOSV(vp)->sv_name); 4642 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: " 4643 "release vnode %s", name)); 4644 kmem_free(name, MAXNAMELEN); 4645 #endif 4646 4647 if (vp->v_type == VREG) { 4648 bool_t recov_failed = FALSE; 4649 4650 e.error = nfs4close_all(vp, cr); 4651 if (e.error) { 4652 /* Check to see if recovery failed */ 4653 mutex_enter(&(VTOMI4(vp)->mi_lock)); 4654 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL) 4655 recov_failed = TRUE; 4656 mutex_exit(&(VTOMI4(vp)->mi_lock)); 4657 if (!recov_failed) { 4658 mutex_enter(&rp->r_statelock); 4659 if (rp->r_flags & R4RECOVERR) 4660 recov_failed = TRUE; 4661 mutex_exit(&rp->r_statelock); 4662 } 4663 if (recov_failed) { 4664 NFS4_DEBUG(nfs4_client_recov_debug, 4665 (CE_NOTE, "nfs4_inactive_otw: " 4666 "close failed (recovery failure)")); 4667 } 4668 } 4669 } 4670 4671 redo: 4672 if (rp->r_unldvp == NULL) { 4673 rp4_addfree(rp, cr); 4674 return; 4675 } 4676 4677 /* 4678 * Save the vnode pointer for the directory where the 4679 * unlinked-open file got renamed, then set it to NULL 4680 * to prevent another thread from getting here before 4681 * we're done with the remove. While we have the 4682 * statelock, make local copies of the pertinent rnode 4683 * fields. If we weren't to do this in an atomic way, the 4684 * the unl* fields could become inconsistent with respect 4685 * to each other due to a race condition between this 4686 * code and nfs_remove(). See bug report 1034328. 4687 */ 4688 mutex_enter(&rp->r_statelock); 4689 if (rp->r_unldvp == NULL) { 4690 mutex_exit(&rp->r_statelock); 4691 rp4_addfree(rp, cr); 4692 return; 4693 } 4694 4695 unldvp = rp->r_unldvp; 4696 rp->r_unldvp = NULL; 4697 unlname = rp->r_unlname; 4698 rp->r_unlname = NULL; 4699 unlcred = rp->r_unlcred; 4700 rp->r_unlcred = NULL; 4701 mutex_exit(&rp->r_statelock); 4702 4703 /* 4704 * If there are any dirty pages left, then flush 4705 * them. This is unfortunate because they just 4706 * may get thrown away during the remove operation, 4707 * but we have to do this for correctness. 4708 */ 4709 if (nfs4_has_pages(vp) && 4710 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 4711 ASSERT(vp->v_type != VCHR); 4712 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr); 4713 if (e.error) { 4714 mutex_enter(&rp->r_statelock); 4715 if (!rp->r_error) 4716 rp->r_error = e.error; 4717 mutex_exit(&rp->r_statelock); 4718 } 4719 } 4720 4721 recov_state.rs_flags = 0; 4722 recov_state.rs_num_retry_despite_err = 0; 4723 recov_retry_remove: 4724 /* 4725 * Do the remove operation on the renamed file 4726 */ 4727 args.ctag = TAG_INACTIVE; 4728 4729 /* 4730 * Remove ops: putfh dir; remove 4731 */ 4732 args.array_len = 2; 4733 args.array = argop; 4734 4735 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state); 4736 if (e.error) { 4737 kmem_free(unlname, MAXNAMELEN); 4738 crfree(unlcred); 4739 VN_RELE(unldvp); 4740 /* 4741 * Try again; this time around r_unldvp will be NULL, so we'll 4742 * just call rp4_addfree() and return. 4743 */ 4744 goto redo; 4745 } 4746 4747 /* putfh directory */ 4748 argop[0].argop = OP_CPUTFH; 4749 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh; 4750 4751 /* remove */ 4752 argop[1].argop = OP_CREMOVE; 4753 argop[1].nfs_argop4_u.opcremove.ctarget = unlname; 4754 4755 doqueue = 1; 4756 resp = &res; 4757 4758 #if 0 /* notyet */ 4759 /* 4760 * Can't do this yet. We may be being called from 4761 * dnlc_purge_XXX while that routine is holding a 4762 * mutex lock to the nc_rele list. The calls to 4763 * nfs3_cache_wcc_data may result in calls to 4764 * dnlc_purge_XXX. This will result in a deadlock. 4765 */ 4766 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4767 if (e.error) { 4768 PURGE_ATTRCACHE4(unldvp); 4769 resp = NULL; 4770 } else if (res.status) { 4771 e.error = geterrno4(res.status); 4772 PURGE_ATTRCACHE4(unldvp); 4773 /* 4774 * This code is inactive right now 4775 * but if made active there should 4776 * be a nfs4_end_op() call before 4777 * nfs4_purge_stale_fh to avoid start_op() 4778 * deadlock. See BugId: 4948726 4779 */ 4780 nfs4_purge_stale_fh(error, unldvp, cr); 4781 } else { 4782 nfs_resop4 *resop; 4783 REMOVE4res *rm_res; 4784 4785 resop = &res.array[1]; 4786 rm_res = &resop->nfs_resop4_u.opremove; 4787 /* 4788 * Update directory cache attribute, 4789 * readdir and dnlc caches. 4790 */ 4791 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL); 4792 } 4793 #else 4794 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e); 4795 4796 PURGE_ATTRCACHE4(unldvp); 4797 #endif 4798 4799 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) { 4800 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL, 4801 NULL, NULL, OP_REMOVE, NULL) == FALSE) { 4802 if (!e.error) 4803 (void) xdr_free(xdr_COMPOUND4res_clnt, 4804 (caddr_t)&res); 4805 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, 4806 &recov_state, TRUE); 4807 goto recov_retry_remove; 4808 } 4809 } 4810 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE); 4811 4812 /* 4813 * Release stuff held for the remove 4814 */ 4815 VN_RELE(unldvp); 4816 if (!e.error && resp) 4817 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 4818 4819 kmem_free(unlname, MAXNAMELEN); 4820 crfree(unlcred); 4821 goto redo; 4822 } 4823 4824 /* 4825 * Remote file system operations having to do with directory manipulation. 4826 */ 4827 /* ARGSUSED3 */ 4828 int 4829 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 4830 int flags, vnode_t *rdir, cred_t *cr) 4831 { 4832 int error; 4833 vnode_t *vp, *avp = NULL; 4834 rnode4_t *drp; 4835 4836 *vpp = NULL; 4837 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 4838 return (EPERM); 4839 /* 4840 * if LOOKUP_XATTR, must replace dvp (object) with 4841 * object's attrdir before continuing with lookup 4842 */ 4843 if (flags & LOOKUP_XATTR) { 4844 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr); 4845 if (error) 4846 return (error); 4847 4848 dvp = avp; 4849 4850 /* 4851 * If lookup is for "", just return dvp now. The attrdir 4852 * has already been activated (from nfs4lookup_xattr), and 4853 * the caller will RELE the original dvp -- not 4854 * the attrdir. So, set vpp and return. 4855 * Currently, when the LOOKUP_XATTR flag is 4856 * passed to VOP_LOOKUP, the name is always empty, and 4857 * shortcircuiting here avoids 3 unneeded lock/unlock 4858 * pairs. 4859 * 4860 * If a non-empty name was provided, then it is the 4861 * attribute name, and it will be looked up below. 4862 */ 4863 if (*nm == '\0') { 4864 *vpp = dvp; 4865 return (0); 4866 } 4867 4868 /* 4869 * The vfs layer never sends a name when asking for the 4870 * attrdir, so we should never get here (unless of course 4871 * name is passed at some time in future -- at which time 4872 * we'll blow up here). 4873 */ 4874 ASSERT(0); 4875 } 4876 4877 drp = VTOR4(dvp); 4878 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4879 return (EINTR); 4880 4881 error = nfs4lookup(dvp, nm, vpp, cr, 0); 4882 nfs_rw_exit(&drp->r_rwlock); 4883 4884 /* 4885 * If vnode is a device, create special vnode. 4886 */ 4887 if (!error && ISVDEV((*vpp)->v_type)) { 4888 vp = *vpp; 4889 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 4890 VN_RELE(vp); 4891 } 4892 4893 return (error); 4894 } 4895 4896 /* ARGSUSED */ 4897 static int 4898 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr) 4899 { 4900 int error; 4901 rnode4_t *drp; 4902 int cflag = ((flags & CREATE_XATTR_DIR) != 0); 4903 mntinfo4_t *mi; 4904 4905 mi = VTOMI4(dvp); 4906 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR)) 4907 return (EINVAL); 4908 4909 drp = VTOR4(dvp); 4910 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) 4911 return (EINTR); 4912 4913 mutex_enter(&drp->r_statelock); 4914 /* 4915 * If the server doesn't support xattrs just return EINVAL 4916 */ 4917 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) { 4918 mutex_exit(&drp->r_statelock); 4919 nfs_rw_exit(&drp->r_rwlock); 4920 return (EINVAL); 4921 } 4922 4923 /* 4924 * If there is a cached xattr directory entry, 4925 * use it as long as the attributes are valid. If the 4926 * attributes are not valid, take the simple approach and 4927 * free the cached value and re-fetch a new value. 4928 * 4929 * We don't negative entry cache for now, if we did we 4930 * would need to check if the file has changed on every 4931 * lookup. But xattrs don't exist very often and failing 4932 * an openattr is not much more expensive than and NVERIFY or GETATTR 4933 * so do an openattr over the wire for now. 4934 */ 4935 if (drp->r_xattr_dir != NULL) { 4936 if (ATTRCACHE4_VALID(dvp)) { 4937 VN_HOLD(drp->r_xattr_dir); 4938 *vpp = drp->r_xattr_dir; 4939 mutex_exit(&drp->r_statelock); 4940 nfs_rw_exit(&drp->r_rwlock); 4941 return (0); 4942 } 4943 VN_RELE(drp->r_xattr_dir); 4944 drp->r_xattr_dir = NULL; 4945 } 4946 mutex_exit(&drp->r_statelock); 4947 4948 error = nfs4openattr(dvp, vpp, cflag, cr); 4949 4950 nfs_rw_exit(&drp->r_rwlock); 4951 4952 return (error); 4953 } 4954 4955 static int 4956 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc) 4957 { 4958 int error; 4959 rnode4_t *drp; 4960 4961 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 4962 4963 /* 4964 * If lookup is for "", just return dvp. Don't need 4965 * to send it over the wire, look it up in the dnlc, 4966 * or perform any access checks. 4967 */ 4968 if (*nm == '\0') { 4969 VN_HOLD(dvp); 4970 *vpp = dvp; 4971 return (0); 4972 } 4973 4974 /* 4975 * Can't do lookups in non-directories. 4976 */ 4977 if (dvp->v_type != VDIR) 4978 return (ENOTDIR); 4979 4980 /* 4981 * If lookup is for ".", just return dvp. Don't need 4982 * to send it over the wire or look it up in the dnlc, 4983 * just need to check access. 4984 */ 4985 if (nm[0] == '.' && nm[1] == '\0') { 4986 error = nfs4_access(dvp, VEXEC, 0, cr); 4987 if (error) 4988 return (error); 4989 VN_HOLD(dvp); 4990 *vpp = dvp; 4991 return (0); 4992 } 4993 4994 drp = VTOR4(dvp); 4995 if (!(drp->r_flags & R4LOOKUP)) { 4996 mutex_enter(&drp->r_statelock); 4997 drp->r_flags |= R4LOOKUP; 4998 mutex_exit(&drp->r_statelock); 4999 } 5000 5001 *vpp = NULL; 5002 /* 5003 * Lookup this name in the DNLC. If there is no entry 5004 * lookup over the wire. 5005 */ 5006 if (!skipdnlc) 5007 *vpp = dnlc_lookup(dvp, nm); 5008 if (*vpp == NULL) { 5009 /* 5010 * We need to go over the wire to lookup the name. 5011 */ 5012 return (nfs4lookupnew_otw(dvp, nm, vpp, cr)); 5013 } 5014 5015 /* 5016 * We hit on the dnlc 5017 */ 5018 if (*vpp != DNLC_NO_VNODE || 5019 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) { 5020 /* 5021 * But our attrs may not be valid. 5022 */ 5023 if (ATTRCACHE4_VALID(dvp)) { 5024 error = nfs4_waitfor_purge_complete(dvp); 5025 if (error) { 5026 VN_RELE(*vpp); 5027 *vpp = NULL; 5028 return (error); 5029 } 5030 5031 /* 5032 * If after the purge completes, check to make sure 5033 * our attrs are still valid. 5034 */ 5035 if (ATTRCACHE4_VALID(dvp)) { 5036 /* 5037 * If we waited for a purge we may have 5038 * lost our vnode so look it up again. 5039 */ 5040 VN_RELE(*vpp); 5041 *vpp = dnlc_lookup(dvp, nm); 5042 if (*vpp == NULL) 5043 return (nfs4lookupnew_otw(dvp, 5044 nm, vpp, cr)); 5045 5046 /* 5047 * The access cache should almost always hit 5048 */ 5049 error = nfs4_access(dvp, VEXEC, 0, cr); 5050 5051 if (error) { 5052 VN_RELE(*vpp); 5053 *vpp = NULL; 5054 return (error); 5055 } 5056 if (*vpp == DNLC_NO_VNODE) { 5057 VN_RELE(*vpp); 5058 *vpp = NULL; 5059 return (ENOENT); 5060 } 5061 return (0); 5062 } 5063 } 5064 } 5065 5066 ASSERT(*vpp != NULL); 5067 5068 /* 5069 * We may have gotten here we have one of the following cases: 5070 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we 5071 * need to validate them. 5072 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always 5073 * must validate. 5074 * 5075 * Go to the server and check if the directory has changed, if 5076 * it hasn't we are done and can use the dnlc entry. 5077 */ 5078 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr)); 5079 } 5080 5081 /* 5082 * Go to the server and check if the directory has changed, if 5083 * it hasn't we are done and can use the dnlc entry. If it 5084 * has changed we get a new copy of its attributes and check 5085 * the access for VEXEC, then relookup the filename and 5086 * get its filehandle and attributes. 5087 * 5088 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR 5089 * if the NVERIFY failed we must 5090 * purge the caches 5091 * cache new attributes (will set r_time_attr_inval) 5092 * cache new access 5093 * recheck VEXEC access 5094 * add name to dnlc, possibly negative 5095 * if LOOKUP succeeded 5096 * cache new attributes 5097 * else 5098 * set a new r_time_attr_inval for dvp 5099 * check to make sure we have access 5100 * 5101 * The vpp returned is the vnode passed in if the directory is valid, 5102 * a new vnode if successful lookup, or NULL on error. 5103 */ 5104 static int 5105 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5106 { 5107 COMPOUND4args_clnt args; 5108 COMPOUND4res_clnt res; 5109 fattr4 *ver_fattr; 5110 fattr4_change dchange; 5111 int32_t *ptr; 5112 int argoplist_size = 7 * sizeof (nfs_argop4); 5113 nfs_argop4 *argop; 5114 int doqueue; 5115 mntinfo4_t *mi; 5116 nfs4_recov_state_t recov_state; 5117 hrtime_t t; 5118 int isdotdot; 5119 vnode_t *nvp; 5120 nfs_fh4 *fhp; 5121 nfs4_sharedfh_t *sfhp; 5122 nfs4_access_type_t cacc; 5123 rnode4_t *nrp; 5124 rnode4_t *drp = VTOR4(dvp); 5125 nfs4_ga_res_t *garp = NULL; 5126 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5127 5128 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5129 ASSERT(nm != NULL); 5130 ASSERT(nm[0] != '\0'); 5131 ASSERT(dvp->v_type == VDIR); 5132 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5133 ASSERT(*vpp != NULL); 5134 5135 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5136 isdotdot = 1; 5137 args.ctag = TAG_LOOKUP_VPARENT; 5138 } else { 5139 /* 5140 * If dvp were a stub, it should have triggered and caused 5141 * a mount for us to get this far. 5142 */ 5143 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5144 5145 isdotdot = 0; 5146 args.ctag = TAG_LOOKUP_VALID; 5147 } 5148 5149 mi = VTOMI4(dvp); 5150 recov_state.rs_flags = 0; 5151 recov_state.rs_num_retry_despite_err = 0; 5152 5153 nvp = NULL; 5154 5155 /* Save the original mount point security information */ 5156 (void) save_mnt_secinfo(mi->mi_curr_serv); 5157 5158 recov_retry: 5159 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5160 &recov_state, NULL); 5161 if (e.error) { 5162 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5163 VN_RELE(*vpp); 5164 *vpp = NULL; 5165 return (e.error); 5166 } 5167 5168 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5169 5170 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */ 5171 args.array_len = 7; 5172 args.array = argop; 5173 5174 /* 0. putfh file */ 5175 argop[0].argop = OP_CPUTFH; 5176 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5177 5178 /* 1. nverify the change info */ 5179 argop[1].argop = OP_NVERIFY; 5180 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes; 5181 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5182 ver_fattr->attrlist4 = (char *)&dchange; 5183 ptr = (int32_t *)&dchange; 5184 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5185 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5186 5187 /* 2. getattr directory */ 5188 argop[2].argop = OP_GETATTR; 5189 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5190 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5191 5192 /* 3. access directory */ 5193 argop[3].argop = OP_ACCESS; 5194 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5195 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5196 5197 /* 4. lookup name */ 5198 if (isdotdot) { 5199 argop[4].argop = OP_LOOKUPP; 5200 } else { 5201 argop[4].argop = OP_CLOOKUP; 5202 argop[4].nfs_argop4_u.opclookup.cname = nm; 5203 } 5204 5205 /* 5. resulting file handle */ 5206 argop[5].argop = OP_GETFH; 5207 5208 /* 6. resulting file attributes */ 5209 argop[6].argop = OP_GETATTR; 5210 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5211 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5212 5213 doqueue = 1; 5214 t = gethrtime(); 5215 5216 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5217 5218 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5219 /* 5220 * For WRONGSEC of a non-dotdot case, send secinfo directly 5221 * from this thread, do not go thru the recovery thread since 5222 * we need the nm information. 5223 * 5224 * Not doing dotdot case because there is no specification 5225 * for (PUTFH, SECINFO "..") yet. 5226 */ 5227 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5228 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5229 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5230 &recov_state, FALSE); 5231 else 5232 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5233 &recov_state, TRUE); 5234 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5235 kmem_free(argop, argoplist_size); 5236 if (!e.error) 5237 goto recov_retry; 5238 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5239 VN_RELE(*vpp); 5240 *vpp = NULL; 5241 return (e.error); 5242 } 5243 5244 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5245 OP_LOOKUP, NULL) == FALSE) { 5246 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5247 &recov_state, TRUE); 5248 5249 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5250 kmem_free(argop, argoplist_size); 5251 goto recov_retry; 5252 } 5253 } 5254 5255 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5256 5257 if (e.error || res.array_len == 0) { 5258 /* 5259 * If e.error isn't set, then reply has no ops (or we couldn't 5260 * be here). The only legal way to reply without an op array 5261 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5262 * be in the reply for all other status values. 5263 * 5264 * For valid replies without an ops array, return ENOTSUP 5265 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5266 * return EIO -- don't trust status. 5267 */ 5268 if (e.error == 0) 5269 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5270 ENOTSUP : EIO; 5271 VN_RELE(*vpp); 5272 *vpp = NULL; 5273 kmem_free(argop, argoplist_size); 5274 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5275 return (e.error); 5276 } 5277 5278 if (res.status != NFS4ERR_SAME) { 5279 e.error = geterrno4(res.status); 5280 5281 /* 5282 * The NVERIFY "failed" so the directory has changed 5283 * First make sure PUTFH succeeded and NVERIFY "failed" 5284 * cleanly. 5285 */ 5286 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5287 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) { 5288 nfs4_purge_stale_fh(e.error, dvp, cr); 5289 VN_RELE(*vpp); 5290 *vpp = NULL; 5291 goto exit; 5292 } 5293 5294 /* 5295 * We know the NVERIFY "failed" so we must: 5296 * purge the caches (access and indirectly dnlc if needed) 5297 */ 5298 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5299 5300 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5301 nfs4_purge_stale_fh(e.error, dvp, cr); 5302 VN_RELE(*vpp); 5303 *vpp = NULL; 5304 goto exit; 5305 } 5306 5307 /* 5308 * Install new cached attributes for the directory 5309 */ 5310 nfs4_attr_cache(dvp, 5311 &res.array[2].nfs_resop4_u.opgetattr.ga_res, 5312 t, cr, FALSE, NULL); 5313 5314 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) { 5315 nfs4_purge_stale_fh(e.error, dvp, cr); 5316 VN_RELE(*vpp); 5317 *vpp = NULL; 5318 e.error = geterrno4(res.status); 5319 goto exit; 5320 } 5321 5322 /* 5323 * Now we know the directory is valid, 5324 * cache new directory access 5325 */ 5326 nfs4_access_cache(drp, 5327 args.array[3].nfs_argop4_u.opaccess.access, 5328 res.array[3].nfs_resop4_u.opaccess.access, cr); 5329 5330 /* 5331 * recheck VEXEC access 5332 */ 5333 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5334 if (cacc != NFS4_ACCESS_ALLOWED) { 5335 /* 5336 * Directory permissions might have been revoked 5337 */ 5338 if (cacc == NFS4_ACCESS_DENIED) { 5339 e.error = EACCES; 5340 VN_RELE(*vpp); 5341 *vpp = NULL; 5342 goto exit; 5343 } 5344 5345 /* 5346 * Somehow we must not have asked for enough 5347 * so try a singleton ACCESS, should never happen. 5348 */ 5349 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5350 if (e.error) { 5351 VN_RELE(*vpp); 5352 *vpp = NULL; 5353 goto exit; 5354 } 5355 } 5356 5357 e.error = geterrno4(res.status); 5358 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) { 5359 /* 5360 * The lookup failed, probably no entry 5361 */ 5362 if (e.error == ENOENT && nfs4_lookup_neg_cache) { 5363 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5364 } else { 5365 /* 5366 * Might be some other error, so remove 5367 * the dnlc entry to make sure we start all 5368 * over again, next time. 5369 */ 5370 dnlc_remove(dvp, nm); 5371 } 5372 VN_RELE(*vpp); 5373 *vpp = NULL; 5374 goto exit; 5375 } 5376 5377 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5378 /* 5379 * The file exists but we can't get its fh for 5380 * some unknown reason. Remove it from the dnlc 5381 * and error out to be safe. 5382 */ 5383 dnlc_remove(dvp, nm); 5384 VN_RELE(*vpp); 5385 *vpp = NULL; 5386 goto exit; 5387 } 5388 fhp = &res.array[5].nfs_resop4_u.opgetfh.object; 5389 if (fhp->nfs_fh4_len == 0) { 5390 /* 5391 * The file exists but a bogus fh 5392 * some unknown reason. Remove it from the dnlc 5393 * and error out to be safe. 5394 */ 5395 e.error = ENOENT; 5396 dnlc_remove(dvp, nm); 5397 VN_RELE(*vpp); 5398 *vpp = NULL; 5399 goto exit; 5400 } 5401 sfhp = sfh4_get(fhp, mi); 5402 5403 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK) 5404 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 5405 5406 /* 5407 * Make the new rnode 5408 */ 5409 if (isdotdot) { 5410 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5411 if (e.error) { 5412 sfh4_rele(&sfhp); 5413 VN_RELE(*vpp); 5414 *vpp = NULL; 5415 goto exit; 5416 } 5417 /* 5418 * XXX if nfs4_make_dotdot uses an existing rnode 5419 * XXX it doesn't update the attributes. 5420 * XXX for now just save them again to save an OTW 5421 */ 5422 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5423 } else { 5424 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5425 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5426 /* 5427 * If v_type == VNON, then garp was NULL because 5428 * the last op in the compound failed and makenfs4node 5429 * could not find the vnode for sfhp. It created 5430 * a new vnode, so we have nothing to purge here. 5431 */ 5432 if (nvp->v_type == VNON) { 5433 vattr_t vattr; 5434 5435 vattr.va_mask = AT_TYPE; 5436 /* 5437 * N.B. We've already called nfs4_end_fop above. 5438 */ 5439 e.error = nfs4getattr(nvp, &vattr, cr); 5440 if (e.error) { 5441 sfh4_rele(&sfhp); 5442 VN_RELE(*vpp); 5443 *vpp = NULL; 5444 VN_RELE(nvp); 5445 goto exit; 5446 } 5447 nvp->v_type = vattr.va_type; 5448 } 5449 } 5450 sfh4_rele(&sfhp); 5451 5452 nrp = VTOR4(nvp); 5453 mutex_enter(&nrp->r_statev4_lock); 5454 if (!nrp->created_v4) { 5455 mutex_exit(&nrp->r_statev4_lock); 5456 dnlc_update(dvp, nm, nvp); 5457 } else 5458 mutex_exit(&nrp->r_statev4_lock); 5459 5460 VN_RELE(*vpp); 5461 *vpp = nvp; 5462 } else { 5463 hrtime_t now; 5464 hrtime_t delta = 0; 5465 5466 e.error = 0; 5467 5468 /* 5469 * Because the NVERIFY "succeeded" we know that the 5470 * directory attributes are still valid 5471 * so update r_time_attr_inval 5472 */ 5473 now = gethrtime(); 5474 mutex_enter(&drp->r_statelock); 5475 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5476 delta = now - drp->r_time_attr_saved; 5477 if (delta < mi->mi_acdirmin) 5478 delta = mi->mi_acdirmin; 5479 else if (delta > mi->mi_acdirmax) 5480 delta = mi->mi_acdirmax; 5481 } 5482 drp->r_time_attr_inval = now + delta; 5483 mutex_exit(&drp->r_statelock); 5484 dnlc_update(dvp, nm, *vpp); 5485 5486 /* 5487 * Even though we have a valid directory attr cache 5488 * and dnlc entry, we may not have access. 5489 * This should almost always hit the cache. 5490 */ 5491 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5492 if (e.error) { 5493 VN_RELE(*vpp); 5494 *vpp = NULL; 5495 } 5496 5497 if (*vpp == DNLC_NO_VNODE) { 5498 VN_RELE(*vpp); 5499 *vpp = NULL; 5500 e.error = ENOENT; 5501 } 5502 } 5503 5504 exit: 5505 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5506 kmem_free(argop, argoplist_size); 5507 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5508 return (e.error); 5509 } 5510 5511 /* 5512 * We need to go over the wire to lookup the name, but 5513 * while we are there verify the directory has not 5514 * changed but if it has, get new attributes and check access 5515 * 5516 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH 5517 * NVERIFY GETATTR ACCESS 5518 * 5519 * With the results: 5520 * if the NVERIFY failed we must purge the caches, add new attributes, 5521 * and cache new access. 5522 * set a new r_time_attr_inval 5523 * add name to dnlc, possibly negative 5524 * if LOOKUP succeeded 5525 * cache new attributes 5526 */ 5527 static int 5528 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 5529 { 5530 COMPOUND4args_clnt args; 5531 COMPOUND4res_clnt res; 5532 fattr4 *ver_fattr; 5533 fattr4_change dchange; 5534 int32_t *ptr; 5535 nfs4_ga_res_t *garp = NULL; 5536 int argoplist_size = 9 * sizeof (nfs_argop4); 5537 nfs_argop4 *argop; 5538 int doqueue; 5539 mntinfo4_t *mi; 5540 nfs4_recov_state_t recov_state; 5541 hrtime_t t; 5542 int isdotdot; 5543 vnode_t *nvp; 5544 nfs_fh4 *fhp; 5545 nfs4_sharedfh_t *sfhp; 5546 nfs4_access_type_t cacc; 5547 rnode4_t *nrp; 5548 rnode4_t *drp = VTOR4(dvp); 5549 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 5550 5551 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 5552 ASSERT(nm != NULL); 5553 ASSERT(nm[0] != '\0'); 5554 ASSERT(dvp->v_type == VDIR); 5555 ASSERT(nm[0] != '.' || nm[1] != '\0'); 5556 ASSERT(*vpp == NULL); 5557 5558 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') { 5559 isdotdot = 1; 5560 args.ctag = TAG_LOOKUP_PARENT; 5561 } else { 5562 /* 5563 * If dvp were a stub, it should have triggered and caused 5564 * a mount for us to get this far. 5565 */ 5566 ASSERT(!RP_ISSTUB(VTOR4(dvp))); 5567 5568 isdotdot = 0; 5569 args.ctag = TAG_LOOKUP; 5570 } 5571 5572 mi = VTOMI4(dvp); 5573 recov_state.rs_flags = 0; 5574 recov_state.rs_num_retry_despite_err = 0; 5575 5576 nvp = NULL; 5577 5578 /* Save the original mount point security information */ 5579 (void) save_mnt_secinfo(mi->mi_curr_serv); 5580 5581 recov_retry: 5582 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP, 5583 &recov_state, NULL); 5584 if (e.error) { 5585 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5586 return (e.error); 5587 } 5588 5589 argop = kmem_alloc(argoplist_size, KM_SLEEP); 5590 5591 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */ 5592 args.array_len = 9; 5593 args.array = argop; 5594 5595 /* 0. putfh file */ 5596 argop[0].argop = OP_CPUTFH; 5597 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh; 5598 5599 /* 1. savefh for the nverify */ 5600 argop[1].argop = OP_SAVEFH; 5601 5602 /* 2. lookup name */ 5603 if (isdotdot) { 5604 argop[2].argop = OP_LOOKUPP; 5605 } else { 5606 argop[2].argop = OP_CLOOKUP; 5607 argop[2].nfs_argop4_u.opclookup.cname = nm; 5608 } 5609 5610 /* 3. resulting file handle */ 5611 argop[3].argop = OP_GETFH; 5612 5613 /* 4. resulting file attributes */ 5614 argop[4].argop = OP_GETATTR; 5615 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5616 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5617 5618 /* 5. restorefh back the directory for the nverify */ 5619 argop[5].argop = OP_RESTOREFH; 5620 5621 /* 6. nverify the change info */ 5622 argop[6].argop = OP_NVERIFY; 5623 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes; 5624 ver_fattr->attrmask = FATTR4_CHANGE_MASK; 5625 ver_fattr->attrlist4 = (char *)&dchange; 5626 ptr = (int32_t *)&dchange; 5627 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change); 5628 ver_fattr->attrlist4_len = sizeof (fattr4_change); 5629 5630 /* 7. getattr directory */ 5631 argop[7].argop = OP_GETATTR; 5632 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 5633 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 5634 5635 /* 8. access directory */ 5636 argop[8].argop = OP_ACCESS; 5637 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE | 5638 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP; 5639 5640 doqueue = 1; 5641 t = gethrtime(); 5642 5643 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 5644 5645 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) { 5646 /* 5647 * For WRONGSEC of a non-dotdot case, send secinfo directly 5648 * from this thread, do not go thru the recovery thread since 5649 * we need the nm information. 5650 * 5651 * Not doing dotdot case because there is no specification 5652 * for (PUTFH, SECINFO "..") yet. 5653 */ 5654 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) { 5655 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr))) 5656 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5657 &recov_state, FALSE); 5658 else 5659 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5660 &recov_state, TRUE); 5661 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5662 kmem_free(argop, argoplist_size); 5663 if (!e.error) 5664 goto recov_retry; 5665 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5666 return (e.error); 5667 } 5668 5669 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 5670 OP_LOOKUP, NULL) == FALSE) { 5671 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, 5672 &recov_state, TRUE); 5673 5674 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5675 kmem_free(argop, argoplist_size); 5676 goto recov_retry; 5677 } 5678 } 5679 5680 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE); 5681 5682 if (e.error || res.array_len == 0) { 5683 /* 5684 * If e.error isn't set, then reply has no ops (or we couldn't 5685 * be here). The only legal way to reply without an op array 5686 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should 5687 * be in the reply for all other status values. 5688 * 5689 * For valid replies without an ops array, return ENOTSUP 5690 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies, 5691 * return EIO -- don't trust status. 5692 */ 5693 if (e.error == 0) 5694 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ? 5695 ENOTSUP : EIO; 5696 5697 kmem_free(argop, argoplist_size); 5698 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5699 return (e.error); 5700 } 5701 5702 e.error = geterrno4(res.status); 5703 5704 /* 5705 * The PUTFH and SAVEFH may have failed. 5706 */ 5707 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) || 5708 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) { 5709 nfs4_purge_stale_fh(e.error, dvp, cr); 5710 goto exit; 5711 } 5712 5713 /* 5714 * Check if the file exists, if it does delay entering 5715 * into the dnlc until after we update the directory 5716 * attributes so we don't cause it to get purged immediately. 5717 */ 5718 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) { 5719 /* 5720 * The lookup failed, probably no entry 5721 */ 5722 if (e.error == ENOENT && nfs4_lookup_neg_cache) 5723 dnlc_update(dvp, nm, DNLC_NO_VNODE); 5724 goto exit; 5725 } 5726 5727 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) { 5728 /* 5729 * The file exists but we can't get its fh for 5730 * some unknown reason. Error out to be safe. 5731 */ 5732 goto exit; 5733 } 5734 5735 fhp = &res.array[3].nfs_resop4_u.opgetfh.object; 5736 if (fhp->nfs_fh4_len == 0) { 5737 /* 5738 * The file exists but a bogus fh 5739 * some unknown reason. Error out to be safe. 5740 */ 5741 e.error = EIO; 5742 goto exit; 5743 } 5744 sfhp = sfh4_get(fhp, mi); 5745 5746 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5747 sfh4_rele(&sfhp); 5748 e.error = EIO; 5749 goto exit; 5750 } 5751 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 5752 5753 /* 5754 * The RESTOREFH may have failed 5755 */ 5756 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) { 5757 sfh4_rele(&sfhp); 5758 e.error = EIO; 5759 goto exit; 5760 } 5761 5762 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) { 5763 /* 5764 * First make sure the NVERIFY failed as we expected, 5765 * if it didn't then be conservative and error out 5766 * as we can't trust the directory. 5767 */ 5768 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) { 5769 sfh4_rele(&sfhp); 5770 e.error = EIO; 5771 goto exit; 5772 } 5773 5774 /* 5775 * We know the NVERIFY "failed" so the directory has changed, 5776 * so we must: 5777 * purge the caches (access and indirectly dnlc if needed) 5778 */ 5779 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE); 5780 5781 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) { 5782 sfh4_rele(&sfhp); 5783 goto exit; 5784 } 5785 nfs4_attr_cache(dvp, 5786 &res.array[7].nfs_resop4_u.opgetattr.ga_res, 5787 t, cr, FALSE, NULL); 5788 5789 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) { 5790 nfs4_purge_stale_fh(e.error, dvp, cr); 5791 sfh4_rele(&sfhp); 5792 e.error = geterrno4(res.status); 5793 goto exit; 5794 } 5795 5796 /* 5797 * Now we know the directory is valid, 5798 * cache new directory access 5799 */ 5800 nfs4_access_cache(drp, 5801 args.array[8].nfs_argop4_u.opaccess.access, 5802 res.array[8].nfs_resop4_u.opaccess.access, cr); 5803 5804 /* 5805 * recheck VEXEC access 5806 */ 5807 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr); 5808 if (cacc != NFS4_ACCESS_ALLOWED) { 5809 /* 5810 * Directory permissions might have been revoked 5811 */ 5812 if (cacc == NFS4_ACCESS_DENIED) { 5813 sfh4_rele(&sfhp); 5814 e.error = EACCES; 5815 goto exit; 5816 } 5817 5818 /* 5819 * Somehow we must not have asked for enough 5820 * so try a singleton ACCESS should never happen 5821 */ 5822 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5823 if (e.error) { 5824 sfh4_rele(&sfhp); 5825 goto exit; 5826 } 5827 } 5828 5829 e.error = geterrno4(res.status); 5830 } else { 5831 hrtime_t now; 5832 hrtime_t delta = 0; 5833 5834 e.error = 0; 5835 5836 /* 5837 * Because the NVERIFY "succeeded" we know that the 5838 * directory attributes are still valid 5839 * so update r_time_attr_inval 5840 */ 5841 now = gethrtime(); 5842 mutex_enter(&drp->r_statelock); 5843 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) { 5844 delta = now - drp->r_time_attr_saved; 5845 if (delta < mi->mi_acdirmin) 5846 delta = mi->mi_acdirmin; 5847 else if (delta > mi->mi_acdirmax) 5848 delta = mi->mi_acdirmax; 5849 } 5850 drp->r_time_attr_inval = now + delta; 5851 mutex_exit(&drp->r_statelock); 5852 5853 /* 5854 * Even though we have a valid directory attr cache, 5855 * we may not have access. 5856 * This should almost always hit the cache. 5857 */ 5858 e.error = nfs4_access(dvp, VEXEC, 0, cr); 5859 if (e.error) { 5860 sfh4_rele(&sfhp); 5861 goto exit; 5862 } 5863 } 5864 5865 /* 5866 * Now we have successfully completed the lookup, if the 5867 * directory has changed we now have the valid attributes. 5868 * We also know we have directory access. 5869 * Create the new rnode and insert it in the dnlc. 5870 */ 5871 if (isdotdot) { 5872 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1); 5873 if (e.error) { 5874 sfh4_rele(&sfhp); 5875 goto exit; 5876 } 5877 /* 5878 * XXX if nfs4_make_dotdot uses an existing rnode 5879 * XXX it doesn't update the attributes. 5880 * XXX for now just save them again to save an OTW 5881 */ 5882 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL); 5883 } else { 5884 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr, 5885 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 5886 } 5887 sfh4_rele(&sfhp); 5888 5889 nrp = VTOR4(nvp); 5890 mutex_enter(&nrp->r_statev4_lock); 5891 if (!nrp->created_v4) { 5892 mutex_exit(&nrp->r_statev4_lock); 5893 dnlc_update(dvp, nm, nvp); 5894 } else 5895 mutex_exit(&nrp->r_statev4_lock); 5896 5897 *vpp = nvp; 5898 5899 exit: 5900 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 5901 kmem_free(argop, argoplist_size); 5902 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp); 5903 return (e.error); 5904 } 5905 5906 #ifdef DEBUG 5907 void 5908 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt) 5909 { 5910 uint_t i, len; 5911 zoneid_t zoneid = getzoneid(); 5912 char *s; 5913 5914 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where); 5915 for (i = 0; i < argcnt; i++) { 5916 nfs_argop4 *op = &argbase[i]; 5917 switch (op->argop) { 5918 case OP_CPUTFH: 5919 case OP_PUTFH: 5920 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i); 5921 break; 5922 case OP_PUTROOTFH: 5923 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i); 5924 break; 5925 case OP_CLOOKUP: 5926 s = op->nfs_argop4_u.opclookup.cname; 5927 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5928 break; 5929 case OP_LOOKUP: 5930 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname, 5931 &len, NULL); 5932 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s); 5933 kmem_free(s, len); 5934 break; 5935 case OP_LOOKUPP: 5936 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i); 5937 break; 5938 case OP_GETFH: 5939 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i); 5940 break; 5941 case OP_GETATTR: 5942 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i); 5943 break; 5944 case OP_OPENATTR: 5945 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i); 5946 break; 5947 default: 5948 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i, 5949 op->argop); 5950 break; 5951 } 5952 } 5953 } 5954 #endif 5955 5956 /* 5957 * nfs4lookup_setup - constructs a multi-lookup compound request. 5958 * 5959 * Given the path "nm1/nm2/.../nmn", the following compound requests 5960 * may be created: 5961 * 5962 * Note: Getfh is not be needed because filehandle attr is mandatory, but it 5963 * is faster, for now. 5964 * 5965 * l4_getattrs indicates the type of compound requested. 5966 * 5967 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo): 5968 * 5969 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} } 5970 * 5971 * total number of ops is n + 1. 5972 * 5973 * LKP4_LAST_NAMED_ATTR - multi-component path for a named 5974 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR 5975 * before the last component, and only get attributes 5976 * for the last component. Note that the second-to-last 5977 * pathname component is XATTR_RPATH, which does NOT go 5978 * over-the-wire as a lookup. 5979 * 5980 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2}; 5981 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr } 5982 * 5983 * and total number of ops is n + 5. 5984 * 5985 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named 5986 * attribute directory: create lookups plus an OPENATTR 5987 * replacing the last lookup. Note that the last pathname 5988 * component is XATTR_RPATH, which does NOT go over-the-wire 5989 * as a lookup. 5990 * 5991 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr; 5992 * Openattr; Getfh; Getattr } 5993 * 5994 * and total number of ops is n + 5. 5995 * 5996 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate 5997 * nodes too. 5998 * 5999 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr; 6000 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr } 6001 * 6002 * and total number of ops is 3*n + 1. 6003 * 6004 * All cases: returns the index in the arg array of the final LOOKUP op, or 6005 * -1 if no LOOKUPs were used. 6006 */ 6007 int 6008 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh) 6009 { 6010 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs; 6011 nfs_argop4 *argbase, *argop; 6012 int arglen, argcnt; 6013 int n = 1; /* number of components */ 6014 int nga = 1; /* number of Getattr's in request */ 6015 char c = '\0', *s, *p; 6016 int lookup_idx = -1; 6017 int argoplist_size; 6018 6019 /* set lookuparg response result to 0 */ 6020 lookupargp->resp->status = NFS4_OK; 6021 6022 /* skip leading "/" or "." e.g. ".//./" if there is */ 6023 for (; ; nm++) { 6024 if (*nm != '/' && *nm != '.') 6025 break; 6026 6027 /* ".." is counted as 1 component */ 6028 if (*nm == '.' && *(nm + 1) == '.') 6029 break; 6030 } 6031 6032 /* 6033 * Find n = number of components - nm must be null terminated 6034 * Skip "." components. 6035 */ 6036 if (*nm != '\0') 6037 for (n = 1, s = nm; *s != '\0'; s++) { 6038 if ((*s == '/') && (*(s + 1) != '/') && 6039 (*(s + 1) != '\0') && 6040 !(*(s + 1) == '.' && (*(s + 2) == '/' || 6041 *(s + 2) == '\0'))) 6042 n++; 6043 } 6044 else 6045 n = 0; 6046 6047 /* 6048 * nga is number of components that need Getfh+Getattr 6049 */ 6050 switch (l4_getattrs) { 6051 case LKP4_NO_ATTRIBUTES: 6052 nga = 0; 6053 break; 6054 case LKP4_ALL_ATTRIBUTES: 6055 nga = n; 6056 /* 6057 * Always have at least 1 getfh, getattr pair 6058 */ 6059 if (nga == 0) 6060 nga++; 6061 break; 6062 case LKP4_LAST_ATTRDIR: 6063 case LKP4_LAST_NAMED_ATTR: 6064 nga = n+1; 6065 break; 6066 } 6067 6068 /* 6069 * If change to use the filehandle attr instead of getfh 6070 * the following line can be deleted. 6071 */ 6072 nga *= 2; 6073 6074 /* 6075 * calculate number of ops in request as 6076 * header + trailer + lookups + getattrs 6077 */ 6078 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga; 6079 6080 argoplist_size = arglen * sizeof (nfs_argop4); 6081 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP); 6082 lookupargp->argsp->array = argop; 6083 6084 argcnt = lookupargp->header_len; 6085 argop += argcnt; 6086 6087 /* 6088 * loop and create a lookup op and possibly getattr/getfh for 6089 * each component. Skip "." components. 6090 */ 6091 for (s = nm; *s != '\0'; s = p) { 6092 /* 6093 * Set up a pathname struct for each component if needed 6094 */ 6095 while (*s == '/') 6096 s++; 6097 if (*s == '\0') 6098 break; 6099 6100 for (p = s; (*p != '/') && (*p != '\0'); p++) 6101 ; 6102 c = *p; 6103 *p = '\0'; 6104 6105 if (s[0] == '.' && s[1] == '\0') { 6106 *p = c; 6107 continue; 6108 } 6109 if (l4_getattrs == LKP4_LAST_ATTRDIR && 6110 strcmp(s, XATTR_RPATH) == 0) { 6111 /* getfh XXX may not be needed in future */ 6112 argop->argop = OP_GETFH; 6113 argop++; 6114 argcnt++; 6115 6116 /* getattr */ 6117 argop->argop = OP_GETATTR; 6118 argop->nfs_argop4_u.opgetattr.attr_request = 6119 lookupargp->ga_bits; 6120 argop->nfs_argop4_u.opgetattr.mi = 6121 lookupargp->mi; 6122 argop++; 6123 argcnt++; 6124 6125 /* openattr */ 6126 argop->argop = OP_OPENATTR; 6127 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR && 6128 strcmp(s, XATTR_RPATH) == 0) { 6129 /* openattr */ 6130 argop->argop = OP_OPENATTR; 6131 argop++; 6132 argcnt++; 6133 6134 /* getfh XXX may not be needed in future */ 6135 argop->argop = OP_GETFH; 6136 argop++; 6137 argcnt++; 6138 6139 /* getattr */ 6140 argop->argop = OP_GETATTR; 6141 argop->nfs_argop4_u.opgetattr.attr_request = 6142 lookupargp->ga_bits; 6143 argop->nfs_argop4_u.opgetattr.mi = 6144 lookupargp->mi; 6145 argop++; 6146 argcnt++; 6147 *p = c; 6148 continue; 6149 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') { 6150 /* lookupp */ 6151 argop->argop = OP_LOOKUPP; 6152 } else { 6153 /* lookup */ 6154 argop->argop = OP_LOOKUP; 6155 (void) str_to_utf8(s, 6156 &argop->nfs_argop4_u.oplookup.objname); 6157 } 6158 lookup_idx = argcnt; 6159 argop++; 6160 argcnt++; 6161 6162 *p = c; 6163 6164 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) { 6165 /* getfh XXX may not be needed in future */ 6166 argop->argop = OP_GETFH; 6167 argop++; 6168 argcnt++; 6169 6170 /* getattr */ 6171 argop->argop = OP_GETATTR; 6172 argop->nfs_argop4_u.opgetattr.attr_request = 6173 lookupargp->ga_bits; 6174 argop->nfs_argop4_u.opgetattr.mi = 6175 lookupargp->mi; 6176 argop++; 6177 argcnt++; 6178 } 6179 } 6180 6181 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) && 6182 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) { 6183 if (needgetfh) { 6184 /* stick in a post-lookup getfh */ 6185 argop->argop = OP_GETFH; 6186 argcnt++; 6187 argop++; 6188 } 6189 /* post-lookup getattr */ 6190 argop->argop = OP_GETATTR; 6191 argop->nfs_argop4_u.opgetattr.attr_request = 6192 lookupargp->ga_bits; 6193 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi; 6194 argcnt++; 6195 } 6196 argcnt += lookupargp->trailer_len; /* actual op count */ 6197 lookupargp->argsp->array_len = argcnt; 6198 lookupargp->arglen = arglen; 6199 6200 #ifdef DEBUG 6201 if (nfs4_client_lookup_debug) 6202 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt); 6203 #endif 6204 6205 return (lookup_idx); 6206 } 6207 6208 static int 6209 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr) 6210 { 6211 COMPOUND4args_clnt args; 6212 COMPOUND4res_clnt res; 6213 GETFH4res *gf_res = NULL; 6214 nfs_argop4 argop[4]; 6215 nfs_resop4 *resop = NULL; 6216 nfs4_sharedfh_t *sfhp; 6217 hrtime_t t; 6218 nfs4_error_t e; 6219 6220 rnode4_t *drp; 6221 int doqueue = 1; 6222 vnode_t *vp; 6223 int needrecov = 0; 6224 nfs4_recov_state_t recov_state; 6225 6226 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 6227 6228 *avp = NULL; 6229 recov_state.rs_flags = 0; 6230 recov_state.rs_num_retry_despite_err = 0; 6231 6232 recov_retry: 6233 /* COMPOUND: putfh, openattr, getfh, getattr */ 6234 args.array_len = 4; 6235 args.array = argop; 6236 args.ctag = TAG_OPENATTR; 6237 6238 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 6239 if (e.error) 6240 return (e.error); 6241 6242 drp = VTOR4(dvp); 6243 6244 /* putfh */ 6245 argop[0].argop = OP_CPUTFH; 6246 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6247 6248 /* openattr */ 6249 argop[1].argop = OP_OPENATTR; 6250 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE); 6251 6252 /* getfh */ 6253 argop[2].argop = OP_GETFH; 6254 6255 /* getattr */ 6256 argop[3].argop = OP_GETATTR; 6257 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6258 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp); 6259 6260 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE, 6261 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first", 6262 rnode4info(drp))); 6263 6264 t = gethrtime(); 6265 6266 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e); 6267 6268 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp); 6269 if (needrecov) { 6270 bool_t abort; 6271 6272 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 6273 "nfs4openattr: initiating recovery\n")); 6274 6275 abort = nfs4_start_recovery(&e, 6276 VTOMI4(dvp), dvp, NULL, NULL, NULL, 6277 OP_OPENATTR, NULL); 6278 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6279 if (!e.error) { 6280 e.error = geterrno4(res.status); 6281 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6282 } 6283 if (abort == FALSE) 6284 goto recov_retry; 6285 return (e.error); 6286 } 6287 6288 if (e.error) { 6289 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6290 return (e.error); 6291 } 6292 6293 if (res.status) { 6294 /* 6295 * If OTW errro is NOTSUPP, then it should be 6296 * translated to EINVAL. All Solaris file system 6297 * implementations return EINVAL to the syscall layer 6298 * when the attrdir cannot be created due to an 6299 * implementation restriction or noxattr mount option. 6300 */ 6301 if (res.status == NFS4ERR_NOTSUPP) { 6302 mutex_enter(&drp->r_statelock); 6303 if (drp->r_xattr_dir) 6304 VN_RELE(drp->r_xattr_dir); 6305 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP); 6306 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP; 6307 mutex_exit(&drp->r_statelock); 6308 6309 e.error = EINVAL; 6310 } else { 6311 e.error = geterrno4(res.status); 6312 } 6313 6314 if (e.error) { 6315 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6316 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 6317 needrecov); 6318 return (e.error); 6319 } 6320 } 6321 6322 resop = &res.array[0]; /* putfh res */ 6323 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK); 6324 6325 resop = &res.array[1]; /* openattr res */ 6326 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK); 6327 6328 resop = &res.array[2]; /* getfh res */ 6329 gf_res = &resop->nfs_resop4_u.opgetfh; 6330 if (gf_res->object.nfs_fh4_len == 0) { 6331 *avp = NULL; 6332 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6333 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6334 return (ENOENT); 6335 } 6336 6337 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp)); 6338 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res, 6339 dvp->v_vfsp, t, cr, dvp, 6340 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH)); 6341 sfh4_rele(&sfhp); 6342 6343 if (e.error) 6344 PURGE_ATTRCACHE4(vp); 6345 6346 mutex_enter(&vp->v_lock); 6347 vp->v_flag |= V_XATTRDIR; 6348 mutex_exit(&vp->v_lock); 6349 6350 *avp = vp; 6351 6352 mutex_enter(&drp->r_statelock); 6353 if (drp->r_xattr_dir) 6354 VN_RELE(drp->r_xattr_dir); 6355 VN_HOLD(vp); 6356 drp->r_xattr_dir = vp; 6357 6358 /* 6359 * Invalidate pathconf4 cache because r_xattr_dir is no longer 6360 * NULL. xattrs could be created at any time, and we have no 6361 * way to update pc4_xattr_exists in the base object if/when 6362 * it happens. 6363 */ 6364 drp->r_pathconf.pc4_xattr_valid = 0; 6365 6366 mutex_exit(&drp->r_statelock); 6367 6368 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 6369 6370 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6371 6372 return (0); 6373 } 6374 6375 /* ARGSUSED */ 6376 static int 6377 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 6378 int mode, vnode_t **vpp, cred_t *cr, int flags) 6379 { 6380 int error; 6381 vnode_t *vp = NULL; 6382 rnode4_t *rp; 6383 struct vattr vattr; 6384 rnode4_t *drp; 6385 vnode_t *tempvp; 6386 enum createmode4 createmode; 6387 bool_t must_trunc = FALSE; 6388 int truncating = 0; 6389 6390 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 6391 return (EPERM); 6392 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) { 6393 return (EINVAL); 6394 } 6395 6396 /* . and .. have special meaning in the protocol, reject them. */ 6397 6398 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0'))) 6399 return (EISDIR); 6400 6401 drp = VTOR4(dvp); 6402 6403 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 6404 return (EINTR); 6405 6406 top: 6407 /* 6408 * We make a copy of the attributes because the caller does not 6409 * expect us to change what va points to. 6410 */ 6411 vattr = *va; 6412 6413 /* 6414 * If the pathname is "", then dvp is the root vnode of 6415 * a remote file mounted over a local directory. 6416 * All that needs to be done is access 6417 * checking and truncation. Note that we avoid doing 6418 * open w/ create because the parent directory might 6419 * be in pseudo-fs and the open would fail. 6420 */ 6421 if (*nm == '\0') { 6422 error = 0; 6423 VN_HOLD(dvp); 6424 vp = dvp; 6425 must_trunc = TRUE; 6426 } else { 6427 /* 6428 * We need to go over the wire, just to be sure whether the 6429 * file exists or not. Using the DNLC can be dangerous in 6430 * this case when making a decision regarding existence. 6431 */ 6432 error = nfs4lookup(dvp, nm, &vp, cr, 1); 6433 } 6434 6435 if (exclusive) 6436 createmode = EXCLUSIVE4; 6437 else 6438 createmode = GUARDED4; 6439 6440 /* 6441 * error would be set if the file does not exist on the 6442 * server, so lets go create it. 6443 */ 6444 if (error) { 6445 goto create_otw; 6446 } 6447 6448 /* 6449 * File does exist on the server 6450 */ 6451 if (exclusive == EXCL) 6452 error = EEXIST; 6453 else if (vp->v_type == VDIR && (mode & VWRITE)) 6454 error = EISDIR; 6455 else { 6456 /* 6457 * If vnode is a device, create special vnode. 6458 */ 6459 if (ISVDEV(vp->v_type)) { 6460 tempvp = vp; 6461 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 6462 VN_RELE(tempvp); 6463 } 6464 if (!(error = VOP_ACCESS(vp, mode, 0, cr))) { 6465 if ((vattr.va_mask & AT_SIZE) && 6466 vp->v_type == VREG) { 6467 rp = VTOR4(vp); 6468 /* 6469 * Check here for large file handled 6470 * by LF-unaware process (as 6471 * ufs_create() does) 6472 */ 6473 if (!(flags & FOFFMAX)) { 6474 mutex_enter(&rp->r_statelock); 6475 if (rp->r_size > MAXOFF32_T) 6476 error = EOVERFLOW; 6477 mutex_exit(&rp->r_statelock); 6478 } 6479 6480 /* if error is set then we need to return */ 6481 if (error) { 6482 nfs_rw_exit(&drp->r_rwlock); 6483 VN_RELE(vp); 6484 return (error); 6485 } 6486 6487 if (must_trunc) { 6488 vattr.va_mask = AT_SIZE; 6489 error = nfs4setattr(vp, &vattr, 0, cr, 6490 NULL); 6491 } else { 6492 /* 6493 * we know we have a regular file that already 6494 * exists and we may end up truncating the file 6495 * as a result of the open_otw, so flush out 6496 * any dirty pages for this file first. 6497 */ 6498 if (nfs4_has_pages(vp) && 6499 ((rp->r_flags & R4DIRTY) || 6500 rp->r_count > 0 || 6501 rp->r_mapcnt > 0)) { 6502 error = nfs4_putpage(vp, 6503 (offset_t)0, 0, 0, cr); 6504 if (error && (error == ENOSPC || 6505 error == EDQUOT)) { 6506 mutex_enter( 6507 &rp->r_statelock); 6508 if (!rp->r_error) 6509 rp->r_error = 6510 error; 6511 mutex_exit( 6512 &rp->r_statelock); 6513 } 6514 } 6515 vattr.va_mask = (AT_SIZE | 6516 AT_TYPE | AT_MODE); 6517 vattr.va_type = VREG; 6518 createmode = UNCHECKED4; 6519 truncating = 1; 6520 goto create_otw; 6521 } 6522 } 6523 } 6524 } 6525 nfs_rw_exit(&drp->r_rwlock); 6526 if (error) { 6527 VN_RELE(vp); 6528 } else { 6529 vnode_t *tvp; 6530 rnode4_t *trp; 6531 /* 6532 * existing file got truncated, notify. 6533 */ 6534 tvp = vp; 6535 if (vp->v_type == VREG) { 6536 trp = VTOR4(vp); 6537 if (IS_SHADOW(vp, trp)) 6538 tvp = RTOV4(trp); 6539 } 6540 vnevent_create(tvp); 6541 *vpp = vp; 6542 } 6543 return (error); 6544 6545 create_otw: 6546 dnlc_remove(dvp, nm); 6547 6548 ASSERT(vattr.va_mask & AT_TYPE); 6549 6550 /* 6551 * If not a regular file let nfs4mknod() handle it. 6552 */ 6553 if (vattr.va_type != VREG) { 6554 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 6555 nfs_rw_exit(&drp->r_rwlock); 6556 return (error); 6557 } 6558 6559 /* 6560 * It _is_ a regular file. 6561 */ 6562 ASSERT(vattr.va_mask & AT_MODE); 6563 if (MANDMODE(vattr.va_mode)) { 6564 nfs_rw_exit(&drp->r_rwlock); 6565 return (EACCES); 6566 } 6567 6568 /* 6569 * If this happens to be a mknod of a regular file, then flags will 6570 * have neither FREAD or FWRITE. However, we must set at least one 6571 * for the call to nfs4open_otw. If it's open(O_CREAT) driving 6572 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been 6573 * set (based on openmode specified by app). 6574 */ 6575 if ((flags & (FREAD|FWRITE)) == 0) 6576 flags |= (FREAD|FWRITE); 6577 6578 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0); 6579 6580 if (vp != NULL) { 6581 /* if create was successful, throw away the file's pages */ 6582 if (!error && (vattr.va_mask & AT_SIZE)) 6583 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK), 6584 cr); 6585 /* release the lookup hold */ 6586 VN_RELE(vp); 6587 vp = NULL; 6588 } 6589 6590 /* 6591 * validate that we opened a regular file. This handles a misbehaving 6592 * server that returns an incorrect FH. 6593 */ 6594 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) { 6595 error = EISDIR; 6596 VN_RELE(*vpp); 6597 } 6598 6599 /* 6600 * If this is not an exclusive create, then the CREATE 6601 * request will be made with the GUARDED mode set. This 6602 * means that the server will return EEXIST if the file 6603 * exists. The file could exist because of a retransmitted 6604 * request. In this case, we recover by starting over and 6605 * checking to see whether the file exists. This second 6606 * time through it should and a CREATE request will not be 6607 * sent. 6608 * 6609 * This handles the problem of a dangling CREATE request 6610 * which contains attributes which indicate that the file 6611 * should be truncated. This retransmitted request could 6612 * possibly truncate valid data in the file if not caught 6613 * by the duplicate request mechanism on the server or if 6614 * not caught by other means. The scenario is: 6615 * 6616 * Client transmits CREATE request with size = 0 6617 * Client times out, retransmits request. 6618 * Response to the first request arrives from the server 6619 * and the client proceeds on. 6620 * Client writes data to the file. 6621 * The server now processes retransmitted CREATE request 6622 * and truncates file. 6623 * 6624 * The use of the GUARDED CREATE request prevents this from 6625 * happening because the retransmitted CREATE would fail 6626 * with EEXIST and would not truncate the file. 6627 */ 6628 if (error == EEXIST && exclusive == NONEXCL) { 6629 #ifdef DEBUG 6630 nfs4_create_misses++; 6631 #endif 6632 goto top; 6633 } 6634 nfs_rw_exit(&drp->r_rwlock); 6635 if (truncating && !error && *vpp) { 6636 vnode_t *tvp; 6637 rnode4_t *trp; 6638 /* 6639 * existing file got truncated, notify. 6640 */ 6641 tvp = *vpp; 6642 trp = VTOR4(tvp); 6643 if (IS_SHADOW(tvp, trp)) 6644 tvp = RTOV4(trp); 6645 vnevent_create(tvp); 6646 } 6647 return (error); 6648 } 6649 6650 /* 6651 * Create compound (for mkdir, mknod, symlink): 6652 * { Putfh <dfh>; Create; Getfh; Getattr } 6653 * It's okay if setattr failed to set gid - this is not considered 6654 * an error, but purge attrs in that case. 6655 */ 6656 static int 6657 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va, 6658 vnode_t **vpp, cred_t *cr, nfs_ftype4 type) 6659 { 6660 int need_end_op = FALSE; 6661 COMPOUND4args_clnt args; 6662 COMPOUND4res_clnt res, *resp = NULL; 6663 nfs_argop4 *argop; 6664 nfs_resop4 *resop; 6665 int doqueue; 6666 mntinfo4_t *mi; 6667 rnode4_t *drp = VTOR4(dvp); 6668 change_info4 *cinfo; 6669 GETFH4res *gf_res; 6670 struct vattr vattr; 6671 vnode_t *vp; 6672 fattr4 *crattr; 6673 bool_t needrecov = FALSE; 6674 nfs4_recov_state_t recov_state; 6675 nfs4_sharedfh_t *sfhp = NULL; 6676 hrtime_t t; 6677 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 6678 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr; 6679 dirattr_info_t dinfo, *dinfop; 6680 servinfo4_t *svp; 6681 bitmap4 supp_attrs; 6682 6683 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK || 6684 type == NF4CHR || type == NF4SOCK || type == NF4FIFO); 6685 6686 mi = VTOMI4(dvp); 6687 6688 /* 6689 * Make sure we properly deal with setting the right gid 6690 * on a new directory to reflect the parent's setgid bit 6691 */ 6692 setgid_flag = 0; 6693 if (type == NF4DIR) { 6694 struct vattr dva; 6695 6696 va->va_mode &= ~VSGID; 6697 dva.va_mask = AT_MODE | AT_GID; 6698 if (VOP_GETATTR(dvp, &dva, 0, cr) == 0) { 6699 6700 /* 6701 * If the parent's directory has the setgid bit set 6702 * _and_ the client was able to get a valid mapping 6703 * for the parent dir's owner_group, we want to 6704 * append NVERIFY(owner_group == dva.va_gid) and 6705 * SETTATTR to the CREATE compound. 6706 */ 6707 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) { 6708 setgid_flag = 1; 6709 va->va_mode |= VSGID; 6710 if (dva.va_gid != GID_NOBODY) { 6711 va->va_mask |= AT_GID; 6712 va->va_gid = dva.va_gid; 6713 } 6714 } 6715 } 6716 } 6717 6718 /* 6719 * Create ops: 6720 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new) 6721 * 5:restorefh(dir) 6:getattr(dir) 6722 * 6723 * if (setgid) 6724 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new) 6725 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new) 6726 * 8:nverify 9:setattr 6727 */ 6728 if (setgid_flag) { 6729 numops = 10; 6730 idx_create = 1; 6731 idx_fattr = 3; 6732 } else { 6733 numops = 7; 6734 idx_create = 2; 6735 idx_fattr = 4; 6736 } 6737 6738 ASSERT(nfs_zone() == mi->mi_zone); 6739 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) { 6740 return (EINTR); 6741 } 6742 recov_state.rs_flags = 0; 6743 recov_state.rs_num_retry_despite_err = 0; 6744 6745 argoplist_size = numops * sizeof (nfs_argop4); 6746 argop = kmem_alloc(argoplist_size, KM_SLEEP); 6747 6748 recov_retry: 6749 if (type == NF4LNK) 6750 args.ctag = TAG_SYMLINK; 6751 else if (type == NF4DIR) 6752 args.ctag = TAG_MKDIR; 6753 else 6754 args.ctag = TAG_MKNOD; 6755 6756 args.array_len = numops; 6757 args.array = argop; 6758 6759 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) { 6760 nfs_rw_exit(&drp->r_rwlock); 6761 kmem_free(argop, argoplist_size); 6762 return (e.error); 6763 } 6764 need_end_op = TRUE; 6765 6766 6767 /* 0: putfh directory */ 6768 argop[0].argop = OP_CPUTFH; 6769 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6770 6771 /* 1/2: Create object */ 6772 argop[idx_create].argop = OP_CCREATE; 6773 argop[idx_create].nfs_argop4_u.opccreate.cname = nm; 6774 argop[idx_create].nfs_argop4_u.opccreate.type = type; 6775 if (type == NF4LNK) { 6776 /* 6777 * symlink, treat name as data 6778 */ 6779 ASSERT(data != NULL); 6780 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata = 6781 (char *)data; 6782 } 6783 if (type == NF4BLK || type == NF4CHR) { 6784 ASSERT(data != NULL); 6785 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata = 6786 *((specdata4 *)data); 6787 } 6788 6789 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs; 6790 6791 svp = drp->r_server; 6792 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 6793 supp_attrs = svp->sv_supp_attrs; 6794 nfs_rw_exit(&svp->sv_lock); 6795 6796 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) { 6797 nfs_rw_exit(&drp->r_rwlock); 6798 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 6799 e.error = EINVAL; 6800 kmem_free(argop, argoplist_size); 6801 return (e.error); 6802 } 6803 6804 /* 2/3: getfh fh of created object */ 6805 ASSERT(idx_create + 1 == idx_fattr - 1); 6806 argop[idx_create + 1].argop = OP_GETFH; 6807 6808 /* 3/4: getattr of new object */ 6809 argop[idx_fattr].argop = OP_GETATTR; 6810 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6811 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi; 6812 6813 if (setgid_flag) { 6814 vattr_t _v; 6815 6816 argop[4].argop = OP_SAVEFH; 6817 6818 argop[5].argop = OP_CPUTFH; 6819 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 6820 6821 argop[6].argop = OP_GETATTR; 6822 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6823 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6824 6825 argop[7].argop = OP_RESTOREFH; 6826 6827 /* 6828 * nverify 6829 * 6830 * XXX - Revisit the last argument to nfs4_end_op() 6831 * once 5020486 is fixed. 6832 */ 6833 _v.va_mask = AT_GID; 6834 _v.va_gid = va->va_gid; 6835 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY, 6836 supp_attrs)) { 6837 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6838 nfs_rw_exit(&drp->r_rwlock); 6839 nfs4_fattr4_free(crattr); 6840 kmem_free(argop, argoplist_size); 6841 return (e.error); 6842 } 6843 6844 /* 6845 * setattr 6846 * 6847 * We _know_ we're not messing with AT_SIZE or AT_XTIME, 6848 * so no need for stateid or flags. Also we specify NULL 6849 * rp since we're only interested in setting owner_group 6850 * attributes. 6851 */ 6852 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs, 6853 &e.error, 0); 6854 6855 if (e.error) { 6856 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE); 6857 nfs_rw_exit(&drp->r_rwlock); 6858 nfs4_fattr4_free(crattr); 6859 nfs4args_verify_free(&argop[8]); 6860 kmem_free(argop, argoplist_size); 6861 return (e.error); 6862 } 6863 } else { 6864 argop[1].argop = OP_SAVEFH; 6865 6866 argop[5].argop = OP_RESTOREFH; 6867 6868 argop[6].argop = OP_GETATTR; 6869 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 6870 argop[6].nfs_argop4_u.opgetattr.mi = mi; 6871 } 6872 6873 dnlc_remove(dvp, nm); 6874 6875 doqueue = 1; 6876 t = gethrtime(); 6877 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 6878 6879 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 6880 if (e.error) { 6881 PURGE_ATTRCACHE4(dvp); 6882 if (!needrecov) 6883 goto out; 6884 } 6885 6886 if (needrecov) { 6887 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL, 6888 OP_CREATE, NULL) == FALSE) { 6889 nfs4_end_op(mi, dvp, NULL, &recov_state, 6890 needrecov); 6891 need_end_op = FALSE; 6892 nfs4_fattr4_free(crattr); 6893 if (setgid_flag) { 6894 nfs4args_verify_free(&argop[8]); 6895 nfs4args_setattr_free(&argop[9]); 6896 } 6897 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 6898 goto recov_retry; 6899 } 6900 } 6901 6902 resp = &res; 6903 6904 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) { 6905 6906 if (res.status == NFS4ERR_BADOWNER) 6907 nfs4_log_badowner(mi, OP_CREATE); 6908 6909 e.error = geterrno4(res.status); 6910 6911 /* 6912 * This check is left over from when create was implemented 6913 * using a setattr op (instead of createattrs). If the 6914 * putfh/create/getfh failed, the error was returned. If 6915 * setattr/getattr failed, we keep going. 6916 * 6917 * It might be better to get rid of the GETFH also, and just 6918 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory. 6919 * Then if any of the operations failed, we could return the 6920 * error now, and remove much of the error code below. 6921 */ 6922 if (res.array_len <= idx_fattr) { 6923 /* 6924 * Either Putfh, Create or Getfh failed. 6925 */ 6926 PURGE_ATTRCACHE4(dvp); 6927 /* 6928 * nfs4_purge_stale_fh() may generate otw calls through 6929 * nfs4_invalidate_pages. Hence the need to call 6930 * nfs4_end_op() here to avoid nfs4_start_op() deadlock. 6931 */ 6932 nfs4_end_op(mi, dvp, NULL, &recov_state, 6933 needrecov); 6934 need_end_op = FALSE; 6935 nfs4_purge_stale_fh(e.error, dvp, cr); 6936 goto out; 6937 } 6938 } 6939 6940 resop = &res.array[idx_create]; /* create res */ 6941 cinfo = &resop->nfs_resop4_u.opcreate.cinfo; 6942 6943 resop = &res.array[idx_create + 1]; /* getfh res */ 6944 gf_res = &resop->nfs_resop4_u.opgetfh; 6945 6946 sfhp = sfh4_get(&gf_res->object, mi); 6947 if (e.error) { 6948 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp, 6949 fn_get(VTOSV(dvp)->sv_name, nm)); 6950 if (vp->v_type == VNON) { 6951 vattr.va_mask = AT_TYPE; 6952 /* 6953 * Need to call nfs4_end_op before nfs4getattr to avoid 6954 * potential nfs4_start_op deadlock. See RFE 4777612. 6955 */ 6956 nfs4_end_op(mi, dvp, NULL, &recov_state, 6957 needrecov); 6958 need_end_op = FALSE; 6959 e.error = nfs4getattr(vp, &vattr, cr); 6960 if (e.error) { 6961 VN_RELE(vp); 6962 *vpp = NULL; 6963 goto out; 6964 } 6965 vp->v_type = vattr.va_type; 6966 } 6967 e.error = 0; 6968 } else { 6969 *vpp = vp = makenfs4node(sfhp, 6970 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res, 6971 dvp->v_vfsp, t, cr, 6972 dvp, fn_get(VTOSV(dvp)->sv_name, nm)); 6973 } 6974 6975 /* 6976 * If compound succeeded, then update dir attrs 6977 */ 6978 if (res.status == NFS4_OK) { 6979 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res; 6980 dinfo.di_cred = cr; 6981 dinfo.di_time_call = t; 6982 dinfop = &dinfo; 6983 } else 6984 dinfop = NULL; 6985 6986 /* Update directory cache attribute, readdir and dnlc caches */ 6987 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop); 6988 6989 out: 6990 if (sfhp != NULL) 6991 sfh4_rele(&sfhp); 6992 nfs_rw_exit(&drp->r_rwlock); 6993 nfs4_fattr4_free(crattr); 6994 if (setgid_flag) { 6995 nfs4args_verify_free(&argop[8]); 6996 nfs4args_setattr_free(&argop[9]); 6997 } 6998 if (resp) 6999 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7000 if (need_end_op) 7001 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov); 7002 7003 kmem_free(argop, argoplist_size); 7004 return (e.error); 7005 } 7006 7007 /* ARGSUSED */ 7008 static int 7009 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 7010 int mode, vnode_t **vpp, cred_t *cr) 7011 { 7012 int error; 7013 vnode_t *vp; 7014 nfs_ftype4 type; 7015 specdata4 spec, *specp = NULL; 7016 7017 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 7018 7019 switch (va->va_type) { 7020 case VCHR: 7021 case VBLK: 7022 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK; 7023 spec.specdata1 = getmajor(va->va_rdev); 7024 spec.specdata2 = getminor(va->va_rdev); 7025 specp = &spec; 7026 break; 7027 7028 case VFIFO: 7029 type = NF4FIFO; 7030 break; 7031 case VSOCK: 7032 type = NF4SOCK; 7033 break; 7034 7035 default: 7036 return (EINVAL); 7037 } 7038 7039 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type); 7040 if (error) { 7041 return (error); 7042 } 7043 7044 /* 7045 * This might not be needed any more; special case to deal 7046 * with problematic v2/v3 servers. Since create was unable 7047 * to set group correctly, not sure what hope setattr has. 7048 */ 7049 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) { 7050 va->va_mask = AT_GID; 7051 (void) nfs4setattr(vp, va, 0, cr, NULL); 7052 } 7053 7054 /* 7055 * If vnode is a device create special vnode 7056 */ 7057 if (ISVDEV(vp->v_type)) { 7058 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 7059 VN_RELE(vp); 7060 } else { 7061 *vpp = vp; 7062 } 7063 return (error); 7064 } 7065 7066 /* 7067 * Remove requires that the current fh be the target directory. 7068 * After the operation, the current fh is unchanged. 7069 * The compound op structure is: 7070 * PUTFH(targetdir), REMOVE 7071 * 7072 * Weirdness: if the vnode to be removed is open 7073 * we rename it instead of removing it and nfs_inactive 7074 * will remove the new name. 7075 */ 7076 static int 7077 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr) 7078 { 7079 COMPOUND4args_clnt args; 7080 COMPOUND4res_clnt res, *resp = NULL; 7081 REMOVE4res *rm_res; 7082 nfs_argop4 argop[3]; 7083 nfs_resop4 *resop; 7084 vnode_t *vp; 7085 char *tmpname; 7086 int doqueue; 7087 mntinfo4_t *mi; 7088 rnode4_t *rp; 7089 rnode4_t *drp; 7090 int needrecov = 0; 7091 nfs4_recov_state_t recov_state; 7092 int isopen; 7093 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7094 dirattr_info_t dinfo; 7095 7096 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 7097 return (EPERM); 7098 drp = VTOR4(dvp); 7099 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 7100 return (EINTR); 7101 7102 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 7103 if (e.error) { 7104 nfs_rw_exit(&drp->r_rwlock); 7105 return (e.error); 7106 } 7107 7108 if (vp->v_type == VDIR) { 7109 VN_RELE(vp); 7110 nfs_rw_exit(&drp->r_rwlock); 7111 return (EISDIR); 7112 } 7113 7114 /* 7115 * First just remove the entry from the name cache, as it 7116 * is most likely the only entry for this vp. 7117 */ 7118 dnlc_remove(dvp, nm); 7119 7120 rp = VTOR4(vp); 7121 7122 /* 7123 * For regular file types, check to see if the file is open by looking 7124 * at the open streams. 7125 * For all other types, check the reference count on the vnode. Since 7126 * they are not opened OTW they never have an open stream. 7127 * 7128 * If the file is open, rename it to .nfsXXXX. 7129 */ 7130 if (vp->v_type != VREG) { 7131 /* 7132 * If the file has a v_count > 1 then there may be more than one 7133 * entry in the name cache due multiple links or an open file, 7134 * but we don't have the real reference count so flush all 7135 * possible entries. 7136 */ 7137 if (vp->v_count > 1) 7138 dnlc_purge_vp(vp); 7139 7140 /* 7141 * Now we have the real reference count. 7142 */ 7143 isopen = vp->v_count > 1; 7144 } else { 7145 mutex_enter(&rp->r_os_lock); 7146 isopen = list_head(&rp->r_open_streams) != NULL; 7147 mutex_exit(&rp->r_os_lock); 7148 } 7149 7150 mutex_enter(&rp->r_statelock); 7151 if (isopen && 7152 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 7153 mutex_exit(&rp->r_statelock); 7154 tmpname = newname(); 7155 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr); 7156 if (e.error) 7157 kmem_free(tmpname, MAXNAMELEN); 7158 else { 7159 mutex_enter(&rp->r_statelock); 7160 if (rp->r_unldvp == NULL) { 7161 VN_HOLD(dvp); 7162 rp->r_unldvp = dvp; 7163 if (rp->r_unlcred != NULL) 7164 crfree(rp->r_unlcred); 7165 crhold(cr); 7166 rp->r_unlcred = cr; 7167 rp->r_unlname = tmpname; 7168 } else { 7169 kmem_free(rp->r_unlname, MAXNAMELEN); 7170 rp->r_unlname = tmpname; 7171 } 7172 mutex_exit(&rp->r_statelock); 7173 } 7174 VN_RELE(vp); 7175 nfs_rw_exit(&drp->r_rwlock); 7176 return (e.error); 7177 } 7178 /* 7179 * Actually remove the file/dir 7180 */ 7181 mutex_exit(&rp->r_statelock); 7182 7183 /* 7184 * We need to flush any dirty pages which happen to 7185 * be hanging around before removing the file. 7186 * This shouldn't happen very often since in NFSv4 7187 * we should be close to open consistent. 7188 */ 7189 if (nfs4_has_pages(vp) && 7190 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) { 7191 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr); 7192 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) { 7193 mutex_enter(&rp->r_statelock); 7194 if (!rp->r_error) 7195 rp->r_error = e.error; 7196 mutex_exit(&rp->r_statelock); 7197 } 7198 } 7199 7200 mi = VTOMI4(dvp); 7201 7202 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN); 7203 recov_state.rs_flags = 0; 7204 recov_state.rs_num_retry_despite_err = 0; 7205 7206 recov_retry: 7207 /* 7208 * Remove ops: putfh dir; remove 7209 */ 7210 args.ctag = TAG_REMOVE; 7211 args.array_len = 3; 7212 args.array = argop; 7213 7214 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 7215 if (e.error) { 7216 nfs_rw_exit(&drp->r_rwlock); 7217 VN_RELE(vp); 7218 return (e.error); 7219 } 7220 7221 /* putfh directory */ 7222 argop[0].argop = OP_CPUTFH; 7223 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 7224 7225 /* remove */ 7226 argop[1].argop = OP_CREMOVE; 7227 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 7228 7229 /* getattr dir */ 7230 argop[2].argop = OP_GETATTR; 7231 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7232 argop[2].nfs_argop4_u.opgetattr.mi = mi; 7233 7234 doqueue = 1; 7235 dinfo.di_time_call = gethrtime(); 7236 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 7237 7238 PURGE_ATTRCACHE4(vp); 7239 7240 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 7241 if (e.error) 7242 PURGE_ATTRCACHE4(dvp); 7243 7244 if (needrecov) { 7245 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, 7246 NULL, NULL, NULL, OP_REMOVE, NULL) == FALSE) { 7247 if (!e.error) 7248 (void) xdr_free(xdr_COMPOUND4res_clnt, 7249 (caddr_t)&res); 7250 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 7251 needrecov); 7252 goto recov_retry; 7253 } 7254 } 7255 7256 /* 7257 * Matching nfs4_end_op() for start_op() above. 7258 * There is a path in the code below which calls 7259 * nfs4_purge_stale_fh(), which may generate otw calls through 7260 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op() 7261 * here to avoid nfs4_start_op() deadlock. 7262 */ 7263 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 7264 7265 if (!e.error) { 7266 resp = &res; 7267 7268 if (res.status) { 7269 e.error = geterrno4(res.status); 7270 PURGE_ATTRCACHE4(dvp); 7271 nfs4_purge_stale_fh(e.error, dvp, cr); 7272 } else { 7273 resop = &res.array[1]; /* remove res */ 7274 rm_res = &resop->nfs_resop4_u.opremove; 7275 7276 dinfo.di_garp = 7277 &res.array[2].nfs_resop4_u.opgetattr.ga_res; 7278 dinfo.di_cred = cr; 7279 7280 /* Update directory attr, readdir and dnlc caches */ 7281 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 7282 &dinfo); 7283 } 7284 } 7285 nfs_rw_exit(&drp->r_rwlock); 7286 if (resp) 7287 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7288 7289 if (e.error == 0) { 7290 vnode_t *tvp; 7291 rnode4_t *trp; 7292 trp = VTOR4(vp); 7293 tvp = vp; 7294 if (IS_SHADOW(vp, trp)) 7295 tvp = RTOV4(trp); 7296 vnevent_remove(tvp, dvp, nm); 7297 } 7298 VN_RELE(vp); 7299 return (e.error); 7300 } 7301 7302 /* 7303 * Link requires that the current fh be the target directory and the 7304 * saved fh be the source fh. After the operation, the current fh is unchanged. 7305 * Thus the compound op structure is: 7306 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH, 7307 * GETATTR(file) 7308 */ 7309 static int 7310 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr) 7311 { 7312 COMPOUND4args_clnt args; 7313 COMPOUND4res_clnt res, *resp = NULL; 7314 LINK4res *ln_res; 7315 int argoplist_size = 7 * sizeof (nfs_argop4); 7316 nfs_argop4 *argop; 7317 nfs_resop4 *resop; 7318 vnode_t *realvp, *nvp; 7319 int doqueue; 7320 mntinfo4_t *mi; 7321 rnode4_t *tdrp; 7322 bool_t needrecov = FALSE; 7323 nfs4_recov_state_t recov_state; 7324 hrtime_t t; 7325 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7326 dirattr_info_t dinfo; 7327 7328 ASSERT(*tnm != '\0'); 7329 ASSERT(tdvp->v_type == VDIR); 7330 ASSERT(nfs4_consistent_type(tdvp)); 7331 ASSERT(nfs4_consistent_type(svp)); 7332 7333 if (nfs_zone() != VTOMI4(tdvp)->mi_zone) 7334 return (EPERM); 7335 if (VOP_REALVP(svp, &realvp) == 0) { 7336 svp = realvp; 7337 ASSERT(nfs4_consistent_type(svp)); 7338 } 7339 7340 tdrp = VTOR4(tdvp); 7341 mi = VTOMI4(svp); 7342 7343 if (!(mi->mi_flags & MI4_LINK)) { 7344 return (EOPNOTSUPP); 7345 } 7346 recov_state.rs_flags = 0; 7347 recov_state.rs_num_retry_despite_err = 0; 7348 7349 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp))) 7350 return (EINTR); 7351 7352 recov_retry: 7353 argop = kmem_alloc(argoplist_size, KM_SLEEP); 7354 7355 args.ctag = TAG_LINK; 7356 7357 /* 7358 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir); 7359 * restorefh; getattr(fl) 7360 */ 7361 args.array_len = 7; 7362 args.array = argop; 7363 7364 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state); 7365 if (e.error) { 7366 kmem_free(argop, argoplist_size); 7367 nfs_rw_exit(&tdrp->r_rwlock); 7368 return (e.error); 7369 } 7370 7371 /* 0. putfh file */ 7372 argop[0].argop = OP_CPUTFH; 7373 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh; 7374 7375 /* 1. save current fh to free up the space for the dir */ 7376 argop[1].argop = OP_SAVEFH; 7377 7378 /* 2. putfh targetdir */ 7379 argop[2].argop = OP_CPUTFH; 7380 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh; 7381 7382 /* 3. link: current_fh is targetdir, saved_fh is source */ 7383 argop[3].argop = OP_CLINK; 7384 argop[3].nfs_argop4_u.opclink.cnewname = tnm; 7385 7386 /* 4. Get attributes of dir */ 7387 argop[4].argop = OP_GETATTR; 7388 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7389 argop[4].nfs_argop4_u.opgetattr.mi = mi; 7390 7391 /* 5. If link was successful, restore current vp to file */ 7392 argop[5].argop = OP_RESTOREFH; 7393 7394 /* 6. Get attributes of linked object */ 7395 argop[6].argop = OP_GETATTR; 7396 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 7397 argop[6].nfs_argop4_u.opgetattr.mi = mi; 7398 7399 dnlc_remove(tdvp, tnm); 7400 7401 doqueue = 1; 7402 t = gethrtime(); 7403 7404 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e); 7405 7406 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp); 7407 if (e.error != 0 && !needrecov) { 7408 PURGE_ATTRCACHE4(tdvp); 7409 PURGE_ATTRCACHE4(svp); 7410 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7411 goto out; 7412 } 7413 7414 if (needrecov) { 7415 bool_t abort; 7416 7417 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp, 7418 NULL, NULL, OP_LINK, NULL); 7419 if (abort == FALSE) { 7420 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, 7421 needrecov); 7422 kmem_free(argop, argoplist_size); 7423 if (!e.error) 7424 (void) xdr_free(xdr_COMPOUND4res_clnt, 7425 (caddr_t)&res); 7426 goto recov_retry; 7427 } else { 7428 if (e.error != 0) { 7429 PURGE_ATTRCACHE4(tdvp); 7430 PURGE_ATTRCACHE4(svp); 7431 nfs4_end_op(VTOMI4(svp), svp, tdvp, 7432 &recov_state, needrecov); 7433 goto out; 7434 } 7435 /* fall through for res.status case */ 7436 } 7437 } 7438 7439 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov); 7440 7441 resp = &res; 7442 if (res.status) { 7443 /* If link succeeded, then don't return error */ 7444 e.error = geterrno4(res.status); 7445 if (res.array_len <= 4) { 7446 /* 7447 * Either Putfh, Savefh, Putfh dir, or Link failed 7448 */ 7449 PURGE_ATTRCACHE4(svp); 7450 PURGE_ATTRCACHE4(tdvp); 7451 if (e.error == EOPNOTSUPP) { 7452 mutex_enter(&mi->mi_lock); 7453 mi->mi_flags &= ~MI4_LINK; 7454 mutex_exit(&mi->mi_lock); 7455 } 7456 /* Remap EISDIR to EPERM for non-root user for SVVS */ 7457 /* XXX-LP */ 7458 if (e.error == EISDIR && crgetuid(cr) != 0) 7459 e.error = EPERM; 7460 goto out; 7461 } 7462 } 7463 7464 /* either no error or one of the postop getattr failed */ 7465 7466 /* 7467 * XXX - if LINK succeeded, but no attrs were returned for link 7468 * file, purge its cache. 7469 * 7470 * XXX Perform a simplified version of wcc checking. Instead of 7471 * have another getattr to get pre-op, just purge cache if 7472 * any of the ops prior to and including the getattr failed. 7473 * If the getattr succeeded then update the attrcache accordingly. 7474 */ 7475 7476 /* 7477 * update cache with link file postattrs. 7478 * Note: at this point resop points to link res. 7479 */ 7480 resop = &res.array[3]; /* link res */ 7481 ln_res = &resop->nfs_resop4_u.oplink; 7482 if (res.status == NFS4_OK) 7483 e.error = nfs4_update_attrcache(res.status, 7484 &res.array[6].nfs_resop4_u.opgetattr.ga_res, 7485 t, svp, cr); 7486 7487 /* 7488 * Call makenfs4node to create the new shadow vp for tnm. 7489 * We pass NULL attrs because we just cached attrs for 7490 * the src object. All we're trying to accomplish is to 7491 * to create the new shadow vnode. 7492 */ 7493 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr, 7494 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm)); 7495 7496 /* Update target cache attribute, readdir and dnlc caches */ 7497 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res; 7498 dinfo.di_time_call = t; 7499 dinfo.di_cred = cr; 7500 7501 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo); 7502 ASSERT(nfs4_consistent_type(tdvp)); 7503 ASSERT(nfs4_consistent_type(svp)); 7504 ASSERT(nfs4_consistent_type(nvp)); 7505 VN_RELE(nvp); 7506 7507 if (!e.error) { 7508 vnode_t *tvp; 7509 rnode4_t *trp; 7510 /* 7511 * Notify the source file of this link operation. 7512 */ 7513 trp = VTOR4(svp); 7514 tvp = svp; 7515 if (IS_SHADOW(svp, trp)) 7516 tvp = RTOV4(trp); 7517 vnevent_link(tvp); 7518 } 7519 out: 7520 kmem_free(argop, argoplist_size); 7521 if (resp) 7522 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 7523 7524 nfs_rw_exit(&tdrp->r_rwlock); 7525 7526 return (e.error); 7527 } 7528 7529 static int 7530 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 7531 { 7532 vnode_t *realvp; 7533 7534 if (nfs_zone() != VTOMI4(odvp)->mi_zone) 7535 return (EPERM); 7536 if (VOP_REALVP(ndvp, &realvp) == 0) 7537 ndvp = realvp; 7538 7539 return (nfs4rename(odvp, onm, ndvp, nnm, cr)); 7540 } 7541 7542 /* 7543 * nfs4rename does the real work of renaming in NFS Version 4. 7544 * 7545 * A file handle is considered volatile for renaming purposes if either 7546 * of the volatile bits are turned on. However, the compound may differ 7547 * based on the likelihood of the filehandle to change during rename. 7548 */ 7549 static int 7550 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 7551 { 7552 int error; 7553 mntinfo4_t *mi; 7554 vnode_t *nvp = NULL; 7555 vnode_t *ovp = NULL; 7556 char *tmpname = NULL; 7557 rnode4_t *rp; 7558 rnode4_t *odrp; 7559 rnode4_t *ndrp; 7560 int did_link = 0; 7561 int do_link = 1; 7562 nfsstat4 stat = NFS4_OK; 7563 7564 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7565 ASSERT(nfs4_consistent_type(odvp)); 7566 ASSERT(nfs4_consistent_type(ndvp)); 7567 7568 if (onm[0] == '.' && (onm[1] == '\0' || 7569 (onm[1] == '.' && onm[2] == '\0'))) 7570 return (EINVAL); 7571 7572 if (nnm[0] == '.' && (nnm[1] == '\0' || 7573 (nnm[1] == '.' && nnm[2] == '\0'))) 7574 return (EINVAL); 7575 7576 odrp = VTOR4(odvp); 7577 ndrp = VTOR4(ndvp); 7578 if ((intptr_t)odrp < (intptr_t)ndrp) { 7579 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) 7580 return (EINTR); 7581 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) { 7582 nfs_rw_exit(&odrp->r_rwlock); 7583 return (EINTR); 7584 } 7585 } else { 7586 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) 7587 return (EINTR); 7588 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) { 7589 nfs_rw_exit(&ndrp->r_rwlock); 7590 return (EINTR); 7591 } 7592 } 7593 7594 /* 7595 * Lookup the target file. If it exists, it needs to be 7596 * checked to see whether it is a mount point and whether 7597 * it is active (open). 7598 */ 7599 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0); 7600 if (!error) { 7601 int isactive; 7602 7603 ASSERT(nfs4_consistent_type(nvp)); 7604 /* 7605 * If this file has been mounted on, then just 7606 * return busy because renaming to it would remove 7607 * the mounted file system from the name space. 7608 */ 7609 if (vn_ismntpt(nvp)) { 7610 VN_RELE(nvp); 7611 nfs_rw_exit(&odrp->r_rwlock); 7612 nfs_rw_exit(&ndrp->r_rwlock); 7613 return (EBUSY); 7614 } 7615 7616 /* 7617 * First just remove the entry from the name cache, as it 7618 * is most likely the only entry for this vp. 7619 */ 7620 dnlc_remove(ndvp, nnm); 7621 7622 rp = VTOR4(nvp); 7623 7624 if (nvp->v_type != VREG) { 7625 /* 7626 * Purge the name cache of all references to this vnode 7627 * so that we can check the reference count to infer 7628 * whether it is active or not. 7629 */ 7630 if (nvp->v_count > 1) 7631 dnlc_purge_vp(nvp); 7632 7633 isactive = nvp->v_count > 1; 7634 } else { 7635 mutex_enter(&rp->r_os_lock); 7636 isactive = list_head(&rp->r_open_streams) != NULL; 7637 mutex_exit(&rp->r_os_lock); 7638 } 7639 7640 /* 7641 * If the vnode is active and is not a directory, 7642 * arrange to rename it to a 7643 * temporary file so that it will continue to be 7644 * accessible. This implements the "unlink-open-file" 7645 * semantics for the target of a rename operation. 7646 * Before doing this though, make sure that the 7647 * source and target files are not already the same. 7648 */ 7649 if (isactive && nvp->v_type != VDIR) { 7650 /* 7651 * Lookup the source name. 7652 */ 7653 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7654 7655 /* 7656 * The source name *should* already exist. 7657 */ 7658 if (error) { 7659 VN_RELE(nvp); 7660 nfs_rw_exit(&odrp->r_rwlock); 7661 nfs_rw_exit(&ndrp->r_rwlock); 7662 return (error); 7663 } 7664 7665 ASSERT(nfs4_consistent_type(ovp)); 7666 7667 /* 7668 * Compare the two vnodes. If they are the same, 7669 * just release all held vnodes and return success. 7670 */ 7671 if (VN_CMP(ovp, nvp)) { 7672 VN_RELE(ovp); 7673 VN_RELE(nvp); 7674 nfs_rw_exit(&odrp->r_rwlock); 7675 nfs_rw_exit(&ndrp->r_rwlock); 7676 return (0); 7677 } 7678 7679 /* 7680 * Can't mix and match directories and non- 7681 * directories in rename operations. We already 7682 * know that the target is not a directory. If 7683 * the source is a directory, return an error. 7684 */ 7685 if (ovp->v_type == VDIR) { 7686 VN_RELE(ovp); 7687 VN_RELE(nvp); 7688 nfs_rw_exit(&odrp->r_rwlock); 7689 nfs_rw_exit(&ndrp->r_rwlock); 7690 return (ENOTDIR); 7691 } 7692 link_call: 7693 /* 7694 * The target file exists, is not the same as 7695 * the source file, and is active. We first 7696 * try to Link it to a temporary filename to 7697 * avoid having the server removing the file 7698 * completely (which could cause data loss to 7699 * the user's POV in the event the Rename fails 7700 * -- see bug 1165874). 7701 */ 7702 /* 7703 * The do_link and did_link booleans are 7704 * introduced in the event we get NFS4ERR_FILE_OPEN 7705 * returned for the Rename. Some servers can 7706 * not Rename over an Open file, so they return 7707 * this error. The client needs to Remove the 7708 * newly created Link and do two Renames, just 7709 * as if the server didn't support LINK. 7710 */ 7711 tmpname = newname(); 7712 error = 0; 7713 7714 if (do_link) { 7715 error = nfs4_link(ndvp, nvp, tmpname, cr); 7716 } 7717 if (error == EOPNOTSUPP || !do_link) { 7718 error = nfs4_rename(ndvp, nnm, ndvp, tmpname, 7719 cr); 7720 did_link = 0; 7721 } else { 7722 did_link = 1; 7723 } 7724 if (error) { 7725 kmem_free(tmpname, MAXNAMELEN); 7726 VN_RELE(ovp); 7727 VN_RELE(nvp); 7728 nfs_rw_exit(&odrp->r_rwlock); 7729 nfs_rw_exit(&ndrp->r_rwlock); 7730 return (error); 7731 } 7732 7733 mutex_enter(&rp->r_statelock); 7734 if (rp->r_unldvp == NULL) { 7735 VN_HOLD(ndvp); 7736 rp->r_unldvp = ndvp; 7737 if (rp->r_unlcred != NULL) 7738 crfree(rp->r_unlcred); 7739 crhold(cr); 7740 rp->r_unlcred = cr; 7741 rp->r_unlname = tmpname; 7742 } else { 7743 if (rp->r_unlname) 7744 kmem_free(rp->r_unlname, MAXNAMELEN); 7745 rp->r_unlname = tmpname; 7746 } 7747 mutex_exit(&rp->r_statelock); 7748 } 7749 7750 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7751 7752 ASSERT(nfs4_consistent_type(nvp)); 7753 } 7754 7755 if (ovp == NULL) { 7756 /* 7757 * When renaming directories to be a subdirectory of a 7758 * different parent, the dnlc entry for ".." will no 7759 * longer be valid, so it must be removed. 7760 * 7761 * We do a lookup here to determine whether we are renaming 7762 * a directory and we need to check if we are renaming 7763 * an unlinked file. This might have already been done 7764 * in previous code, so we check ovp == NULL to avoid 7765 * doing it twice. 7766 */ 7767 error = nfs4lookup(odvp, onm, &ovp, cr, 0); 7768 /* 7769 * The source name *should* already exist. 7770 */ 7771 if (error) { 7772 nfs_rw_exit(&odrp->r_rwlock); 7773 nfs_rw_exit(&ndrp->r_rwlock); 7774 if (nvp) { 7775 VN_RELE(nvp); 7776 } 7777 return (error); 7778 } 7779 ASSERT(ovp != NULL); 7780 ASSERT(nfs4_consistent_type(ovp)); 7781 } 7782 7783 /* 7784 * Is the object being renamed a dir, and if so, is 7785 * it being renamed to a child of itself? The underlying 7786 * fs should ultimately return EINVAL for this case; 7787 * however, buggy beta non-Solaris NFSv4 servers at 7788 * interop testing events have allowed this behavior, 7789 * and it caused our client to panic due to a recursive 7790 * mutex_enter in fn_move. 7791 * 7792 * The tedious locking in fn_move could be changed to 7793 * deal with this case, and the client could avoid the 7794 * panic; however, the client would just confuse itself 7795 * later and misbehave. A better way to handle the broken 7796 * server is to detect this condition and return EINVAL 7797 * without ever sending the the bogus rename to the server. 7798 * We know the rename is invalid -- just fail it now. 7799 */ 7800 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) { 7801 VN_RELE(ovp); 7802 nfs_rw_exit(&odrp->r_rwlock); 7803 nfs_rw_exit(&ndrp->r_rwlock); 7804 if (nvp) { 7805 VN_RELE(nvp); 7806 } 7807 return (EINVAL); 7808 } 7809 7810 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN); 7811 7812 /* 7813 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is 7814 * possible for the filehandle to change due to the rename. 7815 * If neither of these bits is set, but FH4_VOL_MIGRATION is set, 7816 * the fh will not change because of the rename, but we still need 7817 * to update its rnode entry with the new name for 7818 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN 7819 * has no effect on these for now, but for future improvements, 7820 * we might want to use it too to simplify handling of files 7821 * that are open with that flag on. (XXX) 7822 */ 7823 mi = VTOMI4(odvp); 7824 if (NFS4_VOLATILE_FH(mi)) 7825 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr, 7826 &stat); 7827 else 7828 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr, 7829 &stat); 7830 7831 ASSERT(nfs4_consistent_type(odvp)); 7832 ASSERT(nfs4_consistent_type(ndvp)); 7833 ASSERT(nfs4_consistent_type(ovp)); 7834 7835 if (stat == NFS4ERR_FILE_OPEN && did_link) { 7836 do_link = 0; 7837 /* 7838 * Before the 'link_call' code, we did a nfs4_lookup 7839 * that puts a VN_HOLD on nvp. After the nfs4_link 7840 * call we call VN_RELE to match that hold. We need 7841 * to place an additional VN_HOLD here since we will 7842 * be hitting that VN_RELE again. 7843 */ 7844 VN_HOLD(nvp); 7845 7846 (void) nfs4_remove(ndvp, tmpname, cr); 7847 7848 /* Undo the unlinked file naming stuff we just did */ 7849 mutex_enter(&rp->r_statelock); 7850 if (rp->r_unldvp) { 7851 VN_RELE(ndvp); 7852 rp->r_unldvp = NULL; 7853 if (rp->r_unlcred != NULL) 7854 crfree(rp->r_unlcred); 7855 rp->r_unlcred = NULL; 7856 /* rp->r_unlanme points to tmpname */ 7857 if (rp->r_unlname) 7858 kmem_free(rp->r_unlname, MAXNAMELEN); 7859 rp->r_unlname = NULL; 7860 } 7861 mutex_exit(&rp->r_statelock); 7862 7863 if (nvp) { 7864 VN_RELE(nvp); 7865 } 7866 goto link_call; 7867 } 7868 7869 if (error) { 7870 VN_RELE(ovp); 7871 nfs_rw_exit(&odrp->r_rwlock); 7872 nfs_rw_exit(&ndrp->r_rwlock); 7873 if (nvp) { 7874 VN_RELE(nvp); 7875 } 7876 return (error); 7877 } 7878 7879 /* 7880 * when renaming directories to be a subdirectory of a 7881 * different parent, the dnlc entry for ".." will no 7882 * longer be valid, so it must be removed 7883 */ 7884 rp = VTOR4(ovp); 7885 if (ndvp != odvp) { 7886 if (ovp->v_type == VDIR) { 7887 dnlc_remove(ovp, ".."); 7888 if (rp->r_dir != NULL) 7889 nfs4_purge_rddir_cache(ovp); 7890 } 7891 } 7892 7893 /* 7894 * If we are renaming the unlinked file, update the 7895 * r_unldvp and r_unlname as needed. 7896 */ 7897 mutex_enter(&rp->r_statelock); 7898 if (rp->r_unldvp != NULL) { 7899 if (strcmp(rp->r_unlname, onm) == 0) { 7900 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 7901 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 7902 if (ndvp != rp->r_unldvp) { 7903 VN_RELE(rp->r_unldvp); 7904 rp->r_unldvp = ndvp; 7905 VN_HOLD(ndvp); 7906 } 7907 } 7908 } 7909 mutex_exit(&rp->r_statelock); 7910 7911 /* 7912 * Notify the rename vnevents to source vnode, and to the target 7913 * vnode if it already existed. 7914 */ 7915 if (error == 0) { 7916 vnode_t *tvp; 7917 rnode4_t *trp; 7918 /* 7919 * Notify the vnode. Each links is represented by 7920 * a different vnode, in nfsv4. 7921 */ 7922 if (nvp) { 7923 trp = VTOR4(nvp); 7924 tvp = nvp; 7925 if (IS_SHADOW(nvp, trp)) 7926 tvp = RTOV4(trp); 7927 vnevent_rename_dest(tvp, ndvp, nnm); 7928 } 7929 7930 /* 7931 * if the source and destination directory are not the 7932 * same notify the destination directory. 7933 */ 7934 if (VTOR4(odvp) != VTOR4(ndvp)) { 7935 trp = VTOR4(ndvp); 7936 tvp = ndvp; 7937 if (IS_SHADOW(ndvp, trp)) 7938 tvp = RTOV4(trp); 7939 vnevent_rename_dest_dir(tvp); 7940 } 7941 7942 trp = VTOR4(ovp); 7943 tvp = ovp; 7944 if (IS_SHADOW(ovp, trp)) 7945 tvp = RTOV4(trp); 7946 vnevent_rename_src(tvp, odvp, onm); 7947 } 7948 7949 if (nvp) { 7950 VN_RELE(nvp); 7951 } 7952 VN_RELE(ovp); 7953 7954 nfs_rw_exit(&odrp->r_rwlock); 7955 nfs_rw_exit(&ndrp->r_rwlock); 7956 7957 return (error); 7958 } 7959 7960 /* 7961 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4, 7962 * when it is known that the filehandle is persistent through rename. 7963 * 7964 * Rename requires that the current fh be the target directory and the 7965 * saved fh be the source directory. After the operation, the current fh 7966 * is unchanged. 7967 * The compound op structure for persistent fh rename is: 7968 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME 7969 * Rather than bother with the directory postop args, we'll simply 7970 * update that a change occured in the cache, so no post-op getattrs. 7971 */ 7972 static int 7973 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp, 7974 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 7975 { 7976 COMPOUND4args_clnt args; 7977 COMPOUND4res_clnt res, *resp = NULL; 7978 nfs_argop4 *argop; 7979 nfs_resop4 *resop; 7980 int doqueue, argoplist_size; 7981 mntinfo4_t *mi; 7982 rnode4_t *odrp = VTOR4(odvp); 7983 rnode4_t *ndrp = VTOR4(ndvp); 7984 RENAME4res *rn_res; 7985 bool_t needrecov; 7986 nfs4_recov_state_t recov_state; 7987 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 7988 dirattr_info_t dinfo, *dinfop; 7989 7990 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 7991 7992 recov_state.rs_flags = 0; 7993 recov_state.rs_num_retry_despite_err = 0; 7994 7995 /* 7996 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir 7997 * 7998 * If source/target are different dirs, then append putfh(src); getattr 7999 */ 8000 args.array_len = (odvp == ndvp) ? 5 : 7; 8001 argoplist_size = args.array_len * sizeof (nfs_argop4); 8002 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP); 8003 8004 recov_retry: 8005 *statp = NFS4_OK; 8006 8007 /* No need to Lookup the file, persistent fh */ 8008 args.ctag = TAG_RENAME; 8009 8010 mi = VTOMI4(odvp); 8011 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state); 8012 if (e.error) { 8013 kmem_free(argop, argoplist_size); 8014 return (e.error); 8015 } 8016 8017 /* 0: putfh source directory */ 8018 argop[0].argop = OP_CPUTFH; 8019 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8020 8021 /* 1: Save source fh to free up current for target */ 8022 argop[1].argop = OP_SAVEFH; 8023 8024 /* 2: putfh targetdir */ 8025 argop[2].argop = OP_CPUTFH; 8026 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8027 8028 /* 3: current_fh is targetdir, saved_fh is sourcedir */ 8029 argop[3].argop = OP_CRENAME; 8030 argop[3].nfs_argop4_u.opcrename.coldname = onm; 8031 argop[3].nfs_argop4_u.opcrename.cnewname = nnm; 8032 8033 /* 4: getattr (targetdir) */ 8034 argop[4].argop = OP_GETATTR; 8035 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8036 argop[4].nfs_argop4_u.opgetattr.mi = mi; 8037 8038 if (ndvp != odvp) { 8039 8040 /* 5: putfh (sourcedir) */ 8041 argop[5].argop = OP_CPUTFH; 8042 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8043 8044 /* 6: getattr (sourcedir) */ 8045 argop[6].argop = OP_GETATTR; 8046 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8047 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8048 } 8049 8050 dnlc_remove(odvp, onm); 8051 dnlc_remove(ndvp, nnm); 8052 8053 doqueue = 1; 8054 dinfo.di_time_call = gethrtime(); 8055 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8056 8057 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8058 if (e.error) { 8059 PURGE_ATTRCACHE4(odvp); 8060 PURGE_ATTRCACHE4(ndvp); 8061 } else { 8062 *statp = res.status; 8063 } 8064 8065 if (needrecov) { 8066 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8067 OP_RENAME, NULL) == FALSE) { 8068 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8069 if (!e.error) 8070 (void) xdr_free(xdr_COMPOUND4res_clnt, 8071 (caddr_t)&res); 8072 goto recov_retry; 8073 } 8074 } 8075 8076 if (!e.error) { 8077 resp = &res; 8078 /* 8079 * as long as OP_RENAME 8080 */ 8081 if (res.status != NFS4_OK && res.array_len <= 4) { 8082 e.error = geterrno4(res.status); 8083 PURGE_ATTRCACHE4(odvp); 8084 PURGE_ATTRCACHE4(ndvp); 8085 /* 8086 * System V defines rename to return EEXIST, not 8087 * ENOTEMPTY if the target directory is not empty. 8088 * Over the wire, the error is NFSERR_ENOTEMPTY 8089 * which geterrno4 maps to ENOTEMPTY. 8090 */ 8091 if (e.error == ENOTEMPTY) 8092 e.error = EEXIST; 8093 } else { 8094 8095 resop = &res.array[3]; /* rename res */ 8096 rn_res = &resop->nfs_resop4_u.oprename; 8097 8098 if (res.status == NFS4_OK) { 8099 /* 8100 * Update target attribute, readdir and dnlc 8101 * caches. 8102 */ 8103 dinfo.di_garp = 8104 &res.array[4].nfs_resop4_u.opgetattr.ga_res; 8105 dinfo.di_cred = cr; 8106 dinfop = &dinfo; 8107 } else 8108 dinfop = NULL; 8109 8110 nfs4_update_dircaches(&rn_res->target_cinfo, 8111 ndvp, NULL, NULL, dinfop); 8112 8113 /* 8114 * Update source attribute, readdir and dnlc caches 8115 * 8116 */ 8117 if (ndvp != odvp) { 8118 if (dinfop) 8119 dinfo.di_garp = 8120 &(res.array[6].nfs_resop4_u. 8121 opgetattr.ga_res); 8122 8123 nfs4_update_dircaches(&rn_res->source_cinfo, 8124 odvp, NULL, NULL, dinfop); 8125 } 8126 8127 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name, 8128 nnm); 8129 } 8130 } 8131 8132 if (resp) 8133 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8134 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov); 8135 kmem_free(argop, argoplist_size); 8136 8137 return (e.error); 8138 } 8139 8140 /* 8141 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when 8142 * it is possible for the filehandle to change due to the rename. 8143 * 8144 * The compound req in this case includes a post-rename lookup and getattr 8145 * to ensure that we have the correct fh and attributes for the object. 8146 * 8147 * Rename requires that the current fh be the target directory and the 8148 * saved fh be the source directory. After the operation, the current fh 8149 * is unchanged. 8150 * 8151 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can 8152 * update the filehandle for the renamed object. We also get the old 8153 * filehandle for historical reasons; this should be taken out sometime. 8154 * This results in a rather cumbersome compound... 8155 * 8156 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8157 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR 8158 * 8159 */ 8160 static int 8161 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp, 8162 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp) 8163 { 8164 COMPOUND4args_clnt args; 8165 COMPOUND4res_clnt res, *resp = NULL; 8166 int argoplist_size; 8167 nfs_argop4 *argop; 8168 nfs_resop4 *resop; 8169 int doqueue; 8170 mntinfo4_t *mi; 8171 rnode4_t *odrp = VTOR4(odvp); /* old directory */ 8172 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */ 8173 rnode4_t *orp = VTOR4(ovp); /* object being renamed */ 8174 RENAME4res *rn_res; 8175 GETFH4res *ngf_res; 8176 bool_t needrecov; 8177 nfs4_recov_state_t recov_state; 8178 hrtime_t t; 8179 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8180 dirattr_info_t dinfo, *dinfop = &dinfo; 8181 8182 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone); 8183 8184 recov_state.rs_flags = 0; 8185 recov_state.rs_num_retry_despite_err = 0; 8186 8187 recov_retry: 8188 *statp = NFS4_OK; 8189 8190 /* 8191 * There is a window between the RPC and updating the path and 8192 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery 8193 * code, so that it doesn't try to use the old path during that 8194 * window. 8195 */ 8196 mutex_enter(&orp->r_statelock); 8197 while (orp->r_flags & R4RECEXPFH) { 8198 klwp_t *lwp = ttolwp(curthread); 8199 8200 if (lwp != NULL) 8201 lwp->lwp_nostop++; 8202 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) { 8203 mutex_exit(&orp->r_statelock); 8204 if (lwp != NULL) 8205 lwp->lwp_nostop--; 8206 return (EINTR); 8207 } 8208 if (lwp != NULL) 8209 lwp->lwp_nostop--; 8210 } 8211 orp->r_flags |= R4RECEXPFH; 8212 mutex_exit(&orp->r_statelock); 8213 8214 mi = VTOMI4(odvp); 8215 8216 args.ctag = TAG_RENAME_VFH; 8217 args.array_len = (odvp == ndvp) ? 10 : 12; 8218 argoplist_size = args.array_len * sizeof (nfs_argop4); 8219 argop = kmem_alloc(argoplist_size, KM_SLEEP); 8220 8221 /* 8222 * Rename ops: 8223 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old), 8224 * PUTFH(targetdir), RENAME, GETATTR(targetdir) 8225 * LOOKUP(trgt), GETFH(new), GETATTR, 8226 * 8227 * if (odvp != ndvp) 8228 * add putfh(sourcedir), getattr(sourcedir) } 8229 */ 8230 args.array = argop; 8231 8232 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8233 &recov_state, NULL); 8234 if (e.error) { 8235 kmem_free(argop, argoplist_size); 8236 mutex_enter(&orp->r_statelock); 8237 orp->r_flags &= ~R4RECEXPFH; 8238 cv_broadcast(&orp->r_cv); 8239 mutex_exit(&orp->r_statelock); 8240 return (e.error); 8241 } 8242 8243 /* 0: putfh source directory */ 8244 argop[0].argop = OP_CPUTFH; 8245 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh; 8246 8247 /* 1: Save source fh to free up current for target */ 8248 argop[1].argop = OP_SAVEFH; 8249 8250 /* 2: Lookup pre-rename fh of renamed object */ 8251 argop[2].argop = OP_CLOOKUP; 8252 argop[2].nfs_argop4_u.opclookup.cname = onm; 8253 8254 /* 3: getfh fh of renamed object (before rename) */ 8255 argop[3].argop = OP_GETFH; 8256 8257 /* 4: putfh targetdir */ 8258 argop[4].argop = OP_CPUTFH; 8259 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8260 8261 /* 5: current_fh is targetdir, saved_fh is sourcedir */ 8262 argop[5].argop = OP_CRENAME; 8263 argop[5].nfs_argop4_u.opcrename.coldname = onm; 8264 argop[5].nfs_argop4_u.opcrename.cnewname = nnm; 8265 8266 /* 6: getattr of target dir (post op attrs) */ 8267 argop[6].argop = OP_GETATTR; 8268 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8269 argop[6].nfs_argop4_u.opgetattr.mi = mi; 8270 8271 /* 7: Lookup post-rename fh of renamed object */ 8272 argop[7].argop = OP_CLOOKUP; 8273 argop[7].nfs_argop4_u.opclookup.cname = nnm; 8274 8275 /* 8: getfh fh of renamed object (after rename) */ 8276 argop[8].argop = OP_GETFH; 8277 8278 /* 9: getattr of renamed object */ 8279 argop[9].argop = OP_GETATTR; 8280 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8281 argop[9].nfs_argop4_u.opgetattr.mi = mi; 8282 8283 /* 8284 * If source/target dirs are different, then get new post-op 8285 * attrs for source dir also. 8286 */ 8287 if (ndvp != odvp) { 8288 /* 10: putfh (sourcedir) */ 8289 argop[10].argop = OP_CPUTFH; 8290 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh; 8291 8292 /* 11: getattr (sourcedir) */ 8293 argop[11].argop = OP_GETATTR; 8294 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8295 argop[11].nfs_argop4_u.opgetattr.mi = mi; 8296 } 8297 8298 dnlc_remove(odvp, onm); 8299 dnlc_remove(ndvp, nnm); 8300 8301 doqueue = 1; 8302 t = gethrtime(); 8303 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8304 8305 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8306 if (e.error) { 8307 PURGE_ATTRCACHE4(odvp); 8308 PURGE_ATTRCACHE4(ndvp); 8309 if (!needrecov) { 8310 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8311 &recov_state, needrecov); 8312 goto out; 8313 } 8314 } else { 8315 *statp = res.status; 8316 } 8317 8318 if (needrecov) { 8319 bool_t abort; 8320 8321 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL, 8322 OP_RENAME, NULL); 8323 if (abort == FALSE) { 8324 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8325 &recov_state, needrecov); 8326 kmem_free(argop, argoplist_size); 8327 if (!e.error) 8328 (void) xdr_free(xdr_COMPOUND4res_clnt, 8329 (caddr_t)&res); 8330 mutex_enter(&orp->r_statelock); 8331 orp->r_flags &= ~R4RECEXPFH; 8332 cv_broadcast(&orp->r_cv); 8333 mutex_exit(&orp->r_statelock); 8334 goto recov_retry; 8335 } else { 8336 if (e.error != 0) { 8337 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, 8338 &recov_state, needrecov); 8339 goto out; 8340 } 8341 /* fall through for res.status case */ 8342 } 8343 } 8344 8345 resp = &res; 8346 /* 8347 * If OP_RENAME (or any prev op) failed, then return an error. 8348 * OP_RENAME is index 5, so if array len <= 6 we return an error. 8349 */ 8350 if ((res.status != NFS4_OK) && (res.array_len <= 6)) { 8351 /* 8352 * Error in an op other than last Getattr 8353 */ 8354 e.error = geterrno4(res.status); 8355 PURGE_ATTRCACHE4(odvp); 8356 PURGE_ATTRCACHE4(ndvp); 8357 /* 8358 * System V defines rename to return EEXIST, not 8359 * ENOTEMPTY if the target directory is not empty. 8360 * Over the wire, the error is NFSERR_ENOTEMPTY 8361 * which geterrno4 maps to ENOTEMPTY. 8362 */ 8363 if (e.error == ENOTEMPTY) 8364 e.error = EEXIST; 8365 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, 8366 needrecov); 8367 goto out; 8368 } 8369 8370 /* rename results */ 8371 rn_res = &res.array[5].nfs_resop4_u.oprename; 8372 8373 if (res.status == NFS4_OK) { 8374 /* Update target attribute, readdir and dnlc caches */ 8375 dinfo.di_garp = 8376 &res.array[6].nfs_resop4_u.opgetattr.ga_res; 8377 dinfo.di_cred = cr; 8378 dinfo.di_time_call = t; 8379 } else 8380 dinfop = NULL; 8381 8382 /* Update source cache attribute, readdir and dnlc caches */ 8383 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop); 8384 8385 /* Update source cache attribute, readdir and dnlc caches */ 8386 if (ndvp != odvp) { 8387 8388 /* 8389 * If dinfop is non-NULL, then compound succeded, so 8390 * set di_garp to attrs for source dir. dinfop is only 8391 * set to NULL when compound fails. 8392 */ 8393 if (dinfop) 8394 dinfo.di_garp = 8395 &res.array[11].nfs_resop4_u.opgetattr.ga_res; 8396 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL, 8397 dinfop); 8398 } 8399 8400 /* 8401 * Update the rnode with the new component name and args, 8402 * and if the file handle changed, also update it with the new fh. 8403 * This is only necessary if the target object has an rnode 8404 * entry and there is no need to create one for it. 8405 */ 8406 resop = &res.array[8]; /* getfh new res */ 8407 ngf_res = &resop->nfs_resop4_u.opgetfh; 8408 8409 /* 8410 * Update the path and filehandle for the renamed object. 8411 */ 8412 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm); 8413 8414 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov); 8415 8416 if (res.status == NFS4_OK) { 8417 resop++; /* getattr res */ 8418 e.error = nfs4_update_attrcache(res.status, 8419 &resop->nfs_resop4_u.opgetattr.ga_res, 8420 t, ovp, cr); 8421 } 8422 8423 out: 8424 kmem_free(argop, argoplist_size); 8425 if (resp) 8426 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8427 mutex_enter(&orp->r_statelock); 8428 orp->r_flags &= ~R4RECEXPFH; 8429 cv_broadcast(&orp->r_cv); 8430 mutex_exit(&orp->r_statelock); 8431 8432 return (e.error); 8433 } 8434 8435 static int 8436 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr) 8437 { 8438 int error; 8439 vnode_t *vp; 8440 8441 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8442 return (EPERM); 8443 /* 8444 * As ".." has special meaning and rather than send a mkdir 8445 * over the wire to just let the server freak out, we just 8446 * short circuit it here and return EEXIST 8447 */ 8448 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8449 return (EEXIST); 8450 8451 /* 8452 * Decision to get the right gid and setgid bit of the 8453 * new directory is now made in call_nfs4_create_req. 8454 */ 8455 va->va_mask |= AT_MODE; 8456 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR); 8457 if (error) 8458 return (error); 8459 8460 *vpp = vp; 8461 return (0); 8462 } 8463 8464 8465 /* 8466 * rmdir is using the same remove v4 op as does remove. 8467 * Remove requires that the current fh be the target directory. 8468 * After the operation, the current fh is unchanged. 8469 * The compound op structure is: 8470 * PUTFH(targetdir), REMOVE 8471 */ 8472 static int 8473 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr) 8474 { 8475 int need_end_op = FALSE; 8476 COMPOUND4args_clnt args; 8477 COMPOUND4res_clnt res, *resp = NULL; 8478 REMOVE4res *rm_res; 8479 nfs_argop4 argop[3]; 8480 nfs_resop4 *resop; 8481 vnode_t *vp; 8482 int doqueue; 8483 mntinfo4_t *mi; 8484 rnode4_t *drp; 8485 bool_t needrecov = FALSE; 8486 nfs4_recov_state_t recov_state; 8487 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8488 dirattr_info_t dinfo, *dinfop; 8489 8490 if (nfs_zone() != VTOMI4(dvp)->mi_zone) 8491 return (EPERM); 8492 /* 8493 * As ".." has special meaning and rather than send a rmdir 8494 * over the wire to just let the server freak out, we just 8495 * short circuit it here and return EEXIST 8496 */ 8497 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') 8498 return (EEXIST); 8499 8500 drp = VTOR4(dvp); 8501 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) 8502 return (EINTR); 8503 8504 /* 8505 * Attempt to prevent a rmdir(".") from succeeding. 8506 */ 8507 e.error = nfs4lookup(dvp, nm, &vp, cr, 0); 8508 if (e.error) { 8509 nfs_rw_exit(&drp->r_rwlock); 8510 return (e.error); 8511 } 8512 if (vp == cdir) { 8513 VN_RELE(vp); 8514 nfs_rw_exit(&drp->r_rwlock); 8515 return (EINVAL); 8516 } 8517 8518 /* 8519 * Since nfsv4 remove op works on both files and directories, 8520 * check that the removed object is indeed a directory. 8521 */ 8522 if (vp->v_type != VDIR) { 8523 VN_RELE(vp); 8524 nfs_rw_exit(&drp->r_rwlock); 8525 return (ENOTDIR); 8526 } 8527 8528 /* 8529 * First just remove the entry from the name cache, as it 8530 * is most likely an entry for this vp. 8531 */ 8532 dnlc_remove(dvp, nm); 8533 8534 /* 8535 * If there vnode reference count is greater than one, then 8536 * there may be additional references in the DNLC which will 8537 * need to be purged. First, trying removing the entry for 8538 * the parent directory and see if that removes the additional 8539 * reference(s). If that doesn't do it, then use dnlc_purge_vp 8540 * to completely remove any references to the directory which 8541 * might still exist in the DNLC. 8542 */ 8543 if (vp->v_count > 1) { 8544 dnlc_remove(vp, ".."); 8545 if (vp->v_count > 1) 8546 dnlc_purge_vp(vp); 8547 } 8548 8549 mi = VTOMI4(dvp); 8550 recov_state.rs_flags = 0; 8551 recov_state.rs_num_retry_despite_err = 0; 8552 8553 recov_retry: 8554 args.ctag = TAG_RMDIR; 8555 8556 /* 8557 * Rmdir ops: putfh dir; remove 8558 */ 8559 args.array_len = 3; 8560 args.array = argop; 8561 8562 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state); 8563 if (e.error) { 8564 nfs_rw_exit(&drp->r_rwlock); 8565 return (e.error); 8566 } 8567 need_end_op = TRUE; 8568 8569 /* putfh directory */ 8570 argop[0].argop = OP_CPUTFH; 8571 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh; 8572 8573 /* remove */ 8574 argop[1].argop = OP_CREMOVE; 8575 argop[1].nfs_argop4_u.opcremove.ctarget = nm; 8576 8577 /* getattr (postop attrs for dir that contained removed dir) */ 8578 argop[2].argop = OP_GETATTR; 8579 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 8580 argop[2].nfs_argop4_u.opgetattr.mi = mi; 8581 8582 dinfo.di_time_call = gethrtime(); 8583 doqueue = 1; 8584 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 8585 8586 PURGE_ATTRCACHE4(vp); 8587 8588 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 8589 if (e.error) { 8590 PURGE_ATTRCACHE4(dvp); 8591 } 8592 8593 if (needrecov) { 8594 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL, 8595 NULL, OP_REMOVE, NULL) == FALSE) { 8596 if (!e.error) 8597 (void) xdr_free(xdr_COMPOUND4res_clnt, 8598 (caddr_t)&res); 8599 8600 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, 8601 needrecov); 8602 need_end_op = FALSE; 8603 goto recov_retry; 8604 } 8605 } 8606 8607 if (!e.error) { 8608 resp = &res; 8609 8610 /* 8611 * Only return error if first 2 ops (OP_REMOVE or earlier) 8612 * failed. 8613 */ 8614 if (res.status != NFS4_OK && res.array_len <= 2) { 8615 e.error = geterrno4(res.status); 8616 PURGE_ATTRCACHE4(dvp); 8617 nfs4_end_op(VTOMI4(dvp), dvp, NULL, 8618 &recov_state, needrecov); 8619 need_end_op = FALSE; 8620 nfs4_purge_stale_fh(e.error, dvp, cr); 8621 /* 8622 * System V defines rmdir to return EEXIST, not 8623 * ENOTEMPTY if the directory is not empty. Over 8624 * the wire, the error is NFSERR_ENOTEMPTY which 8625 * geterrno4 maps to ENOTEMPTY. 8626 */ 8627 if (e.error == ENOTEMPTY) 8628 e.error = EEXIST; 8629 } else { 8630 resop = &res.array[1]; /* remove res */ 8631 rm_res = &resop->nfs_resop4_u.opremove; 8632 8633 if (res.status == NFS4_OK) { 8634 resop = &res.array[2]; /* dir attrs */ 8635 dinfo.di_garp = 8636 &resop->nfs_resop4_u.opgetattr.ga_res; 8637 dinfo.di_cred = cr; 8638 dinfop = &dinfo; 8639 } else 8640 dinfop = NULL; 8641 8642 /* Update dir attribute, readdir and dnlc caches */ 8643 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL, 8644 dinfop); 8645 8646 /* destroy rddir cache for dir that was removed */ 8647 if (VTOR4(vp)->r_dir != NULL) 8648 nfs4_purge_rddir_cache(vp); 8649 } 8650 } 8651 8652 if (need_end_op) 8653 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov); 8654 8655 nfs_rw_exit(&drp->r_rwlock); 8656 8657 if (resp) 8658 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 8659 8660 if (e.error == 0) { 8661 vnode_t *tvp; 8662 rnode4_t *trp; 8663 trp = VTOR4(vp); 8664 tvp = vp; 8665 if (IS_SHADOW(vp, trp)) 8666 tvp = RTOV4(trp); 8667 vnevent_rmdir(tvp, dvp, nm); 8668 } 8669 8670 VN_RELE(vp); 8671 8672 return (e.error); 8673 } 8674 8675 static int 8676 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr) 8677 { 8678 int error; 8679 vnode_t *vp; 8680 rnode4_t *rp; 8681 char *contents; 8682 mntinfo4_t *mi = VTOMI4(dvp); 8683 8684 if (nfs_zone() != mi->mi_zone) 8685 return (EPERM); 8686 if (!(mi->mi_flags & MI4_SYMLINK)) 8687 return (EOPNOTSUPP); 8688 8689 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK); 8690 if (error) 8691 return (error); 8692 8693 ASSERT(nfs4_consistent_type(vp)); 8694 rp = VTOR4(vp); 8695 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) { 8696 8697 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8698 8699 if (contents != NULL) { 8700 mutex_enter(&rp->r_statelock); 8701 if (rp->r_symlink.contents == NULL) { 8702 rp->r_symlink.len = strlen(tnm); 8703 bcopy(tnm, contents, rp->r_symlink.len); 8704 rp->r_symlink.contents = contents; 8705 rp->r_symlink.size = MAXPATHLEN; 8706 mutex_exit(&rp->r_statelock); 8707 } else { 8708 mutex_exit(&rp->r_statelock); 8709 kmem_free((void *)contents, MAXPATHLEN); 8710 } 8711 } 8712 } 8713 VN_RELE(vp); 8714 8715 return (error); 8716 } 8717 8718 8719 /* 8720 * Read directory entries. 8721 * There are some weird things to look out for here. The uio_loffset 8722 * field is either 0 or it is the offset returned from a previous 8723 * readdir. It is an opaque value used by the server to find the 8724 * correct directory block to read. The count field is the number 8725 * of blocks to read on the server. This is advisory only, the server 8726 * may return only one block's worth of entries. Entries may be compressed 8727 * on the server. 8728 */ 8729 static int 8730 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp) 8731 { 8732 int error; 8733 uint_t count; 8734 rnode4_t *rp; 8735 rddir4_cache *rdc; 8736 rddir4_cache *rrdc; 8737 8738 if (nfs_zone() != VTOMI4(vp)->mi_zone) 8739 return (EIO); 8740 rp = VTOR4(vp); 8741 8742 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 8743 8744 /* 8745 * Make sure that the directory cache is valid. 8746 */ 8747 if (rp->r_dir != NULL) { 8748 if (nfs_disable_rddir_cache != 0) { 8749 /* 8750 * Setting nfs_disable_rddir_cache in /etc/system 8751 * allows interoperability with servers that do not 8752 * properly update the attributes of directories. 8753 * Any cached information gets purged before an 8754 * access is made to it. 8755 */ 8756 nfs4_purge_rddir_cache(vp); 8757 } 8758 8759 error = nfs4_validate_caches(vp, cr); 8760 if (error) 8761 return (error); 8762 } 8763 8764 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE); 8765 8766 /* 8767 * Short circuit last readdir which always returns 0 bytes. 8768 * This can be done after the directory has been read through 8769 * completely at least once. This will set r_direof which 8770 * can be used to find the value of the last cookie. 8771 */ 8772 mutex_enter(&rp->r_statelock); 8773 if (rp->r_direof != NULL && 8774 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) { 8775 mutex_exit(&rp->r_statelock); 8776 #ifdef DEBUG 8777 nfs4_readdir_cache_shorts++; 8778 #endif 8779 if (eofp) 8780 *eofp = 1; 8781 return (0); 8782 } 8783 8784 /* 8785 * Look for a cache entry. Cache entries are identified 8786 * by the NFS cookie value and the byte count requested. 8787 */ 8788 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count); 8789 8790 /* 8791 * If rdc is NULL then the lookup resulted in an unrecoverable error. 8792 */ 8793 if (rdc == NULL) { 8794 mutex_exit(&rp->r_statelock); 8795 return (EINTR); 8796 } 8797 8798 /* 8799 * Check to see if we need to fill this entry in. 8800 */ 8801 if (rdc->flags & RDDIRREQ) { 8802 rdc->flags &= ~RDDIRREQ; 8803 rdc->flags |= RDDIR; 8804 mutex_exit(&rp->r_statelock); 8805 8806 /* 8807 * Do the readdir. 8808 */ 8809 nfs4readdir(vp, rdc, cr); 8810 8811 /* 8812 * Reaquire the lock, so that we can continue 8813 */ 8814 mutex_enter(&rp->r_statelock); 8815 /* 8816 * The entry is now complete 8817 */ 8818 rdc->flags &= ~RDDIR; 8819 } 8820 8821 ASSERT(!(rdc->flags & RDDIR)); 8822 8823 /* 8824 * If an error occurred while attempting 8825 * to fill the cache entry, mark the entry invalid and 8826 * just return the error. 8827 */ 8828 if (rdc->error) { 8829 error = rdc->error; 8830 rdc->flags |= RDDIRREQ; 8831 rddir4_cache_rele(rp, rdc); 8832 mutex_exit(&rp->r_statelock); 8833 return (error); 8834 } 8835 8836 /* 8837 * The cache entry is complete and good, 8838 * copyout the dirent structs to the calling 8839 * thread. 8840 */ 8841 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop); 8842 8843 /* 8844 * If no error occurred during the copyout, 8845 * update the offset in the uio struct to 8846 * contain the value of the next NFS 4 cookie 8847 * and set the eof value appropriately. 8848 */ 8849 if (!error) { 8850 uiop->uio_loffset = rdc->nfs4_ncookie; 8851 if (eofp) 8852 *eofp = rdc->eof; 8853 } 8854 8855 /* 8856 * Decide whether to do readahead. Don't if we 8857 * have already read to the end of directory. 8858 */ 8859 if (rdc->eof) { 8860 /* 8861 * Make the entry the direof only if it is cached 8862 */ 8863 if (rdc->flags & RDDIRCACHED) 8864 rp->r_direof = rdc; 8865 rddir4_cache_rele(rp, rdc); 8866 mutex_exit(&rp->r_statelock); 8867 return (error); 8868 } 8869 8870 /* Determine if a readdir readahead should be done */ 8871 if (!(rp->r_flags & R4LOOKUP)) { 8872 rddir4_cache_rele(rp, rdc); 8873 mutex_exit(&rp->r_statelock); 8874 return (error); 8875 } 8876 8877 /* 8878 * Now look for a readahead entry. 8879 * 8880 * Check to see whether we found an entry for the readahead. 8881 * If so, we don't need to do anything further, so free the new 8882 * entry if one was allocated. Otherwise, allocate a new entry, add 8883 * it to the cache, and then initiate an asynchronous readdir 8884 * operation to fill it. 8885 */ 8886 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count); 8887 8888 /* 8889 * A readdir cache entry could not be obtained for the readahead. In 8890 * this case we skip the readahead and return. 8891 */ 8892 if (rrdc == NULL) { 8893 rddir4_cache_rele(rp, rdc); 8894 mutex_exit(&rp->r_statelock); 8895 return (error); 8896 } 8897 8898 /* 8899 * Check to see if we need to fill this entry in. 8900 */ 8901 if (rrdc->flags & RDDIRREQ) { 8902 rrdc->flags &= ~RDDIRREQ; 8903 rrdc->flags |= RDDIR; 8904 rddir4_cache_rele(rp, rdc); 8905 mutex_exit(&rp->r_statelock); 8906 #ifdef DEBUG 8907 nfs4_readdir_readahead++; 8908 #endif 8909 /* 8910 * Do the readdir. 8911 */ 8912 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir); 8913 return (error); 8914 } 8915 8916 rddir4_cache_rele(rp, rrdc); 8917 rddir4_cache_rele(rp, rdc); 8918 mutex_exit(&rp->r_statelock); 8919 return (error); 8920 } 8921 8922 static int 8923 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8924 { 8925 int error; 8926 rnode4_t *rp; 8927 8928 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 8929 8930 rp = VTOR4(vp); 8931 8932 /* 8933 * Obtain the readdir results for the caller. 8934 */ 8935 nfs4readdir(vp, rdc, cr); 8936 8937 mutex_enter(&rp->r_statelock); 8938 /* 8939 * The entry is now complete 8940 */ 8941 rdc->flags &= ~RDDIR; 8942 8943 error = rdc->error; 8944 if (error) 8945 rdc->flags |= RDDIRREQ; 8946 rddir4_cache_rele(rp, rdc); 8947 mutex_exit(&rp->r_statelock); 8948 8949 return (error); 8950 } 8951 8952 /* 8953 * Read directory entries. 8954 * There are some weird things to look out for here. The uio_loffset 8955 * field is either 0 or it is the offset returned from a previous 8956 * readdir. It is an opaque value used by the server to find the 8957 * correct directory block to read. The count field is the number 8958 * of blocks to read on the server. This is advisory only, the server 8959 * may return only one block's worth of entries. Entries may be compressed 8960 * on the server. 8961 * 8962 * Generates the following compound request: 8963 * 1. If readdir offset is zero and no dnlc entry for parent exists, 8964 * must include a Lookupp as well. In this case, send: 8965 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr } 8966 * 2. Otherwise just do: { Putfh <fh>; Readdir } 8967 * 8968 * Get complete attributes and filehandles for entries if this is the 8969 * first read of the directory. Otherwise, just get fileid's. 8970 */ 8971 static void 8972 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr) 8973 { 8974 COMPOUND4args_clnt args; 8975 COMPOUND4res_clnt res; 8976 READDIR4args *rargs; 8977 READDIR4res_clnt *rd_res; 8978 bitmap4 rd_bitsval; 8979 nfs_argop4 argop[5]; 8980 nfs_resop4 *resop; 8981 rnode4_t *rp = VTOR4(vp); 8982 mntinfo4_t *mi = VTOMI4(vp); 8983 int doqueue; 8984 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */ 8985 vnode_t *dvp; 8986 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie; 8987 int num_ops, res_opcnt; 8988 bool_t needrecov = FALSE; 8989 nfs4_recov_state_t recov_state; 8990 hrtime_t t; 8991 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 8992 8993 ASSERT(nfs_zone() == mi->mi_zone); 8994 ASSERT(rdc->flags & RDDIR); 8995 ASSERT(rdc->entries == NULL); 8996 8997 /* 8998 * If rp were a stub, it should have triggered and caused 8999 * a mount for us to get this far. 9000 */ 9001 ASSERT(!RP_ISSTUB(rp)); 9002 9003 num_ops = 2; 9004 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) { 9005 /* 9006 * Since nfsv4 readdir may not return entries for "." and "..", 9007 * the client must recreate them: 9008 * To find the correct nodeid, do the following: 9009 * For current node, get nodeid from dnlc. 9010 * - if current node is rootvp, set pnodeid to nodeid. 9011 * - else if parent is in the dnlc, get its nodeid from there. 9012 * - else add LOOKUPP+GETATTR to compound. 9013 */ 9014 nodeid = rp->r_attr.va_nodeid; 9015 if (vp->v_flag & VROOT) { 9016 pnodeid = nodeid; /* root of mount point */ 9017 } else { 9018 dvp = dnlc_lookup(vp, ".."); 9019 if (dvp != NULL && dvp != DNLC_NO_VNODE) { 9020 /* parent in dnlc cache - no need for otw */ 9021 pnodeid = VTOR4(dvp)->r_attr.va_nodeid; 9022 } else { 9023 /* 9024 * parent not in dnlc cache, 9025 * do lookupp to get its id 9026 */ 9027 num_ops = 5; 9028 pnodeid = 0; /* set later by getattr parent */ 9029 } 9030 if (dvp) 9031 VN_RELE(dvp); 9032 } 9033 } 9034 recov_state.rs_flags = 0; 9035 recov_state.rs_num_retry_despite_err = 0; 9036 9037 /* Save the original mount point security flavor */ 9038 (void) save_mnt_secinfo(mi->mi_curr_serv); 9039 9040 recov_retry: 9041 args.ctag = TAG_READDIR; 9042 9043 args.array = argop; 9044 args.array_len = num_ops; 9045 9046 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9047 &recov_state, NULL)) { 9048 /* 9049 * If readdir a node that is a stub for a crossed mount point, 9050 * keep the original secinfo flavor for the current file 9051 * system, not the crossed one. 9052 */ 9053 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9054 rdc->error = e.error; 9055 return; 9056 } 9057 9058 /* 9059 * Determine which attrs to request for dirents. This code 9060 * must be protected by nfs4_start/end_fop because of r_server 9061 * (which will change during failover recovery). 9062 * 9063 */ 9064 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) { 9065 /* 9066 * Get all vattr attrs plus filehandle and rdattr_error 9067 */ 9068 rd_bitsval = NFS4_VATTR_MASK | 9069 FATTR4_RDATTR_ERROR_MASK | 9070 FATTR4_FILEHANDLE_MASK; 9071 9072 if (rp->r_flags & R4READDIRWATTR) { 9073 mutex_enter(&rp->r_statelock); 9074 rp->r_flags &= ~R4READDIRWATTR; 9075 mutex_exit(&rp->r_statelock); 9076 } 9077 } else { 9078 servinfo4_t *svp = rp->r_server; 9079 9080 /* 9081 * Already read directory. Use readdir with 9082 * no attrs (except for mounted_on_fileid) for updates. 9083 */ 9084 rd_bitsval = FATTR4_RDATTR_ERROR_MASK; 9085 9086 /* 9087 * request mounted on fileid if supported, else request 9088 * fileid. maybe we should verify that fileid is supported 9089 * and request something else if not. 9090 */ 9091 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 9092 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK) 9093 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK; 9094 nfs_rw_exit(&svp->sv_lock); 9095 } 9096 9097 /* putfh directory fh */ 9098 argop[0].argop = OP_CPUTFH; 9099 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 9100 9101 argop[1].argop = OP_READDIR; 9102 rargs = &argop[1].nfs_argop4_u.opreaddir; 9103 /* 9104 * 1 and 2 are reserved for client "." and ".." entry offset. 9105 * cookie 0 should be used over-the-wire to start reading at 9106 * the beginning of the directory excluding "." and "..". 9107 */ 9108 if (rdc->nfs4_cookie == 0 || 9109 rdc->nfs4_cookie == 1 || 9110 rdc->nfs4_cookie == 2) { 9111 rargs->cookie = (nfs_cookie4)0; 9112 rargs->cookieverf = 0; 9113 } else { 9114 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie; 9115 mutex_enter(&rp->r_statelock); 9116 rargs->cookieverf = rp->r_cookieverf4; 9117 mutex_exit(&rp->r_statelock); 9118 } 9119 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize); 9120 rargs->maxcount = mi->mi_tsize; 9121 rargs->attr_request = rd_bitsval; 9122 rargs->rdc = rdc; 9123 rargs->dvp = vp; 9124 rargs->mi = mi; 9125 rargs->cr = cr; 9126 9127 9128 /* 9129 * If count < than the minimum required, we return no entries 9130 * and fail with EINVAL 9131 */ 9132 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) { 9133 rdc->error = EINVAL; 9134 goto out; 9135 } 9136 9137 if (args.array_len == 5) { 9138 /* 9139 * Add lookupp and getattr for parent nodeid. 9140 */ 9141 argop[2].argop = OP_LOOKUPP; 9142 9143 argop[3].argop = OP_GETFH; 9144 9145 /* getattr parent */ 9146 argop[4].argop = OP_GETATTR; 9147 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK; 9148 argop[4].nfs_argop4_u.opgetattr.mi = mi; 9149 } 9150 9151 doqueue = 1; 9152 9153 if (mi->mi_io_kstats) { 9154 mutex_enter(&mi->mi_lock); 9155 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 9156 mutex_exit(&mi->mi_lock); 9157 } 9158 9159 /* capture the time of this call */ 9160 rargs->t = t = gethrtime(); 9161 9162 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e); 9163 9164 if (mi->mi_io_kstats) { 9165 mutex_enter(&mi->mi_lock); 9166 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 9167 mutex_exit(&mi->mi_lock); 9168 } 9169 9170 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 9171 9172 /* 9173 * If RPC error occurred and it isn't an error that 9174 * triggers recovery, then go ahead and fail now. 9175 */ 9176 if (e.error != 0 && !needrecov) { 9177 rdc->error = e.error; 9178 goto out; 9179 } 9180 9181 if (needrecov) { 9182 bool_t abort; 9183 9184 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 9185 "nfs4readdir: initiating recovery.\n")); 9186 9187 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 9188 NULL, OP_READDIR, NULL); 9189 if (abort == FALSE) { 9190 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9191 &recov_state, needrecov); 9192 if (!e.error) 9193 (void) xdr_free(xdr_COMPOUND4res_clnt, 9194 (caddr_t)&res); 9195 if (rdc->entries != NULL) { 9196 kmem_free(rdc->entries, rdc->entlen); 9197 rdc->entries = NULL; 9198 } 9199 goto recov_retry; 9200 } 9201 9202 if (e.error != 0) { 9203 rdc->error = e.error; 9204 goto out; 9205 } 9206 9207 /* fall through for res.status case */ 9208 } 9209 9210 res_opcnt = res.array_len; 9211 9212 /* 9213 * If compound failed first 2 ops (PUTFH+READDIR), then return 9214 * failure here. Subsequent ops are for filling out dot-dot 9215 * dirent, and if they fail, we still want to give the caller 9216 * the dirents returned by (the successful) READDIR op, so we need 9217 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR). 9218 * 9219 * One example where PUTFH+READDIR ops would succeed but 9220 * LOOKUPP+GETATTR would fail would be a dir that has r perm 9221 * but lacks x. In this case, a POSIX server's VOP_READDIR 9222 * would succeed; however, VOP_LOOKUP(..) would fail since no 9223 * x perm. We need to come up with a non-vendor-specific way 9224 * for a POSIX server to return d_ino from dotdot's dirent if 9225 * client only requests mounted_on_fileid, and just say the 9226 * LOOKUPP succeeded and fill out the GETATTR. However, if 9227 * client requested any mandatory attrs, server would be required 9228 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR 9229 * for dotdot. 9230 */ 9231 9232 if (res.status) { 9233 if (res_opcnt <= 2) { 9234 e.error = geterrno4(res.status); 9235 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR, 9236 &recov_state, needrecov); 9237 nfs4_purge_stale_fh(e.error, vp, cr); 9238 rdc->error = e.error; 9239 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9240 if (rdc->entries != NULL) { 9241 kmem_free(rdc->entries, rdc->entlen); 9242 rdc->entries = NULL; 9243 } 9244 /* 9245 * If readdir a node that is a stub for a 9246 * crossed mount point, keep the original 9247 * secinfo flavor for the current file system, 9248 * not the crossed one. 9249 */ 9250 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9251 return; 9252 } 9253 } 9254 9255 resop = &res.array[1]; /* readdir res */ 9256 rd_res = &resop->nfs_resop4_u.opreaddirclnt; 9257 9258 mutex_enter(&rp->r_statelock); 9259 rp->r_cookieverf4 = rd_res->cookieverf; 9260 mutex_exit(&rp->r_statelock); 9261 9262 /* 9263 * For "." and ".." entries 9264 * e.g. 9265 * seek(cookie=0) -> "." entry with d_off = 1 9266 * seek(cookie=1) -> ".." entry with d_off = 2 9267 */ 9268 if (cookie == (nfs_cookie4) 0) { 9269 if (rd_res->dotp) 9270 rd_res->dotp->d_ino = nodeid; 9271 if (rd_res->dotdotp) 9272 rd_res->dotdotp->d_ino = pnodeid; 9273 } 9274 if (cookie == (nfs_cookie4) 1) { 9275 if (rd_res->dotdotp) 9276 rd_res->dotdotp->d_ino = pnodeid; 9277 } 9278 9279 9280 /* LOOKUPP+GETATTR attemped */ 9281 if (args.array_len == 5 && rd_res->dotdotp) { 9282 if (res.status == NFS4_OK && res_opcnt == 5) { 9283 nfs_fh4 *fhp; 9284 nfs4_sharedfh_t *sfhp; 9285 vnode_t *pvp; 9286 nfs4_ga_res_t *garp; 9287 9288 resop++; /* lookupp */ 9289 resop++; /* getfh */ 9290 fhp = &resop->nfs_resop4_u.opgetfh.object; 9291 9292 resop++; /* getattr of parent */ 9293 9294 /* 9295 * First, take care of finishing the 9296 * readdir results. 9297 */ 9298 garp = &resop->nfs_resop4_u.opgetattr.ga_res; 9299 /* 9300 * The d_ino of .. must be the inode number 9301 * of the mounted filesystem. 9302 */ 9303 if (garp->n4g_va.va_mask & AT_NODEID) 9304 rd_res->dotdotp->d_ino = 9305 garp->n4g_va.va_nodeid; 9306 9307 9308 /* 9309 * Next, create the ".." dnlc entry 9310 */ 9311 sfhp = sfh4_get(fhp, mi); 9312 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) { 9313 dnlc_update(vp, "..", pvp); 9314 VN_RELE(pvp); 9315 } 9316 sfh4_rele(&sfhp); 9317 } 9318 } 9319 9320 if (mi->mi_io_kstats) { 9321 mutex_enter(&mi->mi_lock); 9322 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 9323 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen; 9324 mutex_exit(&mi->mi_lock); 9325 } 9326 9327 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 9328 9329 out: 9330 /* 9331 * If readdir a node that is a stub for a crossed mount point, 9332 * keep the original secinfo flavor for the current file system, 9333 * not the crossed one. 9334 */ 9335 (void) check_mnt_secinfo(mi->mi_curr_serv, vp); 9336 9337 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov); 9338 } 9339 9340 9341 static int 9342 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead) 9343 { 9344 rnode4_t *rp = VTOR4(bp->b_vp); 9345 int count; 9346 int error; 9347 cred_t *cred_otw = NULL; 9348 offset_t offset; 9349 nfs4_open_stream_t *osp = NULL; 9350 bool_t first_time = TRUE; /* first time getting otw cred */ 9351 bool_t last_time = FALSE; /* last time getting otw cred */ 9352 9353 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone); 9354 9355 DTRACE_IO1(start, struct buf *, bp); 9356 offset = ldbtob(bp->b_lblkno); 9357 9358 if (bp->b_flags & B_READ) { 9359 read_again: 9360 /* 9361 * Releases the osp, if it is provided. 9362 * Puts a hold on the cred_otw and the new osp (if found). 9363 */ 9364 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9365 &first_time, &last_time); 9366 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr, 9367 offset, bp->b_bcount, &bp->b_resid, cred_otw, 9368 readahead, NULL); 9369 crfree(cred_otw); 9370 if (!error) { 9371 if (bp->b_resid) { 9372 /* 9373 * Didn't get it all because we hit EOF, 9374 * zero all the memory beyond the EOF. 9375 */ 9376 /* bzero(rdaddr + */ 9377 bzero(bp->b_un.b_addr + 9378 bp->b_bcount - bp->b_resid, bp->b_resid); 9379 } 9380 mutex_enter(&rp->r_statelock); 9381 if (bp->b_resid == bp->b_bcount && 9382 offset >= rp->r_size) { 9383 /* 9384 * We didn't read anything at all as we are 9385 * past EOF. Return an error indicator back 9386 * but don't destroy the pages (yet). 9387 */ 9388 error = NFS_EOF; 9389 } 9390 mutex_exit(&rp->r_statelock); 9391 } else if (error == EACCES && last_time == FALSE) { 9392 goto read_again; 9393 } 9394 } else { 9395 if (!(rp->r_flags & R4STALE)) { 9396 write_again: 9397 /* 9398 * Releases the osp, if it is provided. 9399 * Puts a hold on the cred_otw and the new 9400 * osp (if found). 9401 */ 9402 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 9403 &first_time, &last_time); 9404 mutex_enter(&rp->r_statelock); 9405 count = MIN(bp->b_bcount, rp->r_size - offset); 9406 mutex_exit(&rp->r_statelock); 9407 if (count < 0) 9408 cmn_err(CE_PANIC, "nfs4_bio: write count < 0"); 9409 #ifdef DEBUG 9410 if (count == 0) { 9411 zoneid_t zoneid = getzoneid(); 9412 9413 zcmn_err(zoneid, CE_WARN, 9414 "nfs4_bio: zero length write at %lld", 9415 offset); 9416 zcmn_err(zoneid, CE_CONT, "flags=0x%x, " 9417 "b_bcount=%ld, file size=%lld", 9418 rp->r_flags, (long)bp->b_bcount, 9419 rp->r_size); 9420 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh); 9421 if (nfs4_bio_do_stop) 9422 debug_enter("nfs4_bio"); 9423 } 9424 #endif 9425 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset, 9426 count, cred_otw, stab_comm); 9427 if (error == EACCES && last_time == FALSE) { 9428 crfree(cred_otw); 9429 goto write_again; 9430 } 9431 bp->b_error = error; 9432 if (error && error != EINTR && 9433 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) { 9434 /* 9435 * Don't print EDQUOT errors on the console. 9436 * Don't print asynchronous EACCES errors. 9437 * Don't print EFBIG errors. 9438 * Print all other write errors. 9439 */ 9440 if (error != EDQUOT && error != EFBIG && 9441 (error != EACCES || 9442 !(bp->b_flags & B_ASYNC))) 9443 nfs4_write_error(bp->b_vp, 9444 error, cred_otw); 9445 /* 9446 * Update r_error and r_flags as appropriate. 9447 * If the error was ESTALE, then mark the 9448 * rnode as not being writeable and save 9449 * the error status. Otherwise, save any 9450 * errors which occur from asynchronous 9451 * page invalidations. Any errors occurring 9452 * from other operations should be saved 9453 * by the caller. 9454 */ 9455 mutex_enter(&rp->r_statelock); 9456 if (error == ESTALE) { 9457 rp->r_flags |= R4STALE; 9458 if (!rp->r_error) 9459 rp->r_error = error; 9460 } else if (!rp->r_error && 9461 (bp->b_flags & 9462 (B_INVAL|B_FORCE|B_ASYNC)) == 9463 (B_INVAL|B_FORCE|B_ASYNC)) { 9464 rp->r_error = error; 9465 } 9466 mutex_exit(&rp->r_statelock); 9467 } 9468 crfree(cred_otw); 9469 } else 9470 error = rp->r_error; 9471 } 9472 9473 if (error != 0 && error != NFS_EOF) 9474 bp->b_flags |= B_ERROR; 9475 9476 if (osp) 9477 open_stream_rele(osp, rp); 9478 9479 DTRACE_IO1(done, struct buf *, bp); 9480 9481 return (error); 9482 } 9483 9484 /* ARGSUSED */ 9485 int 9486 nfs4_fid(vnode_t *vp, fid_t *fidp) 9487 { 9488 return (EREMOTE); 9489 } 9490 9491 /* ARGSUSED2 */ 9492 int 9493 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9494 { 9495 rnode4_t *rp = VTOR4(vp); 9496 9497 if (!write_lock) { 9498 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9499 return (V_WRITELOCK_FALSE); 9500 } 9501 9502 if ((rp->r_flags & R4DIRECTIO) || 9503 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) { 9504 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 9505 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp)) 9506 return (V_WRITELOCK_FALSE); 9507 nfs_rw_exit(&rp->r_rwlock); 9508 } 9509 9510 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 9511 return (V_WRITELOCK_TRUE); 9512 } 9513 9514 /* ARGSUSED */ 9515 void 9516 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 9517 { 9518 rnode4_t *rp = VTOR4(vp); 9519 9520 nfs_rw_exit(&rp->r_rwlock); 9521 } 9522 9523 /* ARGSUSED */ 9524 static int 9525 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 9526 { 9527 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9528 return (EIO); 9529 9530 /* 9531 * Because we stuff the readdir cookie into the offset field 9532 * someone may attempt to do an lseek with the cookie which 9533 * we want to succeed. 9534 */ 9535 if (vp->v_type == VDIR) 9536 return (0); 9537 if (*noffp < 0) 9538 return (EINVAL); 9539 return (0); 9540 } 9541 9542 9543 /* 9544 * Return all the pages from [off..off+len) in file 9545 */ 9546 static int 9547 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 9548 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9549 enum seg_rw rw, cred_t *cr) 9550 { 9551 rnode4_t *rp; 9552 int error; 9553 mntinfo4_t *mi; 9554 9555 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9556 return (EIO); 9557 rp = VTOR4(vp); 9558 if (IS_SHADOW(vp, rp)) 9559 vp = RTOV4(rp); 9560 9561 if (vp->v_flag & VNOMAP) 9562 return (ENOSYS); 9563 9564 if (protp != NULL) 9565 *protp = PROT_ALL; 9566 9567 /* 9568 * Now validate that the caches are up to date. 9569 */ 9570 if (error = nfs4_validate_caches(vp, cr)) 9571 return (error); 9572 9573 mi = VTOMI4(vp); 9574 retry: 9575 mutex_enter(&rp->r_statelock); 9576 9577 /* 9578 * Don't create dirty pages faster than they 9579 * can be cleaned so that the system doesn't 9580 * get imbalanced. If the async queue is 9581 * maxed out, then wait for it to drain before 9582 * creating more dirty pages. Also, wait for 9583 * any threads doing pagewalks in the vop_getattr 9584 * entry points so that they don't block for 9585 * long periods. 9586 */ 9587 if (rw == S_CREATE) { 9588 while ((mi->mi_max_threads != 0 && 9589 rp->r_awcount > 2 * mi->mi_max_threads) || 9590 rp->r_gcount > 0) 9591 cv_wait(&rp->r_cv, &rp->r_statelock); 9592 } 9593 9594 /* 9595 * If we are getting called as a side effect of an nfs_write() 9596 * operation the local file size might not be extended yet. 9597 * In this case we want to be able to return pages of zeroes. 9598 */ 9599 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 9600 NFS4_DEBUG(nfs4_pageio_debug, 9601 (CE_NOTE, "getpage beyond EOF: off=%lld, " 9602 "len=%llu, size=%llu, attrsize =%llu", off, 9603 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size)); 9604 mutex_exit(&rp->r_statelock); 9605 return (EFAULT); /* beyond EOF */ 9606 } 9607 9608 mutex_exit(&rp->r_statelock); 9609 9610 if (len <= PAGESIZE) { 9611 error = nfs4_getapage(vp, off, len, protp, pl, plsz, 9612 seg, addr, rw, cr); 9613 NFS4_DEBUG(nfs4_pageio_debug && error, 9614 (CE_NOTE, "getpage error %d; off=%lld, " 9615 "len=%lld", error, off, (u_longlong_t)len)); 9616 } else { 9617 error = pvn_getpages(nfs4_getapage, vp, off, len, protp, 9618 pl, plsz, seg, addr, rw, cr); 9619 NFS4_DEBUG(nfs4_pageio_debug && error, 9620 (CE_NOTE, "getpages error %d; off=%lld, " 9621 "len=%lld", error, off, (u_longlong_t)len)); 9622 } 9623 9624 switch (error) { 9625 case NFS_EOF: 9626 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE); 9627 goto retry; 9628 case ESTALE: 9629 nfs4_purge_stale_fh(error, vp, cr); 9630 } 9631 9632 return (error); 9633 } 9634 9635 /* 9636 * Called from pvn_getpages or nfs4_getpage to get a particular page. 9637 */ 9638 /* ARGSUSED */ 9639 static int 9640 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 9641 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 9642 enum seg_rw rw, cred_t *cr) 9643 { 9644 rnode4_t *rp; 9645 uint_t bsize; 9646 struct buf *bp; 9647 page_t *pp; 9648 u_offset_t lbn; 9649 u_offset_t io_off; 9650 u_offset_t blkoff; 9651 u_offset_t rablkoff; 9652 size_t io_len; 9653 uint_t blksize; 9654 int error; 9655 int readahead; 9656 int readahead_issued = 0; 9657 int ra_window; /* readahead window */ 9658 page_t *pagefound; 9659 page_t *savepp; 9660 9661 if (nfs_zone() != VTOMI4(vp)->mi_zone) 9662 return (EIO); 9663 9664 rp = VTOR4(vp); 9665 ASSERT(!IS_SHADOW(vp, rp)); 9666 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9667 9668 reread: 9669 bp = NULL; 9670 pp = NULL; 9671 pagefound = NULL; 9672 9673 if (pl != NULL) 9674 pl[0] = NULL; 9675 9676 error = 0; 9677 lbn = off / bsize; 9678 blkoff = lbn * bsize; 9679 9680 /* 9681 * Queueing up the readahead before doing the synchronous read 9682 * results in a significant increase in read throughput because 9683 * of the increased parallelism between the async threads and 9684 * the process context. 9685 */ 9686 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 9687 rw != S_CREATE && 9688 !(vp->v_flag & VNOCACHE)) { 9689 mutex_enter(&rp->r_statelock); 9690 9691 /* 9692 * Calculate the number of readaheads to do. 9693 * a) No readaheads at offset = 0. 9694 * b) Do maximum(nfs4_nra) readaheads when the readahead 9695 * window is closed. 9696 * c) Do readaheads between 1 to (nfs4_nra - 1) depending 9697 * upon how far the readahead window is open or close. 9698 * d) No readaheads if rp->r_nextr is not within the scope 9699 * of the readahead window (random i/o). 9700 */ 9701 9702 if (off == 0) 9703 readahead = 0; 9704 else if (blkoff == rp->r_nextr) 9705 readahead = nfs4_nra; 9706 else if (rp->r_nextr > blkoff && 9707 ((ra_window = (rp->r_nextr - blkoff) / bsize) 9708 <= (nfs4_nra - 1))) 9709 readahead = nfs4_nra - ra_window; 9710 else 9711 readahead = 0; 9712 9713 rablkoff = rp->r_nextr; 9714 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 9715 mutex_exit(&rp->r_statelock); 9716 if (nfs4_async_readahead(vp, rablkoff + bsize, 9717 addr + (rablkoff + bsize - off), 9718 seg, cr, nfs4_readahead) < 0) { 9719 mutex_enter(&rp->r_statelock); 9720 break; 9721 } 9722 readahead--; 9723 rablkoff += bsize; 9724 /* 9725 * Indicate that we did a readahead so 9726 * readahead offset is not updated 9727 * by the synchronous read below. 9728 */ 9729 readahead_issued = 1; 9730 mutex_enter(&rp->r_statelock); 9731 /* 9732 * set readahead offset to 9733 * offset of last async readahead 9734 * request. 9735 */ 9736 rp->r_nextr = rablkoff; 9737 } 9738 mutex_exit(&rp->r_statelock); 9739 } 9740 9741 again: 9742 if ((pagefound = page_exists(vp, off)) == NULL) { 9743 if (pl == NULL) { 9744 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr, 9745 nfs4_readahead); 9746 } else if (rw == S_CREATE) { 9747 /* 9748 * Block for this page is not allocated, or the offset 9749 * is beyond the current allocation size, or we're 9750 * allocating a swap slot and the page was not found, 9751 * so allocate it and return a zero page. 9752 */ 9753 if ((pp = page_create_va(vp, off, 9754 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 9755 cmn_err(CE_PANIC, "nfs4_getapage: page_create"); 9756 io_len = PAGESIZE; 9757 mutex_enter(&rp->r_statelock); 9758 rp->r_nextr = off + PAGESIZE; 9759 mutex_exit(&rp->r_statelock); 9760 } else { 9761 /* 9762 * Need to go to server to get a block 9763 */ 9764 mutex_enter(&rp->r_statelock); 9765 if (blkoff < rp->r_size && 9766 blkoff + bsize > rp->r_size) { 9767 /* 9768 * If less than a block left in 9769 * file read less than a block. 9770 */ 9771 if (rp->r_size <= off) { 9772 /* 9773 * Trying to access beyond EOF, 9774 * set up to get at least one page. 9775 */ 9776 blksize = off + PAGESIZE - blkoff; 9777 } else 9778 blksize = rp->r_size - blkoff; 9779 } else if ((off == 0) || 9780 (off != rp->r_nextr && !readahead_issued)) { 9781 blksize = PAGESIZE; 9782 blkoff = off; /* block = page here */ 9783 } else 9784 blksize = bsize; 9785 mutex_exit(&rp->r_statelock); 9786 9787 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 9788 &io_len, blkoff, blksize, 0); 9789 9790 /* 9791 * Some other thread has entered the page, 9792 * so just use it. 9793 */ 9794 if (pp == NULL) 9795 goto again; 9796 9797 /* 9798 * Now round the request size up to page boundaries. 9799 * This ensures that the entire page will be 9800 * initialized to zeroes if EOF is encountered. 9801 */ 9802 io_len = ptob(btopr(io_len)); 9803 9804 bp = pageio_setup(pp, io_len, vp, B_READ); 9805 ASSERT(bp != NULL); 9806 9807 /* 9808 * pageio_setup should have set b_addr to 0. This 9809 * is correct since we want to do I/O on a page 9810 * boundary. bp_mapin will use this addr to calculate 9811 * an offset, and then set b_addr to the kernel virtual 9812 * address it allocated for us. 9813 */ 9814 ASSERT(bp->b_un.b_addr == 0); 9815 9816 bp->b_edev = 0; 9817 bp->b_dev = 0; 9818 bp->b_lblkno = lbtodb(io_off); 9819 bp->b_file = vp; 9820 bp->b_offset = (offset_t)off; 9821 bp_mapin(bp); 9822 9823 /* 9824 * If doing a write beyond what we believe is EOF, 9825 * don't bother trying to read the pages from the 9826 * server, we'll just zero the pages here. We 9827 * don't check that the rw flag is S_WRITE here 9828 * because some implementations may attempt a 9829 * read access to the buffer before copying data. 9830 */ 9831 mutex_enter(&rp->r_statelock); 9832 if (io_off >= rp->r_size && seg == segkmap) { 9833 mutex_exit(&rp->r_statelock); 9834 bzero(bp->b_un.b_addr, io_len); 9835 } else { 9836 mutex_exit(&rp->r_statelock); 9837 error = nfs4_bio(bp, NULL, cr, FALSE); 9838 } 9839 9840 /* 9841 * Unmap the buffer before freeing it. 9842 */ 9843 bp_mapout(bp); 9844 pageio_done(bp); 9845 9846 savepp = pp; 9847 do { 9848 pp->p_fsdata = C_NOCOMMIT; 9849 } while ((pp = pp->p_next) != savepp); 9850 9851 if (error == NFS_EOF) { 9852 /* 9853 * If doing a write system call just return 9854 * zeroed pages, else user tried to get pages 9855 * beyond EOF, return error. We don't check 9856 * that the rw flag is S_WRITE here because 9857 * some implementations may attempt a read 9858 * access to the buffer before copying data. 9859 */ 9860 if (seg == segkmap) 9861 error = 0; 9862 else 9863 error = EFAULT; 9864 } 9865 9866 if (!readahead_issued && !error) { 9867 mutex_enter(&rp->r_statelock); 9868 rp->r_nextr = io_off + io_len; 9869 mutex_exit(&rp->r_statelock); 9870 } 9871 } 9872 } 9873 9874 out: 9875 if (pl == NULL) 9876 return (error); 9877 9878 if (error) { 9879 if (pp != NULL) 9880 pvn_read_done(pp, B_ERROR); 9881 return (error); 9882 } 9883 9884 if (pagefound) { 9885 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 9886 9887 /* 9888 * Page exists in the cache, acquire the appropriate lock. 9889 * If this fails, start all over again. 9890 */ 9891 if ((pp = page_lookup(vp, off, se)) == NULL) { 9892 #ifdef DEBUG 9893 nfs4_lostpage++; 9894 #endif 9895 goto reread; 9896 } 9897 pl[0] = pp; 9898 pl[1] = NULL; 9899 return (0); 9900 } 9901 9902 if (pp != NULL) 9903 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 9904 9905 return (error); 9906 } 9907 9908 static void 9909 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 9910 cred_t *cr) 9911 { 9912 int error; 9913 page_t *pp; 9914 u_offset_t io_off; 9915 size_t io_len; 9916 struct buf *bp; 9917 uint_t bsize, blksize; 9918 rnode4_t *rp = VTOR4(vp); 9919 page_t *savepp; 9920 9921 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 9922 9923 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 9924 9925 mutex_enter(&rp->r_statelock); 9926 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 9927 /* 9928 * If less than a block left in file read less 9929 * than a block. 9930 */ 9931 blksize = rp->r_size - blkoff; 9932 } else 9933 blksize = bsize; 9934 mutex_exit(&rp->r_statelock); 9935 9936 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 9937 &io_off, &io_len, blkoff, blksize, 1); 9938 /* 9939 * The isra flag passed to the kluster function is 1, we may have 9940 * gotten a return value of NULL for a variety of reasons (# of free 9941 * pages < minfree, someone entered the page on the vnode etc). In all 9942 * cases, we want to punt on the readahead. 9943 */ 9944 if (pp == NULL) 9945 return; 9946 9947 /* 9948 * Now round the request size up to page boundaries. 9949 * This ensures that the entire page will be 9950 * initialized to zeroes if EOF is encountered. 9951 */ 9952 io_len = ptob(btopr(io_len)); 9953 9954 bp = pageio_setup(pp, io_len, vp, B_READ); 9955 ASSERT(bp != NULL); 9956 9957 /* 9958 * pageio_setup should have set b_addr to 0. This is correct since 9959 * we want to do I/O on a page boundary. bp_mapin() will use this addr 9960 * to calculate an offset, and then set b_addr to the kernel virtual 9961 * address it allocated for us. 9962 */ 9963 ASSERT(bp->b_un.b_addr == 0); 9964 9965 bp->b_edev = 0; 9966 bp->b_dev = 0; 9967 bp->b_lblkno = lbtodb(io_off); 9968 bp->b_file = vp; 9969 bp->b_offset = (offset_t)blkoff; 9970 bp_mapin(bp); 9971 9972 /* 9973 * If doing a write beyond what we believe is EOF, don't bother trying 9974 * to read the pages from the server, we'll just zero the pages here. 9975 * We don't check that the rw flag is S_WRITE here because some 9976 * implementations may attempt a read access to the buffer before 9977 * copying data. 9978 */ 9979 mutex_enter(&rp->r_statelock); 9980 if (io_off >= rp->r_size && seg == segkmap) { 9981 mutex_exit(&rp->r_statelock); 9982 bzero(bp->b_un.b_addr, io_len); 9983 error = 0; 9984 } else { 9985 mutex_exit(&rp->r_statelock); 9986 error = nfs4_bio(bp, NULL, cr, TRUE); 9987 if (error == NFS_EOF) 9988 error = 0; 9989 } 9990 9991 /* 9992 * Unmap the buffer before freeing it. 9993 */ 9994 bp_mapout(bp); 9995 pageio_done(bp); 9996 9997 savepp = pp; 9998 do { 9999 pp->p_fsdata = C_NOCOMMIT; 10000 } while ((pp = pp->p_next) != savepp); 10001 10002 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 10003 10004 /* 10005 * In case of error set readahead offset 10006 * to the lowest offset. 10007 * pvn_read_done() calls VN_DISPOSE to destroy the pages 10008 */ 10009 if (error && rp->r_nextr > io_off) { 10010 mutex_enter(&rp->r_statelock); 10011 if (rp->r_nextr > io_off) 10012 rp->r_nextr = io_off; 10013 mutex_exit(&rp->r_statelock); 10014 } 10015 } 10016 10017 /* 10018 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 10019 * If len == 0, do from off to EOF. 10020 * 10021 * The normal cases should be len == 0 && off == 0 (entire vp list) or 10022 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 10023 * (from pageout). 10024 */ 10025 static int 10026 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 10027 { 10028 int error; 10029 rnode4_t *rp; 10030 10031 ASSERT(cr != NULL); 10032 10033 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 10034 return (EIO); 10035 10036 rp = VTOR4(vp); 10037 if (IS_SHADOW(vp, rp)) 10038 vp = RTOV4(rp); 10039 10040 /* 10041 * XXX - Why should this check be made here? 10042 */ 10043 if (vp->v_flag & VNOMAP) 10044 return (ENOSYS); 10045 10046 if (len == 0 && !(flags & B_INVAL) && 10047 (vp->v_vfsp->vfs_flag & VFS_RDONLY)) 10048 return (0); 10049 10050 mutex_enter(&rp->r_statelock); 10051 rp->r_count++; 10052 mutex_exit(&rp->r_statelock); 10053 error = nfs4_putpages(vp, off, len, flags, cr); 10054 mutex_enter(&rp->r_statelock); 10055 rp->r_count--; 10056 cv_broadcast(&rp->r_cv); 10057 mutex_exit(&rp->r_statelock); 10058 10059 return (error); 10060 } 10061 10062 /* 10063 * Write out a single page, possibly klustering adjacent dirty pages. 10064 */ 10065 int 10066 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 10067 int flags, cred_t *cr) 10068 { 10069 u_offset_t io_off; 10070 u_offset_t lbn_off; 10071 u_offset_t lbn; 10072 size_t io_len; 10073 uint_t bsize; 10074 int error; 10075 rnode4_t *rp; 10076 10077 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY)); 10078 ASSERT(pp != NULL); 10079 ASSERT(cr != NULL); 10080 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone); 10081 10082 rp = VTOR4(vp); 10083 ASSERT(rp->r_count > 0); 10084 ASSERT(!IS_SHADOW(vp, rp)); 10085 10086 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 10087 lbn = pp->p_offset / bsize; 10088 lbn_off = lbn * bsize; 10089 10090 /* 10091 * Find a kluster that fits in one block, or in 10092 * one page if pages are bigger than blocks. If 10093 * there is less file space allocated than a whole 10094 * page, we'll shorten the i/o request below. 10095 */ 10096 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 10097 roundup(bsize, PAGESIZE), flags); 10098 10099 /* 10100 * pvn_write_kluster shouldn't have returned a page with offset 10101 * behind the original page we were given. Verify that. 10102 */ 10103 ASSERT((pp->p_offset / bsize) >= lbn); 10104 10105 /* 10106 * Now pp will have the list of kept dirty pages marked for 10107 * write back. It will also handle invalidation and freeing 10108 * of pages that are not dirty. Check for page length rounding 10109 * problems. 10110 */ 10111 if (io_off + io_len > lbn_off + bsize) { 10112 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 10113 io_len = lbn_off + bsize - io_off; 10114 } 10115 /* 10116 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10117 * consistent value of r_size. R4MODINPROGRESS is set in writerp4(). 10118 * When R4MODINPROGRESS is set it indicates that a uiomove() is in 10119 * progress and the r_size has not been made consistent with the 10120 * new size of the file. When the uiomove() completes the r_size is 10121 * updated and the R4MODINPROGRESS flag is cleared. 10122 * 10123 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a 10124 * consistent value of r_size. Without this handshaking, it is 10125 * possible that nfs4_bio() picks up the old value of r_size 10126 * before the uiomove() in writerp4() completes. This will result 10127 * in the write through nfs4_bio() being dropped. 10128 * 10129 * More precisely, there is a window between the time the uiomove() 10130 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 10131 * operation intervenes in this window, the page will be picked up, 10132 * because it is dirty (it will be unlocked, unless it was 10133 * pagecreate'd). When the page is picked up as dirty, the dirty 10134 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is 10135 * checked. This will still be the old size. Therefore the page will 10136 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 10137 * the page will be found to be clean and the write will be dropped. 10138 */ 10139 if (rp->r_flags & R4MODINPROGRESS) { 10140 mutex_enter(&rp->r_statelock); 10141 if ((rp->r_flags & R4MODINPROGRESS) && 10142 rp->r_modaddr + MAXBSIZE > io_off && 10143 rp->r_modaddr < io_off + io_len) { 10144 page_t *plist; 10145 /* 10146 * A write is in progress for this region of the file. 10147 * If we did not detect R4MODINPROGRESS here then this 10148 * path through nfs_putapage() would eventually go to 10149 * nfs4_bio() and may not write out all of the data 10150 * in the pages. We end up losing data. So we decide 10151 * to set the modified bit on each page in the page 10152 * list and mark the rnode with R4DIRTY. This write 10153 * will be restarted at some later time. 10154 */ 10155 plist = pp; 10156 while (plist != NULL) { 10157 pp = plist; 10158 page_sub(&plist, pp); 10159 hat_setmod(pp); 10160 page_io_unlock(pp); 10161 page_unlock(pp); 10162 } 10163 rp->r_flags |= R4DIRTY; 10164 mutex_exit(&rp->r_statelock); 10165 if (offp) 10166 *offp = io_off; 10167 if (lenp) 10168 *lenp = io_len; 10169 return (0); 10170 } 10171 mutex_exit(&rp->r_statelock); 10172 } 10173 10174 if (flags & B_ASYNC) { 10175 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr, 10176 nfs4_sync_putapage); 10177 } else 10178 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr); 10179 10180 if (offp) 10181 *offp = io_off; 10182 if (lenp) 10183 *lenp = io_len; 10184 return (error); 10185 } 10186 10187 static int 10188 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 10189 int flags, cred_t *cr) 10190 { 10191 int error; 10192 rnode4_t *rp; 10193 10194 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10195 10196 flags |= B_WRITE; 10197 10198 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 10199 10200 rp = VTOR4(vp); 10201 10202 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 10203 error == EACCES) && 10204 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 10205 if (!(rp->r_flags & R4OUTOFSPACE)) { 10206 mutex_enter(&rp->r_statelock); 10207 rp->r_flags |= R4OUTOFSPACE; 10208 mutex_exit(&rp->r_statelock); 10209 } 10210 flags |= B_ERROR; 10211 pvn_write_done(pp, flags); 10212 /* 10213 * If this was not an async thread, then try again to 10214 * write out the pages, but this time, also destroy 10215 * them whether or not the write is successful. This 10216 * will prevent memory from filling up with these 10217 * pages and destroying them is the only alternative 10218 * if they can't be written out. 10219 * 10220 * Don't do this if this is an async thread because 10221 * when the pages are unlocked in pvn_write_done, 10222 * some other thread could have come along, locked 10223 * them, and queued for an async thread. It would be 10224 * possible for all of the async threads to be tied 10225 * up waiting to lock the pages again and they would 10226 * all already be locked and waiting for an async 10227 * thread to handle them. Deadlock. 10228 */ 10229 if (!(flags & B_ASYNC)) { 10230 error = nfs4_putpage(vp, io_off, io_len, 10231 B_INVAL | B_FORCE, cr); 10232 } 10233 } else { 10234 if (error) 10235 flags |= B_ERROR; 10236 else if (rp->r_flags & R4OUTOFSPACE) { 10237 mutex_enter(&rp->r_statelock); 10238 rp->r_flags &= ~R4OUTOFSPACE; 10239 mutex_exit(&rp->r_statelock); 10240 } 10241 pvn_write_done(pp, flags); 10242 if (freemem < desfree) 10243 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr, 10244 NFS4_WRITE_NOWAIT); 10245 } 10246 10247 return (error); 10248 } 10249 10250 #ifdef DEBUG 10251 int nfs4_force_open_before_mmap = 0; 10252 #endif 10253 10254 static int 10255 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 10256 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 10257 { 10258 struct segvn_crargs vn_a; 10259 int error = 0; 10260 rnode4_t *rp = VTOR4(vp); 10261 mntinfo4_t *mi = VTOMI4(vp); 10262 10263 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10264 return (EIO); 10265 10266 if (vp->v_flag & VNOMAP) 10267 return (ENOSYS); 10268 10269 if (off < 0 || (off + len) < 0) 10270 return (ENXIO); 10271 10272 if (vp->v_type != VREG) 10273 return (ENODEV); 10274 10275 /* 10276 * If the file is delegated to the client don't do anything. 10277 * If the file is not delegated, then validate the data cache. 10278 */ 10279 mutex_enter(&rp->r_statev4_lock); 10280 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) { 10281 mutex_exit(&rp->r_statev4_lock); 10282 error = nfs4_validate_caches(vp, cr); 10283 if (error) 10284 return (error); 10285 } else { 10286 mutex_exit(&rp->r_statev4_lock); 10287 } 10288 10289 /* 10290 * Check to see if the vnode is currently marked as not cachable. 10291 * This means portions of the file are locked (through VOP_FRLOCK). 10292 * In this case the map request must be refused. We use 10293 * rp->r_lkserlock to avoid a race with concurrent lock requests. 10294 */ 10295 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) 10296 return (EINTR); 10297 10298 if (vp->v_flag & VNOCACHE) { 10299 error = EAGAIN; 10300 goto done; 10301 } 10302 10303 /* 10304 * Don't allow concurrent locks and mapping if mandatory locking is 10305 * enabled. 10306 */ 10307 if (flk_has_remote_locks(vp)) { 10308 struct vattr va; 10309 va.va_mask = AT_MODE; 10310 error = nfs4getattr(vp, &va, cr); 10311 if (error != 0) 10312 goto done; 10313 if (MANDLOCK(vp, va.va_mode)) { 10314 error = EAGAIN; 10315 goto done; 10316 } 10317 } 10318 10319 /* 10320 * It is possible that the rnode has a lost lock request that we 10321 * are still trying to recover, and that the request conflicts with 10322 * this map request. 10323 * 10324 * An alternative approach would be for nfs4_safemap() to consider 10325 * queued lock requests when deciding whether to set or clear 10326 * VNOCACHE. This would require the frlock code path to call 10327 * nfs4_safemap() after enqueing a lost request. 10328 */ 10329 if (nfs4_map_lost_lock_conflict(vp)) { 10330 error = EAGAIN; 10331 goto done; 10332 } 10333 10334 as_rangelock(as); 10335 if (!(flags & MAP_FIXED)) { 10336 map_addr(addrp, len, off, 1, flags); 10337 if (*addrp == NULL) { 10338 as_rangeunlock(as); 10339 error = ENOMEM; 10340 goto done; 10341 } 10342 } else { 10343 /* 10344 * User specified address - blow away any previous mappings 10345 */ 10346 (void) as_unmap(as, *addrp, len); 10347 } 10348 10349 if (vp->v_type == VREG) { 10350 /* 10351 * We need to retrieve the open stream 10352 */ 10353 nfs4_open_stream_t *osp = NULL; 10354 nfs4_open_owner_t *oop = NULL; 10355 10356 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10357 if (oop != NULL) { 10358 /* returns with 'os_sync_lock' held */ 10359 osp = find_open_stream(oop, rp); 10360 open_owner_rele(oop); 10361 } 10362 if (osp == NULL) { 10363 #ifdef DEBUG 10364 if (nfs4_force_open_before_mmap) { 10365 error = EIO; 10366 goto done; 10367 } 10368 #endif 10369 /* returns with 'os_sync_lock' held */ 10370 error = open_and_get_osp(vp, cr, &osp); 10371 if (osp == NULL) { 10372 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10373 "nfs4_map: we tried to OPEN the file " 10374 "but again no osp, so fail with EIO")); 10375 goto done; 10376 } 10377 } 10378 10379 if (osp->os_failed_reopen) { 10380 mutex_exit(&osp->os_sync_lock); 10381 open_stream_rele(osp, rp); 10382 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, 10383 "nfs4_map: os_failed_reopen set on " 10384 "osp %p, cr %p, rp %s", (void *)osp, 10385 (void *)cr, rnode4info(rp))); 10386 error = EIO; 10387 goto done; 10388 } 10389 mutex_exit(&osp->os_sync_lock); 10390 open_stream_rele(osp, rp); 10391 } 10392 10393 vn_a.vp = vp; 10394 vn_a.offset = off; 10395 vn_a.type = (flags & MAP_TYPE); 10396 vn_a.prot = (uchar_t)prot; 10397 vn_a.maxprot = (uchar_t)maxprot; 10398 vn_a.flags = (flags & ~MAP_TYPE); 10399 vn_a.cred = cr; 10400 vn_a.amp = NULL; 10401 vn_a.szc = 0; 10402 vn_a.lgrp_mem_policy_flags = 0; 10403 10404 error = as_map(as, *addrp, len, segvn_create, &vn_a); 10405 as_rangeunlock(as); 10406 10407 done: 10408 nfs_rw_exit(&rp->r_lkserlock); 10409 return (error); 10410 } 10411 10412 /* 10413 * We're most likely dealing with a kernel module that likes to READ 10414 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets 10415 * officially OPEN the file to create the necessary client state 10416 * for bookkeeping of os_mmap_read/write counts. 10417 * 10418 * Since VOP_MAP only passes in a pointer to the vnode rather than 10419 * a double pointer, we can't handle the case where nfs4open_otw() 10420 * returns a different vnode than the one passed into VOP_MAP (since 10421 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case, 10422 * we return NULL and let nfs4_map() fail. Note: the only case where 10423 * this should happen is if the file got removed and replaced with the 10424 * same name on the server (in addition to the fact that we're trying 10425 * to VOP_MAP withouth VOP_OPENing the file in the first place). 10426 */ 10427 static int 10428 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp) 10429 { 10430 rnode4_t *rp, *drp; 10431 vnode_t *dvp, *open_vp; 10432 char file_name[MAXNAMELEN]; 10433 int just_created; 10434 nfs4_open_stream_t *osp; 10435 nfs4_open_owner_t *oop; 10436 int error; 10437 10438 *ospp = NULL; 10439 open_vp = map_vp; 10440 10441 rp = VTOR4(open_vp); 10442 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0) 10443 return (error); 10444 drp = VTOR4(dvp); 10445 10446 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) { 10447 VN_RELE(dvp); 10448 return (EINTR); 10449 } 10450 10451 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) { 10452 nfs_rw_exit(&drp->r_rwlock); 10453 VN_RELE(dvp); 10454 return (error); 10455 } 10456 10457 mutex_enter(&rp->r_statev4_lock); 10458 if (rp->created_v4) { 10459 rp->created_v4 = 0; 10460 mutex_exit(&rp->r_statev4_lock); 10461 10462 dnlc_update(dvp, file_name, open_vp); 10463 /* This is needed so we don't bump the open ref count */ 10464 just_created = 1; 10465 } else { 10466 mutex_exit(&rp->r_statev4_lock); 10467 just_created = 0; 10468 } 10469 10470 VN_HOLD(map_vp); 10471 10472 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0, 10473 just_created); 10474 if (error) { 10475 nfs_rw_exit(&drp->r_rwlock); 10476 VN_RELE(dvp); 10477 VN_RELE(map_vp); 10478 return (error); 10479 } 10480 10481 nfs_rw_exit(&drp->r_rwlock); 10482 VN_RELE(dvp); 10483 10484 /* 10485 * If nfs4open_otw() returned a different vnode then "undo" 10486 * the open and return failure to the caller. 10487 */ 10488 if (!VN_CMP(open_vp, map_vp)) { 10489 nfs4_error_t e; 10490 10491 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10492 "open returned a different vnode")); 10493 /* 10494 * If there's an error, ignore it, 10495 * and let VOP_INACTIVE handle it. 10496 */ 10497 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10498 CLOSE_NORM, 0, 0, 0); 10499 VN_RELE(map_vp); 10500 return (EIO); 10501 } 10502 10503 VN_RELE(map_vp); 10504 10505 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp)); 10506 if (!oop) { 10507 nfs4_error_t e; 10508 10509 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: " 10510 "no open owner")); 10511 /* 10512 * If there's an error, ignore it, 10513 * and let VOP_INACTIVE handle it. 10514 */ 10515 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e, 10516 CLOSE_NORM, 0, 0, 0); 10517 return (EIO); 10518 } 10519 osp = find_open_stream(oop, rp); 10520 open_owner_rele(oop); 10521 *ospp = osp; 10522 return (0); 10523 } 10524 10525 /* 10526 * Please be aware that when this function is called, the address space write 10527 * a_lock is held. Do not put over the wire calls in this function. 10528 */ 10529 /* ARGSUSED */ 10530 static int 10531 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10532 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 10533 { 10534 rnode4_t *rp; 10535 int error = 0; 10536 mntinfo4_t *mi; 10537 10538 mi = VTOMI4(vp); 10539 rp = VTOR4(vp); 10540 10541 if (nfs_zone() != mi->mi_zone) 10542 return (EIO); 10543 if (vp->v_flag & VNOMAP) 10544 return (ENOSYS); 10545 10546 /* 10547 * Need to hold rwlock while incrementing the mapcnt so that 10548 * mmap'ing can be serialized with writes so that the caching 10549 * can be handled correctly. 10550 * 10551 * Don't need to update the open stream first, since this 10552 * mmap can't add any additional share access that isn't 10553 * already contained in the open stream (for the case where we 10554 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't 10555 * take into account os_mmap_read[write] counts). 10556 */ 10557 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp))) 10558 return (EINTR); 10559 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 10560 nfs_rw_exit(&rp->r_rwlock); 10561 10562 if (vp->v_type == VREG) { 10563 /* 10564 * We need to retrieve the open stream and update the counts. 10565 * If there is no open stream here, something is wrong. 10566 */ 10567 nfs4_open_stream_t *osp = NULL; 10568 nfs4_open_owner_t *oop = NULL; 10569 10570 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 10571 if (oop != NULL) { 10572 /* returns with 'os_sync_lock' held */ 10573 osp = find_open_stream(oop, rp); 10574 open_owner_rele(oop); 10575 } 10576 if (osp == NULL) { 10577 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, 10578 "nfs4_addmap: we should have an osp" 10579 "but we don't, so fail with EIO")); 10580 error = EIO; 10581 goto out; 10582 } 10583 10584 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p," 10585 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot)); 10586 10587 /* 10588 * Update the map count in the open stream. 10589 * This is necessary in the case where we 10590 * open/mmap/close/, then the server reboots, and we 10591 * attempt to reopen. If the mmap doesn't add share 10592 * access then we send an invalid reopen with 10593 * access = NONE. 10594 * 10595 * We need to specifically check each PROT_* so a mmap 10596 * call of (PROT_WRITE | PROT_EXEC) will ensure us both 10597 * read and write access. A simple comparison of prot 10598 * to ~PROT_WRITE to determine read access is insufficient 10599 * since prot can be |= with PROT_USER, etc. 10600 */ 10601 10602 /* 10603 * Unless we're MAP_SHARED, no sense in adding os_mmap_write 10604 */ 10605 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 10606 osp->os_mmap_write += btopr(len); 10607 if (maxprot & PROT_READ) 10608 osp->os_mmap_read += btopr(len); 10609 if (maxprot & PROT_EXEC) 10610 osp->os_mmap_read += btopr(len); 10611 /* 10612 * Ensure that os_mmap_read gets incremented, even if 10613 * maxprot were to look like PROT_NONE. 10614 */ 10615 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 10616 !(maxprot & PROT_EXEC)) 10617 osp->os_mmap_read += btopr(len); 10618 osp->os_mapcnt += btopr(len); 10619 mutex_exit(&osp->os_sync_lock); 10620 open_stream_rele(osp, rp); 10621 } 10622 10623 out: 10624 /* 10625 * If we got an error, then undo our 10626 * incrementing of 'r_mapcnt'. 10627 */ 10628 10629 if (error) { 10630 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len)); 10631 ASSERT(rp->r_mapcnt >= 0); 10632 } 10633 return (error); 10634 } 10635 10636 static int 10637 nfs4_cmp(vnode_t *vp1, vnode_t *vp2) 10638 { 10639 10640 return (VTOR4(vp1) == VTOR4(vp2)); 10641 } 10642 10643 static int 10644 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10645 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr) 10646 { 10647 int rc; 10648 u_offset_t start, end; 10649 rnode4_t *rp; 10650 int error = 0, intr = INTR4(vp); 10651 nfs4_error_t e; 10652 10653 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10654 return (EIO); 10655 10656 /* check for valid cmd parameter */ 10657 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 10658 return (EINVAL); 10659 10660 /* Verify l_type. */ 10661 switch (bfp->l_type) { 10662 case F_RDLCK: 10663 if (cmd != F_GETLK && !(flag & FREAD)) 10664 return (EBADF); 10665 break; 10666 case F_WRLCK: 10667 if (cmd != F_GETLK && !(flag & FWRITE)) 10668 return (EBADF); 10669 break; 10670 case F_UNLCK: 10671 intr = 0; 10672 break; 10673 10674 default: 10675 return (EINVAL); 10676 } 10677 10678 /* check the validity of the lock range */ 10679 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 10680 return (rc); 10681 if (rc = flk_check_lock_data(start, end, MAXEND)) 10682 return (rc); 10683 10684 /* 10685 * If the filesystem is mounted using local locking, pass the 10686 * request off to the local locking code. 10687 */ 10688 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) { 10689 if (cmd == F_SETLK || cmd == F_SETLKW) { 10690 /* 10691 * For complete safety, we should be holding 10692 * r_lkserlock. However, we can't call 10693 * nfs4_safelock and then fs_frlock while 10694 * holding r_lkserlock, so just invoke 10695 * nfs4_safelock and expect that this will 10696 * catch enough of the cases. 10697 */ 10698 if (!nfs4_safelock(vp, bfp, cr)) 10699 return (EAGAIN); 10700 } 10701 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr)); 10702 } 10703 10704 rp = VTOR4(vp); 10705 10706 /* 10707 * Check whether the given lock request can proceed, given the 10708 * current file mappings. 10709 */ 10710 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 10711 return (EINTR); 10712 if (cmd == F_SETLK || cmd == F_SETLKW) { 10713 if (!nfs4_safelock(vp, bfp, cr)) { 10714 rc = EAGAIN; 10715 goto done; 10716 } 10717 } 10718 10719 /* 10720 * Flush the cache after waiting for async I/O to finish. For new 10721 * locks, this is so that the process gets the latest bits from the 10722 * server. For unlocks, this is so that other clients see the 10723 * latest bits once the file has been unlocked. If currently dirty 10724 * pages can't be flushed, then don't allow a lock to be set. But 10725 * allow unlocks to succeed, to avoid having orphan locks on the 10726 * server. 10727 */ 10728 if (cmd != F_GETLK) { 10729 mutex_enter(&rp->r_statelock); 10730 while (rp->r_count > 0) { 10731 if (intr) { 10732 klwp_t *lwp = ttolwp(curthread); 10733 10734 if (lwp != NULL) 10735 lwp->lwp_nostop++; 10736 if (cv_wait_sig(&rp->r_cv, 10737 &rp->r_statelock) == 0) { 10738 if (lwp != NULL) 10739 lwp->lwp_nostop--; 10740 rc = EINTR; 10741 break; 10742 } 10743 if (lwp != NULL) 10744 lwp->lwp_nostop--; 10745 } else 10746 cv_wait(&rp->r_cv, &rp->r_statelock); 10747 } 10748 mutex_exit(&rp->r_statelock); 10749 if (rc != 0) 10750 goto done; 10751 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr); 10752 if (error) { 10753 if (error == ENOSPC || error == EDQUOT) { 10754 mutex_enter(&rp->r_statelock); 10755 if (!rp->r_error) 10756 rp->r_error = error; 10757 mutex_exit(&rp->r_statelock); 10758 } 10759 if (bfp->l_type != F_UNLCK) { 10760 rc = ENOLCK; 10761 goto done; 10762 } 10763 } 10764 } 10765 10766 /* 10767 * Call the lock manager to do the real work of contacting 10768 * the server and obtaining the lock. 10769 */ 10770 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset, 10771 cr, &e, NULL, NULL); 10772 rc = e.error; 10773 10774 if (rc == 0) 10775 nfs4_lockcompletion(vp, cmd); 10776 10777 done: 10778 nfs_rw_exit(&rp->r_lkserlock); 10779 10780 return (rc); 10781 } 10782 10783 /* 10784 * Free storage space associated with the specified vnode. The portion 10785 * to be freed is specified by bfp->l_start and bfp->l_len (already 10786 * normalized to a "whence" of 0). 10787 * 10788 * This is an experimental facility whose continued existence is not 10789 * guaranteed. Currently, we only support the special case 10790 * of l_len == 0, meaning free to end of file. 10791 */ 10792 /* ARGSUSED */ 10793 static int 10794 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 10795 offset_t offset, cred_t *cr, caller_context_t *ct) 10796 { 10797 int error; 10798 10799 if (nfs_zone() != VTOMI4(vp)->mi_zone) 10800 return (EIO); 10801 ASSERT(vp->v_type == VREG); 10802 if (cmd != F_FREESP) 10803 return (EINVAL); 10804 10805 error = convoff(vp, bfp, 0, offset); 10806 if (!error) { 10807 ASSERT(bfp->l_start >= 0); 10808 if (bfp->l_len == 0) { 10809 struct vattr va; 10810 10811 va.va_mask = AT_SIZE; 10812 va.va_size = bfp->l_start; 10813 error = nfs4setattr(vp, &va, 0, cr, NULL); 10814 } else 10815 error = EINVAL; 10816 } 10817 10818 return (error); 10819 } 10820 10821 /* ARGSUSED */ 10822 int 10823 nfs4_realvp(vnode_t *vp, vnode_t **vpp) 10824 { 10825 rnode4_t *rp; 10826 rp = VTOR4(vp); 10827 10828 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) { 10829 vp = RTOV4(rp); 10830 } 10831 *vpp = vp; 10832 return (0); 10833 } 10834 10835 /* 10836 * Setup and add an address space callback to do the work of the delmap call. 10837 * The callback will (and must be) deleted in the actual callback function. 10838 * 10839 * This is done in order to take care of the problem that we have with holding 10840 * the address space's a_lock for a long period of time (e.g. if the NFS server 10841 * is down). Callbacks will be executed in the address space code while the 10842 * a_lock is not held. Holding the address space's a_lock causes things such 10843 * as ps and fork to hang because they are trying to acquire this lock as well. 10844 */ 10845 /* ARGSUSED */ 10846 static int 10847 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 10848 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 10849 { 10850 int caller_found; 10851 int error; 10852 rnode4_t *rp; 10853 nfs4_delmap_args_t *dmapp; 10854 nfs4_delmapcall_t *delmap_call; 10855 10856 if (vp->v_flag & VNOMAP) 10857 return (ENOSYS); 10858 10859 /* 10860 * A process may not change zones if it has NFS pages mmap'ed 10861 * in, so we can't legitimately get here from the wrong zone. 10862 */ 10863 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 10864 10865 rp = VTOR4(vp); 10866 10867 /* 10868 * The way that the address space of this process deletes its mapping 10869 * of this file is via the following call chains: 10870 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10871 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap() 10872 * 10873 * With the use of address space callbacks we are allowed to drop the 10874 * address space lock, a_lock, while executing the NFS operations that 10875 * need to go over the wire. Returning EAGAIN to the caller of this 10876 * function is what drives the execution of the callback that we add 10877 * below. The callback will be executed by the address space code 10878 * after dropping the a_lock. When the callback is finished, since 10879 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 10880 * is called again on the same segment to finish the rest of the work 10881 * that needs to happen during unmapping. 10882 * 10883 * This action of calling back into the segment driver causes 10884 * nfs4_delmap() to get called again, but since the callback was 10885 * already executed at this point, it already did the work and there 10886 * is nothing left for us to do. 10887 * 10888 * To Summarize: 10889 * - The first time nfs4_delmap is called by the current thread is when 10890 * we add the caller associated with this delmap to the delmap caller 10891 * list, add the callback, and return EAGAIN. 10892 * - The second time in this call chain when nfs4_delmap is called we 10893 * will find this caller in the delmap caller list and realize there 10894 * is no more work to do thus removing this caller from the list and 10895 * returning the error that was set in the callback execution. 10896 */ 10897 caller_found = nfs4_find_and_delete_delmapcall(rp, &error); 10898 if (caller_found) { 10899 /* 10900 * 'error' is from the actual delmap operations. To avoid 10901 * hangs, we need to handle the return of EAGAIN differently 10902 * since this is what drives the callback execution. 10903 * In this case, we don't want to return EAGAIN and do the 10904 * callback execution because there are none to execute. 10905 */ 10906 if (error == EAGAIN) 10907 return (0); 10908 else 10909 return (error); 10910 } 10911 10912 /* current caller was not in the list */ 10913 delmap_call = nfs4_init_delmapcall(); 10914 10915 mutex_enter(&rp->r_statelock); 10916 list_insert_tail(&rp->r_indelmap, delmap_call); 10917 mutex_exit(&rp->r_statelock); 10918 10919 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP); 10920 10921 dmapp->vp = vp; 10922 dmapp->off = off; 10923 dmapp->addr = addr; 10924 dmapp->len = len; 10925 dmapp->prot = prot; 10926 dmapp->maxprot = maxprot; 10927 dmapp->flags = flags; 10928 dmapp->cr = cr; 10929 dmapp->caller = delmap_call; 10930 10931 error = as_add_callback(as, nfs4_delmap_callback, dmapp, 10932 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 10933 10934 return (error ? error : EAGAIN); 10935 } 10936 10937 static nfs4_delmapcall_t * 10938 nfs4_init_delmapcall() 10939 { 10940 nfs4_delmapcall_t *delmap_call; 10941 10942 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP); 10943 delmap_call->call_id = curthread; 10944 delmap_call->error = 0; 10945 10946 return (delmap_call); 10947 } 10948 10949 static void 10950 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call) 10951 { 10952 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t)); 10953 } 10954 10955 /* 10956 * Searches for the current delmap caller (based on curthread) in the list of 10957 * callers. If it is found, we remove it and free the delmap caller. 10958 * Returns: 10959 * 0 if the caller wasn't found 10960 * 1 if the caller was found, removed and freed. *errp will be set 10961 * to what the result of the delmap was. 10962 */ 10963 static int 10964 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp) 10965 { 10966 nfs4_delmapcall_t *delmap_call; 10967 10968 /* 10969 * If the list doesn't exist yet, we create it and return 10970 * that the caller wasn't found. No list = no callers. 10971 */ 10972 mutex_enter(&rp->r_statelock); 10973 if (!(rp->r_flags & R4DELMAPLIST)) { 10974 /* The list does not exist */ 10975 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t), 10976 offsetof(nfs4_delmapcall_t, call_node)); 10977 rp->r_flags |= R4DELMAPLIST; 10978 mutex_exit(&rp->r_statelock); 10979 return (0); 10980 } else { 10981 /* The list exists so search it */ 10982 for (delmap_call = list_head(&rp->r_indelmap); 10983 delmap_call != NULL; 10984 delmap_call = list_next(&rp->r_indelmap, delmap_call)) { 10985 if (delmap_call->call_id == curthread) { 10986 /* current caller is in the list */ 10987 *errp = delmap_call->error; 10988 list_remove(&rp->r_indelmap, delmap_call); 10989 mutex_exit(&rp->r_statelock); 10990 nfs4_free_delmapcall(delmap_call); 10991 return (1); 10992 } 10993 } 10994 } 10995 mutex_exit(&rp->r_statelock); 10996 return (0); 10997 } 10998 10999 /* 11000 * Remove some pages from an mmap'd vnode. Just update the 11001 * count of pages. If doing close-to-open, then flush and 11002 * commit all of the pages associated with this file. 11003 * Otherwise, start an asynchronous page flush to write out 11004 * any dirty pages. This will also associate a credential 11005 * with the rnode which can be used to write the pages. 11006 */ 11007 /* ARGSUSED */ 11008 static void 11009 nfs4_delmap_callback(struct as *as, void *arg, uint_t event) 11010 { 11011 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11012 rnode4_t *rp; 11013 mntinfo4_t *mi; 11014 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg; 11015 11016 rp = VTOR4(dmapp->vp); 11017 mi = VTOMI4(dmapp->vp); 11018 11019 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 11020 ASSERT(rp->r_mapcnt >= 0); 11021 11022 /* 11023 * Initiate a page flush and potential commit if there are 11024 * pages, the file system was not mounted readonly, the segment 11025 * was mapped shared, and the pages themselves were writeable. 11026 */ 11027 if (nfs4_has_pages(dmapp->vp) && 11028 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) && 11029 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 11030 mutex_enter(&rp->r_statelock); 11031 rp->r_flags |= R4DIRTY; 11032 mutex_exit(&rp->r_statelock); 11033 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off, 11034 dmapp->len, dmapp->cr); 11035 if (!e.error) { 11036 mutex_enter(&rp->r_statelock); 11037 e.error = rp->r_error; 11038 rp->r_error = 0; 11039 mutex_exit(&rp->r_statelock); 11040 } 11041 } else 11042 e.error = 0; 11043 11044 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) 11045 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len, 11046 B_INVAL, dmapp->cr); 11047 11048 if (e.error) { 11049 e.stat = puterrno4(e.error); 11050 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11051 OP_COMMIT, FALSE, NULL, 0, dmapp->vp); 11052 dmapp->caller->error = e.error; 11053 } 11054 11055 /* Check to see if we need to close the file */ 11056 11057 if (dmapp->vp->v_type == VREG) { 11058 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e, 11059 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags); 11060 11061 if (e.error != 0 || e.stat != NFS4_OK) { 11062 /* 11063 * Since it is possible that e.error == 0 and 11064 * e.stat != NFS4_OK (and vice versa), 11065 * we do the proper checking in order to get both 11066 * e.error and e.stat reporting the correct info. 11067 */ 11068 if (e.stat == NFS4_OK) 11069 e.stat = puterrno4(e.error); 11070 if (e.error == 0) 11071 e.error = geterrno4(e.stat); 11072 11073 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0, 11074 OP_CLOSE, FALSE, NULL, 0, dmapp->vp); 11075 dmapp->caller->error = e.error; 11076 } 11077 } 11078 11079 (void) as_delete_callback(as, arg); 11080 kmem_free(dmapp, sizeof (nfs4_delmap_args_t)); 11081 } 11082 11083 11084 static uint_t 11085 fattr4_maxfilesize_to_bits(uint64_t ll) 11086 { 11087 uint_t l = 1; 11088 11089 if (ll == 0) { 11090 return (0); 11091 } 11092 11093 if (ll & 0xffffffff00000000) { 11094 l += 32; ll >>= 32; 11095 } 11096 if (ll & 0xffff0000) { 11097 l += 16; ll >>= 16; 11098 } 11099 if (ll & 0xff00) { 11100 l += 8; ll >>= 8; 11101 } 11102 if (ll & 0xf0) { 11103 l += 4; ll >>= 4; 11104 } 11105 if (ll & 0xc) { 11106 l += 2; ll >>= 2; 11107 } 11108 if (ll & 0x2) { 11109 l += 1; 11110 } 11111 return (l); 11112 } 11113 11114 int 11115 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 11116 { 11117 int error; 11118 hrtime_t t; 11119 rnode4_t *rp; 11120 nfs4_ga_res_t gar; 11121 nfs4_ga_ext_res_t ger; 11122 11123 gar.n4g_ext_res = &ger; 11124 11125 if (nfs_zone() != VTOMI4(vp)->mi_zone) 11126 return (EIO); 11127 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) { 11128 *valp = MAXPATHLEN; 11129 return (0); 11130 } 11131 if (cmd == _PC_ACL_ENABLED) { 11132 *valp = _ACL_ACE_ENABLED; 11133 return (0); 11134 } 11135 11136 rp = VTOR4(vp); 11137 if (cmd == _PC_XATTR_EXISTS) { 11138 /* 11139 * Eventually should attempt small client readdir before 11140 * going otw with GETATTR(FATTR4_NAMED_ATTR). For now 11141 * just drive the OTW getattr. This is required because 11142 * _PC_XATTR_EXISTS can only return true if attributes 11143 * exist -- simply checking for existance of the attrdir 11144 * is not sufficient. 11145 * 11146 * pc4_xattr_valid can be only be trusted when r_xattr_dir 11147 * is NULL. Once the xadir vp exists, we can create xattrs, 11148 * and we don't have any way to update the "base" object's 11149 * pc4_xattr_exists from the xattr or xadir. Maybe FEM 11150 * could help out. 11151 */ 11152 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid && 11153 rp->r_xattr_dir == NULL) { 11154 *valp = rp->r_pathconf.pc4_xattr_exists; 11155 return (0); 11156 } 11157 } else { /* OLD CODE */ 11158 if (ATTRCACHE4_VALID(vp)) { 11159 mutex_enter(&rp->r_statelock); 11160 if (rp->r_pathconf.pc4_cache_valid) { 11161 error = 0; 11162 switch (cmd) { 11163 case _PC_FILESIZEBITS: 11164 *valp = 11165 rp->r_pathconf.pc4_filesizebits; 11166 break; 11167 case _PC_LINK_MAX: 11168 *valp = 11169 rp->r_pathconf.pc4_link_max; 11170 break; 11171 case _PC_NAME_MAX: 11172 *valp = 11173 rp->r_pathconf.pc4_name_max; 11174 break; 11175 case _PC_CHOWN_RESTRICTED: 11176 *valp = 11177 rp->r_pathconf.pc4_chown_restricted; 11178 break; 11179 case _PC_NO_TRUNC: 11180 *valp = 11181 rp->r_pathconf.pc4_no_trunc; 11182 break; 11183 default: 11184 error = EINVAL; 11185 break; 11186 } 11187 mutex_exit(&rp->r_statelock); 11188 #ifdef DEBUG 11189 nfs4_pathconf_cache_hits++; 11190 #endif 11191 return (error); 11192 } 11193 mutex_exit(&rp->r_statelock); 11194 } 11195 } 11196 #ifdef DEBUG 11197 nfs4_pathconf_cache_misses++; 11198 #endif 11199 11200 t = gethrtime(); 11201 11202 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr); 11203 11204 if (error) { 11205 mutex_enter(&rp->r_statelock); 11206 rp->r_pathconf.pc4_cache_valid = FALSE; 11207 rp->r_pathconf.pc4_xattr_valid = FALSE; 11208 mutex_exit(&rp->r_statelock); 11209 return (error); 11210 } 11211 11212 /* interpret the max filesize */ 11213 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits = 11214 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize); 11215 11216 /* Store the attributes we just received */ 11217 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL); 11218 11219 switch (cmd) { 11220 case _PC_FILESIZEBITS: 11221 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits; 11222 break; 11223 case _PC_LINK_MAX: 11224 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max; 11225 break; 11226 case _PC_NAME_MAX: 11227 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max; 11228 break; 11229 case _PC_CHOWN_RESTRICTED: 11230 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted; 11231 break; 11232 case _PC_NO_TRUNC: 11233 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc; 11234 break; 11235 case _PC_XATTR_EXISTS: 11236 *valp = gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists; 11237 break; 11238 default: 11239 return (EINVAL); 11240 } 11241 11242 return (0); 11243 } 11244 11245 /* 11246 * Called by async thread to do synchronous pageio. Do the i/o, wait 11247 * for it to complete, and cleanup the page list when done. 11248 */ 11249 static int 11250 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11251 int flags, cred_t *cr) 11252 { 11253 int error; 11254 11255 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11256 11257 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11258 if (flags & B_READ) 11259 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 11260 else 11261 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 11262 return (error); 11263 } 11264 11265 static int 11266 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 11267 int flags, cred_t *cr) 11268 { 11269 int error; 11270 rnode4_t *rp; 11271 11272 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone) 11273 return (EIO); 11274 11275 if (pp == NULL) 11276 return (EINVAL); 11277 11278 rp = VTOR4(vp); 11279 mutex_enter(&rp->r_statelock); 11280 rp->r_count++; 11281 mutex_exit(&rp->r_statelock); 11282 11283 if (flags & B_ASYNC) { 11284 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr, 11285 nfs4_sync_pageio); 11286 } else 11287 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 11288 mutex_enter(&rp->r_statelock); 11289 rp->r_count--; 11290 cv_broadcast(&rp->r_cv); 11291 mutex_exit(&rp->r_statelock); 11292 return (error); 11293 } 11294 11295 static void 11296 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr) 11297 { 11298 int error; 11299 rnode4_t *rp; 11300 page_t *plist; 11301 page_t *pptr; 11302 offset3 offset; 11303 count3 len; 11304 k_sigset_t smask; 11305 11306 /* 11307 * We should get called with fl equal to either B_FREE or 11308 * B_INVAL. Any other value is illegal. 11309 * 11310 * The page that we are either supposed to free or destroy 11311 * should be exclusive locked and its io lock should not 11312 * be held. 11313 */ 11314 ASSERT(fl == B_FREE || fl == B_INVAL); 11315 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 11316 11317 rp = VTOR4(vp); 11318 11319 /* 11320 * If the page doesn't need to be committed or we shouldn't 11321 * even bother attempting to commit it, then just make sure 11322 * that the p_fsdata byte is clear and then either free or 11323 * destroy the page as appropriate. 11324 */ 11325 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) { 11326 pp->p_fsdata = C_NOCOMMIT; 11327 if (fl == B_FREE) 11328 page_free(pp, dn); 11329 else 11330 page_destroy(pp, dn); 11331 return; 11332 } 11333 11334 /* 11335 * If there is a page invalidation operation going on, then 11336 * if this is one of the pages being destroyed, then just 11337 * clear the p_fsdata byte and then either free or destroy 11338 * the page as appropriate. 11339 */ 11340 mutex_enter(&rp->r_statelock); 11341 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 11342 mutex_exit(&rp->r_statelock); 11343 pp->p_fsdata = C_NOCOMMIT; 11344 if (fl == B_FREE) 11345 page_free(pp, dn); 11346 else 11347 page_destroy(pp, dn); 11348 return; 11349 } 11350 11351 /* 11352 * If we are freeing this page and someone else is already 11353 * waiting to do a commit, then just unlock the page and 11354 * return. That other thread will take care of commiting 11355 * this page. The page can be freed sometime after the 11356 * commit has finished. Otherwise, if the page is marked 11357 * as delay commit, then we may be getting called from 11358 * pvn_write_done, one page at a time. This could result 11359 * in one commit per page, so we end up doing lots of small 11360 * commits instead of fewer larger commits. This is bad, 11361 * we want do as few commits as possible. 11362 */ 11363 if (fl == B_FREE) { 11364 if (rp->r_flags & R4COMMITWAIT) { 11365 page_unlock(pp); 11366 mutex_exit(&rp->r_statelock); 11367 return; 11368 } 11369 if (pp->p_fsdata == C_DELAYCOMMIT) { 11370 pp->p_fsdata = C_COMMIT; 11371 page_unlock(pp); 11372 mutex_exit(&rp->r_statelock); 11373 return; 11374 } 11375 } 11376 11377 /* 11378 * Check to see if there is a signal which would prevent an 11379 * attempt to commit the pages from being successful. If so, 11380 * then don't bother with all of the work to gather pages and 11381 * generate the unsuccessful RPC. Just return from here and 11382 * let the page be committed at some later time. 11383 */ 11384 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT); 11385 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 11386 sigunintr(&smask); 11387 page_unlock(pp); 11388 mutex_exit(&rp->r_statelock); 11389 return; 11390 } 11391 sigunintr(&smask); 11392 11393 /* 11394 * We are starting to need to commit pages, so let's try 11395 * to commit as many as possible at once to reduce the 11396 * overhead. 11397 * 11398 * Set the `commit inprogress' state bit. We must 11399 * first wait until any current one finishes. Then 11400 * we initialize the c_pages list with this page. 11401 */ 11402 while (rp->r_flags & R4COMMIT) { 11403 rp->r_flags |= R4COMMITWAIT; 11404 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11405 rp->r_flags &= ~R4COMMITWAIT; 11406 } 11407 rp->r_flags |= R4COMMIT; 11408 mutex_exit(&rp->r_statelock); 11409 ASSERT(rp->r_commit.c_pages == NULL); 11410 rp->r_commit.c_pages = pp; 11411 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11412 rp->r_commit.c_commlen = PAGESIZE; 11413 11414 /* 11415 * Gather together all other pages which can be committed. 11416 * They will all be chained off r_commit.c_pages. 11417 */ 11418 nfs4_get_commit(vp); 11419 11420 /* 11421 * Clear the `commit inprogress' status and disconnect 11422 * the list of pages to be committed from the rnode. 11423 * At this same time, we also save the starting offset 11424 * and length of data to be committed on the server. 11425 */ 11426 plist = rp->r_commit.c_pages; 11427 rp->r_commit.c_pages = NULL; 11428 offset = rp->r_commit.c_commbase; 11429 len = rp->r_commit.c_commlen; 11430 mutex_enter(&rp->r_statelock); 11431 rp->r_flags &= ~R4COMMIT; 11432 cv_broadcast(&rp->r_commit.c_cv); 11433 mutex_exit(&rp->r_statelock); 11434 11435 if (curproc == proc_pageout || curproc == proc_fsflush || 11436 nfs_zone() != VTOMI4(vp)->mi_zone) { 11437 nfs4_async_commit(vp, plist, offset, len, 11438 cr, do_nfs4_async_commit); 11439 return; 11440 } 11441 11442 /* 11443 * Actually generate the COMMIT op over the wire operation. 11444 */ 11445 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr); 11446 11447 /* 11448 * If we got an error during the commit, just unlock all 11449 * of the pages. The pages will get retransmitted to the 11450 * server during a putpage operation. 11451 */ 11452 if (error) { 11453 while (plist != NULL) { 11454 pptr = plist; 11455 page_sub(&plist, pptr); 11456 page_unlock(pptr); 11457 } 11458 return; 11459 } 11460 11461 /* 11462 * We've tried as hard as we can to commit the data to stable 11463 * storage on the server. We just unlock the rest of the pages 11464 * and clear the commit required state. They will be put 11465 * onto the tail of the cachelist if they are nolonger 11466 * mapped. 11467 */ 11468 while (plist != pp) { 11469 pptr = plist; 11470 page_sub(&plist, pptr); 11471 pptr->p_fsdata = C_NOCOMMIT; 11472 page_unlock(pptr); 11473 } 11474 11475 /* 11476 * It is possible that nfs4_commit didn't return error but 11477 * some other thread has modified the page we are going 11478 * to free/destroy. 11479 * In this case we need to rewrite the page. Do an explicit check 11480 * before attempting to free/destroy the page. If modified, needs to 11481 * be rewritten so unlock the page and return. 11482 */ 11483 if (hat_ismod(pp)) { 11484 pp->p_fsdata = C_NOCOMMIT; 11485 page_unlock(pp); 11486 return; 11487 } 11488 11489 /* 11490 * Now, as appropriate, either free or destroy the page 11491 * that we were called with. 11492 */ 11493 pp->p_fsdata = C_NOCOMMIT; 11494 if (fl == B_FREE) 11495 page_free(pp, dn); 11496 else 11497 page_destroy(pp, dn); 11498 } 11499 11500 /* 11501 * Commit requires that the current fh be the file written to. 11502 * The compound op structure is: 11503 * PUTFH(file), COMMIT 11504 */ 11505 static int 11506 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr) 11507 { 11508 COMPOUND4args_clnt args; 11509 COMPOUND4res_clnt res; 11510 COMMIT4res *cm_res; 11511 nfs_argop4 argop[2]; 11512 nfs_resop4 *resop; 11513 int doqueue; 11514 mntinfo4_t *mi; 11515 rnode4_t *rp; 11516 cred_t *cred_otw = NULL; 11517 bool_t needrecov = FALSE; 11518 nfs4_recov_state_t recov_state; 11519 nfs4_open_stream_t *osp = NULL; 11520 bool_t first_time = TRUE; /* first time getting OTW cred */ 11521 bool_t last_time = FALSE; /* last time getting OTW cred */ 11522 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 11523 11524 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11525 11526 rp = VTOR4(vp); 11527 11528 mi = VTOMI4(vp); 11529 recov_state.rs_flags = 0; 11530 recov_state.rs_num_retry_despite_err = 0; 11531 get_commit_cred: 11532 /* 11533 * Releases the osp, if a valid open stream is provided. 11534 * Puts a hold on the cred_otw and the new osp (if found). 11535 */ 11536 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp, 11537 &first_time, &last_time); 11538 args.ctag = TAG_COMMIT; 11539 recov_retry: 11540 /* 11541 * Commit ops: putfh file; commit 11542 */ 11543 args.array_len = 2; 11544 args.array = argop; 11545 11546 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11547 &recov_state, NULL); 11548 if (e.error) { 11549 crfree(cred_otw); 11550 if (osp != NULL) 11551 open_stream_rele(osp, rp); 11552 return (e.error); 11553 } 11554 11555 /* putfh directory */ 11556 argop[0].argop = OP_CPUTFH; 11557 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 11558 11559 /* commit */ 11560 argop[1].argop = OP_COMMIT; 11561 argop[1].nfs_argop4_u.opcommit.offset = offset; 11562 argop[1].nfs_argop4_u.opcommit.count = count; 11563 11564 doqueue = 1; 11565 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e); 11566 11567 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp); 11568 if (!needrecov && e.error) { 11569 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, 11570 needrecov); 11571 crfree(cred_otw); 11572 if (e.error == EACCES && last_time == FALSE) 11573 goto get_commit_cred; 11574 if (osp != NULL) 11575 open_stream_rele(osp, rp); 11576 return (e.error); 11577 } 11578 11579 if (needrecov) { 11580 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 11581 NULL, OP_COMMIT, NULL) == FALSE) { 11582 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11583 &recov_state, needrecov); 11584 if (!e.error) 11585 (void) xdr_free(xdr_COMPOUND4res_clnt, 11586 (caddr_t)&res); 11587 goto recov_retry; 11588 } 11589 if (e.error) { 11590 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11591 &recov_state, needrecov); 11592 crfree(cred_otw); 11593 if (osp != NULL) 11594 open_stream_rele(osp, rp); 11595 return (e.error); 11596 } 11597 /* fall through for res.status case */ 11598 } 11599 11600 if (res.status) { 11601 e.error = geterrno4(res.status); 11602 if (e.error == EACCES && last_time == FALSE) { 11603 crfree(cred_otw); 11604 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11605 &recov_state, needrecov); 11606 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11607 goto get_commit_cred; 11608 } 11609 /* 11610 * Can't do a nfs4_purge_stale_fh here because this 11611 * can cause a deadlock. nfs4_commit can 11612 * be called from nfs4_dispose which can be called 11613 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh 11614 * can call back to pvn_vplist_dirty. 11615 */ 11616 if (e.error == ESTALE) { 11617 mutex_enter(&rp->r_statelock); 11618 rp->r_flags |= R4STALE; 11619 if (!rp->r_error) 11620 rp->r_error = e.error; 11621 mutex_exit(&rp->r_statelock); 11622 PURGE_ATTRCACHE4(vp); 11623 } else { 11624 mutex_enter(&rp->r_statelock); 11625 if (!rp->r_error) 11626 rp->r_error = e.error; 11627 mutex_exit(&rp->r_statelock); 11628 } 11629 } else { 11630 ASSERT(rp->r_flags & R4HAVEVERF); 11631 resop = &res.array[1]; /* commit res */ 11632 cm_res = &resop->nfs_resop4_u.opcommit; 11633 mutex_enter(&rp->r_statelock); 11634 if (cm_res->writeverf == rp->r_writeverf) { 11635 mutex_exit(&rp->r_statelock); 11636 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11637 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, 11638 &recov_state, needrecov); 11639 crfree(cred_otw); 11640 if (osp != NULL) 11641 open_stream_rele(osp, rp); 11642 return (0); 11643 } 11644 nfs4_set_mod(vp); 11645 rp->r_writeverf = cm_res->writeverf; 11646 mutex_exit(&rp->r_statelock); 11647 e.error = NFS_VERF_MISMATCH; 11648 } 11649 11650 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 11651 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov); 11652 crfree(cred_otw); 11653 if (osp != NULL) 11654 open_stream_rele(osp, rp); 11655 11656 return (e.error); 11657 } 11658 11659 static void 11660 nfs4_set_mod(vnode_t *vp) 11661 { 11662 page_t *pp; 11663 kmutex_t *vphm; 11664 rnode4_t *rp; 11665 11666 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11667 11668 /* make sure we're looking at the master vnode, not a shadow */ 11669 11670 rp = VTOR4(vp); 11671 if (IS_SHADOW(vp, rp)) 11672 vp = RTOV4(rp); 11673 11674 vphm = page_vnode_mutex(vp); 11675 mutex_enter(vphm); 11676 /* 11677 * If there are no pages associated with this vnode, then 11678 * just return. 11679 */ 11680 if ((pp = vp->v_pages) == NULL) { 11681 mutex_exit(vphm); 11682 return; 11683 } 11684 11685 do { 11686 if (pp->p_fsdata != C_NOCOMMIT) { 11687 hat_setmod(pp); 11688 pp->p_fsdata = C_NOCOMMIT; 11689 } 11690 } while ((pp = pp->p_vpnext) != vp->v_pages); 11691 mutex_exit(vphm); 11692 } 11693 11694 /* 11695 * This function is used to gather a page list of the pages which 11696 * can be committed on the server. 11697 * 11698 * The calling thread must have set R4COMMIT. This bit is used to 11699 * serialize access to the commit structure in the rnode. As long 11700 * as the thread has set R4COMMIT, then it can manipulate the commit 11701 * structure without requiring any other locks. 11702 * 11703 * When this function is called from nfs4_dispose() the page passed 11704 * into nfs4_dispose() will be SE_EXCL locked, and so this function 11705 * will skip it. This is not a problem since we initially add the 11706 * page to the r_commit page list. 11707 * 11708 */ 11709 static void 11710 nfs4_get_commit(vnode_t *vp) 11711 { 11712 rnode4_t *rp; 11713 page_t *pp; 11714 kmutex_t *vphm; 11715 11716 rp = VTOR4(vp); 11717 11718 ASSERT(rp->r_flags & R4COMMIT); 11719 11720 /* make sure we're looking at the master vnode, not a shadow */ 11721 11722 if (IS_SHADOW(vp, rp)) 11723 vp = RTOV4(rp); 11724 11725 vphm = page_vnode_mutex(vp); 11726 mutex_enter(vphm); 11727 11728 /* 11729 * If there are no pages associated with this vnode, then 11730 * just return. 11731 */ 11732 if ((pp = vp->v_pages) == NULL) { 11733 mutex_exit(vphm); 11734 return; 11735 } 11736 11737 /* 11738 * Step through all of the pages associated with this vnode 11739 * looking for pages which need to be committed. 11740 */ 11741 do { 11742 /* 11743 * First short-cut everything (without the page_lock) 11744 * and see if this page does not need to be committed 11745 * or is modified if so then we'll just skip it. 11746 */ 11747 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 11748 continue; 11749 11750 /* 11751 * Attempt to lock the page. If we can't, then 11752 * someone else is messing with it or we have been 11753 * called from nfs4_dispose and this is the page that 11754 * nfs4_dispose was called with.. anyway just skip it. 11755 */ 11756 if (!page_trylock(pp, SE_EXCL)) 11757 continue; 11758 11759 /* 11760 * Lets check again now that we have the page lock. 11761 */ 11762 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11763 page_unlock(pp); 11764 continue; 11765 } 11766 11767 /* this had better not be a free page */ 11768 ASSERT(PP_ISFREE(pp) == 0); 11769 11770 /* 11771 * The page needs to be committed and we locked it. 11772 * Update the base and length parameters and add it 11773 * to r_pages. 11774 */ 11775 if (rp->r_commit.c_pages == NULL) { 11776 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11777 rp->r_commit.c_commlen = PAGESIZE; 11778 } else if (pp->p_offset < rp->r_commit.c_commbase) { 11779 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 11780 (offset3)pp->p_offset + rp->r_commit.c_commlen; 11781 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11782 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 11783 <= pp->p_offset) { 11784 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11785 rp->r_commit.c_commbase + PAGESIZE; 11786 } 11787 page_add(&rp->r_commit.c_pages, pp); 11788 } while ((pp = pp->p_vpnext) != vp->v_pages); 11789 11790 mutex_exit(vphm); 11791 } 11792 11793 /* 11794 * This routine is used to gather together a page list of the pages 11795 * which are to be committed on the server. This routine must not 11796 * be called if the calling thread holds any locked pages. 11797 * 11798 * The calling thread must have set R4COMMIT. This bit is used to 11799 * serialize access to the commit structure in the rnode. As long 11800 * as the thread has set R4COMMIT, then it can manipulate the commit 11801 * structure without requiring any other locks. 11802 */ 11803 static void 11804 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 11805 { 11806 11807 rnode4_t *rp; 11808 page_t *pp; 11809 u_offset_t end; 11810 u_offset_t off; 11811 ASSERT(len != 0); 11812 rp = VTOR4(vp); 11813 ASSERT(rp->r_flags & R4COMMIT); 11814 11815 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11816 11817 /* make sure we're looking at the master vnode, not a shadow */ 11818 11819 if (IS_SHADOW(vp, rp)) 11820 vp = RTOV4(rp); 11821 11822 /* 11823 * If there are no pages associated with this vnode, then 11824 * just return. 11825 */ 11826 if ((pp = vp->v_pages) == NULL) 11827 return; 11828 /* 11829 * Calculate the ending offset. 11830 */ 11831 end = soff + len; 11832 for (off = soff; off < end; off += PAGESIZE) { 11833 /* 11834 * Lookup each page by vp, offset. 11835 */ 11836 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 11837 continue; 11838 /* 11839 * If this page does not need to be committed or is 11840 * modified, then just skip it. 11841 */ 11842 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 11843 page_unlock(pp); 11844 continue; 11845 } 11846 11847 ASSERT(PP_ISFREE(pp) == 0); 11848 /* 11849 * The page needs to be committed and we locked it. 11850 * Update the base and length parameters and add it 11851 * to r_pages. 11852 */ 11853 if (rp->r_commit.c_pages == NULL) { 11854 rp->r_commit.c_commbase = (offset3)pp->p_offset; 11855 rp->r_commit.c_commlen = PAGESIZE; 11856 } else { 11857 rp->r_commit.c_commlen = (offset3)pp->p_offset - 11858 rp->r_commit.c_commbase + PAGESIZE; 11859 } 11860 page_add(&rp->r_commit.c_pages, pp); 11861 } 11862 } 11863 11864 /* 11865 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap(). 11866 * Flushes and commits data to the server. 11867 */ 11868 static int 11869 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 11870 { 11871 int error; 11872 verifier4 write_verf; 11873 rnode4_t *rp = VTOR4(vp); 11874 11875 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11876 11877 /* 11878 * Flush the data portion of the file and then commit any 11879 * portions which need to be committed. This may need to 11880 * be done twice if the server has changed state since 11881 * data was last written. The data will need to be 11882 * rewritten to the server and then a new commit done. 11883 * 11884 * In fact, this may need to be done several times if the 11885 * server is having problems and crashing while we are 11886 * attempting to do this. 11887 */ 11888 11889 top: 11890 /* 11891 * Do a flush based on the poff and plen arguments. This 11892 * will synchronously write out any modified pages in the 11893 * range specified by (poff, plen). This starts all of the 11894 * i/o operations which will be waited for in the next 11895 * call to nfs4_putpage 11896 */ 11897 11898 mutex_enter(&rp->r_statelock); 11899 write_verf = rp->r_writeverf; 11900 mutex_exit(&rp->r_statelock); 11901 11902 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr); 11903 if (error == EAGAIN) 11904 error = 0; 11905 11906 /* 11907 * Do a flush based on the poff and plen arguments. This 11908 * will synchronously write out any modified pages in the 11909 * range specified by (poff, plen) and wait until all of 11910 * the asynchronous i/o's in that range are done as well. 11911 */ 11912 if (!error) 11913 error = nfs4_putpage(vp, poff, plen, 0, cr); 11914 11915 if (error) 11916 return (error); 11917 11918 mutex_enter(&rp->r_statelock); 11919 if (rp->r_writeverf != write_verf) { 11920 mutex_exit(&rp->r_statelock); 11921 goto top; 11922 } 11923 mutex_exit(&rp->r_statelock); 11924 11925 /* 11926 * Now commit any pages which might need to be committed. 11927 * If the error, NFS_VERF_MISMATCH, is returned, then 11928 * start over with the flush operation. 11929 */ 11930 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT); 11931 11932 if (error == NFS_VERF_MISMATCH) 11933 goto top; 11934 11935 return (error); 11936 } 11937 11938 /* 11939 * nfs4_commit_vp() will wait for other pending commits and 11940 * will either commit the whole file or a range, plen dictates 11941 * if we commit whole file. a value of zero indicates the whole 11942 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage() 11943 */ 11944 static int 11945 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, 11946 cred_t *cr, int wait_on_writes) 11947 { 11948 rnode4_t *rp; 11949 page_t *plist; 11950 offset3 offset; 11951 count3 len; 11952 11953 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 11954 11955 rp = VTOR4(vp); 11956 11957 /* 11958 * before we gather commitable pages make 11959 * sure there are no outstanding async writes 11960 */ 11961 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) { 11962 mutex_enter(&rp->r_statelock); 11963 while (rp->r_count > 0) { 11964 cv_wait(&rp->r_cv, &rp->r_statelock); 11965 } 11966 mutex_exit(&rp->r_statelock); 11967 } 11968 11969 /* 11970 * Set the `commit inprogress' state bit. We must 11971 * first wait until any current one finishes. 11972 */ 11973 mutex_enter(&rp->r_statelock); 11974 while (rp->r_flags & R4COMMIT) { 11975 rp->r_flags |= R4COMMITWAIT; 11976 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 11977 rp->r_flags &= ~R4COMMITWAIT; 11978 } 11979 rp->r_flags |= R4COMMIT; 11980 mutex_exit(&rp->r_statelock); 11981 11982 /* 11983 * Gather all of the pages which need to be 11984 * committed. 11985 */ 11986 if (plen == 0) 11987 nfs4_get_commit(vp); 11988 else 11989 nfs4_get_commit_range(vp, poff, plen); 11990 11991 /* 11992 * Clear the `commit inprogress' bit and disconnect the 11993 * page list which was gathered by nfs4_get_commit. 11994 */ 11995 plist = rp->r_commit.c_pages; 11996 rp->r_commit.c_pages = NULL; 11997 offset = rp->r_commit.c_commbase; 11998 len = rp->r_commit.c_commlen; 11999 mutex_enter(&rp->r_statelock); 12000 rp->r_flags &= ~R4COMMIT; 12001 cv_broadcast(&rp->r_commit.c_cv); 12002 mutex_exit(&rp->r_statelock); 12003 12004 /* 12005 * If any pages need to be committed, commit them and 12006 * then unlock them so that they can be freed some 12007 * time later. 12008 */ 12009 if (plist == NULL) 12010 return (0); 12011 12012 /* 12013 * No error occurred during the flush portion 12014 * of this operation, so now attempt to commit 12015 * the data to stable storage on the server. 12016 * 12017 * This will unlock all of the pages on the list. 12018 */ 12019 return (nfs4_sync_commit(vp, plist, offset, len, cr)); 12020 } 12021 12022 static int 12023 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12024 cred_t *cr) 12025 { 12026 int error; 12027 page_t *pp; 12028 12029 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12030 12031 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr); 12032 12033 /* 12034 * If we got an error, then just unlock all of the pages 12035 * on the list. 12036 */ 12037 if (error) { 12038 while (plist != NULL) { 12039 pp = plist; 12040 page_sub(&plist, pp); 12041 page_unlock(pp); 12042 } 12043 return (error); 12044 } 12045 /* 12046 * We've tried as hard as we can to commit the data to stable 12047 * storage on the server. We just unlock the pages and clear 12048 * the commit required state. They will get freed later. 12049 */ 12050 while (plist != NULL) { 12051 pp = plist; 12052 page_sub(&plist, pp); 12053 pp->p_fsdata = C_NOCOMMIT; 12054 page_unlock(pp); 12055 } 12056 12057 return (error); 12058 } 12059 12060 static void 12061 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 12062 cred_t *cr) 12063 { 12064 12065 (void) nfs4_sync_commit(vp, plist, offset, count, cr); 12066 } 12067 12068 /*ARGSUSED*/ 12069 static int 12070 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 12071 { 12072 int error = 0; 12073 mntinfo4_t *mi; 12074 vattr_t va; 12075 vsecattr_t nfsace4_vsap; 12076 12077 mi = VTOMI4(vp); 12078 if (nfs_zone() != mi->mi_zone) 12079 return (EIO); 12080 if (mi->mi_flags & MI4_ACL) { 12081 /* if we have a delegation, return it */ 12082 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE) 12083 (void) nfs4delegreturn(VTOR4(vp), 12084 NFS4_DR_REOPEN|NFS4_DR_PUSH); 12085 12086 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, 12087 NFS4_ACL_SET); 12088 if (error) /* EINVAL */ 12089 return (error); 12090 12091 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) { 12092 /* 12093 * These are aclent_t type entries. 12094 */ 12095 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap, 12096 vp->v_type == VDIR, FALSE); 12097 if (error) 12098 return (error); 12099 } else { 12100 /* 12101 * These are ace_t type entries. 12102 */ 12103 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap, 12104 FALSE); 12105 if (error) 12106 return (error); 12107 } 12108 bzero(&va, sizeof (va)); 12109 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap); 12110 vs_ace4_destroy(&nfsace4_vsap); 12111 return (error); 12112 } 12113 return (ENOSYS); 12114 } 12115 12116 int 12117 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 12118 { 12119 int error; 12120 mntinfo4_t *mi; 12121 nfs4_ga_res_t gar; 12122 rnode4_t *rp = VTOR4(vp); 12123 12124 mi = VTOMI4(vp); 12125 if (nfs_zone() != mi->mi_zone) 12126 return (EIO); 12127 12128 bzero(&gar, sizeof (gar)); 12129 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask; 12130 12131 /* 12132 * vsecattr->vsa_mask holds the original acl request mask. 12133 * This is needed when determining what to return. 12134 * (See: nfs4_create_getsecattr_return()) 12135 */ 12136 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET); 12137 if (error) /* EINVAL */ 12138 return (error); 12139 12140 if (mi->mi_flags & MI4_ACL) { 12141 /* 12142 * Check if the data is cached and the cache is valid. If it 12143 * is we don't go over the wire. 12144 */ 12145 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) { 12146 mutex_enter(&rp->r_statelock); 12147 if (rp->r_secattr != NULL) { 12148 error = nfs4_create_getsecattr_return( 12149 rp->r_secattr, vsecattr, rp->r_attr.va_uid, 12150 rp->r_attr.va_gid, 12151 vp->v_type == VDIR); 12152 if (!error) { /* error == 0 - Success! */ 12153 mutex_exit(&rp->r_statelock); 12154 return (error); 12155 } 12156 } 12157 mutex_exit(&rp->r_statelock); 12158 } 12159 12160 /* 12161 * The getattr otw call will always get both the acl, in 12162 * the form of a list of nfsace4's, and the number of acl 12163 * entries; independent of the value of gar.n4g_vsa.vsa_mask. 12164 */ 12165 gar.n4g_va.va_mask = AT_ALL; 12166 error = nfs4_getattr_otw(vp, &gar, cr, 1); 12167 if (error) { 12168 vs_ace4_destroy(&gar.n4g_vsa); 12169 if (error == ENOTSUP || error == EOPNOTSUPP) 12170 error = fs_fab_acl(vp, vsecattr, flag, cr); 12171 return (error); 12172 } 12173 12174 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) { 12175 /* 12176 * No error was returned, but according to the response 12177 * bitmap, neither was an acl. 12178 */ 12179 vs_ace4_destroy(&gar.n4g_vsa); 12180 error = fs_fab_acl(vp, vsecattr, flag, cr); 12181 return (error); 12182 } 12183 12184 /* 12185 * Update the cache with the ACL. 12186 */ 12187 nfs4_acl_fill_cache(rp, &gar.n4g_vsa); 12188 12189 error = nfs4_create_getsecattr_return(&gar.n4g_vsa, 12190 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid, 12191 vp->v_type == VDIR); 12192 vs_ace4_destroy(&gar.n4g_vsa); 12193 if ((error) && (vsecattr->vsa_mask & 12194 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) && 12195 (error != EACCES)) { 12196 error = fs_fab_acl(vp, vsecattr, flag, cr); 12197 } 12198 return (error); 12199 } 12200 error = fs_fab_acl(vp, vsecattr, flag, cr); 12201 return (error); 12202 } 12203 12204 /* 12205 * The function returns: 12206 * - 0 (zero) if the passed in "acl_mask" is a valid request. 12207 * - EINVAL if the passed in "acl_mask" is an invalid request. 12208 * 12209 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if: 12210 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12211 * 12212 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if: 12213 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE) 12214 * - We have a count field set without the corresponding acl field set. (e.g. - 12215 * VSA_ACECNT is set, but VSA_ACE is not) 12216 */ 12217 static int 12218 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op) 12219 { 12220 /* Shortcut the masks that are always valid. */ 12221 if (acl_mask == (VSA_ACE | VSA_ACECNT)) 12222 return (0); 12223 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) 12224 return (0); 12225 12226 if (acl_mask & (VSA_ACE | VSA_ACECNT)) { 12227 /* 12228 * We can't have any VSA_ACL type stuff in the mask now. 12229 */ 12230 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12231 VSA_DFACLCNT)) 12232 return (EINVAL); 12233 12234 if (op == NFS4_ACL_SET) { 12235 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE)) 12236 return (EINVAL); 12237 } 12238 } 12239 12240 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) { 12241 /* 12242 * We can't have any VSA_ACE type stuff in the mask now. 12243 */ 12244 if (acl_mask & (VSA_ACE | VSA_ACECNT)) 12245 return (EINVAL); 12246 12247 if (op == NFS4_ACL_SET) { 12248 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL)) 12249 return (EINVAL); 12250 12251 if ((acl_mask & VSA_DFACLCNT) && 12252 !(acl_mask & VSA_DFACL)) 12253 return (EINVAL); 12254 } 12255 } 12256 return (0); 12257 } 12258 12259 /* 12260 * The theory behind creating the correct getsecattr return is simply this: 12261 * "Don't return anything that the caller is not expecting to have to free." 12262 */ 12263 static int 12264 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap, 12265 uid_t uid, gid_t gid, int isdir) 12266 { 12267 int error = 0; 12268 /* Save the mask since the translators modify it. */ 12269 uint_t orig_mask = vsap->vsa_mask; 12270 12271 if (orig_mask & (VSA_ACE | VSA_ACECNT)) { 12272 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, 12273 FALSE, ((orig_mask & VSA_ACE) ? FALSE : TRUE)); 12274 12275 if (error) 12276 return (error); 12277 12278 /* 12279 * If the caller only asked for the ace count (VSA_ACECNT) 12280 * don't give them the full acl (VSA_ACE), free it. 12281 */ 12282 if (!orig_mask & VSA_ACE) { 12283 if (vsap->vsa_aclentp != NULL) { 12284 kmem_free(vsap->vsa_aclentp, 12285 vsap->vsa_aclcnt * sizeof (ace_t)); 12286 vsap->vsa_aclentp = NULL; 12287 } 12288 } 12289 vsap->vsa_mask = orig_mask; 12290 12291 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | 12292 VSA_DFACLCNT)) { 12293 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid, 12294 isdir, FALSE, 12295 ((orig_mask & (VSA_ACL | VSA_DFACL)) ? FALSE : TRUE)); 12296 12297 if (error) 12298 return (error); 12299 12300 /* 12301 * If the caller only asked for the acl count (VSA_ACLCNT) 12302 * and/or the default acl count (VSA_DFACLCNT) don't give them 12303 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it. 12304 */ 12305 if (!orig_mask & VSA_ACL) { 12306 if (vsap->vsa_aclentp != NULL) { 12307 kmem_free(vsap->vsa_aclentp, 12308 vsap->vsa_aclcnt * sizeof (aclent_t)); 12309 vsap->vsa_aclentp = NULL; 12310 } 12311 } 12312 12313 if (!orig_mask & VSA_DFACL) { 12314 if (vsap->vsa_dfaclentp != NULL) { 12315 kmem_free(vsap->vsa_dfaclentp, 12316 vsap->vsa_dfaclcnt * sizeof (aclent_t)); 12317 vsap->vsa_dfaclentp = NULL; 12318 } 12319 } 12320 vsap->vsa_mask = orig_mask; 12321 } 12322 return (0); 12323 } 12324 12325 static int 12326 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr) 12327 { 12328 int error; 12329 12330 if (nfs_zone() != VTOMI4(vp)->mi_zone) 12331 return (EIO); 12332 /* 12333 * check for valid cmd parameter 12334 */ 12335 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 12336 return (EINVAL); 12337 12338 /* 12339 * Check access permissions 12340 */ 12341 if ((cmd & F_SHARE) && 12342 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) || 12343 (shr->s_access == F_WRACC && (flag & FWRITE) == 0))) 12344 return (EBADF); 12345 12346 /* 12347 * If the filesystem is mounted using local locking, pass the 12348 * request off to the local share code. 12349 */ 12350 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) 12351 return (fs_shrlock(vp, cmd, shr, flag, cr)); 12352 12353 switch (cmd) { 12354 case F_SHARE: 12355 case F_UNSHARE: 12356 /* 12357 * This will be properly implemented later, 12358 * see RFE: 4823948 . 12359 */ 12360 error = EAGAIN; 12361 break; 12362 12363 case F_HASREMOTELOCKS: 12364 /* 12365 * NFS client can't store remote locks itself 12366 */ 12367 shr->s_access = 0; 12368 error = 0; 12369 break; 12370 12371 default: 12372 error = EINVAL; 12373 break; 12374 } 12375 12376 return (error); 12377 } 12378 12379 /* 12380 * Common code called by directory ops to update the attrcache 12381 */ 12382 static int 12383 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp, 12384 hrtime_t t, vnode_t *vp, cred_t *cr) 12385 { 12386 int error = 0; 12387 12388 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12389 12390 if (status != NFS4_OK) { 12391 /* getattr not done or failed */ 12392 PURGE_ATTRCACHE4(vp); 12393 return (error); 12394 } 12395 12396 if (garp) { 12397 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL); 12398 } else { 12399 PURGE_ATTRCACHE4(vp); 12400 } 12401 return (error); 12402 } 12403 12404 /* 12405 * Update directory caches for directory modification ops (link, rename, etc.) 12406 * When dinfo is NULL, manage dircaches in the old way. 12407 */ 12408 static void 12409 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm, 12410 dirattr_info_t *dinfo) 12411 { 12412 rnode4_t *drp = VTOR4(dvp); 12413 12414 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone); 12415 12416 /* Purge rddir cache for dir since it changed */ 12417 if (drp->r_dir != NULL) 12418 nfs4_purge_rddir_cache(dvp); 12419 12420 /* 12421 * If caller provided dinfo, then use it to manage dir caches. 12422 */ 12423 if (dinfo != NULL) { 12424 if (vp != NULL) { 12425 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12426 if (!VTOR4(vp)->created_v4) { 12427 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12428 dnlc_update(dvp, nm, vp); 12429 } else { 12430 /* 12431 * XXX don't update if the created_v4 flag is 12432 * set 12433 */ 12434 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12435 NFS4_DEBUG(nfs4_client_state_debug, 12436 (CE_NOTE, "nfs4_update_dircaches: " 12437 "don't update dnlc: created_v4 flag")); 12438 } 12439 } 12440 12441 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call, 12442 dinfo->di_cred, FALSE, cinfo); 12443 12444 return; 12445 } 12446 12447 /* 12448 * Caller didn't provide dinfo, then check change_info4 to update DNLC. 12449 * Since caller modified dir but didn't receive post-dirmod-op dir 12450 * attrs, the dir's attrs must be purged. 12451 * 12452 * XXX this check and dnlc update/purge should really be atomic, 12453 * XXX but can't use rnode statelock because it'll deadlock in 12454 * XXX dnlc_purge_vp, however, the risk is minimal even if a race 12455 * XXX does occur. 12456 * 12457 * XXX We also may want to check that atomic is true in the 12458 * XXX change_info struct. If it is not, the change_info may 12459 * XXX reflect changes by more than one clients which means that 12460 * XXX our cache may not be valid. 12461 */ 12462 PURGE_ATTRCACHE4(dvp); 12463 if (drp->r_change == cinfo->before) { 12464 /* no changes took place in the directory prior to our link */ 12465 if (vp != NULL) { 12466 mutex_enter(&VTOR4(vp)->r_statev4_lock); 12467 if (!VTOR4(vp)->created_v4) { 12468 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12469 dnlc_update(dvp, nm, vp); 12470 } else { 12471 /* 12472 * XXX dont' update if the created_v4 flag 12473 * is set 12474 */ 12475 mutex_exit(&VTOR4(vp)->r_statev4_lock); 12476 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, 12477 "nfs4_update_dircaches: don't" 12478 " update dnlc: created_v4 flag")); 12479 } 12480 } 12481 } else { 12482 /* Another client modified directory - purge its dnlc cache */ 12483 dnlc_purge_vp(dvp); 12484 } 12485 } 12486 12487 /* 12488 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a 12489 * file. 12490 * 12491 * The 'reopening_file' boolean should be set to TRUE if we are reopening this 12492 * file (ie: client recovery) and otherwise set to FALSE. 12493 * 12494 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery 12495 * initiated) calling functions. 12496 * 12497 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result 12498 * of resending a 'lost' open request. 12499 * 12500 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken 12501 * server that hands out BAD_SEQID on open confirm. 12502 * 12503 * Errors are returned via the nfs4_error_t parameter. 12504 */ 12505 void 12506 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr, 12507 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop, 12508 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp) 12509 { 12510 COMPOUND4args_clnt args; 12511 COMPOUND4res_clnt res; 12512 nfs_argop4 argop[2]; 12513 nfs_resop4 *resop; 12514 int doqueue = 1; 12515 mntinfo4_t *mi; 12516 OPEN_CONFIRM4args *open_confirm_args; 12517 int needrecov; 12518 12519 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12520 #if DEBUG 12521 mutex_enter(&oop->oo_lock); 12522 ASSERT(oop->oo_seqid_inuse); 12523 mutex_exit(&oop->oo_lock); 12524 #endif 12525 12526 recov_retry_confirm: 12527 nfs4_error_zinit(ep); 12528 *retry_open = FALSE; 12529 12530 if (resend) 12531 args.ctag = TAG_OPEN_CONFIRM_LOST; 12532 else 12533 args.ctag = TAG_OPEN_CONFIRM; 12534 12535 args.array_len = 2; 12536 args.array = argop; 12537 12538 /* putfh target fh */ 12539 argop[0].argop = OP_CPUTFH; 12540 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh; 12541 12542 argop[1].argop = OP_OPEN_CONFIRM; 12543 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm; 12544 12545 (*seqid) += 1; 12546 open_confirm_args->seqid = *seqid; 12547 open_confirm_args->open_stateid = *stateid; 12548 12549 mi = VTOMI4(vp); 12550 12551 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep); 12552 12553 if (!ep->error && nfs4_need_to_bump_seqid(&res)) { 12554 nfs4_set_open_seqid((*seqid), oop, args.ctag); 12555 } 12556 12557 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp); 12558 if (!needrecov && ep->error) 12559 return; 12560 12561 if (needrecov) { 12562 bool_t abort = FALSE; 12563 12564 if (reopening_file == FALSE) { 12565 nfs4_bseqid_entry_t *bsep = NULL; 12566 12567 if (!ep->error && res.status == NFS4ERR_BAD_SEQID) 12568 bsep = nfs4_create_bseqid_entry(oop, NULL, 12569 vp, 0, args.ctag, 12570 open_confirm_args->seqid); 12571 12572 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, 12573 NULL, NULL, NULL, OP_OPEN_CONFIRM, bsep); 12574 if (bsep) { 12575 kmem_free(bsep, sizeof (*bsep)); 12576 if (num_bseqid_retryp && 12577 --(*num_bseqid_retryp) == 0) 12578 abort = TRUE; 12579 } 12580 } 12581 if ((ep->error == ETIMEDOUT || 12582 res.status == NFS4ERR_RESOURCE) && 12583 abort == FALSE && resend == FALSE) { 12584 if (!ep->error) 12585 (void) xdr_free(xdr_COMPOUND4res_clnt, 12586 (caddr_t)&res); 12587 12588 delay(SEC_TO_TICK(confirm_retry_sec)); 12589 goto recov_retry_confirm; 12590 } 12591 /* State may have changed so retry the entire OPEN op */ 12592 if (abort == FALSE) 12593 *retry_open = TRUE; 12594 else 12595 *retry_open = FALSE; 12596 if (!ep->error) 12597 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12598 return; 12599 } 12600 12601 if (res.status) { 12602 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12603 return; 12604 } 12605 12606 resop = &res.array[1]; /* open confirm res */ 12607 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid, 12608 stateid, sizeof (*stateid)); 12609 12610 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res); 12611 } 12612 12613 /* 12614 * Return the credentials associated with a client state object. The 12615 * caller is responsible for freeing the credentials. 12616 */ 12617 12618 static cred_t * 12619 state_to_cred(nfs4_open_stream_t *osp) 12620 { 12621 cred_t *cr; 12622 12623 /* 12624 * It's ok to not lock the open stream and open owner to get 12625 * the oo_cred since this is only written once (upon creation) 12626 * and will not change. 12627 */ 12628 cr = osp->os_open_owner->oo_cred; 12629 crhold(cr); 12630 12631 return (cr); 12632 } 12633 12634 /* 12635 * nfs4_find_sysid 12636 * 12637 * Find the sysid for the knetconfig associated with the given mi. 12638 */ 12639 static struct lm_sysid * 12640 nfs4_find_sysid(mntinfo4_t *mi) 12641 { 12642 ASSERT(nfs_zone() == mi->mi_zone); 12643 12644 /* 12645 * Switch from RDMA knconf to original mount knconf 12646 */ 12647 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr, 12648 mi->mi_curr_serv->sv_hostname, NULL)); 12649 } 12650 12651 #ifdef DEBUG 12652 /* 12653 * Return a string version of the call type for easy reading. 12654 */ 12655 static char * 12656 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype) 12657 { 12658 switch (ctype) { 12659 case NFS4_LCK_CTYPE_NORM: 12660 return ("NORMAL"); 12661 case NFS4_LCK_CTYPE_RECLAIM: 12662 return ("RECLAIM"); 12663 case NFS4_LCK_CTYPE_RESEND: 12664 return ("RESEND"); 12665 case NFS4_LCK_CTYPE_REINSTATE: 12666 return ("REINSTATE"); 12667 default: 12668 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal " 12669 "type %d", ctype); 12670 return (""); 12671 } 12672 } 12673 #endif 12674 12675 /* 12676 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type 12677 * Unlock requests don't have an over-the-wire locktype, so we just return 12678 * something non-threatening. 12679 */ 12680 12681 static nfs_lock_type4 12682 flk_to_locktype(int cmd, int l_type) 12683 { 12684 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK); 12685 12686 switch (l_type) { 12687 case F_UNLCK: 12688 return (READ_LT); 12689 case F_RDLCK: 12690 if (cmd == F_SETLK) 12691 return (READ_LT); 12692 else 12693 return (READW_LT); 12694 case F_WRLCK: 12695 if (cmd == F_SETLK) 12696 return (WRITE_LT); 12697 else 12698 return (WRITEW_LT); 12699 } 12700 panic("flk_to_locktype"); 12701 /*NOTREACHED*/ 12702 } 12703 12704 /* 12705 * Do some preliminary checks for nfs4frlock. 12706 */ 12707 static int 12708 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp, 12709 u_offset_t offset) 12710 { 12711 int error = 0; 12712 12713 /* 12714 * If we are setting a lock, check that the file is opened 12715 * with the correct mode. 12716 */ 12717 if (cmd == F_SETLK || cmd == F_SETLKW) { 12718 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) || 12719 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) { 12720 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12721 "nfs4frlock_validate_args: file was opened with " 12722 "incorrect mode")); 12723 return (EBADF); 12724 } 12725 } 12726 12727 /* Convert the offset. It may need to be restored before returning. */ 12728 if (error = convoff(vp, flk, 0, offset)) { 12729 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12730 "nfs4frlock_validate_args: convoff => error= %d\n", 12731 error)); 12732 return (error); 12733 } 12734 12735 return (error); 12736 } 12737 12738 /* 12739 * Set the flock64's lm_sysid for nfs4frlock. 12740 */ 12741 static int 12742 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk) 12743 { 12744 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12745 12746 /* Find the lm_sysid */ 12747 *lspp = nfs4_find_sysid(VTOMI4(vp)); 12748 12749 if (*lspp == NULL) { 12750 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 12751 "nfs4frlock_get_sysid: no sysid, return ENOLCK")); 12752 return (ENOLCK); 12753 } 12754 12755 flk->l_sysid = lm_sysidt(*lspp); 12756 12757 return (0); 12758 } 12759 12760 /* 12761 * Do the remaining preliminary setup for nfs4frlock. 12762 */ 12763 static void 12764 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep, 12765 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr, 12766 cred_t **cred_otw) 12767 { 12768 /* 12769 * set tick_delay to the base delay time. 12770 * (NFS4_BASE_WAIT_TIME is in secs) 12771 */ 12772 12773 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000); 12774 12775 /* 12776 * If lock is relative to EOF, we need the newest length of the 12777 * file. Therefore invalidate the ATTR_CACHE. 12778 */ 12779 12780 *whencep = flk->l_whence; 12781 12782 if (*whencep == 2) /* SEEK_END */ 12783 PURGE_ATTRCACHE4(vp); 12784 12785 recov_statep->rs_flags = 0; 12786 recov_statep->rs_num_retry_despite_err = 0; 12787 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL); 12788 } 12789 12790 /* 12791 * Initialize and allocate the data structures necessary for 12792 * the nfs4frlock call. 12793 * Allocates argsp's op array, frees up the saved_rqstpp if there is one. 12794 */ 12795 static void 12796 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp, 12797 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd, 12798 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp, 12799 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp) 12800 { 12801 int argoplist_size; 12802 int num_ops = 2; 12803 12804 *retry = FALSE; 12805 *did_start_fop = FALSE; 12806 *skip_get_err = FALSE; 12807 lost_rqstp->lr_op = 0; 12808 argoplist_size = num_ops * sizeof (nfs_argop4); 12809 /* fill array with zero */ 12810 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP); 12811 12812 *argspp = argsp; 12813 *respp = NULL; 12814 12815 argsp->array_len = num_ops; 12816 argsp->array = *argopp; 12817 12818 /* initialize in case of error; will get real value down below */ 12819 argsp->ctag = TAG_NONE; 12820 12821 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) 12822 *op_hintp = OH_LOCKU; 12823 else 12824 *op_hintp = OH_OTHER; 12825 } 12826 12827 /* 12828 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign 12829 * the proper nfs4_server_t for this instance of nfs4frlock. 12830 * Returns 0 (success) or an errno value. 12831 */ 12832 static int 12833 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp, 12834 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep, 12835 bool_t *did_start_fop, bool_t *startrecovp) 12836 { 12837 int error = 0; 12838 rnode4_t *rp; 12839 12840 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 12841 12842 if (ctype == NFS4_LCK_CTYPE_NORM) { 12843 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint, 12844 recov_statep, startrecovp); 12845 if (error) 12846 return (error); 12847 *did_start_fop = TRUE; 12848 } else { 12849 *did_start_fop = FALSE; 12850 *startrecovp = FALSE; 12851 } 12852 12853 if (!error) { 12854 rp = VTOR4(vp); 12855 12856 /* If the file failed recovery, just quit. */ 12857 mutex_enter(&rp->r_statelock); 12858 if (rp->r_flags & R4RECOVERR) { 12859 error = EIO; 12860 } 12861 mutex_exit(&rp->r_statelock); 12862 } 12863 12864 return (error); 12865 } 12866 12867 /* 12868 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A 12869 * resend nfs4frlock call is initiated by the recovery framework. 12870 * Acquires the lop and oop seqid synchronization. 12871 */ 12872 static void 12873 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp, 12874 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp, 12875 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 12876 LOCK4args **lock_argsp, LOCKU4args **locku_argsp) 12877 { 12878 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp); 12879 int error; 12880 12881 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug), 12882 (CE_NOTE, 12883 "nfs4frlock_setup_resend_lock_args: have lost lock to resend")); 12884 ASSERT(resend_rqstp != NULL); 12885 ASSERT(resend_rqstp->lr_op == OP_LOCK || 12886 resend_rqstp->lr_op == OP_LOCKU); 12887 12888 *oopp = resend_rqstp->lr_oop; 12889 if (resend_rqstp->lr_oop) { 12890 open_owner_hold(resend_rqstp->lr_oop); 12891 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi); 12892 ASSERT(error == 0); /* recov thread always succeeds */ 12893 } 12894 12895 /* Must resend this lost lock/locku request. */ 12896 ASSERT(resend_rqstp->lr_lop != NULL); 12897 *lopp = resend_rqstp->lr_lop; 12898 lock_owner_hold(resend_rqstp->lr_lop); 12899 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi); 12900 ASSERT(error == 0); /* recov thread always succeeds */ 12901 12902 *ospp = resend_rqstp->lr_osp; 12903 if (*ospp) 12904 open_stream_hold(resend_rqstp->lr_osp); 12905 12906 if (resend_rqstp->lr_op == OP_LOCK) { 12907 LOCK4args *lock_args; 12908 12909 argop->argop = OP_LOCK; 12910 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock; 12911 lock_args->locktype = resend_rqstp->lr_locktype; 12912 lock_args->reclaim = 12913 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM); 12914 lock_args->offset = resend_rqstp->lr_flk->l_start; 12915 lock_args->length = resend_rqstp->lr_flk->l_len; 12916 if (lock_args->length == 0) 12917 lock_args->length = ~lock_args->length; 12918 nfs4_setup_lock_args(*lopp, *oopp, *ospp, 12919 mi2clientid(mi), &lock_args->locker); 12920 12921 switch (resend_rqstp->lr_ctype) { 12922 case NFS4_LCK_CTYPE_RESEND: 12923 argsp->ctag = TAG_LOCK_RESEND; 12924 break; 12925 case NFS4_LCK_CTYPE_REINSTATE: 12926 argsp->ctag = TAG_LOCK_REINSTATE; 12927 break; 12928 case NFS4_LCK_CTYPE_RECLAIM: 12929 argsp->ctag = TAG_LOCK_RECLAIM; 12930 break; 12931 default: 12932 argsp->ctag = TAG_LOCK_UNKNOWN; 12933 break; 12934 } 12935 } else { 12936 LOCKU4args *locku_args; 12937 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop; 12938 12939 argop->argop = OP_LOCKU; 12940 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku; 12941 locku_args->locktype = READ_LT; 12942 locku_args->seqid = lop->lock_seqid + 1; 12943 mutex_enter(&lop->lo_lock); 12944 locku_args->lock_stateid = lop->lock_stateid; 12945 mutex_exit(&lop->lo_lock); 12946 locku_args->offset = resend_rqstp->lr_flk->l_start; 12947 locku_args->length = resend_rqstp->lr_flk->l_len; 12948 if (locku_args->length == 0) 12949 locku_args->length = ~locku_args->length; 12950 12951 switch (resend_rqstp->lr_ctype) { 12952 case NFS4_LCK_CTYPE_RESEND: 12953 argsp->ctag = TAG_LOCKU_RESEND; 12954 break; 12955 case NFS4_LCK_CTYPE_REINSTATE: 12956 argsp->ctag = TAG_LOCKU_REINSTATE; 12957 break; 12958 default: 12959 argsp->ctag = TAG_LOCK_UNKNOWN; 12960 break; 12961 } 12962 } 12963 } 12964 12965 /* 12966 * Setup the LOCKT4 arguments. 12967 */ 12968 static void 12969 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 12970 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk, 12971 rnode4_t *rp) 12972 { 12973 LOCKT4args *lockt_args; 12974 12975 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone); 12976 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 12977 argop->argop = OP_LOCKT; 12978 argsp->ctag = TAG_LOCKT; 12979 lockt_args = &argop->nfs_argop4_u.oplockt; 12980 12981 /* 12982 * The locktype will be READ_LT unless it's 12983 * a write lock. We do this because the Solaris 12984 * system call allows the combination of 12985 * F_UNLCK and F_GETLK* and so in that case the 12986 * unlock is mapped to a read. 12987 */ 12988 if (flk->l_type == F_WRLCK) 12989 lockt_args->locktype = WRITE_LT; 12990 else 12991 lockt_args->locktype = READ_LT; 12992 12993 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp))); 12994 /* set the lock owner4 args */ 12995 nfs4_setlockowner_args(&lockt_args->owner, rp, 12996 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 12997 flk->l_pid); 12998 lockt_args->offset = flk->l_start; 12999 lockt_args->length = flk->l_len; 13000 if (flk->l_len == 0) 13001 lockt_args->length = ~lockt_args->length; 13002 13003 *lockt_argsp = lockt_args; 13004 } 13005 13006 /* 13007 * If the client is holding a delegation, and the open stream to be used 13008 * with this lock request is a delegation open stream, then re-open the stream. 13009 * Sets the nfs4_error_t to all zeros unless the open stream has already 13010 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY 13011 * means the caller should retry (like a recovery retry). 13012 */ 13013 static void 13014 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt) 13015 { 13016 open_delegation_type4 dt; 13017 bool_t reopen_needed, force; 13018 nfs4_open_stream_t *osp; 13019 open_claim_type4 oclaim; 13020 rnode4_t *rp = VTOR4(vp); 13021 mntinfo4_t *mi = VTOMI4(vp); 13022 13023 ASSERT(nfs_zone() == mi->mi_zone); 13024 13025 nfs4_error_zinit(ep); 13026 13027 mutex_enter(&rp->r_statev4_lock); 13028 dt = rp->r_deleg_type; 13029 mutex_exit(&rp->r_statev4_lock); 13030 13031 if (dt != OPEN_DELEGATE_NONE) { 13032 nfs4_open_owner_t *oop; 13033 13034 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 13035 if (!oop) { 13036 ep->stat = NFS4ERR_IO; 13037 return; 13038 } 13039 /* returns with 'os_sync_lock' held */ 13040 osp = find_open_stream(oop, rp); 13041 if (!osp) { 13042 open_owner_rele(oop); 13043 ep->stat = NFS4ERR_IO; 13044 return; 13045 } 13046 13047 if (osp->os_failed_reopen) { 13048 NFS4_DEBUG((nfs4_open_stream_debug || 13049 nfs4_client_lock_debug), (CE_NOTE, 13050 "nfs4frlock_check_deleg: os_failed_reopen set " 13051 "for osp %p, cr %p, rp %s", (void *)osp, 13052 (void *)cr, rnode4info(rp))); 13053 mutex_exit(&osp->os_sync_lock); 13054 open_stream_rele(osp, rp); 13055 open_owner_rele(oop); 13056 ep->stat = NFS4ERR_IO; 13057 return; 13058 } 13059 13060 /* 13061 * Determine whether a reopen is needed. If this 13062 * is a delegation open stream, then send the open 13063 * to the server to give visibility to the open owner. 13064 * Even if it isn't a delegation open stream, we need 13065 * to check if the previous open CLAIM_DELEGATE_CUR 13066 * was sufficient. 13067 */ 13068 13069 reopen_needed = osp->os_delegation || 13070 ((lt == F_RDLCK && 13071 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) || 13072 (lt == F_WRLCK && 13073 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE))); 13074 13075 mutex_exit(&osp->os_sync_lock); 13076 open_owner_rele(oop); 13077 13078 if (reopen_needed) { 13079 /* 13080 * Always use CLAIM_PREVIOUS after server reboot. 13081 * The server will reject CLAIM_DELEGATE_CUR if 13082 * it is used during the grace period. 13083 */ 13084 mutex_enter(&mi->mi_lock); 13085 if (mi->mi_recovflags & MI4R_SRV_REBOOT) { 13086 oclaim = CLAIM_PREVIOUS; 13087 force = TRUE; 13088 } else { 13089 oclaim = CLAIM_DELEGATE_CUR; 13090 force = FALSE; 13091 } 13092 mutex_exit(&mi->mi_lock); 13093 13094 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE); 13095 if (ep->error == EAGAIN) { 13096 nfs4_error_zinit(ep); 13097 ep->stat = NFS4ERR_DELAY; 13098 } 13099 } 13100 open_stream_rele(osp, rp); 13101 osp = NULL; 13102 } 13103 } 13104 13105 /* 13106 * Setup the LOCKU4 arguments. 13107 * Returns errors via the nfs4_error_t. 13108 * NFS4_OK no problems. *go_otwp is TRUE if call should go 13109 * over-the-wire. The caller must release the 13110 * reference on *lopp. 13111 * NFS4ERR_DELAY caller should retry (like recovery retry) 13112 * (other) unrecoverable error. 13113 */ 13114 static void 13115 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop, 13116 LOCKU4args **locku_argsp, flock64_t *flk, 13117 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp, 13118 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr, 13119 bool_t *skip_get_err, bool_t *go_otwp) 13120 { 13121 nfs4_lock_owner_t *lop = NULL; 13122 LOCKU4args *locku_args; 13123 pid_t pid; 13124 bool_t is_spec = FALSE; 13125 rnode4_t *rp = VTOR4(vp); 13126 13127 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13128 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13129 13130 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK); 13131 if (ep->error || ep->stat) 13132 return; 13133 13134 argop->argop = OP_LOCKU; 13135 if (ctype == NFS4_LCK_CTYPE_REINSTATE) 13136 argsp->ctag = TAG_LOCKU_REINSTATE; 13137 else 13138 argsp->ctag = TAG_LOCKU; 13139 locku_args = &argop->nfs_argop4_u.oplocku; 13140 *locku_argsp = locku_args; 13141 13142 /* 13143 * XXX what should locku_args->locktype be? 13144 * setting to ALWAYS be READ_LT so at least 13145 * it is a valid locktype. 13146 */ 13147 13148 locku_args->locktype = READ_LT; 13149 13150 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id : 13151 flk->l_pid; 13152 13153 /* 13154 * Get the lock owner stateid. If no lock owner 13155 * exists, return success. 13156 */ 13157 lop = find_lock_owner(rp, pid, LOWN_ANY); 13158 *lopp = lop; 13159 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid)) 13160 is_spec = TRUE; 13161 if (!lop || is_spec) { 13162 /* 13163 * No lock owner so no locks to unlock. 13164 * Return success. If there was a failed 13165 * reclaim earlier, the lock might still be 13166 * registered with the local locking code, 13167 * so notify it of the unlock. 13168 * 13169 * If the lockowner is using a special stateid, 13170 * then the original lock request (that created 13171 * this lockowner) was never successful, so we 13172 * have no lock to undo OTW. 13173 */ 13174 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13175 "nfs4frlock_setup_locku_args: LOCKU: no lock owner " 13176 "(%ld) so return success", (long)pid)); 13177 13178 if (ctype == NFS4_LCK_CTYPE_NORM) 13179 flk->l_pid = curproc->p_pid; 13180 nfs4_register_lock_locally(vp, flk, flag, offset); 13181 /* 13182 * Release our hold and NULL out so final_cleanup 13183 * doesn't try to end a lock seqid sync we 13184 * never started. 13185 */ 13186 if (is_spec) { 13187 lock_owner_rele(lop); 13188 *lopp = NULL; 13189 } 13190 *skip_get_err = TRUE; 13191 *go_otwp = FALSE; 13192 return; 13193 } 13194 13195 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp)); 13196 if (ep->error == EAGAIN) { 13197 lock_owner_rele(lop); 13198 *lopp = NULL; 13199 return; 13200 } 13201 13202 mutex_enter(&lop->lo_lock); 13203 locku_args->lock_stateid = lop->lock_stateid; 13204 mutex_exit(&lop->lo_lock); 13205 locku_args->seqid = lop->lock_seqid + 1; 13206 13207 /* leave the ref count on lop, rele after RPC call */ 13208 13209 locku_args->offset = flk->l_start; 13210 locku_args->length = flk->l_len; 13211 if (flk->l_len == 0) 13212 locku_args->length = ~locku_args->length; 13213 13214 *go_otwp = TRUE; 13215 } 13216 13217 /* 13218 * Setup the LOCK4 arguments. 13219 * 13220 * Returns errors via the nfs4_error_t. 13221 * NFS4_OK no problems 13222 * NFS4ERR_DELAY caller should retry (like recovery retry) 13223 * (other) unrecoverable error 13224 */ 13225 static void 13226 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp, 13227 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13228 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp, 13229 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep) 13230 { 13231 LOCK4args *lock_args; 13232 nfs4_open_owner_t *oop = NULL; 13233 nfs4_open_stream_t *osp = NULL; 13234 nfs4_lock_owner_t *lop = NULL; 13235 pid_t pid; 13236 rnode4_t *rp = VTOR4(vp); 13237 13238 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13239 13240 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type); 13241 if (ep->error || ep->stat != NFS4_OK) 13242 return; 13243 13244 argop->argop = OP_LOCK; 13245 if (ctype == NFS4_LCK_CTYPE_NORM) 13246 argsp->ctag = TAG_LOCK; 13247 else if (ctype == NFS4_LCK_CTYPE_RECLAIM) 13248 argsp->ctag = TAG_RELOCK; 13249 else 13250 argsp->ctag = TAG_LOCK_REINSTATE; 13251 lock_args = &argop->nfs_argop4_u.oplock; 13252 lock_args->locktype = flk_to_locktype(cmd, flk->l_type); 13253 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0; 13254 /* 13255 * Get the lock owner. If no lock owner exists, 13256 * create a 'temporary' one and grab the open seqid 13257 * synchronization (which puts a hold on the open 13258 * owner and open stream). 13259 * This also grabs the lock seqid synchronization. 13260 */ 13261 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid; 13262 ep->stat = 13263 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop); 13264 13265 if (ep->stat != NFS4_OK) 13266 goto out; 13267 13268 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)), 13269 &lock_args->locker); 13270 13271 lock_args->offset = flk->l_start; 13272 lock_args->length = flk->l_len; 13273 if (flk->l_len == 0) 13274 lock_args->length = ~lock_args->length; 13275 *lock_argsp = lock_args; 13276 out: 13277 *oopp = oop; 13278 *ospp = osp; 13279 *lopp = lop; 13280 } 13281 13282 /* 13283 * After we get the reply from the server, record the proper information 13284 * for possible resend lock requests. 13285 * 13286 * Allocates memory for the saved_rqstp if we have a lost lock to save. 13287 */ 13288 static void 13289 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error, 13290 nfs_lock_type4 locktype, nfs4_open_owner_t *oop, 13291 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13292 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp) 13293 { 13294 bool_t unlock = (flk->l_type == F_UNLCK); 13295 13296 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13297 ASSERT(ctype == NFS4_LCK_CTYPE_NORM || 13298 ctype == NFS4_LCK_CTYPE_REINSTATE); 13299 13300 if (error != 0 && !unlock) { 13301 NFS4_DEBUG((nfs4_lost_rqst_debug || 13302 nfs4_client_lock_debug), (CE_NOTE, 13303 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 " 13304 " for lop %p", (void *)lop)); 13305 ASSERT(lop != NULL); 13306 mutex_enter(&lop->lo_lock); 13307 lop->lo_pending_rqsts = 1; 13308 mutex_exit(&lop->lo_lock); 13309 } 13310 13311 lost_rqstp->lr_putfirst = FALSE; 13312 lost_rqstp->lr_op = 0; 13313 13314 /* 13315 * For lock/locku requests, we treat EINTR as ETIMEDOUT for 13316 * recovery purposes so that the lock request that was sent 13317 * can be saved and re-issued later. Ditto for EIO from a forced 13318 * unmount. This is done to have the client's local locking state 13319 * match the v4 server's state; that is, the request was 13320 * potentially received and accepted by the server but the client 13321 * thinks it was not. 13322 */ 13323 if (error == ETIMEDOUT || error == EINTR || 13324 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) { 13325 NFS4_DEBUG((nfs4_lost_rqst_debug || 13326 nfs4_client_lock_debug), (CE_NOTE, 13327 "nfs4frlock_save_lost_rqst: got a lost %s lock for " 13328 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK", 13329 (void *)lop, (void *)oop, (void *)osp)); 13330 if (unlock) 13331 lost_rqstp->lr_op = OP_LOCKU; 13332 else { 13333 lost_rqstp->lr_op = OP_LOCK; 13334 lost_rqstp->lr_locktype = locktype; 13335 } 13336 /* 13337 * Objects are held and rele'd via the recovery code. 13338 * See nfs4_save_lost_rqst. 13339 */ 13340 lost_rqstp->lr_vp = vp; 13341 lost_rqstp->lr_dvp = NULL; 13342 lost_rqstp->lr_oop = oop; 13343 lost_rqstp->lr_osp = osp; 13344 lost_rqstp->lr_lop = lop; 13345 lost_rqstp->lr_cr = cr; 13346 switch (ctype) { 13347 case NFS4_LCK_CTYPE_NORM: 13348 flk->l_pid = ttoproc(curthread)->p_pid; 13349 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND; 13350 break; 13351 case NFS4_LCK_CTYPE_REINSTATE: 13352 lost_rqstp->lr_putfirst = TRUE; 13353 lost_rqstp->lr_ctype = ctype; 13354 break; 13355 default: 13356 break; 13357 } 13358 lost_rqstp->lr_flk = flk; 13359 } 13360 } 13361 13362 /* 13363 * Update lop's seqid. Also update the seqid stored in a resend request, 13364 * if any. (Some recovery errors increment the seqid, and we may have to 13365 * send the resend request again.) 13366 */ 13367 13368 static void 13369 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args, 13370 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type) 13371 { 13372 if (lock_args) { 13373 if (lock_args->locker.new_lock_owner == TRUE) 13374 nfs4_get_and_set_next_open_seqid(oop, tag_type); 13375 else { 13376 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13377 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop); 13378 } 13379 } else if (locku_args) { 13380 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE); 13381 nfs4_set_lock_seqid(lop->lock_seqid +1, lop); 13382 } 13383 } 13384 13385 /* 13386 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13387 * COMPOUND4 args/res for calls that need to retry. 13388 * Switches the *cred_otwp to base_cr. 13389 */ 13390 static void 13391 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint, 13392 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop, 13393 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error, 13394 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp, 13395 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp) 13396 { 13397 nfs4_open_owner_t *oop = *oopp; 13398 nfs4_open_stream_t *osp = *ospp; 13399 nfs4_lock_owner_t *lop = *lopp; 13400 nfs_argop4 *argop = (*argspp)->array; 13401 13402 if (*did_start_fop) { 13403 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13404 needrecov); 13405 *did_start_fop = FALSE; 13406 } 13407 ASSERT((*argspp)->array_len == 2); 13408 if (argop[1].argop == OP_LOCK) 13409 nfs4args_lock_free(&argop[1]); 13410 else if (argop[1].argop == OP_LOCKT) 13411 nfs4args_lockt_free(&argop[1]); 13412 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13413 if (!error) 13414 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13415 *argspp = NULL; 13416 *respp = NULL; 13417 13418 if (lop) { 13419 nfs4_end_lock_seqid_sync(lop); 13420 lock_owner_rele(lop); 13421 *lopp = NULL; 13422 } 13423 13424 /* need to free up the reference on osp for lock args */ 13425 if (osp != NULL) { 13426 open_stream_rele(osp, VTOR4(vp)); 13427 *ospp = NULL; 13428 } 13429 13430 /* need to free up the reference on oop for lock args */ 13431 if (oop != NULL) { 13432 nfs4_end_open_seqid_sync(oop); 13433 open_owner_rele(oop); 13434 *oopp = NULL; 13435 } 13436 13437 crfree(*cred_otwp); 13438 *cred_otwp = base_cr; 13439 crhold(*cred_otwp); 13440 } 13441 13442 /* 13443 * Function to process the client's recovery for nfs4frlock. 13444 * Returns TRUE if we should retry the lock request; FALSE otherwise. 13445 * 13446 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13447 * COMPOUND4 args/res for calls that need to retry. 13448 * 13449 * Note: the rp's r_lkserlock is *not* dropped during this path. 13450 */ 13451 static bool_t 13452 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep, 13453 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13454 LOCK4args *lock_args, LOCKU4args *locku_args, 13455 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp, 13456 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp, 13457 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint, 13458 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk) 13459 { 13460 nfs4_open_owner_t *oop = *oopp; 13461 nfs4_open_stream_t *osp = *ospp; 13462 nfs4_lock_owner_t *lop = *lopp; 13463 13464 bool_t abort, retry; 13465 13466 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13467 ASSERT((*argspp) != NULL); 13468 ASSERT((*respp) != NULL); 13469 if (lock_args || locku_args) 13470 ASSERT(lop != NULL); 13471 13472 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug), 13473 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n")); 13474 13475 retry = TRUE; 13476 abort = FALSE; 13477 if (needrecov) { 13478 nfs4_bseqid_entry_t *bsep = NULL; 13479 nfs_opnum4 op; 13480 13481 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT; 13482 13483 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) { 13484 seqid4 seqid; 13485 13486 if (lock_args) { 13487 if (lock_args->locker.new_lock_owner == TRUE) 13488 seqid = lock_args->locker.locker4_u. 13489 open_owner.open_seqid; 13490 else 13491 seqid = lock_args->locker.locker4_u. 13492 lock_owner.lock_seqid; 13493 } else if (locku_args) { 13494 seqid = locku_args->seqid; 13495 } else { 13496 seqid = 0; 13497 } 13498 13499 bsep = nfs4_create_bseqid_entry(oop, lop, vp, 13500 flk->l_pid, (*argspp)->ctag, seqid); 13501 } 13502 13503 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL, 13504 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK || 13505 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp : 13506 NULL, op, bsep); 13507 13508 if (bsep) 13509 kmem_free(bsep, sizeof (*bsep)); 13510 } 13511 13512 /* 13513 * Return that we do not want to retry the request for 3 cases: 13514 * 1. If we received EINTR or are bailing out because of a forced 13515 * unmount, we came into this code path just for the sake of 13516 * initiating recovery, we now need to return the error. 13517 * 2. If we have aborted recovery. 13518 * 3. We received NFS4ERR_BAD_SEQID. 13519 */ 13520 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) || 13521 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID)) 13522 retry = FALSE; 13523 13524 if (*did_start_fop == TRUE) { 13525 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep, 13526 needrecov); 13527 *did_start_fop = FALSE; 13528 } 13529 13530 if (retry == TRUE) { 13531 nfs_argop4 *argop; 13532 13533 argop = (*argspp)->array; 13534 ASSERT((*argspp)->array_len == 2); 13535 13536 if (argop[1].argop == OP_LOCK) 13537 nfs4args_lock_free(&argop[1]); 13538 else if (argop[1].argop == OP_LOCKT) 13539 nfs4args_lockt_free(&argop[1]); 13540 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13541 if (!ep->error) 13542 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp); 13543 *respp = NULL; 13544 *argspp = NULL; 13545 } 13546 13547 if (lop != NULL) { 13548 nfs4_end_lock_seqid_sync(lop); 13549 lock_owner_rele(lop); 13550 } 13551 13552 *lopp = NULL; 13553 13554 /* need to free up the reference on osp for lock args */ 13555 if (osp != NULL) { 13556 open_stream_rele(osp, rp); 13557 *ospp = NULL; 13558 } 13559 13560 /* need to free up the reference on oop for lock args */ 13561 if (oop != NULL) { 13562 nfs4_end_open_seqid_sync(oop); 13563 open_owner_rele(oop); 13564 *oopp = NULL; 13565 } 13566 13567 return (retry); 13568 } 13569 13570 /* 13571 * Handles the succesful reply from the server for nfs4frlock. 13572 */ 13573 static void 13574 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk, 13575 vnode_t *vp, int flag, u_offset_t offset, 13576 nfs4_lost_rqst_t *resend_rqstp) 13577 { 13578 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13579 if ((cmd == F_SETLK || cmd == F_SETLKW) && 13580 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) { 13581 if (ctype == NFS4_LCK_CTYPE_NORM) { 13582 flk->l_pid = ttoproc(curthread)->p_pid; 13583 /* 13584 * We do not register lost locks locally in 13585 * the 'resend' case since the user/application 13586 * doesn't think we have the lock. 13587 */ 13588 ASSERT(!resend_rqstp); 13589 nfs4_register_lock_locally(vp, flk, flag, offset); 13590 } 13591 } 13592 } 13593 13594 /* 13595 * Handle the DENIED reply from the server for nfs4frlock. 13596 * Returns TRUE if we should retry the request; FALSE otherwise. 13597 * 13598 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13599 * COMPOUND4 args/res for calls that need to retry. Can also 13600 * drop and regrab the r_lkserlock. 13601 */ 13602 static bool_t 13603 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args, 13604 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp, 13605 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd, 13606 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint, 13607 nfs4_recov_state_t *recov_statep, int needrecov, 13608 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, 13609 clock_t *tick_delayp, short *whencep, int *errorp, 13610 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop, 13611 bool_t *skip_get_err) 13612 { 13613 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13614 13615 if (lock_args) { 13616 nfs4_open_owner_t *oop = *oopp; 13617 nfs4_open_stream_t *osp = *ospp; 13618 nfs4_lock_owner_t *lop = *lopp; 13619 int intr; 13620 13621 /* 13622 * Blocking lock needs to sleep and retry from the request. 13623 * 13624 * Do not block and wait for 'resend' or 'reinstate' 13625 * lock requests, just return the error. 13626 * 13627 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW. 13628 */ 13629 if (cmd == F_SETLKW) { 13630 rnode4_t *rp = VTOR4(vp); 13631 nfs_argop4 *argop = (*argspp)->array; 13632 13633 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 13634 13635 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 13636 recov_statep, needrecov); 13637 *did_start_fop = FALSE; 13638 ASSERT((*argspp)->array_len == 2); 13639 if (argop[1].argop == OP_LOCK) 13640 nfs4args_lock_free(&argop[1]); 13641 else if (argop[1].argop == OP_LOCKT) 13642 nfs4args_lockt_free(&argop[1]); 13643 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13644 if (*respp) 13645 (void) xdr_free(xdr_COMPOUND4res_clnt, 13646 (caddr_t)*respp); 13647 *argspp = NULL; 13648 *respp = NULL; 13649 nfs4_end_lock_seqid_sync(lop); 13650 lock_owner_rele(lop); 13651 *lopp = NULL; 13652 if (osp != NULL) { 13653 open_stream_rele(osp, rp); 13654 *ospp = NULL; 13655 } 13656 if (oop != NULL) { 13657 nfs4_end_open_seqid_sync(oop); 13658 open_owner_rele(oop); 13659 *oopp = NULL; 13660 } 13661 13662 nfs_rw_exit(&rp->r_lkserlock); 13663 13664 intr = nfs4_block_and_wait(tick_delayp, rp); 13665 13666 if (intr) { 13667 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13668 RW_WRITER, FALSE); 13669 *errorp = EINTR; 13670 return (FALSE); 13671 } 13672 13673 (void) nfs_rw_enter_sig(&rp->r_lkserlock, 13674 RW_WRITER, FALSE); 13675 13676 /* 13677 * Make sure we are still safe to lock with 13678 * regards to mmapping. 13679 */ 13680 if (!nfs4_safelock(vp, flk, cr)) { 13681 *errorp = EAGAIN; 13682 return (FALSE); 13683 } 13684 13685 return (TRUE); 13686 } 13687 if (ctype == NFS4_LCK_CTYPE_NORM) 13688 *errorp = EAGAIN; 13689 *skip_get_err = TRUE; 13690 flk->l_whence = 0; 13691 *whencep = 0; 13692 return (FALSE); 13693 } else if (lockt_args) { 13694 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13695 "nfs4frlock_results_denied: OP_LOCKT DENIED")); 13696 13697 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied, 13698 flk, lockt_args); 13699 13700 /* according to NLM code */ 13701 *errorp = 0; 13702 *whencep = 0; 13703 *skip_get_err = TRUE; 13704 return (FALSE); 13705 } 13706 return (FALSE); 13707 } 13708 13709 /* 13710 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock. 13711 */ 13712 static void 13713 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp) 13714 { 13715 switch (resp->status) { 13716 case NFS4ERR_ACCESS: 13717 case NFS4ERR_ADMIN_REVOKED: 13718 case NFS4ERR_BADHANDLE: 13719 case NFS4ERR_BAD_RANGE: 13720 case NFS4ERR_BAD_SEQID: 13721 case NFS4ERR_BAD_STATEID: 13722 case NFS4ERR_BADXDR: 13723 case NFS4ERR_DEADLOCK: 13724 case NFS4ERR_DELAY: 13725 case NFS4ERR_EXPIRED: 13726 case NFS4ERR_FHEXPIRED: 13727 case NFS4ERR_GRACE: 13728 case NFS4ERR_INVAL: 13729 case NFS4ERR_ISDIR: 13730 case NFS4ERR_LEASE_MOVED: 13731 case NFS4ERR_LOCK_NOTSUPP: 13732 case NFS4ERR_LOCK_RANGE: 13733 case NFS4ERR_MOVED: 13734 case NFS4ERR_NOFILEHANDLE: 13735 case NFS4ERR_NO_GRACE: 13736 case NFS4ERR_OLD_STATEID: 13737 case NFS4ERR_OPENMODE: 13738 case NFS4ERR_RECLAIM_BAD: 13739 case NFS4ERR_RECLAIM_CONFLICT: 13740 case NFS4ERR_RESOURCE: 13741 case NFS4ERR_SERVERFAULT: 13742 case NFS4ERR_STALE: 13743 case NFS4ERR_STALE_CLIENTID: 13744 case NFS4ERR_STALE_STATEID: 13745 return; 13746 default: 13747 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 13748 "nfs4frlock_results_default: got unrecognizable " 13749 "res.status %d", resp->status)); 13750 *errorp = NFS4ERR_INVAL; 13751 } 13752 } 13753 13754 /* 13755 * The lock request was successful, so update the client's state. 13756 */ 13757 static void 13758 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args, 13759 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop, 13760 vnode_t *vp, flock64_t *flk, cred_t *cr, 13761 nfs4_lost_rqst_t *resend_rqstp) 13762 { 13763 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13764 13765 if (lock_args) { 13766 LOCK4res *lock_res; 13767 13768 lock_res = &resop->nfs_resop4_u.oplock; 13769 /* update the stateid with server's response */ 13770 13771 if (lock_args->locker.new_lock_owner == TRUE) { 13772 mutex_enter(&lop->lo_lock); 13773 lop->lo_just_created = NFS4_PERM_CREATED; 13774 mutex_exit(&lop->lo_lock); 13775 } 13776 13777 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid); 13778 13779 /* 13780 * If the lock was the result of a resending a lost 13781 * request, we've synched up the stateid and seqid 13782 * with the server, but now the server might be out of sync 13783 * with what the application thinks it has for locks. 13784 * Clean that up here. It's unclear whether we should do 13785 * this even if the filesystem has been forcibly unmounted. 13786 * For most servers, it's probably wasted effort, but 13787 * RFC3530 lets servers require that unlocks exactly match 13788 * the locks that are held. 13789 */ 13790 if (resend_rqstp != NULL && 13791 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) { 13792 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop); 13793 } else { 13794 flk->l_whence = 0; 13795 } 13796 } else if (locku_args) { 13797 LOCKU4res *locku_res; 13798 13799 locku_res = &resop->nfs_resop4_u.oplocku; 13800 13801 /* Update the stateid with the server's response */ 13802 nfs4_set_lock_stateid(lop, locku_res->lock_stateid); 13803 } else if (lockt_args) { 13804 /* Switch the lock type to express success, see fcntl */ 13805 flk->l_type = F_UNLCK; 13806 flk->l_whence = 0; 13807 } 13808 } 13809 13810 /* 13811 * Do final cleanup before exiting nfs4frlock. 13812 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the 13813 * COMPOUND4 args/res for calls that haven't already. 13814 */ 13815 static void 13816 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp, 13817 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint, 13818 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop, 13819 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk, 13820 short whence, u_offset_t offset, struct lm_sysid *ls, 13821 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args, 13822 bool_t did_start_fop, bool_t skip_get_err, 13823 cred_t *cred_otw, cred_t *cred) 13824 { 13825 mntinfo4_t *mi = VTOMI4(vp); 13826 rnode4_t *rp = VTOR4(vp); 13827 int error = *errorp; 13828 nfs_argop4 *argop; 13829 13830 ASSERT(nfs_zone() == mi->mi_zone); 13831 /* 13832 * The client recovery code wants the raw status information, 13833 * so don't map the NFS status code to an errno value for 13834 * non-normal call types. 13835 */ 13836 if (ctype == NFS4_LCK_CTYPE_NORM) { 13837 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE) 13838 *errorp = geterrno4(resp->status); 13839 if (did_start_fop == TRUE) 13840 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep, 13841 needrecov); 13842 13843 /* 13844 * We've established a new lock on the server, so invalidate 13845 * the pages associated with the vnode to get the most up to 13846 * date pages from the server after acquiring the lock. We 13847 * want to be sure that the read operation gets the newest data. 13848 * N.B. 13849 * We used to do this in nfs4frlock_results_ok but that doesn't 13850 * work since VOP_PUTPAGE can call nfs4_commit which calls 13851 * nfs4_start_fop. We flush the pages below after calling 13852 * nfs4_end_fop above 13853 */ 13854 if (!error && resp && resp->status == NFS4_OK) { 13855 int error; 13856 13857 error = VOP_PUTPAGE(vp, (u_offset_t)0, 13858 0, B_INVAL, cred); 13859 13860 if (error && (error == ENOSPC || error == EDQUOT)) { 13861 rnode4_t *rp = VTOR4(vp); 13862 13863 mutex_enter(&rp->r_statelock); 13864 if (!rp->r_error) 13865 rp->r_error = error; 13866 mutex_exit(&rp->r_statelock); 13867 } 13868 } 13869 } 13870 if (argsp) { 13871 ASSERT(argsp->array_len == 2); 13872 argop = argsp->array; 13873 if (argop[1].argop == OP_LOCK) 13874 nfs4args_lock_free(&argop[1]); 13875 else if (argop[1].argop == OP_LOCKT) 13876 nfs4args_lockt_free(&argop[1]); 13877 kmem_free(argop, 2 * sizeof (nfs_argop4)); 13878 if (resp) 13879 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp); 13880 } 13881 13882 /* free the reference on the lock owner */ 13883 if (lop != NULL) { 13884 nfs4_end_lock_seqid_sync(lop); 13885 lock_owner_rele(lop); 13886 } 13887 13888 /* need to free up the reference on osp for lock args */ 13889 if (osp != NULL) 13890 open_stream_rele(osp, rp); 13891 13892 /* need to free up the reference on oop for lock args */ 13893 if (oop != NULL) { 13894 nfs4_end_open_seqid_sync(oop); 13895 open_owner_rele(oop); 13896 } 13897 13898 (void) convoff(vp, flk, whence, offset); 13899 13900 lm_rel_sysid(ls); 13901 13902 /* 13903 * Record debug information in the event we get EINVAL. 13904 */ 13905 mutex_enter(&mi->mi_lock); 13906 if (*errorp == EINVAL && (lock_args || locku_args) && 13907 (!(mi->mi_flags & MI4_POSIX_LOCK))) { 13908 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) { 13909 zcmn_err(getzoneid(), CE_NOTE, 13910 "%s operation failed with " 13911 "EINVAL probably since the server, %s," 13912 " doesn't support POSIX style locking", 13913 lock_args ? "LOCK" : "LOCKU", 13914 mi->mi_curr_serv->sv_hostname); 13915 mi->mi_flags |= MI4_LOCK_DEBUG; 13916 } 13917 } 13918 mutex_exit(&mi->mi_lock); 13919 13920 if (cred_otw) 13921 crfree(cred_otw); 13922 } 13923 13924 /* 13925 * This calls the server and the local locking code. 13926 * 13927 * Client locks are registerred locally by oring the sysid with 13928 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid. 13929 * We need to distinguish between the two to avoid collision in case one 13930 * machine is used as both client and server. 13931 * 13932 * Blocking lock requests will continually retry to acquire the lock 13933 * forever. 13934 * 13935 * The ctype is defined as follows: 13936 * NFS4_LCK_CTYPE_NORM: normal lock request. 13937 * 13938 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client 13939 * recovery, get the pid from flk instead of curproc, and don't reregister 13940 * the lock locally. 13941 * 13942 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition 13943 * that we will use the information passed in via resend_rqstp to setup the 13944 * lock/locku request. This resend is the exact same request as the 'lost 13945 * lock', and is initiated by the recovery framework. A successful resend 13946 * request can initiate one or more reinstate requests. 13947 * 13948 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it 13949 * does not trigger additional reinstate requests. This lock call type is 13950 * set for setting the v4 server's locking state back to match what the 13951 * client's local locking state is in the event of a received 'lost lock'. 13952 * 13953 * Errors are returned via the nfs4_error_t parameter. 13954 */ 13955 void 13956 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk, 13957 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep, 13958 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp) 13959 { 13960 COMPOUND4args_clnt args, *argsp = NULL; 13961 COMPOUND4res_clnt res, *resp = NULL; 13962 nfs_argop4 *argop; 13963 nfs_resop4 *resop; 13964 rnode4_t *rp; 13965 int doqueue = 1; 13966 clock_t tick_delay; /* delay in clock ticks */ 13967 struct lm_sysid *ls; 13968 LOCK4args *lock_args = NULL; 13969 LOCKU4args *locku_args = NULL; 13970 LOCKT4args *lockt_args = NULL; 13971 nfs4_open_owner_t *oop = NULL; 13972 nfs4_open_stream_t *osp = NULL; 13973 nfs4_lock_owner_t *lop = NULL; 13974 bool_t needrecov = FALSE; 13975 nfs4_recov_state_t recov_state; 13976 short whence; 13977 nfs4_op_hint_t op_hint; 13978 nfs4_lost_rqst_t lost_rqst; 13979 bool_t retry = FALSE; 13980 bool_t did_start_fop = FALSE; 13981 bool_t skip_get_err = FALSE; 13982 cred_t *cred_otw = NULL; 13983 bool_t recovonly; /* just queue request */ 13984 int frc_no_reclaim = 0; 13985 #ifdef DEBUG 13986 char *name; 13987 #endif 13988 13989 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 13990 13991 #ifdef DEBUG 13992 name = fn_name(VTOSV(vp)->sv_name); 13993 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: " 13994 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", " 13995 "length %"PRIu64", pid %d, sysid %d, call type %s, " 13996 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start, 13997 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : 13998 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype), 13999 resend_rqstp ? "TRUE" : "FALSE")); 14000 kmem_free(name, MAXNAMELEN); 14001 #endif 14002 14003 nfs4_error_zinit(ep); 14004 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset); 14005 if (ep->error) 14006 return; 14007 ep->error = nfs4frlock_get_sysid(&ls, vp, flk); 14008 if (ep->error) 14009 return; 14010 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence, 14011 vp, cr, &cred_otw); 14012 14013 recov_retry: 14014 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd, 14015 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst); 14016 rp = VTOR4(vp); 14017 14018 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state, 14019 &did_start_fop, &recovonly); 14020 14021 if (ep->error) 14022 goto out; 14023 14024 if (recovonly) { 14025 /* 14026 * Leave the request for the recovery system to deal with. 14027 */ 14028 ASSERT(ctype == NFS4_LCK_CTYPE_NORM); 14029 ASSERT(cmd != F_GETLK); 14030 ASSERT(flk->l_type == F_UNLCK); 14031 14032 nfs4_error_init(ep, EINTR); 14033 needrecov = TRUE; 14034 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14035 if (lop != NULL) { 14036 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT, 14037 NULL, NULL, lop, flk, &lost_rqst, cr, vp); 14038 (void) nfs4_start_recovery(ep, 14039 VTOMI4(vp), vp, NULL, NULL, 14040 (lost_rqst.lr_op == OP_LOCK || 14041 lost_rqst.lr_op == OP_LOCKU) ? 14042 &lost_rqst : NULL, OP_LOCKU, NULL); 14043 lock_owner_rele(lop); 14044 lop = NULL; 14045 } 14046 flk->l_pid = curproc->p_pid; 14047 nfs4_register_lock_locally(vp, flk, flag, offset); 14048 goto out; 14049 } 14050 14051 /* putfh directory fh */ 14052 argop[0].argop = OP_CPUTFH; 14053 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh; 14054 14055 /* 14056 * Set up the over-the-wire arguments and get references to the 14057 * open owner, etc. 14058 */ 14059 14060 if (ctype == NFS4_LCK_CTYPE_RESEND || 14061 ctype == NFS4_LCK_CTYPE_REINSTATE) { 14062 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp, 14063 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args); 14064 } else { 14065 bool_t go_otw = TRUE; 14066 14067 ASSERT(resend_rqstp == NULL); 14068 14069 switch (cmd) { 14070 case F_GETLK: 14071 case F_O_GETLK: 14072 nfs4frlock_setup_lockt_args(ctype, &argop[1], 14073 &lockt_args, argsp, flk, rp); 14074 break; 14075 case F_SETLKW: 14076 case F_SETLK: 14077 if (flk->l_type == F_UNLCK) 14078 nfs4frlock_setup_locku_args(ctype, 14079 &argop[1], &locku_args, flk, 14080 &lop, ep, argsp, 14081 vp, flag, offset, cr, 14082 &skip_get_err, &go_otw); 14083 else 14084 nfs4frlock_setup_lock_args(ctype, 14085 &lock_args, &oop, &osp, &lop, &argop[1], 14086 argsp, flk, cmd, vp, cr, ep); 14087 14088 if (ep->error) 14089 goto out; 14090 14091 switch (ep->stat) { 14092 case NFS4_OK: 14093 break; 14094 case NFS4ERR_DELAY: 14095 /* recov thread never gets this error */ 14096 ASSERT(resend_rqstp == NULL); 14097 ASSERT(did_start_fop); 14098 14099 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, 14100 &recov_state, TRUE); 14101 did_start_fop = FALSE; 14102 if (argop[1].argop == OP_LOCK) 14103 nfs4args_lock_free(&argop[1]); 14104 else if (argop[1].argop == OP_LOCKT) 14105 nfs4args_lockt_free(&argop[1]); 14106 kmem_free(argop, 2 * sizeof (nfs_argop4)); 14107 argsp = NULL; 14108 goto recov_retry; 14109 default: 14110 ep->error = EIO; 14111 goto out; 14112 } 14113 break; 14114 default: 14115 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14116 "nfs4_frlock: invalid cmd %d", cmd)); 14117 ep->error = EINVAL; 14118 goto out; 14119 } 14120 14121 if (!go_otw) 14122 goto out; 14123 } 14124 14125 /* XXX should we use the local reclock as a cache ? */ 14126 /* 14127 * Unregister the lock with the local locking code before 14128 * contacting the server. This avoids a potential race where 14129 * another process gets notified that it has been granted a lock 14130 * before we can unregister ourselves locally. 14131 */ 14132 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) { 14133 if (ctype == NFS4_LCK_CTYPE_NORM) 14134 flk->l_pid = ttoproc(curthread)->p_pid; 14135 nfs4_register_lock_locally(vp, flk, flag, offset); 14136 } 14137 14138 /* 14139 * Send the server the lock request. Continually loop with a delay 14140 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE. 14141 */ 14142 resp = &res; 14143 14144 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug), 14145 (CE_NOTE, 14146 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first", 14147 rnode4info(rp))); 14148 14149 if (lock_args && frc_no_reclaim) { 14150 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14151 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14152 "nfs4frlock: frc_no_reclaim: clearing reclaim")); 14153 lock_args->reclaim = FALSE; 14154 if (did_reclaimp) 14155 *did_reclaimp = 0; 14156 } 14157 14158 /* 14159 * Do the OTW call. 14160 */ 14161 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep); 14162 14163 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14164 "nfs4frlock: error %d, status %d", ep->error, resp->status)); 14165 14166 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp); 14167 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14168 "nfs4frlock: needrecov %d", needrecov)); 14169 14170 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp)) 14171 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop, 14172 args.ctag); 14173 14174 /* 14175 * Check if one of these mutually exclusive error cases has 14176 * happened: 14177 * need to swap credentials due to access error 14178 * recovery is needed 14179 * different error (only known case is missing Kerberos ticket) 14180 */ 14181 14182 if ((ep->error == EACCES || 14183 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) && 14184 cred_otw != cr) { 14185 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov, 14186 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp, 14187 cr, &cred_otw); 14188 goto recov_retry; 14189 } 14190 14191 if (needrecov) { 14192 /* 14193 * LOCKT requests don't need to recover from lost 14194 * requests since they don't create/modify state. 14195 */ 14196 if ((ep->error == EINTR || 14197 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) && 14198 lockt_args) 14199 goto out; 14200 /* 14201 * Do not attempt recovery for requests initiated by 14202 * the recovery framework. Let the framework redrive them. 14203 */ 14204 if (ctype != NFS4_LCK_CTYPE_NORM) 14205 goto out; 14206 else { 14207 ASSERT(resend_rqstp == NULL); 14208 } 14209 14210 nfs4frlock_save_lost_rqst(ctype, ep->error, 14211 flk_to_locktype(cmd, flk->l_type), 14212 oop, osp, lop, flk, &lost_rqst, cred_otw, vp); 14213 14214 retry = nfs4frlock_recovery(needrecov, ep, &argsp, 14215 &resp, lock_args, locku_args, &oop, &osp, &lop, 14216 rp, vp, &recov_state, op_hint, &did_start_fop, 14217 cmd != F_GETLK ? &lost_rqst : NULL, flk); 14218 14219 if (retry) { 14220 ASSERT(oop == NULL); 14221 ASSERT(osp == NULL); 14222 ASSERT(lop == NULL); 14223 goto recov_retry; 14224 } 14225 goto out; 14226 } 14227 14228 /* 14229 * Bail out if have reached this point with ep->error set. Can 14230 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr). 14231 * This happens if Kerberos ticket has expired or has been 14232 * destroyed. 14233 */ 14234 if (ep->error != 0) 14235 goto out; 14236 14237 /* 14238 * Process the reply. 14239 */ 14240 switch (resp->status) { 14241 case NFS4_OK: 14242 resop = &resp->array[1]; 14243 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset, 14244 resend_rqstp); 14245 /* 14246 * Have a successful lock operation, now update state. 14247 */ 14248 nfs4frlock_update_state(lock_args, locku_args, lockt_args, 14249 resop, lop, vp, flk, cr, resend_rqstp); 14250 break; 14251 14252 case NFS4ERR_DENIED: 14253 resop = &resp->array[1]; 14254 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args, 14255 &oop, &osp, &lop, cmd, vp, flk, op_hint, 14256 &recov_state, needrecov, &argsp, &resp, 14257 &tick_delay, &whence, &ep->error, resop, cr, 14258 &did_start_fop, &skip_get_err); 14259 14260 if (retry) { 14261 ASSERT(oop == NULL); 14262 ASSERT(osp == NULL); 14263 ASSERT(lop == NULL); 14264 goto recov_retry; 14265 } 14266 break; 14267 /* 14268 * If the server won't let us reclaim, fall-back to trying to lock 14269 * the file from scratch. Code elsewhere will check the changeinfo 14270 * to ensure the file hasn't been changed. 14271 */ 14272 case NFS4ERR_NO_GRACE: 14273 if (lock_args && lock_args->reclaim == TRUE) { 14274 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM); 14275 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14276 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE")); 14277 frc_no_reclaim = 1; 14278 /* clean up before retrying */ 14279 needrecov = 0; 14280 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp, 14281 lock_args, locku_args, &oop, &osp, &lop, rp, vp, 14282 &recov_state, op_hint, &did_start_fop, NULL, flk); 14283 goto recov_retry; 14284 } 14285 /* FALLTHROUGH */ 14286 14287 default: 14288 nfs4frlock_results_default(resp, &ep->error); 14289 break; 14290 } 14291 out: 14292 /* 14293 * Process and cleanup from error. Make interrupted unlock 14294 * requests look successful, since they will be handled by the 14295 * client recovery code. 14296 */ 14297 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state, 14298 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error, 14299 lock_args, locku_args, did_start_fop, 14300 skip_get_err, cred_otw, cr); 14301 14302 if (ep->error == EINTR && flk->l_type == F_UNLCK && 14303 (cmd == F_SETLK || cmd == F_SETLKW)) 14304 ep->error = 0; 14305 } 14306 14307 /* 14308 * nfs4_safelock: 14309 * 14310 * Return non-zero if the given lock request can be handled without 14311 * violating the constraints on concurrent mapping and locking. 14312 */ 14313 14314 static int 14315 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr) 14316 { 14317 rnode4_t *rp = VTOR4(vp); 14318 struct vattr va; 14319 int error; 14320 14321 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14322 ASSERT(rp->r_mapcnt >= 0); 14323 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: " 14324 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ? 14325 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock", 14326 bfp->l_start, bfp->l_len, rp->r_mapcnt)); 14327 14328 if (rp->r_mapcnt == 0) 14329 return (1); /* always safe if not mapped */ 14330 14331 /* 14332 * If the file is already mapped and there are locks, then they 14333 * should be all safe locks. So adding or removing a lock is safe 14334 * as long as the new request is safe (i.e., whole-file, meaning 14335 * length and starting offset are both zero). 14336 */ 14337 14338 if (bfp->l_start != 0 || bfp->l_len != 0) { 14339 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14340 "cannot lock a memory mapped file unless locking the " 14341 "entire file: start %"PRIx64", len %"PRIx64, 14342 bfp->l_start, bfp->l_len)); 14343 return (0); 14344 } 14345 14346 /* mandatory locking and mapping don't mix */ 14347 va.va_mask = AT_MODE; 14348 error = VOP_GETATTR(vp, &va, 0, cr); 14349 if (error != 0) { 14350 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14351 "getattr error %d", error)); 14352 return (0); /* treat errors conservatively */ 14353 } 14354 if (MANDLOCK(vp, va.va_mode)) { 14355 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: " 14356 "cannot mandatory lock and mmap a file")); 14357 return (0); 14358 } 14359 14360 return (1); 14361 } 14362 14363 14364 /* 14365 * Register the lock locally within Solaris. 14366 * As the client, we "or" the sysid with LM_SYSID_CLIENT when 14367 * recording locks locally. 14368 * 14369 * This should handle conflicts/cooperation with NFS v2/v3 since all locks 14370 * are registered locally. 14371 */ 14372 void 14373 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag, 14374 u_offset_t offset) 14375 { 14376 int oldsysid; 14377 int error; 14378 #ifdef DEBUG 14379 char *name; 14380 #endif 14381 14382 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14383 14384 #ifdef DEBUG 14385 name = fn_name(VTOSV(vp)->sv_name); 14386 NFS4_DEBUG(nfs4_client_lock_debug, 14387 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, " 14388 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d", 14389 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid, 14390 flk->l_sysid)); 14391 kmem_free(name, MAXNAMELEN); 14392 #endif 14393 14394 /* register the lock with local locking */ 14395 oldsysid = flk->l_sysid; 14396 flk->l_sysid |= LM_SYSID_CLIENT; 14397 error = reclock(vp, flk, SETFLCK, flag, offset, NULL); 14398 #ifdef DEBUG 14399 if (error != 0) { 14400 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14401 "nfs4_register_lock_locally: could not register with" 14402 " local locking")); 14403 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14404 "error %d, vp 0x%p, pid %d, sysid 0x%x", 14405 error, (void *)vp, flk->l_pid, flk->l_sysid)); 14406 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14407 "type %d off 0x%" PRIx64 " len 0x%" PRIx64, 14408 flk->l_type, flk->l_start, flk->l_len)); 14409 (void) reclock(vp, flk, 0, flag, offset, NULL); 14410 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT, 14411 "blocked by pid %d sysid 0x%x type %d " 14412 "off 0x%" PRIx64 " len 0x%" PRIx64, 14413 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start, 14414 flk->l_len)); 14415 } 14416 #endif 14417 flk->l_sysid = oldsysid; 14418 } 14419 14420 /* 14421 * nfs4_lockrelease: 14422 * 14423 * Release any locks on the given vnode that are held by the current 14424 * process. Also removes the lock owner (if one exists) from the rnode's 14425 * list. 14426 */ 14427 static int 14428 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr) 14429 { 14430 flock64_t ld; 14431 int ret, error; 14432 rnode4_t *rp; 14433 nfs4_lock_owner_t *lop; 14434 nfs4_recov_state_t recov_state; 14435 mntinfo4_t *mi; 14436 bool_t possible_orphan = FALSE; 14437 bool_t recovonly; 14438 14439 ASSERT((uintptr_t)vp > KERNELBASE); 14440 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14441 14442 rp = VTOR4(vp); 14443 mi = VTOMI4(vp); 14444 14445 /* 14446 * If we have not locked anything then we can 14447 * just return since we have no work to do. 14448 */ 14449 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) { 14450 return (0); 14451 } 14452 14453 /* 14454 * We need to comprehend that another thread may 14455 * kick off recovery and the lock_owner we have stashed 14456 * in lop might be invalid so we should NOT cache it 14457 * locally! 14458 */ 14459 recov_state.rs_flags = 0; 14460 recov_state.rs_num_retry_despite_err = 0; 14461 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14462 &recovonly); 14463 if (error) { 14464 mutex_enter(&rp->r_statelock); 14465 rp->r_flags |= R4LODANGLERS; 14466 mutex_exit(&rp->r_statelock); 14467 return (error); 14468 } 14469 14470 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14471 14472 /* 14473 * Check if the lock owner might have a lock (request was sent but 14474 * no response was received). Also check if there are any remote 14475 * locks on the file. (In theory we shouldn't have to make this 14476 * second check if there's no lock owner, but for now we'll be 14477 * conservative and do it anyway.) If either condition is true, 14478 * send an unlock for the entire file to the server. 14479 * 14480 * Note that no explicit synchronization is needed here. At worst, 14481 * flk_has_remote_locks() will return a false positive, in which case 14482 * the unlock call wastes time but doesn't harm correctness. 14483 */ 14484 14485 if (lop) { 14486 mutex_enter(&lop->lo_lock); 14487 possible_orphan = lop->lo_pending_rqsts; 14488 mutex_exit(&lop->lo_lock); 14489 lock_owner_rele(lop); 14490 } 14491 14492 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14493 14494 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14495 "nfs4_lockrelease: possible orphan %d, remote locks %d, for " 14496 "lop %p.", possible_orphan, flk_has_remote_locks(vp), 14497 (void *)lop)); 14498 14499 if (possible_orphan || flk_has_remote_locks(vp)) { 14500 ld.l_type = F_UNLCK; /* set to unlock entire file */ 14501 ld.l_whence = 0; /* unlock from start of file */ 14502 ld.l_start = 0; 14503 ld.l_len = 0; /* do entire file */ 14504 14505 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL, cr); 14506 14507 if (ret != 0) { 14508 /* 14509 * If VOP_FRLOCK fails, make sure we unregister 14510 * local locks before we continue. 14511 */ 14512 ld.l_pid = ttoproc(curthread)->p_pid; 14513 nfs4_register_lock_locally(vp, &ld, flag, offset); 14514 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 14515 "nfs4_lockrelease: lock release error on vp" 14516 " %p: error %d.\n", (void *)vp, ret)); 14517 } 14518 } 14519 14520 recov_state.rs_flags = 0; 14521 recov_state.rs_num_retry_despite_err = 0; 14522 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 14523 &recovonly); 14524 if (error) { 14525 mutex_enter(&rp->r_statelock); 14526 rp->r_flags |= R4LODANGLERS; 14527 mutex_exit(&rp->r_statelock); 14528 return (error); 14529 } 14530 14531 /* 14532 * So, here we're going to need to retrieve the lock-owner 14533 * again (in case recovery has done a switch-a-roo) and 14534 * remove it because we can. 14535 */ 14536 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY); 14537 14538 if (lop) { 14539 nfs4_rnode_remove_lock_owner(rp, lop); 14540 lock_owner_rele(lop); 14541 } 14542 14543 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0); 14544 return (0); 14545 } 14546 14547 /* 14548 * Wait for 'tick_delay' clock ticks. 14549 * Implement exponential backoff until hit the lease_time of this nfs4_server. 14550 * NOTE: lock_lease_time is in seconds. 14551 * 14552 * XXX For future improvements, should implement a waiting queue scheme. 14553 */ 14554 static int 14555 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp) 14556 { 14557 long milliseconds_delay; 14558 time_t lock_lease_time; 14559 14560 /* wait tick_delay clock ticks or siginteruptus */ 14561 if (delay_sig(*tick_delay)) { 14562 return (EINTR); 14563 } 14564 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: " 14565 "reissue the lock request: blocked for %ld clock ticks: %ld " 14566 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000)); 14567 14568 /* get the lease time */ 14569 lock_lease_time = r2lease_time(rp); 14570 14571 /* drv_hztousec converts ticks to microseconds */ 14572 milliseconds_delay = drv_hztousec(*tick_delay) / 1000; 14573 if (milliseconds_delay < lock_lease_time * 1000) { 14574 *tick_delay = 2 * *tick_delay; 14575 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000) 14576 *tick_delay = drv_usectohz(lock_lease_time*1000*1000); 14577 } 14578 return (0); 14579 } 14580 14581 14582 void 14583 nfs4_vnops_init(void) 14584 { 14585 } 14586 14587 void 14588 nfs4_vnops_fini(void) 14589 { 14590 } 14591 14592 /* 14593 * Return a reference to the directory (parent) vnode for a given vnode, 14594 * using the saved pathname information and the directory file handle. The 14595 * caller is responsible for disposing of the reference. 14596 * Returns zero or an errno value. 14597 * 14598 * Caller should set need_start_op to FALSE if it is the recovery 14599 * thread, or if a start_fop has already been done. Otherwise, TRUE. 14600 */ 14601 int 14602 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op) 14603 { 14604 svnode_t *svnp; 14605 vnode_t *dvp = NULL; 14606 servinfo4_t *svp; 14607 nfs4_fname_t *mfname; 14608 int error; 14609 14610 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14611 14612 if (vp->v_flag & VROOT) { 14613 nfs4_sharedfh_t *sfh; 14614 nfs_fh4 fh; 14615 mntinfo4_t *mi; 14616 14617 ASSERT(vp->v_type == VREG); 14618 14619 mi = VTOMI4(vp); 14620 svp = mi->mi_curr_serv; 14621 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14622 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len; 14623 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf; 14624 sfh = sfh4_get(&fh, VTOMI4(vp)); 14625 nfs_rw_exit(&svp->sv_lock); 14626 mfname = mi->mi_fname; 14627 fn_hold(mfname); 14628 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0); 14629 sfh4_rele(&sfh); 14630 14631 if (dvp->v_type == VNON) 14632 dvp->v_type = VDIR; 14633 *dvpp = dvp; 14634 return (0); 14635 } 14636 14637 svnp = VTOSV(vp); 14638 14639 if (svnp == NULL) { 14640 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14641 "shadow node is NULL")); 14642 return (EINVAL); 14643 } 14644 14645 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) { 14646 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14647 "shadow node name or dfh val == NULL")); 14648 return (EINVAL); 14649 } 14650 14651 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp, 14652 (int)need_start_op); 14653 if (error != 0) { 14654 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14655 "nfs4_make_dotdot returned %d", error)); 14656 return (error); 14657 } 14658 if (!dvp) { 14659 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: " 14660 "nfs4_make_dotdot returned a NULL dvp")); 14661 return (EIO); 14662 } 14663 if (dvp->v_type == VNON) 14664 dvp->v_type = VDIR; 14665 ASSERT(dvp->v_type == VDIR); 14666 if (VTOR4(vp)->r_flags & R4ISXATTR) { 14667 mutex_enter(&dvp->v_lock); 14668 dvp->v_flag |= V_XATTRDIR; 14669 mutex_exit(&dvp->v_lock); 14670 } 14671 *dvpp = dvp; 14672 return (0); 14673 } 14674 14675 /* 14676 * Copy the (final) component name of vp to fnamep. maxlen is the maximum 14677 * length that fnamep can accept, including the trailing null. 14678 * Returns 0 if okay, returns an errno value if there was a problem. 14679 */ 14680 14681 int 14682 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen) 14683 { 14684 char *fn; 14685 int err = 0; 14686 servinfo4_t *svp; 14687 svnode_t *shvp; 14688 14689 /* 14690 * If the file being opened has VROOT set, then this is 14691 * a "file" mount. sv_name will not be interesting, so 14692 * go back to the servinfo4 to get the original mount 14693 * path and strip off all but the final edge. Otherwise 14694 * just return the name from the shadow vnode. 14695 */ 14696 14697 if (vp->v_flag & VROOT) { 14698 14699 svp = VTOMI4(vp)->mi_curr_serv; 14700 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0); 14701 14702 fn = strrchr(svp->sv_path, '/'); 14703 if (fn == NULL) 14704 err = EINVAL; 14705 else 14706 fn++; 14707 } else { 14708 shvp = VTOSV(vp); 14709 fn = fn_name(shvp->sv_name); 14710 } 14711 14712 if (err == 0) 14713 if (strlen(fn) < maxlen) 14714 (void) strcpy(fnamep, fn); 14715 else 14716 err = ENAMETOOLONG; 14717 14718 if (vp->v_flag & VROOT) 14719 nfs_rw_exit(&svp->sv_lock); 14720 else 14721 kmem_free(fn, MAXNAMELEN); 14722 14723 return (err); 14724 } 14725 14726 /* 14727 * Bookkeeping for a close that doesn't need to go over the wire. 14728 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise 14729 * it is left at 1. 14730 */ 14731 void 14732 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp) 14733 { 14734 rnode4_t *rp; 14735 mntinfo4_t *mi; 14736 14737 mi = VTOMI4(vp); 14738 rp = VTOR4(vp); 14739 14740 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: " 14741 "rp=%p osp=%p", (void *)rp, (void *)osp)); 14742 ASSERT(nfs_zone() == mi->mi_zone); 14743 ASSERT(mutex_owned(&osp->os_sync_lock)); 14744 ASSERT(*have_lockp); 14745 14746 if (!osp->os_valid || 14747 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 14748 return; 14749 } 14750 14751 /* 14752 * This removes the reference obtained at OPEN; ie, 14753 * when the open stream structure was created. 14754 * 14755 * We don't have to worry about calling 'open_stream_rele' 14756 * since we our currently holding a reference to this 14757 * open stream which means the count can not go to 0 with 14758 * this decrement. 14759 */ 14760 ASSERT(osp->os_ref_count >= 2); 14761 osp->os_ref_count--; 14762 osp->os_valid = 0; 14763 mutex_exit(&osp->os_sync_lock); 14764 *have_lockp = 0; 14765 14766 nfs4_dec_state_ref_count(mi); 14767 } 14768 14769 /* 14770 * Close all remaining open streams on the rnode. These open streams 14771 * could be here because: 14772 * - The close attempted at either close or delmap failed 14773 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE 14774 * - Someone did mknod on a regular file but never opened it 14775 */ 14776 int 14777 nfs4close_all(vnode_t *vp, cred_t *cr) 14778 { 14779 nfs4_open_stream_t *osp; 14780 int error; 14781 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS }; 14782 rnode4_t *rp; 14783 14784 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14785 14786 error = 0; 14787 rp = VTOR4(vp); 14788 14789 /* 14790 * At this point, all we know is that the last time 14791 * someone called vn_rele, the count was 1. Since then, 14792 * the vnode could have been re-activated. We want to 14793 * loop through the open streams and close each one, but 14794 * we have to be careful since once we release the rnode 14795 * hash bucket lock, someone else is free to come in and 14796 * re-activate the rnode and add new open streams. The 14797 * strategy is take the rnode hash bucket lock, verify that 14798 * the count is still 1, grab the open stream off the 14799 * head of the list and mark it invalid, then release the 14800 * rnode hash bucket lock and proceed with that open stream. 14801 * This is ok because nfs4close_one() will acquire the proper 14802 * open/create to close/destroy synchronization for open 14803 * streams, and will ensure that if someone has reopened 14804 * the open stream after we've dropped the hash bucket lock 14805 * then we'll just simply return without destroying the 14806 * open stream. 14807 * Repeat until the list is empty. 14808 */ 14809 14810 for (;;) { 14811 14812 /* make sure vnode hasn't been reactivated */ 14813 rw_enter(&rp->r_hashq->r_lock, RW_READER); 14814 mutex_enter(&vp->v_lock); 14815 if (vp->v_count > 1) { 14816 mutex_exit(&vp->v_lock); 14817 rw_exit(&rp->r_hashq->r_lock); 14818 break; 14819 } 14820 /* 14821 * Grabbing r_os_lock before releasing v_lock prevents 14822 * a window where the rnode/open stream could get 14823 * reactivated (and os_force_close set to 0) before we 14824 * had a chance to set os_force_close to 1. 14825 */ 14826 mutex_enter(&rp->r_os_lock); 14827 mutex_exit(&vp->v_lock); 14828 14829 osp = list_head(&rp->r_open_streams); 14830 if (!osp) { 14831 /* nothing left to CLOSE OTW, so return */ 14832 mutex_exit(&rp->r_os_lock); 14833 rw_exit(&rp->r_hashq->r_lock); 14834 break; 14835 } 14836 14837 mutex_enter(&rp->r_statev4_lock); 14838 /* the file can't still be mem mapped */ 14839 ASSERT(rp->r_mapcnt == 0); 14840 if (rp->created_v4) 14841 rp->created_v4 = 0; 14842 mutex_exit(&rp->r_statev4_lock); 14843 14844 /* 14845 * Grab a ref on this open stream; nfs4close_one 14846 * will mark it as invalid 14847 */ 14848 mutex_enter(&osp->os_sync_lock); 14849 osp->os_ref_count++; 14850 osp->os_force_close = 1; 14851 mutex_exit(&osp->os_sync_lock); 14852 mutex_exit(&rp->r_os_lock); 14853 rw_exit(&rp->r_hashq->r_lock); 14854 14855 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0); 14856 14857 /* Update error if it isn't already non-zero */ 14858 if (error == 0) { 14859 if (e.error) 14860 error = e.error; 14861 else if (e.stat) 14862 error = geterrno4(e.stat); 14863 } 14864 14865 #ifdef DEBUG 14866 nfs4close_all_cnt++; 14867 #endif 14868 /* Release the ref on osp acquired above. */ 14869 open_stream_rele(osp, rp); 14870 14871 /* Proceed to the next open stream, if any */ 14872 } 14873 return (error); 14874 } 14875 14876 /* 14877 * nfs4close_one - close one open stream for a file if needed. 14878 * 14879 * "close_type" indicates which close path this is: 14880 * CLOSE_NORM: close initiated via VOP_CLOSE. 14881 * CLOSE_DELMAP: close initiated via VOP_DELMAP. 14882 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces 14883 * the close and release of client state for this open stream 14884 * (unless someone else has the open stream open). 14885 * CLOSE_RESEND: indicates the request is a replay of an earlier request 14886 * (e.g., due to abort because of a signal). 14887 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN. 14888 * 14889 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client 14890 * recovery. Instead, the caller is expected to deal with retries. 14891 * 14892 * The caller can either pass in the osp ('provided_osp') or not. 14893 * 14894 * 'access_bits' represents the access we are closing/downgrading. 14895 * 14896 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the 14897 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and 14898 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED). 14899 * 14900 * Errors are returned via the nfs4_error_t. 14901 */ 14902 void 14903 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr, 14904 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep, 14905 nfs4_close_type_t close_type, size_t len, uint_t maxprot, 14906 uint_t mmap_flags) 14907 { 14908 nfs4_open_owner_t *oop; 14909 nfs4_open_stream_t *osp = NULL; 14910 int retry = 0; 14911 int num_retries = NFS4_NUM_RECOV_RETRIES; 14912 rnode4_t *rp; 14913 mntinfo4_t *mi; 14914 nfs4_recov_state_t recov_state; 14915 cred_t *cred_otw = NULL; 14916 bool_t recovonly = FALSE; 14917 int isrecov; 14918 int force_close; 14919 int close_failed = 0; 14920 int did_dec_count = 0; 14921 int did_start_op = 0; 14922 int did_force_recovlock = 0; 14923 int did_start_seqid_sync = 0; 14924 int have_sync_lock = 0; 14925 14926 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 14927 14928 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, " 14929 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x", 14930 (void *)vp, (void *)provided_osp, (void *)lrp, close_type, 14931 len, maxprot, mmap_flags, access_bits)); 14932 14933 nfs4_error_zinit(ep); 14934 rp = VTOR4(vp); 14935 mi = VTOMI4(vp); 14936 isrecov = (close_type == CLOSE_RESEND || 14937 close_type == CLOSE_AFTER_RESEND); 14938 14939 /* 14940 * First get the open owner. 14941 */ 14942 if (!provided_osp) { 14943 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi); 14944 } else { 14945 oop = provided_osp->os_open_owner; 14946 ASSERT(oop != NULL); 14947 open_owner_hold(oop); 14948 } 14949 14950 if (!oop) { 14951 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 14952 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, " 14953 "close type %d", (void *)rp, (void *)mi, (void *)cr, 14954 (void *)provided_osp, close_type)); 14955 ep->error = EIO; 14956 goto out; 14957 } 14958 14959 cred_otw = nfs4_get_otw_cred(cr, mi, oop); 14960 recov_retry: 14961 osp = NULL; 14962 close_failed = 0; 14963 force_close = (close_type == CLOSE_FORCE); 14964 retry = 0; 14965 did_start_op = 0; 14966 did_force_recovlock = 0; 14967 did_start_seqid_sync = 0; 14968 have_sync_lock = 0; 14969 recovonly = FALSE; 14970 recov_state.rs_flags = 0; 14971 recov_state.rs_num_retry_despite_err = 0; 14972 14973 /* 14974 * Second synchronize with recovery. 14975 */ 14976 if (!isrecov) { 14977 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE, 14978 &recov_state, &recovonly); 14979 if (!ep->error) { 14980 did_start_op = 1; 14981 } else { 14982 close_failed = 1; 14983 /* 14984 * If we couldn't get start_fop, but have to 14985 * cleanup state, then at least acquire the 14986 * mi_recovlock so we can synchronize with 14987 * recovery. 14988 */ 14989 if (close_type == CLOSE_FORCE) { 14990 (void) nfs_rw_enter_sig(&mi->mi_recovlock, 14991 RW_READER, FALSE); 14992 did_force_recovlock = 1; 14993 } else 14994 goto out; 14995 } 14996 } 14997 14998 /* 14999 * We cannot attempt to get the open seqid sync if nfs4_start_fop 15000 * set 'recovonly' to TRUE since most likely this is due to 15001 * reovery being active (MI4_RECOV_ACTIV). If recovery is active, 15002 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us 15003 * to retry, causing us to loop until recovery finishes. Plus we 15004 * don't need protection over the open seqid since we're not going 15005 * OTW, hence don't need to use the seqid. 15006 */ 15007 if (recovonly == FALSE) { 15008 /* need to grab the open owner sync before 'os_sync_lock' */ 15009 ep->error = nfs4_start_open_seqid_sync(oop, mi); 15010 if (ep->error == EAGAIN) { 15011 ASSERT(!isrecov); 15012 if (did_start_op) 15013 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15014 &recov_state, TRUE); 15015 if (did_force_recovlock) 15016 nfs_rw_exit(&mi->mi_recovlock); 15017 goto recov_retry; 15018 } 15019 did_start_seqid_sync = 1; 15020 } 15021 15022 /* 15023 * Third get an open stream and acquire 'os_sync_lock' to 15024 * sychronize the opening/creating of an open stream with the 15025 * closing/destroying of an open stream. 15026 */ 15027 if (!provided_osp) { 15028 /* returns with 'os_sync_lock' held */ 15029 osp = find_open_stream(oop, rp); 15030 if (!osp) { 15031 ep->error = EIO; 15032 goto out; 15033 } 15034 } else { 15035 osp = provided_osp; 15036 open_stream_hold(osp); 15037 mutex_enter(&osp->os_sync_lock); 15038 } 15039 have_sync_lock = 1; 15040 15041 ASSERT(oop == osp->os_open_owner); 15042 15043 /* 15044 * Fourth, do any special pre-OTW CLOSE processing 15045 * based on the specific close type. 15046 */ 15047 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) && 15048 !did_dec_count) { 15049 ASSERT(osp->os_open_ref_count > 0); 15050 osp->os_open_ref_count--; 15051 did_dec_count = 1; 15052 if (osp->os_open_ref_count == 0) 15053 osp->os_final_close = 1; 15054 } 15055 15056 if (close_type == CLOSE_FORCE) { 15057 /* see if somebody reopened the open stream. */ 15058 if (!osp->os_force_close) { 15059 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15060 "nfs4close_one: skip CLOSE_FORCE as osp %p " 15061 "was reopened, vp %p", (void *)osp, (void *)vp)); 15062 ep->error = 0; 15063 ep->stat = NFS4_OK; 15064 goto out; 15065 } 15066 15067 if (!osp->os_final_close && !did_dec_count) { 15068 osp->os_open_ref_count--; 15069 did_dec_count = 1; 15070 } 15071 15072 /* 15073 * We can't depend on os_open_ref_count being 0 due to the 15074 * way executables are opened (VN_RELE to match a VOP_OPEN). 15075 */ 15076 #ifdef NOTYET 15077 ASSERT(osp->os_open_ref_count == 0); 15078 #endif 15079 if (osp->os_open_ref_count != 0) { 15080 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, 15081 "nfs4close_one: should panic here on an " 15082 "ASSERT(osp->os_open_ref_count == 0). Ignoring " 15083 "since this is probably the exec problem.")); 15084 15085 osp->os_open_ref_count = 0; 15086 } 15087 15088 /* 15089 * There is the possibility that nfs4close_one() 15090 * for close_type == CLOSE_DELMAP couldn't find the 15091 * open stream, thus couldn't decrement its os_mapcnt; 15092 * therefore we can't use this ASSERT yet. 15093 */ 15094 #ifdef NOTYET 15095 ASSERT(osp->os_mapcnt == 0); 15096 #endif 15097 osp->os_mapcnt = 0; 15098 } 15099 15100 if (close_type == CLOSE_DELMAP && !did_dec_count) { 15101 ASSERT(osp->os_mapcnt >= btopr(len)); 15102 15103 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE)) 15104 osp->os_mmap_write -= btopr(len); 15105 if (maxprot & PROT_READ) 15106 osp->os_mmap_read -= btopr(len); 15107 if (maxprot & PROT_EXEC) 15108 osp->os_mmap_read -= btopr(len); 15109 /* mirror the PROT_NONE check in nfs4_addmap() */ 15110 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) && 15111 !(maxprot & PROT_EXEC)) 15112 osp->os_mmap_read -= btopr(len); 15113 osp->os_mapcnt -= btopr(len); 15114 did_dec_count = 1; 15115 } 15116 15117 if (recovonly) { 15118 nfs4_lost_rqst_t lost_rqst; 15119 15120 /* request should not already be in recovery queue */ 15121 ASSERT(lrp == NULL); 15122 nfs4_error_init(ep, EINTR); 15123 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop, 15124 osp, cred_otw, vp); 15125 mutex_exit(&osp->os_sync_lock); 15126 have_sync_lock = 0; 15127 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15128 lost_rqst.lr_op == OP_CLOSE ? 15129 &lost_rqst : NULL, OP_CLOSE, NULL); 15130 close_failed = 1; 15131 force_close = 0; 15132 goto close_cleanup; 15133 } 15134 15135 /* 15136 * If a previous OTW call got NFS4ERR_BAD_SEQID, then 15137 * we stopped operating on the open owner's <old oo_name, old seqid> 15138 * space, which means we stopped operating on the open stream 15139 * too. So don't go OTW (as the seqid is likely bad, and the 15140 * stateid could be stale, potentially triggering a false 15141 * setclientid), and just clean up the client's internal state. 15142 */ 15143 if (osp->os_orig_oo_name != oop->oo_name) { 15144 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug, 15145 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p " 15146 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current " 15147 "oo_name %" PRIx64")", 15148 (void *)osp, (void *)oop, osp->os_orig_oo_name, 15149 oop->oo_name)); 15150 close_failed = 1; 15151 } 15152 15153 /* If the file failed recovery, just quit. */ 15154 mutex_enter(&rp->r_statelock); 15155 if (rp->r_flags & R4RECOVERR) { 15156 close_failed = 1; 15157 } 15158 mutex_exit(&rp->r_statelock); 15159 15160 /* 15161 * If the force close path failed to obtain start_fop 15162 * then skip the OTW close and just remove the state. 15163 */ 15164 if (close_failed) 15165 goto close_cleanup; 15166 15167 /* 15168 * Fifth, check to see if there are still mapped pages or other 15169 * opens using this open stream. If there are then we can't 15170 * close yet but we can see if an OPEN_DOWNGRADE is necessary. 15171 */ 15172 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) { 15173 nfs4_lost_rqst_t new_lost_rqst; 15174 bool_t needrecov = FALSE; 15175 cred_t *odg_cred_otw = NULL; 15176 seqid4 open_dg_seqid = 0; 15177 15178 if (osp->os_delegation) { 15179 /* 15180 * If this open stream was never OPENed OTW then we 15181 * surely can't DOWNGRADE it (especially since the 15182 * osp->open_stateid is really a delegation stateid 15183 * when os_delegation is 1). 15184 */ 15185 if (access_bits & FREAD) 15186 osp->os_share_acc_read--; 15187 if (access_bits & FWRITE) 15188 osp->os_share_acc_write--; 15189 osp->os_share_deny_none--; 15190 nfs4_error_zinit(ep); 15191 goto out; 15192 } 15193 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr, 15194 lrp, ep, &odg_cred_otw, &open_dg_seqid); 15195 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp); 15196 if (needrecov && !isrecov) { 15197 bool_t abort; 15198 nfs4_bseqid_entry_t *bsep = NULL; 15199 15200 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) 15201 bsep = nfs4_create_bseqid_entry(oop, NULL, 15202 vp, 0, 15203 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG, 15204 open_dg_seqid); 15205 15206 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst, 15207 oop, osp, odg_cred_otw, vp, access_bits, 0); 15208 mutex_exit(&osp->os_sync_lock); 15209 have_sync_lock = 0; 15210 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL, 15211 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ? 15212 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE, 15213 bsep); 15214 if (odg_cred_otw) 15215 crfree(odg_cred_otw); 15216 if (bsep) 15217 kmem_free(bsep, sizeof (*bsep)); 15218 15219 if (abort == TRUE) 15220 goto out; 15221 15222 if (did_start_seqid_sync) { 15223 nfs4_end_open_seqid_sync(oop); 15224 did_start_seqid_sync = 0; 15225 } 15226 open_stream_rele(osp, rp); 15227 15228 if (did_start_op) 15229 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15230 &recov_state, FALSE); 15231 if (did_force_recovlock) 15232 nfs_rw_exit(&mi->mi_recovlock); 15233 15234 goto recov_retry; 15235 } else { 15236 if (odg_cred_otw) 15237 crfree(odg_cred_otw); 15238 } 15239 goto out; 15240 } 15241 15242 /* 15243 * If this open stream was created as the results of an open 15244 * while holding a delegation, then just release it; no need 15245 * to do an OTW close. Otherwise do a "normal" OTW close. 15246 */ 15247 if (osp->os_delegation) { 15248 nfs4close_notw(vp, osp, &have_sync_lock); 15249 nfs4_error_zinit(ep); 15250 goto out; 15251 } 15252 15253 /* 15254 * If this stream is not valid, we're done. 15255 */ 15256 if (!osp->os_valid) { 15257 nfs4_error_zinit(ep); 15258 goto out; 15259 } 15260 15261 /* 15262 * Last open or mmap ref has vanished, need to do an OTW close. 15263 * First check to see if a close is still necessary. 15264 */ 15265 if (osp->os_failed_reopen) { 15266 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15267 "don't close OTW osp %p since reopen failed.", 15268 (void *)osp)); 15269 /* 15270 * Reopen of the open stream failed, hence the 15271 * stateid of the open stream is invalid/stale, and 15272 * sending this OTW would incorrectly cause another 15273 * round of recovery. In this case, we need to set 15274 * the 'os_valid' bit to 0 so another thread doesn't 15275 * come in and re-open this open stream before 15276 * this "closing" thread cleans up state (decrementing 15277 * the nfs4_server_t's state_ref_count and decrementing 15278 * the os_ref_count). 15279 */ 15280 osp->os_valid = 0; 15281 /* 15282 * This removes the reference obtained at OPEN; ie, 15283 * when the open stream structure was created. 15284 * 15285 * We don't have to worry about calling 'open_stream_rele' 15286 * since we our currently holding a reference to this 15287 * open stream which means the count can not go to 0 with 15288 * this decrement. 15289 */ 15290 ASSERT(osp->os_ref_count >= 2); 15291 osp->os_ref_count--; 15292 nfs4_error_zinit(ep); 15293 close_failed = 0; 15294 goto close_cleanup; 15295 } 15296 15297 ASSERT(osp->os_ref_count > 1); 15298 15299 /* 15300 * Sixth, try the CLOSE OTW. 15301 */ 15302 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync, 15303 close_type, ep, &have_sync_lock); 15304 15305 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) { 15306 /* 15307 * Let the recovery thread be responsible for 15308 * removing the state for CLOSE. 15309 */ 15310 close_failed = 1; 15311 force_close = 0; 15312 retry = 0; 15313 } 15314 15315 /* See if we need to retry with a different cred */ 15316 if ((ep->error == EACCES || 15317 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) && 15318 cred_otw != cr) { 15319 crfree(cred_otw); 15320 cred_otw = cr; 15321 crhold(cred_otw); 15322 retry = 1; 15323 } 15324 15325 if (ep->error || ep->stat) 15326 close_failed = 1; 15327 15328 if (retry && !isrecov && num_retries-- > 0) { 15329 if (have_sync_lock) { 15330 mutex_exit(&osp->os_sync_lock); 15331 have_sync_lock = 0; 15332 } 15333 if (did_start_seqid_sync) { 15334 nfs4_end_open_seqid_sync(oop); 15335 did_start_seqid_sync = 0; 15336 } 15337 open_stream_rele(osp, rp); 15338 15339 if (did_start_op) 15340 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, 15341 &recov_state, FALSE); 15342 if (did_force_recovlock) 15343 nfs_rw_exit(&mi->mi_recovlock); 15344 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, 15345 "nfs4close_one: need to retry the close " 15346 "operation")); 15347 goto recov_retry; 15348 } 15349 close_cleanup: 15350 /* 15351 * Seventh and lastly, process our results. 15352 */ 15353 if (close_failed && force_close) { 15354 /* 15355 * It's ok to drop and regrab the 'os_sync_lock' since 15356 * nfs4close_notw() will recheck to make sure the 15357 * "close"/removal of state should happen. 15358 */ 15359 if (!have_sync_lock) { 15360 mutex_enter(&osp->os_sync_lock); 15361 have_sync_lock = 1; 15362 } 15363 /* 15364 * This is last call, remove the ref on the open 15365 * stream created by open and clean everything up. 15366 */ 15367 osp->os_pending_close = 0; 15368 nfs4close_notw(vp, osp, &have_sync_lock); 15369 nfs4_error_zinit(ep); 15370 } 15371 15372 if (!close_failed) { 15373 if (have_sync_lock) { 15374 osp->os_pending_close = 0; 15375 mutex_exit(&osp->os_sync_lock); 15376 have_sync_lock = 0; 15377 } else { 15378 mutex_enter(&osp->os_sync_lock); 15379 osp->os_pending_close = 0; 15380 mutex_exit(&osp->os_sync_lock); 15381 } 15382 if (did_start_op && recov_state.rs_sp != NULL) { 15383 mutex_enter(&recov_state.rs_sp->s_lock); 15384 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi); 15385 mutex_exit(&recov_state.rs_sp->s_lock); 15386 } else { 15387 nfs4_dec_state_ref_count(mi); 15388 } 15389 nfs4_error_zinit(ep); 15390 } 15391 15392 out: 15393 if (have_sync_lock) 15394 mutex_exit(&osp->os_sync_lock); 15395 if (did_start_op) 15396 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state, 15397 recovonly ? TRUE : FALSE); 15398 if (did_force_recovlock) 15399 nfs_rw_exit(&mi->mi_recovlock); 15400 if (cred_otw) 15401 crfree(cred_otw); 15402 if (osp) 15403 open_stream_rele(osp, rp); 15404 if (oop) { 15405 if (did_start_seqid_sync) 15406 nfs4_end_open_seqid_sync(oop); 15407 open_owner_rele(oop); 15408 } 15409 } 15410 15411 /* 15412 * Convert information returned by the server in the LOCK4denied 15413 * structure to the form required by fcntl. 15414 */ 15415 static void 15416 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args) 15417 { 15418 nfs4_lo_name_t *lo; 15419 15420 #ifdef DEBUG 15421 if (denied_to_flk_debug) { 15422 lockt_denied_debug = lockt_denied; 15423 debug_enter("lockt_denied"); 15424 } 15425 #endif 15426 15427 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK; 15428 flk->l_whence = 0; /* aka SEEK_SET */ 15429 flk->l_start = lockt_denied->offset; 15430 flk->l_len = lockt_denied->length; 15431 15432 /* 15433 * If the blocking clientid matches our client id, then we can 15434 * interpret the lockowner (since we built it). If not, then 15435 * fabricate a sysid and pid. Note that the l_sysid field 15436 * in *flk already has the local sysid. 15437 */ 15438 15439 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) { 15440 15441 if (lockt_denied->owner.owner_len == sizeof (*lo)) { 15442 lo = (nfs4_lo_name_t *) 15443 lockt_denied->owner.owner_val; 15444 15445 flk->l_pid = lo->ln_pid; 15446 } else { 15447 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15448 "denied_to_flk: bad lock owner length\n")); 15449 15450 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15451 } 15452 } else { 15453 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, 15454 "denied_to_flk: foreign clientid\n")); 15455 15456 /* 15457 * Construct a new sysid which should be different from 15458 * sysids of other systems. 15459 */ 15460 15461 flk->l_sysid++; 15462 flk->l_pid = lo_to_pid(&lockt_denied->owner); 15463 } 15464 } 15465 15466 static pid_t 15467 lo_to_pid(lock_owner4 *lop) 15468 { 15469 pid_t pid = 0; 15470 uchar_t *cp; 15471 int i; 15472 15473 cp = (uchar_t *)&lop->clientid; 15474 15475 for (i = 0; i < sizeof (lop->clientid); i++) 15476 pid += (pid_t)*cp++; 15477 15478 cp = (uchar_t *)lop->owner_val; 15479 15480 for (i = 0; i < lop->owner_len; i++) 15481 pid += (pid_t)*cp++; 15482 15483 return (pid); 15484 } 15485 15486 /* 15487 * Given a lock pointer, returns the length of that lock. 15488 * "end" is the last locked offset the "l_len" covers from 15489 * the start of the lock. 15490 */ 15491 static off64_t 15492 lock_to_end(flock64_t *lock) 15493 { 15494 off64_t lock_end; 15495 15496 if (lock->l_len == 0) 15497 lock_end = (off64_t)MAXEND; 15498 else 15499 lock_end = lock->l_start + lock->l_len - 1; 15500 15501 return (lock_end); 15502 } 15503 15504 /* 15505 * Given the end of a lock, it will return you the length "l_len" for that lock. 15506 */ 15507 static off64_t 15508 end_to_len(off64_t start, off64_t end) 15509 { 15510 off64_t lock_len; 15511 15512 ASSERT(end >= start); 15513 if (end == MAXEND) 15514 lock_len = 0; 15515 else 15516 lock_len = end - start + 1; 15517 15518 return (lock_len); 15519 } 15520 15521 /* 15522 * On given end for a lock it determines if it is the last locked offset 15523 * or not, if so keeps it as is, else adds one to return the length for 15524 * valid start. 15525 */ 15526 static off64_t 15527 start_check(off64_t x) 15528 { 15529 if (x == MAXEND) 15530 return (x); 15531 else 15532 return (x + 1); 15533 } 15534 15535 /* 15536 * See if these two locks overlap, and if so return 1; 15537 * otherwise, return 0. 15538 */ 15539 static int 15540 locks_intersect(flock64_t *llfp, flock64_t *curfp) 15541 { 15542 off64_t llfp_end, curfp_end; 15543 15544 llfp_end = lock_to_end(llfp); 15545 curfp_end = lock_to_end(curfp); 15546 15547 if (((llfp_end >= curfp->l_start) && 15548 (llfp->l_start <= curfp->l_start)) || 15549 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start))) 15550 return (1); 15551 return (0); 15552 } 15553 15554 /* 15555 * Determine what the interseting lock region is, and add that to the 15556 * 'nl_llpp' locklist in increasing order (by l_start). 15557 */ 15558 static void 15559 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp, 15560 locklist_t **nl_llpp, vnode_t *vp) 15561 { 15562 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp; 15563 off64_t lost_flp_end, local_flp_end, len, start; 15564 15565 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:")); 15566 15567 if (!locks_intersect(lost_flp, local_flp)) 15568 return; 15569 15570 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15571 "locks intersect")); 15572 15573 lost_flp_end = lock_to_end(lost_flp); 15574 local_flp_end = lock_to_end(local_flp); 15575 15576 /* Find the starting point of the intersecting region */ 15577 if (local_flp->l_start > lost_flp->l_start) 15578 start = local_flp->l_start; 15579 else 15580 start = lost_flp->l_start; 15581 15582 /* Find the lenght of the intersecting region */ 15583 if (lost_flp_end < local_flp_end) 15584 len = end_to_len(start, lost_flp_end); 15585 else 15586 len = end_to_len(start, local_flp_end); 15587 15588 /* 15589 * Prepare the flock structure for the intersection found and insert 15590 * it into the new list in increasing l_start order. This list contains 15591 * intersections of locks registered by the client with the local host 15592 * and the lost lock. 15593 * The lock type of this lock is the same as that of the local_flp. 15594 */ 15595 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP); 15596 intersect_llp->ll_flock.l_start = start; 15597 intersect_llp->ll_flock.l_len = len; 15598 intersect_llp->ll_flock.l_type = local_flp->l_type; 15599 intersect_llp->ll_flock.l_pid = local_flp->l_pid; 15600 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid; 15601 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */ 15602 intersect_llp->ll_vp = vp; 15603 15604 tmp_fllp = *nl_llpp; 15605 cur_fllp = NULL; 15606 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start < 15607 intersect_llp->ll_flock.l_start) { 15608 cur_fllp = tmp_fllp; 15609 tmp_fllp = tmp_fllp->ll_next; 15610 } 15611 if (cur_fllp == NULL) { 15612 /* first on the list */ 15613 intersect_llp->ll_next = *nl_llpp; 15614 *nl_llpp = intersect_llp; 15615 } else { 15616 intersect_llp->ll_next = cur_fllp->ll_next; 15617 cur_fllp->ll_next = intersect_llp; 15618 } 15619 15620 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: " 15621 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n", 15622 intersect_llp->ll_flock.l_start, 15623 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len, 15624 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE")); 15625 } 15626 15627 /* 15628 * Our local locking current state is potentially different than 15629 * what the NFSv4 server thinks we have due to a lost lock that was 15630 * resent and then received. We need to reset our "NFSv4" locking 15631 * state to match the current local locking state for this pid since 15632 * that is what the user/application sees as what the world is. 15633 * 15634 * We cannot afford to drop the open/lock seqid sync since then we can 15635 * get confused about what the current local locking state "is" versus 15636 * "was". 15637 * 15638 * If we are unable to fix up the locks, we send SIGLOST to the affected 15639 * process. This is not done if the filesystem has been forcibly 15640 * unmounted, in case the process has already exited and a new process 15641 * exists with the same pid. 15642 */ 15643 static void 15644 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr, 15645 nfs4_lock_owner_t *lop) 15646 { 15647 locklist_t *locks, *llp, *ri_llp, *tmp_llp; 15648 mntinfo4_t *mi = VTOMI4(vp); 15649 const int cmd = F_SETLK; 15650 off64_t cur_start, llp_ll_flock_end, lost_flp_end; 15651 flock64_t ul_fl; 15652 15653 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15654 "nfs4_reinstitute_local_lock_state")); 15655 15656 /* 15657 * Find active locks for this vp from the local locking code. 15658 * Scan through this list and find out the locks that intersect with 15659 * the lost lock. Once we find the lock that intersects, add the 15660 * intersection area as a new lock to a new list "ri_llp". The lock 15661 * type of the intersection region lock added to ri_llp is the same 15662 * as that found in the active lock list, "list". The intersecting 15663 * region locks are added to ri_llp in increasing l_start order. 15664 */ 15665 ASSERT(nfs_zone() == mi->mi_zone); 15666 15667 locks = flk_active_locks_for_vp(vp); 15668 ri_llp = NULL; 15669 15670 for (llp = locks; llp != NULL; llp = llp->ll_next) { 15671 ASSERT(llp->ll_vp == vp); 15672 /* 15673 * Pick locks that belong to this pid/lockowner 15674 */ 15675 if (llp->ll_flock.l_pid != lost_flp->l_pid) 15676 continue; 15677 15678 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp); 15679 } 15680 15681 /* 15682 * Now we have the list of intersections with the lost lock. These are 15683 * the locks that were/are active before the server replied to the 15684 * last/lost lock. Issue these locks to the server here. Playing these 15685 * locks to the server will re-establish aur current local locking state 15686 * with the v4 server. 15687 * If we get an error, send SIGLOST to the application for that lock. 15688 */ 15689 15690 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15691 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15692 "nfs4_reinstitute_local_lock_state: need to issue " 15693 "flock: [%"PRIx64" - %"PRIx64"] : %s", 15694 llp->ll_flock.l_start, 15695 llp->ll_flock.l_start + llp->ll_flock.l_len, 15696 llp->ll_flock.l_type == F_RDLCK ? "READ" : 15697 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID")); 15698 /* 15699 * No need to relock what we already have 15700 */ 15701 if (llp->ll_flock.l_type == lost_flp->l_type) 15702 continue; 15703 15704 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop); 15705 } 15706 15707 /* 15708 * Now keeping the start of the lost lock as our reference parse the 15709 * newly created ri_llp locklist to find the ranges that we have locked 15710 * with the v4 server but not in the current local locking. We need 15711 * to unlock these ranges. 15712 * These ranges can also be reffered to as those ranges, where the lost 15713 * lock does not overlap with the locks in the ri_llp but are locked 15714 * since the server replied to the lost lock. 15715 */ 15716 cur_start = lost_flp->l_start; 15717 lost_flp_end = lock_to_end(lost_flp); 15718 15719 ul_fl.l_type = F_UNLCK; 15720 ul_fl.l_whence = 0; /* aka SEEK_SET */ 15721 ul_fl.l_sysid = lost_flp->l_sysid; 15722 ul_fl.l_pid = lost_flp->l_pid; 15723 15724 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) { 15725 llp_ll_flock_end = lock_to_end(&llp->ll_flock); 15726 15727 if (llp->ll_flock.l_start <= cur_start) { 15728 cur_start = start_check(llp_ll_flock_end); 15729 continue; 15730 } 15731 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15732 "nfs4_reinstitute_local_lock_state: " 15733 "UNLOCK [%"PRIx64" - %"PRIx64"]", 15734 cur_start, llp->ll_flock.l_start)); 15735 15736 ul_fl.l_start = cur_start; 15737 ul_fl.l_len = end_to_len(cur_start, 15738 (llp->ll_flock.l_start - 1)); 15739 15740 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15741 cur_start = start_check(llp_ll_flock_end); 15742 } 15743 15744 /* 15745 * In the case where the lost lock ends after all intersecting locks, 15746 * unlock the last part of the lost lock range. 15747 */ 15748 if (cur_start != start_check(lost_flp_end)) { 15749 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, 15750 "nfs4_reinstitute_local_lock_state: UNLOCK end of the " 15751 "lost lock region [%"PRIx64" - %"PRIx64"]", 15752 cur_start, lost_flp->l_start + lost_flp->l_len)); 15753 15754 ul_fl.l_start = cur_start; 15755 /* 15756 * Is it an to-EOF lock? if so unlock till the end 15757 */ 15758 if (lost_flp->l_len == 0) 15759 ul_fl.l_len = 0; 15760 else 15761 ul_fl.l_len = start_check(lost_flp_end) - cur_start; 15762 15763 push_reinstate(vp, cmd, &ul_fl, cr, lop); 15764 } 15765 15766 if (locks != NULL) 15767 flk_free_locklist(locks); 15768 15769 /* Free up our newly created locklist */ 15770 for (llp = ri_llp; llp != NULL; ) { 15771 tmp_llp = llp->ll_next; 15772 kmem_free(llp, sizeof (locklist_t)); 15773 llp = tmp_llp; 15774 } 15775 15776 /* 15777 * Now return back to the original calling nfs4frlock() 15778 * and let us naturally drop our seqid syncs. 15779 */ 15780 } 15781 15782 /* 15783 * Create a lost state record for the given lock reinstantiation request 15784 * and push it onto the lost state queue. 15785 */ 15786 static void 15787 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr, 15788 nfs4_lock_owner_t *lop) 15789 { 15790 nfs4_lost_rqst_t req; 15791 nfs_lock_type4 locktype; 15792 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS }; 15793 15794 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone); 15795 15796 locktype = flk_to_locktype(cmd, flk->l_type); 15797 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype, 15798 NULL, NULL, lop, flk, &req, cr, vp); 15799 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL, 15800 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ? 15801 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK, 15802 NULL); 15803 } 15804