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