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;
601 sa_bulk_attr_t bulk[4];
602 uint64_t mtime[2], ctime[2];
604 #ifdef HAVE_UIO_ZEROCOPY
606 const iovec_t *iovp = uio->uio_iov;
607 ASSERTV(int iovcnt = uio->uio_iovcnt);
611 * Fasttrack empty write
617 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
623 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
624 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
625 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
626 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
630 * Callers might not be able to detect properly that we are read-only,
631 * so check it explicitly here.
633 if (zfs_is_readonly(zsb)) {
635 return (SET_ERROR(EROFS));
639 * If immutable or not appending then return EPERM
641 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
642 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
643 (uio->uio_loffset < zp->z_size))) {
645 return (SET_ERROR(EPERM));
651 * Validate file offset
653 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
656 return (SET_ERROR(EINVAL));
660 * Pre-fault the pages to ensure slow (eg NFS) pages
662 * Skip this if uio contains loaned arc_buf.
664 #ifdef HAVE_UIO_ZEROCOPY
665 if ((uio->uio_extflg == UIO_XUIO) &&
666 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
667 xuio = (xuio_t *)uio;
670 uio_prefaultpages(MIN(n, max_blksz), uio);
673 * If in append mode, set the io offset pointer to eof.
675 if (ioflag & FAPPEND) {
677 * Obtain an appending range lock to guarantee file append
678 * semantics. We reset the write offset once we have the lock.
680 rl = zfs_range_lock(&zp->z_range_lock, 0, n, RL_APPEND);
682 if (rl->r_len == UINT64_MAX) {
684 * We overlocked the file because this write will cause
685 * the file block size to increase.
686 * Note that zp_size cannot change with this lock held.
690 uio->uio_loffset = woff;
693 * Note that if the file block size will change as a result of
694 * this write, then this range lock will lock the entire file
695 * so that we can re-write the block safely.
697 rl = zfs_range_lock(&zp->z_range_lock, woff, n, RL_WRITER);
701 zfs_range_unlock(rl);
703 return (SET_ERROR(EFBIG));
706 if ((woff + n) > limit || woff > (limit - n))
709 /* Will this write extend the file length? */
710 write_eof = (woff + n > zp->z_size);
712 end_size = MAX(zp->z_size, woff + n);
715 * Write the file in reasonable size chunks. Each chunk is written
716 * in a separate transaction; this keeps the intent log records small
717 * and allows us to do more fine-grained space accounting.
721 woff = uio->uio_loffset;
722 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
723 zfs_owner_overquota(zsb, zp, B_TRUE)) {
725 dmu_return_arcbuf(abuf);
726 error = SET_ERROR(EDQUOT);
730 if (xuio && abuf == NULL) {
731 #ifdef HAVE_UIO_ZEROCOPY
732 ASSERT(i_iov < iovcnt);
733 ASSERT3U(uio->uio_segflg, !=, UIO_BVEC);
735 abuf = dmu_xuio_arcbuf(xuio, i_iov);
736 dmu_xuio_clear(xuio, i_iov);
737 ASSERT((aiov->iov_base == abuf->b_data) ||
738 ((char *)aiov->iov_base - (char *)abuf->b_data +
739 aiov->iov_len == arc_buf_size(abuf)));
742 } else if (abuf == NULL && n >= max_blksz &&
743 woff >= zp->z_size &&
744 P2PHASE(woff, max_blksz) == 0 &&
745 zp->z_blksz == max_blksz) {
747 * This write covers a full block. "Borrow" a buffer
748 * from the dmu so that we can fill it before we enter
749 * a transaction. This avoids the possibility of
750 * holding up the transaction if the data copy hangs
751 * up on a pagefault (e.g., from an NFS server mapping).
755 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
757 ASSERT(abuf != NULL);
758 ASSERT(arc_buf_size(abuf) == max_blksz);
759 if ((error = uiocopy(abuf->b_data, max_blksz,
760 UIO_WRITE, uio, &cbytes))) {
761 dmu_return_arcbuf(abuf);
764 ASSERT(cbytes == max_blksz);
768 * Start a transaction.
770 tx = dmu_tx_create(zsb->z_os);
771 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
772 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
773 zfs_sa_upgrade_txholds(tx, zp);
774 error = dmu_tx_assign(tx, TXG_WAIT);
778 dmu_return_arcbuf(abuf);
783 * If zfs_range_lock() over-locked we grow the blocksize
784 * and then reduce the lock range. This will only happen
785 * on the first iteration since zfs_range_reduce() will
786 * shrink down r_len to the appropriate size.
788 if (rl->r_len == UINT64_MAX) {
791 if (zp->z_blksz > max_blksz) {
793 * File's blocksize is already larger than the
794 * "recordsize" property. Only let it grow to
795 * the next power of 2.
797 ASSERT(!ISP2(zp->z_blksz));
798 new_blksz = MIN(end_size,
799 1 << highbit64(zp->z_blksz));
801 new_blksz = MIN(end_size, max_blksz);
803 zfs_grow_blocksize(zp, new_blksz, tx);
804 zfs_range_reduce(rl, woff, n);
808 * XXX - should we really limit each write to z_max_blksz?
809 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
811 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
814 tx_bytes = uio->uio_resid;
815 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
817 tx_bytes -= uio->uio_resid;
820 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
822 * If this is not a full block write, but we are
823 * extending the file past EOF and this data starts
824 * block-aligned, use assign_arcbuf(). Otherwise,
825 * write via dmu_write().
827 if (tx_bytes < max_blksz && (!write_eof ||
828 aiov->iov_base != abuf->b_data)) {
830 dmu_write(zsb->z_os, zp->z_id, woff,
831 aiov->iov_len, aiov->iov_base, tx);
832 dmu_return_arcbuf(abuf);
833 xuio_stat_wbuf_copied();
835 ASSERT(xuio || tx_bytes == max_blksz);
836 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
839 ASSERT(tx_bytes <= uio->uio_resid);
840 uioskip(uio, tx_bytes);
843 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
844 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
847 * If we made no progress, we're done. If we made even
848 * partial progress, update the znode and ZIL accordingly.
851 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
852 (void *)&zp->z_size, sizeof (uint64_t), tx);
859 * Clear Set-UID/Set-GID bits on successful write if not
860 * privileged and at least one of the excute bits is set.
862 * It would be nice to to this after all writes have
863 * been done, but that would still expose the ISUID/ISGID
864 * to another app after the partial write is committed.
866 * Note: we don't call zfs_fuid_map_id() here because
867 * user 0 is not an ephemeral uid.
869 mutex_enter(&zp->z_acl_lock);
870 uid = KUID_TO_SUID(ip->i_uid);
871 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
872 (S_IXUSR >> 6))) != 0 &&
873 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
874 secpolicy_vnode_setid_retain(cr,
875 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) {
877 zp->z_mode &= ~(S_ISUID | S_ISGID);
878 ip->i_mode = newmode = zp->z_mode;
879 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
880 (void *)&newmode, sizeof (uint64_t), tx);
882 mutex_exit(&zp->z_acl_lock);
884 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
887 * Update the file size (zp_size) if it has changed;
888 * account for possible concurrent updates.
890 while ((end_size = zp->z_size) < uio->uio_loffset) {
891 (void) atomic_cas_64(&zp->z_size, end_size,
896 * If we are replaying and eof is non zero then force
897 * the file size to the specified eof. Note, there's no
898 * concurrency during replay.
900 if (zsb->z_replay && zsb->z_replay_eof != 0)
901 zp->z_size = zsb->z_replay_eof;
903 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
905 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
911 ASSERT(tx_bytes == nbytes);
915 uio_prefaultpages(MIN(n, max_blksz), uio);
918 zfs_inode_update(zp);
919 zfs_range_unlock(rl);
922 * If we're in replay mode, or we made no progress, return error.
923 * Otherwise, it's at least a partial write, so it's successful.
925 if (zsb->z_replay || uio->uio_resid == start_resid) {
930 if (ioflag & (FSYNC | FDSYNC) ||
931 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
932 zil_commit(zilog, zp->z_id);
937 EXPORT_SYMBOL(zfs_write);
940 * Drop a reference on the passed inode asynchronously. This ensures
941 * that the caller will never drop the last reference on an inode in
942 * the current context. Doing so while holding open a tx could result
943 * in a deadlock if iput_final() re-enters the filesystem code.
946 zfs_iput_async(struct inode *ip)
948 objset_t *os = ITOZSB(ip)->z_os;
950 ASSERT(atomic_read(&ip->i_count) > 0);
953 if (atomic_read(&ip->i_count) == 1)
954 VERIFY(taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os)),
955 (task_func_t *)iput, ip, TQ_SLEEP) != TASKQID_INVALID);
961 zfs_get_done(zgd_t *zgd, int error)
963 znode_t *zp = zgd->zgd_private;
966 dmu_buf_rele(zgd->zgd_db, zgd);
968 zfs_range_unlock(zgd->zgd_rl);
971 * Release the vnode asynchronously as we currently have the
972 * txg stopped from syncing.
974 zfs_iput_async(ZTOI(zp));
976 if (error == 0 && zgd->zgd_bp)
977 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
979 kmem_free(zgd, sizeof (zgd_t));
983 static int zil_fault_io = 0;
987 * Get data to generate a TX_WRITE intent log record.
990 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
993 objset_t *os = zsb->z_os;
995 uint64_t object = lr->lr_foid;
996 uint64_t offset = lr->lr_offset;
997 uint64_t size = lr->lr_length;
998 blkptr_t *bp = &lr->lr_blkptr;
1003 ASSERT(zio != NULL);
1007 * Nothing to do if the file has been removed
1009 if (zfs_zget(zsb, object, &zp) != 0)
1010 return (SET_ERROR(ENOENT));
1011 if (zp->z_unlinked) {
1013 * Release the vnode asynchronously as we currently have the
1014 * txg stopped from syncing.
1016 zfs_iput_async(ZTOI(zp));
1017 return (SET_ERROR(ENOENT));
1020 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1021 zgd->zgd_zilog = zsb->z_log;
1022 zgd->zgd_private = zp;
1025 * Write records come in two flavors: immediate and indirect.
1026 * For small writes it's cheaper to store the data with the
1027 * log record (immediate); for large writes it's cheaper to
1028 * sync the data and get a pointer to it (indirect) so that
1029 * we don't have to write the data twice.
1031 if (buf != NULL) { /* immediate write */
1032 zgd->zgd_rl = zfs_range_lock(&zp->z_range_lock, offset, size,
1034 /* test for truncation needs to be done while range locked */
1035 if (offset >= zp->z_size) {
1036 error = SET_ERROR(ENOENT);
1038 error = dmu_read(os, object, offset, size, buf,
1039 DMU_READ_NO_PREFETCH);
1041 ASSERT(error == 0 || error == ENOENT);
1042 } else { /* indirect write */
1044 * Have to lock the whole block to ensure when it's
1045 * written out and it's checksum is being calculated
1046 * that no one can change the data. We need to re-check
1047 * blocksize after we get the lock in case it's changed!
1052 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1054 zgd->zgd_rl = zfs_range_lock(&zp->z_range_lock, offset,
1056 if (zp->z_blksz == size)
1059 zfs_range_unlock(zgd->zgd_rl);
1061 /* test for truncation needs to be done while range locked */
1062 if (lr->lr_offset >= zp->z_size)
1063 error = SET_ERROR(ENOENT);
1066 error = SET_ERROR(EIO);
1071 error = dmu_buf_hold(os, object, offset, zgd, &db,
1072 DMU_READ_NO_PREFETCH);
1075 blkptr_t *obp = dmu_buf_get_blkptr(db);
1077 ASSERT(BP_IS_HOLE(bp));
1084 ASSERT(db->db_offset == offset);
1085 ASSERT(db->db_size == size);
1087 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1089 ASSERT(error || lr->lr_length <= zp->z_blksz);
1092 * On success, we need to wait for the write I/O
1093 * initiated by dmu_sync() to complete before we can
1094 * release this dbuf. We will finish everything up
1095 * in the zfs_get_done() callback.
1100 if (error == EALREADY) {
1101 lr->lr_common.lrc_txtype = TX_WRITE2;
1107 zfs_get_done(zgd, error);
1114 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1116 znode_t *zp = ITOZ(ip);
1117 zfs_sb_t *zsb = ITOZSB(ip);
1123 if (flag & V_ACE_MASK)
1124 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1126 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1131 EXPORT_SYMBOL(zfs_access);
1134 * Lookup an entry in a directory, or an extended attribute directory.
1135 * If it exists, return a held inode reference for it.
1137 * IN: dip - inode of directory to search.
1138 * nm - name of entry to lookup.
1139 * flags - LOOKUP_XATTR set if looking for an attribute.
1140 * cr - credentials of caller.
1141 * direntflags - directory lookup flags
1142 * realpnp - returned pathname.
1144 * OUT: ipp - inode of located entry, NULL if not found.
1146 * RETURN: 0 on success, error code on failure.
1153 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1154 cred_t *cr, int *direntflags, pathname_t *realpnp)
1156 znode_t *zdp = ITOZ(dip);
1157 zfs_sb_t *zsb = ITOZSB(dip);
1161 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1163 if (!S_ISDIR(dip->i_mode)) {
1164 return (SET_ERROR(ENOTDIR));
1165 } else if (zdp->z_sa_hdl == NULL) {
1166 return (SET_ERROR(EIO));
1169 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1170 error = zfs_fastaccesschk_execute(zdp, cr);
1179 vnode_t *tvp = dnlc_lookup(dvp, nm);
1182 error = zfs_fastaccesschk_execute(zdp, cr);
1187 if (tvp == DNLC_NO_VNODE) {
1189 return (SET_ERROR(ENOENT));
1192 return (specvp_check(vpp, cr));
1195 #endif /* HAVE_DNLC */
1204 if (flags & LOOKUP_XATTR) {
1206 * We don't allow recursive attributes..
1207 * Maybe someday we will.
1209 if (zdp->z_pflags & ZFS_XATTR) {
1211 return (SET_ERROR(EINVAL));
1214 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1220 * Do we have permission to get into attribute directory?
1223 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1233 if (!S_ISDIR(dip->i_mode)) {
1235 return (SET_ERROR(ENOTDIR));
1239 * Check accessibility of directory.
1242 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1247 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
1248 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1250 return (SET_ERROR(EILSEQ));
1253 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1254 if ((error == 0) && (*ipp))
1255 zfs_inode_update(ITOZ(*ipp));
1260 EXPORT_SYMBOL(zfs_lookup);
1263 * Attempt to create a new entry in a directory. If the entry
1264 * already exists, truncate the file if permissible, else return
1265 * an error. Return the ip of the created or trunc'd file.
1267 * IN: dip - inode of directory to put new file entry in.
1268 * name - name of new file entry.
1269 * vap - attributes of new file.
1270 * excl - flag indicating exclusive or non-exclusive mode.
1271 * mode - mode to open file with.
1272 * cr - credentials of caller.
1273 * flag - large file flag [UNUSED].
1274 * vsecp - ACL to be set
1276 * OUT: ipp - inode of created or trunc'd entry.
1278 * RETURN: 0 on success, error code on failure.
1281 * dip - ctime|mtime updated if new entry created
1282 * ip - ctime|mtime always, atime if new
1287 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1288 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1290 znode_t *zp, *dzp = ITOZ(dip);
1291 zfs_sb_t *zsb = ITOZSB(dip);
1299 zfs_acl_ids_t acl_ids;
1300 boolean_t fuid_dirtied;
1301 boolean_t have_acl = B_FALSE;
1302 boolean_t waited = B_FALSE;
1305 * If we have an ephemeral id, ACL, or XVATTR then
1306 * make sure file system is at proper version
1312 if (zsb->z_use_fuids == B_FALSE &&
1313 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1314 return (SET_ERROR(EINVAL));
1321 if (zsb->z_utf8 && u8_validate(name, strlen(name),
1322 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1324 return (SET_ERROR(EILSEQ));
1327 if (vap->va_mask & ATTR_XVATTR) {
1328 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1329 crgetuid(cr), cr, vap->va_mode)) != 0) {
1337 if (*name == '\0') {
1339 * Null component name refers to the directory itself.
1346 /* possible igrab(zp) */
1349 if (flag & FIGNORECASE)
1352 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1356 zfs_acl_ids_free(&acl_ids);
1357 if (strcmp(name, "..") == 0)
1358 error = SET_ERROR(EISDIR);
1368 * Create a new file object and update the directory
1371 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1373 zfs_acl_ids_free(&acl_ids);
1378 * We only support the creation of regular files in
1379 * extended attribute directories.
1382 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1384 zfs_acl_ids_free(&acl_ids);
1385 error = SET_ERROR(EINVAL);
1389 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1390 cr, vsecp, &acl_ids)) != 0)
1394 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1395 zfs_acl_ids_free(&acl_ids);
1396 error = SET_ERROR(EDQUOT);
1400 tx = dmu_tx_create(os);
1402 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1403 ZFS_SA_BASE_ATTR_SIZE);
1405 fuid_dirtied = zsb->z_fuid_dirty;
1407 zfs_fuid_txhold(zsb, tx);
1408 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1409 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1410 if (!zsb->z_use_sa &&
1411 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1412 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1413 0, acl_ids.z_aclp->z_acl_bytes);
1415 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1417 zfs_dirent_unlock(dl);
1418 if (error == ERESTART) {
1424 zfs_acl_ids_free(&acl_ids);
1429 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1432 zfs_fuid_sync(zsb, tx);
1434 (void) zfs_link_create(dl, zp, tx, ZNEW);
1435 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1436 if (flag & FIGNORECASE)
1438 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1439 vsecp, acl_ids.z_fuidp, vap);
1440 zfs_acl_ids_free(&acl_ids);
1443 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1446 zfs_acl_ids_free(&acl_ids);
1450 * A directory entry already exists for this name.
1453 * Can't truncate an existing file if in exclusive mode.
1456 error = SET_ERROR(EEXIST);
1460 * Can't open a directory for writing.
1462 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1463 error = SET_ERROR(EISDIR);
1467 * Verify requested access to file.
1469 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1473 mutex_enter(&dzp->z_lock);
1475 mutex_exit(&dzp->z_lock);
1478 * Truncate regular files if requested.
1480 if (S_ISREG(ZTOI(zp)->i_mode) &&
1481 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1482 /* we can't hold any locks when calling zfs_freesp() */
1484 zfs_dirent_unlock(dl);
1487 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1493 zfs_dirent_unlock(dl);
1499 zfs_inode_update(dzp);
1500 zfs_inode_update(zp);
1504 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1505 zil_commit(zilog, 0);
1510 EXPORT_SYMBOL(zfs_create);
1513 * Remove an entry from a directory.
1515 * IN: dip - inode of directory to remove entry from.
1516 * name - name of entry to remove.
1517 * cr - credentials of caller.
1519 * RETURN: 0 if success
1520 * error code if failure
1524 * ip - ctime (if nlink > 0)
1527 uint64_t null_xattr = 0;
1531 zfs_remove(struct inode *dip, char *name, cred_t *cr, int flags)
1533 znode_t *zp, *dzp = ITOZ(dip);
1536 zfs_sb_t *zsb = ITOZSB(dip);
1538 uint64_t acl_obj, xattr_obj;
1539 uint64_t xattr_obj_unlinked = 0;
1544 boolean_t may_delete_now, delete_now = FALSE;
1545 boolean_t unlinked, toobig = FALSE;
1547 pathname_t *realnmp = NULL;
1551 boolean_t waited = B_FALSE;
1557 if (flags & FIGNORECASE) {
1567 * Attempt to lock directory; fail if entry doesn't exist.
1569 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1579 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1584 * Need to use rmdir for removing directories.
1586 if (S_ISDIR(ip->i_mode)) {
1587 error = SET_ERROR(EPERM);
1593 dnlc_remove(dvp, realnmp->pn_buf);
1595 dnlc_remove(dvp, name);
1596 #endif /* HAVE_DNLC */
1598 mutex_enter(&zp->z_lock);
1599 may_delete_now = atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped);
1600 mutex_exit(&zp->z_lock);
1603 * We may delete the znode now, or we may put it in the unlinked set;
1604 * it depends on whether we're the last link, and on whether there are
1605 * other holds on the inode. So we dmu_tx_hold() the right things to
1606 * allow for either case.
1609 tx = dmu_tx_create(zsb->z_os);
1610 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1611 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1612 zfs_sa_upgrade_txholds(tx, zp);
1613 zfs_sa_upgrade_txholds(tx, dzp);
1614 if (may_delete_now) {
1615 toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks;
1616 /* if the file is too big, only hold_free a token amount */
1617 dmu_tx_hold_free(tx, zp->z_id, 0,
1618 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1621 /* are there any extended attributes? */
1622 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1623 &xattr_obj, sizeof (xattr_obj));
1624 if (error == 0 && xattr_obj) {
1625 error = zfs_zget(zsb, xattr_obj, &xzp);
1627 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1628 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1631 mutex_enter(&zp->z_lock);
1632 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1633 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1634 mutex_exit(&zp->z_lock);
1636 /* charge as an update -- would be nice not to charge at all */
1637 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1640 * Mark this transaction as typically resulting in a net free of space
1642 dmu_tx_mark_netfree(tx);
1644 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1646 zfs_dirent_unlock(dl);
1650 if (error == ERESTART) {
1664 * Remove the directory entry.
1666 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1675 * Hold z_lock so that we can make sure that the ACL obj
1676 * hasn't changed. Could have been deleted due to
1679 mutex_enter(&zp->z_lock);
1680 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1681 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1682 delete_now = may_delete_now && !toobig &&
1683 atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped) &&
1684 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1689 if (xattr_obj_unlinked) {
1690 ASSERT3U(ZTOI(xzp)->i_nlink, ==, 2);
1691 mutex_enter(&xzp->z_lock);
1692 xzp->z_unlinked = 1;
1693 clear_nlink(ZTOI(xzp));
1695 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zsb),
1696 &links, sizeof (links), tx);
1697 ASSERT3U(error, ==, 0);
1698 mutex_exit(&xzp->z_lock);
1699 zfs_unlinked_add(xzp, tx);
1702 error = sa_remove(zp->z_sa_hdl,
1703 SA_ZPL_XATTR(zsb), tx);
1705 error = sa_update(zp->z_sa_hdl,
1706 SA_ZPL_XATTR(zsb), &null_xattr,
1707 sizeof (uint64_t), tx);
1711 * Add to the unlinked set because a new reference could be
1712 * taken concurrently resulting in a deferred destruction.
1714 zfs_unlinked_add(zp, tx);
1715 mutex_exit(&zp->z_lock);
1716 zfs_inode_update(zp);
1718 } else if (unlinked) {
1719 mutex_exit(&zp->z_lock);
1720 zfs_unlinked_add(zp, tx);
1724 if (flags & FIGNORECASE)
1726 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1733 zfs_dirent_unlock(dl);
1734 zfs_inode_update(dzp);
1737 zfs_inode_update(zp);
1742 zfs_inode_update(xzp);
1743 zfs_iput_async(ZTOI(xzp));
1746 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1747 zil_commit(zilog, 0);
1752 EXPORT_SYMBOL(zfs_remove);
1755 * Create a new directory and insert it into dip using the name
1756 * provided. Return a pointer to the inserted directory.
1758 * IN: dip - inode of directory to add subdir to.
1759 * dirname - name of new directory.
1760 * vap - attributes of new directory.
1761 * cr - credentials of caller.
1762 * vsecp - ACL to be set
1764 * OUT: ipp - inode of created directory.
1766 * RETURN: 0 if success
1767 * error code if failure
1770 * dip - ctime|mtime updated
1771 * ipp - ctime|mtime|atime updated
1775 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1776 cred_t *cr, int flags, vsecattr_t *vsecp)
1778 znode_t *zp, *dzp = ITOZ(dip);
1779 zfs_sb_t *zsb = ITOZSB(dip);
1787 gid_t gid = crgetgid(cr);
1788 zfs_acl_ids_t acl_ids;
1789 boolean_t fuid_dirtied;
1790 boolean_t waited = B_FALSE;
1792 ASSERT(S_ISDIR(vap->va_mode));
1795 * If we have an ephemeral id, ACL, or XVATTR then
1796 * make sure file system is at proper version
1800 if (zsb->z_use_fuids == B_FALSE &&
1801 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1802 return (SET_ERROR(EINVAL));
1808 if (dzp->z_pflags & ZFS_XATTR) {
1810 return (SET_ERROR(EINVAL));
1813 if (zsb->z_utf8 && u8_validate(dirname,
1814 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1816 return (SET_ERROR(EILSEQ));
1818 if (flags & FIGNORECASE)
1821 if (vap->va_mask & ATTR_XVATTR) {
1822 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1823 crgetuid(cr), cr, vap->va_mode)) != 0) {
1829 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1830 vsecp, &acl_ids)) != 0) {
1835 * First make sure the new directory doesn't exist.
1837 * Existence is checked first to make sure we don't return
1838 * EACCES instead of EEXIST which can cause some applications
1844 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1846 zfs_acl_ids_free(&acl_ids);
1851 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1852 zfs_acl_ids_free(&acl_ids);
1853 zfs_dirent_unlock(dl);
1858 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1859 zfs_acl_ids_free(&acl_ids);
1860 zfs_dirent_unlock(dl);
1862 return (SET_ERROR(EDQUOT));
1866 * Add a new entry to the directory.
1868 tx = dmu_tx_create(zsb->z_os);
1869 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1870 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1871 fuid_dirtied = zsb->z_fuid_dirty;
1873 zfs_fuid_txhold(zsb, tx);
1874 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1875 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1876 acl_ids.z_aclp->z_acl_bytes);
1879 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1880 ZFS_SA_BASE_ATTR_SIZE);
1882 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1884 zfs_dirent_unlock(dl);
1885 if (error == ERESTART) {
1891 zfs_acl_ids_free(&acl_ids);
1900 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1903 zfs_fuid_sync(zsb, tx);
1906 * Now put new name in parent dir.
1908 (void) zfs_link_create(dl, zp, tx, ZNEW);
1912 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1913 if (flags & FIGNORECASE)
1915 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1916 acl_ids.z_fuidp, vap);
1918 zfs_acl_ids_free(&acl_ids);
1922 zfs_dirent_unlock(dl);
1924 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1925 zil_commit(zilog, 0);
1927 zfs_inode_update(dzp);
1928 zfs_inode_update(zp);
1932 EXPORT_SYMBOL(zfs_mkdir);
1935 * Remove a directory subdir entry. If the current working
1936 * directory is the same as the subdir to be removed, the
1939 * IN: dip - inode of directory to remove from.
1940 * name - name of directory to be removed.
1941 * cwd - inode of current working directory.
1942 * cr - credentials of caller.
1943 * flags - case flags
1945 * RETURN: 0 on success, error code on failure.
1948 * dip - ctime|mtime updated
1952 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1955 znode_t *dzp = ITOZ(dip);
1958 zfs_sb_t *zsb = ITOZSB(dip);
1964 boolean_t waited = B_FALSE;
1970 if (flags & FIGNORECASE)
1976 * Attempt to lock directory; fail if entry doesn't exist.
1978 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1986 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1990 if (!S_ISDIR(ip->i_mode)) {
1991 error = SET_ERROR(ENOTDIR);
1996 error = SET_ERROR(EINVAL);
2001 * Grab a lock on the directory to make sure that noone is
2002 * trying to add (or lookup) entries while we are removing it.
2004 rw_enter(&zp->z_name_lock, RW_WRITER);
2007 * Grab a lock on the parent pointer to make sure we play well
2008 * with the treewalk and directory rename code.
2010 rw_enter(&zp->z_parent_lock, RW_WRITER);
2012 tx = dmu_tx_create(zsb->z_os);
2013 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2014 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2015 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
2016 zfs_sa_upgrade_txholds(tx, zp);
2017 zfs_sa_upgrade_txholds(tx, dzp);
2018 dmu_tx_mark_netfree(tx);
2019 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2021 rw_exit(&zp->z_parent_lock);
2022 rw_exit(&zp->z_name_lock);
2023 zfs_dirent_unlock(dl);
2025 if (error == ERESTART) {
2036 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2039 uint64_t txtype = TX_RMDIR;
2040 if (flags & FIGNORECASE)
2042 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2047 rw_exit(&zp->z_parent_lock);
2048 rw_exit(&zp->z_name_lock);
2050 zfs_dirent_unlock(dl);
2052 zfs_inode_update(dzp);
2053 zfs_inode_update(zp);
2056 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
2057 zil_commit(zilog, 0);
2062 EXPORT_SYMBOL(zfs_rmdir);
2065 * Read as many directory entries as will fit into the provided
2066 * dirent buffer from the given directory cursor position.
2068 * IN: ip - inode of directory to read.
2069 * dirent - buffer for directory entries.
2071 * OUT: dirent - filler buffer of directory entries.
2073 * RETURN: 0 if success
2074 * error code if failure
2077 * ip - atime updated
2079 * Note that the low 4 bits of the cookie returned by zap is always zero.
2080 * This allows us to use the low range for "special" directory entries:
2081 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2082 * we use the offset 2 for the '.zfs' directory.
2086 zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
2088 znode_t *zp = ITOZ(ip);
2089 zfs_sb_t *zsb = ITOZSB(ip);
2092 zap_attribute_t zap;
2098 uint64_t offset; /* must be unsigned; checks for < 1 */
2103 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
2104 &parent, sizeof (parent))) != 0)
2108 * Quit if directory has been removed (posix)
2116 prefetch = zp->z_zn_prefetch;
2119 * Initialize the iterator cursor.
2123 * Start iteration from the beginning of the directory.
2125 zap_cursor_init(&zc, os, zp->z_id);
2128 * The offset is a serialized cursor.
2130 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2134 * Transform to file-system independent format
2139 * Special case `.', `..', and `.zfs'.
2142 (void) strcpy(zap.za_name, ".");
2143 zap.za_normalization_conflict = 0;
2146 } else if (offset == 1) {
2147 (void) strcpy(zap.za_name, "..");
2148 zap.za_normalization_conflict = 0;
2151 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2152 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2153 zap.za_normalization_conflict = 0;
2154 objnum = ZFSCTL_INO_ROOT;
2160 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2161 if (error == ENOENT)
2168 * Allow multiple entries provided the first entry is
2169 * the object id. Non-zpl consumers may safely make
2170 * use of the additional space.
2172 * XXX: This should be a feature flag for compatibility
2174 if (zap.za_integer_length != 8 ||
2175 zap.za_num_integers == 0) {
2176 cmn_err(CE_WARN, "zap_readdir: bad directory "
2177 "entry, obj = %lld, offset = %lld, "
2178 "length = %d, num = %lld\n",
2179 (u_longlong_t)zp->z_id,
2180 (u_longlong_t)offset,
2181 zap.za_integer_length,
2182 (u_longlong_t)zap.za_num_integers);
2183 error = SET_ERROR(ENXIO);
2187 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2188 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2191 done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name),
2196 /* Prefetch znode */
2198 dmu_prefetch(os, objnum, 0, 0, 0,
2199 ZIO_PRIORITY_SYNC_READ);
2203 * Move to the next entry, fill in the previous offset.
2205 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2206 zap_cursor_advance(&zc);
2207 offset = zap_cursor_serialize(&zc);
2213 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2216 zap_cursor_fini(&zc);
2217 if (error == ENOENT)
2224 EXPORT_SYMBOL(zfs_readdir);
2226 ulong_t zfs_fsync_sync_cnt = 4;
2229 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2231 znode_t *zp = ITOZ(ip);
2232 zfs_sb_t *zsb = ITOZSB(ip);
2234 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2236 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2239 zil_commit(zsb->z_log, zp->z_id);
2242 tsd_set(zfs_fsyncer_key, NULL);
2246 EXPORT_SYMBOL(zfs_fsync);
2250 * Get the requested file attributes and place them in the provided
2253 * IN: ip - inode of file.
2254 * vap - va_mask identifies requested attributes.
2255 * If ATTR_XVATTR set, then optional attrs are requested
2256 * flags - ATTR_NOACLCHECK (CIFS server context)
2257 * cr - credentials of caller.
2259 * OUT: vap - attribute values.
2261 * RETURN: 0 (always succeeds)
2265 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2267 znode_t *zp = ITOZ(ip);
2268 zfs_sb_t *zsb = ITOZSB(ip);
2271 uint64_t atime[2], mtime[2], ctime[2];
2272 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2273 xoptattr_t *xoap = NULL;
2274 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2275 sa_bulk_attr_t bulk[3];
2281 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2283 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
2284 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2285 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2287 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2293 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2294 * Also, if we are the owner don't bother, since owner should
2295 * always be allowed to read basic attributes of file.
2297 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2298 (vap->va_uid != crgetuid(cr))) {
2299 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2307 * Return all attributes. It's cheaper to provide the answer
2308 * than to determine whether we were asked the question.
2311 mutex_enter(&zp->z_lock);
2312 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2313 vap->va_mode = zp->z_mode;
2314 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2315 vap->va_nodeid = zp->z_id;
2316 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2317 links = ZTOI(zp)->i_nlink + 1;
2319 links = ZTOI(zp)->i_nlink;
2320 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2321 vap->va_size = i_size_read(ip);
2322 vap->va_rdev = ip->i_rdev;
2323 vap->va_seq = ip->i_generation;
2326 * Add in any requested optional attributes and the create time.
2327 * Also set the corresponding bits in the returned attribute bitmap.
2329 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2330 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2332 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2333 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2336 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2337 xoap->xoa_readonly =
2338 ((zp->z_pflags & ZFS_READONLY) != 0);
2339 XVA_SET_RTN(xvap, XAT_READONLY);
2342 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2344 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2345 XVA_SET_RTN(xvap, XAT_SYSTEM);
2348 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2350 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2351 XVA_SET_RTN(xvap, XAT_HIDDEN);
2354 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2355 xoap->xoa_nounlink =
2356 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2357 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2360 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2361 xoap->xoa_immutable =
2362 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2363 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2366 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2367 xoap->xoa_appendonly =
2368 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2369 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2372 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2374 ((zp->z_pflags & ZFS_NODUMP) != 0);
2375 XVA_SET_RTN(xvap, XAT_NODUMP);
2378 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2380 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2381 XVA_SET_RTN(xvap, XAT_OPAQUE);
2384 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2385 xoap->xoa_av_quarantined =
2386 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2387 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2390 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2391 xoap->xoa_av_modified =
2392 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2393 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2396 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2397 S_ISREG(ip->i_mode)) {
2398 zfs_sa_get_scanstamp(zp, xvap);
2401 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2404 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2405 times, sizeof (times));
2406 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2407 XVA_SET_RTN(xvap, XAT_CREATETIME);
2410 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2411 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2412 XVA_SET_RTN(xvap, XAT_REPARSE);
2414 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2415 xoap->xoa_generation = ip->i_generation;
2416 XVA_SET_RTN(xvap, XAT_GEN);
2419 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2421 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2422 XVA_SET_RTN(xvap, XAT_OFFLINE);
2425 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2427 ((zp->z_pflags & ZFS_SPARSE) != 0);
2428 XVA_SET_RTN(xvap, XAT_SPARSE);
2432 ZFS_TIME_DECODE(&vap->va_atime, atime);
2433 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2434 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2436 mutex_exit(&zp->z_lock);
2438 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2440 if (zp->z_blksz == 0) {
2442 * Block size hasn't been set; suggest maximal I/O transfers.
2444 vap->va_blksize = zsb->z_max_blksz;
2450 EXPORT_SYMBOL(zfs_getattr);
2453 * Get the basic file attributes and place them in the provided kstat
2454 * structure. The inode is assumed to be the authoritative source
2455 * for most of the attributes. However, the znode currently has the
2456 * authoritative atime, blksize, and block count.
2458 * IN: ip - inode of file.
2460 * OUT: sp - kstat values.
2462 * RETURN: 0 (always succeeds)
2466 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2468 znode_t *zp = ITOZ(ip);
2469 zfs_sb_t *zsb = ITOZSB(ip);
2471 u_longlong_t nblocks;
2476 mutex_enter(&zp->z_lock);
2478 generic_fillattr(ip, sp);
2480 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2481 sp->blksize = blksize;
2482 sp->blocks = nblocks;
2484 if (unlikely(zp->z_blksz == 0)) {
2486 * Block size hasn't been set; suggest maximal I/O transfers.
2488 sp->blksize = zsb->z_max_blksz;
2491 mutex_exit(&zp->z_lock);
2494 * Required to prevent NFS client from detecting different inode
2495 * numbers of snapshot root dentry before and after snapshot mount.
2497 if (zsb->z_issnap) {
2498 if (ip->i_sb->s_root->d_inode == ip)
2499 sp->ino = ZFSCTL_INO_SNAPDIRS -
2500 dmu_objset_id(zsb->z_os);
2507 EXPORT_SYMBOL(zfs_getattr_fast);
2510 * Set the file attributes to the values contained in the
2513 * IN: ip - inode of file to be modified.
2514 * vap - new attribute values.
2515 * If ATTR_XVATTR set, then optional attrs are being set
2516 * flags - ATTR_UTIME set if non-default time values provided.
2517 * - ATTR_NOACLCHECK (CIFS context only).
2518 * cr - credentials of caller.
2520 * RETURN: 0 if success
2521 * error code if failure
2524 * ip - ctime updated, mtime updated if size changed.
2528 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2530 znode_t *zp = ITOZ(ip);
2531 zfs_sb_t *zsb = ITOZSB(ip);
2535 xvattr_t *tmpxvattr;
2536 uint_t mask = vap->va_mask;
2537 uint_t saved_mask = 0;
2540 uint64_t new_kuid = 0, new_kgid = 0, new_uid, new_gid;
2542 uint64_t mtime[2], ctime[2], atime[2];
2544 int need_policy = FALSE;
2546 zfs_fuid_info_t *fuidp = NULL;
2547 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2550 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2551 boolean_t fuid_dirtied = B_FALSE;
2552 sa_bulk_attr_t *bulk, *xattr_bulk;
2553 int count = 0, xattr_count = 0;
2564 * Make sure that if we have ephemeral uid/gid or xvattr specified
2565 * that file system is at proper version level
2568 if (zsb->z_use_fuids == B_FALSE &&
2569 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2570 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2571 (mask & ATTR_XVATTR))) {
2573 return (SET_ERROR(EINVAL));
2576 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2578 return (SET_ERROR(EISDIR));
2581 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2583 return (SET_ERROR(EINVAL));
2587 * If this is an xvattr_t, then get a pointer to the structure of
2588 * optional attributes. If this is NULL, then we have a vattr_t.
2590 xoap = xva_getxoptattr(xvap);
2592 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP);
2593 xva_init(tmpxvattr);
2595 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2596 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2599 * Immutable files can only alter immutable bit and atime
2601 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2602 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2603 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2608 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2614 * Verify timestamps doesn't overflow 32 bits.
2615 * ZFS can handle large timestamps, but 32bit syscalls can't
2616 * handle times greater than 2039. This check should be removed
2617 * once large timestamps are fully supported.
2619 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2620 if (((mask & ATTR_ATIME) &&
2621 TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2622 ((mask & ATTR_MTIME) &&
2623 TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2633 /* Can this be moved to before the top label? */
2634 if (zfs_is_readonly(zsb)) {
2640 * First validate permissions
2643 if (mask & ATTR_SIZE) {
2644 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2649 * XXX - Note, we are not providing any open
2650 * mode flags here (like FNDELAY), so we may
2651 * block if there are locks present... this
2652 * should be addressed in openat().
2654 /* XXX - would it be OK to generate a log record here? */
2655 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2660 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2661 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2662 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2663 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2664 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2665 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2666 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2667 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2668 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2672 if (mask & (ATTR_UID|ATTR_GID)) {
2673 int idmask = (mask & (ATTR_UID|ATTR_GID));
2678 * NOTE: even if a new mode is being set,
2679 * we may clear S_ISUID/S_ISGID bits.
2682 if (!(mask & ATTR_MODE))
2683 vap->va_mode = zp->z_mode;
2686 * Take ownership or chgrp to group we are a member of
2689 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2690 take_group = (mask & ATTR_GID) &&
2691 zfs_groupmember(zsb, vap->va_gid, cr);
2694 * If both ATTR_UID and ATTR_GID are set then take_owner and
2695 * take_group must both be set in order to allow taking
2698 * Otherwise, send the check through secpolicy_vnode_setattr()
2702 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2703 take_owner && take_group) ||
2704 ((idmask == ATTR_UID) && take_owner) ||
2705 ((idmask == ATTR_GID) && take_group)) {
2706 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2707 skipaclchk, cr) == 0) {
2709 * Remove setuid/setgid for non-privileged users
2711 (void) secpolicy_setid_clear(vap, cr);
2712 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2721 mutex_enter(&zp->z_lock);
2722 oldva.va_mode = zp->z_mode;
2723 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2724 if (mask & ATTR_XVATTR) {
2726 * Update xvattr mask to include only those attributes
2727 * that are actually changing.
2729 * the bits will be restored prior to actually setting
2730 * the attributes so the caller thinks they were set.
2732 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2733 if (xoap->xoa_appendonly !=
2734 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2737 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2738 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2742 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2743 if (xoap->xoa_nounlink !=
2744 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2747 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2748 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2752 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2753 if (xoap->xoa_immutable !=
2754 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2757 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2758 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2762 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2763 if (xoap->xoa_nodump !=
2764 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2767 XVA_CLR_REQ(xvap, XAT_NODUMP);
2768 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2772 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2773 if (xoap->xoa_av_modified !=
2774 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2777 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2778 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2782 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2783 if ((!S_ISREG(ip->i_mode) &&
2784 xoap->xoa_av_quarantined) ||
2785 xoap->xoa_av_quarantined !=
2786 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2789 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2790 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2794 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2795 mutex_exit(&zp->z_lock);
2800 if (need_policy == FALSE &&
2801 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2802 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2807 mutex_exit(&zp->z_lock);
2809 if (mask & ATTR_MODE) {
2810 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2811 err = secpolicy_setid_setsticky_clear(ip, vap,
2816 trim_mask |= ATTR_MODE;
2824 * If trim_mask is set then take ownership
2825 * has been granted or write_acl is present and user
2826 * has the ability to modify mode. In that case remove
2827 * UID|GID and or MODE from mask so that
2828 * secpolicy_vnode_setattr() doesn't revoke it.
2832 saved_mask = vap->va_mask;
2833 vap->va_mask &= ~trim_mask;
2835 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2836 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2841 vap->va_mask |= saved_mask;
2845 * secpolicy_vnode_setattr, or take ownership may have
2848 mask = vap->va_mask;
2850 if ((mask & (ATTR_UID | ATTR_GID))) {
2851 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2852 &xattr_obj, sizeof (xattr_obj));
2854 if (err == 0 && xattr_obj) {
2855 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2859 if (mask & ATTR_UID) {
2860 new_kuid = zfs_fuid_create(zsb,
2861 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2862 if (new_kuid != KUID_TO_SUID(ZTOI(zp)->i_uid) &&
2863 zfs_fuid_overquota(zsb, B_FALSE, new_kuid)) {
2871 if (mask & ATTR_GID) {
2872 new_kgid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2873 cr, ZFS_GROUP, &fuidp);
2874 if (new_kgid != KGID_TO_SGID(ZTOI(zp)->i_gid) &&
2875 zfs_fuid_overquota(zsb, B_TRUE, new_kgid)) {
2883 tx = dmu_tx_create(zsb->z_os);
2885 if (mask & ATTR_MODE) {
2886 uint64_t pmode = zp->z_mode;
2888 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2890 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2892 mutex_enter(&zp->z_lock);
2893 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2895 * Are we upgrading ACL from old V0 format
2898 if (zsb->z_version >= ZPL_VERSION_FUID &&
2899 zfs_znode_acl_version(zp) ==
2900 ZFS_ACL_VERSION_INITIAL) {
2901 dmu_tx_hold_free(tx, acl_obj, 0,
2903 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2904 0, aclp->z_acl_bytes);
2906 dmu_tx_hold_write(tx, acl_obj, 0,
2909 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2910 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2911 0, aclp->z_acl_bytes);
2913 mutex_exit(&zp->z_lock);
2914 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2916 if ((mask & ATTR_XVATTR) &&
2917 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2918 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2920 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2924 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2927 fuid_dirtied = zsb->z_fuid_dirty;
2929 zfs_fuid_txhold(zsb, tx);
2931 zfs_sa_upgrade_txholds(tx, zp);
2933 err = dmu_tx_assign(tx, TXG_WAIT);
2939 * Set each attribute requested.
2940 * We group settings according to the locks they need to acquire.
2942 * Note: you cannot set ctime directly, although it will be
2943 * updated as a side-effect of calling this function.
2947 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2948 mutex_enter(&zp->z_acl_lock);
2949 mutex_enter(&zp->z_lock);
2951 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2952 &zp->z_pflags, sizeof (zp->z_pflags));
2955 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2956 mutex_enter(&attrzp->z_acl_lock);
2957 mutex_enter(&attrzp->z_lock);
2958 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2959 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2960 sizeof (attrzp->z_pflags));
2963 if (mask & (ATTR_UID|ATTR_GID)) {
2965 if (mask & ATTR_UID) {
2966 ZTOI(zp)->i_uid = SUID_TO_KUID(new_kuid);
2967 new_uid = zfs_uid_read(ZTOI(zp));
2968 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2969 &new_uid, sizeof (new_uid));
2971 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2972 SA_ZPL_UID(zsb), NULL, &new_uid,
2974 ZTOI(attrzp)->i_uid = SUID_TO_KUID(new_uid);
2978 if (mask & ATTR_GID) {
2979 ZTOI(zp)->i_gid = SGID_TO_KGID(new_kgid);
2980 new_gid = zfs_gid_read(ZTOI(zp));
2981 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2982 NULL, &new_gid, sizeof (new_gid));
2984 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2985 SA_ZPL_GID(zsb), NULL, &new_gid,
2987 ZTOI(attrzp)->i_gid = SGID_TO_KGID(new_kgid);
2990 if (!(mask & ATTR_MODE)) {
2991 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2992 NULL, &new_mode, sizeof (new_mode));
2993 new_mode = zp->z_mode;
2995 err = zfs_acl_chown_setattr(zp);
2998 err = zfs_acl_chown_setattr(attrzp);
3003 if (mask & ATTR_MODE) {
3004 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
3005 &new_mode, sizeof (new_mode));
3006 zp->z_mode = ZTOI(zp)->i_mode = new_mode;
3007 ASSERT3P(aclp, !=, NULL);
3008 err = zfs_aclset_common(zp, aclp, cr, tx);
3010 if (zp->z_acl_cached)
3011 zfs_acl_free(zp->z_acl_cached);
3012 zp->z_acl_cached = aclp;
3016 if ((mask & ATTR_ATIME) || zp->z_atime_dirty) {
3017 zp->z_atime_dirty = 0;
3018 ZFS_TIME_ENCODE(&ip->i_atime, atime);
3019 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
3020 &atime, sizeof (atime));
3023 if (mask & ATTR_MTIME) {
3024 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3025 ZTOI(zp)->i_mtime = timespec_trunc(vap->va_mtime,
3026 ZTOI(zp)->i_sb->s_time_gran);
3028 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
3029 mtime, sizeof (mtime));
3032 if (mask & ATTR_CTIME) {
3033 ZFS_TIME_ENCODE(&vap->va_ctime, ctime);
3034 ZTOI(zp)->i_ctime = timespec_trunc(vap->va_ctime,
3035 ZTOI(zp)->i_sb->s_time_gran);
3036 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
3037 ctime, sizeof (ctime));
3040 if (attrzp && mask) {
3041 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3042 SA_ZPL_CTIME(zsb), NULL, &ctime,
3047 * Do this after setting timestamps to prevent timestamp
3048 * update from toggling bit
3051 if (xoap && (mask & ATTR_XVATTR)) {
3054 * restore trimmed off masks
3055 * so that return masks can be set for caller.
3058 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
3059 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3061 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
3062 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3064 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
3065 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3067 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
3068 XVA_SET_REQ(xvap, XAT_NODUMP);
3070 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
3071 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3073 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
3074 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3077 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3078 ASSERT(S_ISREG(ip->i_mode));
3080 zfs_xvattr_set(zp, xvap, tx);
3084 zfs_fuid_sync(zsb, tx);
3087 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3089 mutex_exit(&zp->z_lock);
3090 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3091 mutex_exit(&zp->z_acl_lock);
3094 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3095 mutex_exit(&attrzp->z_acl_lock);
3096 mutex_exit(&attrzp->z_lock);
3099 if (err == 0 && attrzp) {
3100 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3111 zfs_fuid_info_free(fuidp);
3117 if (err == ERESTART)
3120 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3122 zfs_inode_update(zp);
3126 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3127 zil_commit(zilog, 0);
3130 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * 7);
3131 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 7);
3132 kmem_free(tmpxvattr, sizeof (xvattr_t));
3136 EXPORT_SYMBOL(zfs_setattr);
3138 typedef struct zfs_zlock {
3139 krwlock_t *zl_rwlock; /* lock we acquired */
3140 znode_t *zl_znode; /* znode we held */
3141 struct zfs_zlock *zl_next; /* next in list */
3145 * Drop locks and release vnodes that were held by zfs_rename_lock().
3148 zfs_rename_unlock(zfs_zlock_t **zlpp)
3152 while ((zl = *zlpp) != NULL) {
3153 if (zl->zl_znode != NULL)
3154 iput(ZTOI(zl->zl_znode));
3155 rw_exit(zl->zl_rwlock);
3156 *zlpp = zl->zl_next;
3157 kmem_free(zl, sizeof (*zl));
3162 * Search back through the directory tree, using the ".." entries.
3163 * Lock each directory in the chain to prevent concurrent renames.
3164 * Fail any attempt to move a directory into one of its own descendants.
3165 * XXX - z_parent_lock can overlap with map or grow locks
3168 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3172 uint64_t rootid = ZTOZSB(zp)->z_root;
3173 uint64_t oidp = zp->z_id;
3174 krwlock_t *rwlp = &szp->z_parent_lock;
3175 krw_t rw = RW_WRITER;
3178 * First pass write-locks szp and compares to zp->z_id.
3179 * Later passes read-lock zp and compare to zp->z_parent.
3182 if (!rw_tryenter(rwlp, rw)) {
3184 * Another thread is renaming in this path.
3185 * Note that if we are a WRITER, we don't have any
3186 * parent_locks held yet.
3188 if (rw == RW_READER && zp->z_id > szp->z_id) {
3190 * Drop our locks and restart
3192 zfs_rename_unlock(&zl);
3196 rwlp = &szp->z_parent_lock;
3201 * Wait for other thread to drop its locks
3207 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3208 zl->zl_rwlock = rwlp;
3209 zl->zl_znode = NULL;
3210 zl->zl_next = *zlpp;
3213 if (oidp == szp->z_id) /* We're a descendant of szp */
3214 return (SET_ERROR(EINVAL));
3216 if (oidp == rootid) /* We've hit the top */
3219 if (rw == RW_READER) { /* i.e. not the first pass */
3220 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3225 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3226 &oidp, sizeof (oidp));
3227 rwlp = &zp->z_parent_lock;
3230 } while (zp->z_id != sdzp->z_id);
3236 * Move an entry from the provided source directory to the target
3237 * directory. Change the entry name as indicated.
3239 * IN: sdip - Source directory containing the "old entry".
3240 * snm - Old entry name.
3241 * tdip - Target directory to contain the "new entry".
3242 * tnm - New entry name.
3243 * cr - credentials of caller.
3244 * flags - case flags
3246 * RETURN: 0 on success, error code on failure.
3249 * sdip,tdip - ctime|mtime updated
3253 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3254 cred_t *cr, int flags)
3256 znode_t *tdzp, *szp, *tzp;
3257 znode_t *sdzp = ITOZ(sdip);
3258 zfs_sb_t *zsb = ITOZSB(sdip);
3260 zfs_dirlock_t *sdl, *tdl;
3263 int cmp, serr, terr;
3266 boolean_t waited = B_FALSE;
3269 ZFS_VERIFY_ZP(sdzp);
3273 ZFS_VERIFY_ZP(tdzp);
3276 * We check i_sb because snapshots and the ctldir must have different
3279 if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) {
3281 return (SET_ERROR(EXDEV));
3284 if (zsb->z_utf8 && u8_validate(tnm,
3285 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3287 return (SET_ERROR(EILSEQ));
3290 if (flags & FIGNORECASE)
3299 * This is to prevent the creation of links into attribute space
3300 * by renaming a linked file into/outof an attribute directory.
3301 * See the comment in zfs_link() for why this is considered bad.
3303 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3305 return (SET_ERROR(EINVAL));
3309 * Lock source and target directory entries. To prevent deadlock,
3310 * a lock ordering must be defined. We lock the directory with
3311 * the smallest object id first, or if it's a tie, the one with
3312 * the lexically first name.
3314 if (sdzp->z_id < tdzp->z_id) {
3316 } else if (sdzp->z_id > tdzp->z_id) {
3320 * First compare the two name arguments without
3321 * considering any case folding.
3323 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3325 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3326 ASSERT(error == 0 || !zsb->z_utf8);
3329 * POSIX: "If the old argument and the new argument
3330 * both refer to links to the same existing file,
3331 * the rename() function shall return successfully
3332 * and perform no other action."
3338 * If the file system is case-folding, then we may
3339 * have some more checking to do. A case-folding file
3340 * system is either supporting mixed case sensitivity
3341 * access or is completely case-insensitive. Note
3342 * that the file system is always case preserving.
3344 * In mixed sensitivity mode case sensitive behavior
3345 * is the default. FIGNORECASE must be used to
3346 * explicitly request case insensitive behavior.
3348 * If the source and target names provided differ only
3349 * by case (e.g., a request to rename 'tim' to 'Tim'),
3350 * we will treat this as a special case in the
3351 * case-insensitive mode: as long as the source name
3352 * is an exact match, we will allow this to proceed as
3353 * a name-change request.
3355 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3356 (zsb->z_case == ZFS_CASE_MIXED &&
3357 flags & FIGNORECASE)) &&
3358 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3361 * case preserving rename request, require exact
3370 * If the source and destination directories are the same, we should
3371 * grab the z_name_lock of that directory only once.
3375 rw_enter(&sdzp->z_name_lock, RW_READER);
3379 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3380 ZEXISTS | zflg, NULL, NULL);
3381 terr = zfs_dirent_lock(&tdl,
3382 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3384 terr = zfs_dirent_lock(&tdl,
3385 tdzp, tnm, &tzp, zflg, NULL, NULL);
3386 serr = zfs_dirent_lock(&sdl,
3387 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3393 * Source entry invalid or not there.
3396 zfs_dirent_unlock(tdl);
3402 rw_exit(&sdzp->z_name_lock);
3404 if (strcmp(snm, "..") == 0)
3410 zfs_dirent_unlock(sdl);
3414 rw_exit(&sdzp->z_name_lock);
3416 if (strcmp(tnm, "..") == 0)
3423 * Must have write access at the source to remove the old entry
3424 * and write access at the target to create the new entry.
3425 * Note that if target and source are the same, this can be
3426 * done in a single check.
3429 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3432 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3434 * Check to make sure rename is valid.
3435 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3437 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3442 * Does target exist?
3446 * Source and target must be the same type.
3448 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3449 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3450 error = SET_ERROR(ENOTDIR);
3454 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3455 error = SET_ERROR(EISDIR);
3460 * POSIX dictates that when the source and target
3461 * entries refer to the same file object, rename
3462 * must do nothing and exit without error.
3464 if (szp->z_id == tzp->z_id) {
3470 tx = dmu_tx_create(zsb->z_os);
3471 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3472 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3473 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3474 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3476 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3477 zfs_sa_upgrade_txholds(tx, tdzp);
3480 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3481 zfs_sa_upgrade_txholds(tx, tzp);
3484 zfs_sa_upgrade_txholds(tx, szp);
3485 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3486 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3489 zfs_rename_unlock(&zl);
3490 zfs_dirent_unlock(sdl);
3491 zfs_dirent_unlock(tdl);
3494 rw_exit(&sdzp->z_name_lock);
3499 if (error == ERESTART) {
3510 if (tzp) /* Attempt to remove the existing target */
3511 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3514 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3516 szp->z_pflags |= ZFS_AV_MODIFIED;
3518 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3519 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3522 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3524 zfs_log_rename(zilog, tx, TX_RENAME |
3525 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3526 sdl->dl_name, tdzp, tdl->dl_name, szp);
3529 * At this point, we have successfully created
3530 * the target name, but have failed to remove
3531 * the source name. Since the create was done
3532 * with the ZRENAMING flag, there are
3533 * complications; for one, the link count is
3534 * wrong. The easiest way to deal with this
3535 * is to remove the newly created target, and
3536 * return the original error. This must
3537 * succeed; fortunately, it is very unlikely to
3538 * fail, since we just created it.
3540 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3541 ZRENAMING, NULL), ==, 0);
3549 zfs_rename_unlock(&zl);
3551 zfs_dirent_unlock(sdl);
3552 zfs_dirent_unlock(tdl);
3554 zfs_inode_update(sdzp);
3556 rw_exit(&sdzp->z_name_lock);
3559 zfs_inode_update(tdzp);
3561 zfs_inode_update(szp);
3564 zfs_inode_update(tzp);
3568 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3569 zil_commit(zilog, 0);
3574 EXPORT_SYMBOL(zfs_rename);
3577 * Insert the indicated symbolic reference entry into the directory.
3579 * IN: dip - Directory to contain new symbolic link.
3580 * link - Name for new symlink entry.
3581 * vap - Attributes of new entry.
3582 * target - Target path of new symlink.
3584 * cr - credentials of caller.
3585 * flags - case flags
3587 * RETURN: 0 on success, error code on failure.
3590 * dip - ctime|mtime updated
3594 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3595 struct inode **ipp, cred_t *cr, int flags)
3597 znode_t *zp, *dzp = ITOZ(dip);
3600 zfs_sb_t *zsb = ITOZSB(dip);
3602 uint64_t len = strlen(link);
3605 zfs_acl_ids_t acl_ids;
3606 boolean_t fuid_dirtied;
3607 uint64_t txtype = TX_SYMLINK;
3608 boolean_t waited = B_FALSE;
3610 ASSERT(S_ISLNK(vap->va_mode));
3616 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3617 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3619 return (SET_ERROR(EILSEQ));
3621 if (flags & FIGNORECASE)
3624 if (len > MAXPATHLEN) {
3626 return (SET_ERROR(ENAMETOOLONG));
3629 if ((error = zfs_acl_ids_create(dzp, 0,
3630 vap, cr, NULL, &acl_ids)) != 0) {
3638 * Attempt to lock directory; fail if entry already exists.
3640 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3642 zfs_acl_ids_free(&acl_ids);
3647 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3648 zfs_acl_ids_free(&acl_ids);
3649 zfs_dirent_unlock(dl);
3654 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3655 zfs_acl_ids_free(&acl_ids);
3656 zfs_dirent_unlock(dl);
3658 return (SET_ERROR(EDQUOT));
3660 tx = dmu_tx_create(zsb->z_os);
3661 fuid_dirtied = zsb->z_fuid_dirty;
3662 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3663 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3664 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3665 ZFS_SA_BASE_ATTR_SIZE + len);
3666 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3667 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3668 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3669 acl_ids.z_aclp->z_acl_bytes);
3672 zfs_fuid_txhold(zsb, tx);
3673 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3675 zfs_dirent_unlock(dl);
3676 if (error == ERESTART) {
3682 zfs_acl_ids_free(&acl_ids);
3689 * Create a new object for the symlink.
3690 * for version 4 ZPL datsets the symlink will be an SA attribute
3692 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3695 zfs_fuid_sync(zsb, tx);
3697 mutex_enter(&zp->z_lock);
3699 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3702 zfs_sa_symlink(zp, link, len, tx);
3703 mutex_exit(&zp->z_lock);
3706 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3707 &zp->z_size, sizeof (zp->z_size), tx);
3709 * Insert the new object into the directory.
3711 (void) zfs_link_create(dl, zp, tx, ZNEW);
3713 if (flags & FIGNORECASE)
3715 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3717 zfs_inode_update(dzp);
3718 zfs_inode_update(zp);
3720 zfs_acl_ids_free(&acl_ids);
3724 zfs_dirent_unlock(dl);
3728 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3729 zil_commit(zilog, 0);
3734 EXPORT_SYMBOL(zfs_symlink);
3737 * Return, in the buffer contained in the provided uio structure,
3738 * the symbolic path referred to by ip.
3740 * IN: ip - inode of symbolic link
3741 * uio - structure to contain the link path.
3742 * cr - credentials of caller.
3744 * RETURN: 0 if success
3745 * error code if failure
3748 * ip - atime updated
3752 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3754 znode_t *zp = ITOZ(ip);
3755 zfs_sb_t *zsb = ITOZSB(ip);
3761 mutex_enter(&zp->z_lock);
3763 error = sa_lookup_uio(zp->z_sa_hdl,
3764 SA_ZPL_SYMLINK(zsb), uio);
3766 error = zfs_sa_readlink(zp, uio);
3767 mutex_exit(&zp->z_lock);
3772 EXPORT_SYMBOL(zfs_readlink);
3775 * Insert a new entry into directory tdip referencing sip.
3777 * IN: tdip - Directory to contain new entry.
3778 * sip - inode of new entry.
3779 * name - name of new entry.
3780 * cr - credentials of caller.
3782 * RETURN: 0 if success
3783 * error code if failure
3786 * tdip - ctime|mtime updated
3787 * sip - ctime updated
3791 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr,
3794 znode_t *dzp = ITOZ(tdip);
3796 zfs_sb_t *zsb = ITOZSB(tdip);
3804 boolean_t waited = B_FALSE;
3806 ASSERT(S_ISDIR(tdip->i_mode));
3813 * POSIX dictates that we return EPERM here.
3814 * Better choices include ENOTSUP or EISDIR.
3816 if (S_ISDIR(sip->i_mode)) {
3818 return (SET_ERROR(EPERM));
3825 * We check i_sb because snapshots and the ctldir must have different
3828 if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) {
3830 return (SET_ERROR(EXDEV));
3833 /* Prevent links to .zfs/shares files */
3835 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3836 &parent, sizeof (uint64_t))) != 0) {
3840 if (parent == zsb->z_shares_dir) {
3842 return (SET_ERROR(EPERM));
3845 if (zsb->z_utf8 && u8_validate(name,
3846 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3848 return (SET_ERROR(EILSEQ));
3850 if (flags & FIGNORECASE)
3854 * We do not support links between attributes and non-attributes
3855 * because of the potential security risk of creating links
3856 * into "normal" file space in order to circumvent restrictions
3857 * imposed in attribute space.
3859 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3861 return (SET_ERROR(EINVAL));
3864 owner = zfs_fuid_map_id(zsb, KUID_TO_SUID(sip->i_uid), cr, ZFS_OWNER);
3865 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3867 return (SET_ERROR(EPERM));
3870 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3877 * Attempt to lock directory; fail if entry already exists.
3879 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3885 tx = dmu_tx_create(zsb->z_os);
3886 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3887 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3888 zfs_sa_upgrade_txholds(tx, szp);
3889 zfs_sa_upgrade_txholds(tx, dzp);
3890 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3892 zfs_dirent_unlock(dl);
3893 if (error == ERESTART) {
3904 error = zfs_link_create(dl, szp, tx, 0);
3907 uint64_t txtype = TX_LINK;
3908 if (flags & FIGNORECASE)
3910 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3915 zfs_dirent_unlock(dl);
3917 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3918 zil_commit(zilog, 0);
3920 zfs_inode_update(dzp);
3921 zfs_inode_update(szp);
3925 EXPORT_SYMBOL(zfs_link);
3928 zfs_putpage_commit_cb(void *arg)
3930 struct page *pp = arg;
3933 end_page_writeback(pp);
3937 * Push a page out to disk, once the page is on stable storage the
3938 * registered commit callback will be run as notification of completion.
3940 * IN: ip - page mapped for inode.
3941 * pp - page to push (page is locked)
3942 * wbc - writeback control data
3944 * RETURN: 0 if success
3945 * error code if failure
3948 * ip - ctime|mtime updated
3952 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
3954 znode_t *zp = ITOZ(ip);
3955 zfs_sb_t *zsb = ITOZSB(ip);
3963 uint64_t mtime[2], ctime[2];
3964 sa_bulk_attr_t bulk[3];
3966 struct address_space *mapping;
3971 ASSERT(PageLocked(pp));
3973 pgoff = page_offset(pp); /* Page byte-offset in file */
3974 offset = i_size_read(ip); /* File length in bytes */
3975 pglen = MIN(PAGE_SIZE, /* Page length in bytes */
3976 P2ROUNDUP(offset, PAGE_SIZE)-pgoff);
3978 /* Page is beyond end of file */
3979 if (pgoff >= offset) {
3985 /* Truncate page length to end of file */
3986 if (pgoff + pglen > offset)
3987 pglen = offset - pgoff;
3991 * FIXME: Allow mmap writes past its quota. The correct fix
3992 * is to register a page_mkwrite() handler to count the page
3993 * against its quota when it is about to be dirtied.
3995 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
3996 zfs_owner_overquota(zsb, zp, B_TRUE)) {
4002 * The ordering here is critical and must adhere to the following
4003 * rules in order to avoid deadlocking in either zfs_read() or
4004 * zfs_free_range() due to a lock inversion.
4006 * 1) The page must be unlocked prior to acquiring the range lock.
4007 * This is critical because zfs_read() calls find_lock_page()
4008 * which may block on the page lock while holding the range lock.
4010 * 2) Before setting or clearing write back on a page the range lock
4011 * must be held in order to prevent a lock inversion with the
4012 * zfs_free_range() function.
4014 * This presents a problem because upon entering this function the
4015 * page lock is already held. To safely acquire the range lock the
4016 * page lock must be dropped. This creates a window where another
4017 * process could truncate, invalidate, dirty, or write out the page.
4019 * Therefore, after successfully reacquiring the range and page locks
4020 * the current page state is checked. In the common case everything
4021 * will be as is expected and it can be written out. However, if
4022 * the page state has changed it must be handled accordingly.
4024 mapping = pp->mapping;
4025 redirty_page_for_writepage(wbc, pp);
4028 rl = zfs_range_lock(&zp->z_range_lock, pgoff, pglen, RL_WRITER);
4031 /* Page mapping changed or it was no longer dirty, we're done */
4032 if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) {
4034 zfs_range_unlock(rl);
4039 /* Another process started write block if required */
4040 if (PageWriteback(pp)) {
4042 zfs_range_unlock(rl);
4044 if (wbc->sync_mode != WB_SYNC_NONE)
4045 wait_on_page_writeback(pp);
4051 /* Clear the dirty flag the required locks are held */
4052 if (!clear_page_dirty_for_io(pp)) {
4054 zfs_range_unlock(rl);
4060 * Counterpart for redirty_page_for_writepage() above. This page
4061 * was in fact not skipped and should not be counted as if it were.
4063 wbc->pages_skipped--;
4064 set_page_writeback(pp);
4067 tx = dmu_tx_create(zsb->z_os);
4068 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
4069 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4070 zfs_sa_upgrade_txholds(tx, zp);
4072 err = dmu_tx_assign(tx, TXG_NOWAIT);
4074 if (err == ERESTART)
4078 __set_page_dirty_nobuffers(pp);
4080 end_page_writeback(pp);
4081 zfs_range_unlock(rl);
4087 ASSERT3U(pglen, <=, PAGE_SIZE);
4088 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
4091 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
4092 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
4093 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
4095 /* Preserve the mtime and ctime provided by the inode */
4096 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4097 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4098 zp->z_atime_dirty = 0;
4101 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4103 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
4104 zfs_putpage_commit_cb, pp);
4107 zfs_range_unlock(rl);
4109 if (wbc->sync_mode != WB_SYNC_NONE) {
4111 * Note that this is rarely called under writepages(), because
4112 * writepages() normally handles the entire commit for
4113 * performance reasons.
4115 if (zsb->z_log != NULL)
4116 zil_commit(zsb->z_log, zp->z_id);
4124 * Update the system attributes when the inode has been dirtied. For the
4125 * moment we only update the mode, atime, mtime, and ctime.
4128 zfs_dirty_inode(struct inode *ip, int flags)
4130 znode_t *zp = ITOZ(ip);
4131 zfs_sb_t *zsb = ITOZSB(ip);
4133 uint64_t mode, atime[2], mtime[2], ctime[2];
4134 sa_bulk_attr_t bulk[4];
4138 if (zfs_is_readonly(zsb) || dmu_objset_is_snapshot(zsb->z_os))
4146 * This is the lazytime semantic indroduced in Linux 4.0
4147 * This flag will only be called from update_time when lazytime is set.
4148 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4149 * Fortunately mtime and ctime are managed within ZFS itself, so we
4150 * only need to dirty atime.
4152 if (flags == I_DIRTY_TIME) {
4153 zp->z_atime_dirty = 1;
4158 tx = dmu_tx_create(zsb->z_os);
4160 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4161 zfs_sa_upgrade_txholds(tx, zp);
4163 error = dmu_tx_assign(tx, TXG_WAIT);
4169 mutex_enter(&zp->z_lock);
4170 zp->z_atime_dirty = 0;
4172 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zsb), NULL, &mode, 8);
4173 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
4174 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
4175 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
4177 /* Preserve the mode, mtime and ctime provided by the inode */
4178 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4179 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4180 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4185 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4186 mutex_exit(&zp->z_lock);
4193 EXPORT_SYMBOL(zfs_dirty_inode);
4197 zfs_inactive(struct inode *ip)
4199 znode_t *zp = ITOZ(ip);
4200 zfs_sb_t *zsb = ITOZSB(ip);
4203 int need_unlock = 0;
4205 /* Only read lock if we haven't already write locked, e.g. rollback */
4206 if (!RW_WRITE_HELD(&zsb->z_teardown_inactive_lock)) {
4208 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
4210 if (zp->z_sa_hdl == NULL) {
4212 rw_exit(&zsb->z_teardown_inactive_lock);
4216 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4217 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
4219 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4220 zfs_sa_upgrade_txholds(tx, zp);
4221 error = dmu_tx_assign(tx, TXG_WAIT);
4225 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4226 mutex_enter(&zp->z_lock);
4227 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
4228 (void *)&atime, sizeof (atime), tx);
4229 zp->z_atime_dirty = 0;
4230 mutex_exit(&zp->z_lock);
4237 rw_exit(&zsb->z_teardown_inactive_lock);
4239 EXPORT_SYMBOL(zfs_inactive);
4242 * Bounds-check the seek operation.
4244 * IN: ip - inode seeking within
4245 * ooff - old file offset
4246 * noffp - pointer to new file offset
4247 * ct - caller context
4249 * RETURN: 0 if success
4250 * EINVAL if new offset invalid
4254 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4256 if (S_ISDIR(ip->i_mode))
4258 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4260 EXPORT_SYMBOL(zfs_seek);
4263 * Fill pages with data from the disk.
4266 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4268 znode_t *zp = ITOZ(ip);
4269 zfs_sb_t *zsb = ITOZSB(ip);
4271 struct page *cur_pp;
4272 u_offset_t io_off, total;
4279 io_len = nr_pages << PAGE_SHIFT;
4280 i_size = i_size_read(ip);
4281 io_off = page_offset(pl[0]);
4283 if (io_off + io_len > i_size)
4284 io_len = i_size - io_off;
4287 * Iterate over list of pages and read each page individually.
4290 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4293 cur_pp = pl[page_idx++];
4295 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4299 /* convert checksum errors into IO errors */
4301 err = SET_ERROR(EIO);
4310 * Uses zfs_fillpage to read data from the file and fill the pages.
4312 * IN: ip - inode of file to get data from.
4313 * pl - list of pages to read
4314 * nr_pages - number of pages to read
4316 * RETURN: 0 on success, error code on failure.
4319 * vp - atime updated
4323 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4325 znode_t *zp = ITOZ(ip);
4326 zfs_sb_t *zsb = ITOZSB(ip);
4335 err = zfs_fillpage(ip, pl, nr_pages);
4340 EXPORT_SYMBOL(zfs_getpage);
4343 * Check ZFS specific permissions to memory map a section of a file.
4345 * IN: ip - inode of the file to mmap
4347 * addrp - start address in memory region
4348 * len - length of memory region
4349 * vm_flags- address flags
4351 * RETURN: 0 if success
4352 * error code if failure
4356 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4357 unsigned long vm_flags)
4359 znode_t *zp = ITOZ(ip);
4360 zfs_sb_t *zsb = ITOZSB(ip);
4365 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4366 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4368 return (SET_ERROR(EPERM));
4371 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4372 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4374 return (SET_ERROR(EACCES));
4377 if (off < 0 || len > MAXOFFSET_T - off) {
4379 return (SET_ERROR(ENXIO));
4385 EXPORT_SYMBOL(zfs_map);
4388 * convoff - converts the given data (start, whence) to the
4392 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4397 if ((lckdat->l_whence == 2) || (whence == 2)) {
4398 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4402 switch (lckdat->l_whence) {
4404 lckdat->l_start += offset;
4407 lckdat->l_start += vap.va_size;
4412 return (SET_ERROR(EINVAL));
4415 if (lckdat->l_start < 0)
4416 return (SET_ERROR(EINVAL));
4420 lckdat->l_start -= offset;
4423 lckdat->l_start -= vap.va_size;
4428 return (SET_ERROR(EINVAL));
4431 lckdat->l_whence = (short)whence;
4436 * Free or allocate space in a file. Currently, this function only
4437 * supports the `F_FREESP' command. However, this command is somewhat
4438 * misnamed, as its functionality includes the ability to allocate as
4439 * well as free space.
4441 * IN: ip - inode of file to free data in.
4442 * cmd - action to take (only F_FREESP supported).
4443 * bfp - section of file to free/alloc.
4444 * flag - current file open mode flags.
4445 * offset - current file offset.
4446 * cr - credentials of caller [UNUSED].
4448 * RETURN: 0 on success, error code on failure.
4451 * ip - ctime|mtime updated
4455 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4456 offset_t offset, cred_t *cr)
4458 znode_t *zp = ITOZ(ip);
4459 zfs_sb_t *zsb = ITOZSB(ip);
4466 if (cmd != F_FREESP) {
4468 return (SET_ERROR(EINVAL));
4472 * Callers might not be able to detect properly that we are read-only,
4473 * so check it explicitly here.
4475 if (zfs_is_readonly(zsb)) {
4477 return (SET_ERROR(EROFS));
4480 if ((error = convoff(ip, bfp, 0, offset))) {
4485 if (bfp->l_len < 0) {
4487 return (SET_ERROR(EINVAL));
4491 * Permissions aren't checked on Solaris because on this OS
4492 * zfs_space() can only be called with an opened file handle.
4493 * On Linux we can get here through truncate_range() which
4494 * operates directly on inodes, so we need to check access rights.
4496 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4502 len = bfp->l_len; /* 0 means from off to end of file */
4504 error = zfs_freesp(zp, off, len, flag, TRUE);
4509 EXPORT_SYMBOL(zfs_space);
4513 zfs_fid(struct inode *ip, fid_t *fidp)
4515 znode_t *zp = ITOZ(ip);
4516 zfs_sb_t *zsb = ITOZSB(ip);
4519 uint64_t object = zp->z_id;
4526 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4527 &gen64, sizeof (uint64_t))) != 0) {
4532 gen = (uint32_t)gen64;
4534 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4535 if (fidp->fid_len < size) {
4536 fidp->fid_len = size;
4538 return (SET_ERROR(ENOSPC));
4541 zfid = (zfid_short_t *)fidp;
4543 zfid->zf_len = size;
4545 for (i = 0; i < sizeof (zfid->zf_object); i++)
4546 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4548 /* Must have a non-zero generation number to distinguish from .zfs */
4551 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4552 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4554 if (size == LONG_FID_LEN) {
4555 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4558 zlfid = (zfid_long_t *)fidp;
4560 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4561 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4563 /* XXX - this should be the generation number for the objset */
4564 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4565 zlfid->zf_setgen[i] = 0;
4571 EXPORT_SYMBOL(zfs_fid);
4575 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4577 znode_t *zp = ITOZ(ip);
4578 zfs_sb_t *zsb = ITOZSB(ip);
4580 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4584 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4589 EXPORT_SYMBOL(zfs_getsecattr);
4593 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4595 znode_t *zp = ITOZ(ip);
4596 zfs_sb_t *zsb = ITOZSB(ip);
4598 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4599 zilog_t *zilog = zsb->z_log;
4604 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4606 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4607 zil_commit(zilog, 0);
4612 EXPORT_SYMBOL(zfs_setsecattr);
4614 #ifdef HAVE_UIO_ZEROCOPY
4616 * Tunable, both must be a power of 2.
4618 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4619 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4620 * an arcbuf for a partial block read
4622 int zcr_blksz_min = (1 << 10); /* 1K */
4623 int zcr_blksz_max = (1 << 17); /* 128K */
4627 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4629 znode_t *zp = ITOZ(ip);
4630 zfs_sb_t *zsb = ITOZSB(ip);
4631 int max_blksz = zsb->z_max_blksz;
4632 uio_t *uio = &xuio->xu_uio;
4633 ssize_t size = uio->uio_resid;
4634 offset_t offset = uio->uio_loffset;
4639 int preamble, postamble;
4641 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4642 return (SET_ERROR(EINVAL));
4649 * Loan out an arc_buf for write if write size is bigger than
4650 * max_blksz, and the file's block size is also max_blksz.
4653 if (size < blksz || zp->z_blksz != blksz) {
4655 return (SET_ERROR(EINVAL));
4658 * Caller requests buffers for write before knowing where the
4659 * write offset might be (e.g. NFS TCP write).
4664 preamble = P2PHASE(offset, blksz);
4666 preamble = blksz - preamble;
4671 postamble = P2PHASE(size, blksz);
4674 fullblk = size / blksz;
4675 (void) dmu_xuio_init(xuio,
4676 (preamble != 0) + fullblk + (postamble != 0));
4679 * Have to fix iov base/len for partial buffers. They
4680 * currently represent full arc_buf's.
4683 /* data begins in the middle of the arc_buf */
4684 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4687 (void) dmu_xuio_add(xuio, abuf,
4688 blksz - preamble, preamble);
4691 for (i = 0; i < fullblk; i++) {
4692 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4695 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4699 /* data ends in the middle of the arc_buf */
4700 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4703 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4708 * Loan out an arc_buf for read if the read size is larger than
4709 * the current file block size. Block alignment is not
4710 * considered. Partial arc_buf will be loaned out for read.
4712 blksz = zp->z_blksz;
4713 if (blksz < zcr_blksz_min)
4714 blksz = zcr_blksz_min;
4715 if (blksz > zcr_blksz_max)
4716 blksz = zcr_blksz_max;
4717 /* avoid potential complexity of dealing with it */
4718 if (blksz > max_blksz) {
4720 return (SET_ERROR(EINVAL));
4723 maxsize = zp->z_size - uio->uio_loffset;
4729 return (SET_ERROR(EINVAL));
4734 return (SET_ERROR(EINVAL));
4737 uio->uio_extflg = UIO_XUIO;
4738 XUIO_XUZC_RW(xuio) = ioflag;
4745 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4749 int ioflag = XUIO_XUZC_RW(xuio);
4751 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4753 i = dmu_xuio_cnt(xuio);
4755 abuf = dmu_xuio_arcbuf(xuio, i);
4757 * if abuf == NULL, it must be a write buffer
4758 * that has been returned in zfs_write().
4761 dmu_return_arcbuf(abuf);
4762 ASSERT(abuf || ioflag == UIO_WRITE);
4765 dmu_xuio_fini(xuio);
4768 #endif /* HAVE_UIO_ZEROCOPY */
4770 #if defined(_KERNEL) && defined(HAVE_SPL)
4771 module_param(zfs_delete_blocks, ulong, 0644);
4772 MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async");
4773 module_param(zfs_read_chunk_size, long, 0644);
4774 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");