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.
25 /* Portions Copyright 2007 Jeremy Teo */
26 /* Portions Copyright 2010 Robert Milkowski */
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/resourcevar.h>
36 #include <sys/vnode.h>
40 #include <sys/taskq.h>
42 #include <sys/atomic.h>
43 #include <sys/namei.h>
45 #include <sys/cmn_err.h>
46 #include <sys/errno.h>
47 #include <sys/unistd.h>
48 #include <sys/zfs_dir.h>
49 #include <sys/zfs_ioctl.h>
50 #include <sys/fs/zfs.h>
52 #include <sys/dmu_objset.h>
58 #include <sys/dirent.h>
59 #include <sys/policy.h>
60 #include <sys/sunddi.h>
61 #include <sys/filio.h>
63 #include <sys/zfs_ctldir.h>
64 #include <sys/zfs_fuid.h>
65 #include <sys/zfs_sa.h>
67 #include <sys/zfs_rlock.h>
68 #include <sys/extdirent.h>
69 #include <sys/kidmap.h>
72 #include <sys/sf_buf.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) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
108 * This is critical because we don't want to block while holding locks.
109 * Note, in particular, that if a lock is sometimes acquired before
110 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
111 * use a non-blocking assign can deadlock the system. The scenario:
113 * Thread A has grabbed a lock before calling dmu_tx_assign().
114 * Thread B is in an already-assigned tx, and blocks for this lock.
115 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
116 * forever, because the previous txg can't quiesce until B's tx commits.
118 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
119 * then drop all locks, call dmu_tx_wait(), and try again.
121 * (5) If the operation succeeded, generate the intent log entry for it
122 * before dropping locks. This ensures that the ordering of events
123 * in the intent log matches the order in which they actually occurred.
124 * During ZIL replay the zfs_log_* functions will update the sequence
125 * number to indicate the zil transaction has replayed.
127 * (6) At the end of each vnode op, the DMU tx must always commit,
128 * regardless of whether there were any errors.
130 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
131 * to ensure that synchronous semantics are provided when necessary.
133 * In general, this is how things should be ordered in each vnode op:
135 * ZFS_ENTER(zfsvfs); // exit if unmounted
137 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
138 * rw_enter(...); // grab any other locks you need
139 * tx = dmu_tx_create(...); // get DMU tx
140 * dmu_tx_hold_*(); // hold each object you might modify
141 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
143 * rw_exit(...); // drop locks
144 * zfs_dirent_unlock(dl); // unlock directory entry
145 * VN_RELE(...); // release held vnodes
146 * if (error == ERESTART) {
151 * dmu_tx_abort(tx); // abort DMU tx
152 * ZFS_EXIT(zfsvfs); // finished in zfs
153 * return (error); // really out of space
155 * error = do_real_work(); // do whatever this VOP does
157 * zfs_log_*(...); // on success, make ZIL entry
158 * dmu_tx_commit(tx); // commit DMU tx -- error or not
159 * rw_exit(...); // drop locks
160 * zfs_dirent_unlock(dl); // unlock directory entry
161 * VN_RELE(...); // release held vnodes
162 * zil_commit(zilog, foid); // synchronous when necessary
163 * ZFS_EXIT(zfsvfs); // finished in zfs
164 * return (error); // done, report error
169 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
171 znode_t *zp = VTOZ(*vpp);
172 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
177 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
178 ((flag & FAPPEND) == 0)) {
183 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
184 ZTOV(zp)->v_type == VREG &&
185 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
186 if (fs_vscan(*vpp, cr, 0) != 0) {
192 /* Keep a count of the synchronous opens in the znode */
193 if (flag & (FSYNC | FDSYNC))
194 atomic_inc_32(&zp->z_sync_cnt);
202 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
203 caller_context_t *ct)
205 znode_t *zp = VTOZ(vp);
206 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
209 * Clean up any locks held by this process on the vp.
211 cleanlocks(vp, ddi_get_pid(), 0);
212 cleanshares(vp, ddi_get_pid());
217 /* Decrement the synchronous opens in the znode */
218 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
219 atomic_dec_32(&zp->z_sync_cnt);
221 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
222 ZTOV(zp)->v_type == VREG &&
223 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
224 VERIFY(fs_vscan(vp, cr, 1) == 0);
231 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
232 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
235 zfs_holey(vnode_t *vp, u_long cmd, offset_t *off)
237 znode_t *zp = VTOZ(vp);
238 uint64_t noff = (uint64_t)*off; /* new offset */
243 file_sz = zp->z_size;
244 if (noff >= file_sz) {
248 if (cmd == _FIO_SEEK_HOLE)
253 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
256 if ((error == ESRCH) || (noff > file_sz)) {
258 * Handle the virtual hole at the end of file.
275 zfs_ioctl(vnode_t *vp, u_long com, intptr_t data, int flag, cred_t *cred,
276 int *rvalp, caller_context_t *ct)
288 * The following two ioctls are used by bfu. Faking out,
289 * necessary to avoid bfu errors.
297 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
301 zfsvfs = zp->z_zfsvfs;
305 /* offset parameter is in/out */
306 error = zfs_holey(vp, com, &off);
310 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
318 page_lookup(vnode_t *vp, int64_t start, int64_t off, int64_t nbytes)
324 VM_OBJECT_LOCK_ASSERT(obj, MA_OWNED);
327 if ((pp = vm_page_lookup(obj, OFF_TO_IDX(start))) != NULL &&
328 vm_page_is_valid(pp, (vm_offset_t)off, nbytes)) {
329 if (vm_page_sleep_if_busy(pp, FALSE, "zfsmwb"))
332 vm_page_lock_queues();
334 vm_page_unlock_queues();
336 if (__predict_false(obj->cache != NULL)) {
337 vm_page_cache_free(obj, OFF_TO_IDX(start),
338 OFF_TO_IDX(start) + 1);
348 page_unlock(vm_page_t pp)
355 zfs_map_page(vm_page_t pp, struct sf_buf **sfp)
358 *sfp = sf_buf_alloc(pp, 0);
359 return ((caddr_t)sf_buf_kva(*sfp));
363 zfs_unmap_page(struct sf_buf *sf)
370 * When a file is memory mapped, we must keep the IO data synchronized
371 * between the DMU cache and the memory mapped pages. What this means:
373 * On Write: If we find a memory mapped page, we write to *both*
374 * the page and the dmu buffer.
377 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid,
378 int segflg, dmu_tx_t *tx)
384 ASSERT(vp->v_mount != NULL);
388 off = start & PAGEOFFSET;
390 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
392 int nbytes = MIN(PAGESIZE - off, len);
394 if ((pp = page_lookup(vp, start, off, nbytes)) != NULL) {
397 VM_OBJECT_UNLOCK(obj);
398 va = zfs_map_page(pp, &sf);
399 if (segflg == UIO_NOCOPY) {
400 (void) dmu_write(os, oid, start+off, nbytes,
403 (void) dmu_read(os, oid, start+off, nbytes,
404 va+off, DMU_READ_PREFETCH);
413 VM_OBJECT_UNLOCK(obj);
417 * Read with UIO_NOCOPY flag means that sendfile(2) requests
418 * ZFS to populate a range of page cache pages with data.
420 * NOTE: this function could be optimized to pre-allocate
421 * all pages in advance, drain VPO_BUSY on all of them,
422 * map them into contiguous KVA region and populate them
423 * in one single dmu_read() call.
426 mappedread_sf(vnode_t *vp, int nbytes, uio_t *uio)
428 znode_t *zp = VTOZ(vp);
429 objset_t *os = zp->z_zfsvfs->z_os;
439 ASSERT(uio->uio_segflg == UIO_NOCOPY);
440 ASSERT(vp->v_mount != NULL);
443 ASSERT((uio->uio_loffset & PAGEOFFSET) == 0);
446 for (start = uio->uio_loffset; len > 0; start += PAGESIZE) {
447 int bytes = MIN(PAGESIZE, len);
450 pp = vm_page_lookup(obj, OFF_TO_IDX(start));
451 if (pp != NULL && vm_page_sleep_if_busy(pp, FALSE,
455 pp = vm_page_alloc(obj, OFF_TO_IDX(start),
456 VM_ALLOC_NOBUSY | VM_ALLOC_NORMAL);
458 VM_OBJECT_UNLOCK(obj);
464 if (pp->valid == 0) {
465 vm_page_io_start(pp);
466 VM_OBJECT_UNLOCK(obj);
467 va = zfs_map_page(pp, &sf);
468 error = dmu_read(os, zp->z_id, start, bytes, va,
470 if (bytes != PAGESIZE && error == 0)
471 bzero(va + bytes, PAGESIZE - bytes);
474 vm_page_io_finish(pp);
475 vm_page_lock_queues();
479 pp->valid = VM_PAGE_BITS_ALL;
480 vm_page_activate(pp);
482 vm_page_unlock_queues();
486 uio->uio_resid -= bytes;
487 uio->uio_offset += bytes;
490 VM_OBJECT_UNLOCK(obj);
495 * When a file is memory mapped, we must keep the IO data synchronized
496 * between the DMU cache and the memory mapped pages. What this means:
498 * On Read: We "read" preferentially from memory mapped pages,
499 * else we default from the dmu buffer.
501 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
502 * the file is memory mapped.
505 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
507 znode_t *zp = VTOZ(vp);
508 objset_t *os = zp->z_zfsvfs->z_os;
516 ASSERT(vp->v_mount != NULL);
520 start = uio->uio_loffset;
521 off = start & PAGEOFFSET;
523 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
525 uint64_t bytes = MIN(PAGESIZE - off, len);
527 if (pp = page_lookup(vp, start, off, bytes)) {
530 VM_OBJECT_UNLOCK(obj);
531 va = zfs_map_page(pp, &sf);
532 error = uiomove(va + off, bytes, UIO_READ, uio);
537 VM_OBJECT_UNLOCK(obj);
538 error = dmu_read_uio(os, zp->z_id, uio, bytes);
546 VM_OBJECT_UNLOCK(obj);
550 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
553 * Read bytes from specified file into supplied buffer.
555 * IN: vp - vnode of file to be read from.
556 * uio - structure supplying read location, range info,
558 * ioflag - SYNC flags; used to provide FRSYNC semantics.
559 * cr - credentials of caller.
560 * ct - caller context
562 * OUT: uio - updated offset and range, buffer filled.
564 * RETURN: 0 if success
565 * error code if failure
568 * vp - atime updated if byte count > 0
572 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
574 znode_t *zp = VTOZ(vp);
575 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
586 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
592 * Validate file offset
594 if (uio->uio_loffset < (offset_t)0) {
600 * Fasttrack empty reads
602 if (uio->uio_resid == 0) {
608 * Check for mandatory locks
610 if (MANDMODE(zp->z_mode)) {
611 if (error = chklock(vp, FREAD,
612 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
619 * If we're in FRSYNC mode, sync out this znode before reading it.
621 if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
622 zil_commit(zfsvfs->z_log, zp->z_id);
625 * Lock the range against changes.
627 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
630 * If we are reading past end-of-file we can skip
631 * to the end; but we might still need to set atime.
633 if (uio->uio_loffset >= zp->z_size) {
638 ASSERT(uio->uio_loffset < zp->z_size);
639 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
642 if ((uio->uio_extflg == UIO_XUIO) &&
643 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
645 int blksz = zp->z_blksz;
646 uint64_t offset = uio->uio_loffset;
648 xuio = (xuio_t *)uio;
650 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
653 ASSERT(offset + n <= blksz);
656 (void) dmu_xuio_init(xuio, nblk);
658 if (vn_has_cached_data(vp)) {
660 * For simplicity, we always allocate a full buffer
661 * even if we only expect to read a portion of a block.
663 while (--nblk >= 0) {
664 (void) dmu_xuio_add(xuio,
665 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
673 nbytes = MIN(n, zfs_read_chunk_size -
674 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
677 if (uio->uio_segflg == UIO_NOCOPY)
678 error = mappedread_sf(vp, nbytes, uio);
680 #endif /* __FreeBSD__ */
681 if (vn_has_cached_data(vp))
682 error = mappedread(vp, nbytes, uio);
684 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
686 /* convert checksum errors into IO errors */
695 zfs_range_unlock(rl);
697 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
703 * Write the bytes to a file.
705 * IN: vp - vnode of file to be written to.
706 * uio - structure supplying write location, range info,
708 * ioflag - FAPPEND flag set if in append mode.
709 * cr - credentials of caller.
710 * ct - caller context (NFS/CIFS fem monitor only)
712 * OUT: uio - updated offset and range.
714 * RETURN: 0 if success
715 * error code if failure
718 * vp - ctime|mtime updated if byte count > 0
723 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
725 znode_t *zp = VTOZ(vp);
726 rlim64_t limit = MAXOFFSET_T;
727 ssize_t start_resid = uio->uio_resid;
731 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
736 int max_blksz = zfsvfs->z_max_blksz;
742 int iovcnt = uio->uio_iovcnt;
743 iovec_t *iovp = uio->uio_iov;
746 sa_bulk_attr_t bulk[4];
747 uint64_t mtime[2], ctime[2];
750 * Fasttrack empty write
756 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
762 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
763 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
764 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
766 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
770 * If immutable or not appending then return EPERM
772 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
773 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
774 (uio->uio_loffset < zp->z_size))) {
779 zilog = zfsvfs->z_log;
782 * Validate file offset
784 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
791 * Check for mandatory locks before calling zfs_range_lock()
792 * in order to prevent a deadlock with locks set via fcntl().
794 if (MANDMODE((mode_t)zp->z_mode) &&
795 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
802 * Pre-fault the pages to ensure slow (eg NFS) pages
804 * Skip this if uio contains loaned arc_buf.
806 if ((uio->uio_extflg == UIO_XUIO) &&
807 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
808 xuio = (xuio_t *)uio;
810 uio_prefaultpages(MIN(n, max_blksz), uio);
814 * If in append mode, set the io offset pointer to eof.
816 if (ioflag & FAPPEND) {
818 * Obtain an appending range lock to guarantee file append
819 * semantics. We reset the write offset once we have the lock.
821 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
823 if (rl->r_len == UINT64_MAX) {
825 * We overlocked the file because this write will cause
826 * the file block size to increase.
827 * Note that zp_size cannot change with this lock held.
831 uio->uio_loffset = woff;
834 * Note that if the file block size will change as a result of
835 * this write, then this range lock will lock the entire file
836 * so that we can re-write the block safely.
838 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
842 zfs_range_unlock(rl);
847 if ((woff + n) > limit || woff > (limit - n))
850 /* Will this write extend the file length? */
851 write_eof = (woff + n > zp->z_size);
853 end_size = MAX(zp->z_size, woff + n);
856 * Write the file in reasonable size chunks. Each chunk is written
857 * in a separate transaction; this keeps the intent log records small
858 * and allows us to do more fine-grained space accounting.
862 woff = uio->uio_loffset;
864 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
865 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
867 dmu_return_arcbuf(abuf);
872 if (xuio && abuf == NULL) {
873 ASSERT(i_iov < iovcnt);
875 abuf = dmu_xuio_arcbuf(xuio, i_iov);
876 dmu_xuio_clear(xuio, i_iov);
877 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
878 iovec_t *, aiov, arc_buf_t *, abuf);
879 ASSERT((aiov->iov_base == abuf->b_data) ||
880 ((char *)aiov->iov_base - (char *)abuf->b_data +
881 aiov->iov_len == arc_buf_size(abuf)));
883 } else if (abuf == NULL && n >= max_blksz &&
884 woff >= zp->z_size &&
885 P2PHASE(woff, max_blksz) == 0 &&
886 zp->z_blksz == max_blksz) {
888 * This write covers a full block. "Borrow" a buffer
889 * from the dmu so that we can fill it before we enter
890 * a transaction. This avoids the possibility of
891 * holding up the transaction if the data copy hangs
892 * up on a pagefault (e.g., from an NFS server mapping).
896 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
898 ASSERT(abuf != NULL);
899 ASSERT(arc_buf_size(abuf) == max_blksz);
900 if (error = uiocopy(abuf->b_data, max_blksz,
901 UIO_WRITE, uio, &cbytes)) {
902 dmu_return_arcbuf(abuf);
905 ASSERT(cbytes == max_blksz);
909 * Start a transaction.
911 tx = dmu_tx_create(zfsvfs->z_os);
912 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
913 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
914 zfs_sa_upgrade_txholds(tx, zp);
915 error = dmu_tx_assign(tx, TXG_NOWAIT);
917 if (error == ERESTART) {
924 dmu_return_arcbuf(abuf);
929 * If zfs_range_lock() over-locked we grow the blocksize
930 * and then reduce the lock range. This will only happen
931 * on the first iteration since zfs_range_reduce() will
932 * shrink down r_len to the appropriate size.
934 if (rl->r_len == UINT64_MAX) {
937 if (zp->z_blksz > max_blksz) {
938 ASSERT(!ISP2(zp->z_blksz));
939 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
941 new_blksz = MIN(end_size, max_blksz);
943 zfs_grow_blocksize(zp, new_blksz, tx);
944 zfs_range_reduce(rl, woff, n);
948 * XXX - should we really limit each write to z_max_blksz?
949 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
951 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
953 if (woff + nbytes > zp->z_size)
954 vnode_pager_setsize(vp, woff + nbytes);
957 tx_bytes = uio->uio_resid;
958 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
960 tx_bytes -= uio->uio_resid;
963 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
965 * If this is not a full block write, but we are
966 * extending the file past EOF and this data starts
967 * block-aligned, use assign_arcbuf(). Otherwise,
968 * write via dmu_write().
970 if (tx_bytes < max_blksz && (!write_eof ||
971 aiov->iov_base != abuf->b_data)) {
973 dmu_write(zfsvfs->z_os, zp->z_id, woff,
974 aiov->iov_len, aiov->iov_base, tx);
975 dmu_return_arcbuf(abuf);
976 xuio_stat_wbuf_copied();
978 ASSERT(xuio || tx_bytes == max_blksz);
979 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
982 ASSERT(tx_bytes <= uio->uio_resid);
983 uioskip(uio, tx_bytes);
985 if (tx_bytes && vn_has_cached_data(vp)) {
986 update_pages(vp, woff, tx_bytes, zfsvfs->z_os,
987 zp->z_id, uio->uio_segflg, tx);
991 * If we made no progress, we're done. If we made even
992 * partial progress, update the znode and ZIL accordingly.
995 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
996 (void *)&zp->z_size, sizeof (uint64_t), tx);
1003 * Clear Set-UID/Set-GID bits on successful write if not
1004 * privileged and at least one of the excute bits is set.
1006 * It would be nice to to this after all writes have
1007 * been done, but that would still expose the ISUID/ISGID
1008 * to another app after the partial write is committed.
1010 * Note: we don't call zfs_fuid_map_id() here because
1011 * user 0 is not an ephemeral uid.
1013 mutex_enter(&zp->z_acl_lock);
1014 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
1015 (S_IXUSR >> 6))) != 0 &&
1016 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
1017 secpolicy_vnode_setid_retain(vp, cr,
1018 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
1020 zp->z_mode &= ~(S_ISUID | S_ISGID);
1021 newmode = zp->z_mode;
1022 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
1023 (void *)&newmode, sizeof (uint64_t), tx);
1025 mutex_exit(&zp->z_acl_lock);
1027 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
1031 * Update the file size (zp_size) if it has changed;
1032 * account for possible concurrent updates.
1034 while ((end_size = zp->z_size) < uio->uio_loffset) {
1035 (void) atomic_cas_64(&zp->z_size, end_size,
1040 * If we are replaying and eof is non zero then force
1041 * the file size to the specified eof. Note, there's no
1042 * concurrency during replay.
1044 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
1045 zp->z_size = zfsvfs->z_replay_eof;
1047 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1049 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
1054 ASSERT(tx_bytes == nbytes);
1059 uio_prefaultpages(MIN(n, max_blksz), uio);
1063 zfs_range_unlock(rl);
1066 * If we're in replay mode, or we made no progress, return error.
1067 * Otherwise, it's at least a partial write, so it's successful.
1069 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1074 if (ioflag & (FSYNC | FDSYNC) ||
1075 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1076 zil_commit(zilog, zp->z_id);
1083 zfs_get_done(zgd_t *zgd, int error)
1085 znode_t *zp = zgd->zgd_private;
1086 objset_t *os = zp->z_zfsvfs->z_os;
1090 dmu_buf_rele(zgd->zgd_db, zgd);
1092 zfs_range_unlock(zgd->zgd_rl);
1094 vfslocked = VFS_LOCK_GIANT(zp->z_zfsvfs->z_vfs);
1096 * Release the vnode asynchronously as we currently have the
1097 * txg stopped from syncing.
1099 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1101 if (error == 0 && zgd->zgd_bp)
1102 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1104 kmem_free(zgd, sizeof (zgd_t));
1105 VFS_UNLOCK_GIANT(vfslocked);
1109 static int zil_fault_io = 0;
1113 * Get data to generate a TX_WRITE intent log record.
1116 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1118 zfsvfs_t *zfsvfs = arg;
1119 objset_t *os = zfsvfs->z_os;
1121 uint64_t object = lr->lr_foid;
1122 uint64_t offset = lr->lr_offset;
1123 uint64_t size = lr->lr_length;
1124 blkptr_t *bp = &lr->lr_blkptr;
1129 ASSERT(zio != NULL);
1133 * Nothing to do if the file has been removed
1135 if (zfs_zget(zfsvfs, object, &zp) != 0)
1137 if (zp->z_unlinked) {
1139 * Release the vnode asynchronously as we currently have the
1140 * txg stopped from syncing.
1142 VN_RELE_ASYNC(ZTOV(zp),
1143 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1147 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1148 zgd->zgd_zilog = zfsvfs->z_log;
1149 zgd->zgd_private = zp;
1152 * Write records come in two flavors: immediate and indirect.
1153 * For small writes it's cheaper to store the data with the
1154 * log record (immediate); for large writes it's cheaper to
1155 * sync the data and get a pointer to it (indirect) so that
1156 * we don't have to write the data twice.
1158 if (buf != NULL) { /* immediate write */
1159 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1160 /* test for truncation needs to be done while range locked */
1161 if (offset >= zp->z_size) {
1164 error = dmu_read(os, object, offset, size, buf,
1165 DMU_READ_NO_PREFETCH);
1167 ASSERT(error == 0 || error == ENOENT);
1168 } else { /* indirect write */
1170 * Have to lock the whole block to ensure when it's
1171 * written out and it's checksum is being calculated
1172 * that no one can change the data. We need to re-check
1173 * blocksize after we get the lock in case it's changed!
1178 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1180 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1182 if (zp->z_blksz == size)
1185 zfs_range_unlock(zgd->zgd_rl);
1187 /* test for truncation needs to be done while range locked */
1188 if (lr->lr_offset >= zp->z_size)
1197 error = dmu_buf_hold(os, object, offset, zgd, &db,
1198 DMU_READ_NO_PREFETCH);
1204 ASSERT(db->db_offset == offset);
1205 ASSERT(db->db_size == size);
1207 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1209 ASSERT(error || lr->lr_length <= zp->z_blksz);
1212 * On success, we need to wait for the write I/O
1213 * initiated by dmu_sync() to complete before we can
1214 * release this dbuf. We will finish everything up
1215 * in the zfs_get_done() callback.
1220 if (error == EALREADY) {
1221 lr->lr_common.lrc_txtype = TX_WRITE2;
1227 zfs_get_done(zgd, error);
1234 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1235 caller_context_t *ct)
1237 znode_t *zp = VTOZ(vp);
1238 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1244 if (flag & V_ACE_MASK)
1245 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1247 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1254 * If vnode is for a device return a specfs vnode instead.
1257 specvp_check(vnode_t **vpp, cred_t *cr)
1261 if (IS_DEVVP(*vpp)) {
1264 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1275 * Lookup an entry in a directory, or an extended attribute directory.
1276 * If it exists, return a held vnode reference for it.
1278 * IN: dvp - vnode of directory to search.
1279 * nm - name of entry to lookup.
1280 * pnp - full pathname to lookup [UNUSED].
1281 * flags - LOOKUP_XATTR set if looking for an attribute.
1282 * rdir - root directory vnode [UNUSED].
1283 * cr - credentials of caller.
1284 * ct - caller context
1285 * direntflags - directory lookup flags
1286 * realpnp - returned pathname.
1288 * OUT: vpp - vnode of located entry, NULL if not found.
1290 * RETURN: 0 if success
1291 * error code if failure
1298 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct componentname *cnp,
1299 int nameiop, cred_t *cr, kthread_t *td, int flags)
1301 znode_t *zdp = VTOZ(dvp);
1302 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1304 int *direntflags = NULL;
1305 void *realpnp = NULL;
1308 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1310 if (dvp->v_type != VDIR) {
1312 } else if (zdp->z_sa_hdl == NULL) {
1316 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1317 error = zfs_fastaccesschk_execute(zdp, cr);
1325 vnode_t *tvp = dnlc_lookup(dvp, nm);
1328 error = zfs_fastaccesschk_execute(zdp, cr);
1333 if (tvp == DNLC_NO_VNODE) {
1338 return (specvp_check(vpp, cr));
1344 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1351 if (flags & LOOKUP_XATTR) {
1354 * If the xattr property is off, refuse the lookup request.
1356 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1363 * We don't allow recursive attributes..
1364 * Maybe someday we will.
1366 if (zdp->z_pflags & ZFS_XATTR) {
1371 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1377 * Do we have permission to get into attribute directory?
1380 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1390 if (dvp->v_type != VDIR) {
1396 * Check accessibility of directory.
1399 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1404 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1405 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1410 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1412 error = specvp_check(vpp, cr);
1414 /* Translate errors and add SAVENAME when needed. */
1415 if (cnp->cn_flags & ISLASTCN) {
1419 if (error == ENOENT) {
1420 error = EJUSTRETURN;
1421 cnp->cn_flags |= SAVENAME;
1427 cnp->cn_flags |= SAVENAME;
1431 if (error == 0 && (nm[0] != '.' || nm[1] != '\0')) {
1434 if (cnp->cn_flags & ISDOTDOT) {
1435 ltype = VOP_ISLOCKED(dvp);
1439 error = zfs_vnode_lock(*vpp, cnp->cn_lkflags);
1440 if (cnp->cn_flags & ISDOTDOT)
1441 vn_lock(dvp, ltype | LK_RETRY);
1451 #ifdef FREEBSD_NAMECACHE
1453 * Insert name into cache (as non-existent) if appropriate.
1455 if (error == ENOENT && (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
1456 cache_enter(dvp, *vpp, cnp);
1458 * Insert name into cache if appropriate.
1460 if (error == 0 && (cnp->cn_flags & MAKEENTRY)) {
1461 if (!(cnp->cn_flags & ISLASTCN) ||
1462 (nameiop != DELETE && nameiop != RENAME)) {
1463 cache_enter(dvp, *vpp, cnp);
1472 * Attempt to create a new entry in a directory. If the entry
1473 * already exists, truncate the file if permissible, else return
1474 * an error. Return the vp of the created or trunc'd file.
1476 * IN: dvp - vnode of directory to put new file entry in.
1477 * name - name of new file entry.
1478 * vap - attributes of new file.
1479 * excl - flag indicating exclusive or non-exclusive mode.
1480 * mode - mode to open file with.
1481 * cr - credentials of caller.
1482 * flag - large file flag [UNUSED].
1483 * ct - caller context
1484 * vsecp - ACL to be set
1486 * OUT: vpp - vnode of created or trunc'd entry.
1488 * RETURN: 0 if success
1489 * error code if failure
1492 * dvp - ctime|mtime updated if new entry created
1493 * vp - ctime|mtime always, atime if new
1498 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, int excl, int mode,
1499 vnode_t **vpp, cred_t *cr, kthread_t *td)
1501 znode_t *zp, *dzp = VTOZ(dvp);
1502 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1510 gid_t gid = crgetgid(cr);
1511 zfs_acl_ids_t acl_ids;
1512 boolean_t fuid_dirtied;
1513 boolean_t have_acl = B_FALSE;
1518 * If we have an ephemeral id, ACL, or XVATTR then
1519 * make sure file system is at proper version
1522 ksid = crgetsid(cr, KSID_OWNER);
1524 uid = ksid_getid(ksid);
1528 if (zfsvfs->z_use_fuids == B_FALSE &&
1529 (vsecp || (vap->va_mask & AT_XVATTR) ||
1530 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1536 zilog = zfsvfs->z_log;
1538 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1539 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1544 if (vap->va_mask & AT_XVATTR) {
1545 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
1546 crgetuid(cr), cr, vap->va_type)) != 0) {
1554 if ((vap->va_mode & S_ISVTX) && secpolicy_vnode_stky_modify(cr))
1555 vap->va_mode &= ~S_ISVTX;
1557 if (*name == '\0') {
1559 * Null component name refers to the directory itself.
1566 /* possible VN_HOLD(zp) */
1569 if (flag & FIGNORECASE)
1572 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1576 zfs_acl_ids_free(&acl_ids);
1577 if (strcmp(name, "..") == 0)
1588 * Create a new file object and update the directory
1591 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1593 zfs_acl_ids_free(&acl_ids);
1598 * We only support the creation of regular files in
1599 * extended attribute directories.
1602 if ((dzp->z_pflags & ZFS_XATTR) &&
1603 (vap->va_type != VREG)) {
1605 zfs_acl_ids_free(&acl_ids);
1610 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1611 cr, vsecp, &acl_ids)) != 0)
1615 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1616 zfs_acl_ids_free(&acl_ids);
1621 tx = dmu_tx_create(os);
1623 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1624 ZFS_SA_BASE_ATTR_SIZE);
1626 fuid_dirtied = zfsvfs->z_fuid_dirty;
1628 zfs_fuid_txhold(zfsvfs, tx);
1629 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1630 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1631 if (!zfsvfs->z_use_sa &&
1632 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1633 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1634 0, acl_ids.z_aclp->z_acl_bytes);
1636 error = dmu_tx_assign(tx, TXG_NOWAIT);
1638 zfs_dirent_unlock(dl);
1639 if (error == ERESTART) {
1644 zfs_acl_ids_free(&acl_ids);
1649 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1652 zfs_fuid_sync(zfsvfs, tx);
1654 (void) zfs_link_create(dl, zp, tx, ZNEW);
1655 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1656 if (flag & FIGNORECASE)
1658 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1659 vsecp, acl_ids.z_fuidp, vap);
1660 zfs_acl_ids_free(&acl_ids);
1663 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1666 zfs_acl_ids_free(&acl_ids);
1670 * A directory entry already exists for this name.
1673 * Can't truncate an existing file if in exclusive mode.
1680 * Can't open a directory for writing.
1682 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1687 * Verify requested access to file.
1689 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1693 mutex_enter(&dzp->z_lock);
1695 mutex_exit(&dzp->z_lock);
1698 * Truncate regular files if requested.
1700 if ((ZTOV(zp)->v_type == VREG) &&
1701 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1702 /* we can't hold any locks when calling zfs_freesp() */
1703 zfs_dirent_unlock(dl);
1705 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1707 vnevent_create(ZTOV(zp), ct);
1713 zfs_dirent_unlock(dl);
1720 error = specvp_check(vpp, cr);
1723 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1724 zil_commit(zilog, 0);
1731 * Remove an entry from a directory.
1733 * IN: dvp - vnode of directory to remove entry from.
1734 * name - name of entry to remove.
1735 * cr - credentials of caller.
1736 * ct - caller context
1737 * flags - case flags
1739 * RETURN: 0 if success
1740 * error code if failure
1744 * vp - ctime (if nlink > 0)
1747 uint64_t null_xattr = 0;
1751 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1754 znode_t *zp, *dzp = VTOZ(dvp);
1757 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1759 uint64_t acl_obj, xattr_obj;
1760 uint64_t xattr_obj_unlinked = 0;
1764 boolean_t may_delete_now, delete_now = FALSE;
1765 boolean_t unlinked, toobig = FALSE;
1767 pathname_t *realnmp = NULL;
1774 zilog = zfsvfs->z_log;
1776 if (flags & FIGNORECASE) {
1786 * Attempt to lock directory; fail if entry doesn't exist.
1788 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1798 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1803 * Need to use rmdir for removing directories.
1805 if (vp->v_type == VDIR) {
1810 vnevent_remove(vp, dvp, name, ct);
1813 dnlc_remove(dvp, realnmp->pn_buf);
1815 dnlc_remove(dvp, name);
1818 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1822 * We may delete the znode now, or we may put it in the unlinked set;
1823 * it depends on whether we're the last link, and on whether there are
1824 * other holds on the vnode. So we dmu_tx_hold() the right things to
1825 * allow for either case.
1828 tx = dmu_tx_create(zfsvfs->z_os);
1829 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1830 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1831 zfs_sa_upgrade_txholds(tx, zp);
1832 zfs_sa_upgrade_txholds(tx, dzp);
1833 if (may_delete_now) {
1835 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1836 /* if the file is too big, only hold_free a token amount */
1837 dmu_tx_hold_free(tx, zp->z_id, 0,
1838 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1841 /* are there any extended attributes? */
1842 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1843 &xattr_obj, sizeof (xattr_obj));
1844 if (error == 0 && xattr_obj) {
1845 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1846 ASSERT3U(error, ==, 0);
1847 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1848 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1851 mutex_enter(&zp->z_lock);
1852 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1853 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1854 mutex_exit(&zp->z_lock);
1856 /* charge as an update -- would be nice not to charge at all */
1857 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1859 error = dmu_tx_assign(tx, TXG_NOWAIT);
1861 zfs_dirent_unlock(dl);
1865 if (error == ERESTART) {
1878 * Remove the directory entry.
1880 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1890 * Hold z_lock so that we can make sure that the ACL obj
1891 * hasn't changed. Could have been deleted due to
1894 mutex_enter(&zp->z_lock);
1896 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1897 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1898 delete_now = may_delete_now && !toobig &&
1899 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1900 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1906 if (xattr_obj_unlinked) {
1907 ASSERT3U(xzp->z_links, ==, 2);
1908 mutex_enter(&xzp->z_lock);
1909 xzp->z_unlinked = 1;
1911 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1912 &xzp->z_links, sizeof (xzp->z_links), tx);
1913 ASSERT3U(error, ==, 0);
1914 mutex_exit(&xzp->z_lock);
1915 zfs_unlinked_add(xzp, tx);
1918 error = sa_remove(zp->z_sa_hdl,
1919 SA_ZPL_XATTR(zfsvfs), tx);
1921 error = sa_update(zp->z_sa_hdl,
1922 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1923 sizeof (uint64_t), tx);
1924 ASSERT3U(error, ==, 0);
1928 ASSERT3U(vp->v_count, ==, 0);
1930 mutex_exit(&zp->z_lock);
1931 zfs_znode_delete(zp, tx);
1932 } else if (unlinked) {
1933 mutex_exit(&zp->z_lock);
1934 zfs_unlinked_add(zp, tx);
1938 if (flags & FIGNORECASE)
1940 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1947 zfs_dirent_unlock(dl);
1954 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1955 zil_commit(zilog, 0);
1962 * Create a new directory and insert it into dvp using the name
1963 * provided. Return a pointer to the inserted directory.
1965 * IN: dvp - vnode of directory to add subdir to.
1966 * dirname - name of new directory.
1967 * vap - attributes of new directory.
1968 * cr - credentials of caller.
1969 * ct - caller context
1970 * vsecp - ACL to be set
1972 * OUT: vpp - vnode of created directory.
1974 * RETURN: 0 if success
1975 * error code if failure
1978 * dvp - ctime|mtime updated
1979 * vp - ctime|mtime|atime updated
1983 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1984 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1986 znode_t *zp, *dzp = VTOZ(dvp);
1987 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1996 gid_t gid = crgetgid(cr);
1997 zfs_acl_ids_t acl_ids;
1998 boolean_t fuid_dirtied;
2000 ASSERT(vap->va_type == VDIR);
2003 * If we have an ephemeral id, ACL, or XVATTR then
2004 * make sure file system is at proper version
2007 ksid = crgetsid(cr, KSID_OWNER);
2009 uid = ksid_getid(ksid);
2012 if (zfsvfs->z_use_fuids == B_FALSE &&
2013 (vsecp || (vap->va_mask & AT_XVATTR) ||
2014 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2019 zilog = zfsvfs->z_log;
2021 if (dzp->z_pflags & ZFS_XATTR) {
2026 if (zfsvfs->z_utf8 && u8_validate(dirname,
2027 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2031 if (flags & FIGNORECASE)
2034 if (vap->va_mask & AT_XVATTR) {
2035 if ((error = secpolicy_xvattr(dvp, (xvattr_t *)vap,
2036 crgetuid(cr), cr, vap->va_type)) != 0) {
2042 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
2043 vsecp, &acl_ids)) != 0) {
2048 * First make sure the new directory doesn't exist.
2050 * Existence is checked first to make sure we don't return
2051 * EACCES instead of EEXIST which can cause some applications
2057 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2059 zfs_acl_ids_free(&acl_ids);
2064 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
2065 zfs_acl_ids_free(&acl_ids);
2066 zfs_dirent_unlock(dl);
2071 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
2072 zfs_acl_ids_free(&acl_ids);
2073 zfs_dirent_unlock(dl);
2079 * Add a new entry to the directory.
2081 tx = dmu_tx_create(zfsvfs->z_os);
2082 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2083 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2084 fuid_dirtied = zfsvfs->z_fuid_dirty;
2086 zfs_fuid_txhold(zfsvfs, tx);
2087 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2088 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2089 acl_ids.z_aclp->z_acl_bytes);
2092 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2093 ZFS_SA_BASE_ATTR_SIZE);
2095 error = dmu_tx_assign(tx, TXG_NOWAIT);
2097 zfs_dirent_unlock(dl);
2098 if (error == ERESTART) {
2103 zfs_acl_ids_free(&acl_ids);
2112 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2115 zfs_fuid_sync(zfsvfs, tx);
2118 * Now put new name in parent dir.
2120 (void) zfs_link_create(dl, zp, tx, ZNEW);
2124 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2125 if (flags & FIGNORECASE)
2127 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2128 acl_ids.z_fuidp, vap);
2130 zfs_acl_ids_free(&acl_ids);
2134 zfs_dirent_unlock(dl);
2136 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2137 zil_commit(zilog, 0);
2144 * Remove a directory subdir entry. If the current working
2145 * directory is the same as the subdir to be removed, the
2148 * IN: dvp - vnode of directory to remove from.
2149 * name - name of directory to be removed.
2150 * cwd - vnode of current working directory.
2151 * cr - credentials of caller.
2152 * ct - caller context
2153 * flags - case flags
2155 * RETURN: 0 if success
2156 * error code if failure
2159 * dvp - ctime|mtime updated
2163 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2164 caller_context_t *ct, int flags)
2166 znode_t *dzp = VTOZ(dvp);
2169 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2178 zilog = zfsvfs->z_log;
2180 if (flags & FIGNORECASE)
2186 * Attempt to lock directory; fail if entry doesn't exist.
2188 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2196 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2200 if (vp->v_type != VDIR) {
2210 vnevent_rmdir(vp, dvp, name, ct);
2213 * Grab a lock on the directory to make sure that noone is
2214 * trying to add (or lookup) entries while we are removing it.
2216 rw_enter(&zp->z_name_lock, RW_WRITER);
2219 * Grab a lock on the parent pointer to make sure we play well
2220 * with the treewalk and directory rename code.
2222 rw_enter(&zp->z_parent_lock, RW_WRITER);
2224 tx = dmu_tx_create(zfsvfs->z_os);
2225 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2226 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2227 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2228 zfs_sa_upgrade_txholds(tx, zp);
2229 zfs_sa_upgrade_txholds(tx, dzp);
2230 error = dmu_tx_assign(tx, TXG_NOWAIT);
2232 rw_exit(&zp->z_parent_lock);
2233 rw_exit(&zp->z_name_lock);
2234 zfs_dirent_unlock(dl);
2236 if (error == ERESTART) {
2246 #ifdef FREEBSD_NAMECACHE
2250 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2253 uint64_t txtype = TX_RMDIR;
2254 if (flags & FIGNORECASE)
2256 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2261 rw_exit(&zp->z_parent_lock);
2262 rw_exit(&zp->z_name_lock);
2263 #ifdef FREEBSD_NAMECACHE
2267 zfs_dirent_unlock(dl);
2271 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2272 zil_commit(zilog, 0);
2279 * Read as many directory entries as will fit into the provided
2280 * buffer from the given directory cursor position (specified in
2281 * the uio structure.
2283 * IN: vp - vnode of directory to read.
2284 * uio - structure supplying read location, range info,
2285 * and return buffer.
2286 * cr - credentials of caller.
2287 * ct - caller context
2288 * flags - case flags
2290 * OUT: uio - updated offset and range, buffer filled.
2291 * eofp - set to true if end-of-file detected.
2293 * RETURN: 0 if success
2294 * error code if failure
2297 * vp - atime updated
2299 * Note that the low 4 bits of the cookie returned by zap is always zero.
2300 * This allows us to use the low range for "special" directory entries:
2301 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2302 * we use the offset 2 for the '.zfs' directory.
2306 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, int *ncookies, u_long **cookies)
2308 znode_t *zp = VTOZ(vp);
2312 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2317 zap_attribute_t zap;
2318 uint_t bytes_wanted;
2319 uint64_t offset; /* must be unsigned; checks for < 1 */
2325 boolean_t check_sysattrs;
2328 u_long *cooks = NULL;
2334 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2335 &parent, sizeof (parent))) != 0) {
2341 * If we are not given an eof variable,
2348 * Check for valid iov_len.
2350 if (uio->uio_iov->iov_len <= 0) {
2356 * Quit if directory has been removed (posix)
2358 if ((*eofp = zp->z_unlinked) != 0) {
2365 offset = uio->uio_loffset;
2366 prefetch = zp->z_zn_prefetch;
2369 * Initialize the iterator cursor.
2373 * Start iteration from the beginning of the directory.
2375 zap_cursor_init(&zc, os, zp->z_id);
2378 * The offset is a serialized cursor.
2380 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2384 * Get space to change directory entries into fs independent format.
2386 iovp = uio->uio_iov;
2387 bytes_wanted = iovp->iov_len;
2388 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2389 bufsize = bytes_wanted;
2390 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2391 odp = (struct dirent64 *)outbuf;
2393 bufsize = bytes_wanted;
2394 odp = (struct dirent64 *)iovp->iov_base;
2396 eodp = (struct edirent *)odp;
2398 if (ncookies != NULL) {
2400 * Minimum entry size is dirent size and 1 byte for a file name.
2402 ncooks = uio->uio_resid / (sizeof(struct dirent) - sizeof(((struct dirent *)NULL)->d_name) + 1);
2403 cooks = malloc(ncooks * sizeof(u_long), M_TEMP, M_WAITOK);
2408 * If this VFS supports the system attribute view interface; and
2409 * we're looking at an extended attribute directory; and we care
2410 * about normalization conflicts on this vfs; then we must check
2411 * for normalization conflicts with the sysattr name space.
2414 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2415 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2416 (flags & V_RDDIR_ENTFLAGS);
2422 * Transform to file-system independent format
2425 while (outcount < bytes_wanted) {
2428 off64_t *next = NULL;
2431 * Special case `.', `..', and `.zfs'.
2434 (void) strcpy(zap.za_name, ".");
2435 zap.za_normalization_conflict = 0;
2438 } else if (offset == 1) {
2439 (void) strcpy(zap.za_name, "..");
2440 zap.za_normalization_conflict = 0;
2443 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2444 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2445 zap.za_normalization_conflict = 0;
2446 objnum = ZFSCTL_INO_ROOT;
2452 if (error = zap_cursor_retrieve(&zc, &zap)) {
2453 if ((*eofp = (error == ENOENT)) != 0)
2459 if (zap.za_integer_length != 8 ||
2460 zap.za_num_integers != 1) {
2461 cmn_err(CE_WARN, "zap_readdir: bad directory "
2462 "entry, obj = %lld, offset = %lld\n",
2463 (u_longlong_t)zp->z_id,
2464 (u_longlong_t)offset);
2469 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2471 * MacOS X can extract the object type here such as:
2472 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2474 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2476 if (check_sysattrs && !zap.za_normalization_conflict) {
2478 zap.za_normalization_conflict =
2479 xattr_sysattr_casechk(zap.za_name);
2481 panic("%s:%u: TODO", __func__, __LINE__);
2486 if (flags & V_RDDIR_ACCFILTER) {
2488 * If we have no access at all, don't include
2489 * this entry in the returned information
2492 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2494 if (!zfs_has_access(ezp, cr)) {
2501 if (flags & V_RDDIR_ENTFLAGS)
2502 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2504 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2507 * Will this entry fit in the buffer?
2509 if (outcount + reclen > bufsize) {
2511 * Did we manage to fit anything in the buffer?
2519 if (flags & V_RDDIR_ENTFLAGS) {
2521 * Add extended flag entry:
2523 eodp->ed_ino = objnum;
2524 eodp->ed_reclen = reclen;
2525 /* NOTE: ed_off is the offset for the *next* entry */
2526 next = &(eodp->ed_off);
2527 eodp->ed_eflags = zap.za_normalization_conflict ?
2528 ED_CASE_CONFLICT : 0;
2529 (void) strncpy(eodp->ed_name, zap.za_name,
2530 EDIRENT_NAMELEN(reclen));
2531 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2536 odp->d_ino = objnum;
2537 odp->d_reclen = reclen;
2538 odp->d_namlen = strlen(zap.za_name);
2539 (void) strlcpy(odp->d_name, zap.za_name, odp->d_namlen + 1);
2541 odp = (dirent64_t *)((intptr_t)odp + reclen);
2545 ASSERT(outcount <= bufsize);
2547 /* Prefetch znode */
2549 dmu_prefetch(os, objnum, 0, 0);
2553 * Move to the next entry, fill in the previous offset.
2555 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2556 zap_cursor_advance(&zc);
2557 offset = zap_cursor_serialize(&zc);
2562 if (cooks != NULL) {
2565 KASSERT(ncooks >= 0, ("ncookies=%d", ncooks));
2568 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2570 /* Subtract unused cookies */
2571 if (ncookies != NULL)
2572 *ncookies -= ncooks;
2574 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2575 iovp->iov_base += outcount;
2576 iovp->iov_len -= outcount;
2577 uio->uio_resid -= outcount;
2578 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2580 * Reset the pointer.
2582 offset = uio->uio_loffset;
2586 zap_cursor_fini(&zc);
2587 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2588 kmem_free(outbuf, bufsize);
2590 if (error == ENOENT)
2593 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2595 uio->uio_loffset = offset;
2597 if (error != 0 && cookies != NULL) {
2598 free(*cookies, M_TEMP);
2605 ulong_t zfs_fsync_sync_cnt = 4;
2608 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2610 znode_t *zp = VTOZ(vp);
2611 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2613 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2615 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2618 zil_commit(zfsvfs->z_log, zp->z_id);
2626 * Get the requested file attributes and place them in the provided
2629 * IN: vp - vnode of file.
2630 * vap - va_mask identifies requested attributes.
2631 * If AT_XVATTR set, then optional attrs are requested
2632 * flags - ATTR_NOACLCHECK (CIFS server context)
2633 * cr - credentials of caller.
2634 * ct - caller context
2636 * OUT: vap - attribute values.
2638 * RETURN: 0 (always succeeds)
2642 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2643 caller_context_t *ct)
2645 znode_t *zp = VTOZ(vp);
2646 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2649 u_longlong_t nblocks;
2651 uint64_t mtime[2], ctime[2], crtime[2];
2652 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2653 xoptattr_t *xoap = NULL;
2654 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2655 sa_bulk_attr_t bulk[3];
2661 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2663 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2664 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2665 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &crtime, 16);
2667 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2673 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2674 * Also, if we are the owner don't bother, since owner should
2675 * always be allowed to read basic attributes of file.
2677 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2678 (vap->va_uid != crgetuid(cr))) {
2679 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2687 * Return all attributes. It's cheaper to provide the answer
2688 * than to determine whether we were asked the question.
2691 mutex_enter(&zp->z_lock);
2692 vap->va_type = IFTOVT(zp->z_mode);
2693 vap->va_mode = zp->z_mode & ~S_IFMT;
2694 // vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2695 vap->va_nodeid = zp->z_id;
2696 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2697 links = zp->z_links + 1;
2699 links = zp->z_links;
2700 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2701 vap->va_size = zp->z_size;
2702 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
2703 // vap->va_rdev = zfs_cmpldev(pzp->zp_rdev);
2704 vap->va_seq = zp->z_seq;
2705 vap->va_flags = 0; /* FreeBSD: Reset chflags(2) flags. */
2708 * Add in any requested optional attributes and the create time.
2709 * Also set the corresponding bits in the returned attribute bitmap.
2711 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2712 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2714 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2715 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2718 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2719 xoap->xoa_readonly =
2720 ((zp->z_pflags & ZFS_READONLY) != 0);
2721 XVA_SET_RTN(xvap, XAT_READONLY);
2724 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2726 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2727 XVA_SET_RTN(xvap, XAT_SYSTEM);
2730 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2732 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2733 XVA_SET_RTN(xvap, XAT_HIDDEN);
2736 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2737 xoap->xoa_nounlink =
2738 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2739 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2742 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2743 xoap->xoa_immutable =
2744 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2745 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2748 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2749 xoap->xoa_appendonly =
2750 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2751 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2754 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2756 ((zp->z_pflags & ZFS_NODUMP) != 0);
2757 XVA_SET_RTN(xvap, XAT_NODUMP);
2760 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2762 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2763 XVA_SET_RTN(xvap, XAT_OPAQUE);
2766 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2767 xoap->xoa_av_quarantined =
2768 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2769 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2772 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2773 xoap->xoa_av_modified =
2774 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2775 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2778 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2779 vp->v_type == VREG) {
2780 zfs_sa_get_scanstamp(zp, xvap);
2783 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2786 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2787 times, sizeof (times));
2788 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2789 XVA_SET_RTN(xvap, XAT_CREATETIME);
2792 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2793 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2794 XVA_SET_RTN(xvap, XAT_REPARSE);
2796 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2797 xoap->xoa_generation = zp->z_gen;
2798 XVA_SET_RTN(xvap, XAT_GEN);
2801 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2803 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2804 XVA_SET_RTN(xvap, XAT_OFFLINE);
2807 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2809 ((zp->z_pflags & ZFS_SPARSE) != 0);
2810 XVA_SET_RTN(xvap, XAT_SPARSE);
2814 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2815 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2816 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2817 ZFS_TIME_DECODE(&vap->va_birthtime, crtime);
2819 mutex_exit(&zp->z_lock);
2821 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2822 vap->va_blksize = blksize;
2823 vap->va_bytes = nblocks << 9; /* nblocks * 512 */
2825 if (zp->z_blksz == 0) {
2827 * Block size hasn't been set; suggest maximal I/O transfers.
2829 vap->va_blksize = zfsvfs->z_max_blksz;
2837 * Set the file attributes to the values contained in the
2840 * IN: vp - vnode of file to be modified.
2841 * vap - new attribute values.
2842 * If AT_XVATTR set, then optional attrs are being set
2843 * flags - ATTR_UTIME set if non-default time values provided.
2844 * - ATTR_NOACLCHECK (CIFS context only).
2845 * cr - credentials of caller.
2846 * ct - caller context
2848 * RETURN: 0 if success
2849 * error code if failure
2852 * vp - ctime updated, mtime updated if size changed.
2856 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2857 caller_context_t *ct)
2859 znode_t *zp = VTOZ(vp);
2860 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2865 uint_t mask = vap->va_mask;
2867 uint64_t saved_mode;
2870 uint64_t new_uid, new_gid;
2872 uint64_t mtime[2], ctime[2];
2874 int need_policy = FALSE;
2876 zfs_fuid_info_t *fuidp = NULL;
2877 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2880 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2881 boolean_t fuid_dirtied = B_FALSE;
2882 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2883 int count = 0, xattr_count = 0;
2888 if (mask & AT_NOSET)
2894 zilog = zfsvfs->z_log;
2897 * Make sure that if we have ephemeral uid/gid or xvattr specified
2898 * that file system is at proper version level
2901 if (zfsvfs->z_use_fuids == B_FALSE &&
2902 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2903 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2904 (mask & AT_XVATTR))) {
2909 if (mask & AT_SIZE && vp->v_type == VDIR) {
2914 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2920 * If this is an xvattr_t, then get a pointer to the structure of
2921 * optional attributes. If this is NULL, then we have a vattr_t.
2923 xoap = xva_getxoptattr(xvap);
2925 xva_init(&tmpxvattr);
2928 * Immutable files can only alter immutable bit and atime
2930 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2931 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2932 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2937 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2943 * Verify timestamps doesn't overflow 32 bits.
2944 * ZFS can handle large timestamps, but 32bit syscalls can't
2945 * handle times greater than 2039. This check should be removed
2946 * once large timestamps are fully supported.
2948 if (mask & (AT_ATIME | AT_MTIME)) {
2949 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2950 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2960 /* Can this be moved to before the top label? */
2961 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2967 * First validate permissions
2970 if (mask & AT_SIZE) {
2972 * XXX - Note, we are not providing any open
2973 * mode flags here (like FNDELAY), so we may
2974 * block if there are locks present... this
2975 * should be addressed in openat().
2977 /* XXX - would it be OK to generate a log record here? */
2978 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2985 if (mask & (AT_ATIME|AT_MTIME) ||
2986 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2987 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2988 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2989 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2990 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2991 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2992 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2993 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2997 if (mask & (AT_UID|AT_GID)) {
2998 int idmask = (mask & (AT_UID|AT_GID));
3003 * NOTE: even if a new mode is being set,
3004 * we may clear S_ISUID/S_ISGID bits.
3007 if (!(mask & AT_MODE))
3008 vap->va_mode = zp->z_mode;
3011 * Take ownership or chgrp to group we are a member of
3014 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
3015 take_group = (mask & AT_GID) &&
3016 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3019 * If both AT_UID and AT_GID are set then take_owner and
3020 * take_group must both be set in order to allow taking
3023 * Otherwise, send the check through secpolicy_vnode_setattr()
3027 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
3028 ((idmask == AT_UID) && take_owner) ||
3029 ((idmask == AT_GID) && take_group)) {
3030 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3031 skipaclchk, cr) == 0) {
3033 * Remove setuid/setgid for non-privileged users
3035 secpolicy_setid_clear(vap, vp, cr);
3036 trim_mask = (mask & (AT_UID|AT_GID));
3045 mutex_enter(&zp->z_lock);
3046 oldva.va_mode = zp->z_mode;
3047 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3048 if (mask & AT_XVATTR) {
3050 * Update xvattr mask to include only those attributes
3051 * that are actually changing.
3053 * the bits will be restored prior to actually setting
3054 * the attributes so the caller thinks they were set.
3056 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3057 if (xoap->xoa_appendonly !=
3058 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3061 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3062 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
3066 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3067 if (xoap->xoa_nounlink !=
3068 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3071 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3072 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
3076 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3077 if (xoap->xoa_immutable !=
3078 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3081 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3082 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
3086 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3087 if (xoap->xoa_nodump !=
3088 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3091 XVA_CLR_REQ(xvap, XAT_NODUMP);
3092 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
3096 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3097 if (xoap->xoa_av_modified !=
3098 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3101 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3102 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
3106 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3107 if ((vp->v_type != VREG &&
3108 xoap->xoa_av_quarantined) ||
3109 xoap->xoa_av_quarantined !=
3110 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3113 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3114 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
3118 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3119 mutex_exit(&zp->z_lock);
3124 if (need_policy == FALSE &&
3125 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3126 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3131 mutex_exit(&zp->z_lock);
3133 if (mask & AT_MODE) {
3134 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3135 err = secpolicy_setid_setsticky_clear(vp, vap,
3141 trim_mask |= AT_MODE;
3149 * If trim_mask is set then take ownership
3150 * has been granted or write_acl is present and user
3151 * has the ability to modify mode. In that case remove
3152 * UID|GID and or MODE from mask so that
3153 * secpolicy_vnode_setattr() doesn't revoke it.
3157 saved_mask = vap->va_mask;
3158 vap->va_mask &= ~trim_mask;
3159 if (trim_mask & AT_MODE) {
3161 * Save the mode, as secpolicy_vnode_setattr()
3162 * will overwrite it with ova.va_mode.
3164 saved_mode = vap->va_mode;
3167 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3168 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3175 vap->va_mask |= saved_mask;
3176 if (trim_mask & AT_MODE) {
3178 * Recover the mode after
3179 * secpolicy_vnode_setattr().
3181 vap->va_mode = saved_mode;
3187 * secpolicy_vnode_setattr, or take ownership may have
3190 mask = vap->va_mask;
3192 if ((mask & (AT_UID | AT_GID))) {
3193 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3194 &xattr_obj, sizeof (xattr_obj));
3196 if (err == 0 && xattr_obj) {
3197 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3201 if (mask & AT_UID) {
3202 new_uid = zfs_fuid_create(zfsvfs,
3203 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3204 if (new_uid != zp->z_uid &&
3205 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3207 VN_RELE(ZTOV(attrzp));
3213 if (mask & AT_GID) {
3214 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3215 cr, ZFS_GROUP, &fuidp);
3216 if (new_gid != zp->z_gid &&
3217 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3219 VN_RELE(ZTOV(attrzp));
3225 tx = dmu_tx_create(zfsvfs->z_os);
3227 if (mask & AT_MODE) {
3228 uint64_t pmode = zp->z_mode;
3230 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3232 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3235 mutex_enter(&zp->z_lock);
3236 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3238 * Are we upgrading ACL from old V0 format
3241 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3242 zfs_znode_acl_version(zp) ==
3243 ZFS_ACL_VERSION_INITIAL) {
3244 dmu_tx_hold_free(tx, acl_obj, 0,
3246 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3247 0, aclp->z_acl_bytes);
3249 dmu_tx_hold_write(tx, acl_obj, 0,
3252 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3253 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3254 0, aclp->z_acl_bytes);
3256 mutex_exit(&zp->z_lock);
3257 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3259 if ((mask & AT_XVATTR) &&
3260 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3261 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3263 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3267 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3270 fuid_dirtied = zfsvfs->z_fuid_dirty;
3272 zfs_fuid_txhold(zfsvfs, tx);
3274 zfs_sa_upgrade_txholds(tx, zp);
3276 err = dmu_tx_assign(tx, TXG_NOWAIT);
3278 if (err == ERESTART)
3285 * Set each attribute requested.
3286 * We group settings according to the locks they need to acquire.
3288 * Note: you cannot set ctime directly, although it will be
3289 * updated as a side-effect of calling this function.
3293 if (mask & (AT_UID|AT_GID|AT_MODE))
3294 mutex_enter(&zp->z_acl_lock);
3295 mutex_enter(&zp->z_lock);
3297 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3298 &zp->z_pflags, sizeof (zp->z_pflags));
3301 if (mask & (AT_UID|AT_GID|AT_MODE))
3302 mutex_enter(&attrzp->z_acl_lock);
3303 mutex_enter(&attrzp->z_lock);
3304 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3305 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3306 sizeof (attrzp->z_pflags));
3309 if (mask & (AT_UID|AT_GID)) {
3311 if (mask & AT_UID) {
3312 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3313 &new_uid, sizeof (new_uid));
3314 zp->z_uid = new_uid;
3316 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3317 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3319 attrzp->z_uid = new_uid;
3323 if (mask & AT_GID) {
3324 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3325 NULL, &new_gid, sizeof (new_gid));
3326 zp->z_gid = new_gid;
3328 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3329 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3331 attrzp->z_gid = new_gid;
3334 if (!(mask & AT_MODE)) {
3335 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3336 NULL, &new_mode, sizeof (new_mode));
3337 new_mode = zp->z_mode;
3339 err = zfs_acl_chown_setattr(zp);
3342 err = zfs_acl_chown_setattr(attrzp);
3347 if (mask & AT_MODE) {
3348 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3349 &new_mode, sizeof (new_mode));
3350 zp->z_mode = new_mode;
3351 ASSERT3U((uintptr_t)aclp, !=, 0);
3352 err = zfs_aclset_common(zp, aclp, cr, tx);
3353 ASSERT3U(err, ==, 0);
3354 if (zp->z_acl_cached)
3355 zfs_acl_free(zp->z_acl_cached);
3356 zp->z_acl_cached = aclp;
3361 if (mask & AT_ATIME) {
3362 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3363 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3364 &zp->z_atime, sizeof (zp->z_atime));
3367 if (mask & AT_MTIME) {
3368 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3369 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3370 mtime, sizeof (mtime));
3373 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3374 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3375 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3376 NULL, mtime, sizeof (mtime));
3377 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3378 &ctime, sizeof (ctime));
3379 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3381 } else if (mask != 0) {
3382 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3383 &ctime, sizeof (ctime));
3384 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3387 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3388 SA_ZPL_CTIME(zfsvfs), NULL,
3389 &ctime, sizeof (ctime));
3390 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3391 mtime, ctime, B_TRUE);
3395 * Do this after setting timestamps to prevent timestamp
3396 * update from toggling bit
3399 if (xoap && (mask & AT_XVATTR)) {
3402 * restore trimmed off masks
3403 * so that return masks can be set for caller.
3406 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3407 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3409 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3410 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3412 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3413 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3415 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3416 XVA_SET_REQ(xvap, XAT_NODUMP);
3418 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3419 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3421 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3422 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3425 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3426 ASSERT(vp->v_type == VREG);
3428 zfs_xvattr_set(zp, xvap, tx);
3432 zfs_fuid_sync(zfsvfs, tx);
3435 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3437 mutex_exit(&zp->z_lock);
3438 if (mask & (AT_UID|AT_GID|AT_MODE))
3439 mutex_exit(&zp->z_acl_lock);
3442 if (mask & (AT_UID|AT_GID|AT_MODE))
3443 mutex_exit(&attrzp->z_acl_lock);
3444 mutex_exit(&attrzp->z_lock);
3447 if (err == 0 && attrzp) {
3448 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3454 VN_RELE(ZTOV(attrzp));
3459 zfs_fuid_info_free(fuidp);
3465 if (err == ERESTART)
3468 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3473 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3474 zil_commit(zilog, 0);
3480 typedef struct zfs_zlock {
3481 krwlock_t *zl_rwlock; /* lock we acquired */
3482 znode_t *zl_znode; /* znode we held */
3483 struct zfs_zlock *zl_next; /* next in list */
3487 * Drop locks and release vnodes that were held by zfs_rename_lock().
3490 zfs_rename_unlock(zfs_zlock_t **zlpp)
3494 while ((zl = *zlpp) != NULL) {
3495 if (zl->zl_znode != NULL)
3496 VN_RELE(ZTOV(zl->zl_znode));
3497 rw_exit(zl->zl_rwlock);
3498 *zlpp = zl->zl_next;
3499 kmem_free(zl, sizeof (*zl));
3504 * Search back through the directory tree, using the ".." entries.
3505 * Lock each directory in the chain to prevent concurrent renames.
3506 * Fail any attempt to move a directory into one of its own descendants.
3507 * XXX - z_parent_lock can overlap with map or grow locks
3510 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3514 uint64_t rootid = zp->z_zfsvfs->z_root;
3515 uint64_t oidp = zp->z_id;
3516 krwlock_t *rwlp = &szp->z_parent_lock;
3517 krw_t rw = RW_WRITER;
3520 * First pass write-locks szp and compares to zp->z_id.
3521 * Later passes read-lock zp and compare to zp->z_parent.
3524 if (!rw_tryenter(rwlp, rw)) {
3526 * Another thread is renaming in this path.
3527 * Note that if we are a WRITER, we don't have any
3528 * parent_locks held yet.
3530 if (rw == RW_READER && zp->z_id > szp->z_id) {
3532 * Drop our locks and restart
3534 zfs_rename_unlock(&zl);
3538 rwlp = &szp->z_parent_lock;
3543 * Wait for other thread to drop its locks
3549 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3550 zl->zl_rwlock = rwlp;
3551 zl->zl_znode = NULL;
3552 zl->zl_next = *zlpp;
3555 if (oidp == szp->z_id) /* We're a descendant of szp */
3558 if (oidp == rootid) /* We've hit the top */
3561 if (rw == RW_READER) { /* i.e. not the first pass */
3562 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3567 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3568 &oidp, sizeof (oidp));
3569 rwlp = &zp->z_parent_lock;
3572 } while (zp->z_id != sdzp->z_id);
3578 * Move an entry from the provided source directory to the target
3579 * directory. Change the entry name as indicated.
3581 * IN: sdvp - Source directory containing the "old entry".
3582 * snm - Old entry name.
3583 * tdvp - Target directory to contain the "new entry".
3584 * tnm - New entry name.
3585 * cr - credentials of caller.
3586 * ct - caller context
3587 * flags - case flags
3589 * RETURN: 0 if success
3590 * error code if failure
3593 * sdvp,tdvp - ctime|mtime updated
3597 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3598 caller_context_t *ct, int flags)
3600 znode_t *tdzp, *szp, *tzp;
3601 znode_t *sdzp = VTOZ(sdvp);
3602 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3605 zfs_dirlock_t *sdl, *tdl;
3608 int cmp, serr, terr;
3613 ZFS_VERIFY_ZP(sdzp);
3614 zilog = zfsvfs->z_log;
3617 * Make sure we have the real vp for the target directory.
3619 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3622 if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3628 ZFS_VERIFY_ZP(tdzp);
3629 if (zfsvfs->z_utf8 && u8_validate(tnm,
3630 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3635 if (flags & FIGNORECASE)
3644 * This is to prevent the creation of links into attribute space
3645 * by renaming a linked file into/outof an attribute directory.
3646 * See the comment in zfs_link() for why this is considered bad.
3648 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3654 * Lock source and target directory entries. To prevent deadlock,
3655 * a lock ordering must be defined. We lock the directory with
3656 * the smallest object id first, or if it's a tie, the one with
3657 * the lexically first name.
3659 if (sdzp->z_id < tdzp->z_id) {
3661 } else if (sdzp->z_id > tdzp->z_id) {
3665 * First compare the two name arguments without
3666 * considering any case folding.
3668 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3670 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3671 ASSERT(error == 0 || !zfsvfs->z_utf8);
3674 * POSIX: "If the old argument and the new argument
3675 * both refer to links to the same existing file,
3676 * the rename() function shall return successfully
3677 * and perform no other action."
3683 * If the file system is case-folding, then we may
3684 * have some more checking to do. A case-folding file
3685 * system is either supporting mixed case sensitivity
3686 * access or is completely case-insensitive. Note
3687 * that the file system is always case preserving.
3689 * In mixed sensitivity mode case sensitive behavior
3690 * is the default. FIGNORECASE must be used to
3691 * explicitly request case insensitive behavior.
3693 * If the source and target names provided differ only
3694 * by case (e.g., a request to rename 'tim' to 'Tim'),
3695 * we will treat this as a special case in the
3696 * case-insensitive mode: as long as the source name
3697 * is an exact match, we will allow this to proceed as
3698 * a name-change request.
3700 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3701 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3702 flags & FIGNORECASE)) &&
3703 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3706 * case preserving rename request, require exact
3715 * If the source and destination directories are the same, we should
3716 * grab the z_name_lock of that directory only once.
3720 rw_enter(&sdzp->z_name_lock, RW_READER);
3724 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3725 ZEXISTS | zflg, NULL, NULL);
3726 terr = zfs_dirent_lock(&tdl,
3727 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3729 terr = zfs_dirent_lock(&tdl,
3730 tdzp, tnm, &tzp, zflg, NULL, NULL);
3731 serr = zfs_dirent_lock(&sdl,
3732 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3738 * Source entry invalid or not there.
3741 zfs_dirent_unlock(tdl);
3747 rw_exit(&sdzp->z_name_lock);
3750 * FreeBSD: In OpenSolaris they only check if rename source is
3751 * ".." here, because "." is handled in their lookup. This is
3752 * not the case for FreeBSD, so we check for "." explicitly.
3754 if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0)
3760 zfs_dirent_unlock(sdl);
3764 rw_exit(&sdzp->z_name_lock);
3766 if (strcmp(tnm, "..") == 0)
3773 * Must have write access at the source to remove the old entry
3774 * and write access at the target to create the new entry.
3775 * Note that if target and source are the same, this can be
3776 * done in a single check.
3779 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3782 if (ZTOV(szp)->v_type == VDIR) {
3784 * Check to make sure rename is valid.
3785 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3787 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3792 * Does target exist?
3796 * Source and target must be the same type.
3798 if (ZTOV(szp)->v_type == VDIR) {
3799 if (ZTOV(tzp)->v_type != VDIR) {
3804 if (ZTOV(tzp)->v_type == VDIR) {
3810 * POSIX dictates that when the source and target
3811 * entries refer to the same file object, rename
3812 * must do nothing and exit without error.
3814 if (szp->z_id == tzp->z_id) {
3820 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3822 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3825 * notify the target directory if it is not the same
3826 * as source directory.
3829 vnevent_rename_dest_dir(tdvp, ct);
3832 tx = dmu_tx_create(zfsvfs->z_os);
3833 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3834 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3835 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3836 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3838 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3839 zfs_sa_upgrade_txholds(tx, tdzp);
3842 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3843 zfs_sa_upgrade_txholds(tx, tzp);
3846 zfs_sa_upgrade_txholds(tx, szp);
3847 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3848 error = dmu_tx_assign(tx, TXG_NOWAIT);
3851 zfs_rename_unlock(&zl);
3852 zfs_dirent_unlock(sdl);
3853 zfs_dirent_unlock(tdl);
3856 rw_exit(&sdzp->z_name_lock);
3861 if (error == ERESTART) {
3871 if (tzp) /* Attempt to remove the existing target */
3872 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3875 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3877 szp->z_pflags |= ZFS_AV_MODIFIED;
3879 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3880 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3881 ASSERT3U(error, ==, 0);
3883 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3885 zfs_log_rename(zilog, tx, TX_RENAME |
3886 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3887 sdl->dl_name, tdzp, tdl->dl_name, szp);
3890 * Update path information for the target vnode
3892 vn_renamepath(tdvp, ZTOV(szp), tnm,
3896 * At this point, we have successfully created
3897 * the target name, but have failed to remove
3898 * the source name. Since the create was done
3899 * with the ZRENAMING flag, there are
3900 * complications; for one, the link count is
3901 * wrong. The easiest way to deal with this
3902 * is to remove the newly created target, and
3903 * return the original error. This must
3904 * succeed; fortunately, it is very unlikely to
3905 * fail, since we just created it.
3907 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3908 ZRENAMING, NULL), ==, 0);
3911 #ifdef FREEBSD_NAMECACHE
3922 zfs_rename_unlock(&zl);
3924 zfs_dirent_unlock(sdl);
3925 zfs_dirent_unlock(tdl);
3928 rw_exit(&sdzp->z_name_lock);
3935 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3936 zil_commit(zilog, 0);
3944 * Insert the indicated symbolic reference entry into the directory.
3946 * IN: dvp - Directory to contain new symbolic link.
3947 * link - Name for new symlink entry.
3948 * vap - Attributes of new entry.
3949 * target - Target path of new symlink.
3950 * cr - credentials of caller.
3951 * ct - caller context
3952 * flags - case flags
3954 * RETURN: 0 if success
3955 * error code if failure
3958 * dvp - ctime|mtime updated
3962 zfs_symlink(vnode_t *dvp, vnode_t **vpp, char *name, vattr_t *vap, char *link,
3963 cred_t *cr, kthread_t *td)
3965 znode_t *zp, *dzp = VTOZ(dvp);
3968 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3970 uint64_t len = strlen(link);
3973 zfs_acl_ids_t acl_ids;
3974 boolean_t fuid_dirtied;
3975 uint64_t txtype = TX_SYMLINK;
3978 ASSERT(vap->va_type == VLNK);
3982 zilog = zfsvfs->z_log;
3984 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3985 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3989 if (flags & FIGNORECASE)
3992 if (len > MAXPATHLEN) {
3994 return (ENAMETOOLONG);
3997 if ((error = zfs_acl_ids_create(dzp, 0,
3998 vap, cr, NULL, &acl_ids)) != 0) {
4004 * Attempt to lock directory; fail if entry already exists.
4006 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4008 zfs_acl_ids_free(&acl_ids);
4013 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4014 zfs_acl_ids_free(&acl_ids);
4015 zfs_dirent_unlock(dl);
4020 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
4021 zfs_acl_ids_free(&acl_ids);
4022 zfs_dirent_unlock(dl);
4026 tx = dmu_tx_create(zfsvfs->z_os);
4027 fuid_dirtied = zfsvfs->z_fuid_dirty;
4028 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4029 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4030 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4031 ZFS_SA_BASE_ATTR_SIZE + len);
4032 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4033 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4034 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4035 acl_ids.z_aclp->z_acl_bytes);
4038 zfs_fuid_txhold(zfsvfs, tx);
4039 error = dmu_tx_assign(tx, TXG_NOWAIT);
4041 zfs_dirent_unlock(dl);
4042 if (error == ERESTART) {
4047 zfs_acl_ids_free(&acl_ids);
4054 * Create a new object for the symlink.
4055 * for version 4 ZPL datsets the symlink will be an SA attribute
4057 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4060 zfs_fuid_sync(zfsvfs, tx);
4062 mutex_enter(&zp->z_lock);
4064 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4067 zfs_sa_symlink(zp, link, len, tx);
4068 mutex_exit(&zp->z_lock);
4071 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4072 &zp->z_size, sizeof (zp->z_size), tx);
4074 * Insert the new object into the directory.
4076 (void) zfs_link_create(dl, zp, tx, ZNEW);
4078 if (flags & FIGNORECASE)
4080 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4083 zfs_acl_ids_free(&acl_ids);
4087 zfs_dirent_unlock(dl);
4089 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4090 zil_commit(zilog, 0);
4097 * Return, in the buffer contained in the provided uio structure,
4098 * the symbolic path referred to by vp.
4100 * IN: vp - vnode of symbolic link.
4101 * uoip - structure to contain the link path.
4102 * cr - credentials of caller.
4103 * ct - caller context
4105 * OUT: uio - structure to contain the link path.
4107 * RETURN: 0 if success
4108 * error code if failure
4111 * vp - atime updated
4115 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
4117 znode_t *zp = VTOZ(vp);
4118 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4124 mutex_enter(&zp->z_lock);
4126 error = sa_lookup_uio(zp->z_sa_hdl,
4127 SA_ZPL_SYMLINK(zfsvfs), uio);
4129 error = zfs_sa_readlink(zp, uio);
4130 mutex_exit(&zp->z_lock);
4132 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4139 * Insert a new entry into directory tdvp referencing svp.
4141 * IN: tdvp - Directory to contain new entry.
4142 * svp - vnode of new entry.
4143 * name - name of new entry.
4144 * cr - credentials of caller.
4145 * ct - caller context
4147 * RETURN: 0 if success
4148 * error code if failure
4151 * tdvp - ctime|mtime updated
4152 * svp - ctime updated
4156 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4157 caller_context_t *ct, int flags)
4159 znode_t *dzp = VTOZ(tdvp);
4161 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4171 ASSERT(tdvp->v_type == VDIR);
4175 zilog = zfsvfs->z_log;
4177 if (VOP_REALVP(svp, &realvp, ct) == 0)
4181 * POSIX dictates that we return EPERM here.
4182 * Better choices include ENOTSUP or EISDIR.
4184 if (svp->v_type == VDIR) {
4189 if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
4197 /* Prevent links to .zfs/shares files */
4199 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4200 &parent, sizeof (uint64_t))) != 0) {
4204 if (parent == zfsvfs->z_shares_dir) {
4209 if (zfsvfs->z_utf8 && u8_validate(name,
4210 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4214 if (flags & FIGNORECASE)
4218 * We do not support links between attributes and non-attributes
4219 * because of the potential security risk of creating links
4220 * into "normal" file space in order to circumvent restrictions
4221 * imposed in attribute space.
4223 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4229 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4230 if (owner != crgetuid(cr) && secpolicy_basic_link(svp, cr) != 0) {
4235 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4242 * Attempt to lock directory; fail if entry already exists.
4244 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4250 tx = dmu_tx_create(zfsvfs->z_os);
4251 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4252 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4253 zfs_sa_upgrade_txholds(tx, szp);
4254 zfs_sa_upgrade_txholds(tx, dzp);
4255 error = dmu_tx_assign(tx, TXG_NOWAIT);
4257 zfs_dirent_unlock(dl);
4258 if (error == ERESTART) {
4268 error = zfs_link_create(dl, szp, tx, 0);
4271 uint64_t txtype = TX_LINK;
4272 if (flags & FIGNORECASE)
4274 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4279 zfs_dirent_unlock(dl);
4282 vnevent_link(svp, ct);
4285 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4286 zil_commit(zilog, 0);
4294 * zfs_null_putapage() is used when the file system has been force
4295 * unmounted. It just drops the pages.
4299 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4300 size_t *lenp, int flags, cred_t *cr)
4302 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4307 * Push a page out to disk, klustering if possible.
4309 * IN: vp - file to push page to.
4310 * pp - page to push.
4311 * flags - additional flags.
4312 * cr - credentials of caller.
4314 * OUT: offp - start of range pushed.
4315 * lenp - len of range pushed.
4317 * RETURN: 0 if success
4318 * error code if failure
4320 * NOTE: callers must have locked the page to be pushed. On
4321 * exit, the page (and all other pages in the kluster) must be
4326 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4327 size_t *lenp, int flags, cred_t *cr)
4329 znode_t *zp = VTOZ(vp);
4330 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4332 u_offset_t off, koff;
4339 * If our blocksize is bigger than the page size, try to kluster
4340 * multiple pages so that we write a full block (thus avoiding
4341 * a read-modify-write).
4343 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4344 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4345 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4346 ASSERT(koff <= zp->z_size);
4347 if (koff + klen > zp->z_size)
4348 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4349 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4351 ASSERT3U(btop(len), ==, btopr(len));
4354 * Can't push pages past end-of-file.
4356 if (off >= zp->z_size) {
4357 /* ignore all pages */
4360 } else if (off + len > zp->z_size) {
4361 int npages = btopr(zp->z_size - off);
4364 page_list_break(&pp, &trunc, npages);
4365 /* ignore pages past end of file */
4367 pvn_write_done(trunc, flags);
4368 len = zp->z_size - off;
4371 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4372 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4377 tx = dmu_tx_create(zfsvfs->z_os);
4378 dmu_tx_hold_write(tx, zp->z_id, off, len);
4380 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4381 zfs_sa_upgrade_txholds(tx, zp);
4382 err = dmu_tx_assign(tx, TXG_NOWAIT);
4384 if (err == ERESTART) {
4393 if (zp->z_blksz <= PAGESIZE) {
4394 caddr_t va = zfs_map_page(pp, S_READ);
4395 ASSERT3U(len, <=, PAGESIZE);
4396 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4397 zfs_unmap_page(pp, va);
4399 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4403 uint64_t mtime[2], ctime[2];
4404 sa_bulk_attr_t bulk[3];
4407 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4409 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4411 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4413 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4415 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4420 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4430 * Copy the portion of the file indicated from pages into the file.
4431 * The pages are stored in a page list attached to the files vnode.
4433 * IN: vp - vnode of file to push page data to.
4434 * off - position in file to put data.
4435 * len - amount of data to write.
4436 * flags - flags to control the operation.
4437 * cr - credentials of caller.
4438 * ct - caller context.
4440 * RETURN: 0 if success
4441 * error code if failure
4444 * vp - ctime|mtime updated
4448 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4449 caller_context_t *ct)
4451 znode_t *zp = VTOZ(vp);
4452 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4464 * Align this request to the file block size in case we kluster.
4465 * XXX - this can result in pretty aggresive locking, which can
4466 * impact simultanious read/write access. One option might be
4467 * to break up long requests (len == 0) into block-by-block
4468 * operations to get narrower locking.
4470 blksz = zp->z_blksz;
4472 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4475 if (len > 0 && ISP2(blksz))
4476 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4482 * Search the entire vp list for pages >= io_off.
4484 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4485 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4488 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4490 if (off > zp->z_size) {
4491 /* past end of file */
4492 zfs_range_unlock(rl);
4497 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4499 for (off = io_off; io_off < off + len; io_off += io_len) {
4500 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4501 pp = page_lookup(vp, io_off,
4502 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4504 pp = page_lookup_nowait(vp, io_off,
4505 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4508 if (pp != NULL && pvn_getdirty(pp, flags)) {
4512 * Found a dirty page to push
4514 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4522 zfs_range_unlock(rl);
4523 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4524 zil_commit(zfsvfs->z_log, zp->z_id);
4532 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4534 znode_t *zp = VTOZ(vp);
4535 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4538 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4539 if (zp->z_sa_hdl == NULL) {
4541 * The fs has been unmounted, or we did a
4542 * suspend/resume and this file no longer exists.
4545 ASSERT(vp->v_count <= 1);
4548 vrecycle(vp, curthread);
4549 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4553 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4554 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4556 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4557 zfs_sa_upgrade_txholds(tx, zp);
4558 error = dmu_tx_assign(tx, TXG_WAIT);
4562 mutex_enter(&zp->z_lock);
4563 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4564 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4565 zp->z_atime_dirty = 0;
4566 mutex_exit(&zp->z_lock);
4572 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4577 * Bounds-check the seek operation.
4579 * IN: vp - vnode seeking within
4580 * ooff - old file offset
4581 * noffp - pointer to new file offset
4582 * ct - caller context
4584 * RETURN: 0 if success
4585 * EINVAL if new offset invalid
4589 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4590 caller_context_t *ct)
4592 if (vp->v_type == VDIR)
4594 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4598 * Pre-filter the generic locking function to trap attempts to place
4599 * a mandatory lock on a memory mapped file.
4602 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4603 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4605 znode_t *zp = VTOZ(vp);
4606 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4612 * We are following the UFS semantics with respect to mapcnt
4613 * here: If we see that the file is mapped already, then we will
4614 * return an error, but we don't worry about races between this
4615 * function and zfs_map().
4617 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4622 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4626 * If we can't find a page in the cache, we will create a new page
4627 * and fill it with file data. For efficiency, we may try to fill
4628 * multiple pages at once (klustering) to fill up the supplied page
4629 * list. Note that the pages to be filled are held with an exclusive
4630 * lock to prevent access by other threads while they are being filled.
4633 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4634 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4636 znode_t *zp = VTOZ(vp);
4637 page_t *pp, *cur_pp;
4638 objset_t *os = zp->z_zfsvfs->z_os;
4639 u_offset_t io_off, total;
4643 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4645 * We only have a single page, don't bother klustering
4649 pp = page_create_va(vp, io_off, io_len,
4650 PG_EXCL | PG_WAIT, seg, addr);
4653 * Try to find enough pages to fill the page list
4655 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4656 &io_len, off, plsz, 0);
4660 * The page already exists, nothing to do here.
4667 * Fill the pages in the kluster.
4670 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4673 ASSERT3U(io_off, ==, cur_pp->p_offset);
4674 va = zfs_map_page(cur_pp, S_WRITE);
4675 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4677 zfs_unmap_page(cur_pp, va);
4679 /* On error, toss the entire kluster */
4680 pvn_read_done(pp, B_ERROR);
4681 /* convert checksum errors into IO errors */
4686 cur_pp = cur_pp->p_next;
4690 * Fill in the page list array from the kluster starting
4691 * from the desired offset `off'.
4692 * NOTE: the page list will always be null terminated.
4694 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4695 ASSERT(pl == NULL || (*pl)->p_offset == off);
4701 * Return pointers to the pages for the file region [off, off + len]
4702 * in the pl array. If plsz is greater than len, this function may
4703 * also return page pointers from after the specified region
4704 * (i.e. the region [off, off + plsz]). These additional pages are
4705 * only returned if they are already in the cache, or were created as
4706 * part of a klustered read.
4708 * IN: vp - vnode of file to get data from.
4709 * off - position in file to get data from.
4710 * len - amount of data to retrieve.
4711 * plsz - length of provided page list.
4712 * seg - segment to obtain pages for.
4713 * addr - virtual address of fault.
4714 * rw - mode of created pages.
4715 * cr - credentials of caller.
4716 * ct - caller context.
4718 * OUT: protp - protection mode of created pages.
4719 * pl - list of pages created.
4721 * RETURN: 0 if success
4722 * error code if failure
4725 * vp - atime updated
4729 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4730 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4731 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4733 znode_t *zp = VTOZ(vp);
4734 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4738 /* we do our own caching, faultahead is unnecessary */
4741 else if (len > plsz)
4744 len = P2ROUNDUP(len, PAGESIZE);
4745 ASSERT(plsz >= len);
4754 * Loop through the requested range [off, off + len) looking
4755 * for pages. If we don't find a page, we will need to create
4756 * a new page and fill it with data from the file.
4759 if (*pl = page_lookup(vp, off, SE_SHARED))
4761 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4764 ASSERT3U((*pl)->p_offset, ==, off);
4768 ASSERT3U(len, >=, PAGESIZE);
4771 ASSERT3U(plsz, >=, PAGESIZE);
4778 * Fill out the page array with any pages already in the cache.
4781 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4788 * Release any pages we have previously locked.
4793 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4803 * Request a memory map for a section of a file. This code interacts
4804 * with common code and the VM system as follows:
4806 * common code calls mmap(), which ends up in smmap_common()
4808 * this calls VOP_MAP(), which takes you into (say) zfs
4810 * zfs_map() calls as_map(), passing segvn_create() as the callback
4812 * segvn_create() creates the new segment and calls VOP_ADDMAP()
4814 * zfs_addmap() updates z_mapcnt
4818 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4819 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4820 caller_context_t *ct)
4822 znode_t *zp = VTOZ(vp);
4823 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4824 segvn_crargs_t vn_a;
4830 if ((prot & PROT_WRITE) && (zp->z_pflags &
4831 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4836 if ((prot & (PROT_READ | PROT_EXEC)) &&
4837 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4842 if (vp->v_flag & VNOMAP) {
4847 if (off < 0 || len > MAXOFFSET_T - off) {
4852 if (vp->v_type != VREG) {
4858 * If file is locked, disallow mapping.
4860 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4866 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4874 vn_a.offset = (u_offset_t)off;
4875 vn_a.type = flags & MAP_TYPE;
4877 vn_a.maxprot = maxprot;
4880 vn_a.flags = flags & ~MAP_TYPE;
4882 vn_a.lgrp_mem_policy_flags = 0;
4884 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4893 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4894 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4895 caller_context_t *ct)
4897 uint64_t pages = btopr(len);
4899 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4904 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4905 * more accurate mtime for the associated file. Since we don't have a way of
4906 * detecting when the data was actually modified, we have to resort to
4907 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4908 * last page is pushed. The problem occurs when the msync() call is omitted,
4909 * which by far the most common case:
4917 * putpage() via fsflush
4919 * If we wait until fsflush to come along, we can have a modification time that
4920 * is some arbitrary point in the future. In order to prevent this in the
4921 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4926 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4927 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4928 caller_context_t *ct)
4930 uint64_t pages = btopr(len);
4932 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4933 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4935 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4936 vn_has_cached_data(vp))
4937 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
4943 * Free or allocate space in a file. Currently, this function only
4944 * supports the `F_FREESP' command. However, this command is somewhat
4945 * misnamed, as its functionality includes the ability to allocate as
4946 * well as free space.
4948 * IN: vp - vnode of file to free data in.
4949 * cmd - action to take (only F_FREESP supported).
4950 * bfp - section of file to free/alloc.
4951 * flag - current file open mode flags.
4952 * offset - current file offset.
4953 * cr - credentials of caller [UNUSED].
4954 * ct - caller context.
4956 * RETURN: 0 if success
4957 * error code if failure
4960 * vp - ctime|mtime updated
4964 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4965 offset_t offset, cred_t *cr, caller_context_t *ct)
4967 znode_t *zp = VTOZ(vp);
4968 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4975 if (cmd != F_FREESP) {
4980 if (error = convoff(vp, bfp, 0, offset)) {
4985 if (bfp->l_len < 0) {
4991 len = bfp->l_len; /* 0 means from off to end of file */
4993 error = zfs_freesp(zp, off, len, flag, TRUE);
5000 CTASSERT(sizeof(struct zfid_short) <= sizeof(struct fid));
5001 CTASSERT(sizeof(struct zfid_long) <= sizeof(struct fid));
5005 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
5007 znode_t *zp = VTOZ(vp);
5008 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5011 uint64_t object = zp->z_id;
5018 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5019 &gen64, sizeof (uint64_t))) != 0) {
5024 gen = (uint32_t)gen64;
5026 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
5027 fidp->fid_len = size;
5029 zfid = (zfid_short_t *)fidp;
5031 zfid->zf_len = size;
5033 for (i = 0; i < sizeof (zfid->zf_object); i++)
5034 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5036 /* Must have a non-zero generation number to distinguish from .zfs */
5039 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5040 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5042 if (size == LONG_FID_LEN) {
5043 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
5046 zlfid = (zfid_long_t *)fidp;
5048 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
5049 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
5051 /* XXX - this should be the generation number for the objset */
5052 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
5053 zlfid->zf_setgen[i] = 0;
5061 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5062 caller_context_t *ct)
5074 case _PC_FILESIZEBITS:
5078 case _PC_XATTR_EXISTS:
5080 zfsvfs = zp->z_zfsvfs;
5084 error = zfs_dirent_lock(&dl, zp, "", &xzp,
5085 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
5087 zfs_dirent_unlock(dl);
5088 if (!zfs_dirempty(xzp))
5091 } else if (error == ENOENT) {
5093 * If there aren't extended attributes, it's the
5094 * same as having zero of them.
5101 case _PC_SATTR_ENABLED:
5102 case _PC_SATTR_EXISTS:
5103 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5104 (vp->v_type == VREG || vp->v_type == VDIR);
5107 case _PC_ACCESS_FILTERING:
5108 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5112 case _PC_ACL_ENABLED:
5113 *valp = _ACL_ACE_ENABLED;
5116 case _PC_MIN_HOLE_SIZE:
5117 *valp = (int)SPA_MINBLOCKSIZE;
5120 case _PC_TIMESTAMP_RESOLUTION:
5121 /* nanosecond timestamp resolution */
5125 case _PC_ACL_EXTENDED:
5133 case _PC_ACL_PATH_MAX:
5134 *valp = ACL_MAX_ENTRIES;
5138 return (EOPNOTSUPP);
5144 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5145 caller_context_t *ct)
5147 znode_t *zp = VTOZ(vp);
5148 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5150 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5154 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5162 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5163 caller_context_t *ct)
5165 znode_t *zp = VTOZ(vp);
5166 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5168 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5169 zilog_t *zilog = zfsvfs->z_log;
5174 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5176 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5177 zil_commit(zilog, 0);
5185 * Tunable, both must be a power of 2.
5187 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
5188 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
5189 * an arcbuf for a partial block read
5191 int zcr_blksz_min = (1 << 10); /* 1K */
5192 int zcr_blksz_max = (1 << 17); /* 128K */
5196 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5197 caller_context_t *ct)
5199 znode_t *zp = VTOZ(vp);
5200 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5201 int max_blksz = zfsvfs->z_max_blksz;
5202 uio_t *uio = &xuio->xu_uio;
5203 ssize_t size = uio->uio_resid;
5204 offset_t offset = uio->uio_loffset;
5209 int preamble, postamble;
5211 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5219 * Loan out an arc_buf for write if write size is bigger than
5220 * max_blksz, and the file's block size is also max_blksz.
5223 if (size < blksz || zp->z_blksz != blksz) {
5228 * Caller requests buffers for write before knowing where the
5229 * write offset might be (e.g. NFS TCP write).
5234 preamble = P2PHASE(offset, blksz);
5236 preamble = blksz - preamble;
5241 postamble = P2PHASE(size, blksz);
5244 fullblk = size / blksz;
5245 (void) dmu_xuio_init(xuio,
5246 (preamble != 0) + fullblk + (postamble != 0));
5247 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5248 int, postamble, int,
5249 (preamble != 0) + fullblk + (postamble != 0));
5252 * Have to fix iov base/len for partial buffers. They
5253 * currently represent full arc_buf's.
5256 /* data begins in the middle of the arc_buf */
5257 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5260 (void) dmu_xuio_add(xuio, abuf,
5261 blksz - preamble, preamble);
5264 for (i = 0; i < fullblk; i++) {
5265 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5268 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5272 /* data ends in the middle of the arc_buf */
5273 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5276 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5281 * Loan out an arc_buf for read if the read size is larger than
5282 * the current file block size. Block alignment is not
5283 * considered. Partial arc_buf will be loaned out for read.
5285 blksz = zp->z_blksz;
5286 if (blksz < zcr_blksz_min)
5287 blksz = zcr_blksz_min;
5288 if (blksz > zcr_blksz_max)
5289 blksz = zcr_blksz_max;
5290 /* avoid potential complexity of dealing with it */
5291 if (blksz > max_blksz) {
5296 maxsize = zp->z_size - uio->uio_loffset;
5300 if (size < blksz || vn_has_cached_data(vp)) {
5310 uio->uio_extflg = UIO_XUIO;
5311 XUIO_XUZC_RW(xuio) = ioflag;
5318 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5322 int ioflag = XUIO_XUZC_RW(xuio);
5324 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5326 i = dmu_xuio_cnt(xuio);
5328 abuf = dmu_xuio_arcbuf(xuio, i);
5330 * if abuf == NULL, it must be a write buffer
5331 * that has been returned in zfs_write().
5334 dmu_return_arcbuf(abuf);
5335 ASSERT(abuf || ioflag == UIO_WRITE);
5338 dmu_xuio_fini(xuio);
5343 * Predeclare these here so that the compiler assumes that
5344 * this is an "old style" function declaration that does
5345 * not include arguments => we won't get type mismatch errors
5346 * in the initializations that follow.
5348 static int zfs_inval();
5349 static int zfs_isdir();
5363 * Directory vnode operations template
5365 vnodeops_t *zfs_dvnodeops;
5366 const fs_operation_def_t zfs_dvnodeops_template[] = {
5367 VOPNAME_OPEN, { .vop_open = zfs_open },
5368 VOPNAME_CLOSE, { .vop_close = zfs_close },
5369 VOPNAME_READ, { .error = zfs_isdir },
5370 VOPNAME_WRITE, { .error = zfs_isdir },
5371 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5372 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5373 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5374 VOPNAME_ACCESS, { .vop_access = zfs_access },
5375 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5376 VOPNAME_CREATE, { .vop_create = zfs_create },
5377 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5378 VOPNAME_LINK, { .vop_link = zfs_link },
5379 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5380 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5381 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5382 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5383 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5384 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5385 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5386 VOPNAME_FID, { .vop_fid = zfs_fid },
5387 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5388 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5389 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5390 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5391 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5396 * Regular file vnode operations template
5398 vnodeops_t *zfs_fvnodeops;
5399 const fs_operation_def_t zfs_fvnodeops_template[] = {
5400 VOPNAME_OPEN, { .vop_open = zfs_open },
5401 VOPNAME_CLOSE, { .vop_close = zfs_close },
5402 VOPNAME_READ, { .vop_read = zfs_read },
5403 VOPNAME_WRITE, { .vop_write = zfs_write },
5404 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5405 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5406 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5407 VOPNAME_ACCESS, { .vop_access = zfs_access },
5408 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5409 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5410 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5411 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5412 VOPNAME_FID, { .vop_fid = zfs_fid },
5413 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5414 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5415 VOPNAME_SPACE, { .vop_space = zfs_space },
5416 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5417 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5418 VOPNAME_MAP, { .vop_map = zfs_map },
5419 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5420 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5421 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5422 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5423 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5424 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5425 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5426 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5431 * Symbolic link vnode operations template
5433 vnodeops_t *zfs_symvnodeops;
5434 const fs_operation_def_t zfs_symvnodeops_template[] = {
5435 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5436 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5437 VOPNAME_ACCESS, { .vop_access = zfs_access },
5438 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5439 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5440 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5441 VOPNAME_FID, { .vop_fid = zfs_fid },
5442 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5443 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5448 * special share hidden files vnode operations template
5450 vnodeops_t *zfs_sharevnodeops;
5451 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5452 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5453 VOPNAME_ACCESS, { .vop_access = zfs_access },
5454 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5455 VOPNAME_FID, { .vop_fid = zfs_fid },
5456 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5457 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5458 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5459 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5464 * Extended attribute directory vnode operations template
5465 * This template is identical to the directory vnodes
5466 * operation template except for restricted operations:
5469 * Note that there are other restrictions embedded in:
5470 * zfs_create() - restrict type to VREG
5471 * zfs_link() - no links into/out of attribute space
5472 * zfs_rename() - no moves into/out of attribute space
5474 vnodeops_t *zfs_xdvnodeops;
5475 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5476 VOPNAME_OPEN, { .vop_open = zfs_open },
5477 VOPNAME_CLOSE, { .vop_close = zfs_close },
5478 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5479 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5480 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5481 VOPNAME_ACCESS, { .vop_access = zfs_access },
5482 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5483 VOPNAME_CREATE, { .vop_create = zfs_create },
5484 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5485 VOPNAME_LINK, { .vop_link = zfs_link },
5486 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5487 VOPNAME_MKDIR, { .error = zfs_inval },
5488 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5489 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5490 VOPNAME_SYMLINK, { .error = zfs_inval },
5491 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5492 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5493 VOPNAME_FID, { .vop_fid = zfs_fid },
5494 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5495 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5496 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5497 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5498 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5503 * Error vnode operations template
5505 vnodeops_t *zfs_evnodeops;
5506 const fs_operation_def_t zfs_evnodeops_template[] = {
5507 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5508 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5514 ioflags(int ioflags)
5518 if (ioflags & IO_APPEND)
5520 if (ioflags & IO_NDELAY)
5522 if (ioflags & IO_SYNC)
5523 flags |= (FSYNC | FDSYNC | FRSYNC);
5529 zfs_getpages(struct vnode *vp, vm_page_t *m, int count, int reqpage)
5531 znode_t *zp = VTOZ(vp);
5532 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5533 objset_t *os = zp->z_zfsvfs->z_os;
5544 pcount = round_page(count) / PAGE_SIZE;
5546 object = mreq->object;
5549 KASSERT(vp->v_object == object, ("mismatching object"));
5551 VM_OBJECT_LOCK(object);
5552 vm_page_lock_queues();
5553 for (i = 0; i < pcount; i++) {
5558 vm_page_unlock_queues();
5561 if (mreq->valid != VM_PAGE_BITS_ALL)
5562 vm_page_zero_invalid(mreq, TRUE);
5563 VM_OBJECT_UNLOCK(object);
5565 return (VM_PAGER_OK);
5568 PCPU_INC(cnt.v_vnodein);
5569 PCPU_INC(cnt.v_vnodepgsin);
5571 if (IDX_TO_OFF(mreq->pindex) >= object->un_pager.vnp.vnp_size) {
5572 VM_OBJECT_UNLOCK(object);
5574 return (VM_PAGER_BAD);
5578 if (IDX_TO_OFF(mreq->pindex) + size > object->un_pager.vnp.vnp_size)
5579 size = object->un_pager.vnp.vnp_size - IDX_TO_OFF(mreq->pindex);
5581 VM_OBJECT_UNLOCK(object);
5582 va = zfs_map_page(mreq, &sf);
5583 error = dmu_read(os, zp->z_id, IDX_TO_OFF(mreq->pindex),
5584 size, va, DMU_READ_PREFETCH);
5585 if (size != PAGE_SIZE)
5586 bzero(va + size, PAGE_SIZE - size);
5588 VM_OBJECT_LOCK(object);
5591 mreq->valid = VM_PAGE_BITS_ALL;
5592 KASSERT(mreq->dirty == 0, ("zfs_getpages: page %p is dirty", mreq));
5594 VM_OBJECT_UNLOCK(object);
5596 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
5598 return (error ? VM_PAGER_ERROR : VM_PAGER_OK);
5602 zfs_freebsd_getpages(ap)
5603 struct vop_getpages_args /* {
5608 vm_ooffset_t a_offset;
5612 return (zfs_getpages(ap->a_vp, ap->a_m, ap->a_count, ap->a_reqpage));
5616 zfs_freebsd_open(ap)
5617 struct vop_open_args /* {
5620 struct ucred *a_cred;
5621 struct thread *a_td;
5624 vnode_t *vp = ap->a_vp;
5625 znode_t *zp = VTOZ(vp);
5628 error = zfs_open(&vp, ap->a_mode, ap->a_cred, NULL);
5630 vnode_create_vobject(vp, zp->z_size, ap->a_td);
5635 zfs_freebsd_close(ap)
5636 struct vop_close_args /* {
5639 struct ucred *a_cred;
5640 struct thread *a_td;
5644 return (zfs_close(ap->a_vp, ap->a_fflag, 0, 0, ap->a_cred, NULL));
5648 zfs_freebsd_ioctl(ap)
5649 struct vop_ioctl_args /* {
5659 return (zfs_ioctl(ap->a_vp, ap->a_command, (intptr_t)ap->a_data,
5660 ap->a_fflag, ap->a_cred, NULL, NULL));
5664 zfs_freebsd_read(ap)
5665 struct vop_read_args /* {
5669 struct ucred *a_cred;
5673 return (zfs_read(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
5678 zfs_freebsd_write(ap)
5679 struct vop_write_args /* {
5683 struct ucred *a_cred;
5687 if (ap->a_vp->v_type == VREG && ap->a_uio->uio_td != NULL) {
5688 PROC_LOCK(ap->a_uio->uio_td->td_proc);
5689 if (ap->a_uio->uio_offset + ap->a_uio->uio_resid >
5690 lim_cur(ap->a_uio->uio_td->td_proc, RLIMIT_FSIZE)) {
5691 psignal(ap->a_uio->uio_td->td_proc, SIGXFSZ);
5692 PROC_UNLOCK(ap->a_uio->uio_td->td_proc);
5695 PROC_UNLOCK(ap->a_uio->uio_td->td_proc);
5698 return (zfs_write(ap->a_vp, ap->a_uio, ioflags(ap->a_ioflag),
5703 zfs_freebsd_access(ap)
5704 struct vop_access_args /* {
5706 accmode_t a_accmode;
5707 struct ucred *a_cred;
5708 struct thread *a_td;
5715 * ZFS itself only knowns about VREAD, VWRITE, VEXEC and VAPPEND,
5717 accmode = ap->a_accmode & (VREAD|VWRITE|VEXEC|VAPPEND);
5719 error = zfs_access(ap->a_vp, accmode, 0, ap->a_cred, NULL);
5722 * VADMIN has to be handled by vaccess().
5725 accmode = ap->a_accmode & ~(VREAD|VWRITE|VEXEC|VAPPEND);
5727 vnode_t *vp = ap->a_vp;
5728 znode_t *zp = VTOZ(vp);
5730 error = vaccess(vp->v_type, zp->z_mode, zp->z_uid,
5731 zp->z_gid, accmode, ap->a_cred, NULL);
5739 zfs_freebsd_lookup(ap)
5740 struct vop_lookup_args /* {
5741 struct vnode *a_dvp;
5742 struct vnode **a_vpp;
5743 struct componentname *a_cnp;
5746 struct componentname *cnp = ap->a_cnp;
5747 char nm[NAME_MAX + 1];
5749 ASSERT(cnp->cn_namelen < sizeof(nm));
5750 strlcpy(nm, cnp->cn_nameptr, MIN(cnp->cn_namelen + 1, sizeof(nm)));
5752 return (zfs_lookup(ap->a_dvp, nm, ap->a_vpp, cnp, cnp->cn_nameiop,
5753 cnp->cn_cred, cnp->cn_thread, 0));
5757 zfs_freebsd_create(ap)
5758 struct vop_create_args /* {
5759 struct vnode *a_dvp;
5760 struct vnode **a_vpp;
5761 struct componentname *a_cnp;
5762 struct vattr *a_vap;
5765 struct componentname *cnp = ap->a_cnp;
5766 vattr_t *vap = ap->a_vap;
5769 ASSERT(cnp->cn_flags & SAVENAME);
5771 vattr_init_mask(vap);
5772 mode = vap->va_mode & ALLPERMS;
5774 return (zfs_create(ap->a_dvp, cnp->cn_nameptr, vap, !EXCL, mode,
5775 ap->a_vpp, cnp->cn_cred, cnp->cn_thread));
5779 zfs_freebsd_remove(ap)
5780 struct vop_remove_args /* {
5781 struct vnode *a_dvp;
5783 struct componentname *a_cnp;
5787 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5789 return (zfs_remove(ap->a_dvp, ap->a_cnp->cn_nameptr,
5790 ap->a_cnp->cn_cred, NULL, 0));
5794 zfs_freebsd_mkdir(ap)
5795 struct vop_mkdir_args /* {
5796 struct vnode *a_dvp;
5797 struct vnode **a_vpp;
5798 struct componentname *a_cnp;
5799 struct vattr *a_vap;
5802 vattr_t *vap = ap->a_vap;
5804 ASSERT(ap->a_cnp->cn_flags & SAVENAME);
5806 vattr_init_mask(vap);
5808 return (zfs_mkdir(ap->a_dvp, ap->a_cnp->cn_nameptr, vap, ap->a_vpp,
5809 ap->a_cnp->cn_cred, NULL, 0, NULL));
5813 zfs_freebsd_rmdir(ap)
5814 struct vop_rmdir_args /* {
5815 struct vnode *a_dvp;
5817 struct componentname *a_cnp;
5820 struct componentname *cnp = ap->a_cnp;
5822 ASSERT(cnp->cn_flags & SAVENAME);
5824 return (zfs_rmdir(ap->a_dvp, cnp->cn_nameptr, NULL, cnp->cn_cred, NULL, 0));
5828 zfs_freebsd_readdir(ap)
5829 struct vop_readdir_args /* {
5832 struct ucred *a_cred;
5839 return (zfs_readdir(ap->a_vp, ap->a_uio, ap->a_cred, ap->a_eofflag,
5840 ap->a_ncookies, ap->a_cookies));
5844 zfs_freebsd_fsync(ap)
5845 struct vop_fsync_args /* {
5848 struct thread *a_td;
5853 return (zfs_fsync(ap->a_vp, 0, ap->a_td->td_ucred, NULL));
5857 zfs_freebsd_getattr(ap)
5858 struct vop_getattr_args /* {
5860 struct vattr *a_vap;
5861 struct ucred *a_cred;
5864 vattr_t *vap = ap->a_vap;
5870 xvap.xva_vattr = *vap;
5871 xvap.xva_vattr.va_mask |= AT_XVATTR;
5873 /* Convert chflags into ZFS-type flags. */
5874 /* XXX: what about SF_SETTABLE?. */
5875 XVA_SET_REQ(&xvap, XAT_IMMUTABLE);
5876 XVA_SET_REQ(&xvap, XAT_APPENDONLY);
5877 XVA_SET_REQ(&xvap, XAT_NOUNLINK);
5878 XVA_SET_REQ(&xvap, XAT_NODUMP);
5879 error = zfs_getattr(ap->a_vp, (vattr_t *)&xvap, 0, ap->a_cred, NULL);
5883 /* Convert ZFS xattr into chflags. */
5884 #define FLAG_CHECK(fflag, xflag, xfield) do { \
5885 if (XVA_ISSET_RTN(&xvap, (xflag)) && (xfield) != 0) \
5886 fflags |= (fflag); \
5888 FLAG_CHECK(SF_IMMUTABLE, XAT_IMMUTABLE,
5889 xvap.xva_xoptattrs.xoa_immutable);
5890 FLAG_CHECK(SF_APPEND, XAT_APPENDONLY,
5891 xvap.xva_xoptattrs.xoa_appendonly);
5892 FLAG_CHECK(SF_NOUNLINK, XAT_NOUNLINK,
5893 xvap.xva_xoptattrs.xoa_nounlink);
5894 FLAG_CHECK(UF_NODUMP, XAT_NODUMP,
5895 xvap.xva_xoptattrs.xoa_nodump);
5897 *vap = xvap.xva_vattr;
5898 vap->va_flags = fflags;
5903 zfs_freebsd_setattr(ap)
5904 struct vop_setattr_args /* {
5906 struct vattr *a_vap;
5907 struct ucred *a_cred;
5910 vnode_t *vp = ap->a_vp;
5911 vattr_t *vap = ap->a_vap;
5912 cred_t *cred = ap->a_cred;
5917 vattr_init_mask(vap);
5918 vap->va_mask &= ~AT_NOSET;
5921 xvap.xva_vattr = *vap;
5923 zflags = VTOZ(vp)->z_pflags;
5925 if (vap->va_flags != VNOVAL) {
5926 zfsvfs_t *zfsvfs = VTOZ(vp)->z_zfsvfs;
5929 if (zfsvfs->z_use_fuids == B_FALSE)
5930 return (EOPNOTSUPP);
5932 fflags = vap->va_flags;
5933 if ((fflags & ~(SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK|UF_NODUMP)) != 0)
5934 return (EOPNOTSUPP);
5936 * Unprivileged processes are not permitted to unset system
5937 * flags, or modify flags if any system flags are set.
5938 * Privileged non-jail processes may not modify system flags
5939 * if securelevel > 0 and any existing system flags are set.
5940 * Privileged jail processes behave like privileged non-jail
5941 * processes if the security.jail.chflags_allowed sysctl is
5942 * is non-zero; otherwise, they behave like unprivileged
5945 if (secpolicy_fs_owner(vp->v_mount, cred) == 0 ||
5946 priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0) == 0) {
5948 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5949 error = securelevel_gt(cred, 0);
5955 * Callers may only modify the file flags on objects they
5956 * have VADMIN rights for.
5958 if ((error = VOP_ACCESS(vp, VADMIN, cred, curthread)) != 0)
5961 (ZFS_IMMUTABLE | ZFS_APPENDONLY | ZFS_NOUNLINK)) {
5965 (SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)) {
5970 #define FLAG_CHANGE(fflag, zflag, xflag, xfield) do { \
5971 if (((fflags & (fflag)) && !(zflags & (zflag))) || \
5972 ((zflags & (zflag)) && !(fflags & (fflag)))) { \
5973 XVA_SET_REQ(&xvap, (xflag)); \
5974 (xfield) = ((fflags & (fflag)) != 0); \
5977 /* Convert chflags into ZFS-type flags. */
5978 /* XXX: what about SF_SETTABLE?. */
5979 FLAG_CHANGE(SF_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
5980 xvap.xva_xoptattrs.xoa_immutable);
5981 FLAG_CHANGE(SF_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
5982 xvap.xva_xoptattrs.xoa_appendonly);
5983 FLAG_CHANGE(SF_NOUNLINK, ZFS_NOUNLINK, XAT_NOUNLINK,
5984 xvap.xva_xoptattrs.xoa_nounlink);
5985 FLAG_CHANGE(UF_NODUMP, ZFS_NODUMP, XAT_NODUMP,
5986 xvap.xva_xoptattrs.xoa_nodump);
5989 return (zfs_setattr(vp, (vattr_t *)&xvap, 0, cred, NULL));
5993 zfs_freebsd_rename(ap)
5994 struct vop_rename_args /* {
5995 struct vnode *a_fdvp;
5996 struct vnode *a_fvp;
5997 struct componentname *a_fcnp;
5998 struct vnode *a_tdvp;
5999 struct vnode *a_tvp;
6000 struct componentname *a_tcnp;
6003 vnode_t *fdvp = ap->a_fdvp;
6004 vnode_t *fvp = ap->a_fvp;
6005 vnode_t *tdvp = ap->a_tdvp;
6006 vnode_t *tvp = ap->a_tvp;
6009 ASSERT(ap->a_fcnp->cn_flags & (SAVENAME|SAVESTART));
6010 ASSERT(ap->a_tcnp->cn_flags & (SAVENAME|SAVESTART));
6012 error = zfs_rename(fdvp, ap->a_fcnp->cn_nameptr, tdvp,
6013 ap->a_tcnp->cn_nameptr, ap->a_fcnp->cn_cred, NULL, 0);
6028 zfs_freebsd_symlink(ap)
6029 struct vop_symlink_args /* {
6030 struct vnode *a_dvp;
6031 struct vnode **a_vpp;
6032 struct componentname *a_cnp;
6033 struct vattr *a_vap;
6037 struct componentname *cnp = ap->a_cnp;
6038 vattr_t *vap = ap->a_vap;
6040 ASSERT(cnp->cn_flags & SAVENAME);
6042 vap->va_type = VLNK; /* FreeBSD: Syscall only sets va_mode. */
6043 vattr_init_mask(vap);
6045 return (zfs_symlink(ap->a_dvp, ap->a_vpp, cnp->cn_nameptr, vap,
6046 ap->a_target, cnp->cn_cred, cnp->cn_thread));
6050 zfs_freebsd_readlink(ap)
6051 struct vop_readlink_args /* {
6054 struct ucred *a_cred;
6058 return (zfs_readlink(ap->a_vp, ap->a_uio, ap->a_cred, NULL));
6062 zfs_freebsd_link(ap)
6063 struct vop_link_args /* {
6064 struct vnode *a_tdvp;
6066 struct componentname *a_cnp;
6069 struct componentname *cnp = ap->a_cnp;
6071 ASSERT(cnp->cn_flags & SAVENAME);
6073 return (zfs_link(ap->a_tdvp, ap->a_vp, cnp->cn_nameptr, cnp->cn_cred, NULL, 0));
6077 zfs_freebsd_inactive(ap)
6078 struct vop_inactive_args /* {
6080 struct thread *a_td;
6083 vnode_t *vp = ap->a_vp;
6085 zfs_inactive(vp, ap->a_td->td_ucred, NULL);
6090 zfs_reclaim_complete(void *arg, int pending)
6093 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6095 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6096 if (zp->z_sa_hdl != NULL) {
6097 ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id);
6098 zfs_znode_dmu_fini(zp);
6099 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
6102 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6104 * If the file system is being unmounted, there is a process waiting
6105 * for us, wake it up.
6107 if (zfsvfs->z_unmounted)
6112 zfs_freebsd_reclaim(ap)
6113 struct vop_reclaim_args /* {
6115 struct thread *a_td;
6118 vnode_t *vp = ap->a_vp;
6119 znode_t *zp = VTOZ(vp);
6120 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
6123 rlocked = rw_tryenter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6128 * Destroy the vm object and flush associated pages.
6130 vnode_destroy_vobject(vp);
6132 mutex_enter(&zp->z_lock);
6134 mutex_exit(&zp->z_lock);
6136 if (zp->z_unlinked) {
6138 } else if (!rlocked) {
6139 TASK_INIT(&zp->z_task, 0, zfs_reclaim_complete, zp);
6140 taskqueue_enqueue(taskqueue_thread, &zp->z_task);
6141 } else if (zp->z_sa_hdl == NULL) {
6143 } else /* if (!zp->z_unlinked && zp->z_dbuf != NULL) */ {
6146 locked = MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)) ? 2 :
6147 ZFS_OBJ_HOLD_TRYENTER(zfsvfs, zp->z_id);
6150 * Lock can't be obtained due to deadlock possibility,
6151 * so defer znode destruction.
6153 TASK_INIT(&zp->z_task, 0, zfs_reclaim_complete, zp);
6154 taskqueue_enqueue(taskqueue_thread, &zp->z_task);
6156 zfs_znode_dmu_fini(zp);
6158 ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
6164 ASSERT(vp->v_holdcnt >= 1);
6167 rw_exit(&zfsvfs->z_teardown_inactive_lock);
6173 struct vop_fid_args /* {
6179 return (zfs_fid(ap->a_vp, (void *)ap->a_fid, NULL));
6183 zfs_freebsd_pathconf(ap)
6184 struct vop_pathconf_args /* {
6187 register_t *a_retval;
6193 error = zfs_pathconf(ap->a_vp, ap->a_name, &val, curthread->td_ucred, NULL);
6195 *ap->a_retval = val;
6196 else if (error == EOPNOTSUPP)
6197 error = vop_stdpathconf(ap);
6202 zfs_freebsd_fifo_pathconf(ap)
6203 struct vop_pathconf_args /* {
6206 register_t *a_retval;
6210 switch (ap->a_name) {
6211 case _PC_ACL_EXTENDED:
6213 case _PC_ACL_PATH_MAX:
6214 case _PC_MAC_PRESENT:
6215 return (zfs_freebsd_pathconf(ap));
6217 return (fifo_specops.vop_pathconf(ap));
6222 * FreeBSD's extended attributes namespace defines file name prefix for ZFS'
6223 * extended attribute name:
6226 * system freebsd:system:
6227 * user (none, can be used to access ZFS fsattr(5) attributes
6228 * created on Solaris)
6231 zfs_create_attrname(int attrnamespace, const char *name, char *attrname,
6234 const char *namespace, *prefix, *suffix;
6236 /* We don't allow '/' character in attribute name. */
6237 if (strchr(name, '/') != NULL)
6239 /* We don't allow attribute names that start with "freebsd:" string. */
6240 if (strncmp(name, "freebsd:", 8) == 0)
6243 bzero(attrname, size);
6245 switch (attrnamespace) {
6246 case EXTATTR_NAMESPACE_USER:
6248 prefix = "freebsd:";
6249 namespace = EXTATTR_NAMESPACE_USER_STRING;
6253 * This is the default namespace by which we can access all
6254 * attributes created on Solaris.
6256 prefix = namespace = suffix = "";
6259 case EXTATTR_NAMESPACE_SYSTEM:
6260 prefix = "freebsd:";
6261 namespace = EXTATTR_NAMESPACE_SYSTEM_STRING;
6264 case EXTATTR_NAMESPACE_EMPTY:
6268 if (snprintf(attrname, size, "%s%s%s%s", prefix, namespace, suffix,
6270 return (ENAMETOOLONG);
6276 * Vnode operating to retrieve a named extended attribute.
6279 zfs_getextattr(struct vop_getextattr_args *ap)
6282 IN struct vnode *a_vp;
6283 IN int a_attrnamespace;
6284 IN const char *a_name;
6285 INOUT struct uio *a_uio;
6287 IN struct ucred *a_cred;
6288 IN struct thread *a_td;
6292 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6293 struct thread *td = ap->a_td;
6294 struct nameidata nd;
6297 vnode_t *xvp = NULL, *vp;
6300 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6301 ap->a_cred, ap->a_td, VREAD);
6305 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6312 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6320 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
6322 error = vn_open_cred(&nd, &flags, 0, 0, ap->a_cred, NULL);
6324 NDFREE(&nd, NDF_ONLY_PNBUF);
6327 if (error == ENOENT)
6332 if (ap->a_size != NULL) {
6333 error = VOP_GETATTR(vp, &va, ap->a_cred);
6335 *ap->a_size = (size_t)va.va_size;
6336 } else if (ap->a_uio != NULL)
6337 error = VOP_READ(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
6340 vn_close(vp, flags, ap->a_cred, td);
6347 * Vnode operation to remove a named attribute.
6350 zfs_deleteextattr(struct vop_deleteextattr_args *ap)
6353 IN struct vnode *a_vp;
6354 IN int a_attrnamespace;
6355 IN const char *a_name;
6356 IN struct ucred *a_cred;
6357 IN struct thread *a_td;
6361 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6362 struct thread *td = ap->a_td;
6363 struct nameidata nd;
6366 vnode_t *xvp = NULL, *vp;
6369 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6370 ap->a_cred, ap->a_td, VWRITE);
6374 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6381 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6388 NDINIT_ATVP(&nd, DELETE, NOFOLLOW | LOCKPARENT | LOCKLEAF | MPSAFE,
6389 UIO_SYSSPACE, attrname, xvp, td);
6392 NDFREE(&nd, NDF_ONLY_PNBUF);
6395 if (error == ENOENT)
6399 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
6402 if (vp == nd.ni_dvp)
6412 * Vnode operation to set a named attribute.
6415 zfs_setextattr(struct vop_setextattr_args *ap)
6418 IN struct vnode *a_vp;
6419 IN int a_attrnamespace;
6420 IN const char *a_name;
6421 INOUT struct uio *a_uio;
6422 IN struct ucred *a_cred;
6423 IN struct thread *a_td;
6427 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6428 struct thread *td = ap->a_td;
6429 struct nameidata nd;
6432 vnode_t *xvp = NULL, *vp;
6435 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6436 ap->a_cred, ap->a_td, VWRITE);
6440 error = zfs_create_attrname(ap->a_attrnamespace, ap->a_name, attrname,
6447 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6448 LOOKUP_XATTR | CREATE_XATTR_DIR);
6454 flags = FFLAGS(O_WRONLY | O_CREAT);
6455 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | MPSAFE, UIO_SYSSPACE, attrname,
6457 error = vn_open_cred(&nd, &flags, 0600, 0, ap->a_cred, NULL);
6459 NDFREE(&nd, NDF_ONLY_PNBUF);
6467 error = VOP_SETATTR(vp, &va, ap->a_cred);
6469 VOP_WRITE(vp, ap->a_uio, IO_UNIT | IO_SYNC, ap->a_cred);
6472 vn_close(vp, flags, ap->a_cred, td);
6479 * Vnode operation to retrieve extended attributes on a vnode.
6482 zfs_listextattr(struct vop_listextattr_args *ap)
6485 IN struct vnode *a_vp;
6486 IN int a_attrnamespace;
6487 INOUT struct uio *a_uio;
6489 IN struct ucred *a_cred;
6490 IN struct thread *a_td;
6494 zfsvfs_t *zfsvfs = VTOZ(ap->a_vp)->z_zfsvfs;
6495 struct thread *td = ap->a_td;
6496 struct nameidata nd;
6497 char attrprefix[16];
6498 u_char dirbuf[sizeof(struct dirent)];
6501 struct uio auio, *uio = ap->a_uio;
6502 size_t *sizep = ap->a_size;
6504 vnode_t *xvp = NULL, *vp;
6505 int done, error, eof, pos;
6507 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
6508 ap->a_cred, ap->a_td, VREAD);
6512 error = zfs_create_attrname(ap->a_attrnamespace, "", attrprefix,
6513 sizeof(attrprefix));
6516 plen = strlen(attrprefix);
6523 error = zfs_lookup(ap->a_vp, NULL, &xvp, NULL, 0, ap->a_cred, td,
6528 * ENOATTR means that the EA directory does not yet exist,
6529 * i.e. there are no extended attributes there.
6531 if (error == ENOATTR)
6536 NDINIT_ATVP(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | MPSAFE,
6537 UIO_SYSSPACE, ".", xvp, td);
6540 NDFREE(&nd, NDF_ONLY_PNBUF);
6546 auio.uio_iov = &aiov;
6547 auio.uio_iovcnt = 1;
6548 auio.uio_segflg = UIO_SYSSPACE;
6550 auio.uio_rw = UIO_READ;
6551 auio.uio_offset = 0;
6556 aiov.iov_base = (void *)dirbuf;
6557 aiov.iov_len = sizeof(dirbuf);
6558 auio.uio_resid = sizeof(dirbuf);
6559 error = VOP_READDIR(vp, &auio, ap->a_cred, &eof, NULL, NULL);
6560 done = sizeof(dirbuf) - auio.uio_resid;
6563 for (pos = 0; pos < done;) {
6564 dp = (struct dirent *)(dirbuf + pos);
6565 pos += dp->d_reclen;
6567 * XXX: Temporarily we also accept DT_UNKNOWN, as this
6568 * is what we get when attribute was created on Solaris.
6570 if (dp->d_type != DT_REG && dp->d_type != DT_UNKNOWN)
6572 if (plen == 0 && strncmp(dp->d_name, "freebsd:", 8) == 0)
6574 else if (strncmp(dp->d_name, attrprefix, plen) != 0)
6576 nlen = dp->d_namlen - plen;
6579 else if (uio != NULL) {
6581 * Format of extattr name entry is one byte for
6582 * length and the rest for name.
6584 error = uiomove(&nlen, 1, uio->uio_rw, uio);
6586 error = uiomove(dp->d_name + plen, nlen,
6593 } while (!eof && error == 0);
6602 zfs_freebsd_getacl(ap)
6603 struct vop_getacl_args /* {
6612 vsecattr_t vsecattr;
6614 if (ap->a_type != ACL_TYPE_NFS4)
6617 vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT;
6618 if (error = zfs_getsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL))
6621 error = acl_from_aces(ap->a_aclp, vsecattr.vsa_aclentp, vsecattr.vsa_aclcnt);
6622 if (vsecattr.vsa_aclentp != NULL)
6623 kmem_free(vsecattr.vsa_aclentp, vsecattr.vsa_aclentsz);
6629 zfs_freebsd_setacl(ap)
6630 struct vop_setacl_args /* {
6639 vsecattr_t vsecattr;
6640 int aclbsize; /* size of acl list in bytes */
6643 if (ap->a_type != ACL_TYPE_NFS4)
6646 if (ap->a_aclp->acl_cnt < 1 || ap->a_aclp->acl_cnt > MAX_ACL_ENTRIES)
6650 * With NFSv4 ACLs, chmod(2) may need to add additional entries,
6651 * splitting every entry into two and appending "canonical six"
6652 * entries at the end. Don't allow for setting an ACL that would
6653 * cause chmod(2) to run out of ACL entries.
6655 if (ap->a_aclp->acl_cnt * 2 + 6 > ACL_MAX_ENTRIES)
6658 error = acl_nfs4_check(ap->a_aclp, ap->a_vp->v_type == VDIR);
6662 vsecattr.vsa_mask = VSA_ACE;
6663 aclbsize = ap->a_aclp->acl_cnt * sizeof(ace_t);
6664 vsecattr.vsa_aclentp = kmem_alloc(aclbsize, KM_SLEEP);
6665 aaclp = vsecattr.vsa_aclentp;
6666 vsecattr.vsa_aclentsz = aclbsize;
6668 aces_from_acl(vsecattr.vsa_aclentp, &vsecattr.vsa_aclcnt, ap->a_aclp);
6669 error = zfs_setsecattr(ap->a_vp, &vsecattr, 0, ap->a_cred, NULL);
6670 kmem_free(aaclp, aclbsize);
6676 zfs_freebsd_aclcheck(ap)
6677 struct vop_aclcheck_args /* {
6686 return (EOPNOTSUPP);
6689 struct vop_vector zfs_vnodeops;
6690 struct vop_vector zfs_fifoops;
6691 struct vop_vector zfs_shareops;
6693 struct vop_vector zfs_vnodeops = {
6694 .vop_default = &default_vnodeops,
6695 .vop_inactive = zfs_freebsd_inactive,
6696 .vop_reclaim = zfs_freebsd_reclaim,
6697 .vop_access = zfs_freebsd_access,
6698 #ifdef FREEBSD_NAMECACHE
6699 .vop_lookup = vfs_cache_lookup,
6700 .vop_cachedlookup = zfs_freebsd_lookup,
6702 .vop_lookup = zfs_freebsd_lookup,
6704 .vop_getattr = zfs_freebsd_getattr,
6705 .vop_setattr = zfs_freebsd_setattr,
6706 .vop_create = zfs_freebsd_create,
6707 .vop_mknod = zfs_freebsd_create,
6708 .vop_mkdir = zfs_freebsd_mkdir,
6709 .vop_readdir = zfs_freebsd_readdir,
6710 .vop_fsync = zfs_freebsd_fsync,
6711 .vop_open = zfs_freebsd_open,
6712 .vop_close = zfs_freebsd_close,
6713 .vop_rmdir = zfs_freebsd_rmdir,
6714 .vop_ioctl = zfs_freebsd_ioctl,
6715 .vop_link = zfs_freebsd_link,
6716 .vop_symlink = zfs_freebsd_symlink,
6717 .vop_readlink = zfs_freebsd_readlink,
6718 .vop_read = zfs_freebsd_read,
6719 .vop_write = zfs_freebsd_write,
6720 .vop_remove = zfs_freebsd_remove,
6721 .vop_rename = zfs_freebsd_rename,
6722 .vop_pathconf = zfs_freebsd_pathconf,
6723 .vop_bmap = VOP_EOPNOTSUPP,
6724 .vop_fid = zfs_freebsd_fid,
6725 .vop_getextattr = zfs_getextattr,
6726 .vop_deleteextattr = zfs_deleteextattr,
6727 .vop_setextattr = zfs_setextattr,
6728 .vop_listextattr = zfs_listextattr,
6729 .vop_getacl = zfs_freebsd_getacl,
6730 .vop_setacl = zfs_freebsd_setacl,
6731 .vop_aclcheck = zfs_freebsd_aclcheck,
6732 .vop_getpages = zfs_freebsd_getpages,
6735 struct vop_vector zfs_fifoops = {
6736 .vop_default = &fifo_specops,
6737 .vop_fsync = zfs_freebsd_fsync,
6738 .vop_access = zfs_freebsd_access,
6739 .vop_getattr = zfs_freebsd_getattr,
6740 .vop_inactive = zfs_freebsd_inactive,
6741 .vop_read = VOP_PANIC,
6742 .vop_reclaim = zfs_freebsd_reclaim,
6743 .vop_setattr = zfs_freebsd_setattr,
6744 .vop_write = VOP_PANIC,
6745 .vop_pathconf = zfs_freebsd_fifo_pathconf,
6746 .vop_fid = zfs_freebsd_fid,
6747 .vop_getacl = zfs_freebsd_getacl,
6748 .vop_setacl = zfs_freebsd_setacl,
6749 .vop_aclcheck = zfs_freebsd_aclcheck,
6753 * special share hidden files vnode operations template
6755 struct vop_vector zfs_shareops = {
6756 .vop_default = &default_vnodeops,
6757 .vop_access = zfs_freebsd_access,
6758 .vop_inactive = zfs_freebsd_inactive,
6759 .vop_reclaim = zfs_freebsd_reclaim,
6760 .vop_fid = zfs_freebsd_fid,
6761 .vop_pathconf = zfs_freebsd_pathconf,