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 https://opensource.org/licenses/CDDL-1.0. 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 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved. 25 * Copyright (c) 2014 Integros [integros.com] 26 * Copyright 2017 Nexenta Systems, Inc. 27 */ 28 29 /* Portions Copyright 2007 Jeremy Teo */ 30 /* Portions Copyright 2010 Robert Milkowski */ 31 32 #include <sys/param.h> 33 #include <sys/time.h> 34 #include <sys/systm.h> 35 #include <sys/sysmacros.h> 36 #include <sys/resource.h> 37 #include <security/mac/mac_framework.h> 38 #include <sys/vfs.h> 39 #include <sys/endian.h> 40 #include <sys/vm.h> 41 #include <sys/vnode.h> 42 #if __FreeBSD_version >= 1300102 43 #include <sys/smr.h> 44 #endif 45 #include <sys/dirent.h> 46 #include <sys/file.h> 47 #include <sys/stat.h> 48 #include <sys/kmem.h> 49 #include <sys/taskq.h> 50 #include <sys/uio.h> 51 #include <sys/atomic.h> 52 #include <sys/namei.h> 53 #include <sys/mman.h> 54 #include <sys/cmn_err.h> 55 #include <sys/kdb.h> 56 #include <sys/sysproto.h> 57 #include <sys/errno.h> 58 #include <sys/unistd.h> 59 #include <sys/zfs_dir.h> 60 #include <sys/zfs_ioctl.h> 61 #include <sys/fs/zfs.h> 62 #include <sys/dmu.h> 63 #include <sys/dmu_objset.h> 64 #include <sys/spa.h> 65 #include <sys/txg.h> 66 #include <sys/dbuf.h> 67 #include <sys/zap.h> 68 #include <sys/sa.h> 69 #include <sys/policy.h> 70 #include <sys/sunddi.h> 71 #include <sys/filio.h> 72 #include <sys/sid.h> 73 #include <sys/zfs_ctldir.h> 74 #include <sys/zfs_fuid.h> 75 #include <sys/zfs_quota.h> 76 #include <sys/zfs_sa.h> 77 #include <sys/zfs_rlock.h> 78 #include <sys/bio.h> 79 #include <sys/buf.h> 80 #include <sys/sched.h> 81 #include <sys/acl.h> 82 #include <sys/vmmeter.h> 83 #include <vm/vm_param.h> 84 #include <sys/zil.h> 85 #include <sys/zfs_vnops.h> 86 #include <sys/module.h> 87 #include <sys/sysent.h> 88 89 #include <sys/dmu_impl.h> 90 #include <sys/brt.h> 91 #include <sys/zfeature.h> 92 93 #include <vm/vm_object.h> 94 95 #include <sys/extattr.h> 96 #include <sys/priv.h> 97 98 #ifndef VN_OPEN_INVFS 99 #define VN_OPEN_INVFS 0x0 100 #endif 101 102 VFS_SMR_DECLARE; 103 104 #if __FreeBSD_version < 1300103 105 #define NDFREE_PNBUF(ndp) NDFREE((ndp), NDF_ONLY_PNBUF) 106 #endif 107 108 #if __FreeBSD_version >= 1300047 109 #define vm_page_wire_lock(pp) 110 #define vm_page_wire_unlock(pp) 111 #else 112 #define vm_page_wire_lock(pp) vm_page_lock(pp) 113 #define vm_page_wire_unlock(pp) vm_page_unlock(pp) 114 #endif 115 116 #ifdef DEBUG_VFS_LOCKS 117 #define VNCHECKREF(vp) \ 118 VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp, \ 119 ("%s: wrong ref counts", __func__)); 120 #else 121 #define VNCHECKREF(vp) 122 #endif 123 124 #if __FreeBSD_version >= 1400045 125 typedef uint64_t cookie_t; 126 #else 127 typedef ulong_t cookie_t; 128 #endif 129 130 /* 131 * Programming rules. 132 * 133 * Each vnode op performs some logical unit of work. To do this, the ZPL must 134 * properly lock its in-core state, create a DMU transaction, do the work, 135 * record this work in the intent log (ZIL), commit the DMU transaction, 136 * and wait for the intent log to commit if it is a synchronous operation. 137 * Moreover, the vnode ops must work in both normal and log replay context. 138 * The ordering of events is important to avoid deadlocks and references 139 * to freed memory. The example below illustrates the following Big Rules: 140 * 141 * (1) A check must be made in each zfs thread for a mounted file system. 142 * This is done avoiding races using zfs_enter(zfsvfs). 143 * A zfs_exit(zfsvfs) is needed before all returns. Any znodes 144 * must be checked with zfs_verify_zp(zp). Both of these macros 145 * can return EIO from the calling function. 146 * 147 * (2) VN_RELE() should always be the last thing except for zil_commit() 148 * (if necessary) and zfs_exit(). This is for 3 reasons: 149 * First, if it's the last reference, the vnode/znode 150 * can be freed, so the zp may point to freed memory. Second, the last 151 * reference will call zfs_zinactive(), which may induce a lot of work -- 152 * pushing cached pages (which acquires range locks) and syncing out 153 * cached atime changes. Third, zfs_zinactive() may require a new tx, 154 * which could deadlock the system if you were already holding one. 155 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC(). 156 * 157 * (3) All range locks must be grabbed before calling dmu_tx_assign(), 158 * as they can span dmu_tx_assign() calls. 159 * 160 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to 161 * dmu_tx_assign(). This is critical because we don't want to block 162 * while holding locks. 163 * 164 * If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT. This 165 * reduces lock contention and CPU usage when we must wait (note that if 166 * throughput is constrained by the storage, nearly every transaction 167 * must wait). 168 * 169 * Note, in particular, that if a lock is sometimes acquired before 170 * the tx assigns, and sometimes after (e.g. z_lock), then failing 171 * to use a non-blocking assign can deadlock the system. The scenario: 172 * 173 * Thread A has grabbed a lock before calling dmu_tx_assign(). 174 * Thread B is in an already-assigned tx, and blocks for this lock. 175 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 176 * forever, because the previous txg can't quiesce until B's tx commits. 177 * 178 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 179 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent 180 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT, 181 * to indicate that this operation has already called dmu_tx_wait(). 182 * This will ensure that we don't retry forever, waiting a short bit 183 * each time. 184 * 185 * (5) If the operation succeeded, generate the intent log entry for it 186 * before dropping locks. This ensures that the ordering of events 187 * in the intent log matches the order in which they actually occurred. 188 * During ZIL replay the zfs_log_* functions will update the sequence 189 * number to indicate the zil transaction has replayed. 190 * 191 * (6) At the end of each vnode op, the DMU tx must always commit, 192 * regardless of whether there were any errors. 193 * 194 * (7) After dropping all locks, invoke zil_commit(zilog, foid) 195 * to ensure that synchronous semantics are provided when necessary. 196 * 197 * In general, this is how things should be ordered in each vnode op: 198 * 199 * zfs_enter(zfsvfs); // exit if unmounted 200 * top: 201 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD()) 202 * rw_enter(...); // grab any other locks you need 203 * tx = dmu_tx_create(...); // get DMU tx 204 * dmu_tx_hold_*(); // hold each object you might modify 205 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 206 * if (error) { 207 * rw_exit(...); // drop locks 208 * zfs_dirent_unlock(dl); // unlock directory entry 209 * VN_RELE(...); // release held vnodes 210 * if (error == ERESTART) { 211 * waited = B_TRUE; 212 * dmu_tx_wait(tx); 213 * dmu_tx_abort(tx); 214 * goto top; 215 * } 216 * dmu_tx_abort(tx); // abort DMU tx 217 * zfs_exit(zfsvfs); // finished in zfs 218 * return (error); // really out of space 219 * } 220 * error = do_real_work(); // do whatever this VOP does 221 * if (error == 0) 222 * zfs_log_*(...); // on success, make ZIL entry 223 * dmu_tx_commit(tx); // commit DMU tx -- error or not 224 * rw_exit(...); // drop locks 225 * zfs_dirent_unlock(dl); // unlock directory entry 226 * VN_RELE(...); // release held vnodes 227 * zil_commit(zilog, foid); // synchronous when necessary 228 * zfs_exit(zfsvfs); // finished in zfs 229 * return (error); // done, report error 230 */ 231 static int 232 zfs_open(vnode_t **vpp, int flag, cred_t *cr) 233 { 234 (void) cr; 235 znode_t *zp = VTOZ(*vpp); 236 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 237 int error; 238 239 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 240 return (error); 241 242 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) && 243 ((flag & FAPPEND) == 0)) { 244 zfs_exit(zfsvfs, FTAG); 245 return (SET_ERROR(EPERM)); 246 } 247 248 /* Keep a count of the synchronous opens in the znode */ 249 if (flag & O_SYNC) 250 atomic_inc_32(&zp->z_sync_cnt); 251 252 zfs_exit(zfsvfs, FTAG); 253 return (0); 254 } 255 256 static int 257 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 258 { 259 (void) offset, (void) cr; 260 znode_t *zp = VTOZ(vp); 261 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 262 int error; 263 264 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 265 return (error); 266 267 /* Decrement the synchronous opens in the znode */ 268 if ((flag & O_SYNC) && (count == 1)) 269 atomic_dec_32(&zp->z_sync_cnt); 270 271 zfs_exit(zfsvfs, FTAG); 272 return (0); 273 } 274 275 static int 276 zfs_ioctl(vnode_t *vp, ulong_t com, intptr_t data, int flag, cred_t *cred, 277 int *rvalp) 278 { 279 (void) flag, (void) cred, (void) rvalp; 280 loff_t off; 281 int error; 282 283 switch (com) { 284 case _FIOFFS: 285 { 286 return (0); 287 288 /* 289 * The following two ioctls are used by bfu. Faking out, 290 * necessary to avoid bfu errors. 291 */ 292 } 293 case _FIOGDIO: 294 case _FIOSDIO: 295 { 296 return (0); 297 } 298 299 case F_SEEK_DATA: 300 case F_SEEK_HOLE: 301 { 302 off = *(offset_t *)data; 303 /* offset parameter is in/out */ 304 error = zfs_holey(VTOZ(vp), com, &off); 305 if (error) 306 return (error); 307 *(offset_t *)data = off; 308 return (0); 309 } 310 } 311 return (SET_ERROR(ENOTTY)); 312 } 313 314 static vm_page_t 315 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes) 316 { 317 vm_object_t obj; 318 vm_page_t pp; 319 int64_t end; 320 321 /* 322 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE 323 * aligned boundaries, if the range is not aligned. As a result a 324 * DEV_BSIZE subrange with partially dirty data may get marked as clean. 325 * It may happen that all DEV_BSIZE subranges are marked clean and thus 326 * the whole page would be considered clean despite have some 327 * dirty data. 328 * For this reason we should shrink the range to DEV_BSIZE aligned 329 * boundaries before calling vm_page_clear_dirty. 330 */ 331 end = rounddown2(off + nbytes, DEV_BSIZE); 332 off = roundup2(off, DEV_BSIZE); 333 nbytes = end - off; 334 335 obj = vp->v_object; 336 zfs_vmobject_assert_wlocked_12(obj); 337 #if __FreeBSD_version < 1300050 338 for (;;) { 339 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL && 340 pp->valid) { 341 if (vm_page_xbusied(pp)) { 342 /* 343 * Reference the page before unlocking and 344 * sleeping so that the page daemon is less 345 * likely to reclaim it. 346 */ 347 vm_page_reference(pp); 348 vm_page_lock(pp); 349 zfs_vmobject_wunlock(obj); 350 vm_page_busy_sleep(pp, "zfsmwb", true); 351 zfs_vmobject_wlock(obj); 352 continue; 353 } 354 vm_page_sbusy(pp); 355 } else if (pp != NULL) { 356 ASSERT(!pp->valid); 357 pp = NULL; 358 } 359 if (pp != NULL) { 360 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 361 vm_object_pip_add(obj, 1); 362 pmap_remove_write(pp); 363 if (nbytes != 0) 364 vm_page_clear_dirty(pp, off, nbytes); 365 } 366 break; 367 } 368 #else 369 vm_page_grab_valid_unlocked(&pp, obj, OFF_TO_IDX(start), 370 VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_NORMAL | 371 VM_ALLOC_IGN_SBUSY); 372 if (pp != NULL) { 373 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 374 vm_object_pip_add(obj, 1); 375 pmap_remove_write(pp); 376 if (nbytes != 0) 377 vm_page_clear_dirty(pp, off, nbytes); 378 } 379 #endif 380 return (pp); 381 } 382 383 static void 384 page_unbusy(vm_page_t pp) 385 { 386 387 vm_page_sunbusy(pp); 388 #if __FreeBSD_version >= 1300041 389 vm_object_pip_wakeup(pp->object); 390 #else 391 vm_object_pip_subtract(pp->object, 1); 392 #endif 393 } 394 395 #if __FreeBSD_version > 1300051 396 static vm_page_t 397 page_hold(vnode_t *vp, int64_t start) 398 { 399 vm_object_t obj; 400 vm_page_t m; 401 402 obj = vp->v_object; 403 vm_page_grab_valid_unlocked(&m, obj, OFF_TO_IDX(start), 404 VM_ALLOC_NOCREAT | VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY | 405 VM_ALLOC_NOBUSY); 406 return (m); 407 } 408 #else 409 static vm_page_t 410 page_hold(vnode_t *vp, int64_t start) 411 { 412 vm_object_t obj; 413 vm_page_t pp; 414 415 obj = vp->v_object; 416 zfs_vmobject_assert_wlocked(obj); 417 418 for (;;) { 419 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL && 420 pp->valid) { 421 if (vm_page_xbusied(pp)) { 422 /* 423 * Reference the page before unlocking and 424 * sleeping so that the page daemon is less 425 * likely to reclaim it. 426 */ 427 vm_page_reference(pp); 428 vm_page_lock(pp); 429 zfs_vmobject_wunlock(obj); 430 vm_page_busy_sleep(pp, "zfsmwb", true); 431 zfs_vmobject_wlock(obj); 432 continue; 433 } 434 435 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 436 vm_page_wire_lock(pp); 437 vm_page_hold(pp); 438 vm_page_wire_unlock(pp); 439 440 } else 441 pp = NULL; 442 break; 443 } 444 return (pp); 445 } 446 #endif 447 448 static void 449 page_unhold(vm_page_t pp) 450 { 451 452 vm_page_wire_lock(pp); 453 #if __FreeBSD_version >= 1300035 454 vm_page_unwire(pp, PQ_ACTIVE); 455 #else 456 vm_page_unhold(pp); 457 #endif 458 vm_page_wire_unlock(pp); 459 } 460 461 /* 462 * When a file is memory mapped, we must keep the IO data synchronized 463 * between the DMU cache and the memory mapped pages. What this means: 464 * 465 * On Write: If we find a memory mapped page, we write to *both* 466 * the page and the dmu buffer. 467 */ 468 void 469 update_pages(znode_t *zp, int64_t start, int len, objset_t *os) 470 { 471 vm_object_t obj; 472 struct sf_buf *sf; 473 vnode_t *vp = ZTOV(zp); 474 caddr_t va; 475 int off; 476 477 ASSERT3P(vp->v_mount, !=, NULL); 478 obj = vp->v_object; 479 ASSERT3P(obj, !=, NULL); 480 481 off = start & PAGEOFFSET; 482 zfs_vmobject_wlock_12(obj); 483 #if __FreeBSD_version >= 1300041 484 vm_object_pip_add(obj, 1); 485 #endif 486 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 487 vm_page_t pp; 488 int nbytes = imin(PAGESIZE - off, len); 489 490 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) { 491 zfs_vmobject_wunlock_12(obj); 492 493 va = zfs_map_page(pp, &sf); 494 (void) dmu_read(os, zp->z_id, start + off, nbytes, 495 va + off, DMU_READ_PREFETCH); 496 zfs_unmap_page(sf); 497 498 zfs_vmobject_wlock_12(obj); 499 page_unbusy(pp); 500 } 501 len -= nbytes; 502 off = 0; 503 } 504 #if __FreeBSD_version >= 1300041 505 vm_object_pip_wakeup(obj); 506 #else 507 vm_object_pip_wakeupn(obj, 0); 508 #endif 509 zfs_vmobject_wunlock_12(obj); 510 } 511 512 /* 513 * Read with UIO_NOCOPY flag means that sendfile(2) requests 514 * ZFS to populate a range of page cache pages with data. 515 * 516 * NOTE: this function could be optimized to pre-allocate 517 * all pages in advance, drain exclusive busy on all of them, 518 * map them into contiguous KVA region and populate them 519 * in one single dmu_read() call. 520 */ 521 int 522 mappedread_sf(znode_t *zp, int nbytes, zfs_uio_t *uio) 523 { 524 vnode_t *vp = ZTOV(zp); 525 objset_t *os = zp->z_zfsvfs->z_os; 526 struct sf_buf *sf; 527 vm_object_t obj; 528 vm_page_t pp; 529 int64_t start; 530 caddr_t va; 531 int len = nbytes; 532 int error = 0; 533 534 ASSERT3U(zfs_uio_segflg(uio), ==, UIO_NOCOPY); 535 ASSERT3P(vp->v_mount, !=, NULL); 536 obj = vp->v_object; 537 ASSERT3P(obj, !=, NULL); 538 ASSERT0(zfs_uio_offset(uio) & PAGEOFFSET); 539 540 zfs_vmobject_wlock_12(obj); 541 for (start = zfs_uio_offset(uio); len > 0; start += PAGESIZE) { 542 int bytes = MIN(PAGESIZE, len); 543 544 pp = vm_page_grab_unlocked(obj, OFF_TO_IDX(start), 545 VM_ALLOC_SBUSY | VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY); 546 if (vm_page_none_valid(pp)) { 547 zfs_vmobject_wunlock_12(obj); 548 va = zfs_map_page(pp, &sf); 549 error = dmu_read(os, zp->z_id, start, bytes, va, 550 DMU_READ_PREFETCH); 551 if (bytes != PAGESIZE && error == 0) 552 memset(va + bytes, 0, PAGESIZE - bytes); 553 zfs_unmap_page(sf); 554 zfs_vmobject_wlock_12(obj); 555 #if __FreeBSD_version >= 1300081 556 if (error == 0) { 557 vm_page_valid(pp); 558 vm_page_activate(pp); 559 vm_page_do_sunbusy(pp); 560 } else { 561 zfs_vmobject_wlock(obj); 562 if (!vm_page_wired(pp) && pp->valid == 0 && 563 vm_page_busy_tryupgrade(pp)) 564 vm_page_free(pp); 565 else 566 vm_page_sunbusy(pp); 567 zfs_vmobject_wunlock(obj); 568 } 569 #else 570 vm_page_do_sunbusy(pp); 571 vm_page_lock(pp); 572 if (error) { 573 if (pp->wire_count == 0 && pp->valid == 0 && 574 !vm_page_busied(pp)) 575 vm_page_free(pp); 576 } else { 577 pp->valid = VM_PAGE_BITS_ALL; 578 vm_page_activate(pp); 579 } 580 vm_page_unlock(pp); 581 #endif 582 } else { 583 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL); 584 vm_page_do_sunbusy(pp); 585 } 586 if (error) 587 break; 588 zfs_uio_advance(uio, bytes); 589 len -= bytes; 590 } 591 zfs_vmobject_wunlock_12(obj); 592 return (error); 593 } 594 595 /* 596 * When a file is memory mapped, we must keep the IO data synchronized 597 * between the DMU cache and the memory mapped pages. What this means: 598 * 599 * On Read: We "read" preferentially from memory mapped pages, 600 * else we default from the dmu buffer. 601 * 602 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 603 * the file is memory mapped. 604 */ 605 int 606 mappedread(znode_t *zp, int nbytes, zfs_uio_t *uio) 607 { 608 vnode_t *vp = ZTOV(zp); 609 vm_object_t obj; 610 int64_t start; 611 int len = nbytes; 612 int off; 613 int error = 0; 614 615 ASSERT3P(vp->v_mount, !=, NULL); 616 obj = vp->v_object; 617 ASSERT3P(obj, !=, NULL); 618 619 start = zfs_uio_offset(uio); 620 off = start & PAGEOFFSET; 621 zfs_vmobject_wlock_12(obj); 622 for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 623 vm_page_t pp; 624 uint64_t bytes = MIN(PAGESIZE - off, len); 625 626 if ((pp = page_hold(vp, start))) { 627 struct sf_buf *sf; 628 caddr_t va; 629 630 zfs_vmobject_wunlock_12(obj); 631 va = zfs_map_page(pp, &sf); 632 error = vn_io_fault_uiomove(va + off, bytes, 633 GET_UIO_STRUCT(uio)); 634 zfs_unmap_page(sf); 635 zfs_vmobject_wlock_12(obj); 636 page_unhold(pp); 637 } else { 638 zfs_vmobject_wunlock_12(obj); 639 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl), 640 uio, bytes); 641 zfs_vmobject_wlock_12(obj); 642 } 643 len -= bytes; 644 off = 0; 645 if (error) 646 break; 647 } 648 zfs_vmobject_wunlock_12(obj); 649 return (error); 650 } 651 652 int 653 zfs_write_simple(znode_t *zp, const void *data, size_t len, 654 loff_t pos, size_t *presid) 655 { 656 int error = 0; 657 ssize_t resid; 658 659 error = vn_rdwr(UIO_WRITE, ZTOV(zp), __DECONST(void *, data), len, pos, 660 UIO_SYSSPACE, IO_SYNC, kcred, NOCRED, &resid, curthread); 661 662 if (error) { 663 return (SET_ERROR(error)); 664 } else if (presid == NULL) { 665 if (resid != 0) { 666 error = SET_ERROR(EIO); 667 } 668 } else { 669 *presid = resid; 670 } 671 return (error); 672 } 673 674 void 675 zfs_zrele_async(znode_t *zp) 676 { 677 vnode_t *vp = ZTOV(zp); 678 objset_t *os = ITOZSB(vp)->z_os; 679 680 VN_RELE_ASYNC(vp, dsl_pool_zrele_taskq(dmu_objset_pool(os))); 681 } 682 683 static int 684 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp) 685 { 686 int error; 687 688 *vpp = arg; 689 error = vn_lock(*vpp, lkflags); 690 if (error != 0) 691 vrele(*vpp); 692 return (error); 693 } 694 695 static int 696 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags) 697 { 698 znode_t *zdp = VTOZ(dvp); 699 zfsvfs_t *zfsvfs __unused = zdp->z_zfsvfs; 700 int error; 701 int ltype; 702 703 if (zfsvfs->z_replay == B_FALSE) 704 ASSERT_VOP_LOCKED(dvp, __func__); 705 706 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) { 707 ASSERT3P(dvp, ==, vp); 708 vref(dvp); 709 ltype = lkflags & LK_TYPE_MASK; 710 if (ltype != VOP_ISLOCKED(dvp)) { 711 if (ltype == LK_EXCLUSIVE) 712 vn_lock(dvp, LK_UPGRADE | LK_RETRY); 713 else /* if (ltype == LK_SHARED) */ 714 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY); 715 716 /* 717 * Relock for the "." case could leave us with 718 * reclaimed vnode. 719 */ 720 if (VN_IS_DOOMED(dvp)) { 721 vrele(dvp); 722 return (SET_ERROR(ENOENT)); 723 } 724 } 725 return (0); 726 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) { 727 /* 728 * Note that in this case, dvp is the child vnode, and we 729 * are looking up the parent vnode - exactly reverse from 730 * normal operation. Unlocking dvp requires some rather 731 * tricky unlock/relock dance to prevent mp from being freed; 732 * use vn_vget_ino_gen() which takes care of all that. 733 * 734 * XXX Note that there is a time window when both vnodes are 735 * unlocked. It is possible, although highly unlikely, that 736 * during that window the parent-child relationship between 737 * the vnodes may change, for example, get reversed. 738 * In that case we would have a wrong lock order for the vnodes. 739 * All other filesystems seem to ignore this problem, so we 740 * do the same here. 741 * A potential solution could be implemented as follows: 742 * - using LK_NOWAIT when locking the second vnode and retrying 743 * if necessary 744 * - checking that the parent-child relationship still holds 745 * after locking both vnodes and retrying if it doesn't 746 */ 747 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp); 748 return (error); 749 } else { 750 error = vn_lock(vp, lkflags); 751 if (error != 0) 752 vrele(vp); 753 return (error); 754 } 755 } 756 757 /* 758 * Lookup an entry in a directory, or an extended attribute directory. 759 * If it exists, return a held vnode reference for it. 760 * 761 * IN: dvp - vnode of directory to search. 762 * nm - name of entry to lookup. 763 * pnp - full pathname to lookup [UNUSED]. 764 * flags - LOOKUP_XATTR set if looking for an attribute. 765 * rdir - root directory vnode [UNUSED]. 766 * cr - credentials of caller. 767 * ct - caller context 768 * 769 * OUT: vpp - vnode of located entry, NULL if not found. 770 * 771 * RETURN: 0 on success, error code on failure. 772 * 773 * Timestamps: 774 * NA 775 */ 776 static int 777 zfs_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp, 778 struct componentname *cnp, int nameiop, cred_t *cr, int flags, 779 boolean_t cached) 780 { 781 znode_t *zdp = VTOZ(dvp); 782 znode_t *zp; 783 zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 784 #if __FreeBSD_version > 1300124 785 seqc_t dvp_seqc; 786 #endif 787 int error = 0; 788 789 /* 790 * Fast path lookup, however we must skip DNLC lookup 791 * for case folding or normalizing lookups because the 792 * DNLC code only stores the passed in name. This means 793 * creating 'a' and removing 'A' on a case insensitive 794 * file system would work, but DNLC still thinks 'a' 795 * exists and won't let you create it again on the next 796 * pass through fast path. 797 */ 798 if (!(flags & LOOKUP_XATTR)) { 799 if (dvp->v_type != VDIR) { 800 return (SET_ERROR(ENOTDIR)); 801 } else if (zdp->z_sa_hdl == NULL) { 802 return (SET_ERROR(EIO)); 803 } 804 } 805 806 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, 807 const char *, nm); 808 809 if ((error = zfs_enter_verify_zp(zfsvfs, zdp, FTAG)) != 0) 810 return (error); 811 812 #if __FreeBSD_version > 1300124 813 dvp_seqc = vn_seqc_read_notmodify(dvp); 814 #endif 815 816 *vpp = NULL; 817 818 if (flags & LOOKUP_XATTR) { 819 /* 820 * If the xattr property is off, refuse the lookup request. 821 */ 822 if (!(zfsvfs->z_flags & ZSB_XATTR)) { 823 zfs_exit(zfsvfs, FTAG); 824 return (SET_ERROR(EOPNOTSUPP)); 825 } 826 827 /* 828 * We don't allow recursive attributes.. 829 * Maybe someday we will. 830 */ 831 if (zdp->z_pflags & ZFS_XATTR) { 832 zfs_exit(zfsvfs, FTAG); 833 return (SET_ERROR(EINVAL)); 834 } 835 836 if ((error = zfs_get_xattrdir(VTOZ(dvp), &zp, cr, flags))) { 837 zfs_exit(zfsvfs, FTAG); 838 return (error); 839 } 840 *vpp = ZTOV(zp); 841 842 /* 843 * Do we have permission to get into attribute directory? 844 */ 845 error = zfs_zaccess(zp, ACE_EXECUTE, 0, B_FALSE, cr, NULL); 846 if (error) { 847 vrele(ZTOV(zp)); 848 } 849 850 zfs_exit(zfsvfs, FTAG); 851 return (error); 852 } 853 854 /* 855 * Check accessibility of directory if we're not coming in via 856 * VOP_CACHEDLOOKUP. 857 */ 858 if (!cached) { 859 #ifdef NOEXECCHECK 860 if ((cnp->cn_flags & NOEXECCHECK) != 0) { 861 cnp->cn_flags &= ~NOEXECCHECK; 862 } else 863 #endif 864 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr, 865 NULL))) { 866 zfs_exit(zfsvfs, FTAG); 867 return (error); 868 } 869 } 870 871 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 872 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 873 zfs_exit(zfsvfs, FTAG); 874 return (SET_ERROR(EILSEQ)); 875 } 876 877 878 /* 879 * First handle the special cases. 880 */ 881 if ((cnp->cn_flags & ISDOTDOT) != 0) { 882 /* 883 * If we are a snapshot mounted under .zfs, return 884 * the vp for the snapshot directory. 885 */ 886 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) { 887 struct componentname cn; 888 vnode_t *zfsctl_vp; 889 int ltype; 890 891 zfs_exit(zfsvfs, FTAG); 892 ltype = VOP_ISLOCKED(dvp); 893 VOP_UNLOCK1(dvp); 894 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED, 895 &zfsctl_vp); 896 if (error == 0) { 897 cn.cn_nameptr = "snapshot"; 898 cn.cn_namelen = strlen(cn.cn_nameptr); 899 cn.cn_nameiop = cnp->cn_nameiop; 900 cn.cn_flags = cnp->cn_flags & ~ISDOTDOT; 901 cn.cn_lkflags = cnp->cn_lkflags; 902 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn); 903 vput(zfsctl_vp); 904 } 905 vn_lock(dvp, ltype | LK_RETRY); 906 return (error); 907 } 908 } 909 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) { 910 zfs_exit(zfsvfs, FTAG); 911 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP) 912 return (SET_ERROR(ENOTSUP)); 913 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp); 914 return (error); 915 } 916 917 /* 918 * The loop is retry the lookup if the parent-child relationship 919 * changes during the dot-dot locking complexities. 920 */ 921 for (;;) { 922 uint64_t parent; 923 924 error = zfs_dirlook(zdp, nm, &zp); 925 if (error == 0) 926 *vpp = ZTOV(zp); 927 928 zfs_exit(zfsvfs, FTAG); 929 if (error != 0) 930 break; 931 932 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags); 933 if (error != 0) { 934 /* 935 * If we've got a locking error, then the vnode 936 * got reclaimed because of a force unmount. 937 * We never enter doomed vnodes into the name cache. 938 */ 939 *vpp = NULL; 940 return (error); 941 } 942 943 if ((cnp->cn_flags & ISDOTDOT) == 0) 944 break; 945 946 if ((error = zfs_enter(zfsvfs, FTAG)) != 0) { 947 vput(ZTOV(zp)); 948 *vpp = NULL; 949 return (error); 950 } 951 if (zdp->z_sa_hdl == NULL) { 952 error = SET_ERROR(EIO); 953 } else { 954 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 955 &parent, sizeof (parent)); 956 } 957 if (error != 0) { 958 zfs_exit(zfsvfs, FTAG); 959 vput(ZTOV(zp)); 960 break; 961 } 962 if (zp->z_id == parent) { 963 zfs_exit(zfsvfs, FTAG); 964 break; 965 } 966 vput(ZTOV(zp)); 967 } 968 969 if (error != 0) 970 *vpp = NULL; 971 972 /* Translate errors and add SAVENAME when needed. */ 973 if (cnp->cn_flags & ISLASTCN) { 974 switch (nameiop) { 975 case CREATE: 976 case RENAME: 977 if (error == ENOENT) { 978 error = EJUSTRETURN; 979 #if __FreeBSD_version < 1400068 980 cnp->cn_flags |= SAVENAME; 981 #endif 982 break; 983 } 984 zfs_fallthrough; 985 case DELETE: 986 #if __FreeBSD_version < 1400068 987 if (error == 0) 988 cnp->cn_flags |= SAVENAME; 989 #endif 990 break; 991 } 992 } 993 994 #if __FreeBSD_version > 1300124 995 if ((cnp->cn_flags & ISDOTDOT) != 0) { 996 /* 997 * FIXME: zfs_lookup_lock relocks vnodes and does nothing to 998 * handle races. In particular different callers may end up 999 * with different vnodes and will try to add conflicting 1000 * entries to the namecache. 1001 * 1002 * While finding different result may be acceptable in face 1003 * of concurrent modification, adding conflicting entries 1004 * trips over an assert in the namecache. 1005 * 1006 * Ultimately let an entry through once everything settles. 1007 */ 1008 if (!vn_seqc_consistent(dvp, dvp_seqc)) { 1009 cnp->cn_flags &= ~MAKEENTRY; 1010 } 1011 } 1012 #endif 1013 1014 /* Insert name into cache (as non-existent) if appropriate. */ 1015 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay && 1016 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0) 1017 cache_enter(dvp, NULL, cnp); 1018 1019 /* Insert name into cache if appropriate. */ 1020 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay && 1021 error == 0 && (cnp->cn_flags & MAKEENTRY)) { 1022 if (!(cnp->cn_flags & ISLASTCN) || 1023 (nameiop != DELETE && nameiop != RENAME)) { 1024 cache_enter(dvp, *vpp, cnp); 1025 } 1026 } 1027 1028 return (error); 1029 } 1030 1031 /* 1032 * Attempt to create a new entry in a directory. If the entry 1033 * already exists, truncate the file if permissible, else return 1034 * an error. Return the vp of the created or trunc'd file. 1035 * 1036 * IN: dvp - vnode of directory to put new file entry in. 1037 * name - name of new file entry. 1038 * vap - attributes of new file. 1039 * excl - flag indicating exclusive or non-exclusive mode. 1040 * mode - mode to open file with. 1041 * cr - credentials of caller. 1042 * flag - large file flag [UNUSED]. 1043 * ct - caller context 1044 * vsecp - ACL to be set 1045 * mnt_ns - Unused on FreeBSD 1046 * 1047 * OUT: vpp - vnode of created or trunc'd entry. 1048 * 1049 * RETURN: 0 on success, error code on failure. 1050 * 1051 * Timestamps: 1052 * dvp - ctime|mtime updated if new entry created 1053 * vp - ctime|mtime always, atime if new 1054 */ 1055 int 1056 zfs_create(znode_t *dzp, const char *name, vattr_t *vap, int excl, int mode, 1057 znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp, zuserns_t *mnt_ns) 1058 { 1059 (void) excl, (void) mode, (void) flag; 1060 znode_t *zp; 1061 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1062 zilog_t *zilog; 1063 objset_t *os; 1064 dmu_tx_t *tx; 1065 int error; 1066 uid_t uid = crgetuid(cr); 1067 gid_t gid = crgetgid(cr); 1068 uint64_t projid = ZFS_DEFAULT_PROJID; 1069 zfs_acl_ids_t acl_ids; 1070 boolean_t fuid_dirtied; 1071 uint64_t txtype; 1072 #ifdef DEBUG_VFS_LOCKS 1073 vnode_t *dvp = ZTOV(dzp); 1074 #endif 1075 1076 /* 1077 * If we have an ephemeral id, ACL, or XVATTR then 1078 * make sure file system is at proper version 1079 */ 1080 if (zfsvfs->z_use_fuids == B_FALSE && 1081 (vsecp || (vap->va_mask & AT_XVATTR) || 1082 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 1083 return (SET_ERROR(EINVAL)); 1084 1085 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0) 1086 return (error); 1087 os = zfsvfs->z_os; 1088 zilog = zfsvfs->z_log; 1089 1090 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 1091 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1092 zfs_exit(zfsvfs, FTAG); 1093 return (SET_ERROR(EILSEQ)); 1094 } 1095 1096 if (vap->va_mask & AT_XVATTR) { 1097 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap, 1098 crgetuid(cr), cr, vap->va_type)) != 0) { 1099 zfs_exit(zfsvfs, FTAG); 1100 return (error); 1101 } 1102 } 1103 1104 *zpp = NULL; 1105 1106 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr)) 1107 vap->va_mode &= ~S_ISVTX; 1108 1109 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW); 1110 if (error) { 1111 zfs_exit(zfsvfs, FTAG); 1112 return (error); 1113 } 1114 ASSERT3P(zp, ==, NULL); 1115 1116 /* 1117 * Create a new file object and update the directory 1118 * to reference it. 1119 */ 1120 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) { 1121 goto out; 1122 } 1123 1124 /* 1125 * We only support the creation of regular files in 1126 * extended attribute directories. 1127 */ 1128 1129 if ((dzp->z_pflags & ZFS_XATTR) && 1130 (vap->va_type != VREG)) { 1131 error = SET_ERROR(EINVAL); 1132 goto out; 1133 } 1134 1135 if ((error = zfs_acl_ids_create(dzp, 0, vap, 1136 cr, vsecp, &acl_ids, NULL)) != 0) 1137 goto out; 1138 1139 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode)) 1140 projid = zfs_inherit_projid(dzp); 1141 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) { 1142 zfs_acl_ids_free(&acl_ids); 1143 error = SET_ERROR(EDQUOT); 1144 goto out; 1145 } 1146 1147 getnewvnode_reserve_(); 1148 1149 tx = dmu_tx_create(os); 1150 1151 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 1152 ZFS_SA_BASE_ATTR_SIZE); 1153 1154 fuid_dirtied = zfsvfs->z_fuid_dirty; 1155 if (fuid_dirtied) 1156 zfs_fuid_txhold(zfsvfs, tx); 1157 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1158 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 1159 if (!zfsvfs->z_use_sa && 1160 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1161 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1162 0, acl_ids.z_aclp->z_acl_bytes); 1163 } 1164 error = dmu_tx_assign(tx, TXG_WAIT); 1165 if (error) { 1166 zfs_acl_ids_free(&acl_ids); 1167 dmu_tx_abort(tx); 1168 getnewvnode_drop_reserve(); 1169 zfs_exit(zfsvfs, FTAG); 1170 return (error); 1171 } 1172 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 1173 if (fuid_dirtied) 1174 zfs_fuid_sync(zfsvfs, tx); 1175 1176 (void) zfs_link_create(dzp, name, zp, tx, ZNEW); 1177 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 1178 zfs_log_create(zilog, tx, txtype, dzp, zp, name, 1179 vsecp, acl_ids.z_fuidp, vap); 1180 zfs_acl_ids_free(&acl_ids); 1181 dmu_tx_commit(tx); 1182 1183 getnewvnode_drop_reserve(); 1184 1185 out: 1186 VNCHECKREF(dvp); 1187 if (error == 0) { 1188 *zpp = zp; 1189 } 1190 1191 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1192 zil_commit(zilog, 0); 1193 1194 zfs_exit(zfsvfs, FTAG); 1195 return (error); 1196 } 1197 1198 /* 1199 * Remove an entry from a directory. 1200 * 1201 * IN: dvp - vnode of directory to remove entry from. 1202 * name - name of entry to remove. 1203 * cr - credentials of caller. 1204 * ct - caller context 1205 * flags - case flags 1206 * 1207 * RETURN: 0 on success, error code on failure. 1208 * 1209 * Timestamps: 1210 * dvp - ctime|mtime 1211 * vp - ctime (if nlink > 0) 1212 */ 1213 static int 1214 zfs_remove_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr) 1215 { 1216 znode_t *dzp = VTOZ(dvp); 1217 znode_t *zp; 1218 znode_t *xzp; 1219 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1220 zilog_t *zilog; 1221 uint64_t xattr_obj; 1222 uint64_t obj = 0; 1223 dmu_tx_t *tx; 1224 boolean_t unlinked; 1225 uint64_t txtype; 1226 int error; 1227 1228 1229 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0) 1230 return (error); 1231 zp = VTOZ(vp); 1232 if ((error = zfs_verify_zp(zp)) != 0) { 1233 zfs_exit(zfsvfs, FTAG); 1234 return (error); 1235 } 1236 zilog = zfsvfs->z_log; 1237 1238 xattr_obj = 0; 1239 xzp = NULL; 1240 1241 if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) { 1242 goto out; 1243 } 1244 1245 /* 1246 * Need to use rmdir for removing directories. 1247 */ 1248 if (vp->v_type == VDIR) { 1249 error = SET_ERROR(EPERM); 1250 goto out; 1251 } 1252 1253 vnevent_remove(vp, dvp, name, ct); 1254 1255 obj = zp->z_id; 1256 1257 /* are there any extended attributes? */ 1258 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1259 &xattr_obj, sizeof (xattr_obj)); 1260 if (error == 0 && xattr_obj) { 1261 error = zfs_zget(zfsvfs, xattr_obj, &xzp); 1262 ASSERT0(error); 1263 } 1264 1265 /* 1266 * We may delete the znode now, or we may put it in the unlinked set; 1267 * it depends on whether we're the last link, and on whether there are 1268 * other holds on the vnode. So we dmu_tx_hold() the right things to 1269 * allow for either case. 1270 */ 1271 tx = dmu_tx_create(zfsvfs->z_os); 1272 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1273 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1274 zfs_sa_upgrade_txholds(tx, zp); 1275 zfs_sa_upgrade_txholds(tx, dzp); 1276 1277 if (xzp) { 1278 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 1279 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1280 } 1281 1282 /* charge as an update -- would be nice not to charge at all */ 1283 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1284 1285 /* 1286 * Mark this transaction as typically resulting in a net free of space 1287 */ 1288 dmu_tx_mark_netfree(tx); 1289 1290 error = dmu_tx_assign(tx, TXG_WAIT); 1291 if (error) { 1292 dmu_tx_abort(tx); 1293 zfs_exit(zfsvfs, FTAG); 1294 return (error); 1295 } 1296 1297 /* 1298 * Remove the directory entry. 1299 */ 1300 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked); 1301 1302 if (error) { 1303 dmu_tx_commit(tx); 1304 goto out; 1305 } 1306 1307 if (unlinked) { 1308 zfs_unlinked_add(zp, tx); 1309 vp->v_vflag |= VV_NOSYNC; 1310 } 1311 /* XXX check changes to linux vnops */ 1312 txtype = TX_REMOVE; 1313 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked); 1314 1315 dmu_tx_commit(tx); 1316 out: 1317 1318 if (xzp) 1319 vrele(ZTOV(xzp)); 1320 1321 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1322 zil_commit(zilog, 0); 1323 1324 1325 zfs_exit(zfsvfs, FTAG); 1326 return (error); 1327 } 1328 1329 1330 static int 1331 zfs_lookup_internal(znode_t *dzp, const char *name, vnode_t **vpp, 1332 struct componentname *cnp, int nameiop) 1333 { 1334 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1335 int error; 1336 1337 cnp->cn_nameptr = __DECONST(char *, name); 1338 cnp->cn_namelen = strlen(name); 1339 cnp->cn_nameiop = nameiop; 1340 cnp->cn_flags = ISLASTCN; 1341 #if __FreeBSD_version < 1400068 1342 cnp->cn_flags |= SAVENAME; 1343 #endif 1344 cnp->cn_lkflags = LK_EXCLUSIVE | LK_RETRY; 1345 cnp->cn_cred = kcred; 1346 #if __FreeBSD_version < 1400037 1347 cnp->cn_thread = curthread; 1348 #endif 1349 1350 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay) { 1351 struct vop_lookup_args a; 1352 1353 a.a_gen.a_desc = &vop_lookup_desc; 1354 a.a_dvp = ZTOV(dzp); 1355 a.a_vpp = vpp; 1356 a.a_cnp = cnp; 1357 error = vfs_cache_lookup(&a); 1358 } else { 1359 error = zfs_lookup(ZTOV(dzp), name, vpp, cnp, nameiop, kcred, 0, 1360 B_FALSE); 1361 } 1362 #ifdef ZFS_DEBUG 1363 if (error) { 1364 printf("got error %d on name %s on op %d\n", error, name, 1365 nameiop); 1366 kdb_backtrace(); 1367 } 1368 #endif 1369 return (error); 1370 } 1371 1372 int 1373 zfs_remove(znode_t *dzp, const char *name, cred_t *cr, int flags) 1374 { 1375 vnode_t *vp; 1376 int error; 1377 struct componentname cn; 1378 1379 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE))) 1380 return (error); 1381 1382 error = zfs_remove_(ZTOV(dzp), vp, name, cr); 1383 vput(vp); 1384 return (error); 1385 } 1386 /* 1387 * Create a new directory and insert it into dvp using the name 1388 * provided. Return a pointer to the inserted directory. 1389 * 1390 * IN: dvp - vnode of directory to add subdir to. 1391 * dirname - name of new directory. 1392 * vap - attributes of new directory. 1393 * cr - credentials of caller. 1394 * ct - caller context 1395 * flags - case flags 1396 * vsecp - ACL to be set 1397 * mnt_ns - Unused on FreeBSD 1398 * 1399 * OUT: vpp - vnode of created directory. 1400 * 1401 * RETURN: 0 on success, error code on failure. 1402 * 1403 * Timestamps: 1404 * dvp - ctime|mtime updated 1405 * vp - ctime|mtime|atime updated 1406 */ 1407 int 1408 zfs_mkdir(znode_t *dzp, const char *dirname, vattr_t *vap, znode_t **zpp, 1409 cred_t *cr, int flags, vsecattr_t *vsecp, zuserns_t *mnt_ns) 1410 { 1411 (void) flags, (void) vsecp; 1412 znode_t *zp; 1413 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1414 zilog_t *zilog; 1415 uint64_t txtype; 1416 dmu_tx_t *tx; 1417 int error; 1418 uid_t uid = crgetuid(cr); 1419 gid_t gid = crgetgid(cr); 1420 zfs_acl_ids_t acl_ids; 1421 boolean_t fuid_dirtied; 1422 1423 ASSERT3U(vap->va_type, ==, VDIR); 1424 1425 /* 1426 * If we have an ephemeral id, ACL, or XVATTR then 1427 * make sure file system is at proper version 1428 */ 1429 if (zfsvfs->z_use_fuids == B_FALSE && 1430 ((vap->va_mask & AT_XVATTR) || 1431 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 1432 return (SET_ERROR(EINVAL)); 1433 1434 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0) 1435 return (error); 1436 zilog = zfsvfs->z_log; 1437 1438 if (dzp->z_pflags & ZFS_XATTR) { 1439 zfs_exit(zfsvfs, FTAG); 1440 return (SET_ERROR(EINVAL)); 1441 } 1442 1443 if (zfsvfs->z_utf8 && u8_validate(dirname, 1444 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1445 zfs_exit(zfsvfs, FTAG); 1446 return (SET_ERROR(EILSEQ)); 1447 } 1448 1449 if (vap->va_mask & AT_XVATTR) { 1450 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap, 1451 crgetuid(cr), cr, vap->va_type)) != 0) { 1452 zfs_exit(zfsvfs, FTAG); 1453 return (error); 1454 } 1455 } 1456 1457 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 1458 NULL, &acl_ids, NULL)) != 0) { 1459 zfs_exit(zfsvfs, FTAG); 1460 return (error); 1461 } 1462 1463 /* 1464 * First make sure the new directory doesn't exist. 1465 * 1466 * Existence is checked first to make sure we don't return 1467 * EACCES instead of EEXIST which can cause some applications 1468 * to fail. 1469 */ 1470 *zpp = NULL; 1471 1472 if ((error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW))) { 1473 zfs_acl_ids_free(&acl_ids); 1474 zfs_exit(zfsvfs, FTAG); 1475 return (error); 1476 } 1477 ASSERT3P(zp, ==, NULL); 1478 1479 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr, 1480 mnt_ns))) { 1481 zfs_acl_ids_free(&acl_ids); 1482 zfs_exit(zfsvfs, FTAG); 1483 return (error); 1484 } 1485 1486 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) { 1487 zfs_acl_ids_free(&acl_ids); 1488 zfs_exit(zfsvfs, FTAG); 1489 return (SET_ERROR(EDQUOT)); 1490 } 1491 1492 /* 1493 * Add a new entry to the directory. 1494 */ 1495 getnewvnode_reserve_(); 1496 tx = dmu_tx_create(zfsvfs->z_os); 1497 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 1498 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1499 fuid_dirtied = zfsvfs->z_fuid_dirty; 1500 if (fuid_dirtied) 1501 zfs_fuid_txhold(zfsvfs, tx); 1502 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1503 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 1504 acl_ids.z_aclp->z_acl_bytes); 1505 } 1506 1507 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 1508 ZFS_SA_BASE_ATTR_SIZE); 1509 1510 error = dmu_tx_assign(tx, TXG_WAIT); 1511 if (error) { 1512 zfs_acl_ids_free(&acl_ids); 1513 dmu_tx_abort(tx); 1514 getnewvnode_drop_reserve(); 1515 zfs_exit(zfsvfs, FTAG); 1516 return (error); 1517 } 1518 1519 /* 1520 * Create new node. 1521 */ 1522 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 1523 1524 if (fuid_dirtied) 1525 zfs_fuid_sync(zfsvfs, tx); 1526 1527 /* 1528 * Now put new name in parent dir. 1529 */ 1530 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW); 1531 1532 *zpp = zp; 1533 1534 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap); 1535 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL, 1536 acl_ids.z_fuidp, vap); 1537 1538 zfs_acl_ids_free(&acl_ids); 1539 1540 dmu_tx_commit(tx); 1541 1542 getnewvnode_drop_reserve(); 1543 1544 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1545 zil_commit(zilog, 0); 1546 1547 zfs_exit(zfsvfs, FTAG); 1548 return (0); 1549 } 1550 1551 #if __FreeBSD_version < 1300124 1552 static void 1553 cache_vop_rmdir(struct vnode *dvp, struct vnode *vp) 1554 { 1555 1556 cache_purge(dvp); 1557 cache_purge(vp); 1558 } 1559 #endif 1560 1561 /* 1562 * Remove a directory subdir entry. If the current working 1563 * directory is the same as the subdir to be removed, the 1564 * remove will fail. 1565 * 1566 * IN: dvp - vnode of directory to remove from. 1567 * name - name of directory to be removed. 1568 * cwd - vnode of current working directory. 1569 * cr - credentials of caller. 1570 * ct - caller context 1571 * flags - case flags 1572 * 1573 * RETURN: 0 on success, error code on failure. 1574 * 1575 * Timestamps: 1576 * dvp - ctime|mtime updated 1577 */ 1578 static int 1579 zfs_rmdir_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr) 1580 { 1581 znode_t *dzp = VTOZ(dvp); 1582 znode_t *zp = VTOZ(vp); 1583 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1584 zilog_t *zilog; 1585 dmu_tx_t *tx; 1586 int error; 1587 1588 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0) 1589 return (error); 1590 if ((error = zfs_verify_zp(zp)) != 0) { 1591 zfs_exit(zfsvfs, FTAG); 1592 return (error); 1593 } 1594 zilog = zfsvfs->z_log; 1595 1596 1597 if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) { 1598 goto out; 1599 } 1600 1601 if (vp->v_type != VDIR) { 1602 error = SET_ERROR(ENOTDIR); 1603 goto out; 1604 } 1605 1606 vnevent_rmdir(vp, dvp, name, ct); 1607 1608 tx = dmu_tx_create(zfsvfs->z_os); 1609 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1610 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1611 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1612 zfs_sa_upgrade_txholds(tx, zp); 1613 zfs_sa_upgrade_txholds(tx, dzp); 1614 dmu_tx_mark_netfree(tx); 1615 error = dmu_tx_assign(tx, TXG_WAIT); 1616 if (error) { 1617 dmu_tx_abort(tx); 1618 zfs_exit(zfsvfs, FTAG); 1619 return (error); 1620 } 1621 1622 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL); 1623 1624 if (error == 0) { 1625 uint64_t txtype = TX_RMDIR; 1626 zfs_log_remove(zilog, tx, txtype, dzp, name, 1627 ZFS_NO_OBJECT, B_FALSE); 1628 } 1629 1630 dmu_tx_commit(tx); 1631 1632 if (zfsvfs->z_use_namecache) 1633 cache_vop_rmdir(dvp, vp); 1634 out: 1635 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1636 zil_commit(zilog, 0); 1637 1638 zfs_exit(zfsvfs, FTAG); 1639 return (error); 1640 } 1641 1642 int 1643 zfs_rmdir(znode_t *dzp, const char *name, znode_t *cwd, cred_t *cr, int flags) 1644 { 1645 struct componentname cn; 1646 vnode_t *vp; 1647 int error; 1648 1649 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE))) 1650 return (error); 1651 1652 error = zfs_rmdir_(ZTOV(dzp), vp, name, cr); 1653 vput(vp); 1654 return (error); 1655 } 1656 1657 /* 1658 * Read as many directory entries as will fit into the provided 1659 * buffer from the given directory cursor position (specified in 1660 * the uio structure). 1661 * 1662 * IN: vp - vnode of directory to read. 1663 * uio - structure supplying read location, range info, 1664 * and return buffer. 1665 * cr - credentials of caller. 1666 * ct - caller context 1667 * 1668 * OUT: uio - updated offset and range, buffer filled. 1669 * eofp - set to true if end-of-file detected. 1670 * ncookies- number of entries in cookies 1671 * cookies - offsets to directory entries 1672 * 1673 * RETURN: 0 on success, error code on failure. 1674 * 1675 * Timestamps: 1676 * vp - atime updated 1677 * 1678 * Note that the low 4 bits of the cookie returned by zap is always zero. 1679 * This allows us to use the low range for "special" directory entries: 1680 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1681 * we use the offset 2 for the '.zfs' directory. 1682 */ 1683 static int 1684 zfs_readdir(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, int *eofp, 1685 int *ncookies, cookie_t **cookies) 1686 { 1687 znode_t *zp = VTOZ(vp); 1688 iovec_t *iovp; 1689 dirent64_t *odp; 1690 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1691 objset_t *os; 1692 caddr_t outbuf; 1693 size_t bufsize; 1694 zap_cursor_t zc; 1695 zap_attribute_t zap; 1696 uint_t bytes_wanted; 1697 uint64_t offset; /* must be unsigned; checks for < 1 */ 1698 uint64_t parent; 1699 int local_eof; 1700 int outcount; 1701 int error; 1702 uint8_t prefetch; 1703 uint8_t type; 1704 int ncooks; 1705 cookie_t *cooks = NULL; 1706 1707 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 1708 return (error); 1709 1710 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 1711 &parent, sizeof (parent))) != 0) { 1712 zfs_exit(zfsvfs, FTAG); 1713 return (error); 1714 } 1715 1716 /* 1717 * If we are not given an eof variable, 1718 * use a local one. 1719 */ 1720 if (eofp == NULL) 1721 eofp = &local_eof; 1722 1723 /* 1724 * Check for valid iov_len. 1725 */ 1726 if (GET_UIO_STRUCT(uio)->uio_iov->iov_len <= 0) { 1727 zfs_exit(zfsvfs, FTAG); 1728 return (SET_ERROR(EINVAL)); 1729 } 1730 1731 /* 1732 * Quit if directory has been removed (posix) 1733 */ 1734 if ((*eofp = zp->z_unlinked) != 0) { 1735 zfs_exit(zfsvfs, FTAG); 1736 return (0); 1737 } 1738 1739 error = 0; 1740 os = zfsvfs->z_os; 1741 offset = zfs_uio_offset(uio); 1742 prefetch = zp->z_zn_prefetch; 1743 1744 /* 1745 * Initialize the iterator cursor. 1746 */ 1747 if (offset <= 3) { 1748 /* 1749 * Start iteration from the beginning of the directory. 1750 */ 1751 zap_cursor_init(&zc, os, zp->z_id); 1752 } else { 1753 /* 1754 * The offset is a serialized cursor. 1755 */ 1756 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1757 } 1758 1759 /* 1760 * Get space to change directory entries into fs independent format. 1761 */ 1762 iovp = GET_UIO_STRUCT(uio)->uio_iov; 1763 bytes_wanted = iovp->iov_len; 1764 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1) { 1765 bufsize = bytes_wanted; 1766 outbuf = kmem_alloc(bufsize, KM_SLEEP); 1767 odp = (struct dirent64 *)outbuf; 1768 } else { 1769 bufsize = bytes_wanted; 1770 outbuf = NULL; 1771 odp = (struct dirent64 *)iovp->iov_base; 1772 } 1773 1774 if (ncookies != NULL) { 1775 /* 1776 * Minimum entry size is dirent size and 1 byte for a file name. 1777 */ 1778 ncooks = zfs_uio_resid(uio) / (sizeof (struct dirent) - 1779 sizeof (((struct dirent *)NULL)->d_name) + 1); 1780 cooks = malloc(ncooks * sizeof (*cooks), M_TEMP, M_WAITOK); 1781 *cookies = cooks; 1782 *ncookies = ncooks; 1783 } 1784 1785 /* 1786 * Transform to file-system independent format 1787 */ 1788 outcount = 0; 1789 while (outcount < bytes_wanted) { 1790 ino64_t objnum; 1791 ushort_t reclen; 1792 off64_t *next = NULL; 1793 1794 /* 1795 * Special case `.', `..', and `.zfs'. 1796 */ 1797 if (offset == 0) { 1798 (void) strcpy(zap.za_name, "."); 1799 zap.za_normalization_conflict = 0; 1800 objnum = zp->z_id; 1801 type = DT_DIR; 1802 } else if (offset == 1) { 1803 (void) strcpy(zap.za_name, ".."); 1804 zap.za_normalization_conflict = 0; 1805 objnum = parent; 1806 type = DT_DIR; 1807 } else if (offset == 2 && zfs_show_ctldir(zp)) { 1808 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 1809 zap.za_normalization_conflict = 0; 1810 objnum = ZFSCTL_INO_ROOT; 1811 type = DT_DIR; 1812 } else { 1813 /* 1814 * Grab next entry. 1815 */ 1816 if ((error = zap_cursor_retrieve(&zc, &zap))) { 1817 if ((*eofp = (error == ENOENT)) != 0) 1818 break; 1819 else 1820 goto update; 1821 } 1822 1823 if (zap.za_integer_length != 8 || 1824 zap.za_num_integers != 1) { 1825 cmn_err(CE_WARN, "zap_readdir: bad directory " 1826 "entry, obj = %lld, offset = %lld\n", 1827 (u_longlong_t)zp->z_id, 1828 (u_longlong_t)offset); 1829 error = SET_ERROR(ENXIO); 1830 goto update; 1831 } 1832 1833 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 1834 /* 1835 * MacOS X can extract the object type here such as: 1836 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 1837 */ 1838 type = ZFS_DIRENT_TYPE(zap.za_first_integer); 1839 } 1840 1841 reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1842 1843 /* 1844 * Will this entry fit in the buffer? 1845 */ 1846 if (outcount + reclen > bufsize) { 1847 /* 1848 * Did we manage to fit anything in the buffer? 1849 */ 1850 if (!outcount) { 1851 error = SET_ERROR(EINVAL); 1852 goto update; 1853 } 1854 break; 1855 } 1856 /* 1857 * Add normal entry: 1858 */ 1859 odp->d_ino = objnum; 1860 odp->d_reclen = reclen; 1861 odp->d_namlen = strlen(zap.za_name); 1862 /* NOTE: d_off is the offset for the *next* entry. */ 1863 next = &odp->d_off; 1864 strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1); 1865 odp->d_type = type; 1866 dirent_terminate(odp); 1867 odp = (dirent64_t *)((intptr_t)odp + reclen); 1868 1869 outcount += reclen; 1870 1871 ASSERT3S(outcount, <=, bufsize); 1872 1873 /* Prefetch znode */ 1874 if (prefetch) 1875 dmu_prefetch(os, objnum, 0, 0, 0, 1876 ZIO_PRIORITY_SYNC_READ); 1877 1878 /* 1879 * Move to the next entry, fill in the previous offset. 1880 */ 1881 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1882 zap_cursor_advance(&zc); 1883 offset = zap_cursor_serialize(&zc); 1884 } else { 1885 offset += 1; 1886 } 1887 1888 /* Fill the offset right after advancing the cursor. */ 1889 if (next != NULL) 1890 *next = offset; 1891 if (cooks != NULL) { 1892 *cooks++ = offset; 1893 ncooks--; 1894 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks)); 1895 } 1896 } 1897 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1898 1899 /* Subtract unused cookies */ 1900 if (ncookies != NULL) 1901 *ncookies -= ncooks; 1902 1903 if (zfs_uio_segflg(uio) == UIO_SYSSPACE && zfs_uio_iovcnt(uio) == 1) { 1904 iovp->iov_base += outcount; 1905 iovp->iov_len -= outcount; 1906 zfs_uio_resid(uio) -= outcount; 1907 } else if ((error = 1908 zfs_uiomove(outbuf, (long)outcount, UIO_READ, uio))) { 1909 /* 1910 * Reset the pointer. 1911 */ 1912 offset = zfs_uio_offset(uio); 1913 } 1914 1915 update: 1916 zap_cursor_fini(&zc); 1917 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1) 1918 kmem_free(outbuf, bufsize); 1919 1920 if (error == ENOENT) 1921 error = 0; 1922 1923 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 1924 1925 zfs_uio_setoffset(uio, offset); 1926 zfs_exit(zfsvfs, FTAG); 1927 if (error != 0 && cookies != NULL) { 1928 free(*cookies, M_TEMP); 1929 *cookies = NULL; 1930 *ncookies = 0; 1931 } 1932 return (error); 1933 } 1934 1935 /* 1936 * Get the requested file attributes and place them in the provided 1937 * vattr structure. 1938 * 1939 * IN: vp - vnode of file. 1940 * vap - va_mask identifies requested attributes. 1941 * If AT_XVATTR set, then optional attrs are requested 1942 * flags - ATTR_NOACLCHECK (CIFS server context) 1943 * cr - credentials of caller. 1944 * 1945 * OUT: vap - attribute values. 1946 * 1947 * RETURN: 0 (always succeeds). 1948 */ 1949 static int 1950 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 1951 { 1952 znode_t *zp = VTOZ(vp); 1953 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1954 int error = 0; 1955 uint32_t blksize; 1956 u_longlong_t nblocks; 1957 uint64_t mtime[2], ctime[2], crtime[2], rdev; 1958 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 1959 xoptattr_t *xoap = NULL; 1960 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 1961 sa_bulk_attr_t bulk[4]; 1962 int count = 0; 1963 1964 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 1965 return (error); 1966 1967 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 1968 1969 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 1970 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 1971 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16); 1972 if (vp->v_type == VBLK || vp->v_type == VCHR) 1973 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, 1974 &rdev, 8); 1975 1976 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 1977 zfs_exit(zfsvfs, FTAG); 1978 return (error); 1979 } 1980 1981 /* 1982 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 1983 * Also, if we are the owner don't bother, since owner should 1984 * always be allowed to read basic attributes of file. 1985 */ 1986 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && 1987 (vap->va_uid != crgetuid(cr))) { 1988 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 1989 skipaclchk, cr, NULL))) { 1990 zfs_exit(zfsvfs, FTAG); 1991 return (error); 1992 } 1993 } 1994 1995 /* 1996 * Return all attributes. It's cheaper to provide the answer 1997 * than to determine whether we were asked the question. 1998 */ 1999 2000 vap->va_type = IFTOVT(zp->z_mode); 2001 vap->va_mode = zp->z_mode & ~S_IFMT; 2002 vn_fsid(vp, vap); 2003 vap->va_nodeid = zp->z_id; 2004 vap->va_nlink = zp->z_links; 2005 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp) && 2006 zp->z_links < ZFS_LINK_MAX) 2007 vap->va_nlink++; 2008 vap->va_size = zp->z_size; 2009 if (vp->v_type == VBLK || vp->v_type == VCHR) 2010 vap->va_rdev = zfs_cmpldev(rdev); 2011 vap->va_gen = zp->z_gen; 2012 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */ 2013 vap->va_filerev = zp->z_seq; 2014 2015 /* 2016 * Add in any requested optional attributes and the create time. 2017 * Also set the corresponding bits in the returned attribute bitmap. 2018 */ 2019 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 2020 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 2021 xoap->xoa_archive = 2022 ((zp->z_pflags & ZFS_ARCHIVE) != 0); 2023 XVA_SET_RTN(xvap, XAT_ARCHIVE); 2024 } 2025 2026 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 2027 xoap->xoa_readonly = 2028 ((zp->z_pflags & ZFS_READONLY) != 0); 2029 XVA_SET_RTN(xvap, XAT_READONLY); 2030 } 2031 2032 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 2033 xoap->xoa_system = 2034 ((zp->z_pflags & ZFS_SYSTEM) != 0); 2035 XVA_SET_RTN(xvap, XAT_SYSTEM); 2036 } 2037 2038 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 2039 xoap->xoa_hidden = 2040 ((zp->z_pflags & ZFS_HIDDEN) != 0); 2041 XVA_SET_RTN(xvap, XAT_HIDDEN); 2042 } 2043 2044 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2045 xoap->xoa_nounlink = 2046 ((zp->z_pflags & ZFS_NOUNLINK) != 0); 2047 XVA_SET_RTN(xvap, XAT_NOUNLINK); 2048 } 2049 2050 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2051 xoap->xoa_immutable = 2052 ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 2053 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 2054 } 2055 2056 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2057 xoap->xoa_appendonly = 2058 ((zp->z_pflags & ZFS_APPENDONLY) != 0); 2059 XVA_SET_RTN(xvap, XAT_APPENDONLY); 2060 } 2061 2062 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2063 xoap->xoa_nodump = 2064 ((zp->z_pflags & ZFS_NODUMP) != 0); 2065 XVA_SET_RTN(xvap, XAT_NODUMP); 2066 } 2067 2068 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 2069 xoap->xoa_opaque = 2070 ((zp->z_pflags & ZFS_OPAQUE) != 0); 2071 XVA_SET_RTN(xvap, XAT_OPAQUE); 2072 } 2073 2074 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 2075 xoap->xoa_av_quarantined = 2076 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 2077 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 2078 } 2079 2080 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2081 xoap->xoa_av_modified = 2082 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 2083 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 2084 } 2085 2086 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 2087 vp->v_type == VREG) { 2088 zfs_sa_get_scanstamp(zp, xvap); 2089 } 2090 2091 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2092 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 2093 XVA_SET_RTN(xvap, XAT_REPARSE); 2094 } 2095 if (XVA_ISSET_REQ(xvap, XAT_GEN)) { 2096 xoap->xoa_generation = zp->z_gen; 2097 XVA_SET_RTN(xvap, XAT_GEN); 2098 } 2099 2100 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { 2101 xoap->xoa_offline = 2102 ((zp->z_pflags & ZFS_OFFLINE) != 0); 2103 XVA_SET_RTN(xvap, XAT_OFFLINE); 2104 } 2105 2106 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { 2107 xoap->xoa_sparse = 2108 ((zp->z_pflags & ZFS_SPARSE) != 0); 2109 XVA_SET_RTN(xvap, XAT_SPARSE); 2110 } 2111 2112 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) { 2113 xoap->xoa_projinherit = 2114 ((zp->z_pflags & ZFS_PROJINHERIT) != 0); 2115 XVA_SET_RTN(xvap, XAT_PROJINHERIT); 2116 } 2117 2118 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) { 2119 xoap->xoa_projid = zp->z_projid; 2120 XVA_SET_RTN(xvap, XAT_PROJID); 2121 } 2122 } 2123 2124 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 2125 ZFS_TIME_DECODE(&vap->va_mtime, mtime); 2126 ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2127 ZFS_TIME_DECODE(&vap->va_birthtime, crtime); 2128 2129 2130 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks); 2131 vap->va_blksize = blksize; 2132 vap->va_bytes = nblocks << 9; /* nblocks * 512 */ 2133 2134 if (zp->z_blksz == 0) { 2135 /* 2136 * Block size hasn't been set; suggest maximal I/O transfers. 2137 */ 2138 vap->va_blksize = zfsvfs->z_max_blksz; 2139 } 2140 2141 zfs_exit(zfsvfs, FTAG); 2142 return (0); 2143 } 2144 2145 /* 2146 * Set the file attributes to the values contained in the 2147 * vattr structure. 2148 * 2149 * IN: zp - znode of file to be modified. 2150 * vap - new attribute values. 2151 * If AT_XVATTR set, then optional attrs are being set 2152 * flags - ATTR_UTIME set if non-default time values provided. 2153 * - ATTR_NOACLCHECK (CIFS context only). 2154 * cr - credentials of caller. 2155 * mnt_ns - Unused on FreeBSD 2156 * 2157 * RETURN: 0 on success, error code on failure. 2158 * 2159 * Timestamps: 2160 * vp - ctime updated, mtime updated if size changed. 2161 */ 2162 int 2163 zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr, zuserns_t *mnt_ns) 2164 { 2165 vnode_t *vp = ZTOV(zp); 2166 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2167 objset_t *os; 2168 zilog_t *zilog; 2169 dmu_tx_t *tx; 2170 vattr_t oldva; 2171 xvattr_t tmpxvattr; 2172 uint_t mask = vap->va_mask; 2173 uint_t saved_mask = 0; 2174 uint64_t saved_mode; 2175 int trim_mask = 0; 2176 uint64_t new_mode; 2177 uint64_t new_uid, new_gid; 2178 uint64_t xattr_obj; 2179 uint64_t mtime[2], ctime[2]; 2180 uint64_t projid = ZFS_INVALID_PROJID; 2181 znode_t *attrzp; 2182 int need_policy = FALSE; 2183 int err, err2; 2184 zfs_fuid_info_t *fuidp = NULL; 2185 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 2186 xoptattr_t *xoap; 2187 zfs_acl_t *aclp; 2188 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2189 boolean_t fuid_dirtied = B_FALSE; 2190 sa_bulk_attr_t bulk[7], xattr_bulk[7]; 2191 int count = 0, xattr_count = 0; 2192 2193 if (mask == 0) 2194 return (0); 2195 2196 if (mask & AT_NOSET) 2197 return (SET_ERROR(EINVAL)); 2198 2199 if ((err = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 2200 return (err); 2201 2202 os = zfsvfs->z_os; 2203 zilog = zfsvfs->z_log; 2204 2205 /* 2206 * Make sure that if we have ephemeral uid/gid or xvattr specified 2207 * that file system is at proper version level 2208 */ 2209 2210 if (zfsvfs->z_use_fuids == B_FALSE && 2211 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 2212 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 2213 (mask & AT_XVATTR))) { 2214 zfs_exit(zfsvfs, FTAG); 2215 return (SET_ERROR(EINVAL)); 2216 } 2217 2218 if (mask & AT_SIZE && vp->v_type == VDIR) { 2219 zfs_exit(zfsvfs, FTAG); 2220 return (SET_ERROR(EISDIR)); 2221 } 2222 2223 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 2224 zfs_exit(zfsvfs, FTAG); 2225 return (SET_ERROR(EINVAL)); 2226 } 2227 2228 /* 2229 * If this is an xvattr_t, then get a pointer to the structure of 2230 * optional attributes. If this is NULL, then we have a vattr_t. 2231 */ 2232 xoap = xva_getxoptattr(xvap); 2233 2234 xva_init(&tmpxvattr); 2235 2236 /* 2237 * Immutable files can only alter immutable bit and atime 2238 */ 2239 if ((zp->z_pflags & ZFS_IMMUTABLE) && 2240 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 2241 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 2242 zfs_exit(zfsvfs, FTAG); 2243 return (SET_ERROR(EPERM)); 2244 } 2245 2246 /* 2247 * Note: ZFS_READONLY is handled in zfs_zaccess_common. 2248 */ 2249 2250 /* 2251 * Verify timestamps doesn't overflow 32 bits. 2252 * ZFS can handle large timestamps, but 32bit syscalls can't 2253 * handle times greater than 2039. This check should be removed 2254 * once large timestamps are fully supported. 2255 */ 2256 if (mask & (AT_ATIME | AT_MTIME)) { 2257 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 2258 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 2259 zfs_exit(zfsvfs, FTAG); 2260 return (SET_ERROR(EOVERFLOW)); 2261 } 2262 } 2263 if (xoap != NULL && (mask & AT_XVATTR)) { 2264 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME) && 2265 TIMESPEC_OVERFLOW(&vap->va_birthtime)) { 2266 zfs_exit(zfsvfs, FTAG); 2267 return (SET_ERROR(EOVERFLOW)); 2268 } 2269 2270 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) { 2271 if (!dmu_objset_projectquota_enabled(os) || 2272 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode))) { 2273 zfs_exit(zfsvfs, FTAG); 2274 return (SET_ERROR(EOPNOTSUPP)); 2275 } 2276 2277 projid = xoap->xoa_projid; 2278 if (unlikely(projid == ZFS_INVALID_PROJID)) { 2279 zfs_exit(zfsvfs, FTAG); 2280 return (SET_ERROR(EINVAL)); 2281 } 2282 2283 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID) 2284 projid = ZFS_INVALID_PROJID; 2285 else 2286 need_policy = TRUE; 2287 } 2288 2289 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) && 2290 (xoap->xoa_projinherit != 2291 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) && 2292 (!dmu_objset_projectquota_enabled(os) || 2293 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode)))) { 2294 zfs_exit(zfsvfs, FTAG); 2295 return (SET_ERROR(EOPNOTSUPP)); 2296 } 2297 } 2298 2299 attrzp = NULL; 2300 aclp = NULL; 2301 2302 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2303 zfs_exit(zfsvfs, FTAG); 2304 return (SET_ERROR(EROFS)); 2305 } 2306 2307 /* 2308 * First validate permissions 2309 */ 2310 2311 if (mask & AT_SIZE) { 2312 /* 2313 * XXX - Note, we are not providing any open 2314 * mode flags here (like FNDELAY), so we may 2315 * block if there are locks present... this 2316 * should be addressed in openat(). 2317 */ 2318 /* XXX - would it be OK to generate a log record here? */ 2319 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 2320 if (err) { 2321 zfs_exit(zfsvfs, FTAG); 2322 return (err); 2323 } 2324 } 2325 2326 if (mask & (AT_ATIME|AT_MTIME) || 2327 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 2328 XVA_ISSET_REQ(xvap, XAT_READONLY) || 2329 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 2330 XVA_ISSET_REQ(xvap, XAT_OFFLINE) || 2331 XVA_ISSET_REQ(xvap, XAT_SPARSE) || 2332 XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 2333 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 2334 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 2335 skipaclchk, cr, mnt_ns); 2336 } 2337 2338 if (mask & (AT_UID|AT_GID)) { 2339 int idmask = (mask & (AT_UID|AT_GID)); 2340 int take_owner; 2341 int take_group; 2342 2343 /* 2344 * NOTE: even if a new mode is being set, 2345 * we may clear S_ISUID/S_ISGID bits. 2346 */ 2347 2348 if (!(mask & AT_MODE)) 2349 vap->va_mode = zp->z_mode; 2350 2351 /* 2352 * Take ownership or chgrp to group we are a member of 2353 */ 2354 2355 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 2356 take_group = (mask & AT_GID) && 2357 zfs_groupmember(zfsvfs, vap->va_gid, cr); 2358 2359 /* 2360 * If both AT_UID and AT_GID are set then take_owner and 2361 * take_group must both be set in order to allow taking 2362 * ownership. 2363 * 2364 * Otherwise, send the check through secpolicy_vnode_setattr() 2365 * 2366 */ 2367 2368 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2369 ((idmask == AT_UID) && take_owner) || 2370 ((idmask == AT_GID) && take_group)) { 2371 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 2372 skipaclchk, cr, mnt_ns) == 0) { 2373 /* 2374 * Remove setuid/setgid for non-privileged users 2375 */ 2376 secpolicy_setid_clear(vap, vp, cr); 2377 trim_mask = (mask & (AT_UID|AT_GID)); 2378 } else { 2379 need_policy = TRUE; 2380 } 2381 } else { 2382 need_policy = TRUE; 2383 } 2384 } 2385 2386 oldva.va_mode = zp->z_mode; 2387 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 2388 if (mask & AT_XVATTR) { 2389 /* 2390 * Update xvattr mask to include only those attributes 2391 * that are actually changing. 2392 * 2393 * the bits will be restored prior to actually setting 2394 * the attributes so the caller thinks they were set. 2395 */ 2396 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2397 if (xoap->xoa_appendonly != 2398 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 2399 need_policy = TRUE; 2400 } else { 2401 XVA_CLR_REQ(xvap, XAT_APPENDONLY); 2402 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 2403 } 2404 } 2405 2406 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) { 2407 if (xoap->xoa_projinherit != 2408 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) { 2409 need_policy = TRUE; 2410 } else { 2411 XVA_CLR_REQ(xvap, XAT_PROJINHERIT); 2412 XVA_SET_REQ(&tmpxvattr, XAT_PROJINHERIT); 2413 } 2414 } 2415 2416 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2417 if (xoap->xoa_nounlink != 2418 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 2419 need_policy = TRUE; 2420 } else { 2421 XVA_CLR_REQ(xvap, XAT_NOUNLINK); 2422 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 2423 } 2424 } 2425 2426 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2427 if (xoap->xoa_immutable != 2428 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 2429 need_policy = TRUE; 2430 } else { 2431 XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 2432 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 2433 } 2434 } 2435 2436 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2437 if (xoap->xoa_nodump != 2438 ((zp->z_pflags & ZFS_NODUMP) != 0)) { 2439 need_policy = TRUE; 2440 } else { 2441 XVA_CLR_REQ(xvap, XAT_NODUMP); 2442 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 2443 } 2444 } 2445 2446 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2447 if (xoap->xoa_av_modified != 2448 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 2449 need_policy = TRUE; 2450 } else { 2451 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 2452 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 2453 } 2454 } 2455 2456 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 2457 if ((vp->v_type != VREG && 2458 xoap->xoa_av_quarantined) || 2459 xoap->xoa_av_quarantined != 2460 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 2461 need_policy = TRUE; 2462 } else { 2463 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 2464 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 2465 } 2466 } 2467 2468 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2469 zfs_exit(zfsvfs, FTAG); 2470 return (SET_ERROR(EPERM)); 2471 } 2472 2473 if (need_policy == FALSE && 2474 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 2475 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 2476 need_policy = TRUE; 2477 } 2478 } 2479 2480 if (mask & AT_MODE) { 2481 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr, 2482 mnt_ns) == 0) { 2483 err = secpolicy_setid_setsticky_clear(vp, vap, 2484 &oldva, cr); 2485 if (err) { 2486 zfs_exit(zfsvfs, FTAG); 2487 return (err); 2488 } 2489 trim_mask |= AT_MODE; 2490 } else { 2491 need_policy = TRUE; 2492 } 2493 } 2494 2495 if (need_policy) { 2496 /* 2497 * If trim_mask is set then take ownership 2498 * has been granted or write_acl is present and user 2499 * has the ability to modify mode. In that case remove 2500 * UID|GID and or MODE from mask so that 2501 * secpolicy_vnode_setattr() doesn't revoke it. 2502 */ 2503 2504 if (trim_mask) { 2505 saved_mask = vap->va_mask; 2506 vap->va_mask &= ~trim_mask; 2507 if (trim_mask & AT_MODE) { 2508 /* 2509 * Save the mode, as secpolicy_vnode_setattr() 2510 * will overwrite it with ova.va_mode. 2511 */ 2512 saved_mode = vap->va_mode; 2513 } 2514 } 2515 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 2516 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2517 if (err) { 2518 zfs_exit(zfsvfs, FTAG); 2519 return (err); 2520 } 2521 2522 if (trim_mask) { 2523 vap->va_mask |= saved_mask; 2524 if (trim_mask & AT_MODE) { 2525 /* 2526 * Recover the mode after 2527 * secpolicy_vnode_setattr(). 2528 */ 2529 vap->va_mode = saved_mode; 2530 } 2531 } 2532 } 2533 2534 /* 2535 * secpolicy_vnode_setattr, or take ownership may have 2536 * changed va_mask 2537 */ 2538 mask = vap->va_mask; 2539 2540 if ((mask & (AT_UID | AT_GID)) || projid != ZFS_INVALID_PROJID) { 2541 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 2542 &xattr_obj, sizeof (xattr_obj)); 2543 2544 if (err == 0 && xattr_obj) { 2545 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 2546 if (err == 0) { 2547 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE); 2548 if (err != 0) 2549 vrele(ZTOV(attrzp)); 2550 } 2551 if (err) 2552 goto out2; 2553 } 2554 if (mask & AT_UID) { 2555 new_uid = zfs_fuid_create(zfsvfs, 2556 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 2557 if (new_uid != zp->z_uid && 2558 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT, 2559 new_uid)) { 2560 if (attrzp) 2561 vput(ZTOV(attrzp)); 2562 err = SET_ERROR(EDQUOT); 2563 goto out2; 2564 } 2565 } 2566 2567 if (mask & AT_GID) { 2568 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 2569 cr, ZFS_GROUP, &fuidp); 2570 if (new_gid != zp->z_gid && 2571 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT, 2572 new_gid)) { 2573 if (attrzp) 2574 vput(ZTOV(attrzp)); 2575 err = SET_ERROR(EDQUOT); 2576 goto out2; 2577 } 2578 } 2579 2580 if (projid != ZFS_INVALID_PROJID && 2581 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) { 2582 if (attrzp) 2583 vput(ZTOV(attrzp)); 2584 err = SET_ERROR(EDQUOT); 2585 goto out2; 2586 } 2587 } 2588 tx = dmu_tx_create(os); 2589 2590 if (mask & AT_MODE) { 2591 uint64_t pmode = zp->z_mode; 2592 uint64_t acl_obj; 2593 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2594 2595 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED && 2596 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) { 2597 err = SET_ERROR(EPERM); 2598 goto out; 2599 } 2600 2601 if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))) 2602 goto out; 2603 2604 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 2605 /* 2606 * Are we upgrading ACL from old V0 format 2607 * to V1 format? 2608 */ 2609 if (zfsvfs->z_version >= ZPL_VERSION_FUID && 2610 zfs_znode_acl_version(zp) == 2611 ZFS_ACL_VERSION_INITIAL) { 2612 dmu_tx_hold_free(tx, acl_obj, 0, 2613 DMU_OBJECT_END); 2614 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2615 0, aclp->z_acl_bytes); 2616 } else { 2617 dmu_tx_hold_write(tx, acl_obj, 0, 2618 aclp->z_acl_bytes); 2619 } 2620 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 2621 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2622 0, aclp->z_acl_bytes); 2623 } 2624 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 2625 } else { 2626 if (((mask & AT_XVATTR) && 2627 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) || 2628 (projid != ZFS_INVALID_PROJID && 2629 !(zp->z_pflags & ZFS_PROJID))) 2630 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 2631 else 2632 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 2633 } 2634 2635 if (attrzp) { 2636 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 2637 } 2638 2639 fuid_dirtied = zfsvfs->z_fuid_dirty; 2640 if (fuid_dirtied) 2641 zfs_fuid_txhold(zfsvfs, tx); 2642 2643 zfs_sa_upgrade_txholds(tx, zp); 2644 2645 err = dmu_tx_assign(tx, TXG_WAIT); 2646 if (err) 2647 goto out; 2648 2649 count = 0; 2650 /* 2651 * Set each attribute requested. 2652 * We group settings according to the locks they need to acquire. 2653 * 2654 * Note: you cannot set ctime directly, although it will be 2655 * updated as a side-effect of calling this function. 2656 */ 2657 2658 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) { 2659 /* 2660 * For the existed object that is upgraded from old system, 2661 * its on-disk layout has no slot for the project ID attribute. 2662 * But quota accounting logic needs to access related slots by 2663 * offset directly. So we need to adjust old objects' layout 2664 * to make the project ID to some unified and fixed offset. 2665 */ 2666 if (attrzp) 2667 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid); 2668 if (err == 0) 2669 err = sa_add_projid(zp->z_sa_hdl, tx, projid); 2670 2671 if (unlikely(err == EEXIST)) 2672 err = 0; 2673 else if (err != 0) 2674 goto out; 2675 else 2676 projid = ZFS_INVALID_PROJID; 2677 } 2678 2679 if (mask & (AT_UID|AT_GID|AT_MODE)) 2680 mutex_enter(&zp->z_acl_lock); 2681 2682 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 2683 &zp->z_pflags, sizeof (zp->z_pflags)); 2684 2685 if (attrzp) { 2686 if (mask & (AT_UID|AT_GID|AT_MODE)) 2687 mutex_enter(&attrzp->z_acl_lock); 2688 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2689 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 2690 sizeof (attrzp->z_pflags)); 2691 if (projid != ZFS_INVALID_PROJID) { 2692 attrzp->z_projid = projid; 2693 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2694 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid, 2695 sizeof (attrzp->z_projid)); 2696 } 2697 } 2698 2699 if (mask & (AT_UID|AT_GID)) { 2700 2701 if (mask & AT_UID) { 2702 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 2703 &new_uid, sizeof (new_uid)); 2704 zp->z_uid = new_uid; 2705 if (attrzp) { 2706 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2707 SA_ZPL_UID(zfsvfs), NULL, &new_uid, 2708 sizeof (new_uid)); 2709 attrzp->z_uid = new_uid; 2710 } 2711 } 2712 2713 if (mask & AT_GID) { 2714 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 2715 NULL, &new_gid, sizeof (new_gid)); 2716 zp->z_gid = new_gid; 2717 if (attrzp) { 2718 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2719 SA_ZPL_GID(zfsvfs), NULL, &new_gid, 2720 sizeof (new_gid)); 2721 attrzp->z_gid = new_gid; 2722 } 2723 } 2724 if (!(mask & AT_MODE)) { 2725 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 2726 NULL, &new_mode, sizeof (new_mode)); 2727 new_mode = zp->z_mode; 2728 } 2729 err = zfs_acl_chown_setattr(zp); 2730 ASSERT0(err); 2731 if (attrzp) { 2732 vn_seqc_write_begin(ZTOV(attrzp)); 2733 err = zfs_acl_chown_setattr(attrzp); 2734 vn_seqc_write_end(ZTOV(attrzp)); 2735 ASSERT0(err); 2736 } 2737 } 2738 2739 if (mask & AT_MODE) { 2740 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 2741 &new_mode, sizeof (new_mode)); 2742 zp->z_mode = new_mode; 2743 ASSERT3P(aclp, !=, NULL); 2744 err = zfs_aclset_common(zp, aclp, cr, tx); 2745 ASSERT0(err); 2746 if (zp->z_acl_cached) 2747 zfs_acl_free(zp->z_acl_cached); 2748 zp->z_acl_cached = aclp; 2749 aclp = NULL; 2750 } 2751 2752 2753 if (mask & AT_ATIME) { 2754 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 2755 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 2756 &zp->z_atime, sizeof (zp->z_atime)); 2757 } 2758 2759 if (mask & AT_MTIME) { 2760 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 2761 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 2762 mtime, sizeof (mtime)); 2763 } 2764 2765 if (projid != ZFS_INVALID_PROJID) { 2766 zp->z_projid = projid; 2767 SA_ADD_BULK_ATTR(bulk, count, 2768 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid, 2769 sizeof (zp->z_projid)); 2770 } 2771 2772 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 2773 if (mask & AT_SIZE && !(mask & AT_MTIME)) { 2774 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 2775 NULL, mtime, sizeof (mtime)); 2776 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 2777 &ctime, sizeof (ctime)); 2778 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime); 2779 } else if (mask != 0) { 2780 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 2781 &ctime, sizeof (ctime)); 2782 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime); 2783 if (attrzp) { 2784 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2785 SA_ZPL_CTIME(zfsvfs), NULL, 2786 &ctime, sizeof (ctime)); 2787 zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 2788 mtime, ctime); 2789 } 2790 } 2791 2792 /* 2793 * Do this after setting timestamps to prevent timestamp 2794 * update from toggling bit 2795 */ 2796 2797 if (xoap && (mask & AT_XVATTR)) { 2798 2799 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) 2800 xoap->xoa_createtime = vap->va_birthtime; 2801 /* 2802 * restore trimmed off masks 2803 * so that return masks can be set for caller. 2804 */ 2805 2806 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 2807 XVA_SET_REQ(xvap, XAT_APPENDONLY); 2808 } 2809 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 2810 XVA_SET_REQ(xvap, XAT_NOUNLINK); 2811 } 2812 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 2813 XVA_SET_REQ(xvap, XAT_IMMUTABLE); 2814 } 2815 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 2816 XVA_SET_REQ(xvap, XAT_NODUMP); 2817 } 2818 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 2819 XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 2820 } 2821 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 2822 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 2823 } 2824 if (XVA_ISSET_REQ(&tmpxvattr, XAT_PROJINHERIT)) { 2825 XVA_SET_REQ(xvap, XAT_PROJINHERIT); 2826 } 2827 2828 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 2829 ASSERT3S(vp->v_type, ==, VREG); 2830 2831 zfs_xvattr_set(zp, xvap, tx); 2832 } 2833 2834 if (fuid_dirtied) 2835 zfs_fuid_sync(zfsvfs, tx); 2836 2837 if (mask != 0) 2838 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 2839 2840 if (mask & (AT_UID|AT_GID|AT_MODE)) 2841 mutex_exit(&zp->z_acl_lock); 2842 2843 if (attrzp) { 2844 if (mask & (AT_UID|AT_GID|AT_MODE)) 2845 mutex_exit(&attrzp->z_acl_lock); 2846 } 2847 out: 2848 if (err == 0 && attrzp) { 2849 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 2850 xattr_count, tx); 2851 ASSERT0(err2); 2852 } 2853 2854 if (attrzp) 2855 vput(ZTOV(attrzp)); 2856 2857 if (aclp) 2858 zfs_acl_free(aclp); 2859 2860 if (fuidp) { 2861 zfs_fuid_info_free(fuidp); 2862 fuidp = NULL; 2863 } 2864 2865 if (err) { 2866 dmu_tx_abort(tx); 2867 } else { 2868 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 2869 dmu_tx_commit(tx); 2870 } 2871 2872 out2: 2873 if (os->os_sync == ZFS_SYNC_ALWAYS) 2874 zil_commit(zilog, 0); 2875 2876 zfs_exit(zfsvfs, FTAG); 2877 return (err); 2878 } 2879 2880 /* 2881 * Look up the directory entries corresponding to the source and target 2882 * directory/name pairs. 2883 */ 2884 static int 2885 zfs_rename_relock_lookup(znode_t *sdzp, const struct componentname *scnp, 2886 znode_t **szpp, znode_t *tdzp, const struct componentname *tcnp, 2887 znode_t **tzpp) 2888 { 2889 zfsvfs_t *zfsvfs; 2890 znode_t *szp, *tzp; 2891 int error; 2892 2893 /* 2894 * Before using sdzp and tdzp we must ensure that they are live. 2895 * As a porting legacy from illumos we have two things to worry 2896 * about. One is typical for FreeBSD and it is that the vnode is 2897 * not reclaimed (doomed). The other is that the znode is live. 2898 * The current code can invalidate the znode without acquiring the 2899 * corresponding vnode lock if the object represented by the znode 2900 * and vnode is no longer valid after a rollback or receive operation. 2901 * z_teardown_lock hidden behind zfs_enter and zfs_exit is the lock 2902 * that protects the znodes from the invalidation. 2903 */ 2904 zfsvfs = sdzp->z_zfsvfs; 2905 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs); 2906 if ((error = zfs_enter_verify_zp(zfsvfs, sdzp, FTAG)) != 0) 2907 return (error); 2908 if ((error = zfs_verify_zp(tdzp)) != 0) { 2909 zfs_exit(zfsvfs, FTAG); 2910 return (error); 2911 } 2912 2913 /* 2914 * Re-resolve svp to be certain it still exists and fetch the 2915 * correct vnode. 2916 */ 2917 error = zfs_dirent_lookup(sdzp, scnp->cn_nameptr, &szp, ZEXISTS); 2918 if (error != 0) { 2919 /* Source entry invalid or not there. */ 2920 if ((scnp->cn_flags & ISDOTDOT) != 0 || 2921 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.')) 2922 error = SET_ERROR(EINVAL); 2923 goto out; 2924 } 2925 *szpp = szp; 2926 2927 /* 2928 * Re-resolve tvp, if it disappeared we just carry on. 2929 */ 2930 error = zfs_dirent_lookup(tdzp, tcnp->cn_nameptr, &tzp, 0); 2931 if (error != 0) { 2932 vrele(ZTOV(szp)); 2933 if ((tcnp->cn_flags & ISDOTDOT) != 0) 2934 error = SET_ERROR(EINVAL); 2935 goto out; 2936 } 2937 *tzpp = tzp; 2938 out: 2939 zfs_exit(zfsvfs, FTAG); 2940 return (error); 2941 } 2942 2943 /* 2944 * We acquire all but fdvp locks using non-blocking acquisitions. If we 2945 * fail to acquire any lock in the path we will drop all held locks, 2946 * acquire the new lock in a blocking fashion, and then release it and 2947 * restart the rename. This acquire/release step ensures that we do not 2948 * spin on a lock waiting for release. On error release all vnode locks 2949 * and decrement references the way tmpfs_rename() would do. 2950 */ 2951 static int 2952 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp, 2953 struct vnode *tdvp, struct vnode **tvpp, 2954 const struct componentname *scnp, const struct componentname *tcnp) 2955 { 2956 struct vnode *nvp, *svp, *tvp; 2957 znode_t *sdzp, *tdzp, *szp, *tzp; 2958 int error; 2959 2960 VOP_UNLOCK1(tdvp); 2961 if (*tvpp != NULL && *tvpp != tdvp) 2962 VOP_UNLOCK1(*tvpp); 2963 2964 relock: 2965 error = vn_lock(sdvp, LK_EXCLUSIVE); 2966 if (error) 2967 goto out; 2968 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT); 2969 if (error != 0) { 2970 VOP_UNLOCK1(sdvp); 2971 if (error != EBUSY) 2972 goto out; 2973 error = vn_lock(tdvp, LK_EXCLUSIVE); 2974 if (error) 2975 goto out; 2976 VOP_UNLOCK1(tdvp); 2977 goto relock; 2978 } 2979 tdzp = VTOZ(tdvp); 2980 sdzp = VTOZ(sdvp); 2981 2982 error = zfs_rename_relock_lookup(sdzp, scnp, &szp, tdzp, tcnp, &tzp); 2983 if (error != 0) { 2984 VOP_UNLOCK1(sdvp); 2985 VOP_UNLOCK1(tdvp); 2986 goto out; 2987 } 2988 svp = ZTOV(szp); 2989 tvp = tzp != NULL ? ZTOV(tzp) : NULL; 2990 2991 /* 2992 * Now try acquire locks on svp and tvp. 2993 */ 2994 nvp = svp; 2995 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT); 2996 if (error != 0) { 2997 VOP_UNLOCK1(sdvp); 2998 VOP_UNLOCK1(tdvp); 2999 if (tvp != NULL) 3000 vrele(tvp); 3001 if (error != EBUSY) { 3002 vrele(nvp); 3003 goto out; 3004 } 3005 error = vn_lock(nvp, LK_EXCLUSIVE); 3006 if (error != 0) { 3007 vrele(nvp); 3008 goto out; 3009 } 3010 VOP_UNLOCK1(nvp); 3011 /* 3012 * Concurrent rename race. 3013 * XXX ? 3014 */ 3015 if (nvp == tdvp) { 3016 vrele(nvp); 3017 error = SET_ERROR(EINVAL); 3018 goto out; 3019 } 3020 vrele(*svpp); 3021 *svpp = nvp; 3022 goto relock; 3023 } 3024 vrele(*svpp); 3025 *svpp = nvp; 3026 3027 if (*tvpp != NULL) 3028 vrele(*tvpp); 3029 *tvpp = NULL; 3030 if (tvp != NULL) { 3031 nvp = tvp; 3032 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT); 3033 if (error != 0) { 3034 VOP_UNLOCK1(sdvp); 3035 VOP_UNLOCK1(tdvp); 3036 VOP_UNLOCK1(*svpp); 3037 if (error != EBUSY) { 3038 vrele(nvp); 3039 goto out; 3040 } 3041 error = vn_lock(nvp, LK_EXCLUSIVE); 3042 if (error != 0) { 3043 vrele(nvp); 3044 goto out; 3045 } 3046 vput(nvp); 3047 goto relock; 3048 } 3049 *tvpp = nvp; 3050 } 3051 3052 return (0); 3053 3054 out: 3055 return (error); 3056 } 3057 3058 /* 3059 * Note that we must use VRELE_ASYNC in this function as it walks 3060 * up the directory tree and vrele may need to acquire an exclusive 3061 * lock if a last reference to a vnode is dropped. 3062 */ 3063 static int 3064 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp) 3065 { 3066 zfsvfs_t *zfsvfs; 3067 znode_t *zp, *zp1; 3068 uint64_t parent; 3069 int error; 3070 3071 zfsvfs = tdzp->z_zfsvfs; 3072 if (tdzp == szp) 3073 return (SET_ERROR(EINVAL)); 3074 if (tdzp == sdzp) 3075 return (0); 3076 if (tdzp->z_id == zfsvfs->z_root) 3077 return (0); 3078 zp = tdzp; 3079 for (;;) { 3080 ASSERT(!zp->z_unlinked); 3081 if ((error = sa_lookup(zp->z_sa_hdl, 3082 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) 3083 break; 3084 3085 if (parent == szp->z_id) { 3086 error = SET_ERROR(EINVAL); 3087 break; 3088 } 3089 if (parent == zfsvfs->z_root) 3090 break; 3091 if (parent == sdzp->z_id) 3092 break; 3093 3094 error = zfs_zget(zfsvfs, parent, &zp1); 3095 if (error != 0) 3096 break; 3097 3098 if (zp != tdzp) 3099 VN_RELE_ASYNC(ZTOV(zp), 3100 dsl_pool_zrele_taskq( 3101 dmu_objset_pool(zfsvfs->z_os))); 3102 zp = zp1; 3103 } 3104 3105 if (error == ENOTDIR) 3106 panic("checkpath: .. not a directory\n"); 3107 if (zp != tdzp) 3108 VN_RELE_ASYNC(ZTOV(zp), 3109 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs->z_os))); 3110 return (error); 3111 } 3112 3113 #if __FreeBSD_version < 1300124 3114 static void 3115 cache_vop_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp, 3116 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp) 3117 { 3118 3119 cache_purge(fvp); 3120 if (tvp != NULL) 3121 cache_purge(tvp); 3122 cache_purge_negative(tdvp); 3123 } 3124 #endif 3125 3126 static int 3127 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp, 3128 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp, 3129 cred_t *cr); 3130 3131 /* 3132 * Move an entry from the provided source directory to the target 3133 * directory. Change the entry name as indicated. 3134 * 3135 * IN: sdvp - Source directory containing the "old entry". 3136 * scnp - Old entry name. 3137 * tdvp - Target directory to contain the "new entry". 3138 * tcnp - New entry name. 3139 * cr - credentials of caller. 3140 * INOUT: svpp - Source file 3141 * tvpp - Target file, may point to NULL initially 3142 * 3143 * RETURN: 0 on success, error code on failure. 3144 * 3145 * Timestamps: 3146 * sdvp,tdvp - ctime|mtime updated 3147 */ 3148 static int 3149 zfs_do_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp, 3150 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp, 3151 cred_t *cr) 3152 { 3153 int error; 3154 3155 ASSERT_VOP_ELOCKED(tdvp, __func__); 3156 if (*tvpp != NULL) 3157 ASSERT_VOP_ELOCKED(*tvpp, __func__); 3158 3159 /* Reject renames across filesystems. */ 3160 if ((*svpp)->v_mount != tdvp->v_mount || 3161 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) { 3162 error = SET_ERROR(EXDEV); 3163 goto out; 3164 } 3165 3166 if (zfsctl_is_node(tdvp)) { 3167 error = SET_ERROR(EXDEV); 3168 goto out; 3169 } 3170 3171 /* 3172 * Lock all four vnodes to ensure safety and semantics of renaming. 3173 */ 3174 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp); 3175 if (error != 0) { 3176 /* no vnodes are locked in the case of error here */ 3177 return (error); 3178 } 3179 3180 error = zfs_do_rename_impl(sdvp, svpp, scnp, tdvp, tvpp, tcnp, cr); 3181 VOP_UNLOCK1(sdvp); 3182 VOP_UNLOCK1(*svpp); 3183 out: 3184 if (*tvpp != NULL) 3185 VOP_UNLOCK1(*tvpp); 3186 if (tdvp != *tvpp) 3187 VOP_UNLOCK1(tdvp); 3188 3189 return (error); 3190 } 3191 3192 static int 3193 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp, 3194 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp, 3195 cred_t *cr) 3196 { 3197 dmu_tx_t *tx; 3198 zfsvfs_t *zfsvfs; 3199 zilog_t *zilog; 3200 znode_t *tdzp, *sdzp, *tzp, *szp; 3201 const char *snm = scnp->cn_nameptr; 3202 const char *tnm = tcnp->cn_nameptr; 3203 int error; 3204 3205 tdzp = VTOZ(tdvp); 3206 sdzp = VTOZ(sdvp); 3207 zfsvfs = tdzp->z_zfsvfs; 3208 3209 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0) 3210 return (error); 3211 if ((error = zfs_verify_zp(sdzp)) != 0) { 3212 zfs_exit(zfsvfs, FTAG); 3213 return (error); 3214 } 3215 zilog = zfsvfs->z_log; 3216 3217 if (zfsvfs->z_utf8 && u8_validate(tnm, 3218 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3219 error = SET_ERROR(EILSEQ); 3220 goto out; 3221 } 3222 3223 /* If source and target are the same file, there is nothing to do. */ 3224 if ((*svpp) == (*tvpp)) { 3225 error = 0; 3226 goto out; 3227 } 3228 3229 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) || 3230 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR && 3231 (*tvpp)->v_mountedhere != NULL)) { 3232 error = SET_ERROR(EXDEV); 3233 goto out; 3234 } 3235 3236 szp = VTOZ(*svpp); 3237 if ((error = zfs_verify_zp(szp)) != 0) { 3238 zfs_exit(zfsvfs, FTAG); 3239 return (error); 3240 } 3241 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp); 3242 if (tzp != NULL) { 3243 if ((error = zfs_verify_zp(tzp)) != 0) { 3244 zfs_exit(zfsvfs, FTAG); 3245 return (error); 3246 } 3247 } 3248 3249 /* 3250 * This is to prevent the creation of links into attribute space 3251 * by renaming a linked file into/outof an attribute directory. 3252 * See the comment in zfs_link() for why this is considered bad. 3253 */ 3254 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 3255 error = SET_ERROR(EINVAL); 3256 goto out; 3257 } 3258 3259 /* 3260 * If we are using project inheritance, means if the directory has 3261 * ZFS_PROJINHERIT set, then its descendant directories will inherit 3262 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under 3263 * such case, we only allow renames into our tree when the project 3264 * IDs are the same. 3265 */ 3266 if (tdzp->z_pflags & ZFS_PROJINHERIT && 3267 tdzp->z_projid != szp->z_projid) { 3268 error = SET_ERROR(EXDEV); 3269 goto out; 3270 } 3271 3272 /* 3273 * Must have write access at the source to remove the old entry 3274 * and write access at the target to create the new entry. 3275 * Note that if target and source are the same, this can be 3276 * done in a single check. 3277 */ 3278 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr, NULL))) 3279 goto out; 3280 3281 if ((*svpp)->v_type == VDIR) { 3282 /* 3283 * Avoid ".", "..", and aliases of "." for obvious reasons. 3284 */ 3285 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') || 3286 sdzp == szp || 3287 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) { 3288 error = EINVAL; 3289 goto out; 3290 } 3291 3292 /* 3293 * Check to make sure rename is valid. 3294 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3295 */ 3296 if ((error = zfs_rename_check(szp, sdzp, tdzp))) 3297 goto out; 3298 } 3299 3300 /* 3301 * Does target exist? 3302 */ 3303 if (tzp) { 3304 /* 3305 * Source and target must be the same type. 3306 */ 3307 if ((*svpp)->v_type == VDIR) { 3308 if ((*tvpp)->v_type != VDIR) { 3309 error = SET_ERROR(ENOTDIR); 3310 goto out; 3311 } else { 3312 cache_purge(tdvp); 3313 if (sdvp != tdvp) 3314 cache_purge(sdvp); 3315 } 3316 } else { 3317 if ((*tvpp)->v_type == VDIR) { 3318 error = SET_ERROR(EISDIR); 3319 goto out; 3320 } 3321 } 3322 } 3323 3324 vn_seqc_write_begin(*svpp); 3325 vn_seqc_write_begin(sdvp); 3326 if (*tvpp != NULL) 3327 vn_seqc_write_begin(*tvpp); 3328 if (tdvp != *tvpp) 3329 vn_seqc_write_begin(tdvp); 3330 3331 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct); 3332 if (tzp) 3333 vnevent_rename_dest(*tvpp, tdvp, tnm, ct); 3334 3335 /* 3336 * notify the target directory if it is not the same 3337 * as source directory. 3338 */ 3339 if (tdvp != sdvp) { 3340 vnevent_rename_dest_dir(tdvp, ct); 3341 } 3342 3343 tx = dmu_tx_create(zfsvfs->z_os); 3344 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 3345 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 3346 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 3347 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 3348 if (sdzp != tdzp) { 3349 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 3350 zfs_sa_upgrade_txholds(tx, tdzp); 3351 } 3352 if (tzp) { 3353 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 3354 zfs_sa_upgrade_txholds(tx, tzp); 3355 } 3356 3357 zfs_sa_upgrade_txholds(tx, szp); 3358 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 3359 error = dmu_tx_assign(tx, TXG_WAIT); 3360 if (error) { 3361 dmu_tx_abort(tx); 3362 goto out_seq; 3363 } 3364 3365 if (tzp) /* Attempt to remove the existing target */ 3366 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL); 3367 3368 if (error == 0) { 3369 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING); 3370 if (error == 0) { 3371 szp->z_pflags |= ZFS_AV_MODIFIED; 3372 3373 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 3374 (void *)&szp->z_pflags, sizeof (uint64_t), tx); 3375 ASSERT0(error); 3376 3377 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING, 3378 NULL); 3379 if (error == 0) { 3380 zfs_log_rename(zilog, tx, TX_RENAME, sdzp, 3381 snm, tdzp, tnm, szp); 3382 } else { 3383 /* 3384 * At this point, we have successfully created 3385 * the target name, but have failed to remove 3386 * the source name. Since the create was done 3387 * with the ZRENAMING flag, there are 3388 * complications; for one, the link count is 3389 * wrong. The easiest way to deal with this 3390 * is to remove the newly created target, and 3391 * return the original error. This must 3392 * succeed; fortunately, it is very unlikely to 3393 * fail, since we just created it. 3394 */ 3395 VERIFY0(zfs_link_destroy(tdzp, tnm, szp, tx, 3396 ZRENAMING, NULL)); 3397 } 3398 } 3399 if (error == 0) { 3400 cache_vop_rename(sdvp, *svpp, tdvp, *tvpp, scnp, tcnp); 3401 } 3402 } 3403 3404 dmu_tx_commit(tx); 3405 3406 out_seq: 3407 vn_seqc_write_end(*svpp); 3408 vn_seqc_write_end(sdvp); 3409 if (*tvpp != NULL) 3410 vn_seqc_write_end(*tvpp); 3411 if (tdvp != *tvpp) 3412 vn_seqc_write_end(tdvp); 3413 3414 out: 3415 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3416 zil_commit(zilog, 0); 3417 zfs_exit(zfsvfs, FTAG); 3418 3419 return (error); 3420 } 3421 3422 int 3423 zfs_rename(znode_t *sdzp, const char *sname, znode_t *tdzp, const char *tname, 3424 cred_t *cr, int flags, uint64_t rflags, vattr_t *wo_vap, zuserns_t *mnt_ns) 3425 { 3426 struct componentname scn, tcn; 3427 vnode_t *sdvp, *tdvp; 3428 vnode_t *svp, *tvp; 3429 int error; 3430 svp = tvp = NULL; 3431 3432 if (rflags != 0 || wo_vap != NULL) 3433 return (SET_ERROR(EINVAL)); 3434 3435 sdvp = ZTOV(sdzp); 3436 tdvp = ZTOV(tdzp); 3437 error = zfs_lookup_internal(sdzp, sname, &svp, &scn, DELETE); 3438 if (sdzp->z_zfsvfs->z_replay == B_FALSE) 3439 VOP_UNLOCK1(sdvp); 3440 if (error != 0) 3441 goto fail; 3442 VOP_UNLOCK1(svp); 3443 3444 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY); 3445 error = zfs_lookup_internal(tdzp, tname, &tvp, &tcn, RENAME); 3446 if (error == EJUSTRETURN) 3447 tvp = NULL; 3448 else if (error != 0) { 3449 VOP_UNLOCK1(tdvp); 3450 goto fail; 3451 } 3452 3453 error = zfs_do_rename(sdvp, &svp, &scn, tdvp, &tvp, &tcn, cr); 3454 fail: 3455 if (svp != NULL) 3456 vrele(svp); 3457 if (tvp != NULL) 3458 vrele(tvp); 3459 3460 return (error); 3461 } 3462 3463 /* 3464 * Insert the indicated symbolic reference entry into the directory. 3465 * 3466 * IN: dvp - Directory to contain new symbolic link. 3467 * link - Name for new symlink entry. 3468 * vap - Attributes of new entry. 3469 * cr - credentials of caller. 3470 * ct - caller context 3471 * flags - case flags 3472 * mnt_ns - Unused on FreeBSD 3473 * 3474 * RETURN: 0 on success, error code on failure. 3475 * 3476 * Timestamps: 3477 * dvp - ctime|mtime updated 3478 */ 3479 int 3480 zfs_symlink(znode_t *dzp, const char *name, vattr_t *vap, 3481 const char *link, znode_t **zpp, cred_t *cr, int flags, zuserns_t *mnt_ns) 3482 { 3483 (void) flags; 3484 znode_t *zp; 3485 dmu_tx_t *tx; 3486 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 3487 zilog_t *zilog; 3488 uint64_t len = strlen(link); 3489 int error; 3490 zfs_acl_ids_t acl_ids; 3491 boolean_t fuid_dirtied; 3492 uint64_t txtype = TX_SYMLINK; 3493 3494 ASSERT3S(vap->va_type, ==, VLNK); 3495 3496 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0) 3497 return (error); 3498 zilog = zfsvfs->z_log; 3499 3500 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 3501 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3502 zfs_exit(zfsvfs, FTAG); 3503 return (SET_ERROR(EILSEQ)); 3504 } 3505 3506 if (len > MAXPATHLEN) { 3507 zfs_exit(zfsvfs, FTAG); 3508 return (SET_ERROR(ENAMETOOLONG)); 3509 } 3510 3511 if ((error = zfs_acl_ids_create(dzp, 0, 3512 vap, cr, NULL, &acl_ids, NULL)) != 0) { 3513 zfs_exit(zfsvfs, FTAG); 3514 return (error); 3515 } 3516 3517 /* 3518 * Attempt to lock directory; fail if entry already exists. 3519 */ 3520 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW); 3521 if (error) { 3522 zfs_acl_ids_free(&acl_ids); 3523 zfs_exit(zfsvfs, FTAG); 3524 return (error); 3525 } 3526 3527 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) { 3528 zfs_acl_ids_free(&acl_ids); 3529 zfs_exit(zfsvfs, FTAG); 3530 return (error); 3531 } 3532 3533 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, 3534 0 /* projid */)) { 3535 zfs_acl_ids_free(&acl_ids); 3536 zfs_exit(zfsvfs, FTAG); 3537 return (SET_ERROR(EDQUOT)); 3538 } 3539 3540 getnewvnode_reserve_(); 3541 tx = dmu_tx_create(zfsvfs->z_os); 3542 fuid_dirtied = zfsvfs->z_fuid_dirty; 3543 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3544 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3545 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 3546 ZFS_SA_BASE_ATTR_SIZE + len); 3547 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 3548 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 3549 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 3550 acl_ids.z_aclp->z_acl_bytes); 3551 } 3552 if (fuid_dirtied) 3553 zfs_fuid_txhold(zfsvfs, tx); 3554 error = dmu_tx_assign(tx, TXG_WAIT); 3555 if (error) { 3556 zfs_acl_ids_free(&acl_ids); 3557 dmu_tx_abort(tx); 3558 getnewvnode_drop_reserve(); 3559 zfs_exit(zfsvfs, FTAG); 3560 return (error); 3561 } 3562 3563 /* 3564 * Create a new object for the symlink. 3565 * for version 4 ZPL datasets the symlink will be an SA attribute 3566 */ 3567 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 3568 3569 if (fuid_dirtied) 3570 zfs_fuid_sync(zfsvfs, tx); 3571 3572 if (zp->z_is_sa) 3573 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 3574 __DECONST(void *, link), len, tx); 3575 else 3576 zfs_sa_symlink(zp, __DECONST(char *, link), len, tx); 3577 3578 zp->z_size = len; 3579 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 3580 &zp->z_size, sizeof (zp->z_size), tx); 3581 /* 3582 * Insert the new object into the directory. 3583 */ 3584 (void) zfs_link_create(dzp, name, zp, tx, ZNEW); 3585 3586 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 3587 *zpp = zp; 3588 3589 zfs_acl_ids_free(&acl_ids); 3590 3591 dmu_tx_commit(tx); 3592 3593 getnewvnode_drop_reserve(); 3594 3595 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3596 zil_commit(zilog, 0); 3597 3598 zfs_exit(zfsvfs, FTAG); 3599 return (error); 3600 } 3601 3602 /* 3603 * Return, in the buffer contained in the provided uio structure, 3604 * the symbolic path referred to by vp. 3605 * 3606 * IN: vp - vnode of symbolic link. 3607 * uio - structure to contain the link path. 3608 * cr - credentials of caller. 3609 * ct - caller context 3610 * 3611 * OUT: uio - structure containing the link path. 3612 * 3613 * RETURN: 0 on success, error code on failure. 3614 * 3615 * Timestamps: 3616 * vp - atime updated 3617 */ 3618 static int 3619 zfs_readlink(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, caller_context_t *ct) 3620 { 3621 (void) cr, (void) ct; 3622 znode_t *zp = VTOZ(vp); 3623 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3624 int error; 3625 3626 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 3627 return (error); 3628 3629 if (zp->z_is_sa) 3630 error = sa_lookup_uio(zp->z_sa_hdl, 3631 SA_ZPL_SYMLINK(zfsvfs), uio); 3632 else 3633 error = zfs_sa_readlink(zp, uio); 3634 3635 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3636 3637 zfs_exit(zfsvfs, FTAG); 3638 return (error); 3639 } 3640 3641 /* 3642 * Insert a new entry into directory tdvp referencing svp. 3643 * 3644 * IN: tdvp - Directory to contain new entry. 3645 * svp - vnode of new entry. 3646 * name - name of new entry. 3647 * cr - credentials of caller. 3648 * 3649 * RETURN: 0 on success, error code on failure. 3650 * 3651 * Timestamps: 3652 * tdvp - ctime|mtime updated 3653 * svp - ctime updated 3654 */ 3655 int 3656 zfs_link(znode_t *tdzp, znode_t *szp, const char *name, cred_t *cr, 3657 int flags) 3658 { 3659 (void) flags; 3660 znode_t *tzp; 3661 zfsvfs_t *zfsvfs = tdzp->z_zfsvfs; 3662 zilog_t *zilog; 3663 dmu_tx_t *tx; 3664 int error; 3665 uint64_t parent; 3666 uid_t owner; 3667 3668 ASSERT3S(ZTOV(tdzp)->v_type, ==, VDIR); 3669 3670 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0) 3671 return (error); 3672 zilog = zfsvfs->z_log; 3673 3674 /* 3675 * POSIX dictates that we return EPERM here. 3676 * Better choices include ENOTSUP or EISDIR. 3677 */ 3678 if (ZTOV(szp)->v_type == VDIR) { 3679 zfs_exit(zfsvfs, FTAG); 3680 return (SET_ERROR(EPERM)); 3681 } 3682 3683 if ((error = zfs_verify_zp(szp)) != 0) { 3684 zfs_exit(zfsvfs, FTAG); 3685 return (error); 3686 } 3687 3688 /* 3689 * If we are using project inheritance, means if the directory has 3690 * ZFS_PROJINHERIT set, then its descendant directories will inherit 3691 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under 3692 * such case, we only allow hard link creation in our tree when the 3693 * project IDs are the same. 3694 */ 3695 if (tdzp->z_pflags & ZFS_PROJINHERIT && 3696 tdzp->z_projid != szp->z_projid) { 3697 zfs_exit(zfsvfs, FTAG); 3698 return (SET_ERROR(EXDEV)); 3699 } 3700 3701 if (szp->z_pflags & (ZFS_APPENDONLY | 3702 ZFS_IMMUTABLE | ZFS_READONLY)) { 3703 zfs_exit(zfsvfs, FTAG); 3704 return (SET_ERROR(EPERM)); 3705 } 3706 3707 /* Prevent links to .zfs/shares files */ 3708 3709 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 3710 &parent, sizeof (uint64_t))) != 0) { 3711 zfs_exit(zfsvfs, FTAG); 3712 return (error); 3713 } 3714 if (parent == zfsvfs->z_shares_dir) { 3715 zfs_exit(zfsvfs, FTAG); 3716 return (SET_ERROR(EPERM)); 3717 } 3718 3719 if (zfsvfs->z_utf8 && u8_validate(name, 3720 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3721 zfs_exit(zfsvfs, FTAG); 3722 return (SET_ERROR(EILSEQ)); 3723 } 3724 3725 /* 3726 * We do not support links between attributes and non-attributes 3727 * because of the potential security risk of creating links 3728 * into "normal" file space in order to circumvent restrictions 3729 * imposed in attribute space. 3730 */ 3731 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) { 3732 zfs_exit(zfsvfs, FTAG); 3733 return (SET_ERROR(EINVAL)); 3734 } 3735 3736 3737 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER); 3738 if (owner != crgetuid(cr) && secpolicy_basic_link(ZTOV(szp), cr) != 0) { 3739 zfs_exit(zfsvfs, FTAG); 3740 return (SET_ERROR(EPERM)); 3741 } 3742 3743 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr, NULL))) { 3744 zfs_exit(zfsvfs, FTAG); 3745 return (error); 3746 } 3747 3748 /* 3749 * Attempt to lock directory; fail if entry already exists. 3750 */ 3751 error = zfs_dirent_lookup(tdzp, name, &tzp, ZNEW); 3752 if (error) { 3753 zfs_exit(zfsvfs, FTAG); 3754 return (error); 3755 } 3756 3757 tx = dmu_tx_create(zfsvfs->z_os); 3758 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 3759 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name); 3760 zfs_sa_upgrade_txholds(tx, szp); 3761 zfs_sa_upgrade_txholds(tx, tdzp); 3762 error = dmu_tx_assign(tx, TXG_WAIT); 3763 if (error) { 3764 dmu_tx_abort(tx); 3765 zfs_exit(zfsvfs, FTAG); 3766 return (error); 3767 } 3768 3769 error = zfs_link_create(tdzp, name, szp, tx, 0); 3770 3771 if (error == 0) { 3772 uint64_t txtype = TX_LINK; 3773 zfs_log_link(zilog, tx, txtype, tdzp, szp, name); 3774 } 3775 3776 dmu_tx_commit(tx); 3777 3778 if (error == 0) { 3779 vnevent_link(ZTOV(szp), ct); 3780 } 3781 3782 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3783 zil_commit(zilog, 0); 3784 3785 zfs_exit(zfsvfs, FTAG); 3786 return (error); 3787 } 3788 3789 /* 3790 * Free or allocate space in a file. Currently, this function only 3791 * supports the `F_FREESP' command. However, this command is somewhat 3792 * misnamed, as its functionality includes the ability to allocate as 3793 * well as free space. 3794 * 3795 * IN: ip - inode of file to free data in. 3796 * cmd - action to take (only F_FREESP supported). 3797 * bfp - section of file to free/alloc. 3798 * flag - current file open mode flags. 3799 * offset - current file offset. 3800 * cr - credentials of caller. 3801 * 3802 * RETURN: 0 on success, error code on failure. 3803 * 3804 * Timestamps: 3805 * ip - ctime|mtime updated 3806 */ 3807 int 3808 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag, 3809 offset_t offset, cred_t *cr) 3810 { 3811 (void) offset; 3812 zfsvfs_t *zfsvfs = ZTOZSB(zp); 3813 uint64_t off, len; 3814 int error; 3815 3816 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 3817 return (error); 3818 3819 if (cmd != F_FREESP) { 3820 zfs_exit(zfsvfs, FTAG); 3821 return (SET_ERROR(EINVAL)); 3822 } 3823 3824 /* 3825 * Callers might not be able to detect properly that we are read-only, 3826 * so check it explicitly here. 3827 */ 3828 if (zfs_is_readonly(zfsvfs)) { 3829 zfs_exit(zfsvfs, FTAG); 3830 return (SET_ERROR(EROFS)); 3831 } 3832 3833 if (bfp->l_len < 0) { 3834 zfs_exit(zfsvfs, FTAG); 3835 return (SET_ERROR(EINVAL)); 3836 } 3837 3838 /* 3839 * Permissions aren't checked on Solaris because on this OS 3840 * zfs_space() can only be called with an opened file handle. 3841 * On Linux we can get here through truncate_range() which 3842 * operates directly on inodes, so we need to check access rights. 3843 */ 3844 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr, NULL))) { 3845 zfs_exit(zfsvfs, FTAG); 3846 return (error); 3847 } 3848 3849 off = bfp->l_start; 3850 len = bfp->l_len; /* 0 means from off to end of file */ 3851 3852 error = zfs_freesp(zp, off, len, flag, TRUE); 3853 3854 zfs_exit(zfsvfs, FTAG); 3855 return (error); 3856 } 3857 3858 static void 3859 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 3860 { 3861 (void) cr, (void) ct; 3862 znode_t *zp = VTOZ(vp); 3863 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3864 int error; 3865 3866 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs); 3867 if (zp->z_sa_hdl == NULL) { 3868 /* 3869 * The fs has been unmounted, or we did a 3870 * suspend/resume and this file no longer exists. 3871 */ 3872 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs); 3873 vrecycle(vp); 3874 return; 3875 } 3876 3877 if (zp->z_unlinked) { 3878 /* 3879 * Fast path to recycle a vnode of a removed file. 3880 */ 3881 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs); 3882 vrecycle(vp); 3883 return; 3884 } 3885 3886 if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3887 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3888 3889 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 3890 zfs_sa_upgrade_txholds(tx, zp); 3891 error = dmu_tx_assign(tx, TXG_WAIT); 3892 if (error) { 3893 dmu_tx_abort(tx); 3894 } else { 3895 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 3896 (void *)&zp->z_atime, sizeof (zp->z_atime), tx); 3897 zp->z_atime_dirty = 0; 3898 dmu_tx_commit(tx); 3899 } 3900 } 3901 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs); 3902 } 3903 3904 3905 _Static_assert(sizeof (struct zfid_short) <= sizeof (struct fid), 3906 "struct zfid_short bigger than struct fid"); 3907 _Static_assert(sizeof (struct zfid_long) <= sizeof (struct fid), 3908 "struct zfid_long bigger than struct fid"); 3909 3910 static int 3911 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 3912 { 3913 (void) ct; 3914 znode_t *zp = VTOZ(vp); 3915 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3916 uint32_t gen; 3917 uint64_t gen64; 3918 uint64_t object = zp->z_id; 3919 zfid_short_t *zfid; 3920 int size, i, error; 3921 3922 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 3923 return (error); 3924 3925 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 3926 &gen64, sizeof (uint64_t))) != 0) { 3927 zfs_exit(zfsvfs, FTAG); 3928 return (error); 3929 } 3930 3931 gen = (uint32_t)gen64; 3932 3933 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 3934 fidp->fid_len = size; 3935 3936 zfid = (zfid_short_t *)fidp; 3937 3938 zfid->zf_len = size; 3939 3940 for (i = 0; i < sizeof (zfid->zf_object); i++) 3941 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3942 3943 /* Must have a non-zero generation number to distinguish from .zfs */ 3944 if (gen == 0) 3945 gen = 1; 3946 for (i = 0; i < sizeof (zfid->zf_gen); i++) 3947 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3948 3949 if (size == LONG_FID_LEN) { 3950 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 3951 zfid_long_t *zlfid; 3952 3953 zlfid = (zfid_long_t *)fidp; 3954 3955 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 3956 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 3957 3958 /* XXX - this should be the generation number for the objset */ 3959 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 3960 zlfid->zf_setgen[i] = 0; 3961 } 3962 3963 zfs_exit(zfsvfs, FTAG); 3964 return (0); 3965 } 3966 3967 static int 3968 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 3969 caller_context_t *ct) 3970 { 3971 znode_t *zp; 3972 zfsvfs_t *zfsvfs; 3973 int error; 3974 3975 switch (cmd) { 3976 case _PC_LINK_MAX: 3977 *valp = MIN(LONG_MAX, ZFS_LINK_MAX); 3978 return (0); 3979 3980 case _PC_FILESIZEBITS: 3981 *valp = 64; 3982 return (0); 3983 case _PC_MIN_HOLE_SIZE: 3984 *valp = (int)SPA_MINBLOCKSIZE; 3985 return (0); 3986 case _PC_ACL_EXTENDED: 3987 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */ 3988 zp = VTOZ(vp); 3989 zfsvfs = zp->z_zfsvfs; 3990 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 3991 return (error); 3992 *valp = zfsvfs->z_acl_type == ZFSACLTYPE_POSIX ? 1 : 0; 3993 zfs_exit(zfsvfs, FTAG); 3994 #else 3995 *valp = 0; 3996 #endif 3997 return (0); 3998 3999 case _PC_ACL_NFS4: 4000 zp = VTOZ(vp); 4001 zfsvfs = zp->z_zfsvfs; 4002 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 4003 return (error); 4004 *valp = zfsvfs->z_acl_type == ZFS_ACLTYPE_NFSV4 ? 1 : 0; 4005 zfs_exit(zfsvfs, FTAG); 4006 return (0); 4007 4008 case _PC_ACL_PATH_MAX: 4009 *valp = ACL_MAX_ENTRIES; 4010 return (0); 4011 4012 default: 4013 return (EOPNOTSUPP); 4014 } 4015 } 4016 4017 static int 4018 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind, 4019 int *rahead) 4020 { 4021 znode_t *zp = VTOZ(vp); 4022 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4023 zfs_locked_range_t *lr; 4024 vm_object_t object; 4025 off_t start, end, obj_size; 4026 uint_t blksz; 4027 int pgsin_b, pgsin_a; 4028 int error; 4029 4030 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0) 4031 return (zfs_vm_pagerret_error); 4032 4033 start = IDX_TO_OFF(ma[0]->pindex); 4034 end = IDX_TO_OFF(ma[count - 1]->pindex + 1); 4035 4036 /* 4037 * Lock a range covering all required and optional pages. 4038 * Note that we need to handle the case of the block size growing. 4039 */ 4040 for (;;) { 4041 blksz = zp->z_blksz; 4042 lr = zfs_rangelock_tryenter(&zp->z_rangelock, 4043 rounddown(start, blksz), 4044 roundup(end, blksz) - rounddown(start, blksz), RL_READER); 4045 if (lr == NULL) { 4046 if (rahead != NULL) { 4047 *rahead = 0; 4048 rahead = NULL; 4049 } 4050 if (rbehind != NULL) { 4051 *rbehind = 0; 4052 rbehind = NULL; 4053 } 4054 break; 4055 } 4056 if (blksz == zp->z_blksz) 4057 break; 4058 zfs_rangelock_exit(lr); 4059 } 4060 4061 object = ma[0]->object; 4062 zfs_vmobject_wlock(object); 4063 obj_size = object->un_pager.vnp.vnp_size; 4064 zfs_vmobject_wunlock(object); 4065 if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) { 4066 if (lr != NULL) 4067 zfs_rangelock_exit(lr); 4068 zfs_exit(zfsvfs, FTAG); 4069 return (zfs_vm_pagerret_bad); 4070 } 4071 4072 pgsin_b = 0; 4073 if (rbehind != NULL) { 4074 pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz)); 4075 pgsin_b = MIN(*rbehind, pgsin_b); 4076 } 4077 4078 pgsin_a = 0; 4079 if (rahead != NULL) { 4080 pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end); 4081 if (end + IDX_TO_OFF(pgsin_a) >= obj_size) 4082 pgsin_a = OFF_TO_IDX(round_page(obj_size) - end); 4083 pgsin_a = MIN(*rahead, pgsin_a); 4084 } 4085 4086 /* 4087 * NB: we need to pass the exact byte size of the data that we expect 4088 * to read after accounting for the file size. This is required because 4089 * ZFS will panic if we request DMU to read beyond the end of the last 4090 * allocated block. 4091 */ 4092 error = dmu_read_pages(zfsvfs->z_os, zp->z_id, ma, count, &pgsin_b, 4093 &pgsin_a, MIN(end, obj_size) - (end - PAGE_SIZE)); 4094 4095 if (lr != NULL) 4096 zfs_rangelock_exit(lr); 4097 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4098 4099 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, count*PAGE_SIZE); 4100 4101 zfs_exit(zfsvfs, FTAG); 4102 4103 if (error != 0) 4104 return (zfs_vm_pagerret_error); 4105 4106 VM_CNT_INC(v_vnodein); 4107 VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a); 4108 if (rbehind != NULL) 4109 *rbehind = pgsin_b; 4110 if (rahead != NULL) 4111 *rahead = pgsin_a; 4112 return (zfs_vm_pagerret_ok); 4113 } 4114 4115 #ifndef _SYS_SYSPROTO_H_ 4116 struct vop_getpages_args { 4117 struct vnode *a_vp; 4118 vm_page_t *a_m; 4119 int a_count; 4120 int *a_rbehind; 4121 int *a_rahead; 4122 }; 4123 #endif 4124 4125 static int 4126 zfs_freebsd_getpages(struct vop_getpages_args *ap) 4127 { 4128 4129 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind, 4130 ap->a_rahead)); 4131 } 4132 4133 static int 4134 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags, 4135 int *rtvals) 4136 { 4137 znode_t *zp = VTOZ(vp); 4138 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4139 zfs_locked_range_t *lr; 4140 dmu_tx_t *tx; 4141 struct sf_buf *sf; 4142 vm_object_t object; 4143 vm_page_t m; 4144 caddr_t va; 4145 size_t tocopy; 4146 size_t lo_len; 4147 vm_ooffset_t lo_off; 4148 vm_ooffset_t off; 4149 uint_t blksz; 4150 int ncount; 4151 int pcount; 4152 int err; 4153 int i; 4154 4155 object = vp->v_object; 4156 KASSERT(ma[0]->object == object, ("mismatching object")); 4157 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length")); 4158 4159 pcount = btoc(len); 4160 ncount = pcount; 4161 for (i = 0; i < pcount; i++) 4162 rtvals[i] = zfs_vm_pagerret_error; 4163 4164 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0) 4165 return (zfs_vm_pagerret_error); 4166 4167 off = IDX_TO_OFF(ma[0]->pindex); 4168 blksz = zp->z_blksz; 4169 lo_off = rounddown(off, blksz); 4170 lo_len = roundup(len + (off - lo_off), blksz); 4171 lr = zfs_rangelock_enter(&zp->z_rangelock, lo_off, lo_len, RL_WRITER); 4172 4173 zfs_vmobject_wlock(object); 4174 if (len + off > object->un_pager.vnp.vnp_size) { 4175 if (object->un_pager.vnp.vnp_size > off) { 4176 int pgoff; 4177 4178 len = object->un_pager.vnp.vnp_size - off; 4179 ncount = btoc(len); 4180 if ((pgoff = (int)len & PAGE_MASK) != 0) { 4181 /* 4182 * If the object is locked and the following 4183 * conditions hold, then the page's dirty 4184 * field cannot be concurrently changed by a 4185 * pmap operation. 4186 */ 4187 m = ma[ncount - 1]; 4188 vm_page_assert_sbusied(m); 4189 KASSERT(!pmap_page_is_write_mapped(m), 4190 ("zfs_putpages: page %p is not read-only", 4191 m)); 4192 vm_page_clear_dirty(m, pgoff, PAGE_SIZE - 4193 pgoff); 4194 } 4195 } else { 4196 len = 0; 4197 ncount = 0; 4198 } 4199 if (ncount < pcount) { 4200 for (i = ncount; i < pcount; i++) { 4201 rtvals[i] = zfs_vm_pagerret_bad; 4202 } 4203 } 4204 } 4205 zfs_vmobject_wunlock(object); 4206 4207 if (ncount == 0) 4208 goto out; 4209 4210 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, zp->z_uid) || 4211 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, zp->z_gid) || 4212 (zp->z_projid != ZFS_DEFAULT_PROJID && 4213 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT, 4214 zp->z_projid))) { 4215 goto out; 4216 } 4217 4218 tx = dmu_tx_create(zfsvfs->z_os); 4219 dmu_tx_hold_write(tx, zp->z_id, off, len); 4220 4221 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 4222 zfs_sa_upgrade_txholds(tx, zp); 4223 err = dmu_tx_assign(tx, TXG_WAIT); 4224 if (err != 0) { 4225 dmu_tx_abort(tx); 4226 goto out; 4227 } 4228 4229 if (zp->z_blksz < PAGE_SIZE) { 4230 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) { 4231 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len; 4232 va = zfs_map_page(ma[i], &sf); 4233 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx); 4234 zfs_unmap_page(sf); 4235 } 4236 } else { 4237 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx); 4238 } 4239 4240 if (err == 0) { 4241 uint64_t mtime[2], ctime[2]; 4242 sa_bulk_attr_t bulk[3]; 4243 int count = 0; 4244 4245 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 4246 &mtime, 16); 4247 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 4248 &ctime, 16); 4249 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 4250 &zp->z_pflags, 8); 4251 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime); 4252 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 4253 ASSERT0(err); 4254 /* 4255 * XXX we should be passing a callback to undirty 4256 * but that would make the locking messier 4257 */ 4258 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, 4259 len, 0, NULL, NULL); 4260 4261 zfs_vmobject_wlock(object); 4262 for (i = 0; i < ncount; i++) { 4263 rtvals[i] = zfs_vm_pagerret_ok; 4264 vm_page_undirty(ma[i]); 4265 } 4266 zfs_vmobject_wunlock(object); 4267 VM_CNT_INC(v_vnodeout); 4268 VM_CNT_ADD(v_vnodepgsout, ncount); 4269 } 4270 dmu_tx_commit(tx); 4271 4272 out: 4273 zfs_rangelock_exit(lr); 4274 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 || 4275 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4276 zil_commit(zfsvfs->z_log, zp->z_id); 4277 4278 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, len); 4279 4280 zfs_exit(zfsvfs, FTAG); 4281 return (rtvals[0]); 4282 } 4283 4284 #ifndef _SYS_SYSPROTO_H_ 4285 struct vop_putpages_args { 4286 struct vnode *a_vp; 4287 vm_page_t *a_m; 4288 int a_count; 4289 int a_sync; 4290 int *a_rtvals; 4291 }; 4292 #endif 4293 4294 static int 4295 zfs_freebsd_putpages(struct vop_putpages_args *ap) 4296 { 4297 4298 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync, 4299 ap->a_rtvals)); 4300 } 4301 4302 #ifndef _SYS_SYSPROTO_H_ 4303 struct vop_bmap_args { 4304 struct vnode *a_vp; 4305 daddr_t a_bn; 4306 struct bufobj **a_bop; 4307 daddr_t *a_bnp; 4308 int *a_runp; 4309 int *a_runb; 4310 }; 4311 #endif 4312 4313 static int 4314 zfs_freebsd_bmap(struct vop_bmap_args *ap) 4315 { 4316 4317 if (ap->a_bop != NULL) 4318 *ap->a_bop = &ap->a_vp->v_bufobj; 4319 if (ap->a_bnp != NULL) 4320 *ap->a_bnp = ap->a_bn; 4321 if (ap->a_runp != NULL) 4322 *ap->a_runp = 0; 4323 if (ap->a_runb != NULL) 4324 *ap->a_runb = 0; 4325 4326 return (0); 4327 } 4328 4329 #ifndef _SYS_SYSPROTO_H_ 4330 struct vop_open_args { 4331 struct vnode *a_vp; 4332 int a_mode; 4333 struct ucred *a_cred; 4334 struct thread *a_td; 4335 }; 4336 #endif 4337 4338 static int 4339 zfs_freebsd_open(struct vop_open_args *ap) 4340 { 4341 vnode_t *vp = ap->a_vp; 4342 znode_t *zp = VTOZ(vp); 4343 int error; 4344 4345 error = zfs_open(&vp, ap->a_mode, ap->a_cred); 4346 if (error == 0) 4347 vnode_create_vobject(vp, zp->z_size, ap->a_td); 4348 return (error); 4349 } 4350 4351 #ifndef _SYS_SYSPROTO_H_ 4352 struct vop_close_args { 4353 struct vnode *a_vp; 4354 int a_fflag; 4355 struct ucred *a_cred; 4356 struct thread *a_td; 4357 }; 4358 #endif 4359 4360 static int 4361 zfs_freebsd_close(struct vop_close_args *ap) 4362 { 4363 4364 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred)); 4365 } 4366 4367 #ifndef _SYS_SYSPROTO_H_ 4368 struct vop_ioctl_args { 4369 struct vnode *a_vp; 4370 ulong_t a_command; 4371 caddr_t a_data; 4372 int a_fflag; 4373 struct ucred *cred; 4374 struct thread *td; 4375 }; 4376 #endif 4377 4378 static int 4379 zfs_freebsd_ioctl(struct vop_ioctl_args *ap) 4380 { 4381 4382 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data, 4383 ap->a_fflag, ap->a_cred, NULL)); 4384 } 4385 4386 static int 4387 ioflags(int ioflags) 4388 { 4389 int flags = 0; 4390 4391 if (ioflags & IO_APPEND) 4392 flags |= O_APPEND; 4393 if (ioflags & IO_NDELAY) 4394 flags |= O_NONBLOCK; 4395 if (ioflags & IO_SYNC) 4396 flags |= O_SYNC; 4397 4398 return (flags); 4399 } 4400 4401 #ifndef _SYS_SYSPROTO_H_ 4402 struct vop_read_args { 4403 struct vnode *a_vp; 4404 struct uio *a_uio; 4405 int a_ioflag; 4406 struct ucred *a_cred; 4407 }; 4408 #endif 4409 4410 static int 4411 zfs_freebsd_read(struct vop_read_args *ap) 4412 { 4413 zfs_uio_t uio; 4414 zfs_uio_init(&uio, ap->a_uio); 4415 return (zfs_read(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag), 4416 ap->a_cred)); 4417 } 4418 4419 #ifndef _SYS_SYSPROTO_H_ 4420 struct vop_write_args { 4421 struct vnode *a_vp; 4422 struct uio *a_uio; 4423 int a_ioflag; 4424 struct ucred *a_cred; 4425 }; 4426 #endif 4427 4428 static int 4429 zfs_freebsd_write(struct vop_write_args *ap) 4430 { 4431 zfs_uio_t uio; 4432 zfs_uio_init(&uio, ap->a_uio); 4433 return (zfs_write(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag), 4434 ap->a_cred)); 4435 } 4436 4437 #if __FreeBSD_version >= 1300102 4438 /* 4439 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see 4440 * the comment above cache_fplookup for details. 4441 */ 4442 static int 4443 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args *v) 4444 { 4445 vnode_t *vp; 4446 znode_t *zp; 4447 uint64_t pflags; 4448 4449 vp = v->a_vp; 4450 zp = VTOZ_SMR(vp); 4451 if (__predict_false(zp == NULL)) 4452 return (EAGAIN); 4453 pflags = atomic_load_64(&zp->z_pflags); 4454 if (pflags & ZFS_AV_QUARANTINED) 4455 return (EAGAIN); 4456 if (pflags & ZFS_XATTR) 4457 return (EAGAIN); 4458 if ((pflags & ZFS_NO_EXECS_DENIED) == 0) 4459 return (EAGAIN); 4460 return (0); 4461 } 4462 #endif 4463 4464 #if __FreeBSD_version >= 1300139 4465 static int 4466 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args *v) 4467 { 4468 vnode_t *vp; 4469 znode_t *zp; 4470 char *target; 4471 4472 vp = v->a_vp; 4473 zp = VTOZ_SMR(vp); 4474 if (__predict_false(zp == NULL)) { 4475 return (EAGAIN); 4476 } 4477 4478 target = atomic_load_consume_ptr(&zp->z_cached_symlink); 4479 if (target == NULL) { 4480 return (EAGAIN); 4481 } 4482 return (cache_symlink_resolve(v->a_fpl, target, strlen(target))); 4483 } 4484 #endif 4485 4486 #ifndef _SYS_SYSPROTO_H_ 4487 struct vop_access_args { 4488 struct vnode *a_vp; 4489 accmode_t a_accmode; 4490 struct ucred *a_cred; 4491 struct thread *a_td; 4492 }; 4493 #endif 4494 4495 static int 4496 zfs_freebsd_access(struct vop_access_args *ap) 4497 { 4498 vnode_t *vp = ap->a_vp; 4499 znode_t *zp = VTOZ(vp); 4500 accmode_t accmode; 4501 int error = 0; 4502 4503 4504 if (ap->a_accmode == VEXEC) { 4505 if (zfs_fastaccesschk_execute(zp, ap->a_cred) == 0) 4506 return (0); 4507 } 4508 4509 /* 4510 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND, 4511 */ 4512 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND); 4513 if (accmode != 0) 4514 error = zfs_access(zp, accmode, 0, ap->a_cred); 4515 4516 /* 4517 * VADMIN has to be handled by vaccess(). 4518 */ 4519 if (error == 0) { 4520 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND); 4521 if (accmode != 0) { 4522 #if __FreeBSD_version >= 1300105 4523 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid, 4524 zp->z_gid, accmode, ap->a_cred); 4525 #else 4526 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid, 4527 zp->z_gid, accmode, ap->a_cred, NULL); 4528 #endif 4529 } 4530 } 4531 4532 /* 4533 * For VEXEC, ensure that at least one execute bit is set for 4534 * non-directories. 4535 */ 4536 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR && 4537 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) { 4538 error = EACCES; 4539 } 4540 4541 return (error); 4542 } 4543 4544 #ifndef _SYS_SYSPROTO_H_ 4545 struct vop_lookup_args { 4546 struct vnode *a_dvp; 4547 struct vnode **a_vpp; 4548 struct componentname *a_cnp; 4549 }; 4550 #endif 4551 4552 static int 4553 zfs_freebsd_lookup(struct vop_lookup_args *ap, boolean_t cached) 4554 { 4555 struct componentname *cnp = ap->a_cnp; 4556 char nm[NAME_MAX + 1]; 4557 4558 ASSERT3U(cnp->cn_namelen, <, sizeof (nm)); 4559 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof (nm))); 4560 4561 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop, 4562 cnp->cn_cred, 0, cached)); 4563 } 4564 4565 static int 4566 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args *ap) 4567 { 4568 4569 return (zfs_freebsd_lookup((struct vop_lookup_args *)ap, B_TRUE)); 4570 } 4571 4572 #ifndef _SYS_SYSPROTO_H_ 4573 struct vop_lookup_args { 4574 struct vnode *a_dvp; 4575 struct vnode **a_vpp; 4576 struct componentname *a_cnp; 4577 }; 4578 #endif 4579 4580 static int 4581 zfs_cache_lookup(struct vop_lookup_args *ap) 4582 { 4583 zfsvfs_t *zfsvfs; 4584 4585 zfsvfs = ap->a_dvp->v_mount->mnt_data; 4586 if (zfsvfs->z_use_namecache) 4587 return (vfs_cache_lookup(ap)); 4588 else 4589 return (zfs_freebsd_lookup(ap, B_FALSE)); 4590 } 4591 4592 #ifndef _SYS_SYSPROTO_H_ 4593 struct vop_create_args { 4594 struct vnode *a_dvp; 4595 struct vnode **a_vpp; 4596 struct componentname *a_cnp; 4597 struct vattr *a_vap; 4598 }; 4599 #endif 4600 4601 static int 4602 zfs_freebsd_create(struct vop_create_args *ap) 4603 { 4604 zfsvfs_t *zfsvfs; 4605 struct componentname *cnp = ap->a_cnp; 4606 vattr_t *vap = ap->a_vap; 4607 znode_t *zp = NULL; 4608 int rc, mode; 4609 4610 #if __FreeBSD_version < 1400068 4611 ASSERT(cnp->cn_flags & SAVENAME); 4612 #endif 4613 4614 vattr_init_mask(vap); 4615 mode = vap->va_mode & ALLPERMS; 4616 zfsvfs = ap->a_dvp->v_mount->mnt_data; 4617 *ap->a_vpp = NULL; 4618 4619 rc = zfs_create(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, 0, mode, 4620 &zp, cnp->cn_cred, 0 /* flag */, NULL /* vsecattr */, NULL); 4621 if (rc == 0) 4622 *ap->a_vpp = ZTOV(zp); 4623 if (zfsvfs->z_use_namecache && 4624 rc == 0 && (cnp->cn_flags & MAKEENTRY) != 0) 4625 cache_enter(ap->a_dvp, *ap->a_vpp, cnp); 4626 4627 return (rc); 4628 } 4629 4630 #ifndef _SYS_SYSPROTO_H_ 4631 struct vop_remove_args { 4632 struct vnode *a_dvp; 4633 struct vnode *a_vp; 4634 struct componentname *a_cnp; 4635 }; 4636 #endif 4637 4638 static int 4639 zfs_freebsd_remove(struct vop_remove_args *ap) 4640 { 4641 4642 #if __FreeBSD_version < 1400068 4643 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 4644 #endif 4645 4646 return (zfs_remove_(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr, 4647 ap->a_cnp->cn_cred)); 4648 } 4649 4650 #ifndef _SYS_SYSPROTO_H_ 4651 struct vop_mkdir_args { 4652 struct vnode *a_dvp; 4653 struct vnode **a_vpp; 4654 struct componentname *a_cnp; 4655 struct vattr *a_vap; 4656 }; 4657 #endif 4658 4659 static int 4660 zfs_freebsd_mkdir(struct vop_mkdir_args *ap) 4661 { 4662 vattr_t *vap = ap->a_vap; 4663 znode_t *zp = NULL; 4664 int rc; 4665 4666 #if __FreeBSD_version < 1400068 4667 ASSERT(ap->a_cnp->cn_flags & SAVENAME); 4668 #endif 4669 4670 vattr_init_mask(vap); 4671 *ap->a_vpp = NULL; 4672 4673 rc = zfs_mkdir(VTOZ(ap->a_dvp), ap->a_cnp->cn_nameptr, vap, &zp, 4674 ap->a_cnp->cn_cred, 0, NULL, NULL); 4675 4676 if (rc == 0) 4677 *ap->a_vpp = ZTOV(zp); 4678 return (rc); 4679 } 4680 4681 #ifndef _SYS_SYSPROTO_H_ 4682 struct vop_rmdir_args { 4683 struct vnode *a_dvp; 4684 struct vnode *a_vp; 4685 struct componentname *a_cnp; 4686 }; 4687 #endif 4688 4689 static int 4690 zfs_freebsd_rmdir(struct vop_rmdir_args *ap) 4691 { 4692 struct componentname *cnp = ap->a_cnp; 4693 4694 #if __FreeBSD_version < 1400068 4695 ASSERT(cnp->cn_flags & SAVENAME); 4696 #endif 4697 4698 return (zfs_rmdir_(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred)); 4699 } 4700 4701 #ifndef _SYS_SYSPROTO_H_ 4702 struct vop_readdir_args { 4703 struct vnode *a_vp; 4704 struct uio *a_uio; 4705 struct ucred *a_cred; 4706 int *a_eofflag; 4707 int *a_ncookies; 4708 cookie_t **a_cookies; 4709 }; 4710 #endif 4711 4712 static int 4713 zfs_freebsd_readdir(struct vop_readdir_args *ap) 4714 { 4715 zfs_uio_t uio; 4716 zfs_uio_init(&uio, ap->a_uio); 4717 return (zfs_readdir(ap->a_vp, &uio, ap->a_cred, ap->a_eofflag, 4718 ap->a_ncookies, ap->a_cookies)); 4719 } 4720 4721 #ifndef _SYS_SYSPROTO_H_ 4722 struct vop_fsync_args { 4723 struct vnode *a_vp; 4724 int a_waitfor; 4725 struct thread *a_td; 4726 }; 4727 #endif 4728 4729 static int 4730 zfs_freebsd_fsync(struct vop_fsync_args *ap) 4731 { 4732 4733 return (zfs_fsync(VTOZ(ap->a_vp), 0, ap->a_td->td_ucred)); 4734 } 4735 4736 #ifndef _SYS_SYSPROTO_H_ 4737 struct vop_getattr_args { 4738 struct vnode *a_vp; 4739 struct vattr *a_vap; 4740 struct ucred *a_cred; 4741 }; 4742 #endif 4743 4744 static int 4745 zfs_freebsd_getattr(struct vop_getattr_args *ap) 4746 { 4747 vattr_t *vap = ap->a_vap; 4748 xvattr_t xvap; 4749 ulong_t fflags = 0; 4750 int error; 4751 4752 xva_init(&xvap); 4753 xvap.xva_vattr = *vap; 4754 xvap.xva_vattr.va_mask |= AT_XVATTR; 4755 4756 /* Convert chflags into ZFS-type flags. */ 4757 /* XXX: what about SF_SETTABLE?. */ 4758 XVA_SET_REQ(&xvap, XAT_IMMUTABLE); 4759 XVA_SET_REQ(&xvap, XAT_APPENDONLY); 4760 XVA_SET_REQ(&xvap, XAT_NOUNLINK); 4761 XVA_SET_REQ(&xvap, XAT_NODUMP); 4762 XVA_SET_REQ(&xvap, XAT_READONLY); 4763 XVA_SET_REQ(&xvap, XAT_ARCHIVE); 4764 XVA_SET_REQ(&xvap, XAT_SYSTEM); 4765 XVA_SET_REQ(&xvap, XAT_HIDDEN); 4766 XVA_SET_REQ(&xvap, XAT_REPARSE); 4767 XVA_SET_REQ(&xvap, XAT_OFFLINE); 4768 XVA_SET_REQ(&xvap, XAT_SPARSE); 4769 4770 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred); 4771 if (error != 0) 4772 return (error); 4773 4774 /* Convert ZFS xattr into chflags. */ 4775 #define FLAG_CHECK(fflag, xflag, xfield) do { \ 4776 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \ 4777 fflags |= (fflag); \ 4778 } while (0) 4779 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE, 4780 xvap.xva_xoptattrs.xoa_immutable); 4781 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY, 4782 xvap.xva_xoptattrs.xoa_appendonly); 4783 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK, 4784 xvap.xva_xoptattrs.xoa_nounlink); 4785 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE, 4786 xvap.xva_xoptattrs.xoa_archive); 4787 FLAG_CHECK(UF_NODUMP, XAT_NODUMP, 4788 xvap.xva_xoptattrs.xoa_nodump); 4789 FLAG_CHECK(UF_READONLY, XAT_READONLY, 4790 xvap.xva_xoptattrs.xoa_readonly); 4791 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM, 4792 xvap.xva_xoptattrs.xoa_system); 4793 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN, 4794 xvap.xva_xoptattrs.xoa_hidden); 4795 FLAG_CHECK(UF_REPARSE, XAT_REPARSE, 4796 xvap.xva_xoptattrs.xoa_reparse); 4797 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE, 4798 xvap.xva_xoptattrs.xoa_offline); 4799 FLAG_CHECK(UF_SPARSE, XAT_SPARSE, 4800 xvap.xva_xoptattrs.xoa_sparse); 4801 4802 #undef FLAG_CHECK 4803 *vap = xvap.xva_vattr; 4804 vap->va_flags = fflags; 4805 return (0); 4806 } 4807 4808 #ifndef _SYS_SYSPROTO_H_ 4809 struct vop_setattr_args { 4810 struct vnode *a_vp; 4811 struct vattr *a_vap; 4812 struct ucred *a_cred; 4813 }; 4814 #endif 4815 4816 static int 4817 zfs_freebsd_setattr(struct vop_setattr_args *ap) 4818 { 4819 vnode_t *vp = ap->a_vp; 4820 vattr_t *vap = ap->a_vap; 4821 cred_t *cred = ap->a_cred; 4822 xvattr_t xvap; 4823 ulong_t fflags; 4824 uint64_t zflags; 4825 4826 vattr_init_mask(vap); 4827 vap->va_mask &= ~AT_NOSET; 4828 4829 xva_init(&xvap); 4830 xvap.xva_vattr = *vap; 4831 4832 zflags = VTOZ(vp)->z_pflags; 4833 4834 if (vap->va_flags != VNOVAL) { 4835 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs; 4836 int error; 4837 4838 if (zfsvfs->z_use_fuids == B_FALSE) 4839 return (EOPNOTSUPP); 4840 4841 fflags = vap->va_flags; 4842 /* 4843 * XXX KDM 4844 * We need to figure out whether it makes sense to allow 4845 * UF_REPARSE through, since we don't really have other 4846 * facilities to handle reparse points and zfs_setattr() 4847 * doesn't currently allow setting that attribute anyway. 4848 */ 4849 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE| 4850 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE| 4851 UF_OFFLINE|UF_SPARSE)) != 0) 4852 return (EOPNOTSUPP); 4853 /* 4854 * Unprivileged processes are not permitted to unset system 4855 * flags, or modify flags if any system flags are set. 4856 * Privileged non-jail processes may not modify system flags 4857 * if securelevel > 0 and any existing system flags are set. 4858 * Privileged jail processes behave like privileged non-jail 4859 * processes if the PR_ALLOW_CHFLAGS permission bit is set; 4860 * otherwise, they behave like unprivileged processes. 4861 */ 4862 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 || 4863 spl_priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) { 4864 if (zflags & 4865 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) { 4866 error = securelevel_gt(cred, 0); 4867 if (error != 0) 4868 return (error); 4869 } 4870 } else { 4871 /* 4872 * Callers may only modify the file flags on 4873 * objects they have VADMIN rights for. 4874 */ 4875 if ((error = VOP_ACCESS(vp, VADMIN, cred, 4876 curthread)) != 0) 4877 return (error); 4878 if (zflags & 4879 (ZFS_IMMUTABLE | ZFS_APPENDONLY | 4880 ZFS_NOUNLINK)) { 4881 return (EPERM); 4882 } 4883 if (fflags & 4884 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) { 4885 return (EPERM); 4886 } 4887 } 4888 4889 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \ 4890 if (((fflags & (fflag)) && !(zflags & (zflag))) || \ 4891 ((zflags & (zflag)) && !(fflags & (fflag)))) { \ 4892 XVA_SET_REQ(&xvap, (xflag)); \ 4893 (xfield) = ((fflags & (fflag)) != 0); \ 4894 } \ 4895 } while (0) 4896 /* Convert chflags into ZFS-type flags. */ 4897 /* XXX: what about SF_SETTABLE?. */ 4898 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE, 4899 xvap.xva_xoptattrs.xoa_immutable); 4900 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY, 4901 xvap.xva_xoptattrs.xoa_appendonly); 4902 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK, 4903 xvap.xva_xoptattrs.xoa_nounlink); 4904 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE, 4905 xvap.xva_xoptattrs.xoa_archive); 4906 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP, 4907 xvap.xva_xoptattrs.xoa_nodump); 4908 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY, 4909 xvap.xva_xoptattrs.xoa_readonly); 4910 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM, 4911 xvap.xva_xoptattrs.xoa_system); 4912 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN, 4913 xvap.xva_xoptattrs.xoa_hidden); 4914 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE, 4915 xvap.xva_xoptattrs.xoa_reparse); 4916 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE, 4917 xvap.xva_xoptattrs.xoa_offline); 4918 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE, 4919 xvap.xva_xoptattrs.xoa_sparse); 4920 #undef FLAG_CHANGE 4921 } 4922 if (vap->va_birthtime.tv_sec != VNOVAL) { 4923 xvap.xva_vattr.va_mask |= AT_XVATTR; 4924 XVA_SET_REQ(&xvap, XAT_CREATETIME); 4925 } 4926 return (zfs_setattr(VTOZ(vp), (vattr_t *)&xvap, 0, cred, NULL)); 4927 } 4928 4929 #ifndef _SYS_SYSPROTO_H_ 4930 struct vop_rename_args { 4931 struct vnode *a_fdvp; 4932 struct vnode *a_fvp; 4933 struct componentname *a_fcnp; 4934 struct vnode *a_tdvp; 4935 struct vnode *a_tvp; 4936 struct componentname *a_tcnp; 4937 }; 4938 #endif 4939 4940 static int 4941 zfs_freebsd_rename(struct vop_rename_args *ap) 4942 { 4943 vnode_t *fdvp = ap->a_fdvp; 4944 vnode_t *fvp = ap->a_fvp; 4945 vnode_t *tdvp = ap->a_tdvp; 4946 vnode_t *tvp = ap->a_tvp; 4947 int error; 4948 4949 #if __FreeBSD_version < 1400068 4950 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART)); 4951 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART)); 4952 #endif 4953 4954 error = zfs_do_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp, 4955 ap->a_tcnp, ap->a_fcnp->cn_cred); 4956 4957 vrele(fdvp); 4958 vrele(fvp); 4959 vrele(tdvp); 4960 if (tvp != NULL) 4961 vrele(tvp); 4962 4963 return (error); 4964 } 4965 4966 #ifndef _SYS_SYSPROTO_H_ 4967 struct vop_symlink_args { 4968 struct vnode *a_dvp; 4969 struct vnode **a_vpp; 4970 struct componentname *a_cnp; 4971 struct vattr *a_vap; 4972 char *a_target; 4973 }; 4974 #endif 4975 4976 static int 4977 zfs_freebsd_symlink(struct vop_symlink_args *ap) 4978 { 4979 struct componentname *cnp = ap->a_cnp; 4980 vattr_t *vap = ap->a_vap; 4981 znode_t *zp = NULL; 4982 #if __FreeBSD_version >= 1300139 4983 char *symlink; 4984 size_t symlink_len; 4985 #endif 4986 int rc; 4987 4988 #if __FreeBSD_version < 1400068 4989 ASSERT(cnp->cn_flags & SAVENAME); 4990 #endif 4991 4992 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */ 4993 vattr_init_mask(vap); 4994 *ap->a_vpp = NULL; 4995 4996 rc = zfs_symlink(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, 4997 ap->a_target, &zp, cnp->cn_cred, 0 /* flags */, NULL); 4998 if (rc == 0) { 4999 *ap->a_vpp = ZTOV(zp); 5000 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__); 5001 #if __FreeBSD_version >= 1300139 5002 MPASS(zp->z_cached_symlink == NULL); 5003 symlink_len = strlen(ap->a_target); 5004 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK); 5005 if (symlink != NULL) { 5006 memcpy(symlink, ap->a_target, symlink_len); 5007 symlink[symlink_len] = '\0'; 5008 atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink, 5009 (uintptr_t)symlink); 5010 } 5011 #endif 5012 } 5013 return (rc); 5014 } 5015 5016 #ifndef _SYS_SYSPROTO_H_ 5017 struct vop_readlink_args { 5018 struct vnode *a_vp; 5019 struct uio *a_uio; 5020 struct ucred *a_cred; 5021 }; 5022 #endif 5023 5024 static int 5025 zfs_freebsd_readlink(struct vop_readlink_args *ap) 5026 { 5027 zfs_uio_t uio; 5028 int error; 5029 #if __FreeBSD_version >= 1300139 5030 znode_t *zp = VTOZ(ap->a_vp); 5031 char *symlink, *base; 5032 size_t symlink_len; 5033 bool trycache; 5034 #endif 5035 5036 zfs_uio_init(&uio, ap->a_uio); 5037 #if __FreeBSD_version >= 1300139 5038 trycache = false; 5039 if (zfs_uio_segflg(&uio) == UIO_SYSSPACE && 5040 zfs_uio_iovcnt(&uio) == 1) { 5041 base = zfs_uio_iovbase(&uio, 0); 5042 symlink_len = zfs_uio_iovlen(&uio, 0); 5043 trycache = true; 5044 } 5045 #endif 5046 error = zfs_readlink(ap->a_vp, &uio, ap->a_cred, NULL); 5047 #if __FreeBSD_version >= 1300139 5048 if (atomic_load_ptr(&zp->z_cached_symlink) != NULL || 5049 error != 0 || !trycache) { 5050 return (error); 5051 } 5052 symlink_len -= zfs_uio_resid(&uio); 5053 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK); 5054 if (symlink != NULL) { 5055 memcpy(symlink, base, symlink_len); 5056 symlink[symlink_len] = '\0'; 5057 if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp->z_cached_symlink, 5058 (uintptr_t)NULL, (uintptr_t)symlink)) { 5059 cache_symlink_free(symlink, symlink_len + 1); 5060 } 5061 } 5062 #endif 5063 return (error); 5064 } 5065 5066 #ifndef _SYS_SYSPROTO_H_ 5067 struct vop_link_args { 5068 struct vnode *a_tdvp; 5069 struct vnode *a_vp; 5070 struct componentname *a_cnp; 5071 }; 5072 #endif 5073 5074 static int 5075 zfs_freebsd_link(struct vop_link_args *ap) 5076 { 5077 struct componentname *cnp = ap->a_cnp; 5078 vnode_t *vp = ap->a_vp; 5079 vnode_t *tdvp = ap->a_tdvp; 5080 5081 if (tdvp->v_mount != vp->v_mount) 5082 return (EXDEV); 5083 5084 #if __FreeBSD_version < 1400068 5085 ASSERT(cnp->cn_flags & SAVENAME); 5086 #endif 5087 5088 return (zfs_link(VTOZ(tdvp), VTOZ(vp), 5089 cnp->cn_nameptr, cnp->cn_cred, 0)); 5090 } 5091 5092 #ifndef _SYS_SYSPROTO_H_ 5093 struct vop_inactive_args { 5094 struct vnode *a_vp; 5095 struct thread *a_td; 5096 }; 5097 #endif 5098 5099 static int 5100 zfs_freebsd_inactive(struct vop_inactive_args *ap) 5101 { 5102 vnode_t *vp = ap->a_vp; 5103 5104 #if __FreeBSD_version >= 1300123 5105 zfs_inactive(vp, curthread->td_ucred, NULL); 5106 #else 5107 zfs_inactive(vp, ap->a_td->td_ucred, NULL); 5108 #endif 5109 return (0); 5110 } 5111 5112 #if __FreeBSD_version >= 1300042 5113 #ifndef _SYS_SYSPROTO_H_ 5114 struct vop_need_inactive_args { 5115 struct vnode *a_vp; 5116 struct thread *a_td; 5117 }; 5118 #endif 5119 5120 static int 5121 zfs_freebsd_need_inactive(struct vop_need_inactive_args *ap) 5122 { 5123 vnode_t *vp = ap->a_vp; 5124 znode_t *zp = VTOZ(vp); 5125 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5126 int need; 5127 5128 if (vn_need_pageq_flush(vp)) 5129 return (1); 5130 5131 if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs)) 5132 return (1); 5133 need = (zp->z_sa_hdl == NULL || zp->z_unlinked || zp->z_atime_dirty); 5134 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs); 5135 5136 return (need); 5137 } 5138 #endif 5139 5140 #ifndef _SYS_SYSPROTO_H_ 5141 struct vop_reclaim_args { 5142 struct vnode *a_vp; 5143 struct thread *a_td; 5144 }; 5145 #endif 5146 5147 static int 5148 zfs_freebsd_reclaim(struct vop_reclaim_args *ap) 5149 { 5150 vnode_t *vp = ap->a_vp; 5151 znode_t *zp = VTOZ(vp); 5152 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5153 5154 ASSERT3P(zp, !=, NULL); 5155 5156 #if __FreeBSD_version < 1300042 5157 /* Destroy the vm object and flush associated pages. */ 5158 vnode_destroy_vobject(vp); 5159 #endif 5160 /* 5161 * z_teardown_inactive_lock protects from a race with 5162 * zfs_znode_dmu_fini in zfsvfs_teardown during 5163 * force unmount. 5164 */ 5165 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs); 5166 if (zp->z_sa_hdl == NULL) 5167 zfs_znode_free(zp); 5168 else 5169 zfs_zinactive(zp); 5170 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs); 5171 5172 vp->v_data = NULL; 5173 return (0); 5174 } 5175 5176 #ifndef _SYS_SYSPROTO_H_ 5177 struct vop_fid_args { 5178 struct vnode *a_vp; 5179 struct fid *a_fid; 5180 }; 5181 #endif 5182 5183 static int 5184 zfs_freebsd_fid(struct vop_fid_args *ap) 5185 { 5186 5187 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL)); 5188 } 5189 5190 5191 #ifndef _SYS_SYSPROTO_H_ 5192 struct vop_pathconf_args { 5193 struct vnode *a_vp; 5194 int a_name; 5195 register_t *a_retval; 5196 } *ap; 5197 #endif 5198 5199 static int 5200 zfs_freebsd_pathconf(struct vop_pathconf_args *ap) 5201 { 5202 ulong_t val; 5203 int error; 5204 5205 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, 5206 curthread->td_ucred, NULL); 5207 if (error == 0) { 5208 *ap->a_retval = val; 5209 return (error); 5210 } 5211 if (error != EOPNOTSUPP) 5212 return (error); 5213 5214 switch (ap->a_name) { 5215 case _PC_NAME_MAX: 5216 *ap->a_retval = NAME_MAX; 5217 return (0); 5218 #if __FreeBSD_version >= 1400032 5219 case _PC_DEALLOC_PRESENT: 5220 *ap->a_retval = 1; 5221 return (0); 5222 #endif 5223 case _PC_PIPE_BUF: 5224 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) { 5225 *ap->a_retval = PIPE_BUF; 5226 return (0); 5227 } 5228 return (EINVAL); 5229 default: 5230 return (vop_stdpathconf(ap)); 5231 } 5232 } 5233 5234 static int zfs_xattr_compat = 1; 5235 5236 static int 5237 zfs_check_attrname(const char *name) 5238 { 5239 /* We don't allow '/' character in attribute name. */ 5240 if (strchr(name, '/') != NULL) 5241 return (SET_ERROR(EINVAL)); 5242 /* We don't allow attribute names that start with a namespace prefix. */ 5243 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name)) 5244 return (SET_ERROR(EINVAL)); 5245 return (0); 5246 } 5247 5248 /* 5249 * FreeBSD's extended attributes namespace defines file name prefix for ZFS' 5250 * extended attribute name: 5251 * 5252 * NAMESPACE XATTR_COMPAT PREFIX 5253 * system * freebsd:system: 5254 * user 1 (none, can be used to access ZFS 5255 * fsattr(5) attributes created on Solaris) 5256 * user 0 user. 5257 */ 5258 static int 5259 zfs_create_attrname(int attrnamespace, const char *name, char *attrname, 5260 size_t size, boolean_t compat) 5261 { 5262 const char *namespace, *prefix, *suffix; 5263 5264 memset(attrname, 0, size); 5265 5266 switch (attrnamespace) { 5267 case EXTATTR_NAMESPACE_USER: 5268 if (compat) { 5269 /* 5270 * This is the default namespace by which we can access 5271 * all attributes created on Solaris. 5272 */ 5273 prefix = namespace = suffix = ""; 5274 } else { 5275 /* 5276 * This is compatible with the user namespace encoding 5277 * on Linux prior to xattr_compat, but nothing 5278 * else. 5279 */ 5280 prefix = ""; 5281 namespace = "user"; 5282 suffix = "."; 5283 } 5284 break; 5285 case EXTATTR_NAMESPACE_SYSTEM: 5286 prefix = "freebsd:"; 5287 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING; 5288 suffix = ":"; 5289 break; 5290 case EXTATTR_NAMESPACE_EMPTY: 5291 default: 5292 return (SET_ERROR(EINVAL)); 5293 } 5294 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix, 5295 name) >= size) { 5296 return (SET_ERROR(ENAMETOOLONG)); 5297 } 5298 return (0); 5299 } 5300 5301 static int 5302 zfs_ensure_xattr_cached(znode_t *zp) 5303 { 5304 int error = 0; 5305 5306 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock)); 5307 5308 if (zp->z_xattr_cached != NULL) 5309 return (0); 5310 5311 if (rw_write_held(&zp->z_xattr_lock)) 5312 return (zfs_sa_get_xattr(zp)); 5313 5314 if (!rw_tryupgrade(&zp->z_xattr_lock)) { 5315 rw_exit(&zp->z_xattr_lock); 5316 rw_enter(&zp->z_xattr_lock, RW_WRITER); 5317 } 5318 if (zp->z_xattr_cached == NULL) 5319 error = zfs_sa_get_xattr(zp); 5320 rw_downgrade(&zp->z_xattr_lock); 5321 return (error); 5322 } 5323 5324 #ifndef _SYS_SYSPROTO_H_ 5325 struct vop_getextattr { 5326 IN struct vnode *a_vp; 5327 IN int a_attrnamespace; 5328 IN const char *a_name; 5329 INOUT struct uio *a_uio; 5330 OUT size_t *a_size; 5331 IN struct ucred *a_cred; 5332 IN struct thread *a_td; 5333 }; 5334 #endif 5335 5336 static int 5337 zfs_getextattr_dir(struct vop_getextattr_args *ap, const char *attrname) 5338 { 5339 struct thread *td = ap->a_td; 5340 struct nameidata nd; 5341 struct vattr va; 5342 vnode_t *xvp = NULL, *vp; 5343 int error, flags; 5344 5345 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, 5346 LOOKUP_XATTR, B_FALSE); 5347 if (error != 0) 5348 return (error); 5349 5350 flags = FREAD; 5351 #if __FreeBSD_version < 1400043 5352 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, 5353 xvp, td); 5354 #else 5355 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp); 5356 #endif 5357 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_INVFS, ap->a_cred, NULL); 5358 if (error != 0) 5359 return (SET_ERROR(error)); 5360 vp = nd.ni_vp; 5361 NDFREE_PNBUF(&nd); 5362 5363 if (ap->a_size != NULL) { 5364 error = VOP_GETATTR(vp, &va, ap->a_cred); 5365 if (error == 0) 5366 *ap->a_size = (size_t)va.va_size; 5367 } else if (ap->a_uio != NULL) 5368 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred); 5369 5370 VOP_UNLOCK1(vp); 5371 vn_close(vp, flags, ap->a_cred, td); 5372 return (error); 5373 } 5374 5375 static int 5376 zfs_getextattr_sa(struct vop_getextattr_args *ap, const char *attrname) 5377 { 5378 znode_t *zp = VTOZ(ap->a_vp); 5379 uchar_t *nv_value; 5380 uint_t nv_size; 5381 int error; 5382 5383 error = zfs_ensure_xattr_cached(zp); 5384 if (error != 0) 5385 return (error); 5386 5387 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock)); 5388 ASSERT3P(zp->z_xattr_cached, !=, NULL); 5389 5390 error = nvlist_lookup_byte_array(zp->z_xattr_cached, attrname, 5391 &nv_value, &nv_size); 5392 if (error != 0) 5393 return (SET_ERROR(error)); 5394 5395 if (ap->a_size != NULL) 5396 *ap->a_size = nv_size; 5397 else if (ap->a_uio != NULL) 5398 error = uiomove(nv_value, nv_size, ap->a_uio); 5399 if (error != 0) 5400 return (SET_ERROR(error)); 5401 5402 return (0); 5403 } 5404 5405 static int 5406 zfs_getextattr_impl(struct vop_getextattr_args *ap, boolean_t compat) 5407 { 5408 znode_t *zp = VTOZ(ap->a_vp); 5409 zfsvfs_t *zfsvfs = ZTOZSB(zp); 5410 char attrname[EXTATTR_MAXNAMELEN+1]; 5411 int error; 5412 5413 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 5414 sizeof (attrname), compat); 5415 if (error != 0) 5416 return (error); 5417 5418 error = ENOENT; 5419 if (zfsvfs->z_use_sa && zp->z_is_sa) 5420 error = zfs_getextattr_sa(ap, attrname); 5421 if (error == ENOENT) 5422 error = zfs_getextattr_dir(ap, attrname); 5423 return (error); 5424 } 5425 5426 /* 5427 * Vnode operation to retrieve a named extended attribute. 5428 */ 5429 static int 5430 zfs_getextattr(struct vop_getextattr_args *ap) 5431 { 5432 znode_t *zp = VTOZ(ap->a_vp); 5433 zfsvfs_t *zfsvfs = ZTOZSB(zp); 5434 int error; 5435 5436 /* 5437 * If the xattr property is off, refuse the request. 5438 */ 5439 if (!(zfsvfs->z_flags & ZSB_XATTR)) 5440 return (SET_ERROR(EOPNOTSUPP)); 5441 5442 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 5443 ap->a_cred, ap->a_td, VREAD); 5444 if (error != 0) 5445 return (SET_ERROR(error)); 5446 5447 error = zfs_check_attrname(ap->a_name); 5448 if (error != 0) 5449 return (error); 5450 5451 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 5452 return (error); 5453 error = ENOENT; 5454 rw_enter(&zp->z_xattr_lock, RW_READER); 5455 5456 error = zfs_getextattr_impl(ap, zfs_xattr_compat); 5457 if ((error == ENOENT || error == ENOATTR) && 5458 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) { 5459 /* 5460 * Fall back to the alternate namespace format if we failed to 5461 * find a user xattr. 5462 */ 5463 error = zfs_getextattr_impl(ap, !zfs_xattr_compat); 5464 } 5465 5466 rw_exit(&zp->z_xattr_lock); 5467 zfs_exit(zfsvfs, FTAG); 5468 if (error == ENOENT) 5469 error = SET_ERROR(ENOATTR); 5470 return (error); 5471 } 5472 5473 #ifndef _SYS_SYSPROTO_H_ 5474 struct vop_deleteextattr { 5475 IN struct vnode *a_vp; 5476 IN int a_attrnamespace; 5477 IN const char *a_name; 5478 IN struct ucred *a_cred; 5479 IN struct thread *a_td; 5480 }; 5481 #endif 5482 5483 static int 5484 zfs_deleteextattr_dir(struct vop_deleteextattr_args *ap, const char *attrname) 5485 { 5486 struct nameidata nd; 5487 vnode_t *xvp = NULL, *vp; 5488 int error; 5489 5490 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, 5491 LOOKUP_XATTR, B_FALSE); 5492 if (error != 0) 5493 return (error); 5494 5495 #if __FreeBSD_version < 1400043 5496 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF, 5497 UIO_SYSSPACE, attrname, xvp, ap->a_td); 5498 #else 5499 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF, 5500 UIO_SYSSPACE, attrname, xvp); 5501 #endif 5502 error = namei(&nd); 5503 if (error != 0) 5504 return (SET_ERROR(error)); 5505 5506 vp = nd.ni_vp; 5507 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd); 5508 NDFREE_PNBUF(&nd); 5509 5510 vput(nd.ni_dvp); 5511 if (vp == nd.ni_dvp) 5512 vrele(vp); 5513 else 5514 vput(vp); 5515 5516 return (error); 5517 } 5518 5519 static int 5520 zfs_deleteextattr_sa(struct vop_deleteextattr_args *ap, const char *attrname) 5521 { 5522 znode_t *zp = VTOZ(ap->a_vp); 5523 nvlist_t *nvl; 5524 int error; 5525 5526 error = zfs_ensure_xattr_cached(zp); 5527 if (error != 0) 5528 return (error); 5529 5530 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock)); 5531 ASSERT3P(zp->z_xattr_cached, !=, NULL); 5532 5533 nvl = zp->z_xattr_cached; 5534 error = nvlist_remove(nvl, attrname, DATA_TYPE_BYTE_ARRAY); 5535 if (error != 0) 5536 error = SET_ERROR(error); 5537 else 5538 error = zfs_sa_set_xattr(zp, attrname, NULL, 0); 5539 if (error != 0) { 5540 zp->z_xattr_cached = NULL; 5541 nvlist_free(nvl); 5542 } 5543 return (error); 5544 } 5545 5546 static int 5547 zfs_deleteextattr_impl(struct vop_deleteextattr_args *ap, boolean_t compat) 5548 { 5549 znode_t *zp = VTOZ(ap->a_vp); 5550 zfsvfs_t *zfsvfs = ZTOZSB(zp); 5551 char attrname[EXTATTR_MAXNAMELEN+1]; 5552 int error; 5553 5554 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 5555 sizeof (attrname), compat); 5556 if (error != 0) 5557 return (error); 5558 5559 error = ENOENT; 5560 if (zfsvfs->z_use_sa && zp->z_is_sa) 5561 error = zfs_deleteextattr_sa(ap, attrname); 5562 if (error == ENOENT) 5563 error = zfs_deleteextattr_dir(ap, attrname); 5564 return (error); 5565 } 5566 5567 /* 5568 * Vnode operation to remove a named attribute. 5569 */ 5570 static int 5571 zfs_deleteextattr(struct vop_deleteextattr_args *ap) 5572 { 5573 znode_t *zp = VTOZ(ap->a_vp); 5574 zfsvfs_t *zfsvfs = ZTOZSB(zp); 5575 int error; 5576 5577 /* 5578 * If the xattr property is off, refuse the request. 5579 */ 5580 if (!(zfsvfs->z_flags & ZSB_XATTR)) 5581 return (SET_ERROR(EOPNOTSUPP)); 5582 5583 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 5584 ap->a_cred, ap->a_td, VWRITE); 5585 if (error != 0) 5586 return (SET_ERROR(error)); 5587 5588 error = zfs_check_attrname(ap->a_name); 5589 if (error != 0) 5590 return (error); 5591 5592 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 5593 return (error); 5594 rw_enter(&zp->z_xattr_lock, RW_WRITER); 5595 5596 error = zfs_deleteextattr_impl(ap, zfs_xattr_compat); 5597 if ((error == ENOENT || error == ENOATTR) && 5598 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) { 5599 /* 5600 * Fall back to the alternate namespace format if we failed to 5601 * find a user xattr. 5602 */ 5603 error = zfs_deleteextattr_impl(ap, !zfs_xattr_compat); 5604 } 5605 5606 rw_exit(&zp->z_xattr_lock); 5607 zfs_exit(zfsvfs, FTAG); 5608 if (error == ENOENT) 5609 error = SET_ERROR(ENOATTR); 5610 return (error); 5611 } 5612 5613 #ifndef _SYS_SYSPROTO_H_ 5614 struct vop_setextattr { 5615 IN struct vnode *a_vp; 5616 IN int a_attrnamespace; 5617 IN const char *a_name; 5618 INOUT struct uio *a_uio; 5619 IN struct ucred *a_cred; 5620 IN struct thread *a_td; 5621 }; 5622 #endif 5623 5624 static int 5625 zfs_setextattr_dir(struct vop_setextattr_args *ap, const char *attrname) 5626 { 5627 struct thread *td = ap->a_td; 5628 struct nameidata nd; 5629 struct vattr va; 5630 vnode_t *xvp = NULL, *vp; 5631 int error, flags; 5632 5633 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, 5634 LOOKUP_XATTR | CREATE_XATTR_DIR, B_FALSE); 5635 if (error != 0) 5636 return (error); 5637 5638 flags = FFLAGS(O_WRONLY | O_CREAT); 5639 #if __FreeBSD_version < 1400043 5640 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp, td); 5641 #else 5642 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp); 5643 #endif 5644 error = vn_open_cred(&nd, &flags, 0600, VN_OPEN_INVFS, ap->a_cred, 5645 NULL); 5646 if (error != 0) 5647 return (SET_ERROR(error)); 5648 vp = nd.ni_vp; 5649 NDFREE_PNBUF(&nd); 5650 5651 VATTR_NULL(&va); 5652 va.va_size = 0; 5653 error = VOP_SETATTR(vp, &va, ap->a_cred); 5654 if (error == 0) 5655 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred); 5656 5657 VOP_UNLOCK1(vp); 5658 vn_close(vp, flags, ap->a_cred, td); 5659 return (error); 5660 } 5661 5662 static int 5663 zfs_setextattr_sa(struct vop_setextattr_args *ap, const char *attrname) 5664 { 5665 znode_t *zp = VTOZ(ap->a_vp); 5666 nvlist_t *nvl; 5667 size_t sa_size; 5668 int error; 5669 5670 error = zfs_ensure_xattr_cached(zp); 5671 if (error != 0) 5672 return (error); 5673 5674 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock)); 5675 ASSERT3P(zp->z_xattr_cached, !=, NULL); 5676 5677 nvl = zp->z_xattr_cached; 5678 size_t entry_size = ap->a_uio->uio_resid; 5679 if (entry_size > DXATTR_MAX_ENTRY_SIZE) 5680 return (SET_ERROR(EFBIG)); 5681 error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR); 5682 if (error != 0) 5683 return (SET_ERROR(error)); 5684 if (sa_size > DXATTR_MAX_SA_SIZE) 5685 return (SET_ERROR(EFBIG)); 5686 uchar_t *buf = kmem_alloc(entry_size, KM_SLEEP); 5687 error = uiomove(buf, entry_size, ap->a_uio); 5688 if (error != 0) { 5689 error = SET_ERROR(error); 5690 } else { 5691 error = nvlist_add_byte_array(nvl, attrname, buf, entry_size); 5692 if (error != 0) 5693 error = SET_ERROR(error); 5694 } 5695 if (error == 0) 5696 error = zfs_sa_set_xattr(zp, attrname, buf, entry_size); 5697 kmem_free(buf, entry_size); 5698 if (error != 0) { 5699 zp->z_xattr_cached = NULL; 5700 nvlist_free(nvl); 5701 } 5702 return (error); 5703 } 5704 5705 static int 5706 zfs_setextattr_impl(struct vop_setextattr_args *ap, boolean_t compat) 5707 { 5708 znode_t *zp = VTOZ(ap->a_vp); 5709 zfsvfs_t *zfsvfs = ZTOZSB(zp); 5710 char attrname[EXTATTR_MAXNAMELEN+1]; 5711 int error; 5712 5713 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname, 5714 sizeof (attrname), compat); 5715 if (error != 0) 5716 return (error); 5717 5718 struct vop_deleteextattr_args vda = { 5719 .a_vp = ap->a_vp, 5720 .a_attrnamespace = ap->a_attrnamespace, 5721 .a_name = ap->a_name, 5722 .a_cred = ap->a_cred, 5723 .a_td = ap->a_td, 5724 }; 5725 error = ENOENT; 5726 if (zfsvfs->z_use_sa && zp->z_is_sa && zfsvfs->z_xattr_sa) { 5727 error = zfs_setextattr_sa(ap, attrname); 5728 if (error == 0) { 5729 /* 5730 * Successfully put into SA, we need to clear the one 5731 * in dir if present. 5732 */ 5733 zfs_deleteextattr_dir(&vda, attrname); 5734 } 5735 } 5736 if (error != 0) { 5737 error = zfs_setextattr_dir(ap, attrname); 5738 if (error == 0 && zp->z_is_sa) { 5739 /* 5740 * Successfully put into dir, we need to clear the one 5741 * in SA if present. 5742 */ 5743 zfs_deleteextattr_sa(&vda, attrname); 5744 } 5745 } 5746 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) { 5747 /* 5748 * Also clear all versions of the alternate compat name. 5749 */ 5750 zfs_deleteextattr_impl(&vda, !compat); 5751 } 5752 return (error); 5753 } 5754 5755 /* 5756 * Vnode operation to set a named attribute. 5757 */ 5758 static int 5759 zfs_setextattr(struct vop_setextattr_args *ap) 5760 { 5761 znode_t *zp = VTOZ(ap->a_vp); 5762 zfsvfs_t *zfsvfs = ZTOZSB(zp); 5763 int error; 5764 5765 /* 5766 * If the xattr property is off, refuse the request. 5767 */ 5768 if (!(zfsvfs->z_flags & ZSB_XATTR)) 5769 return (SET_ERROR(EOPNOTSUPP)); 5770 5771 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 5772 ap->a_cred, ap->a_td, VWRITE); 5773 if (error != 0) 5774 return (SET_ERROR(error)); 5775 5776 error = zfs_check_attrname(ap->a_name); 5777 if (error != 0) 5778 return (error); 5779 5780 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 5781 return (error); 5782 rw_enter(&zp->z_xattr_lock, RW_WRITER); 5783 5784 error = zfs_setextattr_impl(ap, zfs_xattr_compat); 5785 5786 rw_exit(&zp->z_xattr_lock); 5787 zfs_exit(zfsvfs, FTAG); 5788 return (error); 5789 } 5790 5791 #ifndef _SYS_SYSPROTO_H_ 5792 struct vop_listextattr { 5793 IN struct vnode *a_vp; 5794 IN int a_attrnamespace; 5795 INOUT struct uio *a_uio; 5796 OUT size_t *a_size; 5797 IN struct ucred *a_cred; 5798 IN struct thread *a_td; 5799 }; 5800 #endif 5801 5802 static int 5803 zfs_listextattr_dir(struct vop_listextattr_args *ap, const char *attrprefix) 5804 { 5805 struct thread *td = ap->a_td; 5806 struct nameidata nd; 5807 uint8_t dirbuf[sizeof (struct dirent)]; 5808 struct iovec aiov; 5809 struct uio auio; 5810 vnode_t *xvp = NULL, *vp; 5811 int error, eof; 5812 5813 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, 5814 LOOKUP_XATTR, B_FALSE); 5815 if (error != 0) { 5816 /* 5817 * ENOATTR means that the EA directory does not yet exist, 5818 * i.e. there are no extended attributes there. 5819 */ 5820 if (error == ENOATTR) 5821 error = 0; 5822 return (error); 5823 } 5824 5825 #if __FreeBSD_version < 1400043 5826 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED, 5827 UIO_SYSSPACE, ".", xvp, td); 5828 #else 5829 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED, 5830 UIO_SYSSPACE, ".", xvp); 5831 #endif 5832 error = namei(&nd); 5833 if (error != 0) 5834 return (SET_ERROR(error)); 5835 vp = nd.ni_vp; 5836 NDFREE_PNBUF(&nd); 5837 5838 auio.uio_iov = &aiov; 5839 auio.uio_iovcnt = 1; 5840 auio.uio_segflg = UIO_SYSSPACE; 5841 auio.uio_td = td; 5842 auio.uio_rw = UIO_READ; 5843 auio.uio_offset = 0; 5844 5845 size_t plen = strlen(attrprefix); 5846 5847 do { 5848 aiov.iov_base = (void *)dirbuf; 5849 aiov.iov_len = sizeof (dirbuf); 5850 auio.uio_resid = sizeof (dirbuf); 5851 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL); 5852 if (error != 0) 5853 break; 5854 int done = sizeof (dirbuf) - auio.uio_resid; 5855 for (int pos = 0; pos < done; ) { 5856 struct dirent *dp = (struct dirent *)(dirbuf + pos); 5857 pos += dp->d_reclen; 5858 /* 5859 * XXX: Temporarily we also accept DT_UNKNOWN, as this 5860 * is what we get when attribute was created on Solaris. 5861 */ 5862 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN) 5863 continue; 5864 else if (plen == 0 && 5865 ZFS_XA_NS_PREFIX_FORBIDDEN(dp->d_name)) 5866 continue; 5867 else if (strncmp(dp->d_name, attrprefix, plen) != 0) 5868 continue; 5869 uint8_t nlen = dp->d_namlen - plen; 5870 if (ap->a_size != NULL) { 5871 *ap->a_size += 1 + nlen; 5872 } else if (ap->a_uio != NULL) { 5873 /* 5874 * Format of extattr name entry is one byte for 5875 * length and the rest for name. 5876 */ 5877 error = uiomove(&nlen, 1, ap->a_uio); 5878 if (error == 0) { 5879 char *namep = dp->d_name + plen; 5880 error = uiomove(namep, nlen, ap->a_uio); 5881 } 5882 if (error != 0) { 5883 error = SET_ERROR(error); 5884 break; 5885 } 5886 } 5887 } 5888 } while (!eof && error == 0); 5889 5890 vput(vp); 5891 return (error); 5892 } 5893 5894 static int 5895 zfs_listextattr_sa(struct vop_listextattr_args *ap, const char *attrprefix) 5896 { 5897 znode_t *zp = VTOZ(ap->a_vp); 5898 int error; 5899 5900 error = zfs_ensure_xattr_cached(zp); 5901 if (error != 0) 5902 return (error); 5903 5904 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock)); 5905 ASSERT3P(zp->z_xattr_cached, !=, NULL); 5906 5907 size_t plen = strlen(attrprefix); 5908 nvpair_t *nvp = NULL; 5909 while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) { 5910 ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY); 5911 5912 const char *name = nvpair_name(nvp); 5913 if (plen == 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name)) 5914 continue; 5915 else if (strncmp(name, attrprefix, plen) != 0) 5916 continue; 5917 uint8_t nlen = strlen(name) - plen; 5918 if (ap->a_size != NULL) { 5919 *ap->a_size += 1 + nlen; 5920 } else if (ap->a_uio != NULL) { 5921 /* 5922 * Format of extattr name entry is one byte for 5923 * length and the rest for name. 5924 */ 5925 error = uiomove(&nlen, 1, ap->a_uio); 5926 if (error == 0) { 5927 char *namep = __DECONST(char *, name) + plen; 5928 error = uiomove(namep, nlen, ap->a_uio); 5929 } 5930 if (error != 0) { 5931 error = SET_ERROR(error); 5932 break; 5933 } 5934 } 5935 } 5936 5937 return (error); 5938 } 5939 5940 static int 5941 zfs_listextattr_impl(struct vop_listextattr_args *ap, boolean_t compat) 5942 { 5943 znode_t *zp = VTOZ(ap->a_vp); 5944 zfsvfs_t *zfsvfs = ZTOZSB(zp); 5945 char attrprefix[16]; 5946 int error; 5947 5948 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix, 5949 sizeof (attrprefix), compat); 5950 if (error != 0) 5951 return (error); 5952 5953 if (zfsvfs->z_use_sa && zp->z_is_sa) 5954 error = zfs_listextattr_sa(ap, attrprefix); 5955 if (error == 0) 5956 error = zfs_listextattr_dir(ap, attrprefix); 5957 return (error); 5958 } 5959 5960 /* 5961 * Vnode operation to retrieve extended attributes on a vnode. 5962 */ 5963 static int 5964 zfs_listextattr(struct vop_listextattr_args *ap) 5965 { 5966 znode_t *zp = VTOZ(ap->a_vp); 5967 zfsvfs_t *zfsvfs = ZTOZSB(zp); 5968 int error; 5969 5970 if (ap->a_size != NULL) 5971 *ap->a_size = 0; 5972 5973 /* 5974 * If the xattr property is off, refuse the request. 5975 */ 5976 if (!(zfsvfs->z_flags & ZSB_XATTR)) 5977 return (SET_ERROR(EOPNOTSUPP)); 5978 5979 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace, 5980 ap->a_cred, ap->a_td, VREAD); 5981 if (error != 0) 5982 return (SET_ERROR(error)); 5983 5984 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 5985 return (error); 5986 rw_enter(&zp->z_xattr_lock, RW_READER); 5987 5988 error = zfs_listextattr_impl(ap, zfs_xattr_compat); 5989 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) { 5990 /* Also list user xattrs with the alternate format. */ 5991 error = zfs_listextattr_impl(ap, !zfs_xattr_compat); 5992 } 5993 5994 rw_exit(&zp->z_xattr_lock); 5995 zfs_exit(zfsvfs, FTAG); 5996 return (error); 5997 } 5998 5999 #ifndef _SYS_SYSPROTO_H_ 6000 struct vop_getacl_args { 6001 struct vnode *vp; 6002 acl_type_t type; 6003 struct acl *aclp; 6004 struct ucred *cred; 6005 struct thread *td; 6006 }; 6007 #endif 6008 6009 static int 6010 zfs_freebsd_getacl(struct vop_getacl_args *ap) 6011 { 6012 int error; 6013 vsecattr_t vsecattr; 6014 6015 if (ap->a_type != ACL_TYPE_NFS4) 6016 return (EINVAL); 6017 6018 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT; 6019 if ((error = zfs_getsecattr(VTOZ(ap->a_vp), 6020 &vsecattr, 0, ap->a_cred))) 6021 return (error); 6022 6023 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, 6024 vsecattr.vsa_aclcnt); 6025 if (vsecattr.vsa_aclentp != NULL) 6026 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz); 6027 6028 return (error); 6029 } 6030 6031 #ifndef _SYS_SYSPROTO_H_ 6032 struct vop_setacl_args { 6033 struct vnode *vp; 6034 acl_type_t type; 6035 struct acl *aclp; 6036 struct ucred *cred; 6037 struct thread *td; 6038 }; 6039 #endif 6040 6041 static int 6042 zfs_freebsd_setacl(struct vop_setacl_args *ap) 6043 { 6044 int error; 6045 vsecattr_t vsecattr; 6046 int aclbsize; /* size of acl list in bytes */ 6047 aclent_t *aaclp; 6048 6049 if (ap->a_type != ACL_TYPE_NFS4) 6050 return (EINVAL); 6051 6052 if (ap->a_aclp == NULL) 6053 return (EINVAL); 6054 6055 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES) 6056 return (EINVAL); 6057 6058 /* 6059 * With NFSv4 ACLs, chmod(2) may need to add additional entries, 6060 * splitting every entry into two and appending "canonical six" 6061 * entries at the end. Don't allow for setting an ACL that would 6062 * cause chmod(2) to run out of ACL entries. 6063 */ 6064 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES) 6065 return (ENOSPC); 6066 6067 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR); 6068 if (error != 0) 6069 return (error); 6070 6071 vsecattr.vsa_mask = VSA_ACE; 6072 aclbsize = ap->a_aclp->acl_cnt * sizeof (ace_t); 6073 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP); 6074 aaclp = vsecattr.vsa_aclentp; 6075 vsecattr.vsa_aclentsz = aclbsize; 6076 6077 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp); 6078 error = zfs_setsecattr(VTOZ(ap->a_vp), &vsecattr, 0, ap->a_cred); 6079 kmem_free(aaclp, aclbsize); 6080 6081 return (error); 6082 } 6083 6084 #ifndef _SYS_SYSPROTO_H_ 6085 struct vop_aclcheck_args { 6086 struct vnode *vp; 6087 acl_type_t type; 6088 struct acl *aclp; 6089 struct ucred *cred; 6090 struct thread *td; 6091 }; 6092 #endif 6093 6094 static int 6095 zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap) 6096 { 6097 6098 return (EOPNOTSUPP); 6099 } 6100 6101 static int 6102 zfs_vptocnp(struct vop_vptocnp_args *ap) 6103 { 6104 vnode_t *covered_vp; 6105 vnode_t *vp = ap->a_vp; 6106 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data; 6107 znode_t *zp = VTOZ(vp); 6108 int ltype; 6109 int error; 6110 6111 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 6112 return (error); 6113 6114 /* 6115 * If we are a snapshot mounted under .zfs, run the operation 6116 * on the covered vnode. 6117 */ 6118 if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) { 6119 char name[MAXNAMLEN + 1]; 6120 znode_t *dzp; 6121 size_t len; 6122 6123 error = zfs_znode_parent_and_name(zp, &dzp, name); 6124 if (error == 0) { 6125 len = strlen(name); 6126 if (*ap->a_buflen < len) 6127 error = SET_ERROR(ENOMEM); 6128 } 6129 if (error == 0) { 6130 *ap->a_buflen -= len; 6131 memcpy(ap->a_buf + *ap->a_buflen, name, len); 6132 *ap->a_vpp = ZTOV(dzp); 6133 } 6134 zfs_exit(zfsvfs, FTAG); 6135 return (error); 6136 } 6137 zfs_exit(zfsvfs, FTAG); 6138 6139 covered_vp = vp->v_mount->mnt_vnodecovered; 6140 #if __FreeBSD_version >= 1300045 6141 enum vgetstate vs = vget_prep(covered_vp); 6142 #else 6143 vhold(covered_vp); 6144 #endif 6145 ltype = VOP_ISLOCKED(vp); 6146 VOP_UNLOCK1(vp); 6147 #if __FreeBSD_version >= 1300045 6148 error = vget_finish(covered_vp, LK_SHARED, vs); 6149 #else 6150 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread); 6151 #endif 6152 if (error == 0) { 6153 #if __FreeBSD_version >= 1300123 6154 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_buf, 6155 ap->a_buflen); 6156 #else 6157 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred, 6158 ap->a_buf, ap->a_buflen); 6159 #endif 6160 vput(covered_vp); 6161 } 6162 vn_lock(vp, ltype | LK_RETRY); 6163 if (VN_IS_DOOMED(vp)) 6164 error = SET_ERROR(ENOENT); 6165 return (error); 6166 } 6167 6168 #if __FreeBSD_version >= 1400032 6169 static int 6170 zfs_deallocate(struct vop_deallocate_args *ap) 6171 { 6172 znode_t *zp = VTOZ(ap->a_vp); 6173 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 6174 zilog_t *zilog; 6175 off_t off, len, file_sz; 6176 int error; 6177 6178 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 6179 return (error); 6180 6181 /* 6182 * Callers might not be able to detect properly that we are read-only, 6183 * so check it explicitly here. 6184 */ 6185 if (zfs_is_readonly(zfsvfs)) { 6186 zfs_exit(zfsvfs, FTAG); 6187 return (SET_ERROR(EROFS)); 6188 } 6189 6190 zilog = zfsvfs->z_log; 6191 off = *ap->a_offset; 6192 len = *ap->a_len; 6193 file_sz = zp->z_size; 6194 if (off + len > file_sz) 6195 len = file_sz - off; 6196 /* Fast path for out-of-range request. */ 6197 if (len <= 0) { 6198 *ap->a_len = 0; 6199 zfs_exit(zfsvfs, FTAG); 6200 return (0); 6201 } 6202 6203 error = zfs_freesp(zp, off, len, O_RDWR, TRUE); 6204 if (error == 0) { 6205 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS || 6206 (ap->a_ioflag & IO_SYNC) != 0) 6207 zil_commit(zilog, zp->z_id); 6208 *ap->a_offset = off + len; 6209 *ap->a_len = 0; 6210 } 6211 6212 zfs_exit(zfsvfs, FTAG); 6213 return (error); 6214 } 6215 #endif 6216 6217 #ifndef _SYS_SYSPROTO_H_ 6218 struct vop_copy_file_range_args { 6219 struct vnode *a_invp; 6220 off_t *a_inoffp; 6221 struct vnode *a_outvp; 6222 off_t *a_outoffp; 6223 size_t *a_lenp; 6224 unsigned int a_flags; 6225 struct ucred *a_incred; 6226 struct ucred *a_outcred; 6227 struct thread *a_fsizetd; 6228 } 6229 #endif 6230 /* 6231 * TODO: FreeBSD will only call file system-specific copy_file_range() if both 6232 * files resides under the same mountpoint. In case of ZFS we want to be called 6233 * even is files are in different datasets (but on the same pools, but we need 6234 * to check that ourselves). 6235 */ 6236 static int 6237 zfs_freebsd_copy_file_range(struct vop_copy_file_range_args *ap) 6238 { 6239 zfsvfs_t *outzfsvfs; 6240 struct vnode *invp = ap->a_invp; 6241 struct vnode *outvp = ap->a_outvp; 6242 struct mount *mp; 6243 struct uio io; 6244 int error; 6245 uint64_t len = *ap->a_lenp; 6246 6247 /* 6248 * TODO: If offset/length is not aligned to recordsize, use 6249 * vn_generic_copy_file_range() on this fragment. 6250 * It would be better to do this after we lock the vnodes, but then we 6251 * need something else than vn_generic_copy_file_range(). 6252 */ 6253 6254 vn_start_write(outvp, &mp, V_WAIT); 6255 if (__predict_true(mp == outvp->v_mount)) { 6256 outzfsvfs = (zfsvfs_t *)mp->mnt_data; 6257 if (!spa_feature_is_enabled(dmu_objset_spa(outzfsvfs->z_os), 6258 SPA_FEATURE_BLOCK_CLONING)) { 6259 goto bad_write_fallback; 6260 } 6261 } 6262 if (invp == outvp) { 6263 if (vn_lock(outvp, LK_EXCLUSIVE) != 0) { 6264 goto bad_write_fallback; 6265 } 6266 } else { 6267 vn_lock_pair(invp, false, LK_EXCLUSIVE, outvp, false, 6268 LK_EXCLUSIVE); 6269 if (VN_IS_DOOMED(invp) || VN_IS_DOOMED(outvp)) { 6270 goto bad_locked_fallback; 6271 } 6272 } 6273 6274 #ifdef MAC 6275 error = mac_vnode_check_write(curthread->td_ucred, ap->a_outcred, 6276 outvp); 6277 if (error != 0) 6278 goto out_locked; 6279 #endif 6280 6281 io.uio_offset = *ap->a_outoffp; 6282 io.uio_resid = *ap->a_lenp; 6283 error = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd); 6284 if (error != 0) 6285 goto out_locked; 6286 6287 error = zfs_clone_range(VTOZ(invp), ap->a_inoffp, VTOZ(outvp), 6288 ap->a_outoffp, &len, ap->a_outcred); 6289 if (error == EXDEV) 6290 goto bad_locked_fallback; 6291 *ap->a_lenp = (size_t)len; 6292 out_locked: 6293 if (invp != outvp) 6294 VOP_UNLOCK(invp); 6295 VOP_UNLOCK(outvp); 6296 if (mp != NULL) 6297 vn_finished_write(mp); 6298 return (error); 6299 6300 bad_locked_fallback: 6301 if (invp != outvp) 6302 VOP_UNLOCK(invp); 6303 VOP_UNLOCK(outvp); 6304 bad_write_fallback: 6305 if (mp != NULL) 6306 vn_finished_write(mp); 6307 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp, 6308 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags, 6309 ap->a_incred, ap->a_outcred, ap->a_fsizetd); 6310 return (error); 6311 } 6312 6313 struct vop_vector zfs_vnodeops; 6314 struct vop_vector zfs_fifoops; 6315 struct vop_vector zfs_shareops; 6316 6317 struct vop_vector zfs_vnodeops = { 6318 .vop_default = &default_vnodeops, 6319 .vop_inactive = zfs_freebsd_inactive, 6320 #if __FreeBSD_version >= 1300042 6321 .vop_need_inactive = zfs_freebsd_need_inactive, 6322 #endif 6323 .vop_reclaim = zfs_freebsd_reclaim, 6324 #if __FreeBSD_version >= 1300102 6325 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec, 6326 #endif 6327 #if __FreeBSD_version >= 1300139 6328 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink, 6329 #endif 6330 .vop_access = zfs_freebsd_access, 6331 .vop_allocate = VOP_EINVAL, 6332 #if __FreeBSD_version >= 1400032 6333 .vop_deallocate = zfs_deallocate, 6334 #endif 6335 .vop_lookup = zfs_cache_lookup, 6336 .vop_cachedlookup = zfs_freebsd_cachedlookup, 6337 .vop_getattr = zfs_freebsd_getattr, 6338 .vop_setattr = zfs_freebsd_setattr, 6339 .vop_create = zfs_freebsd_create, 6340 .vop_mknod = (vop_mknod_t *)zfs_freebsd_create, 6341 .vop_mkdir = zfs_freebsd_mkdir, 6342 .vop_readdir = zfs_freebsd_readdir, 6343 .vop_fsync = zfs_freebsd_fsync, 6344 .vop_open = zfs_freebsd_open, 6345 .vop_close = zfs_freebsd_close, 6346 .vop_rmdir = zfs_freebsd_rmdir, 6347 .vop_ioctl = zfs_freebsd_ioctl, 6348 .vop_link = zfs_freebsd_link, 6349 .vop_symlink = zfs_freebsd_symlink, 6350 .vop_readlink = zfs_freebsd_readlink, 6351 .vop_read = zfs_freebsd_read, 6352 .vop_write = zfs_freebsd_write, 6353 .vop_remove = zfs_freebsd_remove, 6354 .vop_rename = zfs_freebsd_rename, 6355 .vop_pathconf = zfs_freebsd_pathconf, 6356 .vop_bmap = zfs_freebsd_bmap, 6357 .vop_fid = zfs_freebsd_fid, 6358 .vop_getextattr = zfs_getextattr, 6359 .vop_deleteextattr = zfs_deleteextattr, 6360 .vop_setextattr = zfs_setextattr, 6361 .vop_listextattr = zfs_listextattr, 6362 .vop_getacl = zfs_freebsd_getacl, 6363 .vop_setacl = zfs_freebsd_setacl, 6364 .vop_aclcheck = zfs_freebsd_aclcheck, 6365 .vop_getpages = zfs_freebsd_getpages, 6366 .vop_putpages = zfs_freebsd_putpages, 6367 .vop_vptocnp = zfs_vptocnp, 6368 #if __FreeBSD_version >= 1300064 6369 .vop_lock1 = vop_lock, 6370 .vop_unlock = vop_unlock, 6371 .vop_islocked = vop_islocked, 6372 #endif 6373 #if __FreeBSD_version >= 1400043 6374 .vop_add_writecount = vop_stdadd_writecount_nomsync, 6375 #endif 6376 .vop_copy_file_range = zfs_freebsd_copy_file_range, 6377 }; 6378 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops); 6379 6380 struct vop_vector zfs_fifoops = { 6381 .vop_default = &fifo_specops, 6382 .vop_fsync = zfs_freebsd_fsync, 6383 #if __FreeBSD_version >= 1300102 6384 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec, 6385 #endif 6386 #if __FreeBSD_version >= 1300139 6387 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink, 6388 #endif 6389 .vop_access = zfs_freebsd_access, 6390 .vop_getattr = zfs_freebsd_getattr, 6391 .vop_inactive = zfs_freebsd_inactive, 6392 .vop_read = VOP_PANIC, 6393 .vop_reclaim = zfs_freebsd_reclaim, 6394 .vop_setattr = zfs_freebsd_setattr, 6395 .vop_write = VOP_PANIC, 6396 .vop_pathconf = zfs_freebsd_pathconf, 6397 .vop_fid = zfs_freebsd_fid, 6398 .vop_getacl = zfs_freebsd_getacl, 6399 .vop_setacl = zfs_freebsd_setacl, 6400 .vop_aclcheck = zfs_freebsd_aclcheck, 6401 #if __FreeBSD_version >= 1400043 6402 .vop_add_writecount = vop_stdadd_writecount_nomsync, 6403 #endif 6404 }; 6405 VFS_VOP_VECTOR_REGISTER(zfs_fifoops); 6406 6407 /* 6408 * special share hidden files vnode operations template 6409 */ 6410 struct vop_vector zfs_shareops = { 6411 .vop_default = &default_vnodeops, 6412 #if __FreeBSD_version >= 1300121 6413 .vop_fplookup_vexec = VOP_EAGAIN, 6414 #endif 6415 #if __FreeBSD_version >= 1300139 6416 .vop_fplookup_symlink = VOP_EAGAIN, 6417 #endif 6418 .vop_access = zfs_freebsd_access, 6419 .vop_inactive = zfs_freebsd_inactive, 6420 .vop_reclaim = zfs_freebsd_reclaim, 6421 .vop_fid = zfs_freebsd_fid, 6422 .vop_pathconf = zfs_freebsd_pathconf, 6423 #if __FreeBSD_version >= 1400043 6424 .vop_add_writecount = vop_stdadd_writecount_nomsync, 6425 #endif 6426 }; 6427 VFS_VOP_VECTOR_REGISTER(zfs_shareops); 6428 6429 ZFS_MODULE_PARAM(zfs, zfs_, xattr_compat, INT, ZMOD_RW, 6430 "Use legacy ZFS xattr naming for writing new user namespace xattrs"); 6431