4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysmacros.h>
38 #include <sys/resource.h>
40 #include <sys/vfs_opreg.h>
44 #include <sys/taskq.h>
46 #include <sys/vmsystm.h>
47 #include <sys/atomic.h>
49 #include <sys/pathname.h>
50 #include <sys/cmn_err.h>
51 #include <sys/errno.h>
52 #include <sys/unistd.h>
53 #include <sys/zfs_dir.h>
54 #include <sys/zfs_acl.h>
55 #include <sys/zfs_ioctl.h>
56 #include <sys/fs/zfs.h>
58 #include <sys/dmu_objset.h>
64 #include <sys/dirent.h>
65 #include <sys/policy.h>
66 #include <sys/sunddi.h>
69 #include "fs/fs_subr.h"
70 #include <sys/zfs_ctldir.h>
71 #include <sys/zfs_fuid.h>
72 #include <sys/zfs_sa.h>
73 #include <sys/zfs_vnops.h>
75 #include <sys/zfs_rlock.h>
76 #include <sys/extdirent.h>
77 #include <sys/kidmap.h>
86 * Each vnode op performs some logical unit of work. To do this, the ZPL must
87 * properly lock its in-core state, create a DMU transaction, do the work,
88 * record this work in the intent log (ZIL), commit the DMU transaction,
89 * and wait for the intent log to commit if it is a synchronous operation.
90 * Moreover, the vnode ops must work in both normal and log replay context.
91 * The ordering of events is important to avoid deadlocks and references
92 * to freed memory. The example below illustrates the following Big Rules:
94 * (1) A check must be made in each zfs thread for a mounted file system.
95 * This is done avoiding races using ZFS_ENTER(zfsvfs).
96 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
97 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
98 * can return EIO from the calling function.
100 * (2) iput() should always be the last thing except for zil_commit()
101 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
102 * First, if it's the last reference, the vnode/znode
103 * can be freed, so the zp may point to freed memory. Second, the last
104 * reference will call zfs_zinactive(), which may induce a lot of work --
105 * pushing cached pages (which acquires range locks) and syncing out
106 * cached atime changes. Third, zfs_zinactive() may require a new tx,
107 * which could deadlock the system if you were already holding one.
108 * If you must call iput() within a tx then use zfs_iput_async().
110 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
111 * as they can span dmu_tx_assign() calls.
113 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
114 * dmu_tx_assign(). This is critical because we don't want to block
115 * while holding locks.
117 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
118 * reduces lock contention and CPU usage when we must wait (note that if
119 * throughput is constrained by the storage, nearly every transaction
122 * Note, in particular, that if a lock is sometimes acquired before
123 * the tx assigns, and sometimes after (e.g. z_lock), then failing
124 * to use a non-blocking assign can deadlock the system. The scenario:
126 * Thread A has grabbed a lock before calling dmu_tx_assign().
127 * Thread B is in an already-assigned tx, and blocks for this lock.
128 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
129 * forever, because the previous txg can't quiesce until B's tx commits.
131 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
132 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
133 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
134 * to indicate that this operation has already called dmu_tx_wait().
135 * This will ensure that we don't retry forever, waiting a short bit
138 * (5) If the operation succeeded, generate the intent log entry for it
139 * before dropping locks. This ensures that the ordering of events
140 * in the intent log matches the order in which they actually occurred.
141 * During ZIL replay the zfs_log_* functions will update the sequence
142 * number to indicate the zil transaction has replayed.
144 * (6) At the end of each vnode op, the DMU tx must always commit,
145 * regardless of whether there were any errors.
147 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
148 * to ensure that synchronous semantics are provided when necessary.
150 * In general, this is how things should be ordered in each vnode op:
152 * ZFS_ENTER(zfsvfs); // exit if unmounted
154 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
155 * rw_enter(...); // grab any other locks you need
156 * tx = dmu_tx_create(...); // get DMU tx
157 * dmu_tx_hold_*(); // hold each object you might modify
158 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
160 * rw_exit(...); // drop locks
161 * zfs_dirent_unlock(dl); // unlock directory entry
162 * iput(...); // release held vnodes
163 * if (error == ERESTART) {
169 * dmu_tx_abort(tx); // abort DMU tx
170 * ZFS_EXIT(zfsvfs); // finished in zfs
171 * return (error); // really out of space
173 * error = do_real_work(); // do whatever this VOP does
175 * zfs_log_*(...); // on success, make ZIL entry
176 * dmu_tx_commit(tx); // commit DMU tx -- error or not
177 * rw_exit(...); // drop locks
178 * zfs_dirent_unlock(dl); // unlock directory entry
179 * iput(...); // release held vnodes
180 * zil_commit(zilog, foid); // synchronous when necessary
181 * ZFS_EXIT(zfsvfs); // finished in zfs
182 * return (error); // done, report error
186 * Virus scanning is unsupported. It would be possible to add a hook
187 * here to performance the required virus scan. This could be done
188 * entirely in the kernel or potentially as an update to invoke a
192 zfs_vscan(struct inode *ip, cred_t *cr, int async)
199 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
201 znode_t *zp = ITOZ(ip);
202 zfsvfs_t *zfsvfs = ITOZSB(ip);
207 /* Honor ZFS_APPENDONLY file attribute */
208 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
209 ((flag & O_APPEND) == 0)) {
211 return (SET_ERROR(EPERM));
214 /* Virus scan eligible files on open */
215 if (!zfs_has_ctldir(zp) && zfsvfs->z_vscan && S_ISREG(ip->i_mode) &&
216 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
217 if (zfs_vscan(ip, cr, 0) != 0) {
219 return (SET_ERROR(EACCES));
223 /* Keep a count of the synchronous opens in the znode */
225 atomic_inc_32(&zp->z_sync_cnt);
233 zfs_close(struct inode *ip, int flag, cred_t *cr)
235 znode_t *zp = ITOZ(ip);
236 zfsvfs_t *zfsvfs = ITOZSB(ip);
241 /* Decrement the synchronous opens in the znode */
243 atomic_dec_32(&zp->z_sync_cnt);
245 if (!zfs_has_ctldir(zp) && zfsvfs->z_vscan && S_ISREG(ip->i_mode) &&
246 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
247 VERIFY(zfs_vscan(ip, cr, 1) == 0);
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));
282 /* file was dirty, so fall back to using generic logic */
283 if (error == EBUSY) {
291 * We could find a hole that begins after the logical end-of-file,
292 * because dmu_offset_next() only works on whole blocks. If the
293 * EOF falls mid-block, then indicate that the "virtual hole"
294 * at the end of the file begins at the logical EOF, rather than
295 * at the end of the last block.
297 if (noff > file_sz) {
309 zfs_holey(struct inode *ip, int cmd, loff_t *off)
311 znode_t *zp = ITOZ(ip);
312 zfsvfs_t *zfsvfs = ITOZSB(ip);
318 error = zfs_holey_common(ip, cmd, off);
323 #endif /* SEEK_HOLE && SEEK_DATA */
327 * When a file is memory mapped, we must keep the IO data synchronized
328 * between the DMU cache and the memory mapped pages. What this means:
330 * On Write: If we find a memory mapped page, we write to *both*
331 * the page and the dmu buffer.
334 update_pages(struct inode *ip, int64_t start, int len,
335 objset_t *os, uint64_t oid)
337 struct address_space *mp = ip->i_mapping;
343 off = start & (PAGE_SIZE-1);
344 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
345 nbytes = MIN(PAGE_SIZE - off, len);
347 pp = find_lock_page(mp, start >> PAGE_SHIFT);
349 if (mapping_writably_mapped(mp))
350 flush_dcache_page(pp);
353 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
357 if (mapping_writably_mapped(mp))
358 flush_dcache_page(pp);
360 mark_page_accessed(pp);
373 * When a file is memory mapped, we must keep the IO data synchronized
374 * between the DMU cache and the memory mapped pages. What this means:
376 * On Read: We "read" preferentially from memory mapped pages,
377 * else we default from the dmu buffer.
379 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
380 * the file is memory mapped.
383 mappedread(struct inode *ip, int nbytes, uio_t *uio)
385 struct address_space *mp = ip->i_mapping;
387 znode_t *zp = ITOZ(ip);
394 start = uio->uio_loffset;
395 off = start & (PAGE_SIZE-1);
396 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
397 bytes = MIN(PAGE_SIZE - off, len);
399 pp = find_lock_page(mp, start >> PAGE_SHIFT);
401 ASSERT(PageUptodate(pp));
404 error = uiomove(pb + off, bytes, UIO_READ, uio);
407 if (mapping_writably_mapped(mp))
408 flush_dcache_page(pp);
410 mark_page_accessed(pp);
414 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
427 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
428 unsigned long zfs_delete_blocks = DMU_MAX_DELETEBLKCNT;
431 * Read bytes from specified file into supplied buffer.
433 * IN: ip - inode of file to be read from.
434 * uio - structure supplying read location, range info,
436 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
437 * O_DIRECT flag; used to bypass page cache.
438 * cr - credentials of caller.
440 * OUT: uio - updated offset and range, buffer filled.
442 * RETURN: 0 on success, error code on failure.
445 * inode - atime updated if byte count > 0
449 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
451 znode_t *zp = ITOZ(ip);
452 zfsvfs_t *zfsvfs = ITOZSB(ip);
456 #ifdef HAVE_UIO_ZEROCOPY
458 #endif /* HAVE_UIO_ZEROCOPY */
463 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
465 return (SET_ERROR(EACCES));
469 * Validate file offset
471 if (uio->uio_loffset < (offset_t)0) {
473 return (SET_ERROR(EINVAL));
477 * Fasttrack empty reads
479 if (uio->uio_resid == 0) {
485 * If we're in FRSYNC mode, sync out this znode before reading it.
486 * Only do this for non-snapshots.
489 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
490 zil_commit(zfsvfs->z_log, zp->z_id);
493 * Lock the range against changes.
495 rl = zfs_range_lock(&zp->z_range_lock, uio->uio_loffset, uio->uio_resid,
499 * If we are reading past end-of-file we can skip
500 * to the end; but we might still need to set atime.
502 if (uio->uio_loffset >= zp->z_size) {
507 ASSERT(uio->uio_loffset < zp->z_size);
508 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
510 #ifdef HAVE_UIO_ZEROCOPY
511 if ((uio->uio_extflg == UIO_XUIO) &&
512 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
514 int blksz = zp->z_blksz;
515 uint64_t offset = uio->uio_loffset;
517 xuio = (xuio_t *)uio;
519 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
522 ASSERT(offset + n <= blksz);
525 (void) dmu_xuio_init(xuio, nblk);
527 if (vn_has_cached_data(ip)) {
529 * For simplicity, we always allocate a full buffer
530 * even if we only expect to read a portion of a block.
532 while (--nblk >= 0) {
533 (void) dmu_xuio_add(xuio,
534 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
539 #endif /* HAVE_UIO_ZEROCOPY */
542 nbytes = MIN(n, zfs_read_chunk_size -
543 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
545 if (zp->z_is_mapped && !(ioflag & O_DIRECT)) {
546 error = mappedread(ip, nbytes, uio);
548 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
553 /* convert checksum errors into IO errors */
555 error = SET_ERROR(EIO);
562 zfs_range_unlock(rl);
569 * Write the bytes to a file.
571 * IN: ip - inode of file to be written to.
572 * uio - structure supplying write location, range info,
574 * ioflag - FAPPEND flag set if in append mode.
575 * O_DIRECT flag; used to bypass page cache.
576 * cr - credentials of caller.
578 * OUT: uio - updated offset and range.
580 * RETURN: 0 if success
581 * error code if failure
584 * ip - ctime|mtime updated if byte count > 0
589 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
591 znode_t *zp = ITOZ(ip);
592 rlim64_t limit = uio->uio_limit;
593 ssize_t start_resid = uio->uio_resid;
597 zfsvfs_t *zfsvfs = ZTOZSB(zp);
602 int max_blksz = zfsvfs->z_max_blksz;
605 const iovec_t *aiov = NULL;
609 sa_bulk_attr_t bulk[4];
610 uint64_t mtime[2], ctime[2];
612 #ifdef HAVE_UIO_ZEROCOPY
614 const iovec_t *iovp = uio->uio_iov;
615 ASSERTV(int iovcnt = uio->uio_iovcnt);
619 * Fasttrack empty write
625 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
631 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
632 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
633 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
635 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
639 * Callers might not be able to detect properly that we are read-only,
640 * so check it explicitly here.
642 if (zfs_is_readonly(zfsvfs)) {
644 return (SET_ERROR(EROFS));
648 * If immutable or not appending then return EPERM
650 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
651 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
652 (uio->uio_loffset < zp->z_size))) {
654 return (SET_ERROR(EPERM));
657 zilog = zfsvfs->z_log;
660 * Validate file offset
662 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
665 return (SET_ERROR(EINVAL));
669 * Pre-fault the pages to ensure slow (eg NFS) pages
671 * Skip this if uio contains loaned arc_buf.
673 #ifdef HAVE_UIO_ZEROCOPY
674 if ((uio->uio_extflg == UIO_XUIO) &&
675 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
676 xuio = (xuio_t *)uio;
679 uio_prefaultpages(MIN(n, max_blksz), uio);
682 * If in append mode, set the io offset pointer to eof.
684 if (ioflag & FAPPEND) {
686 * Obtain an appending range lock to guarantee file append
687 * semantics. We reset the write offset once we have the lock.
689 rl = zfs_range_lock(&zp->z_range_lock, 0, n, RL_APPEND);
691 if (rl->r_len == UINT64_MAX) {
693 * We overlocked the file because this write will cause
694 * the file block size to increase.
695 * Note that zp_size cannot change with this lock held.
699 uio->uio_loffset = woff;
702 * Note that if the file block size will change as a result of
703 * this write, then this range lock will lock the entire file
704 * so that we can re-write the block safely.
706 rl = zfs_range_lock(&zp->z_range_lock, woff, n, RL_WRITER);
710 zfs_range_unlock(rl);
712 return (SET_ERROR(EFBIG));
715 if ((woff + n) > limit || woff > (limit - n))
718 /* Will this write extend the file length? */
719 write_eof = (woff + n > zp->z_size);
721 end_size = MAX(zp->z_size, woff + n);
724 * Write the file in reasonable size chunks. Each chunk is written
725 * in a separate transaction; this keeps the intent log records small
726 * and allows us to do more fine-grained space accounting.
730 woff = uio->uio_loffset;
731 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
732 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
734 dmu_return_arcbuf(abuf);
735 error = SET_ERROR(EDQUOT);
739 if (xuio && abuf == NULL) {
740 #ifdef HAVE_UIO_ZEROCOPY
741 ASSERT(i_iov < iovcnt);
742 ASSERT3U(uio->uio_segflg, !=, UIO_BVEC);
744 abuf = dmu_xuio_arcbuf(xuio, i_iov);
745 dmu_xuio_clear(xuio, i_iov);
746 ASSERT((aiov->iov_base == abuf->b_data) ||
747 ((char *)aiov->iov_base - (char *)abuf->b_data +
748 aiov->iov_len == arc_buf_size(abuf)));
751 } else if (abuf == NULL && n >= max_blksz &&
752 woff >= zp->z_size &&
753 P2PHASE(woff, max_blksz) == 0 &&
754 zp->z_blksz == max_blksz) {
756 * This write covers a full block. "Borrow" a buffer
757 * from the dmu so that we can fill it before we enter
758 * a transaction. This avoids the possibility of
759 * holding up the transaction if the data copy hangs
760 * up on a pagefault (e.g., from an NFS server mapping).
764 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
766 ASSERT(abuf != NULL);
767 ASSERT(arc_buf_size(abuf) == max_blksz);
768 if ((error = uiocopy(abuf->b_data, max_blksz,
769 UIO_WRITE, uio, &cbytes))) {
770 dmu_return_arcbuf(abuf);
773 ASSERT(cbytes == max_blksz);
777 * Start a transaction.
779 tx = dmu_tx_create(zfsvfs->z_os);
780 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
781 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
782 zfs_sa_upgrade_txholds(tx, zp);
783 error = dmu_tx_assign(tx, TXG_WAIT);
787 dmu_return_arcbuf(abuf);
792 * If zfs_range_lock() over-locked we grow the blocksize
793 * and then reduce the lock range. This will only happen
794 * on the first iteration since zfs_range_reduce() will
795 * shrink down r_len to the appropriate size.
797 if (rl->r_len == UINT64_MAX) {
800 if (zp->z_blksz > max_blksz) {
802 * File's blocksize is already larger than the
803 * "recordsize" property. Only let it grow to
804 * the next power of 2.
806 ASSERT(!ISP2(zp->z_blksz));
807 new_blksz = MIN(end_size,
808 1 << highbit64(zp->z_blksz));
810 new_blksz = MIN(end_size, max_blksz);
812 zfs_grow_blocksize(zp, new_blksz, tx);
813 zfs_range_reduce(rl, woff, n);
817 * XXX - should we really limit each write to z_max_blksz?
818 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
820 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
823 tx_bytes = uio->uio_resid;
824 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
826 tx_bytes -= uio->uio_resid;
829 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
831 * If this is not a full block write, but we are
832 * extending the file past EOF and this data starts
833 * block-aligned, use assign_arcbuf(). Otherwise,
834 * write via dmu_write().
836 if (tx_bytes < max_blksz && (!write_eof ||
837 aiov->iov_base != abuf->b_data)) {
839 dmu_write(zfsvfs->z_os, zp->z_id, woff,
840 /* cppcheck-suppress nullPointer */
841 aiov->iov_len, aiov->iov_base, tx);
842 dmu_return_arcbuf(abuf);
843 xuio_stat_wbuf_copied();
845 ASSERT(xuio || tx_bytes == max_blksz);
846 dmu_assign_arcbuf_by_dbuf(
847 sa_get_db(zp->z_sa_hdl), woff, abuf, tx);
849 ASSERT(tx_bytes <= uio->uio_resid);
850 uioskip(uio, tx_bytes);
852 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT)) {
853 update_pages(ip, woff,
854 tx_bytes, zfsvfs->z_os, zp->z_id);
858 * If we made no progress, we're done. If we made even
859 * partial progress, update the znode and ZIL accordingly.
862 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
863 (void *)&zp->z_size, sizeof (uint64_t), tx);
870 * Clear Set-UID/Set-GID bits on successful write if not
871 * privileged and at least one of the execute bits is set.
873 * It would be nice to to this after all writes have
874 * been done, but that would still expose the ISUID/ISGID
875 * to another app after the partial write is committed.
877 * Note: we don't call zfs_fuid_map_id() here because
878 * user 0 is not an ephemeral uid.
880 mutex_enter(&zp->z_acl_lock);
881 uid = KUID_TO_SUID(ip->i_uid);
882 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
883 (S_IXUSR >> 6))) != 0 &&
884 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
885 secpolicy_vnode_setid_retain(cr,
886 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) {
888 zp->z_mode &= ~(S_ISUID | S_ISGID);
889 ip->i_mode = newmode = zp->z_mode;
890 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
891 (void *)&newmode, sizeof (uint64_t), tx);
893 mutex_exit(&zp->z_acl_lock);
895 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
898 * Update the file size (zp_size) if it has changed;
899 * account for possible concurrent updates.
901 while ((end_size = zp->z_size) < uio->uio_loffset) {
902 (void) atomic_cas_64(&zp->z_size, end_size,
907 * If we are replaying and eof is non zero then force
908 * the file size to the specified eof. Note, there's no
909 * concurrency during replay.
911 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
912 zp->z_size = zfsvfs->z_replay_eof;
914 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
916 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
922 ASSERT(tx_bytes == nbytes);
926 uio_prefaultpages(MIN(n, max_blksz), uio);
929 zfs_inode_update(zp);
930 zfs_range_unlock(rl);
933 * If we're in replay mode, or we made no progress, return error.
934 * Otherwise, it's at least a partial write, so it's successful.
936 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
941 if (ioflag & (FSYNC | FDSYNC) ||
942 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
943 zil_commit(zilog, zp->z_id);
950 * Drop a reference on the passed inode asynchronously. This ensures
951 * that the caller will never drop the last reference on an inode in
952 * the current context. Doing so while holding open a tx could result
953 * in a deadlock if iput_final() re-enters the filesystem code.
956 zfs_iput_async(struct inode *ip)
958 objset_t *os = ITOZSB(ip)->z_os;
960 ASSERT(atomic_read(&ip->i_count) > 0);
963 if (atomic_read(&ip->i_count) == 1)
964 VERIFY(taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os)),
965 (task_func_t *)iput, ip, TQ_SLEEP) != TASKQID_INVALID);
971 zfs_get_done(zgd_t *zgd, int error)
973 znode_t *zp = zgd->zgd_private;
976 dmu_buf_rele(zgd->zgd_db, zgd);
978 zfs_range_unlock(zgd->zgd_rl);
981 * Release the vnode asynchronously as we currently have the
982 * txg stopped from syncing.
984 zfs_iput_async(ZTOI(zp));
986 if (error == 0 && zgd->zgd_bp)
987 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
989 kmem_free(zgd, sizeof (zgd_t));
993 static int zil_fault_io = 0;
997 * Get data to generate a TX_WRITE intent log record.
1000 zfs_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1002 zfsvfs_t *zfsvfs = arg;
1003 objset_t *os = zfsvfs->z_os;
1005 uint64_t object = lr->lr_foid;
1006 uint64_t offset = lr->lr_offset;
1007 uint64_t size = lr->lr_length;
1012 ASSERT3P(lwb, !=, NULL);
1013 ASSERT3P(zio, !=, NULL);
1014 ASSERT3U(size, !=, 0);
1017 * Nothing to do if the file has been removed
1019 if (zfs_zget(zfsvfs, object, &zp) != 0)
1020 return (SET_ERROR(ENOENT));
1021 if (zp->z_unlinked) {
1023 * Release the vnode asynchronously as we currently have the
1024 * txg stopped from syncing.
1026 zfs_iput_async(ZTOI(zp));
1027 return (SET_ERROR(ENOENT));
1030 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1032 zgd->zgd_private = zp;
1035 * Write records come in two flavors: immediate and indirect.
1036 * For small writes it's cheaper to store the data with the
1037 * log record (immediate); for large writes it's cheaper to
1038 * sync the data and get a pointer to it (indirect) so that
1039 * we don't have to write the data twice.
1041 if (buf != NULL) { /* immediate write */
1042 zgd->zgd_rl = zfs_range_lock(&zp->z_range_lock, offset, size,
1044 /* test for truncation needs to be done while range locked */
1045 if (offset >= zp->z_size) {
1046 error = SET_ERROR(ENOENT);
1048 error = dmu_read(os, object, offset, size, buf,
1049 DMU_READ_NO_PREFETCH);
1051 ASSERT(error == 0 || error == ENOENT);
1052 } else { /* indirect write */
1054 * Have to lock the whole block to ensure when it's
1055 * written out and its checksum is being calculated
1056 * that no one can change the data. We need to re-check
1057 * blocksize after we get the lock in case it's changed!
1062 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1064 zgd->zgd_rl = zfs_range_lock(&zp->z_range_lock, offset,
1066 if (zp->z_blksz == size)
1069 zfs_range_unlock(zgd->zgd_rl);
1071 /* test for truncation needs to be done while range locked */
1072 if (lr->lr_offset >= zp->z_size)
1073 error = SET_ERROR(ENOENT);
1076 error = SET_ERROR(EIO);
1081 error = dmu_buf_hold(os, object, offset, zgd, &db,
1082 DMU_READ_NO_PREFETCH);
1085 blkptr_t *bp = &lr->lr_blkptr;
1090 ASSERT(db->db_offset == offset);
1091 ASSERT(db->db_size == size);
1093 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1095 ASSERT(error || lr->lr_length <= size);
1098 * On success, we need to wait for the write I/O
1099 * initiated by dmu_sync() to complete before we can
1100 * release this dbuf. We will finish everything up
1101 * in the zfs_get_done() callback.
1106 if (error == EALREADY) {
1107 lr->lr_common.lrc_txtype = TX_WRITE2;
1113 zfs_get_done(zgd, error);
1120 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1122 znode_t *zp = ITOZ(ip);
1123 zfsvfs_t *zfsvfs = ITOZSB(ip);
1129 if (flag & V_ACE_MASK)
1130 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1132 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1139 * Lookup an entry in a directory, or an extended attribute directory.
1140 * If it exists, return a held inode reference for it.
1142 * IN: dip - inode of directory to search.
1143 * nm - name of entry to lookup.
1144 * flags - LOOKUP_XATTR set if looking for an attribute.
1145 * cr - credentials of caller.
1146 * direntflags - directory lookup flags
1147 * realpnp - returned pathname.
1149 * OUT: ipp - inode of located entry, NULL if not found.
1151 * RETURN: 0 on success, error code on failure.
1158 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1159 cred_t *cr, int *direntflags, pathname_t *realpnp)
1161 znode_t *zdp = ITOZ(dip);
1162 zfsvfs_t *zfsvfs = ITOZSB(dip);
1166 * Fast path lookup, however we must skip DNLC lookup
1167 * for case folding or normalizing lookups because the
1168 * DNLC code only stores the passed in name. This means
1169 * creating 'a' and removing 'A' on a case insensitive
1170 * file system would work, but DNLC still thinks 'a'
1171 * exists and won't let you create it again on the next
1172 * pass through fast path.
1174 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1176 if (!S_ISDIR(dip->i_mode)) {
1177 return (SET_ERROR(ENOTDIR));
1178 } else if (zdp->z_sa_hdl == NULL) {
1179 return (SET_ERROR(EIO));
1182 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1183 error = zfs_fastaccesschk_execute(zdp, cr);
1191 } else if (!zdp->z_zfsvfs->z_norm &&
1192 (zdp->z_zfsvfs->z_case == ZFS_CASE_SENSITIVE)) {
1194 vnode_t *tvp = dnlc_lookup(dvp, nm);
1197 error = zfs_fastaccesschk_execute(zdp, cr);
1202 if (tvp == DNLC_NO_VNODE) {
1204 return (SET_ERROR(ENOENT));
1207 return (specvp_check(vpp, cr));
1210 #endif /* HAVE_DNLC */
1219 if (flags & LOOKUP_XATTR) {
1221 * We don't allow recursive attributes..
1222 * Maybe someday we will.
1224 if (zdp->z_pflags & ZFS_XATTR) {
1226 return (SET_ERROR(EINVAL));
1229 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1235 * Do we have permission to get into attribute directory?
1238 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1248 if (!S_ISDIR(dip->i_mode)) {
1250 return (SET_ERROR(ENOTDIR));
1254 * Check accessibility of directory.
1257 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1262 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1263 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1265 return (SET_ERROR(EILSEQ));
1268 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1269 if ((error == 0) && (*ipp))
1270 zfs_inode_update(ITOZ(*ipp));
1277 * Attempt to create a new entry in a directory. If the entry
1278 * already exists, truncate the file if permissible, else return
1279 * an error. Return the ip of the created or trunc'd file.
1281 * IN: dip - inode of directory to put new file entry in.
1282 * name - name of new file entry.
1283 * vap - attributes of new file.
1284 * excl - flag indicating exclusive or non-exclusive mode.
1285 * mode - mode to open file with.
1286 * cr - credentials of caller.
1287 * flag - large file flag [UNUSED].
1288 * vsecp - ACL to be set
1290 * OUT: ipp - inode of created or trunc'd entry.
1292 * RETURN: 0 on success, error code on failure.
1295 * dip - ctime|mtime updated if new entry created
1296 * ip - ctime|mtime always, atime if new
1301 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1302 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1304 znode_t *zp, *dzp = ITOZ(dip);
1305 zfsvfs_t *zfsvfs = ITOZSB(dip);
1313 zfs_acl_ids_t acl_ids;
1314 boolean_t fuid_dirtied;
1315 boolean_t have_acl = B_FALSE;
1316 boolean_t waited = B_FALSE;
1319 * If we have an ephemeral id, ACL, or XVATTR then
1320 * make sure file system is at proper version
1326 if (zfsvfs->z_use_fuids == B_FALSE &&
1327 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1328 return (SET_ERROR(EINVAL));
1331 return (SET_ERROR(EINVAL));
1336 zilog = zfsvfs->z_log;
1338 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1339 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1341 return (SET_ERROR(EILSEQ));
1344 if (vap->va_mask & ATTR_XVATTR) {
1345 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1346 crgetuid(cr), cr, vap->va_mode)) != 0) {
1354 if (*name == '\0') {
1356 * Null component name refers to the directory itself.
1363 /* possible igrab(zp) */
1366 if (flag & FIGNORECASE)
1369 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1373 zfs_acl_ids_free(&acl_ids);
1374 if (strcmp(name, "..") == 0)
1375 error = SET_ERROR(EISDIR);
1385 * Create a new file object and update the directory
1388 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1390 zfs_acl_ids_free(&acl_ids);
1395 * We only support the creation of regular files in
1396 * extended attribute directories.
1399 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1401 zfs_acl_ids_free(&acl_ids);
1402 error = SET_ERROR(EINVAL);
1406 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1407 cr, vsecp, &acl_ids)) != 0)
1411 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1412 zfs_acl_ids_free(&acl_ids);
1413 error = SET_ERROR(EDQUOT);
1417 tx = dmu_tx_create(os);
1419 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1420 ZFS_SA_BASE_ATTR_SIZE);
1422 fuid_dirtied = zfsvfs->z_fuid_dirty;
1424 zfs_fuid_txhold(zfsvfs, tx);
1425 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1426 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1427 if (!zfsvfs->z_use_sa &&
1428 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1429 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1430 0, acl_ids.z_aclp->z_acl_bytes);
1432 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1434 zfs_dirent_unlock(dl);
1435 if (error == ERESTART) {
1441 zfs_acl_ids_free(&acl_ids);
1446 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1449 zfs_fuid_sync(zfsvfs, tx);
1451 (void) zfs_link_create(dl, zp, tx, ZNEW);
1452 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1453 if (flag & FIGNORECASE)
1455 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1456 vsecp, acl_ids.z_fuidp, vap);
1457 zfs_acl_ids_free(&acl_ids);
1460 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1463 zfs_acl_ids_free(&acl_ids);
1467 * A directory entry already exists for this name.
1470 * Can't truncate an existing file if in exclusive mode.
1473 error = SET_ERROR(EEXIST);
1477 * Can't open a directory for writing.
1479 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1480 error = SET_ERROR(EISDIR);
1484 * Verify requested access to file.
1486 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1490 mutex_enter(&dzp->z_lock);
1492 mutex_exit(&dzp->z_lock);
1495 * Truncate regular files if requested.
1497 if (S_ISREG(ZTOI(zp)->i_mode) &&
1498 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1499 /* we can't hold any locks when calling zfs_freesp() */
1501 zfs_dirent_unlock(dl);
1504 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1510 zfs_dirent_unlock(dl);
1516 zfs_inode_update(dzp);
1517 zfs_inode_update(zp);
1521 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1522 zil_commit(zilog, 0);
1530 zfs_tmpfile(struct inode *dip, vattr_t *vap, int excl,
1531 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1533 znode_t *zp = NULL, *dzp = ITOZ(dip);
1534 zfsvfs_t *zfsvfs = ITOZSB(dip);
1540 zfs_acl_ids_t acl_ids;
1541 boolean_t fuid_dirtied;
1542 boolean_t have_acl = B_FALSE;
1543 boolean_t waited = B_FALSE;
1546 * If we have an ephemeral id, ACL, or XVATTR then
1547 * make sure file system is at proper version
1553 if (zfsvfs->z_use_fuids == B_FALSE &&
1554 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1555 return (SET_ERROR(EINVAL));
1561 if (vap->va_mask & ATTR_XVATTR) {
1562 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1563 crgetuid(cr), cr, vap->va_mode)) != 0) {
1573 * Create a new file object and update the directory
1576 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1578 zfs_acl_ids_free(&acl_ids);
1582 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1583 cr, vsecp, &acl_ids)) != 0)
1587 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1588 zfs_acl_ids_free(&acl_ids);
1589 error = SET_ERROR(EDQUOT);
1593 tx = dmu_tx_create(os);
1595 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1596 ZFS_SA_BASE_ATTR_SIZE);
1597 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1599 fuid_dirtied = zfsvfs->z_fuid_dirty;
1601 zfs_fuid_txhold(zfsvfs, tx);
1602 if (!zfsvfs->z_use_sa &&
1603 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1604 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1605 0, acl_ids.z_aclp->z_acl_bytes);
1607 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1609 if (error == ERESTART) {
1615 zfs_acl_ids_free(&acl_ids);
1620 zfs_mknode(dzp, vap, tx, cr, IS_TMPFILE, &zp, &acl_ids);
1623 zfs_fuid_sync(zfsvfs, tx);
1625 /* Add to unlinked set */
1627 zfs_unlinked_add(zp, tx);
1628 zfs_acl_ids_free(&acl_ids);
1636 zfs_inode_update(dzp);
1637 zfs_inode_update(zp);
1646 * Remove an entry from a directory.
1648 * IN: dip - inode of directory to remove entry from.
1649 * name - name of entry to remove.
1650 * cr - credentials of caller.
1652 * RETURN: 0 if success
1653 * error code if failure
1657 * ip - ctime (if nlink > 0)
1660 uint64_t null_xattr = 0;
1664 zfs_remove(struct inode *dip, char *name, cred_t *cr, int flags)
1666 znode_t *zp, *dzp = ITOZ(dip);
1669 zfsvfs_t *zfsvfs = ITOZSB(dip);
1671 uint64_t acl_obj, xattr_obj;
1672 uint64_t xattr_obj_unlinked = 0;
1677 boolean_t may_delete_now, delete_now = FALSE;
1678 boolean_t unlinked, toobig = FALSE;
1680 pathname_t *realnmp = NULL;
1684 boolean_t waited = B_FALSE;
1687 return (SET_ERROR(EINVAL));
1691 zilog = zfsvfs->z_log;
1693 if (flags & FIGNORECASE) {
1703 * Attempt to lock directory; fail if entry doesn't exist.
1705 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1715 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1720 * Need to use rmdir for removing directories.
1722 if (S_ISDIR(ip->i_mode)) {
1723 error = SET_ERROR(EPERM);
1729 dnlc_remove(dvp, realnmp->pn_buf);
1731 dnlc_remove(dvp, name);
1732 #endif /* HAVE_DNLC */
1734 mutex_enter(&zp->z_lock);
1735 may_delete_now = atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped);
1736 mutex_exit(&zp->z_lock);
1739 * We may delete the znode now, or we may put it in the unlinked set;
1740 * it depends on whether we're the last link, and on whether there are
1741 * other holds on the inode. So we dmu_tx_hold() the right things to
1742 * allow for either case.
1745 tx = dmu_tx_create(zfsvfs->z_os);
1746 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1747 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1748 zfs_sa_upgrade_txholds(tx, zp);
1749 zfs_sa_upgrade_txholds(tx, dzp);
1750 if (may_delete_now) {
1751 toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks;
1752 /* if the file is too big, only hold_free a token amount */
1753 dmu_tx_hold_free(tx, zp->z_id, 0,
1754 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1757 /* are there any extended attributes? */
1758 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1759 &xattr_obj, sizeof (xattr_obj));
1760 if (error == 0 && xattr_obj) {
1761 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1763 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1764 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1767 mutex_enter(&zp->z_lock);
1768 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1769 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1770 mutex_exit(&zp->z_lock);
1772 /* charge as an update -- would be nice not to charge at all */
1773 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1776 * Mark this transaction as typically resulting in a net free of space
1778 dmu_tx_mark_netfree(tx);
1780 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1782 zfs_dirent_unlock(dl);
1783 if (error == ERESTART) {
1803 * Remove the directory entry.
1805 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1814 * Hold z_lock so that we can make sure that the ACL obj
1815 * hasn't changed. Could have been deleted due to
1818 mutex_enter(&zp->z_lock);
1819 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1820 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1821 delete_now = may_delete_now && !toobig &&
1822 atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped) &&
1823 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1828 if (xattr_obj_unlinked) {
1829 ASSERT3U(ZTOI(xzp)->i_nlink, ==, 2);
1830 mutex_enter(&xzp->z_lock);
1831 xzp->z_unlinked = 1;
1832 clear_nlink(ZTOI(xzp));
1834 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1835 &links, sizeof (links), tx);
1836 ASSERT3U(error, ==, 0);
1837 mutex_exit(&xzp->z_lock);
1838 zfs_unlinked_add(xzp, tx);
1841 error = sa_remove(zp->z_sa_hdl,
1842 SA_ZPL_XATTR(zfsvfs), tx);
1844 error = sa_update(zp->z_sa_hdl,
1845 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1846 sizeof (uint64_t), tx);
1850 * Add to the unlinked set because a new reference could be
1851 * taken concurrently resulting in a deferred destruction.
1853 zfs_unlinked_add(zp, tx);
1854 mutex_exit(&zp->z_lock);
1855 } else if (unlinked) {
1856 mutex_exit(&zp->z_lock);
1857 zfs_unlinked_add(zp, tx);
1861 if (flags & FIGNORECASE)
1863 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1870 zfs_dirent_unlock(dl);
1871 zfs_inode_update(dzp);
1872 zfs_inode_update(zp);
1880 zfs_inode_update(xzp);
1881 zfs_iput_async(ZTOI(xzp));
1884 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1885 zil_commit(zilog, 0);
1892 * Create a new directory and insert it into dip using the name
1893 * provided. Return a pointer to the inserted directory.
1895 * IN: dip - inode of directory to add subdir to.
1896 * dirname - name of new directory.
1897 * vap - attributes of new directory.
1898 * cr - credentials of caller.
1899 * vsecp - ACL to be set
1901 * OUT: ipp - inode of created directory.
1903 * RETURN: 0 if success
1904 * error code if failure
1907 * dip - ctime|mtime updated
1908 * ipp - ctime|mtime|atime updated
1912 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1913 cred_t *cr, int flags, vsecattr_t *vsecp)
1915 znode_t *zp, *dzp = ITOZ(dip);
1916 zfsvfs_t *zfsvfs = ITOZSB(dip);
1924 gid_t gid = crgetgid(cr);
1925 zfs_acl_ids_t acl_ids;
1926 boolean_t fuid_dirtied;
1927 boolean_t waited = B_FALSE;
1929 ASSERT(S_ISDIR(vap->va_mode));
1932 * If we have an ephemeral id, ACL, or XVATTR then
1933 * make sure file system is at proper version
1937 if (zfsvfs->z_use_fuids == B_FALSE &&
1938 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1939 return (SET_ERROR(EINVAL));
1941 if (dirname == NULL)
1942 return (SET_ERROR(EINVAL));
1946 zilog = zfsvfs->z_log;
1948 if (dzp->z_pflags & ZFS_XATTR) {
1950 return (SET_ERROR(EINVAL));
1953 if (zfsvfs->z_utf8 && u8_validate(dirname,
1954 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1956 return (SET_ERROR(EILSEQ));
1958 if (flags & FIGNORECASE)
1961 if (vap->va_mask & ATTR_XVATTR) {
1962 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1963 crgetuid(cr), cr, vap->va_mode)) != 0) {
1969 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1970 vsecp, &acl_ids)) != 0) {
1975 * First make sure the new directory doesn't exist.
1977 * Existence is checked first to make sure we don't return
1978 * EACCES instead of EEXIST which can cause some applications
1984 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1986 zfs_acl_ids_free(&acl_ids);
1991 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1992 zfs_acl_ids_free(&acl_ids);
1993 zfs_dirent_unlock(dl);
1998 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1999 zfs_acl_ids_free(&acl_ids);
2000 zfs_dirent_unlock(dl);
2002 return (SET_ERROR(EDQUOT));
2006 * Add a new entry to the directory.
2008 tx = dmu_tx_create(zfsvfs->z_os);
2009 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2010 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2011 fuid_dirtied = zfsvfs->z_fuid_dirty;
2013 zfs_fuid_txhold(zfsvfs, tx);
2014 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2015 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2016 acl_ids.z_aclp->z_acl_bytes);
2019 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2020 ZFS_SA_BASE_ATTR_SIZE);
2022 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2024 zfs_dirent_unlock(dl);
2025 if (error == ERESTART) {
2031 zfs_acl_ids_free(&acl_ids);
2040 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2043 zfs_fuid_sync(zfsvfs, tx);
2046 * Now put new name in parent dir.
2048 (void) zfs_link_create(dl, zp, tx, ZNEW);
2052 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2053 if (flags & FIGNORECASE)
2055 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2056 acl_ids.z_fuidp, vap);
2058 zfs_acl_ids_free(&acl_ids);
2062 zfs_dirent_unlock(dl);
2064 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2065 zil_commit(zilog, 0);
2067 zfs_inode_update(dzp);
2068 zfs_inode_update(zp);
2074 * Remove a directory subdir entry. If the current working
2075 * directory is the same as the subdir to be removed, the
2078 * IN: dip - inode of directory to remove from.
2079 * name - name of directory to be removed.
2080 * cwd - inode of current working directory.
2081 * cr - credentials of caller.
2082 * flags - case flags
2084 * RETURN: 0 on success, error code on failure.
2087 * dip - ctime|mtime updated
2091 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
2094 znode_t *dzp = ITOZ(dip);
2097 zfsvfs_t *zfsvfs = ITOZSB(dip);
2103 boolean_t waited = B_FALSE;
2106 return (SET_ERROR(EINVAL));
2110 zilog = zfsvfs->z_log;
2112 if (flags & FIGNORECASE)
2118 * Attempt to lock directory; fail if entry doesn't exist.
2120 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2128 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
2132 if (!S_ISDIR(ip->i_mode)) {
2133 error = SET_ERROR(ENOTDIR);
2138 error = SET_ERROR(EINVAL);
2143 * Grab a lock on the directory to make sure that no one is
2144 * trying to add (or lookup) entries while we are removing it.
2146 rw_enter(&zp->z_name_lock, RW_WRITER);
2149 * Grab a lock on the parent pointer to make sure we play well
2150 * with the treewalk and directory rename code.
2152 rw_enter(&zp->z_parent_lock, RW_WRITER);
2154 tx = dmu_tx_create(zfsvfs->z_os);
2155 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2156 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2157 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2158 zfs_sa_upgrade_txholds(tx, zp);
2159 zfs_sa_upgrade_txholds(tx, dzp);
2160 dmu_tx_mark_netfree(tx);
2161 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2163 rw_exit(&zp->z_parent_lock);
2164 rw_exit(&zp->z_name_lock);
2165 zfs_dirent_unlock(dl);
2166 if (error == ERESTART) {
2179 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2182 uint64_t txtype = TX_RMDIR;
2183 if (flags & FIGNORECASE)
2185 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2190 rw_exit(&zp->z_parent_lock);
2191 rw_exit(&zp->z_name_lock);
2193 zfs_dirent_unlock(dl);
2195 zfs_inode_update(dzp);
2196 zfs_inode_update(zp);
2199 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2200 zil_commit(zilog, 0);
2207 * Read as many directory entries as will fit into the provided
2208 * dirent buffer from the given directory cursor position.
2210 * IN: ip - inode of directory to read.
2211 * dirent - buffer for directory entries.
2213 * OUT: dirent - filler buffer of directory entries.
2215 * RETURN: 0 if success
2216 * error code if failure
2219 * ip - atime updated
2221 * Note that the low 4 bits of the cookie returned by zap is always zero.
2222 * This allows us to use the low range for "special" directory entries:
2223 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2224 * we use the offset 2 for the '.zfs' directory.
2228 zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
2230 znode_t *zp = ITOZ(ip);
2231 zfsvfs_t *zfsvfs = ITOZSB(ip);
2234 zap_attribute_t zap;
2240 uint64_t offset; /* must be unsigned; checks for < 1 */
2245 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2246 &parent, sizeof (parent))) != 0)
2250 * Quit if directory has been removed (posix)
2258 prefetch = zp->z_zn_prefetch;
2261 * Initialize the iterator cursor.
2265 * Start iteration from the beginning of the directory.
2267 zap_cursor_init(&zc, os, zp->z_id);
2270 * The offset is a serialized cursor.
2272 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2276 * Transform to file-system independent format
2281 * Special case `.', `..', and `.zfs'.
2284 (void) strcpy(zap.za_name, ".");
2285 zap.za_normalization_conflict = 0;
2288 } else if (offset == 1) {
2289 (void) strcpy(zap.za_name, "..");
2290 zap.za_normalization_conflict = 0;
2293 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2294 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2295 zap.za_normalization_conflict = 0;
2296 objnum = ZFSCTL_INO_ROOT;
2302 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2303 if (error == ENOENT)
2310 * Allow multiple entries provided the first entry is
2311 * the object id. Non-zpl consumers may safely make
2312 * use of the additional space.
2314 * XXX: This should be a feature flag for compatibility
2316 if (zap.za_integer_length != 8 ||
2317 zap.za_num_integers == 0) {
2318 cmn_err(CE_WARN, "zap_readdir: bad directory "
2319 "entry, obj = %lld, offset = %lld, "
2320 "length = %d, num = %lld\n",
2321 (u_longlong_t)zp->z_id,
2322 (u_longlong_t)offset,
2323 zap.za_integer_length,
2324 (u_longlong_t)zap.za_num_integers);
2325 error = SET_ERROR(ENXIO);
2329 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2330 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2333 done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name),
2338 /* Prefetch znode */
2340 dmu_prefetch(os, objnum, 0, 0, 0,
2341 ZIO_PRIORITY_SYNC_READ);
2345 * Move to the next entry, fill in the previous offset.
2347 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2348 zap_cursor_advance(&zc);
2349 offset = zap_cursor_serialize(&zc);
2355 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2358 zap_cursor_fini(&zc);
2359 if (error == ENOENT)
2367 ulong_t zfs_fsync_sync_cnt = 4;
2370 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2372 znode_t *zp = ITOZ(ip);
2373 zfsvfs_t *zfsvfs = ITOZSB(ip);
2375 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2377 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2380 zil_commit(zfsvfs->z_log, zp->z_id);
2383 tsd_set(zfs_fsyncer_key, NULL);
2390 * Get the requested file attributes and place them in the provided
2393 * IN: ip - inode of file.
2394 * vap - va_mask identifies requested attributes.
2395 * If ATTR_XVATTR set, then optional attrs are requested
2396 * flags - ATTR_NOACLCHECK (CIFS server context)
2397 * cr - credentials of caller.
2399 * OUT: vap - attribute values.
2401 * RETURN: 0 (always succeeds)
2405 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2407 znode_t *zp = ITOZ(ip);
2408 zfsvfs_t *zfsvfs = ITOZSB(ip);
2411 uint64_t atime[2], mtime[2], ctime[2];
2412 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2413 xoptattr_t *xoap = NULL;
2414 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2415 sa_bulk_attr_t bulk[3];
2421 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2423 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
2424 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2425 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2427 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2433 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2434 * Also, if we are the owner don't bother, since owner should
2435 * always be allowed to read basic attributes of file.
2437 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2438 (vap->va_uid != crgetuid(cr))) {
2439 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2447 * Return all attributes. It's cheaper to provide the answer
2448 * than to determine whether we were asked the question.
2451 mutex_enter(&zp->z_lock);
2452 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2453 vap->va_mode = zp->z_mode;
2454 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2455 vap->va_nodeid = zp->z_id;
2456 if ((zp->z_id == zfsvfs->z_root) && zfs_show_ctldir(zp))
2457 links = ZTOI(zp)->i_nlink + 1;
2459 links = ZTOI(zp)->i_nlink;
2460 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2461 vap->va_size = i_size_read(ip);
2462 vap->va_rdev = ip->i_rdev;
2463 vap->va_seq = ip->i_generation;
2466 * Add in any requested optional attributes and the create time.
2467 * Also set the corresponding bits in the returned attribute bitmap.
2469 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2470 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2472 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2473 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2476 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2477 xoap->xoa_readonly =
2478 ((zp->z_pflags & ZFS_READONLY) != 0);
2479 XVA_SET_RTN(xvap, XAT_READONLY);
2482 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2484 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2485 XVA_SET_RTN(xvap, XAT_SYSTEM);
2488 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2490 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2491 XVA_SET_RTN(xvap, XAT_HIDDEN);
2494 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2495 xoap->xoa_nounlink =
2496 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2497 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2500 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2501 xoap->xoa_immutable =
2502 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2503 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2506 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2507 xoap->xoa_appendonly =
2508 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2509 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2512 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2514 ((zp->z_pflags & ZFS_NODUMP) != 0);
2515 XVA_SET_RTN(xvap, XAT_NODUMP);
2518 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2520 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2521 XVA_SET_RTN(xvap, XAT_OPAQUE);
2524 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2525 xoap->xoa_av_quarantined =
2526 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2527 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2530 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2531 xoap->xoa_av_modified =
2532 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2533 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2536 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2537 S_ISREG(ip->i_mode)) {
2538 zfs_sa_get_scanstamp(zp, xvap);
2541 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2544 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2545 times, sizeof (times));
2546 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2547 XVA_SET_RTN(xvap, XAT_CREATETIME);
2550 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2551 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2552 XVA_SET_RTN(xvap, XAT_REPARSE);
2554 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2555 xoap->xoa_generation = ip->i_generation;
2556 XVA_SET_RTN(xvap, XAT_GEN);
2559 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2561 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2562 XVA_SET_RTN(xvap, XAT_OFFLINE);
2565 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2567 ((zp->z_pflags & ZFS_SPARSE) != 0);
2568 XVA_SET_RTN(xvap, XAT_SPARSE);
2572 ZFS_TIME_DECODE(&vap->va_atime, atime);
2573 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2574 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2576 mutex_exit(&zp->z_lock);
2578 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2580 if (zp->z_blksz == 0) {
2582 * Block size hasn't been set; suggest maximal I/O transfers.
2584 vap->va_blksize = zfsvfs->z_max_blksz;
2592 * Get the basic file attributes and place them in the provided kstat
2593 * structure. The inode is assumed to be the authoritative source
2594 * for most of the attributes. However, the znode currently has the
2595 * authoritative atime, blksize, and block count.
2597 * IN: ip - inode of file.
2599 * OUT: sp - kstat values.
2601 * RETURN: 0 (always succeeds)
2605 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2607 znode_t *zp = ITOZ(ip);
2608 zfsvfs_t *zfsvfs = ITOZSB(ip);
2610 u_longlong_t nblocks;
2615 mutex_enter(&zp->z_lock);
2617 generic_fillattr(ip, sp);
2619 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2620 sp->blksize = blksize;
2621 sp->blocks = nblocks;
2623 if (unlikely(zp->z_blksz == 0)) {
2625 * Block size hasn't been set; suggest maximal I/O transfers.
2627 sp->blksize = zfsvfs->z_max_blksz;
2630 mutex_exit(&zp->z_lock);
2633 * Required to prevent NFS client from detecting different inode
2634 * numbers of snapshot root dentry before and after snapshot mount.
2636 if (zfsvfs->z_issnap) {
2637 if (ip->i_sb->s_root->d_inode == ip)
2638 sp->ino = ZFSCTL_INO_SNAPDIRS -
2639 dmu_objset_id(zfsvfs->z_os);
2648 * Set the file attributes to the values contained in the
2651 * IN: ip - inode of file to be modified.
2652 * vap - new attribute values.
2653 * If ATTR_XVATTR set, then optional attrs are being set
2654 * flags - ATTR_UTIME set if non-default time values provided.
2655 * - ATTR_NOACLCHECK (CIFS context only).
2656 * cr - credentials of caller.
2658 * RETURN: 0 if success
2659 * error code if failure
2662 * ip - ctime updated, mtime updated if size changed.
2666 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2668 znode_t *zp = ITOZ(ip);
2669 zfsvfs_t *zfsvfs = ITOZSB(ip);
2673 xvattr_t *tmpxvattr;
2674 uint_t mask = vap->va_mask;
2675 uint_t saved_mask = 0;
2678 uint64_t new_kuid = 0, new_kgid = 0, new_uid, new_gid;
2680 uint64_t mtime[2], ctime[2], atime[2];
2682 int need_policy = FALSE;
2684 zfs_fuid_info_t *fuidp = NULL;
2685 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2688 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2689 boolean_t fuid_dirtied = B_FALSE;
2690 sa_bulk_attr_t *bulk, *xattr_bulk;
2691 int count = 0, xattr_count = 0;
2699 zilog = zfsvfs->z_log;
2702 * Make sure that if we have ephemeral uid/gid or xvattr specified
2703 * that file system is at proper version level
2706 if (zfsvfs->z_use_fuids == B_FALSE &&
2707 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2708 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2709 (mask & ATTR_XVATTR))) {
2711 return (SET_ERROR(EINVAL));
2714 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2716 return (SET_ERROR(EISDIR));
2719 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2721 return (SET_ERROR(EINVAL));
2725 * If this is an xvattr_t, then get a pointer to the structure of
2726 * optional attributes. If this is NULL, then we have a vattr_t.
2728 xoap = xva_getxoptattr(xvap);
2730 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP);
2731 xva_init(tmpxvattr);
2733 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2734 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2737 * Immutable files can only alter immutable bit and atime
2739 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2740 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2741 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2742 err = SET_ERROR(EPERM);
2746 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2747 err = SET_ERROR(EPERM);
2752 * Verify timestamps doesn't overflow 32 bits.
2753 * ZFS can handle large timestamps, but 32bit syscalls can't
2754 * handle times greater than 2039. This check should be removed
2755 * once large timestamps are fully supported.
2757 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2758 if (((mask & ATTR_ATIME) &&
2759 TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2760 ((mask & ATTR_MTIME) &&
2761 TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2762 err = SET_ERROR(EOVERFLOW);
2771 /* Can this be moved to before the top label? */
2772 if (zfs_is_readonly(zfsvfs)) {
2773 err = SET_ERROR(EROFS);
2778 * First validate permissions
2781 if (mask & ATTR_SIZE) {
2782 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2787 * XXX - Note, we are not providing any open
2788 * mode flags here (like FNDELAY), so we may
2789 * block if there are locks present... this
2790 * should be addressed in openat().
2792 /* XXX - would it be OK to generate a log record here? */
2793 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2798 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2799 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2800 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2801 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2802 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2803 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2804 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2805 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2806 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2810 if (mask & (ATTR_UID|ATTR_GID)) {
2811 int idmask = (mask & (ATTR_UID|ATTR_GID));
2816 * NOTE: even if a new mode is being set,
2817 * we may clear S_ISUID/S_ISGID bits.
2820 if (!(mask & ATTR_MODE))
2821 vap->va_mode = zp->z_mode;
2824 * Take ownership or chgrp to group we are a member of
2827 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2828 take_group = (mask & ATTR_GID) &&
2829 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2832 * If both ATTR_UID and ATTR_GID are set then take_owner and
2833 * take_group must both be set in order to allow taking
2836 * Otherwise, send the check through secpolicy_vnode_setattr()
2840 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2841 take_owner && take_group) ||
2842 ((idmask == ATTR_UID) && take_owner) ||
2843 ((idmask == ATTR_GID) && take_group)) {
2844 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2845 skipaclchk, cr) == 0) {
2847 * Remove setuid/setgid for non-privileged users
2849 (void) secpolicy_setid_clear(vap, cr);
2850 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2859 mutex_enter(&zp->z_lock);
2860 oldva.va_mode = zp->z_mode;
2861 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2862 if (mask & ATTR_XVATTR) {
2864 * Update xvattr mask to include only those attributes
2865 * that are actually changing.
2867 * the bits will be restored prior to actually setting
2868 * the attributes so the caller thinks they were set.
2870 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2871 if (xoap->xoa_appendonly !=
2872 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2875 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2876 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2880 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2881 if (xoap->xoa_nounlink !=
2882 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2885 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2886 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2890 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2891 if (xoap->xoa_immutable !=
2892 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2895 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2896 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2900 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2901 if (xoap->xoa_nodump !=
2902 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2905 XVA_CLR_REQ(xvap, XAT_NODUMP);
2906 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2910 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2911 if (xoap->xoa_av_modified !=
2912 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2915 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2916 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2920 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2921 if ((!S_ISREG(ip->i_mode) &&
2922 xoap->xoa_av_quarantined) ||
2923 xoap->xoa_av_quarantined !=
2924 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2927 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2928 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2932 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2933 mutex_exit(&zp->z_lock);
2934 err = SET_ERROR(EPERM);
2938 if (need_policy == FALSE &&
2939 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2940 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2945 mutex_exit(&zp->z_lock);
2947 if (mask & ATTR_MODE) {
2948 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2949 err = secpolicy_setid_setsticky_clear(ip, vap,
2954 trim_mask |= ATTR_MODE;
2962 * If trim_mask is set then take ownership
2963 * has been granted or write_acl is present and user
2964 * has the ability to modify mode. In that case remove
2965 * UID|GID and or MODE from mask so that
2966 * secpolicy_vnode_setattr() doesn't revoke it.
2970 saved_mask = vap->va_mask;
2971 vap->va_mask &= ~trim_mask;
2973 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2974 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2979 vap->va_mask |= saved_mask;
2983 * secpolicy_vnode_setattr, or take ownership may have
2986 mask = vap->va_mask;
2988 if ((mask & (ATTR_UID | ATTR_GID))) {
2989 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2990 &xattr_obj, sizeof (xattr_obj));
2992 if (err == 0 && xattr_obj) {
2993 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2997 if (mask & ATTR_UID) {
2998 new_kuid = zfs_fuid_create(zfsvfs,
2999 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3000 if (new_kuid != KUID_TO_SUID(ZTOI(zp)->i_uid) &&
3001 zfs_fuid_overquota(zfsvfs, B_FALSE, new_kuid)) {
3004 err = SET_ERROR(EDQUOT);
3009 if (mask & ATTR_GID) {
3010 new_kgid = zfs_fuid_create(zfsvfs,
3011 (uint64_t)vap->va_gid, cr, ZFS_GROUP, &fuidp);
3012 if (new_kgid != KGID_TO_SGID(ZTOI(zp)->i_gid) &&
3013 zfs_fuid_overquota(zfsvfs, B_TRUE, new_kgid)) {
3016 err = SET_ERROR(EDQUOT);
3021 tx = dmu_tx_create(zfsvfs->z_os);
3023 if (mask & ATTR_MODE) {
3024 uint64_t pmode = zp->z_mode;
3026 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3028 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
3030 mutex_enter(&zp->z_lock);
3031 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3033 * Are we upgrading ACL from old V0 format
3036 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3037 zfs_znode_acl_version(zp) ==
3038 ZFS_ACL_VERSION_INITIAL) {
3039 dmu_tx_hold_free(tx, acl_obj, 0,
3041 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3042 0, aclp->z_acl_bytes);
3044 dmu_tx_hold_write(tx, acl_obj, 0,
3047 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3048 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3049 0, aclp->z_acl_bytes);
3051 mutex_exit(&zp->z_lock);
3052 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3054 if ((mask & ATTR_XVATTR) &&
3055 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3056 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3058 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3062 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3065 fuid_dirtied = zfsvfs->z_fuid_dirty;
3067 zfs_fuid_txhold(zfsvfs, tx);
3069 zfs_sa_upgrade_txholds(tx, zp);
3071 err = dmu_tx_assign(tx, TXG_WAIT);
3077 * Set each attribute requested.
3078 * We group settings according to the locks they need to acquire.
3080 * Note: you cannot set ctime directly, although it will be
3081 * updated as a side-effect of calling this function.
3085 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3086 mutex_enter(&zp->z_acl_lock);
3087 mutex_enter(&zp->z_lock);
3089 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3090 &zp->z_pflags, sizeof (zp->z_pflags));
3093 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3094 mutex_enter(&attrzp->z_acl_lock);
3095 mutex_enter(&attrzp->z_lock);
3096 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3097 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3098 sizeof (attrzp->z_pflags));
3101 if (mask & (ATTR_UID|ATTR_GID)) {
3103 if (mask & ATTR_UID) {
3104 ZTOI(zp)->i_uid = SUID_TO_KUID(new_kuid);
3105 new_uid = zfs_uid_read(ZTOI(zp));
3106 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3107 &new_uid, sizeof (new_uid));
3109 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3110 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3112 ZTOI(attrzp)->i_uid = SUID_TO_KUID(new_uid);
3116 if (mask & ATTR_GID) {
3117 ZTOI(zp)->i_gid = SGID_TO_KGID(new_kgid);
3118 new_gid = zfs_gid_read(ZTOI(zp));
3119 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3120 NULL, &new_gid, sizeof (new_gid));
3122 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3123 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3125 ZTOI(attrzp)->i_gid = SGID_TO_KGID(new_kgid);
3128 if (!(mask & ATTR_MODE)) {
3129 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3130 NULL, &new_mode, sizeof (new_mode));
3131 new_mode = zp->z_mode;
3133 err = zfs_acl_chown_setattr(zp);
3136 err = zfs_acl_chown_setattr(attrzp);
3141 if (mask & ATTR_MODE) {
3142 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3143 &new_mode, sizeof (new_mode));
3144 zp->z_mode = ZTOI(zp)->i_mode = new_mode;
3145 ASSERT3P(aclp, !=, NULL);
3146 err = zfs_aclset_common(zp, aclp, cr, tx);
3148 if (zp->z_acl_cached)
3149 zfs_acl_free(zp->z_acl_cached);
3150 zp->z_acl_cached = aclp;
3154 if ((mask & ATTR_ATIME) || zp->z_atime_dirty) {
3155 zp->z_atime_dirty = 0;
3156 ZFS_TIME_ENCODE(&ip->i_atime, atime);
3157 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3158 &atime, sizeof (atime));
3161 if (mask & (ATTR_MTIME | ATTR_SIZE)) {
3162 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3163 ZTOI(zp)->i_mtime = timespec_trunc(vap->va_mtime,
3164 ZTOI(zp)->i_sb->s_time_gran);
3166 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3167 mtime, sizeof (mtime));
3170 if (mask & (ATTR_CTIME | ATTR_SIZE)) {
3171 ZFS_TIME_ENCODE(&vap->va_ctime, ctime);
3172 ZTOI(zp)->i_ctime = timespec_trunc(vap->va_ctime,
3173 ZTOI(zp)->i_sb->s_time_gran);
3174 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3175 ctime, sizeof (ctime));
3178 if (attrzp && mask) {
3179 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3180 SA_ZPL_CTIME(zfsvfs), NULL, &ctime,
3185 * Do this after setting timestamps to prevent timestamp
3186 * update from toggling bit
3189 if (xoap && (mask & ATTR_XVATTR)) {
3192 * restore trimmed off masks
3193 * so that return masks can be set for caller.
3196 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
3197 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3199 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
3200 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3202 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
3203 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3205 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
3206 XVA_SET_REQ(xvap, XAT_NODUMP);
3208 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
3209 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3211 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
3212 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3215 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3216 ASSERT(S_ISREG(ip->i_mode));
3218 zfs_xvattr_set(zp, xvap, tx);
3222 zfs_fuid_sync(zfsvfs, tx);
3225 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3227 mutex_exit(&zp->z_lock);
3228 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3229 mutex_exit(&zp->z_acl_lock);
3232 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3233 mutex_exit(&attrzp->z_acl_lock);
3234 mutex_exit(&attrzp->z_lock);
3237 if (err == 0 && attrzp) {
3238 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3247 zfs_fuid_info_free(fuidp);
3255 if (err == ERESTART)
3258 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3262 zfs_inode_update(zp);
3266 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3267 zil_commit(zilog, 0);
3270 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * 7);
3271 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 7);
3272 kmem_free(tmpxvattr, sizeof (xvattr_t));
3277 typedef struct zfs_zlock {
3278 krwlock_t *zl_rwlock; /* lock we acquired */
3279 znode_t *zl_znode; /* znode we held */
3280 struct zfs_zlock *zl_next; /* next in list */
3284 * Drop locks and release vnodes that were held by zfs_rename_lock().
3287 zfs_rename_unlock(zfs_zlock_t **zlpp)
3291 while ((zl = *zlpp) != NULL) {
3292 if (zl->zl_znode != NULL)
3293 zfs_iput_async(ZTOI(zl->zl_znode));
3294 rw_exit(zl->zl_rwlock);
3295 *zlpp = zl->zl_next;
3296 kmem_free(zl, sizeof (*zl));
3301 * Search back through the directory tree, using the ".." entries.
3302 * Lock each directory in the chain to prevent concurrent renames.
3303 * Fail any attempt to move a directory into one of its own descendants.
3304 * XXX - z_parent_lock can overlap with map or grow locks
3307 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3311 uint64_t rootid = ZTOZSB(zp)->z_root;
3312 uint64_t oidp = zp->z_id;
3313 krwlock_t *rwlp = &szp->z_parent_lock;
3314 krw_t rw = RW_WRITER;
3317 * First pass write-locks szp and compares to zp->z_id.
3318 * Later passes read-lock zp and compare to zp->z_parent.
3321 if (!rw_tryenter(rwlp, rw)) {
3323 * Another thread is renaming in this path.
3324 * Note that if we are a WRITER, we don't have any
3325 * parent_locks held yet.
3327 if (rw == RW_READER && zp->z_id > szp->z_id) {
3329 * Drop our locks and restart
3331 zfs_rename_unlock(&zl);
3335 rwlp = &szp->z_parent_lock;
3340 * Wait for other thread to drop its locks
3346 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3347 zl->zl_rwlock = rwlp;
3348 zl->zl_znode = NULL;
3349 zl->zl_next = *zlpp;
3352 if (oidp == szp->z_id) /* We're a descendant of szp */
3353 return (SET_ERROR(EINVAL));
3355 if (oidp == rootid) /* We've hit the top */
3358 if (rw == RW_READER) { /* i.e. not the first pass */
3359 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3364 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3365 &oidp, sizeof (oidp));
3366 rwlp = &zp->z_parent_lock;
3369 } while (zp->z_id != sdzp->z_id);
3375 * Move an entry from the provided source directory to the target
3376 * directory. Change the entry name as indicated.
3378 * IN: sdip - Source directory containing the "old entry".
3379 * snm - Old entry name.
3380 * tdip - Target directory to contain the "new entry".
3381 * tnm - New entry name.
3382 * cr - credentials of caller.
3383 * flags - case flags
3385 * RETURN: 0 on success, error code on failure.
3388 * sdip,tdip - ctime|mtime updated
3392 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3393 cred_t *cr, int flags)
3395 znode_t *tdzp, *szp, *tzp;
3396 znode_t *sdzp = ITOZ(sdip);
3397 zfsvfs_t *zfsvfs = ITOZSB(sdip);
3399 zfs_dirlock_t *sdl, *tdl;
3402 int cmp, serr, terr;
3405 boolean_t waited = B_FALSE;
3407 if (snm == NULL || tnm == NULL)
3408 return (SET_ERROR(EINVAL));
3411 ZFS_VERIFY_ZP(sdzp);
3412 zilog = zfsvfs->z_log;
3415 ZFS_VERIFY_ZP(tdzp);
3418 * We check i_sb because snapshots and the ctldir must have different
3421 if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) {
3423 return (SET_ERROR(EXDEV));
3426 if (zfsvfs->z_utf8 && u8_validate(tnm,
3427 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3429 return (SET_ERROR(EILSEQ));
3432 if (flags & FIGNORECASE)
3441 * This is to prevent the creation of links into attribute space
3442 * by renaming a linked file into/outof an attribute directory.
3443 * See the comment in zfs_link() for why this is considered bad.
3445 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3447 return (SET_ERROR(EINVAL));
3451 * Lock source and target directory entries. To prevent deadlock,
3452 * a lock ordering must be defined. We lock the directory with
3453 * the smallest object id first, or if it's a tie, the one with
3454 * the lexically first name.
3456 if (sdzp->z_id < tdzp->z_id) {
3458 } else if (sdzp->z_id > tdzp->z_id) {
3462 * First compare the two name arguments without
3463 * considering any case folding.
3465 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3467 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3468 ASSERT(error == 0 || !zfsvfs->z_utf8);
3471 * POSIX: "If the old argument and the new argument
3472 * both refer to links to the same existing file,
3473 * the rename() function shall return successfully
3474 * and perform no other action."
3480 * If the file system is case-folding, then we may
3481 * have some more checking to do. A case-folding file
3482 * system is either supporting mixed case sensitivity
3483 * access or is completely case-insensitive. Note
3484 * that the file system is always case preserving.
3486 * In mixed sensitivity mode case sensitive behavior
3487 * is the default. FIGNORECASE must be used to
3488 * explicitly request case insensitive behavior.
3490 * If the source and target names provided differ only
3491 * by case (e.g., a request to rename 'tim' to 'Tim'),
3492 * we will treat this as a special case in the
3493 * case-insensitive mode: as long as the source name
3494 * is an exact match, we will allow this to proceed as
3495 * a name-change request.
3497 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3498 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3499 flags & FIGNORECASE)) &&
3500 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3503 * case preserving rename request, require exact
3512 * If the source and destination directories are the same, we should
3513 * grab the z_name_lock of that directory only once.
3517 rw_enter(&sdzp->z_name_lock, RW_READER);
3521 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3522 ZEXISTS | zflg, NULL, NULL);
3523 terr = zfs_dirent_lock(&tdl,
3524 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3526 terr = zfs_dirent_lock(&tdl,
3527 tdzp, tnm, &tzp, zflg, NULL, NULL);
3528 serr = zfs_dirent_lock(&sdl,
3529 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3535 * Source entry invalid or not there.
3538 zfs_dirent_unlock(tdl);
3544 rw_exit(&sdzp->z_name_lock);
3546 if (strcmp(snm, "..") == 0)
3552 zfs_dirent_unlock(sdl);
3556 rw_exit(&sdzp->z_name_lock);
3558 if (strcmp(tnm, "..") == 0)
3565 * Must have write access at the source to remove the old entry
3566 * and write access at the target to create the new entry.
3567 * Note that if target and source are the same, this can be
3568 * done in a single check.
3571 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3574 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3576 * Check to make sure rename is valid.
3577 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3579 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3584 * Does target exist?
3588 * Source and target must be the same type.
3590 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3591 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3592 error = SET_ERROR(ENOTDIR);
3596 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3597 error = SET_ERROR(EISDIR);
3602 * POSIX dictates that when the source and target
3603 * entries refer to the same file object, rename
3604 * must do nothing and exit without error.
3606 if (szp->z_id == tzp->z_id) {
3612 tx = dmu_tx_create(zfsvfs->z_os);
3613 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3614 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3615 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3616 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3618 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3619 zfs_sa_upgrade_txholds(tx, tdzp);
3622 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3623 zfs_sa_upgrade_txholds(tx, tzp);
3626 zfs_sa_upgrade_txholds(tx, szp);
3627 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3628 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3631 zfs_rename_unlock(&zl);
3632 zfs_dirent_unlock(sdl);
3633 zfs_dirent_unlock(tdl);
3636 rw_exit(&sdzp->z_name_lock);
3638 if (error == ERESTART) {
3655 if (tzp) /* Attempt to remove the existing target */
3656 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3659 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3661 szp->z_pflags |= ZFS_AV_MODIFIED;
3663 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3664 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3667 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3669 zfs_log_rename(zilog, tx, TX_RENAME |
3670 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3671 sdl->dl_name, tdzp, tdl->dl_name, szp);
3674 * At this point, we have successfully created
3675 * the target name, but have failed to remove
3676 * the source name. Since the create was done
3677 * with the ZRENAMING flag, there are
3678 * complications; for one, the link count is
3679 * wrong. The easiest way to deal with this
3680 * is to remove the newly created target, and
3681 * return the original error. This must
3682 * succeed; fortunately, it is very unlikely to
3683 * fail, since we just created it.
3685 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3686 ZRENAMING, NULL), ==, 0);
3694 zfs_rename_unlock(&zl);
3696 zfs_dirent_unlock(sdl);
3697 zfs_dirent_unlock(tdl);
3699 zfs_inode_update(sdzp);
3701 rw_exit(&sdzp->z_name_lock);
3704 zfs_inode_update(tdzp);
3706 zfs_inode_update(szp);
3709 zfs_inode_update(tzp);
3713 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3714 zil_commit(zilog, 0);
3721 * Insert the indicated symbolic reference entry into the directory.
3723 * IN: dip - Directory to contain new symbolic link.
3724 * link - Name for new symlink entry.
3725 * vap - Attributes of new entry.
3726 * target - Target path of new symlink.
3728 * cr - credentials of caller.
3729 * flags - case flags
3731 * RETURN: 0 on success, error code on failure.
3734 * dip - ctime|mtime updated
3738 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3739 struct inode **ipp, cred_t *cr, int flags)
3741 znode_t *zp, *dzp = ITOZ(dip);
3744 zfsvfs_t *zfsvfs = ITOZSB(dip);
3746 uint64_t len = strlen(link);
3749 zfs_acl_ids_t acl_ids;
3750 boolean_t fuid_dirtied;
3751 uint64_t txtype = TX_SYMLINK;
3752 boolean_t waited = B_FALSE;
3754 ASSERT(S_ISLNK(vap->va_mode));
3757 return (SET_ERROR(EINVAL));
3761 zilog = zfsvfs->z_log;
3763 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3764 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3766 return (SET_ERROR(EILSEQ));
3768 if (flags & FIGNORECASE)
3771 if (len > MAXPATHLEN) {
3773 return (SET_ERROR(ENAMETOOLONG));
3776 if ((error = zfs_acl_ids_create(dzp, 0,
3777 vap, cr, NULL, &acl_ids)) != 0) {
3785 * Attempt to lock directory; fail if entry already exists.
3787 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3789 zfs_acl_ids_free(&acl_ids);
3794 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3795 zfs_acl_ids_free(&acl_ids);
3796 zfs_dirent_unlock(dl);
3801 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3802 zfs_acl_ids_free(&acl_ids);
3803 zfs_dirent_unlock(dl);
3805 return (SET_ERROR(EDQUOT));
3807 tx = dmu_tx_create(zfsvfs->z_os);
3808 fuid_dirtied = zfsvfs->z_fuid_dirty;
3809 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3810 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3811 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3812 ZFS_SA_BASE_ATTR_SIZE + len);
3813 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3814 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3815 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3816 acl_ids.z_aclp->z_acl_bytes);
3819 zfs_fuid_txhold(zfsvfs, tx);
3820 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3822 zfs_dirent_unlock(dl);
3823 if (error == ERESTART) {
3829 zfs_acl_ids_free(&acl_ids);
3836 * Create a new object for the symlink.
3837 * for version 4 ZPL datsets the symlink will be an SA attribute
3839 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3842 zfs_fuid_sync(zfsvfs, tx);
3844 mutex_enter(&zp->z_lock);
3846 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3849 zfs_sa_symlink(zp, link, len, tx);
3850 mutex_exit(&zp->z_lock);
3853 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3854 &zp->z_size, sizeof (zp->z_size), tx);
3856 * Insert the new object into the directory.
3858 (void) zfs_link_create(dl, zp, tx, ZNEW);
3860 if (flags & FIGNORECASE)
3862 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3864 zfs_inode_update(dzp);
3865 zfs_inode_update(zp);
3867 zfs_acl_ids_free(&acl_ids);
3871 zfs_dirent_unlock(dl);
3875 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3876 zil_commit(zilog, 0);
3883 * Return, in the buffer contained in the provided uio structure,
3884 * the symbolic path referred to by ip.
3886 * IN: ip - inode of symbolic link
3887 * uio - structure to contain the link path.
3888 * cr - credentials of caller.
3890 * RETURN: 0 if success
3891 * error code if failure
3894 * ip - atime updated
3898 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3900 znode_t *zp = ITOZ(ip);
3901 zfsvfs_t *zfsvfs = ITOZSB(ip);
3907 mutex_enter(&zp->z_lock);
3909 error = sa_lookup_uio(zp->z_sa_hdl,
3910 SA_ZPL_SYMLINK(zfsvfs), uio);
3912 error = zfs_sa_readlink(zp, uio);
3913 mutex_exit(&zp->z_lock);
3920 * Insert a new entry into directory tdip referencing sip.
3922 * IN: tdip - Directory to contain new entry.
3923 * sip - inode of new entry.
3924 * name - name of new entry.
3925 * cr - credentials of caller.
3927 * RETURN: 0 if success
3928 * error code if failure
3931 * tdip - ctime|mtime updated
3932 * sip - ctime updated
3936 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr,
3939 znode_t *dzp = ITOZ(tdip);
3941 zfsvfs_t *zfsvfs = ITOZSB(tdip);
3949 boolean_t waited = B_FALSE;
3950 boolean_t is_tmpfile = 0;
3953 is_tmpfile = (sip->i_nlink == 0 && (sip->i_state & I_LINKABLE));
3955 ASSERT(S_ISDIR(tdip->i_mode));
3958 return (SET_ERROR(EINVAL));
3962 zilog = zfsvfs->z_log;
3965 * POSIX dictates that we return EPERM here.
3966 * Better choices include ENOTSUP or EISDIR.
3968 if (S_ISDIR(sip->i_mode)) {
3970 return (SET_ERROR(EPERM));
3977 * We check i_sb because snapshots and the ctldir must have different
3980 if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) {
3982 return (SET_ERROR(EXDEV));
3985 /* Prevent links to .zfs/shares files */
3987 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3988 &parent, sizeof (uint64_t))) != 0) {
3992 if (parent == zfsvfs->z_shares_dir) {
3994 return (SET_ERROR(EPERM));
3997 if (zfsvfs->z_utf8 && u8_validate(name,
3998 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4000 return (SET_ERROR(EILSEQ));
4002 if (flags & FIGNORECASE)
4006 * We do not support links between attributes and non-attributes
4007 * because of the potential security risk of creating links
4008 * into "normal" file space in order to circumvent restrictions
4009 * imposed in attribute space.
4011 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4013 return (SET_ERROR(EINVAL));
4016 owner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(sip->i_uid),
4018 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4020 return (SET_ERROR(EPERM));
4023 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
4030 * Attempt to lock directory; fail if entry already exists.
4032 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4038 tx = dmu_tx_create(zfsvfs->z_os);
4039 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4040 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4042 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
4044 zfs_sa_upgrade_txholds(tx, szp);
4045 zfs_sa_upgrade_txholds(tx, dzp);
4046 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4048 zfs_dirent_unlock(dl);
4049 if (error == ERESTART) {
4059 /* unmark z_unlinked so zfs_link_create will not reject */
4061 szp->z_unlinked = 0;
4062 error = zfs_link_create(dl, szp, tx, 0);
4065 uint64_t txtype = TX_LINK;
4067 * tmpfile is created to be in z_unlinkedobj, so remove it.
4068 * Also, we don't log in ZIL, be cause all previous file
4069 * operation on the tmpfile are ignored by ZIL. Instead we
4070 * always wait for txg to sync to make sure all previous
4071 * operation are sync safe.
4074 VERIFY(zap_remove_int(zfsvfs->z_os,
4075 zfsvfs->z_unlinkedobj, szp->z_id, tx) == 0);
4077 if (flags & FIGNORECASE)
4079 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4081 } else if (is_tmpfile) {
4082 /* restore z_unlinked since when linking failed */
4083 szp->z_unlinked = 1;
4085 txg = dmu_tx_get_txg(tx);
4088 zfs_dirent_unlock(dl);
4090 if (!is_tmpfile && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4091 zil_commit(zilog, 0);
4094 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), txg);
4096 zfs_inode_update(dzp);
4097 zfs_inode_update(szp);
4103 zfs_putpage_commit_cb(void *arg)
4105 struct page *pp = arg;
4108 end_page_writeback(pp);
4112 * Push a page out to disk, once the page is on stable storage the
4113 * registered commit callback will be run as notification of completion.
4115 * IN: ip - page mapped for inode.
4116 * pp - page to push (page is locked)
4117 * wbc - writeback control data
4119 * RETURN: 0 if success
4120 * error code if failure
4123 * ip - ctime|mtime updated
4127 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
4129 znode_t *zp = ITOZ(ip);
4130 zfsvfs_t *zfsvfs = ITOZSB(ip);
4138 uint64_t mtime[2], ctime[2];
4139 sa_bulk_attr_t bulk[3];
4141 struct address_space *mapping;
4146 ASSERT(PageLocked(pp));
4148 pgoff = page_offset(pp); /* Page byte-offset in file */
4149 offset = i_size_read(ip); /* File length in bytes */
4150 pglen = MIN(PAGE_SIZE, /* Page length in bytes */
4151 P2ROUNDUP(offset, PAGE_SIZE)-pgoff);
4153 /* Page is beyond end of file */
4154 if (pgoff >= offset) {
4160 /* Truncate page length to end of file */
4161 if (pgoff + pglen > offset)
4162 pglen = offset - pgoff;
4166 * FIXME: Allow mmap writes past its quota. The correct fix
4167 * is to register a page_mkwrite() handler to count the page
4168 * against its quota when it is about to be dirtied.
4170 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4171 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4177 * The ordering here is critical and must adhere to the following
4178 * rules in order to avoid deadlocking in either zfs_read() or
4179 * zfs_free_range() due to a lock inversion.
4181 * 1) The page must be unlocked prior to acquiring the range lock.
4182 * This is critical because zfs_read() calls find_lock_page()
4183 * which may block on the page lock while holding the range lock.
4185 * 2) Before setting or clearing write back on a page the range lock
4186 * must be held in order to prevent a lock inversion with the
4187 * zfs_free_range() function.
4189 * This presents a problem because upon entering this function the
4190 * page lock is already held. To safely acquire the range lock the
4191 * page lock must be dropped. This creates a window where another
4192 * process could truncate, invalidate, dirty, or write out the page.
4194 * Therefore, after successfully reacquiring the range and page locks
4195 * the current page state is checked. In the common case everything
4196 * will be as is expected and it can be written out. However, if
4197 * the page state has changed it must be handled accordingly.
4199 mapping = pp->mapping;
4200 redirty_page_for_writepage(wbc, pp);
4203 rl = zfs_range_lock(&zp->z_range_lock, pgoff, pglen, RL_WRITER);
4206 /* Page mapping changed or it was no longer dirty, we're done */
4207 if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) {
4209 zfs_range_unlock(rl);
4214 /* Another process started write block if required */
4215 if (PageWriteback(pp)) {
4217 zfs_range_unlock(rl);
4219 if (wbc->sync_mode != WB_SYNC_NONE)
4220 wait_on_page_writeback(pp);
4226 /* Clear the dirty flag the required locks are held */
4227 if (!clear_page_dirty_for_io(pp)) {
4229 zfs_range_unlock(rl);
4235 * Counterpart for redirty_page_for_writepage() above. This page
4236 * was in fact not skipped and should not be counted as if it were.
4238 wbc->pages_skipped--;
4239 set_page_writeback(pp);
4242 tx = dmu_tx_create(zfsvfs->z_os);
4243 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
4244 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4245 zfs_sa_upgrade_txholds(tx, zp);
4247 err = dmu_tx_assign(tx, TXG_NOWAIT);
4249 if (err == ERESTART)
4253 __set_page_dirty_nobuffers(pp);
4255 end_page_writeback(pp);
4256 zfs_range_unlock(rl);
4262 ASSERT3U(pglen, <=, PAGE_SIZE);
4263 dmu_write(zfsvfs->z_os, zp->z_id, pgoff, pglen, va, tx);
4266 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
4267 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
4268 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zfsvfs), NULL,
4271 /* Preserve the mtime and ctime provided by the inode */
4272 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4273 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4274 zp->z_atime_dirty = 0;
4277 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4279 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
4280 zfs_putpage_commit_cb, pp);
4283 zfs_range_unlock(rl);
4285 if (wbc->sync_mode != WB_SYNC_NONE) {
4287 * Note that this is rarely called under writepages(), because
4288 * writepages() normally handles the entire commit for
4289 * performance reasons.
4291 zil_commit(zfsvfs->z_log, zp->z_id);
4299 * Update the system attributes when the inode has been dirtied. For the
4300 * moment we only update the mode, atime, mtime, and ctime.
4303 zfs_dirty_inode(struct inode *ip, int flags)
4305 znode_t *zp = ITOZ(ip);
4306 zfsvfs_t *zfsvfs = ITOZSB(ip);
4308 uint64_t mode, atime[2], mtime[2], ctime[2];
4309 sa_bulk_attr_t bulk[4];
4313 if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os))
4321 * This is the lazytime semantic indroduced in Linux 4.0
4322 * This flag will only be called from update_time when lazytime is set.
4323 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4324 * Fortunately mtime and ctime are managed within ZFS itself, so we
4325 * only need to dirty atime.
4327 if (flags == I_DIRTY_TIME) {
4328 zp->z_atime_dirty = 1;
4333 tx = dmu_tx_create(zfsvfs->z_os);
4335 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4336 zfs_sa_upgrade_txholds(tx, zp);
4338 error = dmu_tx_assign(tx, TXG_WAIT);
4344 mutex_enter(&zp->z_lock);
4345 zp->z_atime_dirty = 0;
4347 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
4348 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
4349 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
4350 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
4352 /* Preserve the mode, mtime and ctime provided by the inode */
4353 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4354 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4355 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4360 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4361 mutex_exit(&zp->z_lock);
4371 zfs_inactive(struct inode *ip)
4373 znode_t *zp = ITOZ(ip);
4374 zfsvfs_t *zfsvfs = ITOZSB(ip);
4377 int need_unlock = 0;
4379 /* Only read lock if we haven't already write locked, e.g. rollback */
4380 if (!RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)) {
4382 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4384 if (zp->z_sa_hdl == NULL) {
4386 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4390 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4391 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4393 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4394 zfs_sa_upgrade_txholds(tx, zp);
4395 error = dmu_tx_assign(tx, TXG_WAIT);
4399 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4400 mutex_enter(&zp->z_lock);
4401 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4402 (void *)&atime, sizeof (atime), tx);
4403 zp->z_atime_dirty = 0;
4404 mutex_exit(&zp->z_lock);
4411 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4415 * Bounds-check the seek operation.
4417 * IN: ip - inode seeking within
4418 * ooff - old file offset
4419 * noffp - pointer to new file offset
4420 * ct - caller context
4422 * RETURN: 0 if success
4423 * EINVAL if new offset invalid
4427 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4429 if (S_ISDIR(ip->i_mode))
4431 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4435 * Fill pages with data from the disk.
4438 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4440 znode_t *zp = ITOZ(ip);
4441 zfsvfs_t *zfsvfs = ITOZSB(ip);
4443 struct page *cur_pp;
4444 u_offset_t io_off, total;
4451 io_len = nr_pages << PAGE_SHIFT;
4452 i_size = i_size_read(ip);
4453 io_off = page_offset(pl[0]);
4455 if (io_off + io_len > i_size)
4456 io_len = i_size - io_off;
4459 * Iterate over list of pages and read each page individually.
4462 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4465 cur_pp = pl[page_idx++];
4467 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4471 /* convert checksum errors into IO errors */
4473 err = SET_ERROR(EIO);
4482 * Uses zfs_fillpage to read data from the file and fill the pages.
4484 * IN: ip - inode of file to get data from.
4485 * pl - list of pages to read
4486 * nr_pages - number of pages to read
4488 * RETURN: 0 on success, error code on failure.
4491 * vp - atime updated
4495 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4497 znode_t *zp = ITOZ(ip);
4498 zfsvfs_t *zfsvfs = ITOZSB(ip);
4507 err = zfs_fillpage(ip, pl, nr_pages);
4514 * Check ZFS specific permissions to memory map a section of a file.
4516 * IN: ip - inode of the file to mmap
4518 * addrp - start address in memory region
4519 * len - length of memory region
4520 * vm_flags- address flags
4522 * RETURN: 0 if success
4523 * error code if failure
4527 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4528 unsigned long vm_flags)
4530 znode_t *zp = ITOZ(ip);
4531 zfsvfs_t *zfsvfs = ITOZSB(ip);
4536 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4537 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4539 return (SET_ERROR(EPERM));
4542 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4543 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4545 return (SET_ERROR(EACCES));
4548 if (off < 0 || len > MAXOFFSET_T - off) {
4550 return (SET_ERROR(ENXIO));
4558 * convoff - converts the given data (start, whence) to the
4562 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4567 if ((lckdat->l_whence == 2) || (whence == 2)) {
4568 if ((error = zfs_getattr(ip, &vap, 0, CRED())))
4572 switch (lckdat->l_whence) {
4574 lckdat->l_start += offset;
4577 lckdat->l_start += vap.va_size;
4582 return (SET_ERROR(EINVAL));
4585 if (lckdat->l_start < 0)
4586 return (SET_ERROR(EINVAL));
4590 lckdat->l_start -= offset;
4593 lckdat->l_start -= vap.va_size;
4598 return (SET_ERROR(EINVAL));
4601 lckdat->l_whence = (short)whence;
4606 * Free or allocate space in a file. Currently, this function only
4607 * supports the `F_FREESP' command. However, this command is somewhat
4608 * misnamed, as its functionality includes the ability to allocate as
4609 * well as free space.
4611 * IN: ip - inode of file to free data in.
4612 * cmd - action to take (only F_FREESP supported).
4613 * bfp - section of file to free/alloc.
4614 * flag - current file open mode flags.
4615 * offset - current file offset.
4616 * cr - credentials of caller [UNUSED].
4618 * RETURN: 0 on success, error code on failure.
4621 * ip - ctime|mtime updated
4625 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4626 offset_t offset, cred_t *cr)
4628 znode_t *zp = ITOZ(ip);
4629 zfsvfs_t *zfsvfs = ITOZSB(ip);
4636 if (cmd != F_FREESP) {
4638 return (SET_ERROR(EINVAL));
4642 * Callers might not be able to detect properly that we are read-only,
4643 * so check it explicitly here.
4645 if (zfs_is_readonly(zfsvfs)) {
4647 return (SET_ERROR(EROFS));
4650 if ((error = convoff(ip, bfp, 0, offset))) {
4655 if (bfp->l_len < 0) {
4657 return (SET_ERROR(EINVAL));
4661 * Permissions aren't checked on Solaris because on this OS
4662 * zfs_space() can only be called with an opened file handle.
4663 * On Linux we can get here through truncate_range() which
4664 * operates directly on inodes, so we need to check access rights.
4666 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4672 len = bfp->l_len; /* 0 means from off to end of file */
4674 error = zfs_freesp(zp, off, len, flag, TRUE);
4682 zfs_fid(struct inode *ip, fid_t *fidp)
4684 znode_t *zp = ITOZ(ip);
4685 zfsvfs_t *zfsvfs = ITOZSB(ip);
4688 uint64_t object = zp->z_id;
4695 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4696 &gen64, sizeof (uint64_t))) != 0) {
4701 gen = (uint32_t)gen64;
4703 size = SHORT_FID_LEN;
4705 zfid = (zfid_short_t *)fidp;
4707 zfid->zf_len = size;
4709 for (i = 0; i < sizeof (zfid->zf_object); i++)
4710 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4712 /* Must have a non-zero generation number to distinguish from .zfs */
4715 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4716 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4724 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4726 znode_t *zp = ITOZ(ip);
4727 zfsvfs_t *zfsvfs = ITOZSB(ip);
4729 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4733 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4741 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4743 znode_t *zp = ITOZ(ip);
4744 zfsvfs_t *zfsvfs = ITOZSB(ip);
4746 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4747 zilog_t *zilog = zfsvfs->z_log;
4752 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4754 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4755 zil_commit(zilog, 0);
4761 #ifdef HAVE_UIO_ZEROCOPY
4763 * Tunable, both must be a power of 2.
4765 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4766 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4767 * an arcbuf for a partial block read
4769 int zcr_blksz_min = (1 << 10); /* 1K */
4770 int zcr_blksz_max = (1 << 17); /* 128K */
4774 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4776 znode_t *zp = ITOZ(ip);
4777 zfsvfs_t *zfsvfs = ITOZSB(ip);
4778 int max_blksz = zfsvfs->z_max_blksz;
4779 uio_t *uio = &xuio->xu_uio;
4780 ssize_t size = uio->uio_resid;
4781 offset_t offset = uio->uio_loffset;
4786 int preamble, postamble;
4788 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4789 return (SET_ERROR(EINVAL));
4796 * Loan out an arc_buf for write if write size is bigger than
4797 * max_blksz, and the file's block size is also max_blksz.
4800 if (size < blksz || zp->z_blksz != blksz) {
4802 return (SET_ERROR(EINVAL));
4805 * Caller requests buffers for write before knowing where the
4806 * write offset might be (e.g. NFS TCP write).
4811 preamble = P2PHASE(offset, blksz);
4813 preamble = blksz - preamble;
4818 postamble = P2PHASE(size, blksz);
4821 fullblk = size / blksz;
4822 (void) dmu_xuio_init(xuio,
4823 (preamble != 0) + fullblk + (postamble != 0));
4826 * Have to fix iov base/len for partial buffers. They
4827 * currently represent full arc_buf's.
4830 /* data begins in the middle of the arc_buf */
4831 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4834 (void) dmu_xuio_add(xuio, abuf,
4835 blksz - preamble, preamble);
4838 for (i = 0; i < fullblk; i++) {
4839 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4842 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4846 /* data ends in the middle of the arc_buf */
4847 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4850 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4855 * Loan out an arc_buf for read if the read size is larger than
4856 * the current file block size. Block alignment is not
4857 * considered. Partial arc_buf will be loaned out for read.
4859 blksz = zp->z_blksz;
4860 if (blksz < zcr_blksz_min)
4861 blksz = zcr_blksz_min;
4862 if (blksz > zcr_blksz_max)
4863 blksz = zcr_blksz_max;
4864 /* avoid potential complexity of dealing with it */
4865 if (blksz > max_blksz) {
4867 return (SET_ERROR(EINVAL));
4870 maxsize = zp->z_size - uio->uio_loffset;
4876 return (SET_ERROR(EINVAL));
4881 return (SET_ERROR(EINVAL));
4884 uio->uio_extflg = UIO_XUIO;
4885 XUIO_XUZC_RW(xuio) = ioflag;
4892 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4896 int ioflag = XUIO_XUZC_RW(xuio);
4898 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4900 i = dmu_xuio_cnt(xuio);
4902 abuf = dmu_xuio_arcbuf(xuio, i);
4904 * if abuf == NULL, it must be a write buffer
4905 * that has been returned in zfs_write().
4908 dmu_return_arcbuf(abuf);
4909 ASSERT(abuf || ioflag == UIO_WRITE);
4912 dmu_xuio_fini(xuio);
4915 #endif /* HAVE_UIO_ZEROCOPY */
4917 #if defined(_KERNEL) && defined(HAVE_SPL)
4918 EXPORT_SYMBOL(zfs_open);
4919 EXPORT_SYMBOL(zfs_close);
4920 EXPORT_SYMBOL(zfs_read);
4921 EXPORT_SYMBOL(zfs_write);
4922 EXPORT_SYMBOL(zfs_access);
4923 EXPORT_SYMBOL(zfs_lookup);
4924 EXPORT_SYMBOL(zfs_create);
4925 EXPORT_SYMBOL(zfs_tmpfile);
4926 EXPORT_SYMBOL(zfs_remove);
4927 EXPORT_SYMBOL(zfs_mkdir);
4928 EXPORT_SYMBOL(zfs_rmdir);
4929 EXPORT_SYMBOL(zfs_readdir);
4930 EXPORT_SYMBOL(zfs_fsync);
4931 EXPORT_SYMBOL(zfs_getattr);
4932 EXPORT_SYMBOL(zfs_getattr_fast);
4933 EXPORT_SYMBOL(zfs_setattr);
4934 EXPORT_SYMBOL(zfs_rename);
4935 EXPORT_SYMBOL(zfs_symlink);
4936 EXPORT_SYMBOL(zfs_readlink);
4937 EXPORT_SYMBOL(zfs_link);
4938 EXPORT_SYMBOL(zfs_inactive);
4939 EXPORT_SYMBOL(zfs_space);
4940 EXPORT_SYMBOL(zfs_fid);
4941 EXPORT_SYMBOL(zfs_getsecattr);
4942 EXPORT_SYMBOL(zfs_setsecattr);
4943 EXPORT_SYMBOL(zfs_getpage);
4944 EXPORT_SYMBOL(zfs_putpage);
4945 EXPORT_SYMBOL(zfs_dirty_inode);
4946 EXPORT_SYMBOL(zfs_map);
4949 module_param(zfs_delete_blocks, ulong, 0644);
4950 MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async");
4951 module_param(zfs_read_chunk_size, long, 0644);
4952 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");