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 http://www.opensolaris.org/os/licensing.
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/types.h>
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sysmacros.h>
37 #include <sys/resource.h>
39 #include <sys/endian.h>
41 #include <sys/vnode.h>
42 #if __FreeBSD_version >= 1300102
45 #include <sys/dirent.h>
49 #include <sys/taskq.h>
51 #include <sys/atomic.h>
52 #include <sys/namei.h>
54 #include <sys/cmn_err.h>
56 #include <sys/sysproto.h>
57 #include <sys/errno.h>
58 #include <sys/unistd.h>
59 #include <sys/zfs_dir.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/dmu_objset.h>
69 #include <sys/policy.h>
70 #include <sys/sunddi.h>
71 #include <sys/filio.h>
73 #include <sys/zfs_ctldir.h>
74 #include <sys/zfs_fuid.h>
75 #include <sys/zfs_quota.h>
76 #include <sys/zfs_sa.h>
77 #include <sys/zfs_rlock.h>
78 #include <sys/extdirent.h>
81 #include <sys/sched.h>
83 #include <sys/vmmeter.h>
84 #include <vm/vm_param.h>
86 #include <sys/zfs_vnops.h>
88 #include <vm/vm_object.h>
90 #include <sys/extattr.h>
94 #define VN_OPEN_INVFS 0x0
99 #if __FreeBSD_version >= 1300047
100 #define vm_page_wire_lock(pp)
101 #define vm_page_wire_unlock(pp)
103 #define vm_page_wire_lock(pp) vm_page_lock(pp)
104 #define vm_page_wire_unlock(pp) vm_page_unlock(pp)
107 #ifdef DEBUG_VFS_LOCKS
108 #define VNCHECKREF(vp) \
109 VNASSERT((vp)->v_holdcnt > 0 && (vp)->v_usecount > 0, vp, \
110 ("%s: wrong ref counts", __func__));
112 #define VNCHECKREF(vp)
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
219 zfs_open(vnode_t **vpp, int flag, cred_t *cr)
221 znode_t *zp = VTOZ(*vpp);
222 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
227 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
228 ((flag & FAPPEND) == 0)) {
230 return (SET_ERROR(EPERM));
233 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
234 ZTOV(zp)->v_type == VREG &&
235 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
236 if (fs_vscan(*vpp, cr, 0) != 0) {
238 return (SET_ERROR(EACCES));
242 /* Keep a count of the synchronous opens in the znode */
243 if (flag & (FSYNC | FDSYNC))
244 atomic_inc_32(&zp->z_sync_cnt);
252 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
254 znode_t *zp = VTOZ(vp);
255 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
260 /* Decrement the synchronous opens in the znode */
261 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
262 atomic_dec_32(&zp->z_sync_cnt);
264 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
265 ZTOV(zp)->v_type == VREG &&
266 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
267 VERIFY(fs_vscan(vp, cr, 1) == 0);
274 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
275 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
278 zfs_holey(vnode_t *vp, ulong_t cmd, offset_t *off)
280 znode_t *zp = VTOZ(vp);
281 uint64_t noff = (uint64_t)*off; /* new offset */
286 file_sz = zp->z_size;
287 if (noff >= file_sz) {
288 return (SET_ERROR(ENXIO));
291 if (cmd == _FIO_SEEK_HOLE)
296 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
299 return (SET_ERROR(ENXIO));
301 /* file was dirty, so fall back to using generic logic */
302 if (error == EBUSY) {
310 * We could find a hole that begins after the logical end-of-file,
311 * because dmu_offset_next() only works on whole blocks. If the
312 * EOF falls mid-block, then indicate that the "virtual hole"
313 * at the end of the file begins at the logical EOF, rather than
314 * at the end of the last block.
316 if (noff > file_sz) {
329 zfs_ioctl(vnode_t *vp, ulong_t com, intptr_t data, int flag, cred_t *cred,
343 * The following two ioctls are used by bfu. Faking out,
344 * necessary to avoid bfu errors.
356 off = *(offset_t *)data;
358 zfsvfs = zp->z_zfsvfs;
362 /* offset parameter is in/out */
363 error = zfs_holey(vp, com, &off);
367 *(offset_t *)data = off;
371 return (SET_ERROR(ENOTTY));
375 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
382 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
383 * aligned boundaries, if the range is not aligned. As a result a
384 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
385 * It may happen that all DEV_BSIZE subranges are marked clean and thus
386 * the whole page would be considered clean despite have some
388 * For this reason we should shrink the range to DEV_BSIZE aligned
389 * boundaries before calling vm_page_clear_dirty.
391 end = rounddown2(off + nbytes, DEV_BSIZE);
392 off = roundup2(off, DEV_BSIZE);
396 zfs_vmobject_assert_wlocked_12(obj);
397 #if __FreeBSD_version < 1300050
399 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
401 if (vm_page_xbusied(pp)) {
403 * Reference the page before unlocking and
404 * sleeping so that the page daemon is less
405 * likely to reclaim it.
407 vm_page_reference(pp);
409 zfs_vmobject_wunlock(obj);
410 vm_page_busy_sleep(pp, "zfsmwb", true);
411 zfs_vmobject_wlock(obj);
415 } else if (pp != NULL) {
420 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
421 vm_object_pip_add(obj, 1);
422 pmap_remove_write(pp);
424 vm_page_clear_dirty(pp, off, nbytes);
429 vm_page_grab_valid_unlocked(&pp, obj, OFF_TO_IDX(start),
430 VM_ALLOC_NOCREAT | VM_ALLOC_SBUSY | VM_ALLOC_NORMAL |
433 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
434 vm_object_pip_add(obj, 1);
435 pmap_remove_write(pp);
437 vm_page_clear_dirty(pp, off, nbytes);
444 page_unbusy(vm_page_t pp)
448 #if __FreeBSD_version >= 1300041
449 vm_object_pip_wakeup(pp->object);
451 vm_object_pip_subtract(pp->object, 1);
455 #if __FreeBSD_version > 1300051
457 page_hold(vnode_t *vp, int64_t start)
463 vm_page_grab_valid_unlocked(&m, obj, OFF_TO_IDX(start),
464 VM_ALLOC_NOCREAT | VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY |
470 page_hold(vnode_t *vp, int64_t start)
476 zfs_vmobject_assert_wlocked(obj);
479 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
481 if (vm_page_xbusied(pp)) {
483 * Reference the page before unlocking and
484 * sleeping so that the page daemon is less
485 * likely to reclaim it.
487 vm_page_reference(pp);
489 zfs_vmobject_wunlock(obj);
490 vm_page_busy_sleep(pp, "zfsmwb", true);
491 zfs_vmobject_wlock(obj);
495 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
496 vm_page_wire_lock(pp);
498 vm_page_wire_unlock(pp);
509 page_unhold(vm_page_t pp)
512 vm_page_wire_lock(pp);
513 #if __FreeBSD_version >= 1300035
514 vm_page_unwire(pp, PQ_ACTIVE);
518 vm_page_wire_unlock(pp);
522 * When a file is memory mapped, we must keep the IO data synchronized
523 * between the DMU cache and the memory mapped pages. What this means:
525 * On Write: If we find a memory mapped page, we write to *both*
526 * the page and the dmu buffer.
529 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
530 int segflg, dmu_tx_t *tx)
537 ASSERT(segflg != UIO_NOCOPY);
538 ASSERT(vp->v_mount != NULL);
542 off = start & PAGEOFFSET;
543 zfs_vmobject_wlock_12(obj);
544 #if __FreeBSD_version >= 1300041
545 vm_object_pip_add(obj, 1);
547 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
549 int nbytes = imin(PAGESIZE - off, len);
551 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
552 zfs_vmobject_wunlock_12(obj);
554 va = zfs_map_page(pp, &sf);
555 (void) dmu_read(os, oid, start+off, nbytes,
556 va+off, DMU_READ_PREFETCH);
559 zfs_vmobject_wlock_12(obj);
565 #if __FreeBSD_version >= 1300041
566 vm_object_pip_wakeup(obj);
568 vm_object_pip_wakeupn(obj, 0);
570 zfs_vmobject_wunlock_12(obj);
574 * Read with UIO_NOCOPY flag means that sendfile(2) requests
575 * ZFS to populate a range of page cache pages with data.
577 * NOTE: this function could be optimized to pre-allocate
578 * all pages in advance, drain exclusive busy on all of them,
579 * map them into contiguous KVA region and populate them
580 * in one single dmu_read() call.
583 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
585 znode_t *zp = VTOZ(vp);
586 objset_t *os = zp->z_zfsvfs->z_os;
595 ASSERT(uio->uio_segflg == UIO_NOCOPY);
596 ASSERT(vp->v_mount != NULL);
599 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
601 zfs_vmobject_wlock_12(obj);
602 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
603 int bytes = MIN(PAGESIZE, len);
605 pp = vm_page_grab_unlocked(obj, OFF_TO_IDX(start),
606 VM_ALLOC_SBUSY | VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
607 if (vm_page_none_valid(pp)) {
608 zfs_vmobject_wunlock_12(obj);
609 va = zfs_map_page(pp, &sf);
610 error = dmu_read(os, zp->z_id, start, bytes, va,
612 if (bytes != PAGESIZE && error == 0)
613 bzero(va + bytes, PAGESIZE - bytes);
615 zfs_vmobject_wlock_12(obj);
616 #if __FreeBSD_version >= 1300081
619 vm_page_activate(pp);
620 vm_page_do_sunbusy(pp);
622 zfs_vmobject_wlock(obj);
623 if (!vm_page_wired(pp) && pp->valid == 0 &&
624 vm_page_busy_tryupgrade(pp))
628 zfs_vmobject_wunlock(obj);
631 vm_page_do_sunbusy(pp);
634 if (pp->wire_count == 0 && pp->valid == 0 &&
638 pp->valid = VM_PAGE_BITS_ALL;
639 vm_page_activate(pp);
644 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
645 vm_page_do_sunbusy(pp);
649 uio->uio_resid -= bytes;
650 uio->uio_offset += bytes;
653 zfs_vmobject_wunlock_12(obj);
658 * When a file is memory mapped, we must keep the IO data synchronized
659 * between the DMU cache and the memory mapped pages. What this means:
661 * On Read: We "read" preferentially from memory mapped pages,
662 * else we default from the dmu buffer.
664 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
665 * the file is memory mapped.
668 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
670 znode_t *zp = VTOZ(vp);
677 ASSERT(vp->v_mount != NULL);
681 start = uio->uio_loffset;
682 off = start & PAGEOFFSET;
683 zfs_vmobject_wlock_12(obj);
684 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
686 uint64_t bytes = MIN(PAGESIZE - off, len);
688 if ((pp = page_hold(vp, start))) {
692 zfs_vmobject_wunlock_12(obj);
693 va = zfs_map_page(pp, &sf);
694 error = vn_io_fault_uiomove(va + off, bytes, uio);
696 zfs_vmobject_wlock_12(obj);
699 zfs_vmobject_wunlock_12(obj);
700 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
702 zfs_vmobject_wlock_12(obj);
709 zfs_vmobject_wunlock_12(obj);
713 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
716 * Read bytes from specified file into supplied buffer.
718 * IN: vp - vnode of file to be read from.
719 * uio - structure supplying read location, range info,
721 * ioflag - SYNC flags; used to provide FRSYNC semantics.
722 * cr - credentials of caller.
723 * ct - caller context
725 * OUT: uio - updated offset and range, buffer filled.
727 * RETURN: 0 on success, error code on failure.
730 * vp - atime updated if byte count > 0
734 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr)
736 znode_t *zp = VTOZ(vp);
737 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
738 ssize_t n, nbytes, start_resid;
741 zfs_locked_range_t *lr;
746 /* We don't copy out anything useful for directories. */
747 if (vp->v_type == VDIR) {
749 return (SET_ERROR(EISDIR));
752 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
754 return (SET_ERROR(EACCES));
758 * Validate file offset
760 if (uio->uio_loffset < (offset_t)0) {
762 return (SET_ERROR(EINVAL));
766 * Fasttrack empty reads
768 if (uio->uio_resid == 0) {
774 * If we're in FRSYNC mode, sync out this znode before reading it.
777 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
778 zil_commit(zfsvfs->z_log, zp->z_id);
781 * Lock the range against changes.
783 lr = zfs_rangelock_enter(&zp->z_rangelock, uio->uio_loffset,
784 uio->uio_resid, RL_READER);
787 * If we are reading past end-of-file we can skip
788 * to the end; but we might still need to set atime.
790 if (uio->uio_loffset >= zp->z_size) {
795 ASSERT(uio->uio_loffset < zp->z_size);
796 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
800 nbytes = MIN(n, zfs_read_chunk_size -
801 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
803 if (uio->uio_segflg == UIO_NOCOPY)
804 error = mappedread_sf(vp, nbytes, uio);
805 else if (vn_has_cached_data(vp)) {
806 error = mappedread(vp, nbytes, uio);
808 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
812 /* convert checksum errors into IO errors */
814 error = SET_ERROR(EIO);
821 nread = start_resid - n;
822 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, nread);
825 zfs_rangelock_exit(lr);
827 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
833 * Write the bytes to a file.
835 * IN: vp - vnode of file to be written to.
836 * uio - structure supplying write location, range info,
838 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
839 * set if in append mode.
840 * cr - credentials of caller.
841 * ct - caller context (NFS/CIFS fem monitor only)
843 * OUT: uio - updated offset and range.
845 * RETURN: 0 on success, error code on failure.
848 * vp - ctime|mtime updated if byte count > 0
853 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr)
855 znode_t *zp = VTOZ(vp);
856 rlim64_t limit = MAXOFFSET_T;
857 ssize_t start_resid = uio->uio_resid;
862 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
866 zfs_locked_range_t *lr;
867 int max_blksz = zfsvfs->z_max_blksz;
870 iovec_t *aiov = NULL;
873 int iovcnt __unused = uio->uio_iovcnt;
874 iovec_t *iovp = uio->uio_iov;
877 sa_bulk_attr_t bulk[4];
878 uint64_t mtime[2], ctime[2];
879 uint64_t uid, gid, projid;
883 * Fasttrack empty write
889 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
895 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
896 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
897 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
899 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
903 * Callers might not be able to detect properly that we are read-only,
904 * so check it explicitly here.
906 if (zfs_is_readonly(zfsvfs)) {
908 return (SET_ERROR(EROFS));
912 * If immutable or not appending then return EPERM.
913 * Intentionally allow ZFS_READONLY through here.
914 * See zfs_zaccess_common()
916 if ((zp->z_pflags & ZFS_IMMUTABLE) ||
917 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
918 (uio->uio_loffset < zp->z_size))) {
920 return (SET_ERROR(EPERM));
923 zilog = zfsvfs->z_log;
926 * Validate file offset
928 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
931 return (SET_ERROR(EINVAL));
935 * If in append mode, set the io offset pointer to eof.
937 if (ioflag & FAPPEND) {
939 * Obtain an appending range lock to guarantee file append
940 * semantics. We reset the write offset once we have the lock.
942 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND);
943 woff = lr->lr_offset;
944 if (lr->lr_length == UINT64_MAX) {
946 * We overlocked the file because this write will cause
947 * the file block size to increase.
948 * Note that zp_size cannot change with this lock held.
952 uio->uio_loffset = woff;
955 * Note that if the file block size will change as a result of
956 * this write, then this range lock will lock the entire file
957 * so that we can re-write the block safely.
959 lr = zfs_rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER);
962 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
963 zfs_rangelock_exit(lr);
969 zfs_rangelock_exit(lr);
971 return (SET_ERROR(EFBIG));
974 if ((woff + n) > limit || woff > (limit - n))
977 /* Will this write extend the file length? */
978 write_eof = (woff + n > zp->z_size);
980 end_size = MAX(zp->z_size, woff + n);
984 projid = zp->z_projid;
987 * Write the file in reasonable size chunks. Each chunk is written
988 * in a separate transaction; this keeps the intent log records small
989 * and allows us to do more fine-grained space accounting.
992 woff = uio->uio_loffset;
994 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, uid) ||
995 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, gid) ||
996 (projid != ZFS_DEFAULT_PROJID &&
997 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
999 error = SET_ERROR(EDQUOT);
1005 ASSERT(i_iov < iovcnt);
1006 aiov = &iovp[i_iov];
1007 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1008 dmu_xuio_clear(xuio, i_iov);
1009 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1010 iovec_t *, aiov, arc_buf_t *, abuf);
1011 ASSERT((aiov->iov_base == abuf->b_data) ||
1012 ((char *)aiov->iov_base - (char *)abuf->b_data +
1013 aiov->iov_len == arc_buf_size(abuf)));
1015 } else if (n >= max_blksz &&
1016 woff >= zp->z_size &&
1017 P2PHASE(woff, max_blksz) == 0 &&
1018 zp->z_blksz == max_blksz) {
1020 * This write covers a full block. "Borrow" a buffer
1021 * from the dmu so that we can fill it before we enter
1022 * a transaction. This avoids the possibility of
1023 * holding up the transaction if the data copy hangs
1024 * up on a pagefault (e.g., from an NFS server mapping).
1028 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1030 ASSERT(abuf != NULL);
1031 ASSERT(arc_buf_size(abuf) == max_blksz);
1032 if ((error = uiocopy(abuf->b_data, max_blksz,
1033 UIO_WRITE, uio, &cbytes))) {
1034 dmu_return_arcbuf(abuf);
1037 ASSERT(cbytes == max_blksz);
1041 * Start a transaction.
1043 tx = dmu_tx_create(zfsvfs->z_os);
1044 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1045 db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
1047 dmu_tx_hold_write_by_dnode(tx, DB_DNODE(db), woff,
1050 zfs_sa_upgrade_txholds(tx, zp);
1051 error = dmu_tx_assign(tx, TXG_WAIT);
1055 dmu_return_arcbuf(abuf);
1060 * If zfs_range_lock() over-locked we grow the blocksize
1061 * and then reduce the lock range. This will only happen
1062 * on the first iteration since zfs_range_reduce() will
1063 * shrink down r_len to the appropriate size.
1065 if (lr->lr_length == UINT64_MAX) {
1068 if (zp->z_blksz > max_blksz) {
1070 * File's blocksize is already larger than the
1071 * "recordsize" property. Only let it grow to
1072 * the next power of 2.
1074 ASSERT(!ISP2(zp->z_blksz));
1075 new_blksz = MIN(end_size,
1076 1 << highbit64(zp->z_blksz));
1078 new_blksz = MIN(end_size, max_blksz);
1080 zfs_grow_blocksize(zp, new_blksz, tx);
1081 zfs_rangelock_reduce(lr, woff, n);
1085 * XXX - should we really limit each write to z_max_blksz?
1086 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1088 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1090 if (woff + nbytes > zp->z_size)
1091 vnode_pager_setsize(vp, woff + nbytes);
1094 tx_bytes = uio->uio_resid;
1095 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1097 tx_bytes -= uio->uio_resid;
1100 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1102 * If this is not a full block write, but we are
1103 * extending the file past EOF and this data starts
1104 * block-aligned, use assign_arcbuf(). Otherwise,
1105 * write via dmu_write().
1107 if (tx_bytes < max_blksz && (!write_eof ||
1108 aiov->iov_base != abuf->b_data)) {
1110 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1111 aiov->iov_len, aiov->iov_base, tx);
1112 dmu_return_arcbuf(abuf);
1113 xuio_stat_wbuf_copied();
1115 ASSERT(xuio || tx_bytes == max_blksz);
1116 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl), woff,
1119 ASSERT(tx_bytes <= uio->uio_resid);
1120 uioskip(uio, tx_bytes);
1122 if (tx_bytes && vn_has_cached_data(vp)) {
1123 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1124 zp->z_id, uio->uio_segflg, tx);
1128 * If we made no progress, we're done. If we made even
1129 * partial progress, update the znode and ZIL accordingly.
1131 if (tx_bytes == 0) {
1132 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1133 (void *)&zp->z_size, sizeof (uint64_t), tx);
1140 * Clear Set-UID/Set-GID bits on successful write if not
1141 * privileged and at least one of the execute bits is set.
1143 * It would be nice to to this after all writes have
1144 * been done, but that would still expose the ISUID/ISGID
1145 * to another app after the partial write is committed.
1147 * Note: we don't call zfs_fuid_map_id() here because
1148 * user 0 is not an ephemeral uid.
1150 mutex_enter(&zp->z_acl_lock);
1151 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1152 (S_IXUSR >> 6))) != 0 &&
1153 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1154 secpolicy_vnode_setid_retain(vp, cr,
1155 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1157 zp->z_mode &= ~(S_ISUID | S_ISGID);
1158 newmode = zp->z_mode;
1159 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1160 (void *)&newmode, sizeof (uint64_t), tx);
1162 mutex_exit(&zp->z_acl_lock);
1164 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
1167 * Update the file size (zp_size) if it has changed;
1168 * account for possible concurrent updates.
1170 while ((end_size = zp->z_size) < uio->uio_loffset) {
1171 (void) atomic_cas_64(&zp->z_size, end_size,
1173 ASSERT(error == 0 || error == EFAULT);
1176 * If we are replaying and eof is non zero then force
1177 * the file size to the specified eof. Note, there's no
1178 * concurrency during replay.
1180 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1181 zp->z_size = zfsvfs->z_replay_eof;
1184 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1186 (void) sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1188 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes,
1189 ioflag, NULL, NULL);
1194 ASSERT(tx_bytes == nbytes);
1199 zfs_rangelock_exit(lr);
1202 * If we're in replay mode, or we made no progress, return error.
1203 * Otherwise, it's at least a partial write, so it's successful.
1205 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1211 * EFAULT means that at least one page of the source buffer was not
1212 * available. VFS will re-try remaining I/O upon this error.
1214 if (error == EFAULT) {
1219 if (ioflag & (FSYNC | FDSYNC) ||
1220 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1221 zil_commit(zilog, zp->z_id);
1223 nwritten = start_resid - uio->uio_resid;
1224 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, nwritten);
1231 zfs_write_simple(znode_t *zp, const void *data, size_t len,
1232 loff_t pos, size_t *presid)
1237 error = vn_rdwr(UIO_WRITE, ZTOV(zp), __DECONST(void *, data), len, pos,
1238 UIO_SYSSPACE, IO_SYNC, kcred, NOCRED, &resid, curthread);
1241 return (SET_ERROR(error));
1242 } else if (presid == NULL) {
1244 error = SET_ERROR(EIO);
1253 zfs_get_done(zgd_t *zgd, int error)
1255 znode_t *zp = zgd->zgd_private;
1256 objset_t *os = zp->z_zfsvfs->z_os;
1259 dmu_buf_rele(zgd->zgd_db, zgd);
1261 zfs_rangelock_exit(zgd->zgd_lr);
1264 * Release the vnode asynchronously as we currently have the
1265 * txg stopped from syncing.
1267 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_zrele_taskq(dmu_objset_pool(os)));
1269 kmem_free(zgd, sizeof (zgd_t));
1273 static int zil_fault_io = 0;
1277 * Get data to generate a TX_WRITE intent log record.
1280 zfs_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1282 zfsvfs_t *zfsvfs = arg;
1283 objset_t *os = zfsvfs->z_os;
1285 uint64_t object = lr->lr_foid;
1286 uint64_t offset = lr->lr_offset;
1287 uint64_t size = lr->lr_length;
1292 ASSERT3P(lwb, !=, NULL);
1293 ASSERT3P(zio, !=, NULL);
1294 ASSERT3U(size, !=, 0);
1297 * Nothing to do if the file has been removed
1299 if (zfs_zget(zfsvfs, object, &zp) != 0)
1300 return (SET_ERROR(ENOENT));
1301 if (zp->z_unlinked) {
1303 * Release the vnode asynchronously as we currently have the
1304 * txg stopped from syncing.
1306 VN_RELE_ASYNC(ZTOV(zp),
1307 dsl_pool_zrele_taskq(dmu_objset_pool(os)));
1308 return (SET_ERROR(ENOENT));
1311 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1313 zgd->zgd_private = zp;
1316 * Write records come in two flavors: immediate and indirect.
1317 * For small writes it's cheaper to store the data with the
1318 * log record (immediate); for large writes it's cheaper to
1319 * sync the data and get a pointer to it (indirect) so that
1320 * we don't have to write the data twice.
1322 if (buf != NULL) { /* immediate write */
1323 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock, offset,
1325 /* test for truncation needs to be done while range locked */
1326 if (offset >= zp->z_size) {
1327 error = SET_ERROR(ENOENT);
1329 error = dmu_read(os, object, offset, size, buf,
1330 DMU_READ_NO_PREFETCH);
1332 ASSERT(error == 0 || error == ENOENT);
1333 } else { /* indirect write */
1335 * Have to lock the whole block to ensure when it's
1336 * written out and its checksum is being calculated
1337 * that no one can change the data. We need to re-check
1338 * blocksize after we get the lock in case it's changed!
1343 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1345 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock,
1346 offset, size, RL_READER);
1347 if (zp->z_blksz == size)
1350 zfs_rangelock_exit(zgd->zgd_lr);
1352 /* test for truncation needs to be done while range locked */
1353 if (lr->lr_offset >= zp->z_size)
1354 error = SET_ERROR(ENOENT);
1357 error = SET_ERROR(EIO);
1362 error = dmu_buf_hold(os, object, offset, zgd, &db,
1363 DMU_READ_NO_PREFETCH);
1366 blkptr_t *bp = &lr->lr_blkptr;
1371 ASSERT(db->db_offset == offset);
1372 ASSERT(db->db_size == size);
1374 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1376 ASSERT(error || lr->lr_length <= size);
1379 * On success, we need to wait for the write I/O
1380 * initiated by dmu_sync() to complete before we can
1381 * release this dbuf. We will finish everything up
1382 * in the zfs_get_done() callback.
1387 if (error == EALREADY) {
1388 lr->lr_common.lrc_txtype = TX_WRITE2;
1390 * TX_WRITE2 relies on the data previously
1391 * written by the TX_WRITE that caused
1392 * EALREADY. We zero out the BP because
1393 * it is the old, currently-on-disk BP,
1394 * so there's no need to zio_flush() its
1395 * vdevs (flushing would needlesly hurt
1396 * performance, and doesn't work on
1406 zfs_get_done(zgd, error);
1413 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1414 caller_context_t *ct)
1416 znode_t *zp = VTOZ(vp);
1417 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1423 if (flag & V_ACE_MASK)
1424 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1426 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1433 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1438 error = vn_lock(*vpp, lkflags);
1445 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
1447 znode_t *zdp = VTOZ(dvp);
1448 zfsvfs_t *zfsvfs __unused = zdp->z_zfsvfs;
1452 if (zfsvfs->z_replay == B_FALSE)
1453 ASSERT_VOP_LOCKED(dvp, __func__);
1455 if ((zdp->z_pflags & ZFS_XATTR) == 0)
1456 VERIFY(!RRM_LOCK_HELD(&zfsvfs->z_teardown_lock));
1459 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
1460 ASSERT3P(dvp, ==, vp);
1462 ltype = lkflags & LK_TYPE_MASK;
1463 if (ltype != VOP_ISLOCKED(dvp)) {
1464 if (ltype == LK_EXCLUSIVE)
1465 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1466 else /* if (ltype == LK_SHARED) */
1467 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1470 * Relock for the "." case could leave us with
1473 if (VN_IS_DOOMED(dvp)) {
1475 return (SET_ERROR(ENOENT));
1479 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
1481 * Note that in this case, dvp is the child vnode, and we
1482 * are looking up the parent vnode - exactly reverse from
1483 * normal operation. Unlocking dvp requires some rather
1484 * tricky unlock/relock dance to prevent mp from being freed;
1485 * use vn_vget_ino_gen() which takes care of all that.
1487 * XXX Note that there is a time window when both vnodes are
1488 * unlocked. It is possible, although highly unlikely, that
1489 * during that window the parent-child relationship between
1490 * the vnodes may change, for example, get reversed.
1491 * In that case we would have a wrong lock order for the vnodes.
1492 * All other filesystems seem to ignore this problem, so we
1494 * A potential solution could be implemented as follows:
1495 * - using LK_NOWAIT when locking the second vnode and retrying
1497 * - checking that the parent-child relationship still holds
1498 * after locking both vnodes and retrying if it doesn't
1500 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
1503 error = vn_lock(vp, lkflags);
1511 * Lookup an entry in a directory, or an extended attribute directory.
1512 * If it exists, return a held vnode reference for it.
1514 * IN: dvp - vnode of directory to search.
1515 * nm - name of entry to lookup.
1516 * pnp - full pathname to lookup [UNUSED].
1517 * flags - LOOKUP_XATTR set if looking for an attribute.
1518 * rdir - root directory vnode [UNUSED].
1519 * cr - credentials of caller.
1520 * ct - caller context
1522 * OUT: vpp - vnode of located entry, NULL if not found.
1524 * RETURN: 0 on success, error code on failure.
1531 zfs_lookup(vnode_t *dvp, const char *nm, vnode_t **vpp,
1532 struct componentname *cnp, int nameiop, cred_t *cr, kthread_t *td,
1533 int flags, boolean_t cached)
1535 znode_t *zdp = VTOZ(dvp);
1537 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1541 * Fast path lookup, however we must skip DNLC lookup
1542 * for case folding or normalizing lookups because the
1543 * DNLC code only stores the passed in name. This means
1544 * creating 'a' and removing 'A' on a case insensitive
1545 * file system would work, but DNLC still thinks 'a'
1546 * exists and won't let you create it again on the next
1547 * pass through fast path.
1549 if (!(flags & LOOKUP_XATTR)) {
1550 if (dvp->v_type != VDIR) {
1551 return (SET_ERROR(ENOTDIR));
1552 } else if (zdp->z_sa_hdl == NULL) {
1553 return (SET_ERROR(EIO));
1557 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp,
1565 if (flags & LOOKUP_XATTR) {
1567 * If the xattr property is off, refuse the lookup request.
1569 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
1571 return (SET_ERROR(EOPNOTSUPP));
1575 * We don't allow recursive attributes..
1576 * Maybe someday we will.
1578 if (zdp->z_pflags & ZFS_XATTR) {
1580 return (SET_ERROR(EINVAL));
1583 if ((error = zfs_get_xattrdir(VTOZ(dvp), &zp, cr, flags))) {
1590 * Do we have permission to get into attribute directory?
1592 error = zfs_zaccess(zp, ACE_EXECUTE, 0, B_FALSE, cr);
1602 * Check accessibility of directory if we're not coming in via
1607 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
1608 cnp->cn_flags &= ~NOEXECCHECK;
1611 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1617 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1618 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1620 return (SET_ERROR(EILSEQ));
1625 * First handle the special cases.
1627 if ((cnp->cn_flags & ISDOTDOT) != 0) {
1629 * If we are a snapshot mounted under .zfs, return
1630 * the vp for the snapshot directory.
1632 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
1633 struct componentname cn;
1638 ltype = VOP_ISLOCKED(dvp);
1640 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
1643 cn.cn_nameptr = "snapshot";
1644 cn.cn_namelen = strlen(cn.cn_nameptr);
1645 cn.cn_nameiop = cnp->cn_nameiop;
1646 cn.cn_flags = cnp->cn_flags & ~ISDOTDOT;
1647 cn.cn_lkflags = cnp->cn_lkflags;
1648 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
1651 vn_lock(dvp, ltype | LK_RETRY);
1655 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
1657 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
1658 return (SET_ERROR(ENOTSUP));
1659 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
1664 * The loop is retry the lookup if the parent-child relationship
1665 * changes during the dot-dot locking complexities.
1670 error = zfs_dirlook(zdp, nm, &zp);
1678 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
1681 * If we've got a locking error, then the vnode
1682 * got reclaimed because of a force unmount.
1683 * We never enter doomed vnodes into the name cache.
1689 if ((cnp->cn_flags & ISDOTDOT) == 0)
1693 if (zdp->z_sa_hdl == NULL) {
1694 error = SET_ERROR(EIO);
1696 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1697 &parent, sizeof (parent));
1704 if (zp->z_id == parent) {
1714 /* Translate errors and add SAVENAME when needed. */
1715 if (cnp->cn_flags & ISLASTCN) {
1719 if (error == ENOENT) {
1720 error = EJUSTRETURN;
1721 cnp->cn_flags |= SAVENAME;
1727 cnp->cn_flags |= SAVENAME;
1732 /* Insert name into cache (as non-existent) if appropriate. */
1733 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1734 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1735 cache_enter(dvp, NULL, cnp);
1737 /* Insert name into cache if appropriate. */
1738 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay &&
1739 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1740 if (!(cnp->cn_flags & ISLASTCN) ||
1741 (nameiop != DELETE && nameiop != RENAME)) {
1742 cache_enter(dvp, *vpp, cnp);
1750 * Attempt to create a new entry in a directory. If the entry
1751 * already exists, truncate the file if permissible, else return
1752 * an error. Return the vp of the created or trunc'd file.
1754 * IN: dvp - vnode of directory to put new file entry in.
1755 * name - name of new file entry.
1756 * vap - attributes of new file.
1757 * excl - flag indicating exclusive or non-exclusive mode.
1758 * mode - mode to open file with.
1759 * cr - credentials of caller.
1760 * flag - large file flag [UNUSED].
1761 * ct - caller context
1762 * vsecp - ACL to be set
1764 * OUT: vpp - vnode of created or trunc'd entry.
1766 * RETURN: 0 on success, error code on failure.
1769 * dvp - ctime|mtime updated if new entry created
1770 * vp - ctime|mtime always, atime if new
1775 zfs_create(znode_t *dzp, const char *name, vattr_t *vap, int excl, int mode,
1776 znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp)
1779 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1786 gid_t gid = crgetgid(cr);
1787 uint64_t projid = ZFS_DEFAULT_PROJID;
1788 zfs_acl_ids_t acl_ids;
1789 boolean_t fuid_dirtied;
1791 #ifdef DEBUG_VFS_LOCKS
1792 vnode_t *dvp = ZTOV(dzp);
1796 * If we have an ephemeral id, ACL, or XVATTR then
1797 * make sure file system is at proper version
1800 ksid = crgetsid(cr, KSID_OWNER);
1802 uid = ksid_getid(ksid);
1806 if (zfsvfs->z_use_fuids == B_FALSE &&
1807 (vsecp || (vap->va_mask & AT_XVATTR) ||
1808 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1809 return (SET_ERROR(EINVAL));
1814 zilog = zfsvfs->z_log;
1816 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1817 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1819 return (SET_ERROR(EILSEQ));
1822 if (vap->va_mask & AT_XVATTR) {
1823 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
1824 crgetuid(cr), cr, vap->va_type)) != 0) {
1832 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1833 vap->va_mode &= ~S_ISVTX;
1835 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1840 ASSERT3P(zp, ==, NULL);
1843 * Create a new file object and update the directory
1846 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1851 * We only support the creation of regular files in
1852 * extended attribute directories.
1855 if ((dzp->z_pflags & ZFS_XATTR) &&
1856 (vap->va_type != VREG)) {
1857 error = SET_ERROR(EINVAL);
1861 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1862 cr, vsecp, &acl_ids)) != 0)
1865 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
1866 projid = zfs_inherit_projid(dzp);
1867 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1868 zfs_acl_ids_free(&acl_ids);
1869 error = SET_ERROR(EDQUOT);
1873 getnewvnode_reserve_();
1875 tx = dmu_tx_create(os);
1877 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1878 ZFS_SA_BASE_ATTR_SIZE);
1880 fuid_dirtied = zfsvfs->z_fuid_dirty;
1882 zfs_fuid_txhold(zfsvfs, tx);
1883 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1884 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1885 if (!zfsvfs->z_use_sa &&
1886 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1887 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1888 0, acl_ids.z_aclp->z_acl_bytes);
1890 error = dmu_tx_assign(tx, TXG_WAIT);
1892 zfs_acl_ids_free(&acl_ids);
1894 getnewvnode_drop_reserve();
1898 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1900 zfs_fuid_sync(zfsvfs, tx);
1902 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1903 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1904 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1905 vsecp, acl_ids.z_fuidp, vap);
1906 zfs_acl_ids_free(&acl_ids);
1909 getnewvnode_drop_reserve();
1917 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1918 zil_commit(zilog, 0);
1925 * Remove an entry from a directory.
1927 * IN: dvp - vnode of directory to remove entry from.
1928 * name - name of entry to remove.
1929 * cr - credentials of caller.
1930 * ct - caller context
1931 * flags - case flags
1933 * RETURN: 0 on success, error code on failure.
1937 * vp - ctime (if nlink > 0)
1942 zfs_remove_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
1944 znode_t *dzp = VTOZ(dvp);
1947 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1961 zilog = zfsvfs->z_log;
1966 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1971 * Need to use rmdir for removing directories.
1973 if (vp->v_type == VDIR) {
1974 error = SET_ERROR(EPERM);
1978 vnevent_remove(vp, dvp, name, ct);
1982 /* are there any extended attributes? */
1983 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1984 &xattr_obj, sizeof (xattr_obj));
1985 if (error == 0 && xattr_obj) {
1986 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1991 * We may delete the znode now, or we may put it in the unlinked set;
1992 * it depends on whether we're the last link, and on whether there are
1993 * other holds on the vnode. So we dmu_tx_hold() the right things to
1994 * allow for either case.
1996 tx = dmu_tx_create(zfsvfs->z_os);
1997 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1998 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1999 zfs_sa_upgrade_txholds(tx, zp);
2000 zfs_sa_upgrade_txholds(tx, dzp);
2003 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2004 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
2007 /* charge as an update -- would be nice not to charge at all */
2008 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2011 * Mark this transaction as typically resulting in a net free of space
2013 dmu_tx_mark_netfree(tx);
2015 error = dmu_tx_assign(tx, TXG_WAIT);
2023 * Remove the directory entry.
2025 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
2033 zfs_unlinked_add(zp, tx);
2034 vp->v_vflag |= VV_NOSYNC;
2036 /* XXX check changes to linux vnops */
2038 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
2046 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2047 zil_commit(zilog, 0);
2056 zfs_lookup_internal(znode_t *dzp, const char *name, vnode_t **vpp,
2057 struct componentname *cnp, int nameiop)
2059 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2062 cnp->cn_nameptr = __DECONST(char *, name);
2063 cnp->cn_namelen = strlen(name);
2064 cnp->cn_nameiop = nameiop;
2065 cnp->cn_flags = ISLASTCN | SAVENAME;
2066 cnp->cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
2067 cnp->cn_cred = kcred;
2068 cnp->cn_thread = curthread;
2070 if (zfsvfs->z_use_namecache && !zfsvfs->z_replay) {
2071 struct vop_lookup_args a;
2073 a.a_gen.a_desc = &vop_lookup_desc;
2074 a.a_dvp = ZTOV(dzp);
2077 error = vfs_cache_lookup(&a);
2079 error = zfs_lookup(ZTOV(dzp), name, vpp, cnp, nameiop, kcred,
2080 curthread, 0, B_FALSE);
2084 printf("got error %d on name %s on op %d\n", error, name,
2093 zfs_remove(znode_t *dzp, const char *name, cred_t *cr, int flags)
2097 struct componentname cn;
2099 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
2102 error = zfs_remove_(ZTOV(dzp), vp, name, cr);
2107 * Create a new directory and insert it into dvp using the name
2108 * provided. Return a pointer to the inserted directory.
2110 * IN: dvp - vnode of directory to add subdir to.
2111 * dirname - name of new directory.
2112 * vap - attributes of new directory.
2113 * cr - credentials of caller.
2114 * ct - caller context
2115 * flags - case flags
2116 * vsecp - ACL to be set
2118 * OUT: vpp - vnode of created directory.
2120 * RETURN: 0 on success, error code on failure.
2123 * dvp - ctime|mtime updated
2124 * vp - ctime|mtime|atime updated
2128 zfs_mkdir(znode_t *dzp, const char *dirname, vattr_t *vap, znode_t **zpp,
2129 cred_t *cr, int flags, vsecattr_t *vsecp)
2132 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2139 gid_t gid = crgetgid(cr);
2140 zfs_acl_ids_t acl_ids;
2141 boolean_t fuid_dirtied;
2143 ASSERT(vap->va_type == VDIR);
2146 * If we have an ephemeral id, ACL, or XVATTR then
2147 * make sure file system is at proper version
2150 ksid = crgetsid(cr, KSID_OWNER);
2152 uid = ksid_getid(ksid);
2155 if (zfsvfs->z_use_fuids == B_FALSE &&
2156 ((vap->va_mask & AT_XVATTR) ||
2157 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2158 return (SET_ERROR(EINVAL));
2162 zilog = zfsvfs->z_log;
2164 if (dzp->z_pflags & ZFS_XATTR) {
2166 return (SET_ERROR(EINVAL));
2169 if (zfsvfs->z_utf8 && u8_validate(dirname,
2170 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2172 return (SET_ERROR(EILSEQ));
2175 if (vap->va_mask & AT_XVATTR) {
2176 if ((error = secpolicy_xvattr(ZTOV(dzp), (xvattr_t *)vap,
2177 crgetuid(cr), cr, vap->va_type)) != 0) {
2183 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2184 NULL, &acl_ids)) != 0) {
2190 * First make sure the new directory doesn't exist.
2192 * Existence is checked first to make sure we don't return
2193 * EACCES instead of EEXIST which can cause some applications
2198 if ((error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW))) {
2199 zfs_acl_ids_free(&acl_ids);
2203 ASSERT3P(zp, ==, NULL);
2205 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
2206 zfs_acl_ids_free(&acl_ids);
2211 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
2212 zfs_acl_ids_free(&acl_ids);
2214 return (SET_ERROR(EDQUOT));
2218 * Add a new entry to the directory.
2220 getnewvnode_reserve_();
2221 tx = dmu_tx_create(zfsvfs->z_os);
2222 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2223 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2224 fuid_dirtied = zfsvfs->z_fuid_dirty;
2226 zfs_fuid_txhold(zfsvfs, tx);
2227 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2228 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2229 acl_ids.z_aclp->z_acl_bytes);
2232 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2233 ZFS_SA_BASE_ATTR_SIZE);
2235 error = dmu_tx_assign(tx, TXG_WAIT);
2237 zfs_acl_ids_free(&acl_ids);
2239 getnewvnode_drop_reserve();
2247 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2250 zfs_fuid_sync(zfsvfs, tx);
2253 * Now put new name in parent dir.
2255 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
2259 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
2260 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
2261 acl_ids.z_fuidp, vap);
2263 zfs_acl_ids_free(&acl_ids);
2267 getnewvnode_drop_reserve();
2269 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2270 zil_commit(zilog, 0);
2277 * Remove a directory subdir entry. If the current working
2278 * directory is the same as the subdir to be removed, the
2281 * IN: dvp - vnode of directory to remove from.
2282 * name - name of directory to be removed.
2283 * cwd - vnode of current working directory.
2284 * cr - credentials of caller.
2285 * ct - caller context
2286 * flags - case flags
2288 * RETURN: 0 on success, error code on failure.
2291 * dvp - ctime|mtime updated
2295 zfs_rmdir_(vnode_t *dvp, vnode_t *vp, const char *name, cred_t *cr)
2297 znode_t *dzp = VTOZ(dvp);
2298 znode_t *zp = VTOZ(vp);
2299 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2307 zilog = zfsvfs->z_log;
2310 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
2314 if (vp->v_type != VDIR) {
2315 error = SET_ERROR(ENOTDIR);
2319 vnevent_rmdir(vp, dvp, name, ct);
2321 tx = dmu_tx_create(zfsvfs->z_os);
2322 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2323 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2324 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2325 zfs_sa_upgrade_txholds(tx, zp);
2326 zfs_sa_upgrade_txholds(tx, dzp);
2327 dmu_tx_mark_netfree(tx);
2328 error = dmu_tx_assign(tx, TXG_WAIT);
2337 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
2340 uint64_t txtype = TX_RMDIR;
2341 zfs_log_remove(zilog, tx, txtype, dzp, name,
2342 ZFS_NO_OBJECT, B_FALSE);
2349 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2350 zil_commit(zilog, 0);
2357 zfs_rmdir(znode_t *dzp, const char *name, znode_t *cwd, cred_t *cr, int flags)
2359 struct componentname cn;
2363 if ((error = zfs_lookup_internal(dzp, name, &vp, &cn, DELETE)))
2366 error = zfs_rmdir_(ZTOV(dzp), vp, name, cr);
2372 * Read as many directory entries as will fit into the provided
2373 * buffer from the given directory cursor position (specified in
2374 * the uio structure).
2376 * IN: vp - vnode of directory to read.
2377 * uio - structure supplying read location, range info,
2378 * and return buffer.
2379 * cr - credentials of caller.
2380 * ct - caller context
2381 * flags - case flags
2383 * OUT: uio - updated offset and range, buffer filled.
2384 * eofp - set to true if end-of-file detected.
2386 * RETURN: 0 on success, error code on failure.
2389 * vp - atime updated
2391 * Note that the low 4 bits of the cookie returned by zap is always zero.
2392 * This allows us to use the low range for "special" directory entries:
2393 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2394 * we use the offset 2 for the '.zfs' directory.
2398 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2399 int *ncookies, ulong_t **cookies)
2401 znode_t *zp = VTOZ(vp);
2405 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2410 zap_attribute_t zap;
2411 uint_t bytes_wanted;
2412 uint64_t offset; /* must be unsigned; checks for < 1 */
2418 boolean_t check_sysattrs;
2421 ulong_t *cooks = NULL;
2427 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2428 &parent, sizeof (parent))) != 0) {
2434 * If we are not given an eof variable,
2441 * Check for valid iov_len.
2443 if (uio->uio_iov->iov_len <= 0) {
2445 return (SET_ERROR(EINVAL));
2449 * Quit if directory has been removed (posix)
2451 if ((*eofp = zp->z_unlinked) != 0) {
2458 offset = uio->uio_loffset;
2459 prefetch = zp->z_zn_prefetch;
2462 * Initialize the iterator cursor.
2466 * Start iteration from the beginning of the directory.
2468 zap_cursor_init(&zc, os, zp->z_id);
2471 * The offset is a serialized cursor.
2473 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2477 * Get space to change directory entries into fs independent format.
2479 iovp = uio->uio_iov;
2480 bytes_wanted = iovp->iov_len;
2481 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2482 bufsize = bytes_wanted;
2483 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2484 odp = (struct dirent64 *)outbuf;
2486 bufsize = bytes_wanted;
2488 odp = (struct dirent64 *)iovp->iov_base;
2490 eodp = (struct edirent *)odp;
2492 if (ncookies != NULL) {
2494 * Minimum entry size is dirent size and 1 byte for a file name.
2496 ncooks = uio->uio_resid / (sizeof (struct dirent) -
2497 sizeof (((struct dirent *)NULL)->d_name) + 1);
2498 cooks = malloc(ncooks * sizeof (ulong_t), M_TEMP, M_WAITOK);
2503 * If this VFS supports the system attribute view interface; and
2504 * we're looking at an extended attribute directory; and we care
2505 * about normalization conflicts on this vfs; then we must check
2506 * for normalization conflicts with the sysattr name space.
2509 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2510 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2511 (flags & V_RDDIR_ENTFLAGS);
2517 * Transform to file-system independent format
2520 while (outcount < bytes_wanted) {
2523 off64_t *next = NULL;
2526 * Special case `.', `..', and `.zfs'.
2529 (void) strcpy(zap.za_name, ".");
2530 zap.za_normalization_conflict = 0;
2533 } else if (offset == 1) {
2534 (void) strcpy(zap.za_name, "..");
2535 zap.za_normalization_conflict = 0;
2538 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2539 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2540 zap.za_normalization_conflict = 0;
2541 objnum = ZFSCTL_INO_ROOT;
2547 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2548 if ((*eofp = (error == ENOENT)) != 0)
2554 if (zap.za_integer_length != 8 ||
2555 zap.za_num_integers != 1) {
2556 cmn_err(CE_WARN, "zap_readdir: bad directory "
2557 "entry, obj = %lld, offset = %lld\n",
2558 (u_longlong_t)zp->z_id,
2559 (u_longlong_t)offset);
2560 error = SET_ERROR(ENXIO);
2564 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2566 * MacOS X can extract the object type here such as:
2567 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2569 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2571 if (check_sysattrs && !zap.za_normalization_conflict) {
2573 zap.za_normalization_conflict =
2574 xattr_sysattr_casechk(zap.za_name);
2576 panic("%s:%u: TODO", __func__, __LINE__);
2581 if (flags & V_RDDIR_ACCFILTER) {
2583 * If we have no access at all, don't include
2584 * this entry in the returned information
2587 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2589 if (!zfs_has_access(ezp, cr)) {
2596 if (flags & V_RDDIR_ENTFLAGS)
2597 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2599 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2602 * Will this entry fit in the buffer?
2604 if (outcount + reclen > bufsize) {
2606 * Did we manage to fit anything in the buffer?
2609 error = SET_ERROR(EINVAL);
2614 if (flags & V_RDDIR_ENTFLAGS) {
2616 * Add extended flag entry:
2618 eodp->ed_ino = objnum;
2619 eodp->ed_reclen = reclen;
2620 /* NOTE: ed_off is the offset for the *next* entry */
2621 next = &(eodp->ed_off);
2622 eodp->ed_eflags = zap.za_normalization_conflict ?
2623 ED_CASE_CONFLICT : 0;
2624 (void) strncpy(eodp->ed_name, zap.za_name,
2625 EDIRENT_NAMELEN(reclen));
2626 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2631 odp->d_ino = objnum;
2632 odp->d_reclen = reclen;
2633 odp->d_namlen = strlen(zap.za_name);
2634 /* NOTE: d_off is the offset for the *next* entry. */
2636 strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2638 dirent_terminate(odp);
2639 odp = (dirent64_t *)((intptr_t)odp + reclen);
2643 ASSERT(outcount <= bufsize);
2645 /* Prefetch znode */
2647 dmu_prefetch(os, objnum, 0, 0, 0,
2648 ZIO_PRIORITY_SYNC_READ);
2652 * Move to the next entry, fill in the previous offset.
2654 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2655 zap_cursor_advance(&zc);
2656 offset = zap_cursor_serialize(&zc);
2661 /* Fill the offset right after advancing the cursor. */
2664 if (cooks != NULL) {
2667 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2670 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2672 /* Subtract unused cookies */
2673 if (ncookies != NULL)
2674 *ncookies -= ncooks;
2676 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2677 iovp->iov_base += outcount;
2678 iovp->iov_len -= outcount;
2679 uio->uio_resid -= outcount;
2680 } else if ((error = uiomove(outbuf, (long)outcount, UIO_READ, uio))) {
2682 * Reset the pointer.
2684 offset = uio->uio_loffset;
2688 zap_cursor_fini(&zc);
2689 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2690 kmem_free(outbuf, bufsize);
2692 if (error == ENOENT)
2695 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2697 uio->uio_loffset = offset;
2699 if (error != 0 && cookies != NULL) {
2700 free(*cookies, M_TEMP);
2707 ulong_t zfs_fsync_sync_cnt = 4;
2710 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2712 znode_t *zp = VTOZ(vp);
2713 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2715 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2717 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2720 zil_commit(zfsvfs->z_log, zp->z_id);
2723 tsd_set(zfs_fsyncer_key, NULL);
2729 * Get the requested file attributes and place them in the provided
2732 * IN: vp - vnode of file.
2733 * vap - va_mask identifies requested attributes.
2734 * If AT_XVATTR set, then optional attrs are requested
2735 * flags - ATTR_NOACLCHECK (CIFS server context)
2736 * cr - credentials of caller.
2738 * OUT: vap - attribute values.
2740 * RETURN: 0 (always succeeds).
2744 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
2746 znode_t *zp = VTOZ(vp);
2747 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2750 u_longlong_t nblocks;
2751 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2752 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2753 xoptattr_t *xoap = NULL;
2754 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2755 sa_bulk_attr_t bulk[4];
2761 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2763 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2764 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2765 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2766 if (vp->v_type == VBLK || vp->v_type == VCHR)
2767 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2770 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2776 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2777 * Also, if we are the owner don't bother, since owner should
2778 * always be allowed to read basic attributes of file.
2780 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2781 (vap->va_uid != crgetuid(cr))) {
2782 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2790 * Return all attributes. It's cheaper to provide the answer
2791 * than to determine whether we were asked the question.
2794 vap->va_type = IFTOVT(zp->z_mode);
2795 vap->va_mode = zp->z_mode & ~S_IFMT;
2797 vap->va_nodeid = zp->z_id;
2798 vap->va_nlink = zp->z_links;
2799 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp) &&
2800 zp->z_links < ZFS_LINK_MAX)
2802 vap->va_size = zp->z_size;
2803 if (vp->v_type == VBLK || vp->v_type == VCHR)
2804 vap->va_rdev = zfs_cmpldev(rdev);
2805 vap->va_seq = zp->z_seq;
2806 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2807 vap->va_filerev = zp->z_seq;
2810 * Add in any requested optional attributes and the create time.
2811 * Also set the corresponding bits in the returned attribute bitmap.
2813 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2814 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2816 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2817 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2820 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2821 xoap->xoa_readonly =
2822 ((zp->z_pflags & ZFS_READONLY) != 0);
2823 XVA_SET_RTN(xvap, XAT_READONLY);
2826 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2828 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2829 XVA_SET_RTN(xvap, XAT_SYSTEM);
2832 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2834 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2835 XVA_SET_RTN(xvap, XAT_HIDDEN);
2838 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2839 xoap->xoa_nounlink =
2840 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2841 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2844 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2845 xoap->xoa_immutable =
2846 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2847 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2850 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2851 xoap->xoa_appendonly =
2852 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2853 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2856 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2858 ((zp->z_pflags & ZFS_NODUMP) != 0);
2859 XVA_SET_RTN(xvap, XAT_NODUMP);
2862 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2864 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2865 XVA_SET_RTN(xvap, XAT_OPAQUE);
2868 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2869 xoap->xoa_av_quarantined =
2870 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2871 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2874 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2875 xoap->xoa_av_modified =
2876 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2877 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2880 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2881 vp->v_type == VREG) {
2882 zfs_sa_get_scanstamp(zp, xvap);
2885 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2886 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2887 XVA_SET_RTN(xvap, XAT_REPARSE);
2889 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2890 xoap->xoa_generation = zp->z_gen;
2891 XVA_SET_RTN(xvap, XAT_GEN);
2894 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2896 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2897 XVA_SET_RTN(xvap, XAT_OFFLINE);
2900 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2902 ((zp->z_pflags & ZFS_SPARSE) != 0);
2903 XVA_SET_RTN(xvap, XAT_SPARSE);
2906 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2907 xoap->xoa_projinherit =
2908 ((zp->z_pflags & ZFS_PROJINHERIT) != 0);
2909 XVA_SET_RTN(xvap, XAT_PROJINHERIT);
2912 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2913 xoap->xoa_projid = zp->z_projid;
2914 XVA_SET_RTN(xvap, XAT_PROJID);
2918 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2919 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2920 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2921 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2924 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2925 vap->va_blksize = blksize;
2926 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2928 if (zp->z_blksz == 0) {
2930 * Block size hasn't been set; suggest maximal I/O transfers.
2932 vap->va_blksize = zfsvfs->z_max_blksz;
2940 * Set the file attributes to the values contained in the
2943 * IN: zp - znode of file to be modified.
2944 * vap - new attribute values.
2945 * If AT_XVATTR set, then optional attrs are being set
2946 * flags - ATTR_UTIME set if non-default time values provided.
2947 * - ATTR_NOACLCHECK (CIFS context only).
2948 * cr - credentials of caller.
2949 * ct - caller context
2951 * RETURN: 0 on success, error code on failure.
2954 * vp - ctime updated, mtime updated if size changed.
2958 zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr)
2960 vnode_t *vp = ZTOV(zp);
2961 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2962 objset_t *os = zfsvfs->z_os;
2967 uint_t mask = vap->va_mask;
2968 uint_t saved_mask = 0;
2969 uint64_t saved_mode;
2972 uint64_t new_uid, new_gid;
2974 uint64_t mtime[2], ctime[2];
2975 uint64_t projid = ZFS_INVALID_PROJID;
2977 int need_policy = FALSE;
2979 zfs_fuid_info_t *fuidp = NULL;
2980 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2983 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2984 boolean_t fuid_dirtied = B_FALSE;
2985 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2986 int count = 0, xattr_count = 0;
2991 if (mask & AT_NOSET)
2992 return (SET_ERROR(EINVAL));
2997 zilog = zfsvfs->z_log;
3000 * Make sure that if we have ephemeral uid/gid or xvattr specified
3001 * that file system is at proper version level
3004 if (zfsvfs->z_use_fuids == B_FALSE &&
3005 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3006 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3007 (mask & AT_XVATTR))) {
3009 return (SET_ERROR(EINVAL));
3012 if (mask & AT_SIZE && vp->v_type == VDIR) {
3014 return (SET_ERROR(EISDIR));
3017 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3019 return (SET_ERROR(EINVAL));
3023 * If this is an xvattr_t, then get a pointer to the structure of
3024 * optional attributes. If this is NULL, then we have a vattr_t.
3026 xoap = xva_getxoptattr(xvap);
3028 xva_init(&tmpxvattr);
3031 * Immutable files can only alter immutable bit and atime
3033 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3034 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3035 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3037 return (SET_ERROR(EPERM));
3041 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
3045 * Verify timestamps doesn't overflow 32 bits.
3046 * ZFS can handle large timestamps, but 32bit syscalls can't
3047 * handle times greater than 2039. This check should be removed
3048 * once large timestamps are fully supported.
3050 if (mask & (AT_ATIME | AT_MTIME)) {
3051 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3052 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3054 return (SET_ERROR(EOVERFLOW));
3057 if (xoap != NULL && (mask & AT_XVATTR)) {
3058 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
3059 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
3061 return (SET_ERROR(EOVERFLOW));
3064 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
3065 if (!dmu_objset_projectquota_enabled(os) ||
3066 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode))) {
3068 return (SET_ERROR(EOPNOTSUPP));
3071 projid = xoap->xoa_projid;
3072 if (unlikely(projid == ZFS_INVALID_PROJID)) {
3074 return (SET_ERROR(EINVAL));
3077 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
3078 projid = ZFS_INVALID_PROJID;
3083 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
3084 (xoap->xoa_projinherit !=
3085 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) &&
3086 (!dmu_objset_projectquota_enabled(os) ||
3087 (!S_ISREG(zp->z_mode) && !S_ISDIR(zp->z_mode)))) {
3089 return (SET_ERROR(EOPNOTSUPP));
3096 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3098 return (SET_ERROR(EROFS));
3102 * First validate permissions
3105 if (mask & AT_SIZE) {
3107 * XXX - Note, we are not providing any open
3108 * mode flags here (like FNDELAY), so we may
3109 * block if there are locks present... this
3110 * should be addressed in openat().
3112 /* XXX - would it be OK to generate a log record here? */
3113 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3120 if (mask & (AT_ATIME|AT_MTIME) ||
3121 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3122 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3123 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3124 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3125 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3126 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3127 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3128 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3132 if (mask & (AT_UID|AT_GID)) {
3133 int idmask = (mask & (AT_UID|AT_GID));
3138 * NOTE: even if a new mode is being set,
3139 * we may clear S_ISUID/S_ISGID bits.
3142 if (!(mask & AT_MODE))
3143 vap->va_mode = zp->z_mode;
3146 * Take ownership or chgrp to group we are a member of
3149 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3150 take_group = (mask & AT_GID) &&
3151 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3154 * If both AT_UID and AT_GID are set then take_owner and
3155 * take_group must both be set in order to allow taking
3158 * Otherwise, send the check through secpolicy_vnode_setattr()
3162 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3163 ((idmask == AT_UID) && take_owner) ||
3164 ((idmask == AT_GID) && take_group)) {
3165 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3166 skipaclchk, cr) == 0) {
3168 * Remove setuid/setgid for non-privileged users
3170 secpolicy_setid_clear(vap, vp, cr);
3171 trim_mask = (mask & (AT_UID|AT_GID));
3180 oldva.va_mode = zp->z_mode;
3181 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3182 if (mask & AT_XVATTR) {
3184 * Update xvattr mask to include only those attributes
3185 * that are actually changing.
3187 * the bits will be restored prior to actually setting
3188 * the attributes so the caller thinks they were set.
3190 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3191 if (xoap->xoa_appendonly !=
3192 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3195 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3196 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3200 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
3201 if (xoap->xoa_projinherit !=
3202 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
3205 XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
3206 XVA_SET_REQ(&tmpxvattr, XAT_PROJINHERIT);
3210 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3211 if (xoap->xoa_nounlink !=
3212 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3215 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3216 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3220 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3221 if (xoap->xoa_immutable !=
3222 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3225 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3226 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3230 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3231 if (xoap->xoa_nodump !=
3232 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3235 XVA_CLR_REQ(xvap, XAT_NODUMP);
3236 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3240 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3241 if (xoap->xoa_av_modified !=
3242 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3245 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3246 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3250 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3251 if ((vp->v_type != VREG &&
3252 xoap->xoa_av_quarantined) ||
3253 xoap->xoa_av_quarantined !=
3254 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3257 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3258 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3262 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3264 return (SET_ERROR(EPERM));
3267 if (need_policy == FALSE &&
3268 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3269 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3274 if (mask & AT_MODE) {
3275 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3276 err = secpolicy_setid_setsticky_clear(vp, vap,
3282 trim_mask |= AT_MODE;
3290 * If trim_mask is set then take ownership
3291 * has been granted or write_acl is present and user
3292 * has the ability to modify mode. In that case remove
3293 * UID|GID and or MODE from mask so that
3294 * secpolicy_vnode_setattr() doesn't revoke it.
3298 saved_mask = vap->va_mask;
3299 vap->va_mask &= ~trim_mask;
3300 if (trim_mask & AT_MODE) {
3302 * Save the mode, as secpolicy_vnode_setattr()
3303 * will overwrite it with ova.va_mode.
3305 saved_mode = vap->va_mode;
3308 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3309 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3316 vap->va_mask |= saved_mask;
3317 if (trim_mask & AT_MODE) {
3319 * Recover the mode after
3320 * secpolicy_vnode_setattr().
3322 vap->va_mode = saved_mode;
3328 * secpolicy_vnode_setattr, or take ownership may have
3331 mask = vap->va_mask;
3333 if ((mask & (AT_UID | AT_GID)) || projid != ZFS_INVALID_PROJID) {
3334 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3335 &xattr_obj, sizeof (xattr_obj));
3337 if (err == 0 && xattr_obj) {
3338 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3340 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
3342 vrele(ZTOV(attrzp));
3347 if (mask & AT_UID) {
3348 new_uid = zfs_fuid_create(zfsvfs,
3349 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3350 if (new_uid != zp->z_uid &&
3351 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
3355 err = SET_ERROR(EDQUOT);
3360 if (mask & AT_GID) {
3361 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3362 cr, ZFS_GROUP, &fuidp);
3363 if (new_gid != zp->z_gid &&
3364 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
3368 err = SET_ERROR(EDQUOT);
3373 if (projid != ZFS_INVALID_PROJID &&
3374 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
3377 err = SET_ERROR(EDQUOT);
3381 tx = dmu_tx_create(os);
3383 if (mask & AT_MODE) {
3384 uint64_t pmode = zp->z_mode;
3386 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3388 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3389 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3390 err = SET_ERROR(EPERM);
3394 if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)))
3397 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3399 * Are we upgrading ACL from old V0 format
3402 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3403 zfs_znode_acl_version(zp) ==
3404 ZFS_ACL_VERSION_INITIAL) {
3405 dmu_tx_hold_free(tx, acl_obj, 0,
3407 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3408 0, aclp->z_acl_bytes);
3410 dmu_tx_hold_write(tx, acl_obj, 0,
3413 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3414 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3415 0, aclp->z_acl_bytes);
3417 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3419 if (((mask & AT_XVATTR) &&
3420 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
3421 (projid != ZFS_INVALID_PROJID &&
3422 !(zp->z_pflags & ZFS_PROJID)))
3423 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3425 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3429 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3432 fuid_dirtied = zfsvfs->z_fuid_dirty;
3434 zfs_fuid_txhold(zfsvfs, tx);
3436 zfs_sa_upgrade_txholds(tx, zp);
3438 err = dmu_tx_assign(tx, TXG_WAIT);
3444 * Set each attribute requested.
3445 * We group settings according to the locks they need to acquire.
3447 * Note: you cannot set ctime directly, although it will be
3448 * updated as a side-effect of calling this function.
3451 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) {
3453 * For the existed object that is upgraded from old system,
3454 * its on-disk layout has no slot for the project ID attribute.
3455 * But quota accounting logic needs to access related slots by
3456 * offset directly. So we need to adjust old objects' layout
3457 * to make the project ID to some unified and fixed offset.
3460 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid);
3462 err = sa_add_projid(zp->z_sa_hdl, tx, projid);
3464 if (unlikely(err == EEXIST))
3469 projid = ZFS_INVALID_PROJID;
3472 if (mask & (AT_UID|AT_GID|AT_MODE))
3473 mutex_enter(&zp->z_acl_lock);
3475 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3476 &zp->z_pflags, sizeof (zp->z_pflags));
3479 if (mask & (AT_UID|AT_GID|AT_MODE))
3480 mutex_enter(&attrzp->z_acl_lock);
3481 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3482 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3483 sizeof (attrzp->z_pflags));
3484 if (projid != ZFS_INVALID_PROJID) {
3485 attrzp->z_projid = projid;
3486 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3487 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid,
3488 sizeof (attrzp->z_projid));
3492 if (mask & (AT_UID|AT_GID)) {
3494 if (mask & AT_UID) {
3495 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3496 &new_uid, sizeof (new_uid));
3497 zp->z_uid = new_uid;
3499 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3500 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3502 attrzp->z_uid = new_uid;
3506 if (mask & AT_GID) {
3507 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3508 NULL, &new_gid, sizeof (new_gid));
3509 zp->z_gid = new_gid;
3511 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3512 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3514 attrzp->z_gid = new_gid;
3517 if (!(mask & AT_MODE)) {
3518 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3519 NULL, &new_mode, sizeof (new_mode));
3520 new_mode = zp->z_mode;
3522 err = zfs_acl_chown_setattr(zp);
3525 err = zfs_acl_chown_setattr(attrzp);
3530 if (mask & AT_MODE) {
3531 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3532 &new_mode, sizeof (new_mode));
3533 zp->z_mode = new_mode;
3534 ASSERT3U((uintptr_t)aclp, !=, 0);
3535 err = zfs_aclset_common(zp, aclp, cr, tx);
3537 if (zp->z_acl_cached)
3538 zfs_acl_free(zp->z_acl_cached);
3539 zp->z_acl_cached = aclp;
3544 if (mask & AT_ATIME) {
3545 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3546 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3547 &zp->z_atime, sizeof (zp->z_atime));
3550 if (mask & AT_MTIME) {
3551 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3552 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3553 mtime, sizeof (mtime));
3556 if (projid != ZFS_INVALID_PROJID) {
3557 zp->z_projid = projid;
3558 SA_ADD_BULK_ATTR(bulk, count,
3559 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid,
3560 sizeof (zp->z_projid));
3563 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3564 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3565 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3566 NULL, mtime, sizeof (mtime));
3567 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3568 &ctime, sizeof (ctime));
3569 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
3570 } else if (mask != 0) {
3571 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3572 &ctime, sizeof (ctime));
3573 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime);
3575 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3576 SA_ZPL_CTIME(zfsvfs), NULL,
3577 &ctime, sizeof (ctime));
3578 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3584 * Do this after setting timestamps to prevent timestamp
3585 * update from toggling bit
3588 if (xoap && (mask & AT_XVATTR)) {
3590 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
3591 xoap->xoa_createtime = vap->va_birthtime;
3593 * restore trimmed off masks
3594 * so that return masks can be set for caller.
3597 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3598 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3600 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3601 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3603 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3604 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3606 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3607 XVA_SET_REQ(xvap, XAT_NODUMP);
3609 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3610 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3612 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3613 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3615 if (XVA_ISSET_REQ(&tmpxvattr, XAT_PROJINHERIT)) {
3616 XVA_SET_REQ(xvap, XAT_PROJINHERIT);
3619 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3620 ASSERT(vp->v_type == VREG);
3622 zfs_xvattr_set(zp, xvap, tx);
3626 zfs_fuid_sync(zfsvfs, tx);
3629 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3631 if (mask & (AT_UID|AT_GID|AT_MODE))
3632 mutex_exit(&zp->z_acl_lock);
3635 if (mask & (AT_UID|AT_GID|AT_MODE))
3636 mutex_exit(&attrzp->z_acl_lock);
3639 if (err == 0 && attrzp) {
3640 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3652 zfs_fuid_info_free(fuidp);
3659 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3664 if (os->os_sync == ZFS_SYNC_ALWAYS)
3665 zil_commit(zilog, 0);
3672 * We acquire all but fdvp locks using non-blocking acquisitions. If we
3673 * fail to acquire any lock in the path we will drop all held locks,
3674 * acquire the new lock in a blocking fashion, and then release it and
3675 * restart the rename. This acquire/release step ensures that we do not
3676 * spin on a lock waiting for release. On error release all vnode locks
3677 * and decrement references the way tmpfs_rename() would do.
3680 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
3681 struct vnode *tdvp, struct vnode **tvpp,
3682 const struct componentname *scnp, const struct componentname *tcnp)
3685 struct vnode *nvp, *svp, *tvp;
3686 znode_t *sdzp, *tdzp, *szp, *tzp;
3687 const char *snm = scnp->cn_nameptr;
3688 const char *tnm = tcnp->cn_nameptr;
3692 if (*tvpp != NULL && *tvpp != tdvp)
3696 error = vn_lock(sdvp, LK_EXCLUSIVE);
3701 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
3706 error = vn_lock(tdvp, LK_EXCLUSIVE);
3715 * Before using sdzp and tdzp we must ensure that they are live.
3716 * As a porting legacy from illumos we have two things to worry
3717 * about. One is typical for FreeBSD and it is that the vnode is
3718 * not reclaimed (doomed). The other is that the znode is live.
3719 * The current code can invalidate the znode without acquiring the
3720 * corresponding vnode lock if the object represented by the znode
3721 * and vnode is no longer valid after a rollback or receive operation.
3722 * z_teardown_lock hidden behind ZFS_ENTER and ZFS_EXIT is the lock
3723 * that protects the znodes from the invalidation.
3725 zfsvfs = sdzp->z_zfsvfs;
3726 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
3730 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3731 * bypassing the cleanup code in the case of an error.
3733 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3737 error = SET_ERROR(EIO);
3742 * Re-resolve svp to be certain it still exists and fetch the
3745 error = zfs_dirent_lookup(sdzp, snm, &szp, ZEXISTS);
3747 /* Source entry invalid or not there. */
3751 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
3752 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
3753 error = SET_ERROR(EINVAL);
3759 * Re-resolve tvp, if it disappeared we just carry on.
3761 error = zfs_dirent_lookup(tdzp, tnm, &tzp, 0);
3767 if ((tcnp->cn_flags & ISDOTDOT) != 0)
3768 error = SET_ERROR(EINVAL);
3777 * At present the vnode locks must be acquired before z_teardown_lock,
3778 * although it would be more logical to use the opposite order.
3783 * Now try acquire locks on svp and tvp.
3786 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3792 if (error != EBUSY) {
3796 error = vn_lock(nvp, LK_EXCLUSIVE);
3803 * Concurrent rename race.
3808 error = SET_ERROR(EINVAL);
3823 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3828 if (error != EBUSY) {
3832 error = vn_lock(nvp, LK_EXCLUSIVE);
3850 * Note that we must use VRELE_ASYNC in this function as it walks
3851 * up the directory tree and vrele may need to acquire an exclusive
3852 * lock if a last reference to a vnode is dropped.
3855 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3862 zfsvfs = tdzp->z_zfsvfs;
3864 return (SET_ERROR(EINVAL));
3867 if (tdzp->z_id == zfsvfs->z_root)
3871 ASSERT(!zp->z_unlinked);
3872 if ((error = sa_lookup(zp->z_sa_hdl,
3873 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3876 if (parent == szp->z_id) {
3877 error = SET_ERROR(EINVAL);
3880 if (parent == zfsvfs->z_root)
3882 if (parent == sdzp->z_id)
3885 error = zfs_zget(zfsvfs, parent, &zp1);
3890 VN_RELE_ASYNC(ZTOV(zp),
3891 dsl_pool_zrele_taskq(
3892 dmu_objset_pool(zfsvfs->z_os)));
3896 if (error == ENOTDIR)
3897 panic("checkpath: .. not a directory\n");
3899 VN_RELE_ASYNC(ZTOV(zp),
3900 dsl_pool_zrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3904 #if __FreeBSD_version < 1300110
3906 cache_rename(struct vnode *fdvp, struct vnode *fvp, struct vnode *tdvp,
3907 struct vnode *tvp, struct componentname *fcnp, struct componentname *tcnp)
3913 cache_purge_negative(tdvp);
3918 * Move an entry from the provided source directory to the target
3919 * directory. Change the entry name as indicated.
3921 * IN: sdvp - Source directory containing the "old entry".
3922 * snm - Old entry name.
3923 * tdvp - Target directory to contain the "new entry".
3924 * tnm - New entry name.
3925 * cr - credentials of caller.
3926 * ct - caller context
3927 * flags - case flags
3929 * RETURN: 0 on success, error code on failure.
3932 * sdvp,tdvp - ctime|mtime updated
3936 zfs_rename_(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3937 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3938 cred_t *cr, int log)
3941 znode_t *sdzp, *tdzp, *szp, *tzp;
3942 zilog_t *zilog = NULL;
3944 const char *snm = scnp->cn_nameptr;
3945 const char *tnm = tcnp->cn_nameptr;
3947 bool want_seqc_end __maybe_unused = false;
3949 /* Reject renames across filesystems. */
3950 if ((*svpp)->v_mount != tdvp->v_mount ||
3951 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3952 error = SET_ERROR(EXDEV);
3956 if (zfsctl_is_node(tdvp)) {
3957 error = SET_ERROR(EXDEV);
3962 * Lock all four vnodes to ensure safety and semantics of renaming.
3964 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3966 /* no vnodes are locked in the case of error here */
3972 zfsvfs = tdzp->z_zfsvfs;
3973 zilog = zfsvfs->z_log;
3976 * After we re-enter ZFS_ENTER() we will have to revalidate all
3981 if (zfsvfs->z_utf8 && u8_validate(tnm,
3982 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3983 error = SET_ERROR(EILSEQ);
3987 /* If source and target are the same file, there is nothing to do. */
3988 if ((*svpp) == (*tvpp)) {
3993 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3994 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3995 (*tvpp)->v_mountedhere != NULL)) {
3996 error = SET_ERROR(EXDEV);
4001 * We can not use ZFS_VERIFY_ZP() here because it could directly return
4002 * bypassing the cleanup code in the case of an error.
4004 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
4005 error = SET_ERROR(EIO);
4010 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
4011 if (szp->z_sa_hdl == NULL || (tzp != NULL && tzp->z_sa_hdl == NULL)) {
4012 error = SET_ERROR(EIO);
4017 * This is to prevent the creation of links into attribute space
4018 * by renaming a linked file into/outof an attribute directory.
4019 * See the comment in zfs_link() for why this is considered bad.
4021 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
4022 error = SET_ERROR(EINVAL);
4027 * If we are using project inheritance, means if the directory has
4028 * ZFS_PROJINHERIT set, then its descendant directories will inherit
4029 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
4030 * such case, we only allow renames into our tree when the project
4033 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
4034 tdzp->z_projid != szp->z_projid) {
4035 error = SET_ERROR(EXDEV);
4040 * Must have write access at the source to remove the old entry
4041 * and write access at the target to create the new entry.
4042 * Note that if target and source are the same, this can be
4043 * done in a single check.
4045 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
4048 if ((*svpp)->v_type == VDIR) {
4050 * Avoid ".", "..", and aliases of "." for obvious reasons.
4052 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
4054 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
4060 * Check to make sure rename is valid.
4061 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
4063 if ((error = zfs_rename_check(szp, sdzp, tdzp)))
4068 * Does target exist?
4072 * Source and target must be the same type.
4074 if ((*svpp)->v_type == VDIR) {
4075 if ((*tvpp)->v_type != VDIR) {
4076 error = SET_ERROR(ENOTDIR);
4084 if ((*tvpp)->v_type == VDIR) {
4085 error = SET_ERROR(EISDIR);
4091 vn_seqc_write_begin(*svpp);
4092 vn_seqc_write_begin(sdvp);
4094 vn_seqc_write_begin(*tvpp);
4096 vn_seqc_write_begin(tdvp);
4097 #if __FreeBSD_version >= 1300102
4098 want_seqc_end = true;
4100 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
4102 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
4105 * notify the target directory if it is not the same
4106 * as source directory.
4109 vnevent_rename_dest_dir(tdvp, ct);
4112 tx = dmu_tx_create(zfsvfs->z_os);
4113 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4114 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
4115 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
4116 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
4118 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
4119 zfs_sa_upgrade_txholds(tx, tdzp);
4122 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
4123 zfs_sa_upgrade_txholds(tx, tzp);
4126 zfs_sa_upgrade_txholds(tx, szp);
4127 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
4128 error = dmu_tx_assign(tx, TXG_WAIT);
4135 if (tzp) /* Attempt to remove the existing target */
4136 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
4139 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
4141 szp->z_pflags |= ZFS_AV_MODIFIED;
4143 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
4144 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
4147 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
4150 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
4151 snm, tdzp, tnm, szp);
4154 * Update path information for the target vnode
4156 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
4159 * At this point, we have successfully created
4160 * the target name, but have failed to remove
4161 * the source name. Since the create was done
4162 * with the ZRENAMING flag, there are
4163 * complications; for one, the link count is
4164 * wrong. The easiest way to deal with this
4165 * is to remove the newly created target, and
4166 * return the original error. This must
4167 * succeed; fortunately, it is very unlikely to
4168 * fail, since we just created it.
4170 VERIFY3U(zfs_link_destroy(tdzp, tnm, szp, tx,
4171 ZRENAMING, NULL), ==, 0);
4175 cache_rename(sdvp, *svpp, tdvp, *tvpp, scnp, tcnp);
4181 unlockout: /* all 4 vnodes are locked, ZFS_ENTER called */
4183 if (want_seqc_end) {
4184 vn_seqc_write_end(*svpp);
4185 vn_seqc_write_end(sdvp);
4187 vn_seqc_write_end(*tvpp);
4189 vn_seqc_write_end(tdvp);
4190 want_seqc_end = false;
4195 out: /* original two vnodes are locked */
4196 MPASS(!want_seqc_end);
4197 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4198 zil_commit(zilog, 0);
4208 zfs_rename(znode_t *sdzp, const char *sname, znode_t *tdzp, const char *tname,
4209 cred_t *cr, int flags)
4211 struct componentname scn, tcn;
4212 vnode_t *sdvp, *tdvp;
4219 error = zfs_lookup_internal(sdzp, sname, &svp, &scn, DELETE);
4220 if (sdzp->z_zfsvfs->z_replay == B_FALSE)
4226 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
4227 error = zfs_lookup_internal(tdzp, tname, &tvp, &tcn, RENAME);
4228 if (error == EJUSTRETURN)
4230 else if (error != 0) {
4235 error = zfs_rename_(sdvp, &svp, &scn, tdvp, &tvp, &tcn, cr, 0);
4246 * Insert the indicated symbolic reference entry into the directory.
4248 * IN: dvp - Directory to contain new symbolic link.
4249 * link - Name for new symlink entry.
4250 * vap - Attributes of new entry.
4251 * cr - credentials of caller.
4252 * ct - caller context
4253 * flags - case flags
4255 * RETURN: 0 on success, error code on failure.
4258 * dvp - ctime|mtime updated
4262 zfs_symlink(znode_t *dzp, const char *name, vattr_t *vap,
4263 const char *link, znode_t **zpp, cred_t *cr, int flags)
4267 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4269 uint64_t len = strlen(link);
4271 zfs_acl_ids_t acl_ids;
4272 boolean_t fuid_dirtied;
4273 uint64_t txtype = TX_SYMLINK;
4275 ASSERT(vap->va_type == VLNK);
4279 zilog = zfsvfs->z_log;
4281 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4282 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4284 return (SET_ERROR(EILSEQ));
4287 if (len > MAXPATHLEN) {
4289 return (SET_ERROR(ENAMETOOLONG));
4292 if ((error = zfs_acl_ids_create(dzp, 0,
4293 vap, cr, NULL, &acl_ids)) != 0) {
4299 * Attempt to lock directory; fail if entry already exists.
4301 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
4303 zfs_acl_ids_free(&acl_ids);
4308 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
4309 zfs_acl_ids_free(&acl_ids);
4314 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids,
4316 zfs_acl_ids_free(&acl_ids);
4318 return (SET_ERROR(EDQUOT));
4321 getnewvnode_reserve_();
4322 tx = dmu_tx_create(zfsvfs->z_os);
4323 fuid_dirtied = zfsvfs->z_fuid_dirty;
4324 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4325 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4326 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4327 ZFS_SA_BASE_ATTR_SIZE + len);
4328 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4329 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4330 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4331 acl_ids.z_aclp->z_acl_bytes);
4334 zfs_fuid_txhold(zfsvfs, tx);
4335 error = dmu_tx_assign(tx, TXG_WAIT);
4337 zfs_acl_ids_free(&acl_ids);
4339 getnewvnode_drop_reserve();
4345 * Create a new object for the symlink.
4346 * for version 4 ZPL datsets the symlink will be an SA attribute
4348 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4351 zfs_fuid_sync(zfsvfs, tx);
4354 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4355 __DECONST(void *, link), len, tx);
4357 zfs_sa_symlink(zp, __DECONST(char *, link), len, tx);
4360 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4361 &zp->z_size, sizeof (zp->z_size), tx);
4363 * Insert the new object into the directory.
4365 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
4367 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4370 zfs_acl_ids_free(&acl_ids);
4374 getnewvnode_drop_reserve();
4376 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4377 zil_commit(zilog, 0);
4384 * Return, in the buffer contained in the provided uio structure,
4385 * the symbolic path referred to by vp.
4387 * IN: vp - vnode of symbolic link.
4388 * uio - structure to contain the link path.
4389 * cr - credentials of caller.
4390 * ct - caller context
4392 * OUT: uio - structure containing the link path.
4394 * RETURN: 0 on success, error code on failure.
4397 * vp - atime updated
4401 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4403 znode_t *zp = VTOZ(vp);
4404 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4411 error = sa_lookup_uio(zp->z_sa_hdl,
4412 SA_ZPL_SYMLINK(zfsvfs), uio);
4414 error = zfs_sa_readlink(zp, uio);
4416 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4423 * Insert a new entry into directory tdvp referencing svp.
4425 * IN: tdvp - Directory to contain new entry.
4426 * svp - vnode of new entry.
4427 * name - name of new entry.
4428 * cr - credentials of caller.
4430 * RETURN: 0 on success, error code on failure.
4433 * tdvp - ctime|mtime updated
4434 * svp - ctime updated
4438 zfs_link(znode_t *tdzp, znode_t *szp, const char *name, cred_t *cr,
4442 zfsvfs_t *zfsvfs = tdzp->z_zfsvfs;
4449 ASSERT(ZTOV(tdzp)->v_type == VDIR);
4452 ZFS_VERIFY_ZP(tdzp);
4453 zilog = zfsvfs->z_log;
4456 * POSIX dictates that we return EPERM here.
4457 * Better choices include ENOTSUP or EISDIR.
4459 if (ZTOV(szp)->v_type == VDIR) {
4461 return (SET_ERROR(EPERM));
4467 * If we are using project inheritance, means if the directory has
4468 * ZFS_PROJINHERIT set, then its descendant directories will inherit
4469 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
4470 * such case, we only allow hard link creation in our tree when the
4471 * project IDs are the same.
4473 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
4474 tdzp->z_projid != szp->z_projid) {
4476 return (SET_ERROR(EXDEV));
4479 if (szp->z_pflags & (ZFS_APPENDONLY |
4480 ZFS_IMMUTABLE | ZFS_READONLY)) {
4482 return (SET_ERROR(EPERM));
4485 /* Prevent links to .zfs/shares files */
4487 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4488 &parent, sizeof (uint64_t))) != 0) {
4492 if (parent == zfsvfs->z_shares_dir) {
4494 return (SET_ERROR(EPERM));
4497 if (zfsvfs->z_utf8 && u8_validate(name,
4498 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4500 return (SET_ERROR(EILSEQ));
4504 * We do not support links between attributes and non-attributes
4505 * because of the potential security risk of creating links
4506 * into "normal" file space in order to circumvent restrictions
4507 * imposed in attribute space.
4509 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) {
4511 return (SET_ERROR(EINVAL));
4515 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4516 if (owner != crgetuid(cr) && secpolicy_basic_link(ZTOV(szp), cr) != 0) {
4518 return (SET_ERROR(EPERM));
4521 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
4527 * Attempt to lock directory; fail if entry already exists.
4529 error = zfs_dirent_lookup(tdzp, name, &tzp, ZNEW);
4535 tx = dmu_tx_create(zfsvfs->z_os);
4536 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4537 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name);
4538 zfs_sa_upgrade_txholds(tx, szp);
4539 zfs_sa_upgrade_txholds(tx, tdzp);
4540 error = dmu_tx_assign(tx, TXG_WAIT);
4547 error = zfs_link_create(tdzp, name, szp, tx, 0);
4550 uint64_t txtype = TX_LINK;
4551 zfs_log_link(zilog, tx, txtype, tdzp, szp, name);
4557 vnevent_link(ZTOV(szp), ct);
4560 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4561 zil_commit(zilog, 0);
4568 * Free or allocate space in a file. Currently, this function only
4569 * supports the `F_FREESP' command. However, this command is somewhat
4570 * misnamed, as its functionality includes the ability to allocate as
4571 * well as free space.
4573 * IN: ip - inode of file to free data in.
4574 * cmd - action to take (only F_FREESP supported).
4575 * bfp - section of file to free/alloc.
4576 * flag - current file open mode flags.
4577 * offset - current file offset.
4578 * cr - credentials of caller.
4580 * RETURN: 0 on success, error code on failure.
4583 * ip - ctime|mtime updated
4587 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag,
4588 offset_t offset, cred_t *cr)
4590 zfsvfs_t *zfsvfs = ZTOZSB(zp);
4597 if (cmd != F_FREESP) {
4599 return (SET_ERROR(EINVAL));
4603 * Callers might not be able to detect properly that we are read-only,
4604 * so check it explicitly here.
4606 if (zfs_is_readonly(zfsvfs)) {
4608 return (SET_ERROR(EROFS));
4611 if (bfp->l_len < 0) {
4613 return (SET_ERROR(EINVAL));
4617 * Permissions aren't checked on Solaris because on this OS
4618 * zfs_space() can only be called with an opened file handle.
4619 * On Linux we can get here through truncate_range() which
4620 * operates directly on inodes, so we need to check access rights.
4622 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4628 len = bfp->l_len; /* 0 means from off to end of file */
4630 error = zfs_freesp(zp, off, len, flag, TRUE);
4638 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4640 znode_t *zp = VTOZ(vp);
4641 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4644 ZFS_RLOCK_TEARDOWN_INACTIVE(zfsvfs);
4645 if (zp->z_sa_hdl == NULL) {
4647 * The fs has been unmounted, or we did a
4648 * suspend/resume and this file no longer exists.
4650 ZFS_RUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
4655 if (zp->z_unlinked) {
4657 * Fast path to recycle a vnode of a removed file.
4659 ZFS_RUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
4664 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4665 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4667 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4668 zfs_sa_upgrade_txholds(tx, zp);
4669 error = dmu_tx_assign(tx, TXG_WAIT);
4673 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4674 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4675 zp->z_atime_dirty = 0;
4679 ZFS_RUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
4683 CTASSERT(sizeof (struct zfid_short) <= sizeof (struct fid));
4684 CTASSERT(sizeof (struct zfid_long) <= sizeof (struct fid));
4688 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4690 znode_t *zp = VTOZ(vp);
4691 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4694 uint64_t object = zp->z_id;
4701 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4702 &gen64, sizeof (uint64_t))) != 0) {
4707 gen = (uint32_t)gen64;
4709 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4710 fidp->fid_len = size;
4712 zfid = (zfid_short_t *)fidp;
4714 zfid->zf_len = size;
4716 for (i = 0; i < sizeof (zfid->zf_object); i++)
4717 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4719 /* Must have a non-zero generation number to distinguish from .zfs */
4722 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4723 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4725 if (size == LONG_FID_LEN) {
4726 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4729 zlfid = (zfid_long_t *)fidp;
4731 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4732 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4734 /* XXX - this should be the generation number for the objset */
4735 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4736 zlfid->zf_setgen[i] = 0;
4744 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4745 caller_context_t *ct)
4752 *valp = MIN(LONG_MAX, ZFS_LINK_MAX);
4755 case _PC_FILESIZEBITS:
4758 case _PC_MIN_HOLE_SIZE:
4759 *valp = (int)SPA_MINBLOCKSIZE;
4761 case _PC_ACL_EXTENDED:
4762 #if 0 /* POSIX ACLs are not implemented for ZFS on FreeBSD yet. */
4764 zfsvfs = zp->z_zfsvfs;
4767 *valp = zfsvfs->z_acl_type == ZFSACLTYPE_POSIX ? 1 : 0;
4776 zfsvfs = zp->z_zfsvfs;
4779 *valp = zfsvfs->z_acl_type == ZFS_ACLTYPE_NFSV4 ? 1 : 0;
4783 case _PC_ACL_PATH_MAX:
4784 *valp = ACL_MAX_ENTRIES;
4788 return (EOPNOTSUPP);
4794 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4795 caller_context_t *ct)
4797 znode_t *zp = VTOZ(vp);
4798 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4800 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4804 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4812 zfs_setsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr)
4814 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4816 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4817 zilog_t *zilog = zfsvfs->z_log;
4822 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4824 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4825 zil_commit(zilog, 0);
4832 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind,
4835 znode_t *zp = VTOZ(vp);
4836 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4837 objset_t *os = zp->z_zfsvfs->z_os;
4838 zfs_locked_range_t *lr;
4840 off_t start, end, obj_size;
4842 int pgsin_b, pgsin_a;
4848 start = IDX_TO_OFF(ma[0]->pindex);
4849 end = IDX_TO_OFF(ma[count - 1]->pindex + 1);
4852 * Lock a range covering all required and optional pages.
4853 * Note that we need to handle the case of the block size growing.
4856 blksz = zp->z_blksz;
4857 lr = zfs_rangelock_tryenter(&zp->z_rangelock,
4858 rounddown(start, blksz),
4859 roundup(end, blksz) - rounddown(start, blksz), RL_READER);
4861 if (rahead != NULL) {
4865 if (rbehind != NULL) {
4871 if (blksz == zp->z_blksz)
4873 zfs_rangelock_exit(lr);
4876 object = ma[0]->object;
4877 zfs_vmobject_wlock(object);
4878 obj_size = object->un_pager.vnp.vnp_size;
4879 zfs_vmobject_wunlock(object);
4880 if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) {
4882 zfs_rangelock_exit(lr);
4884 return (zfs_vm_pagerret_bad);
4888 if (rbehind != NULL) {
4889 pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz));
4890 pgsin_b = MIN(*rbehind, pgsin_b);
4894 if (rahead != NULL) {
4895 pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end);
4896 if (end + IDX_TO_OFF(pgsin_a) >= obj_size)
4897 pgsin_a = OFF_TO_IDX(round_page(obj_size) - end);
4898 pgsin_a = MIN(*rahead, pgsin_a);
4902 * NB: we need to pass the exact byte size of the data that we expect
4903 * to read after accounting for the file size. This is required because
4904 * ZFS will panic if we request DMU to read beyond the end of the last
4907 error = dmu_read_pages(os, zp->z_id, ma, count, &pgsin_b, &pgsin_a,
4908 MIN(end, obj_size) - (end - PAGE_SIZE));
4911 zfs_rangelock_exit(lr);
4912 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4916 return (zfs_vm_pagerret_error);
4918 VM_CNT_INC(v_vnodein);
4919 VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a);
4920 if (rbehind != NULL)
4924 return (zfs_vm_pagerret_ok);
4927 #ifndef _SYS_SYSPROTO_H_
4928 struct vop_getpages_args {
4938 zfs_freebsd_getpages(struct vop_getpages_args *ap)
4941 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4946 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4949 znode_t *zp = VTOZ(vp);
4950 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4951 zfs_locked_range_t *lr;
4959 vm_ooffset_t lo_off;
4970 object = vp->v_object;
4974 KASSERT(ma[0]->object == object, ("mismatching object"));
4975 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4977 for (i = 0; i < pcount; i++)
4978 rtvals[i] = zfs_vm_pagerret_error;
4980 off = IDX_TO_OFF(ma[0]->pindex);
4981 blksz = zp->z_blksz;
4982 lo_off = rounddown(off, blksz);
4983 lo_len = roundup(len + (off - lo_off), blksz);
4984 lr = zfs_rangelock_enter(&zp->z_rangelock, lo_off, lo_len, RL_WRITER);
4986 zfs_vmobject_wlock(object);
4987 if (len + off > object->un_pager.vnp.vnp_size) {
4988 if (object->un_pager.vnp.vnp_size > off) {
4991 len = object->un_pager.vnp.vnp_size - off;
4993 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4995 * If the object is locked and the following
4996 * conditions hold, then the page's dirty
4997 * field cannot be concurrently changed by a
5001 vm_page_assert_sbusied(m);
5002 KASSERT(!pmap_page_is_write_mapped(m),
5003 ("zfs_putpages: page %p is not read-only",
5005 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
5012 if (ncount < pcount) {
5013 for (i = ncount; i < pcount; i++) {
5014 rtvals[i] = zfs_vm_pagerret_bad;
5018 zfs_vmobject_wunlock(object);
5023 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, zp->z_uid) ||
5024 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, zp->z_gid) ||
5025 (zp->z_projid != ZFS_DEFAULT_PROJID &&
5026 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
5031 tx = dmu_tx_create(zfsvfs->z_os);
5032 dmu_tx_hold_write(tx, zp->z_id, off, len);
5034 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
5035 zfs_sa_upgrade_txholds(tx, zp);
5036 err = dmu_tx_assign(tx, TXG_WAIT);
5042 if (zp->z_blksz < PAGE_SIZE) {
5043 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
5044 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
5045 va = zfs_map_page(ma[i], &sf);
5046 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
5050 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
5054 uint64_t mtime[2], ctime[2];
5055 sa_bulk_attr_t bulk[3];
5058 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
5060 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
5062 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
5064 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
5065 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
5068 * XXX we should be passing a callback to undirty
5069 * but that would make the locking messier
5071 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off,
5072 len, 0, NULL, NULL);
5074 zfs_vmobject_wlock(object);
5075 for (i = 0; i < ncount; i++) {
5076 rtvals[i] = zfs_vm_pagerret_ok;
5077 vm_page_undirty(ma[i]);
5079 zfs_vmobject_wunlock(object);
5080 VM_CNT_INC(v_vnodeout);
5081 VM_CNT_ADD(v_vnodepgsout, ncount);
5086 zfs_rangelock_exit(lr);
5087 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
5088 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5089 zil_commit(zfsvfs->z_log, zp->z_id);
5094 #ifndef _SYS_SYSPROTO_H_
5095 struct vop_putpages_args {
5105 zfs_freebsd_putpages(struct vop_putpages_args *ap)
5108 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
5112 #ifndef _SYS_SYSPROTO_H_
5113 struct vop_bmap_args {
5116 struct bufobj **a_bop;
5124 zfs_freebsd_bmap(struct vop_bmap_args *ap)
5127 if (ap->a_bop != NULL)
5128 *ap->a_bop = &ap->a_vp->v_bufobj;
5129 if (ap->a_bnp != NULL)
5130 *ap->a_bnp = ap->a_bn;
5131 if (ap->a_runp != NULL)
5133 if (ap->a_runb != NULL)
5139 #ifndef _SYS_SYSPROTO_H_
5140 struct vop_open_args {
5143 struct ucred *a_cred;
5144 struct thread *a_td;
5149 zfs_freebsd_open(struct vop_open_args *ap)
5151 vnode_t *vp = ap->a_vp;
5152 znode_t *zp = VTOZ(vp);
5155 error = zfs_open(&vp, ap->a_mode, ap->a_cred);
5157 vnode_create_vobject(vp, zp->z_size, ap->a_td);
5161 #ifndef _SYS_SYSPROTO_H_
5162 struct vop_close_args {
5165 struct ucred *a_cred;
5166 struct thread *a_td;
5171 zfs_freebsd_close(struct vop_close_args *ap)
5174 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred));
5177 #ifndef _SYS_SYSPROTO_H_
5178 struct vop_ioctl_args {
5189 zfs_freebsd_ioctl(struct vop_ioctl_args *ap)
5192 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
5193 ap->a_fflag, ap->a_cred, NULL));
5197 ioflags(int ioflags)
5201 if (ioflags & IO_APPEND)
5203 if (ioflags & IO_NDELAY)
5205 if (ioflags & IO_SYNC)
5206 flags |= (FSYNC | FDSYNC | FRSYNC);
5211 #ifndef _SYS_SYSPROTO_H_
5212 struct vop_read_args {
5216 struct ucred *a_cred;
5221 zfs_freebsd_read(struct vop_read_args *ap)
5224 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
5228 #ifndef _SYS_SYSPROTO_H_
5229 struct vop_write_args {
5233 struct ucred *a_cred;
5238 zfs_freebsd_write(struct vop_write_args *ap)
5241 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
5245 #if __FreeBSD_version >= 1300102
5247 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
5248 * the comment above cache_fplookup for details.
5251 zfs_freebsd_fplookup_vexec(struct vop_fplookup_vexec_args *v)
5259 if (__predict_false(zp == NULL))
5261 pflags = atomic_load_64(&zp->z_pflags);
5262 if (pflags & ZFS_AV_QUARANTINED)
5264 if (pflags & ZFS_XATTR)
5266 if ((pflags & ZFS_NO_EXECS_DENIED) == 0)
5272 #ifndef _SYS_SYSPROTO_H_
5273 struct vop_access_args {
5275 accmode_t a_accmode;
5276 struct ucred *a_cred;
5277 struct thread *a_td;
5282 zfs_freebsd_access(struct vop_access_args *ap)
5284 vnode_t *vp = ap->a_vp;
5285 znode_t *zp = VTOZ(vp);
5290 if (ap->a_accmode == VEXEC) {
5291 if (zfs_fastaccesschk_execute(zp, ap->a_cred) == 0)
5296 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
5298 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
5300 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
5303 * VADMIN has to be handled by vaccess().
5306 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
5308 #if __FreeBSD_version >= 1300105
5309 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
5310 zp->z_gid, accmode, ap->a_cred);
5312 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
5313 zp->z_gid, accmode, ap->a_cred, NULL);
5319 * For VEXEC, ensure that at least one execute bit is set for
5322 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
5323 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
5330 #ifndef _SYS_SYSPROTO_H_
5331 struct vop_lookup_args {
5332 struct vnode *a_dvp;
5333 struct vnode **a_vpp;
5334 struct componentname *a_cnp;
5339 zfs_freebsd_lookup(struct vop_lookup_args *ap, boolean_t cached)
5341 struct componentname *cnp = ap->a_cnp;
5342 char nm[NAME_MAX + 1];
5344 ASSERT(cnp->cn_namelen < sizeof (nm));
5345 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof (nm)));
5347 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
5348 cnp->cn_cred, cnp->cn_thread, 0, cached));
5352 zfs_freebsd_cachedlookup(struct vop_cachedlookup_args *ap)
5355 return (zfs_freebsd_lookup((struct vop_lookup_args *)ap, B_TRUE));
5358 #ifndef _SYS_SYSPROTO_H_
5359 struct vop_lookup_args {
5360 struct vnode *a_dvp;
5361 struct vnode **a_vpp;
5362 struct componentname *a_cnp;
5367 zfs_cache_lookup(struct vop_lookup_args *ap)
5371 zfsvfs = ap->a_dvp->v_mount->mnt_data;
5372 if (zfsvfs->z_use_namecache)
5373 return (vfs_cache_lookup(ap));
5375 return (zfs_freebsd_lookup(ap, B_FALSE));
5378 #ifndef _SYS_SYSPROTO_H_
5379 struct vop_create_args {
5380 struct vnode *a_dvp;
5381 struct vnode **a_vpp;
5382 struct componentname *a_cnp;
5383 struct vattr *a_vap;
5388 zfs_freebsd_create(struct vop_create_args *ap)
5391 struct componentname *cnp = ap->a_cnp;
5392 vattr_t *vap = ap->a_vap;
5396 ASSERT(cnp->cn_flags & SAVENAME);
5398 vattr_init_mask(vap);
5399 mode = vap->va_mode & ALLPERMS;
5400 zfsvfs = ap->a_dvp->v_mount->mnt_data;
5403 rc = zfs_create(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap, !EXCL, mode,
5404 &zp, cnp->cn_cred, 0 /* flag */, NULL /* vsecattr */);
5406 *ap->a_vpp = ZTOV(zp);
5407 if (zfsvfs->z_use_namecache &&
5408 rc == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
5409 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
5414 #ifndef _SYS_SYSPROTO_H_
5415 struct vop_remove_args {
5416 struct vnode *a_dvp;
5418 struct componentname *a_cnp;
5423 zfs_freebsd_remove(struct vop_remove_args *ap)
5426 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5428 return (zfs_remove_(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
5429 ap->a_cnp->cn_cred));
5432 #ifndef _SYS_SYSPROTO_H_
5433 struct vop_mkdir_args {
5434 struct vnode *a_dvp;
5435 struct vnode **a_vpp;
5436 struct componentname *a_cnp;
5437 struct vattr *a_vap;
5442 zfs_freebsd_mkdir(struct vop_mkdir_args *ap)
5444 vattr_t *vap = ap->a_vap;
5448 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5450 vattr_init_mask(vap);
5453 rc = zfs_mkdir(VTOZ(ap->a_dvp), ap->a_cnp->cn_nameptr, vap, &zp,
5454 ap->a_cnp->cn_cred, 0, NULL);
5457 *ap->a_vpp = ZTOV(zp);
5461 #ifndef _SYS_SYSPROTO_H_
5462 struct vop_rmdir_args {
5463 struct vnode *a_dvp;
5465 struct componentname *a_cnp;
5470 zfs_freebsd_rmdir(struct vop_rmdir_args *ap)
5472 struct componentname *cnp = ap->a_cnp;
5474 ASSERT(cnp->cn_flags & SAVENAME);
5476 return (zfs_rmdir_(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
5479 #ifndef _SYS_SYSPROTO_H_
5480 struct vop_readdir_args {
5483 struct ucred *a_cred;
5486 ulong_t **a_cookies;
5491 zfs_freebsd_readdir(struct vop_readdir_args *ap)
5494 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
5495 ap->a_ncookies, ap->a_cookies));
5498 #ifndef _SYS_SYSPROTO_H_
5499 struct vop_fsync_args {
5502 struct thread *a_td;
5507 zfs_freebsd_fsync(struct vop_fsync_args *ap)
5511 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
5514 #ifndef _SYS_SYSPROTO_H_
5515 struct vop_getattr_args {
5517 struct vattr *a_vap;
5518 struct ucred *a_cred;
5523 zfs_freebsd_getattr(struct vop_getattr_args *ap)
5525 vattr_t *vap = ap->a_vap;
5531 xvap.xva_vattr = *vap;
5532 xvap.xva_vattr.va_mask |= AT_XVATTR;
5534 /* Convert chflags into ZFS-type flags. */
5535 /* XXX: what about SF_SETTABLE?. */
5536 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
5537 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
5538 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
5539 XVA_SET_REQ(&xvap, XAT_NODUMP);
5540 XVA_SET_REQ(&xvap, XAT_READONLY);
5541 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
5542 XVA_SET_REQ(&xvap, XAT_SYSTEM);
5543 XVA_SET_REQ(&xvap, XAT_HIDDEN);
5544 XVA_SET_REQ(&xvap, XAT_REPARSE);
5545 XVA_SET_REQ(&xvap, XAT_OFFLINE);
5546 XVA_SET_REQ(&xvap, XAT_SPARSE);
5548 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred);
5552 /* Convert ZFS xattr into chflags. */
5553 #define FLAG_CHECK(fflag, xflag, xfield) do { \
5554 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
5555 fflags |= (fflag); \
5557 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
5558 xvap.xva_xoptattrs.xoa_immutable);
5559 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
5560 xvap.xva_xoptattrs.xoa_appendonly);
5561 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
5562 xvap.xva_xoptattrs.xoa_nounlink);
5563 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
5564 xvap.xva_xoptattrs.xoa_archive);
5565 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
5566 xvap.xva_xoptattrs.xoa_nodump);
5567 FLAG_CHECK(UF_READONLY, XAT_READONLY,
5568 xvap.xva_xoptattrs.xoa_readonly);
5569 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
5570 xvap.xva_xoptattrs.xoa_system);
5571 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
5572 xvap.xva_xoptattrs.xoa_hidden);
5573 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
5574 xvap.xva_xoptattrs.xoa_reparse);
5575 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
5576 xvap.xva_xoptattrs.xoa_offline);
5577 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
5578 xvap.xva_xoptattrs.xoa_sparse);
5581 *vap = xvap.xva_vattr;
5582 vap->va_flags = fflags;
5586 #ifndef _SYS_SYSPROTO_H_
5587 struct vop_setattr_args {
5589 struct vattr *a_vap;
5590 struct ucred *a_cred;
5595 zfs_freebsd_setattr(struct vop_setattr_args *ap)
5597 vnode_t *vp = ap->a_vp;
5598 vattr_t *vap = ap->a_vap;
5599 cred_t *cred = ap->a_cred;
5604 vattr_init_mask(vap);
5605 vap->va_mask &= ~AT_NOSET;
5608 xvap.xva_vattr = *vap;
5610 zflags = VTOZ(vp)->z_pflags;
5612 if (vap->va_flags != VNOVAL) {
5613 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
5616 if (zfsvfs->z_use_fuids == B_FALSE)
5617 return (EOPNOTSUPP);
5619 fflags = vap->va_flags;
5622 * We need to figure out whether it makes sense to allow
5623 * UF_REPARSE through, since we don't really have other
5624 * facilities to handle reparse points and zfs_setattr()
5625 * doesn't currently allow setting that attribute anyway.
5627 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
5628 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
5629 UF_OFFLINE|UF_SPARSE)) != 0)
5630 return (EOPNOTSUPP);
5632 * Unprivileged processes are not permitted to unset system
5633 * flags, or modify flags if any system flags are set.
5634 * Privileged non-jail processes may not modify system flags
5635 * if securelevel > 0 and any existing system flags are set.
5636 * Privileged jail processes behave like privileged non-jail
5637 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
5638 * otherwise, they behave like unprivileged processes.
5640 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
5641 spl_priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) {
5643 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5644 error = securelevel_gt(cred, 0);
5650 * Callers may only modify the file flags on
5651 * objects they have VADMIN rights for.
5653 if ((error = VOP_ACCESS(vp, VADMIN, cred,
5657 (ZFS_IMMUTABLE | ZFS_APPENDONLY |
5662 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
5667 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
5668 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
5669 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
5670 XVA_SET_REQ(&xvap, (xflag)); \
5671 (xfield) = ((fflags & (fflag)) != 0); \
5674 /* Convert chflags into ZFS-type flags. */
5675 /* XXX: what about SF_SETTABLE?. */
5676 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
5677 xvap.xva_xoptattrs.xoa_immutable);
5678 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
5679 xvap.xva_xoptattrs.xoa_appendonly);
5680 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
5681 xvap.xva_xoptattrs.xoa_nounlink);
5682 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
5683 xvap.xva_xoptattrs.xoa_archive);
5684 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
5685 xvap.xva_xoptattrs.xoa_nodump);
5686 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
5687 xvap.xva_xoptattrs.xoa_readonly);
5688 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
5689 xvap.xva_xoptattrs.xoa_system);
5690 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
5691 xvap.xva_xoptattrs.xoa_hidden);
5692 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
5693 xvap.xva_xoptattrs.xoa_reparse);
5694 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
5695 xvap.xva_xoptattrs.xoa_offline);
5696 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
5697 xvap.xva_xoptattrs.xoa_sparse);
5700 if (vap->va_birthtime.tv_sec != VNOVAL) {
5701 xvap.xva_vattr.va_mask |= AT_XVATTR;
5702 XVA_SET_REQ(&xvap, XAT_CREATETIME);
5704 return (zfs_setattr(VTOZ(vp), (vattr_t *)&xvap, 0, cred));
5707 #ifndef _SYS_SYSPROTO_H_
5708 struct vop_rename_args {
5709 struct vnode *a_fdvp;
5710 struct vnode *a_fvp;
5711 struct componentname *a_fcnp;
5712 struct vnode *a_tdvp;
5713 struct vnode *a_tvp;
5714 struct componentname *a_tcnp;
5719 zfs_freebsd_rename(struct vop_rename_args *ap)
5721 vnode_t *fdvp = ap->a_fdvp;
5722 vnode_t *fvp = ap->a_fvp;
5723 vnode_t *tdvp = ap->a_tdvp;
5724 vnode_t *tvp = ap->a_tvp;
5727 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
5728 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
5730 error = zfs_rename_(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
5731 ap->a_tcnp, ap->a_fcnp->cn_cred, 1);
5742 #ifndef _SYS_SYSPROTO_H_
5743 struct vop_symlink_args {
5744 struct vnode *a_dvp;
5745 struct vnode **a_vpp;
5746 struct componentname *a_cnp;
5747 struct vattr *a_vap;
5753 zfs_freebsd_symlink(struct vop_symlink_args *ap)
5755 struct componentname *cnp = ap->a_cnp;
5756 vattr_t *vap = ap->a_vap;
5760 ASSERT(cnp->cn_flags & SAVENAME);
5762 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
5763 vattr_init_mask(vap);
5766 rc = zfs_symlink(VTOZ(ap->a_dvp), cnp->cn_nameptr, vap,
5767 ap->a_target, &zp, cnp->cn_cred, 0 /* flags */);
5769 *ap->a_vpp = ZTOV(zp);
5773 #ifndef _SYS_SYSPROTO_H_
5774 struct vop_readlink_args {
5777 struct ucred *a_cred;
5782 zfs_freebsd_readlink(struct vop_readlink_args *ap)
5785 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
5788 #ifndef _SYS_SYSPROTO_H_
5789 struct vop_link_args {
5790 struct vnode *a_tdvp;
5792 struct componentname *a_cnp;
5797 zfs_freebsd_link(struct vop_link_args *ap)
5799 struct componentname *cnp = ap->a_cnp;
5800 vnode_t *vp = ap->a_vp;
5801 vnode_t *tdvp = ap->a_tdvp;
5803 if (tdvp->v_mount != vp->v_mount)
5806 ASSERT(cnp->cn_flags & SAVENAME);
5808 return (zfs_link(VTOZ(tdvp), VTOZ(vp),
5809 cnp->cn_nameptr, cnp->cn_cred, 0));
5812 #ifndef _SYS_SYSPROTO_H_
5813 struct vop_inactive_args {
5815 struct thread *a_td;
5820 zfs_freebsd_inactive(struct vop_inactive_args *ap)
5822 vnode_t *vp = ap->a_vp;
5824 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5828 #if __FreeBSD_version >= 1300042
5829 #ifndef _SYS_SYSPROTO_H_
5830 struct vop_need_inactive_args {
5832 struct thread *a_td;
5837 zfs_freebsd_need_inactive(struct vop_need_inactive_args *ap)
5839 vnode_t *vp = ap->a_vp;
5840 znode_t *zp = VTOZ(vp);
5841 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5844 if (vn_need_pageq_flush(vp))
5847 if (!ZFS_TRYRLOCK_TEARDOWN_INACTIVE(zfsvfs))
5849 need = (zp->z_sa_hdl == NULL || zp->z_unlinked || zp->z_atime_dirty);
5850 ZFS_RUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
5856 #ifndef _SYS_SYSPROTO_H_
5857 struct vop_reclaim_args {
5859 struct thread *a_td;
5864 zfs_freebsd_reclaim(struct vop_reclaim_args *ap)
5866 vnode_t *vp = ap->a_vp;
5867 znode_t *zp = VTOZ(vp);
5868 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5872 #if __FreeBSD_version < 1300042
5873 /* Destroy the vm object and flush associated pages. */
5874 vnode_destroy_vobject(vp);
5877 * z_teardown_inactive_lock protects from a race with
5878 * zfs_znode_dmu_fini in zfsvfs_teardown during
5881 ZFS_RLOCK_TEARDOWN_INACTIVE(zfsvfs);
5882 if (zp->z_sa_hdl == NULL)
5886 ZFS_RUNLOCK_TEARDOWN_INACTIVE(zfsvfs);
5892 #ifndef _SYS_SYSPROTO_H_
5893 struct vop_fid_args {
5900 zfs_freebsd_fid(struct vop_fid_args *ap)
5903 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5907 #ifndef _SYS_SYSPROTO_H_
5908 struct vop_pathconf_args {
5911 register_t *a_retval;
5916 zfs_freebsd_pathconf(struct vop_pathconf_args *ap)
5921 error = zfs_pathconf(ap->a_vp, ap->a_name, &val,
5922 curthread->td_ucred, NULL);
5924 *ap->a_retval = val;
5927 if (error != EOPNOTSUPP)
5930 switch (ap->a_name) {
5932 *ap->a_retval = NAME_MAX;
5935 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) {
5936 *ap->a_retval = PIPE_BUF;
5941 return (vop_stdpathconf(ap));
5946 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5947 * extended attribute name:
5950 * system freebsd:system:
5951 * user (none, can be used to access ZFS fsattr(5) attributes
5952 * created on Solaris)
5955 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5958 const char *namespace, *prefix, *suffix;
5960 /* We don't allow '/' character in attribute name. */
5961 if (strchr(name, '/') != NULL)
5963 /* We don't allow attribute names that start with "freebsd:" string. */
5964 if (strncmp(name, "freebsd:", 8) == 0)
5967 bzero(attrname, size);
5969 switch (attrnamespace) {
5970 case EXTATTR_NAMESPACE_USER:
5972 prefix = "freebsd:";
5973 namespace = EXTATTR_NAMESPACE_USER_STRING;
5977 * This is the default namespace by which we can access all
5978 * attributes created on Solaris.
5980 prefix = namespace = suffix = "";
5983 case EXTATTR_NAMESPACE_SYSTEM:
5984 prefix = "freebsd:";
5985 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5988 case EXTATTR_NAMESPACE_EMPTY:
5992 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5994 return (ENAMETOOLONG);
5999 #ifndef _SYS_SYSPROTO_H_
6000 struct vop_getextattr {
6001 IN struct vnode *a_vp;
6002 IN int a_attrnamespace;
6003 IN const char *a_name;
6004 INOUT struct uio *a_uio;
6006 IN struct ucred *a_cred;
6007 IN struct thread *a_td;
6012 * Vnode operating to retrieve a named extended attribute.
6015 zfs_getextattr(struct vop_getextattr_args *ap)
6017 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6018 struct thread *td = ap->a_td;
6019 struct nameidata nd;
6022 vnode_t *xvp = NULL, *vp;
6026 * If the xattr property is off, refuse the request.
6028 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
6029 return (SET_ERROR(EOPNOTSUPP));
6032 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6033 ap->a_cred, ap->a_td, VREAD);
6037 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6044 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6045 LOOKUP_XATTR, B_FALSE);
6052 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6054 error = vn_open_cred(&nd, &flags, 0, VN_OPEN_INVFS, ap->a_cred, NULL);
6056 NDFREE(&nd, NDF_ONLY_PNBUF);
6059 if (error == ENOENT)
6064 if (ap->a_size != NULL) {
6065 error = VOP_GETATTR(vp, &va, ap->a_cred);
6067 *ap->a_size = (size_t)va.va_size;
6068 } else if (ap->a_uio != NULL)
6069 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6072 vn_close(vp, flags, ap->a_cred, td);
6077 #ifndef _SYS_SYSPROTO_H_
6078 struct vop_deleteextattr {
6079 IN struct vnode *a_vp;
6080 IN int a_attrnamespace;
6081 IN const char *a_name;
6082 IN struct ucred *a_cred;
6083 IN struct thread *a_td;
6088 * Vnode operation to remove a named attribute.
6091 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6093 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6094 struct thread *td = ap->a_td;
6095 struct nameidata nd;
6097 vnode_t *xvp = NULL, *vp;
6101 * If the xattr property is off, refuse the request.
6103 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
6104 return (SET_ERROR(EOPNOTSUPP));
6107 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6108 ap->a_cred, ap->a_td, VWRITE);
6112 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6119 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6120 LOOKUP_XATTR, B_FALSE);
6126 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
6127 UIO_SYSSPACE, attrname, xvp, td);
6132 NDFREE(&nd, NDF_ONLY_PNBUF);
6133 if (error == ENOENT)
6138 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6139 NDFREE(&nd, NDF_ONLY_PNBUF);
6142 if (vp == nd.ni_dvp)
6151 #ifndef _SYS_SYSPROTO_H_
6152 struct vop_setextattr {
6153 IN struct vnode *a_vp;
6154 IN int a_attrnamespace;
6155 IN const char *a_name;
6156 INOUT struct uio *a_uio;
6157 IN struct ucred *a_cred;
6158 IN struct thread *a_td;
6163 * Vnode operation to set a named attribute.
6166 zfs_setextattr(struct vop_setextattr_args *ap)
6168 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6169 struct thread *td = ap->a_td;
6170 struct nameidata nd;
6173 vnode_t *xvp = NULL, *vp;
6177 * If the xattr property is off, refuse the request.
6179 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
6180 return (SET_ERROR(EOPNOTSUPP));
6183 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6184 ap->a_cred, ap->a_td, VWRITE);
6187 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6194 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6195 LOOKUP_XATTR | CREATE_XATTR_DIR, B_FALSE);
6201 flags = FFLAGS(O_WRONLY | O_CREAT);
6202 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
6204 error = vn_open_cred(&nd, &flags, 0600, VN_OPEN_INVFS, ap->a_cred,
6207 NDFREE(&nd, NDF_ONLY_PNBUF);
6215 error = VOP_SETATTR(vp, &va, ap->a_cred);
6217 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6220 vn_close(vp, flags, ap->a_cred, td);
6225 #ifndef _SYS_SYSPROTO_H_
6226 struct vop_listextattr {
6227 IN struct vnode *a_vp;
6228 IN int a_attrnamespace;
6229 INOUT struct uio *a_uio;
6231 IN struct ucred *a_cred;
6232 IN struct thread *a_td;
6237 * Vnode operation to retrieve extended attributes on a vnode.
6240 zfs_listextattr(struct vop_listextattr_args *ap)
6242 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6243 struct thread *td = ap->a_td;
6244 struct nameidata nd;
6245 char attrprefix[16];
6246 uint8_t dirbuf[sizeof (struct dirent)];
6249 struct uio auio, *uio = ap->a_uio;
6250 size_t *sizep = ap->a_size;
6252 vnode_t *xvp = NULL, *vp;
6253 int done, error, eof, pos;
6256 * If the xattr property is off, refuse the request.
6258 if (!(zfsvfs->z_flags & ZSB_XATTR)) {
6259 return (SET_ERROR(EOPNOTSUPP));
6262 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6263 ap->a_cred, ap->a_td, VREAD);
6267 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6268 sizeof (attrprefix));
6271 plen = strlen(attrprefix);
6278 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6279 LOOKUP_XATTR, B_FALSE);
6283 * ENOATTR means that the EA directory does not yet exist,
6284 * i.e. there are no extended attributes there.
6286 if (error == ENOATTR)
6291 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
6292 UIO_SYSSPACE, ".", xvp, td);
6295 NDFREE(&nd, NDF_ONLY_PNBUF);
6301 auio.uio_iov = &aiov;
6302 auio.uio_iovcnt = 1;
6303 auio.uio_segflg = UIO_SYSSPACE;
6305 auio.uio_rw = UIO_READ;
6306 auio.uio_offset = 0;
6311 aiov.iov_base = (void *)dirbuf;
6312 aiov.iov_len = sizeof (dirbuf);
6313 auio.uio_resid = sizeof (dirbuf);
6314 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
6315 done = sizeof (dirbuf) - auio.uio_resid;
6318 for (pos = 0; pos < done; ) {
6319 dp = (struct dirent *)(dirbuf + pos);
6320 pos += dp->d_reclen;
6322 * XXX: Temporarily we also accept DT_UNKNOWN, as this
6323 * is what we get when attribute was created on Solaris.
6325 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
6328 strncmp(dp->d_name, "freebsd:", 8) == 0)
6330 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
6332 nlen = dp->d_namlen - plen;
6335 else if (uio != NULL) {
6337 * Format of extattr name entry is one byte for
6338 * length and the rest for name.
6340 error = uiomove(&nlen, 1, uio->uio_rw, uio);
6342 error = uiomove(dp->d_name + plen, nlen,
6349 } while (!eof && error == 0);
6357 #ifndef _SYS_SYSPROTO_H_
6358 struct vop_getacl_args {
6368 zfs_freebsd_getacl(struct vop_getacl_args *ap)
6371 vsecattr_t vsecattr;
6373 if (ap->a_type != ACL_TYPE_NFS4)
6376 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6377 if ((error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL)))
6380 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp,
6381 vsecattr.vsa_aclcnt);
6382 if (vsecattr.vsa_aclentp != NULL)
6383 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6388 #ifndef _SYS_SYSPROTO_H_
6389 struct vop_setacl_args {
6399 zfs_freebsd_setacl(struct vop_setacl_args *ap)
6402 vsecattr_t vsecattr;
6403 int aclbsize; /* size of acl list in bytes */
6406 if (ap->a_type != ACL_TYPE_NFS4)
6409 if (ap->a_aclp == NULL)
6412 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6416 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6417 * splitting every entry into two and appending "canonical six"
6418 * entries at the end. Don't allow for setting an ACL that would
6419 * cause chmod(2) to run out of ACL entries.
6421 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6424 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6428 vsecattr.vsa_mask = VSA_ACE;
6429 aclbsize = ap->a_aclp->acl_cnt * sizeof (ace_t);
6430 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6431 aaclp = vsecattr.vsa_aclentp;
6432 vsecattr.vsa_aclentsz = aclbsize;
6434 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6435 error = zfs_setsecattr(VTOZ(ap->a_vp), &vsecattr, 0, ap->a_cred);
6436 kmem_free(aaclp, aclbsize);
6441 #ifndef _SYS_SYSPROTO_H_
6442 struct vop_aclcheck_args {
6452 zfs_freebsd_aclcheck(struct vop_aclcheck_args *ap)
6455 return (EOPNOTSUPP);
6459 zfs_vptocnp(struct vop_vptocnp_args *ap)
6461 vnode_t *covered_vp;
6462 vnode_t *vp = ap->a_vp;
6463 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
6464 znode_t *zp = VTOZ(vp);
6472 * If we are a snapshot mounted under .zfs, run the operation
6473 * on the covered vnode.
6475 if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) {
6476 char name[MAXNAMLEN + 1];
6480 error = zfs_znode_parent_and_name(zp, &dzp, name);
6483 if (*ap->a_buflen < len)
6484 error = SET_ERROR(ENOMEM);
6487 *ap->a_buflen -= len;
6488 bcopy(name, ap->a_buf + *ap->a_buflen, len);
6489 *ap->a_vpp = ZTOV(dzp);
6496 covered_vp = vp->v_mount->mnt_vnodecovered;
6497 #if __FreeBSD_version >= 1300045
6498 enum vgetstate vs = vget_prep(covered_vp);
6502 ltype = VOP_ISLOCKED(vp);
6504 #if __FreeBSD_version >= 1300045
6505 error = vget_finish(covered_vp, LK_SHARED, vs);
6507 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
6510 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
6511 ap->a_buf, ap->a_buflen);
6514 vn_lock(vp, ltype | LK_RETRY);
6515 if (VN_IS_DOOMED(vp))
6516 error = SET_ERROR(ENOENT);
6521 #ifndef _SYS_SYSPROTO_H_
6522 struct vop_lock1_args {
6531 zfs_lock(struct vop_lock1_args *ap)
6537 #if __FreeBSD_version >= 1300064
6540 err = vop_stdlock(ap);
6542 if (err == 0 && (ap->a_flags & LK_NOWAIT) == 0) {
6545 if (vp->v_mount != NULL && !VN_IS_DOOMED(vp) &&
6546 zp != NULL && (zp->z_pflags & ZFS_XATTR) == 0)
6547 VERIFY(!RRM_LOCK_HELD(&zp->z_zfsvfs->z_teardown_lock));
6553 struct vop_vector zfs_vnodeops;
6554 struct vop_vector zfs_fifoops;
6555 struct vop_vector zfs_shareops;
6557 struct vop_vector zfs_vnodeops = {
6558 .vop_default = &default_vnodeops,
6559 .vop_inactive = zfs_freebsd_inactive,
6560 #if __FreeBSD_version >= 1300042
6561 .vop_need_inactive = zfs_freebsd_need_inactive,
6563 .vop_reclaim = zfs_freebsd_reclaim,
6564 #if __FreeBSD_version >= 1300102
6565 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6567 .vop_access = zfs_freebsd_access,
6568 .vop_allocate = VOP_EINVAL,
6569 .vop_lookup = zfs_cache_lookup,
6570 .vop_cachedlookup = zfs_freebsd_cachedlookup,
6571 .vop_getattr = zfs_freebsd_getattr,
6572 .vop_setattr = zfs_freebsd_setattr,
6573 .vop_create = zfs_freebsd_create,
6574 .vop_mknod = (vop_mknod_t *)zfs_freebsd_create,
6575 .vop_mkdir = zfs_freebsd_mkdir,
6576 .vop_readdir = zfs_freebsd_readdir,
6577 .vop_fsync = zfs_freebsd_fsync,
6578 .vop_open = zfs_freebsd_open,
6579 .vop_close = zfs_freebsd_close,
6580 .vop_rmdir = zfs_freebsd_rmdir,
6581 .vop_ioctl = zfs_freebsd_ioctl,
6582 .vop_link = zfs_freebsd_link,
6583 .vop_symlink = zfs_freebsd_symlink,
6584 .vop_readlink = zfs_freebsd_readlink,
6585 .vop_read = zfs_freebsd_read,
6586 .vop_write = zfs_freebsd_write,
6587 .vop_remove = zfs_freebsd_remove,
6588 .vop_rename = zfs_freebsd_rename,
6589 .vop_pathconf = zfs_freebsd_pathconf,
6590 .vop_bmap = zfs_freebsd_bmap,
6591 .vop_fid = zfs_freebsd_fid,
6592 .vop_getextattr = zfs_getextattr,
6593 .vop_deleteextattr = zfs_deleteextattr,
6594 .vop_setextattr = zfs_setextattr,
6595 .vop_listextattr = zfs_listextattr,
6596 .vop_getacl = zfs_freebsd_getacl,
6597 .vop_setacl = zfs_freebsd_setacl,
6598 .vop_aclcheck = zfs_freebsd_aclcheck,
6599 .vop_getpages = zfs_freebsd_getpages,
6600 .vop_putpages = zfs_freebsd_putpages,
6601 .vop_vptocnp = zfs_vptocnp,
6602 #if __FreeBSD_version >= 1300064
6604 .vop_lock1 = zfs_lock,
6606 .vop_lock1 = vop_lock,
6608 .vop_unlock = vop_unlock,
6609 .vop_islocked = vop_islocked,
6612 .vop_lock1 = zfs_lock,
6616 VFS_VOP_VECTOR_REGISTER(zfs_vnodeops);
6618 struct vop_vector zfs_fifoops = {
6619 .vop_default = &fifo_specops,
6620 .vop_fsync = zfs_freebsd_fsync,
6621 #if __FreeBSD_version >= 1300102
6622 .vop_fplookup_vexec = zfs_freebsd_fplookup_vexec,
6624 .vop_access = zfs_freebsd_access,
6625 .vop_getattr = zfs_freebsd_getattr,
6626 .vop_inactive = zfs_freebsd_inactive,
6627 .vop_read = VOP_PANIC,
6628 .vop_reclaim = zfs_freebsd_reclaim,
6629 .vop_setattr = zfs_freebsd_setattr,
6630 .vop_write = VOP_PANIC,
6631 .vop_pathconf = zfs_freebsd_pathconf,
6632 .vop_fid = zfs_freebsd_fid,
6633 .vop_getacl = zfs_freebsd_getacl,
6634 .vop_setacl = zfs_freebsd_setacl,
6635 .vop_aclcheck = zfs_freebsd_aclcheck,
6637 VFS_VOP_VECTOR_REGISTER(zfs_fifoops);
6640 * special share hidden files vnode operations template
6642 struct vop_vector zfs_shareops = {
6643 .vop_default = &default_vnodeops,
6644 #if __FreeBSD_version >= 1300121
6645 .vop_fplookup_vexec = VOP_EAGAIN,
6647 .vop_access = zfs_freebsd_access,
6648 .vop_inactive = zfs_freebsd_inactive,
6649 .vop_reclaim = zfs_freebsd_reclaim,
6650 .vop_fid = zfs_freebsd_fid,
6651 .vop_pathconf = zfs_freebsd_pathconf,
6653 VFS_VOP_VECTOR_REGISTER(zfs_shareops);