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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
28 /* Portions Copyright 2007 Jeremy Teo */
29 /* Portions Copyright 2010 Robert Milkowski */
31 #include <sys/types.h>
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
39 #include <sys/vnode.h>
43 #include <sys/taskq.h>
45 #include <sys/atomic.h>
46 #include <sys/namei.h>
48 #include <sys/cmn_err.h>
49 #include <sys/errno.h>
50 #include <sys/unistd.h>
51 #include <sys/zfs_dir.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/fs/zfs.h>
55 #include <sys/dmu_objset.h>
61 #include <sys/dirent.h>
62 #include <sys/policy.h>
63 #include <sys/sunddi.h>
64 #include <sys/filio.h>
66 #include <sys/zfs_ctldir.h>
67 #include <sys/zfs_fuid.h>
68 #include <sys/zfs_sa.h>
69 #include <sys/zfs_rlock.h>
70 #include <sys/extdirent.h>
71 #include <sys/kidmap.h>
74 #include <sys/sched.h>
76 #include <vm/vm_param.h>
81 * Each vnode op performs some logical unit of work. To do this, the ZPL must
82 * properly lock its in-core state, create a DMU transaction, do the work,
83 * record this work in the intent log (ZIL), commit the DMU transaction,
84 * and wait for the intent log to commit if it is a synchronous operation.
85 * Moreover, the vnode ops must work in both normal and log replay context.
86 * The ordering of events is important to avoid deadlocks and references
87 * to freed memory. The example below illustrates the following Big Rules:
89 * (1) A check must be made in each zfs thread for a mounted file system.
90 * This is done avoiding races using ZFS_ENTER(zfsvfs).
91 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
92 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
93 * can return EIO from the calling function.
95 * (2) VN_RELE() should always be the last thing except for zil_commit()
96 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
97 * First, if it's the last reference, the vnode/znode
98 * can be freed, so the zp may point to freed memory. Second, the last
99 * reference will call zfs_zinactive(), which may induce a lot of work --
100 * pushing cached pages (which acquires range locks) and syncing out
101 * cached atime changes. Third, zfs_zinactive() may require a new tx,
102 * which could deadlock the system if you were already holding one.
103 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
105 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
106 * as they can span dmu_tx_assign() calls.
108 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
109 * dmu_tx_assign(). This is critical because we don't want to block
110 * while holding locks.
112 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
113 * reduces lock contention and CPU usage when we must wait (note that if
114 * throughput is constrained by the storage, nearly every transaction
117 * Note, in particular, that if a lock is sometimes acquired before
118 * the tx assigns, and sometimes after (e.g. z_lock), then failing
119 * to use a non-blocking assign can deadlock the system. The scenario:
121 * Thread A has grabbed a lock before calling dmu_tx_assign().
122 * Thread B is in an already-assigned tx, and blocks for this lock.
123 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
124 * forever, because the previous txg can't quiesce until B's tx commits.
126 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
127 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
128 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
129 * to indicate that this operation has already called dmu_tx_wait().
130 * This will ensure that we don't retry forever, waiting a short bit
133 * (5) If the operation succeeded, generate the intent log entry for it
134 * before dropping locks. This ensures that the ordering of events
135 * in the intent log matches the order in which they actually occurred.
136 * During ZIL replay the zfs_log_* functions will update the sequence
137 * number to indicate the zil transaction has replayed.
139 * (6) At the end of each vnode op, the DMU tx must always commit,
140 * regardless of whether there were any errors.
142 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
143 * to ensure that synchronous semantics are provided when necessary.
145 * In general, this is how things should be ordered in each vnode op:
147 * ZFS_ENTER(zfsvfs); // exit if unmounted
149 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
150 * rw_enter(...); // grab any other locks you need
151 * tx = dmu_tx_create(...); // get DMU tx
152 * dmu_tx_hold_*(); // hold each object you might modify
153 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
155 * rw_exit(...); // drop locks
156 * zfs_dirent_unlock(dl); // unlock directory entry
157 * VN_RELE(...); // release held vnodes
158 * if (error == ERESTART) {
164 * dmu_tx_abort(tx); // abort DMU tx
165 * ZFS_EXIT(zfsvfs); // finished in zfs
166 * return (error); // really out of space
168 * error = do_real_work(); // do whatever this VOP does
170 * zfs_log_*(...); // on success, make ZIL entry
171 * dmu_tx_commit(tx); // commit DMU tx -- error or not
172 * rw_exit(...); // drop locks
173 * zfs_dirent_unlock(dl); // unlock directory entry
174 * VN_RELE(...); // release held vnodes
175 * zil_commit(zilog, foid); // synchronous when necessary
176 * ZFS_EXIT(zfsvfs); // finished in zfs
177 * return (error); // done, report error
182 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
184 znode_t *zp = VTOZ(*vpp);
185 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
190 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
191 ((flag & FAPPEND) == 0)) {
193 return (SET_ERROR(EPERM));
196 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
197 ZTOV(zp)->v_type == VREG &&
198 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
199 if (fs_vscan(*vpp, cr, 0) != 0) {
201 return (SET_ERROR(EACCES));
205 /* Keep a count of the synchronous opens in the znode */
206 if (flag & (FSYNC | FDSYNC))
207 atomic_inc_32(&zp->z_sync_cnt);
215 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
216 caller_context_t *ct)
218 znode_t *zp = VTOZ(vp);
219 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
222 * Clean up any locks held by this process on the vp.
224 cleanlocks(vp, ddi_get_pid(), 0);
225 cleanshares(vp, ddi_get_pid());
230 /* Decrement the synchronous opens in the znode */
231 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
232 atomic_dec_32(&zp->z_sync_cnt);
234 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
235 ZTOV(zp)->v_type == VREG &&
236 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
237 VERIFY(fs_vscan(vp, cr, 1) == 0);
244 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
245 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
248 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
250 znode_t *zp = VTOZ(vp);
251 uint64_t noff = (uint64_t)*off; /* new offset */
256 file_sz = zp->z_size;
257 if (noff >= file_sz) {
258 return (SET_ERROR(ENXIO));
261 if (cmd == _FIO_SEEK_HOLE)
266 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
269 return (SET_ERROR(ENXIO));
272 * We could find a hole that begins after the logical end-of-file,
273 * because dmu_offset_next() only works on whole blocks. If the
274 * EOF falls mid-block, then indicate that the "virtual hole"
275 * at the end of the file begins at the logical EOF, rather than
276 * at the end of the last block.
278 if (noff > file_sz) {
291 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
292 int *rvalp, caller_context_t *ct)
296 dmu_object_info_t doi;
307 * The following two ioctls are used by bfu. Faking out,
308 * necessary to avoid bfu errors.
321 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
322 return (SET_ERROR(EFAULT));
324 off = *(offset_t *)data;
327 zfsvfs = zp->z_zfsvfs;
331 /* offset parameter is in/out */
332 error = zfs_holey(vp, com, &off);
337 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
338 return (SET_ERROR(EFAULT));
340 *(offset_t *)data = off;
345 case _FIO_COUNT_FILLED:
348 * _FIO_COUNT_FILLED adds a new ioctl command which
349 * exposes the number of filled blocks in a
353 zfsvfs = zp->z_zfsvfs;
358 * Wait for all dirty blocks for this object
359 * to get synced out to disk, and the DMU info
362 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
369 * Retrieve fill count from DMU object.
371 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
377 ndata = doi.doi_fill_count;
380 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
381 return (SET_ERROR(EFAULT));
386 return (SET_ERROR(ENOTTY));
390 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
397 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
398 * aligned boundaries, if the range is not aligned. As a result a
399 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
400 * It may happen that all DEV_BSIZE subranges are marked clean and thus
401 * the whole page would be considred clean despite have some dirty data.
402 * For this reason we should shrink the range to DEV_BSIZE aligned
403 * boundaries before calling vm_page_clear_dirty.
405 end = rounddown2(off + nbytes, DEV_BSIZE);
406 off = roundup2(off, DEV_BSIZE);
410 zfs_vmobject_assert_wlocked(obj);
413 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
415 if (vm_page_xbusied(pp)) {
417 * Reference the page before unlocking and
418 * sleeping so that the page daemon is less
419 * likely to reclaim it.
421 vm_page_reference(pp);
423 zfs_vmobject_wunlock(obj);
424 vm_page_busy_sleep(pp, "zfsmwb", true);
425 zfs_vmobject_wlock(obj);
429 } else if (pp != NULL) {
435 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
436 vm_object_pip_add(obj, 1);
437 pmap_remove_write(pp);
439 vm_page_clear_dirty(pp, off, nbytes);
447 page_unbusy(vm_page_t pp)
451 vm_object_pip_subtract(pp->object, 1);
455 page_hold(vnode_t *vp, int64_t start)
461 zfs_vmobject_assert_wlocked(obj);
464 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
466 if (vm_page_xbusied(pp)) {
468 * Reference the page before unlocking and
469 * sleeping so that the page daemon is less
470 * likely to reclaim it.
472 vm_page_reference(pp);
474 zfs_vmobject_wunlock(obj);
475 vm_page_busy_sleep(pp, "zfsmwb", true);
476 zfs_vmobject_wlock(obj);
480 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
493 page_unhold(vm_page_t pp)
502 * When a file is memory mapped, we must keep the IO data synchronized
503 * between the DMU cache and the memory mapped pages. What this means:
505 * On Write: If we find a memory mapped page, we write to *both*
506 * the page and the dmu buffer.
509 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
510 int segflg, dmu_tx_t *tx)
517 ASSERT(segflg != UIO_NOCOPY);
518 ASSERT(vp->v_mount != NULL);
522 off = start & PAGEOFFSET;
523 zfs_vmobject_wlock(obj);
524 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
526 int nbytes = imin(PAGESIZE - off, len);
528 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
529 zfs_vmobject_wunlock(obj);
531 va = zfs_map_page(pp, &sf);
532 (void) dmu_read(os, oid, start+off, nbytes,
533 va+off, DMU_READ_PREFETCH);;
536 zfs_vmobject_wlock(obj);
542 vm_object_pip_wakeupn(obj, 0);
543 zfs_vmobject_wunlock(obj);
547 * Read with UIO_NOCOPY flag means that sendfile(2) requests
548 * ZFS to populate a range of page cache pages with data.
550 * NOTE: this function could be optimized to pre-allocate
551 * all pages in advance, drain exclusive busy on all of them,
552 * map them into contiguous KVA region and populate them
553 * in one single dmu_read() call.
556 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
558 znode_t *zp = VTOZ(vp);
559 objset_t *os = zp->z_zfsvfs->z_os;
569 ASSERT(uio->uio_segflg == UIO_NOCOPY);
570 ASSERT(vp->v_mount != NULL);
573 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
575 zfs_vmobject_wlock(obj);
576 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
577 int bytes = MIN(PAGESIZE, len);
579 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY |
580 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
581 if (pp->valid == 0) {
582 zfs_vmobject_wunlock(obj);
583 va = zfs_map_page(pp, &sf);
584 error = dmu_read(os, zp->z_id, start, bytes, va,
586 if (bytes != PAGESIZE && error == 0)
587 bzero(va + bytes, PAGESIZE - bytes);
589 zfs_vmobject_wlock(obj);
593 if (pp->wire_count == 0 && pp->valid == 0 &&
597 pp->valid = VM_PAGE_BITS_ALL;
598 vm_page_activate(pp);
602 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
607 uio->uio_resid -= bytes;
608 uio->uio_offset += bytes;
611 zfs_vmobject_wunlock(obj);
616 * When a file is memory mapped, we must keep the IO data synchronized
617 * between the DMU cache and the memory mapped pages. What this means:
619 * On Read: We "read" preferentially from memory mapped pages,
620 * else we default from the dmu buffer.
622 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
623 * the file is memory mapped.
626 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
628 znode_t *zp = VTOZ(vp);
636 ASSERT(vp->v_mount != NULL);
640 start = uio->uio_loffset;
641 off = start & PAGEOFFSET;
642 zfs_vmobject_wlock(obj);
643 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
645 uint64_t bytes = MIN(PAGESIZE - off, len);
647 if (pp = page_hold(vp, start)) {
651 zfs_vmobject_wunlock(obj);
652 va = zfs_map_page(pp, &sf);
654 error = uiomove(va + off, bytes, UIO_READ, uio);
656 error = vn_io_fault_uiomove(va + off, bytes, uio);
659 zfs_vmobject_wlock(obj);
662 zfs_vmobject_wunlock(obj);
663 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
665 zfs_vmobject_wlock(obj);
672 zfs_vmobject_wunlock(obj);
676 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
679 * Read bytes from specified file into supplied buffer.
681 * IN: vp - vnode of file to be read from.
682 * uio - structure supplying read location, range info,
684 * ioflag - SYNC flags; used to provide FRSYNC semantics.
685 * cr - credentials of caller.
686 * ct - caller context
688 * OUT: uio - updated offset and range, buffer filled.
690 * RETURN: 0 on success, error code on failure.
693 * vp - atime updated if byte count > 0
697 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
699 znode_t *zp = VTOZ(vp);
700 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
709 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
711 return (SET_ERROR(EACCES));
715 * Validate file offset
717 if (uio->uio_loffset < (offset_t)0) {
719 return (SET_ERROR(EINVAL));
723 * Fasttrack empty reads
725 if (uio->uio_resid == 0) {
731 * Check for mandatory locks
733 if (MANDMODE(zp->z_mode)) {
734 if (error = chklock(vp, FREAD,
735 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
742 * If we're in FRSYNC mode, sync out this znode before reading it.
745 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
746 zil_commit(zfsvfs->z_log, zp->z_id);
749 * Lock the range against changes.
751 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
754 * If we are reading past end-of-file we can skip
755 * to the end; but we might still need to set atime.
757 if (uio->uio_loffset >= zp->z_size) {
762 ASSERT(uio->uio_loffset < zp->z_size);
763 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
766 if ((uio->uio_extflg == UIO_XUIO) &&
767 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
769 int blksz = zp->z_blksz;
770 uint64_t offset = uio->uio_loffset;
772 xuio = (xuio_t *)uio;
774 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
777 ASSERT(offset + n <= blksz);
780 (void) dmu_xuio_init(xuio, nblk);
782 if (vn_has_cached_data(vp)) {
784 * For simplicity, we always allocate a full buffer
785 * even if we only expect to read a portion of a block.
787 while (--nblk >= 0) {
788 (void) dmu_xuio_add(xuio,
789 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
797 nbytes = MIN(n, zfs_read_chunk_size -
798 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
801 if (uio->uio_segflg == UIO_NOCOPY)
802 error = mappedread_sf(vp, nbytes, uio);
804 #endif /* __FreeBSD__ */
805 if (vn_has_cached_data(vp)) {
806 error = mappedread(vp, nbytes, uio);
808 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
812 /* convert checksum errors into IO errors */
814 error = SET_ERROR(EIO);
821 zfs_range_unlock(rl);
823 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
829 * Write the bytes to a file.
831 * IN: vp - vnode of file to be written to.
832 * uio - structure supplying write location, range info,
834 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
835 * set if in append mode.
836 * cr - credentials of caller.
837 * ct - caller context (NFS/CIFS fem monitor only)
839 * OUT: uio - updated offset and range.
841 * RETURN: 0 on success, error code on failure.
844 * vp - ctime|mtime updated if byte count > 0
849 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
851 znode_t *zp = VTOZ(vp);
852 rlim64_t limit = MAXOFFSET_T;
853 ssize_t start_resid = uio->uio_resid;
857 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
862 int max_blksz = zfsvfs->z_max_blksz;
865 iovec_t *aiov = NULL;
868 int iovcnt = uio->uio_iovcnt;
869 iovec_t *iovp = uio->uio_iov;
872 sa_bulk_attr_t bulk[4];
873 uint64_t mtime[2], ctime[2];
876 * Fasttrack empty write
882 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
888 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
889 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
890 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
892 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
896 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
897 * callers might not be able to detect properly that we are read-only,
898 * so check it explicitly here.
900 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
902 return (SET_ERROR(EROFS));
906 * If immutable or not appending then return EPERM
908 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
909 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
910 (uio->uio_loffset < zp->z_size))) {
912 return (SET_ERROR(EPERM));
915 zilog = zfsvfs->z_log;
918 * Validate file offset
920 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
923 return (SET_ERROR(EINVAL));
927 * Check for mandatory locks before calling zfs_range_lock()
928 * in order to prevent a deadlock with locks set via fcntl().
930 if (MANDMODE((mode_t)zp->z_mode) &&
931 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
938 * Pre-fault the pages to ensure slow (eg NFS) pages
940 * Skip this if uio contains loaned arc_buf.
942 if ((uio->uio_extflg == UIO_XUIO) &&
943 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
944 xuio = (xuio_t *)uio;
946 uio_prefaultpages(MIN(n, max_blksz), uio);
950 * If in append mode, set the io offset pointer to eof.
952 if (ioflag & FAPPEND) {
954 * Obtain an appending range lock to guarantee file append
955 * semantics. We reset the write offset once we have the lock.
957 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
959 if (rl->r_len == UINT64_MAX) {
961 * We overlocked the file because this write will cause
962 * the file block size to increase.
963 * Note that zp_size cannot change with this lock held.
967 uio->uio_loffset = woff;
970 * Note that if the file block size will change as a result of
971 * this write, then this range lock will lock the entire file
972 * so that we can re-write the block safely.
974 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
977 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
978 zfs_range_unlock(rl);
984 zfs_range_unlock(rl);
986 return (SET_ERROR(EFBIG));
989 if ((woff + n) > limit || woff > (limit - n))
992 /* Will this write extend the file length? */
993 write_eof = (woff + n > zp->z_size);
995 end_size = MAX(zp->z_size, woff + n);
998 * Write the file in reasonable size chunks. Each chunk is written
999 * in a separate transaction; this keeps the intent log records small
1000 * and allows us to do more fine-grained space accounting.
1004 woff = uio->uio_loffset;
1005 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
1006 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
1008 dmu_return_arcbuf(abuf);
1009 error = SET_ERROR(EDQUOT);
1013 if (xuio && abuf == NULL) {
1014 ASSERT(i_iov < iovcnt);
1015 aiov = &iovp[i_iov];
1016 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1017 dmu_xuio_clear(xuio, i_iov);
1018 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1019 iovec_t *, aiov, arc_buf_t *, abuf);
1020 ASSERT((aiov->iov_base == abuf->b_data) ||
1021 ((char *)aiov->iov_base - (char *)abuf->b_data +
1022 aiov->iov_len == arc_buf_size(abuf)));
1024 } else if (abuf == NULL && n >= max_blksz &&
1025 woff >= zp->z_size &&
1026 P2PHASE(woff, max_blksz) == 0 &&
1027 zp->z_blksz == max_blksz) {
1029 * This write covers a full block. "Borrow" a buffer
1030 * from the dmu so that we can fill it before we enter
1031 * a transaction. This avoids the possibility of
1032 * holding up the transaction if the data copy hangs
1033 * up on a pagefault (e.g., from an NFS server mapping).
1039 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1041 ASSERT(abuf != NULL);
1042 ASSERT(arc_buf_size(abuf) == max_blksz);
1044 if (error = uiocopy(abuf->b_data, max_blksz,
1045 UIO_WRITE, uio, &cbytes)) {
1046 dmu_return_arcbuf(abuf);
1049 ASSERT(cbytes == max_blksz);
1051 ssize_t resid = uio->uio_resid;
1052 error = vn_io_fault_uiomove(abuf->b_data, max_blksz, uio);
1054 uio->uio_offset -= resid - uio->uio_resid;
1055 uio->uio_resid = resid;
1056 dmu_return_arcbuf(abuf);
1063 * Start a transaction.
1065 tx = dmu_tx_create(zfsvfs->z_os);
1066 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1067 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1068 zfs_sa_upgrade_txholds(tx, zp);
1069 error = dmu_tx_assign(tx, TXG_WAIT);
1073 dmu_return_arcbuf(abuf);
1078 * If zfs_range_lock() over-locked we grow the blocksize
1079 * and then reduce the lock range. This will only happen
1080 * on the first iteration since zfs_range_reduce() will
1081 * shrink down r_len to the appropriate size.
1083 if (rl->r_len == UINT64_MAX) {
1086 if (zp->z_blksz > max_blksz) {
1088 * File's blocksize is already larger than the
1089 * "recordsize" property. Only let it grow to
1090 * the next power of 2.
1092 ASSERT(!ISP2(zp->z_blksz));
1093 new_blksz = MIN(end_size,
1094 1 << highbit64(zp->z_blksz));
1096 new_blksz = MIN(end_size, max_blksz);
1098 zfs_grow_blocksize(zp, new_blksz, tx);
1099 zfs_range_reduce(rl, woff, n);
1103 * XXX - should we really limit each write to z_max_blksz?
1104 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1106 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1108 if (woff + nbytes > zp->z_size)
1109 vnode_pager_setsize(vp, woff + nbytes);
1112 tx_bytes = uio->uio_resid;
1113 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1115 tx_bytes -= uio->uio_resid;
1118 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1120 * If this is not a full block write, but we are
1121 * extending the file past EOF and this data starts
1122 * block-aligned, use assign_arcbuf(). Otherwise,
1123 * write via dmu_write().
1125 if (tx_bytes < max_blksz && (!write_eof ||
1126 aiov->iov_base != abuf->b_data)) {
1128 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1129 aiov->iov_len, aiov->iov_base, tx);
1130 dmu_return_arcbuf(abuf);
1131 xuio_stat_wbuf_copied();
1133 ASSERT(xuio || tx_bytes == max_blksz);
1134 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1138 ASSERT(tx_bytes <= uio->uio_resid);
1139 uioskip(uio, tx_bytes);
1142 if (tx_bytes && vn_has_cached_data(vp)) {
1143 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1144 zp->z_id, uio->uio_segflg, tx);
1148 * If we made no progress, we're done. If we made even
1149 * partial progress, update the znode and ZIL accordingly.
1151 if (tx_bytes == 0) {
1152 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1153 (void *)&zp->z_size, sizeof (uint64_t), tx);
1160 * Clear Set-UID/Set-GID bits on successful write if not
1161 * privileged and at least one of the excute bits is set.
1163 * It would be nice to to this after all writes have
1164 * been done, but that would still expose the ISUID/ISGID
1165 * to another app after the partial write is committed.
1167 * Note: we don't call zfs_fuid_map_id() here because
1168 * user 0 is not an ephemeral uid.
1170 mutex_enter(&zp->z_acl_lock);
1171 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1172 (S_IXUSR >> 6))) != 0 &&
1173 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1174 secpolicy_vnode_setid_retain(vp, cr,
1175 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1177 zp->z_mode &= ~(S_ISUID | S_ISGID);
1178 newmode = zp->z_mode;
1179 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1180 (void *)&newmode, sizeof (uint64_t), tx);
1182 mutex_exit(&zp->z_acl_lock);
1184 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1188 * Update the file size (zp_size) if it has changed;
1189 * account for possible concurrent updates.
1191 while ((end_size = zp->z_size) < uio->uio_loffset) {
1192 (void) atomic_cas_64(&zp->z_size, end_size,
1197 ASSERT(error == 0 || error == EFAULT);
1201 * If we are replaying and eof is non zero then force
1202 * the file size to the specified eof. Note, there's no
1203 * concurrency during replay.
1205 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1206 zp->z_size = zfsvfs->z_replay_eof;
1209 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1211 (void) sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1213 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1218 ASSERT(tx_bytes == nbytes);
1223 uio_prefaultpages(MIN(n, max_blksz), uio);
1227 zfs_range_unlock(rl);
1230 * If we're in replay mode, or we made no progress, return error.
1231 * Otherwise, it's at least a partial write, so it's successful.
1233 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1240 * EFAULT means that at least one page of the source buffer was not
1241 * available. VFS will re-try remaining I/O upon this error.
1243 if (error == EFAULT) {
1249 if (ioflag & (FSYNC | FDSYNC) ||
1250 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1251 zil_commit(zilog, zp->z_id);
1258 zfs_get_done(zgd_t *zgd, int error)
1260 znode_t *zp = zgd->zgd_private;
1261 objset_t *os = zp->z_zfsvfs->z_os;
1264 dmu_buf_rele(zgd->zgd_db, zgd);
1266 zfs_range_unlock(zgd->zgd_rl);
1269 * Release the vnode asynchronously as we currently have the
1270 * txg stopped from syncing.
1272 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1274 if (error == 0 && zgd->zgd_bp)
1275 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1277 kmem_free(zgd, sizeof (zgd_t));
1281 static int zil_fault_io = 0;
1285 * Get data to generate a TX_WRITE intent log record.
1288 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1290 zfsvfs_t *zfsvfs = arg;
1291 objset_t *os = zfsvfs->z_os;
1293 uint64_t object = lr->lr_foid;
1294 uint64_t offset = lr->lr_offset;
1295 uint64_t size = lr->lr_length;
1296 blkptr_t *bp = &lr->lr_blkptr;
1301 ASSERT(zio != NULL);
1305 * Nothing to do if the file has been removed
1307 if (zfs_zget(zfsvfs, object, &zp) != 0)
1308 return (SET_ERROR(ENOENT));
1309 if (zp->z_unlinked) {
1311 * Release the vnode asynchronously as we currently have the
1312 * txg stopped from syncing.
1314 VN_RELE_ASYNC(ZTOV(zp),
1315 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1316 return (SET_ERROR(ENOENT));
1319 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1320 zgd->zgd_zilog = zfsvfs->z_log;
1321 zgd->zgd_private = zp;
1324 * Write records come in two flavors: immediate and indirect.
1325 * For small writes it's cheaper to store the data with the
1326 * log record (immediate); for large writes it's cheaper to
1327 * sync the data and get a pointer to it (indirect) so that
1328 * we don't have to write the data twice.
1330 if (buf != NULL) { /* immediate write */
1331 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1332 /* test for truncation needs to be done while range locked */
1333 if (offset >= zp->z_size) {
1334 error = SET_ERROR(ENOENT);
1336 error = dmu_read(os, object, offset, size, buf,
1337 DMU_READ_NO_PREFETCH);
1339 ASSERT(error == 0 || error == ENOENT);
1340 } else { /* indirect write */
1342 * Have to lock the whole block to ensure when it's
1343 * written out and it's checksum is being calculated
1344 * that no one can change the data. We need to re-check
1345 * blocksize after we get the lock in case it's changed!
1350 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1352 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1354 if (zp->z_blksz == size)
1357 zfs_range_unlock(zgd->zgd_rl);
1359 /* test for truncation needs to be done while range locked */
1360 if (lr->lr_offset >= zp->z_size)
1361 error = SET_ERROR(ENOENT);
1364 error = SET_ERROR(EIO);
1369 error = dmu_buf_hold(os, object, offset, zgd, &db,
1370 DMU_READ_NO_PREFETCH);
1373 blkptr_t *obp = dmu_buf_get_blkptr(db);
1375 ASSERT(BP_IS_HOLE(bp));
1382 ASSERT(db->db_offset == offset);
1383 ASSERT(db->db_size == size);
1385 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1387 ASSERT(error || lr->lr_length <= zp->z_blksz);
1390 * On success, we need to wait for the write I/O
1391 * initiated by dmu_sync() to complete before we can
1392 * release this dbuf. We will finish everything up
1393 * in the zfs_get_done() callback.
1398 if (error == EALREADY) {
1399 lr->lr_common.lrc_txtype = TX_WRITE2;
1405 zfs_get_done(zgd, error);
1412 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1413 caller_context_t *ct)
1415 znode_t *zp = VTOZ(vp);
1416 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1422 if (flag & V_ACE_MASK)
1423 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1425 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1432 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1437 error = vn_lock(*vpp, lkflags);
1444 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
1446 znode_t *zdp = VTOZ(dvp);
1447 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1451 ASSERT_VOP_LOCKED(dvp, __func__);
1453 if ((zdp->z_pflags & ZFS_XATTR) == 0)
1454 VERIFY(!RRM_LOCK_HELD(&zfsvfs->z_teardown_lock));
1457 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
1458 ASSERT3P(dvp, ==, vp);
1460 ltype = lkflags & LK_TYPE_MASK;
1461 if (ltype != VOP_ISLOCKED(dvp)) {
1462 if (ltype == LK_EXCLUSIVE)
1463 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1464 else /* if (ltype == LK_SHARED) */
1465 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1468 * Relock for the "." case could leave us with
1471 if (dvp->v_iflag & VI_DOOMED) {
1473 return (SET_ERROR(ENOENT));
1477 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
1479 * Note that in this case, dvp is the child vnode, and we
1480 * are looking up the parent vnode - exactly reverse from
1481 * normal operation. Unlocking dvp requires some rather
1482 * tricky unlock/relock dance to prevent mp from being freed;
1483 * use vn_vget_ino_gen() which takes care of all that.
1485 * XXX Note that there is a time window when both vnodes are
1486 * unlocked. It is possible, although highly unlikely, that
1487 * during that window the parent-child relationship between
1488 * the vnodes may change, for example, get reversed.
1489 * In that case we would have a wrong lock order for the vnodes.
1490 * All other filesystems seem to ignore this problem, so we
1492 * A potential solution could be implemented as follows:
1493 * - using LK_NOWAIT when locking the second vnode and retrying
1495 * - checking that the parent-child relationship still holds
1496 * after locking both vnodes and retrying if it doesn't
1498 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
1501 error = vn_lock(vp, lkflags);
1509 * Lookup an entry in a directory, or an extended attribute directory.
1510 * If it exists, return a held vnode reference for it.
1512 * IN: dvp - vnode of directory to search.
1513 * nm - name of entry to lookup.
1514 * pnp - full pathname to lookup [UNUSED].
1515 * flags - LOOKUP_XATTR set if looking for an attribute.
1516 * rdir - root directory vnode [UNUSED].
1517 * cr - credentials of caller.
1518 * ct - caller context
1520 * OUT: vpp - vnode of located entry, NULL if not found.
1522 * RETURN: 0 on success, error code on failure.
1529 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1530 int nameiop, cred_t *cr, kthread_t *td, int flags)
1532 znode_t *zdp = VTOZ(dvp);
1534 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1537 /* fast path (should be redundant with vfs namecache) */
1538 if (!(flags & LOOKUP_XATTR)) {
1539 if (dvp->v_type != VDIR) {
1540 return (SET_ERROR(ENOTDIR));
1541 } else if (zdp->z_sa_hdl == NULL) {
1542 return (SET_ERROR(EIO));
1546 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1553 if (flags & LOOKUP_XATTR) {
1556 * If the xattr property is off, refuse the lookup request.
1558 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1560 return (SET_ERROR(EINVAL));
1565 * We don't allow recursive attributes..
1566 * Maybe someday we will.
1568 if (zdp->z_pflags & ZFS_XATTR) {
1570 return (SET_ERROR(EINVAL));
1573 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1579 * Do we have permission to get into attribute directory?
1581 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1592 * Check accessibility of directory.
1594 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1599 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1600 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1602 return (SET_ERROR(EILSEQ));
1607 * First handle the special cases.
1609 if ((cnp->cn_flags & ISDOTDOT) != 0) {
1611 * If we are a snapshot mounted under .zfs, return
1612 * the vp for the snapshot directory.
1614 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
1615 struct componentname cn;
1620 ltype = VOP_ISLOCKED(dvp);
1622 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
1625 cn.cn_nameptr = "snapshot";
1626 cn.cn_namelen = strlen(cn.cn_nameptr);
1627 cn.cn_nameiop = cnp->cn_nameiop;
1628 cn.cn_flags = cnp->cn_flags;
1629 cn.cn_lkflags = cnp->cn_lkflags;
1630 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
1633 vn_lock(dvp, ltype | LK_RETRY);
1637 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
1639 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
1640 return (SET_ERROR(ENOTSUP));
1641 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
1646 * The loop is retry the lookup if the parent-child relationship
1647 * changes during the dot-dot locking complexities.
1652 error = zfs_dirlook(zdp, nm, &zp);
1660 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
1663 * If we've got a locking error, then the vnode
1664 * got reclaimed because of a force unmount.
1665 * We never enter doomed vnodes into the name cache.
1671 if ((cnp->cn_flags & ISDOTDOT) == 0)
1675 if (zdp->z_sa_hdl == NULL) {
1676 error = SET_ERROR(EIO);
1678 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1679 &parent, sizeof (parent));
1686 if (zp->z_id == parent) {
1697 /* Translate errors and add SAVENAME when needed. */
1698 if (cnp->cn_flags & ISLASTCN) {
1702 if (error == ENOENT) {
1703 error = EJUSTRETURN;
1704 cnp->cn_flags |= SAVENAME;
1710 cnp->cn_flags |= SAVENAME;
1715 /* Insert name into cache (as non-existent) if appropriate. */
1716 if (zfsvfs->z_use_namecache &&
1717 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1718 cache_enter(dvp, NULL, cnp);
1720 /* Insert name into cache if appropriate. */
1721 if (zfsvfs->z_use_namecache &&
1722 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1723 if (!(cnp->cn_flags & ISLASTCN) ||
1724 (nameiop != DELETE && nameiop != RENAME)) {
1725 cache_enter(dvp, *vpp, cnp);
1733 * Attempt to create a new entry in a directory. If the entry
1734 * already exists, truncate the file if permissible, else return
1735 * an error. Return the vp of the created or trunc'd file.
1737 * IN: dvp - vnode of directory to put new file entry in.
1738 * name - name of new file entry.
1739 * vap - attributes of new file.
1740 * excl - flag indicating exclusive or non-exclusive mode.
1741 * mode - mode to open file with.
1742 * cr - credentials of caller.
1743 * flag - large file flag [UNUSED].
1744 * ct - caller context
1745 * vsecp - ACL to be set
1747 * OUT: vpp - vnode of created or trunc'd entry.
1749 * RETURN: 0 on success, error code on failure.
1752 * dvp - ctime|mtime updated if new entry created
1753 * vp - ctime|mtime always, atime if new
1758 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1759 vnode_t **vpp, cred_t *cr, kthread_t *td)
1761 znode_t *zp, *dzp = VTOZ(dvp);
1762 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1769 gid_t gid = crgetgid(cr);
1770 zfs_acl_ids_t acl_ids;
1771 boolean_t fuid_dirtied;
1777 * If we have an ephemeral id, ACL, or XVATTR then
1778 * make sure file system is at proper version
1781 ksid = crgetsid(cr, KSID_OWNER);
1783 uid = ksid_getid(ksid);
1787 if (zfsvfs->z_use_fuids == B_FALSE &&
1788 (vsecp || (vap->va_mask & AT_XVATTR) ||
1789 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1790 return (SET_ERROR(EINVAL));
1795 zilog = zfsvfs->z_log;
1797 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1798 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1800 return (SET_ERROR(EILSEQ));
1803 if (vap->va_mask & AT_XVATTR) {
1804 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1805 crgetuid(cr), cr, vap->va_type)) != 0) {
1813 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1814 vap->va_mode &= ~S_ISVTX;
1816 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1821 ASSERT3P(zp, ==, NULL);
1824 * Create a new file object and update the directory
1827 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1832 * We only support the creation of regular files in
1833 * extended attribute directories.
1836 if ((dzp->z_pflags & ZFS_XATTR) &&
1837 (vap->va_type != VREG)) {
1838 error = SET_ERROR(EINVAL);
1842 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1843 cr, vsecp, &acl_ids)) != 0)
1846 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1847 zfs_acl_ids_free(&acl_ids);
1848 error = SET_ERROR(EDQUOT);
1852 getnewvnode_reserve(1);
1854 tx = dmu_tx_create(os);
1856 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1857 ZFS_SA_BASE_ATTR_SIZE);
1859 fuid_dirtied = zfsvfs->z_fuid_dirty;
1861 zfs_fuid_txhold(zfsvfs, tx);
1862 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1863 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1864 if (!zfsvfs->z_use_sa &&
1865 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1866 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1867 0, acl_ids.z_aclp->z_acl_bytes);
1869 error = dmu_tx_assign(tx, TXG_WAIT);
1871 zfs_acl_ids_free(&acl_ids);
1873 getnewvnode_drop_reserve();
1877 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1880 zfs_fuid_sync(zfsvfs, tx);
1882 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1883 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1884 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1885 vsecp, acl_ids.z_fuidp, vap);
1886 zfs_acl_ids_free(&acl_ids);
1889 getnewvnode_drop_reserve();
1896 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1897 zil_commit(zilog, 0);
1904 * Remove an entry from a directory.
1906 * IN: dvp - vnode of directory to remove entry from.
1907 * name - name of entry to remove.
1908 * cr - credentials of caller.
1909 * ct - caller context
1910 * flags - case flags
1912 * RETURN: 0 on success, error code on failure.
1916 * vp - ctime (if nlink > 0)
1921 zfs_remove(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
1923 znode_t *dzp = VTOZ(dvp);
1924 znode_t *zp = VTOZ(vp);
1926 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1928 uint64_t acl_obj, xattr_obj;
1931 boolean_t unlinked, toobig = FALSE;
1938 zilog = zfsvfs->z_log;
1944 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1949 * Need to use rmdir for removing directories.
1951 if (vp->v_type == VDIR) {
1952 error = SET_ERROR(EPERM);
1956 vnevent_remove(vp, dvp, name, ct);
1960 /* are there any extended attributes? */
1961 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1962 &xattr_obj, sizeof (xattr_obj));
1963 if (error == 0 && xattr_obj) {
1964 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1969 * We may delete the znode now, or we may put it in the unlinked set;
1970 * it depends on whether we're the last link, and on whether there are
1971 * other holds on the vnode. So we dmu_tx_hold() the right things to
1972 * allow for either case.
1974 tx = dmu_tx_create(zfsvfs->z_os);
1975 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1976 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1977 zfs_sa_upgrade_txholds(tx, zp);
1978 zfs_sa_upgrade_txholds(tx, dzp);
1981 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1982 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1985 /* charge as an update -- would be nice not to charge at all */
1986 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1989 * Mark this transaction as typically resulting in a net free of space
1991 dmu_tx_mark_netfree(tx);
1993 error = dmu_tx_assign(tx, TXG_WAIT);
2001 * Remove the directory entry.
2003 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
2011 zfs_unlinked_add(zp, tx);
2012 vp->v_vflag |= VV_NOSYNC;
2016 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2024 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2025 zil_commit(zilog, 0);
2032 * Create a new directory and insert it into dvp using the name
2033 * provided. Return a pointer to the inserted directory.
2035 * IN: dvp - vnode of directory to add subdir to.
2036 * dirname - name of new directory.
2037 * vap - attributes of new directory.
2038 * cr - credentials of caller.
2039 * ct - caller context
2040 * flags - case flags
2041 * vsecp - ACL to be set
2043 * OUT: vpp - vnode of created directory.
2045 * RETURN: 0 on success, error code on failure.
2048 * dvp - ctime|mtime updated
2049 * vp - ctime|mtime|atime updated
2053 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
2055 znode_t *zp, *dzp = VTOZ(dvp);
2056 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2063 gid_t gid = crgetgid(cr);
2064 zfs_acl_ids_t acl_ids;
2065 boolean_t fuid_dirtied;
2067 ASSERT(vap->va_type == VDIR);
2070 * If we have an ephemeral id, ACL, or XVATTR then
2071 * make sure file system is at proper version
2074 ksid = crgetsid(cr, KSID_OWNER);
2076 uid = ksid_getid(ksid);
2079 if (zfsvfs->z_use_fuids == B_FALSE &&
2080 ((vap->va_mask & AT_XVATTR) ||
2081 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2082 return (SET_ERROR(EINVAL));
2086 zilog = zfsvfs->z_log;
2088 if (dzp->z_pflags & ZFS_XATTR) {
2090 return (SET_ERROR(EINVAL));
2093 if (zfsvfs->z_utf8 && u8_validate(dirname,
2094 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2096 return (SET_ERROR(EILSEQ));
2099 if (vap->va_mask & AT_XVATTR) {
2100 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2101 crgetuid(cr), cr, vap->va_type)) != 0) {
2107 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2108 NULL, &acl_ids)) != 0) {
2114 * First make sure the new directory doesn't exist.
2116 * Existence is checked first to make sure we don't return
2117 * EACCES instead of EEXIST which can cause some applications
2122 if (error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW)) {
2123 zfs_acl_ids_free(&acl_ids);
2127 ASSERT3P(zp, ==, NULL);
2129 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2130 zfs_acl_ids_free(&acl_ids);
2135 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2136 zfs_acl_ids_free(&acl_ids);
2138 return (SET_ERROR(EDQUOT));
2142 * Add a new entry to the directory.
2144 getnewvnode_reserve(1);
2145 tx = dmu_tx_create(zfsvfs->z_os);
2146 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2147 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2148 fuid_dirtied = zfsvfs->z_fuid_dirty;
2150 zfs_fuid_txhold(zfsvfs, tx);
2151 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2152 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2153 acl_ids.z_aclp->z_acl_bytes);
2156 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2157 ZFS_SA_BASE_ATTR_SIZE);
2159 error = dmu_tx_assign(tx, TXG_WAIT);
2161 zfs_acl_ids_free(&acl_ids);
2163 getnewvnode_drop_reserve();
2171 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2174 zfs_fuid_sync(zfsvfs, tx);
2177 * Now put new name in parent dir.
2179 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
2183 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
2184 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
2185 acl_ids.z_fuidp, vap);
2187 zfs_acl_ids_free(&acl_ids);
2191 getnewvnode_drop_reserve();
2193 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2194 zil_commit(zilog, 0);
2201 * Remove a directory subdir entry. If the current working
2202 * directory is the same as the subdir to be removed, the
2205 * IN: dvp - vnode of directory to remove from.
2206 * name - name of directory to be removed.
2207 * cwd - vnode of current working directory.
2208 * cr - credentials of caller.
2209 * ct - caller context
2210 * flags - case flags
2212 * RETURN: 0 on success, error code on failure.
2215 * dvp - ctime|mtime updated
2219 zfs_rmdir(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
2221 znode_t *dzp = VTOZ(dvp);
2222 znode_t *zp = VTOZ(vp);
2223 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2231 zilog = zfsvfs->z_log;
2234 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2238 if (vp->v_type != VDIR) {
2239 error = SET_ERROR(ENOTDIR);
2243 vnevent_rmdir(vp, dvp, name, ct);
2245 tx = dmu_tx_create(zfsvfs->z_os);
2246 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2247 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2248 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2249 zfs_sa_upgrade_txholds(tx, zp);
2250 zfs_sa_upgrade_txholds(tx, dzp);
2251 dmu_tx_mark_netfree(tx);
2252 error = dmu_tx_assign(tx, TXG_WAIT);
2261 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
2264 uint64_t txtype = TX_RMDIR;
2265 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2272 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2273 zil_commit(zilog, 0);
2280 * Read as many directory entries as will fit into the provided
2281 * buffer from the given directory cursor position (specified in
2282 * the uio structure).
2284 * IN: vp - vnode of directory to read.
2285 * uio - structure supplying read location, range info,
2286 * and return buffer.
2287 * cr - credentials of caller.
2288 * ct - caller context
2289 * flags - case flags
2291 * OUT: uio - updated offset and range, buffer filled.
2292 * eofp - set to true if end-of-file detected.
2294 * RETURN: 0 on success, error code on failure.
2297 * vp - atime updated
2299 * Note that the low 4 bits of the cookie returned by zap is always zero.
2300 * This allows us to use the low range for "special" directory entries:
2301 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2302 * we use the offset 2 for the '.zfs' directory.
2306 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2308 znode_t *zp = VTOZ(vp);
2312 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2317 zap_attribute_t zap;
2318 uint_t bytes_wanted;
2319 uint64_t offset; /* must be unsigned; checks for < 1 */
2325 boolean_t check_sysattrs;
2328 u_long *cooks = NULL;
2334 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2335 &parent, sizeof (parent))) != 0) {
2341 * If we are not given an eof variable,
2348 * Check for valid iov_len.
2350 if (uio->uio_iov->iov_len <= 0) {
2352 return (SET_ERROR(EINVAL));
2356 * Quit if directory has been removed (posix)
2358 if ((*eofp = zp->z_unlinked) != 0) {
2365 offset = uio->uio_loffset;
2366 prefetch = zp->z_zn_prefetch;
2369 * Initialize the iterator cursor.
2373 * Start iteration from the beginning of the directory.
2375 zap_cursor_init(&zc, os, zp->z_id);
2378 * The offset is a serialized cursor.
2380 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2384 * Get space to change directory entries into fs independent format.
2386 iovp = uio->uio_iov;
2387 bytes_wanted = iovp->iov_len;
2388 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2389 bufsize = bytes_wanted;
2390 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2391 odp = (struct dirent64 *)outbuf;
2393 bufsize = bytes_wanted;
2395 odp = (struct dirent64 *)iovp->iov_base;
2397 eodp = (struct edirent *)odp;
2399 if (ncookies != NULL) {
2401 * Minimum entry size is dirent size and 1 byte for a file name.
2403 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2404 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2409 * If this VFS supports the system attribute view interface; and
2410 * we're looking at an extended attribute directory; and we care
2411 * about normalization conflicts on this vfs; then we must check
2412 * for normalization conflicts with the sysattr name space.
2415 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2416 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2417 (flags & V_RDDIR_ENTFLAGS);
2423 * Transform to file-system independent format
2426 while (outcount < bytes_wanted) {
2429 off64_t *next = NULL;
2432 * Special case `.', `..', and `.zfs'.
2435 (void) strcpy(zap.za_name, ".");
2436 zap.za_normalization_conflict = 0;
2439 } else if (offset == 1) {
2440 (void) strcpy(zap.za_name, "..");
2441 zap.za_normalization_conflict = 0;
2444 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2445 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2446 zap.za_normalization_conflict = 0;
2447 objnum = ZFSCTL_INO_ROOT;
2453 if (error = zap_cursor_retrieve(&zc, &zap)) {
2454 if ((*eofp = (error == ENOENT)) != 0)
2460 if (zap.za_integer_length != 8 ||
2461 zap.za_num_integers != 1) {
2462 cmn_err(CE_WARN, "zap_readdir: bad directory "
2463 "entry, obj = %lld, offset = %lld\n",
2464 (u_longlong_t)zp->z_id,
2465 (u_longlong_t)offset);
2466 error = SET_ERROR(ENXIO);
2470 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2472 * MacOS X can extract the object type here such as:
2473 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2475 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2477 if (check_sysattrs && !zap.za_normalization_conflict) {
2479 zap.za_normalization_conflict =
2480 xattr_sysattr_casechk(zap.za_name);
2482 panic("%s:%u: TODO", __func__, __LINE__);
2487 if (flags & V_RDDIR_ACCFILTER) {
2489 * If we have no access at all, don't include
2490 * this entry in the returned information
2493 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2495 if (!zfs_has_access(ezp, cr)) {
2502 if (flags & V_RDDIR_ENTFLAGS)
2503 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2505 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2508 * Will this entry fit in the buffer?
2510 if (outcount + reclen > bufsize) {
2512 * Did we manage to fit anything in the buffer?
2515 error = SET_ERROR(EINVAL);
2520 if (flags & V_RDDIR_ENTFLAGS) {
2522 * Add extended flag entry:
2524 eodp->ed_ino = objnum;
2525 eodp->ed_reclen = reclen;
2526 /* NOTE: ed_off is the offset for the *next* entry */
2527 next = &(eodp->ed_off);
2528 eodp->ed_eflags = zap.za_normalization_conflict ?
2529 ED_CASE_CONFLICT : 0;
2530 (void) strncpy(eodp->ed_name, zap.za_name,
2531 EDIRENT_NAMELEN(reclen));
2532 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2537 odp->d_ino = objnum;
2538 odp->d_reclen = reclen;
2539 odp->d_namlen = strlen(zap.za_name);
2540 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2542 odp = (dirent64_t *)((intptr_t)odp + reclen);
2546 ASSERT(outcount <= bufsize);
2548 /* Prefetch znode */
2550 dmu_prefetch(os, objnum, 0, 0, 0,
2551 ZIO_PRIORITY_SYNC_READ);
2555 * Move to the next entry, fill in the previous offset.
2557 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2558 zap_cursor_advance(&zc);
2559 offset = zap_cursor_serialize(&zc);
2564 if (cooks != NULL) {
2567 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2570 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2572 /* Subtract unused cookies */
2573 if (ncookies != NULL)
2574 *ncookies -= ncooks;
2576 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2577 iovp->iov_base += outcount;
2578 iovp->iov_len -= outcount;
2579 uio->uio_resid -= outcount;
2580 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2582 * Reset the pointer.
2584 offset = uio->uio_loffset;
2588 zap_cursor_fini(&zc);
2589 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2590 kmem_free(outbuf, bufsize);
2592 if (error == ENOENT)
2595 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2597 uio->uio_loffset = offset;
2599 if (error != 0 && cookies != NULL) {
2600 free(*cookies, M_TEMP);
2607 ulong_t zfs_fsync_sync_cnt = 4;
2610 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2612 znode_t *zp = VTOZ(vp);
2613 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2615 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2617 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2620 zil_commit(zfsvfs->z_log, zp->z_id);
2628 * Get the requested file attributes and place them in the provided
2631 * IN: vp - vnode of file.
2632 * vap - va_mask identifies requested attributes.
2633 * If AT_XVATTR set, then optional attrs are requested
2634 * flags - ATTR_NOACLCHECK (CIFS server context)
2635 * cr - credentials of caller.
2636 * ct - caller context
2638 * OUT: vap - attribute values.
2640 * RETURN: 0 (always succeeds).
2644 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2645 caller_context_t *ct)
2647 znode_t *zp = VTOZ(vp);
2648 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2651 u_longlong_t nblocks;
2653 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2654 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2655 xoptattr_t *xoap = NULL;
2656 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2657 sa_bulk_attr_t bulk[4];
2663 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2665 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2666 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2667 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2668 if (vp->v_type == VBLK || vp->v_type == VCHR)
2669 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2672 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2678 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2679 * Also, if we are the owner don't bother, since owner should
2680 * always be allowed to read basic attributes of file.
2682 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2683 (vap->va_uid != crgetuid(cr))) {
2684 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2692 * Return all attributes. It's cheaper to provide the answer
2693 * than to determine whether we were asked the question.
2696 vap->va_type = IFTOVT(zp->z_mode);
2697 vap->va_mode = zp->z_mode & ~S_IFMT;
2699 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2701 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2703 vap->va_nodeid = zp->z_id;
2704 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2705 links = zp->z_links + 1;
2707 links = zp->z_links;
2708 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2709 vap->va_size = zp->z_size;
2711 vap->va_rdev = vp->v_rdev;
2713 if (vp->v_type == VBLK || vp->v_type == VCHR)
2714 vap->va_rdev = zfs_cmpldev(rdev);
2716 vap->va_seq = zp->z_seq;
2717 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2718 vap->va_filerev = zp->z_seq;
2721 * Add in any requested optional attributes and the create time.
2722 * Also set the corresponding bits in the returned attribute bitmap.
2724 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2725 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2727 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2728 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2731 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2732 xoap->xoa_readonly =
2733 ((zp->z_pflags & ZFS_READONLY) != 0);
2734 XVA_SET_RTN(xvap, XAT_READONLY);
2737 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2739 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2740 XVA_SET_RTN(xvap, XAT_SYSTEM);
2743 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2745 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2746 XVA_SET_RTN(xvap, XAT_HIDDEN);
2749 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2750 xoap->xoa_nounlink =
2751 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2752 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2755 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2756 xoap->xoa_immutable =
2757 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2758 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2761 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2762 xoap->xoa_appendonly =
2763 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2764 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2767 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2769 ((zp->z_pflags & ZFS_NODUMP) != 0);
2770 XVA_SET_RTN(xvap, XAT_NODUMP);
2773 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2775 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2776 XVA_SET_RTN(xvap, XAT_OPAQUE);
2779 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2780 xoap->xoa_av_quarantined =
2781 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2782 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2785 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2786 xoap->xoa_av_modified =
2787 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2788 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2791 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2792 vp->v_type == VREG) {
2793 zfs_sa_get_scanstamp(zp, xvap);
2796 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2797 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2798 XVA_SET_RTN(xvap, XAT_REPARSE);
2800 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2801 xoap->xoa_generation = zp->z_gen;
2802 XVA_SET_RTN(xvap, XAT_GEN);
2805 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2807 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2808 XVA_SET_RTN(xvap, XAT_OFFLINE);
2811 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2813 ((zp->z_pflags & ZFS_SPARSE) != 0);
2814 XVA_SET_RTN(xvap, XAT_SPARSE);
2818 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2819 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2820 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2821 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2824 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2825 vap->va_blksize = blksize;
2826 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2828 if (zp->z_blksz == 0) {
2830 * Block size hasn't been set; suggest maximal I/O transfers.
2832 vap->va_blksize = zfsvfs->z_max_blksz;
2840 * Set the file attributes to the values contained in the
2843 * IN: vp - vnode of file to be modified.
2844 * vap - new attribute values.
2845 * If AT_XVATTR set, then optional attrs are being set
2846 * flags - ATTR_UTIME set if non-default time values provided.
2847 * - ATTR_NOACLCHECK (CIFS context only).
2848 * cr - credentials of caller.
2849 * ct - caller context
2851 * RETURN: 0 on success, error code on failure.
2854 * vp - ctime updated, mtime updated if size changed.
2858 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2859 caller_context_t *ct)
2861 znode_t *zp = VTOZ(vp);
2862 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2867 uint_t mask = vap->va_mask;
2868 uint_t saved_mask = 0;
2869 uint64_t saved_mode;
2872 uint64_t new_uid, new_gid;
2874 uint64_t mtime[2], ctime[2];
2876 int need_policy = FALSE;
2878 zfs_fuid_info_t *fuidp = NULL;
2879 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2882 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2883 boolean_t fuid_dirtied = B_FALSE;
2884 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2885 int count = 0, xattr_count = 0;
2890 if (mask & AT_NOSET)
2891 return (SET_ERROR(EINVAL));
2896 zilog = zfsvfs->z_log;
2899 * Make sure that if we have ephemeral uid/gid or xvattr specified
2900 * that file system is at proper version level
2903 if (zfsvfs->z_use_fuids == B_FALSE &&
2904 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2905 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2906 (mask & AT_XVATTR))) {
2908 return (SET_ERROR(EINVAL));
2911 if (mask & AT_SIZE && vp->v_type == VDIR) {
2913 return (SET_ERROR(EISDIR));
2916 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2918 return (SET_ERROR(EINVAL));
2922 * If this is an xvattr_t, then get a pointer to the structure of
2923 * optional attributes. If this is NULL, then we have a vattr_t.
2925 xoap = xva_getxoptattr(xvap);
2927 xva_init(&tmpxvattr);
2930 * Immutable files can only alter immutable bit and atime
2932 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2933 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2934 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2936 return (SET_ERROR(EPERM));
2939 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2941 return (SET_ERROR(EPERM));
2945 * Verify timestamps doesn't overflow 32 bits.
2946 * ZFS can handle large timestamps, but 32bit syscalls can't
2947 * handle times greater than 2039. This check should be removed
2948 * once large timestamps are fully supported.
2950 if (mask & (AT_ATIME | AT_MTIME)) {
2951 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2952 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2954 return (SET_ERROR(EOVERFLOW));
2957 if (xoap && (mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2958 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2960 return (SET_ERROR(EOVERFLOW));
2966 /* Can this be moved to before the top label? */
2967 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2969 return (SET_ERROR(EROFS));
2973 * First validate permissions
2976 if (mask & AT_SIZE) {
2978 * XXX - Note, we are not providing any open
2979 * mode flags here (like FNDELAY), so we may
2980 * block if there are locks present... this
2981 * should be addressed in openat().
2983 /* XXX - would it be OK to generate a log record here? */
2984 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2991 if (mask & (AT_ATIME|AT_MTIME) ||
2992 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2993 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2994 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2995 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2996 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2997 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2998 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2999 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3003 if (mask & (AT_UID|AT_GID)) {
3004 int idmask = (mask & (AT_UID|AT_GID));
3009 * NOTE: even if a new mode is being set,
3010 * we may clear S_ISUID/S_ISGID bits.
3013 if (!(mask & AT_MODE))
3014 vap->va_mode = zp->z_mode;
3017 * Take ownership or chgrp to group we are a member of
3020 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3021 take_group = (mask & AT_GID) &&
3022 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3025 * If both AT_UID and AT_GID are set then take_owner and
3026 * take_group must both be set in order to allow taking
3029 * Otherwise, send the check through secpolicy_vnode_setattr()
3033 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3034 ((idmask == AT_UID) && take_owner) ||
3035 ((idmask == AT_GID) && take_group)) {
3036 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3037 skipaclchk, cr) == 0) {
3039 * Remove setuid/setgid for non-privileged users
3041 secpolicy_setid_clear(vap, vp, cr);
3042 trim_mask = (mask & (AT_UID|AT_GID));
3051 oldva.va_mode = zp->z_mode;
3052 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3053 if (mask & AT_XVATTR) {
3055 * Update xvattr mask to include only those attributes
3056 * that are actually changing.
3058 * the bits will be restored prior to actually setting
3059 * the attributes so the caller thinks they were set.
3061 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3062 if (xoap->xoa_appendonly !=
3063 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3066 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3067 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3071 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3072 if (xoap->xoa_nounlink !=
3073 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3076 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3077 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3081 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3082 if (xoap->xoa_immutable !=
3083 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3086 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3087 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3091 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3092 if (xoap->xoa_nodump !=
3093 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3096 XVA_CLR_REQ(xvap, XAT_NODUMP);
3097 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3101 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3102 if (xoap->xoa_av_modified !=
3103 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3106 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3107 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3111 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3112 if ((vp->v_type != VREG &&
3113 xoap->xoa_av_quarantined) ||
3114 xoap->xoa_av_quarantined !=
3115 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3118 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3119 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3123 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3125 return (SET_ERROR(EPERM));
3128 if (need_policy == FALSE &&
3129 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3130 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3135 if (mask & AT_MODE) {
3136 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3137 err = secpolicy_setid_setsticky_clear(vp, vap,
3143 trim_mask |= AT_MODE;
3151 * If trim_mask is set then take ownership
3152 * has been granted or write_acl is present and user
3153 * has the ability to modify mode. In that case remove
3154 * UID|GID and or MODE from mask so that
3155 * secpolicy_vnode_setattr() doesn't revoke it.
3159 saved_mask = vap->va_mask;
3160 vap->va_mask &= ~trim_mask;
3161 if (trim_mask & AT_MODE) {
3163 * Save the mode, as secpolicy_vnode_setattr()
3164 * will overwrite it with ova.va_mode.
3166 saved_mode = vap->va_mode;
3169 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3170 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3177 vap->va_mask |= saved_mask;
3178 if (trim_mask & AT_MODE) {
3180 * Recover the mode after
3181 * secpolicy_vnode_setattr().
3183 vap->va_mode = saved_mode;
3189 * secpolicy_vnode_setattr, or take ownership may have
3192 mask = vap->va_mask;
3194 if ((mask & (AT_UID | AT_GID))) {
3195 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3196 &xattr_obj, sizeof (xattr_obj));
3198 if (err == 0 && xattr_obj) {
3199 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3201 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
3203 vrele(ZTOV(attrzp));
3208 if (mask & AT_UID) {
3209 new_uid = zfs_fuid_create(zfsvfs,
3210 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3211 if (new_uid != zp->z_uid &&
3212 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3215 err = SET_ERROR(EDQUOT);
3220 if (mask & AT_GID) {
3221 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3222 cr, ZFS_GROUP, &fuidp);
3223 if (new_gid != zp->z_gid &&
3224 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3227 err = SET_ERROR(EDQUOT);
3232 tx = dmu_tx_create(zfsvfs->z_os);
3234 if (mask & AT_MODE) {
3235 uint64_t pmode = zp->z_mode;
3237 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3239 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3240 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3241 err = SET_ERROR(EPERM);
3245 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3248 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3250 * Are we upgrading ACL from old V0 format
3253 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3254 zfs_znode_acl_version(zp) ==
3255 ZFS_ACL_VERSION_INITIAL) {
3256 dmu_tx_hold_free(tx, acl_obj, 0,
3258 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3259 0, aclp->z_acl_bytes);
3261 dmu_tx_hold_write(tx, acl_obj, 0,
3264 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3265 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3266 0, aclp->z_acl_bytes);
3268 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3270 if ((mask & AT_XVATTR) &&
3271 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3272 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3274 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3278 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3281 fuid_dirtied = zfsvfs->z_fuid_dirty;
3283 zfs_fuid_txhold(zfsvfs, tx);
3285 zfs_sa_upgrade_txholds(tx, zp);
3287 err = dmu_tx_assign(tx, TXG_WAIT);
3293 * Set each attribute requested.
3294 * We group settings according to the locks they need to acquire.
3296 * Note: you cannot set ctime directly, although it will be
3297 * updated as a side-effect of calling this function.
3300 if (mask & (AT_UID|AT_GID|AT_MODE))
3301 mutex_enter(&zp->z_acl_lock);
3303 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3304 &zp->z_pflags, sizeof (zp->z_pflags));
3307 if (mask & (AT_UID|AT_GID|AT_MODE))
3308 mutex_enter(&attrzp->z_acl_lock);
3309 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3310 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3311 sizeof (attrzp->z_pflags));
3314 if (mask & (AT_UID|AT_GID)) {
3316 if (mask & AT_UID) {
3317 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3318 &new_uid, sizeof (new_uid));
3319 zp->z_uid = new_uid;
3321 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3322 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3324 attrzp->z_uid = new_uid;
3328 if (mask & AT_GID) {
3329 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3330 NULL, &new_gid, sizeof (new_gid));
3331 zp->z_gid = new_gid;
3333 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3334 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3336 attrzp->z_gid = new_gid;
3339 if (!(mask & AT_MODE)) {
3340 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3341 NULL, &new_mode, sizeof (new_mode));
3342 new_mode = zp->z_mode;
3344 err = zfs_acl_chown_setattr(zp);
3347 err = zfs_acl_chown_setattr(attrzp);
3352 if (mask & AT_MODE) {
3353 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3354 &new_mode, sizeof (new_mode));
3355 zp->z_mode = new_mode;
3356 ASSERT3U((uintptr_t)aclp, !=, 0);
3357 err = zfs_aclset_common(zp, aclp, cr, tx);
3359 if (zp->z_acl_cached)
3360 zfs_acl_free(zp->z_acl_cached);
3361 zp->z_acl_cached = aclp;
3366 if (mask & AT_ATIME) {
3367 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3368 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3369 &zp->z_atime, sizeof (zp->z_atime));
3372 if (mask & AT_MTIME) {
3373 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3374 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3375 mtime, sizeof (mtime));
3378 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3379 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3380 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3381 NULL, mtime, sizeof (mtime));
3382 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3383 &ctime, sizeof (ctime));
3384 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3386 } else if (mask != 0) {
3387 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3388 &ctime, sizeof (ctime));
3389 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3392 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3393 SA_ZPL_CTIME(zfsvfs), NULL,
3394 &ctime, sizeof (ctime));
3395 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3396 mtime, ctime, B_TRUE);
3400 * Do this after setting timestamps to prevent timestamp
3401 * update from toggling bit
3404 if (xoap && (mask & AT_XVATTR)) {
3406 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
3407 xoap->xoa_createtime = vap->va_birthtime;
3409 * restore trimmed off masks
3410 * so that return masks can be set for caller.
3413 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3414 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3416 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3417 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3419 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3420 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3422 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3423 XVA_SET_REQ(xvap, XAT_NODUMP);
3425 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3426 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3428 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3429 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3432 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3433 ASSERT(vp->v_type == VREG);
3435 zfs_xvattr_set(zp, xvap, tx);
3439 zfs_fuid_sync(zfsvfs, tx);
3442 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3444 if (mask & (AT_UID|AT_GID|AT_MODE))
3445 mutex_exit(&zp->z_acl_lock);
3448 if (mask & (AT_UID|AT_GID|AT_MODE))
3449 mutex_exit(&attrzp->z_acl_lock);
3452 if (err == 0 && attrzp) {
3453 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3465 zfs_fuid_info_free(fuidp);
3472 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3477 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3478 zil_commit(zilog, 0);
3485 * We acquire all but fdvp locks using non-blocking acquisitions. If we
3486 * fail to acquire any lock in the path we will drop all held locks,
3487 * acquire the new lock in a blocking fashion, and then release it and
3488 * restart the rename. This acquire/release step ensures that we do not
3489 * spin on a lock waiting for release. On error release all vnode locks
3490 * and decrement references the way tmpfs_rename() would do.
3493 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
3494 struct vnode *tdvp, struct vnode **tvpp,
3495 const struct componentname *scnp, const struct componentname *tcnp)
3498 struct vnode *nvp, *svp, *tvp;
3499 znode_t *sdzp, *tdzp, *szp, *tzp;
3500 const char *snm = scnp->cn_nameptr;
3501 const char *tnm = tcnp->cn_nameptr;
3504 VOP_UNLOCK(tdvp, 0);
3505 if (*tvpp != NULL && *tvpp != tdvp)
3506 VOP_UNLOCK(*tvpp, 0);
3509 error = vn_lock(sdvp, LK_EXCLUSIVE);
3514 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
3516 VOP_UNLOCK(sdvp, 0);
3519 error = vn_lock(tdvp, LK_EXCLUSIVE);
3522 VOP_UNLOCK(tdvp, 0);
3528 * Before using sdzp and tdzp we must ensure that they are live.
3529 * As a porting legacy from illumos we have two things to worry
3530 * about. One is typical for FreeBSD and it is that the vnode is
3531 * not reclaimed (doomed). The other is that the znode is live.
3532 * The current code can invalidate the znode without acquiring the
3533 * corresponding vnode lock if the object represented by the znode
3534 * and vnode is no longer valid after a rollback or receive operation.
3535 * z_teardown_lock hidden behind ZFS_ENTER and ZFS_EXIT is the lock
3536 * that protects the znodes from the invalidation.
3538 zfsvfs = sdzp->z_zfsvfs;
3539 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
3543 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3544 * bypassing the cleanup code in the case of an error.
3546 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3548 VOP_UNLOCK(sdvp, 0);
3549 VOP_UNLOCK(tdvp, 0);
3550 error = SET_ERROR(EIO);
3555 * Re-resolve svp to be certain it still exists and fetch the
3558 error = zfs_dirent_lookup(sdzp, snm, &szp, ZEXISTS);
3560 /* Source entry invalid or not there. */
3562 VOP_UNLOCK(sdvp, 0);
3563 VOP_UNLOCK(tdvp, 0);
3564 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
3565 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
3566 error = SET_ERROR(EINVAL);
3572 * Re-resolve tvp, if it disappeared we just carry on.
3574 error = zfs_dirent_lookup(tdzp, tnm, &tzp, 0);
3577 VOP_UNLOCK(sdvp, 0);
3578 VOP_UNLOCK(tdvp, 0);
3580 if ((tcnp->cn_flags & ISDOTDOT) != 0)
3581 error = SET_ERROR(EINVAL);
3590 * At present the vnode locks must be acquired before z_teardown_lock,
3591 * although it would be more logical to use the opposite order.
3596 * Now try acquire locks on svp and tvp.
3599 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3601 VOP_UNLOCK(sdvp, 0);
3602 VOP_UNLOCK(tdvp, 0);
3605 if (error != EBUSY) {
3609 error = vn_lock(nvp, LK_EXCLUSIVE);
3616 * Concurrent rename race.
3621 error = SET_ERROR(EINVAL);
3636 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3638 VOP_UNLOCK(sdvp, 0);
3639 VOP_UNLOCK(tdvp, 0);
3640 VOP_UNLOCK(*svpp, 0);
3641 if (error != EBUSY) {
3645 error = vn_lock(nvp, LK_EXCLUSIVE);
3663 * Note that we must use VRELE_ASYNC in this function as it walks
3664 * up the directory tree and vrele may need to acquire an exclusive
3665 * lock if a last reference to a vnode is dropped.
3668 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3675 zfsvfs = tdzp->z_zfsvfs;
3677 return (SET_ERROR(EINVAL));
3680 if (tdzp->z_id == zfsvfs->z_root)
3684 ASSERT(!zp->z_unlinked);
3685 if ((error = sa_lookup(zp->z_sa_hdl,
3686 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3689 if (parent == szp->z_id) {
3690 error = SET_ERROR(EINVAL);
3693 if (parent == zfsvfs->z_root)
3695 if (parent == sdzp->z_id)
3698 error = zfs_zget(zfsvfs, parent, &zp1);
3703 VN_RELE_ASYNC(ZTOV(zp),
3704 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3708 if (error == ENOTDIR)
3709 panic("checkpath: .. not a directory\n");
3711 VN_RELE_ASYNC(ZTOV(zp),
3712 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3717 * Move an entry from the provided source directory to the target
3718 * directory. Change the entry name as indicated.
3720 * IN: sdvp - Source directory containing the "old entry".
3721 * snm - Old entry name.
3722 * tdvp - Target directory to contain the "new entry".
3723 * tnm - New entry name.
3724 * cr - credentials of caller.
3725 * ct - caller context
3726 * flags - case flags
3728 * RETURN: 0 on success, error code on failure.
3731 * sdvp,tdvp - ctime|mtime updated
3735 zfs_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3736 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3740 znode_t *sdzp, *tdzp, *szp, *tzp;
3741 zilog_t *zilog = NULL;
3743 char *snm = scnp->cn_nameptr;
3744 char *tnm = tcnp->cn_nameptr;
3747 /* Reject renames across filesystems. */
3748 if ((*svpp)->v_mount != tdvp->v_mount ||
3749 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3750 error = SET_ERROR(EXDEV);
3754 if (zfsctl_is_node(tdvp)) {
3755 error = SET_ERROR(EXDEV);
3760 * Lock all four vnodes to ensure safety and semantics of renaming.
3762 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3764 /* no vnodes are locked in the case of error here */
3770 zfsvfs = tdzp->z_zfsvfs;
3771 zilog = zfsvfs->z_log;
3774 * After we re-enter ZFS_ENTER() we will have to revalidate all
3779 if (zfsvfs->z_utf8 && u8_validate(tnm,
3780 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3781 error = SET_ERROR(EILSEQ);
3785 /* If source and target are the same file, there is nothing to do. */
3786 if ((*svpp) == (*tvpp)) {
3791 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3792 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3793 (*tvpp)->v_mountedhere != NULL)) {
3794 error = SET_ERROR(EXDEV);
3799 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3800 * bypassing the cleanup code in the case of an error.
3802 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3803 error = SET_ERROR(EIO);
3808 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3809 if (szp->z_sa_hdl == NULL || (tzp != NULL && tzp->z_sa_hdl == NULL)) {
3810 error = SET_ERROR(EIO);
3815 * This is to prevent the creation of links into attribute space
3816 * by renaming a linked file into/outof an attribute directory.
3817 * See the comment in zfs_link() for why this is considered bad.
3819 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3820 error = SET_ERROR(EINVAL);
3825 * Must have write access at the source to remove the old entry
3826 * and write access at the target to create the new entry.
3827 * Note that if target and source are the same, this can be
3828 * done in a single check.
3830 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3833 if ((*svpp)->v_type == VDIR) {
3835 * Avoid ".", "..", and aliases of "." for obvious reasons.
3837 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3839 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3845 * Check to make sure rename is valid.
3846 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3848 if (error = zfs_rename_check(szp, sdzp, tdzp))
3853 * Does target exist?
3857 * Source and target must be the same type.
3859 if ((*svpp)->v_type == VDIR) {
3860 if ((*tvpp)->v_type != VDIR) {
3861 error = SET_ERROR(ENOTDIR);
3869 if ((*tvpp)->v_type == VDIR) {
3870 error = SET_ERROR(EISDIR);
3876 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3878 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3881 * notify the target directory if it is not the same
3882 * as source directory.
3885 vnevent_rename_dest_dir(tdvp, ct);
3888 tx = dmu_tx_create(zfsvfs->z_os);
3889 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3890 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3891 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3892 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3894 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3895 zfs_sa_upgrade_txholds(tx, tdzp);
3898 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3899 zfs_sa_upgrade_txholds(tx, tzp);
3902 zfs_sa_upgrade_txholds(tx, szp);
3903 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3904 error = dmu_tx_assign(tx, TXG_WAIT);
3911 if (tzp) /* Attempt to remove the existing target */
3912 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3915 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3917 szp->z_pflags |= ZFS_AV_MODIFIED;
3919 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3920 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3923 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3926 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3927 snm, tdzp, tnm, szp);
3930 * Update path information for the target vnode
3932 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
3935 * At this point, we have successfully created
3936 * the target name, but have failed to remove
3937 * the source name. Since the create was done
3938 * with the ZRENAMING flag, there are
3939 * complications; for one, the link count is
3940 * wrong. The easiest way to deal with this
3941 * is to remove the newly created target, and
3942 * return the original error. This must
3943 * succeed; fortunately, it is very unlikely to
3944 * fail, since we just created it.
3946 VERIFY3U(zfs_link_destroy(tdzp, tnm, szp, tx,
3947 ZRENAMING, NULL), ==, 0);
3954 cache_purge_negative(tdvp);
3960 unlockout: /* all 4 vnodes are locked, ZFS_ENTER called */
3962 VOP_UNLOCK(*svpp, 0);
3963 VOP_UNLOCK(sdvp, 0);
3965 out: /* original two vnodes are locked */
3966 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3967 zil_commit(zilog, 0);
3970 VOP_UNLOCK(*tvpp, 0);
3972 VOP_UNLOCK(tdvp, 0);
3977 * Insert the indicated symbolic reference entry into the directory.
3979 * IN: dvp - Directory to contain new symbolic link.
3980 * link - Name for new symlink entry.
3981 * vap - Attributes of new entry.
3982 * cr - credentials of caller.
3983 * ct - caller context
3984 * flags - case flags
3986 * RETURN: 0 on success, error code on failure.
3989 * dvp - ctime|mtime updated
3993 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
3994 cred_t *cr, kthread_t *td)
3996 znode_t *zp, *dzp = VTOZ(dvp);
3998 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4000 uint64_t len = strlen(link);
4002 zfs_acl_ids_t acl_ids;
4003 boolean_t fuid_dirtied;
4004 uint64_t txtype = TX_SYMLINK;
4007 ASSERT(vap->va_type == VLNK);
4011 zilog = zfsvfs->z_log;
4013 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4014 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4016 return (SET_ERROR(EILSEQ));
4019 if (len > MAXPATHLEN) {
4021 return (SET_ERROR(ENAMETOOLONG));
4024 if ((error = zfs_acl_ids_create(dzp, 0,
4025 vap, cr, NULL, &acl_ids)) != 0) {
4031 * Attempt to lock directory; fail if entry already exists.
4033 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
4035 zfs_acl_ids_free(&acl_ids);
4040 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4041 zfs_acl_ids_free(&acl_ids);
4046 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4047 zfs_acl_ids_free(&acl_ids);
4049 return (SET_ERROR(EDQUOT));
4052 getnewvnode_reserve(1);
4053 tx = dmu_tx_create(zfsvfs->z_os);
4054 fuid_dirtied = zfsvfs->z_fuid_dirty;
4055 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4056 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4057 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4058 ZFS_SA_BASE_ATTR_SIZE + len);
4059 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4060 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4061 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4062 acl_ids.z_aclp->z_acl_bytes);
4065 zfs_fuid_txhold(zfsvfs, tx);
4066 error = dmu_tx_assign(tx, TXG_WAIT);
4068 zfs_acl_ids_free(&acl_ids);
4070 getnewvnode_drop_reserve();
4076 * Create a new object for the symlink.
4077 * for version 4 ZPL datsets the symlink will be an SA attribute
4079 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4082 zfs_fuid_sync(zfsvfs, tx);
4085 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4088 zfs_sa_symlink(zp, link, len, tx);
4091 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4092 &zp->z_size, sizeof (zp->z_size), tx);
4094 * Insert the new object into the directory.
4096 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
4098 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4101 zfs_acl_ids_free(&acl_ids);
4105 getnewvnode_drop_reserve();
4107 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4108 zil_commit(zilog, 0);
4115 * Return, in the buffer contained in the provided uio structure,
4116 * the symbolic path referred to by vp.
4118 * IN: vp - vnode of symbolic link.
4119 * uio - structure to contain the link path.
4120 * cr - credentials of caller.
4121 * ct - caller context
4123 * OUT: uio - structure containing the link path.
4125 * RETURN: 0 on success, error code on failure.
4128 * vp - atime updated
4132 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4134 znode_t *zp = VTOZ(vp);
4135 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4142 error = sa_lookup_uio(zp->z_sa_hdl,
4143 SA_ZPL_SYMLINK(zfsvfs), uio);
4145 error = zfs_sa_readlink(zp, uio);
4147 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4154 * Insert a new entry into directory tdvp referencing svp.
4156 * IN: tdvp - Directory to contain new entry.
4157 * svp - vnode of new entry.
4158 * name - name of new entry.
4159 * cr - credentials of caller.
4160 * ct - caller context
4162 * RETURN: 0 on success, error code on failure.
4165 * tdvp - ctime|mtime updated
4166 * svp - ctime updated
4170 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4171 caller_context_t *ct, int flags)
4173 znode_t *dzp = VTOZ(tdvp);
4175 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4182 ASSERT(tdvp->v_type == VDIR);
4186 zilog = zfsvfs->z_log;
4189 * POSIX dictates that we return EPERM here.
4190 * Better choices include ENOTSUP or EISDIR.
4192 if (svp->v_type == VDIR) {
4194 return (SET_ERROR(EPERM));
4200 if (szp->z_pflags & (ZFS_APPENDONLY | ZFS_IMMUTABLE | ZFS_READONLY)) {
4202 return (SET_ERROR(EPERM));
4205 /* Prevent links to .zfs/shares files */
4207 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4208 &parent, sizeof (uint64_t))) != 0) {
4212 if (parent == zfsvfs->z_shares_dir) {
4214 return (SET_ERROR(EPERM));
4217 if (zfsvfs->z_utf8 && u8_validate(name,
4218 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4220 return (SET_ERROR(EILSEQ));
4224 * We do not support links between attributes and non-attributes
4225 * because of the potential security risk of creating links
4226 * into "normal" file space in order to circumvent restrictions
4227 * imposed in attribute space.
4229 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4231 return (SET_ERROR(EINVAL));
4235 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4236 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4238 return (SET_ERROR(EPERM));
4241 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4247 * Attempt to lock directory; fail if entry already exists.
4249 error = zfs_dirent_lookup(dzp, name, &tzp, ZNEW);
4255 tx = dmu_tx_create(zfsvfs->z_os);
4256 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4257 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4258 zfs_sa_upgrade_txholds(tx, szp);
4259 zfs_sa_upgrade_txholds(tx, dzp);
4260 error = dmu_tx_assign(tx, TXG_WAIT);
4267 error = zfs_link_create(dzp, name, szp, tx, 0);
4270 uint64_t txtype = TX_LINK;
4271 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4277 vnevent_link(svp, ct);
4280 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4281 zil_commit(zilog, 0);
4290 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4292 znode_t *zp = VTOZ(vp);
4293 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4296 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4297 if (zp->z_sa_hdl == NULL) {
4299 * The fs has been unmounted, or we did a
4300 * suspend/resume and this file no longer exists.
4302 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4307 if (zp->z_unlinked) {
4309 * Fast path to recycle a vnode of a removed file.
4311 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4316 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4317 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4319 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4320 zfs_sa_upgrade_txholds(tx, zp);
4321 error = dmu_tx_assign(tx, TXG_WAIT);
4325 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4326 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4327 zp->z_atime_dirty = 0;
4331 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4335 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
4336 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
4340 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4342 znode_t *zp = VTOZ(vp);
4343 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4346 uint64_t object = zp->z_id;
4353 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4354 &gen64, sizeof (uint64_t))) != 0) {
4359 gen = (uint32_t)gen64;
4361 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4364 if (fidp->fid_len < size) {
4365 fidp->fid_len = size;
4367 return (SET_ERROR(ENOSPC));
4370 fidp->fid_len = size;
4373 zfid = (zfid_short_t *)fidp;
4375 zfid->zf_len = size;
4377 for (i = 0; i < sizeof (zfid->zf_object); i++)
4378 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4380 /* Must have a non-zero generation number to distinguish from .zfs */
4383 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4384 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4386 if (size == LONG_FID_LEN) {
4387 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4390 zlfid = (zfid_long_t *)fidp;
4392 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4393 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4395 /* XXX - this should be the generation number for the objset */
4396 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4397 zlfid->zf_setgen[i] = 0;
4405 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4406 caller_context_t *ct)
4417 case _PC_FILESIZEBITS:
4421 case _PC_XATTR_EXISTS:
4423 zfsvfs = zp->z_zfsvfs;
4427 error = zfs_dirent_lookup(zp, "", &xzp,
4428 ZXATTR | ZEXISTS | ZSHARED);
4430 if (!zfs_dirempty(xzp))
4433 } else if (error == ENOENT) {
4435 * If there aren't extended attributes, it's the
4436 * same as having zero of them.
4443 case _PC_SATTR_ENABLED:
4444 case _PC_SATTR_EXISTS:
4445 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4446 (vp->v_type == VREG || vp->v_type == VDIR);
4449 case _PC_ACCESS_FILTERING:
4450 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4454 case _PC_ACL_ENABLED:
4455 *valp = _ACL_ACE_ENABLED;
4457 #endif /* illumos */
4458 case _PC_MIN_HOLE_SIZE:
4459 *valp = (int)SPA_MINBLOCKSIZE;
4462 case _PC_TIMESTAMP_RESOLUTION:
4463 /* nanosecond timestamp resolution */
4467 case _PC_ACL_EXTENDED:
4475 case _PC_ACL_PATH_MAX:
4476 *valp = ACL_MAX_ENTRIES;
4480 return (EOPNOTSUPP);
4486 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4487 caller_context_t *ct)
4489 znode_t *zp = VTOZ(vp);
4490 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4492 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4496 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4504 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4505 caller_context_t *ct)
4507 znode_t *zp = VTOZ(vp);
4508 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4510 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4511 zilog_t *zilog = zfsvfs->z_log;
4516 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4518 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4519 zil_commit(zilog, 0);
4526 ioflags(int ioflags)
4530 if (ioflags & IO_APPEND)
4532 if (ioflags & IO_NDELAY)
4534 if (ioflags & IO_SYNC)
4535 flags |= (FSYNC | FDSYNC | FRSYNC);
4541 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int *rbehind,
4544 znode_t *zp = VTOZ(vp);
4545 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4546 objset_t *os = zp->z_zfsvfs->z_os;
4551 off_t startoff, endoff;
4553 vm_pindex_t reqstart, reqend;
4556 object = m[0]->object;
4562 zfs_vmobject_wlock(object);
4563 if (m[count - 1]->valid != 0 && --count == 0) {
4564 zfs_vmobject_wunlock(object);
4568 mlast = m[count - 1];
4570 if (IDX_TO_OFF(mlast->pindex) >=
4571 object->un_pager.vnp.vnp_size) {
4572 zfs_vmobject_wunlock(object);
4574 return (zfs_vm_pagerret_bad);
4577 PCPU_INC(cnt.v_vnodein);
4578 PCPU_ADD(cnt.v_vnodepgsin, count);
4581 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
4582 lsize = object->un_pager.vnp.vnp_size -
4583 IDX_TO_OFF(mlast->pindex);
4584 zfs_vmobject_wunlock(object);
4586 for (i = 0; i < count; i++) {
4590 va = zfs_map_page(m[i], &sf);
4591 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
4592 size, va, DMU_READ_PREFETCH);
4593 if (size != PAGE_SIZE)
4594 bzero(va + size, PAGE_SIZE - size);
4600 zfs_vmobject_wlock(object);
4601 for (i = 0; i < count; i++)
4602 m[i]->valid = VM_PAGE_BITS_ALL;
4603 zfs_vmobject_wunlock(object);
4606 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4613 return (zfs_vm_pagerret_ok);
4615 return (zfs_vm_pagerret_error);
4619 zfs_freebsd_getpages(ap)
4620 struct vop_getpages_args /* {
4629 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4634 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4637 znode_t *zp = VTOZ(vp);
4638 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4647 vm_ooffset_t lo_off;
4658 object = vp->v_object;
4662 KASSERT(ma[0]->object == object, ("mismatching object"));
4663 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4665 for (i = 0; i < pcount; i++)
4666 rtvals[i] = zfs_vm_pagerret_error;
4668 off = IDX_TO_OFF(ma[0]->pindex);
4669 blksz = zp->z_blksz;
4670 lo_off = rounddown(off, blksz);
4671 lo_len = roundup(len + (off - lo_off), blksz);
4672 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
4674 zfs_vmobject_wlock(object);
4675 if (len + off > object->un_pager.vnp.vnp_size) {
4676 if (object->un_pager.vnp.vnp_size > off) {
4679 len = object->un_pager.vnp.vnp_size - off;
4681 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4683 * If the object is locked and the following
4684 * conditions hold, then the page's dirty
4685 * field cannot be concurrently changed by a
4689 vm_page_assert_sbusied(m);
4690 KASSERT(!pmap_page_is_write_mapped(m),
4691 ("zfs_putpages: page %p is not read-only", m));
4692 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4699 if (ncount < pcount) {
4700 for (i = ncount; i < pcount; i++) {
4701 rtvals[i] = zfs_vm_pagerret_bad;
4705 zfs_vmobject_wunlock(object);
4710 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4711 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4715 tx = dmu_tx_create(zfsvfs->z_os);
4716 dmu_tx_hold_write(tx, zp->z_id, off, len);
4718 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4719 zfs_sa_upgrade_txholds(tx, zp);
4720 err = dmu_tx_assign(tx, TXG_WAIT);
4726 if (zp->z_blksz < PAGE_SIZE) {
4728 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4729 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4730 va = zfs_map_page(ma[i], &sf);
4731 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4735 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4739 uint64_t mtime[2], ctime[2];
4740 sa_bulk_attr_t bulk[3];
4743 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4745 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4747 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4749 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4751 (void)sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4752 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4754 zfs_vmobject_wlock(object);
4755 for (i = 0; i < ncount; i++) {
4756 rtvals[i] = zfs_vm_pagerret_ok;
4757 vm_page_undirty(ma[i]);
4759 zfs_vmobject_wunlock(object);
4760 PCPU_INC(cnt.v_vnodeout);
4761 PCPU_ADD(cnt.v_vnodepgsout, ncount);
4766 zfs_range_unlock(rl);
4767 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4768 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4769 zil_commit(zfsvfs->z_log, zp->z_id);
4775 zfs_freebsd_putpages(ap)
4776 struct vop_putpages_args /* {
4785 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4790 zfs_freebsd_bmap(ap)
4791 struct vop_bmap_args /* {
4794 struct bufobj **a_bop;
4801 if (ap->a_bop != NULL)
4802 *ap->a_bop = &ap->a_vp->v_bufobj;
4803 if (ap->a_bnp != NULL)
4804 *ap->a_bnp = ap->a_bn;
4805 if (ap->a_runp != NULL)
4807 if (ap->a_runb != NULL)
4814 zfs_freebsd_open(ap)
4815 struct vop_open_args /* {
4818 struct ucred *a_cred;
4819 struct thread *a_td;
4822 vnode_t *vp = ap->a_vp;
4823 znode_t *zp = VTOZ(vp);
4826 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
4828 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4833 zfs_freebsd_close(ap)
4834 struct vop_close_args /* {
4837 struct ucred *a_cred;
4838 struct thread *a_td;
4842 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
4846 zfs_freebsd_ioctl(ap)
4847 struct vop_ioctl_args /* {
4857 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4858 ap->a_fflag, ap->a_cred, NULL, NULL));
4862 zfs_freebsd_read(ap)
4863 struct vop_read_args /* {
4867 struct ucred *a_cred;
4871 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4876 zfs_freebsd_write(ap)
4877 struct vop_write_args /* {
4881 struct ucred *a_cred;
4885 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4890 zfs_freebsd_access(ap)
4891 struct vop_access_args /* {
4893 accmode_t a_accmode;
4894 struct ucred *a_cred;
4895 struct thread *a_td;
4898 vnode_t *vp = ap->a_vp;
4899 znode_t *zp = VTOZ(vp);
4904 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4906 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4908 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
4911 * VADMIN has to be handled by vaccess().
4914 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4916 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4917 zp->z_gid, accmode, ap->a_cred, NULL);
4922 * For VEXEC, ensure that at least one execute bit is set for
4925 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4926 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4934 zfs_freebsd_lookup(ap)
4935 struct vop_lookup_args /* {
4936 struct vnode *a_dvp;
4937 struct vnode **a_vpp;
4938 struct componentname *a_cnp;
4941 struct componentname *cnp = ap->a_cnp;
4942 char nm[NAME_MAX + 1];
4944 ASSERT(cnp->cn_namelen < sizeof(nm));
4945 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
4947 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4948 cnp->cn_cred, cnp->cn_thread, 0));
4952 zfs_cache_lookup(ap)
4953 struct vop_lookup_args /* {
4954 struct vnode *a_dvp;
4955 struct vnode **a_vpp;
4956 struct componentname *a_cnp;
4961 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4962 if (zfsvfs->z_use_namecache)
4963 return (vfs_cache_lookup(ap));
4965 return (zfs_freebsd_lookup(ap));
4969 zfs_freebsd_create(ap)
4970 struct vop_create_args /* {
4971 struct vnode *a_dvp;
4972 struct vnode **a_vpp;
4973 struct componentname *a_cnp;
4974 struct vattr *a_vap;
4978 struct componentname *cnp = ap->a_cnp;
4979 vattr_t *vap = ap->a_vap;
4982 ASSERT(cnp->cn_flags & SAVENAME);
4984 vattr_init_mask(vap);
4985 mode = vap->va_mode & ALLPERMS;
4986 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4988 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
4989 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
4990 if (zfsvfs->z_use_namecache &&
4991 error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
4992 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
4997 zfs_freebsd_remove(ap)
4998 struct vop_remove_args /* {
4999 struct vnode *a_dvp;
5001 struct componentname *a_cnp;
5005 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5007 return (zfs_remove(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
5008 ap->a_cnp->cn_cred));
5012 zfs_freebsd_mkdir(ap)
5013 struct vop_mkdir_args /* {
5014 struct vnode *a_dvp;
5015 struct vnode **a_vpp;
5016 struct componentname *a_cnp;
5017 struct vattr *a_vap;
5020 vattr_t *vap = ap->a_vap;
5022 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5024 vattr_init_mask(vap);
5026 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
5027 ap->a_cnp->cn_cred));
5031 zfs_freebsd_rmdir(ap)
5032 struct vop_rmdir_args /* {
5033 struct vnode *a_dvp;
5035 struct componentname *a_cnp;
5038 struct componentname *cnp = ap->a_cnp;
5040 ASSERT(cnp->cn_flags & SAVENAME);
5042 return (zfs_rmdir(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
5046 zfs_freebsd_readdir(ap)
5047 struct vop_readdir_args /* {
5050 struct ucred *a_cred;
5057 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
5058 ap->a_ncookies, ap->a_cookies));
5062 zfs_freebsd_fsync(ap)
5063 struct vop_fsync_args /* {
5066 struct thread *a_td;
5071 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
5075 zfs_freebsd_getattr(ap)
5076 struct vop_getattr_args /* {
5078 struct vattr *a_vap;
5079 struct ucred *a_cred;
5082 vattr_t *vap = ap->a_vap;
5088 xvap.xva_vattr = *vap;
5089 xvap.xva_vattr.va_mask |= AT_XVATTR;
5091 /* Convert chflags into ZFS-type flags. */
5092 /* XXX: what about SF_SETTABLE?. */
5093 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
5094 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
5095 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
5096 XVA_SET_REQ(&xvap, XAT_NODUMP);
5097 XVA_SET_REQ(&xvap, XAT_READONLY);
5098 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
5099 XVA_SET_REQ(&xvap, XAT_SYSTEM);
5100 XVA_SET_REQ(&xvap, XAT_HIDDEN);
5101 XVA_SET_REQ(&xvap, XAT_REPARSE);
5102 XVA_SET_REQ(&xvap, XAT_OFFLINE);
5103 XVA_SET_REQ(&xvap, XAT_SPARSE);
5105 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
5109 /* Convert ZFS xattr into chflags. */
5110 #define FLAG_CHECK(fflag, xflag, xfield) do { \
5111 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
5112 fflags |= (fflag); \
5114 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
5115 xvap.xva_xoptattrs.xoa_immutable);
5116 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
5117 xvap.xva_xoptattrs.xoa_appendonly);
5118 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
5119 xvap.xva_xoptattrs.xoa_nounlink);
5120 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
5121 xvap.xva_xoptattrs.xoa_archive);
5122 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
5123 xvap.xva_xoptattrs.xoa_nodump);
5124 FLAG_CHECK(UF_READONLY, XAT_READONLY,
5125 xvap.xva_xoptattrs.xoa_readonly);
5126 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
5127 xvap.xva_xoptattrs.xoa_system);
5128 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
5129 xvap.xva_xoptattrs.xoa_hidden);
5130 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
5131 xvap.xva_xoptattrs.xoa_reparse);
5132 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
5133 xvap.xva_xoptattrs.xoa_offline);
5134 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
5135 xvap.xva_xoptattrs.xoa_sparse);
5138 *vap = xvap.xva_vattr;
5139 vap->va_flags = fflags;
5144 zfs_freebsd_setattr(ap)
5145 struct vop_setattr_args /* {
5147 struct vattr *a_vap;
5148 struct ucred *a_cred;
5151 vnode_t *vp = ap->a_vp;
5152 vattr_t *vap = ap->a_vap;
5153 cred_t *cred = ap->a_cred;
5158 vattr_init_mask(vap);
5159 vap->va_mask &= ~AT_NOSET;
5162 xvap.xva_vattr = *vap;
5164 zflags = VTOZ(vp)->z_pflags;
5166 if (vap->va_flags != VNOVAL) {
5167 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
5170 if (zfsvfs->z_use_fuids == B_FALSE)
5171 return (EOPNOTSUPP);
5173 fflags = vap->va_flags;
5176 * We need to figure out whether it makes sense to allow
5177 * UF_REPARSE through, since we don't really have other
5178 * facilities to handle reparse points and zfs_setattr()
5179 * doesn't currently allow setting that attribute anyway.
5181 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
5182 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
5183 UF_OFFLINE|UF_SPARSE)) != 0)
5184 return (EOPNOTSUPP);
5186 * Unprivileged processes are not permitted to unset system
5187 * flags, or modify flags if any system flags are set.
5188 * Privileged non-jail processes may not modify system flags
5189 * if securelevel > 0 and any existing system flags are set.
5190 * Privileged jail processes behave like privileged non-jail
5191 * processes if the security.jail.chflags_allowed sysctl is
5192 * is non-zero; otherwise, they behave like unprivileged
5195 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
5196 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
5198 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5199 error = securelevel_gt(cred, 0);
5205 * Callers may only modify the file flags on objects they
5206 * have VADMIN rights for.
5208 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
5211 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5215 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
5220 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
5221 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
5222 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
5223 XVA_SET_REQ(&xvap, (xflag)); \
5224 (xfield) = ((fflags & (fflag)) != 0); \
5227 /* Convert chflags into ZFS-type flags. */
5228 /* XXX: what about SF_SETTABLE?. */
5229 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
5230 xvap.xva_xoptattrs.xoa_immutable);
5231 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
5232 xvap.xva_xoptattrs.xoa_appendonly);
5233 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
5234 xvap.xva_xoptattrs.xoa_nounlink);
5235 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
5236 xvap.xva_xoptattrs.xoa_archive);
5237 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
5238 xvap.xva_xoptattrs.xoa_nodump);
5239 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
5240 xvap.xva_xoptattrs.xoa_readonly);
5241 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
5242 xvap.xva_xoptattrs.xoa_system);
5243 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
5244 xvap.xva_xoptattrs.xoa_hidden);
5245 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
5246 xvap.xva_xoptattrs.xoa_hidden);
5247 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
5248 xvap.xva_xoptattrs.xoa_offline);
5249 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
5250 xvap.xva_xoptattrs.xoa_sparse);
5253 if (vap->va_birthtime.tv_sec != VNOVAL) {
5254 xvap.xva_vattr.va_mask |= AT_XVATTR;
5255 XVA_SET_REQ(&xvap, XAT_CREATETIME);
5257 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
5261 zfs_freebsd_rename(ap)
5262 struct vop_rename_args /* {
5263 struct vnode *a_fdvp;
5264 struct vnode *a_fvp;
5265 struct componentname *a_fcnp;
5266 struct vnode *a_tdvp;
5267 struct vnode *a_tvp;
5268 struct componentname *a_tcnp;
5271 vnode_t *fdvp = ap->a_fdvp;
5272 vnode_t *fvp = ap->a_fvp;
5273 vnode_t *tdvp = ap->a_tdvp;
5274 vnode_t *tvp = ap->a_tvp;
5277 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
5278 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
5280 error = zfs_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
5281 ap->a_tcnp, ap->a_fcnp->cn_cred);
5293 zfs_freebsd_symlink(ap)
5294 struct vop_symlink_args /* {
5295 struct vnode *a_dvp;
5296 struct vnode **a_vpp;
5297 struct componentname *a_cnp;
5298 struct vattr *a_vap;
5302 struct componentname *cnp = ap->a_cnp;
5303 vattr_t *vap = ap->a_vap;
5305 ASSERT(cnp->cn_flags & SAVENAME);
5307 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
5308 vattr_init_mask(vap);
5310 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
5311 ap->a_target, cnp->cn_cred, cnp->cn_thread));
5315 zfs_freebsd_readlink(ap)
5316 struct vop_readlink_args /* {
5319 struct ucred *a_cred;
5323 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
5327 zfs_freebsd_link(ap)
5328 struct vop_link_args /* {
5329 struct vnode *a_tdvp;
5331 struct componentname *a_cnp;
5334 struct componentname *cnp = ap->a_cnp;
5335 vnode_t *vp = ap->a_vp;
5336 vnode_t *tdvp = ap->a_tdvp;
5338 if (tdvp->v_mount != vp->v_mount)
5341 ASSERT(cnp->cn_flags & SAVENAME);
5343 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
5347 zfs_freebsd_inactive(ap)
5348 struct vop_inactive_args /* {
5350 struct thread *a_td;
5353 vnode_t *vp = ap->a_vp;
5355 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5360 zfs_freebsd_reclaim(ap)
5361 struct vop_reclaim_args /* {
5363 struct thread *a_td;
5366 vnode_t *vp = ap->a_vp;
5367 znode_t *zp = VTOZ(vp);
5368 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5372 /* Destroy the vm object and flush associated pages. */
5373 vnode_destroy_vobject(vp);
5376 * z_teardown_inactive_lock protects from a race with
5377 * zfs_znode_dmu_fini in zfsvfs_teardown during
5380 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
5381 if (zp->z_sa_hdl == NULL)
5385 rw_exit(&zfsvfs->z_teardown_inactive_lock);
5393 struct vop_fid_args /* {
5399 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5403 zfs_freebsd_pathconf(ap)
5404 struct vop_pathconf_args /* {
5407 register_t *a_retval;
5413 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
5415 *ap->a_retval = val;
5416 else if (error == EOPNOTSUPP)
5417 error = vop_stdpathconf(ap);
5422 zfs_freebsd_fifo_pathconf(ap)
5423 struct vop_pathconf_args /* {
5426 register_t *a_retval;
5430 switch (ap->a_name) {
5431 case _PC_ACL_EXTENDED:
5433 case _PC_ACL_PATH_MAX:
5434 case _PC_MAC_PRESENT:
5435 return (zfs_freebsd_pathconf(ap));
5437 return (fifo_specops.vop_pathconf(ap));
5442 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5443 * extended attribute name:
5446 * system freebsd:system:
5447 * user (none, can be used to access ZFS fsattr(5) attributes
5448 * created on Solaris)
5451 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5454 const char *namespace, *prefix, *suffix;
5456 /* We don't allow '/' character in attribute name. */
5457 if (strchr(name, '/') != NULL)
5459 /* We don't allow attribute names that start with "freebsd:" string. */
5460 if (strncmp(name, "freebsd:", 8) == 0)
5463 bzero(attrname, size);
5465 switch (attrnamespace) {
5466 case EXTATTR_NAMESPACE_USER:
5468 prefix = "freebsd:";
5469 namespace = EXTATTR_NAMESPACE_USER_STRING;
5473 * This is the default namespace by which we can access all
5474 * attributes created on Solaris.
5476 prefix = namespace = suffix = "";
5479 case EXTATTR_NAMESPACE_SYSTEM:
5480 prefix = "freebsd:";
5481 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5484 case EXTATTR_NAMESPACE_EMPTY:
5488 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5490 return (ENAMETOOLONG);
5496 * Vnode operating to retrieve a named extended attribute.
5499 zfs_getextattr(struct vop_getextattr_args *ap)
5502 IN struct vnode *a_vp;
5503 IN int a_attrnamespace;
5504 IN const char *a_name;
5505 INOUT struct uio *a_uio;
5507 IN struct ucred *a_cred;
5508 IN struct thread *a_td;
5512 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5513 struct thread *td = ap->a_td;
5514 struct nameidata nd;
5517 vnode_t *xvp = NULL, *vp;
5520 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5521 ap->a_cred, ap->a_td, VREAD);
5525 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5532 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5540 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5542 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
5544 NDFREE(&nd, NDF_ONLY_PNBUF);
5547 if (error == ENOENT)
5552 if (ap->a_size != NULL) {
5553 error = VOP_GETATTR(vp, &va, ap->a_cred);
5555 *ap->a_size = (size_t)va.va_size;
5556 } else if (ap->a_uio != NULL)
5557 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5560 vn_close(vp, flags, ap->a_cred, td);
5567 * Vnode operation to remove a named attribute.
5570 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5573 IN struct vnode *a_vp;
5574 IN int a_attrnamespace;
5575 IN const char *a_name;
5576 IN struct ucred *a_cred;
5577 IN struct thread *a_td;
5581 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5582 struct thread *td = ap->a_td;
5583 struct nameidata nd;
5586 vnode_t *xvp = NULL, *vp;
5589 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5590 ap->a_cred, ap->a_td, VWRITE);
5594 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5601 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5608 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5609 UIO_SYSSPACE, attrname, xvp, td);
5614 NDFREE(&nd, NDF_ONLY_PNBUF);
5615 if (error == ENOENT)
5620 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5621 NDFREE(&nd, NDF_ONLY_PNBUF);
5624 if (vp == nd.ni_dvp)
5634 * Vnode operation to set a named attribute.
5637 zfs_setextattr(struct vop_setextattr_args *ap)
5640 IN struct vnode *a_vp;
5641 IN int a_attrnamespace;
5642 IN const char *a_name;
5643 INOUT struct uio *a_uio;
5644 IN struct ucred *a_cred;
5645 IN struct thread *a_td;
5649 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5650 struct thread *td = ap->a_td;
5651 struct nameidata nd;
5654 vnode_t *xvp = NULL, *vp;
5657 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5658 ap->a_cred, ap->a_td, VWRITE);
5662 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5669 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5670 LOOKUP_XATTR | CREATE_XATTR_DIR);
5676 flags = FFLAGS(O_WRONLY | O_CREAT);
5677 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5679 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
5681 NDFREE(&nd, NDF_ONLY_PNBUF);
5689 error = VOP_SETATTR(vp, &va, ap->a_cred);
5691 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5694 vn_close(vp, flags, ap->a_cred, td);
5701 * Vnode operation to retrieve extended attributes on a vnode.
5704 zfs_listextattr(struct vop_listextattr_args *ap)
5707 IN struct vnode *a_vp;
5708 IN int a_attrnamespace;
5709 INOUT struct uio *a_uio;
5711 IN struct ucred *a_cred;
5712 IN struct thread *a_td;
5716 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5717 struct thread *td = ap->a_td;
5718 struct nameidata nd;
5719 char attrprefix[16];
5720 u_char dirbuf[sizeof(struct dirent)];
5723 struct uio auio, *uio = ap->a_uio;
5724 size_t *sizep = ap->a_size;
5726 vnode_t *xvp = NULL, *vp;
5727 int done, error, eof, pos;
5729 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5730 ap->a_cred, ap->a_td, VREAD);
5734 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5735 sizeof(attrprefix));
5738 plen = strlen(attrprefix);
5745 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5750 * ENOATTR means that the EA directory does not yet exist,
5751 * i.e. there are no extended attributes there.
5753 if (error == ENOATTR)
5758 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5759 UIO_SYSSPACE, ".", xvp, td);
5762 NDFREE(&nd, NDF_ONLY_PNBUF);
5768 auio.uio_iov = &aiov;
5769 auio.uio_iovcnt = 1;
5770 auio.uio_segflg = UIO_SYSSPACE;
5772 auio.uio_rw = UIO_READ;
5773 auio.uio_offset = 0;
5778 aiov.iov_base = (void *)dirbuf;
5779 aiov.iov_len = sizeof(dirbuf);
5780 auio.uio_resid = sizeof(dirbuf);
5781 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5782 done = sizeof(dirbuf) - auio.uio_resid;
5785 for (pos = 0; pos < done;) {
5786 dp = (struct dirent *)(dirbuf + pos);
5787 pos += dp->d_reclen;
5789 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5790 * is what we get when attribute was created on Solaris.
5792 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5794 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
5796 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5798 nlen = dp->d_namlen - plen;
5801 else if (uio != NULL) {
5803 * Format of extattr name entry is one byte for
5804 * length and the rest for name.
5806 error = uiomove(&nlen, 1, uio->uio_rw, uio);
5808 error = uiomove(dp->d_name + plen, nlen,
5815 } while (!eof && error == 0);
5824 zfs_freebsd_getacl(ap)
5825 struct vop_getacl_args /* {
5834 vsecattr_t vsecattr;
5836 if (ap->a_type != ACL_TYPE_NFS4)
5839 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
5840 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
5843 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
5844 if (vsecattr.vsa_aclentp != NULL)
5845 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
5851 zfs_freebsd_setacl(ap)
5852 struct vop_setacl_args /* {
5861 vsecattr_t vsecattr;
5862 int aclbsize; /* size of acl list in bytes */
5865 if (ap->a_type != ACL_TYPE_NFS4)
5868 if (ap->a_aclp == NULL)
5871 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
5875 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
5876 * splitting every entry into two and appending "canonical six"
5877 * entries at the end. Don't allow for setting an ACL that would
5878 * cause chmod(2) to run out of ACL entries.
5880 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
5883 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
5887 vsecattr.vsa_mask = VSA_ACE;
5888 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
5889 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
5890 aaclp = vsecattr.vsa_aclentp;
5891 vsecattr.vsa_aclentsz = aclbsize;
5893 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
5894 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
5895 kmem_free(aaclp, aclbsize);
5901 zfs_freebsd_aclcheck(ap)
5902 struct vop_aclcheck_args /* {
5911 return (EOPNOTSUPP);
5915 zfs_vptocnp(struct vop_vptocnp_args *ap)
5917 vnode_t *covered_vp;
5918 vnode_t *vp = ap->a_vp;;
5919 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
5920 znode_t *zp = VTOZ(vp);
5929 * If we are a snapshot mounted under .zfs, run the operation
5930 * on the covered vnode.
5932 if ((error = sa_lookup(zp->z_sa_hdl,
5933 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) {
5938 if (zp->z_id != parent || zfsvfs->z_parent == zfsvfs) {
5939 char name[MAXNAMLEN + 1];
5943 error = zfs_znode_parent_and_name(zp, &dzp, name);
5946 if (*ap->a_buflen < len)
5947 error = SET_ERROR(ENOMEM);
5950 *ap->a_buflen -= len;
5951 bcopy(name, ap->a_buf + *ap->a_buflen, len);
5952 *ap->a_vpp = ZTOV(dzp);
5959 covered_vp = vp->v_mount->mnt_vnodecovered;
5961 ltype = VOP_ISLOCKED(vp);
5963 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
5965 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
5966 ap->a_buf, ap->a_buflen);
5969 vn_lock(vp, ltype | LK_RETRY);
5970 if ((vp->v_iflag & VI_DOOMED) != 0)
5971 error = SET_ERROR(ENOENT);
5978 struct vop_lock1_args /* {
5989 err = vop_stdlock(ap);
5990 if (err == 0 && (ap->a_flags & LK_NOWAIT) == 0) {
5993 if (vp->v_mount != NULL && (vp->v_iflag & VI_DOOMED) == 0 &&
5994 zp != NULL && (zp->z_pflags & ZFS_XATTR) == 0)
5995 VERIFY(!RRM_LOCK_HELD(&zp->z_zfsvfs->z_teardown_lock));
6001 struct vop_vector zfs_vnodeops;
6002 struct vop_vector zfs_fifoops;
6003 struct vop_vector zfs_shareops;
6005 struct vop_vector zfs_vnodeops = {
6006 .vop_default = &default_vnodeops,
6007 .vop_inactive = zfs_freebsd_inactive,
6008 .vop_reclaim = zfs_freebsd_reclaim,
6009 .vop_access = zfs_freebsd_access,
6010 .vop_lookup = zfs_cache_lookup,
6011 .vop_cachedlookup = zfs_freebsd_lookup,
6012 .vop_getattr = zfs_freebsd_getattr,
6013 .vop_setattr = zfs_freebsd_setattr,
6014 .vop_create = zfs_freebsd_create,
6015 .vop_mknod = zfs_freebsd_create,
6016 .vop_mkdir = zfs_freebsd_mkdir,
6017 .vop_readdir = zfs_freebsd_readdir,
6018 .vop_fsync = zfs_freebsd_fsync,
6019 .vop_open = zfs_freebsd_open,
6020 .vop_close = zfs_freebsd_close,
6021 .vop_rmdir = zfs_freebsd_rmdir,
6022 .vop_ioctl = zfs_freebsd_ioctl,
6023 .vop_link = zfs_freebsd_link,
6024 .vop_symlink = zfs_freebsd_symlink,
6025 .vop_readlink = zfs_freebsd_readlink,
6026 .vop_read = zfs_freebsd_read,
6027 .vop_write = zfs_freebsd_write,
6028 .vop_remove = zfs_freebsd_remove,
6029 .vop_rename = zfs_freebsd_rename,
6030 .vop_pathconf = zfs_freebsd_pathconf,
6031 .vop_bmap = zfs_freebsd_bmap,
6032 .vop_fid = zfs_freebsd_fid,
6033 .vop_getextattr = zfs_getextattr,
6034 .vop_deleteextattr = zfs_deleteextattr,
6035 .vop_setextattr = zfs_setextattr,
6036 .vop_listextattr = zfs_listextattr,
6037 .vop_getacl = zfs_freebsd_getacl,
6038 .vop_setacl = zfs_freebsd_setacl,
6039 .vop_aclcheck = zfs_freebsd_aclcheck,
6040 .vop_getpages = zfs_freebsd_getpages,
6041 .vop_putpages = zfs_freebsd_putpages,
6042 .vop_vptocnp = zfs_vptocnp,
6044 .vop_lock1 = zfs_lock,
6048 struct vop_vector zfs_fifoops = {
6049 .vop_default = &fifo_specops,
6050 .vop_fsync = zfs_freebsd_fsync,
6051 .vop_access = zfs_freebsd_access,
6052 .vop_getattr = zfs_freebsd_getattr,
6053 .vop_inactive = zfs_freebsd_inactive,
6054 .vop_read = VOP_PANIC,
6055 .vop_reclaim = zfs_freebsd_reclaim,
6056 .vop_setattr = zfs_freebsd_setattr,
6057 .vop_write = VOP_PANIC,
6058 .vop_pathconf = zfs_freebsd_fifo_pathconf,
6059 .vop_fid = zfs_freebsd_fid,
6060 .vop_getacl = zfs_freebsd_getacl,
6061 .vop_setacl = zfs_freebsd_setacl,
6062 .vop_aclcheck = zfs_freebsd_aclcheck,
6066 * special share hidden files vnode operations template
6068 struct vop_vector zfs_shareops = {
6069 .vop_default = &default_vnodeops,
6070 .vop_access = zfs_freebsd_access,
6071 .vop_inactive = zfs_freebsd_inactive,
6072 .vop_reclaim = zfs_freebsd_reclaim,
6073 .vop_fid = zfs_freebsd_fid,
6074 .vop_pathconf = zfs_freebsd_pathconf,