4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2017 Nexenta Systems, Inc.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sysmacros.h>
37 #include <sys/resource.h>
40 #include <sys/vnode.h>
44 #include <sys/taskq.h>
46 #include <sys/atomic.h>
47 #include <sys/namei.h>
49 #include <sys/cmn_err.h>
50 #include <sys/errno.h>
51 #include <sys/unistd.h>
52 #include <sys/zfs_dir.h>
53 #include <sys/zfs_ioctl.h>
54 #include <sys/fs/zfs.h>
56 #include <sys/dmu_objset.h>
62 #include <sys/dirent.h>
63 #include <sys/policy.h>
64 #include <sys/sunddi.h>
65 #include <sys/filio.h>
67 #include <sys/zfs_ctldir.h>
68 #include <sys/zfs_fuid.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_rlock.h>
71 #include <sys/extdirent.h>
72 #include <sys/kidmap.h>
75 #include <sys/sched.h>
77 #include <sys/vmmeter.h>
78 #include <vm/vm_param.h>
83 * Each vnode op performs some logical unit of work. To do this, the ZPL must
84 * properly lock its in-core state, create a DMU transaction, do the work,
85 * record this work in the intent log (ZIL), commit the DMU transaction,
86 * and wait for the intent log to commit if it is a synchronous operation.
87 * Moreover, the vnode ops must work in both normal and log replay context.
88 * The ordering of events is important to avoid deadlocks and references
89 * to freed memory. The example below illustrates the following Big Rules:
91 * (1) A check must be made in each zfs thread for a mounted file system.
92 * This is done avoiding races using ZFS_ENTER(zfsvfs).
93 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
94 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
95 * can return EIO from the calling function.
97 * (2) VN_RELE() should always be the last thing except for zil_commit()
98 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
99 * First, if it's the last reference, the vnode/znode
100 * can be freed, so the zp may point to freed memory. Second, the last
101 * reference will call zfs_zinactive(), which may induce a lot of work --
102 * pushing cached pages (which acquires range locks) and syncing out
103 * cached atime changes. Third, zfs_zinactive() may require a new tx,
104 * which could deadlock the system if you were already holding one.
105 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
107 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
108 * as they can span dmu_tx_assign() calls.
110 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
111 * dmu_tx_assign(). This is critical because we don't want to block
112 * while holding locks.
114 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
115 * reduces lock contention and CPU usage when we must wait (note that if
116 * throughput is constrained by the storage, nearly every transaction
119 * Note, in particular, that if a lock is sometimes acquired before
120 * the tx assigns, and sometimes after (e.g. z_lock), then failing
121 * to use a non-blocking assign can deadlock the system. The scenario:
123 * Thread A has grabbed a lock before calling dmu_tx_assign().
124 * Thread B is in an already-assigned tx, and blocks for this lock.
125 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
126 * forever, because the previous txg can't quiesce until B's tx commits.
128 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
129 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
130 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
131 * to indicate that this operation has already called dmu_tx_wait().
132 * This will ensure that we don't retry forever, waiting a short bit
135 * (5) If the operation succeeded, generate the intent log entry for it
136 * before dropping locks. This ensures that the ordering of events
137 * in the intent log matches the order in which they actually occurred.
138 * During ZIL replay the zfs_log_* functions will update the sequence
139 * number to indicate the zil transaction has replayed.
141 * (6) At the end of each vnode op, the DMU tx must always commit,
142 * regardless of whether there were any errors.
144 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
145 * to ensure that synchronous semantics are provided when necessary.
147 * In general, this is how things should be ordered in each vnode op:
149 * ZFS_ENTER(zfsvfs); // exit if unmounted
151 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
152 * rw_enter(...); // grab any other locks you need
153 * tx = dmu_tx_create(...); // get DMU tx
154 * dmu_tx_hold_*(); // hold each object you might modify
155 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
157 * rw_exit(...); // drop locks
158 * zfs_dirent_unlock(dl); // unlock directory entry
159 * VN_RELE(...); // release held vnodes
160 * if (error == ERESTART) {
166 * dmu_tx_abort(tx); // abort DMU tx
167 * ZFS_EXIT(zfsvfs); // finished in zfs
168 * return (error); // really out of space
170 * error = do_real_work(); // do whatever this VOP does
172 * zfs_log_*(...); // on success, make ZIL entry
173 * dmu_tx_commit(tx); // commit DMU tx -- error or not
174 * rw_exit(...); // drop locks
175 * zfs_dirent_unlock(dl); // unlock directory entry
176 * VN_RELE(...); // release held vnodes
177 * zil_commit(zilog, foid); // synchronous when necessary
178 * ZFS_EXIT(zfsvfs); // finished in zfs
179 * return (error); // done, report error
184 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
186 znode_t *zp = VTOZ(*vpp);
187 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
192 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
193 ((flag & FAPPEND) == 0)) {
195 return (SET_ERROR(EPERM));
198 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
199 ZTOV(zp)->v_type == VREG &&
200 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
201 if (fs_vscan(*vpp, cr, 0) != 0) {
203 return (SET_ERROR(EACCES));
207 /* Keep a count of the synchronous opens in the znode */
208 if (flag & (FSYNC | FDSYNC))
209 atomic_inc_32(&zp->z_sync_cnt);
217 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
218 caller_context_t *ct)
220 znode_t *zp = VTOZ(vp);
221 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
224 * Clean up any locks held by this process on the vp.
226 cleanlocks(vp, ddi_get_pid(), 0);
227 cleanshares(vp, ddi_get_pid());
232 /* Decrement the synchronous opens in the znode */
233 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
234 atomic_dec_32(&zp->z_sync_cnt);
236 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
237 ZTOV(zp)->v_type == VREG &&
238 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
239 VERIFY(fs_vscan(vp, cr, 1) == 0);
246 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
247 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
250 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
252 znode_t *zp = VTOZ(vp);
253 uint64_t noff = (uint64_t)*off; /* new offset */
258 file_sz = zp->z_size;
259 if (noff >= file_sz) {
260 return (SET_ERROR(ENXIO));
263 if (cmd == _FIO_SEEK_HOLE)
268 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
271 return (SET_ERROR(ENXIO));
274 * We could find a hole that begins after the logical end-of-file,
275 * because dmu_offset_next() only works on whole blocks. If the
276 * EOF falls mid-block, then indicate that the "virtual hole"
277 * at the end of the file begins at the logical EOF, rather than
278 * at the end of the last block.
280 if (noff > file_sz) {
293 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
294 int *rvalp, caller_context_t *ct)
298 dmu_object_info_t doi;
309 * The following two ioctls are used by bfu. Faking out,
310 * necessary to avoid bfu errors.
323 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
324 return (SET_ERROR(EFAULT));
326 off = *(offset_t *)data;
329 zfsvfs = zp->z_zfsvfs;
333 /* offset parameter is in/out */
334 error = zfs_holey(vp, com, &off);
339 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
340 return (SET_ERROR(EFAULT));
342 *(offset_t *)data = off;
347 case _FIO_COUNT_FILLED:
350 * _FIO_COUNT_FILLED adds a new ioctl command which
351 * exposes the number of filled blocks in a
355 zfsvfs = zp->z_zfsvfs;
360 * Wait for all dirty blocks for this object
361 * to get synced out to disk, and the DMU info
364 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
371 * Retrieve fill count from DMU object.
373 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
379 ndata = doi.doi_fill_count;
382 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
383 return (SET_ERROR(EFAULT));
388 return (SET_ERROR(ENOTTY));
392 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
399 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
400 * aligned boundaries, if the range is not aligned. As a result a
401 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
402 * It may happen that all DEV_BSIZE subranges are marked clean and thus
403 * the whole page would be considred clean despite have some dirty data.
404 * For this reason we should shrink the range to DEV_BSIZE aligned
405 * boundaries before calling vm_page_clear_dirty.
407 end = rounddown2(off + nbytes, DEV_BSIZE);
408 off = roundup2(off, DEV_BSIZE);
412 zfs_vmobject_assert_wlocked(obj);
415 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
417 if (vm_page_xbusied(pp)) {
419 * Reference the page before unlocking and
420 * sleeping so that the page daemon is less
421 * likely to reclaim it.
423 vm_page_reference(pp);
425 zfs_vmobject_wunlock(obj);
426 vm_page_busy_sleep(pp, "zfsmwb", true);
427 zfs_vmobject_wlock(obj);
431 } else if (pp != NULL) {
437 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
438 vm_object_pip_add(obj, 1);
439 pmap_remove_write(pp);
441 vm_page_clear_dirty(pp, off, nbytes);
449 page_unbusy(vm_page_t pp)
453 vm_object_pip_subtract(pp->object, 1);
457 page_hold(vnode_t *vp, int64_t start)
463 zfs_vmobject_assert_wlocked(obj);
466 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
468 if (vm_page_xbusied(pp)) {
470 * Reference the page before unlocking and
471 * sleeping so that the page daemon is less
472 * likely to reclaim it.
474 vm_page_reference(pp);
476 zfs_vmobject_wunlock(obj);
477 vm_page_busy_sleep(pp, "zfsmwb", true);
478 zfs_vmobject_wlock(obj);
482 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
495 page_unhold(vm_page_t pp)
504 * When a file is memory mapped, we must keep the IO data synchronized
505 * between the DMU cache and the memory mapped pages. What this means:
507 * On Write: If we find a memory mapped page, we write to *both*
508 * the page and the dmu buffer.
511 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
512 int segflg, dmu_tx_t *tx)
519 ASSERT(segflg != UIO_NOCOPY);
520 ASSERT(vp->v_mount != NULL);
524 off = start & PAGEOFFSET;
525 zfs_vmobject_wlock(obj);
526 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
528 int nbytes = imin(PAGESIZE - off, len);
530 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
531 zfs_vmobject_wunlock(obj);
533 va = zfs_map_page(pp, &sf);
534 (void) dmu_read(os, oid, start+off, nbytes,
535 va+off, DMU_READ_PREFETCH);;
538 zfs_vmobject_wlock(obj);
544 vm_object_pip_wakeupn(obj, 0);
545 zfs_vmobject_wunlock(obj);
549 * Read with UIO_NOCOPY flag means that sendfile(2) requests
550 * ZFS to populate a range of page cache pages with data.
552 * NOTE: this function could be optimized to pre-allocate
553 * all pages in advance, drain exclusive busy on all of them,
554 * map them into contiguous KVA region and populate them
555 * in one single dmu_read() call.
558 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
560 znode_t *zp = VTOZ(vp);
561 objset_t *os = zp->z_zfsvfs->z_os;
571 ASSERT(uio->uio_segflg == UIO_NOCOPY);
572 ASSERT(vp->v_mount != NULL);
575 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
577 zfs_vmobject_wlock(obj);
578 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
579 int bytes = MIN(PAGESIZE, len);
581 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY |
582 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
583 if (pp->valid == 0) {
584 zfs_vmobject_wunlock(obj);
585 va = zfs_map_page(pp, &sf);
586 error = dmu_read(os, zp->z_id, start, bytes, va,
588 if (bytes != PAGESIZE && error == 0)
589 bzero(va + bytes, PAGESIZE - bytes);
591 zfs_vmobject_wlock(obj);
595 if (pp->wire_count == 0 && pp->valid == 0 &&
599 pp->valid = VM_PAGE_BITS_ALL;
600 vm_page_activate(pp);
604 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
609 uio->uio_resid -= bytes;
610 uio->uio_offset += bytes;
613 zfs_vmobject_wunlock(obj);
618 * When a file is memory mapped, we must keep the IO data synchronized
619 * between the DMU cache and the memory mapped pages. What this means:
621 * On Read: We "read" preferentially from memory mapped pages,
622 * else we default from the dmu buffer.
624 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
625 * the file is memory mapped.
628 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
630 znode_t *zp = VTOZ(vp);
638 ASSERT(vp->v_mount != NULL);
642 start = uio->uio_loffset;
643 off = start & PAGEOFFSET;
644 zfs_vmobject_wlock(obj);
645 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
647 uint64_t bytes = MIN(PAGESIZE - off, len);
649 if (pp = page_hold(vp, start)) {
653 zfs_vmobject_wunlock(obj);
654 va = zfs_map_page(pp, &sf);
656 error = uiomove(va + off, bytes, UIO_READ, uio);
658 error = vn_io_fault_uiomove(va + off, bytes, uio);
661 zfs_vmobject_wlock(obj);
664 zfs_vmobject_wunlock(obj);
665 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
667 zfs_vmobject_wlock(obj);
674 zfs_vmobject_wunlock(obj);
678 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
681 * Read bytes from specified file into supplied buffer.
683 * IN: vp - vnode of file to be read from.
684 * uio - structure supplying read location, range info,
686 * ioflag - SYNC flags; used to provide FRSYNC semantics.
687 * cr - credentials of caller.
688 * ct - caller context
690 * OUT: uio - updated offset and range, buffer filled.
692 * RETURN: 0 on success, error code on failure.
695 * vp - atime updated if byte count > 0
699 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
701 znode_t *zp = VTOZ(vp);
702 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
711 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
713 return (SET_ERROR(EACCES));
717 * Validate file offset
719 if (uio->uio_loffset < (offset_t)0) {
721 return (SET_ERROR(EINVAL));
725 * Fasttrack empty reads
727 if (uio->uio_resid == 0) {
733 * Check for mandatory locks
735 if (MANDMODE(zp->z_mode)) {
736 if (error = chklock(vp, FREAD,
737 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
744 * If we're in FRSYNC mode, sync out this znode before reading it.
747 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
748 zil_commit(zfsvfs->z_log, zp->z_id);
751 * Lock the range against changes.
753 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
756 * If we are reading past end-of-file we can skip
757 * to the end; but we might still need to set atime.
759 if (uio->uio_loffset >= zp->z_size) {
764 ASSERT(uio->uio_loffset < zp->z_size);
765 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
768 if ((uio->uio_extflg == UIO_XUIO) &&
769 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
771 int blksz = zp->z_blksz;
772 uint64_t offset = uio->uio_loffset;
774 xuio = (xuio_t *)uio;
776 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
779 ASSERT(offset + n <= blksz);
782 (void) dmu_xuio_init(xuio, nblk);
784 if (vn_has_cached_data(vp)) {
786 * For simplicity, we always allocate a full buffer
787 * even if we only expect to read a portion of a block.
789 while (--nblk >= 0) {
790 (void) dmu_xuio_add(xuio,
791 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
799 nbytes = MIN(n, zfs_read_chunk_size -
800 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
803 if (uio->uio_segflg == UIO_NOCOPY)
804 error = mappedread_sf(vp, nbytes, uio);
806 #endif /* __FreeBSD__ */
807 if (vn_has_cached_data(vp)) {
808 error = mappedread(vp, nbytes, uio);
810 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
814 /* convert checksum errors into IO errors */
816 error = SET_ERROR(EIO);
823 zfs_range_unlock(rl);
825 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
831 * Write the bytes to a file.
833 * IN: vp - vnode of file to be written to.
834 * uio - structure supplying write location, range info,
836 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
837 * set if in append mode.
838 * cr - credentials of caller.
839 * ct - caller context (NFS/CIFS fem monitor only)
841 * OUT: uio - updated offset and range.
843 * RETURN: 0 on success, error code on failure.
846 * vp - ctime|mtime updated if byte count > 0
851 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
853 znode_t *zp = VTOZ(vp);
854 rlim64_t limit = MAXOFFSET_T;
855 ssize_t start_resid = uio->uio_resid;
859 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
864 int max_blksz = zfsvfs->z_max_blksz;
867 iovec_t *aiov = NULL;
870 int iovcnt = uio->uio_iovcnt;
871 iovec_t *iovp = uio->uio_iov;
874 sa_bulk_attr_t bulk[4];
875 uint64_t mtime[2], ctime[2];
878 * Fasttrack empty write
884 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
890 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
891 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
892 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
894 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
898 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
899 * callers might not be able to detect properly that we are read-only,
900 * so check it explicitly here.
902 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
904 return (SET_ERROR(EROFS));
908 * If immutable or not appending then return EPERM
910 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
911 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
912 (uio->uio_loffset < zp->z_size))) {
914 return (SET_ERROR(EPERM));
917 zilog = zfsvfs->z_log;
920 * Validate file offset
922 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
925 return (SET_ERROR(EINVAL));
929 * Check for mandatory locks before calling zfs_range_lock()
930 * in order to prevent a deadlock with locks set via fcntl().
932 if (MANDMODE((mode_t)zp->z_mode) &&
933 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
940 * Pre-fault the pages to ensure slow (eg NFS) pages
942 * Skip this if uio contains loaned arc_buf.
944 if ((uio->uio_extflg == UIO_XUIO) &&
945 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
946 xuio = (xuio_t *)uio;
948 uio_prefaultpages(MIN(n, max_blksz), uio);
952 * If in append mode, set the io offset pointer to eof.
954 if (ioflag & FAPPEND) {
956 * Obtain an appending range lock to guarantee file append
957 * semantics. We reset the write offset once we have the lock.
959 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
961 if (rl->r_len == UINT64_MAX) {
963 * We overlocked the file because this write will cause
964 * the file block size to increase.
965 * Note that zp_size cannot change with this lock held.
969 uio->uio_loffset = woff;
972 * Note that if the file block size will change as a result of
973 * this write, then this range lock will lock the entire file
974 * so that we can re-write the block safely.
976 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
979 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
980 zfs_range_unlock(rl);
986 zfs_range_unlock(rl);
988 return (SET_ERROR(EFBIG));
991 if ((woff + n) > limit || woff > (limit - n))
994 /* Will this write extend the file length? */
995 write_eof = (woff + n > zp->z_size);
997 end_size = MAX(zp->z_size, woff + n);
1000 * Write the file in reasonable size chunks. Each chunk is written
1001 * in a separate transaction; this keeps the intent log records small
1002 * and allows us to do more fine-grained space accounting.
1006 woff = uio->uio_loffset;
1007 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
1008 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
1010 dmu_return_arcbuf(abuf);
1011 error = SET_ERROR(EDQUOT);
1015 if (xuio && abuf == NULL) {
1016 ASSERT(i_iov < iovcnt);
1017 aiov = &iovp[i_iov];
1018 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1019 dmu_xuio_clear(xuio, i_iov);
1020 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1021 iovec_t *, aiov, arc_buf_t *, abuf);
1022 ASSERT((aiov->iov_base == abuf->b_data) ||
1023 ((char *)aiov->iov_base - (char *)abuf->b_data +
1024 aiov->iov_len == arc_buf_size(abuf)));
1026 } else if (abuf == NULL && n >= max_blksz &&
1027 woff >= zp->z_size &&
1028 P2PHASE(woff, max_blksz) == 0 &&
1029 zp->z_blksz == max_blksz) {
1031 * This write covers a full block. "Borrow" a buffer
1032 * from the dmu so that we can fill it before we enter
1033 * a transaction. This avoids the possibility of
1034 * holding up the transaction if the data copy hangs
1035 * up on a pagefault (e.g., from an NFS server mapping).
1041 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1043 ASSERT(abuf != NULL);
1044 ASSERT(arc_buf_size(abuf) == max_blksz);
1046 if (error = uiocopy(abuf->b_data, max_blksz,
1047 UIO_WRITE, uio, &cbytes)) {
1048 dmu_return_arcbuf(abuf);
1051 ASSERT(cbytes == max_blksz);
1053 ssize_t resid = uio->uio_resid;
1054 error = vn_io_fault_uiomove(abuf->b_data, max_blksz, uio);
1056 uio->uio_offset -= resid - uio->uio_resid;
1057 uio->uio_resid = resid;
1058 dmu_return_arcbuf(abuf);
1065 * Start a transaction.
1067 tx = dmu_tx_create(zfsvfs->z_os);
1068 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1069 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1070 zfs_sa_upgrade_txholds(tx, zp);
1071 error = dmu_tx_assign(tx, TXG_WAIT);
1075 dmu_return_arcbuf(abuf);
1080 * If zfs_range_lock() over-locked we grow the blocksize
1081 * and then reduce the lock range. This will only happen
1082 * on the first iteration since zfs_range_reduce() will
1083 * shrink down r_len to the appropriate size.
1085 if (rl->r_len == UINT64_MAX) {
1088 if (zp->z_blksz > max_blksz) {
1090 * File's blocksize is already larger than the
1091 * "recordsize" property. Only let it grow to
1092 * the next power of 2.
1094 ASSERT(!ISP2(zp->z_blksz));
1095 new_blksz = MIN(end_size,
1096 1 << highbit64(zp->z_blksz));
1098 new_blksz = MIN(end_size, max_blksz);
1100 zfs_grow_blocksize(zp, new_blksz, tx);
1101 zfs_range_reduce(rl, woff, n);
1105 * XXX - should we really limit each write to z_max_blksz?
1106 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1108 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1110 if (woff + nbytes > zp->z_size)
1111 vnode_pager_setsize(vp, woff + nbytes);
1114 tx_bytes = uio->uio_resid;
1115 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1117 tx_bytes -= uio->uio_resid;
1120 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1122 * If this is not a full block write, but we are
1123 * extending the file past EOF and this data starts
1124 * block-aligned, use assign_arcbuf(). Otherwise,
1125 * write via dmu_write().
1127 if (tx_bytes < max_blksz && (!write_eof ||
1128 aiov->iov_base != abuf->b_data)) {
1130 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1131 aiov->iov_len, aiov->iov_base, tx);
1132 dmu_return_arcbuf(abuf);
1133 xuio_stat_wbuf_copied();
1135 ASSERT(xuio || tx_bytes == max_blksz);
1136 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1140 ASSERT(tx_bytes <= uio->uio_resid);
1141 uioskip(uio, tx_bytes);
1144 if (tx_bytes && vn_has_cached_data(vp)) {
1145 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1146 zp->z_id, uio->uio_segflg, tx);
1150 * If we made no progress, we're done. If we made even
1151 * partial progress, update the znode and ZIL accordingly.
1153 if (tx_bytes == 0) {
1154 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1155 (void *)&zp->z_size, sizeof (uint64_t), tx);
1162 * Clear Set-UID/Set-GID bits on successful write if not
1163 * privileged and at least one of the excute bits is set.
1165 * It would be nice to to this after all writes have
1166 * been done, but that would still expose the ISUID/ISGID
1167 * to another app after the partial write is committed.
1169 * Note: we don't call zfs_fuid_map_id() here because
1170 * user 0 is not an ephemeral uid.
1172 mutex_enter(&zp->z_acl_lock);
1173 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1174 (S_IXUSR >> 6))) != 0 &&
1175 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1176 secpolicy_vnode_setid_retain(vp, cr,
1177 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1179 zp->z_mode &= ~(S_ISUID | S_ISGID);
1180 newmode = zp->z_mode;
1181 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1182 (void *)&newmode, sizeof (uint64_t), tx);
1184 mutex_exit(&zp->z_acl_lock);
1186 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1190 * Update the file size (zp_size) if it has changed;
1191 * account for possible concurrent updates.
1193 while ((end_size = zp->z_size) < uio->uio_loffset) {
1194 (void) atomic_cas_64(&zp->z_size, end_size,
1199 ASSERT(error == 0 || error == EFAULT);
1203 * If we are replaying and eof is non zero then force
1204 * the file size to the specified eof. Note, there's no
1205 * concurrency during replay.
1207 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1208 zp->z_size = zfsvfs->z_replay_eof;
1211 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1213 (void) sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1215 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1220 ASSERT(tx_bytes == nbytes);
1225 uio_prefaultpages(MIN(n, max_blksz), uio);
1229 zfs_range_unlock(rl);
1232 * If we're in replay mode, or we made no progress, return error.
1233 * Otherwise, it's at least a partial write, so it's successful.
1235 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1242 * EFAULT means that at least one page of the source buffer was not
1243 * available. VFS will re-try remaining I/O upon this error.
1245 if (error == EFAULT) {
1251 if (ioflag & (FSYNC | FDSYNC) ||
1252 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1253 zil_commit(zilog, zp->z_id);
1260 zfs_get_done(zgd_t *zgd, int error)
1262 znode_t *zp = zgd->zgd_private;
1263 objset_t *os = zp->z_zfsvfs->z_os;
1266 dmu_buf_rele(zgd->zgd_db, zgd);
1268 zfs_range_unlock(zgd->zgd_rl);
1271 * Release the vnode asynchronously as we currently have the
1272 * txg stopped from syncing.
1274 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1276 if (error == 0 && zgd->zgd_bp)
1277 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1279 kmem_free(zgd, sizeof (zgd_t));
1283 static int zil_fault_io = 0;
1287 * Get data to generate a TX_WRITE intent log record.
1290 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1292 zfsvfs_t *zfsvfs = arg;
1293 objset_t *os = zfsvfs->z_os;
1295 uint64_t object = lr->lr_foid;
1296 uint64_t offset = lr->lr_offset;
1297 uint64_t size = lr->lr_length;
1298 blkptr_t *bp = &lr->lr_blkptr;
1303 ASSERT(zio != NULL);
1307 * Nothing to do if the file has been removed
1309 if (zfs_zget(zfsvfs, object, &zp) != 0)
1310 return (SET_ERROR(ENOENT));
1311 if (zp->z_unlinked) {
1313 * Release the vnode asynchronously as we currently have the
1314 * txg stopped from syncing.
1316 VN_RELE_ASYNC(ZTOV(zp),
1317 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1318 return (SET_ERROR(ENOENT));
1321 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1322 zgd->zgd_zilog = zfsvfs->z_log;
1323 zgd->zgd_private = zp;
1326 * Write records come in two flavors: immediate and indirect.
1327 * For small writes it's cheaper to store the data with the
1328 * log record (immediate); for large writes it's cheaper to
1329 * sync the data and get a pointer to it (indirect) so that
1330 * we don't have to write the data twice.
1332 if (buf != NULL) { /* immediate write */
1333 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1334 /* test for truncation needs to be done while range locked */
1335 if (offset >= zp->z_size) {
1336 error = SET_ERROR(ENOENT);
1338 error = dmu_read(os, object, offset, size, buf,
1339 DMU_READ_NO_PREFETCH);
1341 ASSERT(error == 0 || error == ENOENT);
1342 } else { /* indirect write */
1344 * Have to lock the whole block to ensure when it's
1345 * written out and it's checksum is being calculated
1346 * that no one can change the data. We need to re-check
1347 * blocksize after we get the lock in case it's changed!
1352 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1354 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1356 if (zp->z_blksz == size)
1359 zfs_range_unlock(zgd->zgd_rl);
1361 /* test for truncation needs to be done while range locked */
1362 if (lr->lr_offset >= zp->z_size)
1363 error = SET_ERROR(ENOENT);
1366 error = SET_ERROR(EIO);
1371 error = dmu_buf_hold(os, object, offset, zgd, &db,
1372 DMU_READ_NO_PREFETCH);
1375 blkptr_t *obp = dmu_buf_get_blkptr(db);
1377 ASSERT(BP_IS_HOLE(bp));
1384 ASSERT(db->db_offset == offset);
1385 ASSERT(db->db_size == size);
1387 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1389 ASSERT(error || lr->lr_length <= size);
1392 * On success, we need to wait for the write I/O
1393 * initiated by dmu_sync() to complete before we can
1394 * release this dbuf. We will finish everything up
1395 * in the zfs_get_done() callback.
1400 if (error == EALREADY) {
1401 lr->lr_common.lrc_txtype = TX_WRITE2;
1407 zfs_get_done(zgd, error);
1414 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1415 caller_context_t *ct)
1417 znode_t *zp = VTOZ(vp);
1418 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1424 if (flag & V_ACE_MASK)
1425 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1427 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1434 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1439 error = vn_lock(*vpp, lkflags);
1446 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
1448 znode_t *zdp = VTOZ(dvp);
1449 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1453 ASSERT_VOP_LOCKED(dvp, __func__);
1455 if ((zdp->z_pflags & ZFS_XATTR) == 0)
1456 VERIFY(!RRM_LOCK_HELD(&zfsvfs->z_teardown_lock));
1459 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
1460 ASSERT3P(dvp, ==, vp);
1462 ltype = lkflags & LK_TYPE_MASK;
1463 if (ltype != VOP_ISLOCKED(dvp)) {
1464 if (ltype == LK_EXCLUSIVE)
1465 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1466 else /* if (ltype == LK_SHARED) */
1467 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1470 * Relock for the "." case could leave us with
1473 if (dvp->v_iflag & VI_DOOMED) {
1475 return (SET_ERROR(ENOENT));
1479 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
1481 * Note that in this case, dvp is the child vnode, and we
1482 * are looking up the parent vnode - exactly reverse from
1483 * normal operation. Unlocking dvp requires some rather
1484 * tricky unlock/relock dance to prevent mp from being freed;
1485 * use vn_vget_ino_gen() which takes care of all that.
1487 * XXX Note that there is a time window when both vnodes are
1488 * unlocked. It is possible, although highly unlikely, that
1489 * during that window the parent-child relationship between
1490 * the vnodes may change, for example, get reversed.
1491 * In that case we would have a wrong lock order for the vnodes.
1492 * All other filesystems seem to ignore this problem, so we
1494 * A potential solution could be implemented as follows:
1495 * - using LK_NOWAIT when locking the second vnode and retrying
1497 * - checking that the parent-child relationship still holds
1498 * after locking both vnodes and retrying if it doesn't
1500 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
1503 error = vn_lock(vp, lkflags);
1511 * Lookup an entry in a directory, or an extended attribute directory.
1512 * If it exists, return a held vnode reference for it.
1514 * IN: dvp - vnode of directory to search.
1515 * nm - name of entry to lookup.
1516 * pnp - full pathname to lookup [UNUSED].
1517 * flags - LOOKUP_XATTR set if looking for an attribute.
1518 * rdir - root directory vnode [UNUSED].
1519 * cr - credentials of caller.
1520 * ct - caller context
1522 * OUT: vpp - vnode of located entry, NULL if not found.
1524 * RETURN: 0 on success, error code on failure.
1531 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1532 int nameiop, cred_t *cr, kthread_t *td, int flags)
1534 znode_t *zdp = VTOZ(dvp);
1536 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1540 * Fast path lookup, however we must skip DNLC lookup
1541 * for case folding or normalizing lookups because the
1542 * DNLC code only stores the passed in name. This means
1543 * creating 'a' and removing 'A' on a case insensitive
1544 * file system would work, but DNLC still thinks 'a'
1545 * exists and won't let you create it again on the next
1546 * pass through fast path.
1548 if (!(flags & LOOKUP_XATTR)) {
1549 if (dvp->v_type != VDIR) {
1550 return (SET_ERROR(ENOTDIR));
1551 } else if (zdp->z_sa_hdl == NULL) {
1552 return (SET_ERROR(EIO));
1556 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1563 if (flags & LOOKUP_XATTR) {
1566 * If the xattr property is off, refuse the lookup request.
1568 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1570 return (SET_ERROR(EINVAL));
1575 * We don't allow recursive attributes..
1576 * Maybe someday we will.
1578 if (zdp->z_pflags & ZFS_XATTR) {
1580 return (SET_ERROR(EINVAL));
1583 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1589 * Do we have permission to get into attribute directory?
1591 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1602 * Check accessibility of directory.
1604 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1609 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1610 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1612 return (SET_ERROR(EILSEQ));
1617 * First handle the special cases.
1619 if ((cnp->cn_flags & ISDOTDOT) != 0) {
1621 * If we are a snapshot mounted under .zfs, return
1622 * the vp for the snapshot directory.
1624 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
1625 struct componentname cn;
1630 ltype = VOP_ISLOCKED(dvp);
1632 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
1635 cn.cn_nameptr = "snapshot";
1636 cn.cn_namelen = strlen(cn.cn_nameptr);
1637 cn.cn_nameiop = cnp->cn_nameiop;
1638 cn.cn_flags = cnp->cn_flags;
1639 cn.cn_lkflags = cnp->cn_lkflags;
1640 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
1643 vn_lock(dvp, ltype | LK_RETRY);
1647 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
1649 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
1650 return (SET_ERROR(ENOTSUP));
1651 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
1656 * The loop is retry the lookup if the parent-child relationship
1657 * changes during the dot-dot locking complexities.
1662 error = zfs_dirlook(zdp, nm, &zp);
1670 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
1673 * If we've got a locking error, then the vnode
1674 * got reclaimed because of a force unmount.
1675 * We never enter doomed vnodes into the name cache.
1681 if ((cnp->cn_flags & ISDOTDOT) == 0)
1685 if (zdp->z_sa_hdl == NULL) {
1686 error = SET_ERROR(EIO);
1688 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1689 &parent, sizeof (parent));
1696 if (zp->z_id == parent) {
1707 /* Translate errors and add SAVENAME when needed. */
1708 if (cnp->cn_flags & ISLASTCN) {
1712 if (error == ENOENT) {
1713 error = EJUSTRETURN;
1714 cnp->cn_flags |= SAVENAME;
1720 cnp->cn_flags |= SAVENAME;
1725 /* Insert name into cache (as non-existent) if appropriate. */
1726 if (zfsvfs->z_use_namecache &&
1727 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1728 cache_enter(dvp, NULL, cnp);
1730 /* Insert name into cache if appropriate. */
1731 if (zfsvfs->z_use_namecache &&
1732 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1733 if (!(cnp->cn_flags & ISLASTCN) ||
1734 (nameiop != DELETE && nameiop != RENAME)) {
1735 cache_enter(dvp, *vpp, cnp);
1743 * Attempt to create a new entry in a directory. If the entry
1744 * already exists, truncate the file if permissible, else return
1745 * an error. Return the vp of the created or trunc'd file.
1747 * IN: dvp - vnode of directory to put new file entry in.
1748 * name - name of new file entry.
1749 * vap - attributes of new file.
1750 * excl - flag indicating exclusive or non-exclusive mode.
1751 * mode - mode to open file with.
1752 * cr - credentials of caller.
1753 * flag - large file flag [UNUSED].
1754 * ct - caller context
1755 * vsecp - ACL to be set
1757 * OUT: vpp - vnode of created or trunc'd entry.
1759 * RETURN: 0 on success, error code on failure.
1762 * dvp - ctime|mtime updated if new entry created
1763 * vp - ctime|mtime always, atime if new
1768 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1769 vnode_t **vpp, cred_t *cr, kthread_t *td)
1771 znode_t *zp, *dzp = VTOZ(dvp);
1772 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1779 gid_t gid = crgetgid(cr);
1780 zfs_acl_ids_t acl_ids;
1781 boolean_t fuid_dirtied;
1787 * If we have an ephemeral id, ACL, or XVATTR then
1788 * make sure file system is at proper version
1791 ksid = crgetsid(cr, KSID_OWNER);
1793 uid = ksid_getid(ksid);
1797 if (zfsvfs->z_use_fuids == B_FALSE &&
1798 (vsecp || (vap->va_mask & AT_XVATTR) ||
1799 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1800 return (SET_ERROR(EINVAL));
1805 zilog = zfsvfs->z_log;
1807 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1808 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1810 return (SET_ERROR(EILSEQ));
1813 if (vap->va_mask & AT_XVATTR) {
1814 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1815 crgetuid(cr), cr, vap->va_type)) != 0) {
1823 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1824 vap->va_mode &= ~S_ISVTX;
1826 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1831 ASSERT3P(zp, ==, NULL);
1834 * Create a new file object and update the directory
1837 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1842 * We only support the creation of regular files in
1843 * extended attribute directories.
1846 if ((dzp->z_pflags & ZFS_XATTR) &&
1847 (vap->va_type != VREG)) {
1848 error = SET_ERROR(EINVAL);
1852 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1853 cr, vsecp, &acl_ids)) != 0)
1856 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1857 zfs_acl_ids_free(&acl_ids);
1858 error = SET_ERROR(EDQUOT);
1862 getnewvnode_reserve(1);
1864 tx = dmu_tx_create(os);
1866 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1867 ZFS_SA_BASE_ATTR_SIZE);
1869 fuid_dirtied = zfsvfs->z_fuid_dirty;
1871 zfs_fuid_txhold(zfsvfs, tx);
1872 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1873 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1874 if (!zfsvfs->z_use_sa &&
1875 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1876 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1877 0, acl_ids.z_aclp->z_acl_bytes);
1879 error = dmu_tx_assign(tx, TXG_WAIT);
1881 zfs_acl_ids_free(&acl_ids);
1883 getnewvnode_drop_reserve();
1887 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1890 zfs_fuid_sync(zfsvfs, tx);
1892 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1893 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1894 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1895 vsecp, acl_ids.z_fuidp, vap);
1896 zfs_acl_ids_free(&acl_ids);
1899 getnewvnode_drop_reserve();
1906 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1907 zil_commit(zilog, 0);
1914 * Remove an entry from a directory.
1916 * IN: dvp - vnode of directory to remove entry from.
1917 * name - name of entry to remove.
1918 * cr - credentials of caller.
1919 * ct - caller context
1920 * flags - case flags
1922 * RETURN: 0 on success, error code on failure.
1926 * vp - ctime (if nlink > 0)
1931 zfs_remove(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
1933 znode_t *dzp = VTOZ(dvp);
1934 znode_t *zp = VTOZ(vp);
1936 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1938 uint64_t acl_obj, xattr_obj;
1941 boolean_t unlinked, toobig = FALSE;
1948 zilog = zfsvfs->z_log;
1954 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1959 * Need to use rmdir for removing directories.
1961 if (vp->v_type == VDIR) {
1962 error = SET_ERROR(EPERM);
1966 vnevent_remove(vp, dvp, name, ct);
1970 /* are there any extended attributes? */
1971 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1972 &xattr_obj, sizeof (xattr_obj));
1973 if (error == 0 && xattr_obj) {
1974 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1979 * We may delete the znode now, or we may put it in the unlinked set;
1980 * it depends on whether we're the last link, and on whether there are
1981 * other holds on the vnode. So we dmu_tx_hold() the right things to
1982 * allow for either case.
1984 tx = dmu_tx_create(zfsvfs->z_os);
1985 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1986 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1987 zfs_sa_upgrade_txholds(tx, zp);
1988 zfs_sa_upgrade_txholds(tx, dzp);
1991 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1992 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1995 /* charge as an update -- would be nice not to charge at all */
1996 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1999 * Mark this transaction as typically resulting in a net free of space
2001 dmu_tx_mark_netfree(tx);
2003 error = dmu_tx_assign(tx, TXG_WAIT);
2011 * Remove the directory entry.
2013 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
2021 zfs_unlinked_add(zp, tx);
2022 vp->v_vflag |= VV_NOSYNC;
2026 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2034 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2035 zil_commit(zilog, 0);
2042 * Create a new directory and insert it into dvp using the name
2043 * provided. Return a pointer to the inserted directory.
2045 * IN: dvp - vnode of directory to add subdir to.
2046 * dirname - name of new directory.
2047 * vap - attributes of new directory.
2048 * cr - credentials of caller.
2049 * ct - caller context
2050 * flags - case flags
2051 * vsecp - ACL to be set
2053 * OUT: vpp - vnode of created directory.
2055 * RETURN: 0 on success, error code on failure.
2058 * dvp - ctime|mtime updated
2059 * vp - ctime|mtime|atime updated
2063 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
2065 znode_t *zp, *dzp = VTOZ(dvp);
2066 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2073 gid_t gid = crgetgid(cr);
2074 zfs_acl_ids_t acl_ids;
2075 boolean_t fuid_dirtied;
2077 ASSERT(vap->va_type == VDIR);
2080 * If we have an ephemeral id, ACL, or XVATTR then
2081 * make sure file system is at proper version
2084 ksid = crgetsid(cr, KSID_OWNER);
2086 uid = ksid_getid(ksid);
2089 if (zfsvfs->z_use_fuids == B_FALSE &&
2090 ((vap->va_mask & AT_XVATTR) ||
2091 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2092 return (SET_ERROR(EINVAL));
2096 zilog = zfsvfs->z_log;
2098 if (dzp->z_pflags & ZFS_XATTR) {
2100 return (SET_ERROR(EINVAL));
2103 if (zfsvfs->z_utf8 && u8_validate(dirname,
2104 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2106 return (SET_ERROR(EILSEQ));
2109 if (vap->va_mask & AT_XVATTR) {
2110 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2111 crgetuid(cr), cr, vap->va_type)) != 0) {
2117 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2118 NULL, &acl_ids)) != 0) {
2124 * First make sure the new directory doesn't exist.
2126 * Existence is checked first to make sure we don't return
2127 * EACCES instead of EEXIST which can cause some applications
2132 if (error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW)) {
2133 zfs_acl_ids_free(&acl_ids);
2137 ASSERT3P(zp, ==, NULL);
2139 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2140 zfs_acl_ids_free(&acl_ids);
2145 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2146 zfs_acl_ids_free(&acl_ids);
2148 return (SET_ERROR(EDQUOT));
2152 * Add a new entry to the directory.
2154 getnewvnode_reserve(1);
2155 tx = dmu_tx_create(zfsvfs->z_os);
2156 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2157 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2158 fuid_dirtied = zfsvfs->z_fuid_dirty;
2160 zfs_fuid_txhold(zfsvfs, tx);
2161 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2162 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2163 acl_ids.z_aclp->z_acl_bytes);
2166 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2167 ZFS_SA_BASE_ATTR_SIZE);
2169 error = dmu_tx_assign(tx, TXG_WAIT);
2171 zfs_acl_ids_free(&acl_ids);
2173 getnewvnode_drop_reserve();
2181 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2184 zfs_fuid_sync(zfsvfs, tx);
2187 * Now put new name in parent dir.
2189 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
2193 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
2194 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
2195 acl_ids.z_fuidp, vap);
2197 zfs_acl_ids_free(&acl_ids);
2201 getnewvnode_drop_reserve();
2203 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2204 zil_commit(zilog, 0);
2211 * Remove a directory subdir entry. If the current working
2212 * directory is the same as the subdir to be removed, the
2215 * IN: dvp - vnode of directory to remove from.
2216 * name - name of directory to be removed.
2217 * cwd - vnode of current working directory.
2218 * cr - credentials of caller.
2219 * ct - caller context
2220 * flags - case flags
2222 * RETURN: 0 on success, error code on failure.
2225 * dvp - ctime|mtime updated
2229 zfs_rmdir(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
2231 znode_t *dzp = VTOZ(dvp);
2232 znode_t *zp = VTOZ(vp);
2233 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2241 zilog = zfsvfs->z_log;
2244 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2248 if (vp->v_type != VDIR) {
2249 error = SET_ERROR(ENOTDIR);
2253 vnevent_rmdir(vp, dvp, name, ct);
2255 tx = dmu_tx_create(zfsvfs->z_os);
2256 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2257 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2258 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2259 zfs_sa_upgrade_txholds(tx, zp);
2260 zfs_sa_upgrade_txholds(tx, dzp);
2261 dmu_tx_mark_netfree(tx);
2262 error = dmu_tx_assign(tx, TXG_WAIT);
2271 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
2274 uint64_t txtype = TX_RMDIR;
2275 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2282 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2283 zil_commit(zilog, 0);
2290 * Read as many directory entries as will fit into the provided
2291 * buffer from the given directory cursor position (specified in
2292 * the uio structure).
2294 * IN: vp - vnode of directory to read.
2295 * uio - structure supplying read location, range info,
2296 * and return buffer.
2297 * cr - credentials of caller.
2298 * ct - caller context
2299 * flags - case flags
2301 * OUT: uio - updated offset and range, buffer filled.
2302 * eofp - set to true if end-of-file detected.
2304 * RETURN: 0 on success, error code on failure.
2307 * vp - atime updated
2309 * Note that the low 4 bits of the cookie returned by zap is always zero.
2310 * This allows us to use the low range for "special" directory entries:
2311 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2312 * we use the offset 2 for the '.zfs' directory.
2316 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2318 znode_t *zp = VTOZ(vp);
2322 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2327 zap_attribute_t zap;
2328 uint_t bytes_wanted;
2329 uint64_t offset; /* must be unsigned; checks for < 1 */
2335 boolean_t check_sysattrs;
2338 u_long *cooks = NULL;
2344 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2345 &parent, sizeof (parent))) != 0) {
2351 * If we are not given an eof variable,
2358 * Check for valid iov_len.
2360 if (uio->uio_iov->iov_len <= 0) {
2362 return (SET_ERROR(EINVAL));
2366 * Quit if directory has been removed (posix)
2368 if ((*eofp = zp->z_unlinked) != 0) {
2375 offset = uio->uio_loffset;
2376 prefetch = zp->z_zn_prefetch;
2379 * Initialize the iterator cursor.
2383 * Start iteration from the beginning of the directory.
2385 zap_cursor_init(&zc, os, zp->z_id);
2388 * The offset is a serialized cursor.
2390 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2394 * Get space to change directory entries into fs independent format.
2396 iovp = uio->uio_iov;
2397 bytes_wanted = iovp->iov_len;
2398 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2399 bufsize = bytes_wanted;
2400 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2401 odp = (struct dirent64 *)outbuf;
2403 bufsize = bytes_wanted;
2405 odp = (struct dirent64 *)iovp->iov_base;
2407 eodp = (struct edirent *)odp;
2409 if (ncookies != NULL) {
2411 * Minimum entry size is dirent size and 1 byte for a file name.
2413 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2414 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2419 * If this VFS supports the system attribute view interface; and
2420 * we're looking at an extended attribute directory; and we care
2421 * about normalization conflicts on this vfs; then we must check
2422 * for normalization conflicts with the sysattr name space.
2425 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2426 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2427 (flags & V_RDDIR_ENTFLAGS);
2433 * Transform to file-system independent format
2436 while (outcount < bytes_wanted) {
2439 off64_t *next = NULL;
2442 * Special case `.', `..', and `.zfs'.
2445 (void) strcpy(zap.za_name, ".");
2446 zap.za_normalization_conflict = 0;
2449 } else if (offset == 1) {
2450 (void) strcpy(zap.za_name, "..");
2451 zap.za_normalization_conflict = 0;
2454 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2455 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2456 zap.za_normalization_conflict = 0;
2457 objnum = ZFSCTL_INO_ROOT;
2463 if (error = zap_cursor_retrieve(&zc, &zap)) {
2464 if ((*eofp = (error == ENOENT)) != 0)
2470 if (zap.za_integer_length != 8 ||
2471 zap.za_num_integers != 1) {
2472 cmn_err(CE_WARN, "zap_readdir: bad directory "
2473 "entry, obj = %lld, offset = %lld\n",
2474 (u_longlong_t)zp->z_id,
2475 (u_longlong_t)offset);
2476 error = SET_ERROR(ENXIO);
2480 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2482 * MacOS X can extract the object type here such as:
2483 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2485 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2487 if (check_sysattrs && !zap.za_normalization_conflict) {
2489 zap.za_normalization_conflict =
2490 xattr_sysattr_casechk(zap.za_name);
2492 panic("%s:%u: TODO", __func__, __LINE__);
2497 if (flags & V_RDDIR_ACCFILTER) {
2499 * If we have no access at all, don't include
2500 * this entry in the returned information
2503 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2505 if (!zfs_has_access(ezp, cr)) {
2512 if (flags & V_RDDIR_ENTFLAGS)
2513 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2515 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2518 * Will this entry fit in the buffer?
2520 if (outcount + reclen > bufsize) {
2522 * Did we manage to fit anything in the buffer?
2525 error = SET_ERROR(EINVAL);
2530 if (flags & V_RDDIR_ENTFLAGS) {
2532 * Add extended flag entry:
2534 eodp->ed_ino = objnum;
2535 eodp->ed_reclen = reclen;
2536 /* NOTE: ed_off is the offset for the *next* entry */
2537 next = &(eodp->ed_off);
2538 eodp->ed_eflags = zap.za_normalization_conflict ?
2539 ED_CASE_CONFLICT : 0;
2540 (void) strncpy(eodp->ed_name, zap.za_name,
2541 EDIRENT_NAMELEN(reclen));
2542 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2547 odp->d_ino = objnum;
2548 odp->d_reclen = reclen;
2549 odp->d_namlen = strlen(zap.za_name);
2550 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2552 odp = (dirent64_t *)((intptr_t)odp + reclen);
2556 ASSERT(outcount <= bufsize);
2558 /* Prefetch znode */
2560 dmu_prefetch(os, objnum, 0, 0, 0,
2561 ZIO_PRIORITY_SYNC_READ);
2565 * Move to the next entry, fill in the previous offset.
2567 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2568 zap_cursor_advance(&zc);
2569 offset = zap_cursor_serialize(&zc);
2574 if (cooks != NULL) {
2577 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2580 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2582 /* Subtract unused cookies */
2583 if (ncookies != NULL)
2584 *ncookies -= ncooks;
2586 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2587 iovp->iov_base += outcount;
2588 iovp->iov_len -= outcount;
2589 uio->uio_resid -= outcount;
2590 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2592 * Reset the pointer.
2594 offset = uio->uio_loffset;
2598 zap_cursor_fini(&zc);
2599 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2600 kmem_free(outbuf, bufsize);
2602 if (error == ENOENT)
2605 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2607 uio->uio_loffset = offset;
2609 if (error != 0 && cookies != NULL) {
2610 free(*cookies, M_TEMP);
2617 ulong_t zfs_fsync_sync_cnt = 4;
2620 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2622 znode_t *zp = VTOZ(vp);
2623 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2625 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2627 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2630 zil_commit(zfsvfs->z_log, zp->z_id);
2638 * Get the requested file attributes and place them in the provided
2641 * IN: vp - vnode of file.
2642 * vap - va_mask identifies requested attributes.
2643 * If AT_XVATTR set, then optional attrs are requested
2644 * flags - ATTR_NOACLCHECK (CIFS server context)
2645 * cr - credentials of caller.
2646 * ct - caller context
2648 * OUT: vap - attribute values.
2650 * RETURN: 0 (always succeeds).
2654 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2655 caller_context_t *ct)
2657 znode_t *zp = VTOZ(vp);
2658 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2661 u_longlong_t nblocks;
2663 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2664 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2665 xoptattr_t *xoap = NULL;
2666 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2667 sa_bulk_attr_t bulk[4];
2673 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2675 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2676 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2677 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2678 if (vp->v_type == VBLK || vp->v_type == VCHR)
2679 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2682 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2688 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2689 * Also, if we are the owner don't bother, since owner should
2690 * always be allowed to read basic attributes of file.
2692 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2693 (vap->va_uid != crgetuid(cr))) {
2694 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2702 * Return all attributes. It's cheaper to provide the answer
2703 * than to determine whether we were asked the question.
2706 vap->va_type = IFTOVT(zp->z_mode);
2707 vap->va_mode = zp->z_mode & ~S_IFMT;
2709 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2711 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2713 vap->va_nodeid = zp->z_id;
2714 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2715 links = zp->z_links + 1;
2717 links = zp->z_links;
2718 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2719 vap->va_size = zp->z_size;
2721 vap->va_rdev = vp->v_rdev;
2723 if (vp->v_type == VBLK || vp->v_type == VCHR)
2724 vap->va_rdev = zfs_cmpldev(rdev);
2726 vap->va_seq = zp->z_seq;
2727 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2728 vap->va_filerev = zp->z_seq;
2731 * Add in any requested optional attributes and the create time.
2732 * Also set the corresponding bits in the returned attribute bitmap.
2734 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2735 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2737 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2738 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2741 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2742 xoap->xoa_readonly =
2743 ((zp->z_pflags & ZFS_READONLY) != 0);
2744 XVA_SET_RTN(xvap, XAT_READONLY);
2747 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2749 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2750 XVA_SET_RTN(xvap, XAT_SYSTEM);
2753 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2755 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2756 XVA_SET_RTN(xvap, XAT_HIDDEN);
2759 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2760 xoap->xoa_nounlink =
2761 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2762 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2765 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2766 xoap->xoa_immutable =
2767 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2768 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2771 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2772 xoap->xoa_appendonly =
2773 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2774 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2777 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2779 ((zp->z_pflags & ZFS_NODUMP) != 0);
2780 XVA_SET_RTN(xvap, XAT_NODUMP);
2783 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2785 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2786 XVA_SET_RTN(xvap, XAT_OPAQUE);
2789 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2790 xoap->xoa_av_quarantined =
2791 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2792 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2795 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2796 xoap->xoa_av_modified =
2797 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2798 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2801 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2802 vp->v_type == VREG) {
2803 zfs_sa_get_scanstamp(zp, xvap);
2806 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2807 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2808 XVA_SET_RTN(xvap, XAT_REPARSE);
2810 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2811 xoap->xoa_generation = zp->z_gen;
2812 XVA_SET_RTN(xvap, XAT_GEN);
2815 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2817 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2818 XVA_SET_RTN(xvap, XAT_OFFLINE);
2821 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2823 ((zp->z_pflags & ZFS_SPARSE) != 0);
2824 XVA_SET_RTN(xvap, XAT_SPARSE);
2828 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2829 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2830 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2831 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2834 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2835 vap->va_blksize = blksize;
2836 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2838 if (zp->z_blksz == 0) {
2840 * Block size hasn't been set; suggest maximal I/O transfers.
2842 vap->va_blksize = zfsvfs->z_max_blksz;
2850 * Set the file attributes to the values contained in the
2853 * IN: vp - vnode of file to be modified.
2854 * vap - new attribute values.
2855 * If AT_XVATTR set, then optional attrs are being set
2856 * flags - ATTR_UTIME set if non-default time values provided.
2857 * - ATTR_NOACLCHECK (CIFS context only).
2858 * cr - credentials of caller.
2859 * ct - caller context
2861 * RETURN: 0 on success, error code on failure.
2864 * vp - ctime updated, mtime updated if size changed.
2868 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2869 caller_context_t *ct)
2871 znode_t *zp = VTOZ(vp);
2872 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2877 uint_t mask = vap->va_mask;
2878 uint_t saved_mask = 0;
2879 uint64_t saved_mode;
2882 uint64_t new_uid, new_gid;
2884 uint64_t mtime[2], ctime[2];
2886 int need_policy = FALSE;
2888 zfs_fuid_info_t *fuidp = NULL;
2889 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2892 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2893 boolean_t fuid_dirtied = B_FALSE;
2894 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2895 int count = 0, xattr_count = 0;
2900 if (mask & AT_NOSET)
2901 return (SET_ERROR(EINVAL));
2906 zilog = zfsvfs->z_log;
2909 * Make sure that if we have ephemeral uid/gid or xvattr specified
2910 * that file system is at proper version level
2913 if (zfsvfs->z_use_fuids == B_FALSE &&
2914 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2915 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2916 (mask & AT_XVATTR))) {
2918 return (SET_ERROR(EINVAL));
2921 if (mask & AT_SIZE && vp->v_type == VDIR) {
2923 return (SET_ERROR(EISDIR));
2926 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2928 return (SET_ERROR(EINVAL));
2932 * If this is an xvattr_t, then get a pointer to the structure of
2933 * optional attributes. If this is NULL, then we have a vattr_t.
2935 xoap = xva_getxoptattr(xvap);
2937 xva_init(&tmpxvattr);
2940 * Immutable files can only alter immutable bit and atime
2942 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2943 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2944 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2946 return (SET_ERROR(EPERM));
2949 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2951 return (SET_ERROR(EPERM));
2955 * Verify timestamps doesn't overflow 32 bits.
2956 * ZFS can handle large timestamps, but 32bit syscalls can't
2957 * handle times greater than 2039. This check should be removed
2958 * once large timestamps are fully supported.
2960 if (mask & (AT_ATIME | AT_MTIME)) {
2961 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2962 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2964 return (SET_ERROR(EOVERFLOW));
2967 if (xoap && (mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2968 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2970 return (SET_ERROR(EOVERFLOW));
2976 /* Can this be moved to before the top label? */
2977 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2979 return (SET_ERROR(EROFS));
2983 * First validate permissions
2986 if (mask & AT_SIZE) {
2988 * XXX - Note, we are not providing any open
2989 * mode flags here (like FNDELAY), so we may
2990 * block if there are locks present... this
2991 * should be addressed in openat().
2993 /* XXX - would it be OK to generate a log record here? */
2994 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3001 if (mask & (AT_ATIME|AT_MTIME) ||
3002 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3003 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3004 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3005 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3006 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3007 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3008 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3009 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3013 if (mask & (AT_UID|AT_GID)) {
3014 int idmask = (mask & (AT_UID|AT_GID));
3019 * NOTE: even if a new mode is being set,
3020 * we may clear S_ISUID/S_ISGID bits.
3023 if (!(mask & AT_MODE))
3024 vap->va_mode = zp->z_mode;
3027 * Take ownership or chgrp to group we are a member of
3030 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3031 take_group = (mask & AT_GID) &&
3032 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3035 * If both AT_UID and AT_GID are set then take_owner and
3036 * take_group must both be set in order to allow taking
3039 * Otherwise, send the check through secpolicy_vnode_setattr()
3043 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3044 ((idmask == AT_UID) && take_owner) ||
3045 ((idmask == AT_GID) && take_group)) {
3046 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3047 skipaclchk, cr) == 0) {
3049 * Remove setuid/setgid for non-privileged users
3051 secpolicy_setid_clear(vap, vp, cr);
3052 trim_mask = (mask & (AT_UID|AT_GID));
3061 oldva.va_mode = zp->z_mode;
3062 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3063 if (mask & AT_XVATTR) {
3065 * Update xvattr mask to include only those attributes
3066 * that are actually changing.
3068 * the bits will be restored prior to actually setting
3069 * the attributes so the caller thinks they were set.
3071 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3072 if (xoap->xoa_appendonly !=
3073 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3076 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3077 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3081 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3082 if (xoap->xoa_nounlink !=
3083 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3086 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3087 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3091 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3092 if (xoap->xoa_immutable !=
3093 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3096 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3097 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3101 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3102 if (xoap->xoa_nodump !=
3103 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3106 XVA_CLR_REQ(xvap, XAT_NODUMP);
3107 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3111 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3112 if (xoap->xoa_av_modified !=
3113 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3116 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3117 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3121 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3122 if ((vp->v_type != VREG &&
3123 xoap->xoa_av_quarantined) ||
3124 xoap->xoa_av_quarantined !=
3125 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3128 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3129 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3133 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3135 return (SET_ERROR(EPERM));
3138 if (need_policy == FALSE &&
3139 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3140 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3145 if (mask & AT_MODE) {
3146 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3147 err = secpolicy_setid_setsticky_clear(vp, vap,
3153 trim_mask |= AT_MODE;
3161 * If trim_mask is set then take ownership
3162 * has been granted or write_acl is present and user
3163 * has the ability to modify mode. In that case remove
3164 * UID|GID and or MODE from mask so that
3165 * secpolicy_vnode_setattr() doesn't revoke it.
3169 saved_mask = vap->va_mask;
3170 vap->va_mask &= ~trim_mask;
3171 if (trim_mask & AT_MODE) {
3173 * Save the mode, as secpolicy_vnode_setattr()
3174 * will overwrite it with ova.va_mode.
3176 saved_mode = vap->va_mode;
3179 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3180 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3187 vap->va_mask |= saved_mask;
3188 if (trim_mask & AT_MODE) {
3190 * Recover the mode after
3191 * secpolicy_vnode_setattr().
3193 vap->va_mode = saved_mode;
3199 * secpolicy_vnode_setattr, or take ownership may have
3202 mask = vap->va_mask;
3204 if ((mask & (AT_UID | AT_GID))) {
3205 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3206 &xattr_obj, sizeof (xattr_obj));
3208 if (err == 0 && xattr_obj) {
3209 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3211 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
3213 vrele(ZTOV(attrzp));
3218 if (mask & AT_UID) {
3219 new_uid = zfs_fuid_create(zfsvfs,
3220 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3221 if (new_uid != zp->z_uid &&
3222 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3225 err = SET_ERROR(EDQUOT);
3230 if (mask & AT_GID) {
3231 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3232 cr, ZFS_GROUP, &fuidp);
3233 if (new_gid != zp->z_gid &&
3234 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3237 err = SET_ERROR(EDQUOT);
3242 tx = dmu_tx_create(zfsvfs->z_os);
3244 if (mask & AT_MODE) {
3245 uint64_t pmode = zp->z_mode;
3247 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3249 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3250 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3251 err = SET_ERROR(EPERM);
3255 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3258 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3260 * Are we upgrading ACL from old V0 format
3263 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3264 zfs_znode_acl_version(zp) ==
3265 ZFS_ACL_VERSION_INITIAL) {
3266 dmu_tx_hold_free(tx, acl_obj, 0,
3268 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3269 0, aclp->z_acl_bytes);
3271 dmu_tx_hold_write(tx, acl_obj, 0,
3274 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3275 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3276 0, aclp->z_acl_bytes);
3278 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3280 if ((mask & AT_XVATTR) &&
3281 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3282 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3284 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3288 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3291 fuid_dirtied = zfsvfs->z_fuid_dirty;
3293 zfs_fuid_txhold(zfsvfs, tx);
3295 zfs_sa_upgrade_txholds(tx, zp);
3297 err = dmu_tx_assign(tx, TXG_WAIT);
3303 * Set each attribute requested.
3304 * We group settings according to the locks they need to acquire.
3306 * Note: you cannot set ctime directly, although it will be
3307 * updated as a side-effect of calling this function.
3310 if (mask & (AT_UID|AT_GID|AT_MODE))
3311 mutex_enter(&zp->z_acl_lock);
3313 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3314 &zp->z_pflags, sizeof (zp->z_pflags));
3317 if (mask & (AT_UID|AT_GID|AT_MODE))
3318 mutex_enter(&attrzp->z_acl_lock);
3319 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3320 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3321 sizeof (attrzp->z_pflags));
3324 if (mask & (AT_UID|AT_GID)) {
3326 if (mask & AT_UID) {
3327 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3328 &new_uid, sizeof (new_uid));
3329 zp->z_uid = new_uid;
3331 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3332 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3334 attrzp->z_uid = new_uid;
3338 if (mask & AT_GID) {
3339 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3340 NULL, &new_gid, sizeof (new_gid));
3341 zp->z_gid = new_gid;
3343 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3344 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3346 attrzp->z_gid = new_gid;
3349 if (!(mask & AT_MODE)) {
3350 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3351 NULL, &new_mode, sizeof (new_mode));
3352 new_mode = zp->z_mode;
3354 err = zfs_acl_chown_setattr(zp);
3357 err = zfs_acl_chown_setattr(attrzp);
3362 if (mask & AT_MODE) {
3363 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3364 &new_mode, sizeof (new_mode));
3365 zp->z_mode = new_mode;
3366 ASSERT3U((uintptr_t)aclp, !=, 0);
3367 err = zfs_aclset_common(zp, aclp, cr, tx);
3369 if (zp->z_acl_cached)
3370 zfs_acl_free(zp->z_acl_cached);
3371 zp->z_acl_cached = aclp;
3376 if (mask & AT_ATIME) {
3377 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3378 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3379 &zp->z_atime, sizeof (zp->z_atime));
3382 if (mask & AT_MTIME) {
3383 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3384 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3385 mtime, sizeof (mtime));
3388 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3389 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3390 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3391 NULL, mtime, sizeof (mtime));
3392 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3393 &ctime, sizeof (ctime));
3394 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3396 } else if (mask != 0) {
3397 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3398 &ctime, sizeof (ctime));
3399 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3402 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3403 SA_ZPL_CTIME(zfsvfs), NULL,
3404 &ctime, sizeof (ctime));
3405 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3406 mtime, ctime, B_TRUE);
3410 * Do this after setting timestamps to prevent timestamp
3411 * update from toggling bit
3414 if (xoap && (mask & AT_XVATTR)) {
3416 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
3417 xoap->xoa_createtime = vap->va_birthtime;
3419 * restore trimmed off masks
3420 * so that return masks can be set for caller.
3423 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3424 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3426 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3427 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3429 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3430 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3432 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3433 XVA_SET_REQ(xvap, XAT_NODUMP);
3435 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3436 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3438 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3439 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3442 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3443 ASSERT(vp->v_type == VREG);
3445 zfs_xvattr_set(zp, xvap, tx);
3449 zfs_fuid_sync(zfsvfs, tx);
3452 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3454 if (mask & (AT_UID|AT_GID|AT_MODE))
3455 mutex_exit(&zp->z_acl_lock);
3458 if (mask & (AT_UID|AT_GID|AT_MODE))
3459 mutex_exit(&attrzp->z_acl_lock);
3462 if (err == 0 && attrzp) {
3463 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3475 zfs_fuid_info_free(fuidp);
3482 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3487 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3488 zil_commit(zilog, 0);
3495 * We acquire all but fdvp locks using non-blocking acquisitions. If we
3496 * fail to acquire any lock in the path we will drop all held locks,
3497 * acquire the new lock in a blocking fashion, and then release it and
3498 * restart the rename. This acquire/release step ensures that we do not
3499 * spin on a lock waiting for release. On error release all vnode locks
3500 * and decrement references the way tmpfs_rename() would do.
3503 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
3504 struct vnode *tdvp, struct vnode **tvpp,
3505 const struct componentname *scnp, const struct componentname *tcnp)
3508 struct vnode *nvp, *svp, *tvp;
3509 znode_t *sdzp, *tdzp, *szp, *tzp;
3510 const char *snm = scnp->cn_nameptr;
3511 const char *tnm = tcnp->cn_nameptr;
3514 VOP_UNLOCK(tdvp, 0);
3515 if (*tvpp != NULL && *tvpp != tdvp)
3516 VOP_UNLOCK(*tvpp, 0);
3519 error = vn_lock(sdvp, LK_EXCLUSIVE);
3524 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
3526 VOP_UNLOCK(sdvp, 0);
3529 error = vn_lock(tdvp, LK_EXCLUSIVE);
3532 VOP_UNLOCK(tdvp, 0);
3538 * Before using sdzp and tdzp we must ensure that they are live.
3539 * As a porting legacy from illumos we have two things to worry
3540 * about. One is typical for FreeBSD and it is that the vnode is
3541 * not reclaimed (doomed). The other is that the znode is live.
3542 * The current code can invalidate the znode without acquiring the
3543 * corresponding vnode lock if the object represented by the znode
3544 * and vnode is no longer valid after a rollback or receive operation.
3545 * z_teardown_lock hidden behind ZFS_ENTER and ZFS_EXIT is the lock
3546 * that protects the znodes from the invalidation.
3548 zfsvfs = sdzp->z_zfsvfs;
3549 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
3553 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3554 * bypassing the cleanup code in the case of an error.
3556 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3558 VOP_UNLOCK(sdvp, 0);
3559 VOP_UNLOCK(tdvp, 0);
3560 error = SET_ERROR(EIO);
3565 * Re-resolve svp to be certain it still exists and fetch the
3568 error = zfs_dirent_lookup(sdzp, snm, &szp, ZEXISTS);
3570 /* Source entry invalid or not there. */
3572 VOP_UNLOCK(sdvp, 0);
3573 VOP_UNLOCK(tdvp, 0);
3574 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
3575 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
3576 error = SET_ERROR(EINVAL);
3582 * Re-resolve tvp, if it disappeared we just carry on.
3584 error = zfs_dirent_lookup(tdzp, tnm, &tzp, 0);
3587 VOP_UNLOCK(sdvp, 0);
3588 VOP_UNLOCK(tdvp, 0);
3590 if ((tcnp->cn_flags & ISDOTDOT) != 0)
3591 error = SET_ERROR(EINVAL);
3600 * At present the vnode locks must be acquired before z_teardown_lock,
3601 * although it would be more logical to use the opposite order.
3606 * Now try acquire locks on svp and tvp.
3609 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3611 VOP_UNLOCK(sdvp, 0);
3612 VOP_UNLOCK(tdvp, 0);
3615 if (error != EBUSY) {
3619 error = vn_lock(nvp, LK_EXCLUSIVE);
3626 * Concurrent rename race.
3631 error = SET_ERROR(EINVAL);
3646 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3648 VOP_UNLOCK(sdvp, 0);
3649 VOP_UNLOCK(tdvp, 0);
3650 VOP_UNLOCK(*svpp, 0);
3651 if (error != EBUSY) {
3655 error = vn_lock(nvp, LK_EXCLUSIVE);
3673 * Note that we must use VRELE_ASYNC in this function as it walks
3674 * up the directory tree and vrele may need to acquire an exclusive
3675 * lock if a last reference to a vnode is dropped.
3678 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3685 zfsvfs = tdzp->z_zfsvfs;
3687 return (SET_ERROR(EINVAL));
3690 if (tdzp->z_id == zfsvfs->z_root)
3694 ASSERT(!zp->z_unlinked);
3695 if ((error = sa_lookup(zp->z_sa_hdl,
3696 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3699 if (parent == szp->z_id) {
3700 error = SET_ERROR(EINVAL);
3703 if (parent == zfsvfs->z_root)
3705 if (parent == sdzp->z_id)
3708 error = zfs_zget(zfsvfs, parent, &zp1);
3713 VN_RELE_ASYNC(ZTOV(zp),
3714 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3718 if (error == ENOTDIR)
3719 panic("checkpath: .. not a directory\n");
3721 VN_RELE_ASYNC(ZTOV(zp),
3722 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3727 * Move an entry from the provided source directory to the target
3728 * directory. Change the entry name as indicated.
3730 * IN: sdvp - Source directory containing the "old entry".
3731 * snm - Old entry name.
3732 * tdvp - Target directory to contain the "new entry".
3733 * tnm - New entry name.
3734 * cr - credentials of caller.
3735 * ct - caller context
3736 * flags - case flags
3738 * RETURN: 0 on success, error code on failure.
3741 * sdvp,tdvp - ctime|mtime updated
3745 zfs_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3746 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3750 znode_t *sdzp, *tdzp, *szp, *tzp;
3751 zilog_t *zilog = NULL;
3753 char *snm = scnp->cn_nameptr;
3754 char *tnm = tcnp->cn_nameptr;
3757 /* Reject renames across filesystems. */
3758 if ((*svpp)->v_mount != tdvp->v_mount ||
3759 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3760 error = SET_ERROR(EXDEV);
3764 if (zfsctl_is_node(tdvp)) {
3765 error = SET_ERROR(EXDEV);
3770 * Lock all four vnodes to ensure safety and semantics of renaming.
3772 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3774 /* no vnodes are locked in the case of error here */
3780 zfsvfs = tdzp->z_zfsvfs;
3781 zilog = zfsvfs->z_log;
3784 * After we re-enter ZFS_ENTER() we will have to revalidate all
3789 if (zfsvfs->z_utf8 && u8_validate(tnm,
3790 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3791 error = SET_ERROR(EILSEQ);
3795 /* If source and target are the same file, there is nothing to do. */
3796 if ((*svpp) == (*tvpp)) {
3801 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3802 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3803 (*tvpp)->v_mountedhere != NULL)) {
3804 error = SET_ERROR(EXDEV);
3809 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3810 * bypassing the cleanup code in the case of an error.
3812 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3813 error = SET_ERROR(EIO);
3818 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3819 if (szp->z_sa_hdl == NULL || (tzp != NULL && tzp->z_sa_hdl == NULL)) {
3820 error = SET_ERROR(EIO);
3825 * This is to prevent the creation of links into attribute space
3826 * by renaming a linked file into/outof an attribute directory.
3827 * See the comment in zfs_link() for why this is considered bad.
3829 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3830 error = SET_ERROR(EINVAL);
3835 * Must have write access at the source to remove the old entry
3836 * and write access at the target to create the new entry.
3837 * Note that if target and source are the same, this can be
3838 * done in a single check.
3840 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3843 if ((*svpp)->v_type == VDIR) {
3845 * Avoid ".", "..", and aliases of "." for obvious reasons.
3847 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3849 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3855 * Check to make sure rename is valid.
3856 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3858 if (error = zfs_rename_check(szp, sdzp, tdzp))
3863 * Does target exist?
3867 * Source and target must be the same type.
3869 if ((*svpp)->v_type == VDIR) {
3870 if ((*tvpp)->v_type != VDIR) {
3871 error = SET_ERROR(ENOTDIR);
3879 if ((*tvpp)->v_type == VDIR) {
3880 error = SET_ERROR(EISDIR);
3886 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3888 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3891 * notify the target directory if it is not the same
3892 * as source directory.
3895 vnevent_rename_dest_dir(tdvp, ct);
3898 tx = dmu_tx_create(zfsvfs->z_os);
3899 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3900 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3901 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3902 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3904 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3905 zfs_sa_upgrade_txholds(tx, tdzp);
3908 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3909 zfs_sa_upgrade_txholds(tx, tzp);
3912 zfs_sa_upgrade_txholds(tx, szp);
3913 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3914 error = dmu_tx_assign(tx, TXG_WAIT);
3921 if (tzp) /* Attempt to remove the existing target */
3922 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3925 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3927 szp->z_pflags |= ZFS_AV_MODIFIED;
3929 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3930 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3933 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3936 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3937 snm, tdzp, tnm, szp);
3940 * Update path information for the target vnode
3942 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
3945 * At this point, we have successfully created
3946 * the target name, but have failed to remove
3947 * the source name. Since the create was done
3948 * with the ZRENAMING flag, there are
3949 * complications; for one, the link count is
3950 * wrong. The easiest way to deal with this
3951 * is to remove the newly created target, and
3952 * return the original error. This must
3953 * succeed; fortunately, it is very unlikely to
3954 * fail, since we just created it.
3956 VERIFY3U(zfs_link_destroy(tdzp, tnm, szp, tx,
3957 ZRENAMING, NULL), ==, 0);
3964 cache_purge_negative(tdvp);
3970 unlockout: /* all 4 vnodes are locked, ZFS_ENTER called */
3972 VOP_UNLOCK(*svpp, 0);
3973 VOP_UNLOCK(sdvp, 0);
3975 out: /* original two vnodes are locked */
3976 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3977 zil_commit(zilog, 0);
3980 VOP_UNLOCK(*tvpp, 0);
3982 VOP_UNLOCK(tdvp, 0);
3987 * Insert the indicated symbolic reference entry into the directory.
3989 * IN: dvp - Directory to contain new symbolic link.
3990 * link - Name for new symlink entry.
3991 * vap - Attributes of new entry.
3992 * cr - credentials of caller.
3993 * ct - caller context
3994 * flags - case flags
3996 * RETURN: 0 on success, error code on failure.
3999 * dvp - ctime|mtime updated
4003 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4004 cred_t *cr, kthread_t *td)
4006 znode_t *zp, *dzp = VTOZ(dvp);
4008 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4010 uint64_t len = strlen(link);
4012 zfs_acl_ids_t acl_ids;
4013 boolean_t fuid_dirtied;
4014 uint64_t txtype = TX_SYMLINK;
4017 ASSERT(vap->va_type == VLNK);
4021 zilog = zfsvfs->z_log;
4023 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4024 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4026 return (SET_ERROR(EILSEQ));
4029 if (len > MAXPATHLEN) {
4031 return (SET_ERROR(ENAMETOOLONG));
4034 if ((error = zfs_acl_ids_create(dzp, 0,
4035 vap, cr, NULL, &acl_ids)) != 0) {
4041 * Attempt to lock directory; fail if entry already exists.
4043 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
4045 zfs_acl_ids_free(&acl_ids);
4050 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4051 zfs_acl_ids_free(&acl_ids);
4056 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4057 zfs_acl_ids_free(&acl_ids);
4059 return (SET_ERROR(EDQUOT));
4062 getnewvnode_reserve(1);
4063 tx = dmu_tx_create(zfsvfs->z_os);
4064 fuid_dirtied = zfsvfs->z_fuid_dirty;
4065 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4066 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4067 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4068 ZFS_SA_BASE_ATTR_SIZE + len);
4069 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4070 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4071 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4072 acl_ids.z_aclp->z_acl_bytes);
4075 zfs_fuid_txhold(zfsvfs, tx);
4076 error = dmu_tx_assign(tx, TXG_WAIT);
4078 zfs_acl_ids_free(&acl_ids);
4080 getnewvnode_drop_reserve();
4086 * Create a new object for the symlink.
4087 * for version 4 ZPL datsets the symlink will be an SA attribute
4089 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4092 zfs_fuid_sync(zfsvfs, tx);
4095 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4098 zfs_sa_symlink(zp, link, len, tx);
4101 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4102 &zp->z_size, sizeof (zp->z_size), tx);
4104 * Insert the new object into the directory.
4106 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
4108 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4111 zfs_acl_ids_free(&acl_ids);
4115 getnewvnode_drop_reserve();
4117 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4118 zil_commit(zilog, 0);
4125 * Return, in the buffer contained in the provided uio structure,
4126 * the symbolic path referred to by vp.
4128 * IN: vp - vnode of symbolic link.
4129 * uio - structure to contain the link path.
4130 * cr - credentials of caller.
4131 * ct - caller context
4133 * OUT: uio - structure containing the link path.
4135 * RETURN: 0 on success, error code on failure.
4138 * vp - atime updated
4142 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4144 znode_t *zp = VTOZ(vp);
4145 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4152 error = sa_lookup_uio(zp->z_sa_hdl,
4153 SA_ZPL_SYMLINK(zfsvfs), uio);
4155 error = zfs_sa_readlink(zp, uio);
4157 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4164 * Insert a new entry into directory tdvp referencing svp.
4166 * IN: tdvp - Directory to contain new entry.
4167 * svp - vnode of new entry.
4168 * name - name of new entry.
4169 * cr - credentials of caller.
4170 * ct - caller context
4172 * RETURN: 0 on success, error code on failure.
4175 * tdvp - ctime|mtime updated
4176 * svp - ctime updated
4180 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4181 caller_context_t *ct, int flags)
4183 znode_t *dzp = VTOZ(tdvp);
4185 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4192 ASSERT(tdvp->v_type == VDIR);
4196 zilog = zfsvfs->z_log;
4199 * POSIX dictates that we return EPERM here.
4200 * Better choices include ENOTSUP or EISDIR.
4202 if (svp->v_type == VDIR) {
4204 return (SET_ERROR(EPERM));
4210 if (szp->z_pflags & (ZFS_APPENDONLY | ZFS_IMMUTABLE | ZFS_READONLY)) {
4212 return (SET_ERROR(EPERM));
4215 /* Prevent links to .zfs/shares files */
4217 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4218 &parent, sizeof (uint64_t))) != 0) {
4222 if (parent == zfsvfs->z_shares_dir) {
4224 return (SET_ERROR(EPERM));
4227 if (zfsvfs->z_utf8 && u8_validate(name,
4228 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4230 return (SET_ERROR(EILSEQ));
4234 * We do not support links between attributes and non-attributes
4235 * because of the potential security risk of creating links
4236 * into "normal" file space in order to circumvent restrictions
4237 * imposed in attribute space.
4239 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4241 return (SET_ERROR(EINVAL));
4245 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4246 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4248 return (SET_ERROR(EPERM));
4251 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4257 * Attempt to lock directory; fail if entry already exists.
4259 error = zfs_dirent_lookup(dzp, name, &tzp, ZNEW);
4265 tx = dmu_tx_create(zfsvfs->z_os);
4266 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4267 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4268 zfs_sa_upgrade_txholds(tx, szp);
4269 zfs_sa_upgrade_txholds(tx, dzp);
4270 error = dmu_tx_assign(tx, TXG_WAIT);
4277 error = zfs_link_create(dzp, name, szp, tx, 0);
4280 uint64_t txtype = TX_LINK;
4281 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4287 vnevent_link(svp, ct);
4290 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4291 zil_commit(zilog, 0);
4300 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4302 znode_t *zp = VTOZ(vp);
4303 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4306 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4307 if (zp->z_sa_hdl == NULL) {
4309 * The fs has been unmounted, or we did a
4310 * suspend/resume and this file no longer exists.
4312 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4317 if (zp->z_unlinked) {
4319 * Fast path to recycle a vnode of a removed file.
4321 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4326 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4327 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4329 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4330 zfs_sa_upgrade_txholds(tx, zp);
4331 error = dmu_tx_assign(tx, TXG_WAIT);
4335 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4336 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4337 zp->z_atime_dirty = 0;
4341 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4345 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
4346 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
4350 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4352 znode_t *zp = VTOZ(vp);
4353 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4356 uint64_t object = zp->z_id;
4363 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4364 &gen64, sizeof (uint64_t))) != 0) {
4369 gen = (uint32_t)gen64;
4371 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4374 if (fidp->fid_len < size) {
4375 fidp->fid_len = size;
4377 return (SET_ERROR(ENOSPC));
4380 fidp->fid_len = size;
4383 zfid = (zfid_short_t *)fidp;
4385 zfid->zf_len = size;
4387 for (i = 0; i < sizeof (zfid->zf_object); i++)
4388 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4390 /* Must have a non-zero generation number to distinguish from .zfs */
4393 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4394 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4396 if (size == LONG_FID_LEN) {
4397 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4400 zlfid = (zfid_long_t *)fidp;
4402 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4403 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4405 /* XXX - this should be the generation number for the objset */
4406 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4407 zlfid->zf_setgen[i] = 0;
4415 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4416 caller_context_t *ct)
4427 case _PC_FILESIZEBITS:
4431 case _PC_XATTR_EXISTS:
4433 zfsvfs = zp->z_zfsvfs;
4437 error = zfs_dirent_lookup(zp, "", &xzp,
4438 ZXATTR | ZEXISTS | ZSHARED);
4440 if (!zfs_dirempty(xzp))
4443 } else if (error == ENOENT) {
4445 * If there aren't extended attributes, it's the
4446 * same as having zero of them.
4453 case _PC_SATTR_ENABLED:
4454 case _PC_SATTR_EXISTS:
4455 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4456 (vp->v_type == VREG || vp->v_type == VDIR);
4459 case _PC_ACCESS_FILTERING:
4460 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4464 case _PC_ACL_ENABLED:
4465 *valp = _ACL_ACE_ENABLED;
4467 #endif /* illumos */
4468 case _PC_MIN_HOLE_SIZE:
4469 *valp = (int)SPA_MINBLOCKSIZE;
4472 case _PC_TIMESTAMP_RESOLUTION:
4473 /* nanosecond timestamp resolution */
4477 case _PC_ACL_EXTENDED:
4485 case _PC_ACL_PATH_MAX:
4486 *valp = ACL_MAX_ENTRIES;
4490 return (EOPNOTSUPP);
4496 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4497 caller_context_t *ct)
4499 znode_t *zp = VTOZ(vp);
4500 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4502 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4506 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4514 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4515 caller_context_t *ct)
4517 znode_t *zp = VTOZ(vp);
4518 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4520 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4521 zilog_t *zilog = zfsvfs->z_log;
4526 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4528 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4529 zil_commit(zilog, 0);
4536 ioflags(int ioflags)
4540 if (ioflags & IO_APPEND)
4542 if (ioflags & IO_NDELAY)
4544 if (ioflags & IO_SYNC)
4545 flags |= (FSYNC | FDSYNC | FRSYNC);
4551 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int *rbehind,
4554 znode_t *zp = VTOZ(vp);
4555 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4556 objset_t *os = zp->z_zfsvfs->z_os;
4561 off_t startoff, endoff;
4563 vm_pindex_t reqstart, reqend;
4566 object = m[0]->object;
4572 zfs_vmobject_wlock(object);
4573 if (m[count - 1]->valid != 0 && --count == 0) {
4574 zfs_vmobject_wunlock(object);
4578 mlast = m[count - 1];
4580 if (IDX_TO_OFF(mlast->pindex) >=
4581 object->un_pager.vnp.vnp_size) {
4582 zfs_vmobject_wunlock(object);
4584 return (zfs_vm_pagerret_bad);
4587 VM_CNT_INC(v_vnodein);
4588 VM_CNT_ADD(v_vnodepgsin, count);
4591 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
4592 lsize = object->un_pager.vnp.vnp_size -
4593 IDX_TO_OFF(mlast->pindex);
4594 zfs_vmobject_wunlock(object);
4596 for (i = 0; i < count; i++) {
4600 va = zfs_map_page(m[i], &sf);
4601 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
4602 size, va, DMU_READ_PREFETCH);
4603 if (size != PAGE_SIZE)
4604 bzero(va + size, PAGE_SIZE - size);
4610 zfs_vmobject_wlock(object);
4611 for (i = 0; i < count; i++)
4612 m[i]->valid = VM_PAGE_BITS_ALL;
4613 zfs_vmobject_wunlock(object);
4616 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4623 return (zfs_vm_pagerret_ok);
4625 return (zfs_vm_pagerret_error);
4629 zfs_freebsd_getpages(ap)
4630 struct vop_getpages_args /* {
4639 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4644 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4647 znode_t *zp = VTOZ(vp);
4648 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4657 vm_ooffset_t lo_off;
4668 object = vp->v_object;
4672 KASSERT(ma[0]->object == object, ("mismatching object"));
4673 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4675 for (i = 0; i < pcount; i++)
4676 rtvals[i] = zfs_vm_pagerret_error;
4678 off = IDX_TO_OFF(ma[0]->pindex);
4679 blksz = zp->z_blksz;
4680 lo_off = rounddown(off, blksz);
4681 lo_len = roundup(len + (off - lo_off), blksz);
4682 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
4684 zfs_vmobject_wlock(object);
4685 if (len + off > object->un_pager.vnp.vnp_size) {
4686 if (object->un_pager.vnp.vnp_size > off) {
4689 len = object->un_pager.vnp.vnp_size - off;
4691 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4693 * If the object is locked and the following
4694 * conditions hold, then the page's dirty
4695 * field cannot be concurrently changed by a
4699 vm_page_assert_sbusied(m);
4700 KASSERT(!pmap_page_is_write_mapped(m),
4701 ("zfs_putpages: page %p is not read-only", m));
4702 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4709 if (ncount < pcount) {
4710 for (i = ncount; i < pcount; i++) {
4711 rtvals[i] = zfs_vm_pagerret_bad;
4715 zfs_vmobject_wunlock(object);
4720 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4721 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4725 tx = dmu_tx_create(zfsvfs->z_os);
4726 dmu_tx_hold_write(tx, zp->z_id, off, len);
4728 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4729 zfs_sa_upgrade_txholds(tx, zp);
4730 err = dmu_tx_assign(tx, TXG_WAIT);
4736 if (zp->z_blksz < PAGE_SIZE) {
4738 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4739 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4740 va = zfs_map_page(ma[i], &sf);
4741 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4745 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4749 uint64_t mtime[2], ctime[2];
4750 sa_bulk_attr_t bulk[3];
4753 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4755 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4757 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4759 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4761 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4763 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4765 zfs_vmobject_wlock(object);
4766 for (i = 0; i < ncount; i++) {
4767 rtvals[i] = zfs_vm_pagerret_ok;
4768 vm_page_undirty(ma[i]);
4770 zfs_vmobject_wunlock(object);
4771 VM_CNT_INC(v_vnodeout);
4772 VM_CNT_ADD(v_vnodepgsout, ncount);
4777 zfs_range_unlock(rl);
4778 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4779 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4780 zil_commit(zfsvfs->z_log, zp->z_id);
4786 zfs_freebsd_putpages(ap)
4787 struct vop_putpages_args /* {
4796 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4801 zfs_freebsd_bmap(ap)
4802 struct vop_bmap_args /* {
4805 struct bufobj **a_bop;
4812 if (ap->a_bop != NULL)
4813 *ap->a_bop = &ap->a_vp->v_bufobj;
4814 if (ap->a_bnp != NULL)
4815 *ap->a_bnp = ap->a_bn;
4816 if (ap->a_runp != NULL)
4818 if (ap->a_runb != NULL)
4825 zfs_freebsd_open(ap)
4826 struct vop_open_args /* {
4829 struct ucred *a_cred;
4830 struct thread *a_td;
4833 vnode_t *vp = ap->a_vp;
4834 znode_t *zp = VTOZ(vp);
4837 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
4839 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4844 zfs_freebsd_close(ap)
4845 struct vop_close_args /* {
4848 struct ucred *a_cred;
4849 struct thread *a_td;
4853 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
4857 zfs_freebsd_ioctl(ap)
4858 struct vop_ioctl_args /* {
4868 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4869 ap->a_fflag, ap->a_cred, NULL, NULL));
4873 zfs_freebsd_read(ap)
4874 struct vop_read_args /* {
4878 struct ucred *a_cred;
4882 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4887 zfs_freebsd_write(ap)
4888 struct vop_write_args /* {
4892 struct ucred *a_cred;
4896 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4901 zfs_freebsd_access(ap)
4902 struct vop_access_args /* {
4904 accmode_t a_accmode;
4905 struct ucred *a_cred;
4906 struct thread *a_td;
4909 vnode_t *vp = ap->a_vp;
4910 znode_t *zp = VTOZ(vp);
4915 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4917 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4919 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
4922 * VADMIN has to be handled by vaccess().
4925 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4927 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4928 zp->z_gid, accmode, ap->a_cred, NULL);
4933 * For VEXEC, ensure that at least one execute bit is set for
4936 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4937 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4945 zfs_freebsd_lookup(ap)
4946 struct vop_lookup_args /* {
4947 struct vnode *a_dvp;
4948 struct vnode **a_vpp;
4949 struct componentname *a_cnp;
4952 struct componentname *cnp = ap->a_cnp;
4953 char nm[NAME_MAX + 1];
4955 ASSERT(cnp->cn_namelen < sizeof(nm));
4956 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
4958 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4959 cnp->cn_cred, cnp->cn_thread, 0));
4963 zfs_cache_lookup(ap)
4964 struct vop_lookup_args /* {
4965 struct vnode *a_dvp;
4966 struct vnode **a_vpp;
4967 struct componentname *a_cnp;
4972 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4973 if (zfsvfs->z_use_namecache)
4974 return (vfs_cache_lookup(ap));
4976 return (zfs_freebsd_lookup(ap));
4980 zfs_freebsd_create(ap)
4981 struct vop_create_args /* {
4982 struct vnode *a_dvp;
4983 struct vnode **a_vpp;
4984 struct componentname *a_cnp;
4985 struct vattr *a_vap;
4989 struct componentname *cnp = ap->a_cnp;
4990 vattr_t *vap = ap->a_vap;
4993 ASSERT(cnp->cn_flags & SAVENAME);
4995 vattr_init_mask(vap);
4996 mode = vap->va_mode & ALLPERMS;
4997 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4999 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
5000 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
5001 if (zfsvfs->z_use_namecache &&
5002 error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
5003 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
5008 zfs_freebsd_remove(ap)
5009 struct vop_remove_args /* {
5010 struct vnode *a_dvp;
5012 struct componentname *a_cnp;
5016 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5018 return (zfs_remove(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
5019 ap->a_cnp->cn_cred));
5023 zfs_freebsd_mkdir(ap)
5024 struct vop_mkdir_args /* {
5025 struct vnode *a_dvp;
5026 struct vnode **a_vpp;
5027 struct componentname *a_cnp;
5028 struct vattr *a_vap;
5031 vattr_t *vap = ap->a_vap;
5033 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5035 vattr_init_mask(vap);
5037 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
5038 ap->a_cnp->cn_cred));
5042 zfs_freebsd_rmdir(ap)
5043 struct vop_rmdir_args /* {
5044 struct vnode *a_dvp;
5046 struct componentname *a_cnp;
5049 struct componentname *cnp = ap->a_cnp;
5051 ASSERT(cnp->cn_flags & SAVENAME);
5053 return (zfs_rmdir(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
5057 zfs_freebsd_readdir(ap)
5058 struct vop_readdir_args /* {
5061 struct ucred *a_cred;
5068 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
5069 ap->a_ncookies, ap->a_cookies));
5073 zfs_freebsd_fsync(ap)
5074 struct vop_fsync_args /* {
5077 struct thread *a_td;
5082 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
5086 zfs_freebsd_getattr(ap)
5087 struct vop_getattr_args /* {
5089 struct vattr *a_vap;
5090 struct ucred *a_cred;
5093 vattr_t *vap = ap->a_vap;
5099 xvap.xva_vattr = *vap;
5100 xvap.xva_vattr.va_mask |= AT_XVATTR;
5102 /* Convert chflags into ZFS-type flags. */
5103 /* XXX: what about SF_SETTABLE?. */
5104 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
5105 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
5106 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
5107 XVA_SET_REQ(&xvap, XAT_NODUMP);
5108 XVA_SET_REQ(&xvap, XAT_READONLY);
5109 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
5110 XVA_SET_REQ(&xvap, XAT_SYSTEM);
5111 XVA_SET_REQ(&xvap, XAT_HIDDEN);
5112 XVA_SET_REQ(&xvap, XAT_REPARSE);
5113 XVA_SET_REQ(&xvap, XAT_OFFLINE);
5114 XVA_SET_REQ(&xvap, XAT_SPARSE);
5116 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
5120 /* Convert ZFS xattr into chflags. */
5121 #define FLAG_CHECK(fflag, xflag, xfield) do { \
5122 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
5123 fflags |= (fflag); \
5125 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
5126 xvap.xva_xoptattrs.xoa_immutable);
5127 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
5128 xvap.xva_xoptattrs.xoa_appendonly);
5129 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
5130 xvap.xva_xoptattrs.xoa_nounlink);
5131 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
5132 xvap.xva_xoptattrs.xoa_archive);
5133 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
5134 xvap.xva_xoptattrs.xoa_nodump);
5135 FLAG_CHECK(UF_READONLY, XAT_READONLY,
5136 xvap.xva_xoptattrs.xoa_readonly);
5137 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
5138 xvap.xva_xoptattrs.xoa_system);
5139 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
5140 xvap.xva_xoptattrs.xoa_hidden);
5141 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
5142 xvap.xva_xoptattrs.xoa_reparse);
5143 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
5144 xvap.xva_xoptattrs.xoa_offline);
5145 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
5146 xvap.xva_xoptattrs.xoa_sparse);
5149 *vap = xvap.xva_vattr;
5150 vap->va_flags = fflags;
5155 zfs_freebsd_setattr(ap)
5156 struct vop_setattr_args /* {
5158 struct vattr *a_vap;
5159 struct ucred *a_cred;
5162 vnode_t *vp = ap->a_vp;
5163 vattr_t *vap = ap->a_vap;
5164 cred_t *cred = ap->a_cred;
5169 vattr_init_mask(vap);
5170 vap->va_mask &= ~AT_NOSET;
5173 xvap.xva_vattr = *vap;
5175 zflags = VTOZ(vp)->z_pflags;
5177 if (vap->va_flags != VNOVAL) {
5178 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
5181 if (zfsvfs->z_use_fuids == B_FALSE)
5182 return (EOPNOTSUPP);
5184 fflags = vap->va_flags;
5187 * We need to figure out whether it makes sense to allow
5188 * UF_REPARSE through, since we don't really have other
5189 * facilities to handle reparse points and zfs_setattr()
5190 * doesn't currently allow setting that attribute anyway.
5192 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
5193 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
5194 UF_OFFLINE|UF_SPARSE)) != 0)
5195 return (EOPNOTSUPP);
5197 * Unprivileged processes are not permitted to unset system
5198 * flags, or modify flags if any system flags are set.
5199 * Privileged non-jail processes may not modify system flags
5200 * if securelevel > 0 and any existing system flags are set.
5201 * Privileged jail processes behave like privileged non-jail
5202 * processes if the security.jail.chflags_allowed sysctl is
5203 * is non-zero; otherwise, they behave like unprivileged
5206 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
5207 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
5209 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5210 error = securelevel_gt(cred, 0);
5216 * Callers may only modify the file flags on objects they
5217 * have VADMIN rights for.
5219 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
5222 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5226 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
5231 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
5232 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
5233 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
5234 XVA_SET_REQ(&xvap, (xflag)); \
5235 (xfield) = ((fflags & (fflag)) != 0); \
5238 /* Convert chflags into ZFS-type flags. */
5239 /* XXX: what about SF_SETTABLE?. */
5240 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
5241 xvap.xva_xoptattrs.xoa_immutable);
5242 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
5243 xvap.xva_xoptattrs.xoa_appendonly);
5244 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
5245 xvap.xva_xoptattrs.xoa_nounlink);
5246 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
5247 xvap.xva_xoptattrs.xoa_archive);
5248 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
5249 xvap.xva_xoptattrs.xoa_nodump);
5250 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
5251 xvap.xva_xoptattrs.xoa_readonly);
5252 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
5253 xvap.xva_xoptattrs.xoa_system);
5254 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
5255 xvap.xva_xoptattrs.xoa_hidden);
5256 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
5257 xvap.xva_xoptattrs.xoa_hidden);
5258 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
5259 xvap.xva_xoptattrs.xoa_offline);
5260 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
5261 xvap.xva_xoptattrs.xoa_sparse);
5264 if (vap->va_birthtime.tv_sec != VNOVAL) {
5265 xvap.xva_vattr.va_mask |= AT_XVATTR;
5266 XVA_SET_REQ(&xvap, XAT_CREATETIME);
5268 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
5272 zfs_freebsd_rename(ap)
5273 struct vop_rename_args /* {
5274 struct vnode *a_fdvp;
5275 struct vnode *a_fvp;
5276 struct componentname *a_fcnp;
5277 struct vnode *a_tdvp;
5278 struct vnode *a_tvp;
5279 struct componentname *a_tcnp;
5282 vnode_t *fdvp = ap->a_fdvp;
5283 vnode_t *fvp = ap->a_fvp;
5284 vnode_t *tdvp = ap->a_tdvp;
5285 vnode_t *tvp = ap->a_tvp;
5288 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
5289 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
5291 error = zfs_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
5292 ap->a_tcnp, ap->a_fcnp->cn_cred);
5304 zfs_freebsd_symlink(ap)
5305 struct vop_symlink_args /* {
5306 struct vnode *a_dvp;
5307 struct vnode **a_vpp;
5308 struct componentname *a_cnp;
5309 struct vattr *a_vap;
5313 struct componentname *cnp = ap->a_cnp;
5314 vattr_t *vap = ap->a_vap;
5316 ASSERT(cnp->cn_flags & SAVENAME);
5318 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
5319 vattr_init_mask(vap);
5321 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
5322 ap->a_target, cnp->cn_cred, cnp->cn_thread));
5326 zfs_freebsd_readlink(ap)
5327 struct vop_readlink_args /* {
5330 struct ucred *a_cred;
5334 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
5338 zfs_freebsd_link(ap)
5339 struct vop_link_args /* {
5340 struct vnode *a_tdvp;
5342 struct componentname *a_cnp;
5345 struct componentname *cnp = ap->a_cnp;
5346 vnode_t *vp = ap->a_vp;
5347 vnode_t *tdvp = ap->a_tdvp;
5349 if (tdvp->v_mount != vp->v_mount)
5352 ASSERT(cnp->cn_flags & SAVENAME);
5354 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
5358 zfs_freebsd_inactive(ap)
5359 struct vop_inactive_args /* {
5361 struct thread *a_td;
5364 vnode_t *vp = ap->a_vp;
5366 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5371 zfs_freebsd_reclaim(ap)
5372 struct vop_reclaim_args /* {
5374 struct thread *a_td;
5377 vnode_t *vp = ap->a_vp;
5378 znode_t *zp = VTOZ(vp);
5379 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5383 /* Destroy the vm object and flush associated pages. */
5384 vnode_destroy_vobject(vp);
5387 * z_teardown_inactive_lock protects from a race with
5388 * zfs_znode_dmu_fini in zfsvfs_teardown during
5391 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
5392 if (zp->z_sa_hdl == NULL)
5396 rw_exit(&zfsvfs->z_teardown_inactive_lock);
5404 struct vop_fid_args /* {
5410 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5414 zfs_freebsd_pathconf(ap)
5415 struct vop_pathconf_args /* {
5418 register_t *a_retval;
5424 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
5426 *ap->a_retval = val;
5427 else if (error == EOPNOTSUPP)
5428 error = vop_stdpathconf(ap);
5433 zfs_freebsd_fifo_pathconf(ap)
5434 struct vop_pathconf_args /* {
5437 register_t *a_retval;
5441 switch (ap->a_name) {
5442 case _PC_ACL_EXTENDED:
5444 case _PC_ACL_PATH_MAX:
5445 case _PC_MAC_PRESENT:
5446 return (zfs_freebsd_pathconf(ap));
5448 return (fifo_specops.vop_pathconf(ap));
5453 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5454 * extended attribute name:
5457 * system freebsd:system:
5458 * user (none, can be used to access ZFS fsattr(5) attributes
5459 * created on Solaris)
5462 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5465 const char *namespace, *prefix, *suffix;
5467 /* We don't allow '/' character in attribute name. */
5468 if (strchr(name, '/') != NULL)
5470 /* We don't allow attribute names that start with "freebsd:" string. */
5471 if (strncmp(name, "freebsd:", 8) == 0)
5474 bzero(attrname, size);
5476 switch (attrnamespace) {
5477 case EXTATTR_NAMESPACE_USER:
5479 prefix = "freebsd:";
5480 namespace = EXTATTR_NAMESPACE_USER_STRING;
5484 * This is the default namespace by which we can access all
5485 * attributes created on Solaris.
5487 prefix = namespace = suffix = "";
5490 case EXTATTR_NAMESPACE_SYSTEM:
5491 prefix = "freebsd:";
5492 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5495 case EXTATTR_NAMESPACE_EMPTY:
5499 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5501 return (ENAMETOOLONG);
5507 * Vnode operating to retrieve a named extended attribute.
5510 zfs_getextattr(struct vop_getextattr_args *ap)
5513 IN struct vnode *a_vp;
5514 IN int a_attrnamespace;
5515 IN const char *a_name;
5516 INOUT struct uio *a_uio;
5518 IN struct ucred *a_cred;
5519 IN struct thread *a_td;
5523 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5524 struct thread *td = ap->a_td;
5525 struct nameidata nd;
5528 vnode_t *xvp = NULL, *vp;
5531 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5532 ap->a_cred, ap->a_td, VREAD);
5536 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5543 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5551 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5553 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
5555 NDFREE(&nd, NDF_ONLY_PNBUF);
5558 if (error == ENOENT)
5563 if (ap->a_size != NULL) {
5564 error = VOP_GETATTR(vp, &va, ap->a_cred);
5566 *ap->a_size = (size_t)va.va_size;
5567 } else if (ap->a_uio != NULL)
5568 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5571 vn_close(vp, flags, ap->a_cred, td);
5578 * Vnode operation to remove a named attribute.
5581 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5584 IN struct vnode *a_vp;
5585 IN int a_attrnamespace;
5586 IN const char *a_name;
5587 IN struct ucred *a_cred;
5588 IN struct thread *a_td;
5592 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5593 struct thread *td = ap->a_td;
5594 struct nameidata nd;
5597 vnode_t *xvp = NULL, *vp;
5600 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5601 ap->a_cred, ap->a_td, VWRITE);
5605 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5612 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5619 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5620 UIO_SYSSPACE, attrname, xvp, td);
5625 NDFREE(&nd, NDF_ONLY_PNBUF);
5626 if (error == ENOENT)
5631 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5632 NDFREE(&nd, NDF_ONLY_PNBUF);
5635 if (vp == nd.ni_dvp)
5645 * Vnode operation to set a named attribute.
5648 zfs_setextattr(struct vop_setextattr_args *ap)
5651 IN struct vnode *a_vp;
5652 IN int a_attrnamespace;
5653 IN const char *a_name;
5654 INOUT struct uio *a_uio;
5655 IN struct ucred *a_cred;
5656 IN struct thread *a_td;
5660 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5661 struct thread *td = ap->a_td;
5662 struct nameidata nd;
5665 vnode_t *xvp = NULL, *vp;
5668 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5669 ap->a_cred, ap->a_td, VWRITE);
5673 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5680 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5681 LOOKUP_XATTR | CREATE_XATTR_DIR);
5687 flags = FFLAGS(O_WRONLY | O_CREAT);
5688 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5690 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
5692 NDFREE(&nd, NDF_ONLY_PNBUF);
5700 error = VOP_SETATTR(vp, &va, ap->a_cred);
5702 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5705 vn_close(vp, flags, ap->a_cred, td);
5712 * Vnode operation to retrieve extended attributes on a vnode.
5715 zfs_listextattr(struct vop_listextattr_args *ap)
5718 IN struct vnode *a_vp;
5719 IN int a_attrnamespace;
5720 INOUT struct uio *a_uio;
5722 IN struct ucred *a_cred;
5723 IN struct thread *a_td;
5727 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5728 struct thread *td = ap->a_td;
5729 struct nameidata nd;
5730 char attrprefix[16];
5731 u_char dirbuf[sizeof(struct dirent)];
5734 struct uio auio, *uio = ap->a_uio;
5735 size_t *sizep = ap->a_size;
5737 vnode_t *xvp = NULL, *vp;
5738 int done, error, eof, pos;
5740 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5741 ap->a_cred, ap->a_td, VREAD);
5745 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5746 sizeof(attrprefix));
5749 plen = strlen(attrprefix);
5756 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5761 * ENOATTR means that the EA directory does not yet exist,
5762 * i.e. there are no extended attributes there.
5764 if (error == ENOATTR)
5769 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5770 UIO_SYSSPACE, ".", xvp, td);
5773 NDFREE(&nd, NDF_ONLY_PNBUF);
5779 auio.uio_iov = &aiov;
5780 auio.uio_iovcnt = 1;
5781 auio.uio_segflg = UIO_SYSSPACE;
5783 auio.uio_rw = UIO_READ;
5784 auio.uio_offset = 0;
5789 aiov.iov_base = (void *)dirbuf;
5790 aiov.iov_len = sizeof(dirbuf);
5791 auio.uio_resid = sizeof(dirbuf);
5792 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5793 done = sizeof(dirbuf) - auio.uio_resid;
5796 for (pos = 0; pos < done;) {
5797 dp = (struct dirent *)(dirbuf + pos);
5798 pos += dp->d_reclen;
5800 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5801 * is what we get when attribute was created on Solaris.
5803 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5805 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
5807 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5809 nlen = dp->d_namlen - plen;
5812 else if (uio != NULL) {
5814 * Format of extattr name entry is one byte for
5815 * length and the rest for name.
5817 error = uiomove(&nlen, 1, uio->uio_rw, uio);
5819 error = uiomove(dp->d_name + plen, nlen,
5826 } while (!eof && error == 0);
5835 zfs_freebsd_getacl(ap)
5836 struct vop_getacl_args /* {
5845 vsecattr_t vsecattr;
5847 if (ap->a_type != ACL_TYPE_NFS4)
5850 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
5851 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
5854 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
5855 if (vsecattr.vsa_aclentp != NULL)
5856 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
5862 zfs_freebsd_setacl(ap)
5863 struct vop_setacl_args /* {
5872 vsecattr_t vsecattr;
5873 int aclbsize; /* size of acl list in bytes */
5876 if (ap->a_type != ACL_TYPE_NFS4)
5879 if (ap->a_aclp == NULL)
5882 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
5886 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
5887 * splitting every entry into two and appending "canonical six"
5888 * entries at the end. Don't allow for setting an ACL that would
5889 * cause chmod(2) to run out of ACL entries.
5891 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
5894 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
5898 vsecattr.vsa_mask = VSA_ACE;
5899 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
5900 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
5901 aaclp = vsecattr.vsa_aclentp;
5902 vsecattr.vsa_aclentsz = aclbsize;
5904 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
5905 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
5906 kmem_free(aaclp, aclbsize);
5912 zfs_freebsd_aclcheck(ap)
5913 struct vop_aclcheck_args /* {
5922 return (EOPNOTSUPP);
5926 zfs_vptocnp(struct vop_vptocnp_args *ap)
5928 vnode_t *covered_vp;
5929 vnode_t *vp = ap->a_vp;;
5930 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
5931 znode_t *zp = VTOZ(vp);
5940 * If we are a snapshot mounted under .zfs, run the operation
5941 * on the covered vnode.
5943 if ((error = sa_lookup(zp->z_sa_hdl,
5944 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) {
5949 if (zp->z_id != parent || zfsvfs->z_parent == zfsvfs) {
5950 char name[MAXNAMLEN + 1];
5954 error = zfs_znode_parent_and_name(zp, &dzp, name);
5957 if (*ap->a_buflen < len)
5958 error = SET_ERROR(ENOMEM);
5961 *ap->a_buflen -= len;
5962 bcopy(name, ap->a_buf + *ap->a_buflen, len);
5963 *ap->a_vpp = ZTOV(dzp);
5970 covered_vp = vp->v_mount->mnt_vnodecovered;
5972 ltype = VOP_ISLOCKED(vp);
5974 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
5976 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
5977 ap->a_buf, ap->a_buflen);
5980 vn_lock(vp, ltype | LK_RETRY);
5981 if ((vp->v_iflag & VI_DOOMED) != 0)
5982 error = SET_ERROR(ENOENT);
5989 struct vop_lock1_args /* {
6000 err = vop_stdlock(ap);
6001 if (err == 0 && (ap->a_flags & LK_NOWAIT) == 0) {
6004 if (vp->v_mount != NULL && (vp->v_iflag & VI_DOOMED) == 0 &&
6005 zp != NULL && (zp->z_pflags & ZFS_XATTR) == 0)
6006 VERIFY(!RRM_LOCK_HELD(&zp->z_zfsvfs->z_teardown_lock));
6012 struct vop_vector zfs_vnodeops;
6013 struct vop_vector zfs_fifoops;
6014 struct vop_vector zfs_shareops;
6016 struct vop_vector zfs_vnodeops = {
6017 .vop_default = &default_vnodeops,
6018 .vop_inactive = zfs_freebsd_inactive,
6019 .vop_reclaim = zfs_freebsd_reclaim,
6020 .vop_access = zfs_freebsd_access,
6021 .vop_lookup = zfs_cache_lookup,
6022 .vop_cachedlookup = zfs_freebsd_lookup,
6023 .vop_getattr = zfs_freebsd_getattr,
6024 .vop_setattr = zfs_freebsd_setattr,
6025 .vop_create = zfs_freebsd_create,
6026 .vop_mknod = zfs_freebsd_create,
6027 .vop_mkdir = zfs_freebsd_mkdir,
6028 .vop_readdir = zfs_freebsd_readdir,
6029 .vop_fsync = zfs_freebsd_fsync,
6030 .vop_open = zfs_freebsd_open,
6031 .vop_close = zfs_freebsd_close,
6032 .vop_rmdir = zfs_freebsd_rmdir,
6033 .vop_ioctl = zfs_freebsd_ioctl,
6034 .vop_link = zfs_freebsd_link,
6035 .vop_symlink = zfs_freebsd_symlink,
6036 .vop_readlink = zfs_freebsd_readlink,
6037 .vop_read = zfs_freebsd_read,
6038 .vop_write = zfs_freebsd_write,
6039 .vop_remove = zfs_freebsd_remove,
6040 .vop_rename = zfs_freebsd_rename,
6041 .vop_pathconf = zfs_freebsd_pathconf,
6042 .vop_bmap = zfs_freebsd_bmap,
6043 .vop_fid = zfs_freebsd_fid,
6044 .vop_getextattr = zfs_getextattr,
6045 .vop_deleteextattr = zfs_deleteextattr,
6046 .vop_setextattr = zfs_setextattr,
6047 .vop_listextattr = zfs_listextattr,
6048 .vop_getacl = zfs_freebsd_getacl,
6049 .vop_setacl = zfs_freebsd_setacl,
6050 .vop_aclcheck = zfs_freebsd_aclcheck,
6051 .vop_getpages = zfs_freebsd_getpages,
6052 .vop_putpages = zfs_freebsd_putpages,
6053 .vop_vptocnp = zfs_vptocnp,
6055 .vop_lock1 = zfs_lock,
6059 struct vop_vector zfs_fifoops = {
6060 .vop_default = &fifo_specops,
6061 .vop_fsync = zfs_freebsd_fsync,
6062 .vop_access = zfs_freebsd_access,
6063 .vop_getattr = zfs_freebsd_getattr,
6064 .vop_inactive = zfs_freebsd_inactive,
6065 .vop_read = VOP_PANIC,
6066 .vop_reclaim = zfs_freebsd_reclaim,
6067 .vop_setattr = zfs_freebsd_setattr,
6068 .vop_write = VOP_PANIC,
6069 .vop_pathconf = zfs_freebsd_fifo_pathconf,
6070 .vop_fid = zfs_freebsd_fid,
6071 .vop_getacl = zfs_freebsd_getacl,
6072 .vop_setacl = zfs_freebsd_setacl,
6073 .vop_aclcheck = zfs_freebsd_aclcheck,
6077 * special share hidden files vnode operations template
6079 struct vop_vector zfs_shareops = {
6080 .vop_default = &default_vnodeops,
6081 .vop_access = zfs_freebsd_access,
6082 .vop_inactive = zfs_freebsd_inactive,
6083 .vop_reclaim = zfs_freebsd_reclaim,
6084 .vop_fid = zfs_freebsd_fid,
6085 .vop_pathconf = zfs_freebsd_pathconf,