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 <sys/vmmeter.h>
77 #include <vm/vm_param.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zfsvfs).
92 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) VN_RELE() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
104 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
110 * dmu_tx_assign(). This is critical because we don't want to block
111 * while holding locks.
113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
114 * reduces lock contention and CPU usage when we must wait (note that if
115 * throughput is constrained by the storage, nearly every transaction
118 * Note, in particular, that if a lock is sometimes acquired before
119 * the tx assigns, and sometimes after (e.g. z_lock), then failing
120 * to use a non-blocking assign can deadlock the system. The scenario:
122 * Thread A has grabbed a lock before calling dmu_tx_assign().
123 * Thread B is in an already-assigned tx, and blocks for this lock.
124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
125 * forever, because the previous txg can't quiesce until B's tx commits.
127 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
130 * to indicate that this operation has already called dmu_tx_wait().
131 * This will ensure that we don't retry forever, waiting a short bit
134 * (5) If the operation succeeded, generate the intent log entry for it
135 * before dropping locks. This ensures that the ordering of events
136 * in the intent log matches the order in which they actually occurred.
137 * During ZIL replay the zfs_log_* functions will update the sequence
138 * number to indicate the zil transaction has replayed.
140 * (6) At the end of each vnode op, the DMU tx must always commit,
141 * regardless of whether there were any errors.
143 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
144 * to ensure that synchronous semantics are provided when necessary.
146 * In general, this is how things should be ordered in each vnode op:
148 * ZFS_ENTER(zfsvfs); // exit if unmounted
150 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
151 * rw_enter(...); // grab any other locks you need
152 * tx = dmu_tx_create(...); // get DMU tx
153 * dmu_tx_hold_*(); // hold each object you might modify
154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
156 * rw_exit(...); // drop locks
157 * zfs_dirent_unlock(dl); // unlock directory entry
158 * VN_RELE(...); // release held vnodes
159 * if (error == ERESTART) {
165 * dmu_tx_abort(tx); // abort DMU tx
166 * ZFS_EXIT(zfsvfs); // finished in zfs
167 * return (error); // really out of space
169 * error = do_real_work(); // do whatever this VOP does
171 * zfs_log_*(...); // on success, make ZIL entry
172 * dmu_tx_commit(tx); // commit DMU tx -- error or not
173 * rw_exit(...); // drop locks
174 * zfs_dirent_unlock(dl); // unlock directory entry
175 * VN_RELE(...); // release held vnodes
176 * zil_commit(zilog, foid); // synchronous when necessary
177 * ZFS_EXIT(zfsvfs); // finished in zfs
178 * return (error); // done, report error
183 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
185 znode_t *zp = VTOZ(*vpp);
186 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
191 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
192 ((flag & FAPPEND) == 0)) {
194 return (SET_ERROR(EPERM));
197 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
198 ZTOV(zp)->v_type == VREG &&
199 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
200 if (fs_vscan(*vpp, cr, 0) != 0) {
202 return (SET_ERROR(EACCES));
206 /* Keep a count of the synchronous opens in the znode */
207 if (flag & (FSYNC | FDSYNC))
208 atomic_inc_32(&zp->z_sync_cnt);
216 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
217 caller_context_t *ct)
219 znode_t *zp = VTOZ(vp);
220 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
223 * Clean up any locks held by this process on the vp.
225 cleanlocks(vp, ddi_get_pid(), 0);
226 cleanshares(vp, ddi_get_pid());
231 /* Decrement the synchronous opens in the znode */
232 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
233 atomic_dec_32(&zp->z_sync_cnt);
235 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
236 ZTOV(zp)->v_type == VREG &&
237 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
238 VERIFY(fs_vscan(vp, cr, 1) == 0);
245 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
246 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
249 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
251 znode_t *zp = VTOZ(vp);
252 uint64_t noff = (uint64_t)*off; /* new offset */
257 file_sz = zp->z_size;
258 if (noff >= file_sz) {
259 return (SET_ERROR(ENXIO));
262 if (cmd == _FIO_SEEK_HOLE)
267 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
270 return (SET_ERROR(ENXIO));
273 * We could find a hole that begins after the logical end-of-file,
274 * because dmu_offset_next() only works on whole blocks. If the
275 * EOF falls mid-block, then indicate that the "virtual hole"
276 * at the end of the file begins at the logical EOF, rather than
277 * at the end of the last block.
279 if (noff > file_sz) {
292 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
293 int *rvalp, caller_context_t *ct)
297 dmu_object_info_t doi;
308 * The following two ioctls are used by bfu. Faking out,
309 * necessary to avoid bfu errors.
322 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
323 return (SET_ERROR(EFAULT));
325 off = *(offset_t *)data;
328 zfsvfs = zp->z_zfsvfs;
332 /* offset parameter is in/out */
333 error = zfs_holey(vp, com, &off);
338 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
339 return (SET_ERROR(EFAULT));
341 *(offset_t *)data = off;
346 case _FIO_COUNT_FILLED:
349 * _FIO_COUNT_FILLED adds a new ioctl command which
350 * exposes the number of filled blocks in a
354 zfsvfs = zp->z_zfsvfs;
359 * Wait for all dirty blocks for this object
360 * to get synced out to disk, and the DMU info
363 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
370 * Retrieve fill count from DMU object.
372 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
378 ndata = doi.doi_fill_count;
381 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
382 return (SET_ERROR(EFAULT));
387 return (SET_ERROR(ENOTTY));
391 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
398 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
399 * aligned boundaries, if the range is not aligned. As a result a
400 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
401 * It may happen that all DEV_BSIZE subranges are marked clean and thus
402 * the whole page would be considred clean despite have some dirty data.
403 * For this reason we should shrink the range to DEV_BSIZE aligned
404 * boundaries before calling vm_page_clear_dirty.
406 end = rounddown2(off + nbytes, DEV_BSIZE);
407 off = roundup2(off, DEV_BSIZE);
411 zfs_vmobject_assert_wlocked(obj);
414 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
416 if (vm_page_xbusied(pp)) {
418 * Reference the page before unlocking and
419 * sleeping so that the page daemon is less
420 * likely to reclaim it.
422 vm_page_reference(pp);
424 zfs_vmobject_wunlock(obj);
425 vm_page_busy_sleep(pp, "zfsmwb", true);
426 zfs_vmobject_wlock(obj);
430 } else if (pp != NULL) {
436 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
437 vm_object_pip_add(obj, 1);
438 pmap_remove_write(pp);
440 vm_page_clear_dirty(pp, off, nbytes);
448 page_unbusy(vm_page_t pp)
452 vm_object_pip_subtract(pp->object, 1);
456 page_hold(vnode_t *vp, int64_t start)
462 zfs_vmobject_assert_wlocked(obj);
465 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
467 if (vm_page_xbusied(pp)) {
469 * Reference the page before unlocking and
470 * sleeping so that the page daemon is less
471 * likely to reclaim it.
473 vm_page_reference(pp);
475 zfs_vmobject_wunlock(obj);
476 vm_page_busy_sleep(pp, "zfsmwb", true);
477 zfs_vmobject_wlock(obj);
481 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
494 page_unhold(vm_page_t pp)
503 * When a file is memory mapped, we must keep the IO data synchronized
504 * between the DMU cache and the memory mapped pages. What this means:
506 * On Write: If we find a memory mapped page, we write to *both*
507 * the page and the dmu buffer.
510 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
511 int segflg, dmu_tx_t *tx)
518 ASSERT(segflg != UIO_NOCOPY);
519 ASSERT(vp->v_mount != NULL);
523 off = start & PAGEOFFSET;
524 zfs_vmobject_wlock(obj);
525 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
527 int nbytes = imin(PAGESIZE - off, len);
529 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
530 zfs_vmobject_wunlock(obj);
532 va = zfs_map_page(pp, &sf);
533 (void) dmu_read(os, oid, start+off, nbytes,
534 va+off, DMU_READ_PREFETCH);;
537 zfs_vmobject_wlock(obj);
543 vm_object_pip_wakeupn(obj, 0);
544 zfs_vmobject_wunlock(obj);
548 * Read with UIO_NOCOPY flag means that sendfile(2) requests
549 * ZFS to populate a range of page cache pages with data.
551 * NOTE: this function could be optimized to pre-allocate
552 * all pages in advance, drain exclusive busy on all of them,
553 * map them into contiguous KVA region and populate them
554 * in one single dmu_read() call.
557 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
559 znode_t *zp = VTOZ(vp);
560 objset_t *os = zp->z_zfsvfs->z_os;
570 ASSERT(uio->uio_segflg == UIO_NOCOPY);
571 ASSERT(vp->v_mount != NULL);
574 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
576 zfs_vmobject_wlock(obj);
577 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
578 int bytes = MIN(PAGESIZE, len);
580 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY |
581 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
582 if (pp->valid == 0) {
583 zfs_vmobject_wunlock(obj);
584 va = zfs_map_page(pp, &sf);
585 error = dmu_read(os, zp->z_id, start, bytes, va,
587 if (bytes != PAGESIZE && error == 0)
588 bzero(va + bytes, PAGESIZE - bytes);
590 zfs_vmobject_wlock(obj);
594 if (pp->wire_count == 0 && pp->valid == 0 &&
598 pp->valid = VM_PAGE_BITS_ALL;
599 vm_page_activate(pp);
603 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
608 uio->uio_resid -= bytes;
609 uio->uio_offset += bytes;
612 zfs_vmobject_wunlock(obj);
617 * When a file is memory mapped, we must keep the IO data synchronized
618 * between the DMU cache and the memory mapped pages. What this means:
620 * On Read: We "read" preferentially from memory mapped pages,
621 * else we default from the dmu buffer.
623 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
624 * the file is memory mapped.
627 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
629 znode_t *zp = VTOZ(vp);
637 ASSERT(vp->v_mount != NULL);
641 start = uio->uio_loffset;
642 off = start & PAGEOFFSET;
643 zfs_vmobject_wlock(obj);
644 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
646 uint64_t bytes = MIN(PAGESIZE - off, len);
648 if (pp = page_hold(vp, start)) {
652 zfs_vmobject_wunlock(obj);
653 va = zfs_map_page(pp, &sf);
655 error = uiomove(va + off, bytes, UIO_READ, uio);
657 error = vn_io_fault_uiomove(va + off, bytes, uio);
660 zfs_vmobject_wlock(obj);
663 zfs_vmobject_wunlock(obj);
664 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
666 zfs_vmobject_wlock(obj);
673 zfs_vmobject_wunlock(obj);
677 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
680 * Read bytes from specified file into supplied buffer.
682 * IN: vp - vnode of file to be read from.
683 * uio - structure supplying read location, range info,
685 * ioflag - SYNC flags; used to provide FRSYNC semantics.
686 * cr - credentials of caller.
687 * ct - caller context
689 * OUT: uio - updated offset and range, buffer filled.
691 * RETURN: 0 on success, error code on failure.
694 * vp - atime updated if byte count > 0
698 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
700 znode_t *zp = VTOZ(vp);
701 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
710 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
712 return (SET_ERROR(EACCES));
716 * Validate file offset
718 if (uio->uio_loffset < (offset_t)0) {
720 return (SET_ERROR(EINVAL));
724 * Fasttrack empty reads
726 if (uio->uio_resid == 0) {
732 * Check for mandatory locks
734 if (MANDMODE(zp->z_mode)) {
735 if (error = chklock(vp, FREAD,
736 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
743 * If we're in FRSYNC mode, sync out this znode before reading it.
746 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
747 zil_commit(zfsvfs->z_log, zp->z_id);
750 * Lock the range against changes.
752 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
755 * If we are reading past end-of-file we can skip
756 * to the end; but we might still need to set atime.
758 if (uio->uio_loffset >= zp->z_size) {
763 ASSERT(uio->uio_loffset < zp->z_size);
764 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
767 if ((uio->uio_extflg == UIO_XUIO) &&
768 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
770 int blksz = zp->z_blksz;
771 uint64_t offset = uio->uio_loffset;
773 xuio = (xuio_t *)uio;
775 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
778 ASSERT(offset + n <= blksz);
781 (void) dmu_xuio_init(xuio, nblk);
783 if (vn_has_cached_data(vp)) {
785 * For simplicity, we always allocate a full buffer
786 * even if we only expect to read a portion of a block.
788 while (--nblk >= 0) {
789 (void) dmu_xuio_add(xuio,
790 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
798 nbytes = MIN(n, zfs_read_chunk_size -
799 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
802 if (uio->uio_segflg == UIO_NOCOPY)
803 error = mappedread_sf(vp, nbytes, uio);
805 #endif /* __FreeBSD__ */
806 if (vn_has_cached_data(vp)) {
807 error = mappedread(vp, nbytes, uio);
809 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
813 /* convert checksum errors into IO errors */
815 error = SET_ERROR(EIO);
822 zfs_range_unlock(rl);
824 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
830 * Write the bytes to a file.
832 * IN: vp - vnode of file to be written to.
833 * uio - structure supplying write location, range info,
835 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
836 * set if in append mode.
837 * cr - credentials of caller.
838 * ct - caller context (NFS/CIFS fem monitor only)
840 * OUT: uio - updated offset and range.
842 * RETURN: 0 on success, error code on failure.
845 * vp - ctime|mtime updated if byte count > 0
850 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
852 znode_t *zp = VTOZ(vp);
853 rlim64_t limit = MAXOFFSET_T;
854 ssize_t start_resid = uio->uio_resid;
858 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
863 int max_blksz = zfsvfs->z_max_blksz;
866 iovec_t *aiov = NULL;
869 int iovcnt = uio->uio_iovcnt;
870 iovec_t *iovp = uio->uio_iov;
873 sa_bulk_attr_t bulk[4];
874 uint64_t mtime[2], ctime[2];
877 * Fasttrack empty write
883 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
889 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
890 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
891 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
893 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
897 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
898 * callers might not be able to detect properly that we are read-only,
899 * so check it explicitly here.
901 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
903 return (SET_ERROR(EROFS));
907 * If immutable or not appending then return EPERM
909 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
910 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
911 (uio->uio_loffset < zp->z_size))) {
913 return (SET_ERROR(EPERM));
916 zilog = zfsvfs->z_log;
919 * Validate file offset
921 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
924 return (SET_ERROR(EINVAL));
928 * Check for mandatory locks before calling zfs_range_lock()
929 * in order to prevent a deadlock with locks set via fcntl().
931 if (MANDMODE((mode_t)zp->z_mode) &&
932 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
939 * Pre-fault the pages to ensure slow (eg NFS) pages
941 * Skip this if uio contains loaned arc_buf.
943 if ((uio->uio_extflg == UIO_XUIO) &&
944 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
945 xuio = (xuio_t *)uio;
947 uio_prefaultpages(MIN(n, max_blksz), uio);
951 * If in append mode, set the io offset pointer to eof.
953 if (ioflag & FAPPEND) {
955 * Obtain an appending range lock to guarantee file append
956 * semantics. We reset the write offset once we have the lock.
958 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
960 if (rl->r_len == UINT64_MAX) {
962 * We overlocked the file because this write will cause
963 * the file block size to increase.
964 * Note that zp_size cannot change with this lock held.
968 uio->uio_loffset = woff;
971 * Note that if the file block size will change as a result of
972 * this write, then this range lock will lock the entire file
973 * so that we can re-write the block safely.
975 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
978 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
979 zfs_range_unlock(rl);
985 zfs_range_unlock(rl);
987 return (SET_ERROR(EFBIG));
990 if ((woff + n) > limit || woff > (limit - n))
993 /* Will this write extend the file length? */
994 write_eof = (woff + n > zp->z_size);
996 end_size = MAX(zp->z_size, woff + n);
999 * Write the file in reasonable size chunks. Each chunk is written
1000 * in a separate transaction; this keeps the intent log records small
1001 * and allows us to do more fine-grained space accounting.
1005 woff = uio->uio_loffset;
1006 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
1007 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
1009 dmu_return_arcbuf(abuf);
1010 error = SET_ERROR(EDQUOT);
1014 if (xuio && abuf == NULL) {
1015 ASSERT(i_iov < iovcnt);
1016 aiov = &iovp[i_iov];
1017 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1018 dmu_xuio_clear(xuio, i_iov);
1019 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1020 iovec_t *, aiov, arc_buf_t *, abuf);
1021 ASSERT((aiov->iov_base == abuf->b_data) ||
1022 ((char *)aiov->iov_base - (char *)abuf->b_data +
1023 aiov->iov_len == arc_buf_size(abuf)));
1025 } else if (abuf == NULL && n >= max_blksz &&
1026 woff >= zp->z_size &&
1027 P2PHASE(woff, max_blksz) == 0 &&
1028 zp->z_blksz == max_blksz) {
1030 * This write covers a full block. "Borrow" a buffer
1031 * from the dmu so that we can fill it before we enter
1032 * a transaction. This avoids the possibility of
1033 * holding up the transaction if the data copy hangs
1034 * up on a pagefault (e.g., from an NFS server mapping).
1040 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1042 ASSERT(abuf != NULL);
1043 ASSERT(arc_buf_size(abuf) == max_blksz);
1045 if (error = uiocopy(abuf->b_data, max_blksz,
1046 UIO_WRITE, uio, &cbytes)) {
1047 dmu_return_arcbuf(abuf);
1050 ASSERT(cbytes == max_blksz);
1052 ssize_t resid = uio->uio_resid;
1053 error = vn_io_fault_uiomove(abuf->b_data, max_blksz, uio);
1055 uio->uio_offset -= resid - uio->uio_resid;
1056 uio->uio_resid = resid;
1057 dmu_return_arcbuf(abuf);
1064 * Start a transaction.
1066 tx = dmu_tx_create(zfsvfs->z_os);
1067 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1068 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1069 zfs_sa_upgrade_txholds(tx, zp);
1070 error = dmu_tx_assign(tx, TXG_WAIT);
1074 dmu_return_arcbuf(abuf);
1079 * If zfs_range_lock() over-locked we grow the blocksize
1080 * and then reduce the lock range. This will only happen
1081 * on the first iteration since zfs_range_reduce() will
1082 * shrink down r_len to the appropriate size.
1084 if (rl->r_len == UINT64_MAX) {
1087 if (zp->z_blksz > max_blksz) {
1089 * File's blocksize is already larger than the
1090 * "recordsize" property. Only let it grow to
1091 * the next power of 2.
1093 ASSERT(!ISP2(zp->z_blksz));
1094 new_blksz = MIN(end_size,
1095 1 << highbit64(zp->z_blksz));
1097 new_blksz = MIN(end_size, max_blksz);
1099 zfs_grow_blocksize(zp, new_blksz, tx);
1100 zfs_range_reduce(rl, woff, n);
1104 * XXX - should we really limit each write to z_max_blksz?
1105 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1107 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1109 if (woff + nbytes > zp->z_size)
1110 vnode_pager_setsize(vp, woff + nbytes);
1113 tx_bytes = uio->uio_resid;
1114 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1116 tx_bytes -= uio->uio_resid;
1119 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1121 * If this is not a full block write, but we are
1122 * extending the file past EOF and this data starts
1123 * block-aligned, use assign_arcbuf(). Otherwise,
1124 * write via dmu_write().
1126 if (tx_bytes < max_blksz && (!write_eof ||
1127 aiov->iov_base != abuf->b_data)) {
1129 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1130 aiov->iov_len, aiov->iov_base, tx);
1131 dmu_return_arcbuf(abuf);
1132 xuio_stat_wbuf_copied();
1134 ASSERT(xuio || tx_bytes == max_blksz);
1135 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1139 ASSERT(tx_bytes <= uio->uio_resid);
1140 uioskip(uio, tx_bytes);
1143 if (tx_bytes && vn_has_cached_data(vp)) {
1144 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1145 zp->z_id, uio->uio_segflg, tx);
1149 * If we made no progress, we're done. If we made even
1150 * partial progress, update the znode and ZIL accordingly.
1152 if (tx_bytes == 0) {
1153 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1154 (void *)&zp->z_size, sizeof (uint64_t), tx);
1161 * Clear Set-UID/Set-GID bits on successful write if not
1162 * privileged and at least one of the excute bits is set.
1164 * It would be nice to to this after all writes have
1165 * been done, but that would still expose the ISUID/ISGID
1166 * to another app after the partial write is committed.
1168 * Note: we don't call zfs_fuid_map_id() here because
1169 * user 0 is not an ephemeral uid.
1171 mutex_enter(&zp->z_acl_lock);
1172 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1173 (S_IXUSR >> 6))) != 0 &&
1174 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1175 secpolicy_vnode_setid_retain(vp, cr,
1176 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1178 zp->z_mode &= ~(S_ISUID | S_ISGID);
1179 newmode = zp->z_mode;
1180 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1181 (void *)&newmode, sizeof (uint64_t), tx);
1183 mutex_exit(&zp->z_acl_lock);
1185 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1189 * Update the file size (zp_size) if it has changed;
1190 * account for possible concurrent updates.
1192 while ((end_size = zp->z_size) < uio->uio_loffset) {
1193 (void) atomic_cas_64(&zp->z_size, end_size,
1198 ASSERT(error == 0 || error == EFAULT);
1202 * If we are replaying and eof is non zero then force
1203 * the file size to the specified eof. Note, there's no
1204 * concurrency during replay.
1206 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1207 zp->z_size = zfsvfs->z_replay_eof;
1210 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1212 (void) sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1214 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1219 ASSERT(tx_bytes == nbytes);
1224 uio_prefaultpages(MIN(n, max_blksz), uio);
1228 zfs_range_unlock(rl);
1231 * If we're in replay mode, or we made no progress, return error.
1232 * Otherwise, it's at least a partial write, so it's successful.
1234 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1241 * EFAULT means that at least one page of the source buffer was not
1242 * available. VFS will re-try remaining I/O upon this error.
1244 if (error == EFAULT) {
1250 if (ioflag & (FSYNC | FDSYNC) ||
1251 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1252 zil_commit(zilog, zp->z_id);
1259 zfs_get_done(zgd_t *zgd, int error)
1261 znode_t *zp = zgd->zgd_private;
1262 objset_t *os = zp->z_zfsvfs->z_os;
1265 dmu_buf_rele(zgd->zgd_db, zgd);
1267 zfs_range_unlock(zgd->zgd_rl);
1270 * Release the vnode asynchronously as we currently have the
1271 * txg stopped from syncing.
1273 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1275 if (error == 0 && zgd->zgd_bp)
1276 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1278 kmem_free(zgd, sizeof (zgd_t));
1282 static int zil_fault_io = 0;
1286 * Get data to generate a TX_WRITE intent log record.
1289 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1291 zfsvfs_t *zfsvfs = arg;
1292 objset_t *os = zfsvfs->z_os;
1294 uint64_t object = lr->lr_foid;
1295 uint64_t offset = lr->lr_offset;
1296 uint64_t size = lr->lr_length;
1297 blkptr_t *bp = &lr->lr_blkptr;
1302 ASSERT(zio != NULL);
1306 * Nothing to do if the file has been removed
1308 if (zfs_zget(zfsvfs, object, &zp) != 0)
1309 return (SET_ERROR(ENOENT));
1310 if (zp->z_unlinked) {
1312 * Release the vnode asynchronously as we currently have the
1313 * txg stopped from syncing.
1315 VN_RELE_ASYNC(ZTOV(zp),
1316 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1317 return (SET_ERROR(ENOENT));
1320 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1321 zgd->zgd_zilog = zfsvfs->z_log;
1322 zgd->zgd_private = zp;
1325 * Write records come in two flavors: immediate and indirect.
1326 * For small writes it's cheaper to store the data with the
1327 * log record (immediate); for large writes it's cheaper to
1328 * sync the data and get a pointer to it (indirect) so that
1329 * we don't have to write the data twice.
1331 if (buf != NULL) { /* immediate write */
1332 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1333 /* test for truncation needs to be done while range locked */
1334 if (offset >= zp->z_size) {
1335 error = SET_ERROR(ENOENT);
1337 error = dmu_read(os, object, offset, size, buf,
1338 DMU_READ_NO_PREFETCH);
1340 ASSERT(error == 0 || error == ENOENT);
1341 } else { /* indirect write */
1343 * Have to lock the whole block to ensure when it's
1344 * written out and it's checksum is being calculated
1345 * that no one can change the data. We need to re-check
1346 * blocksize after we get the lock in case it's changed!
1351 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1353 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1355 if (zp->z_blksz == size)
1358 zfs_range_unlock(zgd->zgd_rl);
1360 /* test for truncation needs to be done while range locked */
1361 if (lr->lr_offset >= zp->z_size)
1362 error = SET_ERROR(ENOENT);
1365 error = SET_ERROR(EIO);
1370 error = dmu_buf_hold(os, object, offset, zgd, &db,
1371 DMU_READ_NO_PREFETCH);
1374 blkptr_t *obp = dmu_buf_get_blkptr(db);
1376 ASSERT(BP_IS_HOLE(bp));
1383 ASSERT(db->db_offset == offset);
1384 ASSERT(db->db_size == size);
1386 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1388 ASSERT(error || lr->lr_length <= zp->z_blksz);
1391 * On success, we need to wait for the write I/O
1392 * initiated by dmu_sync() to complete before we can
1393 * release this dbuf. We will finish everything up
1394 * in the zfs_get_done() callback.
1399 if (error == EALREADY) {
1400 lr->lr_common.lrc_txtype = TX_WRITE2;
1406 zfs_get_done(zgd, error);
1413 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1414 caller_context_t *ct)
1416 znode_t *zp = VTOZ(vp);
1417 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1423 if (flag & V_ACE_MASK)
1424 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1426 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1433 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1438 error = vn_lock(*vpp, lkflags);
1445 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
1447 znode_t *zdp = VTOZ(dvp);
1448 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1452 ASSERT_VOP_LOCKED(dvp, __func__);
1454 if ((zdp->z_pflags & ZFS_XATTR) == 0)
1455 VERIFY(!RRM_LOCK_HELD(&zfsvfs->z_teardown_lock));
1458 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
1459 ASSERT3P(dvp, ==, vp);
1461 ltype = lkflags & LK_TYPE_MASK;
1462 if (ltype != VOP_ISLOCKED(dvp)) {
1463 if (ltype == LK_EXCLUSIVE)
1464 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1465 else /* if (ltype == LK_SHARED) */
1466 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1469 * Relock for the "." case could leave us with
1472 if (dvp->v_iflag & VI_DOOMED) {
1474 return (SET_ERROR(ENOENT));
1478 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
1480 * Note that in this case, dvp is the child vnode, and we
1481 * are looking up the parent vnode - exactly reverse from
1482 * normal operation. Unlocking dvp requires some rather
1483 * tricky unlock/relock dance to prevent mp from being freed;
1484 * use vn_vget_ino_gen() which takes care of all that.
1486 * XXX Note that there is a time window when both vnodes are
1487 * unlocked. It is possible, although highly unlikely, that
1488 * during that window the parent-child relationship between
1489 * the vnodes may change, for example, get reversed.
1490 * In that case we would have a wrong lock order for the vnodes.
1491 * All other filesystems seem to ignore this problem, so we
1493 * A potential solution could be implemented as follows:
1494 * - using LK_NOWAIT when locking the second vnode and retrying
1496 * - checking that the parent-child relationship still holds
1497 * after locking both vnodes and retrying if it doesn't
1499 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
1502 error = vn_lock(vp, lkflags);
1510 * Lookup an entry in a directory, or an extended attribute directory.
1511 * If it exists, return a held vnode reference for it.
1513 * IN: dvp - vnode of directory to search.
1514 * nm - name of entry to lookup.
1515 * pnp - full pathname to lookup [UNUSED].
1516 * flags - LOOKUP_XATTR set if looking for an attribute.
1517 * rdir - root directory vnode [UNUSED].
1518 * cr - credentials of caller.
1519 * ct - caller context
1521 * OUT: vpp - vnode of located entry, NULL if not found.
1523 * RETURN: 0 on success, error code on failure.
1530 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1531 int nameiop, cred_t *cr, kthread_t *td, int flags)
1533 znode_t *zdp = VTOZ(dvp);
1535 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1538 /* fast path (should be redundant with vfs namecache) */
1539 if (!(flags & LOOKUP_XATTR)) {
1540 if (dvp->v_type != VDIR) {
1541 return (SET_ERROR(ENOTDIR));
1542 } else if (zdp->z_sa_hdl == NULL) {
1543 return (SET_ERROR(EIO));
1547 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1554 if (flags & LOOKUP_XATTR) {
1557 * If the xattr property is off, refuse the lookup request.
1559 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1561 return (SET_ERROR(EINVAL));
1566 * We don't allow recursive attributes..
1567 * Maybe someday we will.
1569 if (zdp->z_pflags & ZFS_XATTR) {
1571 return (SET_ERROR(EINVAL));
1574 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1580 * Do we have permission to get into attribute directory?
1582 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1593 * Check accessibility of directory.
1595 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1600 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1601 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1603 return (SET_ERROR(EILSEQ));
1608 * First handle the special cases.
1610 if ((cnp->cn_flags & ISDOTDOT) != 0) {
1612 * If we are a snapshot mounted under .zfs, return
1613 * the vp for the snapshot directory.
1615 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
1616 struct componentname cn;
1621 ltype = VOP_ISLOCKED(dvp);
1623 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
1626 cn.cn_nameptr = "snapshot";
1627 cn.cn_namelen = strlen(cn.cn_nameptr);
1628 cn.cn_nameiop = cnp->cn_nameiop;
1629 cn.cn_flags = cnp->cn_flags;
1630 cn.cn_lkflags = cnp->cn_lkflags;
1631 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
1634 vn_lock(dvp, ltype | LK_RETRY);
1638 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
1640 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
1641 return (SET_ERROR(ENOTSUP));
1642 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
1647 * The loop is retry the lookup if the parent-child relationship
1648 * changes during the dot-dot locking complexities.
1653 error = zfs_dirlook(zdp, nm, &zp);
1661 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
1664 * If we've got a locking error, then the vnode
1665 * got reclaimed because of a force unmount.
1666 * We never enter doomed vnodes into the name cache.
1672 if ((cnp->cn_flags & ISDOTDOT) == 0)
1676 if (zdp->z_sa_hdl == NULL) {
1677 error = SET_ERROR(EIO);
1679 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1680 &parent, sizeof (parent));
1687 if (zp->z_id == parent) {
1698 /* Translate errors and add SAVENAME when needed. */
1699 if (cnp->cn_flags & ISLASTCN) {
1703 if (error == ENOENT) {
1704 error = EJUSTRETURN;
1705 cnp->cn_flags |= SAVENAME;
1711 cnp->cn_flags |= SAVENAME;
1716 /* Insert name into cache (as non-existent) if appropriate. */
1717 if (zfsvfs->z_use_namecache &&
1718 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1719 cache_enter(dvp, NULL, cnp);
1721 /* Insert name into cache if appropriate. */
1722 if (zfsvfs->z_use_namecache &&
1723 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1724 if (!(cnp->cn_flags & ISLASTCN) ||
1725 (nameiop != DELETE && nameiop != RENAME)) {
1726 cache_enter(dvp, *vpp, cnp);
1734 * Attempt to create a new entry in a directory. If the entry
1735 * already exists, truncate the file if permissible, else return
1736 * an error. Return the vp of the created or trunc'd file.
1738 * IN: dvp - vnode of directory to put new file entry in.
1739 * name - name of new file entry.
1740 * vap - attributes of new file.
1741 * excl - flag indicating exclusive or non-exclusive mode.
1742 * mode - mode to open file with.
1743 * cr - credentials of caller.
1744 * flag - large file flag [UNUSED].
1745 * ct - caller context
1746 * vsecp - ACL to be set
1748 * OUT: vpp - vnode of created or trunc'd entry.
1750 * RETURN: 0 on success, error code on failure.
1753 * dvp - ctime|mtime updated if new entry created
1754 * vp - ctime|mtime always, atime if new
1759 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1760 vnode_t **vpp, cred_t *cr, kthread_t *td)
1762 znode_t *zp, *dzp = VTOZ(dvp);
1763 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1770 gid_t gid = crgetgid(cr);
1771 zfs_acl_ids_t acl_ids;
1772 boolean_t fuid_dirtied;
1778 * If we have an ephemeral id, ACL, or XVATTR then
1779 * make sure file system is at proper version
1782 ksid = crgetsid(cr, KSID_OWNER);
1784 uid = ksid_getid(ksid);
1788 if (zfsvfs->z_use_fuids == B_FALSE &&
1789 (vsecp || (vap->va_mask & AT_XVATTR) ||
1790 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1791 return (SET_ERROR(EINVAL));
1796 zilog = zfsvfs->z_log;
1798 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1799 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1801 return (SET_ERROR(EILSEQ));
1804 if (vap->va_mask & AT_XVATTR) {
1805 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1806 crgetuid(cr), cr, vap->va_type)) != 0) {
1814 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1815 vap->va_mode &= ~S_ISVTX;
1817 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1822 ASSERT3P(zp, ==, NULL);
1825 * Create a new file object and update the directory
1828 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1833 * We only support the creation of regular files in
1834 * extended attribute directories.
1837 if ((dzp->z_pflags & ZFS_XATTR) &&
1838 (vap->va_type != VREG)) {
1839 error = SET_ERROR(EINVAL);
1843 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1844 cr, vsecp, &acl_ids)) != 0)
1847 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1848 zfs_acl_ids_free(&acl_ids);
1849 error = SET_ERROR(EDQUOT);
1853 getnewvnode_reserve(1);
1855 tx = dmu_tx_create(os);
1857 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1858 ZFS_SA_BASE_ATTR_SIZE);
1860 fuid_dirtied = zfsvfs->z_fuid_dirty;
1862 zfs_fuid_txhold(zfsvfs, tx);
1863 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1864 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1865 if (!zfsvfs->z_use_sa &&
1866 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1867 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1868 0, acl_ids.z_aclp->z_acl_bytes);
1870 error = dmu_tx_assign(tx, TXG_WAIT);
1872 zfs_acl_ids_free(&acl_ids);
1874 getnewvnode_drop_reserve();
1878 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1881 zfs_fuid_sync(zfsvfs, tx);
1883 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1884 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1885 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1886 vsecp, acl_ids.z_fuidp, vap);
1887 zfs_acl_ids_free(&acl_ids);
1890 getnewvnode_drop_reserve();
1897 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1898 zil_commit(zilog, 0);
1905 * Remove an entry from a directory.
1907 * IN: dvp - vnode of directory to remove entry from.
1908 * name - name of entry to remove.
1909 * cr - credentials of caller.
1910 * ct - caller context
1911 * flags - case flags
1913 * RETURN: 0 on success, error code on failure.
1917 * vp - ctime (if nlink > 0)
1922 zfs_remove(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
1924 znode_t *dzp = VTOZ(dvp);
1925 znode_t *zp = VTOZ(vp);
1927 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1929 uint64_t acl_obj, xattr_obj;
1932 boolean_t unlinked, toobig = FALSE;
1939 zilog = zfsvfs->z_log;
1945 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1950 * Need to use rmdir for removing directories.
1952 if (vp->v_type == VDIR) {
1953 error = SET_ERROR(EPERM);
1957 vnevent_remove(vp, dvp, name, ct);
1961 /* are there any extended attributes? */
1962 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1963 &xattr_obj, sizeof (xattr_obj));
1964 if (error == 0 && xattr_obj) {
1965 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1970 * We may delete the znode now, or we may put it in the unlinked set;
1971 * it depends on whether we're the last link, and on whether there are
1972 * other holds on the vnode. So we dmu_tx_hold() the right things to
1973 * allow for either case.
1975 tx = dmu_tx_create(zfsvfs->z_os);
1976 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1977 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1978 zfs_sa_upgrade_txholds(tx, zp);
1979 zfs_sa_upgrade_txholds(tx, dzp);
1982 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1983 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1986 /* charge as an update -- would be nice not to charge at all */
1987 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1990 * Mark this transaction as typically resulting in a net free of space
1992 dmu_tx_mark_netfree(tx);
1994 error = dmu_tx_assign(tx, TXG_WAIT);
2002 * Remove the directory entry.
2004 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
2012 zfs_unlinked_add(zp, tx);
2013 vp->v_vflag |= VV_NOSYNC;
2017 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2025 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2026 zil_commit(zilog, 0);
2033 * Create a new directory and insert it into dvp using the name
2034 * provided. Return a pointer to the inserted directory.
2036 * IN: dvp - vnode of directory to add subdir to.
2037 * dirname - name of new directory.
2038 * vap - attributes of new directory.
2039 * cr - credentials of caller.
2040 * ct - caller context
2041 * flags - case flags
2042 * vsecp - ACL to be set
2044 * OUT: vpp - vnode of created directory.
2046 * RETURN: 0 on success, error code on failure.
2049 * dvp - ctime|mtime updated
2050 * vp - ctime|mtime|atime updated
2054 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
2056 znode_t *zp, *dzp = VTOZ(dvp);
2057 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2064 gid_t gid = crgetgid(cr);
2065 zfs_acl_ids_t acl_ids;
2066 boolean_t fuid_dirtied;
2068 ASSERT(vap->va_type == VDIR);
2071 * If we have an ephemeral id, ACL, or XVATTR then
2072 * make sure file system is at proper version
2075 ksid = crgetsid(cr, KSID_OWNER);
2077 uid = ksid_getid(ksid);
2080 if (zfsvfs->z_use_fuids == B_FALSE &&
2081 ((vap->va_mask & AT_XVATTR) ||
2082 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2083 return (SET_ERROR(EINVAL));
2087 zilog = zfsvfs->z_log;
2089 if (dzp->z_pflags & ZFS_XATTR) {
2091 return (SET_ERROR(EINVAL));
2094 if (zfsvfs->z_utf8 && u8_validate(dirname,
2095 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2097 return (SET_ERROR(EILSEQ));
2100 if (vap->va_mask & AT_XVATTR) {
2101 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2102 crgetuid(cr), cr, vap->va_type)) != 0) {
2108 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2109 NULL, &acl_ids)) != 0) {
2115 * First make sure the new directory doesn't exist.
2117 * Existence is checked first to make sure we don't return
2118 * EACCES instead of EEXIST which can cause some applications
2123 if (error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW)) {
2124 zfs_acl_ids_free(&acl_ids);
2128 ASSERT3P(zp, ==, NULL);
2130 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2131 zfs_acl_ids_free(&acl_ids);
2136 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2137 zfs_acl_ids_free(&acl_ids);
2139 return (SET_ERROR(EDQUOT));
2143 * Add a new entry to the directory.
2145 getnewvnode_reserve(1);
2146 tx = dmu_tx_create(zfsvfs->z_os);
2147 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2148 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2149 fuid_dirtied = zfsvfs->z_fuid_dirty;
2151 zfs_fuid_txhold(zfsvfs, tx);
2152 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2153 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2154 acl_ids.z_aclp->z_acl_bytes);
2157 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2158 ZFS_SA_BASE_ATTR_SIZE);
2160 error = dmu_tx_assign(tx, TXG_WAIT);
2162 zfs_acl_ids_free(&acl_ids);
2164 getnewvnode_drop_reserve();
2172 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2175 zfs_fuid_sync(zfsvfs, tx);
2178 * Now put new name in parent dir.
2180 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
2184 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
2185 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
2186 acl_ids.z_fuidp, vap);
2188 zfs_acl_ids_free(&acl_ids);
2192 getnewvnode_drop_reserve();
2194 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2195 zil_commit(zilog, 0);
2202 * Remove a directory subdir entry. If the current working
2203 * directory is the same as the subdir to be removed, the
2206 * IN: dvp - vnode of directory to remove from.
2207 * name - name of directory to be removed.
2208 * cwd - vnode of current working directory.
2209 * cr - credentials of caller.
2210 * ct - caller context
2211 * flags - case flags
2213 * RETURN: 0 on success, error code on failure.
2216 * dvp - ctime|mtime updated
2220 zfs_rmdir(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
2222 znode_t *dzp = VTOZ(dvp);
2223 znode_t *zp = VTOZ(vp);
2224 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2232 zilog = zfsvfs->z_log;
2235 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2239 if (vp->v_type != VDIR) {
2240 error = SET_ERROR(ENOTDIR);
2244 vnevent_rmdir(vp, dvp, name, ct);
2246 tx = dmu_tx_create(zfsvfs->z_os);
2247 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2248 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2249 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2250 zfs_sa_upgrade_txholds(tx, zp);
2251 zfs_sa_upgrade_txholds(tx, dzp);
2252 dmu_tx_mark_netfree(tx);
2253 error = dmu_tx_assign(tx, TXG_WAIT);
2262 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
2265 uint64_t txtype = TX_RMDIR;
2266 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2273 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2274 zil_commit(zilog, 0);
2281 * Read as many directory entries as will fit into the provided
2282 * buffer from the given directory cursor position (specified in
2283 * the uio structure).
2285 * IN: vp - vnode of directory to read.
2286 * uio - structure supplying read location, range info,
2287 * and return buffer.
2288 * cr - credentials of caller.
2289 * ct - caller context
2290 * flags - case flags
2292 * OUT: uio - updated offset and range, buffer filled.
2293 * eofp - set to true if end-of-file detected.
2295 * RETURN: 0 on success, error code on failure.
2298 * vp - atime updated
2300 * Note that the low 4 bits of the cookie returned by zap is always zero.
2301 * This allows us to use the low range for "special" directory entries:
2302 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2303 * we use the offset 2 for the '.zfs' directory.
2307 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2309 znode_t *zp = VTOZ(vp);
2313 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2318 zap_attribute_t zap;
2319 uint_t bytes_wanted;
2320 uint64_t offset; /* must be unsigned; checks for < 1 */
2326 boolean_t check_sysattrs;
2329 u_long *cooks = NULL;
2335 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2336 &parent, sizeof (parent))) != 0) {
2342 * If we are not given an eof variable,
2349 * Check for valid iov_len.
2351 if (uio->uio_iov->iov_len <= 0) {
2353 return (SET_ERROR(EINVAL));
2357 * Quit if directory has been removed (posix)
2359 if ((*eofp = zp->z_unlinked) != 0) {
2366 offset = uio->uio_loffset;
2367 prefetch = zp->z_zn_prefetch;
2370 * Initialize the iterator cursor.
2374 * Start iteration from the beginning of the directory.
2376 zap_cursor_init(&zc, os, zp->z_id);
2379 * The offset is a serialized cursor.
2381 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2385 * Get space to change directory entries into fs independent format.
2387 iovp = uio->uio_iov;
2388 bytes_wanted = iovp->iov_len;
2389 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2390 bufsize = bytes_wanted;
2391 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2392 odp = (struct dirent64 *)outbuf;
2394 bufsize = bytes_wanted;
2396 odp = (struct dirent64 *)iovp->iov_base;
2398 eodp = (struct edirent *)odp;
2400 if (ncookies != NULL) {
2402 * Minimum entry size is dirent size and 1 byte for a file name.
2404 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2405 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2410 * If this VFS supports the system attribute view interface; and
2411 * we're looking at an extended attribute directory; and we care
2412 * about normalization conflicts on this vfs; then we must check
2413 * for normalization conflicts with the sysattr name space.
2416 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2417 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2418 (flags & V_RDDIR_ENTFLAGS);
2424 * Transform to file-system independent format
2427 while (outcount < bytes_wanted) {
2430 off64_t *next = NULL;
2433 * Special case `.', `..', and `.zfs'.
2436 (void) strcpy(zap.za_name, ".");
2437 zap.za_normalization_conflict = 0;
2440 } else if (offset == 1) {
2441 (void) strcpy(zap.za_name, "..");
2442 zap.za_normalization_conflict = 0;
2445 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2446 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2447 zap.za_normalization_conflict = 0;
2448 objnum = ZFSCTL_INO_ROOT;
2454 if (error = zap_cursor_retrieve(&zc, &zap)) {
2455 if ((*eofp = (error == ENOENT)) != 0)
2461 if (zap.za_integer_length != 8 ||
2462 zap.za_num_integers != 1) {
2463 cmn_err(CE_WARN, "zap_readdir: bad directory "
2464 "entry, obj = %lld, offset = %lld\n",
2465 (u_longlong_t)zp->z_id,
2466 (u_longlong_t)offset);
2467 error = SET_ERROR(ENXIO);
2471 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2473 * MacOS X can extract the object type here such as:
2474 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2476 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2478 if (check_sysattrs && !zap.za_normalization_conflict) {
2480 zap.za_normalization_conflict =
2481 xattr_sysattr_casechk(zap.za_name);
2483 panic("%s:%u: TODO", __func__, __LINE__);
2488 if (flags & V_RDDIR_ACCFILTER) {
2490 * If we have no access at all, don't include
2491 * this entry in the returned information
2494 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2496 if (!zfs_has_access(ezp, cr)) {
2503 if (flags & V_RDDIR_ENTFLAGS)
2504 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2506 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2509 * Will this entry fit in the buffer?
2511 if (outcount + reclen > bufsize) {
2513 * Did we manage to fit anything in the buffer?
2516 error = SET_ERROR(EINVAL);
2521 if (flags & V_RDDIR_ENTFLAGS) {
2523 * Add extended flag entry:
2525 eodp->ed_ino = objnum;
2526 eodp->ed_reclen = reclen;
2527 /* NOTE: ed_off is the offset for the *next* entry */
2528 next = &(eodp->ed_off);
2529 eodp->ed_eflags = zap.za_normalization_conflict ?
2530 ED_CASE_CONFLICT : 0;
2531 (void) strncpy(eodp->ed_name, zap.za_name,
2532 EDIRENT_NAMELEN(reclen));
2533 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2538 odp->d_ino = objnum;
2539 odp->d_reclen = reclen;
2540 odp->d_namlen = strlen(zap.za_name);
2541 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2543 odp = (dirent64_t *)((intptr_t)odp + reclen);
2547 ASSERT(outcount <= bufsize);
2549 /* Prefetch znode */
2551 dmu_prefetch(os, objnum, 0, 0, 0,
2552 ZIO_PRIORITY_SYNC_READ);
2556 * Move to the next entry, fill in the previous offset.
2558 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2559 zap_cursor_advance(&zc);
2560 offset = zap_cursor_serialize(&zc);
2565 if (cooks != NULL) {
2568 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2571 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2573 /* Subtract unused cookies */
2574 if (ncookies != NULL)
2575 *ncookies -= ncooks;
2577 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2578 iovp->iov_base += outcount;
2579 iovp->iov_len -= outcount;
2580 uio->uio_resid -= outcount;
2581 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2583 * Reset the pointer.
2585 offset = uio->uio_loffset;
2589 zap_cursor_fini(&zc);
2590 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2591 kmem_free(outbuf, bufsize);
2593 if (error == ENOENT)
2596 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2598 uio->uio_loffset = offset;
2600 if (error != 0 && cookies != NULL) {
2601 free(*cookies, M_TEMP);
2608 ulong_t zfs_fsync_sync_cnt = 4;
2611 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2613 znode_t *zp = VTOZ(vp);
2614 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2616 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2618 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2621 zil_commit(zfsvfs->z_log, zp->z_id);
2629 * Get the requested file attributes and place them in the provided
2632 * IN: vp - vnode of file.
2633 * vap - va_mask identifies requested attributes.
2634 * If AT_XVATTR set, then optional attrs are requested
2635 * flags - ATTR_NOACLCHECK (CIFS server context)
2636 * cr - credentials of caller.
2637 * ct - caller context
2639 * OUT: vap - attribute values.
2641 * RETURN: 0 (always succeeds).
2645 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2646 caller_context_t *ct)
2648 znode_t *zp = VTOZ(vp);
2649 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2652 u_longlong_t nblocks;
2654 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2655 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2656 xoptattr_t *xoap = NULL;
2657 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2658 sa_bulk_attr_t bulk[4];
2664 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2666 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2667 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2668 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2669 if (vp->v_type == VBLK || vp->v_type == VCHR)
2670 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2673 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2679 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2680 * Also, if we are the owner don't bother, since owner should
2681 * always be allowed to read basic attributes of file.
2683 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2684 (vap->va_uid != crgetuid(cr))) {
2685 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2693 * Return all attributes. It's cheaper to provide the answer
2694 * than to determine whether we were asked the question.
2697 vap->va_type = IFTOVT(zp->z_mode);
2698 vap->va_mode = zp->z_mode & ~S_IFMT;
2700 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2702 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2704 vap->va_nodeid = zp->z_id;
2705 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2706 links = zp->z_links + 1;
2708 links = zp->z_links;
2709 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2710 vap->va_size = zp->z_size;
2712 vap->va_rdev = vp->v_rdev;
2714 if (vp->v_type == VBLK || vp->v_type == VCHR)
2715 vap->va_rdev = zfs_cmpldev(rdev);
2717 vap->va_seq = zp->z_seq;
2718 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2719 vap->va_filerev = zp->z_seq;
2722 * Add in any requested optional attributes and the create time.
2723 * Also set the corresponding bits in the returned attribute bitmap.
2725 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2726 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2728 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2729 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2732 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2733 xoap->xoa_readonly =
2734 ((zp->z_pflags & ZFS_READONLY) != 0);
2735 XVA_SET_RTN(xvap, XAT_READONLY);
2738 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2740 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2741 XVA_SET_RTN(xvap, XAT_SYSTEM);
2744 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2746 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2747 XVA_SET_RTN(xvap, XAT_HIDDEN);
2750 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2751 xoap->xoa_nounlink =
2752 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2753 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2756 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2757 xoap->xoa_immutable =
2758 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2759 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2762 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2763 xoap->xoa_appendonly =
2764 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2765 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2768 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2770 ((zp->z_pflags & ZFS_NODUMP) != 0);
2771 XVA_SET_RTN(xvap, XAT_NODUMP);
2774 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2776 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2777 XVA_SET_RTN(xvap, XAT_OPAQUE);
2780 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2781 xoap->xoa_av_quarantined =
2782 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2783 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2786 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2787 xoap->xoa_av_modified =
2788 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2789 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2792 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2793 vp->v_type == VREG) {
2794 zfs_sa_get_scanstamp(zp, xvap);
2797 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2798 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2799 XVA_SET_RTN(xvap, XAT_REPARSE);
2801 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2802 xoap->xoa_generation = zp->z_gen;
2803 XVA_SET_RTN(xvap, XAT_GEN);
2806 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2808 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2809 XVA_SET_RTN(xvap, XAT_OFFLINE);
2812 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2814 ((zp->z_pflags & ZFS_SPARSE) != 0);
2815 XVA_SET_RTN(xvap, XAT_SPARSE);
2819 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2820 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2821 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2822 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2825 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2826 vap->va_blksize = blksize;
2827 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2829 if (zp->z_blksz == 0) {
2831 * Block size hasn't been set; suggest maximal I/O transfers.
2833 vap->va_blksize = zfsvfs->z_max_blksz;
2841 * Set the file attributes to the values contained in the
2844 * IN: vp - vnode of file to be modified.
2845 * vap - new attribute values.
2846 * If AT_XVATTR set, then optional attrs are being set
2847 * flags - ATTR_UTIME set if non-default time values provided.
2848 * - ATTR_NOACLCHECK (CIFS context only).
2849 * cr - credentials of caller.
2850 * ct - caller context
2852 * RETURN: 0 on success, error code on failure.
2855 * vp - ctime updated, mtime updated if size changed.
2859 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2860 caller_context_t *ct)
2862 znode_t *zp = VTOZ(vp);
2863 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2868 uint_t mask = vap->va_mask;
2869 uint_t saved_mask = 0;
2870 uint64_t saved_mode;
2873 uint64_t new_uid, new_gid;
2875 uint64_t mtime[2], ctime[2];
2877 int need_policy = FALSE;
2879 zfs_fuid_info_t *fuidp = NULL;
2880 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2883 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2884 boolean_t fuid_dirtied = B_FALSE;
2885 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2886 int count = 0, xattr_count = 0;
2891 if (mask & AT_NOSET)
2892 return (SET_ERROR(EINVAL));
2897 zilog = zfsvfs->z_log;
2900 * Make sure that if we have ephemeral uid/gid or xvattr specified
2901 * that file system is at proper version level
2904 if (zfsvfs->z_use_fuids == B_FALSE &&
2905 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2906 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2907 (mask & AT_XVATTR))) {
2909 return (SET_ERROR(EINVAL));
2912 if (mask & AT_SIZE && vp->v_type == VDIR) {
2914 return (SET_ERROR(EISDIR));
2917 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2919 return (SET_ERROR(EINVAL));
2923 * If this is an xvattr_t, then get a pointer to the structure of
2924 * optional attributes. If this is NULL, then we have a vattr_t.
2926 xoap = xva_getxoptattr(xvap);
2928 xva_init(&tmpxvattr);
2931 * Immutable files can only alter immutable bit and atime
2933 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2934 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2935 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2937 return (SET_ERROR(EPERM));
2940 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2942 return (SET_ERROR(EPERM));
2946 * Verify timestamps doesn't overflow 32 bits.
2947 * ZFS can handle large timestamps, but 32bit syscalls can't
2948 * handle times greater than 2039. This check should be removed
2949 * once large timestamps are fully supported.
2951 if (mask & (AT_ATIME | AT_MTIME)) {
2952 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2953 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2955 return (SET_ERROR(EOVERFLOW));
2958 if (xoap && (mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2959 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2961 return (SET_ERROR(EOVERFLOW));
2967 /* Can this be moved to before the top label? */
2968 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2970 return (SET_ERROR(EROFS));
2974 * First validate permissions
2977 if (mask & AT_SIZE) {
2979 * XXX - Note, we are not providing any open
2980 * mode flags here (like FNDELAY), so we may
2981 * block if there are locks present... this
2982 * should be addressed in openat().
2984 /* XXX - would it be OK to generate a log record here? */
2985 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2992 if (mask & (AT_ATIME|AT_MTIME) ||
2993 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2994 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2995 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2996 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2997 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2998 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2999 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3000 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3004 if (mask & (AT_UID|AT_GID)) {
3005 int idmask = (mask & (AT_UID|AT_GID));
3010 * NOTE: even if a new mode is being set,
3011 * we may clear S_ISUID/S_ISGID bits.
3014 if (!(mask & AT_MODE))
3015 vap->va_mode = zp->z_mode;
3018 * Take ownership or chgrp to group we are a member of
3021 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3022 take_group = (mask & AT_GID) &&
3023 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3026 * If both AT_UID and AT_GID are set then take_owner and
3027 * take_group must both be set in order to allow taking
3030 * Otherwise, send the check through secpolicy_vnode_setattr()
3034 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3035 ((idmask == AT_UID) && take_owner) ||
3036 ((idmask == AT_GID) && take_group)) {
3037 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3038 skipaclchk, cr) == 0) {
3040 * Remove setuid/setgid for non-privileged users
3042 secpolicy_setid_clear(vap, vp, cr);
3043 trim_mask = (mask & (AT_UID|AT_GID));
3052 oldva.va_mode = zp->z_mode;
3053 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3054 if (mask & AT_XVATTR) {
3056 * Update xvattr mask to include only those attributes
3057 * that are actually changing.
3059 * the bits will be restored prior to actually setting
3060 * the attributes so the caller thinks they were set.
3062 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3063 if (xoap->xoa_appendonly !=
3064 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3067 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3068 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3072 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3073 if (xoap->xoa_nounlink !=
3074 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3077 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3078 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3082 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3083 if (xoap->xoa_immutable !=
3084 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3087 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3088 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3092 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3093 if (xoap->xoa_nodump !=
3094 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3097 XVA_CLR_REQ(xvap, XAT_NODUMP);
3098 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3102 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3103 if (xoap->xoa_av_modified !=
3104 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3107 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3108 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3112 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3113 if ((vp->v_type != VREG &&
3114 xoap->xoa_av_quarantined) ||
3115 xoap->xoa_av_quarantined !=
3116 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3119 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3120 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3124 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3126 return (SET_ERROR(EPERM));
3129 if (need_policy == FALSE &&
3130 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3131 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3136 if (mask & AT_MODE) {
3137 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3138 err = secpolicy_setid_setsticky_clear(vp, vap,
3144 trim_mask |= AT_MODE;
3152 * If trim_mask is set then take ownership
3153 * has been granted or write_acl is present and user
3154 * has the ability to modify mode. In that case remove
3155 * UID|GID and or MODE from mask so that
3156 * secpolicy_vnode_setattr() doesn't revoke it.
3160 saved_mask = vap->va_mask;
3161 vap->va_mask &= ~trim_mask;
3162 if (trim_mask & AT_MODE) {
3164 * Save the mode, as secpolicy_vnode_setattr()
3165 * will overwrite it with ova.va_mode.
3167 saved_mode = vap->va_mode;
3170 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3171 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3178 vap->va_mask |= saved_mask;
3179 if (trim_mask & AT_MODE) {
3181 * Recover the mode after
3182 * secpolicy_vnode_setattr().
3184 vap->va_mode = saved_mode;
3190 * secpolicy_vnode_setattr, or take ownership may have
3193 mask = vap->va_mask;
3195 if ((mask & (AT_UID | AT_GID))) {
3196 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3197 &xattr_obj, sizeof (xattr_obj));
3199 if (err == 0 && xattr_obj) {
3200 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3202 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
3204 vrele(ZTOV(attrzp));
3209 if (mask & AT_UID) {
3210 new_uid = zfs_fuid_create(zfsvfs,
3211 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3212 if (new_uid != zp->z_uid &&
3213 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3216 err = SET_ERROR(EDQUOT);
3221 if (mask & AT_GID) {
3222 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3223 cr, ZFS_GROUP, &fuidp);
3224 if (new_gid != zp->z_gid &&
3225 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3228 err = SET_ERROR(EDQUOT);
3233 tx = dmu_tx_create(zfsvfs->z_os);
3235 if (mask & AT_MODE) {
3236 uint64_t pmode = zp->z_mode;
3238 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3240 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3241 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3242 err = SET_ERROR(EPERM);
3246 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3249 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3251 * Are we upgrading ACL from old V0 format
3254 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3255 zfs_znode_acl_version(zp) ==
3256 ZFS_ACL_VERSION_INITIAL) {
3257 dmu_tx_hold_free(tx, acl_obj, 0,
3259 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3260 0, aclp->z_acl_bytes);
3262 dmu_tx_hold_write(tx, acl_obj, 0,
3265 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3266 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3267 0, aclp->z_acl_bytes);
3269 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3271 if ((mask & AT_XVATTR) &&
3272 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3273 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3275 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3279 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3282 fuid_dirtied = zfsvfs->z_fuid_dirty;
3284 zfs_fuid_txhold(zfsvfs, tx);
3286 zfs_sa_upgrade_txholds(tx, zp);
3288 err = dmu_tx_assign(tx, TXG_WAIT);
3294 * Set each attribute requested.
3295 * We group settings according to the locks they need to acquire.
3297 * Note: you cannot set ctime directly, although it will be
3298 * updated as a side-effect of calling this function.
3301 if (mask & (AT_UID|AT_GID|AT_MODE))
3302 mutex_enter(&zp->z_acl_lock);
3304 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3305 &zp->z_pflags, sizeof (zp->z_pflags));
3308 if (mask & (AT_UID|AT_GID|AT_MODE))
3309 mutex_enter(&attrzp->z_acl_lock);
3310 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3311 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3312 sizeof (attrzp->z_pflags));
3315 if (mask & (AT_UID|AT_GID)) {
3317 if (mask & AT_UID) {
3318 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3319 &new_uid, sizeof (new_uid));
3320 zp->z_uid = new_uid;
3322 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3323 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3325 attrzp->z_uid = new_uid;
3329 if (mask & AT_GID) {
3330 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3331 NULL, &new_gid, sizeof (new_gid));
3332 zp->z_gid = new_gid;
3334 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3335 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3337 attrzp->z_gid = new_gid;
3340 if (!(mask & AT_MODE)) {
3341 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3342 NULL, &new_mode, sizeof (new_mode));
3343 new_mode = zp->z_mode;
3345 err = zfs_acl_chown_setattr(zp);
3348 err = zfs_acl_chown_setattr(attrzp);
3353 if (mask & AT_MODE) {
3354 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3355 &new_mode, sizeof (new_mode));
3356 zp->z_mode = new_mode;
3357 ASSERT3U((uintptr_t)aclp, !=, 0);
3358 err = zfs_aclset_common(zp, aclp, cr, tx);
3360 if (zp->z_acl_cached)
3361 zfs_acl_free(zp->z_acl_cached);
3362 zp->z_acl_cached = aclp;
3367 if (mask & AT_ATIME) {
3368 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3369 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3370 &zp->z_atime, sizeof (zp->z_atime));
3373 if (mask & AT_MTIME) {
3374 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3375 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3376 mtime, sizeof (mtime));
3379 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3380 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3381 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3382 NULL, mtime, sizeof (mtime));
3383 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3384 &ctime, sizeof (ctime));
3385 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3387 } else if (mask != 0) {
3388 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3389 &ctime, sizeof (ctime));
3390 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3393 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3394 SA_ZPL_CTIME(zfsvfs), NULL,
3395 &ctime, sizeof (ctime));
3396 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3397 mtime, ctime, B_TRUE);
3401 * Do this after setting timestamps to prevent timestamp
3402 * update from toggling bit
3405 if (xoap && (mask & AT_XVATTR)) {
3407 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
3408 xoap->xoa_createtime = vap->va_birthtime;
3410 * restore trimmed off masks
3411 * so that return masks can be set for caller.
3414 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3415 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3417 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3418 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3420 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3421 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3423 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3424 XVA_SET_REQ(xvap, XAT_NODUMP);
3426 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3427 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3429 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3430 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3433 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3434 ASSERT(vp->v_type == VREG);
3436 zfs_xvattr_set(zp, xvap, tx);
3440 zfs_fuid_sync(zfsvfs, tx);
3443 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3445 if (mask & (AT_UID|AT_GID|AT_MODE))
3446 mutex_exit(&zp->z_acl_lock);
3449 if (mask & (AT_UID|AT_GID|AT_MODE))
3450 mutex_exit(&attrzp->z_acl_lock);
3453 if (err == 0 && attrzp) {
3454 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3466 zfs_fuid_info_free(fuidp);
3473 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3478 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3479 zil_commit(zilog, 0);
3486 * We acquire all but fdvp locks using non-blocking acquisitions. If we
3487 * fail to acquire any lock in the path we will drop all held locks,
3488 * acquire the new lock in a blocking fashion, and then release it and
3489 * restart the rename. This acquire/release step ensures that we do not
3490 * spin on a lock waiting for release. On error release all vnode locks
3491 * and decrement references the way tmpfs_rename() would do.
3494 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
3495 struct vnode *tdvp, struct vnode **tvpp,
3496 const struct componentname *scnp, const struct componentname *tcnp)
3499 struct vnode *nvp, *svp, *tvp;
3500 znode_t *sdzp, *tdzp, *szp, *tzp;
3501 const char *snm = scnp->cn_nameptr;
3502 const char *tnm = tcnp->cn_nameptr;
3505 VOP_UNLOCK(tdvp, 0);
3506 if (*tvpp != NULL && *tvpp != tdvp)
3507 VOP_UNLOCK(*tvpp, 0);
3510 error = vn_lock(sdvp, LK_EXCLUSIVE);
3515 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
3517 VOP_UNLOCK(sdvp, 0);
3520 error = vn_lock(tdvp, LK_EXCLUSIVE);
3523 VOP_UNLOCK(tdvp, 0);
3529 * Before using sdzp and tdzp we must ensure that they are live.
3530 * As a porting legacy from illumos we have two things to worry
3531 * about. One is typical for FreeBSD and it is that the vnode is
3532 * not reclaimed (doomed). The other is that the znode is live.
3533 * The current code can invalidate the znode without acquiring the
3534 * corresponding vnode lock if the object represented by the znode
3535 * and vnode is no longer valid after a rollback or receive operation.
3536 * z_teardown_lock hidden behind ZFS_ENTER and ZFS_EXIT is the lock
3537 * that protects the znodes from the invalidation.
3539 zfsvfs = sdzp->z_zfsvfs;
3540 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
3544 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3545 * bypassing the cleanup code in the case of an error.
3547 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3549 VOP_UNLOCK(sdvp, 0);
3550 VOP_UNLOCK(tdvp, 0);
3551 error = SET_ERROR(EIO);
3556 * Re-resolve svp to be certain it still exists and fetch the
3559 error = zfs_dirent_lookup(sdzp, snm, &szp, ZEXISTS);
3561 /* Source entry invalid or not there. */
3563 VOP_UNLOCK(sdvp, 0);
3564 VOP_UNLOCK(tdvp, 0);
3565 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
3566 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
3567 error = SET_ERROR(EINVAL);
3573 * Re-resolve tvp, if it disappeared we just carry on.
3575 error = zfs_dirent_lookup(tdzp, tnm, &tzp, 0);
3578 VOP_UNLOCK(sdvp, 0);
3579 VOP_UNLOCK(tdvp, 0);
3581 if ((tcnp->cn_flags & ISDOTDOT) != 0)
3582 error = SET_ERROR(EINVAL);
3591 * At present the vnode locks must be acquired before z_teardown_lock,
3592 * although it would be more logical to use the opposite order.
3597 * Now try acquire locks on svp and tvp.
3600 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3602 VOP_UNLOCK(sdvp, 0);
3603 VOP_UNLOCK(tdvp, 0);
3606 if (error != EBUSY) {
3610 error = vn_lock(nvp, LK_EXCLUSIVE);
3617 * Concurrent rename race.
3622 error = SET_ERROR(EINVAL);
3637 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3639 VOP_UNLOCK(sdvp, 0);
3640 VOP_UNLOCK(tdvp, 0);
3641 VOP_UNLOCK(*svpp, 0);
3642 if (error != EBUSY) {
3646 error = vn_lock(nvp, LK_EXCLUSIVE);
3664 * Note that we must use VRELE_ASYNC in this function as it walks
3665 * up the directory tree and vrele may need to acquire an exclusive
3666 * lock if a last reference to a vnode is dropped.
3669 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3676 zfsvfs = tdzp->z_zfsvfs;
3678 return (SET_ERROR(EINVAL));
3681 if (tdzp->z_id == zfsvfs->z_root)
3685 ASSERT(!zp->z_unlinked);
3686 if ((error = sa_lookup(zp->z_sa_hdl,
3687 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3690 if (parent == szp->z_id) {
3691 error = SET_ERROR(EINVAL);
3694 if (parent == zfsvfs->z_root)
3696 if (parent == sdzp->z_id)
3699 error = zfs_zget(zfsvfs, parent, &zp1);
3704 VN_RELE_ASYNC(ZTOV(zp),
3705 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3709 if (error == ENOTDIR)
3710 panic("checkpath: .. not a directory\n");
3712 VN_RELE_ASYNC(ZTOV(zp),
3713 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3718 * Move an entry from the provided source directory to the target
3719 * directory. Change the entry name as indicated.
3721 * IN: sdvp - Source directory containing the "old entry".
3722 * snm - Old entry name.
3723 * tdvp - Target directory to contain the "new entry".
3724 * tnm - New entry name.
3725 * cr - credentials of caller.
3726 * ct - caller context
3727 * flags - case flags
3729 * RETURN: 0 on success, error code on failure.
3732 * sdvp,tdvp - ctime|mtime updated
3736 zfs_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3737 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3741 znode_t *sdzp, *tdzp, *szp, *tzp;
3742 zilog_t *zilog = NULL;
3744 char *snm = scnp->cn_nameptr;
3745 char *tnm = tcnp->cn_nameptr;
3748 /* Reject renames across filesystems. */
3749 if ((*svpp)->v_mount != tdvp->v_mount ||
3750 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3751 error = SET_ERROR(EXDEV);
3755 if (zfsctl_is_node(tdvp)) {
3756 error = SET_ERROR(EXDEV);
3761 * Lock all four vnodes to ensure safety and semantics of renaming.
3763 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3765 /* no vnodes are locked in the case of error here */
3771 zfsvfs = tdzp->z_zfsvfs;
3772 zilog = zfsvfs->z_log;
3775 * After we re-enter ZFS_ENTER() we will have to revalidate all
3780 if (zfsvfs->z_utf8 && u8_validate(tnm,
3781 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3782 error = SET_ERROR(EILSEQ);
3786 /* If source and target are the same file, there is nothing to do. */
3787 if ((*svpp) == (*tvpp)) {
3792 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3793 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3794 (*tvpp)->v_mountedhere != NULL)) {
3795 error = SET_ERROR(EXDEV);
3800 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3801 * bypassing the cleanup code in the case of an error.
3803 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3804 error = SET_ERROR(EIO);
3809 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3810 if (szp->z_sa_hdl == NULL || (tzp != NULL && tzp->z_sa_hdl == NULL)) {
3811 error = SET_ERROR(EIO);
3816 * This is to prevent the creation of links into attribute space
3817 * by renaming a linked file into/outof an attribute directory.
3818 * See the comment in zfs_link() for why this is considered bad.
3820 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3821 error = SET_ERROR(EINVAL);
3826 * Must have write access at the source to remove the old entry
3827 * and write access at the target to create the new entry.
3828 * Note that if target and source are the same, this can be
3829 * done in a single check.
3831 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3834 if ((*svpp)->v_type == VDIR) {
3836 * Avoid ".", "..", and aliases of "." for obvious reasons.
3838 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3840 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3846 * Check to make sure rename is valid.
3847 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3849 if (error = zfs_rename_check(szp, sdzp, tdzp))
3854 * Does target exist?
3858 * Source and target must be the same type.
3860 if ((*svpp)->v_type == VDIR) {
3861 if ((*tvpp)->v_type != VDIR) {
3862 error = SET_ERROR(ENOTDIR);
3870 if ((*tvpp)->v_type == VDIR) {
3871 error = SET_ERROR(EISDIR);
3877 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3879 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3882 * notify the target directory if it is not the same
3883 * as source directory.
3886 vnevent_rename_dest_dir(tdvp, ct);
3889 tx = dmu_tx_create(zfsvfs->z_os);
3890 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3891 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3892 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3893 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3895 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3896 zfs_sa_upgrade_txholds(tx, tdzp);
3899 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3900 zfs_sa_upgrade_txholds(tx, tzp);
3903 zfs_sa_upgrade_txholds(tx, szp);
3904 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3905 error = dmu_tx_assign(tx, TXG_WAIT);
3912 if (tzp) /* Attempt to remove the existing target */
3913 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3916 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3918 szp->z_pflags |= ZFS_AV_MODIFIED;
3920 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3921 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3924 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3927 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3928 snm, tdzp, tnm, szp);
3931 * Update path information for the target vnode
3933 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
3936 * At this point, we have successfully created
3937 * the target name, but have failed to remove
3938 * the source name. Since the create was done
3939 * with the ZRENAMING flag, there are
3940 * complications; for one, the link count is
3941 * wrong. The easiest way to deal with this
3942 * is to remove the newly created target, and
3943 * return the original error. This must
3944 * succeed; fortunately, it is very unlikely to
3945 * fail, since we just created it.
3947 VERIFY3U(zfs_link_destroy(tdzp, tnm, szp, tx,
3948 ZRENAMING, NULL), ==, 0);
3955 cache_purge_negative(tdvp);
3961 unlockout: /* all 4 vnodes are locked, ZFS_ENTER called */
3963 VOP_UNLOCK(*svpp, 0);
3964 VOP_UNLOCK(sdvp, 0);
3966 out: /* original two vnodes are locked */
3967 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3968 zil_commit(zilog, 0);
3971 VOP_UNLOCK(*tvpp, 0);
3973 VOP_UNLOCK(tdvp, 0);
3978 * Insert the indicated symbolic reference entry into the directory.
3980 * IN: dvp - Directory to contain new symbolic link.
3981 * link - Name for new symlink entry.
3982 * vap - Attributes of new entry.
3983 * cr - credentials of caller.
3984 * ct - caller context
3985 * flags - case flags
3987 * RETURN: 0 on success, error code on failure.
3990 * dvp - ctime|mtime updated
3994 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
3995 cred_t *cr, kthread_t *td)
3997 znode_t *zp, *dzp = VTOZ(dvp);
3999 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4001 uint64_t len = strlen(link);
4003 zfs_acl_ids_t acl_ids;
4004 boolean_t fuid_dirtied;
4005 uint64_t txtype = TX_SYMLINK;
4008 ASSERT(vap->va_type == VLNK);
4012 zilog = zfsvfs->z_log;
4014 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4015 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4017 return (SET_ERROR(EILSEQ));
4020 if (len > MAXPATHLEN) {
4022 return (SET_ERROR(ENAMETOOLONG));
4025 if ((error = zfs_acl_ids_create(dzp, 0,
4026 vap, cr, NULL, &acl_ids)) != 0) {
4032 * Attempt to lock directory; fail if entry already exists.
4034 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
4036 zfs_acl_ids_free(&acl_ids);
4041 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4042 zfs_acl_ids_free(&acl_ids);
4047 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4048 zfs_acl_ids_free(&acl_ids);
4050 return (SET_ERROR(EDQUOT));
4053 getnewvnode_reserve(1);
4054 tx = dmu_tx_create(zfsvfs->z_os);
4055 fuid_dirtied = zfsvfs->z_fuid_dirty;
4056 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4057 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4058 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4059 ZFS_SA_BASE_ATTR_SIZE + len);
4060 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4061 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4062 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4063 acl_ids.z_aclp->z_acl_bytes);
4066 zfs_fuid_txhold(zfsvfs, tx);
4067 error = dmu_tx_assign(tx, TXG_WAIT);
4069 zfs_acl_ids_free(&acl_ids);
4071 getnewvnode_drop_reserve();
4077 * Create a new object for the symlink.
4078 * for version 4 ZPL datsets the symlink will be an SA attribute
4080 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4083 zfs_fuid_sync(zfsvfs, tx);
4086 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4089 zfs_sa_symlink(zp, link, len, tx);
4092 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4093 &zp->z_size, sizeof (zp->z_size), tx);
4095 * Insert the new object into the directory.
4097 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
4099 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4102 zfs_acl_ids_free(&acl_ids);
4106 getnewvnode_drop_reserve();
4108 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4109 zil_commit(zilog, 0);
4116 * Return, in the buffer contained in the provided uio structure,
4117 * the symbolic path referred to by vp.
4119 * IN: vp - vnode of symbolic link.
4120 * uio - structure to contain the link path.
4121 * cr - credentials of caller.
4122 * ct - caller context
4124 * OUT: uio - structure containing the link path.
4126 * RETURN: 0 on success, error code on failure.
4129 * vp - atime updated
4133 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4135 znode_t *zp = VTOZ(vp);
4136 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4143 error = sa_lookup_uio(zp->z_sa_hdl,
4144 SA_ZPL_SYMLINK(zfsvfs), uio);
4146 error = zfs_sa_readlink(zp, uio);
4148 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4155 * Insert a new entry into directory tdvp referencing svp.
4157 * IN: tdvp - Directory to contain new entry.
4158 * svp - vnode of new entry.
4159 * name - name of new entry.
4160 * cr - credentials of caller.
4161 * ct - caller context
4163 * RETURN: 0 on success, error code on failure.
4166 * tdvp - ctime|mtime updated
4167 * svp - ctime updated
4171 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4172 caller_context_t *ct, int flags)
4174 znode_t *dzp = VTOZ(tdvp);
4176 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4183 ASSERT(tdvp->v_type == VDIR);
4187 zilog = zfsvfs->z_log;
4190 * POSIX dictates that we return EPERM here.
4191 * Better choices include ENOTSUP or EISDIR.
4193 if (svp->v_type == VDIR) {
4195 return (SET_ERROR(EPERM));
4201 if (szp->z_pflags & (ZFS_APPENDONLY | ZFS_IMMUTABLE | ZFS_READONLY)) {
4203 return (SET_ERROR(EPERM));
4206 /* Prevent links to .zfs/shares files */
4208 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4209 &parent, sizeof (uint64_t))) != 0) {
4213 if (parent == zfsvfs->z_shares_dir) {
4215 return (SET_ERROR(EPERM));
4218 if (zfsvfs->z_utf8 && u8_validate(name,
4219 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4221 return (SET_ERROR(EILSEQ));
4225 * We do not support links between attributes and non-attributes
4226 * because of the potential security risk of creating links
4227 * into "normal" file space in order to circumvent restrictions
4228 * imposed in attribute space.
4230 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4232 return (SET_ERROR(EINVAL));
4236 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4237 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4239 return (SET_ERROR(EPERM));
4242 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4248 * Attempt to lock directory; fail if entry already exists.
4250 error = zfs_dirent_lookup(dzp, name, &tzp, ZNEW);
4256 tx = dmu_tx_create(zfsvfs->z_os);
4257 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4258 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4259 zfs_sa_upgrade_txholds(tx, szp);
4260 zfs_sa_upgrade_txholds(tx, dzp);
4261 error = dmu_tx_assign(tx, TXG_WAIT);
4268 error = zfs_link_create(dzp, name, szp, tx, 0);
4271 uint64_t txtype = TX_LINK;
4272 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4278 vnevent_link(svp, ct);
4281 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4282 zil_commit(zilog, 0);
4291 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4293 znode_t *zp = VTOZ(vp);
4294 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4297 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4298 if (zp->z_sa_hdl == NULL) {
4300 * The fs has been unmounted, or we did a
4301 * suspend/resume and this file no longer exists.
4303 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4308 if (zp->z_unlinked) {
4310 * Fast path to recycle a vnode of a removed file.
4312 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4317 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4318 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4320 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4321 zfs_sa_upgrade_txholds(tx, zp);
4322 error = dmu_tx_assign(tx, TXG_WAIT);
4326 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4327 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4328 zp->z_atime_dirty = 0;
4332 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4336 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
4337 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
4341 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4343 znode_t *zp = VTOZ(vp);
4344 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4347 uint64_t object = zp->z_id;
4354 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4355 &gen64, sizeof (uint64_t))) != 0) {
4360 gen = (uint32_t)gen64;
4362 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4365 if (fidp->fid_len < size) {
4366 fidp->fid_len = size;
4368 return (SET_ERROR(ENOSPC));
4371 fidp->fid_len = size;
4374 zfid = (zfid_short_t *)fidp;
4376 zfid->zf_len = size;
4378 for (i = 0; i < sizeof (zfid->zf_object); i++)
4379 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4381 /* Must have a non-zero generation number to distinguish from .zfs */
4384 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4385 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4387 if (size == LONG_FID_LEN) {
4388 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4391 zlfid = (zfid_long_t *)fidp;
4393 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4394 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4396 /* XXX - this should be the generation number for the objset */
4397 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4398 zlfid->zf_setgen[i] = 0;
4406 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4407 caller_context_t *ct)
4418 case _PC_FILESIZEBITS:
4422 case _PC_XATTR_EXISTS:
4424 zfsvfs = zp->z_zfsvfs;
4428 error = zfs_dirent_lookup(zp, "", &xzp,
4429 ZXATTR | ZEXISTS | ZSHARED);
4431 if (!zfs_dirempty(xzp))
4434 } else if (error == ENOENT) {
4436 * If there aren't extended attributes, it's the
4437 * same as having zero of them.
4444 case _PC_SATTR_ENABLED:
4445 case _PC_SATTR_EXISTS:
4446 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4447 (vp->v_type == VREG || vp->v_type == VDIR);
4450 case _PC_ACCESS_FILTERING:
4451 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4455 case _PC_ACL_ENABLED:
4456 *valp = _ACL_ACE_ENABLED;
4458 #endif /* illumos */
4459 case _PC_MIN_HOLE_SIZE:
4460 *valp = (int)SPA_MINBLOCKSIZE;
4463 case _PC_TIMESTAMP_RESOLUTION:
4464 /* nanosecond timestamp resolution */
4468 case _PC_ACL_EXTENDED:
4476 case _PC_ACL_PATH_MAX:
4477 *valp = ACL_MAX_ENTRIES;
4481 return (EOPNOTSUPP);
4487 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4488 caller_context_t *ct)
4490 znode_t *zp = VTOZ(vp);
4491 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4493 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4497 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4505 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4506 caller_context_t *ct)
4508 znode_t *zp = VTOZ(vp);
4509 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4511 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4512 zilog_t *zilog = zfsvfs->z_log;
4517 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4519 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4520 zil_commit(zilog, 0);
4527 ioflags(int ioflags)
4531 if (ioflags & IO_APPEND)
4533 if (ioflags & IO_NDELAY)
4535 if (ioflags & IO_SYNC)
4536 flags |= (FSYNC | FDSYNC | FRSYNC);
4542 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int *rbehind,
4545 znode_t *zp = VTOZ(vp);
4546 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4547 objset_t *os = zp->z_zfsvfs->z_os;
4552 off_t startoff, endoff;
4554 vm_pindex_t reqstart, reqend;
4557 object = m[0]->object;
4563 zfs_vmobject_wlock(object);
4564 if (m[count - 1]->valid != 0 && --count == 0) {
4565 zfs_vmobject_wunlock(object);
4569 mlast = m[count - 1];
4571 if (IDX_TO_OFF(mlast->pindex) >=
4572 object->un_pager.vnp.vnp_size) {
4573 zfs_vmobject_wunlock(object);
4575 return (zfs_vm_pagerret_bad);
4578 VM_CNT_INC(v_vnodein);
4579 VM_CNT_ADD(v_vnodepgsin, count);
4582 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
4583 lsize = object->un_pager.vnp.vnp_size -
4584 IDX_TO_OFF(mlast->pindex);
4585 zfs_vmobject_wunlock(object);
4587 for (i = 0; i < count; i++) {
4591 va = zfs_map_page(m[i], &sf);
4592 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
4593 size, va, DMU_READ_PREFETCH);
4594 if (size != PAGE_SIZE)
4595 bzero(va + size, PAGE_SIZE - size);
4601 zfs_vmobject_wlock(object);
4602 for (i = 0; i < count; i++)
4603 m[i]->valid = VM_PAGE_BITS_ALL;
4604 zfs_vmobject_wunlock(object);
4607 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4614 return (zfs_vm_pagerret_ok);
4616 return (zfs_vm_pagerret_error);
4620 zfs_freebsd_getpages(ap)
4621 struct vop_getpages_args /* {
4630 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4635 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4638 znode_t *zp = VTOZ(vp);
4639 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4648 vm_ooffset_t lo_off;
4659 object = vp->v_object;
4663 KASSERT(ma[0]->object == object, ("mismatching object"));
4664 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4666 for (i = 0; i < pcount; i++)
4667 rtvals[i] = zfs_vm_pagerret_error;
4669 off = IDX_TO_OFF(ma[0]->pindex);
4670 blksz = zp->z_blksz;
4671 lo_off = rounddown(off, blksz);
4672 lo_len = roundup(len + (off - lo_off), blksz);
4673 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
4675 zfs_vmobject_wlock(object);
4676 if (len + off > object->un_pager.vnp.vnp_size) {
4677 if (object->un_pager.vnp.vnp_size > off) {
4680 len = object->un_pager.vnp.vnp_size - off;
4682 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4684 * If the object is locked and the following
4685 * conditions hold, then the page's dirty
4686 * field cannot be concurrently changed by a
4690 vm_page_assert_sbusied(m);
4691 KASSERT(!pmap_page_is_write_mapped(m),
4692 ("zfs_putpages: page %p is not read-only", m));
4693 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4700 if (ncount < pcount) {
4701 for (i = ncount; i < pcount; i++) {
4702 rtvals[i] = zfs_vm_pagerret_bad;
4706 zfs_vmobject_wunlock(object);
4711 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4712 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4716 tx = dmu_tx_create(zfsvfs->z_os);
4717 dmu_tx_hold_write(tx, zp->z_id, off, len);
4719 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4720 zfs_sa_upgrade_txholds(tx, zp);
4721 err = dmu_tx_assign(tx, TXG_WAIT);
4727 if (zp->z_blksz < PAGE_SIZE) {
4729 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4730 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4731 va = zfs_map_page(ma[i], &sf);
4732 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4736 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4740 uint64_t mtime[2], ctime[2];
4741 sa_bulk_attr_t bulk[3];
4744 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4746 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4748 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4750 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4752 (void)sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4753 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4755 zfs_vmobject_wlock(object);
4756 for (i = 0; i < ncount; i++) {
4757 rtvals[i] = zfs_vm_pagerret_ok;
4758 vm_page_undirty(ma[i]);
4760 zfs_vmobject_wunlock(object);
4761 VM_CNT_INC(v_vnodeout);
4762 VM_CNT_ADD(v_vnodepgsout, ncount);
4767 zfs_range_unlock(rl);
4768 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4769 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4770 zil_commit(zfsvfs->z_log, zp->z_id);
4776 zfs_freebsd_putpages(ap)
4777 struct vop_putpages_args /* {
4786 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4791 zfs_freebsd_bmap(ap)
4792 struct vop_bmap_args /* {
4795 struct bufobj **a_bop;
4802 if (ap->a_bop != NULL)
4803 *ap->a_bop = &ap->a_vp->v_bufobj;
4804 if (ap->a_bnp != NULL)
4805 *ap->a_bnp = ap->a_bn;
4806 if (ap->a_runp != NULL)
4808 if (ap->a_runb != NULL)
4815 zfs_freebsd_open(ap)
4816 struct vop_open_args /* {
4819 struct ucred *a_cred;
4820 struct thread *a_td;
4823 vnode_t *vp = ap->a_vp;
4824 znode_t *zp = VTOZ(vp);
4827 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
4829 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4834 zfs_freebsd_close(ap)
4835 struct vop_close_args /* {
4838 struct ucred *a_cred;
4839 struct thread *a_td;
4843 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
4847 zfs_freebsd_ioctl(ap)
4848 struct vop_ioctl_args /* {
4858 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4859 ap->a_fflag, ap->a_cred, NULL, NULL));
4863 zfs_freebsd_read(ap)
4864 struct vop_read_args /* {
4868 struct ucred *a_cred;
4872 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4877 zfs_freebsd_write(ap)
4878 struct vop_write_args /* {
4882 struct ucred *a_cred;
4886 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4891 zfs_freebsd_access(ap)
4892 struct vop_access_args /* {
4894 accmode_t a_accmode;
4895 struct ucred *a_cred;
4896 struct thread *a_td;
4899 vnode_t *vp = ap->a_vp;
4900 znode_t *zp = VTOZ(vp);
4905 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4907 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4909 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
4912 * VADMIN has to be handled by vaccess().
4915 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4917 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4918 zp->z_gid, accmode, ap->a_cred, NULL);
4923 * For VEXEC, ensure that at least one execute bit is set for
4926 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4927 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4935 zfs_freebsd_lookup(ap)
4936 struct vop_lookup_args /* {
4937 struct vnode *a_dvp;
4938 struct vnode **a_vpp;
4939 struct componentname *a_cnp;
4942 struct componentname *cnp = ap->a_cnp;
4943 char nm[NAME_MAX + 1];
4945 ASSERT(cnp->cn_namelen < sizeof(nm));
4946 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
4948 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4949 cnp->cn_cred, cnp->cn_thread, 0));
4953 zfs_cache_lookup(ap)
4954 struct vop_lookup_args /* {
4955 struct vnode *a_dvp;
4956 struct vnode **a_vpp;
4957 struct componentname *a_cnp;
4962 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4963 if (zfsvfs->z_use_namecache)
4964 return (vfs_cache_lookup(ap));
4966 return (zfs_freebsd_lookup(ap));
4970 zfs_freebsd_create(ap)
4971 struct vop_create_args /* {
4972 struct vnode *a_dvp;
4973 struct vnode **a_vpp;
4974 struct componentname *a_cnp;
4975 struct vattr *a_vap;
4979 struct componentname *cnp = ap->a_cnp;
4980 vattr_t *vap = ap->a_vap;
4983 ASSERT(cnp->cn_flags & SAVENAME);
4985 vattr_init_mask(vap);
4986 mode = vap->va_mode & ALLPERMS;
4987 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4989 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
4990 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
4991 if (zfsvfs->z_use_namecache &&
4992 error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
4993 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
4998 zfs_freebsd_remove(ap)
4999 struct vop_remove_args /* {
5000 struct vnode *a_dvp;
5002 struct componentname *a_cnp;
5006 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5008 return (zfs_remove(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
5009 ap->a_cnp->cn_cred));
5013 zfs_freebsd_mkdir(ap)
5014 struct vop_mkdir_args /* {
5015 struct vnode *a_dvp;
5016 struct vnode **a_vpp;
5017 struct componentname *a_cnp;
5018 struct vattr *a_vap;
5021 vattr_t *vap = ap->a_vap;
5023 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5025 vattr_init_mask(vap);
5027 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
5028 ap->a_cnp->cn_cred));
5032 zfs_freebsd_rmdir(ap)
5033 struct vop_rmdir_args /* {
5034 struct vnode *a_dvp;
5036 struct componentname *a_cnp;
5039 struct componentname *cnp = ap->a_cnp;
5041 ASSERT(cnp->cn_flags & SAVENAME);
5043 return (zfs_rmdir(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
5047 zfs_freebsd_readdir(ap)
5048 struct vop_readdir_args /* {
5051 struct ucred *a_cred;
5058 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
5059 ap->a_ncookies, ap->a_cookies));
5063 zfs_freebsd_fsync(ap)
5064 struct vop_fsync_args /* {
5067 struct thread *a_td;
5072 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
5076 zfs_freebsd_getattr(ap)
5077 struct vop_getattr_args /* {
5079 struct vattr *a_vap;
5080 struct ucred *a_cred;
5083 vattr_t *vap = ap->a_vap;
5089 xvap.xva_vattr = *vap;
5090 xvap.xva_vattr.va_mask |= AT_XVATTR;
5092 /* Convert chflags into ZFS-type flags. */
5093 /* XXX: what about SF_SETTABLE?. */
5094 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
5095 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
5096 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
5097 XVA_SET_REQ(&xvap, XAT_NODUMP);
5098 XVA_SET_REQ(&xvap, XAT_READONLY);
5099 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
5100 XVA_SET_REQ(&xvap, XAT_SYSTEM);
5101 XVA_SET_REQ(&xvap, XAT_HIDDEN);
5102 XVA_SET_REQ(&xvap, XAT_REPARSE);
5103 XVA_SET_REQ(&xvap, XAT_OFFLINE);
5104 XVA_SET_REQ(&xvap, XAT_SPARSE);
5106 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
5110 /* Convert ZFS xattr into chflags. */
5111 #define FLAG_CHECK(fflag, xflag, xfield) do { \
5112 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
5113 fflags |= (fflag); \
5115 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
5116 xvap.xva_xoptattrs.xoa_immutable);
5117 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
5118 xvap.xva_xoptattrs.xoa_appendonly);
5119 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
5120 xvap.xva_xoptattrs.xoa_nounlink);
5121 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
5122 xvap.xva_xoptattrs.xoa_archive);
5123 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
5124 xvap.xva_xoptattrs.xoa_nodump);
5125 FLAG_CHECK(UF_READONLY, XAT_READONLY,
5126 xvap.xva_xoptattrs.xoa_readonly);
5127 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
5128 xvap.xva_xoptattrs.xoa_system);
5129 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
5130 xvap.xva_xoptattrs.xoa_hidden);
5131 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
5132 xvap.xva_xoptattrs.xoa_reparse);
5133 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
5134 xvap.xva_xoptattrs.xoa_offline);
5135 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
5136 xvap.xva_xoptattrs.xoa_sparse);
5139 *vap = xvap.xva_vattr;
5140 vap->va_flags = fflags;
5145 zfs_freebsd_setattr(ap)
5146 struct vop_setattr_args /* {
5148 struct vattr *a_vap;
5149 struct ucred *a_cred;
5152 vnode_t *vp = ap->a_vp;
5153 vattr_t *vap = ap->a_vap;
5154 cred_t *cred = ap->a_cred;
5159 vattr_init_mask(vap);
5160 vap->va_mask &= ~AT_NOSET;
5163 xvap.xva_vattr = *vap;
5165 zflags = VTOZ(vp)->z_pflags;
5167 if (vap->va_flags != VNOVAL) {
5168 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
5171 if (zfsvfs->z_use_fuids == B_FALSE)
5172 return (EOPNOTSUPP);
5174 fflags = vap->va_flags;
5177 * We need to figure out whether it makes sense to allow
5178 * UF_REPARSE through, since we don't really have other
5179 * facilities to handle reparse points and zfs_setattr()
5180 * doesn't currently allow setting that attribute anyway.
5182 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
5183 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
5184 UF_OFFLINE|UF_SPARSE)) != 0)
5185 return (EOPNOTSUPP);
5187 * Unprivileged processes are not permitted to unset system
5188 * flags, or modify flags if any system flags are set.
5189 * Privileged non-jail processes may not modify system flags
5190 * if securelevel > 0 and any existing system flags are set.
5191 * Privileged jail processes behave like privileged non-jail
5192 * processes if the security.jail.chflags_allowed sysctl is
5193 * is non-zero; otherwise, they behave like unprivileged
5196 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
5197 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
5199 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5200 error = securelevel_gt(cred, 0);
5206 * Callers may only modify the file flags on objects they
5207 * have VADMIN rights for.
5209 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
5212 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5216 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
5221 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
5222 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
5223 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
5224 XVA_SET_REQ(&xvap, (xflag)); \
5225 (xfield) = ((fflags & (fflag)) != 0); \
5228 /* Convert chflags into ZFS-type flags. */
5229 /* XXX: what about SF_SETTABLE?. */
5230 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
5231 xvap.xva_xoptattrs.xoa_immutable);
5232 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
5233 xvap.xva_xoptattrs.xoa_appendonly);
5234 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
5235 xvap.xva_xoptattrs.xoa_nounlink);
5236 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
5237 xvap.xva_xoptattrs.xoa_archive);
5238 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
5239 xvap.xva_xoptattrs.xoa_nodump);
5240 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
5241 xvap.xva_xoptattrs.xoa_readonly);
5242 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
5243 xvap.xva_xoptattrs.xoa_system);
5244 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
5245 xvap.xva_xoptattrs.xoa_hidden);
5246 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
5247 xvap.xva_xoptattrs.xoa_hidden);
5248 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
5249 xvap.xva_xoptattrs.xoa_offline);
5250 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
5251 xvap.xva_xoptattrs.xoa_sparse);
5254 if (vap->va_birthtime.tv_sec != VNOVAL) {
5255 xvap.xva_vattr.va_mask |= AT_XVATTR;
5256 XVA_SET_REQ(&xvap, XAT_CREATETIME);
5258 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
5262 zfs_freebsd_rename(ap)
5263 struct vop_rename_args /* {
5264 struct vnode *a_fdvp;
5265 struct vnode *a_fvp;
5266 struct componentname *a_fcnp;
5267 struct vnode *a_tdvp;
5268 struct vnode *a_tvp;
5269 struct componentname *a_tcnp;
5272 vnode_t *fdvp = ap->a_fdvp;
5273 vnode_t *fvp = ap->a_fvp;
5274 vnode_t *tdvp = ap->a_tdvp;
5275 vnode_t *tvp = ap->a_tvp;
5278 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
5279 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
5281 error = zfs_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
5282 ap->a_tcnp, ap->a_fcnp->cn_cred);
5294 zfs_freebsd_symlink(ap)
5295 struct vop_symlink_args /* {
5296 struct vnode *a_dvp;
5297 struct vnode **a_vpp;
5298 struct componentname *a_cnp;
5299 struct vattr *a_vap;
5303 struct componentname *cnp = ap->a_cnp;
5304 vattr_t *vap = ap->a_vap;
5306 ASSERT(cnp->cn_flags & SAVENAME);
5308 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
5309 vattr_init_mask(vap);
5311 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
5312 ap->a_target, cnp->cn_cred, cnp->cn_thread));
5316 zfs_freebsd_readlink(ap)
5317 struct vop_readlink_args /* {
5320 struct ucred *a_cred;
5324 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
5328 zfs_freebsd_link(ap)
5329 struct vop_link_args /* {
5330 struct vnode *a_tdvp;
5332 struct componentname *a_cnp;
5335 struct componentname *cnp = ap->a_cnp;
5336 vnode_t *vp = ap->a_vp;
5337 vnode_t *tdvp = ap->a_tdvp;
5339 if (tdvp->v_mount != vp->v_mount)
5342 ASSERT(cnp->cn_flags & SAVENAME);
5344 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
5348 zfs_freebsd_inactive(ap)
5349 struct vop_inactive_args /* {
5351 struct thread *a_td;
5354 vnode_t *vp = ap->a_vp;
5356 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5361 zfs_freebsd_reclaim(ap)
5362 struct vop_reclaim_args /* {
5364 struct thread *a_td;
5367 vnode_t *vp = ap->a_vp;
5368 znode_t *zp = VTOZ(vp);
5369 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5373 /* Destroy the vm object and flush associated pages. */
5374 vnode_destroy_vobject(vp);
5377 * z_teardown_inactive_lock protects from a race with
5378 * zfs_znode_dmu_fini in zfsvfs_teardown during
5381 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
5382 if (zp->z_sa_hdl == NULL)
5386 rw_exit(&zfsvfs->z_teardown_inactive_lock);
5394 struct vop_fid_args /* {
5400 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5404 zfs_freebsd_pathconf(ap)
5405 struct vop_pathconf_args /* {
5408 register_t *a_retval;
5414 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
5416 *ap->a_retval = val;
5417 else if (error == EOPNOTSUPP)
5418 error = vop_stdpathconf(ap);
5423 zfs_freebsd_fifo_pathconf(ap)
5424 struct vop_pathconf_args /* {
5427 register_t *a_retval;
5431 switch (ap->a_name) {
5432 case _PC_ACL_EXTENDED:
5434 case _PC_ACL_PATH_MAX:
5435 case _PC_MAC_PRESENT:
5436 return (zfs_freebsd_pathconf(ap));
5438 return (fifo_specops.vop_pathconf(ap));
5443 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5444 * extended attribute name:
5447 * system freebsd:system:
5448 * user (none, can be used to access ZFS fsattr(5) attributes
5449 * created on Solaris)
5452 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5455 const char *namespace, *prefix, *suffix;
5457 /* We don't allow '/' character in attribute name. */
5458 if (strchr(name, '/') != NULL)
5460 /* We don't allow attribute names that start with "freebsd:" string. */
5461 if (strncmp(name, "freebsd:", 8) == 0)
5464 bzero(attrname, size);
5466 switch (attrnamespace) {
5467 case EXTATTR_NAMESPACE_USER:
5469 prefix = "freebsd:";
5470 namespace = EXTATTR_NAMESPACE_USER_STRING;
5474 * This is the default namespace by which we can access all
5475 * attributes created on Solaris.
5477 prefix = namespace = suffix = "";
5480 case EXTATTR_NAMESPACE_SYSTEM:
5481 prefix = "freebsd:";
5482 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5485 case EXTATTR_NAMESPACE_EMPTY:
5489 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5491 return (ENAMETOOLONG);
5497 * Vnode operating to retrieve a named extended attribute.
5500 zfs_getextattr(struct vop_getextattr_args *ap)
5503 IN struct vnode *a_vp;
5504 IN int a_attrnamespace;
5505 IN const char *a_name;
5506 INOUT struct uio *a_uio;
5508 IN struct ucred *a_cred;
5509 IN struct thread *a_td;
5513 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5514 struct thread *td = ap->a_td;
5515 struct nameidata nd;
5518 vnode_t *xvp = NULL, *vp;
5521 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5522 ap->a_cred, ap->a_td, VREAD);
5526 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5533 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5541 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5543 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
5545 NDFREE(&nd, NDF_ONLY_PNBUF);
5548 if (error == ENOENT)
5553 if (ap->a_size != NULL) {
5554 error = VOP_GETATTR(vp, &va, ap->a_cred);
5556 *ap->a_size = (size_t)va.va_size;
5557 } else if (ap->a_uio != NULL)
5558 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5561 vn_close(vp, flags, ap->a_cred, td);
5568 * Vnode operation to remove a named attribute.
5571 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5574 IN struct vnode *a_vp;
5575 IN int a_attrnamespace;
5576 IN const char *a_name;
5577 IN struct ucred *a_cred;
5578 IN struct thread *a_td;
5582 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5583 struct thread *td = ap->a_td;
5584 struct nameidata nd;
5587 vnode_t *xvp = NULL, *vp;
5590 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5591 ap->a_cred, ap->a_td, VWRITE);
5595 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5602 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5609 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5610 UIO_SYSSPACE, attrname, xvp, td);
5615 NDFREE(&nd, NDF_ONLY_PNBUF);
5616 if (error == ENOENT)
5621 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5622 NDFREE(&nd, NDF_ONLY_PNBUF);
5625 if (vp == nd.ni_dvp)
5635 * Vnode operation to set a named attribute.
5638 zfs_setextattr(struct vop_setextattr_args *ap)
5641 IN struct vnode *a_vp;
5642 IN int a_attrnamespace;
5643 IN const char *a_name;
5644 INOUT struct uio *a_uio;
5645 IN struct ucred *a_cred;
5646 IN struct thread *a_td;
5650 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5651 struct thread *td = ap->a_td;
5652 struct nameidata nd;
5655 vnode_t *xvp = NULL, *vp;
5658 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5659 ap->a_cred, ap->a_td, VWRITE);
5663 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5670 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5671 LOOKUP_XATTR | CREATE_XATTR_DIR);
5677 flags = FFLAGS(O_WRONLY | O_CREAT);
5678 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5680 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
5682 NDFREE(&nd, NDF_ONLY_PNBUF);
5690 error = VOP_SETATTR(vp, &va, ap->a_cred);
5692 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5695 vn_close(vp, flags, ap->a_cred, td);
5702 * Vnode operation to retrieve extended attributes on a vnode.
5705 zfs_listextattr(struct vop_listextattr_args *ap)
5708 IN struct vnode *a_vp;
5709 IN int a_attrnamespace;
5710 INOUT struct uio *a_uio;
5712 IN struct ucred *a_cred;
5713 IN struct thread *a_td;
5717 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5718 struct thread *td = ap->a_td;
5719 struct nameidata nd;
5720 char attrprefix[16];
5721 u_char dirbuf[sizeof(struct dirent)];
5724 struct uio auio, *uio = ap->a_uio;
5725 size_t *sizep = ap->a_size;
5727 vnode_t *xvp = NULL, *vp;
5728 int done, error, eof, pos;
5730 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5731 ap->a_cred, ap->a_td, VREAD);
5735 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5736 sizeof(attrprefix));
5739 plen = strlen(attrprefix);
5746 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5751 * ENOATTR means that the EA directory does not yet exist,
5752 * i.e. there are no extended attributes there.
5754 if (error == ENOATTR)
5759 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5760 UIO_SYSSPACE, ".", xvp, td);
5763 NDFREE(&nd, NDF_ONLY_PNBUF);
5769 auio.uio_iov = &aiov;
5770 auio.uio_iovcnt = 1;
5771 auio.uio_segflg = UIO_SYSSPACE;
5773 auio.uio_rw = UIO_READ;
5774 auio.uio_offset = 0;
5779 aiov.iov_base = (void *)dirbuf;
5780 aiov.iov_len = sizeof(dirbuf);
5781 auio.uio_resid = sizeof(dirbuf);
5782 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5783 done = sizeof(dirbuf) - auio.uio_resid;
5786 for (pos = 0; pos < done;) {
5787 dp = (struct dirent *)(dirbuf + pos);
5788 pos += dp->d_reclen;
5790 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5791 * is what we get when attribute was created on Solaris.
5793 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5795 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
5797 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5799 nlen = dp->d_namlen - plen;
5802 else if (uio != NULL) {
5804 * Format of extattr name entry is one byte for
5805 * length and the rest for name.
5807 error = uiomove(&nlen, 1, uio->uio_rw, uio);
5809 error = uiomove(dp->d_name + plen, nlen,
5816 } while (!eof && error == 0);
5825 zfs_freebsd_getacl(ap)
5826 struct vop_getacl_args /* {
5835 vsecattr_t vsecattr;
5837 if (ap->a_type != ACL_TYPE_NFS4)
5840 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
5841 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
5844 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
5845 if (vsecattr.vsa_aclentp != NULL)
5846 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
5852 zfs_freebsd_setacl(ap)
5853 struct vop_setacl_args /* {
5862 vsecattr_t vsecattr;
5863 int aclbsize; /* size of acl list in bytes */
5866 if (ap->a_type != ACL_TYPE_NFS4)
5869 if (ap->a_aclp == NULL)
5872 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
5876 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
5877 * splitting every entry into two and appending "canonical six"
5878 * entries at the end. Don't allow for setting an ACL that would
5879 * cause chmod(2) to run out of ACL entries.
5881 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
5884 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
5888 vsecattr.vsa_mask = VSA_ACE;
5889 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
5890 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
5891 aaclp = vsecattr.vsa_aclentp;
5892 vsecattr.vsa_aclentsz = aclbsize;
5894 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
5895 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
5896 kmem_free(aaclp, aclbsize);
5902 zfs_freebsd_aclcheck(ap)
5903 struct vop_aclcheck_args /* {
5912 return (EOPNOTSUPP);
5916 zfs_vptocnp(struct vop_vptocnp_args *ap)
5918 vnode_t *covered_vp;
5919 vnode_t *vp = ap->a_vp;;
5920 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
5921 znode_t *zp = VTOZ(vp);
5930 * If we are a snapshot mounted under .zfs, run the operation
5931 * on the covered vnode.
5933 if ((error = sa_lookup(zp->z_sa_hdl,
5934 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) {
5939 if (zp->z_id != parent || zfsvfs->z_parent == zfsvfs) {
5940 char name[MAXNAMLEN + 1];
5944 error = zfs_znode_parent_and_name(zp, &dzp, name);
5947 if (*ap->a_buflen < len)
5948 error = SET_ERROR(ENOMEM);
5951 *ap->a_buflen -= len;
5952 bcopy(name, ap->a_buf + *ap->a_buflen, len);
5953 *ap->a_vpp = ZTOV(dzp);
5960 covered_vp = vp->v_mount->mnt_vnodecovered;
5962 ltype = VOP_ISLOCKED(vp);
5964 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
5966 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
5967 ap->a_buf, ap->a_buflen);
5970 vn_lock(vp, ltype | LK_RETRY);
5971 if ((vp->v_iflag & VI_DOOMED) != 0)
5972 error = SET_ERROR(ENOENT);
5979 struct vop_lock1_args /* {
5990 err = vop_stdlock(ap);
5991 if (err == 0 && (ap->a_flags & LK_NOWAIT) == 0) {
5994 if (vp->v_mount != NULL && (vp->v_iflag & VI_DOOMED) == 0 &&
5995 zp != NULL && (zp->z_pflags & ZFS_XATTR) == 0)
5996 VERIFY(!RRM_LOCK_HELD(&zp->z_zfsvfs->z_teardown_lock));
6002 struct vop_vector zfs_vnodeops;
6003 struct vop_vector zfs_fifoops;
6004 struct vop_vector zfs_shareops;
6006 struct vop_vector zfs_vnodeops = {
6007 .vop_default = &default_vnodeops,
6008 .vop_inactive = zfs_freebsd_inactive,
6009 .vop_reclaim = zfs_freebsd_reclaim,
6010 .vop_access = zfs_freebsd_access,
6011 .vop_lookup = zfs_cache_lookup,
6012 .vop_cachedlookup = zfs_freebsd_lookup,
6013 .vop_getattr = zfs_freebsd_getattr,
6014 .vop_setattr = zfs_freebsd_setattr,
6015 .vop_create = zfs_freebsd_create,
6016 .vop_mknod = zfs_freebsd_create,
6017 .vop_mkdir = zfs_freebsd_mkdir,
6018 .vop_readdir = zfs_freebsd_readdir,
6019 .vop_fsync = zfs_freebsd_fsync,
6020 .vop_open = zfs_freebsd_open,
6021 .vop_close = zfs_freebsd_close,
6022 .vop_rmdir = zfs_freebsd_rmdir,
6023 .vop_ioctl = zfs_freebsd_ioctl,
6024 .vop_link = zfs_freebsd_link,
6025 .vop_symlink = zfs_freebsd_symlink,
6026 .vop_readlink = zfs_freebsd_readlink,
6027 .vop_read = zfs_freebsd_read,
6028 .vop_write = zfs_freebsd_write,
6029 .vop_remove = zfs_freebsd_remove,
6030 .vop_rename = zfs_freebsd_rename,
6031 .vop_pathconf = zfs_freebsd_pathconf,
6032 .vop_bmap = zfs_freebsd_bmap,
6033 .vop_fid = zfs_freebsd_fid,
6034 .vop_getextattr = zfs_getextattr,
6035 .vop_deleteextattr = zfs_deleteextattr,
6036 .vop_setextattr = zfs_setextattr,
6037 .vop_listextattr = zfs_listextattr,
6038 .vop_getacl = zfs_freebsd_getacl,
6039 .vop_setacl = zfs_freebsd_setacl,
6040 .vop_aclcheck = zfs_freebsd_aclcheck,
6041 .vop_getpages = zfs_freebsd_getpages,
6042 .vop_putpages = zfs_freebsd_putpages,
6043 .vop_vptocnp = zfs_vptocnp,
6045 .vop_lock1 = zfs_lock,
6049 struct vop_vector zfs_fifoops = {
6050 .vop_default = &fifo_specops,
6051 .vop_fsync = zfs_freebsd_fsync,
6052 .vop_access = zfs_freebsd_access,
6053 .vop_getattr = zfs_freebsd_getattr,
6054 .vop_inactive = zfs_freebsd_inactive,
6055 .vop_read = VOP_PANIC,
6056 .vop_reclaim = zfs_freebsd_reclaim,
6057 .vop_setattr = zfs_freebsd_setattr,
6058 .vop_write = VOP_PANIC,
6059 .vop_pathconf = zfs_freebsd_fifo_pathconf,
6060 .vop_fid = zfs_freebsd_fid,
6061 .vop_getacl = zfs_freebsd_getacl,
6062 .vop_setacl = zfs_freebsd_setacl,
6063 .vop_aclcheck = zfs_freebsd_aclcheck,
6067 * special share hidden files vnode operations template
6069 struct vop_vector zfs_shareops = {
6070 .vop_default = &default_vnodeops,
6071 .vop_access = zfs_freebsd_access,
6072 .vop_inactive = zfs_freebsd_inactive,
6073 .vop_reclaim = zfs_freebsd_reclaim,
6074 .vop_fid = zfs_freebsd_fid,
6075 .vop_pathconf = zfs_freebsd_pathconf,