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