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>
40 #include <sys/vnode.h>
44 #include <sys/taskq.h>
46 #include <sys/atomic.h>
47 #include <sys/namei.h>
49 #include <sys/cmn_err.h>
50 #include <sys/errno.h>
51 #include <sys/unistd.h>
52 #include <sys/zfs_dir.h>
53 #include <sys/zfs_ioctl.h>
54 #include <sys/fs/zfs.h>
56 #include <sys/dmu_objset.h>
62 #include <sys/dirent.h>
63 #include <sys/policy.h>
64 #include <sys/sunddi.h>
65 #include <sys/filio.h>
67 #include <sys/zfs_ctldir.h>
68 #include <sys/zfs_fuid.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_rlock.h>
71 #include <sys/extdirent.h>
72 #include <sys/kidmap.h>
75 #include <sys/sched.h>
77 #include <sys/vmmeter.h>
78 #include <vm/vm_param.h>
83 * Each vnode op performs some logical unit of work. To do this, the ZPL must
84 * properly lock its in-core state, create a DMU transaction, do the work,
85 * record this work in the intent log (ZIL), commit the DMU transaction,
86 * and wait for the intent log to commit if it is a synchronous operation.
87 * Moreover, the vnode ops must work in both normal and log replay context.
88 * The ordering of events is important to avoid deadlocks and references
89 * to freed memory. The example below illustrates the following Big Rules:
91 * (1) A check must be made in each zfs thread for a mounted file system.
92 * This is done avoiding races using ZFS_ENTER(zfsvfs).
93 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
94 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
95 * can return EIO from the calling function.
97 * (2) VN_RELE() should always be the last thing except for zil_commit()
98 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
99 * First, if it's the last reference, the vnode/znode
100 * can be freed, so the zp may point to freed memory. Second, the last
101 * reference will call zfs_zinactive(), which may induce a lot of work --
102 * pushing cached pages (which acquires range locks) and syncing out
103 * cached atime changes. Third, zfs_zinactive() may require a new tx,
104 * which could deadlock the system if you were already holding one.
105 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
107 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
108 * as they can span dmu_tx_assign() calls.
110 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
111 * dmu_tx_assign(). This is critical because we don't want to block
112 * while holding locks.
114 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
115 * reduces lock contention and CPU usage when we must wait (note that if
116 * throughput is constrained by the storage, nearly every transaction
119 * Note, in particular, that if a lock is sometimes acquired before
120 * the tx assigns, and sometimes after (e.g. z_lock), then failing
121 * to use a non-blocking assign can deadlock the system. The scenario:
123 * Thread A has grabbed a lock before calling dmu_tx_assign().
124 * Thread B is in an already-assigned tx, and blocks for this lock.
125 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
126 * forever, because the previous txg can't quiesce until B's tx commits.
128 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
129 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
130 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
131 * to indicate that this operation has already called dmu_tx_wait().
132 * This will ensure that we don't retry forever, waiting a short bit
135 * (5) If the operation succeeded, generate the intent log entry for it
136 * before dropping locks. This ensures that the ordering of events
137 * in the intent log matches the order in which they actually occurred.
138 * During ZIL replay the zfs_log_* functions will update the sequence
139 * number to indicate the zil transaction has replayed.
141 * (6) At the end of each vnode op, the DMU tx must always commit,
142 * regardless of whether there were any errors.
144 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
145 * to ensure that synchronous semantics are provided when necessary.
147 * In general, this is how things should be ordered in each vnode op:
149 * ZFS_ENTER(zfsvfs); // exit if unmounted
151 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
152 * rw_enter(...); // grab any other locks you need
153 * tx = dmu_tx_create(...); // get DMU tx
154 * dmu_tx_hold_*(); // hold each object you might modify
155 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
157 * rw_exit(...); // drop locks
158 * zfs_dirent_unlock(dl); // unlock directory entry
159 * VN_RELE(...); // release held vnodes
160 * if (error == ERESTART) {
166 * dmu_tx_abort(tx); // abort DMU tx
167 * ZFS_EXIT(zfsvfs); // finished in zfs
168 * return (error); // really out of space
170 * error = do_real_work(); // do whatever this VOP does
172 * zfs_log_*(...); // on success, make ZIL entry
173 * dmu_tx_commit(tx); // commit DMU tx -- error or not
174 * rw_exit(...); // drop locks
175 * zfs_dirent_unlock(dl); // unlock directory entry
176 * VN_RELE(...); // release held vnodes
177 * zil_commit(zilog, foid); // synchronous when necessary
178 * ZFS_EXIT(zfsvfs); // finished in zfs
179 * return (error); // done, report error
184 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
186 znode_t *zp = VTOZ(*vpp);
187 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
192 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
193 ((flag & FAPPEND) == 0)) {
195 return (SET_ERROR(EPERM));
198 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
199 ZTOV(zp)->v_type == VREG &&
200 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
201 if (fs_vscan(*vpp, cr, 0) != 0) {
203 return (SET_ERROR(EACCES));
207 /* Keep a count of the synchronous opens in the znode */
208 if (flag & (FSYNC | FDSYNC))
209 atomic_inc_32(&zp->z_sync_cnt);
217 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
218 caller_context_t *ct)
220 znode_t *zp = VTOZ(vp);
221 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
224 * Clean up any locks held by this process on the vp.
226 cleanlocks(vp, ddi_get_pid(), 0);
227 cleanshares(vp, ddi_get_pid());
232 /* Decrement the synchronous opens in the znode */
233 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
234 atomic_dec_32(&zp->z_sync_cnt);
236 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
237 ZTOV(zp)->v_type == VREG &&
238 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
239 VERIFY(fs_vscan(vp, cr, 1) == 0);
246 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
247 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
250 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
252 znode_t *zp = VTOZ(vp);
253 uint64_t noff = (uint64_t)*off; /* new offset */
258 file_sz = zp->z_size;
259 if (noff >= file_sz) {
260 return (SET_ERROR(ENXIO));
263 if (cmd == _FIO_SEEK_HOLE)
268 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
271 return (SET_ERROR(ENXIO));
274 * We could find a hole that begins after the logical end-of-file,
275 * because dmu_offset_next() only works on whole blocks. If the
276 * EOF falls mid-block, then indicate that the "virtual hole"
277 * at the end of the file begins at the logical EOF, rather than
278 * at the end of the last block.
280 if (noff > file_sz) {
293 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
294 int *rvalp, caller_context_t *ct)
298 dmu_object_info_t doi;
309 * The following two ioctls are used by bfu. Faking out,
310 * necessary to avoid bfu errors.
323 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
324 return (SET_ERROR(EFAULT));
326 off = *(offset_t *)data;
329 zfsvfs = zp->z_zfsvfs;
333 /* offset parameter is in/out */
334 error = zfs_holey(vp, com, &off);
339 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
340 return (SET_ERROR(EFAULT));
342 *(offset_t *)data = off;
347 case _FIO_COUNT_FILLED:
350 * _FIO_COUNT_FILLED adds a new ioctl command which
351 * exposes the number of filled blocks in a
355 zfsvfs = zp->z_zfsvfs;
360 * Wait for all dirty blocks for this object
361 * to get synced out to disk, and the DMU info
364 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
371 * Retrieve fill count from DMU object.
373 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
379 ndata = doi.doi_fill_count;
382 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
383 return (SET_ERROR(EFAULT));
388 return (SET_ERROR(ENOTTY));
392 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
399 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
400 * aligned boundaries, if the range is not aligned. As a result a
401 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
402 * It may happen that all DEV_BSIZE subranges are marked clean and thus
403 * the whole page would be considred clean despite have some dirty data.
404 * For this reason we should shrink the range to DEV_BSIZE aligned
405 * boundaries before calling vm_page_clear_dirty.
407 end = rounddown2(off + nbytes, DEV_BSIZE);
408 off = roundup2(off, DEV_BSIZE);
412 zfs_vmobject_assert_wlocked(obj);
415 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
417 if (vm_page_xbusied(pp)) {
419 * Reference the page before unlocking and
420 * sleeping so that the page daemon is less
421 * likely to reclaim it.
423 vm_page_reference(pp);
425 zfs_vmobject_wunlock(obj);
426 vm_page_busy_sleep(pp, "zfsmwb", true);
427 zfs_vmobject_wlock(obj);
431 } else if (pp != NULL) {
437 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
438 vm_object_pip_add(obj, 1);
439 pmap_remove_write(pp);
441 vm_page_clear_dirty(pp, off, nbytes);
449 page_unbusy(vm_page_t pp)
453 vm_object_pip_subtract(pp->object, 1);
457 page_hold(vnode_t *vp, int64_t start)
463 zfs_vmobject_assert_wlocked(obj);
466 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
468 if (vm_page_xbusied(pp)) {
470 * Reference the page before unlocking and
471 * sleeping so that the page daemon is less
472 * likely to reclaim it.
474 vm_page_reference(pp);
476 zfs_vmobject_wunlock(obj);
477 vm_page_busy_sleep(pp, "zfsmwb", true);
478 zfs_vmobject_wlock(obj);
482 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
495 page_unhold(vm_page_t pp)
504 * When a file is memory mapped, we must keep the IO data synchronized
505 * between the DMU cache and the memory mapped pages. What this means:
507 * On Write: If we find a memory mapped page, we write to *both*
508 * the page and the dmu buffer.
511 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
512 int segflg, dmu_tx_t *tx)
519 ASSERT(segflg != UIO_NOCOPY);
520 ASSERT(vp->v_mount != NULL);
524 off = start & PAGEOFFSET;
525 zfs_vmobject_wlock(obj);
526 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
528 int nbytes = imin(PAGESIZE - off, len);
530 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
531 zfs_vmobject_wunlock(obj);
533 va = zfs_map_page(pp, &sf);
534 (void) dmu_read(os, oid, start+off, nbytes,
535 va+off, DMU_READ_PREFETCH);;
538 zfs_vmobject_wlock(obj);
544 vm_object_pip_wakeupn(obj, 0);
545 zfs_vmobject_wunlock(obj);
549 * Read with UIO_NOCOPY flag means that sendfile(2) requests
550 * ZFS to populate a range of page cache pages with data.
552 * NOTE: this function could be optimized to pre-allocate
553 * all pages in advance, drain exclusive busy on all of them,
554 * map them into contiguous KVA region and populate them
555 * in one single dmu_read() call.
558 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
560 znode_t *zp = VTOZ(vp);
561 objset_t *os = zp->z_zfsvfs->z_os;
571 ASSERT(uio->uio_segflg == UIO_NOCOPY);
572 ASSERT(vp->v_mount != NULL);
575 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
577 zfs_vmobject_wlock(obj);
578 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
579 int bytes = MIN(PAGESIZE, len);
581 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY |
582 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
583 if (pp->valid == 0) {
584 zfs_vmobject_wunlock(obj);
585 va = zfs_map_page(pp, &sf);
586 error = dmu_read(os, zp->z_id, start, bytes, va,
588 if (bytes != PAGESIZE && error == 0)
589 bzero(va + bytes, PAGESIZE - bytes);
591 zfs_vmobject_wlock(obj);
595 if (pp->wire_count == 0 && pp->valid == 0 &&
599 pp->valid = VM_PAGE_BITS_ALL;
600 vm_page_activate(pp);
604 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
609 uio->uio_resid -= bytes;
610 uio->uio_offset += bytes;
613 zfs_vmobject_wunlock(obj);
618 * When a file is memory mapped, we must keep the IO data synchronized
619 * between the DMU cache and the memory mapped pages. What this means:
621 * On Read: We "read" preferentially from memory mapped pages,
622 * else we default from the dmu buffer.
624 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
625 * the file is memory mapped.
628 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
630 znode_t *zp = VTOZ(vp);
638 ASSERT(vp->v_mount != NULL);
642 start = uio->uio_loffset;
643 off = start & PAGEOFFSET;
644 zfs_vmobject_wlock(obj);
645 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
647 uint64_t bytes = MIN(PAGESIZE - off, len);
649 if (pp = page_hold(vp, start)) {
653 zfs_vmobject_wunlock(obj);
654 va = zfs_map_page(pp, &sf);
656 error = uiomove(va + off, bytes, UIO_READ, uio);
658 error = vn_io_fault_uiomove(va + off, bytes, uio);
661 zfs_vmobject_wlock(obj);
664 zfs_vmobject_wunlock(obj);
665 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
667 zfs_vmobject_wlock(obj);
674 zfs_vmobject_wunlock(obj);
678 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
681 * Read bytes from specified file into supplied buffer.
683 * IN: vp - vnode of file to be read from.
684 * uio - structure supplying read location, range info,
686 * ioflag - SYNC flags; used to provide FRSYNC semantics.
687 * cr - credentials of caller.
688 * ct - caller context
690 * OUT: uio - updated offset and range, buffer filled.
692 * RETURN: 0 on success, error code on failure.
695 * vp - atime updated if byte count > 0
699 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
701 znode_t *zp = VTOZ(vp);
702 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
711 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
713 return (SET_ERROR(EACCES));
717 * Validate file offset
719 if (uio->uio_loffset < (offset_t)0) {
721 return (SET_ERROR(EINVAL));
725 * Fasttrack empty reads
727 if (uio->uio_resid == 0) {
733 * Check for mandatory locks
735 if (MANDMODE(zp->z_mode)) {
736 if (error = chklock(vp, FREAD,
737 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
744 * If we're in FRSYNC mode, sync out this znode before reading it.
747 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
748 zil_commit(zfsvfs->z_log, zp->z_id);
751 * Lock the range against changes.
753 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
756 * If we are reading past end-of-file we can skip
757 * to the end; but we might still need to set atime.
759 if (uio->uio_loffset >= zp->z_size) {
764 ASSERT(uio->uio_loffset < zp->z_size);
765 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
768 if ((uio->uio_extflg == UIO_XUIO) &&
769 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
771 int blksz = zp->z_blksz;
772 uint64_t offset = uio->uio_loffset;
774 xuio = (xuio_t *)uio;
776 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
779 ASSERT(offset + n <= blksz);
782 (void) dmu_xuio_init(xuio, nblk);
784 if (vn_has_cached_data(vp)) {
786 * For simplicity, we always allocate a full buffer
787 * even if we only expect to read a portion of a block.
789 while (--nblk >= 0) {
790 (void) dmu_xuio_add(xuio,
791 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
799 nbytes = MIN(n, zfs_read_chunk_size -
800 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
803 if (uio->uio_segflg == UIO_NOCOPY)
804 error = mappedread_sf(vp, nbytes, uio);
806 #endif /* __FreeBSD__ */
807 if (vn_has_cached_data(vp)) {
808 error = mappedread(vp, nbytes, uio);
810 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
814 /* convert checksum errors into IO errors */
816 error = SET_ERROR(EIO);
823 zfs_range_unlock(rl);
825 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
831 * Write the bytes to a file.
833 * IN: vp - vnode of file to be written to.
834 * uio - structure supplying write location, range info,
836 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
837 * set if in append mode.
838 * cr - credentials of caller.
839 * ct - caller context (NFS/CIFS fem monitor only)
841 * OUT: uio - updated offset and range.
843 * RETURN: 0 on success, error code on failure.
846 * vp - ctime|mtime updated if byte count > 0
851 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
853 znode_t *zp = VTOZ(vp);
854 rlim64_t limit = MAXOFFSET_T;
855 ssize_t start_resid = uio->uio_resid;
859 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
864 int max_blksz = zfsvfs->z_max_blksz;
867 iovec_t *aiov = NULL;
870 int iovcnt = uio->uio_iovcnt;
871 iovec_t *iovp = uio->uio_iov;
874 sa_bulk_attr_t bulk[4];
875 uint64_t mtime[2], ctime[2];
878 * Fasttrack empty write
884 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
890 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
891 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
892 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
894 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
898 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
899 * callers might not be able to detect properly that we are read-only,
900 * so check it explicitly here.
902 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
904 return (SET_ERROR(EROFS));
908 * If immutable or not appending then return EPERM.
909 * Intentionally allow ZFS_READONLY through here.
910 * See zfs_zaccess_common()
912 if ((zp->z_pflags & ZFS_IMMUTABLE) ||
913 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
914 (uio->uio_loffset < zp->z_size))) {
916 return (SET_ERROR(EPERM));
919 zilog = zfsvfs->z_log;
922 * Validate file offset
924 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
927 return (SET_ERROR(EINVAL));
931 * Check for mandatory locks before calling zfs_range_lock()
932 * in order to prevent a deadlock with locks set via fcntl().
934 if (MANDMODE((mode_t)zp->z_mode) &&
935 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
942 * Pre-fault the pages to ensure slow (eg NFS) pages
944 * Skip this if uio contains loaned arc_buf.
946 if ((uio->uio_extflg == UIO_XUIO) &&
947 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
948 xuio = (xuio_t *)uio;
950 uio_prefaultpages(MIN(n, max_blksz), uio);
954 * If in append mode, set the io offset pointer to eof.
956 if (ioflag & FAPPEND) {
958 * Obtain an appending range lock to guarantee file append
959 * semantics. We reset the write offset once we have the lock.
961 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
963 if (rl->r_len == UINT64_MAX) {
965 * We overlocked the file because this write will cause
966 * the file block size to increase.
967 * Note that zp_size cannot change with this lock held.
971 uio->uio_loffset = woff;
974 * Note that if the file block size will change as a result of
975 * this write, then this range lock will lock the entire file
976 * so that we can re-write the block safely.
978 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
981 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
982 zfs_range_unlock(rl);
988 zfs_range_unlock(rl);
990 return (SET_ERROR(EFBIG));
993 if ((woff + n) > limit || woff > (limit - n))
996 /* Will this write extend the file length? */
997 write_eof = (woff + n > zp->z_size);
999 end_size = MAX(zp->z_size, woff + n);
1002 * Write the file in reasonable size chunks. Each chunk is written
1003 * in a separate transaction; this keeps the intent log records small
1004 * and allows us to do more fine-grained space accounting.
1008 woff = uio->uio_loffset;
1009 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
1010 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
1012 dmu_return_arcbuf(abuf);
1013 error = SET_ERROR(EDQUOT);
1017 if (xuio && abuf == NULL) {
1018 ASSERT(i_iov < iovcnt);
1019 aiov = &iovp[i_iov];
1020 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1021 dmu_xuio_clear(xuio, i_iov);
1022 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1023 iovec_t *, aiov, arc_buf_t *, abuf);
1024 ASSERT((aiov->iov_base == abuf->b_data) ||
1025 ((char *)aiov->iov_base - (char *)abuf->b_data +
1026 aiov->iov_len == arc_buf_size(abuf)));
1028 } else if (abuf == NULL && n >= max_blksz &&
1029 woff >= zp->z_size &&
1030 P2PHASE(woff, max_blksz) == 0 &&
1031 zp->z_blksz == max_blksz) {
1033 * This write covers a full block. "Borrow" a buffer
1034 * from the dmu so that we can fill it before we enter
1035 * a transaction. This avoids the possibility of
1036 * holding up the transaction if the data copy hangs
1037 * up on a pagefault (e.g., from an NFS server mapping).
1043 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1045 ASSERT(abuf != NULL);
1046 ASSERT(arc_buf_size(abuf) == max_blksz);
1048 if (error = uiocopy(abuf->b_data, max_blksz,
1049 UIO_WRITE, uio, &cbytes)) {
1050 dmu_return_arcbuf(abuf);
1053 ASSERT(cbytes == max_blksz);
1055 ssize_t resid = uio->uio_resid;
1056 error = vn_io_fault_uiomove(abuf->b_data, max_blksz, uio);
1058 uio->uio_offset -= resid - uio->uio_resid;
1059 uio->uio_resid = resid;
1060 dmu_return_arcbuf(abuf);
1067 * Start a transaction.
1069 tx = dmu_tx_create(zfsvfs->z_os);
1070 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1071 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1072 zfs_sa_upgrade_txholds(tx, zp);
1073 error = dmu_tx_assign(tx, TXG_WAIT);
1077 dmu_return_arcbuf(abuf);
1082 * If zfs_range_lock() over-locked we grow the blocksize
1083 * and then reduce the lock range. This will only happen
1084 * on the first iteration since zfs_range_reduce() will
1085 * shrink down r_len to the appropriate size.
1087 if (rl->r_len == UINT64_MAX) {
1090 if (zp->z_blksz > max_blksz) {
1092 * File's blocksize is already larger than the
1093 * "recordsize" property. Only let it grow to
1094 * the next power of 2.
1096 ASSERT(!ISP2(zp->z_blksz));
1097 new_blksz = MIN(end_size,
1098 1 << highbit64(zp->z_blksz));
1100 new_blksz = MIN(end_size, max_blksz);
1102 zfs_grow_blocksize(zp, new_blksz, tx);
1103 zfs_range_reduce(rl, woff, n);
1107 * XXX - should we really limit each write to z_max_blksz?
1108 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1110 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1112 if (woff + nbytes > zp->z_size)
1113 vnode_pager_setsize(vp, woff + nbytes);
1116 tx_bytes = uio->uio_resid;
1117 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1119 tx_bytes -= uio->uio_resid;
1122 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1124 * If this is not a full block write, but we are
1125 * extending the file past EOF and this data starts
1126 * block-aligned, use assign_arcbuf(). Otherwise,
1127 * write via dmu_write().
1129 if (tx_bytes < max_blksz && (!write_eof ||
1130 aiov->iov_base != abuf->b_data)) {
1132 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1133 aiov->iov_len, aiov->iov_base, tx);
1134 dmu_return_arcbuf(abuf);
1135 xuio_stat_wbuf_copied();
1137 ASSERT(xuio || tx_bytes == max_blksz);
1138 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1142 ASSERT(tx_bytes <= uio->uio_resid);
1143 uioskip(uio, tx_bytes);
1146 if (tx_bytes && vn_has_cached_data(vp)) {
1147 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1148 zp->z_id, uio->uio_segflg, tx);
1152 * If we made no progress, we're done. If we made even
1153 * partial progress, update the znode and ZIL accordingly.
1155 if (tx_bytes == 0) {
1156 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1157 (void *)&zp->z_size, sizeof (uint64_t), tx);
1164 * Clear Set-UID/Set-GID bits on successful write if not
1165 * privileged and at least one of the excute bits is set.
1167 * It would be nice to to this after all writes have
1168 * been done, but that would still expose the ISUID/ISGID
1169 * to another app after the partial write is committed.
1171 * Note: we don't call zfs_fuid_map_id() here because
1172 * user 0 is not an ephemeral uid.
1174 mutex_enter(&zp->z_acl_lock);
1175 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1176 (S_IXUSR >> 6))) != 0 &&
1177 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1178 secpolicy_vnode_setid_retain(vp, cr,
1179 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1181 zp->z_mode &= ~(S_ISUID | S_ISGID);
1182 newmode = zp->z_mode;
1183 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1184 (void *)&newmode, sizeof (uint64_t), tx);
1186 mutex_exit(&zp->z_acl_lock);
1188 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1192 * Update the file size (zp_size) if it has changed;
1193 * account for possible concurrent updates.
1195 while ((end_size = zp->z_size) < uio->uio_loffset) {
1196 (void) atomic_cas_64(&zp->z_size, end_size,
1201 ASSERT(error == 0 || error == EFAULT);
1205 * If we are replaying and eof is non zero then force
1206 * the file size to the specified eof. Note, there's no
1207 * concurrency during replay.
1209 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1210 zp->z_size = zfsvfs->z_replay_eof;
1213 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1215 (void) sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1217 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1222 ASSERT(tx_bytes == nbytes);
1227 uio_prefaultpages(MIN(n, max_blksz), uio);
1231 zfs_range_unlock(rl);
1234 * If we're in replay mode, or we made no progress, return error.
1235 * Otherwise, it's at least a partial write, so it's successful.
1237 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1244 * EFAULT means that at least one page of the source buffer was not
1245 * available. VFS will re-try remaining I/O upon this error.
1247 if (error == EFAULT) {
1253 if (ioflag & (FSYNC | FDSYNC) ||
1254 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1255 zil_commit(zilog, zp->z_id);
1262 zfs_get_done(zgd_t *zgd, int error)
1264 znode_t *zp = zgd->zgd_private;
1265 objset_t *os = zp->z_zfsvfs->z_os;
1268 dmu_buf_rele(zgd->zgd_db, zgd);
1270 zfs_range_unlock(zgd->zgd_rl);
1273 * Release the vnode asynchronously as we currently have the
1274 * txg stopped from syncing.
1276 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1278 if (error == 0 && zgd->zgd_bp)
1279 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1281 kmem_free(zgd, sizeof (zgd_t));
1285 static int zil_fault_io = 0;
1289 * Get data to generate a TX_WRITE intent log record.
1292 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1294 zfsvfs_t *zfsvfs = arg;
1295 objset_t *os = zfsvfs->z_os;
1297 uint64_t object = lr->lr_foid;
1298 uint64_t offset = lr->lr_offset;
1299 uint64_t size = lr->lr_length;
1300 blkptr_t *bp = &lr->lr_blkptr;
1305 ASSERT(zio != NULL);
1309 * Nothing to do if the file has been removed
1311 if (zfs_zget(zfsvfs, object, &zp) != 0)
1312 return (SET_ERROR(ENOENT));
1313 if (zp->z_unlinked) {
1315 * Release the vnode asynchronously as we currently have the
1316 * txg stopped from syncing.
1318 VN_RELE_ASYNC(ZTOV(zp),
1319 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1320 return (SET_ERROR(ENOENT));
1323 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1324 zgd->zgd_zilog = zfsvfs->z_log;
1325 zgd->zgd_private = zp;
1328 * Write records come in two flavors: immediate and indirect.
1329 * For small writes it's cheaper to store the data with the
1330 * log record (immediate); for large writes it's cheaper to
1331 * sync the data and get a pointer to it (indirect) so that
1332 * we don't have to write the data twice.
1334 if (buf != NULL) { /* immediate write */
1335 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1336 /* test for truncation needs to be done while range locked */
1337 if (offset >= zp->z_size) {
1338 error = SET_ERROR(ENOENT);
1340 error = dmu_read(os, object, offset, size, buf,
1341 DMU_READ_NO_PREFETCH);
1343 ASSERT(error == 0 || error == ENOENT);
1344 } else { /* indirect write */
1346 * Have to lock the whole block to ensure when it's
1347 * written out and it's checksum is being calculated
1348 * that no one can change the data. We need to re-check
1349 * blocksize after we get the lock in case it's changed!
1354 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1356 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1358 if (zp->z_blksz == size)
1361 zfs_range_unlock(zgd->zgd_rl);
1363 /* test for truncation needs to be done while range locked */
1364 if (lr->lr_offset >= zp->z_size)
1365 error = SET_ERROR(ENOENT);
1368 error = SET_ERROR(EIO);
1373 error = dmu_buf_hold(os, object, offset, zgd, &db,
1374 DMU_READ_NO_PREFETCH);
1377 blkptr_t *obp = dmu_buf_get_blkptr(db);
1379 ASSERT(BP_IS_HOLE(bp));
1386 ASSERT(db->db_offset == offset);
1387 ASSERT(db->db_size == size);
1389 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1391 ASSERT(error || lr->lr_length <= size);
1394 * On success, we need to wait for the write I/O
1395 * initiated by dmu_sync() to complete before we can
1396 * release this dbuf. We will finish everything up
1397 * in the zfs_get_done() callback.
1402 if (error == EALREADY) {
1403 lr->lr_common.lrc_txtype = TX_WRITE2;
1409 zfs_get_done(zgd, error);
1416 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1417 caller_context_t *ct)
1419 znode_t *zp = VTOZ(vp);
1420 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1426 if (flag & V_ACE_MASK)
1427 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1429 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1436 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1441 error = vn_lock(*vpp, lkflags);
1448 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
1450 znode_t *zdp = VTOZ(dvp);
1451 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1455 ASSERT_VOP_LOCKED(dvp, __func__);
1457 if ((zdp->z_pflags & ZFS_XATTR) == 0)
1458 VERIFY(!RRM_LOCK_HELD(&zfsvfs->z_teardown_lock));
1461 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
1462 ASSERT3P(dvp, ==, vp);
1464 ltype = lkflags & LK_TYPE_MASK;
1465 if (ltype != VOP_ISLOCKED(dvp)) {
1466 if (ltype == LK_EXCLUSIVE)
1467 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1468 else /* if (ltype == LK_SHARED) */
1469 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1472 * Relock for the "." case could leave us with
1475 if (dvp->v_iflag & VI_DOOMED) {
1477 return (SET_ERROR(ENOENT));
1481 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
1483 * Note that in this case, dvp is the child vnode, and we
1484 * are looking up the parent vnode - exactly reverse from
1485 * normal operation. Unlocking dvp requires some rather
1486 * tricky unlock/relock dance to prevent mp from being freed;
1487 * use vn_vget_ino_gen() which takes care of all that.
1489 * XXX Note that there is a time window when both vnodes are
1490 * unlocked. It is possible, although highly unlikely, that
1491 * during that window the parent-child relationship between
1492 * the vnodes may change, for example, get reversed.
1493 * In that case we would have a wrong lock order for the vnodes.
1494 * All other filesystems seem to ignore this problem, so we
1496 * A potential solution could be implemented as follows:
1497 * - using LK_NOWAIT when locking the second vnode and retrying
1499 * - checking that the parent-child relationship still holds
1500 * after locking both vnodes and retrying if it doesn't
1502 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
1505 error = vn_lock(vp, lkflags);
1513 * Lookup an entry in a directory, or an extended attribute directory.
1514 * If it exists, return a held vnode reference for it.
1516 * IN: dvp - vnode of directory to search.
1517 * nm - name of entry to lookup.
1518 * pnp - full pathname to lookup [UNUSED].
1519 * flags - LOOKUP_XATTR set if looking for an attribute.
1520 * rdir - root directory vnode [UNUSED].
1521 * cr - credentials of caller.
1522 * ct - caller context
1524 * OUT: vpp - vnode of located entry, NULL if not found.
1526 * RETURN: 0 on success, error code on failure.
1533 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1534 int nameiop, cred_t *cr, kthread_t *td, int flags)
1536 znode_t *zdp = VTOZ(dvp);
1538 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1542 * Fast path lookup, however we must skip DNLC lookup
1543 * for case folding or normalizing lookups because the
1544 * DNLC code only stores the passed in name. This means
1545 * creating 'a' and removing 'A' on a case insensitive
1546 * file system would work, but DNLC still thinks 'a'
1547 * exists and won't let you create it again on the next
1548 * pass through fast path.
1550 if (!(flags & LOOKUP_XATTR)) {
1551 if (dvp->v_type != VDIR) {
1552 return (SET_ERROR(ENOTDIR));
1553 } else if (zdp->z_sa_hdl == NULL) {
1554 return (SET_ERROR(EIO));
1558 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1565 if (flags & LOOKUP_XATTR) {
1568 * If the xattr property is off, refuse the lookup request.
1570 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1572 return (SET_ERROR(EINVAL));
1577 * We don't allow recursive attributes..
1578 * Maybe someday we will.
1580 if (zdp->z_pflags & ZFS_XATTR) {
1582 return (SET_ERROR(EINVAL));
1585 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1591 * Do we have permission to get into attribute directory?
1593 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1604 * Check accessibility of directory.
1606 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1611 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1612 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1614 return (SET_ERROR(EILSEQ));
1619 * First handle the special cases.
1621 if ((cnp->cn_flags & ISDOTDOT) != 0) {
1623 * If we are a snapshot mounted under .zfs, return
1624 * the vp for the snapshot directory.
1626 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
1627 struct componentname cn;
1632 ltype = VOP_ISLOCKED(dvp);
1634 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
1637 cn.cn_nameptr = "snapshot";
1638 cn.cn_namelen = strlen(cn.cn_nameptr);
1639 cn.cn_nameiop = cnp->cn_nameiop;
1640 cn.cn_flags = cnp->cn_flags & ~ISDOTDOT;
1641 cn.cn_lkflags = cnp->cn_lkflags;
1642 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
1645 vn_lock(dvp, ltype | LK_RETRY);
1649 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
1651 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
1652 return (SET_ERROR(ENOTSUP));
1653 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
1658 * The loop is retry the lookup if the parent-child relationship
1659 * changes during the dot-dot locking complexities.
1664 error = zfs_dirlook(zdp, nm, &zp);
1672 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
1675 * If we've got a locking error, then the vnode
1676 * got reclaimed because of a force unmount.
1677 * We never enter doomed vnodes into the name cache.
1683 if ((cnp->cn_flags & ISDOTDOT) == 0)
1687 if (zdp->z_sa_hdl == NULL) {
1688 error = SET_ERROR(EIO);
1690 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1691 &parent, sizeof (parent));
1698 if (zp->z_id == parent) {
1709 /* Translate errors and add SAVENAME when needed. */
1710 if (cnp->cn_flags & ISLASTCN) {
1714 if (error == ENOENT) {
1715 error = EJUSTRETURN;
1716 cnp->cn_flags |= SAVENAME;
1722 cnp->cn_flags |= SAVENAME;
1727 /* Insert name into cache (as non-existent) if appropriate. */
1728 if (zfsvfs->z_use_namecache &&
1729 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1730 cache_enter(dvp, NULL, cnp);
1732 /* Insert name into cache if appropriate. */
1733 if (zfsvfs->z_use_namecache &&
1734 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1735 if (!(cnp->cn_flags & ISLASTCN) ||
1736 (nameiop != DELETE && nameiop != RENAME)) {
1737 cache_enter(dvp, *vpp, cnp);
1745 * Attempt to create a new entry in a directory. If the entry
1746 * already exists, truncate the file if permissible, else return
1747 * an error. Return the vp of the created or trunc'd file.
1749 * IN: dvp - vnode of directory to put new file entry in.
1750 * name - name of new file entry.
1751 * vap - attributes of new file.
1752 * excl - flag indicating exclusive or non-exclusive mode.
1753 * mode - mode to open file with.
1754 * cr - credentials of caller.
1755 * flag - large file flag [UNUSED].
1756 * ct - caller context
1757 * vsecp - ACL to be set
1759 * OUT: vpp - vnode of created or trunc'd entry.
1761 * RETURN: 0 on success, error code on failure.
1764 * dvp - ctime|mtime updated if new entry created
1765 * vp - ctime|mtime always, atime if new
1770 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1771 vnode_t **vpp, cred_t *cr, kthread_t *td)
1773 znode_t *zp, *dzp = VTOZ(dvp);
1774 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1781 gid_t gid = crgetgid(cr);
1782 zfs_acl_ids_t acl_ids;
1783 boolean_t fuid_dirtied;
1789 * If we have an ephemeral id, ACL, or XVATTR then
1790 * make sure file system is at proper version
1793 ksid = crgetsid(cr, KSID_OWNER);
1795 uid = ksid_getid(ksid);
1799 if (zfsvfs->z_use_fuids == B_FALSE &&
1800 (vsecp || (vap->va_mask & AT_XVATTR) ||
1801 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1802 return (SET_ERROR(EINVAL));
1807 zilog = zfsvfs->z_log;
1809 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1810 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1812 return (SET_ERROR(EILSEQ));
1815 if (vap->va_mask & AT_XVATTR) {
1816 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1817 crgetuid(cr), cr, vap->va_type)) != 0) {
1825 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1826 vap->va_mode &= ~S_ISVTX;
1828 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1833 ASSERT3P(zp, ==, NULL);
1836 * Create a new file object and update the directory
1839 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1844 * We only support the creation of regular files in
1845 * extended attribute directories.
1848 if ((dzp->z_pflags & ZFS_XATTR) &&
1849 (vap->va_type != VREG)) {
1850 error = SET_ERROR(EINVAL);
1854 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1855 cr, vsecp, &acl_ids)) != 0)
1858 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1859 zfs_acl_ids_free(&acl_ids);
1860 error = SET_ERROR(EDQUOT);
1864 getnewvnode_reserve(1);
1866 tx = dmu_tx_create(os);
1868 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1869 ZFS_SA_BASE_ATTR_SIZE);
1871 fuid_dirtied = zfsvfs->z_fuid_dirty;
1873 zfs_fuid_txhold(zfsvfs, tx);
1874 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1875 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1876 if (!zfsvfs->z_use_sa &&
1877 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1878 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1879 0, acl_ids.z_aclp->z_acl_bytes);
1881 error = dmu_tx_assign(tx, TXG_WAIT);
1883 zfs_acl_ids_free(&acl_ids);
1885 getnewvnode_drop_reserve();
1889 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1892 zfs_fuid_sync(zfsvfs, tx);
1894 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1895 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1896 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1897 vsecp, acl_ids.z_fuidp, vap);
1898 zfs_acl_ids_free(&acl_ids);
1901 getnewvnode_drop_reserve();
1908 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1909 zil_commit(zilog, 0);
1916 * Remove an entry from a directory.
1918 * IN: dvp - vnode of directory to remove entry from.
1919 * name - name of entry to remove.
1920 * cr - credentials of caller.
1921 * ct - caller context
1922 * flags - case flags
1924 * RETURN: 0 on success, error code on failure.
1928 * vp - ctime (if nlink > 0)
1933 zfs_remove(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
1935 znode_t *dzp = VTOZ(dvp);
1936 znode_t *zp = VTOZ(vp);
1938 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1940 uint64_t acl_obj, xattr_obj;
1943 boolean_t unlinked, toobig = FALSE;
1950 zilog = zfsvfs->z_log;
1956 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1961 * Need to use rmdir for removing directories.
1963 if (vp->v_type == VDIR) {
1964 error = SET_ERROR(EPERM);
1968 vnevent_remove(vp, dvp, name, ct);
1972 /* are there any extended attributes? */
1973 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1974 &xattr_obj, sizeof (xattr_obj));
1975 if (error == 0 && xattr_obj) {
1976 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1981 * We may delete the znode now, or we may put it in the unlinked set;
1982 * it depends on whether we're the last link, and on whether there are
1983 * other holds on the vnode. So we dmu_tx_hold() the right things to
1984 * allow for either case.
1986 tx = dmu_tx_create(zfsvfs->z_os);
1987 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1988 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1989 zfs_sa_upgrade_txholds(tx, zp);
1990 zfs_sa_upgrade_txholds(tx, dzp);
1993 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1994 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1997 /* charge as an update -- would be nice not to charge at all */
1998 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2001 * Mark this transaction as typically resulting in a net free of space
2003 dmu_tx_mark_netfree(tx);
2005 error = dmu_tx_assign(tx, TXG_WAIT);
2013 * Remove the directory entry.
2015 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
2023 zfs_unlinked_add(zp, tx);
2024 vp->v_vflag |= VV_NOSYNC;
2028 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2036 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2037 zil_commit(zilog, 0);
2044 * Create a new directory and insert it into dvp using the name
2045 * provided. Return a pointer to the inserted directory.
2047 * IN: dvp - vnode of directory to add subdir to.
2048 * dirname - name of new directory.
2049 * vap - attributes of new directory.
2050 * cr - credentials of caller.
2051 * ct - caller context
2052 * flags - case flags
2053 * vsecp - ACL to be set
2055 * OUT: vpp - vnode of created directory.
2057 * RETURN: 0 on success, error code on failure.
2060 * dvp - ctime|mtime updated
2061 * vp - ctime|mtime|atime updated
2065 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
2067 znode_t *zp, *dzp = VTOZ(dvp);
2068 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2075 gid_t gid = crgetgid(cr);
2076 zfs_acl_ids_t acl_ids;
2077 boolean_t fuid_dirtied;
2079 ASSERT(vap->va_type == VDIR);
2082 * If we have an ephemeral id, ACL, or XVATTR then
2083 * make sure file system is at proper version
2086 ksid = crgetsid(cr, KSID_OWNER);
2088 uid = ksid_getid(ksid);
2091 if (zfsvfs->z_use_fuids == B_FALSE &&
2092 ((vap->va_mask & AT_XVATTR) ||
2093 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2094 return (SET_ERROR(EINVAL));
2098 zilog = zfsvfs->z_log;
2100 if (dzp->z_pflags & ZFS_XATTR) {
2102 return (SET_ERROR(EINVAL));
2105 if (zfsvfs->z_utf8 && u8_validate(dirname,
2106 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2108 return (SET_ERROR(EILSEQ));
2111 if (vap->va_mask & AT_XVATTR) {
2112 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2113 crgetuid(cr), cr, vap->va_type)) != 0) {
2119 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2120 NULL, &acl_ids)) != 0) {
2126 * First make sure the new directory doesn't exist.
2128 * Existence is checked first to make sure we don't return
2129 * EACCES instead of EEXIST which can cause some applications
2134 if (error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW)) {
2135 zfs_acl_ids_free(&acl_ids);
2139 ASSERT3P(zp, ==, NULL);
2141 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2142 zfs_acl_ids_free(&acl_ids);
2147 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2148 zfs_acl_ids_free(&acl_ids);
2150 return (SET_ERROR(EDQUOT));
2154 * Add a new entry to the directory.
2156 getnewvnode_reserve(1);
2157 tx = dmu_tx_create(zfsvfs->z_os);
2158 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2159 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2160 fuid_dirtied = zfsvfs->z_fuid_dirty;
2162 zfs_fuid_txhold(zfsvfs, tx);
2163 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2164 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2165 acl_ids.z_aclp->z_acl_bytes);
2168 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2169 ZFS_SA_BASE_ATTR_SIZE);
2171 error = dmu_tx_assign(tx, TXG_WAIT);
2173 zfs_acl_ids_free(&acl_ids);
2175 getnewvnode_drop_reserve();
2183 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2186 zfs_fuid_sync(zfsvfs, tx);
2189 * Now put new name in parent dir.
2191 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
2195 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
2196 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
2197 acl_ids.z_fuidp, vap);
2199 zfs_acl_ids_free(&acl_ids);
2203 getnewvnode_drop_reserve();
2205 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2206 zil_commit(zilog, 0);
2213 * Remove a directory subdir entry. If the current working
2214 * directory is the same as the subdir to be removed, the
2217 * IN: dvp - vnode of directory to remove from.
2218 * name - name of directory to be removed.
2219 * cwd - vnode of current working directory.
2220 * cr - credentials of caller.
2221 * ct - caller context
2222 * flags - case flags
2224 * RETURN: 0 on success, error code on failure.
2227 * dvp - ctime|mtime updated
2231 zfs_rmdir(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
2233 znode_t *dzp = VTOZ(dvp);
2234 znode_t *zp = VTOZ(vp);
2235 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2243 zilog = zfsvfs->z_log;
2246 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2250 if (vp->v_type != VDIR) {
2251 error = SET_ERROR(ENOTDIR);
2255 vnevent_rmdir(vp, dvp, name, ct);
2257 tx = dmu_tx_create(zfsvfs->z_os);
2258 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2259 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2260 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2261 zfs_sa_upgrade_txholds(tx, zp);
2262 zfs_sa_upgrade_txholds(tx, dzp);
2263 dmu_tx_mark_netfree(tx);
2264 error = dmu_tx_assign(tx, TXG_WAIT);
2273 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
2276 uint64_t txtype = TX_RMDIR;
2277 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2284 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2285 zil_commit(zilog, 0);
2292 * Read as many directory entries as will fit into the provided
2293 * buffer from the given directory cursor position (specified in
2294 * the uio structure).
2296 * IN: vp - vnode of directory to read.
2297 * uio - structure supplying read location, range info,
2298 * and return buffer.
2299 * cr - credentials of caller.
2300 * ct - caller context
2301 * flags - case flags
2303 * OUT: uio - updated offset and range, buffer filled.
2304 * eofp - set to true if end-of-file detected.
2306 * RETURN: 0 on success, error code on failure.
2309 * vp - atime updated
2311 * Note that the low 4 bits of the cookie returned by zap is always zero.
2312 * This allows us to use the low range for "special" directory entries:
2313 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2314 * we use the offset 2 for the '.zfs' directory.
2318 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2320 znode_t *zp = VTOZ(vp);
2324 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2329 zap_attribute_t zap;
2330 uint_t bytes_wanted;
2331 uint64_t offset; /* must be unsigned; checks for < 1 */
2337 boolean_t check_sysattrs;
2340 u_long *cooks = NULL;
2346 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2347 &parent, sizeof (parent))) != 0) {
2353 * If we are not given an eof variable,
2360 * Check for valid iov_len.
2362 if (uio->uio_iov->iov_len <= 0) {
2364 return (SET_ERROR(EINVAL));
2368 * Quit if directory has been removed (posix)
2370 if ((*eofp = zp->z_unlinked) != 0) {
2377 offset = uio->uio_loffset;
2378 prefetch = zp->z_zn_prefetch;
2381 * Initialize the iterator cursor.
2385 * Start iteration from the beginning of the directory.
2387 zap_cursor_init(&zc, os, zp->z_id);
2390 * The offset is a serialized cursor.
2392 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2396 * Get space to change directory entries into fs independent format.
2398 iovp = uio->uio_iov;
2399 bytes_wanted = iovp->iov_len;
2400 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2401 bufsize = bytes_wanted;
2402 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2403 odp = (struct dirent64 *)outbuf;
2405 bufsize = bytes_wanted;
2407 odp = (struct dirent64 *)iovp->iov_base;
2409 eodp = (struct edirent *)odp;
2411 if (ncookies != NULL) {
2413 * Minimum entry size is dirent size and 1 byte for a file name.
2415 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2416 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2421 * If this VFS supports the system attribute view interface; and
2422 * we're looking at an extended attribute directory; and we care
2423 * about normalization conflicts on this vfs; then we must check
2424 * for normalization conflicts with the sysattr name space.
2427 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2428 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2429 (flags & V_RDDIR_ENTFLAGS);
2435 * Transform to file-system independent format
2438 while (outcount < bytes_wanted) {
2441 off64_t *next = NULL;
2444 * Special case `.', `..', and `.zfs'.
2447 (void) strcpy(zap.za_name, ".");
2448 zap.za_normalization_conflict = 0;
2451 } else if (offset == 1) {
2452 (void) strcpy(zap.za_name, "..");
2453 zap.za_normalization_conflict = 0;
2456 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2457 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2458 zap.za_normalization_conflict = 0;
2459 objnum = ZFSCTL_INO_ROOT;
2465 if (error = zap_cursor_retrieve(&zc, &zap)) {
2466 if ((*eofp = (error == ENOENT)) != 0)
2472 if (zap.za_integer_length != 8 ||
2473 zap.za_num_integers != 1) {
2474 cmn_err(CE_WARN, "zap_readdir: bad directory "
2475 "entry, obj = %lld, offset = %lld\n",
2476 (u_longlong_t)zp->z_id,
2477 (u_longlong_t)offset);
2478 error = SET_ERROR(ENXIO);
2482 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2484 * MacOS X can extract the object type here such as:
2485 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2487 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2489 if (check_sysattrs && !zap.za_normalization_conflict) {
2491 zap.za_normalization_conflict =
2492 xattr_sysattr_casechk(zap.za_name);
2494 panic("%s:%u: TODO", __func__, __LINE__);
2499 if (flags & V_RDDIR_ACCFILTER) {
2501 * If we have no access at all, don't include
2502 * this entry in the returned information
2505 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2507 if (!zfs_has_access(ezp, cr)) {
2514 if (flags & V_RDDIR_ENTFLAGS)
2515 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2517 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2520 * Will this entry fit in the buffer?
2522 if (outcount + reclen > bufsize) {
2524 * Did we manage to fit anything in the buffer?
2527 error = SET_ERROR(EINVAL);
2532 if (flags & V_RDDIR_ENTFLAGS) {
2534 * Add extended flag entry:
2536 eodp->ed_ino = objnum;
2537 eodp->ed_reclen = reclen;
2538 /* NOTE: ed_off is the offset for the *next* entry */
2539 next = &(eodp->ed_off);
2540 eodp->ed_eflags = zap.za_normalization_conflict ?
2541 ED_CASE_CONFLICT : 0;
2542 (void) strncpy(eodp->ed_name, zap.za_name,
2543 EDIRENT_NAMELEN(reclen));
2544 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2549 odp->d_ino = objnum;
2550 odp->d_reclen = reclen;
2551 odp->d_namlen = strlen(zap.za_name);
2552 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2554 odp = (dirent64_t *)((intptr_t)odp + reclen);
2558 ASSERT(outcount <= bufsize);
2560 /* Prefetch znode */
2562 dmu_prefetch(os, objnum, 0, 0, 0,
2563 ZIO_PRIORITY_SYNC_READ);
2567 * Move to the next entry, fill in the previous offset.
2569 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2570 zap_cursor_advance(&zc);
2571 offset = zap_cursor_serialize(&zc);
2576 if (cooks != NULL) {
2579 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2582 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2584 /* Subtract unused cookies */
2585 if (ncookies != NULL)
2586 *ncookies -= ncooks;
2588 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2589 iovp->iov_base += outcount;
2590 iovp->iov_len -= outcount;
2591 uio->uio_resid -= outcount;
2592 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2594 * Reset the pointer.
2596 offset = uio->uio_loffset;
2600 zap_cursor_fini(&zc);
2601 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2602 kmem_free(outbuf, bufsize);
2604 if (error == ENOENT)
2607 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2609 uio->uio_loffset = offset;
2611 if (error != 0 && cookies != NULL) {
2612 free(*cookies, M_TEMP);
2619 ulong_t zfs_fsync_sync_cnt = 4;
2622 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2624 znode_t *zp = VTOZ(vp);
2625 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2627 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2629 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2632 zil_commit(zfsvfs->z_log, zp->z_id);
2640 * Get the requested file attributes and place them in the provided
2643 * IN: vp - vnode of file.
2644 * vap - va_mask identifies requested attributes.
2645 * If AT_XVATTR set, then optional attrs are requested
2646 * flags - ATTR_NOACLCHECK (CIFS server context)
2647 * cr - credentials of caller.
2648 * ct - caller context
2650 * OUT: vap - attribute values.
2652 * RETURN: 0 (always succeeds).
2656 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2657 caller_context_t *ct)
2659 znode_t *zp = VTOZ(vp);
2660 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2663 u_longlong_t nblocks;
2665 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2666 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2667 xoptattr_t *xoap = NULL;
2668 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2669 sa_bulk_attr_t bulk[4];
2675 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2677 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2678 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2679 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2680 if (vp->v_type == VBLK || vp->v_type == VCHR)
2681 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2684 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2690 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2691 * Also, if we are the owner don't bother, since owner should
2692 * always be allowed to read basic attributes of file.
2694 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2695 (vap->va_uid != crgetuid(cr))) {
2696 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2704 * Return all attributes. It's cheaper to provide the answer
2705 * than to determine whether we were asked the question.
2708 vap->va_type = IFTOVT(zp->z_mode);
2709 vap->va_mode = zp->z_mode & ~S_IFMT;
2711 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2715 vap->va_nodeid = zp->z_id;
2716 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2717 links = zp->z_links + 1;
2719 links = zp->z_links;
2720 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2721 vap->va_size = zp->z_size;
2723 vap->va_rdev = vp->v_rdev;
2725 if (vp->v_type == VBLK || vp->v_type == VCHR)
2726 vap->va_rdev = zfs_cmpldev(rdev);
2728 vap->va_seq = zp->z_seq;
2729 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2730 vap->va_filerev = zp->z_seq;
2733 * Add in any requested optional attributes and the create time.
2734 * Also set the corresponding bits in the returned attribute bitmap.
2736 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2737 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2739 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2740 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2743 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2744 xoap->xoa_readonly =
2745 ((zp->z_pflags & ZFS_READONLY) != 0);
2746 XVA_SET_RTN(xvap, XAT_READONLY);
2749 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2751 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2752 XVA_SET_RTN(xvap, XAT_SYSTEM);
2755 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2757 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2758 XVA_SET_RTN(xvap, XAT_HIDDEN);
2761 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2762 xoap->xoa_nounlink =
2763 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2764 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2767 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2768 xoap->xoa_immutable =
2769 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2770 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2773 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2774 xoap->xoa_appendonly =
2775 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2776 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2779 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2781 ((zp->z_pflags & ZFS_NODUMP) != 0);
2782 XVA_SET_RTN(xvap, XAT_NODUMP);
2785 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2787 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2788 XVA_SET_RTN(xvap, XAT_OPAQUE);
2791 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2792 xoap->xoa_av_quarantined =
2793 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2794 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2797 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2798 xoap->xoa_av_modified =
2799 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2800 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2803 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2804 vp->v_type == VREG) {
2805 zfs_sa_get_scanstamp(zp, xvap);
2808 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2809 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2810 XVA_SET_RTN(xvap, XAT_REPARSE);
2812 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2813 xoap->xoa_generation = zp->z_gen;
2814 XVA_SET_RTN(xvap, XAT_GEN);
2817 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2819 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2820 XVA_SET_RTN(xvap, XAT_OFFLINE);
2823 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2825 ((zp->z_pflags & ZFS_SPARSE) != 0);
2826 XVA_SET_RTN(xvap, XAT_SPARSE);
2830 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2831 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2832 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2833 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2836 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2837 vap->va_blksize = blksize;
2838 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2840 if (zp->z_blksz == 0) {
2842 * Block size hasn't been set; suggest maximal I/O transfers.
2844 vap->va_blksize = zfsvfs->z_max_blksz;
2852 * Set the file attributes to the values contained in the
2855 * IN: vp - vnode of file to be modified.
2856 * vap - new attribute values.
2857 * If AT_XVATTR set, then optional attrs are being set
2858 * flags - ATTR_UTIME set if non-default time values provided.
2859 * - ATTR_NOACLCHECK (CIFS context only).
2860 * cr - credentials of caller.
2861 * ct - caller context
2863 * RETURN: 0 on success, error code on failure.
2866 * vp - ctime updated, mtime updated if size changed.
2870 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2871 caller_context_t *ct)
2873 znode_t *zp = VTOZ(vp);
2874 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2879 uint_t mask = vap->va_mask;
2880 uint_t saved_mask = 0;
2881 uint64_t saved_mode;
2884 uint64_t new_uid, new_gid;
2886 uint64_t mtime[2], ctime[2];
2888 int need_policy = FALSE;
2890 zfs_fuid_info_t *fuidp = NULL;
2891 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2894 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2895 boolean_t fuid_dirtied = B_FALSE;
2896 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2897 int count = 0, xattr_count = 0;
2902 if (mask & AT_NOSET)
2903 return (SET_ERROR(EINVAL));
2908 zilog = zfsvfs->z_log;
2911 * Make sure that if we have ephemeral uid/gid or xvattr specified
2912 * that file system is at proper version level
2915 if (zfsvfs->z_use_fuids == B_FALSE &&
2916 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2917 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2918 (mask & AT_XVATTR))) {
2920 return (SET_ERROR(EINVAL));
2923 if (mask & AT_SIZE && vp->v_type == VDIR) {
2925 return (SET_ERROR(EISDIR));
2928 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2930 return (SET_ERROR(EINVAL));
2934 * If this is an xvattr_t, then get a pointer to the structure of
2935 * optional attributes. If this is NULL, then we have a vattr_t.
2937 xoap = xva_getxoptattr(xvap);
2939 xva_init(&tmpxvattr);
2942 * Immutable files can only alter immutable bit and atime
2944 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2945 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2946 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2948 return (SET_ERROR(EPERM));
2952 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2956 * Verify timestamps doesn't overflow 32 bits.
2957 * ZFS can handle large timestamps, but 32bit syscalls can't
2958 * handle times greater than 2039. This check should be removed
2959 * once large timestamps are fully supported.
2961 if (mask & (AT_ATIME | AT_MTIME)) {
2962 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2963 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2965 return (SET_ERROR(EOVERFLOW));
2968 if (xoap && (mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2969 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2971 return (SET_ERROR(EOVERFLOW));
2977 /* Can this be moved to before the top label? */
2978 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2980 return (SET_ERROR(EROFS));
2984 * First validate permissions
2987 if (mask & AT_SIZE) {
2989 * XXX - Note, we are not providing any open
2990 * mode flags here (like FNDELAY), so we may
2991 * block if there are locks present... this
2992 * should be addressed in openat().
2994 /* XXX - would it be OK to generate a log record here? */
2995 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3002 if (mask & (AT_ATIME|AT_MTIME) ||
3003 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3004 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3005 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3006 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3007 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3008 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3009 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3010 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3014 if (mask & (AT_UID|AT_GID)) {
3015 int idmask = (mask & (AT_UID|AT_GID));
3020 * NOTE: even if a new mode is being set,
3021 * we may clear S_ISUID/S_ISGID bits.
3024 if (!(mask & AT_MODE))
3025 vap->va_mode = zp->z_mode;
3028 * Take ownership or chgrp to group we are a member of
3031 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3032 take_group = (mask & AT_GID) &&
3033 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3036 * If both AT_UID and AT_GID are set then take_owner and
3037 * take_group must both be set in order to allow taking
3040 * Otherwise, send the check through secpolicy_vnode_setattr()
3044 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3045 ((idmask == AT_UID) && take_owner) ||
3046 ((idmask == AT_GID) && take_group)) {
3047 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3048 skipaclchk, cr) == 0) {
3050 * Remove setuid/setgid for non-privileged users
3052 secpolicy_setid_clear(vap, vp, cr);
3053 trim_mask = (mask & (AT_UID|AT_GID));
3062 oldva.va_mode = zp->z_mode;
3063 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3064 if (mask & AT_XVATTR) {
3066 * Update xvattr mask to include only those attributes
3067 * that are actually changing.
3069 * the bits will be restored prior to actually setting
3070 * the attributes so the caller thinks they were set.
3072 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3073 if (xoap->xoa_appendonly !=
3074 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3077 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3078 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3082 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3083 if (xoap->xoa_nounlink !=
3084 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3087 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3088 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3092 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3093 if (xoap->xoa_immutable !=
3094 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3097 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3098 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3102 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3103 if (xoap->xoa_nodump !=
3104 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3107 XVA_CLR_REQ(xvap, XAT_NODUMP);
3108 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3112 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3113 if (xoap->xoa_av_modified !=
3114 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3117 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3118 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3122 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3123 if ((vp->v_type != VREG &&
3124 xoap->xoa_av_quarantined) ||
3125 xoap->xoa_av_quarantined !=
3126 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3129 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3130 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3134 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3136 return (SET_ERROR(EPERM));
3139 if (need_policy == FALSE &&
3140 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3141 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3146 if (mask & AT_MODE) {
3147 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3148 err = secpolicy_setid_setsticky_clear(vp, vap,
3154 trim_mask |= AT_MODE;
3162 * If trim_mask is set then take ownership
3163 * has been granted or write_acl is present and user
3164 * has the ability to modify mode. In that case remove
3165 * UID|GID and or MODE from mask so that
3166 * secpolicy_vnode_setattr() doesn't revoke it.
3170 saved_mask = vap->va_mask;
3171 vap->va_mask &= ~trim_mask;
3172 if (trim_mask & AT_MODE) {
3174 * Save the mode, as secpolicy_vnode_setattr()
3175 * will overwrite it with ova.va_mode.
3177 saved_mode = vap->va_mode;
3180 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3181 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3188 vap->va_mask |= saved_mask;
3189 if (trim_mask & AT_MODE) {
3191 * Recover the mode after
3192 * secpolicy_vnode_setattr().
3194 vap->va_mode = saved_mode;
3200 * secpolicy_vnode_setattr, or take ownership may have
3203 mask = vap->va_mask;
3205 if ((mask & (AT_UID | AT_GID))) {
3206 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3207 &xattr_obj, sizeof (xattr_obj));
3209 if (err == 0 && xattr_obj) {
3210 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3212 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
3214 vrele(ZTOV(attrzp));
3219 if (mask & AT_UID) {
3220 new_uid = zfs_fuid_create(zfsvfs,
3221 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3222 if (new_uid != zp->z_uid &&
3223 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3226 err = SET_ERROR(EDQUOT);
3231 if (mask & AT_GID) {
3232 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3233 cr, ZFS_GROUP, &fuidp);
3234 if (new_gid != zp->z_gid &&
3235 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3238 err = SET_ERROR(EDQUOT);
3243 tx = dmu_tx_create(zfsvfs->z_os);
3245 if (mask & AT_MODE) {
3246 uint64_t pmode = zp->z_mode;
3248 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3250 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3251 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3252 err = SET_ERROR(EPERM);
3256 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3259 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3261 * Are we upgrading ACL from old V0 format
3264 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3265 zfs_znode_acl_version(zp) ==
3266 ZFS_ACL_VERSION_INITIAL) {
3267 dmu_tx_hold_free(tx, acl_obj, 0,
3269 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3270 0, aclp->z_acl_bytes);
3272 dmu_tx_hold_write(tx, acl_obj, 0,
3275 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3276 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3277 0, aclp->z_acl_bytes);
3279 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3281 if ((mask & AT_XVATTR) &&
3282 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3283 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3285 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3289 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3292 fuid_dirtied = zfsvfs->z_fuid_dirty;
3294 zfs_fuid_txhold(zfsvfs, tx);
3296 zfs_sa_upgrade_txholds(tx, zp);
3298 err = dmu_tx_assign(tx, TXG_WAIT);
3304 * Set each attribute requested.
3305 * We group settings according to the locks they need to acquire.
3307 * Note: you cannot set ctime directly, although it will be
3308 * updated as a side-effect of calling this function.
3311 if (mask & (AT_UID|AT_GID|AT_MODE))
3312 mutex_enter(&zp->z_acl_lock);
3314 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3315 &zp->z_pflags, sizeof (zp->z_pflags));
3318 if (mask & (AT_UID|AT_GID|AT_MODE))
3319 mutex_enter(&attrzp->z_acl_lock);
3320 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3321 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3322 sizeof (attrzp->z_pflags));
3325 if (mask & (AT_UID|AT_GID)) {
3327 if (mask & AT_UID) {
3328 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3329 &new_uid, sizeof (new_uid));
3330 zp->z_uid = new_uid;
3332 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3333 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3335 attrzp->z_uid = new_uid;
3339 if (mask & AT_GID) {
3340 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3341 NULL, &new_gid, sizeof (new_gid));
3342 zp->z_gid = new_gid;
3344 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3345 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3347 attrzp->z_gid = new_gid;
3350 if (!(mask & AT_MODE)) {
3351 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3352 NULL, &new_mode, sizeof (new_mode));
3353 new_mode = zp->z_mode;
3355 err = zfs_acl_chown_setattr(zp);
3358 err = zfs_acl_chown_setattr(attrzp);
3363 if (mask & AT_MODE) {
3364 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3365 &new_mode, sizeof (new_mode));
3366 zp->z_mode = new_mode;
3367 ASSERT3U((uintptr_t)aclp, !=, 0);
3368 err = zfs_aclset_common(zp, aclp, cr, tx);
3370 if (zp->z_acl_cached)
3371 zfs_acl_free(zp->z_acl_cached);
3372 zp->z_acl_cached = aclp;
3377 if (mask & AT_ATIME) {
3378 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3379 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3380 &zp->z_atime, sizeof (zp->z_atime));
3383 if (mask & AT_MTIME) {
3384 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3385 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3386 mtime, sizeof (mtime));
3389 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3390 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3391 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3392 NULL, mtime, sizeof (mtime));
3393 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3394 &ctime, sizeof (ctime));
3395 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3397 } else if (mask != 0) {
3398 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3399 &ctime, sizeof (ctime));
3400 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3403 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3404 SA_ZPL_CTIME(zfsvfs), NULL,
3405 &ctime, sizeof (ctime));
3406 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3407 mtime, ctime, B_TRUE);
3411 * Do this after setting timestamps to prevent timestamp
3412 * update from toggling bit
3415 if (xoap && (mask & AT_XVATTR)) {
3417 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
3418 xoap->xoa_createtime = vap->va_birthtime;
3420 * restore trimmed off masks
3421 * so that return masks can be set for caller.
3424 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3425 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3427 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3428 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3430 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3431 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3433 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3434 XVA_SET_REQ(xvap, XAT_NODUMP);
3436 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3437 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3439 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3440 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3443 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3444 ASSERT(vp->v_type == VREG);
3446 zfs_xvattr_set(zp, xvap, tx);
3450 zfs_fuid_sync(zfsvfs, tx);
3453 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3455 if (mask & (AT_UID|AT_GID|AT_MODE))
3456 mutex_exit(&zp->z_acl_lock);
3459 if (mask & (AT_UID|AT_GID|AT_MODE))
3460 mutex_exit(&attrzp->z_acl_lock);
3463 if (err == 0 && attrzp) {
3464 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3476 zfs_fuid_info_free(fuidp);
3483 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3488 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3489 zil_commit(zilog, 0);
3496 * We acquire all but fdvp locks using non-blocking acquisitions. If we
3497 * fail to acquire any lock in the path we will drop all held locks,
3498 * acquire the new lock in a blocking fashion, and then release it and
3499 * restart the rename. This acquire/release step ensures that we do not
3500 * spin on a lock waiting for release. On error release all vnode locks
3501 * and decrement references the way tmpfs_rename() would do.
3504 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
3505 struct vnode *tdvp, struct vnode **tvpp,
3506 const struct componentname *scnp, const struct componentname *tcnp)
3509 struct vnode *nvp, *svp, *tvp;
3510 znode_t *sdzp, *tdzp, *szp, *tzp;
3511 const char *snm = scnp->cn_nameptr;
3512 const char *tnm = tcnp->cn_nameptr;
3515 VOP_UNLOCK(tdvp, 0);
3516 if (*tvpp != NULL && *tvpp != tdvp)
3517 VOP_UNLOCK(*tvpp, 0);
3520 error = vn_lock(sdvp, LK_EXCLUSIVE);
3525 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
3527 VOP_UNLOCK(sdvp, 0);
3530 error = vn_lock(tdvp, LK_EXCLUSIVE);
3533 VOP_UNLOCK(tdvp, 0);
3539 * Before using sdzp and tdzp we must ensure that they are live.
3540 * As a porting legacy from illumos we have two things to worry
3541 * about. One is typical for FreeBSD and it is that the vnode is
3542 * not reclaimed (doomed). The other is that the znode is live.
3543 * The current code can invalidate the znode without acquiring the
3544 * corresponding vnode lock if the object represented by the znode
3545 * and vnode is no longer valid after a rollback or receive operation.
3546 * z_teardown_lock hidden behind ZFS_ENTER and ZFS_EXIT is the lock
3547 * that protects the znodes from the invalidation.
3549 zfsvfs = sdzp->z_zfsvfs;
3550 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
3554 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3555 * bypassing the cleanup code in the case of an error.
3557 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3559 VOP_UNLOCK(sdvp, 0);
3560 VOP_UNLOCK(tdvp, 0);
3561 error = SET_ERROR(EIO);
3566 * Re-resolve svp to be certain it still exists and fetch the
3569 error = zfs_dirent_lookup(sdzp, snm, &szp, ZEXISTS);
3571 /* Source entry invalid or not there. */
3573 VOP_UNLOCK(sdvp, 0);
3574 VOP_UNLOCK(tdvp, 0);
3575 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
3576 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
3577 error = SET_ERROR(EINVAL);
3583 * Re-resolve tvp, if it disappeared we just carry on.
3585 error = zfs_dirent_lookup(tdzp, tnm, &tzp, 0);
3588 VOP_UNLOCK(sdvp, 0);
3589 VOP_UNLOCK(tdvp, 0);
3591 if ((tcnp->cn_flags & ISDOTDOT) != 0)
3592 error = SET_ERROR(EINVAL);
3601 * At present the vnode locks must be acquired before z_teardown_lock,
3602 * although it would be more logical to use the opposite order.
3607 * Now try acquire locks on svp and tvp.
3610 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3612 VOP_UNLOCK(sdvp, 0);
3613 VOP_UNLOCK(tdvp, 0);
3616 if (error != EBUSY) {
3620 error = vn_lock(nvp, LK_EXCLUSIVE);
3627 * Concurrent rename race.
3632 error = SET_ERROR(EINVAL);
3647 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3649 VOP_UNLOCK(sdvp, 0);
3650 VOP_UNLOCK(tdvp, 0);
3651 VOP_UNLOCK(*svpp, 0);
3652 if (error != EBUSY) {
3656 error = vn_lock(nvp, LK_EXCLUSIVE);
3674 * Note that we must use VRELE_ASYNC in this function as it walks
3675 * up the directory tree and vrele may need to acquire an exclusive
3676 * lock if a last reference to a vnode is dropped.
3679 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3686 zfsvfs = tdzp->z_zfsvfs;
3688 return (SET_ERROR(EINVAL));
3691 if (tdzp->z_id == zfsvfs->z_root)
3695 ASSERT(!zp->z_unlinked);
3696 if ((error = sa_lookup(zp->z_sa_hdl,
3697 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3700 if (parent == szp->z_id) {
3701 error = SET_ERROR(EINVAL);
3704 if (parent == zfsvfs->z_root)
3706 if (parent == sdzp->z_id)
3709 error = zfs_zget(zfsvfs, parent, &zp1);
3714 VN_RELE_ASYNC(ZTOV(zp),
3715 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3719 if (error == ENOTDIR)
3720 panic("checkpath: .. not a directory\n");
3722 VN_RELE_ASYNC(ZTOV(zp),
3723 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3728 * Move an entry from the provided source directory to the target
3729 * directory. Change the entry name as indicated.
3731 * IN: sdvp - Source directory containing the "old entry".
3732 * snm - Old entry name.
3733 * tdvp - Target directory to contain the "new entry".
3734 * tnm - New entry name.
3735 * cr - credentials of caller.
3736 * ct - caller context
3737 * flags - case flags
3739 * RETURN: 0 on success, error code on failure.
3742 * sdvp,tdvp - ctime|mtime updated
3746 zfs_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3747 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3751 znode_t *sdzp, *tdzp, *szp, *tzp;
3752 zilog_t *zilog = NULL;
3754 char *snm = scnp->cn_nameptr;
3755 char *tnm = tcnp->cn_nameptr;
3758 /* Reject renames across filesystems. */
3759 if ((*svpp)->v_mount != tdvp->v_mount ||
3760 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3761 error = SET_ERROR(EXDEV);
3765 if (zfsctl_is_node(tdvp)) {
3766 error = SET_ERROR(EXDEV);
3771 * Lock all four vnodes to ensure safety and semantics of renaming.
3773 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3775 /* no vnodes are locked in the case of error here */
3781 zfsvfs = tdzp->z_zfsvfs;
3782 zilog = zfsvfs->z_log;
3785 * After we re-enter ZFS_ENTER() we will have to revalidate all
3790 if (zfsvfs->z_utf8 && u8_validate(tnm,
3791 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3792 error = SET_ERROR(EILSEQ);
3796 /* If source and target are the same file, there is nothing to do. */
3797 if ((*svpp) == (*tvpp)) {
3802 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3803 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3804 (*tvpp)->v_mountedhere != NULL)) {
3805 error = SET_ERROR(EXDEV);
3810 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3811 * bypassing the cleanup code in the case of an error.
3813 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3814 error = SET_ERROR(EIO);
3819 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3820 if (szp->z_sa_hdl == NULL || (tzp != NULL && tzp->z_sa_hdl == NULL)) {
3821 error = SET_ERROR(EIO);
3826 * This is to prevent the creation of links into attribute space
3827 * by renaming a linked file into/outof an attribute directory.
3828 * See the comment in zfs_link() for why this is considered bad.
3830 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3831 error = SET_ERROR(EINVAL);
3836 * Must have write access at the source to remove the old entry
3837 * and write access at the target to create the new entry.
3838 * Note that if target and source are the same, this can be
3839 * done in a single check.
3841 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3844 if ((*svpp)->v_type == VDIR) {
3846 * Avoid ".", "..", and aliases of "." for obvious reasons.
3848 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3850 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3856 * Check to make sure rename is valid.
3857 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3859 if (error = zfs_rename_check(szp, sdzp, tdzp))
3864 * Does target exist?
3868 * Source and target must be the same type.
3870 if ((*svpp)->v_type == VDIR) {
3871 if ((*tvpp)->v_type != VDIR) {
3872 error = SET_ERROR(ENOTDIR);
3880 if ((*tvpp)->v_type == VDIR) {
3881 error = SET_ERROR(EISDIR);
3887 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3889 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3892 * notify the target directory if it is not the same
3893 * as source directory.
3896 vnevent_rename_dest_dir(tdvp, ct);
3899 tx = dmu_tx_create(zfsvfs->z_os);
3900 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3901 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3902 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3903 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3905 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3906 zfs_sa_upgrade_txholds(tx, tdzp);
3909 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3910 zfs_sa_upgrade_txholds(tx, tzp);
3913 zfs_sa_upgrade_txholds(tx, szp);
3914 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3915 error = dmu_tx_assign(tx, TXG_WAIT);
3922 if (tzp) /* Attempt to remove the existing target */
3923 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3926 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3928 szp->z_pflags |= ZFS_AV_MODIFIED;
3930 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3931 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3934 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3937 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3938 snm, tdzp, tnm, szp);
3941 * Update path information for the target vnode
3943 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
3946 * At this point, we have successfully created
3947 * the target name, but have failed to remove
3948 * the source name. Since the create was done
3949 * with the ZRENAMING flag, there are
3950 * complications; for one, the link count is
3951 * wrong. The easiest way to deal with this
3952 * is to remove the newly created target, and
3953 * return the original error. This must
3954 * succeed; fortunately, it is very unlikely to
3955 * fail, since we just created it.
3957 VERIFY3U(zfs_link_destroy(tdzp, tnm, szp, tx,
3958 ZRENAMING, NULL), ==, 0);
3965 cache_purge_negative(tdvp);
3971 unlockout: /* all 4 vnodes are locked, ZFS_ENTER called */
3973 VOP_UNLOCK(*svpp, 0);
3974 VOP_UNLOCK(sdvp, 0);
3976 out: /* original two vnodes are locked */
3977 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3978 zil_commit(zilog, 0);
3981 VOP_UNLOCK(*tvpp, 0);
3983 VOP_UNLOCK(tdvp, 0);
3988 * Insert the indicated symbolic reference entry into the directory.
3990 * IN: dvp - Directory to contain new symbolic link.
3991 * link - Name for new symlink entry.
3992 * vap - Attributes of new entry.
3993 * cr - credentials of caller.
3994 * ct - caller context
3995 * flags - case flags
3997 * RETURN: 0 on success, error code on failure.
4000 * dvp - ctime|mtime updated
4004 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4005 cred_t *cr, kthread_t *td)
4007 znode_t *zp, *dzp = VTOZ(dvp);
4009 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4011 uint64_t len = strlen(link);
4013 zfs_acl_ids_t acl_ids;
4014 boolean_t fuid_dirtied;
4015 uint64_t txtype = TX_SYMLINK;
4018 ASSERT(vap->va_type == VLNK);
4022 zilog = zfsvfs->z_log;
4024 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4025 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4027 return (SET_ERROR(EILSEQ));
4030 if (len > MAXPATHLEN) {
4032 return (SET_ERROR(ENAMETOOLONG));
4035 if ((error = zfs_acl_ids_create(dzp, 0,
4036 vap, cr, NULL, &acl_ids)) != 0) {
4042 * Attempt to lock directory; fail if entry already exists.
4044 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
4046 zfs_acl_ids_free(&acl_ids);
4051 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4052 zfs_acl_ids_free(&acl_ids);
4057 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4058 zfs_acl_ids_free(&acl_ids);
4060 return (SET_ERROR(EDQUOT));
4063 getnewvnode_reserve(1);
4064 tx = dmu_tx_create(zfsvfs->z_os);
4065 fuid_dirtied = zfsvfs->z_fuid_dirty;
4066 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4067 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4068 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4069 ZFS_SA_BASE_ATTR_SIZE + len);
4070 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4071 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4072 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4073 acl_ids.z_aclp->z_acl_bytes);
4076 zfs_fuid_txhold(zfsvfs, tx);
4077 error = dmu_tx_assign(tx, TXG_WAIT);
4079 zfs_acl_ids_free(&acl_ids);
4081 getnewvnode_drop_reserve();
4087 * Create a new object for the symlink.
4088 * for version 4 ZPL datsets the symlink will be an SA attribute
4090 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4093 zfs_fuid_sync(zfsvfs, tx);
4096 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4099 zfs_sa_symlink(zp, link, len, tx);
4102 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4103 &zp->z_size, sizeof (zp->z_size), tx);
4105 * Insert the new object into the directory.
4107 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
4109 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4112 zfs_acl_ids_free(&acl_ids);
4116 getnewvnode_drop_reserve();
4118 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4119 zil_commit(zilog, 0);
4126 * Return, in the buffer contained in the provided uio structure,
4127 * the symbolic path referred to by vp.
4129 * IN: vp - vnode of symbolic link.
4130 * uio - structure to contain the link path.
4131 * cr - credentials of caller.
4132 * ct - caller context
4134 * OUT: uio - structure containing the link path.
4136 * RETURN: 0 on success, error code on failure.
4139 * vp - atime updated
4143 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4145 znode_t *zp = VTOZ(vp);
4146 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4153 error = sa_lookup_uio(zp->z_sa_hdl,
4154 SA_ZPL_SYMLINK(zfsvfs), uio);
4156 error = zfs_sa_readlink(zp, uio);
4158 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4165 * Insert a new entry into directory tdvp referencing svp.
4167 * IN: tdvp - Directory to contain new entry.
4168 * svp - vnode of new entry.
4169 * name - name of new entry.
4170 * cr - credentials of caller.
4171 * ct - caller context
4173 * RETURN: 0 on success, error code on failure.
4176 * tdvp - ctime|mtime updated
4177 * svp - ctime updated
4181 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4182 caller_context_t *ct, int flags)
4184 znode_t *dzp = VTOZ(tdvp);
4186 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4193 ASSERT(tdvp->v_type == VDIR);
4197 zilog = zfsvfs->z_log;
4200 * POSIX dictates that we return EPERM here.
4201 * Better choices include ENOTSUP or EISDIR.
4203 if (svp->v_type == VDIR) {
4205 return (SET_ERROR(EPERM));
4211 if (szp->z_pflags & (ZFS_APPENDONLY | ZFS_IMMUTABLE | ZFS_READONLY)) {
4213 return (SET_ERROR(EPERM));
4216 /* Prevent links to .zfs/shares files */
4218 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4219 &parent, sizeof (uint64_t))) != 0) {
4223 if (parent == zfsvfs->z_shares_dir) {
4225 return (SET_ERROR(EPERM));
4228 if (zfsvfs->z_utf8 && u8_validate(name,
4229 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4231 return (SET_ERROR(EILSEQ));
4235 * We do not support links between attributes and non-attributes
4236 * because of the potential security risk of creating links
4237 * into "normal" file space in order to circumvent restrictions
4238 * imposed in attribute space.
4240 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4242 return (SET_ERROR(EINVAL));
4246 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4247 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4249 return (SET_ERROR(EPERM));
4252 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4258 * Attempt to lock directory; fail if entry already exists.
4260 error = zfs_dirent_lookup(dzp, name, &tzp, ZNEW);
4266 tx = dmu_tx_create(zfsvfs->z_os);
4267 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4268 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4269 zfs_sa_upgrade_txholds(tx, szp);
4270 zfs_sa_upgrade_txholds(tx, dzp);
4271 error = dmu_tx_assign(tx, TXG_WAIT);
4278 error = zfs_link_create(dzp, name, szp, tx, 0);
4281 uint64_t txtype = TX_LINK;
4282 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4288 vnevent_link(svp, ct);
4291 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4292 zil_commit(zilog, 0);
4301 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4303 znode_t *zp = VTOZ(vp);
4304 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4307 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4308 if (zp->z_sa_hdl == NULL) {
4310 * The fs has been unmounted, or we did a
4311 * suspend/resume and this file no longer exists.
4313 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4318 if (zp->z_unlinked) {
4320 * Fast path to recycle a vnode of a removed file.
4322 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4327 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4328 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4330 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4331 zfs_sa_upgrade_txholds(tx, zp);
4332 error = dmu_tx_assign(tx, TXG_WAIT);
4336 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4337 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4338 zp->z_atime_dirty = 0;
4342 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4346 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
4347 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
4351 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4353 znode_t *zp = VTOZ(vp);
4354 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4357 uint64_t object = zp->z_id;
4364 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4365 &gen64, sizeof (uint64_t))) != 0) {
4370 gen = (uint32_t)gen64;
4372 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4375 if (fidp->fid_len < size) {
4376 fidp->fid_len = size;
4378 return (SET_ERROR(ENOSPC));
4381 fidp->fid_len = size;
4384 zfid = (zfid_short_t *)fidp;
4386 zfid->zf_len = size;
4388 for (i = 0; i < sizeof (zfid->zf_object); i++)
4389 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4391 /* Must have a non-zero generation number to distinguish from .zfs */
4394 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4395 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4397 if (size == LONG_FID_LEN) {
4398 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4401 zlfid = (zfid_long_t *)fidp;
4403 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4404 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4406 /* XXX - this should be the generation number for the objset */
4407 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4408 zlfid->zf_setgen[i] = 0;
4416 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4417 caller_context_t *ct)
4428 case _PC_FILESIZEBITS:
4432 case _PC_XATTR_EXISTS:
4434 zfsvfs = zp->z_zfsvfs;
4438 error = zfs_dirent_lookup(zp, "", &xzp,
4439 ZXATTR | ZEXISTS | ZSHARED);
4441 if (!zfs_dirempty(xzp))
4444 } else if (error == ENOENT) {
4446 * If there aren't extended attributes, it's the
4447 * same as having zero of them.
4454 case _PC_SATTR_ENABLED:
4455 case _PC_SATTR_EXISTS:
4456 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4457 (vp->v_type == VREG || vp->v_type == VDIR);
4460 case _PC_ACCESS_FILTERING:
4461 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4465 case _PC_ACL_ENABLED:
4466 *valp = _ACL_ACE_ENABLED;
4468 #endif /* illumos */
4469 case _PC_MIN_HOLE_SIZE:
4470 *valp = (int)SPA_MINBLOCKSIZE;
4473 case _PC_TIMESTAMP_RESOLUTION:
4474 /* nanosecond timestamp resolution */
4478 case _PC_ACL_EXTENDED:
4486 case _PC_ACL_PATH_MAX:
4487 *valp = ACL_MAX_ENTRIES;
4491 return (EOPNOTSUPP);
4497 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4498 caller_context_t *ct)
4500 znode_t *zp = VTOZ(vp);
4501 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4503 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4507 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4515 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4516 caller_context_t *ct)
4518 znode_t *zp = VTOZ(vp);
4519 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4521 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4522 zilog_t *zilog = zfsvfs->z_log;
4527 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4529 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4530 zil_commit(zilog, 0);
4537 ioflags(int ioflags)
4541 if (ioflags & IO_APPEND)
4543 if (ioflags & IO_NDELAY)
4545 if (ioflags & IO_SYNC)
4546 flags |= (FSYNC | FDSYNC | FRSYNC);
4552 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int *rbehind,
4555 znode_t *zp = VTOZ(vp);
4556 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4557 objset_t *os = zp->z_zfsvfs->z_os;
4562 off_t startoff, endoff;
4564 vm_pindex_t reqstart, reqend;
4567 object = m[0]->object;
4573 zfs_vmobject_wlock(object);
4574 if (m[count - 1]->valid != 0 && --count == 0) {
4575 zfs_vmobject_wunlock(object);
4579 mlast = m[count - 1];
4581 if (IDX_TO_OFF(mlast->pindex) >=
4582 object->un_pager.vnp.vnp_size) {
4583 zfs_vmobject_wunlock(object);
4585 return (zfs_vm_pagerret_bad);
4588 VM_CNT_INC(v_vnodein);
4589 VM_CNT_ADD(v_vnodepgsin, count);
4592 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
4593 lsize = object->un_pager.vnp.vnp_size -
4594 IDX_TO_OFF(mlast->pindex);
4595 zfs_vmobject_wunlock(object);
4597 for (i = 0; i < count; i++) {
4601 va = zfs_map_page(m[i], &sf);
4602 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
4603 size, va, DMU_READ_PREFETCH);
4604 if (size != PAGE_SIZE)
4605 bzero(va + size, PAGE_SIZE - size);
4611 zfs_vmobject_wlock(object);
4612 for (i = 0; i < count; i++)
4613 m[i]->valid = VM_PAGE_BITS_ALL;
4614 zfs_vmobject_wunlock(object);
4617 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4624 return (zfs_vm_pagerret_ok);
4626 return (zfs_vm_pagerret_error);
4630 zfs_freebsd_getpages(ap)
4631 struct vop_getpages_args /* {
4640 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4645 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4648 znode_t *zp = VTOZ(vp);
4649 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4658 vm_ooffset_t lo_off;
4669 object = vp->v_object;
4673 KASSERT(ma[0]->object == object, ("mismatching object"));
4674 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4676 for (i = 0; i < pcount; i++)
4677 rtvals[i] = zfs_vm_pagerret_error;
4679 off = IDX_TO_OFF(ma[0]->pindex);
4680 blksz = zp->z_blksz;
4681 lo_off = rounddown(off, blksz);
4682 lo_len = roundup(len + (off - lo_off), blksz);
4683 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
4685 zfs_vmobject_wlock(object);
4686 if (len + off > object->un_pager.vnp.vnp_size) {
4687 if (object->un_pager.vnp.vnp_size > off) {
4690 len = object->un_pager.vnp.vnp_size - off;
4692 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4694 * If the object is locked and the following
4695 * conditions hold, then the page's dirty
4696 * field cannot be concurrently changed by a
4700 vm_page_assert_sbusied(m);
4701 KASSERT(!pmap_page_is_write_mapped(m),
4702 ("zfs_putpages: page %p is not read-only", m));
4703 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4710 if (ncount < pcount) {
4711 for (i = ncount; i < pcount; i++) {
4712 rtvals[i] = zfs_vm_pagerret_bad;
4716 zfs_vmobject_wunlock(object);
4721 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4722 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4726 tx = dmu_tx_create(zfsvfs->z_os);
4727 dmu_tx_hold_write(tx, zp->z_id, off, len);
4729 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4730 zfs_sa_upgrade_txholds(tx, zp);
4731 err = dmu_tx_assign(tx, TXG_WAIT);
4737 if (zp->z_blksz < PAGE_SIZE) {
4739 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4740 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4741 va = zfs_map_page(ma[i], &sf);
4742 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4746 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4750 uint64_t mtime[2], ctime[2];
4751 sa_bulk_attr_t bulk[3];
4754 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4756 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4758 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4760 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4762 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4764 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4766 zfs_vmobject_wlock(object);
4767 for (i = 0; i < ncount; i++) {
4768 rtvals[i] = zfs_vm_pagerret_ok;
4769 vm_page_undirty(ma[i]);
4771 zfs_vmobject_wunlock(object);
4772 VM_CNT_INC(v_vnodeout);
4773 VM_CNT_ADD(v_vnodepgsout, ncount);
4778 zfs_range_unlock(rl);
4779 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4780 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4781 zil_commit(zfsvfs->z_log, zp->z_id);
4787 zfs_freebsd_putpages(ap)
4788 struct vop_putpages_args /* {
4797 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4802 zfs_freebsd_bmap(ap)
4803 struct vop_bmap_args /* {
4806 struct bufobj **a_bop;
4813 if (ap->a_bop != NULL)
4814 *ap->a_bop = &ap->a_vp->v_bufobj;
4815 if (ap->a_bnp != NULL)
4816 *ap->a_bnp = ap->a_bn;
4817 if (ap->a_runp != NULL)
4819 if (ap->a_runb != NULL)
4826 zfs_freebsd_open(ap)
4827 struct vop_open_args /* {
4830 struct ucred *a_cred;
4831 struct thread *a_td;
4834 vnode_t *vp = ap->a_vp;
4835 znode_t *zp = VTOZ(vp);
4838 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
4840 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4845 zfs_freebsd_close(ap)
4846 struct vop_close_args /* {
4849 struct ucred *a_cred;
4850 struct thread *a_td;
4854 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
4858 zfs_freebsd_ioctl(ap)
4859 struct vop_ioctl_args /* {
4869 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4870 ap->a_fflag, ap->a_cred, NULL, NULL));
4874 zfs_freebsd_read(ap)
4875 struct vop_read_args /* {
4879 struct ucred *a_cred;
4883 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4888 zfs_freebsd_write(ap)
4889 struct vop_write_args /* {
4893 struct ucred *a_cred;
4897 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4902 zfs_freebsd_access(ap)
4903 struct vop_access_args /* {
4905 accmode_t a_accmode;
4906 struct ucred *a_cred;
4907 struct thread *a_td;
4910 vnode_t *vp = ap->a_vp;
4911 znode_t *zp = VTOZ(vp);
4916 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4918 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4920 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
4923 * VADMIN has to be handled by vaccess().
4926 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4928 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4929 zp->z_gid, accmode, ap->a_cred, NULL);
4934 * For VEXEC, ensure that at least one execute bit is set for
4937 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4938 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4946 zfs_freebsd_lookup(ap)
4947 struct vop_lookup_args /* {
4948 struct vnode *a_dvp;
4949 struct vnode **a_vpp;
4950 struct componentname *a_cnp;
4953 struct componentname *cnp = ap->a_cnp;
4954 char nm[NAME_MAX + 1];
4956 ASSERT(cnp->cn_namelen < sizeof(nm));
4957 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
4959 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4960 cnp->cn_cred, cnp->cn_thread, 0));
4964 zfs_cache_lookup(ap)
4965 struct vop_lookup_args /* {
4966 struct vnode *a_dvp;
4967 struct vnode **a_vpp;
4968 struct componentname *a_cnp;
4973 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4974 if (zfsvfs->z_use_namecache)
4975 return (vfs_cache_lookup(ap));
4977 return (zfs_freebsd_lookup(ap));
4981 zfs_freebsd_create(ap)
4982 struct vop_create_args /* {
4983 struct vnode *a_dvp;
4984 struct vnode **a_vpp;
4985 struct componentname *a_cnp;
4986 struct vattr *a_vap;
4990 struct componentname *cnp = ap->a_cnp;
4991 vattr_t *vap = ap->a_vap;
4994 ASSERT(cnp->cn_flags & SAVENAME);
4996 vattr_init_mask(vap);
4997 mode = vap->va_mode & ALLPERMS;
4998 zfsvfs = ap->a_dvp->v_mount->mnt_data;
5000 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
5001 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
5002 if (zfsvfs->z_use_namecache &&
5003 error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
5004 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
5009 zfs_freebsd_remove(ap)
5010 struct vop_remove_args /* {
5011 struct vnode *a_dvp;
5013 struct componentname *a_cnp;
5017 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5019 return (zfs_remove(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
5020 ap->a_cnp->cn_cred));
5024 zfs_freebsd_mkdir(ap)
5025 struct vop_mkdir_args /* {
5026 struct vnode *a_dvp;
5027 struct vnode **a_vpp;
5028 struct componentname *a_cnp;
5029 struct vattr *a_vap;
5032 vattr_t *vap = ap->a_vap;
5034 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5036 vattr_init_mask(vap);
5038 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
5039 ap->a_cnp->cn_cred));
5043 zfs_freebsd_rmdir(ap)
5044 struct vop_rmdir_args /* {
5045 struct vnode *a_dvp;
5047 struct componentname *a_cnp;
5050 struct componentname *cnp = ap->a_cnp;
5052 ASSERT(cnp->cn_flags & SAVENAME);
5054 return (zfs_rmdir(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
5058 zfs_freebsd_readdir(ap)
5059 struct vop_readdir_args /* {
5062 struct ucred *a_cred;
5069 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
5070 ap->a_ncookies, ap->a_cookies));
5074 zfs_freebsd_fsync(ap)
5075 struct vop_fsync_args /* {
5078 struct thread *a_td;
5083 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
5087 zfs_freebsd_getattr(ap)
5088 struct vop_getattr_args /* {
5090 struct vattr *a_vap;
5091 struct ucred *a_cred;
5094 vattr_t *vap = ap->a_vap;
5100 xvap.xva_vattr = *vap;
5101 xvap.xva_vattr.va_mask |= AT_XVATTR;
5103 /* Convert chflags into ZFS-type flags. */
5104 /* XXX: what about SF_SETTABLE?. */
5105 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
5106 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
5107 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
5108 XVA_SET_REQ(&xvap, XAT_NODUMP);
5109 XVA_SET_REQ(&xvap, XAT_READONLY);
5110 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
5111 XVA_SET_REQ(&xvap, XAT_SYSTEM);
5112 XVA_SET_REQ(&xvap, XAT_HIDDEN);
5113 XVA_SET_REQ(&xvap, XAT_REPARSE);
5114 XVA_SET_REQ(&xvap, XAT_OFFLINE);
5115 XVA_SET_REQ(&xvap, XAT_SPARSE);
5117 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
5121 /* Convert ZFS xattr into chflags. */
5122 #define FLAG_CHECK(fflag, xflag, xfield) do { \
5123 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
5124 fflags |= (fflag); \
5126 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
5127 xvap.xva_xoptattrs.xoa_immutable);
5128 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
5129 xvap.xva_xoptattrs.xoa_appendonly);
5130 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
5131 xvap.xva_xoptattrs.xoa_nounlink);
5132 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
5133 xvap.xva_xoptattrs.xoa_archive);
5134 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
5135 xvap.xva_xoptattrs.xoa_nodump);
5136 FLAG_CHECK(UF_READONLY, XAT_READONLY,
5137 xvap.xva_xoptattrs.xoa_readonly);
5138 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
5139 xvap.xva_xoptattrs.xoa_system);
5140 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
5141 xvap.xva_xoptattrs.xoa_hidden);
5142 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
5143 xvap.xva_xoptattrs.xoa_reparse);
5144 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
5145 xvap.xva_xoptattrs.xoa_offline);
5146 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
5147 xvap.xva_xoptattrs.xoa_sparse);
5150 *vap = xvap.xva_vattr;
5151 vap->va_flags = fflags;
5156 zfs_freebsd_setattr(ap)
5157 struct vop_setattr_args /* {
5159 struct vattr *a_vap;
5160 struct ucred *a_cred;
5163 vnode_t *vp = ap->a_vp;
5164 vattr_t *vap = ap->a_vap;
5165 cred_t *cred = ap->a_cred;
5170 vattr_init_mask(vap);
5171 vap->va_mask &= ~AT_NOSET;
5174 xvap.xva_vattr = *vap;
5176 zflags = VTOZ(vp)->z_pflags;
5178 if (vap->va_flags != VNOVAL) {
5179 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
5182 if (zfsvfs->z_use_fuids == B_FALSE)
5183 return (EOPNOTSUPP);
5185 fflags = vap->va_flags;
5188 * We need to figure out whether it makes sense to allow
5189 * UF_REPARSE through, since we don't really have other
5190 * facilities to handle reparse points and zfs_setattr()
5191 * doesn't currently allow setting that attribute anyway.
5193 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
5194 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
5195 UF_OFFLINE|UF_SPARSE)) != 0)
5196 return (EOPNOTSUPP);
5198 * Unprivileged processes are not permitted to unset system
5199 * flags, or modify flags if any system flags are set.
5200 * Privileged non-jail processes may not modify system flags
5201 * if securelevel > 0 and any existing system flags are set.
5202 * Privileged jail processes behave like privileged non-jail
5203 * processes if the security.jail.chflags_allowed sysctl is
5204 * is non-zero; otherwise, they behave like unprivileged
5207 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
5208 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
5210 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5211 error = securelevel_gt(cred, 0);
5217 * Callers may only modify the file flags on objects they
5218 * have VADMIN rights for.
5220 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
5223 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5227 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
5232 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
5233 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
5234 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
5235 XVA_SET_REQ(&xvap, (xflag)); \
5236 (xfield) = ((fflags & (fflag)) != 0); \
5239 /* Convert chflags into ZFS-type flags. */
5240 /* XXX: what about SF_SETTABLE?. */
5241 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
5242 xvap.xva_xoptattrs.xoa_immutable);
5243 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
5244 xvap.xva_xoptattrs.xoa_appendonly);
5245 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
5246 xvap.xva_xoptattrs.xoa_nounlink);
5247 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
5248 xvap.xva_xoptattrs.xoa_archive);
5249 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
5250 xvap.xva_xoptattrs.xoa_nodump);
5251 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
5252 xvap.xva_xoptattrs.xoa_readonly);
5253 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
5254 xvap.xva_xoptattrs.xoa_system);
5255 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
5256 xvap.xva_xoptattrs.xoa_hidden);
5257 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
5258 xvap.xva_xoptattrs.xoa_hidden);
5259 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
5260 xvap.xva_xoptattrs.xoa_offline);
5261 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
5262 xvap.xva_xoptattrs.xoa_sparse);
5265 if (vap->va_birthtime.tv_sec != VNOVAL) {
5266 xvap.xva_vattr.va_mask |= AT_XVATTR;
5267 XVA_SET_REQ(&xvap, XAT_CREATETIME);
5269 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
5273 zfs_freebsd_rename(ap)
5274 struct vop_rename_args /* {
5275 struct vnode *a_fdvp;
5276 struct vnode *a_fvp;
5277 struct componentname *a_fcnp;
5278 struct vnode *a_tdvp;
5279 struct vnode *a_tvp;
5280 struct componentname *a_tcnp;
5283 vnode_t *fdvp = ap->a_fdvp;
5284 vnode_t *fvp = ap->a_fvp;
5285 vnode_t *tdvp = ap->a_tdvp;
5286 vnode_t *tvp = ap->a_tvp;
5289 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
5290 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
5292 error = zfs_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
5293 ap->a_tcnp, ap->a_fcnp->cn_cred);
5305 zfs_freebsd_symlink(ap)
5306 struct vop_symlink_args /* {
5307 struct vnode *a_dvp;
5308 struct vnode **a_vpp;
5309 struct componentname *a_cnp;
5310 struct vattr *a_vap;
5314 struct componentname *cnp = ap->a_cnp;
5315 vattr_t *vap = ap->a_vap;
5317 ASSERT(cnp->cn_flags & SAVENAME);
5319 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
5320 vattr_init_mask(vap);
5322 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
5323 ap->a_target, cnp->cn_cred, cnp->cn_thread));
5327 zfs_freebsd_readlink(ap)
5328 struct vop_readlink_args /* {
5331 struct ucred *a_cred;
5335 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
5339 zfs_freebsd_link(ap)
5340 struct vop_link_args /* {
5341 struct vnode *a_tdvp;
5343 struct componentname *a_cnp;
5346 struct componentname *cnp = ap->a_cnp;
5347 vnode_t *vp = ap->a_vp;
5348 vnode_t *tdvp = ap->a_tdvp;
5350 if (tdvp->v_mount != vp->v_mount)
5353 ASSERT(cnp->cn_flags & SAVENAME);
5355 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
5359 zfs_freebsd_inactive(ap)
5360 struct vop_inactive_args /* {
5362 struct thread *a_td;
5365 vnode_t *vp = ap->a_vp;
5367 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5372 zfs_freebsd_reclaim(ap)
5373 struct vop_reclaim_args /* {
5375 struct thread *a_td;
5378 vnode_t *vp = ap->a_vp;
5379 znode_t *zp = VTOZ(vp);
5380 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5384 /* Destroy the vm object and flush associated pages. */
5385 vnode_destroy_vobject(vp);
5388 * z_teardown_inactive_lock protects from a race with
5389 * zfs_znode_dmu_fini in zfsvfs_teardown during
5392 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
5393 if (zp->z_sa_hdl == NULL)
5397 rw_exit(&zfsvfs->z_teardown_inactive_lock);
5405 struct vop_fid_args /* {
5411 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5415 zfs_freebsd_pathconf(ap)
5416 struct vop_pathconf_args /* {
5419 register_t *a_retval;
5425 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
5427 *ap->a_retval = val;
5428 else if (error == EOPNOTSUPP)
5429 error = vop_stdpathconf(ap);
5434 zfs_freebsd_fifo_pathconf(ap)
5435 struct vop_pathconf_args /* {
5438 register_t *a_retval;
5442 switch (ap->a_name) {
5443 case _PC_ACL_EXTENDED:
5445 case _PC_ACL_PATH_MAX:
5446 case _PC_MAC_PRESENT:
5447 return (zfs_freebsd_pathconf(ap));
5449 return (fifo_specops.vop_pathconf(ap));
5454 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5455 * extended attribute name:
5458 * system freebsd:system:
5459 * user (none, can be used to access ZFS fsattr(5) attributes
5460 * created on Solaris)
5463 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5466 const char *namespace, *prefix, *suffix;
5468 /* We don't allow '/' character in attribute name. */
5469 if (strchr(name, '/') != NULL)
5471 /* We don't allow attribute names that start with "freebsd:" string. */
5472 if (strncmp(name, "freebsd:", 8) == 0)
5475 bzero(attrname, size);
5477 switch (attrnamespace) {
5478 case EXTATTR_NAMESPACE_USER:
5480 prefix = "freebsd:";
5481 namespace = EXTATTR_NAMESPACE_USER_STRING;
5485 * This is the default namespace by which we can access all
5486 * attributes created on Solaris.
5488 prefix = namespace = suffix = "";
5491 case EXTATTR_NAMESPACE_SYSTEM:
5492 prefix = "freebsd:";
5493 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5496 case EXTATTR_NAMESPACE_EMPTY:
5500 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5502 return (ENAMETOOLONG);
5508 * Vnode operating to retrieve a named extended attribute.
5511 zfs_getextattr(struct vop_getextattr_args *ap)
5514 IN struct vnode *a_vp;
5515 IN int a_attrnamespace;
5516 IN const char *a_name;
5517 INOUT struct uio *a_uio;
5519 IN struct ucred *a_cred;
5520 IN struct thread *a_td;
5524 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5525 struct thread *td = ap->a_td;
5526 struct nameidata nd;
5529 vnode_t *xvp = NULL, *vp;
5532 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5533 ap->a_cred, ap->a_td, VREAD);
5537 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5544 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5552 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5554 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
5556 NDFREE(&nd, NDF_ONLY_PNBUF);
5559 if (error == ENOENT)
5564 if (ap->a_size != NULL) {
5565 error = VOP_GETATTR(vp, &va, ap->a_cred);
5567 *ap->a_size = (size_t)va.va_size;
5568 } else if (ap->a_uio != NULL)
5569 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5572 vn_close(vp, flags, ap->a_cred, td);
5579 * Vnode operation to remove a named attribute.
5582 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5585 IN struct vnode *a_vp;
5586 IN int a_attrnamespace;
5587 IN const char *a_name;
5588 IN struct ucred *a_cred;
5589 IN struct thread *a_td;
5593 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5594 struct thread *td = ap->a_td;
5595 struct nameidata nd;
5598 vnode_t *xvp = NULL, *vp;
5601 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5602 ap->a_cred, ap->a_td, VWRITE);
5606 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5613 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5620 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5621 UIO_SYSSPACE, attrname, xvp, td);
5626 NDFREE(&nd, NDF_ONLY_PNBUF);
5627 if (error == ENOENT)
5632 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5633 NDFREE(&nd, NDF_ONLY_PNBUF);
5636 if (vp == nd.ni_dvp)
5646 * Vnode operation to set a named attribute.
5649 zfs_setextattr(struct vop_setextattr_args *ap)
5652 IN struct vnode *a_vp;
5653 IN int a_attrnamespace;
5654 IN const char *a_name;
5655 INOUT struct uio *a_uio;
5656 IN struct ucred *a_cred;
5657 IN struct thread *a_td;
5661 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5662 struct thread *td = ap->a_td;
5663 struct nameidata nd;
5666 vnode_t *xvp = NULL, *vp;
5669 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5670 ap->a_cred, ap->a_td, VWRITE);
5674 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5681 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5682 LOOKUP_XATTR | CREATE_XATTR_DIR);
5688 flags = FFLAGS(O_WRONLY | O_CREAT);
5689 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5691 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
5693 NDFREE(&nd, NDF_ONLY_PNBUF);
5701 error = VOP_SETATTR(vp, &va, ap->a_cred);
5703 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5706 vn_close(vp, flags, ap->a_cred, td);
5713 * Vnode operation to retrieve extended attributes on a vnode.
5716 zfs_listextattr(struct vop_listextattr_args *ap)
5719 IN struct vnode *a_vp;
5720 IN int a_attrnamespace;
5721 INOUT struct uio *a_uio;
5723 IN struct ucred *a_cred;
5724 IN struct thread *a_td;
5728 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5729 struct thread *td = ap->a_td;
5730 struct nameidata nd;
5731 char attrprefix[16];
5732 u_char dirbuf[sizeof(struct dirent)];
5735 struct uio auio, *uio = ap->a_uio;
5736 size_t *sizep = ap->a_size;
5738 vnode_t *xvp = NULL, *vp;
5739 int done, error, eof, pos;
5741 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5742 ap->a_cred, ap->a_td, VREAD);
5746 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5747 sizeof(attrprefix));
5750 plen = strlen(attrprefix);
5757 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5762 * ENOATTR means that the EA directory does not yet exist,
5763 * i.e. there are no extended attributes there.
5765 if (error == ENOATTR)
5770 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5771 UIO_SYSSPACE, ".", xvp, td);
5774 NDFREE(&nd, NDF_ONLY_PNBUF);
5780 auio.uio_iov = &aiov;
5781 auio.uio_iovcnt = 1;
5782 auio.uio_segflg = UIO_SYSSPACE;
5784 auio.uio_rw = UIO_READ;
5785 auio.uio_offset = 0;
5790 aiov.iov_base = (void *)dirbuf;
5791 aiov.iov_len = sizeof(dirbuf);
5792 auio.uio_resid = sizeof(dirbuf);
5793 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5794 done = sizeof(dirbuf) - auio.uio_resid;
5797 for (pos = 0; pos < done;) {
5798 dp = (struct dirent *)(dirbuf + pos);
5799 pos += dp->d_reclen;
5801 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5802 * is what we get when attribute was created on Solaris.
5804 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5806 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
5808 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5810 nlen = dp->d_namlen - plen;
5813 else if (uio != NULL) {
5815 * Format of extattr name entry is one byte for
5816 * length and the rest for name.
5818 error = uiomove(&nlen, 1, uio->uio_rw, uio);
5820 error = uiomove(dp->d_name + plen, nlen,
5827 } while (!eof && error == 0);
5836 zfs_freebsd_getacl(ap)
5837 struct vop_getacl_args /* {
5846 vsecattr_t vsecattr;
5848 if (ap->a_type != ACL_TYPE_NFS4)
5851 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
5852 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
5855 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
5856 if (vsecattr.vsa_aclentp != NULL)
5857 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
5863 zfs_freebsd_setacl(ap)
5864 struct vop_setacl_args /* {
5873 vsecattr_t vsecattr;
5874 int aclbsize; /* size of acl list in bytes */
5877 if (ap->a_type != ACL_TYPE_NFS4)
5880 if (ap->a_aclp == NULL)
5883 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
5887 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
5888 * splitting every entry into two and appending "canonical six"
5889 * entries at the end. Don't allow for setting an ACL that would
5890 * cause chmod(2) to run out of ACL entries.
5892 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
5895 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
5899 vsecattr.vsa_mask = VSA_ACE;
5900 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
5901 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
5902 aaclp = vsecattr.vsa_aclentp;
5903 vsecattr.vsa_aclentsz = aclbsize;
5905 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
5906 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
5907 kmem_free(aaclp, aclbsize);
5913 zfs_freebsd_aclcheck(ap)
5914 struct vop_aclcheck_args /* {
5923 return (EOPNOTSUPP);
5927 zfs_vptocnp(struct vop_vptocnp_args *ap)
5929 vnode_t *covered_vp;
5930 vnode_t *vp = ap->a_vp;;
5931 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
5932 znode_t *zp = VTOZ(vp);
5941 * If we are a snapshot mounted under .zfs, run the operation
5942 * on the covered vnode.
5944 if ((error = sa_lookup(zp->z_sa_hdl,
5945 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) {
5950 if (zp->z_id != parent || zfsvfs->z_parent == zfsvfs) {
5951 char name[MAXNAMLEN + 1];
5955 error = zfs_znode_parent_and_name(zp, &dzp, name);
5958 if (*ap->a_buflen < len)
5959 error = SET_ERROR(ENOMEM);
5962 *ap->a_buflen -= len;
5963 bcopy(name, ap->a_buf + *ap->a_buflen, len);
5964 *ap->a_vpp = ZTOV(dzp);
5971 covered_vp = vp->v_mount->mnt_vnodecovered;
5973 ltype = VOP_ISLOCKED(vp);
5975 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
5977 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
5978 ap->a_buf, ap->a_buflen);
5981 vn_lock(vp, ltype | LK_RETRY);
5982 if ((vp->v_iflag & VI_DOOMED) != 0)
5983 error = SET_ERROR(ENOENT);
5990 struct vop_lock1_args /* {
6001 err = vop_stdlock(ap);
6002 if (err == 0 && (ap->a_flags & LK_NOWAIT) == 0) {
6005 if (vp->v_mount != NULL && (vp->v_iflag & VI_DOOMED) == 0 &&
6006 zp != NULL && (zp->z_pflags & ZFS_XATTR) == 0)
6007 VERIFY(!RRM_LOCK_HELD(&zp->z_zfsvfs->z_teardown_lock));
6013 struct vop_vector zfs_vnodeops;
6014 struct vop_vector zfs_fifoops;
6015 struct vop_vector zfs_shareops;
6017 struct vop_vector zfs_vnodeops = {
6018 .vop_default = &default_vnodeops,
6019 .vop_inactive = zfs_freebsd_inactive,
6020 .vop_reclaim = zfs_freebsd_reclaim,
6021 .vop_access = zfs_freebsd_access,
6022 .vop_lookup = zfs_cache_lookup,
6023 .vop_cachedlookup = zfs_freebsd_lookup,
6024 .vop_getattr = zfs_freebsd_getattr,
6025 .vop_setattr = zfs_freebsd_setattr,
6026 .vop_create = zfs_freebsd_create,
6027 .vop_mknod = zfs_freebsd_create,
6028 .vop_mkdir = zfs_freebsd_mkdir,
6029 .vop_readdir = zfs_freebsd_readdir,
6030 .vop_fsync = zfs_freebsd_fsync,
6031 .vop_open = zfs_freebsd_open,
6032 .vop_close = zfs_freebsd_close,
6033 .vop_rmdir = zfs_freebsd_rmdir,
6034 .vop_ioctl = zfs_freebsd_ioctl,
6035 .vop_link = zfs_freebsd_link,
6036 .vop_symlink = zfs_freebsd_symlink,
6037 .vop_readlink = zfs_freebsd_readlink,
6038 .vop_read = zfs_freebsd_read,
6039 .vop_write = zfs_freebsd_write,
6040 .vop_remove = zfs_freebsd_remove,
6041 .vop_rename = zfs_freebsd_rename,
6042 .vop_pathconf = zfs_freebsd_pathconf,
6043 .vop_bmap = zfs_freebsd_bmap,
6044 .vop_fid = zfs_freebsd_fid,
6045 .vop_getextattr = zfs_getextattr,
6046 .vop_deleteextattr = zfs_deleteextattr,
6047 .vop_setextattr = zfs_setextattr,
6048 .vop_listextattr = zfs_listextattr,
6049 .vop_getacl = zfs_freebsd_getacl,
6050 .vop_setacl = zfs_freebsd_setacl,
6051 .vop_aclcheck = zfs_freebsd_aclcheck,
6052 .vop_getpages = zfs_freebsd_getpages,
6053 .vop_putpages = zfs_freebsd_putpages,
6054 .vop_vptocnp = zfs_vptocnp,
6056 .vop_lock1 = zfs_lock,
6060 struct vop_vector zfs_fifoops = {
6061 .vop_default = &fifo_specops,
6062 .vop_fsync = zfs_freebsd_fsync,
6063 .vop_access = zfs_freebsd_access,
6064 .vop_getattr = zfs_freebsd_getattr,
6065 .vop_inactive = zfs_freebsd_inactive,
6066 .vop_read = VOP_PANIC,
6067 .vop_reclaim = zfs_freebsd_reclaim,
6068 .vop_setattr = zfs_freebsd_setattr,
6069 .vop_write = VOP_PANIC,
6070 .vop_pathconf = zfs_freebsd_fifo_pathconf,
6071 .vop_fid = zfs_freebsd_fid,
6072 .vop_getacl = zfs_freebsd_getacl,
6073 .vop_setacl = zfs_freebsd_setacl,
6074 .vop_aclcheck = zfs_freebsd_aclcheck,
6078 * special share hidden files vnode operations template
6080 struct vop_vector zfs_shareops = {
6081 .vop_default = &default_vnodeops,
6082 .vop_access = zfs_freebsd_access,
6083 .vop_inactive = zfs_freebsd_inactive,
6084 .vop_reclaim = zfs_freebsd_reclaim,
6085 .vop_fid = zfs_freebsd_fid,
6086 .vop_pathconf = zfs_freebsd_pathconf,