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>
84 * Each vnode op performs some logical unit of work. To do this, the ZPL must
85 * properly lock its in-core state, create a DMU transaction, do the work,
86 * record this work in the intent log (ZIL), commit the DMU transaction,
87 * and wait for the intent log to commit if it is a synchronous operation.
88 * Moreover, the vnode ops must work in both normal and log replay context.
89 * The ordering of events is important to avoid deadlocks and references
90 * to freed memory. The example below illustrates the following Big Rules:
92 * (1) A check must be made in each zfs thread for a mounted file system.
93 * This is done avoiding races using ZFS_ENTER(zfsvfs).
94 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
95 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
96 * can return EIO from the calling function.
98 * (2) VN_RELE() should always be the last thing except for zil_commit()
99 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
100 * First, if it's the last reference, the vnode/znode
101 * can be freed, so the zp may point to freed memory. Second, the last
102 * reference will call zfs_zinactive(), which may induce a lot of work --
103 * pushing cached pages (which acquires range locks) and syncing out
104 * cached atime changes. Third, zfs_zinactive() may require a new tx,
105 * which could deadlock the system if you were already holding one.
106 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
108 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
109 * as they can span dmu_tx_assign() calls.
111 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
112 * dmu_tx_assign(). This is critical because we don't want to block
113 * while holding locks.
115 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
116 * reduces lock contention and CPU usage when we must wait (note that if
117 * throughput is constrained by the storage, nearly every transaction
120 * Note, in particular, that if a lock is sometimes acquired before
121 * the tx assigns, and sometimes after (e.g. z_lock), then failing
122 * to use a non-blocking assign can deadlock the system. The scenario:
124 * Thread A has grabbed a lock before calling dmu_tx_assign().
125 * Thread B is in an already-assigned tx, and blocks for this lock.
126 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
127 * forever, because the previous txg can't quiesce until B's tx commits.
129 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
130 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
131 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
132 * to indicate that this operation has already called dmu_tx_wait().
133 * This will ensure that we don't retry forever, waiting a short bit
136 * (5) If the operation succeeded, generate the intent log entry for it
137 * before dropping locks. This ensures that the ordering of events
138 * in the intent log matches the order in which they actually occurred.
139 * During ZIL replay the zfs_log_* functions will update the sequence
140 * number to indicate the zil transaction has replayed.
142 * (6) At the end of each vnode op, the DMU tx must always commit,
143 * regardless of whether there were any errors.
145 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
146 * to ensure that synchronous semantics are provided when necessary.
148 * In general, this is how things should be ordered in each vnode op:
150 * ZFS_ENTER(zfsvfs); // exit if unmounted
152 * zfs_dirent_lookup(&dl, ...) // lock directory entry (may VN_HOLD())
153 * rw_enter(...); // grab any other locks you need
154 * tx = dmu_tx_create(...); // get DMU tx
155 * dmu_tx_hold_*(); // hold each object you might modify
156 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
158 * rw_exit(...); // drop locks
159 * zfs_dirent_unlock(dl); // unlock directory entry
160 * VN_RELE(...); // release held vnodes
161 * if (error == ERESTART) {
167 * dmu_tx_abort(tx); // abort DMU tx
168 * ZFS_EXIT(zfsvfs); // finished in zfs
169 * return (error); // really out of space
171 * error = do_real_work(); // do whatever this VOP does
173 * zfs_log_*(...); // on success, make ZIL entry
174 * dmu_tx_commit(tx); // commit DMU tx -- error or not
175 * rw_exit(...); // drop locks
176 * zfs_dirent_unlock(dl); // unlock directory entry
177 * VN_RELE(...); // release held vnodes
178 * zil_commit(zilog, foid); // synchronous when necessary
179 * ZFS_EXIT(zfsvfs); // finished in zfs
180 * return (error); // done, report error
185 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
187 znode_t *zp = VTOZ(*vpp);
188 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
193 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
194 ((flag & FAPPEND) == 0)) {
196 return (SET_ERROR(EPERM));
199 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
200 ZTOV(zp)->v_type == VREG &&
201 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
202 if (fs_vscan(*vpp, cr, 0) != 0) {
204 return (SET_ERROR(EACCES));
208 /* Keep a count of the synchronous opens in the znode */
209 if (flag & (FSYNC | FDSYNC))
210 atomic_inc_32(&zp->z_sync_cnt);
218 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
219 caller_context_t *ct)
221 znode_t *zp = VTOZ(vp);
222 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
225 * Clean up any locks held by this process on the vp.
227 cleanlocks(vp, ddi_get_pid(), 0);
228 cleanshares(vp, ddi_get_pid());
233 /* Decrement the synchronous opens in the znode */
234 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
235 atomic_dec_32(&zp->z_sync_cnt);
237 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
238 ZTOV(zp)->v_type == VREG &&
239 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
240 VERIFY(fs_vscan(vp, cr, 1) == 0);
247 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
248 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
251 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
253 znode_t *zp = VTOZ(vp);
254 uint64_t noff = (uint64_t)*off; /* new offset */
259 file_sz = zp->z_size;
260 if (noff >= file_sz) {
261 return (SET_ERROR(ENXIO));
264 if (cmd == _FIO_SEEK_HOLE)
269 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
272 return (SET_ERROR(ENXIO));
275 * We could find a hole that begins after the logical end-of-file,
276 * because dmu_offset_next() only works on whole blocks. If the
277 * EOF falls mid-block, then indicate that the "virtual hole"
278 * at the end of the file begins at the logical EOF, rather than
279 * at the end of the last block.
281 if (noff > file_sz) {
294 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
295 int *rvalp, caller_context_t *ct)
299 dmu_object_info_t doi;
310 * The following two ioctls are used by bfu. Faking out,
311 * necessary to avoid bfu errors.
324 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
325 return (SET_ERROR(EFAULT));
327 off = *(offset_t *)data;
330 zfsvfs = zp->z_zfsvfs;
334 /* offset parameter is in/out */
335 error = zfs_holey(vp, com, &off);
340 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
341 return (SET_ERROR(EFAULT));
343 *(offset_t *)data = off;
348 case _FIO_COUNT_FILLED:
351 * _FIO_COUNT_FILLED adds a new ioctl command which
352 * exposes the number of filled blocks in a
356 zfsvfs = zp->z_zfsvfs;
361 * Wait for all dirty blocks for this object
362 * to get synced out to disk, and the DMU info
365 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
372 * Retrieve fill count from DMU object.
374 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
380 ndata = doi.doi_fill_count;
383 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
384 return (SET_ERROR(EFAULT));
389 return (SET_ERROR(ENOTTY));
393 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
400 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
401 * aligned boundaries, if the range is not aligned. As a result a
402 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
403 * It may happen that all DEV_BSIZE subranges are marked clean and thus
404 * the whole page would be considred clean despite have some dirty data.
405 * For this reason we should shrink the range to DEV_BSIZE aligned
406 * boundaries before calling vm_page_clear_dirty.
408 end = rounddown2(off + nbytes, DEV_BSIZE);
409 off = roundup2(off, DEV_BSIZE);
413 zfs_vmobject_assert_wlocked(obj);
416 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
418 if (vm_page_xbusied(pp)) {
420 * Reference the page before unlocking and
421 * sleeping so that the page daemon is less
422 * likely to reclaim it.
424 vm_page_reference(pp);
426 zfs_vmobject_wunlock(obj);
427 vm_page_busy_sleep(pp, "zfsmwb", true);
428 zfs_vmobject_wlock(obj);
432 } else if (pp != NULL) {
438 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
439 vm_object_pip_add(obj, 1);
440 pmap_remove_write(pp);
442 vm_page_clear_dirty(pp, off, nbytes);
450 page_unbusy(vm_page_t pp)
454 vm_object_pip_subtract(pp->object, 1);
458 page_hold(vnode_t *vp, int64_t start)
464 zfs_vmobject_assert_wlocked(obj);
467 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
469 if (vm_page_xbusied(pp)) {
471 * Reference the page before unlocking and
472 * sleeping so that the page daemon is less
473 * likely to reclaim it.
475 vm_page_reference(pp);
477 zfs_vmobject_wunlock(obj);
478 vm_page_busy_sleep(pp, "zfsmwb", true);
479 zfs_vmobject_wlock(obj);
483 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
496 page_unhold(vm_page_t pp)
505 * When a file is memory mapped, we must keep the IO data synchronized
506 * between the DMU cache and the memory mapped pages. What this means:
508 * On Write: If we find a memory mapped page, we write to *both*
509 * the page and the dmu buffer.
512 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
513 int segflg, dmu_tx_t *tx)
520 ASSERT(segflg != UIO_NOCOPY);
521 ASSERT(vp->v_mount != NULL);
525 off = start & PAGEOFFSET;
526 zfs_vmobject_wlock(obj);
527 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
529 int nbytes = imin(PAGESIZE - off, len);
531 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
532 zfs_vmobject_wunlock(obj);
534 va = zfs_map_page(pp, &sf);
535 (void) dmu_read(os, oid, start+off, nbytes,
536 va+off, DMU_READ_PREFETCH);;
539 zfs_vmobject_wlock(obj);
545 vm_object_pip_wakeupn(obj, 0);
546 zfs_vmobject_wunlock(obj);
550 * Read with UIO_NOCOPY flag means that sendfile(2) requests
551 * ZFS to populate a range of page cache pages with data.
553 * NOTE: this function could be optimized to pre-allocate
554 * all pages in advance, drain exclusive busy on all of them,
555 * map them into contiguous KVA region and populate them
556 * in one single dmu_read() call.
559 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
561 znode_t *zp = VTOZ(vp);
562 objset_t *os = zp->z_zfsvfs->z_os;
572 ASSERT(uio->uio_segflg == UIO_NOCOPY);
573 ASSERT(vp->v_mount != NULL);
576 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
578 zfs_vmobject_wlock(obj);
579 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
580 int bytes = MIN(PAGESIZE, len);
582 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_SBUSY |
583 VM_ALLOC_NORMAL | VM_ALLOC_IGN_SBUSY);
584 if (pp->valid == 0) {
585 zfs_vmobject_wunlock(obj);
586 va = zfs_map_page(pp, &sf);
587 error = dmu_read(os, zp->z_id, start, bytes, va,
589 if (bytes != PAGESIZE && error == 0)
590 bzero(va + bytes, PAGESIZE - bytes);
592 zfs_vmobject_wlock(obj);
596 if (pp->wire_count == 0 && pp->valid == 0 &&
600 pp->valid = VM_PAGE_BITS_ALL;
601 vm_page_activate(pp);
605 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
610 uio->uio_resid -= bytes;
611 uio->uio_offset += bytes;
614 zfs_vmobject_wunlock(obj);
619 * When a file is memory mapped, we must keep the IO data synchronized
620 * between the DMU cache and the memory mapped pages. What this means:
622 * On Read: We "read" preferentially from memory mapped pages,
623 * else we default from the dmu buffer.
625 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
626 * the file is memory mapped.
629 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
631 znode_t *zp = VTOZ(vp);
639 ASSERT(vp->v_mount != NULL);
643 start = uio->uio_loffset;
644 off = start & PAGEOFFSET;
645 zfs_vmobject_wlock(obj);
646 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
648 uint64_t bytes = MIN(PAGESIZE - off, len);
650 if (pp = page_hold(vp, start)) {
654 zfs_vmobject_wunlock(obj);
655 va = zfs_map_page(pp, &sf);
657 error = uiomove(va + off, bytes, UIO_READ, uio);
659 error = vn_io_fault_uiomove(va + off, bytes, uio);
662 zfs_vmobject_wlock(obj);
665 zfs_vmobject_wunlock(obj);
666 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
668 zfs_vmobject_wlock(obj);
675 zfs_vmobject_wunlock(obj);
679 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
682 * Read bytes from specified file into supplied buffer.
684 * IN: vp - vnode of file to be read from.
685 * uio - structure supplying read location, range info,
687 * ioflag - SYNC flags; used to provide FRSYNC semantics.
688 * cr - credentials of caller.
689 * ct - caller context
691 * OUT: uio - updated offset and range, buffer filled.
693 * RETURN: 0 on success, error code on failure.
696 * vp - atime updated if byte count > 0
700 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
702 znode_t *zp = VTOZ(vp);
703 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
712 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
714 return (SET_ERROR(EACCES));
718 * Validate file offset
720 if (uio->uio_loffset < (offset_t)0) {
722 return (SET_ERROR(EINVAL));
726 * Fasttrack empty reads
728 if (uio->uio_resid == 0) {
734 * Check for mandatory locks
736 if (MANDMODE(zp->z_mode)) {
737 if (error = chklock(vp, FREAD,
738 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
745 * If we're in FRSYNC mode, sync out this znode before reading it.
748 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
749 zil_commit(zfsvfs->z_log, zp->z_id);
752 * Lock the range against changes.
754 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
757 * If we are reading past end-of-file we can skip
758 * to the end; but we might still need to set atime.
760 if (uio->uio_loffset >= zp->z_size) {
765 ASSERT(uio->uio_loffset < zp->z_size);
766 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
769 if ((uio->uio_extflg == UIO_XUIO) &&
770 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
772 int blksz = zp->z_blksz;
773 uint64_t offset = uio->uio_loffset;
775 xuio = (xuio_t *)uio;
777 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
780 ASSERT(offset + n <= blksz);
783 (void) dmu_xuio_init(xuio, nblk);
785 if (vn_has_cached_data(vp)) {
787 * For simplicity, we always allocate a full buffer
788 * even if we only expect to read a portion of a block.
790 while (--nblk >= 0) {
791 (void) dmu_xuio_add(xuio,
792 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
800 nbytes = MIN(n, zfs_read_chunk_size -
801 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
804 if (uio->uio_segflg == UIO_NOCOPY)
805 error = mappedread_sf(vp, nbytes, uio);
807 #endif /* __FreeBSD__ */
808 if (vn_has_cached_data(vp)) {
809 error = mappedread(vp, nbytes, uio);
811 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
815 /* convert checksum errors into IO errors */
817 error = SET_ERROR(EIO);
824 zfs_range_unlock(rl);
826 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
832 * Write the bytes to a file.
834 * IN: vp - vnode of file to be written to.
835 * uio - structure supplying write location, range info,
837 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
838 * set if in append mode.
839 * cr - credentials of caller.
840 * ct - caller context (NFS/CIFS fem monitor only)
842 * OUT: uio - updated offset and range.
844 * RETURN: 0 on success, error code on failure.
847 * vp - ctime|mtime updated if byte count > 0
852 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
854 znode_t *zp = VTOZ(vp);
855 rlim64_t limit = MAXOFFSET_T;
856 ssize_t start_resid = uio->uio_resid;
860 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
865 int max_blksz = zfsvfs->z_max_blksz;
868 iovec_t *aiov = NULL;
871 int iovcnt = uio->uio_iovcnt;
872 iovec_t *iovp = uio->uio_iov;
875 sa_bulk_attr_t bulk[4];
876 uint64_t mtime[2], ctime[2];
879 * Fasttrack empty write
885 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
891 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
892 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
893 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
895 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
899 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
900 * callers might not be able to detect properly that we are read-only,
901 * so check it explicitly here.
903 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
905 return (SET_ERROR(EROFS));
909 * If immutable or not appending then return EPERM.
910 * Intentionally allow ZFS_READONLY through here.
911 * See zfs_zaccess_common()
913 if ((zp->z_pflags & ZFS_IMMUTABLE) ||
914 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
915 (uio->uio_loffset < zp->z_size))) {
917 return (SET_ERROR(EPERM));
920 zilog = zfsvfs->z_log;
923 * Validate file offset
925 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
928 return (SET_ERROR(EINVAL));
932 * Check for mandatory locks before calling zfs_range_lock()
933 * in order to prevent a deadlock with locks set via fcntl().
935 if (MANDMODE((mode_t)zp->z_mode) &&
936 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
943 * Pre-fault the pages to ensure slow (eg NFS) pages
945 * Skip this if uio contains loaned arc_buf.
947 if ((uio->uio_extflg == UIO_XUIO) &&
948 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
949 xuio = (xuio_t *)uio;
951 uio_prefaultpages(MIN(n, max_blksz), uio);
955 * If in append mode, set the io offset pointer to eof.
957 if (ioflag & FAPPEND) {
959 * Obtain an appending range lock to guarantee file append
960 * semantics. We reset the write offset once we have the lock.
962 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
964 if (rl->r_len == UINT64_MAX) {
966 * We overlocked the file because this write will cause
967 * the file block size to increase.
968 * Note that zp_size cannot change with this lock held.
972 uio->uio_loffset = woff;
975 * Note that if the file block size will change as a result of
976 * this write, then this range lock will lock the entire file
977 * so that we can re-write the block safely.
979 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
982 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
983 zfs_range_unlock(rl);
989 zfs_range_unlock(rl);
991 return (SET_ERROR(EFBIG));
994 if ((woff + n) > limit || woff > (limit - n))
997 /* Will this write extend the file length? */
998 write_eof = (woff + n > zp->z_size);
1000 end_size = MAX(zp->z_size, woff + n);
1003 * Write the file in reasonable size chunks. Each chunk is written
1004 * in a separate transaction; this keeps the intent log records small
1005 * and allows us to do more fine-grained space accounting.
1009 woff = uio->uio_loffset;
1010 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
1011 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
1013 dmu_return_arcbuf(abuf);
1014 error = SET_ERROR(EDQUOT);
1018 if (xuio && abuf == NULL) {
1019 ASSERT(i_iov < iovcnt);
1020 aiov = &iovp[i_iov];
1021 abuf = dmu_xuio_arcbuf(xuio, i_iov);
1022 dmu_xuio_clear(xuio, i_iov);
1023 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
1024 iovec_t *, aiov, arc_buf_t *, abuf);
1025 ASSERT((aiov->iov_base == abuf->b_data) ||
1026 ((char *)aiov->iov_base - (char *)abuf->b_data +
1027 aiov->iov_len == arc_buf_size(abuf)));
1029 } else if (abuf == NULL && n >= max_blksz &&
1030 woff >= zp->z_size &&
1031 P2PHASE(woff, max_blksz) == 0 &&
1032 zp->z_blksz == max_blksz) {
1034 * This write covers a full block. "Borrow" a buffer
1035 * from the dmu so that we can fill it before we enter
1036 * a transaction. This avoids the possibility of
1037 * holding up the transaction if the data copy hangs
1038 * up on a pagefault (e.g., from an NFS server mapping).
1042 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
1044 ASSERT(abuf != NULL);
1045 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);
1055 * Start a transaction.
1057 tx = dmu_tx_create(zfsvfs->z_os);
1058 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1059 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
1060 zfs_sa_upgrade_txholds(tx, zp);
1061 error = dmu_tx_assign(tx, TXG_WAIT);
1065 dmu_return_arcbuf(abuf);
1070 * If zfs_range_lock() over-locked we grow the blocksize
1071 * and then reduce the lock range. This will only happen
1072 * on the first iteration since zfs_range_reduce() will
1073 * shrink down r_len to the appropriate size.
1075 if (rl->r_len == UINT64_MAX) {
1078 if (zp->z_blksz > max_blksz) {
1080 * File's blocksize is already larger than the
1081 * "recordsize" property. Only let it grow to
1082 * the next power of 2.
1084 ASSERT(!ISP2(zp->z_blksz));
1085 new_blksz = MIN(end_size,
1086 1 << highbit64(zp->z_blksz));
1088 new_blksz = MIN(end_size, max_blksz);
1090 zfs_grow_blocksize(zp, new_blksz, tx);
1091 zfs_range_reduce(rl, woff, n);
1095 * XXX - should we really limit each write to z_max_blksz?
1096 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1098 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1100 if (woff + nbytes > zp->z_size)
1101 vnode_pager_setsize(vp, woff + nbytes);
1104 tx_bytes = uio->uio_resid;
1105 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1107 tx_bytes -= uio->uio_resid;
1110 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1112 * If this is not a full block write, but we are
1113 * extending the file past EOF and this data starts
1114 * block-aligned, use assign_arcbuf(). Otherwise,
1115 * write via dmu_write().
1117 if (tx_bytes < max_blksz && (!write_eof ||
1118 aiov->iov_base != abuf->b_data)) {
1120 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1121 aiov->iov_len, aiov->iov_base, tx);
1122 dmu_return_arcbuf(abuf);
1123 xuio_stat_wbuf_copied();
1125 ASSERT(xuio || tx_bytes == max_blksz);
1126 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1129 ASSERT(tx_bytes <= uio->uio_resid);
1130 uioskip(uio, tx_bytes);
1132 if (tx_bytes && vn_has_cached_data(vp)) {
1133 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1134 zp->z_id, uio->uio_segflg, tx);
1138 * If we made no progress, we're done. If we made even
1139 * partial progress, update the znode and ZIL accordingly.
1141 if (tx_bytes == 0) {
1142 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1143 (void *)&zp->z_size, sizeof (uint64_t), tx);
1150 * Clear Set-UID/Set-GID bits on successful write if not
1151 * privileged and at least one of the excute bits is set.
1153 * It would be nice to to this after all writes have
1154 * been done, but that would still expose the ISUID/ISGID
1155 * to another app after the partial write is committed.
1157 * Note: we don't call zfs_fuid_map_id() here because
1158 * user 0 is not an ephemeral uid.
1160 mutex_enter(&zp->z_acl_lock);
1161 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1162 (S_IXUSR >> 6))) != 0 &&
1163 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1164 secpolicy_vnode_setid_retain(vp, cr,
1165 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1167 zp->z_mode &= ~(S_ISUID | S_ISGID);
1168 newmode = zp->z_mode;
1169 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1170 (void *)&newmode, sizeof (uint64_t), tx);
1172 mutex_exit(&zp->z_acl_lock);
1174 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1178 * Update the file size (zp_size) if it has changed;
1179 * account for possible concurrent updates.
1181 while ((end_size = zp->z_size) < uio->uio_loffset) {
1182 (void) atomic_cas_64(&zp->z_size, end_size,
1187 ASSERT(error == 0 || error == EFAULT);
1191 * If we are replaying and eof is non zero then force
1192 * the file size to the specified eof. Note, there's no
1193 * concurrency during replay.
1195 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1196 zp->z_size = zfsvfs->z_replay_eof;
1199 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1201 (void) sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1203 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1208 ASSERT(tx_bytes == nbytes);
1213 uio_prefaultpages(MIN(n, max_blksz), uio);
1217 zfs_range_unlock(rl);
1220 * If we're in replay mode, or we made no progress, return error.
1221 * Otherwise, it's at least a partial write, so it's successful.
1223 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1230 * EFAULT means that at least one page of the source buffer was not
1231 * available. VFS will re-try remaining I/O upon this error.
1233 if (error == EFAULT) {
1239 if (ioflag & (FSYNC | FDSYNC) ||
1240 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1241 zil_commit(zilog, zp->z_id);
1248 zfs_get_done(zgd_t *zgd, int error)
1250 znode_t *zp = zgd->zgd_private;
1251 objset_t *os = zp->z_zfsvfs->z_os;
1254 dmu_buf_rele(zgd->zgd_db, zgd);
1256 zfs_range_unlock(zgd->zgd_rl);
1259 * Release the vnode asynchronously as we currently have the
1260 * txg stopped from syncing.
1262 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1264 if (error == 0 && zgd->zgd_bp)
1265 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1267 kmem_free(zgd, sizeof (zgd_t));
1271 static int zil_fault_io = 0;
1275 * Get data to generate a TX_WRITE intent log record.
1278 zfs_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1280 zfsvfs_t *zfsvfs = arg;
1281 objset_t *os = zfsvfs->z_os;
1283 uint64_t object = lr->lr_foid;
1284 uint64_t offset = lr->lr_offset;
1285 uint64_t size = lr->lr_length;
1290 ASSERT3P(lwb, !=, NULL);
1291 ASSERT3P(zio, !=, NULL);
1292 ASSERT3U(size, !=, 0);
1295 * Nothing to do if the file has been removed
1297 if (zfs_zget(zfsvfs, object, &zp) != 0)
1298 return (SET_ERROR(ENOENT));
1299 if (zp->z_unlinked) {
1301 * Release the vnode asynchronously as we currently have the
1302 * txg stopped from syncing.
1304 VN_RELE_ASYNC(ZTOV(zp),
1305 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1306 return (SET_ERROR(ENOENT));
1309 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1311 zgd->zgd_private = zp;
1314 * Write records come in two flavors: immediate and indirect.
1315 * For small writes it's cheaper to store the data with the
1316 * log record (immediate); for large writes it's cheaper to
1317 * sync the data and get a pointer to it (indirect) so that
1318 * we don't have to write the data twice.
1320 if (buf != NULL) { /* immediate write */
1321 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1322 /* test for truncation needs to be done while range locked */
1323 if (offset >= zp->z_size) {
1324 error = SET_ERROR(ENOENT);
1326 error = dmu_read(os, object, offset, size, buf,
1327 DMU_READ_NO_PREFETCH);
1329 ASSERT(error == 0 || error == ENOENT);
1330 } else { /* indirect write */
1332 * Have to lock the whole block to ensure when it's
1333 * written out and its checksum is being calculated
1334 * that no one can change the data. We need to re-check
1335 * blocksize after we get the lock in case it's changed!
1340 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1342 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1344 if (zp->z_blksz == size)
1347 zfs_range_unlock(zgd->zgd_rl);
1349 /* test for truncation needs to be done while range locked */
1350 if (lr->lr_offset >= zp->z_size)
1351 error = SET_ERROR(ENOENT);
1354 error = SET_ERROR(EIO);
1359 error = dmu_buf_hold(os, object, offset, zgd, &db,
1360 DMU_READ_NO_PREFETCH);
1363 blkptr_t *bp = &lr->lr_blkptr;
1368 ASSERT(db->db_offset == offset);
1369 ASSERT(db->db_size == size);
1371 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1373 ASSERT(error || lr->lr_length <= size);
1376 * On success, we need to wait for the write I/O
1377 * initiated by dmu_sync() to complete before we can
1378 * release this dbuf. We will finish everything up
1379 * in the zfs_get_done() callback.
1384 if (error == EALREADY) {
1385 lr->lr_common.lrc_txtype = TX_WRITE2;
1387 * TX_WRITE2 relies on the data previously
1388 * written by the TX_WRITE that caused
1389 * EALREADY. We zero out the BP because
1390 * it is the old, currently-on-disk BP,
1391 * so there's no need to zio_flush() its
1392 * vdevs (flushing would needlesly hurt
1393 * performance, and doesn't work on
1403 zfs_get_done(zgd, error);
1410 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1411 caller_context_t *ct)
1413 znode_t *zp = VTOZ(vp);
1414 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1420 if (flag & V_ACE_MASK)
1421 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1423 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1430 zfs_dd_callback(struct mount *mp, void *arg, int lkflags, struct vnode **vpp)
1435 error = vn_lock(*vpp, lkflags);
1442 zfs_lookup_lock(vnode_t *dvp, vnode_t *vp, const char *name, int lkflags)
1444 znode_t *zdp = VTOZ(dvp);
1445 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1449 ASSERT_VOP_LOCKED(dvp, __func__);
1451 if ((zdp->z_pflags & ZFS_XATTR) == 0)
1452 VERIFY(!RRM_LOCK_HELD(&zfsvfs->z_teardown_lock));
1455 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
1456 ASSERT3P(dvp, ==, vp);
1458 ltype = lkflags & LK_TYPE_MASK;
1459 if (ltype != VOP_ISLOCKED(dvp)) {
1460 if (ltype == LK_EXCLUSIVE)
1461 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1462 else /* if (ltype == LK_SHARED) */
1463 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1466 * Relock for the "." case could leave us with
1469 if (dvp->v_iflag & VI_DOOMED) {
1471 return (SET_ERROR(ENOENT));
1475 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
1477 * Note that in this case, dvp is the child vnode, and we
1478 * are looking up the parent vnode - exactly reverse from
1479 * normal operation. Unlocking dvp requires some rather
1480 * tricky unlock/relock dance to prevent mp from being freed;
1481 * use vn_vget_ino_gen() which takes care of all that.
1483 * XXX Note that there is a time window when both vnodes are
1484 * unlocked. It is possible, although highly unlikely, that
1485 * during that window the parent-child relationship between
1486 * the vnodes may change, for example, get reversed.
1487 * In that case we would have a wrong lock order for the vnodes.
1488 * All other filesystems seem to ignore this problem, so we
1490 * A potential solution could be implemented as follows:
1491 * - using LK_NOWAIT when locking the second vnode and retrying
1493 * - checking that the parent-child relationship still holds
1494 * after locking both vnodes and retrying if it doesn't
1496 error = vn_vget_ino_gen(dvp, zfs_dd_callback, vp, lkflags, &vp);
1499 error = vn_lock(vp, lkflags);
1507 * Lookup an entry in a directory, or an extended attribute directory.
1508 * If it exists, return a held vnode reference for it.
1510 * IN: dvp - vnode of directory to search.
1511 * nm - name of entry to lookup.
1512 * pnp - full pathname to lookup [UNUSED].
1513 * flags - LOOKUP_XATTR set if looking for an attribute.
1514 * rdir - root directory vnode [UNUSED].
1515 * cr - credentials of caller.
1516 * ct - caller context
1518 * OUT: vpp - vnode of located entry, NULL if not found.
1520 * RETURN: 0 on success, error code on failure.
1527 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1528 int nameiop, cred_t *cr, kthread_t *td, int flags)
1530 znode_t *zdp = VTOZ(dvp);
1532 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1536 * Fast path lookup, however we must skip DNLC lookup
1537 * for case folding or normalizing lookups because the
1538 * DNLC code only stores the passed in name. This means
1539 * creating 'a' and removing 'A' on a case insensitive
1540 * file system would work, but DNLC still thinks 'a'
1541 * exists and won't let you create it again on the next
1542 * pass through fast path.
1544 if (!(flags & LOOKUP_XATTR)) {
1545 if (dvp->v_type != VDIR) {
1546 return (SET_ERROR(ENOTDIR));
1547 } else if (zdp->z_sa_hdl == NULL) {
1548 return (SET_ERROR(EIO));
1552 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1559 if (flags & LOOKUP_XATTR) {
1562 * If the xattr property is off, refuse the lookup request.
1564 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1566 return (SET_ERROR(EINVAL));
1571 * We don't allow recursive attributes..
1572 * Maybe someday we will.
1574 if (zdp->z_pflags & ZFS_XATTR) {
1576 return (SET_ERROR(EINVAL));
1579 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1585 * Do we have permission to get into attribute directory?
1587 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1598 * Check accessibility of directory.
1600 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1605 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1606 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1608 return (SET_ERROR(EILSEQ));
1613 * First handle the special cases.
1615 if ((cnp->cn_flags & ISDOTDOT) != 0) {
1617 * If we are a snapshot mounted under .zfs, return
1618 * the vp for the snapshot directory.
1620 if (zdp->z_id == zfsvfs->z_root && zfsvfs->z_parent != zfsvfs) {
1621 struct componentname cn;
1626 ltype = VOP_ISLOCKED(dvp);
1628 error = zfsctl_root(zfsvfs->z_parent, LK_SHARED,
1631 cn.cn_nameptr = "snapshot";
1632 cn.cn_namelen = strlen(cn.cn_nameptr);
1633 cn.cn_nameiop = cnp->cn_nameiop;
1634 cn.cn_flags = cnp->cn_flags & ~ISDOTDOT;
1635 cn.cn_lkflags = cnp->cn_lkflags;
1636 error = VOP_LOOKUP(zfsctl_vp, vpp, &cn);
1639 vn_lock(dvp, ltype | LK_RETRY);
1643 if (zfs_has_ctldir(zdp) && strcmp(nm, ZFS_CTLDIR_NAME) == 0) {
1645 if ((cnp->cn_flags & ISLASTCN) != 0 && nameiop != LOOKUP)
1646 return (SET_ERROR(ENOTSUP));
1647 error = zfsctl_root(zfsvfs, cnp->cn_lkflags, vpp);
1652 * The loop is retry the lookup if the parent-child relationship
1653 * changes during the dot-dot locking complexities.
1658 error = zfs_dirlook(zdp, nm, &zp);
1666 error = zfs_lookup_lock(dvp, *vpp, nm, cnp->cn_lkflags);
1669 * If we've got a locking error, then the vnode
1670 * got reclaimed because of a force unmount.
1671 * We never enter doomed vnodes into the name cache.
1677 if ((cnp->cn_flags & ISDOTDOT) == 0)
1681 if (zdp->z_sa_hdl == NULL) {
1682 error = SET_ERROR(EIO);
1684 error = sa_lookup(zdp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1685 &parent, sizeof (parent));
1692 if (zp->z_id == parent) {
1703 /* Translate errors and add SAVENAME when needed. */
1704 if (cnp->cn_flags & ISLASTCN) {
1708 if (error == ENOENT) {
1709 error = EJUSTRETURN;
1710 cnp->cn_flags |= SAVENAME;
1716 cnp->cn_flags |= SAVENAME;
1721 /* Insert name into cache (as non-existent) if appropriate. */
1722 if (zfsvfs->z_use_namecache &&
1723 error == ENOENT && (cnp->cn_flags & MAKEENTRY) != 0)
1724 cache_enter(dvp, NULL, cnp);
1726 /* Insert name into cache if appropriate. */
1727 if (zfsvfs->z_use_namecache &&
1728 error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1729 if (!(cnp->cn_flags & ISLASTCN) ||
1730 (nameiop != DELETE && nameiop != RENAME)) {
1731 cache_enter(dvp, *vpp, cnp);
1739 * Attempt to create a new entry in a directory. If the entry
1740 * already exists, truncate the file if permissible, else return
1741 * an error. Return the vp of the created or trunc'd file.
1743 * IN: dvp - vnode of directory to put new file entry in.
1744 * name - name of new file entry.
1745 * vap - attributes of new file.
1746 * excl - flag indicating exclusive or non-exclusive mode.
1747 * mode - mode to open file with.
1748 * cr - credentials of caller.
1749 * flag - large file flag [UNUSED].
1750 * ct - caller context
1751 * vsecp - ACL to be set
1753 * OUT: vpp - vnode of created or trunc'd entry.
1755 * RETURN: 0 on success, error code on failure.
1758 * dvp - ctime|mtime updated if new entry created
1759 * vp - ctime|mtime always, atime if new
1764 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1765 vnode_t **vpp, cred_t *cr, kthread_t *td)
1767 znode_t *zp, *dzp = VTOZ(dvp);
1768 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1775 gid_t gid = crgetgid(cr);
1776 zfs_acl_ids_t acl_ids;
1777 boolean_t fuid_dirtied;
1783 * If we have an ephemeral id, ACL, or XVATTR then
1784 * make sure file system is at proper version
1787 ksid = crgetsid(cr, KSID_OWNER);
1789 uid = ksid_getid(ksid);
1793 if (zfsvfs->z_use_fuids == B_FALSE &&
1794 (vsecp || (vap->va_mask & AT_XVATTR) ||
1795 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1796 return (SET_ERROR(EINVAL));
1801 zilog = zfsvfs->z_log;
1803 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1804 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1806 return (SET_ERROR(EILSEQ));
1809 if (vap->va_mask & AT_XVATTR) {
1810 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1811 crgetuid(cr), cr, vap->va_type)) != 0) {
1819 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1820 vap->va_mode &= ~S_ISVTX;
1822 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
1827 ASSERT3P(zp, ==, NULL);
1830 * Create a new file object and update the directory
1833 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1838 * We only support the creation of regular files in
1839 * extended attribute directories.
1842 if ((dzp->z_pflags & ZFS_XATTR) &&
1843 (vap->va_type != VREG)) {
1844 error = SET_ERROR(EINVAL);
1848 if ((error = zfs_acl_ids_create(dzp, 0, vap,
1849 cr, vsecp, &acl_ids)) != 0)
1852 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1853 zfs_acl_ids_free(&acl_ids);
1854 error = SET_ERROR(EDQUOT);
1858 getnewvnode_reserve(1);
1860 tx = dmu_tx_create(os);
1862 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1863 ZFS_SA_BASE_ATTR_SIZE);
1865 fuid_dirtied = zfsvfs->z_fuid_dirty;
1867 zfs_fuid_txhold(zfsvfs, tx);
1868 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1869 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1870 if (!zfsvfs->z_use_sa &&
1871 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1872 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1873 0, acl_ids.z_aclp->z_acl_bytes);
1875 error = dmu_tx_assign(tx, TXG_WAIT);
1877 zfs_acl_ids_free(&acl_ids);
1879 getnewvnode_drop_reserve();
1883 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1886 zfs_fuid_sync(zfsvfs, tx);
1888 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
1889 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1890 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1891 vsecp, acl_ids.z_fuidp, vap);
1892 zfs_acl_ids_free(&acl_ids);
1895 getnewvnode_drop_reserve();
1902 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1903 zil_commit(zilog, 0);
1910 * Remove an entry from a directory.
1912 * IN: dvp - vnode of directory to remove entry from.
1913 * name - name of entry to remove.
1914 * cr - credentials of caller.
1915 * ct - caller context
1916 * flags - case flags
1918 * RETURN: 0 on success, error code on failure.
1922 * vp - ctime (if nlink > 0)
1927 zfs_remove(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
1929 znode_t *dzp = VTOZ(dvp);
1930 znode_t *zp = VTOZ(vp);
1932 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1934 uint64_t acl_obj, xattr_obj;
1937 boolean_t unlinked, toobig = FALSE;
1944 zilog = zfsvfs->z_log;
1950 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1955 * Need to use rmdir for removing directories.
1957 if (vp->v_type == VDIR) {
1958 error = SET_ERROR(EPERM);
1962 vnevent_remove(vp, dvp, name, ct);
1966 /* are there any extended attributes? */
1967 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1968 &xattr_obj, sizeof (xattr_obj));
1969 if (error == 0 && xattr_obj) {
1970 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1975 * We may delete the znode now, or we may put it in the unlinked set;
1976 * it depends on whether we're the last link, and on whether there are
1977 * other holds on the vnode. So we dmu_tx_hold() the right things to
1978 * allow for either case.
1980 tx = dmu_tx_create(zfsvfs->z_os);
1981 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1982 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1983 zfs_sa_upgrade_txholds(tx, zp);
1984 zfs_sa_upgrade_txholds(tx, dzp);
1987 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1988 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1991 /* charge as an update -- would be nice not to charge at all */
1992 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1995 * Mark this transaction as typically resulting in a net free of space
1997 dmu_tx_mark_netfree(tx);
1999 error = dmu_tx_assign(tx, TXG_WAIT);
2007 * Remove the directory entry.
2009 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, &unlinked);
2017 zfs_unlinked_add(zp, tx);
2018 vp->v_vflag |= VV_NOSYNC;
2022 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2030 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2031 zil_commit(zilog, 0);
2038 * Create a new directory and insert it into dvp using the name
2039 * provided. Return a pointer to the inserted directory.
2041 * IN: dvp - vnode of directory to add subdir to.
2042 * dirname - name of new directory.
2043 * vap - attributes of new directory.
2044 * cr - credentials of caller.
2045 * ct - caller context
2046 * flags - case flags
2047 * vsecp - ACL to be set
2049 * OUT: vpp - vnode of created directory.
2051 * RETURN: 0 on success, error code on failure.
2054 * dvp - ctime|mtime updated
2055 * vp - ctime|mtime|atime updated
2059 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
2061 znode_t *zp, *dzp = VTOZ(dvp);
2062 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2069 gid_t gid = crgetgid(cr);
2070 zfs_acl_ids_t acl_ids;
2071 boolean_t fuid_dirtied;
2073 ASSERT(vap->va_type == VDIR);
2076 * If we have an ephemeral id, ACL, or XVATTR then
2077 * make sure file system is at proper version
2080 ksid = crgetsid(cr, KSID_OWNER);
2082 uid = ksid_getid(ksid);
2085 if (zfsvfs->z_use_fuids == B_FALSE &&
2086 ((vap->va_mask & AT_XVATTR) ||
2087 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2088 return (SET_ERROR(EINVAL));
2092 zilog = zfsvfs->z_log;
2094 if (dzp->z_pflags & ZFS_XATTR) {
2096 return (SET_ERROR(EINVAL));
2099 if (zfsvfs->z_utf8 && u8_validate(dirname,
2100 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2102 return (SET_ERROR(EILSEQ));
2105 if (vap->va_mask & AT_XVATTR) {
2106 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2107 crgetuid(cr), cr, vap->va_type)) != 0) {
2113 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2114 NULL, &acl_ids)) != 0) {
2120 * First make sure the new directory doesn't exist.
2122 * Existence is checked first to make sure we don't return
2123 * EACCES instead of EEXIST which can cause some applications
2128 if (error = zfs_dirent_lookup(dzp, dirname, &zp, ZNEW)) {
2129 zfs_acl_ids_free(&acl_ids);
2133 ASSERT3P(zp, ==, NULL);
2135 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2136 zfs_acl_ids_free(&acl_ids);
2141 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2142 zfs_acl_ids_free(&acl_ids);
2144 return (SET_ERROR(EDQUOT));
2148 * Add a new entry to the directory.
2150 getnewvnode_reserve(1);
2151 tx = dmu_tx_create(zfsvfs->z_os);
2152 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2153 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2154 fuid_dirtied = zfsvfs->z_fuid_dirty;
2156 zfs_fuid_txhold(zfsvfs, tx);
2157 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2158 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2159 acl_ids.z_aclp->z_acl_bytes);
2162 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2163 ZFS_SA_BASE_ATTR_SIZE);
2165 error = dmu_tx_assign(tx, TXG_WAIT);
2167 zfs_acl_ids_free(&acl_ids);
2169 getnewvnode_drop_reserve();
2177 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2180 zfs_fuid_sync(zfsvfs, tx);
2183 * Now put new name in parent dir.
2185 (void) zfs_link_create(dzp, dirname, zp, tx, ZNEW);
2189 txtype = zfs_log_create_txtype(Z_DIR, NULL, vap);
2190 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, NULL,
2191 acl_ids.z_fuidp, vap);
2193 zfs_acl_ids_free(&acl_ids);
2197 getnewvnode_drop_reserve();
2199 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2200 zil_commit(zilog, 0);
2207 * Remove a directory subdir entry. If the current working
2208 * directory is the same as the subdir to be removed, the
2211 * IN: dvp - vnode of directory to remove from.
2212 * name - name of directory to be removed.
2213 * cwd - vnode of current working directory.
2214 * cr - credentials of caller.
2215 * ct - caller context
2216 * flags - case flags
2218 * RETURN: 0 on success, error code on failure.
2221 * dvp - ctime|mtime updated
2225 zfs_rmdir(vnode_t *dvp, vnode_t *vp, char *name, cred_t *cr)
2227 znode_t *dzp = VTOZ(dvp);
2228 znode_t *zp = VTOZ(vp);
2229 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2237 zilog = zfsvfs->z_log;
2240 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2244 if (vp->v_type != VDIR) {
2245 error = SET_ERROR(ENOTDIR);
2249 vnevent_rmdir(vp, dvp, name, ct);
2251 tx = dmu_tx_create(zfsvfs->z_os);
2252 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2253 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2254 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2255 zfs_sa_upgrade_txholds(tx, zp);
2256 zfs_sa_upgrade_txholds(tx, dzp);
2257 dmu_tx_mark_netfree(tx);
2258 error = dmu_tx_assign(tx, TXG_WAIT);
2267 error = zfs_link_destroy(dzp, name, zp, tx, ZEXISTS, NULL);
2270 uint64_t txtype = TX_RMDIR;
2271 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2278 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2279 zil_commit(zilog, 0);
2286 * Read as many directory entries as will fit into the provided
2287 * buffer from the given directory cursor position (specified in
2288 * the uio structure).
2290 * IN: vp - vnode of directory to read.
2291 * uio - structure supplying read location, range info,
2292 * and return buffer.
2293 * cr - credentials of caller.
2294 * ct - caller context
2295 * flags - case flags
2297 * OUT: uio - updated offset and range, buffer filled.
2298 * eofp - set to true if end-of-file detected.
2300 * RETURN: 0 on success, error code on failure.
2303 * vp - atime updated
2305 * Note that the low 4 bits of the cookie returned by zap is always zero.
2306 * This allows us to use the low range for "special" directory entries:
2307 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2308 * we use the offset 2 for the '.zfs' directory.
2312 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2314 znode_t *zp = VTOZ(vp);
2318 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2323 zap_attribute_t zap;
2324 uint_t bytes_wanted;
2325 uint64_t offset; /* must be unsigned; checks for < 1 */
2331 boolean_t check_sysattrs;
2334 u_long *cooks = NULL;
2340 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2341 &parent, sizeof (parent))) != 0) {
2347 * If we are not given an eof variable,
2354 * Check for valid iov_len.
2356 if (uio->uio_iov->iov_len <= 0) {
2358 return (SET_ERROR(EINVAL));
2362 * Quit if directory has been removed (posix)
2364 if ((*eofp = zp->z_unlinked) != 0) {
2371 offset = uio->uio_loffset;
2372 prefetch = zp->z_zn_prefetch;
2375 * Initialize the iterator cursor.
2379 * Start iteration from the beginning of the directory.
2381 zap_cursor_init(&zc, os, zp->z_id);
2384 * The offset is a serialized cursor.
2386 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2390 * Get space to change directory entries into fs independent format.
2392 iovp = uio->uio_iov;
2393 bytes_wanted = iovp->iov_len;
2394 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2395 bufsize = bytes_wanted;
2396 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2397 odp = (struct dirent64 *)outbuf;
2399 bufsize = bytes_wanted;
2401 odp = (struct dirent64 *)iovp->iov_base;
2403 eodp = (struct edirent *)odp;
2405 if (ncookies != NULL) {
2407 * Minimum entry size is dirent size and 1 byte for a file name.
2409 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2410 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2415 * If this VFS supports the system attribute view interface; and
2416 * we're looking at an extended attribute directory; and we care
2417 * about normalization conflicts on this vfs; then we must check
2418 * for normalization conflicts with the sysattr name space.
2421 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2422 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2423 (flags & V_RDDIR_ENTFLAGS);
2429 * Transform to file-system independent format
2432 while (outcount < bytes_wanted) {
2435 off64_t *next = NULL;
2438 * Special case `.', `..', and `.zfs'.
2441 (void) strcpy(zap.za_name, ".");
2442 zap.za_normalization_conflict = 0;
2445 } else if (offset == 1) {
2446 (void) strcpy(zap.za_name, "..");
2447 zap.za_normalization_conflict = 0;
2450 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2451 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2452 zap.za_normalization_conflict = 0;
2453 objnum = ZFSCTL_INO_ROOT;
2459 if (error = zap_cursor_retrieve(&zc, &zap)) {
2460 if ((*eofp = (error == ENOENT)) != 0)
2466 if (zap.za_integer_length != 8 ||
2467 zap.za_num_integers != 1) {
2468 cmn_err(CE_WARN, "zap_readdir: bad directory "
2469 "entry, obj = %lld, offset = %lld\n",
2470 (u_longlong_t)zp->z_id,
2471 (u_longlong_t)offset);
2472 error = SET_ERROR(ENXIO);
2476 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2478 * MacOS X can extract the object type here such as:
2479 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2481 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2483 if (check_sysattrs && !zap.za_normalization_conflict) {
2485 zap.za_normalization_conflict =
2486 xattr_sysattr_casechk(zap.za_name);
2488 panic("%s:%u: TODO", __func__, __LINE__);
2493 if (flags & V_RDDIR_ACCFILTER) {
2495 * If we have no access at all, don't include
2496 * this entry in the returned information
2499 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2501 if (!zfs_has_access(ezp, cr)) {
2508 if (flags & V_RDDIR_ENTFLAGS)
2509 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2511 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2514 * Will this entry fit in the buffer?
2516 if (outcount + reclen > bufsize) {
2518 * Did we manage to fit anything in the buffer?
2521 error = SET_ERROR(EINVAL);
2526 if (flags & V_RDDIR_ENTFLAGS) {
2528 * Add extended flag entry:
2530 eodp->ed_ino = objnum;
2531 eodp->ed_reclen = reclen;
2532 /* NOTE: ed_off is the offset for the *next* entry. */
2533 next = &eodp->ed_off;
2534 eodp->ed_eflags = zap.za_normalization_conflict ?
2535 ED_CASE_CONFLICT : 0;
2536 (void) strncpy(eodp->ed_name, zap.za_name,
2537 EDIRENT_NAMELEN(reclen));
2538 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2543 odp->d_ino = objnum;
2544 odp->d_reclen = reclen;
2545 odp->d_namlen = strlen(zap.za_name);
2546 /* NOTE: d_off is the offset for the *next* entry. */
2548 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2550 dirent_terminate(odp);
2551 odp = (dirent64_t *)((intptr_t)odp + reclen);
2555 ASSERT(outcount <= bufsize);
2557 /* Prefetch znode */
2559 dmu_prefetch(os, objnum, 0, 0, 0,
2560 ZIO_PRIORITY_SYNC_READ);
2564 * Move to the next entry, fill in the previous offset.
2566 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2567 zap_cursor_advance(&zc);
2568 offset = zap_cursor_serialize(&zc);
2573 /* Fill the offset right after advancing the cursor. */
2576 if (cooks != NULL) {
2579 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2582 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2584 /* Subtract unused cookies */
2585 if (ncookies != NULL)
2586 *ncookies -= ncooks;
2588 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2589 iovp->iov_base += outcount;
2590 iovp->iov_len -= outcount;
2591 uio->uio_resid -= outcount;
2592 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2594 * Reset the pointer.
2596 offset = uio->uio_loffset;
2600 zap_cursor_fini(&zc);
2601 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2602 kmem_free(outbuf, bufsize);
2604 if (error == ENOENT)
2607 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2609 uio->uio_loffset = offset;
2611 if (error != 0 && cookies != NULL) {
2612 free(*cookies, M_TEMP);
2619 ulong_t zfs_fsync_sync_cnt = 4;
2622 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2624 znode_t *zp = VTOZ(vp);
2625 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2627 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2629 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2632 zil_commit(zfsvfs->z_log, zp->z_id);
2640 * Get the requested file attributes and place them in the provided
2643 * IN: vp - vnode of file.
2644 * vap - va_mask identifies requested attributes.
2645 * If AT_XVATTR set, then optional attrs are requested
2646 * flags - ATTR_NOACLCHECK (CIFS server context)
2647 * cr - credentials of caller.
2648 * ct - caller context
2650 * OUT: vap - attribute values.
2652 * RETURN: 0 (always succeeds).
2656 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2657 caller_context_t *ct)
2659 znode_t *zp = VTOZ(vp);
2660 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2663 u_longlong_t nblocks;
2664 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2665 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2666 xoptattr_t *xoap = NULL;
2667 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2668 sa_bulk_attr_t bulk[4];
2674 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2676 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2677 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2678 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2679 if (vp->v_type == VBLK || vp->v_type == VCHR)
2680 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2683 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2689 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2690 * Also, if we are the owner don't bother, since owner should
2691 * always be allowed to read basic attributes of file.
2693 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2694 (vap->va_uid != crgetuid(cr))) {
2695 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2703 * Return all attributes. It's cheaper to provide the answer
2704 * than to determine whether we were asked the question.
2707 vap->va_type = IFTOVT(zp->z_mode);
2708 vap->va_mode = zp->z_mode & ~S_IFMT;
2710 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2714 vap->va_nodeid = zp->z_id;
2715 vap->va_nlink = zp->z_links;
2716 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp) &&
2717 zp->z_links < ZFS_LINK_MAX)
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));
2950 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2954 * Verify timestamps doesn't overflow 32 bits.
2955 * ZFS can handle large timestamps, but 32bit syscalls can't
2956 * handle times greater than 2039. This check should be removed
2957 * once large timestamps are fully supported.
2959 if (mask & (AT_ATIME | AT_MTIME)) {
2960 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2961 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2963 return (SET_ERROR(EOVERFLOW));
2966 if (xoap && (mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME) &&
2967 TIMESPEC_OVERFLOW(&vap->va_birthtime)) {
2969 return (SET_ERROR(EOVERFLOW));
2975 /* Can this be moved to before the top label? */
2976 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2978 return (SET_ERROR(EROFS));
2982 * First validate permissions
2985 if (mask & AT_SIZE) {
2987 * XXX - Note, we are not providing any open
2988 * mode flags here (like FNDELAY), so we may
2989 * block if there are locks present... this
2990 * should be addressed in openat().
2992 /* XXX - would it be OK to generate a log record here? */
2993 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3000 if (mask & (AT_ATIME|AT_MTIME) ||
3001 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3002 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3003 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3004 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3005 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3006 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3007 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3008 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3012 if (mask & (AT_UID|AT_GID)) {
3013 int idmask = (mask & (AT_UID|AT_GID));
3018 * NOTE: even if a new mode is being set,
3019 * we may clear S_ISUID/S_ISGID bits.
3022 if (!(mask & AT_MODE))
3023 vap->va_mode = zp->z_mode;
3026 * Take ownership or chgrp to group we are a member of
3029 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3030 take_group = (mask & AT_GID) &&
3031 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3034 * If both AT_UID and AT_GID are set then take_owner and
3035 * take_group must both be set in order to allow taking
3038 * Otherwise, send the check through secpolicy_vnode_setattr()
3042 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3043 ((idmask == AT_UID) && take_owner) ||
3044 ((idmask == AT_GID) && take_group)) {
3045 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3046 skipaclchk, cr) == 0) {
3048 * Remove setuid/setgid for non-privileged users
3050 secpolicy_setid_clear(vap, vp, cr);
3051 trim_mask = (mask & (AT_UID|AT_GID));
3060 oldva.va_mode = zp->z_mode;
3061 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3062 if (mask & AT_XVATTR) {
3064 * Update xvattr mask to include only those attributes
3065 * that are actually changing.
3067 * the bits will be restored prior to actually setting
3068 * the attributes so the caller thinks they were set.
3070 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3071 if (xoap->xoa_appendonly !=
3072 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3075 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3076 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3080 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3081 if (xoap->xoa_nounlink !=
3082 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3085 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3086 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3090 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3091 if (xoap->xoa_immutable !=
3092 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3095 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3096 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3100 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3101 if (xoap->xoa_nodump !=
3102 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3105 XVA_CLR_REQ(xvap, XAT_NODUMP);
3106 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3110 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3111 if (xoap->xoa_av_modified !=
3112 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3115 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3116 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3120 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3121 if ((vp->v_type != VREG &&
3122 xoap->xoa_av_quarantined) ||
3123 xoap->xoa_av_quarantined !=
3124 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3127 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3128 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3132 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3134 return (SET_ERROR(EPERM));
3137 if (need_policy == FALSE &&
3138 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3139 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3144 if (mask & AT_MODE) {
3145 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3146 err = secpolicy_setid_setsticky_clear(vp, vap,
3152 trim_mask |= AT_MODE;
3160 * If trim_mask is set then take ownership
3161 * has been granted or write_acl is present and user
3162 * has the ability to modify mode. In that case remove
3163 * UID|GID and or MODE from mask so that
3164 * secpolicy_vnode_setattr() doesn't revoke it.
3168 saved_mask = vap->va_mask;
3169 vap->va_mask &= ~trim_mask;
3170 if (trim_mask & AT_MODE) {
3172 * Save the mode, as secpolicy_vnode_setattr()
3173 * will overwrite it with ova.va_mode.
3175 saved_mode = vap->va_mode;
3178 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3179 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3186 vap->va_mask |= saved_mask;
3187 if (trim_mask & AT_MODE) {
3189 * Recover the mode after
3190 * secpolicy_vnode_setattr().
3192 vap->va_mode = saved_mode;
3198 * secpolicy_vnode_setattr, or take ownership may have
3201 mask = vap->va_mask;
3203 if ((mask & (AT_UID | AT_GID))) {
3204 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3205 &xattr_obj, sizeof (xattr_obj));
3207 if (err == 0 && xattr_obj) {
3208 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3210 err = vn_lock(ZTOV(attrzp), LK_EXCLUSIVE);
3212 vrele(ZTOV(attrzp));
3217 if (mask & AT_UID) {
3218 new_uid = zfs_fuid_create(zfsvfs,
3219 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3220 if (new_uid != zp->z_uid &&
3221 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3224 err = SET_ERROR(EDQUOT);
3229 if (mask & AT_GID) {
3230 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3231 cr, ZFS_GROUP, &fuidp);
3232 if (new_gid != zp->z_gid &&
3233 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3236 err = SET_ERROR(EDQUOT);
3241 tx = dmu_tx_create(zfsvfs->z_os);
3243 if (mask & AT_MODE) {
3244 uint64_t pmode = zp->z_mode;
3246 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3248 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3249 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3250 err = SET_ERROR(EPERM);
3254 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3257 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3259 * Are we upgrading ACL from old V0 format
3262 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3263 zfs_znode_acl_version(zp) ==
3264 ZFS_ACL_VERSION_INITIAL) {
3265 dmu_tx_hold_free(tx, acl_obj, 0,
3267 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3268 0, aclp->z_acl_bytes);
3270 dmu_tx_hold_write(tx, acl_obj, 0,
3273 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3274 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3275 0, aclp->z_acl_bytes);
3277 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3279 if ((mask & AT_XVATTR) &&
3280 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3281 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3283 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3287 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3290 fuid_dirtied = zfsvfs->z_fuid_dirty;
3292 zfs_fuid_txhold(zfsvfs, tx);
3294 zfs_sa_upgrade_txholds(tx, zp);
3296 err = dmu_tx_assign(tx, TXG_WAIT);
3302 * Set each attribute requested.
3303 * We group settings according to the locks they need to acquire.
3305 * Note: you cannot set ctime directly, although it will be
3306 * updated as a side-effect of calling this function.
3309 if (mask & (AT_UID|AT_GID|AT_MODE))
3310 mutex_enter(&zp->z_acl_lock);
3312 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3313 &zp->z_pflags, sizeof (zp->z_pflags));
3316 if (mask & (AT_UID|AT_GID|AT_MODE))
3317 mutex_enter(&attrzp->z_acl_lock);
3318 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3319 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3320 sizeof (attrzp->z_pflags));
3323 if (mask & (AT_UID|AT_GID)) {
3325 if (mask & AT_UID) {
3326 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3327 &new_uid, sizeof (new_uid));
3328 zp->z_uid = new_uid;
3330 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3331 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3333 attrzp->z_uid = new_uid;
3337 if (mask & AT_GID) {
3338 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3339 NULL, &new_gid, sizeof (new_gid));
3340 zp->z_gid = new_gid;
3342 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3343 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3345 attrzp->z_gid = new_gid;
3348 if (!(mask & AT_MODE)) {
3349 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3350 NULL, &new_mode, sizeof (new_mode));
3351 new_mode = zp->z_mode;
3353 err = zfs_acl_chown_setattr(zp);
3356 err = zfs_acl_chown_setattr(attrzp);
3361 if (mask & AT_MODE) {
3362 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3363 &new_mode, sizeof (new_mode));
3364 zp->z_mode = new_mode;
3365 ASSERT3U((uintptr_t)aclp, !=, 0);
3366 err = zfs_aclset_common(zp, aclp, cr, tx);
3368 if (zp->z_acl_cached)
3369 zfs_acl_free(zp->z_acl_cached);
3370 zp->z_acl_cached = aclp;
3375 if (mask & AT_ATIME) {
3376 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3377 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3378 &zp->z_atime, sizeof (zp->z_atime));
3381 if (mask & AT_MTIME) {
3382 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3383 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3384 mtime, sizeof (mtime));
3387 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3388 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3389 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3390 NULL, mtime, sizeof (mtime));
3391 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3392 &ctime, sizeof (ctime));
3393 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3395 } else if (mask != 0) {
3396 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3397 &ctime, sizeof (ctime));
3398 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3401 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3402 SA_ZPL_CTIME(zfsvfs), NULL,
3403 &ctime, sizeof (ctime));
3404 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3405 mtime, ctime, B_TRUE);
3409 * Do this after setting timestamps to prevent timestamp
3410 * update from toggling bit
3413 if (xoap && (mask & AT_XVATTR)) {
3415 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME))
3416 xoap->xoa_createtime = vap->va_birthtime;
3418 * restore trimmed off masks
3419 * so that return masks can be set for caller.
3422 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3423 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3425 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3426 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3428 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3429 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3431 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3432 XVA_SET_REQ(xvap, XAT_NODUMP);
3434 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3435 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3437 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3438 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3441 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3442 ASSERT(vp->v_type == VREG);
3444 zfs_xvattr_set(zp, xvap, tx);
3448 zfs_fuid_sync(zfsvfs, tx);
3451 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3453 if (mask & (AT_UID|AT_GID|AT_MODE))
3454 mutex_exit(&zp->z_acl_lock);
3457 if (mask & (AT_UID|AT_GID|AT_MODE))
3458 mutex_exit(&attrzp->z_acl_lock);
3461 if (err == 0 && attrzp) {
3462 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3474 zfs_fuid_info_free(fuidp);
3481 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3486 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3487 zil_commit(zilog, 0);
3494 * We acquire all but fdvp locks using non-blocking acquisitions. If we
3495 * fail to acquire any lock in the path we will drop all held locks,
3496 * acquire the new lock in a blocking fashion, and then release it and
3497 * restart the rename. This acquire/release step ensures that we do not
3498 * spin on a lock waiting for release. On error release all vnode locks
3499 * and decrement references the way tmpfs_rename() would do.
3502 zfs_rename_relock(struct vnode *sdvp, struct vnode **svpp,
3503 struct vnode *tdvp, struct vnode **tvpp,
3504 const struct componentname *scnp, const struct componentname *tcnp)
3507 struct vnode *nvp, *svp, *tvp;
3508 znode_t *sdzp, *tdzp, *szp, *tzp;
3509 const char *snm = scnp->cn_nameptr;
3510 const char *tnm = tcnp->cn_nameptr;
3513 VOP_UNLOCK(tdvp, 0);
3514 if (*tvpp != NULL && *tvpp != tdvp)
3515 VOP_UNLOCK(*tvpp, 0);
3518 error = vn_lock(sdvp, LK_EXCLUSIVE);
3523 error = vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT);
3525 VOP_UNLOCK(sdvp, 0);
3528 error = vn_lock(tdvp, LK_EXCLUSIVE);
3531 VOP_UNLOCK(tdvp, 0);
3537 * Before using sdzp and tdzp we must ensure that they are live.
3538 * As a porting legacy from illumos we have two things to worry
3539 * about. One is typical for FreeBSD and it is that the vnode is
3540 * not reclaimed (doomed). The other is that the znode is live.
3541 * The current code can invalidate the znode without acquiring the
3542 * corresponding vnode lock if the object represented by the znode
3543 * and vnode is no longer valid after a rollback or receive operation.
3544 * z_teardown_lock hidden behind ZFS_ENTER and ZFS_EXIT is the lock
3545 * that protects the znodes from the invalidation.
3547 zfsvfs = sdzp->z_zfsvfs;
3548 ASSERT3P(zfsvfs, ==, tdzp->z_zfsvfs);
3552 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3553 * bypassing the cleanup code in the case of an error.
3555 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3557 VOP_UNLOCK(sdvp, 0);
3558 VOP_UNLOCK(tdvp, 0);
3559 error = SET_ERROR(EIO);
3564 * Re-resolve svp to be certain it still exists and fetch the
3567 error = zfs_dirent_lookup(sdzp, snm, &szp, ZEXISTS);
3569 /* Source entry invalid or not there. */
3571 VOP_UNLOCK(sdvp, 0);
3572 VOP_UNLOCK(tdvp, 0);
3573 if ((scnp->cn_flags & ISDOTDOT) != 0 ||
3574 (scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.'))
3575 error = SET_ERROR(EINVAL);
3581 * Re-resolve tvp, if it disappeared we just carry on.
3583 error = zfs_dirent_lookup(tdzp, tnm, &tzp, 0);
3586 VOP_UNLOCK(sdvp, 0);
3587 VOP_UNLOCK(tdvp, 0);
3589 if ((tcnp->cn_flags & ISDOTDOT) != 0)
3590 error = SET_ERROR(EINVAL);
3599 * At present the vnode locks must be acquired before z_teardown_lock,
3600 * although it would be more logical to use the opposite order.
3605 * Now try acquire locks on svp and tvp.
3608 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3610 VOP_UNLOCK(sdvp, 0);
3611 VOP_UNLOCK(tdvp, 0);
3614 if (error != EBUSY) {
3618 error = vn_lock(nvp, LK_EXCLUSIVE);
3625 * Concurrent rename race.
3630 error = SET_ERROR(EINVAL);
3645 error = vn_lock(nvp, LK_EXCLUSIVE | LK_NOWAIT);
3647 VOP_UNLOCK(sdvp, 0);
3648 VOP_UNLOCK(tdvp, 0);
3649 VOP_UNLOCK(*svpp, 0);
3650 if (error != EBUSY) {
3654 error = vn_lock(nvp, LK_EXCLUSIVE);
3672 * Note that we must use VRELE_ASYNC in this function as it walks
3673 * up the directory tree and vrele may need to acquire an exclusive
3674 * lock if a last reference to a vnode is dropped.
3677 zfs_rename_check(znode_t *szp, znode_t *sdzp, znode_t *tdzp)
3684 zfsvfs = tdzp->z_zfsvfs;
3686 return (SET_ERROR(EINVAL));
3689 if (tdzp->z_id == zfsvfs->z_root)
3693 ASSERT(!zp->z_unlinked);
3694 if ((error = sa_lookup(zp->z_sa_hdl,
3695 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
3698 if (parent == szp->z_id) {
3699 error = SET_ERROR(EINVAL);
3702 if (parent == zfsvfs->z_root)
3704 if (parent == sdzp->z_id)
3707 error = zfs_zget(zfsvfs, parent, &zp1);
3712 VN_RELE_ASYNC(ZTOV(zp),
3713 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3717 if (error == ENOTDIR)
3718 panic("checkpath: .. not a directory\n");
3720 VN_RELE_ASYNC(ZTOV(zp),
3721 dsl_pool_vnrele_taskq(dmu_objset_pool(zfsvfs->z_os)));
3726 * Move an entry from the provided source directory to the target
3727 * directory. Change the entry name as indicated.
3729 * IN: sdvp - Source directory containing the "old entry".
3730 * snm - Old entry name.
3731 * tdvp - Target directory to contain the "new entry".
3732 * tnm - New entry name.
3733 * cr - credentials of caller.
3734 * ct - caller context
3735 * flags - case flags
3737 * RETURN: 0 on success, error code on failure.
3740 * sdvp,tdvp - ctime|mtime updated
3744 zfs_rename(vnode_t *sdvp, vnode_t **svpp, struct componentname *scnp,
3745 vnode_t *tdvp, vnode_t **tvpp, struct componentname *tcnp,
3749 znode_t *sdzp, *tdzp, *szp, *tzp;
3750 zilog_t *zilog = NULL;
3752 char *snm = scnp->cn_nameptr;
3753 char *tnm = tcnp->cn_nameptr;
3756 /* Reject renames across filesystems. */
3757 if ((*svpp)->v_mount != tdvp->v_mount ||
3758 ((*tvpp) != NULL && (*svpp)->v_mount != (*tvpp)->v_mount)) {
3759 error = SET_ERROR(EXDEV);
3763 if (zfsctl_is_node(tdvp)) {
3764 error = SET_ERROR(EXDEV);
3769 * Lock all four vnodes to ensure safety and semantics of renaming.
3771 error = zfs_rename_relock(sdvp, svpp, tdvp, tvpp, scnp, tcnp);
3773 /* no vnodes are locked in the case of error here */
3779 zfsvfs = tdzp->z_zfsvfs;
3780 zilog = zfsvfs->z_log;
3783 * After we re-enter ZFS_ENTER() we will have to revalidate all
3788 if (zfsvfs->z_utf8 && u8_validate(tnm,
3789 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3790 error = SET_ERROR(EILSEQ);
3794 /* If source and target are the same file, there is nothing to do. */
3795 if ((*svpp) == (*tvpp)) {
3800 if (((*svpp)->v_type == VDIR && (*svpp)->v_mountedhere != NULL) ||
3801 ((*tvpp) != NULL && (*tvpp)->v_type == VDIR &&
3802 (*tvpp)->v_mountedhere != NULL)) {
3803 error = SET_ERROR(EXDEV);
3808 * We can not use ZFS_VERIFY_ZP() here because it could directly return
3809 * bypassing the cleanup code in the case of an error.
3811 if (tdzp->z_sa_hdl == NULL || sdzp->z_sa_hdl == NULL) {
3812 error = SET_ERROR(EIO);
3817 tzp = *tvpp == NULL ? NULL : VTOZ(*tvpp);
3818 if (szp->z_sa_hdl == NULL || (tzp != NULL && tzp->z_sa_hdl == NULL)) {
3819 error = SET_ERROR(EIO);
3824 * This is to prevent the creation of links into attribute space
3825 * by renaming a linked file into/outof an attribute directory.
3826 * See the comment in zfs_link() for why this is considered bad.
3828 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3829 error = SET_ERROR(EINVAL);
3834 * Must have write access at the source to remove the old entry
3835 * and write access at the target to create the new entry.
3836 * Note that if target and source are the same, this can be
3837 * done in a single check.
3839 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3842 if ((*svpp)->v_type == VDIR) {
3844 * Avoid ".", "..", and aliases of "." for obvious reasons.
3846 if ((scnp->cn_namelen == 1 && scnp->cn_nameptr[0] == '.') ||
3848 (scnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) {
3854 * Check to make sure rename is valid.
3855 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3857 if (error = zfs_rename_check(szp, sdzp, tdzp))
3862 * Does target exist?
3866 * Source and target must be the same type.
3868 if ((*svpp)->v_type == VDIR) {
3869 if ((*tvpp)->v_type != VDIR) {
3870 error = SET_ERROR(ENOTDIR);
3878 if ((*tvpp)->v_type == VDIR) {
3879 error = SET_ERROR(EISDIR);
3885 vnevent_rename_src(*svpp, sdvp, scnp->cn_nameptr, ct);
3887 vnevent_rename_dest(*tvpp, tdvp, tnm, ct);
3890 * notify the target directory if it is not the same
3891 * as source directory.
3894 vnevent_rename_dest_dir(tdvp, ct);
3897 tx = dmu_tx_create(zfsvfs->z_os);
3898 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3899 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3900 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3901 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3903 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3904 zfs_sa_upgrade_txholds(tx, tdzp);
3907 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3908 zfs_sa_upgrade_txholds(tx, tzp);
3911 zfs_sa_upgrade_txholds(tx, szp);
3912 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3913 error = dmu_tx_assign(tx, TXG_WAIT);
3920 if (tzp) /* Attempt to remove the existing target */
3921 error = zfs_link_destroy(tdzp, tnm, tzp, tx, 0, NULL);
3924 error = zfs_link_create(tdzp, tnm, szp, tx, ZRENAMING);
3926 szp->z_pflags |= ZFS_AV_MODIFIED;
3928 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3929 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3932 error = zfs_link_destroy(sdzp, snm, szp, tx, ZRENAMING,
3935 zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
3936 snm, tdzp, tnm, szp);
3939 * Update path information for the target vnode
3941 vn_renamepath(tdvp, *svpp, tnm, strlen(tnm));
3944 * At this point, we have successfully created
3945 * the target name, but have failed to remove
3946 * the source name. Since the create was done
3947 * with the ZRENAMING flag, there are
3948 * complications; for one, the link count is
3949 * wrong. The easiest way to deal with this
3950 * is to remove the newly created target, and
3951 * return the original error. This must
3952 * succeed; fortunately, it is very unlikely to
3953 * fail, since we just created it.
3955 VERIFY3U(zfs_link_destroy(tdzp, tnm, szp, tx,
3956 ZRENAMING, NULL), ==, 0);
3963 cache_purge_negative(tdvp);
3969 unlockout: /* all 4 vnodes are locked, ZFS_ENTER called */
3971 VOP_UNLOCK(*svpp, 0);
3972 VOP_UNLOCK(sdvp, 0);
3974 out: /* original two vnodes are locked */
3975 if (error == 0 && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3976 zil_commit(zilog, 0);
3979 VOP_UNLOCK(*tvpp, 0);
3981 VOP_UNLOCK(tdvp, 0);
3986 * Insert the indicated symbolic reference entry into the directory.
3988 * IN: dvp - Directory to contain new symbolic link.
3989 * link - Name for new symlink entry.
3990 * vap - Attributes of new entry.
3991 * cr - credentials of caller.
3992 * ct - caller context
3993 * flags - case flags
3995 * RETURN: 0 on success, error code on failure.
3998 * dvp - ctime|mtime updated
4002 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4003 cred_t *cr, kthread_t *td)
4005 znode_t *zp, *dzp = VTOZ(dvp);
4007 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4009 uint64_t len = strlen(link);
4011 zfs_acl_ids_t acl_ids;
4012 boolean_t fuid_dirtied;
4013 uint64_t txtype = TX_SYMLINK;
4016 ASSERT(vap->va_type == VLNK);
4020 zilog = zfsvfs->z_log;
4022 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4023 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4025 return (SET_ERROR(EILSEQ));
4028 if (len > MAXPATHLEN) {
4030 return (SET_ERROR(ENAMETOOLONG));
4033 if ((error = zfs_acl_ids_create(dzp, 0,
4034 vap, cr, NULL, &acl_ids)) != 0) {
4040 * Attempt to lock directory; fail if entry already exists.
4042 error = zfs_dirent_lookup(dzp, name, &zp, ZNEW);
4044 zfs_acl_ids_free(&acl_ids);
4049 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4050 zfs_acl_ids_free(&acl_ids);
4055 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4056 zfs_acl_ids_free(&acl_ids);
4058 return (SET_ERROR(EDQUOT));
4061 getnewvnode_reserve(1);
4062 tx = dmu_tx_create(zfsvfs->z_os);
4063 fuid_dirtied = zfsvfs->z_fuid_dirty;
4064 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4065 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4066 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4067 ZFS_SA_BASE_ATTR_SIZE + len);
4068 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4069 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4070 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4071 acl_ids.z_aclp->z_acl_bytes);
4074 zfs_fuid_txhold(zfsvfs, tx);
4075 error = dmu_tx_assign(tx, TXG_WAIT);
4077 zfs_acl_ids_free(&acl_ids);
4079 getnewvnode_drop_reserve();
4085 * Create a new object for the symlink.
4086 * for version 4 ZPL datsets the symlink will be an SA attribute
4088 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4091 zfs_fuid_sync(zfsvfs, tx);
4094 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4097 zfs_sa_symlink(zp, link, len, tx);
4100 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4101 &zp->z_size, sizeof (zp->z_size), tx);
4103 * Insert the new object into the directory.
4105 (void) zfs_link_create(dzp, name, zp, tx, ZNEW);
4107 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4110 zfs_acl_ids_free(&acl_ids);
4114 getnewvnode_drop_reserve();
4116 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4117 zil_commit(zilog, 0);
4124 * Return, in the buffer contained in the provided uio structure,
4125 * the symbolic path referred to by vp.
4127 * IN: vp - vnode of symbolic link.
4128 * uio - structure to contain the link path.
4129 * cr - credentials of caller.
4130 * ct - caller context
4132 * OUT: uio - structure containing the link path.
4134 * RETURN: 0 on success, error code on failure.
4137 * vp - atime updated
4141 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4143 znode_t *zp = VTOZ(vp);
4144 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4151 error = sa_lookup_uio(zp->z_sa_hdl,
4152 SA_ZPL_SYMLINK(zfsvfs), uio);
4154 error = zfs_sa_readlink(zp, uio);
4156 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4163 * Insert a new entry into directory tdvp referencing svp.
4165 * IN: tdvp - Directory to contain new entry.
4166 * svp - vnode of new entry.
4167 * name - name of new entry.
4168 * cr - credentials of caller.
4169 * ct - caller context
4171 * RETURN: 0 on success, error code on failure.
4174 * tdvp - ctime|mtime updated
4175 * svp - ctime updated
4179 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4180 caller_context_t *ct, int flags)
4182 znode_t *dzp = VTOZ(tdvp);
4184 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4191 ASSERT(tdvp->v_type == VDIR);
4195 zilog = zfsvfs->z_log;
4198 * POSIX dictates that we return EPERM here.
4199 * Better choices include ENOTSUP or EISDIR.
4201 if (svp->v_type == VDIR) {
4203 return (SET_ERROR(EPERM));
4209 if (szp->z_pflags & (ZFS_APPENDONLY | ZFS_IMMUTABLE | ZFS_READONLY)) {
4211 return (SET_ERROR(EPERM));
4214 /* Prevent links to .zfs/shares files */
4216 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4217 &parent, sizeof (uint64_t))) != 0) {
4221 if (parent == zfsvfs->z_shares_dir) {
4223 return (SET_ERROR(EPERM));
4226 if (zfsvfs->z_utf8 && u8_validate(name,
4227 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4229 return (SET_ERROR(EILSEQ));
4233 * We do not support links between attributes and non-attributes
4234 * because of the potential security risk of creating links
4235 * into "normal" file space in order to circumvent restrictions
4236 * imposed in attribute space.
4238 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4240 return (SET_ERROR(EINVAL));
4244 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4245 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4247 return (SET_ERROR(EPERM));
4250 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4256 * Attempt to lock directory; fail if entry already exists.
4258 error = zfs_dirent_lookup(dzp, name, &tzp, ZNEW);
4264 tx = dmu_tx_create(zfsvfs->z_os);
4265 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4266 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4267 zfs_sa_upgrade_txholds(tx, szp);
4268 zfs_sa_upgrade_txholds(tx, dzp);
4269 error = dmu_tx_assign(tx, TXG_WAIT);
4276 error = zfs_link_create(dzp, name, szp, tx, 0);
4279 uint64_t txtype = TX_LINK;
4280 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4286 vnevent_link(svp, ct);
4289 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4290 zil_commit(zilog, 0);
4299 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4301 znode_t *zp = VTOZ(vp);
4302 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4305 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4306 if (zp->z_sa_hdl == NULL) {
4308 * The fs has been unmounted, or we did a
4309 * suspend/resume and this file no longer exists.
4311 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4316 if (zp->z_unlinked) {
4318 * Fast path to recycle a vnode of a removed file.
4320 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4325 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4326 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4328 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4329 zfs_sa_upgrade_txholds(tx, zp);
4330 error = dmu_tx_assign(tx, TXG_WAIT);
4334 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4335 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4336 zp->z_atime_dirty = 0;
4340 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4344 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
4345 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
4349 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4351 znode_t *zp = VTOZ(vp);
4352 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4355 uint64_t object = zp->z_id;
4362 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4363 &gen64, sizeof (uint64_t))) != 0) {
4368 gen = (uint32_t)gen64;
4370 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4373 if (fidp->fid_len < size) {
4374 fidp->fid_len = size;
4376 return (SET_ERROR(ENOSPC));
4379 fidp->fid_len = size;
4382 zfid = (zfid_short_t *)fidp;
4384 zfid->zf_len = size;
4386 for (i = 0; i < sizeof (zfid->zf_object); i++)
4387 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4389 /* Must have a non-zero generation number to distinguish from .zfs */
4392 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4393 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4395 if (size == LONG_FID_LEN) {
4396 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4399 zlfid = (zfid_long_t *)fidp;
4401 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4402 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4404 /* XXX - this should be the generation number for the objset */
4405 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4406 zlfid->zf_setgen[i] = 0;
4414 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4415 caller_context_t *ct)
4423 *valp = MIN(LONG_MAX, ZFS_LINK_MAX);
4426 case _PC_FILESIZEBITS:
4430 case _PC_XATTR_EXISTS:
4432 zfsvfs = zp->z_zfsvfs;
4436 error = zfs_dirent_lookup(zp, "", &xzp,
4437 ZXATTR | ZEXISTS | ZSHARED);
4439 if (!zfs_dirempty(xzp))
4442 } else if (error == ENOENT) {
4444 * If there aren't extended attributes, it's the
4445 * same as having zero of them.
4452 case _PC_SATTR_ENABLED:
4453 case _PC_SATTR_EXISTS:
4454 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4455 (vp->v_type == VREG || vp->v_type == VDIR);
4458 case _PC_ACCESS_FILTERING:
4459 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4463 case _PC_ACL_ENABLED:
4464 *valp = _ACL_ACE_ENABLED;
4466 #endif /* illumos */
4467 case _PC_MIN_HOLE_SIZE:
4468 *valp = (int)SPA_MINBLOCKSIZE;
4471 case _PC_TIMESTAMP_RESOLUTION:
4472 /* nanosecond timestamp resolution */
4476 case _PC_ACL_EXTENDED:
4484 case _PC_ACL_PATH_MAX:
4485 *valp = ACL_MAX_ENTRIES;
4489 return (EOPNOTSUPP);
4495 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4496 caller_context_t *ct)
4498 znode_t *zp = VTOZ(vp);
4499 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4501 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4505 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4513 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4514 caller_context_t *ct)
4516 znode_t *zp = VTOZ(vp);
4517 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4519 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4520 zilog_t *zilog = zfsvfs->z_log;
4525 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4527 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4528 zil_commit(zilog, 0);
4535 zfs_getpages(struct vnode *vp, vm_page_t *ma, int count, int *rbehind,
4538 znode_t *zp = VTOZ(vp);
4539 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4540 objset_t *os = zp->z_zfsvfs->z_os;
4543 off_t start, end, obj_size;
4545 int pgsin_b, pgsin_a;
4551 start = IDX_TO_OFF(ma[0]->pindex);
4552 end = IDX_TO_OFF(ma[count - 1]->pindex + 1);
4555 * Lock a range covering all required and optional pages.
4556 * Note that we need to handle the case of the block size growing.
4559 blksz = zp->z_blksz;
4560 rl = zfs_range_lock(zp, rounddown(start, blksz),
4561 roundup(end, blksz) - rounddown(start, blksz), RL_READER);
4562 if (blksz == zp->z_blksz)
4564 zfs_range_unlock(rl);
4567 object = ma[0]->object;
4568 zfs_vmobject_wlock(object);
4569 obj_size = object->un_pager.vnp.vnp_size;
4570 zfs_vmobject_wunlock(object);
4571 if (IDX_TO_OFF(ma[count - 1]->pindex) >= obj_size) {
4572 zfs_range_unlock(rl);
4574 return (zfs_vm_pagerret_bad);
4578 if (rbehind != NULL) {
4579 pgsin_b = OFF_TO_IDX(start - rounddown(start, blksz));
4580 pgsin_b = MIN(*rbehind, pgsin_b);
4584 if (rahead != NULL) {
4585 pgsin_a = OFF_TO_IDX(roundup(end, blksz) - end);
4586 if (end + IDX_TO_OFF(pgsin_a) >= obj_size)
4587 pgsin_a = OFF_TO_IDX(round_page(obj_size) - end);
4588 pgsin_a = MIN(*rahead, pgsin_a);
4592 * NB: we need to pass the exact byte size of the data that we expect
4593 * to read after accounting for the file size. This is required because
4594 * ZFS will panic if we request DMU to read beyond the end of the last
4597 error = dmu_read_pages(os, zp->z_id, ma, count, &pgsin_b, &pgsin_a,
4598 MIN(end, obj_size) - (end - PAGE_SIZE));
4600 zfs_range_unlock(rl);
4601 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4605 return (zfs_vm_pagerret_error);
4607 VM_CNT_INC(v_vnodein);
4608 VM_CNT_ADD(v_vnodepgsin, count + pgsin_b + pgsin_a);
4609 if (rbehind != NULL)
4613 return (zfs_vm_pagerret_ok);
4617 zfs_freebsd_getpages(ap)
4618 struct vop_getpages_args /* {
4627 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_rbehind,
4632 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
4635 znode_t *zp = VTOZ(vp);
4636 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4645 vm_ooffset_t lo_off;
4656 object = vp->v_object;
4660 KASSERT(ma[0]->object == object, ("mismatching object"));
4661 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
4663 for (i = 0; i < pcount; i++)
4664 rtvals[i] = zfs_vm_pagerret_error;
4666 off = IDX_TO_OFF(ma[0]->pindex);
4667 blksz = zp->z_blksz;
4668 lo_off = rounddown(off, blksz);
4669 lo_len = roundup(len + (off - lo_off), blksz);
4670 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
4672 zfs_vmobject_wlock(object);
4673 if (len + off > object->un_pager.vnp.vnp_size) {
4674 if (object->un_pager.vnp.vnp_size > off) {
4677 len = object->un_pager.vnp.vnp_size - off;
4679 if ((pgoff = (int)len & PAGE_MASK) != 0) {
4681 * If the object is locked and the following
4682 * conditions hold, then the page's dirty
4683 * field cannot be concurrently changed by a
4687 vm_page_assert_sbusied(m);
4688 KASSERT(!pmap_page_is_write_mapped(m),
4689 ("zfs_putpages: page %p is not read-only", m));
4690 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
4697 if (ncount < pcount) {
4698 for (i = ncount; i < pcount; i++) {
4699 rtvals[i] = zfs_vm_pagerret_bad;
4703 zfs_vmobject_wunlock(object);
4708 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4709 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4713 tx = dmu_tx_create(zfsvfs->z_os);
4714 dmu_tx_hold_write(tx, zp->z_id, off, len);
4716 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4717 zfs_sa_upgrade_txholds(tx, zp);
4718 err = dmu_tx_assign(tx, TXG_WAIT);
4724 if (zp->z_blksz < PAGE_SIZE) {
4725 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
4726 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
4727 va = zfs_map_page(ma[i], &sf);
4728 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
4732 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
4736 uint64_t mtime[2], ctime[2];
4737 sa_bulk_attr_t bulk[3];
4740 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4742 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4744 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4746 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4748 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4750 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4752 zfs_vmobject_wlock(object);
4753 for (i = 0; i < ncount; i++) {
4754 rtvals[i] = zfs_vm_pagerret_ok;
4755 vm_page_undirty(ma[i]);
4757 zfs_vmobject_wunlock(object);
4758 VM_CNT_INC(v_vnodeout);
4759 VM_CNT_ADD(v_vnodepgsout, ncount);
4764 zfs_range_unlock(rl);
4765 if ((flags & (zfs_vm_pagerput_sync | zfs_vm_pagerput_inval)) != 0 ||
4766 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4767 zil_commit(zfsvfs->z_log, zp->z_id);
4773 zfs_freebsd_putpages(ap)
4774 struct vop_putpages_args /* {
4783 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
4788 zfs_freebsd_bmap(ap)
4789 struct vop_bmap_args /* {
4792 struct bufobj **a_bop;
4799 if (ap->a_bop != NULL)
4800 *ap->a_bop = &ap->a_vp->v_bufobj;
4801 if (ap->a_bnp != NULL)
4802 *ap->a_bnp = ap->a_bn;
4803 if (ap->a_runp != NULL)
4805 if (ap->a_runb != NULL)
4812 zfs_freebsd_open(ap)
4813 struct vop_open_args /* {
4816 struct ucred *a_cred;
4817 struct thread *a_td;
4820 vnode_t *vp = ap->a_vp;
4821 znode_t *zp = VTOZ(vp);
4824 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
4826 vnode_create_vobject(vp, zp->z_size, ap->a_td);
4831 zfs_freebsd_close(ap)
4832 struct vop_close_args /* {
4835 struct ucred *a_cred;
4836 struct thread *a_td;
4840 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
4844 zfs_freebsd_ioctl(ap)
4845 struct vop_ioctl_args /* {
4855 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
4856 ap->a_fflag, ap->a_cred, NULL, NULL));
4860 ioflags(int ioflags)
4864 if (ioflags & IO_APPEND)
4866 if (ioflags & IO_NDELAY)
4868 if (ioflags & IO_SYNC)
4869 flags |= (FSYNC | FDSYNC | FRSYNC);
4875 zfs_freebsd_read(ap)
4876 struct vop_read_args /* {
4880 struct ucred *a_cred;
4884 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4889 zfs_freebsd_write(ap)
4890 struct vop_write_args /* {
4894 struct ucred *a_cred;
4898 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
4903 zfs_freebsd_access(ap)
4904 struct vop_access_args /* {
4906 accmode_t a_accmode;
4907 struct ucred *a_cred;
4908 struct thread *a_td;
4911 vnode_t *vp = ap->a_vp;
4912 znode_t *zp = VTOZ(vp);
4917 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
4919 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
4921 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
4924 * VADMIN has to be handled by vaccess().
4927 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
4929 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
4930 zp->z_gid, accmode, ap->a_cred, NULL);
4935 * For VEXEC, ensure that at least one execute bit is set for
4938 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
4939 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
4947 zfs_freebsd_lookup(ap)
4948 struct vop_lookup_args /* {
4949 struct vnode *a_dvp;
4950 struct vnode **a_vpp;
4951 struct componentname *a_cnp;
4954 struct componentname *cnp = ap->a_cnp;
4955 char nm[NAME_MAX + 1];
4957 ASSERT(cnp->cn_namelen < sizeof(nm));
4958 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
4960 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
4961 cnp->cn_cred, cnp->cn_thread, 0));
4965 zfs_cache_lookup(ap)
4966 struct vop_lookup_args /* {
4967 struct vnode *a_dvp;
4968 struct vnode **a_vpp;
4969 struct componentname *a_cnp;
4974 zfsvfs = ap->a_dvp->v_mount->mnt_data;
4975 if (zfsvfs->z_use_namecache)
4976 return (vfs_cache_lookup(ap));
4978 return (zfs_freebsd_lookup(ap));
4982 zfs_freebsd_create(ap)
4983 struct vop_create_args /* {
4984 struct vnode *a_dvp;
4985 struct vnode **a_vpp;
4986 struct componentname *a_cnp;
4987 struct vattr *a_vap;
4991 struct componentname *cnp = ap->a_cnp;
4992 vattr_t *vap = ap->a_vap;
4995 ASSERT(cnp->cn_flags & SAVENAME);
4997 vattr_init_mask(vap);
4998 mode = vap->va_mode & ALLPERMS;
4999 zfsvfs = ap->a_dvp->v_mount->mnt_data;
5001 error = zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
5002 ap->a_vpp, cnp->cn_cred, cnp->cn_thread);
5003 if (zfsvfs->z_use_namecache &&
5004 error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
5005 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
5010 zfs_freebsd_remove(ap)
5011 struct vop_remove_args /* {
5012 struct vnode *a_dvp;
5014 struct componentname *a_cnp;
5018 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5020 return (zfs_remove(ap->a_dvp, ap->a_vp, ap->a_cnp->cn_nameptr,
5021 ap->a_cnp->cn_cred));
5025 zfs_freebsd_mkdir(ap)
5026 struct vop_mkdir_args /* {
5027 struct vnode *a_dvp;
5028 struct vnode **a_vpp;
5029 struct componentname *a_cnp;
5030 struct vattr *a_vap;
5033 vattr_t *vap = ap->a_vap;
5035 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5037 vattr_init_mask(vap);
5039 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
5040 ap->a_cnp->cn_cred));
5044 zfs_freebsd_rmdir(ap)
5045 struct vop_rmdir_args /* {
5046 struct vnode *a_dvp;
5048 struct componentname *a_cnp;
5051 struct componentname *cnp = ap->a_cnp;
5053 ASSERT(cnp->cn_flags & SAVENAME);
5055 return (zfs_rmdir(ap->a_dvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred));
5059 zfs_freebsd_readdir(ap)
5060 struct vop_readdir_args /* {
5063 struct ucred *a_cred;
5070 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
5071 ap->a_ncookies, ap->a_cookies));
5075 zfs_freebsd_fsync(ap)
5076 struct vop_fsync_args /* {
5079 struct thread *a_td;
5084 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
5088 zfs_freebsd_getattr(ap)
5089 struct vop_getattr_args /* {
5091 struct vattr *a_vap;
5092 struct ucred *a_cred;
5095 vattr_t *vap = ap->a_vap;
5101 xvap.xva_vattr = *vap;
5102 xvap.xva_vattr.va_mask |= AT_XVATTR;
5104 /* Convert chflags into ZFS-type flags. */
5105 /* XXX: what about SF_SETTABLE?. */
5106 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
5107 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
5108 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
5109 XVA_SET_REQ(&xvap, XAT_NODUMP);
5110 XVA_SET_REQ(&xvap, XAT_READONLY);
5111 XVA_SET_REQ(&xvap, XAT_ARCHIVE);
5112 XVA_SET_REQ(&xvap, XAT_SYSTEM);
5113 XVA_SET_REQ(&xvap, XAT_HIDDEN);
5114 XVA_SET_REQ(&xvap, XAT_REPARSE);
5115 XVA_SET_REQ(&xvap, XAT_OFFLINE);
5116 XVA_SET_REQ(&xvap, XAT_SPARSE);
5118 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
5122 /* Convert ZFS xattr into chflags. */
5123 #define FLAG_CHECK(fflag, xflag, xfield) do { \
5124 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
5125 fflags |= (fflag); \
5127 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
5128 xvap.xva_xoptattrs.xoa_immutable);
5129 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
5130 xvap.xva_xoptattrs.xoa_appendonly);
5131 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
5132 xvap.xva_xoptattrs.xoa_nounlink);
5133 FLAG_CHECK(UF_ARCHIVE, XAT_ARCHIVE,
5134 xvap.xva_xoptattrs.xoa_archive);
5135 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
5136 xvap.xva_xoptattrs.xoa_nodump);
5137 FLAG_CHECK(UF_READONLY, XAT_READONLY,
5138 xvap.xva_xoptattrs.xoa_readonly);
5139 FLAG_CHECK(UF_SYSTEM, XAT_SYSTEM,
5140 xvap.xva_xoptattrs.xoa_system);
5141 FLAG_CHECK(UF_HIDDEN, XAT_HIDDEN,
5142 xvap.xva_xoptattrs.xoa_hidden);
5143 FLAG_CHECK(UF_REPARSE, XAT_REPARSE,
5144 xvap.xva_xoptattrs.xoa_reparse);
5145 FLAG_CHECK(UF_OFFLINE, XAT_OFFLINE,
5146 xvap.xva_xoptattrs.xoa_offline);
5147 FLAG_CHECK(UF_SPARSE, XAT_SPARSE,
5148 xvap.xva_xoptattrs.xoa_sparse);
5151 *vap = xvap.xva_vattr;
5152 vap->va_flags = fflags;
5157 zfs_freebsd_setattr(ap)
5158 struct vop_setattr_args /* {
5160 struct vattr *a_vap;
5161 struct ucred *a_cred;
5164 vnode_t *vp = ap->a_vp;
5165 vattr_t *vap = ap->a_vap;
5166 cred_t *cred = ap->a_cred;
5171 vattr_init_mask(vap);
5172 vap->va_mask &= ~AT_NOSET;
5175 xvap.xva_vattr = *vap;
5177 zflags = VTOZ(vp)->z_pflags;
5179 if (vap->va_flags != VNOVAL) {
5180 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
5183 if (zfsvfs->z_use_fuids == B_FALSE)
5184 return (EOPNOTSUPP);
5186 fflags = vap->va_flags;
5189 * We need to figure out whether it makes sense to allow
5190 * UF_REPARSE through, since we don't really have other
5191 * facilities to handle reparse points and zfs_setattr()
5192 * doesn't currently allow setting that attribute anyway.
5194 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_ARCHIVE|
5195 UF_NODUMP|UF_SYSTEM|UF_HIDDEN|UF_READONLY|UF_REPARSE|
5196 UF_OFFLINE|UF_SPARSE)) != 0)
5197 return (EOPNOTSUPP);
5199 * Unprivileged processes are not permitted to unset system
5200 * flags, or modify flags if any system flags are set.
5201 * Privileged non-jail processes may not modify system flags
5202 * if securelevel > 0 and any existing system flags are set.
5203 * Privileged jail processes behave like privileged non-jail
5204 * processes if the PR_ALLOW_CHFLAGS permission bit is set;
5205 * otherwise, they behave like unprivileged processes.
5207 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
5208 priv_check_cred(cred, PRIV_VFS_SYSFLAGS) == 0) {
5210 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5211 error = securelevel_gt(cred, 0);
5217 * Callers may only modify the file flags on objects they
5218 * have VADMIN rights for.
5220 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
5223 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5227 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
5232 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
5233 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
5234 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
5235 XVA_SET_REQ(&xvap, (xflag)); \
5236 (xfield) = ((fflags & (fflag)) != 0); \
5239 /* Convert chflags into ZFS-type flags. */
5240 /* XXX: what about SF_SETTABLE?. */
5241 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
5242 xvap.xva_xoptattrs.xoa_immutable);
5243 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
5244 xvap.xva_xoptattrs.xoa_appendonly);
5245 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
5246 xvap.xva_xoptattrs.xoa_nounlink);
5247 FLAG_CHANGE(UF_ARCHIVE, ZFS_ARCHIVE, XAT_ARCHIVE,
5248 xvap.xva_xoptattrs.xoa_archive);
5249 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
5250 xvap.xva_xoptattrs.xoa_nodump);
5251 FLAG_CHANGE(UF_READONLY, ZFS_READONLY, XAT_READONLY,
5252 xvap.xva_xoptattrs.xoa_readonly);
5253 FLAG_CHANGE(UF_SYSTEM, ZFS_SYSTEM, XAT_SYSTEM,
5254 xvap.xva_xoptattrs.xoa_system);
5255 FLAG_CHANGE(UF_HIDDEN, ZFS_HIDDEN, XAT_HIDDEN,
5256 xvap.xva_xoptattrs.xoa_hidden);
5257 FLAG_CHANGE(UF_REPARSE, ZFS_REPARSE, XAT_REPARSE,
5258 xvap.xva_xoptattrs.xoa_hidden);
5259 FLAG_CHANGE(UF_OFFLINE, ZFS_OFFLINE, XAT_OFFLINE,
5260 xvap.xva_xoptattrs.xoa_offline);
5261 FLAG_CHANGE(UF_SPARSE, ZFS_SPARSE, XAT_SPARSE,
5262 xvap.xva_xoptattrs.xoa_sparse);
5265 if (vap->va_birthtime.tv_sec != VNOVAL) {
5266 xvap.xva_vattr.va_mask |= AT_XVATTR;
5267 XVA_SET_REQ(&xvap, XAT_CREATETIME);
5269 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
5273 zfs_freebsd_rename(ap)
5274 struct vop_rename_args /* {
5275 struct vnode *a_fdvp;
5276 struct vnode *a_fvp;
5277 struct componentname *a_fcnp;
5278 struct vnode *a_tdvp;
5279 struct vnode *a_tvp;
5280 struct componentname *a_tcnp;
5283 vnode_t *fdvp = ap->a_fdvp;
5284 vnode_t *fvp = ap->a_fvp;
5285 vnode_t *tdvp = ap->a_tdvp;
5286 vnode_t *tvp = ap->a_tvp;
5289 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
5290 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
5292 error = zfs_rename(fdvp, &fvp, ap->a_fcnp, tdvp, &tvp,
5293 ap->a_tcnp, ap->a_fcnp->cn_cred);
5305 zfs_freebsd_symlink(ap)
5306 struct vop_symlink_args /* {
5307 struct vnode *a_dvp;
5308 struct vnode **a_vpp;
5309 struct componentname *a_cnp;
5310 struct vattr *a_vap;
5314 struct componentname *cnp = ap->a_cnp;
5315 vattr_t *vap = ap->a_vap;
5317 ASSERT(cnp->cn_flags & SAVENAME);
5319 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
5320 vattr_init_mask(vap);
5322 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
5323 __DECONST(char *, ap->a_target), cnp->cn_cred, cnp->cn_thread));
5327 zfs_freebsd_readlink(ap)
5328 struct vop_readlink_args /* {
5331 struct ucred *a_cred;
5335 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
5339 zfs_freebsd_link(ap)
5340 struct vop_link_args /* {
5341 struct vnode *a_tdvp;
5343 struct componentname *a_cnp;
5346 struct componentname *cnp = ap->a_cnp;
5347 vnode_t *vp = ap->a_vp;
5348 vnode_t *tdvp = ap->a_tdvp;
5350 if (tdvp->v_mount != vp->v_mount)
5353 ASSERT(cnp->cn_flags & SAVENAME);
5355 return (zfs_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
5359 zfs_freebsd_inactive(ap)
5360 struct vop_inactive_args /* {
5362 struct thread *a_td;
5365 vnode_t *vp = ap->a_vp;
5367 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
5372 zfs_freebsd_reclaim(ap)
5373 struct vop_reclaim_args /* {
5375 struct thread *a_td;
5378 vnode_t *vp = ap->a_vp;
5379 znode_t *zp = VTOZ(vp);
5380 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5384 /* Destroy the vm object and flush associated pages. */
5385 vnode_destroy_vobject(vp);
5388 * z_teardown_inactive_lock protects from a race with
5389 * zfs_znode_dmu_fini in zfsvfs_teardown during
5392 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
5393 if (zp->z_sa_hdl == NULL)
5397 rw_exit(&zfsvfs->z_teardown_inactive_lock);
5405 struct vop_fid_args /* {
5411 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
5415 zfs_freebsd_pathconf(ap)
5416 struct vop_pathconf_args /* {
5419 register_t *a_retval;
5425 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
5427 *ap->a_retval = val;
5430 if (error != EOPNOTSUPP)
5433 switch (ap->a_name) {
5435 *ap->a_retval = NAME_MAX;
5438 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO) {
5439 *ap->a_retval = PIPE_BUF;
5444 return (vop_stdpathconf(ap));
5449 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
5450 * extended attribute name:
5453 * system freebsd:system:
5454 * user (none, can be used to access ZFS fsattr(5) attributes
5455 * created on Solaris)
5458 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
5461 const char *namespace, *prefix, *suffix;
5463 /* We don't allow '/' character in attribute name. */
5464 if (strchr(name, '/') != NULL)
5466 /* We don't allow attribute names that start with "freebsd:" string. */
5467 if (strncmp(name, "freebsd:", 8) == 0)
5470 bzero(attrname, size);
5472 switch (attrnamespace) {
5473 case EXTATTR_NAMESPACE_USER:
5475 prefix = "freebsd:";
5476 namespace = EXTATTR_NAMESPACE_USER_STRING;
5480 * This is the default namespace by which we can access all
5481 * attributes created on Solaris.
5483 prefix = namespace = suffix = "";
5486 case EXTATTR_NAMESPACE_SYSTEM:
5487 prefix = "freebsd:";
5488 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
5491 case EXTATTR_NAMESPACE_EMPTY:
5495 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
5497 return (ENAMETOOLONG);
5503 * Vnode operating to retrieve a named extended attribute.
5506 zfs_getextattr(struct vop_getextattr_args *ap)
5509 IN struct vnode *a_vp;
5510 IN int a_attrnamespace;
5511 IN const char *a_name;
5512 INOUT struct uio *a_uio;
5514 IN struct ucred *a_cred;
5515 IN struct thread *a_td;
5519 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5520 struct thread *td = ap->a_td;
5521 struct nameidata nd;
5524 vnode_t *xvp = NULL, *vp;
5527 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5528 ap->a_cred, ap->a_td, VREAD);
5532 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5539 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5547 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5549 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
5551 NDFREE(&nd, NDF_ONLY_PNBUF);
5554 if (error == ENOENT)
5559 if (ap->a_size != NULL) {
5560 error = VOP_GETATTR(vp, &va, ap->a_cred);
5562 *ap->a_size = (size_t)va.va_size;
5563 } else if (ap->a_uio != NULL)
5564 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5567 vn_close(vp, flags, ap->a_cred, td);
5574 * Vnode operation to remove a named attribute.
5577 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
5580 IN struct vnode *a_vp;
5581 IN int a_attrnamespace;
5582 IN const char *a_name;
5583 IN struct ucred *a_cred;
5584 IN struct thread *a_td;
5588 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5589 struct thread *td = ap->a_td;
5590 struct nameidata nd;
5593 vnode_t *xvp = NULL, *vp;
5596 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5597 ap->a_cred, ap->a_td, VWRITE);
5601 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5608 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5615 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF,
5616 UIO_SYSSPACE, attrname, xvp, td);
5621 NDFREE(&nd, NDF_ONLY_PNBUF);
5622 if (error == ENOENT)
5627 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
5628 NDFREE(&nd, NDF_ONLY_PNBUF);
5631 if (vp == nd.ni_dvp)
5641 * Vnode operation to set a named attribute.
5644 zfs_setextattr(struct vop_setextattr_args *ap)
5647 IN struct vnode *a_vp;
5648 IN int a_attrnamespace;
5649 IN const char *a_name;
5650 INOUT struct uio *a_uio;
5651 IN struct ucred *a_cred;
5652 IN struct thread *a_td;
5656 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5657 struct thread *td = ap->a_td;
5658 struct nameidata nd;
5661 vnode_t *xvp = NULL, *vp;
5664 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5665 ap->a_cred, ap->a_td, VWRITE);
5669 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
5676 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5677 LOOKUP_XATTR | CREATE_XATTR_DIR);
5683 flags = FFLAGS(O_WRONLY | O_CREAT);
5684 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, attrname,
5686 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
5688 NDFREE(&nd, NDF_ONLY_PNBUF);
5696 error = VOP_SETATTR(vp, &va, ap->a_cred);
5698 VOP_WRITE(vp, ap->a_uio, IO_UNIT, ap->a_cred);
5701 vn_close(vp, flags, ap->a_cred, td);
5708 * Vnode operation to retrieve extended attributes on a vnode.
5711 zfs_listextattr(struct vop_listextattr_args *ap)
5714 IN struct vnode *a_vp;
5715 IN int a_attrnamespace;
5716 INOUT struct uio *a_uio;
5718 IN struct ucred *a_cred;
5719 IN struct thread *a_td;
5723 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
5724 struct thread *td = ap->a_td;
5725 struct nameidata nd;
5726 char attrprefix[16];
5727 u_char dirbuf[sizeof(struct dirent)];
5730 struct uio auio, *uio = ap->a_uio;
5731 size_t *sizep = ap->a_size;
5733 vnode_t *xvp = NULL, *vp;
5734 int done, error, eof, pos;
5736 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
5737 ap->a_cred, ap->a_td, VREAD);
5741 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
5742 sizeof(attrprefix));
5745 plen = strlen(attrprefix);
5752 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
5757 * ENOATTR means that the EA directory does not yet exist,
5758 * i.e. there are no extended attributes there.
5760 if (error == ENOATTR)
5765 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED,
5766 UIO_SYSSPACE, ".", xvp, td);
5769 NDFREE(&nd, NDF_ONLY_PNBUF);
5775 auio.uio_iov = &aiov;
5776 auio.uio_iovcnt = 1;
5777 auio.uio_segflg = UIO_SYSSPACE;
5779 auio.uio_rw = UIO_READ;
5780 auio.uio_offset = 0;
5785 aiov.iov_base = (void *)dirbuf;
5786 aiov.iov_len = sizeof(dirbuf);
5787 auio.uio_resid = sizeof(dirbuf);
5788 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
5789 done = sizeof(dirbuf) - auio.uio_resid;
5792 for (pos = 0; pos < done;) {
5793 dp = (struct dirent *)(dirbuf + pos);
5794 pos += dp->d_reclen;
5796 * XXX: Temporarily we also accept DT_UNKNOWN, as this
5797 * is what we get when attribute was created on Solaris.
5799 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
5801 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
5803 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
5805 nlen = dp->d_namlen - plen;
5808 else if (uio != NULL) {
5810 * Format of extattr name entry is one byte for
5811 * length and the rest for name.
5813 error = uiomove(&nlen, 1, uio->uio_rw, uio);
5815 error = uiomove(dp->d_name + plen, nlen,
5822 } while (!eof && error == 0);
5831 zfs_freebsd_getacl(ap)
5832 struct vop_getacl_args /* {
5841 vsecattr_t vsecattr;
5843 if (ap->a_type != ACL_TYPE_NFS4)
5846 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
5847 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
5850 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
5851 if (vsecattr.vsa_aclentp != NULL)
5852 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
5858 zfs_freebsd_setacl(ap)
5859 struct vop_setacl_args /* {
5868 vsecattr_t vsecattr;
5869 int aclbsize; /* size of acl list in bytes */
5872 if (ap->a_type != ACL_TYPE_NFS4)
5875 if (ap->a_aclp == NULL)
5878 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
5882 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
5883 * splitting every entry into two and appending "canonical six"
5884 * entries at the end. Don't allow for setting an ACL that would
5885 * cause chmod(2) to run out of ACL entries.
5887 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
5890 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
5894 vsecattr.vsa_mask = VSA_ACE;
5895 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
5896 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
5897 aaclp = vsecattr.vsa_aclentp;
5898 vsecattr.vsa_aclentsz = aclbsize;
5900 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
5901 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
5902 kmem_free(aaclp, aclbsize);
5908 zfs_freebsd_aclcheck(ap)
5909 struct vop_aclcheck_args /* {
5918 return (EOPNOTSUPP);
5922 zfs_vptocnp(struct vop_vptocnp_args *ap)
5924 vnode_t *covered_vp;
5925 vnode_t *vp = ap->a_vp;;
5926 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
5927 znode_t *zp = VTOZ(vp);
5935 * If we are a snapshot mounted under .zfs, run the operation
5936 * on the covered vnode.
5938 if (zp->z_id != zfsvfs->z_root || zfsvfs->z_parent == zfsvfs) {
5939 char name[MAXNAMLEN + 1];
5943 error = zfs_znode_parent_and_name(zp, &dzp, name);
5946 if (*ap->a_buflen < len)
5947 error = SET_ERROR(ENOMEM);
5950 *ap->a_buflen -= len;
5951 bcopy(name, ap->a_buf + *ap->a_buflen, len);
5952 *ap->a_vpp = ZTOV(dzp);
5959 covered_vp = vp->v_mount->mnt_vnodecovered;
5961 ltype = VOP_ISLOCKED(vp);
5963 error = vget(covered_vp, LK_SHARED | LK_VNHELD, curthread);
5965 error = VOP_VPTOCNP(covered_vp, ap->a_vpp, ap->a_cred,
5966 ap->a_buf, ap->a_buflen);
5969 vn_lock(vp, ltype | LK_RETRY);
5970 if ((vp->v_iflag & VI_DOOMED) != 0)
5971 error = SET_ERROR(ENOENT);
5978 struct vop_lock1_args /* {
5989 err = vop_stdlock(ap);
5990 if (err == 0 && (ap->a_flags & LK_NOWAIT) == 0) {
5993 if (vp->v_mount != NULL && (vp->v_iflag & VI_DOOMED) == 0 &&
5994 zp != NULL && (zp->z_pflags & ZFS_XATTR) == 0)
5995 VERIFY(!RRM_LOCK_HELD(&zp->z_zfsvfs->z_teardown_lock));
6001 struct vop_vector zfs_vnodeops;
6002 struct vop_vector zfs_fifoops;
6003 struct vop_vector zfs_shareops;
6005 struct vop_vector zfs_vnodeops = {
6006 .vop_default = &default_vnodeops,
6007 .vop_inactive = zfs_freebsd_inactive,
6008 .vop_reclaim = zfs_freebsd_reclaim,
6009 .vop_access = zfs_freebsd_access,
6010 .vop_allocate = VOP_EINVAL,
6011 .vop_lookup = zfs_cache_lookup,
6012 .vop_cachedlookup = zfs_freebsd_lookup,
6013 .vop_getattr = zfs_freebsd_getattr,
6014 .vop_setattr = zfs_freebsd_setattr,
6015 .vop_create = zfs_freebsd_create,
6016 .vop_mknod = zfs_freebsd_create,
6017 .vop_mkdir = zfs_freebsd_mkdir,
6018 .vop_readdir = zfs_freebsd_readdir,
6019 .vop_fsync = zfs_freebsd_fsync,
6020 .vop_open = zfs_freebsd_open,
6021 .vop_close = zfs_freebsd_close,
6022 .vop_rmdir = zfs_freebsd_rmdir,
6023 .vop_ioctl = zfs_freebsd_ioctl,
6024 .vop_link = zfs_freebsd_link,
6025 .vop_symlink = zfs_freebsd_symlink,
6026 .vop_readlink = zfs_freebsd_readlink,
6027 .vop_read = zfs_freebsd_read,
6028 .vop_write = zfs_freebsd_write,
6029 .vop_remove = zfs_freebsd_remove,
6030 .vop_rename = zfs_freebsd_rename,
6031 .vop_pathconf = zfs_freebsd_pathconf,
6032 .vop_bmap = zfs_freebsd_bmap,
6033 .vop_fid = zfs_freebsd_fid,
6034 .vop_getextattr = zfs_getextattr,
6035 .vop_deleteextattr = zfs_deleteextattr,
6036 .vop_setextattr = zfs_setextattr,
6037 .vop_listextattr = zfs_listextattr,
6038 .vop_getacl = zfs_freebsd_getacl,
6039 .vop_setacl = zfs_freebsd_setacl,
6040 .vop_aclcheck = zfs_freebsd_aclcheck,
6041 .vop_getpages = zfs_freebsd_getpages,
6042 .vop_putpages = zfs_freebsd_putpages,
6043 .vop_vptocnp = zfs_vptocnp,
6045 .vop_lock1 = zfs_lock,
6049 struct vop_vector zfs_fifoops = {
6050 .vop_default = &fifo_specops,
6051 .vop_fsync = zfs_freebsd_fsync,
6052 .vop_access = zfs_freebsd_access,
6053 .vop_getattr = zfs_freebsd_getattr,
6054 .vop_inactive = zfs_freebsd_inactive,
6055 .vop_read = VOP_PANIC,
6056 .vop_reclaim = zfs_freebsd_reclaim,
6057 .vop_setattr = zfs_freebsd_setattr,
6058 .vop_write = VOP_PANIC,
6059 .vop_pathconf = zfs_freebsd_pathconf,
6060 .vop_fid = zfs_freebsd_fid,
6061 .vop_getacl = zfs_freebsd_getacl,
6062 .vop_setacl = zfs_freebsd_setacl,
6063 .vop_aclcheck = zfs_freebsd_aclcheck,
6067 * special share hidden files vnode operations template
6069 struct vop_vector zfs_shareops = {
6070 .vop_default = &default_vnodeops,
6071 .vop_access = zfs_freebsd_access,
6072 .vop_inactive = zfs_freebsd_inactive,
6073 .vop_reclaim = zfs_freebsd_reclaim,
6074 .vop_fid = zfs_freebsd_fid,
6075 .vop_pathconf = zfs_freebsd_pathconf,