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 */
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysmacros.h>
38 #include <sys/resource.h>
40 #include <sys/endian.h>
42 #include <sys/vnode.h>
43 #if __FreeBSD_version >= 1300102
46 #include <sys/dirent.h>
50 #include <sys/taskq.h>
52 #include <sys/atomic.h>
53 #include <sys/namei.h>
55 #include <sys/cmn_err.h>
57 #include <sys/sysproto.h>
58 #include <sys/errno.h>
59 #include <sys/unistd.h>
60 #include <sys/zfs_dir.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/fs/zfs.h>
64 #include <sys/dmu_objset.h>
70 #include <sys/policy.h>
71 #include <sys/sunddi.h>
72 #include <sys/filio.h>
74 #include <sys/zfs_ctldir.h>
75 #include <sys/zfs_fuid.h>
76 #include <sys/zfs_quota.h>
77 #include <sys/zfs_sa.h>
78 #include <sys/zfs_rlock.h>
81 #include <sys/sched.h>
83 #include <sys/vmmeter.h>
84 #include <vm/vm_param.h>
86 #include <sys/zfs_vnops.h>
88 #include <vm/vm_object.h>
90 #include <sys/extattr.h>
94 #define VN_OPEN_INVFS 0x0
99 #if __FreeBSD_version < 1300103
100 #define NDFREE_PNBUF(ndp) NDFREE((ndp), NDF_ONLY_PNBUF)
103 #if __FreeBSD_version >= 1300047
104 #define vm_page_wire_lock(pp)
105 #define vm_page_wire_unlock(pp)
107 #define vm_page_wire_lock(pp) vm_page_lock(pp)
108 #define vm_page_wire_unlock(pp) vm_page_unlock(pp)
111 #ifdef DEBUG_VFS_LOCKS
112 #define VNCHECKREF(vp) \
113 VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp, \
114 ("%s: wrong ref counts", __func__));
116 #define VNCHECKREF(vp)
119 #if __FreeBSD_version >= 1400045
120 typedef uint64_t cookie_t;
122 typedef ulong_t cookie_t;
128 * Each vnode op performs some logical unit of work. To do this, the ZPL must
129 * properly lock its in-core state, create a DMU transaction, do the work,
130 * record this work in the intent log (ZIL), commit the DMU transaction,
131 * and wait for the intent log to commit if it is a synchronous operation.
132 * Moreover, the vnode ops must work in both normal and log replay context.
133 * The ordering of events is important to avoid deadlocks and references
134 * to freed memory. The example below illustrates the following Big Rules:
136 * (1) A check must be made in each zfs thread for a mounted file system.
137 * This is done avoiding races using zfs_enter(zfsvfs).
138 * A zfs_exit(zfsvfs) is needed before all returns. Any znodes
139 * must be checked with zfs_verify_zp(zp). Both of these macros
140 * can return EIO from the calling function.
142 * (2) VN_RELE() should always be the last thing except for zil_commit()
143 * (if necessary) and zfs_exit(). This is for 3 reasons:
144 * First, if it's the last reference, the vnode/znode
145 * can be freed, so the zp may point to freed memory. Second, the last
146 * reference will call zfs_zinactive(), which may induce a lot of work --
147 * pushing cached pages (which acquires range locks) and syncing out
148 * cached atime changes. Third, zfs_zinactive() may require a new tx,
149 * which could deadlock the system if you were already holding one.
150 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
152 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
153 * as they can span dmu_tx_assign() calls.
155 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
156 * dmu_tx_assign(). This is critical because we don't want to block
157 * while holding locks.
159 * If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT. This
160 * reduces lock contention and CPU usage when we must wait (note that if
161 * throughput is constrained by the storage, nearly every transaction
164 * Note, in particular, that if a lock is sometimes acquired before
165 * the tx assigns, and sometimes after (e.g. z_lock), then failing
166 * to use a non-blocking assign can deadlock the system. The scenario:
168 * Thread A has grabbed a lock before calling dmu_tx_assign().
169 * Thread B is in an already-assigned tx, and blocks for this lock.
170 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
171 * forever, because the previous txg can't quiesce until B's tx commits.
173 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
174 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
175 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
176 * to indicate that this operation has already called dmu_tx_wait().
177 * This will ensure that we don't retry forever, waiting a short bit
180 * (5) If the operation succeeded, generate the intent log entry for it
181 * before dropping locks. This ensures that the ordering of events
182 * in the intent log matches the order in which they actually occurred.
183 * During ZIL replay the zfs_log_* functions will update the sequence
184 * number to indicate the zil transaction has replayed.
186 * (6) At the end of each vnode op, the DMU tx must always commit,
187 * regardless of whether there were any errors.
189 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
190 * to ensure that synchronous semantics are provided when necessary.
192 * In general, this is how things should be ordered in each vnode op:
194 * zfs_enter(zfsvfs); // exit if unmounted
196 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
197 * rw_enter(...); // grab any other locks you need
198 * tx = dmu_tx_create(...); // get DMU tx
199 * dmu_tx_hold_*(); // hold each object you might modify
200 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
202 * rw_exit(...); // drop locks
203 * zfs_dirent_unlock(dl); // unlock directory entry
204 * VN_RELE(...); // release held vnodes
205 * if (error == ERESTART) {
211 * dmu_tx_abort(tx); // abort DMU tx
212 * zfs_exit(zfsvfs); // finished in zfs
213 * return (error); // really out of space
215 * error = do_real_work(); // do whatever this VOP does
217 * zfs_log_*(...); // on success, make ZIL entry
218 * dmu_tx_commit(tx); // commit DMU tx -- error or not
219 * rw_exit(...); // drop locks
220 * zfs_dirent_unlock(dl); // unlock directory entry
221 * VN_RELE(...); // release held vnodes
222 * zil_commit(zilog, foid); // synchronous when necessary
223 * zfs_exit(zfsvfs); // finished in zfs
224 * return (error); // done, report error
227 zfs_open(vnode_t **vpp, int flag, cred_t *cr)
230 znode_t *zp = VTOZ(*vpp);
231 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
234 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
237 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
238 ((flag & FAPPEND) == 0)) {
239 zfs_exit(zfsvfs, FTAG);
240 return (SET_ERROR(EPERM));
243 /* Keep a count of the synchronous opens in the znode */
245 atomic_inc_32(&zp->z_sync_cnt);
247 zfs_exit(zfsvfs, FTAG);
252 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
254 (void) offset, (void) cr;
255 znode_t *zp = VTOZ(vp);
256 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
259 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
262 /* Decrement the synchronous opens in the znode */
263 if ((flag & O_SYNC) && (count == 1))
264 atomic_dec_32(&zp->z_sync_cnt);
266 zfs_exit(zfsvfs, FTAG);
271 zfs_ioctl(vnode_t *vp, ulong_t com, intptr_t data, int flag, cred_t *cred,
274 (void) flag, (void) cred, (void) rvalp;
284 * The following two ioctls are used by bfu. Faking out,
285 * necessary to avoid bfu errors.
297 off = *(offset_t *)data;
298 /* offset parameter is in/out */
299 error = zfs_holey(VTOZ(vp), com, &off);
302 *(offset_t *)data = off;
306 return (SET_ERROR(ENOTTY));
310 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
317 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
318 * aligned boundaries, if the range is not aligned. As a result a
319 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
320 * It may happen that all DEV_BSIZE subranges are marked clean and thus
321 * the whole page would be considered clean despite have some
323 * For this reason we should shrink the range to DEV_BSIZE aligned
324 * boundaries before calling vm_page_clear_dirty.
326 end = rounddown2(off + nbytes, DEV_BSIZE);
327 off = roundup2(off, DEV_BSIZE);
331 zfs_vmobject_assert_wlocked_12(obj);
332 #if __FreeBSD_version < 1300050
334 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
336 if (vm_page_xbusied(pp)) {
338 * Reference the page before unlocking and
339 * sleeping so that the page daemon is less
340 * likely to reclaim it.
342 vm_page_reference(pp);
344 zfs_vmobject_wunlock(obj);
345 vm_page_busy_sleep(pp, "zfsmwb", true);
346 zfs_vmobject_wlock(obj);
350 } else if (pp != NULL) {
355 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
356 vm_object_pip_add(obj, 1);
357 pmap_remove_write(pp);
359 vm_page_clear_dirty(pp, off, nbytes);
364 vm_page_grab_valid_unlocked(&pp, obj, OFF_TO_IDX(start),
365 VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_NORMAL |
368 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
369 vm_object_pip_add(obj, 1);
370 pmap_remove_write(pp);
372 vm_page_clear_dirty(pp, off, nbytes);
379 page_unbusy(vm_page_t pp)
383 #if __FreeBSD_version >= 1300041
384 vm_object_pip_wakeup(pp->object);
386 vm_object_pip_subtract(pp->object, 1);
390 #if __FreeBSD_version > 1300051
392 page_hold(vnode_t *vp, int64_t start)
398 vm_page_grab_valid_unlocked(&m, obj, OFF_TO_IDX(start),
399 VM_ALLOC_NOCREAT | VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY |
405 page_hold(vnode_t *vp, int64_t start)
411 zfs_vmobject_assert_wlocked(obj);
414 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
416 if (vm_page_xbusied(pp)) {
418 * Reference the page before unlocking and
419 * sleeping so that the page daemon is less
420 * likely to reclaim it.
422 vm_page_reference(pp);
424 zfs_vmobject_wunlock(obj);
425 vm_page_busy_sleep(pp, "zfsmwb", true);
426 zfs_vmobject_wlock(obj);
430 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
431 vm_page_wire_lock(pp);
433 vm_page_wire_unlock(pp);
444 page_unhold(vm_page_t pp)
447 vm_page_wire_lock(pp);
448 #if __FreeBSD_version >= 1300035
449 vm_page_unwire(pp, PQ_ACTIVE);
453 vm_page_wire_unlock(pp);
457 * When a file is memory mapped, we must keep the IO data synchronized
458 * between the DMU cache and the memory mapped pages. What this means:
460 * On Write: If we find a memory mapped page, we write to *both*
461 * the page and the dmu buffer.
464 update_pages(znode_t *zp, int64_t start, int len, objset_t *os)
468 vnode_t *vp = ZTOV(zp);
472 ASSERT3P(vp->v_mount, !=, NULL);
474 ASSERT3P(obj, !=, NULL);
476 off = start & PAGEOFFSET;
477 zfs_vmobject_wlock_12(obj);
478 #if __FreeBSD_version >= 1300041
479 vm_object_pip_add(obj, 1);
481 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
483 int nbytes = imin(PAGESIZE - off, len);
485 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
486 zfs_vmobject_wunlock_12(obj);
488 va = zfs_map_page(pp, &sf);
489 (void) dmu_read(os, zp->z_id, start + off, nbytes,
490 va + off, DMU_READ_PREFETCH);
493 zfs_vmobject_wlock_12(obj);
499 #if __FreeBSD_version >= 1300041
500 vm_object_pip_wakeup(obj);
502 vm_object_pip_wakeupn(obj, 0);
504 zfs_vmobject_wunlock_12(obj);
508 * Read with UIO_NOCOPY flag means that sendfile(2) requests
509 * ZFS to populate a range of page cache pages with data.
511 * NOTE: this function could be optimized to pre-allocate
512 * all pages in advance, drain exclusive busy on all of them,
513 * map them into contiguous KVA region and populate them
514 * in one single dmu_read() call.
517 mappedread_sf(znode_t *zp, int nbytes, zfs_uio_t *uio)
519 vnode_t *vp = ZTOV(zp);
520 objset_t *os = zp->z_zfsvfs->z_os;
529 ASSERT3U(zfs_uio_segflg(uio), ==, UIO_NOCOPY);
530 ASSERT3P(vp->v_mount, !=, NULL);
532 ASSERT3P(obj, !=, NULL);
533 ASSERT0(zfs_uio_offset(uio) & PAGEOFFSET);
535 zfs_vmobject_wlock_12(obj);
536 for (start = zfs_uio_offset(uio); len > 0; start += PAGESIZE) {
537 int bytes = MIN(PAGESIZE, len);
539 pp = vm_page_grab_unlocked(obj, OFF_TO_IDX(start),
540 VM_ALLOC_SBUSY | VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
541 if (vm_page_none_valid(pp)) {
542 zfs_vmobject_wunlock_12(obj);
543 va = zfs_map_page(pp, &sf);
544 error = dmu_read(os, zp->z_id, start, bytes, va,
546 if (bytes != PAGESIZE && error == 0)
547 memset(va + bytes, 0, PAGESIZE - bytes);
549 zfs_vmobject_wlock_12(obj);
550 #if __FreeBSD_version >= 1300081
553 vm_page_activate(pp);
554 vm_page_do_sunbusy(pp);
556 zfs_vmobject_wlock(obj);
557 if (!vm_page_wired(pp) && pp->valid == 0 &&
558 vm_page_busy_tryupgrade(pp))
562 zfs_vmobject_wunlock(obj);
565 vm_page_do_sunbusy(pp);
568 if (pp->wire_count == 0 && pp->valid == 0 &&
572 pp->valid = VM_PAGE_BITS_ALL;
573 vm_page_activate(pp);
578 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
579 vm_page_do_sunbusy(pp);
583 zfs_uio_advance(uio, bytes);
586 zfs_vmobject_wunlock_12(obj);
591 * When a file is memory mapped, we must keep the IO data synchronized
592 * between the DMU cache and the memory mapped pages. What this means:
594 * On Read: We "read" preferentially from memory mapped pages,
595 * else we default from the dmu buffer.
597 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
598 * the file is memory mapped.
601 mappedread(znode_t *zp, int nbytes, zfs_uio_t *uio)
603 vnode_t *vp = ZTOV(zp);
610 ASSERT3P(vp->v_mount, !=, NULL);
612 ASSERT3P(obj, !=, NULL);
614 start = zfs_uio_offset(uio);
615 off = start & PAGEOFFSET;
616 zfs_vmobject_wlock_12(obj);
617 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
619 uint64_t bytes = MIN(PAGESIZE - off, len);
621 if ((pp = page_hold(vp, start))) {
625 zfs_vmobject_wunlock_12(obj);
626 va = zfs_map_page(pp, &sf);
627 error = vn_io_fault_uiomove(va + off, bytes,
628 GET_UIO_STRUCT(uio));
630 zfs_vmobject_wlock_12(obj);
633 zfs_vmobject_wunlock_12(obj);
634 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
636 zfs_vmobject_wlock_12(obj);
643 zfs_vmobject_wunlock_12(obj);
648 zfs_write_simple(znode_t *zp, const void *data, size_t len,
649 loff_t pos, size_t *presid)
654 error = vn_rdwr(UIO_WRITE, ZTOV(zp), __DECONST(void *, data), len, pos,
655 UIO_SYSSPACE, IO_SYNC, kcred, NOCRED, &resid, curthread);
658 return (SET_ERROR(error));
659 } else if (presid == NULL) {
661 error = SET_ERROR(EIO);
670 zfs_zrele_async(znode_t *zp)
672 vnode_t *vp = ZTOV(zp);
673 objset_t *os = ITOZSB(vp)->z_os;
675 VN_RELE_ASYNC(vp, dsl_pool_zrele_taskq(dmu_objset_pool(os)));
679 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
684 error = vn_lock(*vpp, lkflags);
691 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
693 znode_t *zdp = VTOZ(dvp);
694 zfsvfs_t *zfsvfs __unused = zdp->z_zfsvfs;
698 if (zfsvfs->z_replay == B_FALSE)
699 ASSERT_VOP_LOCKED(dvp, __func__);
701 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
702 ASSERT3P(dvp, ==, vp);
704 ltype = lkflags & LK_TYPE_MASK;
705 if (ltype != VOP_ISLOCKED(dvp)) {
706 if (ltype == LK_EXCLUSIVE)
707 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
708 else /* if (ltype == LK_SHARED) */
709 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
712 * Relock for the "." case could leave us with
715 if (VN_IS_DOOMED(dvp)) {
717 return (SET_ERROR(ENOENT));
721 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
723 * Note that in this case, dvp is the child vnode, and we
724 * are looking up the parent vnode - exactly reverse from
725 * normal operation. Unlocking dvp requires some rather
726 * tricky unlock/relock dance to prevent mp from being freed;
727 * use vn_vget_ino_gen() which takes care of all that.
729 * XXX Note that there is a time window when both vnodes are
730 * unlocked. It is possible, although highly unlikely, that
731 * during that window the parent-child relationship between
732 * the vnodes may change, for example, get reversed.
733 * In that case we would have a wrong lock order for the vnodes.
734 * All other filesystems seem to ignore this problem, so we
736 * A potential solution could be implemented as follows:
737 * - using LK_NOWAIT when locking the second vnode and retrying
739 * - checking that the parent-child relationship still holds
740 * after locking both vnodes and retrying if it doesn't
742 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
745 error = vn_lock(vp, lkflags);
753 * Lookup an entry in a directory, or an extended attribute directory.
754 * If it exists, return a held vnode reference for it.
756 * IN: dvp - vnode of directory to search.
757 * nm - name of entry to lookup.
758 * pnp - full pathname to lookup [UNUSED].
759 * flags - LOOKUP_XATTR set if looking for an attribute.
760 * rdir - root directory vnode [UNUSED].
761 * cr - credentials of caller.
762 * ct - caller context
764 * OUT: vpp - vnode of located entry, NULL if not found.
766 * RETURN: 0 on success, error code on failure.
772 zfs_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp,
773 struct componentname *cnp, int nameiop, cred_t *cr, int flags,
776 znode_t *zdp = VTOZ(dvp);
778 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
779 #if __FreeBSD_version > 1300124
785 * Fast path lookup, however we must skip DNLC lookup
786 * for case folding or normalizing lookups because the
787 * DNLC code only stores the passed in name. This means
788 * creating 'a' and removing 'A' on a case insensitive
789 * file system would work, but DNLC still thinks 'a'
790 * exists and won't let you create it again on the next
791 * pass through fast path.
793 if (!(flags & LOOKUP_XATTR)) {
794 if (dvp->v_type != VDIR) {
795 return (SET_ERROR(ENOTDIR));
796 } else if (zdp->z_sa_hdl == NULL) {
797 return (SET_ERROR(EIO));
801 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp,
804 if ((error = zfs_enter_verify_zp(zfsvfs, zdp, FTAG)) != 0)
807 #if __FreeBSD_version > 1300124
808 dvp_seqc = vn_seqc_read_notmodify(dvp);
813 if (flags & LOOKUP_XATTR) {
815 * If the xattr property is off, refuse the lookup request.
817 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
818 zfs_exit(zfsvfs, FTAG);
819 return (SET_ERROR(EOPNOTSUPP));
823 * We don't allow recursive attributes..
824 * Maybe someday we will.
826 if (zdp->z_pflags & ZFS_XATTR) {
827 zfs_exit(zfsvfs, FTAG);
828 return (SET_ERROR(EINVAL));
831 if ((error = zfs_get_xattrdir(VTOZ(dvp), &zp, cr, flags))) {
832 zfs_exit(zfsvfs, FTAG);
838 * Do we have permission to get into attribute directory?
840 error = zfs_zaccess(zp, ACE_EXECUTE, 0, B_FALSE, cr, NULL);
845 zfs_exit(zfsvfs, FTAG);
850 * Check accessibility of directory if we're not coming in via
855 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
856 cnp->cn_flags &= ~NOEXECCHECK;
859 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr,
861 zfs_exit(zfsvfs, FTAG);
866 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
867 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
868 zfs_exit(zfsvfs, FTAG);
869 return (SET_ERROR(EILSEQ));
874 * First handle the special cases.
876 if ((cnp->cn_flags & ISDOTDOT) != 0) {
878 * If we are a snapshot mounted under .zfs, return
879 * the vp for the snapshot directory.
881 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
882 struct componentname cn;
886 zfs_exit(zfsvfs, FTAG);
887 ltype = VOP_ISLOCKED(dvp);
889 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
892 cn.cn_nameptr = "snapshot";
893 cn.cn_namelen = strlen(cn.cn_nameptr);
894 cn.cn_nameiop = cnp->cn_nameiop;
895 cn.cn_flags = cnp->cn_flags & ~ISDOTDOT;
896 cn.cn_lkflags = cnp->cn_lkflags;
897 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
900 vn_lock(dvp, ltype | LK_RETRY);
904 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
905 zfs_exit(zfsvfs, FTAG);
906 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
907 return (SET_ERROR(ENOTSUP));
908 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
913 * The loop is retry the lookup if the parent-child relationship
914 * changes during the dot-dot locking complexities.
919 error = zfs_dirlook(zdp, nm, &zp);
923 zfs_exit(zfsvfs, FTAG);
927 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
930 * If we've got a locking error, then the vnode
931 * got reclaimed because of a force unmount.
932 * We never enter doomed vnodes into the name cache.
938 if ((cnp->cn_flags & ISDOTDOT) == 0)
941 if ((error = zfs_enter(zfsvfs, FTAG)) != 0) {
946 if (zdp->z_sa_hdl == NULL) {
947 error = SET_ERROR(EIO);
949 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
950 &parent, sizeof (parent));
953 zfs_exit(zfsvfs, FTAG);
957 if (zp->z_id == parent) {
958 zfs_exit(zfsvfs, FTAG);
967 /* Translate errors and add SAVENAME when needed. */
968 if (cnp->cn_flags & ISLASTCN) {
972 if (error == ENOENT) {
974 #if __FreeBSD_version < 1400068
975 cnp->cn_flags |= SAVENAME;
981 #if __FreeBSD_version < 1400068
983 cnp->cn_flags |= SAVENAME;
989 #if __FreeBSD_version > 1300124
990 if ((cnp->cn_flags & ISDOTDOT) != 0) {
992 * FIXME: zfs_lookup_lock relocks vnodes and does nothing to
993 * handle races. In particular different callers may end up
994 * with different vnodes and will try to add conflicting
995 * entries to the namecache.
997 * While finding different result may be acceptable in face
998 * of concurrent modification, adding conflicting entries
999 * trips over an assert in the namecache.
1001 * Ultimately let an entry through once everything settles.
1003 if (!vn_seqc_consistent(dvp, dvp_seqc)) {
1004 cnp->cn_flags &= ~MAKEENTRY;
1009 /* Insert name into cache (as non-existent) if appropriate. */
1010 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1011 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1012 cache_enter(dvp, NULL, cnp);
1014 /* Insert name into cache if appropriate. */
1015 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1016 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1017 if (!(cnp->cn_flags & ISLASTCN) ||
1018 (nameiop != DELETE && nameiop != RENAME)) {
1019 cache_enter(dvp, *vpp, cnp);
1027 * Attempt to create a new entry in a directory. If the entry
1028 * already exists, truncate the file if permissible, else return
1029 * an error. Return the vp of the created or trunc'd file.
1031 * IN: dvp - vnode of directory to put new file entry in.
1032 * name - name of new file entry.
1033 * vap - attributes of new file.
1034 * excl - flag indicating exclusive or non-exclusive mode.
1035 * mode - mode to open file with.
1036 * cr - credentials of caller.
1037 * flag - large file flag [UNUSED].
1038 * ct - caller context
1039 * vsecp - ACL to be set
1040 * mnt_ns - Unused on FreeBSD
1042 * OUT: vpp - vnode of created or trunc'd entry.
1044 * RETURN: 0 on success, error code on failure.
1047 * dvp - ctime|mtime updated if new entry created
1048 * vp - ctime|mtime always, atime if new
1051 zfs_create(znode_t *dzp, const char *name, vattr_t *vap, int excl, int mode,
1052 znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp, zuserns_t *mnt_ns)
1054 (void) excl, (void) mode, (void) flag;
1056 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1061 uid_t uid = crgetuid(cr);
1062 gid_t gid = crgetgid(cr);
1063 uint64_t projid = ZFS_DEFAULT_PROJID;
1064 zfs_acl_ids_t acl_ids;
1065 boolean_t fuid_dirtied;
1067 #ifdef DEBUG_VFS_LOCKS
1068 vnode_t *dvp = ZTOV(dzp);
1072 * If we have an ephemeral id, ACL, or XVATTR then
1073 * make sure file system is at proper version
1075 if (zfsvfs->z_use_fuids == B_FALSE &&
1076 (vsecp || (vap->va_mask & AT_XVATTR) ||
1077 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1078 return (SET_ERROR(EINVAL));
1080 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1083 zilog = zfsvfs->z_log;
1085 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1086 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1087 zfs_exit(zfsvfs, FTAG);
1088 return (SET_ERROR(EILSEQ));
1091 if (vap->va_mask & AT_XVATTR) {
1092 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1093 crgetuid(cr), cr, vap->va_type)) != 0) {
1094 zfs_exit(zfsvfs, FTAG);
1101 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1102 vap->va_mode &= ~S_ISVTX;
1104 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1106 zfs_exit(zfsvfs, FTAG);
1109 ASSERT3P(zp, ==, NULL);
1112 * Create a new file object and update the directory
1115 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
1120 * We only support the creation of regular files in
1121 * extended attribute directories.
1124 if ((dzp->z_pflags & ZFS_XATTR) &&
1125 (vap->va_type != VREG)) {
1126 error = SET_ERROR(EINVAL);
1130 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1131 cr, vsecp, &acl_ids, NULL)) != 0)
1134 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
1135 projid = zfs_inherit_projid(dzp);
1136 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1137 zfs_acl_ids_free(&acl_ids);
1138 error = SET_ERROR(EDQUOT);
1142 getnewvnode_reserve_();
1144 tx = dmu_tx_create(os);
1146 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1147 ZFS_SA_BASE_ATTR_SIZE);
1149 fuid_dirtied = zfsvfs->z_fuid_dirty;
1151 zfs_fuid_txhold(zfsvfs, tx);
1152 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1153 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1154 if (!zfsvfs->z_use_sa &&
1155 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1156 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1157 0, acl_ids.z_aclp->z_acl_bytes);
1159 error = dmu_tx_assign(tx, TXG_WAIT);
1161 zfs_acl_ids_free(&acl_ids);
1163 getnewvnode_drop_reserve();
1164 zfs_exit(zfsvfs, FTAG);
1167 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1169 zfs_fuid_sync(zfsvfs, tx);
1171 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1172 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1173 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1174 vsecp, acl_ids.z_fuidp, vap);
1175 zfs_acl_ids_free(&acl_ids);
1178 getnewvnode_drop_reserve();
1186 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1187 zil_commit(zilog, 0);
1189 zfs_exit(zfsvfs, FTAG);
1194 * Remove an entry from a directory.
1196 * IN: dvp - vnode of directory to remove entry from.
1197 * name - name of entry to remove.
1198 * cr - credentials of caller.
1199 * ct - caller context
1200 * flags - case flags
1202 * RETURN: 0 on success, error code on failure.
1206 * vp - ctime (if nlink > 0)
1209 zfs_remove_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1211 znode_t *dzp = VTOZ(dvp);
1214 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1224 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1227 if ((error = zfs_verify_zp(zp)) != 0) {
1228 zfs_exit(zfsvfs, FTAG);
1231 zilog = zfsvfs->z_log;
1236 if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) {
1241 * Need to use rmdir for removing directories.
1243 if (vp->v_type == VDIR) {
1244 error = SET_ERROR(EPERM);
1248 vnevent_remove(vp, dvp, name, ct);
1252 /* are there any extended attributes? */
1253 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1254 &xattr_obj, sizeof (xattr_obj));
1255 if (error == 0 && xattr_obj) {
1256 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1261 * We may delete the znode now, or we may put it in the unlinked set;
1262 * it depends on whether we're the last link, and on whether there are
1263 * other holds on the vnode. So we dmu_tx_hold() the right things to
1264 * allow for either case.
1266 tx = dmu_tx_create(zfsvfs->z_os);
1267 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1268 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1269 zfs_sa_upgrade_txholds(tx, zp);
1270 zfs_sa_upgrade_txholds(tx, dzp);
1273 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1274 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1277 /* charge as an update -- would be nice not to charge at all */
1278 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1281 * Mark this transaction as typically resulting in a net free of space
1283 dmu_tx_mark_netfree(tx);
1285 error = dmu_tx_assign(tx, TXG_WAIT);
1288 zfs_exit(zfsvfs, FTAG);
1293 * Remove the directory entry.
1295 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
1303 zfs_unlinked_add(zp, tx);
1304 vp->v_vflag |= VV_NOSYNC;
1306 /* XXX check changes to linux vnops */
1308 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
1316 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1317 zil_commit(zilog, 0);
1320 zfs_exit(zfsvfs, FTAG);
1326 zfs_lookup_internal(znode_t *dzp, const char *name, vnode_t **vpp,
1327 struct componentname *cnp, int nameiop)
1329 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1332 cnp->cn_nameptr = __DECONST(char *, name);
1333 cnp->cn_namelen = strlen(name);
1334 cnp->cn_nameiop = nameiop;
1335 cnp->cn_flags = ISLASTCN;
1336 #if __FreeBSD_version < 1400068
1337 cnp->cn_flags |= SAVENAME;
1339 cnp->cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
1340 cnp->cn_cred = kcred;
1341 #if __FreeBSD_version < 1400037
1342 cnp->cn_thread = curthread;
1345 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay) {
1346 struct vop_lookup_args a;
1348 a.a_gen.a_desc = &vop_lookup_desc;
1349 a.a_dvp = ZTOV(dzp);
1352 error = vfs_cache_lookup(&a);
1354 error = zfs_lookup(ZTOV(dzp), name, vpp, cnp, nameiop, kcred, 0,
1359 printf("got error %d on name %s on op %d\n", error, name,
1368 zfs_remove(znode_t *dzp, const char *name, cred_t *cr, int flags)
1372 struct componentname cn;
1374 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1377 error = zfs_remove_(ZTOV(dzp), vp, name, cr);
1382 * Create a new directory and insert it into dvp using the name
1383 * provided. Return a pointer to the inserted directory.
1385 * IN: dvp - vnode of directory to add subdir to.
1386 * dirname - name of new directory.
1387 * vap - attributes of new directory.
1388 * cr - credentials of caller.
1389 * ct - caller context
1390 * flags - case flags
1391 * vsecp - ACL to be set
1392 * mnt_ns - Unused on FreeBSD
1394 * OUT: vpp - vnode of created directory.
1396 * RETURN: 0 on success, error code on failure.
1399 * dvp - ctime|mtime updated
1400 * vp - ctime|mtime|atime updated
1403 zfs_mkdir(znode_t *dzp, const char *dirname, vattr_t *vap, znode_t **zpp,
1404 cred_t *cr, int flags, vsecattr_t *vsecp, zuserns_t *mnt_ns)
1406 (void) flags, (void) vsecp;
1408 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1413 uid_t uid = crgetuid(cr);
1414 gid_t gid = crgetgid(cr);
1415 zfs_acl_ids_t acl_ids;
1416 boolean_t fuid_dirtied;
1418 ASSERT3U(vap->va_type, ==, VDIR);
1421 * If we have an ephemeral id, ACL, or XVATTR then
1422 * make sure file system is at proper version
1424 if (zfsvfs->z_use_fuids == B_FALSE &&
1425 ((vap->va_mask & AT_XVATTR) ||
1426 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1427 return (SET_ERROR(EINVAL));
1429 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1431 zilog = zfsvfs->z_log;
1433 if (dzp->z_pflags & ZFS_XATTR) {
1434 zfs_exit(zfsvfs, FTAG);
1435 return (SET_ERROR(EINVAL));
1438 if (zfsvfs->z_utf8 && u8_validate(dirname,
1439 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1440 zfs_exit(zfsvfs, FTAG);
1441 return (SET_ERROR(EILSEQ));
1444 if (vap->va_mask & AT_XVATTR) {
1445 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1446 crgetuid(cr), cr, vap->va_type)) != 0) {
1447 zfs_exit(zfsvfs, FTAG);
1452 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1453 NULL, &acl_ids, NULL)) != 0) {
1454 zfs_exit(zfsvfs, FTAG);
1459 * First make sure the new directory doesn't exist.
1461 * Existence is checked first to make sure we don't return
1462 * EACCES instead of EEXIST which can cause some applications
1467 if ((error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW))) {
1468 zfs_acl_ids_free(&acl_ids);
1469 zfs_exit(zfsvfs, FTAG);
1472 ASSERT3P(zp, ==, NULL);
1474 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr,
1476 zfs_acl_ids_free(&acl_ids);
1477 zfs_exit(zfsvfs, FTAG);
1481 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
1482 zfs_acl_ids_free(&acl_ids);
1483 zfs_exit(zfsvfs, FTAG);
1484 return (SET_ERROR(EDQUOT));
1488 * Add a new entry to the directory.
1490 getnewvnode_reserve_();
1491 tx = dmu_tx_create(zfsvfs->z_os);
1492 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1493 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1494 fuid_dirtied = zfsvfs->z_fuid_dirty;
1496 zfs_fuid_txhold(zfsvfs, tx);
1497 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1498 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1499 acl_ids.z_aclp->z_acl_bytes);
1502 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1503 ZFS_SA_BASE_ATTR_SIZE);
1505 error = dmu_tx_assign(tx, TXG_WAIT);
1507 zfs_acl_ids_free(&acl_ids);
1509 getnewvnode_drop_reserve();
1510 zfs_exit(zfsvfs, FTAG);
1517 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1520 zfs_fuid_sync(zfsvfs, tx);
1523 * Now put new name in parent dir.
1525 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
1529 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
1530 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
1531 acl_ids.z_fuidp, vap);
1533 zfs_acl_ids_free(&acl_ids);
1537 getnewvnode_drop_reserve();
1539 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1540 zil_commit(zilog, 0);
1542 zfs_exit(zfsvfs, FTAG);
1546 #if __FreeBSD_version < 1300124
1548 cache_vop_rmdir(struct vnode *dvp, struct vnode *vp)
1557 * Remove a directory subdir entry. If the current working
1558 * directory is the same as the subdir to be removed, the
1561 * IN: dvp - vnode of directory to remove from.
1562 * name - name of directory to be removed.
1563 * cwd - vnode of current working directory.
1564 * cr - credentials of caller.
1565 * ct - caller context
1566 * flags - case flags
1568 * RETURN: 0 on success, error code on failure.
1571 * dvp - ctime|mtime updated
1574 zfs_rmdir_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1576 znode_t *dzp = VTOZ(dvp);
1577 znode_t *zp = VTOZ(vp);
1578 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1583 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
1585 if ((error = zfs_verify_zp(zp)) != 0) {
1586 zfs_exit(zfsvfs, FTAG);
1589 zilog = zfsvfs->z_log;
1592 if ((error = zfs_zaccess_delete(dzp, zp, cr, NULL))) {
1596 if (vp->v_type != VDIR) {
1597 error = SET_ERROR(ENOTDIR);
1601 vnevent_rmdir(vp, dvp, name, ct);
1603 tx = dmu_tx_create(zfsvfs->z_os);
1604 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1605 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1606 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1607 zfs_sa_upgrade_txholds(tx, zp);
1608 zfs_sa_upgrade_txholds(tx, dzp);
1609 dmu_tx_mark_netfree(tx);
1610 error = dmu_tx_assign(tx, TXG_WAIT);
1613 zfs_exit(zfsvfs, FTAG);
1617 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
1620 uint64_t txtype = TX_RMDIR;
1621 zfs_log_remove(zilog, tx, txtype, dzp, name,
1622 ZFS_NO_OBJECT, B_FALSE);
1627 cache_vop_rmdir(dvp, vp);
1629 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1630 zil_commit(zilog, 0);
1632 zfs_exit(zfsvfs, FTAG);
1637 zfs_rmdir(znode_t *dzp, const char *name, znode_t *cwd, cred_t *cr, int flags)
1639 struct componentname cn;
1643 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
1646 error = zfs_rmdir_(ZTOV(dzp), vp, name, cr);
1652 * Read as many directory entries as will fit into the provided
1653 * buffer from the given directory cursor position (specified in
1654 * the uio structure).
1656 * IN: vp - vnode of directory to read.
1657 * uio - structure supplying read location, range info,
1658 * and return buffer.
1659 * cr - credentials of caller.
1660 * ct - caller context
1662 * OUT: uio - updated offset and range, buffer filled.
1663 * eofp - set to true if end-of-file detected.
1664 * ncookies- number of entries in cookies
1665 * cookies - offsets to directory entries
1667 * RETURN: 0 on success, error code on failure.
1670 * vp - atime updated
1672 * Note that the low 4 bits of the cookie returned by zap is always zero.
1673 * This allows us to use the low range for "special" directory entries:
1674 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1675 * we use the offset 2 for the '.zfs' directory.
1678 zfs_readdir(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, int *eofp,
1679 int *ncookies, cookie_t **cookies)
1681 znode_t *zp = VTOZ(vp);
1684 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1689 zap_attribute_t zap;
1690 uint_t bytes_wanted;
1691 uint64_t offset; /* must be unsigned; checks for < 1 */
1699 cookie_t *cooks = NULL;
1701 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1704 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1705 &parent, sizeof (parent))) != 0) {
1706 zfs_exit(zfsvfs, FTAG);
1711 * If we are not given an eof variable,
1718 * Check for valid iov_len.
1720 if (GET_UIO_STRUCT(uio)->uio_iov->iov_len <= 0) {
1721 zfs_exit(zfsvfs, FTAG);
1722 return (SET_ERROR(EINVAL));
1726 * Quit if directory has been removed (posix)
1728 if ((*eofp = zp->z_unlinked) != 0) {
1729 zfs_exit(zfsvfs, FTAG);
1735 offset = zfs_uio_offset(uio);
1736 prefetch = zp->z_zn_prefetch;
1739 * Initialize the iterator cursor.
1743 * Start iteration from the beginning of the directory.
1745 zap_cursor_init(&zc, os, zp->z_id);
1748 * The offset is a serialized cursor.
1750 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1754 * Get space to change directory entries into fs independent format.
1756 iovp = GET_UIO_STRUCT(uio)->uio_iov;
1757 bytes_wanted = iovp->iov_len;
1758 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1) {
1759 bufsize = bytes_wanted;
1760 outbuf = kmem_alloc(bufsize, KM_SLEEP);
1761 odp = (struct dirent64 *)outbuf;
1763 bufsize = bytes_wanted;
1765 odp = (struct dirent64 *)iovp->iov_base;
1768 if (ncookies != NULL) {
1770 * Minimum entry size is dirent size and 1 byte for a file name.
1772 ncooks = zfs_uio_resid(uio) / (sizeof (struct dirent) -
1773 sizeof (((struct dirent *)NULL)->d_name) + 1);
1774 cooks = malloc(ncooks * sizeof (*cooks), M_TEMP, M_WAITOK);
1780 * Transform to file-system independent format
1783 while (outcount < bytes_wanted) {
1786 off64_t *next = NULL;
1789 * Special case `.', `..', and `.zfs'.
1792 (void) strcpy(zap.za_name, ".");
1793 zap.za_normalization_conflict = 0;
1796 } else if (offset == 1) {
1797 (void) strcpy(zap.za_name, "..");
1798 zap.za_normalization_conflict = 0;
1801 } else if (offset == 2 && zfs_show_ctldir(zp)) {
1802 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1803 zap.za_normalization_conflict = 0;
1804 objnum = ZFSCTL_INO_ROOT;
1810 if ((error = zap_cursor_retrieve(&zc, &zap))) {
1811 if ((*eofp = (error == ENOENT)) != 0)
1817 if (zap.za_integer_length != 8 ||
1818 zap.za_num_integers != 1) {
1819 cmn_err(CE_WARN, "zap_readdir: bad directory "
1820 "entry, obj = %lld, offset = %lld\n",
1821 (u_longlong_t)zp->z_id,
1822 (u_longlong_t)offset);
1823 error = SET_ERROR(ENXIO);
1827 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
1829 * MacOS X can extract the object type here such as:
1830 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1832 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1835 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1838 * Will this entry fit in the buffer?
1840 if (outcount + reclen > bufsize) {
1842 * Did we manage to fit anything in the buffer?
1845 error = SET_ERROR(EINVAL);
1853 odp->d_ino = objnum;
1854 odp->d_reclen = reclen;
1855 odp->d_namlen = strlen(zap.za_name);
1856 /* NOTE: d_off is the offset for the *next* entry. */
1858 strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
1860 dirent_terminate(odp);
1861 odp = (dirent64_t *)((intptr_t)odp + reclen);
1865 ASSERT3S(outcount, <=, bufsize);
1867 /* Prefetch znode */
1869 dmu_prefetch(os, objnum, 0, 0, 0,
1870 ZIO_PRIORITY_SYNC_READ);
1873 * Move to the next entry, fill in the previous offset.
1875 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
1876 zap_cursor_advance(&zc);
1877 offset = zap_cursor_serialize(&zc);
1882 /* Fill the offset right after advancing the cursor. */
1885 if (cooks != NULL) {
1888 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
1891 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
1893 /* Subtract unused cookies */
1894 if (ncookies != NULL)
1895 *ncookies -= ncooks;
1897 if (zfs_uio_segflg(uio) == UIO_SYSSPACE && zfs_uio_iovcnt(uio) == 1) {
1898 iovp->iov_base += outcount;
1899 iovp->iov_len -= outcount;
1900 zfs_uio_resid(uio) -= outcount;
1902 zfs_uiomove(outbuf, (long)outcount, UIO_READ, uio))) {
1904 * Reset the pointer.
1906 offset = zfs_uio_offset(uio);
1910 zap_cursor_fini(&zc);
1911 if (zfs_uio_segflg(uio) != UIO_SYSSPACE || zfs_uio_iovcnt(uio) != 1)
1912 kmem_free(outbuf, bufsize);
1914 if (error == ENOENT)
1917 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
1919 zfs_uio_setoffset(uio, offset);
1920 zfs_exit(zfsvfs, FTAG);
1921 if (error != 0 && cookies != NULL) {
1922 free(*cookies, M_TEMP);
1930 * Get the requested file attributes and place them in the provided
1933 * IN: vp - vnode of file.
1934 * vap - va_mask identifies requested attributes.
1935 * If AT_XVATTR set, then optional attrs are requested
1936 * flags - ATTR_NOACLCHECK (CIFS server context)
1937 * cr - credentials of caller.
1939 * OUT: vap - attribute values.
1941 * RETURN: 0 (always succeeds).
1944 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1946 znode_t *zp = VTOZ(vp);
1947 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1950 u_longlong_t nblocks;
1951 uint64_t mtime[2], ctime[2], crtime[2], rdev;
1952 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
1953 xoptattr_t *xoap = NULL;
1954 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
1955 sa_bulk_attr_t bulk[4];
1958 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
1961 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
1963 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
1964 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
1965 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
1966 if (vp->v_type == VBLK || vp->v_type == VCHR)
1967 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
1970 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
1971 zfs_exit(zfsvfs, FTAG);
1976 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1977 * Also, if we are the owner don't bother, since owner should
1978 * always be allowed to read basic attributes of file.
1980 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
1981 (vap->va_uid != crgetuid(cr))) {
1982 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
1983 skipaclchk, cr, NULL))) {
1984 zfs_exit(zfsvfs, FTAG);
1990 * Return all attributes. It's cheaper to provide the answer
1991 * than to determine whether we were asked the question.
1994 vap->va_type = IFTOVT(zp->z_mode);
1995 vap->va_mode = zp->z_mode & ~S_IFMT;
1997 vap->va_nodeid = zp->z_id;
1998 vap->va_nlink = zp->z_links;
1999 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp) &&
2000 zp->z_links < ZFS_LINK_MAX)
2002 vap->va_size = zp->z_size;
2003 if (vp->v_type == VBLK || vp->v_type == VCHR)
2004 vap->va_rdev = zfs_cmpldev(rdev);
2005 vap->va_gen = zp->z_gen;
2006 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2007 vap->va_filerev = zp->z_seq;
2010 * Add in any requested optional attributes and the create time.
2011 * Also set the corresponding bits in the returned attribute bitmap.
2013 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2014 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2016 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2017 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2020 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2021 xoap->xoa_readonly =
2022 ((zp->z_pflags & ZFS_READONLY) != 0);
2023 XVA_SET_RTN(xvap, XAT_READONLY);
2026 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2028 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2029 XVA_SET_RTN(xvap, XAT_SYSTEM);
2032 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2034 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2035 XVA_SET_RTN(xvap, XAT_HIDDEN);
2038 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2039 xoap->xoa_nounlink =
2040 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2041 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2044 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2045 xoap->xoa_immutable =
2046 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2047 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2050 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2051 xoap->xoa_appendonly =
2052 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2053 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2056 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2058 ((zp->z_pflags & ZFS_NODUMP) != 0);
2059 XVA_SET_RTN(xvap, XAT_NODUMP);
2062 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2064 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2065 XVA_SET_RTN(xvap, XAT_OPAQUE);
2068 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2069 xoap->xoa_av_quarantined =
2070 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2071 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2074 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2075 xoap->xoa_av_modified =
2076 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2077 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2080 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2081 vp->v_type == VREG) {
2082 zfs_sa_get_scanstamp(zp, xvap);
2085 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2086 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2087 XVA_SET_RTN(xvap, XAT_REPARSE);
2089 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2090 xoap->xoa_generation = zp->z_gen;
2091 XVA_SET_RTN(xvap, XAT_GEN);
2094 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2096 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2097 XVA_SET_RTN(xvap, XAT_OFFLINE);
2100 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2102 ((zp->z_pflags & ZFS_SPARSE) != 0);
2103 XVA_SET_RTN(xvap, XAT_SPARSE);
2106 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2107 xoap->xoa_projinherit =
2108 ((zp->z_pflags & ZFS_PROJINHERIT) != 0);
2109 XVA_SET_RTN(xvap, XAT_PROJINHERIT);
2112 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2113 xoap->xoa_projid = zp->z_projid;
2114 XVA_SET_RTN(xvap, XAT_PROJID);
2118 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2119 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2120 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2121 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2124 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2125 vap->va_blksize = blksize;
2126 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2128 if (zp->z_blksz == 0) {
2130 * Block size hasn't been set; suggest maximal I/O transfers.
2132 vap->va_blksize = zfsvfs->z_max_blksz;
2135 zfs_exit(zfsvfs, FTAG);
2140 * Set the file attributes to the values contained in the
2143 * IN: zp - znode of file to be modified.
2144 * vap - new attribute values.
2145 * If AT_XVATTR set, then optional attrs are being set
2146 * flags - ATTR_UTIME set if non-default time values provided.
2147 * - ATTR_NOACLCHECK (CIFS context only).
2148 * cr - credentials of caller.
2149 * mnt_ns - Unused on FreeBSD
2151 * RETURN: 0 on success, error code on failure.
2154 * vp - ctime updated, mtime updated if size changed.
2157 zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr, zuserns_t *mnt_ns)
2159 vnode_t *vp = ZTOV(zp);
2160 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2166 uint_t mask = vap->va_mask;
2167 uint_t saved_mask = 0;
2168 uint64_t saved_mode;
2171 uint64_t new_uid, new_gid;
2173 uint64_t mtime[2], ctime[2];
2174 uint64_t projid = ZFS_INVALID_PROJID;
2176 int need_policy = FALSE;
2178 zfs_fuid_info_t *fuidp = NULL;
2179 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2182 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2183 boolean_t fuid_dirtied = B_FALSE;
2184 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2185 int count = 0, xattr_count = 0;
2190 if (mask & AT_NOSET)
2191 return (SET_ERROR(EINVAL));
2193 if ((err = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
2197 zilog = zfsvfs->z_log;
2200 * Make sure that if we have ephemeral uid/gid or xvattr specified
2201 * that file system is at proper version level
2204 if (zfsvfs->z_use_fuids == B_FALSE &&
2205 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2206 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2207 (mask & AT_XVATTR))) {
2208 zfs_exit(zfsvfs, FTAG);
2209 return (SET_ERROR(EINVAL));
2212 if (mask & AT_SIZE && vp->v_type == VDIR) {
2213 zfs_exit(zfsvfs, FTAG);
2214 return (SET_ERROR(EISDIR));
2217 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2218 zfs_exit(zfsvfs, FTAG);
2219 return (SET_ERROR(EINVAL));
2223 * If this is an xvattr_t, then get a pointer to the structure of
2224 * optional attributes. If this is NULL, then we have a vattr_t.
2226 xoap = xva_getxoptattr(xvap);
2228 xva_init(&tmpxvattr);
2231 * Immutable files can only alter immutable bit and atime
2233 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2234 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2235 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2236 zfs_exit(zfsvfs, FTAG);
2237 return (SET_ERROR(EPERM));
2241 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2245 * Verify timestamps doesn't overflow 32 bits.
2246 * ZFS can handle large timestamps, but 32bit syscalls can't
2247 * handle times greater than 2039. This check should be removed
2248 * once large timestamps are fully supported.
2250 if (mask & (AT_ATIME | AT_MTIME)) {
2251 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2252 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2253 zfs_exit(zfsvfs, FTAG);
2254 return (SET_ERROR(EOVERFLOW));
2257 if (xoap != NULL && (mask & AT_XVATTR)) {
2258 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2259 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2260 zfs_exit(zfsvfs, FTAG);
2261 return (SET_ERROR(EOVERFLOW));
2264 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2265 if (!dmu_objset_projectquota_enabled(os) ||
2266 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode))) {
2267 zfs_exit(zfsvfs, FTAG);
2268 return (SET_ERROR(EOPNOTSUPP));
2271 projid = xoap->xoa_projid;
2272 if (unlikely(projid == ZFS_INVALID_PROJID)) {
2273 zfs_exit(zfsvfs, FTAG);
2274 return (SET_ERROR(EINVAL));
2277 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
2278 projid = ZFS_INVALID_PROJID;
2283 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
2284 (xoap->xoa_projinherit !=
2285 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) &&
2286 (!dmu_objset_projectquota_enabled(os) ||
2287 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode)))) {
2288 zfs_exit(zfsvfs, FTAG);
2289 return (SET_ERROR(EOPNOTSUPP));
2296 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2297 zfs_exit(zfsvfs, FTAG);
2298 return (SET_ERROR(EROFS));
2302 * First validate permissions
2305 if (mask & AT_SIZE) {
2307 * XXX - Note, we are not providing any open
2308 * mode flags here (like FNDELAY), so we may
2309 * block if there are locks present... this
2310 * should be addressed in openat().
2312 /* XXX - would it be OK to generate a log record here? */
2313 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2315 zfs_exit(zfsvfs, FTAG);
2320 if (mask & (AT_ATIME|AT_MTIME) ||
2321 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2322 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2323 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2324 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2325 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2326 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2327 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2328 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2329 skipaclchk, cr, mnt_ns);
2332 if (mask & (AT_UID|AT_GID)) {
2333 int idmask = (mask & (AT_UID|AT_GID));
2338 * NOTE: even if a new mode is being set,
2339 * we may clear S_ISUID/S_ISGID bits.
2342 if (!(mask & AT_MODE))
2343 vap->va_mode = zp->z_mode;
2346 * Take ownership or chgrp to group we are a member of
2349 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2350 take_group = (mask & AT_GID) &&
2351 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2354 * If both AT_UID and AT_GID are set then take_owner and
2355 * take_group must both be set in order to allow taking
2358 * Otherwise, send the check through secpolicy_vnode_setattr()
2362 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2363 ((idmask == AT_UID) && take_owner) ||
2364 ((idmask == AT_GID) && take_group)) {
2365 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2366 skipaclchk, cr, mnt_ns) == 0) {
2368 * Remove setuid/setgid for non-privileged users
2370 secpolicy_setid_clear(vap, vp, cr);
2371 trim_mask = (mask & (AT_UID|AT_GID));
2380 oldva.va_mode = zp->z_mode;
2381 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2382 if (mask & AT_XVATTR) {
2384 * Update xvattr mask to include only those attributes
2385 * that are actually changing.
2387 * the bits will be restored prior to actually setting
2388 * the attributes so the caller thinks they were set.
2390 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2391 if (xoap->xoa_appendonly !=
2392 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2395 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2396 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2400 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2401 if (xoap->xoa_projinherit !=
2402 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
2405 XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
2406 XVA_SET_REQ(&tmpxvattr, XAT_PROJINHERIT);
2410 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2411 if (xoap->xoa_nounlink !=
2412 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2415 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2416 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2420 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2421 if (xoap->xoa_immutable !=
2422 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2425 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2426 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2430 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2431 if (xoap->xoa_nodump !=
2432 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2435 XVA_CLR_REQ(xvap, XAT_NODUMP);
2436 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2440 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2441 if (xoap->xoa_av_modified !=
2442 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2445 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2446 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2450 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2451 if ((vp->v_type != VREG &&
2452 xoap->xoa_av_quarantined) ||
2453 xoap->xoa_av_quarantined !=
2454 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2457 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2458 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2462 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2463 zfs_exit(zfsvfs, FTAG);
2464 return (SET_ERROR(EPERM));
2467 if (need_policy == FALSE &&
2468 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2469 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2474 if (mask & AT_MODE) {
2475 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr,
2477 err = secpolicy_setid_setsticky_clear(vp, vap,
2480 zfs_exit(zfsvfs, FTAG);
2483 trim_mask |= AT_MODE;
2491 * If trim_mask is set then take ownership
2492 * has been granted or write_acl is present and user
2493 * has the ability to modify mode. In that case remove
2494 * UID|GID and or MODE from mask so that
2495 * secpolicy_vnode_setattr() doesn't revoke it.
2499 saved_mask = vap->va_mask;
2500 vap->va_mask &= ~trim_mask;
2501 if (trim_mask & AT_MODE) {
2503 * Save the mode, as secpolicy_vnode_setattr()
2504 * will overwrite it with ova.va_mode.
2506 saved_mode = vap->va_mode;
2509 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2510 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2512 zfs_exit(zfsvfs, FTAG);
2517 vap->va_mask |= saved_mask;
2518 if (trim_mask & AT_MODE) {
2520 * Recover the mode after
2521 * secpolicy_vnode_setattr().
2523 vap->va_mode = saved_mode;
2529 * secpolicy_vnode_setattr, or take ownership may have
2532 mask = vap->va_mask;
2534 if ((mask & (AT_UID | AT_GID)) || projid != ZFS_INVALID_PROJID) {
2535 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2536 &xattr_obj, sizeof (xattr_obj));
2538 if (err == 0 && xattr_obj) {
2539 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
2541 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
2543 vrele(ZTOV(attrzp));
2548 if (mask & AT_UID) {
2549 new_uid = zfs_fuid_create(zfsvfs,
2550 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2551 if (new_uid != zp->z_uid &&
2552 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
2556 err = SET_ERROR(EDQUOT);
2561 if (mask & AT_GID) {
2562 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2563 cr, ZFS_GROUP, &fuidp);
2564 if (new_gid != zp->z_gid &&
2565 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
2569 err = SET_ERROR(EDQUOT);
2574 if (projid != ZFS_INVALID_PROJID &&
2575 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
2578 err = SET_ERROR(EDQUOT);
2582 tx = dmu_tx_create(os);
2584 if (mask & AT_MODE) {
2585 uint64_t pmode = zp->z_mode;
2587 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2589 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
2590 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
2591 err = SET_ERROR(EPERM);
2595 if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)))
2598 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2600 * Are we upgrading ACL from old V0 format
2603 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
2604 zfs_znode_acl_version(zp) ==
2605 ZFS_ACL_VERSION_INITIAL) {
2606 dmu_tx_hold_free(tx, acl_obj, 0,
2608 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2609 0, aclp->z_acl_bytes);
2611 dmu_tx_hold_write(tx, acl_obj, 0,
2614 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2615 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2616 0, aclp->z_acl_bytes);
2618 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2620 if (((mask & AT_XVATTR) &&
2621 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
2622 (projid != ZFS_INVALID_PROJID &&
2623 !(zp->z_pflags & ZFS_PROJID)))
2624 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2626 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2630 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2633 fuid_dirtied = zfsvfs->z_fuid_dirty;
2635 zfs_fuid_txhold(zfsvfs, tx);
2637 zfs_sa_upgrade_txholds(tx, zp);
2639 err = dmu_tx_assign(tx, TXG_WAIT);
2645 * Set each attribute requested.
2646 * We group settings according to the locks they need to acquire.
2648 * Note: you cannot set ctime directly, although it will be
2649 * updated as a side-effect of calling this function.
2652 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) {
2654 * For the existed object that is upgraded from old system,
2655 * its on-disk layout has no slot for the project ID attribute.
2656 * But quota accounting logic needs to access related slots by
2657 * offset directly. So we need to adjust old objects' layout
2658 * to make the project ID to some unified and fixed offset.
2661 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid);
2663 err = sa_add_projid(zp->z_sa_hdl, tx, projid);
2665 if (unlikely(err == EEXIST))
2670 projid = ZFS_INVALID_PROJID;
2673 if (mask & (AT_UID|AT_GID|AT_MODE))
2674 mutex_enter(&zp->z_acl_lock);
2676 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
2677 &zp->z_pflags, sizeof (zp->z_pflags));
2680 if (mask & (AT_UID|AT_GID|AT_MODE))
2681 mutex_enter(&attrzp->z_acl_lock);
2682 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2683 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
2684 sizeof (attrzp->z_pflags));
2685 if (projid != ZFS_INVALID_PROJID) {
2686 attrzp->z_projid = projid;
2687 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2688 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid,
2689 sizeof (attrzp->z_projid));
2693 if (mask & (AT_UID|AT_GID)) {
2695 if (mask & AT_UID) {
2696 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2697 &new_uid, sizeof (new_uid));
2698 zp->z_uid = new_uid;
2700 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2701 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
2703 attrzp->z_uid = new_uid;
2707 if (mask & AT_GID) {
2708 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
2709 NULL, &new_gid, sizeof (new_gid));
2710 zp->z_gid = new_gid;
2712 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2713 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
2715 attrzp->z_gid = new_gid;
2718 if (!(mask & AT_MODE)) {
2719 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
2720 NULL, &new_mode, sizeof (new_mode));
2721 new_mode = zp->z_mode;
2723 err = zfs_acl_chown_setattr(zp);
2726 vn_seqc_write_begin(ZTOV(attrzp));
2727 err = zfs_acl_chown_setattr(attrzp);
2728 vn_seqc_write_end(ZTOV(attrzp));
2733 if (mask & AT_MODE) {
2734 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
2735 &new_mode, sizeof (new_mode));
2736 zp->z_mode = new_mode;
2737 ASSERT3P(aclp, !=, NULL);
2738 err = zfs_aclset_common(zp, aclp, cr, tx);
2740 if (zp->z_acl_cached)
2741 zfs_acl_free(zp->z_acl_cached);
2742 zp->z_acl_cached = aclp;
2747 if (mask & AT_ATIME) {
2748 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2749 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
2750 &zp->z_atime, sizeof (zp->z_atime));
2753 if (mask & AT_MTIME) {
2754 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2755 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
2756 mtime, sizeof (mtime));
2759 if (projid != ZFS_INVALID_PROJID) {
2760 zp->z_projid = projid;
2761 SA_ADD_BULK_ATTR(bulk, count,
2762 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid,
2763 sizeof (zp->z_projid));
2766 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2767 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
2768 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
2769 NULL, mtime, sizeof (mtime));
2770 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2771 &ctime, sizeof (ctime));
2772 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
2773 } else if (mask != 0) {
2774 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
2775 &ctime, sizeof (ctime));
2776 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime);
2778 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2779 SA_ZPL_CTIME(zfsvfs), NULL,
2780 &ctime, sizeof (ctime));
2781 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2787 * Do this after setting timestamps to prevent timestamp
2788 * update from toggling bit
2791 if (xoap && (mask & AT_XVATTR)) {
2793 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
2794 xoap->xoa_createtime = vap->va_birthtime;
2796 * restore trimmed off masks
2797 * so that return masks can be set for caller.
2800 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
2801 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2803 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
2804 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2806 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
2807 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2809 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
2810 XVA_SET_REQ(xvap, XAT_NODUMP);
2812 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
2813 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2815 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
2816 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2818 if (XVA_ISSET_REQ(&tmpxvattr, XAT_PROJINHERIT)) {
2819 XVA_SET_REQ(xvap, XAT_PROJINHERIT);
2822 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2823 ASSERT3S(vp->v_type, ==, VREG);
2825 zfs_xvattr_set(zp, xvap, tx);
2829 zfs_fuid_sync(zfsvfs, tx);
2832 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2834 if (mask & (AT_UID|AT_GID|AT_MODE))
2835 mutex_exit(&zp->z_acl_lock);
2838 if (mask & (AT_UID|AT_GID|AT_MODE))
2839 mutex_exit(&attrzp->z_acl_lock);
2842 if (err == 0 && attrzp) {
2843 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2855 zfs_fuid_info_free(fuidp);
2862 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2867 if (os->os_sync == ZFS_SYNC_ALWAYS)
2868 zil_commit(zilog, 0);
2870 zfs_exit(zfsvfs, FTAG);
2875 * Look up the directory entries corresponding to the source and target
2876 * directory/name pairs.
2879 zfs_rename_relock_lookup(znode_t *sdzp, const struct componentname *scnp,
2880 znode_t **szpp, znode_t *tdzp, const struct componentname *tcnp,
2888 * Before using sdzp and tdzp we must ensure that they are live.
2889 * As a porting legacy from illumos we have two things to worry
2890 * about. One is typical for FreeBSD and it is that the vnode is
2891 * not reclaimed (doomed). The other is that the znode is live.
2892 * The current code can invalidate the znode without acquiring the
2893 * corresponding vnode lock if the object represented by the znode
2894 * and vnode is no longer valid after a rollback or receive operation.
2895 * z_teardown_lock hidden behind zfs_enter and zfs_exit is the lock
2896 * that protects the znodes from the invalidation.
2898 zfsvfs = sdzp->z_zfsvfs;
2899 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
2900 if ((error = zfs_enter_verify_zp(zfsvfs, sdzp, FTAG)) != 0)
2902 if ((error = zfs_verify_zp(tdzp)) != 0) {
2903 zfs_exit(zfsvfs, FTAG);
2908 * Re-resolve svp to be certain it still exists and fetch the
2911 error = zfs_dirent_lookup(sdzp, scnp->cn_nameptr, &szp, ZEXISTS);
2913 /* Source entry invalid or not there. */
2914 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
2915 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
2916 error = SET_ERROR(EINVAL);
2922 * Re-resolve tvp, if it disappeared we just carry on.
2924 error = zfs_dirent_lookup(tdzp, tcnp->cn_nameptr, &tzp, 0);
2927 if ((tcnp->cn_flags & ISDOTDOT) != 0)
2928 error = SET_ERROR(EINVAL);
2933 zfs_exit(zfsvfs, FTAG);
2938 * We acquire all but fdvp locks using non-blocking acquisitions. If we
2939 * fail to acquire any lock in the path we will drop all held locks,
2940 * acquire the new lock in a blocking fashion, and then release it and
2941 * restart the rename. This acquire/release step ensures that we do not
2942 * spin on a lock waiting for release. On error release all vnode locks
2943 * and decrement references the way tmpfs_rename() would do.
2946 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
2947 struct vnode *tdvp, struct vnode **tvpp,
2948 const struct componentname *scnp, const struct componentname *tcnp)
2950 struct vnode *nvp, *svp, *tvp;
2951 znode_t *sdzp, *tdzp, *szp, *tzp;
2955 if (*tvpp != NULL && *tvpp != tdvp)
2959 error = vn_lock(sdvp, LK_EXCLUSIVE);
2962 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
2967 error = vn_lock(tdvp, LK_EXCLUSIVE);
2976 error = zfs_rename_relock_lookup(sdzp, scnp, &szp, tdzp, tcnp, &tzp);
2983 tvp = tzp != NULL ? ZTOV(tzp) : NULL;
2986 * Now try acquire locks on svp and tvp.
2989 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
2995 if (error != EBUSY) {
2999 error = vn_lock(nvp, LK_EXCLUSIVE);
3006 * Concurrent rename race.
3011 error = SET_ERROR(EINVAL);
3026 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3031 if (error != EBUSY) {
3035 error = vn_lock(nvp, LK_EXCLUSIVE);
3053 * Note that we must use VRELE_ASYNC in this function as it walks
3054 * up the directory tree and vrele may need to acquire an exclusive
3055 * lock if a last reference to a vnode is dropped.
3058 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3065 zfsvfs = tdzp->z_zfsvfs;
3067 return (SET_ERROR(EINVAL));
3070 if (tdzp->z_id == zfsvfs->z_root)
3074 ASSERT(!zp->z_unlinked);
3075 if ((error = sa_lookup(zp->z_sa_hdl,
3076 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3079 if (parent == szp->z_id) {
3080 error = SET_ERROR(EINVAL);
3083 if (parent == zfsvfs->z_root)
3085 if (parent == sdzp->z_id)
3088 error = zfs_zget(zfsvfs, parent, &zp1);
3093 VN_RELE_ASYNC(ZTOV(zp),
3094 dsl_pool_zrele_taskq(
3095 dmu_objset_pool(zfsvfs->z_os)));
3099 if (error == ENOTDIR)
3100 panic("checkpath: .. not a directory\n");
3102 VN_RELE_ASYNC(ZTOV(zp),
3103 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3107 #if __FreeBSD_version < 1300124
3109 cache_vop_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
3110 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
3116 cache_purge_negative(tdvp);
3121 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3122 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3126 * Move an entry from the provided source directory to the target
3127 * directory. Change the entry name as indicated.
3129 * IN: sdvp - Source directory containing the "old entry".
3130 * scnp - Old entry name.
3131 * tdvp - Target directory to contain the "new entry".
3132 * tcnp - New entry name.
3133 * cr - credentials of caller.
3134 * INOUT: svpp - Source file
3135 * tvpp - Target file, may point to NULL initially
3137 * RETURN: 0 on success, error code on failure.
3140 * sdvp,tdvp - ctime|mtime updated
3143 zfs_do_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3144 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3149 ASSERT_VOP_ELOCKED(tdvp, __func__);
3151 ASSERT_VOP_ELOCKED(*tvpp, __func__);
3153 /* Reject renames across filesystems. */
3154 if ((*svpp)->v_mount != tdvp->v_mount ||
3155 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3156 error = SET_ERROR(EXDEV);
3160 if (zfsctl_is_node(tdvp)) {
3161 error = SET_ERROR(EXDEV);
3166 * Lock all four vnodes to ensure safety and semantics of renaming.
3168 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3170 /* no vnodes are locked in the case of error here */
3174 error = zfs_do_rename_impl(sdvp, svpp, scnp, tdvp, tvpp, tcnp, cr);
3187 zfs_do_rename_impl(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3188 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3194 znode_t *tdzp, *sdzp, *tzp, *szp;
3195 const char *snm = scnp->cn_nameptr;
3196 const char *tnm = tcnp->cn_nameptr;
3201 zfsvfs = tdzp->z_zfsvfs;
3203 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3205 if ((error = zfs_verify_zp(sdzp)) != 0) {
3206 zfs_exit(zfsvfs, FTAG);
3209 zilog = zfsvfs->z_log;
3211 if (zfsvfs->z_utf8 && u8_validate(tnm,
3212 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3213 error = SET_ERROR(EILSEQ);
3217 /* If source and target are the same file, there is nothing to do. */
3218 if ((*svpp) == (*tvpp)) {
3223 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3224 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3225 (*tvpp)->v_mountedhere != NULL)) {
3226 error = SET_ERROR(EXDEV);
3231 if ((error = zfs_verify_zp(szp)) != 0) {
3232 zfs_exit(zfsvfs, FTAG);
3235 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3237 if ((error = zfs_verify_zp(tzp)) != 0) {
3238 zfs_exit(zfsvfs, FTAG);
3244 * This is to prevent the creation of links into attribute space
3245 * by renaming a linked file into/outof an attribute directory.
3246 * See the comment in zfs_link() for why this is considered bad.
3248 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3249 error = SET_ERROR(EINVAL);
3254 * If we are using project inheritance, means if the directory has
3255 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3256 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3257 * such case, we only allow renames into our tree when the project
3260 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3261 tdzp->z_projid != szp->z_projid) {
3262 error = SET_ERROR(EXDEV);
3267 * Must have write access at the source to remove the old entry
3268 * and write access at the target to create the new entry.
3269 * Note that if target and source are the same, this can be
3270 * done in a single check.
3272 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr, NULL)))
3275 if ((*svpp)->v_type == VDIR) {
3277 * Avoid ".", "..", and aliases of "." for obvious reasons.
3279 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3281 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3287 * Check to make sure rename is valid.
3288 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3290 if ((error = zfs_rename_check(szp, sdzp, tdzp)))
3295 * Does target exist?
3299 * Source and target must be the same type.
3301 if ((*svpp)->v_type == VDIR) {
3302 if ((*tvpp)->v_type != VDIR) {
3303 error = SET_ERROR(ENOTDIR);
3311 if ((*tvpp)->v_type == VDIR) {
3312 error = SET_ERROR(EISDIR);
3318 vn_seqc_write_begin(*svpp);
3319 vn_seqc_write_begin(sdvp);
3321 vn_seqc_write_begin(*tvpp);
3323 vn_seqc_write_begin(tdvp);
3325 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3327 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3330 * notify the target directory if it is not the same
3331 * as source directory.
3334 vnevent_rename_dest_dir(tdvp, ct);
3337 tx = dmu_tx_create(zfsvfs->z_os);
3338 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3339 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3340 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3341 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3343 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3344 zfs_sa_upgrade_txholds(tx, tdzp);
3347 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3348 zfs_sa_upgrade_txholds(tx, tzp);
3351 zfs_sa_upgrade_txholds(tx, szp);
3352 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3353 error = dmu_tx_assign(tx, TXG_WAIT);
3359 if (tzp) /* Attempt to remove the existing target */
3360 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3363 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3365 szp->z_pflags |= ZFS_AV_MODIFIED;
3367 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3368 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3371 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3374 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3375 snm, tdzp, tnm, szp);
3378 * Update path information for the target vnode
3380 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
3383 * At this point, we have successfully created
3384 * the target name, but have failed to remove
3385 * the source name. Since the create was done
3386 * with the ZRENAMING flag, there are
3387 * complications; for one, the link count is
3388 * wrong. The easiest way to deal with this
3389 * is to remove the newly created target, and
3390 * return the original error. This must
3391 * succeed; fortunately, it is very unlikely to
3392 * fail, since we just created it.
3394 VERIFY0(zfs_link_destroy(tdzp, tnm, szp, tx,
3399 cache_vop_rename(sdvp, *svpp, tdvp, *tvpp, scnp, tcnp);
3406 vn_seqc_write_end(*svpp);
3407 vn_seqc_write_end(sdvp);
3409 vn_seqc_write_end(*tvpp);
3411 vn_seqc_write_end(tdvp);
3414 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3415 zil_commit(zilog, 0);
3416 zfs_exit(zfsvfs, FTAG);
3422 zfs_rename(znode_t *sdzp, const char *sname, znode_t *tdzp, const char *tname,
3423 cred_t *cr, int flags, zuserns_t *mnt_ns)
3425 struct componentname scn, tcn;
3426 vnode_t *sdvp, *tdvp;
3433 error = zfs_lookup_internal(sdzp, sname, &svp, &scn, DELETE);
3434 if (sdzp->z_zfsvfs->z_replay == B_FALSE)
3440 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
3441 error = zfs_lookup_internal(tdzp, tname, &tvp, &tcn, RENAME);
3442 if (error == EJUSTRETURN)
3444 else if (error != 0) {
3449 error = zfs_do_rename(sdvp, &svp, &scn, tdvp, &tvp, &tcn, cr);
3460 * Insert the indicated symbolic reference entry into the directory.
3462 * IN: dvp - Directory to contain new symbolic link.
3463 * link - Name for new symlink entry.
3464 * vap - Attributes of new entry.
3465 * cr - credentials of caller.
3466 * ct - caller context
3467 * flags - case flags
3468 * mnt_ns - Unused on FreeBSD
3470 * RETURN: 0 on success, error code on failure.
3473 * dvp - ctime|mtime updated
3476 zfs_symlink(znode_t *dzp, const char *name, vattr_t *vap,
3477 const char *link, znode_t **zpp, cred_t *cr, int flags, zuserns_t *mnt_ns)
3482 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3484 uint64_t len = strlen(link);
3486 zfs_acl_ids_t acl_ids;
3487 boolean_t fuid_dirtied;
3488 uint64_t txtype = TX_SYMLINK;
3490 ASSERT3S(vap->va_type, ==, VLNK);
3492 if ((error = zfs_enter_verify_zp(zfsvfs, dzp, FTAG)) != 0)
3494 zilog = zfsvfs->z_log;
3496 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3497 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3498 zfs_exit(zfsvfs, FTAG);
3499 return (SET_ERROR(EILSEQ));
3502 if (len > MAXPATHLEN) {
3503 zfs_exit(zfsvfs, FTAG);
3504 return (SET_ERROR(ENAMETOOLONG));
3507 if ((error = zfs_acl_ids_create(dzp, 0,
3508 vap, cr, NULL, &acl_ids, NULL)) != 0) {
3509 zfs_exit(zfsvfs, FTAG);
3514 * Attempt to lock directory; fail if entry already exists.
3516 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
3518 zfs_acl_ids_free(&acl_ids);
3519 zfs_exit(zfsvfs, FTAG);
3523 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr, mnt_ns))) {
3524 zfs_acl_ids_free(&acl_ids);
3525 zfs_exit(zfsvfs, FTAG);
3529 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids,
3531 zfs_acl_ids_free(&acl_ids);
3532 zfs_exit(zfsvfs, FTAG);
3533 return (SET_ERROR(EDQUOT));
3536 getnewvnode_reserve_();
3537 tx = dmu_tx_create(zfsvfs->z_os);
3538 fuid_dirtied = zfsvfs->z_fuid_dirty;
3539 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3540 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3541 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3542 ZFS_SA_BASE_ATTR_SIZE + len);
3543 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3544 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3545 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3546 acl_ids.z_aclp->z_acl_bytes);
3549 zfs_fuid_txhold(zfsvfs, tx);
3550 error = dmu_tx_assign(tx, TXG_WAIT);
3552 zfs_acl_ids_free(&acl_ids);
3554 getnewvnode_drop_reserve();
3555 zfs_exit(zfsvfs, FTAG);
3560 * Create a new object for the symlink.
3561 * for version 4 ZPL datasets the symlink will be an SA attribute
3563 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3566 zfs_fuid_sync(zfsvfs, tx);
3569 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3570 __DECONST(void *, link), len, tx);
3572 zfs_sa_symlink(zp, __DECONST(char *, link), len, tx);
3575 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3576 &zp->z_size, sizeof (zp->z_size), tx);
3578 * Insert the new object into the directory.
3580 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
3582 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3585 zfs_acl_ids_free(&acl_ids);
3589 getnewvnode_drop_reserve();
3591 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3592 zil_commit(zilog, 0);
3594 zfs_exit(zfsvfs, FTAG);
3599 * Return, in the buffer contained in the provided uio structure,
3600 * the symbolic path referred to by vp.
3602 * IN: vp - vnode of symbolic link.
3603 * uio - structure to contain the link path.
3604 * cr - credentials of caller.
3605 * ct - caller context
3607 * OUT: uio - structure containing the link path.
3609 * RETURN: 0 on success, error code on failure.
3612 * vp - atime updated
3615 zfs_readlink(vnode_t *vp, zfs_uio_t *uio, cred_t *cr, caller_context_t *ct)
3617 (void) cr, (void) ct;
3618 znode_t *zp = VTOZ(vp);
3619 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3622 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3626 error = sa_lookup_uio(zp->z_sa_hdl,
3627 SA_ZPL_SYMLINK(zfsvfs), uio);
3629 error = zfs_sa_readlink(zp, uio);
3631 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3633 zfs_exit(zfsvfs, FTAG);
3638 * Insert a new entry into directory tdvp referencing svp.
3640 * IN: tdvp - Directory to contain new entry.
3641 * svp - vnode of new entry.
3642 * name - name of new entry.
3643 * cr - credentials of caller.
3645 * RETURN: 0 on success, error code on failure.
3648 * tdvp - ctime|mtime updated
3649 * svp - ctime updated
3652 zfs_link(znode_t *tdzp, znode_t *szp, const char *name, cred_t *cr,
3657 zfsvfs_t *zfsvfs = tdzp->z_zfsvfs;
3664 ASSERT3S(ZTOV(tdzp)->v_type, ==, VDIR);
3666 if ((error = zfs_enter_verify_zp(zfsvfs, tdzp, FTAG)) != 0)
3668 zilog = zfsvfs->z_log;
3671 * POSIX dictates that we return EPERM here.
3672 * Better choices include ENOTSUP or EISDIR.
3674 if (ZTOV(szp)->v_type == VDIR) {
3675 zfs_exit(zfsvfs, FTAG);
3676 return (SET_ERROR(EPERM));
3679 if ((error = zfs_verify_zp(szp)) != 0) {
3680 zfs_exit(zfsvfs, FTAG);
3685 * If we are using project inheritance, means if the directory has
3686 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3687 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3688 * such case, we only allow hard link creation in our tree when the
3689 * project IDs are the same.
3691 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3692 tdzp->z_projid != szp->z_projid) {
3693 zfs_exit(zfsvfs, FTAG);
3694 return (SET_ERROR(EXDEV));
3697 if (szp->z_pflags & (ZFS_APPENDONLY |
3698 ZFS_IMMUTABLE | ZFS_READONLY)) {
3699 zfs_exit(zfsvfs, FTAG);
3700 return (SET_ERROR(EPERM));
3703 /* Prevent links to .zfs/shares files */
3705 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3706 &parent, sizeof (uint64_t))) != 0) {
3707 zfs_exit(zfsvfs, FTAG);
3710 if (parent == zfsvfs->z_shares_dir) {
3711 zfs_exit(zfsvfs, FTAG);
3712 return (SET_ERROR(EPERM));
3715 if (zfsvfs->z_utf8 && u8_validate(name,
3716 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3717 zfs_exit(zfsvfs, FTAG);
3718 return (SET_ERROR(EILSEQ));
3722 * We do not support links between attributes and non-attributes
3723 * because of the potential security risk of creating links
3724 * into "normal" file space in order to circumvent restrictions
3725 * imposed in attribute space.
3727 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) {
3728 zfs_exit(zfsvfs, FTAG);
3729 return (SET_ERROR(EINVAL));
3733 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
3734 if (owner != crgetuid(cr) && secpolicy_basic_link(ZTOV(szp), cr) != 0) {
3735 zfs_exit(zfsvfs, FTAG);
3736 return (SET_ERROR(EPERM));
3739 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr, NULL))) {
3740 zfs_exit(zfsvfs, FTAG);
3745 * Attempt to lock directory; fail if entry already exists.
3747 error = zfs_dirent_lookup(tdzp, name, &tzp, ZNEW);
3749 zfs_exit(zfsvfs, FTAG);
3753 tx = dmu_tx_create(zfsvfs->z_os);
3754 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3755 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name);
3756 zfs_sa_upgrade_txholds(tx, szp);
3757 zfs_sa_upgrade_txholds(tx, tdzp);
3758 error = dmu_tx_assign(tx, TXG_WAIT);
3761 zfs_exit(zfsvfs, FTAG);
3765 error = zfs_link_create(tdzp, name, szp, tx, 0);
3768 uint64_t txtype = TX_LINK;
3769 zfs_log_link(zilog, tx, txtype, tdzp, szp, name);
3775 vnevent_link(ZTOV(szp), ct);
3778 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3779 zil_commit(zilog, 0);
3781 zfs_exit(zfsvfs, FTAG);
3786 * Free or allocate space in a file. Currently, this function only
3787 * supports the `F_FREESP' command. However, this command is somewhat
3788 * misnamed, as its functionality includes the ability to allocate as
3789 * well as free space.
3791 * IN: ip - inode of file to free data in.
3792 * cmd - action to take (only F_FREESP supported).
3793 * bfp - section of file to free/alloc.
3794 * flag - current file open mode flags.
3795 * offset - current file offset.
3796 * cr - credentials of caller.
3798 * RETURN: 0 on success, error code on failure.
3801 * ip - ctime|mtime updated
3804 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag,
3805 offset_t offset, cred_t *cr)
3808 zfsvfs_t *zfsvfs = ZTOZSB(zp);
3812 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3815 if (cmd != F_FREESP) {
3816 zfs_exit(zfsvfs, FTAG);
3817 return (SET_ERROR(EINVAL));
3821 * Callers might not be able to detect properly that we are read-only,
3822 * so check it explicitly here.
3824 if (zfs_is_readonly(zfsvfs)) {
3825 zfs_exit(zfsvfs, FTAG);
3826 return (SET_ERROR(EROFS));
3829 if (bfp->l_len < 0) {
3830 zfs_exit(zfsvfs, FTAG);
3831 return (SET_ERROR(EINVAL));
3835 * Permissions aren't checked on Solaris because on this OS
3836 * zfs_space() can only be called with an opened file handle.
3837 * On Linux we can get here through truncate_range() which
3838 * operates directly on inodes, so we need to check access rights.
3840 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr, NULL))) {
3841 zfs_exit(zfsvfs, FTAG);
3846 len = bfp->l_len; /* 0 means from off to end of file */
3848 error = zfs_freesp(zp, off, len, flag, TRUE);
3850 zfs_exit(zfsvfs, FTAG);
3855 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3857 (void) cr, (void) ct;
3858 znode_t *zp = VTOZ(vp);
3859 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3862 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
3863 if (zp->z_sa_hdl == NULL) {
3865 * The fs has been unmounted, or we did a
3866 * suspend/resume and this file no longer exists.
3868 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3873 if (zp->z_unlinked) {
3875 * Fast path to recycle a vnode of a removed file.
3877 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3882 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3883 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3885 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3886 zfs_sa_upgrade_txholds(tx, zp);
3887 error = dmu_tx_assign(tx, TXG_WAIT);
3891 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
3892 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3893 zp->z_atime_dirty = 0;
3897 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
3901 _Static_assert(sizeof (struct zfid_short) <= sizeof (struct fid),
3902 "struct zfid_short bigger than struct fid");
3903 _Static_assert(sizeof (struct zfid_long) <= sizeof (struct fid),
3904 "struct zfid_long bigger than struct fid");
3907 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3910 znode_t *zp = VTOZ(vp);
3911 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3914 uint64_t object = zp->z_id;
3918 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3921 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
3922 &gen64, sizeof (uint64_t))) != 0) {
3923 zfs_exit(zfsvfs, FTAG);
3927 gen = (uint32_t)gen64;
3929 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
3930 fidp->fid_len = size;
3932 zfid = (zfid_short_t *)fidp;
3934 zfid->zf_len = size;
3936 for (i = 0; i < sizeof (zfid->zf_object); i++)
3937 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
3939 /* Must have a non-zero generation number to distinguish from .zfs */
3942 for (i = 0; i < sizeof (zfid->zf_gen); i++)
3943 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
3945 if (size == LONG_FID_LEN) {
3946 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
3949 zlfid = (zfid_long_t *)fidp;
3951 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
3952 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
3954 /* XXX - this should be the generation number for the objset */
3955 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
3956 zlfid->zf_setgen[i] = 0;
3959 zfs_exit(zfsvfs, FTAG);
3964 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
3965 caller_context_t *ct)
3973 *valp = MIN(LONG_MAX, ZFS_LINK_MAX);
3976 case _PC_FILESIZEBITS:
3979 case _PC_MIN_HOLE_SIZE:
3980 *valp = (int)SPA_MINBLOCKSIZE;
3982 case _PC_ACL_EXTENDED:
3983 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
3985 zfsvfs = zp->z_zfsvfs;
3986 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
3988 *valp = zfsvfs->z_acl_type == ZFSACLTYPE_POSIX ? 1 : 0;
3989 zfs_exit(zfsvfs, FTAG);
3997 zfsvfs = zp->z_zfsvfs;
3998 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
4000 *valp = zfsvfs->z_acl_type == ZFS_ACLTYPE_NFSV4 ? 1 : 0;
4001 zfs_exit(zfsvfs, FTAG);
4004 case _PC_ACL_PATH_MAX:
4005 *valp = ACL_MAX_ENTRIES;
4009 return (EOPNOTSUPP);
4014 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind,
4017 znode_t *zp = VTOZ(vp);
4018 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4019 zfs_locked_range_t *lr;
4021 off_t start, end, obj_size;
4023 int pgsin_b, pgsin_a;
4026 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4027 return (zfs_vm_pagerret_error);
4029 start = IDX_TO_OFF(ma[0]->pindex);
4030 end = IDX_TO_OFF(ma[count - 1]->pindex + 1);
4033 * Lock a range covering all required and optional pages.
4034 * Note that we need to handle the case of the block size growing.
4037 blksz = zp->z_blksz;
4038 lr = zfs_rangelock_tryenter(&zp->z_rangelock,
4039 rounddown(start, blksz),
4040 roundup(end, blksz) - rounddown(start, blksz), RL_READER);
4042 if (rahead != NULL) {
4046 if (rbehind != NULL) {
4052 if (blksz == zp->z_blksz)
4054 zfs_rangelock_exit(lr);
4057 object = ma[0]->object;
4058 zfs_vmobject_wlock(object);
4059 obj_size = object->un_pager.vnp.vnp_size;
4060 zfs_vmobject_wunlock(object);
4061 if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) {
4063 zfs_rangelock_exit(lr);
4064 zfs_exit(zfsvfs, FTAG);
4065 return (zfs_vm_pagerret_bad);
4069 if (rbehind != NULL) {
4070 pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz));
4071 pgsin_b = MIN(*rbehind, pgsin_b);
4075 if (rahead != NULL) {
4076 pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end);
4077 if (end + IDX_TO_OFF(pgsin_a) >= obj_size)
4078 pgsin_a = OFF_TO_IDX(round_page(obj_size) - end);
4079 pgsin_a = MIN(*rahead, pgsin_a);
4083 * NB: we need to pass the exact byte size of the data that we expect
4084 * to read after accounting for the file size. This is required because
4085 * ZFS will panic if we request DMU to read beyond the end of the last
4088 error = dmu_read_pages(zfsvfs->z_os, zp->z_id, ma, count, &pgsin_b,
4089 &pgsin_a, MIN(end, obj_size) - (end - PAGE_SIZE));
4092 zfs_rangelock_exit(lr);
4093 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4095 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, count*PAGE_SIZE);
4097 zfs_exit(zfsvfs, FTAG);
4100 return (zfs_vm_pagerret_error);
4102 VM_CNT_INC(v_vnodein);
4103 VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a);
4104 if (rbehind != NULL)
4108 return (zfs_vm_pagerret_ok);
4111 #ifndef _SYS_SYSPROTO_H_
4112 struct vop_getpages_args {
4122 zfs_freebsd_getpages(struct vop_getpages_args *ap)
4125 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4130 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4133 znode_t *zp = VTOZ(vp);
4134 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4135 zfs_locked_range_t *lr;
4143 vm_ooffset_t lo_off;
4151 object = vp->v_object;
4152 KASSERT(ma[0]->object == object, ("mismatching object"));
4153 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4157 for (i = 0; i < pcount; i++)
4158 rtvals[i] = zfs_vm_pagerret_error;
4160 if (zfs_enter_verify_zp(zfsvfs, zp, FTAG) != 0)
4161 return (zfs_vm_pagerret_error);
4163 off = IDX_TO_OFF(ma[0]->pindex);
4164 blksz = zp->z_blksz;
4165 lo_off = rounddown(off, blksz);
4166 lo_len = roundup(len + (off - lo_off), blksz);
4167 lr = zfs_rangelock_enter(&zp->z_rangelock, lo_off, lo_len, RL_WRITER);
4169 zfs_vmobject_wlock(object);
4170 if (len + off > object->un_pager.vnp.vnp_size) {
4171 if (object->un_pager.vnp.vnp_size > off) {
4174 len = object->un_pager.vnp.vnp_size - off;
4176 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4178 * If the object is locked and the following
4179 * conditions hold, then the page's dirty
4180 * field cannot be concurrently changed by a
4184 vm_page_assert_sbusied(m);
4185 KASSERT(!pmap_page_is_write_mapped(m),
4186 ("zfs_putpages: page %p is not read-only",
4188 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4195 if (ncount < pcount) {
4196 for (i = ncount; i < pcount; i++) {
4197 rtvals[i] = zfs_vm_pagerret_bad;
4201 zfs_vmobject_wunlock(object);
4206 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, zp->z_uid) ||
4207 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, zp->z_gid) ||
4208 (zp->z_projid != ZFS_DEFAULT_PROJID &&
4209 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
4214 tx = dmu_tx_create(zfsvfs->z_os);
4215 dmu_tx_hold_write(tx, zp->z_id, off, len);
4217 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4218 zfs_sa_upgrade_txholds(tx, zp);
4219 err = dmu_tx_assign(tx, TXG_WAIT);
4225 if (zp->z_blksz < PAGE_SIZE) {
4226 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4227 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4228 va = zfs_map_page(ma[i], &sf);
4229 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4233 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4237 uint64_t mtime[2], ctime[2];
4238 sa_bulk_attr_t bulk[3];
4241 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4243 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4245 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4247 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
4248 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4251 * XXX we should be passing a callback to undirty
4252 * but that would make the locking messier
4254 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off,
4255 len, 0, NULL, NULL);
4257 zfs_vmobject_wlock(object);
4258 for (i = 0; i < ncount; i++) {
4259 rtvals[i] = zfs_vm_pagerret_ok;
4260 vm_page_undirty(ma[i]);
4262 zfs_vmobject_wunlock(object);
4263 VM_CNT_INC(v_vnodeout);
4264 VM_CNT_ADD(v_vnodepgsout, ncount);
4269 zfs_rangelock_exit(lr);
4270 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4271 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4272 zil_commit(zfsvfs->z_log, zp->z_id);
4274 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, len);
4276 zfs_exit(zfsvfs, FTAG);
4280 #ifndef _SYS_SYSPROTO_H_
4281 struct vop_putpages_args {
4291 zfs_freebsd_putpages(struct vop_putpages_args *ap)
4294 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4298 #ifndef _SYS_SYSPROTO_H_
4299 struct vop_bmap_args {
4302 struct bufobj **a_bop;
4310 zfs_freebsd_bmap(struct vop_bmap_args *ap)
4313 if (ap->a_bop != NULL)
4314 *ap->a_bop = &ap->a_vp->v_bufobj;
4315 if (ap->a_bnp != NULL)
4316 *ap->a_bnp = ap->a_bn;
4317 if (ap->a_runp != NULL)
4319 if (ap->a_runb != NULL)
4325 #ifndef _SYS_SYSPROTO_H_
4326 struct vop_open_args {
4329 struct ucred *a_cred;
4330 struct thread *a_td;
4335 zfs_freebsd_open(struct vop_open_args *ap)
4337 vnode_t *vp = ap->a_vp;
4338 znode_t *zp = VTOZ(vp);
4341 error = zfs_open(&vp, ap->a_mode, ap->a_cred);
4343 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4347 #ifndef _SYS_SYSPROTO_H_
4348 struct vop_close_args {
4351 struct ucred *a_cred;
4352 struct thread *a_td;
4357 zfs_freebsd_close(struct vop_close_args *ap)
4360 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred));
4363 #ifndef _SYS_SYSPROTO_H_
4364 struct vop_ioctl_args {
4375 zfs_freebsd_ioctl(struct vop_ioctl_args *ap)
4378 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4379 ap->a_fflag, ap->a_cred, NULL));
4383 ioflags(int ioflags)
4387 if (ioflags & IO_APPEND)
4389 if (ioflags & IO_NDELAY)
4390 flags |= O_NONBLOCK;
4391 if (ioflags & IO_SYNC)
4397 #ifndef _SYS_SYSPROTO_H_
4398 struct vop_read_args {
4402 struct ucred *a_cred;
4407 zfs_freebsd_read(struct vop_read_args *ap)
4410 zfs_uio_init(&uio, ap->a_uio);
4411 return (zfs_read(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4415 #ifndef _SYS_SYSPROTO_H_
4416 struct vop_write_args {
4420 struct ucred *a_cred;
4425 zfs_freebsd_write(struct vop_write_args *ap)
4428 zfs_uio_init(&uio, ap->a_uio);
4429 return (zfs_write(VTOZ(ap->a_vp), &uio, ioflags(ap->a_ioflag),
4433 #if __FreeBSD_version >= 1300102
4435 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4436 * the comment above cache_fplookup for details.
4439 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args *v)
4447 if (__predict_false(zp == NULL))
4449 pflags = atomic_load_64(&zp->z_pflags);
4450 if (pflags & ZFS_AV_QUARANTINED)
4452 if (pflags & ZFS_XATTR)
4454 if ((pflags & ZFS_NO_EXECS_DENIED) == 0)
4460 #if __FreeBSD_version >= 1300139
4462 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args *v)
4470 if (__predict_false(zp == NULL)) {
4474 target = atomic_load_consume_ptr(&zp->z_cached_symlink);
4475 if (target == NULL) {
4478 return (cache_symlink_resolve(v->a_fpl, target, strlen(target)));
4482 #ifndef _SYS_SYSPROTO_H_
4483 struct vop_access_args {
4485 accmode_t a_accmode;
4486 struct ucred *a_cred;
4487 struct thread *a_td;
4492 zfs_freebsd_access(struct vop_access_args *ap)
4494 vnode_t *vp = ap->a_vp;
4495 znode_t *zp = VTOZ(vp);
4500 if (ap->a_accmode == VEXEC) {
4501 if (zfs_fastaccesschk_execute(zp, ap->a_cred) == 0)
4506 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4508 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4510 error = zfs_access(zp, accmode, 0, ap->a_cred);
4513 * VADMIN has to be handled by vaccess().
4516 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4518 #if __FreeBSD_version >= 1300105
4519 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4520 zp->z_gid, accmode, ap->a_cred);
4522 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4523 zp->z_gid, accmode, ap->a_cred, NULL);
4529 * For VEXEC, ensure that at least one execute bit is set for
4532 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4533 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4540 #ifndef _SYS_SYSPROTO_H_
4541 struct vop_lookup_args {
4542 struct vnode *a_dvp;
4543 struct vnode **a_vpp;
4544 struct componentname *a_cnp;
4549 zfs_freebsd_lookup(struct vop_lookup_args *ap, boolean_t cached)
4551 struct componentname *cnp = ap->a_cnp;
4552 char nm[NAME_MAX + 1];
4554 ASSERT3U(cnp->cn_namelen, <, sizeof (nm));
4555 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof (nm)));
4557 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4558 cnp->cn_cred, 0, cached));
4562 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args *ap)
4565 return (zfs_freebsd_lookup((struct vop_lookup_args *)ap, B_TRUE));
4568 #ifndef _SYS_SYSPROTO_H_
4569 struct vop_lookup_args {
4570 struct vnode *a_dvp;
4571 struct vnode **a_vpp;
4572 struct componentname *a_cnp;
4577 zfs_cache_lookup(struct vop_lookup_args *ap)
4581 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4582 if (zfsvfs->z_use_namecache)
4583 return (vfs_cache_lookup(ap));
4585 return (zfs_freebsd_lookup(ap, B_FALSE));
4588 #ifndef _SYS_SYSPROTO_H_
4589 struct vop_create_args {
4590 struct vnode *a_dvp;
4591 struct vnode **a_vpp;
4592 struct componentname *a_cnp;
4593 struct vattr *a_vap;
4598 zfs_freebsd_create(struct vop_create_args *ap)
4601 struct componentname *cnp = ap->a_cnp;
4602 vattr_t *vap = ap->a_vap;
4606 #if __FreeBSD_version < 1400068
4607 ASSERT(cnp->cn_flags & SAVENAME);
4610 vattr_init_mask(vap);
4611 mode = vap->va_mode & ALLPERMS;
4612 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4615 rc = zfs_create(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, 0, mode,
4616 &zp, cnp->cn_cred, 0 /* flag */, NULL /* vsecattr */, NULL);
4618 *ap->a_vpp = ZTOV(zp);
4619 if (zfsvfs->z_use_namecache &&
4620 rc == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
4621 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
4626 #ifndef _SYS_SYSPROTO_H_
4627 struct vop_remove_args {
4628 struct vnode *a_dvp;
4630 struct componentname *a_cnp;
4635 zfs_freebsd_remove(struct vop_remove_args *ap)
4638 #if __FreeBSD_version < 1400068
4639 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4642 return (zfs_remove_(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
4643 ap->a_cnp->cn_cred));
4646 #ifndef _SYS_SYSPROTO_H_
4647 struct vop_mkdir_args {
4648 struct vnode *a_dvp;
4649 struct vnode **a_vpp;
4650 struct componentname *a_cnp;
4651 struct vattr *a_vap;
4656 zfs_freebsd_mkdir(struct vop_mkdir_args *ap)
4658 vattr_t *vap = ap->a_vap;
4662 #if __FreeBSD_version < 1400068
4663 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
4666 vattr_init_mask(vap);
4669 rc = zfs_mkdir(VTOZ(ap->a_dvp), ap->a_cnp->cn_nameptr, vap, &zp,
4670 ap->a_cnp->cn_cred, 0, NULL, NULL);
4673 *ap->a_vpp = ZTOV(zp);
4677 #ifndef _SYS_SYSPROTO_H_
4678 struct vop_rmdir_args {
4679 struct vnode *a_dvp;
4681 struct componentname *a_cnp;
4686 zfs_freebsd_rmdir(struct vop_rmdir_args *ap)
4688 struct componentname *cnp = ap->a_cnp;
4690 #if __FreeBSD_version < 1400068
4691 ASSERT(cnp->cn_flags & SAVENAME);
4694 return (zfs_rmdir_(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
4697 #ifndef _SYS_SYSPROTO_H_
4698 struct vop_readdir_args {
4701 struct ucred *a_cred;
4704 cookie_t **a_cookies;
4709 zfs_freebsd_readdir(struct vop_readdir_args *ap)
4712 zfs_uio_init(&uio, ap->a_uio);
4713 return (zfs_readdir(ap->a_vp, &uio, ap->a_cred, ap->a_eofflag,
4714 ap->a_ncookies, ap->a_cookies));
4717 #ifndef _SYS_SYSPROTO_H_
4718 struct vop_fsync_args {
4721 struct thread *a_td;
4726 zfs_freebsd_fsync(struct vop_fsync_args *ap)
4730 return (zfs_fsync(VTOZ(ap->a_vp), 0, ap->a_td->td_ucred));
4733 #ifndef _SYS_SYSPROTO_H_
4734 struct vop_getattr_args {
4736 struct vattr *a_vap;
4737 struct ucred *a_cred;
4742 zfs_freebsd_getattr(struct vop_getattr_args *ap)
4744 vattr_t *vap = ap->a_vap;
4750 xvap.xva_vattr = *vap;
4751 xvap.xva_vattr.va_mask |= AT_XVATTR;
4753 /* Convert chflags into ZFS-type flags. */
4754 /* XXX: what about SF_SETTABLE?. */
4755 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
4756 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
4757 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
4758 XVA_SET_REQ(&xvap, XAT_NODUMP);
4759 XVA_SET_REQ(&xvap, XAT_READONLY);
4760 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
4761 XVA_SET_REQ(&xvap, XAT_SYSTEM);
4762 XVA_SET_REQ(&xvap, XAT_HIDDEN);
4763 XVA_SET_REQ(&xvap, XAT_REPARSE);
4764 XVA_SET_REQ(&xvap, XAT_OFFLINE);
4765 XVA_SET_REQ(&xvap, XAT_SPARSE);
4767 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred);
4771 /* Convert ZFS xattr into chflags. */
4772 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4773 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4774 fflags |= (fflag); \
4776 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
4777 xvap.xva_xoptattrs.xoa_immutable);
4778 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
4779 xvap.xva_xoptattrs.xoa_appendonly);
4780 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
4781 xvap.xva_xoptattrs.xoa_nounlink);
4782 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
4783 xvap.xva_xoptattrs.xoa_archive);
4784 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
4785 xvap.xva_xoptattrs.xoa_nodump);
4786 FLAG_CHECK(UF_READONLY, XAT_READONLY,
4787 xvap.xva_xoptattrs.xoa_readonly);
4788 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
4789 xvap.xva_xoptattrs.xoa_system);
4790 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
4791 xvap.xva_xoptattrs.xoa_hidden);
4792 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
4793 xvap.xva_xoptattrs.xoa_reparse);
4794 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
4795 xvap.xva_xoptattrs.xoa_offline);
4796 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
4797 xvap.xva_xoptattrs.xoa_sparse);
4800 *vap = xvap.xva_vattr;
4801 vap->va_flags = fflags;
4805 #ifndef _SYS_SYSPROTO_H_
4806 struct vop_setattr_args {
4808 struct vattr *a_vap;
4809 struct ucred *a_cred;
4814 zfs_freebsd_setattr(struct vop_setattr_args *ap)
4816 vnode_t *vp = ap->a_vp;
4817 vattr_t *vap = ap->a_vap;
4818 cred_t *cred = ap->a_cred;
4823 vattr_init_mask(vap);
4824 vap->va_mask &= ~AT_NOSET;
4827 xvap.xva_vattr = *vap;
4829 zflags = VTOZ(vp)->z_pflags;
4831 if (vap->va_flags != VNOVAL) {
4832 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
4835 if (zfsvfs->z_use_fuids == B_FALSE)
4836 return (EOPNOTSUPP);
4838 fflags = vap->va_flags;
4841 * We need to figure out whether it makes sense to allow
4842 * UF_REPARSE through, since we don't really have other
4843 * facilities to handle reparse points and zfs_setattr()
4844 * doesn't currently allow setting that attribute anyway.
4846 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
4847 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
4848 UF_OFFLINE|UF_SPARSE)) != 0)
4849 return (EOPNOTSUPP);
4851 * Unprivileged processes are not permitted to unset system
4852 * flags, or modify flags if any system flags are set.
4853 * Privileged non-jail processes may not modify system flags
4854 * if securelevel > 0 and any existing system flags are set.
4855 * Privileged jail processes behave like privileged non-jail
4856 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
4857 * otherwise, they behave like unprivileged processes.
4859 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
4860 spl_priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) {
4862 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
4863 error = securelevel_gt(cred, 0);
4869 * Callers may only modify the file flags on
4870 * objects they have VADMIN rights for.
4872 if ((error = VOP_ACCESS(vp, VADMIN, cred,
4876 (ZFS_IMMUTABLE | ZFS_APPENDONLY |
4881 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
4886 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4887 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4888 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4889 XVA_SET_REQ(&xvap, (xflag)); \
4890 (xfield) = ((fflags & (fflag)) != 0); \
4893 /* Convert chflags into ZFS-type flags. */
4894 /* XXX: what about SF_SETTABLE?. */
4895 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
4896 xvap.xva_xoptattrs.xoa_immutable);
4897 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
4898 xvap.xva_xoptattrs.xoa_appendonly);
4899 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
4900 xvap.xva_xoptattrs.xoa_nounlink);
4901 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
4902 xvap.xva_xoptattrs.xoa_archive);
4903 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
4904 xvap.xva_xoptattrs.xoa_nodump);
4905 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
4906 xvap.xva_xoptattrs.xoa_readonly);
4907 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
4908 xvap.xva_xoptattrs.xoa_system);
4909 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
4910 xvap.xva_xoptattrs.xoa_hidden);
4911 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
4912 xvap.xva_xoptattrs.xoa_reparse);
4913 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
4914 xvap.xva_xoptattrs.xoa_offline);
4915 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
4916 xvap.xva_xoptattrs.xoa_sparse);
4919 if (vap->va_birthtime.tv_sec != VNOVAL) {
4920 xvap.xva_vattr.va_mask |= AT_XVATTR;
4921 XVA_SET_REQ(&xvap, XAT_CREATETIME);
4923 return (zfs_setattr(VTOZ(vp), (vattr_t *)&xvap, 0, cred, NULL));
4926 #ifndef _SYS_SYSPROTO_H_
4927 struct vop_rename_args {
4928 struct vnode *a_fdvp;
4929 struct vnode *a_fvp;
4930 struct componentname *a_fcnp;
4931 struct vnode *a_tdvp;
4932 struct vnode *a_tvp;
4933 struct componentname *a_tcnp;
4938 zfs_freebsd_rename(struct vop_rename_args *ap)
4940 vnode_t *fdvp = ap->a_fdvp;
4941 vnode_t *fvp = ap->a_fvp;
4942 vnode_t *tdvp = ap->a_tdvp;
4943 vnode_t *tvp = ap->a_tvp;
4946 #if __FreeBSD_version < 1400068
4947 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
4948 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
4951 error = zfs_do_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
4952 ap->a_tcnp, ap->a_fcnp->cn_cred);
4963 #ifndef _SYS_SYSPROTO_H_
4964 struct vop_symlink_args {
4965 struct vnode *a_dvp;
4966 struct vnode **a_vpp;
4967 struct componentname *a_cnp;
4968 struct vattr *a_vap;
4974 zfs_freebsd_symlink(struct vop_symlink_args *ap)
4976 struct componentname *cnp = ap->a_cnp;
4977 vattr_t *vap = ap->a_vap;
4979 #if __FreeBSD_version >= 1300139
4985 #if __FreeBSD_version < 1400068
4986 ASSERT(cnp->cn_flags & SAVENAME);
4989 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
4990 vattr_init_mask(vap);
4993 rc = zfs_symlink(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap,
4994 ap->a_target, &zp, cnp->cn_cred, 0 /* flags */, NULL);
4996 *ap->a_vpp = ZTOV(zp);
4997 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
4998 #if __FreeBSD_version >= 1300139
4999 MPASS(zp->z_cached_symlink == NULL);
5000 symlink_len = strlen(ap->a_target);
5001 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5002 if (symlink != NULL) {
5003 memcpy(symlink, ap->a_target, symlink_len);
5004 symlink[symlink_len] = '\0';
5005 atomic_store_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5006 (uintptr_t)symlink);
5013 #ifndef _SYS_SYSPROTO_H_
5014 struct vop_readlink_args {
5017 struct ucred *a_cred;
5022 zfs_freebsd_readlink(struct vop_readlink_args *ap)
5026 #if __FreeBSD_version >= 1300139
5027 znode_t *zp = VTOZ(ap->a_vp);
5028 char *symlink, *base;
5033 zfs_uio_init(&uio, ap->a_uio);
5034 #if __FreeBSD_version >= 1300139
5036 if (zfs_uio_segflg(&uio) == UIO_SYSSPACE &&
5037 zfs_uio_iovcnt(&uio) == 1) {
5038 base = zfs_uio_iovbase(&uio, 0);
5039 symlink_len = zfs_uio_iovlen(&uio, 0);
5043 error = zfs_readlink(ap->a_vp, &uio, ap->a_cred, NULL);
5044 #if __FreeBSD_version >= 1300139
5045 if (atomic_load_ptr(&zp->z_cached_symlink) != NULL ||
5046 error != 0 || !trycache) {
5049 symlink_len -= zfs_uio_resid(&uio);
5050 symlink = cache_symlink_alloc(symlink_len + 1, M_WAITOK);
5051 if (symlink != NULL) {
5052 memcpy(symlink, base, symlink_len);
5053 symlink[symlink_len] = '\0';
5054 if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp->z_cached_symlink,
5055 (uintptr_t)NULL, (uintptr_t)symlink)) {
5056 cache_symlink_free(symlink, symlink_len + 1);
5063 #ifndef _SYS_SYSPROTO_H_
5064 struct vop_link_args {
5065 struct vnode *a_tdvp;
5067 struct componentname *a_cnp;
5072 zfs_freebsd_link(struct vop_link_args *ap)
5074 struct componentname *cnp = ap->a_cnp;
5075 vnode_t *vp = ap->a_vp;
5076 vnode_t *tdvp = ap->a_tdvp;
5078 if (tdvp->v_mount != vp->v_mount)
5081 #if __FreeBSD_version < 1400068
5082 ASSERT(cnp->cn_flags & SAVENAME);
5085 return (zfs_link(VTOZ(tdvp), VTOZ(vp),
5086 cnp->cn_nameptr, cnp->cn_cred, 0));
5089 #ifndef _SYS_SYSPROTO_H_
5090 struct vop_inactive_args {
5092 struct thread *a_td;
5097 zfs_freebsd_inactive(struct vop_inactive_args *ap)
5099 vnode_t *vp = ap->a_vp;
5101 #if __FreeBSD_version >= 1300123
5102 zfs_inactive(vp, curthread->td_ucred, NULL);
5104 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5109 #if __FreeBSD_version >= 1300042
5110 #ifndef _SYS_SYSPROTO_H_
5111 struct vop_need_inactive_args {
5113 struct thread *a_td;
5118 zfs_freebsd_need_inactive(struct vop_need_inactive_args *ap)
5120 vnode_t *vp = ap->a_vp;
5121 znode_t *zp = VTOZ(vp);
5122 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5125 if (vn_need_pageq_flush(vp))
5128 if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs))
5130 need = (zp->z_sa_hdl == NULL || zp->z_unlinked || zp->z_atime_dirty);
5131 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5137 #ifndef _SYS_SYSPROTO_H_
5138 struct vop_reclaim_args {
5140 struct thread *a_td;
5145 zfs_freebsd_reclaim(struct vop_reclaim_args *ap)
5147 vnode_t *vp = ap->a_vp;
5148 znode_t *zp = VTOZ(vp);
5149 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5151 ASSERT3P(zp, !=, NULL);
5153 #if __FreeBSD_version < 1300042
5154 /* Destroy the vm object and flush associated pages. */
5155 vnode_destroy_vobject(vp);
5158 * z_teardown_inactive_lock protects from a race with
5159 * zfs_znode_dmu_fini in zfsvfs_teardown during
5162 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs);
5163 if (zp->z_sa_hdl == NULL)
5167 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs);
5173 #ifndef _SYS_SYSPROTO_H_
5174 struct vop_fid_args {
5181 zfs_freebsd_fid(struct vop_fid_args *ap)
5184 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5188 #ifndef _SYS_SYSPROTO_H_
5189 struct vop_pathconf_args {
5192 register_t *a_retval;
5197 zfs_freebsd_pathconf(struct vop_pathconf_args *ap)
5202 error = zfs_pathconf(ap->a_vp, ap->a_name, &val,
5203 curthread->td_ucred, NULL);
5205 *ap->a_retval = val;
5208 if (error != EOPNOTSUPP)
5211 switch (ap->a_name) {
5213 *ap->a_retval = NAME_MAX;
5215 #if __FreeBSD_version >= 1400032
5216 case _PC_DEALLOC_PRESENT:
5221 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) {
5222 *ap->a_retval = PIPE_BUF;
5227 return (vop_stdpathconf(ap));
5231 static int zfs_xattr_compat = 1;
5234 zfs_check_attrname(const char *name)
5236 /* We don't allow '/' character in attribute name. */
5237 if (strchr(name, '/') != NULL)
5238 return (SET_ERROR(EINVAL));
5239 /* We don't allow attribute names that start with a namespace prefix. */
5240 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5241 return (SET_ERROR(EINVAL));
5246 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5247 * extended attribute name:
5249 * NAMESPACE XATTR_COMPAT PREFIX
5250 * system * freebsd:system:
5251 * user 1 (none, can be used to access ZFS
5252 * fsattr(5) attributes created on Solaris)
5256 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5257 size_t size, boolean_t compat)
5259 const char *namespace, *prefix, *suffix;
5261 memset(attrname, 0, size);
5263 switch (attrnamespace) {
5264 case EXTATTR_NAMESPACE_USER:
5267 * This is the default namespace by which we can access
5268 * all attributes created on Solaris.
5270 prefix = namespace = suffix = "";
5273 * This is compatible with the user namespace encoding
5274 * on Linux prior to xattr_compat, but nothing
5282 case EXTATTR_NAMESPACE_SYSTEM:
5283 prefix = "freebsd:";
5284 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5287 case EXTATTR_NAMESPACE_EMPTY:
5289 return (SET_ERROR(EINVAL));
5291 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5293 return (SET_ERROR(ENAMETOOLONG));
5299 zfs_ensure_xattr_cached(znode_t *zp)
5303 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5305 if (zp->z_xattr_cached != NULL)
5308 if (rw_write_held(&zp->z_xattr_lock))
5309 return (zfs_sa_get_xattr(zp));
5311 if (!rw_tryupgrade(&zp->z_xattr_lock)) {
5312 rw_exit(&zp->z_xattr_lock);
5313 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5315 if (zp->z_xattr_cached == NULL)
5316 error = zfs_sa_get_xattr(zp);
5317 rw_downgrade(&zp->z_xattr_lock);
5321 #ifndef _SYS_SYSPROTO_H_
5322 struct vop_getextattr {
5323 IN struct vnode *a_vp;
5324 IN int a_attrnamespace;
5325 IN const char *a_name;
5326 INOUT struct uio *a_uio;
5328 IN struct ucred *a_cred;
5329 IN struct thread *a_td;
5334 zfs_getextattr_dir(struct vop_getextattr_args *ap, const char *attrname)
5336 struct thread *td = ap->a_td;
5337 struct nameidata nd;
5339 vnode_t *xvp = NULL, *vp;
5342 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5343 LOOKUP_XATTR, B_FALSE);
5348 #if __FreeBSD_version < 1400043
5349 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5352 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5354 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_INVFS, ap->a_cred, NULL);
5356 return (SET_ERROR(error));
5360 if (ap->a_size != NULL) {
5361 error = VOP_GETATTR(vp, &va, ap->a_cred);
5363 *ap->a_size = (size_t)va.va_size;
5364 } else if (ap->a_uio != NULL)
5365 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5368 vn_close(vp, flags, ap->a_cred, td);
5373 zfs_getextattr_sa(struct vop_getextattr_args *ap, const char *attrname)
5375 znode_t *zp = VTOZ(ap->a_vp);
5380 error = zfs_ensure_xattr_cached(zp);
5384 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5385 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5387 error = nvlist_lookup_byte_array(zp->z_xattr_cached, attrname,
5388 &nv_value, &nv_size);
5390 return (SET_ERROR(error));
5392 if (ap->a_size != NULL)
5393 *ap->a_size = nv_size;
5394 else if (ap->a_uio != NULL)
5395 error = uiomove(nv_value, nv_size, ap->a_uio);
5397 return (SET_ERROR(error));
5403 zfs_getextattr_impl(struct vop_getextattr_args *ap, boolean_t compat)
5405 znode_t *zp = VTOZ(ap->a_vp);
5406 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5407 char attrname[EXTATTR_MAXNAMELEN+1];
5410 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5411 sizeof (attrname), compat);
5416 if (zfsvfs->z_use_sa && zp->z_is_sa)
5417 error = zfs_getextattr_sa(ap, attrname);
5418 if (error == ENOENT)
5419 error = zfs_getextattr_dir(ap, attrname);
5424 * Vnode operation to retrieve a named extended attribute.
5427 zfs_getextattr(struct vop_getextattr_args *ap)
5429 znode_t *zp = VTOZ(ap->a_vp);
5430 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5434 * If the xattr property is off, refuse the request.
5436 if (!(zfsvfs->z_flags & ZSB_XATTR))
5437 return (SET_ERROR(EOPNOTSUPP));
5439 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5440 ap->a_cred, ap->a_td, VREAD);
5442 return (SET_ERROR(error));
5444 error = zfs_check_attrname(ap->a_name);
5448 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5451 rw_enter(&zp->z_xattr_lock, RW_READER);
5453 error = zfs_getextattr_impl(ap, zfs_xattr_compat);
5454 if ((error == ENOENT || error == ENOATTR) &&
5455 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5457 * Fall back to the alternate namespace format if we failed to
5458 * find a user xattr.
5460 error = zfs_getextattr_impl(ap, !zfs_xattr_compat);
5463 rw_exit(&zp->z_xattr_lock);
5464 zfs_exit(zfsvfs, FTAG);
5465 if (error == ENOENT)
5466 error = SET_ERROR(ENOATTR);
5470 #ifndef _SYS_SYSPROTO_H_
5471 struct vop_deleteextattr {
5472 IN struct vnode *a_vp;
5473 IN int a_attrnamespace;
5474 IN const char *a_name;
5475 IN struct ucred *a_cred;
5476 IN struct thread *a_td;
5481 zfs_deleteextattr_dir(struct vop_deleteextattr_args *ap, const char *attrname)
5483 struct nameidata nd;
5484 vnode_t *xvp = NULL, *vp;
5487 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5488 LOOKUP_XATTR, B_FALSE);
5492 #if __FreeBSD_version < 1400043
5493 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5494 UIO_SYSSPACE, attrname, xvp, ap->a_td);
5496 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5497 UIO_SYSSPACE, attrname, xvp);
5501 return (SET_ERROR(error));
5504 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5508 if (vp == nd.ni_dvp)
5517 zfs_deleteextattr_sa(struct vop_deleteextattr_args *ap, const char *attrname)
5519 znode_t *zp = VTOZ(ap->a_vp);
5523 error = zfs_ensure_xattr_cached(zp);
5527 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5528 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5530 nvl = zp->z_xattr_cached;
5531 error = nvlist_remove(nvl, attrname, DATA_TYPE_BYTE_ARRAY);
5533 error = SET_ERROR(error);
5535 error = zfs_sa_set_xattr(zp, attrname, NULL, 0);
5537 zp->z_xattr_cached = NULL;
5544 zfs_deleteextattr_impl(struct vop_deleteextattr_args *ap, boolean_t compat)
5546 znode_t *zp = VTOZ(ap->a_vp);
5547 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5548 char attrname[EXTATTR_MAXNAMELEN+1];
5551 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5552 sizeof (attrname), compat);
5557 if (zfsvfs->z_use_sa && zp->z_is_sa)
5558 error = zfs_deleteextattr_sa(ap, attrname);
5559 if (error == ENOENT)
5560 error = zfs_deleteextattr_dir(ap, attrname);
5565 * Vnode operation to remove a named attribute.
5568 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5570 znode_t *zp = VTOZ(ap->a_vp);
5571 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5575 * If the xattr property is off, refuse the request.
5577 if (!(zfsvfs->z_flags & ZSB_XATTR))
5578 return (SET_ERROR(EOPNOTSUPP));
5580 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5581 ap->a_cred, ap->a_td, VWRITE);
5583 return (SET_ERROR(error));
5585 error = zfs_check_attrname(ap->a_name);
5589 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5591 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5593 error = zfs_deleteextattr_impl(ap, zfs_xattr_compat);
5594 if ((error == ENOENT || error == ENOATTR) &&
5595 ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5597 * Fall back to the alternate namespace format if we failed to
5598 * find a user xattr.
5600 error = zfs_deleteextattr_impl(ap, !zfs_xattr_compat);
5603 rw_exit(&zp->z_xattr_lock);
5604 zfs_exit(zfsvfs, FTAG);
5605 if (error == ENOENT)
5606 error = SET_ERROR(ENOATTR);
5610 #ifndef _SYS_SYSPROTO_H_
5611 struct vop_setextattr {
5612 IN struct vnode *a_vp;
5613 IN int a_attrnamespace;
5614 IN const char *a_name;
5615 INOUT struct uio *a_uio;
5616 IN struct ucred *a_cred;
5617 IN struct thread *a_td;
5622 zfs_setextattr_dir(struct vop_setextattr_args *ap, const char *attrname)
5624 struct thread *td = ap->a_td;
5625 struct nameidata nd;
5627 vnode_t *xvp = NULL, *vp;
5630 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5631 LOOKUP_XATTR | CREATE_XATTR_DIR, B_FALSE);
5635 flags = FFLAGS(O_WRONLY | O_CREAT);
5636 #if __FreeBSD_version < 1400043
5637 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp, td);
5639 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname, xvp);
5641 error = vn_open_cred(&nd, &flags, 0600, VN_OPEN_INVFS, ap->a_cred,
5644 return (SET_ERROR(error));
5650 error = VOP_SETATTR(vp, &va, ap->a_cred);
5652 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5655 vn_close(vp, flags, ap->a_cred, td);
5660 zfs_setextattr_sa(struct vop_setextattr_args *ap, const char *attrname)
5662 znode_t *zp = VTOZ(ap->a_vp);
5667 error = zfs_ensure_xattr_cached(zp);
5671 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
5672 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5674 nvl = zp->z_xattr_cached;
5675 size_t entry_size = ap->a_uio->uio_resid;
5676 if (entry_size > DXATTR_MAX_ENTRY_SIZE)
5677 return (SET_ERROR(EFBIG));
5678 error = nvlist_size(nvl, &sa_size, NV_ENCODE_XDR);
5680 return (SET_ERROR(error));
5681 if (sa_size > DXATTR_MAX_SA_SIZE)
5682 return (SET_ERROR(EFBIG));
5683 uchar_t *buf = kmem_alloc(entry_size, KM_SLEEP);
5684 error = uiomove(buf, entry_size, ap->a_uio);
5686 error = SET_ERROR(error);
5688 error = nvlist_add_byte_array(nvl, attrname, buf, entry_size);
5690 error = SET_ERROR(error);
5693 error = zfs_sa_set_xattr(zp, attrname, buf, entry_size);
5694 kmem_free(buf, entry_size);
5696 zp->z_xattr_cached = NULL;
5703 zfs_setextattr_impl(struct vop_setextattr_args *ap, boolean_t compat)
5705 znode_t *zp = VTOZ(ap->a_vp);
5706 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5707 char attrname[EXTATTR_MAXNAMELEN+1];
5710 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5711 sizeof (attrname), compat);
5715 struct vop_deleteextattr_args vda = {
5717 .a_attrnamespace = ap->a_attrnamespace,
5718 .a_name = ap->a_name,
5719 .a_cred = ap->a_cred,
5723 if (zfsvfs->z_use_sa && zp->z_is_sa && zfsvfs->z_xattr_sa) {
5724 error = zfs_setextattr_sa(ap, attrname);
5727 * Successfully put into SA, we need to clear the one
5728 * in dir if present.
5730 zfs_deleteextattr_dir(&vda, attrname);
5734 error = zfs_setextattr_dir(ap, attrname);
5735 if (error == 0 && zp->z_is_sa) {
5737 * Successfully put into dir, we need to clear the one
5740 zfs_deleteextattr_sa(&vda, attrname);
5743 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5745 * Also clear all versions of the alternate compat name.
5747 zfs_deleteextattr_impl(&vda, !compat);
5753 * Vnode operation to set a named attribute.
5756 zfs_setextattr(struct vop_setextattr_args *ap)
5758 znode_t *zp = VTOZ(ap->a_vp);
5759 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5763 * If the xattr property is off, refuse the request.
5765 if (!(zfsvfs->z_flags & ZSB_XATTR))
5766 return (SET_ERROR(EOPNOTSUPP));
5768 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5769 ap->a_cred, ap->a_td, VWRITE);
5771 return (SET_ERROR(error));
5773 error = zfs_check_attrname(ap->a_name);
5777 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5779 rw_enter(&zp->z_xattr_lock, RW_WRITER);
5781 error = zfs_setextattr_impl(ap, zfs_xattr_compat);
5783 rw_exit(&zp->z_xattr_lock);
5784 zfs_exit(zfsvfs, FTAG);
5788 #ifndef _SYS_SYSPROTO_H_
5789 struct vop_listextattr {
5790 IN struct vnode *a_vp;
5791 IN int a_attrnamespace;
5792 INOUT struct uio *a_uio;
5794 IN struct ucred *a_cred;
5795 IN struct thread *a_td;
5800 zfs_listextattr_dir(struct vop_listextattr_args *ap, const char *attrprefix)
5802 struct thread *td = ap->a_td;
5803 struct nameidata nd;
5804 uint8_t dirbuf[sizeof (struct dirent)];
5807 vnode_t *xvp = NULL, *vp;
5810 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred,
5811 LOOKUP_XATTR, B_FALSE);
5814 * ENOATTR means that the EA directory does not yet exist,
5815 * i.e. there are no extended attributes there.
5817 if (error == ENOATTR)
5822 #if __FreeBSD_version < 1400043
5823 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5824 UIO_SYSSPACE, ".", xvp, td);
5826 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5827 UIO_SYSSPACE, ".", xvp);
5831 return (SET_ERROR(error));
5835 auio.uio_iov = &aiov;
5836 auio.uio_iovcnt = 1;
5837 auio.uio_segflg = UIO_SYSSPACE;
5839 auio.uio_rw = UIO_READ;
5840 auio.uio_offset = 0;
5842 size_t plen = strlen(attrprefix);
5845 aiov.iov_base = (void *)dirbuf;
5846 aiov.iov_len = sizeof (dirbuf);
5847 auio.uio_resid = sizeof (dirbuf);
5848 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5851 int done = sizeof (dirbuf) - auio.uio_resid;
5852 for (int pos = 0; pos < done; ) {
5853 struct dirent *dp = (struct dirent *)(dirbuf + pos);
5854 pos += dp->d_reclen;
5856 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5857 * is what we get when attribute was created on Solaris.
5859 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5861 else if (plen == 0 &&
5862 ZFS_XA_NS_PREFIX_FORBIDDEN(dp->d_name))
5864 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5866 uint8_t nlen = dp->d_namlen - plen;
5867 if (ap->a_size != NULL) {
5868 *ap->a_size += 1 + nlen;
5869 } else if (ap->a_uio != NULL) {
5871 * Format of extattr name entry is one byte for
5872 * length and the rest for name.
5874 error = uiomove(&nlen, 1, ap->a_uio);
5876 char *namep = dp->d_name + plen;
5877 error = uiomove(namep, nlen, ap->a_uio);
5880 error = SET_ERROR(error);
5885 } while (!eof && error == 0);
5892 zfs_listextattr_sa(struct vop_listextattr_args *ap, const char *attrprefix)
5894 znode_t *zp = VTOZ(ap->a_vp);
5897 error = zfs_ensure_xattr_cached(zp);
5901 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
5902 ASSERT3P(zp->z_xattr_cached, !=, NULL);
5904 size_t plen = strlen(attrprefix);
5905 nvpair_t *nvp = NULL;
5906 while ((nvp = nvlist_next_nvpair(zp->z_xattr_cached, nvp)) != NULL) {
5907 ASSERT3U(nvpair_type(nvp), ==, DATA_TYPE_BYTE_ARRAY);
5909 const char *name = nvpair_name(nvp);
5910 if (plen == 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name))
5912 else if (strncmp(name, attrprefix, plen) != 0)
5914 uint8_t nlen = strlen(name) - plen;
5915 if (ap->a_size != NULL) {
5916 *ap->a_size += 1 + nlen;
5917 } else if (ap->a_uio != NULL) {
5919 * Format of extattr name entry is one byte for
5920 * length and the rest for name.
5922 error = uiomove(&nlen, 1, ap->a_uio);
5924 char *namep = __DECONST(char *, name) + plen;
5925 error = uiomove(namep, nlen, ap->a_uio);
5928 error = SET_ERROR(error);
5938 zfs_listextattr_impl(struct vop_listextattr_args *ap, boolean_t compat)
5940 znode_t *zp = VTOZ(ap->a_vp);
5941 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5942 char attrprefix[16];
5945 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5946 sizeof (attrprefix), compat);
5950 if (zfsvfs->z_use_sa && zp->z_is_sa)
5951 error = zfs_listextattr_sa(ap, attrprefix);
5953 error = zfs_listextattr_dir(ap, attrprefix);
5958 * Vnode operation to retrieve extended attributes on a vnode.
5961 zfs_listextattr(struct vop_listextattr_args *ap)
5963 znode_t *zp = VTOZ(ap->a_vp);
5964 zfsvfs_t *zfsvfs = ZTOZSB(zp);
5967 if (ap->a_size != NULL)
5971 * If the xattr property is off, refuse the request.
5973 if (!(zfsvfs->z_flags & ZSB_XATTR))
5974 return (SET_ERROR(EOPNOTSUPP));
5976 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5977 ap->a_cred, ap->a_td, VREAD);
5979 return (SET_ERROR(error));
5981 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
5983 rw_enter(&zp->z_xattr_lock, RW_READER);
5985 error = zfs_listextattr_impl(ap, zfs_xattr_compat);
5986 if (error == 0 && ap->a_attrnamespace == EXTATTR_NAMESPACE_USER) {
5987 /* Also list user xattrs with the alternate format. */
5988 error = zfs_listextattr_impl(ap, !zfs_xattr_compat);
5991 rw_exit(&zp->z_xattr_lock);
5992 zfs_exit(zfsvfs, FTAG);
5996 #ifndef _SYS_SYSPROTO_H_
5997 struct vop_getacl_args {
6007 zfs_freebsd_getacl(struct vop_getacl_args *ap)
6010 vsecattr_t vsecattr;
6012 if (ap->a_type != ACL_TYPE_NFS4)
6015 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6016 if ((error = zfs_getsecattr(VTOZ(ap->a_vp),
6017 &vsecattr, 0, ap->a_cred)))
6020 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp,
6021 vsecattr.vsa_aclcnt);
6022 if (vsecattr.vsa_aclentp != NULL)
6023 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6028 #ifndef _SYS_SYSPROTO_H_
6029 struct vop_setacl_args {
6039 zfs_freebsd_setacl(struct vop_setacl_args *ap)
6042 vsecattr_t vsecattr;
6043 int aclbsize; /* size of acl list in bytes */
6046 if (ap->a_type != ACL_TYPE_NFS4)
6049 if (ap->a_aclp == NULL)
6052 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6056 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6057 * splitting every entry into two and appending "canonical six"
6058 * entries at the end. Don't allow for setting an ACL that would
6059 * cause chmod(2) to run out of ACL entries.
6061 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6064 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6068 vsecattr.vsa_mask = VSA_ACE;
6069 aclbsize = ap->a_aclp->acl_cnt * sizeof (ace_t);
6070 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6071 aaclp = vsecattr.vsa_aclentp;
6072 vsecattr.vsa_aclentsz = aclbsize;
6074 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6075 error = zfs_setsecattr(VTOZ(ap->a_vp), &vsecattr, 0, ap->a_cred);
6076 kmem_free(aaclp, aclbsize);
6081 #ifndef _SYS_SYSPROTO_H_
6082 struct vop_aclcheck_args {
6092 zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap)
6095 return (EOPNOTSUPP);
6099 zfs_vptocnp(struct vop_vptocnp_args *ap)
6101 vnode_t *covered_vp;
6102 vnode_t *vp = ap->a_vp;
6103 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
6104 znode_t *zp = VTOZ(vp);
6108 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6112 * If we are a snapshot mounted under .zfs, run the operation
6113 * on the covered vnode.
6115 if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) {
6116 char name[MAXNAMLEN + 1];
6120 error = zfs_znode_parent_and_name(zp, &dzp, name);
6123 if (*ap->a_buflen < len)
6124 error = SET_ERROR(ENOMEM);
6127 *ap->a_buflen -= len;
6128 memcpy(ap->a_buf + *ap->a_buflen, name, len);
6129 *ap->a_vpp = ZTOV(dzp);
6131 zfs_exit(zfsvfs, FTAG);
6134 zfs_exit(zfsvfs, FTAG);
6136 covered_vp = vp->v_mount->mnt_vnodecovered;
6137 #if __FreeBSD_version >= 1300045
6138 enum vgetstate vs = vget_prep(covered_vp);
6142 ltype = VOP_ISLOCKED(vp);
6144 #if __FreeBSD_version >= 1300045
6145 error = vget_finish(covered_vp, LK_SHARED, vs);
6147 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
6150 #if __FreeBSD_version >= 1300123
6151 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_buf,
6154 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
6155 ap->a_buf, ap->a_buflen);
6159 vn_lock(vp, ltype | LK_RETRY);
6160 if (VN_IS_DOOMED(vp))
6161 error = SET_ERROR(ENOENT);
6165 #if __FreeBSD_version >= 1400032
6167 zfs_deallocate(struct vop_deallocate_args *ap)
6169 znode_t *zp = VTOZ(ap->a_vp);
6170 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6172 off_t off, len, file_sz;
6175 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
6179 * Callers might not be able to detect properly that we are read-only,
6180 * so check it explicitly here.
6182 if (zfs_is_readonly(zfsvfs)) {
6183 zfs_exit(zfsvfs, FTAG);
6184 return (SET_ERROR(EROFS));
6187 zilog = zfsvfs->z_log;
6188 off = *ap->a_offset;
6190 file_sz = zp->z_size;
6191 if (off + len > file_sz)
6192 len = file_sz - off;
6193 /* Fast path for out-of-range request. */
6196 zfs_exit(zfsvfs, FTAG);
6200 error = zfs_freesp(zp, off, len, O_RDWR, TRUE);
6202 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS ||
6203 (ap->a_ioflag & IO_SYNC) != 0)
6204 zil_commit(zilog, zp->z_id);
6205 *ap->a_offset = off + len;
6209 zfs_exit(zfsvfs, FTAG);
6214 struct vop_vector zfs_vnodeops;
6215 struct vop_vector zfs_fifoops;
6216 struct vop_vector zfs_shareops;
6218 struct vop_vector zfs_vnodeops = {
6219 .vop_default = &default_vnodeops,
6220 .vop_inactive = zfs_freebsd_inactive,
6221 #if __FreeBSD_version >= 1300042
6222 .vop_need_inactive = zfs_freebsd_need_inactive,
6224 .vop_reclaim = zfs_freebsd_reclaim,
6225 #if __FreeBSD_version >= 1300102
6226 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6228 #if __FreeBSD_version >= 1300139
6229 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6231 .vop_access = zfs_freebsd_access,
6232 .vop_allocate = VOP_EINVAL,
6233 #if __FreeBSD_version >= 1400032
6234 .vop_deallocate = zfs_deallocate,
6236 .vop_lookup = zfs_cache_lookup,
6237 .vop_cachedlookup = zfs_freebsd_cachedlookup,
6238 .vop_getattr = zfs_freebsd_getattr,
6239 .vop_setattr = zfs_freebsd_setattr,
6240 .vop_create = zfs_freebsd_create,
6241 .vop_mknod = (vop_mknod_t *)zfs_freebsd_create,
6242 .vop_mkdir = zfs_freebsd_mkdir,
6243 .vop_readdir = zfs_freebsd_readdir,
6244 .vop_fsync = zfs_freebsd_fsync,
6245 .vop_open = zfs_freebsd_open,
6246 .vop_close = zfs_freebsd_close,
6247 .vop_rmdir = zfs_freebsd_rmdir,
6248 .vop_ioctl = zfs_freebsd_ioctl,
6249 .vop_link = zfs_freebsd_link,
6250 .vop_symlink = zfs_freebsd_symlink,
6251 .vop_readlink = zfs_freebsd_readlink,
6252 .vop_read = zfs_freebsd_read,
6253 .vop_write = zfs_freebsd_write,
6254 .vop_remove = zfs_freebsd_remove,
6255 .vop_rename = zfs_freebsd_rename,
6256 .vop_pathconf = zfs_freebsd_pathconf,
6257 .vop_bmap = zfs_freebsd_bmap,
6258 .vop_fid = zfs_freebsd_fid,
6259 .vop_getextattr = zfs_getextattr,
6260 .vop_deleteextattr = zfs_deleteextattr,
6261 .vop_setextattr = zfs_setextattr,
6262 .vop_listextattr = zfs_listextattr,
6263 .vop_getacl = zfs_freebsd_getacl,
6264 .vop_setacl = zfs_freebsd_setacl,
6265 .vop_aclcheck = zfs_freebsd_aclcheck,
6266 .vop_getpages = zfs_freebsd_getpages,
6267 .vop_putpages = zfs_freebsd_putpages,
6268 .vop_vptocnp = zfs_vptocnp,
6269 #if __FreeBSD_version >= 1300064
6270 .vop_lock1 = vop_lock,
6271 .vop_unlock = vop_unlock,
6272 .vop_islocked = vop_islocked,
6274 #if __FreeBSD_version >= 1400043
6275 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6278 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops);
6280 struct vop_vector zfs_fifoops = {
6281 .vop_default = &fifo_specops,
6282 .vop_fsync = zfs_freebsd_fsync,
6283 #if __FreeBSD_version >= 1300102
6284 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6286 #if __FreeBSD_version >= 1300139
6287 .vop_fplookup_symlink = zfs_freebsd_fplookup_symlink,
6289 .vop_access = zfs_freebsd_access,
6290 .vop_getattr = zfs_freebsd_getattr,
6291 .vop_inactive = zfs_freebsd_inactive,
6292 .vop_read = VOP_PANIC,
6293 .vop_reclaim = zfs_freebsd_reclaim,
6294 .vop_setattr = zfs_freebsd_setattr,
6295 .vop_write = VOP_PANIC,
6296 .vop_pathconf = zfs_freebsd_pathconf,
6297 .vop_fid = zfs_freebsd_fid,
6298 .vop_getacl = zfs_freebsd_getacl,
6299 .vop_setacl = zfs_freebsd_setacl,
6300 .vop_aclcheck = zfs_freebsd_aclcheck,
6301 #if __FreeBSD_version >= 1400043
6302 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6305 VFS_VOP_VECTOR_REGISTER(zfs_fifoops);
6308 * special share hidden files vnode operations template
6310 struct vop_vector zfs_shareops = {
6311 .vop_default = &default_vnodeops,
6312 #if __FreeBSD_version >= 1300121
6313 .vop_fplookup_vexec = VOP_EAGAIN,
6315 #if __FreeBSD_version >= 1300139
6316 .vop_fplookup_symlink = VOP_EAGAIN,
6318 .vop_access = zfs_freebsd_access,
6319 .vop_inactive = zfs_freebsd_inactive,
6320 .vop_reclaim = zfs_freebsd_reclaim,
6321 .vop_fid = zfs_freebsd_fid,
6322 .vop_pathconf = zfs_freebsd_pathconf,
6323 #if __FreeBSD_version >= 1400043
6324 .vop_add_writecount = vop_stdadd_writecount_nomsync,
6327 VFS_VOP_VECTOR_REGISTER(zfs_shareops);
6329 ZFS_MODULE_PARAM(zfs, zfs_, xattr_compat, INT, ZMOD_RW,
6330 "Use legacy ZFS xattr naming for writing new user namespace xattrs");