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
);
1872 /* Prefetch znode */
1874 dmu_prefetch(os
, objnum
, 0, 0, 0,
1875 ZIO_PRIORITY_SYNC_READ
);
1878 * Move to the next entry, fill in the previous offset.
1880 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
1881 zap_cursor_advance(&zc
);
1882 offset
= zap_cursor_serialize(&zc
);
1887 /* Fill the offset right after advancing the cursor. */
1890 if (cooks
!= NULL
) {
1893 KASSERT(ncooks
>= 0, ("ncookies=%d", ncooks
));
1896 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
1898 /* Subtract unused cookies */
1899 if (ncookies
!= NULL
)
1900 *ncookies
-= ncooks
;
1902 if (zfs_uio_segflg(uio
) == UIO_SYSSPACE
&& zfs_uio_iovcnt(uio
) == 1) {
1903 iovp
->iov_base
+= outcount
;
1904 iovp
->iov_len
-= outcount
;
1905 zfs_uio_resid(uio
) -= outcount
;
1907 zfs_uiomove(outbuf
, (long)outcount
, UIO_READ
, uio
))) {
1909 * Reset the pointer.
1911 offset
= zfs_uio_offset(uio
);
1915 zap_cursor_fini(&zc
);
1916 if (zfs_uio_segflg(uio
) != UIO_SYSSPACE
|| zfs_uio_iovcnt(uio
) != 1)
1917 kmem_free(outbuf
, bufsize
);
1919 if (error
== ENOENT
)
1922 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
1924 zfs_uio_setoffset(uio
, offset
);
1925 zfs_exit(zfsvfs
, FTAG
);
1926 if (error
!= 0 && cookies
!= NULL
) {
1927 free(*cookies
, M_TEMP
);
1935 * Get the requested file attributes and place them in the provided
1938 * IN: vp - vnode of file.
1939 * vap - va_mask identifies requested attributes.
1940 * If AT_XVATTR set, then optional attrs are requested
1941 * flags - ATTR_NOACLCHECK (CIFS server context)
1942 * cr - credentials of caller.
1944 * OUT: vap - attribute values.
1946 * RETURN: 0 (always succeeds).
1949 zfs_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
)
1951 znode_t
*zp
= VTOZ(vp
);
1952 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1955 u_longlong_t nblocks
;
1956 uint64_t mtime
[2], ctime
[2], crtime
[2], rdev
;
1957 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
1958 xoptattr_t
*xoap
= NULL
;
1959 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
1960 sa_bulk_attr_t bulk
[4];
1963 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1966 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
1968 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
1969 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
1970 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CRTIME(zfsvfs
), NULL
, &crtime
, 16);
1971 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1972 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_RDEV(zfsvfs
), NULL
,
1975 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
1976 zfs_exit(zfsvfs
, FTAG
);
1981 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1982 * Also, if we are the owner don't bother, since owner should
1983 * always be allowed to read basic attributes of file.
1985 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
1986 (vap
->va_uid
!= crgetuid(cr
))) {
1987 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
1988 skipaclchk
, cr
, NULL
))) {
1989 zfs_exit(zfsvfs
, FTAG
);
1995 * Return all attributes. It's cheaper to provide the answer
1996 * than to determine whether we were asked the question.
1999 vap
->va_type
= IFTOVT(zp
->z_mode
);
2000 vap
->va_mode
= zp
->z_mode
& ~S_IFMT
;
2002 vap
->va_nodeid
= zp
->z_id
;
2003 vap
->va_nlink
= zp
->z_links
;
2004 if ((vp
->v_flag
& VROOT
) && zfs_show_ctldir(zp
) &&
2005 zp
->z_links
< ZFS_LINK_MAX
)
2007 vap
->va_size
= zp
->z_size
;
2008 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
2009 vap
->va_rdev
= zfs_cmpldev(rdev
);
2010 vap
->va_gen
= zp
->z_gen
;
2011 vap
->va_flags
= 0; /* FreeBSD: Reset chflags(2) flags. */
2012 vap
->va_filerev
= zp
->z_seq
;
2015 * Add in any requested optional attributes and the create time.
2016 * Also set the corresponding bits in the returned attribute bitmap.
2018 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
2019 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2021 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2022 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2025 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2026 xoap
->xoa_readonly
=
2027 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2028 XVA_SET_RTN(xvap
, XAT_READONLY
);
2031 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2033 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2034 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2037 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2039 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2040 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2043 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2044 xoap
->xoa_nounlink
=
2045 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2046 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2049 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2050 xoap
->xoa_immutable
=
2051 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2052 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2055 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2056 xoap
->xoa_appendonly
=
2057 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2058 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2061 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2063 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2064 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2067 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2069 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2070 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2073 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2074 xoap
->xoa_av_quarantined
=
2075 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2076 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2079 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2080 xoap
->xoa_av_modified
=
2081 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2082 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2085 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2086 vp
->v_type
== VREG
) {
2087 zfs_sa_get_scanstamp(zp
, xvap
);
2090 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2091 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2092 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2094 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2095 xoap
->xoa_generation
= zp
->z_gen
;
2096 XVA_SET_RTN(xvap
, XAT_GEN
);
2099 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2101 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2102 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2105 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2107 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2108 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2111 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2112 xoap
->xoa_projinherit
=
2113 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0);
2114 XVA_SET_RTN(xvap
, XAT_PROJINHERIT
);
2117 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2118 xoap
->xoa_projid
= zp
->z_projid
;
2119 XVA_SET_RTN(xvap
, XAT_PROJID
);
2123 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2124 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2125 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2126 ZFS_TIME_DECODE(&vap
->va_birthtime
, crtime
);
2129 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2130 vap
->va_blksize
= blksize
;
2131 vap
->va_bytes
= nblocks
<< 9; /* nblocks * 512 */
2133 if (zp
->z_blksz
== 0) {
2135 * Block size hasn't been set; suggest maximal I/O transfers.
2137 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2140 zfs_exit(zfsvfs
, FTAG
);
2145 * Set the file attributes to the values contained in the
2148 * IN: zp - znode of file to be modified.
2149 * vap - new attribute values.
2150 * If AT_XVATTR set, then optional attrs are being set
2151 * flags - ATTR_UTIME set if non-default time values provided.
2152 * - ATTR_NOACLCHECK (CIFS context only).
2153 * cr - credentials of caller.
2154 * mnt_ns - Unused on FreeBSD
2156 * RETURN: 0 on success, error code on failure.
2159 * vp - ctime updated, mtime updated if size changed.
2162 zfs_setattr(znode_t
*zp
, vattr_t
*vap
, int flags
, cred_t
*cr
, zidmap_t
*mnt_ns
)
2164 vnode_t
*vp
= ZTOV(zp
);
2165 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2171 uint_t mask
= vap
->va_mask
;
2172 uint_t saved_mask
= 0;
2173 uint64_t saved_mode
;
2176 uint64_t new_uid
, new_gid
;
2178 uint64_t mtime
[2], ctime
[2];
2179 uint64_t projid
= ZFS_INVALID_PROJID
;
2181 int need_policy
= FALSE
;
2183 zfs_fuid_info_t
*fuidp
= NULL
;
2184 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2187 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2188 boolean_t fuid_dirtied
= B_FALSE
;
2189 sa_bulk_attr_t bulk
[7], xattr_bulk
[7];
2190 int count
= 0, xattr_count
= 0;
2195 if (mask
& AT_NOSET
)
2196 return (SET_ERROR(EINVAL
));
2198 if ((err
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
2202 zilog
= zfsvfs
->z_log
;
2205 * Make sure that if we have ephemeral uid/gid or xvattr specified
2206 * that file system is at proper version level
2209 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2210 (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2211 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2212 (mask
& AT_XVATTR
))) {
2213 zfs_exit(zfsvfs
, FTAG
);
2214 return (SET_ERROR(EINVAL
));
2217 if (mask
& AT_SIZE
&& vp
->v_type
== VDIR
) {
2218 zfs_exit(zfsvfs
, FTAG
);
2219 return (SET_ERROR(EISDIR
));
2222 if (mask
& AT_SIZE
&& vp
->v_type
!= VREG
&& vp
->v_type
!= VFIFO
) {
2223 zfs_exit(zfsvfs
, FTAG
);
2224 return (SET_ERROR(EINVAL
));
2228 * If this is an xvattr_t, then get a pointer to the structure of
2229 * optional attributes. If this is NULL, then we have a vattr_t.
2231 xoap
= xva_getxoptattr(xvap
);
2233 xva_init(&tmpxvattr
);
2236 * Immutable files can only alter immutable bit and atime
2238 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2239 ((mask
& (AT_SIZE
|AT_UID
|AT_GID
|AT_MTIME
|AT_MODE
)) ||
2240 ((mask
& AT_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2241 zfs_exit(zfsvfs
, FTAG
);
2242 return (SET_ERROR(EPERM
));
2246 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2250 * Verify timestamps doesn't overflow 32 bits.
2251 * ZFS can handle large timestamps, but 32bit syscalls can't
2252 * handle times greater than 2039. This check should be removed
2253 * once large timestamps are fully supported.
2255 if (mask
& (AT_ATIME
| AT_MTIME
)) {
2256 if (((mask
& AT_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2257 ((mask
& AT_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2258 zfs_exit(zfsvfs
, FTAG
);
2259 return (SET_ERROR(EOVERFLOW
));
2262 if (xoap
!= NULL
&& (mask
& AT_XVATTR
)) {
2263 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) &&
2264 TIMESPEC_OVERFLOW(&vap
->va_birthtime
)) {
2265 zfs_exit(zfsvfs
, FTAG
);
2266 return (SET_ERROR(EOVERFLOW
));
2269 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2270 if (!dmu_objset_projectquota_enabled(os
) ||
2271 (!S_ISREG(zp
->z_mode
) && !S_ISDIR(zp
->z_mode
))) {
2272 zfs_exit(zfsvfs
, FTAG
);
2273 return (SET_ERROR(EOPNOTSUPP
));
2276 projid
= xoap
->xoa_projid
;
2277 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
2278 zfs_exit(zfsvfs
, FTAG
);
2279 return (SET_ERROR(EINVAL
));
2282 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
2283 projid
= ZFS_INVALID_PROJID
;
2288 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
2289 (xoap
->xoa_projinherit
!=
2290 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) &&
2291 (!dmu_objset_projectquota_enabled(os
) ||
2292 (!S_ISREG(zp
->z_mode
) && !S_ISDIR(zp
->z_mode
)))) {
2293 zfs_exit(zfsvfs
, FTAG
);
2294 return (SET_ERROR(EOPNOTSUPP
));
2301 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
2302 zfs_exit(zfsvfs
, FTAG
);
2303 return (SET_ERROR(EROFS
));
2307 * First validate permissions
2310 if (mask
& AT_SIZE
) {
2312 * XXX - Note, we are not providing any open
2313 * mode flags here (like FNDELAY), so we may
2314 * block if there are locks present... this
2315 * should be addressed in openat().
2317 /* XXX - would it be OK to generate a log record here? */
2318 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2320 zfs_exit(zfsvfs
, FTAG
);
2325 if (mask
& (AT_ATIME
|AT_MTIME
) ||
2326 ((mask
& AT_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2327 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2328 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2329 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2330 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2331 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2332 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2333 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2334 skipaclchk
, cr
, mnt_ns
);
2337 if (mask
& (AT_UID
|AT_GID
)) {
2338 int idmask
= (mask
& (AT_UID
|AT_GID
));
2343 * NOTE: even if a new mode is being set,
2344 * we may clear S_ISUID/S_ISGID bits.
2347 if (!(mask
& AT_MODE
))
2348 vap
->va_mode
= zp
->z_mode
;
2351 * Take ownership or chgrp to group we are a member of
2354 take_owner
= (mask
& AT_UID
) && (vap
->va_uid
== crgetuid(cr
));
2355 take_group
= (mask
& AT_GID
) &&
2356 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2359 * If both AT_UID and AT_GID are set then take_owner and
2360 * take_group must both be set in order to allow taking
2363 * Otherwise, send the check through secpolicy_vnode_setattr()
2367 if (((idmask
== (AT_UID
|AT_GID
)) && take_owner
&& take_group
) ||
2368 ((idmask
== AT_UID
) && take_owner
) ||
2369 ((idmask
== AT_GID
) && take_group
)) {
2370 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2371 skipaclchk
, cr
, mnt_ns
) == 0) {
2373 * Remove setuid/setgid for non-privileged users
2375 secpolicy_setid_clear(vap
, vp
, cr
);
2376 trim_mask
= (mask
& (AT_UID
|AT_GID
));
2385 oldva
.va_mode
= zp
->z_mode
;
2386 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2387 if (mask
& AT_XVATTR
) {
2389 * Update xvattr mask to include only those attributes
2390 * that are actually changing.
2392 * the bits will be restored prior to actually setting
2393 * the attributes so the caller thinks they were set.
2395 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2396 if (xoap
->xoa_appendonly
!=
2397 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2400 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2401 XVA_SET_REQ(&tmpxvattr
, XAT_APPENDONLY
);
2405 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2406 if (xoap
->xoa_projinherit
!=
2407 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
2410 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
2411 XVA_SET_REQ(&tmpxvattr
, XAT_PROJINHERIT
);
2415 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2416 if (xoap
->xoa_nounlink
!=
2417 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2420 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2421 XVA_SET_REQ(&tmpxvattr
, XAT_NOUNLINK
);
2425 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2426 if (xoap
->xoa_immutable
!=
2427 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2430 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2431 XVA_SET_REQ(&tmpxvattr
, XAT_IMMUTABLE
);
2435 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2436 if (xoap
->xoa_nodump
!=
2437 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2440 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2441 XVA_SET_REQ(&tmpxvattr
, XAT_NODUMP
);
2445 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2446 if (xoap
->xoa_av_modified
!=
2447 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2450 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2451 XVA_SET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
);
2455 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2456 if ((vp
->v_type
!= VREG
&&
2457 xoap
->xoa_av_quarantined
) ||
2458 xoap
->xoa_av_quarantined
!=
2459 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2462 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2463 XVA_SET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
);
2467 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2468 zfs_exit(zfsvfs
, FTAG
);
2469 return (SET_ERROR(EPERM
));
2472 if (need_policy
== FALSE
&&
2473 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2474 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2479 if (mask
& AT_MODE
) {
2480 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
,
2482 err
= secpolicy_setid_setsticky_clear(vp
, vap
,
2485 zfs_exit(zfsvfs
, FTAG
);
2488 trim_mask
|= AT_MODE
;
2496 * If trim_mask is set then take ownership
2497 * has been granted or write_acl is present and user
2498 * has the ability to modify mode. In that case remove
2499 * UID|GID and or MODE from mask so that
2500 * secpolicy_vnode_setattr() doesn't revoke it.
2504 saved_mask
= vap
->va_mask
;
2505 vap
->va_mask
&= ~trim_mask
;
2506 if (trim_mask
& AT_MODE
) {
2508 * Save the mode, as secpolicy_vnode_setattr()
2509 * will overwrite it with ova.va_mode.
2511 saved_mode
= vap
->va_mode
;
2514 err
= secpolicy_vnode_setattr(cr
, vp
, vap
, &oldva
, flags
,
2515 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2517 zfs_exit(zfsvfs
, FTAG
);
2522 vap
->va_mask
|= saved_mask
;
2523 if (trim_mask
& AT_MODE
) {
2525 * Recover the mode after
2526 * secpolicy_vnode_setattr().
2528 vap
->va_mode
= saved_mode
;
2534 * secpolicy_vnode_setattr, or take ownership may have
2537 mask
= vap
->va_mask
;
2539 if ((mask
& (AT_UID
| AT_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
2540 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
2541 &xattr_obj
, sizeof (xattr_obj
));
2543 if (err
== 0 && xattr_obj
) {
2544 err
= zfs_zget(zp
->z_zfsvfs
, xattr_obj
, &attrzp
);
2546 err
= vn_lock(ZTOV(attrzp
), LK_EXCLUSIVE
);
2548 vrele(ZTOV(attrzp
));
2553 if (mask
& AT_UID
) {
2554 new_uid
= zfs_fuid_create(zfsvfs
,
2555 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2556 if (new_uid
!= zp
->z_uid
&&
2557 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
2561 err
= SET_ERROR(EDQUOT
);
2566 if (mask
& AT_GID
) {
2567 new_gid
= zfs_fuid_create(zfsvfs
, (uint64_t)vap
->va_gid
,
2568 cr
, ZFS_GROUP
, &fuidp
);
2569 if (new_gid
!= zp
->z_gid
&&
2570 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
2574 err
= SET_ERROR(EDQUOT
);
2579 if (projid
!= ZFS_INVALID_PROJID
&&
2580 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
2583 err
= SET_ERROR(EDQUOT
);
2587 tx
= dmu_tx_create(os
);
2589 if (mask
& AT_MODE
) {
2590 uint64_t pmode
= zp
->z_mode
;
2592 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2594 if (zp
->z_zfsvfs
->z_acl_mode
== ZFS_ACL_RESTRICTED
&&
2595 !(zp
->z_pflags
& ZFS_ACL_TRIVIAL
)) {
2596 err
= SET_ERROR(EPERM
);
2600 if ((err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
)))
2603 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2605 * Are we upgrading ACL from old V0 format
2608 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
2609 zfs_znode_acl_version(zp
) ==
2610 ZFS_ACL_VERSION_INITIAL
) {
2611 dmu_tx_hold_free(tx
, acl_obj
, 0,
2613 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2614 0, aclp
->z_acl_bytes
);
2616 dmu_tx_hold_write(tx
, acl_obj
, 0,
2619 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2620 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2621 0, aclp
->z_acl_bytes
);
2623 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2625 if (((mask
& AT_XVATTR
) &&
2626 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
2627 (projid
!= ZFS_INVALID_PROJID
&&
2628 !(zp
->z_pflags
& ZFS_PROJID
)))
2629 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2631 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2635 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2638 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2640 zfs_fuid_txhold(zfsvfs
, tx
);
2642 zfs_sa_upgrade_txholds(tx
, zp
);
2644 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2650 * Set each attribute requested.
2651 * We group settings according to the locks they need to acquire.
2653 * Note: you cannot set ctime directly, although it will be
2654 * updated as a side-effect of calling this function.
2657 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
2659 * For the existed object that is upgraded from old system,
2660 * its on-disk layout has no slot for the project ID attribute.
2661 * But quota accounting logic needs to access related slots by
2662 * offset directly. So we need to adjust old objects' layout
2663 * to make the project ID to some unified and fixed offset.
2666 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
2668 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
2670 if (unlikely(err
== EEXIST
))
2675 projid
= ZFS_INVALID_PROJID
;
2678 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2679 mutex_enter(&zp
->z_acl_lock
);
2681 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
2682 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2685 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2686 mutex_enter(&attrzp
->z_acl_lock
);
2687 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2688 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
2689 sizeof (attrzp
->z_pflags
));
2690 if (projid
!= ZFS_INVALID_PROJID
) {
2691 attrzp
->z_projid
= projid
;
2692 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2693 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
2694 sizeof (attrzp
->z_projid
));
2698 if (mask
& (AT_UID
|AT_GID
)) {
2700 if (mask
& AT_UID
) {
2701 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2702 &new_uid
, sizeof (new_uid
));
2703 zp
->z_uid
= new_uid
;
2705 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2706 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
2708 attrzp
->z_uid
= new_uid
;
2712 if (mask
& AT_GID
) {
2713 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
2714 NULL
, &new_gid
, sizeof (new_gid
));
2715 zp
->z_gid
= new_gid
;
2717 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2718 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
2720 attrzp
->z_gid
= new_gid
;
2723 if (!(mask
& AT_MODE
)) {
2724 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
2725 NULL
, &new_mode
, sizeof (new_mode
));
2726 new_mode
= zp
->z_mode
;
2728 err
= zfs_acl_chown_setattr(zp
);
2731 vn_seqc_write_begin(ZTOV(attrzp
));
2732 err
= zfs_acl_chown_setattr(attrzp
);
2733 vn_seqc_write_end(ZTOV(attrzp
));
2738 if (mask
& AT_MODE
) {
2739 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
2740 &new_mode
, sizeof (new_mode
));
2741 zp
->z_mode
= new_mode
;
2742 ASSERT3P(aclp
, !=, NULL
);
2743 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2745 if (zp
->z_acl_cached
)
2746 zfs_acl_free(zp
->z_acl_cached
);
2747 zp
->z_acl_cached
= aclp
;
2752 if (mask
& AT_ATIME
) {
2753 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
2754 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
2755 &zp
->z_atime
, sizeof (zp
->z_atime
));
2758 if (mask
& AT_MTIME
) {
2759 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2760 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
2761 mtime
, sizeof (mtime
));
2764 if (projid
!= ZFS_INVALID_PROJID
) {
2765 zp
->z_projid
= projid
;
2766 SA_ADD_BULK_ATTR(bulk
, count
,
2767 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
2768 sizeof (zp
->z_projid
));
2771 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2772 if (mask
& AT_SIZE
&& !(mask
& AT_MTIME
)) {
2773 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
),
2774 NULL
, mtime
, sizeof (mtime
));
2775 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2776 &ctime
, sizeof (ctime
));
2777 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
2778 } else if (mask
!= 0) {
2779 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2780 &ctime
, sizeof (ctime
));
2781 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
);
2783 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2784 SA_ZPL_CTIME(zfsvfs
), NULL
,
2785 &ctime
, sizeof (ctime
));
2786 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2792 * Do this after setting timestamps to prevent timestamp
2793 * update from toggling bit
2796 if (xoap
&& (mask
& AT_XVATTR
)) {
2798 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
))
2799 xoap
->xoa_createtime
= vap
->va_birthtime
;
2801 * restore trimmed off masks
2802 * so that return masks can be set for caller.
2805 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_APPENDONLY
)) {
2806 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2808 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NOUNLINK
)) {
2809 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2811 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_IMMUTABLE
)) {
2812 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2814 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NODUMP
)) {
2815 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2817 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
)) {
2818 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2820 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
)) {
2821 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2823 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_PROJINHERIT
)) {
2824 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
2827 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2828 ASSERT3S(vp
->v_type
, ==, VREG
);
2830 zfs_xvattr_set(zp
, xvap
, tx
);
2834 zfs_fuid_sync(zfsvfs
, tx
);
2837 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2839 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2840 mutex_exit(&zp
->z_acl_lock
);
2843 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2844 mutex_exit(&attrzp
->z_acl_lock
);
2847 if (err
== 0 && attrzp
) {
2848 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
2860 zfs_fuid_info_free(fuidp
);
2867 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2872 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
2873 zil_commit(zilog
, 0);
2875 zfs_exit(zfsvfs
, FTAG
);
2880 * Look up the directory entries corresponding to the source and target
2881 * directory/name pairs.
2884 zfs_rename_relock_lookup(znode_t
*sdzp
, const struct componentname
*scnp
,
2885 znode_t
**szpp
, znode_t
*tdzp
, const struct componentname
*tcnp
,
2893 * Before using sdzp and tdzp we must ensure that they are live.
2894 * As a porting legacy from illumos we have two things to worry
2895 * about. One is typical for FreeBSD and it is that the vnode is
2896 * not reclaimed (doomed). The other is that the znode is live.
2897 * The current code can invalidate the znode without acquiring the
2898 * corresponding vnode lock if the object represented by the znode
2899 * and vnode is no longer valid after a rollback or receive operation.
2900 * z_teardown_lock hidden behind zfs_enter and zfs_exit is the lock
2901 * that protects the znodes from the invalidation.
2903 zfsvfs
= sdzp
->z_zfsvfs
;
2904 ASSERT3P(zfsvfs
, ==, tdzp
->z_zfsvfs
);
2905 if ((error
= zfs_enter_verify_zp(zfsvfs
, sdzp
, FTAG
)) != 0)
2907 if ((error
= zfs_verify_zp(tdzp
)) != 0) {
2908 zfs_exit(zfsvfs
, FTAG
);
2913 * Re-resolve svp to be certain it still exists and fetch the
2916 error
= zfs_dirent_lookup(sdzp
, scnp
->cn_nameptr
, &szp
, ZEXISTS
);
2918 /* Source entry invalid or not there. */
2919 if ((scnp
->cn_flags
& ISDOTDOT
) != 0 ||
2920 (scnp
->cn_namelen
== 1 && scnp
->cn_nameptr
[0] == '.'))
2921 error
= SET_ERROR(EINVAL
);
2927 * Re-resolve tvp, if it disappeared we just carry on.
2929 error
= zfs_dirent_lookup(tdzp
, tcnp
->cn_nameptr
, &tzp
, 0);
2932 if ((tcnp
->cn_flags
& ISDOTDOT
) != 0)
2933 error
= SET_ERROR(EINVAL
);
2938 zfs_exit(zfsvfs
, FTAG
);
2943 * We acquire all but fdvp locks using non-blocking acquisitions. If we
2944 * fail to acquire any lock in the path we will drop all held locks,
2945 * acquire the new lock in a blocking fashion, and then release it and
2946 * restart the rename. This acquire/release step ensures that we do not
2947 * spin on a lock waiting for release. On error release all vnode locks
2948 * and decrement references the way tmpfs_rename() would do.
2951 zfs_rename_relock(struct vnode
*sdvp
, struct vnode
**svpp
,
2952 struct vnode
*tdvp
, struct vnode
**tvpp
,
2953 const struct componentname
*scnp
, const struct componentname
*tcnp
)
2955 struct vnode
*nvp
, *svp
, *tvp
;
2956 znode_t
*sdzp
, *tdzp
, *szp
, *tzp
;
2960 if (*tvpp
!= NULL
&& *tvpp
!= tdvp
)
2964 error
= vn_lock(sdvp
, LK_EXCLUSIVE
);
2967 error
= vn_lock(tdvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
2972 error
= vn_lock(tdvp
, LK_EXCLUSIVE
);
2981 error
= zfs_rename_relock_lookup(sdzp
, scnp
, &szp
, tdzp
, tcnp
, &tzp
);
2988 tvp
= tzp
!= NULL
? ZTOV(tzp
) : NULL
;
2991 * Now try acquire locks on svp and tvp.
2994 error
= vn_lock(nvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
3000 if (error
!= EBUSY
) {
3004 error
= vn_lock(nvp
, LK_EXCLUSIVE
);
3011 * Concurrent rename race.
3016 error
= SET_ERROR(EINVAL
);
3031 error
= vn_lock(nvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
3036 if (error
!= EBUSY
) {
3040 error
= vn_lock(nvp
, LK_EXCLUSIVE
);
3058 * Note that we must use VRELE_ASYNC in this function as it walks
3059 * up the directory tree and vrele may need to acquire an exclusive
3060 * lock if a last reference to a vnode is dropped.
3063 zfs_rename_check(znode_t
*szp
, znode_t
*sdzp
, znode_t
*tdzp
)
3070 zfsvfs
= tdzp
->z_zfsvfs
;
3072 return (SET_ERROR(EINVAL
));
3075 if (tdzp
->z_id
== zfsvfs
->z_root
)
3079 ASSERT(!zp
->z_unlinked
);
3080 if ((error
= sa_lookup(zp
->z_sa_hdl
,
3081 SA_ZPL_PARENT(zfsvfs
), &parent
, sizeof (parent
))) != 0)
3084 if (parent
== szp
->z_id
) {
3085 error
= SET_ERROR(EINVAL
);
3088 if (parent
== zfsvfs
->z_root
)
3090 if (parent
== sdzp
->z_id
)
3093 error
= zfs_zget(zfsvfs
, parent
, &zp1
);
3098 VN_RELE_ASYNC(ZTOV(zp
),
3099 dsl_pool_zrele_taskq(
3100 dmu_objset_pool(zfsvfs
->z_os
)));
3104 if (error
== ENOTDIR
)
3105 panic("checkpath: .. not a directory\n");
3107 VN_RELE_ASYNC(ZTOV(zp
),
3108 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs
->z_os
)));
3112 #if __FreeBSD_version < 1300124
3114 cache_vop_rename(struct vnode
*fdvp
, struct vnode
*fvp
, struct vnode
*tdvp
,
3115 struct vnode
*tvp
, struct componentname
*fcnp
, struct componentname
*tcnp
)
3121 cache_purge_negative(tdvp
);
3126 zfs_do_rename_impl(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3127 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3131 * Move an entry from the provided source directory to the target
3132 * directory. Change the entry name as indicated.
3134 * IN: sdvp - Source directory containing the "old entry".
3135 * scnp - Old entry name.
3136 * tdvp - Target directory to contain the "new entry".
3137 * tcnp - New entry name.
3138 * cr - credentials of caller.
3139 * INOUT: svpp - Source file
3140 * tvpp - Target file, may point to NULL initially
3142 * RETURN: 0 on success, error code on failure.
3145 * sdvp,tdvp - ctime|mtime updated
3148 zfs_do_rename(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3149 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3154 ASSERT_VOP_ELOCKED(tdvp
, __func__
);
3156 ASSERT_VOP_ELOCKED(*tvpp
, __func__
);
3158 /* Reject renames across filesystems. */
3159 if ((*svpp
)->v_mount
!= tdvp
->v_mount
||
3160 ((*tvpp
) != NULL
&& (*svpp
)->v_mount
!= (*tvpp
)->v_mount
)) {
3161 error
= SET_ERROR(EXDEV
);
3165 if (zfsctl_is_node(tdvp
)) {
3166 error
= SET_ERROR(EXDEV
);
3171 * Lock all four vnodes to ensure safety and semantics of renaming.
3173 error
= zfs_rename_relock(sdvp
, svpp
, tdvp
, tvpp
, scnp
, tcnp
);
3175 /* no vnodes are locked in the case of error here */
3179 error
= zfs_do_rename_impl(sdvp
, svpp
, scnp
, tdvp
, tvpp
, tcnp
, cr
);
3192 zfs_do_rename_impl(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3193 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3199 znode_t
*tdzp
, *sdzp
, *tzp
, *szp
;
3200 const char *snm
= scnp
->cn_nameptr
;
3201 const char *tnm
= tcnp
->cn_nameptr
;
3206 zfsvfs
= tdzp
->z_zfsvfs
;
3208 if ((error
= zfs_enter_verify_zp(zfsvfs
, tdzp
, FTAG
)) != 0)
3210 if ((error
= zfs_verify_zp(sdzp
)) != 0) {
3211 zfs_exit(zfsvfs
, FTAG
);
3214 zilog
= zfsvfs
->z_log
;
3216 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
3217 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3218 error
= SET_ERROR(EILSEQ
);
3222 /* If source and target are the same file, there is nothing to do. */
3223 if ((*svpp
) == (*tvpp
)) {
3228 if (((*svpp
)->v_type
== VDIR
&& (*svpp
)->v_mountedhere
!= NULL
) ||
3229 ((*tvpp
) != NULL
&& (*tvpp
)->v_type
== VDIR
&&
3230 (*tvpp
)->v_mountedhere
!= NULL
)) {
3231 error
= SET_ERROR(EXDEV
);
3236 if ((error
= zfs_verify_zp(szp
)) != 0) {
3237 zfs_exit(zfsvfs
, FTAG
);
3240 tzp
= *tvpp
== NULL
? NULL
: VTOZ(*tvpp
);
3242 if ((error
= zfs_verify_zp(tzp
)) != 0) {
3243 zfs_exit(zfsvfs
, FTAG
);
3249 * This is to prevent the creation of links into attribute space
3250 * by renaming a linked file into/outof an attribute directory.
3251 * See the comment in zfs_link() for why this is considered bad.
3253 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3254 error
= SET_ERROR(EINVAL
);
3259 * If we are using project inheritance, means if the directory has
3260 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3261 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3262 * such case, we only allow renames into our tree when the project
3265 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3266 tdzp
->z_projid
!= szp
->z_projid
) {
3267 error
= SET_ERROR(EXDEV
);
3272 * Must have write access at the source to remove the old entry
3273 * and write access at the target to create the new entry.
3274 * Note that if target and source are the same, this can be
3275 * done in a single check.
3277 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
, NULL
)))
3280 if ((*svpp
)->v_type
== VDIR
) {
3282 * Avoid ".", "..", and aliases of "." for obvious reasons.
3284 if ((scnp
->cn_namelen
== 1 && scnp
->cn_nameptr
[0] == '.') ||
3286 (scnp
->cn_flags
| tcnp
->cn_flags
) & ISDOTDOT
) {
3292 * Check to make sure rename is valid.
3293 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3295 if ((error
= zfs_rename_check(szp
, sdzp
, tdzp
)))
3300 * Does target exist?
3304 * Source and target must be the same type.
3306 if ((*svpp
)->v_type
== VDIR
) {
3307 if ((*tvpp
)->v_type
!= VDIR
) {
3308 error
= SET_ERROR(ENOTDIR
);
3316 if ((*tvpp
)->v_type
== VDIR
) {
3317 error
= SET_ERROR(EISDIR
);
3323 vn_seqc_write_begin(*svpp
);
3324 vn_seqc_write_begin(sdvp
);
3326 vn_seqc_write_begin(*tvpp
);
3328 vn_seqc_write_begin(tdvp
);
3330 vnevent_rename_src(*svpp
, sdvp
, scnp
->cn_nameptr
, ct
);
3332 vnevent_rename_dest(*tvpp
, tdvp
, tnm
, ct
);
3335 * notify the target directory if it is not the same
3336 * as source directory.
3339 vnevent_rename_dest_dir(tdvp
, ct
);
3342 tx
= dmu_tx_create(zfsvfs
->z_os
);
3343 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3344 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3345 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3346 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3348 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3349 zfs_sa_upgrade_txholds(tx
, tdzp
);
3352 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3353 zfs_sa_upgrade_txholds(tx
, tzp
);
3356 zfs_sa_upgrade_txholds(tx
, szp
);
3357 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3358 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3364 if (tzp
) /* Attempt to remove the existing target */
3365 error
= zfs_link_destroy(tdzp
, tnm
, tzp
, tx
, 0, NULL
);
3368 error
= zfs_link_create(tdzp
, tnm
, szp
, tx
, ZRENAMING
);
3370 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3372 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3373 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3376 error
= zfs_link_destroy(sdzp
, snm
, szp
, tx
, ZRENAMING
,
3379 zfs_log_rename(zilog
, tx
, TX_RENAME
, sdzp
,
3380 snm
, tdzp
, tnm
, szp
);
3383 * At this point, we have successfully created
3384 * the target name, but have failed to remove
3385 * the source name. Since the create was done
3386 * with the ZRENAMING flag, there are
3387 * complications; for one, the link count is
3388 * wrong. The easiest way to deal with this
3389 * is to remove the newly created target, and
3390 * return the original error. This must
3391 * succeed; fortunately, it is very unlikely to
3392 * fail, since we just created it.
3394 VERIFY0(zfs_link_destroy(tdzp
, tnm
, szp
, tx
,
3399 cache_vop_rename(sdvp
, *svpp
, tdvp
, *tvpp
, scnp
, tcnp
);
3406 vn_seqc_write_end(*svpp
);
3407 vn_seqc_write_end(sdvp
);
3409 vn_seqc_write_end(*tvpp
);
3411 vn_seqc_write_end(tdvp
);
3414 if (error
== 0 && zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3415 zil_commit(zilog
, 0);
3416 zfs_exit(zfsvfs
, FTAG
);
3422 zfs_rename(znode_t
*sdzp
, const char *sname
, znode_t
*tdzp
, const char *tname
,
3423 cred_t
*cr
, int flags
, uint64_t rflags
, vattr_t
*wo_vap
, zidmap_t
*mnt_ns
)
3425 struct componentname scn
, tcn
;
3426 vnode_t
*sdvp
, *tdvp
;
3431 if (rflags
!= 0 || wo_vap
!= NULL
)
3432 return (SET_ERROR(EINVAL
));
3436 error
= zfs_lookup_internal(sdzp
, sname
, &svp
, &scn
, DELETE
);
3437 if (sdzp
->z_zfsvfs
->z_replay
== B_FALSE
)
3443 vn_lock(tdvp
, LK_EXCLUSIVE
| LK_RETRY
);
3444 error
= zfs_lookup_internal(tdzp
, tname
, &tvp
, &tcn
, RENAME
);
3445 if (error
== EJUSTRETURN
)
3447 else if (error
!= 0) {
3452 error
= zfs_do_rename(sdvp
, &svp
, &scn
, tdvp
, &tvp
, &tcn
, cr
);
3463 * Insert the indicated symbolic reference entry into the directory.
3465 * IN: dvp - Directory to contain new symbolic link.
3466 * link - Name for new symlink entry.
3467 * vap - Attributes of new entry.
3468 * cr - credentials of caller.
3469 * ct - caller context
3470 * flags - case flags
3471 * mnt_ns - Unused on FreeBSD
3473 * RETURN: 0 on success, error code on failure.
3476 * dvp - ctime|mtime updated
3479 zfs_symlink(znode_t
*dzp
, const char *name
, vattr_t
*vap
,
3480 const char *link
, znode_t
**zpp
, cred_t
*cr
, int flags
, zidmap_t
*mnt_ns
)
3485 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3487 uint64_t len
= strlen(link
);
3489 zfs_acl_ids_t acl_ids
;
3490 boolean_t fuid_dirtied
;
3491 uint64_t txtype
= TX_SYMLINK
;
3493 ASSERT3S(vap
->va_type
, ==, VLNK
);
3495 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
3497 zilog
= zfsvfs
->z_log
;
3499 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3500 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3501 zfs_exit(zfsvfs
, FTAG
);
3502 return (SET_ERROR(EILSEQ
));
3505 if (len
> MAXPATHLEN
) {
3506 zfs_exit(zfsvfs
, FTAG
);
3507 return (SET_ERROR(ENAMETOOLONG
));
3510 if ((error
= zfs_acl_ids_create(dzp
, 0,
3511 vap
, cr
, NULL
, &acl_ids
, NULL
)) != 0) {
3512 zfs_exit(zfsvfs
, FTAG
);
3517 * Attempt to lock directory; fail if entry already exists.
3519 error
= zfs_dirent_lookup(dzp
, name
, &zp
, ZNEW
);
3521 zfs_acl_ids_free(&acl_ids
);
3522 zfs_exit(zfsvfs
, FTAG
);
3526 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, mnt_ns
))) {
3527 zfs_acl_ids_free(&acl_ids
);
3528 zfs_exit(zfsvfs
, FTAG
);
3532 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
,
3534 zfs_acl_ids_free(&acl_ids
);
3535 zfs_exit(zfsvfs
, FTAG
);
3536 return (SET_ERROR(EDQUOT
));
3539 getnewvnode_reserve_();
3540 tx
= dmu_tx_create(zfsvfs
->z_os
);
3541 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3542 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3543 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3544 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3545 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3546 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3547 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3548 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3549 acl_ids
.z_aclp
->z_acl_bytes
);
3552 zfs_fuid_txhold(zfsvfs
, tx
);
3553 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3555 zfs_acl_ids_free(&acl_ids
);
3557 getnewvnode_drop_reserve();
3558 zfs_exit(zfsvfs
, FTAG
);
3563 * Create a new object for the symlink.
3564 * for version 4 ZPL datasets the symlink will be an SA attribute
3566 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3569 zfs_fuid_sync(zfsvfs
, tx
);
3572 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
3573 __DECONST(void *, link
), len
, tx
);
3575 zfs_sa_symlink(zp
, __DECONST(char *, link
), len
, tx
);
3578 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
3579 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3581 * Insert the new object into the directory.
3583 (void) zfs_link_create(dzp
, name
, zp
, tx
, ZNEW
);
3585 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3588 zfs_acl_ids_free(&acl_ids
);
3592 getnewvnode_drop_reserve();
3594 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3595 zil_commit(zilog
, 0);
3597 zfs_exit(zfsvfs
, FTAG
);
3602 * Return, in the buffer contained in the provided uio structure,
3603 * the symbolic path referred to by vp.
3605 * IN: vp - vnode of symbolic link.
3606 * uio - structure to contain the link path.
3607 * cr - credentials of caller.
3608 * ct - caller context
3610 * OUT: uio - structure containing the link path.
3612 * RETURN: 0 on success, error code on failure.
3615 * vp - atime updated
3618 zfs_readlink(vnode_t
*vp
, zfs_uio_t
*uio
, cred_t
*cr
, caller_context_t
*ct
)
3620 (void) cr
, (void) ct
;
3621 znode_t
*zp
= VTOZ(vp
);
3622 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3625 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3629 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3630 SA_ZPL_SYMLINK(zfsvfs
), uio
);
3632 error
= zfs_sa_readlink(zp
, uio
);
3634 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3636 zfs_exit(zfsvfs
, FTAG
);
3641 * Insert a new entry into directory tdvp referencing svp.
3643 * IN: tdvp - Directory to contain new entry.
3644 * svp - vnode of new entry.
3645 * name - name of new entry.
3646 * cr - credentials of caller.
3648 * RETURN: 0 on success, error code on failure.
3651 * tdvp - ctime|mtime updated
3652 * svp - ctime updated
3655 zfs_link(znode_t
*tdzp
, znode_t
*szp
, const char *name
, cred_t
*cr
,
3660 zfsvfs_t
*zfsvfs
= tdzp
->z_zfsvfs
;
3667 ASSERT3S(ZTOV(tdzp
)->v_type
, ==, VDIR
);
3669 if ((error
= zfs_enter_verify_zp(zfsvfs
, tdzp
, FTAG
)) != 0)
3671 zilog
= zfsvfs
->z_log
;
3674 * POSIX dictates that we return EPERM here.
3675 * Better choices include ENOTSUP or EISDIR.
3677 if (ZTOV(szp
)->v_type
== VDIR
) {
3678 zfs_exit(zfsvfs
, FTAG
);
3679 return (SET_ERROR(EPERM
));
3682 if ((error
= zfs_verify_zp(szp
)) != 0) {
3683 zfs_exit(zfsvfs
, FTAG
);
3688 * If we are using project inheritance, means if the directory has
3689 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3690 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3691 * such case, we only allow hard link creation in our tree when the
3692 * project IDs are the same.
3694 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3695 tdzp
->z_projid
!= szp
->z_projid
) {
3696 zfs_exit(zfsvfs
, FTAG
);
3697 return (SET_ERROR(EXDEV
));
3700 if (szp
->z_pflags
& (ZFS_APPENDONLY
|
3701 ZFS_IMMUTABLE
| ZFS_READONLY
)) {
3702 zfs_exit(zfsvfs
, FTAG
);
3703 return (SET_ERROR(EPERM
));
3706 /* Prevent links to .zfs/shares files */
3708 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
3709 &parent
, sizeof (uint64_t))) != 0) {
3710 zfs_exit(zfsvfs
, FTAG
);
3713 if (parent
== zfsvfs
->z_shares_dir
) {
3714 zfs_exit(zfsvfs
, FTAG
);
3715 return (SET_ERROR(EPERM
));
3718 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3719 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3720 zfs_exit(zfsvfs
, FTAG
);
3721 return (SET_ERROR(EILSEQ
));
3725 * We do not support links between attributes and non-attributes
3726 * because of the potential security risk of creating links
3727 * into "normal" file space in order to circumvent restrictions
3728 * imposed in attribute space.
3730 if ((szp
->z_pflags
& ZFS_XATTR
) != (tdzp
->z_pflags
& ZFS_XATTR
)) {
3731 zfs_exit(zfsvfs
, FTAG
);
3732 return (SET_ERROR(EINVAL
));
3736 owner
= zfs_fuid_map_id(zfsvfs
, szp
->z_uid
, cr
, ZFS_OWNER
);
3737 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(ZTOV(szp
), cr
) != 0) {
3738 zfs_exit(zfsvfs
, FTAG
);
3739 return (SET_ERROR(EPERM
));
3742 if ((error
= zfs_zaccess(tdzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, NULL
))) {
3743 zfs_exit(zfsvfs
, FTAG
);
3748 * Attempt to lock directory; fail if entry already exists.
3750 error
= zfs_dirent_lookup(tdzp
, name
, &tzp
, ZNEW
);
3752 zfs_exit(zfsvfs
, FTAG
);
3756 tx
= dmu_tx_create(zfsvfs
->z_os
);
3757 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3758 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, name
);
3759 zfs_sa_upgrade_txholds(tx
, szp
);
3760 zfs_sa_upgrade_txholds(tx
, tdzp
);
3761 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3764 zfs_exit(zfsvfs
, FTAG
);
3768 error
= zfs_link_create(tdzp
, name
, szp
, tx
, 0);
3771 uint64_t txtype
= TX_LINK
;
3772 zfs_log_link(zilog
, tx
, txtype
, tdzp
, szp
, name
);
3778 vnevent_link(ZTOV(szp
), ct
);
3781 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3782 zil_commit(zilog
, 0);
3784 zfs_exit(zfsvfs
, FTAG
);
3789 * Free or allocate space in a file. Currently, this function only
3790 * supports the `F_FREESP' command. However, this command is somewhat
3791 * misnamed, as its functionality includes the ability to allocate as
3792 * well as free space.
3794 * IN: ip - inode of file to free data in.
3795 * cmd - action to take (only F_FREESP supported).
3796 * bfp - section of file to free/alloc.
3797 * flag - current file open mode flags.
3798 * offset - current file offset.
3799 * cr - credentials of caller.
3801 * RETURN: 0 on success, error code on failure.
3804 * ip - ctime|mtime updated
3807 zfs_space(znode_t
*zp
, int cmd
, flock64_t
*bfp
, int flag
,
3808 offset_t offset
, cred_t
*cr
)
3811 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
3815 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3818 if (cmd
!= F_FREESP
) {
3819 zfs_exit(zfsvfs
, FTAG
);
3820 return (SET_ERROR(EINVAL
));
3824 * Callers might not be able to detect properly that we are read-only,
3825 * so check it explicitly here.
3827 if (zfs_is_readonly(zfsvfs
)) {
3828 zfs_exit(zfsvfs
, FTAG
);
3829 return (SET_ERROR(EROFS
));
3832 if (bfp
->l_len
< 0) {
3833 zfs_exit(zfsvfs
, FTAG
);
3834 return (SET_ERROR(EINVAL
));
3838 * Permissions aren't checked on Solaris because on this OS
3839 * zfs_space() can only be called with an opened file handle.
3840 * On Linux we can get here through truncate_range() which
3841 * operates directly on inodes, so we need to check access rights.
3843 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
, NULL
))) {
3844 zfs_exit(zfsvfs
, FTAG
);
3849 len
= bfp
->l_len
; /* 0 means from off to end of file */
3851 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
3853 zfs_exit(zfsvfs
, FTAG
);
3858 zfs_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
3860 (void) cr
, (void) ct
;
3861 znode_t
*zp
= VTOZ(vp
);
3862 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3865 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs
);
3866 if (zp
->z_sa_hdl
== NULL
) {
3868 * The fs has been unmounted, or we did a
3869 * suspend/resume and this file no longer exists.
3871 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3876 if (zp
->z_unlinked
) {
3878 * Fast path to recycle a vnode of a removed file.
3880 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3885 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
3886 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
3888 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3889 zfs_sa_upgrade_txholds(tx
, zp
);
3890 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3894 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
3895 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
3896 zp
->z_atime_dirty
= 0;
3900 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3904 _Static_assert(sizeof (struct zfid_short
) <= sizeof (struct fid
),
3905 "struct zfid_short bigger than struct fid");
3906 _Static_assert(sizeof (struct zfid_long
) <= sizeof (struct fid
),
3907 "struct zfid_long bigger than struct fid");
3910 zfs_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
3913 znode_t
*zp
= VTOZ(vp
);
3914 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3917 uint64_t object
= zp
->z_id
;
3921 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3924 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
3925 &gen64
, sizeof (uint64_t))) != 0) {
3926 zfs_exit(zfsvfs
, FTAG
);
3930 gen
= (uint32_t)gen64
;
3932 size
= (zfsvfs
->z_parent
!= zfsvfs
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
3933 fidp
->fid_len
= size
;
3935 zfid
= (zfid_short_t
*)fidp
;
3937 zfid
->zf_len
= size
;
3939 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
3940 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
3942 /* Must have a non-zero generation number to distinguish from .zfs */
3945 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
3946 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
3948 if (size
== LONG_FID_LEN
) {
3949 uint64_t objsetid
= dmu_objset_id(zfsvfs
->z_os
);
3952 zlfid
= (zfid_long_t
*)fidp
;
3954 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
3955 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
3957 /* XXX - this should be the generation number for the objset */
3958 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
3959 zlfid
->zf_setgen
[i
] = 0;
3962 zfs_exit(zfsvfs
, FTAG
);
3967 zfs_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
3968 caller_context_t
*ct
)
3976 *valp
= MIN(LONG_MAX
, ZFS_LINK_MAX
);
3979 case _PC_FILESIZEBITS
:
3982 case _PC_MIN_HOLE_SIZE
:
3983 *valp
= (int)SPA_MINBLOCKSIZE
;
3985 case _PC_ACL_EXTENDED
:
3986 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
3988 zfsvfs
= zp
->z_zfsvfs
;
3989 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3991 *valp
= zfsvfs
->z_acl_type
== ZFSACLTYPE_POSIX
? 1 : 0;
3992 zfs_exit(zfsvfs
, FTAG
);
4000 zfsvfs
= zp
->z_zfsvfs
;
4001 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
4003 *valp
= zfsvfs
->z_acl_type
== ZFS_ACLTYPE_NFSV4
? 1 : 0;
4004 zfs_exit(zfsvfs
, FTAG
);
4007 case _PC_ACL_PATH_MAX
:
4008 *valp
= ACL_MAX_ENTRIES
;
4012 return (EOPNOTSUPP
);
4017 zfs_getpages(struct vnode
*vp
, vm_page_t
*ma
, int count
, int *rbehind
,
4020 znode_t
*zp
= VTOZ(vp
);
4021 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4022 zfs_locked_range_t
*lr
;
4024 off_t start
, end
, obj_size
;
4026 int pgsin_b
, pgsin_a
;
4029 if (zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
) != 0)
4030 return (zfs_vm_pagerret_error
);
4032 start
= IDX_TO_OFF(ma
[0]->pindex
);
4033 end
= IDX_TO_OFF(ma
[count
- 1]->pindex
+ 1);
4036 * Lock a range covering all required and optional pages.
4037 * Note that we need to handle the case of the block size growing.
4040 blksz
= zp
->z_blksz
;
4041 lr
= zfs_rangelock_tryenter(&zp
->z_rangelock
,
4042 rounddown(start
, blksz
),
4043 roundup(end
, blksz
) - rounddown(start
, blksz
), RL_READER
);
4045 if (rahead
!= NULL
) {
4049 if (rbehind
!= NULL
) {
4055 if (blksz
== zp
->z_blksz
)
4057 zfs_rangelock_exit(lr
);
4060 object
= ma
[0]->object
;
4061 zfs_vmobject_wlock(object
);
4062 obj_size
= object
->un_pager
.vnp
.vnp_size
;
4063 zfs_vmobject_wunlock(object
);
4064 if (IDX_TO_OFF(ma
[count
- 1]->pindex
) >= obj_size
) {
4066 zfs_rangelock_exit(lr
);
4067 zfs_exit(zfsvfs
, FTAG
);
4068 return (zfs_vm_pagerret_bad
);
4072 if (rbehind
!= NULL
) {
4073 pgsin_b
= OFF_TO_IDX(start
- rounddown(start
, blksz
));
4074 pgsin_b
= MIN(*rbehind
, pgsin_b
);
4078 if (rahead
!= NULL
) {
4079 pgsin_a
= OFF_TO_IDX(roundup(end
, blksz
) - end
);
4080 if (end
+ IDX_TO_OFF(pgsin_a
) >= obj_size
)
4081 pgsin_a
= OFF_TO_IDX(round_page(obj_size
) - end
);
4082 pgsin_a
= MIN(*rahead
, pgsin_a
);
4086 * NB: we need to pass the exact byte size of the data that we expect
4087 * to read after accounting for the file size. This is required because
4088 * ZFS will panic if we request DMU to read beyond the end of the last
4091 error
= dmu_read_pages(zfsvfs
->z_os
, zp
->z_id
, ma
, count
, &pgsin_b
,
4092 &pgsin_a
, MIN(end
, obj_size
) - (end
- PAGE_SIZE
));
4095 zfs_rangelock_exit(lr
);
4096 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
4098 dataset_kstats_update_read_kstats(&zfsvfs
->z_kstat
, count
*PAGE_SIZE
);
4100 zfs_exit(zfsvfs
, FTAG
);
4103 return (zfs_vm_pagerret_error
);
4105 VM_CNT_INC(v_vnodein
);
4106 VM_CNT_ADD(v_vnodepgsin
, count
+ pgsin_b
+ pgsin_a
);
4107 if (rbehind
!= NULL
)
4111 return (zfs_vm_pagerret_ok
);
4114 #ifndef _SYS_SYSPROTO_H_
4115 struct vop_getpages_args
{
4125 zfs_freebsd_getpages(struct vop_getpages_args
*ap
)
4128 return (zfs_getpages(ap
->a_vp
, ap
->a_m
, ap
->a_count
, ap
->a_rbehind
,
4133 zfs_putpages(struct vnode
*vp
, vm_page_t
*ma
, size_t len
, int flags
,
4136 znode_t
*zp
= VTOZ(vp
);
4137 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4138 zfs_locked_range_t
*lr
;
4146 vm_ooffset_t lo_off
;
4154 object
= vp
->v_object
;
4155 KASSERT(ma
[0]->object
== object
, ("mismatching object"));
4156 KASSERT(len
> 0 && (len
& PAGE_MASK
) == 0, ("unexpected length"));
4160 for (i
= 0; i
< pcount
; i
++)
4161 rtvals
[i
] = zfs_vm_pagerret_error
;
4163 if (zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
) != 0)
4164 return (zfs_vm_pagerret_error
);
4166 off
= IDX_TO_OFF(ma
[0]->pindex
);
4167 blksz
= zp
->z_blksz
;
4168 lo_off
= rounddown(off
, blksz
);
4169 lo_len
= roundup(len
+ (off
- lo_off
), blksz
);
4170 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, lo_off
, lo_len
, RL_WRITER
);
4172 zfs_vmobject_wlock(object
);
4173 if (len
+ off
> object
->un_pager
.vnp
.vnp_size
) {
4174 if (object
->un_pager
.vnp
.vnp_size
> off
) {
4177 len
= object
->un_pager
.vnp
.vnp_size
- off
;
4179 if ((pgoff
= (int)len
& PAGE_MASK
) != 0) {
4181 * If the object is locked and the following
4182 * conditions hold, then the page's dirty
4183 * field cannot be concurrently changed by a
4187 vm_page_assert_sbusied(m
);
4188 KASSERT(!pmap_page_is_write_mapped(m
),
4189 ("zfs_putpages: page %p is not read-only",
4191 vm_page_clear_dirty(m
, pgoff
, PAGE_SIZE
-
4198 if (ncount
< pcount
) {
4199 for (i
= ncount
; i
< pcount
; i
++) {
4200 rtvals
[i
] = zfs_vm_pagerret_bad
;
4204 zfs_vmobject_wunlock(object
);
4209 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
, zp
->z_uid
) ||
4210 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
, zp
->z_gid
) ||
4211 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
4212 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
4217 tx
= dmu_tx_create(zfsvfs
->z_os
);
4218 dmu_tx_hold_write(tx
, zp
->z_id
, off
, len
);
4220 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4221 zfs_sa_upgrade_txholds(tx
, zp
);
4222 err
= dmu_tx_assign(tx
, TXG_WAIT
);
4228 if (zp
->z_blksz
< PAGE_SIZE
) {
4229 for (i
= 0; len
> 0; off
+= tocopy
, len
-= tocopy
, i
++) {
4230 tocopy
= len
> PAGE_SIZE
? PAGE_SIZE
: len
;
4231 va
= zfs_map_page(ma
[i
], &sf
);
4232 dmu_write(zfsvfs
->z_os
, zp
->z_id
, off
, tocopy
, va
, tx
);
4236 err
= dmu_write_pages(zfsvfs
->z_os
, zp
->z_id
, off
, len
, ma
, tx
);
4240 uint64_t mtime
[2], ctime
[2];
4241 sa_bulk_attr_t bulk
[3];
4244 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
4246 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
4248 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
4250 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
4251 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
4254 * XXX we should be passing a callback to undirty
4255 * but that would make the locking messier
4257 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, off
,
4258 len
, 0, NULL
, NULL
);
4260 zfs_vmobject_wlock(object
);
4261 for (i
= 0; i
< ncount
; i
++) {
4262 rtvals
[i
] = zfs_vm_pagerret_ok
;
4263 vm_page_undirty(ma
[i
]);
4265 zfs_vmobject_wunlock(object
);
4266 VM_CNT_INC(v_vnodeout
);
4267 VM_CNT_ADD(v_vnodepgsout
, ncount
);
4272 zfs_rangelock_exit(lr
);
4273 if ((flags
& (zfs_vm_pagerput_sync
| zfs_vm_pagerput_inval
)) != 0 ||
4274 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4275 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
4277 dataset_kstats_update_write_kstats(&zfsvfs
->z_kstat
, len
);
4279 zfs_exit(zfsvfs
, FTAG
);
4283 #ifndef _SYS_SYSPROTO_H_
4284 struct vop_putpages_args
{
4294 zfs_freebsd_putpages(struct vop_putpages_args
*ap
)
4297 return (zfs_putpages(ap
->a_vp
, ap
->a_m
, ap
->a_count
, ap
->a_sync
,
4301 #ifndef _SYS_SYSPROTO_H_
4302 struct vop_bmap_args
{
4305 struct bufobj
**a_bop
;
4313 zfs_freebsd_bmap(struct vop_bmap_args
*ap
)
4316 if (ap
->a_bop
!= NULL
)
4317 *ap
->a_bop
= &ap
->a_vp
->v_bufobj
;
4318 if (ap
->a_bnp
!= NULL
)
4319 *ap
->a_bnp
= ap
->a_bn
;
4320 if (ap
->a_runp
!= NULL
)
4322 if (ap
->a_runb
!= NULL
)
4328 #ifndef _SYS_SYSPROTO_H_
4329 struct vop_open_args
{
4332 struct ucred
*a_cred
;
4333 struct thread
*a_td
;
4338 zfs_freebsd_open(struct vop_open_args
*ap
)
4340 vnode_t
*vp
= ap
->a_vp
;
4341 znode_t
*zp
= VTOZ(vp
);
4344 error
= zfs_open(&vp
, ap
->a_mode
, ap
->a_cred
);
4346 vnode_create_vobject(vp
, zp
->z_size
, ap
->a_td
);
4350 #ifndef _SYS_SYSPROTO_H_
4351 struct vop_close_args
{
4354 struct ucred
*a_cred
;
4355 struct thread
*a_td
;
4360 zfs_freebsd_close(struct vop_close_args
*ap
)
4363 return (zfs_close(ap
->a_vp
, ap
->a_fflag
, 1, 0, ap
->a_cred
));
4366 #ifndef _SYS_SYSPROTO_H_
4367 struct vop_ioctl_args
{
4378 zfs_freebsd_ioctl(struct vop_ioctl_args
*ap
)
4381 return (zfs_ioctl(ap
->a_vp
, ap
->a_command
, (intptr_t)ap
->a_data
,
4382 ap
->a_fflag
, ap
->a_cred
, NULL
));
4386 ioflags(int ioflags
)
4390 if (ioflags
& IO_APPEND
)
4392 if (ioflags
& IO_NDELAY
)
4393 flags
|= O_NONBLOCK
;
4394 if (ioflags
& IO_SYNC
)
4400 #ifndef _SYS_SYSPROTO_H_
4401 struct vop_read_args
{
4405 struct ucred
*a_cred
;
4410 zfs_freebsd_read(struct vop_read_args
*ap
)
4413 zfs_uio_init(&uio
, ap
->a_uio
);
4414 return (zfs_read(VTOZ(ap
->a_vp
), &uio
, ioflags(ap
->a_ioflag
),
4418 #ifndef _SYS_SYSPROTO_H_
4419 struct vop_write_args
{
4423 struct ucred
*a_cred
;
4428 zfs_freebsd_write(struct vop_write_args
*ap
)
4431 zfs_uio_init(&uio
, ap
->a_uio
);
4432 return (zfs_write(VTOZ(ap
->a_vp
), &uio
, ioflags(ap
->a_ioflag
),
4436 #if __FreeBSD_version >= 1300102
4438 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4439 * the comment above cache_fplookup for details.
4442 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args
*v
)
4450 if (__predict_false(zp
== NULL
))
4452 pflags
= atomic_load_64(&zp
->z_pflags
);
4453 if (pflags
& ZFS_AV_QUARANTINED
)
4455 if (pflags
& ZFS_XATTR
)
4457 if ((pflags
& ZFS_NO_EXECS_DENIED
) == 0)
4463 #if __FreeBSD_version >= 1300139
4465 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args
*v
)
4473 if (__predict_false(zp
== NULL
)) {
4477 target
= atomic_load_consume_ptr(&zp
->z_cached_symlink
);
4478 if (target
== NULL
) {
4481 return (cache_symlink_resolve(v
->a_fpl
, target
, strlen(target
)));
4485 #ifndef _SYS_SYSPROTO_H_
4486 struct vop_access_args
{
4488 accmode_t a_accmode
;
4489 struct ucred
*a_cred
;
4490 struct thread
*a_td
;
4495 zfs_freebsd_access(struct vop_access_args
*ap
)
4497 vnode_t
*vp
= ap
->a_vp
;
4498 znode_t
*zp
= VTOZ(vp
);
4503 if (ap
->a_accmode
== VEXEC
) {
4504 if (zfs_fastaccesschk_execute(zp
, ap
->a_cred
) == 0)
4509 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4511 accmode
= ap
->a_accmode
& (VREAD
|VWRITE
|VEXEC
|VAPPEND
);
4513 error
= zfs_access(zp
, accmode
, 0, ap
->a_cred
);
4516 * VADMIN has to be handled by vaccess().
4519 accmode
= ap
->a_accmode
& ~(VREAD
|VWRITE
|VEXEC
|VAPPEND
);
4521 #if __FreeBSD_version >= 1300105
4522 error
= vaccess(vp
->v_type
, zp
->z_mode
, zp
->z_uid
,
4523 zp
->z_gid
, accmode
, ap
->a_cred
);
4525 error
= vaccess(vp
->v_type
, zp
->z_mode
, zp
->z_uid
,
4526 zp
->z_gid
, accmode
, ap
->a_cred
, NULL
);
4532 * For VEXEC, ensure that at least one execute bit is set for
4535 if (error
== 0 && (ap
->a_accmode
& VEXEC
) != 0 && vp
->v_type
!= VDIR
&&
4536 (zp
->z_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0) {
4543 #ifndef _SYS_SYSPROTO_H_
4544 struct vop_lookup_args
{
4545 struct vnode
*a_dvp
;
4546 struct vnode
**a_vpp
;
4547 struct componentname
*a_cnp
;
4552 zfs_freebsd_lookup(struct vop_lookup_args
*ap
, boolean_t cached
)
4554 struct componentname
*cnp
= ap
->a_cnp
;
4555 char nm
[NAME_MAX
+ 1];
4557 ASSERT3U(cnp
->cn_namelen
, <, sizeof (nm
));
4558 strlcpy(nm
, cnp
->cn_nameptr
, MIN(cnp
->cn_namelen
+ 1, sizeof (nm
)));
4560 return (zfs_lookup(ap
->a_dvp
, nm
, ap
->a_vpp
, cnp
, cnp
->cn_nameiop
,
4561 cnp
->cn_cred
, 0, cached
));
4565 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args
*ap
)
4568 return (zfs_freebsd_lookup((struct vop_lookup_args
*)ap
, B_TRUE
));
4571 #ifndef _SYS_SYSPROTO_H_
4572 struct vop_lookup_args
{
4573 struct vnode
*a_dvp
;
4574 struct vnode
**a_vpp
;
4575 struct componentname
*a_cnp
;
4580 zfs_cache_lookup(struct vop_lookup_args
*ap
)
4584 zfsvfs
= ap
->a_dvp
->v_mount
->mnt_data
;
4585 if (zfsvfs
->z_use_namecache
)
4586 return (vfs_cache_lookup(ap
));
4588 return (zfs_freebsd_lookup(ap
, B_FALSE
));
4591 #ifndef _SYS_SYSPROTO_H_
4592 struct vop_create_args
{
4593 struct vnode
*a_dvp
;
4594 struct vnode
**a_vpp
;
4595 struct componentname
*a_cnp
;
4596 struct vattr
*a_vap
;
4601 zfs_freebsd_create(struct vop_create_args
*ap
)
4604 struct componentname
*cnp
= ap
->a_cnp
;
4605 vattr_t
*vap
= ap
->a_vap
;
4609 #if __FreeBSD_version < 1400068
4610 ASSERT(cnp
->cn_flags
& SAVENAME
);
4613 vattr_init_mask(vap
);
4614 mode
= vap
->va_mode
& ALLPERMS
;
4615 zfsvfs
= ap
->a_dvp
->v_mount
->mnt_data
;
4618 rc
= zfs_create(VTOZ(ap
->a_dvp
), cnp
->cn_nameptr
, vap
, 0, mode
,
4619 &zp
, cnp
->cn_cred
, 0 /* flag */, NULL
/* vsecattr */, NULL
);
4621 *ap
->a_vpp
= ZTOV(zp
);
4622 if (zfsvfs
->z_use_namecache
&&
4623 rc
== 0 && (cnp
->cn_flags
& MAKEENTRY
) != 0)
4624 cache_enter(ap
->a_dvp
, *ap
->a_vpp
, cnp
);
4629 #ifndef _SYS_SYSPROTO_H_
4630 struct vop_remove_args
{
4631 struct vnode
*a_dvp
;
4633 struct componentname
*a_cnp
;
4638 zfs_freebsd_remove(struct vop_remove_args
*ap
)
4641 #if __FreeBSD_version < 1400068
4642 ASSERT(ap
->a_cnp
->cn_flags
& SAVENAME
);
4645 return (zfs_remove_(ap
->a_dvp
, ap
->a_vp
, ap
->a_cnp
->cn_nameptr
,
4646 ap
->a_cnp
->cn_cred
));
4649 #ifndef _SYS_SYSPROTO_H_
4650 struct vop_mkdir_args
{
4651 struct vnode
*a_dvp
;
4652 struct vnode
**a_vpp
;
4653 struct componentname
*a_cnp
;
4654 struct vattr
*a_vap
;
4659 zfs_freebsd_mkdir(struct vop_mkdir_args
*ap
)
4661 vattr_t
*vap
= ap
->a_vap
;
4665 #if __FreeBSD_version < 1400068
4666 ASSERT(ap
->a_cnp
->cn_flags
& SAVENAME
);
4669 vattr_init_mask(vap
);
4672 rc
= zfs_mkdir(VTOZ(ap
->a_dvp
), ap
->a_cnp
->cn_nameptr
, vap
, &zp
,
4673 ap
->a_cnp
->cn_cred
, 0, NULL
, NULL
);
4676 *ap
->a_vpp
= ZTOV(zp
);
4680 #ifndef _SYS_SYSPROTO_H_
4681 struct vop_rmdir_args
{
4682 struct vnode
*a_dvp
;
4684 struct componentname
*a_cnp
;
4689 zfs_freebsd_rmdir(struct vop_rmdir_args
*ap
)
4691 struct componentname
*cnp
= ap
->a_cnp
;
4693 #if __FreeBSD_version < 1400068
4694 ASSERT(cnp
->cn_flags
& SAVENAME
);
4697 return (zfs_rmdir_(ap
->a_dvp
, ap
->a_vp
, cnp
->cn_nameptr
, cnp
->cn_cred
));
4700 #ifndef _SYS_SYSPROTO_H_
4701 struct vop_readdir_args
{
4704 struct ucred
*a_cred
;
4707 cookie_t
**a_cookies
;
4712 zfs_freebsd_readdir(struct vop_readdir_args
*ap
)
4715 zfs_uio_init(&uio
, ap
->a_uio
);
4716 return (zfs_readdir(ap
->a_vp
, &uio
, ap
->a_cred
, ap
->a_eofflag
,
4717 ap
->a_ncookies
, ap
->a_cookies
));
4720 #ifndef _SYS_SYSPROTO_H_
4721 struct vop_fsync_args
{
4724 struct thread
*a_td
;
4729 zfs_freebsd_fsync(struct vop_fsync_args
*ap
)
4732 return (zfs_fsync(VTOZ(ap
->a_vp
), 0, ap
->a_td
->td_ucred
));
4735 #ifndef _SYS_SYSPROTO_H_
4736 struct vop_getattr_args
{
4738 struct vattr
*a_vap
;
4739 struct ucred
*a_cred
;
4744 zfs_freebsd_getattr(struct vop_getattr_args
*ap
)
4746 vattr_t
*vap
= ap
->a_vap
;
4752 xvap
.xva_vattr
= *vap
;
4753 xvap
.xva_vattr
.va_mask
|= AT_XVATTR
;
4755 /* Convert chflags into ZFS-type flags. */
4756 /* XXX: what about SF_SETTABLE?. */
4757 XVA_SET_REQ(&xvap
, XAT_IMMUTABLE
);
4758 XVA_SET_REQ(&xvap
, XAT_APPENDONLY
);
4759 XVA_SET_REQ(&xvap
, XAT_NOUNLINK
);
4760 XVA_SET_REQ(&xvap
, XAT_NODUMP
);
4761 XVA_SET_REQ(&xvap
, XAT_READONLY
);
4762 XVA_SET_REQ(&xvap
, XAT_ARCHIVE
);
4763 XVA_SET_REQ(&xvap
, XAT_SYSTEM
);
4764 XVA_SET_REQ(&xvap
, XAT_HIDDEN
);
4765 XVA_SET_REQ(&xvap
, XAT_REPARSE
);
4766 XVA_SET_REQ(&xvap
, XAT_OFFLINE
);
4767 XVA_SET_REQ(&xvap
, XAT_SPARSE
);
4769 error
= zfs_getattr(ap
->a_vp
, (vattr_t
*)&xvap
, 0, ap
->a_cred
);
4773 /* Convert ZFS xattr into chflags. */
4774 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4775 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4776 fflags |= (fflag); \
4778 FLAG_CHECK(SF_IMMUTABLE
, XAT_IMMUTABLE
,
4779 xvap
.xva_xoptattrs
.xoa_immutable
);
4780 FLAG_CHECK(SF_APPEND
, XAT_APPENDONLY
,
4781 xvap
.xva_xoptattrs
.xoa_appendonly
);
4782 FLAG_CHECK(SF_NOUNLINK
, XAT_NOUNLINK
,
4783 xvap
.xva_xoptattrs
.xoa_nounlink
);
4784 FLAG_CHECK(UF_ARCHIVE
, XAT_ARCHIVE
,
4785 xvap
.xva_xoptattrs
.xoa_archive
);
4786 FLAG_CHECK(UF_NODUMP
, XAT_NODUMP
,
4787 xvap
.xva_xoptattrs
.xoa_nodump
);
4788 FLAG_CHECK(UF_READONLY
, XAT_READONLY
,
4789 xvap
.xva_xoptattrs
.xoa_readonly
);
4790 FLAG_CHECK(UF_SYSTEM
, XAT_SYSTEM
,
4791 xvap
.xva_xoptattrs
.xoa_system
);
4792 FLAG_CHECK(UF_HIDDEN
, XAT_HIDDEN
,
4793 xvap
.xva_xoptattrs
.xoa_hidden
);
4794 FLAG_CHECK(UF_REPARSE
, XAT_REPARSE
,
4795 xvap
.xva_xoptattrs
.xoa_reparse
);
4796 FLAG_CHECK(UF_OFFLINE
, XAT_OFFLINE
,
4797 xvap
.xva_xoptattrs
.xoa_offline
);
4798 FLAG_CHECK(UF_SPARSE
, XAT_SPARSE
,
4799 xvap
.xva_xoptattrs
.xoa_sparse
);
4802 *vap
= xvap
.xva_vattr
;
4803 vap
->va_flags
= fflags
;
4807 #ifndef _SYS_SYSPROTO_H_
4808 struct vop_setattr_args
{
4810 struct vattr
*a_vap
;
4811 struct ucred
*a_cred
;
4816 zfs_freebsd_setattr(struct vop_setattr_args
*ap
)
4818 vnode_t
*vp
= ap
->a_vp
;
4819 vattr_t
*vap
= ap
->a_vap
;
4820 cred_t
*cred
= ap
->a_cred
;
4825 vattr_init_mask(vap
);
4826 vap
->va_mask
&= ~AT_NOSET
;
4829 xvap
.xva_vattr
= *vap
;
4831 zflags
= VTOZ(vp
)->z_pflags
;
4833 if (vap
->va_flags
!= VNOVAL
) {
4834 zfsvfs_t
*zfsvfs
= VTOZ(vp
)->z_zfsvfs
;
4837 if (zfsvfs
->z_use_fuids
== B_FALSE
)
4838 return (EOPNOTSUPP
);
4840 fflags
= vap
->va_flags
;
4843 * We need to figure out whether it makes sense to allow
4844 * UF_REPARSE through, since we don't really have other
4845 * facilities to handle reparse points and zfs_setattr()
4846 * doesn't currently allow setting that attribute anyway.
4848 if ((fflags
& ~(SF_IMMUTABLE
|SF_APPEND
|SF_NOUNLINK
|UF_ARCHIVE
|
4849 UF_NODUMP
|UF_SYSTEM
|UF_HIDDEN
|UF_READONLY
|UF_REPARSE
|
4850 UF_OFFLINE
|UF_SPARSE
)) != 0)
4851 return (EOPNOTSUPP
);
4853 * Unprivileged processes are not permitted to unset system
4854 * flags, or modify flags if any system flags are set.
4855 * Privileged non-jail processes may not modify system flags
4856 * if securelevel > 0 and any existing system flags are set.
4857 * Privileged jail processes behave like privileged non-jail
4858 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
4859 * otherwise, they behave like unprivileged processes.
4861 if (secpolicy_fs_owner(vp
->v_mount
, cred
) == 0 ||
4862 spl_priv_check_cred(cred
, PRIV_VFS_SYSFLAGS
) == 0) {
4864 (ZFS_IMMUTABLE
| ZFS_APPENDONLY
| ZFS_NOUNLINK
)) {
4865 error
= securelevel_gt(cred
, 0);
4871 * Callers may only modify the file flags on
4872 * objects they have VADMIN rights for.
4874 if ((error
= VOP_ACCESS(vp
, VADMIN
, cred
,
4878 (ZFS_IMMUTABLE
| ZFS_APPENDONLY
|
4883 (SF_IMMUTABLE
| SF_APPEND
| SF_NOUNLINK
)) {
4888 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4889 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4890 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4891 XVA_SET_REQ(&xvap, (xflag)); \
4892 (xfield) = ((fflags & (fflag)) != 0); \
4895 /* Convert chflags into ZFS-type flags. */
4896 /* XXX: what about SF_SETTABLE?. */
4897 FLAG_CHANGE(SF_IMMUTABLE
, ZFS_IMMUTABLE
, XAT_IMMUTABLE
,
4898 xvap
.xva_xoptattrs
.xoa_immutable
);
4899 FLAG_CHANGE(SF_APPEND
, ZFS_APPENDONLY
, XAT_APPENDONLY
,
4900 xvap
.xva_xoptattrs
.xoa_appendonly
);
4901 FLAG_CHANGE(SF_NOUNLINK
, ZFS_NOUNLINK
, XAT_NOUNLINK
,
4902 xvap
.xva_xoptattrs
.xoa_nounlink
);
4903 FLAG_CHANGE(UF_ARCHIVE
, ZFS_ARCHIVE
, XAT_ARCHIVE
,
4904 xvap
.xva_xoptattrs
.xoa_archive
);
4905 FLAG_CHANGE(UF_NODUMP
, ZFS_NODUMP
, XAT_NODUMP
,
4906 xvap
.xva_xoptattrs
.xoa_nodump
);
4907 FLAG_CHANGE(UF_READONLY
, ZFS_READONLY
, XAT_READONLY
,
4908 xvap
.xva_xoptattrs
.xoa_readonly
);
4909 FLAG_CHANGE(UF_SYSTEM
, ZFS_SYSTEM
, XAT_SYSTEM
,
4910 xvap
.xva_xoptattrs
.xoa_system
);
4911 FLAG_CHANGE(UF_HIDDEN
, ZFS_HIDDEN
, XAT_HIDDEN
,
4912 xvap
.xva_xoptattrs
.xoa_hidden
);
4913 FLAG_CHANGE(UF_REPARSE
, ZFS_REPARSE
, XAT_REPARSE
,
4914 xvap
.xva_xoptattrs
.xoa_reparse
);
4915 FLAG_CHANGE(UF_OFFLINE
, ZFS_OFFLINE
, XAT_OFFLINE
,
4916 xvap
.xva_xoptattrs
.xoa_offline
);
4917 FLAG_CHANGE(UF_SPARSE
, ZFS_SPARSE
, XAT_SPARSE
,
4918 xvap
.xva_xoptattrs
.xoa_sparse
);
4921 if (vap
->va_birthtime
.tv_sec
!= VNOVAL
) {
4922 xvap
.xva_vattr
.va_mask
|= AT_XVATTR
;
4923 XVA_SET_REQ(&xvap
, XAT_CREATETIME
);
4925 return (zfs_setattr(VTOZ(vp
), (vattr_t
*)&xvap
, 0, cred
, NULL
));
4928 #ifndef _SYS_SYSPROTO_H_
4929 struct vop_rename_args
{
4930 struct vnode
*a_fdvp
;
4931 struct vnode
*a_fvp
;
4932 struct componentname
*a_fcnp
;
4933 struct vnode
*a_tdvp
;
4934 struct vnode
*a_tvp
;
4935 struct componentname
*a_tcnp
;
4940 zfs_freebsd_rename(struct vop_rename_args
*ap
)
4942 vnode_t
*fdvp
= ap
->a_fdvp
;
4943 vnode_t
*fvp
= ap
->a_fvp
;
4944 vnode_t
*tdvp
= ap
->a_tdvp
;
4945 vnode_t
*tvp
= ap
->a_tvp
;
4948 #if __FreeBSD_version < 1400068
4949 ASSERT(ap
->a_fcnp
->cn_flags
& (SAVENAME
|SAVESTART
));
4950 ASSERT(ap
->a_tcnp
->cn_flags
& (SAVENAME
|SAVESTART
));
4953 error
= zfs_do_rename(fdvp
, &fvp
, ap
->a_fcnp
, tdvp
, &tvp
,
4954 ap
->a_tcnp
, ap
->a_fcnp
->cn_cred
);
4965 #ifndef _SYS_SYSPROTO_H_
4966 struct vop_symlink_args
{
4967 struct vnode
*a_dvp
;
4968 struct vnode
**a_vpp
;
4969 struct componentname
*a_cnp
;
4970 struct vattr
*a_vap
;
4976 zfs_freebsd_symlink(struct vop_symlink_args
*ap
)
4978 struct componentname
*cnp
= ap
->a_cnp
;
4979 vattr_t
*vap
= ap
->a_vap
;
4981 #if __FreeBSD_version >= 1300139
4987 #if __FreeBSD_version < 1400068
4988 ASSERT(cnp
->cn_flags
& SAVENAME
);
4991 vap
->va_type
= VLNK
; /* FreeBSD: Syscall only sets va_mode. */
4992 vattr_init_mask(vap
);
4995 rc
= zfs_symlink(VTOZ(ap
->a_dvp
), cnp
->cn_nameptr
, vap
,
4996 ap
->a_target
, &zp
, cnp
->cn_cred
, 0 /* flags */, NULL
);
4998 *ap
->a_vpp
= ZTOV(zp
);
4999 ASSERT_VOP_ELOCKED(ZTOV(zp
), __func__
);
5000 #if __FreeBSD_version >= 1300139
5001 MPASS(zp
->z_cached_symlink
== NULL
);
5002 symlink_len
= strlen(ap
->a_target
);
5003 symlink
= cache_symlink_alloc(symlink_len
+ 1, M_WAITOK
);
5004 if (symlink
!= NULL
) {
5005 memcpy(symlink
, ap
->a_target
, symlink_len
);
5006 symlink
[symlink_len
] = '\0';
5007 atomic_store_rel_ptr((uintptr_t *)&zp
->z_cached_symlink
,
5008 (uintptr_t)symlink
);
5015 #ifndef _SYS_SYSPROTO_H_
5016 struct vop_readlink_args
{
5019 struct ucred
*a_cred
;
5024 zfs_freebsd_readlink(struct vop_readlink_args
*ap
)
5028 #if __FreeBSD_version >= 1300139
5029 znode_t
*zp
= VTOZ(ap
->a_vp
);
5030 char *symlink
, *base
;
5035 zfs_uio_init(&uio
, ap
->a_uio
);
5036 #if __FreeBSD_version >= 1300139
5038 if (zfs_uio_segflg(&uio
) == UIO_SYSSPACE
&&
5039 zfs_uio_iovcnt(&uio
) == 1) {
5040 base
= zfs_uio_iovbase(&uio
, 0);
5041 symlink_len
= zfs_uio_iovlen(&uio
, 0);
5045 error
= zfs_readlink(ap
->a_vp
, &uio
, ap
->a_cred
, NULL
);
5046 #if __FreeBSD_version >= 1300139
5047 if (atomic_load_ptr(&zp
->z_cached_symlink
) != NULL
||
5048 error
!= 0 || !trycache
) {
5051 symlink_len
-= zfs_uio_resid(&uio
);
5052 symlink
= cache_symlink_alloc(symlink_len
+ 1, M_WAITOK
);
5053 if (symlink
!= NULL
) {
5054 memcpy(symlink
, base
, symlink_len
);
5055 symlink
[symlink_len
] = '\0';
5056 if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp
->z_cached_symlink
,
5057 (uintptr_t)NULL
, (uintptr_t)symlink
)) {
5058 cache_symlink_free(symlink
, symlink_len
+ 1);
5065 #ifndef _SYS_SYSPROTO_H_
5066 struct vop_link_args
{
5067 struct vnode
*a_tdvp
;
5069 struct componentname
*a_cnp
;
5074 zfs_freebsd_link(struct vop_link_args
*ap
)
5076 struct componentname
*cnp
= ap
->a_cnp
;
5077 vnode_t
*vp
= ap
->a_vp
;
5078 vnode_t
*tdvp
= ap
->a_tdvp
;
5080 if (tdvp
->v_mount
!= vp
->v_mount
)
5083 #if __FreeBSD_version < 1400068
5084 ASSERT(cnp
->cn_flags
& SAVENAME
);
5087 return (zfs_link(VTOZ(tdvp
), VTOZ(vp
),
5088 cnp
->cn_nameptr
, cnp
->cn_cred
, 0));
5091 #ifndef _SYS_SYSPROTO_H_
5092 struct vop_inactive_args
{
5094 struct thread
*a_td
;
5099 zfs_freebsd_inactive(struct vop_inactive_args
*ap
)
5101 vnode_t
*vp
= ap
->a_vp
;
5103 #if __FreeBSD_version >= 1300123
5104 zfs_inactive(vp
, curthread
->td_ucred
, NULL
);
5106 zfs_inactive(vp
, ap
->a_td
->td_ucred
, NULL
);
5111 #if __FreeBSD_version >= 1300042
5112 #ifndef _SYS_SYSPROTO_H_
5113 struct vop_need_inactive_args
{
5115 struct thread
*a_td
;
5120 zfs_freebsd_need_inactive(struct vop_need_inactive_args
*ap
)
5122 vnode_t
*vp
= ap
->a_vp
;
5123 znode_t
*zp
= VTOZ(vp
);
5124 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
5127 if (vn_need_pageq_flush(vp
))
5130 if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs
))
5132 need
= (zp
->z_sa_hdl
== NULL
|| zp
->z_unlinked
|| zp
->z_atime_dirty
);
5133 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
5139 #ifndef _SYS_SYSPROTO_H_
5140 struct vop_reclaim_args
{
5142 struct thread
*a_td
;
5147 zfs_freebsd_reclaim(struct vop_reclaim_args
*ap
)
5149 vnode_t
*vp
= ap
->a_vp
;
5150 znode_t
*zp
= VTOZ(vp
);
5151 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
5153 ASSERT3P(zp
, !=, NULL
);
5155 #if __FreeBSD_version < 1300042
5156 /* Destroy the vm object and flush associated pages. */
5157 vnode_destroy_vobject(vp
);
5160 * z_teardown_inactive_lock protects from a race with
5161 * zfs_znode_dmu_fini in zfsvfs_teardown during
5164 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs
);
5165 if (zp
->z_sa_hdl
== NULL
)
5169 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
5175 #ifndef _SYS_SYSPROTO_H_
5176 struct vop_fid_args
{
5183 zfs_freebsd_fid(struct vop_fid_args
*ap
)
5186 return (zfs_fid(ap
->a_vp
, (void *)ap
->a_fid
, NULL
));
5190 #ifndef _SYS_SYSPROTO_H_
5191 struct vop_pathconf_args
{
5194 register_t
*a_retval
;
5199 zfs_freebsd_pathconf(struct vop_pathconf_args
*ap
)
5204 error
= zfs_pathconf(ap
->a_vp
, ap
->a_name
, &val
,
5205 curthread
->td_ucred
, NULL
);
5207 *ap
->a_retval
= val
;
5210 if (error
!= EOPNOTSUPP
)
5213 switch (ap
->a_name
) {
5215 *ap
->a_retval
= NAME_MAX
;
5217 #if __FreeBSD_version >= 1400032
5218 case _PC_DEALLOC_PRESENT
:
5223 if (ap
->a_vp
->v_type
== VDIR
|| ap
->a_vp
->v_type
== VFIFO
) {
5224 *ap
->a_retval
= PIPE_BUF
;
5229 return (vop_stdpathconf(ap
));
5233 static int zfs_xattr_compat
= 1;
5236 zfs_check_attrname(const char *name
)
5238 /* We don't allow '/' character in attribute name. */
5239 if (strchr(name
, '/') != NULL
)
5240 return (SET_ERROR(EINVAL
));
5241 /* We don't allow attribute names that start with a namespace prefix. */
5242 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name
))
5243 return (SET_ERROR(EINVAL
));
5248 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5249 * extended attribute name:
5251 * NAMESPACE XATTR_COMPAT PREFIX
5252 * system * freebsd:system:
5253 * user 1 (none, can be used to access ZFS
5254 * fsattr(5) attributes created on Solaris)
5258 zfs_create_attrname(int attrnamespace
, const char *name
, char *attrname
,
5259 size_t size
, boolean_t compat
)
5261 const char *namespace, *prefix
, *suffix
;
5263 memset(attrname
, 0, size
);
5265 switch (attrnamespace
) {
5266 case EXTATTR_NAMESPACE_USER
:
5269 * This is the default namespace by which we can access
5270 * all attributes created on Solaris.
5272 prefix
= namespace = suffix
= "";
5275 * This is compatible with the user namespace encoding
5276 * on Linux prior to xattr_compat, but nothing
5284 case EXTATTR_NAMESPACE_SYSTEM
:
5285 prefix
= "freebsd:";
5286 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING
;
5289 case EXTATTR_NAMESPACE_EMPTY
:
5291 return (SET_ERROR(EINVAL
));
5293 if (snprintf(attrname
, size
, "%s%s%s%s", prefix
, namespace, suffix
,
5295 return (SET_ERROR(ENAMETOOLONG
));
5301 zfs_ensure_xattr_cached(znode_t
*zp
)
5305 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
5307 if (zp
->z_xattr_cached
!= NULL
)
5310 if (rw_write_held(&zp
->z_xattr_lock
))
5311 return (zfs_sa_get_xattr(zp
));
5313 if (!rw_tryupgrade(&zp
->z_xattr_lock
)) {
5314 rw_exit(&zp
->z_xattr_lock
);
5315 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
5317 if (zp
->z_xattr_cached
== NULL
)
5318 error
= zfs_sa_get_xattr(zp
);
5319 rw_downgrade(&zp
->z_xattr_lock
);
5323 #ifndef _SYS_SYSPROTO_H_
5324 struct vop_getextattr
{
5325 IN
struct vnode
*a_vp
;
5326 IN
int a_attrnamespace
;
5327 IN
const char *a_name
;
5328 INOUT
struct uio
*a_uio
;
5330 IN
struct ucred
*a_cred
;
5331 IN
struct thread
*a_td
;
5336 zfs_getextattr_dir(struct vop_getextattr_args
*ap
, const char *attrname
)
5338 struct thread
*td
= ap
->a_td
;
5339 struct nameidata nd
;
5341 vnode_t
*xvp
= NULL
, *vp
;
5344 error
= zfs_lookup(ap
->a_vp
, NULL
, &xvp
, NULL
, 0, ap
->a_cred
,
5345 LOOKUP_XATTR
, B_FALSE
);
5350 #if __FreeBSD_version < 1400043
5351 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, attrname
,
5354 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, attrname
, xvp
);
5356 error
= vn_open_cred(&nd
, &flags
, 0, VN_OPEN_INVFS
, ap
->a_cred
, NULL
);
5358 return (SET_ERROR(error
));
5362 if (ap
->a_size
!= NULL
) {
5363 error
= VOP_GETATTR(vp
, &va
, ap
->a_cred
);
5365 *ap
->a_size
= (size_t)va
.va_size
;
5366 } else if (ap
->a_uio
!= NULL
)
5367 error
= VOP_READ(vp
, ap
->a_uio
, IO_UNIT
, ap
->a_cred
);
5370 vn_close(vp
, flags
, ap
->a_cred
, td
);
5375 zfs_getextattr_sa(struct vop_getextattr_args
*ap
, const char *attrname
)
5377 znode_t
*zp
= VTOZ(ap
->a_vp
);
5382 error
= zfs_ensure_xattr_cached(zp
);
5386 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
5387 ASSERT3P(zp
->z_xattr_cached
, !=, NULL
);
5389 error
= nvlist_lookup_byte_array(zp
->z_xattr_cached
, attrname
,
5390 &nv_value
, &nv_size
);
5392 return (SET_ERROR(error
));
5394 if (ap
->a_size
!= NULL
)
5395 *ap
->a_size
= nv_size
;
5396 else if (ap
->a_uio
!= NULL
)
5397 error
= uiomove(nv_value
, nv_size
, ap
->a_uio
);
5399 return (SET_ERROR(error
));
5405 zfs_getextattr_impl(struct vop_getextattr_args
*ap
, boolean_t compat
)
5407 znode_t
*zp
= VTOZ(ap
->a_vp
);
5408 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5409 char attrname
[EXTATTR_MAXNAMELEN
+1];
5412 error
= zfs_create_attrname(ap
->a_attrnamespace
, ap
->a_name
, attrname
,
5413 sizeof (attrname
), compat
);
5418 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
)
5419 error
= zfs_getextattr_sa(ap
, attrname
);
5420 if (error
== ENOENT
)
5421 error
= zfs_getextattr_dir(ap
, attrname
);
5426 * Vnode operation to retrieve a named extended attribute.
5429 zfs_getextattr(struct vop_getextattr_args
*ap
)
5431 znode_t
*zp
= VTOZ(ap
->a_vp
);
5432 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5436 * If the xattr property is off, refuse the request.
5438 if (!(zfsvfs
->z_flags
& ZSB_XATTR
))
5439 return (SET_ERROR(EOPNOTSUPP
));
5441 error
= extattr_check_cred(ap
->a_vp
, ap
->a_attrnamespace
,
5442 ap
->a_cred
, ap
->a_td
, VREAD
);
5444 return (SET_ERROR(error
));
5446 error
= zfs_check_attrname(ap
->a_name
);
5450 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
5453 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
5455 error
= zfs_getextattr_impl(ap
, zfs_xattr_compat
);
5456 if ((error
== ENOENT
|| error
== ENOATTR
) &&
5457 ap
->a_attrnamespace
== EXTATTR_NAMESPACE_USER
) {
5459 * Fall back to the alternate namespace format if we failed to
5460 * find a user xattr.
5462 error
= zfs_getextattr_impl(ap
, !zfs_xattr_compat
);
5465 rw_exit(&zp
->z_xattr_lock
);
5466 zfs_exit(zfsvfs
, FTAG
);
5467 if (error
== ENOENT
)
5468 error
= SET_ERROR(ENOATTR
);
5472 #ifndef _SYS_SYSPROTO_H_
5473 struct vop_deleteextattr
{
5474 IN
struct vnode
*a_vp
;
5475 IN
int a_attrnamespace
;
5476 IN
const char *a_name
;
5477 IN
struct ucred
*a_cred
;
5478 IN
struct thread
*a_td
;
5483 zfs_deleteextattr_dir(struct vop_deleteextattr_args
*ap
, const char *attrname
)
5485 struct nameidata nd
;
5486 vnode_t
*xvp
= NULL
, *vp
;
5489 error
= zfs_lookup(ap
->a_vp
, NULL
, &xvp
, NULL
, 0, ap
->a_cred
,
5490 LOOKUP_XATTR
, B_FALSE
);
5494 #if __FreeBSD_version < 1400043
5495 NDINIT_ATVP(&nd
, DELETE
, NOFOLLOW
| LOCKPARENT
| LOCKLEAF
,
5496 UIO_SYSSPACE
, attrname
, xvp
, ap
->a_td
);
5498 NDINIT_ATVP(&nd
, DELETE
, NOFOLLOW
| LOCKPARENT
| LOCKLEAF
,
5499 UIO_SYSSPACE
, attrname
, xvp
);
5503 return (SET_ERROR(error
));
5506 error
= VOP_REMOVE(nd
.ni_dvp
, vp
, &nd
.ni_cnd
);
5510 if (vp
== nd
.ni_dvp
)
5519 zfs_deleteextattr_sa(struct vop_deleteextattr_args
*ap
, const char *attrname
)
5521 znode_t
*zp
= VTOZ(ap
->a_vp
);
5525 error
= zfs_ensure_xattr_cached(zp
);
5529 ASSERT(RW_WRITE_HELD(&zp
->z_xattr_lock
));
5530 ASSERT3P(zp
->z_xattr_cached
, !=, NULL
);
5532 nvl
= zp
->z_xattr_cached
;
5533 error
= nvlist_remove(nvl
, attrname
, DATA_TYPE_BYTE_ARRAY
);
5535 error
= SET_ERROR(error
);
5537 error
= zfs_sa_set_xattr(zp
, attrname
, NULL
, 0);
5539 zp
->z_xattr_cached
= NULL
;
5546 zfs_deleteextattr_impl(struct vop_deleteextattr_args
*ap
, boolean_t compat
)
5548 znode_t
*zp
= VTOZ(ap
->a_vp
);
5549 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5550 char attrname
[EXTATTR_MAXNAMELEN
+1];
5553 error
= zfs_create_attrname(ap
->a_attrnamespace
, ap
->a_name
, attrname
,
5554 sizeof (attrname
), compat
);
5559 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
)
5560 error
= zfs_deleteextattr_sa(ap
, attrname
);
5561 if (error
== ENOENT
)
5562 error
= zfs_deleteextattr_dir(ap
, attrname
);
5567 * Vnode operation to remove a named attribute.
5570 zfs_deleteextattr(struct vop_deleteextattr_args
*ap
)
5572 znode_t
*zp
= VTOZ(ap
->a_vp
);
5573 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5577 * If the xattr property is off, refuse the request.
5579 if (!(zfsvfs
->z_flags
& ZSB_XATTR
))
5580 return (SET_ERROR(EOPNOTSUPP
));
5582 error
= extattr_check_cred(ap
->a_vp
, ap
->a_attrnamespace
,
5583 ap
->a_cred
, ap
->a_td
, VWRITE
);
5585 return (SET_ERROR(error
));
5587 error
= zfs_check_attrname(ap
->a_name
);
5591 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
5593 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
5595 error
= zfs_deleteextattr_impl(ap
, zfs_xattr_compat
);
5596 if ((error
== ENOENT
|| error
== ENOATTR
) &&
5597 ap
->a_attrnamespace
== EXTATTR_NAMESPACE_USER
) {
5599 * Fall back to the alternate namespace format if we failed to
5600 * find a user xattr.
5602 error
= zfs_deleteextattr_impl(ap
, !zfs_xattr_compat
);
5605 rw_exit(&zp
->z_xattr_lock
);
5606 zfs_exit(zfsvfs
, FTAG
);
5607 if (error
== ENOENT
)
5608 error
= SET_ERROR(ENOATTR
);
5612 #ifndef _SYS_SYSPROTO_H_
5613 struct vop_setextattr
{
5614 IN
struct vnode
*a_vp
;
5615 IN
int a_attrnamespace
;
5616 IN
const char *a_name
;
5617 INOUT
struct uio
*a_uio
;
5618 IN
struct ucred
*a_cred
;
5619 IN
struct thread
*a_td
;
5624 zfs_setextattr_dir(struct vop_setextattr_args
*ap
, const char *attrname
)
5626 struct thread
*td
= ap
->a_td
;
5627 struct nameidata nd
;
5629 vnode_t
*xvp
= NULL
, *vp
;
5632 error
= zfs_lookup(ap
->a_vp
, NULL
, &xvp
, NULL
, 0, ap
->a_cred
,
5633 LOOKUP_XATTR
| CREATE_XATTR_DIR
, B_FALSE
);
5637 flags
= FFLAGS(O_WRONLY
| O_CREAT
);
5638 #if __FreeBSD_version < 1400043
5639 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, attrname
, xvp
, td
);
5641 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, attrname
, xvp
);
5643 error
= vn_open_cred(&nd
, &flags
, 0600, VN_OPEN_INVFS
, ap
->a_cred
,
5646 return (SET_ERROR(error
));
5652 error
= VOP_SETATTR(vp
, &va
, ap
->a_cred
);
5654 VOP_WRITE(vp
, ap
->a_uio
, IO_UNIT
, ap
->a_cred
);
5657 vn_close(vp
, flags
, ap
->a_cred
, td
);
5662 zfs_setextattr_sa(struct vop_setextattr_args
*ap
, const char *attrname
)
5664 znode_t
*zp
= VTOZ(ap
->a_vp
);
5669 error
= zfs_ensure_xattr_cached(zp
);
5673 ASSERT(RW_WRITE_HELD(&zp
->z_xattr_lock
));
5674 ASSERT3P(zp
->z_xattr_cached
, !=, NULL
);
5676 nvl
= zp
->z_xattr_cached
;
5677 size_t entry_size
= ap
->a_uio
->uio_resid
;
5678 if (entry_size
> DXATTR_MAX_ENTRY_SIZE
)
5679 return (SET_ERROR(EFBIG
));
5680 error
= nvlist_size(nvl
, &sa_size
, NV_ENCODE_XDR
);
5682 return (SET_ERROR(error
));
5683 if (sa_size
> DXATTR_MAX_SA_SIZE
)
5684 return (SET_ERROR(EFBIG
));
5685 uchar_t
*buf
= kmem_alloc(entry_size
, KM_SLEEP
);
5686 error
= uiomove(buf
, entry_size
, ap
->a_uio
);
5688 error
= SET_ERROR(error
);
5690 error
= nvlist_add_byte_array(nvl
, attrname
, buf
, entry_size
);
5692 error
= SET_ERROR(error
);
5695 error
= zfs_sa_set_xattr(zp
, attrname
, buf
, entry_size
);
5696 kmem_free(buf
, entry_size
);
5698 zp
->z_xattr_cached
= NULL
;
5705 zfs_setextattr_impl(struct vop_setextattr_args
*ap
, boolean_t compat
)
5707 znode_t
*zp
= VTOZ(ap
->a_vp
);
5708 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5709 char attrname
[EXTATTR_MAXNAMELEN
+1];
5712 error
= zfs_create_attrname(ap
->a_attrnamespace
, ap
->a_name
, attrname
,
5713 sizeof (attrname
), compat
);
5717 struct vop_deleteextattr_args vda
= {
5719 .a_attrnamespace
= ap
->a_attrnamespace
,
5720 .a_name
= ap
->a_name
,
5721 .a_cred
= ap
->a_cred
,
5725 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
&& zfsvfs
->z_xattr_sa
) {
5726 error
= zfs_setextattr_sa(ap
, attrname
);
5729 * Successfully put into SA, we need to clear the one
5730 * in dir if present.
5732 zfs_deleteextattr_dir(&vda
, attrname
);
5736 error
= zfs_setextattr_dir(ap
, attrname
);
5737 if (error
== 0 && zp
->z_is_sa
) {
5739 * Successfully put into dir, we need to clear the one
5742 zfs_deleteextattr_sa(&vda
, attrname
);
5745 if (error
== 0 && ap
->a_attrnamespace
== EXTATTR_NAMESPACE_USER
) {
5747 * Also clear all versions of the alternate compat name.
5749 zfs_deleteextattr_impl(&vda
, !compat
);
5755 * Vnode operation to set a named attribute.
5758 zfs_setextattr(struct vop_setextattr_args
*ap
)
5760 znode_t
*zp
= VTOZ(ap
->a_vp
);
5761 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5765 * If the xattr property is off, refuse the request.
5767 if (!(zfsvfs
->z_flags
& ZSB_XATTR
))
5768 return (SET_ERROR(EOPNOTSUPP
));
5770 error
= extattr_check_cred(ap
->a_vp
, ap
->a_attrnamespace
,
5771 ap
->a_cred
, ap
->a_td
, VWRITE
);
5773 return (SET_ERROR(error
));
5775 error
= zfs_check_attrname(ap
->a_name
);
5779 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
5781 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
5783 error
= zfs_setextattr_impl(ap
, zfs_xattr_compat
);
5785 rw_exit(&zp
->z_xattr_lock
);
5786 zfs_exit(zfsvfs
, FTAG
);
5790 #ifndef _SYS_SYSPROTO_H_
5791 struct vop_listextattr
{
5792 IN
struct vnode
*a_vp
;
5793 IN
int a_attrnamespace
;
5794 INOUT
struct uio
*a_uio
;
5796 IN
struct ucred
*a_cred
;
5797 IN
struct thread
*a_td
;
5802 zfs_listextattr_dir(struct vop_listextattr_args
*ap
, const char *attrprefix
)
5804 struct thread
*td
= ap
->a_td
;
5805 struct nameidata nd
;
5806 uint8_t dirbuf
[sizeof (struct dirent
)];
5809 vnode_t
*xvp
= NULL
, *vp
;
5812 error
= zfs_lookup(ap
->a_vp
, NULL
, &xvp
, NULL
, 0, ap
->a_cred
,
5813 LOOKUP_XATTR
, B_FALSE
);
5816 * ENOATTR means that the EA directory does not yet exist,
5817 * i.e. there are no extended attributes there.
5819 if (error
== ENOATTR
)
5824 #if __FreeBSD_version < 1400043
5825 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
| LOCKLEAF
| LOCKSHARED
,
5826 UIO_SYSSPACE
, ".", xvp
, td
);
5828 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
| LOCKLEAF
| LOCKSHARED
,
5829 UIO_SYSSPACE
, ".", xvp
);
5833 return (SET_ERROR(error
));
5837 auio
.uio_iov
= &aiov
;
5838 auio
.uio_iovcnt
= 1;
5839 auio
.uio_segflg
= UIO_SYSSPACE
;
5841 auio
.uio_rw
= UIO_READ
;
5842 auio
.uio_offset
= 0;
5844 size_t plen
= strlen(attrprefix
);
5847 aiov
.iov_base
= (void *)dirbuf
;
5848 aiov
.iov_len
= sizeof (dirbuf
);
5849 auio
.uio_resid
= sizeof (dirbuf
);
5850 error
= VOP_READDIR(vp
, &auio
, ap
->a_cred
, &eof
, NULL
, NULL
);
5853 int done
= sizeof (dirbuf
) - auio
.uio_resid
;
5854 for (int pos
= 0; pos
< done
; ) {
5855 struct dirent
*dp
= (struct dirent
*)(dirbuf
+ pos
);
5856 pos
+= dp
->d_reclen
;
5858 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5859 * is what we get when attribute was created on Solaris.
5861 if (dp
->d_type
!= DT_REG
&& dp
->d_type
!= DT_UNKNOWN
)
5863 else if (plen
== 0 &&
5864 ZFS_XA_NS_PREFIX_FORBIDDEN(dp
->d_name
))
5866 else if (strncmp(dp
->d_name
, attrprefix
, plen
) != 0)
5868 uint8_t nlen
= dp
->d_namlen
- plen
;
5869 if (ap
->a_size
!= NULL
) {
5870 *ap
->a_size
+= 1 + nlen
;
5871 } else if (ap
->a_uio
!= NULL
) {
5873 * Format of extattr name entry is one byte for
5874 * length and the rest for name.
5876 error
= uiomove(&nlen
, 1, ap
->a_uio
);
5878 char *namep
= dp
->d_name
+ plen
;
5879 error
= uiomove(namep
, nlen
, ap
->a_uio
);
5882 error
= SET_ERROR(error
);
5887 } while (!eof
&& error
== 0);
5894 zfs_listextattr_sa(struct vop_listextattr_args
*ap
, const char *attrprefix
)
5896 znode_t
*zp
= VTOZ(ap
->a_vp
);
5899 error
= zfs_ensure_xattr_cached(zp
);
5903 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
5904 ASSERT3P(zp
->z_xattr_cached
, !=, NULL
);
5906 size_t plen
= strlen(attrprefix
);
5907 nvpair_t
*nvp
= NULL
;
5908 while ((nvp
= nvlist_next_nvpair(zp
->z_xattr_cached
, nvp
)) != NULL
) {
5909 ASSERT3U(nvpair_type(nvp
), ==, DATA_TYPE_BYTE_ARRAY
);
5911 const char *name
= nvpair_name(nvp
);
5912 if (plen
== 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name
))
5914 else if (strncmp(name
, attrprefix
, plen
) != 0)
5916 uint8_t nlen
= strlen(name
) - plen
;
5917 if (ap
->a_size
!= NULL
) {
5918 *ap
->a_size
+= 1 + nlen
;
5919 } else if (ap
->a_uio
!= NULL
) {
5921 * Format of extattr name entry is one byte for
5922 * length and the rest for name.
5924 error
= uiomove(&nlen
, 1, ap
->a_uio
);
5926 char *namep
= __DECONST(char *, name
) + plen
;
5927 error
= uiomove(namep
, nlen
, ap
->a_uio
);
5930 error
= SET_ERROR(error
);
5940 zfs_listextattr_impl(struct vop_listextattr_args
*ap
, boolean_t compat
)
5942 znode_t
*zp
= VTOZ(ap
->a_vp
);
5943 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5944 char attrprefix
[16];
5947 error
= zfs_create_attrname(ap
->a_attrnamespace
, "", attrprefix
,
5948 sizeof (attrprefix
), compat
);
5952 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
)
5953 error
= zfs_listextattr_sa(ap
, attrprefix
);
5955 error
= zfs_listextattr_dir(ap
, attrprefix
);
5960 * Vnode operation to retrieve extended attributes on a vnode.
5963 zfs_listextattr(struct vop_listextattr_args
*ap
)
5965 znode_t
*zp
= VTOZ(ap
->a_vp
);
5966 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5969 if (ap
->a_size
!= NULL
)
5973 * If the xattr property is off, refuse the request.
5975 if (!(zfsvfs
->z_flags
& ZSB_XATTR
))
5976 return (SET_ERROR(EOPNOTSUPP
));
5978 error
= extattr_check_cred(ap
->a_vp
, ap
->a_attrnamespace
,
5979 ap
->a_cred
, ap
->a_td
, VREAD
);
5981 return (SET_ERROR(error
));
5983 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
5985 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
5987 error
= zfs_listextattr_impl(ap
, zfs_xattr_compat
);
5988 if (error
== 0 && ap
->a_attrnamespace
== EXTATTR_NAMESPACE_USER
) {
5989 /* Also list user xattrs with the alternate format. */
5990 error
= zfs_listextattr_impl(ap
, !zfs_xattr_compat
);
5993 rw_exit(&zp
->z_xattr_lock
);
5994 zfs_exit(zfsvfs
, FTAG
);
5998 #ifndef _SYS_SYSPROTO_H_
5999 struct vop_getacl_args
{
6009 zfs_freebsd_getacl(struct vop_getacl_args
*ap
)
6012 vsecattr_t vsecattr
;
6014 if (ap
->a_type
!= ACL_TYPE_NFS4
)
6017 vsecattr
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
6018 if ((error
= zfs_getsecattr(VTOZ(ap
->a_vp
),
6019 &vsecattr
, 0, ap
->a_cred
)))
6022 error
= acl_from_aces(ap
->a_aclp
, vsecattr
.vsa_aclentp
,
6023 vsecattr
.vsa_aclcnt
);
6024 if (vsecattr
.vsa_aclentp
!= NULL
)
6025 kmem_free(vsecattr
.vsa_aclentp
, vsecattr
.vsa_aclentsz
);
6030 #ifndef _SYS_SYSPROTO_H_
6031 struct vop_setacl_args
{
6041 zfs_freebsd_setacl(struct vop_setacl_args
*ap
)
6044 vsecattr_t vsecattr
;
6045 int aclbsize
; /* size of acl list in bytes */
6048 if (ap
->a_type
!= ACL_TYPE_NFS4
)
6051 if (ap
->a_aclp
== NULL
)
6054 if (ap
->a_aclp
->acl_cnt
< 1 || ap
->a_aclp
->acl_cnt
> MAX_ACL_ENTRIES
)
6058 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6059 * splitting every entry into two and appending "canonical six"
6060 * entries at the end. Don't allow for setting an ACL that would
6061 * cause chmod(2) to run out of ACL entries.
6063 if (ap
->a_aclp
->acl_cnt
* 2 + 6 > ACL_MAX_ENTRIES
)
6066 error
= acl_nfs4_check(ap
->a_aclp
, ap
->a_vp
->v_type
== VDIR
);
6070 vsecattr
.vsa_mask
= VSA_ACE
;
6071 aclbsize
= ap
->a_aclp
->acl_cnt
* sizeof (ace_t
);
6072 vsecattr
.vsa_aclentp
= kmem_alloc(aclbsize
, KM_SLEEP
);
6073 aaclp
= vsecattr
.vsa_aclentp
;
6074 vsecattr
.vsa_aclentsz
= aclbsize
;
6076 aces_from_acl(vsecattr
.vsa_aclentp
, &vsecattr
.vsa_aclcnt
, ap
->a_aclp
);
6077 error
= zfs_setsecattr(VTOZ(ap
->a_vp
), &vsecattr
, 0, ap
->a_cred
);
6078 kmem_free(aaclp
, aclbsize
);
6083 #ifndef _SYS_SYSPROTO_H_
6084 struct vop_aclcheck_args
{
6094 zfs_freebsd_aclcheck(struct vop_aclcheck_args
*ap
)
6097 return (EOPNOTSUPP
);
6101 zfs_vptocnp(struct vop_vptocnp_args
*ap
)
6103 vnode_t
*covered_vp
;
6104 vnode_t
*vp
= ap
->a_vp
;
6105 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
6106 znode_t
*zp
= VTOZ(vp
);
6110 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
6114 * If we are a snapshot mounted under .zfs, run the operation
6115 * on the covered vnode.
6117 if (zp
->z_id
!= zfsvfs
->z_root
|| zfsvfs
->z_parent
== zfsvfs
) {
6118 char name
[MAXNAMLEN
+ 1];
6122 error
= zfs_znode_parent_and_name(zp
, &dzp
, name
);
6125 if (*ap
->a_buflen
< len
)
6126 error
= SET_ERROR(ENOMEM
);
6129 *ap
->a_buflen
-= len
;
6130 memcpy(ap
->a_buf
+ *ap
->a_buflen
, name
, len
);
6131 *ap
->a_vpp
= ZTOV(dzp
);
6133 zfs_exit(zfsvfs
, FTAG
);
6136 zfs_exit(zfsvfs
, FTAG
);
6138 covered_vp
= vp
->v_mount
->mnt_vnodecovered
;
6139 #if __FreeBSD_version >= 1300045
6140 enum vgetstate vs
= vget_prep(covered_vp
);
6144 ltype
= VOP_ISLOCKED(vp
);
6146 #if __FreeBSD_version >= 1300045
6147 error
= vget_finish(covered_vp
, LK_SHARED
, vs
);
6149 error
= vget(covered_vp
, LK_SHARED
| LK_VNHELD
, curthread
);
6152 #if __FreeBSD_version >= 1300123
6153 error
= VOP_VPTOCNP(covered_vp
, ap
->a_vpp
, ap
->a_buf
,
6156 error
= VOP_VPTOCNP(covered_vp
, ap
->a_vpp
, ap
->a_cred
,
6157 ap
->a_buf
, ap
->a_buflen
);
6161 vn_lock(vp
, ltype
| LK_RETRY
);
6162 if (VN_IS_DOOMED(vp
))
6163 error
= SET_ERROR(ENOENT
);
6167 #if __FreeBSD_version >= 1400032
6169 zfs_deallocate(struct vop_deallocate_args
*ap
)
6171 znode_t
*zp
= VTOZ(ap
->a_vp
);
6172 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
6174 off_t off
, len
, file_sz
;
6177 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
6181 * Callers might not be able to detect properly that we are read-only,
6182 * so check it explicitly here.
6184 if (zfs_is_readonly(zfsvfs
)) {
6185 zfs_exit(zfsvfs
, FTAG
);
6186 return (SET_ERROR(EROFS
));
6189 zilog
= zfsvfs
->z_log
;
6190 off
= *ap
->a_offset
;
6192 file_sz
= zp
->z_size
;
6193 if (off
+ len
> file_sz
)
6194 len
= file_sz
- off
;
6195 /* Fast path for out-of-range request. */
6198 zfs_exit(zfsvfs
, FTAG
);
6202 error
= zfs_freesp(zp
, off
, len
, O_RDWR
, TRUE
);
6204 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
||
6205 (ap
->a_ioflag
& IO_SYNC
) != 0)
6206 zil_commit(zilog
, zp
->z_id
);
6207 *ap
->a_offset
= off
+ len
;
6211 zfs_exit(zfsvfs
, FTAG
);
6216 #ifndef _SYS_SYSPROTO_H_
6217 struct vop_copy_file_range_args
{
6218 struct vnode
*a_invp
;
6220 struct vnode
*a_outvp
;
6223 unsigned int a_flags
;
6224 struct ucred
*a_incred
;
6225 struct ucred
*a_outcred
;
6226 struct thread
*a_fsizetd
;
6230 * TODO: FreeBSD will only call file system-specific copy_file_range() if both
6231 * files resides under the same mountpoint. In case of ZFS we want to be called
6232 * even is files are in different datasets (but on the same pools, but we need
6233 * to check that ourselves).
6236 zfs_freebsd_copy_file_range(struct vop_copy_file_range_args
*ap
)
6238 zfsvfs_t
*outzfsvfs
;
6239 struct vnode
*invp
= ap
->a_invp
;
6240 struct vnode
*outvp
= ap
->a_outvp
;
6244 uint64_t len
= *ap
->a_lenp
;
6247 * TODO: If offset/length is not aligned to recordsize, use
6248 * vn_generic_copy_file_range() on this fragment.
6249 * It would be better to do this after we lock the vnodes, but then we
6250 * need something else than vn_generic_copy_file_range().
6253 vn_start_write(outvp
, &mp
, V_WAIT
);
6254 if (__predict_true(mp
== outvp
->v_mount
)) {
6255 outzfsvfs
= (zfsvfs_t
*)mp
->mnt_data
;
6256 if (!spa_feature_is_enabled(dmu_objset_spa(outzfsvfs
->z_os
),
6257 SPA_FEATURE_BLOCK_CLONING
)) {
6258 goto bad_write_fallback
;
6261 if (invp
== outvp
) {
6262 if (vn_lock(outvp
, LK_EXCLUSIVE
) != 0) {
6263 goto bad_write_fallback
;
6266 #if (__FreeBSD_version >= 1302506 && __FreeBSD_version < 1400000) || \
6267 __FreeBSD_version >= 1400086
6268 vn_lock_pair(invp
, false, LK_EXCLUSIVE
, outvp
, false,
6271 vn_lock_pair(invp
, false, outvp
, false);
6273 if (VN_IS_DOOMED(invp
) || VN_IS_DOOMED(outvp
)) {
6274 goto bad_locked_fallback
;
6279 error
= mac_vnode_check_write(curthread
->td_ucred
, ap
->a_outcred
,
6285 io
.uio_offset
= *ap
->a_outoffp
;
6286 io
.uio_resid
= *ap
->a_lenp
;
6287 error
= vn_rlimit_fsize(outvp
, &io
, ap
->a_fsizetd
);
6291 error
= zfs_clone_range(VTOZ(invp
), ap
->a_inoffp
, VTOZ(outvp
),
6292 ap
->a_outoffp
, &len
, ap
->a_outcred
);
6293 if (error
== EXDEV
|| error
== EOPNOTSUPP
)
6294 goto bad_locked_fallback
;
6295 *ap
->a_lenp
= (size_t)len
;
6301 vn_finished_write(mp
);
6304 bad_locked_fallback
:
6310 vn_finished_write(mp
);
6311 error
= vn_generic_copy_file_range(ap
->a_invp
, ap
->a_inoffp
,
6312 ap
->a_outvp
, ap
->a_outoffp
, ap
->a_lenp
, ap
->a_flags
,
6313 ap
->a_incred
, ap
->a_outcred
, ap
->a_fsizetd
);
6317 struct vop_vector zfs_vnodeops
;
6318 struct vop_vector zfs_fifoops
;
6319 struct vop_vector zfs_shareops
;
6321 struct vop_vector zfs_vnodeops
= {
6322 .vop_default
= &default_vnodeops
,
6323 .vop_inactive
= zfs_freebsd_inactive
,
6324 #if __FreeBSD_version >= 1300042
6325 .vop_need_inactive
= zfs_freebsd_need_inactive
,
6327 .vop_reclaim
= zfs_freebsd_reclaim
,
6328 #if __FreeBSD_version >= 1300102
6329 .vop_fplookup_vexec
= zfs_freebsd_fplookup_vexec
,
6331 #if __FreeBSD_version >= 1300139
6332 .vop_fplookup_symlink
= zfs_freebsd_fplookup_symlink
,
6334 .vop_access
= zfs_freebsd_access
,
6335 .vop_allocate
= VOP_EINVAL
,
6336 #if __FreeBSD_version >= 1400032
6337 .vop_deallocate
= zfs_deallocate
,
6339 .vop_lookup
= zfs_cache_lookup
,
6340 .vop_cachedlookup
= zfs_freebsd_cachedlookup
,
6341 .vop_getattr
= zfs_freebsd_getattr
,
6342 .vop_setattr
= zfs_freebsd_setattr
,
6343 .vop_create
= zfs_freebsd_create
,
6344 .vop_mknod
= (vop_mknod_t
*)zfs_freebsd_create
,
6345 .vop_mkdir
= zfs_freebsd_mkdir
,
6346 .vop_readdir
= zfs_freebsd_readdir
,
6347 .vop_fsync
= zfs_freebsd_fsync
,
6348 .vop_open
= zfs_freebsd_open
,
6349 .vop_close
= zfs_freebsd_close
,
6350 .vop_rmdir
= zfs_freebsd_rmdir
,
6351 .vop_ioctl
= zfs_freebsd_ioctl
,
6352 .vop_link
= zfs_freebsd_link
,
6353 .vop_symlink
= zfs_freebsd_symlink
,
6354 .vop_readlink
= zfs_freebsd_readlink
,
6355 .vop_read
= zfs_freebsd_read
,
6356 .vop_write
= zfs_freebsd_write
,
6357 .vop_remove
= zfs_freebsd_remove
,
6358 .vop_rename
= zfs_freebsd_rename
,
6359 .vop_pathconf
= zfs_freebsd_pathconf
,
6360 .vop_bmap
= zfs_freebsd_bmap
,
6361 .vop_fid
= zfs_freebsd_fid
,
6362 .vop_getextattr
= zfs_getextattr
,
6363 .vop_deleteextattr
= zfs_deleteextattr
,
6364 .vop_setextattr
= zfs_setextattr
,
6365 .vop_listextattr
= zfs_listextattr
,
6366 .vop_getacl
= zfs_freebsd_getacl
,
6367 .vop_setacl
= zfs_freebsd_setacl
,
6368 .vop_aclcheck
= zfs_freebsd_aclcheck
,
6369 .vop_getpages
= zfs_freebsd_getpages
,
6370 .vop_putpages
= zfs_freebsd_putpages
,
6371 .vop_vptocnp
= zfs_vptocnp
,
6372 #if __FreeBSD_version >= 1300064
6373 .vop_lock1
= vop_lock
,
6374 .vop_unlock
= vop_unlock
,
6375 .vop_islocked
= vop_islocked
,
6377 #if __FreeBSD_version >= 1400043
6378 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6380 .vop_copy_file_range
= zfs_freebsd_copy_file_range
,
6382 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops
);
6384 struct vop_vector zfs_fifoops
= {
6385 .vop_default
= &fifo_specops
,
6386 .vop_fsync
= zfs_freebsd_fsync
,
6387 #if __FreeBSD_version >= 1300102
6388 .vop_fplookup_vexec
= zfs_freebsd_fplookup_vexec
,
6390 #if __FreeBSD_version >= 1300139
6391 .vop_fplookup_symlink
= zfs_freebsd_fplookup_symlink
,
6393 .vop_access
= zfs_freebsd_access
,
6394 .vop_getattr
= zfs_freebsd_getattr
,
6395 .vop_inactive
= zfs_freebsd_inactive
,
6396 .vop_read
= VOP_PANIC
,
6397 .vop_reclaim
= zfs_freebsd_reclaim
,
6398 .vop_setattr
= zfs_freebsd_setattr
,
6399 .vop_write
= VOP_PANIC
,
6400 .vop_pathconf
= zfs_freebsd_pathconf
,
6401 .vop_fid
= zfs_freebsd_fid
,
6402 .vop_getacl
= zfs_freebsd_getacl
,
6403 .vop_setacl
= zfs_freebsd_setacl
,
6404 .vop_aclcheck
= zfs_freebsd_aclcheck
,
6405 #if __FreeBSD_version >= 1400043
6406 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6409 VFS_VOP_VECTOR_REGISTER(zfs_fifoops
);
6412 * special share hidden files vnode operations template
6414 struct vop_vector zfs_shareops
= {
6415 .vop_default
= &default_vnodeops
,
6416 #if __FreeBSD_version >= 1300121
6417 .vop_fplookup_vexec
= VOP_EAGAIN
,
6419 #if __FreeBSD_version >= 1300139
6420 .vop_fplookup_symlink
= VOP_EAGAIN
,
6422 .vop_access
= zfs_freebsd_access
,
6423 .vop_inactive
= zfs_freebsd_inactive
,
6424 .vop_reclaim
= zfs_freebsd_reclaim
,
6425 .vop_fid
= zfs_freebsd_fid
,
6426 .vop_pathconf
= zfs_freebsd_pathconf
,
6427 #if __FreeBSD_version >= 1400043
6428 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6431 VFS_VOP_VECTOR_REGISTER(zfs_shareops
);
6433 ZFS_MODULE_PARAM(zfs
, zfs_
, xattr_compat
, INT
, ZMOD_RW
,
6434 "Use legacy ZFS xattr naming for writing new user namespace xattrs");