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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T. 29 * All rights reserved. 30 */ 31 32 #pragma ident "%Z%%M% %I% %E% SMI" 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/systm.h> 37 #include <sys/cred.h> 38 #include <sys/time.h> 39 #include <sys/vnode.h> 40 #include <sys/vfs.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/atomic.h> 64 #include <sys/policy.h> 65 #include <sys/sdt.h> 66 67 #include <rpc/types.h> 68 #include <rpc/auth.h> 69 #include <rpc/clnt.h> 70 71 #include <nfs/nfs.h> 72 #include <nfs/nfs_clnt.h> 73 #include <nfs/rnode.h> 74 #include <nfs/nfs_acl.h> 75 #include <nfs/lm.h> 76 77 #include <vm/hat.h> 78 #include <vm/as.h> 79 #include <vm/page.h> 80 #include <vm/pvn.h> 81 #include <vm/seg.h> 82 #include <vm/seg_map.h> 83 #include <vm/seg_kpm.h> 84 #include <vm/seg_vn.h> 85 86 #include <fs/fs_subr.h> 87 88 #include <sys/ddi.h> 89 90 static int nfs3_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int, 91 cred_t *); 92 static int nfs3write(vnode_t *, caddr_t, u_offset_t, int, cred_t *, 93 stable_how *); 94 static int nfs3read(vnode_t *, caddr_t, offset_t, int, size_t *, cred_t *); 95 static int nfs3setattr(vnode_t *, struct vattr *, int, cred_t *); 96 static int nfs3_accessx(void *, int, cred_t *); 97 static int nfs3lookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *); 98 static int nfs3lookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int); 99 static int nfs3create(vnode_t *, char *, struct vattr *, enum vcexcl, 100 int, vnode_t **, cred_t *, int); 101 static int nfs3excl_create_settimes(vnode_t *, struct vattr *, cred_t *); 102 static int nfs3mknod(vnode_t *, char *, struct vattr *, enum vcexcl, 103 int, vnode_t **, cred_t *); 104 static int nfs3rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 105 static int do_nfs3readdir(vnode_t *, rddir_cache *, cred_t *); 106 static void nfs3readdir(vnode_t *, rddir_cache *, cred_t *); 107 static void nfs3readdirplus(vnode_t *, rddir_cache *, cred_t *); 108 static int nfs3_bio(struct buf *, stable_how *, cred_t *); 109 static int nfs3_getapage(vnode_t *, u_offset_t, size_t, uint_t *, 110 page_t *[], size_t, struct seg *, caddr_t, 111 enum seg_rw, cred_t *); 112 static void nfs3_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *, 113 cred_t *); 114 static int nfs3_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t, 115 int, cred_t *); 116 static int nfs3_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t, 117 int, cred_t *); 118 static int nfs3_commit(vnode_t *, offset3, count3, cred_t *); 119 static void nfs3_set_mod(vnode_t *); 120 static void nfs3_get_commit(vnode_t *); 121 static void nfs3_get_commit_range(vnode_t *, u_offset_t, size_t); 122 #if 0 /* unused */ 123 #ifdef DEBUG 124 static int nfs3_no_uncommitted_pages(vnode_t *); 125 #endif 126 #endif /* unused */ 127 static int nfs3_putpage_commit(vnode_t *, offset_t, size_t, cred_t *); 128 static int nfs3_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *); 129 static int nfs3_sync_commit(vnode_t *, page_t *, offset3, count3, 130 cred_t *); 131 static void nfs3_async_commit(vnode_t *, page_t *, offset3, count3, 132 cred_t *); 133 static void nfs3_delmap_callback(struct as *, void *, uint_t); 134 135 /* 136 * Error flags used to pass information about certain special errors 137 * which need to be handled specially. 138 */ 139 #define NFS_EOF -98 140 #define NFS_VERF_MISMATCH -97 141 142 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */ 143 #define ALIGN64(x, ptr, sz) \ 144 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \ 145 if (x) { \ 146 x = sizeof (uint64_t) - (x); \ 147 sz -= (x); \ 148 ptr += (x); \ 149 } 150 151 /* 152 * These are the vnode ops routines which implement the vnode interface to 153 * the networked file system. These routines just take their parameters, 154 * make them look networkish by putting the right info into interface structs, 155 * and then calling the appropriate remote routine(s) to do the work. 156 * 157 * Note on directory name lookup cacheing: If we detect a stale fhandle, 158 * we purge the directory cache relative to that vnode. This way, the 159 * user won't get burned by the cache repeatedly. See <nfs/rnode.h> for 160 * more details on rnode locking. 161 */ 162 163 static int nfs3_open(vnode_t **, int, cred_t *); 164 static int nfs3_close(vnode_t *, int, int, offset_t, cred_t *); 165 static int nfs3_read(vnode_t *, struct uio *, int, cred_t *, 166 caller_context_t *); 167 static int nfs3_write(vnode_t *, struct uio *, int, cred_t *, 168 caller_context_t *); 169 static int nfs3_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *); 170 static int nfs3_getattr(vnode_t *, struct vattr *, int, cred_t *); 171 static int nfs3_setattr(vnode_t *, struct vattr *, int, cred_t *, 172 caller_context_t *); 173 static int nfs3_access(vnode_t *, int, int, cred_t *); 174 static int nfs3_readlink(vnode_t *, struct uio *, cred_t *); 175 static int nfs3_fsync(vnode_t *, int, cred_t *); 176 static void nfs3_inactive(vnode_t *, cred_t *); 177 static int nfs3_lookup(vnode_t *, char *, vnode_t **, 178 struct pathname *, int, vnode_t *, cred_t *); 179 static int nfs3_create(vnode_t *, char *, struct vattr *, enum vcexcl, 180 int, vnode_t **, cred_t *, int); 181 static int nfs3_remove(vnode_t *, char *, cred_t *); 182 static int nfs3_link(vnode_t *, vnode_t *, char *, cred_t *); 183 static int nfs3_rename(vnode_t *, char *, vnode_t *, char *, cred_t *); 184 static int nfs3_mkdir(vnode_t *, char *, struct vattr *, 185 vnode_t **, cred_t *); 186 static int nfs3_rmdir(vnode_t *, char *, vnode_t *, cred_t *); 187 static int nfs3_symlink(vnode_t *, char *, struct vattr *, char *, 188 cred_t *); 189 static int nfs3_readdir(vnode_t *, struct uio *, cred_t *, int *); 190 static int nfs3_fid(vnode_t *, fid_t *); 191 static int nfs3_rwlock(vnode_t *, int, caller_context_t *); 192 static void nfs3_rwunlock(vnode_t *, int, caller_context_t *); 193 static int nfs3_seek(vnode_t *, offset_t, offset_t *); 194 static int nfs3_getpage(vnode_t *, offset_t, size_t, uint_t *, 195 page_t *[], size_t, struct seg *, caddr_t, 196 enum seg_rw, cred_t *); 197 static int nfs3_putpage(vnode_t *, offset_t, size_t, int, cred_t *); 198 static int nfs3_map(vnode_t *, offset_t, struct as *, caddr_t *, 199 size_t, uchar_t, uchar_t, uint_t, cred_t *); 200 static int nfs3_addmap(vnode_t *, offset_t, struct as *, caddr_t, 201 size_t, uchar_t, uchar_t, uint_t, cred_t *); 202 static int nfs3_frlock(vnode_t *, int, struct flock64 *, int, offset_t, 203 struct flk_callback *, cred_t *); 204 static int nfs3_space(vnode_t *, int, struct flock64 *, int, offset_t, 205 cred_t *, caller_context_t *); 206 static int nfs3_realvp(vnode_t *, vnode_t **); 207 static int nfs3_delmap(vnode_t *, offset_t, struct as *, caddr_t, 208 size_t, uint_t, uint_t, uint_t, cred_t *); 209 static int nfs3_pathconf(vnode_t *, int, ulong_t *, cred_t *); 210 static int nfs3_pageio(vnode_t *, page_t *, u_offset_t, size_t, int, 211 cred_t *); 212 static void nfs3_dispose(vnode_t *, page_t *, int, int, cred_t *); 213 static int nfs3_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 214 static int nfs3_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *); 215 static int nfs3_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *); 216 217 struct vnodeops *nfs3_vnodeops; 218 219 const fs_operation_def_t nfs3_vnodeops_template[] = { 220 VOPNAME_OPEN, nfs3_open, 221 VOPNAME_CLOSE, nfs3_close, 222 VOPNAME_READ, nfs3_read, 223 VOPNAME_WRITE, nfs3_write, 224 VOPNAME_IOCTL, nfs3_ioctl, 225 VOPNAME_GETATTR, nfs3_getattr, 226 VOPNAME_SETATTR, nfs3_setattr, 227 VOPNAME_ACCESS, nfs3_access, 228 VOPNAME_LOOKUP, nfs3_lookup, 229 VOPNAME_CREATE, nfs3_create, 230 VOPNAME_REMOVE, nfs3_remove, 231 VOPNAME_LINK, nfs3_link, 232 VOPNAME_RENAME, nfs3_rename, 233 VOPNAME_MKDIR, nfs3_mkdir, 234 VOPNAME_RMDIR, nfs3_rmdir, 235 VOPNAME_READDIR, nfs3_readdir, 236 VOPNAME_SYMLINK, nfs3_symlink, 237 VOPNAME_READLINK, nfs3_readlink, 238 VOPNAME_FSYNC, nfs3_fsync, 239 VOPNAME_INACTIVE, (fs_generic_func_p) nfs3_inactive, 240 VOPNAME_FID, nfs3_fid, 241 VOPNAME_RWLOCK, nfs3_rwlock, 242 VOPNAME_RWUNLOCK, (fs_generic_func_p) nfs3_rwunlock, 243 VOPNAME_SEEK, nfs3_seek, 244 VOPNAME_FRLOCK, nfs3_frlock, 245 VOPNAME_SPACE, nfs3_space, 246 VOPNAME_REALVP, nfs3_realvp, 247 VOPNAME_GETPAGE, nfs3_getpage, 248 VOPNAME_PUTPAGE, nfs3_putpage, 249 VOPNAME_MAP, (fs_generic_func_p) nfs3_map, 250 VOPNAME_ADDMAP, (fs_generic_func_p) nfs3_addmap, 251 VOPNAME_DELMAP, nfs3_delmap, 252 VOPNAME_DUMP, nfs_dump, /* there is no separate nfs3_dump */ 253 VOPNAME_PATHCONF, nfs3_pathconf, 254 VOPNAME_PAGEIO, nfs3_pageio, 255 VOPNAME_DISPOSE, (fs_generic_func_p) nfs3_dispose, 256 VOPNAME_SETSECATTR, nfs3_setsecattr, 257 VOPNAME_GETSECATTR, nfs3_getsecattr, 258 VOPNAME_SHRLOCK, nfs3_shrlock, 259 NULL, NULL 260 }; 261 262 /* 263 * XXX: This is referenced in modstubs.s 264 */ 265 struct vnodeops * 266 nfs3_getvnodeops(void) 267 { 268 return (nfs3_vnodeops); 269 } 270 271 /* ARGSUSED */ 272 static int 273 nfs3_open(vnode_t **vpp, int flag, cred_t *cr) 274 { 275 int error; 276 struct vattr va; 277 rnode_t *rp; 278 vnode_t *vp; 279 280 vp = *vpp; 281 if (curproc->p_zone != VTOMI(vp)->mi_zone) 282 return (EIO); 283 rp = VTOR(vp); 284 mutex_enter(&rp->r_statelock); 285 if (rp->r_cred == NULL) { 286 crhold(cr); 287 rp->r_cred = cr; 288 } 289 mutex_exit(&rp->r_statelock); 290 291 /* 292 * If there is no cached data or if close-to-open 293 * consistency checking is turned off, we can avoid 294 * the over the wire getattr. Otherwise, if the 295 * file system is mounted readonly, then just verify 296 * the caches are up to date using the normal mechanism. 297 * Else, if the file is not mmap'd, then just mark 298 * the attributes as timed out. They will be refreshed 299 * and the caches validated prior to being used. 300 * Else, the file system is mounted writeable so 301 * force an over the wire GETATTR in order to ensure 302 * that all cached data is valid. 303 */ 304 if (vp->v_count > 1 || 305 ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) && 306 !(VTOMI(vp)->mi_flags & MI_NOCTO))) { 307 if (vn_is_readonly(vp)) 308 error = nfs3_validate_caches(vp, cr); 309 else if (rp->r_mapcnt == 0 && vp->v_count == 1) { 310 PURGE_ATTRCACHE(vp); 311 error = 0; 312 } else { 313 va.va_mask = AT_ALL; 314 error = nfs3_getattr_otw(vp, &va, cr); 315 } 316 } else 317 error = 0; 318 319 return (error); 320 } 321 322 static int 323 nfs3_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 324 { 325 rnode_t *rp; 326 int error; 327 struct vattr va; 328 329 /* 330 * zone_enter(2) prevents processes from changing zones with NFS files 331 * open; if we happen to get here from the wrong zone we can't do 332 * anything over the wire. 333 */ 334 if (VTOMI(vp)->mi_zone != curproc->p_zone) { 335 /* 336 * We could attempt to clean up locks, except we're sure 337 * that the current process didn't acquire any locks on 338 * the file: any attempt to lock a file belong to another zone 339 * will fail, and one can't lock an NFS file and then change 340 * zones, as that fails too. 341 * 342 * Returning an error here is the sane thing to do. A 343 * subsequent call to VN_RELE() which translates to a 344 * nfs3_inactive() will clean up state: if the zone of the 345 * vnode's origin is still alive and kicking, an async worker 346 * thread will handle the request (from the correct zone), and 347 * everything (minus the commit and final nfs3_getattr_otw() 348 * call) should be OK. If the zone is going away 349 * nfs_async_inactive() will throw away cached pages inline. 350 */ 351 return (EIO); 352 } 353 354 /* 355 * If we are using local locking for this filesystem, then 356 * release all of the SYSV style record locks. Otherwise, 357 * we are doing network locking and we need to release all 358 * of the network locks. All of the locks held by this 359 * process on this file are released no matter what the 360 * incoming reference count is. 361 */ 362 if (VTOMI(vp)->mi_flags & MI_LLOCK) { 363 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 364 cleanshares(vp, ttoproc(curthread)->p_pid); 365 } else 366 nfs_lockrelease(vp, flag, offset, cr); 367 368 if (count > 1) 369 return (0); 370 371 /* 372 * If the file has been `unlinked', then purge the 373 * DNLC so that this vnode will get reycled quicker 374 * and the .nfs* file on the server will get removed. 375 */ 376 rp = VTOR(vp); 377 if (rp->r_unldvp != NULL) 378 dnlc_purge_vp(vp); 379 380 /* 381 * If the file was open for write and there are pages, 382 * then if the file system was mounted using the "no-close- 383 * to-open" semantics, then start an asynchronous flush 384 * of the all of the pages in the file. 385 * else the file system was not mounted using the "no-close- 386 * to-open" semantics, then do a synchronous flush and 387 * commit of all of the dirty and uncommitted pages. 388 * 389 * The asynchronous flush of the pages in the "nocto" path 390 * mostly just associates a cred pointer with the rnode so 391 * writes which happen later will have a better chance of 392 * working. It also starts the data being written to the 393 * server, but without unnecessarily delaying the application. 394 */ 395 if ((flag & FWRITE) && vn_has_cached_data(vp)) { 396 if (VTOMI(vp)->mi_flags & MI_NOCTO) { 397 error = nfs3_putpage(vp, (offset_t)0, 0, B_ASYNC, cr); 398 if (error == EAGAIN) 399 error = 0; 400 } else 401 error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr); 402 if (!error) { 403 mutex_enter(&rp->r_statelock); 404 error = rp->r_error; 405 rp->r_error = 0; 406 mutex_exit(&rp->r_statelock); 407 } 408 } else { 409 mutex_enter(&rp->r_statelock); 410 error = rp->r_error; 411 rp->r_error = 0; 412 mutex_exit(&rp->r_statelock); 413 } 414 415 /* 416 * If RWRITEATTR is set, then issue an over the wire GETATTR to 417 * refresh the attribute cache with a set of attributes which 418 * weren't returned from a WRITE. This will enable the close- 419 * to-open processing to work. 420 */ 421 if (rp->r_flags & RWRITEATTR) 422 (void) nfs3_getattr_otw(vp, &va, cr); 423 424 return (error); 425 } 426 427 /* ARGSUSED */ 428 static int 429 nfs3_directio_read(vnode_t *vp, struct uio *uiop, cred_t *cr) 430 { 431 mntinfo_t *mi; 432 READ3args args; 433 READ3uiores res; 434 int tsize; 435 offset_t offset; 436 ssize_t count; 437 int error; 438 int douprintf; 439 failinfo_t fi; 440 char *sv_hostname; 441 442 mi = VTOMI(vp); 443 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 444 sv_hostname = VTOR(vp)->r_server->sv_hostname; 445 446 douprintf = 1; 447 args.file = *VTOFH3(vp); 448 fi.vp = vp; 449 fi.fhp = (caddr_t)&args.file; 450 fi.copyproc = nfs3copyfh; 451 fi.lookupproc = nfs3lookup; 452 fi.xattrdirproc = acl_getxattrdir3; 453 454 res.uiop = uiop; 455 456 offset = uiop->uio_loffset; 457 count = uiop->uio_resid; 458 459 do { 460 if (mi->mi_io_kstats) { 461 mutex_enter(&mi->mi_lock); 462 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 463 mutex_exit(&mi->mi_lock); 464 } 465 466 do { 467 tsize = MIN(mi->mi_tsize, count); 468 args.offset = (offset3)offset; 469 args.count = (count3)tsize; 470 res.size = (uint_t)tsize; 471 error = rfs3call(mi, NFSPROC3_READ, 472 xdr_READ3args, (caddr_t)&args, 473 xdr_READ3uiores, (caddr_t)&res, cr, 474 &douprintf, &res.status, 0, &fi); 475 } while (error == ENFS_TRYAGAIN); 476 477 if (mi->mi_io_kstats) { 478 mutex_enter(&mi->mi_lock); 479 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 480 mutex_exit(&mi->mi_lock); 481 } 482 483 if (error) 484 return (error); 485 486 error = geterrno3(res.status); 487 if (error) 488 return (error); 489 490 if (res.count != res.size) { 491 zcmn_err(getzoneid(), CE_WARN, 492 "nfs3_directio_read: server %s returned incorrect amount", 493 sv_hostname); 494 return (EIO); 495 } 496 count -= res.count; 497 offset += res.count; 498 if (mi->mi_io_kstats) { 499 mutex_enter(&mi->mi_lock); 500 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 501 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count; 502 mutex_exit(&mi->mi_lock); 503 } 504 lwp_stat_update(LWP_STAT_INBLK, 1); 505 } while (count && !res.eof); 506 507 return (0); 508 } 509 510 /* ARGSUSED */ 511 static int 512 nfs3_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 513 caller_context_t *ct) 514 { 515 rnode_t *rp; 516 u_offset_t off; 517 offset_t diff; 518 int on; 519 size_t n; 520 caddr_t base; 521 uint_t flags; 522 int error = 0; 523 mntinfo_t *mi; 524 525 rp = VTOR(vp); 526 mi = VTOMI(vp); 527 528 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 529 530 if (curproc->p_zone != mi->mi_zone) 531 return (EIO); 532 533 if (vp->v_type != VREG) 534 return (EISDIR); 535 536 if (uiop->uio_resid == 0) 537 return (0); 538 539 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0) 540 return (EINVAL); 541 542 /* 543 * Bypass VM if caching has been disabled (e.g., locking) or if 544 * using client-side direct I/O and the file is not mmap'd and 545 * there are no cached pages. 546 */ 547 if ((vp->v_flag & VNOCACHE) || 548 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) && 549 rp->r_mapcnt == 0 && !vn_has_cached_data(vp))) { 550 return (nfs3_directio_read(vp, uiop, cr)); 551 } 552 553 do { 554 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 555 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 556 n = MIN(MAXBSIZE - on, uiop->uio_resid); 557 558 error = nfs3_validate_caches(vp, cr); 559 if (error) 560 break; 561 562 mutex_enter(&rp->r_statelock); 563 diff = rp->r_size - uiop->uio_loffset; 564 mutex_exit(&rp->r_statelock); 565 if (diff <= 0) 566 break; 567 if (diff < n) 568 n = (size_t)diff; 569 570 base = segmap_getmapflt(segkmap, vp, off + on, n, 1, S_READ); 571 572 error = uiomove(base + on, n, UIO_READ, uiop); 573 574 if (!error) { 575 /* 576 * If read a whole block or read to eof, 577 * won't need this buffer again soon. 578 */ 579 mutex_enter(&rp->r_statelock); 580 if (n + on == MAXBSIZE || 581 uiop->uio_loffset == rp->r_size) 582 flags = SM_DONTNEED; 583 else 584 flags = 0; 585 mutex_exit(&rp->r_statelock); 586 error = segmap_release(segkmap, base, flags); 587 } else 588 (void) segmap_release(segkmap, base, 0); 589 } while (!error && uiop->uio_resid > 0); 590 591 return (error); 592 } 593 594 /* ARGSUSED */ 595 static int 596 nfs3_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr, 597 caller_context_t *ct) 598 { 599 rlim64_t limit = uiop->uio_llimit; 600 rnode_t *rp; 601 u_offset_t off; 602 caddr_t base; 603 uint_t flags; 604 int remainder; 605 size_t n; 606 int on; 607 int error; 608 int resid; 609 offset_t offset; 610 mntinfo_t *mi; 611 uint_t bsize; 612 613 rp = VTOR(vp); 614 615 if (vp->v_type != VREG) 616 return (EISDIR); 617 618 mi = VTOMI(vp); 619 if (curproc->p_zone != mi->mi_zone) 620 return (EIO); 621 if (uiop->uio_resid == 0) 622 return (0); 623 624 if (ioflag & FAPPEND) { 625 struct vattr va; 626 627 /* 628 * Must serialize if appending. 629 */ 630 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) { 631 nfs_rw_exit(&rp->r_rwlock); 632 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, 633 INTR(vp))) 634 return (EINTR); 635 } 636 637 va.va_mask = AT_SIZE; 638 error = nfs3getattr(vp, &va, cr); 639 if (error) 640 return (error); 641 uiop->uio_loffset = va.va_size; 642 } 643 644 offset = uiop->uio_loffset + uiop->uio_resid; 645 646 if (uiop->uio_loffset < 0 || offset < 0) 647 return (EINVAL); 648 649 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 650 limit = MAXOFFSET_T; 651 652 /* 653 * Check to make sure that the process will not exceed 654 * its limit on file size. It is okay to write up to 655 * the limit, but not beyond. Thus, the write which 656 * reaches the limit will be short and the next write 657 * will return an error. 658 */ 659 remainder = 0; 660 if (offset > limit) { 661 remainder = offset - limit; 662 uiop->uio_resid = limit - uiop->uio_loffset; 663 if (uiop->uio_resid <= 0) { 664 proc_t *p = ttoproc(curthread); 665 666 uiop->uio_resid += remainder; 667 mutex_enter(&p->p_lock); 668 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], 669 p->p_rctls, p, RCA_UNSAFE_SIGINFO); 670 mutex_exit(&p->p_lock); 671 return (EFBIG); 672 } 673 } 674 675 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) 676 return (EINTR); 677 678 /* 679 * Bypass VM if caching has been disabled (e.g., locking) or if 680 * using client-side direct I/O and the file is not mmap'd and 681 * there are no cached pages. 682 */ 683 if ((vp->v_flag & VNOCACHE) || 684 (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) && 685 rp->r_mapcnt == 0 && !vn_has_cached_data(vp))) { 686 size_t bufsize; 687 int count; 688 u_offset_t org_offset; 689 stable_how stab_comm; 690 691 nfs3_fwrite: 692 if (rp->r_flags & RSTALE) { 693 resid = uiop->uio_resid; 694 offset = uiop->uio_loffset; 695 error = rp->r_error; 696 goto bottom; 697 } 698 bufsize = MIN(uiop->uio_resid, mi->mi_stsize); 699 base = kmem_alloc(bufsize, KM_SLEEP); 700 do { 701 if (ioflag & FDSYNC) 702 stab_comm = DATA_SYNC; 703 else 704 stab_comm = FILE_SYNC; 705 resid = uiop->uio_resid; 706 offset = uiop->uio_loffset; 707 count = MIN(uiop->uio_resid, bufsize); 708 org_offset = uiop->uio_loffset; 709 error = uiomove(base, count, UIO_WRITE, uiop); 710 if (!error) { 711 error = nfs3write(vp, base, org_offset, 712 count, cr, &stab_comm); 713 } 714 } while (!error && uiop->uio_resid > 0); 715 kmem_free(base, bufsize); 716 goto bottom; 717 } 718 719 720 bsize = vp->v_vfsp->vfs_bsize; 721 722 do { 723 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */ 724 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */ 725 n = MIN(MAXBSIZE - on, uiop->uio_resid); 726 727 resid = uiop->uio_resid; 728 offset = uiop->uio_loffset; 729 730 if (rp->r_flags & RSTALE) { 731 error = rp->r_error; 732 break; 733 } 734 735 /* 736 * Don't create dirty pages faster than they 737 * can be cleaned so that the system doesn't 738 * get imbalanced. If the async queue is 739 * maxed out, then wait for it to drain before 740 * creating more dirty pages. Also, wait for 741 * any threads doing pagewalks in the vop_getattr 742 * entry points so that they don't block for 743 * long periods. 744 */ 745 mutex_enter(&rp->r_statelock); 746 while ((mi->mi_max_threads != 0 && 747 rp->r_awcount > 2 * mi->mi_max_threads) || 748 rp->r_gcount > 0) 749 cv_wait(&rp->r_cv, &rp->r_statelock); 750 mutex_exit(&rp->r_statelock); 751 752 if (segmap_kpm) { 753 int pon = uiop->uio_loffset & PAGEOFFSET; 754 size_t pn = MIN(PAGESIZE - pon, uiop->uio_resid); 755 int pagecreate; 756 757 mutex_enter(&rp->r_statelock); 758 pagecreate = (pon == 0) && (pn == PAGESIZE || 759 uiop->uio_loffset + pn >= rp->r_size); 760 mutex_exit(&rp->r_statelock); 761 762 base = segmap_getmapflt(segkmap, vp, off + on, 763 pn, !pagecreate, S_WRITE); 764 765 error = writerp(rp, base + pon, n, uiop, pagecreate); 766 767 } else { 768 base = segmap_getmapflt(segkmap, vp, off + on, 769 n, 0, S_READ); 770 error = writerp(rp, base + on, n, uiop, 0); 771 } 772 773 if (!error) { 774 if (mi->mi_flags & MI_NOAC) 775 flags = SM_WRITE; 776 else if ((uiop->uio_loffset % bsize) == 0 || 777 IS_SWAPVP(vp)) { 778 /* 779 * Have written a whole block. 780 * Start an asynchronous write 781 * and mark the buffer to 782 * indicate that it won't be 783 * needed again soon. 784 */ 785 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 786 } else 787 flags = 0; 788 if ((ioflag & (FSYNC|FDSYNC)) || 789 (rp->r_flags & ROUTOFSPACE)) { 790 flags &= ~SM_ASYNC; 791 flags |= SM_WRITE; 792 } 793 error = segmap_release(segkmap, base, flags); 794 } else { 795 (void) segmap_release(segkmap, base, 0); 796 /* 797 * In the event that we got an access error while 798 * faulting in a page for a write-only file just 799 * force a write. 800 */ 801 if (error == EACCES) 802 goto nfs3_fwrite; 803 } 804 } while (!error && uiop->uio_resid > 0); 805 806 bottom: 807 if (error) { 808 uiop->uio_resid = resid + remainder; 809 uiop->uio_loffset = offset; 810 } else 811 uiop->uio_resid += remainder; 812 813 nfs_rw_exit(&rp->r_lkserlock); 814 815 return (error); 816 } 817 818 /* 819 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 820 */ 821 static int 822 nfs3_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len, 823 int flags, cred_t *cr) 824 { 825 struct buf *bp; 826 int error; 827 page_t *savepp; 828 uchar_t fsdata; 829 stable_how stab_comm; 830 831 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 832 bp = pageio_setup(pp, len, vp, flags); 833 ASSERT(bp != NULL); 834 835 /* 836 * pageio_setup should have set b_addr to 0. This 837 * is correct since we want to do I/O on a page 838 * boundary. bp_mapin will use this addr to calculate 839 * an offset, and then set b_addr to the kernel virtual 840 * address it allocated for us. 841 */ 842 ASSERT(bp->b_un.b_addr == 0); 843 844 bp->b_edev = 0; 845 bp->b_dev = 0; 846 bp->b_lblkno = lbtodb(off); 847 bp->b_file = vp; 848 bp->b_offset = (offset_t)off; 849 bp_mapin(bp); 850 851 /* 852 * Calculate the desired level of stability to write data 853 * on the server and then mark all of the pages to reflect 854 * this. 855 */ 856 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) && 857 freemem > desfree) { 858 stab_comm = UNSTABLE; 859 fsdata = C_DELAYCOMMIT; 860 } else { 861 stab_comm = FILE_SYNC; 862 fsdata = C_NOCOMMIT; 863 } 864 865 savepp = pp; 866 do { 867 pp->p_fsdata = fsdata; 868 } while ((pp = pp->p_next) != savepp); 869 870 error = nfs3_bio(bp, &stab_comm, cr); 871 872 bp_mapout(bp); 873 pageio_done(bp); 874 875 /* 876 * If the server wrote pages in a more stable fashion than 877 * was requested, then clear all of the marks in the pages 878 * indicating that COMMIT operations were required. 879 */ 880 if (stab_comm != UNSTABLE && fsdata == C_DELAYCOMMIT) { 881 do { 882 pp->p_fsdata = C_NOCOMMIT; 883 } while ((pp = pp->p_next) != savepp); 884 } 885 886 return (error); 887 } 888 889 /* 890 * Write to file. Writes to remote server in largest size 891 * chunks that the server can handle. Write is synchronous. 892 */ 893 static int 894 nfs3write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr, 895 stable_how *stab_comm) 896 { 897 mntinfo_t *mi; 898 WRITE3args args; 899 WRITE3res res; 900 int error; 901 int tsize; 902 rnode_t *rp; 903 int douprintf; 904 905 rp = VTOR(vp); 906 mi = VTOMI(vp); 907 908 ASSERT(curproc->p_zone == mi->mi_zone); 909 910 args.file = *VTOFH3(vp); 911 args.stable = *stab_comm; 912 913 *stab_comm = FILE_SYNC; 914 915 douprintf = 1; 916 917 do { 918 if ((vp->v_flag & VNOCACHE) || 919 (rp->r_flags & RDIRECTIO) || 920 (mi->mi_flags & MI_DIRECTIO)) 921 tsize = MIN(mi->mi_stsize, count); 922 else 923 tsize = MIN(mi->mi_curwrite, count); 924 args.offset = (offset3)offset; 925 args.count = (count3)tsize; 926 args.data.data_len = (uint_t)tsize; 927 args.data.data_val = base; 928 929 if (mi->mi_io_kstats) { 930 mutex_enter(&mi->mi_lock); 931 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 932 mutex_exit(&mi->mi_lock); 933 } 934 args.mblk = NULL; 935 do { 936 error = rfs3call(mi, NFSPROC3_WRITE, 937 xdr_WRITE3args, (caddr_t)&args, 938 xdr_WRITE3res, (caddr_t)&res, cr, 939 &douprintf, &res.status, 0, NULL); 940 } while (error == ENFS_TRYAGAIN); 941 if (mi->mi_io_kstats) { 942 mutex_enter(&mi->mi_lock); 943 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 944 mutex_exit(&mi->mi_lock); 945 } 946 947 if (error) 948 return (error); 949 error = geterrno3(res.status); 950 if (!error) { 951 if (res.resok.count > args.count) { 952 zcmn_err(getzoneid(), CE_WARN, 953 "nfs3write: server %s wrote %u, " 954 "requested was %u", 955 rp->r_server->sv_hostname, 956 res.resok.count, args.count); 957 return (EIO); 958 } 959 if (res.resok.committed == UNSTABLE) { 960 *stab_comm = UNSTABLE; 961 if (args.stable == DATA_SYNC || 962 args.stable == FILE_SYNC) { 963 zcmn_err(getzoneid(), CE_WARN, 964 "nfs3write: server %s did not commit to stable storage", 965 rp->r_server->sv_hostname); 966 return (EIO); 967 } 968 } 969 tsize = (int)res.resok.count; 970 count -= tsize; 971 base += tsize; 972 offset += tsize; 973 if (mi->mi_io_kstats) { 974 mutex_enter(&mi->mi_lock); 975 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++; 976 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten += 977 tsize; 978 mutex_exit(&mi->mi_lock); 979 } 980 lwp_stat_update(LWP_STAT_OUBLK, 1); 981 mutex_enter(&rp->r_statelock); 982 if (rp->r_flags & RHAVEVERF) { 983 if (rp->r_verf != res.resok.verf) { 984 nfs3_set_mod(vp); 985 rp->r_verf = res.resok.verf; 986 /* 987 * If the data was written UNSTABLE, 988 * then might as well stop because 989 * the whole block will have to get 990 * rewritten anyway. 991 */ 992 if (*stab_comm == UNSTABLE) { 993 mutex_exit(&rp->r_statelock); 994 break; 995 } 996 } 997 } else { 998 rp->r_verf = res.resok.verf; 999 rp->r_flags |= RHAVEVERF; 1000 } 1001 /* 1002 * Mark the attribute cache as timed out and 1003 * set RWRITEATTR to indicate that the file 1004 * was modified with a WRITE operation and 1005 * that the attributes can not be trusted. 1006 */ 1007 PURGE_ATTRCACHE_LOCKED(rp); 1008 rp->r_flags |= RWRITEATTR; 1009 mutex_exit(&rp->r_statelock); 1010 } 1011 } while (!error && count); 1012 1013 return (error); 1014 } 1015 1016 /* 1017 * Read from a file. Reads data in largest chunks our interface can handle. 1018 */ 1019 static int 1020 nfs3read(vnode_t *vp, caddr_t base, offset_t offset, int count, 1021 size_t *residp, cred_t *cr) 1022 { 1023 mntinfo_t *mi; 1024 READ3args args; 1025 READ3vres res; 1026 int tsize; 1027 int error; 1028 int douprintf; 1029 failinfo_t fi; 1030 rnode_t *rp; 1031 struct vattr va; 1032 hrtime_t t; 1033 1034 rp = VTOR(vp); 1035 mi = VTOMI(vp); 1036 ASSERT(curproc->p_zone == mi->mi_zone); 1037 douprintf = 1; 1038 1039 args.file = *VTOFH3(vp); 1040 fi.vp = vp; 1041 fi.fhp = (caddr_t)&args.file; 1042 fi.copyproc = nfs3copyfh; 1043 fi.lookupproc = nfs3lookup; 1044 fi.xattrdirproc = acl_getxattrdir3; 1045 1046 res.pov.fres.vp = vp; 1047 res.pov.fres.vap = &va; 1048 1049 *residp = count; 1050 do { 1051 if (mi->mi_io_kstats) { 1052 mutex_enter(&mi->mi_lock); 1053 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 1054 mutex_exit(&mi->mi_lock); 1055 } 1056 1057 do { 1058 if ((vp->v_flag & VNOCACHE) || 1059 (rp->r_flags & RDIRECTIO) || 1060 (mi->mi_flags & MI_DIRECTIO)) 1061 tsize = MIN(mi->mi_tsize, count); 1062 else 1063 tsize = MIN(mi->mi_curread, count); 1064 res.data.data_val = base; 1065 res.data.data_len = tsize; 1066 args.offset = (offset3)offset; 1067 args.count = (count3)tsize; 1068 t = gethrtime(); 1069 error = rfs3call(mi, NFSPROC3_READ, 1070 xdr_READ3args, (caddr_t)&args, 1071 xdr_READ3vres, (caddr_t)&res, cr, 1072 &douprintf, &res.status, 0, &fi); 1073 } while (error == ENFS_TRYAGAIN); 1074 1075 if (mi->mi_io_kstats) { 1076 mutex_enter(&mi->mi_lock); 1077 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 1078 mutex_exit(&mi->mi_lock); 1079 } 1080 1081 if (error) 1082 return (error); 1083 1084 error = geterrno3(res.status); 1085 if (error) 1086 return (error); 1087 1088 if (res.count != res.data.data_len) { 1089 zcmn_err(getzoneid(), CE_WARN, 1090 "nfs3read: server %s returned incorrect amount", 1091 rp->r_server->sv_hostname); 1092 return (EIO); 1093 } 1094 1095 count -= res.count; 1096 *residp = count; 1097 base += res.count; 1098 offset += res.count; 1099 if (mi->mi_io_kstats) { 1100 mutex_enter(&mi->mi_lock); 1101 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 1102 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count; 1103 mutex_exit(&mi->mi_lock); 1104 } 1105 lwp_stat_update(LWP_STAT_INBLK, 1); 1106 } while (count && !res.eof); 1107 1108 if (res.pov.attributes) { 1109 mutex_enter(&rp->r_statelock); 1110 if (!CACHE_VALID(rp, va.va_mtime, va.va_size)) { 1111 mutex_exit(&rp->r_statelock); 1112 PURGE_ATTRCACHE(vp); 1113 } else { 1114 if (rp->r_mtime <= t) 1115 nfs_attrcache_va(vp, &va); 1116 mutex_exit(&rp->r_statelock); 1117 } 1118 } 1119 1120 return (0); 1121 } 1122 1123 /* ARGSUSED */ 1124 static int 1125 nfs3_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp) 1126 { 1127 1128 if (curproc->p_zone != VTOMI(vp)->mi_zone) 1129 return (EIO); 1130 switch (cmd) { 1131 case _FIODIRECTIO: 1132 return (nfs_directio(vp, (int)arg, cr)); 1133 default: 1134 return (ENOTTY); 1135 } 1136 } 1137 1138 static int 1139 nfs3_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr) 1140 { 1141 int error; 1142 rnode_t *rp; 1143 1144 if (curproc->p_zone != VTOMI(vp)->mi_zone) 1145 return (EIO); 1146 /* 1147 * If it has been specified that the return value will 1148 * just be used as a hint, and we are only being asked 1149 * for size, fsid or rdevid, then return the client's 1150 * notion of these values without checking to make sure 1151 * that the attribute cache is up to date. 1152 * The whole point is to avoid an over the wire GETATTR 1153 * call. 1154 */ 1155 rp = VTOR(vp); 1156 if (flags & ATTR_HINT) { 1157 if (vap->va_mask == 1158 (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) { 1159 mutex_enter(&rp->r_statelock); 1160 if (vap->va_mask | AT_SIZE) 1161 vap->va_size = rp->r_size; 1162 if (vap->va_mask | AT_FSID) 1163 vap->va_fsid = rp->r_attr.va_fsid; 1164 if (vap->va_mask | AT_RDEV) 1165 vap->va_rdev = rp->r_attr.va_rdev; 1166 mutex_exit(&rp->r_statelock); 1167 return (0); 1168 } 1169 } 1170 1171 /* 1172 * Only need to flush pages if asking for the mtime 1173 * and if there any dirty pages or any outstanding 1174 * asynchronous (write) requests for this file. 1175 */ 1176 if (vap->va_mask & AT_MTIME) { 1177 if (vn_has_cached_data(vp) && 1178 ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) { 1179 mutex_enter(&rp->r_statelock); 1180 rp->r_gcount++; 1181 mutex_exit(&rp->r_statelock); 1182 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr); 1183 mutex_enter(&rp->r_statelock); 1184 if (error && (error == ENOSPC || error == EDQUOT)) { 1185 if (!rp->r_error) 1186 rp->r_error = error; 1187 } 1188 if (--rp->r_gcount == 0) 1189 cv_broadcast(&rp->r_cv); 1190 mutex_exit(&rp->r_statelock); 1191 } 1192 } 1193 1194 return (nfs3getattr(vp, vap, cr)); 1195 } 1196 1197 /*ARGSUSED4*/ 1198 static int 1199 nfs3_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr, 1200 caller_context_t *ct) 1201 { 1202 int error; 1203 struct vattr va; 1204 1205 if (vap->va_mask & AT_NOSET) 1206 return (EINVAL); 1207 if (curproc->p_zone != VTOMI(vp)->mi_zone) 1208 return (EIO); 1209 1210 va.va_mask = AT_UID | AT_MODE; 1211 error = nfs3getattr(vp, &va, cr); 1212 if (error) 1213 return (error); 1214 1215 error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs3_accessx, 1216 vp); 1217 if (error) 1218 return (error); 1219 1220 return (nfs3setattr(vp, vap, flags, cr)); 1221 } 1222 1223 static int 1224 nfs3setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr) 1225 { 1226 int error; 1227 uint_t mask; 1228 SETATTR3args args; 1229 SETATTR3res res; 1230 int douprintf; 1231 rnode_t *rp; 1232 struct vattr va; 1233 mode_t omode; 1234 vsecattr_t *vsp; 1235 hrtime_t t; 1236 1237 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 1238 mask = vap->va_mask; 1239 1240 rp = VTOR(vp); 1241 1242 /* 1243 * Only need to flush pages if there are any pages and 1244 * if the file is marked as dirty in some fashion. The 1245 * file must be flushed so that we can accurately 1246 * determine the size of the file and the cached data 1247 * after the SETATTR returns. A file is considered to 1248 * be dirty if it is either marked with RDIRTY, has 1249 * outstanding i/o's active, or is mmap'd. In this 1250 * last case, we can't tell whether there are dirty 1251 * pages, so we flush just to be sure. 1252 */ 1253 if (vn_has_cached_data(vp) && 1254 ((rp->r_flags & RDIRTY) || 1255 rp->r_count > 0 || 1256 rp->r_mapcnt > 0)) { 1257 ASSERT(vp->v_type != VCHR); 1258 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr); 1259 if (error && (error == ENOSPC || error == EDQUOT)) { 1260 mutex_enter(&rp->r_statelock); 1261 if (!rp->r_error) 1262 rp->r_error = error; 1263 mutex_exit(&rp->r_statelock); 1264 } 1265 } 1266 1267 args.object = *RTOFH3(rp); 1268 /* 1269 * If the intent is for the server to set the times, 1270 * there is no point in have the mask indicating set mtime or 1271 * atime, because the vap values may be junk, and so result 1272 * in an overflow error. Remove these flags from the vap mask 1273 * before calling in this case, and restore them afterwards. 1274 */ 1275 if ((mask & (AT_ATIME | AT_MTIME)) && !(flags & ATTR_UTIME)) { 1276 /* Use server times, so don't set the args time fields */ 1277 vap->va_mask &= ~(AT_ATIME | AT_MTIME); 1278 error = vattr_to_sattr3(vap, &args.new_attributes); 1279 vap->va_mask |= (mask & (AT_ATIME | AT_MTIME)); 1280 if (mask & AT_ATIME) { 1281 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME; 1282 } 1283 if (mask & AT_MTIME) { 1284 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME; 1285 } 1286 } else { 1287 /* Either do not set times or use the client specified times */ 1288 error = vattr_to_sattr3(vap, &args.new_attributes); 1289 } 1290 1291 if (error) { 1292 /* req time field(s) overflow - return immediately */ 1293 return (error); 1294 } 1295 1296 va.va_mask = AT_MODE | AT_CTIME; 1297 error = nfs3getattr(vp, &va, cr); 1298 if (error) 1299 return (error); 1300 omode = va.va_mode; 1301 1302 tryagain: 1303 if (mask & AT_SIZE) { 1304 args.guard.check = TRUE; 1305 args.guard.obj_ctime.seconds = va.va_ctime.tv_sec; 1306 args.guard.obj_ctime.nseconds = va.va_ctime.tv_nsec; 1307 } else 1308 args.guard.check = FALSE; 1309 1310 douprintf = 1; 1311 1312 t = gethrtime(); 1313 1314 error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR, 1315 xdr_SETATTR3args, (caddr_t)&args, 1316 xdr_SETATTR3res, (caddr_t)&res, cr, 1317 &douprintf, &res.status, 0, NULL); 1318 1319 /* 1320 * Purge the access cache and ACL cache if changing either the 1321 * owner of the file, the group owner, or the mode. These may 1322 * change the access permissions of the file, so purge old 1323 * information and start over again. 1324 */ 1325 if (mask & (AT_UID | AT_GID | AT_MODE)) { 1326 (void) nfs_access_purge_rp(rp); 1327 if (rp->r_secattr != NULL) { 1328 mutex_enter(&rp->r_statelock); 1329 vsp = rp->r_secattr; 1330 rp->r_secattr = NULL; 1331 mutex_exit(&rp->r_statelock); 1332 if (vsp != NULL) 1333 nfs_acl_free(vsp); 1334 } 1335 } 1336 1337 if (error) { 1338 PURGE_ATTRCACHE(vp); 1339 return (error); 1340 } 1341 1342 error = geterrno3(res.status); 1343 if (!error) { 1344 /* 1345 * If changing the size of the file, invalidate 1346 * any local cached data which is no longer part 1347 * of the file. We also possibly invalidate the 1348 * last page in the file. We could use 1349 * pvn_vpzero(), but this would mark the page as 1350 * modified and require it to be written back to 1351 * the server for no particularly good reason. 1352 * This way, if we access it, then we bring it 1353 * back in. A read should be cheaper than a 1354 * write. 1355 */ 1356 if (mask & AT_SIZE) { 1357 nfs_invalidate_pages(vp, 1358 (vap->va_size & PAGEMASK), cr); 1359 } 1360 nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr); 1361 /* 1362 * Some servers will change the mode to clear the setuid 1363 * and setgid bits when changing the uid or gid. The 1364 * client needs to compensate appropriately. 1365 */ 1366 if (mask & (AT_UID | AT_GID)) { 1367 int terror; 1368 1369 va.va_mask = AT_MODE; 1370 terror = nfs3getattr(vp, &va, cr); 1371 if (!terror && 1372 (((mask & AT_MODE) && va.va_mode != vap->va_mode) || 1373 (!(mask & AT_MODE) && va.va_mode != omode))) { 1374 va.va_mask = AT_MODE; 1375 if (mask & AT_MODE) 1376 va.va_mode = vap->va_mode; 1377 else 1378 va.va_mode = omode; 1379 (void) nfs3setattr(vp, &va, 0, cr); 1380 } 1381 } 1382 } else { 1383 nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr); 1384 /* 1385 * If we got back a "not synchronized" error, then 1386 * we need to retry with a new guard value. The 1387 * guard value used is the change time. If the 1388 * server returned post_op_attr, then we can just 1389 * retry because we have the latest attributes. 1390 * Otherwise, we issue a GETATTR to get the latest 1391 * attributes and then retry. If we couldn't get 1392 * the attributes this way either, then we give 1393 * up because we can't complete the operation as 1394 * required. 1395 */ 1396 if (res.status == NFS3ERR_NOT_SYNC) { 1397 va.va_mask = AT_CTIME; 1398 if (nfs3getattr(vp, &va, cr) == 0) 1399 goto tryagain; 1400 } 1401 PURGE_STALE_FH(error, vp, cr); 1402 } 1403 1404 return (error); 1405 } 1406 1407 static int 1408 nfs3_accessx(void *vp, int mode, cred_t *cr) 1409 { 1410 ASSERT(curproc->p_zone == VTOMI((vnode_t *)vp)->mi_zone); 1411 return (nfs3_access(vp, mode, 0, cr)); 1412 } 1413 1414 /* ARGSUSED */ 1415 static int 1416 nfs3_access(vnode_t *vp, int mode, int flags, cred_t *cr) 1417 { 1418 int error; 1419 ACCESS3args args; 1420 ACCESS3res res; 1421 int douprintf; 1422 uint32 acc; 1423 rnode_t *rp; 1424 cred_t *cred; 1425 failinfo_t fi; 1426 nfs_access_type_t cacc; 1427 hrtime_t t; 1428 1429 acc = 0; 1430 if (curproc->p_zone != VTOMI(vp)->mi_zone) 1431 return (EIO); 1432 if (mode & VREAD) 1433 acc |= ACCESS3_READ; 1434 if (mode & VWRITE) { 1435 if (vn_is_readonly(vp) && !IS_DEVVP(vp)) 1436 return (EROFS); 1437 if (vp->v_type == VDIR) 1438 acc |= ACCESS3_DELETE; 1439 acc |= ACCESS3_MODIFY | ACCESS3_EXTEND; 1440 } 1441 if (mode & VEXEC) { 1442 if (vp->v_type == VDIR) 1443 acc |= ACCESS3_LOOKUP; 1444 else 1445 acc |= ACCESS3_EXECUTE; 1446 } 1447 1448 rp = VTOR(vp); 1449 if (rp->r_acache != NULL) { 1450 cacc = nfs_access_check(rp, acc, cr); 1451 if (cacc == NFS_ACCESS_ALLOWED) 1452 return (0); 1453 if (cacc == NFS_ACCESS_DENIED) 1454 return (EACCES); 1455 } 1456 1457 args.object = *VTOFH3(vp); 1458 if (vp->v_type == VDIR) { 1459 args.access = ACCESS3_READ | ACCESS3_DELETE | ACCESS3_MODIFY | 1460 ACCESS3_EXTEND | ACCESS3_LOOKUP; 1461 } else { 1462 args.access = ACCESS3_READ | ACCESS3_MODIFY | ACCESS3_EXTEND | 1463 ACCESS3_EXECUTE; 1464 } 1465 fi.vp = vp; 1466 fi.fhp = (caddr_t)&args.object; 1467 fi.copyproc = nfs3copyfh; 1468 fi.lookupproc = nfs3lookup; 1469 fi.xattrdirproc = acl_getxattrdir3; 1470 1471 cred = cr; 1472 tryagain: 1473 douprintf = 1; 1474 1475 t = gethrtime(); 1476 1477 error = rfs3call(VTOMI(vp), NFSPROC3_ACCESS, 1478 xdr_ACCESS3args, (caddr_t)&args, 1479 xdr_ACCESS3res, (caddr_t)&res, cred, 1480 &douprintf, &res.status, 0, &fi); 1481 1482 if (error) { 1483 if (cred != cr) 1484 crfree(cred); 1485 return (error); 1486 } 1487 1488 error = geterrno3(res.status); 1489 if (!error) { 1490 nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr); 1491 if ((acc & res.resok.access) != acc) { 1492 cred_t *ncr = crnetadjust(cred); 1493 1494 if (ncr != NULL) { 1495 cred = ncr; 1496 goto tryagain; 1497 } 1498 error = EACCES; 1499 } 1500 nfs_access_cache(rp, args.access, res.resok.access, cr); 1501 } else { 1502 nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr); 1503 PURGE_STALE_FH(error, vp, cr); 1504 } 1505 1506 if (cred != cr) 1507 crfree(cred); 1508 1509 return (error); 1510 } 1511 1512 static int nfs3_do_symlink_cache = 1; 1513 1514 static int 1515 nfs3_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr) 1516 { 1517 int error; 1518 READLINK3args args; 1519 READLINK3res res; 1520 nfspath3 resdata_backup; 1521 rnode_t *rp; 1522 int douprintf; 1523 int len; 1524 failinfo_t fi; 1525 hrtime_t t; 1526 1527 /* 1528 * Can't readlink anything other than a symbolic link. 1529 */ 1530 if (vp->v_type != VLNK) 1531 return (EINVAL); 1532 if (curproc->p_zone != VTOMI(vp)->mi_zone) 1533 return (EIO); 1534 1535 rp = VTOR(vp); 1536 if (nfs3_do_symlink_cache && rp->r_symlink.contents != NULL) { 1537 error = nfs3_validate_caches(vp, cr); 1538 if (error) 1539 return (error); 1540 mutex_enter(&rp->r_statelock); 1541 if (rp->r_symlink.contents != NULL) { 1542 error = uiomove(rp->r_symlink.contents, 1543 rp->r_symlink.len, UIO_READ, uiop); 1544 mutex_exit(&rp->r_statelock); 1545 return (error); 1546 } 1547 mutex_exit(&rp->r_statelock); 1548 } 1549 1550 args.symlink = *VTOFH3(vp); 1551 fi.vp = vp; 1552 fi.fhp = (caddr_t)&args.symlink; 1553 fi.copyproc = nfs3copyfh; 1554 fi.lookupproc = nfs3lookup; 1555 fi.xattrdirproc = acl_getxattrdir3; 1556 1557 res.resok.data = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1558 1559 resdata_backup = res.resok.data; 1560 1561 douprintf = 1; 1562 1563 t = gethrtime(); 1564 1565 error = rfs3call(VTOMI(vp), NFSPROC3_READLINK, 1566 xdr_nfs_fh3, (caddr_t)&args, 1567 xdr_READLINK3res, (caddr_t)&res, cr, 1568 &douprintf, &res.status, 0, &fi); 1569 1570 if (res.resok.data == nfs3nametoolong) 1571 error = EINVAL; 1572 1573 if (error) { 1574 kmem_free(resdata_backup, MAXPATHLEN); 1575 return (error); 1576 } 1577 1578 error = geterrno3(res.status); 1579 if (!error) { 1580 nfs3_cache_post_op_attr(vp, &res.resok.symlink_attributes, t, 1581 cr); 1582 len = strlen(res.resok.data); 1583 error = uiomove(res.resok.data, len, UIO_READ, uiop); 1584 if (nfs3_do_symlink_cache && rp->r_symlink.contents == NULL) { 1585 mutex_enter(&rp->r_statelock); 1586 if (rp->r_symlink.contents == NULL) { 1587 rp->r_symlink.contents = res.resok.data; 1588 rp->r_symlink.len = len; 1589 rp->r_symlink.size = MAXPATHLEN; 1590 mutex_exit(&rp->r_statelock); 1591 } else { 1592 mutex_exit(&rp->r_statelock); 1593 1594 kmem_free((void *)res.resok.data, MAXPATHLEN); 1595 } 1596 } else { 1597 kmem_free((void *)res.resok.data, MAXPATHLEN); 1598 } 1599 } else { 1600 nfs3_cache_post_op_attr(vp, 1601 &res.resfail.symlink_attributes, t, cr); 1602 PURGE_STALE_FH(error, vp, cr); 1603 1604 kmem_free((void *)res.resok.data, MAXPATHLEN); 1605 1606 } 1607 1608 /* 1609 * The over the wire error for attempting to readlink something 1610 * other than a symbolic link is ENXIO. However, we need to 1611 * return EINVAL instead of ENXIO, so we map it here. 1612 */ 1613 return (error == ENXIO ? EINVAL : error); 1614 } 1615 1616 /* 1617 * Flush local dirty pages to stable storage on the server. 1618 * 1619 * If FNODSYNC is specified, then there is nothing to do because 1620 * metadata changes are not cached on the client before being 1621 * sent to the server. 1622 */ 1623 static int 1624 nfs3_fsync(vnode_t *vp, int syncflag, cred_t *cr) 1625 { 1626 int error; 1627 1628 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp)) 1629 return (0); 1630 if (curproc->p_zone != VTOMI(vp)->mi_zone) 1631 return (EIO); 1632 1633 error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr); 1634 if (!error) 1635 error = VTOR(vp)->r_error; 1636 return (error); 1637 } 1638 1639 /* 1640 * Weirdness: if the file was removed or the target of a rename 1641 * operation while it was open, it got renamed instead. Here we 1642 * remove the renamed file. 1643 */ 1644 static void 1645 nfs3_inactive(vnode_t *vp, cred_t *cr) 1646 { 1647 rnode_t *rp; 1648 1649 ASSERT(vp != DNLC_NO_VNODE); 1650 1651 /* 1652 * If this is coming from the wrong zone, we let someone in the right 1653 * zone take care of it asynchronously. We can get here due to 1654 * VN_RELE() being called from pageout() or fsflush(). This call may 1655 * potentially turn into an expensive no-op if, for instance, v_count 1656 * gets incremented in the meantime, but it's still correct. 1657 */ 1658 if (curproc->p_zone != VTOMI(vp)->mi_zone) { 1659 nfs_async_inactive(vp, cr, nfs3_inactive); 1660 return; 1661 } 1662 1663 rp = VTOR(vp); 1664 redo: 1665 if (rp->r_unldvp != NULL) { 1666 /* 1667 * Save the vnode pointer for the directory where the 1668 * unlinked-open file got renamed, then set it to NULL 1669 * to prevent another thread from getting here before 1670 * we're done with the remove. While we have the 1671 * statelock, make local copies of the pertinent rnode 1672 * fields. If we weren't to do this in an atomic way, the 1673 * the unl* fields could become inconsistent with respect 1674 * to each other due to a race condition between this 1675 * code and nfs_remove(). See bug report 1034328. 1676 */ 1677 mutex_enter(&rp->r_statelock); 1678 if (rp->r_unldvp != NULL) { 1679 vnode_t *unldvp; 1680 char *unlname; 1681 cred_t *unlcred; 1682 REMOVE3args args; 1683 REMOVE3res res; 1684 int douprintf; 1685 int error; 1686 hrtime_t t; 1687 1688 unldvp = rp->r_unldvp; 1689 rp->r_unldvp = NULL; 1690 unlname = rp->r_unlname; 1691 rp->r_unlname = NULL; 1692 unlcred = rp->r_unlcred; 1693 rp->r_unlcred = NULL; 1694 mutex_exit(&rp->r_statelock); 1695 1696 /* 1697 * If there are any dirty pages left, then flush 1698 * them. This is unfortunate because they just 1699 * may get thrown away during the remove operation, 1700 * but we have to do this for correctness. 1701 */ 1702 if (vn_has_cached_data(vp) && 1703 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) { 1704 ASSERT(vp->v_type != VCHR); 1705 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr); 1706 if (error) { 1707 mutex_enter(&rp->r_statelock); 1708 if (!rp->r_error) 1709 rp->r_error = error; 1710 mutex_exit(&rp->r_statelock); 1711 } 1712 } 1713 1714 /* 1715 * Do the remove operation on the renamed file 1716 */ 1717 setdiropargs3(&args.object, unlname, unldvp); 1718 1719 douprintf = 1; 1720 1721 t = gethrtime(); 1722 1723 error = rfs3call(VTOMI(unldvp), NFSPROC3_REMOVE, 1724 xdr_diropargs3, (caddr_t)&args, 1725 xdr_REMOVE3res, (caddr_t)&res, unlcred, 1726 &douprintf, &res.status, 0, NULL); 1727 1728 if (error) { 1729 PURGE_ATTRCACHE(unldvp); 1730 } else { 1731 error = geterrno3(res.status); 1732 if (!error) { 1733 nfs3_cache_wcc_data(unldvp, 1734 &res.resok.dir_wcc, t, cr); 1735 if (HAVE_RDDIR_CACHE(VTOR(unldvp))) 1736 nfs_purge_rddir_cache(unldvp); 1737 } else { 1738 nfs3_cache_wcc_data(unldvp, 1739 &res.resfail.dir_wcc, t, cr); 1740 PURGE_STALE_FH(error, unldvp, cr); 1741 } 1742 } 1743 1744 /* 1745 * Release stuff held for the remove 1746 */ 1747 VN_RELE(unldvp); 1748 kmem_free(unlname, MAXNAMELEN); 1749 crfree(unlcred); 1750 goto redo; 1751 } 1752 mutex_exit(&rp->r_statelock); 1753 } 1754 1755 rp_addfree(rp, cr); 1756 } 1757 1758 /* 1759 * Remote file system operations having to do with directory manipulation. 1760 */ 1761 1762 static int 1763 nfs3_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 1764 int flags, vnode_t *rdir, cred_t *cr) 1765 { 1766 int error; 1767 vnode_t *vp; 1768 vnode_t *avp = NULL; 1769 rnode_t *drp; 1770 1771 if (curproc->p_zone != VTOMI(dvp)->mi_zone) 1772 return (EPERM); 1773 1774 drp = VTOR(dvp); 1775 1776 /* 1777 * Are we looking up extended attributes? If so, "dvp" is 1778 * the file or directory for which we want attributes, and 1779 * we need a lookup of the hidden attribute directory 1780 * before we lookup the rest of the path. 1781 */ 1782 if (flags & LOOKUP_XATTR) { 1783 bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0); 1784 mntinfo_t *mi; 1785 1786 mi = VTOMI(dvp); 1787 if (!(mi->mi_flags & MI_EXTATTR)) 1788 return (EINVAL); 1789 1790 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) 1791 return (EINTR); 1792 1793 (void) nfs3lookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr); 1794 if (avp == NULL) 1795 error = acl_getxattrdir3(dvp, &avp, cflag, cr, 0); 1796 else 1797 error = 0; 1798 1799 nfs_rw_exit(&drp->r_rwlock); 1800 1801 if (error) { 1802 if (mi->mi_flags & MI_EXTATTR) 1803 return (error); 1804 return (EINVAL); 1805 } 1806 dvp = avp; 1807 drp = VTOR(dvp); 1808 } 1809 1810 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) { 1811 error = EINTR; 1812 goto out; 1813 } 1814 1815 error = nfs3lookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0); 1816 1817 nfs_rw_exit(&drp->r_rwlock); 1818 1819 /* 1820 * If vnode is a device, create special vnode. 1821 */ 1822 if (!error && IS_DEVVP(*vpp)) { 1823 vp = *vpp; 1824 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 1825 VN_RELE(vp); 1826 } 1827 1828 out: 1829 if (avp != NULL) 1830 VN_RELE(avp); 1831 1832 return (error); 1833 } 1834 1835 static int nfs3_lookup_neg_cache = 1; 1836 1837 #ifdef DEBUG 1838 static int nfs3_lookup_dnlc_hits = 0; 1839 static int nfs3_lookup_dnlc_misses = 0; 1840 static int nfs3_lookup_dnlc_neg_hits = 0; 1841 static int nfs3_lookup_dnlc_disappears = 0; 1842 static int nfs3_lookup_dnlc_lookups = 0; 1843 #endif 1844 1845 /* ARGSUSED */ 1846 int 1847 nfs3lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 1848 int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags) 1849 { 1850 int error; 1851 rnode_t *drp; 1852 1853 ASSERT(curproc->p_zone == VTOMI(dvp)->mi_zone); 1854 /* 1855 * If lookup is for "", just return dvp. Don't need 1856 * to send it over the wire, look it up in the dnlc, 1857 * or perform any access checks. 1858 */ 1859 if (*nm == '\0') { 1860 VN_HOLD(dvp); 1861 *vpp = dvp; 1862 return (0); 1863 } 1864 1865 /* 1866 * Can't do lookups in non-directories. 1867 */ 1868 if (dvp->v_type != VDIR) 1869 return (ENOTDIR); 1870 1871 /* 1872 * If we're called with RFSCALL_SOFT, it's important that 1873 * the only rfscall is one we make directly; if we permit 1874 * an access call because we're looking up "." or validating 1875 * a dnlc hit, we'll deadlock because that rfscall will not 1876 * have the RFSCALL_SOFT set. 1877 */ 1878 if (rfscall_flags & RFSCALL_SOFT) 1879 goto callit; 1880 1881 /* 1882 * If lookup is for ".", just return dvp. Don't need 1883 * to send it over the wire or look it up in the dnlc, 1884 * just need to check access. 1885 */ 1886 if (strcmp(nm, ".") == 0) { 1887 error = nfs3_access(dvp, VEXEC, 0, cr); 1888 if (error) 1889 return (error); 1890 VN_HOLD(dvp); 1891 *vpp = dvp; 1892 return (0); 1893 } 1894 1895 drp = VTOR(dvp); 1896 if (!(drp->r_flags & RLOOKUP)) { 1897 mutex_enter(&drp->r_statelock); 1898 drp->r_flags |= RLOOKUP; 1899 mutex_exit(&drp->r_statelock); 1900 } 1901 1902 /* 1903 * Lookup this name in the DNLC. If there was a valid entry, 1904 * then return the results of the lookup. 1905 */ 1906 error = nfs3lookup_dnlc(dvp, nm, vpp, cr); 1907 if (error || *vpp != NULL) 1908 return (error); 1909 1910 callit: 1911 error = nfs3lookup_otw(dvp, nm, vpp, cr, rfscall_flags); 1912 1913 return (error); 1914 } 1915 1916 static int 1917 nfs3lookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr) 1918 { 1919 int error; 1920 vnode_t *vp; 1921 1922 ASSERT(*nm != '\0'); 1923 ASSERT(curproc->p_zone == VTOMI(dvp)->mi_zone); 1924 /* 1925 * Lookup this name in the DNLC. If successful, then validate 1926 * the caches and then recheck the DNLC. The DNLC is rechecked 1927 * just in case this entry got invalidated during the call 1928 * to nfs3_validate_caches. 1929 * 1930 * An assumption is being made that it is safe to say that a 1931 * file exists which may not on the server. Any operations to 1932 * the server will fail with ESTALE. 1933 */ 1934 #ifdef DEBUG 1935 nfs3_lookup_dnlc_lookups++; 1936 #endif 1937 vp = dnlc_lookup(dvp, nm); 1938 if (vp != NULL) { 1939 VN_RELE(vp); 1940 if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) { 1941 PURGE_ATTRCACHE(dvp); 1942 } 1943 error = nfs3_validate_caches(dvp, cr); 1944 if (error) 1945 return (error); 1946 vp = dnlc_lookup(dvp, nm); 1947 if (vp != NULL) { 1948 error = nfs3_access(dvp, VEXEC, 0, cr); 1949 if (error) { 1950 VN_RELE(vp); 1951 return (error); 1952 } 1953 if (vp == DNLC_NO_VNODE) { 1954 VN_RELE(vp); 1955 #ifdef DEBUG 1956 nfs3_lookup_dnlc_neg_hits++; 1957 #endif 1958 return (ENOENT); 1959 } 1960 *vpp = vp; 1961 #ifdef DEBUG 1962 nfs3_lookup_dnlc_hits++; 1963 #endif 1964 return (0); 1965 } 1966 #ifdef DEBUG 1967 nfs3_lookup_dnlc_disappears++; 1968 #endif 1969 } 1970 #ifdef DEBUG 1971 else 1972 nfs3_lookup_dnlc_misses++; 1973 #endif 1974 1975 *vpp = NULL; 1976 1977 return (0); 1978 } 1979 1980 static int 1981 nfs3lookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, 1982 int rfscall_flags) 1983 { 1984 int error; 1985 LOOKUP3args args; 1986 LOOKUP3vres res; 1987 int douprintf; 1988 struct vattr vattr; 1989 struct vattr dvattr; 1990 vnode_t *vp; 1991 failinfo_t fi; 1992 hrtime_t t; 1993 1994 ASSERT(*nm != '\0'); 1995 ASSERT(dvp->v_type == VDIR); 1996 ASSERT(curproc->p_zone == VTOMI(dvp)->mi_zone); 1997 1998 setdiropargs3(&args.what, nm, dvp); 1999 2000 fi.vp = dvp; 2001 fi.fhp = (caddr_t)&args.what.dir; 2002 fi.copyproc = nfs3copyfh; 2003 fi.lookupproc = nfs3lookup; 2004 fi.xattrdirproc = acl_getxattrdir3; 2005 res.obj_attributes.fres.vp = dvp; 2006 res.obj_attributes.fres.vap = &vattr; 2007 res.dir_attributes.fres.vp = dvp; 2008 res.dir_attributes.fres.vap = &dvattr; 2009 2010 douprintf = 1; 2011 2012 t = gethrtime(); 2013 2014 error = rfs3call(VTOMI(dvp), NFSPROC3_LOOKUP, 2015 xdr_diropargs3, (caddr_t)&args, 2016 xdr_LOOKUP3vres, (caddr_t)&res, cr, 2017 &douprintf, &res.status, rfscall_flags, &fi); 2018 2019 if (error) 2020 return (error); 2021 2022 nfs3_cache_post_op_vattr(dvp, &res.dir_attributes, t, cr); 2023 2024 error = geterrno3(res.status); 2025 if (error) { 2026 PURGE_STALE_FH(error, dvp, cr); 2027 if (error == ENOENT && nfs3_lookup_neg_cache) 2028 dnlc_enter(dvp, nm, DNLC_NO_VNODE); 2029 return (error); 2030 } 2031 2032 if (res.obj_attributes.attributes) { 2033 vp = makenfs3node_va(&res.object, res.obj_attributes.fres.vap, 2034 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm); 2035 } else { 2036 vp = makenfs3node_va(&res.object, NULL, 2037 dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm); 2038 if (vp->v_type == VNON) { 2039 vattr.va_mask = AT_TYPE; 2040 error = nfs3getattr(vp, &vattr, cr); 2041 if (error) { 2042 VN_RELE(vp); 2043 return (error); 2044 } 2045 vp->v_type = vattr.va_type; 2046 } 2047 } 2048 2049 if (!(rfscall_flags & RFSCALL_SOFT)) 2050 dnlc_update(dvp, nm, vp); 2051 2052 *vpp = vp; 2053 2054 return (error); 2055 } 2056 2057 #ifdef DEBUG 2058 static int nfs3_create_misses = 0; 2059 #endif 2060 2061 /* ARGSUSED */ 2062 static int 2063 nfs3_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 2064 int mode, vnode_t **vpp, cred_t *cr, int lfaware) 2065 { 2066 int error; 2067 vnode_t *vp; 2068 rnode_t *rp; 2069 struct vattr vattr; 2070 rnode_t *drp; 2071 vnode_t *tempvp; 2072 2073 drp = VTOR(dvp); 2074 if (curproc->p_zone != VTOMI(dvp)->mi_zone) 2075 return (EPERM); 2076 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp))) 2077 return (EINTR); 2078 2079 top: 2080 /* 2081 * We make a copy of the attributes because the caller does not 2082 * expect us to change what va points to. 2083 */ 2084 vattr = *va; 2085 2086 /* 2087 * If the pathname is "", just use dvp. Don't need 2088 * to send it over the wire, look it up in the dnlc, 2089 * or perform any access checks. 2090 */ 2091 if (*nm == '\0') { 2092 error = 0; 2093 VN_HOLD(dvp); 2094 vp = dvp; 2095 /* 2096 * If the pathname is ".", just use dvp. Don't need 2097 * to send it over the wire or look it up in the dnlc, 2098 * just need to check access. 2099 */ 2100 } else if (strcmp(nm, ".") == 0) { 2101 error = nfs3_access(dvp, VEXEC, 0, cr); 2102 if (error) { 2103 nfs_rw_exit(&drp->r_rwlock); 2104 return (error); 2105 } 2106 VN_HOLD(dvp); 2107 vp = dvp; 2108 /* 2109 * We need to go over the wire, just to be sure whether the 2110 * file exists or not. Using the DNLC can be dangerous in 2111 * this case when making a decision regarding existence. 2112 */ 2113 } else { 2114 error = nfs3lookup_otw(dvp, nm, &vp, cr, 0); 2115 } 2116 if (!error) { 2117 if (exclusive == EXCL) 2118 error = EEXIST; 2119 else if (vp->v_type == VDIR && (mode & VWRITE)) 2120 error = EISDIR; 2121 else { 2122 /* 2123 * If vnode is a device, create special vnode. 2124 */ 2125 if (IS_DEVVP(vp)) { 2126 tempvp = vp; 2127 vp = specvp(vp, vp->v_rdev, vp->v_type, cr); 2128 VN_RELE(tempvp); 2129 } 2130 if (!(error = VOP_ACCESS(vp, mode, 0, cr))) { 2131 if ((vattr.va_mask & AT_SIZE) && 2132 vp->v_type == VREG) { 2133 rp = VTOR(vp); 2134 /* 2135 * Check here for large file handled 2136 * by LF-unaware process (as 2137 * ufs_create() does) 2138 */ 2139 if (!(lfaware & FOFFMAX)) { 2140 mutex_enter(&rp->r_statelock); 2141 if (rp->r_size > MAXOFF32_T) 2142 error = EOVERFLOW; 2143 mutex_exit(&rp->r_statelock); 2144 } 2145 if (!error) { 2146 vattr.va_mask = AT_SIZE; 2147 error = nfs3setattr(vp, 2148 &vattr, 0, cr); 2149 } 2150 } 2151 } 2152 } 2153 nfs_rw_exit(&drp->r_rwlock); 2154 if (error) { 2155 VN_RELE(vp); 2156 } else 2157 *vpp = vp; 2158 return (error); 2159 } 2160 2161 dnlc_remove(dvp, nm); 2162 2163 /* 2164 * Decide what the group-id of the created file should be. 2165 * Set it in attribute list as advisory... 2166 */ 2167 error = setdirgid(dvp, &vattr.va_gid, cr); 2168 if (error) { 2169 nfs_rw_exit(&drp->r_rwlock); 2170 return (error); 2171 } 2172 vattr.va_mask |= AT_GID; 2173 2174 ASSERT(vattr.va_mask & AT_TYPE); 2175 if (vattr.va_type == VREG) { 2176 ASSERT(vattr.va_mask & AT_MODE); 2177 if (MANDMODE(vattr.va_mode)) { 2178 nfs_rw_exit(&drp->r_rwlock); 2179 return (EACCES); 2180 } 2181 error = nfs3create(dvp, nm, &vattr, exclusive, mode, vpp, cr, 2182 lfaware); 2183 /* 2184 * If this is not an exclusive create, then the CREATE 2185 * request will be made with the GUARDED mode set. This 2186 * means that the server will return EEXIST if the file 2187 * exists. The file could exist because of a retransmitted 2188 * request. In this case, we recover by starting over and 2189 * checking to see whether the file exists. This second 2190 * time through it should and a CREATE request will not be 2191 * sent. 2192 * 2193 * This handles the problem of a dangling CREATE request 2194 * which contains attributes which indicate that the file 2195 * should be truncated. This retransmitted request could 2196 * possibly truncate valid data in the file if not caught 2197 * by the duplicate request mechanism on the server or if 2198 * not caught by other means. The scenario is: 2199 * 2200 * Client transmits CREATE request with size = 0 2201 * Client times out, retransmits request. 2202 * Response to the first request arrives from the server 2203 * and the client proceeds on. 2204 * Client writes data to the file. 2205 * The server now processes retransmitted CREATE request 2206 * and truncates file. 2207 * 2208 * The use of the GUARDED CREATE request prevents this from 2209 * happening because the retransmitted CREATE would fail 2210 * with EEXIST and would not truncate the file. 2211 */ 2212 if (error == EEXIST && exclusive == NONEXCL) { 2213 #ifdef DEBUG 2214 nfs3_create_misses++; 2215 #endif 2216 goto top; 2217 } 2218 nfs_rw_exit(&drp->r_rwlock); 2219 return (error); 2220 } 2221 error = nfs3mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr); 2222 nfs_rw_exit(&drp->r_rwlock); 2223 return (error); 2224 } 2225 2226 /* ARGSUSED */ 2227 static int 2228 nfs3create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 2229 int mode, vnode_t **vpp, cred_t *cr, int lfaware) 2230 { 2231 int error; 2232 CREATE3args args; 2233 CREATE3res res; 2234 int douprintf; 2235 vnode_t *vp; 2236 struct vattr vattr; 2237 nfstime3 *verfp; 2238 rnode_t *rp; 2239 timestruc_t now; 2240 hrtime_t t; 2241 2242 ASSERT(curproc->p_zone == VTOMI(dvp)->mi_zone); 2243 setdiropargs3(&args.where, nm, dvp); 2244 if (exclusive == EXCL) { 2245 args.how.mode = EXCLUSIVE; 2246 /* 2247 * Construct the create verifier. This verifier needs 2248 * to be unique between different clients. It also needs 2249 * to vary for each exclusive create request generated 2250 * from the client to the server. 2251 * 2252 * The first attempt is made to use the hostid and a 2253 * unique number on the client. If the hostid has not 2254 * been set, the high resolution time that the exclusive 2255 * create request is being made is used. This will work 2256 * unless two different clients, both with the hostid 2257 * not set, attempt an exclusive create request on the 2258 * same file, at exactly the same clock time. The 2259 * chances of this happening seem small enough to be 2260 * reasonable. 2261 */ 2262 verfp = (nfstime3 *)&args.how.createhow3_u.verf; 2263 verfp->seconds = nfs_atoi(hw_serial); 2264 if (verfp->seconds != 0) 2265 verfp->nseconds = newnum(); 2266 else { 2267 gethrestime(&now); 2268 verfp->seconds = now.tv_sec; 2269 verfp->nseconds = now.tv_nsec; 2270 } 2271 /* 2272 * Since the server will use this value for the mtime, 2273 * make sure that it can't overflow. Zero out the MSB. 2274 * The actual value does not matter here, only its uniqeness. 2275 */ 2276 verfp->seconds %= INT32_MAX; 2277 } else { 2278 /* 2279 * Issue the non-exclusive create in guarded mode. This 2280 * may result in some false EEXIST responses for 2281 * retransmitted requests, but these will be handled at 2282 * a higher level. By using GUARDED, duplicate requests 2283 * to do file truncation and possible access problems 2284 * can be avoided. 2285 */ 2286 args.how.mode = GUARDED; 2287 error = vattr_to_sattr3(va, 2288 &args.how.createhow3_u.obj_attributes); 2289 if (error) { 2290 /* req time field(s) overflow - return immediately */ 2291 return (error); 2292 } 2293 } 2294 2295 douprintf = 1; 2296 2297 t = gethrtime(); 2298 2299 error = rfs3call(VTOMI(dvp), NFSPROC3_CREATE, 2300 xdr_CREATE3args, (caddr_t)&args, 2301 xdr_CREATE3res, (caddr_t)&res, cr, 2302 &douprintf, &res.status, 0, NULL); 2303 2304 if (error) { 2305 PURGE_ATTRCACHE(dvp); 2306 return (error); 2307 } 2308 2309 error = geterrno3(res.status); 2310 if (!error) { 2311 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr); 2312 if (HAVE_RDDIR_CACHE(VTOR(dvp))) 2313 nfs_purge_rddir_cache(dvp); 2314 2315 /* 2316 * On exclusive create the times need to be explicitly 2317 * set to clear any potential verifier that may be stored 2318 * in one of these fields (see comment below). This 2319 * is done here to cover the case where no post op attrs 2320 * were returned or a 'invalid' time was returned in 2321 * the attributes. 2322 */ 2323 if (exclusive == EXCL) 2324 va->va_mask |= (AT_MTIME | AT_ATIME); 2325 2326 if (!res.resok.obj.handle_follows) { 2327 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0); 2328 if (error) 2329 return (error); 2330 } else { 2331 if (res.resok.obj_attributes.attributes) { 2332 vp = makenfs3node(&res.resok.obj.handle, 2333 &res.resok.obj_attributes.attr, 2334 dvp->v_vfsp, t, cr, NULL, NULL); 2335 } else { 2336 vp = makenfs3node(&res.resok.obj.handle, NULL, 2337 dvp->v_vfsp, t, cr, NULL, NULL); 2338 2339 /* 2340 * On an exclusive create, it is possible 2341 * that attributes were returned but those 2342 * postop attributes failed to decode 2343 * properly. If this is the case, 2344 * then most likely the atime or mtime 2345 * were invalid for our client; this 2346 * is caused by the server storing the 2347 * create verifier in one of the time 2348 * fields(most likely mtime). 2349 * So... we are going to setattr just the 2350 * atime/mtime to clear things up. 2351 */ 2352 if (exclusive == EXCL) { 2353 if (error = 2354 nfs3excl_create_settimes(vp, 2355 va, cr)) { 2356 /* 2357 * Setting the times failed. 2358 * Remove the file and return 2359 * the error. 2360 */ 2361 VN_RELE(vp); 2362 (void) nfs3_remove(dvp, 2363 nm, cr); 2364 return (error); 2365 } 2366 } 2367 2368 /* 2369 * This handles the non-exclusive case 2370 * and the exclusive case where no post op 2371 * attrs were returned. 2372 */ 2373 if (vp->v_type == VNON) { 2374 vattr.va_mask = AT_TYPE; 2375 error = nfs3getattr(vp, &vattr, cr); 2376 if (error) { 2377 VN_RELE(vp); 2378 return (error); 2379 } 2380 vp->v_type = vattr.va_type; 2381 } 2382 } 2383 dnlc_update(dvp, nm, vp); 2384 } 2385 2386 rp = VTOR(vp); 2387 2388 /* 2389 * Check here for large file handled by 2390 * LF-unaware process (as ufs_create() does) 2391 */ 2392 if ((va->va_mask & AT_SIZE) && vp->v_type == VREG && 2393 !(lfaware & FOFFMAX)) { 2394 mutex_enter(&rp->r_statelock); 2395 if (rp->r_size > MAXOFF32_T) { 2396 mutex_exit(&rp->r_statelock); 2397 VN_RELE(vp); 2398 return (EOVERFLOW); 2399 } 2400 mutex_exit(&rp->r_statelock); 2401 } 2402 2403 if (exclusive == EXCL && 2404 (va->va_mask & ~(AT_GID | AT_SIZE))) { 2405 /* 2406 * If doing an exclusive create, then generate 2407 * a SETATTR to set the initial attributes. 2408 * Try to set the mtime and the atime to the 2409 * server's current time. It is somewhat 2410 * expected that these fields will be used to 2411 * store the exclusive create cookie. If not, 2412 * server implementors will need to know that 2413 * a SETATTR will follow an exclusive create 2414 * and the cookie should be destroyed if 2415 * appropriate. This work may have been done 2416 * earlier in this function if post op attrs 2417 * were not available. 2418 * 2419 * The AT_GID and AT_SIZE bits are turned off 2420 * so that the SETATTR request will not attempt 2421 * to process these. The gid will be set 2422 * separately if appropriate. The size is turned 2423 * off because it is assumed that a new file will 2424 * be created empty and if the file wasn't empty, 2425 * then the exclusive create will have failed 2426 * because the file must have existed already. 2427 * Therefore, no truncate operation is needed. 2428 */ 2429 va->va_mask &= ~(AT_GID | AT_SIZE); 2430 error = nfs3setattr(vp, va, 0, cr); 2431 if (error) { 2432 /* 2433 * Couldn't correct the attributes of 2434 * the newly created file and the 2435 * attributes are wrong. Remove the 2436 * file and return an error to the 2437 * application. 2438 */ 2439 VN_RELE(vp); 2440 (void) nfs3_remove(dvp, nm, cr); 2441 return (error); 2442 } 2443 } 2444 2445 if (va->va_gid != rp->r_attr.va_gid) { 2446 /* 2447 * If the gid on the file isn't right, then 2448 * generate a SETATTR to attempt to change 2449 * it. This may or may not work, depending 2450 * upon the server's semantics for allowing 2451 * file ownership changes. 2452 */ 2453 va->va_mask = AT_GID; 2454 (void) nfs3setattr(vp, va, 0, cr); 2455 } 2456 2457 /* 2458 * If vnode is a device create special vnode 2459 */ 2460 if (IS_DEVVP(vp)) { 2461 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 2462 VN_RELE(vp); 2463 } else 2464 *vpp = vp; 2465 } else { 2466 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr); 2467 PURGE_STALE_FH(error, dvp, cr); 2468 } 2469 2470 return (error); 2471 } 2472 2473 /* 2474 * Special setattr function to take care of rest of atime/mtime 2475 * after successful exclusive create. This function exists to avoid 2476 * handling attributes from the server; exclusive the atime/mtime fields 2477 * may be 'invalid' in client's view and therefore can not be trusted. 2478 */ 2479 static int 2480 nfs3excl_create_settimes(vnode_t *vp, struct vattr *vap, cred_t *cr) 2481 { 2482 int error; 2483 uint_t mask; 2484 SETATTR3args args; 2485 SETATTR3res res; 2486 int douprintf; 2487 rnode_t *rp; 2488 hrtime_t t; 2489 2490 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 2491 /* save the caller's mask so that it can be reset later */ 2492 mask = vap->va_mask; 2493 2494 rp = VTOR(vp); 2495 2496 args.object = *RTOFH3(rp); 2497 args.guard.check = FALSE; 2498 2499 /* Use the mask to initialize the arguments */ 2500 vap->va_mask = 0; 2501 error = vattr_to_sattr3(vap, &args.new_attributes); 2502 2503 /* We want to set just atime/mtime on this request */ 2504 args.new_attributes.atime.set_it = SET_TO_SERVER_TIME; 2505 args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME; 2506 2507 douprintf = 1; 2508 2509 t = gethrtime(); 2510 2511 error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR, 2512 xdr_SETATTR3args, (caddr_t)&args, 2513 xdr_SETATTR3res, (caddr_t)&res, cr, 2514 &douprintf, &res.status, 0, NULL); 2515 2516 if (error) { 2517 vap->va_mask = mask; 2518 return (error); 2519 } 2520 2521 error = geterrno3(res.status); 2522 if (!error) { 2523 /* 2524 * It is important to pick up the attributes. 2525 * Since this is the exclusive create path, the 2526 * attributes on the initial create were ignored 2527 * and we need these to have the correct info. 2528 */ 2529 nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr); 2530 /* 2531 * No need to do the atime/mtime work again so clear 2532 * the bits. 2533 */ 2534 mask &= ~(AT_ATIME | AT_MTIME); 2535 } else { 2536 nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr); 2537 } 2538 2539 vap->va_mask = mask; 2540 2541 return (error); 2542 } 2543 2544 /* ARGSUSED */ 2545 static int 2546 nfs3mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive, 2547 int mode, vnode_t **vpp, cred_t *cr) 2548 { 2549 int error; 2550 MKNOD3args args; 2551 MKNOD3res res; 2552 int douprintf; 2553 vnode_t *vp; 2554 struct vattr vattr; 2555 hrtime_t t; 2556 2557 ASSERT(curproc->p_zone == VTOMI(dvp)->mi_zone); 2558 switch (va->va_type) { 2559 case VCHR: 2560 case VBLK: 2561 setdiropargs3(&args.where, nm, dvp); 2562 args.what.type = (va->va_type == VCHR) ? NF3CHR : NF3BLK; 2563 error = vattr_to_sattr3(va, 2564 &args.what.mknoddata3_u.device.dev_attributes); 2565 if (error) { 2566 /* req time field(s) overflow - return immediately */ 2567 return (error); 2568 } 2569 args.what.mknoddata3_u.device.spec.specdata1 = 2570 getmajor(va->va_rdev); 2571 args.what.mknoddata3_u.device.spec.specdata2 = 2572 getminor(va->va_rdev); 2573 break; 2574 2575 case VFIFO: 2576 case VSOCK: 2577 setdiropargs3(&args.where, nm, dvp); 2578 args.what.type = (va->va_type == VFIFO) ? NF3FIFO : NF3SOCK; 2579 error = vattr_to_sattr3(va, 2580 &args.what.mknoddata3_u.pipe_attributes); 2581 if (error) { 2582 /* req time field(s) overflow - return immediately */ 2583 return (error); 2584 } 2585 break; 2586 2587 default: 2588 return (EINVAL); 2589 } 2590 2591 douprintf = 1; 2592 2593 t = gethrtime(); 2594 2595 error = rfs3call(VTOMI(dvp), NFSPROC3_MKNOD, 2596 xdr_MKNOD3args, (caddr_t)&args, 2597 xdr_MKNOD3res, (caddr_t)&res, cr, 2598 &douprintf, &res.status, 0, NULL); 2599 2600 if (error) { 2601 PURGE_ATTRCACHE(dvp); 2602 return (error); 2603 } 2604 2605 error = geterrno3(res.status); 2606 if (!error) { 2607 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr); 2608 if (HAVE_RDDIR_CACHE(VTOR(dvp))) 2609 nfs_purge_rddir_cache(dvp); 2610 2611 if (!res.resok.obj.handle_follows) { 2612 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0); 2613 if (error) 2614 return (error); 2615 } else { 2616 if (res.resok.obj_attributes.attributes) { 2617 vp = makenfs3node(&res.resok.obj.handle, 2618 &res.resok.obj_attributes.attr, 2619 dvp->v_vfsp, t, cr, NULL, NULL); 2620 } else { 2621 vp = makenfs3node(&res.resok.obj.handle, NULL, 2622 dvp->v_vfsp, t, cr, NULL, NULL); 2623 if (vp->v_type == VNON) { 2624 vattr.va_mask = AT_TYPE; 2625 error = nfs3getattr(vp, &vattr, cr); 2626 if (error) { 2627 VN_RELE(vp); 2628 return (error); 2629 } 2630 vp->v_type = vattr.va_type; 2631 } 2632 2633 } 2634 dnlc_update(dvp, nm, vp); 2635 } 2636 2637 if (va->va_gid != VTOR(vp)->r_attr.va_gid) { 2638 va->va_mask = AT_GID; 2639 (void) nfs3setattr(vp, va, 0, cr); 2640 } 2641 2642 /* 2643 * If vnode is a device create special vnode 2644 */ 2645 if (IS_DEVVP(vp)) { 2646 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr); 2647 VN_RELE(vp); 2648 } else 2649 *vpp = vp; 2650 } else { 2651 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr); 2652 PURGE_STALE_FH(error, dvp, cr); 2653 } 2654 return (error); 2655 } 2656 2657 /* 2658 * Weirdness: if the vnode to be removed is open 2659 * we rename it instead of removing it and nfs_inactive 2660 * will remove the new name. 2661 */ 2662 static int 2663 nfs3_remove(vnode_t *dvp, char *nm, cred_t *cr) 2664 { 2665 int error; 2666 REMOVE3args args; 2667 REMOVE3res res; 2668 vnode_t *vp; 2669 char *tmpname; 2670 int douprintf; 2671 rnode_t *rp; 2672 rnode_t *drp; 2673 hrtime_t t; 2674 2675 if (curproc->p_zone != VTOMI(dvp)->mi_zone) 2676 return (EPERM); 2677 drp = VTOR(dvp); 2678 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp))) 2679 return (EINTR); 2680 2681 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0); 2682 if (error) { 2683 nfs_rw_exit(&drp->r_rwlock); 2684 return (error); 2685 } 2686 2687 if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) { 2688 VN_RELE(vp); 2689 nfs_rw_exit(&drp->r_rwlock); 2690 return (EPERM); 2691 } 2692 2693 /* 2694 * First just remove the entry from the name cache, as it 2695 * is most likely the only entry for this vp. 2696 */ 2697 dnlc_remove(dvp, nm); 2698 2699 /* 2700 * If the file has a v_count > 1 then there may be more than one 2701 * entry in the name cache due multiple links or an open file, 2702 * but we don't have the real reference count so flush all 2703 * possible entries. 2704 */ 2705 if (vp->v_count > 1) 2706 dnlc_purge_vp(vp); 2707 2708 /* 2709 * Now we have the real reference count on the vnode 2710 */ 2711 rp = VTOR(vp); 2712 mutex_enter(&rp->r_statelock); 2713 if (vp->v_count > 1 && 2714 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) { 2715 mutex_exit(&rp->r_statelock); 2716 tmpname = newname(); 2717 error = nfs3rename(dvp, nm, dvp, tmpname, cr); 2718 if (error) 2719 kmem_free(tmpname, MAXNAMELEN); 2720 else { 2721 mutex_enter(&rp->r_statelock); 2722 if (rp->r_unldvp == NULL) { 2723 VN_HOLD(dvp); 2724 rp->r_unldvp = dvp; 2725 if (rp->r_unlcred != NULL) 2726 crfree(rp->r_unlcred); 2727 crhold(cr); 2728 rp->r_unlcred = cr; 2729 rp->r_unlname = tmpname; 2730 } else { 2731 kmem_free(rp->r_unlname, MAXNAMELEN); 2732 rp->r_unlname = tmpname; 2733 } 2734 mutex_exit(&rp->r_statelock); 2735 } 2736 } else { 2737 mutex_exit(&rp->r_statelock); 2738 /* 2739 * We need to flush any dirty pages which happen to 2740 * be hanging around before removing the file. This 2741 * shouldn't happen very often and mostly on file 2742 * systems mounted "nocto". 2743 */ 2744 if (vn_has_cached_data(vp) && 2745 ((rp->r_flags & RDIRTY) || rp->r_count > 0)) { 2746 error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr); 2747 if (error && (error == ENOSPC || error == EDQUOT)) { 2748 mutex_enter(&rp->r_statelock); 2749 if (!rp->r_error) 2750 rp->r_error = error; 2751 mutex_exit(&rp->r_statelock); 2752 } 2753 } 2754 2755 setdiropargs3(&args.object, nm, dvp); 2756 2757 douprintf = 1; 2758 2759 t = gethrtime(); 2760 2761 error = rfs3call(VTOMI(dvp), NFSPROC3_REMOVE, 2762 xdr_diropargs3, (caddr_t)&args, 2763 xdr_REMOVE3res, (caddr_t)&res, cr, 2764 &douprintf, &res.status, 0, NULL); 2765 2766 /* 2767 * The xattr dir may be gone after last attr is removed, 2768 * so flush it from dnlc. 2769 */ 2770 if (dvp->v_flag & V_XATTRDIR) 2771 dnlc_purge_vp(dvp); 2772 2773 PURGE_ATTRCACHE(vp); 2774 2775 if (error) { 2776 PURGE_ATTRCACHE(dvp); 2777 } else { 2778 error = geterrno3(res.status); 2779 if (!error) { 2780 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, 2781 cr); 2782 if (HAVE_RDDIR_CACHE(drp)) 2783 nfs_purge_rddir_cache(dvp); 2784 } else { 2785 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, 2786 t, cr); 2787 PURGE_STALE_FH(error, dvp, cr); 2788 } 2789 } 2790 } 2791 2792 VN_RELE(vp); 2793 2794 nfs_rw_exit(&drp->r_rwlock); 2795 2796 return (error); 2797 } 2798 2799 static int 2800 nfs3_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr) 2801 { 2802 int error; 2803 LINK3args args; 2804 LINK3res res; 2805 vnode_t *realvp; 2806 int douprintf; 2807 mntinfo_t *mi; 2808 rnode_t *tdrp; 2809 hrtime_t t; 2810 2811 if (curproc->p_zone != VTOMI(tdvp)->mi_zone) 2812 return (EPERM); 2813 if (VOP_REALVP(svp, &realvp) == 0) 2814 svp = realvp; 2815 2816 mi = VTOMI(svp); 2817 2818 if (!(mi->mi_flags & MI_LINK)) 2819 return (EOPNOTSUPP); 2820 2821 args.file = *VTOFH3(svp); 2822 setdiropargs3(&args.link, tnm, tdvp); 2823 2824 tdrp = VTOR(tdvp); 2825 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp))) 2826 return (EINTR); 2827 2828 dnlc_remove(tdvp, tnm); 2829 2830 douprintf = 1; 2831 2832 t = gethrtime(); 2833 2834 error = rfs3call(mi, NFSPROC3_LINK, 2835 xdr_LINK3args, (caddr_t)&args, 2836 xdr_LINK3res, (caddr_t)&res, cr, 2837 &douprintf, &res.status, 0, NULL); 2838 2839 if (error) { 2840 PURGE_ATTRCACHE(tdvp); 2841 PURGE_ATTRCACHE(svp); 2842 nfs_rw_exit(&tdrp->r_rwlock); 2843 return (error); 2844 } 2845 2846 error = geterrno3(res.status); 2847 2848 if (!error) { 2849 nfs3_cache_post_op_attr(svp, &res.resok.file_attributes, t, cr); 2850 nfs3_cache_wcc_data(tdvp, &res.resok.linkdir_wcc, t, cr); 2851 if (HAVE_RDDIR_CACHE(tdrp)) 2852 nfs_purge_rddir_cache(tdvp); 2853 dnlc_update(tdvp, tnm, svp); 2854 } else { 2855 nfs3_cache_post_op_attr(svp, &res.resfail.file_attributes, t, 2856 cr); 2857 nfs3_cache_wcc_data(tdvp, &res.resfail.linkdir_wcc, t, cr); 2858 if (error == EOPNOTSUPP) { 2859 mutex_enter(&mi->mi_lock); 2860 mi->mi_flags &= ~MI_LINK; 2861 mutex_exit(&mi->mi_lock); 2862 } 2863 } 2864 2865 nfs_rw_exit(&tdrp->r_rwlock); 2866 2867 return (error); 2868 } 2869 2870 static int 2871 nfs3_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 2872 { 2873 vnode_t *realvp; 2874 2875 if (curproc->p_zone != VTOMI(odvp)->mi_zone) 2876 return (EPERM); 2877 if (VOP_REALVP(ndvp, &realvp) == 0) 2878 ndvp = realvp; 2879 2880 return (nfs3rename(odvp, onm, ndvp, nnm, cr)); 2881 } 2882 2883 /* 2884 * nfs3rename does the real work of renaming in NFS Version 3. 2885 */ 2886 static int 2887 nfs3rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr) 2888 { 2889 int error; 2890 RENAME3args args; 2891 RENAME3res res; 2892 int douprintf; 2893 vnode_t *nvp; 2894 vnode_t *ovp = NULL; 2895 char *tmpname; 2896 rnode_t *rp; 2897 rnode_t *odrp; 2898 rnode_t *ndrp; 2899 hrtime_t t; 2900 2901 ASSERT(curproc->p_zone == VTOMI(odvp)->mi_zone); 2902 2903 if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 || 2904 strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0) 2905 return (EINVAL); 2906 2907 odrp = VTOR(odvp); 2908 ndrp = VTOR(ndvp); 2909 if ((intptr_t)odrp < (intptr_t)ndrp) { 2910 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) 2911 return (EINTR); 2912 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) { 2913 nfs_rw_exit(&odrp->r_rwlock); 2914 return (EINTR); 2915 } 2916 } else { 2917 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) 2918 return (EINTR); 2919 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) { 2920 nfs_rw_exit(&ndrp->r_rwlock); 2921 return (EINTR); 2922 } 2923 } 2924 2925 /* 2926 * Lookup the target file. If it exists, it needs to be 2927 * checked to see whether it is a mount point and whether 2928 * it is active (open). 2929 */ 2930 error = nfs3lookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0); 2931 if (!error) { 2932 /* 2933 * If this file has been mounted on, then just 2934 * return busy because renaming to it would remove 2935 * the mounted file system from the name space. 2936 */ 2937 if (vn_mountedvfs(nvp) != NULL) { 2938 VN_RELE(nvp); 2939 nfs_rw_exit(&odrp->r_rwlock); 2940 nfs_rw_exit(&ndrp->r_rwlock); 2941 return (EBUSY); 2942 } 2943 2944 /* 2945 * Purge the name cache of all references to this vnode 2946 * so that we can check the reference count to infer 2947 * whether it is active or not. 2948 */ 2949 /* 2950 * First just remove the entry from the name cache, as it 2951 * is most likely the only entry for this vp. 2952 */ 2953 dnlc_remove(ndvp, nnm); 2954 /* 2955 * If the file has a v_count > 1 then there may be more 2956 * than one entry in the name cache due multiple links 2957 * or an open file, but we don't have the real reference 2958 * count so flush all possible entries. 2959 */ 2960 if (nvp->v_count > 1) 2961 dnlc_purge_vp(nvp); 2962 2963 /* 2964 * If the vnode is active and is not a directory, 2965 * arrange to rename it to a 2966 * temporary file so that it will continue to be 2967 * accessible. This implements the "unlink-open-file" 2968 * semantics for the target of a rename operation. 2969 * Before doing this though, make sure that the 2970 * source and target files are not already the same. 2971 */ 2972 if (nvp->v_count > 1 && nvp->v_type != VDIR) { 2973 /* 2974 * Lookup the source name. 2975 */ 2976 error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL, 2977 cr, 0); 2978 2979 /* 2980 * The source name *should* already exist. 2981 */ 2982 if (error) { 2983 VN_RELE(nvp); 2984 nfs_rw_exit(&odrp->r_rwlock); 2985 nfs_rw_exit(&ndrp->r_rwlock); 2986 return (error); 2987 } 2988 2989 /* 2990 * Compare the two vnodes. If they are the same, 2991 * just release all held vnodes and return success. 2992 */ 2993 if (ovp == nvp) { 2994 VN_RELE(ovp); 2995 VN_RELE(nvp); 2996 nfs_rw_exit(&odrp->r_rwlock); 2997 nfs_rw_exit(&ndrp->r_rwlock); 2998 return (0); 2999 } 3000 3001 /* 3002 * Can't mix and match directories and non- 3003 * directories in rename operations. We already 3004 * know that the target is not a directory. If 3005 * the source is a directory, return an error. 3006 */ 3007 if (ovp->v_type == VDIR) { 3008 VN_RELE(ovp); 3009 VN_RELE(nvp); 3010 nfs_rw_exit(&odrp->r_rwlock); 3011 nfs_rw_exit(&ndrp->r_rwlock); 3012 return (ENOTDIR); 3013 } 3014 3015 /* 3016 * The target file exists, is not the same as 3017 * the source file, and is active. Link it 3018 * to a temporary filename to avoid having 3019 * the server removing the file completely. 3020 */ 3021 tmpname = newname(); 3022 error = nfs3_link(ndvp, nvp, tmpname, cr); 3023 if (error == EOPNOTSUPP) { 3024 error = nfs3_rename(ndvp, nnm, ndvp, tmpname, 3025 cr); 3026 } 3027 if (error) { 3028 kmem_free(tmpname, MAXNAMELEN); 3029 VN_RELE(ovp); 3030 VN_RELE(nvp); 3031 nfs_rw_exit(&odrp->r_rwlock); 3032 nfs_rw_exit(&ndrp->r_rwlock); 3033 return (error); 3034 } 3035 rp = VTOR(nvp); 3036 mutex_enter(&rp->r_statelock); 3037 if (rp->r_unldvp == NULL) { 3038 VN_HOLD(ndvp); 3039 rp->r_unldvp = ndvp; 3040 if (rp->r_unlcred != NULL) 3041 crfree(rp->r_unlcred); 3042 crhold(cr); 3043 rp->r_unlcred = cr; 3044 rp->r_unlname = tmpname; 3045 } else { 3046 kmem_free(rp->r_unlname, MAXNAMELEN); 3047 rp->r_unlname = tmpname; 3048 } 3049 mutex_exit(&rp->r_statelock); 3050 } 3051 3052 VN_RELE(nvp); 3053 } 3054 3055 if (ovp == NULL) { 3056 /* 3057 * When renaming directories to be a subdirectory of a 3058 * different parent, the dnlc entry for ".." will no 3059 * longer be valid, so it must be removed. 3060 * 3061 * We do a lookup here to determine whether we are renaming 3062 * a directory and we need to check if we are renaming 3063 * an unlinked file. This might have already been done 3064 * in previous code, so we check ovp == NULL to avoid 3065 * doing it twice. 3066 */ 3067 3068 error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0); 3069 /* 3070 * The source name *should* already exist. 3071 */ 3072 if (error) { 3073 nfs_rw_exit(&odrp->r_rwlock); 3074 nfs_rw_exit(&ndrp->r_rwlock); 3075 return (error); 3076 } 3077 ASSERT(ovp != NULL); 3078 } 3079 3080 dnlc_remove(odvp, onm); 3081 dnlc_remove(ndvp, nnm); 3082 3083 setdiropargs3(&args.from, onm, odvp); 3084 setdiropargs3(&args.to, nnm, ndvp); 3085 3086 douprintf = 1; 3087 3088 t = gethrtime(); 3089 3090 error = rfs3call(VTOMI(odvp), NFSPROC3_RENAME, 3091 xdr_RENAME3args, (caddr_t)&args, 3092 xdr_RENAME3res, (caddr_t)&res, cr, 3093 &douprintf, &res.status, 0, NULL); 3094 3095 if (error) { 3096 PURGE_ATTRCACHE(odvp); 3097 PURGE_ATTRCACHE(ndvp); 3098 VN_RELE(ovp); 3099 nfs_rw_exit(&odrp->r_rwlock); 3100 nfs_rw_exit(&ndrp->r_rwlock); 3101 return (error); 3102 } 3103 3104 error = geterrno3(res.status); 3105 3106 if (!error) { 3107 nfs3_cache_wcc_data(odvp, &res.resok.fromdir_wcc, t, cr); 3108 if (HAVE_RDDIR_CACHE(odrp)) 3109 nfs_purge_rddir_cache(odvp); 3110 if (ndvp != odvp) { 3111 nfs3_cache_wcc_data(ndvp, &res.resok.todir_wcc, t, cr); 3112 if (HAVE_RDDIR_CACHE(ndrp)) 3113 nfs_purge_rddir_cache(ndvp); 3114 } 3115 /* 3116 * when renaming directories to be a subdirectory of a 3117 * different parent, the dnlc entry for ".." will no 3118 * longer be valid, so it must be removed 3119 */ 3120 rp = VTOR(ovp); 3121 if (ndvp != odvp) { 3122 if (ovp->v_type == VDIR) { 3123 dnlc_remove(ovp, ".."); 3124 if (HAVE_RDDIR_CACHE(rp)) 3125 nfs_purge_rddir_cache(ovp); 3126 } 3127 } 3128 3129 /* 3130 * If we are renaming the unlinked file, update the 3131 * r_unldvp and r_unlname as needed. 3132 */ 3133 mutex_enter(&rp->r_statelock); 3134 if (rp->r_unldvp != NULL) { 3135 if (strcmp(rp->r_unlname, onm) == 0) { 3136 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN); 3137 rp->r_unlname[MAXNAMELEN - 1] = '\0'; 3138 3139 if (ndvp != rp->r_unldvp) { 3140 VN_RELE(rp->r_unldvp); 3141 rp->r_unldvp = ndvp; 3142 VN_HOLD(ndvp); 3143 } 3144 } 3145 } 3146 mutex_exit(&rp->r_statelock); 3147 } else { 3148 nfs3_cache_wcc_data(odvp, &res.resfail.fromdir_wcc, t, cr); 3149 if (ndvp != odvp) { 3150 nfs3_cache_wcc_data(ndvp, &res.resfail.todir_wcc, t, 3151 cr); 3152 } 3153 /* 3154 * System V defines rename to return EEXIST, not 3155 * ENOTEMPTY if the target directory is not empty. 3156 * Over the wire, the error is NFSERR_ENOTEMPTY 3157 * which geterrno maps to ENOTEMPTY. 3158 */ 3159 if (error == ENOTEMPTY) 3160 error = EEXIST; 3161 } 3162 3163 VN_RELE(ovp); 3164 3165 nfs_rw_exit(&odrp->r_rwlock); 3166 nfs_rw_exit(&ndrp->r_rwlock); 3167 3168 return (error); 3169 } 3170 3171 static int 3172 nfs3_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr) 3173 { 3174 int error; 3175 MKDIR3args args; 3176 MKDIR3res res; 3177 int douprintf; 3178 struct vattr vattr; 3179 vnode_t *vp; 3180 rnode_t *drp; 3181 hrtime_t t; 3182 3183 if (curproc->p_zone != VTOMI(dvp)->mi_zone) 3184 return (EPERM); 3185 setdiropargs3(&args.where, nm, dvp); 3186 3187 /* 3188 * Decide what the group-id and set-gid bit of the created directory 3189 * should be. May have to do a setattr to get the gid right. 3190 */ 3191 error = setdirgid(dvp, &va->va_gid, cr); 3192 if (error) 3193 return (error); 3194 error = setdirmode(dvp, &va->va_mode, cr); 3195 if (error) 3196 return (error); 3197 va->va_mask |= AT_MODE|AT_GID; 3198 3199 error = vattr_to_sattr3(va, &args.attributes); 3200 if (error) { 3201 /* req time field(s) overflow - return immediately */ 3202 return (error); 3203 } 3204 3205 drp = VTOR(dvp); 3206 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp))) 3207 return (EINTR); 3208 3209 dnlc_remove(dvp, nm); 3210 3211 douprintf = 1; 3212 3213 t = gethrtime(); 3214 3215 error = rfs3call(VTOMI(dvp), NFSPROC3_MKDIR, 3216 xdr_MKDIR3args, (caddr_t)&args, 3217 xdr_MKDIR3res, (caddr_t)&res, cr, 3218 &douprintf, &res.status, 0, NULL); 3219 3220 if (error) { 3221 PURGE_ATTRCACHE(dvp); 3222 nfs_rw_exit(&drp->r_rwlock); 3223 return (error); 3224 } 3225 3226 error = geterrno3(res.status); 3227 if (!error) { 3228 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr); 3229 if (HAVE_RDDIR_CACHE(drp)) 3230 nfs_purge_rddir_cache(dvp); 3231 3232 if (!res.resok.obj.handle_follows) { 3233 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0); 3234 if (error) { 3235 nfs_rw_exit(&drp->r_rwlock); 3236 return (error); 3237 } 3238 } else { 3239 if (res.resok.obj_attributes.attributes) { 3240 vp = makenfs3node(&res.resok.obj.handle, 3241 &res.resok.obj_attributes.attr, 3242 dvp->v_vfsp, t, cr, NULL, NULL); 3243 } else { 3244 vp = makenfs3node(&res.resok.obj.handle, NULL, 3245 dvp->v_vfsp, t, cr, NULL, NULL); 3246 if (vp->v_type == VNON) { 3247 vattr.va_mask = AT_TYPE; 3248 error = nfs3getattr(vp, &vattr, cr); 3249 if (error) { 3250 VN_RELE(vp); 3251 nfs_rw_exit(&drp->r_rwlock); 3252 return (error); 3253 } 3254 vp->v_type = vattr.va_type; 3255 } 3256 } 3257 dnlc_update(dvp, nm, vp); 3258 } 3259 if (va->va_gid != VTOR(vp)->r_attr.va_gid) { 3260 va->va_mask = AT_GID; 3261 (void) nfs3setattr(vp, va, 0, cr); 3262 } 3263 *vpp = vp; 3264 } else { 3265 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr); 3266 PURGE_STALE_FH(error, dvp, cr); 3267 } 3268 3269 nfs_rw_exit(&drp->r_rwlock); 3270 3271 return (error); 3272 } 3273 3274 static int 3275 nfs3_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr) 3276 { 3277 int error; 3278 RMDIR3args args; 3279 RMDIR3res res; 3280 vnode_t *vp; 3281 int douprintf; 3282 rnode_t *drp; 3283 hrtime_t t; 3284 3285 if (curproc->p_zone != VTOMI(dvp)->mi_zone) 3286 return (EPERM); 3287 drp = VTOR(dvp); 3288 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp))) 3289 return (EINTR); 3290 3291 /* 3292 * Attempt to prevent a rmdir(".") from succeeding. 3293 */ 3294 error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0); 3295 if (error) { 3296 nfs_rw_exit(&drp->r_rwlock); 3297 return (error); 3298 } 3299 3300 if (vp == cdir) { 3301 VN_RELE(vp); 3302 nfs_rw_exit(&drp->r_rwlock); 3303 return (EINVAL); 3304 } 3305 3306 setdiropargs3(&args.object, nm, dvp); 3307 3308 /* 3309 * First just remove the entry from the name cache, as it 3310 * is most likely an entry for this vp. 3311 */ 3312 dnlc_remove(dvp, nm); 3313 3314 /* 3315 * If there vnode reference count is greater than one, then 3316 * there may be additional references in the DNLC which will 3317 * need to be purged. First, trying removing the entry for 3318 * the parent directory and see if that removes the additional 3319 * reference(s). If that doesn't do it, then use dnlc_purge_vp 3320 * to completely remove any references to the directory which 3321 * might still exist in the DNLC. 3322 */ 3323 if (vp->v_count > 1) { 3324 dnlc_remove(vp, ".."); 3325 if (vp->v_count > 1) 3326 dnlc_purge_vp(vp); 3327 } 3328 3329 douprintf = 1; 3330 3331 t = gethrtime(); 3332 3333 error = rfs3call(VTOMI(dvp), NFSPROC3_RMDIR, 3334 xdr_diropargs3, (caddr_t)&args, 3335 xdr_RMDIR3res, (caddr_t)&res, cr, 3336 &douprintf, &res.status, 0, NULL); 3337 3338 PURGE_ATTRCACHE(vp); 3339 3340 if (error) { 3341 PURGE_ATTRCACHE(dvp); 3342 VN_RELE(vp); 3343 nfs_rw_exit(&drp->r_rwlock); 3344 return (error); 3345 } 3346 3347 error = geterrno3(res.status); 3348 if (!error) { 3349 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr); 3350 if (HAVE_RDDIR_CACHE(drp)) 3351 nfs_purge_rddir_cache(dvp); 3352 if (HAVE_RDDIR_CACHE(VTOR(vp))) 3353 nfs_purge_rddir_cache(vp); 3354 } else { 3355 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr); 3356 PURGE_STALE_FH(error, dvp, cr); 3357 /* 3358 * System V defines rmdir to return EEXIST, not 3359 * ENOTEMPTY if the directory is not empty. Over 3360 * the wire, the error is NFSERR_ENOTEMPTY which 3361 * geterrno maps to ENOTEMPTY. 3362 */ 3363 if (error == ENOTEMPTY) 3364 error = EEXIST; 3365 } 3366 3367 VN_RELE(vp); 3368 3369 nfs_rw_exit(&drp->r_rwlock); 3370 3371 return (error); 3372 } 3373 3374 static int 3375 nfs3_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr) 3376 { 3377 int error; 3378 SYMLINK3args args; 3379 SYMLINK3res res; 3380 int douprintf; 3381 mntinfo_t *mi; 3382 vnode_t *vp; 3383 rnode_t *rp; 3384 char *contents; 3385 rnode_t *drp; 3386 hrtime_t t; 3387 3388 mi = VTOMI(dvp); 3389 3390 if (curproc->p_zone != mi->mi_zone) 3391 return (EPERM); 3392 if (!(mi->mi_flags & MI_SYMLINK)) 3393 return (EOPNOTSUPP); 3394 3395 setdiropargs3(&args.where, lnm, dvp); 3396 error = vattr_to_sattr3(tva, &args.symlink.symlink_attributes); 3397 if (error) { 3398 /* req time field(s) overflow - return immediately */ 3399 return (error); 3400 } 3401 args.symlink.symlink_data = tnm; 3402 3403 drp = VTOR(dvp); 3404 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp))) 3405 return (EINTR); 3406 3407 dnlc_remove(dvp, lnm); 3408 3409 douprintf = 1; 3410 3411 t = gethrtime(); 3412 3413 error = rfs3call(mi, NFSPROC3_SYMLINK, 3414 xdr_SYMLINK3args, (caddr_t)&args, 3415 xdr_SYMLINK3res, (caddr_t)&res, cr, 3416 &douprintf, &res.status, 0, NULL); 3417 3418 if (error) { 3419 PURGE_ATTRCACHE(dvp); 3420 nfs_rw_exit(&drp->r_rwlock); 3421 return (error); 3422 } 3423 3424 error = geterrno3(res.status); 3425 if (!error) { 3426 nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr); 3427 if (HAVE_RDDIR_CACHE(drp)) 3428 nfs_purge_rddir_cache(dvp); 3429 3430 if (res.resok.obj.handle_follows) { 3431 if (res.resok.obj_attributes.attributes) { 3432 vp = makenfs3node(&res.resok.obj.handle, 3433 &res.resok.obj_attributes.attr, 3434 dvp->v_vfsp, t, cr, NULL, NULL); 3435 } else { 3436 vp = makenfs3node(&res.resok.obj.handle, NULL, 3437 dvp->v_vfsp, t, cr, NULL, NULL); 3438 vp->v_type = VLNK; 3439 vp->v_rdev = 0; 3440 } 3441 dnlc_update(dvp, lnm, vp); 3442 rp = VTOR(vp); 3443 if (nfs3_do_symlink_cache && 3444 rp->r_symlink.contents == NULL) { 3445 3446 contents = kmem_alloc(MAXPATHLEN, 3447 KM_NOSLEEP); 3448 3449 if (contents != NULL) { 3450 mutex_enter(&rp->r_statelock); 3451 if (rp->r_symlink.contents == NULL) { 3452 rp->r_symlink.len = strlen(tnm); 3453 bcopy(tnm, contents, 3454 rp->r_symlink.len); 3455 rp->r_symlink.contents = 3456 contents; 3457 rp->r_symlink.size = MAXPATHLEN; 3458 mutex_exit(&rp->r_statelock); 3459 } else { 3460 mutex_exit(&rp->r_statelock); 3461 kmem_free((void *)contents, 3462 MAXPATHLEN); 3463 } 3464 } 3465 } 3466 VN_RELE(vp); 3467 } 3468 } else { 3469 nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr); 3470 PURGE_STALE_FH(error, dvp, cr); 3471 if (error == EOPNOTSUPP) { 3472 mutex_enter(&mi->mi_lock); 3473 mi->mi_flags &= ~MI_SYMLINK; 3474 mutex_exit(&mi->mi_lock); 3475 } 3476 } 3477 3478 nfs_rw_exit(&drp->r_rwlock); 3479 3480 return (error); 3481 } 3482 3483 #ifdef DEBUG 3484 static int nfs3_readdir_cache_hits = 0; 3485 static int nfs3_readdir_cache_shorts = 0; 3486 static int nfs3_readdir_cache_waits = 0; 3487 static int nfs3_readdir_cache_misses = 0; 3488 static int nfs3_readdir_readahead = 0; 3489 #endif 3490 3491 static int nfs3_shrinkreaddir = 0; 3492 3493 /* 3494 * Read directory entries. 3495 * There are some weird things to look out for here. The uio_loffset 3496 * field is either 0 or it is the offset returned from a previous 3497 * readdir. It is an opaque value used by the server to find the 3498 * correct directory block to read. The count field is the number 3499 * of blocks to read on the server. This is advisory only, the server 3500 * may return only one block's worth of entries. Entries may be compressed 3501 * on the server. 3502 */ 3503 static int 3504 nfs3_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp) 3505 { 3506 int error; 3507 size_t count; 3508 rnode_t *rp; 3509 rddir_cache *rdc; 3510 rddir_cache *nrdc; 3511 rddir_cache *rrdc; 3512 #ifdef DEBUG 3513 int missed; 3514 #endif 3515 int doreadahead; 3516 rddir_cache srdc; 3517 avl_index_t where; 3518 3519 if (curproc->p_zone != VTOMI(vp)->mi_zone) 3520 return (EIO); 3521 rp = VTOR(vp); 3522 3523 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER)); 3524 3525 /* 3526 * Make sure that the directory cache is valid. 3527 */ 3528 if (HAVE_RDDIR_CACHE(rp)) { 3529 if (nfs_disable_rddir_cache) { 3530 /* 3531 * Setting nfs_disable_rddir_cache in /etc/system 3532 * allows interoperability with servers that do not 3533 * properly update the attributes of directories. 3534 * Any cached information gets purged before an 3535 * access is made to it. 3536 */ 3537 nfs_purge_rddir_cache(vp); 3538 } else { 3539 error = nfs3_validate_caches(vp, cr); 3540 if (error) 3541 return (error); 3542 } 3543 } 3544 3545 /* 3546 * It is possible that some servers may not be able to correctly 3547 * handle a large READDIR or READDIRPLUS request due to bugs in 3548 * their implementation. In order to continue to interoperate 3549 * with them, this workaround is provided to limit the maximum 3550 * size of a READDIRPLUS request to 1024. In any case, the request 3551 * size is limited to MAXBSIZE. 3552 */ 3553 count = MIN(uiop->uio_iov->iov_len, 3554 nfs3_shrinkreaddir ? 1024 : MAXBSIZE); 3555 3556 nrdc = NULL; 3557 #ifdef DEBUG 3558 missed = 0; 3559 #endif 3560 top: 3561 /* 3562 * Short circuit last readdir which always returns 0 bytes. 3563 * This can be done after the directory has been read through 3564 * completely at least once. This will set r_direof which 3565 * can be used to find the value of the last cookie. 3566 */ 3567 mutex_enter(&rp->r_statelock); 3568 if (rp->r_direof != NULL && 3569 uiop->uio_loffset == rp->r_direof->nfs3_ncookie) { 3570 mutex_exit(&rp->r_statelock); 3571 #ifdef DEBUG 3572 nfs3_readdir_cache_shorts++; 3573 #endif 3574 if (eofp) 3575 *eofp = 1; 3576 if (nrdc != NULL) 3577 rddir_cache_rele(nrdc); 3578 return (0); 3579 } 3580 /* 3581 * Look for a cache entry. Cache entries are identified 3582 * by the NFS cookie value and the byte count requested. 3583 */ 3584 srdc.nfs3_cookie = uiop->uio_loffset; 3585 srdc.buflen = count; 3586 rdc = avl_find(&rp->r_dir, &srdc, &where); 3587 if (rdc != NULL) { 3588 rddir_cache_hold(rdc); 3589 /* 3590 * If the cache entry is in the process of being 3591 * filled in, wait until this completes. The 3592 * RDDIRWAIT bit is set to indicate that someone 3593 * is waiting and then the thread currently 3594 * filling the entry is done, it should do a 3595 * cv_broadcast to wakeup all of the threads 3596 * waiting for it to finish. 3597 */ 3598 if (rdc->flags & RDDIR) { 3599 nfs_rw_exit(&rp->r_rwlock); 3600 rdc->flags |= RDDIRWAIT; 3601 #ifdef DEBUG 3602 nfs3_readdir_cache_waits++; 3603 #endif 3604 if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) { 3605 /* 3606 * We got interrupted, probably 3607 * the user typed ^C or an alarm 3608 * fired. We free the new entry 3609 * if we allocated one. 3610 */ 3611 mutex_exit(&rp->r_statelock); 3612 (void) nfs_rw_enter_sig(&rp->r_rwlock, 3613 RW_READER, FALSE); 3614 rddir_cache_rele(rdc); 3615 if (nrdc != NULL) 3616 rddir_cache_rele(nrdc); 3617 return (EINTR); 3618 } 3619 mutex_exit(&rp->r_statelock); 3620 (void) nfs_rw_enter_sig(&rp->r_rwlock, 3621 RW_READER, FALSE); 3622 rddir_cache_rele(rdc); 3623 goto top; 3624 } 3625 /* 3626 * Check to see if a readdir is required to 3627 * fill the entry. If so, mark this entry 3628 * as being filled, remove our reference, 3629 * and branch to the code to fill the entry. 3630 */ 3631 if (rdc->flags & RDDIRREQ) { 3632 rdc->flags &= ~RDDIRREQ; 3633 rdc->flags |= RDDIR; 3634 if (nrdc != NULL) 3635 rddir_cache_rele(nrdc); 3636 nrdc = rdc; 3637 mutex_exit(&rp->r_statelock); 3638 goto bottom; 3639 } 3640 #ifdef DEBUG 3641 if (!missed) 3642 nfs3_readdir_cache_hits++; 3643 #endif 3644 /* 3645 * If an error occurred while attempting 3646 * to fill the cache entry, just return it. 3647 */ 3648 if (rdc->error) { 3649 error = rdc->error; 3650 mutex_exit(&rp->r_statelock); 3651 rddir_cache_rele(rdc); 3652 if (nrdc != NULL) 3653 rddir_cache_rele(nrdc); 3654 return (error); 3655 } 3656 3657 /* 3658 * The cache entry is complete and good, 3659 * copyout the dirent structs to the calling 3660 * thread. 3661 */ 3662 error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop); 3663 3664 /* 3665 * If no error occurred during the copyout, 3666 * update the offset in the uio struct to 3667 * contain the value of the next cookie 3668 * and set the eof value appropriately. 3669 */ 3670 if (!error) { 3671 uiop->uio_loffset = rdc->nfs3_ncookie; 3672 if (eofp) 3673 *eofp = rdc->eof; 3674 } 3675 3676 /* 3677 * Decide whether to do readahead. 3678 * 3679 * Don't if have already read to the end of 3680 * directory. There is nothing more to read. 3681 * 3682 * Don't if the application is not doing 3683 * lookups in the directory. The readahead 3684 * is only effective if the application can 3685 * be doing work while an async thread is 3686 * handling the over the wire request. 3687 */ 3688 if (rdc->eof) { 3689 rp->r_direof = rdc; 3690 doreadahead = FALSE; 3691 } else if (!(rp->r_flags & RLOOKUP)) 3692 doreadahead = FALSE; 3693 else 3694 doreadahead = TRUE; 3695 3696 if (!doreadahead) { 3697 mutex_exit(&rp->r_statelock); 3698 rddir_cache_rele(rdc); 3699 if (nrdc != NULL) 3700 rddir_cache_rele(nrdc); 3701 return (error); 3702 } 3703 3704 /* 3705 * Check to see whether we found an entry 3706 * for the readahead. If so, we don't need 3707 * to do anything further, so free the new 3708 * entry if one was allocated. Otherwise, 3709 * allocate a new entry, add it to the cache, 3710 * and then initiate an asynchronous readdir 3711 * operation to fill it. 3712 */ 3713 srdc.nfs3_cookie = rdc->nfs3_ncookie; 3714 srdc.buflen = count; 3715 rrdc = avl_find(&rp->r_dir, &srdc, &where); 3716 if (rrdc != NULL) { 3717 if (nrdc != NULL) 3718 rddir_cache_rele(nrdc); 3719 } else { 3720 if (nrdc != NULL) 3721 rrdc = nrdc; 3722 else { 3723 rrdc = rddir_cache_alloc(KM_NOSLEEP); 3724 } 3725 if (rrdc != NULL) { 3726 rrdc->nfs3_cookie = rdc->nfs3_ncookie; 3727 rrdc->buflen = count; 3728 avl_insert(&rp->r_dir, rrdc, where); 3729 rddir_cache_hold(rrdc); 3730 mutex_exit(&rp->r_statelock); 3731 rddir_cache_rele(rdc); 3732 #ifdef DEBUG 3733 nfs3_readdir_readahead++; 3734 #endif 3735 nfs_async_readdir(vp, rrdc, cr, do_nfs3readdir); 3736 return (error); 3737 } 3738 } 3739 3740 mutex_exit(&rp->r_statelock); 3741 rddir_cache_rele(rdc); 3742 return (error); 3743 } 3744 3745 /* 3746 * Didn't find an entry in the cache. Construct a new empty 3747 * entry and link it into the cache. Other processes attempting 3748 * to access this entry will need to wait until it is filled in. 3749 * 3750 * Since kmem_alloc may block, another pass through the cache 3751 * will need to be taken to make sure that another process 3752 * hasn't already added an entry to the cache for this request. 3753 */ 3754 if (nrdc == NULL) { 3755 mutex_exit(&rp->r_statelock); 3756 nrdc = rddir_cache_alloc(KM_SLEEP); 3757 nrdc->nfs3_cookie = uiop->uio_loffset; 3758 nrdc->buflen = count; 3759 goto top; 3760 } 3761 3762 /* 3763 * Add this entry to the cache. 3764 */ 3765 avl_insert(&rp->r_dir, nrdc, where); 3766 rddir_cache_hold(nrdc); 3767 mutex_exit(&rp->r_statelock); 3768 3769 bottom: 3770 #ifdef DEBUG 3771 missed = 1; 3772 nfs3_readdir_cache_misses++; 3773 #endif 3774 /* 3775 * Do the readdir. This routine decides whether to use 3776 * READDIR or READDIRPLUS. 3777 */ 3778 error = do_nfs3readdir(vp, nrdc, cr); 3779 3780 /* 3781 * If this operation failed, just return the error which occurred. 3782 */ 3783 if (error != 0) 3784 return (error); 3785 3786 /* 3787 * Since the RPC operation will have taken sometime and blocked 3788 * this process, another pass through the cache will need to be 3789 * taken to find the correct cache entry. It is possible that 3790 * the correct cache entry will not be there (although one was 3791 * added) because the directory changed during the RPC operation 3792 * and the readdir cache was flushed. In this case, just start 3793 * over. It is hoped that this will not happen too often... :-) 3794 */ 3795 nrdc = NULL; 3796 goto top; 3797 /* NOTREACHED */ 3798 } 3799 3800 static int 3801 do_nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr) 3802 { 3803 int error; 3804 rnode_t *rp; 3805 mntinfo_t *mi; 3806 3807 rp = VTOR(vp); 3808 mi = VTOMI(vp); 3809 ASSERT(curproc->p_zone == mi->mi_zone); 3810 /* 3811 * Issue the proper request. 3812 * 3813 * If the server does not support READDIRPLUS, then use READDIR. 3814 * 3815 * Otherwise -- 3816 * Issue a READDIRPLUS if reading to fill an empty cache or if 3817 * an application has performed a lookup in the directory which 3818 * required an over the wire lookup. The use of READDIRPLUS 3819 * will help to (re)populate the DNLC. 3820 */ 3821 if (!(mi->mi_flags & MI_READDIRONLY) && 3822 (rp->r_flags & (RLOOKUP | RREADDIRPLUS))) { 3823 if (rp->r_flags & RREADDIRPLUS) { 3824 mutex_enter(&rp->r_statelock); 3825 rp->r_flags &= ~RREADDIRPLUS; 3826 mutex_exit(&rp->r_statelock); 3827 } 3828 nfs3readdirplus(vp, rdc, cr); 3829 if (rdc->error == EOPNOTSUPP) 3830 nfs3readdir(vp, rdc, cr); 3831 } else 3832 nfs3readdir(vp, rdc, cr); 3833 3834 mutex_enter(&rp->r_statelock); 3835 rdc->flags &= ~RDDIR; 3836 if (rdc->flags & RDDIRWAIT) { 3837 rdc->flags &= ~RDDIRWAIT; 3838 cv_broadcast(&rdc->cv); 3839 } 3840 error = rdc->error; 3841 if (error) 3842 rdc->flags |= RDDIRREQ; 3843 mutex_exit(&rp->r_statelock); 3844 3845 rddir_cache_rele(rdc); 3846 3847 return (error); 3848 } 3849 3850 static void 3851 nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr) 3852 { 3853 int error; 3854 READDIR3args args; 3855 READDIR3vres res; 3856 vattr_t dva; 3857 rnode_t *rp; 3858 int douprintf; 3859 failinfo_t fi, *fip = NULL; 3860 mntinfo_t *mi; 3861 hrtime_t t; 3862 3863 rp = VTOR(vp); 3864 mi = VTOMI(vp); 3865 ASSERT(curproc->p_zone == mi->mi_zone); 3866 3867 args.dir = *RTOFH3(rp); 3868 args.cookie = (cookie3)rdc->nfs3_cookie; 3869 args.cookieverf = rp->r_cookieverf; 3870 args.count = rdc->buflen; 3871 3872 /* 3873 * NFS client failover support 3874 * suppress failover unless we have a zero cookie 3875 */ 3876 if (args.cookie == (cookie3) 0) { 3877 fi.vp = vp; 3878 fi.fhp = (caddr_t)&args.dir; 3879 fi.copyproc = nfs3copyfh; 3880 fi.lookupproc = nfs3lookup; 3881 fi.xattrdirproc = acl_getxattrdir3; 3882 fip = &fi; 3883 } 3884 3885 #ifdef DEBUG 3886 rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP); 3887 #else 3888 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP); 3889 #endif 3890 3891 res.entries = (dirent64_t *)rdc->entries; 3892 res.entries_size = rdc->buflen; 3893 res.dir_attributes.fres.vap = &dva; 3894 res.dir_attributes.fres.vp = vp; 3895 res.loff = rdc->nfs3_cookie; 3896 3897 douprintf = 1; 3898 3899 if (mi->mi_io_kstats) { 3900 mutex_enter(&mi->mi_lock); 3901 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 3902 mutex_exit(&mi->mi_lock); 3903 } 3904 3905 t = gethrtime(); 3906 3907 error = rfs3call(VTOMI(vp), NFSPROC3_READDIR, 3908 xdr_READDIR3args, (caddr_t)&args, 3909 xdr_READDIR3vres, (caddr_t)&res, cr, 3910 &douprintf, &res.status, 0, fip); 3911 3912 if (mi->mi_io_kstats) { 3913 mutex_enter(&mi->mi_lock); 3914 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 3915 mutex_exit(&mi->mi_lock); 3916 } 3917 3918 if (error) 3919 goto err; 3920 3921 nfs3_cache_post_op_vattr(vp, &res.dir_attributes, t, cr); 3922 3923 error = geterrno3(res.status); 3924 if (error) { 3925 PURGE_STALE_FH(error, vp, cr); 3926 goto err; 3927 } 3928 3929 if (mi->mi_io_kstats) { 3930 mutex_enter(&mi->mi_lock); 3931 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 3932 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size; 3933 mutex_exit(&mi->mi_lock); 3934 } 3935 3936 rdc->nfs3_ncookie = res.loff; 3937 rp->r_cookieverf = res.cookieverf; 3938 rdc->eof = res.eof ? 1 : 0; 3939 rdc->entlen = res.size; 3940 ASSERT(rdc->entlen <= rdc->buflen); 3941 rdc->error = 0; 3942 return; 3943 3944 err: 3945 kmem_free(rdc->entries, rdc->buflen); 3946 rdc->entries = NULL; 3947 rdc->error = error; 3948 } 3949 3950 /* 3951 * Read directory entries. 3952 * There are some weird things to look out for here. The uio_loffset 3953 * field is either 0 or it is the offset returned from a previous 3954 * readdir. It is an opaque value used by the server to find the 3955 * correct directory block to read. The count field is the number 3956 * of blocks to read on the server. This is advisory only, the server 3957 * may return only one block's worth of entries. Entries may be compressed 3958 * on the server. 3959 */ 3960 static void 3961 nfs3readdirplus(vnode_t *vp, rddir_cache *rdc, cred_t *cr) 3962 { 3963 int error; 3964 READDIRPLUS3args args; 3965 READDIRPLUS3vres res; 3966 vattr_t dva; 3967 rnode_t *rp; 3968 mntinfo_t *mi; 3969 int douprintf; 3970 failinfo_t fi, *fip = NULL; 3971 3972 rp = VTOR(vp); 3973 mi = VTOMI(vp); 3974 ASSERT(curproc->p_zone == mi->mi_zone); 3975 3976 args.dir = *RTOFH3(rp); 3977 args.cookie = (cookie3)rdc->nfs3_cookie; 3978 args.cookieverf = rp->r_cookieverf; 3979 args.dircount = rdc->buflen; 3980 args.maxcount = mi->mi_tsize; 3981 3982 /* 3983 * NFS client failover support 3984 * suppress failover unless we have a zero cookie 3985 */ 3986 if (args.cookie == (cookie3)0) { 3987 fi.vp = vp; 3988 fi.fhp = (caddr_t)&args.dir; 3989 fi.copyproc = nfs3copyfh; 3990 fi.lookupproc = nfs3lookup; 3991 fi.xattrdirproc = acl_getxattrdir3; 3992 fip = &fi; 3993 } 3994 3995 #ifdef DEBUG 3996 rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP); 3997 #else 3998 rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP); 3999 #endif 4000 4001 res.entries = (dirent64_t *)rdc->entries; 4002 res.entries_size = rdc->buflen; 4003 res.dir_attributes.fres.vap = &dva; 4004 res.dir_attributes.fres.vp = vp; 4005 res.loff = rdc->nfs3_cookie; 4006 res.credentials = cr; 4007 4008 douprintf = 1; 4009 4010 if (mi->mi_io_kstats) { 4011 mutex_enter(&mi->mi_lock); 4012 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats)); 4013 mutex_exit(&mi->mi_lock); 4014 } 4015 4016 res.time = gethrtime(); 4017 4018 error = rfs3call(mi, NFSPROC3_READDIRPLUS, 4019 xdr_READDIRPLUS3args, (caddr_t)&args, 4020 xdr_READDIRPLUS3vres, (caddr_t)&res, cr, 4021 &douprintf, &res.status, 0, fip); 4022 4023 if (mi->mi_io_kstats) { 4024 mutex_enter(&mi->mi_lock); 4025 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats)); 4026 mutex_exit(&mi->mi_lock); 4027 } 4028 4029 if (error) { 4030 goto err; 4031 } 4032 4033 nfs3_cache_post_op_vattr(vp, &res.dir_attributes, res.time, cr); 4034 4035 error = geterrno3(res.status); 4036 if (error) { 4037 PURGE_STALE_FH(error, vp, cr); 4038 if (error == EOPNOTSUPP) { 4039 mutex_enter(&mi->mi_lock); 4040 mi->mi_flags |= MI_READDIRONLY; 4041 mutex_exit(&mi->mi_lock); 4042 } 4043 goto err; 4044 } 4045 4046 if (mi->mi_io_kstats) { 4047 mutex_enter(&mi->mi_lock); 4048 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++; 4049 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size; 4050 mutex_exit(&mi->mi_lock); 4051 } 4052 4053 rdc->nfs3_ncookie = res.loff; 4054 rp->r_cookieverf = res.cookieverf; 4055 rdc->eof = res.eof ? 1 : 0; 4056 rdc->entlen = res.size; 4057 ASSERT(rdc->entlen <= rdc->buflen); 4058 rdc->error = 0; 4059 4060 return; 4061 4062 err: 4063 kmem_free(rdc->entries, rdc->buflen); 4064 rdc->entries = NULL; 4065 rdc->error = error; 4066 } 4067 4068 #ifdef DEBUG 4069 static int nfs3_bio_do_stop = 0; 4070 #endif 4071 4072 static int 4073 nfs3_bio(struct buf *bp, stable_how *stab_comm, cred_t *cr) 4074 { 4075 rnode_t *rp = VTOR(bp->b_vp); 4076 int count; 4077 int error; 4078 cred_t *cred; 4079 offset_t offset; 4080 4081 ASSERT(curproc->p_zone == VTOMI(bp->b_vp)->mi_zone); 4082 offset = ldbtob(bp->b_lblkno); 4083 4084 DTRACE_IO1(start, struct buf *, bp); 4085 4086 if (bp->b_flags & B_READ) { 4087 mutex_enter(&rp->r_statelock); 4088 if (rp->r_cred != NULL) { 4089 cred = rp->r_cred; 4090 crhold(cred); 4091 } else { 4092 rp->r_cred = cr; 4093 crhold(cr); 4094 cred = cr; 4095 crhold(cred); 4096 } 4097 mutex_exit(&rp->r_statelock); 4098 read_again: 4099 error = bp->b_error = nfs3read(bp->b_vp, bp->b_un.b_addr, 4100 offset, bp->b_bcount, &bp->b_resid, cred); 4101 crfree(cred); 4102 if (!error) { 4103 if (bp->b_resid) { 4104 /* 4105 * Didn't get it all because we hit EOF, 4106 * zero all the memory beyond the EOF. 4107 */ 4108 /* bzero(rdaddr + */ 4109 bzero(bp->b_un.b_addr + 4110 bp->b_bcount - bp->b_resid, bp->b_resid); 4111 } 4112 mutex_enter(&rp->r_statelock); 4113 if (bp->b_resid == bp->b_bcount && 4114 offset >= rp->r_size) { 4115 /* 4116 * We didn't read anything at all as we are 4117 * past EOF. Return an error indicator back 4118 * but don't destroy the pages (yet). 4119 */ 4120 error = NFS_EOF; 4121 } 4122 mutex_exit(&rp->r_statelock); 4123 } else if (error == EACCES) { 4124 mutex_enter(&rp->r_statelock); 4125 if (cred != cr) { 4126 if (rp->r_cred != NULL) 4127 crfree(rp->r_cred); 4128 rp->r_cred = cr; 4129 crhold(cr); 4130 cred = cr; 4131 crhold(cred); 4132 mutex_exit(&rp->r_statelock); 4133 goto read_again; 4134 } 4135 mutex_exit(&rp->r_statelock); 4136 } 4137 } else { 4138 if (!(rp->r_flags & RSTALE)) { 4139 mutex_enter(&rp->r_statelock); 4140 if (rp->r_cred != NULL) { 4141 cred = rp->r_cred; 4142 crhold(cred); 4143 } else { 4144 rp->r_cred = cr; 4145 crhold(cr); 4146 cred = cr; 4147 crhold(cred); 4148 } 4149 mutex_exit(&rp->r_statelock); 4150 write_again: 4151 mutex_enter(&rp->r_statelock); 4152 count = MIN(bp->b_bcount, rp->r_size - offset); 4153 mutex_exit(&rp->r_statelock); 4154 if (count < 0) 4155 cmn_err(CE_PANIC, "nfs3_bio: write count < 0"); 4156 #ifdef DEBUG 4157 if (count == 0) { 4158 zcmn_err(getzoneid(), CE_WARN, 4159 "nfs3_bio: zero length write at %lld", 4160 offset); 4161 nfs_printfhandle(&rp->r_fh); 4162 if (nfs3_bio_do_stop) 4163 debug_enter("nfs3_bio"); 4164 } 4165 #endif 4166 error = nfs3write(bp->b_vp, bp->b_un.b_addr, offset, 4167 count, cred, stab_comm); 4168 if (error == EACCES) { 4169 mutex_enter(&rp->r_statelock); 4170 if (cred != cr) { 4171 if (rp->r_cred != NULL) 4172 crfree(rp->r_cred); 4173 rp->r_cred = cr; 4174 crhold(cr); 4175 crfree(cred); 4176 cred = cr; 4177 crhold(cred); 4178 mutex_exit(&rp->r_statelock); 4179 goto write_again; 4180 } 4181 mutex_exit(&rp->r_statelock); 4182 } 4183 bp->b_error = error; 4184 if (error && error != EINTR) { 4185 /* 4186 * Don't print EDQUOT errors on the console. 4187 * Don't print asynchronous EACCES errors. 4188 * Don't print EFBIG errors. 4189 * Print all other write errors. 4190 */ 4191 if (error != EDQUOT && error != EFBIG && 4192 (error != EACCES || 4193 !(bp->b_flags & B_ASYNC))) 4194 nfs_write_error(bp->b_vp, error, cred); 4195 /* 4196 * Update r_error and r_flags as appropriate. 4197 * If the error was ESTALE, then mark the 4198 * rnode as not being writeable and save 4199 * the error status. Otherwise, save any 4200 * errors which occur from asynchronous 4201 * page invalidations. Any errors occurring 4202 * from other operations should be saved 4203 * by the caller. 4204 */ 4205 mutex_enter(&rp->r_statelock); 4206 if (error == ESTALE) { 4207 rp->r_flags |= RSTALE; 4208 if (!rp->r_error) 4209 rp->r_error = error; 4210 } else if (!rp->r_error && 4211 (bp->b_flags & 4212 (B_INVAL|B_FORCE|B_ASYNC)) == 4213 (B_INVAL|B_FORCE|B_ASYNC)) { 4214 rp->r_error = error; 4215 } 4216 mutex_exit(&rp->r_statelock); 4217 } 4218 crfree(cred); 4219 } else 4220 error = rp->r_error; 4221 } 4222 4223 if (error != 0 && error != NFS_EOF) 4224 bp->b_flags |= B_ERROR; 4225 4226 DTRACE_IO1(done, struct buf *, bp); 4227 4228 return (error); 4229 } 4230 4231 static int 4232 nfs3_fid(vnode_t *vp, fid_t *fidp) 4233 { 4234 rnode_t *rp; 4235 4236 if (curproc->p_zone != VTOMI(vp)->mi_zone) 4237 return (EIO); 4238 rp = VTOR(vp); 4239 4240 if (fidp->fid_len < (ushort_t)rp->r_fh.fh_len) { 4241 fidp->fid_len = rp->r_fh.fh_len; 4242 return (ENOSPC); 4243 } 4244 fidp->fid_len = rp->r_fh.fh_len; 4245 bcopy(rp->r_fh.fh_buf, fidp->fid_data, fidp->fid_len); 4246 return (0); 4247 } 4248 4249 /* ARGSUSED2 */ 4250 static int 4251 nfs3_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 4252 { 4253 rnode_t *rp = VTOR(vp); 4254 4255 if (!write_lock) { 4256 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 4257 return (V_WRITELOCK_FALSE); 4258 } 4259 4260 if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) { 4261 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE); 4262 if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp)) 4263 return (V_WRITELOCK_FALSE); 4264 nfs_rw_exit(&rp->r_rwlock); 4265 } 4266 4267 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE); 4268 return (V_WRITELOCK_TRUE); 4269 } 4270 4271 /* ARGSUSED */ 4272 static void 4273 nfs3_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp) 4274 { 4275 rnode_t *rp = VTOR(vp); 4276 4277 nfs_rw_exit(&rp->r_rwlock); 4278 } 4279 4280 /* ARGSUSED */ 4281 static int 4282 nfs3_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 4283 { 4284 4285 /* 4286 * Because we stuff the readdir cookie into the offset field 4287 * someone may attempt to do an lseek with the cookie which 4288 * we want to succeed. 4289 */ 4290 if (vp->v_type == VDIR) 4291 return (0); 4292 if (*noffp < 0) 4293 return (EINVAL); 4294 return (0); 4295 } 4296 4297 /* 4298 * number of nfs3_bsize blocks to read ahead. 4299 */ 4300 static int nfs3_nra = 4; 4301 4302 #ifdef DEBUG 4303 static int nfs3_lostpage = 0; /* number of times we lost original page */ 4304 #endif 4305 4306 /* 4307 * Return all the pages from [off..off+len) in file 4308 */ 4309 static int 4310 nfs3_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4311 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 4312 enum seg_rw rw, cred_t *cr) 4313 { 4314 rnode_t *rp; 4315 int error; 4316 mntinfo_t *mi; 4317 4318 if (vp->v_flag & VNOMAP) 4319 return (ENOSYS); 4320 4321 if (curproc->p_zone != VTOMI(vp)->mi_zone) 4322 return (EIO); 4323 if (protp != NULL) 4324 *protp = PROT_ALL; 4325 4326 /* 4327 * Now valididate that the caches are up to date. 4328 */ 4329 error = nfs3_validate_caches(vp, cr); 4330 if (error) 4331 return (error); 4332 4333 rp = VTOR(vp); 4334 mi = VTOMI(vp); 4335 retry: 4336 mutex_enter(&rp->r_statelock); 4337 4338 /* 4339 * Don't create dirty pages faster than they 4340 * can be cleaned so that the system doesn't 4341 * get imbalanced. If the async queue is 4342 * maxed out, then wait for it to drain before 4343 * creating more dirty pages. Also, wait for 4344 * any threads doing pagewalks in the vop_getattr 4345 * entry points so that they don't block for 4346 * long periods. 4347 */ 4348 if (rw == S_CREATE) { 4349 while ((mi->mi_max_threads != 0 && 4350 rp->r_awcount > 2 * mi->mi_max_threads) || 4351 rp->r_gcount > 0) 4352 cv_wait(&rp->r_cv, &rp->r_statelock); 4353 } 4354 4355 /* 4356 * If we are getting called as a side effect of an nfs_write() 4357 * operation the local file size might not be extended yet. 4358 * In this case we want to be able to return pages of zeroes. 4359 */ 4360 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) { 4361 mutex_exit(&rp->r_statelock); 4362 return (EFAULT); /* beyond EOF */ 4363 } 4364 4365 mutex_exit(&rp->r_statelock); 4366 4367 if (len <= PAGESIZE) { 4368 error = nfs3_getapage(vp, off, len, protp, pl, plsz, 4369 seg, addr, rw, cr); 4370 } else { 4371 error = pvn_getpages(nfs3_getapage, vp, off, len, protp, 4372 pl, plsz, seg, addr, rw, cr); 4373 } 4374 4375 switch (error) { 4376 case NFS_EOF: 4377 nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr); 4378 goto retry; 4379 case ESTALE: 4380 PURGE_STALE_FH(error, vp, cr); 4381 } 4382 4383 return (error); 4384 } 4385 4386 /* 4387 * Called from pvn_getpages or nfs3_getpage to get a particular page. 4388 */ 4389 /* ARGSUSED */ 4390 static int 4391 nfs3_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp, 4392 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 4393 enum seg_rw rw, cred_t *cr) 4394 { 4395 rnode_t *rp; 4396 uint_t bsize; 4397 struct buf *bp; 4398 page_t *pp; 4399 u_offset_t lbn; 4400 u_offset_t io_off; 4401 u_offset_t blkoff; 4402 u_offset_t rablkoff; 4403 size_t io_len; 4404 uint_t blksize; 4405 int error; 4406 int readahead; 4407 int readahead_issued = 0; 4408 int ra_window; /* readahead window */ 4409 page_t *pagefound; 4410 page_t *savepp; 4411 4412 if (curproc->p_zone != VTOMI(vp)->mi_zone) 4413 return (EIO); 4414 rp = VTOR(vp); 4415 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 4416 4417 reread: 4418 bp = NULL; 4419 pp = NULL; 4420 pagefound = NULL; 4421 4422 if (pl != NULL) 4423 pl[0] = NULL; 4424 4425 error = 0; 4426 lbn = off / bsize; 4427 blkoff = lbn * bsize; 4428 4429 /* 4430 * Queueing up the readahead before doing the synchronous read 4431 * results in a significant increase in read throughput because 4432 * of the increased parallelism between the async threads and 4433 * the process context. 4434 */ 4435 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 && 4436 rw != S_CREATE && 4437 !(vp->v_flag & VNOCACHE)) { 4438 mutex_enter(&rp->r_statelock); 4439 4440 /* 4441 * Calculate the number of readaheads to do. 4442 * a) No readaheads at offset = 0. 4443 * b) Do maximum(nfs3_nra) readaheads when the readahead 4444 * window is closed. 4445 * c) Do readaheads between 1 to (nfs3_nra - 1) depending 4446 * upon how far the readahead window is open or close. 4447 * d) No readaheads if rp->r_nextr is not within the scope 4448 * of the readahead window (random i/o). 4449 */ 4450 4451 if (off == 0) 4452 readahead = 0; 4453 else if (blkoff == rp->r_nextr) 4454 readahead = nfs3_nra; 4455 else if (rp->r_nextr > blkoff && 4456 ((ra_window = (rp->r_nextr - blkoff) / bsize) 4457 <= (nfs3_nra - 1))) 4458 readahead = nfs3_nra - ra_window; 4459 else 4460 readahead = 0; 4461 4462 rablkoff = rp->r_nextr; 4463 while (readahead > 0 && rablkoff + bsize < rp->r_size) { 4464 mutex_exit(&rp->r_statelock); 4465 if (nfs_async_readahead(vp, rablkoff + bsize, 4466 addr + (rablkoff + bsize - off), seg, cr, 4467 nfs3_readahead) < 0) { 4468 mutex_enter(&rp->r_statelock); 4469 break; 4470 } 4471 readahead--; 4472 rablkoff += bsize; 4473 /* 4474 * Indicate that we did a readahead so 4475 * readahead offset is not updated 4476 * by the synchronous read below. 4477 */ 4478 readahead_issued = 1; 4479 mutex_enter(&rp->r_statelock); 4480 /* 4481 * set readahead offset to 4482 * offset of last async readahead 4483 * request. 4484 */ 4485 rp->r_nextr = rablkoff; 4486 } 4487 mutex_exit(&rp->r_statelock); 4488 } 4489 4490 again: 4491 if ((pagefound = page_exists(vp, off)) == NULL) { 4492 if (pl == NULL) { 4493 (void) nfs_async_readahead(vp, blkoff, addr, seg, cr, 4494 nfs3_readahead); 4495 } else if (rw == S_CREATE) { 4496 /* 4497 * Block for this page is not allocated, or the offset 4498 * is beyond the current allocation size, or we're 4499 * allocating a swap slot and the page was not found, 4500 * so allocate it and return a zero page. 4501 */ 4502 if ((pp = page_create_va(vp, off, 4503 PAGESIZE, PG_WAIT, seg, addr)) == NULL) 4504 cmn_err(CE_PANIC, "nfs3_getapage: page_create"); 4505 io_len = PAGESIZE; 4506 mutex_enter(&rp->r_statelock); 4507 rp->r_nextr = off + PAGESIZE; 4508 mutex_exit(&rp->r_statelock); 4509 } else { 4510 /* 4511 * Need to go to server to get a BLOCK, exception to 4512 * that being while reading at offset = 0 or doing 4513 * random i/o, in that case read only a PAGE. 4514 */ 4515 mutex_enter(&rp->r_statelock); 4516 if (blkoff < rp->r_size && 4517 blkoff + bsize >= rp->r_size) { 4518 /* 4519 * If only a block or less is left in 4520 * the file, read all that is remaining. 4521 */ 4522 if (rp->r_size <= off) { 4523 /* 4524 * Trying to access beyond EOF, 4525 * set up to get at least one page. 4526 */ 4527 blksize = off + PAGESIZE - blkoff; 4528 } else 4529 blksize = rp->r_size - blkoff; 4530 } else if ((off == 0) || 4531 (off != rp->r_nextr && !readahead_issued)) { 4532 blksize = PAGESIZE; 4533 blkoff = off; /* block = page here */ 4534 } else 4535 blksize = bsize; 4536 mutex_exit(&rp->r_statelock); 4537 4538 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 4539 &io_len, blkoff, blksize, 0); 4540 4541 /* 4542 * Some other thread has entered the page, 4543 * so just use it. 4544 */ 4545 if (pp == NULL) 4546 goto again; 4547 4548 /* 4549 * Now round the request size up to page boundaries. 4550 * This ensures that the entire page will be 4551 * initialized to zeroes if EOF is encountered. 4552 */ 4553 io_len = ptob(btopr(io_len)); 4554 4555 bp = pageio_setup(pp, io_len, vp, B_READ); 4556 ASSERT(bp != NULL); 4557 4558 /* 4559 * pageio_setup should have set b_addr to 0. This 4560 * is correct since we want to do I/O on a page 4561 * boundary. bp_mapin will use this addr to calculate 4562 * an offset, and then set b_addr to the kernel virtual 4563 * address it allocated for us. 4564 */ 4565 ASSERT(bp->b_un.b_addr == 0); 4566 4567 bp->b_edev = 0; 4568 bp->b_dev = 0; 4569 bp->b_lblkno = lbtodb(io_off); 4570 bp->b_file = vp; 4571 bp->b_offset = (offset_t)off; 4572 bp_mapin(bp); 4573 4574 /* 4575 * If doing a write beyond what we believe is EOF, 4576 * don't bother trying to read the pages from the 4577 * server, we'll just zero the pages here. We 4578 * don't check that the rw flag is S_WRITE here 4579 * because some implementations may attempt a 4580 * read access to the buffer before copying data. 4581 */ 4582 mutex_enter(&rp->r_statelock); 4583 if (io_off >= rp->r_size && seg == segkmap) { 4584 mutex_exit(&rp->r_statelock); 4585 bzero(bp->b_un.b_addr, io_len); 4586 } else { 4587 mutex_exit(&rp->r_statelock); 4588 error = nfs3_bio(bp, NULL, cr); 4589 } 4590 4591 /* 4592 * Unmap the buffer before freeing it. 4593 */ 4594 bp_mapout(bp); 4595 pageio_done(bp); 4596 4597 savepp = pp; 4598 do { 4599 pp->p_fsdata = C_NOCOMMIT; 4600 } while ((pp = pp->p_next) != savepp); 4601 4602 if (error == NFS_EOF) { 4603 /* 4604 * If doing a write system call just return 4605 * zeroed pages, else user tried to get pages 4606 * beyond EOF, return error. We don't check 4607 * that the rw flag is S_WRITE here because 4608 * some implementations may attempt a read 4609 * access to the buffer before copying data. 4610 */ 4611 if (seg == segkmap) 4612 error = 0; 4613 else 4614 error = EFAULT; 4615 } 4616 4617 if (!readahead_issued && !error) { 4618 mutex_enter(&rp->r_statelock); 4619 rp->r_nextr = io_off + io_len; 4620 mutex_exit(&rp->r_statelock); 4621 } 4622 } 4623 } 4624 4625 out: 4626 if (pl == NULL) 4627 return (error); 4628 4629 if (error) { 4630 if (pp != NULL) 4631 pvn_read_done(pp, B_ERROR); 4632 return (error); 4633 } 4634 4635 if (pagefound) { 4636 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 4637 4638 /* 4639 * Page exists in the cache, acquire the appropriate lock. 4640 * If this fails, start all over again. 4641 */ 4642 if ((pp = page_lookup(vp, off, se)) == NULL) { 4643 #ifdef DEBUG 4644 nfs3_lostpage++; 4645 #endif 4646 goto reread; 4647 } 4648 pl[0] = pp; 4649 pl[1] = NULL; 4650 return (0); 4651 } 4652 4653 if (pp != NULL) 4654 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 4655 4656 return (error); 4657 } 4658 4659 static void 4660 nfs3_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg, 4661 cred_t *cr) 4662 { 4663 int error; 4664 page_t *pp; 4665 u_offset_t io_off; 4666 size_t io_len; 4667 struct buf *bp; 4668 uint_t bsize, blksize; 4669 rnode_t *rp = VTOR(vp); 4670 page_t *savepp; 4671 4672 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 4673 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 4674 4675 mutex_enter(&rp->r_statelock); 4676 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) { 4677 /* 4678 * If less than a block left in file read less 4679 * than a block. 4680 */ 4681 blksize = rp->r_size - blkoff; 4682 } else 4683 blksize = bsize; 4684 mutex_exit(&rp->r_statelock); 4685 4686 pp = pvn_read_kluster(vp, blkoff, segkmap, addr, 4687 &io_off, &io_len, blkoff, blksize, 1); 4688 /* 4689 * The isra flag passed to the kluster function is 1, we may have 4690 * gotten a return value of NULL for a variety of reasons (# of free 4691 * pages < minfree, someone entered the page on the vnode etc). In all 4692 * cases, we want to punt on the readahead. 4693 */ 4694 if (pp == NULL) 4695 return; 4696 4697 /* 4698 * Now round the request size up to page boundaries. 4699 * This ensures that the entire page will be 4700 * initialized to zeroes if EOF is encountered. 4701 */ 4702 io_len = ptob(btopr(io_len)); 4703 4704 bp = pageio_setup(pp, io_len, vp, B_READ); 4705 ASSERT(bp != NULL); 4706 4707 /* 4708 * pageio_setup should have set b_addr to 0. This is correct since 4709 * we want to do I/O on a page boundary. bp_mapin() will use this addr 4710 * to calculate an offset, and then set b_addr to the kernel virtual 4711 * address it allocated for us. 4712 */ 4713 ASSERT(bp->b_un.b_addr == 0); 4714 4715 bp->b_edev = 0; 4716 bp->b_dev = 0; 4717 bp->b_lblkno = lbtodb(io_off); 4718 bp->b_file = vp; 4719 bp->b_offset = (offset_t)blkoff; 4720 bp_mapin(bp); 4721 4722 /* 4723 * If doing a write beyond what we believe is EOF, don't bother trying 4724 * to read the pages from the server, we'll just zero the pages here. 4725 * We don't check that the rw flag is S_WRITE here because some 4726 * implementations may attempt a read access to the buffer before 4727 * copying data. 4728 */ 4729 mutex_enter(&rp->r_statelock); 4730 if (io_off >= rp->r_size && seg == segkmap) { 4731 mutex_exit(&rp->r_statelock); 4732 bzero(bp->b_un.b_addr, io_len); 4733 error = 0; 4734 } else { 4735 mutex_exit(&rp->r_statelock); 4736 error = nfs3_bio(bp, NULL, cr); 4737 if (error == NFS_EOF) 4738 error = 0; 4739 } 4740 4741 /* 4742 * Unmap the buffer before freeing it. 4743 */ 4744 bp_mapout(bp); 4745 pageio_done(bp); 4746 4747 savepp = pp; 4748 do { 4749 pp->p_fsdata = C_NOCOMMIT; 4750 } while ((pp = pp->p_next) != savepp); 4751 4752 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ); 4753 4754 /* 4755 * In case of error set readahead offset 4756 * to the lowest offset. 4757 * pvn_read_done() calls VN_DISPOSE to destroy the pages 4758 */ 4759 if (error && rp->r_nextr > io_off) { 4760 mutex_enter(&rp->r_statelock); 4761 if (rp->r_nextr > io_off) 4762 rp->r_nextr = io_off; 4763 mutex_exit(&rp->r_statelock); 4764 } 4765 } 4766 4767 /* 4768 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE} 4769 * If len == 0, do from off to EOF. 4770 * 4771 * The normal cases should be len == 0 && off == 0 (entire vp list), 4772 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 4773 * (from pageout). 4774 */ 4775 static int 4776 nfs3_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 4777 { 4778 int error; 4779 rnode_t *rp; 4780 4781 ASSERT(cr != NULL); 4782 4783 /* 4784 * XXX - Why should this check be made here? 4785 */ 4786 if (vp->v_flag & VNOMAP) 4787 return (ENOSYS); 4788 if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp)) 4789 return (0); 4790 if (!(flags & B_ASYNC) && curproc->p_zone != VTOMI(vp)->mi_zone) 4791 return (EIO); 4792 4793 rp = VTOR(vp); 4794 mutex_enter(&rp->r_statelock); 4795 rp->r_count++; 4796 mutex_exit(&rp->r_statelock); 4797 error = nfs_putpages(vp, off, len, flags, cr); 4798 mutex_enter(&rp->r_statelock); 4799 rp->r_count--; 4800 cv_broadcast(&rp->r_cv); 4801 mutex_exit(&rp->r_statelock); 4802 4803 return (error); 4804 } 4805 4806 /* 4807 * Write out a single page, possibly klustering adjacent dirty pages. 4808 */ 4809 int 4810 nfs3_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 4811 int flags, cred_t *cr) 4812 { 4813 u_offset_t io_off; 4814 u_offset_t lbn_off; 4815 u_offset_t lbn; 4816 size_t io_len; 4817 uint_t bsize; 4818 int error; 4819 rnode_t *rp; 4820 4821 ASSERT(!vn_is_readonly(vp)); 4822 ASSERT(pp != NULL); 4823 ASSERT(cr != NULL); 4824 ASSERT((flags & B_ASYNC) || curproc->p_zone == VTOMI(vp)->mi_zone); 4825 4826 rp = VTOR(vp); 4827 ASSERT(rp->r_count > 0); 4828 4829 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE); 4830 lbn = pp->p_offset / bsize; 4831 lbn_off = lbn * bsize; 4832 4833 /* 4834 * Find a kluster that fits in one block, or in 4835 * one page if pages are bigger than blocks. If 4836 * there is less file space allocated than a whole 4837 * page, we'll shorten the i/o request below. 4838 */ 4839 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off, 4840 roundup(bsize, PAGESIZE), flags); 4841 4842 /* 4843 * pvn_write_kluster shouldn't have returned a page with offset 4844 * behind the original page we were given. Verify that. 4845 */ 4846 ASSERT((pp->p_offset / bsize) >= lbn); 4847 4848 /* 4849 * Now pp will have the list of kept dirty pages marked for 4850 * write back. It will also handle invalidation and freeing 4851 * of pages that are not dirty. Check for page length rounding 4852 * problems. 4853 */ 4854 if (io_off + io_len > lbn_off + bsize) { 4855 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE); 4856 io_len = lbn_off + bsize - io_off; 4857 } 4858 /* 4859 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a 4860 * consistent value of r_size. RMODINPROGRESS is set in writerp(). 4861 * When RMODINPROGRESS is set it indicates that a uiomove() is in 4862 * progress and the r_size has not been made consistent with the 4863 * new size of the file. When the uiomove() completes the r_size is 4864 * updated and the RMODINPROGRESS flag is cleared. 4865 * 4866 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a 4867 * consistent value of r_size. Without this handshaking, it is 4868 * possible that nfs(3)_bio() picks up the old value of r_size 4869 * before the uiomove() in writerp() completes. This will result 4870 * in the write through nfs(3)_bio() being dropped. 4871 * 4872 * More precisely, there is a window between the time the uiomove() 4873 * completes and the time the r_size is updated. If a VOP_PUTPAGE() 4874 * operation intervenes in this window, the page will be picked up, 4875 * because it is dirty (it will be unlocked, unless it was 4876 * pagecreate'd). When the page is picked up as dirty, the dirty 4877 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is 4878 * checked. This will still be the old size. Therefore the page will 4879 * not be written out. When segmap_release() calls VOP_PUTPAGE(), 4880 * the page will be found to be clean and the write will be dropped. 4881 */ 4882 if (rp->r_flags & RMODINPROGRESS) { 4883 mutex_enter(&rp->r_statelock); 4884 if ((rp->r_flags & RMODINPROGRESS) && 4885 rp->r_modaddr + MAXBSIZE > io_off && 4886 rp->r_modaddr < io_off + io_len) { 4887 page_t *plist; 4888 /* 4889 * A write is in progress for this region of the file. 4890 * If we did not detect RMODINPROGRESS here then this 4891 * path through nfs_putapage() would eventually go to 4892 * nfs(3)_bio() and may not write out all of the data 4893 * in the pages. We end up losing data. So we decide 4894 * to set the modified bit on each page in the page 4895 * list and mark the rnode with RDIRTY. This write 4896 * will be restarted at some later time. 4897 */ 4898 plist = pp; 4899 while (plist != NULL) { 4900 pp = plist; 4901 page_sub(&plist, pp); 4902 hat_setmod(pp); 4903 page_io_unlock(pp); 4904 page_unlock(pp); 4905 } 4906 rp->r_flags |= RDIRTY; 4907 mutex_exit(&rp->r_statelock); 4908 if (offp) 4909 *offp = io_off; 4910 if (lenp) 4911 *lenp = io_len; 4912 return (0); 4913 } 4914 mutex_exit(&rp->r_statelock); 4915 } 4916 4917 if (flags & B_ASYNC) { 4918 error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr, 4919 nfs3_sync_putapage); 4920 } else 4921 error = nfs3_sync_putapage(vp, pp, io_off, io_len, flags, cr); 4922 4923 if (offp) 4924 *offp = io_off; 4925 if (lenp) 4926 *lenp = io_len; 4927 return (error); 4928 } 4929 4930 static int 4931 nfs3_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 4932 int flags, cred_t *cr) 4933 { 4934 int error; 4935 rnode_t *rp; 4936 4937 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 4938 4939 flags |= B_WRITE; 4940 4941 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 4942 4943 rp = VTOR(vp); 4944 4945 if ((error == ENOSPC || error == EDQUOT || error == EFBIG || 4946 error == EACCES) && 4947 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) { 4948 if (!(rp->r_flags & ROUTOFSPACE)) { 4949 mutex_enter(&rp->r_statelock); 4950 rp->r_flags |= ROUTOFSPACE; 4951 mutex_exit(&rp->r_statelock); 4952 } 4953 flags |= B_ERROR; 4954 pvn_write_done(pp, flags); 4955 /* 4956 * If this was not an async thread, then try again to 4957 * write out the pages, but this time, also destroy 4958 * them whether or not the write is successful. This 4959 * will prevent memory from filling up with these 4960 * pages and destroying them is the only alternative 4961 * if they can't be written out. 4962 * 4963 * Don't do this if this is an async thread because 4964 * when the pages are unlocked in pvn_write_done, 4965 * some other thread could have come along, locked 4966 * them, and queued for an async thread. It would be 4967 * possible for all of the async threads to be tied 4968 * up waiting to lock the pages again and they would 4969 * all already be locked and waiting for an async 4970 * thread to handle them. Deadlock. 4971 */ 4972 if (!(flags & B_ASYNC)) { 4973 error = nfs3_putpage(vp, io_off, io_len, 4974 B_INVAL | B_FORCE, cr); 4975 } 4976 } else { 4977 if (error) 4978 flags |= B_ERROR; 4979 else if (rp->r_flags & ROUTOFSPACE) { 4980 mutex_enter(&rp->r_statelock); 4981 rp->r_flags &= ~ROUTOFSPACE; 4982 mutex_exit(&rp->r_statelock); 4983 } 4984 pvn_write_done(pp, flags); 4985 if (freemem < desfree) 4986 (void) nfs3_commit_vp(vp, (u_offset_t)0, 0, cr); 4987 } 4988 4989 return (error); 4990 } 4991 4992 static int 4993 nfs3_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 4994 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 4995 { 4996 struct segvn_crargs vn_a; 4997 int error; 4998 rnode_t *rp; 4999 struct vattr va; 5000 5001 if (curproc->p_zone != VTOMI(vp)->mi_zone) 5002 return (EIO); 5003 5004 if (vp->v_flag & VNOMAP) 5005 return (ENOSYS); 5006 5007 if (off < 0 || off + len < 0) 5008 return (ENXIO); 5009 5010 if (vp->v_type != VREG) 5011 return (ENODEV); 5012 5013 /* 5014 * If there is cached data and if close-to-open consistency 5015 * checking is not turned off and if the file system is not 5016 * mounted readonly, then force an over the wire getattr. 5017 * Otherwise, just invoke nfs3getattr to get a copy of the 5018 * attributes. The attribute cache will be used unless it 5019 * is timed out and if it is, then an over the wire getattr 5020 * will be issued. 5021 */ 5022 va.va_mask = AT_ALL; 5023 if (vn_has_cached_data(vp) && 5024 !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp)) 5025 error = nfs3_getattr_otw(vp, &va, cr); 5026 else 5027 error = nfs3getattr(vp, &va, cr); 5028 if (error) 5029 return (error); 5030 5031 /* 5032 * Check to see if the vnode is currently marked as not cachable. 5033 * This means portions of the file are locked (through VOP_FRLOCK). 5034 * In this case the map request must be refused. We use 5035 * rp->r_lkserlock to avoid a race with concurrent lock requests. 5036 */ 5037 rp = VTOR(vp); 5038 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) 5039 return (EINTR); 5040 5041 if (vp->v_flag & VNOCACHE) { 5042 error = EAGAIN; 5043 goto done; 5044 } 5045 5046 /* 5047 * Don't allow concurrent locks and mapping if mandatory locking is 5048 * enabled. 5049 */ 5050 if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) && 5051 MANDLOCK(vp, va.va_mode)) { 5052 error = EAGAIN; 5053 goto done; 5054 } 5055 5056 as_rangelock(as); 5057 if (!(flags & MAP_FIXED)) { 5058 map_addr(addrp, len, off, 1, flags); 5059 if (*addrp == NULL) { 5060 as_rangeunlock(as); 5061 error = ENOMEM; 5062 goto done; 5063 } 5064 } else { 5065 /* 5066 * User specified address - blow away any previous mappings 5067 */ 5068 (void) as_unmap(as, *addrp, len); 5069 } 5070 5071 vn_a.vp = vp; 5072 vn_a.offset = off; 5073 vn_a.type = (flags & MAP_TYPE); 5074 vn_a.prot = (uchar_t)prot; 5075 vn_a.maxprot = (uchar_t)maxprot; 5076 vn_a.flags = (flags & ~MAP_TYPE); 5077 vn_a.cred = cr; 5078 vn_a.amp = NULL; 5079 vn_a.szc = 0; 5080 vn_a.lgrp_mem_policy_flags = 0; 5081 5082 error = as_map(as, *addrp, len, segvn_create, &vn_a); 5083 as_rangeunlock(as); 5084 5085 done: 5086 nfs_rw_exit(&rp->r_lkserlock); 5087 return (error); 5088 } 5089 5090 /* ARGSUSED */ 5091 static int 5092 nfs3_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 5093 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 5094 { 5095 rnode_t *rp; 5096 5097 if (vp->v_flag & VNOMAP) 5098 return (ENOSYS); 5099 if (curproc->p_zone != VTOMI(vp)->mi_zone) 5100 return (EIO); 5101 5102 /* 5103 * Need to hold rwlock while incrementing the mapcnt so that 5104 * mmap'ing can be serialized with writes so that the caching 5105 * can be handled correctly. 5106 */ 5107 rp = VTOR(vp); 5108 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp))) 5109 return (EINTR); 5110 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len)); 5111 nfs_rw_exit(&rp->r_rwlock); 5112 5113 return (0); 5114 } 5115 5116 static int 5117 nfs3_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 5118 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr) 5119 { 5120 netobj lm_fh3; 5121 int rc; 5122 u_offset_t start, end; 5123 rnode_t *rp; 5124 int error = 0, intr = INTR(vp); 5125 5126 if (curproc->p_zone != VTOMI(vp)->mi_zone) 5127 return (EIO); 5128 /* check for valid cmd parameter */ 5129 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) 5130 return (EINVAL); 5131 5132 /* Verify l_type. */ 5133 switch (bfp->l_type) { 5134 case F_RDLCK: 5135 if (cmd != F_GETLK && !(flag & FREAD)) 5136 return (EBADF); 5137 break; 5138 case F_WRLCK: 5139 if (cmd != F_GETLK && !(flag & FWRITE)) 5140 return (EBADF); 5141 break; 5142 case F_UNLCK: 5143 intr = 0; 5144 break; 5145 5146 default: 5147 return (EINVAL); 5148 } 5149 5150 /* check the validity of the lock range */ 5151 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset)) 5152 return (rc); 5153 if (rc = flk_check_lock_data(start, end, MAXEND)) 5154 return (rc); 5155 5156 /* 5157 * If the filesystem is mounted using local locking, pass the 5158 * request off to the local locking code. 5159 */ 5160 if (VTOMI(vp)->mi_flags & MI_LLOCK) { 5161 if (cmd == F_SETLK || cmd == F_SETLKW) { 5162 /* 5163 * For complete safety, we should be holding 5164 * r_lkserlock. However, we can't call 5165 * lm_safelock and then fs_frlock while 5166 * holding r_lkserlock, so just invoke 5167 * lm_safelock and expect that this will 5168 * catch enough of the cases. 5169 */ 5170 if (!lm_safelock(vp, bfp, cr)) 5171 return (EAGAIN); 5172 } 5173 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr)); 5174 } 5175 5176 rp = VTOR(vp); 5177 5178 /* 5179 * Check whether the given lock request can proceed, given the 5180 * current file mappings. 5181 */ 5182 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr)) 5183 return (EINTR); 5184 if (cmd == F_SETLK || cmd == F_SETLKW) { 5185 if (!lm_safelock(vp, bfp, cr)) { 5186 rc = EAGAIN; 5187 goto done; 5188 } 5189 } 5190 5191 /* 5192 * Flush the cache after waiting for async I/O to finish. For new 5193 * locks, this is so that the process gets the latest bits from the 5194 * server. For unlocks, this is so that other clients see the 5195 * latest bits once the file has been unlocked. If currently dirty 5196 * pages can't be flushed, then don't allow a lock to be set. But 5197 * allow unlocks to succeed, to avoid having orphan locks on the 5198 * server. 5199 */ 5200 if (cmd != F_GETLK) { 5201 mutex_enter(&rp->r_statelock); 5202 while (rp->r_count > 0) { 5203 if (intr) { 5204 klwp_t *lwp = ttolwp(curthread); 5205 5206 if (lwp != NULL) 5207 lwp->lwp_nostop++; 5208 if (cv_wait_sig(&rp->r_cv, &rp->r_statelock) == 0) { 5209 if (lwp != NULL) 5210 lwp->lwp_nostop--; 5211 rc = EINTR; 5212 break; 5213 } 5214 if (lwp != NULL) 5215 lwp->lwp_nostop--; 5216 } else 5217 cv_wait(&rp->r_cv, &rp->r_statelock); 5218 } 5219 mutex_exit(&rp->r_statelock); 5220 if (rc != 0) 5221 goto done; 5222 error = nfs3_putpage(vp, (offset_t)0, 0, B_INVAL, cr); 5223 if (error) { 5224 if (error == ENOSPC || error == EDQUOT) { 5225 mutex_enter(&rp->r_statelock); 5226 if (!rp->r_error) 5227 rp->r_error = error; 5228 mutex_exit(&rp->r_statelock); 5229 } 5230 if (bfp->l_type != F_UNLCK) { 5231 rc = ENOLCK; 5232 goto done; 5233 } 5234 } 5235 } 5236 5237 lm_fh3.n_len = VTOFH3(vp)->fh3_length; 5238 lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data); 5239 5240 /* 5241 * Call the lock manager to do the real work of contacting 5242 * the server and obtaining the lock. 5243 */ 5244 rc = lm4_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh3, flk_cbp); 5245 5246 if (rc == 0) 5247 nfs_lockcompletion(vp, cmd); 5248 5249 done: 5250 nfs_rw_exit(&rp->r_lkserlock); 5251 return (rc); 5252 } 5253 5254 /* 5255 * Free storage space associated with the specified vnode. The portion 5256 * to be freed is specified by bfp->l_start and bfp->l_len (already 5257 * normalized to a "whence" of 0). 5258 * 5259 * This is an experimental facility whose continued existence is not 5260 * guaranteed. Currently, we only support the special case 5261 * of l_len == 0, meaning free to end of file. 5262 */ 5263 /* ARGSUSED */ 5264 static int 5265 nfs3_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, 5266 offset_t offset, cred_t *cr, caller_context_t *ct) 5267 { 5268 int error; 5269 5270 ASSERT(vp->v_type == VREG); 5271 if (cmd != F_FREESP) 5272 return (EINVAL); 5273 if (curproc->p_zone != VTOMI(vp)->mi_zone) 5274 return (EIO); 5275 5276 error = convoff(vp, bfp, 0, offset); 5277 if (!error) { 5278 ASSERT(bfp->l_start >= 0); 5279 if (bfp->l_len == 0) { 5280 struct vattr va; 5281 5282 /* 5283 * ftruncate should not change the ctime and 5284 * mtime if we truncate the file to its 5285 * previous size. 5286 */ 5287 va.va_mask = AT_SIZE; 5288 error = nfs3getattr(vp, &va, cr); 5289 if (error || va.va_size == bfp->l_start) 5290 return (error); 5291 va.va_mask = AT_SIZE; 5292 va.va_size = bfp->l_start; 5293 error = nfs3setattr(vp, &va, 0, cr); 5294 } else 5295 error = EINVAL; 5296 } 5297 5298 return (error); 5299 } 5300 5301 /* ARGSUSED */ 5302 static int 5303 nfs3_realvp(vnode_t *vp, vnode_t **vpp) 5304 { 5305 5306 return (EINVAL); 5307 } 5308 5309 /* 5310 * Setup and add an address space callback to do the work of the delmap call. 5311 * The callback will (and must be) deleted in the actual callback function. 5312 * 5313 * This is done in order to take care of the problem that we have with holding 5314 * the address space's a_lock for a long period of time (e.g. if the NFS server 5315 * is down). Callbacks will be executed in the address space code while the 5316 * a_lock is not held. Holding the address space's a_lock causes things such 5317 * as ps and fork to hang because they are trying to acquire this lock as well. 5318 */ 5319 /* ARGSUSED */ 5320 static int 5321 nfs3_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 5322 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 5323 { 5324 int caller_found; 5325 int error; 5326 rnode_t *rp; 5327 nfs_delmap_args_t *dmapp; 5328 nfs_delmapcall_t *delmap_call; 5329 5330 if (vp->v_flag & VNOMAP) 5331 return (ENOSYS); 5332 /* 5333 * A process may not change zones if it has NFS pages mmap'ed 5334 * in, so we can't legitimately get here from the wrong zone. 5335 */ 5336 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 5337 5338 rp = VTOR(vp); 5339 5340 /* 5341 * The way that the address space of this process deletes its mapping 5342 * of this file is via the following call chains: 5343 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap() 5344 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap() 5345 * 5346 * With the use of address space callbacks we are allowed to drop the 5347 * address space lock, a_lock, while executing the NFS operations that 5348 * need to go over the wire. Returning EAGAIN to the caller of this 5349 * function is what drives the execution of the callback that we add 5350 * below. The callback will be executed by the address space code 5351 * after dropping the a_lock. When the callback is finished, since 5352 * we dropped the a_lock, it must be re-acquired and segvn_unmap() 5353 * is called again on the same segment to finish the rest of the work 5354 * that needs to happen during unmapping. 5355 * 5356 * This action of calling back into the segment driver causes 5357 * nfs3_delmap() to get called again, but since the callback was 5358 * already executed at this point, it already did the work and there 5359 * is nothing left for us to do. 5360 * 5361 * To Summarize: 5362 * - The first time nfs3_delmap is called by the current thread is when 5363 * we add the caller associated with this delmap to the delmap caller 5364 * list, add the callback, and return EAGAIN. 5365 * - The second time in this call chain when nfs3_delmap is called we 5366 * will find this caller in the delmap caller list and realize there 5367 * is no more work to do thus removing this caller from the list and 5368 * returning the error that was set in the callback execution. 5369 */ 5370 caller_found = nfs_find_and_delete_delmapcall(rp, &error); 5371 if (caller_found) { 5372 /* 5373 * 'error' is from the actual delmap operations. To avoid 5374 * hangs, we need to handle the return of EAGAIN differently 5375 * since this is what drives the callback execution. 5376 * In this case, we don't want to return EAGAIN and do the 5377 * callback execution because there are none to execute. 5378 */ 5379 if (error == EAGAIN) 5380 return (0); 5381 else 5382 return (error); 5383 } 5384 5385 /* current caller was not in the list */ 5386 delmap_call = nfs_init_delmapcall(); 5387 5388 mutex_enter(&rp->r_statelock); 5389 list_insert_tail(&rp->r_indelmap, delmap_call); 5390 mutex_exit(&rp->r_statelock); 5391 5392 dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP); 5393 5394 dmapp->vp = vp; 5395 dmapp->off = off; 5396 dmapp->addr = addr; 5397 dmapp->len = len; 5398 dmapp->prot = prot; 5399 dmapp->maxprot = maxprot; 5400 dmapp->flags = flags; 5401 dmapp->cr = cr; 5402 dmapp->caller = delmap_call; 5403 5404 error = as_add_callback(as, nfs3_delmap_callback, dmapp, 5405 AS_UNMAP_EVENT, addr, len, KM_SLEEP); 5406 5407 return (error ? error : EAGAIN); 5408 } 5409 5410 /* 5411 * Remove some pages from an mmap'd vnode. Just update the 5412 * count of pages. If doing close-to-open, then flush and 5413 * commit all of the pages associated with this file. 5414 * Otherwise, start an asynchronous page flush to write out 5415 * any dirty pages. This will also associate a credential 5416 * with the rnode which can be used to write the pages. 5417 */ 5418 /* ARGSUSED */ 5419 static void 5420 nfs3_delmap_callback(struct as *as, void *arg, uint_t event) 5421 { 5422 int error; 5423 rnode_t *rp; 5424 mntinfo_t *mi; 5425 nfs_delmap_args_t *dmapp = (nfs_delmap_args_t *)arg; 5426 5427 rp = VTOR(dmapp->vp); 5428 mi = VTOMI(dmapp->vp); 5429 5430 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len)); 5431 ASSERT(rp->r_mapcnt >= 0); 5432 5433 /* 5434 * Initiate a page flush and potential commit if there are 5435 * pages, the file system was not mounted readonly, the segment 5436 * was mapped shared, and the pages themselves were writeable. 5437 */ 5438 if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) && 5439 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) { 5440 mutex_enter(&rp->r_statelock); 5441 rp->r_flags |= RDIRTY; 5442 mutex_exit(&rp->r_statelock); 5443 /* 5444 * If this is a cross-zone access a sync putpage won't work, so 5445 * the best we can do is try an async putpage. That seems 5446 * better than something more draconian such as discarding the 5447 * dirty pages. 5448 */ 5449 if ((mi->mi_flags & MI_NOCTO) || 5450 curproc->p_zone != mi->mi_zone) 5451 error = nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len, 5452 B_ASYNC, dmapp->cr); 5453 else 5454 error = nfs3_putpage_commit(dmapp->vp, dmapp->off, 5455 dmapp->len, dmapp->cr); 5456 if (!error) { 5457 mutex_enter(&rp->r_statelock); 5458 error = rp->r_error; 5459 rp->r_error = 0; 5460 mutex_exit(&rp->r_statelock); 5461 } 5462 } else 5463 error = 0; 5464 5465 if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) 5466 (void) nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len, 5467 B_INVAL, dmapp->cr); 5468 5469 dmapp->caller->error = error; 5470 (void) as_delete_callback(as, arg); 5471 kmem_free(dmapp, sizeof (nfs_delmap_args_t)); 5472 } 5473 5474 static int nfs3_pathconf_disable_cache = 0; 5475 5476 #ifdef DEBUG 5477 static int nfs3_pathconf_cache_hits = 0; 5478 static int nfs3_pathconf_cache_misses = 0; 5479 #endif 5480 5481 static int 5482 nfs3_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 5483 { 5484 int error; 5485 PATHCONF3args args; 5486 PATHCONF3res res; 5487 int douprintf; 5488 failinfo_t fi; 5489 rnode_t *rp; 5490 hrtime_t t; 5491 5492 if (curproc->p_zone != VTOMI(vp)->mi_zone) 5493 return (EIO); 5494 /* 5495 * Large file spec - need to base answer on info stored 5496 * on original FSINFO response. 5497 */ 5498 if (cmd == _PC_FILESIZEBITS) { 5499 unsigned long long ll; 5500 long l = 1; 5501 5502 ll = VTOMI(vp)->mi_maxfilesize; 5503 5504 if (ll == 0) { 5505 *valp = 0; 5506 return (0); 5507 } 5508 5509 if (ll & 0xffffffff00000000) { 5510 l += 32; ll >>= 32; 5511 } 5512 if (ll & 0xffff0000) { 5513 l += 16; ll >>= 16; 5514 } 5515 if (ll & 0xff00) { 5516 l += 8; ll >>= 8; 5517 } 5518 if (ll & 0xf0) { 5519 l += 4; ll >>= 4; 5520 } 5521 if (ll & 0xc) { 5522 l += 2; ll >>= 2; 5523 } 5524 if (ll & 0x2) 5525 l += 2; 5526 else if (ll & 0x1) 5527 l += 1; 5528 *valp = l; 5529 return (0); 5530 } 5531 5532 if (cmd == _PC_ACL_ENABLED) { 5533 *valp = _ACL_ACLENT_ENABLED; 5534 return (0); 5535 } 5536 5537 if (cmd == _PC_XATTR_EXISTS) { 5538 error = 0; 5539 *valp = 0; 5540 if (vp->v_vfsp->vfs_flag & VFS_XATTR) { 5541 vnode_t *avp; 5542 rnode_t *rp; 5543 int error = 0; 5544 mntinfo_t *mi = VTOMI(vp); 5545 5546 if (!(mi->mi_flags & MI_EXTATTR)) 5547 return (0); 5548 5549 rp = VTOR(vp); 5550 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, 5551 INTR(vp))) 5552 return (EINTR); 5553 5554 error = nfs3lookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr); 5555 if (error || avp == NULL) 5556 error = acl_getxattrdir3(vp, &avp, 0, cr, 0); 5557 5558 nfs_rw_exit(&rp->r_rwlock); 5559 5560 if (error == 0 && avp != NULL) { 5561 VN_RELE(avp); 5562 *valp = 1; 5563 } else if (error == ENOENT) 5564 error = 0; 5565 } 5566 return (error); 5567 } 5568 5569 rp = VTOR(vp); 5570 if (rp->r_pathconf != NULL) { 5571 mutex_enter(&rp->r_statelock); 5572 if (rp->r_pathconf != NULL && nfs3_pathconf_disable_cache) { 5573 kmem_free(rp->r_pathconf, sizeof (*rp->r_pathconf)); 5574 rp->r_pathconf = NULL; 5575 } 5576 if (rp->r_pathconf != NULL) { 5577 error = 0; 5578 switch (cmd) { 5579 case _PC_LINK_MAX: 5580 *valp = rp->r_pathconf->link_max; 5581 break; 5582 case _PC_NAME_MAX: 5583 *valp = rp->r_pathconf->name_max; 5584 break; 5585 case _PC_PATH_MAX: 5586 case _PC_SYMLINK_MAX: 5587 *valp = MAXPATHLEN; 5588 break; 5589 case _PC_CHOWN_RESTRICTED: 5590 *valp = rp->r_pathconf->chown_restricted; 5591 break; 5592 case _PC_NO_TRUNC: 5593 *valp = rp->r_pathconf->no_trunc; 5594 break; 5595 default: 5596 error = EINVAL; 5597 break; 5598 } 5599 mutex_exit(&rp->r_statelock); 5600 #ifdef DEBUG 5601 nfs3_pathconf_cache_hits++; 5602 #endif 5603 return (error); 5604 } 5605 mutex_exit(&rp->r_statelock); 5606 } 5607 #ifdef DEBUG 5608 nfs3_pathconf_cache_misses++; 5609 #endif 5610 5611 args.object = *VTOFH3(vp); 5612 fi.vp = vp; 5613 fi.fhp = (caddr_t)&args.object; 5614 fi.copyproc = nfs3copyfh; 5615 fi.lookupproc = nfs3lookup; 5616 fi.xattrdirproc = acl_getxattrdir3; 5617 5618 douprintf = 1; 5619 5620 t = gethrtime(); 5621 5622 error = rfs3call(VTOMI(vp), NFSPROC3_PATHCONF, 5623 xdr_nfs_fh3, (caddr_t)&args, 5624 xdr_PATHCONF3res, (caddr_t)&res, cr, 5625 &douprintf, &res.status, 0, &fi); 5626 5627 if (error) 5628 return (error); 5629 5630 error = geterrno3(res.status); 5631 5632 if (!error) { 5633 nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr); 5634 if (!nfs3_pathconf_disable_cache) { 5635 mutex_enter(&rp->r_statelock); 5636 if (rp->r_pathconf == NULL) { 5637 rp->r_pathconf = kmem_alloc( 5638 sizeof (*rp->r_pathconf), KM_NOSLEEP); 5639 if (rp->r_pathconf != NULL) 5640 *rp->r_pathconf = res.resok.info; 5641 } 5642 mutex_exit(&rp->r_statelock); 5643 } 5644 switch (cmd) { 5645 case _PC_LINK_MAX: 5646 *valp = res.resok.info.link_max; 5647 break; 5648 case _PC_NAME_MAX: 5649 *valp = res.resok.info.name_max; 5650 break; 5651 case _PC_PATH_MAX: 5652 case _PC_SYMLINK_MAX: 5653 *valp = MAXPATHLEN; 5654 break; 5655 case _PC_CHOWN_RESTRICTED: 5656 *valp = res.resok.info.chown_restricted; 5657 break; 5658 case _PC_NO_TRUNC: 5659 *valp = res.resok.info.no_trunc; 5660 break; 5661 default: 5662 return (EINVAL); 5663 } 5664 } else { 5665 nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr); 5666 PURGE_STALE_FH(error, vp, cr); 5667 } 5668 5669 return (error); 5670 } 5671 5672 /* 5673 * Called by async thread to do synchronous pageio. Do the i/o, wait 5674 * for it to complete, and cleanup the page list when done. 5675 */ 5676 static int 5677 nfs3_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 5678 int flags, cred_t *cr) 5679 { 5680 int error; 5681 5682 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 5683 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 5684 if (flags & B_READ) 5685 pvn_read_done(pp, (error ? B_ERROR : 0) | flags); 5686 else 5687 pvn_write_done(pp, (error ? B_ERROR : 0) | flags); 5688 return (error); 5689 } 5690 5691 static int 5692 nfs3_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len, 5693 int flags, cred_t *cr) 5694 { 5695 int error; 5696 rnode_t *rp; 5697 5698 if (pp == NULL) 5699 return (EINVAL); 5700 if (!(flags & B_ASYNC) && curproc->p_zone != VTOMI(vp)->mi_zone) 5701 return (EIO); 5702 5703 rp = VTOR(vp); 5704 mutex_enter(&rp->r_statelock); 5705 rp->r_count++; 5706 mutex_exit(&rp->r_statelock); 5707 5708 if (flags & B_ASYNC) { 5709 error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr, 5710 nfs3_sync_pageio); 5711 } else 5712 error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr); 5713 mutex_enter(&rp->r_statelock); 5714 rp->r_count--; 5715 cv_broadcast(&rp->r_cv); 5716 mutex_exit(&rp->r_statelock); 5717 return (error); 5718 } 5719 5720 static void 5721 nfs3_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr) 5722 { 5723 int error; 5724 rnode_t *rp; 5725 page_t *plist; 5726 page_t *pptr; 5727 offset3 offset; 5728 count3 len; 5729 k_sigset_t smask; 5730 5731 /* 5732 * We should get called with fl equal to either B_FREE or 5733 * B_INVAL. Any other value is illegal. 5734 * 5735 * The page that we are either supposed to free or destroy 5736 * should be exclusive locked and its io lock should not 5737 * be held. 5738 */ 5739 ASSERT(fl == B_FREE || fl == B_INVAL); 5740 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr); 5741 rp = VTOR(vp); 5742 5743 /* 5744 * If the page doesn't need to be committed or we shouldn't 5745 * even bother attempting to commit it, then just make sure 5746 * that the p_fsdata byte is clear and then either free or 5747 * destroy the page as appropriate. 5748 */ 5749 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & RSTALE)) { 5750 pp->p_fsdata = C_NOCOMMIT; 5751 if (fl == B_FREE) 5752 page_free(pp, dn); 5753 else 5754 page_destroy(pp, dn); 5755 return; 5756 } 5757 5758 /* 5759 * If there is a page invalidation operation going on, then 5760 * if this is one of the pages being destroyed, then just 5761 * clear the p_fsdata byte and then either free or destroy 5762 * the page as appropriate. 5763 */ 5764 mutex_enter(&rp->r_statelock); 5765 if ((rp->r_flags & RTRUNCATE) && pp->p_offset >= rp->r_truncaddr) { 5766 mutex_exit(&rp->r_statelock); 5767 pp->p_fsdata = C_NOCOMMIT; 5768 if (fl == B_FREE) 5769 page_free(pp, dn); 5770 else 5771 page_destroy(pp, dn); 5772 return; 5773 } 5774 5775 /* 5776 * If we are freeing this page and someone else is already 5777 * waiting to do a commit, then just unlock the page and 5778 * return. That other thread will take care of commiting 5779 * this page. The page can be freed sometime after the 5780 * commit has finished. Otherwise, if the page is marked 5781 * as delay commit, then we may be getting called from 5782 * pvn_write_done, one page at a time. This could result 5783 * in one commit per page, so we end up doing lots of small 5784 * commits instead of fewer larger commits. This is bad, 5785 * we want do as few commits as possible. 5786 */ 5787 if (fl == B_FREE) { 5788 if (rp->r_flags & RCOMMITWAIT) { 5789 page_unlock(pp); 5790 mutex_exit(&rp->r_statelock); 5791 return; 5792 } 5793 if (pp->p_fsdata == C_DELAYCOMMIT) { 5794 pp->p_fsdata = C_COMMIT; 5795 page_unlock(pp); 5796 mutex_exit(&rp->r_statelock); 5797 return; 5798 } 5799 } 5800 5801 /* 5802 * Check to see if there is a signal which would prevent an 5803 * attempt to commit the pages from being successful. If so, 5804 * then don't bother with all of the work to gather pages and 5805 * generate the unsuccessful RPC. Just return from here and 5806 * let the page be committed at some later time. 5807 */ 5808 sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT); 5809 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) { 5810 sigunintr(&smask); 5811 page_unlock(pp); 5812 mutex_exit(&rp->r_statelock); 5813 return; 5814 } 5815 sigunintr(&smask); 5816 5817 /* 5818 * We are starting to need to commit pages, so let's try 5819 * to commit as many as possible at once to reduce the 5820 * overhead. 5821 * 5822 * Set the `commit inprogress' state bit. We must 5823 * first wait until any current one finishes. Then 5824 * we initialize the c_pages list with this page. 5825 */ 5826 while (rp->r_flags & RCOMMIT) { 5827 rp->r_flags |= RCOMMITWAIT; 5828 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 5829 rp->r_flags &= ~RCOMMITWAIT; 5830 } 5831 rp->r_flags |= RCOMMIT; 5832 mutex_exit(&rp->r_statelock); 5833 ASSERT(rp->r_commit.c_pages == NULL); 5834 rp->r_commit.c_pages = pp; 5835 rp->r_commit.c_commbase = (offset3)pp->p_offset; 5836 rp->r_commit.c_commlen = PAGESIZE; 5837 5838 /* 5839 * Gather together all other pages which can be committed. 5840 * They will all be chained off r_commit.c_pages. 5841 */ 5842 nfs3_get_commit(vp); 5843 5844 /* 5845 * Clear the `commit inprogress' status and disconnect 5846 * the list of pages to be committed from the rnode. 5847 * At this same time, we also save the starting offset 5848 * and length of data to be committed on the server. 5849 */ 5850 plist = rp->r_commit.c_pages; 5851 rp->r_commit.c_pages = NULL; 5852 offset = rp->r_commit.c_commbase; 5853 len = rp->r_commit.c_commlen; 5854 mutex_enter(&rp->r_statelock); 5855 rp->r_flags &= ~RCOMMIT; 5856 cv_broadcast(&rp->r_commit.c_cv); 5857 mutex_exit(&rp->r_statelock); 5858 5859 if (curproc == proc_pageout || curproc == proc_fsflush || 5860 curproc->p_zone != VTOMI(vp)->mi_zone) { 5861 nfs_async_commit(vp, plist, offset, len, cr, nfs3_async_commit); 5862 return; 5863 } 5864 5865 /* 5866 * Actually generate the COMMIT3 over the wire operation. 5867 */ 5868 error = nfs3_commit(vp, offset, len, cr); 5869 5870 /* 5871 * If we got an error during the commit, just unlock all 5872 * of the pages. The pages will get retransmitted to the 5873 * server during a putpage operation. 5874 */ 5875 if (error) { 5876 while (plist != NULL) { 5877 pptr = plist; 5878 page_sub(&plist, pptr); 5879 page_unlock(pptr); 5880 } 5881 return; 5882 } 5883 5884 /* 5885 * We've tried as hard as we can to commit the data to stable 5886 * storage on the server. We release the rest of the pages 5887 * and clear the commit required state. They will be put 5888 * onto the tail of the cachelist if they are nolonger 5889 * mapped. 5890 */ 5891 while (plist != pp) { 5892 pptr = plist; 5893 page_sub(&plist, pptr); 5894 pptr->p_fsdata = C_NOCOMMIT; 5895 (void) page_release(pptr, 1); 5896 } 5897 5898 /* 5899 * It is possible that nfs3_commit didn't return error but 5900 * some other thread has modified the page we are going 5901 * to free/destroy. 5902 * In this case we need to rewrite the page. Do an explicit check 5903 * before attempting to free/destroy the page. If modified, needs to 5904 * be rewritten so unlock the page and return. 5905 */ 5906 if (hat_ismod(pp)) { 5907 pp->p_fsdata = C_NOCOMMIT; 5908 page_unlock(pp); 5909 return; 5910 } 5911 5912 /* 5913 * Now, as appropriate, either free or destroy the page 5914 * that we were called with. 5915 */ 5916 pp->p_fsdata = C_NOCOMMIT; 5917 if (fl == B_FREE) 5918 page_free(pp, dn); 5919 else 5920 page_destroy(pp, dn); 5921 } 5922 5923 static int 5924 nfs3_commit(vnode_t *vp, offset3 offset, count3 count, cred_t *cr) 5925 { 5926 int error; 5927 rnode_t *rp; 5928 COMMIT3args args; 5929 COMMIT3res res; 5930 int douprintf; 5931 cred_t *cred; 5932 5933 rp = VTOR(vp); 5934 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 5935 5936 mutex_enter(&rp->r_statelock); 5937 if (rp->r_cred != NULL) { 5938 cred = rp->r_cred; 5939 crhold(cred); 5940 } else { 5941 rp->r_cred = cr; 5942 crhold(cr); 5943 cred = cr; 5944 crhold(cred); 5945 } 5946 mutex_exit(&rp->r_statelock); 5947 5948 args.file = *VTOFH3(vp); 5949 args.offset = offset; 5950 args.count = count; 5951 5952 doitagain: 5953 douprintf = 1; 5954 error = rfs3call(VTOMI(vp), NFSPROC3_COMMIT, 5955 xdr_COMMIT3args, (caddr_t)&args, 5956 xdr_COMMIT3res, (caddr_t)&res, cred, 5957 &douprintf, &res.status, 0, NULL); 5958 5959 crfree(cred); 5960 5961 if (error) 5962 return (error); 5963 5964 error = geterrno3(res.status); 5965 if (!error) { 5966 ASSERT(rp->r_flags & RHAVEVERF); 5967 mutex_enter(&rp->r_statelock); 5968 if (rp->r_verf == res.resok.verf) { 5969 mutex_exit(&rp->r_statelock); 5970 return (0); 5971 } 5972 nfs3_set_mod(vp); 5973 rp->r_verf = res.resok.verf; 5974 mutex_exit(&rp->r_statelock); 5975 error = NFS_VERF_MISMATCH; 5976 } else { 5977 if (error == EACCES) { 5978 mutex_enter(&rp->r_statelock); 5979 if (cred != cr) { 5980 if (rp->r_cred != NULL) 5981 crfree(rp->r_cred); 5982 rp->r_cred = cr; 5983 crhold(cr); 5984 cred = cr; 5985 crhold(cred); 5986 mutex_exit(&rp->r_statelock); 5987 goto doitagain; 5988 } 5989 mutex_exit(&rp->r_statelock); 5990 } 5991 /* 5992 * Can't do a PURGE_STALE_FH here because this 5993 * can cause a deadlock. nfs3_commit can 5994 * be called from nfs3_dispose which can be called 5995 * indirectly via pvn_vplist_dirty. PURGE_STALE_FH 5996 * can call back to pvn_vplist_dirty. 5997 */ 5998 if (error == ESTALE) { 5999 mutex_enter(&rp->r_statelock); 6000 rp->r_flags |= RSTALE; 6001 if (!rp->r_error) 6002 rp->r_error = error; 6003 mutex_exit(&rp->r_statelock); 6004 PURGE_ATTRCACHE(vp); 6005 } else { 6006 mutex_enter(&rp->r_statelock); 6007 if (!rp->r_error) 6008 rp->r_error = error; 6009 mutex_exit(&rp->r_statelock); 6010 } 6011 } 6012 6013 return (error); 6014 } 6015 6016 static void 6017 nfs3_set_mod(vnode_t *vp) 6018 { 6019 page_t *pp; 6020 kmutex_t *vphm; 6021 6022 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 6023 vphm = page_vnode_mutex(vp); 6024 mutex_enter(vphm); 6025 if ((pp = vp->v_pages) != NULL) { 6026 do { 6027 if (pp->p_fsdata != C_NOCOMMIT) { 6028 hat_setmod(pp); 6029 pp->p_fsdata = C_NOCOMMIT; 6030 } 6031 } while ((pp = pp->p_vpnext) != vp->v_pages); 6032 } 6033 mutex_exit(vphm); 6034 } 6035 6036 6037 /* 6038 * This routine is used to gather together a page list of the pages 6039 * which are to be committed on the server. This routine must not 6040 * be called if the calling thread holds any locked pages. 6041 * 6042 * The calling thread must have set RCOMMIT. This bit is used to 6043 * serialize access to the commit structure in the rnode. As long 6044 * as the thread has set RCOMMIT, then it can manipulate the commit 6045 * structure without requiring any other locks. 6046 */ 6047 static void 6048 nfs3_get_commit(vnode_t *vp) 6049 { 6050 rnode_t *rp; 6051 page_t *pp; 6052 kmutex_t *vphm; 6053 6054 rp = VTOR(vp); 6055 6056 ASSERT(rp->r_flags & RCOMMIT); 6057 6058 vphm = page_vnode_mutex(vp); 6059 mutex_enter(vphm); 6060 6061 /* 6062 * If there are no pages associated with this vnode, then 6063 * just return. 6064 */ 6065 if ((pp = vp->v_pages) == NULL) { 6066 mutex_exit(vphm); 6067 return; 6068 } 6069 6070 /* 6071 * Step through all of the pages associated with this vnode 6072 * looking for pages which need to be committed. 6073 */ 6074 do { 6075 /* 6076 * If this page does not need to be committed or is 6077 * modified, then just skip it. 6078 */ 6079 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) 6080 continue; 6081 6082 /* 6083 * Attempt to lock the page. If we can't, then 6084 * someone else is messing with it and we will 6085 * just skip it. 6086 */ 6087 if (!page_trylock(pp, SE_EXCL)) 6088 continue; 6089 6090 /* 6091 * If this page does not need to be committed or is 6092 * modified, then just skip it. Recheck now that 6093 * the page is locked. 6094 */ 6095 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 6096 page_unlock(pp); 6097 continue; 6098 } 6099 6100 if (PP_ISFREE(pp)) { 6101 cmn_err(CE_PANIC, "nfs3_get_commit: %p is free", 6102 (void *)pp); 6103 } 6104 6105 /* 6106 * The page needs to be committed and we locked it. 6107 * Update the base and length parameters and add it 6108 * to r_pages. 6109 */ 6110 if (rp->r_commit.c_pages == NULL) { 6111 rp->r_commit.c_commbase = (offset3)pp->p_offset; 6112 rp->r_commit.c_commlen = PAGESIZE; 6113 } else if (pp->p_offset < rp->r_commit.c_commbase) { 6114 rp->r_commit.c_commlen = rp->r_commit.c_commbase - 6115 (offset3)pp->p_offset + rp->r_commit.c_commlen; 6116 rp->r_commit.c_commbase = (offset3)pp->p_offset; 6117 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen) 6118 <= pp->p_offset) { 6119 rp->r_commit.c_commlen = (offset3)pp->p_offset - 6120 rp->r_commit.c_commbase + PAGESIZE; 6121 } 6122 page_add(&rp->r_commit.c_pages, pp); 6123 } while ((pp = pp->p_vpnext) != vp->v_pages); 6124 6125 mutex_exit(vphm); 6126 } 6127 6128 /* 6129 * This routine is used to gather together a page list of the pages 6130 * which are to be committed on the server. This routine must not 6131 * be called if the calling thread holds any locked pages. 6132 * 6133 * The calling thread must have set RCOMMIT. This bit is used to 6134 * serialize access to the commit structure in the rnode. As long 6135 * as the thread has set RCOMMIT, then it can manipulate the commit 6136 * structure without requiring any other locks. 6137 */ 6138 static void 6139 nfs3_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len) 6140 { 6141 6142 rnode_t *rp; 6143 page_t *pp; 6144 u_offset_t end; 6145 u_offset_t off; 6146 6147 ASSERT(len != 0); 6148 6149 rp = VTOR(vp); 6150 6151 ASSERT(rp->r_flags & RCOMMIT); 6152 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 6153 6154 /* 6155 * If there are no pages associated with this vnode, then 6156 * just return. 6157 */ 6158 if ((pp = vp->v_pages) == NULL) 6159 return; 6160 6161 /* 6162 * Calculate the ending offset. 6163 */ 6164 end = soff + len; 6165 6166 for (off = soff; off < end; off += PAGESIZE) { 6167 /* 6168 * Lookup each page by vp, offset. 6169 */ 6170 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL) 6171 continue; 6172 6173 /* 6174 * If this page does not need to be committed or is 6175 * modified, then just skip it. 6176 */ 6177 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) { 6178 page_unlock(pp); 6179 continue; 6180 } 6181 6182 ASSERT(PP_ISFREE(pp) == 0); 6183 6184 /* 6185 * The page needs to be committed and we locked it. 6186 * Update the base and length parameters and add it 6187 * to r_pages. 6188 */ 6189 if (rp->r_commit.c_pages == NULL) { 6190 rp->r_commit.c_commbase = (offset3)pp->p_offset; 6191 rp->r_commit.c_commlen = PAGESIZE; 6192 } else { 6193 rp->r_commit.c_commlen = (offset3)pp->p_offset - 6194 rp->r_commit.c_commbase + PAGESIZE; 6195 } 6196 page_add(&rp->r_commit.c_pages, pp); 6197 } 6198 } 6199 6200 #if 0 /* unused */ 6201 #ifdef DEBUG 6202 static int 6203 nfs3_no_uncommitted_pages(vnode_t *vp) 6204 { 6205 page_t *pp; 6206 kmutex_t *vphm; 6207 6208 vphm = page_vnode_mutex(vp); 6209 mutex_enter(vphm); 6210 if ((pp = vp->v_pages) != NULL) { 6211 do { 6212 if (pp->p_fsdata != C_NOCOMMIT) { 6213 mutex_exit(vphm); 6214 return (0); 6215 } 6216 } while ((pp = pp->p_vpnext) != vp->v_pages); 6217 } 6218 mutex_exit(vphm); 6219 6220 return (1); 6221 } 6222 #endif 6223 #endif 6224 6225 static int 6226 nfs3_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr) 6227 { 6228 int error; 6229 writeverf3 write_verf; 6230 rnode_t *rp = VTOR(vp); 6231 6232 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 6233 /* 6234 * Flush the data portion of the file and then commit any 6235 * portions which need to be committed. This may need to 6236 * be done twice if the server has changed state since 6237 * data was last written. The data will need to be 6238 * rewritten to the server and then a new commit done. 6239 * 6240 * In fact, this may need to be done several times if the 6241 * server is having problems and crashing while we are 6242 * attempting to do this. 6243 */ 6244 6245 top: 6246 /* 6247 * Do a flush based on the poff and plen arguments. This 6248 * will asynchronously write out any modified pages in the 6249 * range specified by (poff, plen). This starts all of the 6250 * i/o operations which will be waited for in the next 6251 * call to nfs3_putpage 6252 */ 6253 6254 mutex_enter(&rp->r_statelock); 6255 write_verf = rp->r_verf; 6256 mutex_exit(&rp->r_statelock); 6257 6258 error = nfs3_putpage(vp, poff, plen, B_ASYNC, cr); 6259 if (error == EAGAIN) 6260 error = 0; 6261 6262 /* 6263 * Do a flush based on the poff and plen arguments. This 6264 * will synchronously write out any modified pages in the 6265 * range specified by (poff, plen) and wait until all of 6266 * the asynchronous i/o's in that range are done as well. 6267 */ 6268 if (!error) 6269 error = nfs3_putpage(vp, poff, plen, 0, cr); 6270 6271 if (error) 6272 return (error); 6273 6274 mutex_enter(&rp->r_statelock); 6275 if (rp->r_verf != write_verf) { 6276 mutex_exit(&rp->r_statelock); 6277 goto top; 6278 } 6279 mutex_exit(&rp->r_statelock); 6280 6281 /* 6282 * Now commit any pages which might need to be committed. 6283 * If the error, NFS_VERF_MISMATCH, is returned, then 6284 * start over with the flush operation. 6285 */ 6286 6287 error = nfs3_commit_vp(vp, poff, plen, cr); 6288 6289 if (error == NFS_VERF_MISMATCH) 6290 goto top; 6291 6292 return (error); 6293 } 6294 6295 static int 6296 nfs3_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, cred_t *cr) 6297 { 6298 rnode_t *rp; 6299 page_t *plist; 6300 offset3 offset; 6301 count3 len; 6302 6303 6304 rp = VTOR(vp); 6305 6306 if (curproc->p_zone != VTOMI(vp)->mi_zone) 6307 return (EIO); 6308 /* 6309 * Set the `commit inprogress' state bit. We must 6310 * first wait until any current one finishes. 6311 */ 6312 mutex_enter(&rp->r_statelock); 6313 while (rp->r_flags & RCOMMIT) { 6314 rp->r_flags |= RCOMMITWAIT; 6315 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock); 6316 rp->r_flags &= ~RCOMMITWAIT; 6317 } 6318 rp->r_flags |= RCOMMIT; 6319 mutex_exit(&rp->r_statelock); 6320 6321 /* 6322 * Gather together all of the pages which need to be 6323 * committed. 6324 */ 6325 if (plen == 0) 6326 nfs3_get_commit(vp); 6327 else 6328 nfs3_get_commit_range(vp, poff, plen); 6329 6330 /* 6331 * Clear the `commit inprogress' bit and disconnect the 6332 * page list which was gathered together in nfs3_get_commit. 6333 */ 6334 plist = rp->r_commit.c_pages; 6335 rp->r_commit.c_pages = NULL; 6336 offset = rp->r_commit.c_commbase; 6337 len = rp->r_commit.c_commlen; 6338 mutex_enter(&rp->r_statelock); 6339 rp->r_flags &= ~RCOMMIT; 6340 cv_broadcast(&rp->r_commit.c_cv); 6341 mutex_exit(&rp->r_statelock); 6342 6343 /* 6344 * If any pages need to be committed, commit them and 6345 * then unlock them so that they can be freed some 6346 * time later. 6347 */ 6348 if (plist != NULL) { 6349 /* 6350 * No error occurred during the flush portion 6351 * of this operation, so now attempt to commit 6352 * the data to stable storage on the server. 6353 * 6354 * This will unlock all of the pages on the list. 6355 */ 6356 return (nfs3_sync_commit(vp, plist, offset, len, cr)); 6357 } 6358 return (0); 6359 } 6360 6361 static int 6362 nfs3_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 6363 cred_t *cr) 6364 { 6365 int error; 6366 page_t *pp; 6367 6368 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 6369 error = nfs3_commit(vp, offset, count, cr); 6370 6371 /* 6372 * If we got an error, then just unlock all of the pages 6373 * on the list. 6374 */ 6375 if (error) { 6376 while (plist != NULL) { 6377 pp = plist; 6378 page_sub(&plist, pp); 6379 page_unlock(pp); 6380 } 6381 return (error); 6382 } 6383 /* 6384 * We've tried as hard as we can to commit the data to stable 6385 * storage on the server. We just unlock the pages and clear 6386 * the commit required state. They will get freed later. 6387 */ 6388 while (plist != NULL) { 6389 pp = plist; 6390 page_sub(&plist, pp); 6391 pp->p_fsdata = C_NOCOMMIT; 6392 page_unlock(pp); 6393 } 6394 6395 return (error); 6396 } 6397 6398 static void 6399 nfs3_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count, 6400 cred_t *cr) 6401 { 6402 ASSERT(curproc->p_zone == VTOMI(vp)->mi_zone); 6403 (void) nfs3_sync_commit(vp, plist, offset, count, cr); 6404 } 6405 6406 static int 6407 nfs3_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 6408 { 6409 int error; 6410 mntinfo_t *mi; 6411 6412 mi = VTOMI(vp); 6413 6414 if (curproc->p_zone != mi->mi_zone) 6415 return (EIO); 6416 6417 if (mi->mi_flags & MI_ACL) { 6418 error = acl_setacl3(vp, vsecattr, flag, cr); 6419 if (mi->mi_flags & MI_ACL) 6420 return (error); 6421 } 6422 6423 return (ENOSYS); 6424 } 6425 6426 static int 6427 nfs3_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr) 6428 { 6429 int error; 6430 mntinfo_t *mi; 6431 6432 mi = VTOMI(vp); 6433 6434 if (curproc->p_zone != mi->mi_zone) 6435 return (EIO); 6436 6437 if (mi->mi_flags & MI_ACL) { 6438 error = acl_getacl3(vp, vsecattr, flag, cr); 6439 if (mi->mi_flags & MI_ACL) 6440 return (error); 6441 } 6442 6443 return (fs_fab_acl(vp, vsecattr, flag, cr)); 6444 } 6445 6446 static int 6447 nfs3_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr) 6448 { 6449 int error; 6450 struct shrlock nshr; 6451 struct nfs_owner nfs_owner; 6452 netobj lm_fh3; 6453 6454 if (curproc->p_zone != VTOMI(vp)->mi_zone) 6455 return (EIO); 6456 6457 /* 6458 * check for valid cmd parameter 6459 */ 6460 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS) 6461 return (EINVAL); 6462 6463 /* 6464 * Check access permissions 6465 */ 6466 if (cmd == F_SHARE && 6467 (((shr->s_access & F_RDACC) && !(flag & FREAD)) || 6468 ((shr->s_access & F_WRACC) && !(flag & FWRITE)))) 6469 return (EBADF); 6470 6471 /* 6472 * If the filesystem is mounted using local locking, pass the 6473 * request off to the local share code. 6474 */ 6475 if (VTOMI(vp)->mi_flags & MI_LLOCK) 6476 return (fs_shrlock(vp, cmd, shr, flag, cr)); 6477 6478 switch (cmd) { 6479 case F_SHARE: 6480 case F_UNSHARE: 6481 lm_fh3.n_len = VTOFH3(vp)->fh3_length; 6482 lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data); 6483 6484 /* 6485 * If passed an owner that is too large to fit in an 6486 * nfs_owner it is likely a recursive call from the 6487 * lock manager client and pass it straight through. If 6488 * it is not a nfs_owner then simply return an error. 6489 */ 6490 if (shr->s_own_len > sizeof (nfs_owner.lowner)) { 6491 if (((struct nfs_owner *)shr->s_owner)->magic != 6492 NFS_OWNER_MAGIC) 6493 return (EINVAL); 6494 6495 if (error = lm4_shrlock(vp, cmd, shr, flag, &lm_fh3)) { 6496 error = set_errno(error); 6497 } 6498 return (error); 6499 } 6500 /* 6501 * Remote share reservations owner is a combination of 6502 * a magic number, hostname, and the local owner 6503 */ 6504 bzero(&nfs_owner, sizeof (nfs_owner)); 6505 nfs_owner.magic = NFS_OWNER_MAGIC; 6506 (void) strncpy(nfs_owner.hname, uts_nodename(), 6507 sizeof (nfs_owner.hname)); 6508 bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len); 6509 nshr.s_access = shr->s_access; 6510 nshr.s_deny = shr->s_deny; 6511 nshr.s_sysid = 0; 6512 nshr.s_pid = ttoproc(curthread)->p_pid; 6513 nshr.s_own_len = sizeof (nfs_owner); 6514 nshr.s_owner = (caddr_t)&nfs_owner; 6515 6516 if (error = lm4_shrlock(vp, cmd, &nshr, flag, &lm_fh3)) { 6517 error = set_errno(error); 6518 } 6519 6520 break; 6521 6522 case F_HASREMOTELOCKS: 6523 /* 6524 * NFS client can't store remote locks itself 6525 */ 6526 shr->s_access = 0; 6527 error = 0; 6528 break; 6529 6530 default: 6531 error = EINVAL; 6532 break; 6533 } 6534 6535 return (error); 6536 } 6537