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>
43 #include <sys/dirent.h>
47 #include <sys/taskq.h>
49 #include <sys/atomic.h>
50 #include <sys/namei.h>
52 #include <sys/cmn_err.h>
54 #include <sys/sysproto.h>
55 #include <sys/errno.h>
56 #include <sys/unistd.h>
57 #include <sys/zfs_dir.h>
58 #include <sys/zfs_ioctl.h>
59 #include <sys/fs/zfs.h>
61 #include <sys/dmu_objset.h>
67 #include <sys/policy.h>
68 #include <sys/sunddi.h>
69 #include <sys/filio.h>
71 #include <sys/zfs_ctldir.h>
72 #include <sys/zfs_fuid.h>
73 #include <sys/zfs_quota.h>
74 #include <sys/zfs_sa.h>
75 #include <sys/zfs_rlock.h>
78 #include <sys/sched.h>
80 #include <sys/vmmeter.h>
81 #include <vm/vm_param.h>
83 #include <sys/zfs_vnops.h>
84 #include <sys/module.h>
85 #include <sys/sysent.h>
86 #include <sys/dmu_impl.h>
88 #include <sys/zfeature.h>
90 #include <vm/vm_object.h>
92 #include <sys/extattr.h>
96 #define VN_OPEN_INVFS 0x0
101 #ifdef DEBUG_VFS_LOCKS
102 #define VNCHECKREF(vp) \
103 VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp, \
104 ("%s: wrong ref counts", __func__));
106 #define VNCHECKREF(vp)
109 #if __FreeBSD_version >= 1400045
110 typedef uint64_t cookie_t
;
112 typedef ulong_t cookie_t
;
118 * Each vnode op performs some logical unit of work. To do this, the ZPL must
119 * properly lock its in-core state, create a DMU transaction, do the work,
120 * record this work in the intent log (ZIL), commit the DMU transaction,
121 * and wait for the intent log to commit if it is a synchronous operation.
122 * Moreover, the vnode ops must work in both normal and log replay context.
123 * The ordering of events is important to avoid deadlocks and references
124 * to freed memory. The example below illustrates the following Big Rules:
126 * (1) A check must be made in each zfs thread for a mounted file system.
127 * This is done avoiding races using zfs_enter(zfsvfs).
128 * A zfs_exit(zfsvfs) is needed before all returns. Any znodes
129 * must be checked with zfs_verify_zp(zp). Both of these macros
130 * can return EIO from the calling function.
132 * (2) VN_RELE() should always be the last thing except for zil_commit()
133 * (if necessary) and zfs_exit(). This is for 3 reasons:
134 * First, if it's the last reference, the vnode/znode
135 * can be freed, so the zp may point to freed memory. Second, the last
136 * reference will call zfs_zinactive(), which may induce a lot of work --
137 * pushing cached pages (which acquires range locks) and syncing out
138 * cached atime changes. Third, zfs_zinactive() may require a new tx,
139 * which could deadlock the system if you were already holding one.
140 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
142 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
143 * as they can span dmu_tx_assign() calls.
145 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
146 * dmu_tx_assign(). This is critical because we don't want to block
147 * while holding locks.
149 * If no ZPL locks are held (aside from zfs_enter()), use TXG_WAIT. This
150 * reduces lock contention and CPU usage when we must wait (note that if
151 * throughput is constrained by the storage, nearly every transaction
154 * Note, in particular, that if a lock is sometimes acquired before
155 * the tx assigns, and sometimes after (e.g. z_lock), then failing
156 * to use a non-blocking assign can deadlock the system. The scenario:
158 * Thread A has grabbed a lock before calling dmu_tx_assign().
159 * Thread B is in an already-assigned tx, and blocks for this lock.
160 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
161 * forever, because the previous txg can't quiesce until B's tx commits.
163 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
164 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
165 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
166 * to indicate that this operation has already called dmu_tx_wait().
167 * This will ensure that we don't retry forever, waiting a short bit
170 * (5) If the operation succeeded, generate the intent log entry for it
171 * before dropping locks. This ensures that the ordering of events
172 * in the intent log matches the order in which they actually occurred.
173 * During ZIL replay the zfs_log_* functions will update the sequence
174 * number to indicate the zil transaction has replayed.
176 * (6) At the end of each vnode op, the DMU tx must always commit,
177 * regardless of whether there were any errors.
179 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
180 * to ensure that synchronous semantics are provided when necessary.
182 * In general, this is how things should be ordered in each vnode op:
184 * zfs_enter(zfsvfs); // exit if unmounted
186 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
187 * rw_enter(...); // grab any other locks you need
188 * tx = dmu_tx_create(...); // get DMU tx
189 * dmu_tx_hold_*(); // hold each object you might modify
190 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
192 * rw_exit(...); // drop locks
193 * zfs_dirent_unlock(dl); // unlock directory entry
194 * VN_RELE(...); // release held vnodes
195 * if (error == ERESTART) {
201 * dmu_tx_abort(tx); // abort DMU tx
202 * zfs_exit(zfsvfs); // finished in zfs
203 * return (error); // really out of space
205 * error = do_real_work(); // do whatever this VOP does
207 * zfs_log_*(...); // on success, make ZIL entry
208 * dmu_tx_commit(tx); // commit DMU tx -- error or not
209 * rw_exit(...); // drop locks
210 * zfs_dirent_unlock(dl); // unlock directory entry
211 * VN_RELE(...); // release held vnodes
212 * zil_commit(zilog, foid); // synchronous when necessary
213 * zfs_exit(zfsvfs); // finished in zfs
214 * return (error); // done, report error
217 zfs_open(vnode_t
**vpp
, int flag
, cred_t
*cr
)
220 znode_t
*zp
= VTOZ(*vpp
);
221 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
224 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
227 if ((flag
& FWRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
228 ((flag
& FAPPEND
) == 0)) {
229 zfs_exit(zfsvfs
, FTAG
);
230 return (SET_ERROR(EPERM
));
234 * Keep a count of the synchronous opens in the znode. On first
235 * synchronous open we must convert all previous async transactions
236 * into sync to keep correct ordering.
239 if (atomic_inc_32_nv(&zp
->z_sync_cnt
) == 1)
240 zil_async_to_sync(zfsvfs
->z_log
, zp
->z_id
);
243 zfs_exit(zfsvfs
, FTAG
);
248 zfs_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
)
250 (void) offset
, (void) cr
;
251 znode_t
*zp
= VTOZ(vp
);
252 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
255 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
258 /* Decrement the synchronous opens in the znode */
259 if ((flag
& O_SYNC
) && (count
== 1))
260 atomic_dec_32(&zp
->z_sync_cnt
);
262 zfs_exit(zfsvfs
, FTAG
);
267 zfs_ioctl(vnode_t
*vp
, ulong_t com
, intptr_t data
, int flag
, cred_t
*cred
,
270 (void) flag
, (void) cred
, (void) rvalp
;
280 * The following two ioctls are used by bfu. Faking out,
281 * necessary to avoid bfu errors.
293 off
= *(offset_t
*)data
;
294 /* offset parameter is in/out */
295 error
= zfs_holey(VTOZ(vp
), com
, &off
);
298 *(offset_t
*)data
= off
;
302 return (SET_ERROR(ENOTTY
));
306 page_busy(vnode_t
*vp
, int64_t start
, int64_t off
, int64_t nbytes
)
313 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
314 * aligned boundaries, if the range is not aligned. As a result a
315 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
316 * It may happen that all DEV_BSIZE subranges are marked clean and thus
317 * the whole page would be considered clean despite have some
319 * For this reason we should shrink the range to DEV_BSIZE aligned
320 * boundaries before calling vm_page_clear_dirty.
322 end
= rounddown2(off
+ nbytes
, DEV_BSIZE
);
323 off
= roundup2(off
, DEV_BSIZE
);
327 vm_page_grab_valid_unlocked(&pp
, obj
, OFF_TO_IDX(start
),
328 VM_ALLOC_NOCREAT
| VM_ALLOC_SBUSY
| VM_ALLOC_NORMAL
|
331 ASSERT3U(pp
->valid
, ==, VM_PAGE_BITS_ALL
);
332 vm_object_pip_add(obj
, 1);
333 pmap_remove_write(pp
);
335 vm_page_clear_dirty(pp
, off
, nbytes
);
341 page_unbusy(vm_page_t pp
)
345 vm_object_pip_wakeup(pp
->object
);
349 page_hold(vnode_t
*vp
, int64_t start
)
355 vm_page_grab_valid_unlocked(&m
, obj
, OFF_TO_IDX(start
),
356 VM_ALLOC_NOCREAT
| VM_ALLOC_WIRED
| VM_ALLOC_IGN_SBUSY
|
362 page_unhold(vm_page_t pp
)
364 vm_page_unwire(pp
, PQ_ACTIVE
);
368 * When a file is memory mapped, we must keep the IO data synchronized
369 * between the DMU cache and the memory mapped pages. What this means:
371 * On Write: If we find a memory mapped page, we write to *both*
372 * the page and the dmu buffer.
375 update_pages(znode_t
*zp
, int64_t start
, int len
, objset_t
*os
)
379 vnode_t
*vp
= ZTOV(zp
);
383 ASSERT3P(vp
->v_mount
, !=, NULL
);
385 ASSERT3P(obj
, !=, NULL
);
387 off
= start
& PAGEOFFSET
;
388 vm_object_pip_add(obj
, 1);
389 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
391 int nbytes
= imin(PAGESIZE
- off
, len
);
393 if ((pp
= page_busy(vp
, start
, off
, nbytes
)) != NULL
) {
394 va
= zfs_map_page(pp
, &sf
);
395 (void) dmu_read(os
, zp
->z_id
, start
+ off
, nbytes
,
396 va
+ off
, DMU_READ_PREFETCH
);
403 vm_object_pip_wakeup(obj
);
407 * Read with UIO_NOCOPY flag means that sendfile(2) requests
408 * ZFS to populate a range of page cache pages with data.
410 * NOTE: this function could be optimized to pre-allocate
411 * all pages in advance, drain exclusive busy on all of them,
412 * map them into contiguous KVA region and populate them
413 * in one single dmu_read() call.
416 mappedread_sf(znode_t
*zp
, int nbytes
, zfs_uio_t
*uio
)
418 vnode_t
*vp
= ZTOV(zp
);
419 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
428 ASSERT3U(zfs_uio_segflg(uio
), ==, UIO_NOCOPY
);
429 ASSERT3P(vp
->v_mount
, !=, NULL
);
431 ASSERT3P(obj
, !=, NULL
);
432 ASSERT0(zfs_uio_offset(uio
) & PAGEOFFSET
);
434 for (start
= zfs_uio_offset(uio
); len
> 0; start
+= PAGESIZE
) {
435 int bytes
= MIN(PAGESIZE
, len
);
437 pp
= vm_page_grab_unlocked(obj
, OFF_TO_IDX(start
),
438 VM_ALLOC_SBUSY
| VM_ALLOC_NORMAL
| VM_ALLOC_IGN_SBUSY
);
439 if (vm_page_none_valid(pp
)) {
440 va
= zfs_map_page(pp
, &sf
);
441 error
= dmu_read(os
, zp
->z_id
, start
, bytes
, va
,
443 if (bytes
!= PAGESIZE
&& error
== 0)
444 memset(va
+ bytes
, 0, PAGESIZE
- bytes
);
448 vm_page_activate(pp
);
451 zfs_vmobject_wlock(obj
);
452 if (!vm_page_wired(pp
) && pp
->valid
== 0 &&
453 vm_page_busy_tryupgrade(pp
))
457 zfs_vmobject_wunlock(obj
);
460 ASSERT3U(pp
->valid
, ==, VM_PAGE_BITS_ALL
);
465 zfs_uio_advance(uio
, bytes
);
472 * When a file is memory mapped, we must keep the IO data synchronized
473 * between the DMU cache and the memory mapped pages. What this means:
475 * On Read: We "read" preferentially from memory mapped pages,
476 * else we default from the dmu buffer.
478 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
479 * the file is memory mapped.
482 mappedread(znode_t
*zp
, int nbytes
, zfs_uio_t
*uio
)
484 vnode_t
*vp
= ZTOV(zp
);
491 ASSERT3P(vp
->v_mount
, !=, NULL
);
493 ASSERT3P(obj
, !=, NULL
);
495 start
= zfs_uio_offset(uio
);
496 off
= start
& PAGEOFFSET
;
497 for (start
&= PAGEMASK
; len
> 0; start
+= PAGESIZE
) {
499 uint64_t bytes
= MIN(PAGESIZE
- off
, len
);
501 if ((pp
= page_hold(vp
, start
))) {
505 va
= zfs_map_page(pp
, &sf
);
506 error
= vn_io_fault_uiomove(va
+ off
, bytes
,
507 GET_UIO_STRUCT(uio
));
511 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
523 zfs_write_simple(znode_t
*zp
, const void *data
, size_t len
,
524 loff_t pos
, size_t *presid
)
529 error
= vn_rdwr(UIO_WRITE
, ZTOV(zp
), __DECONST(void *, data
), len
, pos
,
530 UIO_SYSSPACE
, IO_SYNC
, kcred
, NOCRED
, &resid
, curthread
);
533 return (SET_ERROR(error
));
534 } else if (presid
== NULL
) {
536 error
= SET_ERROR(EIO
);
545 zfs_zrele_async(znode_t
*zp
)
547 vnode_t
*vp
= ZTOV(zp
);
548 objset_t
*os
= ITOZSB(vp
)->z_os
;
550 VN_RELE_ASYNC(vp
, dsl_pool_zrele_taskq(dmu_objset_pool(os
)));
554 zfs_dd_callback(struct mount
*mp
, void *arg
, int lkflags
, struct vnode
**vpp
)
559 error
= vn_lock(*vpp
, lkflags
);
566 zfs_lookup_lock(vnode_t
*dvp
, vnode_t
*vp
, const char *name
, int lkflags
)
568 znode_t
*zdp
= VTOZ(dvp
);
569 zfsvfs_t
*zfsvfs __unused
= zdp
->z_zfsvfs
;
573 if (zfsvfs
->z_replay
== B_FALSE
)
574 ASSERT_VOP_LOCKED(dvp
, __func__
);
576 if (name
[0] == 0 || (name
[0] == '.' && name
[1] == 0)) {
577 ASSERT3P(dvp
, ==, vp
);
579 ltype
= lkflags
& LK_TYPE_MASK
;
580 if (ltype
!= VOP_ISLOCKED(dvp
)) {
581 if (ltype
== LK_EXCLUSIVE
)
582 vn_lock(dvp
, LK_UPGRADE
| LK_RETRY
);
583 else /* if (ltype == LK_SHARED) */
584 vn_lock(dvp
, LK_DOWNGRADE
| LK_RETRY
);
587 * Relock for the "." case could leave us with
590 if (VN_IS_DOOMED(dvp
)) {
592 return (SET_ERROR(ENOENT
));
596 } else if (name
[0] == '.' && name
[1] == '.' && name
[2] == 0) {
598 * Note that in this case, dvp is the child vnode, and we
599 * are looking up the parent vnode - exactly reverse from
600 * normal operation. Unlocking dvp requires some rather
601 * tricky unlock/relock dance to prevent mp from being freed;
602 * use vn_vget_ino_gen() which takes care of all that.
604 * XXX Note that there is a time window when both vnodes are
605 * unlocked. It is possible, although highly unlikely, that
606 * during that window the parent-child relationship between
607 * the vnodes may change, for example, get reversed.
608 * In that case we would have a wrong lock order for the vnodes.
609 * All other filesystems seem to ignore this problem, so we
611 * A potential solution could be implemented as follows:
612 * - using LK_NOWAIT when locking the second vnode and retrying
614 * - checking that the parent-child relationship still holds
615 * after locking both vnodes and retrying if it doesn't
617 error
= vn_vget_ino_gen(dvp
, zfs_dd_callback
, vp
, lkflags
, &vp
);
620 error
= vn_lock(vp
, lkflags
);
628 * Lookup an entry in a directory, or an extended attribute directory.
629 * If it exists, return a held vnode reference for it.
631 * IN: dvp - vnode of directory to search.
632 * nm - name of entry to lookup.
633 * pnp - full pathname to lookup [UNUSED].
634 * flags - LOOKUP_XATTR set if looking for an attribute.
635 * rdir - root directory vnode [UNUSED].
636 * cr - credentials of caller.
637 * ct - caller context
639 * OUT: vpp - vnode of located entry, NULL if not found.
641 * RETURN: 0 on success, error code on failure.
647 zfs_lookup(vnode_t
*dvp
, const char *nm
, vnode_t
**vpp
,
648 struct componentname
*cnp
, int nameiop
, cred_t
*cr
, int flags
,
651 znode_t
*zdp
= VTOZ(dvp
);
653 zfsvfs_t
*zfsvfs
= zdp
->z_zfsvfs
;
658 * Fast path lookup, however we must skip DNLC lookup
659 * for case folding or normalizing lookups because the
660 * DNLC code only stores the passed in name. This means
661 * creating 'a' and removing 'A' on a case insensitive
662 * file system would work, but DNLC still thinks 'a'
663 * exists and won't let you create it again on the next
664 * pass through fast path.
666 if (!(flags
& LOOKUP_XATTR
)) {
667 if (dvp
->v_type
!= VDIR
) {
668 return (SET_ERROR(ENOTDIR
));
669 } else if (zdp
->z_sa_hdl
== NULL
) {
670 return (SET_ERROR(EIO
));
674 DTRACE_PROBE2(zfs__fastpath__lookup__miss
, vnode_t
*, dvp
,
677 if ((error
= zfs_enter_verify_zp(zfsvfs
, zdp
, FTAG
)) != 0)
680 dvp_seqc
= vn_seqc_read_notmodify(dvp
);
684 if (flags
& LOOKUP_XATTR
) {
686 * If the xattr property is off, refuse the lookup request.
688 if (!(zfsvfs
->z_flags
& ZSB_XATTR
)) {
689 zfs_exit(zfsvfs
, FTAG
);
690 return (SET_ERROR(EOPNOTSUPP
));
694 * We don't allow recursive attributes..
695 * Maybe someday we will.
697 if (zdp
->z_pflags
& ZFS_XATTR
) {
698 zfs_exit(zfsvfs
, FTAG
);
699 return (SET_ERROR(EINVAL
));
702 if ((error
= zfs_get_xattrdir(VTOZ(dvp
), &zp
, cr
, flags
))) {
703 zfs_exit(zfsvfs
, FTAG
);
709 * Do we have permission to get into attribute directory?
711 error
= zfs_zaccess(zp
, ACE_EXECUTE
, 0, B_FALSE
, cr
, NULL
);
716 zfs_exit(zfsvfs
, FTAG
);
721 * Check accessibility of directory if we're not coming in via
726 if ((cnp
->cn_flags
& NOEXECCHECK
) != 0) {
727 cnp
->cn_flags
&= ~NOEXECCHECK
;
730 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
,
732 zfs_exit(zfsvfs
, FTAG
);
737 if (zfsvfs
->z_utf8
&& u8_validate(nm
, strlen(nm
),
738 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
739 zfs_exit(zfsvfs
, FTAG
);
740 return (SET_ERROR(EILSEQ
));
745 * First handle the special cases.
747 if ((cnp
->cn_flags
& ISDOTDOT
) != 0) {
749 * If we are a snapshot mounted under .zfs, return
750 * the vp for the snapshot directory.
752 if (zdp
->z_id
== zfsvfs
->z_root
&& zfsvfs
->z_parent
!= zfsvfs
) {
753 struct componentname cn
;
757 zfs_exit(zfsvfs
, FTAG
);
758 ltype
= VOP_ISLOCKED(dvp
);
760 error
= zfsctl_root(zfsvfs
->z_parent
, LK_SHARED
,
763 cn
.cn_nameptr
= "snapshot";
764 cn
.cn_namelen
= strlen(cn
.cn_nameptr
);
765 cn
.cn_nameiop
= cnp
->cn_nameiop
;
766 cn
.cn_flags
= cnp
->cn_flags
& ~ISDOTDOT
;
767 cn
.cn_lkflags
= cnp
->cn_lkflags
;
768 error
= VOP_LOOKUP(zfsctl_vp
, vpp
, &cn
);
771 vn_lock(dvp
, ltype
| LK_RETRY
);
775 if (zfs_has_ctldir(zdp
) && strcmp(nm
, ZFS_CTLDIR_NAME
) == 0) {
776 zfs_exit(zfsvfs
, FTAG
);
777 if (zfsvfs
->z_show_ctldir
== ZFS_SNAPDIR_DISABLED
)
778 return (SET_ERROR(ENOENT
));
779 if ((cnp
->cn_flags
& ISLASTCN
) != 0 && nameiop
!= LOOKUP
)
780 return (SET_ERROR(ENOTSUP
));
781 error
= zfsctl_root(zfsvfs
, cnp
->cn_lkflags
, vpp
);
786 * The loop is retry the lookup if the parent-child relationship
787 * changes during the dot-dot locking complexities.
792 error
= zfs_dirlook(zdp
, nm
, &zp
);
796 zfs_exit(zfsvfs
, FTAG
);
800 error
= zfs_lookup_lock(dvp
, *vpp
, nm
, cnp
->cn_lkflags
);
803 * If we've got a locking error, then the vnode
804 * got reclaimed because of a force unmount.
805 * We never enter doomed vnodes into the name cache.
811 if ((cnp
->cn_flags
& ISDOTDOT
) == 0)
814 if ((error
= zfs_enter(zfsvfs
, FTAG
)) != 0) {
819 if (zdp
->z_sa_hdl
== NULL
) {
820 error
= SET_ERROR(EIO
);
822 error
= sa_lookup(zdp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
823 &parent
, sizeof (parent
));
826 zfs_exit(zfsvfs
, FTAG
);
830 if (zp
->z_id
== parent
) {
831 zfs_exit(zfsvfs
, FTAG
);
840 /* Translate errors and add SAVENAME when needed. */
841 if (cnp
->cn_flags
& ISLASTCN
) {
845 if (error
== ENOENT
) {
847 #if __FreeBSD_version < 1400068
848 cnp
->cn_flags
|= SAVENAME
;
854 #if __FreeBSD_version < 1400068
856 cnp
->cn_flags
|= SAVENAME
;
862 if ((cnp
->cn_flags
& ISDOTDOT
) != 0) {
864 * FIXME: zfs_lookup_lock relocks vnodes and does nothing to
865 * handle races. In particular different callers may end up
866 * with different vnodes and will try to add conflicting
867 * entries to the namecache.
869 * While finding different result may be acceptable in face
870 * of concurrent modification, adding conflicting entries
871 * trips over an assert in the namecache.
873 * Ultimately let an entry through once everything settles.
875 if (!vn_seqc_consistent(dvp
, dvp_seqc
)) {
876 cnp
->cn_flags
&= ~MAKEENTRY
;
880 /* Insert name into cache (as non-existent) if appropriate. */
881 if (zfsvfs
->z_use_namecache
&& !zfsvfs
->z_replay
&&
882 error
== ENOENT
&& (cnp
->cn_flags
& MAKEENTRY
) != 0)
883 cache_enter(dvp
, NULL
, cnp
);
885 /* Insert name into cache if appropriate. */
886 if (zfsvfs
->z_use_namecache
&& !zfsvfs
->z_replay
&&
887 error
== 0 && (cnp
->cn_flags
& MAKEENTRY
)) {
888 if (!(cnp
->cn_flags
& ISLASTCN
) ||
889 (nameiop
!= DELETE
&& nameiop
!= RENAME
)) {
890 cache_enter(dvp
, *vpp
, cnp
);
898 is_nametoolong(zfsvfs_t
*zfsvfs
, const char *name
)
900 size_t dlen
= strlen(name
);
901 return ((!zfsvfs
->z_longname
&& dlen
>= ZAP_MAXNAMELEN
) ||
902 dlen
>= ZAP_MAXNAMELEN_NEW
);
906 * Attempt to create a new entry in a directory. If the entry
907 * already exists, truncate the file if permissible, else return
908 * an error. Return the vp of the created or trunc'd file.
910 * IN: dvp - vnode of directory to put new file entry in.
911 * name - name of new file entry.
912 * vap - attributes of new file.
913 * excl - flag indicating exclusive or non-exclusive mode.
914 * mode - mode to open file with.
915 * cr - credentials of caller.
916 * flag - large file flag [UNUSED].
917 * ct - caller context
918 * vsecp - ACL to be set
919 * mnt_ns - Unused on FreeBSD
921 * OUT: vpp - vnode of created or trunc'd entry.
923 * RETURN: 0 on success, error code on failure.
926 * dvp - ctime|mtime updated if new entry created
927 * vp - ctime|mtime always, atime if new
930 zfs_create(znode_t
*dzp
, const char *name
, vattr_t
*vap
, int excl
, int mode
,
931 znode_t
**zpp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
, zidmap_t
*mnt_ns
)
933 (void) excl
, (void) mode
, (void) flag
;
935 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
940 uid_t uid
= crgetuid(cr
);
941 gid_t gid
= crgetgid(cr
);
942 uint64_t projid
= ZFS_DEFAULT_PROJID
;
943 zfs_acl_ids_t acl_ids
;
944 boolean_t fuid_dirtied
;
946 #ifdef DEBUG_VFS_LOCKS
947 vnode_t
*dvp
= ZTOV(dzp
);
950 if (is_nametoolong(zfsvfs
, name
))
951 return (SET_ERROR(ENAMETOOLONG
));
954 * If we have an ephemeral id, ACL, or XVATTR then
955 * make sure file system is at proper version
957 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
958 (vsecp
|| (vap
->va_mask
& AT_XVATTR
) ||
959 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
960 return (SET_ERROR(EINVAL
));
962 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
965 zilog
= zfsvfs
->z_log
;
967 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
968 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
969 zfs_exit(zfsvfs
, FTAG
);
970 return (SET_ERROR(EILSEQ
));
973 if (vap
->va_mask
& AT_XVATTR
) {
974 if ((error
= secpolicy_xvattr(ZTOV(dzp
), (xvattr_t
*)vap
,
975 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
976 zfs_exit(zfsvfs
, FTAG
);
983 if ((vap
->va_mode
& S_ISVTX
) && secpolicy_vnode_stky_modify(cr
))
984 vap
->va_mode
&= ~S_ISVTX
;
986 error
= zfs_dirent_lookup(dzp
, name
, &zp
, ZNEW
);
988 zfs_exit(zfsvfs
, FTAG
);
991 ASSERT3P(zp
, ==, NULL
);
994 * Create a new file object and update the directory
997 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, mnt_ns
))) {
1002 * We only support the creation of regular files in
1003 * extended attribute directories.
1006 if ((dzp
->z_pflags
& ZFS_XATTR
) &&
1007 (vap
->va_type
!= VREG
)) {
1008 error
= SET_ERROR(EINVAL
);
1012 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
,
1013 cr
, vsecp
, &acl_ids
, NULL
)) != 0)
1016 if (S_ISREG(vap
->va_mode
) || S_ISDIR(vap
->va_mode
))
1017 projid
= zfs_inherit_projid(dzp
);
1018 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, projid
)) {
1019 zfs_acl_ids_free(&acl_ids
);
1020 error
= SET_ERROR(EDQUOT
);
1024 getnewvnode_reserve();
1026 tx
= dmu_tx_create(os
);
1028 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1029 ZFS_SA_BASE_ATTR_SIZE
);
1031 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1033 zfs_fuid_txhold(zfsvfs
, tx
);
1034 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1035 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1036 if (!zfsvfs
->z_use_sa
&&
1037 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1038 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1039 0, acl_ids
.z_aclp
->z_acl_bytes
);
1041 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1043 zfs_acl_ids_free(&acl_ids
);
1045 getnewvnode_drop_reserve();
1046 zfs_exit(zfsvfs
, FTAG
);
1049 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1051 error
= zfs_link_create(dzp
, name
, zp
, tx
, ZNEW
);
1054 * Since, we failed to add the directory entry for it,
1055 * delete the newly created dnode.
1057 zfs_znode_delete(zp
, tx
);
1058 VOP_UNLOCK(ZTOV(zp
));
1060 zfs_acl_ids_free(&acl_ids
);
1062 getnewvnode_drop_reserve();
1067 zfs_fuid_sync(zfsvfs
, tx
);
1069 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1070 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1071 vsecp
, acl_ids
.z_fuidp
, vap
);
1072 zfs_acl_ids_free(&acl_ids
);
1075 getnewvnode_drop_reserve();
1083 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1084 zil_commit(zilog
, 0);
1086 zfs_exit(zfsvfs
, FTAG
);
1091 * Remove an entry from a directory.
1093 * IN: dvp - vnode of directory to remove entry from.
1094 * name - name of entry to remove.
1095 * cr - credentials of caller.
1096 * ct - caller context
1097 * flags - case flags
1099 * RETURN: 0 on success, error code on failure.
1103 * vp - ctime (if nlink > 0)
1106 zfs_remove_(vnode_t
*dvp
, vnode_t
*vp
, const char *name
, cred_t
*cr
)
1108 znode_t
*dzp
= VTOZ(dvp
);
1111 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1121 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1124 if ((error
= zfs_verify_zp(zp
)) != 0) {
1125 zfs_exit(zfsvfs
, FTAG
);
1128 zilog
= zfsvfs
->z_log
;
1133 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
, NULL
))) {
1138 * Need to use rmdir for removing directories.
1140 if (vp
->v_type
== VDIR
) {
1141 error
= SET_ERROR(EPERM
);
1145 vnevent_remove(vp
, dvp
, name
, ct
);
1149 /* are there any extended attributes? */
1150 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
1151 &xattr_obj
, sizeof (xattr_obj
));
1152 if (error
== 0 && xattr_obj
) {
1153 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
1158 * We may delete the znode now, or we may put it in the unlinked set;
1159 * it depends on whether we're the last link, and on whether there are
1160 * other holds on the vnode. So we dmu_tx_hold() the right things to
1161 * allow for either case.
1163 tx
= dmu_tx_create(zfsvfs
->z_os
);
1164 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1165 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1166 zfs_sa_upgrade_txholds(tx
, zp
);
1167 zfs_sa_upgrade_txholds(tx
, dzp
);
1170 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1171 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1174 /* charge as an update -- would be nice not to charge at all */
1175 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1178 * Mark this transaction as typically resulting in a net free of space
1180 dmu_tx_mark_netfree(tx
);
1182 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1185 zfs_exit(zfsvfs
, FTAG
);
1190 * Remove the directory entry.
1192 error
= zfs_link_destroy(dzp
, name
, zp
, tx
, ZEXISTS
, &unlinked
);
1200 zfs_unlinked_add(zp
, tx
);
1201 vp
->v_vflag
|= VV_NOSYNC
;
1203 /* XXX check changes to linux vnops */
1205 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
, unlinked
);
1213 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1214 zil_commit(zilog
, 0);
1217 zfs_exit(zfsvfs
, FTAG
);
1223 zfs_lookup_internal(znode_t
*dzp
, const char *name
, vnode_t
**vpp
,
1224 struct componentname
*cnp
, int nameiop
)
1226 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1229 cnp
->cn_nameptr
= __DECONST(char *, name
);
1230 cnp
->cn_namelen
= strlen(name
);
1231 cnp
->cn_nameiop
= nameiop
;
1232 cnp
->cn_flags
= ISLASTCN
;
1233 #if __FreeBSD_version < 1400068
1234 cnp
->cn_flags
|= SAVENAME
;
1236 cnp
->cn_lkflags
= LK_EXCLUSIVE
| LK_RETRY
;
1237 cnp
->cn_cred
= kcred
;
1238 #if __FreeBSD_version < 1400037
1239 cnp
->cn_thread
= curthread
;
1242 if (zfsvfs
->z_use_namecache
&& !zfsvfs
->z_replay
) {
1243 struct vop_lookup_args a
;
1245 a
.a_gen
.a_desc
= &vop_lookup_desc
;
1246 a
.a_dvp
= ZTOV(dzp
);
1249 error
= vfs_cache_lookup(&a
);
1251 error
= zfs_lookup(ZTOV(dzp
), name
, vpp
, cnp
, nameiop
, kcred
, 0,
1256 printf("got error %d on name %s on op %d\n", error
, name
,
1265 zfs_remove(znode_t
*dzp
, const char *name
, cred_t
*cr
, int flags
)
1269 struct componentname cn
;
1271 if ((error
= zfs_lookup_internal(dzp
, name
, &vp
, &cn
, DELETE
)))
1274 error
= zfs_remove_(ZTOV(dzp
), vp
, name
, cr
);
1279 * Create a new directory and insert it into dvp using the name
1280 * provided. Return a pointer to the inserted directory.
1282 * IN: dvp - vnode of directory to add subdir to.
1283 * dirname - name of new directory.
1284 * vap - attributes of new directory.
1285 * cr - credentials of caller.
1286 * ct - caller context
1287 * flags - case flags
1288 * vsecp - ACL to be set
1289 * mnt_ns - Unused on FreeBSD
1291 * OUT: vpp - vnode of created directory.
1293 * RETURN: 0 on success, error code on failure.
1296 * dvp - ctime|mtime updated
1297 * vp - ctime|mtime|atime updated
1300 zfs_mkdir(znode_t
*dzp
, const char *dirname
, vattr_t
*vap
, znode_t
**zpp
,
1301 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
, zidmap_t
*mnt_ns
)
1303 (void) flags
, (void) vsecp
;
1305 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1310 uid_t uid
= crgetuid(cr
);
1311 gid_t gid
= crgetgid(cr
);
1312 zfs_acl_ids_t acl_ids
;
1313 boolean_t fuid_dirtied
;
1315 ASSERT3U(vap
->va_type
, ==, VDIR
);
1317 if (is_nametoolong(zfsvfs
, dirname
))
1318 return (SET_ERROR(ENAMETOOLONG
));
1321 * If we have an ephemeral id, ACL, or XVATTR then
1322 * make sure file system is at proper version
1324 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
1325 ((vap
->va_mask
& AT_XVATTR
) ||
1326 IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1327 return (SET_ERROR(EINVAL
));
1329 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1331 zilog
= zfsvfs
->z_log
;
1333 if (dzp
->z_pflags
& ZFS_XATTR
) {
1334 zfs_exit(zfsvfs
, FTAG
);
1335 return (SET_ERROR(EINVAL
));
1338 if (zfsvfs
->z_utf8
&& u8_validate(dirname
,
1339 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1340 zfs_exit(zfsvfs
, FTAG
);
1341 return (SET_ERROR(EILSEQ
));
1344 if (vap
->va_mask
& AT_XVATTR
) {
1345 if ((error
= secpolicy_xvattr(ZTOV(dzp
), (xvattr_t
*)vap
,
1346 crgetuid(cr
), cr
, vap
->va_type
)) != 0) {
1347 zfs_exit(zfsvfs
, FTAG
);
1352 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1353 NULL
, &acl_ids
, NULL
)) != 0) {
1354 zfs_exit(zfsvfs
, FTAG
);
1359 * First make sure the new directory doesn't exist.
1361 * Existence is checked first to make sure we don't return
1362 * EACCES instead of EEXIST which can cause some applications
1367 if ((error
= zfs_dirent_lookup(dzp
, dirname
, &zp
, ZNEW
))) {
1368 zfs_acl_ids_free(&acl_ids
);
1369 zfs_exit(zfsvfs
, FTAG
);
1372 ASSERT3P(zp
, ==, NULL
);
1374 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
,
1376 zfs_acl_ids_free(&acl_ids
);
1377 zfs_exit(zfsvfs
, FTAG
);
1381 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
, zfs_inherit_projid(dzp
))) {
1382 zfs_acl_ids_free(&acl_ids
);
1383 zfs_exit(zfsvfs
, FTAG
);
1384 return (SET_ERROR(EDQUOT
));
1388 * Add a new entry to the directory.
1390 getnewvnode_reserve();
1391 tx
= dmu_tx_create(zfsvfs
->z_os
);
1392 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1393 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1394 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
1396 zfs_fuid_txhold(zfsvfs
, tx
);
1397 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1398 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1399 acl_ids
.z_aclp
->z_acl_bytes
);
1402 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1403 ZFS_SA_BASE_ATTR_SIZE
);
1405 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1407 zfs_acl_ids_free(&acl_ids
);
1409 getnewvnode_drop_reserve();
1410 zfs_exit(zfsvfs
, FTAG
);
1417 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1420 * Now put new name in parent dir.
1422 error
= zfs_link_create(dzp
, dirname
, zp
, tx
, ZNEW
);
1424 zfs_znode_delete(zp
, tx
);
1425 VOP_UNLOCK(ZTOV(zp
));
1431 zfs_fuid_sync(zfsvfs
, tx
);
1435 txtype
= zfs_log_create_txtype(Z_DIR
, NULL
, vap
);
1436 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, NULL
,
1437 acl_ids
.z_fuidp
, vap
);
1440 zfs_acl_ids_free(&acl_ids
);
1444 getnewvnode_drop_reserve();
1446 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1447 zil_commit(zilog
, 0);
1449 zfs_exit(zfsvfs
, FTAG
);
1454 * Remove a directory subdir entry. If the current working
1455 * directory is the same as the subdir to be removed, the
1458 * IN: dvp - vnode of directory to remove from.
1459 * name - name of directory to be removed.
1460 * cwd - vnode of current working directory.
1461 * cr - credentials of caller.
1462 * ct - caller context
1463 * flags - case flags
1465 * RETURN: 0 on success, error code on failure.
1468 * dvp - ctime|mtime updated
1471 zfs_rmdir_(vnode_t
*dvp
, vnode_t
*vp
, const char *name
, cred_t
*cr
)
1473 znode_t
*dzp
= VTOZ(dvp
);
1474 znode_t
*zp
= VTOZ(vp
);
1475 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
1480 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
1482 if ((error
= zfs_verify_zp(zp
)) != 0) {
1483 zfs_exit(zfsvfs
, FTAG
);
1486 zilog
= zfsvfs
->z_log
;
1489 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
, NULL
))) {
1493 if (vp
->v_type
!= VDIR
) {
1494 error
= SET_ERROR(ENOTDIR
);
1498 vnevent_rmdir(vp
, dvp
, name
, ct
);
1500 tx
= dmu_tx_create(zfsvfs
->z_os
);
1501 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1502 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1503 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
1504 zfs_sa_upgrade_txholds(tx
, zp
);
1505 zfs_sa_upgrade_txholds(tx
, dzp
);
1506 dmu_tx_mark_netfree(tx
);
1507 error
= dmu_tx_assign(tx
, TXG_WAIT
);
1510 zfs_exit(zfsvfs
, FTAG
);
1514 error
= zfs_link_destroy(dzp
, name
, zp
, tx
, ZEXISTS
, NULL
);
1517 uint64_t txtype
= TX_RMDIR
;
1518 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
,
1519 ZFS_NO_OBJECT
, B_FALSE
);
1524 if (zfsvfs
->z_use_namecache
)
1525 cache_vop_rmdir(dvp
, vp
);
1527 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1528 zil_commit(zilog
, 0);
1530 zfs_exit(zfsvfs
, FTAG
);
1535 zfs_rmdir(znode_t
*dzp
, const char *name
, znode_t
*cwd
, cred_t
*cr
, int flags
)
1537 struct componentname cn
;
1541 if ((error
= zfs_lookup_internal(dzp
, name
, &vp
, &cn
, DELETE
)))
1544 error
= zfs_rmdir_(ZTOV(dzp
), vp
, name
, cr
);
1550 * Read as many directory entries as will fit into the provided
1551 * buffer from the given directory cursor position (specified in
1552 * the uio structure).
1554 * IN: vp - vnode of directory to read.
1555 * uio - structure supplying read location, range info,
1556 * and return buffer.
1557 * cr - credentials of caller.
1558 * ct - caller context
1560 * OUT: uio - updated offset and range, buffer filled.
1561 * eofp - set to true if end-of-file detected.
1562 * ncookies- number of entries in cookies
1563 * cookies - offsets to directory entries
1565 * RETURN: 0 on success, error code on failure.
1568 * vp - atime updated
1570 * Note that the low 4 bits of the cookie returned by zap is always zero.
1571 * This allows us to use the low range for "special" directory entries:
1572 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1573 * we use the offset 2 for the '.zfs' directory.
1576 zfs_readdir(vnode_t
*vp
, zfs_uio_t
*uio
, cred_t
*cr
, int *eofp
,
1577 int *ncookies
, cookie_t
**cookies
)
1579 znode_t
*zp
= VTOZ(vp
);
1582 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1587 zap_attribute_t
*zap
;
1588 uint_t bytes_wanted
;
1589 uint64_t offset
; /* must be unsigned; checks for < 1 */
1597 cookie_t
*cooks
= NULL
;
1599 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1602 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
1603 &parent
, sizeof (parent
))) != 0) {
1604 zfs_exit(zfsvfs
, FTAG
);
1609 * If we are not given an eof variable,
1616 * Check for valid iov_len.
1618 if (GET_UIO_STRUCT(uio
)->uio_iov
->iov_len
<= 0) {
1619 zfs_exit(zfsvfs
, FTAG
);
1620 return (SET_ERROR(EINVAL
));
1624 * Quit if directory has been removed (posix)
1626 if ((*eofp
= zp
->z_unlinked
) != 0) {
1627 zfs_exit(zfsvfs
, FTAG
);
1633 offset
= zfs_uio_offset(uio
);
1634 prefetch
= zp
->z_zn_prefetch
;
1635 zap
= zap_attribute_long_alloc();
1638 * Initialize the iterator cursor.
1642 * Start iteration from the beginning of the directory.
1644 zap_cursor_init(&zc
, os
, zp
->z_id
);
1647 * The offset is a serialized cursor.
1649 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
1653 * Get space to change directory entries into fs independent format.
1655 iovp
= GET_UIO_STRUCT(uio
)->uio_iov
;
1656 bytes_wanted
= iovp
->iov_len
;
1657 if (zfs_uio_segflg(uio
) != UIO_SYSSPACE
|| zfs_uio_iovcnt(uio
) != 1) {
1658 bufsize
= bytes_wanted
;
1659 outbuf
= kmem_alloc(bufsize
, KM_SLEEP
);
1660 odp
= (struct dirent64
*)outbuf
;
1662 bufsize
= bytes_wanted
;
1664 odp
= (struct dirent64
*)iovp
->iov_base
;
1667 if (ncookies
!= NULL
) {
1669 * Minimum entry size is dirent size and 1 byte for a file name.
1671 ncooks
= zfs_uio_resid(uio
) / (sizeof (struct dirent
) -
1672 sizeof (((struct dirent
*)NULL
)->d_name
) + 1);
1673 cooks
= malloc(ncooks
* sizeof (*cooks
), M_TEMP
, M_WAITOK
);
1679 * Transform to file-system independent format
1682 while (outcount
< bytes_wanted
) {
1685 off64_t
*next
= NULL
;
1688 * Special case `.', `..', and `.zfs'.
1691 (void) strcpy(zap
->za_name
, ".");
1692 zap
->za_normalization_conflict
= 0;
1695 } else if (offset
== 1) {
1696 (void) strcpy(zap
->za_name
, "..");
1697 zap
->za_normalization_conflict
= 0;
1700 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
1701 (void) strcpy(zap
->za_name
, ZFS_CTLDIR_NAME
);
1702 zap
->za_normalization_conflict
= 0;
1703 objnum
= ZFSCTL_INO_ROOT
;
1709 if ((error
= zap_cursor_retrieve(&zc
, zap
))) {
1710 if ((*eofp
= (error
== ENOENT
)) != 0)
1716 if (zap
->za_integer_length
!= 8 ||
1717 zap
->za_num_integers
!= 1) {
1718 cmn_err(CE_WARN
, "zap_readdir: bad directory "
1719 "entry, obj = %lld, offset = %lld\n",
1720 (u_longlong_t
)zp
->z_id
,
1721 (u_longlong_t
)offset
);
1722 error
= SET_ERROR(ENXIO
);
1726 objnum
= ZFS_DIRENT_OBJ(zap
->za_first_integer
);
1728 * MacOS X can extract the object type here such as:
1729 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1731 type
= ZFS_DIRENT_TYPE(zap
->za_first_integer
);
1734 reclen
= DIRENT64_RECLEN(strlen(zap
->za_name
));
1737 * Will this entry fit in the buffer?
1739 if (outcount
+ reclen
> bufsize
) {
1741 * Did we manage to fit anything in the buffer?
1744 error
= SET_ERROR(EINVAL
);
1752 odp
->d_ino
= objnum
;
1753 odp
->d_reclen
= reclen
;
1754 odp
->d_namlen
= strlen(zap
->za_name
);
1755 /* NOTE: d_off is the offset for the *next* entry. */
1757 strlcpy(odp
->d_name
, zap
->za_name
, odp
->d_namlen
+ 1);
1759 dirent_terminate(odp
);
1760 odp
= (dirent64_t
*)((intptr_t)odp
+ reclen
);
1764 ASSERT3S(outcount
, <=, bufsize
);
1767 dmu_prefetch_dnode(os
, objnum
, ZIO_PRIORITY_SYNC_READ
);
1770 * Move to the next entry, fill in the previous offset.
1772 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
1773 zap_cursor_advance(&zc
);
1774 offset
= zap_cursor_serialize(&zc
);
1779 /* Fill the offset right after advancing the cursor. */
1782 if (cooks
!= NULL
) {
1785 KASSERT(ncooks
>= 0, ("ncookies=%d", ncooks
));
1788 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
1790 /* Subtract unused cookies */
1791 if (ncookies
!= NULL
)
1792 *ncookies
-= ncooks
;
1794 if (zfs_uio_segflg(uio
) == UIO_SYSSPACE
&& zfs_uio_iovcnt(uio
) == 1) {
1795 iovp
->iov_base
+= outcount
;
1796 iovp
->iov_len
-= outcount
;
1797 zfs_uio_resid(uio
) -= outcount
;
1799 zfs_uiomove(outbuf
, (long)outcount
, UIO_READ
, uio
))) {
1801 * Reset the pointer.
1803 offset
= zfs_uio_offset(uio
);
1807 zap_cursor_fini(&zc
);
1808 zap_attribute_free(zap
);
1809 if (zfs_uio_segflg(uio
) != UIO_SYSSPACE
|| zfs_uio_iovcnt(uio
) != 1)
1810 kmem_free(outbuf
, bufsize
);
1812 if (error
== ENOENT
)
1815 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
1817 zfs_uio_setoffset(uio
, offset
);
1818 zfs_exit(zfsvfs
, FTAG
);
1819 if (error
!= 0 && cookies
!= NULL
) {
1820 free(*cookies
, M_TEMP
);
1828 * Get the requested file attributes and place them in the provided
1831 * IN: vp - vnode of file.
1832 * vap - va_mask identifies requested attributes.
1833 * If AT_XVATTR set, then optional attrs are requested
1834 * flags - ATTR_NOACLCHECK (CIFS server context)
1835 * cr - credentials of caller.
1837 * OUT: vap - attribute values.
1839 * RETURN: 0 (always succeeds).
1842 zfs_getattr(vnode_t
*vp
, vattr_t
*vap
, int flags
, cred_t
*cr
)
1844 znode_t
*zp
= VTOZ(vp
);
1845 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1848 u_longlong_t nblocks
;
1849 uint64_t mtime
[2], ctime
[2], crtime
[2], rdev
;
1850 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
1851 xoptattr_t
*xoap
= NULL
;
1852 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
1853 sa_bulk_attr_t bulk
[4];
1856 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
1859 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
1861 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
1862 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
1863 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CRTIME(zfsvfs
), NULL
, &crtime
, 16);
1864 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1865 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_RDEV(zfsvfs
), NULL
,
1868 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
1869 zfs_exit(zfsvfs
, FTAG
);
1874 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
1875 * Also, if we are the owner don't bother, since owner should
1876 * always be allowed to read basic attributes of file.
1878 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
1879 (vap
->va_uid
!= crgetuid(cr
))) {
1880 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
1881 skipaclchk
, cr
, NULL
))) {
1882 zfs_exit(zfsvfs
, FTAG
);
1888 * Return all attributes. It's cheaper to provide the answer
1889 * than to determine whether we were asked the question.
1892 vap
->va_type
= IFTOVT(zp
->z_mode
);
1893 vap
->va_mode
= zp
->z_mode
& ~S_IFMT
;
1895 vap
->va_nodeid
= zp
->z_id
;
1896 vap
->va_nlink
= zp
->z_links
;
1897 if ((vp
->v_flag
& VROOT
) && zfs_show_ctldir(zp
) &&
1898 zp
->z_links
< ZFS_LINK_MAX
)
1900 vap
->va_size
= zp
->z_size
;
1901 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1902 vap
->va_rdev
= zfs_cmpldev(rdev
);
1905 vap
->va_gen
= zp
->z_gen
;
1906 vap
->va_flags
= 0; /* FreeBSD: Reset chflags(2) flags. */
1907 vap
->va_filerev
= zp
->z_seq
;
1910 * Add in any requested optional attributes and the create time.
1911 * Also set the corresponding bits in the returned attribute bitmap.
1913 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zfsvfs
->z_use_fuids
) {
1914 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
1916 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
1917 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
1920 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
1921 xoap
->xoa_readonly
=
1922 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
1923 XVA_SET_RTN(xvap
, XAT_READONLY
);
1926 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
1928 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
1929 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
1932 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
1934 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
1935 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
1938 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
1939 xoap
->xoa_nounlink
=
1940 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
1941 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
1944 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
1945 xoap
->xoa_immutable
=
1946 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
1947 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
1950 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
1951 xoap
->xoa_appendonly
=
1952 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
1953 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
1956 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
1958 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
1959 XVA_SET_RTN(xvap
, XAT_NODUMP
);
1962 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
1964 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
1965 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
1968 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
1969 xoap
->xoa_av_quarantined
=
1970 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
1971 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
1974 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
1975 xoap
->xoa_av_modified
=
1976 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
1977 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
1980 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
1981 vp
->v_type
== VREG
) {
1982 zfs_sa_get_scanstamp(zp
, xvap
);
1985 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
1986 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
1987 XVA_SET_RTN(xvap
, XAT_REPARSE
);
1989 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
1990 xoap
->xoa_generation
= zp
->z_gen
;
1991 XVA_SET_RTN(xvap
, XAT_GEN
);
1994 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
1996 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
1997 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2000 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2002 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2003 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2006 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2007 xoap
->xoa_projinherit
=
2008 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0);
2009 XVA_SET_RTN(xvap
, XAT_PROJINHERIT
);
2012 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2013 xoap
->xoa_projid
= zp
->z_projid
;
2014 XVA_SET_RTN(xvap
, XAT_PROJID
);
2018 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2019 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2020 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2021 ZFS_TIME_DECODE(&vap
->va_birthtime
, crtime
);
2024 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2025 vap
->va_blksize
= blksize
;
2026 vap
->va_bytes
= nblocks
<< 9; /* nblocks * 512 */
2028 if (zp
->z_blksz
== 0) {
2030 * Block size hasn't been set; suggest maximal I/O transfers.
2032 vap
->va_blksize
= zfsvfs
->z_max_blksz
;
2035 zfs_exit(zfsvfs
, FTAG
);
2040 * Set the file attributes to the values contained in the
2043 * IN: zp - znode of file to be modified.
2044 * vap - new attribute values.
2045 * If AT_XVATTR set, then optional attrs are being set
2046 * flags - ATTR_UTIME set if non-default time values provided.
2047 * - ATTR_NOACLCHECK (CIFS context only).
2048 * cr - credentials of caller.
2049 * mnt_ns - Unused on FreeBSD
2051 * RETURN: 0 on success, error code on failure.
2054 * vp - ctime updated, mtime updated if size changed.
2057 zfs_setattr(znode_t
*zp
, vattr_t
*vap
, int flags
, cred_t
*cr
, zidmap_t
*mnt_ns
)
2059 vnode_t
*vp
= ZTOV(zp
);
2060 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
2066 uint_t mask
= vap
->va_mask
;
2067 uint_t saved_mask
= 0;
2068 uint64_t saved_mode
;
2071 uint64_t new_uid
, new_gid
;
2073 uint64_t mtime
[2], ctime
[2];
2074 uint64_t projid
= ZFS_INVALID_PROJID
;
2076 int need_policy
= FALSE
;
2078 zfs_fuid_info_t
*fuidp
= NULL
;
2079 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2082 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2083 boolean_t fuid_dirtied
= B_FALSE
;
2084 sa_bulk_attr_t bulk
[7], xattr_bulk
[7];
2085 int count
= 0, xattr_count
= 0;
2090 if (mask
& AT_NOSET
)
2091 return (SET_ERROR(EINVAL
));
2093 if ((err
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
2097 zilog
= zfsvfs
->z_log
;
2100 * Make sure that if we have ephemeral uid/gid or xvattr specified
2101 * that file system is at proper version level
2104 if (zfsvfs
->z_use_fuids
== B_FALSE
&&
2105 (((mask
& AT_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2106 ((mask
& AT_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2107 (mask
& AT_XVATTR
))) {
2108 zfs_exit(zfsvfs
, FTAG
);
2109 return (SET_ERROR(EINVAL
));
2112 if (mask
& AT_SIZE
&& vp
->v_type
== VDIR
) {
2113 zfs_exit(zfsvfs
, FTAG
);
2114 return (SET_ERROR(EISDIR
));
2117 if (mask
& AT_SIZE
&& vp
->v_type
!= VREG
&& vp
->v_type
!= VFIFO
) {
2118 zfs_exit(zfsvfs
, FTAG
);
2119 return (SET_ERROR(EINVAL
));
2123 * If this is an xvattr_t, then get a pointer to the structure of
2124 * optional attributes. If this is NULL, then we have a vattr_t.
2126 xoap
= xva_getxoptattr(xvap
);
2128 xva_init(&tmpxvattr
);
2131 * Immutable files can only alter immutable bit and atime
2133 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2134 ((mask
& (AT_SIZE
|AT_UID
|AT_GID
|AT_MTIME
|AT_MODE
)) ||
2135 ((mask
& AT_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2136 zfs_exit(zfsvfs
, FTAG
);
2137 return (SET_ERROR(EPERM
));
2141 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2145 * Verify timestamps doesn't overflow 32 bits.
2146 * ZFS can handle large timestamps, but 32bit syscalls can't
2147 * handle times greater than 2039. This check should be removed
2148 * once large timestamps are fully supported.
2150 if (mask
& (AT_ATIME
| AT_MTIME
)) {
2151 if (((mask
& AT_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2152 ((mask
& AT_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2153 zfs_exit(zfsvfs
, FTAG
);
2154 return (SET_ERROR(EOVERFLOW
));
2157 if (xoap
!= NULL
&& (mask
& AT_XVATTR
)) {
2158 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) &&
2159 TIMESPEC_OVERFLOW(&vap
->va_birthtime
)) {
2160 zfs_exit(zfsvfs
, FTAG
);
2161 return (SET_ERROR(EOVERFLOW
));
2164 if (XVA_ISSET_REQ(xvap
, XAT_PROJID
)) {
2165 if (!dmu_objset_projectquota_enabled(os
) ||
2166 (!S_ISREG(zp
->z_mode
) && !S_ISDIR(zp
->z_mode
))) {
2167 zfs_exit(zfsvfs
, FTAG
);
2168 return (SET_ERROR(EOPNOTSUPP
));
2171 projid
= xoap
->xoa_projid
;
2172 if (unlikely(projid
== ZFS_INVALID_PROJID
)) {
2173 zfs_exit(zfsvfs
, FTAG
);
2174 return (SET_ERROR(EINVAL
));
2177 if (projid
== zp
->z_projid
&& zp
->z_pflags
& ZFS_PROJID
)
2178 projid
= ZFS_INVALID_PROJID
;
2183 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
) &&
2184 (xoap
->xoa_projinherit
!=
2185 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) &&
2186 (!dmu_objset_projectquota_enabled(os
) ||
2187 (!S_ISREG(zp
->z_mode
) && !S_ISDIR(zp
->z_mode
)))) {
2188 zfs_exit(zfsvfs
, FTAG
);
2189 return (SET_ERROR(EOPNOTSUPP
));
2196 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
2197 zfs_exit(zfsvfs
, FTAG
);
2198 return (SET_ERROR(EROFS
));
2202 * First validate permissions
2205 if (mask
& AT_SIZE
) {
2207 * XXX - Note, we are not providing any open
2208 * mode flags here (like FNDELAY), so we may
2209 * block if there are locks present... this
2210 * should be addressed in openat().
2212 /* XXX - would it be OK to generate a log record here? */
2213 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2215 zfs_exit(zfsvfs
, FTAG
);
2220 if (mask
& (AT_ATIME
|AT_MTIME
) ||
2221 ((mask
& AT_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2222 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2223 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2224 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2225 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2226 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2227 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2228 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2229 skipaclchk
, cr
, mnt_ns
);
2232 if (mask
& (AT_UID
|AT_GID
)) {
2233 int idmask
= (mask
& (AT_UID
|AT_GID
));
2238 * NOTE: even if a new mode is being set,
2239 * we may clear S_ISUID/S_ISGID bits.
2242 if (!(mask
& AT_MODE
))
2243 vap
->va_mode
= zp
->z_mode
;
2246 * Take ownership or chgrp to group we are a member of
2249 take_owner
= (mask
& AT_UID
) && (vap
->va_uid
== crgetuid(cr
));
2250 take_group
= (mask
& AT_GID
) &&
2251 zfs_groupmember(zfsvfs
, vap
->va_gid
, cr
);
2254 * If both AT_UID and AT_GID are set then take_owner and
2255 * take_group must both be set in order to allow taking
2258 * Otherwise, send the check through secpolicy_vnode_setattr()
2262 if (((idmask
== (AT_UID
|AT_GID
)) && take_owner
&& take_group
) ||
2263 ((idmask
== AT_UID
) && take_owner
) ||
2264 ((idmask
== AT_GID
) && take_group
)) {
2265 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2266 skipaclchk
, cr
, mnt_ns
) == 0) {
2268 * Remove setuid/setgid for non-privileged users
2270 secpolicy_setid_clear(vap
, vp
, cr
);
2271 trim_mask
= (mask
& (AT_UID
|AT_GID
));
2280 oldva
.va_mode
= zp
->z_mode
;
2281 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2282 if (mask
& AT_XVATTR
) {
2284 * Update xvattr mask to include only those attributes
2285 * that are actually changing.
2287 * the bits will be restored prior to actually setting
2288 * the attributes so the caller thinks they were set.
2290 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2291 if (xoap
->xoa_appendonly
!=
2292 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2295 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2296 XVA_SET_REQ(&tmpxvattr
, XAT_APPENDONLY
);
2300 if (XVA_ISSET_REQ(xvap
, XAT_PROJINHERIT
)) {
2301 if (xoap
->xoa_projinherit
!=
2302 ((zp
->z_pflags
& ZFS_PROJINHERIT
) != 0)) {
2305 XVA_CLR_REQ(xvap
, XAT_PROJINHERIT
);
2306 XVA_SET_REQ(&tmpxvattr
, XAT_PROJINHERIT
);
2310 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2311 if (xoap
->xoa_nounlink
!=
2312 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2315 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2316 XVA_SET_REQ(&tmpxvattr
, XAT_NOUNLINK
);
2320 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2321 if (xoap
->xoa_immutable
!=
2322 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2325 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2326 XVA_SET_REQ(&tmpxvattr
, XAT_IMMUTABLE
);
2330 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2331 if (xoap
->xoa_nodump
!=
2332 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2335 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2336 XVA_SET_REQ(&tmpxvattr
, XAT_NODUMP
);
2340 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2341 if (xoap
->xoa_av_modified
!=
2342 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2345 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2346 XVA_SET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
);
2350 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2351 if ((vp
->v_type
!= VREG
&&
2352 xoap
->xoa_av_quarantined
) ||
2353 xoap
->xoa_av_quarantined
!=
2354 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2357 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2358 XVA_SET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
);
2362 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2363 zfs_exit(zfsvfs
, FTAG
);
2364 return (SET_ERROR(EPERM
));
2367 if (need_policy
== FALSE
&&
2368 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2369 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2374 if (mask
& AT_MODE
) {
2375 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
,
2377 err
= secpolicy_setid_setsticky_clear(vp
, vap
,
2380 zfs_exit(zfsvfs
, FTAG
);
2383 trim_mask
|= AT_MODE
;
2391 * If trim_mask is set then take ownership
2392 * has been granted or write_acl is present and user
2393 * has the ability to modify mode. In that case remove
2394 * UID|GID and or MODE from mask so that
2395 * secpolicy_vnode_setattr() doesn't revoke it.
2399 saved_mask
= vap
->va_mask
;
2400 vap
->va_mask
&= ~trim_mask
;
2401 if (trim_mask
& AT_MODE
) {
2403 * Save the mode, as secpolicy_vnode_setattr()
2404 * will overwrite it with ova.va_mode.
2406 saved_mode
= vap
->va_mode
;
2409 err
= secpolicy_vnode_setattr(cr
, vp
, vap
, &oldva
, flags
,
2410 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2412 zfs_exit(zfsvfs
, FTAG
);
2417 vap
->va_mask
|= saved_mask
;
2418 if (trim_mask
& AT_MODE
) {
2420 * Recover the mode after
2421 * secpolicy_vnode_setattr().
2423 vap
->va_mode
= saved_mode
;
2429 * secpolicy_vnode_setattr, or take ownership may have
2432 mask
= vap
->va_mask
;
2434 if ((mask
& (AT_UID
| AT_GID
)) || projid
!= ZFS_INVALID_PROJID
) {
2435 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
2436 &xattr_obj
, sizeof (xattr_obj
));
2438 if (err
== 0 && xattr_obj
) {
2439 err
= zfs_zget(zp
->z_zfsvfs
, xattr_obj
, &attrzp
);
2441 err
= vn_lock(ZTOV(attrzp
), LK_EXCLUSIVE
);
2443 vrele(ZTOV(attrzp
));
2448 if (mask
& AT_UID
) {
2449 new_uid
= zfs_fuid_create(zfsvfs
,
2450 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2451 if (new_uid
!= zp
->z_uid
&&
2452 zfs_id_overquota(zfsvfs
, DMU_USERUSED_OBJECT
,
2456 err
= SET_ERROR(EDQUOT
);
2461 if (mask
& AT_GID
) {
2462 new_gid
= zfs_fuid_create(zfsvfs
, (uint64_t)vap
->va_gid
,
2463 cr
, ZFS_GROUP
, &fuidp
);
2464 if (new_gid
!= zp
->z_gid
&&
2465 zfs_id_overquota(zfsvfs
, DMU_GROUPUSED_OBJECT
,
2469 err
= SET_ERROR(EDQUOT
);
2474 if (projid
!= ZFS_INVALID_PROJID
&&
2475 zfs_id_overquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
, projid
)) {
2478 err
= SET_ERROR(EDQUOT
);
2482 tx
= dmu_tx_create(os
);
2484 if (mask
& AT_MODE
) {
2485 uint64_t pmode
= zp
->z_mode
;
2487 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2489 if (zp
->z_zfsvfs
->z_acl_mode
== ZFS_ACL_RESTRICTED
&&
2490 !(zp
->z_pflags
& ZFS_ACL_TRIVIAL
)) {
2491 err
= SET_ERROR(EPERM
);
2495 if ((err
= zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
)))
2498 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2500 * Are we upgrading ACL from old V0 format
2503 if (zfsvfs
->z_version
>= ZPL_VERSION_FUID
&&
2504 zfs_znode_acl_version(zp
) ==
2505 ZFS_ACL_VERSION_INITIAL
) {
2506 dmu_tx_hold_free(tx
, acl_obj
, 0,
2508 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2509 0, aclp
->z_acl_bytes
);
2511 dmu_tx_hold_write(tx
, acl_obj
, 0,
2514 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2515 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2516 0, aclp
->z_acl_bytes
);
2518 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2520 if (((mask
& AT_XVATTR
) &&
2521 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
)) ||
2522 (projid
!= ZFS_INVALID_PROJID
&&
2523 !(zp
->z_pflags
& ZFS_PROJID
)))
2524 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2526 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2530 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2533 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
2535 zfs_fuid_txhold(zfsvfs
, tx
);
2537 zfs_sa_upgrade_txholds(tx
, zp
);
2539 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2545 * Set each attribute requested.
2546 * We group settings according to the locks they need to acquire.
2548 * Note: you cannot set ctime directly, although it will be
2549 * updated as a side-effect of calling this function.
2552 if (projid
!= ZFS_INVALID_PROJID
&& !(zp
->z_pflags
& ZFS_PROJID
)) {
2554 * For the existed object that is upgraded from old system,
2555 * its on-disk layout has no slot for the project ID attribute.
2556 * But quota accounting logic needs to access related slots by
2557 * offset directly. So we need to adjust old objects' layout
2558 * to make the project ID to some unified and fixed offset.
2561 err
= sa_add_projid(attrzp
->z_sa_hdl
, tx
, projid
);
2563 err
= sa_add_projid(zp
->z_sa_hdl
, tx
, projid
);
2565 if (unlikely(err
== EEXIST
))
2570 projid
= ZFS_INVALID_PROJID
;
2573 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2574 mutex_enter(&zp
->z_acl_lock
);
2576 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
2577 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2580 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2581 mutex_enter(&attrzp
->z_acl_lock
);
2582 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2583 SA_ZPL_FLAGS(zfsvfs
), NULL
, &attrzp
->z_pflags
,
2584 sizeof (attrzp
->z_pflags
));
2585 if (projid
!= ZFS_INVALID_PROJID
) {
2586 attrzp
->z_projid
= projid
;
2587 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2588 SA_ZPL_PROJID(zfsvfs
), NULL
, &attrzp
->z_projid
,
2589 sizeof (attrzp
->z_projid
));
2593 if (mask
& (AT_UID
|AT_GID
)) {
2595 if (mask
& AT_UID
) {
2596 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
,
2597 &new_uid
, sizeof (new_uid
));
2598 zp
->z_uid
= new_uid
;
2600 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2601 SA_ZPL_UID(zfsvfs
), NULL
, &new_uid
,
2603 attrzp
->z_uid
= new_uid
;
2607 if (mask
& AT_GID
) {
2608 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
),
2609 NULL
, &new_gid
, sizeof (new_gid
));
2610 zp
->z_gid
= new_gid
;
2612 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2613 SA_ZPL_GID(zfsvfs
), NULL
, &new_gid
,
2615 attrzp
->z_gid
= new_gid
;
2618 if (!(mask
& AT_MODE
)) {
2619 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
),
2620 NULL
, &new_mode
, sizeof (new_mode
));
2621 new_mode
= zp
->z_mode
;
2623 err
= zfs_acl_chown_setattr(zp
);
2626 vn_seqc_write_begin(ZTOV(attrzp
));
2627 err
= zfs_acl_chown_setattr(attrzp
);
2628 vn_seqc_write_end(ZTOV(attrzp
));
2633 if (mask
& AT_MODE
) {
2634 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
,
2635 &new_mode
, sizeof (new_mode
));
2636 zp
->z_mode
= new_mode
;
2637 ASSERT3P(aclp
, !=, NULL
);
2638 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2640 if (zp
->z_acl_cached
)
2641 zfs_acl_free(zp
->z_acl_cached
);
2642 zp
->z_acl_cached
= aclp
;
2647 if (mask
& AT_ATIME
) {
2648 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
2649 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
2650 &zp
->z_atime
, sizeof (zp
->z_atime
));
2653 if (mask
& AT_MTIME
) {
2654 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2655 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
2656 mtime
, sizeof (mtime
));
2659 if (projid
!= ZFS_INVALID_PROJID
) {
2660 zp
->z_projid
= projid
;
2661 SA_ADD_BULK_ATTR(bulk
, count
,
2662 SA_ZPL_PROJID(zfsvfs
), NULL
, &zp
->z_projid
,
2663 sizeof (zp
->z_projid
));
2666 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2667 if (mask
& AT_SIZE
&& !(mask
& AT_MTIME
)) {
2668 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
),
2669 NULL
, mtime
, sizeof (mtime
));
2670 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2671 &ctime
, sizeof (ctime
));
2672 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
2673 } else if (mask
!= 0) {
2674 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
2675 &ctime
, sizeof (ctime
));
2676 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
);
2678 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2679 SA_ZPL_CTIME(zfsvfs
), NULL
,
2680 &ctime
, sizeof (ctime
));
2681 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2687 * Do this after setting timestamps to prevent timestamp
2688 * update from toggling bit
2691 if (xoap
&& (mask
& AT_XVATTR
)) {
2693 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
))
2694 xoap
->xoa_createtime
= vap
->va_birthtime
;
2696 * restore trimmed off masks
2697 * so that return masks can be set for caller.
2700 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_APPENDONLY
)) {
2701 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2703 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NOUNLINK
)) {
2704 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2706 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_IMMUTABLE
)) {
2707 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2709 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_NODUMP
)) {
2710 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2712 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_MODIFIED
)) {
2713 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2715 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_AV_QUARANTINED
)) {
2716 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2718 if (XVA_ISSET_REQ(&tmpxvattr
, XAT_PROJINHERIT
)) {
2719 XVA_SET_REQ(xvap
, XAT_PROJINHERIT
);
2722 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2723 ASSERT3S(vp
->v_type
, ==, VREG
);
2725 zfs_xvattr_set(zp
, xvap
, tx
);
2729 zfs_fuid_sync(zfsvfs
, tx
);
2732 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2734 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2735 mutex_exit(&zp
->z_acl_lock
);
2738 if (mask
& (AT_UID
|AT_GID
|AT_MODE
))
2739 mutex_exit(&attrzp
->z_acl_lock
);
2742 if (err
== 0 && attrzp
) {
2743 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
2755 zfs_fuid_info_free(fuidp
);
2762 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2767 if (os
->os_sync
== ZFS_SYNC_ALWAYS
)
2768 zil_commit(zilog
, 0);
2770 zfs_exit(zfsvfs
, FTAG
);
2775 * Look up the directory entries corresponding to the source and target
2776 * directory/name pairs.
2779 zfs_rename_relock_lookup(znode_t
*sdzp
, const struct componentname
*scnp
,
2780 znode_t
**szpp
, znode_t
*tdzp
, const struct componentname
*tcnp
,
2788 * Before using sdzp and tdzp we must ensure that they are live.
2789 * As a porting legacy from illumos we have two things to worry
2790 * about. One is typical for FreeBSD and it is that the vnode is
2791 * not reclaimed (doomed). The other is that the znode is live.
2792 * The current code can invalidate the znode without acquiring the
2793 * corresponding vnode lock if the object represented by the znode
2794 * and vnode is no longer valid after a rollback or receive operation.
2795 * z_teardown_lock hidden behind zfs_enter and zfs_exit is the lock
2796 * that protects the znodes from the invalidation.
2798 zfsvfs
= sdzp
->z_zfsvfs
;
2799 ASSERT3P(zfsvfs
, ==, tdzp
->z_zfsvfs
);
2800 if ((error
= zfs_enter_verify_zp(zfsvfs
, sdzp
, FTAG
)) != 0)
2802 if ((error
= zfs_verify_zp(tdzp
)) != 0) {
2803 zfs_exit(zfsvfs
, FTAG
);
2808 * Re-resolve svp to be certain it still exists and fetch the
2811 error
= zfs_dirent_lookup(sdzp
, scnp
->cn_nameptr
, &szp
, ZEXISTS
);
2813 /* Source entry invalid or not there. */
2814 if ((scnp
->cn_flags
& ISDOTDOT
) != 0 ||
2815 (scnp
->cn_namelen
== 1 && scnp
->cn_nameptr
[0] == '.'))
2816 error
= SET_ERROR(EINVAL
);
2822 * Re-resolve tvp, if it disappeared we just carry on.
2824 error
= zfs_dirent_lookup(tdzp
, tcnp
->cn_nameptr
, &tzp
, 0);
2827 if ((tcnp
->cn_flags
& ISDOTDOT
) != 0)
2828 error
= SET_ERROR(EINVAL
);
2833 zfs_exit(zfsvfs
, FTAG
);
2838 * We acquire all but fdvp locks using non-blocking acquisitions. If we
2839 * fail to acquire any lock in the path we will drop all held locks,
2840 * acquire the new lock in a blocking fashion, and then release it and
2841 * restart the rename. This acquire/release step ensures that we do not
2842 * spin on a lock waiting for release. On error release all vnode locks
2843 * and decrement references the way tmpfs_rename() would do.
2846 zfs_rename_relock(struct vnode
*sdvp
, struct vnode
**svpp
,
2847 struct vnode
*tdvp
, struct vnode
**tvpp
,
2848 const struct componentname
*scnp
, const struct componentname
*tcnp
)
2850 struct vnode
*nvp
, *svp
, *tvp
;
2851 znode_t
*sdzp
, *tdzp
, *szp
, *tzp
;
2855 if (*tvpp
!= NULL
&& *tvpp
!= tdvp
)
2859 error
= vn_lock(sdvp
, LK_EXCLUSIVE
);
2862 error
= vn_lock(tdvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
2867 error
= vn_lock(tdvp
, LK_EXCLUSIVE
);
2876 error
= zfs_rename_relock_lookup(sdzp
, scnp
, &szp
, tdzp
, tcnp
, &tzp
);
2883 tvp
= tzp
!= NULL
? ZTOV(tzp
) : NULL
;
2886 * Now try acquire locks on svp and tvp.
2889 error
= vn_lock(nvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
2895 if (error
!= EBUSY
) {
2899 error
= vn_lock(nvp
, LK_EXCLUSIVE
);
2906 * Concurrent rename race.
2911 error
= SET_ERROR(EINVAL
);
2926 error
= vn_lock(nvp
, LK_EXCLUSIVE
| LK_NOWAIT
);
2931 if (error
!= EBUSY
) {
2935 error
= vn_lock(nvp
, LK_EXCLUSIVE
);
2953 * Note that we must use VRELE_ASYNC in this function as it walks
2954 * up the directory tree and vrele may need to acquire an exclusive
2955 * lock if a last reference to a vnode is dropped.
2958 zfs_rename_check(znode_t
*szp
, znode_t
*sdzp
, znode_t
*tdzp
)
2965 zfsvfs
= tdzp
->z_zfsvfs
;
2967 return (SET_ERROR(EINVAL
));
2970 if (tdzp
->z_id
== zfsvfs
->z_root
)
2974 ASSERT(!zp
->z_unlinked
);
2975 if ((error
= sa_lookup(zp
->z_sa_hdl
,
2976 SA_ZPL_PARENT(zfsvfs
), &parent
, sizeof (parent
))) != 0)
2979 if (parent
== szp
->z_id
) {
2980 error
= SET_ERROR(EINVAL
);
2983 if (parent
== zfsvfs
->z_root
)
2985 if (parent
== sdzp
->z_id
)
2988 error
= zfs_zget(zfsvfs
, parent
, &zp1
);
2993 VN_RELE_ASYNC(ZTOV(zp
),
2994 dsl_pool_zrele_taskq(
2995 dmu_objset_pool(zfsvfs
->z_os
)));
2999 if (error
== ENOTDIR
)
3000 panic("checkpath: .. not a directory\n");
3002 VN_RELE_ASYNC(ZTOV(zp
),
3003 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs
->z_os
)));
3008 zfs_do_rename_impl(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3009 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3013 * Move an entry from the provided source directory to the target
3014 * directory. Change the entry name as indicated.
3016 * IN: sdvp - Source directory containing the "old entry".
3017 * scnp - Old entry name.
3018 * tdvp - Target directory to contain the "new entry".
3019 * tcnp - New entry name.
3020 * cr - credentials of caller.
3021 * INOUT: svpp - Source file
3022 * tvpp - Target file, may point to NULL initially
3024 * RETURN: 0 on success, error code on failure.
3027 * sdvp,tdvp - ctime|mtime updated
3030 zfs_do_rename(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3031 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3036 ASSERT_VOP_ELOCKED(tdvp
, __func__
);
3038 ASSERT_VOP_ELOCKED(*tvpp
, __func__
);
3040 /* Reject renames across filesystems. */
3041 if ((*svpp
)->v_mount
!= tdvp
->v_mount
||
3042 ((*tvpp
) != NULL
&& (*svpp
)->v_mount
!= (*tvpp
)->v_mount
)) {
3043 error
= SET_ERROR(EXDEV
);
3047 if (zfsctl_is_node(tdvp
)) {
3048 error
= SET_ERROR(EXDEV
);
3053 * Lock all four vnodes to ensure safety and semantics of renaming.
3055 error
= zfs_rename_relock(sdvp
, svpp
, tdvp
, tvpp
, scnp
, tcnp
);
3057 /* no vnodes are locked in the case of error here */
3061 error
= zfs_do_rename_impl(sdvp
, svpp
, scnp
, tdvp
, tvpp
, tcnp
, cr
);
3074 zfs_do_rename_impl(vnode_t
*sdvp
, vnode_t
**svpp
, struct componentname
*scnp
,
3075 vnode_t
*tdvp
, vnode_t
**tvpp
, struct componentname
*tcnp
,
3081 znode_t
*tdzp
, *sdzp
, *tzp
, *szp
;
3082 const char *snm
= scnp
->cn_nameptr
;
3083 const char *tnm
= tcnp
->cn_nameptr
;
3088 zfsvfs
= tdzp
->z_zfsvfs
;
3090 if ((error
= zfs_enter_verify_zp(zfsvfs
, tdzp
, FTAG
)) != 0)
3092 if ((error
= zfs_verify_zp(sdzp
)) != 0) {
3093 zfs_exit(zfsvfs
, FTAG
);
3096 zilog
= zfsvfs
->z_log
;
3098 if (zfsvfs
->z_utf8
&& u8_validate(tnm
,
3099 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3100 error
= SET_ERROR(EILSEQ
);
3104 /* If source and target are the same file, there is nothing to do. */
3105 if ((*svpp
) == (*tvpp
)) {
3110 if (((*svpp
)->v_type
== VDIR
&& (*svpp
)->v_mountedhere
!= NULL
) ||
3111 ((*tvpp
) != NULL
&& (*tvpp
)->v_type
== VDIR
&&
3112 (*tvpp
)->v_mountedhere
!= NULL
)) {
3113 error
= SET_ERROR(EXDEV
);
3118 if ((error
= zfs_verify_zp(szp
)) != 0) {
3119 zfs_exit(zfsvfs
, FTAG
);
3122 tzp
= *tvpp
== NULL
? NULL
: VTOZ(*tvpp
);
3124 if ((error
= zfs_verify_zp(tzp
)) != 0) {
3125 zfs_exit(zfsvfs
, FTAG
);
3131 * This is to prevent the creation of links into attribute space
3132 * by renaming a linked file into/outof an attribute directory.
3133 * See the comment in zfs_link() for why this is considered bad.
3135 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3136 error
= SET_ERROR(EINVAL
);
3141 * If we are using project inheritance, means if the directory has
3142 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3143 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3144 * such case, we only allow renames into our tree when the project
3147 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3148 tdzp
->z_projid
!= szp
->z_projid
) {
3149 error
= SET_ERROR(EXDEV
);
3154 * Must have write access at the source to remove the old entry
3155 * and write access at the target to create the new entry.
3156 * Note that if target and source are the same, this can be
3157 * done in a single check.
3159 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
, NULL
)))
3162 if ((*svpp
)->v_type
== VDIR
) {
3164 * Avoid ".", "..", and aliases of "." for obvious reasons.
3166 if ((scnp
->cn_namelen
== 1 && scnp
->cn_nameptr
[0] == '.') ||
3168 (scnp
->cn_flags
| tcnp
->cn_flags
) & ISDOTDOT
) {
3174 * Check to make sure rename is valid.
3175 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3177 if ((error
= zfs_rename_check(szp
, sdzp
, tdzp
)))
3182 * Does target exist?
3186 * Source and target must be the same type.
3188 if ((*svpp
)->v_type
== VDIR
) {
3189 if ((*tvpp
)->v_type
!= VDIR
) {
3190 error
= SET_ERROR(ENOTDIR
);
3198 if ((*tvpp
)->v_type
== VDIR
) {
3199 error
= SET_ERROR(EISDIR
);
3205 vn_seqc_write_begin(*svpp
);
3206 vn_seqc_write_begin(sdvp
);
3208 vn_seqc_write_begin(*tvpp
);
3210 vn_seqc_write_begin(tdvp
);
3212 vnevent_rename_src(*svpp
, sdvp
, scnp
->cn_nameptr
, ct
);
3214 vnevent_rename_dest(*tvpp
, tdvp
, tnm
, ct
);
3217 * notify the target directory if it is not the same
3218 * as source directory.
3221 vnevent_rename_dest_dir(tdvp
, ct
);
3224 tx
= dmu_tx_create(zfsvfs
->z_os
);
3225 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3226 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3227 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3228 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3230 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3231 zfs_sa_upgrade_txholds(tx
, tdzp
);
3234 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3235 zfs_sa_upgrade_txholds(tx
, tzp
);
3238 zfs_sa_upgrade_txholds(tx
, szp
);
3239 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
3240 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3246 if (tzp
) /* Attempt to remove the existing target */
3247 error
= zfs_link_destroy(tdzp
, tnm
, tzp
, tx
, 0, NULL
);
3250 error
= zfs_link_create(tdzp
, tnm
, szp
, tx
, ZRENAMING
);
3252 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3254 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
3255 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3258 error
= zfs_link_destroy(sdzp
, snm
, szp
, tx
, ZRENAMING
,
3261 zfs_log_rename(zilog
, tx
, TX_RENAME
, sdzp
,
3262 snm
, tdzp
, tnm
, szp
);
3265 * At this point, we have successfully created
3266 * the target name, but have failed to remove
3267 * the source name. Since the create was done
3268 * with the ZRENAMING flag, there are
3269 * complications; for one, the link count is
3270 * wrong. The easiest way to deal with this
3271 * is to remove the newly created target, and
3272 * return the original error. This must
3273 * succeed; fortunately, it is very unlikely to
3274 * fail, since we just created it.
3276 VERIFY0(zfs_link_destroy(tdzp
, tnm
, szp
, tx
,
3281 cache_vop_rename(sdvp
, *svpp
, tdvp
, *tvpp
, scnp
, tcnp
);
3288 vn_seqc_write_end(*svpp
);
3289 vn_seqc_write_end(sdvp
);
3291 vn_seqc_write_end(*tvpp
);
3293 vn_seqc_write_end(tdvp
);
3296 if (error
== 0 && zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3297 zil_commit(zilog
, 0);
3298 zfs_exit(zfsvfs
, FTAG
);
3304 zfs_rename(znode_t
*sdzp
, const char *sname
, znode_t
*tdzp
, const char *tname
,
3305 cred_t
*cr
, int flags
, uint64_t rflags
, vattr_t
*wo_vap
, zidmap_t
*mnt_ns
)
3307 struct componentname scn
, tcn
;
3308 vnode_t
*sdvp
, *tdvp
;
3313 if (is_nametoolong(tdzp
->z_zfsvfs
, tname
))
3314 return (SET_ERROR(ENAMETOOLONG
));
3316 if (rflags
!= 0 || wo_vap
!= NULL
)
3317 return (SET_ERROR(EINVAL
));
3321 error
= zfs_lookup_internal(sdzp
, sname
, &svp
, &scn
, DELETE
);
3322 if (sdzp
->z_zfsvfs
->z_replay
== B_FALSE
)
3328 vn_lock(tdvp
, LK_EXCLUSIVE
| LK_RETRY
);
3329 error
= zfs_lookup_internal(tdzp
, tname
, &tvp
, &tcn
, RENAME
);
3330 if (error
== EJUSTRETURN
)
3332 else if (error
!= 0) {
3337 error
= zfs_do_rename(sdvp
, &svp
, &scn
, tdvp
, &tvp
, &tcn
, cr
);
3348 * Insert the indicated symbolic reference entry into the directory.
3350 * IN: dvp - Directory to contain new symbolic link.
3351 * link - Name for new symlink entry.
3352 * vap - Attributes of new entry.
3353 * cr - credentials of caller.
3354 * ct - caller context
3355 * flags - case flags
3356 * mnt_ns - Unused on FreeBSD
3358 * RETURN: 0 on success, error code on failure.
3361 * dvp - ctime|mtime updated
3364 zfs_symlink(znode_t
*dzp
, const char *name
, vattr_t
*vap
,
3365 const char *link
, znode_t
**zpp
, cred_t
*cr
, int flags
, zidmap_t
*mnt_ns
)
3370 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
3372 uint64_t len
= strlen(link
);
3374 zfs_acl_ids_t acl_ids
;
3375 boolean_t fuid_dirtied
;
3376 uint64_t txtype
= TX_SYMLINK
;
3378 ASSERT3S(vap
->va_type
, ==, VLNK
);
3380 if (is_nametoolong(zfsvfs
, name
))
3381 return (SET_ERROR(ENAMETOOLONG
));
3383 if ((error
= zfs_enter_verify_zp(zfsvfs
, dzp
, FTAG
)) != 0)
3385 zilog
= zfsvfs
->z_log
;
3387 if (zfsvfs
->z_utf8
&& u8_validate(name
, strlen(name
),
3388 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3389 zfs_exit(zfsvfs
, FTAG
);
3390 return (SET_ERROR(EILSEQ
));
3393 if (len
> MAXPATHLEN
) {
3394 zfs_exit(zfsvfs
, FTAG
);
3395 return (SET_ERROR(ENAMETOOLONG
));
3398 if ((error
= zfs_acl_ids_create(dzp
, 0,
3399 vap
, cr
, NULL
, &acl_ids
, NULL
)) != 0) {
3400 zfs_exit(zfsvfs
, FTAG
);
3405 * Attempt to lock directory; fail if entry already exists.
3407 error
= zfs_dirent_lookup(dzp
, name
, &zp
, ZNEW
);
3409 zfs_acl_ids_free(&acl_ids
);
3410 zfs_exit(zfsvfs
, FTAG
);
3414 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, mnt_ns
))) {
3415 zfs_acl_ids_free(&acl_ids
);
3416 zfs_exit(zfsvfs
, FTAG
);
3420 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
,
3422 zfs_acl_ids_free(&acl_ids
);
3423 zfs_exit(zfsvfs
, FTAG
);
3424 return (SET_ERROR(EDQUOT
));
3427 getnewvnode_reserve();
3428 tx
= dmu_tx_create(zfsvfs
->z_os
);
3429 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
3430 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3431 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3432 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3433 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3434 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3435 if (!zfsvfs
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3436 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3437 acl_ids
.z_aclp
->z_acl_bytes
);
3440 zfs_fuid_txhold(zfsvfs
, tx
);
3441 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3443 zfs_acl_ids_free(&acl_ids
);
3445 getnewvnode_drop_reserve();
3446 zfs_exit(zfsvfs
, FTAG
);
3451 * Create a new object for the symlink.
3452 * for version 4 ZPL datasets the symlink will be an SA attribute
3454 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3457 zfs_fuid_sync(zfsvfs
, tx
);
3460 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zfsvfs
),
3461 __DECONST(void *, link
), len
, tx
);
3463 zfs_sa_symlink(zp
, __DECONST(char *, link
), len
, tx
);
3466 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
3467 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3469 * Insert the new object into the directory.
3471 error
= zfs_link_create(dzp
, name
, zp
, tx
, ZNEW
);
3473 zfs_znode_delete(zp
, tx
);
3474 VOP_UNLOCK(ZTOV(zp
));
3477 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3480 zfs_acl_ids_free(&acl_ids
);
3484 getnewvnode_drop_reserve();
3489 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3490 zil_commit(zilog
, 0);
3493 zfs_exit(zfsvfs
, FTAG
);
3498 * Return, in the buffer contained in the provided uio structure,
3499 * the symbolic path referred to by vp.
3501 * IN: vp - vnode of symbolic link.
3502 * uio - structure to contain the link path.
3503 * cr - credentials of caller.
3504 * ct - caller context
3506 * OUT: uio - structure containing the link path.
3508 * RETURN: 0 on success, error code on failure.
3511 * vp - atime updated
3514 zfs_readlink(vnode_t
*vp
, zfs_uio_t
*uio
, cred_t
*cr
, caller_context_t
*ct
)
3516 (void) cr
, (void) ct
;
3517 znode_t
*zp
= VTOZ(vp
);
3518 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3521 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3525 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3526 SA_ZPL_SYMLINK(zfsvfs
), uio
);
3528 error
= zfs_sa_readlink(zp
, uio
);
3530 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3532 zfs_exit(zfsvfs
, FTAG
);
3537 * Insert a new entry into directory tdvp referencing svp.
3539 * IN: tdvp - Directory to contain new entry.
3540 * svp - vnode of new entry.
3541 * name - name of new entry.
3542 * cr - credentials of caller.
3544 * RETURN: 0 on success, error code on failure.
3547 * tdvp - ctime|mtime updated
3548 * svp - ctime updated
3551 zfs_link(znode_t
*tdzp
, znode_t
*szp
, const char *name
, cred_t
*cr
,
3556 zfsvfs_t
*zfsvfs
= tdzp
->z_zfsvfs
;
3563 ASSERT3S(ZTOV(tdzp
)->v_type
, ==, VDIR
);
3565 if (is_nametoolong(zfsvfs
, name
))
3566 return (SET_ERROR(ENAMETOOLONG
));
3568 if ((error
= zfs_enter_verify_zp(zfsvfs
, tdzp
, FTAG
)) != 0)
3570 zilog
= zfsvfs
->z_log
;
3573 * POSIX dictates that we return EPERM here.
3574 * Better choices include ENOTSUP or EISDIR.
3576 if (ZTOV(szp
)->v_type
== VDIR
) {
3577 zfs_exit(zfsvfs
, FTAG
);
3578 return (SET_ERROR(EPERM
));
3581 if ((error
= zfs_verify_zp(szp
)) != 0) {
3582 zfs_exit(zfsvfs
, FTAG
);
3587 * If we are using project inheritance, means if the directory has
3588 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3589 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3590 * such case, we only allow hard link creation in our tree when the
3591 * project IDs are the same.
3593 if (tdzp
->z_pflags
& ZFS_PROJINHERIT
&&
3594 tdzp
->z_projid
!= szp
->z_projid
) {
3595 zfs_exit(zfsvfs
, FTAG
);
3596 return (SET_ERROR(EXDEV
));
3599 if (szp
->z_pflags
& (ZFS_APPENDONLY
|
3600 ZFS_IMMUTABLE
| ZFS_READONLY
)) {
3601 zfs_exit(zfsvfs
, FTAG
);
3602 return (SET_ERROR(EPERM
));
3605 /* Prevent links to .zfs/shares files */
3607 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
3608 &parent
, sizeof (uint64_t))) != 0) {
3609 zfs_exit(zfsvfs
, FTAG
);
3612 if (parent
== zfsvfs
->z_shares_dir
) {
3613 zfs_exit(zfsvfs
, FTAG
);
3614 return (SET_ERROR(EPERM
));
3617 if (zfsvfs
->z_utf8
&& u8_validate(name
,
3618 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3619 zfs_exit(zfsvfs
, FTAG
);
3620 return (SET_ERROR(EILSEQ
));
3624 * We do not support links between attributes and non-attributes
3625 * because of the potential security risk of creating links
3626 * into "normal" file space in order to circumvent restrictions
3627 * imposed in attribute space.
3629 if ((szp
->z_pflags
& ZFS_XATTR
) != (tdzp
->z_pflags
& ZFS_XATTR
)) {
3630 zfs_exit(zfsvfs
, FTAG
);
3631 return (SET_ERROR(EINVAL
));
3635 owner
= zfs_fuid_map_id(zfsvfs
, szp
->z_uid
, cr
, ZFS_OWNER
);
3636 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(ZTOV(szp
), cr
) != 0) {
3637 zfs_exit(zfsvfs
, FTAG
);
3638 return (SET_ERROR(EPERM
));
3641 if ((error
= zfs_zaccess(tdzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
, NULL
))) {
3642 zfs_exit(zfsvfs
, FTAG
);
3647 * Attempt to lock directory; fail if entry already exists.
3649 error
= zfs_dirent_lookup(tdzp
, name
, &tzp
, ZNEW
);
3651 zfs_exit(zfsvfs
, FTAG
);
3655 tx
= dmu_tx_create(zfsvfs
->z_os
);
3656 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3657 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, name
);
3658 zfs_sa_upgrade_txholds(tx
, szp
);
3659 zfs_sa_upgrade_txholds(tx
, tdzp
);
3660 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3663 zfs_exit(zfsvfs
, FTAG
);
3667 error
= zfs_link_create(tdzp
, name
, szp
, tx
, 0);
3670 uint64_t txtype
= TX_LINK
;
3671 zfs_log_link(zilog
, tx
, txtype
, tdzp
, szp
, name
);
3677 vnevent_link(ZTOV(szp
), ct
);
3680 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3681 zil_commit(zilog
, 0);
3683 zfs_exit(zfsvfs
, FTAG
);
3688 * Free or allocate space in a file. Currently, this function only
3689 * supports the `F_FREESP' command. However, this command is somewhat
3690 * misnamed, as its functionality includes the ability to allocate as
3691 * well as free space.
3693 * IN: ip - inode of file to free data in.
3694 * cmd - action to take (only F_FREESP supported).
3695 * bfp - section of file to free/alloc.
3696 * flag - current file open mode flags.
3697 * offset - current file offset.
3698 * cr - credentials of caller.
3700 * RETURN: 0 on success, error code on failure.
3703 * ip - ctime|mtime updated
3706 zfs_space(znode_t
*zp
, int cmd
, flock64_t
*bfp
, int flag
,
3707 offset_t offset
, cred_t
*cr
)
3710 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
3714 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3717 if (cmd
!= F_FREESP
) {
3718 zfs_exit(zfsvfs
, FTAG
);
3719 return (SET_ERROR(EINVAL
));
3723 * Callers might not be able to detect properly that we are read-only,
3724 * so check it explicitly here.
3726 if (zfs_is_readonly(zfsvfs
)) {
3727 zfs_exit(zfsvfs
, FTAG
);
3728 return (SET_ERROR(EROFS
));
3731 if (bfp
->l_len
< 0) {
3732 zfs_exit(zfsvfs
, FTAG
);
3733 return (SET_ERROR(EINVAL
));
3737 * Permissions aren't checked on Solaris because on this OS
3738 * zfs_space() can only be called with an opened file handle.
3739 * On Linux we can get here through truncate_range() which
3740 * operates directly on inodes, so we need to check access rights.
3742 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
, NULL
))) {
3743 zfs_exit(zfsvfs
, FTAG
);
3748 len
= bfp
->l_len
; /* 0 means from off to end of file */
3750 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
3752 zfs_exit(zfsvfs
, FTAG
);
3757 zfs_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
3759 (void) cr
, (void) ct
;
3760 znode_t
*zp
= VTOZ(vp
);
3761 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3764 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs
);
3765 if (zp
->z_sa_hdl
== NULL
) {
3767 * The fs has been unmounted, or we did a
3768 * suspend/resume and this file no longer exists.
3770 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3775 if (zp
->z_unlinked
) {
3777 * Fast path to recycle a vnode of a removed file.
3779 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3784 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
3785 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
3787 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3788 zfs_sa_upgrade_txholds(tx
, zp
);
3789 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3793 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zfsvfs
),
3794 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
3795 zp
->z_atime_dirty
= 0;
3799 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
3803 _Static_assert(sizeof (struct zfid_short
) <= sizeof (struct fid
),
3804 "struct zfid_short bigger than struct fid");
3805 _Static_assert(sizeof (struct zfid_long
) <= sizeof (struct fid
),
3806 "struct zfid_long bigger than struct fid");
3809 zfs_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
3812 znode_t
*zp
= VTOZ(vp
);
3813 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3816 uint64_t object
= zp
->z_id
;
3820 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3823 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
),
3824 &gen64
, sizeof (uint64_t))) != 0) {
3825 zfs_exit(zfsvfs
, FTAG
);
3829 gen
= (uint32_t)gen64
;
3831 size
= (zfsvfs
->z_parent
!= zfsvfs
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
3832 fidp
->fid_len
= size
;
3834 zfid
= (zfid_short_t
*)fidp
;
3836 zfid
->zf_len
= size
;
3838 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
3839 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
3841 /* Must have a non-zero generation number to distinguish from .zfs */
3844 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
3845 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
3847 if (size
== LONG_FID_LEN
) {
3848 uint64_t objsetid
= dmu_objset_id(zfsvfs
->z_os
);
3851 zlfid
= (zfid_long_t
*)fidp
;
3853 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
3854 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
3856 /* XXX - this should be the generation number for the objset */
3857 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
3858 zlfid
->zf_setgen
[i
] = 0;
3861 zfs_exit(zfsvfs
, FTAG
);
3866 zfs_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
3867 caller_context_t
*ct
)
3875 *valp
= MIN(LONG_MAX
, ZFS_LINK_MAX
);
3878 case _PC_FILESIZEBITS
:
3881 case _PC_MIN_HOLE_SIZE
:
3882 *valp
= (int)SPA_MINBLOCKSIZE
;
3884 case _PC_ACL_EXTENDED
:
3885 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
3887 zfsvfs
= zp
->z_zfsvfs
;
3888 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3890 *valp
= zfsvfs
->z_acl_type
== ZFSACLTYPE_POSIX
? 1 : 0;
3891 zfs_exit(zfsvfs
, FTAG
);
3899 zfsvfs
= zp
->z_zfsvfs
;
3900 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
3902 *valp
= zfsvfs
->z_acl_type
== ZFS_ACLTYPE_NFSV4
? 1 : 0;
3903 zfs_exit(zfsvfs
, FTAG
);
3906 case _PC_ACL_PATH_MAX
:
3907 *valp
= ACL_MAX_ENTRIES
;
3911 return (EOPNOTSUPP
);
3916 zfs_getpages(struct vnode
*vp
, vm_page_t
*ma
, int count
, int *rbehind
,
3919 znode_t
*zp
= VTOZ(vp
);
3920 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3921 zfs_locked_range_t
*lr
;
3923 off_t start
, end
, obj_size
;
3925 int pgsin_b
, pgsin_a
;
3928 if (zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
) != 0)
3929 return (zfs_vm_pagerret_error
);
3931 start
= IDX_TO_OFF(ma
[0]->pindex
);
3932 end
= IDX_TO_OFF(ma
[count
- 1]->pindex
+ 1);
3935 * Lock a range covering all required and optional pages.
3936 * Note that we need to handle the case of the block size growing.
3939 blksz
= zp
->z_blksz
;
3940 lr
= zfs_rangelock_tryenter(&zp
->z_rangelock
,
3941 rounddown(start
, blksz
),
3942 roundup(end
, blksz
) - rounddown(start
, blksz
), RL_READER
);
3944 if (rahead
!= NULL
) {
3948 if (rbehind
!= NULL
) {
3954 if (blksz
== zp
->z_blksz
)
3956 zfs_rangelock_exit(lr
);
3959 object
= ma
[0]->object
;
3960 zfs_vmobject_wlock(object
);
3961 obj_size
= object
->un_pager
.vnp
.vnp_size
;
3962 zfs_vmobject_wunlock(object
);
3963 if (IDX_TO_OFF(ma
[count
- 1]->pindex
) >= obj_size
) {
3965 zfs_rangelock_exit(lr
);
3966 zfs_exit(zfsvfs
, FTAG
);
3967 return (zfs_vm_pagerret_bad
);
3971 if (rbehind
!= NULL
) {
3972 pgsin_b
= OFF_TO_IDX(start
- rounddown(start
, blksz
));
3973 pgsin_b
= MIN(*rbehind
, pgsin_b
);
3977 if (rahead
!= NULL
) {
3978 pgsin_a
= OFF_TO_IDX(roundup(end
, blksz
) - end
);
3979 if (end
+ IDX_TO_OFF(pgsin_a
) >= obj_size
)
3980 pgsin_a
= OFF_TO_IDX(round_page(obj_size
) - end
);
3981 pgsin_a
= MIN(*rahead
, pgsin_a
);
3985 * NB: we need to pass the exact byte size of the data that we expect
3986 * to read after accounting for the file size. This is required because
3987 * ZFS will panic if we request DMU to read beyond the end of the last
3990 error
= dmu_read_pages(zfsvfs
->z_os
, zp
->z_id
, ma
, count
, &pgsin_b
,
3991 &pgsin_a
, MIN(end
, obj_size
) - (end
- PAGE_SIZE
));
3994 zfs_rangelock_exit(lr
);
3995 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3997 dataset_kstats_update_read_kstats(&zfsvfs
->z_kstat
, count
*PAGE_SIZE
);
3999 zfs_exit(zfsvfs
, FTAG
);
4002 return (zfs_vm_pagerret_error
);
4004 VM_CNT_INC(v_vnodein
);
4005 VM_CNT_ADD(v_vnodepgsin
, count
+ pgsin_b
+ pgsin_a
);
4006 if (rbehind
!= NULL
)
4010 return (zfs_vm_pagerret_ok
);
4013 #ifndef _SYS_SYSPROTO_H_
4014 struct vop_getpages_args
{
4024 zfs_freebsd_getpages(struct vop_getpages_args
*ap
)
4027 return (zfs_getpages(ap
->a_vp
, ap
->a_m
, ap
->a_count
, ap
->a_rbehind
,
4032 zfs_putpages(struct vnode
*vp
, vm_page_t
*ma
, size_t len
, int flags
,
4035 znode_t
*zp
= VTOZ(vp
);
4036 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
4037 zfs_locked_range_t
*lr
;
4045 vm_ooffset_t lo_off
;
4053 object
= vp
->v_object
;
4054 KASSERT(ma
[0]->object
== object
, ("mismatching object"));
4055 KASSERT(len
> 0 && (len
& PAGE_MASK
) == 0, ("unexpected length"));
4059 for (i
= 0; i
< pcount
; i
++)
4060 rtvals
[i
] = zfs_vm_pagerret_error
;
4062 if (zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
) != 0)
4063 return (zfs_vm_pagerret_error
);
4065 off
= IDX_TO_OFF(ma
[0]->pindex
);
4066 blksz
= zp
->z_blksz
;
4067 lo_off
= rounddown(off
, blksz
);
4068 lo_len
= roundup(len
+ (off
- lo_off
), blksz
);
4069 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, lo_off
, lo_len
, RL_WRITER
);
4071 zfs_vmobject_wlock(object
);
4072 if (len
+ off
> object
->un_pager
.vnp
.vnp_size
) {
4073 if (object
->un_pager
.vnp
.vnp_size
> off
) {
4076 len
= object
->un_pager
.vnp
.vnp_size
- off
;
4078 if ((pgoff
= (int)len
& PAGE_MASK
) != 0) {
4080 * If the object is locked and the following
4081 * conditions hold, then the page's dirty
4082 * field cannot be concurrently changed by a
4086 vm_page_assert_sbusied(m
);
4087 KASSERT(!pmap_page_is_write_mapped(m
),
4088 ("zfs_putpages: page %p is not read-only",
4090 vm_page_clear_dirty(m
, pgoff
, PAGE_SIZE
-
4097 if (ncount
< pcount
) {
4098 for (i
= ncount
; i
< pcount
; i
++) {
4099 rtvals
[i
] = zfs_vm_pagerret_bad
;
4103 zfs_vmobject_wunlock(object
);
4105 boolean_t commit
= (flags
& (zfs_vm_pagerput_sync
|
4106 zfs_vm_pagerput_inval
)) != 0 ||
4107 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
;
4112 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
, zp
->z_uid
) ||
4113 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
, zp
->z_gid
) ||
4114 (zp
->z_projid
!= ZFS_DEFAULT_PROJID
&&
4115 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
4120 tx
= dmu_tx_create(zfsvfs
->z_os
);
4121 dmu_tx_hold_write(tx
, zp
->z_id
, off
, len
);
4123 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4124 zfs_sa_upgrade_txholds(tx
, zp
);
4125 err
= dmu_tx_assign(tx
, TXG_WAIT
);
4131 if (zp
->z_blksz
< PAGE_SIZE
) {
4132 for (i
= 0; len
> 0; off
+= tocopy
, len
-= tocopy
, i
++) {
4133 tocopy
= len
> PAGE_SIZE
? PAGE_SIZE
: len
;
4134 va
= zfs_map_page(ma
[i
], &sf
);
4135 dmu_write(zfsvfs
->z_os
, zp
->z_id
, off
, tocopy
, va
, tx
);
4139 err
= dmu_write_pages(zfsvfs
->z_os
, zp
->z_id
, off
, len
, ma
, tx
);
4143 uint64_t mtime
[2], ctime
[2];
4144 sa_bulk_attr_t bulk
[3];
4147 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
4149 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
4151 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
4153 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
4154 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
4157 * XXX we should be passing a callback to undirty
4158 * but that would make the locking messier
4160 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, off
,
4161 len
, commit
, B_FALSE
, NULL
, NULL
);
4163 zfs_vmobject_wlock(object
);
4164 for (i
= 0; i
< ncount
; i
++) {
4165 rtvals
[i
] = zfs_vm_pagerret_ok
;
4166 vm_page_undirty(ma
[i
]);
4168 zfs_vmobject_wunlock(object
);
4169 VM_CNT_INC(v_vnodeout
);
4170 VM_CNT_ADD(v_vnodepgsout
, ncount
);
4175 zfs_rangelock_exit(lr
);
4177 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
4179 dataset_kstats_update_write_kstats(&zfsvfs
->z_kstat
, len
);
4181 zfs_exit(zfsvfs
, FTAG
);
4185 #ifndef _SYS_SYSPROTO_H_
4186 struct vop_putpages_args
{
4196 zfs_freebsd_putpages(struct vop_putpages_args
*ap
)
4199 return (zfs_putpages(ap
->a_vp
, ap
->a_m
, ap
->a_count
, ap
->a_sync
,
4203 #ifndef _SYS_SYSPROTO_H_
4204 struct vop_bmap_args
{
4207 struct bufobj
**a_bop
;
4215 zfs_freebsd_bmap(struct vop_bmap_args
*ap
)
4218 if (ap
->a_bop
!= NULL
)
4219 *ap
->a_bop
= &ap
->a_vp
->v_bufobj
;
4220 if (ap
->a_bnp
!= NULL
)
4221 *ap
->a_bnp
= ap
->a_bn
;
4222 if (ap
->a_runp
!= NULL
)
4224 if (ap
->a_runb
!= NULL
)
4230 #ifndef _SYS_SYSPROTO_H_
4231 struct vop_open_args
{
4234 struct ucred
*a_cred
;
4235 struct thread
*a_td
;
4240 zfs_freebsd_open(struct vop_open_args
*ap
)
4242 vnode_t
*vp
= ap
->a_vp
;
4243 znode_t
*zp
= VTOZ(vp
);
4246 error
= zfs_open(&vp
, ap
->a_mode
, ap
->a_cred
);
4248 vnode_create_vobject(vp
, zp
->z_size
, ap
->a_td
);
4252 #ifndef _SYS_SYSPROTO_H_
4253 struct vop_close_args
{
4256 struct ucred
*a_cred
;
4257 struct thread
*a_td
;
4262 zfs_freebsd_close(struct vop_close_args
*ap
)
4265 return (zfs_close(ap
->a_vp
, ap
->a_fflag
, 1, 0, ap
->a_cred
));
4268 #ifndef _SYS_SYSPROTO_H_
4269 struct vop_ioctl_args
{
4280 zfs_freebsd_ioctl(struct vop_ioctl_args
*ap
)
4283 return (zfs_ioctl(ap
->a_vp
, ap
->a_command
, (intptr_t)ap
->a_data
,
4284 ap
->a_fflag
, ap
->a_cred
, NULL
));
4288 ioflags(int ioflags
)
4292 if (ioflags
& IO_APPEND
)
4294 if (ioflags
& IO_NDELAY
)
4295 flags
|= O_NONBLOCK
;
4296 if (ioflags
& IO_DIRECT
)
4298 if (ioflags
& IO_SYNC
)
4304 #ifndef _SYS_SYSPROTO_H_
4305 struct vop_read_args
{
4309 struct ucred
*a_cred
;
4314 zfs_freebsd_read(struct vop_read_args
*ap
)
4318 zfs_uio_init(&uio
, ap
->a_uio
);
4319 error
= zfs_read(VTOZ(ap
->a_vp
), &uio
, ioflags(ap
->a_ioflag
),
4322 * XXX We occasionally get an EFAULT for Direct I/O reads on
4323 * FreeBSD 13. This still needs to be resolved. The EFAULT comes
4325 * zfs_uio_get__dio_pages_alloc() ->
4326 * zfs_uio_get_dio_pages_impl() ->
4327 * zfs_uio_iov_step() ->
4328 * zfs_uio_get_user_pages().
4329 * We return EFAULT from zfs_uio_iov_step(). When a Direct I/O
4330 * read fails to map in the user pages (returning EFAULT) the
4331 * Direct I/O request is broken up into two separate IO requests
4332 * and issued separately using Direct I/O.
4335 if (error
== EFAULT
&& uio
.uio_extflg
& UIO_DIRECT
) {
4337 printf("%s(%d): Direct I/O read returning EFAULT "
4338 "uio = %p, zfs_uio_offset(uio) = %lu "
4339 "zfs_uio_resid(uio) = %lu\n",
4340 __FUNCTION__
, __LINE__
, &uio
, zfs_uio_offset(&uio
),
4341 zfs_uio_resid(&uio
));
4349 #ifndef _SYS_SYSPROTO_H_
4350 struct vop_write_args
{
4354 struct ucred
*a_cred
;
4359 zfs_freebsd_write(struct vop_write_args
*ap
)
4362 zfs_uio_init(&uio
, ap
->a_uio
);
4363 return (zfs_write(VTOZ(ap
->a_vp
), &uio
, ioflags(ap
->a_ioflag
),
4368 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
4369 * the comment above cache_fplookup for details.
4372 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args
*v
)
4380 if (__predict_false(zp
== NULL
))
4382 pflags
= atomic_load_64(&zp
->z_pflags
);
4383 if (pflags
& ZFS_AV_QUARANTINED
)
4385 if (pflags
& ZFS_XATTR
)
4387 if ((pflags
& ZFS_NO_EXECS_DENIED
) == 0)
4393 zfs_freebsd_fplookup_symlink(struct vop_fplookup_symlink_args
*v
)
4401 if (__predict_false(zp
== NULL
)) {
4405 target
= atomic_load_consume_ptr(&zp
->z_cached_symlink
);
4406 if (target
== NULL
) {
4409 return (cache_symlink_resolve(v
->a_fpl
, target
, strlen(target
)));
4412 #ifndef _SYS_SYSPROTO_H_
4413 struct vop_access_args
{
4415 accmode_t a_accmode
;
4416 struct ucred
*a_cred
;
4417 struct thread
*a_td
;
4422 zfs_freebsd_access(struct vop_access_args
*ap
)
4424 vnode_t
*vp
= ap
->a_vp
;
4425 znode_t
*zp
= VTOZ(vp
);
4430 if (ap
->a_accmode
== VEXEC
) {
4431 if (zfs_fastaccesschk_execute(zp
, ap
->a_cred
) == 0)
4436 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4438 accmode
= ap
->a_accmode
& (VREAD
|VWRITE
|VEXEC
|VAPPEND
);
4440 error
= zfs_access(zp
, accmode
, 0, ap
->a_cred
);
4443 * VADMIN has to be handled by vaccess().
4446 accmode
= ap
->a_accmode
& ~(VREAD
|VWRITE
|VEXEC
|VAPPEND
);
4448 error
= vaccess(vp
->v_type
, zp
->z_mode
, zp
->z_uid
,
4449 zp
->z_gid
, accmode
, ap
->a_cred
);
4454 * For VEXEC, ensure that at least one execute bit is set for
4457 if (error
== 0 && (ap
->a_accmode
& VEXEC
) != 0 && vp
->v_type
!= VDIR
&&
4458 (zp
->z_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) == 0) {
4465 #ifndef _SYS_SYSPROTO_H_
4466 struct vop_lookup_args
{
4467 struct vnode
*a_dvp
;
4468 struct vnode
**a_vpp
;
4469 struct componentname
*a_cnp
;
4474 zfs_freebsd_lookup(struct vop_lookup_args
*ap
, boolean_t cached
)
4476 struct componentname
*cnp
= ap
->a_cnp
;
4477 char nm
[NAME_MAX
+ 1];
4479 ASSERT3U(cnp
->cn_namelen
, <, sizeof (nm
));
4480 strlcpy(nm
, cnp
->cn_nameptr
, MIN(cnp
->cn_namelen
+ 1, sizeof (nm
)));
4482 return (zfs_lookup(ap
->a_dvp
, nm
, ap
->a_vpp
, cnp
, cnp
->cn_nameiop
,
4483 cnp
->cn_cred
, 0, cached
));
4487 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args
*ap
)
4490 return (zfs_freebsd_lookup((struct vop_lookup_args
*)ap
, B_TRUE
));
4493 #ifndef _SYS_SYSPROTO_H_
4494 struct vop_lookup_args
{
4495 struct vnode
*a_dvp
;
4496 struct vnode
**a_vpp
;
4497 struct componentname
*a_cnp
;
4502 zfs_cache_lookup(struct vop_lookup_args
*ap
)
4506 zfsvfs
= ap
->a_dvp
->v_mount
->mnt_data
;
4507 if (zfsvfs
->z_use_namecache
)
4508 return (vfs_cache_lookup(ap
));
4510 return (zfs_freebsd_lookup(ap
, B_FALSE
));
4513 #ifndef _SYS_SYSPROTO_H_
4514 struct vop_create_args
{
4515 struct vnode
*a_dvp
;
4516 struct vnode
**a_vpp
;
4517 struct componentname
*a_cnp
;
4518 struct vattr
*a_vap
;
4523 zfs_freebsd_create(struct vop_create_args
*ap
)
4526 struct componentname
*cnp
= ap
->a_cnp
;
4527 vattr_t
*vap
= ap
->a_vap
;
4531 #if __FreeBSD_version < 1400068
4532 ASSERT(cnp
->cn_flags
& SAVENAME
);
4535 vattr_init_mask(vap
);
4536 mode
= vap
->va_mode
& ALLPERMS
;
4537 zfsvfs
= ap
->a_dvp
->v_mount
->mnt_data
;
4540 rc
= zfs_create(VTOZ(ap
->a_dvp
), cnp
->cn_nameptr
, vap
, 0, mode
,
4541 &zp
, cnp
->cn_cred
, 0 /* flag */, NULL
/* vsecattr */, NULL
);
4543 *ap
->a_vpp
= ZTOV(zp
);
4544 if (zfsvfs
->z_use_namecache
&&
4545 rc
== 0 && (cnp
->cn_flags
& MAKEENTRY
) != 0)
4546 cache_enter(ap
->a_dvp
, *ap
->a_vpp
, cnp
);
4551 #ifndef _SYS_SYSPROTO_H_
4552 struct vop_remove_args
{
4553 struct vnode
*a_dvp
;
4555 struct componentname
*a_cnp
;
4560 zfs_freebsd_remove(struct vop_remove_args
*ap
)
4563 #if __FreeBSD_version < 1400068
4564 ASSERT(ap
->a_cnp
->cn_flags
& SAVENAME
);
4567 return (zfs_remove_(ap
->a_dvp
, ap
->a_vp
, ap
->a_cnp
->cn_nameptr
,
4568 ap
->a_cnp
->cn_cred
));
4571 #ifndef _SYS_SYSPROTO_H_
4572 struct vop_mkdir_args
{
4573 struct vnode
*a_dvp
;
4574 struct vnode
**a_vpp
;
4575 struct componentname
*a_cnp
;
4576 struct vattr
*a_vap
;
4581 zfs_freebsd_mkdir(struct vop_mkdir_args
*ap
)
4583 vattr_t
*vap
= ap
->a_vap
;
4587 #if __FreeBSD_version < 1400068
4588 ASSERT(ap
->a_cnp
->cn_flags
& SAVENAME
);
4591 vattr_init_mask(vap
);
4594 rc
= zfs_mkdir(VTOZ(ap
->a_dvp
), ap
->a_cnp
->cn_nameptr
, vap
, &zp
,
4595 ap
->a_cnp
->cn_cred
, 0, NULL
, NULL
);
4598 *ap
->a_vpp
= ZTOV(zp
);
4602 #ifndef _SYS_SYSPROTO_H_
4603 struct vop_rmdir_args
{
4604 struct vnode
*a_dvp
;
4606 struct componentname
*a_cnp
;
4611 zfs_freebsd_rmdir(struct vop_rmdir_args
*ap
)
4613 struct componentname
*cnp
= ap
->a_cnp
;
4615 #if __FreeBSD_version < 1400068
4616 ASSERT(cnp
->cn_flags
& SAVENAME
);
4619 return (zfs_rmdir_(ap
->a_dvp
, ap
->a_vp
, cnp
->cn_nameptr
, cnp
->cn_cred
));
4622 #ifndef _SYS_SYSPROTO_H_
4623 struct vop_readdir_args
{
4626 struct ucred
*a_cred
;
4629 cookie_t
**a_cookies
;
4634 zfs_freebsd_readdir(struct vop_readdir_args
*ap
)
4637 zfs_uio_init(&uio
, ap
->a_uio
);
4638 return (zfs_readdir(ap
->a_vp
, &uio
, ap
->a_cred
, ap
->a_eofflag
,
4639 ap
->a_ncookies
, ap
->a_cookies
));
4642 #ifndef _SYS_SYSPROTO_H_
4643 struct vop_fsync_args
{
4646 struct thread
*a_td
;
4651 zfs_freebsd_fsync(struct vop_fsync_args
*ap
)
4654 return (zfs_fsync(VTOZ(ap
->a_vp
), 0, ap
->a_td
->td_ucred
));
4657 #ifndef _SYS_SYSPROTO_H_
4658 struct vop_getattr_args
{
4660 struct vattr
*a_vap
;
4661 struct ucred
*a_cred
;
4666 zfs_freebsd_getattr(struct vop_getattr_args
*ap
)
4668 vattr_t
*vap
= ap
->a_vap
;
4674 xvap
.xva_vattr
= *vap
;
4675 xvap
.xva_vattr
.va_mask
|= AT_XVATTR
;
4677 /* Convert chflags into ZFS-type flags. */
4678 /* XXX: what about SF_SETTABLE?. */
4679 XVA_SET_REQ(&xvap
, XAT_IMMUTABLE
);
4680 XVA_SET_REQ(&xvap
, XAT_APPENDONLY
);
4681 XVA_SET_REQ(&xvap
, XAT_NOUNLINK
);
4682 XVA_SET_REQ(&xvap
, XAT_NODUMP
);
4683 XVA_SET_REQ(&xvap
, XAT_READONLY
);
4684 XVA_SET_REQ(&xvap
, XAT_ARCHIVE
);
4685 XVA_SET_REQ(&xvap
, XAT_SYSTEM
);
4686 XVA_SET_REQ(&xvap
, XAT_HIDDEN
);
4687 XVA_SET_REQ(&xvap
, XAT_REPARSE
);
4688 XVA_SET_REQ(&xvap
, XAT_OFFLINE
);
4689 XVA_SET_REQ(&xvap
, XAT_SPARSE
);
4691 error
= zfs_getattr(ap
->a_vp
, (vattr_t
*)&xvap
, 0, ap
->a_cred
);
4695 /* Convert ZFS xattr into chflags. */
4696 #define FLAG_CHECK(fflag, xflag, xfield) do { \
4697 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
4698 fflags |= (fflag); \
4700 FLAG_CHECK(SF_IMMUTABLE
, XAT_IMMUTABLE
,
4701 xvap
.xva_xoptattrs
.xoa_immutable
);
4702 FLAG_CHECK(SF_APPEND
, XAT_APPENDONLY
,
4703 xvap
.xva_xoptattrs
.xoa_appendonly
);
4704 FLAG_CHECK(SF_NOUNLINK
, XAT_NOUNLINK
,
4705 xvap
.xva_xoptattrs
.xoa_nounlink
);
4706 FLAG_CHECK(UF_ARCHIVE
, XAT_ARCHIVE
,
4707 xvap
.xva_xoptattrs
.xoa_archive
);
4708 FLAG_CHECK(UF_NODUMP
, XAT_NODUMP
,
4709 xvap
.xva_xoptattrs
.xoa_nodump
);
4710 FLAG_CHECK(UF_READONLY
, XAT_READONLY
,
4711 xvap
.xva_xoptattrs
.xoa_readonly
);
4712 FLAG_CHECK(UF_SYSTEM
, XAT_SYSTEM
,
4713 xvap
.xva_xoptattrs
.xoa_system
);
4714 FLAG_CHECK(UF_HIDDEN
, XAT_HIDDEN
,
4715 xvap
.xva_xoptattrs
.xoa_hidden
);
4716 FLAG_CHECK(UF_REPARSE
, XAT_REPARSE
,
4717 xvap
.xva_xoptattrs
.xoa_reparse
);
4718 FLAG_CHECK(UF_OFFLINE
, XAT_OFFLINE
,
4719 xvap
.xva_xoptattrs
.xoa_offline
);
4720 FLAG_CHECK(UF_SPARSE
, XAT_SPARSE
,
4721 xvap
.xva_xoptattrs
.xoa_sparse
);
4724 *vap
= xvap
.xva_vattr
;
4725 vap
->va_flags
= fflags
;
4729 #ifndef _SYS_SYSPROTO_H_
4730 struct vop_setattr_args
{
4732 struct vattr
*a_vap
;
4733 struct ucred
*a_cred
;
4738 zfs_freebsd_setattr(struct vop_setattr_args
*ap
)
4740 vnode_t
*vp
= ap
->a_vp
;
4741 vattr_t
*vap
= ap
->a_vap
;
4742 cred_t
*cred
= ap
->a_cred
;
4747 vattr_init_mask(vap
);
4748 vap
->va_mask
&= ~AT_NOSET
;
4751 xvap
.xva_vattr
= *vap
;
4753 zflags
= VTOZ(vp
)->z_pflags
;
4755 if (vap
->va_flags
!= VNOVAL
) {
4756 zfsvfs_t
*zfsvfs
= VTOZ(vp
)->z_zfsvfs
;
4759 if (zfsvfs
->z_use_fuids
== B_FALSE
)
4760 return (EOPNOTSUPP
);
4762 fflags
= vap
->va_flags
;
4765 * We need to figure out whether it makes sense to allow
4766 * UF_REPARSE through, since we don't really have other
4767 * facilities to handle reparse points and zfs_setattr()
4768 * doesn't currently allow setting that attribute anyway.
4770 if ((fflags
& ~(SF_IMMUTABLE
|SF_APPEND
|SF_NOUNLINK
|UF_ARCHIVE
|
4771 UF_NODUMP
|UF_SYSTEM
|UF_HIDDEN
|UF_READONLY
|UF_REPARSE
|
4772 UF_OFFLINE
|UF_SPARSE
)) != 0)
4773 return (EOPNOTSUPP
);
4775 * Unprivileged processes are not permitted to unset system
4776 * flags, or modify flags if any system flags are set.
4777 * Privileged non-jail processes may not modify system flags
4778 * if securelevel > 0 and any existing system flags are set.
4779 * Privileged jail processes behave like privileged non-jail
4780 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
4781 * otherwise, they behave like unprivileged processes.
4783 if (secpolicy_fs_owner(vp
->v_mount
, cred
) == 0 ||
4784 priv_check_cred(cred
, PRIV_VFS_SYSFLAGS
) == 0) {
4786 (ZFS_IMMUTABLE
| ZFS_APPENDONLY
| ZFS_NOUNLINK
)) {
4787 error
= securelevel_gt(cred
, 0);
4793 * Callers may only modify the file flags on
4794 * objects they have VADMIN rights for.
4796 if ((error
= VOP_ACCESS(vp
, VADMIN
, cred
,
4800 (ZFS_IMMUTABLE
| ZFS_APPENDONLY
|
4805 (SF_IMMUTABLE
| SF_APPEND
| SF_NOUNLINK
)) {
4810 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
4811 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
4812 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
4813 XVA_SET_REQ(&xvap, (xflag)); \
4814 (xfield) = ((fflags & (fflag)) != 0); \
4817 /* Convert chflags into ZFS-type flags. */
4818 /* XXX: what about SF_SETTABLE?. */
4819 FLAG_CHANGE(SF_IMMUTABLE
, ZFS_IMMUTABLE
, XAT_IMMUTABLE
,
4820 xvap
.xva_xoptattrs
.xoa_immutable
);
4821 FLAG_CHANGE(SF_APPEND
, ZFS_APPENDONLY
, XAT_APPENDONLY
,
4822 xvap
.xva_xoptattrs
.xoa_appendonly
);
4823 FLAG_CHANGE(SF_NOUNLINK
, ZFS_NOUNLINK
, XAT_NOUNLINK
,
4824 xvap
.xva_xoptattrs
.xoa_nounlink
);
4825 FLAG_CHANGE(UF_ARCHIVE
, ZFS_ARCHIVE
, XAT_ARCHIVE
,
4826 xvap
.xva_xoptattrs
.xoa_archive
);
4827 FLAG_CHANGE(UF_NODUMP
, ZFS_NODUMP
, XAT_NODUMP
,
4828 xvap
.xva_xoptattrs
.xoa_nodump
);
4829 FLAG_CHANGE(UF_READONLY
, ZFS_READONLY
, XAT_READONLY
,
4830 xvap
.xva_xoptattrs
.xoa_readonly
);
4831 FLAG_CHANGE(UF_SYSTEM
, ZFS_SYSTEM
, XAT_SYSTEM
,
4832 xvap
.xva_xoptattrs
.xoa_system
);
4833 FLAG_CHANGE(UF_HIDDEN
, ZFS_HIDDEN
, XAT_HIDDEN
,
4834 xvap
.xva_xoptattrs
.xoa_hidden
);
4835 FLAG_CHANGE(UF_REPARSE
, ZFS_REPARSE
, XAT_REPARSE
,
4836 xvap
.xva_xoptattrs
.xoa_reparse
);
4837 FLAG_CHANGE(UF_OFFLINE
, ZFS_OFFLINE
, XAT_OFFLINE
,
4838 xvap
.xva_xoptattrs
.xoa_offline
);
4839 FLAG_CHANGE(UF_SPARSE
, ZFS_SPARSE
, XAT_SPARSE
,
4840 xvap
.xva_xoptattrs
.xoa_sparse
);
4843 if (vap
->va_birthtime
.tv_sec
!= VNOVAL
) {
4844 xvap
.xva_vattr
.va_mask
|= AT_XVATTR
;
4845 XVA_SET_REQ(&xvap
, XAT_CREATETIME
);
4847 return (zfs_setattr(VTOZ(vp
), (vattr_t
*)&xvap
, 0, cred
, NULL
));
4850 #ifndef _SYS_SYSPROTO_H_
4851 struct vop_rename_args
{
4852 struct vnode
*a_fdvp
;
4853 struct vnode
*a_fvp
;
4854 struct componentname
*a_fcnp
;
4855 struct vnode
*a_tdvp
;
4856 struct vnode
*a_tvp
;
4857 struct componentname
*a_tcnp
;
4862 zfs_freebsd_rename(struct vop_rename_args
*ap
)
4864 vnode_t
*fdvp
= ap
->a_fdvp
;
4865 vnode_t
*fvp
= ap
->a_fvp
;
4866 vnode_t
*tdvp
= ap
->a_tdvp
;
4867 vnode_t
*tvp
= ap
->a_tvp
;
4870 #if __FreeBSD_version < 1400068
4871 ASSERT(ap
->a_fcnp
->cn_flags
& (SAVENAME
|SAVESTART
));
4872 ASSERT(ap
->a_tcnp
->cn_flags
& (SAVENAME
|SAVESTART
));
4875 error
= zfs_do_rename(fdvp
, &fvp
, ap
->a_fcnp
, tdvp
, &tvp
,
4876 ap
->a_tcnp
, ap
->a_fcnp
->cn_cred
);
4887 #ifndef _SYS_SYSPROTO_H_
4888 struct vop_symlink_args
{
4889 struct vnode
*a_dvp
;
4890 struct vnode
**a_vpp
;
4891 struct componentname
*a_cnp
;
4892 struct vattr
*a_vap
;
4898 zfs_freebsd_symlink(struct vop_symlink_args
*ap
)
4900 struct componentname
*cnp
= ap
->a_cnp
;
4901 vattr_t
*vap
= ap
->a_vap
;
4907 #if __FreeBSD_version < 1400068
4908 ASSERT(cnp
->cn_flags
& SAVENAME
);
4911 vap
->va_type
= VLNK
; /* FreeBSD: Syscall only sets va_mode. */
4912 vattr_init_mask(vap
);
4915 rc
= zfs_symlink(VTOZ(ap
->a_dvp
), cnp
->cn_nameptr
, vap
,
4916 ap
->a_target
, &zp
, cnp
->cn_cred
, 0 /* flags */, NULL
);
4918 *ap
->a_vpp
= ZTOV(zp
);
4919 ASSERT_VOP_ELOCKED(ZTOV(zp
), __func__
);
4920 MPASS(zp
->z_cached_symlink
== NULL
);
4921 symlink_len
= strlen(ap
->a_target
);
4922 symlink
= cache_symlink_alloc(symlink_len
+ 1, M_WAITOK
);
4923 if (symlink
!= NULL
) {
4924 memcpy(symlink
, ap
->a_target
, symlink_len
);
4925 symlink
[symlink_len
] = '\0';
4926 atomic_store_rel_ptr((uintptr_t *)&zp
->z_cached_symlink
,
4927 (uintptr_t)symlink
);
4933 #ifndef _SYS_SYSPROTO_H_
4934 struct vop_readlink_args
{
4937 struct ucred
*a_cred
;
4942 zfs_freebsd_readlink(struct vop_readlink_args
*ap
)
4946 znode_t
*zp
= VTOZ(ap
->a_vp
);
4947 char *symlink
, *base
;
4951 zfs_uio_init(&uio
, ap
->a_uio
);
4953 if (zfs_uio_segflg(&uio
) == UIO_SYSSPACE
&&
4954 zfs_uio_iovcnt(&uio
) == 1) {
4955 base
= zfs_uio_iovbase(&uio
, 0);
4956 symlink_len
= zfs_uio_iovlen(&uio
, 0);
4959 error
= zfs_readlink(ap
->a_vp
, &uio
, ap
->a_cred
, NULL
);
4960 if (atomic_load_ptr(&zp
->z_cached_symlink
) != NULL
||
4961 error
!= 0 || !trycache
) {
4964 symlink_len
-= zfs_uio_resid(&uio
);
4965 symlink
= cache_symlink_alloc(symlink_len
+ 1, M_WAITOK
);
4966 if (symlink
!= NULL
) {
4967 memcpy(symlink
, base
, symlink_len
);
4968 symlink
[symlink_len
] = '\0';
4969 if (!atomic_cmpset_rel_ptr((uintptr_t *)&zp
->z_cached_symlink
,
4970 (uintptr_t)NULL
, (uintptr_t)symlink
)) {
4971 cache_symlink_free(symlink
, symlink_len
+ 1);
4977 #ifndef _SYS_SYSPROTO_H_
4978 struct vop_link_args
{
4979 struct vnode
*a_tdvp
;
4981 struct componentname
*a_cnp
;
4986 zfs_freebsd_link(struct vop_link_args
*ap
)
4988 struct componentname
*cnp
= ap
->a_cnp
;
4989 vnode_t
*vp
= ap
->a_vp
;
4990 vnode_t
*tdvp
= ap
->a_tdvp
;
4992 if (tdvp
->v_mount
!= vp
->v_mount
)
4995 #if __FreeBSD_version < 1400068
4996 ASSERT(cnp
->cn_flags
& SAVENAME
);
4999 return (zfs_link(VTOZ(tdvp
), VTOZ(vp
),
5000 cnp
->cn_nameptr
, cnp
->cn_cred
, 0));
5003 #ifndef _SYS_SYSPROTO_H_
5004 struct vop_inactive_args
{
5006 struct thread
*a_td
;
5011 zfs_freebsd_inactive(struct vop_inactive_args
*ap
)
5013 vnode_t
*vp
= ap
->a_vp
;
5015 zfs_inactive(vp
, curthread
->td_ucred
, NULL
);
5019 #ifndef _SYS_SYSPROTO_H_
5020 struct vop_need_inactive_args
{
5022 struct thread
*a_td
;
5027 zfs_freebsd_need_inactive(struct vop_need_inactive_args
*ap
)
5029 vnode_t
*vp
= ap
->a_vp
;
5030 znode_t
*zp
= VTOZ(vp
);
5031 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
5034 if (vn_need_pageq_flush(vp
))
5037 if (!ZFS_TEARDOWN_INACTIVE_TRY_ENTER_READ(zfsvfs
))
5039 need
= (zp
->z_sa_hdl
== NULL
|| zp
->z_unlinked
|| zp
->z_atime_dirty
);
5040 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
5045 #ifndef _SYS_SYSPROTO_H_
5046 struct vop_reclaim_args
{
5048 struct thread
*a_td
;
5053 zfs_freebsd_reclaim(struct vop_reclaim_args
*ap
)
5055 vnode_t
*vp
= ap
->a_vp
;
5056 znode_t
*zp
= VTOZ(vp
);
5057 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
5059 ASSERT3P(zp
, !=, NULL
);
5062 * z_teardown_inactive_lock protects from a race with
5063 * zfs_znode_dmu_fini in zfsvfs_teardown during
5066 ZFS_TEARDOWN_INACTIVE_ENTER_READ(zfsvfs
);
5067 if (zp
->z_sa_hdl
== NULL
)
5071 ZFS_TEARDOWN_INACTIVE_EXIT_READ(zfsvfs
);
5077 #ifndef _SYS_SYSPROTO_H_
5078 struct vop_fid_args
{
5085 zfs_freebsd_fid(struct vop_fid_args
*ap
)
5088 return (zfs_fid(ap
->a_vp
, (void *)ap
->a_fid
, NULL
));
5092 #ifndef _SYS_SYSPROTO_H_
5093 struct vop_pathconf_args
{
5096 register_t
*a_retval
;
5101 zfs_freebsd_pathconf(struct vop_pathconf_args
*ap
)
5106 error
= zfs_pathconf(ap
->a_vp
, ap
->a_name
, &val
,
5107 curthread
->td_ucred
, NULL
);
5109 *ap
->a_retval
= val
;
5112 if (error
!= EOPNOTSUPP
)
5115 switch (ap
->a_name
) {
5117 *ap
->a_retval
= NAME_MAX
;
5119 #if __FreeBSD_version >= 1400032
5120 case _PC_DEALLOC_PRESENT
:
5125 if (ap
->a_vp
->v_type
== VDIR
|| ap
->a_vp
->v_type
== VFIFO
) {
5126 *ap
->a_retval
= PIPE_BUF
;
5131 return (vop_stdpathconf(ap
));
5135 static int zfs_xattr_compat
= 1;
5138 zfs_check_attrname(const char *name
)
5140 /* We don't allow '/' character in attribute name. */
5141 if (strchr(name
, '/') != NULL
)
5142 return (SET_ERROR(EINVAL
));
5143 /* We don't allow attribute names that start with a namespace prefix. */
5144 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name
))
5145 return (SET_ERROR(EINVAL
));
5150 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5151 * extended attribute name:
5153 * NAMESPACE XATTR_COMPAT PREFIX
5154 * system * freebsd:system:
5155 * user 1 (none, can be used to access ZFS
5156 * fsattr(5) attributes created on Solaris)
5160 zfs_create_attrname(int attrnamespace
, const char *name
, char *attrname
,
5161 size_t size
, boolean_t compat
)
5163 const char *namespace, *prefix
, *suffix
;
5165 memset(attrname
, 0, size
);
5167 switch (attrnamespace
) {
5168 case EXTATTR_NAMESPACE_USER
:
5171 * This is the default namespace by which we can access
5172 * all attributes created on Solaris.
5174 prefix
= namespace = suffix
= "";
5177 * This is compatible with the user namespace encoding
5178 * on Linux prior to xattr_compat, but nothing
5186 case EXTATTR_NAMESPACE_SYSTEM
:
5187 prefix
= "freebsd:";
5188 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING
;
5191 case EXTATTR_NAMESPACE_EMPTY
:
5193 return (SET_ERROR(EINVAL
));
5195 if (snprintf(attrname
, size
, "%s%s%s%s", prefix
, namespace, suffix
,
5197 return (SET_ERROR(ENAMETOOLONG
));
5203 zfs_ensure_xattr_cached(znode_t
*zp
)
5207 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
5209 if (zp
->z_xattr_cached
!= NULL
)
5212 if (rw_write_held(&zp
->z_xattr_lock
))
5213 return (zfs_sa_get_xattr(zp
));
5215 if (!rw_tryupgrade(&zp
->z_xattr_lock
)) {
5216 rw_exit(&zp
->z_xattr_lock
);
5217 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
5219 if (zp
->z_xattr_cached
== NULL
)
5220 error
= zfs_sa_get_xattr(zp
);
5221 rw_downgrade(&zp
->z_xattr_lock
);
5225 #ifndef _SYS_SYSPROTO_H_
5226 struct vop_getextattr
{
5227 IN
struct vnode
*a_vp
;
5228 IN
int a_attrnamespace
;
5229 IN
const char *a_name
;
5230 INOUT
struct uio
*a_uio
;
5232 IN
struct ucred
*a_cred
;
5233 IN
struct thread
*a_td
;
5238 zfs_getextattr_dir(struct vop_getextattr_args
*ap
, const char *attrname
)
5240 struct thread
*td
= ap
->a_td
;
5241 struct nameidata nd
;
5243 vnode_t
*xvp
= NULL
, *vp
;
5246 error
= zfs_lookup(ap
->a_vp
, NULL
, &xvp
, NULL
, 0, ap
->a_cred
,
5247 LOOKUP_XATTR
, B_FALSE
);
5252 #if __FreeBSD_version < 1400043
5253 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, attrname
,
5256 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, attrname
, xvp
);
5258 error
= vn_open_cred(&nd
, &flags
, 0, VN_OPEN_INVFS
, ap
->a_cred
, NULL
);
5260 return (SET_ERROR(error
));
5264 if (ap
->a_size
!= NULL
) {
5265 error
= VOP_GETATTR(vp
, &va
, ap
->a_cred
);
5267 *ap
->a_size
= (size_t)va
.va_size
;
5268 } else if (ap
->a_uio
!= NULL
)
5269 error
= VOP_READ(vp
, ap
->a_uio
, IO_UNIT
, ap
->a_cred
);
5272 vn_close(vp
, flags
, ap
->a_cred
, td
);
5277 zfs_getextattr_sa(struct vop_getextattr_args
*ap
, const char *attrname
)
5279 znode_t
*zp
= VTOZ(ap
->a_vp
);
5284 error
= zfs_ensure_xattr_cached(zp
);
5288 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
5289 ASSERT3P(zp
->z_xattr_cached
, !=, NULL
);
5291 error
= nvlist_lookup_byte_array(zp
->z_xattr_cached
, attrname
,
5292 &nv_value
, &nv_size
);
5294 return (SET_ERROR(error
));
5296 if (ap
->a_size
!= NULL
)
5297 *ap
->a_size
= nv_size
;
5298 else if (ap
->a_uio
!= NULL
)
5299 error
= uiomove(nv_value
, nv_size
, ap
->a_uio
);
5301 return (SET_ERROR(error
));
5307 zfs_getextattr_impl(struct vop_getextattr_args
*ap
, boolean_t compat
)
5309 znode_t
*zp
= VTOZ(ap
->a_vp
);
5310 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5311 char attrname
[EXTATTR_MAXNAMELEN
+1];
5314 error
= zfs_create_attrname(ap
->a_attrnamespace
, ap
->a_name
, attrname
,
5315 sizeof (attrname
), compat
);
5320 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
)
5321 error
= zfs_getextattr_sa(ap
, attrname
);
5322 if (error
== ENOENT
)
5323 error
= zfs_getextattr_dir(ap
, attrname
);
5328 * Vnode operation to retrieve a named extended attribute.
5331 zfs_getextattr(struct vop_getextattr_args
*ap
)
5333 znode_t
*zp
= VTOZ(ap
->a_vp
);
5334 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5338 * If the xattr property is off, refuse the request.
5340 if (!(zfsvfs
->z_flags
& ZSB_XATTR
))
5341 return (SET_ERROR(EOPNOTSUPP
));
5343 error
= extattr_check_cred(ap
->a_vp
, ap
->a_attrnamespace
,
5344 ap
->a_cred
, ap
->a_td
, VREAD
);
5346 return (SET_ERROR(error
));
5348 error
= zfs_check_attrname(ap
->a_name
);
5352 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
5355 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
5357 error
= zfs_getextattr_impl(ap
, zfs_xattr_compat
);
5358 if ((error
== ENOENT
|| error
== ENOATTR
) &&
5359 ap
->a_attrnamespace
== EXTATTR_NAMESPACE_USER
) {
5361 * Fall back to the alternate namespace format if we failed to
5362 * find a user xattr.
5364 error
= zfs_getextattr_impl(ap
, !zfs_xattr_compat
);
5367 rw_exit(&zp
->z_xattr_lock
);
5368 zfs_exit(zfsvfs
, FTAG
);
5369 if (error
== ENOENT
)
5370 error
= SET_ERROR(ENOATTR
);
5374 #ifndef _SYS_SYSPROTO_H_
5375 struct vop_deleteextattr
{
5376 IN
struct vnode
*a_vp
;
5377 IN
int a_attrnamespace
;
5378 IN
const char *a_name
;
5379 IN
struct ucred
*a_cred
;
5380 IN
struct thread
*a_td
;
5385 zfs_deleteextattr_dir(struct vop_deleteextattr_args
*ap
, const char *attrname
)
5387 struct nameidata nd
;
5388 vnode_t
*xvp
= NULL
, *vp
;
5391 error
= zfs_lookup(ap
->a_vp
, NULL
, &xvp
, NULL
, 0, ap
->a_cred
,
5392 LOOKUP_XATTR
, B_FALSE
);
5396 #if __FreeBSD_version < 1400043
5397 NDINIT_ATVP(&nd
, DELETE
, NOFOLLOW
| LOCKPARENT
| LOCKLEAF
,
5398 UIO_SYSSPACE
, attrname
, xvp
, ap
->a_td
);
5400 NDINIT_ATVP(&nd
, DELETE
, NOFOLLOW
| LOCKPARENT
| LOCKLEAF
,
5401 UIO_SYSSPACE
, attrname
, xvp
);
5405 return (SET_ERROR(error
));
5408 error
= VOP_REMOVE(nd
.ni_dvp
, vp
, &nd
.ni_cnd
);
5412 if (vp
== nd
.ni_dvp
)
5421 zfs_deleteextattr_sa(struct vop_deleteextattr_args
*ap
, const char *attrname
)
5423 znode_t
*zp
= VTOZ(ap
->a_vp
);
5427 error
= zfs_ensure_xattr_cached(zp
);
5431 ASSERT(RW_WRITE_HELD(&zp
->z_xattr_lock
));
5432 ASSERT3P(zp
->z_xattr_cached
, !=, NULL
);
5434 nvl
= zp
->z_xattr_cached
;
5435 error
= nvlist_remove(nvl
, attrname
, DATA_TYPE_BYTE_ARRAY
);
5437 error
= SET_ERROR(error
);
5439 error
= zfs_sa_set_xattr(zp
, attrname
, NULL
, 0);
5441 zp
->z_xattr_cached
= NULL
;
5448 zfs_deleteextattr_impl(struct vop_deleteextattr_args
*ap
, boolean_t compat
)
5450 znode_t
*zp
= VTOZ(ap
->a_vp
);
5451 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5452 char attrname
[EXTATTR_MAXNAMELEN
+1];
5455 error
= zfs_create_attrname(ap
->a_attrnamespace
, ap
->a_name
, attrname
,
5456 sizeof (attrname
), compat
);
5461 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
)
5462 error
= zfs_deleteextattr_sa(ap
, attrname
);
5463 if (error
== ENOENT
)
5464 error
= zfs_deleteextattr_dir(ap
, attrname
);
5469 * Vnode operation to remove a named attribute.
5472 zfs_deleteextattr(struct vop_deleteextattr_args
*ap
)
5474 znode_t
*zp
= VTOZ(ap
->a_vp
);
5475 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5479 * If the xattr property is off, refuse the request.
5481 if (!(zfsvfs
->z_flags
& ZSB_XATTR
))
5482 return (SET_ERROR(EOPNOTSUPP
));
5484 error
= extattr_check_cred(ap
->a_vp
, ap
->a_attrnamespace
,
5485 ap
->a_cred
, ap
->a_td
, VWRITE
);
5487 return (SET_ERROR(error
));
5489 error
= zfs_check_attrname(ap
->a_name
);
5493 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
5495 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
5497 error
= zfs_deleteextattr_impl(ap
, zfs_xattr_compat
);
5498 if ((error
== ENOENT
|| error
== ENOATTR
) &&
5499 ap
->a_attrnamespace
== EXTATTR_NAMESPACE_USER
) {
5501 * Fall back to the alternate namespace format if we failed to
5502 * find a user xattr.
5504 error
= zfs_deleteextattr_impl(ap
, !zfs_xattr_compat
);
5507 rw_exit(&zp
->z_xattr_lock
);
5508 zfs_exit(zfsvfs
, FTAG
);
5509 if (error
== ENOENT
)
5510 error
= SET_ERROR(ENOATTR
);
5514 #ifndef _SYS_SYSPROTO_H_
5515 struct vop_setextattr
{
5516 IN
struct vnode
*a_vp
;
5517 IN
int a_attrnamespace
;
5518 IN
const char *a_name
;
5519 INOUT
struct uio
*a_uio
;
5520 IN
struct ucred
*a_cred
;
5521 IN
struct thread
*a_td
;
5526 zfs_setextattr_dir(struct vop_setextattr_args
*ap
, const char *attrname
)
5528 struct thread
*td
= ap
->a_td
;
5529 struct nameidata nd
;
5531 vnode_t
*xvp
= NULL
, *vp
;
5534 error
= zfs_lookup(ap
->a_vp
, NULL
, &xvp
, NULL
, 0, ap
->a_cred
,
5535 LOOKUP_XATTR
| CREATE_XATTR_DIR
, B_FALSE
);
5539 flags
= FFLAGS(O_WRONLY
| O_CREAT
);
5540 #if __FreeBSD_version < 1400043
5541 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, attrname
, xvp
, td
);
5543 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
, UIO_SYSSPACE
, attrname
, xvp
);
5545 error
= vn_open_cred(&nd
, &flags
, 0600, VN_OPEN_INVFS
, ap
->a_cred
,
5548 return (SET_ERROR(error
));
5554 error
= VOP_SETATTR(vp
, &va
, ap
->a_cred
);
5556 VOP_WRITE(vp
, ap
->a_uio
, IO_UNIT
, ap
->a_cred
);
5559 vn_close(vp
, flags
, ap
->a_cred
, td
);
5564 zfs_setextattr_sa(struct vop_setextattr_args
*ap
, const char *attrname
)
5566 znode_t
*zp
= VTOZ(ap
->a_vp
);
5571 error
= zfs_ensure_xattr_cached(zp
);
5575 ASSERT(RW_WRITE_HELD(&zp
->z_xattr_lock
));
5576 ASSERT3P(zp
->z_xattr_cached
, !=, NULL
);
5578 nvl
= zp
->z_xattr_cached
;
5579 size_t entry_size
= ap
->a_uio
->uio_resid
;
5580 if (entry_size
> DXATTR_MAX_ENTRY_SIZE
)
5581 return (SET_ERROR(EFBIG
));
5582 error
= nvlist_size(nvl
, &sa_size
, NV_ENCODE_XDR
);
5584 return (SET_ERROR(error
));
5585 if (sa_size
> DXATTR_MAX_SA_SIZE
)
5586 return (SET_ERROR(EFBIG
));
5587 uchar_t
*buf
= kmem_alloc(entry_size
, KM_SLEEP
);
5588 error
= uiomove(buf
, entry_size
, ap
->a_uio
);
5590 error
= SET_ERROR(error
);
5592 error
= nvlist_add_byte_array(nvl
, attrname
, buf
, entry_size
);
5594 error
= SET_ERROR(error
);
5597 error
= zfs_sa_set_xattr(zp
, attrname
, buf
, entry_size
);
5598 kmem_free(buf
, entry_size
);
5600 zp
->z_xattr_cached
= NULL
;
5607 zfs_setextattr_impl(struct vop_setextattr_args
*ap
, boolean_t compat
)
5609 znode_t
*zp
= VTOZ(ap
->a_vp
);
5610 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5611 char attrname
[EXTATTR_MAXNAMELEN
+1];
5614 error
= zfs_create_attrname(ap
->a_attrnamespace
, ap
->a_name
, attrname
,
5615 sizeof (attrname
), compat
);
5619 struct vop_deleteextattr_args vda
= {
5621 .a_attrnamespace
= ap
->a_attrnamespace
,
5622 .a_name
= ap
->a_name
,
5623 .a_cred
= ap
->a_cred
,
5627 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
&& zfsvfs
->z_xattr_sa
) {
5628 error
= zfs_setextattr_sa(ap
, attrname
);
5631 * Successfully put into SA, we need to clear the one
5632 * in dir if present.
5634 zfs_deleteextattr_dir(&vda
, attrname
);
5638 error
= zfs_setextattr_dir(ap
, attrname
);
5639 if (error
== 0 && zp
->z_is_sa
) {
5641 * Successfully put into dir, we need to clear the one
5644 zfs_deleteextattr_sa(&vda
, attrname
);
5647 if (error
== 0 && ap
->a_attrnamespace
== EXTATTR_NAMESPACE_USER
) {
5649 * Also clear all versions of the alternate compat name.
5651 zfs_deleteextattr_impl(&vda
, !compat
);
5657 * Vnode operation to set a named attribute.
5660 zfs_setextattr(struct vop_setextattr_args
*ap
)
5662 znode_t
*zp
= VTOZ(ap
->a_vp
);
5663 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5667 * If the xattr property is off, refuse the request.
5669 if (!(zfsvfs
->z_flags
& ZSB_XATTR
))
5670 return (SET_ERROR(EOPNOTSUPP
));
5672 error
= extattr_check_cred(ap
->a_vp
, ap
->a_attrnamespace
,
5673 ap
->a_cred
, ap
->a_td
, VWRITE
);
5675 return (SET_ERROR(error
));
5677 error
= zfs_check_attrname(ap
->a_name
);
5681 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
5683 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
5685 error
= zfs_setextattr_impl(ap
, zfs_xattr_compat
);
5687 rw_exit(&zp
->z_xattr_lock
);
5688 zfs_exit(zfsvfs
, FTAG
);
5692 #ifndef _SYS_SYSPROTO_H_
5693 struct vop_listextattr
{
5694 IN
struct vnode
*a_vp
;
5695 IN
int a_attrnamespace
;
5696 INOUT
struct uio
*a_uio
;
5698 IN
struct ucred
*a_cred
;
5699 IN
struct thread
*a_td
;
5704 zfs_listextattr_dir(struct vop_listextattr_args
*ap
, const char *attrprefix
)
5706 struct thread
*td
= ap
->a_td
;
5707 struct nameidata nd
;
5708 uint8_t dirbuf
[sizeof (struct dirent
)];
5711 vnode_t
*xvp
= NULL
, *vp
;
5714 error
= zfs_lookup(ap
->a_vp
, NULL
, &xvp
, NULL
, 0, ap
->a_cred
,
5715 LOOKUP_XATTR
, B_FALSE
);
5718 * ENOATTR means that the EA directory does not yet exist,
5719 * i.e. there are no extended attributes there.
5721 if (error
== ENOATTR
)
5726 #if __FreeBSD_version < 1400043
5727 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
| LOCKLEAF
| LOCKSHARED
,
5728 UIO_SYSSPACE
, ".", xvp
, td
);
5730 NDINIT_ATVP(&nd
, LOOKUP
, NOFOLLOW
| LOCKLEAF
| LOCKSHARED
,
5731 UIO_SYSSPACE
, ".", xvp
);
5735 return (SET_ERROR(error
));
5739 auio
.uio_iov
= &aiov
;
5740 auio
.uio_iovcnt
= 1;
5741 auio
.uio_segflg
= UIO_SYSSPACE
;
5743 auio
.uio_rw
= UIO_READ
;
5744 auio
.uio_offset
= 0;
5746 size_t plen
= strlen(attrprefix
);
5749 aiov
.iov_base
= (void *)dirbuf
;
5750 aiov
.iov_len
= sizeof (dirbuf
);
5751 auio
.uio_resid
= sizeof (dirbuf
);
5752 error
= VOP_READDIR(vp
, &auio
, ap
->a_cred
, &eof
, NULL
, NULL
);
5755 int done
= sizeof (dirbuf
) - auio
.uio_resid
;
5756 for (int pos
= 0; pos
< done
; ) {
5757 struct dirent
*dp
= (struct dirent
*)(dirbuf
+ pos
);
5758 pos
+= dp
->d_reclen
;
5760 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5761 * is what we get when attribute was created on Solaris.
5763 if (dp
->d_type
!= DT_REG
&& dp
->d_type
!= DT_UNKNOWN
)
5765 else if (plen
== 0 &&
5766 ZFS_XA_NS_PREFIX_FORBIDDEN(dp
->d_name
))
5768 else if (strncmp(dp
->d_name
, attrprefix
, plen
) != 0)
5770 uint8_t nlen
= dp
->d_namlen
- plen
;
5771 if (ap
->a_size
!= NULL
) {
5772 *ap
->a_size
+= 1 + nlen
;
5773 } else if (ap
->a_uio
!= NULL
) {
5775 * Format of extattr name entry is one byte for
5776 * length and the rest for name.
5778 error
= uiomove(&nlen
, 1, ap
->a_uio
);
5780 char *namep
= dp
->d_name
+ plen
;
5781 error
= uiomove(namep
, nlen
, ap
->a_uio
);
5784 error
= SET_ERROR(error
);
5789 } while (!eof
&& error
== 0);
5796 zfs_listextattr_sa(struct vop_listextattr_args
*ap
, const char *attrprefix
)
5798 znode_t
*zp
= VTOZ(ap
->a_vp
);
5801 error
= zfs_ensure_xattr_cached(zp
);
5805 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
5806 ASSERT3P(zp
->z_xattr_cached
, !=, NULL
);
5808 size_t plen
= strlen(attrprefix
);
5809 nvpair_t
*nvp
= NULL
;
5810 while ((nvp
= nvlist_next_nvpair(zp
->z_xattr_cached
, nvp
)) != NULL
) {
5811 ASSERT3U(nvpair_type(nvp
), ==, DATA_TYPE_BYTE_ARRAY
);
5813 const char *name
= nvpair_name(nvp
);
5814 if (plen
== 0 && ZFS_XA_NS_PREFIX_FORBIDDEN(name
))
5816 else if (strncmp(name
, attrprefix
, plen
) != 0)
5818 uint8_t nlen
= strlen(name
) - plen
;
5819 if (ap
->a_size
!= NULL
) {
5820 *ap
->a_size
+= 1 + nlen
;
5821 } else if (ap
->a_uio
!= NULL
) {
5823 * Format of extattr name entry is one byte for
5824 * length and the rest for name.
5826 error
= uiomove(&nlen
, 1, ap
->a_uio
);
5828 char *namep
= __DECONST(char *, name
) + plen
;
5829 error
= uiomove(namep
, nlen
, ap
->a_uio
);
5832 error
= SET_ERROR(error
);
5842 zfs_listextattr_impl(struct vop_listextattr_args
*ap
, boolean_t compat
)
5844 znode_t
*zp
= VTOZ(ap
->a_vp
);
5845 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5846 char attrprefix
[16];
5849 error
= zfs_create_attrname(ap
->a_attrnamespace
, "", attrprefix
,
5850 sizeof (attrprefix
), compat
);
5854 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
)
5855 error
= zfs_listextattr_sa(ap
, attrprefix
);
5857 error
= zfs_listextattr_dir(ap
, attrprefix
);
5862 * Vnode operation to retrieve extended attributes on a vnode.
5865 zfs_listextattr(struct vop_listextattr_args
*ap
)
5867 znode_t
*zp
= VTOZ(ap
->a_vp
);
5868 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
5871 if (ap
->a_size
!= NULL
)
5875 * If the xattr property is off, refuse the request.
5877 if (!(zfsvfs
->z_flags
& ZSB_XATTR
))
5878 return (SET_ERROR(EOPNOTSUPP
));
5880 error
= extattr_check_cred(ap
->a_vp
, ap
->a_attrnamespace
,
5881 ap
->a_cred
, ap
->a_td
, VREAD
);
5883 return (SET_ERROR(error
));
5885 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
5887 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
5889 error
= zfs_listextattr_impl(ap
, zfs_xattr_compat
);
5890 if (error
== 0 && ap
->a_attrnamespace
== EXTATTR_NAMESPACE_USER
) {
5891 /* Also list user xattrs with the alternate format. */
5892 error
= zfs_listextattr_impl(ap
, !zfs_xattr_compat
);
5895 rw_exit(&zp
->z_xattr_lock
);
5896 zfs_exit(zfsvfs
, FTAG
);
5900 #ifndef _SYS_SYSPROTO_H_
5901 struct vop_getacl_args
{
5911 zfs_freebsd_getacl(struct vop_getacl_args
*ap
)
5914 vsecattr_t vsecattr
;
5916 if (ap
->a_type
!= ACL_TYPE_NFS4
)
5919 vsecattr
.vsa_mask
= VSA_ACE
| VSA_ACECNT
;
5920 if ((error
= zfs_getsecattr(VTOZ(ap
->a_vp
),
5921 &vsecattr
, 0, ap
->a_cred
)))
5924 error
= acl_from_aces(ap
->a_aclp
, vsecattr
.vsa_aclentp
,
5925 vsecattr
.vsa_aclcnt
);
5926 if (vsecattr
.vsa_aclentp
!= NULL
)
5927 kmem_free(vsecattr
.vsa_aclentp
, vsecattr
.vsa_aclentsz
);
5932 #ifndef _SYS_SYSPROTO_H_
5933 struct vop_setacl_args
{
5943 zfs_freebsd_setacl(struct vop_setacl_args
*ap
)
5946 vsecattr_t vsecattr
;
5947 int aclbsize
; /* size of acl list in bytes */
5950 if (ap
->a_type
!= ACL_TYPE_NFS4
)
5953 if (ap
->a_aclp
== NULL
)
5956 if (ap
->a_aclp
->acl_cnt
< 1 || ap
->a_aclp
->acl_cnt
> MAX_ACL_ENTRIES
)
5960 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
5961 * splitting every entry into two and appending "canonical six"
5962 * entries at the end. Don't allow for setting an ACL that would
5963 * cause chmod(2) to run out of ACL entries.
5965 if (ap
->a_aclp
->acl_cnt
* 2 + 6 > ACL_MAX_ENTRIES
)
5968 error
= acl_nfs4_check(ap
->a_aclp
, ap
->a_vp
->v_type
== VDIR
);
5972 vsecattr
.vsa_mask
= VSA_ACE
;
5973 aclbsize
= ap
->a_aclp
->acl_cnt
* sizeof (ace_t
);
5974 vsecattr
.vsa_aclentp
= kmem_alloc(aclbsize
, KM_SLEEP
);
5975 aaclp
= vsecattr
.vsa_aclentp
;
5976 vsecattr
.vsa_aclentsz
= aclbsize
;
5978 aces_from_acl(vsecattr
.vsa_aclentp
, &vsecattr
.vsa_aclcnt
, ap
->a_aclp
);
5979 error
= zfs_setsecattr(VTOZ(ap
->a_vp
), &vsecattr
, 0, ap
->a_cred
);
5980 kmem_free(aaclp
, aclbsize
);
5985 #ifndef _SYS_SYSPROTO_H_
5986 struct vop_aclcheck_args
{
5996 zfs_freebsd_aclcheck(struct vop_aclcheck_args
*ap
)
5999 return (EOPNOTSUPP
);
6003 zfs_vptocnp(struct vop_vptocnp_args
*ap
)
6005 vnode_t
*covered_vp
;
6006 vnode_t
*vp
= ap
->a_vp
;
6007 zfsvfs_t
*zfsvfs
= vp
->v_vfsp
->vfs_data
;
6008 znode_t
*zp
= VTOZ(vp
);
6012 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
6016 * If we are a snapshot mounted under .zfs, run the operation
6017 * on the covered vnode.
6019 if (zp
->z_id
!= zfsvfs
->z_root
|| zfsvfs
->z_parent
== zfsvfs
) {
6020 char name
[MAXNAMLEN
+ 1];
6024 error
= zfs_znode_parent_and_name(zp
, &dzp
, name
,
6028 if (*ap
->a_buflen
< len
)
6029 error
= SET_ERROR(ENOMEM
);
6032 *ap
->a_buflen
-= len
;
6033 memcpy(ap
->a_buf
+ *ap
->a_buflen
, name
, len
);
6034 *ap
->a_vpp
= ZTOV(dzp
);
6036 zfs_exit(zfsvfs
, FTAG
);
6039 zfs_exit(zfsvfs
, FTAG
);
6041 covered_vp
= vp
->v_mount
->mnt_vnodecovered
;
6042 enum vgetstate vs
= vget_prep(covered_vp
);
6043 ltype
= VOP_ISLOCKED(vp
);
6045 error
= vget_finish(covered_vp
, LK_SHARED
, vs
);
6047 error
= VOP_VPTOCNP(covered_vp
, ap
->a_vpp
, ap
->a_buf
,
6051 vn_lock(vp
, ltype
| LK_RETRY
);
6052 if (VN_IS_DOOMED(vp
))
6053 error
= SET_ERROR(ENOENT
);
6057 #if __FreeBSD_version >= 1400032
6059 zfs_deallocate(struct vop_deallocate_args
*ap
)
6061 znode_t
*zp
= VTOZ(ap
->a_vp
);
6062 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
6064 off_t off
, len
, file_sz
;
6067 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
6071 * Callers might not be able to detect properly that we are read-only,
6072 * so check it explicitly here.
6074 if (zfs_is_readonly(zfsvfs
)) {
6075 zfs_exit(zfsvfs
, FTAG
);
6076 return (SET_ERROR(EROFS
));
6079 zilog
= zfsvfs
->z_log
;
6080 off
= *ap
->a_offset
;
6082 file_sz
= zp
->z_size
;
6083 if (off
+ len
> file_sz
)
6084 len
= file_sz
- off
;
6085 /* Fast path for out-of-range request. */
6088 zfs_exit(zfsvfs
, FTAG
);
6092 error
= zfs_freesp(zp
, off
, len
, O_RDWR
, TRUE
);
6094 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
||
6095 (ap
->a_ioflag
& IO_SYNC
) != 0)
6096 zil_commit(zilog
, zp
->z_id
);
6097 *ap
->a_offset
= off
+ len
;
6101 zfs_exit(zfsvfs
, FTAG
);
6106 #ifndef _SYS_SYSPROTO_H_
6107 struct vop_copy_file_range_args
{
6108 struct vnode
*a_invp
;
6110 struct vnode
*a_outvp
;
6113 unsigned int a_flags
;
6114 struct ucred
*a_incred
;
6115 struct ucred
*a_outcred
;
6116 struct thread
*a_fsizetd
;
6120 * TODO: FreeBSD will only call file system-specific copy_file_range() if both
6121 * files resides under the same mountpoint. In case of ZFS we want to be called
6122 * even is files are in different datasets (but on the same pools, but we need
6123 * to check that ourselves).
6126 zfs_freebsd_copy_file_range(struct vop_copy_file_range_args
*ap
)
6128 zfsvfs_t
*outzfsvfs
;
6129 struct vnode
*invp
= ap
->a_invp
;
6130 struct vnode
*outvp
= ap
->a_outvp
;
6133 uint64_t len
= *ap
->a_lenp
;
6135 if (!zfs_bclone_enabled
) {
6137 goto bad_write_fallback
;
6141 * TODO: If offset/length is not aligned to recordsize, use
6142 * vn_generic_copy_file_range() on this fragment.
6143 * It would be better to do this after we lock the vnodes, but then we
6144 * need something else than vn_generic_copy_file_range().
6147 vn_start_write(outvp
, &mp
, V_WAIT
);
6148 if (__predict_true(mp
== outvp
->v_mount
)) {
6149 outzfsvfs
= (zfsvfs_t
*)mp
->mnt_data
;
6150 if (!spa_feature_is_enabled(dmu_objset_spa(outzfsvfs
->z_os
),
6151 SPA_FEATURE_BLOCK_CLONING
)) {
6152 goto bad_write_fallback
;
6155 if (invp
== outvp
) {
6156 if (vn_lock(outvp
, LK_EXCLUSIVE
) != 0) {
6157 goto bad_write_fallback
;
6160 #if (__FreeBSD_version >= 1302506 && __FreeBSD_version < 1400000) || \
6161 __FreeBSD_version >= 1400086
6162 vn_lock_pair(invp
, false, LK_EXCLUSIVE
, outvp
, false,
6165 vn_lock_pair(invp
, false, outvp
, false);
6167 if (VN_IS_DOOMED(invp
) || VN_IS_DOOMED(outvp
)) {
6168 goto bad_locked_fallback
;
6173 error
= mac_vnode_check_write(curthread
->td_ucred
, ap
->a_outcred
,
6179 error
= zfs_clone_range(VTOZ(invp
), ap
->a_inoffp
, VTOZ(outvp
),
6180 ap
->a_outoffp
, &len
, ap
->a_outcred
);
6181 if (error
== EXDEV
|| error
== EAGAIN
|| error
== EINVAL
||
6182 error
== EOPNOTSUPP
)
6183 goto bad_locked_fallback
;
6184 *ap
->a_lenp
= (size_t)len
;
6192 vn_finished_write(mp
);
6195 bad_locked_fallback
:
6201 vn_finished_write(mp
);
6202 error
= vn_generic_copy_file_range(ap
->a_invp
, ap
->a_inoffp
,
6203 ap
->a_outvp
, ap
->a_outoffp
, ap
->a_lenp
, ap
->a_flags
,
6204 ap
->a_incred
, ap
->a_outcred
, ap
->a_fsizetd
);
6208 struct vop_vector zfs_vnodeops
;
6209 struct vop_vector zfs_fifoops
;
6210 struct vop_vector zfs_shareops
;
6212 struct vop_vector zfs_vnodeops
= {
6213 .vop_default
= &default_vnodeops
,
6214 .vop_inactive
= zfs_freebsd_inactive
,
6215 .vop_need_inactive
= zfs_freebsd_need_inactive
,
6216 .vop_reclaim
= zfs_freebsd_reclaim
,
6217 .vop_fplookup_vexec
= zfs_freebsd_fplookup_vexec
,
6218 .vop_fplookup_symlink
= zfs_freebsd_fplookup_symlink
,
6219 .vop_access
= zfs_freebsd_access
,
6220 .vop_allocate
= VOP_EINVAL
,
6221 #if __FreeBSD_version >= 1400032
6222 .vop_deallocate
= zfs_deallocate
,
6224 .vop_lookup
= zfs_cache_lookup
,
6225 .vop_cachedlookup
= zfs_freebsd_cachedlookup
,
6226 .vop_getattr
= zfs_freebsd_getattr
,
6227 .vop_setattr
= zfs_freebsd_setattr
,
6228 .vop_create
= zfs_freebsd_create
,
6229 .vop_mknod
= (vop_mknod_t
*)zfs_freebsd_create
,
6230 .vop_mkdir
= zfs_freebsd_mkdir
,
6231 .vop_readdir
= zfs_freebsd_readdir
,
6232 .vop_fsync
= zfs_freebsd_fsync
,
6233 .vop_open
= zfs_freebsd_open
,
6234 .vop_close
= zfs_freebsd_close
,
6235 .vop_rmdir
= zfs_freebsd_rmdir
,
6236 .vop_ioctl
= zfs_freebsd_ioctl
,
6237 .vop_link
= zfs_freebsd_link
,
6238 .vop_symlink
= zfs_freebsd_symlink
,
6239 .vop_readlink
= zfs_freebsd_readlink
,
6240 .vop_read
= zfs_freebsd_read
,
6241 .vop_write
= zfs_freebsd_write
,
6242 .vop_remove
= zfs_freebsd_remove
,
6243 .vop_rename
= zfs_freebsd_rename
,
6244 .vop_pathconf
= zfs_freebsd_pathconf
,
6245 .vop_bmap
= zfs_freebsd_bmap
,
6246 .vop_fid
= zfs_freebsd_fid
,
6247 .vop_getextattr
= zfs_getextattr
,
6248 .vop_deleteextattr
= zfs_deleteextattr
,
6249 .vop_setextattr
= zfs_setextattr
,
6250 .vop_listextattr
= zfs_listextattr
,
6251 .vop_getacl
= zfs_freebsd_getacl
,
6252 .vop_setacl
= zfs_freebsd_setacl
,
6253 .vop_aclcheck
= zfs_freebsd_aclcheck
,
6254 .vop_getpages
= zfs_freebsd_getpages
,
6255 .vop_putpages
= zfs_freebsd_putpages
,
6256 .vop_vptocnp
= zfs_vptocnp
,
6257 .vop_lock1
= vop_lock
,
6258 .vop_unlock
= vop_unlock
,
6259 .vop_islocked
= vop_islocked
,
6260 #if __FreeBSD_version >= 1400043
6261 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6263 .vop_copy_file_range
= zfs_freebsd_copy_file_range
,
6265 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops
);
6267 struct vop_vector zfs_fifoops
= {
6268 .vop_default
= &fifo_specops
,
6269 .vop_fsync
= zfs_freebsd_fsync
,
6270 .vop_fplookup_vexec
= zfs_freebsd_fplookup_vexec
,
6271 .vop_fplookup_symlink
= zfs_freebsd_fplookup_symlink
,
6272 .vop_access
= zfs_freebsd_access
,
6273 .vop_getattr
= zfs_freebsd_getattr
,
6274 .vop_inactive
= zfs_freebsd_inactive
,
6275 .vop_read
= VOP_PANIC
,
6276 .vop_reclaim
= zfs_freebsd_reclaim
,
6277 .vop_setattr
= zfs_freebsd_setattr
,
6278 .vop_write
= VOP_PANIC
,
6279 .vop_pathconf
= zfs_freebsd_pathconf
,
6280 .vop_fid
= zfs_freebsd_fid
,
6281 .vop_getacl
= zfs_freebsd_getacl
,
6282 .vop_setacl
= zfs_freebsd_setacl
,
6283 .vop_aclcheck
= zfs_freebsd_aclcheck
,
6284 #if __FreeBSD_version >= 1400043
6285 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6288 VFS_VOP_VECTOR_REGISTER(zfs_fifoops
);
6291 * special share hidden files vnode operations template
6293 struct vop_vector zfs_shareops
= {
6294 .vop_default
= &default_vnodeops
,
6295 .vop_fplookup_vexec
= VOP_EAGAIN
,
6296 .vop_fplookup_symlink
= VOP_EAGAIN
,
6297 .vop_access
= zfs_freebsd_access
,
6298 .vop_inactive
= zfs_freebsd_inactive
,
6299 .vop_reclaim
= zfs_freebsd_reclaim
,
6300 .vop_fid
= zfs_freebsd_fid
,
6301 .vop_pathconf
= zfs_freebsd_pathconf
,
6302 #if __FreeBSD_version >= 1400043
6303 .vop_add_writecount
= vop_stdadd_writecount_nomsync
,
6306 VFS_VOP_VECTOR_REGISTER(zfs_shareops
);
6308 ZFS_MODULE_PARAM(zfs
, zfs_
, xattr_compat
, INT
, ZMOD_RW
,
6309 "Use legacy ZFS xattr naming for writing new user namespace xattrs");