4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
27 /* Portions Copyright 2010 Robert Milkowski */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
37 #include <sys/vnode.h>
41 #include <sys/taskq.h>
43 #include <sys/atomic.h>
44 #include <sys/namei.h>
46 #include <sys/cmn_err.h>
47 #include <sys/errno.h>
48 #include <sys/unistd.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/fs/zfs.h>
53 #include <sys/dmu_objset.h>
59 #include <sys/dirent.h>
60 #include <sys/policy.h>
61 #include <sys/sunddi.h>
62 #include <sys/filio.h>
64 #include <sys/zfs_ctldir.h>
65 #include <sys/zfs_fuid.h>
66 #include <sys/zfs_sa.h>
68 #include <sys/zfs_rlock.h>
69 #include <sys/extdirent.h>
70 #include <sys/kidmap.h>
73 #include <sys/sched.h>
75 #include <vm/vm_pageout.h>
80 * Each vnode op performs some logical unit of work. To do this, the ZPL must
81 * properly lock its in-core state, create a DMU transaction, do the work,
82 * record this work in the intent log (ZIL), commit the DMU transaction,
83 * and wait for the intent log to commit if it is a synchronous operation.
84 * Moreover, the vnode ops must work in both normal and log replay context.
85 * The ordering of events is important to avoid deadlocks and references
86 * to freed memory. The example below illustrates the following Big Rules:
88 * (1) A check must be made in each zfs thread for a mounted file system.
89 * This is done avoiding races using ZFS_ENTER(zfsvfs).
90 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
91 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
92 * can return EIO from the calling function.
94 * (2) VN_RELE() should always be the last thing except for zil_commit()
95 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
96 * First, if it's the last reference, the vnode/znode
97 * can be freed, so the zp may point to freed memory. Second, the last
98 * reference will call zfs_zinactive(), which may induce a lot of work --
99 * pushing cached pages (which acquires range locks) and syncing out
100 * cached atime changes. Third, zfs_zinactive() may require a new tx,
101 * which could deadlock the system if you were already holding one.
102 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
104 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
105 * as they can span dmu_tx_assign() calls.
107 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
108 * dmu_tx_assign(). This is critical because we don't want to block
109 * while holding locks.
111 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
112 * reduces lock contention and CPU usage when we must wait (note that if
113 * throughput is constrained by the storage, nearly every transaction
116 * Note, in particular, that if a lock is sometimes acquired before
117 * the tx assigns, and sometimes after (e.g. z_lock), then failing
118 * to use a non-blocking assign can deadlock the system. The scenario:
120 * Thread A has grabbed a lock before calling dmu_tx_assign().
121 * Thread B is in an already-assigned tx, and blocks for this lock.
122 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
123 * forever, because the previous txg can't quiesce until B's tx commits.
125 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
126 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
127 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
128 * to indicate that this operation has already called dmu_tx_wait().
129 * This will ensure that we don't retry forever, waiting a short bit
132 * (5) If the operation succeeded, generate the intent log entry for it
133 * before dropping locks. This ensures that the ordering of events
134 * in the intent log matches the order in which they actually occurred.
135 * During ZIL replay the zfs_log_* functions will update the sequence
136 * number to indicate the zil transaction has replayed.
138 * (6) At the end of each vnode op, the DMU tx must always commit,
139 * regardless of whether there were any errors.
141 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
142 * to ensure that synchronous semantics are provided when necessary.
144 * In general, this is how things should be ordered in each vnode op:
146 * ZFS_ENTER(zfsvfs); // exit if unmounted
148 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
149 * rw_enter(...); // grab any other locks you need
150 * tx = dmu_tx_create(...); // get DMU tx
151 * dmu_tx_hold_*(); // hold each object you might modify
152 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
154 * rw_exit(...); // drop locks
155 * zfs_dirent_unlock(dl); // unlock directory entry
156 * VN_RELE(...); // release held vnodes
157 * if (error == ERESTART) {
163 * dmu_tx_abort(tx); // abort DMU tx
164 * ZFS_EXIT(zfsvfs); // finished in zfs
165 * return (error); // really out of space
167 * error = do_real_work(); // do whatever this VOP does
169 * zfs_log_*(...); // on success, make ZIL entry
170 * dmu_tx_commit(tx); // commit DMU tx -- error or not
171 * rw_exit(...); // drop locks
172 * zfs_dirent_unlock(dl); // unlock directory entry
173 * VN_RELE(...); // release held vnodes
174 * zil_commit(zilog, foid); // synchronous when necessary
175 * ZFS_EXIT(zfsvfs); // finished in zfs
176 * return (error); // done, report error
181 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
183 znode_t *zp = VTOZ(*vpp);
184 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
189 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
190 ((flag & FAPPEND) == 0)) {
192 return (SET_ERROR(EPERM));
195 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
196 ZTOV(zp)->v_type == VREG &&
197 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
198 if (fs_vscan(*vpp, cr, 0) != 0) {
200 return (SET_ERROR(EACCES));
204 /* Keep a count of the synchronous opens in the znode */
205 if (flag & (FSYNC | FDSYNC))
206 atomic_inc_32(&zp->z_sync_cnt);
214 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
215 caller_context_t *ct)
217 znode_t *zp = VTOZ(vp);
218 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
221 * Clean up any locks held by this process on the vp.
223 cleanlocks(vp, ddi_get_pid(), 0);
224 cleanshares(vp, ddi_get_pid());
229 /* Decrement the synchronous opens in the znode */
230 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
231 atomic_dec_32(&zp->z_sync_cnt);
233 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
234 ZTOV(zp)->v_type == VREG &&
235 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
236 VERIFY(fs_vscan(vp, cr, 1) == 0);
243 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
244 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
247 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
249 znode_t *zp = VTOZ(vp);
250 uint64_t noff = (uint64_t)*off; /* new offset */
255 file_sz = zp->z_size;
256 if (noff >= file_sz) {
257 return (SET_ERROR(ENXIO));
260 if (cmd == _FIO_SEEK_HOLE)
265 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
268 if ((error == ESRCH) || (noff > file_sz)) {
270 * Handle the virtual hole at the end of file.
276 return (SET_ERROR(ENXIO));
287 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
288 int *rvalp, caller_context_t *ct)
300 * The following two ioctls are used by bfu. Faking out,
301 * necessary to avoid bfu errors.
310 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
311 return (SET_ERROR(EFAULT));
313 off = *(offset_t *)data;
316 zfsvfs = zp->z_zfsvfs;
320 /* offset parameter is in/out */
321 error = zfs_holey(vp, com, &off);
326 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
327 return (SET_ERROR(EFAULT));
329 *(offset_t *)data = off;
333 return (SET_ERROR(ENOTTY));
337 page_busy(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
344 * At present vm_page_clear_dirty extends the cleared range to DEV_BSIZE
345 * aligned boundaries, if the range is not aligned. As a result a
346 * DEV_BSIZE subrange with partially dirty data may get marked as clean.
347 * It may happen that all DEV_BSIZE subranges are marked clean and thus
348 * the whole page would be considred clean despite have some dirty data.
349 * For this reason we should shrink the range to DEV_BSIZE aligned
350 * boundaries before calling vm_page_clear_dirty.
352 end = rounddown2(off + nbytes, DEV_BSIZE);
353 off = roundup2(off, DEV_BSIZE);
357 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
360 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
362 if ((pp->oflags & VPO_BUSY) != 0) {
364 * Reference the page before unlocking and
365 * sleeping so that the page daemon is less
366 * likely to reclaim it.
368 vm_page_reference(pp);
369 vm_page_sleep(pp, "zfsmwb");
372 } else if (pp == NULL) {
373 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
374 VM_ALLOC_SYSTEM | VM_ALLOC_IFCACHED |
377 ASSERT(pp != NULL && !pp->valid);
382 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
383 vm_object_pip_add(obj, 1);
384 vm_page_io_start(pp);
385 pmap_remove_write(pp);
387 vm_page_clear_dirty(pp, off, nbytes);
395 page_unbusy(vm_page_t pp)
398 vm_page_io_finish(pp);
399 vm_object_pip_subtract(pp->object, 1);
403 page_hold(vnode_t *vp, int64_t start)
409 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
412 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
414 if ((pp->oflags & VPO_BUSY) != 0) {
416 * Reference the page before unlocking and
417 * sleeping so that the page daemon is less
418 * likely to reclaim it.
420 vm_page_reference(pp);
421 vm_page_sleep(pp, "zfsmwb");
425 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
438 page_unhold(vm_page_t pp)
447 * When a file is memory mapped, we must keep the IO data synchronized
448 * between the DMU cache and the memory mapped pages. What this means:
450 * On Write: If we find a memory mapped page, we write to *both*
451 * the page and the dmu buffer.
454 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
455 int segflg, dmu_tx_t *tx)
462 ASSERT(segflg != UIO_NOCOPY);
463 ASSERT(vp->v_mount != NULL);
467 off = start & PAGEOFFSET;
469 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
471 int nbytes = imin(PAGESIZE - off, len);
473 if ((pp = page_busy(vp, start, off, nbytes)) != NULL) {
474 VM_OBJECT_UNLOCK(obj);
476 va = zfs_map_page(pp, &sf);
477 (void) dmu_read(os, oid, start+off, nbytes,
478 va+off, DMU_READ_PREFETCH);;
487 vm_object_pip_wakeupn(obj, 0);
488 VM_OBJECT_UNLOCK(obj);
492 * Read with UIO_NOCOPY flag means that sendfile(2) requests
493 * ZFS to populate a range of page cache pages with data.
495 * NOTE: this function could be optimized to pre-allocate
496 * all pages in advance, drain VPO_BUSY on all of them,
497 * map them into contiguous KVA region and populate them
498 * in one single dmu_read() call.
501 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
503 znode_t *zp = VTOZ(vp);
504 objset_t *os = zp->z_zfsvfs->z_os;
514 ASSERT(uio->uio_segflg == UIO_NOCOPY);
515 ASSERT(vp->v_mount != NULL);
518 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
521 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
522 int bytes = MIN(PAGESIZE, len);
524 pp = vm_page_grab(obj, OFF_TO_IDX(start), VM_ALLOC_NOBUSY |
525 VM_ALLOC_NORMAL | VM_ALLOC_RETRY | VM_ALLOC_IGN_SBUSY);
526 if (pp->valid == 0) {
527 vm_page_io_start(pp);
528 VM_OBJECT_UNLOCK(obj);
529 va = zfs_map_page(pp, &sf);
530 error = dmu_read(os, zp->z_id, start, bytes, va,
532 if (bytes != PAGESIZE && error == 0)
533 bzero(va + bytes, PAGESIZE - bytes);
536 vm_page_io_finish(pp);
539 if (pp->wire_count == 0 && pp->valid == 0 &&
540 pp->busy == 0 && !(pp->oflags & VPO_BUSY))
543 pp->valid = VM_PAGE_BITS_ALL;
544 vm_page_activate(pp);
548 ASSERT3U(pp->valid, ==, VM_PAGE_BITS_ALL);
552 uio->uio_resid -= bytes;
553 uio->uio_offset += bytes;
556 VM_OBJECT_UNLOCK(obj);
561 * When a file is memory mapped, we must keep the IO data synchronized
562 * between the DMU cache and the memory mapped pages. What this means:
564 * On Read: We "read" preferentially from memory mapped pages,
565 * else we default from the dmu buffer.
567 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
568 * the file is memory mapped.
571 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
573 znode_t *zp = VTOZ(vp);
574 objset_t *os = zp->z_zfsvfs->z_os;
582 ASSERT(vp->v_mount != NULL);
586 start = uio->uio_loffset;
587 off = start & PAGEOFFSET;
589 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
591 uint64_t bytes = MIN(PAGESIZE - off, len);
593 if (pp = page_hold(vp, start)) {
597 VM_OBJECT_UNLOCK(obj);
598 va = zfs_map_page(pp, &sf);
599 error = uiomove(va + off, bytes, UIO_READ, uio);
604 VM_OBJECT_UNLOCK(obj);
605 error = dmu_read_uio(os, zp->z_id, uio, bytes);
613 VM_OBJECT_UNLOCK(obj);
617 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
620 * Read bytes from specified file into supplied buffer.
622 * IN: vp - vnode of file to be read from.
623 * uio - structure supplying read location, range info,
625 * ioflag - SYNC flags; used to provide FRSYNC semantics.
626 * cr - credentials of caller.
627 * ct - caller context
629 * OUT: uio - updated offset and range, buffer filled.
631 * RETURN: 0 on success, error code on failure.
634 * vp - atime updated if byte count > 0
638 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
640 znode_t *zp = VTOZ(vp);
641 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
652 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
654 return (SET_ERROR(EACCES));
658 * Validate file offset
660 if (uio->uio_loffset < (offset_t)0) {
662 return (SET_ERROR(EINVAL));
666 * Fasttrack empty reads
668 if (uio->uio_resid == 0) {
674 * Check for mandatory locks
676 if (MANDMODE(zp->z_mode)) {
677 if (error = chklock(vp, FREAD,
678 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
685 * If we're in FRSYNC mode, sync out this znode before reading it.
688 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
689 zil_commit(zfsvfs->z_log, zp->z_id);
692 * Lock the range against changes.
694 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
697 * If we are reading past end-of-file we can skip
698 * to the end; but we might still need to set atime.
700 if (uio->uio_loffset >= zp->z_size) {
705 ASSERT(uio->uio_loffset < zp->z_size);
706 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
709 if ((uio->uio_extflg == UIO_XUIO) &&
710 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
712 int blksz = zp->z_blksz;
713 uint64_t offset = uio->uio_loffset;
715 xuio = (xuio_t *)uio;
717 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
720 ASSERT(offset + n <= blksz);
723 (void) dmu_xuio_init(xuio, nblk);
725 if (vn_has_cached_data(vp)) {
727 * For simplicity, we always allocate a full buffer
728 * even if we only expect to read a portion of a block.
730 while (--nblk >= 0) {
731 (void) dmu_xuio_add(xuio,
732 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
740 nbytes = MIN(n, zfs_read_chunk_size -
741 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
744 if (uio->uio_segflg == UIO_NOCOPY)
745 error = mappedread_sf(vp, nbytes, uio);
747 #endif /* __FreeBSD__ */
748 if (vn_has_cached_data(vp))
749 error = mappedread(vp, nbytes, uio);
751 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
753 /* convert checksum errors into IO errors */
755 error = SET_ERROR(EIO);
762 zfs_range_unlock(rl);
764 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
770 * Write the bytes to a file.
772 * IN: vp - vnode of file to be written to.
773 * uio - structure supplying write location, range info,
775 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
776 * set if in append mode.
777 * cr - credentials of caller.
778 * ct - caller context (NFS/CIFS fem monitor only)
780 * OUT: uio - updated offset and range.
782 * RETURN: 0 on success, error code on failure.
785 * vp - ctime|mtime updated if byte count > 0
790 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
792 znode_t *zp = VTOZ(vp);
793 rlim64_t limit = MAXOFFSET_T;
794 ssize_t start_resid = uio->uio_resid;
798 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
803 int max_blksz = zfsvfs->z_max_blksz;
806 iovec_t *aiov = NULL;
809 int iovcnt = uio->uio_iovcnt;
810 iovec_t *iovp = uio->uio_iov;
813 sa_bulk_attr_t bulk[4];
814 uint64_t mtime[2], ctime[2];
817 * Fasttrack empty write
823 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
829 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
830 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
831 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
833 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
837 * If immutable or not appending then return EPERM
839 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
840 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
841 (uio->uio_loffset < zp->z_size))) {
843 return (SET_ERROR(EPERM));
846 zilog = zfsvfs->z_log;
849 * Validate file offset
851 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
854 return (SET_ERROR(EINVAL));
858 * Check for mandatory locks before calling zfs_range_lock()
859 * in order to prevent a deadlock with locks set via fcntl().
861 if (MANDMODE((mode_t)zp->z_mode) &&
862 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
869 * Pre-fault the pages to ensure slow (eg NFS) pages
871 * Skip this if uio contains loaned arc_buf.
873 if ((uio->uio_extflg == UIO_XUIO) &&
874 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
875 xuio = (xuio_t *)uio;
877 uio_prefaultpages(MIN(n, max_blksz), uio);
881 * If in append mode, set the io offset pointer to eof.
883 if (ioflag & FAPPEND) {
885 * Obtain an appending range lock to guarantee file append
886 * semantics. We reset the write offset once we have the lock.
888 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
890 if (rl->r_len == UINT64_MAX) {
892 * We overlocked the file because this write will cause
893 * the file block size to increase.
894 * Note that zp_size cannot change with this lock held.
898 uio->uio_loffset = woff;
901 * Note that if the file block size will change as a result of
902 * this write, then this range lock will lock the entire file
903 * so that we can re-write the block safely.
905 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
908 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) {
909 zfs_range_unlock(rl);
915 zfs_range_unlock(rl);
917 return (SET_ERROR(EFBIG));
920 if ((woff + n) > limit || woff > (limit - n))
923 /* Will this write extend the file length? */
924 write_eof = (woff + n > zp->z_size);
926 end_size = MAX(zp->z_size, woff + n);
929 * Write the file in reasonable size chunks. Each chunk is written
930 * in a separate transaction; this keeps the intent log records small
931 * and allows us to do more fine-grained space accounting.
935 woff = uio->uio_loffset;
936 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
937 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
939 dmu_return_arcbuf(abuf);
940 error = SET_ERROR(EDQUOT);
944 if (xuio && abuf == NULL) {
945 ASSERT(i_iov < iovcnt);
947 abuf = dmu_xuio_arcbuf(xuio, i_iov);
948 dmu_xuio_clear(xuio, i_iov);
949 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
950 iovec_t *, aiov, arc_buf_t *, abuf);
951 ASSERT((aiov->iov_base == abuf->b_data) ||
952 ((char *)aiov->iov_base - (char *)abuf->b_data +
953 aiov->iov_len == arc_buf_size(abuf)));
955 } else if (abuf == NULL && n >= max_blksz &&
956 woff >= zp->z_size &&
957 P2PHASE(woff, max_blksz) == 0 &&
958 zp->z_blksz == max_blksz) {
960 * This write covers a full block. "Borrow" a buffer
961 * from the dmu so that we can fill it before we enter
962 * a transaction. This avoids the possibility of
963 * holding up the transaction if the data copy hangs
964 * up on a pagefault (e.g., from an NFS server mapping).
968 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
970 ASSERT(abuf != NULL);
971 ASSERT(arc_buf_size(abuf) == max_blksz);
972 if (error = uiocopy(abuf->b_data, max_blksz,
973 UIO_WRITE, uio, &cbytes)) {
974 dmu_return_arcbuf(abuf);
977 ASSERT(cbytes == max_blksz);
981 * Start a transaction.
983 tx = dmu_tx_create(zfsvfs->z_os);
984 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
985 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
986 zfs_sa_upgrade_txholds(tx, zp);
987 error = dmu_tx_assign(tx, TXG_WAIT);
991 dmu_return_arcbuf(abuf);
996 * If zfs_range_lock() over-locked we grow the blocksize
997 * and then reduce the lock range. This will only happen
998 * on the first iteration since zfs_range_reduce() will
999 * shrink down r_len to the appropriate size.
1001 if (rl->r_len == UINT64_MAX) {
1004 if (zp->z_blksz > max_blksz) {
1005 ASSERT(!ISP2(zp->z_blksz));
1006 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
1008 new_blksz = MIN(end_size, max_blksz);
1010 zfs_grow_blocksize(zp, new_blksz, tx);
1011 zfs_range_reduce(rl, woff, n);
1015 * XXX - should we really limit each write to z_max_blksz?
1016 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
1018 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
1020 if (woff + nbytes > zp->z_size)
1021 vnode_pager_setsize(vp, woff + nbytes);
1024 tx_bytes = uio->uio_resid;
1025 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
1027 tx_bytes -= uio->uio_resid;
1030 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
1032 * If this is not a full block write, but we are
1033 * extending the file past EOF and this data starts
1034 * block-aligned, use assign_arcbuf(). Otherwise,
1035 * write via dmu_write().
1037 if (tx_bytes < max_blksz && (!write_eof ||
1038 aiov->iov_base != abuf->b_data)) {
1040 dmu_write(zfsvfs->z_os, zp->z_id, woff,
1041 aiov->iov_len, aiov->iov_base, tx);
1042 dmu_return_arcbuf(abuf);
1043 xuio_stat_wbuf_copied();
1045 ASSERT(xuio || tx_bytes == max_blksz);
1046 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
1049 ASSERT(tx_bytes <= uio->uio_resid);
1050 uioskip(uio, tx_bytes);
1052 if (tx_bytes && vn_has_cached_data(vp)) {
1053 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
1054 zp->z_id, uio->uio_segflg, tx);
1058 * If we made no progress, we're done. If we made even
1059 * partial progress, update the znode and ZIL accordingly.
1061 if (tx_bytes == 0) {
1062 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
1063 (void *)&zp->z_size, sizeof (uint64_t), tx);
1070 * Clear Set-UID/Set-GID bits on successful write if not
1071 * privileged and at least one of the excute bits is set.
1073 * It would be nice to to this after all writes have
1074 * been done, but that would still expose the ISUID/ISGID
1075 * to another app after the partial write is committed.
1077 * Note: we don't call zfs_fuid_map_id() here because
1078 * user 0 is not an ephemeral uid.
1080 mutex_enter(&zp->z_acl_lock);
1081 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1082 (S_IXUSR >> 6))) != 0 &&
1083 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1084 secpolicy_vnode_setid_retain(vp, cr,
1085 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1087 zp->z_mode &= ~(S_ISUID | S_ISGID);
1088 newmode = zp->z_mode;
1089 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1090 (void *)&newmode, sizeof (uint64_t), tx);
1092 mutex_exit(&zp->z_acl_lock);
1094 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1098 * Update the file size (zp_size) if it has changed;
1099 * account for possible concurrent updates.
1101 while ((end_size = zp->z_size) < uio->uio_loffset) {
1102 (void) atomic_cas_64(&zp->z_size, end_size,
1107 * If we are replaying and eof is non zero then force
1108 * the file size to the specified eof. Note, there's no
1109 * concurrency during replay.
1111 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1112 zp->z_size = zfsvfs->z_replay_eof;
1114 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1116 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1121 ASSERT(tx_bytes == nbytes);
1126 uio_prefaultpages(MIN(n, max_blksz), uio);
1130 zfs_range_unlock(rl);
1133 * If we're in replay mode, or we made no progress, return error.
1134 * Otherwise, it's at least a partial write, so it's successful.
1136 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1141 if (ioflag & (FSYNC | FDSYNC) ||
1142 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1143 zil_commit(zilog, zp->z_id);
1150 zfs_get_done(zgd_t *zgd, int error)
1152 znode_t *zp = zgd->zgd_private;
1153 objset_t *os = zp->z_zfsvfs->z_os;
1157 dmu_buf_rele(zgd->zgd_db, zgd);
1159 zfs_range_unlock(zgd->zgd_rl);
1161 vfslocked = VFS_LOCK_GIANT(zp->z_zfsvfs->z_vfs);
1163 * Release the vnode asynchronously as we currently have the
1164 * txg stopped from syncing.
1166 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1168 if (error == 0 && zgd->zgd_bp)
1169 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1171 kmem_free(zgd, sizeof (zgd_t));
1172 VFS_UNLOCK_GIANT(vfslocked);
1176 static int zil_fault_io = 0;
1180 * Get data to generate a TX_WRITE intent log record.
1183 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1185 zfsvfs_t *zfsvfs = arg;
1186 objset_t *os = zfsvfs->z_os;
1188 uint64_t object = lr->lr_foid;
1189 uint64_t offset = lr->lr_offset;
1190 uint64_t size = lr->lr_length;
1191 blkptr_t *bp = &lr->lr_blkptr;
1196 ASSERT(zio != NULL);
1200 * Nothing to do if the file has been removed
1202 if (zfs_zget(zfsvfs, object, &zp) != 0)
1203 return (SET_ERROR(ENOENT));
1204 if (zp->z_unlinked) {
1206 * Release the vnode asynchronously as we currently have the
1207 * txg stopped from syncing.
1209 VN_RELE_ASYNC(ZTOV(zp),
1210 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1211 return (SET_ERROR(ENOENT));
1214 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1215 zgd->zgd_zilog = zfsvfs->z_log;
1216 zgd->zgd_private = zp;
1219 * Write records come in two flavors: immediate and indirect.
1220 * For small writes it's cheaper to store the data with the
1221 * log record (immediate); for large writes it's cheaper to
1222 * sync the data and get a pointer to it (indirect) so that
1223 * we don't have to write the data twice.
1225 if (buf != NULL) { /* immediate write */
1226 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1227 /* test for truncation needs to be done while range locked */
1228 if (offset >= zp->z_size) {
1229 error = SET_ERROR(ENOENT);
1231 error = dmu_read(os, object, offset, size, buf,
1232 DMU_READ_NO_PREFETCH);
1234 ASSERT(error == 0 || error == ENOENT);
1235 } else { /* indirect write */
1237 * Have to lock the whole block to ensure when it's
1238 * written out and it's checksum is being calculated
1239 * that no one can change the data. We need to re-check
1240 * blocksize after we get the lock in case it's changed!
1245 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1247 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1249 if (zp->z_blksz == size)
1252 zfs_range_unlock(zgd->zgd_rl);
1254 /* test for truncation needs to be done while range locked */
1255 if (lr->lr_offset >= zp->z_size)
1256 error = SET_ERROR(ENOENT);
1259 error = SET_ERROR(EIO);
1264 error = dmu_buf_hold(os, object, offset, zgd, &db,
1265 DMU_READ_NO_PREFETCH);
1268 blkptr_t *obp = dmu_buf_get_blkptr(db);
1270 ASSERT(BP_IS_HOLE(bp));
1277 ASSERT(db->db_offset == offset);
1278 ASSERT(db->db_size == size);
1280 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1282 ASSERT(error || lr->lr_length <= zp->z_blksz);
1285 * On success, we need to wait for the write I/O
1286 * initiated by dmu_sync() to complete before we can
1287 * release this dbuf. We will finish everything up
1288 * in the zfs_get_done() callback.
1293 if (error == EALREADY) {
1294 lr->lr_common.lrc_txtype = TX_WRITE2;
1300 zfs_get_done(zgd, error);
1307 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1308 caller_context_t *ct)
1310 znode_t *zp = VTOZ(vp);
1311 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1317 if (flag & V_ACE_MASK)
1318 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1320 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1327 * If vnode is for a device return a specfs vnode instead.
1330 specvp_check(vnode_t **vpp, cred_t *cr)
1334 if (IS_DEVVP(*vpp)) {
1337 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1340 error = SET_ERROR(ENOSYS);
1348 * Lookup an entry in a directory, or an extended attribute directory.
1349 * If it exists, return a held vnode reference for it.
1351 * IN: dvp - vnode of directory to search.
1352 * nm - name of entry to lookup.
1353 * pnp - full pathname to lookup [UNUSED].
1354 * flags - LOOKUP_XATTR set if looking for an attribute.
1355 * rdir - root directory vnode [UNUSED].
1356 * cr - credentials of caller.
1357 * ct - caller context
1358 * direntflags - directory lookup flags
1359 * realpnp - returned pathname.
1361 * OUT: vpp - vnode of located entry, NULL if not found.
1363 * RETURN: 0 on success, error code on failure.
1370 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1371 int nameiop, cred_t *cr, kthread_t *td, int flags)
1373 znode_t *zdp = VTOZ(dvp);
1374 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1376 int *direntflags = NULL;
1377 void *realpnp = NULL;
1380 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1382 if (dvp->v_type != VDIR) {
1383 return (SET_ERROR(ENOTDIR));
1384 } else if (zdp->z_sa_hdl == NULL) {
1385 return (SET_ERROR(EIO));
1388 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1389 error = zfs_fastaccesschk_execute(zdp, cr);
1397 vnode_t *tvp = dnlc_lookup(dvp, nm);
1400 error = zfs_fastaccesschk_execute(zdp, cr);
1405 if (tvp == DNLC_NO_VNODE) {
1407 return (SET_ERROR(ENOENT));
1410 return (specvp_check(vpp, cr));
1416 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1423 if (flags & LOOKUP_XATTR) {
1426 * If the xattr property is off, refuse the lookup request.
1428 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1430 return (SET_ERROR(EINVAL));
1435 * We don't allow recursive attributes..
1436 * Maybe someday we will.
1438 if (zdp->z_pflags & ZFS_XATTR) {
1440 return (SET_ERROR(EINVAL));
1443 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1449 * Do we have permission to get into attribute directory?
1452 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1462 if (dvp->v_type != VDIR) {
1464 return (SET_ERROR(ENOTDIR));
1468 * Check accessibility of directory.
1471 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1476 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1477 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1479 return (SET_ERROR(EILSEQ));
1482 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1484 error = specvp_check(vpp, cr);
1486 /* Translate errors and add SAVENAME when needed. */
1487 if (cnp->cn_flags & ISLASTCN) {
1491 if (error == ENOENT) {
1492 error = EJUSTRETURN;
1493 cnp->cn_flags |= SAVENAME;
1499 cnp->cn_flags |= SAVENAME;
1503 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1506 if (cnp->cn_flags & ISDOTDOT) {
1507 ltype = VOP_ISLOCKED(dvp);
1511 error = vn_lock(*vpp, cnp->cn_lkflags);
1512 if (cnp->cn_flags & ISDOTDOT)
1513 vn_lock(dvp, ltype | LK_RETRY);
1523 #ifdef FREEBSD_NAMECACHE
1525 * Insert name into cache (as non-existent) if appropriate.
1527 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
1528 cache_enter(dvp, *vpp, cnp);
1530 * Insert name into cache if appropriate.
1532 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1533 if (!(cnp->cn_flags & ISLASTCN) ||
1534 (nameiop != DELETE && nameiop != RENAME)) {
1535 cache_enter(dvp, *vpp, cnp);
1544 * Attempt to create a new entry in a directory. If the entry
1545 * already exists, truncate the file if permissible, else return
1546 * an error. Return the vp of the created or trunc'd file.
1548 * IN: dvp - vnode of directory to put new file entry in.
1549 * name - name of new file entry.
1550 * vap - attributes of new file.
1551 * excl - flag indicating exclusive or non-exclusive mode.
1552 * mode - mode to open file with.
1553 * cr - credentials of caller.
1554 * flag - large file flag [UNUSED].
1555 * ct - caller context
1556 * vsecp - ACL to be set
1558 * OUT: vpp - vnode of created or trunc'd entry.
1560 * RETURN: 0 on success, error code on failure.
1563 * dvp - ctime|mtime updated if new entry created
1564 * vp - ctime|mtime always, atime if new
1569 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1570 vnode_t **vpp, cred_t *cr, kthread_t *td)
1572 znode_t *zp, *dzp = VTOZ(dvp);
1573 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1581 gid_t gid = crgetgid(cr);
1582 zfs_acl_ids_t acl_ids;
1583 boolean_t fuid_dirtied;
1584 boolean_t have_acl = B_FALSE;
1585 boolean_t waited = B_FALSE;
1590 * If we have an ephemeral id, ACL, or XVATTR then
1591 * make sure file system is at proper version
1594 ksid = crgetsid(cr, KSID_OWNER);
1596 uid = ksid_getid(ksid);
1600 if (zfsvfs->z_use_fuids == B_FALSE &&
1601 (vsecp || (vap->va_mask & AT_XVATTR) ||
1602 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1603 return (SET_ERROR(EINVAL));
1608 zilog = zfsvfs->z_log;
1610 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1611 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1613 return (SET_ERROR(EILSEQ));
1616 if (vap->va_mask & AT_XVATTR) {
1617 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1618 crgetuid(cr), cr, vap->va_type)) != 0) {
1624 getnewvnode_reserve(1);
1629 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1630 vap->va_mode &= ~S_ISVTX;
1632 if (*name == '\0') {
1634 * Null component name refers to the directory itself.
1641 /* possible VN_HOLD(zp) */
1644 if (flag & FIGNORECASE)
1647 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1651 zfs_acl_ids_free(&acl_ids);
1652 if (strcmp(name, "..") == 0)
1653 error = SET_ERROR(EISDIR);
1654 getnewvnode_drop_reserve();
1664 * Create a new file object and update the directory
1667 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1669 zfs_acl_ids_free(&acl_ids);
1674 * We only support the creation of regular files in
1675 * extended attribute directories.
1678 if ((dzp->z_pflags & ZFS_XATTR) &&
1679 (vap->va_type != VREG)) {
1681 zfs_acl_ids_free(&acl_ids);
1682 error = SET_ERROR(EINVAL);
1686 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1687 cr, vsecp, &acl_ids)) != 0)
1691 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1692 zfs_acl_ids_free(&acl_ids);
1693 error = SET_ERROR(EDQUOT);
1697 tx = dmu_tx_create(os);
1699 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1700 ZFS_SA_BASE_ATTR_SIZE);
1702 fuid_dirtied = zfsvfs->z_fuid_dirty;
1704 zfs_fuid_txhold(zfsvfs, tx);
1705 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1706 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1707 if (!zfsvfs->z_use_sa &&
1708 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1709 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1710 0, acl_ids.z_aclp->z_acl_bytes);
1712 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1714 zfs_dirent_unlock(dl);
1715 if (error == ERESTART) {
1721 zfs_acl_ids_free(&acl_ids);
1723 getnewvnode_drop_reserve();
1727 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1730 zfs_fuid_sync(zfsvfs, tx);
1732 (void) zfs_link_create(dl, zp, tx, ZNEW);
1733 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1734 if (flag & FIGNORECASE)
1736 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1737 vsecp, acl_ids.z_fuidp, vap);
1738 zfs_acl_ids_free(&acl_ids);
1741 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1744 zfs_acl_ids_free(&acl_ids);
1748 * A directory entry already exists for this name.
1751 * Can't truncate an existing file if in exclusive mode.
1754 error = SET_ERROR(EEXIST);
1758 * Can't open a directory for writing.
1760 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1761 error = SET_ERROR(EISDIR);
1765 * Verify requested access to file.
1767 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1771 mutex_enter(&dzp->z_lock);
1773 mutex_exit(&dzp->z_lock);
1776 * Truncate regular files if requested.
1778 if ((ZTOV(zp)->v_type == VREG) &&
1779 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1780 /* we can't hold any locks when calling zfs_freesp() */
1781 zfs_dirent_unlock(dl);
1783 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1785 vnevent_create(ZTOV(zp), ct);
1790 getnewvnode_drop_reserve();
1792 zfs_dirent_unlock(dl);
1799 error = specvp_check(vpp, cr);
1802 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1803 zil_commit(zilog, 0);
1810 * Remove an entry from a directory.
1812 * IN: dvp - vnode of directory to remove entry from.
1813 * name - name of entry to remove.
1814 * cr - credentials of caller.
1815 * ct - caller context
1816 * flags - case flags
1818 * RETURN: 0 on success, error code on failure.
1822 * vp - ctime (if nlink > 0)
1825 uint64_t null_xattr = 0;
1829 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1832 znode_t *zp, *dzp = VTOZ(dvp);
1835 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1837 uint64_t acl_obj, xattr_obj;
1838 uint64_t xattr_obj_unlinked = 0;
1842 boolean_t may_delete_now, delete_now = FALSE;
1843 boolean_t unlinked, toobig = FALSE;
1845 pathname_t *realnmp = NULL;
1849 boolean_t waited = B_FALSE;
1853 zilog = zfsvfs->z_log;
1855 if (flags & FIGNORECASE) {
1865 * Attempt to lock directory; fail if entry doesn't exist.
1867 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1877 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1882 * Need to use rmdir for removing directories.
1884 if (vp->v_type == VDIR) {
1885 error = SET_ERROR(EPERM);
1889 vnevent_remove(vp, dvp, name, ct);
1892 dnlc_remove(dvp, realnmp->pn_buf);
1894 dnlc_remove(dvp, name);
1897 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1901 * We may delete the znode now, or we may put it in the unlinked set;
1902 * it depends on whether we're the last link, and on whether there are
1903 * other holds on the vnode. So we dmu_tx_hold() the right things to
1904 * allow for either case.
1907 tx = dmu_tx_create(zfsvfs->z_os);
1908 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1909 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1910 zfs_sa_upgrade_txholds(tx, zp);
1911 zfs_sa_upgrade_txholds(tx, dzp);
1912 if (may_delete_now) {
1914 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1915 /* if the file is too big, only hold_free a token amount */
1916 dmu_tx_hold_free(tx, zp->z_id, 0,
1917 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1920 /* are there any extended attributes? */
1921 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1922 &xattr_obj, sizeof (xattr_obj));
1923 if (error == 0 && xattr_obj) {
1924 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1926 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1927 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1930 mutex_enter(&zp->z_lock);
1931 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1932 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1933 mutex_exit(&zp->z_lock);
1935 /* charge as an update -- would be nice not to charge at all */
1936 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1938 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1940 zfs_dirent_unlock(dl);
1944 if (error == ERESTART) {
1958 * Remove the directory entry.
1960 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1970 * Hold z_lock so that we can make sure that the ACL obj
1971 * hasn't changed. Could have been deleted due to
1974 mutex_enter(&zp->z_lock);
1976 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1977 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1978 delete_now = may_delete_now && !toobig &&
1979 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1980 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1987 panic("zfs_remove: delete_now branch taken");
1989 if (xattr_obj_unlinked) {
1990 ASSERT3U(xzp->z_links, ==, 2);
1991 mutex_enter(&xzp->z_lock);
1992 xzp->z_unlinked = 1;
1994 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1995 &xzp->z_links, sizeof (xzp->z_links), tx);
1996 ASSERT3U(error, ==, 0);
1997 mutex_exit(&xzp->z_lock);
1998 zfs_unlinked_add(xzp, tx);
2001 error = sa_remove(zp->z_sa_hdl,
2002 SA_ZPL_XATTR(zfsvfs), tx);
2004 error = sa_update(zp->z_sa_hdl,
2005 SA_ZPL_XATTR(zfsvfs), &null_xattr,
2006 sizeof (uint64_t), tx);
2011 ASSERT0(vp->v_count);
2013 mutex_exit(&zp->z_lock);
2014 zfs_znode_delete(zp, tx);
2015 } else if (unlinked) {
2016 mutex_exit(&zp->z_lock);
2017 zfs_unlinked_add(zp, tx);
2019 vp->v_vflag |= VV_NOSYNC;
2024 if (flags & FIGNORECASE)
2026 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
2033 zfs_dirent_unlock(dl);
2040 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2041 zil_commit(zilog, 0);
2048 * Create a new directory and insert it into dvp using the name
2049 * provided. Return a pointer to the inserted directory.
2051 * IN: dvp - vnode of directory to add subdir to.
2052 * dirname - name of new directory.
2053 * vap - attributes of new directory.
2054 * cr - credentials of caller.
2055 * ct - caller context
2056 * flags - case flags
2057 * vsecp - ACL to be set
2059 * OUT: vpp - vnode of created directory.
2061 * RETURN: 0 on success, error code on failure.
2064 * dvp - ctime|mtime updated
2065 * vp - ctime|mtime|atime updated
2069 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
2070 caller_context_t *ct, int flags, vsecattr_t *vsecp)
2072 znode_t *zp, *dzp = VTOZ(dvp);
2073 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2082 gid_t gid = crgetgid(cr);
2083 zfs_acl_ids_t acl_ids;
2084 boolean_t fuid_dirtied;
2085 boolean_t waited = B_FALSE;
2087 ASSERT(vap->va_type == VDIR);
2090 * If we have an ephemeral id, ACL, or XVATTR then
2091 * make sure file system is at proper version
2094 ksid = crgetsid(cr, KSID_OWNER);
2096 uid = ksid_getid(ksid);
2099 if (zfsvfs->z_use_fuids == B_FALSE &&
2100 (vsecp || (vap->va_mask & AT_XVATTR) ||
2101 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2102 return (SET_ERROR(EINVAL));
2106 zilog = zfsvfs->z_log;
2108 if (dzp->z_pflags & ZFS_XATTR) {
2110 return (SET_ERROR(EINVAL));
2113 if (zfsvfs->z_utf8 && u8_validate(dirname,
2114 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2116 return (SET_ERROR(EILSEQ));
2118 if (flags & FIGNORECASE)
2121 if (vap->va_mask & AT_XVATTR) {
2122 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2123 crgetuid(cr), cr, vap->va_type)) != 0) {
2129 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2130 vsecp, &acl_ids)) != 0) {
2135 getnewvnode_reserve(1);
2138 * First make sure the new directory doesn't exist.
2140 * Existence is checked first to make sure we don't return
2141 * EACCES instead of EEXIST which can cause some applications
2147 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2149 zfs_acl_ids_free(&acl_ids);
2150 getnewvnode_drop_reserve();
2155 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2156 zfs_acl_ids_free(&acl_ids);
2157 zfs_dirent_unlock(dl);
2158 getnewvnode_drop_reserve();
2163 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2164 zfs_acl_ids_free(&acl_ids);
2165 zfs_dirent_unlock(dl);
2166 getnewvnode_drop_reserve();
2168 return (SET_ERROR(EDQUOT));
2172 * Add a new entry to the directory.
2174 tx = dmu_tx_create(zfsvfs->z_os);
2175 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2176 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2177 fuid_dirtied = zfsvfs->z_fuid_dirty;
2179 zfs_fuid_txhold(zfsvfs, tx);
2180 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2181 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2182 acl_ids.z_aclp->z_acl_bytes);
2185 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2186 ZFS_SA_BASE_ATTR_SIZE);
2188 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2190 zfs_dirent_unlock(dl);
2191 if (error == ERESTART) {
2197 zfs_acl_ids_free(&acl_ids);
2199 getnewvnode_drop_reserve();
2207 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2210 zfs_fuid_sync(zfsvfs, tx);
2213 * Now put new name in parent dir.
2215 (void) zfs_link_create(dl, zp, tx, ZNEW);
2219 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2220 if (flags & FIGNORECASE)
2222 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2223 acl_ids.z_fuidp, vap);
2225 zfs_acl_ids_free(&acl_ids);
2229 getnewvnode_drop_reserve();
2231 zfs_dirent_unlock(dl);
2233 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2234 zil_commit(zilog, 0);
2241 * Remove a directory subdir entry. If the current working
2242 * directory is the same as the subdir to be removed, the
2245 * IN: dvp - vnode of directory to remove from.
2246 * name - name of directory to be removed.
2247 * cwd - vnode of current working directory.
2248 * cr - credentials of caller.
2249 * ct - caller context
2250 * flags - case flags
2252 * RETURN: 0 on success, error code on failure.
2255 * dvp - ctime|mtime updated
2259 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2260 caller_context_t *ct, int flags)
2262 znode_t *dzp = VTOZ(dvp);
2265 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2271 boolean_t waited = B_FALSE;
2275 zilog = zfsvfs->z_log;
2277 if (flags & FIGNORECASE)
2283 * Attempt to lock directory; fail if entry doesn't exist.
2285 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2293 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2297 if (vp->v_type != VDIR) {
2298 error = SET_ERROR(ENOTDIR);
2303 error = SET_ERROR(EINVAL);
2307 vnevent_rmdir(vp, dvp, name, ct);
2310 * Grab a lock on the directory to make sure that noone is
2311 * trying to add (or lookup) entries while we are removing it.
2313 rw_enter(&zp->z_name_lock, RW_WRITER);
2316 * Grab a lock on the parent pointer to make sure we play well
2317 * with the treewalk and directory rename code.
2319 rw_enter(&zp->z_parent_lock, RW_WRITER);
2321 tx = dmu_tx_create(zfsvfs->z_os);
2322 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2323 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2324 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2325 zfs_sa_upgrade_txholds(tx, zp);
2326 zfs_sa_upgrade_txholds(tx, dzp);
2327 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2329 rw_exit(&zp->z_parent_lock);
2330 rw_exit(&zp->z_name_lock);
2331 zfs_dirent_unlock(dl);
2333 if (error == ERESTART) {
2344 #ifdef FREEBSD_NAMECACHE
2348 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2351 uint64_t txtype = TX_RMDIR;
2352 if (flags & FIGNORECASE)
2354 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2359 rw_exit(&zp->z_parent_lock);
2360 rw_exit(&zp->z_name_lock);
2361 #ifdef FREEBSD_NAMECACHE
2365 zfs_dirent_unlock(dl);
2369 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2370 zil_commit(zilog, 0);
2377 * Read as many directory entries as will fit into the provided
2378 * buffer from the given directory cursor position (specified in
2379 * the uio structure).
2381 * IN: vp - vnode of directory to read.
2382 * uio - structure supplying read location, range info,
2383 * and return buffer.
2384 * cr - credentials of caller.
2385 * ct - caller context
2386 * flags - case flags
2388 * OUT: uio - updated offset and range, buffer filled.
2389 * eofp - set to true if end-of-file detected.
2391 * RETURN: 0 on success, error code on failure.
2394 * vp - atime updated
2396 * Note that the low 4 bits of the cookie returned by zap is always zero.
2397 * This allows us to use the low range for "special" directory entries:
2398 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2399 * we use the offset 2 for the '.zfs' directory.
2403 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2405 znode_t *zp = VTOZ(vp);
2409 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2414 zap_attribute_t zap;
2415 uint_t bytes_wanted;
2416 uint64_t offset; /* must be unsigned; checks for < 1 */
2422 boolean_t check_sysattrs;
2425 u_long *cooks = NULL;
2431 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2432 &parent, sizeof (parent))) != 0) {
2438 * If we are not given an eof variable,
2445 * Check for valid iov_len.
2447 if (uio->uio_iov->iov_len <= 0) {
2449 return (SET_ERROR(EINVAL));
2453 * Quit if directory has been removed (posix)
2455 if ((*eofp = zp->z_unlinked) != 0) {
2462 offset = uio->uio_loffset;
2463 prefetch = zp->z_zn_prefetch;
2466 * Initialize the iterator cursor.
2470 * Start iteration from the beginning of the directory.
2472 zap_cursor_init(&zc, os, zp->z_id);
2475 * The offset is a serialized cursor.
2477 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2481 * Get space to change directory entries into fs independent format.
2483 iovp = uio->uio_iov;
2484 bytes_wanted = iovp->iov_len;
2485 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2486 bufsize = bytes_wanted;
2487 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2488 odp = (struct dirent64 *)outbuf;
2490 bufsize = bytes_wanted;
2492 odp = (struct dirent64 *)iovp->iov_base;
2494 eodp = (struct edirent *)odp;
2496 if (ncookies != NULL) {
2498 * Minimum entry size is dirent size and 1 byte for a file name.
2500 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2501 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2506 * If this VFS supports the system attribute view interface; and
2507 * we're looking at an extended attribute directory; and we care
2508 * about normalization conflicts on this vfs; then we must check
2509 * for normalization conflicts with the sysattr name space.
2512 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2513 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2514 (flags & V_RDDIR_ENTFLAGS);
2520 * Transform to file-system independent format
2523 while (outcount < bytes_wanted) {
2526 off64_t *next = NULL;
2529 * Special case `.', `..', and `.zfs'.
2532 (void) strcpy(zap.za_name, ".");
2533 zap.za_normalization_conflict = 0;
2536 } else if (offset == 1) {
2537 (void) strcpy(zap.za_name, "..");
2538 zap.za_normalization_conflict = 0;
2541 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2542 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2543 zap.za_normalization_conflict = 0;
2544 objnum = ZFSCTL_INO_ROOT;
2550 if (error = zap_cursor_retrieve(&zc, &zap)) {
2551 if ((*eofp = (error == ENOENT)) != 0)
2557 if (zap.za_integer_length != 8 ||
2558 zap.za_num_integers != 1) {
2559 cmn_err(CE_WARN, "zap_readdir: bad directory "
2560 "entry, obj = %lld, offset = %lld\n",
2561 (u_longlong_t)zp->z_id,
2562 (u_longlong_t)offset);
2563 error = SET_ERROR(ENXIO);
2567 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2569 * MacOS X can extract the object type here such as:
2570 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2572 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2574 if (check_sysattrs && !zap.za_normalization_conflict) {
2576 zap.za_normalization_conflict =
2577 xattr_sysattr_casechk(zap.za_name);
2579 panic("%s:%u: TODO", __func__, __LINE__);
2584 if (flags & V_RDDIR_ACCFILTER) {
2586 * If we have no access at all, don't include
2587 * this entry in the returned information
2590 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2592 if (!zfs_has_access(ezp, cr)) {
2599 if (flags & V_RDDIR_ENTFLAGS)
2600 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2602 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2605 * Will this entry fit in the buffer?
2607 if (outcount + reclen > bufsize) {
2609 * Did we manage to fit anything in the buffer?
2612 error = SET_ERROR(EINVAL);
2617 if (flags & V_RDDIR_ENTFLAGS) {
2619 * Add extended flag entry:
2621 eodp->ed_ino = objnum;
2622 eodp->ed_reclen = reclen;
2623 /* NOTE: ed_off is the offset for the *next* entry */
2624 next = &(eodp->ed_off);
2625 eodp->ed_eflags = zap.za_normalization_conflict ?
2626 ED_CASE_CONFLICT : 0;
2627 (void) strncpy(eodp->ed_name, zap.za_name,
2628 EDIRENT_NAMELEN(reclen));
2629 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2634 odp->d_ino = objnum;
2635 odp->d_reclen = reclen;
2636 odp->d_namlen = strlen(zap.za_name);
2637 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2639 odp = (dirent64_t *)((intptr_t)odp + reclen);
2643 ASSERT(outcount <= bufsize);
2645 /* Prefetch znode */
2647 dmu_prefetch(os, objnum, 0, 0);
2651 * Move to the next entry, fill in the previous offset.
2653 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2654 zap_cursor_advance(&zc);
2655 offset = zap_cursor_serialize(&zc);
2660 if (cooks != NULL) {
2663 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2666 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2668 /* Subtract unused cookies */
2669 if (ncookies != NULL)
2670 *ncookies -= ncooks;
2672 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2673 iovp->iov_base += outcount;
2674 iovp->iov_len -= outcount;
2675 uio->uio_resid -= outcount;
2676 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2678 * Reset the pointer.
2680 offset = uio->uio_loffset;
2684 zap_cursor_fini(&zc);
2685 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2686 kmem_free(outbuf, bufsize);
2688 if (error == ENOENT)
2691 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2693 uio->uio_loffset = offset;
2695 if (error != 0 && cookies != NULL) {
2696 free(*cookies, M_TEMP);
2703 ulong_t zfs_fsync_sync_cnt = 4;
2706 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2708 znode_t *zp = VTOZ(vp);
2709 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2711 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2713 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2716 zil_commit(zfsvfs->z_log, zp->z_id);
2724 * Get the requested file attributes and place them in the provided
2727 * IN: vp - vnode of file.
2728 * vap - va_mask identifies requested attributes.
2729 * If AT_XVATTR set, then optional attrs are requested
2730 * flags - ATTR_NOACLCHECK (CIFS server context)
2731 * cr - credentials of caller.
2732 * ct - caller context
2734 * OUT: vap - attribute values.
2736 * RETURN: 0 (always succeeds).
2740 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2741 caller_context_t *ct)
2743 znode_t *zp = VTOZ(vp);
2744 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2747 u_longlong_t nblocks;
2749 uint64_t mtime[2], ctime[2], crtime[2], rdev;
2750 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2751 xoptattr_t *xoap = NULL;
2752 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2753 sa_bulk_attr_t bulk[4];
2759 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2761 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2762 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2763 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2764 if (vp->v_type == VBLK || vp->v_type == VCHR)
2765 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL,
2768 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2774 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2775 * Also, if we are the owner don't bother, since owner should
2776 * always be allowed to read basic attributes of file.
2778 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2779 (vap->va_uid != crgetuid(cr))) {
2780 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2788 * Return all attributes. It's cheaper to provide the answer
2789 * than to determine whether we were asked the question.
2792 mutex_enter(&zp->z_lock);
2793 vap->va_type = IFTOVT(zp->z_mode);
2794 vap->va_mode = zp->z_mode & ~S_IFMT;
2796 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2798 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2800 vap->va_nodeid = zp->z_id;
2801 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2802 links = zp->z_links + 1;
2804 links = zp->z_links;
2805 vap->va_nlink = MIN(links, LINK_MAX); /* nlink_t limit! */
2806 vap->va_size = zp->z_size;
2808 vap->va_rdev = vp->v_rdev;
2810 if (vp->v_type == VBLK || vp->v_type == VCHR)
2811 vap->va_rdev = zfs_cmpldev(rdev);
2813 vap->va_seq = zp->z_seq;
2814 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2817 * Add in any requested optional attributes and the create time.
2818 * Also set the corresponding bits in the returned attribute bitmap.
2820 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2821 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2823 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2824 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2827 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2828 xoap->xoa_readonly =
2829 ((zp->z_pflags & ZFS_READONLY) != 0);
2830 XVA_SET_RTN(xvap, XAT_READONLY);
2833 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2835 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2836 XVA_SET_RTN(xvap, XAT_SYSTEM);
2839 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2841 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2842 XVA_SET_RTN(xvap, XAT_HIDDEN);
2845 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2846 xoap->xoa_nounlink =
2847 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2848 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2851 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2852 xoap->xoa_immutable =
2853 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2854 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2857 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2858 xoap->xoa_appendonly =
2859 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2860 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2863 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2865 ((zp->z_pflags & ZFS_NODUMP) != 0);
2866 XVA_SET_RTN(xvap, XAT_NODUMP);
2869 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2871 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2872 XVA_SET_RTN(xvap, XAT_OPAQUE);
2875 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2876 xoap->xoa_av_quarantined =
2877 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2878 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2881 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2882 xoap->xoa_av_modified =
2883 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2884 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2887 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2888 vp->v_type == VREG) {
2889 zfs_sa_get_scanstamp(zp, xvap);
2892 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2895 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2896 times, sizeof (times));
2897 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2898 XVA_SET_RTN(xvap, XAT_CREATETIME);
2901 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2902 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2903 XVA_SET_RTN(xvap, XAT_REPARSE);
2905 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2906 xoap->xoa_generation = zp->z_gen;
2907 XVA_SET_RTN(xvap, XAT_GEN);
2910 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2912 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2913 XVA_SET_RTN(xvap, XAT_OFFLINE);
2916 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2918 ((zp->z_pflags & ZFS_SPARSE) != 0);
2919 XVA_SET_RTN(xvap, XAT_SPARSE);
2923 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2924 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2925 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2926 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2928 mutex_exit(&zp->z_lock);
2930 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2931 vap->va_blksize = blksize;
2932 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2934 if (zp->z_blksz == 0) {
2936 * Block size hasn't been set; suggest maximal I/O transfers.
2938 vap->va_blksize = zfsvfs->z_max_blksz;
2946 * Set the file attributes to the values contained in the
2949 * IN: vp - vnode of file to be modified.
2950 * vap - new attribute values.
2951 * If AT_XVATTR set, then optional attrs are being set
2952 * flags - ATTR_UTIME set if non-default time values provided.
2953 * - ATTR_NOACLCHECK (CIFS context only).
2954 * cr - credentials of caller.
2955 * ct - caller context
2957 * RETURN: 0 on success, error code on failure.
2960 * vp - ctime updated, mtime updated if size changed.
2964 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2965 caller_context_t *ct)
2967 znode_t *zp = VTOZ(vp);
2968 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2973 uint_t mask = vap->va_mask;
2974 uint_t saved_mask = 0;
2975 uint64_t saved_mode;
2978 uint64_t new_uid, new_gid;
2980 uint64_t mtime[2], ctime[2];
2982 int need_policy = FALSE;
2984 zfs_fuid_info_t *fuidp = NULL;
2985 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2988 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2989 boolean_t fuid_dirtied = B_FALSE;
2990 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2991 int count = 0, xattr_count = 0;
2996 if (mask & AT_NOSET)
2997 return (SET_ERROR(EINVAL));
3002 zilog = zfsvfs->z_log;
3005 * Make sure that if we have ephemeral uid/gid or xvattr specified
3006 * that file system is at proper version level
3009 if (zfsvfs->z_use_fuids == B_FALSE &&
3010 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3011 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
3012 (mask & AT_XVATTR))) {
3014 return (SET_ERROR(EINVAL));
3017 if (mask & AT_SIZE && vp->v_type == VDIR) {
3019 return (SET_ERROR(EISDIR));
3022 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
3024 return (SET_ERROR(EINVAL));
3028 * If this is an xvattr_t, then get a pointer to the structure of
3029 * optional attributes. If this is NULL, then we have a vattr_t.
3031 xoap = xva_getxoptattr(xvap);
3033 xva_init(&tmpxvattr);
3036 * Immutable files can only alter immutable bit and atime
3038 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
3039 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
3040 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
3042 return (SET_ERROR(EPERM));
3045 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
3047 return (SET_ERROR(EPERM));
3051 * Verify timestamps doesn't overflow 32 bits.
3052 * ZFS can handle large timestamps, but 32bit syscalls can't
3053 * handle times greater than 2039. This check should be removed
3054 * once large timestamps are fully supported.
3056 if (mask & (AT_ATIME | AT_MTIME)) {
3057 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
3058 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
3060 return (SET_ERROR(EOVERFLOW));
3068 /* Can this be moved to before the top label? */
3069 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
3071 return (SET_ERROR(EROFS));
3075 * First validate permissions
3078 if (mask & AT_SIZE) {
3080 * XXX - Note, we are not providing any open
3081 * mode flags here (like FNDELAY), so we may
3082 * block if there are locks present... this
3083 * should be addressed in openat().
3085 /* XXX - would it be OK to generate a log record here? */
3086 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3093 if (mask & (AT_ATIME|AT_MTIME) ||
3094 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3095 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3096 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3097 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3098 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3099 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3100 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3101 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3105 if (mask & (AT_UID|AT_GID)) {
3106 int idmask = (mask & (AT_UID|AT_GID));
3111 * NOTE: even if a new mode is being set,
3112 * we may clear S_ISUID/S_ISGID bits.
3115 if (!(mask & AT_MODE))
3116 vap->va_mode = zp->z_mode;
3119 * Take ownership or chgrp to group we are a member of
3122 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3123 take_group = (mask & AT_GID) &&
3124 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3127 * If both AT_UID and AT_GID are set then take_owner and
3128 * take_group must both be set in order to allow taking
3131 * Otherwise, send the check through secpolicy_vnode_setattr()
3135 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3136 ((idmask == AT_UID) && take_owner) ||
3137 ((idmask == AT_GID) && take_group)) {
3138 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3139 skipaclchk, cr) == 0) {
3141 * Remove setuid/setgid for non-privileged users
3143 secpolicy_setid_clear(vap, vp, cr);
3144 trim_mask = (mask & (AT_UID|AT_GID));
3153 mutex_enter(&zp->z_lock);
3154 oldva.va_mode = zp->z_mode;
3155 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3156 if (mask & AT_XVATTR) {
3158 * Update xvattr mask to include only those attributes
3159 * that are actually changing.
3161 * the bits will be restored prior to actually setting
3162 * the attributes so the caller thinks they were set.
3164 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3165 if (xoap->xoa_appendonly !=
3166 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3169 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3170 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3174 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3175 if (xoap->xoa_nounlink !=
3176 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3179 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3180 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3184 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3185 if (xoap->xoa_immutable !=
3186 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3189 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3190 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3194 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3195 if (xoap->xoa_nodump !=
3196 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3199 XVA_CLR_REQ(xvap, XAT_NODUMP);
3200 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3204 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3205 if (xoap->xoa_av_modified !=
3206 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3209 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3210 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3214 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3215 if ((vp->v_type != VREG &&
3216 xoap->xoa_av_quarantined) ||
3217 xoap->xoa_av_quarantined !=
3218 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3221 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3222 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3226 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3227 mutex_exit(&zp->z_lock);
3229 return (SET_ERROR(EPERM));
3232 if (need_policy == FALSE &&
3233 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3234 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3239 mutex_exit(&zp->z_lock);
3241 if (mask & AT_MODE) {
3242 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3243 err = secpolicy_setid_setsticky_clear(vp, vap,
3249 trim_mask |= AT_MODE;
3257 * If trim_mask is set then take ownership
3258 * has been granted or write_acl is present and user
3259 * has the ability to modify mode. In that case remove
3260 * UID|GID and or MODE from mask so that
3261 * secpolicy_vnode_setattr() doesn't revoke it.
3265 saved_mask = vap->va_mask;
3266 vap->va_mask &= ~trim_mask;
3267 if (trim_mask & AT_MODE) {
3269 * Save the mode, as secpolicy_vnode_setattr()
3270 * will overwrite it with ova.va_mode.
3272 saved_mode = vap->va_mode;
3275 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3276 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3283 vap->va_mask |= saved_mask;
3284 if (trim_mask & AT_MODE) {
3286 * Recover the mode after
3287 * secpolicy_vnode_setattr().
3289 vap->va_mode = saved_mode;
3295 * secpolicy_vnode_setattr, or take ownership may have
3298 mask = vap->va_mask;
3300 if ((mask & (AT_UID | AT_GID))) {
3301 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3302 &xattr_obj, sizeof (xattr_obj));
3304 if (err == 0 && xattr_obj) {
3305 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3309 if (mask & AT_UID) {
3310 new_uid = zfs_fuid_create(zfsvfs,
3311 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3312 if (new_uid != zp->z_uid &&
3313 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3315 VN_RELE(ZTOV(attrzp));
3316 err = SET_ERROR(EDQUOT);
3321 if (mask & AT_GID) {
3322 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3323 cr, ZFS_GROUP, &fuidp);
3324 if (new_gid != zp->z_gid &&
3325 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3327 VN_RELE(ZTOV(attrzp));
3328 err = SET_ERROR(EDQUOT);
3333 tx = dmu_tx_create(zfsvfs->z_os);
3335 if (mask & AT_MODE) {
3336 uint64_t pmode = zp->z_mode;
3338 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3340 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3341 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3342 err = SET_ERROR(EPERM);
3346 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3349 mutex_enter(&zp->z_lock);
3350 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3352 * Are we upgrading ACL from old V0 format
3355 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3356 zfs_znode_acl_version(zp) ==
3357 ZFS_ACL_VERSION_INITIAL) {
3358 dmu_tx_hold_free(tx, acl_obj, 0,
3360 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3361 0, aclp->z_acl_bytes);
3363 dmu_tx_hold_write(tx, acl_obj, 0,
3366 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3367 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3368 0, aclp->z_acl_bytes);
3370 mutex_exit(&zp->z_lock);
3371 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3373 if ((mask & AT_XVATTR) &&
3374 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3375 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3377 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3381 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3384 fuid_dirtied = zfsvfs->z_fuid_dirty;
3386 zfs_fuid_txhold(zfsvfs, tx);
3388 zfs_sa_upgrade_txholds(tx, zp);
3390 err = dmu_tx_assign(tx, TXG_WAIT);
3396 * Set each attribute requested.
3397 * We group settings according to the locks they need to acquire.
3399 * Note: you cannot set ctime directly, although it will be
3400 * updated as a side-effect of calling this function.
3404 if (mask & (AT_UID|AT_GID|AT_MODE))
3405 mutex_enter(&zp->z_acl_lock);
3406 mutex_enter(&zp->z_lock);
3408 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3409 &zp->z_pflags, sizeof (zp->z_pflags));
3412 if (mask & (AT_UID|AT_GID|AT_MODE))
3413 mutex_enter(&attrzp->z_acl_lock);
3414 mutex_enter(&attrzp->z_lock);
3415 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3416 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3417 sizeof (attrzp->z_pflags));
3420 if (mask & (AT_UID|AT_GID)) {
3422 if (mask & AT_UID) {
3423 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3424 &new_uid, sizeof (new_uid));
3425 zp->z_uid = new_uid;
3427 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3428 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3430 attrzp->z_uid = new_uid;
3434 if (mask & AT_GID) {
3435 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3436 NULL, &new_gid, sizeof (new_gid));
3437 zp->z_gid = new_gid;
3439 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3440 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3442 attrzp->z_gid = new_gid;
3445 if (!(mask & AT_MODE)) {
3446 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3447 NULL, &new_mode, sizeof (new_mode));
3448 new_mode = zp->z_mode;
3450 err = zfs_acl_chown_setattr(zp);
3453 err = zfs_acl_chown_setattr(attrzp);
3458 if (mask & AT_MODE) {
3459 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3460 &new_mode, sizeof (new_mode));
3461 zp->z_mode = new_mode;
3462 ASSERT3U((uintptr_t)aclp, !=, 0);
3463 err = zfs_aclset_common(zp, aclp, cr, tx);
3465 if (zp->z_acl_cached)
3466 zfs_acl_free(zp->z_acl_cached);
3467 zp->z_acl_cached = aclp;
3472 if (mask & AT_ATIME) {
3473 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3474 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3475 &zp->z_atime, sizeof (zp->z_atime));
3478 if (mask & AT_MTIME) {
3479 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3480 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3481 mtime, sizeof (mtime));
3484 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3485 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3486 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3487 NULL, mtime, sizeof (mtime));
3488 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3489 &ctime, sizeof (ctime));
3490 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3492 } else if (mask != 0) {
3493 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3494 &ctime, sizeof (ctime));
3495 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3498 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3499 SA_ZPL_CTIME(zfsvfs), NULL,
3500 &ctime, sizeof (ctime));
3501 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3502 mtime, ctime, B_TRUE);
3506 * Do this after setting timestamps to prevent timestamp
3507 * update from toggling bit
3510 if (xoap && (mask & AT_XVATTR)) {
3513 * restore trimmed off masks
3514 * so that return masks can be set for caller.
3517 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3518 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3520 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3521 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3523 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3524 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3526 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3527 XVA_SET_REQ(xvap, XAT_NODUMP);
3529 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3530 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3532 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3533 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3536 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3537 ASSERT(vp->v_type == VREG);
3539 zfs_xvattr_set(zp, xvap, tx);
3543 zfs_fuid_sync(zfsvfs, tx);
3546 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3548 mutex_exit(&zp->z_lock);
3549 if (mask & (AT_UID|AT_GID|AT_MODE))
3550 mutex_exit(&zp->z_acl_lock);
3553 if (mask & (AT_UID|AT_GID|AT_MODE))
3554 mutex_exit(&attrzp->z_acl_lock);
3555 mutex_exit(&attrzp->z_lock);
3558 if (err == 0 && attrzp) {
3559 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3565 VN_RELE(ZTOV(attrzp));
3571 zfs_fuid_info_free(fuidp);
3577 if (err == ERESTART)
3580 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3585 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3586 zil_commit(zilog, 0);
3592 typedef struct zfs_zlock {
3593 krwlock_t *zl_rwlock; /* lock we acquired */
3594 znode_t *zl_znode; /* znode we held */
3595 struct zfs_zlock *zl_next; /* next in list */
3599 * Drop locks and release vnodes that were held by zfs_rename_lock().
3602 zfs_rename_unlock(zfs_zlock_t **zlpp)
3606 while ((zl = *zlpp) != NULL) {
3607 if (zl->zl_znode != NULL)
3608 VN_RELE(ZTOV(zl->zl_znode));
3609 rw_exit(zl->zl_rwlock);
3610 *zlpp = zl->zl_next;
3611 kmem_free(zl, sizeof (*zl));
3616 * Search back through the directory tree, using the ".." entries.
3617 * Lock each directory in the chain to prevent concurrent renames.
3618 * Fail any attempt to move a directory into one of its own descendants.
3619 * XXX - z_parent_lock can overlap with map or grow locks
3622 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3626 uint64_t rootid = zp->z_zfsvfs->z_root;
3627 uint64_t oidp = zp->z_id;
3628 krwlock_t *rwlp = &szp->z_parent_lock;
3629 krw_t rw = RW_WRITER;
3632 * First pass write-locks szp and compares to zp->z_id.
3633 * Later passes read-lock zp and compare to zp->z_parent.
3636 if (!rw_tryenter(rwlp, rw)) {
3638 * Another thread is renaming in this path.
3639 * Note that if we are a WRITER, we don't have any
3640 * parent_locks held yet.
3642 if (rw == RW_READER && zp->z_id > szp->z_id) {
3644 * Drop our locks and restart
3646 zfs_rename_unlock(&zl);
3650 rwlp = &szp->z_parent_lock;
3655 * Wait for other thread to drop its locks
3661 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3662 zl->zl_rwlock = rwlp;
3663 zl->zl_znode = NULL;
3664 zl->zl_next = *zlpp;
3667 if (oidp == szp->z_id) /* We're a descendant of szp */
3668 return (SET_ERROR(EINVAL));
3670 if (oidp == rootid) /* We've hit the top */
3673 if (rw == RW_READER) { /* i.e. not the first pass */
3674 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3679 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3680 &oidp, sizeof (oidp));
3681 rwlp = &zp->z_parent_lock;
3684 } while (zp->z_id != sdzp->z_id);
3690 * Move an entry from the provided source directory to the target
3691 * directory. Change the entry name as indicated.
3693 * IN: sdvp - Source directory containing the "old entry".
3694 * snm - Old entry name.
3695 * tdvp - Target directory to contain the "new entry".
3696 * tnm - New entry name.
3697 * cr - credentials of caller.
3698 * ct - caller context
3699 * flags - case flags
3701 * RETURN: 0 on success, error code on failure.
3704 * sdvp,tdvp - ctime|mtime updated
3708 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3709 caller_context_t *ct, int flags)
3711 znode_t *tdzp, *szp, *tzp;
3712 znode_t *sdzp = VTOZ(sdvp);
3713 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3716 zfs_dirlock_t *sdl, *tdl;
3719 int cmp, serr, terr;
3722 boolean_t waited = B_FALSE;
3725 ZFS_VERIFY_ZP(sdzp);
3726 zilog = zfsvfs->z_log;
3729 * Make sure we have the real vp for the target directory.
3731 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3734 if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3736 return (SET_ERROR(EXDEV));
3740 ZFS_VERIFY_ZP(tdzp);
3741 if (zfsvfs->z_utf8 && u8_validate(tnm,
3742 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3744 return (SET_ERROR(EILSEQ));
3747 if (flags & FIGNORECASE)
3756 * This is to prevent the creation of links into attribute space
3757 * by renaming a linked file into/outof an attribute directory.
3758 * See the comment in zfs_link() for why this is considered bad.
3760 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3762 return (SET_ERROR(EINVAL));
3766 * Lock source and target directory entries. To prevent deadlock,
3767 * a lock ordering must be defined. We lock the directory with
3768 * the smallest object id first, or if it's a tie, the one with
3769 * the lexically first name.
3771 if (sdzp->z_id < tdzp->z_id) {
3773 } else if (sdzp->z_id > tdzp->z_id) {
3777 * First compare the two name arguments without
3778 * considering any case folding.
3780 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3782 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3783 ASSERT(error == 0 || !zfsvfs->z_utf8);
3786 * POSIX: "If the old argument and the new argument
3787 * both refer to links to the same existing file,
3788 * the rename() function shall return successfully
3789 * and perform no other action."
3795 * If the file system is case-folding, then we may
3796 * have some more checking to do. A case-folding file
3797 * system is either supporting mixed case sensitivity
3798 * access or is completely case-insensitive. Note
3799 * that the file system is always case preserving.
3801 * In mixed sensitivity mode case sensitive behavior
3802 * is the default. FIGNORECASE must be used to
3803 * explicitly request case insensitive behavior.
3805 * If the source and target names provided differ only
3806 * by case (e.g., a request to rename 'tim' to 'Tim'),
3807 * we will treat this as a special case in the
3808 * case-insensitive mode: as long as the source name
3809 * is an exact match, we will allow this to proceed as
3810 * a name-change request.
3812 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3813 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3814 flags & FIGNORECASE)) &&
3815 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3818 * case preserving rename request, require exact
3827 * If the source and destination directories are the same, we should
3828 * grab the z_name_lock of that directory only once.
3832 rw_enter(&sdzp->z_name_lock, RW_READER);
3836 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3837 ZEXISTS | zflg, NULL, NULL);
3838 terr = zfs_dirent_lock(&tdl,
3839 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3841 terr = zfs_dirent_lock(&tdl,
3842 tdzp, tnm, &tzp, zflg, NULL, NULL);
3843 serr = zfs_dirent_lock(&sdl,
3844 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3850 * Source entry invalid or not there.
3853 zfs_dirent_unlock(tdl);
3859 rw_exit(&sdzp->z_name_lock);
3862 * FreeBSD: In OpenSolaris they only check if rename source is
3863 * ".." here, because "." is handled in their lookup. This is
3864 * not the case for FreeBSD, so we check for "." explicitly.
3866 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3867 serr = SET_ERROR(EINVAL);
3872 zfs_dirent_unlock(sdl);
3876 rw_exit(&sdzp->z_name_lock);
3878 if (strcmp(tnm, "..") == 0)
3879 terr = SET_ERROR(EINVAL);
3885 * Must have write access at the source to remove the old entry
3886 * and write access at the target to create the new entry.
3887 * Note that if target and source are the same, this can be
3888 * done in a single check.
3891 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3894 if (ZTOV(szp)->v_type == VDIR) {
3896 * Check to make sure rename is valid.
3897 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3899 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3904 * Does target exist?
3908 * Source and target must be the same type.
3910 if (ZTOV(szp)->v_type == VDIR) {
3911 if (ZTOV(tzp)->v_type != VDIR) {
3912 error = SET_ERROR(ENOTDIR);
3916 if (ZTOV(tzp)->v_type == VDIR) {
3917 error = SET_ERROR(EISDIR);
3922 * POSIX dictates that when the source and target
3923 * entries refer to the same file object, rename
3924 * must do nothing and exit without error.
3926 if (szp->z_id == tzp->z_id) {
3932 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3934 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3937 * notify the target directory if it is not the same
3938 * as source directory.
3941 vnevent_rename_dest_dir(tdvp, ct);
3944 tx = dmu_tx_create(zfsvfs->z_os);
3945 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3946 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3947 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3948 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3950 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3951 zfs_sa_upgrade_txholds(tx, tdzp);
3954 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3955 zfs_sa_upgrade_txholds(tx, tzp);
3958 zfs_sa_upgrade_txholds(tx, szp);
3959 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3960 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3963 zfs_rename_unlock(&zl);
3964 zfs_dirent_unlock(sdl);
3965 zfs_dirent_unlock(tdl);
3968 rw_exit(&sdzp->z_name_lock);
3973 if (error == ERESTART) {
3984 if (tzp) /* Attempt to remove the existing target */
3985 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3988 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3990 szp->z_pflags |= ZFS_AV_MODIFIED;
3992 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3993 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3996 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3998 zfs_log_rename(zilog, tx, TX_RENAME |
3999 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
4000 sdl->dl_name, tdzp, tdl->dl_name, szp);
4003 * Update path information for the target vnode
4005 vn_renamepath(tdvp, ZTOV(szp), tnm,
4009 * At this point, we have successfully created
4010 * the target name, but have failed to remove
4011 * the source name. Since the create was done
4012 * with the ZRENAMING flag, there are
4013 * complications; for one, the link count is
4014 * wrong. The easiest way to deal with this
4015 * is to remove the newly created target, and
4016 * return the original error. This must
4017 * succeed; fortunately, it is very unlikely to
4018 * fail, since we just created it.
4020 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
4021 ZRENAMING, NULL), ==, 0);
4024 #ifdef FREEBSD_NAMECACHE
4035 zfs_rename_unlock(&zl);
4037 zfs_dirent_unlock(sdl);
4038 zfs_dirent_unlock(tdl);
4041 rw_exit(&sdzp->z_name_lock);
4048 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4049 zil_commit(zilog, 0);
4057 * Insert the indicated symbolic reference entry into the directory.
4059 * IN: dvp - Directory to contain new symbolic link.
4060 * link - Name for new symlink entry.
4061 * vap - Attributes of new entry.
4062 * cr - credentials of caller.
4063 * ct - caller context
4064 * flags - case flags
4066 * RETURN: 0 on success, error code on failure.
4069 * dvp - ctime|mtime updated
4073 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
4074 cred_t *cr, kthread_t *td)
4076 znode_t *zp, *dzp = VTOZ(dvp);
4079 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4081 uint64_t len = strlen(link);
4084 zfs_acl_ids_t acl_ids;
4085 boolean_t fuid_dirtied;
4086 uint64_t txtype = TX_SYMLINK;
4087 boolean_t waited = B_FALSE;
4090 ASSERT(vap->va_type == VLNK);
4094 zilog = zfsvfs->z_log;
4096 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4097 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4099 return (SET_ERROR(EILSEQ));
4101 if (flags & FIGNORECASE)
4104 if (len > MAXPATHLEN) {
4106 return (SET_ERROR(ENAMETOOLONG));
4109 if ((error = zfs_acl_ids_create(dzp, 0,
4110 vap, cr, NULL, &acl_ids)) != 0) {
4115 getnewvnode_reserve(1);
4119 * Attempt to lock directory; fail if entry already exists.
4121 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4123 zfs_acl_ids_free(&acl_ids);
4124 getnewvnode_drop_reserve();
4129 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4130 zfs_acl_ids_free(&acl_ids);
4131 zfs_dirent_unlock(dl);
4132 getnewvnode_drop_reserve();
4137 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4138 zfs_acl_ids_free(&acl_ids);
4139 zfs_dirent_unlock(dl);
4140 getnewvnode_drop_reserve();
4142 return (SET_ERROR(EDQUOT));
4144 tx = dmu_tx_create(zfsvfs->z_os);
4145 fuid_dirtied = zfsvfs->z_fuid_dirty;
4146 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4147 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4148 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4149 ZFS_SA_BASE_ATTR_SIZE + len);
4150 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4151 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4152 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4153 acl_ids.z_aclp->z_acl_bytes);
4156 zfs_fuid_txhold(zfsvfs, tx);
4157 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4159 zfs_dirent_unlock(dl);
4160 if (error == ERESTART) {
4166 zfs_acl_ids_free(&acl_ids);
4168 getnewvnode_drop_reserve();
4174 * Create a new object for the symlink.
4175 * for version 4 ZPL datsets the symlink will be an SA attribute
4177 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4180 zfs_fuid_sync(zfsvfs, tx);
4182 mutex_enter(&zp->z_lock);
4184 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4187 zfs_sa_symlink(zp, link, len, tx);
4188 mutex_exit(&zp->z_lock);
4191 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4192 &zp->z_size, sizeof (zp->z_size), tx);
4194 * Insert the new object into the directory.
4196 (void) zfs_link_create(dl, zp, tx, ZNEW);
4198 if (flags & FIGNORECASE)
4200 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4203 zfs_acl_ids_free(&acl_ids);
4207 getnewvnode_drop_reserve();
4209 zfs_dirent_unlock(dl);
4211 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4212 zil_commit(zilog, 0);
4219 * Return, in the buffer contained in the provided uio structure,
4220 * the symbolic path referred to by vp.
4222 * IN: vp - vnode of symbolic link.
4223 * uio - structure to contain the link path.
4224 * cr - credentials of caller.
4225 * ct - caller context
4227 * OUT: uio - structure containing the link path.
4229 * RETURN: 0 on success, error code on failure.
4232 * vp - atime updated
4236 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4238 znode_t *zp = VTOZ(vp);
4239 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4245 mutex_enter(&zp->z_lock);
4247 error = sa_lookup_uio(zp->z_sa_hdl,
4248 SA_ZPL_SYMLINK(zfsvfs), uio);
4250 error = zfs_sa_readlink(zp, uio);
4251 mutex_exit(&zp->z_lock);
4253 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4260 * Insert a new entry into directory tdvp referencing svp.
4262 * IN: tdvp - Directory to contain new entry.
4263 * svp - vnode of new entry.
4264 * name - name of new entry.
4265 * cr - credentials of caller.
4266 * ct - caller context
4268 * RETURN: 0 on success, error code on failure.
4271 * tdvp - ctime|mtime updated
4272 * svp - ctime updated
4276 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4277 caller_context_t *ct, int flags)
4279 znode_t *dzp = VTOZ(tdvp);
4281 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4290 boolean_t waited = B_FALSE;
4292 ASSERT(tdvp->v_type == VDIR);
4296 zilog = zfsvfs->z_log;
4298 if (VOP_REALVP(svp, &realvp, ct) == 0)
4302 * POSIX dictates that we return EPERM here.
4303 * Better choices include ENOTSUP or EISDIR.
4305 if (svp->v_type == VDIR) {
4307 return (SET_ERROR(EPERM));
4310 if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
4312 return (SET_ERROR(EXDEV));
4318 /* Prevent links to .zfs/shares files */
4320 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4321 &parent, sizeof (uint64_t))) != 0) {
4325 if (parent == zfsvfs->z_shares_dir) {
4327 return (SET_ERROR(EPERM));
4330 if (zfsvfs->z_utf8 && u8_validate(name,
4331 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4333 return (SET_ERROR(EILSEQ));
4335 if (flags & FIGNORECASE)
4339 * We do not support links between attributes and non-attributes
4340 * because of the potential security risk of creating links
4341 * into "normal" file space in order to circumvent restrictions
4342 * imposed in attribute space.
4344 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4346 return (SET_ERROR(EINVAL));
4350 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4351 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4353 return (SET_ERROR(EPERM));
4356 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4363 * Attempt to lock directory; fail if entry already exists.
4365 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4371 tx = dmu_tx_create(zfsvfs->z_os);
4372 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4373 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4374 zfs_sa_upgrade_txholds(tx, szp);
4375 zfs_sa_upgrade_txholds(tx, dzp);
4376 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4378 zfs_dirent_unlock(dl);
4379 if (error == ERESTART) {
4390 error = zfs_link_create(dl, szp, tx, 0);
4393 uint64_t txtype = TX_LINK;
4394 if (flags & FIGNORECASE)
4396 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4401 zfs_dirent_unlock(dl);
4404 vnevent_link(svp, ct);
4407 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4408 zil_commit(zilog, 0);
4416 * zfs_null_putapage() is used when the file system has been force
4417 * unmounted. It just drops the pages.
4421 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4422 size_t *lenp, int flags, cred_t *cr)
4424 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4429 * Push a page out to disk, klustering if possible.
4431 * IN: vp - file to push page to.
4432 * pp - page to push.
4433 * flags - additional flags.
4434 * cr - credentials of caller.
4436 * OUT: offp - start of range pushed.
4437 * lenp - len of range pushed.
4439 * RETURN: 0 on success, error code on failure.
4441 * NOTE: callers must have locked the page to be pushed. On
4442 * exit, the page (and all other pages in the kluster) must be
4447 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4448 size_t *lenp, int flags, cred_t *cr)
4450 znode_t *zp = VTOZ(vp);
4451 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4453 u_offset_t off, koff;
4460 * If our blocksize is bigger than the page size, try to kluster
4461 * multiple pages so that we write a full block (thus avoiding
4462 * a read-modify-write).
4464 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4465 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4466 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4467 ASSERT(koff <= zp->z_size);
4468 if (koff + klen > zp->z_size)
4469 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4470 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4472 ASSERT3U(btop(len), ==, btopr(len));
4475 * Can't push pages past end-of-file.
4477 if (off >= zp->z_size) {
4478 /* ignore all pages */
4481 } else if (off + len > zp->z_size) {
4482 int npages = btopr(zp->z_size - off);
4485 page_list_break(&pp, &trunc, npages);
4486 /* ignore pages past end of file */
4488 pvn_write_done(trunc, flags);
4489 len = zp->z_size - off;
4492 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4493 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4494 err = SET_ERROR(EDQUOT);
4497 tx = dmu_tx_create(zfsvfs->z_os);
4498 dmu_tx_hold_write(tx, zp->z_id, off, len);
4500 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4501 zfs_sa_upgrade_txholds(tx, zp);
4502 err = dmu_tx_assign(tx, TXG_WAIT);
4508 if (zp->z_blksz <= PAGESIZE) {
4509 caddr_t va = zfs_map_page(pp, S_READ);
4510 ASSERT3U(len, <=, PAGESIZE);
4511 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4512 zfs_unmap_page(pp, va);
4514 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4518 uint64_t mtime[2], ctime[2];
4519 sa_bulk_attr_t bulk[3];
4522 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4524 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4526 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4528 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4530 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4535 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4545 * Copy the portion of the file indicated from pages into the file.
4546 * The pages are stored in a page list attached to the files vnode.
4548 * IN: vp - vnode of file to push page data to.
4549 * off - position in file to put data.
4550 * len - amount of data to write.
4551 * flags - flags to control the operation.
4552 * cr - credentials of caller.
4553 * ct - caller context.
4555 * RETURN: 0 on success, error code on failure.
4558 * vp - ctime|mtime updated
4562 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4563 caller_context_t *ct)
4565 znode_t *zp = VTOZ(vp);
4566 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4578 * Align this request to the file block size in case we kluster.
4579 * XXX - this can result in pretty aggresive locking, which can
4580 * impact simultanious read/write access. One option might be
4581 * to break up long requests (len == 0) into block-by-block
4582 * operations to get narrower locking.
4584 blksz = zp->z_blksz;
4586 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4589 if (len > 0 && ISP2(blksz))
4590 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4596 * Search the entire vp list for pages >= io_off.
4598 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4599 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4602 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4604 if (off > zp->z_size) {
4605 /* past end of file */
4606 zfs_range_unlock(rl);
4611 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4613 for (off = io_off; io_off < off + len; io_off += io_len) {
4614 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4615 pp = page_lookup(vp, io_off,
4616 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4618 pp = page_lookup_nowait(vp, io_off,
4619 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4622 if (pp != NULL && pvn_getdirty(pp, flags)) {
4626 * Found a dirty page to push
4628 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4636 zfs_range_unlock(rl);
4637 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4638 zil_commit(zfsvfs->z_log, zp->z_id);
4646 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4648 znode_t *zp = VTOZ(vp);
4649 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4652 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4653 if (zp->z_sa_hdl == NULL) {
4655 * The fs has been unmounted, or we did a
4656 * suspend/resume and this file no longer exists.
4658 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4659 vrecycle(vp, curthread);
4663 mutex_enter(&zp->z_lock);
4664 if (zp->z_unlinked) {
4666 * Fast path to recycle a vnode of a removed file.
4668 mutex_exit(&zp->z_lock);
4669 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4670 vrecycle(vp, curthread);
4673 mutex_exit(&zp->z_lock);
4675 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4676 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4678 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4679 zfs_sa_upgrade_txholds(tx, zp);
4680 error = dmu_tx_assign(tx, TXG_WAIT);
4684 mutex_enter(&zp->z_lock);
4685 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4686 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4687 zp->z_atime_dirty = 0;
4688 mutex_exit(&zp->z_lock);
4692 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4697 * Bounds-check the seek operation.
4699 * IN: vp - vnode seeking within
4700 * ooff - old file offset
4701 * noffp - pointer to new file offset
4702 * ct - caller context
4704 * RETURN: 0 on success, EINVAL if new offset invalid.
4708 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4709 caller_context_t *ct)
4711 if (vp->v_type == VDIR)
4713 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4717 * Pre-filter the generic locking function to trap attempts to place
4718 * a mandatory lock on a memory mapped file.
4721 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4722 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4724 znode_t *zp = VTOZ(vp);
4725 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4731 * We are following the UFS semantics with respect to mapcnt
4732 * here: If we see that the file is mapped already, then we will
4733 * return an error, but we don't worry about races between this
4734 * function and zfs_map().
4736 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4738 return (SET_ERROR(EAGAIN));
4741 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4745 * If we can't find a page in the cache, we will create a new page
4746 * and fill it with file data. For efficiency, we may try to fill
4747 * multiple pages at once (klustering) to fill up the supplied page
4748 * list. Note that the pages to be filled are held with an exclusive
4749 * lock to prevent access by other threads while they are being filled.
4752 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4753 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4755 znode_t *zp = VTOZ(vp);
4756 page_t *pp, *cur_pp;
4757 objset_t *os = zp->z_zfsvfs->z_os;
4758 u_offset_t io_off, total;
4762 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4764 * We only have a single page, don't bother klustering
4768 pp = page_create_va(vp, io_off, io_len,
4769 PG_EXCL | PG_WAIT, seg, addr);
4772 * Try to find enough pages to fill the page list
4774 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4775 &io_len, off, plsz, 0);
4779 * The page already exists, nothing to do here.
4786 * Fill the pages in the kluster.
4789 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4792 ASSERT3U(io_off, ==, cur_pp->p_offset);
4793 va = zfs_map_page(cur_pp, S_WRITE);
4794 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4796 zfs_unmap_page(cur_pp, va);
4798 /* On error, toss the entire kluster */
4799 pvn_read_done(pp, B_ERROR);
4800 /* convert checksum errors into IO errors */
4802 err = SET_ERROR(EIO);
4805 cur_pp = cur_pp->p_next;
4809 * Fill in the page list array from the kluster starting
4810 * from the desired offset `off'.
4811 * NOTE: the page list will always be null terminated.
4813 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4814 ASSERT(pl == NULL || (*pl)->p_offset == off);
4820 * Return pointers to the pages for the file region [off, off + len]
4821 * in the pl array. If plsz is greater than len, this function may
4822 * also return page pointers from after the specified region
4823 * (i.e. the region [off, off + plsz]). These additional pages are
4824 * only returned if they are already in the cache, or were created as
4825 * part of a klustered read.
4827 * IN: vp - vnode of file to get data from.
4828 * off - position in file to get data from.
4829 * len - amount of data to retrieve.
4830 * plsz - length of provided page list.
4831 * seg - segment to obtain pages for.
4832 * addr - virtual address of fault.
4833 * rw - mode of created pages.
4834 * cr - credentials of caller.
4835 * ct - caller context.
4837 * OUT: protp - protection mode of created pages.
4838 * pl - list of pages created.
4840 * RETURN: 0 on success, error code on failure.
4843 * vp - atime updated
4847 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4848 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4849 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4851 znode_t *zp = VTOZ(vp);
4852 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4856 /* we do our own caching, faultahead is unnecessary */
4859 else if (len > plsz)
4862 len = P2ROUNDUP(len, PAGESIZE);
4863 ASSERT(plsz >= len);
4872 * Loop through the requested range [off, off + len) looking
4873 * for pages. If we don't find a page, we will need to create
4874 * a new page and fill it with data from the file.
4877 if (*pl = page_lookup(vp, off, SE_SHARED))
4879 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4882 ASSERT3U((*pl)->p_offset, ==, off);
4886 ASSERT3U(len, >=, PAGESIZE);
4889 ASSERT3U(plsz, >=, PAGESIZE);
4896 * Fill out the page array with any pages already in the cache.
4899 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4906 * Release any pages we have previously locked.
4911 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4921 * Request a memory map for a section of a file. This code interacts
4922 * with common code and the VM system as follows:
4924 * - common code calls mmap(), which ends up in smmap_common()
4925 * - this calls VOP_MAP(), which takes you into (say) zfs
4926 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4927 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4928 * - zfs_addmap() updates z_mapcnt
4932 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4933 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4934 caller_context_t *ct)
4936 znode_t *zp = VTOZ(vp);
4937 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4938 segvn_crargs_t vn_a;
4944 if ((prot & PROT_WRITE) && (zp->z_pflags &
4945 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4947 return (SET_ERROR(EPERM));
4950 if ((prot & (PROT_READ | PROT_EXEC)) &&
4951 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4953 return (SET_ERROR(EACCES));
4956 if (vp->v_flag & VNOMAP) {
4958 return (SET_ERROR(ENOSYS));
4961 if (off < 0 || len > MAXOFFSET_T - off) {
4963 return (SET_ERROR(ENXIO));
4966 if (vp->v_type != VREG) {
4968 return (SET_ERROR(ENODEV));
4972 * If file is locked, disallow mapping.
4974 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4976 return (SET_ERROR(EAGAIN));
4980 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4988 vn_a.offset = (u_offset_t)off;
4989 vn_a.type = flags & MAP_TYPE;
4991 vn_a.maxprot = maxprot;
4994 vn_a.flags = flags & ~MAP_TYPE;
4996 vn_a.lgrp_mem_policy_flags = 0;
4998 error = as_map(as, *addrp, len, segvn_create, &vn_a);
5007 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5008 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
5009 caller_context_t *ct)
5011 uint64_t pages = btopr(len);
5013 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
5018 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
5019 * more accurate mtime for the associated file. Since we don't have a way of
5020 * detecting when the data was actually modified, we have to resort to
5021 * heuristics. If an explicit msync() is done, then we mark the mtime when the
5022 * last page is pushed. The problem occurs when the msync() call is omitted,
5023 * which by far the most common case:
5031 * putpage() via fsflush
5033 * If we wait until fsflush to come along, we can have a modification time that
5034 * is some arbitrary point in the future. In order to prevent this in the
5035 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
5040 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5041 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
5042 caller_context_t *ct)
5044 uint64_t pages = btopr(len);
5046 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
5047 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
5049 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
5050 vn_has_cached_data(vp))
5051 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
5057 * Free or allocate space in a file. Currently, this function only
5058 * supports the `F_FREESP' command. However, this command is somewhat
5059 * misnamed, as its functionality includes the ability to allocate as
5060 * well as free space.
5062 * IN: vp - vnode of file to free data in.
5063 * cmd - action to take (only F_FREESP supported).
5064 * bfp - section of file to free/alloc.
5065 * flag - current file open mode flags.
5066 * offset - current file offset.
5067 * cr - credentials of caller [UNUSED].
5068 * ct - caller context.
5070 * RETURN: 0 on success, error code on failure.
5073 * vp - ctime|mtime updated
5077 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
5078 offset_t offset, cred_t *cr, caller_context_t *ct)
5080 znode_t *zp = VTOZ(vp);
5081 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5088 if (cmd != F_FREESP) {
5090 return (SET_ERROR(EINVAL));
5093 if (error = convoff(vp, bfp, 0, offset)) {
5098 if (bfp->l_len < 0) {
5100 return (SET_ERROR(EINVAL));
5104 len = bfp->l_len; /* 0 means from off to end of file */
5106 error = zfs_freesp(zp, off, len, flag, TRUE);
5113 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
5114 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
5118 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
5120 znode_t *zp = VTOZ(vp);
5121 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5124 uint64_t object = zp->z_id;
5131 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5132 &gen64, sizeof (uint64_t))) != 0) {
5137 gen = (uint32_t)gen64;
5139 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
5142 if (fidp->fid_len < size) {
5143 fidp->fid_len = size;
5145 return (SET_ERROR(ENOSPC));
5148 fidp->fid_len = size;
5151 zfid = (zfid_short_t *)fidp;
5153 zfid->zf_len = size;
5155 for (i = 0; i < sizeof (zfid->zf_object); i++)
5156 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5158 /* Must have a non-zero generation number to distinguish from .zfs */
5161 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5162 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5164 if (size == LONG_FID_LEN) {
5165 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
5168 zlfid = (zfid_long_t *)fidp;
5170 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
5171 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
5173 /* XXX - this should be the generation number for the objset */
5174 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
5175 zlfid->zf_setgen[i] = 0;
5183 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5184 caller_context_t *ct)
5196 case _PC_FILESIZEBITS:
5200 case _PC_XATTR_EXISTS:
5202 zfsvfs = zp->z_zfsvfs;
5206 error = zfs_dirent_lock(&dl, zp, "", &xzp,
5207 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
5209 zfs_dirent_unlock(dl);
5210 if (!zfs_dirempty(xzp))
5213 } else if (error == ENOENT) {
5215 * If there aren't extended attributes, it's the
5216 * same as having zero of them.
5223 case _PC_SATTR_ENABLED:
5224 case _PC_SATTR_EXISTS:
5225 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5226 (vp->v_type == VREG || vp->v_type == VDIR);
5229 case _PC_ACCESS_FILTERING:
5230 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5234 case _PC_ACL_ENABLED:
5235 *valp = _ACL_ACE_ENABLED;
5238 case _PC_MIN_HOLE_SIZE:
5239 *valp = (int)SPA_MINBLOCKSIZE;
5242 case _PC_TIMESTAMP_RESOLUTION:
5243 /* nanosecond timestamp resolution */
5247 case _PC_ACL_EXTENDED:
5255 case _PC_ACL_PATH_MAX:
5256 *valp = ACL_MAX_ENTRIES;
5260 return (EOPNOTSUPP);
5266 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5267 caller_context_t *ct)
5269 znode_t *zp = VTOZ(vp);
5270 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5272 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5276 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5284 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5285 caller_context_t *ct)
5287 znode_t *zp = VTOZ(vp);
5288 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5290 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5291 zilog_t *zilog = zfsvfs->z_log;
5296 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5298 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5299 zil_commit(zilog, 0);
5307 * The smallest read we may consider to loan out an arcbuf.
5308 * This must be a power of 2.
5310 int zcr_blksz_min = (1 << 10); /* 1K */
5312 * If set to less than the file block size, allow loaning out of an
5313 * arcbuf for a partial block read. This must be a power of 2.
5315 int zcr_blksz_max = (1 << 17); /* 128K */
5319 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5320 caller_context_t *ct)
5322 znode_t *zp = VTOZ(vp);
5323 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5324 int max_blksz = zfsvfs->z_max_blksz;
5325 uio_t *uio = &xuio->xu_uio;
5326 ssize_t size = uio->uio_resid;
5327 offset_t offset = uio->uio_loffset;
5332 int preamble, postamble;
5334 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5335 return (SET_ERROR(EINVAL));
5342 * Loan out an arc_buf for write if write size is bigger than
5343 * max_blksz, and the file's block size is also max_blksz.
5346 if (size < blksz || zp->z_blksz != blksz) {
5348 return (SET_ERROR(EINVAL));
5351 * Caller requests buffers for write before knowing where the
5352 * write offset might be (e.g. NFS TCP write).
5357 preamble = P2PHASE(offset, blksz);
5359 preamble = blksz - preamble;
5364 postamble = P2PHASE(size, blksz);
5367 fullblk = size / blksz;
5368 (void) dmu_xuio_init(xuio,
5369 (preamble != 0) + fullblk + (postamble != 0));
5370 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5371 int, postamble, int,
5372 (preamble != 0) + fullblk + (postamble != 0));
5375 * Have to fix iov base/len for partial buffers. They
5376 * currently represent full arc_buf's.
5379 /* data begins in the middle of the arc_buf */
5380 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5383 (void) dmu_xuio_add(xuio, abuf,
5384 blksz - preamble, preamble);
5387 for (i = 0; i < fullblk; i++) {
5388 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5391 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5395 /* data ends in the middle of the arc_buf */
5396 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5399 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5404 * Loan out an arc_buf for read if the read size is larger than
5405 * the current file block size. Block alignment is not
5406 * considered. Partial arc_buf will be loaned out for read.
5408 blksz = zp->z_blksz;
5409 if (blksz < zcr_blksz_min)
5410 blksz = zcr_blksz_min;
5411 if (blksz > zcr_blksz_max)
5412 blksz = zcr_blksz_max;
5413 /* avoid potential complexity of dealing with it */
5414 if (blksz > max_blksz) {
5416 return (SET_ERROR(EINVAL));
5419 maxsize = zp->z_size - uio->uio_loffset;
5423 if (size < blksz || vn_has_cached_data(vp)) {
5425 return (SET_ERROR(EINVAL));
5430 return (SET_ERROR(EINVAL));
5433 uio->uio_extflg = UIO_XUIO;
5434 XUIO_XUZC_RW(xuio) = ioflag;
5441 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5445 int ioflag = XUIO_XUZC_RW(xuio);
5447 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5449 i = dmu_xuio_cnt(xuio);
5451 abuf = dmu_xuio_arcbuf(xuio, i);
5453 * if abuf == NULL, it must be a write buffer
5454 * that has been returned in zfs_write().
5457 dmu_return_arcbuf(abuf);
5458 ASSERT(abuf || ioflag == UIO_WRITE);
5461 dmu_xuio_fini(xuio);
5466 * Predeclare these here so that the compiler assumes that
5467 * this is an "old style" function declaration that does
5468 * not include arguments => we won't get type mismatch errors
5469 * in the initializations that follow.
5471 static int zfs_inval();
5472 static int zfs_isdir();
5477 return (SET_ERROR(EINVAL));
5483 return (SET_ERROR(EISDIR));
5486 * Directory vnode operations template
5488 vnodeops_t *zfs_dvnodeops;
5489 const fs_operation_def_t zfs_dvnodeops_template[] = {
5490 VOPNAME_OPEN, { .vop_open = zfs_open },
5491 VOPNAME_CLOSE, { .vop_close = zfs_close },
5492 VOPNAME_READ, { .error = zfs_isdir },
5493 VOPNAME_WRITE, { .error = zfs_isdir },
5494 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5495 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5496 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5497 VOPNAME_ACCESS, { .vop_access = zfs_access },
5498 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5499 VOPNAME_CREATE, { .vop_create = zfs_create },
5500 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5501 VOPNAME_LINK, { .vop_link = zfs_link },
5502 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5503 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5504 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5505 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5506 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5507 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5508 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5509 VOPNAME_FID, { .vop_fid = zfs_fid },
5510 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5511 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5512 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5513 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5514 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5519 * Regular file vnode operations template
5521 vnodeops_t *zfs_fvnodeops;
5522 const fs_operation_def_t zfs_fvnodeops_template[] = {
5523 VOPNAME_OPEN, { .vop_open = zfs_open },
5524 VOPNAME_CLOSE, { .vop_close = zfs_close },
5525 VOPNAME_READ, { .vop_read = zfs_read },
5526 VOPNAME_WRITE, { .vop_write = zfs_write },
5527 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5528 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5529 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5530 VOPNAME_ACCESS, { .vop_access = zfs_access },
5531 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5532 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5533 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5534 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5535 VOPNAME_FID, { .vop_fid = zfs_fid },
5536 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5537 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5538 VOPNAME_SPACE, { .vop_space = zfs_space },
5539 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5540 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5541 VOPNAME_MAP, { .vop_map = zfs_map },
5542 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5543 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5544 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5545 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5546 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5547 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5548 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5549 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5554 * Symbolic link vnode operations template
5556 vnodeops_t *zfs_symvnodeops;
5557 const fs_operation_def_t zfs_symvnodeops_template[] = {
5558 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5559 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5560 VOPNAME_ACCESS, { .vop_access = zfs_access },
5561 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5562 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5563 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5564 VOPNAME_FID, { .vop_fid = zfs_fid },
5565 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5566 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5571 * special share hidden files vnode operations template
5573 vnodeops_t *zfs_sharevnodeops;
5574 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5575 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5576 VOPNAME_ACCESS, { .vop_access = zfs_access },
5577 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5578 VOPNAME_FID, { .vop_fid = zfs_fid },
5579 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5580 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5581 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5582 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5587 * Extended attribute directory vnode operations template
5589 * This template is identical to the directory vnodes
5590 * operation template except for restricted operations:
5594 * Note that there are other restrictions embedded in:
5595 * zfs_create() - restrict type to VREG
5596 * zfs_link() - no links into/out of attribute space
5597 * zfs_rename() - no moves into/out of attribute space
5599 vnodeops_t *zfs_xdvnodeops;
5600 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5601 VOPNAME_OPEN, { .vop_open = zfs_open },
5602 VOPNAME_CLOSE, { .vop_close = zfs_close },
5603 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5604 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5605 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5606 VOPNAME_ACCESS, { .vop_access = zfs_access },
5607 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5608 VOPNAME_CREATE, { .vop_create = zfs_create },
5609 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5610 VOPNAME_LINK, { .vop_link = zfs_link },
5611 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5612 VOPNAME_MKDIR, { .error = zfs_inval },
5613 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5614 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5615 VOPNAME_SYMLINK, { .error = zfs_inval },
5616 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5617 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5618 VOPNAME_FID, { .vop_fid = zfs_fid },
5619 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5620 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5621 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5622 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5623 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5628 * Error vnode operations template
5630 vnodeops_t *zfs_evnodeops;
5631 const fs_operation_def_t zfs_evnodeops_template[] = {
5632 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5633 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5639 ioflags(int ioflags)
5643 if (ioflags & IO_APPEND)
5645 if (ioflags & IO_NDELAY)
5647 if (ioflags & IO_SYNC)
5648 flags |= (FSYNC | FDSYNC | FRSYNC);
5654 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage)
5656 znode_t *zp = VTOZ(vp);
5657 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5658 objset_t *os = zp->z_zfsvfs->z_os;
5659 vm_page_t mfirst, mlast, mreq;
5663 off_t startoff, endoff;
5665 vm_pindex_t reqstart, reqend;
5666 int pcount, lsize, reqsize, size;
5671 pcount = OFF_TO_IDX(round_page(count));
5673 object = mreq->object;
5676 KASSERT(vp->v_object == object, ("mismatching object"));
5678 if (pcount > 1 && zp->z_blksz > PAGESIZE) {
5679 startoff = rounddown(IDX_TO_OFF(mreq->pindex), zp->z_blksz);
5680 reqstart = OFF_TO_IDX(round_page(startoff));
5681 if (reqstart < m[0]->pindex)
5684 reqstart = reqstart - m[0]->pindex;
5685 endoff = roundup(IDX_TO_OFF(mreq->pindex) + PAGE_SIZE,
5687 reqend = OFF_TO_IDX(trunc_page(endoff)) - 1;
5688 if (reqend > m[pcount - 1]->pindex)
5689 reqend = m[pcount - 1]->pindex;
5690 reqsize = reqend - m[reqstart]->pindex + 1;
5691 KASSERT(reqstart <= reqpage && reqpage < reqstart + reqsize,
5692 ("reqpage beyond [reqstart, reqstart + reqsize[ bounds"));
5697 mfirst = m[reqstart];
5698 mlast = m[reqstart + reqsize - 1];
5700 VM_OBJECT_LOCK(object);
5702 for (i = 0; i < reqstart; i++) {
5705 vm_page_unlock(m[i]);
5707 for (i = reqstart + reqsize; i < pcount; i++) {
5710 vm_page_unlock(m[i]);
5713 if (mreq->valid && reqsize == 1) {
5714 if (mreq->valid != VM_PAGE_BITS_ALL)
5715 vm_page_zero_invalid(mreq, TRUE);
5716 VM_OBJECT_UNLOCK(object);
5718 return (VM_PAGER_OK);
5721 PCPU_INC(cnt.v_vnodein);
5722 PCPU_ADD(cnt.v_vnodepgsin, reqsize);
5724 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) {
5725 for (i = reqstart; i < reqstart + reqsize; i++) {
5729 vm_page_unlock(m[i]);
5732 VM_OBJECT_UNLOCK(object);
5734 return (VM_PAGER_BAD);
5738 if (IDX_TO_OFF(mlast->pindex) + lsize > object->un_pager.vnp.vnp_size)
5739 lsize = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mlast->pindex);
5741 VM_OBJECT_UNLOCK(object);
5743 for (i = reqstart; i < reqstart + reqsize; i++) {
5745 if (i == (reqstart + reqsize - 1))
5747 va = zfs_map_page(m[i], &sf);
5748 error = dmu_read(os, zp->z_id, IDX_TO_OFF(m[i]->pindex),
5749 size, va, DMU_READ_PREFETCH);
5750 if (size != PAGE_SIZE)
5751 bzero(va + size, PAGE_SIZE - size);
5757 VM_OBJECT_LOCK(object);
5759 for (i = reqstart; i < reqstart + reqsize; i++) {
5761 m[i]->valid = VM_PAGE_BITS_ALL;
5762 KASSERT(m[i]->dirty == 0, ("zfs_getpages: page %p is dirty", m[i]));
5764 vm_page_readahead_finish(m[i]);
5767 VM_OBJECT_UNLOCK(object);
5769 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5771 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
5775 zfs_freebsd_getpages(ap)
5776 struct vop_getpages_args /* {
5781 vm_ooffset_t a_offset;
5785 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage));
5789 zfs_putpages(struct vnode *vp, vm_page_t *ma, size_t len, int flags,
5792 znode_t *zp = VTOZ(vp);
5793 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5802 vm_ooffset_t lo_off;
5813 object = vp->v_object;
5817 KASSERT(ma[0]->object == object, ("mismatching object"));
5818 KASSERT(len > 0 && (len & PAGE_MASK) == 0, ("unexpected length"));
5820 for (i = 0; i < pcount; i++)
5821 rtvals[i] = VM_PAGER_ERROR;
5823 off = IDX_TO_OFF(ma[0]->pindex);
5824 blksz = zp->z_blksz;
5825 lo_off = rounddown(off, blksz);
5826 lo_len = roundup(len + (off - lo_off), blksz);
5827 rl = zfs_range_lock(zp, lo_off, lo_len, RL_WRITER);
5829 VM_OBJECT_LOCK(object);
5830 if (len + off > object->un_pager.vnp.vnp_size) {
5831 if (object->un_pager.vnp.vnp_size > off) {
5834 len = object->un_pager.vnp.vnp_size - off;
5836 if ((pgoff = (int)len & PAGE_MASK) != 0) {
5838 * If the object is locked and the following
5839 * conditions hold, then the page's dirty
5840 * field cannot be concurrently changed by a
5844 KASSERT(m->busy > 0,
5845 ("zfs_putpages: page %p is not busy", m));
5846 KASSERT(!pmap_page_is_write_mapped(m),
5847 ("zfs_putpages: page %p is not read-only", m));
5848 vm_page_clear_dirty(m, pgoff, PAGE_SIZE -
5855 if (ncount < pcount) {
5856 for (i = ncount; i < pcount; i++) {
5857 rtvals[i] = VM_PAGER_BAD;
5861 VM_OBJECT_UNLOCK(object);
5866 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
5867 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
5872 tx = dmu_tx_create(zfsvfs->z_os);
5873 dmu_tx_hold_write(tx, zp->z_id, off, len);
5875 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
5876 zfs_sa_upgrade_txholds(tx, zp);
5877 err = dmu_tx_assign(tx, TXG_NOWAIT);
5879 if (err == ERESTART) {
5888 if (zp->z_blksz < PAGE_SIZE) {
5890 for (i = 0; len > 0; off += tocopy, len -= tocopy, i++) {
5891 tocopy = len > PAGE_SIZE ? PAGE_SIZE : len;
5892 va = zfs_map_page(ma[i], &sf);
5893 dmu_write(zfsvfs->z_os, zp->z_id, off, tocopy, va, tx);
5897 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, ma, tx);
5901 uint64_t mtime[2], ctime[2];
5902 sa_bulk_attr_t bulk[3];
5905 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
5907 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
5909 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
5911 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
5913 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
5915 VM_OBJECT_LOCK(object);
5916 for (i = 0; i < ncount; i++) {
5917 rtvals[i] = VM_PAGER_OK;
5918 vm_page_undirty(ma[i]);
5920 VM_OBJECT_UNLOCK(object);
5921 PCPU_INC(cnt.v_vnodeout);
5922 PCPU_ADD(cnt.v_vnodepgsout, ncount);
5927 zfs_range_unlock(rl);
5928 if ((flags & (VM_PAGER_PUT_SYNC | VM_PAGER_PUT_INVAL)) != 0 ||
5929 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5930 zil_commit(zfsvfs->z_log, zp->z_id);
5936 zfs_freebsd_putpages(ap)
5937 struct vop_putpages_args /* {
5943 vm_ooffset_t a_offset;
5947 return (zfs_putpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_sync,
5952 zfs_freebsd_bmap(ap)
5953 struct vop_bmap_args /* {
5956 struct bufobj **a_bop;
5963 if (ap->a_bop != NULL)
5964 *ap->a_bop = &ap->a_vp->v_bufobj;
5965 if (ap->a_bnp != NULL)
5966 *ap->a_bnp = ap->a_bn;
5967 if (ap->a_runp != NULL)
5969 if (ap->a_runb != NULL)
5976 zfs_freebsd_open(ap)
5977 struct vop_open_args /* {
5980 struct ucred *a_cred;
5981 struct thread *a_td;
5984 vnode_t *vp = ap->a_vp;
5985 znode_t *zp = VTOZ(vp);
5988 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
5990 vnode_create_vobject(vp, zp->z_size, ap->a_td);
5995 zfs_freebsd_close(ap)
5996 struct vop_close_args /* {
5999 struct ucred *a_cred;
6000 struct thread *a_td;
6004 return (zfs_close(ap->a_vp, ap->a_fflag, 1, 0, ap->a_cred, NULL));
6008 zfs_freebsd_ioctl(ap)
6009 struct vop_ioctl_args /* {
6019 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
6020 ap->a_fflag, ap->a_cred, NULL, NULL));
6024 zfs_freebsd_read(ap)
6025 struct vop_read_args /* {
6029 struct ucred *a_cred;
6033 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6038 zfs_freebsd_write(ap)
6039 struct vop_write_args /* {
6043 struct ucred *a_cred;
6047 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
6052 zfs_freebsd_access(ap)
6053 struct vop_access_args /* {
6055 accmode_t a_accmode;
6056 struct ucred *a_cred;
6057 struct thread *a_td;
6060 vnode_t *vp = ap->a_vp;
6061 znode_t *zp = VTOZ(vp);
6066 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
6068 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
6070 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
6073 * VADMIN has to be handled by vaccess().
6076 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
6078 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
6079 zp->z_gid, accmode, ap->a_cred, NULL);
6084 * For VEXEC, ensure that at least one execute bit is set for
6087 if (error == 0 && (ap->a_accmode & VEXEC) != 0 && vp->v_type != VDIR &&
6088 (zp->z_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) {
6096 zfs_freebsd_lookup(ap)
6097 struct vop_lookup_args /* {
6098 struct vnode *a_dvp;
6099 struct vnode **a_vpp;
6100 struct componentname *a_cnp;
6103 struct componentname *cnp = ap->a_cnp;
6104 char nm[NAME_MAX + 1];
6106 ASSERT(cnp->cn_namelen < sizeof(nm));
6107 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
6109 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
6110 cnp->cn_cred, cnp->cn_thread, 0));
6114 zfs_freebsd_create(ap)
6115 struct vop_create_args /* {
6116 struct vnode *a_dvp;
6117 struct vnode **a_vpp;
6118 struct componentname *a_cnp;
6119 struct vattr *a_vap;
6122 struct componentname *cnp = ap->a_cnp;
6123 vattr_t *vap = ap->a_vap;
6126 ASSERT(cnp->cn_flags & SAVENAME);
6128 vattr_init_mask(vap);
6129 mode = vap->va_mode & ALLPERMS;
6131 return (zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
6132 ap->a_vpp, cnp->cn_cred, cnp->cn_thread));
6136 zfs_freebsd_remove(ap)
6137 struct vop_remove_args /* {
6138 struct vnode *a_dvp;
6140 struct componentname *a_cnp;
6144 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6146 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
6147 ap->a_cnp->cn_cred, NULL, 0));
6151 zfs_freebsd_mkdir(ap)
6152 struct vop_mkdir_args /* {
6153 struct vnode *a_dvp;
6154 struct vnode **a_vpp;
6155 struct componentname *a_cnp;
6156 struct vattr *a_vap;
6159 vattr_t *vap = ap->a_vap;
6161 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
6163 vattr_init_mask(vap);
6165 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
6166 ap->a_cnp->cn_cred, NULL, 0, NULL));
6170 zfs_freebsd_rmdir(ap)
6171 struct vop_rmdir_args /* {
6172 struct vnode *a_dvp;
6174 struct componentname *a_cnp;
6177 struct componentname *cnp = ap->a_cnp;
6179 ASSERT(cnp->cn_flags & SAVENAME);
6181 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
6185 zfs_freebsd_readdir(ap)
6186 struct vop_readdir_args /* {
6189 struct ucred *a_cred;
6196 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
6197 ap->a_ncookies, ap->a_cookies));
6201 zfs_freebsd_fsync(ap)
6202 struct vop_fsync_args /* {
6205 struct thread *a_td;
6210 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
6214 zfs_freebsd_getattr(ap)
6215 struct vop_getattr_args /* {
6217 struct vattr *a_vap;
6218 struct ucred *a_cred;
6221 vattr_t *vap = ap->a_vap;
6227 xvap.xva_vattr = *vap;
6228 xvap.xva_vattr.va_mask |= AT_XVATTR;
6230 /* Convert chflags into ZFS-type flags. */
6231 /* XXX: what about SF_SETTABLE?. */
6232 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
6233 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
6234 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
6235 XVA_SET_REQ(&xvap, XAT_NODUMP);
6236 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
6240 /* Convert ZFS xattr into chflags. */
6241 #define FLAG_CHECK(fflag, xflag, xfield) do { \
6242 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
6243 fflags |= (fflag); \
6245 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
6246 xvap.xva_xoptattrs.xoa_immutable);
6247 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
6248 xvap.xva_xoptattrs.xoa_appendonly);
6249 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
6250 xvap.xva_xoptattrs.xoa_nounlink);
6251 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
6252 xvap.xva_xoptattrs.xoa_nodump);
6254 *vap = xvap.xva_vattr;
6255 vap->va_flags = fflags;
6260 zfs_freebsd_setattr(ap)
6261 struct vop_setattr_args /* {
6263 struct vattr *a_vap;
6264 struct ucred *a_cred;
6267 vnode_t *vp = ap->a_vp;
6268 vattr_t *vap = ap->a_vap;
6269 cred_t *cred = ap->a_cred;
6274 vattr_init_mask(vap);
6275 vap->va_mask &= ~AT_NOSET;
6278 xvap.xva_vattr = *vap;
6280 zflags = VTOZ(vp)->z_pflags;
6282 if (vap->va_flags != VNOVAL) {
6283 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
6286 if (zfsvfs->z_use_fuids == B_FALSE)
6287 return (EOPNOTSUPP);
6289 fflags = vap->va_flags;
6290 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_NODUMP)) != 0)
6291 return (EOPNOTSUPP);
6293 * Unprivileged processes are not permitted to unset system
6294 * flags, or modify flags if any system flags are set.
6295 * Privileged non-jail processes may not modify system flags
6296 * if securelevel > 0 and any existing system flags are set.
6297 * Privileged jail processes behave like privileged non-jail
6298 * processes if the security.jail.chflags_allowed sysctl is
6299 * is non-zero; otherwise, they behave like unprivileged
6302 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
6303 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
6305 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6306 error = securelevel_gt(cred, 0);
6312 * Callers may only modify the file flags on objects they
6313 * have VADMIN rights for.
6315 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
6318 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
6322 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
6327 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
6328 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
6329 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
6330 XVA_SET_REQ(&xvap, (xflag)); \
6331 (xfield) = ((fflags & (fflag)) != 0); \
6334 /* Convert chflags into ZFS-type flags. */
6335 /* XXX: what about SF_SETTABLE?. */
6336 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
6337 xvap.xva_xoptattrs.xoa_immutable);
6338 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
6339 xvap.xva_xoptattrs.xoa_appendonly);
6340 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
6341 xvap.xva_xoptattrs.xoa_nounlink);
6342 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
6343 xvap.xva_xoptattrs.xoa_nodump);
6346 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
6350 zfs_freebsd_rename(ap)
6351 struct vop_rename_args /* {
6352 struct vnode *a_fdvp;
6353 struct vnode *a_fvp;
6354 struct componentname *a_fcnp;
6355 struct vnode *a_tdvp;
6356 struct vnode *a_tvp;
6357 struct componentname *a_tcnp;
6360 vnode_t *fdvp = ap->a_fdvp;
6361 vnode_t *fvp = ap->a_fvp;
6362 vnode_t *tdvp = ap->a_tdvp;
6363 vnode_t *tvp = ap->a_tvp;
6366 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
6367 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
6369 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
6370 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
6385 zfs_freebsd_symlink(ap)
6386 struct vop_symlink_args /* {
6387 struct vnode *a_dvp;
6388 struct vnode **a_vpp;
6389 struct componentname *a_cnp;
6390 struct vattr *a_vap;
6394 struct componentname *cnp = ap->a_cnp;
6395 vattr_t *vap = ap->a_vap;
6397 ASSERT(cnp->cn_flags & SAVENAME);
6399 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
6400 vattr_init_mask(vap);
6402 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
6403 ap->a_target, cnp->cn_cred, cnp->cn_thread));
6407 zfs_freebsd_readlink(ap)
6408 struct vop_readlink_args /* {
6411 struct ucred *a_cred;
6415 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
6419 zfs_freebsd_link(ap)
6420 struct vop_link_args /* {
6421 struct vnode *a_tdvp;
6423 struct componentname *a_cnp;
6426 struct componentname *cnp = ap->a_cnp;
6428 ASSERT(cnp->cn_flags & SAVENAME);
6430 return (zfs_link(ap->a_tdvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
6434 zfs_freebsd_inactive(ap)
6435 struct vop_inactive_args /* {
6437 struct thread *a_td;
6440 vnode_t *vp = ap->a_vp;
6442 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
6447 zfs_freebsd_reclaim(ap)
6448 struct vop_reclaim_args /* {
6450 struct thread *a_td;
6453 vnode_t *vp = ap->a_vp;
6454 znode_t *zp = VTOZ(vp);
6455 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6459 /* Destroy the vm object and flush associated pages. */
6460 vnode_destroy_vobject(vp);
6463 * z_teardown_inactive_lock protects from a race with
6464 * zfs_znode_dmu_fini in zfsvfs_teardown during
6467 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6468 if (zp->z_sa_hdl == NULL)
6472 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6480 struct vop_fid_args /* {
6486 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
6490 zfs_freebsd_pathconf(ap)
6491 struct vop_pathconf_args /* {
6494 register_t *a_retval;
6500 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
6502 *ap->a_retval = val;
6503 else if (error == EOPNOTSUPP)
6504 error = vop_stdpathconf(ap);
6509 zfs_freebsd_fifo_pathconf(ap)
6510 struct vop_pathconf_args /* {
6513 register_t *a_retval;
6517 switch (ap->a_name) {
6518 case _PC_ACL_EXTENDED:
6520 case _PC_ACL_PATH_MAX:
6521 case _PC_MAC_PRESENT:
6522 return (zfs_freebsd_pathconf(ap));
6524 return (fifo_specops.vop_pathconf(ap));
6529 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
6530 * extended attribute name:
6533 * system freebsd:system:
6534 * user (none, can be used to access ZFS fsattr(5) attributes
6535 * created on Solaris)
6538 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
6541 const char *namespace, *prefix, *suffix;
6543 /* We don't allow '/' character in attribute name. */
6544 if (strchr(name, '/') != NULL)
6546 /* We don't allow attribute names that start with "freebsd:" string. */
6547 if (strncmp(name, "freebsd:", 8) == 0)
6550 bzero(attrname, size);
6552 switch (attrnamespace) {
6553 case EXTATTR_NAMESPACE_USER:
6555 prefix = "freebsd:";
6556 namespace = EXTATTR_NAMESPACE_USER_STRING;
6560 * This is the default namespace by which we can access all
6561 * attributes created on Solaris.
6563 prefix = namespace = suffix = "";
6566 case EXTATTR_NAMESPACE_SYSTEM:
6567 prefix = "freebsd:";
6568 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
6571 case EXTATTR_NAMESPACE_EMPTY:
6575 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
6577 return (ENAMETOOLONG);
6583 * Vnode operating to retrieve a named extended attribute.
6586 zfs_getextattr(struct vop_getextattr_args *ap)
6589 IN struct vnode *a_vp;
6590 IN int a_attrnamespace;
6591 IN const char *a_name;
6592 INOUT struct uio *a_uio;
6594 IN struct ucred *a_cred;
6595 IN struct thread *a_td;
6599 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6600 struct thread *td = ap->a_td;
6601 struct nameidata nd;
6604 vnode_t *xvp = NULL, *vp;
6607 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6608 ap->a_cred, ap->a_td, VREAD);
6612 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6619 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6627 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
6629 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
6631 NDFREE(&nd, NDF_ONLY_PNBUF);
6634 if (error == ENOENT)
6639 if (ap->a_size != NULL) {
6640 error = VOP_GETATTR(vp, &va, ap->a_cred);
6642 *ap->a_size = (size_t)va.va_size;
6643 } else if (ap->a_uio != NULL)
6644 error = VOP_READ(vp, ap->a_uio, IO_UNIT, ap->a_cred);
6647 vn_close(vp, flags, ap->a_cred, td);
6654 * Vnode operation to remove a named attribute.
6657 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6660 IN struct vnode *a_vp;
6661 IN int a_attrnamespace;
6662 IN const char *a_name;
6663 IN struct ucred *a_cred;
6664 IN struct thread *a_td;
6668 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6669 struct thread *td = ap->a_td;
6670 struct nameidata nd;
6673 vnode_t *xvp = NULL, *vp;
6676 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6677 ap->a_cred, ap->a_td, VWRITE);
6681 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6688 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6695 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF | MPSAFE,
6696 UIO_SYSSPACE, attrname, xvp, td);
6701 NDFREE(&nd, NDF_ONLY_PNBUF);
6702 if (error == ENOENT)
6707 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6708 NDFREE(&nd, NDF_ONLY_PNBUF);
6711 if (vp == nd.ni_dvp)
6721 * Vnode operation to set a named attribute.
6724 zfs_setextattr(struct vop_setextattr_args *ap)
6727 IN struct vnode *a_vp;
6728 IN int a_attrnamespace;
6729 IN const char *a_name;
6730 INOUT struct uio *a_uio;
6731 IN struct ucred *a_cred;
6732 IN struct thread *a_td;
6736 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6737 struct thread *td = ap->a_td;
6738 struct nameidata nd;
6741 vnode_t *xvp = NULL, *vp;
6744 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6745 ap->a_cred, ap->a_td, VWRITE);
6749 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6756 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6757 LOOKUP_XATTR | CREATE_XATTR_DIR);
6763 flags = FFLAGS(O_WRONLY | O_CREAT);
6764 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
6766 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
6768 NDFREE(&nd, NDF_ONLY_PNBUF);
6776 error = VOP_SETATTR(vp, &va, ap->a_cred);
6778 VOP_WRITE(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
6781 vn_close(vp, flags, ap->a_cred, td);
6788 * Vnode operation to retrieve extended attributes on a vnode.
6791 zfs_listextattr(struct vop_listextattr_args *ap)
6794 IN struct vnode *a_vp;
6795 IN int a_attrnamespace;
6796 INOUT struct uio *a_uio;
6798 IN struct ucred *a_cred;
6799 IN struct thread *a_td;
6803 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6804 struct thread *td = ap->a_td;
6805 struct nameidata nd;
6806 char attrprefix[16];
6807 u_char dirbuf[sizeof(struct dirent)];
6810 struct uio auio, *uio = ap->a_uio;
6811 size_t *sizep = ap->a_size;
6813 vnode_t *xvp = NULL, *vp;
6814 int done, error, eof, pos;
6816 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6817 ap->a_cred, ap->a_td, VREAD);
6821 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6822 sizeof(attrprefix));
6825 plen = strlen(attrprefix);
6832 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6837 * ENOATTR means that the EA directory does not yet exist,
6838 * i.e. there are no extended attributes there.
6840 if (error == ENOATTR)
6845 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
6846 UIO_SYSSPACE, ".", xvp, td);
6849 NDFREE(&nd, NDF_ONLY_PNBUF);
6855 auio.uio_iov = &aiov;
6856 auio.uio_iovcnt = 1;
6857 auio.uio_segflg = UIO_SYSSPACE;
6859 auio.uio_rw = UIO_READ;
6860 auio.uio_offset = 0;
6865 aiov.iov_base = (void *)dirbuf;
6866 aiov.iov_len = sizeof(dirbuf);
6867 auio.uio_resid = sizeof(dirbuf);
6868 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
6869 done = sizeof(dirbuf) - auio.uio_resid;
6872 for (pos = 0; pos < done;) {
6873 dp = (struct dirent *)(dirbuf + pos);
6874 pos += dp->d_reclen;
6876 * XXX: Temporarily we also accept DT_UNKNOWN, as this
6877 * is what we get when attribute was created on Solaris.
6879 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
6881 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
6883 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
6885 nlen = dp->d_namlen - plen;
6888 else if (uio != NULL) {
6890 * Format of extattr name entry is one byte for
6891 * length and the rest for name.
6893 error = uiomove(&nlen, 1, uio->uio_rw, uio);
6895 error = uiomove(dp->d_name + plen, nlen,
6902 } while (!eof && error == 0);
6911 zfs_freebsd_getacl(ap)
6912 struct vop_getacl_args /* {
6921 vsecattr_t vsecattr;
6923 if (ap->a_type != ACL_TYPE_NFS4)
6926 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6927 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
6930 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
6931 if (vsecattr.vsa_aclentp != NULL)
6932 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6938 zfs_freebsd_setacl(ap)
6939 struct vop_setacl_args /* {
6948 vsecattr_t vsecattr;
6949 int aclbsize; /* size of acl list in bytes */
6952 if (ap->a_type != ACL_TYPE_NFS4)
6955 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6959 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6960 * splitting every entry into two and appending "canonical six"
6961 * entries at the end. Don't allow for setting an ACL that would
6962 * cause chmod(2) to run out of ACL entries.
6964 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6967 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6971 vsecattr.vsa_mask = VSA_ACE;
6972 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
6973 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6974 aaclp = vsecattr.vsa_aclentp;
6975 vsecattr.vsa_aclentsz = aclbsize;
6977 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6978 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
6979 kmem_free(aaclp, aclbsize);
6985 zfs_freebsd_aclcheck(ap)
6986 struct vop_aclcheck_args /* {
6995 return (EOPNOTSUPP);
6998 struct vop_vector zfs_vnodeops;
6999 struct vop_vector zfs_fifoops;
7000 struct vop_vector zfs_shareops;
7002 struct vop_vector zfs_vnodeops = {
7003 .vop_default = &default_vnodeops,
7004 .vop_inactive = zfs_freebsd_inactive,
7005 .vop_reclaim = zfs_freebsd_reclaim,
7006 .vop_access = zfs_freebsd_access,
7007 #ifdef FREEBSD_NAMECACHE
7008 .vop_lookup = vfs_cache_lookup,
7009 .vop_cachedlookup = zfs_freebsd_lookup,
7011 .vop_lookup = zfs_freebsd_lookup,
7013 .vop_getattr = zfs_freebsd_getattr,
7014 .vop_setattr = zfs_freebsd_setattr,
7015 .vop_create = zfs_freebsd_create,
7016 .vop_mknod = zfs_freebsd_create,
7017 .vop_mkdir = zfs_freebsd_mkdir,
7018 .vop_readdir = zfs_freebsd_readdir,
7019 .vop_fsync = zfs_freebsd_fsync,
7020 .vop_open = zfs_freebsd_open,
7021 .vop_close = zfs_freebsd_close,
7022 .vop_rmdir = zfs_freebsd_rmdir,
7023 .vop_ioctl = zfs_freebsd_ioctl,
7024 .vop_link = zfs_freebsd_link,
7025 .vop_symlink = zfs_freebsd_symlink,
7026 .vop_readlink = zfs_freebsd_readlink,
7027 .vop_read = zfs_freebsd_read,
7028 .vop_write = zfs_freebsd_write,
7029 .vop_remove = zfs_freebsd_remove,
7030 .vop_rename = zfs_freebsd_rename,
7031 .vop_pathconf = zfs_freebsd_pathconf,
7032 .vop_bmap = zfs_freebsd_bmap,
7033 .vop_fid = zfs_freebsd_fid,
7034 .vop_getextattr = zfs_getextattr,
7035 .vop_deleteextattr = zfs_deleteextattr,
7036 .vop_setextattr = zfs_setextattr,
7037 .vop_listextattr = zfs_listextattr,
7038 .vop_getacl = zfs_freebsd_getacl,
7039 .vop_setacl = zfs_freebsd_setacl,
7040 .vop_aclcheck = zfs_freebsd_aclcheck,
7041 .vop_getpages = zfs_freebsd_getpages,
7042 .vop_putpages = zfs_freebsd_putpages,
7045 struct vop_vector zfs_fifoops = {
7046 .vop_default = &fifo_specops,
7047 .vop_fsync = zfs_freebsd_fsync,
7048 .vop_access = zfs_freebsd_access,
7049 .vop_getattr = zfs_freebsd_getattr,
7050 .vop_inactive = zfs_freebsd_inactive,
7051 .vop_read = VOP_PANIC,
7052 .vop_reclaim = zfs_freebsd_reclaim,
7053 .vop_setattr = zfs_freebsd_setattr,
7054 .vop_write = VOP_PANIC,
7055 .vop_pathconf = zfs_freebsd_fifo_pathconf,
7056 .vop_fid = zfs_freebsd_fid,
7057 .vop_getacl = zfs_freebsd_getacl,
7058 .vop_setacl = zfs_freebsd_setacl,
7059 .vop_aclcheck = zfs_freebsd_aclcheck,
7063 * special share hidden files vnode operations template
7065 struct vop_vector zfs_shareops = {
7066 .vop_default = &default_vnodeops,
7067 .vop_access = zfs_freebsd_access,
7068 .vop_inactive = zfs_freebsd_inactive,
7069 .vop_reclaim = zfs_freebsd_reclaim,
7070 .vop_fid = zfs_freebsd_fid,
7071 .vop_pathconf = zfs_freebsd_pathconf,