4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Portions Copyright 2007 Jeremy Teo */
26 /* Portions Copyright 2010 Robert Milkowski */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
36 #include <sys/vfs_opreg.h>
40 #include <sys/taskq.h>
42 #include <sys/vmsystm.h>
43 #include <sys/atomic.h>
45 #include <sys/pathname.h>
46 #include <sys/cmn_err.h>
47 #include <sys/errno.h>
48 #include <sys/unistd.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/fs/zfs.h>
54 #include <sys/dmu_objset.h>
60 #include <sys/dirent.h>
61 #include <sys/policy.h>
62 #include <sys/sunddi.h>
65 #include "fs/fs_subr.h"
66 #include <sys/zfs_ctldir.h>
67 #include <sys/zfs_fuid.h>
68 #include <sys/zfs_sa.h>
69 #include <sys/zfs_vnops.h>
71 #include <sys/zfs_rlock.h>
72 #include <sys/extdirent.h>
73 #include <sys/kidmap.h>
81 * Each vnode op performs some logical unit of work. To do this, the ZPL must
82 * properly lock its in-core state, create a DMU transaction, do the work,
83 * record this work in the intent log (ZIL), commit the DMU transaction,
84 * and wait for the intent log to commit if it is a synchronous operation.
85 * Moreover, the vnode ops must work in both normal and log replay context.
86 * The ordering of events is important to avoid deadlocks and references
87 * to freed memory. The example below illustrates the following Big Rules:
89 * (1) A check must be made in each zfs thread for a mounted file system.
90 * This is done avoiding races using ZFS_ENTER(zsb).
91 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
92 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
93 * can return EIO from the calling function.
95 * (2) iput() should always be the last thing except for zil_commit()
96 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
97 * First, if it's the last reference, the vnode/znode
98 * can be freed, so the zp may point to freed memory. Second, the last
99 * reference will call zfs_zinactive(), which may induce a lot of work --
100 * pushing cached pages (which acquires range locks) and syncing out
101 * cached atime changes. Third, zfs_zinactive() may require a new tx,
102 * which could deadlock the system if you were already holding one.
103 * If you must call iput() within a tx then use iput_ASYNC().
105 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
106 * as they can span dmu_tx_assign() calls.
108 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
109 * This is critical because we don't want to block while holding locks.
110 * Note, in particular, that if a lock is sometimes acquired before
111 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
112 * use a non-blocking assign can deadlock the system. The scenario:
114 * Thread A has grabbed a lock before calling dmu_tx_assign().
115 * Thread B is in an already-assigned tx, and blocks for this lock.
116 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
117 * forever, because the previous txg can't quiesce until B's tx commits.
119 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
120 * then drop all locks, call dmu_tx_wait(), and try again.
122 * (5) If the operation succeeded, generate the intent log entry for it
123 * before dropping locks. This ensures that the ordering of events
124 * in the intent log matches the order in which they actually occurred.
125 * During ZIL replay the zfs_log_* functions will update the sequence
126 * number to indicate the zil transaction has replayed.
128 * (6) At the end of each vnode op, the DMU tx must always commit,
129 * regardless of whether there were any errors.
131 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
132 * to ensure that synchronous semantics are provided when necessary.
134 * In general, this is how things should be ordered in each vnode op:
136 * ZFS_ENTER(zsb); // exit if unmounted
138 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
139 * rw_enter(...); // grab any other locks you need
140 * tx = dmu_tx_create(...); // get DMU tx
141 * dmu_tx_hold_*(); // hold each object you might modify
142 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
144 * rw_exit(...); // drop locks
145 * zfs_dirent_unlock(dl); // unlock directory entry
146 * iput(...); // release held vnodes
147 * if (error == ERESTART) {
152 * dmu_tx_abort(tx); // abort DMU tx
153 * ZFS_EXIT(zsb); // finished in zfs
154 * return (error); // really out of space
156 * error = do_real_work(); // do whatever this VOP does
158 * zfs_log_*(...); // on success, make ZIL entry
159 * dmu_tx_commit(tx); // commit DMU tx -- error or not
160 * rw_exit(...); // drop locks
161 * zfs_dirent_unlock(dl); // unlock directory entry
162 * iput(...); // release held vnodes
163 * zil_commit(zilog, foid); // synchronous when necessary
164 * ZFS_EXIT(zsb); // finished in zfs
165 * return (error); // done, report error
169 * Virus scanning is unsupported. It would be possible to add a hook
170 * here to performance the required virus scan. This could be done
171 * entirely in the kernel or potentially as an update to invoke a
175 zfs_vscan(struct inode *ip, cred_t *cr, int async)
182 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
184 znode_t *zp = ITOZ(ip);
185 zfs_sb_t *zsb = ITOZSB(ip);
190 /* Honor ZFS_APPENDONLY file attribute */
191 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
192 ((flag & O_APPEND) == 0)) {
197 /* Virus scan eligible files on open */
198 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
199 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
200 if (zfs_vscan(ip, cr, 0) != 0) {
206 /* Keep a count of the synchronous opens in the znode */
208 atomic_inc_32(&zp->z_sync_cnt);
213 EXPORT_SYMBOL(zfs_open);
217 zfs_close(struct inode *ip, int flag, cred_t *cr)
219 znode_t *zp = ITOZ(ip);
220 zfs_sb_t *zsb = ITOZSB(ip);
227 * When closing an mmap()'ed file ensure the inode atime, mtime, and
228 * ctime are written to disk. These values may have been updated in
229 * memory by filemap_page_mkwrite() bit are not yet reflected in the
230 * znode since writepage() may occur after the close.
232 if (zp->z_is_mapped) {
235 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
236 vap->va_mask = ATTR_ATIME | ATTR_MTIME | ATTR_CTIME;
237 vap->va_atime = ip->i_atime;
238 vap->va_mtime = ip->i_mtime;
239 vap->va_ctime = ip->i_ctime;
241 error = zfs_setattr(ip, vap, 0, cr);
243 kmem_free(vap, sizeof(vattr_t));
247 * Zero the synchronous opens in the znode. Under Linux the
248 * zfs_close() hook is not symmetric with zfs_open(), it is
249 * only called once when the last reference is dropped.
254 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
255 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
256 VERIFY(zfs_vscan(ip, cr, 1) == 0);
261 EXPORT_SYMBOL(zfs_close);
265 * When a file is memory mapped, we must keep the IO data synchronized
266 * between the DMU cache and the memory mapped pages. What this means:
268 * On Write: If we find a memory mapped page, we write to *both*
269 * the page and the dmu buffer.
272 update_pages(struct inode *ip, int64_t start, int len,
273 objset_t *os, uint64_t oid)
275 struct address_space *mp = ip->i_mapping;
281 off = start & (PAGE_CACHE_SIZE-1);
282 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
283 nbytes = MIN(PAGE_CACHE_SIZE - off, len);
285 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
287 if (mapping_writably_mapped(mp))
288 flush_dcache_page(pp);
291 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
295 if (mapping_writably_mapped(mp))
296 flush_dcache_page(pp);
298 mark_page_accessed(pp);
302 page_cache_release(pp);
311 * When a file is memory mapped, we must keep the IO data synchronized
312 * between the DMU cache and the memory mapped pages. What this means:
314 * On Read: We "read" preferentially from memory mapped pages,
315 * else we default from the dmu buffer.
317 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
318 * the file is memory mapped.
321 mappedread(struct inode *ip, int nbytes, uio_t *uio)
323 struct address_space *mp = ip->i_mapping;
325 znode_t *zp = ITOZ(ip);
326 objset_t *os = ITOZSB(ip)->z_os;
333 start = uio->uio_loffset;
334 off = start & (PAGE_CACHE_SIZE-1);
335 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
336 bytes = MIN(PAGE_CACHE_SIZE - off, len);
338 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
340 ASSERT(PageUptodate(pp));
343 error = uiomove(pb + off, bytes, UIO_READ, uio);
346 if (mapping_writably_mapped(mp))
347 flush_dcache_page(pp);
349 mark_page_accessed(pp);
351 page_cache_release(pp);
353 error = dmu_read_uio(os, zp->z_id, uio, bytes);
365 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
368 * Read bytes from specified file into supplied buffer.
370 * IN: ip - inode of file to be read from.
371 * uio - structure supplying read location, range info,
373 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
374 * O_DIRECT flag; used to bypass page cache.
375 * cr - credentials of caller.
377 * OUT: uio - updated offset and range, buffer filled.
379 * RETURN: 0 if success
380 * error code if failure
383 * inode - atime updated if byte count > 0
387 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
389 znode_t *zp = ITOZ(ip);
390 zfs_sb_t *zsb = ITOZSB(ip);
395 #ifdef HAVE_UIO_ZEROCOPY
397 #endif /* HAVE_UIO_ZEROCOPY */
403 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
409 * Validate file offset
411 if (uio->uio_loffset < (offset_t)0) {
417 * Fasttrack empty reads
419 if (uio->uio_resid == 0) {
425 * Check for mandatory locks
427 if (mandatory_lock(ip) &&
428 !lock_may_read(ip, uio->uio_loffset, uio->uio_resid)) {
434 * If we're in FRSYNC mode, sync out this znode before reading it.
436 if (ioflag & FRSYNC || zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
437 zil_commit(zsb->z_log, zp->z_id);
440 * Lock the range against changes.
442 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
445 * If we are reading past end-of-file we can skip
446 * to the end; but we might still need to set atime.
448 if (uio->uio_loffset >= zp->z_size) {
453 ASSERT(uio->uio_loffset < zp->z_size);
454 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
456 #ifdef HAVE_UIO_ZEROCOPY
457 if ((uio->uio_extflg == UIO_XUIO) &&
458 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
460 int blksz = zp->z_blksz;
461 uint64_t offset = uio->uio_loffset;
463 xuio = (xuio_t *)uio;
465 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
468 ASSERT(offset + n <= blksz);
471 (void) dmu_xuio_init(xuio, nblk);
473 if (vn_has_cached_data(ip)) {
475 * For simplicity, we always allocate a full buffer
476 * even if we only expect to read a portion of a block.
478 while (--nblk >= 0) {
479 (void) dmu_xuio_add(xuio,
480 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
485 #endif /* HAVE_UIO_ZEROCOPY */
488 nbytes = MIN(n, zfs_read_chunk_size -
489 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
491 if (zp->z_is_mapped && !(ioflag & O_DIRECT))
492 error = mappedread(ip, nbytes, uio);
494 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
497 /* convert checksum errors into IO errors */
506 zfs_range_unlock(rl);
508 ZFS_ACCESSTIME_STAMP(zsb, zp);
509 zfs_inode_update(zp);
513 EXPORT_SYMBOL(zfs_read);
516 * Write the bytes to a file.
518 * IN: ip - inode of file to be written to.
519 * uio - structure supplying write location, range info,
521 * ioflag - FAPPEND flag set if in append mode.
522 * O_DIRECT flag; used to bypass page cache.
523 * cr - credentials of caller.
525 * OUT: uio - updated offset and range.
527 * RETURN: 0 if success
528 * error code if failure
531 * ip - ctime|mtime updated if byte count > 0
536 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
538 znode_t *zp = ITOZ(ip);
539 rlim64_t limit = uio->uio_limit;
540 ssize_t start_resid = uio->uio_resid;
544 zfs_sb_t *zsb = ZTOZSB(zp);
549 int max_blksz = zsb->z_max_blksz;
552 iovec_t *aiov = NULL;
555 iovec_t *iovp = uio->uio_iov;
558 sa_bulk_attr_t bulk[4];
559 uint64_t mtime[2], ctime[2];
560 ASSERTV(int iovcnt = uio->uio_iovcnt);
563 * Fasttrack empty write
569 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
575 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
576 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
577 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
578 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
582 * If immutable or not appending then return EPERM
584 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
585 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
586 (uio->uio_loffset < zp->z_size))) {
594 * Validate file offset
596 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
603 * Check for mandatory locks before calling zfs_range_lock()
604 * in order to prevent a deadlock with locks set via fcntl().
606 if (mandatory_lock(ip) && !lock_may_write(ip, woff, n)) {
611 #ifdef HAVE_UIO_ZEROCOPY
613 * Pre-fault the pages to ensure slow (eg NFS) pages
615 * Skip this if uio contains loaned arc_buf.
617 if ((uio->uio_extflg == UIO_XUIO) &&
618 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
619 xuio = (xuio_t *)uio;
621 uio_prefaultpages(MIN(n, max_blksz), uio);
622 #endif /* HAVE_UIO_ZEROCOPY */
625 * If in append mode, set the io offset pointer to eof.
627 if (ioflag & FAPPEND) {
629 * Obtain an appending range lock to guarantee file append
630 * semantics. We reset the write offset once we have the lock.
632 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
634 if (rl->r_len == UINT64_MAX) {
636 * We overlocked the file because this write will cause
637 * the file block size to increase.
638 * Note that zp_size cannot change with this lock held.
642 uio->uio_loffset = woff;
645 * Note that if the file block size will change as a result of
646 * this write, then this range lock will lock the entire file
647 * so that we can re-write the block safely.
649 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
653 zfs_range_unlock(rl);
658 if ((woff + n) > limit || woff > (limit - n))
661 /* Will this write extend the file length? */
662 write_eof = (woff + n > zp->z_size);
664 end_size = MAX(zp->z_size, woff + n);
667 * Write the file in reasonable size chunks. Each chunk is written
668 * in a separate transaction; this keeps the intent log records small
669 * and allows us to do more fine-grained space accounting.
673 woff = uio->uio_loffset;
675 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
676 zfs_owner_overquota(zsb, zp, B_TRUE)) {
678 dmu_return_arcbuf(abuf);
683 if (xuio && abuf == NULL) {
684 ASSERT(i_iov < iovcnt);
686 abuf = dmu_xuio_arcbuf(xuio, i_iov);
687 dmu_xuio_clear(xuio, i_iov);
688 ASSERT((aiov->iov_base == abuf->b_data) ||
689 ((char *)aiov->iov_base - (char *)abuf->b_data +
690 aiov->iov_len == arc_buf_size(abuf)));
692 } else if (abuf == NULL && n >= max_blksz &&
693 woff >= zp->z_size &&
694 P2PHASE(woff, max_blksz) == 0 &&
695 zp->z_blksz == max_blksz) {
697 * This write covers a full block. "Borrow" a buffer
698 * from the dmu so that we can fill it before we enter
699 * a transaction. This avoids the possibility of
700 * holding up the transaction if the data copy hangs
701 * up on a pagefault (e.g., from an NFS server mapping).
705 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
707 ASSERT(abuf != NULL);
708 ASSERT(arc_buf_size(abuf) == max_blksz);
709 if ((error = uiocopy(abuf->b_data, max_blksz,
710 UIO_WRITE, uio, &cbytes))) {
711 dmu_return_arcbuf(abuf);
714 ASSERT(cbytes == max_blksz);
718 * Start a transaction.
720 tx = dmu_tx_create(zsb->z_os);
721 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
722 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
723 zfs_sa_upgrade_txholds(tx, zp);
724 error = dmu_tx_assign(tx, TXG_NOWAIT);
726 if (error == ERESTART) {
733 dmu_return_arcbuf(abuf);
738 * If zfs_range_lock() over-locked we grow the blocksize
739 * and then reduce the lock range. This will only happen
740 * on the first iteration since zfs_range_reduce() will
741 * shrink down r_len to the appropriate size.
743 if (rl->r_len == UINT64_MAX) {
746 if (zp->z_blksz > max_blksz) {
747 ASSERT(!ISP2(zp->z_blksz));
748 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
750 new_blksz = MIN(end_size, max_blksz);
752 zfs_grow_blocksize(zp, new_blksz, tx);
753 zfs_range_reduce(rl, woff, n);
757 * XXX - should we really limit each write to z_max_blksz?
758 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
760 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
763 tx_bytes = uio->uio_resid;
764 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
766 tx_bytes -= uio->uio_resid;
769 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
771 * If this is not a full block write, but we are
772 * extending the file past EOF and this data starts
773 * block-aligned, use assign_arcbuf(). Otherwise,
774 * write via dmu_write().
776 if (tx_bytes < max_blksz && (!write_eof ||
777 aiov->iov_base != abuf->b_data)) {
779 dmu_write(zsb->z_os, zp->z_id, woff,
780 aiov->iov_len, aiov->iov_base, tx);
781 dmu_return_arcbuf(abuf);
782 xuio_stat_wbuf_copied();
784 ASSERT(xuio || tx_bytes == max_blksz);
785 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
788 ASSERT(tx_bytes <= uio->uio_resid);
789 uioskip(uio, tx_bytes);
792 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
793 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
796 * If we made no progress, we're done. If we made even
797 * partial progress, update the znode and ZIL accordingly.
800 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
801 (void *)&zp->z_size, sizeof (uint64_t), tx);
808 * Clear Set-UID/Set-GID bits on successful write if not
809 * privileged and at least one of the excute bits is set.
811 * It would be nice to to this after all writes have
812 * been done, but that would still expose the ISUID/ISGID
813 * to another app after the partial write is committed.
815 * Note: we don't call zfs_fuid_map_id() here because
816 * user 0 is not an ephemeral uid.
818 mutex_enter(&zp->z_acl_lock);
819 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
820 (S_IXUSR >> 6))) != 0 &&
821 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
822 secpolicy_vnode_setid_retain(cr,
823 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
825 zp->z_mode &= ~(S_ISUID | S_ISGID);
826 newmode = zp->z_mode;
827 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
828 (void *)&newmode, sizeof (uint64_t), tx);
830 mutex_exit(&zp->z_acl_lock);
832 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
836 * Update the file size (zp_size) if it has changed;
837 * account for possible concurrent updates.
839 while ((end_size = zp->z_size) < uio->uio_loffset) {
840 (void) atomic_cas_64(&zp->z_size, end_size,
845 * If we are replaying and eof is non zero then force
846 * the file size to the specified eof. Note, there's no
847 * concurrency during replay.
849 if (zsb->z_replay && zsb->z_replay_eof != 0)
850 zp->z_size = zsb->z_replay_eof;
852 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
854 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
859 ASSERT(tx_bytes == nbytes);
863 uio_prefaultpages(MIN(n, max_blksz), uio);
866 zfs_range_unlock(rl);
869 * If we're in replay mode, or we made no progress, return error.
870 * Otherwise, it's at least a partial write, so it's successful.
872 if (zsb->z_replay || uio->uio_resid == start_resid) {
877 if (ioflag & (FSYNC | FDSYNC) ||
878 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
879 zil_commit(zilog, zp->z_id);
881 zfs_inode_update(zp);
885 EXPORT_SYMBOL(zfs_write);
888 iput_async(struct inode *ip, taskq_t *taskq)
890 ASSERT(atomic_read(&ip->i_count) > 0);
891 if (atomic_read(&ip->i_count) == 1)
892 taskq_dispatch(taskq, (task_func_t *)iput, ip, TQ_PUSHPAGE);
898 zfs_get_done(zgd_t *zgd, int error)
900 znode_t *zp = zgd->zgd_private;
901 objset_t *os = ZTOZSB(zp)->z_os;
904 dmu_buf_rele(zgd->zgd_db, zgd);
906 zfs_range_unlock(zgd->zgd_rl);
909 * Release the vnode asynchronously as we currently have the
910 * txg stopped from syncing.
912 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
914 if (error == 0 && zgd->zgd_bp)
915 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
917 kmem_free(zgd, sizeof (zgd_t));
921 static int zil_fault_io = 0;
925 * Get data to generate a TX_WRITE intent log record.
928 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
931 objset_t *os = zsb->z_os;
933 uint64_t object = lr->lr_foid;
934 uint64_t offset = lr->lr_offset;
935 uint64_t size = lr->lr_length;
936 blkptr_t *bp = &lr->lr_blkptr;
945 * Nothing to do if the file has been removed
947 if (zfs_zget(zsb, object, &zp) != 0)
949 if (zp->z_unlinked) {
951 * Release the vnode asynchronously as we currently have the
952 * txg stopped from syncing.
954 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
958 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_PUSHPAGE);
959 zgd->zgd_zilog = zsb->z_log;
960 zgd->zgd_private = zp;
963 * Write records come in two flavors: immediate and indirect.
964 * For small writes it's cheaper to store the data with the
965 * log record (immediate); for large writes it's cheaper to
966 * sync the data and get a pointer to it (indirect) so that
967 * we don't have to write the data twice.
969 if (buf != NULL) { /* immediate write */
970 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
971 /* test for truncation needs to be done while range locked */
972 if (offset >= zp->z_size) {
975 error = dmu_read(os, object, offset, size, buf,
976 DMU_READ_NO_PREFETCH);
978 ASSERT(error == 0 || error == ENOENT);
979 } else { /* indirect write */
981 * Have to lock the whole block to ensure when it's
982 * written out and it's checksum is being calculated
983 * that no one can change the data. We need to re-check
984 * blocksize after we get the lock in case it's changed!
989 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
991 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
993 if (zp->z_blksz == size)
996 zfs_range_unlock(zgd->zgd_rl);
998 /* test for truncation needs to be done while range locked */
999 if (lr->lr_offset >= zp->z_size)
1008 error = dmu_buf_hold(os, object, offset, zgd, &db,
1009 DMU_READ_NO_PREFETCH);
1015 ASSERT(db->db_offset == offset);
1016 ASSERT(db->db_size == size);
1018 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1020 ASSERT(error || lr->lr_length <= zp->z_blksz);
1023 * On success, we need to wait for the write I/O
1024 * initiated by dmu_sync() to complete before we can
1025 * release this dbuf. We will finish everything up
1026 * in the zfs_get_done() callback.
1031 if (error == EALREADY) {
1032 lr->lr_common.lrc_txtype = TX_WRITE2;
1038 zfs_get_done(zgd, error);
1045 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1047 znode_t *zp = ITOZ(ip);
1048 zfs_sb_t *zsb = ITOZSB(ip);
1054 if (flag & V_ACE_MASK)
1055 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1057 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1062 EXPORT_SYMBOL(zfs_access);
1065 * Lookup an entry in a directory, or an extended attribute directory.
1066 * If it exists, return a held inode reference for it.
1068 * IN: dip - inode of directory to search.
1069 * nm - name of entry to lookup.
1070 * flags - LOOKUP_XATTR set if looking for an attribute.
1071 * cr - credentials of caller.
1072 * direntflags - directory lookup flags
1073 * realpnp - returned pathname.
1075 * OUT: ipp - inode of located entry, NULL if not found.
1077 * RETURN: 0 if success
1078 * error code if failure
1085 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1086 cred_t *cr, int *direntflags, pathname_t *realpnp)
1088 znode_t *zdp = ITOZ(dip);
1089 zfs_sb_t *zsb = ITOZSB(dip);
1093 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1095 if (!S_ISDIR(dip->i_mode)) {
1097 } else if (zdp->z_sa_hdl == NULL) {
1101 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1102 error = zfs_fastaccesschk_execute(zdp, cr);
1111 vnode_t *tvp = dnlc_lookup(dvp, nm);
1114 error = zfs_fastaccesschk_execute(zdp, cr);
1119 if (tvp == DNLC_NO_VNODE) {
1124 return (specvp_check(vpp, cr));
1127 #endif /* HAVE_DNLC */
1136 if (flags & LOOKUP_XATTR) {
1138 * We don't allow recursive attributes..
1139 * Maybe someday we will.
1141 if (zdp->z_pflags & ZFS_XATTR) {
1146 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1152 * Do we have permission to get into attribute directory?
1155 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1165 if (!S_ISDIR(dip->i_mode)) {
1171 * Check accessibility of directory.
1174 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1179 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
1180 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1185 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1186 if ((error == 0) && (*ipp))
1187 zfs_inode_update(ITOZ(*ipp));
1192 EXPORT_SYMBOL(zfs_lookup);
1195 * Attempt to create a new entry in a directory. If the entry
1196 * already exists, truncate the file if permissible, else return
1197 * an error. Return the ip of the created or trunc'd file.
1199 * IN: dip - inode of directory to put new file entry in.
1200 * name - name of new file entry.
1201 * vap - attributes of new file.
1202 * excl - flag indicating exclusive or non-exclusive mode.
1203 * mode - mode to open file with.
1204 * cr - credentials of caller.
1205 * flag - large file flag [UNUSED].
1206 * vsecp - ACL to be set
1208 * OUT: ipp - inode of created or trunc'd entry.
1210 * RETURN: 0 if success
1211 * error code if failure
1214 * dip - ctime|mtime updated if new entry created
1215 * ip - ctime|mtime always, atime if new
1220 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1221 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1223 znode_t *zp, *dzp = ITOZ(dip);
1224 zfs_sb_t *zsb = ITOZSB(dip);
1232 zfs_acl_ids_t acl_ids;
1233 boolean_t fuid_dirtied;
1234 boolean_t have_acl = B_FALSE;
1237 * If we have an ephemeral id, ACL, or XVATTR then
1238 * make sure file system is at proper version
1244 if (zsb->z_use_fuids == B_FALSE &&
1245 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1253 if (zsb->z_utf8 && u8_validate(name, strlen(name),
1254 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1259 if (vap->va_mask & ATTR_XVATTR) {
1260 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1261 crgetuid(cr), cr, vap->va_mode)) != 0) {
1269 if (*name == '\0') {
1271 * Null component name refers to the directory itself.
1278 /* possible igrab(zp) */
1281 if (flag & FIGNORECASE)
1284 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1288 zfs_acl_ids_free(&acl_ids);
1289 if (strcmp(name, "..") == 0)
1300 * Create a new file object and update the directory
1303 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1305 zfs_acl_ids_free(&acl_ids);
1310 * We only support the creation of regular files in
1311 * extended attribute directories.
1314 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1316 zfs_acl_ids_free(&acl_ids);
1321 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1322 cr, vsecp, &acl_ids)) != 0)
1326 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1327 zfs_acl_ids_free(&acl_ids);
1332 tx = dmu_tx_create(os);
1334 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1335 ZFS_SA_BASE_ATTR_SIZE);
1337 fuid_dirtied = zsb->z_fuid_dirty;
1339 zfs_fuid_txhold(zsb, tx);
1340 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1341 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1342 if (!zsb->z_use_sa &&
1343 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1344 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1345 0, acl_ids.z_aclp->z_acl_bytes);
1347 error = dmu_tx_assign(tx, TXG_NOWAIT);
1349 zfs_dirent_unlock(dl);
1350 if (error == ERESTART) {
1355 zfs_acl_ids_free(&acl_ids);
1360 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1363 zfs_fuid_sync(zsb, tx);
1365 (void) zfs_link_create(dl, zp, tx, ZNEW);
1366 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1367 if (flag & FIGNORECASE)
1369 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1370 vsecp, acl_ids.z_fuidp, vap);
1371 zfs_acl_ids_free(&acl_ids);
1374 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1377 zfs_acl_ids_free(&acl_ids);
1381 * A directory entry already exists for this name.
1384 * Can't truncate an existing file if in exclusive mode.
1391 * Can't open a directory for writing.
1393 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1398 * Verify requested access to file.
1400 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1404 mutex_enter(&dzp->z_lock);
1406 mutex_exit(&dzp->z_lock);
1409 * Truncate regular files if requested.
1411 if (S_ISREG(ZTOI(zp)->i_mode) &&
1412 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1413 /* we can't hold any locks when calling zfs_freesp() */
1414 zfs_dirent_unlock(dl);
1416 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1422 zfs_dirent_unlock(dl);
1428 zfs_inode_update(dzp);
1429 zfs_inode_update(zp);
1433 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1434 zil_commit(zilog, 0);
1439 EXPORT_SYMBOL(zfs_create);
1442 * Remove an entry from a directory.
1444 * IN: dip - inode of directory to remove entry from.
1445 * name - name of entry to remove.
1446 * cr - credentials of caller.
1448 * RETURN: 0 if success
1449 * error code if failure
1453 * ip - ctime (if nlink > 0)
1456 uint64_t null_xattr = 0;
1460 zfs_remove(struct inode *dip, char *name, cred_t *cr)
1462 znode_t *zp, *dzp = ITOZ(dip);
1465 zfs_sb_t *zsb = ITOZSB(dip);
1468 uint64_t xattr_obj_unlinked = 0;
1474 pathname_t *realnmp = NULL;
1475 #ifdef HAVE_PN_UTILS
1477 #endif /* HAVE_PN_UTILS */
1485 #ifdef HAVE_PN_UTILS
1486 if (flags & FIGNORECASE) {
1491 #endif /* HAVE_PN_UTILS */
1497 * Attempt to lock directory; fail if entry doesn't exist.
1499 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1501 #ifdef HAVE_PN_UTILS
1504 #endif /* HAVE_PN_UTILS */
1511 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1516 * Need to use rmdir for removing directories.
1518 if (S_ISDIR(ip->i_mode)) {
1525 dnlc_remove(dvp, realnmp->pn_buf);
1527 dnlc_remove(dvp, name);
1528 #endif /* HAVE_DNLC */
1531 * We never delete the znode and always place it in the unlinked
1532 * set. The dentry cache will always hold the last reference and
1533 * is responsible for safely freeing the znode.
1536 tx = dmu_tx_create(zsb->z_os);
1537 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1538 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1539 zfs_sa_upgrade_txholds(tx, zp);
1540 zfs_sa_upgrade_txholds(tx, dzp);
1542 /* are there any extended attributes? */
1543 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1544 &xattr_obj, sizeof (xattr_obj));
1545 if (error == 0 && xattr_obj) {
1546 error = zfs_zget(zsb, xattr_obj, &xzp);
1547 ASSERT3U(error, ==, 0);
1548 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1549 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1552 /* charge as an update -- would be nice not to charge at all */
1553 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1555 error = dmu_tx_assign(tx, TXG_NOWAIT);
1557 zfs_dirent_unlock(dl);
1561 if (error == ERESTART) {
1566 #ifdef HAVE_PN_UTILS
1569 #endif /* HAVE_PN_UTILS */
1576 * Remove the directory entry.
1578 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1587 * Hold z_lock so that we can make sure that the ACL obj
1588 * hasn't changed. Could have been deleted due to
1591 mutex_enter(&zp->z_lock);
1592 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1593 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1594 mutex_exit(&zp->z_lock);
1595 zfs_unlinked_add(zp, tx);
1599 #ifdef HAVE_PN_UTILS
1600 if (flags & FIGNORECASE)
1602 #endif /* HAVE_PN_UTILS */
1603 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1607 #ifdef HAVE_PN_UTILS
1610 #endif /* HAVE_PN_UTILS */
1612 zfs_dirent_unlock(dl);
1613 zfs_inode_update(dzp);
1614 zfs_inode_update(zp);
1616 zfs_inode_update(xzp);
1622 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1623 zil_commit(zilog, 0);
1628 EXPORT_SYMBOL(zfs_remove);
1631 * Create a new directory and insert it into dip using the name
1632 * provided. Return a pointer to the inserted directory.
1634 * IN: dip - inode of directory to add subdir to.
1635 * dirname - name of new directory.
1636 * vap - attributes of new directory.
1637 * cr - credentials of caller.
1638 * vsecp - ACL to be set
1640 * OUT: ipp - inode of created directory.
1642 * RETURN: 0 if success
1643 * error code if failure
1646 * dip - ctime|mtime updated
1647 * ipp - ctime|mtime|atime updated
1651 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1652 cred_t *cr, int flags, vsecattr_t *vsecp)
1654 znode_t *zp, *dzp = ITOZ(dip);
1655 zfs_sb_t *zsb = ITOZSB(dip);
1663 gid_t gid = crgetgid(cr);
1664 zfs_acl_ids_t acl_ids;
1665 boolean_t fuid_dirtied;
1667 ASSERT(S_ISDIR(vap->va_mode));
1670 * If we have an ephemeral id, ACL, or XVATTR then
1671 * make sure file system is at proper version
1675 if (zsb->z_use_fuids == B_FALSE &&
1676 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1683 if (dzp->z_pflags & ZFS_XATTR) {
1688 if (zsb->z_utf8 && u8_validate(dirname,
1689 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1693 if (flags & FIGNORECASE)
1696 if (vap->va_mask & ATTR_XVATTR) {
1697 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1698 crgetuid(cr), cr, vap->va_mode)) != 0) {
1704 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1705 vsecp, &acl_ids)) != 0) {
1710 * First make sure the new directory doesn't exist.
1712 * Existence is checked first to make sure we don't return
1713 * EACCES instead of EEXIST which can cause some applications
1719 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1721 zfs_acl_ids_free(&acl_ids);
1726 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1727 zfs_acl_ids_free(&acl_ids);
1728 zfs_dirent_unlock(dl);
1733 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1734 zfs_acl_ids_free(&acl_ids);
1735 zfs_dirent_unlock(dl);
1741 * Add a new entry to the directory.
1743 tx = dmu_tx_create(zsb->z_os);
1744 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1745 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1746 fuid_dirtied = zsb->z_fuid_dirty;
1748 zfs_fuid_txhold(zsb, tx);
1749 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1750 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1751 acl_ids.z_aclp->z_acl_bytes);
1754 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1755 ZFS_SA_BASE_ATTR_SIZE);
1757 error = dmu_tx_assign(tx, TXG_NOWAIT);
1759 zfs_dirent_unlock(dl);
1760 if (error == ERESTART) {
1765 zfs_acl_ids_free(&acl_ids);
1774 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1777 zfs_fuid_sync(zsb, tx);
1780 * Now put new name in parent dir.
1782 (void) zfs_link_create(dl, zp, tx, ZNEW);
1786 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1787 if (flags & FIGNORECASE)
1789 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1790 acl_ids.z_fuidp, vap);
1792 zfs_acl_ids_free(&acl_ids);
1796 zfs_dirent_unlock(dl);
1798 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1799 zil_commit(zilog, 0);
1801 zfs_inode_update(dzp);
1802 zfs_inode_update(zp);
1806 EXPORT_SYMBOL(zfs_mkdir);
1809 * Remove a directory subdir entry. If the current working
1810 * directory is the same as the subdir to be removed, the
1813 * IN: dip - inode of directory to remove from.
1814 * name - name of directory to be removed.
1815 * cwd - inode of current working directory.
1816 * cr - credentials of caller.
1817 * flags - case flags
1819 * RETURN: 0 if success
1820 * error code if failure
1823 * dip - ctime|mtime updated
1827 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1830 znode_t *dzp = ITOZ(dip);
1833 zfs_sb_t *zsb = ITOZSB(dip);
1844 if (flags & FIGNORECASE)
1850 * Attempt to lock directory; fail if entry doesn't exist.
1852 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1860 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1864 if (!S_ISDIR(ip->i_mode)) {
1875 * Grab a lock on the directory to make sure that noone is
1876 * trying to add (or lookup) entries while we are removing it.
1878 rw_enter(&zp->z_name_lock, RW_WRITER);
1881 * Grab a lock on the parent pointer to make sure we play well
1882 * with the treewalk and directory rename code.
1884 rw_enter(&zp->z_parent_lock, RW_WRITER);
1886 tx = dmu_tx_create(zsb->z_os);
1887 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1888 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1889 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1890 zfs_sa_upgrade_txholds(tx, zp);
1891 zfs_sa_upgrade_txholds(tx, dzp);
1892 error = dmu_tx_assign(tx, TXG_NOWAIT);
1894 rw_exit(&zp->z_parent_lock);
1895 rw_exit(&zp->z_name_lock);
1896 zfs_dirent_unlock(dl);
1898 if (error == ERESTART) {
1908 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1911 uint64_t txtype = TX_RMDIR;
1912 if (flags & FIGNORECASE)
1914 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
1919 rw_exit(&zp->z_parent_lock);
1920 rw_exit(&zp->z_name_lock);
1922 zfs_dirent_unlock(dl);
1924 zfs_inode_update(dzp);
1925 zfs_inode_update(zp);
1928 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1929 zil_commit(zilog, 0);
1934 EXPORT_SYMBOL(zfs_rmdir);
1937 * Read as many directory entries as will fit into the provided
1938 * dirent buffer from the given directory cursor position.
1940 * IN: ip - inode of directory to read.
1941 * dirent - buffer for directory entries.
1943 * OUT: dirent - filler buffer of directory entries.
1945 * RETURN: 0 if success
1946 * error code if failure
1949 * ip - atime updated
1951 * Note that the low 4 bits of the cookie returned by zap is always zero.
1952 * This allows us to use the low range for "special" directory entries:
1953 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1954 * we use the offset 2 for the '.zfs' directory.
1958 zfs_readdir(struct inode *ip, void *dirent, filldir_t filldir,
1959 loff_t *pos, cred_t *cr)
1961 znode_t *zp = ITOZ(ip);
1962 zfs_sb_t *zsb = ITOZSB(ip);
1965 zap_attribute_t zap;
1975 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
1976 &parent, sizeof (parent))) != 0)
1980 * Quit if directory has been removed (posix)
1987 prefetch = zp->z_zn_prefetch;
1990 * Initialize the iterator cursor.
1994 * Start iteration from the beginning of the directory.
1996 zap_cursor_init(&zc, os, zp->z_id);
1999 * The offset is a serialized cursor.
2001 zap_cursor_init_serialized(&zc, os, zp->z_id, *pos);
2005 * Transform to file-system independent format
2012 * Special case `.', `..', and `.zfs'.
2015 (void) strcpy(zap.za_name, ".");
2016 zap.za_normalization_conflict = 0;
2018 } else if (*pos == 1) {
2019 (void) strcpy(zap.za_name, "..");
2020 zap.za_normalization_conflict = 0;
2022 } else if (*pos == 2 && zfs_show_ctldir(zp)) {
2023 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2024 zap.za_normalization_conflict = 0;
2025 objnum = ZFSCTL_INO_ROOT;
2030 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2031 if (error == ENOENT)
2038 * Allow multiple entries provided the first entry is
2039 * the object id. Non-zpl consumers may safely make
2040 * use of the additional space.
2042 * XXX: This should be a feature flag for compatibility
2044 if (zap.za_integer_length != 8 ||
2045 zap.za_num_integers == 0) {
2046 cmn_err(CE_WARN, "zap_readdir: bad directory "
2047 "entry, obj = %lld, offset = %lld, "
2048 "length = %d, num = %lld\n",
2049 (u_longlong_t)zp->z_id,
2051 zap.za_integer_length,
2052 (u_longlong_t)zap.za_num_integers);
2057 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2059 done = filldir(dirent, zap.za_name, strlen(zap.za_name),
2060 zap_cursor_serialize(&zc), objnum, 0);
2065 /* Prefetch znode */
2067 dmu_prefetch(os, objnum, 0, 0);
2070 if (*pos > 2 || (*pos == 2 && !zfs_show_ctldir(zp))) {
2071 zap_cursor_advance(&zc);
2072 *pos = zap_cursor_serialize(&zc);
2077 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2080 zap_cursor_fini(&zc);
2081 if (error == ENOENT)
2084 ZFS_ACCESSTIME_STAMP(zsb, zp);
2085 zfs_inode_update(zp);
2092 EXPORT_SYMBOL(zfs_readdir);
2094 ulong_t zfs_fsync_sync_cnt = 4;
2097 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2099 znode_t *zp = ITOZ(ip);
2100 zfs_sb_t *zsb = ITOZSB(ip);
2102 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2104 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2107 zil_commit(zsb->z_log, zp->z_id);
2112 EXPORT_SYMBOL(zfs_fsync);
2116 * Get the requested file attributes and place them in the provided
2119 * IN: ip - inode of file.
2120 * vap - va_mask identifies requested attributes.
2121 * If ATTR_XVATTR set, then optional attrs are requested
2122 * flags - ATTR_NOACLCHECK (CIFS server context)
2123 * cr - credentials of caller.
2125 * OUT: vap - attribute values.
2127 * RETURN: 0 (always succeeds)
2131 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2133 znode_t *zp = ITOZ(ip);
2134 zfs_sb_t *zsb = ITOZSB(ip);
2137 uint64_t mtime[2], ctime[2];
2138 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2139 xoptattr_t *xoap = NULL;
2140 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2141 sa_bulk_attr_t bulk[2];
2147 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2149 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2150 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2152 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2158 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2159 * Also, if we are the owner don't bother, since owner should
2160 * always be allowed to read basic attributes of file.
2162 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2163 (vap->va_uid != crgetuid(cr))) {
2164 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2172 * Return all attributes. It's cheaper to provide the answer
2173 * than to determine whether we were asked the question.
2176 mutex_enter(&zp->z_lock);
2177 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2178 vap->va_mode = zp->z_mode;
2179 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2180 vap->va_nodeid = zp->z_id;
2181 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2182 links = zp->z_links + 1;
2184 links = zp->z_links;
2185 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2186 vap->va_size = i_size_read(ip);
2187 vap->va_rdev = ip->i_rdev;
2188 vap->va_seq = ip->i_generation;
2191 * Add in any requested optional attributes and the create time.
2192 * Also set the corresponding bits in the returned attribute bitmap.
2194 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2195 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2197 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2198 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2201 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2202 xoap->xoa_readonly =
2203 ((zp->z_pflags & ZFS_READONLY) != 0);
2204 XVA_SET_RTN(xvap, XAT_READONLY);
2207 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2209 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2210 XVA_SET_RTN(xvap, XAT_SYSTEM);
2213 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2215 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2216 XVA_SET_RTN(xvap, XAT_HIDDEN);
2219 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2220 xoap->xoa_nounlink =
2221 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2222 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2225 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2226 xoap->xoa_immutable =
2227 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2228 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2231 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2232 xoap->xoa_appendonly =
2233 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2234 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2237 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2239 ((zp->z_pflags & ZFS_NODUMP) != 0);
2240 XVA_SET_RTN(xvap, XAT_NODUMP);
2243 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2245 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2246 XVA_SET_RTN(xvap, XAT_OPAQUE);
2249 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2250 xoap->xoa_av_quarantined =
2251 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2252 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2255 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2256 xoap->xoa_av_modified =
2257 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2258 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2261 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2262 S_ISREG(ip->i_mode)) {
2263 zfs_sa_get_scanstamp(zp, xvap);
2266 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2269 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2270 times, sizeof (times));
2271 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2272 XVA_SET_RTN(xvap, XAT_CREATETIME);
2275 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2276 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2277 XVA_SET_RTN(xvap, XAT_REPARSE);
2279 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2280 xoap->xoa_generation = zp->z_gen;
2281 XVA_SET_RTN(xvap, XAT_GEN);
2284 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2286 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2287 XVA_SET_RTN(xvap, XAT_OFFLINE);
2290 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2292 ((zp->z_pflags & ZFS_SPARSE) != 0);
2293 XVA_SET_RTN(xvap, XAT_SPARSE);
2297 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2298 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2299 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2301 mutex_exit(&zp->z_lock);
2303 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2305 if (zp->z_blksz == 0) {
2307 * Block size hasn't been set; suggest maximal I/O transfers.
2309 vap->va_blksize = zsb->z_max_blksz;
2315 EXPORT_SYMBOL(zfs_getattr);
2318 * Get the basic file attributes and place them in the provided kstat
2319 * structure. The inode is assumed to be the authoritative source
2320 * for most of the attributes. However, the znode currently has the
2321 * authoritative atime, blksize, and block count.
2323 * IN: ip - inode of file.
2325 * OUT: sp - kstat values.
2327 * RETURN: 0 (always succeeds)
2331 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2333 znode_t *zp = ITOZ(ip);
2334 zfs_sb_t *zsb = ITOZSB(ip);
2339 mutex_enter(&zp->z_lock);
2341 generic_fillattr(ip, sp);
2342 ZFS_TIME_DECODE(&sp->atime, zp->z_atime);
2344 sa_object_size(zp->z_sa_hdl, (uint32_t *)&sp->blksize, &sp->blocks);
2345 if (unlikely(zp->z_blksz == 0)) {
2347 * Block size hasn't been set; suggest maximal I/O transfers.
2349 sp->blksize = zsb->z_max_blksz;
2352 mutex_exit(&zp->z_lock);
2358 EXPORT_SYMBOL(zfs_getattr_fast);
2361 * Set the file attributes to the values contained in the
2364 * IN: ip - inode of file to be modified.
2365 * vap - new attribute values.
2366 * If ATTR_XVATTR set, then optional attrs are being set
2367 * flags - ATTR_UTIME set if non-default time values provided.
2368 * - ATTR_NOACLCHECK (CIFS context only).
2369 * cr - credentials of caller.
2371 * RETURN: 0 if success
2372 * error code if failure
2375 * ip - ctime updated, mtime updated if size changed.
2379 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2381 znode_t *zp = ITOZ(ip);
2382 zfs_sb_t *zsb = ITOZSB(ip);
2386 xvattr_t *tmpxvattr;
2387 uint_t mask = vap->va_mask;
2391 uint64_t new_uid, new_gid;
2393 uint64_t mtime[2], ctime[2];
2395 int need_policy = FALSE;
2397 zfs_fuid_info_t *fuidp = NULL;
2398 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2401 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2402 boolean_t fuid_dirtied = B_FALSE;
2403 sa_bulk_attr_t *bulk, *xattr_bulk;
2404 int count = 0, xattr_count = 0;
2415 * Make sure that if we have ephemeral uid/gid or xvattr specified
2416 * that file system is at proper version level
2419 if (zsb->z_use_fuids == B_FALSE &&
2420 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2421 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2422 (mask & ATTR_XVATTR))) {
2427 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2432 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2438 * If this is an xvattr_t, then get a pointer to the structure of
2439 * optional attributes. If this is NULL, then we have a vattr_t.
2441 xoap = xva_getxoptattr(xvap);
2443 tmpxvattr = kmem_alloc(sizeof(xvattr_t), KM_SLEEP);
2444 xva_init(tmpxvattr);
2446 bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2447 xattr_bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2450 * Immutable files can only alter immutable bit and atime
2452 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2453 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2454 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2459 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2465 * Verify timestamps doesn't overflow 32 bits.
2466 * ZFS can handle large timestamps, but 32bit syscalls can't
2467 * handle times greater than 2039. This check should be removed
2468 * once large timestamps are fully supported.
2470 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2471 if (((mask & ATTR_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2472 ((mask & ATTR_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2482 /* Can this be moved to before the top label? */
2483 if (zfs_is_readonly(zsb)) {
2489 * First validate permissions
2492 if (mask & ATTR_SIZE) {
2493 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2497 truncate_setsize(ip, vap->va_size);
2500 * XXX - Note, we are not providing any open
2501 * mode flags here (like FNDELAY), so we may
2502 * block if there are locks present... this
2503 * should be addressed in openat().
2505 /* XXX - would it be OK to generate a log record here? */
2506 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2511 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2512 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2513 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2514 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2515 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2516 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2517 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2518 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2519 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2523 if (mask & (ATTR_UID|ATTR_GID)) {
2524 int idmask = (mask & (ATTR_UID|ATTR_GID));
2529 * NOTE: even if a new mode is being set,
2530 * we may clear S_ISUID/S_ISGID bits.
2533 if (!(mask & ATTR_MODE))
2534 vap->va_mode = zp->z_mode;
2537 * Take ownership or chgrp to group we are a member of
2540 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2541 take_group = (mask & ATTR_GID) &&
2542 zfs_groupmember(zsb, vap->va_gid, cr);
2545 * If both ATTR_UID and ATTR_GID are set then take_owner and
2546 * take_group must both be set in order to allow taking
2549 * Otherwise, send the check through secpolicy_vnode_setattr()
2553 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2554 take_owner && take_group) ||
2555 ((idmask == ATTR_UID) && take_owner) ||
2556 ((idmask == ATTR_GID) && take_group)) {
2557 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2558 skipaclchk, cr) == 0) {
2560 * Remove setuid/setgid for non-privileged users
2562 (void) secpolicy_setid_clear(vap, cr);
2563 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2572 mutex_enter(&zp->z_lock);
2573 oldva.va_mode = zp->z_mode;
2574 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2575 if (mask & ATTR_XVATTR) {
2577 * Update xvattr mask to include only those attributes
2578 * that are actually changing.
2580 * the bits will be restored prior to actually setting
2581 * the attributes so the caller thinks they were set.
2583 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2584 if (xoap->xoa_appendonly !=
2585 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2588 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2589 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2593 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2594 if (xoap->xoa_nounlink !=
2595 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2598 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2599 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2603 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2604 if (xoap->xoa_immutable !=
2605 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2608 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2609 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2613 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2614 if (xoap->xoa_nodump !=
2615 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2618 XVA_CLR_REQ(xvap, XAT_NODUMP);
2619 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2623 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2624 if (xoap->xoa_av_modified !=
2625 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2628 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2629 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2633 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2634 if ((!S_ISREG(ip->i_mode) &&
2635 xoap->xoa_av_quarantined) ||
2636 xoap->xoa_av_quarantined !=
2637 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2640 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2641 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2645 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2646 mutex_exit(&zp->z_lock);
2651 if (need_policy == FALSE &&
2652 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2653 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2658 mutex_exit(&zp->z_lock);
2660 if (mask & ATTR_MODE) {
2661 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2662 err = secpolicy_setid_setsticky_clear(ip, vap,
2667 trim_mask |= ATTR_MODE;
2675 * If trim_mask is set then take ownership
2676 * has been granted or write_acl is present and user
2677 * has the ability to modify mode. In that case remove
2678 * UID|GID and or MODE from mask so that
2679 * secpolicy_vnode_setattr() doesn't revoke it.
2683 saved_mask = vap->va_mask;
2684 vap->va_mask &= ~trim_mask;
2686 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2687 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2692 vap->va_mask |= saved_mask;
2696 * secpolicy_vnode_setattr, or take ownership may have
2699 mask = vap->va_mask;
2701 if ((mask & (ATTR_UID | ATTR_GID))) {
2702 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2703 &xattr_obj, sizeof (xattr_obj));
2705 if (err == 0 && xattr_obj) {
2706 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2710 if (mask & ATTR_UID) {
2711 new_uid = zfs_fuid_create(zsb,
2712 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2713 if (new_uid != zp->z_uid &&
2714 zfs_fuid_overquota(zsb, B_FALSE, new_uid)) {
2722 if (mask & ATTR_GID) {
2723 new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2724 cr, ZFS_GROUP, &fuidp);
2725 if (new_gid != zp->z_gid &&
2726 zfs_fuid_overquota(zsb, B_TRUE, new_gid)) {
2734 tx = dmu_tx_create(zsb->z_os);
2736 if (mask & ATTR_MODE) {
2737 uint64_t pmode = zp->z_mode;
2739 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2741 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2743 mutex_enter(&zp->z_lock);
2744 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2746 * Are we upgrading ACL from old V0 format
2749 if (zsb->z_version >= ZPL_VERSION_FUID &&
2750 zfs_znode_acl_version(zp) ==
2751 ZFS_ACL_VERSION_INITIAL) {
2752 dmu_tx_hold_free(tx, acl_obj, 0,
2754 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2755 0, aclp->z_acl_bytes);
2757 dmu_tx_hold_write(tx, acl_obj, 0,
2760 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2761 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2762 0, aclp->z_acl_bytes);
2764 mutex_exit(&zp->z_lock);
2765 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2767 if ((mask & ATTR_XVATTR) &&
2768 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2769 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2771 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2775 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2778 fuid_dirtied = zsb->z_fuid_dirty;
2780 zfs_fuid_txhold(zsb, tx);
2782 zfs_sa_upgrade_txholds(tx, zp);
2784 err = dmu_tx_assign(tx, TXG_NOWAIT);
2786 if (err == ERESTART)
2793 * Set each attribute requested.
2794 * We group settings according to the locks they need to acquire.
2796 * Note: you cannot set ctime directly, although it will be
2797 * updated as a side-effect of calling this function.
2801 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2802 mutex_enter(&zp->z_acl_lock);
2803 mutex_enter(&zp->z_lock);
2805 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2806 &zp->z_pflags, sizeof (zp->z_pflags));
2809 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2810 mutex_enter(&attrzp->z_acl_lock);
2811 mutex_enter(&attrzp->z_lock);
2812 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2813 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2814 sizeof (attrzp->z_pflags));
2817 if (mask & (ATTR_UID|ATTR_GID)) {
2819 if (mask & ATTR_UID) {
2820 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2821 &new_uid, sizeof (new_uid));
2822 zp->z_uid = new_uid;
2824 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2825 SA_ZPL_UID(zsb), NULL, &new_uid,
2827 attrzp->z_uid = new_uid;
2831 if (mask & ATTR_GID) {
2832 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2833 NULL, &new_gid, sizeof (new_gid));
2834 zp->z_gid = new_gid;
2836 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2837 SA_ZPL_GID(zsb), NULL, &new_gid,
2839 attrzp->z_gid = new_gid;
2842 if (!(mask & ATTR_MODE)) {
2843 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2844 NULL, &new_mode, sizeof (new_mode));
2845 new_mode = zp->z_mode;
2847 err = zfs_acl_chown_setattr(zp);
2850 err = zfs_acl_chown_setattr(attrzp);
2855 if (mask & ATTR_MODE) {
2856 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
2857 &new_mode, sizeof (new_mode));
2858 zp->z_mode = new_mode;
2859 ASSERT3P(aclp, !=, NULL);
2860 err = zfs_aclset_common(zp, aclp, cr, tx);
2861 ASSERT3U(err, ==, 0);
2862 if (zp->z_acl_cached)
2863 zfs_acl_free(zp->z_acl_cached);
2864 zp->z_acl_cached = aclp;
2869 if (mask & ATTR_ATIME) {
2870 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2871 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
2872 &zp->z_atime, sizeof (zp->z_atime));
2875 if (mask & ATTR_MTIME) {
2876 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2877 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
2878 mtime, sizeof (mtime));
2881 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2882 if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) {
2883 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
2884 NULL, mtime, sizeof (mtime));
2885 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2886 &ctime, sizeof (ctime));
2887 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
2889 } else if (mask != 0) {
2890 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2891 &ctime, sizeof (ctime));
2892 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
2895 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2896 SA_ZPL_CTIME(zsb), NULL,
2897 &ctime, sizeof (ctime));
2898 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2899 mtime, ctime, B_TRUE);
2903 * Do this after setting timestamps to prevent timestamp
2904 * update from toggling bit
2907 if (xoap && (mask & ATTR_XVATTR)) {
2910 * restore trimmed off masks
2911 * so that return masks can be set for caller.
2914 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
2915 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2917 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
2918 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2920 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
2921 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2923 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
2924 XVA_SET_REQ(xvap, XAT_NODUMP);
2926 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
2927 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2929 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
2930 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2933 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2934 ASSERT(S_ISREG(ip->i_mode));
2936 zfs_xvattr_set(zp, xvap, tx);
2940 zfs_fuid_sync(zsb, tx);
2943 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2945 mutex_exit(&zp->z_lock);
2946 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2947 mutex_exit(&zp->z_acl_lock);
2950 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2951 mutex_exit(&attrzp->z_acl_lock);
2952 mutex_exit(&attrzp->z_lock);
2955 if (err == 0 && attrzp) {
2956 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2967 zfs_fuid_info_free(fuidp);
2973 if (err == ERESTART)
2976 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2978 zfs_inode_update(zp);
2982 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
2983 zil_commit(zilog, 0);
2986 kmem_free(xattr_bulk, sizeof(sa_bulk_attr_t) * 7);
2987 kmem_free(bulk, sizeof(sa_bulk_attr_t) * 7);
2988 kmem_free(tmpxvattr, sizeof(xvattr_t));
2992 EXPORT_SYMBOL(zfs_setattr);
2994 typedef struct zfs_zlock {
2995 krwlock_t *zl_rwlock; /* lock we acquired */
2996 znode_t *zl_znode; /* znode we held */
2997 struct zfs_zlock *zl_next; /* next in list */
3001 * Drop locks and release vnodes that were held by zfs_rename_lock().
3004 zfs_rename_unlock(zfs_zlock_t **zlpp)
3008 while ((zl = *zlpp) != NULL) {
3009 if (zl->zl_znode != NULL)
3010 iput(ZTOI(zl->zl_znode));
3011 rw_exit(zl->zl_rwlock);
3012 *zlpp = zl->zl_next;
3013 kmem_free(zl, sizeof (*zl));
3018 * Search back through the directory tree, using the ".." entries.
3019 * Lock each directory in the chain to prevent concurrent renames.
3020 * Fail any attempt to move a directory into one of its own descendants.
3021 * XXX - z_parent_lock can overlap with map or grow locks
3024 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3028 uint64_t rootid = ZTOZSB(zp)->z_root;
3029 uint64_t oidp = zp->z_id;
3030 krwlock_t *rwlp = &szp->z_parent_lock;
3031 krw_t rw = RW_WRITER;
3034 * First pass write-locks szp and compares to zp->z_id.
3035 * Later passes read-lock zp and compare to zp->z_parent.
3038 if (!rw_tryenter(rwlp, rw)) {
3040 * Another thread is renaming in this path.
3041 * Note that if we are a WRITER, we don't have any
3042 * parent_locks held yet.
3044 if (rw == RW_READER && zp->z_id > szp->z_id) {
3046 * Drop our locks and restart
3048 zfs_rename_unlock(&zl);
3052 rwlp = &szp->z_parent_lock;
3057 * Wait for other thread to drop its locks
3063 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3064 zl->zl_rwlock = rwlp;
3065 zl->zl_znode = NULL;
3066 zl->zl_next = *zlpp;
3069 if (oidp == szp->z_id) /* We're a descendant of szp */
3072 if (oidp == rootid) /* We've hit the top */
3075 if (rw == RW_READER) { /* i.e. not the first pass */
3076 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3081 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3082 &oidp, sizeof (oidp));
3083 rwlp = &zp->z_parent_lock;
3086 } while (zp->z_id != sdzp->z_id);
3092 * Move an entry from the provided source directory to the target
3093 * directory. Change the entry name as indicated.
3095 * IN: sdip - Source directory containing the "old entry".
3096 * snm - Old entry name.
3097 * tdip - Target directory to contain the "new entry".
3098 * tnm - New entry name.
3099 * cr - credentials of caller.
3100 * flags - case flags
3102 * RETURN: 0 if success
3103 * error code if failure
3106 * sdip,tdip - ctime|mtime updated
3110 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3111 cred_t *cr, int flags)
3113 znode_t *tdzp, *szp, *tzp;
3114 znode_t *sdzp = ITOZ(sdip);
3115 zfs_sb_t *zsb = ITOZSB(sdip);
3117 zfs_dirlock_t *sdl, *tdl;
3120 int cmp, serr, terr;
3125 ZFS_VERIFY_ZP(sdzp);
3128 if (tdip->i_sb != sdip->i_sb) {
3134 ZFS_VERIFY_ZP(tdzp);
3135 if (zsb->z_utf8 && u8_validate(tnm,
3136 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3141 if (flags & FIGNORECASE)
3150 * This is to prevent the creation of links into attribute space
3151 * by renaming a linked file into/outof an attribute directory.
3152 * See the comment in zfs_link() for why this is considered bad.
3154 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3160 * Lock source and target directory entries. To prevent deadlock,
3161 * a lock ordering must be defined. We lock the directory with
3162 * the smallest object id first, or if it's a tie, the one with
3163 * the lexically first name.
3165 if (sdzp->z_id < tdzp->z_id) {
3167 } else if (sdzp->z_id > tdzp->z_id) {
3171 * First compare the two name arguments without
3172 * considering any case folding.
3174 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3176 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3177 ASSERT(error == 0 || !zsb->z_utf8);
3180 * POSIX: "If the old argument and the new argument
3181 * both refer to links to the same existing file,
3182 * the rename() function shall return successfully
3183 * and perform no other action."
3189 * If the file system is case-folding, then we may
3190 * have some more checking to do. A case-folding file
3191 * system is either supporting mixed case sensitivity
3192 * access or is completely case-insensitive. Note
3193 * that the file system is always case preserving.
3195 * In mixed sensitivity mode case sensitive behavior
3196 * is the default. FIGNORECASE must be used to
3197 * explicitly request case insensitive behavior.
3199 * If the source and target names provided differ only
3200 * by case (e.g., a request to rename 'tim' to 'Tim'),
3201 * we will treat this as a special case in the
3202 * case-insensitive mode: as long as the source name
3203 * is an exact match, we will allow this to proceed as
3204 * a name-change request.
3206 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3207 (zsb->z_case == ZFS_CASE_MIXED &&
3208 flags & FIGNORECASE)) &&
3209 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3212 * case preserving rename request, require exact
3221 * If the source and destination directories are the same, we should
3222 * grab the z_name_lock of that directory only once.
3226 rw_enter(&sdzp->z_name_lock, RW_READER);
3230 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3231 ZEXISTS | zflg, NULL, NULL);
3232 terr = zfs_dirent_lock(&tdl,
3233 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3235 terr = zfs_dirent_lock(&tdl,
3236 tdzp, tnm, &tzp, zflg, NULL, NULL);
3237 serr = zfs_dirent_lock(&sdl,
3238 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3244 * Source entry invalid or not there.
3247 zfs_dirent_unlock(tdl);
3253 rw_exit(&sdzp->z_name_lock);
3255 if (strcmp(snm, "..") == 0)
3261 zfs_dirent_unlock(sdl);
3265 rw_exit(&sdzp->z_name_lock);
3267 if (strcmp(tnm, "..") == 0)
3274 * Must have write access at the source to remove the old entry
3275 * and write access at the target to create the new entry.
3276 * Note that if target and source are the same, this can be
3277 * done in a single check.
3280 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3283 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3285 * Check to make sure rename is valid.
3286 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3288 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3293 * Does target exist?
3297 * Source and target must be the same type.
3299 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3300 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3305 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3311 * POSIX dictates that when the source and target
3312 * entries refer to the same file object, rename
3313 * must do nothing and exit without error.
3315 if (szp->z_id == tzp->z_id) {
3321 tx = dmu_tx_create(zsb->z_os);
3322 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3323 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3324 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3325 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3327 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3328 zfs_sa_upgrade_txholds(tx, tdzp);
3331 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3332 zfs_sa_upgrade_txholds(tx, tzp);
3335 zfs_sa_upgrade_txholds(tx, szp);
3336 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3337 error = dmu_tx_assign(tx, TXG_NOWAIT);
3340 zfs_rename_unlock(&zl);
3341 zfs_dirent_unlock(sdl);
3342 zfs_dirent_unlock(tdl);
3345 rw_exit(&sdzp->z_name_lock);
3350 if (error == ERESTART) {
3360 if (tzp) /* Attempt to remove the existing target */
3361 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3364 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3366 szp->z_pflags |= ZFS_AV_MODIFIED;
3368 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3369 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3370 ASSERT3U(error, ==, 0);
3372 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3374 zfs_log_rename(zilog, tx, TX_RENAME |
3375 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3376 sdl->dl_name, tdzp, tdl->dl_name, szp);
3379 * At this point, we have successfully created
3380 * the target name, but have failed to remove
3381 * the source name. Since the create was done
3382 * with the ZRENAMING flag, there are
3383 * complications; for one, the link count is
3384 * wrong. The easiest way to deal with this
3385 * is to remove the newly created target, and
3386 * return the original error. This must
3387 * succeed; fortunately, it is very unlikely to
3388 * fail, since we just created it.
3390 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3391 ZRENAMING, NULL), ==, 0);
3399 zfs_rename_unlock(&zl);
3401 zfs_dirent_unlock(sdl);
3402 zfs_dirent_unlock(tdl);
3404 zfs_inode_update(sdzp);
3406 rw_exit(&sdzp->z_name_lock);
3409 zfs_inode_update(tdzp);
3411 zfs_inode_update(szp);
3414 zfs_inode_update(tzp);
3418 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3419 zil_commit(zilog, 0);
3424 EXPORT_SYMBOL(zfs_rename);
3427 * Insert the indicated symbolic reference entry into the directory.
3429 * IN: dip - Directory to contain new symbolic link.
3430 * link - Name for new symlink entry.
3431 * vap - Attributes of new entry.
3432 * target - Target path of new symlink.
3434 * cr - credentials of caller.
3435 * flags - case flags
3437 * RETURN: 0 if success
3438 * error code if failure
3441 * dip - ctime|mtime updated
3445 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3446 struct inode **ipp, cred_t *cr, int flags)
3448 znode_t *zp, *dzp = ITOZ(dip);
3451 zfs_sb_t *zsb = ITOZSB(dip);
3453 uint64_t len = strlen(link);
3456 zfs_acl_ids_t acl_ids;
3457 boolean_t fuid_dirtied;
3458 uint64_t txtype = TX_SYMLINK;
3460 ASSERT(S_ISLNK(vap->va_mode));
3466 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3467 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3471 if (flags & FIGNORECASE)
3474 if (len > MAXPATHLEN) {
3476 return (ENAMETOOLONG);
3479 if ((error = zfs_acl_ids_create(dzp, 0,
3480 vap, cr, NULL, &acl_ids)) != 0) {
3488 * Attempt to lock directory; fail if entry already exists.
3490 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3492 zfs_acl_ids_free(&acl_ids);
3497 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3498 zfs_acl_ids_free(&acl_ids);
3499 zfs_dirent_unlock(dl);
3504 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3505 zfs_acl_ids_free(&acl_ids);
3506 zfs_dirent_unlock(dl);
3510 tx = dmu_tx_create(zsb->z_os);
3511 fuid_dirtied = zsb->z_fuid_dirty;
3512 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3513 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3514 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3515 ZFS_SA_BASE_ATTR_SIZE + len);
3516 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3517 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3518 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3519 acl_ids.z_aclp->z_acl_bytes);
3522 zfs_fuid_txhold(zsb, tx);
3523 error = dmu_tx_assign(tx, TXG_NOWAIT);
3525 zfs_dirent_unlock(dl);
3526 if (error == ERESTART) {
3531 zfs_acl_ids_free(&acl_ids);
3538 * Create a new object for the symlink.
3539 * for version 4 ZPL datsets the symlink will be an SA attribute
3541 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3544 zfs_fuid_sync(zsb, tx);
3546 mutex_enter(&zp->z_lock);
3548 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3551 zfs_sa_symlink(zp, link, len, tx);
3552 mutex_exit(&zp->z_lock);
3555 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3556 &zp->z_size, sizeof (zp->z_size), tx);
3558 * Insert the new object into the directory.
3560 (void) zfs_link_create(dl, zp, tx, ZNEW);
3562 if (flags & FIGNORECASE)
3564 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3566 zfs_inode_update(dzp);
3567 zfs_inode_update(zp);
3569 zfs_acl_ids_free(&acl_ids);
3573 zfs_dirent_unlock(dl);
3577 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3578 zil_commit(zilog, 0);
3583 EXPORT_SYMBOL(zfs_symlink);
3586 * Return, in the buffer contained in the provided uio structure,
3587 * the symbolic path referred to by ip.
3589 * IN: ip - inode of symbolic link
3590 * uio - structure to contain the link path.
3591 * cr - credentials of caller.
3593 * RETURN: 0 if success
3594 * error code if failure
3597 * ip - atime updated
3601 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3603 znode_t *zp = ITOZ(ip);
3604 zfs_sb_t *zsb = ITOZSB(ip);
3610 mutex_enter(&zp->z_lock);
3612 error = sa_lookup_uio(zp->z_sa_hdl,
3613 SA_ZPL_SYMLINK(zsb), uio);
3615 error = zfs_sa_readlink(zp, uio);
3616 mutex_exit(&zp->z_lock);
3618 ZFS_ACCESSTIME_STAMP(zsb, zp);
3619 zfs_inode_update(zp);
3623 EXPORT_SYMBOL(zfs_readlink);
3626 * Insert a new entry into directory tdip referencing sip.
3628 * IN: tdip - Directory to contain new entry.
3629 * sip - inode of new entry.
3630 * name - name of new entry.
3631 * cr - credentials of caller.
3633 * RETURN: 0 if success
3634 * error code if failure
3637 * tdip - ctime|mtime updated
3638 * sip - ctime updated
3642 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr)
3644 znode_t *dzp = ITOZ(tdip);
3646 zfs_sb_t *zsb = ITOZSB(tdip);
3655 ASSERT(S_ISDIR(tdip->i_mode));
3662 * POSIX dictates that we return EPERM here.
3663 * Better choices include ENOTSUP or EISDIR.
3665 if (S_ISDIR(sip->i_mode)) {
3670 if (sip->i_sb != tdip->i_sb) {
3678 /* Prevent links to .zfs/shares files */
3680 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3681 &parent, sizeof (uint64_t))) != 0) {
3685 if (parent == zsb->z_shares_dir) {
3690 if (zsb->z_utf8 && u8_validate(name,
3691 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3695 #ifdef HAVE_PN_UTILS
3696 if (flags & FIGNORECASE)
3698 #endif /* HAVE_PN_UTILS */
3701 * We do not support links between attributes and non-attributes
3702 * because of the potential security risk of creating links
3703 * into "normal" file space in order to circumvent restrictions
3704 * imposed in attribute space.
3706 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3711 owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER);
3712 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3717 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3724 * Attempt to lock directory; fail if entry already exists.
3726 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3732 tx = dmu_tx_create(zsb->z_os);
3733 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3734 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3735 zfs_sa_upgrade_txholds(tx, szp);
3736 zfs_sa_upgrade_txholds(tx, dzp);
3737 error = dmu_tx_assign(tx, TXG_NOWAIT);
3739 zfs_dirent_unlock(dl);
3740 if (error == ERESTART) {
3750 error = zfs_link_create(dl, szp, tx, 0);
3753 uint64_t txtype = TX_LINK;
3754 #ifdef HAVE_PN_UTILS
3755 if (flags & FIGNORECASE)
3757 #endif /* HAVE_PN_UTILS */
3758 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3763 zfs_dirent_unlock(dl);
3765 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3766 zil_commit(zilog, 0);
3768 zfs_inode_update(dzp);
3769 zfs_inode_update(szp);
3773 EXPORT_SYMBOL(zfs_link);
3776 zfs_putpage_commit_cb(void *arg, int error)
3778 struct page *pp = arg;
3781 __set_page_dirty_nobuffers(pp);
3783 if (error != ECANCELED)
3789 end_page_writeback(pp);
3793 * Push a page out to disk, once the page is on stable storage the
3794 * registered commit callback will be run as notification of completion.
3796 * IN: ip - page mapped for inode.
3797 * pp - page to push (page is locked)
3798 * wbc - writeback control data
3800 * RETURN: 0 if success
3801 * error code if failure
3804 * ip - ctime|mtime updated
3808 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
3810 znode_t *zp = ITOZ(ip);
3811 zfs_sb_t *zsb = ITOZSB(ip);
3819 uint64_t mtime[2], ctime[2];
3820 sa_bulk_attr_t bulk[3];
3827 ASSERT(PageLocked(pp));
3829 pgoff = page_offset(pp); /* Page byte-offset in file */
3830 offset = i_size_read(ip); /* File length in bytes */
3831 pglen = MIN(PAGE_CACHE_SIZE, /* Page length in bytes */
3832 P2ROUNDUP(offset, PAGE_CACHE_SIZE)-pgoff);
3834 /* Page is beyond end of file */
3835 if (pgoff >= offset) {
3841 /* Truncate page length to end of file */
3842 if (pgoff + pglen > offset)
3843 pglen = offset - pgoff;
3847 * FIXME: Allow mmap writes past its quota. The correct fix
3848 * is to register a page_mkwrite() handler to count the page
3849 * against its quota when it is about to be dirtied.
3851 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
3852 zfs_owner_overquota(zsb, zp, B_TRUE)) {
3857 set_page_writeback(pp);
3860 rl = zfs_range_lock(zp, pgoff, pglen, RL_WRITER);
3861 tx = dmu_tx_create(zsb->z_os);
3863 sync = ((zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) ||
3864 (wbc->sync_mode == WB_SYNC_ALL));
3866 dmu_tx_callback_register(tx, zfs_putpage_commit_cb, pp);
3868 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
3870 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3871 zfs_sa_upgrade_txholds(tx, zp);
3872 err = dmu_tx_assign(tx, TXG_NOWAIT);
3874 if (err == ERESTART)
3877 /* Will call all registered commit callbacks */
3881 * For the synchronous case the commit callback must be
3882 * explicitly called because there is no registered callback.
3885 zfs_putpage_commit_cb(pp, ECANCELED);
3887 zfs_range_unlock(rl);
3893 ASSERT3U(pglen, <=, PAGE_CACHE_SIZE);
3894 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
3897 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3898 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3899 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
3901 /* Preserve the mtime and ctime provided by the inode */
3902 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
3903 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
3904 zp->z_atime_dirty = 0;
3907 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3909 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0);
3912 zfs_range_unlock(rl);
3915 zil_commit(zsb->z_log, zp->z_id);
3916 zfs_putpage_commit_cb(pp, err);
3925 zfs_inactive(struct inode *ip)
3927 znode_t *zp = ITOZ(ip);
3928 zfs_sb_t *zsb = ITOZSB(ip);
3931 if (zfsctl_is_node(ip)) {
3932 zfsctl_inode_inactive(ip);
3936 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
3937 if (zp->z_sa_hdl == NULL) {
3938 rw_exit(&zsb->z_teardown_inactive_lock);
3942 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3943 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
3945 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3946 zfs_sa_upgrade_txholds(tx, zp);
3947 error = dmu_tx_assign(tx, TXG_WAIT);
3951 mutex_enter(&zp->z_lock);
3952 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
3953 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3954 zp->z_atime_dirty = 0;
3955 mutex_exit(&zp->z_lock);
3961 rw_exit(&zsb->z_teardown_inactive_lock);
3963 EXPORT_SYMBOL(zfs_inactive);
3966 * Bounds-check the seek operation.
3968 * IN: ip - inode seeking within
3969 * ooff - old file offset
3970 * noffp - pointer to new file offset
3971 * ct - caller context
3973 * RETURN: 0 if success
3974 * EINVAL if new offset invalid
3978 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
3980 if (S_ISDIR(ip->i_mode))
3982 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3984 EXPORT_SYMBOL(zfs_seek);
3987 * Fill pages with data from the disk.
3990 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
3992 znode_t *zp = ITOZ(ip);
3993 zfs_sb_t *zsb = ITOZSB(ip);
3995 struct page *cur_pp;
3996 u_offset_t io_off, total;
4003 io_len = nr_pages << PAGE_CACHE_SHIFT;
4004 i_size = i_size_read(ip);
4005 io_off = page_offset(pl[0]);
4007 if (io_off + io_len > i_size)
4008 io_len = i_size - io_off;
4011 * Iterate over list of pages and read each page individually.
4015 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4019 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4023 /* convert checksum errors into IO errors */
4028 cur_pp = pl[++page_idx];
4035 * Uses zfs_fillpage to read data from the file and fill the pages.
4037 * IN: ip - inode of file to get data from.
4038 * pl - list of pages to read
4039 * nr_pages - number of pages to read
4041 * RETURN: 0 if success
4042 * error code if failure
4045 * vp - atime updated
4049 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4051 znode_t *zp = ITOZ(ip);
4052 zfs_sb_t *zsb = ITOZSB(ip);
4061 err = zfs_fillpage(ip, pl, nr_pages);
4064 ZFS_ACCESSTIME_STAMP(zsb, zp);
4069 EXPORT_SYMBOL(zfs_getpage);
4072 * Check ZFS specific permissions to memory map a section of a file.
4074 * IN: ip - inode of the file to mmap
4076 * addrp - start address in memory region
4077 * len - length of memory region
4078 * vm_flags- address flags
4080 * RETURN: 0 if success
4081 * error code if failure
4085 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4086 unsigned long vm_flags)
4088 znode_t *zp = ITOZ(ip);
4089 zfs_sb_t *zsb = ITOZSB(ip);
4094 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4095 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4100 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4101 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4106 if (off < 0 || len > MAXOFFSET_T - off) {
4114 EXPORT_SYMBOL(zfs_map);
4117 * convoff - converts the given data (start, whence) to the
4121 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4126 if ((lckdat->l_whence == 2) || (whence == 2)) {
4127 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4131 switch (lckdat->l_whence) {
4133 lckdat->l_start += offset;
4136 lckdat->l_start += vap.va_size;
4144 if (lckdat->l_start < 0)
4149 lckdat->l_start -= offset;
4152 lckdat->l_start -= vap.va_size;
4160 lckdat->l_whence = (short)whence;
4165 * Free or allocate space in a file. Currently, this function only
4166 * supports the `F_FREESP' command. However, this command is somewhat
4167 * misnamed, as its functionality includes the ability to allocate as
4168 * well as free space.
4170 * IN: ip - inode of file to free data in.
4171 * cmd - action to take (only F_FREESP supported).
4172 * bfp - section of file to free/alloc.
4173 * flag - current file open mode flags.
4174 * offset - current file offset.
4175 * cr - credentials of caller [UNUSED].
4177 * RETURN: 0 if success
4178 * error code if failure
4181 * ip - ctime|mtime updated
4185 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4186 offset_t offset, cred_t *cr)
4188 znode_t *zp = ITOZ(ip);
4189 zfs_sb_t *zsb = ITOZSB(ip);
4196 if (cmd != F_FREESP) {
4201 if ((error = convoff(ip, bfp, 0, offset))) {
4206 if (bfp->l_len < 0) {
4212 * Permissions aren't checked on Solaris because on this OS
4213 * zfs_space() can only be called with an opened file handle.
4214 * On Linux we can get here through truncate_range() which
4215 * operates directly on inodes, so we need to check access rights.
4217 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4223 len = bfp->l_len; /* 0 means from off to end of file */
4225 error = zfs_freesp(zp, off, len, flag, TRUE);
4230 EXPORT_SYMBOL(zfs_space);
4234 zfs_fid(struct inode *ip, fid_t *fidp)
4236 znode_t *zp = ITOZ(ip);
4237 zfs_sb_t *zsb = ITOZSB(ip);
4240 uint64_t object = zp->z_id;
4247 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4248 &gen64, sizeof (uint64_t))) != 0) {
4253 gen = (uint32_t)gen64;
4255 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4256 if (fidp->fid_len < size) {
4257 fidp->fid_len = size;
4262 zfid = (zfid_short_t *)fidp;
4264 zfid->zf_len = size;
4266 for (i = 0; i < sizeof (zfid->zf_object); i++)
4267 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4269 /* Must have a non-zero generation number to distinguish from .zfs */
4272 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4273 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4275 if (size == LONG_FID_LEN) {
4276 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4279 zlfid = (zfid_long_t *)fidp;
4281 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4282 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4284 /* XXX - this should be the generation number for the objset */
4285 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4286 zlfid->zf_setgen[i] = 0;
4292 EXPORT_SYMBOL(zfs_fid);
4296 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4298 znode_t *zp = ITOZ(ip);
4299 zfs_sb_t *zsb = ITOZSB(ip);
4301 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4305 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4310 EXPORT_SYMBOL(zfs_getsecattr);
4314 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4316 znode_t *zp = ITOZ(ip);
4317 zfs_sb_t *zsb = ITOZSB(ip);
4319 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4320 zilog_t *zilog = zsb->z_log;
4325 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4327 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4328 zil_commit(zilog, 0);
4333 EXPORT_SYMBOL(zfs_setsecattr);
4335 #ifdef HAVE_UIO_ZEROCOPY
4337 * Tunable, both must be a power of 2.
4339 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4340 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4341 * an arcbuf for a partial block read
4343 int zcr_blksz_min = (1 << 10); /* 1K */
4344 int zcr_blksz_max = (1 << 17); /* 128K */
4348 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4350 znode_t *zp = ITOZ(ip);
4351 zfs_sb_t *zsb = ITOZSB(ip);
4352 int max_blksz = zsb->z_max_blksz;
4353 uio_t *uio = &xuio->xu_uio;
4354 ssize_t size = uio->uio_resid;
4355 offset_t offset = uio->uio_loffset;
4360 int preamble, postamble;
4362 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4370 * Loan out an arc_buf for write if write size is bigger than
4371 * max_blksz, and the file's block size is also max_blksz.
4374 if (size < blksz || zp->z_blksz != blksz) {
4379 * Caller requests buffers for write before knowing where the
4380 * write offset might be (e.g. NFS TCP write).
4385 preamble = P2PHASE(offset, blksz);
4387 preamble = blksz - preamble;
4392 postamble = P2PHASE(size, blksz);
4395 fullblk = size / blksz;
4396 (void) dmu_xuio_init(xuio,
4397 (preamble != 0) + fullblk + (postamble != 0));
4400 * Have to fix iov base/len for partial buffers. They
4401 * currently represent full arc_buf's.
4404 /* data begins in the middle of the arc_buf */
4405 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4408 (void) dmu_xuio_add(xuio, abuf,
4409 blksz - preamble, preamble);
4412 for (i = 0; i < fullblk; i++) {
4413 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4416 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4420 /* data ends in the middle of the arc_buf */
4421 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4424 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4429 * Loan out an arc_buf for read if the read size is larger than
4430 * the current file block size. Block alignment is not
4431 * considered. Partial arc_buf will be loaned out for read.
4433 blksz = zp->z_blksz;
4434 if (blksz < zcr_blksz_min)
4435 blksz = zcr_blksz_min;
4436 if (blksz > zcr_blksz_max)
4437 blksz = zcr_blksz_max;
4438 /* avoid potential complexity of dealing with it */
4439 if (blksz > max_blksz) {
4444 maxsize = zp->z_size - uio->uio_loffset;
4458 uio->uio_extflg = UIO_XUIO;
4459 XUIO_XUZC_RW(xuio) = ioflag;
4466 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4470 int ioflag = XUIO_XUZC_RW(xuio);
4472 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4474 i = dmu_xuio_cnt(xuio);
4476 abuf = dmu_xuio_arcbuf(xuio, i);
4478 * if abuf == NULL, it must be a write buffer
4479 * that has been returned in zfs_write().
4482 dmu_return_arcbuf(abuf);
4483 ASSERT(abuf || ioflag == UIO_WRITE);
4486 dmu_xuio_fini(xuio);
4489 #endif /* HAVE_UIO_ZEROCOPY */
4491 #if defined(_KERNEL) && defined(HAVE_SPL)
4492 module_param(zfs_read_chunk_size, long, 0644);
4493 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");