4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2017 Nexenta Systems, Inc.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <security/mac/mac_framework.h>
39 #include <sys/endian.h>
41 #include <sys/vnode.h>
42 #if __FreeBSD_version >= 1300102
45 #include <sys/dirent.h>
49 #include <sys/taskq.h>
51 #include <sys/atomic.h>
52 #include <sys/namei.h>
54 #include <sys/cmn_err.h>
56 #include <sys/sysproto.h>
57 #include <sys/errno.h>
58 #include <sys/unistd.h>
59 #include <sys/zfs_dir.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/dmu_objset.h>
69 #include <sys/policy.h>
70 #include <sys/sunddi.h>
71 #include <sys/filio.h>
73 #include <sys/zfs_ctldir.h>
74 #include <sys/zfs_fuid.h>
75 #include <sys/zfs_quota.h>
76 #include <sys/zfs_sa.h>
77 #include <sys/zfs_rlock.h>
80 #include <sys/sched.h>
82 #include <sys/vmmeter.h>
83 #include <vm/vm_param.h>
85 #include <sys/zfs_vnops.h>
86 #include <sys/module.h>
87 #include <sys/sysent.h>
88 #include <sys/dmu_impl.h>
90 #include <sys/zfeature.h>
92 #include <vm/vm_object.h>
94 #include <sys/extattr.h>
98 #define VN_OPEN_INVFS 0x0
103 #if __FreeBSD_version < 1300103
104 #define NDFREE_PNBUF(ndp) NDFREE((ndp), NDF_ONLY_PNBUF)
107 #if __FreeBSD_version >= 1300047
108 #define vm_page_wire_lock(pp)
109 #define vm_page_wire_unlock(pp)
111 #define vm_page_wire_lock(pp) vm_page_lock(pp)
112 #define vm_page_wire_unlock(pp) vm_page_unlock(pp)
115 #ifdef DEBUG_VFS_LOCKS
116 #define VNCHECKREF(vp) \
117 VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp, \
118 ("%s: wrong ref counts", __func__));
120 #define VNCHECKREF(vp)
123 #if __FreeBSD_version >= 1400045
124 typedef uint64_t cookie_t
;
126 typedef ulong_t cookie_t
;
132 * Each vnode op performs some logical unit of work. To do this, the ZPL must
133 * properly lock its in-core state, create a DMU transaction, do the work,
134 * record this work in the intent log (ZIL), commit the DMU transaction,
135 * and wait for the intent log to commit if it is a synchronous operation.
136 * Moreover, the vnode ops must work in both normal and log replay context.
137 * The ordering of events is important to avoid deadlocks and references
138 * to freed memory. The example below illustrates the following Big Rules:
140 * (1) A check must be made in each zfs thread for a mounted file system.
141 * This is done avoiding races using zfs_enter(zfsvfs).
142 * A zfs_exit(zfsvfs) is needed before all returns. Any znodes
143 * must be checked with zfs_verify_zp(zp). Both of these macros
144 * can return EIO from the calling function.
146 * (2) VN_RELE() should always be the last thing except for zil_commit()
147 * (if necessary) and zfs_exit(). This is for 3 reasons:
148 * First, if it's the last reference, the vnode/znode
149 * can be freed, so the zp may point to freed memory. Second, the last
150 * reference will call zfs_zinactive(), which may induce a lot of work --
151 * pushing cached pages (which acquires range locks) and syncing out
152 * cached atime changes. Third, zfs_zinactive() may require a new tx,
153 * which could deadlock the system if you were already holding one.
154 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
156 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
157 * as they can span dmu_tx_assign() calls.
159 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
160 * dmu_tx_assign(). This is critical because we don't want to block
161 * while holding locks.
163 * If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT. This
164 * reduces lock contention and CPU usage when we must wait (note that if
165 * throughput is constrained by the storage, nearly every transaction
168 * Note, in particular, that if a lock is sometimes acquired before
169 * the tx assigns, and sometimes after (e.g. z_lock), then failing
170 * to use a non-blocking assign can deadlock the system. The scenario:
172 * Thread A has grabbed a lock before calling dmu_tx_assign().
173 * Thread B is in an already-assigned tx, and blocks for this lock.
174 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
175 * forever, because the previous txg can't quiesce until B's tx commits.
177 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
178 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
179 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
180 * to indicate that this operation has already called dmu_tx_wait().
181 * This will ensure that we don't retry forever, waiting a short bit
184 * (5) If the operation succeeded, generate the intent log entry for it
185 * before dropping locks. This ensures that the ordering of events
186 * in the intent log matches the order in which they actually occurred.
187 * During ZIL replay the zfs_log_* functions will update the sequence
188 * number to indicate the zil transaction has replayed.
190 * (6) At the end of each vnode op, the DMU tx must always commit,
191 * regardless of whether there were any errors.
193 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
194 * to ensure that synchronous semantics are provided when necessary.
196 * In general, this is how things should be ordered in each vnode op:
198 * zfs_enter(zfsvfs); // exit if unmounted
200 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
201 * rw_enter(...); // grab any other locks you need
202 * tx = dmu_tx_create(...); // get DMU tx
203 * dmu_tx_hold_*(); // hold each object you might modify
204 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
206 * rw_exit(...); // drop locks
207 * zfs_dirent_unlock(dl); // unlock directory entry
208 * VN_RELE(...); // release held vnodes
209 * if (error == ERESTART) {
215 * dmu_tx_abort(tx); // abort DMU tx
216 * zfs_exit(zfsvfs); // finished in zfs
217 * return (error); // really out of space
219 * error = do_real_work(); // do whatever this VOP does
221 * zfs_log_*(...); // on success, make ZIL entry
222 * dmu_tx_commit(tx); // commit DMU tx -- error or not
223 * rw_exit(...); // drop locks
224 * zfs_dirent_unlock(dl); // unlock directory entry
225 * VN_RELE(...); // release held vnodes
226 * zil_commit(zilog, foid); // synchronous when necessary
227 * zfs_exit(zfsvfs); // finished in zfs
228 * return (error); // done, report error
231 zfs_open(vnode_t
**vpp
, int flag
, cred_t
*cr
)
234 znode_t
*zp
= VTOZ(*vpp
);
235 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
238 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
241 if ((flag
& FWRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
242 ((flag
& FAPPEND
) == 0)) {
243 zfs_exit(zfsvfs
, FTAG
);
244 return (SET_ERROR(EPERM
));
247 /* Keep a count of the synchronous opens in the znode */
249 atomic_inc_32(&zp
->z_sync_cnt
);
251 zfs_exit(zfsvfs
, FTAG
);
256 zfs_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
)
258 (void) offset
, (void) cr
;
259 znode_t
*zp
= VTOZ(vp
);
260 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
263 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
266 /* Decrement the synchronous opens in the znode */
267 if ((flag
& O_SYNC
) && (count
== 1))
268 atomic_dec_32(&zp
->z_sync_cnt
);
270 zfs_exit(zfsvfs
, FTAG
);
275 zfs_ioctl(vnode_t
*vp
, ulong_t com
, intptr_t data
, int flag
, cred_t
*cred
,
278 (void) flag
, (void) cred
, (void) rvalp
;
288 * The following two ioctls are used by bfu. Faking out,
289 * necessary to avoid bfu errors.
301 off
= *(offset_t
*)data
;
302 /* offset parameter is in/out */
303 error
= zfs_holey(VTOZ(vp
), com
, &off
);
306 *(offset_t
*)data
= off
;
310 return (SET_ERROR(ENOTTY
));
314 page_busy(vnode_t
*vp
, int64_t start
, int64_t off
, int64_t nbytes
)
321 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
322 * aligned boundaries, if the range is not aligned. As a result a
323 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
324 * It may happen that all DEV_BSIZE subranges are marked clean and thus
325 * the whole page would be considered clean despite have some
327 * For this reason we should shrink the range to DEV_BSIZE aligned
328 * boundaries before calling vm_page_clear_dirty.
330 end
= rounddown2(off
+ nbytes
, DEV_BSIZE
);
331 off
= roundup2(off
, DEV_BSIZE
);
335 zfs_vmobject_assert_wlocked_12(obj
);
336 #if __FreeBSD_version < 1300050
338 if ((pp
= vm_page_lookup(obj
, OFF_TO_IDX(start
))) != NULL
&&
340 if (vm_page_xbusied(pp
)) {
342 * Reference the page before unlocking and
343 * sleeping so that the page daemon is less
344 * likely to reclaim it.
346 vm_page_reference(pp
);
348 zfs_vmobject_wunlock(obj
);
349 vm_page_busy_sleep(pp
, "zfsmwb", true);
350 zfs_vmobject_wlock(obj
);
354 } else if (pp
!= NULL
) {
359 ASSERT3U(pp
->valid
, ==, VM_PAGE_BITS_ALL
);
360 vm_object_pip_add(obj
, 1);
361 pmap_remove_write(pp
);
363 vm_page_clear_dirty(pp
, off
, nbytes
);
368 vm_page_grab_valid_unlocked(&pp
, obj
, OFF_TO_IDX(start
),
369 VM_ALLOC_NOCREAT
| VM_ALLOC_SBUSY
| VM_ALLOC_NORMAL
|
372 ASSERT3U(pp
->valid
, ==, VM_PAGE_BITS_ALL
);
373 vm_object_pip_add(obj
, 1);
374 pmap_remove_write(pp
);
376 vm_page_clear_dirty(pp
, off
, nbytes
);
383 page_unbusy(vm_page_t pp
)
387 #if __FreeBSD_version >= 1300041
388 vm_object_pip_wakeup(pp
->object
);
390 vm_object_pip_subtract(pp
->object
, 1);
394 #if __FreeBSD_version > 1300051
396 page_hold(vnode_t
*vp
, int64_t start
)
402 vm_page_grab_valid_unlocked(&m
, obj
, OFF_TO_IDX(start
),
403 VM_ALLOC_NOCREAT
| VM_ALLOC_WIRED
| VM_ALLOC_IGN_SBUSY
|
409 page_hold(vnode_t
*vp
, int64_t start
)
415 zfs_vmobject_assert_wlocked(obj
);
418 if ((pp
= vm_page_lookup(obj
, OFF_TO_IDX(start
))) != NULL
&&
420 if (vm_page_xbusied(pp
)) {
422 * Reference the page before unlocking and
423 * sleeping so that the page daemon is less
424 * likely to reclaim it.
426 vm_page_reference(pp
);
428 zfs_vmobject_wunlock(obj
);
429 vm_page_busy_sleep(pp
, "zfsmwb", true);
430 zfs_vmobject_wlock(obj
);
434 ASSERT3U(pp
->valid
, ==, VM_PAGE_BITS_ALL
);
435 vm_page_wire_lock(pp
);
437 vm_page_wire_unlock(pp
);
448 page_unhold(vm_page_t pp
)
451 vm_page_wire_lock(pp
);
452 #if __FreeBSD_version >= 1300035
453 vm_page_unwire(pp
, PQ_ACTIVE
);
457 vm_page_wire_unlock(pp
);
461 * When a file is memory mapped, we must keep the IO data synchronized
462 * between the DMU cache and the memory mapped pages. What this means:
464 * On Write: If we find a memory mapped page, we write to *both*
465 * the page and the dmu buffer.
468 update_pages(znode_t
*zp
, int64_t start
, int len
, objset_t
*os
)
472 vnode_t
*vp
= ZTOV(zp
);
476 ASSERT3P(vp
->v_mount
, !=, NULL
);
478 ASSERT3P(obj
, !=, NULL
);
480 off
= start
& PAGEOFFSET
;
481 zfs_vmobject_wlock_12(obj
);
482 #if __FreeBSD_version >= 1300041
483 vm_object_pip_add(obj
, 1);
485 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
487 int nbytes
= imin(PAGESIZE
- off
, len
);
489 if ((pp
= page_busy(vp
, start
, off
, nbytes
)) != NULL
) {
490 zfs_vmobject_wunlock_12(obj
);
492 va
= zfs_map_page(pp
, &sf
);
493 (void) dmu_read(os
, zp
->z_id
, start
+ off
, nbytes
,
494 va
+ off
, DMU_READ_PREFETCH
);
497 zfs_vmobject_wlock_12(obj
);
503 #if __FreeBSD_version >= 1300041
504 vm_object_pip_wakeup(obj
);
506 vm_object_pip_wakeupn(obj
, 0);
508 zfs_vmobject_wunlock_12(obj
);
512 * Read with UIO_NOCOPY flag means that sendfile(2) requests
513 * ZFS to populate a range of page cache pages with data.
515 * NOTE: this function could be optimized to pre-allocate
516 * all pages in advance, drain exclusive busy on all of them,
517 * map them into contiguous KVA region and populate them
518 * in one single dmu_read() call.
521 mappedread_sf(znode_t
*zp
, int nbytes
, zfs_uio_t
*uio
)
523 vnode_t
*vp
= ZTOV(zp
);
524 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
533 ASSERT3U(zfs_uio_segflg(uio
), ==, UIO_NOCOPY
);
534 ASSERT3P(vp
->v_mount
, !=, NULL
);
536 ASSERT3P(obj
, !=, NULL
);
537 ASSERT0(zfs_uio_offset(uio
) & PAGEOFFSET
);
539 zfs_vmobject_wlock_12(obj
);
540 for (start
= zfs_uio_offset(uio
); len
> 0; start
+= PAGESIZE
) {
541 int bytes
= MIN(PAGESIZE
, len
);
543 pp
= vm_page_grab_unlocked(obj
, OFF_TO_IDX(start
),
544 VM_ALLOC_SBUSY
| VM_ALLOC_NORMAL
| VM_ALLOC_IGN_SBUSY
);
545 if (vm_page_none_valid(pp
)) {
546 zfs_vmobject_wunlock_12(obj
);
547 va
= zfs_map_page(pp
, &sf
);
548 error
= dmu_read(os
, zp
->z_id
, start
, bytes
, va
,
550 if (bytes
!= PAGESIZE
&& error
== 0)
551 memset(va
+ bytes
, 0, PAGESIZE
- bytes
);
553 zfs_vmobject_wlock_12(obj
);
554 #if __FreeBSD_version >= 1300081
557 vm_page_activate(pp
);
558 vm_page_do_sunbusy(pp
);
560 zfs_vmobject_wlock(obj
);
561 if (!vm_page_wired(pp
) && pp
->valid
== 0 &&
562 vm_page_busy_tryupgrade(pp
))
566 zfs_vmobject_wunlock(obj
);
569 vm_page_do_sunbusy(pp
);
572 if (pp
->wire_count
== 0 && pp
->valid
== 0 &&
576 pp
->valid
= VM_PAGE_BITS_ALL
;
577 vm_page_activate(pp
);
582 ASSERT3U(pp
->valid
, ==, VM_PAGE_BITS_ALL
);
583 vm_page_do_sunbusy(pp
);
587 zfs_uio_advance(uio
, bytes
);
590 zfs_vmobject_wunlock_12(obj
);
595 * When a file is memory mapped, we must keep the IO data synchronized
596 * between the DMU cache and the memory mapped pages. What this means:
598 * On Read: We "read" preferentially from memory mapped pages,
599 * else we default from the dmu buffer.
601 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
602 * the file is memory mapped.
605 mappedread(znode_t
*zp
, int nbytes
, zfs_uio_t
*uio
)
607 vnode_t
*vp
= ZTOV(zp
);
614 ASSERT3P(vp
->v_mount
, !=, NULL
);
616 ASSERT3P(obj
, !=, NULL
);
618 start
= zfs_uio_offset(uio
);
619 off
= start
& PAGEOFFSET
;
620 zfs_vmobject_wlock_12(obj
);
621 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
623 uint64_t bytes
= MIN(PAGESIZE
- off
, len
);
625 if ((pp
= page_hold(vp
, start
))) {
629 zfs_vmobject_wunlock_12(obj
);
630 va
= zfs_map_page(pp
, &sf
);
631 error
= vn_io_fault_uiomove(va
+ off
, bytes
,
632 GET_UIO_STRUCT(uio
));
634 zfs_vmobject_wlock_12(obj
);
637 zfs_vmobject_wunlock_12(obj
);
638 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
640 zfs_vmobject_wlock_12(obj
);
647 zfs_vmobject_wunlock_12(obj
);
652 zfs_write_simple(znode_t
*zp
, const void *data
, size_t len
,
653 loff_t pos
, size_t *presid
)
658 error
= vn_rdwr(UIO_WRITE
, ZTOV(zp
), __DECONST(void *, data
), len
, pos
,
659 UIO_SYSSPACE
, IO_SYNC
, kcred
, NOCRED
, &resid
, curthread
);
662 return (SET_ERROR(error
));
663 } else if (presid
== NULL
) {
665 error
= SET_ERROR(EIO
);
674 zfs_zrele_async(znode_t
*zp
)
676 vnode_t
*vp
= ZTOV(zp
);
677 objset_t
*os
= ITOZSB(vp
)->z_os
;
679 VN_RELE_ASYNC(vp
, dsl_pool_zrele_taskq(dmu_objset_pool(os
)));
683 zfs_dd_callback(struct mount
*mp
, void *arg
, int lkflags
, struct vnode
**vpp
)
688 error
= vn_lock(*vpp
, lkflags
);
695 zfs_lookup_lock(vnode_t
*dvp
, vnode_t
*vp
, const char *name
, int lkflags
)
697 znode_t
*zdp
= VTOZ(dvp
);
698 zfsvfs_t
*zfsvfs __unused
= zdp
->z_zfsvfs
;
702 if (zfsvfs
->z_replay
== B_FALSE
)
703 ASSERT_VOP_LOCKED(dvp
, __func__
);
705 if (name
[0] == 0 || (name
[0] == '.' && name
[1] == 0)) {
706 ASSERT3P(dvp
, ==, vp
);
708 ltype
= lkflags
& LK_TYPE_MASK
;
709 if (ltype
!= VOP_ISLOCKED(dvp
)) {
710 if (ltype
== LK_EXCLUSIVE
)
711 vn_lock(dvp
, LK_UPGRADE
| LK_RETRY
);
712 else /* if (ltype == LK_SHARED) */
713 vn_lock(dvp
, LK_DOWNGRADE
| LK_RETRY
);
716 * Relock for the "." case could leave us with
719 if (VN_IS_DOOMED(dvp
)) {
721 return (SET_ERROR(ENOENT
));
725 } else if (name
[0] == '.' && name
[1] == '.' && name
[2] == 0) {
727 * Note that in this case, dvp is the child vnode, and we
728 * are looking up the parent vnode - exactly reverse from
729 * normal operation. Unlocking dvp requires some rather
730 * tricky unlock/relock dance to prevent mp from being freed;
731 * use vn_vget_ino_gen() which takes care of all that.
733 * XXX Note that there is a time window when both vnodes are
734 * unlocked. It is possible, although highly unlikely, that
735 * during that window the parent-child relationship between
736 * the vnodes may change, for example, get reversed.
737 * In that case we would have a wrong lock order for the vnodes.
738 * All other filesystems seem to ignore this problem, so we
740 * A potential solution could be implemented as follows:
741 * - using LK_NOWAIT when locking the second vnode and retrying
743 * - checking that the parent-child relationship still holds
744 * after locking both vnodes and retrying if it doesn't
746 error
= vn_vget_ino_gen(dvp
, zfs_dd_callback
, vp
, lkflags
, &vp
);
749 error
= vn_lock(vp
, lkflags
);
757 * Lookup an entry in a directory, or an extended attribute directory.
758 * If it exists, return a held vnode reference for it.
760 * IN: dvp - vnode of directory to search.
761 * nm - name of entry to lookup.
762 * pnp - full pathname to lookup [UNUSED].
763 * flags - LOOKUP_XATTR set if looking for an attribute.
764 * rdir - root directory vnode [UNUSED].
765 * cr - credentials of caller.
766 * ct - caller context
768 * OUT: vpp - vnode of located entry, NULL if not found.
770 * RETURN: 0 on success, error code on failure.
776 zfs_lookup(vnode_t
*dvp
, const char *nm
, vnode_t
**vpp
,
777 struct componentname
*cnp
, int nameiop
, cred_t
*cr
, int flags
,
780 znode_t
*zdp
= VTOZ(dvp
);
782 zfsvfs_t
*zfsvfs
= zdp
->z_zfsvfs
;
783 #if __FreeBSD_version > 1300124
789 * Fast path lookup, however we must skip DNLC lookup
790 * for case folding or normalizing lookups because the
791 * DNLC code only stores the passed in name. This means
792 * creating 'a' and removing 'A' on a case insensitive
793 * file system would work, but DNLC still thinks 'a'
794 * exists and won't let you create it again on the next
795 * pass through fast path.
797 if (!(flags
& LOOKUP_XATTR
)) {
798 if (dvp
->v_type
!= VDIR
) {
799 return (SET_ERROR(ENOTDIR
));
800 } else if (zdp
->z_sa_hdl
== NULL
) {
801 return (SET_ERROR(EIO
));
805 DTRACE_PROBE2(zfs__fastpath__lookup__miss
, vnode_t
*, dvp
,
808 if ((error
= zfs_enter_verify_zp(zfsvfs
, zdp
, FTAG
)) != 0)
811 #if __FreeBSD_version > 1300124
812 dvp_seqc
= vn_seqc_read_notmodify(dvp
);
817 if (flags
& LOOKUP_XATTR
) {
819 * If the xattr property is off, refuse the lookup request.
821 if (!(zfsvfs
->z_flags
& ZSB_XATTR
)) {
822 zfs_exit(zfsvfs
, FTAG
);
823 return (SET_ERROR(EOPNOTSUPP
));
827 * We don't allow recursive attributes..
828 * Maybe someday we will.
830 if (zdp
->z_pflags
& ZFS_XATTR
) {
831 zfs_exit(zfsvfs
, FTAG
);
832 return (SET_ERROR(EINVAL
));
835 if ((error
= zfs_get_xattrdir(VTOZ(dvp
), &zp
, cr
, flags
))) {
836 zfs_exit(zfsvfs
, FTAG
);
842 * Do we have permission to get into attribute directory?
844 error
= zfs_zaccess(zp
, ACE_EXECUTE
, 0, B_FALSE
, cr
, NULL
);
849 zfs_exit(zfsvfs
, FTAG
);
854 * Check accessibility of directory if we're not coming in via
859 if ((cnp
->cn_flags
& NOEXECCHECK
) != 0) {
860 cnp
->cn_flags
&= ~NOEXECCHECK
;
863 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
,
865 zfs_exit(zfsvfs
, FTAG
);
870 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
871 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
872 zfs_exit(zfsvfs
, FTAG
);
873 return (SET_ERROR(EILSEQ
));
878 * First handle the special cases.
880 if ((cnp
->cn_flags
& ISDOTDOT
) != 0) {
882 * If we are a snapshot mounted under .zfs, return
883 * the vp for the snapshot directory.
885 if (zdp
->z_id
== zfsvfs
->z_root
&& zfsvfs
->z_parent
!= zfsvfs
) {
886 struct componentname cn
;
890 zfs_exit(zfsvfs
, FTAG
);
891 ltype
= VOP_ISLOCKED(dvp
);
893 error
= zfsctl_root(zfsvfs
->z_parent
, LK_SHARED
,
896 cn
.cn_nameptr
= "snapshot";
897 cn
.cn_namelen
= strlen(cn
.cn_nameptr
);
898 cn
.cn_nameiop
= cnp
->cn_nameiop
;
899 cn
.cn_flags
= cnp
->cn_flags
& ~ISDOTDOT
;
900 cn
.cn_lkflags
= cnp
->cn_lkflags
;
901 error
= VOP_LOOKUP(zfsctl_vp
, vpp
, &cn
);
904 vn_lock(dvp
, ltype
| LK_RETRY
);
908 if (zfs_has_ctldir(zdp
) && strcmp(nm
, ZFS_CTLDIR_NAME
) == 0) {
909 zfs_exit(zfsvfs
, FTAG
);
910 if ((cnp
->cn_flags
& ISLASTCN
) != 0 && nameiop
!= LOOKUP
)
911 return (SET_ERROR(ENOTSUP
));
912 error
= zfsctl_root(zfsvfs
, cnp
->cn_lkflags
, vpp
);
917 * The loop is retry the lookup if the parent-child relationship
918 * changes during the dot-dot locking complexities.
923 error
= zfs_dirlook(zdp
, nm
, &zp
);
927 zfs_exit(zfsvfs
, FTAG
);
931 error
= zfs_lookup_lock(dvp
, *vpp
, nm
, cnp
->cn_lkflags
);
934 * If we've got a locking error, then the vnode
935 * got reclaimed because of a force unmount.
936 * We never enter doomed vnodes into the name cache.
942 if ((cnp
->cn_flags
& ISDOTDOT
) == 0)
945 if ((error
= zfs_enter(zfsvfs
, FTAG
)) != 0) {
950 if (zdp
->z_sa_hdl
== NULL
) {
951 error
= SET_ERROR(EIO
);
953 error
= sa_lookup(zdp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
954 &parent
, sizeof (parent
));
957 zfs_exit(zfsvfs
, FTAG
);
961 if (zp
->z_id
== parent
) {
962 zfs_exit(zfsvfs
, FTAG
);
971 /* Translate errors and add SAVENAME when needed. */
972 if (cnp
->cn_flags
& ISLASTCN
) {
976 if (error
== ENOENT
) {
978 #if __FreeBSD_version < 1400068
979 cnp
->cn_flags
|= SAVENAME
;
985 #if __FreeBSD_version < 1400068
987 cnp
->cn_flags
|= SAVENAME
;
993 #if __FreeBSD_version > 1300124
994 if ((cnp
->cn_flags
& ISDOTDOT
) != 0) {
996 * FIXME: zfs_lookup_lock relocks vnodes and does nothing to
997 * handle races. In particular different callers may end up
998 * with different vnodes and will try to add conflicting
999 * entries to the namecache.
1001 * While finding different result may be acceptable in face
1002 * of concurrent modification, adding conflicting entries
1003 * trips over an assert in the namecache.
1005 * Ultimately let an entry through once everything settles.
1007 if (!vn_seqc_consistent(dvp
, dvp_seqc
)) {
1008 cnp
->cn_flags
&= ~MAKEENTRY
;
1013 /* Insert name into cache (as non-existent) if appropriate. */
1014 if (zfsvfs
->z_use_namecache
&& !zfsvfs
->z_replay
&&
1015 error
== ENOENT
&& (cnp
->cn_flags
& MAKEENTRY
) != 0)
1016 cache_enter(dvp
, NULL
, cnp
);
1018 /* Insert name into cache if appropriate. */
1019 if (zfsvfs
->z_use_namecache
&& !zfsvfs
->z_replay
&&
1020 error
== 0 && (cnp
->cn_flags
& MAKEENTRY
)) {
1021 if (!(cnp
->cn_flags
& ISLASTCN
) ||
1022 (nameiop
!= DELETE
&& nameiop
!= RENAME
)) {
1023 cache_enter(dvp
, *vpp
, cnp
);
1031 * Attempt to create a new entry in a directory. If the entry
1032 * already exists, truncate the file if permissible, else return
1033 * an error. Return the vp of the created or trunc'd file.
1035 * IN: dvp - vnode of directory to put new file entry in.
1036 * name - name of new file entry.
1037 * vap - attributes of new file.
1038 * excl - flag indicating exclusive or non-exclusive mode.
1039 * mode - mode to open file with.
1040 * cr - credentials of caller.
1041 * flag - large file flag [UNUSED].
1042 * ct - caller context
1043 * vsecp - ACL to be set
1044 * mnt_ns - Unused on FreeBSD
1046 * OUT: vpp - vnode of created or trunc'd entry.
1048 * RETURN: 0 on success, error code on failure.
1051 * dvp - ctime|mtime updated if new entry created
1052 * vp - ctime|mtime always, atime if new
1055 zfs_create(znode_t
*dzp
, const char *name
, vattr_t
*vap
, int excl
, int mode
,
1056 znode_t
**zpp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
, zidmap_t
*mnt_ns
)
1058 (void) excl
, (void) mode
, (void) flag
;
1060 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1065 uid_t uid
= crgetuid(cr
);
1066 gid_t gid
= crgetgid(cr
);
1067 uint64_t projid
= ZFS_DEFAULT_PROJID
;
1068 zfs_acl_ids_t acl_ids
;
1069 boolean_t fuid_dirtied
;
1071 #ifdef DEBUG_VFS_LOCKS
1072 vnode_t
*dvp
= ZTOV(dzp
);
1076 * If we have an ephemeral id, ACL, or XVATTR then
1077 * make sure file system is at proper version
1079 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1080 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
1081 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1082 return (SET_ERROR(EINVAL
));
1084 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1087 zilog
= zfsvfs
->z_log
;
1089 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
1090 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1091 zfs_exit(zfsvfs
, FTAG
);
1092 return (SET_ERROR(EILSEQ
));
1095 if (vap
->va_mask
& AT_XVATTR
) {
1096 if ((error
= secpolicy_xvattr(ZTOV(dzp
), (xvattr_t
*)vap
,
1097 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1098 zfs_exit(zfsvfs
, FTAG
);
1105 if ((vap
->va_mode
& S_ISVTX
) && secpolicy_vnode_stky_modify(cr
))
1106 vap
->va_mode
&= ~S_ISVTX
;
1108 error
= zfs_dirent_lookup(dzp
, name
, &zp
, ZNEW
);
1110 zfs_exit(zfsvfs
, FTAG
);
1113 ASSERT3P(zp
, ==, NULL
);
1116 * Create a new file object and update the directory
1119 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, mnt_ns
))) {
1124 * We only support the creation of regular files in
1125 * extended attribute directories.
1128 if ((dzp
->z_pflags
& ZFS_XATTR
) &&
1129 (vap
->va_type
!= VREG
)) {
1130 error
= SET_ERROR(EINVAL
);
1134 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
,
1135 cr
, vsecp
, &acl_ids
, NULL
)) != 0)
1138 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
1139 projid
= zfs_inherit_projid(dzp
);
1140 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
1141 zfs_acl_ids_free(&acl_ids
);
1142 error
= SET_ERROR(EDQUOT
);
1146 getnewvnode_reserve_();
1148 tx
= dmu_tx_create(os
);
1150 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1151 ZFS_SA_BASE_ATTR_SIZE
);
1153 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1155 zfs_fuid_txhold(zfsvfs
, tx
);
1156 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1157 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1158 if (!zfsvfs
->z_use_sa
&&
1159 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1160 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1161 0, acl_ids
.z_aclp
->z_acl_bytes
);
1163 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1165 zfs_acl_ids_free(&acl_ids
);
1167 getnewvnode_drop_reserve();
1168 zfs_exit(zfsvfs
, FTAG
);
1171 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1173 zfs_fuid_sync(zfsvfs
, tx
);
1175 (void) zfs_link_create(dzp
, name
, zp
, tx
, ZNEW
);
1176 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1177 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1178 vsecp
, acl_ids
.z_fuidp
, vap
);
1179 zfs_acl_ids_free(&acl_ids
);
1182 getnewvnode_drop_reserve();
1190 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1191 zil_commit(zilog
, 0);
1193 zfs_exit(zfsvfs
, FTAG
);
1198 * Remove an entry from a directory.
1200 * IN: dvp - vnode of directory to remove entry from.
1201 * name - name of entry to remove.
1202 * cr - credentials of caller.
1203 * ct - caller context
1204 * flags - case flags
1206 * RETURN: 0 on success, error code on failure.
1210 * vp - ctime (if nlink > 0)
1213 zfs_remove_(vnode_t
*dvp
, vnode_t
*vp
, const char *name
, cred_t
*cr
)
1215 znode_t
*dzp
= VTOZ(dvp
);
1218 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1228 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1231 if ((error
= zfs_verify_zp(zp
)) != 0) {
1232 zfs_exit(zfsvfs
, FTAG
);
1235 zilog
= zfsvfs
->z_log
;
1240 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
, NULL
))) {
1245 * Need to use rmdir for removing directories.
1247 if (vp
->v_type
== VDIR
) {
1248 error
= SET_ERROR(EPERM
);
1252 vnevent_remove(vp
, dvp
, name
, ct
);
1256 /* are there any extended attributes? */
1257 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1258 &xattr_obj
, sizeof (xattr_obj
));
1259 if (error
== 0 && xattr_obj
) {
1260 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1265 * We may delete the znode now, or we may put it in the unlinked set;
1266 * it depends on whether we're the last link, and on whether there are
1267 * other holds on the vnode. So we dmu_tx_hold() the right things to
1268 * allow for either case.
1270 tx
= dmu_tx_create(zfsvfs
->z_os
);
1271 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1272 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1273 zfs_sa_upgrade_txholds(tx
, zp
);
1274 zfs_sa_upgrade_txholds(tx
, dzp
);
1277 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1278 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1281 /* charge as an update -- would be nice not to charge at all */
1282 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1285 * Mark this transaction as typically resulting in a net free of space
1287 dmu_tx_mark_netfree(tx
);
1289 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1292 zfs_exit(zfsvfs
, FTAG
);
1297 * Remove the directory entry.
1299 error
= zfs_link_destroy(dzp
, name
, zp
, tx
, ZEXISTS
, &unlinked
);
1307 zfs_unlinked_add(zp
, tx
);
1308 vp
->v_vflag
|= VV_NOSYNC
;
1310 /* XXX check changes to linux vnops */
1312 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
, unlinked
);
1320 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1321 zil_commit(zilog
, 0);
1324 zfs_exit(zfsvfs
, FTAG
);
1330 zfs_lookup_internal(znode_t
*dzp
, const char *name
, vnode_t
**vpp
,
1331 struct componentname
*cnp
, int nameiop
)
1333 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1336 cnp
->cn_nameptr
= __DECONST(char *, name
);
1337 cnp
->cn_namelen
= strlen(name
);
1338 cnp
->cn_nameiop
= nameiop
;
1339 cnp
->cn_flags
= ISLASTCN
;
1340 #if __FreeBSD_version < 1400068
1341 cnp
->cn_flags
|= SAVENAME
;
1343 cnp
->cn_lkflags
= LK_EXCLUSIVE
| LK_RETRY
;
1344 cnp
->cn_cred
= kcred
;
1345 #if __FreeBSD_version < 1400037
1346 cnp
->cn_thread
= curthread
;
1349 if (zfsvfs
->z_use_namecache
&& !zfsvfs
->z_replay
) {
1350 struct vop_lookup_args a
;
1352 a
.a_gen
.a_desc
= &vop_lookup_desc
;
1353 a
.a_dvp
= ZTOV(dzp
);
1356 error
= vfs_cache_lookup(&a
);
1358 error
= zfs_lookup(ZTOV(dzp
), name
, vpp
, cnp
, nameiop
, kcred
, 0,
1363 printf("got error %d on name %s on op %d\n", error
, name
,
1372 zfs_remove(znode_t
*dzp
, const char *name
, cred_t
*cr
, int flags
)
1376 struct componentname cn
;
1378 if ((error
= zfs_lookup_internal(dzp
, name
, &vp
, &cn
, DELETE
)))
1381 error
= zfs_remove_(ZTOV(dzp
), vp
, name
, cr
);
1386 * Create a new directory and insert it into dvp using the name
1387 * provided. Return a pointer to the inserted directory.
1389 * IN: dvp - vnode of directory to add subdir to.
1390 * dirname - name of new directory.
1391 * vap - attributes of new directory.
1392 * cr - credentials of caller.
1393 * ct - caller context
1394 * flags - case flags
1395 * vsecp - ACL to be set
1396 * mnt_ns - Unused on FreeBSD
1398 * OUT: vpp - vnode of created directory.
1400 * RETURN: 0 on success, error code on failure.
1403 * dvp - ctime|mtime updated
1404 * vp - ctime|mtime|atime updated
1407 zfs_mkdir(znode_t
*dzp
, const char *dirname
, vattr_t
*vap
, znode_t
**zpp
,
1408 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
, zidmap_t
*mnt_ns
)
1410 (void) flags
, (void) vsecp
;
1412 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1417 uid_t uid
= crgetuid(cr
);
1418 gid_t gid
= crgetgid(cr
);
1419 zfs_acl_ids_t acl_ids
;
1420 boolean_t fuid_dirtied
;
1422 ASSERT3U(vap
->va_type
, ==, VDIR
);
1425 * If we have an ephemeral id, ACL, or XVATTR then
1426 * make sure file system is at proper version
1428 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1429 ((vap
->va_mask
& AT_XVATTR
) ||
1430 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1431 return (SET_ERROR(EINVAL
));
1433 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1435 zilog
= zfsvfs
->z_log
;
1437 if (dzp
->z_pflags
& ZFS_XATTR
) {
1438 zfs_exit(zfsvfs
, FTAG
);
1439 return (SET_ERROR(EINVAL
));
1442 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1443 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1444 zfs_exit(zfsvfs
, FTAG
);
1445 return (SET_ERROR(EILSEQ
));
1448 if (vap
->va_mask
& AT_XVATTR
) {
1449 if ((error
= secpolicy_xvattr(ZTOV(dzp
), (xvattr_t
*)vap
,
1450 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1451 zfs_exit(zfsvfs
, FTAG
);
1456 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1457 NULL
, &acl_ids
, NULL
)) != 0) {
1458 zfs_exit(zfsvfs
, FTAG
);
1463 * First make sure the new directory doesn't exist.
1465 * Existence is checked first to make sure we don't return
1466 * EACCES instead of EEXIST which can cause some applications
1471 if ((error
= zfs_dirent_lookup(dzp
, dirname
, &zp
, ZNEW
))) {
1472 zfs_acl_ids_free(&acl_ids
);
1473 zfs_exit(zfsvfs
, FTAG
);
1476 ASSERT3P(zp
, ==, NULL
);
1478 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
,
1480 zfs_acl_ids_free(&acl_ids
);
1481 zfs_exit(zfsvfs
, FTAG
);
1485 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, zfs_inherit_projid(dzp
))) {
1486 zfs_acl_ids_free(&acl_ids
);
1487 zfs_exit(zfsvfs
, FTAG
);
1488 return (SET_ERROR(EDQUOT
));
1492 * Add a new entry to the directory.
1494 getnewvnode_reserve_();
1495 tx
= dmu_tx_create(zfsvfs
->z_os
);
1496 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1497 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1498 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1500 zfs_fuid_txhold(zfsvfs
, tx
);
1501 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1502 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1503 acl_ids
.z_aclp
->z_acl_bytes
);
1506 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1507 ZFS_SA_BASE_ATTR_SIZE
);
1509 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1511 zfs_acl_ids_free(&acl_ids
);
1513 getnewvnode_drop_reserve();
1514 zfs_exit(zfsvfs
, FTAG
);
1521 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1524 zfs_fuid_sync(zfsvfs
, tx
);
1527 * Now put new name in parent dir.
1529 (void) zfs_link_create(dzp
, dirname
, zp
, tx
, ZNEW
);
1533 txtype
= zfs_log_create_txtype(Z_DIR
, NULL
, vap
);
1534 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, NULL
,
1535 acl_ids
.z_fuidp
, vap
);
1537 zfs_acl_ids_free(&acl_ids
);
1541 getnewvnode_drop_reserve();
1543 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1544 zil_commit(zilog
, 0);
1546 zfs_exit(zfsvfs
, FTAG
);
1550 #if __FreeBSD_version < 1300124
1552 cache_vop_rmdir(struct vnode
*dvp
, struct vnode
*vp
)
1561 * Remove a directory subdir entry. If the current working
1562 * directory is the same as the subdir to be removed, the
1565 * IN: dvp - vnode of directory to remove from.
1566 * name - name of directory to be removed.
1567 * cwd - vnode of current working directory.
1568 * cr - credentials of caller.
1569 * ct - caller context
1570 * flags - case flags
1572 * RETURN: 0 on success, error code on failure.
1575 * dvp - ctime|mtime updated
1578 zfs_rmdir_(vnode_t
*dvp
, vnode_t
*vp
, const char *name
, cred_t
*cr
)
1580 znode_t
*dzp
= VTOZ(dvp
);
1581 znode_t
*zp
= VTOZ(vp
);
1582 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1587 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1589 if ((error
= zfs_verify_zp(zp
)) != 0) {
1590 zfs_exit(zfsvfs
, FTAG
);
1593 zilog
= zfsvfs
->z_log
;
1596 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
, NULL
))) {
1600 if (vp
->v_type
!= VDIR
) {
1601 error
= SET_ERROR(ENOTDIR
);
1605 vnevent_rmdir(vp
, dvp
, name
, ct
);
1607 tx
= dmu_tx_create(zfsvfs
->z_os
);
1608 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1609 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1610 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1611 zfs_sa_upgrade_txholds(tx
, zp
);
1612 zfs_sa_upgrade_txholds(tx
, dzp
);
1613 dmu_tx_mark_netfree(tx
);
1614 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1617 zfs_exit(zfsvfs
, FTAG
);
1621 error
= zfs_link_destroy(dzp
, name
, zp
, tx
, ZEXISTS
, NULL
);
1624 uint64_t txtype
= TX_RMDIR
;
1625 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
,
1626 ZFS_NO_OBJECT
, B_FALSE
);
1631 if (zfsvfs
->z_use_namecache
)
1632 cache_vop_rmdir(dvp
, vp
);
1634 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1635 zil_commit(zilog
, 0);
1637 zfs_exit(zfsvfs
, FTAG
);
1642 zfs_rmdir(znode_t
*dzp
, const char *name
, znode_t
*cwd
, cred_t
*cr
, int flags
)
1644 struct componentname cn
;
1648 if ((error
= zfs_lookup_internal(dzp
, name
, &vp
, &cn
, DELETE
)))
1651 error
= zfs_rmdir_(ZTOV(dzp
), vp
, name
, cr
);
1657 * Read as many directory entries as will fit into the provided
1658 * buffer from the given directory cursor position (specified in
1659 * the uio structure).
1661 * IN: vp - vnode of directory to read.
1662 * uio - structure supplying read location, range info,
1663 * and return buffer.
1664 * cr - credentials of caller.
1665 * ct - caller context
1667 * OUT: uio - updated offset and range, buffer filled.
1668 * eofp - set to true if end-of-file detected.
1669 * ncookies- number of entries in cookies
1670 * cookies - offsets to directory entries
1672 * RETURN: 0 on success, error code on failure.
1675 * vp - atime updated
1677 * Note that the low 4 bits of the cookie returned by zap is always zero.
1678 * This allows us to use the low range for "special" directory entries:
1679 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1680 * we use the offset 2 for the '.zfs' directory.
1683 zfs_readdir(vnode_t
*vp
, zfs_uio_t
*uio
, cred_t
*cr
, int *eofp
,
1684 int *ncookies
, cookie_t
**cookies
)
1686 znode_t
*zp
= VTOZ(vp
);
1689 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1694 zap_attribute_t zap
;
1695 uint_t bytes_wanted
;
1696 uint64_t offset
; /* must be unsigned; checks for < 1 */
1704 cookie_t
*cooks
= NULL
;
1706 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1709 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
1710 &parent
, sizeof (parent
))) != 0) {
1711 zfs_exit(zfsvfs
, FTAG
);
1716 * If we are not given an eof variable,
1723 * Check for valid iov_len.
1725 if (GET_UIO_STRUCT(uio
)->uio_iov
->iov_len
<= 0) {
1726 zfs_exit(zfsvfs
, FTAG
);
1727 return (SET_ERROR(EINVAL
));
1731 * Quit if directory has been removed (posix)
1733 if ((*eofp
= zp
->z_unlinked
) != 0) {
1734 zfs_exit(zfsvfs
, FTAG
);
1740 offset
= zfs_uio_offset(uio
);
1741 prefetch
= zp
->z_zn_prefetch
;
1744 * Initialize the iterator cursor.
1748 * Start iteration from the beginning of the directory.
1750 zap_cursor_init(&zc
, os
, zp
->z_id
);
1753 * The offset is a serialized cursor.
1755 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
1759 * Get space to change directory entries into fs independent format.
1761 iovp
= GET_UIO_STRUCT(uio
)->uio_iov
;
1762 bytes_wanted
= iovp
->iov_len
;
1763 if (zfs_uio_segflg(uio
) != UIO_SYSSPACE
|| zfs_uio_iovcnt(uio
) != 1) {
1764 bufsize
= bytes_wanted
;
1765 outbuf
= kmem_alloc(bufsize
, KM_SLEEP
);
1766 odp
= (struct dirent64
*)outbuf
;
1768 bufsize
= bytes_wanted
;
1770 odp
= (struct dirent64
*)iovp
->iov_base
;
1773 if (ncookies
!= NULL
) {
1775 * Minimum entry size is dirent size and 1 byte for a file name.
1777 ncooks
= zfs_uio_resid(uio
) / (sizeof (struct dirent
) -
1778 sizeof (((struct dirent
*)NULL
)->d_name
) + 1);
1779 cooks
= malloc(ncooks
* sizeof (*cooks
), M_TEMP
, M_WAITOK
);
1785 * Transform to file-system independent format
1788 while (outcount
< bytes_wanted
) {
1791 off64_t
*next
= NULL
;
1794 * Special case `.', `..', and `.zfs'.
1797 (void) strcpy(zap
.za_name
, ".");
1798 zap
.za_normalization_conflict
= 0;
1801 } else if (offset
== 1) {
1802 (void) strcpy(zap
.za_name
, "..");
1803 zap
.za_normalization_conflict
= 0;
1806 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
1807 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
1808 zap
.za_normalization_conflict
= 0;
1809 objnum
= ZFSCTL_INO_ROOT
;
1815 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
1816 if ((*eofp
= (error
== ENOENT
)) != 0)
1822 if (zap
.za_integer_length
!= 8 ||
1823 zap
.za_num_integers
!= 1) {
1824 cmn_err(CE_WARN
, "zap_readdir: bad directory "
1825 "entry, obj = %lld, offset = %lld\n",
1826 (u_longlong_t
)zp
->z_id
,
1827 (u_longlong_t
)offset
);
1828 error
= SET_ERROR(ENXIO
);
1832 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
1834 * MacOS X can extract the object type here such as:
1835 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1837 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
1840 reclen
= DIRENT64_RECLEN(strlen(zap
.za_name
));
1843 * Will this entry fit in the buffer?
1845 if (outcount
+ reclen
> bufsize
) {
1847 * Did we manage to fit anything in the buffer?
1850 error
= SET_ERROR(EINVAL
);
1858 odp
->d_ino
= objnum
;
1859 odp
->d_reclen
= reclen
;
1860 odp
->d_namlen
= strlen(zap
.za_name
);
1861 /* NOTE: d_off is the offset for the *next* entry. */
1863 strlcpy(odp
->d_name
, zap
.za_name
, odp
->d_namlen
+ 1);
1865 dirent_terminate(odp
);
1866 odp
= (dirent64_t
*)((intptr_t)odp
+ reclen
);
1870 ASSERT3S(outcount
, <=, bufsize
);
1873 dmu_prefetch_dnode(os
, objnum
, ZIO_PRIORITY_SYNC_READ
);
1876 * Move to the next entry, fill in the previous offset.
1878 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
1879 zap_cursor_advance(&zc
);
1880 offset
= zap_cursor_serialize(&zc
);
1885 /* Fill the offset right after advancing the cursor. */
1888 if (cooks
!= NULL
) {
1891 KASSERT(ncooks
>= 0, ("ncookies=%d", ncooks
));
1894 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
1896 /* Subtract unused cookies */
1897 if (ncookies
!= NULL
)
1898 *ncookies
-= ncooks
;
1900 if (zfs_uio_segflg(uio
) == UIO_SYSSPACE
&& zfs_uio_iovcnt(uio
) == 1) {
1901 iovp
->iov_base
+= outcount
;
1902 iovp
->iov_len
-= outcount
;
1903 zfs_uio_resid(uio
) -= outcount
;
1905 zfs_uiomove(outbuf
, (long)outcount
, UIO_READ
, uio
))) {
1907 * Reset the pointer.
1909 offset
= zfs_uio_offset(uio
);
1913 zap_cursor_fini(&zc
);
1914 if (zfs_uio_segflg(uio
) != UIO_SYSSPACE
|| zfs_uio_iovcnt(uio
) != 1)
1915 kmem_free(outbuf
, bufsize
);
1917 if (error
== ENOENT
)
1920 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
1922 zfs_uio_setoffset(uio
, offset
);
1923 zfs_exit(zfsvfs
, FTAG
);
1924 if (error
!= 0 && cookies
!= NULL
) {
1925 free(*cookies
, M_TEMP
);
1933 * Get the requested file attributes and place them in the provided
1936 * IN: vp - vnode of file.
1937 * vap - va_mask identifies requested attributes.
1938 * If AT_XVATTR set, then optional attrs are requested
1939 * flags - ATTR_NOACLCHECK (CIFS server context)
1940 * cr - credentials of caller.
1942 * OUT: vap - attribute values.
1944 * RETURN: 0 (always succeeds).
1947 zfs_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
)
1949 znode_t
*zp
= VTOZ(vp
);
1950 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1953 u_longlong_t nblocks
;
1954 uint64_t mtime
[2], ctime
[2], crtime
[2], rdev
;
1955 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
1956 xoptattr_t
*xoap
= NULL
;
1957 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
1958 sa_bulk_attr_t bulk
[4];
1961 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1964 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
1966 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
1967 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
1968 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CRTIME(zfsvfs
), NULL
, &crtime
, 16);
1969 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1970 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_RDEV(zfsvfs
), NULL
,
1973 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
1974 zfs_exit(zfsvfs
, FTAG
);
1979 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1980 * Also, if we are the owner don't bother, since owner should
1981 * always be allowed to read basic attributes of file.
1983 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
1984 (vap
->va_uid
!= crgetuid(cr
))) {
1985 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
1986 skipaclchk
, cr
, NULL
))) {
1987 zfs_exit(zfsvfs
, FTAG
);
1993 * Return all attributes. It's cheaper to provide the answer
1994 * than to determine whether we were asked the question.
1997 vap
->va_type
= IFTOVT(zp
->z_mode
);
1998 vap
->va_mode
= zp
->z_mode
& ~S_IFMT
;
2000 vap
->va_nodeid
= zp
->z_id
;
2001 vap
->va_nlink
= zp
->z_links
;
2002 if ((vp
->v_flag
& VROOT
) && zfs_show_ctldir(zp
) &&
2003 zp
->z_links
< ZFS_LINK_MAX
)
2005 vap
->va_size
= zp
->z_size
;
2006 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
2007 vap
->va_rdev
= zfs_cmpldev(rdev
);
2008 vap
->va_gen
= zp
->z_gen
;
2009 vap
->va_flags
= 0; /* FreeBSD: Reset chflags(2) flags. */
2010 vap
->va_filerev
= zp
->z_seq
;
2013 * Add in any requested optional attributes and the create time.
2014 * Also set the corresponding bits in the returned attribute bitmap.
2016 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2017 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2019 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2020 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2023 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2024 xoap
->xoa_readonly
=
2025 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2026 XVA_SET_RTN(xvap
, XAT_READONLY
);
2029 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2031 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2032 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2035 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2037 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2038 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2041 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2042 xoap
->xoa_nounlink
=
2043 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2044 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2047 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2048 xoap
->xoa_immutable
=
2049 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2050 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2053 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2054 xoap
->xoa_appendonly
=
2055 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2056 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2059 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2061 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2062 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2065 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2067 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2068 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2071 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2072 xoap
->xoa_av_quarantined
=
2073 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2074 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2077 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2078 xoap
->xoa_av_modified
=
2079 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2080 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2083 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2084 vp
->v_type
== VREG
) {
2085 zfs_sa_get_scanstamp(zp
, xvap
);
2088 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2089 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2090 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2092 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2093 xoap
->xoa_generation
= zp
->z_gen
;
2094 XVA_SET_RTN(xvap
, XAT_GEN
);
2097 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2099 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2100 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2103 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2105 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2106 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2109 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2110 xoap
->xoa_projinherit
=
2111 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0);
2112 XVA_SET_RTN(xvap
, XAT_PROJINHERIT
);
2115 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2116 xoap
->xoa_projid
= zp
->z_projid
;
2117 XVA_SET_RTN(xvap
, XAT_PROJID
);
2121 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2122 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2123 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2124 ZFS_TIME_DECODE(&vap
->va_birthtime
, crtime
);
2127 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2128 vap
->va_blksize
= blksize
;
2129 vap
->va_bytes
= nblocks
<< 9; /* nblocks * 512 */
2131 if (zp
->z_blksz
== 0) {
2133 * Block size hasn't been set; suggest maximal I/O transfers.
2135 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2138 zfs_exit(zfsvfs
, FTAG
);
2143 * Set the file attributes to the values contained in the
2146 * IN: zp - znode of file to be modified.
2147 * vap - new attribute values.
2148 * If AT_XVATTR set, then optional attrs are being set
2149 * flags - ATTR_UTIME set if non-default time values provided.
2150 * - ATTR_NOACLCHECK (CIFS context only).
2151 * cr - credentials of caller.
2152 * mnt_ns - Unused on FreeBSD
2154 * RETURN: 0 on success, error code on failure.
2157 * vp - ctime updated, mtime updated if size changed.
2160 zfs_setattr(znode_t
*zp
, vattr_t
*vap
, int flags
, cred_t
*cr
, zidmap_t
*mnt_ns
)
2162 vnode_t
*vp
= ZTOV(zp
);
2163 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2169 uint_t mask
= vap
->va_mask
;
2170 uint_t saved_mask
= 0;
2171 uint64_t saved_mode
;
2174 uint64_t new_uid
, new_gid
;
2176 uint64_t mtime
[2], ctime
[2];
2177 uint64_t projid
= ZFS_INVALID_PROJID
;
2179 int need_policy
= FALSE
;
2181 zfs_fuid_info_t
*fuidp
= NULL
;
2182 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2185 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2186 boolean_t fuid_dirtied
= B_FALSE
;
2187 sa_bulk_attr_t bulk
[7], xattr_bulk
[7];
2188 int count
= 0, xattr_count
= 0;
2193 if (mask
& AT_NOSET
)
2194 return (SET_ERROR(EINVAL
));
2196 if ((err
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
2200 zilog
= zfsvfs
->z_log
;
2203 * Make sure that if we have ephemeral uid/gid or xvattr specified
2204 * that file system is at proper version level
2207 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2208 (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2209 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2210 (mask
& AT_XVATTR
))) {
2211 zfs_exit(zfsvfs
, FTAG
);
2212 return (SET_ERROR(EINVAL
));
2215 if (mask
& AT_SIZE
&& vp
->v_type
== VDIR
) {
2216 zfs_exit(zfsvfs
, FTAG
);
2217 return (SET_ERROR(EISDIR
));
2220 if (mask
& AT_SIZE
&& vp
->v_type
!= VREG
&& vp
->v_type
!= VFIFO
) {
2221 zfs_exit(zfsvfs
, FTAG
);
2222 return (SET_ERROR(EINVAL
));
2226 * If this is an xvattr_t, then get a pointer to the structure of
2227 * optional attributes. If this is NULL, then we have a vattr_t.
2229 xoap
= xva_getxoptattr(xvap
);
2231 xva_init(&tmpxvattr
);
2234 * Immutable files can only alter immutable bit and atime
2236 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2237 ((mask
& (AT_SIZE
|AT_UID
|AT_GID
|AT_MTIME
|AT_MODE
)) ||
2238 ((mask
& AT_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2239 zfs_exit(zfsvfs
, FTAG
);
2240 return (SET_ERROR(EPERM
));
2244 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2248 * Verify timestamps doesn't overflow 32 bits.
2249 * ZFS can handle large timestamps, but 32bit syscalls can't
2250 * handle times greater than 2039. This check should be removed
2251 * once large timestamps are fully supported.
2253 if (mask
& (AT_ATIME
| AT_MTIME
)) {
2254 if (((mask
& AT_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2255 ((mask
& AT_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2256 zfs_exit(zfsvfs
, FTAG
);
2257 return (SET_ERROR(EOVERFLOW
));
2260 if (xoap
!= NULL
&& (mask
& AT_XVATTR
)) {
2261 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) &&
2262 TIMESPEC_OVERFLOW(&vap
->va_birthtime
)) {
2263 zfs_exit(zfsvfs
, FTAG
);
2264 return (SET_ERROR(EOVERFLOW
));
2267 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2268 if (!dmu_objset_projectquota_enabled(os
) ||
2269 (!S_ISREG(zp
->z_mode
) && !S_ISDIR(zp
->z_mode
))) {
2270 zfs_exit(zfsvfs
, FTAG
);
2271 return (SET_ERROR(EOPNOTSUPP
));
2274 projid
= xoap
->xoa_projid
;
2275 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
2276 zfs_exit(zfsvfs
, FTAG
);
2277 return (SET_ERROR(EINVAL
));
2280 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
2281 projid
= ZFS_INVALID_PROJID
;
2286 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
2287 (xoap
->xoa_projinherit
!=
2288 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) &&
2289 (!dmu_objset_projectquota_enabled(os
) ||
2290 (!S_ISREG(zp
->z_mode
) && !S_ISDIR(zp
->z_mode
)))) {
2291 zfs_exit(zfsvfs
, FTAG
);
2292 return (SET_ERROR(EOPNOTSUPP
));
2299 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
2300 zfs_exit(zfsvfs
, FTAG
);
2301 return (SET_ERROR(EROFS
));
2305 * First validate permissions
2308 if (mask
& AT_SIZE
) {
2310 * XXX - Note, we are not providing any open
2311 * mode flags here (like FNDELAY), so we may
2312 * block if there are locks present... this
2313 * should be addressed in openat().
2315 /* XXX - would it be OK to generate a log record here? */
2316 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2318 zfs_exit(zfsvfs
, FTAG
);
2323 if (mask
& (AT_ATIME
|AT_MTIME
) ||
2324 ((mask
& AT_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2325 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2326 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2327 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2328 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2329 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2330 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2331 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2332 skipaclchk
, cr
, mnt_ns
);
2335 if (mask
& (AT_UID
|AT_GID
)) {
2336 int idmask
= (mask
& (AT_UID
|AT_GID
));
2341 * NOTE: even if a new mode is being set,
2342 * we may clear S_ISUID/S_ISGID bits.
2345 if (!(mask
& AT_MODE
))
2346 vap
->va_mode
= zp
->z_mode
;
2349 * Take ownership or chgrp to group we are a member of
2352 take_owner
= (mask
& AT_UID
) && (vap
->va_uid
== crgetuid(cr
));
2353 take_group
= (mask
& AT_GID
) &&
2354 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2357 * If both AT_UID and AT_GID are set then take_owner and
2358 * take_group must both be set in order to allow taking
2361 * Otherwise, send the check through secpolicy_vnode_setattr()
2365 if (((idmask
== (AT_UID
|AT_GID
)) && take_owner
&& take_group
) ||
2366 ((idmask
== AT_UID
) && take_owner
) ||
2367 ((idmask
== AT_GID
) && take_group
)) {
2368 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2369 skipaclchk
, cr
, mnt_ns
) == 0) {
2371 * Remove setuid/setgid for non-privileged users
2373 secpolicy_setid_clear(vap
, vp
, cr
);
2374 trim_mask
= (mask
& (AT_UID
|AT_GID
));
2383 oldva
.va_mode
= zp
->z_mode
;
2384 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2385 if (mask
& AT_XVATTR
) {
2387 * Update xvattr mask to include only those attributes
2388 * that are actually changing.
2390 * the bits will be restored prior to actually setting
2391 * the attributes so the caller thinks they were set.
2393 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2394 if (xoap
->xoa_appendonly
!=
2395 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2398 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2399 XVA_SET_REQ(&tmpxvattr
, XAT_APPENDONLY
);
2403 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2404 if (xoap
->xoa_projinherit
!=
2405 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
2408 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
2409 XVA_SET_REQ(&tmpxvattr
, XAT_PROJINHERIT
);
2413 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2414 if (xoap
->xoa_nounlink
!=
2415 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2418 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2419 XVA_SET_REQ(&tmpxvattr
, XAT_NOUNLINK
);
2423 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2424 if (xoap
->xoa_immutable
!=
2425 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2428 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2429 XVA_SET_REQ(&tmpxvattr
, XAT_IMMUTABLE
);
2433 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2434 if (xoap
->xoa_nodump
!=
2435 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2438 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2439 XVA_SET_REQ(&tmpxvattr
, XAT_NODUMP
);
2443 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2444 if (xoap
->xoa_av_modified
!=
2445 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2448 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2449 XVA_SET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
);
2453 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2454 if ((vp
->v_type
!= VREG
&&
2455 xoap
->xoa_av_quarantined
) ||
2456 xoap
->xoa_av_quarantined
!=
2457 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2460 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2461 XVA_SET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
);
2465 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2466 zfs_exit(zfsvfs
, FTAG
);
2467 return (SET_ERROR(EPERM
));
2470 if (need_policy
== FALSE
&&
2471 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2472 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2477 if (mask
& AT_MODE
) {
2478 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
,
2480 err
= secpolicy_setid_setsticky_clear(vp
, vap
,
2483 zfs_exit(zfsvfs
, FTAG
);
2486 trim_mask
|= AT_MODE
;
2494 * If trim_mask is set then take ownership
2495 * has been granted or write_acl is present and user
2496 * has the ability to modify mode. In that case remove
2497 * UID|GID and or MODE from mask so that
2498 * secpolicy_vnode_setattr() doesn't revoke it.
2502 saved_mask
= vap
->va_mask
;
2503 vap
->va_mask
&= ~trim_mask
;
2504 if (trim_mask
& AT_MODE
) {
2506 * Save the mode, as secpolicy_vnode_setattr()
2507 * will overwrite it with ova.va_mode.
2509 saved_mode
= vap
->va_mode
;
2512 err
= secpolicy_vnode_setattr(cr
, vp
, vap
, &oldva
, flags
,
2513 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2515 zfs_exit(zfsvfs
, FTAG
);
2520 vap
->va_mask
|= saved_mask
;
2521 if (trim_mask
& AT_MODE
) {
2523 * Recover the mode after
2524 * secpolicy_vnode_setattr().
2526 vap
->va_mode
= saved_mode
;
2532 * secpolicy_vnode_setattr, or take ownership may have
2535 mask
= vap
->va_mask
;
2537 if ((mask
& (AT_UID
| AT_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
2538 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
2539 &xattr_obj
, sizeof (xattr_obj
));
2541 if (err
== 0 && xattr_obj
) {
2542 err
= zfs_zget(zp
->z_zfsvfs
, xattr_obj
, &attrzp
);
2544 err
= vn_lock(ZTOV(attrzp
), LK_EXCLUSIVE
);
2546 vrele(ZTOV(attrzp
));
2551 if (mask
& AT_UID
) {
2552 new_uid
= zfs_fuid_create(zfsvfs
,
2553 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2554 if (new_uid
!= zp
->z_uid
&&
2555 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
2559 err
= SET_ERROR(EDQUOT
);
2564 if (mask
& AT_GID
) {
2565 new_gid
= zfs_fuid_create(zfsvfs
, (uint64_t)vap
->va_gid
,
2566 cr
, ZFS_GROUP
, &fuidp
);
2567 if (new_gid
!= zp
->z_gid
&&
2568 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
2572 err
= SET_ERROR(EDQUOT
);
2577 if (projid
!= ZFS_INVALID_PROJID
&&
2578 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
2581 err
= SET_ERROR(EDQUOT
);
2585 tx
= dmu_tx_create(os
);
2587 if (mask
& AT_MODE
) {
2588 uint64_t pmode
= zp
->z_mode
;
2590 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2592 if (zp
->z_zfsvfs
->z_acl_mode
== ZFS_ACL_RESTRICTED
&&
2593 !(zp
->z_pflags
& ZFS_ACL_TRIVIAL
)) {
2594 err
= SET_ERROR(EPERM
);
2598 if ((err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
)))
2601 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2603 * Are we upgrading ACL from old V0 format
2606 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
2607 zfs_znode_acl_version(zp
) ==
2608 ZFS_ACL_VERSION_INITIAL
) {
2609 dmu_tx_hold_free(tx
, acl_obj
, 0,
2611 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2612 0, aclp
->z_acl_bytes
);
2614 dmu_tx_hold_write(tx
, acl_obj
, 0,
2617 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2618 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2619 0, aclp
->z_acl_bytes
);
2621 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2623 if (((mask
& AT_XVATTR
) &&
2624 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
2625 (projid
!= ZFS_INVALID_PROJID
&&
2626 !(zp
->z_pflags
& ZFS_PROJID
)))
2627 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2629 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2633 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2636 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2638 zfs_fuid_txhold(zfsvfs
, tx
);
2640 zfs_sa_upgrade_txholds(tx
, zp
);
2642 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2648 * Set each attribute requested.
2649 * We group settings according to the locks they need to acquire.
2651 * Note: you cannot set ctime directly, although it will be
2652 * updated as a side-effect of calling this function.
2655 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
2657 * For the existed object that is upgraded from old system,
2658 * its on-disk layout has no slot for the project ID attribute.
2659 * But quota accounting logic needs to access related slots by
2660 * offset directly. So we need to adjust old objects' layout
2661 * to make the project ID to some unified and fixed offset.
2664 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
2666 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
2668 if (unlikely(err
== EEXIST
))
2673 projid
= ZFS_INVALID_PROJID
;
2676 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2677 mutex_enter(&zp
->z_acl_lock
);
2679 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
2680 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2683 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2684 mutex_enter(&attrzp
->z_acl_lock
);
2685 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2686 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
2687 sizeof (attrzp
->z_pflags
));
2688 if (projid
!= ZFS_INVALID_PROJID
) {
2689 attrzp
->z_projid
= projid
;
2690 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2691 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
2692 sizeof (attrzp
->z_projid
));
2696 if (mask
& (AT_UID
|AT_GID
)) {
2698 if (mask
& AT_UID
) {
2699 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2700 &new_uid
, sizeof (new_uid
));
2701 zp
->z_uid
= new_uid
;
2703 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2704 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
2706 attrzp
->z_uid
= new_uid
;
2710 if (mask
& AT_GID
) {
2711 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
2712 NULL
, &new_gid
, sizeof (new_gid
));
2713 zp
->z_gid
= new_gid
;
2715 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2716 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
2718 attrzp
->z_gid
= new_gid
;
2721 if (!(mask
& AT_MODE
)) {
2722 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
2723 NULL
, &new_mode
, sizeof (new_mode
));
2724 new_mode
= zp
->z_mode
;
2726 err
= zfs_acl_chown_setattr(zp
);
2729 vn_seqc_write_begin(ZTOV(attrzp
));
2730 err
= zfs_acl_chown_setattr(attrzp
);
2731 vn_seqc_write_end(ZTOV(attrzp
));
2736 if (mask
& AT_MODE
) {
2737 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
2738 &new_mode
, sizeof (new_mode
));
2739 zp
->z_mode
= new_mode
;
2740 ASSERT3P(aclp
, !=, NULL
);
2741 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2743 if (zp
->z_acl_cached
)
2744 zfs_acl_free(zp
->z_acl_cached
);
2745 zp
->z_acl_cached
= aclp
;
2750 if (mask
& AT_ATIME
) {
2751 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
2752 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
2753 &zp
->z_atime
, sizeof (zp
->z_atime
));
2756 if (mask
& AT_MTIME
) {
2757 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2758 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
2759 mtime
, sizeof (mtime
));
2762 if (projid
!= ZFS_INVALID_PROJID
) {
2763 zp
->z_projid
= projid
;
2764 SA_ADD_BULK_ATTR(bulk
, count
,
2765 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
2766 sizeof (zp
->z_projid
));
2769 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2770 if (mask
& AT_SIZE
&& !(mask
& AT_MTIME
)) {
2771 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
),
2772 NULL
, mtime
, sizeof (mtime
));
2773 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2774 &ctime
, sizeof (ctime
));
2775 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
2776 } else if (mask
!= 0) {
2777 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2778 &ctime
, sizeof (ctime
));
2779 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
);
2781 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2782 SA_ZPL_CTIME(zfsvfs
), NULL
,
2783 &ctime
, sizeof (ctime
));
2784 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2790 * Do this after setting timestamps to prevent timestamp
2791 * update from toggling bit
2794 if (xoap
&& (mask
& AT_XVATTR
)) {
2796 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
))
2797 xoap
->xoa_createtime
= vap
->va_birthtime
;
2799 * restore trimmed off masks
2800 * so that return masks can be set for caller.
2803 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_APPENDONLY
)) {
2804 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2806 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NOUNLINK
)) {
2807 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2809 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_IMMUTABLE
)) {
2810 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2812 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NODUMP
)) {
2813 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2815 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
)) {
2816 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2818 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
)) {
2819 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2821 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_PROJINHERIT
)) {
2822 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
2825 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2826 ASSERT3S(vp
->v_type
, ==, VREG
);
2828 zfs_xvattr_set(zp
, xvap
, tx
);
2832 zfs_fuid_sync(zfsvfs
, tx
);
2835 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2837 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2838 mutex_exit(&zp
->z_acl_lock
);
2841 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2842 mutex_exit(&attrzp
->z_acl_lock
);
2845 if (err
== 0 && attrzp
) {
2846 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
2858 zfs_fuid_info_free(fuidp
);
2865 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2870 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
2871 zil_commit(zilog
, 0);
2873 zfs_exit(zfsvfs
, FTAG
);
2878 * Look up the directory entries corresponding to the source and target
2879 * directory/name pairs.
2882 zfs_rename_relock_lookup(znode_t
*sdzp
, const struct componentname
*scnp
,
2883 znode_t
**szpp
, znode_t
*tdzp
, const struct componentname
*tcnp
,
2891 * Before using sdzp and tdzp we must ensure that they are live.
2892 * As a porting legacy from illumos we have two things to worry
2893 * about. One is typical for FreeBSD and it is that the vnode is
2894 * not reclaimed (doomed). The other is that the znode is live.
2895 * The current code can invalidate the znode without acquiring the
2896 * corresponding vnode lock if the object represented by the znode
2897 * and vnode is no longer valid after a rollback or receive operation.
2898 * z_teardown_lock hidden behind zfs_enter and zfs_exit is the lock
2899 * that protects the znodes from the invalidation.
2901 zfsvfs
= sdzp
->z_zfsvfs
;
2902 ASSERT3P(zfsvfs
, ==, tdzp
->z_zfsvfs
);
2903 if ((error
= zfs_enter_verify_zp(zfsvfs
, sdzp
, FTAG
)) != 0)
2905 if ((error
= zfs_verify_zp(tdzp
)) != 0) {
2906 zfs_exit(zfsvfs
, FTAG
);
2911 * Re-resolve svp to be certain it still exists and fetch the
2914 error
= zfs_dirent_lookup(sdzp
, scnp
->cn_nameptr
, &szp
, ZEXISTS
);
2916 /* Source entry invalid or not there. */
2917 if ((scnp
->cn_flags
& ISDOTDOT
) != 0 ||
2918 (scnp
->cn_namelen
== 1 && scnp
->cn_nameptr
[0] == '.'))
2919 error
= SET_ERROR(EINVAL
);
2925 * Re-resolve tvp, if it disappeared we just carry on.
2927 error
= zfs_dirent_lookup(tdzp
, tcnp
->cn_nameptr
, &tzp
, 0);
2930 if ((tcnp
->cn_flags
& ISDOTDOT
) != 0)
2931 error
= SET_ERROR(EINVAL
);
2936 zfs_exit(zfsvfs
, FTAG
);
2941 * We acquire all but fdvp locks using non-blocking acquisitions. If we
2942 * fail to acquire any lock in the path we will drop all held locks,
2943 * acquire the new lock in a blocking fashion, and then release it and
2944 * restart the rename. This acquire/release step ensures that we do not
2945 * spin on a lock waiting for release. On error release all vnode locks
2946 * and decrement references the way tmpfs_rename() would do.
2949 zfs_rename_relock(struct vnode
*sdvp
, struct vnode
**svpp
,
2950 struct vnode
*tdvp
, struct vnode
**tvpp
,
2951 const struct componentname
*scnp
, const struct componentname
*tcnp
)
2953 struct vnode
*nvp
, *svp
, *tvp
;
2954 znode_t
*sdzp
, *tdzp
, *szp
, *tzp
;
2958 if (*tvpp
!= NULL
&& *tvpp
!= tdvp
)
2962 error
= vn_lock(sdvp
, LK_EXCLUSIVE
);
2965 error
= vn_lock(tdvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
2970 error
= vn_lock(tdvp
, LK_EXCLUSIVE
);
2979 error
= zfs_rename_relock_lookup(sdzp
, scnp
, &szp
, tdzp
, tcnp
, &tzp
);
2986 tvp
= tzp
!= NULL
? ZTOV(tzp
) : NULL
;
2989 * Now try acquire locks on svp and tvp.
2992 error
= vn_lock(nvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
2998 if (error
!= EBUSY
) {
3002 error
= vn_lock(nvp
, LK_EXCLUSIVE
);
3009 * Concurrent rename race.
3014 error
= SET_ERROR(EINVAL
);
3029 error
= vn_lock(nvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
3034 if (error
!= EBUSY
) {
3038 error
= vn_lock(nvp
, LK_EXCLUSIVE
);
3056 * Note that we must use VRELE_ASYNC in this function as it walks
3057 * up the directory tree and vrele may need to acquire an exclusive
3058 * lock if a last reference to a vnode is dropped.
3061 zfs_rename_check(znode_t
*szp
, znode_t
*sdzp
, znode_t
*tdzp
)
3068 zfsvfs
= tdzp
->z_zfsvfs
;
3070 return (SET_ERROR(EINVAL
));
3073 if (tdzp
->z_id
== zfsvfs
->z_root
)
3077 ASSERT(!zp
->z_unlinked
);
3078 if ((error
= sa_lookup(zp
->z_sa_hdl
,
3079 SA_ZPL_PARENT(zfsvfs
), &parent
, sizeof (parent
))) != 0)
3082 if (parent
== szp
->z_id
) {
3083 error
= SET_ERROR(EINVAL
);
3086 if (parent
== zfsvfs
->z_root
)
3088 if (parent
== sdzp
->z_id
)
3091 error
= zfs_zget(zfsvfs
, parent
, &zp1
);
3096 VN_RELE_ASYNC(ZTOV(zp
),
3097 dsl_pool_zrele_taskq(
3098 dmu_objset_pool(zfsvfs
->z_os
)));
3102 if (error
== ENOTDIR
)
3103 panic("checkpath: .. not a directory\n");
3105 VN_RELE_ASYNC(ZTOV(zp
),
3106 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs
->z_os
)));
3110 #if __FreeBSD_version < 1300124
3112 cache_vop_rename(struct vnode
*fdvp
, struct vnode
*fvp
, struct vnode
*tdvp
,
3113 struct vnode
*tvp
, struct componentname
*fcnp
, struct componentname
*tcnp
)
3119 cache_purge_negative(tdvp
);
3124 zfs_do_rename_impl(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3125 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3129 * Move an entry from the provided source directory to the target
3130 * directory. Change the entry name as indicated.
3132 * IN: sdvp - Source directory containing the "old entry".
3133 * scnp - Old entry name.
3134 * tdvp - Target directory to contain the "new entry".
3135 * tcnp - New entry name.
3136 * cr - credentials of caller.
3137 * INOUT: svpp - Source file
3138 * tvpp - Target file, may point to NULL initially
3140 * RETURN: 0 on success, error code on failure.
3143 * sdvp,tdvp - ctime|mtime updated
3146 zfs_do_rename(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3147 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3152 ASSERT_VOP_ELOCKED(tdvp
, __func__
);
3154 ASSERT_VOP_ELOCKED(*tvpp
, __func__
);
3156 /* Reject renames across filesystems. */
3157 if ((*svpp
)->v_mount
!= tdvp
->v_mount
||
3158 ((*tvpp
) != NULL
&& (*svpp
)->v_mount
!= (*tvpp
)->v_mount
)) {
3159 error
= SET_ERROR(EXDEV
);
3163 if (zfsctl_is_node(tdvp
)) {
3164 error
= SET_ERROR(EXDEV
);
3169 * Lock all four vnodes to ensure safety and semantics of renaming.
3171 error
= zfs_rename_relock(sdvp
, svpp
, tdvp
, tvpp
, scnp
, tcnp
);
3173 /* no vnodes are locked in the case of error here */
3177 error
= zfs_do_rename_impl(sdvp
, svpp
, scnp
, tdvp
, tvpp
, tcnp
, cr
);
3190 zfs_do_rename_impl(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3191 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3197 znode_t
*tdzp
, *sdzp
, *tzp
, *szp
;
3198 const char *snm
= scnp
->cn_nameptr
;
3199 const char *tnm
= tcnp
->cn_nameptr
;
3204 zfsvfs
= tdzp
->z_zfsvfs
;
3206 if ((error
= zfs_enter_verify_zp(zfsvfs
, tdzp
, FTAG
)) != 0)
3208 if ((error
= zfs_verify_zp(sdzp
)) != 0) {
3209 zfs_exit(zfsvfs
, FTAG
);
3212 zilog
= zfsvfs
->z_log
;
3214 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
3215 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3216 error
= SET_ERROR(EILSEQ
);
3220 /* If source and target are the same file, there is nothing to do. */
3221 if ((*svpp
) == (*tvpp
)) {
3226 if (((*svpp
)->v_type
== VDIR
&& (*svpp
)->v_mountedhere
!= NULL
) ||
3227 ((*tvpp
) != NULL
&& (*tvpp
)->v_type
== VDIR
&&
3228 (*tvpp
)->v_mountedhere
!= NULL
)) {
3229 error
= SET_ERROR(EXDEV
);
3234 if ((error
= zfs_verify_zp(szp
)) != 0) {
3235 zfs_exit(zfsvfs
, FTAG
);
3238 tzp
= *tvpp
== NULL
? NULL
: VTOZ(*tvpp
);
3240 if ((error
= zfs_verify_zp(tzp
)) != 0) {
3241 zfs_exit(zfsvfs
, FTAG
);
3247 * This is to prevent the creation of links into attribute space
3248 * by renaming a linked file into/outof an attribute directory.
3249 * See the comment in zfs_link() for why this is considered bad.
3251 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3252 error
= SET_ERROR(EINVAL
);
3257 * If we are using project inheritance, means if the directory has
3258 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3259 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3260 * such case, we only allow renames into our tree when the project
3263 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3264 tdzp
->z_projid
!= szp
->z_projid
) {
3265 error
= SET_ERROR(EXDEV
);
3270 * Must have write access at the source to remove the old entry
3271 * and write access at the target to create the new entry.
3272 * Note that if target and source are the same, this can be
3273 * done in a single check.
3275 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
, NULL
)))
3278 if ((*svpp
)->v_type
== VDIR
) {
3280 * Avoid ".", "..", and aliases of "." for obvious reasons.
3282 if ((scnp
->cn_namelen
== 1 && scnp
->cn_nameptr
[0] == '.') ||
3284 (scnp
->cn_flags
| tcnp
->cn_flags
) & ISDOTDOT
) {
3290 * Check to make sure rename is valid.
3291 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3293 if ((error
= zfs_rename_check(szp
, sdzp
, tdzp
)))
3298 * Does target exist?
3302 * Source and target must be the same type.
3304 if ((*svpp
)->v_type
== VDIR
) {
3305 if ((*tvpp
)->v_type
!= VDIR
) {
3306 error
= SET_ERROR(ENOTDIR
);
3314 if ((*tvpp
)->v_type
== VDIR
) {
3315 error
= SET_ERROR(EISDIR
);
3321 vn_seqc_write_begin(*svpp
);
3322 vn_seqc_write_begin(sdvp
);
3324 vn_seqc_write_begin(*tvpp
);
3326 vn_seqc_write_begin(tdvp
);
3328 vnevent_rename_src(*svpp
, sdvp
, scnp
->cn_nameptr
, ct
);
3330 vnevent_rename_dest(*tvpp
, tdvp
, tnm
, ct
);
3333 * notify the target directory if it is not the same
3334 * as source directory.
3337 vnevent_rename_dest_dir(tdvp
, ct
);
3340 tx
= dmu_tx_create(zfsvfs
->z_os
);
3341 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3342 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3343 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3344 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3346 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3347 zfs_sa_upgrade_txholds(tx
, tdzp
);
3350 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3351 zfs_sa_upgrade_txholds(tx
, tzp
);
3354 zfs_sa_upgrade_txholds(tx
, szp
);
3355 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3356 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3362 if (tzp
) /* Attempt to remove the existing target */
3363 error
= zfs_link_destroy(tdzp
, tnm
, tzp
, tx
, 0, NULL
);
3366 error
= zfs_link_create(tdzp
, tnm
, szp
, tx
, ZRENAMING
);
3368 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3370 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3371 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3374 error
= zfs_link_destroy(sdzp
, snm
, szp
, tx
, ZRENAMING
,
3377 zfs_log_rename(zilog
, tx
, TX_RENAME
, sdzp
,
3378 snm
, tdzp
, tnm
, szp
);
3381 * At this point, we have successfully created
3382 * the target name, but have failed to remove
3383 * the source name. Since the create was done
3384 * with the ZRENAMING flag, there are
3385 * complications; for one, the link count is
3386 * wrong. The easiest way to deal with this
3387 * is to remove the newly created target, and
3388 * return the original error. This must
3389 * succeed; fortunately, it is very unlikely to
3390 * fail, since we just created it.
3392 VERIFY0(zfs_link_destroy(tdzp
, tnm
, szp
, tx
,
3397 cache_vop_rename(sdvp
, *svpp
, tdvp
, *tvpp
, scnp
, tcnp
);
3404 vn_seqc_write_end(*svpp
);
3405 vn_seqc_write_end(sdvp
);
3407 vn_seqc_write_end(*tvpp
);
3409 vn_seqc_write_end(tdvp
);
3412 if (error
== 0 && zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3413 zil_commit(zilog
, 0);
3414 zfs_exit(zfsvfs
, FTAG
);
3420 zfs_rename(znode_t
*sdzp
, const char *sname
, znode_t
*tdzp
, const char *tname
,
3421 cred_t
*cr
, int flags
, uint64_t rflags
, vattr_t
*wo_vap
, zidmap_t
*mnt_ns
)
3423 struct componentname scn
, tcn
;
3424 vnode_t
*sdvp
, *tdvp
;
3429 if (rflags
!= 0 || wo_vap
!= NULL
)
3430 return (SET_ERROR(EINVAL
));
3434 error
= zfs_lookup_internal(sdzp
, sname
, &svp
, &scn
, DELETE
);
3435 if (sdzp
->z_zfsvfs
->z_replay
== B_FALSE
)
3441 vn_lock(tdvp
, LK_EXCLUSIVE
| LK_RETRY
);
3442 error
= zfs_lookup_internal(tdzp
, tname
, &tvp
, &tcn
, RENAME
);
3443 if (error
== EJUSTRETURN
)
3445 else if (error
!= 0) {
3450 error
= zfs_do_rename(sdvp
, &svp
, &scn
, tdvp
, &tvp
, &tcn
, cr
);
3461 * Insert the indicated symbolic reference entry into the directory.
3463 * IN: dvp - Directory to contain new symbolic link.
3464 * link - Name for new symlink entry.
3465 * vap - Attributes of new entry.
3466 * cr - credentials of caller.
3467 * ct - caller context
3468 * flags - case flags
3469 * mnt_ns - Unused on FreeBSD
3471 * RETURN: 0 on success, error code on failure.
3474 * dvp - ctime|mtime updated
3477 zfs_symlink(znode_t
*dzp
, const char *name
, vattr_t
*vap
,
3478 const char *link
, znode_t
**zpp
, cred_t
*cr
, int flags
, zidmap_t
*mnt_ns
)
3483 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3485 uint64_t len
= strlen(link
);
3487 zfs_acl_ids_t acl_ids
;
3488 boolean_t fuid_dirtied
;
3489 uint64_t txtype
= TX_SYMLINK
;
3491 ASSERT3S(vap
->va_type
, ==, VLNK
);
3493 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
3495 zilog
= zfsvfs
->z_log
;
3497 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3498 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3499 zfs_exit(zfsvfs
, FTAG
);
3500 return (SET_ERROR(EILSEQ
));
3503 if (len
> MAXPATHLEN
) {
3504 zfs_exit(zfsvfs
, FTAG
);
3505 return (SET_ERROR(ENAMETOOLONG
));
3508 if ((error
= zfs_acl_ids_create(dzp
, 0,
3509 vap
, cr
, NULL
, &acl_ids
, NULL
)) != 0) {
3510 zfs_exit(zfsvfs
, FTAG
);
3515 * Attempt to lock directory; fail if entry already exists.
3517 error
= zfs_dirent_lookup(dzp
, name
, &zp
, ZNEW
);
3519 zfs_acl_ids_free(&acl_ids
);
3520 zfs_exit(zfsvfs
, FTAG
);
3524 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, mnt_ns
))) {
3525 zfs_acl_ids_free(&acl_ids
);
3526 zfs_exit(zfsvfs
, FTAG
);
3530 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
,
3532 zfs_acl_ids_free(&acl_ids
);
3533 zfs_exit(zfsvfs
, FTAG
);
3534 return (SET_ERROR(EDQUOT
));
3537 getnewvnode_reserve_();
3538 tx
= dmu_tx_create(zfsvfs
->z_os
);
3539 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3540 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3541 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3542 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3543 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3544 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3545 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3546 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3547 acl_ids
.z_aclp
->z_acl_bytes
);
3550 zfs_fuid_txhold(zfsvfs
, tx
);
3551 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3553 zfs_acl_ids_free(&acl_ids
);
3555 getnewvnode_drop_reserve();
3556 zfs_exit(zfsvfs
, FTAG
);
3561 * Create a new object for the symlink.
3562 * for version 4 ZPL datasets the symlink will be an SA attribute
3564 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3567 zfs_fuid_sync(zfsvfs
, tx
);
3570 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
3571 __DECONST(void *, link
), len
, tx
);
3573 zfs_sa_symlink(zp
, __DECONST(char *, link
), len
, tx
);
3576 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
3577 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3579 * Insert the new object into the directory.
3581 (void) zfs_link_create(dzp
, name
, zp
, tx
, ZNEW
);
3583 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3586 zfs_acl_ids_free(&acl_ids
);
3590 getnewvnode_drop_reserve();
3592 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3593 zil_commit(zilog
, 0);
3595 zfs_exit(zfsvfs
, FTAG
);
3600 * Return, in the buffer contained in the provided uio structure,
3601 * the symbolic path referred to by vp.
3603 * IN: vp - vnode of symbolic link.
3604 * uio - structure to contain the link path.
3605 * cr - credentials of caller.
3606 * ct - caller context
3608 * OUT: uio - structure containing the link path.
3610 * RETURN: 0 on success, error code on failure.
3613 * vp - atime updated
3616 zfs_readlink(vnode_t
*vp
, zfs_uio_t
*uio
, cred_t
*cr
, caller_context_t
*ct
)
3618 (void) cr
, (void) ct
;
3619 znode_t
*zp
= VTOZ(vp
);
3620 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3623 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3627 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3628 SA_ZPL_SYMLINK(zfsvfs
), uio
);
3630 error
= zfs_sa_readlink(zp
, uio
);
3632 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3634 zfs_exit(zfsvfs
, FTAG
);
3639 * Insert a new entry into directory tdvp referencing svp.
3641 * IN: tdvp - Directory to contain new entry.
3642 * svp - vnode of new entry.
3643 * name - name of new entry.
3644 * cr - credentials of caller.
3646 * RETURN: 0 on success, error code on failure.
3649 * tdvp - ctime|mtime updated
3650 * svp - ctime updated
3653 zfs_link(znode_t
*tdzp
, znode_t
*szp
, const char *name
, cred_t
*cr
,
3658 zfsvfs_t
*zfsvfs
= tdzp
->z_zfsvfs
;
3665 ASSERT3S(ZTOV(tdzp
)->v_type
, ==, VDIR
);
3667 if ((error
= zfs_enter_verify_zp(zfsvfs
, tdzp
, FTAG
)) != 0)
3669 zilog
= zfsvfs
->z_log
;
3672 * POSIX dictates that we return EPERM here.
3673 * Better choices include ENOTSUP or EISDIR.
3675 if (ZTOV(szp
)->v_type
== VDIR
) {
3676 zfs_exit(zfsvfs
, FTAG
);
3677 return (SET_ERROR(EPERM
));
3680 if ((error
= zfs_verify_zp(szp
)) != 0) {
3681 zfs_exit(zfsvfs
, FTAG
);
3686 * If we are using project inheritance, means if the directory has
3687 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3688 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3689 * such case, we only allow hard link creation in our tree when the
3690 * project IDs are the same.
3692 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3693 tdzp
->z_projid
!= szp
->z_projid
) {
3694 zfs_exit(zfsvfs
, FTAG
);
3695 return (SET_ERROR(EXDEV
));
3698 if (szp
->z_pflags
& (ZFS_APPENDONLY
|
3699 ZFS_IMMUTABLE
| ZFS_READONLY
)) {
3700 zfs_exit(zfsvfs
, FTAG
);
3701 return (SET_ERROR(EPERM
));
3704 /* Prevent links to .zfs/shares files */
3706 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
3707 &parent
, sizeof (uint64_t))) != 0) {
3708 zfs_exit(zfsvfs
, FTAG
);
3711 if (parent
== zfsvfs
->z_shares_dir
) {
3712 zfs_exit(zfsvfs
, FTAG
);
3713 return (SET_ERROR(EPERM
));
3716 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3717 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3718 zfs_exit(zfsvfs
, FTAG
);
3719 return (SET_ERROR(EILSEQ
));
3723 * We do not support links between attributes and non-attributes
3724 * because of the potential security risk of creating links
3725 * into "normal" file space in order to circumvent restrictions
3726 * imposed in attribute space.
3728 if ((szp
->z_pflags
& ZFS_XATTR
) != (tdzp
->z_pflags
& ZFS_XATTR
)) {
3729 zfs_exit(zfsvfs
, FTAG
);
3730 return (SET_ERROR(EINVAL
));
3734 owner
= zfs_fuid_map_id(zfsvfs
, szp
->z_uid
, cr
, ZFS_OWNER
);
3735 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(ZTOV(szp
), cr
) != 0) {
3736 zfs_exit(zfsvfs
, FTAG
);
3737 return (SET_ERROR(EPERM
));
3740 if ((error
= zfs_zaccess(tdzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, NULL
))) {
3741 zfs_exit(zfsvfs
, FTAG
);
3746 * Attempt to lock directory; fail if entry already exists.
3748 error
= zfs_dirent_lookup(tdzp
, name
, &tzp
, ZNEW
);
3750 zfs_exit(zfsvfs
, FTAG
);
3754 tx
= dmu_tx_create(zfsvfs
->z_os
);
3755 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3756 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, name
);
3757 zfs_sa_upgrade_txholds(tx
, szp
);
3758 zfs_sa_upgrade_txholds(tx
, tdzp
);
3759 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3762 zfs_exit(zfsvfs
, FTAG
);
3766 error
= zfs_link_create(tdzp
, name
, szp
, tx
, 0);
3769 uint64_t txtype
= TX_LINK
;
3770 zfs_log_link(zilog
, tx
, txtype
, tdzp
, szp
, name
);
3776 vnevent_link(ZTOV(szp
), ct
);
3779 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3780 zil_commit(zilog
, 0);
3782 zfs_exit(zfsvfs
, FTAG
);
3787 * Free or allocate space in a file. Currently, this function only
3788 * supports the `F_FREESP' command. However, this command is somewhat
3789 * misnamed, as its functionality includes the ability to allocate as
3790 * well as free space.
3792 * IN: ip - inode of file to free data in.
3793 * cmd - action to take (only F_FREESP supported).
3794 * bfp - section of file to free/alloc.
3795 * flag - current file open mode flags.
3796 * offset - current file offset.
3797 * cr - credentials of caller.
3799 * RETURN: 0 on success, error code on failure.
3802 * ip - ctime|mtime updated
3805 zfs_space(znode_t
*zp
, int cmd
, flock64_t
*bfp
, int flag
,
3806 offset_t offset
, cred_t
*cr
)
3809 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
3813 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3816 if (cmd
!= F_FREESP
) {
3817 zfs_exit(zfsvfs
, FTAG
);
3818 return (SET_ERROR(EINVAL
));
3822 * Callers might not be able to detect properly that we are read-only,
3823 * so check it explicitly here.
3825 if (zfs_is_readonly(zfsvfs
)) {
3826 zfs_exit(zfsvfs
, FTAG
);
3827 return (SET_ERROR(EROFS
));
3830 if (bfp
->l_len
< 0) {
3831 zfs_exit(zfsvfs
, FTAG
);
3832 return (SET_ERROR(EINVAL
));
3836 * Permissions aren't checked on Solaris because on this OS
3837 * zfs_space() can only be called with an opened file handle.
3838 * On Linux we can get here through truncate_range() which
3839 * operates directly on inodes, so we need to check access rights.
3841 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
, NULL
))) {
3842 zfs_exit(zfsvfs
, FTAG
);
3847 len
= bfp
->l_len
; /* 0 means from off to end of file */
3849 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
3851 zfs_exit(zfsvfs
, FTAG
);
3856 zfs_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
3858 (void) cr
, (void) ct
;
3859 znode_t
*zp
= VTOZ(vp
);
3860 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3863 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs
);
3864 if (zp
->z_sa_hdl
== NULL
) {
3866 * The fs has been unmounted, or we did a
3867 * suspend/resume and this file no longer exists.
3869 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3874 if (zp
->z_unlinked
) {
3876 * Fast path to recycle a vnode of a removed file.
3878 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3883 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
3884 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
3886 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3887 zfs_sa_upgrade_txholds(tx
, zp
);
3888 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3892 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
3893 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
3894 zp
->z_atime_dirty
= 0;
3898 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3902 _Static_assert(sizeof (struct zfid_short
) <= sizeof (struct fid
),
3903 "struct zfid_short bigger than struct fid");
3904 _Static_assert(sizeof (struct zfid_long
) <= sizeof (struct fid
),
3905 "struct zfid_long bigger than struct fid");
3908 zfs_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
3911 znode_t
*zp
= VTOZ(vp
);
3912 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3915 uint64_t object
= zp
->z_id
;
3919 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3922 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
3923 &gen64
, sizeof (uint64_t))) != 0) {
3924 zfs_exit(zfsvfs
, FTAG
);
3928 gen
= (uint32_t)gen64
;
3930 size
= (zfsvfs
->z_parent
!= zfsvfs
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
3931 fidp
->fid_len
= size
;
3933 zfid
= (zfid_short_t
*)fidp
;
3935 zfid
->zf_len
= size
;
3937 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
3938 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
3940 /* Must have a non-zero generation number to distinguish from .zfs */
3943 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
3944 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
3946 if (size
== LONG_FID_LEN
) {
3947 uint64_t objsetid
= dmu_objset_id(zfsvfs
->z_os
);
3950 zlfid
= (zfid_long_t
*)fidp
;
3952 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
3953 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
3955 /* XXX - this should be the generation number for the objset */
3956 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
3957 zlfid
->zf_setgen
[i
] = 0;
3960 zfs_exit(zfsvfs
, FTAG
);
3965 zfs_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
3966 caller_context_t
*ct
)
3974 *valp
= MIN(LONG_MAX
, ZFS_LINK_MAX
);
3977 case _PC_FILESIZEBITS
:
3980 case _PC_MIN_HOLE_SIZE
:
3981 *valp
= (int)SPA_MINBLOCKSIZE
;
3983 case _PC_ACL_EXTENDED
:
3984 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
3986 zfsvfs
= zp
->z_zfsvfs
;
3987 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3989 *valp
= zfsvfs
->z_acl_type
== ZFSACLTYPE_POSIX
? 1 : 0;
3990 zfs_exit(zfsvfs
, FTAG
);
3998 zfsvfs
= zp
->z_zfsvfs
;
3999 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
4001 *valp
= zfsvfs
->z_acl_type
== ZFS_ACLTYPE_NFSV4
? 1 : 0;
4002 zfs_exit(zfsvfs
, FTAG
);
4005 case _PC_ACL_PATH_MAX
:
4006 *valp
= ACL_MAX_ENTRIES
;
4010 return (EOPNOTSUPP
);
4015 zfs_getpages(struct vnode
*vp
, vm_page_t
*ma
, int count
, int *rbehind
,
4018 znode_t
*zp
= VTOZ(vp
);
4019 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4020 zfs_locked_range_t
*lr
;
4022 off_t start
, end
, obj_size
;
4024 int pgsin_b
, pgsin_a
;
4027 if (zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
) != 0)
4028 return (zfs_vm_pagerret_error
);
4030 start
= IDX_TO_OFF(ma
[0]->pindex
);
4031 end
= IDX_TO_OFF(ma
[count
- 1]->pindex
+ 1);
4034 * Lock a range covering all required and optional pages.
4035 * Note that we need to handle the case of the block size growing.
4038 blksz
= zp
->z_blksz
;
4039 lr
= zfs_rangelock_tryenter(&zp
->z_rangelock
,
4040 rounddown(start
, blksz
),
4041 roundup(end
, blksz
) - rounddown(start
, blksz
), RL_READER
);
4043 if (rahead
!= NULL
) {
4047 if (rbehind
!= NULL
) {
4053 if (blksz
== zp
->z_blksz
)
4055 zfs_rangelock_exit(lr
);
4058 object
= ma
[0]->object
;
4059 zfs_vmobject_wlock(object
);
4060 obj_size
= object
->un_pager
.vnp
.vnp_size
;
4061 zfs_vmobject_wunlock(object
);
4062 if (IDX_TO_OFF(ma
[count
- 1]->pindex
) >= obj_size
) {
4064 zfs_rangelock_exit(lr
);
4065 zfs_exit(zfsvfs
, FTAG
);
4066 return (zfs_vm_pagerret_bad
);
4070 if (rbehind
!= NULL
) {
4071 pgsin_b
= OFF_TO_IDX(start
- rounddown(start
, blksz
));
4072 pgsin_b
= MIN(*rbehind
, pgsin_b
);
4076 if (rahead
!= NULL
) {
4077 pgsin_a
= OFF_TO_IDX(roundup(end
, blksz
) - end
);
4078 if (end
+ IDX_TO_OFF(pgsin_a
) >= obj_size
)
4079 pgsin_a
= OFF_TO_IDX(round_page(obj_size
) - end
);
4080 pgsin_a
= MIN(*rahead
, pgsin_a
);
4084 * NB: we need to pass the exact byte size of the data that we expect
4085 * to read after accounting for the file size. This is required because
4086 * ZFS will panic if we request DMU to read beyond the end of the last
4089 error
= dmu_read_pages(zfsvfs
->z_os
, zp
->z_id
, ma
, count
, &pgsin_b
,
4090 &pgsin_a
, MIN(end
, obj_size
) - (end
- PAGE_SIZE
));
4093 zfs_rangelock_exit(lr
);
4094 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
4096 dataset_kstats_update_read_kstats(&zfsvfs
->z_kstat
, count
*PAGE_SIZE
);
4098 zfs_exit(zfsvfs
, FTAG
);
4101 return (zfs_vm_pagerret_error
);
4103 VM_CNT_INC(v_vnodein
);
4104 VM_CNT_ADD(v_vnodepgsin
, count
+ pgsin_b
+ pgsin_a
);
4105 if (rbehind
!= NULL
)
4109 return (zfs_vm_pagerret_ok
);
4112 #ifndef _SYS_SYSPROTO_H_
4113 struct vop_getpages_args
{
4123 zfs_freebsd_getpages(struct vop_getpages_args
*ap
)
4126 return (zfs_getpages(ap
->a_vp
, ap
->a_m
, ap
->a_count
, ap
->a_rbehind
,
4131 zfs_putpages(struct vnode
*vp
, vm_page_t
*ma
, size_t len
, int flags
,
4134 znode_t
*zp
= VTOZ(vp
);
4135 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4136 zfs_locked_range_t
*lr
;
4144 vm_ooffset_t lo_off
;
4152 object
= vp
->v_object
;
4153 KASSERT(ma
[0]->object
== object
, ("mismatching object"));
4154 KASSERT(len
> 0 && (len
& PAGE_MASK
) == 0, ("unexpected length"));
4158 for (i
= 0; i
< pcount
; i
++)
4159 rtvals
[i
] = zfs_vm_pagerret_error
;
4161 if (zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
) != 0)
4162 return (zfs_vm_pagerret_error
);
4164 off
= IDX_TO_OFF(ma
[0]->pindex
);
4165 blksz
= zp
->z_blksz
;
4166 lo_off
= rounddown(off
, blksz
);
4167 lo_len
= roundup(len
+ (off
- lo_off
), blksz
);
4168 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, lo_off
, lo_len
, RL_WRITER
);
4170 zfs_vmobject_wlock(object
);
4171 if (len
+ off
> object
->un_pager
.vnp
.vnp_size
) {
4172 if (object
->un_pager
.vnp
.vnp_size
> off
) {
4175 len
= object
->un_pager
.vnp
.vnp_size
- off
;
4177 if ((pgoff
= (int)len
& PAGE_MASK
) != 0) {
4179 * If the object is locked and the following
4180 * conditions hold, then the page's dirty
4181 * field cannot be concurrently changed by a
4185 vm_page_assert_sbusied(m
);
4186 KASSERT(!pmap_page_is_write_mapped(m
),
4187 ("zfs_putpages: page %p is not read-only",
4189 vm_page_clear_dirty(m
, pgoff
, PAGE_SIZE
-
4196 if (ncount
< pcount
) {
4197 for (i
= ncount
; i
< pcount
; i
++) {
4198 rtvals
[i
] = zfs_vm_pagerret_bad
;
4202 zfs_vmobject_wunlock(object
);
4207 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
, zp
->z_uid
) ||
4208 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
, zp
->z_gid
) ||
4209 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
4210 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
4215 tx
= dmu_tx_create(zfsvfs
->z_os
);
4216 dmu_tx_hold_write(tx
, zp
->z_id
, off
, len
);
4218 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4219 zfs_sa_upgrade_txholds(tx
, zp
);
4220 err
= dmu_tx_assign(tx
, TXG_WAIT
);
4226 if (zp
->z_blksz
< PAGE_SIZE
) {
4227 for (i
= 0; len
> 0; off
+= tocopy
, len
-= tocopy
, i
++) {
4228 tocopy
= len
> PAGE_SIZE
? PAGE_SIZE
: len
;
4229 va
= zfs_map_page(ma
[i
], &sf
);
4230 dmu_write(zfsvfs
->z_os
, zp
->z_id
, off
, tocopy
, va
, tx
);
4234 err
= dmu_write_pages(zfsvfs
->z_os
, zp
->z_id
, off
, len
, ma
, tx
);
4238 uint64_t mtime
[2], ctime
[2];
4239 sa_bulk_attr_t bulk
[3];
4242 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
4244 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
4246 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
4248 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
4249 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
4252 * XXX we should be passing a callback to undirty
4253 * but that would make the locking messier
4255 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, off
,
4256 len
, 0, NULL
, NULL
);
4258 zfs_vmobject_wlock(object
);
4259 for (i
= 0; i
< ncount
; i
++) {
4260 rtvals
[i
] = zfs_vm_pagerret_ok
;
4261 vm_page_undirty(ma
[i
]);
4263 zfs_vmobject_wunlock(object
);
4264 VM_CNT_INC(v_vnodeout
);
4265 VM_CNT_ADD(v_vnodepgsout
, ncount
);
4270 zfs_rangelock_exit(lr
);
4271 if ((flags
& (zfs_vm_pagerput_sync
| zfs_vm_pagerput_inval
)) != 0 ||
4272 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4273 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
4275 dataset_kstats_update_write_kstats(&zfsvfs
->z_kstat
, len
);
4277 zfs_exit(zfsvfs
, FTAG
);
4281 #ifndef _SYS_SYSPROTO_H_
4282 struct vop_putpages_args
{
4292 zfs_freebsd_putpages(struct vop_putpages_args
*ap
)
4295 return (zfs_putpages(ap
->a_vp
, ap
->a_m
, ap
->a_count
, ap
->a_sync
,
4299 #ifndef _SYS_SYSPROTO_H_
4300 struct vop_bmap_args
{
4303 struct bufobj
**a_bop
;
4311 zfs_freebsd_bmap(struct vop_bmap_args
*ap
)
4314 if (ap
->a_bop
!= NULL
)
4315 *ap
->a_bop
= &ap
->a_vp
->v_bufobj
;
4316 if (ap
->a_bnp
!= NULL
)
4317 *ap
->a_bnp
= ap
->a_bn
;
4318 if (ap
->a_runp
!= NULL
)
4320 if (ap
->a_runb
!= NULL
)
4326 #ifndef _SYS_SYSPROTO_H_
4327 struct vop_open_args
{
4330 struct ucred
*a_cred
;
4331 struct thread
*a_td
;
4336 zfs_freebsd_open(struct vop_open_args
*ap
)
4338 vnode_t
*vp
= ap
->a_vp
;
4339 znode_t
*zp
= VTOZ(vp
);
4342 error
= zfs_open(&vp
, ap
->a_mode
, ap
->a_cred
);
4344 vnode_create_vobject(vp
, zp
->z_size
, ap
->a_td
);
4348 #ifndef _SYS_SYSPROTO_H_
4349 struct vop_close_args
{
4352 struct ucred
*a_cred
;
4353 struct thread
*a_td
;
4358 zfs_freebsd_close(struct vop_close_args
*ap
)
4361 return (zfs_close(ap
->a_vp
, ap
->a_fflag
, 1, 0, ap
->a_cred
));
4364 #ifndef _SYS_SYSPROTO_H_
4365 struct vop_ioctl_args
{
4376 zfs_freebsd_ioctl(struct vop_ioctl_args
*ap
)
4379 return (zfs_ioctl(ap
->a_vp
, ap
->a_command
, (intptr_t)ap
->a_data
,
4380 ap
->a_fflag
, ap
->a_cred
, NULL
));
4384 ioflags(int ioflags
)
4388 if (ioflags
& IO_APPEND
)
4390 if (ioflags
& IO_NDELAY
)
4391 flags
|= O_NONBLOCK
;
4392 if (ioflags
& IO_SYNC
)
4398 #ifndef _SYS_SYSPROTO_H_
4399 struct vop_read_args
{
4403 struct ucred
*a_cred
;
4408 zfs_freebsd_read(struct vop_read_args
*ap
)
4411 zfs_uio_init(&uio
, ap
->a_uio
);
4412 return (zfs_read(VTOZ(ap
->a_vp
), &uio
, ioflags(ap
->a_ioflag
),
4416 #ifndef _SYS_SYSPROTO_H_
4417 struct vop_write_args
{
4421 struct ucred
*a_cred
;
4426 zfs_freebsd_write(struct vop_write_args
*ap
)
4429 zfs_uio_init(&uio
, ap
->a_uio
);
4430 return (zfs_write(VTOZ(ap
->a_vp
), &uio
, ioflags(ap
->a_ioflag
),
4434 #if __FreeBSD_version >= 1300102
4436 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4437 * the comment above cache_fplookup for details.
4440 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args
*v
)
4448 if (__predict_false(zp
== NULL
))
4450 pflags
= atomic_load_64(&zp
->z_pflags
);
4451 if (pflags
& ZFS_AV_QUARANTINED
)
4453 if (pflags
& ZFS_XATTR
)
4455 if ((pflags
& ZFS_NO_EXECS_DENIED
) == 0)
4461 #if __FreeBSD_version >= 1300139
4463 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args
*v
)
4471 if (__predict_false(zp
== NULL
)) {
4475 target
= atomic_load_consume_ptr(&zp
->z_cached_symlink
);
4476 if (target
== NULL
) {
4479 return (cache_symlink_resolve(v
->a_fpl
, target
, strlen(target
)));
4483 #ifndef _SYS_SYSPROTO_H_
4484 struct vop_access_args
{
4486 accmode_t a_accmode
;
4487 struct ucred
*a_cred
;
4488 struct thread
*a_td
;
4493 zfs_freebsd_access(struct vop_access_args
*ap
)
4495 vnode_t
*vp
= ap
->a_vp
;
4496 znode_t
*zp
= VTOZ(vp
);
4501 if (ap
->a_accmode
== VEXEC
) {
4502 if (zfs_fastaccesschk_execute(zp
, ap
->a_cred
) == 0)
4507 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4509 accmode
= ap
->a_accmode
& (VREAD
|VWRITE
|VEXEC
|VAPPEND
);
4511 error
= zfs_access(zp
, accmode
, 0, ap
->a_cred
);
4514 * VADMIN has to be handled by vaccess().
4517 accmode
= ap
->a_accmode
& ~(VREAD
|VWRITE
|VEXEC
|VAPPEND
);
4519 #if __FreeBSD_version >= 1300105
4520 error
= vaccess(vp
->v_type
, zp
->z_mode
, zp
->z_uid
,
4521 zp
->z_gid
, accmode
, ap
->a_cred
);
4523 error
= vaccess(vp
->v_type
, zp
->z_mode
, zp
->z_uid
,
4524 zp
->z_gid
, accmode
, ap
->a_cred
, NULL
);
4530 * For VEXEC, ensure that at least one execute bit is set for
4533 if (error
== 0 && (ap
->a_accmode
& VEXEC
) != 0 && vp
->v_type
!= VDIR
&&
4534 (zp
->z_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0) {
4541 #ifndef _SYS_SYSPROTO_H_
4542 struct vop_lookup_args
{
4543 struct vnode
*a_dvp
;
4544 struct vnode
**a_vpp
;
4545 struct componentname
*a_cnp
;
4550 zfs_freebsd_lookup(struct vop_lookup_args
*ap
, boolean_t cached
)
4552 struct componentname
*cnp
= ap
->a_cnp
;
4553 char nm
[NAME_MAX
+ 1];
4555 ASSERT3U(cnp
->cn_namelen
, <, sizeof (nm
));
4556 strlcpy(nm
, cnp
->cn_nameptr
, MIN(cnp
->cn_namelen
+ 1, sizeof (nm
)));
4558 return (zfs_lookup(ap
->a_dvp
, nm
, ap
->a_vpp
, cnp
, cnp
->cn_nameiop
,
4559 cnp
->cn_cred
, 0, cached
));
4563 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args
*ap
)
4566 return (zfs_freebsd_lookup((struct vop_lookup_args
*)ap
, B_TRUE
));
4569 #ifndef _SYS_SYSPROTO_H_
4570 struct vop_lookup_args
{
4571 struct vnode
*a_dvp
;
4572 struct vnode
**a_vpp
;
4573 struct componentname
*a_cnp
;
4578 zfs_cache_lookup(struct vop_lookup_args
*ap
)
4582 zfsvfs
= ap
->a_dvp
->v_mount
->mnt_data
;
4583 if (zfsvfs
->z_use_namecache
)
4584 return (vfs_cache_lookup(ap
));
4586 return (zfs_freebsd_lookup(ap
, B_FALSE
));
4589 #ifndef _SYS_SYSPROTO_H_
4590 struct vop_create_args
{
4591 struct vnode
*a_dvp
;
4592 struct vnode
**a_vpp
;
4593 struct componentname
*a_cnp
;
4594 struct vattr
*a_vap
;
4599 zfs_freebsd_create(struct vop_create_args
*ap
)
4602 struct componentname
*cnp
= ap
->a_cnp
;
4603 vattr_t
*vap
= ap
->a_vap
;
4607 #if __FreeBSD_version < 1400068
4608 ASSERT(cnp
->cn_flags
& SAVENAME
);
4611 vattr_init_mask(vap
);
4612 mode
= vap
->va_mode
& ALLPERMS
;
4613 zfsvfs
= ap
->a_dvp
->v_mount
->mnt_data
;
4616 rc
= zfs_create(VTOZ(ap
->a_dvp
), cnp
->cn_nameptr
, vap
, 0, mode
,
4617 &zp
, cnp
->cn_cred
, 0 /* flag */, NULL
/* vsecattr */, NULL
);
4619 *ap
->a_vpp
= ZTOV(zp
);
4620 if (zfsvfs
->z_use_namecache
&&
4621 rc
== 0 && (cnp
->cn_flags
& MAKEENTRY
) != 0)
4622 cache_enter(ap
->a_dvp
, *ap
->a_vpp
, cnp
);
4627 #ifndef _SYS_SYSPROTO_H_
4628 struct vop_remove_args
{
4629 struct vnode
*a_dvp
;
4631 struct componentname
*a_cnp
;
4636 zfs_freebsd_remove(struct vop_remove_args
*ap
)
4639 #if __FreeBSD_version < 1400068
4640 ASSERT(ap
->a_cnp
->cn_flags
& SAVENAME
);
4643 return (zfs_remove_(ap
->a_dvp
, ap
->a_vp
, ap
->a_cnp
->cn_nameptr
,
4644 ap
->a_cnp
->cn_cred
));
4647 #ifndef _SYS_SYSPROTO_H_
4648 struct vop_mkdir_args
{
4649 struct vnode
*a_dvp
;
4650 struct vnode
**a_vpp
;
4651 struct componentname
*a_cnp
;
4652 struct vattr
*a_vap
;
4657 zfs_freebsd_mkdir(struct vop_mkdir_args
*ap
)
4659 vattr_t
*vap
= ap
->a_vap
;
4663 #if __FreeBSD_version < 1400068
4664 ASSERT(ap
->a_cnp
->cn_flags
& SAVENAME
);
4667 vattr_init_mask(vap
);
4670 rc
= zfs_mkdir(VTOZ(ap
->a_dvp
), ap
->a_cnp
->cn_nameptr
, vap
, &zp
,
4671 ap
->a_cnp
->cn_cred
, 0, NULL
, NULL
);
4674 *ap
->a_vpp
= ZTOV(zp
);
4678 #ifndef _SYS_SYSPROTO_H_
4679 struct vop_rmdir_args
{
4680 struct vnode
*a_dvp
;
4682 struct componentname
*a_cnp
;
4687 zfs_freebsd_rmdir(struct vop_rmdir_args
*ap
)
4689 struct componentname
*cnp
= ap
->a_cnp
;
4691 #if __FreeBSD_version < 1400068
4692 ASSERT(cnp
->cn_flags
& SAVENAME
);
4695 return (zfs_rmdir_(ap
->a_dvp
, ap
->a_vp
, cnp
->cn_nameptr
, cnp
->cn_cred
));
4698 #ifndef _SYS_SYSPROTO_H_
4699 struct vop_readdir_args
{
4702 struct ucred
*a_cred
;
4705 cookie_t
**a_cookies
;
4710 zfs_freebsd_readdir(struct vop_readdir_args
*ap
)
4713 zfs_uio_init(&uio
, ap
->a_uio
);
4714 return (zfs_readdir(ap
->a_vp
, &uio
, ap
->a_cred
, ap
->a_eofflag
,
4715 ap
->a_ncookies
, ap
->a_cookies
));
4718 #ifndef _SYS_SYSPROTO_H_
4719 struct vop_fsync_args
{
4722 struct thread
*a_td
;
4727 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 #if __FreeBSD_version >= 1300039
6215 #ifndef _SYS_SYSPROTO_H_
6216 struct vop_copy_file_range_args
{
6217 struct vnode
*a_invp
;
6219 struct vnode
*a_outvp
;
6222 unsigned int a_flags
;
6223 struct ucred
*a_incred
;
6224 struct ucred
*a_outcred
;
6225 struct thread
*a_fsizetd
;
6229 * TODO: FreeBSD will only call file system-specific copy_file_range() if both
6230 * files resides under the same mountpoint. In case of ZFS we want to be called
6231 * even is files are in different datasets (but on the same pools, but we need
6232 * to check that ourselves).
6235 zfs_freebsd_copy_file_range(struct vop_copy_file_range_args
*ap
)
6237 zfsvfs_t
*outzfsvfs
;
6238 struct vnode
*invp
= ap
->a_invp
;
6239 struct vnode
*outvp
= ap
->a_outvp
;
6243 uint64_t len
= *ap
->a_lenp
;
6245 if (!zfs_bclone_enabled
) {
6247 goto bad_write_fallback
;
6251 * TODO: If offset/length is not aligned to recordsize, use
6252 * vn_generic_copy_file_range() on this fragment.
6253 * It would be better to do this after we lock the vnodes, but then we
6254 * need something else than vn_generic_copy_file_range().
6257 vn_start_write(outvp
, &mp
, V_WAIT
);
6258 if (__predict_true(mp
== outvp
->v_mount
)) {
6259 outzfsvfs
= (zfsvfs_t
*)mp
->mnt_data
;
6260 if (!spa_feature_is_enabled(dmu_objset_spa(outzfsvfs
->z_os
),
6261 SPA_FEATURE_BLOCK_CLONING
)) {
6262 goto bad_write_fallback
;
6265 if (invp
== outvp
) {
6266 if (vn_lock(outvp
, LK_EXCLUSIVE
) != 0) {
6267 goto bad_write_fallback
;
6270 #if (__FreeBSD_version >= 1302506 && __FreeBSD_version < 1400000) || \
6271 __FreeBSD_version >= 1400086
6272 vn_lock_pair(invp
, false, LK_EXCLUSIVE
, outvp
, false,
6275 vn_lock_pair(invp
, false, outvp
, false);
6277 if (VN_IS_DOOMED(invp
) || VN_IS_DOOMED(outvp
)) {
6278 goto bad_locked_fallback
;
6283 error
= mac_vnode_check_write(curthread
->td_ucred
, ap
->a_outcred
,
6289 io
.uio_offset
= *ap
->a_outoffp
;
6290 io
.uio_resid
= *ap
->a_lenp
;
6291 error
= vn_rlimit_fsize(outvp
, &io
, ap
->a_fsizetd
);
6295 error
= zfs_clone_range(VTOZ(invp
), ap
->a_inoffp
, VTOZ(outvp
),
6296 ap
->a_outoffp
, &len
, ap
->a_outcred
);
6297 if (error
== EXDEV
|| error
== EAGAIN
|| error
== EINVAL
||
6298 error
== EOPNOTSUPP
)
6299 goto bad_locked_fallback
;
6300 *ap
->a_lenp
= (size_t)len
;
6306 vn_finished_write(mp
);
6309 bad_locked_fallback
:
6315 vn_finished_write(mp
);
6316 error
= vn_generic_copy_file_range(ap
->a_invp
, ap
->a_inoffp
,
6317 ap
->a_outvp
, ap
->a_outoffp
, ap
->a_lenp
, ap
->a_flags
,
6318 ap
->a_incred
, ap
->a_outcred
, ap
->a_fsizetd
);
6323 struct vop_vector zfs_vnodeops
;
6324 struct vop_vector zfs_fifoops
;
6325 struct vop_vector zfs_shareops
;
6327 struct vop_vector zfs_vnodeops
= {
6328 .vop_default
= &default_vnodeops
,
6329 .vop_inactive
= zfs_freebsd_inactive
,
6330 #if __FreeBSD_version >= 1300042
6331 .vop_need_inactive
= zfs_freebsd_need_inactive
,
6333 .vop_reclaim
= zfs_freebsd_reclaim
,
6334 #if __FreeBSD_version >= 1300102
6335 .vop_fplookup_vexec
= zfs_freebsd_fplookup_vexec
,
6337 #if __FreeBSD_version >= 1300139
6338 .vop_fplookup_symlink
= zfs_freebsd_fplookup_symlink
,
6340 .vop_access
= zfs_freebsd_access
,
6341 .vop_allocate
= VOP_EINVAL
,
6342 #if __FreeBSD_version >= 1400032
6343 .vop_deallocate
= zfs_deallocate
,
6345 .vop_lookup
= zfs_cache_lookup
,
6346 .vop_cachedlookup
= zfs_freebsd_cachedlookup
,
6347 .vop_getattr
= zfs_freebsd_getattr
,
6348 .vop_setattr
= zfs_freebsd_setattr
,
6349 .vop_create
= zfs_freebsd_create
,
6350 .vop_mknod
= (vop_mknod_t
*)zfs_freebsd_create
,
6351 .vop_mkdir
= zfs_freebsd_mkdir
,
6352 .vop_readdir
= zfs_freebsd_readdir
,
6353 .vop_fsync
= zfs_freebsd_fsync
,
6354 .vop_open
= zfs_freebsd_open
,
6355 .vop_close
= zfs_freebsd_close
,
6356 .vop_rmdir
= zfs_freebsd_rmdir
,
6357 .vop_ioctl
= zfs_freebsd_ioctl
,
6358 .vop_link
= zfs_freebsd_link
,
6359 .vop_symlink
= zfs_freebsd_symlink
,
6360 .vop_readlink
= zfs_freebsd_readlink
,
6361 .vop_read
= zfs_freebsd_read
,
6362 .vop_write
= zfs_freebsd_write
,
6363 .vop_remove
= zfs_freebsd_remove
,
6364 .vop_rename
= zfs_freebsd_rename
,
6365 .vop_pathconf
= zfs_freebsd_pathconf
,
6366 .vop_bmap
= zfs_freebsd_bmap
,
6367 .vop_fid
= zfs_freebsd_fid
,
6368 .vop_getextattr
= zfs_getextattr
,
6369 .vop_deleteextattr
= zfs_deleteextattr
,
6370 .vop_setextattr
= zfs_setextattr
,
6371 .vop_listextattr
= zfs_listextattr
,
6372 .vop_getacl
= zfs_freebsd_getacl
,
6373 .vop_setacl
= zfs_freebsd_setacl
,
6374 .vop_aclcheck
= zfs_freebsd_aclcheck
,
6375 .vop_getpages
= zfs_freebsd_getpages
,
6376 .vop_putpages
= zfs_freebsd_putpages
,
6377 .vop_vptocnp
= zfs_vptocnp
,
6378 #if __FreeBSD_version >= 1300064
6379 .vop_lock1
= vop_lock
,
6380 .vop_unlock
= vop_unlock
,
6381 .vop_islocked
= vop_islocked
,
6383 #if __FreeBSD_version >= 1400043
6384 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6386 #if __FreeBSD_version >= 1300039
6387 .vop_copy_file_range
= zfs_freebsd_copy_file_range
,
6390 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops
);
6392 struct vop_vector zfs_fifoops
= {
6393 .vop_default
= &fifo_specops
,
6394 .vop_fsync
= zfs_freebsd_fsync
,
6395 #if __FreeBSD_version >= 1300102
6396 .vop_fplookup_vexec
= zfs_freebsd_fplookup_vexec
,
6398 #if __FreeBSD_version >= 1300139
6399 .vop_fplookup_symlink
= zfs_freebsd_fplookup_symlink
,
6401 .vop_access
= zfs_freebsd_access
,
6402 .vop_getattr
= zfs_freebsd_getattr
,
6403 .vop_inactive
= zfs_freebsd_inactive
,
6404 .vop_read
= VOP_PANIC
,
6405 .vop_reclaim
= zfs_freebsd_reclaim
,
6406 .vop_setattr
= zfs_freebsd_setattr
,
6407 .vop_write
= VOP_PANIC
,
6408 .vop_pathconf
= zfs_freebsd_pathconf
,
6409 .vop_fid
= zfs_freebsd_fid
,
6410 .vop_getacl
= zfs_freebsd_getacl
,
6411 .vop_setacl
= zfs_freebsd_setacl
,
6412 .vop_aclcheck
= zfs_freebsd_aclcheck
,
6413 #if __FreeBSD_version >= 1400043
6414 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6417 VFS_VOP_VECTOR_REGISTER(zfs_fifoops
);
6420 * special share hidden files vnode operations template
6422 struct vop_vector zfs_shareops
= {
6423 .vop_default
= &default_vnodeops
,
6424 #if __FreeBSD_version >= 1300121
6425 .vop_fplookup_vexec
= VOP_EAGAIN
,
6427 #if __FreeBSD_version >= 1300139
6428 .vop_fplookup_symlink
= VOP_EAGAIN
,
6430 .vop_access
= zfs_freebsd_access
,
6431 .vop_inactive
= zfs_freebsd_inactive
,
6432 .vop_reclaim
= zfs_freebsd_reclaim
,
6433 .vop_fid
= zfs_freebsd_fid
,
6434 .vop_pathconf
= zfs_freebsd_pathconf
,
6435 #if __FreeBSD_version >= 1400043
6436 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6439 VFS_VOP_VECTOR_REGISTER(zfs_shareops
);
6441 ZFS_MODULE_PARAM(zfs
, zfs_
, xattr_compat
, INT
, ZMOD_RW
,
6442 "Use legacy ZFS xattr naming for writing new user namespace xattrs");