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 */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
37 #include <sys/vfs_opreg.h>
38 #include <sys/vnode.h>
42 #include <sys/taskq.h>
44 #include <sys/vmsystm.h>
45 #include <sys/atomic.h>
47 #include <vm/seg_vn.h>
51 #include <vm/seg_kpm.h>
53 #include <sys/pathname.h>
54 #include <sys/cmn_err.h>
55 #include <sys/errno.h>
56 #include <sys/unistd.h>
57 #include <sys/zfs_dir.h>
58 #include <sys/zfs_acl.h>
59 #include <sys/zfs_ioctl.h>
60 #include <sys/fs/zfs.h>
62 #include <sys/dmu_objset.h>
68 #include <sys/dirent.h>
69 #include <sys/policy.h>
70 #include <sys/sunddi.h>
71 #include <sys/filio.h>
73 #include "fs/fs_subr.h"
74 #include <sys/zfs_ctldir.h>
75 #include <sys/zfs_fuid.h>
76 #include <sys/zfs_sa.h>
78 #include <sys/zfs_rlock.h>
79 #include <sys/extdirent.h>
80 #include <sys/kidmap.h>
87 * Each vnode op performs some logical unit of work. To do this, the ZPL must
88 * properly lock its in-core state, create a DMU transaction, do the work,
89 * record this work in the intent log (ZIL), commit the DMU transaction,
90 * and wait for the intent log to commit if it is a synchronous operation.
91 * Moreover, the vnode ops must work in both normal and log replay context.
92 * The ordering of events is important to avoid deadlocks and references
93 * to freed memory. The example below illustrates the following Big Rules:
95 * (1) A check must be made in each zfs thread for a mounted file system.
96 * This is done avoiding races using ZFS_ENTER(zfsvfs).
97 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
98 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
99 * can return EIO from the calling function.
101 * (2) VN_RELE() should always be the last thing except for zil_commit()
102 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
103 * First, if it's the last reference, the vnode/znode
104 * can be freed, so the zp may point to freed memory. Second, the last
105 * reference will call zfs_zinactive(), which may induce a lot of work --
106 * pushing cached pages (which acquires range locks) and syncing out
107 * cached atime changes. Third, zfs_zinactive() may require a new tx,
108 * which could deadlock the system if you were already holding one.
109 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
111 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
112 * as they can span dmu_tx_assign() calls.
114 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
115 * This is critical because we don't want to block while holding locks.
116 * Note, in particular, that if a lock is sometimes acquired before
117 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
118 * use a non-blocking assign can deadlock the system. The scenario:
120 * Thread A has grabbed a lock before calling dmu_tx_assign().
121 * Thread B is in an already-assigned tx, and blocks for this lock.
122 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
123 * forever, because the previous txg can't quiesce until B's tx commits.
125 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
126 * then drop all locks, call dmu_tx_wait(), and try again.
128 * (5) If the operation succeeded, generate the intent log entry for it
129 * before dropping locks. This ensures that the ordering of events
130 * in the intent log matches the order in which they actually occurred.
131 * During ZIL replay the zfs_log_* functions will update the sequence
132 * number to indicate the zil transaction has replayed.
134 * (6) At the end of each vnode op, the DMU tx must always commit,
135 * regardless of whether there were any errors.
137 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
138 * to ensure that synchronous semantics are provided when necessary.
140 * In general, this is how things should be ordered in each vnode op:
142 * ZFS_ENTER(zfsvfs); // exit if unmounted
144 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
145 * rw_enter(...); // grab any other locks you need
146 * tx = dmu_tx_create(...); // get DMU tx
147 * dmu_tx_hold_*(); // hold each object you might modify
148 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
150 * rw_exit(...); // drop locks
151 * zfs_dirent_unlock(dl); // unlock directory entry
152 * VN_RELE(...); // release held vnodes
153 * if (error == ERESTART) {
158 * dmu_tx_abort(tx); // abort DMU tx
159 * ZFS_EXIT(zfsvfs); // finished in zfs
160 * return (error); // really out of space
162 * error = do_real_work(); // do whatever this VOP does
164 * zfs_log_*(...); // on success, make ZIL entry
165 * dmu_tx_commit(tx); // commit DMU tx -- error or not
166 * rw_exit(...); // drop locks
167 * zfs_dirent_unlock(dl); // unlock directory entry
168 * VN_RELE(...); // release held vnodes
169 * zil_commit(zilog, foid); // synchronous when necessary
170 * ZFS_EXIT(zfsvfs); // finished in zfs
171 * return (error); // done, report error
176 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
178 znode_t *zp = VTOZ(*vpp);
179 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
184 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
185 ((flag & FAPPEND) == 0)) {
190 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
191 ZTOV(zp)->v_type == VREG &&
192 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
193 if (fs_vscan(*vpp, cr, 0) != 0) {
199 /* Keep a count of the synchronous opens in the znode */
200 if (flag & (FSYNC | FDSYNC))
201 atomic_inc_32(&zp->z_sync_cnt);
209 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
210 caller_context_t *ct)
212 znode_t *zp = VTOZ(vp);
213 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
216 * Clean up any locks held by this process on the vp.
218 cleanlocks(vp, ddi_get_pid(), 0);
219 cleanshares(vp, ddi_get_pid());
224 /* Decrement the synchronous opens in the znode */
225 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
226 atomic_dec_32(&zp->z_sync_cnt);
228 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
229 ZTOV(zp)->v_type == VREG &&
230 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
231 VERIFY(fs_vscan(vp, cr, 1) == 0);
238 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
239 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
242 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
244 znode_t *zp = VTOZ(vp);
245 uint64_t noff = (uint64_t)*off; /* new offset */
250 file_sz = zp->z_size;
251 if (noff >= file_sz) {
255 if (cmd == _FIO_SEEK_HOLE)
260 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
263 if ((error == ESRCH) || (noff > file_sz)) {
265 * Handle the virtual hole at the end of file.
282 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
283 int *rvalp, caller_context_t *ct)
292 return (zfs_sync(vp->v_vfsp, 0, cred));
295 * The following two ioctls are used by bfu. Faking out,
296 * necessary to avoid bfu errors.
304 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
308 zfsvfs = zp->z_zfsvfs;
312 /* offset parameter is in/out */
313 error = zfs_holey(vp, com, &off);
317 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
324 #if defined(_KERNEL) && defined(HAVE_UIO_RW)
326 * Utility functions to map and unmap a single physical page. These
327 * are used to manage the mappable copies of ZFS file data, and therefore
328 * do not update ref/mod bits.
331 zfs_map_page(page_t *pp, enum seg_rw rw)
334 return (hat_kpm_mapin(pp, 0));
335 ASSERT(rw == S_READ || rw == S_WRITE);
336 return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
341 zfs_unmap_page(page_t *pp, caddr_t addr)
344 hat_kpm_mapout(pp, 0, addr);
349 #endif /* _KERNEL && HAVE_UIO_RW */
352 * When a file is memory mapped, we must keep the IO data synchronized
353 * between the DMU cache and the memory mapped pages. What this means:
355 * On Write: If we find a memory mapped page, we write to *both*
356 * the page and the dmu buffer.
359 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
363 off = start & PAGEOFFSET;
364 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
366 uint64_t nbytes = MIN(PAGESIZE - off, len);
368 if (pp = page_lookup(vp, start, SE_SHARED)) {
371 va = zfs_map_page(pp, S_WRITE);
372 (void) dmu_read(os, oid, start+off, nbytes, va+off,
374 zfs_unmap_page(pp, va);
383 * When a file is memory mapped, we must keep the IO data synchronized
384 * between the DMU cache and the memory mapped pages. What this means:
386 * On Read: We "read" preferentially from memory mapped pages,
387 * else we default from the dmu buffer.
389 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
390 * the file is memory mapped.
393 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
395 znode_t *zp = VTOZ(vp);
396 objset_t *os = zp->z_zfsvfs->z_os;
401 start = uio->uio_loffset;
402 off = start & PAGEOFFSET;
403 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
405 uint64_t bytes = MIN(PAGESIZE - off, len);
407 if (pp = page_lookup(vp, start, SE_SHARED)) {
410 va = zfs_map_page(pp, S_READ);
411 error = uiomove(va + off, bytes, UIO_READ, uio);
412 zfs_unmap_page(pp, va);
415 error = dmu_read_uio(os, zp->z_id, uio, bytes);
425 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
428 * Read bytes from specified file into supplied buffer.
430 * IN: vp - vnode of file to be read from.
431 * uio - structure supplying read location, range info,
433 * ioflag - SYNC flags; used to provide FRSYNC semantics.
434 * cr - credentials of caller.
435 * ct - caller context
437 * OUT: uio - updated offset and range, buffer filled.
439 * RETURN: 0 if success
440 * error code if failure
443 * vp - atime updated if byte count > 0
447 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
449 znode_t *zp = VTOZ(vp);
450 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
461 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
467 * Validate file offset
469 if (uio->uio_loffset < (offset_t)0) {
475 * Fasttrack empty reads
477 if (uio->uio_resid == 0) {
483 * Check for mandatory locks
485 if (MANDMODE(zp->z_mode)) {
486 if (error = chklock(vp, FREAD,
487 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
494 * If we're in FRSYNC mode, sync out this znode before reading it.
496 if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
497 zil_commit(zfsvfs->z_log, zp->z_id);
500 * Lock the range against changes.
502 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
505 * If we are reading past end-of-file we can skip
506 * to the end; but we might still need to set atime.
508 if (uio->uio_loffset >= zp->z_size) {
513 ASSERT(uio->uio_loffset < zp->z_size);
514 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
516 if ((uio->uio_extflg == UIO_XUIO) &&
517 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
519 int blksz = zp->z_blksz;
520 uint64_t offset = uio->uio_loffset;
522 xuio = (xuio_t *)uio;
524 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
527 ASSERT(offset + n <= blksz);
530 (void) dmu_xuio_init(xuio, nblk);
532 if (vn_has_cached_data(vp)) {
534 * For simplicity, we always allocate a full buffer
535 * even if we only expect to read a portion of a block.
537 while (--nblk >= 0) {
538 (void) dmu_xuio_add(xuio,
539 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
546 nbytes = MIN(n, zfs_read_chunk_size -
547 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
549 if (vn_has_cached_data(vp))
550 error = mappedread(vp, nbytes, uio);
552 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
554 /* convert checksum errors into IO errors */
563 zfs_range_unlock(rl);
565 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
571 * Write the bytes to a file.
573 * IN: vp - vnode of file to be written to.
574 * uio - structure supplying write location, range info,
576 * ioflag - FAPPEND flag set if in append mode.
577 * cr - credentials of caller.
578 * ct - caller context (NFS/CIFS fem monitor only)
580 * OUT: uio - updated offset and range.
582 * RETURN: 0 if success
583 * error code if failure
586 * vp - ctime|mtime updated if byte count > 0
591 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
593 znode_t *zp = VTOZ(vp);
594 rlim64_t limit = uio->uio_llimit;
595 ssize_t start_resid = uio->uio_resid;
599 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
604 int max_blksz = zfsvfs->z_max_blksz;
610 int iovcnt = uio->uio_iovcnt;
611 iovec_t *iovp = uio->uio_iov;
614 sa_bulk_attr_t bulk[4];
615 uint64_t mtime[2], ctime[2];
618 * Fasttrack empty write
624 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
630 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
631 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
632 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
634 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
638 * If immutable or not appending then return EPERM
640 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
641 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
642 (uio->uio_loffset < zp->z_size))) {
647 zilog = zfsvfs->z_log;
650 * Validate file offset
652 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
659 * Check for mandatory locks before calling zfs_range_lock()
660 * in order to prevent a deadlock with locks set via fcntl().
662 if (MANDMODE((mode_t)zp->z_mode) &&
663 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
669 * Pre-fault the pages to ensure slow (eg NFS) pages
671 * Skip this if uio contains loaned arc_buf.
673 if ((uio->uio_extflg == UIO_XUIO) &&
674 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
675 xuio = (xuio_t *)uio;
677 uio_prefaultpages(MIN(n, max_blksz), uio);
680 * If in append mode, set the io offset pointer to eof.
682 if (ioflag & FAPPEND) {
684 * Obtain an appending range lock to guarantee file append
685 * semantics. We reset the write offset once we have the lock.
687 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
689 if (rl->r_len == UINT64_MAX) {
691 * We overlocked the file because this write will cause
692 * the file block size to increase.
693 * Note that zp_size cannot change with this lock held.
697 uio->uio_loffset = woff;
700 * Note that if the file block size will change as a result of
701 * this write, then this range lock will lock the entire file
702 * so that we can re-write the block safely.
704 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
708 zfs_range_unlock(rl);
713 if ((woff + n) > limit || woff > (limit - n))
716 /* Will this write extend the file length? */
717 write_eof = (woff + n > zp->z_size);
719 end_size = MAX(zp->z_size, woff + n);
722 * Write the file in reasonable size chunks. Each chunk is written
723 * in a separate transaction; this keeps the intent log records small
724 * and allows us to do more fine-grained space accounting.
728 woff = uio->uio_loffset;
730 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
731 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
733 dmu_return_arcbuf(abuf);
738 if (xuio && abuf == NULL) {
739 ASSERT(i_iov < iovcnt);
741 abuf = dmu_xuio_arcbuf(xuio, i_iov);
742 dmu_xuio_clear(xuio, i_iov);
743 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
744 iovec_t *, aiov, arc_buf_t *, abuf);
745 ASSERT((aiov->iov_base == abuf->b_data) ||
746 ((char *)aiov->iov_base - (char *)abuf->b_data +
747 aiov->iov_len == arc_buf_size(abuf)));
749 } else if (abuf == NULL && n >= max_blksz &&
750 woff >= zp->z_size &&
751 P2PHASE(woff, max_blksz) == 0 &&
752 zp->z_blksz == max_blksz) {
754 * This write covers a full block. "Borrow" a buffer
755 * from the dmu so that we can fill it before we enter
756 * a transaction. This avoids the possibility of
757 * holding up the transaction if the data copy hangs
758 * up on a pagefault (e.g., from an NFS server mapping).
762 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
764 ASSERT(abuf != NULL);
765 ASSERT(arc_buf_size(abuf) == max_blksz);
766 if ((error = uiocopy(abuf->b_data, max_blksz,
767 UIO_WRITE, uio, &cbytes))) {
768 dmu_return_arcbuf(abuf);
771 ASSERT(cbytes == max_blksz);
775 * Start a transaction.
777 tx = dmu_tx_create(zfsvfs->z_os);
778 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
779 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
780 zfs_sa_upgrade_txholds(tx, zp);
781 error = dmu_tx_assign(tx, TXG_NOWAIT);
783 if (error == ERESTART) {
790 dmu_return_arcbuf(abuf);
795 * If zfs_range_lock() over-locked we grow the blocksize
796 * and then reduce the lock range. This will only happen
797 * on the first iteration since zfs_range_reduce() will
798 * shrink down r_len to the appropriate size.
800 if (rl->r_len == UINT64_MAX) {
803 if (zp->z_blksz > max_blksz) {
804 ASSERT(!ISP2(zp->z_blksz));
805 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
807 new_blksz = MIN(end_size, max_blksz);
809 zfs_grow_blocksize(zp, new_blksz, tx);
810 zfs_range_reduce(rl, woff, n);
814 * XXX - should we really limit each write to z_max_blksz?
815 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
817 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
820 tx_bytes = uio->uio_resid;
821 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
823 tx_bytes -= uio->uio_resid;
826 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
828 * If this is not a full block write, but we are
829 * extending the file past EOF and this data starts
830 * block-aligned, use assign_arcbuf(). Otherwise,
831 * write via dmu_write().
833 if (tx_bytes < max_blksz && (!write_eof ||
834 aiov->iov_base != abuf->b_data)) {
836 dmu_write(zfsvfs->z_os, zp->z_id, woff,
837 aiov->iov_len, aiov->iov_base, tx);
838 dmu_return_arcbuf(abuf);
839 xuio_stat_wbuf_copied();
841 ASSERT(xuio || tx_bytes == max_blksz);
842 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
845 ASSERT(tx_bytes <= uio->uio_resid);
846 uioskip(uio, tx_bytes);
848 if (tx_bytes && vn_has_cached_data(vp)) {
849 update_pages(vp, woff,
850 tx_bytes, zfsvfs->z_os, zp->z_id);
854 * If we made no progress, we're done. If we made even
855 * partial progress, update the znode and ZIL accordingly.
858 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
859 (void *)&zp->z_size, sizeof (uint64_t), tx);
866 * Clear Set-UID/Set-GID bits on successful write if not
867 * privileged and at least one of the excute bits is set.
869 * It would be nice to to this after all writes have
870 * been done, but that would still expose the ISUID/ISGID
871 * to another app after the partial write is committed.
873 * Note: we don't call zfs_fuid_map_id() here because
874 * user 0 is not an ephemeral uid.
876 mutex_enter(&zp->z_acl_lock);
877 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
878 (S_IXUSR >> 6))) != 0 &&
879 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
880 secpolicy_vnode_setid_retain(cr,
881 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
883 zp->z_mode &= ~(S_ISUID | S_ISGID);
884 newmode = zp->z_mode;
885 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
886 (void *)&newmode, sizeof (uint64_t), tx);
888 mutex_exit(&zp->z_acl_lock);
890 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
894 * Update the file size (zp_size) if it has changed;
895 * account for possible concurrent updates.
897 while ((end_size = zp->z_size) < uio->uio_loffset) {
898 (void) atomic_cas_64(&zp->z_size, end_size,
903 * If we are replaying and eof is non zero then force
904 * the file size to the specified eof. Note, there's no
905 * concurrency during replay.
907 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
908 zp->z_size = zfsvfs->z_replay_eof;
910 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
912 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
917 ASSERT(tx_bytes == nbytes);
921 uio_prefaultpages(MIN(n, max_blksz), uio);
924 zfs_range_unlock(rl);
927 * If we're in replay mode, or we made no progress, return error.
928 * Otherwise, it's at least a partial write, so it's successful.
930 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
935 if (ioflag & (FSYNC | FDSYNC) ||
936 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
937 zil_commit(zilog, zp->z_id);
944 zfs_get_done(zgd_t *zgd, int error)
946 znode_t *zp = zgd->zgd_private;
947 objset_t *os = zp->z_zfsvfs->z_os;
950 dmu_buf_rele(zgd->zgd_db, zgd);
952 zfs_range_unlock(zgd->zgd_rl);
955 * Release the vnode asynchronously as we currently have the
956 * txg stopped from syncing.
958 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
960 if (error == 0 && zgd->zgd_bp)
961 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
963 kmem_free(zgd, sizeof (zgd_t));
967 static int zil_fault_io = 0;
971 * Get data to generate a TX_WRITE intent log record.
974 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
976 zfsvfs_t *zfsvfs = arg;
977 objset_t *os = zfsvfs->z_os;
979 uint64_t object = lr->lr_foid;
980 uint64_t offset = lr->lr_offset;
981 uint64_t size = lr->lr_length;
982 blkptr_t *bp = &lr->lr_blkptr;
991 * Nothing to do if the file has been removed
993 if (zfs_zget(zfsvfs, object, &zp) != 0)
995 if (zp->z_unlinked) {
997 * Release the vnode asynchronously as we currently have the
998 * txg stopped from syncing.
1000 VN_RELE_ASYNC(ZTOV(zp),
1001 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1005 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1006 zgd->zgd_zilog = zfsvfs->z_log;
1007 zgd->zgd_private = zp;
1010 * Write records come in two flavors: immediate and indirect.
1011 * For small writes it's cheaper to store the data with the
1012 * log record (immediate); for large writes it's cheaper to
1013 * sync the data and get a pointer to it (indirect) so that
1014 * we don't have to write the data twice.
1016 if (buf != NULL) { /* immediate write */
1017 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1018 /* test for truncation needs to be done while range locked */
1019 if (offset >= zp->z_size) {
1022 error = dmu_read(os, object, offset, size, buf,
1023 DMU_READ_NO_PREFETCH);
1025 ASSERT(error == 0 || error == ENOENT);
1026 } else { /* indirect write */
1028 * Have to lock the whole block to ensure when it's
1029 * written out and it's checksum is being calculated
1030 * that no one can change the data. We need to re-check
1031 * blocksize after we get the lock in case it's changed!
1036 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1038 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1040 if (zp->z_blksz == size)
1043 zfs_range_unlock(zgd->zgd_rl);
1045 /* test for truncation needs to be done while range locked */
1046 if (lr->lr_offset >= zp->z_size)
1055 error = dmu_buf_hold(os, object, offset, zgd, &db,
1056 DMU_READ_NO_PREFETCH);
1062 ASSERT(db->db_offset == offset);
1063 ASSERT(db->db_size == size);
1065 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1067 ASSERT(error || lr->lr_length <= zp->z_blksz);
1070 * On success, we need to wait for the write I/O
1071 * initiated by dmu_sync() to complete before we can
1072 * release this dbuf. We will finish everything up
1073 * in the zfs_get_done() callback.
1078 if (error == EALREADY) {
1079 lr->lr_common.lrc_txtype = TX_WRITE2;
1085 zfs_get_done(zgd, error);
1092 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1093 caller_context_t *ct)
1095 znode_t *zp = VTOZ(vp);
1096 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1102 if (flag & V_ACE_MASK)
1103 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1105 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1112 * If vnode is for a device return a specfs vnode instead.
1115 specvp_check(vnode_t **vpp, cred_t *cr)
1119 if (IS_DEVVP(*vpp)) {
1122 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1133 * Lookup an entry in a directory, or an extended attribute directory.
1134 * If it exists, return a held vnode reference for it.
1136 * IN: dvp - vnode of directory to search.
1137 * nm - name of entry to lookup.
1138 * pnp - full pathname to lookup [UNUSED].
1139 * flags - LOOKUP_XATTR set if looking for an attribute.
1140 * rdir - root directory vnode [UNUSED].
1141 * cr - credentials of caller.
1142 * ct - caller context
1143 * direntflags - directory lookup flags
1144 * realpnp - returned pathname.
1146 * OUT: vpp - vnode of located entry, NULL if not found.
1148 * RETURN: 0 if success
1149 * error code if failure
1156 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1157 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1158 int *direntflags, pathname_t *realpnp)
1160 znode_t *zdp = VTOZ(dvp);
1161 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1165 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1167 if (dvp->v_type != VDIR) {
1169 } else if (zdp->z_sa_hdl == NULL) {
1173 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1174 error = zfs_fastaccesschk_execute(zdp, cr);
1182 vnode_t *tvp = dnlc_lookup(dvp, nm);
1185 error = zfs_fastaccesschk_execute(zdp, cr);
1190 if (tvp == DNLC_NO_VNODE) {
1195 return (specvp_check(vpp, cr));
1201 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1208 if (flags & LOOKUP_XATTR) {
1210 * If the xattr property is off, refuse the lookup request.
1212 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1218 * We don't allow recursive attributes..
1219 * Maybe someday we will.
1221 if (zdp->z_pflags & ZFS_XATTR) {
1226 if ((error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags))) {
1232 * Do we have permission to get into attribute directory?
1235 if ((error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1245 if (dvp->v_type != VDIR) {
1251 * Check accessibility of directory.
1254 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1259 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1260 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1265 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1267 error = specvp_check(vpp, cr);
1274 * Attempt to create a new entry in a directory. If the entry
1275 * already exists, truncate the file if permissible, else return
1276 * an error. Return the vp of the created or trunc'd file.
1278 * IN: dvp - vnode of directory to put new file entry in.
1279 * name - name of new file entry.
1280 * vap - attributes of new file.
1281 * excl - flag indicating exclusive or non-exclusive mode.
1282 * mode - mode to open file with.
1283 * cr - credentials of caller.
1284 * flag - large file flag [UNUSED].
1285 * ct - caller context
1286 * vsecp - ACL to be set
1288 * OUT: vpp - vnode of created or trunc'd entry.
1290 * RETURN: 0 if success
1291 * error code if failure
1294 * dvp - ctime|mtime updated if new entry created
1295 * vp - ctime|mtime always, atime if new
1300 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1301 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1304 znode_t *zp, *dzp = VTOZ(dvp);
1305 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1314 zfs_acl_ids_t acl_ids;
1315 boolean_t fuid_dirtied;
1316 boolean_t have_acl = B_FALSE;
1319 * If we have an ephemeral id, ACL, or XVATTR then
1320 * make sure file system is at proper version
1324 ksid = crgetsid(cr, KSID_OWNER);
1326 uid = ksid_getid(ksid);
1330 if (zfsvfs->z_use_fuids == B_FALSE &&
1331 (vsecp || (vap->va_mask & AT_XVATTR) ||
1332 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1338 zilog = zfsvfs->z_log;
1340 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1341 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1346 if (vap->va_mask & AT_XVATTR) {
1347 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1348 crgetuid(cr), cr, vap->va_type)) != 0) {
1356 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1357 vap->va_mode &= ~VSVTX;
1359 if (*name == '\0') {
1361 * Null component name refers to the directory itself.
1368 /* possible VN_HOLD(zp) */
1371 if (flag & FIGNORECASE)
1374 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1378 zfs_acl_ids_free(&acl_ids);
1379 if (strcmp(name, "..") == 0)
1390 * Create a new file object and update the directory
1393 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1395 zfs_acl_ids_free(&acl_ids);
1400 * We only support the creation of regular files in
1401 * extended attribute directories.
1404 if ((dzp->z_pflags & ZFS_XATTR) &&
1405 (vap->va_type != VREG)) {
1407 zfs_acl_ids_free(&acl_ids);
1412 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1413 cr, vsecp, &acl_ids)) != 0)
1417 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1418 zfs_acl_ids_free(&acl_ids);
1423 tx = dmu_tx_create(os);
1425 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1426 ZFS_SA_BASE_ATTR_SIZE);
1428 fuid_dirtied = zfsvfs->z_fuid_dirty;
1430 zfs_fuid_txhold(zfsvfs, tx);
1431 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1432 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1433 if (!zfsvfs->z_use_sa &&
1434 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1435 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1436 0, acl_ids.z_aclp->z_acl_bytes);
1438 error = dmu_tx_assign(tx, TXG_NOWAIT);
1440 zfs_dirent_unlock(dl);
1441 if (error == ERESTART) {
1446 zfs_acl_ids_free(&acl_ids);
1451 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1454 zfs_fuid_sync(zfsvfs, tx);
1456 (void) zfs_link_create(dl, zp, tx, ZNEW);
1457 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1458 if (flag & FIGNORECASE)
1460 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1461 vsecp, acl_ids.z_fuidp, vap);
1462 zfs_acl_ids_free(&acl_ids);
1465 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1468 zfs_acl_ids_free(&acl_ids);
1472 * A directory entry already exists for this name.
1475 * Can't truncate an existing file if in exclusive mode.
1482 * Can't open a directory for writing.
1484 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1489 * Verify requested access to file.
1491 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1495 mutex_enter(&dzp->z_lock);
1497 mutex_exit(&dzp->z_lock);
1500 * Truncate regular files if requested.
1502 if ((ZTOV(zp)->v_type == VREG) &&
1503 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1504 /* we can't hold any locks when calling zfs_freesp() */
1505 zfs_dirent_unlock(dl);
1507 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1509 vnevent_create(ZTOV(zp), ct);
1516 zfs_dirent_unlock(dl);
1523 error = specvp_check(vpp, cr);
1526 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1527 zil_commit(zilog, 0);
1534 * Remove an entry from a directory.
1536 * IN: dvp - vnode of directory to remove entry from.
1537 * name - name of entry to remove.
1538 * cr - credentials of caller.
1539 * ct - caller context
1540 * flags - case flags
1542 * RETURN: 0 if success
1543 * error code if failure
1547 * vp - ctime (if nlink > 0)
1550 uint64_t null_xattr = 0;
1554 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1557 znode_t *zp, *dzp = VTOZ(dvp);
1560 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1562 uint64_t acl_obj, xattr_obj;
1563 uint64_t xattr_obj_unlinked = 0;
1567 boolean_t may_delete_now, delete_now = FALSE;
1568 boolean_t unlinked, toobig = FALSE;
1570 pathname_t *realnmp = NULL;
1577 zilog = zfsvfs->z_log;
1579 if (flags & FIGNORECASE) {
1589 * Attempt to lock directory; fail if entry doesn't exist.
1591 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1601 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1606 * Need to use rmdir for removing directories.
1608 if (vp->v_type == VDIR) {
1613 vnevent_remove(vp, dvp, name, ct);
1616 dnlc_remove(dvp, realnmp->pn_buf);
1618 dnlc_remove(dvp, name);
1620 mutex_enter(&vp->v_lock);
1621 may_delete_now = ((vp->v_count == 1) && (!vn_has_cached_data(vp)));
1622 mutex_exit(&vp->v_lock);
1625 * We may delete the znode now, or we may put it in the unlinked set;
1626 * it depends on whether we're the last link, and on whether there are
1627 * other holds on the vnode. So we dmu_tx_hold() the right things to
1628 * allow for either case.
1631 tx = dmu_tx_create(zfsvfs->z_os);
1632 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1633 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1634 zfs_sa_upgrade_txholds(tx, zp);
1635 zfs_sa_upgrade_txholds(tx, dzp);
1636 if (may_delete_now) {
1638 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1639 /* if the file is too big, only hold_free a token amount */
1640 dmu_tx_hold_free(tx, zp->z_id, 0,
1641 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1644 /* are there any extended attributes? */
1645 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1646 &xattr_obj, sizeof (xattr_obj));
1647 if (error == 0 && xattr_obj) {
1648 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1649 ASSERT3U(error, ==, 0);
1650 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1651 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1654 mutex_enter(&zp->z_lock);
1655 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1656 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1657 mutex_exit(&zp->z_lock);
1659 /* charge as an update -- would be nice not to charge at all */
1660 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1662 error = dmu_tx_assign(tx, TXG_NOWAIT);
1664 zfs_dirent_unlock(dl);
1668 if (error == ERESTART) {
1681 * Remove the directory entry.
1683 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1693 * Hold z_lock so that we can make sure that the ACL obj
1694 * hasn't changed. Could have been deleted due to
1697 mutex_enter(&zp->z_lock);
1698 mutex_enter(&vp->v_lock);
1699 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1700 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1701 delete_now = may_delete_now && !toobig &&
1702 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1703 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1705 mutex_exit(&vp->v_lock);
1709 if (xattr_obj_unlinked) {
1710 ASSERT3U(xzp->z_links, ==, 2);
1711 mutex_enter(&xzp->z_lock);
1712 xzp->z_unlinked = 1;
1714 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1715 &xzp->z_links, sizeof (xzp->z_links), tx);
1716 ASSERT3U(error, ==, 0);
1717 mutex_exit(&xzp->z_lock);
1718 zfs_unlinked_add(xzp, tx);
1721 error = sa_remove(zp->z_sa_hdl,
1722 SA_ZPL_XATTR(zfsvfs), tx);
1724 error = sa_update(zp->z_sa_hdl,
1725 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1726 sizeof (uint64_t), tx);
1727 ASSERT3U(error, ==, 0);
1729 mutex_enter(&vp->v_lock);
1731 ASSERT3U(vp->v_count, ==, 0);
1732 mutex_exit(&vp->v_lock);
1733 mutex_exit(&zp->z_lock);
1734 zfs_znode_delete(zp, tx);
1735 } else if (unlinked) {
1736 mutex_exit(&zp->z_lock);
1737 zfs_unlinked_add(zp, tx);
1741 if (flags & FIGNORECASE)
1743 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1750 zfs_dirent_unlock(dl);
1757 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1758 zil_commit(zilog, 0);
1765 * Create a new directory and insert it into dvp using the name
1766 * provided. Return a pointer to the inserted directory.
1768 * IN: dvp - vnode of directory to add subdir to.
1769 * dirname - name of new directory.
1770 * vap - attributes of new directory.
1771 * cr - credentials of caller.
1772 * ct - caller context
1773 * vsecp - ACL to be set
1775 * OUT: vpp - vnode of created directory.
1777 * RETURN: 0 if success
1778 * error code if failure
1781 * dvp - ctime|mtime updated
1782 * vp - ctime|mtime|atime updated
1786 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1787 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1789 znode_t *zp, *dzp = VTOZ(dvp);
1790 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1799 gid_t gid = crgetgid(cr);
1800 zfs_acl_ids_t acl_ids;
1801 boolean_t fuid_dirtied;
1803 ASSERT(vap->va_type == VDIR);
1806 * If we have an ephemeral id, ACL, or XVATTR then
1807 * make sure file system is at proper version
1810 ksid = crgetsid(cr, KSID_OWNER);
1812 uid = ksid_getid(ksid);
1815 if (zfsvfs->z_use_fuids == B_FALSE &&
1816 (vsecp || (vap->va_mask & AT_XVATTR) ||
1817 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1822 zilog = zfsvfs->z_log;
1824 if (dzp->z_pflags & ZFS_XATTR) {
1829 if (zfsvfs->z_utf8 && u8_validate(dirname,
1830 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1834 if (flags & FIGNORECASE)
1837 if (vap->va_mask & AT_XVATTR) {
1838 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1839 crgetuid(cr), cr, vap->va_type)) != 0) {
1845 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1846 vsecp, &acl_ids)) != 0) {
1851 * First make sure the new directory doesn't exist.
1853 * Existence is checked first to make sure we don't return
1854 * EACCES instead of EEXIST which can cause some applications
1860 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1862 zfs_acl_ids_free(&acl_ids);
1867 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1868 zfs_acl_ids_free(&acl_ids);
1869 zfs_dirent_unlock(dl);
1874 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1875 zfs_acl_ids_free(&acl_ids);
1876 zfs_dirent_unlock(dl);
1882 * Add a new entry to the directory.
1884 tx = dmu_tx_create(zfsvfs->z_os);
1885 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1886 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1887 fuid_dirtied = zfsvfs->z_fuid_dirty;
1889 zfs_fuid_txhold(zfsvfs, tx);
1890 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1891 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1892 acl_ids.z_aclp->z_acl_bytes);
1895 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1896 ZFS_SA_BASE_ATTR_SIZE);
1898 error = dmu_tx_assign(tx, TXG_NOWAIT);
1900 zfs_dirent_unlock(dl);
1901 if (error == ERESTART) {
1906 zfs_acl_ids_free(&acl_ids);
1915 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1918 zfs_fuid_sync(zfsvfs, tx);
1921 * Now put new name in parent dir.
1923 (void) zfs_link_create(dl, zp, tx, ZNEW);
1927 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1928 if (flags & FIGNORECASE)
1930 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1931 acl_ids.z_fuidp, vap);
1933 zfs_acl_ids_free(&acl_ids);
1937 zfs_dirent_unlock(dl);
1939 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1940 zil_commit(zilog, 0);
1947 * Remove a directory subdir entry. If the current working
1948 * directory is the same as the subdir to be removed, the
1951 * IN: dvp - vnode of directory to remove from.
1952 * name - name of directory to be removed.
1953 * cwd - vnode of current working directory.
1954 * cr - credentials of caller.
1955 * ct - caller context
1956 * flags - case flags
1958 * RETURN: 0 if success
1959 * error code if failure
1962 * dvp - ctime|mtime updated
1966 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
1967 caller_context_t *ct, int flags)
1969 znode_t *dzp = VTOZ(dvp);
1972 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1981 zilog = zfsvfs->z_log;
1983 if (flags & FIGNORECASE)
1989 * Attempt to lock directory; fail if entry doesn't exist.
1991 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1999 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
2003 if (vp->v_type != VDIR) {
2013 vnevent_rmdir(vp, dvp, name, ct);
2016 * Grab a lock on the directory to make sure that noone is
2017 * trying to add (or lookup) entries while we are removing it.
2019 rw_enter(&zp->z_name_lock, RW_WRITER);
2022 * Grab a lock on the parent pointer to make sure we play well
2023 * with the treewalk and directory rename code.
2025 rw_enter(&zp->z_parent_lock, RW_WRITER);
2027 tx = dmu_tx_create(zfsvfs->z_os);
2028 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2029 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2030 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2031 zfs_sa_upgrade_txholds(tx, zp);
2032 zfs_sa_upgrade_txholds(tx, dzp);
2033 error = dmu_tx_assign(tx, TXG_NOWAIT);
2035 rw_exit(&zp->z_parent_lock);
2036 rw_exit(&zp->z_name_lock);
2037 zfs_dirent_unlock(dl);
2039 if (error == ERESTART) {
2049 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2052 uint64_t txtype = TX_RMDIR;
2053 if (flags & FIGNORECASE)
2055 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2060 rw_exit(&zp->z_parent_lock);
2061 rw_exit(&zp->z_name_lock);
2063 zfs_dirent_unlock(dl);
2067 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2068 zil_commit(zilog, 0);
2075 * Read as many directory entries as will fit into the provided
2076 * buffer from the given directory cursor position (specified in
2077 * the uio structure.
2079 * IN: vp - vnode of directory to read.
2080 * uio - structure supplying read location, range info,
2081 * and return buffer.
2082 * cr - credentials of caller.
2083 * ct - caller context
2084 * flags - case flags
2086 * OUT: uio - updated offset and range, buffer filled.
2087 * eofp - set to true if end-of-file detected.
2089 * RETURN: 0 if success
2090 * error code if failure
2093 * vp - atime updated
2095 * Note that the low 4 bits of the cookie returned by zap is always zero.
2096 * This allows us to use the low range for "special" directory entries:
2097 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2098 * we use the offset 2 for the '.zfs' directory.
2102 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2103 caller_context_t *ct, int flags)
2105 znode_t *zp = VTOZ(vp);
2109 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2114 zap_attribute_t zap;
2115 uint_t bytes_wanted;
2116 uint64_t offset; /* must be unsigned; checks for < 1 */
2122 boolean_t check_sysattrs;
2127 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2128 &parent, sizeof (parent))) != 0) {
2134 * If we are not given an eof variable,
2141 * Check for valid iov_len.
2143 if (uio->uio_iov->iov_len <= 0) {
2149 * Quit if directory has been removed (posix)
2151 if ((*eofp = zp->z_unlinked) != 0) {
2158 offset = uio->uio_loffset;
2159 prefetch = zp->z_zn_prefetch;
2162 * Initialize the iterator cursor.
2166 * Start iteration from the beginning of the directory.
2168 zap_cursor_init(&zc, os, zp->z_id);
2171 * The offset is a serialized cursor.
2173 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2177 * Get space to change directory entries into fs independent format.
2179 iovp = uio->uio_iov;
2180 bytes_wanted = iovp->iov_len;
2181 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2182 bufsize = bytes_wanted;
2183 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2184 odp = (struct dirent64 *)outbuf;
2186 bufsize = bytes_wanted;
2187 odp = (struct dirent64 *)iovp->iov_base;
2189 eodp = (struct edirent *)odp;
2192 * If this VFS supports the system attribute view interface; and
2193 * we're looking at an extended attribute directory; and we care
2194 * about normalization conflicts on this vfs; then we must check
2195 * for normalization conflicts with the sysattr name space.
2197 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2198 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2199 (flags & V_RDDIR_ENTFLAGS);
2202 * Transform to file-system independent format
2205 while (outcount < bytes_wanted) {
2208 off64_t *next = NULL;
2211 * Special case `.', `..', and `.zfs'.
2214 (void) strcpy(zap.za_name, ".");
2215 zap.za_normalization_conflict = 0;
2217 } else if (offset == 1) {
2218 (void) strcpy(zap.za_name, "..");
2219 zap.za_normalization_conflict = 0;
2221 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2222 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2223 zap.za_normalization_conflict = 0;
2224 objnum = ZFSCTL_INO_ROOT;
2229 if (error = zap_cursor_retrieve(&zc, &zap)) {
2230 if ((*eofp = (error == ENOENT)) != 0)
2236 if (zap.za_integer_length != 8 ||
2237 zap.za_num_integers != 1) {
2238 cmn_err(CE_WARN, "zap_readdir: bad directory "
2239 "entry, obj = %lld, offset = %lld\n",
2240 (u_longlong_t)zp->z_id,
2241 (u_longlong_t)offset);
2246 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2248 * MacOS X can extract the object type here such as:
2249 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2252 if (check_sysattrs && !zap.za_normalization_conflict) {
2253 zap.za_normalization_conflict =
2254 xattr_sysattr_casechk(zap.za_name);
2258 if (flags & V_RDDIR_ACCFILTER) {
2260 * If we have no access at all, don't include
2261 * this entry in the returned information
2264 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2266 if (!zfs_has_access(ezp, cr)) {
2273 if (flags & V_RDDIR_ENTFLAGS)
2274 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2276 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2279 * Will this entry fit in the buffer?
2281 if (outcount + reclen > bufsize) {
2283 * Did we manage to fit anything in the buffer?
2291 if (flags & V_RDDIR_ENTFLAGS) {
2293 * Add extended flag entry:
2295 eodp->ed_ino = objnum;
2296 eodp->ed_reclen = reclen;
2297 /* NOTE: ed_off is the offset for the *next* entry */
2298 next = &(eodp->ed_off);
2299 eodp->ed_eflags = zap.za_normalization_conflict ?
2300 ED_CASE_CONFLICT : 0;
2301 (void) strncpy(eodp->ed_name, zap.za_name,
2302 EDIRENT_NAMELEN(reclen));
2303 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2308 odp->d_ino = objnum;
2309 odp->d_reclen = reclen;
2310 /* NOTE: d_off is the offset for the *next* entry */
2311 next = &(odp->d_off);
2312 (void) strncpy(odp->d_name, zap.za_name,
2313 DIRENT64_NAMELEN(reclen));
2314 odp = (dirent64_t *)((intptr_t)odp + reclen);
2318 ASSERT(outcount <= bufsize);
2320 /* Prefetch znode */
2322 dmu_prefetch(os, objnum, 0, 0);
2326 * Move to the next entry, fill in the previous offset.
2328 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2329 zap_cursor_advance(&zc);
2330 offset = zap_cursor_serialize(&zc);
2337 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2339 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2340 iovp->iov_base += outcount;
2341 iovp->iov_len -= outcount;
2342 uio->uio_resid -= outcount;
2343 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2345 * Reset the pointer.
2347 offset = uio->uio_loffset;
2351 zap_cursor_fini(&zc);
2352 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2353 kmem_free(outbuf, bufsize);
2355 if (error == ENOENT)
2358 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2360 uio->uio_loffset = offset;
2365 ulong_t zfs_fsync_sync_cnt = 4;
2368 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2370 znode_t *zp = VTOZ(vp);
2371 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2374 * Regardless of whether this is required for standards conformance,
2375 * this is the logical behavior when fsync() is called on a file with
2376 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2377 * going to be pushed out as part of the zil_commit().
2379 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2380 (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2381 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2383 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2385 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2388 zil_commit(zfsvfs->z_log, zp->z_id);
2396 * Get the requested file attributes and place them in the provided
2399 * IN: vp - vnode of file.
2400 * vap - va_mask identifies requested attributes.
2401 * If AT_XVATTR set, then optional attrs are requested
2402 * flags - ATTR_NOACLCHECK (CIFS server context)
2403 * cr - credentials of caller.
2404 * ct - caller context
2406 * OUT: vap - attribute values.
2408 * RETURN: 0 (always succeeds)
2412 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2413 caller_context_t *ct)
2415 znode_t *zp = VTOZ(vp);
2416 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2419 uint64_t mtime[2], ctime[2];
2420 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2421 xoptattr_t *xoap = NULL;
2422 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2423 sa_bulk_attr_t bulk[2];
2429 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2431 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2432 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2434 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2440 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2441 * Also, if we are the owner don't bother, since owner should
2442 * always be allowed to read basic attributes of file.
2444 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2445 (vap->va_uid != crgetuid(cr))) {
2446 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2454 * Return all attributes. It's cheaper to provide the answer
2455 * than to determine whether we were asked the question.
2458 mutex_enter(&zp->z_lock);
2459 vap->va_type = vp->v_type;
2460 vap->va_mode = zp->z_mode & MODEMASK;
2461 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2462 vap->va_nodeid = zp->z_id;
2463 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2464 links = zp->z_links + 1;
2466 links = zp->z_links;
2467 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2468 vap->va_size = zp->z_size;
2469 vap->va_rdev = vp->v_rdev;
2470 vap->va_seq = zp->z_seq;
2473 * Add in any requested optional attributes and the create time.
2474 * Also set the corresponding bits in the returned attribute bitmap.
2476 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2477 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2479 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2480 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2483 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2484 xoap->xoa_readonly =
2485 ((zp->z_pflags & ZFS_READONLY) != 0);
2486 XVA_SET_RTN(xvap, XAT_READONLY);
2489 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2491 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2492 XVA_SET_RTN(xvap, XAT_SYSTEM);
2495 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2497 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2498 XVA_SET_RTN(xvap, XAT_HIDDEN);
2501 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2502 xoap->xoa_nounlink =
2503 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2504 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2507 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2508 xoap->xoa_immutable =
2509 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2510 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2513 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2514 xoap->xoa_appendonly =
2515 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2516 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2519 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2521 ((zp->z_pflags & ZFS_NODUMP) != 0);
2522 XVA_SET_RTN(xvap, XAT_NODUMP);
2525 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2527 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2528 XVA_SET_RTN(xvap, XAT_OPAQUE);
2531 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2532 xoap->xoa_av_quarantined =
2533 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2534 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2537 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2538 xoap->xoa_av_modified =
2539 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2540 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2543 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2544 vp->v_type == VREG) {
2545 zfs_sa_get_scanstamp(zp, xvap);
2548 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2551 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2552 times, sizeof (times));
2553 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2554 XVA_SET_RTN(xvap, XAT_CREATETIME);
2557 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2558 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2559 XVA_SET_RTN(xvap, XAT_REPARSE);
2561 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2562 xoap->xoa_generation = zp->z_gen;
2563 XVA_SET_RTN(xvap, XAT_GEN);
2566 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2568 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2569 XVA_SET_RTN(xvap, XAT_OFFLINE);
2572 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2574 ((zp->z_pflags & ZFS_SPARSE) != 0);
2575 XVA_SET_RTN(xvap, XAT_SPARSE);
2579 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2580 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2581 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2583 mutex_exit(&zp->z_lock);
2585 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2587 if (zp->z_blksz == 0) {
2589 * Block size hasn't been set; suggest maximal I/O transfers.
2591 vap->va_blksize = zfsvfs->z_max_blksz;
2599 * Set the file attributes to the values contained in the
2602 * IN: vp - vnode of file to be modified.
2603 * vap - new attribute values.
2604 * If AT_XVATTR set, then optional attrs are being set
2605 * flags - ATTR_UTIME set if non-default time values provided.
2606 * - ATTR_NOACLCHECK (CIFS context only).
2607 * cr - credentials of caller.
2608 * ct - caller context
2610 * RETURN: 0 if success
2611 * error code if failure
2614 * vp - ctime updated, mtime updated if size changed.
2618 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2619 caller_context_t *ct)
2621 znode_t *zp = VTOZ(vp);
2622 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2627 uint_t mask = vap->va_mask;
2631 uint64_t new_uid, new_gid;
2633 uint64_t mtime[2], ctime[2];
2635 int need_policy = FALSE;
2637 zfs_fuid_info_t *fuidp = NULL;
2638 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2641 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2642 boolean_t fuid_dirtied = B_FALSE;
2643 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2644 int count = 0, xattr_count = 0;
2649 if (mask & AT_NOSET)
2655 zilog = zfsvfs->z_log;
2658 * Make sure that if we have ephemeral uid/gid or xvattr specified
2659 * that file system is at proper version level
2662 if (zfsvfs->z_use_fuids == B_FALSE &&
2663 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2664 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2665 (mask & AT_XVATTR))) {
2670 if (mask & AT_SIZE && vp->v_type == VDIR) {
2675 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2681 * If this is an xvattr_t, then get a pointer to the structure of
2682 * optional attributes. If this is NULL, then we have a vattr_t.
2684 xoap = xva_getxoptattr(xvap);
2686 xva_init(&tmpxvattr);
2689 * Immutable files can only alter immutable bit and atime
2691 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2692 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2693 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2698 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2704 * Verify timestamps doesn't overflow 32 bits.
2705 * ZFS can handle large timestamps, but 32bit syscalls can't
2706 * handle times greater than 2039. This check should be removed
2707 * once large timestamps are fully supported.
2709 if (mask & (AT_ATIME | AT_MTIME)) {
2710 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2711 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2721 /* Can this be moved to before the top label? */
2722 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2728 * First validate permissions
2731 if (mask & AT_SIZE) {
2732 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2738 * XXX - Note, we are not providing any open
2739 * mode flags here (like FNDELAY), so we may
2740 * block if there are locks present... this
2741 * should be addressed in openat().
2743 /* XXX - would it be OK to generate a log record here? */
2744 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2751 if (mask & (AT_ATIME|AT_MTIME) ||
2752 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2753 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2754 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2755 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2756 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2757 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2758 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2759 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2763 if (mask & (AT_UID|AT_GID)) {
2764 int idmask = (mask & (AT_UID|AT_GID));
2769 * NOTE: even if a new mode is being set,
2770 * we may clear S_ISUID/S_ISGID bits.
2773 if (!(mask & AT_MODE))
2774 vap->va_mode = zp->z_mode;
2777 * Take ownership or chgrp to group we are a member of
2780 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2781 take_group = (mask & AT_GID) &&
2782 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2785 * If both AT_UID and AT_GID are set then take_owner and
2786 * take_group must both be set in order to allow taking
2789 * Otherwise, send the check through secpolicy_vnode_setattr()
2793 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2794 ((idmask == AT_UID) && take_owner) ||
2795 ((idmask == AT_GID) && take_group)) {
2796 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2797 skipaclchk, cr) == 0) {
2799 * Remove setuid/setgid for non-privileged users
2801 secpolicy_setid_clear(vap, cr);
2802 trim_mask = (mask & (AT_UID|AT_GID));
2811 mutex_enter(&zp->z_lock);
2812 oldva.va_mode = zp->z_mode;
2813 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2814 if (mask & AT_XVATTR) {
2816 * Update xvattr mask to include only those attributes
2817 * that are actually changing.
2819 * the bits will be restored prior to actually setting
2820 * the attributes so the caller thinks they were set.
2822 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2823 if (xoap->xoa_appendonly !=
2824 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2827 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2828 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2832 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2833 if (xoap->xoa_nounlink !=
2834 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2837 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2838 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2842 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2843 if (xoap->xoa_immutable !=
2844 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2847 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2848 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2852 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2853 if (xoap->xoa_nodump !=
2854 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2857 XVA_CLR_REQ(xvap, XAT_NODUMP);
2858 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2862 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2863 if (xoap->xoa_av_modified !=
2864 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2867 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2868 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2872 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2873 if ((vp->v_type != VREG &&
2874 xoap->xoa_av_quarantined) ||
2875 xoap->xoa_av_quarantined !=
2876 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2879 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2880 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2884 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2885 mutex_exit(&zp->z_lock);
2890 if (need_policy == FALSE &&
2891 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2892 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2897 mutex_exit(&zp->z_lock);
2899 if (mask & AT_MODE) {
2900 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2901 err = secpolicy_setid_setsticky_clear(vp, vap,
2907 trim_mask |= AT_MODE;
2915 * If trim_mask is set then take ownership
2916 * has been granted or write_acl is present and user
2917 * has the ability to modify mode. In that case remove
2918 * UID|GID and or MODE from mask so that
2919 * secpolicy_vnode_setattr() doesn't revoke it.
2923 saved_mask = vap->va_mask;
2924 vap->va_mask &= ~trim_mask;
2926 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2927 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2934 vap->va_mask |= saved_mask;
2938 * secpolicy_vnode_setattr, or take ownership may have
2941 mask = vap->va_mask;
2943 if ((mask & (AT_UID | AT_GID))) {
2944 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2945 &xattr_obj, sizeof (xattr_obj));
2947 if (err == 0 && xattr_obj) {
2948 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
2952 if (mask & AT_UID) {
2953 new_uid = zfs_fuid_create(zfsvfs,
2954 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2955 if (new_uid != zp->z_uid &&
2956 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
2958 VN_RELE(ZTOV(attrzp));
2964 if (mask & AT_GID) {
2965 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2966 cr, ZFS_GROUP, &fuidp);
2967 if (new_gid != zp->z_gid &&
2968 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
2970 VN_RELE(ZTOV(attrzp));
2976 tx = dmu_tx_create(zfsvfs->z_os);
2978 if (mask & AT_MODE) {
2979 uint64_t pmode = zp->z_mode;
2981 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2983 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2985 mutex_enter(&zp->z_lock);
2986 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2988 * Are we upgrading ACL from old V0 format
2991 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
2992 zfs_znode_acl_version(zp) ==
2993 ZFS_ACL_VERSION_INITIAL) {
2994 dmu_tx_hold_free(tx, acl_obj, 0,
2996 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2997 0, aclp->z_acl_bytes);
2999 dmu_tx_hold_write(tx, acl_obj, 0,
3002 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3003 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3004 0, aclp->z_acl_bytes);
3006 mutex_exit(&zp->z_lock);
3007 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3009 if ((mask & AT_XVATTR) &&
3010 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3011 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3013 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3017 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3020 fuid_dirtied = zfsvfs->z_fuid_dirty;
3022 zfs_fuid_txhold(zfsvfs, tx);
3024 zfs_sa_upgrade_txholds(tx, zp);
3026 err = dmu_tx_assign(tx, TXG_NOWAIT);
3028 if (err == ERESTART)
3035 * Set each attribute requested.
3036 * We group settings according to the locks they need to acquire.
3038 * Note: you cannot set ctime directly, although it will be
3039 * updated as a side-effect of calling this function.
3043 if (mask & (AT_UID|AT_GID|AT_MODE))
3044 mutex_enter(&zp->z_acl_lock);
3045 mutex_enter(&zp->z_lock);
3047 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3048 &zp->z_pflags, sizeof (zp->z_pflags));
3051 if (mask & (AT_UID|AT_GID|AT_MODE))
3052 mutex_enter(&attrzp->z_acl_lock);
3053 mutex_enter(&attrzp->z_lock);
3054 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3055 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3056 sizeof (attrzp->z_pflags));
3059 if (mask & (AT_UID|AT_GID)) {
3061 if (mask & AT_UID) {
3062 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3063 &new_uid, sizeof (new_uid));
3064 zp->z_uid = new_uid;
3066 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3067 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3069 attrzp->z_uid = new_uid;
3073 if (mask & AT_GID) {
3074 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3075 NULL, &new_gid, sizeof (new_gid));
3076 zp->z_gid = new_gid;
3078 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3079 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3081 attrzp->z_gid = new_gid;
3084 if (!(mask & AT_MODE)) {
3085 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3086 NULL, &new_mode, sizeof (new_mode));
3087 new_mode = zp->z_mode;
3089 err = zfs_acl_chown_setattr(zp);
3092 err = zfs_acl_chown_setattr(attrzp);
3097 if (mask & AT_MODE) {
3098 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3099 &new_mode, sizeof (new_mode));
3100 zp->z_mode = new_mode;
3101 ASSERT3U((uintptr_t)aclp, !=, NULL);
3102 err = zfs_aclset_common(zp, aclp, cr, tx);
3103 ASSERT3U(err, ==, 0);
3104 if (zp->z_acl_cached)
3105 zfs_acl_free(zp->z_acl_cached);
3106 zp->z_acl_cached = aclp;
3111 if (mask & AT_ATIME) {
3112 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3113 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3114 &zp->z_atime, sizeof (zp->z_atime));
3117 if (mask & AT_MTIME) {
3118 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3119 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3120 mtime, sizeof (mtime));
3123 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3124 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3125 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3126 NULL, mtime, sizeof (mtime));
3127 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3128 &ctime, sizeof (ctime));
3129 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3131 } else if (mask != 0) {
3132 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3133 &ctime, sizeof (ctime));
3134 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3137 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3138 SA_ZPL_CTIME(zfsvfs), NULL,
3139 &ctime, sizeof (ctime));
3140 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3141 mtime, ctime, B_TRUE);
3145 * Do this after setting timestamps to prevent timestamp
3146 * update from toggling bit
3149 if (xoap && (mask & AT_XVATTR)) {
3152 * restore trimmed off masks
3153 * so that return masks can be set for caller.
3156 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3157 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3159 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3160 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3162 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3163 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3165 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3166 XVA_SET_REQ(xvap, XAT_NODUMP);
3168 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3169 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3171 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3172 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3175 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3176 ASSERT(vp->v_type == VREG);
3178 zfs_xvattr_set(zp, xvap, tx);
3182 zfs_fuid_sync(zfsvfs, tx);
3185 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3187 mutex_exit(&zp->z_lock);
3188 if (mask & (AT_UID|AT_GID|AT_MODE))
3189 mutex_exit(&zp->z_acl_lock);
3192 if (mask & (AT_UID|AT_GID|AT_MODE))
3193 mutex_exit(&attrzp->z_acl_lock);
3194 mutex_exit(&attrzp->z_lock);
3197 if (err == 0 && attrzp) {
3198 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3204 VN_RELE(ZTOV(attrzp));
3209 zfs_fuid_info_free(fuidp);
3215 if (err == ERESTART)
3218 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3223 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3224 zil_commit(zilog, 0);
3230 typedef struct zfs_zlock {
3231 krwlock_t *zl_rwlock; /* lock we acquired */
3232 znode_t *zl_znode; /* znode we held */
3233 struct zfs_zlock *zl_next; /* next in list */
3237 * Drop locks and release vnodes that were held by zfs_rename_lock().
3240 zfs_rename_unlock(zfs_zlock_t **zlpp)
3244 while ((zl = *zlpp) != NULL) {
3245 if (zl->zl_znode != NULL)
3246 VN_RELE(ZTOV(zl->zl_znode));
3247 rw_exit(zl->zl_rwlock);
3248 *zlpp = zl->zl_next;
3249 kmem_free(zl, sizeof (*zl));
3254 * Search back through the directory tree, using the ".." entries.
3255 * Lock each directory in the chain to prevent concurrent renames.
3256 * Fail any attempt to move a directory into one of its own descendants.
3257 * XXX - z_parent_lock can overlap with map or grow locks
3260 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3264 uint64_t rootid = zp->z_zfsvfs->z_root;
3265 uint64_t oidp = zp->z_id;
3266 krwlock_t *rwlp = &szp->z_parent_lock;
3267 krw_t rw = RW_WRITER;
3270 * First pass write-locks szp and compares to zp->z_id.
3271 * Later passes read-lock zp and compare to zp->z_parent.
3274 if (!rw_tryenter(rwlp, rw)) {
3276 * Another thread is renaming in this path.
3277 * Note that if we are a WRITER, we don't have any
3278 * parent_locks held yet.
3280 if (rw == RW_READER && zp->z_id > szp->z_id) {
3282 * Drop our locks and restart
3284 zfs_rename_unlock(&zl);
3288 rwlp = &szp->z_parent_lock;
3293 * Wait for other thread to drop its locks
3299 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3300 zl->zl_rwlock = rwlp;
3301 zl->zl_znode = NULL;
3302 zl->zl_next = *zlpp;
3305 if (oidp == szp->z_id) /* We're a descendant of szp */
3308 if (oidp == rootid) /* We've hit the top */
3311 if (rw == RW_READER) { /* i.e. not the first pass */
3312 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3317 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3318 &oidp, sizeof (oidp));
3319 rwlp = &zp->z_parent_lock;
3322 } while (zp->z_id != sdzp->z_id);
3328 * Move an entry from the provided source directory to the target
3329 * directory. Change the entry name as indicated.
3331 * IN: sdvp - Source directory containing the "old entry".
3332 * snm - Old entry name.
3333 * tdvp - Target directory to contain the "new entry".
3334 * tnm - New entry name.
3335 * cr - credentials of caller.
3336 * ct - caller context
3337 * flags - case flags
3339 * RETURN: 0 if success
3340 * error code if failure
3343 * sdvp,tdvp - ctime|mtime updated
3347 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3348 caller_context_t *ct, int flags)
3350 znode_t *tdzp, *szp, *tzp;
3351 znode_t *sdzp = VTOZ(sdvp);
3352 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3355 zfs_dirlock_t *sdl, *tdl;
3358 int cmp, serr, terr;
3363 ZFS_VERIFY_ZP(sdzp);
3364 zilog = zfsvfs->z_log;
3367 * Make sure we have the real vp for the target directory.
3369 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3372 if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) {
3378 ZFS_VERIFY_ZP(tdzp);
3379 if (zfsvfs->z_utf8 && u8_validate(tnm,
3380 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3385 if (flags & FIGNORECASE)
3394 * This is to prevent the creation of links into attribute space
3395 * by renaming a linked file into/outof an attribute directory.
3396 * See the comment in zfs_link() for why this is considered bad.
3398 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3404 * Lock source and target directory entries. To prevent deadlock,
3405 * a lock ordering must be defined. We lock the directory with
3406 * the smallest object id first, or if it's a tie, the one with
3407 * the lexically first name.
3409 if (sdzp->z_id < tdzp->z_id) {
3411 } else if (sdzp->z_id > tdzp->z_id) {
3415 * First compare the two name arguments without
3416 * considering any case folding.
3418 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3420 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3421 ASSERT(error == 0 || !zfsvfs->z_utf8);
3424 * POSIX: "If the old argument and the new argument
3425 * both refer to links to the same existing file,
3426 * the rename() function shall return successfully
3427 * and perform no other action."
3433 * If the file system is case-folding, then we may
3434 * have some more checking to do. A case-folding file
3435 * system is either supporting mixed case sensitivity
3436 * access or is completely case-insensitive. Note
3437 * that the file system is always case preserving.
3439 * In mixed sensitivity mode case sensitive behavior
3440 * is the default. FIGNORECASE must be used to
3441 * explicitly request case insensitive behavior.
3443 * If the source and target names provided differ only
3444 * by case (e.g., a request to rename 'tim' to 'Tim'),
3445 * we will treat this as a special case in the
3446 * case-insensitive mode: as long as the source name
3447 * is an exact match, we will allow this to proceed as
3448 * a name-change request.
3450 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3451 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3452 flags & FIGNORECASE)) &&
3453 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3456 * case preserving rename request, require exact
3465 * If the source and destination directories are the same, we should
3466 * grab the z_name_lock of that directory only once.
3470 rw_enter(&sdzp->z_name_lock, RW_READER);
3474 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3475 ZEXISTS | zflg, NULL, NULL);
3476 terr = zfs_dirent_lock(&tdl,
3477 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3479 terr = zfs_dirent_lock(&tdl,
3480 tdzp, tnm, &tzp, zflg, NULL, NULL);
3481 serr = zfs_dirent_lock(&sdl,
3482 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3488 * Source entry invalid or not there.
3491 zfs_dirent_unlock(tdl);
3497 rw_exit(&sdzp->z_name_lock);
3499 if (strcmp(snm, "..") == 0)
3505 zfs_dirent_unlock(sdl);
3509 rw_exit(&sdzp->z_name_lock);
3511 if (strcmp(tnm, "..") == 0)
3518 * Must have write access at the source to remove the old entry
3519 * and write access at the target to create the new entry.
3520 * Note that if target and source are the same, this can be
3521 * done in a single check.
3524 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3527 if (ZTOV(szp)->v_type == VDIR) {
3529 * Check to make sure rename is valid.
3530 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3532 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3537 * Does target exist?
3541 * Source and target must be the same type.
3543 if (ZTOV(szp)->v_type == VDIR) {
3544 if (ZTOV(tzp)->v_type != VDIR) {
3549 if (ZTOV(tzp)->v_type == VDIR) {
3555 * POSIX dictates that when the source and target
3556 * entries refer to the same file object, rename
3557 * must do nothing and exit without error.
3559 if (szp->z_id == tzp->z_id) {
3565 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3567 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3570 * notify the target directory if it is not the same
3571 * as source directory.
3574 vnevent_rename_dest_dir(tdvp, ct);
3577 tx = dmu_tx_create(zfsvfs->z_os);
3578 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3579 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3580 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3581 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3583 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3584 zfs_sa_upgrade_txholds(tx, tdzp);
3587 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3588 zfs_sa_upgrade_txholds(tx, tzp);
3591 zfs_sa_upgrade_txholds(tx, szp);
3592 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3593 error = dmu_tx_assign(tx, TXG_NOWAIT);
3596 zfs_rename_unlock(&zl);
3597 zfs_dirent_unlock(sdl);
3598 zfs_dirent_unlock(tdl);
3601 rw_exit(&sdzp->z_name_lock);
3606 if (error == ERESTART) {
3616 if (tzp) /* Attempt to remove the existing target */
3617 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3620 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3622 szp->z_pflags |= ZFS_AV_MODIFIED;
3624 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3625 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3626 ASSERT3U(error, ==, 0);
3628 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3630 zfs_log_rename(zilog, tx, TX_RENAME |
3631 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3632 sdl->dl_name, tdzp, tdl->dl_name, szp);
3635 * Update path information for the target vnode
3637 vn_renamepath(tdvp, ZTOV(szp), tnm,
3641 * At this point, we have successfully created
3642 * the target name, but have failed to remove
3643 * the source name. Since the create was done
3644 * with the ZRENAMING flag, there are
3645 * complications; for one, the link count is
3646 * wrong. The easiest way to deal with this
3647 * is to remove the newly created target, and
3648 * return the original error. This must
3649 * succeed; fortunately, it is very unlikely to
3650 * fail, since we just created it.
3652 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3653 ZRENAMING, NULL), ==, 0);
3661 zfs_rename_unlock(&zl);
3663 zfs_dirent_unlock(sdl);
3664 zfs_dirent_unlock(tdl);
3667 rw_exit(&sdzp->z_name_lock);
3674 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3675 zil_commit(zilog, 0);
3682 * Insert the indicated symbolic reference entry into the directory.
3684 * IN: dvp - Directory to contain new symbolic link.
3685 * link - Name for new symlink entry.
3686 * vap - Attributes of new entry.
3687 * target - Target path of new symlink.
3688 * cr - credentials of caller.
3689 * ct - caller context
3690 * flags - case flags
3692 * RETURN: 0 if success
3693 * error code if failure
3696 * dvp - ctime|mtime updated
3700 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3701 caller_context_t *ct, int flags)
3703 znode_t *zp, *dzp = VTOZ(dvp);
3706 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3708 uint64_t len = strlen(link);
3711 zfs_acl_ids_t acl_ids;
3712 boolean_t fuid_dirtied;
3713 uint64_t txtype = TX_SYMLINK;
3715 ASSERT(vap->va_type == VLNK);
3719 zilog = zfsvfs->z_log;
3721 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3722 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3726 if (flags & FIGNORECASE)
3729 if (len > MAXPATHLEN) {
3731 return (ENAMETOOLONG);
3734 if ((error = zfs_acl_ids_create(dzp, 0,
3735 vap, cr, NULL, &acl_ids)) != 0) {
3741 * Attempt to lock directory; fail if entry already exists.
3743 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3745 zfs_acl_ids_free(&acl_ids);
3750 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3751 zfs_acl_ids_free(&acl_ids);
3752 zfs_dirent_unlock(dl);
3757 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3758 zfs_acl_ids_free(&acl_ids);
3759 zfs_dirent_unlock(dl);
3763 tx = dmu_tx_create(zfsvfs->z_os);
3764 fuid_dirtied = zfsvfs->z_fuid_dirty;
3765 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3766 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3767 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3768 ZFS_SA_BASE_ATTR_SIZE + len);
3769 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3770 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3771 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3772 acl_ids.z_aclp->z_acl_bytes);
3775 zfs_fuid_txhold(zfsvfs, tx);
3776 error = dmu_tx_assign(tx, TXG_NOWAIT);
3778 zfs_dirent_unlock(dl);
3779 if (error == ERESTART) {
3784 zfs_acl_ids_free(&acl_ids);
3791 * Create a new object for the symlink.
3792 * for version 4 ZPL datsets the symlink will be an SA attribute
3794 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3797 zfs_fuid_sync(zfsvfs, tx);
3799 mutex_enter(&zp->z_lock);
3801 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3804 zfs_sa_symlink(zp, link, len, tx);
3805 mutex_exit(&zp->z_lock);
3808 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3809 &zp->z_size, sizeof (zp->z_size), tx);
3811 * Insert the new object into the directory.
3813 (void) zfs_link_create(dl, zp, tx, ZNEW);
3815 if (flags & FIGNORECASE)
3817 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3819 zfs_acl_ids_free(&acl_ids);
3823 zfs_dirent_unlock(dl);
3827 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3828 zil_commit(zilog, 0);
3835 * Return, in the buffer contained in the provided uio structure,
3836 * the symbolic path referred to by vp.
3838 * IN: vp - vnode of symbolic link.
3839 * uoip - structure to contain the link path.
3840 * cr - credentials of caller.
3841 * ct - caller context
3843 * OUT: uio - structure to contain the link path.
3845 * RETURN: 0 if success
3846 * error code if failure
3849 * vp - atime updated
3853 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3855 znode_t *zp = VTOZ(vp);
3856 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3862 mutex_enter(&zp->z_lock);
3864 error = sa_lookup_uio(zp->z_sa_hdl,
3865 SA_ZPL_SYMLINK(zfsvfs), uio);
3867 error = zfs_sa_readlink(zp, uio);
3868 mutex_exit(&zp->z_lock);
3870 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3877 * Insert a new entry into directory tdvp referencing svp.
3879 * IN: tdvp - Directory to contain new entry.
3880 * svp - vnode of new entry.
3881 * name - name of new entry.
3882 * cr - credentials of caller.
3883 * ct - caller context
3885 * RETURN: 0 if success
3886 * error code if failure
3889 * tdvp - ctime|mtime updated
3890 * svp - ctime updated
3894 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
3895 caller_context_t *ct, int flags)
3897 znode_t *dzp = VTOZ(tdvp);
3899 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3909 ASSERT(tdvp->v_type == VDIR);
3913 zilog = zfsvfs->z_log;
3915 if (VOP_REALVP(svp, &realvp, ct) == 0)
3919 * POSIX dictates that we return EPERM here.
3920 * Better choices include ENOTSUP or EISDIR.
3922 if (svp->v_type == VDIR) {
3927 if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) {
3935 /* Prevent links to .zfs/shares files */
3937 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3938 &parent, sizeof (uint64_t))) != 0) {
3942 if (parent == zfsvfs->z_shares_dir) {
3947 if (zfsvfs->z_utf8 && u8_validate(name,
3948 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3952 if (flags & FIGNORECASE)
3956 * We do not support links between attributes and non-attributes
3957 * because of the potential security risk of creating links
3958 * into "normal" file space in order to circumvent restrictions
3959 * imposed in attribute space.
3961 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3967 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
3968 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3973 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3980 * Attempt to lock directory; fail if entry already exists.
3982 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3988 tx = dmu_tx_create(zfsvfs->z_os);
3989 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3990 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3991 zfs_sa_upgrade_txholds(tx, szp);
3992 zfs_sa_upgrade_txholds(tx, dzp);
3993 error = dmu_tx_assign(tx, TXG_NOWAIT);
3995 zfs_dirent_unlock(dl);
3996 if (error == ERESTART) {
4006 error = zfs_link_create(dl, szp, tx, 0);
4009 uint64_t txtype = TX_LINK;
4010 if (flags & FIGNORECASE)
4012 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4017 zfs_dirent_unlock(dl);
4020 vnevent_link(svp, ct);
4023 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4024 zil_commit(zilog, 0);
4031 * zfs_null_putapage() is used when the file system has been force
4032 * unmounted. It just drops the pages.
4036 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4037 size_t *lenp, int flags, cred_t *cr)
4039 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4044 * Push a page out to disk, klustering if possible.
4046 * IN: vp - file to push page to.
4047 * pp - page to push.
4048 * flags - additional flags.
4049 * cr - credentials of caller.
4051 * OUT: offp - start of range pushed.
4052 * lenp - len of range pushed.
4054 * RETURN: 0 if success
4055 * error code if failure
4057 * NOTE: callers must have locked the page to be pushed. On
4058 * exit, the page (and all other pages in the kluster) must be
4063 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4064 size_t *lenp, int flags, cred_t *cr)
4066 znode_t *zp = VTOZ(vp);
4067 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4069 u_offset_t off, koff;
4076 * If our blocksize is bigger than the page size, try to kluster
4077 * multiple pages so that we write a full block (thus avoiding
4078 * a read-modify-write).
4080 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4081 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4082 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4083 ASSERT(koff <= zp->z_size);
4084 if (koff + klen > zp->z_size)
4085 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4086 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4088 ASSERT3U(btop(len), ==, btopr(len));
4091 * Can't push pages past end-of-file.
4093 if (off >= zp->z_size) {
4094 /* ignore all pages */
4097 } else if (off + len > zp->z_size) {
4098 int npages = btopr(zp->z_size - off);
4101 page_list_break(&pp, &trunc, npages);
4102 /* ignore pages past end of file */
4104 pvn_write_done(trunc, flags);
4105 len = zp->z_size - off;
4108 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4109 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4114 tx = dmu_tx_create(zfsvfs->z_os);
4115 dmu_tx_hold_write(tx, zp->z_id, off, len);
4117 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4118 zfs_sa_upgrade_txholds(tx, zp);
4119 err = dmu_tx_assign(tx, TXG_NOWAIT);
4121 if (err == ERESTART) {
4130 if (zp->z_blksz <= PAGESIZE) {
4131 caddr_t va = zfs_map_page(pp, S_READ);
4132 ASSERT3U(len, <=, PAGESIZE);
4133 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4134 zfs_unmap_page(pp, va);
4136 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4140 uint64_t mtime[2], ctime[2];
4141 sa_bulk_attr_t bulk[3];
4144 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4146 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4148 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4150 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4152 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4157 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4167 * Copy the portion of the file indicated from pages into the file.
4168 * The pages are stored in a page list attached to the files vnode.
4170 * IN: vp - vnode of file to push page data to.
4171 * off - position in file to put data.
4172 * len - amount of data to write.
4173 * flags - flags to control the operation.
4174 * cr - credentials of caller.
4175 * ct - caller context.
4177 * RETURN: 0 if success
4178 * error code if failure
4181 * vp - ctime|mtime updated
4185 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4186 caller_context_t *ct)
4188 znode_t *zp = VTOZ(vp);
4189 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4201 * Align this request to the file block size in case we kluster.
4202 * XXX - this can result in pretty aggresive locking, which can
4203 * impact simultanious read/write access. One option might be
4204 * to break up long requests (len == 0) into block-by-block
4205 * operations to get narrower locking.
4207 blksz = zp->z_blksz;
4209 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4212 if (len > 0 && ISP2(blksz))
4213 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4219 * Search the entire vp list for pages >= io_off.
4221 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4222 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4225 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4227 if (off > zp->z_size) {
4228 /* past end of file */
4229 zfs_range_unlock(rl);
4234 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4236 for (off = io_off; io_off < off + len; io_off += io_len) {
4237 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4238 pp = page_lookup(vp, io_off,
4239 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4241 pp = page_lookup_nowait(vp, io_off,
4242 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4245 if (pp != NULL && pvn_getdirty(pp, flags)) {
4249 * Found a dirty page to push
4251 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4259 zfs_range_unlock(rl);
4260 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4261 zil_commit(zfsvfs->z_log, zp->z_id);
4268 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4270 znode_t *zp = VTOZ(vp);
4271 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4274 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4275 if (zp->z_sa_hdl == NULL) {
4277 * The fs has been unmounted, or we did a
4278 * suspend/resume and this file no longer exists.
4280 if (vn_has_cached_data(vp)) {
4281 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4285 mutex_enter(&zp->z_lock);
4286 mutex_enter(&vp->v_lock);
4287 ASSERT(vp->v_count == 1);
4289 mutex_exit(&vp->v_lock);
4290 mutex_exit(&zp->z_lock);
4291 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4297 * Attempt to push any data in the page cache. If this fails
4298 * we will get kicked out later in zfs_zinactive().
4300 if (vn_has_cached_data(vp)) {
4301 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4305 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4306 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4308 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4309 zfs_sa_upgrade_txholds(tx, zp);
4310 error = dmu_tx_assign(tx, TXG_WAIT);
4314 mutex_enter(&zp->z_lock);
4315 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4316 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4317 zp->z_atime_dirty = 0;
4318 mutex_exit(&zp->z_lock);
4324 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4328 * Bounds-check the seek operation.
4330 * IN: vp - vnode seeking within
4331 * ooff - old file offset
4332 * noffp - pointer to new file offset
4333 * ct - caller context
4335 * RETURN: 0 if success
4336 * EINVAL if new offset invalid
4340 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4341 caller_context_t *ct)
4343 if (vp->v_type == VDIR)
4345 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4349 * Pre-filter the generic locking function to trap attempts to place
4350 * a mandatory lock on a memory mapped file.
4353 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4354 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4356 znode_t *zp = VTOZ(vp);
4357 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4363 * We are following the UFS semantics with respect to mapcnt
4364 * here: If we see that the file is mapped already, then we will
4365 * return an error, but we don't worry about races between this
4366 * function and zfs_map().
4368 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4373 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4377 * If we can't find a page in the cache, we will create a new page
4378 * and fill it with file data. For efficiency, we may try to fill
4379 * multiple pages at once (klustering) to fill up the supplied page
4380 * list. Note that the pages to be filled are held with an exclusive
4381 * lock to prevent access by other threads while they are being filled.
4384 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4385 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4387 znode_t *zp = VTOZ(vp);
4388 page_t *pp, *cur_pp;
4389 objset_t *os = zp->z_zfsvfs->z_os;
4390 u_offset_t io_off, total;
4394 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4396 * We only have a single page, don't bother klustering
4400 pp = page_create_va(vp, io_off, io_len,
4401 PG_EXCL | PG_WAIT, seg, addr);
4404 * Try to find enough pages to fill the page list
4406 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4407 &io_len, off, plsz, 0);
4411 * The page already exists, nothing to do here.
4418 * Fill the pages in the kluster.
4421 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4424 ASSERT3U(io_off, ==, cur_pp->p_offset);
4425 va = zfs_map_page(cur_pp, S_WRITE);
4426 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4428 zfs_unmap_page(cur_pp, va);
4430 /* On error, toss the entire kluster */
4431 pvn_read_done(pp, B_ERROR);
4432 /* convert checksum errors into IO errors */
4437 cur_pp = cur_pp->p_next;
4441 * Fill in the page list array from the kluster starting
4442 * from the desired offset `off'.
4443 * NOTE: the page list will always be null terminated.
4445 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4446 ASSERT(pl == NULL || (*pl)->p_offset == off);
4452 * Return pointers to the pages for the file region [off, off + len]
4453 * in the pl array. If plsz is greater than len, this function may
4454 * also return page pointers from after the specified region
4455 * (i.e. the region [off, off + plsz]). These additional pages are
4456 * only returned if they are already in the cache, or were created as
4457 * part of a klustered read.
4459 * IN: vp - vnode of file to get data from.
4460 * off - position in file to get data from.
4461 * len - amount of data to retrieve.
4462 * plsz - length of provided page list.
4463 * seg - segment to obtain pages for.
4464 * addr - virtual address of fault.
4465 * rw - mode of created pages.
4466 * cr - credentials of caller.
4467 * ct - caller context.
4469 * OUT: protp - protection mode of created pages.
4470 * pl - list of pages created.
4472 * RETURN: 0 if success
4473 * error code if failure
4476 * vp - atime updated
4480 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4481 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4482 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4484 znode_t *zp = VTOZ(vp);
4485 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4489 /* we do our own caching, faultahead is unnecessary */
4492 else if (len > plsz)
4495 len = P2ROUNDUP(len, PAGESIZE);
4496 ASSERT(plsz >= len);
4505 * Loop through the requested range [off, off + len) looking
4506 * for pages. If we don't find a page, we will need to create
4507 * a new page and fill it with data from the file.
4510 if (*pl = page_lookup(vp, off, SE_SHARED))
4512 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4515 ASSERT3U((*pl)->p_offset, ==, off);
4519 ASSERT3U(len, >=, PAGESIZE);
4522 ASSERT3U(plsz, >=, PAGESIZE);
4529 * Fill out the page array with any pages already in the cache.
4532 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4539 * Release any pages we have previously locked.
4544 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4554 * Request a memory map for a section of a file. This code interacts
4555 * with common code and the VM system as follows:
4557 * common code calls mmap(), which ends up in smmap_common()
4559 * this calls VOP_MAP(), which takes you into (say) zfs
4561 * zfs_map() calls as_map(), passing segvn_create() as the callback
4563 * segvn_create() creates the new segment and calls VOP_ADDMAP()
4565 * zfs_addmap() updates z_mapcnt
4569 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4570 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4571 caller_context_t *ct)
4573 znode_t *zp = VTOZ(vp);
4574 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4575 segvn_crargs_t vn_a;
4581 if ((prot & PROT_WRITE) && (zp->z_pflags &
4582 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4587 if ((prot & (PROT_READ | PROT_EXEC)) &&
4588 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4593 if (vp->v_flag & VNOMAP) {
4598 if (off < 0 || len > MAXOFFSET_T - off) {
4603 if (vp->v_type != VREG) {
4609 * If file is locked, disallow mapping.
4611 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4617 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4625 vn_a.offset = (u_offset_t)off;
4626 vn_a.type = flags & MAP_TYPE;
4628 vn_a.maxprot = maxprot;
4631 vn_a.flags = flags & ~MAP_TYPE;
4633 vn_a.lgrp_mem_policy_flags = 0;
4635 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4644 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4645 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4646 caller_context_t *ct)
4648 uint64_t pages = btopr(len);
4650 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4655 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4656 * more accurate mtime for the associated file. Since we don't have a way of
4657 * detecting when the data was actually modified, we have to resort to
4658 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4659 * last page is pushed. The problem occurs when the msync() call is omitted,
4660 * which by far the most common case:
4668 * putpage() via fsflush
4670 * If we wait until fsflush to come along, we can have a modification time that
4671 * is some arbitrary point in the future. In order to prevent this in the
4672 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4677 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4678 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4679 caller_context_t *ct)
4681 uint64_t pages = btopr(len);
4683 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4684 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4686 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4687 vn_has_cached_data(vp))
4688 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
4694 * Free or allocate space in a file. Currently, this function only
4695 * supports the `F_FREESP' command. However, this command is somewhat
4696 * misnamed, as its functionality includes the ability to allocate as
4697 * well as free space.
4699 * IN: vp - vnode of file to free data in.
4700 * cmd - action to take (only F_FREESP supported).
4701 * bfp - section of file to free/alloc.
4702 * flag - current file open mode flags.
4703 * offset - current file offset.
4704 * cr - credentials of caller [UNUSED].
4705 * ct - caller context.
4707 * RETURN: 0 if success
4708 * error code if failure
4711 * vp - ctime|mtime updated
4715 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4716 offset_t offset, cred_t *cr, caller_context_t *ct)
4718 znode_t *zp = VTOZ(vp);
4719 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4726 if (cmd != F_FREESP) {
4731 if ((error = convoff(vp, bfp, 0, offset))) {
4736 if (bfp->l_len < 0) {
4742 len = bfp->l_len; /* 0 means from off to end of file */
4744 error = zfs_freesp(zp, off, len, flag, TRUE);
4752 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4754 znode_t *zp = VTOZ(vp);
4755 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4758 uint64_t object = zp->z_id;
4765 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4766 &gen64, sizeof (uint64_t))) != 0) {
4771 gen = (uint32_t)gen64;
4773 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4774 if (fidp->fid_len < size) {
4775 fidp->fid_len = size;
4780 zfid = (zfid_short_t *)fidp;
4782 zfid->zf_len = size;
4784 for (i = 0; i < sizeof (zfid->zf_object); i++)
4785 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4787 /* Must have a non-zero generation number to distinguish from .zfs */
4790 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4791 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4793 if (size == LONG_FID_LEN) {
4794 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4797 zlfid = (zfid_long_t *)fidp;
4799 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4800 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4802 /* XXX - this should be the generation number for the objset */
4803 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4804 zlfid->zf_setgen[i] = 0;
4812 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4813 caller_context_t *ct)
4825 case _PC_FILESIZEBITS:
4829 case _PC_XATTR_EXISTS:
4831 zfsvfs = zp->z_zfsvfs;
4835 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4836 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4838 zfs_dirent_unlock(dl);
4839 if (!zfs_dirempty(xzp))
4842 } else if (error == ENOENT) {
4844 * If there aren't extended attributes, it's the
4845 * same as having zero of them.
4852 case _PC_SATTR_ENABLED:
4853 case _PC_SATTR_EXISTS:
4854 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4855 (vp->v_type == VREG || vp->v_type == VDIR);
4858 case _PC_ACCESS_FILTERING:
4859 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4863 case _PC_ACL_ENABLED:
4864 *valp = _ACL_ACE_ENABLED;
4867 case _PC_MIN_HOLE_SIZE:
4868 *valp = (ulong_t)SPA_MINBLOCKSIZE;
4871 case _PC_TIMESTAMP_RESOLUTION:
4872 /* nanosecond timestamp resolution */
4877 return (fs_pathconf(vp, cmd, valp, cr, ct));
4883 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4884 caller_context_t *ct)
4886 znode_t *zp = VTOZ(vp);
4887 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4889 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4893 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4901 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4902 caller_context_t *ct)
4904 znode_t *zp = VTOZ(vp);
4905 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4907 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4908 zilog_t *zilog = zfsvfs->z_log;
4913 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4915 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4916 zil_commit(zilog, 0);
4923 * Tunable, both must be a power of 2.
4925 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4926 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4927 * an arcbuf for a partial block read
4929 int zcr_blksz_min = (1 << 10); /* 1K */
4930 int zcr_blksz_max = (1 << 17); /* 128K */
4934 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
4935 caller_context_t *ct)
4937 znode_t *zp = VTOZ(vp);
4938 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4939 int max_blksz = zfsvfs->z_max_blksz;
4940 uio_t *uio = &xuio->xu_uio;
4941 ssize_t size = uio->uio_resid;
4942 offset_t offset = uio->uio_loffset;
4947 int preamble, postamble;
4949 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4957 * Loan out an arc_buf for write if write size is bigger than
4958 * max_blksz, and the file's block size is also max_blksz.
4961 if (size < blksz || zp->z_blksz != blksz) {
4966 * Caller requests buffers for write before knowing where the
4967 * write offset might be (e.g. NFS TCP write).
4972 preamble = P2PHASE(offset, blksz);
4974 preamble = blksz - preamble;
4979 postamble = P2PHASE(size, blksz);
4982 fullblk = size / blksz;
4983 (void) dmu_xuio_init(xuio,
4984 (preamble != 0) + fullblk + (postamble != 0));
4985 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
4986 int, postamble, int,
4987 (preamble != 0) + fullblk + (postamble != 0));
4990 * Have to fix iov base/len for partial buffers. They
4991 * currently represent full arc_buf's.
4994 /* data begins in the middle of the arc_buf */
4995 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4998 (void) dmu_xuio_add(xuio, abuf,
4999 blksz - preamble, preamble);
5002 for (i = 0; i < fullblk; i++) {
5003 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5006 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5010 /* data ends in the middle of the arc_buf */
5011 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5014 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5019 * Loan out an arc_buf for read if the read size is larger than
5020 * the current file block size. Block alignment is not
5021 * considered. Partial arc_buf will be loaned out for read.
5023 blksz = zp->z_blksz;
5024 if (blksz < zcr_blksz_min)
5025 blksz = zcr_blksz_min;
5026 if (blksz > zcr_blksz_max)
5027 blksz = zcr_blksz_max;
5028 /* avoid potential complexity of dealing with it */
5029 if (blksz > max_blksz) {
5034 maxsize = zp->z_size - uio->uio_loffset;
5038 if (size < blksz || vn_has_cached_data(vp)) {
5048 uio->uio_extflg = UIO_XUIO;
5049 XUIO_XUZC_RW(xuio) = ioflag;
5056 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5060 int ioflag = XUIO_XUZC_RW(xuio);
5062 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5064 i = dmu_xuio_cnt(xuio);
5066 abuf = dmu_xuio_arcbuf(xuio, i);
5068 * if abuf == NULL, it must be a write buffer
5069 * that has been returned in zfs_write().
5072 dmu_return_arcbuf(abuf);
5073 ASSERT(abuf || ioflag == UIO_WRITE);
5076 dmu_xuio_fini(xuio);
5081 * Predeclare these here so that the compiler assumes that
5082 * this is an "old style" function declaration that does
5083 * not include arguments => we won't get type mismatch errors
5084 * in the initializations that follow.
5086 static int zfs_inval();
5087 static int zfs_isdir();
5101 * Directory vnode operations template
5103 vnodeops_t *zfs_dvnodeops;
5104 const fs_operation_def_t zfs_dvnodeops_template[] = {
5105 VOPNAME_OPEN, { .vop_open = zfs_open },
5106 VOPNAME_CLOSE, { .vop_close = zfs_close },
5107 VOPNAME_READ, { .error = zfs_isdir },
5108 VOPNAME_WRITE, { .error = zfs_isdir },
5109 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5110 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5111 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5112 VOPNAME_ACCESS, { .vop_access = zfs_access },
5113 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5114 VOPNAME_CREATE, { .vop_create = zfs_create },
5115 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5116 VOPNAME_LINK, { .vop_link = zfs_link },
5117 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5118 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5119 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5120 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5121 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5122 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5123 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5124 VOPNAME_FID, { .vop_fid = zfs_fid },
5125 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5126 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5127 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5128 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5129 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5134 * Regular file vnode operations template
5136 vnodeops_t *zfs_fvnodeops;
5137 const fs_operation_def_t zfs_fvnodeops_template[] = {
5138 VOPNAME_OPEN, { .vop_open = zfs_open },
5139 VOPNAME_CLOSE, { .vop_close = zfs_close },
5140 VOPNAME_READ, { .vop_read = zfs_read },
5141 VOPNAME_WRITE, { .vop_write = zfs_write },
5142 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5143 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5144 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5145 VOPNAME_ACCESS, { .vop_access = zfs_access },
5146 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5147 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5148 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5149 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5150 VOPNAME_FID, { .vop_fid = zfs_fid },
5151 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5152 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5153 VOPNAME_SPACE, { .vop_space = zfs_space },
5154 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5155 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5156 VOPNAME_MAP, { .vop_map = zfs_map },
5157 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5158 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5159 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5160 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5161 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5162 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5163 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5164 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5169 * Symbolic link vnode operations template
5171 vnodeops_t *zfs_symvnodeops;
5172 const fs_operation_def_t zfs_symvnodeops_template[] = {
5173 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5174 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5175 VOPNAME_ACCESS, { .vop_access = zfs_access },
5176 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5177 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5178 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5179 VOPNAME_FID, { .vop_fid = zfs_fid },
5180 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5181 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5186 * special share hidden files vnode operations template
5188 vnodeops_t *zfs_sharevnodeops;
5189 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5190 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5191 VOPNAME_ACCESS, { .vop_access = zfs_access },
5192 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5193 VOPNAME_FID, { .vop_fid = zfs_fid },
5194 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5195 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5196 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5197 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5202 * Extended attribute directory vnode operations template
5203 * This template is identical to the directory vnodes
5204 * operation template except for restricted operations:
5207 * Note that there are other restrictions embedded in:
5208 * zfs_create() - restrict type to VREG
5209 * zfs_link() - no links into/out of attribute space
5210 * zfs_rename() - no moves into/out of attribute space
5212 vnodeops_t *zfs_xdvnodeops;
5213 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5214 VOPNAME_OPEN, { .vop_open = zfs_open },
5215 VOPNAME_CLOSE, { .vop_close = zfs_close },
5216 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5217 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5218 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5219 VOPNAME_ACCESS, { .vop_access = zfs_access },
5220 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5221 VOPNAME_CREATE, { .vop_create = zfs_create },
5222 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5223 VOPNAME_LINK, { .vop_link = zfs_link },
5224 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5225 VOPNAME_MKDIR, { .error = zfs_inval },
5226 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5227 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5228 VOPNAME_SYMLINK, { .error = zfs_inval },
5229 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5230 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5231 VOPNAME_FID, { .vop_fid = zfs_fid },
5232 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5233 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5234 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5235 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5236 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5241 * Error vnode operations template
5243 vnodeops_t *zfs_evnodeops;
5244 const fs_operation_def_t zfs_evnodeops_template[] = {
5245 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5246 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5249 #endif /* HAVE_ZPL */