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