zfs_vnops.c revision 260786
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21/* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2013 by Delphix. All rights reserved. 24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 25 */ 26 27/* Portions Copyright 2007 Jeremy Teo */ 28/* Portions Copyright 2010 Robert Milkowski */ 29 30#include <sys/types.h> 31#include <sys/param.h> 32#include <sys/time.h> 33#include <sys/systm.h> 34#include <sys/sysmacros.h> 35#include <sys/resource.h> 36#include <sys/vfs.h> 37#include <sys/vm.h> 38#include <sys/vnode.h> 39#include <sys/file.h> 40#include <sys/stat.h> 41#include <sys/kmem.h> 42#include <sys/taskq.h> 43#include <sys/uio.h> 44#include <sys/atomic.h> 45#include <sys/namei.h> 46#include <sys/mman.h> 47#include <sys/cmn_err.h> 48#include <sys/errno.h> 49#include <sys/unistd.h> 50#include <sys/zfs_dir.h> 51#include <sys/zfs_ioctl.h> 52#include <sys/fs/zfs.h> 53#include <sys/dmu.h> 54#include <sys/dmu_objset.h> 55#include <sys/spa.h> 56#include <sys/txg.h> 57#include <sys/dbuf.h> 58#include <sys/zap.h> 59#include <sys/sa.h> 60#include <sys/dirent.h> 61#include <sys/policy.h> 62#include <sys/sunddi.h> 63#include <sys/filio.h> 64#include <sys/sid.h> 65#include <sys/zfs_ctldir.h> 66#include <sys/zfs_fuid.h> 67#include <sys/zfs_sa.h> 68#include <sys/dnlc.h> 69#include <sys/zfs_rlock.h> 70#include <sys/extdirent.h> 71#include <sys/kidmap.h> 72#include <sys/bio.h> 73#include <sys/buf.h> 74#include <sys/sched.h> 75#include <sys/acl.h> 76#include <vm/vm_param.h> 77#include <vm/vm_pageout.h> 78 79/* 80 * Programming rules. 81 * 82 * Each vnode op performs some logical unit of work. To do this, the ZPL must 83 * properly lock its in-core state, create a DMU transaction, do the work, 84 * record this work in the intent log (ZIL), commit the DMU transaction, 85 * and wait for the intent log to commit if it is a synchronous operation. 86 * Moreover, the vnode ops must work in both normal and log replay context. 87 * The ordering of events is important to avoid deadlocks and references 88 * to freed memory. The example below illustrates the following Big Rules: 89 * 90 * (1) A check must be made in each zfs thread for a mounted file system. 91 * This is done avoiding races using ZFS_ENTER(zfsvfs). 92 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes 93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros 94 * can return EIO from the calling function. 95 * 96 * (2) VN_RELE() should always be the last thing except for zil_commit() 97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 98 * First, if it's the last reference, the vnode/znode 99 * can be freed, so the zp may point to freed memory. Second, the last 100 * reference will call zfs_zinactive(), which may induce a lot of work -- 101 * pushing cached pages (which acquires range locks) and syncing out 102 * cached atime changes. Third, zfs_zinactive() may require a new tx, 103 * which could deadlock the system if you were already holding one. 104 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC(). 105 * 106 * (3) All range locks must be grabbed before calling dmu_tx_assign(), 107 * as they can span dmu_tx_assign() calls. 108 * 109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to 110 * dmu_tx_assign(). This is critical because we don't want to block 111 * while holding locks. 112 * 113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This 114 * reduces lock contention and CPU usage when we must wait (note that if 115 * throughput is constrained by the storage, nearly every transaction 116 * must wait). 117 * 118 * Note, in particular, that if a lock is sometimes acquired before 119 * the tx assigns, and sometimes after (e.g. z_lock), then failing 120 * to use a non-blocking assign can deadlock the system. The scenario: 121 * 122 * Thread A has grabbed a lock before calling dmu_tx_assign(). 123 * Thread B is in an already-assigned tx, and blocks for this lock. 124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 125 * forever, because the previous txg can't quiesce until B's tx commits. 126 * 127 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent 129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT, 130 * to indicate that this operation has already called dmu_tx_wait(). 131 * This will ensure that we don't retry forever, waiting a short bit 132 * each time. 133 * 134 * (5) If the operation succeeded, generate the intent log entry for it 135 * before dropping locks. This ensures that the ordering of events 136 * in the intent log matches the order in which they actually occurred. 137 * During ZIL replay the zfs_log_* functions will update the sequence 138 * number to indicate the zil transaction has replayed. 139 * 140 * (6) At the end of each vnode op, the DMU tx must always commit, 141 * regardless of whether there were any errors. 142 * 143 * (7) After dropping all locks, invoke zil_commit(zilog, foid) 144 * to ensure that synchronous semantics are provided when necessary. 145 * 146 * In general, this is how things should be ordered in each vnode op: 147 * 148 * ZFS_ENTER(zfsvfs); // exit if unmounted 149 * top: 150 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 151 * rw_enter(...); // grab any other locks you need 152 * tx = dmu_tx_create(...); // get DMU tx 153 * dmu_tx_hold_*(); // hold each object you might modify 154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 155 * if (error) { 156 * rw_exit(...); // drop locks 157 * zfs_dirent_unlock(dl); // unlock directory entry 158 * VN_RELE(...); // release held vnodes 159 * if (error == ERESTART) { 160 * waited = B_TRUE; 161 * dmu_tx_wait(tx); 162 * dmu_tx_abort(tx); 163 * goto top; 164 * } 165 * dmu_tx_abort(tx); // abort DMU tx 166 * ZFS_EXIT(zfsvfs); // finished in zfs 167 * return (error); // really out of space 168 * } 169 * error = do_real_work(); // do whatever this VOP does 170 * if (error == 0) 171 * zfs_log_*(...); // on success, make ZIL entry 172 * dmu_tx_commit(tx); // commit DMU tx -- error or not 173 * rw_exit(...); // drop locks 174 * zfs_dirent_unlock(dl); // unlock directory entry 175 * VN_RELE(...); // release held vnodes 176 * zil_commit(zilog, foid); // synchronous when necessary 177 * ZFS_EXIT(zfsvfs); // finished in zfs 178 * return (error); // done, report error 179 */ 180 181/* ARGSUSED */ 182static int 183zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 184{ 185 znode_t *zp = VTOZ(*vpp); 186 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 187 188 ZFS_ENTER(zfsvfs); 189 ZFS_VERIFY_ZP(zp); 190 191 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) && 192 ((flag & FAPPEND) == 0)) { 193 ZFS_EXIT(zfsvfs); 194 return (SET_ERROR(EPERM)); 195 } 196 197 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 198 ZTOV(zp)->v_type == VREG && 199 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) { 200 if (fs_vscan(*vpp, cr, 0) != 0) { 201 ZFS_EXIT(zfsvfs); 202 return (SET_ERROR(EACCES)); 203 } 204 } 205 206 /* Keep a count of the synchronous opens in the znode */ 207 if (flag & (FSYNC | FDSYNC)) 208 atomic_inc_32(&zp->z_sync_cnt); 209 210 ZFS_EXIT(zfsvfs); 211 return (0); 212} 213 214/* ARGSUSED */ 215static int 216zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 217 caller_context_t *ct) 218{ 219 znode_t *zp = VTOZ(vp); 220 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 221 222 /* 223 * Clean up any locks held by this process on the vp. 224 */ 225 cleanlocks(vp, ddi_get_pid(), 0); 226 cleanshares(vp, ddi_get_pid()); 227 228 ZFS_ENTER(zfsvfs); 229 ZFS_VERIFY_ZP(zp); 230 231 /* Decrement the synchronous opens in the znode */ 232 if ((flag & (FSYNC | FDSYNC)) && (count == 1)) 233 atomic_dec_32(&zp->z_sync_cnt); 234 235 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 236 ZTOV(zp)->v_type == VREG && 237 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) 238 VERIFY(fs_vscan(vp, cr, 1) == 0); 239 240 ZFS_EXIT(zfsvfs); 241 return (0); 242} 243 244/* 245 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 246 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 247 */ 248static int 249zfs_holey(vnode_t *vp, u_long cmd, offset_t *off) 250{ 251 znode_t *zp = VTOZ(vp); 252 uint64_t noff = (uint64_t)*off; /* new offset */ 253 uint64_t file_sz; 254 int error; 255 boolean_t hole; 256 257 file_sz = zp->z_size; 258 if (noff >= file_sz) { 259 return (SET_ERROR(ENXIO)); 260 } 261 262 if (cmd == _FIO_SEEK_HOLE) 263 hole = B_TRUE; 264 else 265 hole = B_FALSE; 266 267 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 268 269 /* end of file? */ 270 if ((error == ESRCH) || (noff > file_sz)) { 271 /* 272 * Handle the virtual hole at the end of file. 273 */ 274 if (hole) { 275 *off = file_sz; 276 return (0); 277 } 278 return (SET_ERROR(ENXIO)); 279 } 280 281 if (noff < *off) 282 return (error); 283 *off = noff; 284 return (error); 285} 286 287/* ARGSUSED */ 288static int 289zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred, 290 int *rvalp, caller_context_t *ct) 291{ 292 offset_t off; 293 int error; 294 zfsvfs_t *zfsvfs; 295 znode_t *zp; 296 297 switch (com) { 298 case _FIOFFS: 299 return (0); 300 301 /* 302 * The following two ioctls are used by bfu. Faking out, 303 * necessary to avoid bfu errors. 304 */ 305 case _FIOGDIO: 306 case _FIOSDIO: 307 return (0); 308 309 case _FIO_SEEK_DATA: 310 case _FIO_SEEK_HOLE: 311#ifdef sun 312 if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 313 return (SET_ERROR(EFAULT)); 314#else 315 off = *(offset_t *)data; 316#endif 317 zp = VTOZ(vp); 318 zfsvfs = zp->z_zfsvfs; 319 ZFS_ENTER(zfsvfs); 320 ZFS_VERIFY_ZP(zp); 321 322 /* offset parameter is in/out */ 323 error = zfs_holey(vp, com, &off); 324 ZFS_EXIT(zfsvfs); 325 if (error) 326 return (error); 327#ifdef sun 328 if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 329 return (SET_ERROR(EFAULT)); 330#else 331 *(offset_t *)data = off; 332#endif 333 return (0); 334 } 335 return (SET_ERROR(ENOTTY)); 336} 337 338static vm_page_t 339page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes) 340{ 341 vm_object_t obj; 342 vm_page_t pp; 343 int64_t end; 344 345 /* 346 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE 347 * aligned boundaries, if the range is not aligned. As a result a 348 * DEV_BSIZE subrange with partially dirty data may get marked as clean. 349 * It may happen that all DEV_BSIZE subranges are marked clean and thus 350 * the whole page would be considred clean despite have some dirty data. 351 * For this reason we should shrink the range to DEV_BSIZE aligned 352 * boundaries before calling vm_page_clear_dirty. 353 */ 354 end = rounddown2(off + nbytes, DEV_BSIZE); 355 off = roundup2(off, DEV_BSIZE); 356 nbytes = end - off; 357 358 obj = vp->v_object; 359 zfs_vmobject_assert_wlocked(obj); 360 361 for (;;) { 362 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL && 363 pp->valid) { 364 if (vm_page_xbusied(pp)) { 365 /* 366 * Reference the page before unlocking and 367 * sleeping so that the page daemon is less 368 * likely to reclaim it. 369 */ 370 vm_page_reference(pp); 371 vm_page_lock(pp); 372 zfs_vmobject_wunlock(obj); 373 vm_page_busy_sleep(pp, "zfsmwb"); 374 zfs_vmobject_wlock(obj); 375 continue; 376 } 377 vm_page_sbusy(pp); 378 } else if (pp == NULL) { 379 pp = vm_page_alloc(obj, OFF_TO_IDX(start), 380 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED | 381 VM_ALLOC_SBUSY); 382 } else { 383 ASSERT(pp != NULL && !pp->valid); 384 pp = NULL; 385 } 386 387 if (pp != NULL) { 388 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 389 vm_object_pip_add(obj, 1); 390 pmap_remove_write(pp); 391 if (nbytes != 0) 392 vm_page_clear_dirty(pp, off, nbytes); 393 } 394 break; 395 } 396 return (pp); 397} 398 399static void 400page_unbusy(vm_page_t pp) 401{ 402 403 vm_page_sunbusy(pp); 404 vm_object_pip_subtract(pp->object, 1); 405} 406 407static vm_page_t 408page_hold(vnode_t *vp, int64_t start) 409{ 410 vm_object_t obj; 411 vm_page_t pp; 412 413 obj = vp->v_object; 414 zfs_vmobject_assert_wlocked(obj); 415 416 for (;;) { 417 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL && 418 pp->valid) { 419 if (vm_page_xbusied(pp)) { 420 /* 421 * Reference the page before unlocking and 422 * sleeping so that the page daemon is less 423 * likely to reclaim it. 424 */ 425 vm_page_reference(pp); 426 vm_page_lock(pp); 427 zfs_vmobject_wunlock(obj); 428 vm_page_busy_sleep(pp, "zfsmwb"); 429 zfs_vmobject_wlock(obj); 430 continue; 431 } 432 433 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 434 vm_page_lock(pp); 435 vm_page_hold(pp); 436 vm_page_unlock(pp); 437 438 } else 439 pp = NULL; 440 break; 441 } 442 return (pp); 443} 444 445static void 446page_unhold(vm_page_t pp) 447{ 448 449 vm_page_lock(pp); 450 vm_page_unhold(pp); 451 vm_page_unlock(pp); 452} 453 454/* 455 * When a file is memory mapped, we must keep the IO data synchronized 456 * between the DMU cache and the memory mapped pages. What this means: 457 * 458 * On Write: If we find a memory mapped page, we write to *both* 459 * the page and the dmu buffer. 460 */ 461static void 462update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid, 463 int segflg, dmu_tx_t *tx) 464{ 465 vm_object_t obj; 466 struct sf_buf *sf; 467 caddr_t va; 468 int off; 469 470 ASSERT(segflg != UIO_NOCOPY); 471 ASSERT(vp->v_mount != NULL); 472 obj = vp->v_object; 473 ASSERT(obj != NULL); 474 475 off = start & PAGEOFFSET; 476 zfs_vmobject_wlock(obj); 477 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 478 vm_page_t pp; 479 int nbytes = imin(PAGESIZE - off, len); 480 481 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) { 482 zfs_vmobject_wunlock(obj); 483 484 va = zfs_map_page(pp, &sf); 485 (void) dmu_read(os, oid, start+off, nbytes, 486 va+off, DMU_READ_PREFETCH);; 487 zfs_unmap_page(sf); 488 489 zfs_vmobject_wlock(obj); 490 page_unbusy(pp); 491 } 492 len -= nbytes; 493 off = 0; 494 } 495 vm_object_pip_wakeupn(obj, 0); 496 zfs_vmobject_wunlock(obj); 497} 498 499/* 500 * Read with UIO_NOCOPY flag means that sendfile(2) requests 501 * ZFS to populate a range of page cache pages with data. 502 * 503 * NOTE: this function could be optimized to pre-allocate 504 * all pages in advance, drain exclusive busy on all of them, 505 * map them into contiguous KVA region and populate them 506 * in one single dmu_read() call. 507 */ 508static int 509mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio) 510{ 511 znode_t *zp = VTOZ(vp); 512 objset_t *os = zp->z_zfsvfs->z_os; 513 struct sf_buf *sf; 514 vm_object_t obj; 515 vm_page_t pp; 516 int64_t start; 517 caddr_t va; 518 int len = nbytes; 519 int off; 520 int error = 0; 521 522 ASSERT(uio->uio_segflg == UIO_NOCOPY); 523 ASSERT(vp->v_mount != NULL); 524 obj = vp->v_object; 525 ASSERT(obj != NULL); 526 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0); 527 528 zfs_vmobject_wlock(obj); 529 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) { 530 int bytes = MIN(PAGESIZE, len); 531 532 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY | 533 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY); 534 if (pp->valid == 0) { 535 zfs_vmobject_wunlock(obj); 536 va = zfs_map_page(pp, &sf); 537 error = dmu_read(os, zp->z_id, start, bytes, va, 538 DMU_READ_PREFETCH); 539 if (bytes != PAGESIZE && error == 0) 540 bzero(va + bytes, PAGESIZE - bytes); 541 zfs_unmap_page(sf); 542 zfs_vmobject_wlock(obj); 543 vm_page_sunbusy(pp); 544 vm_page_lock(pp); 545 if (error) { 546 if (pp->wire_count == 0 && pp->valid == 0 && 547 !vm_page_busied(pp)) 548 vm_page_free(pp); 549 } else { 550 pp->valid = VM_PAGE_BITS_ALL; 551 vm_page_activate(pp); 552 } 553 vm_page_unlock(pp); 554 } else { 555 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 556 vm_page_sunbusy(pp); 557 } 558 if (error) 559 break; 560 uio->uio_resid -= bytes; 561 uio->uio_offset += bytes; 562 len -= bytes; 563 } 564 zfs_vmobject_wunlock(obj); 565 return (error); 566} 567 568/* 569 * When a file is memory mapped, we must keep the IO data synchronized 570 * between the DMU cache and the memory mapped pages. What this means: 571 * 572 * On Read: We "read" preferentially from memory mapped pages, 573 * else we default from the dmu buffer. 574 * 575 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 576 * the file is memory mapped. 577 */ 578static int 579mappedread(vnode_t *vp, int nbytes, uio_t *uio) 580{ 581 znode_t *zp = VTOZ(vp); 582 objset_t *os = zp->z_zfsvfs->z_os; 583 vm_object_t obj; 584 int64_t start; 585 caddr_t va; 586 int len = nbytes; 587 int off; 588 int error = 0; 589 590 ASSERT(vp->v_mount != NULL); 591 obj = vp->v_object; 592 ASSERT(obj != NULL); 593 594 start = uio->uio_loffset; 595 off = start & PAGEOFFSET; 596 zfs_vmobject_wlock(obj); 597 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 598 vm_page_t pp; 599 uint64_t bytes = MIN(PAGESIZE - off, len); 600 601 if (pp = page_hold(vp, start)) { 602 struct sf_buf *sf; 603 caddr_t va; 604 605 zfs_vmobject_wunlock(obj); 606 va = zfs_map_page(pp, &sf); 607 error = uiomove(va + off, bytes, UIO_READ, uio); 608 zfs_unmap_page(sf); 609 zfs_vmobject_wlock(obj); 610 page_unhold(pp); 611 } else { 612 zfs_vmobject_wunlock(obj); 613 error = dmu_read_uio(os, zp->z_id, uio, bytes); 614 zfs_vmobject_wlock(obj); 615 } 616 len -= bytes; 617 off = 0; 618 if (error) 619 break; 620 } 621 zfs_vmobject_wunlock(obj); 622 return (error); 623} 624 625offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 626 627/* 628 * Read bytes from specified file into supplied buffer. 629 * 630 * IN: vp - vnode of file to be read from. 631 * uio - structure supplying read location, range info, 632 * and return buffer. 633 * ioflag - SYNC flags; used to provide FRSYNC semantics. 634 * cr - credentials of caller. 635 * ct - caller context 636 * 637 * OUT: uio - updated offset and range, buffer filled. 638 * 639 * RETURN: 0 on success, error code on failure. 640 * 641 * Side Effects: 642 * vp - atime updated if byte count > 0 643 */ 644/* ARGSUSED */ 645static int 646zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 647{ 648 znode_t *zp = VTOZ(vp); 649 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 650 objset_t *os; 651 ssize_t n, nbytes; 652 int error = 0; 653 rl_t *rl; 654 xuio_t *xuio = NULL; 655 656 ZFS_ENTER(zfsvfs); 657 ZFS_VERIFY_ZP(zp); 658 os = zfsvfs->z_os; 659 660 if (zp->z_pflags & ZFS_AV_QUARANTINED) { 661 ZFS_EXIT(zfsvfs); 662 return (SET_ERROR(EACCES)); 663 } 664 665 /* 666 * Validate file offset 667 */ 668 if (uio->uio_loffset < (offset_t)0) { 669 ZFS_EXIT(zfsvfs); 670 return (SET_ERROR(EINVAL)); 671 } 672 673 /* 674 * Fasttrack empty reads 675 */ 676 if (uio->uio_resid == 0) { 677 ZFS_EXIT(zfsvfs); 678 return (0); 679 } 680 681 /* 682 * Check for mandatory locks 683 */ 684 if (MANDMODE(zp->z_mode)) { 685 if (error = chklock(vp, FREAD, 686 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 687 ZFS_EXIT(zfsvfs); 688 return (error); 689 } 690 } 691 692 /* 693 * If we're in FRSYNC mode, sync out this znode before reading it. 694 */ 695 if (zfsvfs->z_log && 696 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)) 697 zil_commit(zfsvfs->z_log, zp->z_id); 698 699 /* 700 * Lock the range against changes. 701 */ 702 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 703 704 /* 705 * If we are reading past end-of-file we can skip 706 * to the end; but we might still need to set atime. 707 */ 708 if (uio->uio_loffset >= zp->z_size) { 709 error = 0; 710 goto out; 711 } 712 713 ASSERT(uio->uio_loffset < zp->z_size); 714 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset); 715 716#ifdef sun 717 if ((uio->uio_extflg == UIO_XUIO) && 718 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) { 719 int nblk; 720 int blksz = zp->z_blksz; 721 uint64_t offset = uio->uio_loffset; 722 723 xuio = (xuio_t *)uio; 724 if ((ISP2(blksz))) { 725 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset, 726 blksz)) / blksz; 727 } else { 728 ASSERT(offset + n <= blksz); 729 nblk = 1; 730 } 731 (void) dmu_xuio_init(xuio, nblk); 732 733 if (vn_has_cached_data(vp)) { 734 /* 735 * For simplicity, we always allocate a full buffer 736 * even if we only expect to read a portion of a block. 737 */ 738 while (--nblk >= 0) { 739 (void) dmu_xuio_add(xuio, 740 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 741 blksz), 0, blksz); 742 } 743 } 744 } 745#endif /* sun */ 746 747 while (n > 0) { 748 nbytes = MIN(n, zfs_read_chunk_size - 749 P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 750 751#ifdef __FreeBSD__ 752 if (uio->uio_segflg == UIO_NOCOPY) 753 error = mappedread_sf(vp, nbytes, uio); 754 else 755#endif /* __FreeBSD__ */ 756 if (vn_has_cached_data(vp)) 757 error = mappedread(vp, nbytes, uio); 758 else 759 error = dmu_read_uio(os, zp->z_id, uio, nbytes); 760 if (error) { 761 /* convert checksum errors into IO errors */ 762 if (error == ECKSUM) 763 error = SET_ERROR(EIO); 764 break; 765 } 766 767 n -= nbytes; 768 } 769out: 770 zfs_range_unlock(rl); 771 772 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 773 ZFS_EXIT(zfsvfs); 774 return (error); 775} 776 777/* 778 * Write the bytes to a file. 779 * 780 * IN: vp - vnode of file to be written to. 781 * uio - structure supplying write location, range info, 782 * and data buffer. 783 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is 784 * set if in append mode. 785 * cr - credentials of caller. 786 * ct - caller context (NFS/CIFS fem monitor only) 787 * 788 * OUT: uio - updated offset and range. 789 * 790 * RETURN: 0 on success, error code on failure. 791 * 792 * Timestamps: 793 * vp - ctime|mtime updated if byte count > 0 794 */ 795 796/* ARGSUSED */ 797static int 798zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 799{ 800 znode_t *zp = VTOZ(vp); 801 rlim64_t limit = MAXOFFSET_T; 802 ssize_t start_resid = uio->uio_resid; 803 ssize_t tx_bytes; 804 uint64_t end_size; 805 dmu_tx_t *tx; 806 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 807 zilog_t *zilog; 808 offset_t woff; 809 ssize_t n, nbytes; 810 rl_t *rl; 811 int max_blksz = zfsvfs->z_max_blksz; 812 int error = 0; 813 arc_buf_t *abuf; 814 iovec_t *aiov = NULL; 815 xuio_t *xuio = NULL; 816 int i_iov = 0; 817 int iovcnt = uio->uio_iovcnt; 818 iovec_t *iovp = uio->uio_iov; 819 int write_eof; 820 int count = 0; 821 sa_bulk_attr_t bulk[4]; 822 uint64_t mtime[2], ctime[2]; 823 824 /* 825 * Fasttrack empty write 826 */ 827 n = start_resid; 828 if (n == 0) 829 return (0); 830 831 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 832 limit = MAXOFFSET_T; 833 834 ZFS_ENTER(zfsvfs); 835 ZFS_VERIFY_ZP(zp); 836 837 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 838 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 839 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 840 &zp->z_size, 8); 841 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 842 &zp->z_pflags, 8); 843 844 /* 845 * If immutable or not appending then return EPERM 846 */ 847 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 848 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 849 (uio->uio_loffset < zp->z_size))) { 850 ZFS_EXIT(zfsvfs); 851 return (SET_ERROR(EPERM)); 852 } 853 854 zilog = zfsvfs->z_log; 855 856 /* 857 * Validate file offset 858 */ 859 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset; 860 if (woff < 0) { 861 ZFS_EXIT(zfsvfs); 862 return (SET_ERROR(EINVAL)); 863 } 864 865 /* 866 * Check for mandatory locks before calling zfs_range_lock() 867 * in order to prevent a deadlock with locks set via fcntl(). 868 */ 869 if (MANDMODE((mode_t)zp->z_mode) && 870 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 871 ZFS_EXIT(zfsvfs); 872 return (error); 873 } 874 875#ifdef sun 876 /* 877 * Pre-fault the pages to ensure slow (eg NFS) pages 878 * don't hold up txg. 879 * Skip this if uio contains loaned arc_buf. 880 */ 881 if ((uio->uio_extflg == UIO_XUIO) && 882 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) 883 xuio = (xuio_t *)uio; 884 else 885 uio_prefaultpages(MIN(n, max_blksz), uio); 886#endif /* sun */ 887 888 /* 889 * If in append mode, set the io offset pointer to eof. 890 */ 891 if (ioflag & FAPPEND) { 892 /* 893 * Obtain an appending range lock to guarantee file append 894 * semantics. We reset the write offset once we have the lock. 895 */ 896 rl = zfs_range_lock(zp, 0, n, RL_APPEND); 897 woff = rl->r_off; 898 if (rl->r_len == UINT64_MAX) { 899 /* 900 * We overlocked the file because this write will cause 901 * the file block size to increase. 902 * Note that zp_size cannot change with this lock held. 903 */ 904 woff = zp->z_size; 905 } 906 uio->uio_loffset = woff; 907 } else { 908 /* 909 * Note that if the file block size will change as a result of 910 * this write, then this range lock will lock the entire file 911 * so that we can re-write the block safely. 912 */ 913 rl = zfs_range_lock(zp, woff, n, RL_WRITER); 914 } 915 916 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) { 917 zfs_range_unlock(rl); 918 ZFS_EXIT(zfsvfs); 919 return (EFBIG); 920 } 921 922 if (woff >= limit) { 923 zfs_range_unlock(rl); 924 ZFS_EXIT(zfsvfs); 925 return (SET_ERROR(EFBIG)); 926 } 927 928 if ((woff + n) > limit || woff > (limit - n)) 929 n = limit - woff; 930 931 /* Will this write extend the file length? */ 932 write_eof = (woff + n > zp->z_size); 933 934 end_size = MAX(zp->z_size, woff + n); 935 936 /* 937 * Write the file in reasonable size chunks. Each chunk is written 938 * in a separate transaction; this keeps the intent log records small 939 * and allows us to do more fine-grained space accounting. 940 */ 941 while (n > 0) { 942 abuf = NULL; 943 woff = uio->uio_loffset; 944 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 945 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 946 if (abuf != NULL) 947 dmu_return_arcbuf(abuf); 948 error = SET_ERROR(EDQUOT); 949 break; 950 } 951 952 if (xuio && abuf == NULL) { 953 ASSERT(i_iov < iovcnt); 954 aiov = &iovp[i_iov]; 955 abuf = dmu_xuio_arcbuf(xuio, i_iov); 956 dmu_xuio_clear(xuio, i_iov); 957 DTRACE_PROBE3(zfs_cp_write, int, i_iov, 958 iovec_t *, aiov, arc_buf_t *, abuf); 959 ASSERT((aiov->iov_base == abuf->b_data) || 960 ((char *)aiov->iov_base - (char *)abuf->b_data + 961 aiov->iov_len == arc_buf_size(abuf))); 962 i_iov++; 963 } else if (abuf == NULL && n >= max_blksz && 964 woff >= zp->z_size && 965 P2PHASE(woff, max_blksz) == 0 && 966 zp->z_blksz == max_blksz) { 967 /* 968 * This write covers a full block. "Borrow" a buffer 969 * from the dmu so that we can fill it before we enter 970 * a transaction. This avoids the possibility of 971 * holding up the transaction if the data copy hangs 972 * up on a pagefault (e.g., from an NFS server mapping). 973 */ 974 size_t cbytes; 975 976 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 977 max_blksz); 978 ASSERT(abuf != NULL); 979 ASSERT(arc_buf_size(abuf) == max_blksz); 980 if (error = uiocopy(abuf->b_data, max_blksz, 981 UIO_WRITE, uio, &cbytes)) { 982 dmu_return_arcbuf(abuf); 983 break; 984 } 985 ASSERT(cbytes == max_blksz); 986 } 987 988 /* 989 * Start a transaction. 990 */ 991 tx = dmu_tx_create(zfsvfs->z_os); 992 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 993 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 994 zfs_sa_upgrade_txholds(tx, zp); 995 error = dmu_tx_assign(tx, TXG_WAIT); 996 if (error) { 997 dmu_tx_abort(tx); 998 if (abuf != NULL) 999 dmu_return_arcbuf(abuf); 1000 break; 1001 } 1002 1003 /* 1004 * If zfs_range_lock() over-locked we grow the blocksize 1005 * and then reduce the lock range. This will only happen 1006 * on the first iteration since zfs_range_reduce() will 1007 * shrink down r_len to the appropriate size. 1008 */ 1009 if (rl->r_len == UINT64_MAX) { 1010 uint64_t new_blksz; 1011 1012 if (zp->z_blksz > max_blksz) { 1013 ASSERT(!ISP2(zp->z_blksz)); 1014 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 1015 } else { 1016 new_blksz = MIN(end_size, max_blksz); 1017 } 1018 zfs_grow_blocksize(zp, new_blksz, tx); 1019 zfs_range_reduce(rl, woff, n); 1020 } 1021 1022 /* 1023 * XXX - should we really limit each write to z_max_blksz? 1024 * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 1025 */ 1026 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 1027 1028 if (woff + nbytes > zp->z_size) 1029 vnode_pager_setsize(vp, woff + nbytes); 1030 1031 if (abuf == NULL) { 1032 tx_bytes = uio->uio_resid; 1033 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl), 1034 uio, nbytes, tx); 1035 tx_bytes -= uio->uio_resid; 1036 } else { 1037 tx_bytes = nbytes; 1038 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len); 1039 /* 1040 * If this is not a full block write, but we are 1041 * extending the file past EOF and this data starts 1042 * block-aligned, use assign_arcbuf(). Otherwise, 1043 * write via dmu_write(). 1044 */ 1045 if (tx_bytes < max_blksz && (!write_eof || 1046 aiov->iov_base != abuf->b_data)) { 1047 ASSERT(xuio); 1048 dmu_write(zfsvfs->z_os, zp->z_id, woff, 1049 aiov->iov_len, aiov->iov_base, tx); 1050 dmu_return_arcbuf(abuf); 1051 xuio_stat_wbuf_copied(); 1052 } else { 1053 ASSERT(xuio || tx_bytes == max_blksz); 1054 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl), 1055 woff, abuf, tx); 1056 } 1057 ASSERT(tx_bytes <= uio->uio_resid); 1058 uioskip(uio, tx_bytes); 1059 } 1060 if (tx_bytes && vn_has_cached_data(vp)) { 1061 update_pages(vp, woff, tx_bytes, zfsvfs->z_os, 1062 zp->z_id, uio->uio_segflg, tx); 1063 } 1064 1065 /* 1066 * If we made no progress, we're done. If we made even 1067 * partial progress, update the znode and ZIL accordingly. 1068 */ 1069 if (tx_bytes == 0) { 1070 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 1071 (void *)&zp->z_size, sizeof (uint64_t), tx); 1072 dmu_tx_commit(tx); 1073 ASSERT(error != 0); 1074 break; 1075 } 1076 1077 /* 1078 * Clear Set-UID/Set-GID bits on successful write if not 1079 * privileged and at least one of the excute bits is set. 1080 * 1081 * It would be nice to to this after all writes have 1082 * been done, but that would still expose the ISUID/ISGID 1083 * to another app after the partial write is committed. 1084 * 1085 * Note: we don't call zfs_fuid_map_id() here because 1086 * user 0 is not an ephemeral uid. 1087 */ 1088 mutex_enter(&zp->z_acl_lock); 1089 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) | 1090 (S_IXUSR >> 6))) != 0 && 1091 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 && 1092 secpolicy_vnode_setid_retain(vp, cr, 1093 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) { 1094 uint64_t newmode; 1095 zp->z_mode &= ~(S_ISUID | S_ISGID); 1096 newmode = zp->z_mode; 1097 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), 1098 (void *)&newmode, sizeof (uint64_t), tx); 1099 } 1100 mutex_exit(&zp->z_acl_lock); 1101 1102 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 1103 B_TRUE); 1104 1105 /* 1106 * Update the file size (zp_size) if it has changed; 1107 * account for possible concurrent updates. 1108 */ 1109 while ((end_size = zp->z_size) < uio->uio_loffset) { 1110 (void) atomic_cas_64(&zp->z_size, end_size, 1111 uio->uio_loffset); 1112 ASSERT(error == 0); 1113 } 1114 /* 1115 * If we are replaying and eof is non zero then force 1116 * the file size to the specified eof. Note, there's no 1117 * concurrency during replay. 1118 */ 1119 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0) 1120 zp->z_size = zfsvfs->z_replay_eof; 1121 1122 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 1123 1124 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 1125 dmu_tx_commit(tx); 1126 1127 if (error != 0) 1128 break; 1129 ASSERT(tx_bytes == nbytes); 1130 n -= nbytes; 1131 1132#ifdef sun 1133 if (!xuio && n > 0) 1134 uio_prefaultpages(MIN(n, max_blksz), uio); 1135#endif /* sun */ 1136 } 1137 1138 zfs_range_unlock(rl); 1139 1140 /* 1141 * If we're in replay mode, or we made no progress, return error. 1142 * Otherwise, it's at least a partial write, so it's successful. 1143 */ 1144 if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 1145 ZFS_EXIT(zfsvfs); 1146 return (error); 1147 } 1148 1149 if (ioflag & (FSYNC | FDSYNC) || 1150 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1151 zil_commit(zilog, zp->z_id); 1152 1153 ZFS_EXIT(zfsvfs); 1154 return (0); 1155} 1156 1157void 1158zfs_get_done(zgd_t *zgd, int error) 1159{ 1160 znode_t *zp = zgd->zgd_private; 1161 objset_t *os = zp->z_zfsvfs->z_os; 1162 1163 if (zgd->zgd_db) 1164 dmu_buf_rele(zgd->zgd_db, zgd); 1165 1166 zfs_range_unlock(zgd->zgd_rl); 1167 1168 /* 1169 * Release the vnode asynchronously as we currently have the 1170 * txg stopped from syncing. 1171 */ 1172 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 1173 1174 if (error == 0 && zgd->zgd_bp) 1175 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 1176 1177 kmem_free(zgd, sizeof (zgd_t)); 1178} 1179 1180#ifdef DEBUG 1181static int zil_fault_io = 0; 1182#endif 1183 1184/* 1185 * Get data to generate a TX_WRITE intent log record. 1186 */ 1187int 1188zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 1189{ 1190 zfsvfs_t *zfsvfs = arg; 1191 objset_t *os = zfsvfs->z_os; 1192 znode_t *zp; 1193 uint64_t object = lr->lr_foid; 1194 uint64_t offset = lr->lr_offset; 1195 uint64_t size = lr->lr_length; 1196 blkptr_t *bp = &lr->lr_blkptr; 1197 dmu_buf_t *db; 1198 zgd_t *zgd; 1199 int error = 0; 1200 1201 ASSERT(zio != NULL); 1202 ASSERT(size != 0); 1203 1204 /* 1205 * Nothing to do if the file has been removed 1206 */ 1207 if (zfs_zget(zfsvfs, object, &zp) != 0) 1208 return (SET_ERROR(ENOENT)); 1209 if (zp->z_unlinked) { 1210 /* 1211 * Release the vnode asynchronously as we currently have the 1212 * txg stopped from syncing. 1213 */ 1214 VN_RELE_ASYNC(ZTOV(zp), 1215 dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 1216 return (SET_ERROR(ENOENT)); 1217 } 1218 1219 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 1220 zgd->zgd_zilog = zfsvfs->z_log; 1221 zgd->zgd_private = zp; 1222 1223 /* 1224 * Write records come in two flavors: immediate and indirect. 1225 * For small writes it's cheaper to store the data with the 1226 * log record (immediate); for large writes it's cheaper to 1227 * sync the data and get a pointer to it (indirect) so that 1228 * we don't have to write the data twice. 1229 */ 1230 if (buf != NULL) { /* immediate write */ 1231 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER); 1232 /* test for truncation needs to be done while range locked */ 1233 if (offset >= zp->z_size) { 1234 error = SET_ERROR(ENOENT); 1235 } else { 1236 error = dmu_read(os, object, offset, size, buf, 1237 DMU_READ_NO_PREFETCH); 1238 } 1239 ASSERT(error == 0 || error == ENOENT); 1240 } else { /* indirect write */ 1241 /* 1242 * Have to lock the whole block to ensure when it's 1243 * written out and it's checksum is being calculated 1244 * that no one can change the data. We need to re-check 1245 * blocksize after we get the lock in case it's changed! 1246 */ 1247 for (;;) { 1248 uint64_t blkoff; 1249 size = zp->z_blksz; 1250 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 1251 offset -= blkoff; 1252 zgd->zgd_rl = zfs_range_lock(zp, offset, size, 1253 RL_READER); 1254 if (zp->z_blksz == size) 1255 break; 1256 offset += blkoff; 1257 zfs_range_unlock(zgd->zgd_rl); 1258 } 1259 /* test for truncation needs to be done while range locked */ 1260 if (lr->lr_offset >= zp->z_size) 1261 error = SET_ERROR(ENOENT); 1262#ifdef DEBUG 1263 if (zil_fault_io) { 1264 error = SET_ERROR(EIO); 1265 zil_fault_io = 0; 1266 } 1267#endif 1268 if (error == 0) 1269 error = dmu_buf_hold(os, object, offset, zgd, &db, 1270 DMU_READ_NO_PREFETCH); 1271 1272 if (error == 0) { 1273 blkptr_t *obp = dmu_buf_get_blkptr(db); 1274 if (obp) { 1275 ASSERT(BP_IS_HOLE(bp)); 1276 *bp = *obp; 1277 } 1278 1279 zgd->zgd_db = db; 1280 zgd->zgd_bp = bp; 1281 1282 ASSERT(db->db_offset == offset); 1283 ASSERT(db->db_size == size); 1284 1285 error = dmu_sync(zio, lr->lr_common.lrc_txg, 1286 zfs_get_done, zgd); 1287 ASSERT(error || lr->lr_length <= zp->z_blksz); 1288 1289 /* 1290 * On success, we need to wait for the write I/O 1291 * initiated by dmu_sync() to complete before we can 1292 * release this dbuf. We will finish everything up 1293 * in the zfs_get_done() callback. 1294 */ 1295 if (error == 0) 1296 return (0); 1297 1298 if (error == EALREADY) { 1299 lr->lr_common.lrc_txtype = TX_WRITE2; 1300 error = 0; 1301 } 1302 } 1303 } 1304 1305 zfs_get_done(zgd, error); 1306 1307 return (error); 1308} 1309 1310/*ARGSUSED*/ 1311static int 1312zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 1313 caller_context_t *ct) 1314{ 1315 znode_t *zp = VTOZ(vp); 1316 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1317 int error; 1318 1319 ZFS_ENTER(zfsvfs); 1320 ZFS_VERIFY_ZP(zp); 1321 1322 if (flag & V_ACE_MASK) 1323 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 1324 else 1325 error = zfs_zaccess_rwx(zp, mode, flag, cr); 1326 1327 ZFS_EXIT(zfsvfs); 1328 return (error); 1329} 1330 1331/* 1332 * If vnode is for a device return a specfs vnode instead. 1333 */ 1334static int 1335specvp_check(vnode_t **vpp, cred_t *cr) 1336{ 1337 int error = 0; 1338 1339 if (IS_DEVVP(*vpp)) { 1340 struct vnode *svp; 1341 1342 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1343 VN_RELE(*vpp); 1344 if (svp == NULL) 1345 error = SET_ERROR(ENOSYS); 1346 *vpp = svp; 1347 } 1348 return (error); 1349} 1350 1351 1352/* 1353 * Lookup an entry in a directory, or an extended attribute directory. 1354 * If it exists, return a held vnode reference for it. 1355 * 1356 * IN: dvp - vnode of directory to search. 1357 * nm - name of entry to lookup. 1358 * pnp - full pathname to lookup [UNUSED]. 1359 * flags - LOOKUP_XATTR set if looking for an attribute. 1360 * rdir - root directory vnode [UNUSED]. 1361 * cr - credentials of caller. 1362 * ct - caller context 1363 * direntflags - directory lookup flags 1364 * realpnp - returned pathname. 1365 * 1366 * OUT: vpp - vnode of located entry, NULL if not found. 1367 * 1368 * RETURN: 0 on success, error code on failure. 1369 * 1370 * Timestamps: 1371 * NA 1372 */ 1373/* ARGSUSED */ 1374static int 1375zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp, 1376 int nameiop, cred_t *cr, kthread_t *td, int flags) 1377{ 1378 znode_t *zdp = VTOZ(dvp); 1379 zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 1380 int error = 0; 1381 int *direntflags = NULL; 1382 void *realpnp = NULL; 1383 1384 /* fast path */ 1385 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 1386 1387 if (dvp->v_type != VDIR) { 1388 return (SET_ERROR(ENOTDIR)); 1389 } else if (zdp->z_sa_hdl == NULL) { 1390 return (SET_ERROR(EIO)); 1391 } 1392 1393 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 1394 error = zfs_fastaccesschk_execute(zdp, cr); 1395 if (!error) { 1396 *vpp = dvp; 1397 VN_HOLD(*vpp); 1398 return (0); 1399 } 1400 return (error); 1401 } else { 1402 vnode_t *tvp = dnlc_lookup(dvp, nm); 1403 1404 if (tvp) { 1405 error = zfs_fastaccesschk_execute(zdp, cr); 1406 if (error) { 1407 VN_RELE(tvp); 1408 return (error); 1409 } 1410 if (tvp == DNLC_NO_VNODE) { 1411 VN_RELE(tvp); 1412 return (SET_ERROR(ENOENT)); 1413 } else { 1414 *vpp = tvp; 1415 return (specvp_check(vpp, cr)); 1416 } 1417 } 1418 } 1419 } 1420 1421 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1422 1423 ZFS_ENTER(zfsvfs); 1424 ZFS_VERIFY_ZP(zdp); 1425 1426 *vpp = NULL; 1427 1428 if (flags & LOOKUP_XATTR) { 1429#ifdef TODO 1430 /* 1431 * If the xattr property is off, refuse the lookup request. 1432 */ 1433 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 1434 ZFS_EXIT(zfsvfs); 1435 return (SET_ERROR(EINVAL)); 1436 } 1437#endif 1438 1439 /* 1440 * We don't allow recursive attributes.. 1441 * Maybe someday we will. 1442 */ 1443 if (zdp->z_pflags & ZFS_XATTR) { 1444 ZFS_EXIT(zfsvfs); 1445 return (SET_ERROR(EINVAL)); 1446 } 1447 1448 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1449 ZFS_EXIT(zfsvfs); 1450 return (error); 1451 } 1452 1453 /* 1454 * Do we have permission to get into attribute directory? 1455 */ 1456 1457 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 1458 B_FALSE, cr)) { 1459 VN_RELE(*vpp); 1460 *vpp = NULL; 1461 } 1462 1463 ZFS_EXIT(zfsvfs); 1464 return (error); 1465 } 1466 1467 if (dvp->v_type != VDIR) { 1468 ZFS_EXIT(zfsvfs); 1469 return (SET_ERROR(ENOTDIR)); 1470 } 1471 1472 /* 1473 * Check accessibility of directory. 1474 */ 1475 1476 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1477 ZFS_EXIT(zfsvfs); 1478 return (error); 1479 } 1480 1481 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 1482 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1483 ZFS_EXIT(zfsvfs); 1484 return (SET_ERROR(EILSEQ)); 1485 } 1486 1487 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 1488 if (error == 0) 1489 error = specvp_check(vpp, cr); 1490 1491 /* Translate errors and add SAVENAME when needed. */ 1492 if (cnp->cn_flags & ISLASTCN) { 1493 switch (nameiop) { 1494 case CREATE: 1495 case RENAME: 1496 if (error == ENOENT) { 1497 error = EJUSTRETURN; 1498 cnp->cn_flags |= SAVENAME; 1499 break; 1500 } 1501 /* FALLTHROUGH */ 1502 case DELETE: 1503 if (error == 0) 1504 cnp->cn_flags |= SAVENAME; 1505 break; 1506 } 1507 } 1508 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) { 1509 int ltype = 0; 1510 1511 if (cnp->cn_flags & ISDOTDOT) { 1512 ltype = VOP_ISLOCKED(dvp); 1513 VOP_UNLOCK(dvp, 0); 1514 } 1515 ZFS_EXIT(zfsvfs); 1516 error = vn_lock(*vpp, cnp->cn_lkflags); 1517 if (cnp->cn_flags & ISDOTDOT) 1518 vn_lock(dvp, ltype | LK_RETRY); 1519 if (error != 0) { 1520 VN_RELE(*vpp); 1521 *vpp = NULL; 1522 return (error); 1523 } 1524 } else { 1525 ZFS_EXIT(zfsvfs); 1526 } 1527 1528#ifdef FREEBSD_NAMECACHE 1529 /* 1530 * Insert name into cache (as non-existent) if appropriate. 1531 */ 1532 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 1533 cache_enter(dvp, *vpp, cnp); 1534 /* 1535 * Insert name into cache if appropriate. 1536 */ 1537 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) { 1538 if (!(cnp->cn_flags & ISLASTCN) || 1539 (nameiop != DELETE && nameiop != RENAME)) { 1540 cache_enter(dvp, *vpp, cnp); 1541 } 1542 } 1543#endif 1544 1545 return (error); 1546} 1547 1548/* 1549 * Attempt to create a new entry in a directory. If the entry 1550 * already exists, truncate the file if permissible, else return 1551 * an error. Return the vp of the created or trunc'd file. 1552 * 1553 * IN: dvp - vnode of directory to put new file entry in. 1554 * name - name of new file entry. 1555 * vap - attributes of new file. 1556 * excl - flag indicating exclusive or non-exclusive mode. 1557 * mode - mode to open file with. 1558 * cr - credentials of caller. 1559 * flag - large file flag [UNUSED]. 1560 * ct - caller context 1561 * vsecp - ACL to be set 1562 * 1563 * OUT: vpp - vnode of created or trunc'd entry. 1564 * 1565 * RETURN: 0 on success, error code on failure. 1566 * 1567 * Timestamps: 1568 * dvp - ctime|mtime updated if new entry created 1569 * vp - ctime|mtime always, atime if new 1570 */ 1571 1572/* ARGSUSED */ 1573static int 1574zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode, 1575 vnode_t **vpp, cred_t *cr, kthread_t *td) 1576{ 1577 znode_t *zp, *dzp = VTOZ(dvp); 1578 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1579 zilog_t *zilog; 1580 objset_t *os; 1581 zfs_dirlock_t *dl; 1582 dmu_tx_t *tx; 1583 int error; 1584 ksid_t *ksid; 1585 uid_t uid; 1586 gid_t gid = crgetgid(cr); 1587 zfs_acl_ids_t acl_ids; 1588 boolean_t fuid_dirtied; 1589 boolean_t have_acl = B_FALSE; 1590 boolean_t waited = B_FALSE; 1591 void *vsecp = NULL; 1592 int flag = 0; 1593 1594 /* 1595 * If we have an ephemeral id, ACL, or XVATTR then 1596 * make sure file system is at proper version 1597 */ 1598 1599 ksid = crgetsid(cr, KSID_OWNER); 1600 if (ksid) 1601 uid = ksid_getid(ksid); 1602 else 1603 uid = crgetuid(cr); 1604 1605 if (zfsvfs->z_use_fuids == B_FALSE && 1606 (vsecp || (vap->va_mask & AT_XVATTR) || 1607 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 1608 return (SET_ERROR(EINVAL)); 1609 1610 ZFS_ENTER(zfsvfs); 1611 ZFS_VERIFY_ZP(dzp); 1612 os = zfsvfs->z_os; 1613 zilog = zfsvfs->z_log; 1614 1615 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 1616 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1617 ZFS_EXIT(zfsvfs); 1618 return (SET_ERROR(EILSEQ)); 1619 } 1620 1621 if (vap->va_mask & AT_XVATTR) { 1622 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap, 1623 crgetuid(cr), cr, vap->va_type)) != 0) { 1624 ZFS_EXIT(zfsvfs); 1625 return (error); 1626 } 1627 } 1628top: 1629 *vpp = NULL; 1630 1631 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr)) 1632 vap->va_mode &= ~S_ISVTX; 1633 1634 if (*name == '\0') { 1635 /* 1636 * Null component name refers to the directory itself. 1637 */ 1638 VN_HOLD(dvp); 1639 zp = dzp; 1640 dl = NULL; 1641 error = 0; 1642 } else { 1643 /* possible VN_HOLD(zp) */ 1644 int zflg = 0; 1645 1646 if (flag & FIGNORECASE) 1647 zflg |= ZCILOOK; 1648 1649 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 1650 NULL, NULL); 1651 if (error) { 1652 if (have_acl) 1653 zfs_acl_ids_free(&acl_ids); 1654 if (strcmp(name, "..") == 0) 1655 error = SET_ERROR(EISDIR); 1656 ZFS_EXIT(zfsvfs); 1657 return (error); 1658 } 1659 } 1660 1661 if (zp == NULL) { 1662 uint64_t txtype; 1663 1664 /* 1665 * Create a new file object and update the directory 1666 * to reference it. 1667 */ 1668 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1669 if (have_acl) 1670 zfs_acl_ids_free(&acl_ids); 1671 goto out; 1672 } 1673 1674 /* 1675 * We only support the creation of regular files in 1676 * extended attribute directories. 1677 */ 1678 1679 if ((dzp->z_pflags & ZFS_XATTR) && 1680 (vap->va_type != VREG)) { 1681 if (have_acl) 1682 zfs_acl_ids_free(&acl_ids); 1683 error = SET_ERROR(EINVAL); 1684 goto out; 1685 } 1686 1687 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 1688 cr, vsecp, &acl_ids)) != 0) 1689 goto out; 1690 have_acl = B_TRUE; 1691 1692 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 1693 zfs_acl_ids_free(&acl_ids); 1694 error = SET_ERROR(EDQUOT); 1695 goto out; 1696 } 1697 1698 tx = dmu_tx_create(os); 1699 1700 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 1701 ZFS_SA_BASE_ATTR_SIZE); 1702 1703 fuid_dirtied = zfsvfs->z_fuid_dirty; 1704 if (fuid_dirtied) 1705 zfs_fuid_txhold(zfsvfs, tx); 1706 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1707 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 1708 if (!zfsvfs->z_use_sa && 1709 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1710 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1711 0, acl_ids.z_aclp->z_acl_bytes); 1712 } 1713 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 1714 if (error) { 1715 zfs_dirent_unlock(dl); 1716 if (error == ERESTART) { 1717 waited = B_TRUE; 1718 dmu_tx_wait(tx); 1719 dmu_tx_abort(tx); 1720 goto top; 1721 } 1722 zfs_acl_ids_free(&acl_ids); 1723 dmu_tx_abort(tx); 1724 ZFS_EXIT(zfsvfs); 1725 return (error); 1726 } 1727 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 1728 1729 if (fuid_dirtied) 1730 zfs_fuid_sync(zfsvfs, tx); 1731 1732 (void) zfs_link_create(dl, zp, tx, ZNEW); 1733 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 1734 if (flag & FIGNORECASE) 1735 txtype |= TX_CI; 1736 zfs_log_create(zilog, tx, txtype, dzp, zp, name, 1737 vsecp, acl_ids.z_fuidp, vap); 1738 zfs_acl_ids_free(&acl_ids); 1739 dmu_tx_commit(tx); 1740 } else { 1741 int aflags = (flag & FAPPEND) ? V_APPEND : 0; 1742 1743 if (have_acl) 1744 zfs_acl_ids_free(&acl_ids); 1745 have_acl = B_FALSE; 1746 1747 /* 1748 * A directory entry already exists for this name. 1749 */ 1750 /* 1751 * Can't truncate an existing file if in exclusive mode. 1752 */ 1753 if (excl == EXCL) { 1754 error = SET_ERROR(EEXIST); 1755 goto out; 1756 } 1757 /* 1758 * Can't open a directory for writing. 1759 */ 1760 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1761 error = SET_ERROR(EISDIR); 1762 goto out; 1763 } 1764 /* 1765 * Verify requested access to file. 1766 */ 1767 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1768 goto out; 1769 } 1770 1771 mutex_enter(&dzp->z_lock); 1772 dzp->z_seq++; 1773 mutex_exit(&dzp->z_lock); 1774 1775 /* 1776 * Truncate regular files if requested. 1777 */ 1778 if ((ZTOV(zp)->v_type == VREG) && 1779 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 1780 /* we can't hold any locks when calling zfs_freesp() */ 1781 zfs_dirent_unlock(dl); 1782 dl = NULL; 1783 error = zfs_freesp(zp, 0, 0, mode, TRUE); 1784 if (error == 0) { 1785 vnevent_create(ZTOV(zp), ct); 1786 } 1787 } 1788 } 1789out: 1790 if (dl) 1791 zfs_dirent_unlock(dl); 1792 1793 if (error) { 1794 if (zp) 1795 VN_RELE(ZTOV(zp)); 1796 } else { 1797 *vpp = ZTOV(zp); 1798 error = specvp_check(vpp, cr); 1799 } 1800 1801 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1802 zil_commit(zilog, 0); 1803 1804 ZFS_EXIT(zfsvfs); 1805 return (error); 1806} 1807 1808/* 1809 * Remove an entry from a directory. 1810 * 1811 * IN: dvp - vnode of directory to remove entry from. 1812 * name - name of entry to remove. 1813 * cr - credentials of caller. 1814 * ct - caller context 1815 * flags - case flags 1816 * 1817 * RETURN: 0 on success, error code on failure. 1818 * 1819 * Timestamps: 1820 * dvp - ctime|mtime 1821 * vp - ctime (if nlink > 0) 1822 */ 1823 1824uint64_t null_xattr = 0; 1825 1826/*ARGSUSED*/ 1827static int 1828zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 1829 int flags) 1830{ 1831 znode_t *zp, *dzp = VTOZ(dvp); 1832 znode_t *xzp; 1833 vnode_t *vp; 1834 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1835 zilog_t *zilog; 1836 uint64_t acl_obj, xattr_obj; 1837 uint64_t xattr_obj_unlinked = 0; 1838 uint64_t obj = 0; 1839 zfs_dirlock_t *dl; 1840 dmu_tx_t *tx; 1841 boolean_t may_delete_now, delete_now = FALSE; 1842 boolean_t unlinked, toobig = FALSE; 1843 uint64_t txtype; 1844 pathname_t *realnmp = NULL; 1845 pathname_t realnm; 1846 int error; 1847 int zflg = ZEXISTS; 1848 boolean_t waited = B_FALSE; 1849 1850 ZFS_ENTER(zfsvfs); 1851 ZFS_VERIFY_ZP(dzp); 1852 zilog = zfsvfs->z_log; 1853 1854 if (flags & FIGNORECASE) { 1855 zflg |= ZCILOOK; 1856 pn_alloc(&realnm); 1857 realnmp = &realnm; 1858 } 1859 1860top: 1861 xattr_obj = 0; 1862 xzp = NULL; 1863 /* 1864 * Attempt to lock directory; fail if entry doesn't exist. 1865 */ 1866 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 1867 NULL, realnmp)) { 1868 if (realnmp) 1869 pn_free(realnmp); 1870 ZFS_EXIT(zfsvfs); 1871 return (error); 1872 } 1873 1874 vp = ZTOV(zp); 1875 1876 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1877 goto out; 1878 } 1879 1880 /* 1881 * Need to use rmdir for removing directories. 1882 */ 1883 if (vp->v_type == VDIR) { 1884 error = SET_ERROR(EPERM); 1885 goto out; 1886 } 1887 1888 vnevent_remove(vp, dvp, name, ct); 1889 1890 if (realnmp) 1891 dnlc_remove(dvp, realnmp->pn_buf); 1892 else 1893 dnlc_remove(dvp, name); 1894 1895 VI_LOCK(vp); 1896 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1897 VI_UNLOCK(vp); 1898 1899 /* 1900 * We may delete the znode now, or we may put it in the unlinked set; 1901 * it depends on whether we're the last link, and on whether there are 1902 * other holds on the vnode. So we dmu_tx_hold() the right things to 1903 * allow for either case. 1904 */ 1905 obj = zp->z_id; 1906 tx = dmu_tx_create(zfsvfs->z_os); 1907 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1908 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1909 zfs_sa_upgrade_txholds(tx, zp); 1910 zfs_sa_upgrade_txholds(tx, dzp); 1911 if (may_delete_now) { 1912 toobig = 1913 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 1914 /* if the file is too big, only hold_free a token amount */ 1915 dmu_tx_hold_free(tx, zp->z_id, 0, 1916 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 1917 } 1918 1919 /* are there any extended attributes? */ 1920 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1921 &xattr_obj, sizeof (xattr_obj)); 1922 if (error == 0 && xattr_obj) { 1923 error = zfs_zget(zfsvfs, xattr_obj, &xzp); 1924 ASSERT0(error); 1925 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 1926 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1927 } 1928 1929 mutex_enter(&zp->z_lock); 1930 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) 1931 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1932 mutex_exit(&zp->z_lock); 1933 1934 /* charge as an update -- would be nice not to charge at all */ 1935 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1936 1937 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 1938 if (error) { 1939 zfs_dirent_unlock(dl); 1940 VN_RELE(vp); 1941 if (xzp) 1942 VN_RELE(ZTOV(xzp)); 1943 if (error == ERESTART) { 1944 waited = B_TRUE; 1945 dmu_tx_wait(tx); 1946 dmu_tx_abort(tx); 1947 goto top; 1948 } 1949 if (realnmp) 1950 pn_free(realnmp); 1951 dmu_tx_abort(tx); 1952 ZFS_EXIT(zfsvfs); 1953 return (error); 1954 } 1955 1956 /* 1957 * Remove the directory entry. 1958 */ 1959 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1960 1961 if (error) { 1962 dmu_tx_commit(tx); 1963 goto out; 1964 } 1965 1966 if (unlinked) { 1967 1968 /* 1969 * Hold z_lock so that we can make sure that the ACL obj 1970 * hasn't changed. Could have been deleted due to 1971 * zfs_sa_upgrade(). 1972 */ 1973 mutex_enter(&zp->z_lock); 1974 VI_LOCK(vp); 1975 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1976 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 1977 delete_now = may_delete_now && !toobig && 1978 vp->v_count == 1 && !vn_has_cached_data(vp) && 1979 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == 1980 acl_obj; 1981 VI_UNLOCK(vp); 1982 } 1983 1984 if (delete_now) { 1985#ifdef __FreeBSD__ 1986 panic("zfs_remove: delete_now branch taken"); 1987#endif 1988 if (xattr_obj_unlinked) { 1989 ASSERT3U(xzp->z_links, ==, 2); 1990 mutex_enter(&xzp->z_lock); 1991 xzp->z_unlinked = 1; 1992 xzp->z_links = 0; 1993 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 1994 &xzp->z_links, sizeof (xzp->z_links), tx); 1995 ASSERT3U(error, ==, 0); 1996 mutex_exit(&xzp->z_lock); 1997 zfs_unlinked_add(xzp, tx); 1998 1999 if (zp->z_is_sa) 2000 error = sa_remove(zp->z_sa_hdl, 2001 SA_ZPL_XATTR(zfsvfs), tx); 2002 else 2003 error = sa_update(zp->z_sa_hdl, 2004 SA_ZPL_XATTR(zfsvfs), &null_xattr, 2005 sizeof (uint64_t), tx); 2006 ASSERT0(error); 2007 } 2008 VI_LOCK(vp); 2009 vp->v_count--; 2010 ASSERT0(vp->v_count); 2011 VI_UNLOCK(vp); 2012 mutex_exit(&zp->z_lock); 2013 zfs_znode_delete(zp, tx); 2014 } else if (unlinked) { 2015 mutex_exit(&zp->z_lock); 2016 zfs_unlinked_add(zp, tx); 2017#ifdef __FreeBSD__ 2018 vp->v_vflag |= VV_NOSYNC; 2019#endif 2020 } 2021 2022 txtype = TX_REMOVE; 2023 if (flags & FIGNORECASE) 2024 txtype |= TX_CI; 2025 zfs_log_remove(zilog, tx, txtype, dzp, name, obj); 2026 2027 dmu_tx_commit(tx); 2028out: 2029 if (realnmp) 2030 pn_free(realnmp); 2031 2032 zfs_dirent_unlock(dl); 2033 2034 if (!delete_now) 2035 VN_RELE(vp); 2036 if (xzp) 2037 VN_RELE(ZTOV(xzp)); 2038 2039 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2040 zil_commit(zilog, 0); 2041 2042 ZFS_EXIT(zfsvfs); 2043 return (error); 2044} 2045 2046/* 2047 * Create a new directory and insert it into dvp using the name 2048 * provided. Return a pointer to the inserted directory. 2049 * 2050 * IN: dvp - vnode of directory to add subdir to. 2051 * dirname - name of new directory. 2052 * vap - attributes of new directory. 2053 * cr - credentials of caller. 2054 * ct - caller context 2055 * flags - case flags 2056 * vsecp - ACL to be set 2057 * 2058 * OUT: vpp - vnode of created directory. 2059 * 2060 * RETURN: 0 on success, error code on failure. 2061 * 2062 * Timestamps: 2063 * dvp - ctime|mtime updated 2064 * vp - ctime|mtime|atime updated 2065 */ 2066/*ARGSUSED*/ 2067static int 2068zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 2069 caller_context_t *ct, int flags, vsecattr_t *vsecp) 2070{ 2071 znode_t *zp, *dzp = VTOZ(dvp); 2072 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2073 zilog_t *zilog; 2074 zfs_dirlock_t *dl; 2075 uint64_t txtype; 2076 dmu_tx_t *tx; 2077 int error; 2078 int zf = ZNEW; 2079 ksid_t *ksid; 2080 uid_t uid; 2081 gid_t gid = crgetgid(cr); 2082 zfs_acl_ids_t acl_ids; 2083 boolean_t fuid_dirtied; 2084 boolean_t waited = B_FALSE; 2085 2086 ASSERT(vap->va_type == VDIR); 2087 2088 /* 2089 * If we have an ephemeral id, ACL, or XVATTR then 2090 * make sure file system is at proper version 2091 */ 2092 2093 ksid = crgetsid(cr, KSID_OWNER); 2094 if (ksid) 2095 uid = ksid_getid(ksid); 2096 else 2097 uid = crgetuid(cr); 2098 if (zfsvfs->z_use_fuids == B_FALSE && 2099 (vsecp || (vap->va_mask & AT_XVATTR) || 2100 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 2101 return (SET_ERROR(EINVAL)); 2102 2103 ZFS_ENTER(zfsvfs); 2104 ZFS_VERIFY_ZP(dzp); 2105 zilog = zfsvfs->z_log; 2106 2107 if (dzp->z_pflags & ZFS_XATTR) { 2108 ZFS_EXIT(zfsvfs); 2109 return (SET_ERROR(EINVAL)); 2110 } 2111 2112 if (zfsvfs->z_utf8 && u8_validate(dirname, 2113 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 2114 ZFS_EXIT(zfsvfs); 2115 return (SET_ERROR(EILSEQ)); 2116 } 2117 if (flags & FIGNORECASE) 2118 zf |= ZCILOOK; 2119 2120 if (vap->va_mask & AT_XVATTR) { 2121 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap, 2122 crgetuid(cr), cr, vap->va_type)) != 0) { 2123 ZFS_EXIT(zfsvfs); 2124 return (error); 2125 } 2126 } 2127 2128 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 2129 vsecp, &acl_ids)) != 0) { 2130 ZFS_EXIT(zfsvfs); 2131 return (error); 2132 } 2133 /* 2134 * First make sure the new directory doesn't exist. 2135 * 2136 * Existence is checked first to make sure we don't return 2137 * EACCES instead of EEXIST which can cause some applications 2138 * to fail. 2139 */ 2140top: 2141 *vpp = NULL; 2142 2143 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 2144 NULL, NULL)) { 2145 zfs_acl_ids_free(&acl_ids); 2146 ZFS_EXIT(zfsvfs); 2147 return (error); 2148 } 2149 2150 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 2151 zfs_acl_ids_free(&acl_ids); 2152 zfs_dirent_unlock(dl); 2153 ZFS_EXIT(zfsvfs); 2154 return (error); 2155 } 2156 2157 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 2158 zfs_acl_ids_free(&acl_ids); 2159 zfs_dirent_unlock(dl); 2160 ZFS_EXIT(zfsvfs); 2161 return (SET_ERROR(EDQUOT)); 2162 } 2163 2164 /* 2165 * Add a new entry to the directory. 2166 */ 2167 tx = dmu_tx_create(zfsvfs->z_os); 2168 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 2169 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 2170 fuid_dirtied = zfsvfs->z_fuid_dirty; 2171 if (fuid_dirtied) 2172 zfs_fuid_txhold(zfsvfs, tx); 2173 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 2174 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 2175 acl_ids.z_aclp->z_acl_bytes); 2176 } 2177 2178 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 2179 ZFS_SA_BASE_ATTR_SIZE); 2180 2181 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 2182 if (error) { 2183 zfs_dirent_unlock(dl); 2184 if (error == ERESTART) { 2185 waited = B_TRUE; 2186 dmu_tx_wait(tx); 2187 dmu_tx_abort(tx); 2188 goto top; 2189 } 2190 zfs_acl_ids_free(&acl_ids); 2191 dmu_tx_abort(tx); 2192 ZFS_EXIT(zfsvfs); 2193 return (error); 2194 } 2195 2196 /* 2197 * Create new node. 2198 */ 2199 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 2200 2201 if (fuid_dirtied) 2202 zfs_fuid_sync(zfsvfs, tx); 2203 2204 /* 2205 * Now put new name in parent dir. 2206 */ 2207 (void) zfs_link_create(dl, zp, tx, ZNEW); 2208 2209 *vpp = ZTOV(zp); 2210 2211 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 2212 if (flags & FIGNORECASE) 2213 txtype |= TX_CI; 2214 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 2215 acl_ids.z_fuidp, vap); 2216 2217 zfs_acl_ids_free(&acl_ids); 2218 2219 dmu_tx_commit(tx); 2220 2221 zfs_dirent_unlock(dl); 2222 2223 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2224 zil_commit(zilog, 0); 2225 2226 ZFS_EXIT(zfsvfs); 2227 return (0); 2228} 2229 2230/* 2231 * Remove a directory subdir entry. If the current working 2232 * directory is the same as the subdir to be removed, the 2233 * remove will fail. 2234 * 2235 * IN: dvp - vnode of directory to remove from. 2236 * name - name of directory to be removed. 2237 * cwd - vnode of current working directory. 2238 * cr - credentials of caller. 2239 * ct - caller context 2240 * flags - case flags 2241 * 2242 * RETURN: 0 on success, error code on failure. 2243 * 2244 * Timestamps: 2245 * dvp - ctime|mtime updated 2246 */ 2247/*ARGSUSED*/ 2248static int 2249zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 2250 caller_context_t *ct, int flags) 2251{ 2252 znode_t *dzp = VTOZ(dvp); 2253 znode_t *zp; 2254 vnode_t *vp; 2255 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2256 zilog_t *zilog; 2257 zfs_dirlock_t *dl; 2258 dmu_tx_t *tx; 2259 int error; 2260 int zflg = ZEXISTS; 2261 boolean_t waited = B_FALSE; 2262 2263 ZFS_ENTER(zfsvfs); 2264 ZFS_VERIFY_ZP(dzp); 2265 zilog = zfsvfs->z_log; 2266 2267 if (flags & FIGNORECASE) 2268 zflg |= ZCILOOK; 2269top: 2270 zp = NULL; 2271 2272 /* 2273 * Attempt to lock directory; fail if entry doesn't exist. 2274 */ 2275 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 2276 NULL, NULL)) { 2277 ZFS_EXIT(zfsvfs); 2278 return (error); 2279 } 2280 2281 vp = ZTOV(zp); 2282 2283 if (error = zfs_zaccess_delete(dzp, zp, cr)) { 2284 goto out; 2285 } 2286 2287 if (vp->v_type != VDIR) { 2288 error = SET_ERROR(ENOTDIR); 2289 goto out; 2290 } 2291 2292 if (vp == cwd) { 2293 error = SET_ERROR(EINVAL); 2294 goto out; 2295 } 2296 2297 vnevent_rmdir(vp, dvp, name, ct); 2298 2299 /* 2300 * Grab a lock on the directory to make sure that noone is 2301 * trying to add (or lookup) entries while we are removing it. 2302 */ 2303 rw_enter(&zp->z_name_lock, RW_WRITER); 2304 2305 /* 2306 * Grab a lock on the parent pointer to make sure we play well 2307 * with the treewalk and directory rename code. 2308 */ 2309 rw_enter(&zp->z_parent_lock, RW_WRITER); 2310 2311 tx = dmu_tx_create(zfsvfs->z_os); 2312 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 2313 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 2314 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 2315 zfs_sa_upgrade_txholds(tx, zp); 2316 zfs_sa_upgrade_txholds(tx, dzp); 2317 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 2318 if (error) { 2319 rw_exit(&zp->z_parent_lock); 2320 rw_exit(&zp->z_name_lock); 2321 zfs_dirent_unlock(dl); 2322 VN_RELE(vp); 2323 if (error == ERESTART) { 2324 waited = B_TRUE; 2325 dmu_tx_wait(tx); 2326 dmu_tx_abort(tx); 2327 goto top; 2328 } 2329 dmu_tx_abort(tx); 2330 ZFS_EXIT(zfsvfs); 2331 return (error); 2332 } 2333 2334#ifdef FREEBSD_NAMECACHE 2335 cache_purge(dvp); 2336#endif 2337 2338 error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 2339 2340 if (error == 0) { 2341 uint64_t txtype = TX_RMDIR; 2342 if (flags & FIGNORECASE) 2343 txtype |= TX_CI; 2344 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); 2345 } 2346 2347 dmu_tx_commit(tx); 2348 2349 rw_exit(&zp->z_parent_lock); 2350 rw_exit(&zp->z_name_lock); 2351#ifdef FREEBSD_NAMECACHE 2352 cache_purge(vp); 2353#endif 2354out: 2355 zfs_dirent_unlock(dl); 2356 2357 VN_RELE(vp); 2358 2359 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2360 zil_commit(zilog, 0); 2361 2362 ZFS_EXIT(zfsvfs); 2363 return (error); 2364} 2365 2366/* 2367 * Read as many directory entries as will fit into the provided 2368 * buffer from the given directory cursor position (specified in 2369 * the uio structure). 2370 * 2371 * IN: vp - vnode of directory to read. 2372 * uio - structure supplying read location, range info, 2373 * and return buffer. 2374 * cr - credentials of caller. 2375 * ct - caller context 2376 * flags - case flags 2377 * 2378 * OUT: uio - updated offset and range, buffer filled. 2379 * eofp - set to true if end-of-file detected. 2380 * 2381 * RETURN: 0 on success, error code on failure. 2382 * 2383 * Timestamps: 2384 * vp - atime updated 2385 * 2386 * Note that the low 4 bits of the cookie returned by zap is always zero. 2387 * This allows us to use the low range for "special" directory entries: 2388 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 2389 * we use the offset 2 for the '.zfs' directory. 2390 */ 2391/* ARGSUSED */ 2392static int 2393zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies) 2394{ 2395 znode_t *zp = VTOZ(vp); 2396 iovec_t *iovp; 2397 edirent_t *eodp; 2398 dirent64_t *odp; 2399 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2400 objset_t *os; 2401 caddr_t outbuf; 2402 size_t bufsize; 2403 zap_cursor_t zc; 2404 zap_attribute_t zap; 2405 uint_t bytes_wanted; 2406 uint64_t offset; /* must be unsigned; checks for < 1 */ 2407 uint64_t parent; 2408 int local_eof; 2409 int outcount; 2410 int error; 2411 uint8_t prefetch; 2412 boolean_t check_sysattrs; 2413 uint8_t type; 2414 int ncooks; 2415 u_long *cooks = NULL; 2416 int flags = 0; 2417 2418 ZFS_ENTER(zfsvfs); 2419 ZFS_VERIFY_ZP(zp); 2420 2421 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 2422 &parent, sizeof (parent))) != 0) { 2423 ZFS_EXIT(zfsvfs); 2424 return (error); 2425 } 2426 2427 /* 2428 * If we are not given an eof variable, 2429 * use a local one. 2430 */ 2431 if (eofp == NULL) 2432 eofp = &local_eof; 2433 2434 /* 2435 * Check for valid iov_len. 2436 */ 2437 if (uio->uio_iov->iov_len <= 0) { 2438 ZFS_EXIT(zfsvfs); 2439 return (SET_ERROR(EINVAL)); 2440 } 2441 2442 /* 2443 * Quit if directory has been removed (posix) 2444 */ 2445 if ((*eofp = zp->z_unlinked) != 0) { 2446 ZFS_EXIT(zfsvfs); 2447 return (0); 2448 } 2449 2450 error = 0; 2451 os = zfsvfs->z_os; 2452 offset = uio->uio_loffset; 2453 prefetch = zp->z_zn_prefetch; 2454 2455 /* 2456 * Initialize the iterator cursor. 2457 */ 2458 if (offset <= 3) { 2459 /* 2460 * Start iteration from the beginning of the directory. 2461 */ 2462 zap_cursor_init(&zc, os, zp->z_id); 2463 } else { 2464 /* 2465 * The offset is a serialized cursor. 2466 */ 2467 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2468 } 2469 2470 /* 2471 * Get space to change directory entries into fs independent format. 2472 */ 2473 iovp = uio->uio_iov; 2474 bytes_wanted = iovp->iov_len; 2475 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2476 bufsize = bytes_wanted; 2477 outbuf = kmem_alloc(bufsize, KM_SLEEP); 2478 odp = (struct dirent64 *)outbuf; 2479 } else { 2480 bufsize = bytes_wanted; 2481 outbuf = NULL; 2482 odp = (struct dirent64 *)iovp->iov_base; 2483 } 2484 eodp = (struct edirent *)odp; 2485 2486 if (ncookies != NULL) { 2487 /* 2488 * Minimum entry size is dirent size and 1 byte for a file name. 2489 */ 2490 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1); 2491 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK); 2492 *cookies = cooks; 2493 *ncookies = ncooks; 2494 } 2495 /* 2496 * If this VFS supports the system attribute view interface; and 2497 * we're looking at an extended attribute directory; and we care 2498 * about normalization conflicts on this vfs; then we must check 2499 * for normalization conflicts with the sysattr name space. 2500 */ 2501#ifdef TODO 2502 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 2503 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 2504 (flags & V_RDDIR_ENTFLAGS); 2505#else 2506 check_sysattrs = 0; 2507#endif 2508 2509 /* 2510 * Transform to file-system independent format 2511 */ 2512 outcount = 0; 2513 while (outcount < bytes_wanted) { 2514 ino64_t objnum; 2515 ushort_t reclen; 2516 off64_t *next = NULL; 2517 2518 /* 2519 * Special case `.', `..', and `.zfs'. 2520 */ 2521 if (offset == 0) { 2522 (void) strcpy(zap.za_name, "."); 2523 zap.za_normalization_conflict = 0; 2524 objnum = zp->z_id; 2525 type = DT_DIR; 2526 } else if (offset == 1) { 2527 (void) strcpy(zap.za_name, ".."); 2528 zap.za_normalization_conflict = 0; 2529 objnum = parent; 2530 type = DT_DIR; 2531 } else if (offset == 2 && zfs_show_ctldir(zp)) { 2532 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 2533 zap.za_normalization_conflict = 0; 2534 objnum = ZFSCTL_INO_ROOT; 2535 type = DT_DIR; 2536 } else { 2537 /* 2538 * Grab next entry. 2539 */ 2540 if (error = zap_cursor_retrieve(&zc, &zap)) { 2541 if ((*eofp = (error == ENOENT)) != 0) 2542 break; 2543 else 2544 goto update; 2545 } 2546 2547 if (zap.za_integer_length != 8 || 2548 zap.za_num_integers != 1) { 2549 cmn_err(CE_WARN, "zap_readdir: bad directory " 2550 "entry, obj = %lld, offset = %lld\n", 2551 (u_longlong_t)zp->z_id, 2552 (u_longlong_t)offset); 2553 error = SET_ERROR(ENXIO); 2554 goto update; 2555 } 2556 2557 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 2558 /* 2559 * MacOS X can extract the object type here such as: 2560 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 2561 */ 2562 type = ZFS_DIRENT_TYPE(zap.za_first_integer); 2563 2564 if (check_sysattrs && !zap.za_normalization_conflict) { 2565#ifdef TODO 2566 zap.za_normalization_conflict = 2567 xattr_sysattr_casechk(zap.za_name); 2568#else 2569 panic("%s:%u: TODO", __func__, __LINE__); 2570#endif 2571 } 2572 } 2573 2574 if (flags & V_RDDIR_ACCFILTER) { 2575 /* 2576 * If we have no access at all, don't include 2577 * this entry in the returned information 2578 */ 2579 znode_t *ezp; 2580 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 2581 goto skip_entry; 2582 if (!zfs_has_access(ezp, cr)) { 2583 VN_RELE(ZTOV(ezp)); 2584 goto skip_entry; 2585 } 2586 VN_RELE(ZTOV(ezp)); 2587 } 2588 2589 if (flags & V_RDDIR_ENTFLAGS) 2590 reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 2591 else 2592 reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2593 2594 /* 2595 * Will this entry fit in the buffer? 2596 */ 2597 if (outcount + reclen > bufsize) { 2598 /* 2599 * Did we manage to fit anything in the buffer? 2600 */ 2601 if (!outcount) { 2602 error = SET_ERROR(EINVAL); 2603 goto update; 2604 } 2605 break; 2606 } 2607 if (flags & V_RDDIR_ENTFLAGS) { 2608 /* 2609 * Add extended flag entry: 2610 */ 2611 eodp->ed_ino = objnum; 2612 eodp->ed_reclen = reclen; 2613 /* NOTE: ed_off is the offset for the *next* entry */ 2614 next = &(eodp->ed_off); 2615 eodp->ed_eflags = zap.za_normalization_conflict ? 2616 ED_CASE_CONFLICT : 0; 2617 (void) strncpy(eodp->ed_name, zap.za_name, 2618 EDIRENT_NAMELEN(reclen)); 2619 eodp = (edirent_t *)((intptr_t)eodp + reclen); 2620 } else { 2621 /* 2622 * Add normal entry: 2623 */ 2624 odp->d_ino = objnum; 2625 odp->d_reclen = reclen; 2626 odp->d_namlen = strlen(zap.za_name); 2627 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1); 2628 odp->d_type = type; 2629 odp = (dirent64_t *)((intptr_t)odp + reclen); 2630 } 2631 outcount += reclen; 2632 2633 ASSERT(outcount <= bufsize); 2634 2635 /* Prefetch znode */ 2636 if (prefetch) 2637 dmu_prefetch(os, objnum, 0, 0); 2638 2639 skip_entry: 2640 /* 2641 * Move to the next entry, fill in the previous offset. 2642 */ 2643 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2644 zap_cursor_advance(&zc); 2645 offset = zap_cursor_serialize(&zc); 2646 } else { 2647 offset += 1; 2648 } 2649 2650 if (cooks != NULL) { 2651 *cooks++ = offset; 2652 ncooks--; 2653 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks)); 2654 } 2655 } 2656 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2657 2658 /* Subtract unused cookies */ 2659 if (ncookies != NULL) 2660 *ncookies -= ncooks; 2661 2662 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2663 iovp->iov_base += outcount; 2664 iovp->iov_len -= outcount; 2665 uio->uio_resid -= outcount; 2666 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2667 /* 2668 * Reset the pointer. 2669 */ 2670 offset = uio->uio_loffset; 2671 } 2672 2673update: 2674 zap_cursor_fini(&zc); 2675 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2676 kmem_free(outbuf, bufsize); 2677 2678 if (error == ENOENT) 2679 error = 0; 2680 2681 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2682 2683 uio->uio_loffset = offset; 2684 ZFS_EXIT(zfsvfs); 2685 if (error != 0 && cookies != NULL) { 2686 free(*cookies, M_TEMP); 2687 *cookies = NULL; 2688 *ncookies = 0; 2689 } 2690 return (error); 2691} 2692 2693ulong_t zfs_fsync_sync_cnt = 4; 2694 2695static int 2696zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2697{ 2698 znode_t *zp = VTOZ(vp); 2699 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2700 2701 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 2702 2703 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 2704 ZFS_ENTER(zfsvfs); 2705 ZFS_VERIFY_ZP(zp); 2706 zil_commit(zfsvfs->z_log, zp->z_id); 2707 ZFS_EXIT(zfsvfs); 2708 } 2709 return (0); 2710} 2711 2712 2713/* 2714 * Get the requested file attributes and place them in the provided 2715 * vattr structure. 2716 * 2717 * IN: vp - vnode of file. 2718 * vap - va_mask identifies requested attributes. 2719 * If AT_XVATTR set, then optional attrs are requested 2720 * flags - ATTR_NOACLCHECK (CIFS server context) 2721 * cr - credentials of caller. 2722 * ct - caller context 2723 * 2724 * OUT: vap - attribute values. 2725 * 2726 * RETURN: 0 (always succeeds). 2727 */ 2728/* ARGSUSED */ 2729static int 2730zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2731 caller_context_t *ct) 2732{ 2733 znode_t *zp = VTOZ(vp); 2734 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2735 int error = 0; 2736 uint32_t blksize; 2737 u_longlong_t nblocks; 2738 uint64_t links; 2739 uint64_t mtime[2], ctime[2], crtime[2], rdev; 2740 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 2741 xoptattr_t *xoap = NULL; 2742 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2743 sa_bulk_attr_t bulk[4]; 2744 int count = 0; 2745 2746 ZFS_ENTER(zfsvfs); 2747 ZFS_VERIFY_ZP(zp); 2748 2749 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2750 2751 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 2752 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 2753 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16); 2754 if (vp->v_type == VBLK || vp->v_type == VCHR) 2755 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, 2756 &rdev, 8); 2757 2758 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 2759 ZFS_EXIT(zfsvfs); 2760 return (error); 2761 } 2762 2763 /* 2764 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 2765 * Also, if we are the owner don't bother, since owner should 2766 * always be allowed to read basic attributes of file. 2767 */ 2768 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && 2769 (vap->va_uid != crgetuid(cr))) { 2770 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 2771 skipaclchk, cr)) { 2772 ZFS_EXIT(zfsvfs); 2773 return (error); 2774 } 2775 } 2776 2777 /* 2778 * Return all attributes. It's cheaper to provide the answer 2779 * than to determine whether we were asked the question. 2780 */ 2781 2782 mutex_enter(&zp->z_lock); 2783 vap->va_type = IFTOVT(zp->z_mode); 2784 vap->va_mode = zp->z_mode & ~S_IFMT; 2785#ifdef sun 2786 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2787#else 2788 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 2789#endif 2790 vap->va_nodeid = zp->z_id; 2791 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 2792 links = zp->z_links + 1; 2793 else 2794 links = zp->z_links; 2795 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */ 2796 vap->va_size = zp->z_size; 2797#ifdef sun 2798 vap->va_rdev = vp->v_rdev; 2799#else 2800 if (vp->v_type == VBLK || vp->v_type == VCHR) 2801 vap->va_rdev = zfs_cmpldev(rdev); 2802#endif 2803 vap->va_seq = zp->z_seq; 2804 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */ 2805 2806 /* 2807 * Add in any requested optional attributes and the create time. 2808 * Also set the corresponding bits in the returned attribute bitmap. 2809 */ 2810 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 2811 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 2812 xoap->xoa_archive = 2813 ((zp->z_pflags & ZFS_ARCHIVE) != 0); 2814 XVA_SET_RTN(xvap, XAT_ARCHIVE); 2815 } 2816 2817 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 2818 xoap->xoa_readonly = 2819 ((zp->z_pflags & ZFS_READONLY) != 0); 2820 XVA_SET_RTN(xvap, XAT_READONLY); 2821 } 2822 2823 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 2824 xoap->xoa_system = 2825 ((zp->z_pflags & ZFS_SYSTEM) != 0); 2826 XVA_SET_RTN(xvap, XAT_SYSTEM); 2827 } 2828 2829 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 2830 xoap->xoa_hidden = 2831 ((zp->z_pflags & ZFS_HIDDEN) != 0); 2832 XVA_SET_RTN(xvap, XAT_HIDDEN); 2833 } 2834 2835 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2836 xoap->xoa_nounlink = 2837 ((zp->z_pflags & ZFS_NOUNLINK) != 0); 2838 XVA_SET_RTN(xvap, XAT_NOUNLINK); 2839 } 2840 2841 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2842 xoap->xoa_immutable = 2843 ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 2844 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 2845 } 2846 2847 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2848 xoap->xoa_appendonly = 2849 ((zp->z_pflags & ZFS_APPENDONLY) != 0); 2850 XVA_SET_RTN(xvap, XAT_APPENDONLY); 2851 } 2852 2853 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2854 xoap->xoa_nodump = 2855 ((zp->z_pflags & ZFS_NODUMP) != 0); 2856 XVA_SET_RTN(xvap, XAT_NODUMP); 2857 } 2858 2859 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 2860 xoap->xoa_opaque = 2861 ((zp->z_pflags & ZFS_OPAQUE) != 0); 2862 XVA_SET_RTN(xvap, XAT_OPAQUE); 2863 } 2864 2865 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 2866 xoap->xoa_av_quarantined = 2867 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 2868 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 2869 } 2870 2871 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2872 xoap->xoa_av_modified = 2873 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 2874 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 2875 } 2876 2877 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 2878 vp->v_type == VREG) { 2879 zfs_sa_get_scanstamp(zp, xvap); 2880 } 2881 2882 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 2883 uint64_t times[2]; 2884 2885 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 2886 times, sizeof (times)); 2887 ZFS_TIME_DECODE(&xoap->xoa_createtime, times); 2888 XVA_SET_RTN(xvap, XAT_CREATETIME); 2889 } 2890 2891 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2892 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 2893 XVA_SET_RTN(xvap, XAT_REPARSE); 2894 } 2895 if (XVA_ISSET_REQ(xvap, XAT_GEN)) { 2896 xoap->xoa_generation = zp->z_gen; 2897 XVA_SET_RTN(xvap, XAT_GEN); 2898 } 2899 2900 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { 2901 xoap->xoa_offline = 2902 ((zp->z_pflags & ZFS_OFFLINE) != 0); 2903 XVA_SET_RTN(xvap, XAT_OFFLINE); 2904 } 2905 2906 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { 2907 xoap->xoa_sparse = 2908 ((zp->z_pflags & ZFS_SPARSE) != 0); 2909 XVA_SET_RTN(xvap, XAT_SPARSE); 2910 } 2911 } 2912 2913 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 2914 ZFS_TIME_DECODE(&vap->va_mtime, mtime); 2915 ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2916 ZFS_TIME_DECODE(&vap->va_birthtime, crtime); 2917 2918 mutex_exit(&zp->z_lock); 2919 2920 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks); 2921 vap->va_blksize = blksize; 2922 vap->va_bytes = nblocks << 9; /* nblocks * 512 */ 2923 2924 if (zp->z_blksz == 0) { 2925 /* 2926 * Block size hasn't been set; suggest maximal I/O transfers. 2927 */ 2928 vap->va_blksize = zfsvfs->z_max_blksz; 2929 } 2930 2931 ZFS_EXIT(zfsvfs); 2932 return (0); 2933} 2934 2935/* 2936 * Set the file attributes to the values contained in the 2937 * vattr structure. 2938 * 2939 * IN: vp - vnode of file to be modified. 2940 * vap - new attribute values. 2941 * If AT_XVATTR set, then optional attrs are being set 2942 * flags - ATTR_UTIME set if non-default time values provided. 2943 * - ATTR_NOACLCHECK (CIFS context only). 2944 * cr - credentials of caller. 2945 * ct - caller context 2946 * 2947 * RETURN: 0 on success, error code on failure. 2948 * 2949 * Timestamps: 2950 * vp - ctime updated, mtime updated if size changed. 2951 */ 2952/* ARGSUSED */ 2953static int 2954zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2955 caller_context_t *ct) 2956{ 2957 znode_t *zp = VTOZ(vp); 2958 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2959 zilog_t *zilog; 2960 dmu_tx_t *tx; 2961 vattr_t oldva; 2962 xvattr_t tmpxvattr; 2963 uint_t mask = vap->va_mask; 2964 uint_t saved_mask = 0; 2965 uint64_t saved_mode; 2966 int trim_mask = 0; 2967 uint64_t new_mode; 2968 uint64_t new_uid, new_gid; 2969 uint64_t xattr_obj; 2970 uint64_t mtime[2], ctime[2]; 2971 znode_t *attrzp; 2972 int need_policy = FALSE; 2973 int err, err2; 2974 zfs_fuid_info_t *fuidp = NULL; 2975 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 2976 xoptattr_t *xoap; 2977 zfs_acl_t *aclp; 2978 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2979 boolean_t fuid_dirtied = B_FALSE; 2980 sa_bulk_attr_t bulk[7], xattr_bulk[7]; 2981 int count = 0, xattr_count = 0; 2982 2983 if (mask == 0) 2984 return (0); 2985 2986 if (mask & AT_NOSET) 2987 return (SET_ERROR(EINVAL)); 2988 2989 ZFS_ENTER(zfsvfs); 2990 ZFS_VERIFY_ZP(zp); 2991 2992 zilog = zfsvfs->z_log; 2993 2994 /* 2995 * Make sure that if we have ephemeral uid/gid or xvattr specified 2996 * that file system is at proper version level 2997 */ 2998 2999 if (zfsvfs->z_use_fuids == B_FALSE && 3000 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 3001 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 3002 (mask & AT_XVATTR))) { 3003 ZFS_EXIT(zfsvfs); 3004 return (SET_ERROR(EINVAL)); 3005 } 3006 3007 if (mask & AT_SIZE && vp->v_type == VDIR) { 3008 ZFS_EXIT(zfsvfs); 3009 return (SET_ERROR(EISDIR)); 3010 } 3011 3012 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 3013 ZFS_EXIT(zfsvfs); 3014 return (SET_ERROR(EINVAL)); 3015 } 3016 3017 /* 3018 * If this is an xvattr_t, then get a pointer to the structure of 3019 * optional attributes. If this is NULL, then we have a vattr_t. 3020 */ 3021 xoap = xva_getxoptattr(xvap); 3022 3023 xva_init(&tmpxvattr); 3024 3025 /* 3026 * Immutable files can only alter immutable bit and atime 3027 */ 3028 if ((zp->z_pflags & ZFS_IMMUTABLE) && 3029 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 3030 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 3031 ZFS_EXIT(zfsvfs); 3032 return (SET_ERROR(EPERM)); 3033 } 3034 3035 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 3036 ZFS_EXIT(zfsvfs); 3037 return (SET_ERROR(EPERM)); 3038 } 3039 3040 /* 3041 * Verify timestamps doesn't overflow 32 bits. 3042 * ZFS can handle large timestamps, but 32bit syscalls can't 3043 * handle times greater than 2039. This check should be removed 3044 * once large timestamps are fully supported. 3045 */ 3046 if (mask & (AT_ATIME | AT_MTIME)) { 3047 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 3048 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 3049 ZFS_EXIT(zfsvfs); 3050 return (SET_ERROR(EOVERFLOW)); 3051 } 3052 } 3053 3054top: 3055 attrzp = NULL; 3056 aclp = NULL; 3057 3058 /* Can this be moved to before the top label? */ 3059 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 3060 ZFS_EXIT(zfsvfs); 3061 return (SET_ERROR(EROFS)); 3062 } 3063 3064 /* 3065 * First validate permissions 3066 */ 3067 3068 if (mask & AT_SIZE) { 3069 /* 3070 * XXX - Note, we are not providing any open 3071 * mode flags here (like FNDELAY), so we may 3072 * block if there are locks present... this 3073 * should be addressed in openat(). 3074 */ 3075 /* XXX - would it be OK to generate a log record here? */ 3076 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 3077 if (err) { 3078 ZFS_EXIT(zfsvfs); 3079 return (err); 3080 } 3081 } 3082 3083 if (mask & (AT_ATIME|AT_MTIME) || 3084 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 3085 XVA_ISSET_REQ(xvap, XAT_READONLY) || 3086 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 3087 XVA_ISSET_REQ(xvap, XAT_OFFLINE) || 3088 XVA_ISSET_REQ(xvap, XAT_SPARSE) || 3089 XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 3090 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 3091 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 3092 skipaclchk, cr); 3093 } 3094 3095 if (mask & (AT_UID|AT_GID)) { 3096 int idmask = (mask & (AT_UID|AT_GID)); 3097 int take_owner; 3098 int take_group; 3099 3100 /* 3101 * NOTE: even if a new mode is being set, 3102 * we may clear S_ISUID/S_ISGID bits. 3103 */ 3104 3105 if (!(mask & AT_MODE)) 3106 vap->va_mode = zp->z_mode; 3107 3108 /* 3109 * Take ownership or chgrp to group we are a member of 3110 */ 3111 3112 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 3113 take_group = (mask & AT_GID) && 3114 zfs_groupmember(zfsvfs, vap->va_gid, cr); 3115 3116 /* 3117 * If both AT_UID and AT_GID are set then take_owner and 3118 * take_group must both be set in order to allow taking 3119 * ownership. 3120 * 3121 * Otherwise, send the check through secpolicy_vnode_setattr() 3122 * 3123 */ 3124 3125 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 3126 ((idmask == AT_UID) && take_owner) || 3127 ((idmask == AT_GID) && take_group)) { 3128 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 3129 skipaclchk, cr) == 0) { 3130 /* 3131 * Remove setuid/setgid for non-privileged users 3132 */ 3133 secpolicy_setid_clear(vap, vp, cr); 3134 trim_mask = (mask & (AT_UID|AT_GID)); 3135 } else { 3136 need_policy = TRUE; 3137 } 3138 } else { 3139 need_policy = TRUE; 3140 } 3141 } 3142 3143 mutex_enter(&zp->z_lock); 3144 oldva.va_mode = zp->z_mode; 3145 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 3146 if (mask & AT_XVATTR) { 3147 /* 3148 * Update xvattr mask to include only those attributes 3149 * that are actually changing. 3150 * 3151 * the bits will be restored prior to actually setting 3152 * the attributes so the caller thinks they were set. 3153 */ 3154 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 3155 if (xoap->xoa_appendonly != 3156 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 3157 need_policy = TRUE; 3158 } else { 3159 XVA_CLR_REQ(xvap, XAT_APPENDONLY); 3160 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 3161 } 3162 } 3163 3164 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 3165 if (xoap->xoa_nounlink != 3166 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 3167 need_policy = TRUE; 3168 } else { 3169 XVA_CLR_REQ(xvap, XAT_NOUNLINK); 3170 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 3171 } 3172 } 3173 3174 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 3175 if (xoap->xoa_immutable != 3176 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 3177 need_policy = TRUE; 3178 } else { 3179 XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 3180 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 3181 } 3182 } 3183 3184 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 3185 if (xoap->xoa_nodump != 3186 ((zp->z_pflags & ZFS_NODUMP) != 0)) { 3187 need_policy = TRUE; 3188 } else { 3189 XVA_CLR_REQ(xvap, XAT_NODUMP); 3190 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 3191 } 3192 } 3193 3194 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 3195 if (xoap->xoa_av_modified != 3196 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 3197 need_policy = TRUE; 3198 } else { 3199 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 3200 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 3201 } 3202 } 3203 3204 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 3205 if ((vp->v_type != VREG && 3206 xoap->xoa_av_quarantined) || 3207 xoap->xoa_av_quarantined != 3208 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 3209 need_policy = TRUE; 3210 } else { 3211 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 3212 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 3213 } 3214 } 3215 3216 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 3217 mutex_exit(&zp->z_lock); 3218 ZFS_EXIT(zfsvfs); 3219 return (SET_ERROR(EPERM)); 3220 } 3221 3222 if (need_policy == FALSE && 3223 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 3224 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 3225 need_policy = TRUE; 3226 } 3227 } 3228 3229 mutex_exit(&zp->z_lock); 3230 3231 if (mask & AT_MODE) { 3232 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 3233 err = secpolicy_setid_setsticky_clear(vp, vap, 3234 &oldva, cr); 3235 if (err) { 3236 ZFS_EXIT(zfsvfs); 3237 return (err); 3238 } 3239 trim_mask |= AT_MODE; 3240 } else { 3241 need_policy = TRUE; 3242 } 3243 } 3244 3245 if (need_policy) { 3246 /* 3247 * If trim_mask is set then take ownership 3248 * has been granted or write_acl is present and user 3249 * has the ability to modify mode. In that case remove 3250 * UID|GID and or MODE from mask so that 3251 * secpolicy_vnode_setattr() doesn't revoke it. 3252 */ 3253 3254 if (trim_mask) { 3255 saved_mask = vap->va_mask; 3256 vap->va_mask &= ~trim_mask; 3257 if (trim_mask & AT_MODE) { 3258 /* 3259 * Save the mode, as secpolicy_vnode_setattr() 3260 * will overwrite it with ova.va_mode. 3261 */ 3262 saved_mode = vap->va_mode; 3263 } 3264 } 3265 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 3266 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 3267 if (err) { 3268 ZFS_EXIT(zfsvfs); 3269 return (err); 3270 } 3271 3272 if (trim_mask) { 3273 vap->va_mask |= saved_mask; 3274 if (trim_mask & AT_MODE) { 3275 /* 3276 * Recover the mode after 3277 * secpolicy_vnode_setattr(). 3278 */ 3279 vap->va_mode = saved_mode; 3280 } 3281 } 3282 } 3283 3284 /* 3285 * secpolicy_vnode_setattr, or take ownership may have 3286 * changed va_mask 3287 */ 3288 mask = vap->va_mask; 3289 3290 if ((mask & (AT_UID | AT_GID))) { 3291 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 3292 &xattr_obj, sizeof (xattr_obj)); 3293 3294 if (err == 0 && xattr_obj) { 3295 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 3296 if (err) 3297 goto out2; 3298 } 3299 if (mask & AT_UID) { 3300 new_uid = zfs_fuid_create(zfsvfs, 3301 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 3302 if (new_uid != zp->z_uid && 3303 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) { 3304 if (attrzp) 3305 VN_RELE(ZTOV(attrzp)); 3306 err = SET_ERROR(EDQUOT); 3307 goto out2; 3308 } 3309 } 3310 3311 if (mask & AT_GID) { 3312 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 3313 cr, ZFS_GROUP, &fuidp); 3314 if (new_gid != zp->z_gid && 3315 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) { 3316 if (attrzp) 3317 VN_RELE(ZTOV(attrzp)); 3318 err = SET_ERROR(EDQUOT); 3319 goto out2; 3320 } 3321 } 3322 } 3323 tx = dmu_tx_create(zfsvfs->z_os); 3324 3325 if (mask & AT_MODE) { 3326 uint64_t pmode = zp->z_mode; 3327 uint64_t acl_obj; 3328 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 3329 3330 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED && 3331 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) { 3332 err = SET_ERROR(EPERM); 3333 goto out; 3334 } 3335 3336 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 3337 goto out; 3338 3339 mutex_enter(&zp->z_lock); 3340 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 3341 /* 3342 * Are we upgrading ACL from old V0 format 3343 * to V1 format? 3344 */ 3345 if (zfsvfs->z_version >= ZPL_VERSION_FUID && 3346 zfs_znode_acl_version(zp) == 3347 ZFS_ACL_VERSION_INITIAL) { 3348 dmu_tx_hold_free(tx, acl_obj, 0, 3349 DMU_OBJECT_END); 3350 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 3351 0, aclp->z_acl_bytes); 3352 } else { 3353 dmu_tx_hold_write(tx, acl_obj, 0, 3354 aclp->z_acl_bytes); 3355 } 3356 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 3357 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 3358 0, aclp->z_acl_bytes); 3359 } 3360 mutex_exit(&zp->z_lock); 3361 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 3362 } else { 3363 if ((mask & AT_XVATTR) && 3364 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 3365 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 3366 else 3367 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 3368 } 3369 3370 if (attrzp) { 3371 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 3372 } 3373 3374 fuid_dirtied = zfsvfs->z_fuid_dirty; 3375 if (fuid_dirtied) 3376 zfs_fuid_txhold(zfsvfs, tx); 3377 3378 zfs_sa_upgrade_txholds(tx, zp); 3379 3380 err = dmu_tx_assign(tx, TXG_WAIT); 3381 if (err) 3382 goto out; 3383 3384 count = 0; 3385 /* 3386 * Set each attribute requested. 3387 * We group settings according to the locks they need to acquire. 3388 * 3389 * Note: you cannot set ctime directly, although it will be 3390 * updated as a side-effect of calling this function. 3391 */ 3392 3393 3394 if (mask & (AT_UID|AT_GID|AT_MODE)) 3395 mutex_enter(&zp->z_acl_lock); 3396 mutex_enter(&zp->z_lock); 3397 3398 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 3399 &zp->z_pflags, sizeof (zp->z_pflags)); 3400 3401 if (attrzp) { 3402 if (mask & (AT_UID|AT_GID|AT_MODE)) 3403 mutex_enter(&attrzp->z_acl_lock); 3404 mutex_enter(&attrzp->z_lock); 3405 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3406 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 3407 sizeof (attrzp->z_pflags)); 3408 } 3409 3410 if (mask & (AT_UID|AT_GID)) { 3411 3412 if (mask & AT_UID) { 3413 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 3414 &new_uid, sizeof (new_uid)); 3415 zp->z_uid = new_uid; 3416 if (attrzp) { 3417 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3418 SA_ZPL_UID(zfsvfs), NULL, &new_uid, 3419 sizeof (new_uid)); 3420 attrzp->z_uid = new_uid; 3421 } 3422 } 3423 3424 if (mask & AT_GID) { 3425 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 3426 NULL, &new_gid, sizeof (new_gid)); 3427 zp->z_gid = new_gid; 3428 if (attrzp) { 3429 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3430 SA_ZPL_GID(zfsvfs), NULL, &new_gid, 3431 sizeof (new_gid)); 3432 attrzp->z_gid = new_gid; 3433 } 3434 } 3435 if (!(mask & AT_MODE)) { 3436 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 3437 NULL, &new_mode, sizeof (new_mode)); 3438 new_mode = zp->z_mode; 3439 } 3440 err = zfs_acl_chown_setattr(zp); 3441 ASSERT(err == 0); 3442 if (attrzp) { 3443 err = zfs_acl_chown_setattr(attrzp); 3444 ASSERT(err == 0); 3445 } 3446 } 3447 3448 if (mask & AT_MODE) { 3449 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 3450 &new_mode, sizeof (new_mode)); 3451 zp->z_mode = new_mode; 3452 ASSERT3U((uintptr_t)aclp, !=, 0); 3453 err = zfs_aclset_common(zp, aclp, cr, tx); 3454 ASSERT0(err); 3455 if (zp->z_acl_cached) 3456 zfs_acl_free(zp->z_acl_cached); 3457 zp->z_acl_cached = aclp; 3458 aclp = NULL; 3459 } 3460 3461 3462 if (mask & AT_ATIME) { 3463 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 3464 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 3465 &zp->z_atime, sizeof (zp->z_atime)); 3466 } 3467 3468 if (mask & AT_MTIME) { 3469 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 3470 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 3471 mtime, sizeof (mtime)); 3472 } 3473 3474 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 3475 if (mask & AT_SIZE && !(mask & AT_MTIME)) { 3476 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 3477 NULL, mtime, sizeof (mtime)); 3478 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 3479 &ctime, sizeof (ctime)); 3480 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 3481 B_TRUE); 3482 } else if (mask != 0) { 3483 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 3484 &ctime, sizeof (ctime)); 3485 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, 3486 B_TRUE); 3487 if (attrzp) { 3488 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 3489 SA_ZPL_CTIME(zfsvfs), NULL, 3490 &ctime, sizeof (ctime)); 3491 zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 3492 mtime, ctime, B_TRUE); 3493 } 3494 } 3495 /* 3496 * Do this after setting timestamps to prevent timestamp 3497 * update from toggling bit 3498 */ 3499 3500 if (xoap && (mask & AT_XVATTR)) { 3501 3502 /* 3503 * restore trimmed off masks 3504 * so that return masks can be set for caller. 3505 */ 3506 3507 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 3508 XVA_SET_REQ(xvap, XAT_APPENDONLY); 3509 } 3510 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 3511 XVA_SET_REQ(xvap, XAT_NOUNLINK); 3512 } 3513 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 3514 XVA_SET_REQ(xvap, XAT_IMMUTABLE); 3515 } 3516 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 3517 XVA_SET_REQ(xvap, XAT_NODUMP); 3518 } 3519 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 3520 XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 3521 } 3522 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 3523 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 3524 } 3525 3526 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 3527 ASSERT(vp->v_type == VREG); 3528 3529 zfs_xvattr_set(zp, xvap, tx); 3530 } 3531 3532 if (fuid_dirtied) 3533 zfs_fuid_sync(zfsvfs, tx); 3534 3535 if (mask != 0) 3536 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 3537 3538 mutex_exit(&zp->z_lock); 3539 if (mask & (AT_UID|AT_GID|AT_MODE)) 3540 mutex_exit(&zp->z_acl_lock); 3541 3542 if (attrzp) { 3543 if (mask & (AT_UID|AT_GID|AT_MODE)) 3544 mutex_exit(&attrzp->z_acl_lock); 3545 mutex_exit(&attrzp->z_lock); 3546 } 3547out: 3548 if (err == 0 && attrzp) { 3549 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 3550 xattr_count, tx); 3551 ASSERT(err2 == 0); 3552 } 3553 3554 if (attrzp) 3555 VN_RELE(ZTOV(attrzp)); 3556 3557 if (aclp) 3558 zfs_acl_free(aclp); 3559 3560 if (fuidp) { 3561 zfs_fuid_info_free(fuidp); 3562 fuidp = NULL; 3563 } 3564 3565 if (err) { 3566 dmu_tx_abort(tx); 3567 if (err == ERESTART) 3568 goto top; 3569 } else { 3570 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 3571 dmu_tx_commit(tx); 3572 } 3573 3574out2: 3575 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3576 zil_commit(zilog, 0); 3577 3578 ZFS_EXIT(zfsvfs); 3579 return (err); 3580} 3581 3582typedef struct zfs_zlock { 3583 krwlock_t *zl_rwlock; /* lock we acquired */ 3584 znode_t *zl_znode; /* znode we held */ 3585 struct zfs_zlock *zl_next; /* next in list */ 3586} zfs_zlock_t; 3587 3588/* 3589 * Drop locks and release vnodes that were held by zfs_rename_lock(). 3590 */ 3591static void 3592zfs_rename_unlock(zfs_zlock_t **zlpp) 3593{ 3594 zfs_zlock_t *zl; 3595 3596 while ((zl = *zlpp) != NULL) { 3597 if (zl->zl_znode != NULL) 3598 VN_RELE(ZTOV(zl->zl_znode)); 3599 rw_exit(zl->zl_rwlock); 3600 *zlpp = zl->zl_next; 3601 kmem_free(zl, sizeof (*zl)); 3602 } 3603} 3604 3605/* 3606 * Search back through the directory tree, using the ".." entries. 3607 * Lock each directory in the chain to prevent concurrent renames. 3608 * Fail any attempt to move a directory into one of its own descendants. 3609 * XXX - z_parent_lock can overlap with map or grow locks 3610 */ 3611static int 3612zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 3613{ 3614 zfs_zlock_t *zl; 3615 znode_t *zp = tdzp; 3616 uint64_t rootid = zp->z_zfsvfs->z_root; 3617 uint64_t oidp = zp->z_id; 3618 krwlock_t *rwlp = &szp->z_parent_lock; 3619 krw_t rw = RW_WRITER; 3620 3621 /* 3622 * First pass write-locks szp and compares to zp->z_id. 3623 * Later passes read-lock zp and compare to zp->z_parent. 3624 */ 3625 do { 3626 if (!rw_tryenter(rwlp, rw)) { 3627 /* 3628 * Another thread is renaming in this path. 3629 * Note that if we are a WRITER, we don't have any 3630 * parent_locks held yet. 3631 */ 3632 if (rw == RW_READER && zp->z_id > szp->z_id) { 3633 /* 3634 * Drop our locks and restart 3635 */ 3636 zfs_rename_unlock(&zl); 3637 *zlpp = NULL; 3638 zp = tdzp; 3639 oidp = zp->z_id; 3640 rwlp = &szp->z_parent_lock; 3641 rw = RW_WRITER; 3642 continue; 3643 } else { 3644 /* 3645 * Wait for other thread to drop its locks 3646 */ 3647 rw_enter(rwlp, rw); 3648 } 3649 } 3650 3651 zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3652 zl->zl_rwlock = rwlp; 3653 zl->zl_znode = NULL; 3654 zl->zl_next = *zlpp; 3655 *zlpp = zl; 3656 3657 if (oidp == szp->z_id) /* We're a descendant of szp */ 3658 return (SET_ERROR(EINVAL)); 3659 3660 if (oidp == rootid) /* We've hit the top */ 3661 return (0); 3662 3663 if (rw == RW_READER) { /* i.e. not the first pass */ 3664 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp); 3665 if (error) 3666 return (error); 3667 zl->zl_znode = zp; 3668 } 3669 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs), 3670 &oidp, sizeof (oidp)); 3671 rwlp = &zp->z_parent_lock; 3672 rw = RW_READER; 3673 3674 } while (zp->z_id != sdzp->z_id); 3675 3676 return (0); 3677} 3678 3679/* 3680 * Move an entry from the provided source directory to the target 3681 * directory. Change the entry name as indicated. 3682 * 3683 * IN: sdvp - Source directory containing the "old entry". 3684 * snm - Old entry name. 3685 * tdvp - Target directory to contain the "new entry". 3686 * tnm - New entry name. 3687 * cr - credentials of caller. 3688 * ct - caller context 3689 * flags - case flags 3690 * 3691 * RETURN: 0 on success, error code on failure. 3692 * 3693 * Timestamps: 3694 * sdvp,tdvp - ctime|mtime updated 3695 */ 3696/*ARGSUSED*/ 3697static int 3698zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 3699 caller_context_t *ct, int flags) 3700{ 3701 znode_t *tdzp, *szp, *tzp; 3702 znode_t *sdzp = VTOZ(sdvp); 3703 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 3704 zilog_t *zilog; 3705 vnode_t *realvp; 3706 zfs_dirlock_t *sdl, *tdl; 3707 dmu_tx_t *tx; 3708 zfs_zlock_t *zl; 3709 int cmp, serr, terr; 3710 int error = 0; 3711 int zflg = 0; 3712 boolean_t waited = B_FALSE; 3713 3714 ZFS_ENTER(zfsvfs); 3715 ZFS_VERIFY_ZP(sdzp); 3716 zilog = zfsvfs->z_log; 3717 3718 /* 3719 * Make sure we have the real vp for the target directory. 3720 */ 3721 if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3722 tdvp = realvp; 3723 3724 tdzp = VTOZ(tdvp); 3725 ZFS_VERIFY_ZP(tdzp); 3726 3727 /* 3728 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the 3729 * ctldir appear to have the same v_vfsp. 3730 */ 3731 if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) { 3732 ZFS_EXIT(zfsvfs); 3733 return (SET_ERROR(EXDEV)); 3734 } 3735 3736 if (zfsvfs->z_utf8 && u8_validate(tnm, 3737 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3738 ZFS_EXIT(zfsvfs); 3739 return (SET_ERROR(EILSEQ)); 3740 } 3741 3742 if (flags & FIGNORECASE) 3743 zflg |= ZCILOOK; 3744 3745top: 3746 szp = NULL; 3747 tzp = NULL; 3748 zl = NULL; 3749 3750 /* 3751 * This is to prevent the creation of links into attribute space 3752 * by renaming a linked file into/outof an attribute directory. 3753 * See the comment in zfs_link() for why this is considered bad. 3754 */ 3755 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 3756 ZFS_EXIT(zfsvfs); 3757 return (SET_ERROR(EINVAL)); 3758 } 3759 3760 /* 3761 * Lock source and target directory entries. To prevent deadlock, 3762 * a lock ordering must be defined. We lock the directory with 3763 * the smallest object id first, or if it's a tie, the one with 3764 * the lexically first name. 3765 */ 3766 if (sdzp->z_id < tdzp->z_id) { 3767 cmp = -1; 3768 } else if (sdzp->z_id > tdzp->z_id) { 3769 cmp = 1; 3770 } else { 3771 /* 3772 * First compare the two name arguments without 3773 * considering any case folding. 3774 */ 3775 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 3776 3777 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 3778 ASSERT(error == 0 || !zfsvfs->z_utf8); 3779 if (cmp == 0) { 3780 /* 3781 * POSIX: "If the old argument and the new argument 3782 * both refer to links to the same existing file, 3783 * the rename() function shall return successfully 3784 * and perform no other action." 3785 */ 3786 ZFS_EXIT(zfsvfs); 3787 return (0); 3788 } 3789 /* 3790 * If the file system is case-folding, then we may 3791 * have some more checking to do. A case-folding file 3792 * system is either supporting mixed case sensitivity 3793 * access or is completely case-insensitive. Note 3794 * that the file system is always case preserving. 3795 * 3796 * In mixed sensitivity mode case sensitive behavior 3797 * is the default. FIGNORECASE must be used to 3798 * explicitly request case insensitive behavior. 3799 * 3800 * If the source and target names provided differ only 3801 * by case (e.g., a request to rename 'tim' to 'Tim'), 3802 * we will treat this as a special case in the 3803 * case-insensitive mode: as long as the source name 3804 * is an exact match, we will allow this to proceed as 3805 * a name-change request. 3806 */ 3807 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 3808 (zfsvfs->z_case == ZFS_CASE_MIXED && 3809 flags & FIGNORECASE)) && 3810 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 3811 &error) == 0) { 3812 /* 3813 * case preserving rename request, require exact 3814 * name matches 3815 */ 3816 zflg |= ZCIEXACT; 3817 zflg &= ~ZCILOOK; 3818 } 3819 } 3820 3821 /* 3822 * If the source and destination directories are the same, we should 3823 * grab the z_name_lock of that directory only once. 3824 */ 3825 if (sdzp == tdzp) { 3826 zflg |= ZHAVELOCK; 3827 rw_enter(&sdzp->z_name_lock, RW_READER); 3828 } 3829 3830 if (cmp < 0) { 3831 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 3832 ZEXISTS | zflg, NULL, NULL); 3833 terr = zfs_dirent_lock(&tdl, 3834 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3835 } else { 3836 terr = zfs_dirent_lock(&tdl, 3837 tdzp, tnm, &tzp, zflg, NULL, NULL); 3838 serr = zfs_dirent_lock(&sdl, 3839 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 3840 NULL, NULL); 3841 } 3842 3843 if (serr) { 3844 /* 3845 * Source entry invalid or not there. 3846 */ 3847 if (!terr) { 3848 zfs_dirent_unlock(tdl); 3849 if (tzp) 3850 VN_RELE(ZTOV(tzp)); 3851 } 3852 3853 if (sdzp == tdzp) 3854 rw_exit(&sdzp->z_name_lock); 3855 3856 /* 3857 * FreeBSD: In OpenSolaris they only check if rename source is 3858 * ".." here, because "." is handled in their lookup. This is 3859 * not the case for FreeBSD, so we check for "." explicitly. 3860 */ 3861 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0) 3862 serr = SET_ERROR(EINVAL); 3863 ZFS_EXIT(zfsvfs); 3864 return (serr); 3865 } 3866 if (terr) { 3867 zfs_dirent_unlock(sdl); 3868 VN_RELE(ZTOV(szp)); 3869 3870 if (sdzp == tdzp) 3871 rw_exit(&sdzp->z_name_lock); 3872 3873 if (strcmp(tnm, "..") == 0) 3874 terr = SET_ERROR(EINVAL); 3875 ZFS_EXIT(zfsvfs); 3876 return (terr); 3877 } 3878 3879 /* 3880 * Must have write access at the source to remove the old entry 3881 * and write access at the target to create the new entry. 3882 * Note that if target and source are the same, this can be 3883 * done in a single check. 3884 */ 3885 3886 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3887 goto out; 3888 3889 if (ZTOV(szp)->v_type == VDIR) { 3890 /* 3891 * Check to make sure rename is valid. 3892 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3893 */ 3894 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3895 goto out; 3896 } 3897 3898 /* 3899 * Does target exist? 3900 */ 3901 if (tzp) { 3902 /* 3903 * Source and target must be the same type. 3904 */ 3905 if (ZTOV(szp)->v_type == VDIR) { 3906 if (ZTOV(tzp)->v_type != VDIR) { 3907 error = SET_ERROR(ENOTDIR); 3908 goto out; 3909 } 3910 } else { 3911 if (ZTOV(tzp)->v_type == VDIR) { 3912 error = SET_ERROR(EISDIR); 3913 goto out; 3914 } 3915 } 3916 /* 3917 * POSIX dictates that when the source and target 3918 * entries refer to the same file object, rename 3919 * must do nothing and exit without error. 3920 */ 3921 if (szp->z_id == tzp->z_id) { 3922 error = 0; 3923 goto out; 3924 } 3925 } 3926 3927 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3928 if (tzp) 3929 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 3930 3931 /* 3932 * notify the target directory if it is not the same 3933 * as source directory. 3934 */ 3935 if (tdvp != sdvp) { 3936 vnevent_rename_dest_dir(tdvp, ct); 3937 } 3938 3939 tx = dmu_tx_create(zfsvfs->z_os); 3940 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 3941 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 3942 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 3943 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 3944 if (sdzp != tdzp) { 3945 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 3946 zfs_sa_upgrade_txholds(tx, tdzp); 3947 } 3948 if (tzp) { 3949 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 3950 zfs_sa_upgrade_txholds(tx, tzp); 3951 } 3952 3953 zfs_sa_upgrade_txholds(tx, szp); 3954 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 3955 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 3956 if (error) { 3957 if (zl != NULL) 3958 zfs_rename_unlock(&zl); 3959 zfs_dirent_unlock(sdl); 3960 zfs_dirent_unlock(tdl); 3961 3962 if (sdzp == tdzp) 3963 rw_exit(&sdzp->z_name_lock); 3964 3965 VN_RELE(ZTOV(szp)); 3966 if (tzp) 3967 VN_RELE(ZTOV(tzp)); 3968 if (error == ERESTART) { 3969 waited = B_TRUE; 3970 dmu_tx_wait(tx); 3971 dmu_tx_abort(tx); 3972 goto top; 3973 } 3974 dmu_tx_abort(tx); 3975 ZFS_EXIT(zfsvfs); 3976 return (error); 3977 } 3978 3979 if (tzp) /* Attempt to remove the existing target */ 3980 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3981 3982 if (error == 0) { 3983 error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3984 if (error == 0) { 3985 szp->z_pflags |= ZFS_AV_MODIFIED; 3986 3987 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 3988 (void *)&szp->z_pflags, sizeof (uint64_t), tx); 3989 ASSERT0(error); 3990 3991 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3992 if (error == 0) { 3993 zfs_log_rename(zilog, tx, TX_RENAME | 3994 (flags & FIGNORECASE ? TX_CI : 0), sdzp, 3995 sdl->dl_name, tdzp, tdl->dl_name, szp); 3996 3997 /* 3998 * Update path information for the target vnode 3999 */ 4000 vn_renamepath(tdvp, ZTOV(szp), tnm, 4001 strlen(tnm)); 4002 } else { 4003 /* 4004 * At this point, we have successfully created 4005 * the target name, but have failed to remove 4006 * the source name. Since the create was done 4007 * with the ZRENAMING flag, there are 4008 * complications; for one, the link count is 4009 * wrong. The easiest way to deal with this 4010 * is to remove the newly created target, and 4011 * return the original error. This must 4012 * succeed; fortunately, it is very unlikely to 4013 * fail, since we just created it. 4014 */ 4015 VERIFY3U(zfs_link_destroy(tdl, szp, tx, 4016 ZRENAMING, NULL), ==, 0); 4017 } 4018 } 4019#ifdef FREEBSD_NAMECACHE 4020 if (error == 0) { 4021 cache_purge(sdvp); 4022 cache_purge(tdvp); 4023 cache_purge(ZTOV(szp)); 4024 if (tzp) 4025 cache_purge(ZTOV(tzp)); 4026 } 4027#endif 4028 } 4029 4030 dmu_tx_commit(tx); 4031out: 4032 if (zl != NULL) 4033 zfs_rename_unlock(&zl); 4034 4035 zfs_dirent_unlock(sdl); 4036 zfs_dirent_unlock(tdl); 4037 4038 if (sdzp == tdzp) 4039 rw_exit(&sdzp->z_name_lock); 4040 4041 4042 VN_RELE(ZTOV(szp)); 4043 if (tzp) 4044 VN_RELE(ZTOV(tzp)); 4045 4046 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4047 zil_commit(zilog, 0); 4048 4049 ZFS_EXIT(zfsvfs); 4050 4051 return (error); 4052} 4053 4054/* 4055 * Insert the indicated symbolic reference entry into the directory. 4056 * 4057 * IN: dvp - Directory to contain new symbolic link. 4058 * link - Name for new symlink entry. 4059 * vap - Attributes of new entry. 4060 * cr - credentials of caller. 4061 * ct - caller context 4062 * flags - case flags 4063 * 4064 * RETURN: 0 on success, error code on failure. 4065 * 4066 * Timestamps: 4067 * dvp - ctime|mtime updated 4068 */ 4069/*ARGSUSED*/ 4070static int 4071zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link, 4072 cred_t *cr, kthread_t *td) 4073{ 4074 znode_t *zp, *dzp = VTOZ(dvp); 4075 zfs_dirlock_t *dl; 4076 dmu_tx_t *tx; 4077 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4078 zilog_t *zilog; 4079 uint64_t len = strlen(link); 4080 int error; 4081 int zflg = ZNEW; 4082 zfs_acl_ids_t acl_ids; 4083 boolean_t fuid_dirtied; 4084 uint64_t txtype = TX_SYMLINK; 4085 boolean_t waited = B_FALSE; 4086 int flags = 0; 4087 4088 ASSERT(vap->va_type == VLNK); 4089 4090 ZFS_ENTER(zfsvfs); 4091 ZFS_VERIFY_ZP(dzp); 4092 zilog = zfsvfs->z_log; 4093 4094 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 4095 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 4096 ZFS_EXIT(zfsvfs); 4097 return (SET_ERROR(EILSEQ)); 4098 } 4099 if (flags & FIGNORECASE) 4100 zflg |= ZCILOOK; 4101 4102 if (len > MAXPATHLEN) { 4103 ZFS_EXIT(zfsvfs); 4104 return (SET_ERROR(ENAMETOOLONG)); 4105 } 4106 4107 if ((error = zfs_acl_ids_create(dzp, 0, 4108 vap, cr, NULL, &acl_ids)) != 0) { 4109 ZFS_EXIT(zfsvfs); 4110 return (error); 4111 } 4112top: 4113 /* 4114 * Attempt to lock directory; fail if entry already exists. 4115 */ 4116 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 4117 if (error) { 4118 zfs_acl_ids_free(&acl_ids); 4119 ZFS_EXIT(zfsvfs); 4120 return (error); 4121 } 4122 4123 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 4124 zfs_acl_ids_free(&acl_ids); 4125 zfs_dirent_unlock(dl); 4126 ZFS_EXIT(zfsvfs); 4127 return (error); 4128 } 4129 4130 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 4131 zfs_acl_ids_free(&acl_ids); 4132 zfs_dirent_unlock(dl); 4133 ZFS_EXIT(zfsvfs); 4134 return (SET_ERROR(EDQUOT)); 4135 } 4136 tx = dmu_tx_create(zfsvfs->z_os); 4137 fuid_dirtied = zfsvfs->z_fuid_dirty; 4138 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 4139 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 4140 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 4141 ZFS_SA_BASE_ATTR_SIZE + len); 4142 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 4143 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 4144 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 4145 acl_ids.z_aclp->z_acl_bytes); 4146 } 4147 if (fuid_dirtied) 4148 zfs_fuid_txhold(zfsvfs, tx); 4149 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 4150 if (error) { 4151 zfs_dirent_unlock(dl); 4152 if (error == ERESTART) { 4153 waited = B_TRUE; 4154 dmu_tx_wait(tx); 4155 dmu_tx_abort(tx); 4156 goto top; 4157 } 4158 zfs_acl_ids_free(&acl_ids); 4159 dmu_tx_abort(tx); 4160 ZFS_EXIT(zfsvfs); 4161 return (error); 4162 } 4163 4164 /* 4165 * Create a new object for the symlink. 4166 * for version 4 ZPL datsets the symlink will be an SA attribute 4167 */ 4168 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 4169 4170 if (fuid_dirtied) 4171 zfs_fuid_sync(zfsvfs, tx); 4172 4173 mutex_enter(&zp->z_lock); 4174 if (zp->z_is_sa) 4175 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 4176 link, len, tx); 4177 else 4178 zfs_sa_symlink(zp, link, len, tx); 4179 mutex_exit(&zp->z_lock); 4180 4181 zp->z_size = len; 4182 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 4183 &zp->z_size, sizeof (zp->z_size), tx); 4184 /* 4185 * Insert the new object into the directory. 4186 */ 4187 (void) zfs_link_create(dl, zp, tx, ZNEW); 4188 4189 if (flags & FIGNORECASE) 4190 txtype |= TX_CI; 4191 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 4192 *vpp = ZTOV(zp); 4193 4194 zfs_acl_ids_free(&acl_ids); 4195 4196 dmu_tx_commit(tx); 4197 4198 zfs_dirent_unlock(dl); 4199 4200 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4201 zil_commit(zilog, 0); 4202 4203 ZFS_EXIT(zfsvfs); 4204 return (error); 4205} 4206 4207/* 4208 * Return, in the buffer contained in the provided uio structure, 4209 * the symbolic path referred to by vp. 4210 * 4211 * IN: vp - vnode of symbolic link. 4212 * uio - structure to contain the link path. 4213 * cr - credentials of caller. 4214 * ct - caller context 4215 * 4216 * OUT: uio - structure containing the link path. 4217 * 4218 * RETURN: 0 on success, error code on failure. 4219 * 4220 * Timestamps: 4221 * vp - atime updated 4222 */ 4223/* ARGSUSED */ 4224static int 4225zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 4226{ 4227 znode_t *zp = VTOZ(vp); 4228 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4229 int error; 4230 4231 ZFS_ENTER(zfsvfs); 4232 ZFS_VERIFY_ZP(zp); 4233 4234 mutex_enter(&zp->z_lock); 4235 if (zp->z_is_sa) 4236 error = sa_lookup_uio(zp->z_sa_hdl, 4237 SA_ZPL_SYMLINK(zfsvfs), uio); 4238 else 4239 error = zfs_sa_readlink(zp, uio); 4240 mutex_exit(&zp->z_lock); 4241 4242 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4243 4244 ZFS_EXIT(zfsvfs); 4245 return (error); 4246} 4247 4248/* 4249 * Insert a new entry into directory tdvp referencing svp. 4250 * 4251 * IN: tdvp - Directory to contain new entry. 4252 * svp - vnode of new entry. 4253 * name - name of new entry. 4254 * cr - credentials of caller. 4255 * ct - caller context 4256 * 4257 * RETURN: 0 on success, error code on failure. 4258 * 4259 * Timestamps: 4260 * tdvp - ctime|mtime updated 4261 * svp - ctime updated 4262 */ 4263/* ARGSUSED */ 4264static int 4265zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 4266 caller_context_t *ct, int flags) 4267{ 4268 znode_t *dzp = VTOZ(tdvp); 4269 znode_t *tzp, *szp; 4270 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 4271 zilog_t *zilog; 4272 zfs_dirlock_t *dl; 4273 dmu_tx_t *tx; 4274 vnode_t *realvp; 4275 int error; 4276 int zf = ZNEW; 4277 uint64_t parent; 4278 uid_t owner; 4279 boolean_t waited = B_FALSE; 4280 4281 ASSERT(tdvp->v_type == VDIR); 4282 4283 ZFS_ENTER(zfsvfs); 4284 ZFS_VERIFY_ZP(dzp); 4285 zilog = zfsvfs->z_log; 4286 4287 if (VOP_REALVP(svp, &realvp, ct) == 0) 4288 svp = realvp; 4289 4290 /* 4291 * POSIX dictates that we return EPERM here. 4292 * Better choices include ENOTSUP or EISDIR. 4293 */ 4294 if (svp->v_type == VDIR) { 4295 ZFS_EXIT(zfsvfs); 4296 return (SET_ERROR(EPERM)); 4297 } 4298 4299 szp = VTOZ(svp); 4300 ZFS_VERIFY_ZP(szp); 4301 4302 /* 4303 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the 4304 * ctldir appear to have the same v_vfsp. 4305 */ 4306 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) { 4307 ZFS_EXIT(zfsvfs); 4308 return (SET_ERROR(EXDEV)); 4309 } 4310 4311 /* Prevent links to .zfs/shares files */ 4312 4313 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 4314 &parent, sizeof (uint64_t))) != 0) { 4315 ZFS_EXIT(zfsvfs); 4316 return (error); 4317 } 4318 if (parent == zfsvfs->z_shares_dir) { 4319 ZFS_EXIT(zfsvfs); 4320 return (SET_ERROR(EPERM)); 4321 } 4322 4323 if (zfsvfs->z_utf8 && u8_validate(name, 4324 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 4325 ZFS_EXIT(zfsvfs); 4326 return (SET_ERROR(EILSEQ)); 4327 } 4328 if (flags & FIGNORECASE) 4329 zf |= ZCILOOK; 4330 4331 /* 4332 * We do not support links between attributes and non-attributes 4333 * because of the potential security risk of creating links 4334 * into "normal" file space in order to circumvent restrictions 4335 * imposed in attribute space. 4336 */ 4337 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { 4338 ZFS_EXIT(zfsvfs); 4339 return (SET_ERROR(EINVAL)); 4340 } 4341 4342 4343 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER); 4344 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) { 4345 ZFS_EXIT(zfsvfs); 4346 return (SET_ERROR(EPERM)); 4347 } 4348 4349 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 4350 ZFS_EXIT(zfsvfs); 4351 return (error); 4352 } 4353 4354top: 4355 /* 4356 * Attempt to lock directory; fail if entry already exists. 4357 */ 4358 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 4359 if (error) { 4360 ZFS_EXIT(zfsvfs); 4361 return (error); 4362 } 4363 4364 tx = dmu_tx_create(zfsvfs->z_os); 4365 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 4366 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 4367 zfs_sa_upgrade_txholds(tx, szp); 4368 zfs_sa_upgrade_txholds(tx, dzp); 4369 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT); 4370 if (error) { 4371 zfs_dirent_unlock(dl); 4372 if (error == ERESTART) { 4373 waited = B_TRUE; 4374 dmu_tx_wait(tx); 4375 dmu_tx_abort(tx); 4376 goto top; 4377 } 4378 dmu_tx_abort(tx); 4379 ZFS_EXIT(zfsvfs); 4380 return (error); 4381 } 4382 4383 error = zfs_link_create(dl, szp, tx, 0); 4384 4385 if (error == 0) { 4386 uint64_t txtype = TX_LINK; 4387 if (flags & FIGNORECASE) 4388 txtype |= TX_CI; 4389 zfs_log_link(zilog, tx, txtype, dzp, szp, name); 4390 } 4391 4392 dmu_tx_commit(tx); 4393 4394 zfs_dirent_unlock(dl); 4395 4396 if (error == 0) { 4397 vnevent_link(svp, ct); 4398 } 4399 4400 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4401 zil_commit(zilog, 0); 4402 4403 ZFS_EXIT(zfsvfs); 4404 return (error); 4405} 4406 4407#ifdef sun 4408/* 4409 * zfs_null_putapage() is used when the file system has been force 4410 * unmounted. It just drops the pages. 4411 */ 4412/* ARGSUSED */ 4413static int 4414zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4415 size_t *lenp, int flags, cred_t *cr) 4416{ 4417 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 4418 return (0); 4419} 4420 4421/* 4422 * Push a page out to disk, klustering if possible. 4423 * 4424 * IN: vp - file to push page to. 4425 * pp - page to push. 4426 * flags - additional flags. 4427 * cr - credentials of caller. 4428 * 4429 * OUT: offp - start of range pushed. 4430 * lenp - len of range pushed. 4431 * 4432 * RETURN: 0 on success, error code on failure. 4433 * 4434 * NOTE: callers must have locked the page to be pushed. On 4435 * exit, the page (and all other pages in the kluster) must be 4436 * unlocked. 4437 */ 4438/* ARGSUSED */ 4439static int 4440zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4441 size_t *lenp, int flags, cred_t *cr) 4442{ 4443 znode_t *zp = VTOZ(vp); 4444 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4445 dmu_tx_t *tx; 4446 u_offset_t off, koff; 4447 size_t len, klen; 4448 int err; 4449 4450 off = pp->p_offset; 4451 len = PAGESIZE; 4452 /* 4453 * If our blocksize is bigger than the page size, try to kluster 4454 * multiple pages so that we write a full block (thus avoiding 4455 * a read-modify-write). 4456 */ 4457 if (off < zp->z_size && zp->z_blksz > PAGESIZE) { 4458 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 4459 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 4460 ASSERT(koff <= zp->z_size); 4461 if (koff + klen > zp->z_size) 4462 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE); 4463 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 4464 } 4465 ASSERT3U(btop(len), ==, btopr(len)); 4466 4467 /* 4468 * Can't push pages past end-of-file. 4469 */ 4470 if (off >= zp->z_size) { 4471 /* ignore all pages */ 4472 err = 0; 4473 goto out; 4474 } else if (off + len > zp->z_size) { 4475 int npages = btopr(zp->z_size - off); 4476 page_t *trunc; 4477 4478 page_list_break(&pp, &trunc, npages); 4479 /* ignore pages past end of file */ 4480 if (trunc) 4481 pvn_write_done(trunc, flags); 4482 len = zp->z_size - off; 4483 } 4484 4485 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 4486 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 4487 err = SET_ERROR(EDQUOT); 4488 goto out; 4489 } 4490 tx = dmu_tx_create(zfsvfs->z_os); 4491 dmu_tx_hold_write(tx, zp->z_id, off, len); 4492 4493 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 4494 zfs_sa_upgrade_txholds(tx, zp); 4495 err = dmu_tx_assign(tx, TXG_WAIT); 4496 if (err != 0) { 4497 dmu_tx_abort(tx); 4498 goto out; 4499 } 4500 4501 if (zp->z_blksz <= PAGESIZE) { 4502 caddr_t va = zfs_map_page(pp, S_READ); 4503 ASSERT3U(len, <=, PAGESIZE); 4504 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 4505 zfs_unmap_page(pp, va); 4506 } else { 4507 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 4508 } 4509 4510 if (err == 0) { 4511 uint64_t mtime[2], ctime[2]; 4512 sa_bulk_attr_t bulk[3]; 4513 int count = 0; 4514 4515 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 4516 &mtime, 16); 4517 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 4518 &ctime, 16); 4519 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 4520 &zp->z_pflags, 8); 4521 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 4522 B_TRUE); 4523 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 4524 } 4525 dmu_tx_commit(tx); 4526 4527out: 4528 pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 4529 if (offp) 4530 *offp = off; 4531 if (lenp) 4532 *lenp = len; 4533 4534 return (err); 4535} 4536 4537/* 4538 * Copy the portion of the file indicated from pages into the file. 4539 * The pages are stored in a page list attached to the files vnode. 4540 * 4541 * IN: vp - vnode of file to push page data to. 4542 * off - position in file to put data. 4543 * len - amount of data to write. 4544 * flags - flags to control the operation. 4545 * cr - credentials of caller. 4546 * ct - caller context. 4547 * 4548 * RETURN: 0 on success, error code on failure. 4549 * 4550 * Timestamps: 4551 * vp - ctime|mtime updated 4552 */ 4553/*ARGSUSED*/ 4554static int 4555zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 4556 caller_context_t *ct) 4557{ 4558 znode_t *zp = VTOZ(vp); 4559 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4560 page_t *pp; 4561 size_t io_len; 4562 u_offset_t io_off; 4563 uint_t blksz; 4564 rl_t *rl; 4565 int error = 0; 4566 4567 ZFS_ENTER(zfsvfs); 4568 ZFS_VERIFY_ZP(zp); 4569 4570 /* 4571 * Align this request to the file block size in case we kluster. 4572 * XXX - this can result in pretty aggresive locking, which can 4573 * impact simultanious read/write access. One option might be 4574 * to break up long requests (len == 0) into block-by-block 4575 * operations to get narrower locking. 4576 */ 4577 blksz = zp->z_blksz; 4578 if (ISP2(blksz)) 4579 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 4580 else 4581 io_off = 0; 4582 if (len > 0 && ISP2(blksz)) 4583 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 4584 else 4585 io_len = 0; 4586 4587 if (io_len == 0) { 4588 /* 4589 * Search the entire vp list for pages >= io_off. 4590 */ 4591 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 4592 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 4593 goto out; 4594 } 4595 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 4596 4597 if (off > zp->z_size) { 4598 /* past end of file */ 4599 zfs_range_unlock(rl); 4600 ZFS_EXIT(zfsvfs); 4601 return (0); 4602 } 4603 4604 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off); 4605 4606 for (off = io_off; io_off < off + len; io_off += io_len) { 4607 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 4608 pp = page_lookup(vp, io_off, 4609 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 4610 } else { 4611 pp = page_lookup_nowait(vp, io_off, 4612 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 4613 } 4614 4615 if (pp != NULL && pvn_getdirty(pp, flags)) { 4616 int err; 4617 4618 /* 4619 * Found a dirty page to push 4620 */ 4621 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 4622 if (err) 4623 error = err; 4624 } else { 4625 io_len = PAGESIZE; 4626 } 4627 } 4628out: 4629 zfs_range_unlock(rl); 4630 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4631 zil_commit(zfsvfs->z_log, zp->z_id); 4632 ZFS_EXIT(zfsvfs); 4633 return (error); 4634} 4635#endif /* sun */ 4636 4637/*ARGSUSED*/ 4638void 4639zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4640{ 4641 znode_t *zp = VTOZ(vp); 4642 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4643 int error; 4644 4645 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 4646 if (zp->z_sa_hdl == NULL) { 4647 /* 4648 * The fs has been unmounted, or we did a 4649 * suspend/resume and this file no longer exists. 4650 */ 4651 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4652 vrecycle(vp); 4653 return; 4654 } 4655 4656 mutex_enter(&zp->z_lock); 4657 if (zp->z_unlinked) { 4658 /* 4659 * Fast path to recycle a vnode of a removed file. 4660 */ 4661 mutex_exit(&zp->z_lock); 4662 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4663 vrecycle(vp); 4664 return; 4665 } 4666 mutex_exit(&zp->z_lock); 4667 4668 if (zp->z_atime_dirty && zp->z_unlinked == 0) { 4669 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 4670 4671 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 4672 zfs_sa_upgrade_txholds(tx, zp); 4673 error = dmu_tx_assign(tx, TXG_WAIT); 4674 if (error) { 4675 dmu_tx_abort(tx); 4676 } else { 4677 mutex_enter(&zp->z_lock); 4678 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 4679 (void *)&zp->z_atime, sizeof (zp->z_atime), tx); 4680 zp->z_atime_dirty = 0; 4681 mutex_exit(&zp->z_lock); 4682 dmu_tx_commit(tx); 4683 } 4684 } 4685 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4686} 4687 4688#ifdef sun 4689/* 4690 * Bounds-check the seek operation. 4691 * 4692 * IN: vp - vnode seeking within 4693 * ooff - old file offset 4694 * noffp - pointer to new file offset 4695 * ct - caller context 4696 * 4697 * RETURN: 0 on success, EINVAL if new offset invalid. 4698 */ 4699/* ARGSUSED */ 4700static int 4701zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 4702 caller_context_t *ct) 4703{ 4704 if (vp->v_type == VDIR) 4705 return (0); 4706 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 4707} 4708 4709/* 4710 * Pre-filter the generic locking function to trap attempts to place 4711 * a mandatory lock on a memory mapped file. 4712 */ 4713static int 4714zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 4715 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 4716{ 4717 znode_t *zp = VTOZ(vp); 4718 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4719 4720 ZFS_ENTER(zfsvfs); 4721 ZFS_VERIFY_ZP(zp); 4722 4723 /* 4724 * We are following the UFS semantics with respect to mapcnt 4725 * here: If we see that the file is mapped already, then we will 4726 * return an error, but we don't worry about races between this 4727 * function and zfs_map(). 4728 */ 4729 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) { 4730 ZFS_EXIT(zfsvfs); 4731 return (SET_ERROR(EAGAIN)); 4732 } 4733 ZFS_EXIT(zfsvfs); 4734 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 4735} 4736 4737/* 4738 * If we can't find a page in the cache, we will create a new page 4739 * and fill it with file data. For efficiency, we may try to fill 4740 * multiple pages at once (klustering) to fill up the supplied page 4741 * list. Note that the pages to be filled are held with an exclusive 4742 * lock to prevent access by other threads while they are being filled. 4743 */ 4744static int 4745zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4746 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4747{ 4748 znode_t *zp = VTOZ(vp); 4749 page_t *pp, *cur_pp; 4750 objset_t *os = zp->z_zfsvfs->z_os; 4751 u_offset_t io_off, total; 4752 size_t io_len; 4753 int err; 4754 4755 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 4756 /* 4757 * We only have a single page, don't bother klustering 4758 */ 4759 io_off = off; 4760 io_len = PAGESIZE; 4761 pp = page_create_va(vp, io_off, io_len, 4762 PG_EXCL | PG_WAIT, seg, addr); 4763 } else { 4764 /* 4765 * Try to find enough pages to fill the page list 4766 */ 4767 pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 4768 &io_len, off, plsz, 0); 4769 } 4770 if (pp == NULL) { 4771 /* 4772 * The page already exists, nothing to do here. 4773 */ 4774 *pl = NULL; 4775 return (0); 4776 } 4777 4778 /* 4779 * Fill the pages in the kluster. 4780 */ 4781 cur_pp = pp; 4782 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 4783 caddr_t va; 4784 4785 ASSERT3U(io_off, ==, cur_pp->p_offset); 4786 va = zfs_map_page(cur_pp, S_WRITE); 4787 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 4788 DMU_READ_PREFETCH); 4789 zfs_unmap_page(cur_pp, va); 4790 if (err) { 4791 /* On error, toss the entire kluster */ 4792 pvn_read_done(pp, B_ERROR); 4793 /* convert checksum errors into IO errors */ 4794 if (err == ECKSUM) 4795 err = SET_ERROR(EIO); 4796 return (err); 4797 } 4798 cur_pp = cur_pp->p_next; 4799 } 4800 4801 /* 4802 * Fill in the page list array from the kluster starting 4803 * from the desired offset `off'. 4804 * NOTE: the page list will always be null terminated. 4805 */ 4806 pvn_plist_init(pp, pl, plsz, off, io_len, rw); 4807 ASSERT(pl == NULL || (*pl)->p_offset == off); 4808 4809 return (0); 4810} 4811 4812/* 4813 * Return pointers to the pages for the file region [off, off + len] 4814 * in the pl array. If plsz is greater than len, this function may 4815 * also return page pointers from after the specified region 4816 * (i.e. the region [off, off + plsz]). These additional pages are 4817 * only returned if they are already in the cache, or were created as 4818 * part of a klustered read. 4819 * 4820 * IN: vp - vnode of file to get data from. 4821 * off - position in file to get data from. 4822 * len - amount of data to retrieve. 4823 * plsz - length of provided page list. 4824 * seg - segment to obtain pages for. 4825 * addr - virtual address of fault. 4826 * rw - mode of created pages. 4827 * cr - credentials of caller. 4828 * ct - caller context. 4829 * 4830 * OUT: protp - protection mode of created pages. 4831 * pl - list of pages created. 4832 * 4833 * RETURN: 0 on success, error code on failure. 4834 * 4835 * Timestamps: 4836 * vp - atime updated 4837 */ 4838/* ARGSUSED */ 4839static int 4840zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4841 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 4842 enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4843{ 4844 znode_t *zp = VTOZ(vp); 4845 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4846 page_t **pl0 = pl; 4847 int err = 0; 4848 4849 /* we do our own caching, faultahead is unnecessary */ 4850 if (pl == NULL) 4851 return (0); 4852 else if (len > plsz) 4853 len = plsz; 4854 else 4855 len = P2ROUNDUP(len, PAGESIZE); 4856 ASSERT(plsz >= len); 4857 4858 ZFS_ENTER(zfsvfs); 4859 ZFS_VERIFY_ZP(zp); 4860 4861 if (protp) 4862 *protp = PROT_ALL; 4863 4864 /* 4865 * Loop through the requested range [off, off + len) looking 4866 * for pages. If we don't find a page, we will need to create 4867 * a new page and fill it with data from the file. 4868 */ 4869 while (len > 0) { 4870 if (*pl = page_lookup(vp, off, SE_SHARED)) 4871 *(pl+1) = NULL; 4872 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 4873 goto out; 4874 while (*pl) { 4875 ASSERT3U((*pl)->p_offset, ==, off); 4876 off += PAGESIZE; 4877 addr += PAGESIZE; 4878 if (len > 0) { 4879 ASSERT3U(len, >=, PAGESIZE); 4880 len -= PAGESIZE; 4881 } 4882 ASSERT3U(plsz, >=, PAGESIZE); 4883 plsz -= PAGESIZE; 4884 pl++; 4885 } 4886 } 4887 4888 /* 4889 * Fill out the page array with any pages already in the cache. 4890 */ 4891 while (plsz > 0 && 4892 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 4893 off += PAGESIZE; 4894 plsz -= PAGESIZE; 4895 } 4896out: 4897 if (err) { 4898 /* 4899 * Release any pages we have previously locked. 4900 */ 4901 while (pl > pl0) 4902 page_unlock(*--pl); 4903 } else { 4904 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4905 } 4906 4907 *pl = NULL; 4908 4909 ZFS_EXIT(zfsvfs); 4910 return (err); 4911} 4912 4913/* 4914 * Request a memory map for a section of a file. This code interacts 4915 * with common code and the VM system as follows: 4916 * 4917 * - common code calls mmap(), which ends up in smmap_common() 4918 * - this calls VOP_MAP(), which takes you into (say) zfs 4919 * - zfs_map() calls as_map(), passing segvn_create() as the callback 4920 * - segvn_create() creates the new segment and calls VOP_ADDMAP() 4921 * - zfs_addmap() updates z_mapcnt 4922 */ 4923/*ARGSUSED*/ 4924static int 4925zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 4926 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 4927 caller_context_t *ct) 4928{ 4929 znode_t *zp = VTOZ(vp); 4930 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4931 segvn_crargs_t vn_a; 4932 int error; 4933 4934 ZFS_ENTER(zfsvfs); 4935 ZFS_VERIFY_ZP(zp); 4936 4937 if ((prot & PROT_WRITE) && (zp->z_pflags & 4938 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { 4939 ZFS_EXIT(zfsvfs); 4940 return (SET_ERROR(EPERM)); 4941 } 4942 4943 if ((prot & (PROT_READ | PROT_EXEC)) && 4944 (zp->z_pflags & ZFS_AV_QUARANTINED)) { 4945 ZFS_EXIT(zfsvfs); 4946 return (SET_ERROR(EACCES)); 4947 } 4948 4949 if (vp->v_flag & VNOMAP) { 4950 ZFS_EXIT(zfsvfs); 4951 return (SET_ERROR(ENOSYS)); 4952 } 4953 4954 if (off < 0 || len > MAXOFFSET_T - off) { 4955 ZFS_EXIT(zfsvfs); 4956 return (SET_ERROR(ENXIO)); 4957 } 4958 4959 if (vp->v_type != VREG) { 4960 ZFS_EXIT(zfsvfs); 4961 return (SET_ERROR(ENODEV)); 4962 } 4963 4964 /* 4965 * If file is locked, disallow mapping. 4966 */ 4967 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) { 4968 ZFS_EXIT(zfsvfs); 4969 return (SET_ERROR(EAGAIN)); 4970 } 4971 4972 as_rangelock(as); 4973 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 4974 if (error != 0) { 4975 as_rangeunlock(as); 4976 ZFS_EXIT(zfsvfs); 4977 return (error); 4978 } 4979 4980 vn_a.vp = vp; 4981 vn_a.offset = (u_offset_t)off; 4982 vn_a.type = flags & MAP_TYPE; 4983 vn_a.prot = prot; 4984 vn_a.maxprot = maxprot; 4985 vn_a.cred = cr; 4986 vn_a.amp = NULL; 4987 vn_a.flags = flags & ~MAP_TYPE; 4988 vn_a.szc = 0; 4989 vn_a.lgrp_mem_policy_flags = 0; 4990 4991 error = as_map(as, *addrp, len, segvn_create, &vn_a); 4992 4993 as_rangeunlock(as); 4994 ZFS_EXIT(zfsvfs); 4995 return (error); 4996} 4997 4998/* ARGSUSED */ 4999static int 5000zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 5001 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 5002 caller_context_t *ct) 5003{ 5004 uint64_t pages = btopr(len); 5005 5006 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 5007 return (0); 5008} 5009 5010/* 5011 * The reason we push dirty pages as part of zfs_delmap() is so that we get a 5012 * more accurate mtime for the associated file. Since we don't have a way of 5013 * detecting when the data was actually modified, we have to resort to 5014 * heuristics. If an explicit msync() is done, then we mark the mtime when the 5015 * last page is pushed. The problem occurs when the msync() call is omitted, 5016 * which by far the most common case: 5017 * 5018 * open() 5019 * mmap() 5020 * <modify memory> 5021 * munmap() 5022 * close() 5023 * <time lapse> 5024 * putpage() via fsflush 5025 * 5026 * If we wait until fsflush to come along, we can have a modification time that 5027 * is some arbitrary point in the future. In order to prevent this in the 5028 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 5029 * torn down. 5030 */ 5031/* ARGSUSED */ 5032static int 5033zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 5034 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 5035 caller_context_t *ct) 5036{ 5037 uint64_t pages = btopr(len); 5038 5039 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 5040 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 5041 5042 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 5043 vn_has_cached_data(vp)) 5044 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 5045 5046 return (0); 5047} 5048 5049/* 5050 * Free or allocate space in a file. Currently, this function only 5051 * supports the `F_FREESP' command. However, this command is somewhat 5052 * misnamed, as its functionality includes the ability to allocate as 5053 * well as free space. 5054 * 5055 * IN: vp - vnode of file to free data in. 5056 * cmd - action to take (only F_FREESP supported). 5057 * bfp - section of file to free/alloc. 5058 * flag - current file open mode flags. 5059 * offset - current file offset. 5060 * cr - credentials of caller [UNUSED]. 5061 * ct - caller context. 5062 * 5063 * RETURN: 0 on success, error code on failure. 5064 * 5065 * Timestamps: 5066 * vp - ctime|mtime updated 5067 */ 5068/* ARGSUSED */ 5069static int 5070zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 5071 offset_t offset, cred_t *cr, caller_context_t *ct) 5072{ 5073 znode_t *zp = VTOZ(vp); 5074 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5075 uint64_t off, len; 5076 int error; 5077 5078 ZFS_ENTER(zfsvfs); 5079 ZFS_VERIFY_ZP(zp); 5080 5081 if (cmd != F_FREESP) { 5082 ZFS_EXIT(zfsvfs); 5083 return (SET_ERROR(EINVAL)); 5084 } 5085 5086 if (error = convoff(vp, bfp, 0, offset)) { 5087 ZFS_EXIT(zfsvfs); 5088 return (error); 5089 } 5090 5091 if (bfp->l_len < 0) { 5092 ZFS_EXIT(zfsvfs); 5093 return (SET_ERROR(EINVAL)); 5094 } 5095 5096 off = bfp->l_start; 5097 len = bfp->l_len; /* 0 means from off to end of file */ 5098 5099 error = zfs_freesp(zp, off, len, flag, TRUE); 5100 5101 ZFS_EXIT(zfsvfs); 5102 return (error); 5103} 5104#endif /* sun */ 5105 5106CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid)); 5107CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid)); 5108 5109/*ARGSUSED*/ 5110static int 5111zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 5112{ 5113 znode_t *zp = VTOZ(vp); 5114 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5115 uint32_t gen; 5116 uint64_t gen64; 5117 uint64_t object = zp->z_id; 5118 zfid_short_t *zfid; 5119 int size, i, error; 5120 5121 ZFS_ENTER(zfsvfs); 5122 ZFS_VERIFY_ZP(zp); 5123 5124 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 5125 &gen64, sizeof (uint64_t))) != 0) { 5126 ZFS_EXIT(zfsvfs); 5127 return (error); 5128 } 5129 5130 gen = (uint32_t)gen64; 5131 5132 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 5133 5134#ifdef illumos 5135 if (fidp->fid_len < size) { 5136 fidp->fid_len = size; 5137 ZFS_EXIT(zfsvfs); 5138 return (SET_ERROR(ENOSPC)); 5139 } 5140#else 5141 fidp->fid_len = size; 5142#endif 5143 5144 zfid = (zfid_short_t *)fidp; 5145 5146 zfid->zf_len = size; 5147 5148 for (i = 0; i < sizeof (zfid->zf_object); i++) 5149 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 5150 5151 /* Must have a non-zero generation number to distinguish from .zfs */ 5152 if (gen == 0) 5153 gen = 1; 5154 for (i = 0; i < sizeof (zfid->zf_gen); i++) 5155 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 5156 5157 if (size == LONG_FID_LEN) { 5158 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 5159 zfid_long_t *zlfid; 5160 5161 zlfid = (zfid_long_t *)fidp; 5162 5163 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 5164 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 5165 5166 /* XXX - this should be the generation number for the objset */ 5167 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 5168 zlfid->zf_setgen[i] = 0; 5169 } 5170 5171 ZFS_EXIT(zfsvfs); 5172 return (0); 5173} 5174 5175static int 5176zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 5177 caller_context_t *ct) 5178{ 5179 znode_t *zp, *xzp; 5180 zfsvfs_t *zfsvfs; 5181 zfs_dirlock_t *dl; 5182 int error; 5183 5184 switch (cmd) { 5185 case _PC_LINK_MAX: 5186 *valp = INT_MAX; 5187 return (0); 5188 5189 case _PC_FILESIZEBITS: 5190 *valp = 64; 5191 return (0); 5192#ifdef sun 5193 case _PC_XATTR_EXISTS: 5194 zp = VTOZ(vp); 5195 zfsvfs = zp->z_zfsvfs; 5196 ZFS_ENTER(zfsvfs); 5197 ZFS_VERIFY_ZP(zp); 5198 *valp = 0; 5199 error = zfs_dirent_lock(&dl, zp, "", &xzp, 5200 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 5201 if (error == 0) { 5202 zfs_dirent_unlock(dl); 5203 if (!zfs_dirempty(xzp)) 5204 *valp = 1; 5205 VN_RELE(ZTOV(xzp)); 5206 } else if (error == ENOENT) { 5207 /* 5208 * If there aren't extended attributes, it's the 5209 * same as having zero of them. 5210 */ 5211 error = 0; 5212 } 5213 ZFS_EXIT(zfsvfs); 5214 return (error); 5215 5216 case _PC_SATTR_ENABLED: 5217 case _PC_SATTR_EXISTS: 5218 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 5219 (vp->v_type == VREG || vp->v_type == VDIR); 5220 return (0); 5221 5222 case _PC_ACCESS_FILTERING: 5223 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 5224 vp->v_type == VDIR; 5225 return (0); 5226 5227 case _PC_ACL_ENABLED: 5228 *valp = _ACL_ACE_ENABLED; 5229 return (0); 5230#endif /* sun */ 5231 case _PC_MIN_HOLE_SIZE: 5232 *valp = (int)SPA_MINBLOCKSIZE; 5233 return (0); 5234#ifdef sun 5235 case _PC_TIMESTAMP_RESOLUTION: 5236 /* nanosecond timestamp resolution */ 5237 *valp = 1L; 5238 return (0); 5239#endif /* sun */ 5240 case _PC_ACL_EXTENDED: 5241 *valp = 0; 5242 return (0); 5243 5244 case _PC_ACL_NFS4: 5245 *valp = 1; 5246 return (0); 5247 5248 case _PC_ACL_PATH_MAX: 5249 *valp = ACL_MAX_ENTRIES; 5250 return (0); 5251 5252 default: 5253 return (EOPNOTSUPP); 5254 } 5255} 5256 5257/*ARGSUSED*/ 5258static int 5259zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 5260 caller_context_t *ct) 5261{ 5262 znode_t *zp = VTOZ(vp); 5263 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5264 int error; 5265 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 5266 5267 ZFS_ENTER(zfsvfs); 5268 ZFS_VERIFY_ZP(zp); 5269 error = zfs_getacl(zp, vsecp, skipaclchk, cr); 5270 ZFS_EXIT(zfsvfs); 5271 5272 return (error); 5273} 5274 5275/*ARGSUSED*/ 5276int 5277zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 5278 caller_context_t *ct) 5279{ 5280 znode_t *zp = VTOZ(vp); 5281 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5282 int error; 5283 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 5284 zilog_t *zilog = zfsvfs->z_log; 5285 5286 ZFS_ENTER(zfsvfs); 5287 ZFS_VERIFY_ZP(zp); 5288 5289 error = zfs_setacl(zp, vsecp, skipaclchk, cr); 5290 5291 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 5292 zil_commit(zilog, 0); 5293 5294 ZFS_EXIT(zfsvfs); 5295 return (error); 5296} 5297 5298#ifdef sun 5299/* 5300 * The smallest read we may consider to loan out an arcbuf. 5301 * This must be a power of 2. 5302 */ 5303int zcr_blksz_min = (1 << 10); /* 1K */ 5304/* 5305 * If set to less than the file block size, allow loaning out of an 5306 * arcbuf for a partial block read. This must be a power of 2. 5307 */ 5308int zcr_blksz_max = (1 << 17); /* 128K */ 5309 5310/*ARGSUSED*/ 5311static int 5312zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr, 5313 caller_context_t *ct) 5314{ 5315 znode_t *zp = VTOZ(vp); 5316 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5317 int max_blksz = zfsvfs->z_max_blksz; 5318 uio_t *uio = &xuio->xu_uio; 5319 ssize_t size = uio->uio_resid; 5320 offset_t offset = uio->uio_loffset; 5321 int blksz; 5322 int fullblk, i; 5323 arc_buf_t *abuf; 5324 ssize_t maxsize; 5325 int preamble, postamble; 5326 5327 if (xuio->xu_type != UIOTYPE_ZEROCOPY) 5328 return (SET_ERROR(EINVAL)); 5329 5330 ZFS_ENTER(zfsvfs); 5331 ZFS_VERIFY_ZP(zp); 5332 switch (ioflag) { 5333 case UIO_WRITE: 5334 /* 5335 * Loan out an arc_buf for write if write size is bigger than 5336 * max_blksz, and the file's block size is also max_blksz. 5337 */ 5338 blksz = max_blksz; 5339 if (size < blksz || zp->z_blksz != blksz) { 5340 ZFS_EXIT(zfsvfs); 5341 return (SET_ERROR(EINVAL)); 5342 } 5343 /* 5344 * Caller requests buffers for write before knowing where the 5345 * write offset might be (e.g. NFS TCP write). 5346 */ 5347 if (offset == -1) { 5348 preamble = 0; 5349 } else { 5350 preamble = P2PHASE(offset, blksz); 5351 if (preamble) { 5352 preamble = blksz - preamble; 5353 size -= preamble; 5354 } 5355 } 5356 5357 postamble = P2PHASE(size, blksz); 5358 size -= postamble; 5359 5360 fullblk = size / blksz; 5361 (void) dmu_xuio_init(xuio, 5362 (preamble != 0) + fullblk + (postamble != 0)); 5363 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble, 5364 int, postamble, int, 5365 (preamble != 0) + fullblk + (postamble != 0)); 5366 5367 /* 5368 * Have to fix iov base/len for partial buffers. They 5369 * currently represent full arc_buf's. 5370 */ 5371 if (preamble) { 5372 /* data begins in the middle of the arc_buf */ 5373 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5374 blksz); 5375 ASSERT(abuf); 5376 (void) dmu_xuio_add(xuio, abuf, 5377 blksz - preamble, preamble); 5378 } 5379 5380 for (i = 0; i < fullblk; i++) { 5381 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5382 blksz); 5383 ASSERT(abuf); 5384 (void) dmu_xuio_add(xuio, abuf, 0, blksz); 5385 } 5386 5387 if (postamble) { 5388 /* data ends in the middle of the arc_buf */ 5389 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 5390 blksz); 5391 ASSERT(abuf); 5392 (void) dmu_xuio_add(xuio, abuf, 0, postamble); 5393 } 5394 break; 5395 case UIO_READ: 5396 /* 5397 * Loan out an arc_buf for read if the read size is larger than 5398 * the current file block size. Block alignment is not 5399 * considered. Partial arc_buf will be loaned out for read. 5400 */ 5401 blksz = zp->z_blksz; 5402 if (blksz < zcr_blksz_min) 5403 blksz = zcr_blksz_min; 5404 if (blksz > zcr_blksz_max) 5405 blksz = zcr_blksz_max; 5406 /* avoid potential complexity of dealing with it */ 5407 if (blksz > max_blksz) { 5408 ZFS_EXIT(zfsvfs); 5409 return (SET_ERROR(EINVAL)); 5410 } 5411 5412 maxsize = zp->z_size - uio->uio_loffset; 5413 if (size > maxsize) 5414 size = maxsize; 5415 5416 if (size < blksz || vn_has_cached_data(vp)) { 5417 ZFS_EXIT(zfsvfs); 5418 return (SET_ERROR(EINVAL)); 5419 } 5420 break; 5421 default: 5422 ZFS_EXIT(zfsvfs); 5423 return (SET_ERROR(EINVAL)); 5424 } 5425 5426 uio->uio_extflg = UIO_XUIO; 5427 XUIO_XUZC_RW(xuio) = ioflag; 5428 ZFS_EXIT(zfsvfs); 5429 return (0); 5430} 5431 5432/*ARGSUSED*/ 5433static int 5434zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct) 5435{ 5436 int i; 5437 arc_buf_t *abuf; 5438 int ioflag = XUIO_XUZC_RW(xuio); 5439 5440 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); 5441 5442 i = dmu_xuio_cnt(xuio); 5443 while (i-- > 0) { 5444 abuf = dmu_xuio_arcbuf(xuio, i); 5445 /* 5446 * if abuf == NULL, it must be a write buffer 5447 * that has been returned in zfs_write(). 5448 */ 5449 if (abuf) 5450 dmu_return_arcbuf(abuf); 5451 ASSERT(abuf || ioflag == UIO_WRITE); 5452 } 5453 5454 dmu_xuio_fini(xuio); 5455 return (0); 5456} 5457 5458/* 5459 * Predeclare these here so that the compiler assumes that 5460 * this is an "old style" function declaration that does 5461 * not include arguments => we won't get type mismatch errors 5462 * in the initializations that follow. 5463 */ 5464static int zfs_inval(); 5465static int zfs_isdir(); 5466 5467static int 5468zfs_inval() 5469{ 5470 return (SET_ERROR(EINVAL)); 5471} 5472 5473static int 5474zfs_isdir() 5475{ 5476 return (SET_ERROR(EISDIR)); 5477} 5478/* 5479 * Directory vnode operations template 5480 */ 5481vnodeops_t *zfs_dvnodeops; 5482const fs_operation_def_t zfs_dvnodeops_template[] = { 5483 VOPNAME_OPEN, { .vop_open = zfs_open }, 5484 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5485 VOPNAME_READ, { .error = zfs_isdir }, 5486 VOPNAME_WRITE, { .error = zfs_isdir }, 5487 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5488 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5489 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5490 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5491 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5492 VOPNAME_CREATE, { .vop_create = zfs_create }, 5493 VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 5494 VOPNAME_LINK, { .vop_link = zfs_link }, 5495 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5496 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 5497 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 5498 VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 5499 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 5500 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5501 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5502 VOPNAME_FID, { .vop_fid = zfs_fid }, 5503 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5504 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5505 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5506 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5507 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5508 NULL, NULL 5509}; 5510 5511/* 5512 * Regular file vnode operations template 5513 */ 5514vnodeops_t *zfs_fvnodeops; 5515const fs_operation_def_t zfs_fvnodeops_template[] = { 5516 VOPNAME_OPEN, { .vop_open = zfs_open }, 5517 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5518 VOPNAME_READ, { .vop_read = zfs_read }, 5519 VOPNAME_WRITE, { .vop_write = zfs_write }, 5520 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5521 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5522 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5523 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5524 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5525 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5526 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5527 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5528 VOPNAME_FID, { .vop_fid = zfs_fid }, 5529 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5530 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 5531 VOPNAME_SPACE, { .vop_space = zfs_space }, 5532 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 5533 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 5534 VOPNAME_MAP, { .vop_map = zfs_map }, 5535 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 5536 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 5537 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5538 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5539 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5540 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5541 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf }, 5542 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf }, 5543 NULL, NULL 5544}; 5545 5546/* 5547 * Symbolic link vnode operations template 5548 */ 5549vnodeops_t *zfs_symvnodeops; 5550const fs_operation_def_t zfs_symvnodeops_template[] = { 5551 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5552 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5553 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5554 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5555 VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 5556 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5557 VOPNAME_FID, { .vop_fid = zfs_fid }, 5558 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5559 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5560 NULL, NULL 5561}; 5562 5563/* 5564 * special share hidden files vnode operations template 5565 */ 5566vnodeops_t *zfs_sharevnodeops; 5567const fs_operation_def_t zfs_sharevnodeops_template[] = { 5568 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5569 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5570 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5571 VOPNAME_FID, { .vop_fid = zfs_fid }, 5572 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5573 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5574 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5575 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5576 NULL, NULL 5577}; 5578 5579/* 5580 * Extended attribute directory vnode operations template 5581 * 5582 * This template is identical to the directory vnodes 5583 * operation template except for restricted operations: 5584 * VOP_MKDIR() 5585 * VOP_SYMLINK() 5586 * 5587 * Note that there are other restrictions embedded in: 5588 * zfs_create() - restrict type to VREG 5589 * zfs_link() - no links into/out of attribute space 5590 * zfs_rename() - no moves into/out of attribute space 5591 */ 5592vnodeops_t *zfs_xdvnodeops; 5593const fs_operation_def_t zfs_xdvnodeops_template[] = { 5594 VOPNAME_OPEN, { .vop_open = zfs_open }, 5595 VOPNAME_CLOSE, { .vop_close = zfs_close }, 5596 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 5597 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 5598 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 5599 VOPNAME_ACCESS, { .vop_access = zfs_access }, 5600 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 5601 VOPNAME_CREATE, { .vop_create = zfs_create }, 5602 VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 5603 VOPNAME_LINK, { .vop_link = zfs_link }, 5604 VOPNAME_RENAME, { .vop_rename = zfs_rename }, 5605 VOPNAME_MKDIR, { .error = zfs_inval }, 5606 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 5607 VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 5608 VOPNAME_SYMLINK, { .error = zfs_inval }, 5609 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 5610 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5611 VOPNAME_FID, { .vop_fid = zfs_fid }, 5612 VOPNAME_SEEK, { .vop_seek = zfs_seek }, 5613 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5614 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 5615 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 5616 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 5617 NULL, NULL 5618}; 5619 5620/* 5621 * Error vnode operations template 5622 */ 5623vnodeops_t *zfs_evnodeops; 5624const fs_operation_def_t zfs_evnodeops_template[] = { 5625 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 5626 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 5627 NULL, NULL 5628}; 5629#endif /* sun */ 5630 5631static int 5632ioflags(int ioflags) 5633{ 5634 int flags = 0; 5635 5636 if (ioflags & IO_APPEND) 5637 flags |= FAPPEND; 5638 if (ioflags & IO_NDELAY) 5639 flags |= FNONBLOCK; 5640 if (ioflags & IO_SYNC) 5641 flags |= (FSYNC | FDSYNC | FRSYNC); 5642 5643 return (flags); 5644} 5645 5646static int 5647zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage) 5648{ 5649 znode_t *zp = VTOZ(vp); 5650 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5651 objset_t *os = zp->z_zfsvfs->z_os; 5652 vm_page_t mfirst, mlast, mreq; 5653 vm_object_t object; 5654 caddr_t va; 5655 struct sf_buf *sf; 5656 off_t startoff, endoff; 5657 int i, error; 5658 vm_pindex_t reqstart, reqend; 5659 int pcount, lsize, reqsize, size; 5660 5661 ZFS_ENTER(zfsvfs); 5662 ZFS_VERIFY_ZP(zp); 5663 5664 pcount = OFF_TO_IDX(round_page(count)); 5665 mreq = m[reqpage]; 5666 object = mreq->object; 5667 error = 0; 5668 5669 KASSERT(vp->v_object == object, ("mismatching object")); 5670 5671 if (pcount > 1 && zp->z_blksz > PAGESIZE) { 5672 startoff = rounddown(IDX_TO_OFF(mreq->pindex), zp->z_blksz); 5673 reqstart = OFF_TO_IDX(round_page(startoff)); 5674 if (reqstart < m[0]->pindex) 5675 reqstart = 0; 5676 else 5677 reqstart = reqstart - m[0]->pindex; 5678 endoff = roundup(IDX_TO_OFF(mreq->pindex) + PAGE_SIZE, 5679 zp->z_blksz); 5680 reqend = OFF_TO_IDX(trunc_page(endoff)) - 1; 5681 if (reqend > m[pcount - 1]->pindex) 5682 reqend = m[pcount - 1]->pindex; 5683 reqsize = reqend - m[reqstart]->pindex + 1; 5684 KASSERT(reqstart <= reqpage && reqpage < reqstart + reqsize, 5685 ("reqpage beyond [reqstart, reqstart + reqsize[ bounds")); 5686 } else { 5687 reqstart = reqpage; 5688 reqsize = 1; 5689 } 5690 mfirst = m[reqstart]; 5691 mlast = m[reqstart + reqsize - 1]; 5692 5693 zfs_vmobject_wlock(object); 5694 5695 for (i = 0; i < reqstart; i++) { 5696 vm_page_lock(m[i]); 5697 vm_page_free(m[i]); 5698 vm_page_unlock(m[i]); 5699 } 5700 for (i = reqstart + reqsize; i < pcount; i++) { 5701 vm_page_lock(m[i]); 5702 vm_page_free(m[i]); 5703 vm_page_unlock(m[i]); 5704 } 5705 5706 if (mreq->valid && reqsize == 1) { 5707 if (mreq->valid != VM_PAGE_BITS_ALL) 5708 vm_page_zero_invalid(mreq, TRUE); 5709 zfs_vmobject_wunlock(object); 5710 ZFS_EXIT(zfsvfs); 5711 return (zfs_vm_pagerret_ok); 5712 } 5713 5714 PCPU_INC(cnt.v_vnodein); 5715 PCPU_ADD(cnt.v_vnodepgsin, reqsize); 5716 5717 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) { 5718 for (i = reqstart; i < reqstart + reqsize; i++) { 5719 if (i != reqpage) { 5720 vm_page_lock(m[i]); 5721 vm_page_free(m[i]); 5722 vm_page_unlock(m[i]); 5723 } 5724 } 5725 zfs_vmobject_wunlock(object); 5726 ZFS_EXIT(zfsvfs); 5727 return (zfs_vm_pagerret_bad); 5728 } 5729 5730 lsize = PAGE_SIZE; 5731 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size) 5732 lsize = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mlast->pindex); 5733 5734 zfs_vmobject_wunlock(object); 5735 5736 for (i = reqstart; i < reqstart + reqsize; i++) { 5737 size = PAGE_SIZE; 5738 if (i == (reqstart + reqsize - 1)) 5739 size = lsize; 5740 va = zfs_map_page(m[i], &sf); 5741 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex), 5742 size, va, DMU_READ_PREFETCH); 5743 if (size != PAGE_SIZE) 5744 bzero(va + size, PAGE_SIZE - size); 5745 zfs_unmap_page(sf); 5746 if (error != 0) 5747 break; 5748 } 5749 5750 zfs_vmobject_wlock(object); 5751 5752 for (i = reqstart; i < reqstart + reqsize; i++) { 5753 if (!error) 5754 m[i]->valid = VM_PAGE_BITS_ALL; 5755 KASSERT(m[i]->dirty == 0, ("zfs_getpages: page %p is dirty", m[i])); 5756 if (i != reqpage) 5757 vm_page_readahead_finish(m[i]); 5758 } 5759 5760 zfs_vmobject_wunlock(object); 5761 5762 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 5763 ZFS_EXIT(zfsvfs); 5764 return (error ? zfs_vm_pagerret_error : zfs_vm_pagerret_ok); 5765} 5766 5767static int 5768zfs_freebsd_getpages(ap) 5769 struct vop_getpages_args /* { 5770 struct vnode *a_vp; 5771 vm_page_t *a_m; 5772 int a_count; 5773 int a_reqpage; 5774 vm_ooffset_t a_offset; 5775 } */ *ap; 5776{ 5777 5778 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage)); 5779} 5780 5781static int 5782zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags, 5783 int *rtvals) 5784{ 5785 znode_t *zp = VTOZ(vp); 5786 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5787 rl_t *rl; 5788 dmu_tx_t *tx; 5789 struct sf_buf *sf; 5790 vm_object_t object; 5791 vm_page_t m; 5792 caddr_t va; 5793 size_t tocopy; 5794 size_t lo_len; 5795 vm_ooffset_t lo_off; 5796 vm_ooffset_t off; 5797 uint_t blksz; 5798 int ncount; 5799 int pcount; 5800 int err; 5801 int i; 5802 5803 ZFS_ENTER(zfsvfs); 5804 ZFS_VERIFY_ZP(zp); 5805 5806 object = vp->v_object; 5807 pcount = btoc(len); 5808 ncount = pcount; 5809 5810 KASSERT(ma[0]->object == object, ("mismatching object")); 5811 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length")); 5812 5813 for (i = 0; i < pcount; i++) 5814 rtvals[i] = zfs_vm_pagerret_error; 5815 5816 off = IDX_TO_OFF(ma[0]->pindex); 5817 blksz = zp->z_blksz; 5818 lo_off = rounddown(off, blksz); 5819 lo_len = roundup(len + (off - lo_off), blksz); 5820 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER); 5821 5822 zfs_vmobject_wlock(object); 5823 if (len + off > object->un_pager.vnp.vnp_size) { 5824 if (object->un_pager.vnp.vnp_size > off) { 5825 int pgoff; 5826 5827 len = object->un_pager.vnp.vnp_size - off; 5828 ncount = btoc(len); 5829 if ((pgoff = (int)len & PAGE_MASK) != 0) { 5830 /* 5831 * If the object is locked and the following 5832 * conditions hold, then the page's dirty 5833 * field cannot be concurrently changed by a 5834 * pmap operation. 5835 */ 5836 m = ma[ncount - 1]; 5837 vm_page_assert_sbusied(m); 5838 KASSERT(!pmap_page_is_write_mapped(m), 5839 ("zfs_putpages: page %p is not read-only", m)); 5840 vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 5841 pgoff); 5842 } 5843 } else { 5844 len = 0; 5845 ncount = 0; 5846 } 5847 if (ncount < pcount) { 5848 for (i = ncount; i < pcount; i++) { 5849 rtvals[i] = zfs_vm_pagerret_bad; 5850 } 5851 } 5852 } 5853 zfs_vmobject_wunlock(object); 5854 5855 if (ncount == 0) 5856 goto out; 5857 5858 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 5859 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 5860 goto out; 5861 } 5862 5863top: 5864 tx = dmu_tx_create(zfsvfs->z_os); 5865 dmu_tx_hold_write(tx, zp->z_id, off, len); 5866 5867 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 5868 zfs_sa_upgrade_txholds(tx, zp); 5869 err = dmu_tx_assign(tx, TXG_NOWAIT); 5870 if (err != 0) { 5871 if (err == ERESTART) { 5872 dmu_tx_wait(tx); 5873 dmu_tx_abort(tx); 5874 goto top; 5875 } 5876 dmu_tx_abort(tx); 5877 goto out; 5878 } 5879 5880 if (zp->z_blksz < PAGE_SIZE) { 5881 i = 0; 5882 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) { 5883 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len; 5884 va = zfs_map_page(ma[i], &sf); 5885 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx); 5886 zfs_unmap_page(sf); 5887 } 5888 } else { 5889 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx); 5890 } 5891 5892 if (err == 0) { 5893 uint64_t mtime[2], ctime[2]; 5894 sa_bulk_attr_t bulk[3]; 5895 int count = 0; 5896 5897 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 5898 &mtime, 16); 5899 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 5900 &ctime, 16); 5901 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 5902 &zp->z_pflags, 8); 5903 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 5904 B_TRUE); 5905 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 5906 5907 zfs_vmobject_wlock(object); 5908 for (i = 0; i < ncount; i++) { 5909 rtvals[i] = zfs_vm_pagerret_ok; 5910 vm_page_undirty(ma[i]); 5911 } 5912 zfs_vmobject_wunlock(object); 5913 PCPU_INC(cnt.v_vnodeout); 5914 PCPU_ADD(cnt.v_vnodepgsout, ncount); 5915 } 5916 dmu_tx_commit(tx); 5917 5918out: 5919 zfs_range_unlock(rl); 5920 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 || 5921 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 5922 zil_commit(zfsvfs->z_log, zp->z_id); 5923 ZFS_EXIT(zfsvfs); 5924 return (rtvals[0]); 5925} 5926 5927int 5928zfs_freebsd_putpages(ap) 5929 struct vop_putpages_args /* { 5930 struct vnode *a_vp; 5931 vm_page_t *a_m; 5932 int a_count; 5933 int a_sync; 5934 int *a_rtvals; 5935 vm_ooffset_t a_offset; 5936 } */ *ap; 5937{ 5938 5939 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync, 5940 ap->a_rtvals)); 5941} 5942 5943static int 5944zfs_freebsd_bmap(ap) 5945 struct vop_bmap_args /* { 5946 struct vnode *a_vp; 5947 daddr_t a_bn; 5948 struct bufobj **a_bop; 5949 daddr_t *a_bnp; 5950 int *a_runp; 5951 int *a_runb; 5952 } */ *ap; 5953{ 5954 5955 if (ap->a_bop != NULL) 5956 *ap->a_bop = &ap->a_vp->v_bufobj; 5957 if (ap->a_bnp != NULL) 5958 *ap->a_bnp = ap->a_bn; 5959 if (ap->a_runp != NULL) 5960 *ap->a_runp = 0; 5961 if (ap->a_runb != NULL) 5962 *ap->a_runb = 0; 5963 5964 return (0); 5965} 5966 5967static int 5968zfs_freebsd_open(ap) 5969 struct vop_open_args /* { 5970 struct vnode *a_vp; 5971 int a_mode; 5972 struct ucred *a_cred; 5973 struct thread *a_td; 5974 } */ *ap; 5975{ 5976 vnode_t *vp = ap->a_vp; 5977 znode_t *zp = VTOZ(vp); 5978 int error; 5979 5980 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL); 5981 if (error == 0) 5982 vnode_create_vobject(vp, zp->z_size, ap->a_td); 5983 return (error); 5984} 5985 5986static int 5987zfs_freebsd_close(ap) 5988 struct vop_close_args /* { 5989 struct vnode *a_vp; 5990 int a_fflag; 5991 struct ucred *a_cred; 5992 struct thread *a_td; 5993 } */ *ap; 5994{ 5995 5996 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL)); 5997} 5998 5999static int 6000zfs_freebsd_ioctl(ap) 6001 struct vop_ioctl_args /* { 6002 struct vnode *a_vp; 6003 u_long a_command; 6004 caddr_t a_data; 6005 int a_fflag; 6006 struct ucred *cred; 6007 struct thread *td; 6008 } */ *ap; 6009{ 6010 6011 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data, 6012 ap->a_fflag, ap->a_cred, NULL, NULL)); 6013} 6014 6015static int 6016zfs_freebsd_read(ap) 6017 struct vop_read_args /* { 6018 struct vnode *a_vp; 6019 struct uio *a_uio; 6020 int a_ioflag; 6021 struct ucred *a_cred; 6022 } */ *ap; 6023{ 6024 6025 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag), 6026 ap->a_cred, NULL)); 6027} 6028 6029static int 6030zfs_freebsd_write(ap) 6031 struct vop_write_args /* { 6032 struct vnode *a_vp; 6033 struct uio *a_uio; 6034 int a_ioflag; 6035 struct ucred *a_cred; 6036 } */ *ap; 6037{ 6038 6039 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag), 6040 ap->a_cred, NULL)); 6041} 6042 6043static int 6044zfs_freebsd_access(ap) 6045 struct vop_access_args /* { 6046 struct vnode *a_vp; 6047 accmode_t a_accmode; 6048 struct ucred *a_cred; 6049 struct thread *a_td; 6050 } */ *ap; 6051{ 6052 vnode_t *vp = ap->a_vp; 6053 znode_t *zp = VTOZ(vp); 6054 accmode_t accmode; 6055 int error = 0; 6056 6057 /* 6058 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND, 6059 */ 6060 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND); 6061 if (accmode != 0) 6062 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL); 6063 6064 /* 6065 * VADMIN has to be handled by vaccess(). 6066 */ 6067 if (error == 0) { 6068 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND); 6069 if (accmode != 0) { 6070 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid, 6071 zp->z_gid, accmode, ap->a_cred, NULL); 6072 } 6073 } 6074 6075 /* 6076 * For VEXEC, ensure that at least one execute bit is set for 6077 * non-directories. 6078 */ 6079 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR && 6080 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) { 6081 error = EACCES; 6082 } 6083 6084 return (error); 6085} 6086 6087static int 6088zfs_freebsd_lookup(ap) 6089 struct vop_lookup_args /* { 6090 struct vnode *a_dvp; 6091 struct vnode **a_vpp; 6092 struct componentname *a_cnp; 6093 } */ *ap; 6094{ 6095 struct componentname *cnp = ap->a_cnp; 6096 char nm[NAME_MAX + 1]; 6097 6098 ASSERT(cnp->cn_namelen < sizeof(nm)); 6099 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm))); 6100 6101 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop, 6102 cnp->cn_cred, cnp->cn_thread, 0)); 6103} 6104 6105static int 6106zfs_freebsd_create(ap) 6107 struct vop_create_args /* { 6108 struct vnode *a_dvp; 6109 struct vnode **a_vpp; 6110 struct componentname *a_cnp; 6111 struct vattr *a_vap; 6112 } */ *ap; 6113{ 6114 struct componentname *cnp = ap->a_cnp; 6115 vattr_t *vap = ap->a_vap; 6116 int mode; 6117 6118 ASSERT(cnp->cn_flags & SAVENAME); 6119 6120 vattr_init_mask(vap); 6121 mode = vap->va_mode & ALLPERMS; 6122 6123 return (zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode, 6124 ap->a_vpp, cnp->cn_cred, cnp->cn_thread)); 6125} 6126 6127static int 6128zfs_freebsd_remove(ap) 6129 struct vop_remove_args /* { 6130 struct vnode *a_dvp; 6131 struct vnode *a_vp; 6132 struct componentname *a_cnp; 6133 } */ *ap; 6134{ 6135 6136 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 6137 6138 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr, 6139 ap->a_cnp->cn_cred, NULL, 0)); 6140} 6141 6142static int 6143zfs_freebsd_mkdir(ap) 6144 struct vop_mkdir_args /* { 6145 struct vnode *a_dvp; 6146 struct vnode **a_vpp; 6147 struct componentname *a_cnp; 6148 struct vattr *a_vap; 6149 } */ *ap; 6150{ 6151 vattr_t *vap = ap->a_vap; 6152 6153 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 6154 6155 vattr_init_mask(vap); 6156 6157 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp, 6158 ap->a_cnp->cn_cred, NULL, 0, NULL)); 6159} 6160 6161static int 6162zfs_freebsd_rmdir(ap) 6163 struct vop_rmdir_args /* { 6164 struct vnode *a_dvp; 6165 struct vnode *a_vp; 6166 struct componentname *a_cnp; 6167 } */ *ap; 6168{ 6169 struct componentname *cnp = ap->a_cnp; 6170 6171 ASSERT(cnp->cn_flags & SAVENAME); 6172 6173 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0)); 6174} 6175 6176static int 6177zfs_freebsd_readdir(ap) 6178 struct vop_readdir_args /* { 6179 struct vnode *a_vp; 6180 struct uio *a_uio; 6181 struct ucred *a_cred; 6182 int *a_eofflag; 6183 int *a_ncookies; 6184 u_long **a_cookies; 6185 } */ *ap; 6186{ 6187 6188 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag, 6189 ap->a_ncookies, ap->a_cookies)); 6190} 6191 6192static int 6193zfs_freebsd_fsync(ap) 6194 struct vop_fsync_args /* { 6195 struct vnode *a_vp; 6196 int a_waitfor; 6197 struct thread *a_td; 6198 } */ *ap; 6199{ 6200 6201 vop_stdfsync(ap); 6202 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL)); 6203} 6204 6205static int 6206zfs_freebsd_getattr(ap) 6207 struct vop_getattr_args /* { 6208 struct vnode *a_vp; 6209 struct vattr *a_vap; 6210 struct ucred *a_cred; 6211 } */ *ap; 6212{ 6213 vattr_t *vap = ap->a_vap; 6214 xvattr_t xvap; 6215 u_long fflags = 0; 6216 int error; 6217 6218 xva_init(&xvap); 6219 xvap.xva_vattr = *vap; 6220 xvap.xva_vattr.va_mask |= AT_XVATTR; 6221 6222 /* Convert chflags into ZFS-type flags. */ 6223 /* XXX: what about SF_SETTABLE?. */ 6224 XVA_SET_REQ(&xvap, XAT_IMMUTABLE); 6225 XVA_SET_REQ(&xvap, XAT_APPENDONLY); 6226 XVA_SET_REQ(&xvap, XAT_NOUNLINK); 6227 XVA_SET_REQ(&xvap, XAT_NODUMP); 6228 XVA_SET_REQ(&xvap, XAT_READONLY); 6229 XVA_SET_REQ(&xvap, XAT_ARCHIVE); 6230 XVA_SET_REQ(&xvap, XAT_SYSTEM); 6231 XVA_SET_REQ(&xvap, XAT_HIDDEN); 6232 XVA_SET_REQ(&xvap, XAT_REPARSE); 6233 XVA_SET_REQ(&xvap, XAT_OFFLINE); 6234 XVA_SET_REQ(&xvap, XAT_SPARSE); 6235 6236 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL); 6237 if (error != 0) 6238 return (error); 6239 6240 /* Convert ZFS xattr into chflags. */ 6241#define FLAG_CHECK(fflag, xflag, xfield) do { \ 6242 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \ 6243 fflags |= (fflag); \ 6244} while (0) 6245 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE, 6246 xvap.xva_xoptattrs.xoa_immutable); 6247 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY, 6248 xvap.xva_xoptattrs.xoa_appendonly); 6249 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK, 6250 xvap.xva_xoptattrs.xoa_nounlink); 6251 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE, 6252 xvap.xva_xoptattrs.xoa_archive); 6253 FLAG_CHECK(UF_NODUMP, XAT_NODUMP, 6254 xvap.xva_xoptattrs.xoa_nodump); 6255 FLAG_CHECK(UF_READONLY, XAT_READONLY, 6256 xvap.xva_xoptattrs.xoa_readonly); 6257 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM, 6258 xvap.xva_xoptattrs.xoa_system); 6259 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN, 6260 xvap.xva_xoptattrs.xoa_hidden); 6261 FLAG_CHECK(UF_REPARSE, XAT_REPARSE, 6262 xvap.xva_xoptattrs.xoa_reparse); 6263 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE, 6264 xvap.xva_xoptattrs.xoa_offline); 6265 FLAG_CHECK(UF_SPARSE, XAT_SPARSE, 6266 xvap.xva_xoptattrs.xoa_sparse); 6267 6268#undef FLAG_CHECK 6269 *vap = xvap.xva_vattr; 6270 vap->va_flags = fflags; 6271 return (0); 6272} 6273 6274static int 6275zfs_freebsd_setattr(ap) 6276 struct vop_setattr_args /* { 6277 struct vnode *a_vp; 6278 struct vattr *a_vap; 6279 struct ucred *a_cred; 6280 } */ *ap; 6281{ 6282 vnode_t *vp = ap->a_vp; 6283 vattr_t *vap = ap->a_vap; 6284 cred_t *cred = ap->a_cred; 6285 xvattr_t xvap; 6286 u_long fflags; 6287 uint64_t zflags; 6288 6289 vattr_init_mask(vap); 6290 vap->va_mask &= ~AT_NOSET; 6291 6292 xva_init(&xvap); 6293 xvap.xva_vattr = *vap; 6294 6295 zflags = VTOZ(vp)->z_pflags; 6296 6297 if (vap->va_flags != VNOVAL) { 6298 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs; 6299 int error; 6300 6301 if (zfsvfs->z_use_fuids == B_FALSE) 6302 return (EOPNOTSUPP); 6303 6304 fflags = vap->va_flags; 6305 /* 6306 * XXX KDM 6307 * We need to figure out whether it makes sense to allow 6308 * UF_REPARSE through, since we don't really have other 6309 * facilities to handle reparse points and zfs_setattr() 6310 * doesn't currently allow setting that attribute anyway. 6311 */ 6312 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE| 6313 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE| 6314 UF_OFFLINE|UF_SPARSE)) != 0) 6315 return (EOPNOTSUPP); 6316 /* 6317 * Unprivileged processes are not permitted to unset system 6318 * flags, or modify flags if any system flags are set. 6319 * Privileged non-jail processes may not modify system flags 6320 * if securelevel > 0 and any existing system flags are set. 6321 * Privileged jail processes behave like privileged non-jail 6322 * processes if the security.jail.chflags_allowed sysctl is 6323 * is non-zero; otherwise, they behave like unprivileged 6324 * processes. 6325 */ 6326 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 || 6327 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) { 6328 if (zflags & 6329 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) { 6330 error = securelevel_gt(cred, 0); 6331 if (error != 0) 6332 return (error); 6333 } 6334 } else { 6335 /* 6336 * Callers may only modify the file flags on objects they 6337 * have VADMIN rights for. 6338 */ 6339 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0) 6340 return (error); 6341 if (zflags & 6342 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) { 6343 return (EPERM); 6344 } 6345 if (fflags & 6346 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) { 6347 return (EPERM); 6348 } 6349 } 6350 6351#define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \ 6352 if (((fflags & (fflag)) && !(zflags & (zflag))) || \ 6353 ((zflags & (zflag)) && !(fflags & (fflag)))) { \ 6354 XVA_SET_REQ(&xvap, (xflag)); \ 6355 (xfield) = ((fflags & (fflag)) != 0); \ 6356 } \ 6357} while (0) 6358 /* Convert chflags into ZFS-type flags. */ 6359 /* XXX: what about SF_SETTABLE?. */ 6360 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE, 6361 xvap.xva_xoptattrs.xoa_immutable); 6362 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY, 6363 xvap.xva_xoptattrs.xoa_appendonly); 6364 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK, 6365 xvap.xva_xoptattrs.xoa_nounlink); 6366 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE, 6367 xvap.xva_xoptattrs.xoa_archive); 6368 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP, 6369 xvap.xva_xoptattrs.xoa_nodump); 6370 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY, 6371 xvap.xva_xoptattrs.xoa_readonly); 6372 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM, 6373 xvap.xva_xoptattrs.xoa_system); 6374 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN, 6375 xvap.xva_xoptattrs.xoa_hidden); 6376 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE, 6377 xvap.xva_xoptattrs.xoa_hidden); 6378 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE, 6379 xvap.xva_xoptattrs.xoa_offline); 6380 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE, 6381 xvap.xva_xoptattrs.xoa_sparse); 6382#undef FLAG_CHANGE 6383 } 6384 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL)); 6385} 6386 6387static int 6388zfs_freebsd_rename(ap) 6389 struct vop_rename_args /* { 6390 struct vnode *a_fdvp; 6391 struct vnode *a_fvp; 6392 struct componentname *a_fcnp; 6393 struct vnode *a_tdvp; 6394 struct vnode *a_tvp; 6395 struct componentname *a_tcnp; 6396 } */ *ap; 6397{ 6398 vnode_t *fdvp = ap->a_fdvp; 6399 vnode_t *fvp = ap->a_fvp; 6400 vnode_t *tdvp = ap->a_tdvp; 6401 vnode_t *tvp = ap->a_tvp; 6402 int error; 6403 6404 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART)); 6405 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART)); 6406 6407 /* 6408 * Check for cross-device rename. 6409 */ 6410 if ((fdvp->v_mount != tdvp->v_mount) || 6411 (tvp && (fdvp->v_mount != tvp->v_mount))) 6412 error = EXDEV; 6413 else 6414 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp, 6415 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0); 6416 if (tdvp == tvp) 6417 VN_RELE(tdvp); 6418 else 6419 VN_URELE(tdvp); 6420 if (tvp) 6421 VN_URELE(tvp); 6422 VN_RELE(fdvp); 6423 VN_RELE(fvp); 6424 6425 return (error); 6426} 6427 6428static int 6429zfs_freebsd_symlink(ap) 6430 struct vop_symlink_args /* { 6431 struct vnode *a_dvp; 6432 struct vnode **a_vpp; 6433 struct componentname *a_cnp; 6434 struct vattr *a_vap; 6435 char *a_target; 6436 } */ *ap; 6437{ 6438 struct componentname *cnp = ap->a_cnp; 6439 vattr_t *vap = ap->a_vap; 6440 6441 ASSERT(cnp->cn_flags & SAVENAME); 6442 6443 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */ 6444 vattr_init_mask(vap); 6445 6446 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap, 6447 ap->a_target, cnp->cn_cred, cnp->cn_thread)); 6448} 6449 6450static int 6451zfs_freebsd_readlink(ap) 6452 struct vop_readlink_args /* { 6453 struct vnode *a_vp; 6454 struct uio *a_uio; 6455 struct ucred *a_cred; 6456 } */ *ap; 6457{ 6458 6459 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL)); 6460} 6461 6462static int 6463zfs_freebsd_link(ap) 6464 struct vop_link_args /* { 6465 struct vnode *a_tdvp; 6466 struct vnode *a_vp; 6467 struct componentname *a_cnp; 6468 } */ *ap; 6469{ 6470 struct componentname *cnp = ap->a_cnp; 6471 vnode_t *vp = ap->a_vp; 6472 vnode_t *tdvp = ap->a_tdvp; 6473 6474 if (tdvp->v_mount != vp->v_mount) 6475 return (EXDEV); 6476 6477 ASSERT(cnp->cn_flags & SAVENAME); 6478 6479 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0)); 6480} 6481 6482static int 6483zfs_freebsd_inactive(ap) 6484 struct vop_inactive_args /* { 6485 struct vnode *a_vp; 6486 struct thread *a_td; 6487 } */ *ap; 6488{ 6489 vnode_t *vp = ap->a_vp; 6490 6491 zfs_inactive(vp, ap->a_td->td_ucred, NULL); 6492 return (0); 6493} 6494 6495static int 6496zfs_freebsd_reclaim(ap) 6497 struct vop_reclaim_args /* { 6498 struct vnode *a_vp; 6499 struct thread *a_td; 6500 } */ *ap; 6501{ 6502 vnode_t *vp = ap->a_vp; 6503 znode_t *zp = VTOZ(vp); 6504 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 6505 6506 ASSERT(zp != NULL); 6507 6508 /* Destroy the vm object and flush associated pages. */ 6509 vnode_destroy_vobject(vp); 6510 6511 /* 6512 * z_teardown_inactive_lock protects from a race with 6513 * zfs_znode_dmu_fini in zfsvfs_teardown during 6514 * force unmount. 6515 */ 6516 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 6517 if (zp->z_sa_hdl == NULL) 6518 zfs_znode_free(zp); 6519 else 6520 zfs_zinactive(zp); 6521 rw_exit(&zfsvfs->z_teardown_inactive_lock); 6522 6523 vp->v_data = NULL; 6524 return (0); 6525} 6526 6527static int 6528zfs_freebsd_fid(ap) 6529 struct vop_fid_args /* { 6530 struct vnode *a_vp; 6531 struct fid *a_fid; 6532 } */ *ap; 6533{ 6534 6535 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL)); 6536} 6537 6538static int 6539zfs_freebsd_pathconf(ap) 6540 struct vop_pathconf_args /* { 6541 struct vnode *a_vp; 6542 int a_name; 6543 register_t *a_retval; 6544 } */ *ap; 6545{ 6546 ulong_t val; 6547 int error; 6548 6549 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL); 6550 if (error == 0) 6551 *ap->a_retval = val; 6552 else if (error == EOPNOTSUPP) 6553 error = vop_stdpathconf(ap); 6554 return (error); 6555} 6556 6557static int 6558zfs_freebsd_fifo_pathconf(ap) 6559 struct vop_pathconf_args /* { 6560 struct vnode *a_vp; 6561 int a_name; 6562 register_t *a_retval; 6563 } */ *ap; 6564{ 6565 6566 switch (ap->a_name) { 6567 case _PC_ACL_EXTENDED: 6568 case _PC_ACL_NFS4: 6569 case _PC_ACL_PATH_MAX: 6570 case _PC_MAC_PRESENT: 6571 return (zfs_freebsd_pathconf(ap)); 6572 default: 6573 return (fifo_specops.vop_pathconf(ap)); 6574 } 6575} 6576 6577/* 6578 * FreeBSD's extended attributes namespace defines file name prefix for ZFS' 6579 * extended attribute name: 6580 * 6581 * NAMESPACE PREFIX 6582 * system freebsd:system: 6583 * user (none, can be used to access ZFS fsattr(5) attributes 6584 * created on Solaris) 6585 */ 6586static int 6587zfs_create_attrname(int attrnamespace, const char *name, char *attrname, 6588 size_t size) 6589{ 6590 const char *namespace, *prefix, *suffix; 6591 6592 /* We don't allow '/' character in attribute name. */ 6593 if (strchr(name, '/') != NULL) 6594 return (EINVAL); 6595 /* We don't allow attribute names that start with "freebsd:" string. */ 6596 if (strncmp(name, "freebsd:", 8) == 0) 6597 return (EINVAL); 6598 6599 bzero(attrname, size); 6600 6601 switch (attrnamespace) { 6602 case EXTATTR_NAMESPACE_USER: 6603#if 0 6604 prefix = "freebsd:"; 6605 namespace = EXTATTR_NAMESPACE_USER_STRING; 6606 suffix = ":"; 6607#else 6608 /* 6609 * This is the default namespace by which we can access all 6610 * attributes created on Solaris. 6611 */ 6612 prefix = namespace = suffix = ""; 6613#endif 6614 break; 6615 case EXTATTR_NAMESPACE_SYSTEM: 6616 prefix = "freebsd:"; 6617 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING; 6618 suffix = ":"; 6619 break; 6620 case EXTATTR_NAMESPACE_EMPTY: 6621 default: 6622 return (EINVAL); 6623 } 6624 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix, 6625 name) >= size) { 6626 return (ENAMETOOLONG); 6627 } 6628 return (0); 6629} 6630 6631/* 6632 * Vnode operating to retrieve a named extended attribute. 6633 */ 6634static int 6635zfs_getextattr(struct vop_getextattr_args *ap) 6636/* 6637vop_getextattr { 6638 IN struct vnode *a_vp; 6639 IN int a_attrnamespace; 6640 IN const char *a_name; 6641 INOUT struct uio *a_uio; 6642 OUT size_t *a_size; 6643 IN struct ucred *a_cred; 6644 IN struct thread *a_td; 6645}; 6646*/ 6647{ 6648 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs; 6649 struct thread *td = ap->a_td; 6650 struct nameidata nd; 6651 char attrname[255]; 6652 struct vattr va; 6653 vnode_t *xvp = NULL, *vp; 6654 int error, flags; 6655 6656 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 6657 ap->a_cred, ap->a_td, VREAD); 6658 if (error != 0) 6659 return (error); 6660 6661 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 6662 sizeof(attrname)); 6663 if (error != 0) 6664 return (error); 6665 6666 ZFS_ENTER(zfsvfs); 6667 6668 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td, 6669 LOOKUP_XATTR); 6670 if (error != 0) { 6671 ZFS_EXIT(zfsvfs); 6672 return (error); 6673 } 6674 6675 flags = FREAD; 6676 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, 6677 xvp, td); 6678 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL); 6679 vp = nd.ni_vp; 6680 NDFREE(&nd, NDF_ONLY_PNBUF); 6681 if (error != 0) { 6682 ZFS_EXIT(zfsvfs); 6683 if (error == ENOENT) 6684 error = ENOATTR; 6685 return (error); 6686 } 6687 6688 if (ap->a_size != NULL) { 6689 error = VOP_GETATTR(vp, &va, ap->a_cred); 6690 if (error == 0) 6691 *ap->a_size = (size_t)va.va_size; 6692 } else if (ap->a_uio != NULL) 6693 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred); 6694 6695 VOP_UNLOCK(vp, 0); 6696 vn_close(vp, flags, ap->a_cred, td); 6697 ZFS_EXIT(zfsvfs); 6698 6699 return (error); 6700} 6701 6702/* 6703 * Vnode operation to remove a named attribute. 6704 */ 6705int 6706zfs_deleteextattr(struct vop_deleteextattr_args *ap) 6707/* 6708vop_deleteextattr { 6709 IN struct vnode *a_vp; 6710 IN int a_attrnamespace; 6711 IN const char *a_name; 6712 IN struct ucred *a_cred; 6713 IN struct thread *a_td; 6714}; 6715*/ 6716{ 6717 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs; 6718 struct thread *td = ap->a_td; 6719 struct nameidata nd; 6720 char attrname[255]; 6721 struct vattr va; 6722 vnode_t *xvp = NULL, *vp; 6723 int error, flags; 6724 6725 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 6726 ap->a_cred, ap->a_td, VWRITE); 6727 if (error != 0) 6728 return (error); 6729 6730 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 6731 sizeof(attrname)); 6732 if (error != 0) 6733 return (error); 6734 6735 ZFS_ENTER(zfsvfs); 6736 6737 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td, 6738 LOOKUP_XATTR); 6739 if (error != 0) { 6740 ZFS_EXIT(zfsvfs); 6741 return (error); 6742 } 6743 6744 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF, 6745 UIO_SYSSPACE, attrname, xvp, td); 6746 error = namei(&nd); 6747 vp = nd.ni_vp; 6748 NDFREE(&nd, NDF_ONLY_PNBUF); 6749 if (error != 0) { 6750 ZFS_EXIT(zfsvfs); 6751 if (error == ENOENT) 6752 error = ENOATTR; 6753 return (error); 6754 } 6755 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd); 6756 6757 vput(nd.ni_dvp); 6758 if (vp == nd.ni_dvp) 6759 vrele(vp); 6760 else 6761 vput(vp); 6762 ZFS_EXIT(zfsvfs); 6763 6764 return (error); 6765} 6766 6767/* 6768 * Vnode operation to set a named attribute. 6769 */ 6770static int 6771zfs_setextattr(struct vop_setextattr_args *ap) 6772/* 6773vop_setextattr { 6774 IN struct vnode *a_vp; 6775 IN int a_attrnamespace; 6776 IN const char *a_name; 6777 INOUT struct uio *a_uio; 6778 IN struct ucred *a_cred; 6779 IN struct thread *a_td; 6780}; 6781*/ 6782{ 6783 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs; 6784 struct thread *td = ap->a_td; 6785 struct nameidata nd; 6786 char attrname[255]; 6787 struct vattr va; 6788 vnode_t *xvp = NULL, *vp; 6789 int error, flags; 6790 6791 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 6792 ap->a_cred, ap->a_td, VWRITE); 6793 if (error != 0) 6794 return (error); 6795 6796 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 6797 sizeof(attrname)); 6798 if (error != 0) 6799 return (error); 6800 6801 ZFS_ENTER(zfsvfs); 6802 6803 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td, 6804 LOOKUP_XATTR | CREATE_XATTR_DIR); 6805 if (error != 0) { 6806 ZFS_EXIT(zfsvfs); 6807 return (error); 6808 } 6809 6810 flags = FFLAGS(O_WRONLY | O_CREAT); 6811 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, 6812 xvp, td); 6813 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL); 6814 vp = nd.ni_vp; 6815 NDFREE(&nd, NDF_ONLY_PNBUF); 6816 if (error != 0) { 6817 ZFS_EXIT(zfsvfs); 6818 return (error); 6819 } 6820 6821 VATTR_NULL(&va); 6822 va.va_size = 0; 6823 error = VOP_SETATTR(vp, &va, ap->a_cred); 6824 if (error == 0) 6825 VOP_WRITE(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred); 6826 6827 VOP_UNLOCK(vp, 0); 6828 vn_close(vp, flags, ap->a_cred, td); 6829 ZFS_EXIT(zfsvfs); 6830 6831 return (error); 6832} 6833 6834/* 6835 * Vnode operation to retrieve extended attributes on a vnode. 6836 */ 6837static int 6838zfs_listextattr(struct vop_listextattr_args *ap) 6839/* 6840vop_listextattr { 6841 IN struct vnode *a_vp; 6842 IN int a_attrnamespace; 6843 INOUT struct uio *a_uio; 6844 OUT size_t *a_size; 6845 IN struct ucred *a_cred; 6846 IN struct thread *a_td; 6847}; 6848*/ 6849{ 6850 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs; 6851 struct thread *td = ap->a_td; 6852 struct nameidata nd; 6853 char attrprefix[16]; 6854 u_char dirbuf[sizeof(struct dirent)]; 6855 struct dirent *dp; 6856 struct iovec aiov; 6857 struct uio auio, *uio = ap->a_uio; 6858 size_t *sizep = ap->a_size; 6859 size_t plen; 6860 vnode_t *xvp = NULL, *vp; 6861 int done, error, eof, pos; 6862 6863 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 6864 ap->a_cred, ap->a_td, VREAD); 6865 if (error != 0) 6866 return (error); 6867 6868 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix, 6869 sizeof(attrprefix)); 6870 if (error != 0) 6871 return (error); 6872 plen = strlen(attrprefix); 6873 6874 ZFS_ENTER(zfsvfs); 6875 6876 if (sizep != NULL) 6877 *sizep = 0; 6878 6879 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td, 6880 LOOKUP_XATTR); 6881 if (error != 0) { 6882 ZFS_EXIT(zfsvfs); 6883 /* 6884 * ENOATTR means that the EA directory does not yet exist, 6885 * i.e. there are no extended attributes there. 6886 */ 6887 if (error == ENOATTR) 6888 error = 0; 6889 return (error); 6890 } 6891 6892 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED, 6893 UIO_SYSSPACE, ".", xvp, td); 6894 error = namei(&nd); 6895 vp = nd.ni_vp; 6896 NDFREE(&nd, NDF_ONLY_PNBUF); 6897 if (error != 0) { 6898 ZFS_EXIT(zfsvfs); 6899 return (error); 6900 } 6901 6902 auio.uio_iov = &aiov; 6903 auio.uio_iovcnt = 1; 6904 auio.uio_segflg = UIO_SYSSPACE; 6905 auio.uio_td = td; 6906 auio.uio_rw = UIO_READ; 6907 auio.uio_offset = 0; 6908 6909 do { 6910 u_char nlen; 6911 6912 aiov.iov_base = (void *)dirbuf; 6913 aiov.iov_len = sizeof(dirbuf); 6914 auio.uio_resid = sizeof(dirbuf); 6915 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL); 6916 done = sizeof(dirbuf) - auio.uio_resid; 6917 if (error != 0) 6918 break; 6919 for (pos = 0; pos < done;) { 6920 dp = (struct dirent *)(dirbuf + pos); 6921 pos += dp->d_reclen; 6922 /* 6923 * XXX: Temporarily we also accept DT_UNKNOWN, as this 6924 * is what we get when attribute was created on Solaris. 6925 */ 6926 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN) 6927 continue; 6928 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0) 6929 continue; 6930 else if (strncmp(dp->d_name, attrprefix, plen) != 0) 6931 continue; 6932 nlen = dp->d_namlen - plen; 6933 if (sizep != NULL) 6934 *sizep += 1 + nlen; 6935 else if (uio != NULL) { 6936 /* 6937 * Format of extattr name entry is one byte for 6938 * length and the rest for name. 6939 */ 6940 error = uiomove(&nlen, 1, uio->uio_rw, uio); 6941 if (error == 0) { 6942 error = uiomove(dp->d_name + plen, nlen, 6943 uio->uio_rw, uio); 6944 } 6945 if (error != 0) 6946 break; 6947 } 6948 } 6949 } while (!eof && error == 0); 6950 6951 vput(vp); 6952 ZFS_EXIT(zfsvfs); 6953 6954 return (error); 6955} 6956 6957int 6958zfs_freebsd_getacl(ap) 6959 struct vop_getacl_args /* { 6960 struct vnode *vp; 6961 acl_type_t type; 6962 struct acl *aclp; 6963 struct ucred *cred; 6964 struct thread *td; 6965 } */ *ap; 6966{ 6967 int error; 6968 vsecattr_t vsecattr; 6969 6970 if (ap->a_type != ACL_TYPE_NFS4) 6971 return (EINVAL); 6972 6973 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT; 6974 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL)) 6975 return (error); 6976 6977 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt); 6978 if (vsecattr.vsa_aclentp != NULL) 6979 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz); 6980 6981 return (error); 6982} 6983 6984int 6985zfs_freebsd_setacl(ap) 6986 struct vop_setacl_args /* { 6987 struct vnode *vp; 6988 acl_type_t type; 6989 struct acl *aclp; 6990 struct ucred *cred; 6991 struct thread *td; 6992 } */ *ap; 6993{ 6994 int error; 6995 vsecattr_t vsecattr; 6996 int aclbsize; /* size of acl list in bytes */ 6997 aclent_t *aaclp; 6998 6999 if (ap->a_type != ACL_TYPE_NFS4) 7000 return (EINVAL); 7001 7002 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES) 7003 return (EINVAL); 7004 7005 /* 7006 * With NFSv4 ACLs, chmod(2) may need to add additional entries, 7007 * splitting every entry into two and appending "canonical six" 7008 * entries at the end. Don't allow for setting an ACL that would 7009 * cause chmod(2) to run out of ACL entries. 7010 */ 7011 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES) 7012 return (ENOSPC); 7013 7014 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR); 7015 if (error != 0) 7016 return (error); 7017 7018 vsecattr.vsa_mask = VSA_ACE; 7019 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t); 7020 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP); 7021 aaclp = vsecattr.vsa_aclentp; 7022 vsecattr.vsa_aclentsz = aclbsize; 7023 7024 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp); 7025 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL); 7026 kmem_free(aaclp, aclbsize); 7027 7028 return (error); 7029} 7030 7031int 7032zfs_freebsd_aclcheck(ap) 7033 struct vop_aclcheck_args /* { 7034 struct vnode *vp; 7035 acl_type_t type; 7036 struct acl *aclp; 7037 struct ucred *cred; 7038 struct thread *td; 7039 } */ *ap; 7040{ 7041 7042 return (EOPNOTSUPP); 7043} 7044 7045struct vop_vector zfs_vnodeops; 7046struct vop_vector zfs_fifoops; 7047struct vop_vector zfs_shareops; 7048 7049struct vop_vector zfs_vnodeops = { 7050 .vop_default = &default_vnodeops, 7051 .vop_inactive = zfs_freebsd_inactive, 7052 .vop_reclaim = zfs_freebsd_reclaim, 7053 .vop_access = zfs_freebsd_access, 7054#ifdef FREEBSD_NAMECACHE 7055 .vop_lookup = vfs_cache_lookup, 7056 .vop_cachedlookup = zfs_freebsd_lookup, 7057#else 7058 .vop_lookup = zfs_freebsd_lookup, 7059#endif 7060 .vop_getattr = zfs_freebsd_getattr, 7061 .vop_setattr = zfs_freebsd_setattr, 7062 .vop_create = zfs_freebsd_create, 7063 .vop_mknod = zfs_freebsd_create, 7064 .vop_mkdir = zfs_freebsd_mkdir, 7065 .vop_readdir = zfs_freebsd_readdir, 7066 .vop_fsync = zfs_freebsd_fsync, 7067 .vop_open = zfs_freebsd_open, 7068 .vop_close = zfs_freebsd_close, 7069 .vop_rmdir = zfs_freebsd_rmdir, 7070 .vop_ioctl = zfs_freebsd_ioctl, 7071 .vop_link = zfs_freebsd_link, 7072 .vop_symlink = zfs_freebsd_symlink, 7073 .vop_readlink = zfs_freebsd_readlink, 7074 .vop_read = zfs_freebsd_read, 7075 .vop_write = zfs_freebsd_write, 7076 .vop_remove = zfs_freebsd_remove, 7077 .vop_rename = zfs_freebsd_rename, 7078 .vop_pathconf = zfs_freebsd_pathconf, 7079 .vop_bmap = zfs_freebsd_bmap, 7080 .vop_fid = zfs_freebsd_fid, 7081 .vop_getextattr = zfs_getextattr, 7082 .vop_deleteextattr = zfs_deleteextattr, 7083 .vop_setextattr = zfs_setextattr, 7084 .vop_listextattr = zfs_listextattr, 7085 .vop_getacl = zfs_freebsd_getacl, 7086 .vop_setacl = zfs_freebsd_setacl, 7087 .vop_aclcheck = zfs_freebsd_aclcheck, 7088 .vop_getpages = zfs_freebsd_getpages, 7089 .vop_putpages = zfs_freebsd_putpages, 7090}; 7091 7092struct vop_vector zfs_fifoops = { 7093 .vop_default = &fifo_specops, 7094 .vop_fsync = zfs_freebsd_fsync, 7095 .vop_access = zfs_freebsd_access, 7096 .vop_getattr = zfs_freebsd_getattr, 7097 .vop_inactive = zfs_freebsd_inactive, 7098 .vop_read = VOP_PANIC, 7099 .vop_reclaim = zfs_freebsd_reclaim, 7100 .vop_setattr = zfs_freebsd_setattr, 7101 .vop_write = VOP_PANIC, 7102 .vop_pathconf = zfs_freebsd_fifo_pathconf, 7103 .vop_fid = zfs_freebsd_fid, 7104 .vop_getacl = zfs_freebsd_getacl, 7105 .vop_setacl = zfs_freebsd_setacl, 7106 .vop_aclcheck = zfs_freebsd_aclcheck, 7107}; 7108 7109/* 7110 * special share hidden files vnode operations template 7111 */ 7112struct vop_vector zfs_shareops = { 7113 .vop_default = &default_vnodeops, 7114 .vop_access = zfs_freebsd_access, 7115 .vop_inactive = zfs_freebsd_inactive, 7116 .vop_reclaim = zfs_freebsd_reclaim, 7117 .vop_fid = zfs_freebsd_fid, 7118 .vop_pathconf = zfs_freebsd_pathconf, 7119}; 7120