4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/sysmacros.h>
41 #include <sys/taskq.h>
43 #include <sys/vmsystm.h>
44 #include <sys/atomic.h>
45 #include <sys/pathname.h>
46 #include <sys/cmn_err.h>
47 #include <sys/errno.h>
48 #include <sys/zfs_dir.h>
49 #include <sys/zfs_acl.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/fs/zfs.h>
53 #include <sys/dmu_objset.h>
59 #include <sys/policy.h>
60 #include <sys/sunddi.h>
63 #include <sys/zfs_ctldir.h>
64 #include <sys/zfs_fuid.h>
65 #include <sys/zfs_sa.h>
66 #include <sys/zfs_vnops.h>
67 #include <sys/zfs_rlock.h>
71 #include <sys/sa_impl.h>
76 * Each vnode op performs some logical unit of work. To do this, the ZPL must
77 * properly lock its in-core state, create a DMU transaction, do the work,
78 * record this work in the intent log (ZIL), commit the DMU transaction,
79 * and wait for the intent log to commit if it is a synchronous operation.
80 * Moreover, the vnode ops must work in both normal and log replay context.
81 * The ordering of events is important to avoid deadlocks and references
82 * to freed memory. The example below illustrates the following Big Rules:
84 * (1) A check must be made in each zfs thread for a mounted file system.
85 * This is done avoiding races using ZFS_ENTER(zfsvfs).
86 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
87 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
88 * can return EIO from the calling function.
90 * (2) iput() should always be the last thing except for zil_commit()
91 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
92 * First, if it's the last reference, the vnode/znode
93 * can be freed, so the zp may point to freed memory. Second, the last
94 * reference will call zfs_zinactive(), which may induce a lot of work --
95 * pushing cached pages (which acquires range locks) and syncing out
96 * cached atime changes. Third, zfs_zinactive() may require a new tx,
97 * which could deadlock the system if you were already holding one.
98 * If you must call iput() within a tx then use zfs_iput_async().
100 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
101 * as they can span dmu_tx_assign() calls.
103 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
104 * dmu_tx_assign(). This is critical because we don't want to block
105 * while holding locks.
107 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
108 * reduces lock contention and CPU usage when we must wait (note that if
109 * throughput is constrained by the storage, nearly every transaction
112 * Note, in particular, that if a lock is sometimes acquired before
113 * the tx assigns, and sometimes after (e.g. z_lock), then failing
114 * to use a non-blocking assign can deadlock the system. The scenario:
116 * Thread A has grabbed a lock before calling dmu_tx_assign().
117 * Thread B is in an already-assigned tx, and blocks for this lock.
118 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
119 * forever, because the previous txg can't quiesce until B's tx commits.
121 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
122 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
123 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT,
124 * to indicate that this operation has already called dmu_tx_wait().
125 * This will ensure that we don't retry forever, waiting a short bit
128 * (5) If the operation succeeded, generate the intent log entry for it
129 * before dropping locks. This ensures that the ordering of events
130 * in the intent log matches the order in which they actually occurred.
131 * During ZIL replay the zfs_log_* functions will update the sequence
132 * number to indicate the zil transaction has replayed.
134 * (6) At the end of each vnode op, the DMU tx must always commit,
135 * regardless of whether there were any errors.
137 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
138 * to ensure that synchronous semantics are provided when necessary.
140 * In general, this is how things should be ordered in each vnode op:
142 * ZFS_ENTER(zfsvfs); // exit if unmounted
144 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
145 * rw_enter(...); // grab any other locks you need
146 * tx = dmu_tx_create(...); // get DMU tx
147 * dmu_tx_hold_*(); // hold each object you might modify
148 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
150 * rw_exit(...); // drop locks
151 * zfs_dirent_unlock(dl); // unlock directory entry
152 * iput(...); // release held vnodes
153 * if (error == ERESTART) {
159 * dmu_tx_abort(tx); // abort DMU tx
160 * ZFS_EXIT(zfsvfs); // finished in zfs
161 * return (error); // really out of space
163 * error = do_real_work(); // do whatever this VOP does
165 * zfs_log_*(...); // on success, make ZIL entry
166 * dmu_tx_commit(tx); // commit DMU tx -- error or not
167 * rw_exit(...); // drop locks
168 * zfs_dirent_unlock(dl); // unlock directory entry
169 * iput(...); // release held vnodes
170 * zil_commit(zilog, foid); // synchronous when necessary
171 * ZFS_EXIT(zfsvfs); // finished in zfs
172 * return (error); // done, report error
176 * Virus scanning is unsupported. It would be possible to add a hook
177 * here to performance the required virus scan. This could be done
178 * entirely in the kernel or potentially as an update to invoke a
182 zfs_vscan(struct inode *ip, cred_t *cr, int async)
189 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
191 znode_t *zp = ITOZ(ip);
192 zfsvfs_t *zfsvfs = ITOZSB(ip);
197 /* Honor ZFS_APPENDONLY file attribute */
198 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
199 ((flag & O_APPEND) == 0)) {
201 return (SET_ERROR(EPERM));
204 /* Virus scan eligible files on open */
205 if (!zfs_has_ctldir(zp) && zfsvfs->z_vscan && S_ISREG(ip->i_mode) &&
206 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
207 if (zfs_vscan(ip, cr, 0) != 0) {
209 return (SET_ERROR(EACCES));
213 /* Keep a count of the synchronous opens in the znode */
215 atomic_inc_32(&zp->z_sync_cnt);
223 zfs_close(struct inode *ip, int flag, cred_t *cr)
225 znode_t *zp = ITOZ(ip);
226 zfsvfs_t *zfsvfs = ITOZSB(ip);
231 /* Decrement the synchronous opens in the znode */
233 atomic_dec_32(&zp->z_sync_cnt);
235 if (!zfs_has_ctldir(zp) && zfsvfs->z_vscan && S_ISREG(ip->i_mode) &&
236 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
237 VERIFY(zfs_vscan(ip, cr, 1) == 0);
243 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
245 * Lseek support for finding holes (cmd == SEEK_HOLE) and
246 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
249 zfs_holey_common(struct inode *ip, int cmd, loff_t *off)
251 znode_t *zp = ITOZ(ip);
252 uint64_t noff = (uint64_t)*off; /* new offset */
257 file_sz = zp->z_size;
258 if (noff >= file_sz) {
259 return (SET_ERROR(ENXIO));
262 if (cmd == SEEK_HOLE)
267 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
270 return (SET_ERROR(ENXIO));
272 /* file was dirty, so fall back to using generic logic */
273 if (error == EBUSY) {
281 * We could find a hole that begins after the logical end-of-file,
282 * because dmu_offset_next() only works on whole blocks. If the
283 * EOF falls mid-block, then indicate that the "virtual hole"
284 * at the end of the file begins at the logical EOF, rather than
285 * at the end of the last block.
287 if (noff > file_sz) {
299 zfs_holey(struct inode *ip, int cmd, loff_t *off)
301 znode_t *zp = ITOZ(ip);
302 zfsvfs_t *zfsvfs = ITOZSB(ip);
308 error = zfs_holey_common(ip, cmd, off);
313 #endif /* SEEK_HOLE && SEEK_DATA */
317 * When a file is memory mapped, we must keep the IO data synchronized
318 * between the DMU cache and the memory mapped pages. What this means:
320 * On Write: If we find a memory mapped page, we write to *both*
321 * the page and the dmu buffer.
324 update_pages(struct inode *ip, int64_t start, int len,
325 objset_t *os, uint64_t oid)
327 struct address_space *mp = ip->i_mapping;
333 off = start & (PAGE_SIZE-1);
334 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
335 nbytes = MIN(PAGE_SIZE - off, len);
337 pp = find_lock_page(mp, start >> PAGE_SHIFT);
339 if (mapping_writably_mapped(mp))
340 flush_dcache_page(pp);
343 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
347 if (mapping_writably_mapped(mp))
348 flush_dcache_page(pp);
350 mark_page_accessed(pp);
363 * When a file is memory mapped, we must keep the IO data synchronized
364 * between the DMU cache and the memory mapped pages. What this means:
366 * On Read: We "read" preferentially from memory mapped pages,
367 * else we default from the dmu buffer.
369 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
370 * the file is memory mapped.
373 mappedread(struct inode *ip, int nbytes, uio_t *uio)
375 struct address_space *mp = ip->i_mapping;
377 znode_t *zp = ITOZ(ip);
384 start = uio->uio_loffset;
385 off = start & (PAGE_SIZE-1);
386 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
387 bytes = MIN(PAGE_SIZE - off, len);
389 pp = find_lock_page(mp, start >> PAGE_SHIFT);
391 ASSERT(PageUptodate(pp));
395 error = uiomove(pb + off, bytes, UIO_READ, uio);
398 if (mapping_writably_mapped(mp))
399 flush_dcache_page(pp);
401 mark_page_accessed(pp);
404 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
417 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
418 unsigned long zfs_delete_blocks = DMU_MAX_DELETEBLKCNT;
421 * Read bytes from specified file into supplied buffer.
423 * IN: ip - inode of file to be read from.
424 * uio - structure supplying read location, range info,
426 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
427 * O_DIRECT flag; used to bypass page cache.
428 * cr - credentials of caller.
430 * OUT: uio - updated offset and range, buffer filled.
432 * RETURN: 0 on success, error code on failure.
435 * inode - atime updated if byte count > 0
439 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
442 boolean_t frsync = B_FALSE;
444 znode_t *zp = ITOZ(ip);
445 zfsvfs_t *zfsvfs = ITOZSB(ip);
449 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
451 return (SET_ERROR(EACCES));
455 * Validate file offset
457 if (uio->uio_loffset < (offset_t)0) {
459 return (SET_ERROR(EINVAL));
463 * Fasttrack empty reads
465 if (uio->uio_resid == 0) {
472 * If we're in FRSYNC mode, sync out this znode before reading it.
473 * Only do this for non-snapshots.
475 * Some platforms do not support FRSYNC and instead map it
476 * to FSYNC, which results in unnecessary calls to zil_commit. We
477 * only honor FRSYNC requests on platforms which support it.
479 frsync = !!(ioflag & FRSYNC);
482 (frsync || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
483 zil_commit(zfsvfs->z_log, zp->z_id);
486 * Lock the range against changes.
488 locked_range_t *lr = rangelock_enter(&zp->z_rangelock,
489 uio->uio_loffset, uio->uio_resid, RL_READER);
492 * If we are reading past end-of-file we can skip
493 * to the end; but we might still need to set atime.
495 if (uio->uio_loffset >= zp->z_size) {
500 ASSERT(uio->uio_loffset < zp->z_size);
501 ssize_t n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
502 ssize_t start_resid = n;
504 #ifdef HAVE_UIO_ZEROCOPY
506 if ((uio->uio_extflg == UIO_XUIO) &&
507 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
509 int blksz = zp->z_blksz;
510 uint64_t offset = uio->uio_loffset;
512 xuio = (xuio_t *)uio;
514 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
517 ASSERT(offset + n <= blksz);
520 (void) dmu_xuio_init(xuio, nblk);
522 if (vn_has_cached_data(ip)) {
524 * For simplicity, we always allocate a full buffer
525 * even if we only expect to read a portion of a block.
527 while (--nblk >= 0) {
528 (void) dmu_xuio_add(xuio,
529 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
534 #endif /* HAVE_UIO_ZEROCOPY */
537 ssize_t nbytes = MIN(n, zfs_read_chunk_size -
538 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
540 if (zp->z_is_mapped && !(ioflag & O_DIRECT)) {
541 error = mappedread(ip, nbytes, uio);
543 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
548 /* convert checksum errors into IO errors */
550 error = SET_ERROR(EIO);
557 int64_t nread = start_resid - n;
558 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, nread);
559 task_io_account_read(nread);
568 * Write the bytes to a file.
570 * IN: ip - inode of file to be written to.
571 * uio - structure supplying write location, range info,
573 * ioflag - FAPPEND flag set if in append mode.
574 * O_DIRECT flag; used to bypass page cache.
575 * cr - credentials of caller.
577 * OUT: uio - updated offset and range.
579 * RETURN: 0 if success
580 * error code if failure
583 * ip - ctime|mtime updated if byte count > 0
588 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
591 ssize_t start_resid = uio->uio_resid;
594 * Fasttrack empty write
596 ssize_t n = start_resid;
600 rlim64_t limit = uio->uio_limit;
601 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
604 znode_t *zp = ITOZ(ip);
605 zfsvfs_t *zfsvfs = ZTOZSB(zp);
609 sa_bulk_attr_t bulk[4];
611 uint64_t mtime[2], ctime[2];
612 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
613 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
614 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
616 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
620 * Callers might not be able to detect properly that we are read-only,
621 * so check it explicitly here.
623 if (zfs_is_readonly(zfsvfs)) {
625 return (SET_ERROR(EROFS));
629 * If immutable or not appending then return EPERM
631 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
632 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
633 (uio->uio_loffset < zp->z_size))) {
635 return (SET_ERROR(EPERM));
639 * Validate file offset
641 offset_t woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
644 return (SET_ERROR(EINVAL));
647 int max_blksz = zfsvfs->z_max_blksz;
651 * Pre-fault the pages to ensure slow (eg NFS) pages
653 * Skip this if uio contains loaned arc_buf.
655 #ifdef HAVE_UIO_ZEROCOPY
656 if ((uio->uio_extflg == UIO_XUIO) &&
657 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
658 xuio = (xuio_t *)uio;
661 if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
663 return (SET_ERROR(EFAULT));
667 * If in append mode, set the io offset pointer to eof.
670 if (ioflag & FAPPEND) {
672 * Obtain an appending range lock to guarantee file append
673 * semantics. We reset the write offset once we have the lock.
675 lr = rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND);
676 woff = lr->lr_offset;
677 if (lr->lr_length == UINT64_MAX) {
679 * We overlocked the file because this write will cause
680 * the file block size to increase.
681 * Note that zp_size cannot change with this lock held.
685 uio->uio_loffset = woff;
688 * Note that if the file block size will change as a result of
689 * this write, then this range lock will lock the entire file
690 * so that we can re-write the block safely.
692 lr = rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER);
698 return (SET_ERROR(EFBIG));
701 if ((woff + n) > limit || woff > (limit - n))
704 /* Will this write extend the file length? */
705 int write_eof = (woff + n > zp->z_size);
707 uint64_t end_size = MAX(zp->z_size, woff + n);
708 zilog_t *zilog = zfsvfs->z_log;
709 #ifdef HAVE_UIO_ZEROCOPY
711 const iovec_t *iovp = uio->uio_iov;
712 ASSERTV(int iovcnt = uio->uio_iovcnt);
717 * Write the file in reasonable size chunks. Each chunk is written
718 * in a separate transaction; this keeps the intent log records small
719 * and allows us to do more fine-grained space accounting.
722 woff = uio->uio_loffset;
724 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT,
725 KUID_TO_SUID(ip->i_uid)) ||
726 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT,
727 KGID_TO_SGID(ip->i_gid)) ||
728 (zp->z_projid != ZFS_DEFAULT_PROJID &&
729 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
731 error = SET_ERROR(EDQUOT);
735 arc_buf_t *abuf = NULL;
736 const iovec_t *aiov = NULL;
738 #ifdef HAVE_UIO_ZEROCOPY
739 ASSERT(i_iov < iovcnt);
740 ASSERT3U(uio->uio_segflg, !=, UIO_BVEC);
742 abuf = dmu_xuio_arcbuf(xuio, i_iov);
743 dmu_xuio_clear(xuio, i_iov);
744 ASSERT((aiov->iov_base == abuf->b_data) ||
745 ((char *)aiov->iov_base - (char *)abuf->b_data +
746 aiov->iov_len == arc_buf_size(abuf)));
749 } else if (n >= max_blksz && woff >= zp->z_size &&
750 P2PHASE(woff, max_blksz) == 0 &&
751 zp->z_blksz == max_blksz) {
753 * This write covers a full block. "Borrow" a buffer
754 * from the dmu so that we can fill it before we enter
755 * a transaction. This avoids the possibility of
756 * holding up the transaction if the data copy hangs
757 * up on a pagefault (e.g., from an NFS server mapping).
761 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
763 ASSERT(abuf != NULL);
764 ASSERT(arc_buf_size(abuf) == max_blksz);
765 if ((error = uiocopy(abuf->b_data, max_blksz,
766 UIO_WRITE, uio, &cbytes))) {
767 dmu_return_arcbuf(abuf);
770 ASSERT(cbytes == max_blksz);
774 * Start a transaction.
776 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
777 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
778 dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl);
780 dmu_tx_hold_write_by_dnode(tx, DB_DNODE(db), woff,
783 zfs_sa_upgrade_txholds(tx, zp);
784 error = dmu_tx_assign(tx, TXG_WAIT);
788 dmu_return_arcbuf(abuf);
793 * If rangelock_enter() over-locked we grow the blocksize
794 * and then reduce the lock range. This will only happen
795 * on the first iteration since rangelock_reduce() will
796 * shrink down lr_length to the appropriate size.
798 if (lr->lr_length == UINT64_MAX) {
801 if (zp->z_blksz > max_blksz) {
803 * File's blocksize is already larger than the
804 * "recordsize" property. Only let it grow to
805 * the next power of 2.
807 ASSERT(!ISP2(zp->z_blksz));
808 new_blksz = MIN(end_size,
809 1 << highbit64(zp->z_blksz));
811 new_blksz = MIN(end_size, max_blksz);
813 zfs_grow_blocksize(zp, new_blksz, tx);
814 rangelock_reduce(lr, woff, n);
818 * XXX - should we really limit each write to z_max_blksz?
819 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
821 ssize_t nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
825 tx_bytes = uio->uio_resid;
826 uio->uio_fault_disable = B_TRUE;
827 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
829 uio->uio_fault_disable = B_FALSE;
830 if (error == EFAULT) {
832 if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
836 } else if (error != 0) {
840 tx_bytes -= uio->uio_resid;
843 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
845 * If this is not a full block write, but we are
846 * extending the file past EOF and this data starts
847 * block-aligned, use assign_arcbuf(). Otherwise,
848 * write via dmu_write().
850 if (tx_bytes < max_blksz && (!write_eof ||
851 aiov->iov_base != abuf->b_data)) {
853 dmu_write(zfsvfs->z_os, zp->z_id, woff,
854 /* cppcheck-suppress nullPointer */
855 aiov->iov_len, aiov->iov_base, tx);
856 dmu_return_arcbuf(abuf);
857 xuio_stat_wbuf_copied();
859 ASSERT(xuio || tx_bytes == max_blksz);
860 error = dmu_assign_arcbuf_by_dbuf(
861 sa_get_db(zp->z_sa_hdl), woff, abuf, tx);
863 dmu_return_arcbuf(abuf);
868 ASSERT(tx_bytes <= uio->uio_resid);
869 uioskip(uio, tx_bytes);
871 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT)) {
872 update_pages(ip, woff,
873 tx_bytes, zfsvfs->z_os, zp->z_id);
877 * If we made no progress, we're done. If we made even
878 * partial progress, update the znode and ZIL accordingly.
881 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
882 (void *)&zp->z_size, sizeof (uint64_t), tx);
889 * Clear Set-UID/Set-GID bits on successful write if not
890 * privileged and at least one of the execute bits is set.
892 * It would be nice to do this after all writes have
893 * been done, but that would still expose the ISUID/ISGID
894 * to another app after the partial write is committed.
896 * Note: we don't call zfs_fuid_map_id() here because
897 * user 0 is not an ephemeral uid.
899 mutex_enter(&zp->z_acl_lock);
900 uint32_t uid = KUID_TO_SUID(ip->i_uid);
901 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
902 (S_IXUSR >> 6))) != 0 &&
903 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
904 secpolicy_vnode_setid_retain(cr,
905 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) {
907 zp->z_mode &= ~(S_ISUID | S_ISGID);
908 ip->i_mode = newmode = zp->z_mode;
909 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
910 (void *)&newmode, sizeof (uint64_t), tx);
912 mutex_exit(&zp->z_acl_lock);
914 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
917 * Update the file size (zp_size) if it has changed;
918 * account for possible concurrent updates.
920 while ((end_size = zp->z_size) < uio->uio_loffset) {
921 (void) atomic_cas_64(&zp->z_size, end_size,
926 * If we are replaying and eof is non zero then force
927 * the file size to the specified eof. Note, there's no
928 * concurrency during replay.
930 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
931 zp->z_size = zfsvfs->z_replay_eof;
933 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
935 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
941 ASSERT(tx_bytes == nbytes);
944 if (!xuio && n > 0) {
945 if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
952 zfs_inode_update(zp);
956 * If we're in replay mode, or we made no progress, return error.
957 * Otherwise, it's at least a partial write, so it's successful.
959 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
964 if (ioflag & (FSYNC | FDSYNC) ||
965 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
966 zil_commit(zilog, zp->z_id);
968 int64_t nwritten = start_resid - uio->uio_resid;
969 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, nwritten);
970 task_io_account_write(nwritten);
977 * Drop a reference on the passed inode asynchronously. This ensures
978 * that the caller will never drop the last reference on an inode in
979 * the current context. Doing so while holding open a tx could result
980 * in a deadlock if iput_final() re-enters the filesystem code.
983 zfs_iput_async(struct inode *ip)
985 objset_t *os = ITOZSB(ip)->z_os;
987 ASSERT(atomic_read(&ip->i_count) > 0);
990 if (atomic_read(&ip->i_count) == 1)
991 VERIFY(taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os)),
992 (task_func_t *)iput, ip, TQ_SLEEP) != TASKQID_INVALID);
999 zfs_get_done(zgd_t *zgd, int error)
1001 znode_t *zp = zgd->zgd_private;
1004 dmu_buf_rele(zgd->zgd_db, zgd);
1006 rangelock_exit(zgd->zgd_lr);
1009 * Release the vnode asynchronously as we currently have the
1010 * txg stopped from syncing.
1012 zfs_iput_async(ZTOI(zp));
1014 kmem_free(zgd, sizeof (zgd_t));
1018 static int zil_fault_io = 0;
1022 * Get data to generate a TX_WRITE intent log record.
1025 zfs_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1027 zfsvfs_t *zfsvfs = arg;
1028 objset_t *os = zfsvfs->z_os;
1030 uint64_t object = lr->lr_foid;
1031 uint64_t offset = lr->lr_offset;
1032 uint64_t size = lr->lr_length;
1037 ASSERT3P(lwb, !=, NULL);
1038 ASSERT3P(zio, !=, NULL);
1039 ASSERT3U(size, !=, 0);
1042 * Nothing to do if the file has been removed
1044 if (zfs_zget(zfsvfs, object, &zp) != 0)
1045 return (SET_ERROR(ENOENT));
1046 if (zp->z_unlinked) {
1048 * Release the vnode asynchronously as we currently have the
1049 * txg stopped from syncing.
1051 zfs_iput_async(ZTOI(zp));
1052 return (SET_ERROR(ENOENT));
1055 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1057 zgd->zgd_private = zp;
1060 * Write records come in two flavors: immediate and indirect.
1061 * For small writes it's cheaper to store the data with the
1062 * log record (immediate); for large writes it's cheaper to
1063 * sync the data and get a pointer to it (indirect) so that
1064 * we don't have to write the data twice.
1066 if (buf != NULL) { /* immediate write */
1067 zgd->zgd_lr = rangelock_enter(&zp->z_rangelock,
1068 offset, size, RL_READER);
1069 /* test for truncation needs to be done while range locked */
1070 if (offset >= zp->z_size) {
1071 error = SET_ERROR(ENOENT);
1073 error = dmu_read(os, object, offset, size, buf,
1074 DMU_READ_NO_PREFETCH);
1076 ASSERT(error == 0 || error == ENOENT);
1077 } else { /* indirect write */
1079 * Have to lock the whole block to ensure when it's
1080 * written out and its checksum is being calculated
1081 * that no one can change the data. We need to re-check
1082 * blocksize after we get the lock in case it's changed!
1087 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1089 zgd->zgd_lr = rangelock_enter(&zp->z_rangelock,
1090 offset, size, RL_READER);
1091 if (zp->z_blksz == size)
1094 rangelock_exit(zgd->zgd_lr);
1096 /* test for truncation needs to be done while range locked */
1097 if (lr->lr_offset >= zp->z_size)
1098 error = SET_ERROR(ENOENT);
1101 error = SET_ERROR(EIO);
1106 error = dmu_buf_hold(os, object, offset, zgd, &db,
1107 DMU_READ_NO_PREFETCH);
1110 blkptr_t *bp = &lr->lr_blkptr;
1115 ASSERT(db->db_offset == offset);
1116 ASSERT(db->db_size == size);
1118 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1120 ASSERT(error || lr->lr_length <= size);
1123 * On success, we need to wait for the write I/O
1124 * initiated by dmu_sync() to complete before we can
1125 * release this dbuf. We will finish everything up
1126 * in the zfs_get_done() callback.
1131 if (error == EALREADY) {
1132 lr->lr_common.lrc_txtype = TX_WRITE2;
1134 * TX_WRITE2 relies on the data previously
1135 * written by the TX_WRITE that caused
1136 * EALREADY. We zero out the BP because
1137 * it is the old, currently-on-disk BP.
1146 zfs_get_done(zgd, error);
1153 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1155 znode_t *zp = ITOZ(ip);
1156 zfsvfs_t *zfsvfs = ITOZSB(ip);
1162 if (flag & V_ACE_MASK)
1163 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1165 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1172 * Lookup an entry in a directory, or an extended attribute directory.
1173 * If it exists, return a held inode reference for it.
1175 * IN: dip - inode of directory to search.
1176 * nm - name of entry to lookup.
1177 * flags - LOOKUP_XATTR set if looking for an attribute.
1178 * cr - credentials of caller.
1179 * direntflags - directory lookup flags
1180 * realpnp - returned pathname.
1182 * OUT: ipp - inode of located entry, NULL if not found.
1184 * RETURN: 0 on success, error code on failure.
1191 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1192 cred_t *cr, int *direntflags, pathname_t *realpnp)
1194 znode_t *zdp = ITOZ(dip);
1195 zfsvfs_t *zfsvfs = ITOZSB(dip);
1199 * Fast path lookup, however we must skip DNLC lookup
1200 * for case folding or normalizing lookups because the
1201 * DNLC code only stores the passed in name. This means
1202 * creating 'a' and removing 'A' on a case insensitive
1203 * file system would work, but DNLC still thinks 'a'
1204 * exists and won't let you create it again on the next
1205 * pass through fast path.
1207 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1209 if (!S_ISDIR(dip->i_mode)) {
1210 return (SET_ERROR(ENOTDIR));
1211 } else if (zdp->z_sa_hdl == NULL) {
1212 return (SET_ERROR(EIO));
1215 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1216 error = zfs_fastaccesschk_execute(zdp, cr);
1231 if (flags & LOOKUP_XATTR) {
1233 * We don't allow recursive attributes..
1234 * Maybe someday we will.
1236 if (zdp->z_pflags & ZFS_XATTR) {
1238 return (SET_ERROR(EINVAL));
1241 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1247 * Do we have permission to get into attribute directory?
1250 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1260 if (!S_ISDIR(dip->i_mode)) {
1262 return (SET_ERROR(ENOTDIR));
1266 * Check accessibility of directory.
1269 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1274 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1275 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1277 return (SET_ERROR(EILSEQ));
1280 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1281 if ((error == 0) && (*ipp))
1282 zfs_inode_update(ITOZ(*ipp));
1289 * Attempt to create a new entry in a directory. If the entry
1290 * already exists, truncate the file if permissible, else return
1291 * an error. Return the ip of the created or trunc'd file.
1293 * IN: dip - inode of directory to put new file entry in.
1294 * name - name of new file entry.
1295 * vap - attributes of new file.
1296 * excl - flag indicating exclusive or non-exclusive mode.
1297 * mode - mode to open file with.
1298 * cr - credentials of caller.
1300 * vsecp - ACL to be set
1302 * OUT: ipp - inode of created or trunc'd entry.
1304 * RETURN: 0 on success, error code on failure.
1307 * dip - ctime|mtime updated if new entry created
1308 * ip - ctime|mtime always, atime if new
1313 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1314 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1316 znode_t *zp, *dzp = ITOZ(dip);
1317 zfsvfs_t *zfsvfs = ITOZSB(dip);
1325 zfs_acl_ids_t acl_ids;
1326 boolean_t fuid_dirtied;
1327 boolean_t have_acl = B_FALSE;
1328 boolean_t waited = B_FALSE;
1331 * If we have an ephemeral id, ACL, or XVATTR then
1332 * make sure file system is at proper version
1338 if (zfsvfs->z_use_fuids == B_FALSE &&
1339 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1340 return (SET_ERROR(EINVAL));
1343 return (SET_ERROR(EINVAL));
1348 zilog = zfsvfs->z_log;
1350 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1351 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1353 return (SET_ERROR(EILSEQ));
1356 if (vap->va_mask & ATTR_XVATTR) {
1357 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1358 crgetuid(cr), cr, vap->va_mode)) != 0) {
1366 if (*name == '\0') {
1368 * Null component name refers to the directory itself.
1375 /* possible igrab(zp) */
1378 if (flag & FIGNORECASE)
1381 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1385 zfs_acl_ids_free(&acl_ids);
1386 if (strcmp(name, "..") == 0)
1387 error = SET_ERROR(EISDIR);
1395 uint64_t projid = ZFS_DEFAULT_PROJID;
1398 * Create a new file object and update the directory
1401 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1403 zfs_acl_ids_free(&acl_ids);
1408 * We only support the creation of regular files in
1409 * extended attribute directories.
1412 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1414 zfs_acl_ids_free(&acl_ids);
1415 error = SET_ERROR(EINVAL);
1419 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1420 cr, vsecp, &acl_ids)) != 0)
1424 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
1425 projid = zfs_inherit_projid(dzp);
1426 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1427 zfs_acl_ids_free(&acl_ids);
1428 error = SET_ERROR(EDQUOT);
1432 tx = dmu_tx_create(os);
1434 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1435 ZFS_SA_BASE_ATTR_SIZE);
1437 fuid_dirtied = zfsvfs->z_fuid_dirty;
1439 zfs_fuid_txhold(zfsvfs, tx);
1440 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1441 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1442 if (!zfsvfs->z_use_sa &&
1443 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1444 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1445 0, acl_ids.z_aclp->z_acl_bytes);
1448 error = dmu_tx_assign(tx,
1449 (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
1451 zfs_dirent_unlock(dl);
1452 if (error == ERESTART) {
1458 zfs_acl_ids_free(&acl_ids);
1463 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1465 error = zfs_link_create(dl, zp, tx, ZNEW);
1468 * Since, we failed to add the directory entry for it,
1469 * delete the newly created dnode.
1471 zfs_znode_delete(zp, tx);
1472 remove_inode_hash(ZTOI(zp));
1473 zfs_acl_ids_free(&acl_ids);
1479 zfs_fuid_sync(zfsvfs, tx);
1481 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1482 if (flag & FIGNORECASE)
1484 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1485 vsecp, acl_ids.z_fuidp, vap);
1486 zfs_acl_ids_free(&acl_ids);
1489 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1492 zfs_acl_ids_free(&acl_ids);
1496 * A directory entry already exists for this name.
1499 * Can't truncate an existing file if in exclusive mode.
1502 error = SET_ERROR(EEXIST);
1506 * Can't open a directory for writing.
1508 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1509 error = SET_ERROR(EISDIR);
1513 * Verify requested access to file.
1515 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1519 mutex_enter(&dzp->z_lock);
1521 mutex_exit(&dzp->z_lock);
1524 * Truncate regular files if requested.
1526 if (S_ISREG(ZTOI(zp)->i_mode) &&
1527 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1528 /* we can't hold any locks when calling zfs_freesp() */
1530 zfs_dirent_unlock(dl);
1533 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1539 zfs_dirent_unlock(dl);
1545 zfs_inode_update(dzp);
1546 zfs_inode_update(zp);
1550 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1551 zil_commit(zilog, 0);
1559 zfs_tmpfile(struct inode *dip, vattr_t *vap, int excl,
1560 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1562 znode_t *zp = NULL, *dzp = ITOZ(dip);
1563 zfsvfs_t *zfsvfs = ITOZSB(dip);
1569 zfs_acl_ids_t acl_ids;
1570 uint64_t projid = ZFS_DEFAULT_PROJID;
1571 boolean_t fuid_dirtied;
1572 boolean_t have_acl = B_FALSE;
1573 boolean_t waited = B_FALSE;
1576 * If we have an ephemeral id, ACL, or XVATTR then
1577 * make sure file system is at proper version
1583 if (zfsvfs->z_use_fuids == B_FALSE &&
1584 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1585 return (SET_ERROR(EINVAL));
1591 if (vap->va_mask & ATTR_XVATTR) {
1592 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1593 crgetuid(cr), cr, vap->va_mode)) != 0) {
1603 * Create a new file object and update the directory
1606 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1608 zfs_acl_ids_free(&acl_ids);
1612 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1613 cr, vsecp, &acl_ids)) != 0)
1617 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode))
1618 projid = zfs_inherit_projid(dzp);
1619 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) {
1620 zfs_acl_ids_free(&acl_ids);
1621 error = SET_ERROR(EDQUOT);
1625 tx = dmu_tx_create(os);
1627 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1628 ZFS_SA_BASE_ATTR_SIZE);
1629 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1631 fuid_dirtied = zfsvfs->z_fuid_dirty;
1633 zfs_fuid_txhold(zfsvfs, tx);
1634 if (!zfsvfs->z_use_sa &&
1635 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1636 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1637 0, acl_ids.z_aclp->z_acl_bytes);
1639 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
1641 if (error == ERESTART) {
1647 zfs_acl_ids_free(&acl_ids);
1652 zfs_mknode(dzp, vap, tx, cr, IS_TMPFILE, &zp, &acl_ids);
1655 zfs_fuid_sync(zfsvfs, tx);
1657 /* Add to unlinked set */
1658 zp->z_unlinked = B_TRUE;
1659 zfs_unlinked_add(zp, tx);
1660 zfs_acl_ids_free(&acl_ids);
1668 zfs_inode_update(dzp);
1669 zfs_inode_update(zp);
1678 * Remove an entry from a directory.
1680 * IN: dip - inode of directory to remove entry from.
1681 * name - name of entry to remove.
1682 * cr - credentials of caller.
1683 * flags - case flags.
1685 * RETURN: 0 if success
1686 * error code if failure
1690 * ip - ctime (if nlink > 0)
1693 uint64_t null_xattr = 0;
1697 zfs_remove(struct inode *dip, char *name, cred_t *cr, int flags)
1699 znode_t *zp, *dzp = ITOZ(dip);
1702 zfsvfs_t *zfsvfs = ITOZSB(dip);
1704 uint64_t acl_obj, xattr_obj;
1705 uint64_t xattr_obj_unlinked = 0;
1710 boolean_t may_delete_now, delete_now = FALSE;
1711 boolean_t unlinked, toobig = FALSE;
1713 pathname_t *realnmp = NULL;
1717 boolean_t waited = B_FALSE;
1720 return (SET_ERROR(EINVAL));
1724 zilog = zfsvfs->z_log;
1726 if (flags & FIGNORECASE) {
1736 * Attempt to lock directory; fail if entry doesn't exist.
1738 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1748 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1753 * Need to use rmdir for removing directories.
1755 if (S_ISDIR(ip->i_mode)) {
1756 error = SET_ERROR(EPERM);
1760 mutex_enter(&zp->z_lock);
1761 may_delete_now = atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped);
1762 mutex_exit(&zp->z_lock);
1765 * We may delete the znode now, or we may put it in the unlinked set;
1766 * it depends on whether we're the last link, and on whether there are
1767 * other holds on the inode. So we dmu_tx_hold() the right things to
1768 * allow for either case.
1771 tx = dmu_tx_create(zfsvfs->z_os);
1772 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1773 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1774 zfs_sa_upgrade_txholds(tx, zp);
1775 zfs_sa_upgrade_txholds(tx, dzp);
1776 if (may_delete_now) {
1777 toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks;
1778 /* if the file is too big, only hold_free a token amount */
1779 dmu_tx_hold_free(tx, zp->z_id, 0,
1780 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1783 /* are there any extended attributes? */
1784 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1785 &xattr_obj, sizeof (xattr_obj));
1786 if (error == 0 && xattr_obj) {
1787 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1789 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1790 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1793 mutex_enter(&zp->z_lock);
1794 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1795 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1796 mutex_exit(&zp->z_lock);
1798 /* charge as an update -- would be nice not to charge at all */
1799 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1802 * Mark this transaction as typically resulting in a net free of space
1804 dmu_tx_mark_netfree(tx);
1806 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
1808 zfs_dirent_unlock(dl);
1809 if (error == ERESTART) {
1829 * Remove the directory entry.
1831 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1840 * Hold z_lock so that we can make sure that the ACL obj
1841 * hasn't changed. Could have been deleted due to
1844 mutex_enter(&zp->z_lock);
1845 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1846 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1847 delete_now = may_delete_now && !toobig &&
1848 atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped) &&
1849 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1854 if (xattr_obj_unlinked) {
1855 ASSERT3U(ZTOI(xzp)->i_nlink, ==, 2);
1856 mutex_enter(&xzp->z_lock);
1857 xzp->z_unlinked = B_TRUE;
1858 clear_nlink(ZTOI(xzp));
1860 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1861 &links, sizeof (links), tx);
1862 ASSERT3U(error, ==, 0);
1863 mutex_exit(&xzp->z_lock);
1864 zfs_unlinked_add(xzp, tx);
1867 error = sa_remove(zp->z_sa_hdl,
1868 SA_ZPL_XATTR(zfsvfs), tx);
1870 error = sa_update(zp->z_sa_hdl,
1871 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1872 sizeof (uint64_t), tx);
1876 * Add to the unlinked set because a new reference could be
1877 * taken concurrently resulting in a deferred destruction.
1879 zfs_unlinked_add(zp, tx);
1880 mutex_exit(&zp->z_lock);
1881 } else if (unlinked) {
1882 mutex_exit(&zp->z_lock);
1883 zfs_unlinked_add(zp, tx);
1887 if (flags & FIGNORECASE)
1889 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked);
1896 zfs_dirent_unlock(dl);
1897 zfs_inode_update(dzp);
1898 zfs_inode_update(zp);
1906 zfs_inode_update(xzp);
1907 zfs_iput_async(ZTOI(xzp));
1910 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1911 zil_commit(zilog, 0);
1918 * Create a new directory and insert it into dip using the name
1919 * provided. Return a pointer to the inserted directory.
1921 * IN: dip - inode of directory to add subdir to.
1922 * dirname - name of new directory.
1923 * vap - attributes of new directory.
1924 * cr - credentials of caller.
1925 * flags - case flags.
1926 * vsecp - ACL to be set
1928 * OUT: ipp - inode of created directory.
1930 * RETURN: 0 if success
1931 * error code if failure
1934 * dip - ctime|mtime updated
1935 * ipp - ctime|mtime|atime updated
1939 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1940 cred_t *cr, int flags, vsecattr_t *vsecp)
1942 znode_t *zp, *dzp = ITOZ(dip);
1943 zfsvfs_t *zfsvfs = ITOZSB(dip);
1951 gid_t gid = crgetgid(cr);
1952 zfs_acl_ids_t acl_ids;
1953 boolean_t fuid_dirtied;
1954 boolean_t waited = B_FALSE;
1956 ASSERT(S_ISDIR(vap->va_mode));
1959 * If we have an ephemeral id, ACL, or XVATTR then
1960 * make sure file system is at proper version
1964 if (zfsvfs->z_use_fuids == B_FALSE &&
1965 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1966 return (SET_ERROR(EINVAL));
1968 if (dirname == NULL)
1969 return (SET_ERROR(EINVAL));
1973 zilog = zfsvfs->z_log;
1975 if (dzp->z_pflags & ZFS_XATTR) {
1977 return (SET_ERROR(EINVAL));
1980 if (zfsvfs->z_utf8 && u8_validate(dirname,
1981 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1983 return (SET_ERROR(EILSEQ));
1985 if (flags & FIGNORECASE)
1988 if (vap->va_mask & ATTR_XVATTR) {
1989 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1990 crgetuid(cr), cr, vap->va_mode)) != 0) {
1996 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1997 vsecp, &acl_ids)) != 0) {
2002 * First make sure the new directory doesn't exist.
2004 * Existence is checked first to make sure we don't return
2005 * EACCES instead of EEXIST which can cause some applications
2011 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
2013 zfs_acl_ids_free(&acl_ids);
2018 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
2019 zfs_acl_ids_free(&acl_ids);
2020 zfs_dirent_unlock(dl);
2025 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) {
2026 zfs_acl_ids_free(&acl_ids);
2027 zfs_dirent_unlock(dl);
2029 return (SET_ERROR(EDQUOT));
2033 * Add a new entry to the directory.
2035 tx = dmu_tx_create(zfsvfs->z_os);
2036 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2037 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2038 fuid_dirtied = zfsvfs->z_fuid_dirty;
2040 zfs_fuid_txhold(zfsvfs, tx);
2041 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2042 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2043 acl_ids.z_aclp->z_acl_bytes);
2046 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2047 ZFS_SA_BASE_ATTR_SIZE);
2049 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
2051 zfs_dirent_unlock(dl);
2052 if (error == ERESTART) {
2058 zfs_acl_ids_free(&acl_ids);
2067 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2070 * Now put new name in parent dir.
2072 error = zfs_link_create(dl, zp, tx, ZNEW);
2074 zfs_znode_delete(zp, tx);
2075 remove_inode_hash(ZTOI(zp));
2080 zfs_fuid_sync(zfsvfs, tx);
2084 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2085 if (flags & FIGNORECASE)
2087 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2088 acl_ids.z_fuidp, vap);
2091 zfs_acl_ids_free(&acl_ids);
2095 zfs_dirent_unlock(dl);
2097 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2098 zil_commit(zilog, 0);
2103 zfs_inode_update(dzp);
2104 zfs_inode_update(zp);
2111 * Remove a directory subdir entry. If the current working
2112 * directory is the same as the subdir to be removed, the
2115 * IN: dip - inode of directory to remove from.
2116 * name - name of directory to be removed.
2117 * cwd - inode of current working directory.
2118 * cr - credentials of caller.
2119 * flags - case flags
2121 * RETURN: 0 on success, error code on failure.
2124 * dip - ctime|mtime updated
2128 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
2131 znode_t *dzp = ITOZ(dip);
2134 zfsvfs_t *zfsvfs = ITOZSB(dip);
2140 boolean_t waited = B_FALSE;
2143 return (SET_ERROR(EINVAL));
2147 zilog = zfsvfs->z_log;
2149 if (flags & FIGNORECASE)
2155 * Attempt to lock directory; fail if entry doesn't exist.
2157 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2165 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
2169 if (!S_ISDIR(ip->i_mode)) {
2170 error = SET_ERROR(ENOTDIR);
2175 error = SET_ERROR(EINVAL);
2180 * Grab a lock on the directory to make sure that no one is
2181 * trying to add (or lookup) entries while we are removing it.
2183 rw_enter(&zp->z_name_lock, RW_WRITER);
2186 * Grab a lock on the parent pointer to make sure we play well
2187 * with the treewalk and directory rename code.
2189 rw_enter(&zp->z_parent_lock, RW_WRITER);
2191 tx = dmu_tx_create(zfsvfs->z_os);
2192 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2193 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2194 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2195 zfs_sa_upgrade_txholds(tx, zp);
2196 zfs_sa_upgrade_txholds(tx, dzp);
2197 dmu_tx_mark_netfree(tx);
2198 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
2200 rw_exit(&zp->z_parent_lock);
2201 rw_exit(&zp->z_name_lock);
2202 zfs_dirent_unlock(dl);
2203 if (error == ERESTART) {
2216 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2219 uint64_t txtype = TX_RMDIR;
2220 if (flags & FIGNORECASE)
2222 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT,
2228 rw_exit(&zp->z_parent_lock);
2229 rw_exit(&zp->z_name_lock);
2231 zfs_dirent_unlock(dl);
2233 zfs_inode_update(dzp);
2234 zfs_inode_update(zp);
2237 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2238 zil_commit(zilog, 0);
2245 * Read directory entries from the given directory cursor position and emit
2246 * name and position for each entry.
2248 * IN: ip - inode of directory to read.
2249 * ctx - directory entry context.
2250 * cr - credentials of caller.
2252 * RETURN: 0 if success
2253 * error code if failure
2256 * ip - atime updated
2258 * Note that the low 4 bits of the cookie returned by zap is always zero.
2259 * This allows us to use the low range for "special" directory entries:
2260 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2261 * we use the offset 2 for the '.zfs' directory.
2265 zfs_readdir(struct inode *ip, zpl_dir_context_t *ctx, cred_t *cr)
2267 znode_t *zp = ITOZ(ip);
2268 zfsvfs_t *zfsvfs = ITOZSB(ip);
2271 zap_attribute_t zap;
2277 uint64_t offset; /* must be unsigned; checks for < 1 */
2282 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2283 &parent, sizeof (parent))) != 0)
2287 * Quit if directory has been removed (posix)
2295 prefetch = zp->z_zn_prefetch;
2298 * Initialize the iterator cursor.
2302 * Start iteration from the beginning of the directory.
2304 zap_cursor_init(&zc, os, zp->z_id);
2307 * The offset is a serialized cursor.
2309 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2313 * Transform to file-system independent format
2318 * Special case `.', `..', and `.zfs'.
2321 (void) strcpy(zap.za_name, ".");
2322 zap.za_normalization_conflict = 0;
2325 } else if (offset == 1) {
2326 (void) strcpy(zap.za_name, "..");
2327 zap.za_normalization_conflict = 0;
2330 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2331 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2332 zap.za_normalization_conflict = 0;
2333 objnum = ZFSCTL_INO_ROOT;
2339 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2340 if (error == ENOENT)
2347 * Allow multiple entries provided the first entry is
2348 * the object id. Non-zpl consumers may safely make
2349 * use of the additional space.
2351 * XXX: This should be a feature flag for compatibility
2353 if (zap.za_integer_length != 8 ||
2354 zap.za_num_integers == 0) {
2355 cmn_err(CE_WARN, "zap_readdir: bad directory "
2356 "entry, obj = %lld, offset = %lld, "
2357 "length = %d, num = %lld\n",
2358 (u_longlong_t)zp->z_id,
2359 (u_longlong_t)offset,
2360 zap.za_integer_length,
2361 (u_longlong_t)zap.za_num_integers);
2362 error = SET_ERROR(ENXIO);
2366 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2367 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2370 done = !zpl_dir_emit(ctx, zap.za_name, strlen(zap.za_name),
2375 /* Prefetch znode */
2377 dmu_prefetch(os, objnum, 0, 0, 0,
2378 ZIO_PRIORITY_SYNC_READ);
2382 * Move to the next entry, fill in the previous offset.
2384 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2385 zap_cursor_advance(&zc);
2386 offset = zap_cursor_serialize(&zc);
2392 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2395 zap_cursor_fini(&zc);
2396 if (error == ENOENT)
2404 ulong_t zfs_fsync_sync_cnt = 4;
2407 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2409 znode_t *zp = ITOZ(ip);
2410 zfsvfs_t *zfsvfs = ITOZSB(ip);
2412 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2414 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2417 zil_commit(zfsvfs->z_log, zp->z_id);
2420 tsd_set(zfs_fsyncer_key, NULL);
2427 * Get the requested file attributes and place them in the provided
2430 * IN: ip - inode of file.
2431 * vap - va_mask identifies requested attributes.
2432 * If ATTR_XVATTR set, then optional attrs are requested
2433 * flags - ATTR_NOACLCHECK (CIFS server context)
2434 * cr - credentials of caller.
2436 * OUT: vap - attribute values.
2438 * RETURN: 0 (always succeeds)
2442 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2444 znode_t *zp = ITOZ(ip);
2445 zfsvfs_t *zfsvfs = ITOZSB(ip);
2448 uint64_t atime[2], mtime[2], ctime[2];
2449 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2450 xoptattr_t *xoap = NULL;
2451 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2452 sa_bulk_attr_t bulk[3];
2458 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2460 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
2461 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2462 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2464 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2470 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2471 * Also, if we are the owner don't bother, since owner should
2472 * always be allowed to read basic attributes of file.
2474 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2475 (vap->va_uid != crgetuid(cr))) {
2476 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2484 * Return all attributes. It's cheaper to provide the answer
2485 * than to determine whether we were asked the question.
2488 mutex_enter(&zp->z_lock);
2489 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2490 vap->va_mode = zp->z_mode;
2491 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2492 vap->va_nodeid = zp->z_id;
2493 if ((zp->z_id == zfsvfs->z_root) && zfs_show_ctldir(zp))
2494 links = ZTOI(zp)->i_nlink + 1;
2496 links = ZTOI(zp)->i_nlink;
2497 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2498 vap->va_size = i_size_read(ip);
2499 vap->va_rdev = ip->i_rdev;
2500 vap->va_seq = ip->i_generation;
2503 * Add in any requested optional attributes and the create time.
2504 * Also set the corresponding bits in the returned attribute bitmap.
2506 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2507 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2509 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2510 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2513 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2514 xoap->xoa_readonly =
2515 ((zp->z_pflags & ZFS_READONLY) != 0);
2516 XVA_SET_RTN(xvap, XAT_READONLY);
2519 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2521 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2522 XVA_SET_RTN(xvap, XAT_SYSTEM);
2525 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2527 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2528 XVA_SET_RTN(xvap, XAT_HIDDEN);
2531 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2532 xoap->xoa_nounlink =
2533 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2534 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2537 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2538 xoap->xoa_immutable =
2539 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2540 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2543 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2544 xoap->xoa_appendonly =
2545 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2546 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2549 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2551 ((zp->z_pflags & ZFS_NODUMP) != 0);
2552 XVA_SET_RTN(xvap, XAT_NODUMP);
2555 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2557 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2558 XVA_SET_RTN(xvap, XAT_OPAQUE);
2561 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2562 xoap->xoa_av_quarantined =
2563 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2564 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2567 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2568 xoap->xoa_av_modified =
2569 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2570 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2573 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2574 S_ISREG(ip->i_mode)) {
2575 zfs_sa_get_scanstamp(zp, xvap);
2578 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2581 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2582 times, sizeof (times));
2583 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2584 XVA_SET_RTN(xvap, XAT_CREATETIME);
2587 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2588 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2589 XVA_SET_RTN(xvap, XAT_REPARSE);
2591 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2592 xoap->xoa_generation = ip->i_generation;
2593 XVA_SET_RTN(xvap, XAT_GEN);
2596 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2598 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2599 XVA_SET_RTN(xvap, XAT_OFFLINE);
2602 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2604 ((zp->z_pflags & ZFS_SPARSE) != 0);
2605 XVA_SET_RTN(xvap, XAT_SPARSE);
2608 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
2609 xoap->xoa_projinherit =
2610 ((zp->z_pflags & ZFS_PROJINHERIT) != 0);
2611 XVA_SET_RTN(xvap, XAT_PROJINHERIT);
2614 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2615 xoap->xoa_projid = zp->z_projid;
2616 XVA_SET_RTN(xvap, XAT_PROJID);
2620 ZFS_TIME_DECODE(&vap->va_atime, atime);
2621 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2622 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2624 mutex_exit(&zp->z_lock);
2626 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2628 if (zp->z_blksz == 0) {
2630 * Block size hasn't been set; suggest maximal I/O transfers.
2632 vap->va_blksize = zfsvfs->z_max_blksz;
2640 * Get the basic file attributes and place them in the provided kstat
2641 * structure. The inode is assumed to be the authoritative source
2642 * for most of the attributes. However, the znode currently has the
2643 * authoritative atime, blksize, and block count.
2645 * IN: ip - inode of file.
2647 * OUT: sp - kstat values.
2649 * RETURN: 0 (always succeeds)
2653 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2655 znode_t *zp = ITOZ(ip);
2656 zfsvfs_t *zfsvfs = ITOZSB(ip);
2658 u_longlong_t nblocks;
2663 mutex_enter(&zp->z_lock);
2665 generic_fillattr(ip, sp);
2667 * +1 link count for root inode with visible '.zfs' directory.
2669 if ((zp->z_id == zfsvfs->z_root) && zfs_show_ctldir(zp))
2670 if (sp->nlink < ZFS_LINK_MAX)
2673 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2674 sp->blksize = blksize;
2675 sp->blocks = nblocks;
2677 if (unlikely(zp->z_blksz == 0)) {
2679 * Block size hasn't been set; suggest maximal I/O transfers.
2681 sp->blksize = zfsvfs->z_max_blksz;
2684 mutex_exit(&zp->z_lock);
2687 * Required to prevent NFS client from detecting different inode
2688 * numbers of snapshot root dentry before and after snapshot mount.
2690 if (zfsvfs->z_issnap) {
2691 if (ip->i_sb->s_root->d_inode == ip)
2692 sp->ino = ZFSCTL_INO_SNAPDIRS -
2693 dmu_objset_id(zfsvfs->z_os);
2702 * For the operation of changing file's user/group/project, we need to
2703 * handle not only the main object that is assigned to the file directly,
2704 * but also the ones that are used by the file via hidden xattr directory.
2706 * Because the xattr directory may contains many EA entries, as to it may
2707 * be impossible to change all of them via the transaction of changing the
2708 * main object's user/group/project attributes. Then we have to change them
2709 * via other multiple independent transactions one by one. It may be not good
2710 * solution, but we have no better idea yet.
2713 zfs_setattr_dir(znode_t *dzp)
2715 struct inode *dxip = ZTOI(dzp);
2716 struct inode *xip = NULL;
2717 zfsvfs_t *zfsvfs = ITOZSB(dxip);
2718 objset_t *os = zfsvfs->z_os;
2720 zap_attribute_t zap;
2723 dmu_tx_t *tx = NULL;
2725 sa_bulk_attr_t bulk[4];
2729 zap_cursor_init(&zc, os, dzp->z_id);
2730 while ((err = zap_cursor_retrieve(&zc, &zap)) == 0) {
2732 if (zap.za_integer_length != 8 || zap.za_num_integers != 1) {
2737 err = zfs_dirent_lock(&dl, dzp, (char *)zap.za_name, &zp,
2738 ZEXISTS, NULL, NULL);
2745 if (KUID_TO_SUID(xip->i_uid) == KUID_TO_SUID(dxip->i_uid) &&
2746 KGID_TO_SGID(xip->i_gid) == KGID_TO_SGID(dxip->i_gid) &&
2747 zp->z_projid == dzp->z_projid)
2750 tx = dmu_tx_create(os);
2751 if (!(zp->z_pflags & ZFS_PROJID))
2752 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2754 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2756 err = dmu_tx_assign(tx, TXG_WAIT);
2760 mutex_enter(&dzp->z_lock);
2762 if (KUID_TO_SUID(xip->i_uid) != KUID_TO_SUID(dxip->i_uid)) {
2763 xip->i_uid = dxip->i_uid;
2764 uid = zfs_uid_read(dxip);
2765 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
2766 &uid, sizeof (uid));
2769 if (KGID_TO_SGID(xip->i_gid) != KGID_TO_SGID(dxip->i_gid)) {
2770 xip->i_gid = dxip->i_gid;
2771 gid = zfs_gid_read(dxip);
2772 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
2773 &gid, sizeof (gid));
2776 if (zp->z_projid != dzp->z_projid) {
2777 if (!(zp->z_pflags & ZFS_PROJID)) {
2778 zp->z_pflags |= ZFS_PROJID;
2779 SA_ADD_BULK_ATTR(bulk, count,
2780 SA_ZPL_FLAGS(zfsvfs), NULL, &zp->z_pflags,
2781 sizeof (zp->z_pflags));
2784 zp->z_projid = dzp->z_projid;
2785 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PROJID(zfsvfs),
2786 NULL, &zp->z_projid, sizeof (zp->z_projid));
2789 mutex_exit(&dzp->z_lock);
2791 if (likely(count > 0)) {
2792 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2798 if (err != 0 && err != ENOENT)
2805 zfs_dirent_unlock(dl);
2807 zap_cursor_advance(&zc);
2814 zfs_dirent_unlock(dl);
2816 zap_cursor_fini(&zc);
2818 return (err == ENOENT ? 0 : err);
2822 * Set the file attributes to the values contained in the
2825 * IN: ip - inode of file to be modified.
2826 * vap - new attribute values.
2827 * If ATTR_XVATTR set, then optional attrs are being set
2828 * flags - ATTR_UTIME set if non-default time values provided.
2829 * - ATTR_NOACLCHECK (CIFS context only).
2830 * cr - credentials of caller.
2832 * RETURN: 0 if success
2833 * error code if failure
2836 * ip - ctime updated, mtime updated if size changed.
2840 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2842 znode_t *zp = ITOZ(ip);
2843 zfsvfs_t *zfsvfs = ITOZSB(ip);
2844 objset_t *os = zfsvfs->z_os;
2848 xvattr_t *tmpxvattr;
2849 uint_t mask = vap->va_mask;
2850 uint_t saved_mask = 0;
2853 uint64_t new_kuid = 0, new_kgid = 0, new_uid, new_gid;
2855 uint64_t mtime[2], ctime[2], atime[2];
2856 uint64_t projid = ZFS_INVALID_PROJID;
2858 int need_policy = FALSE;
2860 zfs_fuid_info_t *fuidp = NULL;
2861 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2864 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2865 boolean_t fuid_dirtied = B_FALSE;
2866 boolean_t handle_eadir = B_FALSE;
2867 sa_bulk_attr_t *bulk, *xattr_bulk;
2868 int count = 0, xattr_count = 0, bulks = 8;
2877 * If this is a xvattr_t, then get a pointer to the structure of
2878 * optional attributes. If this is NULL, then we have a vattr_t.
2880 xoap = xva_getxoptattr(xvap);
2881 if (xoap != NULL && (mask & ATTR_XVATTR)) {
2882 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) {
2883 if (!dmu_objset_projectquota_enabled(os) ||
2884 (!S_ISREG(ip->i_mode) && !S_ISDIR(ip->i_mode))) {
2886 return (SET_ERROR(ENOTSUP));
2889 projid = xoap->xoa_projid;
2890 if (unlikely(projid == ZFS_INVALID_PROJID)) {
2892 return (SET_ERROR(EINVAL));
2895 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID)
2896 projid = ZFS_INVALID_PROJID;
2901 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) &&
2902 (xoap->xoa_projinherit !=
2903 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) &&
2904 (!dmu_objset_projectquota_enabled(os) ||
2905 (!S_ISREG(ip->i_mode) && !S_ISDIR(ip->i_mode)))) {
2907 return (SET_ERROR(ENOTSUP));
2911 zilog = zfsvfs->z_log;
2914 * Make sure that if we have ephemeral uid/gid or xvattr specified
2915 * that file system is at proper version level
2918 if (zfsvfs->z_use_fuids == B_FALSE &&
2919 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2920 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2921 (mask & ATTR_XVATTR))) {
2923 return (SET_ERROR(EINVAL));
2926 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2928 return (SET_ERROR(EISDIR));
2931 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2933 return (SET_ERROR(EINVAL));
2936 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP);
2937 xva_init(tmpxvattr);
2939 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * bulks, KM_SLEEP);
2940 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * bulks, KM_SLEEP);
2943 * Immutable files can only alter immutable bit and atime
2945 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2946 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2947 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2948 err = SET_ERROR(EPERM);
2952 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2953 err = SET_ERROR(EPERM);
2958 * Verify timestamps doesn't overflow 32 bits.
2959 * ZFS can handle large timestamps, but 32bit syscalls can't
2960 * handle times greater than 2039. This check should be removed
2961 * once large timestamps are fully supported.
2963 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2964 if (((mask & ATTR_ATIME) &&
2965 TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2966 ((mask & ATTR_MTIME) &&
2967 TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2968 err = SET_ERROR(EOVERFLOW);
2977 /* Can this be moved to before the top label? */
2978 if (zfs_is_readonly(zfsvfs)) {
2979 err = SET_ERROR(EROFS);
2984 * First validate permissions
2987 if (mask & ATTR_SIZE) {
2988 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2993 * XXX - Note, we are not providing any open
2994 * mode flags here (like FNDELAY), so we may
2995 * block if there are locks present... this
2996 * should be addressed in openat().
2998 /* XXX - would it be OK to generate a log record here? */
2999 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
3004 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
3005 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3006 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3007 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3008 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
3009 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
3010 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3011 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
3012 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3016 if (mask & (ATTR_UID|ATTR_GID)) {
3017 int idmask = (mask & (ATTR_UID|ATTR_GID));
3022 * NOTE: even if a new mode is being set,
3023 * we may clear S_ISUID/S_ISGID bits.
3026 if (!(mask & ATTR_MODE))
3027 vap->va_mode = zp->z_mode;
3030 * Take ownership or chgrp to group we are a member of
3033 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
3034 take_group = (mask & ATTR_GID) &&
3035 zfs_groupmember(zfsvfs, vap->va_gid, cr);
3038 * If both ATTR_UID and ATTR_GID are set then take_owner and
3039 * take_group must both be set in order to allow taking
3042 * Otherwise, send the check through secpolicy_vnode_setattr()
3046 if (((idmask == (ATTR_UID|ATTR_GID)) &&
3047 take_owner && take_group) ||
3048 ((idmask == ATTR_UID) && take_owner) ||
3049 ((idmask == ATTR_GID) && take_group)) {
3050 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3051 skipaclchk, cr) == 0) {
3053 * Remove setuid/setgid for non-privileged users
3055 (void) secpolicy_setid_clear(vap, cr);
3056 trim_mask = (mask & (ATTR_UID|ATTR_GID));
3065 mutex_enter(&zp->z_lock);
3066 oldva.va_mode = zp->z_mode;
3067 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
3068 if (mask & ATTR_XVATTR) {
3070 * Update xvattr mask to include only those attributes
3071 * that are actually changing.
3073 * the bits will be restored prior to actually setting
3074 * the attributes so the caller thinks they were set.
3076 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3077 if (xoap->xoa_appendonly !=
3078 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
3081 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
3082 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
3086 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) {
3087 if (xoap->xoa_projinherit !=
3088 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) {
3091 XVA_CLR_REQ(xvap, XAT_PROJINHERIT);
3092 XVA_SET_REQ(tmpxvattr, XAT_PROJINHERIT);
3096 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3097 if (xoap->xoa_nounlink !=
3098 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
3101 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
3102 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
3106 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3107 if (xoap->xoa_immutable !=
3108 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
3111 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
3112 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
3116 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3117 if (xoap->xoa_nodump !=
3118 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
3121 XVA_CLR_REQ(xvap, XAT_NODUMP);
3122 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
3126 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3127 if (xoap->xoa_av_modified !=
3128 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
3131 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
3132 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
3136 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3137 if ((!S_ISREG(ip->i_mode) &&
3138 xoap->xoa_av_quarantined) ||
3139 xoap->xoa_av_quarantined !=
3140 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
3143 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
3144 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
3148 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
3149 mutex_exit(&zp->z_lock);
3150 err = SET_ERROR(EPERM);
3154 if (need_policy == FALSE &&
3155 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3156 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3161 mutex_exit(&zp->z_lock);
3163 if (mask & ATTR_MODE) {
3164 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3165 err = secpolicy_setid_setsticky_clear(ip, vap,
3170 trim_mask |= ATTR_MODE;
3178 * If trim_mask is set then take ownership
3179 * has been granted or write_acl is present and user
3180 * has the ability to modify mode. In that case remove
3181 * UID|GID and or MODE from mask so that
3182 * secpolicy_vnode_setattr() doesn't revoke it.
3186 saved_mask = vap->va_mask;
3187 vap->va_mask &= ~trim_mask;
3189 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
3190 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3195 vap->va_mask |= saved_mask;
3199 * secpolicy_vnode_setattr, or take ownership may have
3202 mask = vap->va_mask;
3204 if ((mask & (ATTR_UID | ATTR_GID)) || projid != ZFS_INVALID_PROJID) {
3205 handle_eadir = B_TRUE;
3206 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3207 &xattr_obj, sizeof (xattr_obj));
3209 if (err == 0 && xattr_obj) {
3210 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
3214 if (mask & ATTR_UID) {
3215 new_kuid = zfs_fuid_create(zfsvfs,
3216 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3217 if (new_kuid != KUID_TO_SUID(ZTOI(zp)->i_uid) &&
3218 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT,
3222 err = SET_ERROR(EDQUOT);
3227 if (mask & ATTR_GID) {
3228 new_kgid = zfs_fuid_create(zfsvfs,
3229 (uint64_t)vap->va_gid, cr, ZFS_GROUP, &fuidp);
3230 if (new_kgid != KGID_TO_SGID(ZTOI(zp)->i_gid) &&
3231 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT,
3235 err = SET_ERROR(EDQUOT);
3240 if (projid != ZFS_INVALID_PROJID &&
3241 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) {
3248 tx = dmu_tx_create(os);
3250 if (mask & ATTR_MODE) {
3251 uint64_t pmode = zp->z_mode;
3253 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3255 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
3257 mutex_enter(&zp->z_lock);
3258 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3260 * Are we upgrading ACL from old V0 format
3263 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3264 zfs_znode_acl_version(zp) ==
3265 ZFS_ACL_VERSION_INITIAL) {
3266 dmu_tx_hold_free(tx, acl_obj, 0,
3268 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3269 0, aclp->z_acl_bytes);
3271 dmu_tx_hold_write(tx, acl_obj, 0,
3274 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3275 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3276 0, aclp->z_acl_bytes);
3278 mutex_exit(&zp->z_lock);
3279 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3281 if (((mask & ATTR_XVATTR) &&
3282 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
3283 (projid != ZFS_INVALID_PROJID &&
3284 !(zp->z_pflags & ZFS_PROJID)))
3285 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3287 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3291 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3294 fuid_dirtied = zfsvfs->z_fuid_dirty;
3296 zfs_fuid_txhold(zfsvfs, tx);
3298 zfs_sa_upgrade_txholds(tx, zp);
3300 err = dmu_tx_assign(tx, TXG_WAIT);
3306 * Set each attribute requested.
3307 * We group settings according to the locks they need to acquire.
3309 * Note: you cannot set ctime directly, although it will be
3310 * updated as a side-effect of calling this function.
3313 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) {
3315 * For the existed object that is upgraded from old system,
3316 * its on-disk layout has no slot for the project ID attribute.
3317 * But quota accounting logic needs to access related slots by
3318 * offset directly. So we need to adjust old objects' layout
3319 * to make the project ID to some unified and fixed offset.
3322 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid);
3324 err = sa_add_projid(zp->z_sa_hdl, tx, projid);
3326 if (unlikely(err == EEXIST))
3331 projid = ZFS_INVALID_PROJID;
3334 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3335 mutex_enter(&zp->z_acl_lock);
3336 mutex_enter(&zp->z_lock);
3338 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3339 &zp->z_pflags, sizeof (zp->z_pflags));
3342 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3343 mutex_enter(&attrzp->z_acl_lock);
3344 mutex_enter(&attrzp->z_lock);
3345 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3346 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3347 sizeof (attrzp->z_pflags));
3348 if (projid != ZFS_INVALID_PROJID) {
3349 attrzp->z_projid = projid;
3350 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3351 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid,
3352 sizeof (attrzp->z_projid));
3356 if (mask & (ATTR_UID|ATTR_GID)) {
3358 if (mask & ATTR_UID) {
3359 ZTOI(zp)->i_uid = SUID_TO_KUID(new_kuid);
3360 new_uid = zfs_uid_read(ZTOI(zp));
3361 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3362 &new_uid, sizeof (new_uid));
3364 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3365 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3367 ZTOI(attrzp)->i_uid = SUID_TO_KUID(new_uid);
3371 if (mask & ATTR_GID) {
3372 ZTOI(zp)->i_gid = SGID_TO_KGID(new_kgid);
3373 new_gid = zfs_gid_read(ZTOI(zp));
3374 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3375 NULL, &new_gid, sizeof (new_gid));
3377 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3378 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3380 ZTOI(attrzp)->i_gid = SGID_TO_KGID(new_kgid);
3383 if (!(mask & ATTR_MODE)) {
3384 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3385 NULL, &new_mode, sizeof (new_mode));
3386 new_mode = zp->z_mode;
3388 err = zfs_acl_chown_setattr(zp);
3391 err = zfs_acl_chown_setattr(attrzp);
3396 if (mask & ATTR_MODE) {
3397 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3398 &new_mode, sizeof (new_mode));
3399 zp->z_mode = ZTOI(zp)->i_mode = new_mode;
3400 ASSERT3P(aclp, !=, NULL);
3401 err = zfs_aclset_common(zp, aclp, cr, tx);
3403 if (zp->z_acl_cached)
3404 zfs_acl_free(zp->z_acl_cached);
3405 zp->z_acl_cached = aclp;
3409 if ((mask & ATTR_ATIME) || zp->z_atime_dirty) {
3410 zp->z_atime_dirty = B_FALSE;
3411 ZFS_TIME_ENCODE(&ip->i_atime, atime);
3412 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3413 &atime, sizeof (atime));
3416 if (mask & (ATTR_MTIME | ATTR_SIZE)) {
3417 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3418 ZTOI(zp)->i_mtime = zpl_inode_timespec_trunc(vap->va_mtime,
3419 ZTOI(zp)->i_sb->s_time_gran);
3421 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3422 mtime, sizeof (mtime));
3425 if (mask & (ATTR_CTIME | ATTR_SIZE)) {
3426 ZFS_TIME_ENCODE(&vap->va_ctime, ctime);
3427 ZTOI(zp)->i_ctime = zpl_inode_timespec_trunc(vap->va_ctime,
3428 ZTOI(zp)->i_sb->s_time_gran);
3429 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3430 ctime, sizeof (ctime));
3433 if (projid != ZFS_INVALID_PROJID) {
3434 zp->z_projid = projid;
3435 SA_ADD_BULK_ATTR(bulk, count,
3436 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid,
3437 sizeof (zp->z_projid));
3440 if (attrzp && mask) {
3441 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3442 SA_ZPL_CTIME(zfsvfs), NULL, &ctime,
3447 * Do this after setting timestamps to prevent timestamp
3448 * update from toggling bit
3451 if (xoap && (mask & ATTR_XVATTR)) {
3454 * restore trimmed off masks
3455 * so that return masks can be set for caller.
3458 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
3459 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3461 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
3462 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3464 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
3465 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3467 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
3468 XVA_SET_REQ(xvap, XAT_NODUMP);
3470 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
3471 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3473 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
3474 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3476 if (XVA_ISSET_REQ(tmpxvattr, XAT_PROJINHERIT)) {
3477 XVA_SET_REQ(xvap, XAT_PROJINHERIT);
3480 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3481 ASSERT(S_ISREG(ip->i_mode));
3483 zfs_xvattr_set(zp, xvap, tx);
3487 zfs_fuid_sync(zfsvfs, tx);
3490 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3492 mutex_exit(&zp->z_lock);
3493 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3494 mutex_exit(&zp->z_acl_lock);
3497 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3498 mutex_exit(&attrzp->z_acl_lock);
3499 mutex_exit(&attrzp->z_lock);
3502 if (err == 0 && xattr_count > 0) {
3503 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3512 zfs_fuid_info_free(fuidp);
3520 if (err == ERESTART)
3524 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3527 if (err2 == 0 && handle_eadir)
3528 err2 = zfs_setattr_dir(attrzp);
3531 zfs_inode_update(zp);
3535 if (os->os_sync == ZFS_SYNC_ALWAYS)
3536 zil_commit(zilog, 0);
3539 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * bulks);
3540 kmem_free(bulk, sizeof (sa_bulk_attr_t) * bulks);
3541 kmem_free(tmpxvattr, sizeof (xvattr_t));
3546 typedef struct zfs_zlock {
3547 krwlock_t *zl_rwlock; /* lock we acquired */
3548 znode_t *zl_znode; /* znode we held */
3549 struct zfs_zlock *zl_next; /* next in list */
3553 * Drop locks and release vnodes that were held by zfs_rename_lock().
3556 zfs_rename_unlock(zfs_zlock_t **zlpp)
3560 while ((zl = *zlpp) != NULL) {
3561 if (zl->zl_znode != NULL)
3562 zfs_iput_async(ZTOI(zl->zl_znode));
3563 rw_exit(zl->zl_rwlock);
3564 *zlpp = zl->zl_next;
3565 kmem_free(zl, sizeof (*zl));
3570 * Search back through the directory tree, using the ".." entries.
3571 * Lock each directory in the chain to prevent concurrent renames.
3572 * Fail any attempt to move a directory into one of its own descendants.
3573 * XXX - z_parent_lock can overlap with map or grow locks
3576 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3580 uint64_t rootid = ZTOZSB(zp)->z_root;
3581 uint64_t oidp = zp->z_id;
3582 krwlock_t *rwlp = &szp->z_parent_lock;
3583 krw_t rw = RW_WRITER;
3586 * First pass write-locks szp and compares to zp->z_id.
3587 * Later passes read-lock zp and compare to zp->z_parent.
3590 if (!rw_tryenter(rwlp, rw)) {
3592 * Another thread is renaming in this path.
3593 * Note that if we are a WRITER, we don't have any
3594 * parent_locks held yet.
3596 if (rw == RW_READER && zp->z_id > szp->z_id) {
3598 * Drop our locks and restart
3600 zfs_rename_unlock(&zl);
3604 rwlp = &szp->z_parent_lock;
3609 * Wait for other thread to drop its locks
3615 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3616 zl->zl_rwlock = rwlp;
3617 zl->zl_znode = NULL;
3618 zl->zl_next = *zlpp;
3621 if (oidp == szp->z_id) /* We're a descendant of szp */
3622 return (SET_ERROR(EINVAL));
3624 if (oidp == rootid) /* We've hit the top */
3627 if (rw == RW_READER) { /* i.e. not the first pass */
3628 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3633 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3634 &oidp, sizeof (oidp));
3635 rwlp = &zp->z_parent_lock;
3638 } while (zp->z_id != sdzp->z_id);
3644 * Move an entry from the provided source directory to the target
3645 * directory. Change the entry name as indicated.
3647 * IN: sdip - Source directory containing the "old entry".
3648 * snm - Old entry name.
3649 * tdip - Target directory to contain the "new entry".
3650 * tnm - New entry name.
3651 * cr - credentials of caller.
3652 * flags - case flags
3654 * RETURN: 0 on success, error code on failure.
3657 * sdip,tdip - ctime|mtime updated
3661 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3662 cred_t *cr, int flags)
3664 znode_t *tdzp, *szp, *tzp;
3665 znode_t *sdzp = ITOZ(sdip);
3666 zfsvfs_t *zfsvfs = ITOZSB(sdip);
3668 zfs_dirlock_t *sdl, *tdl;
3671 int cmp, serr, terr;
3674 boolean_t waited = B_FALSE;
3676 if (snm == NULL || tnm == NULL)
3677 return (SET_ERROR(EINVAL));
3680 ZFS_VERIFY_ZP(sdzp);
3681 zilog = zfsvfs->z_log;
3684 ZFS_VERIFY_ZP(tdzp);
3687 * We check i_sb because snapshots and the ctldir must have different
3690 if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) {
3692 return (SET_ERROR(EXDEV));
3695 if (zfsvfs->z_utf8 && u8_validate(tnm,
3696 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3698 return (SET_ERROR(EILSEQ));
3701 if (flags & FIGNORECASE)
3710 * This is to prevent the creation of links into attribute space
3711 * by renaming a linked file into/outof an attribute directory.
3712 * See the comment in zfs_link() for why this is considered bad.
3714 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3716 return (SET_ERROR(EINVAL));
3720 * Lock source and target directory entries. To prevent deadlock,
3721 * a lock ordering must be defined. We lock the directory with
3722 * the smallest object id first, or if it's a tie, the one with
3723 * the lexically first name.
3725 if (sdzp->z_id < tdzp->z_id) {
3727 } else if (sdzp->z_id > tdzp->z_id) {
3731 * First compare the two name arguments without
3732 * considering any case folding.
3734 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3736 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3737 ASSERT(error == 0 || !zfsvfs->z_utf8);
3740 * POSIX: "If the old argument and the new argument
3741 * both refer to links to the same existing file,
3742 * the rename() function shall return successfully
3743 * and perform no other action."
3749 * If the file system is case-folding, then we may
3750 * have some more checking to do. A case-folding file
3751 * system is either supporting mixed case sensitivity
3752 * access or is completely case-insensitive. Note
3753 * that the file system is always case preserving.
3755 * In mixed sensitivity mode case sensitive behavior
3756 * is the default. FIGNORECASE must be used to
3757 * explicitly request case insensitive behavior.
3759 * If the source and target names provided differ only
3760 * by case (e.g., a request to rename 'tim' to 'Tim'),
3761 * we will treat this as a special case in the
3762 * case-insensitive mode: as long as the source name
3763 * is an exact match, we will allow this to proceed as
3764 * a name-change request.
3766 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3767 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3768 flags & FIGNORECASE)) &&
3769 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3772 * case preserving rename request, require exact
3781 * If the source and destination directories are the same, we should
3782 * grab the z_name_lock of that directory only once.
3786 rw_enter(&sdzp->z_name_lock, RW_READER);
3790 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3791 ZEXISTS | zflg, NULL, NULL);
3792 terr = zfs_dirent_lock(&tdl,
3793 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3795 terr = zfs_dirent_lock(&tdl,
3796 tdzp, tnm, &tzp, zflg, NULL, NULL);
3797 serr = zfs_dirent_lock(&sdl,
3798 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3804 * Source entry invalid or not there.
3807 zfs_dirent_unlock(tdl);
3813 rw_exit(&sdzp->z_name_lock);
3815 if (strcmp(snm, "..") == 0)
3821 zfs_dirent_unlock(sdl);
3825 rw_exit(&sdzp->z_name_lock);
3827 if (strcmp(tnm, "..") == 0)
3834 * If we are using project inheritance, means if the directory has
3835 * ZFS_PROJINHERIT set, then its descendant directories will inherit
3836 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
3837 * such case, we only allow renames into our tree when the project
3840 if (tdzp->z_pflags & ZFS_PROJINHERIT &&
3841 tdzp->z_projid != szp->z_projid) {
3842 error = SET_ERROR(EXDEV);
3847 * Must have write access at the source to remove the old entry
3848 * and write access at the target to create the new entry.
3849 * Note that if target and source are the same, this can be
3850 * done in a single check.
3853 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3856 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3858 * Check to make sure rename is valid.
3859 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3861 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3866 * Does target exist?
3870 * Source and target must be the same type.
3872 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3873 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3874 error = SET_ERROR(ENOTDIR);
3878 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3879 error = SET_ERROR(EISDIR);
3884 * POSIX dictates that when the source and target
3885 * entries refer to the same file object, rename
3886 * must do nothing and exit without error.
3888 if (szp->z_id == tzp->z_id) {
3894 tx = dmu_tx_create(zfsvfs->z_os);
3895 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3896 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3897 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3898 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3900 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3901 zfs_sa_upgrade_txholds(tx, tdzp);
3904 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3905 zfs_sa_upgrade_txholds(tx, tzp);
3908 zfs_sa_upgrade_txholds(tx, szp);
3909 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3910 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
3913 zfs_rename_unlock(&zl);
3914 zfs_dirent_unlock(sdl);
3915 zfs_dirent_unlock(tdl);
3918 rw_exit(&sdzp->z_name_lock);
3920 if (error == ERESTART) {
3937 if (tzp) /* Attempt to remove the existing target */
3938 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3941 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3943 szp->z_pflags |= ZFS_AV_MODIFIED;
3944 if (tdzp->z_pflags & ZFS_PROJINHERIT)
3945 szp->z_pflags |= ZFS_PROJINHERIT;
3947 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3948 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3951 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3953 zfs_log_rename(zilog, tx, TX_RENAME |
3954 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3955 sdl->dl_name, tdzp, tdl->dl_name, szp);
3958 * At this point, we have successfully created
3959 * the target name, but have failed to remove
3960 * the source name. Since the create was done
3961 * with the ZRENAMING flag, there are
3962 * complications; for one, the link count is
3963 * wrong. The easiest way to deal with this
3964 * is to remove the newly created target, and
3965 * return the original error. This must
3966 * succeed; fortunately, it is very unlikely to
3967 * fail, since we just created it.
3969 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3970 ZRENAMING, NULL), ==, 0);
3974 * If we had removed the existing target, subsequent
3975 * call to zfs_link_create() to add back the same entry
3976 * but, the new dnode (szp) should not fail.
3978 ASSERT(tzp == NULL);
3985 zfs_rename_unlock(&zl);
3987 zfs_dirent_unlock(sdl);
3988 zfs_dirent_unlock(tdl);
3990 zfs_inode_update(sdzp);
3992 rw_exit(&sdzp->z_name_lock);
3995 zfs_inode_update(tdzp);
3997 zfs_inode_update(szp);
4000 zfs_inode_update(tzp);
4004 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4005 zil_commit(zilog, 0);
4012 * Insert the indicated symbolic reference entry into the directory.
4014 * IN: dip - Directory to contain new symbolic link.
4015 * name - Name of directory entry in dip.
4016 * vap - Attributes of new entry.
4017 * link - Name for new symlink entry.
4018 * cr - credentials of caller.
4019 * flags - case flags
4021 * OUT: ipp - Inode for new symbolic link.
4023 * RETURN: 0 on success, error code on failure.
4026 * dip - ctime|mtime updated
4030 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
4031 struct inode **ipp, cred_t *cr, int flags)
4033 znode_t *zp, *dzp = ITOZ(dip);
4036 zfsvfs_t *zfsvfs = ITOZSB(dip);
4038 uint64_t len = strlen(link);
4041 zfs_acl_ids_t acl_ids;
4042 boolean_t fuid_dirtied;
4043 uint64_t txtype = TX_SYMLINK;
4044 boolean_t waited = B_FALSE;
4046 ASSERT(S_ISLNK(vap->va_mode));
4049 return (SET_ERROR(EINVAL));
4053 zilog = zfsvfs->z_log;
4055 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
4056 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4058 return (SET_ERROR(EILSEQ));
4060 if (flags & FIGNORECASE)
4063 if (len > MAXPATHLEN) {
4065 return (SET_ERROR(ENAMETOOLONG));
4068 if ((error = zfs_acl_ids_create(dzp, 0,
4069 vap, cr, NULL, &acl_ids)) != 0) {
4077 * Attempt to lock directory; fail if entry already exists.
4079 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
4081 zfs_acl_ids_free(&acl_ids);
4086 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
4087 zfs_acl_ids_free(&acl_ids);
4088 zfs_dirent_unlock(dl);
4093 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, ZFS_DEFAULT_PROJID)) {
4094 zfs_acl_ids_free(&acl_ids);
4095 zfs_dirent_unlock(dl);
4097 return (SET_ERROR(EDQUOT));
4099 tx = dmu_tx_create(zfsvfs->z_os);
4100 fuid_dirtied = zfsvfs->z_fuid_dirty;
4101 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
4102 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4103 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
4104 ZFS_SA_BASE_ATTR_SIZE + len);
4105 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
4106 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
4107 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
4108 acl_ids.z_aclp->z_acl_bytes);
4111 zfs_fuid_txhold(zfsvfs, tx);
4112 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
4114 zfs_dirent_unlock(dl);
4115 if (error == ERESTART) {
4121 zfs_acl_ids_free(&acl_ids);
4128 * Create a new object for the symlink.
4129 * for version 4 ZPL datsets the symlink will be an SA attribute
4131 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
4134 zfs_fuid_sync(zfsvfs, tx);
4136 mutex_enter(&zp->z_lock);
4138 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
4141 zfs_sa_symlink(zp, link, len, tx);
4142 mutex_exit(&zp->z_lock);
4145 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
4146 &zp->z_size, sizeof (zp->z_size), tx);
4148 * Insert the new object into the directory.
4150 error = zfs_link_create(dl, zp, tx, ZNEW);
4152 zfs_znode_delete(zp, tx);
4153 remove_inode_hash(ZTOI(zp));
4155 if (flags & FIGNORECASE)
4157 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
4159 zfs_inode_update(dzp);
4160 zfs_inode_update(zp);
4163 zfs_acl_ids_free(&acl_ids);
4167 zfs_dirent_unlock(dl);
4172 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4173 zil_commit(zilog, 0);
4183 * Return, in the buffer contained in the provided uio structure,
4184 * the symbolic path referred to by ip.
4186 * IN: ip - inode of symbolic link
4187 * uio - structure to contain the link path.
4188 * cr - credentials of caller.
4190 * RETURN: 0 if success
4191 * error code if failure
4194 * ip - atime updated
4198 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
4200 znode_t *zp = ITOZ(ip);
4201 zfsvfs_t *zfsvfs = ITOZSB(ip);
4207 mutex_enter(&zp->z_lock);
4209 error = sa_lookup_uio(zp->z_sa_hdl,
4210 SA_ZPL_SYMLINK(zfsvfs), uio);
4212 error = zfs_sa_readlink(zp, uio);
4213 mutex_exit(&zp->z_lock);
4220 * Insert a new entry into directory tdip referencing sip.
4222 * IN: tdip - Directory to contain new entry.
4223 * sip - inode of new entry.
4224 * name - name of new entry.
4225 * cr - credentials of caller.
4226 * flags - case flags.
4228 * RETURN: 0 if success
4229 * error code if failure
4232 * tdip - ctime|mtime updated
4233 * sip - ctime updated
4237 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr,
4240 znode_t *dzp = ITOZ(tdip);
4242 zfsvfs_t *zfsvfs = ITOZSB(tdip);
4250 boolean_t waited = B_FALSE;
4251 boolean_t is_tmpfile = 0;
4254 is_tmpfile = (sip->i_nlink == 0 && (sip->i_state & I_LINKABLE));
4256 ASSERT(S_ISDIR(tdip->i_mode));
4259 return (SET_ERROR(EINVAL));
4263 zilog = zfsvfs->z_log;
4266 * POSIX dictates that we return EPERM here.
4267 * Better choices include ENOTSUP or EISDIR.
4269 if (S_ISDIR(sip->i_mode)) {
4271 return (SET_ERROR(EPERM));
4278 * If we are using project inheritance, means if the directory has
4279 * ZFS_PROJINHERIT set, then its descendant directories will inherit
4280 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under
4281 * such case, we only allow hard link creation in our tree when the
4282 * project IDs are the same.
4284 if (dzp->z_pflags & ZFS_PROJINHERIT && dzp->z_projid != szp->z_projid) {
4286 return (SET_ERROR(EXDEV));
4290 * We check i_sb because snapshots and the ctldir must have different
4293 if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) {
4295 return (SET_ERROR(EXDEV));
4298 /* Prevent links to .zfs/shares files */
4300 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4301 &parent, sizeof (uint64_t))) != 0) {
4305 if (parent == zfsvfs->z_shares_dir) {
4307 return (SET_ERROR(EPERM));
4310 if (zfsvfs->z_utf8 && u8_validate(name,
4311 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4313 return (SET_ERROR(EILSEQ));
4315 if (flags & FIGNORECASE)
4319 * We do not support links between attributes and non-attributes
4320 * because of the potential security risk of creating links
4321 * into "normal" file space in order to circumvent restrictions
4322 * imposed in attribute space.
4324 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4326 return (SET_ERROR(EINVAL));
4329 owner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(sip->i_uid),
4331 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4333 return (SET_ERROR(EPERM));
4336 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
4343 * Attempt to lock directory; fail if entry already exists.
4345 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4351 tx = dmu_tx_create(zfsvfs->z_os);
4352 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4353 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4355 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
4357 zfs_sa_upgrade_txholds(tx, szp);
4358 zfs_sa_upgrade_txholds(tx, dzp);
4359 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
4361 zfs_dirent_unlock(dl);
4362 if (error == ERESTART) {
4372 /* unmark z_unlinked so zfs_link_create will not reject */
4374 szp->z_unlinked = B_FALSE;
4375 error = zfs_link_create(dl, szp, tx, 0);
4378 uint64_t txtype = TX_LINK;
4380 * tmpfile is created to be in z_unlinkedobj, so remove it.
4381 * Also, we don't log in ZIL, because all previous file
4382 * operation on the tmpfile are ignored by ZIL. Instead we
4383 * always wait for txg to sync to make sure all previous
4384 * operation are sync safe.
4387 VERIFY(zap_remove_int(zfsvfs->z_os,
4388 zfsvfs->z_unlinkedobj, szp->z_id, tx) == 0);
4390 if (flags & FIGNORECASE)
4392 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4394 } else if (is_tmpfile) {
4395 /* restore z_unlinked since when linking failed */
4396 szp->z_unlinked = B_TRUE;
4398 txg = dmu_tx_get_txg(tx);
4401 zfs_dirent_unlock(dl);
4403 if (!is_tmpfile && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4404 zil_commit(zilog, 0);
4407 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), txg);
4409 zfs_inode_update(dzp);
4410 zfs_inode_update(szp);
4416 zfs_putpage_commit_cb(void *arg)
4418 struct page *pp = arg;
4421 end_page_writeback(pp);
4425 * Push a page out to disk, once the page is on stable storage the
4426 * registered commit callback will be run as notification of completion.
4428 * IN: ip - page mapped for inode.
4429 * pp - page to push (page is locked)
4430 * wbc - writeback control data
4432 * RETURN: 0 if success
4433 * error code if failure
4436 * ip - ctime|mtime updated
4440 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
4442 znode_t *zp = ITOZ(ip);
4443 zfsvfs_t *zfsvfs = ITOZSB(ip);
4450 uint64_t mtime[2], ctime[2];
4451 sa_bulk_attr_t bulk[3];
4453 struct address_space *mapping;
4458 ASSERT(PageLocked(pp));
4460 pgoff = page_offset(pp); /* Page byte-offset in file */
4461 offset = i_size_read(ip); /* File length in bytes */
4462 pglen = MIN(PAGE_SIZE, /* Page length in bytes */
4463 P2ROUNDUP(offset, PAGE_SIZE)-pgoff);
4465 /* Page is beyond end of file */
4466 if (pgoff >= offset) {
4472 /* Truncate page length to end of file */
4473 if (pgoff + pglen > offset)
4474 pglen = offset - pgoff;
4478 * FIXME: Allow mmap writes past its quota. The correct fix
4479 * is to register a page_mkwrite() handler to count the page
4480 * against its quota when it is about to be dirtied.
4482 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT,
4483 KUID_TO_SUID(ip->i_uid)) ||
4484 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT,
4485 KGID_TO_SGID(ip->i_gid)) ||
4486 (zp->z_projid != ZFS_DEFAULT_PROJID &&
4487 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT,
4494 * The ordering here is critical and must adhere to the following
4495 * rules in order to avoid deadlocking in either zfs_read() or
4496 * zfs_free_range() due to a lock inversion.
4498 * 1) The page must be unlocked prior to acquiring the range lock.
4499 * This is critical because zfs_read() calls find_lock_page()
4500 * which may block on the page lock while holding the range lock.
4502 * 2) Before setting or clearing write back on a page the range lock
4503 * must be held in order to prevent a lock inversion with the
4504 * zfs_free_range() function.
4506 * This presents a problem because upon entering this function the
4507 * page lock is already held. To safely acquire the range lock the
4508 * page lock must be dropped. This creates a window where another
4509 * process could truncate, invalidate, dirty, or write out the page.
4511 * Therefore, after successfully reacquiring the range and page locks
4512 * the current page state is checked. In the common case everything
4513 * will be as is expected and it can be written out. However, if
4514 * the page state has changed it must be handled accordingly.
4516 mapping = pp->mapping;
4517 redirty_page_for_writepage(wbc, pp);
4520 locked_range_t *lr = rangelock_enter(&zp->z_rangelock,
4521 pgoff, pglen, RL_WRITER);
4524 /* Page mapping changed or it was no longer dirty, we're done */
4525 if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) {
4532 /* Another process started write block if required */
4533 if (PageWriteback(pp)) {
4537 if (wbc->sync_mode != WB_SYNC_NONE) {
4538 if (PageWriteback(pp))
4539 wait_on_page_bit(pp, PG_writeback);
4546 /* Clear the dirty flag the required locks are held */
4547 if (!clear_page_dirty_for_io(pp)) {
4555 * Counterpart for redirty_page_for_writepage() above. This page
4556 * was in fact not skipped and should not be counted as if it were.
4558 wbc->pages_skipped--;
4559 set_page_writeback(pp);
4562 tx = dmu_tx_create(zfsvfs->z_os);
4563 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
4564 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4565 zfs_sa_upgrade_txholds(tx, zp);
4567 err = dmu_tx_assign(tx, TXG_NOWAIT);
4569 if (err == ERESTART)
4573 __set_page_dirty_nobuffers(pp);
4575 end_page_writeback(pp);
4582 ASSERT3U(pglen, <=, PAGE_SIZE);
4583 dmu_write(zfsvfs->z_os, zp->z_id, pgoff, pglen, va, tx);
4586 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
4587 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
4588 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zfsvfs), NULL,
4591 /* Preserve the mtime and ctime provided by the inode */
4592 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4593 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4594 zp->z_atime_dirty = B_FALSE;
4597 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4599 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
4600 zfs_putpage_commit_cb, pp);
4605 if (wbc->sync_mode != WB_SYNC_NONE) {
4607 * Note that this is rarely called under writepages(), because
4608 * writepages() normally handles the entire commit for
4609 * performance reasons.
4611 zil_commit(zfsvfs->z_log, zp->z_id);
4619 * Update the system attributes when the inode has been dirtied. For the
4620 * moment we only update the mode, atime, mtime, and ctime.
4623 zfs_dirty_inode(struct inode *ip, int flags)
4625 znode_t *zp = ITOZ(ip);
4626 zfsvfs_t *zfsvfs = ITOZSB(ip);
4628 uint64_t mode, atime[2], mtime[2], ctime[2];
4629 sa_bulk_attr_t bulk[4];
4633 if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os))
4641 * This is the lazytime semantic introduced in Linux 4.0
4642 * This flag will only be called from update_time when lazytime is set.
4643 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4644 * Fortunately mtime and ctime are managed within ZFS itself, so we
4645 * only need to dirty atime.
4647 if (flags == I_DIRTY_TIME) {
4648 zp->z_atime_dirty = B_TRUE;
4653 tx = dmu_tx_create(zfsvfs->z_os);
4655 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4656 zfs_sa_upgrade_txholds(tx, zp);
4658 error = dmu_tx_assign(tx, TXG_WAIT);
4664 mutex_enter(&zp->z_lock);
4665 zp->z_atime_dirty = B_FALSE;
4667 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
4668 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
4669 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
4670 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
4672 /* Preserve the mode, mtime and ctime provided by the inode */
4673 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4674 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4675 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4680 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4681 mutex_exit(&zp->z_lock);
4691 zfs_inactive(struct inode *ip)
4693 znode_t *zp = ITOZ(ip);
4694 zfsvfs_t *zfsvfs = ITOZSB(ip);
4697 int need_unlock = 0;
4699 /* Only read lock if we haven't already write locked, e.g. rollback */
4700 if (!RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)) {
4702 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4704 if (zp->z_sa_hdl == NULL) {
4706 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4710 if (zp->z_atime_dirty && zp->z_unlinked == B_FALSE) {
4711 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4713 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4714 zfs_sa_upgrade_txholds(tx, zp);
4715 error = dmu_tx_assign(tx, TXG_WAIT);
4719 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4720 mutex_enter(&zp->z_lock);
4721 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4722 (void *)&atime, sizeof (atime), tx);
4723 zp->z_atime_dirty = B_FALSE;
4724 mutex_exit(&zp->z_lock);
4731 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4735 * Bounds-check the seek operation.
4737 * IN: ip - inode seeking within
4738 * ooff - old file offset
4739 * noffp - pointer to new file offset
4741 * RETURN: 0 if success
4742 * EINVAL if new offset invalid
4746 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4748 if (S_ISDIR(ip->i_mode))
4750 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4754 * Fill pages with data from the disk.
4757 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4759 znode_t *zp = ITOZ(ip);
4760 zfsvfs_t *zfsvfs = ITOZSB(ip);
4762 struct page *cur_pp;
4763 u_offset_t io_off, total;
4770 io_len = nr_pages << PAGE_SHIFT;
4771 i_size = i_size_read(ip);
4772 io_off = page_offset(pl[0]);
4774 if (io_off + io_len > i_size)
4775 io_len = i_size - io_off;
4778 * Iterate over list of pages and read each page individually.
4781 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4784 cur_pp = pl[page_idx++];
4786 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4790 /* convert checksum errors into IO errors */
4792 err = SET_ERROR(EIO);
4801 * Uses zfs_fillpage to read data from the file and fill the pages.
4803 * IN: ip - inode of file to get data from.
4804 * pl - list of pages to read
4805 * nr_pages - number of pages to read
4807 * RETURN: 0 on success, error code on failure.
4810 * vp - atime updated
4814 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4816 znode_t *zp = ITOZ(ip);
4817 zfsvfs_t *zfsvfs = ITOZSB(ip);
4826 err = zfs_fillpage(ip, pl, nr_pages);
4833 * Check ZFS specific permissions to memory map a section of a file.
4835 * IN: ip - inode of the file to mmap
4837 * addrp - start address in memory region
4838 * len - length of memory region
4839 * vm_flags- address flags
4841 * RETURN: 0 if success
4842 * error code if failure
4846 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4847 unsigned long vm_flags)
4849 znode_t *zp = ITOZ(ip);
4850 zfsvfs_t *zfsvfs = ITOZSB(ip);
4855 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4856 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4858 return (SET_ERROR(EPERM));
4861 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4862 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4864 return (SET_ERROR(EACCES));
4867 if (off < 0 || len > MAXOFFSET_T - off) {
4869 return (SET_ERROR(ENXIO));
4877 * convoff - converts the given data (start, whence) to the
4881 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4886 if ((lckdat->l_whence == SEEK_END) || (whence == SEEK_END)) {
4887 if ((error = zfs_getattr(ip, &vap, 0, CRED())))
4891 switch (lckdat->l_whence) {
4893 lckdat->l_start += offset;
4896 lckdat->l_start += vap.va_size;
4901 return (SET_ERROR(EINVAL));
4904 if (lckdat->l_start < 0)
4905 return (SET_ERROR(EINVAL));
4909 lckdat->l_start -= offset;
4912 lckdat->l_start -= vap.va_size;
4917 return (SET_ERROR(EINVAL));
4920 lckdat->l_whence = (short)whence;
4925 * Free or allocate space in a file. Currently, this function only
4926 * supports the `F_FREESP' command. However, this command is somewhat
4927 * misnamed, as its functionality includes the ability to allocate as
4928 * well as free space.
4930 * IN: ip - inode of file to free data in.
4931 * cmd - action to take (only F_FREESP supported).
4932 * bfp - section of file to free/alloc.
4933 * flag - current file open mode flags.
4934 * offset - current file offset.
4935 * cr - credentials of caller.
4937 * RETURN: 0 on success, error code on failure.
4940 * ip - ctime|mtime updated
4944 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4945 offset_t offset, cred_t *cr)
4947 znode_t *zp = ITOZ(ip);
4948 zfsvfs_t *zfsvfs = ITOZSB(ip);
4955 if (cmd != F_FREESP) {
4957 return (SET_ERROR(EINVAL));
4961 * Callers might not be able to detect properly that we are read-only,
4962 * so check it explicitly here.
4964 if (zfs_is_readonly(zfsvfs)) {
4966 return (SET_ERROR(EROFS));
4969 if ((error = convoff(ip, bfp, SEEK_SET, offset))) {
4974 if (bfp->l_len < 0) {
4976 return (SET_ERROR(EINVAL));
4980 * Permissions aren't checked on Solaris because on this OS
4981 * zfs_space() can only be called with an opened file handle.
4982 * On Linux we can get here through truncate_range() which
4983 * operates directly on inodes, so we need to check access rights.
4985 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4991 len = bfp->l_len; /* 0 means from off to end of file */
4993 error = zfs_freesp(zp, off, len, flag, TRUE);
5001 zfs_fid(struct inode *ip, fid_t *fidp)
5003 znode_t *zp = ITOZ(ip);
5004 zfsvfs_t *zfsvfs = ITOZSB(ip);
5007 uint64_t object = zp->z_id;
5014 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
5015 &gen64, sizeof (uint64_t))) != 0) {
5020 gen = (uint32_t)gen64;
5022 size = SHORT_FID_LEN;
5024 zfid = (zfid_short_t *)fidp;
5026 zfid->zf_len = size;
5028 for (i = 0; i < sizeof (zfid->zf_object); i++)
5029 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
5031 /* Must have a non-zero generation number to distinguish from .zfs */
5034 for (i = 0; i < sizeof (zfid->zf_gen); i++)
5035 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
5043 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
5045 znode_t *zp = ITOZ(ip);
5046 zfsvfs_t *zfsvfs = ITOZSB(ip);
5048 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5052 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5060 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
5062 znode_t *zp = ITOZ(ip);
5063 zfsvfs_t *zfsvfs = ITOZSB(ip);
5065 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5066 zilog_t *zilog = zfsvfs->z_log;
5071 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5073 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5074 zil_commit(zilog, 0);
5080 #ifdef HAVE_UIO_ZEROCOPY
5082 * The smallest read we may consider to loan out an arcbuf.
5083 * This must be a power of 2.
5085 int zcr_blksz_min = (1 << 10); /* 1K */
5087 * If set to less than the file block size, allow loaning out of an
5088 * arcbuf for a partial block read. This must be a power of 2.
5090 int zcr_blksz_max = (1 << 17); /* 128K */
5094 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
5096 znode_t *zp = ITOZ(ip);
5097 zfsvfs_t *zfsvfs = ITOZSB(ip);
5098 int max_blksz = zfsvfs->z_max_blksz;
5099 uio_t *uio = &xuio->xu_uio;
5100 ssize_t size = uio->uio_resid;
5101 offset_t offset = uio->uio_loffset;
5106 int preamble, postamble;
5108 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5109 return (SET_ERROR(EINVAL));
5116 * Loan out an arc_buf for write if write size is bigger than
5117 * max_blksz, and the file's block size is also max_blksz.
5120 if (size < blksz || zp->z_blksz != blksz) {
5122 return (SET_ERROR(EINVAL));
5125 * Caller requests buffers for write before knowing where the
5126 * write offset might be (e.g. NFS TCP write).
5131 preamble = P2PHASE(offset, blksz);
5133 preamble = blksz - preamble;
5138 postamble = P2PHASE(size, blksz);
5141 fullblk = size / blksz;
5142 (void) dmu_xuio_init(xuio,
5143 (preamble != 0) + fullblk + (postamble != 0));
5146 * Have to fix iov base/len for partial buffers. They
5147 * currently represent full arc_buf's.
5150 /* data begins in the middle of the arc_buf */
5151 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5154 (void) dmu_xuio_add(xuio, abuf,
5155 blksz - preamble, preamble);
5158 for (i = 0; i < fullblk; i++) {
5159 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5162 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5166 /* data ends in the middle of the arc_buf */
5167 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5170 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5175 * Loan out an arc_buf for read if the read size is larger than
5176 * the current file block size. Block alignment is not
5177 * considered. Partial arc_buf will be loaned out for read.
5179 blksz = zp->z_blksz;
5180 if (blksz < zcr_blksz_min)
5181 blksz = zcr_blksz_min;
5182 if (blksz > zcr_blksz_max)
5183 blksz = zcr_blksz_max;
5184 /* avoid potential complexity of dealing with it */
5185 if (blksz > max_blksz) {
5187 return (SET_ERROR(EINVAL));
5190 maxsize = zp->z_size - uio->uio_loffset;
5196 return (SET_ERROR(EINVAL));
5201 return (SET_ERROR(EINVAL));
5204 uio->uio_extflg = UIO_XUIO;
5205 XUIO_XUZC_RW(xuio) = ioflag;
5212 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
5216 int ioflag = XUIO_XUZC_RW(xuio);
5218 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5220 i = dmu_xuio_cnt(xuio);
5222 abuf = dmu_xuio_arcbuf(xuio, i);
5224 * if abuf == NULL, it must be a write buffer
5225 * that has been returned in zfs_write().
5228 dmu_return_arcbuf(abuf);
5229 ASSERT(abuf || ioflag == UIO_WRITE);
5232 dmu_xuio_fini(xuio);
5235 #endif /* HAVE_UIO_ZEROCOPY */
5237 #if defined(_KERNEL)
5238 EXPORT_SYMBOL(zfs_open);
5239 EXPORT_SYMBOL(zfs_close);
5240 EXPORT_SYMBOL(zfs_read);
5241 EXPORT_SYMBOL(zfs_write);
5242 EXPORT_SYMBOL(zfs_access);
5243 EXPORT_SYMBOL(zfs_lookup);
5244 EXPORT_SYMBOL(zfs_create);
5245 EXPORT_SYMBOL(zfs_tmpfile);
5246 EXPORT_SYMBOL(zfs_remove);
5247 EXPORT_SYMBOL(zfs_mkdir);
5248 EXPORT_SYMBOL(zfs_rmdir);
5249 EXPORT_SYMBOL(zfs_readdir);
5250 EXPORT_SYMBOL(zfs_fsync);
5251 EXPORT_SYMBOL(zfs_getattr);
5252 EXPORT_SYMBOL(zfs_getattr_fast);
5253 EXPORT_SYMBOL(zfs_setattr);
5254 EXPORT_SYMBOL(zfs_rename);
5255 EXPORT_SYMBOL(zfs_symlink);
5256 EXPORT_SYMBOL(zfs_readlink);
5257 EXPORT_SYMBOL(zfs_link);
5258 EXPORT_SYMBOL(zfs_inactive);
5259 EXPORT_SYMBOL(zfs_space);
5260 EXPORT_SYMBOL(zfs_fid);
5261 EXPORT_SYMBOL(zfs_getsecattr);
5262 EXPORT_SYMBOL(zfs_setsecattr);
5263 EXPORT_SYMBOL(zfs_getpage);
5264 EXPORT_SYMBOL(zfs_putpage);
5265 EXPORT_SYMBOL(zfs_dirty_inode);
5266 EXPORT_SYMBOL(zfs_map);
5269 module_param(zfs_delete_blocks, ulong, 0644);
5270 MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async");
5271 module_param(zfs_read_chunk_size, ulong, 0644);
5272 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");