4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
28 /* Portions Copyright 2007 Jeremy Teo */
29 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sysmacros.h>
37 #include <sys/resource.h>
39 #include <sys/vfs_opreg.h>
43 #include <sys/taskq.h>
45 #include <sys/vmsystm.h>
46 #include <sys/atomic.h>
48 #include <sys/pathname.h>
49 #include <sys/cmn_err.h>
50 #include <sys/errno.h>
51 #include <sys/unistd.h>
52 #include <sys/zfs_dir.h>
53 #include <sys/zfs_acl.h>
54 #include <sys/zfs_ioctl.h>
55 #include <sys/fs/zfs.h>
57 #include <sys/dmu_objset.h>
63 #include <sys/dirent.h>
64 #include <sys/policy.h>
65 #include <sys/sunddi.h>
68 #include "fs/fs_subr.h"
69 #include <sys/zfs_ctldir.h>
70 #include <sys/zfs_fuid.h>
71 #include <sys/zfs_sa.h>
72 #include <sys/zfs_vnops.h>
74 #include <sys/zfs_rlock.h>
75 #include <sys/extdirent.h>
76 #include <sys/kidmap.h>
84 * Each vnode op performs some logical unit of work. To do this, the ZPL must
85 * properly lock its in-core state, create a DMU transaction, do the work,
86 * record this work in the intent log (ZIL), commit the DMU transaction,
87 * and wait for the intent log to commit if it is a synchronous operation.
88 * Moreover, the vnode ops must work in both normal and log replay context.
89 * The ordering of events is important to avoid deadlocks and references
90 * to freed memory. The example below illustrates the following Big Rules:
92 * (1) A check must be made in each zfs thread for a mounted file system.
93 * This is done avoiding races using ZFS_ENTER(zsb).
94 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
95 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
96 * can return EIO from the calling function.
98 * (2) iput() should always be the last thing except for zil_commit()
99 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
100 * First, if it's the last reference, the vnode/znode
101 * can be freed, so the zp may point to freed memory. Second, the last
102 * reference will call zfs_zinactive(), which may induce a lot of work --
103 * pushing cached pages (which acquires range locks) and syncing out
104 * cached atime changes. Third, zfs_zinactive() may require a new tx,
105 * which could deadlock the system if you were already holding one.
106 * If you must call iput() within a tx then use zfs_iput_async().
108 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
109 * as they can span dmu_tx_assign() calls.
111 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
112 * dmu_tx_assign(). This is critical because we don't want to block
113 * while holding locks.
115 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
116 * reduces lock contention and CPU usage when we must wait (note that if
117 * throughput is constrained by the storage, nearly every transaction
120 * Note, in particular, that if a lock is sometimes acquired before
121 * the tx assigns, and sometimes after (e.g. z_lock), then failing
122 * to use a non-blocking assign can deadlock the system. The scenario:
124 * Thread A has grabbed a lock before calling dmu_tx_assign().
125 * Thread B is in an already-assigned tx, and blocks for this lock.
126 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
127 * forever, because the previous txg can't quiesce until B's tx commits.
129 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
130 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
131 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
132 * to indicate that this operation has already called dmu_tx_wait().
133 * This will ensure that we don't retry forever, waiting a short bit
136 * (5) If the operation succeeded, generate the intent log entry for it
137 * before dropping locks. This ensures that the ordering of events
138 * in the intent log matches the order in which they actually occurred.
139 * During ZIL replay the zfs_log_* functions will update the sequence
140 * number to indicate the zil transaction has replayed.
142 * (6) At the end of each vnode op, the DMU tx must always commit,
143 * regardless of whether there were any errors.
145 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
146 * to ensure that synchronous semantics are provided when necessary.
148 * In general, this is how things should be ordered in each vnode op:
150 * ZFS_ENTER(zsb); // exit if unmounted
152 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
153 * rw_enter(...); // grab any other locks you need
154 * tx = dmu_tx_create(...); // get DMU tx
155 * dmu_tx_hold_*(); // hold each object you might modify
156 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
158 * rw_exit(...); // drop locks
159 * zfs_dirent_unlock(dl); // unlock directory entry
160 * iput(...); // release held vnodes
161 * if (error == ERESTART) {
167 * dmu_tx_abort(tx); // abort DMU tx
168 * ZFS_EXIT(zsb); // finished in zfs
169 * return (error); // really out of space
171 * error = do_real_work(); // do whatever this VOP does
173 * zfs_log_*(...); // on success, make ZIL entry
174 * dmu_tx_commit(tx); // commit DMU tx -- error or not
175 * rw_exit(...); // drop locks
176 * zfs_dirent_unlock(dl); // unlock directory entry
177 * iput(...); // release held vnodes
178 * zil_commit(zilog, foid); // synchronous when necessary
179 * ZFS_EXIT(zsb); // finished in zfs
180 * return (error); // done, report error
184 * Virus scanning is unsupported. It would be possible to add a hook
185 * here to performance the required virus scan. This could be done
186 * entirely in the kernel or potentially as an update to invoke a
190 zfs_vscan(struct inode *ip, cred_t *cr, int async)
197 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
199 znode_t *zp = ITOZ(ip);
200 zfs_sb_t *zsb = ITOZSB(ip);
205 /* Honor ZFS_APPENDONLY file attribute */
206 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
207 ((flag & O_APPEND) == 0)) {
209 return (SET_ERROR(EPERM));
212 /* Virus scan eligible files on open */
213 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
214 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
215 if (zfs_vscan(ip, cr, 0) != 0) {
217 return (SET_ERROR(EACCES));
221 /* Keep a count of the synchronous opens in the znode */
223 atomic_inc_32(&zp->z_sync_cnt);
228 EXPORT_SYMBOL(zfs_open);
232 zfs_close(struct inode *ip, int flag, cred_t *cr)
234 znode_t *zp = ITOZ(ip);
235 zfs_sb_t *zsb = ITOZSB(ip);
240 /* Decrement the synchronous opens in the znode */
242 atomic_dec_32(&zp->z_sync_cnt);
244 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
245 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
246 VERIFY(zfs_vscan(ip, cr, 1) == 0);
251 EXPORT_SYMBOL(zfs_close);
253 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
255 * Lseek support for finding holes (cmd == SEEK_HOLE) and
256 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
259 zfs_holey_common(struct inode *ip, int cmd, loff_t *off)
261 znode_t *zp = ITOZ(ip);
262 uint64_t noff = (uint64_t)*off; /* new offset */
267 file_sz = zp->z_size;
268 if (noff >= file_sz) {
269 return (SET_ERROR(ENXIO));
272 if (cmd == SEEK_HOLE)
277 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
280 return (SET_ERROR(ENXIO));
283 * We could find a hole that begins after the logical end-of-file,
284 * because dmu_offset_next() only works on whole blocks. If the
285 * EOF falls mid-block, then indicate that the "virtual hole"
286 * at the end of the file begins at the logical EOF, rather than
287 * at the end of the last block.
289 if (noff > file_sz) {
301 zfs_holey(struct inode *ip, int cmd, loff_t *off)
303 znode_t *zp = ITOZ(ip);
304 zfs_sb_t *zsb = ITOZSB(ip);
310 error = zfs_holey_common(ip, cmd, off);
315 EXPORT_SYMBOL(zfs_holey);
316 #endif /* SEEK_HOLE && SEEK_DATA */
320 * When a file is memory mapped, we must keep the IO data synchronized
321 * between the DMU cache and the memory mapped pages. What this means:
323 * On Write: If we find a memory mapped page, we write to *both*
324 * the page and the dmu buffer.
327 update_pages(struct inode *ip, int64_t start, int len,
328 objset_t *os, uint64_t oid)
330 struct address_space *mp = ip->i_mapping;
336 off = start & (PAGE_SIZE-1);
337 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
338 nbytes = MIN(PAGE_SIZE - off, len);
340 pp = find_lock_page(mp, start >> PAGE_SHIFT);
342 if (mapping_writably_mapped(mp))
343 flush_dcache_page(pp);
346 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
350 if (mapping_writably_mapped(mp))
351 flush_dcache_page(pp);
353 mark_page_accessed(pp);
366 * When a file is memory mapped, we must keep the IO data synchronized
367 * between the DMU cache and the memory mapped pages. What this means:
369 * On Read: We "read" preferentially from memory mapped pages,
370 * else we default from the dmu buffer.
372 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
373 * the file is memory mapped.
376 mappedread(struct inode *ip, int nbytes, uio_t *uio)
378 struct address_space *mp = ip->i_mapping;
380 znode_t *zp = ITOZ(ip);
387 start = uio->uio_loffset;
388 off = start & (PAGE_SIZE-1);
389 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
390 bytes = MIN(PAGE_SIZE - off, len);
392 pp = find_lock_page(mp, start >> PAGE_SHIFT);
394 ASSERT(PageUptodate(pp));
397 error = uiomove(pb + off, bytes, UIO_READ, uio);
400 if (mapping_writably_mapped(mp))
401 flush_dcache_page(pp);
403 mark_page_accessed(pp);
407 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
420 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
421 unsigned long zfs_delete_blocks = DMU_MAX_DELETEBLKCNT;
424 * Read bytes from specified file into supplied buffer.
426 * IN: ip - inode of file to be read from.
427 * uio - structure supplying read location, range info,
429 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
430 * O_DIRECT flag; used to bypass page cache.
431 * cr - credentials of caller.
433 * OUT: uio - updated offset and range, buffer filled.
435 * RETURN: 0 on success, error code on failure.
438 * inode - atime updated if byte count > 0
442 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
444 znode_t *zp = ITOZ(ip);
445 zfs_sb_t *zsb = ITOZSB(ip);
449 #ifdef HAVE_UIO_ZEROCOPY
451 #endif /* HAVE_UIO_ZEROCOPY */
456 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
458 return (SET_ERROR(EACCES));
462 * Validate file offset
464 if (uio->uio_loffset < (offset_t)0) {
466 return (SET_ERROR(EINVAL));
470 * Fasttrack empty reads
472 if (uio->uio_resid == 0) {
478 * If we're in FRSYNC mode, sync out this znode before reading it.
480 if (ioflag & FRSYNC || zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
481 zil_commit(zsb->z_log, zp->z_id);
484 * Lock the range against changes.
486 rl = zfs_range_lock(&zp->z_range_lock, uio->uio_loffset, uio->uio_resid,
490 * If we are reading past end-of-file we can skip
491 * to the end; but we might still need to set atime.
493 if (uio->uio_loffset >= zp->z_size) {
498 ASSERT(uio->uio_loffset < zp->z_size);
499 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
501 #ifdef HAVE_UIO_ZEROCOPY
502 if ((uio->uio_extflg == UIO_XUIO) &&
503 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
505 int blksz = zp->z_blksz;
506 uint64_t offset = uio->uio_loffset;
508 xuio = (xuio_t *)uio;
510 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
513 ASSERT(offset + n <= blksz);
516 (void) dmu_xuio_init(xuio, nblk);
518 if (vn_has_cached_data(ip)) {
520 * For simplicity, we always allocate a full buffer
521 * even if we only expect to read a portion of a block.
523 while (--nblk >= 0) {
524 (void) dmu_xuio_add(xuio,
525 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
530 #endif /* HAVE_UIO_ZEROCOPY */
533 nbytes = MIN(n, zfs_read_chunk_size -
534 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
536 if (zp->z_is_mapped && !(ioflag & O_DIRECT)) {
537 error = mappedread(ip, nbytes, uio);
539 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
544 /* convert checksum errors into IO errors */
546 error = SET_ERROR(EIO);
553 zfs_range_unlock(rl);
558 EXPORT_SYMBOL(zfs_read);
561 * Write the bytes to a file.
563 * IN: ip - inode of file to be written to.
564 * uio - structure supplying write location, range info,
566 * ioflag - FAPPEND flag set if in append mode.
567 * O_DIRECT flag; used to bypass page cache.
568 * cr - credentials of caller.
570 * OUT: uio - updated offset and range.
572 * RETURN: 0 if success
573 * error code if failure
576 * ip - ctime|mtime updated if byte count > 0
581 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
583 znode_t *zp = ITOZ(ip);
584 rlim64_t limit = uio->uio_limit;
585 ssize_t start_resid = uio->uio_resid;
589 zfs_sb_t *zsb = ZTOZSB(zp);
594 int max_blksz = zsb->z_max_blksz;
597 const iovec_t *aiov = NULL;
600 const iovec_t *iovp = uio->uio_iov;
603 sa_bulk_attr_t bulk[4];
604 uint64_t mtime[2], ctime[2];
606 ASSERTV(int iovcnt = uio->uio_iovcnt);
609 * Fasttrack empty write
615 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
621 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
622 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
623 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
624 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
628 * Callers might not be able to detect properly that we are read-only,
629 * so check it explicitly here.
631 if (zfs_is_readonly(zsb)) {
633 return (SET_ERROR(EROFS));
637 * If immutable or not appending then return EPERM
639 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
640 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
641 (uio->uio_loffset < zp->z_size))) {
643 return (SET_ERROR(EPERM));
649 * Validate file offset
651 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
654 return (SET_ERROR(EINVAL));
658 * Pre-fault the pages to ensure slow (eg NFS) pages
660 * Skip this if uio contains loaned arc_buf.
662 #ifdef HAVE_UIO_ZEROCOPY
663 if ((uio->uio_extflg == UIO_XUIO) &&
664 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
665 xuio = (xuio_t *)uio;
668 uio_prefaultpages(MIN(n, max_blksz), uio);
671 * If in append mode, set the io offset pointer to eof.
673 if (ioflag & FAPPEND) {
675 * Obtain an appending range lock to guarantee file append
676 * semantics. We reset the write offset once we have the lock.
678 rl = zfs_range_lock(&zp->z_range_lock, 0, n, RL_APPEND);
680 if (rl->r_len == UINT64_MAX) {
682 * We overlocked the file because this write will cause
683 * the file block size to increase.
684 * Note that zp_size cannot change with this lock held.
688 uio->uio_loffset = woff;
691 * Note that if the file block size will change as a result of
692 * this write, then this range lock will lock the entire file
693 * so that we can re-write the block safely.
695 rl = zfs_range_lock(&zp->z_range_lock, woff, n, RL_WRITER);
699 zfs_range_unlock(rl);
701 return (SET_ERROR(EFBIG));
704 if ((woff + n) > limit || woff > (limit - n))
707 /* Will this write extend the file length? */
708 write_eof = (woff + n > zp->z_size);
710 end_size = MAX(zp->z_size, woff + n);
713 * Write the file in reasonable size chunks. Each chunk is written
714 * in a separate transaction; this keeps the intent log records small
715 * and allows us to do more fine-grained space accounting.
719 woff = uio->uio_loffset;
720 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
721 zfs_owner_overquota(zsb, zp, B_TRUE)) {
723 dmu_return_arcbuf(abuf);
724 error = SET_ERROR(EDQUOT);
728 if (xuio && abuf == NULL) {
729 ASSERT(i_iov < iovcnt);
730 ASSERT3U(uio->uio_segflg, !=, UIO_BVEC);
732 abuf = dmu_xuio_arcbuf(xuio, i_iov);
733 dmu_xuio_clear(xuio, i_iov);
734 ASSERT((aiov->iov_base == abuf->b_data) ||
735 ((char *)aiov->iov_base - (char *)abuf->b_data +
736 aiov->iov_len == arc_buf_size(abuf)));
738 } else if (abuf == NULL && n >= max_blksz &&
739 woff >= zp->z_size &&
740 P2PHASE(woff, max_blksz) == 0 &&
741 zp->z_blksz == max_blksz) {
743 * This write covers a full block. "Borrow" a buffer
744 * from the dmu so that we can fill it before we enter
745 * a transaction. This avoids the possibility of
746 * holding up the transaction if the data copy hangs
747 * up on a pagefault (e.g., from an NFS server mapping).
751 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
753 ASSERT(abuf != NULL);
754 ASSERT(arc_buf_size(abuf) == max_blksz);
755 if ((error = uiocopy(abuf->b_data, max_blksz,
756 UIO_WRITE, uio, &cbytes))) {
757 dmu_return_arcbuf(abuf);
760 ASSERT(cbytes == max_blksz);
764 * Start a transaction.
766 tx = dmu_tx_create(zsb->z_os);
767 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
768 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
769 zfs_sa_upgrade_txholds(tx, zp);
770 error = dmu_tx_assign(tx, TXG_WAIT);
774 dmu_return_arcbuf(abuf);
779 * If zfs_range_lock() over-locked we grow the blocksize
780 * and then reduce the lock range. This will only happen
781 * on the first iteration since zfs_range_reduce() will
782 * shrink down r_len to the appropriate size.
784 if (rl->r_len == UINT64_MAX) {
787 if (zp->z_blksz > max_blksz) {
789 * File's blocksize is already larger than the
790 * "recordsize" property. Only let it grow to
791 * the next power of 2.
793 ASSERT(!ISP2(zp->z_blksz));
794 new_blksz = MIN(end_size,
795 1 << highbit64(zp->z_blksz));
797 new_blksz = MIN(end_size, max_blksz);
799 zfs_grow_blocksize(zp, new_blksz, tx);
800 zfs_range_reduce(rl, woff, n);
804 * XXX - should we really limit each write to z_max_blksz?
805 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
807 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
810 tx_bytes = uio->uio_resid;
811 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
813 tx_bytes -= uio->uio_resid;
816 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
818 * If this is not a full block write, but we are
819 * extending the file past EOF and this data starts
820 * block-aligned, use assign_arcbuf(). Otherwise,
821 * write via dmu_write().
823 if (tx_bytes < max_blksz && (!write_eof ||
824 aiov->iov_base != abuf->b_data)) {
826 dmu_write(zsb->z_os, zp->z_id, woff,
827 aiov->iov_len, aiov->iov_base, tx);
828 dmu_return_arcbuf(abuf);
829 xuio_stat_wbuf_copied();
831 ASSERT(xuio || tx_bytes == max_blksz);
832 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
835 ASSERT(tx_bytes <= uio->uio_resid);
836 uioskip(uio, tx_bytes);
839 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
840 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
843 * If we made no progress, we're done. If we made even
844 * partial progress, update the znode and ZIL accordingly.
847 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
848 (void *)&zp->z_size, sizeof (uint64_t), tx);
855 * Clear Set-UID/Set-GID bits on successful write if not
856 * privileged and at least one of the excute bits is set.
858 * It would be nice to to this after all writes have
859 * been done, but that would still expose the ISUID/ISGID
860 * to another app after the partial write is committed.
862 * Note: we don't call zfs_fuid_map_id() here because
863 * user 0 is not an ephemeral uid.
865 mutex_enter(&zp->z_acl_lock);
866 uid = KUID_TO_SUID(ip->i_uid);
867 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
868 (S_IXUSR >> 6))) != 0 &&
869 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
870 secpolicy_vnode_setid_retain(cr,
871 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) {
873 zp->z_mode &= ~(S_ISUID | S_ISGID);
874 ip->i_mode = newmode = zp->z_mode;
875 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
876 (void *)&newmode, sizeof (uint64_t), tx);
878 mutex_exit(&zp->z_acl_lock);
880 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
883 * Update the file size (zp_size) if it has changed;
884 * account for possible concurrent updates.
886 while ((end_size = zp->z_size) < uio->uio_loffset) {
887 (void) atomic_cas_64(&zp->z_size, end_size,
892 * If we are replaying and eof is non zero then force
893 * the file size to the specified eof. Note, there's no
894 * concurrency during replay.
896 if (zsb->z_replay && zsb->z_replay_eof != 0)
897 zp->z_size = zsb->z_replay_eof;
899 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
901 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
907 ASSERT(tx_bytes == nbytes);
911 uio_prefaultpages(MIN(n, max_blksz), uio);
914 zfs_inode_update(zp);
915 zfs_range_unlock(rl);
918 * If we're in replay mode, or we made no progress, return error.
919 * Otherwise, it's at least a partial write, so it's successful.
921 if (zsb->z_replay || uio->uio_resid == start_resid) {
926 if (ioflag & (FSYNC | FDSYNC) ||
927 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
928 zil_commit(zilog, zp->z_id);
933 EXPORT_SYMBOL(zfs_write);
936 zfs_iput_async(struct inode *ip)
938 objset_t *os = ITOZSB(ip)->z_os;
940 ASSERT(atomic_read(&ip->i_count) > 0);
943 if (atomic_read(&ip->i_count) == 1)
944 taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os)),
945 (task_func_t *)iput, ip, TQ_SLEEP);
951 zfs_get_done(zgd_t *zgd, int error)
953 znode_t *zp = zgd->zgd_private;
956 dmu_buf_rele(zgd->zgd_db, zgd);
958 zfs_range_unlock(zgd->zgd_rl);
961 * Release the vnode asynchronously as we currently have the
962 * txg stopped from syncing.
964 zfs_iput_async(ZTOI(zp));
966 if (error == 0 && zgd->zgd_bp)
967 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
969 kmem_free(zgd, sizeof (zgd_t));
973 static int zil_fault_io = 0;
977 * Get data to generate a TX_WRITE intent log record.
980 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
983 objset_t *os = zsb->z_os;
985 uint64_t object = lr->lr_foid;
986 uint64_t offset = lr->lr_offset;
987 uint64_t size = lr->lr_length;
988 blkptr_t *bp = &lr->lr_blkptr;
997 * Nothing to do if the file has been removed
999 if (zfs_zget(zsb, object, &zp) != 0)
1000 return (SET_ERROR(ENOENT));
1001 if (zp->z_unlinked) {
1003 * Release the vnode asynchronously as we currently have the
1004 * txg stopped from syncing.
1006 zfs_iput_async(ZTOI(zp));
1007 return (SET_ERROR(ENOENT));
1010 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1011 zgd->zgd_zilog = zsb->z_log;
1012 zgd->zgd_private = zp;
1015 * Write records come in two flavors: immediate and indirect.
1016 * For small writes it's cheaper to store the data with the
1017 * log record (immediate); for large writes it's cheaper to
1018 * sync the data and get a pointer to it (indirect) so that
1019 * we don't have to write the data twice.
1021 if (buf != NULL) { /* immediate write */
1022 zgd->zgd_rl = zfs_range_lock(&zp->z_range_lock, offset, size,
1024 /* test for truncation needs to be done while range locked */
1025 if (offset >= zp->z_size) {
1026 error = SET_ERROR(ENOENT);
1028 error = dmu_read(os, object, offset, size, buf,
1029 DMU_READ_NO_PREFETCH);
1031 ASSERT(error == 0 || error == ENOENT);
1032 } else { /* indirect write */
1034 * Have to lock the whole block to ensure when it's
1035 * written out and it's checksum is being calculated
1036 * that no one can change the data. We need to re-check
1037 * blocksize after we get the lock in case it's changed!
1042 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1044 zgd->zgd_rl = zfs_range_lock(&zp->z_range_lock, offset,
1046 if (zp->z_blksz == size)
1049 zfs_range_unlock(zgd->zgd_rl);
1051 /* test for truncation needs to be done while range locked */
1052 if (lr->lr_offset >= zp->z_size)
1053 error = SET_ERROR(ENOENT);
1056 error = SET_ERROR(EIO);
1061 error = dmu_buf_hold(os, object, offset, zgd, &db,
1062 DMU_READ_NO_PREFETCH);
1065 blkptr_t *obp = dmu_buf_get_blkptr(db);
1067 ASSERT(BP_IS_HOLE(bp));
1074 ASSERT(db->db_offset == offset);
1075 ASSERT(db->db_size == size);
1077 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1079 ASSERT(error || lr->lr_length <= zp->z_blksz);
1082 * On success, we need to wait for the write I/O
1083 * initiated by dmu_sync() to complete before we can
1084 * release this dbuf. We will finish everything up
1085 * in the zfs_get_done() callback.
1090 if (error == EALREADY) {
1091 lr->lr_common.lrc_txtype = TX_WRITE2;
1097 zfs_get_done(zgd, error);
1104 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1106 znode_t *zp = ITOZ(ip);
1107 zfs_sb_t *zsb = ITOZSB(ip);
1113 if (flag & V_ACE_MASK)
1114 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1116 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1121 EXPORT_SYMBOL(zfs_access);
1124 * Lookup an entry in a directory, or an extended attribute directory.
1125 * If it exists, return a held inode reference for it.
1127 * IN: dip - inode of directory to search.
1128 * nm - name of entry to lookup.
1129 * flags - LOOKUP_XATTR set if looking for an attribute.
1130 * cr - credentials of caller.
1131 * direntflags - directory lookup flags
1132 * realpnp - returned pathname.
1134 * OUT: ipp - inode of located entry, NULL if not found.
1136 * RETURN: 0 on success, error code on failure.
1143 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1144 cred_t *cr, int *direntflags, pathname_t *realpnp)
1146 znode_t *zdp = ITOZ(dip);
1147 zfs_sb_t *zsb = ITOZSB(dip);
1151 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1153 if (!S_ISDIR(dip->i_mode)) {
1154 return (SET_ERROR(ENOTDIR));
1155 } else if (zdp->z_sa_hdl == NULL) {
1156 return (SET_ERROR(EIO));
1159 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1160 error = zfs_fastaccesschk_execute(zdp, cr);
1169 vnode_t *tvp = dnlc_lookup(dvp, nm);
1172 error = zfs_fastaccesschk_execute(zdp, cr);
1177 if (tvp == DNLC_NO_VNODE) {
1179 return (SET_ERROR(ENOENT));
1182 return (specvp_check(vpp, cr));
1185 #endif /* HAVE_DNLC */
1194 if (flags & LOOKUP_XATTR) {
1196 * We don't allow recursive attributes..
1197 * Maybe someday we will.
1199 if (zdp->z_pflags & ZFS_XATTR) {
1201 return (SET_ERROR(EINVAL));
1204 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1210 * Do we have permission to get into attribute directory?
1213 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1223 if (!S_ISDIR(dip->i_mode)) {
1225 return (SET_ERROR(ENOTDIR));
1229 * Check accessibility of directory.
1232 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1237 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
1238 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1240 return (SET_ERROR(EILSEQ));
1243 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1244 if ((error == 0) && (*ipp))
1245 zfs_inode_update(ITOZ(*ipp));
1250 EXPORT_SYMBOL(zfs_lookup);
1253 * Attempt to create a new entry in a directory. If the entry
1254 * already exists, truncate the file if permissible, else return
1255 * an error. Return the ip of the created or trunc'd file.
1257 * IN: dip - inode of directory to put new file entry in.
1258 * name - name of new file entry.
1259 * vap - attributes of new file.
1260 * excl - flag indicating exclusive or non-exclusive mode.
1261 * mode - mode to open file with.
1262 * cr - credentials of caller.
1263 * flag - large file flag [UNUSED].
1264 * vsecp - ACL to be set
1266 * OUT: ipp - inode of created or trunc'd entry.
1268 * RETURN: 0 on success, error code on failure.
1271 * dip - ctime|mtime updated if new entry created
1272 * ip - ctime|mtime always, atime if new
1277 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1278 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1280 znode_t *zp, *dzp = ITOZ(dip);
1281 zfs_sb_t *zsb = ITOZSB(dip);
1289 zfs_acl_ids_t acl_ids;
1290 boolean_t fuid_dirtied;
1291 boolean_t have_acl = B_FALSE;
1292 boolean_t waited = B_FALSE;
1295 * If we have an ephemeral id, ACL, or XVATTR then
1296 * make sure file system is at proper version
1302 if (zsb->z_use_fuids == B_FALSE &&
1303 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1304 return (SET_ERROR(EINVAL));
1311 if (zsb->z_utf8 && u8_validate(name, strlen(name),
1312 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1314 return (SET_ERROR(EILSEQ));
1317 if (vap->va_mask & ATTR_XVATTR) {
1318 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1319 crgetuid(cr), cr, vap->va_mode)) != 0) {
1327 if (*name == '\0') {
1329 * Null component name refers to the directory itself.
1336 /* possible igrab(zp) */
1339 if (flag & FIGNORECASE)
1342 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1346 zfs_acl_ids_free(&acl_ids);
1347 if (strcmp(name, "..") == 0)
1348 error = SET_ERROR(EISDIR);
1358 * Create a new file object and update the directory
1361 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1363 zfs_acl_ids_free(&acl_ids);
1368 * We only support the creation of regular files in
1369 * extended attribute directories.
1372 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1374 zfs_acl_ids_free(&acl_ids);
1375 error = SET_ERROR(EINVAL);
1379 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1380 cr, vsecp, &acl_ids)) != 0)
1384 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1385 zfs_acl_ids_free(&acl_ids);
1386 error = SET_ERROR(EDQUOT);
1390 tx = dmu_tx_create(os);
1392 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1393 ZFS_SA_BASE_ATTR_SIZE);
1395 fuid_dirtied = zsb->z_fuid_dirty;
1397 zfs_fuid_txhold(zsb, tx);
1398 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1399 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1400 if (!zsb->z_use_sa &&
1401 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1402 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1403 0, acl_ids.z_aclp->z_acl_bytes);
1405 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1407 zfs_dirent_unlock(dl);
1408 if (error == ERESTART) {
1414 zfs_acl_ids_free(&acl_ids);
1419 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1422 zfs_fuid_sync(zsb, tx);
1424 (void) zfs_link_create(dl, zp, tx, ZNEW);
1425 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1426 if (flag & FIGNORECASE)
1428 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1429 vsecp, acl_ids.z_fuidp, vap);
1430 zfs_acl_ids_free(&acl_ids);
1433 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1436 zfs_acl_ids_free(&acl_ids);
1440 * A directory entry already exists for this name.
1443 * Can't truncate an existing file if in exclusive mode.
1446 error = SET_ERROR(EEXIST);
1450 * Can't open a directory for writing.
1452 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1453 error = SET_ERROR(EISDIR);
1457 * Verify requested access to file.
1459 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1463 mutex_enter(&dzp->z_lock);
1465 mutex_exit(&dzp->z_lock);
1468 * Truncate regular files if requested.
1470 if (S_ISREG(ZTOI(zp)->i_mode) &&
1471 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1472 /* we can't hold any locks when calling zfs_freesp() */
1474 zfs_dirent_unlock(dl);
1477 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1483 zfs_dirent_unlock(dl);
1489 zfs_inode_update(dzp);
1490 zfs_inode_update(zp);
1494 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1495 zil_commit(zilog, 0);
1500 EXPORT_SYMBOL(zfs_create);
1503 * Remove an entry from a directory.
1505 * IN: dip - inode of directory to remove entry from.
1506 * name - name of entry to remove.
1507 * cr - credentials of caller.
1509 * RETURN: 0 if success
1510 * error code if failure
1514 * ip - ctime (if nlink > 0)
1517 uint64_t null_xattr = 0;
1521 zfs_remove(struct inode *dip, char *name, cred_t *cr, int flags)
1523 znode_t *zp, *dzp = ITOZ(dip);
1526 zfs_sb_t *zsb = ITOZSB(dip);
1528 uint64_t acl_obj, xattr_obj;
1529 uint64_t xattr_obj_unlinked = 0;
1534 boolean_t may_delete_now, delete_now = FALSE;
1535 boolean_t unlinked, toobig = FALSE;
1537 pathname_t *realnmp = NULL;
1541 boolean_t waited = B_FALSE;
1547 if (flags & FIGNORECASE) {
1557 * Attempt to lock directory; fail if entry doesn't exist.
1559 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1569 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1574 * Need to use rmdir for removing directories.
1576 if (S_ISDIR(ip->i_mode)) {
1577 error = SET_ERROR(EPERM);
1583 dnlc_remove(dvp, realnmp->pn_buf);
1585 dnlc_remove(dvp, name);
1586 #endif /* HAVE_DNLC */
1588 mutex_enter(&zp->z_lock);
1589 may_delete_now = atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped);
1590 mutex_exit(&zp->z_lock);
1593 * We may delete the znode now, or we may put it in the unlinked set;
1594 * it depends on whether we're the last link, and on whether there are
1595 * other holds on the inode. So we dmu_tx_hold() the right things to
1596 * allow for either case.
1599 tx = dmu_tx_create(zsb->z_os);
1600 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1601 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1602 zfs_sa_upgrade_txholds(tx, zp);
1603 zfs_sa_upgrade_txholds(tx, dzp);
1604 if (may_delete_now) {
1605 toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks;
1606 /* if the file is too big, only hold_free a token amount */
1607 dmu_tx_hold_free(tx, zp->z_id, 0,
1608 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1611 /* are there any extended attributes? */
1612 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1613 &xattr_obj, sizeof (xattr_obj));
1614 if (error == 0 && xattr_obj) {
1615 error = zfs_zget(zsb, xattr_obj, &xzp);
1617 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1618 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1621 mutex_enter(&zp->z_lock);
1622 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1623 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1624 mutex_exit(&zp->z_lock);
1626 /* charge as an update -- would be nice not to charge at all */
1627 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1630 * Mark this transaction as typically resulting in a net free of space
1632 dmu_tx_mark_netfree(tx);
1634 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1636 zfs_dirent_unlock(dl);
1640 if (error == ERESTART) {
1654 * Remove the directory entry.
1656 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1665 * Hold z_lock so that we can make sure that the ACL obj
1666 * hasn't changed. Could have been deleted due to
1669 mutex_enter(&zp->z_lock);
1670 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1671 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1672 delete_now = may_delete_now && !toobig &&
1673 atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped) &&
1674 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1679 if (xattr_obj_unlinked) {
1680 ASSERT3U(ZTOI(xzp)->i_nlink, ==, 2);
1681 mutex_enter(&xzp->z_lock);
1682 xzp->z_unlinked = 1;
1683 clear_nlink(ZTOI(xzp));
1685 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zsb),
1686 &links, sizeof (links), tx);
1687 ASSERT3U(error, ==, 0);
1688 mutex_exit(&xzp->z_lock);
1689 zfs_unlinked_add(xzp, tx);
1692 error = sa_remove(zp->z_sa_hdl,
1693 SA_ZPL_XATTR(zsb), tx);
1695 error = sa_update(zp->z_sa_hdl,
1696 SA_ZPL_XATTR(zsb), &null_xattr,
1697 sizeof (uint64_t), tx);
1701 * Add to the unlinked set because a new reference could be
1702 * taken concurrently resulting in a deferred destruction.
1704 zfs_unlinked_add(zp, tx);
1705 mutex_exit(&zp->z_lock);
1706 zfs_inode_update(zp);
1708 } else if (unlinked) {
1709 mutex_exit(&zp->z_lock);
1710 zfs_unlinked_add(zp, tx);
1714 if (flags & FIGNORECASE)
1716 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1723 zfs_dirent_unlock(dl);
1724 zfs_inode_update(dzp);
1727 zfs_inode_update(zp);
1732 zfs_inode_update(xzp);
1733 zfs_iput_async(ZTOI(xzp));
1736 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1737 zil_commit(zilog, 0);
1742 EXPORT_SYMBOL(zfs_remove);
1745 * Create a new directory and insert it into dip using the name
1746 * provided. Return a pointer to the inserted directory.
1748 * IN: dip - inode of directory to add subdir to.
1749 * dirname - name of new directory.
1750 * vap - attributes of new directory.
1751 * cr - credentials of caller.
1752 * vsecp - ACL to be set
1754 * OUT: ipp - inode of created directory.
1756 * RETURN: 0 if success
1757 * error code if failure
1760 * dip - ctime|mtime updated
1761 * ipp - ctime|mtime|atime updated
1765 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1766 cred_t *cr, int flags, vsecattr_t *vsecp)
1768 znode_t *zp, *dzp = ITOZ(dip);
1769 zfs_sb_t *zsb = ITOZSB(dip);
1777 gid_t gid = crgetgid(cr);
1778 zfs_acl_ids_t acl_ids;
1779 boolean_t fuid_dirtied;
1780 boolean_t waited = B_FALSE;
1782 ASSERT(S_ISDIR(vap->va_mode));
1785 * If we have an ephemeral id, ACL, or XVATTR then
1786 * make sure file system is at proper version
1790 if (zsb->z_use_fuids == B_FALSE &&
1791 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1792 return (SET_ERROR(EINVAL));
1798 if (dzp->z_pflags & ZFS_XATTR) {
1800 return (SET_ERROR(EINVAL));
1803 if (zsb->z_utf8 && u8_validate(dirname,
1804 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1806 return (SET_ERROR(EILSEQ));
1808 if (flags & FIGNORECASE)
1811 if (vap->va_mask & ATTR_XVATTR) {
1812 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1813 crgetuid(cr), cr, vap->va_mode)) != 0) {
1819 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1820 vsecp, &acl_ids)) != 0) {
1825 * First make sure the new directory doesn't exist.
1827 * Existence is checked first to make sure we don't return
1828 * EACCES instead of EEXIST which can cause some applications
1834 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1836 zfs_acl_ids_free(&acl_ids);
1841 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1842 zfs_acl_ids_free(&acl_ids);
1843 zfs_dirent_unlock(dl);
1848 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1849 zfs_acl_ids_free(&acl_ids);
1850 zfs_dirent_unlock(dl);
1852 return (SET_ERROR(EDQUOT));
1856 * Add a new entry to the directory.
1858 tx = dmu_tx_create(zsb->z_os);
1859 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1860 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1861 fuid_dirtied = zsb->z_fuid_dirty;
1863 zfs_fuid_txhold(zsb, tx);
1864 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1865 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1866 acl_ids.z_aclp->z_acl_bytes);
1869 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1870 ZFS_SA_BASE_ATTR_SIZE);
1872 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1874 zfs_dirent_unlock(dl);
1875 if (error == ERESTART) {
1881 zfs_acl_ids_free(&acl_ids);
1890 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1893 zfs_fuid_sync(zsb, tx);
1896 * Now put new name in parent dir.
1898 (void) zfs_link_create(dl, zp, tx, ZNEW);
1902 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1903 if (flags & FIGNORECASE)
1905 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1906 acl_ids.z_fuidp, vap);
1908 zfs_acl_ids_free(&acl_ids);
1912 zfs_dirent_unlock(dl);
1914 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1915 zil_commit(zilog, 0);
1917 zfs_inode_update(dzp);
1918 zfs_inode_update(zp);
1922 EXPORT_SYMBOL(zfs_mkdir);
1925 * Remove a directory subdir entry. If the current working
1926 * directory is the same as the subdir to be removed, the
1929 * IN: dip - inode of directory to remove from.
1930 * name - name of directory to be removed.
1931 * cwd - inode of current working directory.
1932 * cr - credentials of caller.
1933 * flags - case flags
1935 * RETURN: 0 on success, error code on failure.
1938 * dip - ctime|mtime updated
1942 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1945 znode_t *dzp = ITOZ(dip);
1948 zfs_sb_t *zsb = ITOZSB(dip);
1954 boolean_t waited = B_FALSE;
1960 if (flags & FIGNORECASE)
1966 * Attempt to lock directory; fail if entry doesn't exist.
1968 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1976 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1980 if (!S_ISDIR(ip->i_mode)) {
1981 error = SET_ERROR(ENOTDIR);
1986 error = SET_ERROR(EINVAL);
1991 * Grab a lock on the directory to make sure that noone is
1992 * trying to add (or lookup) entries while we are removing it.
1994 rw_enter(&zp->z_name_lock, RW_WRITER);
1997 * Grab a lock on the parent pointer to make sure we play well
1998 * with the treewalk and directory rename code.
2000 rw_enter(&zp->z_parent_lock, RW_WRITER);
2002 tx = dmu_tx_create(zsb->z_os);
2003 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2004 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2005 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
2006 zfs_sa_upgrade_txholds(tx, zp);
2007 zfs_sa_upgrade_txholds(tx, dzp);
2008 dmu_tx_mark_netfree(tx);
2009 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2011 rw_exit(&zp->z_parent_lock);
2012 rw_exit(&zp->z_name_lock);
2013 zfs_dirent_unlock(dl);
2015 if (error == ERESTART) {
2026 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2029 uint64_t txtype = TX_RMDIR;
2030 if (flags & FIGNORECASE)
2032 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2037 rw_exit(&zp->z_parent_lock);
2038 rw_exit(&zp->z_name_lock);
2040 zfs_dirent_unlock(dl);
2042 zfs_inode_update(dzp);
2043 zfs_inode_update(zp);
2046 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
2047 zil_commit(zilog, 0);
2052 EXPORT_SYMBOL(zfs_rmdir);
2055 * Read as many directory entries as will fit into the provided
2056 * dirent buffer from the given directory cursor position.
2058 * IN: ip - inode of directory to read.
2059 * dirent - buffer for directory entries.
2061 * OUT: dirent - filler buffer of directory entries.
2063 * RETURN: 0 if success
2064 * error code if failure
2067 * ip - atime updated
2069 * Note that the low 4 bits of the cookie returned by zap is always zero.
2070 * This allows us to use the low range for "special" directory entries:
2071 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2072 * we use the offset 2 for the '.zfs' directory.
2076 zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
2078 znode_t *zp = ITOZ(ip);
2079 zfs_sb_t *zsb = ITOZSB(ip);
2082 zap_attribute_t zap;
2088 uint64_t offset; /* must be unsigned; checks for < 1 */
2093 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
2094 &parent, sizeof (parent))) != 0)
2098 * Quit if directory has been removed (posix)
2106 prefetch = zp->z_zn_prefetch;
2109 * Initialize the iterator cursor.
2113 * Start iteration from the beginning of the directory.
2115 zap_cursor_init(&zc, os, zp->z_id);
2118 * The offset is a serialized cursor.
2120 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2124 * Transform to file-system independent format
2129 * Special case `.', `..', and `.zfs'.
2132 (void) strcpy(zap.za_name, ".");
2133 zap.za_normalization_conflict = 0;
2136 } else if (offset == 1) {
2137 (void) strcpy(zap.za_name, "..");
2138 zap.za_normalization_conflict = 0;
2141 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2142 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2143 zap.za_normalization_conflict = 0;
2144 objnum = ZFSCTL_INO_ROOT;
2150 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2151 if (error == ENOENT)
2158 * Allow multiple entries provided the first entry is
2159 * the object id. Non-zpl consumers may safely make
2160 * use of the additional space.
2162 * XXX: This should be a feature flag for compatibility
2164 if (zap.za_integer_length != 8 ||
2165 zap.za_num_integers == 0) {
2166 cmn_err(CE_WARN, "zap_readdir: bad directory "
2167 "entry, obj = %lld, offset = %lld, "
2168 "length = %d, num = %lld\n",
2169 (u_longlong_t)zp->z_id,
2170 (u_longlong_t)offset,
2171 zap.za_integer_length,
2172 (u_longlong_t)zap.za_num_integers);
2173 error = SET_ERROR(ENXIO);
2177 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2178 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2181 done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name),
2186 /* Prefetch znode */
2188 dmu_prefetch(os, objnum, 0, 0, 0,
2189 ZIO_PRIORITY_SYNC_READ);
2193 * Move to the next entry, fill in the previous offset.
2195 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2196 zap_cursor_advance(&zc);
2197 offset = zap_cursor_serialize(&zc);
2203 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2206 zap_cursor_fini(&zc);
2207 if (error == ENOENT)
2214 EXPORT_SYMBOL(zfs_readdir);
2216 ulong_t zfs_fsync_sync_cnt = 4;
2219 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2221 znode_t *zp = ITOZ(ip);
2222 zfs_sb_t *zsb = ITOZSB(ip);
2224 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2226 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2229 zil_commit(zsb->z_log, zp->z_id);
2232 tsd_set(zfs_fsyncer_key, NULL);
2236 EXPORT_SYMBOL(zfs_fsync);
2240 * Get the requested file attributes and place them in the provided
2243 * IN: ip - inode of file.
2244 * vap - va_mask identifies requested attributes.
2245 * If ATTR_XVATTR set, then optional attrs are requested
2246 * flags - ATTR_NOACLCHECK (CIFS server context)
2247 * cr - credentials of caller.
2249 * OUT: vap - attribute values.
2251 * RETURN: 0 (always succeeds)
2255 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2257 znode_t *zp = ITOZ(ip);
2258 zfs_sb_t *zsb = ITOZSB(ip);
2261 uint64_t atime[2], mtime[2], ctime[2];
2262 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2263 xoptattr_t *xoap = NULL;
2264 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2265 sa_bulk_attr_t bulk[3];
2271 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2273 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
2274 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2275 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2277 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2283 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2284 * Also, if we are the owner don't bother, since owner should
2285 * always be allowed to read basic attributes of file.
2287 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2288 (vap->va_uid != crgetuid(cr))) {
2289 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2297 * Return all attributes. It's cheaper to provide the answer
2298 * than to determine whether we were asked the question.
2301 mutex_enter(&zp->z_lock);
2302 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2303 vap->va_mode = zp->z_mode;
2304 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2305 vap->va_nodeid = zp->z_id;
2306 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2307 links = ZTOI(zp)->i_nlink + 1;
2309 links = ZTOI(zp)->i_nlink;
2310 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2311 vap->va_size = i_size_read(ip);
2312 vap->va_rdev = ip->i_rdev;
2313 vap->va_seq = ip->i_generation;
2316 * Add in any requested optional attributes and the create time.
2317 * Also set the corresponding bits in the returned attribute bitmap.
2319 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2320 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2322 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2323 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2326 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2327 xoap->xoa_readonly =
2328 ((zp->z_pflags & ZFS_READONLY) != 0);
2329 XVA_SET_RTN(xvap, XAT_READONLY);
2332 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2334 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2335 XVA_SET_RTN(xvap, XAT_SYSTEM);
2338 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2340 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2341 XVA_SET_RTN(xvap, XAT_HIDDEN);
2344 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2345 xoap->xoa_nounlink =
2346 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2347 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2350 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2351 xoap->xoa_immutable =
2352 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2353 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2356 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2357 xoap->xoa_appendonly =
2358 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2359 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2362 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2364 ((zp->z_pflags & ZFS_NODUMP) != 0);
2365 XVA_SET_RTN(xvap, XAT_NODUMP);
2368 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2370 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2371 XVA_SET_RTN(xvap, XAT_OPAQUE);
2374 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2375 xoap->xoa_av_quarantined =
2376 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2377 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2380 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2381 xoap->xoa_av_modified =
2382 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2383 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2386 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2387 S_ISREG(ip->i_mode)) {
2388 zfs_sa_get_scanstamp(zp, xvap);
2391 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2394 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2395 times, sizeof (times));
2396 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2397 XVA_SET_RTN(xvap, XAT_CREATETIME);
2400 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2401 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2402 XVA_SET_RTN(xvap, XAT_REPARSE);
2404 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2405 xoap->xoa_generation = ip->i_generation;
2406 XVA_SET_RTN(xvap, XAT_GEN);
2409 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2411 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2412 XVA_SET_RTN(xvap, XAT_OFFLINE);
2415 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2417 ((zp->z_pflags & ZFS_SPARSE) != 0);
2418 XVA_SET_RTN(xvap, XAT_SPARSE);
2422 ZFS_TIME_DECODE(&vap->va_atime, atime);
2423 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2424 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2426 mutex_exit(&zp->z_lock);
2428 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2430 if (zp->z_blksz == 0) {
2432 * Block size hasn't been set; suggest maximal I/O transfers.
2434 vap->va_blksize = zsb->z_max_blksz;
2440 EXPORT_SYMBOL(zfs_getattr);
2443 * Get the basic file attributes and place them in the provided kstat
2444 * structure. The inode is assumed to be the authoritative source
2445 * for most of the attributes. However, the znode currently has the
2446 * authoritative atime, blksize, and block count.
2448 * IN: ip - inode of file.
2450 * OUT: sp - kstat values.
2452 * RETURN: 0 (always succeeds)
2456 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2458 znode_t *zp = ITOZ(ip);
2459 zfs_sb_t *zsb = ITOZSB(ip);
2461 u_longlong_t nblocks;
2466 mutex_enter(&zp->z_lock);
2468 generic_fillattr(ip, sp);
2470 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2471 sp->blksize = blksize;
2472 sp->blocks = nblocks;
2474 if (unlikely(zp->z_blksz == 0)) {
2476 * Block size hasn't been set; suggest maximal I/O transfers.
2478 sp->blksize = zsb->z_max_blksz;
2481 mutex_exit(&zp->z_lock);
2484 * Required to prevent NFS client from detecting different inode
2485 * numbers of snapshot root dentry before and after snapshot mount.
2487 if (zsb->z_issnap) {
2488 if (ip->i_sb->s_root->d_inode == ip)
2489 sp->ino = ZFSCTL_INO_SNAPDIRS -
2490 dmu_objset_id(zsb->z_os);
2497 EXPORT_SYMBOL(zfs_getattr_fast);
2500 * Set the file attributes to the values contained in the
2503 * IN: ip - inode of file to be modified.
2504 * vap - new attribute values.
2505 * If ATTR_XVATTR set, then optional attrs are being set
2506 * flags - ATTR_UTIME set if non-default time values provided.
2507 * - ATTR_NOACLCHECK (CIFS context only).
2508 * cr - credentials of caller.
2510 * RETURN: 0 if success
2511 * error code if failure
2514 * ip - ctime updated, mtime updated if size changed.
2518 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2520 znode_t *zp = ITOZ(ip);
2521 zfs_sb_t *zsb = ITOZSB(ip);
2525 xvattr_t *tmpxvattr;
2526 uint_t mask = vap->va_mask;
2527 uint_t saved_mask = 0;
2530 uint64_t new_kuid = 0, new_kgid = 0, new_uid, new_gid;
2532 uint64_t mtime[2], ctime[2], atime[2];
2534 int need_policy = FALSE;
2536 zfs_fuid_info_t *fuidp = NULL;
2537 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2540 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2541 boolean_t fuid_dirtied = B_FALSE;
2542 sa_bulk_attr_t *bulk, *xattr_bulk;
2543 int count = 0, xattr_count = 0;
2554 * Make sure that if we have ephemeral uid/gid or xvattr specified
2555 * that file system is at proper version level
2558 if (zsb->z_use_fuids == B_FALSE &&
2559 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2560 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2561 (mask & ATTR_XVATTR))) {
2563 return (SET_ERROR(EINVAL));
2566 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2568 return (SET_ERROR(EISDIR));
2571 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2573 return (SET_ERROR(EINVAL));
2577 * If this is an xvattr_t, then get a pointer to the structure of
2578 * optional attributes. If this is NULL, then we have a vattr_t.
2580 xoap = xva_getxoptattr(xvap);
2582 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP);
2583 xva_init(tmpxvattr);
2585 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2586 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2589 * Immutable files can only alter immutable bit and atime
2591 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2592 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2593 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2598 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2604 * Verify timestamps doesn't overflow 32 bits.
2605 * ZFS can handle large timestamps, but 32bit syscalls can't
2606 * handle times greater than 2039. This check should be removed
2607 * once large timestamps are fully supported.
2609 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2610 if (((mask & ATTR_ATIME) &&
2611 TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2612 ((mask & ATTR_MTIME) &&
2613 TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2623 /* Can this be moved to before the top label? */
2624 if (zfs_is_readonly(zsb)) {
2630 * First validate permissions
2633 if (mask & ATTR_SIZE) {
2634 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2639 * XXX - Note, we are not providing any open
2640 * mode flags here (like FNDELAY), so we may
2641 * block if there are locks present... this
2642 * should be addressed in openat().
2644 /* XXX - would it be OK to generate a log record here? */
2645 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2650 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2651 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2652 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2653 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2654 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2655 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2656 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2657 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2658 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2662 if (mask & (ATTR_UID|ATTR_GID)) {
2663 int idmask = (mask & (ATTR_UID|ATTR_GID));
2668 * NOTE: even if a new mode is being set,
2669 * we may clear S_ISUID/S_ISGID bits.
2672 if (!(mask & ATTR_MODE))
2673 vap->va_mode = zp->z_mode;
2676 * Take ownership or chgrp to group we are a member of
2679 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2680 take_group = (mask & ATTR_GID) &&
2681 zfs_groupmember(zsb, vap->va_gid, cr);
2684 * If both ATTR_UID and ATTR_GID are set then take_owner and
2685 * take_group must both be set in order to allow taking
2688 * Otherwise, send the check through secpolicy_vnode_setattr()
2692 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2693 take_owner && take_group) ||
2694 ((idmask == ATTR_UID) && take_owner) ||
2695 ((idmask == ATTR_GID) && take_group)) {
2696 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2697 skipaclchk, cr) == 0) {
2699 * Remove setuid/setgid for non-privileged users
2701 (void) secpolicy_setid_clear(vap, cr);
2702 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2711 mutex_enter(&zp->z_lock);
2712 oldva.va_mode = zp->z_mode;
2713 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2714 if (mask & ATTR_XVATTR) {
2716 * Update xvattr mask to include only those attributes
2717 * that are actually changing.
2719 * the bits will be restored prior to actually setting
2720 * the attributes so the caller thinks they were set.
2722 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2723 if (xoap->xoa_appendonly !=
2724 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2727 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2728 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2732 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2733 if (xoap->xoa_nounlink !=
2734 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2737 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2738 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2742 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2743 if (xoap->xoa_immutable !=
2744 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2747 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2748 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2752 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2753 if (xoap->xoa_nodump !=
2754 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2757 XVA_CLR_REQ(xvap, XAT_NODUMP);
2758 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2762 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2763 if (xoap->xoa_av_modified !=
2764 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2767 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2768 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2772 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2773 if ((!S_ISREG(ip->i_mode) &&
2774 xoap->xoa_av_quarantined) ||
2775 xoap->xoa_av_quarantined !=
2776 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2779 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2780 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2784 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2785 mutex_exit(&zp->z_lock);
2790 if (need_policy == FALSE &&
2791 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2792 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2797 mutex_exit(&zp->z_lock);
2799 if (mask & ATTR_MODE) {
2800 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2801 err = secpolicy_setid_setsticky_clear(ip, vap,
2806 trim_mask |= ATTR_MODE;
2814 * If trim_mask is set then take ownership
2815 * has been granted or write_acl is present and user
2816 * has the ability to modify mode. In that case remove
2817 * UID|GID and or MODE from mask so that
2818 * secpolicy_vnode_setattr() doesn't revoke it.
2822 saved_mask = vap->va_mask;
2823 vap->va_mask &= ~trim_mask;
2825 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2826 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2831 vap->va_mask |= saved_mask;
2835 * secpolicy_vnode_setattr, or take ownership may have
2838 mask = vap->va_mask;
2840 if ((mask & (ATTR_UID | ATTR_GID))) {
2841 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2842 &xattr_obj, sizeof (xattr_obj));
2844 if (err == 0 && xattr_obj) {
2845 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2849 if (mask & ATTR_UID) {
2850 new_kuid = zfs_fuid_create(zsb,
2851 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2852 if (new_kuid != KUID_TO_SUID(ZTOI(zp)->i_uid) &&
2853 zfs_fuid_overquota(zsb, B_FALSE, new_kuid)) {
2861 if (mask & ATTR_GID) {
2862 new_kgid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2863 cr, ZFS_GROUP, &fuidp);
2864 if (new_kgid != KGID_TO_SGID(ZTOI(zp)->i_gid) &&
2865 zfs_fuid_overquota(zsb, B_TRUE, new_kgid)) {
2873 tx = dmu_tx_create(zsb->z_os);
2875 if (mask & ATTR_MODE) {
2876 uint64_t pmode = zp->z_mode;
2878 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2880 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2882 mutex_enter(&zp->z_lock);
2883 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2885 * Are we upgrading ACL from old V0 format
2888 if (zsb->z_version >= ZPL_VERSION_FUID &&
2889 zfs_znode_acl_version(zp) ==
2890 ZFS_ACL_VERSION_INITIAL) {
2891 dmu_tx_hold_free(tx, acl_obj, 0,
2893 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2894 0, aclp->z_acl_bytes);
2896 dmu_tx_hold_write(tx, acl_obj, 0,
2899 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2900 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2901 0, aclp->z_acl_bytes);
2903 mutex_exit(&zp->z_lock);
2904 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2906 if ((mask & ATTR_XVATTR) &&
2907 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2908 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2910 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2914 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2917 fuid_dirtied = zsb->z_fuid_dirty;
2919 zfs_fuid_txhold(zsb, tx);
2921 zfs_sa_upgrade_txholds(tx, zp);
2923 err = dmu_tx_assign(tx, TXG_WAIT);
2929 * Set each attribute requested.
2930 * We group settings according to the locks they need to acquire.
2932 * Note: you cannot set ctime directly, although it will be
2933 * updated as a side-effect of calling this function.
2937 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2938 mutex_enter(&zp->z_acl_lock);
2939 mutex_enter(&zp->z_lock);
2941 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2942 &zp->z_pflags, sizeof (zp->z_pflags));
2945 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2946 mutex_enter(&attrzp->z_acl_lock);
2947 mutex_enter(&attrzp->z_lock);
2948 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2949 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2950 sizeof (attrzp->z_pflags));
2953 if (mask & (ATTR_UID|ATTR_GID)) {
2955 if (mask & ATTR_UID) {
2956 ZTOI(zp)->i_uid = SUID_TO_KUID(new_kuid);
2957 new_uid = zfs_uid_read(ZTOI(zp));
2958 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2959 &new_uid, sizeof (new_uid));
2961 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2962 SA_ZPL_UID(zsb), NULL, &new_uid,
2964 ZTOI(attrzp)->i_uid = SUID_TO_KUID(new_uid);
2968 if (mask & ATTR_GID) {
2969 ZTOI(zp)->i_gid = SGID_TO_KGID(new_kgid);
2970 new_gid = zfs_gid_read(ZTOI(zp));
2971 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2972 NULL, &new_gid, sizeof (new_gid));
2974 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2975 SA_ZPL_GID(zsb), NULL, &new_gid,
2977 ZTOI(attrzp)->i_gid = SGID_TO_KGID(new_kgid);
2980 if (!(mask & ATTR_MODE)) {
2981 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2982 NULL, &new_mode, sizeof (new_mode));
2983 new_mode = zp->z_mode;
2985 err = zfs_acl_chown_setattr(zp);
2988 err = zfs_acl_chown_setattr(attrzp);
2993 if (mask & ATTR_MODE) {
2994 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
2995 &new_mode, sizeof (new_mode));
2996 zp->z_mode = ZTOI(zp)->i_mode = new_mode;
2997 ASSERT3P(aclp, !=, NULL);
2998 err = zfs_aclset_common(zp, aclp, cr, tx);
3000 if (zp->z_acl_cached)
3001 zfs_acl_free(zp->z_acl_cached);
3002 zp->z_acl_cached = aclp;
3006 if ((mask & ATTR_ATIME) || zp->z_atime_dirty) {
3007 zp->z_atime_dirty = 0;
3008 ZFS_TIME_ENCODE(&ip->i_atime, atime);
3009 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
3010 &atime, sizeof (atime));
3013 if (mask & ATTR_MTIME) {
3014 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3015 ZTOI(zp)->i_mtime = timespec_trunc(vap->va_mtime,
3016 ZTOI(zp)->i_sb->s_time_gran);
3018 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
3019 mtime, sizeof (mtime));
3022 if (mask & ATTR_CTIME) {
3023 ZFS_TIME_ENCODE(&vap->va_ctime, ctime);
3024 ZTOI(zp)->i_ctime = timespec_trunc(vap->va_ctime,
3025 ZTOI(zp)->i_sb->s_time_gran);
3026 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
3027 ctime, sizeof (ctime));
3030 if (attrzp && mask) {
3031 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3032 SA_ZPL_CTIME(zsb), NULL, &ctime,
3037 * Do this after setting timestamps to prevent timestamp
3038 * update from toggling bit
3041 if (xoap && (mask & ATTR_XVATTR)) {
3044 * restore trimmed off masks
3045 * so that return masks can be set for caller.
3048 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
3049 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3051 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
3052 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3054 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
3055 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3057 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
3058 XVA_SET_REQ(xvap, XAT_NODUMP);
3060 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
3061 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3063 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
3064 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3067 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3068 ASSERT(S_ISREG(ip->i_mode));
3070 zfs_xvattr_set(zp, xvap, tx);
3074 zfs_fuid_sync(zsb, tx);
3077 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3079 mutex_exit(&zp->z_lock);
3080 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3081 mutex_exit(&zp->z_acl_lock);
3084 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3085 mutex_exit(&attrzp->z_acl_lock);
3086 mutex_exit(&attrzp->z_lock);
3089 if (err == 0 && attrzp) {
3090 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3101 zfs_fuid_info_free(fuidp);
3107 if (err == ERESTART)
3110 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3112 zfs_inode_update(zp);
3116 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3117 zil_commit(zilog, 0);
3120 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * 7);
3121 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 7);
3122 kmem_free(tmpxvattr, sizeof (xvattr_t));
3126 EXPORT_SYMBOL(zfs_setattr);
3128 typedef struct zfs_zlock {
3129 krwlock_t *zl_rwlock; /* lock we acquired */
3130 znode_t *zl_znode; /* znode we held */
3131 struct zfs_zlock *zl_next; /* next in list */
3135 * Drop locks and release vnodes that were held by zfs_rename_lock().
3138 zfs_rename_unlock(zfs_zlock_t **zlpp)
3142 while ((zl = *zlpp) != NULL) {
3143 if (zl->zl_znode != NULL)
3144 iput(ZTOI(zl->zl_znode));
3145 rw_exit(zl->zl_rwlock);
3146 *zlpp = zl->zl_next;
3147 kmem_free(zl, sizeof (*zl));
3152 * Search back through the directory tree, using the ".." entries.
3153 * Lock each directory in the chain to prevent concurrent renames.
3154 * Fail any attempt to move a directory into one of its own descendants.
3155 * XXX - z_parent_lock can overlap with map or grow locks
3158 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3162 uint64_t rootid = ZTOZSB(zp)->z_root;
3163 uint64_t oidp = zp->z_id;
3164 krwlock_t *rwlp = &szp->z_parent_lock;
3165 krw_t rw = RW_WRITER;
3168 * First pass write-locks szp and compares to zp->z_id.
3169 * Later passes read-lock zp and compare to zp->z_parent.
3172 if (!rw_tryenter(rwlp, rw)) {
3174 * Another thread is renaming in this path.
3175 * Note that if we are a WRITER, we don't have any
3176 * parent_locks held yet.
3178 if (rw == RW_READER && zp->z_id > szp->z_id) {
3180 * Drop our locks and restart
3182 zfs_rename_unlock(&zl);
3186 rwlp = &szp->z_parent_lock;
3191 * Wait for other thread to drop its locks
3197 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3198 zl->zl_rwlock = rwlp;
3199 zl->zl_znode = NULL;
3200 zl->zl_next = *zlpp;
3203 if (oidp == szp->z_id) /* We're a descendant of szp */
3204 return (SET_ERROR(EINVAL));
3206 if (oidp == rootid) /* We've hit the top */
3209 if (rw == RW_READER) { /* i.e. not the first pass */
3210 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3215 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3216 &oidp, sizeof (oidp));
3217 rwlp = &zp->z_parent_lock;
3220 } while (zp->z_id != sdzp->z_id);
3226 * Move an entry from the provided source directory to the target
3227 * directory. Change the entry name as indicated.
3229 * IN: sdip - Source directory containing the "old entry".
3230 * snm - Old entry name.
3231 * tdip - Target directory to contain the "new entry".
3232 * tnm - New entry name.
3233 * cr - credentials of caller.
3234 * flags - case flags
3236 * RETURN: 0 on success, error code on failure.
3239 * sdip,tdip - ctime|mtime updated
3243 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3244 cred_t *cr, int flags)
3246 znode_t *tdzp, *szp, *tzp;
3247 znode_t *sdzp = ITOZ(sdip);
3248 zfs_sb_t *zsb = ITOZSB(sdip);
3250 zfs_dirlock_t *sdl, *tdl;
3253 int cmp, serr, terr;
3256 boolean_t waited = B_FALSE;
3259 ZFS_VERIFY_ZP(sdzp);
3263 ZFS_VERIFY_ZP(tdzp);
3266 * We check i_sb because snapshots and the ctldir must have different
3269 if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) {
3271 return (SET_ERROR(EXDEV));
3274 if (zsb->z_utf8 && u8_validate(tnm,
3275 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3277 return (SET_ERROR(EILSEQ));
3280 if (flags & FIGNORECASE)
3289 * This is to prevent the creation of links into attribute space
3290 * by renaming a linked file into/outof an attribute directory.
3291 * See the comment in zfs_link() for why this is considered bad.
3293 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3295 return (SET_ERROR(EINVAL));
3299 * Lock source and target directory entries. To prevent deadlock,
3300 * a lock ordering must be defined. We lock the directory with
3301 * the smallest object id first, or if it's a tie, the one with
3302 * the lexically first name.
3304 if (sdzp->z_id < tdzp->z_id) {
3306 } else if (sdzp->z_id > tdzp->z_id) {
3310 * First compare the two name arguments without
3311 * considering any case folding.
3313 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3315 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3316 ASSERT(error == 0 || !zsb->z_utf8);
3319 * POSIX: "If the old argument and the new argument
3320 * both refer to links to the same existing file,
3321 * the rename() function shall return successfully
3322 * and perform no other action."
3328 * If the file system is case-folding, then we may
3329 * have some more checking to do. A case-folding file
3330 * system is either supporting mixed case sensitivity
3331 * access or is completely case-insensitive. Note
3332 * that the file system is always case preserving.
3334 * In mixed sensitivity mode case sensitive behavior
3335 * is the default. FIGNORECASE must be used to
3336 * explicitly request case insensitive behavior.
3338 * If the source and target names provided differ only
3339 * by case (e.g., a request to rename 'tim' to 'Tim'),
3340 * we will treat this as a special case in the
3341 * case-insensitive mode: as long as the source name
3342 * is an exact match, we will allow this to proceed as
3343 * a name-change request.
3345 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3346 (zsb->z_case == ZFS_CASE_MIXED &&
3347 flags & FIGNORECASE)) &&
3348 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3351 * case preserving rename request, require exact
3360 * If the source and destination directories are the same, we should
3361 * grab the z_name_lock of that directory only once.
3365 rw_enter(&sdzp->z_name_lock, RW_READER);
3369 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3370 ZEXISTS | zflg, NULL, NULL);
3371 terr = zfs_dirent_lock(&tdl,
3372 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3374 terr = zfs_dirent_lock(&tdl,
3375 tdzp, tnm, &tzp, zflg, NULL, NULL);
3376 serr = zfs_dirent_lock(&sdl,
3377 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3383 * Source entry invalid or not there.
3386 zfs_dirent_unlock(tdl);
3392 rw_exit(&sdzp->z_name_lock);
3394 if (strcmp(snm, "..") == 0)
3400 zfs_dirent_unlock(sdl);
3404 rw_exit(&sdzp->z_name_lock);
3406 if (strcmp(tnm, "..") == 0)
3413 * Must have write access at the source to remove the old entry
3414 * and write access at the target to create the new entry.
3415 * Note that if target and source are the same, this can be
3416 * done in a single check.
3419 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3422 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3424 * Check to make sure rename is valid.
3425 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3427 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3432 * Does target exist?
3436 * Source and target must be the same type.
3438 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3439 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3440 error = SET_ERROR(ENOTDIR);
3444 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3445 error = SET_ERROR(EISDIR);
3450 * POSIX dictates that when the source and target
3451 * entries refer to the same file object, rename
3452 * must do nothing and exit without error.
3454 if (szp->z_id == tzp->z_id) {
3460 tx = dmu_tx_create(zsb->z_os);
3461 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3462 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3463 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3464 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3466 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3467 zfs_sa_upgrade_txholds(tx, tdzp);
3470 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3471 zfs_sa_upgrade_txholds(tx, tzp);
3474 zfs_sa_upgrade_txholds(tx, szp);
3475 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3476 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3479 zfs_rename_unlock(&zl);
3480 zfs_dirent_unlock(sdl);
3481 zfs_dirent_unlock(tdl);
3484 rw_exit(&sdzp->z_name_lock);
3489 if (error == ERESTART) {
3500 if (tzp) /* Attempt to remove the existing target */
3501 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3504 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3506 szp->z_pflags |= ZFS_AV_MODIFIED;
3508 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3509 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3512 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3514 zfs_log_rename(zilog, tx, TX_RENAME |
3515 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3516 sdl->dl_name, tdzp, tdl->dl_name, szp);
3519 * At this point, we have successfully created
3520 * the target name, but have failed to remove
3521 * the source name. Since the create was done
3522 * with the ZRENAMING flag, there are
3523 * complications; for one, the link count is
3524 * wrong. The easiest way to deal with this
3525 * is to remove the newly created target, and
3526 * return the original error. This must
3527 * succeed; fortunately, it is very unlikely to
3528 * fail, since we just created it.
3530 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3531 ZRENAMING, NULL), ==, 0);
3539 zfs_rename_unlock(&zl);
3541 zfs_dirent_unlock(sdl);
3542 zfs_dirent_unlock(tdl);
3544 zfs_inode_update(sdzp);
3546 rw_exit(&sdzp->z_name_lock);
3549 zfs_inode_update(tdzp);
3551 zfs_inode_update(szp);
3554 zfs_inode_update(tzp);
3558 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3559 zil_commit(zilog, 0);
3564 EXPORT_SYMBOL(zfs_rename);
3567 * Insert the indicated symbolic reference entry into the directory.
3569 * IN: dip - Directory to contain new symbolic link.
3570 * link - Name for new symlink entry.
3571 * vap - Attributes of new entry.
3572 * target - Target path of new symlink.
3574 * cr - credentials of caller.
3575 * flags - case flags
3577 * RETURN: 0 on success, error code on failure.
3580 * dip - ctime|mtime updated
3584 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3585 struct inode **ipp, cred_t *cr, int flags)
3587 znode_t *zp, *dzp = ITOZ(dip);
3590 zfs_sb_t *zsb = ITOZSB(dip);
3592 uint64_t len = strlen(link);
3595 zfs_acl_ids_t acl_ids;
3596 boolean_t fuid_dirtied;
3597 uint64_t txtype = TX_SYMLINK;
3598 boolean_t waited = B_FALSE;
3600 ASSERT(S_ISLNK(vap->va_mode));
3606 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3607 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3609 return (SET_ERROR(EILSEQ));
3611 if (flags & FIGNORECASE)
3614 if (len > MAXPATHLEN) {
3616 return (SET_ERROR(ENAMETOOLONG));
3619 if ((error = zfs_acl_ids_create(dzp, 0,
3620 vap, cr, NULL, &acl_ids)) != 0) {
3628 * Attempt to lock directory; fail if entry already exists.
3630 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3632 zfs_acl_ids_free(&acl_ids);
3637 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3638 zfs_acl_ids_free(&acl_ids);
3639 zfs_dirent_unlock(dl);
3644 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3645 zfs_acl_ids_free(&acl_ids);
3646 zfs_dirent_unlock(dl);
3648 return (SET_ERROR(EDQUOT));
3650 tx = dmu_tx_create(zsb->z_os);
3651 fuid_dirtied = zsb->z_fuid_dirty;
3652 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3653 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3654 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3655 ZFS_SA_BASE_ATTR_SIZE + len);
3656 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3657 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3658 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3659 acl_ids.z_aclp->z_acl_bytes);
3662 zfs_fuid_txhold(zsb, tx);
3663 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3665 zfs_dirent_unlock(dl);
3666 if (error == ERESTART) {
3672 zfs_acl_ids_free(&acl_ids);
3679 * Create a new object for the symlink.
3680 * for version 4 ZPL datsets the symlink will be an SA attribute
3682 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3685 zfs_fuid_sync(zsb, tx);
3687 mutex_enter(&zp->z_lock);
3689 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3692 zfs_sa_symlink(zp, link, len, tx);
3693 mutex_exit(&zp->z_lock);
3696 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3697 &zp->z_size, sizeof (zp->z_size), tx);
3699 * Insert the new object into the directory.
3701 (void) zfs_link_create(dl, zp, tx, ZNEW);
3703 if (flags & FIGNORECASE)
3705 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3707 zfs_inode_update(dzp);
3708 zfs_inode_update(zp);
3710 zfs_acl_ids_free(&acl_ids);
3714 zfs_dirent_unlock(dl);
3718 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3719 zil_commit(zilog, 0);
3724 EXPORT_SYMBOL(zfs_symlink);
3727 * Return, in the buffer contained in the provided uio structure,
3728 * the symbolic path referred to by ip.
3730 * IN: ip - inode of symbolic link
3731 * uio - structure to contain the link path.
3732 * cr - credentials of caller.
3734 * RETURN: 0 if success
3735 * error code if failure
3738 * ip - atime updated
3742 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3744 znode_t *zp = ITOZ(ip);
3745 zfs_sb_t *zsb = ITOZSB(ip);
3751 mutex_enter(&zp->z_lock);
3753 error = sa_lookup_uio(zp->z_sa_hdl,
3754 SA_ZPL_SYMLINK(zsb), uio);
3756 error = zfs_sa_readlink(zp, uio);
3757 mutex_exit(&zp->z_lock);
3762 EXPORT_SYMBOL(zfs_readlink);
3765 * Insert a new entry into directory tdip referencing sip.
3767 * IN: tdip - Directory to contain new entry.
3768 * sip - inode of new entry.
3769 * name - name of new entry.
3770 * cr - credentials of caller.
3772 * RETURN: 0 if success
3773 * error code if failure
3776 * tdip - ctime|mtime updated
3777 * sip - ctime updated
3781 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr,
3784 znode_t *dzp = ITOZ(tdip);
3786 zfs_sb_t *zsb = ITOZSB(tdip);
3794 boolean_t waited = B_FALSE;
3796 ASSERT(S_ISDIR(tdip->i_mode));
3803 * POSIX dictates that we return EPERM here.
3804 * Better choices include ENOTSUP or EISDIR.
3806 if (S_ISDIR(sip->i_mode)) {
3808 return (SET_ERROR(EPERM));
3815 * We check i_sb because snapshots and the ctldir must have different
3818 if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) {
3820 return (SET_ERROR(EXDEV));
3823 /* Prevent links to .zfs/shares files */
3825 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3826 &parent, sizeof (uint64_t))) != 0) {
3830 if (parent == zsb->z_shares_dir) {
3832 return (SET_ERROR(EPERM));
3835 if (zsb->z_utf8 && u8_validate(name,
3836 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3838 return (SET_ERROR(EILSEQ));
3840 if (flags & FIGNORECASE)
3844 * We do not support links between attributes and non-attributes
3845 * because of the potential security risk of creating links
3846 * into "normal" file space in order to circumvent restrictions
3847 * imposed in attribute space.
3849 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3851 return (SET_ERROR(EINVAL));
3854 owner = zfs_fuid_map_id(zsb, KUID_TO_SUID(sip->i_uid), cr, ZFS_OWNER);
3855 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3857 return (SET_ERROR(EPERM));
3860 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3867 * Attempt to lock directory; fail if entry already exists.
3869 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3875 tx = dmu_tx_create(zsb->z_os);
3876 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3877 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3878 zfs_sa_upgrade_txholds(tx, szp);
3879 zfs_sa_upgrade_txholds(tx, dzp);
3880 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3882 zfs_dirent_unlock(dl);
3883 if (error == ERESTART) {
3894 error = zfs_link_create(dl, szp, tx, 0);
3897 uint64_t txtype = TX_LINK;
3898 if (flags & FIGNORECASE)
3900 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3905 zfs_dirent_unlock(dl);
3907 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3908 zil_commit(zilog, 0);
3910 zfs_inode_update(dzp);
3911 zfs_inode_update(szp);
3915 EXPORT_SYMBOL(zfs_link);
3918 zfs_putpage_commit_cb(void *arg)
3920 struct page *pp = arg;
3923 end_page_writeback(pp);
3927 * Push a page out to disk, once the page is on stable storage the
3928 * registered commit callback will be run as notification of completion.
3930 * IN: ip - page mapped for inode.
3931 * pp - page to push (page is locked)
3932 * wbc - writeback control data
3934 * RETURN: 0 if success
3935 * error code if failure
3938 * ip - ctime|mtime updated
3942 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
3944 znode_t *zp = ITOZ(ip);
3945 zfs_sb_t *zsb = ITOZSB(ip);
3953 uint64_t mtime[2], ctime[2];
3954 sa_bulk_attr_t bulk[3];
3956 struct address_space *mapping;
3961 ASSERT(PageLocked(pp));
3963 pgoff = page_offset(pp); /* Page byte-offset in file */
3964 offset = i_size_read(ip); /* File length in bytes */
3965 pglen = MIN(PAGE_SIZE, /* Page length in bytes */
3966 P2ROUNDUP(offset, PAGE_SIZE)-pgoff);
3968 /* Page is beyond end of file */
3969 if (pgoff >= offset) {
3975 /* Truncate page length to end of file */
3976 if (pgoff + pglen > offset)
3977 pglen = offset - pgoff;
3981 * FIXME: Allow mmap writes past its quota. The correct fix
3982 * is to register a page_mkwrite() handler to count the page
3983 * against its quota when it is about to be dirtied.
3985 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
3986 zfs_owner_overquota(zsb, zp, B_TRUE)) {
3992 * The ordering here is critical and must adhere to the following
3993 * rules in order to avoid deadlocking in either zfs_read() or
3994 * zfs_free_range() due to a lock inversion.
3996 * 1) The page must be unlocked prior to acquiring the range lock.
3997 * This is critical because zfs_read() calls find_lock_page()
3998 * which may block on the page lock while holding the range lock.
4000 * 2) Before setting or clearing write back on a page the range lock
4001 * must be held in order to prevent a lock inversion with the
4002 * zfs_free_range() function.
4004 * This presents a problem because upon entering this function the
4005 * page lock is already held. To safely acquire the range lock the
4006 * page lock must be dropped. This creates a window where another
4007 * process could truncate, invalidate, dirty, or write out the page.
4009 * Therefore, after successfully reacquiring the range and page locks
4010 * the current page state is checked. In the common case everything
4011 * will be as is expected and it can be written out. However, if
4012 * the page state has changed it must be handled accordingly.
4014 mapping = pp->mapping;
4015 redirty_page_for_writepage(wbc, pp);
4018 rl = zfs_range_lock(&zp->z_range_lock, pgoff, pglen, RL_WRITER);
4021 /* Page mapping changed or it was no longer dirty, we're done */
4022 if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) {
4024 zfs_range_unlock(rl);
4029 /* Another process started write block if required */
4030 if (PageWriteback(pp)) {
4032 zfs_range_unlock(rl);
4034 if (wbc->sync_mode != WB_SYNC_NONE)
4035 wait_on_page_writeback(pp);
4041 /* Clear the dirty flag the required locks are held */
4042 if (!clear_page_dirty_for_io(pp)) {
4044 zfs_range_unlock(rl);
4050 * Counterpart for redirty_page_for_writepage() above. This page
4051 * was in fact not skipped and should not be counted as if it were.
4053 wbc->pages_skipped--;
4054 set_page_writeback(pp);
4057 tx = dmu_tx_create(zsb->z_os);
4058 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
4059 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4060 zfs_sa_upgrade_txholds(tx, zp);
4062 err = dmu_tx_assign(tx, TXG_NOWAIT);
4064 if (err == ERESTART)
4068 __set_page_dirty_nobuffers(pp);
4070 end_page_writeback(pp);
4071 zfs_range_unlock(rl);
4077 ASSERT3U(pglen, <=, PAGE_SIZE);
4078 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
4081 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
4082 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
4083 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
4085 /* Preserve the mtime and ctime provided by the inode */
4086 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4087 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4088 zp->z_atime_dirty = 0;
4091 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4093 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
4094 zfs_putpage_commit_cb, pp);
4097 zfs_range_unlock(rl);
4099 if (wbc->sync_mode != WB_SYNC_NONE) {
4101 * Note that this is rarely called under writepages(), because
4102 * writepages() normally handles the entire commit for
4103 * performance reasons.
4105 if (zsb->z_log != NULL)
4106 zil_commit(zsb->z_log, zp->z_id);
4114 * Update the system attributes when the inode has been dirtied. For the
4115 * moment we only update the mode, atime, mtime, and ctime.
4118 zfs_dirty_inode(struct inode *ip, int flags)
4120 znode_t *zp = ITOZ(ip);
4121 zfs_sb_t *zsb = ITOZSB(ip);
4123 uint64_t mode, atime[2], mtime[2], ctime[2];
4124 sa_bulk_attr_t bulk[4];
4128 if (zfs_is_readonly(zsb) || dmu_objset_is_snapshot(zsb->z_os))
4136 * This is the lazytime semantic indroduced in Linux 4.0
4137 * This flag will only be called from update_time when lazytime is set.
4138 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4139 * Fortunately mtime and ctime are managed within ZFS itself, so we
4140 * only need to dirty atime.
4142 if (flags == I_DIRTY_TIME) {
4143 zp->z_atime_dirty = 1;
4148 tx = dmu_tx_create(zsb->z_os);
4150 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4151 zfs_sa_upgrade_txholds(tx, zp);
4153 error = dmu_tx_assign(tx, TXG_WAIT);
4159 mutex_enter(&zp->z_lock);
4160 zp->z_atime_dirty = 0;
4162 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zsb), NULL, &mode, 8);
4163 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
4164 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
4165 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
4167 /* Preserve the mode, mtime and ctime provided by the inode */
4168 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4169 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4170 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4175 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4176 mutex_exit(&zp->z_lock);
4183 EXPORT_SYMBOL(zfs_dirty_inode);
4187 zfs_inactive(struct inode *ip)
4189 znode_t *zp = ITOZ(ip);
4190 zfs_sb_t *zsb = ITOZSB(ip);
4193 int need_unlock = 0;
4195 /* Only read lock if we haven't already write locked, e.g. rollback */
4196 if (!RW_WRITE_HELD(&zsb->z_teardown_inactive_lock)) {
4198 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
4200 if (zp->z_sa_hdl == NULL) {
4202 rw_exit(&zsb->z_teardown_inactive_lock);
4206 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4207 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
4209 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4210 zfs_sa_upgrade_txholds(tx, zp);
4211 error = dmu_tx_assign(tx, TXG_WAIT);
4215 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4216 mutex_enter(&zp->z_lock);
4217 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
4218 (void *)&atime, sizeof (atime), tx);
4219 zp->z_atime_dirty = 0;
4220 mutex_exit(&zp->z_lock);
4227 rw_exit(&zsb->z_teardown_inactive_lock);
4229 EXPORT_SYMBOL(zfs_inactive);
4232 * Bounds-check the seek operation.
4234 * IN: ip - inode seeking within
4235 * ooff - old file offset
4236 * noffp - pointer to new file offset
4237 * ct - caller context
4239 * RETURN: 0 if success
4240 * EINVAL if new offset invalid
4244 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4246 if (S_ISDIR(ip->i_mode))
4248 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4250 EXPORT_SYMBOL(zfs_seek);
4253 * Fill pages with data from the disk.
4256 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4258 znode_t *zp = ITOZ(ip);
4259 zfs_sb_t *zsb = ITOZSB(ip);
4261 struct page *cur_pp;
4262 u_offset_t io_off, total;
4269 io_len = nr_pages << PAGE_SHIFT;
4270 i_size = i_size_read(ip);
4271 io_off = page_offset(pl[0]);
4273 if (io_off + io_len > i_size)
4274 io_len = i_size - io_off;
4277 * Iterate over list of pages and read each page individually.
4280 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4283 cur_pp = pl[page_idx++];
4285 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4289 /* convert checksum errors into IO errors */
4291 err = SET_ERROR(EIO);
4300 * Uses zfs_fillpage to read data from the file and fill the pages.
4302 * IN: ip - inode of file to get data from.
4303 * pl - list of pages to read
4304 * nr_pages - number of pages to read
4306 * RETURN: 0 on success, error code on failure.
4309 * vp - atime updated
4313 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4315 znode_t *zp = ITOZ(ip);
4316 zfs_sb_t *zsb = ITOZSB(ip);
4325 err = zfs_fillpage(ip, pl, nr_pages);
4330 EXPORT_SYMBOL(zfs_getpage);
4333 * Check ZFS specific permissions to memory map a section of a file.
4335 * IN: ip - inode of the file to mmap
4337 * addrp - start address in memory region
4338 * len - length of memory region
4339 * vm_flags- address flags
4341 * RETURN: 0 if success
4342 * error code if failure
4346 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4347 unsigned long vm_flags)
4349 znode_t *zp = ITOZ(ip);
4350 zfs_sb_t *zsb = ITOZSB(ip);
4355 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4356 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4358 return (SET_ERROR(EPERM));
4361 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4362 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4364 return (SET_ERROR(EACCES));
4367 if (off < 0 || len > MAXOFFSET_T - off) {
4369 return (SET_ERROR(ENXIO));
4375 EXPORT_SYMBOL(zfs_map);
4378 * convoff - converts the given data (start, whence) to the
4382 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4387 if ((lckdat->l_whence == 2) || (whence == 2)) {
4388 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4392 switch (lckdat->l_whence) {
4394 lckdat->l_start += offset;
4397 lckdat->l_start += vap.va_size;
4402 return (SET_ERROR(EINVAL));
4405 if (lckdat->l_start < 0)
4406 return (SET_ERROR(EINVAL));
4410 lckdat->l_start -= offset;
4413 lckdat->l_start -= vap.va_size;
4418 return (SET_ERROR(EINVAL));
4421 lckdat->l_whence = (short)whence;
4426 * Free or allocate space in a file. Currently, this function only
4427 * supports the `F_FREESP' command. However, this command is somewhat
4428 * misnamed, as its functionality includes the ability to allocate as
4429 * well as free space.
4431 * IN: ip - inode of file to free data in.
4432 * cmd - action to take (only F_FREESP supported).
4433 * bfp - section of file to free/alloc.
4434 * flag - current file open mode flags.
4435 * offset - current file offset.
4436 * cr - credentials of caller [UNUSED].
4438 * RETURN: 0 on success, error code on failure.
4441 * ip - ctime|mtime updated
4445 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4446 offset_t offset, cred_t *cr)
4448 znode_t *zp = ITOZ(ip);
4449 zfs_sb_t *zsb = ITOZSB(ip);
4456 if (cmd != F_FREESP) {
4458 return (SET_ERROR(EINVAL));
4462 * Callers might not be able to detect properly that we are read-only,
4463 * so check it explicitly here.
4465 if (zfs_is_readonly(zsb)) {
4467 return (SET_ERROR(EROFS));
4470 if ((error = convoff(ip, bfp, 0, offset))) {
4475 if (bfp->l_len < 0) {
4477 return (SET_ERROR(EINVAL));
4481 * Permissions aren't checked on Solaris because on this OS
4482 * zfs_space() can only be called with an opened file handle.
4483 * On Linux we can get here through truncate_range() which
4484 * operates directly on inodes, so we need to check access rights.
4486 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4492 len = bfp->l_len; /* 0 means from off to end of file */
4494 error = zfs_freesp(zp, off, len, flag, TRUE);
4499 EXPORT_SYMBOL(zfs_space);
4503 zfs_fid(struct inode *ip, fid_t *fidp)
4505 znode_t *zp = ITOZ(ip);
4506 zfs_sb_t *zsb = ITOZSB(ip);
4509 uint64_t object = zp->z_id;
4516 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4517 &gen64, sizeof (uint64_t))) != 0) {
4522 gen = (uint32_t)gen64;
4524 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4525 if (fidp->fid_len < size) {
4526 fidp->fid_len = size;
4528 return (SET_ERROR(ENOSPC));
4531 zfid = (zfid_short_t *)fidp;
4533 zfid->zf_len = size;
4535 for (i = 0; i < sizeof (zfid->zf_object); i++)
4536 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4538 /* Must have a non-zero generation number to distinguish from .zfs */
4541 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4542 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4544 if (size == LONG_FID_LEN) {
4545 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4548 zlfid = (zfid_long_t *)fidp;
4550 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4551 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4553 /* XXX - this should be the generation number for the objset */
4554 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4555 zlfid->zf_setgen[i] = 0;
4561 EXPORT_SYMBOL(zfs_fid);
4565 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4567 znode_t *zp = ITOZ(ip);
4568 zfs_sb_t *zsb = ITOZSB(ip);
4570 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4574 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4579 EXPORT_SYMBOL(zfs_getsecattr);
4583 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4585 znode_t *zp = ITOZ(ip);
4586 zfs_sb_t *zsb = ITOZSB(ip);
4588 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4589 zilog_t *zilog = zsb->z_log;
4594 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4596 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4597 zil_commit(zilog, 0);
4602 EXPORT_SYMBOL(zfs_setsecattr);
4604 #ifdef HAVE_UIO_ZEROCOPY
4606 * Tunable, both must be a power of 2.
4608 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4609 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4610 * an arcbuf for a partial block read
4612 int zcr_blksz_min = (1 << 10); /* 1K */
4613 int zcr_blksz_max = (1 << 17); /* 128K */
4617 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4619 znode_t *zp = ITOZ(ip);
4620 zfs_sb_t *zsb = ITOZSB(ip);
4621 int max_blksz = zsb->z_max_blksz;
4622 uio_t *uio = &xuio->xu_uio;
4623 ssize_t size = uio->uio_resid;
4624 offset_t offset = uio->uio_loffset;
4629 int preamble, postamble;
4631 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4632 return (SET_ERROR(EINVAL));
4639 * Loan out an arc_buf for write if write size is bigger than
4640 * max_blksz, and the file's block size is also max_blksz.
4643 if (size < blksz || zp->z_blksz != blksz) {
4645 return (SET_ERROR(EINVAL));
4648 * Caller requests buffers for write before knowing where the
4649 * write offset might be (e.g. NFS TCP write).
4654 preamble = P2PHASE(offset, blksz);
4656 preamble = blksz - preamble;
4661 postamble = P2PHASE(size, blksz);
4664 fullblk = size / blksz;
4665 (void) dmu_xuio_init(xuio,
4666 (preamble != 0) + fullblk + (postamble != 0));
4669 * Have to fix iov base/len for partial buffers. They
4670 * currently represent full arc_buf's.
4673 /* data begins in the middle of the arc_buf */
4674 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4677 (void) dmu_xuio_add(xuio, abuf,
4678 blksz - preamble, preamble);
4681 for (i = 0; i < fullblk; i++) {
4682 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4685 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4689 /* data ends in the middle of the arc_buf */
4690 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4693 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4698 * Loan out an arc_buf for read if the read size is larger than
4699 * the current file block size. Block alignment is not
4700 * considered. Partial arc_buf will be loaned out for read.
4702 blksz = zp->z_blksz;
4703 if (blksz < zcr_blksz_min)
4704 blksz = zcr_blksz_min;
4705 if (blksz > zcr_blksz_max)
4706 blksz = zcr_blksz_max;
4707 /* avoid potential complexity of dealing with it */
4708 if (blksz > max_blksz) {
4710 return (SET_ERROR(EINVAL));
4713 maxsize = zp->z_size - uio->uio_loffset;
4719 return (SET_ERROR(EINVAL));
4724 return (SET_ERROR(EINVAL));
4727 uio->uio_extflg = UIO_XUIO;
4728 XUIO_XUZC_RW(xuio) = ioflag;
4735 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4739 int ioflag = XUIO_XUZC_RW(xuio);
4741 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4743 i = dmu_xuio_cnt(xuio);
4745 abuf = dmu_xuio_arcbuf(xuio, i);
4747 * if abuf == NULL, it must be a write buffer
4748 * that has been returned in zfs_write().
4751 dmu_return_arcbuf(abuf);
4752 ASSERT(abuf || ioflag == UIO_WRITE);
4755 dmu_xuio_fini(xuio);
4758 #endif /* HAVE_UIO_ZEROCOPY */
4760 #if defined(_KERNEL) && defined(HAVE_SPL)
4761 module_param(zfs_delete_blocks, ulong, 0644);
4762 MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async");
4763 module_param(zfs_read_chunk_size, long, 0644);
4764 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");