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>
85 * Each vnode op performs some logical unit of work. To do this, the ZPL must
86 * properly lock its in-core state, create a DMU transaction, do the work,
87 * record this work in the intent log (ZIL), commit the DMU transaction,
88 * and wait for the intent log to commit if it is a synchronous operation.
89 * Moreover, the vnode ops must work in both normal and log replay context.
90 * The ordering of events is important to avoid deadlocks and references
91 * to freed memory. The example below illustrates the following Big Rules:
93 * (1) A check must be made in each zfs thread for a mounted file system.
94 * This is done avoiding races using ZFS_ENTER(zfsvfs).
95 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
96 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
97 * can return EIO from the calling function.
99 * (2) iput() should always be the last thing except for zil_commit()
100 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
101 * First, if it's the last reference, the vnode/znode
102 * can be freed, so the zp may point to freed memory. Second, the last
103 * reference will call zfs_zinactive(), which may induce a lot of work --
104 * pushing cached pages (which acquires range locks) and syncing out
105 * cached atime changes. Third, zfs_zinactive() may require a new tx,
106 * which could deadlock the system if you were already holding one.
107 * If you must call iput() within a tx then use zfs_iput_async().
109 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
110 * as they can span dmu_tx_assign() calls.
112 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
113 * dmu_tx_assign(). This is critical because we don't want to block
114 * while holding locks.
116 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
117 * reduces lock contention and CPU usage when we must wait (note that if
118 * throughput is constrained by the storage, nearly every transaction
121 * Note, in particular, that if a lock is sometimes acquired before
122 * the tx assigns, and sometimes after (e.g. z_lock), then failing
123 * to use a non-blocking assign can deadlock the system. The scenario:
125 * Thread A has grabbed a lock before calling dmu_tx_assign().
126 * Thread B is in an already-assigned tx, and blocks for this lock.
127 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
128 * forever, because the previous txg can't quiesce until B's tx commits.
130 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
131 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
132 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
133 * to indicate that this operation has already called dmu_tx_wait().
134 * This will ensure that we don't retry forever, waiting a short bit
137 * (5) If the operation succeeded, generate the intent log entry for it
138 * before dropping locks. This ensures that the ordering of events
139 * in the intent log matches the order in which they actually occurred.
140 * During ZIL replay the zfs_log_* functions will update the sequence
141 * number to indicate the zil transaction has replayed.
143 * (6) At the end of each vnode op, the DMU tx must always commit,
144 * regardless of whether there were any errors.
146 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
147 * to ensure that synchronous semantics are provided when necessary.
149 * In general, this is how things should be ordered in each vnode op:
151 * ZFS_ENTER(zfsvfs); // exit if unmounted
153 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
154 * rw_enter(...); // grab any other locks you need
155 * tx = dmu_tx_create(...); // get DMU tx
156 * dmu_tx_hold_*(); // hold each object you might modify
157 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
159 * rw_exit(...); // drop locks
160 * zfs_dirent_unlock(dl); // unlock directory entry
161 * iput(...); // release held vnodes
162 * if (error == ERESTART) {
168 * dmu_tx_abort(tx); // abort DMU tx
169 * ZFS_EXIT(zfsvfs); // finished in zfs
170 * return (error); // really out of space
172 * error = do_real_work(); // do whatever this VOP does
174 * zfs_log_*(...); // on success, make ZIL entry
175 * dmu_tx_commit(tx); // commit DMU tx -- error or not
176 * rw_exit(...); // drop locks
177 * zfs_dirent_unlock(dl); // unlock directory entry
178 * iput(...); // release held vnodes
179 * zil_commit(zilog, foid); // synchronous when necessary
180 * ZFS_EXIT(zfsvfs); // finished in zfs
181 * return (error); // done, report error
185 * Virus scanning is unsupported. It would be possible to add a hook
186 * here to performance the required virus scan. This could be done
187 * entirely in the kernel or potentially as an update to invoke a
191 zfs_vscan(struct inode *ip, cred_t *cr, int async)
198 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
200 znode_t *zp = ITOZ(ip);
201 zfsvfs_t *zfsvfs = ITOZSB(ip);
206 /* Honor ZFS_APPENDONLY file attribute */
207 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
208 ((flag & O_APPEND) == 0)) {
210 return (SET_ERROR(EPERM));
213 /* Virus scan eligible files on open */
214 if (!zfs_has_ctldir(zp) && zfsvfs->z_vscan && S_ISREG(ip->i_mode) &&
215 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
216 if (zfs_vscan(ip, cr, 0) != 0) {
218 return (SET_ERROR(EACCES));
222 /* Keep a count of the synchronous opens in the znode */
224 atomic_inc_32(&zp->z_sync_cnt);
232 zfs_close(struct inode *ip, int flag, cred_t *cr)
234 znode_t *zp = ITOZ(ip);
235 zfsvfs_t *zfsvfs = ITOZSB(ip);
240 /* Decrement the synchronous opens in the znode */
242 atomic_dec_32(&zp->z_sync_cnt);
244 if (!zfs_has_ctldir(zp) && zfsvfs->z_vscan && S_ISREG(ip->i_mode) &&
245 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
246 VERIFY(zfs_vscan(ip, cr, 1) == 0);
252 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
254 * Lseek support for finding holes (cmd == SEEK_HOLE) and
255 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
258 zfs_holey_common(struct inode *ip, int cmd, loff_t *off)
260 znode_t *zp = ITOZ(ip);
261 uint64_t noff = (uint64_t)*off; /* new offset */
266 file_sz = zp->z_size;
267 if (noff >= file_sz) {
268 return (SET_ERROR(ENXIO));
271 if (cmd == SEEK_HOLE)
276 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
279 return (SET_ERROR(ENXIO));
281 /* file was dirty, so fall back to using generic logic */
282 if (error == EBUSY) {
290 * We could find a hole that begins after the logical end-of-file,
291 * because dmu_offset_next() only works on whole blocks. If the
292 * EOF falls mid-block, then indicate that the "virtual hole"
293 * at the end of the file begins at the logical EOF, rather than
294 * at the end of the last block.
296 if (noff > file_sz) {
308 zfs_holey(struct inode *ip, int cmd, loff_t *off)
310 znode_t *zp = ITOZ(ip);
311 zfsvfs_t *zfsvfs = ITOZSB(ip);
317 error = zfs_holey_common(ip, cmd, off);
322 #endif /* SEEK_HOLE && SEEK_DATA */
326 * When a file is memory mapped, we must keep the IO data synchronized
327 * between the DMU cache and the memory mapped pages. What this means:
329 * On Write: If we find a memory mapped page, we write to *both*
330 * the page and the dmu buffer.
333 update_pages(struct inode *ip, int64_t start, int len,
334 objset_t *os, uint64_t oid)
336 struct address_space *mp = ip->i_mapping;
342 off = start & (PAGE_SIZE-1);
343 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
344 nbytes = MIN(PAGE_SIZE - off, len);
346 pp = find_lock_page(mp, start >> PAGE_SHIFT);
348 if (mapping_writably_mapped(mp))
349 flush_dcache_page(pp);
352 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
356 if (mapping_writably_mapped(mp))
357 flush_dcache_page(pp);
359 mark_page_accessed(pp);
372 * When a file is memory mapped, we must keep the IO data synchronized
373 * between the DMU cache and the memory mapped pages. What this means:
375 * On Read: We "read" preferentially from memory mapped pages,
376 * else we default from the dmu buffer.
378 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
379 * the file is memory mapped.
382 mappedread(struct inode *ip, int nbytes, uio_t *uio)
384 struct address_space *mp = ip->i_mapping;
386 znode_t *zp = ITOZ(ip);
393 start = uio->uio_loffset;
394 off = start & (PAGE_SIZE-1);
395 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) {
396 bytes = MIN(PAGE_SIZE - off, len);
398 pp = find_lock_page(mp, start >> PAGE_SHIFT);
400 ASSERT(PageUptodate(pp));
403 error = uiomove(pb + off, bytes, UIO_READ, uio);
406 if (mapping_writably_mapped(mp))
407 flush_dcache_page(pp);
409 mark_page_accessed(pp);
413 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
426 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
427 unsigned long zfs_delete_blocks = DMU_MAX_DELETEBLKCNT;
430 * Read bytes from specified file into supplied buffer.
432 * IN: ip - inode of file to be read from.
433 * uio - structure supplying read location, range info,
435 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
436 * O_DIRECT flag; used to bypass page cache.
437 * cr - credentials of caller.
439 * OUT: uio - updated offset and range, buffer filled.
441 * RETURN: 0 on success, error code on failure.
444 * inode - atime updated if byte count > 0
448 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
450 znode_t *zp = ITOZ(ip);
451 zfsvfs_t *zfsvfs = ITOZSB(ip);
455 #ifdef HAVE_UIO_ZEROCOPY
457 #endif /* HAVE_UIO_ZEROCOPY */
462 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
464 return (SET_ERROR(EACCES));
468 * Validate file offset
470 if (uio->uio_loffset < (offset_t)0) {
472 return (SET_ERROR(EINVAL));
476 * Fasttrack empty reads
478 if (uio->uio_resid == 0) {
484 * If we're in FRSYNC mode, sync out this znode before reading it.
485 * Only do this for non-snapshots.
488 (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS))
489 zil_commit(zfsvfs->z_log, zp->z_id);
492 * Lock the range against changes.
494 rl = zfs_range_lock(&zp->z_range_lock, uio->uio_loffset, uio->uio_resid,
498 * If we are reading past end-of-file we can skip
499 * to the end; but we might still need to set atime.
501 if (uio->uio_loffset >= zp->z_size) {
506 ASSERT(uio->uio_loffset < zp->z_size);
507 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
509 #ifdef HAVE_UIO_ZEROCOPY
510 if ((uio->uio_extflg == UIO_XUIO) &&
511 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
513 int blksz = zp->z_blksz;
514 uint64_t offset = uio->uio_loffset;
516 xuio = (xuio_t *)uio;
518 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
521 ASSERT(offset + n <= blksz);
524 (void) dmu_xuio_init(xuio, nblk);
526 if (vn_has_cached_data(ip)) {
528 * For simplicity, we always allocate a full buffer
529 * even if we only expect to read a portion of a block.
531 while (--nblk >= 0) {
532 (void) dmu_xuio_add(xuio,
533 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
538 #endif /* HAVE_UIO_ZEROCOPY */
541 nbytes = MIN(n, zfs_read_chunk_size -
542 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
544 if (zp->z_is_mapped && !(ioflag & O_DIRECT)) {
545 error = mappedread(ip, nbytes, uio);
547 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
552 /* convert checksum errors into IO errors */
554 error = SET_ERROR(EIO);
561 zfs_range_unlock(rl);
568 * Write the bytes to a file.
570 * IN: ip - inode of file to be written to.
571 * uio - structure supplying write location, range info,
573 * ioflag - FAPPEND flag set if in append mode.
574 * O_DIRECT flag; used to bypass page cache.
575 * cr - credentials of caller.
577 * OUT: uio - updated offset and range.
579 * RETURN: 0 if success
580 * error code if failure
583 * ip - ctime|mtime updated if byte count > 0
588 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
590 znode_t *zp = ITOZ(ip);
591 rlim64_t limit = uio->uio_limit;
592 ssize_t start_resid = uio->uio_resid;
596 zfsvfs_t *zfsvfs = ZTOZSB(zp);
601 int max_blksz = zfsvfs->z_max_blksz;
604 const iovec_t *aiov = NULL;
608 sa_bulk_attr_t bulk[4];
609 uint64_t mtime[2], ctime[2];
611 #ifdef HAVE_UIO_ZEROCOPY
613 const iovec_t *iovp = uio->uio_iov;
614 ASSERTV(int iovcnt = uio->uio_iovcnt);
618 * Fasttrack empty write
624 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
630 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
631 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
632 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
634 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
638 * Callers might not be able to detect properly that we are read-only,
639 * so check it explicitly here.
641 if (zfs_is_readonly(zfsvfs)) {
643 return (SET_ERROR(EROFS));
647 * If immutable or not appending then return EPERM
649 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
650 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
651 (uio->uio_loffset < zp->z_size))) {
653 return (SET_ERROR(EPERM));
656 zilog = zfsvfs->z_log;
659 * Validate file offset
661 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
664 return (SET_ERROR(EINVAL));
668 * Pre-fault the pages to ensure slow (eg NFS) pages
670 * Skip this if uio contains loaned arc_buf.
672 #ifdef HAVE_UIO_ZEROCOPY
673 if ((uio->uio_extflg == UIO_XUIO) &&
674 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
675 xuio = (xuio_t *)uio;
678 uio_prefaultpages(MIN(n, max_blksz), uio);
681 * If in append mode, set the io offset pointer to eof.
683 if (ioflag & FAPPEND) {
685 * Obtain an appending range lock to guarantee file append
686 * semantics. We reset the write offset once we have the lock.
688 rl = zfs_range_lock(&zp->z_range_lock, 0, n, RL_APPEND);
690 if (rl->r_len == UINT64_MAX) {
692 * We overlocked the file because this write will cause
693 * the file block size to increase.
694 * Note that zp_size cannot change with this lock held.
698 uio->uio_loffset = woff;
701 * Note that if the file block size will change as a result of
702 * this write, then this range lock will lock the entire file
703 * so that we can re-write the block safely.
705 rl = zfs_range_lock(&zp->z_range_lock, woff, n, RL_WRITER);
709 zfs_range_unlock(rl);
711 return (SET_ERROR(EFBIG));
714 if ((woff + n) > limit || woff > (limit - n))
717 /* Will this write extend the file length? */
718 write_eof = (woff + n > zp->z_size);
720 end_size = MAX(zp->z_size, woff + n);
723 * Write the file in reasonable size chunks. Each chunk is written
724 * in a separate transaction; this keeps the intent log records small
725 * and allows us to do more fine-grained space accounting.
729 woff = uio->uio_loffset;
730 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
731 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
733 dmu_return_arcbuf(abuf);
734 error = SET_ERROR(EDQUOT);
738 if (xuio && abuf == NULL) {
739 #ifdef HAVE_UIO_ZEROCOPY
740 ASSERT(i_iov < iovcnt);
741 ASSERT3U(uio->uio_segflg, !=, UIO_BVEC);
743 abuf = dmu_xuio_arcbuf(xuio, i_iov);
744 dmu_xuio_clear(xuio, i_iov);
745 ASSERT((aiov->iov_base == abuf->b_data) ||
746 ((char *)aiov->iov_base - (char *)abuf->b_data +
747 aiov->iov_len == arc_buf_size(abuf)));
750 } else if (abuf == NULL && n >= max_blksz &&
751 woff >= zp->z_size &&
752 P2PHASE(woff, max_blksz) == 0 &&
753 zp->z_blksz == max_blksz) {
755 * This write covers a full block. "Borrow" a buffer
756 * from the dmu so that we can fill it before we enter
757 * a transaction. This avoids the possibility of
758 * holding up the transaction if the data copy hangs
759 * up on a pagefault (e.g., from an NFS server mapping).
763 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
765 ASSERT(abuf != NULL);
766 ASSERT(arc_buf_size(abuf) == max_blksz);
767 if ((error = uiocopy(abuf->b_data, max_blksz,
768 UIO_WRITE, uio, &cbytes))) {
769 dmu_return_arcbuf(abuf);
772 ASSERT(cbytes == max_blksz);
776 * Start a transaction.
778 tx = dmu_tx_create(zfsvfs->z_os);
779 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
780 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
781 zfs_sa_upgrade_txholds(tx, zp);
782 error = dmu_tx_assign(tx, TXG_WAIT);
786 dmu_return_arcbuf(abuf);
791 * If zfs_range_lock() over-locked we grow the blocksize
792 * and then reduce the lock range. This will only happen
793 * on the first iteration since zfs_range_reduce() will
794 * shrink down r_len to the appropriate size.
796 if (rl->r_len == UINT64_MAX) {
799 if (zp->z_blksz > max_blksz) {
801 * File's blocksize is already larger than the
802 * "recordsize" property. Only let it grow to
803 * the next power of 2.
805 ASSERT(!ISP2(zp->z_blksz));
806 new_blksz = MIN(end_size,
807 1 << highbit64(zp->z_blksz));
809 new_blksz = MIN(end_size, max_blksz);
811 zfs_grow_blocksize(zp, new_blksz, tx);
812 zfs_range_reduce(rl, woff, n);
816 * XXX - should we really limit each write to z_max_blksz?
817 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
819 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
822 tx_bytes = uio->uio_resid;
823 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
825 tx_bytes -= uio->uio_resid;
828 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
830 * If this is not a full block write, but we are
831 * extending the file past EOF and this data starts
832 * block-aligned, use assign_arcbuf(). Otherwise,
833 * write via dmu_write().
835 if (tx_bytes < max_blksz && (!write_eof ||
836 aiov->iov_base != abuf->b_data)) {
838 dmu_write(zfsvfs->z_os, zp->z_id, woff,
839 aiov->iov_len, aiov->iov_base, tx);
840 dmu_return_arcbuf(abuf);
841 xuio_stat_wbuf_copied();
843 ASSERT(xuio || tx_bytes == max_blksz);
844 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
847 ASSERT(tx_bytes <= uio->uio_resid);
848 uioskip(uio, tx_bytes);
850 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT)) {
851 update_pages(ip, woff,
852 tx_bytes, zfsvfs->z_os, zp->z_id);
856 * If we made no progress, we're done. If we made even
857 * partial progress, update the znode and ZIL accordingly.
860 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
861 (void *)&zp->z_size, sizeof (uint64_t), tx);
868 * Clear Set-UID/Set-GID bits on successful write if not
869 * privileged and at least one of the execute bits is set.
871 * It would be nice to to this after all writes have
872 * been done, but that would still expose the ISUID/ISGID
873 * to another app after the partial write is committed.
875 * Note: we don't call zfs_fuid_map_id() here because
876 * user 0 is not an ephemeral uid.
878 mutex_enter(&zp->z_acl_lock);
879 uid = KUID_TO_SUID(ip->i_uid);
880 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
881 (S_IXUSR >> 6))) != 0 &&
882 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
883 secpolicy_vnode_setid_retain(cr,
884 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) {
886 zp->z_mode &= ~(S_ISUID | S_ISGID);
887 ip->i_mode = newmode = zp->z_mode;
888 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
889 (void *)&newmode, sizeof (uint64_t), tx);
891 mutex_exit(&zp->z_acl_lock);
893 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);
896 * Update the file size (zp_size) if it has changed;
897 * account for possible concurrent updates.
899 while ((end_size = zp->z_size) < uio->uio_loffset) {
900 (void) atomic_cas_64(&zp->z_size, end_size,
905 * If we are replaying and eof is non zero then force
906 * the file size to the specified eof. Note, there's no
907 * concurrency during replay.
909 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
910 zp->z_size = zfsvfs->z_replay_eof;
912 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
914 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
920 ASSERT(tx_bytes == nbytes);
924 uio_prefaultpages(MIN(n, max_blksz), uio);
927 zfs_inode_update(zp);
928 zfs_range_unlock(rl);
931 * If we're in replay mode, or we made no progress, return error.
932 * Otherwise, it's at least a partial write, so it's successful.
934 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
939 if (ioflag & (FSYNC | FDSYNC) ||
940 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
941 zil_commit(zilog, zp->z_id);
948 * Drop a reference on the passed inode asynchronously. This ensures
949 * that the caller will never drop the last reference on an inode in
950 * the current context. Doing so while holding open a tx could result
951 * in a deadlock if iput_final() re-enters the filesystem code.
954 zfs_iput_async(struct inode *ip)
956 objset_t *os = ITOZSB(ip)->z_os;
958 ASSERT(atomic_read(&ip->i_count) > 0);
961 if (atomic_read(&ip->i_count) == 1)
962 VERIFY(taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os)),
963 (task_func_t *)iput, ip, TQ_SLEEP) != TASKQID_INVALID);
969 zfs_get_done(zgd_t *zgd, int error)
971 znode_t *zp = zgd->zgd_private;
974 dmu_buf_rele(zgd->zgd_db, zgd);
976 zfs_range_unlock(zgd->zgd_rl);
979 * Release the vnode asynchronously as we currently have the
980 * txg stopped from syncing.
982 zfs_iput_async(ZTOI(zp));
984 if (error == 0 && zgd->zgd_bp)
985 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
987 kmem_free(zgd, sizeof (zgd_t));
991 static int zil_fault_io = 0;
995 * Get data to generate a TX_WRITE intent log record.
998 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1000 zfsvfs_t *zfsvfs = arg;
1001 objset_t *os = zfsvfs->z_os;
1003 uint64_t object = lr->lr_foid;
1004 uint64_t offset = lr->lr_offset;
1005 uint64_t size = lr->lr_length;
1010 ASSERT(zio != NULL);
1014 * Nothing to do if the file has been removed
1016 if (zfs_zget(zfsvfs, object, &zp) != 0)
1017 return (SET_ERROR(ENOENT));
1018 if (zp->z_unlinked) {
1020 * Release the vnode asynchronously as we currently have the
1021 * txg stopped from syncing.
1023 zfs_iput_async(ZTOI(zp));
1024 return (SET_ERROR(ENOENT));
1027 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1028 zgd->zgd_zilog = zfsvfs->z_log;
1029 zgd->zgd_private = zp;
1032 * Write records come in two flavors: immediate and indirect.
1033 * For small writes it's cheaper to store the data with the
1034 * log record (immediate); for large writes it's cheaper to
1035 * sync the data and get a pointer to it (indirect) so that
1036 * we don't have to write the data twice.
1038 if (buf != NULL) { /* immediate write */
1039 zgd->zgd_rl = zfs_range_lock(&zp->z_range_lock, offset, size,
1041 /* test for truncation needs to be done while range locked */
1042 if (offset >= zp->z_size) {
1043 error = SET_ERROR(ENOENT);
1045 error = dmu_read(os, object, offset, size, buf,
1046 DMU_READ_NO_PREFETCH);
1048 ASSERT(error == 0 || error == ENOENT);
1049 } else { /* indirect write */
1051 * Have to lock the whole block to ensure when it's
1052 * written out and its checksum is being calculated
1053 * that no one can change the data. We need to re-check
1054 * blocksize after we get the lock in case it's changed!
1059 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1061 zgd->zgd_rl = zfs_range_lock(&zp->z_range_lock, offset,
1063 if (zp->z_blksz == size)
1066 zfs_range_unlock(zgd->zgd_rl);
1068 /* test for truncation needs to be done while range locked */
1069 if (lr->lr_offset >= zp->z_size)
1070 error = SET_ERROR(ENOENT);
1073 error = SET_ERROR(EIO);
1078 error = dmu_buf_hold(os, object, offset, zgd, &db,
1079 DMU_READ_NO_PREFETCH);
1082 blkptr_t *bp = &lr->lr_blkptr;
1087 ASSERT(db->db_offset == offset);
1088 ASSERT(db->db_size == size);
1090 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1092 ASSERT(error || lr->lr_length <= size);
1095 * On success, we need to wait for the write I/O
1096 * initiated by dmu_sync() to complete before we can
1097 * release this dbuf. We will finish everything up
1098 * in the zfs_get_done() callback.
1103 if (error == EALREADY) {
1104 lr->lr_common.lrc_txtype = TX_WRITE2;
1110 zfs_get_done(zgd, error);
1117 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1119 znode_t *zp = ITOZ(ip);
1120 zfsvfs_t *zfsvfs = ITOZSB(ip);
1126 if (flag & V_ACE_MASK)
1127 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1129 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1136 * Lookup an entry in a directory, or an extended attribute directory.
1137 * If it exists, return a held inode reference for it.
1139 * IN: dip - inode of directory to search.
1140 * nm - name of entry to lookup.
1141 * flags - LOOKUP_XATTR set if looking for an attribute.
1142 * cr - credentials of caller.
1143 * direntflags - directory lookup flags
1144 * realpnp - returned pathname.
1146 * OUT: ipp - inode of located entry, NULL if not found.
1148 * RETURN: 0 on success, error code on failure.
1155 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1156 cred_t *cr, int *direntflags, pathname_t *realpnp)
1158 znode_t *zdp = ITOZ(dip);
1159 zfsvfs_t *zfsvfs = ITOZSB(dip);
1163 * Fast path lookup, however we must skip DNLC lookup
1164 * for case folding or normalizing lookups because the
1165 * DNLC code only stores the passed in name. This means
1166 * creating 'a' and removing 'A' on a case insensitive
1167 * file system would work, but DNLC still thinks 'a'
1168 * exists and won't let you create it again on the next
1169 * pass through fast path.
1171 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1173 if (!S_ISDIR(dip->i_mode)) {
1174 return (SET_ERROR(ENOTDIR));
1175 } else if (zdp->z_sa_hdl == NULL) {
1176 return (SET_ERROR(EIO));
1179 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1180 error = zfs_fastaccesschk_execute(zdp, cr);
1188 } else if (!zdp->z_zfsvfs->z_norm &&
1189 (zdp->z_zfsvfs->z_case == ZFS_CASE_SENSITIVE)) {
1191 vnode_t *tvp = dnlc_lookup(dvp, nm);
1194 error = zfs_fastaccesschk_execute(zdp, cr);
1199 if (tvp == DNLC_NO_VNODE) {
1201 return (SET_ERROR(ENOENT));
1204 return (specvp_check(vpp, cr));
1207 #endif /* HAVE_DNLC */
1216 if (flags & LOOKUP_XATTR) {
1218 * We don't allow recursive attributes..
1219 * Maybe someday we will.
1221 if (zdp->z_pflags & ZFS_XATTR) {
1223 return (SET_ERROR(EINVAL));
1226 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1232 * Do we have permission to get into attribute directory?
1235 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1245 if (!S_ISDIR(dip->i_mode)) {
1247 return (SET_ERROR(ENOTDIR));
1251 * Check accessibility of directory.
1254 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1259 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1260 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1262 return (SET_ERROR(EILSEQ));
1265 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1266 if ((error == 0) && (*ipp))
1267 zfs_inode_update(ITOZ(*ipp));
1274 * Attempt to create a new entry in a directory. If the entry
1275 * already exists, truncate the file if permissible, else return
1276 * an error. Return the ip of the created or trunc'd file.
1278 * IN: dip - inode of directory to put new file entry in.
1279 * name - name of new file entry.
1280 * vap - attributes of new file.
1281 * excl - flag indicating exclusive or non-exclusive mode.
1282 * mode - mode to open file with.
1283 * cr - credentials of caller.
1284 * flag - large file flag [UNUSED].
1285 * vsecp - ACL to be set
1287 * OUT: ipp - inode of created or trunc'd entry.
1289 * RETURN: 0 on success, error code on failure.
1292 * dip - ctime|mtime updated if new entry created
1293 * ip - ctime|mtime always, atime if new
1298 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1299 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1301 znode_t *zp, *dzp = ITOZ(dip);
1302 zfsvfs_t *zfsvfs = ITOZSB(dip);
1310 zfs_acl_ids_t acl_ids;
1311 boolean_t fuid_dirtied;
1312 boolean_t have_acl = B_FALSE;
1313 boolean_t waited = B_FALSE;
1316 * If we have an ephemeral id, ACL, or XVATTR then
1317 * make sure file system is at proper version
1323 if (zfsvfs->z_use_fuids == B_FALSE &&
1324 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1325 return (SET_ERROR(EINVAL));
1328 return (SET_ERROR(EINVAL));
1333 zilog = zfsvfs->z_log;
1335 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1336 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1338 return (SET_ERROR(EILSEQ));
1341 if (vap->va_mask & ATTR_XVATTR) {
1342 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1343 crgetuid(cr), cr, vap->va_mode)) != 0) {
1351 if (*name == '\0') {
1353 * Null component name refers to the directory itself.
1360 /* possible igrab(zp) */
1363 if (flag & FIGNORECASE)
1366 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1370 zfs_acl_ids_free(&acl_ids);
1371 if (strcmp(name, "..") == 0)
1372 error = SET_ERROR(EISDIR);
1382 * Create a new file object and update the directory
1385 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1387 zfs_acl_ids_free(&acl_ids);
1392 * We only support the creation of regular files in
1393 * extended attribute directories.
1396 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1398 zfs_acl_ids_free(&acl_ids);
1399 error = SET_ERROR(EINVAL);
1403 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1404 cr, vsecp, &acl_ids)) != 0)
1408 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1409 zfs_acl_ids_free(&acl_ids);
1410 error = SET_ERROR(EDQUOT);
1414 tx = dmu_tx_create(os);
1416 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1417 ZFS_SA_BASE_ATTR_SIZE);
1419 fuid_dirtied = zfsvfs->z_fuid_dirty;
1421 zfs_fuid_txhold(zfsvfs, tx);
1422 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1423 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1424 if (!zfsvfs->z_use_sa &&
1425 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1426 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1427 0, acl_ids.z_aclp->z_acl_bytes);
1429 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1431 zfs_dirent_unlock(dl);
1432 if (error == ERESTART) {
1438 zfs_acl_ids_free(&acl_ids);
1443 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1446 zfs_fuid_sync(zfsvfs, tx);
1448 (void) zfs_link_create(dl, zp, tx, ZNEW);
1449 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1450 if (flag & FIGNORECASE)
1452 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1453 vsecp, acl_ids.z_fuidp, vap);
1454 zfs_acl_ids_free(&acl_ids);
1457 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1460 zfs_acl_ids_free(&acl_ids);
1464 * A directory entry already exists for this name.
1467 * Can't truncate an existing file if in exclusive mode.
1470 error = SET_ERROR(EEXIST);
1474 * Can't open a directory for writing.
1476 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1477 error = SET_ERROR(EISDIR);
1481 * Verify requested access to file.
1483 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1487 mutex_enter(&dzp->z_lock);
1489 mutex_exit(&dzp->z_lock);
1492 * Truncate regular files if requested.
1494 if (S_ISREG(ZTOI(zp)->i_mode) &&
1495 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1496 /* we can't hold any locks when calling zfs_freesp() */
1498 zfs_dirent_unlock(dl);
1501 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1507 zfs_dirent_unlock(dl);
1513 zfs_inode_update(dzp);
1514 zfs_inode_update(zp);
1518 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1519 zil_commit(zilog, 0);
1527 zfs_tmpfile(struct inode *dip, vattr_t *vap, int excl,
1528 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1530 znode_t *zp = NULL, *dzp = ITOZ(dip);
1531 zfsvfs_t *zfsvfs = ITOZSB(dip);
1537 zfs_acl_ids_t acl_ids;
1538 boolean_t fuid_dirtied;
1539 boolean_t have_acl = B_FALSE;
1540 boolean_t waited = B_FALSE;
1543 * If we have an ephemeral id, ACL, or XVATTR then
1544 * make sure file system is at proper version
1550 if (zfsvfs->z_use_fuids == B_FALSE &&
1551 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1552 return (SET_ERROR(EINVAL));
1558 if (vap->va_mask & ATTR_XVATTR) {
1559 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1560 crgetuid(cr), cr, vap->va_mode)) != 0) {
1570 * Create a new file object and update the directory
1573 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1575 zfs_acl_ids_free(&acl_ids);
1579 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1580 cr, vsecp, &acl_ids)) != 0)
1584 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1585 zfs_acl_ids_free(&acl_ids);
1586 error = SET_ERROR(EDQUOT);
1590 tx = dmu_tx_create(os);
1592 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1593 ZFS_SA_BASE_ATTR_SIZE);
1594 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1596 fuid_dirtied = zfsvfs->z_fuid_dirty;
1598 zfs_fuid_txhold(zfsvfs, tx);
1599 if (!zfsvfs->z_use_sa &&
1600 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1601 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1602 0, acl_ids.z_aclp->z_acl_bytes);
1604 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1606 if (error == ERESTART) {
1612 zfs_acl_ids_free(&acl_ids);
1617 zfs_mknode(dzp, vap, tx, cr, IS_TMPFILE, &zp, &acl_ids);
1620 zfs_fuid_sync(zfsvfs, tx);
1622 /* Add to unlinked set */
1624 zfs_unlinked_add(zp, tx);
1625 zfs_acl_ids_free(&acl_ids);
1633 zfs_inode_update(dzp);
1634 zfs_inode_update(zp);
1643 * Remove an entry from a directory.
1645 * IN: dip - inode of directory to remove entry from.
1646 * name - name of entry to remove.
1647 * cr - credentials of caller.
1649 * RETURN: 0 if success
1650 * error code if failure
1654 * ip - ctime (if nlink > 0)
1657 uint64_t null_xattr = 0;
1661 zfs_remove(struct inode *dip, char *name, cred_t *cr, int flags)
1663 znode_t *zp, *dzp = ITOZ(dip);
1666 zfsvfs_t *zfsvfs = ITOZSB(dip);
1668 uint64_t acl_obj, xattr_obj;
1669 uint64_t xattr_obj_unlinked = 0;
1674 boolean_t may_delete_now, delete_now = FALSE;
1675 boolean_t unlinked, toobig = FALSE;
1677 pathname_t *realnmp = NULL;
1681 boolean_t waited = B_FALSE;
1684 return (SET_ERROR(EINVAL));
1688 zilog = zfsvfs->z_log;
1690 if (flags & FIGNORECASE) {
1700 * Attempt to lock directory; fail if entry doesn't exist.
1702 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1712 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1717 * Need to use rmdir for removing directories.
1719 if (S_ISDIR(ip->i_mode)) {
1720 error = SET_ERROR(EPERM);
1726 dnlc_remove(dvp, realnmp->pn_buf);
1728 dnlc_remove(dvp, name);
1729 #endif /* HAVE_DNLC */
1731 mutex_enter(&zp->z_lock);
1732 may_delete_now = atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped);
1733 mutex_exit(&zp->z_lock);
1736 * We may delete the znode now, or we may put it in the unlinked set;
1737 * it depends on whether we're the last link, and on whether there are
1738 * other holds on the inode. So we dmu_tx_hold() the right things to
1739 * allow for either case.
1742 tx = dmu_tx_create(zfsvfs->z_os);
1743 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1744 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1745 zfs_sa_upgrade_txholds(tx, zp);
1746 zfs_sa_upgrade_txholds(tx, dzp);
1747 if (may_delete_now) {
1748 toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks;
1749 /* if the file is too big, only hold_free a token amount */
1750 dmu_tx_hold_free(tx, zp->z_id, 0,
1751 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1754 /* are there any extended attributes? */
1755 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1756 &xattr_obj, sizeof (xattr_obj));
1757 if (error == 0 && xattr_obj) {
1758 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1760 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1761 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1764 mutex_enter(&zp->z_lock);
1765 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1766 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1767 mutex_exit(&zp->z_lock);
1769 /* charge as an update -- would be nice not to charge at all */
1770 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1773 * Mark this transaction as typically resulting in a net free of space
1775 dmu_tx_mark_netfree(tx);
1777 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1779 zfs_dirent_unlock(dl);
1780 if (error == ERESTART) {
1800 * Remove the directory entry.
1802 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1811 * Hold z_lock so that we can make sure that the ACL obj
1812 * hasn't changed. Could have been deleted due to
1815 mutex_enter(&zp->z_lock);
1816 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1817 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1818 delete_now = may_delete_now && !toobig &&
1819 atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped) &&
1820 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1825 if (xattr_obj_unlinked) {
1826 ASSERT3U(ZTOI(xzp)->i_nlink, ==, 2);
1827 mutex_enter(&xzp->z_lock);
1828 xzp->z_unlinked = 1;
1829 clear_nlink(ZTOI(xzp));
1831 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1832 &links, sizeof (links), tx);
1833 ASSERT3U(error, ==, 0);
1834 mutex_exit(&xzp->z_lock);
1835 zfs_unlinked_add(xzp, tx);
1838 error = sa_remove(zp->z_sa_hdl,
1839 SA_ZPL_XATTR(zfsvfs), tx);
1841 error = sa_update(zp->z_sa_hdl,
1842 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1843 sizeof (uint64_t), tx);
1847 * Add to the unlinked set because a new reference could be
1848 * taken concurrently resulting in a deferred destruction.
1850 zfs_unlinked_add(zp, tx);
1851 mutex_exit(&zp->z_lock);
1852 } else if (unlinked) {
1853 mutex_exit(&zp->z_lock);
1854 zfs_unlinked_add(zp, tx);
1858 if (flags & FIGNORECASE)
1860 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1867 zfs_dirent_unlock(dl);
1868 zfs_inode_update(dzp);
1869 zfs_inode_update(zp);
1877 zfs_inode_update(xzp);
1878 zfs_iput_async(ZTOI(xzp));
1881 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1882 zil_commit(zilog, 0);
1889 * Create a new directory and insert it into dip using the name
1890 * provided. Return a pointer to the inserted directory.
1892 * IN: dip - inode of directory to add subdir to.
1893 * dirname - name of new directory.
1894 * vap - attributes of new directory.
1895 * cr - credentials of caller.
1896 * vsecp - ACL to be set
1898 * OUT: ipp - inode of created directory.
1900 * RETURN: 0 if success
1901 * error code if failure
1904 * dip - ctime|mtime updated
1905 * ipp - ctime|mtime|atime updated
1909 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1910 cred_t *cr, int flags, vsecattr_t *vsecp)
1912 znode_t *zp, *dzp = ITOZ(dip);
1913 zfsvfs_t *zfsvfs = ITOZSB(dip);
1921 gid_t gid = crgetgid(cr);
1922 zfs_acl_ids_t acl_ids;
1923 boolean_t fuid_dirtied;
1924 boolean_t waited = B_FALSE;
1926 ASSERT(S_ISDIR(vap->va_mode));
1929 * If we have an ephemeral id, ACL, or XVATTR then
1930 * make sure file system is at proper version
1934 if (zfsvfs->z_use_fuids == B_FALSE &&
1935 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1936 return (SET_ERROR(EINVAL));
1938 if (dirname == NULL)
1939 return (SET_ERROR(EINVAL));
1943 zilog = zfsvfs->z_log;
1945 if (dzp->z_pflags & ZFS_XATTR) {
1947 return (SET_ERROR(EINVAL));
1950 if (zfsvfs->z_utf8 && u8_validate(dirname,
1951 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1953 return (SET_ERROR(EILSEQ));
1955 if (flags & FIGNORECASE)
1958 if (vap->va_mask & ATTR_XVATTR) {
1959 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1960 crgetuid(cr), cr, vap->va_mode)) != 0) {
1966 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1967 vsecp, &acl_ids)) != 0) {
1972 * First make sure the new directory doesn't exist.
1974 * Existence is checked first to make sure we don't return
1975 * EACCES instead of EEXIST which can cause some applications
1981 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1983 zfs_acl_ids_free(&acl_ids);
1988 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1989 zfs_acl_ids_free(&acl_ids);
1990 zfs_dirent_unlock(dl);
1995 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1996 zfs_acl_ids_free(&acl_ids);
1997 zfs_dirent_unlock(dl);
1999 return (SET_ERROR(EDQUOT));
2003 * Add a new entry to the directory.
2005 tx = dmu_tx_create(zfsvfs->z_os);
2006 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
2007 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
2008 fuid_dirtied = zfsvfs->z_fuid_dirty;
2010 zfs_fuid_txhold(zfsvfs, tx);
2011 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2012 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2013 acl_ids.z_aclp->z_acl_bytes);
2016 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
2017 ZFS_SA_BASE_ATTR_SIZE);
2019 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2021 zfs_dirent_unlock(dl);
2022 if (error == ERESTART) {
2028 zfs_acl_ids_free(&acl_ids);
2037 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2040 zfs_fuid_sync(zfsvfs, tx);
2043 * Now put new name in parent dir.
2045 (void) zfs_link_create(dl, zp, tx, ZNEW);
2049 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2050 if (flags & FIGNORECASE)
2052 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2053 acl_ids.z_fuidp, vap);
2055 zfs_acl_ids_free(&acl_ids);
2059 zfs_dirent_unlock(dl);
2061 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2062 zil_commit(zilog, 0);
2064 zfs_inode_update(dzp);
2065 zfs_inode_update(zp);
2071 * Remove a directory subdir entry. If the current working
2072 * directory is the same as the subdir to be removed, the
2075 * IN: dip - inode of directory to remove from.
2076 * name - name of directory to be removed.
2077 * cwd - inode of current working directory.
2078 * cr - credentials of caller.
2079 * flags - case flags
2081 * RETURN: 0 on success, error code on failure.
2084 * dip - ctime|mtime updated
2088 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
2091 znode_t *dzp = ITOZ(dip);
2094 zfsvfs_t *zfsvfs = ITOZSB(dip);
2100 boolean_t waited = B_FALSE;
2103 return (SET_ERROR(EINVAL));
2107 zilog = zfsvfs->z_log;
2109 if (flags & FIGNORECASE)
2115 * Attempt to lock directory; fail if entry doesn't exist.
2117 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2125 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
2129 if (!S_ISDIR(ip->i_mode)) {
2130 error = SET_ERROR(ENOTDIR);
2135 error = SET_ERROR(EINVAL);
2140 * Grab a lock on the directory to make sure that no one is
2141 * trying to add (or lookup) entries while we are removing it.
2143 rw_enter(&zp->z_name_lock, RW_WRITER);
2146 * Grab a lock on the parent pointer to make sure we play well
2147 * with the treewalk and directory rename code.
2149 rw_enter(&zp->z_parent_lock, RW_WRITER);
2151 tx = dmu_tx_create(zfsvfs->z_os);
2152 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2153 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2154 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2155 zfs_sa_upgrade_txholds(tx, zp);
2156 zfs_sa_upgrade_txholds(tx, dzp);
2157 dmu_tx_mark_netfree(tx);
2158 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2160 rw_exit(&zp->z_parent_lock);
2161 rw_exit(&zp->z_name_lock);
2162 zfs_dirent_unlock(dl);
2163 if (error == ERESTART) {
2176 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2179 uint64_t txtype = TX_RMDIR;
2180 if (flags & FIGNORECASE)
2182 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2187 rw_exit(&zp->z_parent_lock);
2188 rw_exit(&zp->z_name_lock);
2190 zfs_dirent_unlock(dl);
2192 zfs_inode_update(dzp);
2193 zfs_inode_update(zp);
2196 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2197 zil_commit(zilog, 0);
2204 * Read as many directory entries as will fit into the provided
2205 * dirent buffer from the given directory cursor position.
2207 * IN: ip - inode of directory to read.
2208 * dirent - buffer for directory entries.
2210 * OUT: dirent - filler buffer of directory entries.
2212 * RETURN: 0 if success
2213 * error code if failure
2216 * ip - atime updated
2218 * Note that the low 4 bits of the cookie returned by zap is always zero.
2219 * This allows us to use the low range for "special" directory entries:
2220 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2221 * we use the offset 2 for the '.zfs' directory.
2225 zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
2227 znode_t *zp = ITOZ(ip);
2228 zfsvfs_t *zfsvfs = ITOZSB(ip);
2231 zap_attribute_t zap;
2237 uint64_t offset; /* must be unsigned; checks for < 1 */
2242 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2243 &parent, sizeof (parent))) != 0)
2247 * Quit if directory has been removed (posix)
2255 prefetch = zp->z_zn_prefetch;
2258 * Initialize the iterator cursor.
2262 * Start iteration from the beginning of the directory.
2264 zap_cursor_init(&zc, os, zp->z_id);
2267 * The offset is a serialized cursor.
2269 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2273 * Transform to file-system independent format
2278 * Special case `.', `..', and `.zfs'.
2281 (void) strcpy(zap.za_name, ".");
2282 zap.za_normalization_conflict = 0;
2285 } else if (offset == 1) {
2286 (void) strcpy(zap.za_name, "..");
2287 zap.za_normalization_conflict = 0;
2290 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2291 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2292 zap.za_normalization_conflict = 0;
2293 objnum = ZFSCTL_INO_ROOT;
2299 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2300 if (error == ENOENT)
2307 * Allow multiple entries provided the first entry is
2308 * the object id. Non-zpl consumers may safely make
2309 * use of the additional space.
2311 * XXX: This should be a feature flag for compatibility
2313 if (zap.za_integer_length != 8 ||
2314 zap.za_num_integers == 0) {
2315 cmn_err(CE_WARN, "zap_readdir: bad directory "
2316 "entry, obj = %lld, offset = %lld, "
2317 "length = %d, num = %lld\n",
2318 (u_longlong_t)zp->z_id,
2319 (u_longlong_t)offset,
2320 zap.za_integer_length,
2321 (u_longlong_t)zap.za_num_integers);
2322 error = SET_ERROR(ENXIO);
2326 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2327 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2330 done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name),
2335 /* Prefetch znode */
2337 dmu_prefetch(os, objnum, 0, 0, 0,
2338 ZIO_PRIORITY_SYNC_READ);
2342 * Move to the next entry, fill in the previous offset.
2344 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2345 zap_cursor_advance(&zc);
2346 offset = zap_cursor_serialize(&zc);
2352 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2355 zap_cursor_fini(&zc);
2356 if (error == ENOENT)
2364 ulong_t zfs_fsync_sync_cnt = 4;
2367 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2369 znode_t *zp = ITOZ(ip);
2370 zfsvfs_t *zfsvfs = ITOZSB(ip);
2372 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2374 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2377 zil_commit(zfsvfs->z_log, zp->z_id);
2380 tsd_set(zfs_fsyncer_key, NULL);
2387 * Get the requested file attributes and place them in the provided
2390 * IN: ip - inode of file.
2391 * vap - va_mask identifies requested attributes.
2392 * If ATTR_XVATTR set, then optional attrs are requested
2393 * flags - ATTR_NOACLCHECK (CIFS server context)
2394 * cr - credentials of caller.
2396 * OUT: vap - attribute values.
2398 * RETURN: 0 (always succeeds)
2402 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2404 znode_t *zp = ITOZ(ip);
2405 zfsvfs_t *zfsvfs = ITOZSB(ip);
2408 uint64_t atime[2], mtime[2], ctime[2];
2409 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2410 xoptattr_t *xoap = NULL;
2411 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2412 sa_bulk_attr_t bulk[3];
2418 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2420 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
2421 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2422 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2424 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2430 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2431 * Also, if we are the owner don't bother, since owner should
2432 * always be allowed to read basic attributes of file.
2434 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2435 (vap->va_uid != crgetuid(cr))) {
2436 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2444 * Return all attributes. It's cheaper to provide the answer
2445 * than to determine whether we were asked the question.
2448 mutex_enter(&zp->z_lock);
2449 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2450 vap->va_mode = zp->z_mode;
2451 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2452 vap->va_nodeid = zp->z_id;
2453 if ((zp->z_id == zfsvfs->z_root) && zfs_show_ctldir(zp))
2454 links = ZTOI(zp)->i_nlink + 1;
2456 links = ZTOI(zp)->i_nlink;
2457 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2458 vap->va_size = i_size_read(ip);
2459 vap->va_rdev = ip->i_rdev;
2460 vap->va_seq = ip->i_generation;
2463 * Add in any requested optional attributes and the create time.
2464 * Also set the corresponding bits in the returned attribute bitmap.
2466 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2467 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2469 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2470 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2473 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2474 xoap->xoa_readonly =
2475 ((zp->z_pflags & ZFS_READONLY) != 0);
2476 XVA_SET_RTN(xvap, XAT_READONLY);
2479 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2481 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2482 XVA_SET_RTN(xvap, XAT_SYSTEM);
2485 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2487 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2488 XVA_SET_RTN(xvap, XAT_HIDDEN);
2491 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2492 xoap->xoa_nounlink =
2493 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2494 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2497 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2498 xoap->xoa_immutable =
2499 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2500 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2503 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2504 xoap->xoa_appendonly =
2505 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2506 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2509 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2511 ((zp->z_pflags & ZFS_NODUMP) != 0);
2512 XVA_SET_RTN(xvap, XAT_NODUMP);
2515 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2517 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2518 XVA_SET_RTN(xvap, XAT_OPAQUE);
2521 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2522 xoap->xoa_av_quarantined =
2523 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2524 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2527 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2528 xoap->xoa_av_modified =
2529 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2530 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2533 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2534 S_ISREG(ip->i_mode)) {
2535 zfs_sa_get_scanstamp(zp, xvap);
2538 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2541 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2542 times, sizeof (times));
2543 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2544 XVA_SET_RTN(xvap, XAT_CREATETIME);
2547 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2548 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2549 XVA_SET_RTN(xvap, XAT_REPARSE);
2551 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2552 xoap->xoa_generation = ip->i_generation;
2553 XVA_SET_RTN(xvap, XAT_GEN);
2556 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2558 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2559 XVA_SET_RTN(xvap, XAT_OFFLINE);
2562 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2564 ((zp->z_pflags & ZFS_SPARSE) != 0);
2565 XVA_SET_RTN(xvap, XAT_SPARSE);
2569 ZFS_TIME_DECODE(&vap->va_atime, atime);
2570 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2571 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2573 mutex_exit(&zp->z_lock);
2575 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2577 if (zp->z_blksz == 0) {
2579 * Block size hasn't been set; suggest maximal I/O transfers.
2581 vap->va_blksize = zfsvfs->z_max_blksz;
2589 * Get the basic file attributes and place them in the provided kstat
2590 * structure. The inode is assumed to be the authoritative source
2591 * for most of the attributes. However, the znode currently has the
2592 * authoritative atime, blksize, and block count.
2594 * IN: ip - inode of file.
2596 * OUT: sp - kstat values.
2598 * RETURN: 0 (always succeeds)
2602 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2604 znode_t *zp = ITOZ(ip);
2605 zfsvfs_t *zfsvfs = ITOZSB(ip);
2607 u_longlong_t nblocks;
2612 mutex_enter(&zp->z_lock);
2614 generic_fillattr(ip, sp);
2616 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2617 sp->blksize = blksize;
2618 sp->blocks = nblocks;
2620 if (unlikely(zp->z_blksz == 0)) {
2622 * Block size hasn't been set; suggest maximal I/O transfers.
2624 sp->blksize = zfsvfs->z_max_blksz;
2627 mutex_exit(&zp->z_lock);
2630 * Required to prevent NFS client from detecting different inode
2631 * numbers of snapshot root dentry before and after snapshot mount.
2633 if (zfsvfs->z_issnap) {
2634 if (ip->i_sb->s_root->d_inode == ip)
2635 sp->ino = ZFSCTL_INO_SNAPDIRS -
2636 dmu_objset_id(zfsvfs->z_os);
2645 * Set the file attributes to the values contained in the
2648 * IN: ip - inode of file to be modified.
2649 * vap - new attribute values.
2650 * If ATTR_XVATTR set, then optional attrs are being set
2651 * flags - ATTR_UTIME set if non-default time values provided.
2652 * - ATTR_NOACLCHECK (CIFS context only).
2653 * cr - credentials of caller.
2655 * RETURN: 0 if success
2656 * error code if failure
2659 * ip - ctime updated, mtime updated if size changed.
2663 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2665 znode_t *zp = ITOZ(ip);
2666 zfsvfs_t *zfsvfs = ITOZSB(ip);
2670 xvattr_t *tmpxvattr;
2671 uint_t mask = vap->va_mask;
2672 uint_t saved_mask = 0;
2675 uint64_t new_kuid = 0, new_kgid = 0, new_uid, new_gid;
2677 uint64_t mtime[2], ctime[2], atime[2];
2679 int need_policy = FALSE;
2681 zfs_fuid_info_t *fuidp = NULL;
2682 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2685 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2686 boolean_t fuid_dirtied = B_FALSE;
2687 sa_bulk_attr_t *bulk, *xattr_bulk;
2688 int count = 0, xattr_count = 0;
2696 zilog = zfsvfs->z_log;
2699 * Make sure that if we have ephemeral uid/gid or xvattr specified
2700 * that file system is at proper version level
2703 if (zfsvfs->z_use_fuids == B_FALSE &&
2704 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2705 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2706 (mask & ATTR_XVATTR))) {
2708 return (SET_ERROR(EINVAL));
2711 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2713 return (SET_ERROR(EISDIR));
2716 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2718 return (SET_ERROR(EINVAL));
2722 * If this is an xvattr_t, then get a pointer to the structure of
2723 * optional attributes. If this is NULL, then we have a vattr_t.
2725 xoap = xva_getxoptattr(xvap);
2727 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP);
2728 xva_init(tmpxvattr);
2730 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2731 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2734 * Immutable files can only alter immutable bit and atime
2736 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2737 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2738 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2739 err = SET_ERROR(EPERM);
2743 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2744 err = SET_ERROR(EPERM);
2749 * Verify timestamps doesn't overflow 32 bits.
2750 * ZFS can handle large timestamps, but 32bit syscalls can't
2751 * handle times greater than 2039. This check should be removed
2752 * once large timestamps are fully supported.
2754 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2755 if (((mask & ATTR_ATIME) &&
2756 TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2757 ((mask & ATTR_MTIME) &&
2758 TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2759 err = SET_ERROR(EOVERFLOW);
2768 /* Can this be moved to before the top label? */
2769 if (zfs_is_readonly(zfsvfs)) {
2770 err = SET_ERROR(EROFS);
2775 * First validate permissions
2778 if (mask & ATTR_SIZE) {
2779 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2784 * XXX - Note, we are not providing any open
2785 * mode flags here (like FNDELAY), so we may
2786 * block if there are locks present... this
2787 * should be addressed in openat().
2789 /* XXX - would it be OK to generate a log record here? */
2790 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2795 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2796 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2797 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2798 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2799 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2800 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2801 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2802 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2803 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2807 if (mask & (ATTR_UID|ATTR_GID)) {
2808 int idmask = (mask & (ATTR_UID|ATTR_GID));
2813 * NOTE: even if a new mode is being set,
2814 * we may clear S_ISUID/S_ISGID bits.
2817 if (!(mask & ATTR_MODE))
2818 vap->va_mode = zp->z_mode;
2821 * Take ownership or chgrp to group we are a member of
2824 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2825 take_group = (mask & ATTR_GID) &&
2826 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2829 * If both ATTR_UID and ATTR_GID are set then take_owner and
2830 * take_group must both be set in order to allow taking
2833 * Otherwise, send the check through secpolicy_vnode_setattr()
2837 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2838 take_owner && take_group) ||
2839 ((idmask == ATTR_UID) && take_owner) ||
2840 ((idmask == ATTR_GID) && take_group)) {
2841 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2842 skipaclchk, cr) == 0) {
2844 * Remove setuid/setgid for non-privileged users
2846 (void) secpolicy_setid_clear(vap, cr);
2847 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2856 mutex_enter(&zp->z_lock);
2857 oldva.va_mode = zp->z_mode;
2858 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2859 if (mask & ATTR_XVATTR) {
2861 * Update xvattr mask to include only those attributes
2862 * that are actually changing.
2864 * the bits will be restored prior to actually setting
2865 * the attributes so the caller thinks they were set.
2867 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2868 if (xoap->xoa_appendonly !=
2869 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2872 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2873 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2877 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2878 if (xoap->xoa_nounlink !=
2879 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2882 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2883 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2887 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2888 if (xoap->xoa_immutable !=
2889 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2892 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2893 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2897 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2898 if (xoap->xoa_nodump !=
2899 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2902 XVA_CLR_REQ(xvap, XAT_NODUMP);
2903 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2907 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2908 if (xoap->xoa_av_modified !=
2909 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2912 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2913 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2917 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2918 if ((!S_ISREG(ip->i_mode) &&
2919 xoap->xoa_av_quarantined) ||
2920 xoap->xoa_av_quarantined !=
2921 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2924 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2925 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2929 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2930 mutex_exit(&zp->z_lock);
2931 err = SET_ERROR(EPERM);
2935 if (need_policy == FALSE &&
2936 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2937 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2942 mutex_exit(&zp->z_lock);
2944 if (mask & ATTR_MODE) {
2945 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2946 err = secpolicy_setid_setsticky_clear(ip, vap,
2951 trim_mask |= ATTR_MODE;
2959 * If trim_mask is set then take ownership
2960 * has been granted or write_acl is present and user
2961 * has the ability to modify mode. In that case remove
2962 * UID|GID and or MODE from mask so that
2963 * secpolicy_vnode_setattr() doesn't revoke it.
2967 saved_mask = vap->va_mask;
2968 vap->va_mask &= ~trim_mask;
2970 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2971 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2976 vap->va_mask |= saved_mask;
2980 * secpolicy_vnode_setattr, or take ownership may have
2983 mask = vap->va_mask;
2985 if ((mask & (ATTR_UID | ATTR_GID))) {
2986 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2987 &xattr_obj, sizeof (xattr_obj));
2989 if (err == 0 && xattr_obj) {
2990 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2994 if (mask & ATTR_UID) {
2995 new_kuid = zfs_fuid_create(zfsvfs,
2996 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2997 if (new_kuid != KUID_TO_SUID(ZTOI(zp)->i_uid) &&
2998 zfs_fuid_overquota(zfsvfs, B_FALSE, new_kuid)) {
3001 err = SET_ERROR(EDQUOT);
3006 if (mask & ATTR_GID) {
3007 new_kgid = zfs_fuid_create(zfsvfs,
3008 (uint64_t)vap->va_gid, cr, ZFS_GROUP, &fuidp);
3009 if (new_kgid != KGID_TO_SGID(ZTOI(zp)->i_gid) &&
3010 zfs_fuid_overquota(zfsvfs, B_TRUE, new_kgid)) {
3013 err = SET_ERROR(EDQUOT);
3018 tx = dmu_tx_create(zfsvfs->z_os);
3020 if (mask & ATTR_MODE) {
3021 uint64_t pmode = zp->z_mode;
3023 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3025 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
3027 mutex_enter(&zp->z_lock);
3028 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3030 * Are we upgrading ACL from old V0 format
3033 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3034 zfs_znode_acl_version(zp) ==
3035 ZFS_ACL_VERSION_INITIAL) {
3036 dmu_tx_hold_free(tx, acl_obj, 0,
3038 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3039 0, aclp->z_acl_bytes);
3041 dmu_tx_hold_write(tx, acl_obj, 0,
3044 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3045 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3046 0, aclp->z_acl_bytes);
3048 mutex_exit(&zp->z_lock);
3049 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3051 if ((mask & ATTR_XVATTR) &&
3052 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3053 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3055 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3059 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3062 fuid_dirtied = zfsvfs->z_fuid_dirty;
3064 zfs_fuid_txhold(zfsvfs, tx);
3066 zfs_sa_upgrade_txholds(tx, zp);
3068 err = dmu_tx_assign(tx, TXG_WAIT);
3074 * Set each attribute requested.
3075 * We group settings according to the locks they need to acquire.
3077 * Note: you cannot set ctime directly, although it will be
3078 * updated as a side-effect of calling this function.
3082 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3083 mutex_enter(&zp->z_acl_lock);
3084 mutex_enter(&zp->z_lock);
3086 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3087 &zp->z_pflags, sizeof (zp->z_pflags));
3090 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3091 mutex_enter(&attrzp->z_acl_lock);
3092 mutex_enter(&attrzp->z_lock);
3093 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3094 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3095 sizeof (attrzp->z_pflags));
3098 if (mask & (ATTR_UID|ATTR_GID)) {
3100 if (mask & ATTR_UID) {
3101 ZTOI(zp)->i_uid = SUID_TO_KUID(new_kuid);
3102 new_uid = zfs_uid_read(ZTOI(zp));
3103 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3104 &new_uid, sizeof (new_uid));
3106 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3107 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3109 ZTOI(attrzp)->i_uid = SUID_TO_KUID(new_uid);
3113 if (mask & ATTR_GID) {
3114 ZTOI(zp)->i_gid = SGID_TO_KGID(new_kgid);
3115 new_gid = zfs_gid_read(ZTOI(zp));
3116 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3117 NULL, &new_gid, sizeof (new_gid));
3119 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3120 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3122 ZTOI(attrzp)->i_gid = SGID_TO_KGID(new_kgid);
3125 if (!(mask & ATTR_MODE)) {
3126 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3127 NULL, &new_mode, sizeof (new_mode));
3128 new_mode = zp->z_mode;
3130 err = zfs_acl_chown_setattr(zp);
3133 err = zfs_acl_chown_setattr(attrzp);
3138 if (mask & ATTR_MODE) {
3139 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3140 &new_mode, sizeof (new_mode));
3141 zp->z_mode = ZTOI(zp)->i_mode = new_mode;
3142 ASSERT3P(aclp, !=, NULL);
3143 err = zfs_aclset_common(zp, aclp, cr, tx);
3145 if (zp->z_acl_cached)
3146 zfs_acl_free(zp->z_acl_cached);
3147 zp->z_acl_cached = aclp;
3151 if ((mask & ATTR_ATIME) || zp->z_atime_dirty) {
3152 zp->z_atime_dirty = 0;
3153 ZFS_TIME_ENCODE(&ip->i_atime, atime);
3154 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3155 &atime, sizeof (atime));
3158 if (mask & ATTR_MTIME) {
3159 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3160 ZTOI(zp)->i_mtime = timespec_trunc(vap->va_mtime,
3161 ZTOI(zp)->i_sb->s_time_gran);
3163 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3164 mtime, sizeof (mtime));
3167 if (mask & ATTR_CTIME) {
3168 ZFS_TIME_ENCODE(&vap->va_ctime, ctime);
3169 ZTOI(zp)->i_ctime = timespec_trunc(vap->va_ctime,
3170 ZTOI(zp)->i_sb->s_time_gran);
3171 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3172 ctime, sizeof (ctime));
3175 if (attrzp && mask) {
3176 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3177 SA_ZPL_CTIME(zfsvfs), NULL, &ctime,
3182 * Do this after setting timestamps to prevent timestamp
3183 * update from toggling bit
3186 if (xoap && (mask & ATTR_XVATTR)) {
3189 * restore trimmed off masks
3190 * so that return masks can be set for caller.
3193 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
3194 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3196 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
3197 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3199 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
3200 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3202 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
3203 XVA_SET_REQ(xvap, XAT_NODUMP);
3205 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
3206 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3208 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
3209 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3212 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3213 ASSERT(S_ISREG(ip->i_mode));
3215 zfs_xvattr_set(zp, xvap, tx);
3219 zfs_fuid_sync(zfsvfs, tx);
3222 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3224 mutex_exit(&zp->z_lock);
3225 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3226 mutex_exit(&zp->z_acl_lock);
3229 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
3230 mutex_exit(&attrzp->z_acl_lock);
3231 mutex_exit(&attrzp->z_lock);
3234 if (err == 0 && attrzp) {
3235 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3244 zfs_fuid_info_free(fuidp);
3252 if (err == ERESTART)
3255 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3259 zfs_inode_update(zp);
3263 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3264 zil_commit(zilog, 0);
3267 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * 7);
3268 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 7);
3269 kmem_free(tmpxvattr, sizeof (xvattr_t));
3274 typedef struct zfs_zlock {
3275 krwlock_t *zl_rwlock; /* lock we acquired */
3276 znode_t *zl_znode; /* znode we held */
3277 struct zfs_zlock *zl_next; /* next in list */
3281 * Drop locks and release vnodes that were held by zfs_rename_lock().
3284 zfs_rename_unlock(zfs_zlock_t **zlpp)
3288 while ((zl = *zlpp) != NULL) {
3289 if (zl->zl_znode != NULL)
3290 zfs_iput_async(ZTOI(zl->zl_znode));
3291 rw_exit(zl->zl_rwlock);
3292 *zlpp = zl->zl_next;
3293 kmem_free(zl, sizeof (*zl));
3298 * Search back through the directory tree, using the ".." entries.
3299 * Lock each directory in the chain to prevent concurrent renames.
3300 * Fail any attempt to move a directory into one of its own descendants.
3301 * XXX - z_parent_lock can overlap with map or grow locks
3304 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3308 uint64_t rootid = ZTOZSB(zp)->z_root;
3309 uint64_t oidp = zp->z_id;
3310 krwlock_t *rwlp = &szp->z_parent_lock;
3311 krw_t rw = RW_WRITER;
3314 * First pass write-locks szp and compares to zp->z_id.
3315 * Later passes read-lock zp and compare to zp->z_parent.
3318 if (!rw_tryenter(rwlp, rw)) {
3320 * Another thread is renaming in this path.
3321 * Note that if we are a WRITER, we don't have any
3322 * parent_locks held yet.
3324 if (rw == RW_READER && zp->z_id > szp->z_id) {
3326 * Drop our locks and restart
3328 zfs_rename_unlock(&zl);
3332 rwlp = &szp->z_parent_lock;
3337 * Wait for other thread to drop its locks
3343 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3344 zl->zl_rwlock = rwlp;
3345 zl->zl_znode = NULL;
3346 zl->zl_next = *zlpp;
3349 if (oidp == szp->z_id) /* We're a descendant of szp */
3350 return (SET_ERROR(EINVAL));
3352 if (oidp == rootid) /* We've hit the top */
3355 if (rw == RW_READER) { /* i.e. not the first pass */
3356 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3361 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3362 &oidp, sizeof (oidp));
3363 rwlp = &zp->z_parent_lock;
3366 } while (zp->z_id != sdzp->z_id);
3372 * Move an entry from the provided source directory to the target
3373 * directory. Change the entry name as indicated.
3375 * IN: sdip - Source directory containing the "old entry".
3376 * snm - Old entry name.
3377 * tdip - Target directory to contain the "new entry".
3378 * tnm - New entry name.
3379 * cr - credentials of caller.
3380 * flags - case flags
3382 * RETURN: 0 on success, error code on failure.
3385 * sdip,tdip - ctime|mtime updated
3389 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3390 cred_t *cr, int flags)
3392 znode_t *tdzp, *szp, *tzp;
3393 znode_t *sdzp = ITOZ(sdip);
3394 zfsvfs_t *zfsvfs = ITOZSB(sdip);
3396 zfs_dirlock_t *sdl, *tdl;
3399 int cmp, serr, terr;
3402 boolean_t waited = B_FALSE;
3404 if (snm == NULL || tnm == NULL)
3405 return (SET_ERROR(EINVAL));
3408 ZFS_VERIFY_ZP(sdzp);
3409 zilog = zfsvfs->z_log;
3412 ZFS_VERIFY_ZP(tdzp);
3415 * We check i_sb because snapshots and the ctldir must have different
3418 if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) {
3420 return (SET_ERROR(EXDEV));
3423 if (zfsvfs->z_utf8 && u8_validate(tnm,
3424 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3426 return (SET_ERROR(EILSEQ));
3429 if (flags & FIGNORECASE)
3438 * This is to prevent the creation of links into attribute space
3439 * by renaming a linked file into/outof an attribute directory.
3440 * See the comment in zfs_link() for why this is considered bad.
3442 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3444 return (SET_ERROR(EINVAL));
3448 * Lock source and target directory entries. To prevent deadlock,
3449 * a lock ordering must be defined. We lock the directory with
3450 * the smallest object id first, or if it's a tie, the one with
3451 * the lexically first name.
3453 if (sdzp->z_id < tdzp->z_id) {
3455 } else if (sdzp->z_id > tdzp->z_id) {
3459 * First compare the two name arguments without
3460 * considering any case folding.
3462 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3464 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3465 ASSERT(error == 0 || !zfsvfs->z_utf8);
3468 * POSIX: "If the old argument and the new argument
3469 * both refer to links to the same existing file,
3470 * the rename() function shall return successfully
3471 * and perform no other action."
3477 * If the file system is case-folding, then we may
3478 * have some more checking to do. A case-folding file
3479 * system is either supporting mixed case sensitivity
3480 * access or is completely case-insensitive. Note
3481 * that the file system is always case preserving.
3483 * In mixed sensitivity mode case sensitive behavior
3484 * is the default. FIGNORECASE must be used to
3485 * explicitly request case insensitive behavior.
3487 * If the source and target names provided differ only
3488 * by case (e.g., a request to rename 'tim' to 'Tim'),
3489 * we will treat this as a special case in the
3490 * case-insensitive mode: as long as the source name
3491 * is an exact match, we will allow this to proceed as
3492 * a name-change request.
3494 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3495 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3496 flags & FIGNORECASE)) &&
3497 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3500 * case preserving rename request, require exact
3509 * If the source and destination directories are the same, we should
3510 * grab the z_name_lock of that directory only once.
3514 rw_enter(&sdzp->z_name_lock, RW_READER);
3518 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3519 ZEXISTS | zflg, NULL, NULL);
3520 terr = zfs_dirent_lock(&tdl,
3521 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3523 terr = zfs_dirent_lock(&tdl,
3524 tdzp, tnm, &tzp, zflg, NULL, NULL);
3525 serr = zfs_dirent_lock(&sdl,
3526 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3532 * Source entry invalid or not there.
3535 zfs_dirent_unlock(tdl);
3541 rw_exit(&sdzp->z_name_lock);
3543 if (strcmp(snm, "..") == 0)
3549 zfs_dirent_unlock(sdl);
3553 rw_exit(&sdzp->z_name_lock);
3555 if (strcmp(tnm, "..") == 0)
3562 * Must have write access at the source to remove the old entry
3563 * and write access at the target to create the new entry.
3564 * Note that if target and source are the same, this can be
3565 * done in a single check.
3568 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3571 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3573 * Check to make sure rename is valid.
3574 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3576 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3581 * Does target exist?
3585 * Source and target must be the same type.
3587 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3588 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3589 error = SET_ERROR(ENOTDIR);
3593 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3594 error = SET_ERROR(EISDIR);
3599 * POSIX dictates that when the source and target
3600 * entries refer to the same file object, rename
3601 * must do nothing and exit without error.
3603 if (szp->z_id == tzp->z_id) {
3609 tx = dmu_tx_create(zfsvfs->z_os);
3610 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3611 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3612 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3613 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3615 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3616 zfs_sa_upgrade_txholds(tx, tdzp);
3619 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3620 zfs_sa_upgrade_txholds(tx, tzp);
3623 zfs_sa_upgrade_txholds(tx, szp);
3624 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3625 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3628 zfs_rename_unlock(&zl);
3629 zfs_dirent_unlock(sdl);
3630 zfs_dirent_unlock(tdl);
3633 rw_exit(&sdzp->z_name_lock);
3635 if (error == ERESTART) {
3652 if (tzp) /* Attempt to remove the existing target */
3653 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3656 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3658 szp->z_pflags |= ZFS_AV_MODIFIED;
3660 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3661 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3664 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3666 zfs_log_rename(zilog, tx, TX_RENAME |
3667 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3668 sdl->dl_name, tdzp, tdl->dl_name, szp);
3671 * At this point, we have successfully created
3672 * the target name, but have failed to remove
3673 * the source name. Since the create was done
3674 * with the ZRENAMING flag, there are
3675 * complications; for one, the link count is
3676 * wrong. The easiest way to deal with this
3677 * is to remove the newly created target, and
3678 * return the original error. This must
3679 * succeed; fortunately, it is very unlikely to
3680 * fail, since we just created it.
3682 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3683 ZRENAMING, NULL), ==, 0);
3691 zfs_rename_unlock(&zl);
3693 zfs_dirent_unlock(sdl);
3694 zfs_dirent_unlock(tdl);
3696 zfs_inode_update(sdzp);
3698 rw_exit(&sdzp->z_name_lock);
3701 zfs_inode_update(tdzp);
3703 zfs_inode_update(szp);
3706 zfs_inode_update(tzp);
3710 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3711 zil_commit(zilog, 0);
3718 * Insert the indicated symbolic reference entry into the directory.
3720 * IN: dip - Directory to contain new symbolic link.
3721 * link - Name for new symlink entry.
3722 * vap - Attributes of new entry.
3723 * target - Target path of new symlink.
3725 * cr - credentials of caller.
3726 * flags - case flags
3728 * RETURN: 0 on success, error code on failure.
3731 * dip - ctime|mtime updated
3735 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3736 struct inode **ipp, cred_t *cr, int flags)
3738 znode_t *zp, *dzp = ITOZ(dip);
3741 zfsvfs_t *zfsvfs = ITOZSB(dip);
3743 uint64_t len = strlen(link);
3746 zfs_acl_ids_t acl_ids;
3747 boolean_t fuid_dirtied;
3748 uint64_t txtype = TX_SYMLINK;
3749 boolean_t waited = B_FALSE;
3751 ASSERT(S_ISLNK(vap->va_mode));
3754 return (SET_ERROR(EINVAL));
3758 zilog = zfsvfs->z_log;
3760 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3761 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3763 return (SET_ERROR(EILSEQ));
3765 if (flags & FIGNORECASE)
3768 if (len > MAXPATHLEN) {
3770 return (SET_ERROR(ENAMETOOLONG));
3773 if ((error = zfs_acl_ids_create(dzp, 0,
3774 vap, cr, NULL, &acl_ids)) != 0) {
3782 * Attempt to lock directory; fail if entry already exists.
3784 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3786 zfs_acl_ids_free(&acl_ids);
3791 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3792 zfs_acl_ids_free(&acl_ids);
3793 zfs_dirent_unlock(dl);
3798 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3799 zfs_acl_ids_free(&acl_ids);
3800 zfs_dirent_unlock(dl);
3802 return (SET_ERROR(EDQUOT));
3804 tx = dmu_tx_create(zfsvfs->z_os);
3805 fuid_dirtied = zfsvfs->z_fuid_dirty;
3806 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3807 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3808 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3809 ZFS_SA_BASE_ATTR_SIZE + len);
3810 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3811 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3812 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3813 acl_ids.z_aclp->z_acl_bytes);
3816 zfs_fuid_txhold(zfsvfs, tx);
3817 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3819 zfs_dirent_unlock(dl);
3820 if (error == ERESTART) {
3826 zfs_acl_ids_free(&acl_ids);
3833 * Create a new object for the symlink.
3834 * for version 4 ZPL datsets the symlink will be an SA attribute
3836 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3839 zfs_fuid_sync(zfsvfs, tx);
3841 mutex_enter(&zp->z_lock);
3843 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3846 zfs_sa_symlink(zp, link, len, tx);
3847 mutex_exit(&zp->z_lock);
3850 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3851 &zp->z_size, sizeof (zp->z_size), tx);
3853 * Insert the new object into the directory.
3855 (void) zfs_link_create(dl, zp, tx, ZNEW);
3857 if (flags & FIGNORECASE)
3859 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3861 zfs_inode_update(dzp);
3862 zfs_inode_update(zp);
3864 zfs_acl_ids_free(&acl_ids);
3868 zfs_dirent_unlock(dl);
3872 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3873 zil_commit(zilog, 0);
3880 * Return, in the buffer contained in the provided uio structure,
3881 * the symbolic path referred to by ip.
3883 * IN: ip - inode of symbolic link
3884 * uio - structure to contain the link path.
3885 * cr - credentials of caller.
3887 * RETURN: 0 if success
3888 * error code if failure
3891 * ip - atime updated
3895 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3897 znode_t *zp = ITOZ(ip);
3898 zfsvfs_t *zfsvfs = ITOZSB(ip);
3904 mutex_enter(&zp->z_lock);
3906 error = sa_lookup_uio(zp->z_sa_hdl,
3907 SA_ZPL_SYMLINK(zfsvfs), uio);
3909 error = zfs_sa_readlink(zp, uio);
3910 mutex_exit(&zp->z_lock);
3917 * Insert a new entry into directory tdip referencing sip.
3919 * IN: tdip - Directory to contain new entry.
3920 * sip - inode of new entry.
3921 * name - name of new entry.
3922 * cr - credentials of caller.
3924 * RETURN: 0 if success
3925 * error code if failure
3928 * tdip - ctime|mtime updated
3929 * sip - ctime updated
3933 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr,
3936 znode_t *dzp = ITOZ(tdip);
3938 zfsvfs_t *zfsvfs = ITOZSB(tdip);
3946 boolean_t waited = B_FALSE;
3947 boolean_t is_tmpfile = 0;
3950 is_tmpfile = (sip->i_nlink == 0 && (sip->i_state & I_LINKABLE));
3952 ASSERT(S_ISDIR(tdip->i_mode));
3955 return (SET_ERROR(EINVAL));
3959 zilog = zfsvfs->z_log;
3962 * POSIX dictates that we return EPERM here.
3963 * Better choices include ENOTSUP or EISDIR.
3965 if (S_ISDIR(sip->i_mode)) {
3967 return (SET_ERROR(EPERM));
3974 * We check i_sb because snapshots and the ctldir must have different
3977 if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) {
3979 return (SET_ERROR(EXDEV));
3982 /* Prevent links to .zfs/shares files */
3984 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3985 &parent, sizeof (uint64_t))) != 0) {
3989 if (parent == zfsvfs->z_shares_dir) {
3991 return (SET_ERROR(EPERM));
3994 if (zfsvfs->z_utf8 && u8_validate(name,
3995 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3997 return (SET_ERROR(EILSEQ));
3999 if (flags & FIGNORECASE)
4003 * We do not support links between attributes and non-attributes
4004 * because of the potential security risk of creating links
4005 * into "normal" file space in order to circumvent restrictions
4006 * imposed in attribute space.
4008 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4010 return (SET_ERROR(EINVAL));
4013 owner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(sip->i_uid),
4015 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4017 return (SET_ERROR(EPERM));
4020 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
4027 * Attempt to lock directory; fail if entry already exists.
4029 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4035 tx = dmu_tx_create(zfsvfs->z_os);
4036 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4037 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4039 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
4041 zfs_sa_upgrade_txholds(tx, szp);
4042 zfs_sa_upgrade_txholds(tx, dzp);
4043 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4045 zfs_dirent_unlock(dl);
4046 if (error == ERESTART) {
4056 /* unmark z_unlinked so zfs_link_create will not reject */
4058 szp->z_unlinked = 0;
4059 error = zfs_link_create(dl, szp, tx, 0);
4062 uint64_t txtype = TX_LINK;
4064 * tmpfile is created to be in z_unlinkedobj, so remove it.
4065 * Also, we don't log in ZIL, be cause all previous file
4066 * operation on the tmpfile are ignored by ZIL. Instead we
4067 * always wait for txg to sync to make sure all previous
4068 * operation are sync safe.
4071 VERIFY(zap_remove_int(zfsvfs->z_os,
4072 zfsvfs->z_unlinkedobj, szp->z_id, tx) == 0);
4074 if (flags & FIGNORECASE)
4076 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4078 } else if (is_tmpfile) {
4079 /* restore z_unlinked since when linking failed */
4080 szp->z_unlinked = 1;
4082 txg = dmu_tx_get_txg(tx);
4085 zfs_dirent_unlock(dl);
4087 if (!is_tmpfile && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4088 zil_commit(zilog, 0);
4091 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), txg);
4093 zfs_inode_update(dzp);
4094 zfs_inode_update(szp);
4100 zfs_putpage_commit_cb(void *arg)
4102 struct page *pp = arg;
4105 end_page_writeback(pp);
4109 * Push a page out to disk, once the page is on stable storage the
4110 * registered commit callback will be run as notification of completion.
4112 * IN: ip - page mapped for inode.
4113 * pp - page to push (page is locked)
4114 * wbc - writeback control data
4116 * RETURN: 0 if success
4117 * error code if failure
4120 * ip - ctime|mtime updated
4124 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
4126 znode_t *zp = ITOZ(ip);
4127 zfsvfs_t *zfsvfs = ITOZSB(ip);
4135 uint64_t mtime[2], ctime[2];
4136 sa_bulk_attr_t bulk[3];
4138 struct address_space *mapping;
4143 ASSERT(PageLocked(pp));
4145 pgoff = page_offset(pp); /* Page byte-offset in file */
4146 offset = i_size_read(ip); /* File length in bytes */
4147 pglen = MIN(PAGE_SIZE, /* Page length in bytes */
4148 P2ROUNDUP(offset, PAGE_SIZE)-pgoff);
4150 /* Page is beyond end of file */
4151 if (pgoff >= offset) {
4157 /* Truncate page length to end of file */
4158 if (pgoff + pglen > offset)
4159 pglen = offset - pgoff;
4163 * FIXME: Allow mmap writes past its quota. The correct fix
4164 * is to register a page_mkwrite() handler to count the page
4165 * against its quota when it is about to be dirtied.
4167 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4168 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4174 * The ordering here is critical and must adhere to the following
4175 * rules in order to avoid deadlocking in either zfs_read() or
4176 * zfs_free_range() due to a lock inversion.
4178 * 1) The page must be unlocked prior to acquiring the range lock.
4179 * This is critical because zfs_read() calls find_lock_page()
4180 * which may block on the page lock while holding the range lock.
4182 * 2) Before setting or clearing write back on a page the range lock
4183 * must be held in order to prevent a lock inversion with the
4184 * zfs_free_range() function.
4186 * This presents a problem because upon entering this function the
4187 * page lock is already held. To safely acquire the range lock the
4188 * page lock must be dropped. This creates a window where another
4189 * process could truncate, invalidate, dirty, or write out the page.
4191 * Therefore, after successfully reacquiring the range and page locks
4192 * the current page state is checked. In the common case everything
4193 * will be as is expected and it can be written out. However, if
4194 * the page state has changed it must be handled accordingly.
4196 mapping = pp->mapping;
4197 redirty_page_for_writepage(wbc, pp);
4200 rl = zfs_range_lock(&zp->z_range_lock, pgoff, pglen, RL_WRITER);
4203 /* Page mapping changed or it was no longer dirty, we're done */
4204 if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) {
4206 zfs_range_unlock(rl);
4211 /* Another process started write block if required */
4212 if (PageWriteback(pp)) {
4214 zfs_range_unlock(rl);
4216 if (wbc->sync_mode != WB_SYNC_NONE)
4217 wait_on_page_writeback(pp);
4223 /* Clear the dirty flag the required locks are held */
4224 if (!clear_page_dirty_for_io(pp)) {
4226 zfs_range_unlock(rl);
4232 * Counterpart for redirty_page_for_writepage() above. This page
4233 * was in fact not skipped and should not be counted as if it were.
4235 wbc->pages_skipped--;
4236 set_page_writeback(pp);
4239 tx = dmu_tx_create(zfsvfs->z_os);
4240 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
4241 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4242 zfs_sa_upgrade_txholds(tx, zp);
4244 err = dmu_tx_assign(tx, TXG_NOWAIT);
4246 if (err == ERESTART)
4250 __set_page_dirty_nobuffers(pp);
4252 end_page_writeback(pp);
4253 zfs_range_unlock(rl);
4259 ASSERT3U(pglen, <=, PAGE_SIZE);
4260 dmu_write(zfsvfs->z_os, zp->z_id, pgoff, pglen, va, tx);
4263 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
4264 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
4265 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zfsvfs), NULL,
4268 /* Preserve the mtime and ctime provided by the inode */
4269 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4270 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4271 zp->z_atime_dirty = 0;
4274 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4276 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
4277 zfs_putpage_commit_cb, pp);
4280 zfs_range_unlock(rl);
4282 if (wbc->sync_mode != WB_SYNC_NONE) {
4284 * Note that this is rarely called under writepages(), because
4285 * writepages() normally handles the entire commit for
4286 * performance reasons.
4288 zil_commit(zfsvfs->z_log, zp->z_id);
4296 * Update the system attributes when the inode has been dirtied. For the
4297 * moment we only update the mode, atime, mtime, and ctime.
4300 zfs_dirty_inode(struct inode *ip, int flags)
4302 znode_t *zp = ITOZ(ip);
4303 zfsvfs_t *zfsvfs = ITOZSB(ip);
4305 uint64_t mode, atime[2], mtime[2], ctime[2];
4306 sa_bulk_attr_t bulk[4];
4310 if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os))
4318 * This is the lazytime semantic indroduced in Linux 4.0
4319 * This flag will only be called from update_time when lazytime is set.
4320 * (Note, I_DIRTY_SYNC will also set if not lazytime)
4321 * Fortunately mtime and ctime are managed within ZFS itself, so we
4322 * only need to dirty atime.
4324 if (flags == I_DIRTY_TIME) {
4325 zp->z_atime_dirty = 1;
4330 tx = dmu_tx_create(zfsvfs->z_os);
4332 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4333 zfs_sa_upgrade_txholds(tx, zp);
4335 error = dmu_tx_assign(tx, TXG_WAIT);
4341 mutex_enter(&zp->z_lock);
4342 zp->z_atime_dirty = 0;
4344 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
4345 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
4346 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
4347 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
4349 /* Preserve the mode, mtime and ctime provided by the inode */
4350 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4351 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4352 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
4357 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4358 mutex_exit(&zp->z_lock);
4368 zfs_inactive(struct inode *ip)
4370 znode_t *zp = ITOZ(ip);
4371 zfsvfs_t *zfsvfs = ITOZSB(ip);
4374 int need_unlock = 0;
4376 /* Only read lock if we haven't already write locked, e.g. rollback */
4377 if (!RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)) {
4379 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4381 if (zp->z_sa_hdl == NULL) {
4383 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4387 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4388 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4390 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4391 zfs_sa_upgrade_txholds(tx, zp);
4392 error = dmu_tx_assign(tx, TXG_WAIT);
4396 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4397 mutex_enter(&zp->z_lock);
4398 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4399 (void *)&atime, sizeof (atime), tx);
4400 zp->z_atime_dirty = 0;
4401 mutex_exit(&zp->z_lock);
4408 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4412 * Bounds-check the seek operation.
4414 * IN: ip - inode seeking within
4415 * ooff - old file offset
4416 * noffp - pointer to new file offset
4417 * ct - caller context
4419 * RETURN: 0 if success
4420 * EINVAL if new offset invalid
4424 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4426 if (S_ISDIR(ip->i_mode))
4428 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4432 * Fill pages with data from the disk.
4435 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4437 znode_t *zp = ITOZ(ip);
4438 zfsvfs_t *zfsvfs = ITOZSB(ip);
4440 struct page *cur_pp;
4441 u_offset_t io_off, total;
4448 io_len = nr_pages << PAGE_SHIFT;
4449 i_size = i_size_read(ip);
4450 io_off = page_offset(pl[0]);
4452 if (io_off + io_len > i_size)
4453 io_len = i_size - io_off;
4456 * Iterate over list of pages and read each page individually.
4459 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4462 cur_pp = pl[page_idx++];
4464 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4468 /* convert checksum errors into IO errors */
4470 err = SET_ERROR(EIO);
4479 * Uses zfs_fillpage to read data from the file and fill the pages.
4481 * IN: ip - inode of file to get data from.
4482 * pl - list of pages to read
4483 * nr_pages - number of pages to read
4485 * RETURN: 0 on success, error code on failure.
4488 * vp - atime updated
4492 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4494 znode_t *zp = ITOZ(ip);
4495 zfsvfs_t *zfsvfs = ITOZSB(ip);
4504 err = zfs_fillpage(ip, pl, nr_pages);
4511 * Check ZFS specific permissions to memory map a section of a file.
4513 * IN: ip - inode of the file to mmap
4515 * addrp - start address in memory region
4516 * len - length of memory region
4517 * vm_flags- address flags
4519 * RETURN: 0 if success
4520 * error code if failure
4524 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4525 unsigned long vm_flags)
4527 znode_t *zp = ITOZ(ip);
4528 zfsvfs_t *zfsvfs = ITOZSB(ip);
4533 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4534 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4536 return (SET_ERROR(EPERM));
4539 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4540 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4542 return (SET_ERROR(EACCES));
4545 if (off < 0 || len > MAXOFFSET_T - off) {
4547 return (SET_ERROR(ENXIO));
4555 * convoff - converts the given data (start, whence) to the
4559 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4564 if ((lckdat->l_whence == 2) || (whence == 2)) {
4565 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4569 switch (lckdat->l_whence) {
4571 lckdat->l_start += offset;
4574 lckdat->l_start += vap.va_size;
4579 return (SET_ERROR(EINVAL));
4582 if (lckdat->l_start < 0)
4583 return (SET_ERROR(EINVAL));
4587 lckdat->l_start -= offset;
4590 lckdat->l_start -= vap.va_size;
4595 return (SET_ERROR(EINVAL));
4598 lckdat->l_whence = (short)whence;
4603 * Free or allocate space in a file. Currently, this function only
4604 * supports the `F_FREESP' command. However, this command is somewhat
4605 * misnamed, as its functionality includes the ability to allocate as
4606 * well as free space.
4608 * IN: ip - inode of file to free data in.
4609 * cmd - action to take (only F_FREESP supported).
4610 * bfp - section of file to free/alloc.
4611 * flag - current file open mode flags.
4612 * offset - current file offset.
4613 * cr - credentials of caller [UNUSED].
4615 * RETURN: 0 on success, error code on failure.
4618 * ip - ctime|mtime updated
4622 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4623 offset_t offset, cred_t *cr)
4625 znode_t *zp = ITOZ(ip);
4626 zfsvfs_t *zfsvfs = ITOZSB(ip);
4633 if (cmd != F_FREESP) {
4635 return (SET_ERROR(EINVAL));
4639 * Callers might not be able to detect properly that we are read-only,
4640 * so check it explicitly here.
4642 if (zfs_is_readonly(zfsvfs)) {
4644 return (SET_ERROR(EROFS));
4647 if ((error = convoff(ip, bfp, 0, offset))) {
4652 if (bfp->l_len < 0) {
4654 return (SET_ERROR(EINVAL));
4658 * Permissions aren't checked on Solaris because on this OS
4659 * zfs_space() can only be called with an opened file handle.
4660 * On Linux we can get here through truncate_range() which
4661 * operates directly on inodes, so we need to check access rights.
4663 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4669 len = bfp->l_len; /* 0 means from off to end of file */
4671 error = zfs_freesp(zp, off, len, flag, TRUE);
4679 zfs_fid(struct inode *ip, fid_t *fidp)
4681 znode_t *zp = ITOZ(ip);
4682 zfsvfs_t *zfsvfs = ITOZSB(ip);
4685 uint64_t object = zp->z_id;
4692 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4693 &gen64, sizeof (uint64_t))) != 0) {
4698 gen = (uint32_t)gen64;
4700 size = SHORT_FID_LEN;
4702 zfid = (zfid_short_t *)fidp;
4704 zfid->zf_len = size;
4706 for (i = 0; i < sizeof (zfid->zf_object); i++)
4707 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4709 /* Must have a non-zero generation number to distinguish from .zfs */
4712 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4713 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4721 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4723 znode_t *zp = ITOZ(ip);
4724 zfsvfs_t *zfsvfs = ITOZSB(ip);
4726 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4730 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4738 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4740 znode_t *zp = ITOZ(ip);
4741 zfsvfs_t *zfsvfs = ITOZSB(ip);
4743 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4744 zilog_t *zilog = zfsvfs->z_log;
4749 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4751 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4752 zil_commit(zilog, 0);
4758 #ifdef HAVE_UIO_ZEROCOPY
4760 * Tunable, both must be a power of 2.
4762 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4763 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4764 * an arcbuf for a partial block read
4766 int zcr_blksz_min = (1 << 10); /* 1K */
4767 int zcr_blksz_max = (1 << 17); /* 128K */
4771 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4773 znode_t *zp = ITOZ(ip);
4774 zfsvfs_t *zfsvfs = ITOZSB(ip);
4775 int max_blksz = zfsvfs->z_max_blksz;
4776 uio_t *uio = &xuio->xu_uio;
4777 ssize_t size = uio->uio_resid;
4778 offset_t offset = uio->uio_loffset;
4783 int preamble, postamble;
4785 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4786 return (SET_ERROR(EINVAL));
4793 * Loan out an arc_buf for write if write size is bigger than
4794 * max_blksz, and the file's block size is also max_blksz.
4797 if (size < blksz || zp->z_blksz != blksz) {
4799 return (SET_ERROR(EINVAL));
4802 * Caller requests buffers for write before knowing where the
4803 * write offset might be (e.g. NFS TCP write).
4808 preamble = P2PHASE(offset, blksz);
4810 preamble = blksz - preamble;
4815 postamble = P2PHASE(size, blksz);
4818 fullblk = size / blksz;
4819 (void) dmu_xuio_init(xuio,
4820 (preamble != 0) + fullblk + (postamble != 0));
4823 * Have to fix iov base/len for partial buffers. They
4824 * currently represent full arc_buf's.
4827 /* data begins in the middle of the arc_buf */
4828 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4831 (void) dmu_xuio_add(xuio, abuf,
4832 blksz - preamble, preamble);
4835 for (i = 0; i < fullblk; i++) {
4836 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4839 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4843 /* data ends in the middle of the arc_buf */
4844 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4847 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4852 * Loan out an arc_buf for read if the read size is larger than
4853 * the current file block size. Block alignment is not
4854 * considered. Partial arc_buf will be loaned out for read.
4856 blksz = zp->z_blksz;
4857 if (blksz < zcr_blksz_min)
4858 blksz = zcr_blksz_min;
4859 if (blksz > zcr_blksz_max)
4860 blksz = zcr_blksz_max;
4861 /* avoid potential complexity of dealing with it */
4862 if (blksz > max_blksz) {
4864 return (SET_ERROR(EINVAL));
4867 maxsize = zp->z_size - uio->uio_loffset;
4873 return (SET_ERROR(EINVAL));
4878 return (SET_ERROR(EINVAL));
4881 uio->uio_extflg = UIO_XUIO;
4882 XUIO_XUZC_RW(xuio) = ioflag;
4889 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4893 int ioflag = XUIO_XUZC_RW(xuio);
4895 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4897 i = dmu_xuio_cnt(xuio);
4899 abuf = dmu_xuio_arcbuf(xuio, i);
4901 * if abuf == NULL, it must be a write buffer
4902 * that has been returned in zfs_write().
4905 dmu_return_arcbuf(abuf);
4906 ASSERT(abuf || ioflag == UIO_WRITE);
4909 dmu_xuio_fini(xuio);
4912 #endif /* HAVE_UIO_ZEROCOPY */
4914 #if defined(_KERNEL) && defined(HAVE_SPL)
4915 EXPORT_SYMBOL(zfs_open);
4916 EXPORT_SYMBOL(zfs_close);
4917 EXPORT_SYMBOL(zfs_read);
4918 EXPORT_SYMBOL(zfs_write);
4919 EXPORT_SYMBOL(zfs_access);
4920 EXPORT_SYMBOL(zfs_lookup);
4921 EXPORT_SYMBOL(zfs_create);
4922 EXPORT_SYMBOL(zfs_tmpfile);
4923 EXPORT_SYMBOL(zfs_remove);
4924 EXPORT_SYMBOL(zfs_mkdir);
4925 EXPORT_SYMBOL(zfs_rmdir);
4926 EXPORT_SYMBOL(zfs_readdir);
4927 EXPORT_SYMBOL(zfs_fsync);
4928 EXPORT_SYMBOL(zfs_getattr);
4929 EXPORT_SYMBOL(zfs_getattr_fast);
4930 EXPORT_SYMBOL(zfs_setattr);
4931 EXPORT_SYMBOL(zfs_rename);
4932 EXPORT_SYMBOL(zfs_symlink);
4933 EXPORT_SYMBOL(zfs_readlink);
4934 EXPORT_SYMBOL(zfs_link);
4935 EXPORT_SYMBOL(zfs_inactive);
4936 EXPORT_SYMBOL(zfs_space);
4937 EXPORT_SYMBOL(zfs_fid);
4938 EXPORT_SYMBOL(zfs_getsecattr);
4939 EXPORT_SYMBOL(zfs_setsecattr);
4940 EXPORT_SYMBOL(zfs_getpage);
4941 EXPORT_SYMBOL(zfs_putpage);
4942 EXPORT_SYMBOL(zfs_dirty_inode);
4943 EXPORT_SYMBOL(zfs_map);
4946 module_param(zfs_delete_blocks, ulong, 0644);
4947 MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async");
4948 module_param(zfs_read_chunk_size, long, 0644);
4949 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");