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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
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.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <security/mac/mac_framework.h>
39 #include <sys/endian.h>
41 #include <sys/vnode.h>
42 #if __FreeBSD_version >= 1300102
45 #include <sys/dirent.h>
49 #include <sys/taskq.h>
51 #include <sys/atomic.h>
52 #include <sys/namei.h>
54 #include <sys/cmn_err.h>
56 #include <sys/sysproto.h>
57 #include <sys/errno.h>
58 #include <sys/unistd.h>
59 #include <sys/zfs_dir.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/dmu_objset.h>
69 #include <sys/policy.h>
70 #include <sys/sunddi.h>
71 #include <sys/filio.h>
73 #include <sys/zfs_ctldir.h>
74 #include <sys/zfs_fuid.h>
75 #include <sys/zfs_quota.h>
76 #include <sys/zfs_sa.h>
77 #include <sys/zfs_rlock.h>
80 #include <sys/sched.h>
82 #include <sys/vmmeter.h>
83 #include <vm/vm_param.h>
85 #include <sys/zfs_vnops.h>
86 #include <sys/module.h>
87 #include <sys/sysent.h>
88 #include <sys/dmu_impl.h>
90 #include <sys/zfeature.h>
92 #include <vm/vm_object.h>
94 #include <sys/extattr.h>
98 #define VN_OPEN_INVFS 0x0
103 #if __FreeBSD_version < 1300103
104 #define NDFREE_PNBUF(ndp) NDFREE((ndp), NDF_ONLY_PNBUF)
107 #if __FreeBSD_version >= 1300047
108 #define vm_page_wire_lock(pp)
109 #define vm_page_wire_unlock(pp)
111 #define vm_page_wire_lock(pp) vm_page_lock(pp)
112 #define vm_page_wire_unlock(pp) vm_page_unlock(pp)
115 #ifdef DEBUG_VFS_LOCKS
116 #define VNCHECKREF(vp) \
117 VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp, \
118 ("%s: wrong ref counts", __func__));
120 #define VNCHECKREF(vp)
123 #if __FreeBSD_version >= 1400045
124 typedef uint64_t cookie_t;
126 typedef ulong_t cookie_t;
132 * Each vnode op performs some logical unit of work. To do this, the ZPL must
133 * properly lock its in-core state, create a DMU transaction, do the work,
134 * record this work in the intent log (ZIL), commit the DMU transaction,
135 * and wait for the intent log to commit if it is a synchronous operation.
136 * Moreover, the vnode ops must work in both normal and log replay context.
137 * The ordering of events is important to avoid deadlocks and references
138 * to freed memory. The example below illustrates the following Big Rules:
140 * (1) A check must be made in each zfs thread for a mounted file system.
141 * This is done avoiding races using zfs_enter(zfsvfs).
142 * A zfs_exit(zfsvfs) is needed before all returns. Any znodes
143 * must be checked with zfs_verify_zp(zp). Both of these macros
144 * can return EIO from the calling function.
146 * (2) VN_RELE() should always be the last thing except for zil_commit()
147 * (if necessary) and zfs_exit(). This is for 3 reasons:
148 * First, if it's the last reference, the vnode/znode
149 * can be freed, so the zp may point to freed memory. Second, the last
150 * reference will call zfs_zinactive(), which may induce a lot of work --
151 * pushing cached pages (which acquires range locks) and syncing out
152 * cached atime changes. Third, zfs_zinactive() may require a new tx,
153 * which could deadlock the system if you were already holding one.
154 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
156 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
157 * as they can span dmu_tx_assign() calls.
159 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
160 * dmu_tx_assign(). This is critical because we don't want to block
161 * while holding locks.
163 * If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT. This
164 * reduces lock contention and CPU usage when we must wait (note that if
165 * throughput is constrained by the storage, nearly every transaction
168 * Note, in particular, that if a lock is sometimes acquired before
169 * the tx assigns, and sometimes after (e.g. z_lock), then failing
170 * to use a non-blocking assign can deadlock the system. The scenario:
172 * Thread A has grabbed a lock before calling dmu_tx_assign().
173 * Thread B is in an already-assigned tx, and blocks for this lock.
174 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
175 * forever, because the previous txg can't quiesce until B's tx commits.
177 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
178 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
179 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
180 * to indicate that this operation has already called dmu_tx_wait().
181 * This will ensure that we don't retry forever, waiting a short bit
184 * (5) If the operation succeeded, generate the intent log entry for it
185 * before dropping locks. This ensures that the ordering of events
186 * in the intent log matches the order in which they actually occurred.
187 * During ZIL replay the zfs_log_* functions will update the sequence
188 * number to indicate the zil transaction has replayed.
190 * (6) At the end of each vnode op, the DMU tx must always commit,
191 * regardless of whether there were any errors.
193 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
194 * to ensure that synchronous semantics are provided when necessary.
196 * In general, this is how things should be ordered in each vnode op:
198 * zfs_enter(zfsvfs); // exit if unmounted
200 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
201 * rw_enter(...); // grab any other locks you need
202 * tx = dmu_tx_create(...); // get DMU tx
203 * dmu_tx_hold_*(); // hold each object you might modify
204 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
206 * rw_exit(...); // drop locks
207 * zfs_dirent_unlock(dl); // unlock directory entry
208 * VN_RELE(...); // release held vnodes
209 * if (error == ERESTART) {
215 * dmu_tx_abort(tx); // abort DMU tx
216 * zfs_exit(zfsvfs); // finished in zfs
217 * return (error); // really out of space
219 * error = do_real_work(); // do whatever this VOP does
221 * zfs_log_*(...); // on success, make ZIL entry
222 * dmu_tx_commit(tx); // commit DMU tx -- error or not
223 * rw_exit(...); // drop locks
224 * zfs_dirent_unlock(dl); // unlock directory entry
225 * VN_RELE(...); // release held vnodes
226 * zil_commit(zilog, foid); // synchronous when necessary
227 * zfs_exit(zfsvfs); // finished in zfs
228 * return (error); // done, report error
231 zfs_open(vnode_t **vpp, int flag, cred_t *cr)
234 znode_t *zp = VTOZ(*vpp);
235 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
238 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
241 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
242 ((flag & FAPPEND) == 0)) {
243 zfs_exit(zfsvfs, FTAG);
244 return (SET_ERROR(EPERM));
248 * Keep a count of the synchronous opens in the znode. On first
249 * synchronous open we must convert all previous async transactions
250 * into sync to keep correct ordering.
253 if (atomic_inc_32_nv(&zp->z_sync_cnt) == 1)
254 zil_async_to_sync(zfsvfs->z_log, zp->z_id);
257 zfs_exit(zfsvfs, FTAG);
262 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
264 (void) offset, (void) cr;
265 znode_t *zp = VTOZ(vp);
266 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
269 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
272 /* Decrement the synchronous opens in the znode */
273 if ((flag & O_SYNC) && (count == 1))
274 atomic_dec_32(&zp->z_sync_cnt);
276 zfs_exit(zfsvfs, FTAG);
281 zfs_ioctl(vnode_t *vp, ulong_t com, intptr_t data, int flag, cred_t *cred,
284 (void) flag, (void) cred, (void) rvalp;
294 * The following two ioctls are used by bfu. Faking out,
295 * necessary to avoid bfu errors.
307 off = *(offset_t *)data;
308 /* offset parameter is in/out */
309 error = zfs_holey(VTOZ(vp), com, &off);
312 *(offset_t *)data = off;
316 return (SET_ERROR(ENOTTY));
320 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
327 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
328 * aligned boundaries, if the range is not aligned. As a result a
329 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
330 * It may happen that all DEV_BSIZE subranges are marked clean and thus
331 * the whole page would be considered clean despite have some
333 * For this reason we should shrink the range to DEV_BSIZE aligned
334 * boundaries before calling vm_page_clear_dirty.
336 end = rounddown2(off + nbytes, DEV_BSIZE);
337 off = roundup2(off, DEV_BSIZE);
341 zfs_vmobject_assert_wlocked_12(obj);
342 #if __FreeBSD_version < 1300050
344 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
346 if (vm_page_xbusied(pp)) {
348 * Reference the page before unlocking and
349 * sleeping so that the page daemon is less
350 * likely to reclaim it.
352 vm_page_reference(pp);
354 zfs_vmobject_wunlock(obj);
355 vm_page_busy_sleep(pp, "zfsmwb", true);
356 zfs_vmobject_wlock(obj);
360 } else if (pp != NULL) {
365 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
366 vm_object_pip_add(obj, 1);
367 pmap_remove_write(pp);
369 vm_page_clear_dirty(pp, off, nbytes);
374 vm_page_grab_valid_unlocked(&pp, obj, OFF_TO_IDX(start),
375 VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_NORMAL |
378 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
379 vm_object_pip_add(obj, 1);
380 pmap_remove_write(pp);
382 vm_page_clear_dirty(pp, off, nbytes);
389 page_unbusy(vm_page_t pp)
393 #if __FreeBSD_version >= 1300041
394 vm_object_pip_wakeup(pp->object);
396 vm_object_pip_subtract(pp->object, 1);
400 #if __FreeBSD_version > 1300051
402 page_hold(vnode_t *vp, int64_t start)
408 vm_page_grab_valid_unlocked(&m, obj, OFF_TO_IDX(start),
409 VM_ALLOC_NOCREAT | VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY |
415 page_hold(vnode_t *vp, int64_t start)
421 zfs_vmobject_assert_wlocked(obj);
424 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
426 if (vm_page_xbusied(pp)) {
428 * Reference the page before unlocking and
429 * sleeping so that the page daemon is less
430 * likely to reclaim it.
432 vm_page_reference(pp);
434 zfs_vmobject_wunlock(obj);
435 vm_page_busy_sleep(pp, "zfsmwb", true);
436 zfs_vmobject_wlock(obj);
440 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
441 vm_page_wire_lock(pp);
443 vm_page_wire_unlock(pp);
454 page_unhold(vm_page_t pp)
457 vm_page_wire_lock(pp);
458 #if __FreeBSD_version >= 1300035
459 vm_page_unwire(pp, PQ_ACTIVE);
463 vm_page_wire_unlock(pp);
467 * When a file is memory mapped, we must keep the IO data synchronized
468 * between the DMU cache and the memory mapped pages. What this means:
470 * On Write: If we find a memory mapped page, we write to *both*
471 * the page and the dmu buffer.
474 update_pages(znode_t *zp, int64_t start, int len, objset_t *os)
478 vnode_t *vp = ZTOV(zp);
482 ASSERT3P(vp->v_mount, !=, NULL);
484 ASSERT3P(obj, !=, NULL);
486 off = start & PAGEOFFSET;
487 zfs_vmobject_wlock_12(obj);
488 #if __FreeBSD_version >= 1300041
489 vm_object_pip_add(obj, 1);
491 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
493 int nbytes = imin(PAGESIZE - off, len);
495 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
496 zfs_vmobject_wunlock_12(obj);
498 va = zfs_map_page(pp, &sf);
499 (void) dmu_read(os, zp->z_id, start + off, nbytes,
500 va + off, DMU_READ_PREFETCH);
503 zfs_vmobject_wlock_12(obj);
509 #if __FreeBSD_version >= 1300041
510 vm_object_pip_wakeup(obj);
512 vm_object_pip_wakeupn(obj, 0);
514 zfs_vmobject_wunlock_12(obj);
518 * Read with UIO_NOCOPY flag means that sendfile(2) requests
519 * ZFS to populate a range of page cache pages with data.
521 * NOTE: this function could be optimized to pre-allocate
522 * all pages in advance, drain exclusive busy on all of them,
523 * map them into contiguous KVA region and populate them
524 * in one single dmu_read() call.
527 mappedread_sf(znode_t *zp, int nbytes, zfs_uio_t *uio)
529 vnode_t *vp = ZTOV(zp);
530 objset_t *os = zp->z_zfsvfs->z_os;
539 ASSERT3U(zfs_uio_segflg(uio), ==, UIO_NOCOPY);
540 ASSERT3P(vp->v_mount, !=, NULL);
542 ASSERT3P(obj, !=, NULL);
543 ASSERT0(zfs_uio_offset(uio) & PAGEOFFSET);
545 zfs_vmobject_wlock_12(obj);
546 for (start = zfs_uio_offset(uio); len > 0; start += PAGESIZE) {
547 int bytes = MIN(PAGESIZE, len);
549 pp = vm_page_grab_unlocked(obj, OFF_TO_IDX(start),
550 VM_ALLOC_SBUSY | VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
551 if (vm_page_none_valid(pp)) {
552 zfs_vmobject_wunlock_12(obj);
553 va = zfs_map_page(pp, &sf);
554 error = dmu_read(os, zp->z_id, start, bytes, va,
556 if (bytes != PAGESIZE && error == 0)
557 memset(va + bytes, 0, PAGESIZE - bytes);
559 zfs_vmobject_wlock_12(obj);
560 #if __FreeBSD_version >= 1300081
563 vm_page_activate(pp);
564 vm_page_do_sunbusy(pp);
566 zfs_vmobject_wlock(obj);
567 if (!vm_page_wired(pp) && pp->valid == 0 &&
568 vm_page_busy_tryupgrade(pp))
572 zfs_vmobject_wunlock(obj);
575 vm_page_do_sunbusy(pp);
578 if (pp->wire_count == 0 && pp->valid == 0 &&
582 pp->valid = VM_PAGE_BITS_ALL;
583 vm_page_activate(pp);
588 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
589 vm_page_do_sunbusy(pp);
593 zfs_uio_advance(uio, bytes);
596 zfs_vmobject_wunlock_12(obj);
601 * When a file is memory mapped, we must keep the IO data synchronized
602 * between the DMU cache and the memory mapped pages. What this means:
604 * On Read: We "read" preferentially from memory mapped pages,
605 * else we default from the dmu buffer.
607 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
608 * the file is memory mapped.
611 mappedread(znode_t *zp, int nbytes, zfs_uio_t *uio)
613 vnode_t *vp = ZTOV(zp);
620 ASSERT3P(vp->v_mount, !=, NULL);
622 ASSERT3P(obj, !=, NULL);
624 start = zfs_uio_offset(uio);
625 off = start & PAGEOFFSET;
626 zfs_vmobject_wlock_12(obj);
627 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
629 uint64_t bytes = MIN(PAGESIZE - off, len);
631 if ((pp = page_hold(vp, start))) {
635 zfs_vmobject_wunlock_12(obj);
636 va = zfs_map_page(pp, &sf);
637 error = vn_io_fault_uiomove(va + off, bytes,
638 GET_UIO_STRUCT(uio));
640 zfs_vmobject_wlock_12(obj);
643 zfs_vmobject_wunlock_12(obj);
644 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
646 zfs_vmobject_wlock_12(obj);
653 zfs_vmobject_wunlock_12(obj);
658 zfs_write_simple(znode_t *zp, const void *data, size_t len,
659 loff_t pos, size_t *presid)
664 error = vn_rdwr(UIO_WRITE, ZTOV(zp), __DECONST(void *, data), len, pos,
665 UIO_SYSSPACE, IO_SYNC, kcred, NOCRED, &resid, curthread);
668 return (SET_ERROR(error));
669 } else if (presid == NULL) {
671 error = SET_ERROR(EIO);
680 zfs_zrele_async(znode_t *zp)
682 vnode_t *vp = ZTOV(zp);
683 objset_t *os = ITOZSB(vp)->z_os;
685 VN_RELE_ASYNC(vp, dsl_pool_zrele_taskq(dmu_objset_pool(os)));
689 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
694 error = vn_lock(*vpp, lkflags);
701 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
703 znode_t *zdp = VTOZ(dvp);
704 zfsvfs_t *zfsvfs __unused = zdp->z_zfsvfs;
708 if (zfsvfs->z_replay == B_FALSE)
709 ASSERT_VOP_LOCKED(dvp, __func__);
711 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
712 ASSERT3P(dvp, ==, vp);
714 ltype = lkflags & LK_TYPE_MASK;
715 if (ltype != VOP_ISLOCKED(dvp)) {
716 if (ltype == LK_EXCLUSIVE)
717 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
718 else /* if (ltype == LK_SHARED) */
719 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
722 * Relock for the "." case could leave us with
725 if (VN_IS_DOOMED(dvp)) {
727 return (SET_ERROR(ENOENT));
731 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
733 * Note that in this case, dvp is the child vnode, and we
734 * are looking up the parent vnode - exactly reverse from
735 * normal operation. Unlocking dvp requires some rather
736 * tricky unlock/relock dance to prevent mp from being freed;
737 * use vn_vget_ino_gen() which takes care of all that.
739 * XXX Note that there is a time window when both vnodes are
740 * unlocked. It is possible, although highly unlikely, that
741 * during that window the parent-child relationship between
742 * the vnodes may change, for example, get reversed.
743 * In that case we would have a wrong lock order for the vnodes.
744 * All other filesystems seem to ignore this problem, so we
746 * A potential solution could be implemented as follows:
747 * - using LK_NOWAIT when locking the second vnode and retrying
749 * - checking that the parent-child relationship still holds
750 * after locking both vnodes and retrying if it doesn't
752 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
755 error = vn_lock(vp, lkflags);
763 * Lookup an entry in a directory, or an extended attribute directory.
764 * If it exists, return a held vnode reference for it.
766 * IN: dvp - vnode of directory to search.
767 * nm - name of entry to lookup.
768 * pnp - full pathname to lookup [UNUSED].
769 * flags - LOOKUP_XATTR set if looking for an attribute.
770 * rdir - root directory vnode [UNUSED].
771 * cr - credentials of caller.
772 * ct - caller context
774 * OUT: vpp - vnode of located entry, NULL if not found.
776 * RETURN: 0 on success, error code on failure.
782 zfs_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp,
783 struct componentname *cnp, int nameiop, cred_t *cr, int flags,
786 znode_t *zdp = VTOZ(dvp);
788 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
789 #if __FreeBSD_version > 1300124
795 * Fast path lookup, however we must skip DNLC lookup
796 * for case folding or normalizing lookups because the
797 * DNLC code only stores the passed in name. This means
798 * creating 'a' and removing 'A' on a case insensitive
799 * file system would work, but DNLC still thinks 'a'
800 * exists and won't let you create it again on the next
801 * pass through fast path.
803 if (!(flags & LOOKUP_XATTR)) {
804 if (dvp->v_type != VDIR) {
805 return (SET_ERROR(ENOTDIR));
806 } else if (zdp->z_sa_hdl == NULL) {
807 return (SET_ERROR(EIO));
811 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp,
814 if ((error = zfs_enter_verify_zp(zfsvfs, zdp, FTAG)) != 0)
817 #if __FreeBSD_version > 1300124
818 dvp_seqc = vn_seqc_read_notmodify(dvp);
823 if (flags & LOOKUP_XATTR) {
825 * If the xattr property is off, refuse the lookup request.
827 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
828 zfs_exit(zfsvfs, FTAG);
829 return (SET_ERROR(EOPNOTSUPP));
833 * We don't allow recursive attributes..
834 * Maybe someday we will.
836 if (zdp->z_pflags & ZFS_XATTR) {
837 zfs_exit(zfsvfs, FTAG);
838 return (SET_ERROR(EINVAL));
841 if ((error = zfs_get_xattrdir(VTOZ(dvp), &zp, cr, flags))) {
842 zfs_exit(zfsvfs, FTAG);
848 * Do we have permission to get into attribute directory?
850 error = zfs_zaccess(zp, ACE_EXECUTE, 0, B_FALSE, cr, NULL);
855 zfs_exit(zfsvfs, FTAG);
860 * Check accessibility of directory if we're not coming in via
865 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
866 cnp->cn_flags &= ~NOEXECCHECK;
869 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr,
871 zfs_exit(zfsvfs, FTAG);
876 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
877 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
878 zfs_exit(zfsvfs, FTAG);
879 return (SET_ERROR(EILSEQ));
884 * First handle the special cases.
886 if ((cnp->cn_flags & ISDOTDOT) != 0) {
888 * If we are a snapshot mounted under .zfs, return
889 * the vp for the snapshot directory.
891 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
892 struct componentname cn;
896 zfs_exit(zfsvfs, FTAG);
897 ltype = VOP_ISLOCKED(dvp);
899 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
902 cn.cn_nameptr = "snapshot";
903 cn.cn_namelen = strlen(cn.cn_nameptr);
904 cn.cn_nameiop = cnp->cn_nameiop;
905 cn.cn_flags = cnp->cn_flags & ~ISDOTDOT;
906 cn.cn_lkflags = cnp->cn_lkflags;
907 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
910 vn_lock(dvp, ltype | LK_RETRY);
914 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
915 zfs_exit(zfsvfs, FTAG);
916 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
917 return (SET_ERROR(ENOTSUP));
918 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
923 * The loop is retry the lookup if the parent-child relationship
924 * changes during the dot-dot locking complexities.
929 error = zfs_dirlook(zdp, nm, &zp);
933 zfs_exit(zfsvfs, FTAG);
937 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
940 * If we've got a locking error, then the vnode
941 * got reclaimed because of a force unmount.
942 * We never enter doomed vnodes into the name cache.
948 if ((cnp->cn_flags & ISDOTDOT) == 0)
951 if ((error = zfs_enter(zfsvfs, FTAG)) != 0) {
956 if (zdp->z_sa_hdl == NULL) {
957 error = SET_ERROR(EIO);
959 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
960 &parent, sizeof (parent));
963 zfs_exit(zfsvfs, FTAG);
967 if (zp->z_id == parent) {
968 zfs_exit(zfsvfs, FTAG);
977 /* Translate errors and add SAVENAME when needed. */
978 if (cnp->cn_flags & ISLASTCN) {
982 if (error == ENOENT) {
984 #if __FreeBSD_version < 1400068
985 cnp->cn_flags |= SAVENAME;
991 #if __FreeBSD_version < 1400068
993 cnp->cn_flags |= SAVENAME;
999 #if __FreeBSD_version > 1300124
1000 if ((cnp->cn_flags & ISDOTDOT) != 0) {
1002 * FIXME: zfs_lookup_lock relocks vnodes and does nothing to
1003 * handle races. In particular different callers may end up
1004 * with different vnodes and will try to add conflicting
1005 * entries to the namecache.
1007 * While finding different result may be acceptable in face
1008 * of concurrent modification, adding conflicting entries
1009 * trips over an assert in the namecache.
1011 * Ultimately let an entry through once everything settles.
1013 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
1014 cnp->cn_flags &= ~MAKEENTRY;
1019 /* Insert name into cache (as non-existent) if appropriate. */
1020 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1021 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1022 cache_enter(dvp, NULL, cnp);
1024 /* Insert name into cache if appropriate. */
1025 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1026 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1027 if (!(cnp->cn_flags & ISLASTCN) ||
1028 (nameiop != DELETE && nameiop != RENAME)) {
1029 cache_enter(dvp, *vpp, cnp);
1037 * Attempt to create a new entry in a directory. If the entry
1038 * already exists, truncate the file if permissible, else return
1039 * an error. Return the vp of the created or trunc'd file.
1041 * IN: dvp - vnode of directory to put new file entry in.
1042 * name - name of new file entry.
1043 * vap - attributes of new file.
1044 * excl - flag indicating exclusive or non-exclusive mode.
1045 * mode - mode to open file with.
1046 * cr - credentials of caller.
1047 * flag - large file flag [UNUSED].
1048 * ct - caller context
1049 * vsecp - ACL to be set
1050 * mnt_ns - Unused on FreeBSD
1052 * OUT: vpp - vnode of created or trunc'd entry.
1054 * RETURN: 0 on success, error code on failure.
1057 * dvp - ctime|mtime updated if new entry created
1058 * vp - ctime|mtime always, atime if new
1061 zfs_create(znode_t *dzp, const char *name, vattr_t *vap, int excl, int mode,
1062 znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp, zidmap_t *mnt_ns)
1064 (void) excl, (void) mode, (void) flag;
1066 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1071 uid_t uid = crgetuid(cr);
1072 gid_t gid = crgetgid(cr);
1073 uint64_t projid = ZFS_DEFAULT_PROJID;
1074 zfs_acl_ids_t acl_ids;
1075 boolean_t fuid_dirtied;
1077 #ifdef DEBUG_VFS_LOCKS
1078 vnode_t *dvp = ZTOV(dzp);
1082 * If we have an ephemeral id, ACL, or XVATTR then
1083 * make sure file system is at proper version
1085 if (zfsvfs->z_use_fuids == B_FALSE &&
1086 (vsecp || (vap->va_mask & AT_XVATTR) ||
1087 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1088 return (SET_ERROR(EINVAL));
1090 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1093 zilog = zfsvfs->z_log;
1095 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1096 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1097 zfs_exit(zfsvfs, FTAG);
1098 return (SET_ERROR(EILSEQ));
1101 if (vap->va_mask & AT_XVATTR) {
1102 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1103 crgetuid(cr), cr, vap->va_type)) != 0) {
1104 zfs_exit(zfsvfs, FTAG);
1111 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1112 vap->va_mode &= ~S_ISVTX;
1114 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1116 zfs_exit(zfsvfs, FTAG);
1119 ASSERT3P(zp, ==, NULL);
1122 * Create a new file object and update the directory
1125 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
1130 * We only support the creation of regular files in
1131 * extended attribute directories.
1134 if ((dzp->z_pflags & ZFS_XATTR) &&
1135 (vap->va_type != VREG)) {
1136 error = SET_ERROR(EINVAL);
1140 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1141 cr, vsecp, &acl_ids, NULL)) != 0)
1144 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
1145 projid = zfs_inherit_projid(dzp);
1146 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1147 zfs_acl_ids_free(&acl_ids);
1148 error = SET_ERROR(EDQUOT);
1152 getnewvnode_reserve_();
1154 tx = dmu_tx_create(os);
1156 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1157 ZFS_SA_BASE_ATTR_SIZE);
1159 fuid_dirtied = zfsvfs->z_fuid_dirty;
1161 zfs_fuid_txhold(zfsvfs, tx);
1162 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1163 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1164 if (!zfsvfs->z_use_sa &&
1165 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1166 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1167 0, acl_ids.z_aclp->z_acl_bytes);
1169 error = dmu_tx_assign(tx, TXG_WAIT);
1171 zfs_acl_ids_free(&acl_ids);
1173 getnewvnode_drop_reserve();
1174 zfs_exit(zfsvfs, FTAG);
1177 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1179 zfs_fuid_sync(zfsvfs, tx);
1181 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1182 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1183 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1184 vsecp, acl_ids.z_fuidp, vap);
1185 zfs_acl_ids_free(&acl_ids);
1188 getnewvnode_drop_reserve();
1196 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1197 zil_commit(zilog, 0);
1199 zfs_exit(zfsvfs, FTAG);
1204 * Remove an entry from a directory.
1206 * IN: dvp - vnode of directory to remove entry from.
1207 * name - name of entry to remove.
1208 * cr - credentials of caller.
1209 * ct - caller context
1210 * flags - case flags
1212 * RETURN: 0 on success, error code on failure.
1216 * vp - ctime (if nlink > 0)
1219 zfs_remove_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1221 znode_t *dzp = VTOZ(dvp);
1224 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1234 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1237 if ((error = zfs_verify_zp(zp)) != 0) {
1238 zfs_exit(zfsvfs, FTAG);
1241 zilog = zfsvfs->z_log;
1246 if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) {
1251 * Need to use rmdir for removing directories.
1253 if (vp->v_type == VDIR) {
1254 error = SET_ERROR(EPERM);
1258 vnevent_remove(vp, dvp, name, ct);
1262 /* are there any extended attributes? */
1263 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1264 &xattr_obj, sizeof (xattr_obj));
1265 if (error == 0 && xattr_obj) {
1266 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1271 * We may delete the znode now, or we may put it in the unlinked set;
1272 * it depends on whether we're the last link, and on whether there are
1273 * other holds on the vnode. So we dmu_tx_hold() the right things to
1274 * allow for either case.
1276 tx = dmu_tx_create(zfsvfs->z_os);
1277 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1278 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1279 zfs_sa_upgrade_txholds(tx, zp);
1280 zfs_sa_upgrade_txholds(tx, dzp);
1283 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1284 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1287 /* charge as an update -- would be nice not to charge at all */
1288 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1291 * Mark this transaction as typically resulting in a net free of space
1293 dmu_tx_mark_netfree(tx);
1295 error = dmu_tx_assign(tx, TXG_WAIT);
1298 zfs_exit(zfsvfs, FTAG);
1303 * Remove the directory entry.
1305 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
1313 zfs_unlinked_add(zp, tx);
1314 vp->v_vflag |= VV_NOSYNC;
1316 /* XXX check changes to linux vnops */
1318 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
1326 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1327 zil_commit(zilog, 0);
1330 zfs_exit(zfsvfs, FTAG);
1336 zfs_lookup_internal(znode_t *dzp, const char *name, vnode_t **vpp,
1337 struct componentname *cnp, int nameiop)
1339 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1342 cnp->cn_nameptr = __DECONST(char *, name);
1343 cnp->cn_namelen = strlen(name);
1344 cnp->cn_nameiop = nameiop;
1345 cnp->cn_flags = ISLASTCN;
1346 #if __FreeBSD_version < 1400068
1347 cnp->cn_flags |= SAVENAME;
1349 cnp->cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
1350 cnp->cn_cred = kcred;
1351 #if __FreeBSD_version < 1400037
1352 cnp->cn_thread = curthread;
1355 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay) {
1356 struct vop_lookup_args a;
1358 a.a_gen.a_desc = &vop_lookup_desc;
1359 a.a_dvp = ZTOV(dzp);
1362 error = vfs_cache_lookup(&a);
1364 error = zfs_lookup(ZTOV(dzp), name, vpp, cnp, nameiop, kcred, 0,
1369 printf("got error %d on name %s on op %d\n", error, name,
1378 zfs_remove(znode_t *dzp, const char *name, cred_t *cr, int flags)
1382 struct componentname cn;
1384 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1387 error = zfs_remove_(ZTOV(dzp), vp, name, cr);
1392 * Create a new directory and insert it into dvp using the name
1393 * provided. Return a pointer to the inserted directory.
1395 * IN: dvp - vnode of directory to add subdir to.
1396 * dirname - name of new directory.
1397 * vap - attributes of new directory.
1398 * cr - credentials of caller.
1399 * ct - caller context
1400 * flags - case flags
1401 * vsecp - ACL to be set
1402 * mnt_ns - Unused on FreeBSD
1404 * OUT: vpp - vnode of created directory.
1406 * RETURN: 0 on success, error code on failure.
1409 * dvp - ctime|mtime updated
1410 * vp - ctime|mtime|atime updated
1413 zfs_mkdir(znode_t *dzp, const char *dirname, vattr_t *vap, znode_t **zpp,
1414 cred_t *cr, int flags, vsecattr_t *vsecp, zidmap_t *mnt_ns)
1416 (void) flags, (void) vsecp;
1418 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1423 uid_t uid = crgetuid(cr);
1424 gid_t gid = crgetgid(cr);
1425 zfs_acl_ids_t acl_ids;
1426 boolean_t fuid_dirtied;
1428 ASSERT3U(vap->va_type, ==, VDIR);
1431 * If we have an ephemeral id, ACL, or XVATTR then
1432 * make sure file system is at proper version
1434 if (zfsvfs->z_use_fuids == B_FALSE &&
1435 ((vap->va_mask & AT_XVATTR) ||
1436 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1437 return (SET_ERROR(EINVAL));
1439 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1441 zilog = zfsvfs->z_log;
1443 if (dzp->z_pflags & ZFS_XATTR) {
1444 zfs_exit(zfsvfs, FTAG);
1445 return (SET_ERROR(EINVAL));
1448 if (zfsvfs->z_utf8 && u8_validate(dirname,
1449 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1450 zfs_exit(zfsvfs, FTAG);
1451 return (SET_ERROR(EILSEQ));
1454 if (vap->va_mask & AT_XVATTR) {
1455 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1456 crgetuid(cr), cr, vap->va_type)) != 0) {
1457 zfs_exit(zfsvfs, FTAG);
1462 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1463 NULL, &acl_ids, NULL)) != 0) {
1464 zfs_exit(zfsvfs, FTAG);
1469 * First make sure the new directory doesn't exist.
1471 * Existence is checked first to make sure we don't return
1472 * EACCES instead of EEXIST which can cause some applications
1477 if ((error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW))) {
1478 zfs_acl_ids_free(&acl_ids);
1479 zfs_exit(zfsvfs, FTAG);
1482 ASSERT3P(zp, ==, NULL);
1484 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr,
1486 zfs_acl_ids_free(&acl_ids);
1487 zfs_exit(zfsvfs, FTAG);
1491 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
1492 zfs_acl_ids_free(&acl_ids);
1493 zfs_exit(zfsvfs, FTAG);
1494 return (SET_ERROR(EDQUOT));
1498 * Add a new entry to the directory.
1500 getnewvnode_reserve_();
1501 tx = dmu_tx_create(zfsvfs->z_os);
1502 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1503 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1504 fuid_dirtied = zfsvfs->z_fuid_dirty;
1506 zfs_fuid_txhold(zfsvfs, tx);
1507 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1508 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1509 acl_ids.z_aclp->z_acl_bytes);
1512 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1513 ZFS_SA_BASE_ATTR_SIZE);
1515 error = dmu_tx_assign(tx, TXG_WAIT);
1517 zfs_acl_ids_free(&acl_ids);
1519 getnewvnode_drop_reserve();
1520 zfs_exit(zfsvfs, FTAG);
1527 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1530 zfs_fuid_sync(zfsvfs, tx);
1533 * Now put new name in parent dir.
1535 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
1539 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
1540 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
1541 acl_ids.z_fuidp, vap);
1543 zfs_acl_ids_free(&acl_ids);
1547 getnewvnode_drop_reserve();
1549 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1550 zil_commit(zilog, 0);
1552 zfs_exit(zfsvfs, FTAG);
1556 #if __FreeBSD_version < 1300124
1558 cache_vop_rmdir(struct vnode *dvp, struct vnode *vp)
1567 * Remove a directory subdir entry. If the current working
1568 * directory is the same as the subdir to be removed, the
1571 * IN: dvp - vnode of directory to remove from.
1572 * name - name of directory to be removed.
1573 * cwd - vnode of current working directory.
1574 * cr - credentials of caller.
1575 * ct - caller context
1576 * flags - case flags
1578 * RETURN: 0 on success, error code on failure.
1581 * dvp - ctime|mtime updated
1584 zfs_rmdir_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1586 znode_t *dzp = VTOZ(dvp);
1587 znode_t *zp = VTOZ(vp);
1588 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1593 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1595 if ((error = zfs_verify_zp(zp)) != 0) {
1596 zfs_exit(zfsvfs, FTAG);
1599 zilog = zfsvfs->z_log;
1602 if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) {
1606 if (vp->v_type != VDIR) {
1607 error = SET_ERROR(ENOTDIR);
1611 vnevent_rmdir(vp, dvp, name, ct);
1613 tx = dmu_tx_create(zfsvfs->z_os);
1614 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1615 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1616 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1617 zfs_sa_upgrade_txholds(tx, zp);
1618 zfs_sa_upgrade_txholds(tx, dzp);
1619 dmu_tx_mark_netfree(tx);
1620 error = dmu_tx_assign(tx, TXG_WAIT);
1623 zfs_exit(zfsvfs, FTAG);
1627 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
1630 uint64_t txtype = TX_RMDIR;
1631 zfs_log_remove(zilog, tx, txtype, dzp, name,
1632 ZFS_NO_OBJECT, B_FALSE);
1637 if (zfsvfs->z_use_namecache)
1638 cache_vop_rmdir(dvp, vp);
1640 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1641 zil_commit(zilog, 0);
1643 zfs_exit(zfsvfs, FTAG);
1648 zfs_rmdir(znode_t *dzp, const char *name, znode_t *cwd, cred_t *cr, int flags)
1650 struct componentname cn;
1654 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1657 error = zfs_rmdir_(ZTOV(dzp), vp, name, cr);
1663 * Read as many directory entries as will fit into the provided
1664 * buffer from the given directory cursor position (specified in
1665 * the uio structure).
1667 * IN: vp - vnode of directory to read.
1668 * uio - structure supplying read location, range info,
1669 * and return buffer.
1670 * cr - credentials of caller.
1671 * ct - caller context
1673 * OUT: uio - updated offset and range, buffer filled.
1674 * eofp - set to true if end-of-file detected.
1675 * ncookies- number of entries in cookies
1676 * cookies - offsets to directory entries
1678 * RETURN: 0 on success, error code on failure.
1681 * vp - atime updated
1683 * Note that the low 4 bits of the cookie returned by zap is always zero.
1684 * This allows us to use the low range for "special" directory entries:
1685 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1686 * we use the offset 2 for the '.zfs' directory.
1689 zfs_readdir(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, int *eofp,
1690 int *ncookies, cookie_t **cookies)
1692 znode_t *zp = VTOZ(vp);
1695 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1700 zap_attribute_t zap;
1701 uint_t bytes_wanted;
1702 uint64_t offset; /* must be unsigned; checks for < 1 */
1710 cookie_t *cooks = NULL;
1712 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1715 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1716 &parent, sizeof (parent))) != 0) {
1717 zfs_exit(zfsvfs, FTAG);
1722 * If we are not given an eof variable,
1729 * Check for valid iov_len.
1731 if (GET_UIO_STRUCT(uio)->uio_iov->iov_len <= 0) {
1732 zfs_exit(zfsvfs, FTAG);
1733 return (SET_ERROR(EINVAL));
1737 * Quit if directory has been removed (posix)
1739 if ((*eofp = zp->z_unlinked) != 0) {
1740 zfs_exit(zfsvfs, FTAG);
1746 offset = zfs_uio_offset(uio);
1747 prefetch = zp->z_zn_prefetch;
1750 * Initialize the iterator cursor.
1754 * Start iteration from the beginning of the directory.
1756 zap_cursor_init(&zc, os, zp->z_id);
1759 * The offset is a serialized cursor.
1761 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1765 * Get space to change directory entries into fs independent format.
1767 iovp = GET_UIO_STRUCT(uio)->uio_iov;
1768 bytes_wanted = iovp->iov_len;
1769 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1) {
1770 bufsize = bytes_wanted;
1771 outbuf = kmem_alloc(bufsize, KM_SLEEP);
1772 odp = (struct dirent64 *)outbuf;
1774 bufsize = bytes_wanted;
1776 odp = (struct dirent64 *)iovp->iov_base;
1779 if (ncookies != NULL) {
1781 * Minimum entry size is dirent size and 1 byte for a file name.
1783 ncooks = zfs_uio_resid(uio) / (sizeof (struct dirent) -
1784 sizeof (((struct dirent *)NULL)->d_name) + 1);
1785 cooks = malloc(ncooks * sizeof (*cooks), M_TEMP, M_WAITOK);
1791 * Transform to file-system independent format
1794 while (outcount < bytes_wanted) {
1797 off64_t *next = NULL;
1800 * Special case `.', `..', and `.zfs'.
1803 (void) strcpy(zap.za_name, ".");
1804 zap.za_normalization_conflict = 0;
1807 } else if (offset == 1) {
1808 (void) strcpy(zap.za_name, "..");
1809 zap.za_normalization_conflict = 0;
1812 } else if (offset == 2 && zfs_show_ctldir(zp)) {
1813 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1814 zap.za_normalization_conflict = 0;
1815 objnum = ZFSCTL_INO_ROOT;
1821 if ((error = zap_cursor_retrieve(&zc, &zap))) {
1822 if ((*eofp = (error == ENOENT)) != 0)
1828 if (zap.za_integer_length != 8 ||
1829 zap.za_num_integers != 1) {
1830 cmn_err(CE_WARN, "zap_readdir: bad directory "
1831 "entry, obj = %lld, offset = %lld\n",
1832 (u_longlong_t)zp->z_id,
1833 (u_longlong_t)offset);
1834 error = SET_ERROR(ENXIO);
1838 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
1840 * MacOS X can extract the object type here such as:
1841 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1843 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1846 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1849 * Will this entry fit in the buffer?
1851 if (outcount + reclen > bufsize) {
1853 * Did we manage to fit anything in the buffer?
1856 error = SET_ERROR(EINVAL);
1864 odp->d_ino = objnum;
1865 odp->d_reclen = reclen;
1866 odp->d_namlen = strlen(zap.za_name);
1867 /* NOTE: d_off is the offset for the *next* entry. */
1869 strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
1871 dirent_terminate(odp);
1872 odp = (dirent64_t *)((intptr_t)odp + reclen);
1876 ASSERT3S(outcount, <=, bufsize);
1879 dmu_prefetch_dnode(os, objnum, ZIO_PRIORITY_SYNC_READ);
1882 * Move to the next entry, fill in the previous offset.
1884 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1885 zap_cursor_advance(&zc);
1886 offset = zap_cursor_serialize(&zc);
1891 /* Fill the offset right after advancing the cursor. */
1894 if (cooks != NULL) {
1897 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
1900 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1902 /* Subtract unused cookies */
1903 if (ncookies != NULL)
1904 *ncookies -= ncooks;
1906 if (zfs_uio_segflg(uio) == UIO_SYSSPACE && zfs_uio_iovcnt(uio) == 1) {
1907 iovp->iov_base += outcount;
1908 iovp->iov_len -= outcount;
1909 zfs_uio_resid(uio) -= outcount;
1911 zfs_uiomove(outbuf, (long)outcount, UIO_READ, uio))) {
1913 * Reset the pointer.
1915 offset = zfs_uio_offset(uio);
1919 zap_cursor_fini(&zc);
1920 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1)
1921 kmem_free(outbuf, bufsize);
1923 if (error == ENOENT)
1926 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1928 zfs_uio_setoffset(uio, offset);
1929 zfs_exit(zfsvfs, FTAG);
1930 if (error != 0 && cookies != NULL) {
1931 free(*cookies, M_TEMP);
1939 * Get the requested file attributes and place them in the provided
1942 * IN: vp - vnode of file.
1943 * vap - va_mask identifies requested attributes.
1944 * If AT_XVATTR set, then optional attrs are requested
1945 * flags - ATTR_NOACLCHECK (CIFS server context)
1946 * cr - credentials of caller.
1948 * OUT: vap - attribute values.
1950 * RETURN: 0 (always succeeds).
1953 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1955 znode_t *zp = VTOZ(vp);
1956 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1959 u_longlong_t nblocks;
1960 uint64_t mtime[2], ctime[2], crtime[2], rdev;
1961 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
1962 xoptattr_t *xoap = NULL;
1963 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
1964 sa_bulk_attr_t bulk[4];
1967 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1970 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
1972 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
1973 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
1974 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
1975 if (vp->v_type == VBLK || vp->v_type == VCHR)
1976 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1979 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
1980 zfs_exit(zfsvfs, FTAG);
1985 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1986 * Also, if we are the owner don't bother, since owner should
1987 * always be allowed to read basic attributes of file.
1989 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
1990 (vap->va_uid != crgetuid(cr))) {
1991 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
1992 skipaclchk, cr, NULL))) {
1993 zfs_exit(zfsvfs, FTAG);
1999 * Return all attributes. It's cheaper to provide the answer
2000 * than to determine whether we were asked the question.
2003 vap->va_type = IFTOVT(zp->z_mode);
2004 vap->va_mode = zp->z_mode & ~S_IFMT;
2006 vap->va_nodeid = zp->z_id;
2007 vap->va_nlink = zp->z_links;
2008 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp) &&
2009 zp->z_links < ZFS_LINK_MAX)
2011 vap->va_size = zp->z_size;
2012 if (vp->v_type == VBLK || vp->v_type == VCHR)
2013 vap->va_rdev = zfs_cmpldev(rdev);
2014 vap->va_gen = zp->z_gen;
2015 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2016 vap->va_filerev = zp->z_seq;
2019 * Add in any requested optional attributes and the create time.
2020 * Also set the corresponding bits in the returned attribute bitmap.
2022 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2023 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2025 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2026 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2029 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2030 xoap->xoa_readonly =
2031 ((zp->z_pflags & ZFS_READONLY) != 0);
2032 XVA_SET_RTN(xvap, XAT_READONLY);
2035 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2037 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2038 XVA_SET_RTN(xvap, XAT_SYSTEM);
2041 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2043 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2044 XVA_SET_RTN(xvap, XAT_HIDDEN);
2047 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2048 xoap->xoa_nounlink =
2049 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2050 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2053 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2054 xoap->xoa_immutable =
2055 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2056 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2059 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2060 xoap->xoa_appendonly =
2061 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2062 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2065 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2067 ((zp->z_pflags & ZFS_NODUMP) != 0);
2068 XVA_SET_RTN(xvap, XAT_NODUMP);
2071 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2073 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2074 XVA_SET_RTN(xvap, XAT_OPAQUE);
2077 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2078 xoap->xoa_av_quarantined =
2079 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2080 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2083 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2084 xoap->xoa_av_modified =
2085 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2086 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2089 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2090 vp->v_type == VREG) {
2091 zfs_sa_get_scanstamp(zp, xvap);
2094 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2095 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2096 XVA_SET_RTN(xvap, XAT_REPARSE);
2098 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2099 xoap->xoa_generation = zp->z_gen;
2100 XVA_SET_RTN(xvap, XAT_GEN);
2103 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2105 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2106 XVA_SET_RTN(xvap, XAT_OFFLINE);
2109 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2111 ((zp->z_pflags & ZFS_SPARSE) != 0);
2112 XVA_SET_RTN(xvap, XAT_SPARSE);
2115 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2116 xoap->xoa_projinherit =
2117 ((zp->z_pflags & ZFS_PROJINHERIT) != 0);
2118 XVA_SET_RTN(xvap, XAT_PROJINHERIT);
2121 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2122 xoap->xoa_projid = zp->z_projid;
2123 XVA_SET_RTN(xvap, XAT_PROJID);
2127 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2128 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2129 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2130 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2133 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2134 vap->va_blksize = blksize;
2135 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2137 if (zp->z_blksz == 0) {
2139 * Block size hasn't been set; suggest maximal I/O transfers.
2141 vap->va_blksize = zfsvfs->z_max_blksz;
2144 zfs_exit(zfsvfs, FTAG);
2149 * Set the file attributes to the values contained in the
2152 * IN: zp - znode of file to be modified.
2153 * vap - new attribute values.
2154 * If AT_XVATTR set, then optional attrs are being set
2155 * flags - ATTR_UTIME set if non-default time values provided.
2156 * - ATTR_NOACLCHECK (CIFS context only).
2157 * cr - credentials of caller.
2158 * mnt_ns - Unused on FreeBSD
2160 * RETURN: 0 on success, error code on failure.
2163 * vp - ctime updated, mtime updated if size changed.
2166 zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr, zidmap_t *mnt_ns)
2168 vnode_t *vp = ZTOV(zp);
2169 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2175 uint_t mask = vap->va_mask;
2176 uint_t saved_mask = 0;
2177 uint64_t saved_mode;
2180 uint64_t new_uid, new_gid;
2182 uint64_t mtime[2], ctime[2];
2183 uint64_t projid = ZFS_INVALID_PROJID;
2185 int need_policy = FALSE;
2187 zfs_fuid_info_t *fuidp = NULL;
2188 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2191 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2192 boolean_t fuid_dirtied = B_FALSE;
2193 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2194 int count = 0, xattr_count = 0;
2199 if (mask & AT_NOSET)
2200 return (SET_ERROR(EINVAL));
2202 if ((err = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
2206 zilog = zfsvfs->z_log;
2209 * Make sure that if we have ephemeral uid/gid or xvattr specified
2210 * that file system is at proper version level
2213 if (zfsvfs->z_use_fuids == B_FALSE &&
2214 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2215 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2216 (mask & AT_XVATTR))) {
2217 zfs_exit(zfsvfs, FTAG);
2218 return (SET_ERROR(EINVAL));
2221 if (mask & AT_SIZE && vp->v_type == VDIR) {
2222 zfs_exit(zfsvfs, FTAG);
2223 return (SET_ERROR(EISDIR));
2226 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2227 zfs_exit(zfsvfs, FTAG);
2228 return (SET_ERROR(EINVAL));
2232 * If this is an xvattr_t, then get a pointer to the structure of
2233 * optional attributes. If this is NULL, then we have a vattr_t.
2235 xoap = xva_getxoptattr(xvap);
2237 xva_init(&tmpxvattr);
2240 * Immutable files can only alter immutable bit and atime
2242 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2243 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2244 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2245 zfs_exit(zfsvfs, FTAG);
2246 return (SET_ERROR(EPERM));
2250 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2254 * Verify timestamps doesn't overflow 32 bits.
2255 * ZFS can handle large timestamps, but 32bit syscalls can't
2256 * handle times greater than 2039. This check should be removed
2257 * once large timestamps are fully supported.
2259 if (mask & (AT_ATIME | AT_MTIME)) {
2260 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2261 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2262 zfs_exit(zfsvfs, FTAG);
2263 return (SET_ERROR(EOVERFLOW));
2266 if (xoap != NULL && (mask & AT_XVATTR)) {
2267 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2268 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2269 zfs_exit(zfsvfs, FTAG);
2270 return (SET_ERROR(EOVERFLOW));
2273 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2274 if (!dmu_objset_projectquota_enabled(os) ||
2275 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode))) {
2276 zfs_exit(zfsvfs, FTAG);
2277 return (SET_ERROR(EOPNOTSUPP));
2280 projid = xoap->xoa_projid;
2281 if (unlikely(projid == ZFS_INVALID_PROJID)) {
2282 zfs_exit(zfsvfs, FTAG);
2283 return (SET_ERROR(EINVAL));
2286 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
2287 projid = ZFS_INVALID_PROJID;
2292 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
2293 (xoap->xoa_projinherit !=
2294 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) &&
2295 (!dmu_objset_projectquota_enabled(os) ||
2296 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode)))) {
2297 zfs_exit(zfsvfs, FTAG);
2298 return (SET_ERROR(EOPNOTSUPP));
2305 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2306 zfs_exit(zfsvfs, FTAG);
2307 return (SET_ERROR(EROFS));
2311 * First validate permissions
2314 if (mask & AT_SIZE) {
2316 * XXX - Note, we are not providing any open
2317 * mode flags here (like FNDELAY), so we may
2318 * block if there are locks present... this
2319 * should be addressed in openat().
2321 /* XXX - would it be OK to generate a log record here? */
2322 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2324 zfs_exit(zfsvfs, FTAG);
2329 if (mask & (AT_ATIME|AT_MTIME) ||
2330 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2331 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2332 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2333 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2334 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2335 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2336 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2337 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2338 skipaclchk, cr, mnt_ns);
2341 if (mask & (AT_UID|AT_GID)) {
2342 int idmask = (mask & (AT_UID|AT_GID));
2347 * NOTE: even if a new mode is being set,
2348 * we may clear S_ISUID/S_ISGID bits.
2351 if (!(mask & AT_MODE))
2352 vap->va_mode = zp->z_mode;
2355 * Take ownership or chgrp to group we are a member of
2358 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2359 take_group = (mask & AT_GID) &&
2360 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2363 * If both AT_UID and AT_GID are set then take_owner and
2364 * take_group must both be set in order to allow taking
2367 * Otherwise, send the check through secpolicy_vnode_setattr()
2371 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2372 ((idmask == AT_UID) && take_owner) ||
2373 ((idmask == AT_GID) && take_group)) {
2374 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2375 skipaclchk, cr, mnt_ns) == 0) {
2377 * Remove setuid/setgid for non-privileged users
2379 secpolicy_setid_clear(vap, vp, cr);
2380 trim_mask = (mask & (AT_UID|AT_GID));
2389 oldva.va_mode = zp->z_mode;
2390 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2391 if (mask & AT_XVATTR) {
2393 * Update xvattr mask to include only those attributes
2394 * that are actually changing.
2396 * the bits will be restored prior to actually setting
2397 * the attributes so the caller thinks they were set.
2399 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2400 if (xoap->xoa_appendonly !=
2401 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2404 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2405 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2409 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2410 if (xoap->xoa_projinherit !=
2411 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
2414 XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
2415 XVA_SET_REQ(&tmpxvattr, XAT_PROJINHERIT);
2419 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2420 if (xoap->xoa_nounlink !=
2421 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2424 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2425 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2429 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2430 if (xoap->xoa_immutable !=
2431 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2434 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2435 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2439 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2440 if (xoap->xoa_nodump !=
2441 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2444 XVA_CLR_REQ(xvap, XAT_NODUMP);
2445 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2449 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2450 if (xoap->xoa_av_modified !=
2451 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2454 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2455 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2459 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2460 if ((vp->v_type != VREG &&
2461 xoap->xoa_av_quarantined) ||
2462 xoap->xoa_av_quarantined !=
2463 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2466 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2467 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2471 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2472 zfs_exit(zfsvfs, FTAG);
2473 return (SET_ERROR(EPERM));
2476 if (need_policy == FALSE &&
2477 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2478 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2483 if (mask & AT_MODE) {
2484 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr,
2486 err = secpolicy_setid_setsticky_clear(vp, vap,
2489 zfs_exit(zfsvfs, FTAG);
2492 trim_mask |= AT_MODE;
2500 * If trim_mask is set then take ownership
2501 * has been granted or write_acl is present and user
2502 * has the ability to modify mode. In that case remove
2503 * UID|GID and or MODE from mask so that
2504 * secpolicy_vnode_setattr() doesn't revoke it.
2508 saved_mask = vap->va_mask;
2509 vap->va_mask &= ~trim_mask;
2510 if (trim_mask & AT_MODE) {
2512 * Save the mode, as secpolicy_vnode_setattr()
2513 * will overwrite it with ova.va_mode.
2515 saved_mode = vap->va_mode;
2518 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2519 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2521 zfs_exit(zfsvfs, FTAG);
2526 vap->va_mask |= saved_mask;
2527 if (trim_mask & AT_MODE) {
2529 * Recover the mode after
2530 * secpolicy_vnode_setattr().
2532 vap->va_mode = saved_mode;
2538 * secpolicy_vnode_setattr, or take ownership may have
2541 mask = vap->va_mask;
2543 if ((mask & (AT_UID | AT_GID)) || projid != ZFS_INVALID_PROJID) {
2544 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2545 &xattr_obj, sizeof (xattr_obj));
2547 if (err == 0 && xattr_obj) {
2548 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
2550 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
2552 vrele(ZTOV(attrzp));
2557 if (mask & AT_UID) {
2558 new_uid = zfs_fuid_create(zfsvfs,
2559 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2560 if (new_uid != zp->z_uid &&
2561 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
2565 err = SET_ERROR(EDQUOT);
2570 if (mask & AT_GID) {
2571 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2572 cr, ZFS_GROUP, &fuidp);
2573 if (new_gid != zp->z_gid &&
2574 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
2578 err = SET_ERROR(EDQUOT);
2583 if (projid != ZFS_INVALID_PROJID &&
2584 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
2587 err = SET_ERROR(EDQUOT);
2591 tx = dmu_tx_create(os);
2593 if (mask & AT_MODE) {
2594 uint64_t pmode = zp->z_mode;
2596 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2598 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
2599 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
2600 err = SET_ERROR(EPERM);
2604 if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)))
2607 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2609 * Are we upgrading ACL from old V0 format
2612 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
2613 zfs_znode_acl_version(zp) ==
2614 ZFS_ACL_VERSION_INITIAL) {
2615 dmu_tx_hold_free(tx, acl_obj, 0,
2617 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2618 0, aclp->z_acl_bytes);
2620 dmu_tx_hold_write(tx, acl_obj, 0,
2623 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2624 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2625 0, aclp->z_acl_bytes);
2627 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2629 if (((mask & AT_XVATTR) &&
2630 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
2631 (projid != ZFS_INVALID_PROJID &&
2632 !(zp->z_pflags & ZFS_PROJID)))
2633 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2635 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2639 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2642 fuid_dirtied = zfsvfs->z_fuid_dirty;
2644 zfs_fuid_txhold(zfsvfs, tx);
2646 zfs_sa_upgrade_txholds(tx, zp);
2648 err = dmu_tx_assign(tx, TXG_WAIT);
2654 * Set each attribute requested.
2655 * We group settings according to the locks they need to acquire.
2657 * Note: you cannot set ctime directly, although it will be
2658 * updated as a side-effect of calling this function.
2661 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) {
2663 * For the existed object that is upgraded from old system,
2664 * its on-disk layout has no slot for the project ID attribute.
2665 * But quota accounting logic needs to access related slots by
2666 * offset directly. So we need to adjust old objects' layout
2667 * to make the project ID to some unified and fixed offset.
2670 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid);
2672 err = sa_add_projid(zp->z_sa_hdl, tx, projid);
2674 if (unlikely(err == EEXIST))
2679 projid = ZFS_INVALID_PROJID;
2682 if (mask & (AT_UID|AT_GID|AT_MODE))
2683 mutex_enter(&zp->z_acl_lock);
2685 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
2686 &zp->z_pflags, sizeof (zp->z_pflags));
2689 if (mask & (AT_UID|AT_GID|AT_MODE))
2690 mutex_enter(&attrzp->z_acl_lock);
2691 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2692 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
2693 sizeof (attrzp->z_pflags));
2694 if (projid != ZFS_INVALID_PROJID) {
2695 attrzp->z_projid = projid;
2696 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2697 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid,
2698 sizeof (attrzp->z_projid));
2702 if (mask & (AT_UID|AT_GID)) {
2704 if (mask & AT_UID) {
2705 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2706 &new_uid, sizeof (new_uid));
2707 zp->z_uid = new_uid;
2709 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2710 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
2712 attrzp->z_uid = new_uid;
2716 if (mask & AT_GID) {
2717 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
2718 NULL, &new_gid, sizeof (new_gid));
2719 zp->z_gid = new_gid;
2721 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2722 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
2724 attrzp->z_gid = new_gid;
2727 if (!(mask & AT_MODE)) {
2728 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
2729 NULL, &new_mode, sizeof (new_mode));
2730 new_mode = zp->z_mode;
2732 err = zfs_acl_chown_setattr(zp);
2735 vn_seqc_write_begin(ZTOV(attrzp));
2736 err = zfs_acl_chown_setattr(attrzp);
2737 vn_seqc_write_end(ZTOV(attrzp));
2742 if (mask & AT_MODE) {
2743 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
2744 &new_mode, sizeof (new_mode));
2745 zp->z_mode = new_mode;
2746 ASSERT3P(aclp, !=, NULL);
2747 err = zfs_aclset_common(zp, aclp, cr, tx);
2749 if (zp->z_acl_cached)
2750 zfs_acl_free(zp->z_acl_cached);
2751 zp->z_acl_cached = aclp;
2756 if (mask & AT_ATIME) {
2757 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2758 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
2759 &zp->z_atime, sizeof (zp->z_atime));
2762 if (mask & AT_MTIME) {
2763 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2764 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
2765 mtime, sizeof (mtime));
2768 if (projid != ZFS_INVALID_PROJID) {
2769 zp->z_projid = projid;
2770 SA_ADD_BULK_ATTR(bulk, count,
2771 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid,
2772 sizeof (zp->z_projid));
2775 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2776 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
2777 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
2778 NULL, mtime, sizeof (mtime));
2779 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2780 &ctime, sizeof (ctime));
2781 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
2782 } else if (mask != 0) {
2783 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2784 &ctime, sizeof (ctime));
2785 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime);
2787 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2788 SA_ZPL_CTIME(zfsvfs), NULL,
2789 &ctime, sizeof (ctime));
2790 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2796 * Do this after setting timestamps to prevent timestamp
2797 * update from toggling bit
2800 if (xoap && (mask & AT_XVATTR)) {
2802 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
2803 xoap->xoa_createtime = vap->va_birthtime;
2805 * restore trimmed off masks
2806 * so that return masks can be set for caller.
2809 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
2810 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2812 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
2813 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2815 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
2816 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2818 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
2819 XVA_SET_REQ(xvap, XAT_NODUMP);
2821 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
2822 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2824 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
2825 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2827 if (XVA_ISSET_REQ(&tmpxvattr, XAT_PROJINHERIT)) {
2828 XVA_SET_REQ(xvap, XAT_PROJINHERIT);
2831 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2832 ASSERT3S(vp->v_type, ==, VREG);
2834 zfs_xvattr_set(zp, xvap, tx);
2838 zfs_fuid_sync(zfsvfs, tx);
2841 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2843 if (mask & (AT_UID|AT_GID|AT_MODE))
2844 mutex_exit(&zp->z_acl_lock);
2847 if (mask & (AT_UID|AT_GID|AT_MODE))
2848 mutex_exit(&attrzp->z_acl_lock);
2851 if (err == 0 && attrzp) {
2852 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2864 zfs_fuid_info_free(fuidp);
2871 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2876 if (os->os_sync == ZFS_SYNC_ALWAYS)
2877 zil_commit(zilog, 0);
2879 zfs_exit(zfsvfs, FTAG);
2884 * Look up the directory entries corresponding to the source and target
2885 * directory/name pairs.
2888 zfs_rename_relock_lookup(znode_t *sdzp, const struct componentname *scnp,
2889 znode_t **szpp, znode_t *tdzp, const struct componentname *tcnp,
2897 * Before using sdzp and tdzp we must ensure that they are live.
2898 * As a porting legacy from illumos we have two things to worry
2899 * about. One is typical for FreeBSD and it is that the vnode is
2900 * not reclaimed (doomed). The other is that the znode is live.
2901 * The current code can invalidate the znode without acquiring the
2902 * corresponding vnode lock if the object represented by the znode
2903 * and vnode is no longer valid after a rollback or receive operation.
2904 * z_teardown_lock hidden behind zfs_enter and zfs_exit is the lock
2905 * that protects the znodes from the invalidation.
2907 zfsvfs = sdzp->z_zfsvfs;
2908 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
2909 if ((error = zfs_enter_verify_zp(zfsvfs, sdzp, FTAG)) != 0)
2911 if ((error = zfs_verify_zp(tdzp)) != 0) {
2912 zfs_exit(zfsvfs, FTAG);
2917 * Re-resolve svp to be certain it still exists and fetch the
2920 error = zfs_dirent_lookup(sdzp, scnp->cn_nameptr, &szp, ZEXISTS);
2922 /* Source entry invalid or not there. */
2923 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
2924 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
2925 error = SET_ERROR(EINVAL);
2931 * Re-resolve tvp, if it disappeared we just carry on.
2933 error = zfs_dirent_lookup(tdzp, tcnp->cn_nameptr, &tzp, 0);
2936 if ((tcnp->cn_flags & ISDOTDOT) != 0)
2937 error = SET_ERROR(EINVAL);
2942 zfs_exit(zfsvfs, FTAG);
2947 * We acquire all but fdvp locks using non-blocking acquisitions. If we
2948 * fail to acquire any lock in the path we will drop all held locks,
2949 * acquire the new lock in a blocking fashion, and then release it and
2950 * restart the rename. This acquire/release step ensures that we do not
2951 * spin on a lock waiting for release. On error release all vnode locks
2952 * and decrement references the way tmpfs_rename() would do.
2955 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
2956 struct vnode *tdvp, struct vnode **tvpp,
2957 const struct componentname *scnp, const struct componentname *tcnp)
2959 struct vnode *nvp, *svp, *tvp;
2960 znode_t *sdzp, *tdzp, *szp, *tzp;
2964 if (*tvpp != NULL && *tvpp != tdvp)
2968 error = vn_lock(sdvp, LK_EXCLUSIVE);
2971 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
2976 error = vn_lock(tdvp, LK_EXCLUSIVE);
2985 error = zfs_rename_relock_lookup(sdzp, scnp, &szp, tdzp, tcnp, &tzp);
2992 tvp = tzp != NULL ? ZTOV(tzp) : NULL;
2995 * Now try acquire locks on svp and tvp.
2998 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3004 if (error != EBUSY) {
3008 error = vn_lock(nvp, LK_EXCLUSIVE);
3015 * Concurrent rename race.
3020 error = SET_ERROR(EINVAL);
3035 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3040 if (error != EBUSY) {
3044 error = vn_lock(nvp, LK_EXCLUSIVE);
3062 * Note that we must use VRELE_ASYNC in this function as it walks
3063 * up the directory tree and vrele may need to acquire an exclusive
3064 * lock if a last reference to a vnode is dropped.
3067 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3074 zfsvfs = tdzp->z_zfsvfs;
3076 return (SET_ERROR(EINVAL));
3079 if (tdzp->z_id == zfsvfs->z_root)
3083 ASSERT(!zp->z_unlinked);
3084 if ((error = sa_lookup(zp->z_sa_hdl,
3085 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3088 if (parent == szp->z_id) {
3089 error = SET_ERROR(EINVAL);
3092 if (parent == zfsvfs->z_root)
3094 if (parent == sdzp->z_id)
3097 error = zfs_zget(zfsvfs, parent, &zp1);
3102 VN_RELE_ASYNC(ZTOV(zp),
3103 dsl_pool_zrele_taskq(
3104 dmu_objset_pool(zfsvfs->z_os)));
3108 if (error == ENOTDIR)
3109 panic("checkpath: .. not a directory\n");
3111 VN_RELE_ASYNC(ZTOV(zp),
3112 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3116 #if __FreeBSD_version < 1300124
3118 cache_vop_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
3119 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
3125 cache_purge_negative(tdvp);
3130 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3131 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3135 * Move an entry from the provided source directory to the target
3136 * directory. Change the entry name as indicated.
3138 * IN: sdvp - Source directory containing the "old entry".
3139 * scnp - Old entry name.
3140 * tdvp - Target directory to contain the "new entry".
3141 * tcnp - New entry name.
3142 * cr - credentials of caller.
3143 * INOUT: svpp - Source file
3144 * tvpp - Target file, may point to NULL initially
3146 * RETURN: 0 on success, error code on failure.
3149 * sdvp,tdvp - ctime|mtime updated
3152 zfs_do_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3153 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3158 ASSERT_VOP_ELOCKED(tdvp, __func__);
3160 ASSERT_VOP_ELOCKED(*tvpp, __func__);
3162 /* Reject renames across filesystems. */
3163 if ((*svpp)->v_mount != tdvp->v_mount ||
3164 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3165 error = SET_ERROR(EXDEV);
3169 if (zfsctl_is_node(tdvp)) {
3170 error = SET_ERROR(EXDEV);
3175 * Lock all four vnodes to ensure safety and semantics of renaming.
3177 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3179 /* no vnodes are locked in the case of error here */
3183 error = zfs_do_rename_impl(sdvp, svpp, scnp, tdvp, tvpp, tcnp, cr);
3196 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3197 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3203 znode_t *tdzp, *sdzp, *tzp, *szp;
3204 const char *snm = scnp->cn_nameptr;
3205 const char *tnm = tcnp->cn_nameptr;
3210 zfsvfs = tdzp->z_zfsvfs;
3212 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3214 if ((error = zfs_verify_zp(sdzp)) != 0) {
3215 zfs_exit(zfsvfs, FTAG);
3218 zilog = zfsvfs->z_log;
3220 if (zfsvfs->z_utf8 && u8_validate(tnm,
3221 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3222 error = SET_ERROR(EILSEQ);
3226 /* If source and target are the same file, there is nothing to do. */
3227 if ((*svpp) == (*tvpp)) {
3232 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3233 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3234 (*tvpp)->v_mountedhere != NULL)) {
3235 error = SET_ERROR(EXDEV);
3240 if ((error = zfs_verify_zp(szp)) != 0) {
3241 zfs_exit(zfsvfs, FTAG);
3244 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3246 if ((error = zfs_verify_zp(tzp)) != 0) {
3247 zfs_exit(zfsvfs, FTAG);
3253 * This is to prevent the creation of links into attribute space
3254 * by renaming a linked file into/outof an attribute directory.
3255 * See the comment in zfs_link() for why this is considered bad.
3257 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3258 error = SET_ERROR(EINVAL);
3263 * If we are using project inheritance, means if the directory has
3264 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3265 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3266 * such case, we only allow renames into our tree when the project
3269 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3270 tdzp->z_projid != szp->z_projid) {
3271 error = SET_ERROR(EXDEV);
3276 * Must have write access at the source to remove the old entry
3277 * and write access at the target to create the new entry.
3278 * Note that if target and source are the same, this can be
3279 * done in a single check.
3281 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr, NULL)))
3284 if ((*svpp)->v_type == VDIR) {
3286 * Avoid ".", "..", and aliases of "." for obvious reasons.
3288 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3290 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3296 * Check to make sure rename is valid.
3297 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3299 if ((error = zfs_rename_check(szp, sdzp, tdzp)))
3304 * Does target exist?
3308 * Source and target must be the same type.
3310 if ((*svpp)->v_type == VDIR) {
3311 if ((*tvpp)->v_type != VDIR) {
3312 error = SET_ERROR(ENOTDIR);
3320 if ((*tvpp)->v_type == VDIR) {
3321 error = SET_ERROR(EISDIR);
3327 vn_seqc_write_begin(*svpp);
3328 vn_seqc_write_begin(sdvp);
3330 vn_seqc_write_begin(*tvpp);
3332 vn_seqc_write_begin(tdvp);
3334 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3336 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3339 * notify the target directory if it is not the same
3340 * as source directory.
3343 vnevent_rename_dest_dir(tdvp, ct);
3346 tx = dmu_tx_create(zfsvfs->z_os);
3347 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3348 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3349 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3350 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3352 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3353 zfs_sa_upgrade_txholds(tx, tdzp);
3356 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3357 zfs_sa_upgrade_txholds(tx, tzp);
3360 zfs_sa_upgrade_txholds(tx, szp);
3361 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3362 error = dmu_tx_assign(tx, TXG_WAIT);
3368 if (tzp) /* Attempt to remove the existing target */
3369 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3372 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3374 szp->z_pflags |= ZFS_AV_MODIFIED;
3376 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3377 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3380 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3383 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3384 snm, tdzp, tnm, szp);
3387 * At this point, we have successfully created
3388 * the target name, but have failed to remove
3389 * the source name. Since the create was done
3390 * with the ZRENAMING flag, there are
3391 * complications; for one, the link count is
3392 * wrong. The easiest way to deal with this
3393 * is to remove the newly created target, and
3394 * return the original error. This must
3395 * succeed; fortunately, it is very unlikely to
3396 * fail, since we just created it.
3398 VERIFY0(zfs_link_destroy(tdzp, tnm, szp, tx,
3403 cache_vop_rename(sdvp, *svpp, tdvp, *tvpp, scnp, tcnp);
3410 vn_seqc_write_end(*svpp);
3411 vn_seqc_write_end(sdvp);
3413 vn_seqc_write_end(*tvpp);
3415 vn_seqc_write_end(tdvp);
3418 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3419 zil_commit(zilog, 0);
3420 zfs_exit(zfsvfs, FTAG);
3426 zfs_rename(znode_t *sdzp, const char *sname, znode_t *tdzp, const char *tname,
3427 cred_t *cr, int flags, uint64_t rflags, vattr_t *wo_vap, zidmap_t *mnt_ns)
3429 struct componentname scn, tcn;
3430 vnode_t *sdvp, *tdvp;
3435 if (rflags != 0 || wo_vap != NULL)
3436 return (SET_ERROR(EINVAL));
3440 error = zfs_lookup_internal(sdzp, sname, &svp, &scn, DELETE);
3441 if (sdzp->z_zfsvfs->z_replay == B_FALSE)
3447 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
3448 error = zfs_lookup_internal(tdzp, tname, &tvp, &tcn, RENAME);
3449 if (error == EJUSTRETURN)
3451 else if (error != 0) {
3456 error = zfs_do_rename(sdvp, &svp, &scn, tdvp, &tvp, &tcn, cr);
3467 * Insert the indicated symbolic reference entry into the directory.
3469 * IN: dvp - Directory to contain new symbolic link.
3470 * link - Name for new symlink entry.
3471 * vap - Attributes of new entry.
3472 * cr - credentials of caller.
3473 * ct - caller context
3474 * flags - case flags
3475 * mnt_ns - Unused on FreeBSD
3477 * RETURN: 0 on success, error code on failure.
3480 * dvp - ctime|mtime updated
3483 zfs_symlink(znode_t *dzp, const char *name, vattr_t *vap,
3484 const char *link, znode_t **zpp, cred_t *cr, int flags, zidmap_t *mnt_ns)
3489 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3491 uint64_t len = strlen(link);
3493 zfs_acl_ids_t acl_ids;
3494 boolean_t fuid_dirtied;
3495 uint64_t txtype = TX_SYMLINK;
3497 ASSERT3S(vap->va_type, ==, VLNK);
3499 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
3501 zilog = zfsvfs->z_log;
3503 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3504 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3505 zfs_exit(zfsvfs, FTAG);
3506 return (SET_ERROR(EILSEQ));
3509 if (len > MAXPATHLEN) {
3510 zfs_exit(zfsvfs, FTAG);
3511 return (SET_ERROR(ENAMETOOLONG));
3514 if ((error = zfs_acl_ids_create(dzp, 0,
3515 vap, cr, NULL, &acl_ids, NULL)) != 0) {
3516 zfs_exit(zfsvfs, FTAG);
3521 * Attempt to lock directory; fail if entry already exists.
3523 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
3525 zfs_acl_ids_free(&acl_ids);
3526 zfs_exit(zfsvfs, FTAG);
3530 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
3531 zfs_acl_ids_free(&acl_ids);
3532 zfs_exit(zfsvfs, FTAG);
3536 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids,
3538 zfs_acl_ids_free(&acl_ids);
3539 zfs_exit(zfsvfs, FTAG);
3540 return (SET_ERROR(EDQUOT));
3543 getnewvnode_reserve_();
3544 tx = dmu_tx_create(zfsvfs->z_os);
3545 fuid_dirtied = zfsvfs->z_fuid_dirty;
3546 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3547 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3548 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3549 ZFS_SA_BASE_ATTR_SIZE + len);
3550 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3551 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3552 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3553 acl_ids.z_aclp->z_acl_bytes);
3556 zfs_fuid_txhold(zfsvfs, tx);
3557 error = dmu_tx_assign(tx, TXG_WAIT);
3559 zfs_acl_ids_free(&acl_ids);
3561 getnewvnode_drop_reserve();
3562 zfs_exit(zfsvfs, FTAG);
3567 * Create a new object for the symlink.
3568 * for version 4 ZPL datasets the symlink will be an SA attribute
3570 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3573 zfs_fuid_sync(zfsvfs, tx);
3576 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3577 __DECONST(void *, link), len, tx);
3579 zfs_sa_symlink(zp, __DECONST(char *, link), len, tx);
3582 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3583 &zp->z_size, sizeof (zp->z_size), tx);
3585 * Insert the new object into the directory.
3587 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
3589 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3592 zfs_acl_ids_free(&acl_ids);
3596 getnewvnode_drop_reserve();
3598 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3599 zil_commit(zilog, 0);
3601 zfs_exit(zfsvfs, FTAG);
3606 * Return, in the buffer contained in the provided uio structure,
3607 * the symbolic path referred to by vp.
3609 * IN: vp - vnode of symbolic link.
3610 * uio - structure to contain the link path.
3611 * cr - credentials of caller.
3612 * ct - caller context
3614 * OUT: uio - structure containing the link path.
3616 * RETURN: 0 on success, error code on failure.
3619 * vp - atime updated
3622 zfs_readlink(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, caller_context_t *ct)
3624 (void) cr, (void) ct;
3625 znode_t *zp = VTOZ(vp);
3626 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3629 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3633 error = sa_lookup_uio(zp->z_sa_hdl,
3634 SA_ZPL_SYMLINK(zfsvfs), uio);
3636 error = zfs_sa_readlink(zp, uio);
3638 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3640 zfs_exit(zfsvfs, FTAG);
3645 * Insert a new entry into directory tdvp referencing svp.
3647 * IN: tdvp - Directory to contain new entry.
3648 * svp - vnode of new entry.
3649 * name - name of new entry.
3650 * cr - credentials of caller.
3652 * RETURN: 0 on success, error code on failure.
3655 * tdvp - ctime|mtime updated
3656 * svp - ctime updated
3659 zfs_link(znode_t *tdzp, znode_t *szp, const char *name, cred_t *cr,
3664 zfsvfs_t *zfsvfs = tdzp->z_zfsvfs;
3671 ASSERT3S(ZTOV(tdzp)->v_type, ==, VDIR);
3673 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3675 zilog = zfsvfs->z_log;
3678 * POSIX dictates that we return EPERM here.
3679 * Better choices include ENOTSUP or EISDIR.
3681 if (ZTOV(szp)->v_type == VDIR) {
3682 zfs_exit(zfsvfs, FTAG);
3683 return (SET_ERROR(EPERM));
3686 if ((error = zfs_verify_zp(szp)) != 0) {
3687 zfs_exit(zfsvfs, FTAG);
3692 * If we are using project inheritance, means if the directory has
3693 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3694 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3695 * such case, we only allow hard link creation in our tree when the
3696 * project IDs are the same.
3698 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3699 tdzp->z_projid != szp->z_projid) {
3700 zfs_exit(zfsvfs, FTAG);
3701 return (SET_ERROR(EXDEV));
3704 if (szp->z_pflags & (ZFS_APPENDONLY |
3705 ZFS_IMMUTABLE | ZFS_READONLY)) {
3706 zfs_exit(zfsvfs, FTAG);
3707 return (SET_ERROR(EPERM));
3710 /* Prevent links to .zfs/shares files */
3712 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3713 &parent, sizeof (uint64_t))) != 0) {
3714 zfs_exit(zfsvfs, FTAG);
3717 if (parent == zfsvfs->z_shares_dir) {
3718 zfs_exit(zfsvfs, FTAG);
3719 return (SET_ERROR(EPERM));
3722 if (zfsvfs->z_utf8 && u8_validate(name,
3723 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3724 zfs_exit(zfsvfs, FTAG);
3725 return (SET_ERROR(EILSEQ));
3729 * We do not support links between attributes and non-attributes
3730 * because of the potential security risk of creating links
3731 * into "normal" file space in order to circumvent restrictions
3732 * imposed in attribute space.
3734 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) {
3735 zfs_exit(zfsvfs, FTAG);
3736 return (SET_ERROR(EINVAL));
3740 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
3741 if (owner != crgetuid(cr) && secpolicy_basic_link(ZTOV(szp), cr) != 0) {
3742 zfs_exit(zfsvfs, FTAG);
3743 return (SET_ERROR(EPERM));
3746 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr, NULL))) {
3747 zfs_exit(zfsvfs, FTAG);
3752 * Attempt to lock directory; fail if entry already exists.
3754 error = zfs_dirent_lookup(tdzp, name, &tzp, ZNEW);
3756 zfs_exit(zfsvfs, FTAG);
3760 tx = dmu_tx_create(zfsvfs->z_os);
3761 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3762 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name);
3763 zfs_sa_upgrade_txholds(tx, szp);
3764 zfs_sa_upgrade_txholds(tx, tdzp);
3765 error = dmu_tx_assign(tx, TXG_WAIT);
3768 zfs_exit(zfsvfs, FTAG);
3772 error = zfs_link_create(tdzp, name, szp, tx, 0);
3775 uint64_t txtype = TX_LINK;
3776 zfs_log_link(zilog, tx, txtype, tdzp, szp, name);
3782 vnevent_link(ZTOV(szp), ct);
3785 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3786 zil_commit(zilog, 0);
3788 zfs_exit(zfsvfs, FTAG);
3793 * Free or allocate space in a file. Currently, this function only
3794 * supports the `F_FREESP' command. However, this command is somewhat
3795 * misnamed, as its functionality includes the ability to allocate as
3796 * well as free space.
3798 * IN: ip - inode of file to free data in.
3799 * cmd - action to take (only F_FREESP supported).
3800 * bfp - section of file to free/alloc.
3801 * flag - current file open mode flags.
3802 * offset - current file offset.
3803 * cr - credentials of caller.
3805 * RETURN: 0 on success, error code on failure.
3808 * ip - ctime|mtime updated
3811 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag,
3812 offset_t offset, cred_t *cr)
3815 zfsvfs_t *zfsvfs = ZTOZSB(zp);
3819 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3822 if (cmd != F_FREESP) {
3823 zfs_exit(zfsvfs, FTAG);
3824 return (SET_ERROR(EINVAL));
3828 * Callers might not be able to detect properly that we are read-only,
3829 * so check it explicitly here.
3831 if (zfs_is_readonly(zfsvfs)) {
3832 zfs_exit(zfsvfs, FTAG);
3833 return (SET_ERROR(EROFS));
3836 if (bfp->l_len < 0) {
3837 zfs_exit(zfsvfs, FTAG);
3838 return (SET_ERROR(EINVAL));
3842 * Permissions aren't checked on Solaris because on this OS
3843 * zfs_space() can only be called with an opened file handle.
3844 * On Linux we can get here through truncate_range() which
3845 * operates directly on inodes, so we need to check access rights.
3847 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr, NULL))) {
3848 zfs_exit(zfsvfs, FTAG);
3853 len = bfp->l_len; /* 0 means from off to end of file */
3855 error = zfs_freesp(zp, off, len, flag, TRUE);
3857 zfs_exit(zfsvfs, FTAG);
3862 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3864 (void) cr, (void) ct;
3865 znode_t *zp = VTOZ(vp);
3866 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3869 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
3870 if (zp->z_sa_hdl == NULL) {
3872 * The fs has been unmounted, or we did a
3873 * suspend/resume and this file no longer exists.
3875 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3880 if (zp->z_unlinked) {
3882 * Fast path to recycle a vnode of a removed file.
3884 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3889 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3890 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3892 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3893 zfs_sa_upgrade_txholds(tx, zp);
3894 error = dmu_tx_assign(tx, TXG_WAIT);
3898 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
3899 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3900 zp->z_atime_dirty = 0;
3904 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3908 _Static_assert(sizeof (struct zfid_short) <= sizeof (struct fid),
3909 "struct zfid_short bigger than struct fid");
3910 _Static_assert(sizeof (struct zfid_long) <= sizeof (struct fid),
3911 "struct zfid_long bigger than struct fid");
3914 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3917 znode_t *zp = VTOZ(vp);
3918 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3921 uint64_t object = zp->z_id;
3925 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3928 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
3929 &gen64, sizeof (uint64_t))) != 0) {
3930 zfs_exit(zfsvfs, FTAG);
3934 gen = (uint32_t)gen64;
3936 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3937 fidp->fid_len = size;
3939 zfid = (zfid_short_t *)fidp;
3941 zfid->zf_len = size;
3943 for (i = 0; i < sizeof (zfid->zf_object); i++)
3944 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3946 /* Must have a non-zero generation number to distinguish from .zfs */
3949 for (i = 0; i < sizeof (zfid->zf_gen); i++)
3950 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3952 if (size == LONG_FID_LEN) {
3953 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
3956 zlfid = (zfid_long_t *)fidp;
3958 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3959 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3961 /* XXX - this should be the generation number for the objset */
3962 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3963 zlfid->zf_setgen[i] = 0;
3966 zfs_exit(zfsvfs, FTAG);
3971 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
3972 caller_context_t *ct)
3980 *valp = MIN(LONG_MAX, ZFS_LINK_MAX);
3983 case _PC_FILESIZEBITS:
3986 case _PC_MIN_HOLE_SIZE:
3987 *valp = (int)SPA_MINBLOCKSIZE;
3989 case _PC_ACL_EXTENDED:
3990 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
3992 zfsvfs = zp->z_zfsvfs;
3993 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3995 *valp = zfsvfs->z_acl_type == ZFSACLTYPE_POSIX ? 1 : 0;
3996 zfs_exit(zfsvfs, FTAG);
4004 zfsvfs = zp->z_zfsvfs;
4005 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
4007 *valp = zfsvfs->z_acl_type == ZFS_ACLTYPE_NFSV4 ? 1 : 0;
4008 zfs_exit(zfsvfs, FTAG);
4011 case _PC_ACL_PATH_MAX:
4012 *valp = ACL_MAX_ENTRIES;
4016 return (EOPNOTSUPP);
4021 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind,
4024 znode_t *zp = VTOZ(vp);
4025 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4026 zfs_locked_range_t *lr;
4028 off_t start, end, obj_size;
4030 int pgsin_b, pgsin_a;
4033 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4034 return (zfs_vm_pagerret_error);
4036 start = IDX_TO_OFF(ma[0]->pindex);
4037 end = IDX_TO_OFF(ma[count - 1]->pindex + 1);
4040 * Lock a range covering all required and optional pages.
4041 * Note that we need to handle the case of the block size growing.
4044 blksz = zp->z_blksz;
4045 lr = zfs_rangelock_tryenter(&zp->z_rangelock,
4046 rounddown(start, blksz),
4047 roundup(end, blksz) - rounddown(start, blksz), RL_READER);
4049 if (rahead != NULL) {
4053 if (rbehind != NULL) {
4059 if (blksz == zp->z_blksz)
4061 zfs_rangelock_exit(lr);
4064 object = ma[0]->object;
4065 zfs_vmobject_wlock(object);
4066 obj_size = object->un_pager.vnp.vnp_size;
4067 zfs_vmobject_wunlock(object);
4068 if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) {
4070 zfs_rangelock_exit(lr);
4071 zfs_exit(zfsvfs, FTAG);
4072 return (zfs_vm_pagerret_bad);
4076 if (rbehind != NULL) {
4077 pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz));
4078 pgsin_b = MIN(*rbehind, pgsin_b);
4082 if (rahead != NULL) {
4083 pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end);
4084 if (end + IDX_TO_OFF(pgsin_a) >= obj_size)
4085 pgsin_a = OFF_TO_IDX(round_page(obj_size) - end);
4086 pgsin_a = MIN(*rahead, pgsin_a);
4090 * NB: we need to pass the exact byte size of the data that we expect
4091 * to read after accounting for the file size. This is required because
4092 * ZFS will panic if we request DMU to read beyond the end of the last
4095 error = dmu_read_pages(zfsvfs->z_os, zp->z_id, ma, count, &pgsin_b,
4096 &pgsin_a, MIN(end, obj_size) - (end - PAGE_SIZE));
4099 zfs_rangelock_exit(lr);
4100 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4102 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, count*PAGE_SIZE);
4104 zfs_exit(zfsvfs, FTAG);
4107 return (zfs_vm_pagerret_error);
4109 VM_CNT_INC(v_vnodein);
4110 VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a);
4111 if (rbehind != NULL)
4115 return (zfs_vm_pagerret_ok);
4118 #ifndef _SYS_SYSPROTO_H_
4119 struct vop_getpages_args {
4129 zfs_freebsd_getpages(struct vop_getpages_args *ap)
4132 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4137 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4140 znode_t *zp = VTOZ(vp);
4141 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4142 zfs_locked_range_t *lr;
4150 vm_ooffset_t lo_off;
4158 object = vp->v_object;
4159 KASSERT(ma[0]->object == object, ("mismatching object"));
4160 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4164 for (i = 0; i < pcount; i++)
4165 rtvals[i] = zfs_vm_pagerret_error;
4167 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4168 return (zfs_vm_pagerret_error);
4170 off = IDX_TO_OFF(ma[0]->pindex);
4171 blksz = zp->z_blksz;
4172 lo_off = rounddown(off, blksz);
4173 lo_len = roundup(len + (off - lo_off), blksz);
4174 lr = zfs_rangelock_enter(&zp->z_rangelock, lo_off, lo_len, RL_WRITER);
4176 zfs_vmobject_wlock(object);
4177 if (len + off > object->un_pager.vnp.vnp_size) {
4178 if (object->un_pager.vnp.vnp_size > off) {
4181 len = object->un_pager.vnp.vnp_size - off;
4183 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4185 * If the object is locked and the following
4186 * conditions hold, then the page's dirty
4187 * field cannot be concurrently changed by a
4191 vm_page_assert_sbusied(m);
4192 KASSERT(!pmap_page_is_write_mapped(m),
4193 ("zfs_putpages: page %p is not read-only",
4195 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4202 if (ncount < pcount) {
4203 for (i = ncount; i < pcount; i++) {
4204 rtvals[i] = zfs_vm_pagerret_bad;
4208 zfs_vmobject_wunlock(object);
4210 boolean_t commit = (flags & (zfs_vm_pagerput_sync |
4211 zfs_vm_pagerput_inval)) != 0 ||
4212 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS;
4217 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, zp->z_uid) ||
4218 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, zp->z_gid) ||
4219 (zp->z_projid != ZFS_DEFAULT_PROJID &&
4220 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
4225 tx = dmu_tx_create(zfsvfs->z_os);
4226 dmu_tx_hold_write(tx, zp->z_id, off, len);
4228 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4229 zfs_sa_upgrade_txholds(tx, zp);
4230 err = dmu_tx_assign(tx, TXG_WAIT);
4236 if (zp->z_blksz < PAGE_SIZE) {
4237 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4238 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4239 va = zfs_map_page(ma[i], &sf);
4240 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4244 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4248 uint64_t mtime[2], ctime[2];
4249 sa_bulk_attr_t bulk[3];
4252 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4254 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4256 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4258 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
4259 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4262 * XXX we should be passing a callback to undirty
4263 * but that would make the locking messier
4265 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off,
4266 len, commit, NULL, NULL);
4268 zfs_vmobject_wlock(object);
4269 for (i = 0; i < ncount; i++) {
4270 rtvals[i] = zfs_vm_pagerret_ok;
4271 vm_page_undirty(ma[i]);
4273 zfs_vmobject_wunlock(object);
4274 VM_CNT_INC(v_vnodeout);
4275 VM_CNT_ADD(v_vnodepgsout, ncount);
4280 zfs_rangelock_exit(lr);
4282 zil_commit(zfsvfs->z_log, zp->z_id);
4284 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, len);
4286 zfs_exit(zfsvfs, FTAG);
4290 #ifndef _SYS_SYSPROTO_H_
4291 struct vop_putpages_args {
4301 zfs_freebsd_putpages(struct vop_putpages_args *ap)
4304 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4308 #ifndef _SYS_SYSPROTO_H_
4309 struct vop_bmap_args {
4312 struct bufobj **a_bop;
4320 zfs_freebsd_bmap(struct vop_bmap_args *ap)
4323 if (ap->a_bop != NULL)
4324 *ap->a_bop = &ap->a_vp->v_bufobj;
4325 if (ap->a_bnp != NULL)
4326 *ap->a_bnp = ap->a_bn;
4327 if (ap->a_runp != NULL)
4329 if (ap->a_runb != NULL)
4335 #ifndef _SYS_SYSPROTO_H_
4336 struct vop_open_args {
4339 struct ucred *a_cred;
4340 struct thread *a_td;
4345 zfs_freebsd_open(struct vop_open_args *ap)
4347 vnode_t *vp = ap->a_vp;
4348 znode_t *zp = VTOZ(vp);
4351 error = zfs_open(&vp, ap->a_mode, ap->a_cred);
4353 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4357 #ifndef _SYS_SYSPROTO_H_
4358 struct vop_close_args {
4361 struct ucred *a_cred;
4362 struct thread *a_td;
4367 zfs_freebsd_close(struct vop_close_args *ap)
4370 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred));
4373 #ifndef _SYS_SYSPROTO_H_
4374 struct vop_ioctl_args {
4385 zfs_freebsd_ioctl(struct vop_ioctl_args *ap)
4388 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4389 ap->a_fflag, ap->a_cred, NULL));
4393 ioflags(int ioflags)
4397 if (ioflags & IO_APPEND)
4399 if (ioflags & IO_NDELAY)
4400 flags |= O_NONBLOCK;
4401 if (ioflags & IO_SYNC)
4407 #ifndef _SYS_SYSPROTO_H_
4408 struct vop_read_args {
4412 struct ucred *a_cred;
4417 zfs_freebsd_read(struct vop_read_args *ap)
4420 zfs_uio_init(&uio, ap->a_uio);
4421 return (zfs_read(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4425 #ifndef _SYS_SYSPROTO_H_
4426 struct vop_write_args {
4430 struct ucred *a_cred;
4435 zfs_freebsd_write(struct vop_write_args *ap)
4438 zfs_uio_init(&uio, ap->a_uio);
4439 return (zfs_write(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4443 #if __FreeBSD_version >= 1300102
4445 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4446 * the comment above cache_fplookup for details.
4449 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args *v)
4457 if (__predict_false(zp == NULL))
4459 pflags = atomic_load_64(&zp->z_pflags);
4460 if (pflags & ZFS_AV_QUARANTINED)
4462 if (pflags & ZFS_XATTR)
4464 if ((pflags & ZFS_NO_EXECS_DENIED) == 0)
4470 #if __FreeBSD_version >= 1300139
4472 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args *v)
4480 if (__predict_false(zp == NULL)) {
4484 target = atomic_load_consume_ptr(&zp->z_cached_symlink);
4485 if (target == NULL) {
4488 return (cache_symlink_resolve(v->a_fpl, target, strlen(target)));
4492 #ifndef _SYS_SYSPROTO_H_
4493 struct vop_access_args {
4495 accmode_t a_accmode;
4496 struct ucred *a_cred;
4497 struct thread *a_td;
4502 zfs_freebsd_access(struct vop_access_args *ap)
4504 vnode_t *vp = ap->a_vp;
4505 znode_t *zp = VTOZ(vp);
4510 if (ap->a_accmode == VEXEC) {
4511 if (zfs_fastaccesschk_execute(zp, ap->a_cred) == 0)
4516 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4518 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4520 error = zfs_access(zp, accmode, 0, ap->a_cred);
4523 * VADMIN has to be handled by vaccess().
4526 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4528 #if __FreeBSD_version >= 1300105
4529 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4530 zp->z_gid, accmode, ap->a_cred);
4532 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4533 zp->z_gid, accmode, ap->a_cred, NULL);
4539 * For VEXEC, ensure that at least one execute bit is set for
4542 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4543 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4550 #ifndef _SYS_SYSPROTO_H_
4551 struct vop_lookup_args {
4552 struct vnode *a_dvp;
4553 struct vnode **a_vpp;
4554 struct componentname *a_cnp;
4559 zfs_freebsd_lookup(struct vop_lookup_args *ap, boolean_t cached)
4561 struct componentname *cnp = ap->a_cnp;
4562 char nm[NAME_MAX + 1];
4564 ASSERT3U(cnp->cn_namelen, <, sizeof (nm));
4565 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof (nm)));
4567 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4568 cnp->cn_cred, 0, cached));
4572 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args *ap)
4575 return (zfs_freebsd_lookup((struct vop_lookup_args *)ap, B_TRUE));
4578 #ifndef _SYS_SYSPROTO_H_
4579 struct vop_lookup_args {
4580 struct vnode *a_dvp;
4581 struct vnode **a_vpp;
4582 struct componentname *a_cnp;
4587 zfs_cache_lookup(struct vop_lookup_args *ap)
4591 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4592 if (zfsvfs->z_use_namecache)
4593 return (vfs_cache_lookup(ap));
4595 return (zfs_freebsd_lookup(ap, B_FALSE));
4598 #ifndef _SYS_SYSPROTO_H_
4599 struct vop_create_args {
4600 struct vnode *a_dvp;
4601 struct vnode **a_vpp;
4602 struct componentname *a_cnp;
4603 struct vattr *a_vap;
4608 zfs_freebsd_create(struct vop_create_args *ap)
4611 struct componentname *cnp = ap->a_cnp;
4612 vattr_t *vap = ap->a_vap;
4616 #if __FreeBSD_version < 1400068
4617 ASSERT(cnp->cn_flags & SAVENAME);
4620 vattr_init_mask(vap);
4621 mode = vap->va_mode & ALLPERMS;
4622 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4625 rc = zfs_create(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, 0, mode,
4626 &zp, cnp->cn_cred, 0 /* flag */, NULL /* vsecattr */, NULL);
4628 *ap->a_vpp = ZTOV(zp);
4629 if (zfsvfs->z_use_namecache &&
4630 rc == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
4631 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
4636 #ifndef _SYS_SYSPROTO_H_
4637 struct vop_remove_args {
4638 struct vnode *a_dvp;
4640 struct componentname *a_cnp;
4645 zfs_freebsd_remove(struct vop_remove_args *ap)
4648 #if __FreeBSD_version < 1400068
4649 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4652 return (zfs_remove_(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
4653 ap->a_cnp->cn_cred));
4656 #ifndef _SYS_SYSPROTO_H_
4657 struct vop_mkdir_args {
4658 struct vnode *a_dvp;
4659 struct vnode **a_vpp;
4660 struct componentname *a_cnp;
4661 struct vattr *a_vap;
4666 zfs_freebsd_mkdir(struct vop_mkdir_args *ap)
4668 vattr_t *vap = ap->a_vap;
4672 #if __FreeBSD_version < 1400068
4673 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4676 vattr_init_mask(vap);
4679 rc = zfs_mkdir(VTOZ(ap->a_dvp), ap->a_cnp->cn_nameptr, vap, &zp,
4680 ap->a_cnp->cn_cred, 0, NULL, NULL);
4683 *ap->a_vpp = ZTOV(zp);
4687 #ifndef _SYS_SYSPROTO_H_
4688 struct vop_rmdir_args {
4689 struct vnode *a_dvp;
4691 struct componentname *a_cnp;
4696 zfs_freebsd_rmdir(struct vop_rmdir_args *ap)
4698 struct componentname *cnp = ap->a_cnp;
4700 #if __FreeBSD_version < 1400068
4701 ASSERT(cnp->cn_flags & SAVENAME);
4704 return (zfs_rmdir_(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
4707 #ifndef _SYS_SYSPROTO_H_
4708 struct vop_readdir_args {
4711 struct ucred *a_cred;
4714 cookie_t **a_cookies;
4719 zfs_freebsd_readdir(struct vop_readdir_args *ap)
4722 zfs_uio_init(&uio, ap->a_uio);
4723 return (zfs_readdir(ap->a_vp, &uio, ap->a_cred, ap->a_eofflag,
4724 ap->a_ncookies, ap->a_cookies));
4727 #ifndef _SYS_SYSPROTO_H_
4728 struct vop_fsync_args {
4731 struct thread *a_td;
4736 zfs_freebsd_fsync(struct vop_fsync_args *ap)
4739 return (zfs_fsync(VTOZ(ap->a_vp), 0, ap->a_td->td_ucred));
4742 #ifndef _SYS_SYSPROTO_H_
4743 struct vop_getattr_args {
4745 struct vattr *a_vap;
4746 struct ucred *a_cred;
4751 zfs_freebsd_getattr(struct vop_getattr_args *ap)
4753 vattr_t *vap = ap->a_vap;
4759 xvap.xva_vattr = *vap;
4760 xvap.xva_vattr.va_mask |= AT_XVATTR;
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);
4776 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred);
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); \
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);
4809 *vap = xvap.xva_vattr;
4810 vap->va_flags = fflags;
4814 #ifndef _SYS_SYSPROTO_H_
4815 struct vop_setattr_args {
4817 struct vattr *a_vap;
4818 struct ucred *a_cred;
4823 zfs_freebsd_setattr(struct vop_setattr_args *ap)
4825 vnode_t *vp = ap->a_vp;
4826 vattr_t *vap = ap->a_vap;
4827 cred_t *cred = ap->a_cred;
4832 vattr_init_mask(vap);
4833 vap->va_mask &= ~AT_NOSET;
4836 xvap.xva_vattr = *vap;
4838 zflags = VTOZ(vp)->z_pflags;
4840 if (vap->va_flags != VNOVAL) {
4841 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4844 if (zfsvfs->z_use_fuids == B_FALSE)
4845 return (EOPNOTSUPP);
4847 fflags = vap->va_flags;
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.
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);
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.
4868 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4869 spl_priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) {
4871 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4872 error = securelevel_gt(cred, 0);
4878 * Callers may only modify the file flags on
4879 * objects they have VADMIN rights for.
4881 if ((error = VOP_ACCESS(vp, VADMIN, cred,
4885 (ZFS_IMMUTABLE | ZFS_APPENDONLY |
4890 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
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); \
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);
4928 if (vap->va_birthtime.tv_sec != VNOVAL) {
4929 xvap.xva_vattr.va_mask |= AT_XVATTR;
4930 XVA_SET_REQ(&xvap, XAT_CREATETIME);
4932 return (zfs_setattr(VTOZ(vp), (vattr_t *)&xvap, 0, cred, NULL));
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;
4947 zfs_freebsd_rename(struct vop_rename_args *ap)
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;
4955 #if __FreeBSD_version < 1400068
4956 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4957 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4960 error = zfs_do_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
4961 ap->a_tcnp, ap->a_fcnp->cn_cred);
4972 #ifndef _SYS_SYSPROTO_H_
4973 struct vop_symlink_args {
4974 struct vnode *a_dvp;
4975 struct vnode **a_vpp;
4976 struct componentname *a_cnp;
4977 struct vattr *a_vap;
4983 zfs_freebsd_symlink(struct vop_symlink_args *ap)
4985 struct componentname *cnp = ap->a_cnp;
4986 vattr_t *vap = ap->a_vap;
4988 #if __FreeBSD_version >= 1300139
4994 #if __FreeBSD_version < 1400068
4995 ASSERT(cnp->cn_flags & SAVENAME);
4998 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
4999 vattr_init_mask(vap);
5002 rc = zfs_symlink(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap,
5003 ap->a_target, &zp, cnp->cn_cred, 0 /* flags */, NULL);
5005 *ap->a_vpp = ZTOV(zp);
5006 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
5007 #if __FreeBSD_version >= 1300139
5008 MPASS(zp->z_cached_symlink == NULL);
5009 symlink_len = strlen(ap->a_target);
5010 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5011 if (symlink != NULL) {
5012 memcpy(symlink, ap->a_target, symlink_len);
5013 symlink[symlink_len] = '\0';
5014 atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5015 (uintptr_t)symlink);
5022 #ifndef _SYS_SYSPROTO_H_
5023 struct vop_readlink_args {
5026 struct ucred *a_cred;
5031 zfs_freebsd_readlink(struct vop_readlink_args *ap)
5035 #if __FreeBSD_version >= 1300139
5036 znode_t *zp = VTOZ(ap->a_vp);
5037 char *symlink, *base;
5042 zfs_uio_init(&uio, ap->a_uio);
5043 #if __FreeBSD_version >= 1300139
5045 if (zfs_uio_segflg(&uio) == UIO_SYSSPACE &&
5046 zfs_uio_iovcnt(&uio) == 1) {
5047 base = zfs_uio_iovbase(&uio, 0);
5048 symlink_len = zfs_uio_iovlen(&uio, 0);
5052 error = zfs_readlink(ap->a_vp, &uio, ap->a_cred, NULL);
5053 #if __FreeBSD_version >= 1300139
5054 if (atomic_load_ptr(&zp->z_cached_symlink) != NULL ||
5055 error != 0 || !trycache) {
5058 symlink_len -= zfs_uio_resid(&uio);
5059 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5060 if (symlink != NULL) {
5061 memcpy(symlink, base, symlink_len);
5062 symlink[symlink_len] = '\0';
5063 if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5064 (uintptr_t)NULL, (uintptr_t)symlink)) {
5065 cache_symlink_free(symlink, symlink_len + 1);
5072 #ifndef _SYS_SYSPROTO_H_
5073 struct vop_link_args {
5074 struct vnode *a_tdvp;
5076 struct componentname *a_cnp;
5081 zfs_freebsd_link(struct vop_link_args *ap)
5083 struct componentname *cnp = ap->a_cnp;
5084 vnode_t *vp = ap->a_vp;
5085 vnode_t *tdvp = ap->a_tdvp;
5087 if (tdvp->v_mount != vp->v_mount)
5090 #if __FreeBSD_version < 1400068
5091 ASSERT(cnp->cn_flags & SAVENAME);
5094 return (zfs_link(VTOZ(tdvp), VTOZ(vp),
5095 cnp->cn_nameptr, cnp->cn_cred, 0));
5098 #ifndef _SYS_SYSPROTO_H_
5099 struct vop_inactive_args {
5101 struct thread *a_td;
5106 zfs_freebsd_inactive(struct vop_inactive_args *ap)
5108 vnode_t *vp = ap->a_vp;
5110 #if __FreeBSD_version >= 1300123
5111 zfs_inactive(vp, curthread->td_ucred, NULL);
5113 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5118 #if __FreeBSD_version >= 1300042
5119 #ifndef _SYS_SYSPROTO_H_
5120 struct vop_need_inactive_args {
5122 struct thread *a_td;
5127 zfs_freebsd_need_inactive(struct vop_need_inactive_args *ap)
5129 vnode_t *vp = ap->a_vp;
5130 znode_t *zp = VTOZ(vp);
5131 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5134 if (vn_need_pageq_flush(vp))
5137 if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs))
5139 need = (zp->z_sa_hdl == NULL || zp->z_unlinked || zp->z_atime_dirty);
5140 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5146 #ifndef _SYS_SYSPROTO_H_
5147 struct vop_reclaim_args {
5149 struct thread *a_td;
5154 zfs_freebsd_reclaim(struct vop_reclaim_args *ap)
5156 vnode_t *vp = ap->a_vp;
5157 znode_t *zp = VTOZ(vp);
5158 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5160 ASSERT3P(zp, !=, NULL);
5162 #if __FreeBSD_version < 1300042
5163 /* Destroy the vm object and flush associated pages. */
5164 vnode_destroy_vobject(vp);
5167 * z_teardown_inactive_lock protects from a race with
5168 * zfs_znode_dmu_fini in zfsvfs_teardown during
5171 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
5172 if (zp->z_sa_hdl == NULL)
5176 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5182 #ifndef _SYS_SYSPROTO_H_
5183 struct vop_fid_args {
5190 zfs_freebsd_fid(struct vop_fid_args *ap)
5193 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5197 #ifndef _SYS_SYSPROTO_H_
5198 struct vop_pathconf_args {
5201 register_t *a_retval;
5206 zfs_freebsd_pathconf(struct vop_pathconf_args *ap)
5211 error = zfs_pathconf(ap->a_vp, ap->a_name, &val,
5212 curthread->td_ucred, NULL);
5214 *ap->a_retval = val;
5217 if (error != EOPNOTSUPP)
5220 switch (ap->a_name) {
5222 *ap->a_retval = NAME_MAX;
5224 #if __FreeBSD_version >= 1400032
5225 case _PC_DEALLOC_PRESENT:
5230 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) {
5231 *ap->a_retval = PIPE_BUF;
5236 return (vop_stdpathconf(ap));
5240 static int zfs_xattr_compat = 1;
5243 zfs_check_attrname(const char *name)
5245 /* We don't allow '/' character in attribute name. */
5246 if (strchr(name, '/') != NULL)
5247 return (SET_ERROR(EINVAL));
5248 /* We don't allow attribute names that start with a namespace prefix. */
5249 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5250 return (SET_ERROR(EINVAL));
5255 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5256 * extended attribute name:
5258 * NAMESPACE XATTR_COMPAT PREFIX
5259 * system * freebsd:system:
5260 * user 1 (none, can be used to access ZFS
5261 * fsattr(5) attributes created on Solaris)
5265 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5266 size_t size, boolean_t compat)
5268 const char *namespace, *prefix, *suffix;
5270 memset(attrname, 0, size);
5272 switch (attrnamespace) {
5273 case EXTATTR_NAMESPACE_USER:
5276 * This is the default namespace by which we can access
5277 * all attributes created on Solaris.
5279 prefix = namespace = suffix = "";
5282 * This is compatible with the user namespace encoding
5283 * on Linux prior to xattr_compat, but nothing
5291 case EXTATTR_NAMESPACE_SYSTEM:
5292 prefix = "freebsd:";
5293 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5296 case EXTATTR_NAMESPACE_EMPTY:
5298 return (SET_ERROR(EINVAL));
5300 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5302 return (SET_ERROR(ENAMETOOLONG));
5308 zfs_ensure_xattr_cached(znode_t *zp)
5312 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5314 if (zp->z_xattr_cached != NULL)
5317 if (rw_write_held(&zp->z_xattr_lock))
5318 return (zfs_sa_get_xattr(zp));
5320 if (!rw_tryupgrade(&zp->z_xattr_lock)) {
5321 rw_exit(&zp->z_xattr_lock);
5322 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5324 if (zp->z_xattr_cached == NULL)
5325 error = zfs_sa_get_xattr(zp);
5326 rw_downgrade(&zp->z_xattr_lock);
5330 #ifndef _SYS_SYSPROTO_H_
5331 struct vop_getextattr {
5332 IN struct vnode *a_vp;
5333 IN int a_attrnamespace;
5334 IN const char *a_name;
5335 INOUT struct uio *a_uio;
5337 IN struct ucred *a_cred;
5338 IN struct thread *a_td;
5343 zfs_getextattr_dir(struct vop_getextattr_args *ap, const char *attrname)
5345 struct thread *td = ap->a_td;
5346 struct nameidata nd;
5348 vnode_t *xvp = NULL, *vp;
5351 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5352 LOOKUP_XATTR, B_FALSE);
5357 #if __FreeBSD_version < 1400043
5358 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5361 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5363 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_INVFS, ap->a_cred, NULL);
5365 return (SET_ERROR(error));
5369 if (ap->a_size != NULL) {
5370 error = VOP_GETATTR(vp, &va, ap->a_cred);
5372 *ap->a_size = (size_t)va.va_size;
5373 } else if (ap->a_uio != NULL)
5374 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5377 vn_close(vp, flags, ap->a_cred, td);
5382 zfs_getextattr_sa(struct vop_getextattr_args *ap, const char *attrname)
5384 znode_t *zp = VTOZ(ap->a_vp);
5389 error = zfs_ensure_xattr_cached(zp);
5393 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5394 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5396 error = nvlist_lookup_byte_array(zp->z_xattr_cached, attrname,
5397 &nv_value, &nv_size);
5399 return (SET_ERROR(error));
5401 if (ap->a_size != NULL)
5402 *ap->a_size = nv_size;
5403 else if (ap->a_uio != NULL)
5404 error = uiomove(nv_value, nv_size, ap->a_uio);
5406 return (SET_ERROR(error));
5412 zfs_getextattr_impl(struct vop_getextattr_args *ap, boolean_t compat)
5414 znode_t *zp = VTOZ(ap->a_vp);
5415 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5416 char attrname[EXTATTR_MAXNAMELEN+1];
5419 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5420 sizeof (attrname), compat);
5425 if (zfsvfs->z_use_sa && zp->z_is_sa)
5426 error = zfs_getextattr_sa(ap, attrname);
5427 if (error == ENOENT)
5428 error = zfs_getextattr_dir(ap, attrname);
5433 * Vnode operation to retrieve a named extended attribute.
5436 zfs_getextattr(struct vop_getextattr_args *ap)
5438 znode_t *zp = VTOZ(ap->a_vp);
5439 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5443 * If the xattr property is off, refuse the request.
5445 if (!(zfsvfs->z_flags & ZSB_XATTR))
5446 return (SET_ERROR(EOPNOTSUPP));
5448 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5449 ap->a_cred, ap->a_td, VREAD);
5451 return (SET_ERROR(error));
5453 error = zfs_check_attrname(ap->a_name);
5457 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5460 rw_enter(&zp->z_xattr_lock, RW_READER);
5462 error = zfs_getextattr_impl(ap, zfs_xattr_compat);
5463 if ((error == ENOENT || error == ENOATTR) &&
5464 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5466 * Fall back to the alternate namespace format if we failed to
5467 * find a user xattr.
5469 error = zfs_getextattr_impl(ap, !zfs_xattr_compat);
5472 rw_exit(&zp->z_xattr_lock);
5473 zfs_exit(zfsvfs, FTAG);
5474 if (error == ENOENT)
5475 error = SET_ERROR(ENOATTR);
5479 #ifndef _SYS_SYSPROTO_H_
5480 struct vop_deleteextattr {
5481 IN struct vnode *a_vp;
5482 IN int a_attrnamespace;
5483 IN const char *a_name;
5484 IN struct ucred *a_cred;
5485 IN struct thread *a_td;
5490 zfs_deleteextattr_dir(struct vop_deleteextattr_args *ap, const char *attrname)
5492 struct nameidata nd;
5493 vnode_t *xvp = NULL, *vp;
5496 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5497 LOOKUP_XATTR, B_FALSE);
5501 #if __FreeBSD_version < 1400043
5502 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5503 UIO_SYSSPACE, attrname, xvp, ap->a_td);
5505 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5506 UIO_SYSSPACE, attrname, xvp);
5510 return (SET_ERROR(error));
5513 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5517 if (vp == nd.ni_dvp)
5526 zfs_deleteextattr_sa(struct vop_deleteextattr_args *ap, const char *attrname)
5528 znode_t *zp = VTOZ(ap->a_vp);
5532 error = zfs_ensure_xattr_cached(zp);
5536 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5537 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5539 nvl = zp->z_xattr_cached;
5540 error = nvlist_remove(nvl, attrname, DATA_TYPE_BYTE_ARRAY);
5542 error = SET_ERROR(error);
5544 error = zfs_sa_set_xattr(zp, attrname, NULL, 0);
5546 zp->z_xattr_cached = NULL;
5553 zfs_deleteextattr_impl(struct vop_deleteextattr_args *ap, boolean_t compat)
5555 znode_t *zp = VTOZ(ap->a_vp);
5556 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5557 char attrname[EXTATTR_MAXNAMELEN+1];
5560 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5561 sizeof (attrname), compat);
5566 if (zfsvfs->z_use_sa && zp->z_is_sa)
5567 error = zfs_deleteextattr_sa(ap, attrname);
5568 if (error == ENOENT)
5569 error = zfs_deleteextattr_dir(ap, attrname);
5574 * Vnode operation to remove a named attribute.
5577 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5579 znode_t *zp = VTOZ(ap->a_vp);
5580 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5584 * If the xattr property is off, refuse the request.
5586 if (!(zfsvfs->z_flags & ZSB_XATTR))
5587 return (SET_ERROR(EOPNOTSUPP));
5589 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5590 ap->a_cred, ap->a_td, VWRITE);
5592 return (SET_ERROR(error));
5594 error = zfs_check_attrname(ap->a_name);
5598 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5600 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5602 error = zfs_deleteextattr_impl(ap, zfs_xattr_compat);
5603 if ((error == ENOENT || error == ENOATTR) &&
5604 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5606 * Fall back to the alternate namespace format if we failed to
5607 * find a user xattr.
5609 error = zfs_deleteextattr_impl(ap, !zfs_xattr_compat);
5612 rw_exit(&zp->z_xattr_lock);
5613 zfs_exit(zfsvfs, FTAG);
5614 if (error == ENOENT)
5615 error = SET_ERROR(ENOATTR);
5619 #ifndef _SYS_SYSPROTO_H_
5620 struct vop_setextattr {
5621 IN struct vnode *a_vp;
5622 IN int a_attrnamespace;
5623 IN const char *a_name;
5624 INOUT struct uio *a_uio;
5625 IN struct ucred *a_cred;
5626 IN struct thread *a_td;
5631 zfs_setextattr_dir(struct vop_setextattr_args *ap, const char *attrname)
5633 struct thread *td = ap->a_td;
5634 struct nameidata nd;
5636 vnode_t *xvp = NULL, *vp;
5639 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5640 LOOKUP_XATTR | CREATE_XATTR_DIR, B_FALSE);
5644 flags = FFLAGS(O_WRONLY | O_CREAT);
5645 #if __FreeBSD_version < 1400043
5646 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp, td);
5648 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5650 error = vn_open_cred(&nd, &flags, 0600, VN_OPEN_INVFS, ap->a_cred,
5653 return (SET_ERROR(error));
5659 error = VOP_SETATTR(vp, &va, ap->a_cred);
5661 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5664 vn_close(vp, flags, ap->a_cred, td);
5669 zfs_setextattr_sa(struct vop_setextattr_args *ap, const char *attrname)
5671 znode_t *zp = VTOZ(ap->a_vp);
5676 error = zfs_ensure_xattr_cached(zp);
5680 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5681 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5683 nvl = zp->z_xattr_cached;
5684 size_t entry_size = ap->a_uio->uio_resid;
5685 if (entry_size > DXATTR_MAX_ENTRY_SIZE)
5686 return (SET_ERROR(EFBIG));
5687 error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
5689 return (SET_ERROR(error));
5690 if (sa_size > DXATTR_MAX_SA_SIZE)
5691 return (SET_ERROR(EFBIG));
5692 uchar_t *buf = kmem_alloc(entry_size, KM_SLEEP);
5693 error = uiomove(buf, entry_size, ap->a_uio);
5695 error = SET_ERROR(error);
5697 error = nvlist_add_byte_array(nvl, attrname, buf, entry_size);
5699 error = SET_ERROR(error);
5702 error = zfs_sa_set_xattr(zp, attrname, buf, entry_size);
5703 kmem_free(buf, entry_size);
5705 zp->z_xattr_cached = NULL;
5712 zfs_setextattr_impl(struct vop_setextattr_args *ap, boolean_t compat)
5714 znode_t *zp = VTOZ(ap->a_vp);
5715 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5716 char attrname[EXTATTR_MAXNAMELEN+1];
5719 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5720 sizeof (attrname), compat);
5724 struct vop_deleteextattr_args vda = {
5726 .a_attrnamespace = ap->a_attrnamespace,
5727 .a_name = ap->a_name,
5728 .a_cred = ap->a_cred,
5732 if (zfsvfs->z_use_sa && zp->z_is_sa && zfsvfs->z_xattr_sa) {
5733 error = zfs_setextattr_sa(ap, attrname);
5736 * Successfully put into SA, we need to clear the one
5737 * in dir if present.
5739 zfs_deleteextattr_dir(&vda, attrname);
5743 error = zfs_setextattr_dir(ap, attrname);
5744 if (error == 0 && zp->z_is_sa) {
5746 * Successfully put into dir, we need to clear the one
5749 zfs_deleteextattr_sa(&vda, attrname);
5752 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5754 * Also clear all versions of the alternate compat name.
5756 zfs_deleteextattr_impl(&vda, !compat);
5762 * Vnode operation to set a named attribute.
5765 zfs_setextattr(struct vop_setextattr_args *ap)
5767 znode_t *zp = VTOZ(ap->a_vp);
5768 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5772 * If the xattr property is off, refuse the request.
5774 if (!(zfsvfs->z_flags & ZSB_XATTR))
5775 return (SET_ERROR(EOPNOTSUPP));
5777 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5778 ap->a_cred, ap->a_td, VWRITE);
5780 return (SET_ERROR(error));
5782 error = zfs_check_attrname(ap->a_name);
5786 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5788 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5790 error = zfs_setextattr_impl(ap, zfs_xattr_compat);
5792 rw_exit(&zp->z_xattr_lock);
5793 zfs_exit(zfsvfs, FTAG);
5797 #ifndef _SYS_SYSPROTO_H_
5798 struct vop_listextattr {
5799 IN struct vnode *a_vp;
5800 IN int a_attrnamespace;
5801 INOUT struct uio *a_uio;
5803 IN struct ucred *a_cred;
5804 IN struct thread *a_td;
5809 zfs_listextattr_dir(struct vop_listextattr_args *ap, const char *attrprefix)
5811 struct thread *td = ap->a_td;
5812 struct nameidata nd;
5813 uint8_t dirbuf[sizeof (struct dirent)];
5816 vnode_t *xvp = NULL, *vp;
5819 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5820 LOOKUP_XATTR, B_FALSE);
5823 * ENOATTR means that the EA directory does not yet exist,
5824 * i.e. there are no extended attributes there.
5826 if (error == ENOATTR)
5831 #if __FreeBSD_version < 1400043
5832 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5833 UIO_SYSSPACE, ".", xvp, td);
5835 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5836 UIO_SYSSPACE, ".", xvp);
5840 return (SET_ERROR(error));
5844 auio.uio_iov = &aiov;
5845 auio.uio_iovcnt = 1;
5846 auio.uio_segflg = UIO_SYSSPACE;
5848 auio.uio_rw = UIO_READ;
5849 auio.uio_offset = 0;
5851 size_t plen = strlen(attrprefix);
5854 aiov.iov_base = (void *)dirbuf;
5855 aiov.iov_len = sizeof (dirbuf);
5856 auio.uio_resid = sizeof (dirbuf);
5857 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5860 int done = sizeof (dirbuf) - auio.uio_resid;
5861 for (int pos = 0; pos < done; ) {
5862 struct dirent *dp = (struct dirent *)(dirbuf + pos);
5863 pos += dp->d_reclen;
5865 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5866 * is what we get when attribute was created on Solaris.
5868 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5870 else if (plen == 0 &&
5871 ZFS_XA_NS_PREFIX_FORBIDDEN(dp->d_name))
5873 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5875 uint8_t nlen = dp->d_namlen - plen;
5876 if (ap->a_size != NULL) {
5877 *ap->a_size += 1 + nlen;
5878 } else if (ap->a_uio != NULL) {
5880 * Format of extattr name entry is one byte for
5881 * length and the rest for name.
5883 error = uiomove(&nlen, 1, ap->a_uio);
5885 char *namep = dp->d_name + plen;
5886 error = uiomove(namep, nlen, ap->a_uio);
5889 error = SET_ERROR(error);
5894 } while (!eof && error == 0);
5901 zfs_listextattr_sa(struct vop_listextattr_args *ap, const char *attrprefix)
5903 znode_t *zp = VTOZ(ap->a_vp);
5906 error = zfs_ensure_xattr_cached(zp);
5910 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5911 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5913 size_t plen = strlen(attrprefix);
5914 nvpair_t *nvp = NULL;
5915 while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) {
5916 ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY);
5918 const char *name = nvpair_name(nvp);
5919 if (plen == 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5921 else if (strncmp(name, attrprefix, plen) != 0)
5923 uint8_t nlen = strlen(name) - plen;
5924 if (ap->a_size != NULL) {
5925 *ap->a_size += 1 + nlen;
5926 } else if (ap->a_uio != NULL) {
5928 * Format of extattr name entry is one byte for
5929 * length and the rest for name.
5931 error = uiomove(&nlen, 1, ap->a_uio);
5933 char *namep = __DECONST(char *, name) + plen;
5934 error = uiomove(namep, nlen, ap->a_uio);
5937 error = SET_ERROR(error);
5947 zfs_listextattr_impl(struct vop_listextattr_args *ap, boolean_t compat)
5949 znode_t *zp = VTOZ(ap->a_vp);
5950 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5951 char attrprefix[16];
5954 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5955 sizeof (attrprefix), compat);
5959 if (zfsvfs->z_use_sa && zp->z_is_sa)
5960 error = zfs_listextattr_sa(ap, attrprefix);
5962 error = zfs_listextattr_dir(ap, attrprefix);
5967 * Vnode operation to retrieve extended attributes on a vnode.
5970 zfs_listextattr(struct vop_listextattr_args *ap)
5972 znode_t *zp = VTOZ(ap->a_vp);
5973 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5976 if (ap->a_size != NULL)
5980 * If the xattr property is off, refuse the request.
5982 if (!(zfsvfs->z_flags & ZSB_XATTR))
5983 return (SET_ERROR(EOPNOTSUPP));
5985 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5986 ap->a_cred, ap->a_td, VREAD);
5988 return (SET_ERROR(error));
5990 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5992 rw_enter(&zp->z_xattr_lock, RW_READER);
5994 error = zfs_listextattr_impl(ap, zfs_xattr_compat);
5995 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5996 /* Also list user xattrs with the alternate format. */
5997 error = zfs_listextattr_impl(ap, !zfs_xattr_compat);
6000 rw_exit(&zp->z_xattr_lock);
6001 zfs_exit(zfsvfs, FTAG);
6005 #ifndef _SYS_SYSPROTO_H_
6006 struct vop_getacl_args {
6016 zfs_freebsd_getacl(struct vop_getacl_args *ap)
6019 vsecattr_t vsecattr;
6021 if (ap->a_type != ACL_TYPE_NFS4)
6024 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6025 if ((error = zfs_getsecattr(VTOZ(ap->a_vp),
6026 &vsecattr, 0, ap->a_cred)))
6029 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp,
6030 vsecattr.vsa_aclcnt);
6031 if (vsecattr.vsa_aclentp != NULL)
6032 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6037 #ifndef _SYS_SYSPROTO_H_
6038 struct vop_setacl_args {
6048 zfs_freebsd_setacl(struct vop_setacl_args *ap)
6051 vsecattr_t vsecattr;
6052 int aclbsize; /* size of acl list in bytes */
6055 if (ap->a_type != ACL_TYPE_NFS4)
6058 if (ap->a_aclp == NULL)
6061 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6065 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6066 * splitting every entry into two and appending "canonical six"
6067 * entries at the end. Don't allow for setting an ACL that would
6068 * cause chmod(2) to run out of ACL entries.
6070 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6073 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6077 vsecattr.vsa_mask = VSA_ACE;
6078 aclbsize = ap->a_aclp->acl_cnt * sizeof (ace_t);
6079 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6080 aaclp = vsecattr.vsa_aclentp;
6081 vsecattr.vsa_aclentsz = aclbsize;
6083 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6084 error = zfs_setsecattr(VTOZ(ap->a_vp), &vsecattr, 0, ap->a_cred);
6085 kmem_free(aaclp, aclbsize);
6090 #ifndef _SYS_SYSPROTO_H_
6091 struct vop_aclcheck_args {
6101 zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap)
6104 return (EOPNOTSUPP);
6108 zfs_vptocnp(struct vop_vptocnp_args *ap)
6110 vnode_t *covered_vp;
6111 vnode_t *vp = ap->a_vp;
6112 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
6113 znode_t *zp = VTOZ(vp);
6117 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6121 * If we are a snapshot mounted under .zfs, run the operation
6122 * on the covered vnode.
6124 if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) {
6125 char name[MAXNAMLEN + 1];
6129 error = zfs_znode_parent_and_name(zp, &dzp, name);
6132 if (*ap->a_buflen < len)
6133 error = SET_ERROR(ENOMEM);
6136 *ap->a_buflen -= len;
6137 memcpy(ap->a_buf + *ap->a_buflen, name, len);
6138 *ap->a_vpp = ZTOV(dzp);
6140 zfs_exit(zfsvfs, FTAG);
6143 zfs_exit(zfsvfs, FTAG);
6145 covered_vp = vp->v_mount->mnt_vnodecovered;
6146 #if __FreeBSD_version >= 1300045
6147 enum vgetstate vs = vget_prep(covered_vp);
6151 ltype = VOP_ISLOCKED(vp);
6153 #if __FreeBSD_version >= 1300045
6154 error = vget_finish(covered_vp, LK_SHARED, vs);
6156 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
6159 #if __FreeBSD_version >= 1300123
6160 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_buf,
6163 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
6164 ap->a_buf, ap->a_buflen);
6168 vn_lock(vp, ltype | LK_RETRY);
6169 if (VN_IS_DOOMED(vp))
6170 error = SET_ERROR(ENOENT);
6174 #if __FreeBSD_version >= 1400032
6176 zfs_deallocate(struct vop_deallocate_args *ap)
6178 znode_t *zp = VTOZ(ap->a_vp);
6179 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6181 off_t off, len, file_sz;
6184 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6188 * Callers might not be able to detect properly that we are read-only,
6189 * so check it explicitly here.
6191 if (zfs_is_readonly(zfsvfs)) {
6192 zfs_exit(zfsvfs, FTAG);
6193 return (SET_ERROR(EROFS));
6196 zilog = zfsvfs->z_log;
6197 off = *ap->a_offset;
6199 file_sz = zp->z_size;
6200 if (off + len > file_sz)
6201 len = file_sz - off;
6202 /* Fast path for out-of-range request. */
6205 zfs_exit(zfsvfs, FTAG);
6209 error = zfs_freesp(zp, off, len, O_RDWR, TRUE);
6211 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS ||
6212 (ap->a_ioflag & IO_SYNC) != 0)
6213 zil_commit(zilog, zp->z_id);
6214 *ap->a_offset = off + len;
6218 zfs_exit(zfsvfs, FTAG);
6223 #if __FreeBSD_version >= 1300039
6224 #ifndef _SYS_SYSPROTO_H_
6225 struct vop_copy_file_range_args {
6226 struct vnode *a_invp;
6228 struct vnode *a_outvp;
6231 unsigned int a_flags;
6232 struct ucred *a_incred;
6233 struct ucred *a_outcred;
6234 struct thread *a_fsizetd;
6238 * TODO: FreeBSD will only call file system-specific copy_file_range() if both
6239 * files resides under the same mountpoint. In case of ZFS we want to be called
6240 * even is files are in different datasets (but on the same pools, but we need
6241 * to check that ourselves).
6244 zfs_freebsd_copy_file_range(struct vop_copy_file_range_args *ap)
6246 zfsvfs_t *outzfsvfs;
6247 struct vnode *invp = ap->a_invp;
6248 struct vnode *outvp = ap->a_outvp;
6252 uint64_t len = *ap->a_lenp;
6254 if (!zfs_bclone_enabled) {
6256 goto bad_write_fallback;
6260 * TODO: If offset/length is not aligned to recordsize, use
6261 * vn_generic_copy_file_range() on this fragment.
6262 * It would be better to do this after we lock the vnodes, but then we
6263 * need something else than vn_generic_copy_file_range().
6266 vn_start_write(outvp, &mp, V_WAIT);
6267 if (__predict_true(mp == outvp->v_mount)) {
6268 outzfsvfs = (zfsvfs_t *)mp->mnt_data;
6269 if (!spa_feature_is_enabled(dmu_objset_spa(outzfsvfs->z_os),
6270 SPA_FEATURE_BLOCK_CLONING)) {
6271 goto bad_write_fallback;
6274 if (invp == outvp) {
6275 if (vn_lock(outvp, LK_EXCLUSIVE) != 0) {
6276 goto bad_write_fallback;
6279 #if (__FreeBSD_version >= 1302506 && __FreeBSD_version < 1400000) || \
6280 __FreeBSD_version >= 1400086
6281 vn_lock_pair(invp, false, LK_EXCLUSIVE, outvp, false,
6284 vn_lock_pair(invp, false, outvp, false);
6286 if (VN_IS_DOOMED(invp) || VN_IS_DOOMED(outvp)) {
6287 goto bad_locked_fallback;
6292 error = mac_vnode_check_write(curthread->td_ucred, ap->a_outcred,
6298 io.uio_offset = *ap->a_outoffp;
6299 io.uio_resid = *ap->a_lenp;
6300 error = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
6304 error = zfs_clone_range(VTOZ(invp), ap->a_inoffp, VTOZ(outvp),
6305 ap->a_outoffp, &len, ap->a_outcred);
6306 if (error == EXDEV || error == EAGAIN || error == EINVAL ||
6307 error == EOPNOTSUPP)
6308 goto bad_locked_fallback;
6309 *ap->a_lenp = (size_t)len;
6315 vn_finished_write(mp);
6318 bad_locked_fallback:
6324 vn_finished_write(mp);
6325 error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
6326 ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
6327 ap->a_incred, ap->a_outcred, ap->a_fsizetd);
6332 struct vop_vector zfs_vnodeops;
6333 struct vop_vector zfs_fifoops;
6334 struct vop_vector zfs_shareops;
6336 struct vop_vector zfs_vnodeops = {
6337 .vop_default = &default_vnodeops,
6338 .vop_inactive = zfs_freebsd_inactive,
6339 #if __FreeBSD_version >= 1300042
6340 .vop_need_inactive = zfs_freebsd_need_inactive,
6342 .vop_reclaim = zfs_freebsd_reclaim,
6343 #if __FreeBSD_version >= 1300102
6344 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6346 #if __FreeBSD_version >= 1300139
6347 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6349 .vop_access = zfs_freebsd_access,
6350 .vop_allocate = VOP_EINVAL,
6351 #if __FreeBSD_version >= 1400032
6352 .vop_deallocate = zfs_deallocate,
6354 .vop_lookup = zfs_cache_lookup,
6355 .vop_cachedlookup = zfs_freebsd_cachedlookup,
6356 .vop_getattr = zfs_freebsd_getattr,
6357 .vop_setattr = zfs_freebsd_setattr,
6358 .vop_create = zfs_freebsd_create,
6359 .vop_mknod = (vop_mknod_t *)zfs_freebsd_create,
6360 .vop_mkdir = zfs_freebsd_mkdir,
6361 .vop_readdir = zfs_freebsd_readdir,
6362 .vop_fsync = zfs_freebsd_fsync,
6363 .vop_open = zfs_freebsd_open,
6364 .vop_close = zfs_freebsd_close,
6365 .vop_rmdir = zfs_freebsd_rmdir,
6366 .vop_ioctl = zfs_freebsd_ioctl,
6367 .vop_link = zfs_freebsd_link,
6368 .vop_symlink = zfs_freebsd_symlink,
6369 .vop_readlink = zfs_freebsd_readlink,
6370 .vop_read = zfs_freebsd_read,
6371 .vop_write = zfs_freebsd_write,
6372 .vop_remove = zfs_freebsd_remove,
6373 .vop_rename = zfs_freebsd_rename,
6374 .vop_pathconf = zfs_freebsd_pathconf,
6375 .vop_bmap = zfs_freebsd_bmap,
6376 .vop_fid = zfs_freebsd_fid,
6377 .vop_getextattr = zfs_getextattr,
6378 .vop_deleteextattr = zfs_deleteextattr,
6379 .vop_setextattr = zfs_setextattr,
6380 .vop_listextattr = zfs_listextattr,
6381 .vop_getacl = zfs_freebsd_getacl,
6382 .vop_setacl = zfs_freebsd_setacl,
6383 .vop_aclcheck = zfs_freebsd_aclcheck,
6384 .vop_getpages = zfs_freebsd_getpages,
6385 .vop_putpages = zfs_freebsd_putpages,
6386 .vop_vptocnp = zfs_vptocnp,
6387 #if __FreeBSD_version >= 1300064
6388 .vop_lock1 = vop_lock,
6389 .vop_unlock = vop_unlock,
6390 .vop_islocked = vop_islocked,
6392 #if __FreeBSD_version >= 1400043
6393 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6395 #if __FreeBSD_version >= 1300039
6396 .vop_copy_file_range = zfs_freebsd_copy_file_range,
6399 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops);
6401 struct vop_vector zfs_fifoops = {
6402 .vop_default = &fifo_specops,
6403 .vop_fsync = zfs_freebsd_fsync,
6404 #if __FreeBSD_version >= 1300102
6405 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6407 #if __FreeBSD_version >= 1300139
6408 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6410 .vop_access = zfs_freebsd_access,
6411 .vop_getattr = zfs_freebsd_getattr,
6412 .vop_inactive = zfs_freebsd_inactive,
6413 .vop_read = VOP_PANIC,
6414 .vop_reclaim = zfs_freebsd_reclaim,
6415 .vop_setattr = zfs_freebsd_setattr,
6416 .vop_write = VOP_PANIC,
6417 .vop_pathconf = zfs_freebsd_pathconf,
6418 .vop_fid = zfs_freebsd_fid,
6419 .vop_getacl = zfs_freebsd_getacl,
6420 .vop_setacl = zfs_freebsd_setacl,
6421 .vop_aclcheck = zfs_freebsd_aclcheck,
6422 #if __FreeBSD_version >= 1400043
6423 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6426 VFS_VOP_VECTOR_REGISTER(zfs_fifoops);
6429 * special share hidden files vnode operations template
6431 struct vop_vector zfs_shareops = {
6432 .vop_default = &default_vnodeops,
6433 #if __FreeBSD_version >= 1300121
6434 .vop_fplookup_vexec = VOP_EAGAIN,
6436 #if __FreeBSD_version >= 1300139
6437 .vop_fplookup_symlink = VOP_EAGAIN,
6439 .vop_access = zfs_freebsd_access,
6440 .vop_inactive = zfs_freebsd_inactive,
6441 .vop_reclaim = zfs_freebsd_reclaim,
6442 .vop_fid = zfs_freebsd_fid,
6443 .vop_pathconf = zfs_freebsd_pathconf,
6444 #if __FreeBSD_version >= 1400043
6445 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6448 VFS_VOP_VECTOR_REGISTER(zfs_shareops);
6450 ZFS_MODULE_PARAM(zfs, zfs_, xattr_compat, INT, ZMOD_RW,
6451 "Use legacy ZFS xattr naming for writing new user namespace xattrs");