4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
27 /* Portions Copyright 2010 Robert Milkowski */
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>
41 #include <sys/taskq.h>
43 #include <sys/vmsystm.h>
44 #include <sys/atomic.h>
46 #include <sys/pathname.h>
47 #include <sys/cmn_err.h>
48 #include <sys/errno.h>
49 #include <sys/unistd.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/fs/zfs.h>
55 #include <sys/dmu_objset.h>
61 #include <sys/dirent.h>
62 #include <sys/policy.h>
63 #include <sys/sunddi.h>
66 #include "fs/fs_subr.h"
67 #include <sys/zfs_ctldir.h>
68 #include <sys/zfs_fuid.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_vnops.h>
72 #include <sys/zfs_rlock.h>
73 #include <sys/extdirent.h>
74 #include <sys/kidmap.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zsb).
92 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) iput() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
104 * If you must call iput() within a tx then use iput_ASYNC().
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
110 * dmu_tx_assign(). This is critical because we don't want to block
111 * while holding locks.
113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
114 * reduces lock contention and CPU usage when we must wait (note that if
115 * throughput is constrained by the storage, nearly every transaction
118 * Note, in particular, that if a lock is sometimes acquired before
119 * the tx assigns, and sometimes after (e.g. z_lock), then failing
120 * to use a non-blocking assign can deadlock the system. The scenario:
122 * Thread A has grabbed a lock before calling dmu_tx_assign().
123 * Thread B is in an already-assigned tx, and blocks for this lock.
124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
125 * forever, because the previous txg can't quiesce until B's tx commits.
127 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
128 * then drop all locks, call dmu_tx_wait(), and try again.
130 * (5) If the operation succeeded, generate the intent log entry for it
131 * before dropping locks. This ensures that the ordering of events
132 * in the intent log matches the order in which they actually occurred.
133 * During ZIL replay the zfs_log_* functions will update the sequence
134 * number to indicate the zil transaction has replayed.
136 * (6) At the end of each vnode op, the DMU tx must always commit,
137 * regardless of whether there were any errors.
139 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
140 * to ensure that synchronous semantics are provided when necessary.
142 * In general, this is how things should be ordered in each vnode op:
144 * ZFS_ENTER(zsb); // exit if unmounted
146 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
147 * rw_enter(...); // grab any other locks you need
148 * tx = dmu_tx_create(...); // get DMU tx
149 * dmu_tx_hold_*(); // hold each object you might modify
150 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
152 * rw_exit(...); // drop locks
153 * zfs_dirent_unlock(dl); // unlock directory entry
154 * iput(...); // release held vnodes
155 * if (error == ERESTART) {
160 * dmu_tx_abort(tx); // abort DMU tx
161 * ZFS_EXIT(zsb); // finished in zfs
162 * return (error); // really out of space
164 * error = do_real_work(); // do whatever this VOP does
166 * zfs_log_*(...); // on success, make ZIL entry
167 * dmu_tx_commit(tx); // commit DMU tx -- error or not
168 * rw_exit(...); // drop locks
169 * zfs_dirent_unlock(dl); // unlock directory entry
170 * iput(...); // release held vnodes
171 * zil_commit(zilog, foid); // synchronous when necessary
172 * ZFS_EXIT(zsb); // finished in zfs
173 * return (error); // done, report error
177 * Virus scanning is unsupported. It would be possible to add a hook
178 * here to performance the required virus scan. This could be done
179 * entirely in the kernel or potentially as an update to invoke a
183 zfs_vscan(struct inode *ip, cred_t *cr, int async)
190 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
192 znode_t *zp = ITOZ(ip);
193 zfs_sb_t *zsb = ITOZSB(ip);
198 /* Honor ZFS_APPENDONLY file attribute */
199 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
200 ((flag & O_APPEND) == 0)) {
202 return (SET_ERROR(EPERM));
205 /* Virus scan eligible files on open */
206 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
207 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
208 if (zfs_vscan(ip, cr, 0) != 0) {
210 return (SET_ERROR(EACCES));
214 /* Keep a count of the synchronous opens in the znode */
216 atomic_inc_32(&zp->z_sync_cnt);
221 EXPORT_SYMBOL(zfs_open);
225 zfs_close(struct inode *ip, int flag, cred_t *cr)
227 znode_t *zp = ITOZ(ip);
228 zfs_sb_t *zsb = ITOZSB(ip);
234 * Zero the synchronous opens in the znode. Under Linux the
235 * zfs_close() hook is not symmetric with zfs_open(), it is
236 * only called once when the last reference is dropped.
241 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
242 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
243 VERIFY(zfs_vscan(ip, cr, 1) == 0);
248 EXPORT_SYMBOL(zfs_close);
250 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
252 * Lseek support for finding holes (cmd == SEEK_HOLE) and
253 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
256 zfs_holey_common(struct inode *ip, int cmd, loff_t *off)
258 znode_t *zp = ITOZ(ip);
259 uint64_t noff = (uint64_t)*off; /* new offset */
264 file_sz = zp->z_size;
265 if (noff >= file_sz) {
266 return (SET_ERROR(ENXIO));
269 if (cmd == SEEK_HOLE)
274 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
277 if ((error == ESRCH) || (noff > file_sz)) {
279 * Handle the virtual hole at the end of file.
285 return (SET_ERROR(ENXIO));
295 zfs_holey(struct inode *ip, int cmd, loff_t *off)
297 znode_t *zp = ITOZ(ip);
298 zfs_sb_t *zsb = ITOZSB(ip);
304 error = zfs_holey_common(ip, cmd, off);
309 EXPORT_SYMBOL(zfs_holey);
310 #endif /* SEEK_HOLE && SEEK_DATA */
314 * When a file is memory mapped, we must keep the IO data synchronized
315 * between the DMU cache and the memory mapped pages. What this means:
317 * On Write: If we find a memory mapped page, we write to *both*
318 * the page and the dmu buffer.
321 update_pages(struct inode *ip, int64_t start, int len,
322 objset_t *os, uint64_t oid)
324 struct address_space *mp = ip->i_mapping;
330 off = start & (PAGE_CACHE_SIZE-1);
331 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
332 nbytes = MIN(PAGE_CACHE_SIZE - off, len);
334 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
336 if (mapping_writably_mapped(mp))
337 flush_dcache_page(pp);
340 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
344 if (mapping_writably_mapped(mp))
345 flush_dcache_page(pp);
347 mark_page_accessed(pp);
351 page_cache_release(pp);
360 * When a file is memory mapped, we must keep the IO data synchronized
361 * between the DMU cache and the memory mapped pages. What this means:
363 * On Read: We "read" preferentially from memory mapped pages,
364 * else we default from the dmu buffer.
366 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
367 * the file is memory mapped.
370 mappedread(struct inode *ip, int nbytes, uio_t *uio)
372 struct address_space *mp = ip->i_mapping;
374 znode_t *zp = ITOZ(ip);
375 objset_t *os = ITOZSB(ip)->z_os;
382 start = uio->uio_loffset;
383 off = start & (PAGE_CACHE_SIZE-1);
384 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
385 bytes = MIN(PAGE_CACHE_SIZE - off, len);
387 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
389 ASSERT(PageUptodate(pp));
392 error = uiomove(pb + off, bytes, UIO_READ, uio);
395 if (mapping_writably_mapped(mp))
396 flush_dcache_page(pp);
398 mark_page_accessed(pp);
400 page_cache_release(pp);
402 error = dmu_read_uio(os, zp->z_id, uio, bytes);
414 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
417 * Read bytes from specified file into supplied buffer.
419 * IN: ip - inode of file to be read from.
420 * uio - structure supplying read location, range info,
422 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
423 * O_DIRECT flag; used to bypass page cache.
424 * cr - credentials of caller.
426 * OUT: uio - updated offset and range, buffer filled.
428 * RETURN: 0 on success, error code on failure.
431 * inode - atime updated if byte count > 0
435 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
437 znode_t *zp = ITOZ(ip);
438 zfs_sb_t *zsb = ITOZSB(ip);
443 #ifdef HAVE_UIO_ZEROCOPY
445 #endif /* HAVE_UIO_ZEROCOPY */
451 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
453 return (SET_ERROR(EACCES));
457 * Validate file offset
459 if (uio->uio_loffset < (offset_t)0) {
461 return (SET_ERROR(EINVAL));
465 * Fasttrack empty reads
467 if (uio->uio_resid == 0) {
473 * Check for mandatory locks
475 if (mandatory_lock(ip) &&
476 !lock_may_read(ip, uio->uio_loffset, uio->uio_resid)) {
478 return (SET_ERROR(EAGAIN));
482 * If we're in FRSYNC mode, sync out this znode before reading it.
484 if (ioflag & FRSYNC || zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
485 zil_commit(zsb->z_log, zp->z_id);
488 * Lock the range against changes.
490 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
493 * If we are reading past end-of-file we can skip
494 * to the end; but we might still need to set atime.
496 if (uio->uio_loffset >= zp->z_size) {
501 ASSERT(uio->uio_loffset < zp->z_size);
502 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
504 #ifdef HAVE_UIO_ZEROCOPY
505 if ((uio->uio_extflg == UIO_XUIO) &&
506 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
508 int blksz = zp->z_blksz;
509 uint64_t offset = uio->uio_loffset;
511 xuio = (xuio_t *)uio;
513 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
516 ASSERT(offset + n <= blksz);
519 (void) dmu_xuio_init(xuio, nblk);
521 if (vn_has_cached_data(ip)) {
523 * For simplicity, we always allocate a full buffer
524 * even if we only expect to read a portion of a block.
526 while (--nblk >= 0) {
527 (void) dmu_xuio_add(xuio,
528 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
533 #endif /* HAVE_UIO_ZEROCOPY */
536 nbytes = MIN(n, zfs_read_chunk_size -
537 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
539 if (zp->z_is_mapped && !(ioflag & O_DIRECT))
540 error = mappedread(ip, nbytes, uio);
542 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
545 /* convert checksum errors into IO errors */
547 error = SET_ERROR(EIO);
554 zfs_range_unlock(rl);
556 ZFS_ACCESSTIME_STAMP(zsb, zp);
557 zfs_inode_update(zp);
561 EXPORT_SYMBOL(zfs_read);
564 * Write the bytes to a file.
566 * IN: ip - inode of file to be written to.
567 * uio - structure supplying write location, range info,
569 * ioflag - FAPPEND flag set if in append mode.
570 * O_DIRECT flag; used to bypass page cache.
571 * cr - credentials of caller.
573 * OUT: uio - updated offset and range.
575 * RETURN: 0 if success
576 * error code if failure
579 * ip - ctime|mtime updated if byte count > 0
584 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
586 znode_t *zp = ITOZ(ip);
587 rlim64_t limit = uio->uio_limit;
588 ssize_t start_resid = uio->uio_resid;
592 zfs_sb_t *zsb = ZTOZSB(zp);
597 int max_blksz = zsb->z_max_blksz;
600 iovec_t *aiov = NULL;
603 iovec_t *iovp = uio->uio_iov;
606 sa_bulk_attr_t bulk[4];
607 uint64_t mtime[2], ctime[2];
608 ASSERTV(int iovcnt = uio->uio_iovcnt);
611 * Fasttrack empty write
617 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
623 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
624 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
625 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
626 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
630 * If immutable or not appending then return EPERM
632 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
633 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
634 (uio->uio_loffset < zp->z_size))) {
636 return (SET_ERROR(EPERM));
642 * Validate file offset
644 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
647 return (SET_ERROR(EINVAL));
651 * Check for mandatory locks before calling zfs_range_lock()
652 * in order to prevent a deadlock with locks set via fcntl().
654 if (mandatory_lock(ip) && !lock_may_write(ip, woff, n)) {
656 return (SET_ERROR(EAGAIN));
660 * Pre-fault the pages to ensure slow (eg NFS) pages
662 * Skip this if uio contains loaned arc_buf.
664 #ifdef HAVE_UIO_ZEROCOPY
665 if ((uio->uio_extflg == UIO_XUIO) &&
666 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
667 xuio = (xuio_t *)uio;
670 uio_prefaultpages(MIN(n, max_blksz), uio);
673 * If in append mode, set the io offset pointer to eof.
675 if (ioflag & FAPPEND) {
677 * Obtain an appending range lock to guarantee file append
678 * semantics. We reset the write offset once we have the lock.
680 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
682 if (rl->r_len == UINT64_MAX) {
684 * We overlocked the file because this write will cause
685 * the file block size to increase.
686 * Note that zp_size cannot change with this lock held.
690 uio->uio_loffset = woff;
693 * Note that if the file block size will change as a result of
694 * this write, then this range lock will lock the entire file
695 * so that we can re-write the block safely.
697 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
701 zfs_range_unlock(rl);
703 return (SET_ERROR(EFBIG));
706 if ((woff + n) > limit || woff > (limit - n))
709 /* Will this write extend the file length? */
710 write_eof = (woff + n > zp->z_size);
712 end_size = MAX(zp->z_size, woff + n);
715 * Write the file in reasonable size chunks. Each chunk is written
716 * in a separate transaction; this keeps the intent log records small
717 * and allows us to do more fine-grained space accounting.
721 woff = uio->uio_loffset;
722 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
723 zfs_owner_overquota(zsb, zp, B_TRUE)) {
725 dmu_return_arcbuf(abuf);
726 error = SET_ERROR(EDQUOT);
730 if (xuio && abuf == NULL) {
731 ASSERT(i_iov < iovcnt);
733 abuf = dmu_xuio_arcbuf(xuio, i_iov);
734 dmu_xuio_clear(xuio, i_iov);
735 ASSERT((aiov->iov_base == abuf->b_data) ||
736 ((char *)aiov->iov_base - (char *)abuf->b_data +
737 aiov->iov_len == arc_buf_size(abuf)));
739 } else if (abuf == NULL && n >= max_blksz &&
740 woff >= zp->z_size &&
741 P2PHASE(woff, max_blksz) == 0 &&
742 zp->z_blksz == max_blksz) {
744 * This write covers a full block. "Borrow" a buffer
745 * from the dmu so that we can fill it before we enter
746 * a transaction. This avoids the possibility of
747 * holding up the transaction if the data copy hangs
748 * up on a pagefault (e.g., from an NFS server mapping).
752 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
754 ASSERT(abuf != NULL);
755 ASSERT(arc_buf_size(abuf) == max_blksz);
756 if ((error = uiocopy(abuf->b_data, max_blksz,
757 UIO_WRITE, uio, &cbytes))) {
758 dmu_return_arcbuf(abuf);
761 ASSERT(cbytes == max_blksz);
765 * Start a transaction.
767 tx = dmu_tx_create(zsb->z_os);
768 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
769 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
770 zfs_sa_upgrade_txholds(tx, zp);
771 error = dmu_tx_assign(tx, TXG_WAIT);
775 dmu_return_arcbuf(abuf);
780 * If zfs_range_lock() over-locked we grow the blocksize
781 * and then reduce the lock range. This will only happen
782 * on the first iteration since zfs_range_reduce() will
783 * shrink down r_len to the appropriate size.
785 if (rl->r_len == UINT64_MAX) {
788 if (zp->z_blksz > max_blksz) {
789 ASSERT(!ISP2(zp->z_blksz));
790 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
792 new_blksz = MIN(end_size, max_blksz);
794 zfs_grow_blocksize(zp, new_blksz, tx);
795 zfs_range_reduce(rl, woff, n);
799 * XXX - should we really limit each write to z_max_blksz?
800 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
802 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
805 tx_bytes = uio->uio_resid;
806 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
808 tx_bytes -= uio->uio_resid;
811 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
813 * If this is not a full block write, but we are
814 * extending the file past EOF and this data starts
815 * block-aligned, use assign_arcbuf(). Otherwise,
816 * write via dmu_write().
818 if (tx_bytes < max_blksz && (!write_eof ||
819 aiov->iov_base != abuf->b_data)) {
821 dmu_write(zsb->z_os, zp->z_id, woff,
822 aiov->iov_len, aiov->iov_base, tx);
823 dmu_return_arcbuf(abuf);
824 xuio_stat_wbuf_copied();
826 ASSERT(xuio || tx_bytes == max_blksz);
827 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
830 ASSERT(tx_bytes <= uio->uio_resid);
831 uioskip(uio, tx_bytes);
834 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
835 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
838 * If we made no progress, we're done. If we made even
839 * partial progress, update the znode and ZIL accordingly.
842 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
843 (void *)&zp->z_size, sizeof (uint64_t), tx);
850 * Clear Set-UID/Set-GID bits on successful write if not
851 * privileged and at least one of the excute bits is set.
853 * It would be nice to to this after all writes have
854 * been done, but that would still expose the ISUID/ISGID
855 * to another app after the partial write is committed.
857 * Note: we don't call zfs_fuid_map_id() here because
858 * user 0 is not an ephemeral uid.
860 mutex_enter(&zp->z_acl_lock);
861 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
862 (S_IXUSR >> 6))) != 0 &&
863 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
864 secpolicy_vnode_setid_retain(cr,
865 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
867 zp->z_mode &= ~(S_ISUID | S_ISGID);
868 newmode = zp->z_mode;
869 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
870 (void *)&newmode, sizeof (uint64_t), tx);
872 mutex_exit(&zp->z_acl_lock);
874 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
878 * Update the file size (zp_size) if it has changed;
879 * account for possible concurrent updates.
881 while ((end_size = zp->z_size) < uio->uio_loffset) {
882 (void) atomic_cas_64(&zp->z_size, end_size,
887 * If we are replaying and eof is non zero then force
888 * the file size to the specified eof. Note, there's no
889 * concurrency during replay.
891 if (zsb->z_replay && zsb->z_replay_eof != 0)
892 zp->z_size = zsb->z_replay_eof;
894 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
896 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
902 ASSERT(tx_bytes == nbytes);
906 uio_prefaultpages(MIN(n, max_blksz), uio);
909 zfs_range_unlock(rl);
912 * If we're in replay mode, or we made no progress, return error.
913 * Otherwise, it's at least a partial write, so it's successful.
915 if (zsb->z_replay || uio->uio_resid == start_resid) {
920 if (ioflag & (FSYNC | FDSYNC) ||
921 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
922 zil_commit(zilog, zp->z_id);
924 zfs_inode_update(zp);
928 EXPORT_SYMBOL(zfs_write);
931 iput_async(struct inode *ip, taskq_t *taskq)
933 ASSERT(atomic_read(&ip->i_count) > 0);
934 if (atomic_read(&ip->i_count) == 1)
935 taskq_dispatch(taskq, (task_func_t *)iput, ip, TQ_PUSHPAGE);
941 zfs_get_done(zgd_t *zgd, int error)
943 znode_t *zp = zgd->zgd_private;
944 objset_t *os = ZTOZSB(zp)->z_os;
947 dmu_buf_rele(zgd->zgd_db, zgd);
949 zfs_range_unlock(zgd->zgd_rl);
952 * Release the vnode asynchronously as we currently have the
953 * txg stopped from syncing.
955 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
957 if (error == 0 && zgd->zgd_bp)
958 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
960 kmem_free(zgd, sizeof (zgd_t));
964 static int zil_fault_io = 0;
968 * Get data to generate a TX_WRITE intent log record.
971 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
974 objset_t *os = zsb->z_os;
976 uint64_t object = lr->lr_foid;
977 uint64_t offset = lr->lr_offset;
978 uint64_t size = lr->lr_length;
979 blkptr_t *bp = &lr->lr_blkptr;
988 * Nothing to do if the file has been removed
990 if (zfs_zget(zsb, object, &zp) != 0)
991 return (SET_ERROR(ENOENT));
992 if (zp->z_unlinked) {
994 * Release the vnode asynchronously as we currently have the
995 * txg stopped from syncing.
997 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
998 return (SET_ERROR(ENOENT));
1001 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_PUSHPAGE);
1002 zgd->zgd_zilog = zsb->z_log;
1003 zgd->zgd_private = zp;
1006 * Write records come in two flavors: immediate and indirect.
1007 * For small writes it's cheaper to store the data with the
1008 * log record (immediate); for large writes it's cheaper to
1009 * sync the data and get a pointer to it (indirect) so that
1010 * we don't have to write the data twice.
1012 if (buf != NULL) { /* immediate write */
1013 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1014 /* test for truncation needs to be done while range locked */
1015 if (offset >= zp->z_size) {
1016 error = SET_ERROR(ENOENT);
1018 error = dmu_read(os, object, offset, size, buf,
1019 DMU_READ_NO_PREFETCH);
1021 ASSERT(error == 0 || error == ENOENT);
1022 } else { /* indirect write */
1024 * Have to lock the whole block to ensure when it's
1025 * written out and it's checksum is being calculated
1026 * that no one can change the data. We need to re-check
1027 * blocksize after we get the lock in case it's changed!
1032 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1034 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1036 if (zp->z_blksz == size)
1039 zfs_range_unlock(zgd->zgd_rl);
1041 /* test for truncation needs to be done while range locked */
1042 if (lr->lr_offset >= zp->z_size)
1043 error = SET_ERROR(ENOENT);
1046 error = SET_ERROR(EIO);
1051 error = dmu_buf_hold(os, object, offset, zgd, &db,
1052 DMU_READ_NO_PREFETCH);
1055 blkptr_t *obp = dmu_buf_get_blkptr(db);
1057 ASSERT(BP_IS_HOLE(bp));
1064 ASSERT(db->db_offset == offset);
1065 ASSERT(db->db_size == size);
1067 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1069 ASSERT(error || lr->lr_length <= zp->z_blksz);
1072 * On success, we need to wait for the write I/O
1073 * initiated by dmu_sync() to complete before we can
1074 * release this dbuf. We will finish everything up
1075 * in the zfs_get_done() callback.
1080 if (error == EALREADY) {
1081 lr->lr_common.lrc_txtype = TX_WRITE2;
1087 zfs_get_done(zgd, error);
1094 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1096 znode_t *zp = ITOZ(ip);
1097 zfs_sb_t *zsb = ITOZSB(ip);
1103 if (flag & V_ACE_MASK)
1104 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1106 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1111 EXPORT_SYMBOL(zfs_access);
1114 * Lookup an entry in a directory, or an extended attribute directory.
1115 * If it exists, return a held inode reference for it.
1117 * IN: dip - inode of directory to search.
1118 * nm - name of entry to lookup.
1119 * flags - LOOKUP_XATTR set if looking for an attribute.
1120 * cr - credentials of caller.
1121 * direntflags - directory lookup flags
1122 * realpnp - returned pathname.
1124 * OUT: ipp - inode of located entry, NULL if not found.
1126 * RETURN: 0 on success, error code on failure.
1133 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1134 cred_t *cr, int *direntflags, pathname_t *realpnp)
1136 znode_t *zdp = ITOZ(dip);
1137 zfs_sb_t *zsb = ITOZSB(dip);
1141 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1143 if (!S_ISDIR(dip->i_mode)) {
1144 return (SET_ERROR(ENOTDIR));
1145 } else if (zdp->z_sa_hdl == NULL) {
1146 return (SET_ERROR(EIO));
1149 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1150 error = zfs_fastaccesschk_execute(zdp, cr);
1159 vnode_t *tvp = dnlc_lookup(dvp, nm);
1162 error = zfs_fastaccesschk_execute(zdp, cr);
1167 if (tvp == DNLC_NO_VNODE) {
1169 return (SET_ERROR(ENOENT));
1172 return (specvp_check(vpp, cr));
1175 #endif /* HAVE_DNLC */
1184 if (flags & LOOKUP_XATTR) {
1186 * We don't allow recursive attributes..
1187 * Maybe someday we will.
1189 if (zdp->z_pflags & ZFS_XATTR) {
1191 return (SET_ERROR(EINVAL));
1194 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1200 * Do we have permission to get into attribute directory?
1203 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1213 if (!S_ISDIR(dip->i_mode)) {
1215 return (SET_ERROR(ENOTDIR));
1219 * Check accessibility of directory.
1222 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1227 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
1228 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1230 return (SET_ERROR(EILSEQ));
1233 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1234 if ((error == 0) && (*ipp))
1235 zfs_inode_update(ITOZ(*ipp));
1240 EXPORT_SYMBOL(zfs_lookup);
1243 * Attempt to create a new entry in a directory. If the entry
1244 * already exists, truncate the file if permissible, else return
1245 * an error. Return the ip of the created or trunc'd file.
1247 * IN: dip - inode of directory to put new file entry in.
1248 * name - name of new file entry.
1249 * vap - attributes of new file.
1250 * excl - flag indicating exclusive or non-exclusive mode.
1251 * mode - mode to open file with.
1252 * cr - credentials of caller.
1253 * flag - large file flag [UNUSED].
1254 * vsecp - ACL to be set
1256 * OUT: ipp - inode of created or trunc'd entry.
1258 * RETURN: 0 on success, error code on failure.
1261 * dip - ctime|mtime updated if new entry created
1262 * ip - ctime|mtime always, atime if new
1267 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1268 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1270 znode_t *zp, *dzp = ITOZ(dip);
1271 zfs_sb_t *zsb = ITOZSB(dip);
1279 zfs_acl_ids_t acl_ids;
1280 boolean_t fuid_dirtied;
1281 boolean_t have_acl = B_FALSE;
1284 * If we have an ephemeral id, ACL, or XVATTR then
1285 * make sure file system is at proper version
1291 if (zsb->z_use_fuids == B_FALSE &&
1292 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1293 return (SET_ERROR(EINVAL));
1300 if (zsb->z_utf8 && u8_validate(name, strlen(name),
1301 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1303 return (SET_ERROR(EILSEQ));
1306 if (vap->va_mask & ATTR_XVATTR) {
1307 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1308 crgetuid(cr), cr, vap->va_mode)) != 0) {
1316 if (*name == '\0') {
1318 * Null component name refers to the directory itself.
1325 /* possible igrab(zp) */
1328 if (flag & FIGNORECASE)
1331 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1335 zfs_acl_ids_free(&acl_ids);
1336 if (strcmp(name, "..") == 0)
1337 error = SET_ERROR(EISDIR);
1347 * Create a new file object and update the directory
1350 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1352 zfs_acl_ids_free(&acl_ids);
1357 * We only support the creation of regular files in
1358 * extended attribute directories.
1361 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1363 zfs_acl_ids_free(&acl_ids);
1364 error = SET_ERROR(EINVAL);
1368 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1369 cr, vsecp, &acl_ids)) != 0)
1373 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1374 zfs_acl_ids_free(&acl_ids);
1375 error = SET_ERROR(EDQUOT);
1379 tx = dmu_tx_create(os);
1381 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1382 ZFS_SA_BASE_ATTR_SIZE);
1384 fuid_dirtied = zsb->z_fuid_dirty;
1386 zfs_fuid_txhold(zsb, tx);
1387 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1388 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1389 if (!zsb->z_use_sa &&
1390 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1391 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1392 0, acl_ids.z_aclp->z_acl_bytes);
1394 error = dmu_tx_assign(tx, TXG_NOWAIT);
1396 zfs_dirent_unlock(dl);
1397 if (error == ERESTART) {
1402 zfs_acl_ids_free(&acl_ids);
1407 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1410 zfs_fuid_sync(zsb, tx);
1412 (void) zfs_link_create(dl, zp, tx, ZNEW);
1413 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1414 if (flag & FIGNORECASE)
1416 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1417 vsecp, acl_ids.z_fuidp, vap);
1418 zfs_acl_ids_free(&acl_ids);
1421 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1424 zfs_acl_ids_free(&acl_ids);
1428 * A directory entry already exists for this name.
1431 * Can't truncate an existing file if in exclusive mode.
1434 error = SET_ERROR(EEXIST);
1438 * Can't open a directory for writing.
1440 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1441 error = SET_ERROR(EISDIR);
1445 * Verify requested access to file.
1447 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1451 mutex_enter(&dzp->z_lock);
1453 mutex_exit(&dzp->z_lock);
1456 * Truncate regular files if requested.
1458 if (S_ISREG(ZTOI(zp)->i_mode) &&
1459 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1460 /* we can't hold any locks when calling zfs_freesp() */
1461 zfs_dirent_unlock(dl);
1463 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1469 zfs_dirent_unlock(dl);
1475 zfs_inode_update(dzp);
1476 zfs_inode_update(zp);
1480 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1481 zil_commit(zilog, 0);
1486 EXPORT_SYMBOL(zfs_create);
1489 * Remove an entry from a directory.
1491 * IN: dip - inode of directory to remove entry from.
1492 * name - name of entry to remove.
1493 * cr - credentials of caller.
1495 * RETURN: 0 if success
1496 * error code if failure
1500 * ip - ctime (if nlink > 0)
1503 uint64_t null_xattr = 0;
1507 zfs_remove(struct inode *dip, char *name, cred_t *cr)
1509 znode_t *zp, *dzp = ITOZ(dip);
1512 zfs_sb_t *zsb = ITOZSB(dip);
1515 uint64_t xattr_obj_unlinked = 0;
1521 pathname_t *realnmp = NULL;
1522 #ifdef HAVE_PN_UTILS
1524 #endif /* HAVE_PN_UTILS */
1532 #ifdef HAVE_PN_UTILS
1533 if (flags & FIGNORECASE) {
1538 #endif /* HAVE_PN_UTILS */
1544 * Attempt to lock directory; fail if entry doesn't exist.
1546 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1548 #ifdef HAVE_PN_UTILS
1551 #endif /* HAVE_PN_UTILS */
1558 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1563 * Need to use rmdir for removing directories.
1565 if (S_ISDIR(ip->i_mode)) {
1566 error = SET_ERROR(EPERM);
1572 dnlc_remove(dvp, realnmp->pn_buf);
1574 dnlc_remove(dvp, name);
1575 #endif /* HAVE_DNLC */
1578 * We never delete the znode and always place it in the unlinked
1579 * set. The dentry cache will always hold the last reference and
1580 * is responsible for safely freeing the znode.
1583 tx = dmu_tx_create(zsb->z_os);
1584 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1585 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1586 zfs_sa_upgrade_txholds(tx, zp);
1587 zfs_sa_upgrade_txholds(tx, dzp);
1589 /* are there any extended attributes? */
1590 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1591 &xattr_obj, sizeof (xattr_obj));
1592 if (error == 0 && xattr_obj) {
1593 error = zfs_zget(zsb, xattr_obj, &xzp);
1595 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1596 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1599 /* charge as an update -- would be nice not to charge at all */
1600 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1602 error = dmu_tx_assign(tx, TXG_NOWAIT);
1604 zfs_dirent_unlock(dl);
1608 if (error == ERESTART) {
1613 #ifdef HAVE_PN_UTILS
1616 #endif /* HAVE_PN_UTILS */
1623 * Remove the directory entry.
1625 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1634 * Hold z_lock so that we can make sure that the ACL obj
1635 * hasn't changed. Could have been deleted due to
1638 mutex_enter(&zp->z_lock);
1639 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1640 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1641 mutex_exit(&zp->z_lock);
1642 zfs_unlinked_add(zp, tx);
1646 #ifdef HAVE_PN_UTILS
1647 if (flags & FIGNORECASE)
1649 #endif /* HAVE_PN_UTILS */
1650 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1654 #ifdef HAVE_PN_UTILS
1657 #endif /* HAVE_PN_UTILS */
1659 zfs_dirent_unlock(dl);
1660 zfs_inode_update(dzp);
1661 zfs_inode_update(zp);
1663 zfs_inode_update(xzp);
1669 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1670 zil_commit(zilog, 0);
1675 EXPORT_SYMBOL(zfs_remove);
1678 * Create a new directory and insert it into dip using the name
1679 * provided. Return a pointer to the inserted directory.
1681 * IN: dip - inode of directory to add subdir to.
1682 * dirname - name of new directory.
1683 * vap - attributes of new directory.
1684 * cr - credentials of caller.
1685 * vsecp - ACL to be set
1687 * OUT: ipp - inode of created directory.
1689 * RETURN: 0 if success
1690 * error code if failure
1693 * dip - ctime|mtime updated
1694 * ipp - ctime|mtime|atime updated
1698 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1699 cred_t *cr, int flags, vsecattr_t *vsecp)
1701 znode_t *zp, *dzp = ITOZ(dip);
1702 zfs_sb_t *zsb = ITOZSB(dip);
1710 gid_t gid = crgetgid(cr);
1711 zfs_acl_ids_t acl_ids;
1712 boolean_t fuid_dirtied;
1714 ASSERT(S_ISDIR(vap->va_mode));
1717 * If we have an ephemeral id, ACL, or XVATTR then
1718 * make sure file system is at proper version
1722 if (zsb->z_use_fuids == B_FALSE &&
1723 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1724 return (SET_ERROR(EINVAL));
1730 if (dzp->z_pflags & ZFS_XATTR) {
1732 return (SET_ERROR(EINVAL));
1735 if (zsb->z_utf8 && u8_validate(dirname,
1736 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1738 return (SET_ERROR(EILSEQ));
1740 if (flags & FIGNORECASE)
1743 if (vap->va_mask & ATTR_XVATTR) {
1744 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1745 crgetuid(cr), cr, vap->va_mode)) != 0) {
1751 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1752 vsecp, &acl_ids)) != 0) {
1757 * First make sure the new directory doesn't exist.
1759 * Existence is checked first to make sure we don't return
1760 * EACCES instead of EEXIST which can cause some applications
1766 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1768 zfs_acl_ids_free(&acl_ids);
1773 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1774 zfs_acl_ids_free(&acl_ids);
1775 zfs_dirent_unlock(dl);
1780 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1781 zfs_acl_ids_free(&acl_ids);
1782 zfs_dirent_unlock(dl);
1784 return (SET_ERROR(EDQUOT));
1788 * Add a new entry to the directory.
1790 tx = dmu_tx_create(zsb->z_os);
1791 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1792 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1793 fuid_dirtied = zsb->z_fuid_dirty;
1795 zfs_fuid_txhold(zsb, tx);
1796 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1797 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1798 acl_ids.z_aclp->z_acl_bytes);
1801 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1802 ZFS_SA_BASE_ATTR_SIZE);
1804 error = dmu_tx_assign(tx, TXG_NOWAIT);
1806 zfs_dirent_unlock(dl);
1807 if (error == ERESTART) {
1812 zfs_acl_ids_free(&acl_ids);
1821 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1824 zfs_fuid_sync(zsb, tx);
1827 * Now put new name in parent dir.
1829 (void) zfs_link_create(dl, zp, tx, ZNEW);
1833 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1834 if (flags & FIGNORECASE)
1836 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1837 acl_ids.z_fuidp, vap);
1839 zfs_acl_ids_free(&acl_ids);
1843 zfs_dirent_unlock(dl);
1845 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1846 zil_commit(zilog, 0);
1848 zfs_inode_update(dzp);
1849 zfs_inode_update(zp);
1853 EXPORT_SYMBOL(zfs_mkdir);
1856 * Remove a directory subdir entry. If the current working
1857 * directory is the same as the subdir to be removed, the
1860 * IN: dip - inode of directory to remove from.
1861 * name - name of directory to be removed.
1862 * cwd - inode of current working directory.
1863 * cr - credentials of caller.
1864 * flags - case flags
1866 * RETURN: 0 on success, error code on failure.
1869 * dip - ctime|mtime updated
1873 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1876 znode_t *dzp = ITOZ(dip);
1879 zfs_sb_t *zsb = ITOZSB(dip);
1890 if (flags & FIGNORECASE)
1896 * Attempt to lock directory; fail if entry doesn't exist.
1898 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1906 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1910 if (!S_ISDIR(ip->i_mode)) {
1911 error = SET_ERROR(ENOTDIR);
1916 error = SET_ERROR(EINVAL);
1921 * Grab a lock on the directory to make sure that noone is
1922 * trying to add (or lookup) entries while we are removing it.
1924 rw_enter(&zp->z_name_lock, RW_WRITER);
1927 * Grab a lock on the parent pointer to make sure we play well
1928 * with the treewalk and directory rename code.
1930 rw_enter(&zp->z_parent_lock, RW_WRITER);
1932 tx = dmu_tx_create(zsb->z_os);
1933 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1934 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1935 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1936 zfs_sa_upgrade_txholds(tx, zp);
1937 zfs_sa_upgrade_txholds(tx, dzp);
1938 error = dmu_tx_assign(tx, TXG_NOWAIT);
1940 rw_exit(&zp->z_parent_lock);
1941 rw_exit(&zp->z_name_lock);
1942 zfs_dirent_unlock(dl);
1944 if (error == ERESTART) {
1954 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1957 uint64_t txtype = TX_RMDIR;
1958 if (flags & FIGNORECASE)
1960 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
1965 rw_exit(&zp->z_parent_lock);
1966 rw_exit(&zp->z_name_lock);
1968 zfs_dirent_unlock(dl);
1970 zfs_inode_update(dzp);
1971 zfs_inode_update(zp);
1974 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1975 zil_commit(zilog, 0);
1980 EXPORT_SYMBOL(zfs_rmdir);
1983 * Read as many directory entries as will fit into the provided
1984 * dirent buffer from the given directory cursor position.
1986 * IN: ip - inode of directory to read.
1987 * dirent - buffer for directory entries.
1989 * OUT: dirent - filler buffer of directory entries.
1991 * RETURN: 0 if success
1992 * error code if failure
1995 * ip - atime updated
1997 * Note that the low 4 bits of the cookie returned by zap is always zero.
1998 * This allows us to use the low range for "special" directory entries:
1999 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2000 * we use the offset 2 for the '.zfs' directory.
2004 zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
2006 znode_t *zp = ITOZ(ip);
2007 zfs_sb_t *zsb = ITOZSB(ip);
2010 zap_attribute_t zap;
2016 uint64_t offset; /* must be unsigned; checks for < 1 */
2021 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
2022 &parent, sizeof (parent))) != 0)
2026 * Quit if directory has been removed (posix)
2034 prefetch = zp->z_zn_prefetch;
2037 * Initialize the iterator cursor.
2041 * Start iteration from the beginning of the directory.
2043 zap_cursor_init(&zc, os, zp->z_id);
2046 * The offset is a serialized cursor.
2048 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2052 * Transform to file-system independent format
2057 * Special case `.', `..', and `.zfs'.
2060 (void) strcpy(zap.za_name, ".");
2061 zap.za_normalization_conflict = 0;
2064 } else if (offset == 1) {
2065 (void) strcpy(zap.za_name, "..");
2066 zap.za_normalization_conflict = 0;
2069 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2070 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2071 zap.za_normalization_conflict = 0;
2072 objnum = ZFSCTL_INO_ROOT;
2078 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2079 if (error == ENOENT)
2086 * Allow multiple entries provided the first entry is
2087 * the object id. Non-zpl consumers may safely make
2088 * use of the additional space.
2090 * XXX: This should be a feature flag for compatibility
2092 if (zap.za_integer_length != 8 ||
2093 zap.za_num_integers == 0) {
2094 cmn_err(CE_WARN, "zap_readdir: bad directory "
2095 "entry, obj = %lld, offset = %lld, "
2096 "length = %d, num = %lld\n",
2097 (u_longlong_t)zp->z_id,
2098 (u_longlong_t)offset,
2099 zap.za_integer_length,
2100 (u_longlong_t)zap.za_num_integers);
2101 error = SET_ERROR(ENXIO);
2105 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2106 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2109 done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name),
2114 /* Prefetch znode */
2116 dmu_prefetch(os, objnum, 0, 0);
2120 * Move to the next entry, fill in the previous offset.
2122 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2123 zap_cursor_advance(&zc);
2124 offset = zap_cursor_serialize(&zc);
2130 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2133 zap_cursor_fini(&zc);
2134 if (error == ENOENT)
2137 ZFS_ACCESSTIME_STAMP(zsb, zp);
2138 zfs_inode_update(zp);
2145 EXPORT_SYMBOL(zfs_readdir);
2147 ulong_t zfs_fsync_sync_cnt = 4;
2150 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2152 znode_t *zp = ITOZ(ip);
2153 zfs_sb_t *zsb = ITOZSB(ip);
2155 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2157 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2160 zil_commit(zsb->z_log, zp->z_id);
2165 EXPORT_SYMBOL(zfs_fsync);
2169 * Get the requested file attributes and place them in the provided
2172 * IN: ip - inode of file.
2173 * vap - va_mask identifies requested attributes.
2174 * If ATTR_XVATTR set, then optional attrs are requested
2175 * flags - ATTR_NOACLCHECK (CIFS server context)
2176 * cr - credentials of caller.
2178 * OUT: vap - attribute values.
2180 * RETURN: 0 (always succeeds)
2184 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2186 znode_t *zp = ITOZ(ip);
2187 zfs_sb_t *zsb = ITOZSB(ip);
2190 uint64_t mtime[2], ctime[2];
2191 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2192 xoptattr_t *xoap = NULL;
2193 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2194 sa_bulk_attr_t bulk[2];
2200 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2202 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2203 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2205 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2211 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2212 * Also, if we are the owner don't bother, since owner should
2213 * always be allowed to read basic attributes of file.
2215 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2216 (vap->va_uid != crgetuid(cr))) {
2217 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2225 * Return all attributes. It's cheaper to provide the answer
2226 * than to determine whether we were asked the question.
2229 mutex_enter(&zp->z_lock);
2230 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2231 vap->va_mode = zp->z_mode;
2232 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2233 vap->va_nodeid = zp->z_id;
2234 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2235 links = zp->z_links + 1;
2237 links = zp->z_links;
2238 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2239 vap->va_size = i_size_read(ip);
2240 vap->va_rdev = ip->i_rdev;
2241 vap->va_seq = ip->i_generation;
2244 * Add in any requested optional attributes and the create time.
2245 * Also set the corresponding bits in the returned attribute bitmap.
2247 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2248 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2250 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2251 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2254 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2255 xoap->xoa_readonly =
2256 ((zp->z_pflags & ZFS_READONLY) != 0);
2257 XVA_SET_RTN(xvap, XAT_READONLY);
2260 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2262 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2263 XVA_SET_RTN(xvap, XAT_SYSTEM);
2266 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2268 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2269 XVA_SET_RTN(xvap, XAT_HIDDEN);
2272 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2273 xoap->xoa_nounlink =
2274 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2275 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2278 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2279 xoap->xoa_immutable =
2280 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2281 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2284 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2285 xoap->xoa_appendonly =
2286 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2287 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2290 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2292 ((zp->z_pflags & ZFS_NODUMP) != 0);
2293 XVA_SET_RTN(xvap, XAT_NODUMP);
2296 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2298 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2299 XVA_SET_RTN(xvap, XAT_OPAQUE);
2302 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2303 xoap->xoa_av_quarantined =
2304 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2305 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2308 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2309 xoap->xoa_av_modified =
2310 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2311 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2314 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2315 S_ISREG(ip->i_mode)) {
2316 zfs_sa_get_scanstamp(zp, xvap);
2319 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2322 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2323 times, sizeof (times));
2324 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2325 XVA_SET_RTN(xvap, XAT_CREATETIME);
2328 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2329 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2330 XVA_SET_RTN(xvap, XAT_REPARSE);
2332 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2333 xoap->xoa_generation = zp->z_gen;
2334 XVA_SET_RTN(xvap, XAT_GEN);
2337 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2339 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2340 XVA_SET_RTN(xvap, XAT_OFFLINE);
2343 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2345 ((zp->z_pflags & ZFS_SPARSE) != 0);
2346 XVA_SET_RTN(xvap, XAT_SPARSE);
2350 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2351 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2352 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2354 mutex_exit(&zp->z_lock);
2356 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2358 if (zp->z_blksz == 0) {
2360 * Block size hasn't been set; suggest maximal I/O transfers.
2362 vap->va_blksize = zsb->z_max_blksz;
2368 EXPORT_SYMBOL(zfs_getattr);
2371 * Get the basic file attributes and place them in the provided kstat
2372 * structure. The inode is assumed to be the authoritative source
2373 * for most of the attributes. However, the znode currently has the
2374 * authoritative atime, blksize, and block count.
2376 * IN: ip - inode of file.
2378 * OUT: sp - kstat values.
2380 * RETURN: 0 (always succeeds)
2384 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2386 znode_t *zp = ITOZ(ip);
2387 zfs_sb_t *zsb = ITOZSB(ip);
2392 mutex_enter(&zp->z_lock);
2394 generic_fillattr(ip, sp);
2395 ZFS_TIME_DECODE(&sp->atime, zp->z_atime);
2397 sa_object_size(zp->z_sa_hdl, (uint32_t *)&sp->blksize, &sp->blocks);
2398 if (unlikely(zp->z_blksz == 0)) {
2400 * Block size hasn't been set; suggest maximal I/O transfers.
2402 sp->blksize = zsb->z_max_blksz;
2405 mutex_exit(&zp->z_lock);
2411 EXPORT_SYMBOL(zfs_getattr_fast);
2414 * Set the file attributes to the values contained in the
2417 * IN: ip - inode of file to be modified.
2418 * vap - new attribute values.
2419 * If ATTR_XVATTR set, then optional attrs are being set
2420 * flags - ATTR_UTIME set if non-default time values provided.
2421 * - ATTR_NOACLCHECK (CIFS context only).
2422 * cr - credentials of caller.
2424 * RETURN: 0 if success
2425 * error code if failure
2428 * ip - ctime updated, mtime updated if size changed.
2432 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2434 znode_t *zp = ITOZ(ip);
2435 zfs_sb_t *zsb = ITOZSB(ip);
2439 xvattr_t *tmpxvattr;
2440 uint_t mask = vap->va_mask;
2441 uint_t saved_mask = 0;
2444 uint64_t new_uid, new_gid;
2446 uint64_t mtime[2], ctime[2];
2448 int need_policy = FALSE;
2450 zfs_fuid_info_t *fuidp = NULL;
2451 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2454 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2455 boolean_t fuid_dirtied = B_FALSE;
2456 sa_bulk_attr_t *bulk, *xattr_bulk;
2457 int count = 0, xattr_count = 0;
2468 * Make sure that if we have ephemeral uid/gid or xvattr specified
2469 * that file system is at proper version level
2472 if (zsb->z_use_fuids == B_FALSE &&
2473 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2474 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2475 (mask & ATTR_XVATTR))) {
2477 return (SET_ERROR(EINVAL));
2480 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2482 return (SET_ERROR(EISDIR));
2485 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2487 return (SET_ERROR(EINVAL));
2491 * If this is an xvattr_t, then get a pointer to the structure of
2492 * optional attributes. If this is NULL, then we have a vattr_t.
2494 xoap = xva_getxoptattr(xvap);
2496 tmpxvattr = kmem_alloc(sizeof(xvattr_t), KM_SLEEP);
2497 xva_init(tmpxvattr);
2499 bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2500 xattr_bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2503 * Immutable files can only alter immutable bit and atime
2505 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2506 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2507 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2512 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2518 * Verify timestamps doesn't overflow 32 bits.
2519 * ZFS can handle large timestamps, but 32bit syscalls can't
2520 * handle times greater than 2039. This check should be removed
2521 * once large timestamps are fully supported.
2523 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2524 if (((mask & ATTR_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2525 ((mask & ATTR_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2535 /* Can this be moved to before the top label? */
2536 if (zfs_is_readonly(zsb)) {
2542 * First validate permissions
2545 if (mask & ATTR_SIZE) {
2546 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2550 truncate_setsize(ip, vap->va_size);
2553 * XXX - Note, we are not providing any open
2554 * mode flags here (like FNDELAY), so we may
2555 * block if there are locks present... this
2556 * should be addressed in openat().
2558 /* XXX - would it be OK to generate a log record here? */
2559 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2564 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2565 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2566 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2567 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2568 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2569 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2570 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2571 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2572 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2576 if (mask & (ATTR_UID|ATTR_GID)) {
2577 int idmask = (mask & (ATTR_UID|ATTR_GID));
2582 * NOTE: even if a new mode is being set,
2583 * we may clear S_ISUID/S_ISGID bits.
2586 if (!(mask & ATTR_MODE))
2587 vap->va_mode = zp->z_mode;
2590 * Take ownership or chgrp to group we are a member of
2593 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2594 take_group = (mask & ATTR_GID) &&
2595 zfs_groupmember(zsb, vap->va_gid, cr);
2598 * If both ATTR_UID and ATTR_GID are set then take_owner and
2599 * take_group must both be set in order to allow taking
2602 * Otherwise, send the check through secpolicy_vnode_setattr()
2606 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2607 take_owner && take_group) ||
2608 ((idmask == ATTR_UID) && take_owner) ||
2609 ((idmask == ATTR_GID) && take_group)) {
2610 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2611 skipaclchk, cr) == 0) {
2613 * Remove setuid/setgid for non-privileged users
2615 (void) secpolicy_setid_clear(vap, cr);
2616 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2625 mutex_enter(&zp->z_lock);
2626 oldva.va_mode = zp->z_mode;
2627 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2628 if (mask & ATTR_XVATTR) {
2630 * Update xvattr mask to include only those attributes
2631 * that are actually changing.
2633 * the bits will be restored prior to actually setting
2634 * the attributes so the caller thinks they were set.
2636 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2637 if (xoap->xoa_appendonly !=
2638 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2641 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2642 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2646 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2647 if (xoap->xoa_nounlink !=
2648 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2651 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2652 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2656 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2657 if (xoap->xoa_immutable !=
2658 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2661 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2662 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2666 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2667 if (xoap->xoa_nodump !=
2668 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2671 XVA_CLR_REQ(xvap, XAT_NODUMP);
2672 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2676 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2677 if (xoap->xoa_av_modified !=
2678 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2681 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2682 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2686 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2687 if ((!S_ISREG(ip->i_mode) &&
2688 xoap->xoa_av_quarantined) ||
2689 xoap->xoa_av_quarantined !=
2690 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2693 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2694 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2698 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2699 mutex_exit(&zp->z_lock);
2704 if (need_policy == FALSE &&
2705 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2706 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2711 mutex_exit(&zp->z_lock);
2713 if (mask & ATTR_MODE) {
2714 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2715 err = secpolicy_setid_setsticky_clear(ip, vap,
2720 trim_mask |= ATTR_MODE;
2728 * If trim_mask is set then take ownership
2729 * has been granted or write_acl is present and user
2730 * has the ability to modify mode. In that case remove
2731 * UID|GID and or MODE from mask so that
2732 * secpolicy_vnode_setattr() doesn't revoke it.
2736 saved_mask = vap->va_mask;
2737 vap->va_mask &= ~trim_mask;
2739 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2740 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2745 vap->va_mask |= saved_mask;
2749 * secpolicy_vnode_setattr, or take ownership may have
2752 mask = vap->va_mask;
2754 if ((mask & (ATTR_UID | ATTR_GID))) {
2755 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2756 &xattr_obj, sizeof (xattr_obj));
2758 if (err == 0 && xattr_obj) {
2759 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2763 if (mask & ATTR_UID) {
2764 new_uid = zfs_fuid_create(zsb,
2765 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2766 if (new_uid != zp->z_uid &&
2767 zfs_fuid_overquota(zsb, B_FALSE, new_uid)) {
2775 if (mask & ATTR_GID) {
2776 new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2777 cr, ZFS_GROUP, &fuidp);
2778 if (new_gid != zp->z_gid &&
2779 zfs_fuid_overquota(zsb, B_TRUE, new_gid)) {
2787 tx = dmu_tx_create(zsb->z_os);
2789 if (mask & ATTR_MODE) {
2790 uint64_t pmode = zp->z_mode;
2792 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2794 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2796 mutex_enter(&zp->z_lock);
2797 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2799 * Are we upgrading ACL from old V0 format
2802 if (zsb->z_version >= ZPL_VERSION_FUID &&
2803 zfs_znode_acl_version(zp) ==
2804 ZFS_ACL_VERSION_INITIAL) {
2805 dmu_tx_hold_free(tx, acl_obj, 0,
2807 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2808 0, aclp->z_acl_bytes);
2810 dmu_tx_hold_write(tx, acl_obj, 0,
2813 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2814 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2815 0, aclp->z_acl_bytes);
2817 mutex_exit(&zp->z_lock);
2818 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2820 if ((mask & ATTR_XVATTR) &&
2821 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2822 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2824 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2828 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2831 fuid_dirtied = zsb->z_fuid_dirty;
2833 zfs_fuid_txhold(zsb, tx);
2835 zfs_sa_upgrade_txholds(tx, zp);
2837 err = dmu_tx_assign(tx, TXG_WAIT);
2843 * Set each attribute requested.
2844 * We group settings according to the locks they need to acquire.
2846 * Note: you cannot set ctime directly, although it will be
2847 * updated as a side-effect of calling this function.
2851 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2852 mutex_enter(&zp->z_acl_lock);
2853 mutex_enter(&zp->z_lock);
2855 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2856 &zp->z_pflags, sizeof (zp->z_pflags));
2859 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2860 mutex_enter(&attrzp->z_acl_lock);
2861 mutex_enter(&attrzp->z_lock);
2862 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2863 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2864 sizeof (attrzp->z_pflags));
2867 if (mask & (ATTR_UID|ATTR_GID)) {
2869 if (mask & ATTR_UID) {
2870 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2871 &new_uid, sizeof (new_uid));
2872 zp->z_uid = new_uid;
2874 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2875 SA_ZPL_UID(zsb), NULL, &new_uid,
2877 attrzp->z_uid = new_uid;
2881 if (mask & ATTR_GID) {
2882 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2883 NULL, &new_gid, sizeof (new_gid));
2884 zp->z_gid = new_gid;
2886 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2887 SA_ZPL_GID(zsb), NULL, &new_gid,
2889 attrzp->z_gid = new_gid;
2892 if (!(mask & ATTR_MODE)) {
2893 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2894 NULL, &new_mode, sizeof (new_mode));
2895 new_mode = zp->z_mode;
2897 err = zfs_acl_chown_setattr(zp);
2900 err = zfs_acl_chown_setattr(attrzp);
2905 if (mask & ATTR_MODE) {
2906 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
2907 &new_mode, sizeof (new_mode));
2908 zp->z_mode = new_mode;
2909 ASSERT3P(aclp, !=, NULL);
2910 err = zfs_aclset_common(zp, aclp, cr, tx);
2912 if (zp->z_acl_cached)
2913 zfs_acl_free(zp->z_acl_cached);
2914 zp->z_acl_cached = aclp;
2919 if (mask & ATTR_ATIME) {
2920 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2921 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
2922 &zp->z_atime, sizeof (zp->z_atime));
2925 if (mask & ATTR_MTIME) {
2926 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2927 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
2928 mtime, sizeof (mtime));
2931 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2932 if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) {
2933 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
2934 NULL, mtime, sizeof (mtime));
2935 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2936 &ctime, sizeof (ctime));
2937 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
2939 } else if (mask != 0) {
2940 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2941 &ctime, sizeof (ctime));
2942 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
2945 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2946 SA_ZPL_CTIME(zsb), NULL,
2947 &ctime, sizeof (ctime));
2948 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2949 mtime, ctime, B_TRUE);
2953 * Do this after setting timestamps to prevent timestamp
2954 * update from toggling bit
2957 if (xoap && (mask & ATTR_XVATTR)) {
2960 * restore trimmed off masks
2961 * so that return masks can be set for caller.
2964 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
2965 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2967 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
2968 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2970 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
2971 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2973 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
2974 XVA_SET_REQ(xvap, XAT_NODUMP);
2976 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
2977 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2979 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
2980 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2983 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2984 ASSERT(S_ISREG(ip->i_mode));
2986 zfs_xvattr_set(zp, xvap, tx);
2990 zfs_fuid_sync(zsb, tx);
2993 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2995 mutex_exit(&zp->z_lock);
2996 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2997 mutex_exit(&zp->z_acl_lock);
3000 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3001 mutex_exit(&attrzp->z_acl_lock);
3002 mutex_exit(&attrzp->z_lock);
3005 if (err == 0 && attrzp) {
3006 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3017 zfs_fuid_info_free(fuidp);
3023 if (err == ERESTART)
3026 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3028 zfs_inode_update(zp);
3032 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3033 zil_commit(zilog, 0);
3036 kmem_free(xattr_bulk, sizeof(sa_bulk_attr_t) * 7);
3037 kmem_free(bulk, sizeof(sa_bulk_attr_t) * 7);
3038 kmem_free(tmpxvattr, sizeof(xvattr_t));
3042 EXPORT_SYMBOL(zfs_setattr);
3044 typedef struct zfs_zlock {
3045 krwlock_t *zl_rwlock; /* lock we acquired */
3046 znode_t *zl_znode; /* znode we held */
3047 struct zfs_zlock *zl_next; /* next in list */
3051 * Drop locks and release vnodes that were held by zfs_rename_lock().
3054 zfs_rename_unlock(zfs_zlock_t **zlpp)
3058 while ((zl = *zlpp) != NULL) {
3059 if (zl->zl_znode != NULL)
3060 iput(ZTOI(zl->zl_znode));
3061 rw_exit(zl->zl_rwlock);
3062 *zlpp = zl->zl_next;
3063 kmem_free(zl, sizeof (*zl));
3068 * Search back through the directory tree, using the ".." entries.
3069 * Lock each directory in the chain to prevent concurrent renames.
3070 * Fail any attempt to move a directory into one of its own descendants.
3071 * XXX - z_parent_lock can overlap with map or grow locks
3074 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3078 uint64_t rootid = ZTOZSB(zp)->z_root;
3079 uint64_t oidp = zp->z_id;
3080 krwlock_t *rwlp = &szp->z_parent_lock;
3081 krw_t rw = RW_WRITER;
3084 * First pass write-locks szp and compares to zp->z_id.
3085 * Later passes read-lock zp and compare to zp->z_parent.
3088 if (!rw_tryenter(rwlp, rw)) {
3090 * Another thread is renaming in this path.
3091 * Note that if we are a WRITER, we don't have any
3092 * parent_locks held yet.
3094 if (rw == RW_READER && zp->z_id > szp->z_id) {
3096 * Drop our locks and restart
3098 zfs_rename_unlock(&zl);
3102 rwlp = &szp->z_parent_lock;
3107 * Wait for other thread to drop its locks
3113 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3114 zl->zl_rwlock = rwlp;
3115 zl->zl_znode = NULL;
3116 zl->zl_next = *zlpp;
3119 if (oidp == szp->z_id) /* We're a descendant of szp */
3120 return (SET_ERROR(EINVAL));
3122 if (oidp == rootid) /* We've hit the top */
3125 if (rw == RW_READER) { /* i.e. not the first pass */
3126 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3131 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3132 &oidp, sizeof (oidp));
3133 rwlp = &zp->z_parent_lock;
3136 } while (zp->z_id != sdzp->z_id);
3142 * Move an entry from the provided source directory to the target
3143 * directory. Change the entry name as indicated.
3145 * IN: sdip - Source directory containing the "old entry".
3146 * snm - Old entry name.
3147 * tdip - Target directory to contain the "new entry".
3148 * tnm - New entry name.
3149 * cr - credentials of caller.
3150 * flags - case flags
3152 * RETURN: 0 on success, error code on failure.
3155 * sdip,tdip - ctime|mtime updated
3159 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3160 cred_t *cr, int flags)
3162 znode_t *tdzp, *szp, *tzp;
3163 znode_t *sdzp = ITOZ(sdip);
3164 zfs_sb_t *zsb = ITOZSB(sdip);
3166 zfs_dirlock_t *sdl, *tdl;
3169 int cmp, serr, terr;
3174 ZFS_VERIFY_ZP(sdzp);
3177 if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) {
3179 return (SET_ERROR(EXDEV));
3183 ZFS_VERIFY_ZP(tdzp);
3184 if (zsb->z_utf8 && u8_validate(tnm,
3185 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3187 return (SET_ERROR(EILSEQ));
3190 if (flags & FIGNORECASE)
3199 * This is to prevent the creation of links into attribute space
3200 * by renaming a linked file into/outof an attribute directory.
3201 * See the comment in zfs_link() for why this is considered bad.
3203 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3205 return (SET_ERROR(EINVAL));
3209 * Lock source and target directory entries. To prevent deadlock,
3210 * a lock ordering must be defined. We lock the directory with
3211 * the smallest object id first, or if it's a tie, the one with
3212 * the lexically first name.
3214 if (sdzp->z_id < tdzp->z_id) {
3216 } else if (sdzp->z_id > tdzp->z_id) {
3220 * First compare the two name arguments without
3221 * considering any case folding.
3223 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3225 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3226 ASSERT(error == 0 || !zsb->z_utf8);
3229 * POSIX: "If the old argument and the new argument
3230 * both refer to links to the same existing file,
3231 * the rename() function shall return successfully
3232 * and perform no other action."
3238 * If the file system is case-folding, then we may
3239 * have some more checking to do. A case-folding file
3240 * system is either supporting mixed case sensitivity
3241 * access or is completely case-insensitive. Note
3242 * that the file system is always case preserving.
3244 * In mixed sensitivity mode case sensitive behavior
3245 * is the default. FIGNORECASE must be used to
3246 * explicitly request case insensitive behavior.
3248 * If the source and target names provided differ only
3249 * by case (e.g., a request to rename 'tim' to 'Tim'),
3250 * we will treat this as a special case in the
3251 * case-insensitive mode: as long as the source name
3252 * is an exact match, we will allow this to proceed as
3253 * a name-change request.
3255 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3256 (zsb->z_case == ZFS_CASE_MIXED &&
3257 flags & FIGNORECASE)) &&
3258 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3261 * case preserving rename request, require exact
3270 * If the source and destination directories are the same, we should
3271 * grab the z_name_lock of that directory only once.
3275 rw_enter(&sdzp->z_name_lock, RW_READER);
3279 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3280 ZEXISTS | zflg, NULL, NULL);
3281 terr = zfs_dirent_lock(&tdl,
3282 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3284 terr = zfs_dirent_lock(&tdl,
3285 tdzp, tnm, &tzp, zflg, NULL, NULL);
3286 serr = zfs_dirent_lock(&sdl,
3287 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3293 * Source entry invalid or not there.
3296 zfs_dirent_unlock(tdl);
3302 rw_exit(&sdzp->z_name_lock);
3304 if (strcmp(snm, "..") == 0)
3310 zfs_dirent_unlock(sdl);
3314 rw_exit(&sdzp->z_name_lock);
3316 if (strcmp(tnm, "..") == 0)
3323 * Must have write access at the source to remove the old entry
3324 * and write access at the target to create the new entry.
3325 * Note that if target and source are the same, this can be
3326 * done in a single check.
3329 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3332 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3334 * Check to make sure rename is valid.
3335 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3337 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3342 * Does target exist?
3346 * Source and target must be the same type.
3348 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3349 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3350 error = SET_ERROR(ENOTDIR);
3354 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3355 error = SET_ERROR(EISDIR);
3360 * POSIX dictates that when the source and target
3361 * entries refer to the same file object, rename
3362 * must do nothing and exit without error.
3364 if (szp->z_id == tzp->z_id) {
3370 tx = dmu_tx_create(zsb->z_os);
3371 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3372 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3373 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3374 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3376 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3377 zfs_sa_upgrade_txholds(tx, tdzp);
3380 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3381 zfs_sa_upgrade_txholds(tx, tzp);
3384 zfs_sa_upgrade_txholds(tx, szp);
3385 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3386 error = dmu_tx_assign(tx, TXG_NOWAIT);
3389 zfs_rename_unlock(&zl);
3390 zfs_dirent_unlock(sdl);
3391 zfs_dirent_unlock(tdl);
3394 rw_exit(&sdzp->z_name_lock);
3399 if (error == ERESTART) {
3409 if (tzp) /* Attempt to remove the existing target */
3410 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3413 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3415 szp->z_pflags |= ZFS_AV_MODIFIED;
3417 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3418 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3421 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3423 zfs_log_rename(zilog, tx, TX_RENAME |
3424 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3425 sdl->dl_name, tdzp, tdl->dl_name, szp);
3428 * At this point, we have successfully created
3429 * the target name, but have failed to remove
3430 * the source name. Since the create was done
3431 * with the ZRENAMING flag, there are
3432 * complications; for one, the link count is
3433 * wrong. The easiest way to deal with this
3434 * is to remove the newly created target, and
3435 * return the original error. This must
3436 * succeed; fortunately, it is very unlikely to
3437 * fail, since we just created it.
3439 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3440 ZRENAMING, NULL), ==, 0);
3448 zfs_rename_unlock(&zl);
3450 zfs_dirent_unlock(sdl);
3451 zfs_dirent_unlock(tdl);
3453 zfs_inode_update(sdzp);
3455 rw_exit(&sdzp->z_name_lock);
3458 zfs_inode_update(tdzp);
3460 zfs_inode_update(szp);
3463 zfs_inode_update(tzp);
3467 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3468 zil_commit(zilog, 0);
3473 EXPORT_SYMBOL(zfs_rename);
3476 * Insert the indicated symbolic reference entry into the directory.
3478 * IN: dip - Directory to contain new symbolic link.
3479 * link - Name for new symlink entry.
3480 * vap - Attributes of new entry.
3481 * target - Target path of new symlink.
3483 * cr - credentials of caller.
3484 * flags - case flags
3486 * RETURN: 0 on success, error code on failure.
3489 * dip - ctime|mtime updated
3493 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3494 struct inode **ipp, cred_t *cr, int flags)
3496 znode_t *zp, *dzp = ITOZ(dip);
3499 zfs_sb_t *zsb = ITOZSB(dip);
3501 uint64_t len = strlen(link);
3504 zfs_acl_ids_t acl_ids;
3505 boolean_t fuid_dirtied;
3506 uint64_t txtype = TX_SYMLINK;
3508 ASSERT(S_ISLNK(vap->va_mode));
3514 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3515 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3517 return (SET_ERROR(EILSEQ));
3519 if (flags & FIGNORECASE)
3522 if (len > MAXPATHLEN) {
3524 return (SET_ERROR(ENAMETOOLONG));
3527 if ((error = zfs_acl_ids_create(dzp, 0,
3528 vap, cr, NULL, &acl_ids)) != 0) {
3536 * Attempt to lock directory; fail if entry already exists.
3538 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3540 zfs_acl_ids_free(&acl_ids);
3545 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3546 zfs_acl_ids_free(&acl_ids);
3547 zfs_dirent_unlock(dl);
3552 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3553 zfs_acl_ids_free(&acl_ids);
3554 zfs_dirent_unlock(dl);
3556 return (SET_ERROR(EDQUOT));
3558 tx = dmu_tx_create(zsb->z_os);
3559 fuid_dirtied = zsb->z_fuid_dirty;
3560 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3561 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3562 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3563 ZFS_SA_BASE_ATTR_SIZE + len);
3564 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3565 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3566 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3567 acl_ids.z_aclp->z_acl_bytes);
3570 zfs_fuid_txhold(zsb, tx);
3571 error = dmu_tx_assign(tx, TXG_NOWAIT);
3573 zfs_dirent_unlock(dl);
3574 if (error == ERESTART) {
3579 zfs_acl_ids_free(&acl_ids);
3586 * Create a new object for the symlink.
3587 * for version 4 ZPL datsets the symlink will be an SA attribute
3589 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3592 zfs_fuid_sync(zsb, tx);
3594 mutex_enter(&zp->z_lock);
3596 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3599 zfs_sa_symlink(zp, link, len, tx);
3600 mutex_exit(&zp->z_lock);
3603 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3604 &zp->z_size, sizeof (zp->z_size), tx);
3606 * Insert the new object into the directory.
3608 (void) zfs_link_create(dl, zp, tx, ZNEW);
3610 if (flags & FIGNORECASE)
3612 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3614 zfs_inode_update(dzp);
3615 zfs_inode_update(zp);
3617 zfs_acl_ids_free(&acl_ids);
3621 zfs_dirent_unlock(dl);
3625 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3626 zil_commit(zilog, 0);
3631 EXPORT_SYMBOL(zfs_symlink);
3634 * Return, in the buffer contained in the provided uio structure,
3635 * the symbolic path referred to by ip.
3637 * IN: ip - inode of symbolic link
3638 * uio - structure to contain the link path.
3639 * cr - credentials of caller.
3641 * RETURN: 0 if success
3642 * error code if failure
3645 * ip - atime updated
3649 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3651 znode_t *zp = ITOZ(ip);
3652 zfs_sb_t *zsb = ITOZSB(ip);
3658 mutex_enter(&zp->z_lock);
3660 error = sa_lookup_uio(zp->z_sa_hdl,
3661 SA_ZPL_SYMLINK(zsb), uio);
3663 error = zfs_sa_readlink(zp, uio);
3664 mutex_exit(&zp->z_lock);
3666 ZFS_ACCESSTIME_STAMP(zsb, zp);
3667 zfs_inode_update(zp);
3671 EXPORT_SYMBOL(zfs_readlink);
3674 * Insert a new entry into directory tdip referencing sip.
3676 * IN: tdip - Directory to contain new entry.
3677 * sip - inode of new entry.
3678 * name - name of new entry.
3679 * cr - credentials of caller.
3681 * RETURN: 0 if success
3682 * error code if failure
3685 * tdip - ctime|mtime updated
3686 * sip - ctime updated
3690 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr)
3692 znode_t *dzp = ITOZ(tdip);
3694 zfs_sb_t *zsb = ITOZSB(tdip);
3703 ASSERT(S_ISDIR(tdip->i_mode));
3710 * POSIX dictates that we return EPERM here.
3711 * Better choices include ENOTSUP or EISDIR.
3713 if (S_ISDIR(sip->i_mode)) {
3715 return (SET_ERROR(EPERM));
3718 if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) {
3720 return (SET_ERROR(EXDEV));
3726 /* Prevent links to .zfs/shares files */
3728 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3729 &parent, sizeof (uint64_t))) != 0) {
3733 if (parent == zsb->z_shares_dir) {
3735 return (SET_ERROR(EPERM));
3738 if (zsb->z_utf8 && u8_validate(name,
3739 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3741 return (SET_ERROR(EILSEQ));
3743 #ifdef HAVE_PN_UTILS
3744 if (flags & FIGNORECASE)
3746 #endif /* HAVE_PN_UTILS */
3749 * We do not support links between attributes and non-attributes
3750 * because of the potential security risk of creating links
3751 * into "normal" file space in order to circumvent restrictions
3752 * imposed in attribute space.
3754 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3756 return (SET_ERROR(EINVAL));
3759 owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER);
3760 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3762 return (SET_ERROR(EPERM));
3765 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3772 * Attempt to lock directory; fail if entry already exists.
3774 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3780 tx = dmu_tx_create(zsb->z_os);
3781 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3782 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3783 zfs_sa_upgrade_txholds(tx, szp);
3784 zfs_sa_upgrade_txholds(tx, dzp);
3785 error = dmu_tx_assign(tx, TXG_NOWAIT);
3787 zfs_dirent_unlock(dl);
3788 if (error == ERESTART) {
3798 error = zfs_link_create(dl, szp, tx, 0);
3801 uint64_t txtype = TX_LINK;
3802 #ifdef HAVE_PN_UTILS
3803 if (flags & FIGNORECASE)
3805 #endif /* HAVE_PN_UTILS */
3806 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3811 zfs_dirent_unlock(dl);
3813 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3814 zil_commit(zilog, 0);
3816 zfs_inode_update(dzp);
3817 zfs_inode_update(szp);
3821 EXPORT_SYMBOL(zfs_link);
3824 zfs_putpage_commit_cb(void *arg)
3826 struct page *pp = arg;
3829 end_page_writeback(pp);
3833 * Push a page out to disk, once the page is on stable storage the
3834 * registered commit callback will be run as notification of completion.
3836 * IN: ip - page mapped for inode.
3837 * pp - page to push (page is locked)
3838 * wbc - writeback control data
3840 * RETURN: 0 if success
3841 * error code if failure
3844 * ip - ctime|mtime updated
3848 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
3850 znode_t *zp = ITOZ(ip);
3851 zfs_sb_t *zsb = ITOZSB(ip);
3859 uint64_t mtime[2], ctime[2];
3860 sa_bulk_attr_t bulk[3];
3866 ASSERT(PageLocked(pp));
3868 pgoff = page_offset(pp); /* Page byte-offset in file */
3869 offset = i_size_read(ip); /* File length in bytes */
3870 pglen = MIN(PAGE_CACHE_SIZE, /* Page length in bytes */
3871 P2ROUNDUP(offset, PAGE_CACHE_SIZE)-pgoff);
3873 /* Page is beyond end of file */
3874 if (pgoff >= offset) {
3880 /* Truncate page length to end of file */
3881 if (pgoff + pglen > offset)
3882 pglen = offset - pgoff;
3886 * FIXME: Allow mmap writes past its quota. The correct fix
3887 * is to register a page_mkwrite() handler to count the page
3888 * against its quota when it is about to be dirtied.
3890 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
3891 zfs_owner_overquota(zsb, zp, B_TRUE)) {
3896 set_page_writeback(pp);
3899 rl = zfs_range_lock(zp, pgoff, pglen, RL_WRITER);
3900 tx = dmu_tx_create(zsb->z_os);
3902 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
3904 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3905 zfs_sa_upgrade_txholds(tx, zp);
3906 err = dmu_tx_assign(tx, TXG_NOWAIT);
3908 if (err == ERESTART)
3912 __set_page_dirty_nobuffers(pp);
3914 end_page_writeback(pp);
3915 zfs_range_unlock(rl);
3921 ASSERT3U(pglen, <=, PAGE_CACHE_SIZE);
3922 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
3925 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3926 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3927 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
3929 /* Preserve the mtime and ctime provided by the inode */
3930 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
3931 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
3932 zp->z_atime_dirty = 0;
3935 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3937 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
3938 zfs_putpage_commit_cb, pp);
3941 zfs_range_unlock(rl);
3943 if (wbc->sync_mode != WB_SYNC_NONE) {
3945 * Note that this is rarely called under writepages(), because
3946 * writepages() normally handles the entire commit for
3947 * performance reasons.
3949 zil_commit(zsb->z_log, zp->z_id);
3957 * Update the system attributes when the inode has been dirtied. For the
3958 * moment we only update the mode, atime, mtime, and ctime.
3961 zfs_dirty_inode(struct inode *ip, int flags)
3963 znode_t *zp = ITOZ(ip);
3964 zfs_sb_t *zsb = ITOZSB(ip);
3966 uint64_t mode, atime[2], mtime[2], ctime[2];
3967 sa_bulk_attr_t bulk[4];
3974 tx = dmu_tx_create(zsb->z_os);
3976 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3977 zfs_sa_upgrade_txholds(tx, zp);
3979 error = dmu_tx_assign(tx, TXG_WAIT);
3985 mutex_enter(&zp->z_lock);
3986 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zsb), NULL, &mode, 8);
3987 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
3988 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3989 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3991 /* Preserve the mode, mtime and ctime provided by the inode */
3992 ZFS_TIME_ENCODE(&ip->i_atime, atime);
3993 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
3994 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
3998 zp->z_atime_dirty = 0;
4000 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4001 mutex_exit(&zp->z_lock);
4008 EXPORT_SYMBOL(zfs_dirty_inode);
4012 zfs_inactive(struct inode *ip)
4014 znode_t *zp = ITOZ(ip);
4015 zfs_sb_t *zsb = ITOZSB(ip);
4018 if (zfsctl_is_node(ip)) {
4019 zfsctl_inode_inactive(ip);
4023 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
4024 if (zp->z_sa_hdl == NULL) {
4025 rw_exit(&zsb->z_teardown_inactive_lock);
4029 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4030 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
4032 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4033 zfs_sa_upgrade_txholds(tx, zp);
4034 error = dmu_tx_assign(tx, TXG_WAIT);
4038 mutex_enter(&zp->z_lock);
4039 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
4040 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4041 zp->z_atime_dirty = 0;
4042 mutex_exit(&zp->z_lock);
4048 rw_exit(&zsb->z_teardown_inactive_lock);
4050 EXPORT_SYMBOL(zfs_inactive);
4053 * Bounds-check the seek operation.
4055 * IN: ip - inode seeking within
4056 * ooff - old file offset
4057 * noffp - pointer to new file offset
4058 * ct - caller context
4060 * RETURN: 0 if success
4061 * EINVAL if new offset invalid
4065 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4067 if (S_ISDIR(ip->i_mode))
4069 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4071 EXPORT_SYMBOL(zfs_seek);
4074 * Fill pages with data from the disk.
4077 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4079 znode_t *zp = ITOZ(ip);
4080 zfs_sb_t *zsb = ITOZSB(ip);
4082 struct page *cur_pp;
4083 u_offset_t io_off, total;
4090 io_len = nr_pages << PAGE_CACHE_SHIFT;
4091 i_size = i_size_read(ip);
4092 io_off = page_offset(pl[0]);
4094 if (io_off + io_len > i_size)
4095 io_len = i_size - io_off;
4098 * Iterate over list of pages and read each page individually.
4102 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4106 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4110 /* convert checksum errors into IO errors */
4112 err = SET_ERROR(EIO);
4115 cur_pp = pl[++page_idx];
4122 * Uses zfs_fillpage to read data from the file and fill the pages.
4124 * IN: ip - inode of file to get data from.
4125 * pl - list of pages to read
4126 * nr_pages - number of pages to read
4128 * RETURN: 0 on success, error code on failure.
4131 * vp - atime updated
4135 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4137 znode_t *zp = ITOZ(ip);
4138 zfs_sb_t *zsb = ITOZSB(ip);
4147 err = zfs_fillpage(ip, pl, nr_pages);
4150 ZFS_ACCESSTIME_STAMP(zsb, zp);
4155 EXPORT_SYMBOL(zfs_getpage);
4158 * Check ZFS specific permissions to memory map a section of a file.
4160 * IN: ip - inode of the file to mmap
4162 * addrp - start address in memory region
4163 * len - length of memory region
4164 * vm_flags- address flags
4166 * RETURN: 0 if success
4167 * error code if failure
4171 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4172 unsigned long vm_flags)
4174 znode_t *zp = ITOZ(ip);
4175 zfs_sb_t *zsb = ITOZSB(ip);
4180 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4181 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4183 return (SET_ERROR(EPERM));
4186 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4187 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4189 return (SET_ERROR(EACCES));
4192 if (off < 0 || len > MAXOFFSET_T - off) {
4194 return (SET_ERROR(ENXIO));
4200 EXPORT_SYMBOL(zfs_map);
4203 * convoff - converts the given data (start, whence) to the
4207 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4212 if ((lckdat->l_whence == 2) || (whence == 2)) {
4213 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4217 switch (lckdat->l_whence) {
4219 lckdat->l_start += offset;
4222 lckdat->l_start += vap.va_size;
4227 return (SET_ERROR(EINVAL));
4230 if (lckdat->l_start < 0)
4231 return (SET_ERROR(EINVAL));
4235 lckdat->l_start -= offset;
4238 lckdat->l_start -= vap.va_size;
4243 return (SET_ERROR(EINVAL));
4246 lckdat->l_whence = (short)whence;
4251 * Free or allocate space in a file. Currently, this function only
4252 * supports the `F_FREESP' command. However, this command is somewhat
4253 * misnamed, as its functionality includes the ability to allocate as
4254 * well as free space.
4256 * IN: ip - inode of file to free data in.
4257 * cmd - action to take (only F_FREESP supported).
4258 * bfp - section of file to free/alloc.
4259 * flag - current file open mode flags.
4260 * offset - current file offset.
4261 * cr - credentials of caller [UNUSED].
4263 * RETURN: 0 on success, error code on failure.
4266 * ip - ctime|mtime updated
4270 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4271 offset_t offset, cred_t *cr)
4273 znode_t *zp = ITOZ(ip);
4274 zfs_sb_t *zsb = ITOZSB(ip);
4281 if (cmd != F_FREESP) {
4283 return (SET_ERROR(EINVAL));
4286 if ((error = convoff(ip, bfp, 0, offset))) {
4291 if (bfp->l_len < 0) {
4293 return (SET_ERROR(EINVAL));
4297 * Permissions aren't checked on Solaris because on this OS
4298 * zfs_space() can only be called with an opened file handle.
4299 * On Linux we can get here through truncate_range() which
4300 * operates directly on inodes, so we need to check access rights.
4302 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4308 len = bfp->l_len; /* 0 means from off to end of file */
4310 error = zfs_freesp(zp, off, len, flag, TRUE);
4315 EXPORT_SYMBOL(zfs_space);
4319 zfs_fid(struct inode *ip, fid_t *fidp)
4321 znode_t *zp = ITOZ(ip);
4322 zfs_sb_t *zsb = ITOZSB(ip);
4325 uint64_t object = zp->z_id;
4332 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4333 &gen64, sizeof (uint64_t))) != 0) {
4338 gen = (uint32_t)gen64;
4340 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4341 if (fidp->fid_len < size) {
4342 fidp->fid_len = size;
4344 return (SET_ERROR(ENOSPC));
4347 zfid = (zfid_short_t *)fidp;
4349 zfid->zf_len = size;
4351 for (i = 0; i < sizeof (zfid->zf_object); i++)
4352 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4354 /* Must have a non-zero generation number to distinguish from .zfs */
4357 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4358 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4360 if (size == LONG_FID_LEN) {
4361 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4364 zlfid = (zfid_long_t *)fidp;
4366 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4367 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4369 /* XXX - this should be the generation number for the objset */
4370 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4371 zlfid->zf_setgen[i] = 0;
4377 EXPORT_SYMBOL(zfs_fid);
4381 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4383 znode_t *zp = ITOZ(ip);
4384 zfs_sb_t *zsb = ITOZSB(ip);
4386 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4390 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4395 EXPORT_SYMBOL(zfs_getsecattr);
4399 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4401 znode_t *zp = ITOZ(ip);
4402 zfs_sb_t *zsb = ITOZSB(ip);
4404 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4405 zilog_t *zilog = zsb->z_log;
4410 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4412 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4413 zil_commit(zilog, 0);
4418 EXPORT_SYMBOL(zfs_setsecattr);
4420 #ifdef HAVE_UIO_ZEROCOPY
4422 * Tunable, both must be a power of 2.
4424 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4425 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4426 * an arcbuf for a partial block read
4428 int zcr_blksz_min = (1 << 10); /* 1K */
4429 int zcr_blksz_max = (1 << 17); /* 128K */
4433 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4435 znode_t *zp = ITOZ(ip);
4436 zfs_sb_t *zsb = ITOZSB(ip);
4437 int max_blksz = zsb->z_max_blksz;
4438 uio_t *uio = &xuio->xu_uio;
4439 ssize_t size = uio->uio_resid;
4440 offset_t offset = uio->uio_loffset;
4445 int preamble, postamble;
4447 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4448 return (SET_ERROR(EINVAL));
4455 * Loan out an arc_buf for write if write size is bigger than
4456 * max_blksz, and the file's block size is also max_blksz.
4459 if (size < blksz || zp->z_blksz != blksz) {
4461 return (SET_ERROR(EINVAL));
4464 * Caller requests buffers for write before knowing where the
4465 * write offset might be (e.g. NFS TCP write).
4470 preamble = P2PHASE(offset, blksz);
4472 preamble = blksz - preamble;
4477 postamble = P2PHASE(size, blksz);
4480 fullblk = size / blksz;
4481 (void) dmu_xuio_init(xuio,
4482 (preamble != 0) + fullblk + (postamble != 0));
4485 * Have to fix iov base/len for partial buffers. They
4486 * currently represent full arc_buf's.
4489 /* data begins in the middle of the arc_buf */
4490 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4493 (void) dmu_xuio_add(xuio, abuf,
4494 blksz - preamble, preamble);
4497 for (i = 0; i < fullblk; i++) {
4498 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4501 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4505 /* data ends in the middle of the arc_buf */
4506 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4509 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4514 * Loan out an arc_buf for read if the read size is larger than
4515 * the current file block size. Block alignment is not
4516 * considered. Partial arc_buf will be loaned out for read.
4518 blksz = zp->z_blksz;
4519 if (blksz < zcr_blksz_min)
4520 blksz = zcr_blksz_min;
4521 if (blksz > zcr_blksz_max)
4522 blksz = zcr_blksz_max;
4523 /* avoid potential complexity of dealing with it */
4524 if (blksz > max_blksz) {
4526 return (SET_ERROR(EINVAL));
4529 maxsize = zp->z_size - uio->uio_loffset;
4535 return (SET_ERROR(EINVAL));
4540 return (SET_ERROR(EINVAL));
4543 uio->uio_extflg = UIO_XUIO;
4544 XUIO_XUZC_RW(xuio) = ioflag;
4551 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4555 int ioflag = XUIO_XUZC_RW(xuio);
4557 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4559 i = dmu_xuio_cnt(xuio);
4561 abuf = dmu_xuio_arcbuf(xuio, i);
4563 * if abuf == NULL, it must be a write buffer
4564 * that has been returned in zfs_write().
4567 dmu_return_arcbuf(abuf);
4568 ASSERT(abuf || ioflag == UIO_WRITE);
4571 dmu_xuio_fini(xuio);
4574 #endif /* HAVE_UIO_ZEROCOPY */
4576 #if defined(_KERNEL) && defined(HAVE_SPL)
4577 module_param(zfs_read_chunk_size, long, 0644);
4578 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");