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_fuid.h>
67 #include <sys/zfs_sa.h>
68 #include <sys/zfs_vnops.h>
70 #include <sys/zfs_rlock.h>
71 #include <sys/extdirent.h>
72 #include <sys/kidmap.h>
80 * Each vnode op performs some logical unit of work. To do this, the ZPL must
81 * properly lock its in-core state, create a DMU transaction, do the work,
82 * record this work in the intent log (ZIL), commit the DMU transaction,
83 * and wait for the intent log to commit if it is a synchronous operation.
84 * Moreover, the vnode ops must work in both normal and log replay context.
85 * The ordering of events is important to avoid deadlocks and references
86 * to freed memory. The example below illustrates the following Big Rules:
88 * (1) A check must be made in each zfs thread for a mounted file system.
89 * This is done avoiding races using ZFS_ENTER(zsb).
90 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
91 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
92 * can return EIO from the calling function.
94 * (2) iput() should always be the last thing except for zil_commit()
95 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
96 * First, if it's the last reference, the vnode/znode
97 * can be freed, so the zp may point to freed memory. Second, the last
98 * reference will call zfs_zinactive(), which may induce a lot of work --
99 * pushing cached pages (which acquires range locks) and syncing out
100 * cached atime changes. Third, zfs_zinactive() may require a new tx,
101 * which could deadlock the system if you were already holding one.
102 * If you must call iput() within a tx then use iput_ASYNC().
104 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
105 * as they can span dmu_tx_assign() calls.
107 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
108 * This is critical because we don't want to block while holding locks.
109 * Note, in particular, that if a lock is sometimes acquired before
110 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
111 * use a non-blocking assign can deadlock the system. The scenario:
113 * Thread A has grabbed a lock before calling dmu_tx_assign().
114 * Thread B is in an already-assigned tx, and blocks for this lock.
115 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
116 * forever, because the previous txg can't quiesce until B's tx commits.
118 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
119 * then drop all locks, call dmu_tx_wait(), and try again.
121 * (5) If the operation succeeded, generate the intent log entry for it
122 * before dropping locks. This ensures that the ordering of events
123 * in the intent log matches the order in which they actually occurred.
124 * During ZIL replay the zfs_log_* functions will update the sequence
125 * number to indicate the zil transaction has replayed.
127 * (6) At the end of each vnode op, the DMU tx must always commit,
128 * regardless of whether there were any errors.
130 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
131 * to ensure that synchronous semantics are provided when necessary.
133 * In general, this is how things should be ordered in each vnode op:
135 * ZFS_ENTER(zsb); // exit if unmounted
137 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
138 * rw_enter(...); // grab any other locks you need
139 * tx = dmu_tx_create(...); // get DMU tx
140 * dmu_tx_hold_*(); // hold each object you might modify
141 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
143 * rw_exit(...); // drop locks
144 * zfs_dirent_unlock(dl); // unlock directory entry
145 * iput(...); // release held vnodes
146 * if (error == ERESTART) {
151 * dmu_tx_abort(tx); // abort DMU tx
152 * ZFS_EXIT(zsb); // finished in zfs
153 * return (error); // really out of space
155 * error = do_real_work(); // do whatever this VOP does
157 * zfs_log_*(...); // on success, make ZIL entry
158 * dmu_tx_commit(tx); // commit DMU tx -- error or not
159 * rw_exit(...); // drop locks
160 * zfs_dirent_unlock(dl); // unlock directory entry
161 * iput(...); // release held vnodes
162 * zil_commit(zilog, foid); // synchronous when necessary
163 * ZFS_EXIT(zsb); // finished in zfs
164 * return (error); // done, report error
168 * Virus scanning is unsupported. It would be possible to add a hook
169 * here to performance the required virus scan. This could be done
170 * entirely in the kernel or potentially as an update to invoke a
174 zfs_vscan(struct inode *ip, cred_t *cr, int async)
181 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
183 znode_t *zp = ITOZ(ip);
184 zfs_sb_t *zsb = ITOZSB(ip);
189 /* Honor ZFS_APPENDONLY file attribute */
190 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
191 ((flag & O_APPEND) == 0)) {
196 /* Virus scan eligible files on open */
197 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
198 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
199 if (zfs_vscan(ip, cr, 0) != 0) {
205 /* Keep a count of the synchronous opens in the znode */
207 atomic_inc_32(&zp->z_sync_cnt);
212 EXPORT_SYMBOL(zfs_open);
216 zfs_close(struct inode *ip, int flag, cred_t *cr)
218 znode_t *zp = ITOZ(ip);
219 zfs_sb_t *zsb = ITOZSB(ip);
225 * Zero the synchronous opens in the znode. Under Linux the
226 * zfs_close() hook is not symmetric with zfs_open(), it is
227 * only called once when the last reference is dropped.
232 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
233 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
234 VERIFY(zfs_vscan(ip, cr, 1) == 0);
239 EXPORT_SYMBOL(zfs_close);
243 * When a file is memory mapped, we must keep the IO data synchronized
244 * between the DMU cache and the memory mapped pages. What this means:
246 * On Write: If we find a memory mapped page, we write to *both*
247 * the page and the dmu buffer.
250 update_pages(struct inode *ip, int64_t start, int len,
251 objset_t *os, uint64_t oid)
253 struct address_space *mp = ip->i_mapping;
259 off = start & (PAGE_CACHE_SIZE-1);
260 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
261 nbytes = MIN(PAGE_CACHE_SIZE - off, len);
263 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
265 if (mapping_writably_mapped(mp))
266 flush_dcache_page(pp);
269 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
273 if (mapping_writably_mapped(mp))
274 flush_dcache_page(pp);
276 mark_page_accessed(pp);
280 page_cache_release(pp);
289 * When a file is memory mapped, we must keep the IO data synchronized
290 * between the DMU cache and the memory mapped pages. What this means:
292 * On Read: We "read" preferentially from memory mapped pages,
293 * else we default from the dmu buffer.
295 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
296 * the file is memory mapped.
299 mappedread(struct inode *ip, int nbytes, uio_t *uio)
301 struct address_space *mp = ip->i_mapping;
303 znode_t *zp = ITOZ(ip);
304 objset_t *os = ITOZSB(ip)->z_os;
311 start = uio->uio_loffset;
312 off = start & (PAGE_CACHE_SIZE-1);
313 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
314 bytes = MIN(PAGE_CACHE_SIZE - off, len);
316 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
318 ASSERT(PageUptodate(pp));
321 error = uiomove(pb + off, bytes, UIO_READ, uio);
324 if (mapping_writably_mapped(mp))
325 flush_dcache_page(pp);
327 mark_page_accessed(pp);
329 page_cache_release(pp);
331 error = dmu_read_uio(os, zp->z_id, uio, bytes);
343 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
346 * Read bytes from specified file into supplied buffer.
348 * IN: ip - inode of file to be read from.
349 * uio - structure supplying read location, range info,
351 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
352 * O_DIRECT flag; used to bypass page cache.
353 * cr - credentials of caller.
355 * OUT: uio - updated offset and range, buffer filled.
357 * RETURN: 0 if success
358 * error code if failure
361 * inode - atime updated if byte count > 0
365 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
367 znode_t *zp = ITOZ(ip);
368 zfs_sb_t *zsb = ITOZSB(ip);
373 #ifdef HAVE_UIO_ZEROCOPY
375 #endif /* HAVE_UIO_ZEROCOPY */
381 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
387 * Validate file offset
389 if (uio->uio_loffset < (offset_t)0) {
395 * Fasttrack empty reads
397 if (uio->uio_resid == 0) {
403 * Check for mandatory locks
405 if (mandatory_lock(ip) &&
406 !lock_may_read(ip, uio->uio_loffset, uio->uio_resid)) {
412 * If we're in FRSYNC mode, sync out this znode before reading it.
414 if (ioflag & FRSYNC || zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
415 zil_commit(zsb->z_log, zp->z_id);
418 * Lock the range against changes.
420 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
423 * If we are reading past end-of-file we can skip
424 * to the end; but we might still need to set atime.
426 if (uio->uio_loffset >= zp->z_size) {
431 ASSERT(uio->uio_loffset < zp->z_size);
432 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
434 #ifdef HAVE_UIO_ZEROCOPY
435 if ((uio->uio_extflg == UIO_XUIO) &&
436 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
438 int blksz = zp->z_blksz;
439 uint64_t offset = uio->uio_loffset;
441 xuio = (xuio_t *)uio;
443 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
446 ASSERT(offset + n <= blksz);
449 (void) dmu_xuio_init(xuio, nblk);
451 if (vn_has_cached_data(ip)) {
453 * For simplicity, we always allocate a full buffer
454 * even if we only expect to read a portion of a block.
456 while (--nblk >= 0) {
457 (void) dmu_xuio_add(xuio,
458 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
463 #endif /* HAVE_UIO_ZEROCOPY */
466 nbytes = MIN(n, zfs_read_chunk_size -
467 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
469 if (zp->z_is_mapped && !(ioflag & O_DIRECT))
470 error = mappedread(ip, nbytes, uio);
472 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
475 /* convert checksum errors into IO errors */
484 zfs_range_unlock(rl);
486 ZFS_ACCESSTIME_STAMP(zsb, zp);
487 zfs_inode_update(zp);
491 EXPORT_SYMBOL(zfs_read);
494 * Write the bytes to a file.
496 * IN: ip - inode of file to be written to.
497 * uio - structure supplying write location, range info,
499 * ioflag - FAPPEND flag set if in append mode.
500 * O_DIRECT flag; used to bypass page cache.
501 * cr - credentials of caller.
503 * OUT: uio - updated offset and range.
505 * RETURN: 0 if success
506 * error code if failure
509 * ip - ctime|mtime updated if byte count > 0
514 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
516 znode_t *zp = ITOZ(ip);
517 rlim64_t limit = uio->uio_limit;
518 ssize_t start_resid = uio->uio_resid;
522 zfs_sb_t *zsb = ZTOZSB(zp);
527 int max_blksz = zsb->z_max_blksz;
530 iovec_t *aiov = NULL;
533 iovec_t *iovp = uio->uio_iov;
536 sa_bulk_attr_t bulk[4];
537 uint64_t mtime[2], ctime[2];
538 ASSERTV(int iovcnt = uio->uio_iovcnt);
541 * Fasttrack empty write
547 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
553 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
554 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
555 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
556 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
560 * If immutable or not appending then return EPERM
562 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
563 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
564 (uio->uio_loffset < zp->z_size))) {
572 * Validate file offset
574 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
581 * Check for mandatory locks before calling zfs_range_lock()
582 * in order to prevent a deadlock with locks set via fcntl().
584 if (mandatory_lock(ip) && !lock_may_write(ip, woff, n)) {
589 #ifdef HAVE_UIO_ZEROCOPY
591 * Pre-fault the pages to ensure slow (eg NFS) pages
593 * Skip this if uio contains loaned arc_buf.
595 if ((uio->uio_extflg == UIO_XUIO) &&
596 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
597 xuio = (xuio_t *)uio;
599 uio_prefaultpages(MIN(n, max_blksz), uio);
600 #endif /* HAVE_UIO_ZEROCOPY */
603 * If in append mode, set the io offset pointer to eof.
605 if (ioflag & FAPPEND) {
607 * Obtain an appending range lock to guarantee file append
608 * semantics. We reset the write offset once we have the lock.
610 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
612 if (rl->r_len == UINT64_MAX) {
614 * We overlocked the file because this write will cause
615 * the file block size to increase.
616 * Note that zp_size cannot change with this lock held.
620 uio->uio_loffset = woff;
623 * Note that if the file block size will change as a result of
624 * this write, then this range lock will lock the entire file
625 * so that we can re-write the block safely.
627 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
631 zfs_range_unlock(rl);
636 if ((woff + n) > limit || woff > (limit - n))
639 /* Will this write extend the file length? */
640 write_eof = (woff + n > zp->z_size);
642 end_size = MAX(zp->z_size, woff + n);
645 * Write the file in reasonable size chunks. Each chunk is written
646 * in a separate transaction; this keeps the intent log records small
647 * and allows us to do more fine-grained space accounting.
651 woff = uio->uio_loffset;
653 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
654 zfs_owner_overquota(zsb, zp, B_TRUE)) {
656 dmu_return_arcbuf(abuf);
661 if (xuio && abuf == NULL) {
662 ASSERT(i_iov < iovcnt);
664 abuf = dmu_xuio_arcbuf(xuio, i_iov);
665 dmu_xuio_clear(xuio, i_iov);
666 ASSERT((aiov->iov_base == abuf->b_data) ||
667 ((char *)aiov->iov_base - (char *)abuf->b_data +
668 aiov->iov_len == arc_buf_size(abuf)));
670 } else if (abuf == NULL && n >= max_blksz &&
671 woff >= zp->z_size &&
672 P2PHASE(woff, max_blksz) == 0 &&
673 zp->z_blksz == max_blksz) {
675 * This write covers a full block. "Borrow" a buffer
676 * from the dmu so that we can fill it before we enter
677 * a transaction. This avoids the possibility of
678 * holding up the transaction if the data copy hangs
679 * up on a pagefault (e.g., from an NFS server mapping).
683 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
685 ASSERT(abuf != NULL);
686 ASSERT(arc_buf_size(abuf) == max_blksz);
687 if ((error = uiocopy(abuf->b_data, max_blksz,
688 UIO_WRITE, uio, &cbytes))) {
689 dmu_return_arcbuf(abuf);
692 ASSERT(cbytes == max_blksz);
696 * Start a transaction.
698 tx = dmu_tx_create(zsb->z_os);
699 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
700 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
701 zfs_sa_upgrade_txholds(tx, zp);
702 error = dmu_tx_assign(tx, TXG_NOWAIT);
704 if (error == ERESTART) {
711 dmu_return_arcbuf(abuf);
716 * If zfs_range_lock() over-locked we grow the blocksize
717 * and then reduce the lock range. This will only happen
718 * on the first iteration since zfs_range_reduce() will
719 * shrink down r_len to the appropriate size.
721 if (rl->r_len == UINT64_MAX) {
724 if (zp->z_blksz > max_blksz) {
725 ASSERT(!ISP2(zp->z_blksz));
726 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
728 new_blksz = MIN(end_size, max_blksz);
730 zfs_grow_blocksize(zp, new_blksz, tx);
731 zfs_range_reduce(rl, woff, n);
735 * XXX - should we really limit each write to z_max_blksz?
736 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
738 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
741 tx_bytes = uio->uio_resid;
742 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
744 tx_bytes -= uio->uio_resid;
747 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
749 * If this is not a full block write, but we are
750 * extending the file past EOF and this data starts
751 * block-aligned, use assign_arcbuf(). Otherwise,
752 * write via dmu_write().
754 if (tx_bytes < max_blksz && (!write_eof ||
755 aiov->iov_base != abuf->b_data)) {
757 dmu_write(zsb->z_os, zp->z_id, woff,
758 aiov->iov_len, aiov->iov_base, tx);
759 dmu_return_arcbuf(abuf);
760 xuio_stat_wbuf_copied();
762 ASSERT(xuio || tx_bytes == max_blksz);
763 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
766 ASSERT(tx_bytes <= uio->uio_resid);
767 uioskip(uio, tx_bytes);
770 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
771 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
774 * If we made no progress, we're done. If we made even
775 * partial progress, update the znode and ZIL accordingly.
778 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
779 (void *)&zp->z_size, sizeof (uint64_t), tx);
786 * Clear Set-UID/Set-GID bits on successful write if not
787 * privileged and at least one of the excute bits is set.
789 * It would be nice to to this after all writes have
790 * been done, but that would still expose the ISUID/ISGID
791 * to another app after the partial write is committed.
793 * Note: we don't call zfs_fuid_map_id() here because
794 * user 0 is not an ephemeral uid.
796 mutex_enter(&zp->z_acl_lock);
797 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
798 (S_IXUSR >> 6))) != 0 &&
799 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
800 secpolicy_vnode_setid_retain(cr,
801 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
803 zp->z_mode &= ~(S_ISUID | S_ISGID);
804 newmode = zp->z_mode;
805 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
806 (void *)&newmode, sizeof (uint64_t), tx);
808 mutex_exit(&zp->z_acl_lock);
810 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
814 * Update the file size (zp_size) if it has changed;
815 * account for possible concurrent updates.
817 while ((end_size = zp->z_size) < uio->uio_loffset) {
818 (void) atomic_cas_64(&zp->z_size, end_size,
823 * If we are replaying and eof is non zero then force
824 * the file size to the specified eof. Note, there's no
825 * concurrency during replay.
827 if (zsb->z_replay && zsb->z_replay_eof != 0)
828 zp->z_size = zsb->z_replay_eof;
830 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
832 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
837 ASSERT(tx_bytes == nbytes);
841 uio_prefaultpages(MIN(n, max_blksz), uio);
844 zfs_range_unlock(rl);
847 * If we're in replay mode, or we made no progress, return error.
848 * Otherwise, it's at least a partial write, so it's successful.
850 if (zsb->z_replay || uio->uio_resid == start_resid) {
855 if (ioflag & (FSYNC | FDSYNC) ||
856 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
857 zil_commit(zilog, zp->z_id);
859 zfs_inode_update(zp);
863 EXPORT_SYMBOL(zfs_write);
866 iput_async(struct inode *ip, taskq_t *taskq)
868 ASSERT(atomic_read(&ip->i_count) > 0);
869 if (atomic_read(&ip->i_count) == 1)
870 taskq_dispatch(taskq, (task_func_t *)iput, ip, TQ_SLEEP);
876 zfs_get_done(zgd_t *zgd, int error)
878 znode_t *zp = zgd->zgd_private;
879 objset_t *os = ZTOZSB(zp)->z_os;
882 dmu_buf_rele(zgd->zgd_db, zgd);
884 zfs_range_unlock(zgd->zgd_rl);
887 * Release the vnode asynchronously as we currently have the
888 * txg stopped from syncing.
890 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
892 if (error == 0 && zgd->zgd_bp)
893 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
895 kmem_free(zgd, sizeof (zgd_t));
899 static int zil_fault_io = 0;
903 * Get data to generate a TX_WRITE intent log record.
906 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
909 objset_t *os = zsb->z_os;
911 uint64_t object = lr->lr_foid;
912 uint64_t offset = lr->lr_offset;
913 uint64_t size = lr->lr_length;
914 blkptr_t *bp = &lr->lr_blkptr;
923 * Nothing to do if the file has been removed
925 if (zfs_zget(zsb, object, &zp) != 0)
927 if (zp->z_unlinked) {
929 * Release the vnode asynchronously as we currently have the
930 * txg stopped from syncing.
932 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
936 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
937 zgd->zgd_zilog = zsb->z_log;
938 zgd->zgd_private = zp;
941 * Write records come in two flavors: immediate and indirect.
942 * For small writes it's cheaper to store the data with the
943 * log record (immediate); for large writes it's cheaper to
944 * sync the data and get a pointer to it (indirect) so that
945 * we don't have to write the data twice.
947 if (buf != NULL) { /* immediate write */
948 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
949 /* test for truncation needs to be done while range locked */
950 if (offset >= zp->z_size) {
953 error = dmu_read(os, object, offset, size, buf,
954 DMU_READ_NO_PREFETCH);
956 ASSERT(error == 0 || error == ENOENT);
957 } else { /* indirect write */
959 * Have to lock the whole block to ensure when it's
960 * written out and it's checksum is being calculated
961 * that no one can change the data. We need to re-check
962 * blocksize after we get the lock in case it's changed!
967 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
969 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
971 if (zp->z_blksz == size)
974 zfs_range_unlock(zgd->zgd_rl);
976 /* test for truncation needs to be done while range locked */
977 if (lr->lr_offset >= zp->z_size)
986 error = dmu_buf_hold(os, object, offset, zgd, &db,
987 DMU_READ_NO_PREFETCH);
993 ASSERT(db->db_offset == offset);
994 ASSERT(db->db_size == size);
996 error = dmu_sync(zio, lr->lr_common.lrc_txg,
998 ASSERT(error || lr->lr_length <= zp->z_blksz);
1001 * On success, we need to wait for the write I/O
1002 * initiated by dmu_sync() to complete before we can
1003 * release this dbuf. We will finish everything up
1004 * in the zfs_get_done() callback.
1009 if (error == EALREADY) {
1010 lr->lr_common.lrc_txtype = TX_WRITE2;
1016 zfs_get_done(zgd, error);
1023 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1025 znode_t *zp = ITOZ(ip);
1026 zfs_sb_t *zsb = ITOZSB(ip);
1032 if (flag & V_ACE_MASK)
1033 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1035 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1040 EXPORT_SYMBOL(zfs_access);
1043 * Lookup an entry in a directory, or an extended attribute directory.
1044 * If it exists, return a held inode reference for it.
1046 * IN: dip - inode of directory to search.
1047 * nm - name of entry to lookup.
1048 * flags - LOOKUP_XATTR set if looking for an attribute.
1049 * cr - credentials of caller.
1050 * direntflags - directory lookup flags
1051 * realpnp - returned pathname.
1053 * OUT: ipp - inode of located entry, NULL if not found.
1055 * RETURN: 0 if success
1056 * error code if failure
1063 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1064 cred_t *cr, int *direntflags, pathname_t *realpnp)
1066 znode_t *zdp = ITOZ(dip);
1067 zfs_sb_t *zsb = ITOZSB(dip);
1071 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1073 if (!S_ISDIR(dip->i_mode)) {
1075 } else if (zdp->z_sa_hdl == NULL) {
1079 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1080 error = zfs_fastaccesschk_execute(zdp, cr);
1089 vnode_t *tvp = dnlc_lookup(dvp, nm);
1092 error = zfs_fastaccesschk_execute(zdp, cr);
1097 if (tvp == DNLC_NO_VNODE) {
1102 return (specvp_check(vpp, cr));
1105 #endif /* HAVE_DNLC */
1114 if (flags & LOOKUP_XATTR) {
1116 * We don't allow recursive attributes..
1117 * Maybe someday we will.
1119 if (zdp->z_pflags & ZFS_XATTR) {
1124 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1130 * Do we have permission to get into attribute directory?
1133 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1143 if (!S_ISDIR(dip->i_mode)) {
1149 * Check accessibility of directory.
1152 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1157 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
1158 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1163 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1164 if ((error == 0) && (*ipp))
1165 zfs_inode_update(ITOZ(*ipp));
1170 EXPORT_SYMBOL(zfs_lookup);
1173 * Attempt to create a new entry in a directory. If the entry
1174 * already exists, truncate the file if permissible, else return
1175 * an error. Return the ip of the created or trunc'd file.
1177 * IN: dip - inode of directory to put new file entry in.
1178 * name - name of new file entry.
1179 * vap - attributes of new file.
1180 * excl - flag indicating exclusive or non-exclusive mode.
1181 * mode - mode to open file with.
1182 * cr - credentials of caller.
1183 * flag - large file flag [UNUSED].
1184 * vsecp - ACL to be set
1186 * OUT: ipp - inode of created or trunc'd entry.
1188 * RETURN: 0 if success
1189 * error code if failure
1192 * dip - ctime|mtime updated if new entry created
1193 * ip - ctime|mtime always, atime if new
1198 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1199 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1201 znode_t *zp, *dzp = ITOZ(dip);
1202 zfs_sb_t *zsb = ITOZSB(dip);
1210 zfs_acl_ids_t acl_ids;
1211 boolean_t fuid_dirtied;
1212 boolean_t have_acl = B_FALSE;
1215 * If we have an ephemeral id, ACL, or XVATTR then
1216 * make sure file system is at proper version
1222 if (zsb->z_use_fuids == B_FALSE &&
1223 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1231 if (zsb->z_utf8 && u8_validate(name, strlen(name),
1232 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1237 if (vap->va_mask & ATTR_XVATTR) {
1238 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1239 crgetuid(cr), cr, vap->va_mode)) != 0) {
1247 if (*name == '\0') {
1249 * Null component name refers to the directory itself.
1256 /* possible igrab(zp) */
1259 if (flag & FIGNORECASE)
1262 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1266 zfs_acl_ids_free(&acl_ids);
1267 if (strcmp(name, "..") == 0)
1278 * Create a new file object and update the directory
1281 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1283 zfs_acl_ids_free(&acl_ids);
1288 * We only support the creation of regular files in
1289 * extended attribute directories.
1292 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1294 zfs_acl_ids_free(&acl_ids);
1299 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1300 cr, vsecp, &acl_ids)) != 0)
1304 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1305 zfs_acl_ids_free(&acl_ids);
1310 tx = dmu_tx_create(os);
1312 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1313 ZFS_SA_BASE_ATTR_SIZE);
1315 fuid_dirtied = zsb->z_fuid_dirty;
1317 zfs_fuid_txhold(zsb, tx);
1318 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1319 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1320 if (!zsb->z_use_sa &&
1321 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1322 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1323 0, acl_ids.z_aclp->z_acl_bytes);
1325 error = dmu_tx_assign(tx, TXG_NOWAIT);
1327 zfs_dirent_unlock(dl);
1328 if (error == ERESTART) {
1333 zfs_acl_ids_free(&acl_ids);
1338 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1341 zfs_fuid_sync(zsb, tx);
1343 (void) zfs_link_create(dl, zp, tx, ZNEW);
1344 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1345 if (flag & FIGNORECASE)
1347 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1348 vsecp, acl_ids.z_fuidp, vap);
1349 zfs_acl_ids_free(&acl_ids);
1352 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1355 zfs_acl_ids_free(&acl_ids);
1359 * A directory entry already exists for this name.
1362 * Can't truncate an existing file if in exclusive mode.
1369 * Can't open a directory for writing.
1371 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1376 * Verify requested access to file.
1378 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1382 mutex_enter(&dzp->z_lock);
1384 mutex_exit(&dzp->z_lock);
1387 * Truncate regular files if requested.
1389 if (S_ISREG(ZTOI(zp)->i_mode) &&
1390 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1391 /* we can't hold any locks when calling zfs_freesp() */
1392 zfs_dirent_unlock(dl);
1394 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1400 zfs_dirent_unlock(dl);
1406 zfs_inode_update(dzp);
1407 zfs_inode_update(zp);
1411 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1412 zil_commit(zilog, 0);
1417 EXPORT_SYMBOL(zfs_create);
1420 * Remove an entry from a directory.
1422 * IN: dip - inode of directory to remove entry from.
1423 * name - name of entry to remove.
1424 * cr - credentials of caller.
1426 * RETURN: 0 if success
1427 * error code if failure
1431 * ip - ctime (if nlink > 0)
1434 uint64_t null_xattr = 0;
1438 zfs_remove(struct inode *dip, char *name, cred_t *cr)
1440 znode_t *zp, *dzp = ITOZ(dip);
1443 zfs_sb_t *zsb = ITOZSB(dip);
1446 uint64_t xattr_obj_unlinked = 0;
1452 pathname_t *realnmp = NULL;
1453 #ifdef HAVE_PN_UTILS
1455 #endif /* HAVE_PN_UTILS */
1463 #ifdef HAVE_PN_UTILS
1464 if (flags & FIGNORECASE) {
1469 #endif /* HAVE_PN_UTILS */
1475 * Attempt to lock directory; fail if entry doesn't exist.
1477 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1479 #ifdef HAVE_PN_UTILS
1482 #endif /* HAVE_PN_UTILS */
1489 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1494 * Need to use rmdir for removing directories.
1496 if (S_ISDIR(ip->i_mode)) {
1503 dnlc_remove(dvp, realnmp->pn_buf);
1505 dnlc_remove(dvp, name);
1506 #endif /* HAVE_DNLC */
1509 * We never delete the znode and always place it in the unlinked
1510 * set. The dentry cache will always hold the last reference and
1511 * is responsible for safely freeing the znode.
1514 tx = dmu_tx_create(zsb->z_os);
1515 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1516 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1517 zfs_sa_upgrade_txholds(tx, zp);
1518 zfs_sa_upgrade_txholds(tx, dzp);
1520 /* are there any extended attributes? */
1521 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1522 &xattr_obj, sizeof (xattr_obj));
1523 if (error == 0 && xattr_obj) {
1524 error = zfs_zget(zsb, xattr_obj, &xzp);
1525 ASSERT3U(error, ==, 0);
1526 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1527 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1530 /* charge as an update -- would be nice not to charge at all */
1531 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1533 error = dmu_tx_assign(tx, TXG_NOWAIT);
1535 zfs_dirent_unlock(dl);
1539 if (error == ERESTART) {
1544 #ifdef HAVE_PN_UTILS
1547 #endif /* HAVE_PN_UTILS */
1554 * Remove the directory entry.
1556 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1565 * Hold z_lock so that we can make sure that the ACL obj
1566 * hasn't changed. Could have been deleted due to
1569 mutex_enter(&zp->z_lock);
1570 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1571 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1572 mutex_exit(&zp->z_lock);
1573 zfs_unlinked_add(zp, tx);
1577 #ifdef HAVE_PN_UTILS
1578 if (flags & FIGNORECASE)
1580 #endif /* HAVE_PN_UTILS */
1581 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1585 #ifdef HAVE_PN_UTILS
1588 #endif /* HAVE_PN_UTILS */
1590 zfs_dirent_unlock(dl);
1591 zfs_inode_update(dzp);
1592 zfs_inode_update(zp);
1594 zfs_inode_update(xzp);
1600 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1601 zil_commit(zilog, 0);
1606 EXPORT_SYMBOL(zfs_remove);
1609 * Create a new directory and insert it into dip using the name
1610 * provided. Return a pointer to the inserted directory.
1612 * IN: dip - inode of directory to add subdir to.
1613 * dirname - name of new directory.
1614 * vap - attributes of new directory.
1615 * cr - credentials of caller.
1616 * vsecp - ACL to be set
1618 * OUT: ipp - inode of created directory.
1620 * RETURN: 0 if success
1621 * error code if failure
1624 * dip - ctime|mtime updated
1625 * ipp - ctime|mtime|atime updated
1629 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1630 cred_t *cr, int flags, vsecattr_t *vsecp)
1632 znode_t *zp, *dzp = ITOZ(dip);
1633 zfs_sb_t *zsb = ITOZSB(dip);
1641 gid_t gid = crgetgid(cr);
1642 zfs_acl_ids_t acl_ids;
1643 boolean_t fuid_dirtied;
1645 ASSERT(S_ISDIR(vap->va_mode));
1648 * If we have an ephemeral id, ACL, or XVATTR then
1649 * make sure file system is at proper version
1653 if (zsb->z_use_fuids == B_FALSE &&
1654 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1661 if (dzp->z_pflags & ZFS_XATTR) {
1666 if (zsb->z_utf8 && u8_validate(dirname,
1667 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1671 if (flags & FIGNORECASE)
1674 if (vap->va_mask & ATTR_XVATTR) {
1675 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1676 crgetuid(cr), cr, vap->va_mode)) != 0) {
1682 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1683 vsecp, &acl_ids)) != 0) {
1688 * First make sure the new directory doesn't exist.
1690 * Existence is checked first to make sure we don't return
1691 * EACCES instead of EEXIST which can cause some applications
1697 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1699 zfs_acl_ids_free(&acl_ids);
1704 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1705 zfs_acl_ids_free(&acl_ids);
1706 zfs_dirent_unlock(dl);
1711 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1712 zfs_acl_ids_free(&acl_ids);
1713 zfs_dirent_unlock(dl);
1719 * Add a new entry to the directory.
1721 tx = dmu_tx_create(zsb->z_os);
1722 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1723 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1724 fuid_dirtied = zsb->z_fuid_dirty;
1726 zfs_fuid_txhold(zsb, tx);
1727 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1728 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1729 acl_ids.z_aclp->z_acl_bytes);
1732 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1733 ZFS_SA_BASE_ATTR_SIZE);
1735 error = dmu_tx_assign(tx, TXG_NOWAIT);
1737 zfs_dirent_unlock(dl);
1738 if (error == ERESTART) {
1743 zfs_acl_ids_free(&acl_ids);
1752 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1755 zfs_fuid_sync(zsb, tx);
1758 * Now put new name in parent dir.
1760 (void) zfs_link_create(dl, zp, tx, ZNEW);
1764 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1765 if (flags & FIGNORECASE)
1767 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1768 acl_ids.z_fuidp, vap);
1770 zfs_acl_ids_free(&acl_ids);
1774 zfs_dirent_unlock(dl);
1776 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1777 zil_commit(zilog, 0);
1779 zfs_inode_update(dzp);
1780 zfs_inode_update(zp);
1784 EXPORT_SYMBOL(zfs_mkdir);
1787 * Remove a directory subdir entry. If the current working
1788 * directory is the same as the subdir to be removed, the
1791 * IN: dip - inode of directory to remove from.
1792 * name - name of directory to be removed.
1793 * cwd - inode of current working directory.
1794 * cr - credentials of caller.
1795 * flags - case flags
1797 * RETURN: 0 if success
1798 * error code if failure
1801 * dip - ctime|mtime updated
1805 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1808 znode_t *dzp = ITOZ(dip);
1811 zfs_sb_t *zsb = ITOZSB(dip);
1822 if (flags & FIGNORECASE)
1828 * Attempt to lock directory; fail if entry doesn't exist.
1830 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1838 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1842 if (!S_ISDIR(ip->i_mode)) {
1853 * Grab a lock on the directory to make sure that noone is
1854 * trying to add (or lookup) entries while we are removing it.
1856 rw_enter(&zp->z_name_lock, RW_WRITER);
1859 * Grab a lock on the parent pointer to make sure we play well
1860 * with the treewalk and directory rename code.
1862 rw_enter(&zp->z_parent_lock, RW_WRITER);
1864 tx = dmu_tx_create(zsb->z_os);
1865 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1866 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1867 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1868 zfs_sa_upgrade_txholds(tx, zp);
1869 zfs_sa_upgrade_txholds(tx, dzp);
1870 error = dmu_tx_assign(tx, TXG_NOWAIT);
1872 rw_exit(&zp->z_parent_lock);
1873 rw_exit(&zp->z_name_lock);
1874 zfs_dirent_unlock(dl);
1876 if (error == ERESTART) {
1886 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1889 uint64_t txtype = TX_RMDIR;
1890 if (flags & FIGNORECASE)
1892 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
1897 rw_exit(&zp->z_parent_lock);
1898 rw_exit(&zp->z_name_lock);
1900 zfs_dirent_unlock(dl);
1904 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1905 zil_commit(zilog, 0);
1907 zfs_inode_update(dzp);
1908 zfs_inode_update(zp);
1912 EXPORT_SYMBOL(zfs_rmdir);
1915 * Read as many directory entries as will fit into the provided
1916 * dirent buffer from the given directory cursor position.
1918 * IN: ip - inode of directory to read.
1919 * dirent - buffer for directory entries.
1921 * OUT: dirent - filler buffer of directory entries.
1923 * RETURN: 0 if success
1924 * error code if failure
1927 * ip - atime updated
1929 * Note that the low 4 bits of the cookie returned by zap is always zero.
1930 * This allows us to use the low range for "special" directory entries:
1931 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1932 * we use the offset 2 for the '.zfs' directory.
1936 zfs_readdir(struct inode *ip, void *dirent, filldir_t filldir,
1937 loff_t *pos, cred_t *cr)
1939 znode_t *zp = ITOZ(ip);
1940 zfs_sb_t *zsb = ITOZSB(ip);
1943 zap_attribute_t zap;
1953 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
1954 &parent, sizeof (parent))) != 0)
1958 * Quit if directory has been removed (posix)
1965 prefetch = zp->z_zn_prefetch;
1968 * Initialize the iterator cursor.
1972 * Start iteration from the beginning of the directory.
1974 zap_cursor_init(&zc, os, zp->z_id);
1977 * The offset is a serialized cursor.
1979 zap_cursor_init_serialized(&zc, os, zp->z_id, *pos);
1983 * Transform to file-system independent format
1990 * Special case `.', `..', and `.zfs'.
1993 (void) strcpy(zap.za_name, ".");
1994 zap.za_normalization_conflict = 0;
1996 } else if (*pos == 1) {
1997 (void) strcpy(zap.za_name, "..");
1998 zap.za_normalization_conflict = 0;
2000 } else if (*pos == 2 && zfs_show_ctldir(zp)) {
2001 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2002 zap.za_normalization_conflict = 0;
2003 objnum = ZFSCTL_INO_ROOT;
2008 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2009 if (error == ENOENT)
2015 if (zap.za_integer_length != 8 ||
2016 zap.za_num_integers != 1) {
2017 cmn_err(CE_WARN, "zap_readdir: bad directory "
2018 "entry, obj = %lld, offset = %lld\n",
2019 (u_longlong_t)zp->z_id,
2020 (u_longlong_t)*pos);
2025 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2027 done = filldir(dirent, zap.za_name, strlen(zap.za_name),
2028 zap_cursor_serialize(&zc), objnum, 0);
2033 /* Prefetch znode */
2035 dmu_prefetch(os, objnum, 0, 0);
2039 zap_cursor_advance(&zc);
2040 *pos = zap_cursor_serialize(&zc);
2045 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2048 zap_cursor_fini(&zc);
2049 if (error == ENOENT)
2052 ZFS_ACCESSTIME_STAMP(zsb, zp);
2053 zfs_inode_update(zp);
2060 EXPORT_SYMBOL(zfs_readdir);
2062 ulong_t zfs_fsync_sync_cnt = 4;
2065 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2067 znode_t *zp = ITOZ(ip);
2068 zfs_sb_t *zsb = ITOZSB(ip);
2070 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2072 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2075 zil_commit(zsb->z_log, zp->z_id);
2080 EXPORT_SYMBOL(zfs_fsync);
2084 * Get the requested file attributes and place them in the provided
2087 * IN: ip - inode of file.
2088 * vap - va_mask identifies requested attributes.
2089 * If ATTR_XVATTR set, then optional attrs are requested
2090 * flags - ATTR_NOACLCHECK (CIFS server context)
2091 * cr - credentials of caller.
2093 * OUT: vap - attribute values.
2095 * RETURN: 0 (always succeeds)
2099 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2101 znode_t *zp = ITOZ(ip);
2102 zfs_sb_t *zsb = ITOZSB(ip);
2105 uint64_t mtime[2], ctime[2];
2106 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2107 xoptattr_t *xoap = NULL;
2108 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2109 sa_bulk_attr_t bulk[2];
2115 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2117 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2118 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2120 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2126 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2127 * Also, if we are the owner don't bother, since owner should
2128 * always be allowed to read basic attributes of file.
2130 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2131 (vap->va_uid != crgetuid(cr))) {
2132 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2140 * Return all attributes. It's cheaper to provide the answer
2141 * than to determine whether we were asked the question.
2144 mutex_enter(&zp->z_lock);
2145 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2146 vap->va_mode = zp->z_mode;
2147 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2148 vap->va_nodeid = zp->z_id;
2149 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2150 links = zp->z_links + 1;
2152 links = zp->z_links;
2153 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2154 vap->va_size = i_size_read(ip);
2155 vap->va_rdev = ip->i_rdev;
2156 vap->va_seq = ip->i_generation;
2159 * Add in any requested optional attributes and the create time.
2160 * Also set the corresponding bits in the returned attribute bitmap.
2162 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2163 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2165 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2166 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2169 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2170 xoap->xoa_readonly =
2171 ((zp->z_pflags & ZFS_READONLY) != 0);
2172 XVA_SET_RTN(xvap, XAT_READONLY);
2175 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2177 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2178 XVA_SET_RTN(xvap, XAT_SYSTEM);
2181 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2183 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2184 XVA_SET_RTN(xvap, XAT_HIDDEN);
2187 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2188 xoap->xoa_nounlink =
2189 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2190 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2193 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2194 xoap->xoa_immutable =
2195 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2196 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2199 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2200 xoap->xoa_appendonly =
2201 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2202 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2205 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2207 ((zp->z_pflags & ZFS_NODUMP) != 0);
2208 XVA_SET_RTN(xvap, XAT_NODUMP);
2211 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2213 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2214 XVA_SET_RTN(xvap, XAT_OPAQUE);
2217 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2218 xoap->xoa_av_quarantined =
2219 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2220 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2223 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2224 xoap->xoa_av_modified =
2225 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2226 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2229 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2230 S_ISREG(ip->i_mode)) {
2231 zfs_sa_get_scanstamp(zp, xvap);
2234 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2237 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2238 times, sizeof (times));
2239 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2240 XVA_SET_RTN(xvap, XAT_CREATETIME);
2243 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2244 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2245 XVA_SET_RTN(xvap, XAT_REPARSE);
2247 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2248 xoap->xoa_generation = zp->z_gen;
2249 XVA_SET_RTN(xvap, XAT_GEN);
2252 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2254 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2255 XVA_SET_RTN(xvap, XAT_OFFLINE);
2258 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2260 ((zp->z_pflags & ZFS_SPARSE) != 0);
2261 XVA_SET_RTN(xvap, XAT_SPARSE);
2265 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2266 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2267 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2269 mutex_exit(&zp->z_lock);
2271 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2273 if (zp->z_blksz == 0) {
2275 * Block size hasn't been set; suggest maximal I/O transfers.
2277 vap->va_blksize = zsb->z_max_blksz;
2283 EXPORT_SYMBOL(zfs_getattr);
2286 * Get the basic file attributes and place them in the provided kstat
2287 * structure. The inode is assumed to be the authoritative source
2288 * for most of the attributes. However, the znode currently has the
2289 * authoritative atime, blksize, and block count.
2291 * IN: ip - inode of file.
2293 * OUT: sp - kstat values.
2295 * RETURN: 0 (always succeeds)
2299 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2301 znode_t *zp = ITOZ(ip);
2302 zfs_sb_t *zsb = ITOZSB(ip);
2307 mutex_enter(&zp->z_lock);
2309 generic_fillattr(ip, sp);
2310 ZFS_TIME_DECODE(&sp->atime, zp->z_atime);
2312 sa_object_size(zp->z_sa_hdl, (uint32_t *)&sp->blksize, &sp->blocks);
2313 if (unlikely(zp->z_blksz == 0)) {
2315 * Block size hasn't been set; suggest maximal I/O transfers.
2317 sp->blksize = zsb->z_max_blksz;
2320 mutex_exit(&zp->z_lock);
2326 EXPORT_SYMBOL(zfs_getattr_fast);
2329 * Set the file attributes to the values contained in the
2332 * IN: ip - inode of file to be modified.
2333 * vap - new attribute values.
2334 * If ATTR_XVATTR set, then optional attrs are being set
2335 * flags - ATTR_UTIME set if non-default time values provided.
2336 * - ATTR_NOACLCHECK (CIFS context only).
2337 * cr - credentials of caller.
2339 * RETURN: 0 if success
2340 * error code if failure
2343 * ip - ctime updated, mtime updated if size changed.
2347 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2349 znode_t *zp = ITOZ(ip);
2350 zfs_sb_t *zsb = ITOZSB(ip);
2354 xvattr_t *tmpxvattr;
2355 uint_t mask = vap->va_mask;
2359 uint64_t new_uid, new_gid;
2361 uint64_t mtime[2], ctime[2];
2363 int need_policy = FALSE;
2365 zfs_fuid_info_t *fuidp = NULL;
2366 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2369 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2370 boolean_t fuid_dirtied = B_FALSE;
2371 sa_bulk_attr_t *bulk, *xattr_bulk;
2372 int count = 0, xattr_count = 0;
2383 * Make sure that if we have ephemeral uid/gid or xvattr specified
2384 * that file system is at proper version level
2387 if (zsb->z_use_fuids == B_FALSE &&
2388 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2389 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2390 (mask & ATTR_XVATTR))) {
2395 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2400 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2406 * If this is an xvattr_t, then get a pointer to the structure of
2407 * optional attributes. If this is NULL, then we have a vattr_t.
2409 xoap = xva_getxoptattr(xvap);
2411 tmpxvattr = kmem_alloc(sizeof(xvattr_t), KM_SLEEP);
2412 xva_init(tmpxvattr);
2414 bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2415 xattr_bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2418 * Immutable files can only alter immutable bit and atime
2420 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2421 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2422 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2427 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2433 * Verify timestamps doesn't overflow 32 bits.
2434 * ZFS can handle large timestamps, but 32bit syscalls can't
2435 * handle times greater than 2039. This check should be removed
2436 * once large timestamps are fully supported.
2438 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2439 if (((mask & ATTR_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2440 ((mask & ATTR_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2450 /* Can this be moved to before the top label? */
2451 if (zfs_is_readonly(zsb)) {
2457 * First validate permissions
2460 if (mask & ATTR_SIZE) {
2461 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2465 truncate_setsize(ip, vap->va_size);
2468 * XXX - Note, we are not providing any open
2469 * mode flags here (like FNDELAY), so we may
2470 * block if there are locks present... this
2471 * should be addressed in openat().
2473 /* XXX - would it be OK to generate a log record here? */
2474 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2479 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2480 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2481 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2482 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2483 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2484 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2485 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2486 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2487 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2491 if (mask & (ATTR_UID|ATTR_GID)) {
2492 int idmask = (mask & (ATTR_UID|ATTR_GID));
2497 * NOTE: even if a new mode is being set,
2498 * we may clear S_ISUID/S_ISGID bits.
2501 if (!(mask & ATTR_MODE))
2502 vap->va_mode = zp->z_mode;
2505 * Take ownership or chgrp to group we are a member of
2508 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2509 take_group = (mask & ATTR_GID) &&
2510 zfs_groupmember(zsb, vap->va_gid, cr);
2513 * If both ATTR_UID and ATTR_GID are set then take_owner and
2514 * take_group must both be set in order to allow taking
2517 * Otherwise, send the check through secpolicy_vnode_setattr()
2521 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2522 take_owner && take_group) ||
2523 ((idmask == ATTR_UID) && take_owner) ||
2524 ((idmask == ATTR_GID) && take_group)) {
2525 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2526 skipaclchk, cr) == 0) {
2528 * Remove setuid/setgid for non-privileged users
2530 (void) secpolicy_setid_clear(vap, cr);
2531 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2540 mutex_enter(&zp->z_lock);
2541 oldva.va_mode = zp->z_mode;
2542 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2543 if (mask & ATTR_XVATTR) {
2545 * Update xvattr mask to include only those attributes
2546 * that are actually changing.
2548 * the bits will be restored prior to actually setting
2549 * the attributes so the caller thinks they were set.
2551 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2552 if (xoap->xoa_appendonly !=
2553 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2556 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2557 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2561 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2562 if (xoap->xoa_nounlink !=
2563 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2566 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2567 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2571 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2572 if (xoap->xoa_immutable !=
2573 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2576 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2577 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2581 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2582 if (xoap->xoa_nodump !=
2583 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2586 XVA_CLR_REQ(xvap, XAT_NODUMP);
2587 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2591 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2592 if (xoap->xoa_av_modified !=
2593 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2596 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2597 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2601 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2602 if ((!S_ISREG(ip->i_mode) &&
2603 xoap->xoa_av_quarantined) ||
2604 xoap->xoa_av_quarantined !=
2605 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2608 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2609 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2613 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2614 mutex_exit(&zp->z_lock);
2619 if (need_policy == FALSE &&
2620 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2621 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2626 mutex_exit(&zp->z_lock);
2628 if (mask & ATTR_MODE) {
2629 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2630 err = secpolicy_setid_setsticky_clear(ip, vap,
2635 trim_mask |= ATTR_MODE;
2643 * If trim_mask is set then take ownership
2644 * has been granted or write_acl is present and user
2645 * has the ability to modify mode. In that case remove
2646 * UID|GID and or MODE from mask so that
2647 * secpolicy_vnode_setattr() doesn't revoke it.
2651 saved_mask = vap->va_mask;
2652 vap->va_mask &= ~trim_mask;
2654 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2655 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2660 vap->va_mask |= saved_mask;
2664 * secpolicy_vnode_setattr, or take ownership may have
2667 mask = vap->va_mask;
2669 if ((mask & (ATTR_UID | ATTR_GID))) {
2670 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2671 &xattr_obj, sizeof (xattr_obj));
2673 if (err == 0 && xattr_obj) {
2674 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2678 if (mask & ATTR_UID) {
2679 new_uid = zfs_fuid_create(zsb,
2680 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2681 if (new_uid != zp->z_uid &&
2682 zfs_fuid_overquota(zsb, B_FALSE, new_uid)) {
2690 if (mask & ATTR_GID) {
2691 new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2692 cr, ZFS_GROUP, &fuidp);
2693 if (new_gid != zp->z_gid &&
2694 zfs_fuid_overquota(zsb, B_TRUE, new_gid)) {
2702 tx = dmu_tx_create(zsb->z_os);
2704 if (mask & ATTR_MODE) {
2705 uint64_t pmode = zp->z_mode;
2707 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2709 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2711 mutex_enter(&zp->z_lock);
2712 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2714 * Are we upgrading ACL from old V0 format
2717 if (zsb->z_version >= ZPL_VERSION_FUID &&
2718 zfs_znode_acl_version(zp) ==
2719 ZFS_ACL_VERSION_INITIAL) {
2720 dmu_tx_hold_free(tx, acl_obj, 0,
2722 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2723 0, aclp->z_acl_bytes);
2725 dmu_tx_hold_write(tx, acl_obj, 0,
2728 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2729 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2730 0, aclp->z_acl_bytes);
2732 mutex_exit(&zp->z_lock);
2733 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2735 if ((mask & ATTR_XVATTR) &&
2736 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2737 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2739 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2743 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2746 fuid_dirtied = zsb->z_fuid_dirty;
2748 zfs_fuid_txhold(zsb, tx);
2750 zfs_sa_upgrade_txholds(tx, zp);
2752 err = dmu_tx_assign(tx, TXG_NOWAIT);
2754 if (err == ERESTART)
2761 * Set each attribute requested.
2762 * We group settings according to the locks they need to acquire.
2764 * Note: you cannot set ctime directly, although it will be
2765 * updated as a side-effect of calling this function.
2769 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2770 mutex_enter(&zp->z_acl_lock);
2771 mutex_enter(&zp->z_lock);
2773 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2774 &zp->z_pflags, sizeof (zp->z_pflags));
2777 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2778 mutex_enter(&attrzp->z_acl_lock);
2779 mutex_enter(&attrzp->z_lock);
2780 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2781 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2782 sizeof (attrzp->z_pflags));
2785 if (mask & (ATTR_UID|ATTR_GID)) {
2787 if (mask & ATTR_UID) {
2788 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2789 &new_uid, sizeof (new_uid));
2790 zp->z_uid = new_uid;
2792 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2793 SA_ZPL_UID(zsb), NULL, &new_uid,
2795 attrzp->z_uid = new_uid;
2799 if (mask & ATTR_GID) {
2800 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2801 NULL, &new_gid, sizeof (new_gid));
2802 zp->z_gid = new_gid;
2804 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2805 SA_ZPL_GID(zsb), NULL, &new_gid,
2807 attrzp->z_gid = new_gid;
2810 if (!(mask & ATTR_MODE)) {
2811 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2812 NULL, &new_mode, sizeof (new_mode));
2813 new_mode = zp->z_mode;
2815 err = zfs_acl_chown_setattr(zp);
2818 err = zfs_acl_chown_setattr(attrzp);
2823 if (mask & ATTR_MODE) {
2824 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
2825 &new_mode, sizeof (new_mode));
2826 zp->z_mode = new_mode;
2827 ASSERT3P(aclp, !=, NULL);
2828 err = zfs_aclset_common(zp, aclp, cr, tx);
2829 ASSERT3U(err, ==, 0);
2830 if (zp->z_acl_cached)
2831 zfs_acl_free(zp->z_acl_cached);
2832 zp->z_acl_cached = aclp;
2837 if (mask & ATTR_ATIME) {
2838 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2839 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
2840 &zp->z_atime, sizeof (zp->z_atime));
2843 if (mask & ATTR_MTIME) {
2844 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2845 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
2846 mtime, sizeof (mtime));
2849 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2850 if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) {
2851 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
2852 NULL, mtime, sizeof (mtime));
2853 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2854 &ctime, sizeof (ctime));
2855 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
2857 } else if (mask != 0) {
2858 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2859 &ctime, sizeof (ctime));
2860 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
2863 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2864 SA_ZPL_CTIME(zsb), NULL,
2865 &ctime, sizeof (ctime));
2866 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2867 mtime, ctime, B_TRUE);
2871 * Do this after setting timestamps to prevent timestamp
2872 * update from toggling bit
2875 if (xoap && (mask & ATTR_XVATTR)) {
2878 * restore trimmed off masks
2879 * so that return masks can be set for caller.
2882 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
2883 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2885 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
2886 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2888 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
2889 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2891 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
2892 XVA_SET_REQ(xvap, XAT_NODUMP);
2894 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
2895 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2897 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
2898 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2901 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2902 ASSERT(S_ISREG(ip->i_mode));
2904 zfs_xvattr_set(zp, xvap, tx);
2908 zfs_fuid_sync(zsb, tx);
2911 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2913 mutex_exit(&zp->z_lock);
2914 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2915 mutex_exit(&zp->z_acl_lock);
2918 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2919 mutex_exit(&attrzp->z_acl_lock);
2920 mutex_exit(&attrzp->z_lock);
2923 if (err == 0 && attrzp) {
2924 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2935 zfs_fuid_info_free(fuidp);
2941 if (err == ERESTART)
2944 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2946 zfs_inode_update(zp);
2950 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
2951 zil_commit(zilog, 0);
2954 kmem_free(xattr_bulk, sizeof(sa_bulk_attr_t) * 7);
2955 kmem_free(bulk, sizeof(sa_bulk_attr_t) * 7);
2956 kmem_free(tmpxvattr, sizeof(xvattr_t));
2960 EXPORT_SYMBOL(zfs_setattr);
2962 typedef struct zfs_zlock {
2963 krwlock_t *zl_rwlock; /* lock we acquired */
2964 znode_t *zl_znode; /* znode we held */
2965 struct zfs_zlock *zl_next; /* next in list */
2969 * Drop locks and release vnodes that were held by zfs_rename_lock().
2972 zfs_rename_unlock(zfs_zlock_t **zlpp)
2976 while ((zl = *zlpp) != NULL) {
2977 if (zl->zl_znode != NULL)
2978 iput(ZTOI(zl->zl_znode));
2979 rw_exit(zl->zl_rwlock);
2980 *zlpp = zl->zl_next;
2981 kmem_free(zl, sizeof (*zl));
2986 * Search back through the directory tree, using the ".." entries.
2987 * Lock each directory in the chain to prevent concurrent renames.
2988 * Fail any attempt to move a directory into one of its own descendants.
2989 * XXX - z_parent_lock can overlap with map or grow locks
2992 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2996 uint64_t rootid = ZTOZSB(zp)->z_root;
2997 uint64_t oidp = zp->z_id;
2998 krwlock_t *rwlp = &szp->z_parent_lock;
2999 krw_t rw = RW_WRITER;
3002 * First pass write-locks szp and compares to zp->z_id.
3003 * Later passes read-lock zp and compare to zp->z_parent.
3006 if (!rw_tryenter(rwlp, rw)) {
3008 * Another thread is renaming in this path.
3009 * Note that if we are a WRITER, we don't have any
3010 * parent_locks held yet.
3012 if (rw == RW_READER && zp->z_id > szp->z_id) {
3014 * Drop our locks and restart
3016 zfs_rename_unlock(&zl);
3020 rwlp = &szp->z_parent_lock;
3025 * Wait for other thread to drop its locks
3031 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3032 zl->zl_rwlock = rwlp;
3033 zl->zl_znode = NULL;
3034 zl->zl_next = *zlpp;
3037 if (oidp == szp->z_id) /* We're a descendant of szp */
3040 if (oidp == rootid) /* We've hit the top */
3043 if (rw == RW_READER) { /* i.e. not the first pass */
3044 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3049 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3050 &oidp, sizeof (oidp));
3051 rwlp = &zp->z_parent_lock;
3054 } while (zp->z_id != sdzp->z_id);
3060 * Move an entry from the provided source directory to the target
3061 * directory. Change the entry name as indicated.
3063 * IN: sdip - Source directory containing the "old entry".
3064 * snm - Old entry name.
3065 * tdip - Target directory to contain the "new entry".
3066 * tnm - New entry name.
3067 * cr - credentials of caller.
3068 * flags - case flags
3070 * RETURN: 0 if success
3071 * error code if failure
3074 * sdip,tdip - ctime|mtime updated
3078 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3079 cred_t *cr, int flags)
3081 znode_t *tdzp, *szp, *tzp;
3082 znode_t *sdzp = ITOZ(sdip);
3083 zfs_sb_t *zsb = ITOZSB(sdip);
3085 zfs_dirlock_t *sdl, *tdl;
3088 int cmp, serr, terr;
3093 ZFS_VERIFY_ZP(sdzp);
3096 if (tdip->i_sb != sdip->i_sb) {
3102 ZFS_VERIFY_ZP(tdzp);
3103 if (zsb->z_utf8 && u8_validate(tnm,
3104 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3109 if (flags & FIGNORECASE)
3118 * This is to prevent the creation of links into attribute space
3119 * by renaming a linked file into/outof an attribute directory.
3120 * See the comment in zfs_link() for why this is considered bad.
3122 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3128 * Lock source and target directory entries. To prevent deadlock,
3129 * a lock ordering must be defined. We lock the directory with
3130 * the smallest object id first, or if it's a tie, the one with
3131 * the lexically first name.
3133 if (sdzp->z_id < tdzp->z_id) {
3135 } else if (sdzp->z_id > tdzp->z_id) {
3139 * First compare the two name arguments without
3140 * considering any case folding.
3142 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3144 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3145 ASSERT(error == 0 || !zsb->z_utf8);
3148 * POSIX: "If the old argument and the new argument
3149 * both refer to links to the same existing file,
3150 * the rename() function shall return successfully
3151 * and perform no other action."
3157 * If the file system is case-folding, then we may
3158 * have some more checking to do. A case-folding file
3159 * system is either supporting mixed case sensitivity
3160 * access or is completely case-insensitive. Note
3161 * that the file system is always case preserving.
3163 * In mixed sensitivity mode case sensitive behavior
3164 * is the default. FIGNORECASE must be used to
3165 * explicitly request case insensitive behavior.
3167 * If the source and target names provided differ only
3168 * by case (e.g., a request to rename 'tim' to 'Tim'),
3169 * we will treat this as a special case in the
3170 * case-insensitive mode: as long as the source name
3171 * is an exact match, we will allow this to proceed as
3172 * a name-change request.
3174 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3175 (zsb->z_case == ZFS_CASE_MIXED &&
3176 flags & FIGNORECASE)) &&
3177 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3180 * case preserving rename request, require exact
3189 * If the source and destination directories are the same, we should
3190 * grab the z_name_lock of that directory only once.
3194 rw_enter(&sdzp->z_name_lock, RW_READER);
3198 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3199 ZEXISTS | zflg, NULL, NULL);
3200 terr = zfs_dirent_lock(&tdl,
3201 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3203 terr = zfs_dirent_lock(&tdl,
3204 tdzp, tnm, &tzp, zflg, NULL, NULL);
3205 serr = zfs_dirent_lock(&sdl,
3206 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3212 * Source entry invalid or not there.
3215 zfs_dirent_unlock(tdl);
3221 rw_exit(&sdzp->z_name_lock);
3223 if (strcmp(snm, "..") == 0)
3229 zfs_dirent_unlock(sdl);
3233 rw_exit(&sdzp->z_name_lock);
3235 if (strcmp(tnm, "..") == 0)
3242 * Must have write access at the source to remove the old entry
3243 * and write access at the target to create the new entry.
3244 * Note that if target and source are the same, this can be
3245 * done in a single check.
3248 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3251 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3253 * Check to make sure rename is valid.
3254 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3256 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3261 * Does target exist?
3265 * Source and target must be the same type.
3267 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3268 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3273 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3279 * POSIX dictates that when the source and target
3280 * entries refer to the same file object, rename
3281 * must do nothing and exit without error.
3283 if (szp->z_id == tzp->z_id) {
3289 tx = dmu_tx_create(zsb->z_os);
3290 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3291 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3292 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3293 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3295 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3296 zfs_sa_upgrade_txholds(tx, tdzp);
3299 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3300 zfs_sa_upgrade_txholds(tx, tzp);
3303 zfs_sa_upgrade_txholds(tx, szp);
3304 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3305 error = dmu_tx_assign(tx, TXG_NOWAIT);
3308 zfs_rename_unlock(&zl);
3309 zfs_dirent_unlock(sdl);
3310 zfs_dirent_unlock(tdl);
3313 rw_exit(&sdzp->z_name_lock);
3318 if (error == ERESTART) {
3328 if (tzp) /* Attempt to remove the existing target */
3329 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3332 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3334 szp->z_pflags |= ZFS_AV_MODIFIED;
3336 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3337 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3338 ASSERT3U(error, ==, 0);
3340 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3342 zfs_log_rename(zilog, tx, TX_RENAME |
3343 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3344 sdl->dl_name, tdzp, tdl->dl_name, szp);
3347 * At this point, we have successfully created
3348 * the target name, but have failed to remove
3349 * the source name. Since the create was done
3350 * with the ZRENAMING flag, there are
3351 * complications; for one, the link count is
3352 * wrong. The easiest way to deal with this
3353 * is to remove the newly created target, and
3354 * return the original error. This must
3355 * succeed; fortunately, it is very unlikely to
3356 * fail, since we just created it.
3358 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3359 ZRENAMING, NULL), ==, 0);
3367 zfs_rename_unlock(&zl);
3369 zfs_dirent_unlock(sdl);
3370 zfs_dirent_unlock(tdl);
3372 zfs_inode_update(sdzp);
3374 rw_exit(&sdzp->z_name_lock);
3377 zfs_inode_update(tdzp);
3379 zfs_inode_update(szp);
3382 zfs_inode_update(tzp);
3386 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3387 zil_commit(zilog, 0);
3392 EXPORT_SYMBOL(zfs_rename);
3395 * Insert the indicated symbolic reference entry into the directory.
3397 * IN: dip - Directory to contain new symbolic link.
3398 * link - Name for new symlink entry.
3399 * vap - Attributes of new entry.
3400 * target - Target path of new symlink.
3402 * cr - credentials of caller.
3403 * flags - case flags
3405 * RETURN: 0 if success
3406 * error code if failure
3409 * dip - ctime|mtime updated
3413 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3414 struct inode **ipp, cred_t *cr, int flags)
3416 znode_t *zp, *dzp = ITOZ(dip);
3419 zfs_sb_t *zsb = ITOZSB(dip);
3421 uint64_t len = strlen(link);
3424 zfs_acl_ids_t acl_ids;
3425 boolean_t fuid_dirtied;
3426 uint64_t txtype = TX_SYMLINK;
3428 ASSERT(S_ISLNK(vap->va_mode));
3434 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3435 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3439 if (flags & FIGNORECASE)
3442 if (len > MAXPATHLEN) {
3444 return (ENAMETOOLONG);
3447 if ((error = zfs_acl_ids_create(dzp, 0,
3448 vap, cr, NULL, &acl_ids)) != 0) {
3456 * Attempt to lock directory; fail if entry already exists.
3458 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3460 zfs_acl_ids_free(&acl_ids);
3465 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3466 zfs_acl_ids_free(&acl_ids);
3467 zfs_dirent_unlock(dl);
3472 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3473 zfs_acl_ids_free(&acl_ids);
3474 zfs_dirent_unlock(dl);
3478 tx = dmu_tx_create(zsb->z_os);
3479 fuid_dirtied = zsb->z_fuid_dirty;
3480 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3481 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3482 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3483 ZFS_SA_BASE_ATTR_SIZE + len);
3484 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3485 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3486 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3487 acl_ids.z_aclp->z_acl_bytes);
3490 zfs_fuid_txhold(zsb, tx);
3491 error = dmu_tx_assign(tx, TXG_NOWAIT);
3493 zfs_dirent_unlock(dl);
3494 if (error == ERESTART) {
3499 zfs_acl_ids_free(&acl_ids);
3506 * Create a new object for the symlink.
3507 * for version 4 ZPL datsets the symlink will be an SA attribute
3509 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3512 zfs_fuid_sync(zsb, tx);
3514 mutex_enter(&zp->z_lock);
3516 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3519 zfs_sa_symlink(zp, link, len, tx);
3520 mutex_exit(&zp->z_lock);
3523 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3524 &zp->z_size, sizeof (zp->z_size), tx);
3526 * Insert the new object into the directory.
3528 (void) zfs_link_create(dl, zp, tx, ZNEW);
3530 if (flags & FIGNORECASE)
3532 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3534 zfs_inode_update(dzp);
3535 zfs_inode_update(zp);
3537 zfs_acl_ids_free(&acl_ids);
3541 zfs_dirent_unlock(dl);
3545 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3546 zil_commit(zilog, 0);
3551 EXPORT_SYMBOL(zfs_symlink);
3554 * Return, in the buffer contained in the provided uio structure,
3555 * the symbolic path referred to by ip.
3557 * IN: ip - inode of symbolic link
3558 * uio - structure to contain the link path.
3559 * cr - credentials of caller.
3561 * RETURN: 0 if success
3562 * error code if failure
3565 * ip - atime updated
3569 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3571 znode_t *zp = ITOZ(ip);
3572 zfs_sb_t *zsb = ITOZSB(ip);
3578 mutex_enter(&zp->z_lock);
3580 error = sa_lookup_uio(zp->z_sa_hdl,
3581 SA_ZPL_SYMLINK(zsb), uio);
3583 error = zfs_sa_readlink(zp, uio);
3584 mutex_exit(&zp->z_lock);
3586 ZFS_ACCESSTIME_STAMP(zsb, zp);
3587 zfs_inode_update(zp);
3591 EXPORT_SYMBOL(zfs_readlink);
3594 * Insert a new entry into directory tdip referencing sip.
3596 * IN: tdip - Directory to contain new entry.
3597 * sip - inode of new entry.
3598 * name - name of new entry.
3599 * cr - credentials of caller.
3601 * RETURN: 0 if success
3602 * error code if failure
3605 * tdip - ctime|mtime updated
3606 * sip - ctime updated
3610 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr)
3612 znode_t *dzp = ITOZ(tdip);
3614 zfs_sb_t *zsb = ITOZSB(tdip);
3623 ASSERT(S_ISDIR(tdip->i_mode));
3630 * POSIX dictates that we return EPERM here.
3631 * Better choices include ENOTSUP or EISDIR.
3633 if (S_ISDIR(sip->i_mode)) {
3638 if (sip->i_sb != tdip->i_sb) {
3646 /* Prevent links to .zfs/shares files */
3648 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3649 &parent, sizeof (uint64_t))) != 0) {
3653 if (parent == zsb->z_shares_dir) {
3658 if (zsb->z_utf8 && u8_validate(name,
3659 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3663 #ifdef HAVE_PN_UTILS
3664 if (flags & FIGNORECASE)
3666 #endif /* HAVE_PN_UTILS */
3669 * We do not support links between attributes and non-attributes
3670 * because of the potential security risk of creating links
3671 * into "normal" file space in order to circumvent restrictions
3672 * imposed in attribute space.
3674 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3679 owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER);
3680 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3685 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3692 * Attempt to lock directory; fail if entry already exists.
3694 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3700 tx = dmu_tx_create(zsb->z_os);
3701 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3702 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3703 zfs_sa_upgrade_txholds(tx, szp);
3704 zfs_sa_upgrade_txholds(tx, dzp);
3705 error = dmu_tx_assign(tx, TXG_NOWAIT);
3707 zfs_dirent_unlock(dl);
3708 if (error == ERESTART) {
3718 error = zfs_link_create(dl, szp, tx, 0);
3721 uint64_t txtype = TX_LINK;
3722 #ifdef HAVE_PN_UTILS
3723 if (flags & FIGNORECASE)
3725 #endif /* HAVE_PN_UTILS */
3726 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3731 zfs_dirent_unlock(dl);
3733 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3734 zil_commit(zilog, 0);
3736 zfs_inode_update(dzp);
3737 zfs_inode_update(szp);
3741 EXPORT_SYMBOL(zfs_link);
3744 zfs_putpage_commit_cb(void *arg, int error)
3746 struct page *pp = arg;
3749 __set_page_dirty_nobuffers(pp);
3751 if (error != ECANCELED)
3757 end_page_writeback(pp);
3761 * Push a page out to disk, once the page is on stable storage the
3762 * registered commit callback will be run as notification of completion.
3764 * IN: ip - page mapped for inode.
3765 * pp - page to push (page is locked)
3766 * wbc - writeback control data
3768 * RETURN: 0 if success
3769 * error code if failure
3772 * ip - ctime|mtime updated
3776 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
3778 znode_t *zp = ITOZ(ip);
3779 zfs_sb_t *zsb = ITOZSB(ip);
3786 uint64_t mtime[2], ctime[2];
3787 sa_bulk_attr_t bulk[3];
3791 ASSERT(PageLocked(pp));
3793 pgoff = page_offset(pp); /* Page byte-offset in file */
3794 offset = i_size_read(ip); /* File length in bytes */
3795 pglen = MIN(PAGE_CACHE_SIZE, /* Page length in bytes */
3796 P2ROUNDUP(offset, PAGE_CACHE_SIZE)-pgoff);
3798 /* Page is beyond end of file */
3799 if (pgoff >= offset) {
3804 /* Truncate page length to end of file */
3805 if (pgoff + pglen > offset)
3806 pglen = offset - pgoff;
3810 * FIXME: Allow mmap writes past its quota. The correct fix
3811 * is to register a page_mkwrite() handler to count the page
3812 * against its quota when it is about to be dirtied.
3814 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
3815 zfs_owner_overquota(zsb, zp, B_TRUE)) {
3820 set_page_writeback(pp);
3823 tx = dmu_tx_create(zsb->z_os);
3825 dmu_tx_callback_register(tx, zfs_putpage_commit_cb, pp);
3827 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
3829 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3830 zfs_sa_upgrade_txholds(tx, zp);
3831 err = dmu_tx_assign(tx, TXG_NOWAIT);
3833 if (err == ERESTART)
3841 ASSERT3U(pglen, <=, PAGE_CACHE_SIZE);
3842 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
3845 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3846 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3847 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
3848 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
3849 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0);
3852 ASSERT3S(err, ==, 0);
3854 if ((zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) ||
3855 (wbc->sync_mode == WB_SYNC_ALL))
3856 zil_commit(zsb->z_log, zp->z_id);
3863 zfs_inactive(struct inode *ip)
3865 znode_t *zp = ITOZ(ip);
3866 zfs_sb_t *zsb = ITOZSB(ip);
3869 #ifdef HAVE_SNAPSHOT
3870 /* Early return for snapshot inode? */
3871 #endif /* HAVE_SNAPSHOT */
3873 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
3874 if (zp->z_sa_hdl == NULL) {
3875 rw_exit(&zsb->z_teardown_inactive_lock);
3879 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3880 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
3882 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3883 zfs_sa_upgrade_txholds(tx, zp);
3884 error = dmu_tx_assign(tx, TXG_WAIT);
3888 mutex_enter(&zp->z_lock);
3889 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
3890 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3891 zp->z_atime_dirty = 0;
3892 mutex_exit(&zp->z_lock);
3898 rw_exit(&zsb->z_teardown_inactive_lock);
3900 EXPORT_SYMBOL(zfs_inactive);
3903 * Bounds-check the seek operation.
3905 * IN: ip - inode seeking within
3906 * ooff - old file offset
3907 * noffp - pointer to new file offset
3908 * ct - caller context
3910 * RETURN: 0 if success
3911 * EINVAL if new offset invalid
3915 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
3917 if (S_ISDIR(ip->i_mode))
3919 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3921 EXPORT_SYMBOL(zfs_seek);
3924 * Fill pages with data from the disk.
3927 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
3929 znode_t *zp = ITOZ(ip);
3930 zfs_sb_t *zsb = ITOZSB(ip);
3932 struct page *cur_pp;
3933 u_offset_t io_off, total;
3940 io_len = nr_pages << PAGE_CACHE_SHIFT;
3941 i_size = i_size_read(ip);
3942 io_off = page_offset(pl[0]);
3944 if (io_off + io_len > i_size)
3945 io_len = i_size - io_off;
3948 * Iterate over list of pages and read each page individually.
3952 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
3956 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
3960 /* convert checksum errors into IO errors */
3965 cur_pp = pl[++page_idx];
3972 * Uses zfs_fillpage to read data from the file and fill the pages.
3974 * IN: ip - inode of file to get data from.
3975 * pl - list of pages to read
3976 * nr_pages - number of pages to read
3978 * RETURN: 0 if success
3979 * error code if failure
3982 * vp - atime updated
3986 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
3988 znode_t *zp = ITOZ(ip);
3989 zfs_sb_t *zsb = ITOZSB(ip);
3998 err = zfs_fillpage(ip, pl, nr_pages);
4001 ZFS_ACCESSTIME_STAMP(zsb, zp);
4006 EXPORT_SYMBOL(zfs_getpage);
4009 * Check ZFS specific permissions to memory map a section of a file.
4011 * IN: ip - inode of the file to mmap
4013 * addrp - start address in memory region
4014 * len - length of memory region
4015 * vm_flags- address flags
4017 * RETURN: 0 if success
4018 * error code if failure
4022 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4023 unsigned long vm_flags)
4025 znode_t *zp = ITOZ(ip);
4026 zfs_sb_t *zsb = ITOZSB(ip);
4031 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4032 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4037 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4038 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4043 if (off < 0 || len > MAXOFFSET_T - off) {
4051 EXPORT_SYMBOL(zfs_map);
4054 * convoff - converts the given data (start, whence) to the
4058 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4063 if ((lckdat->l_whence == 2) || (whence == 2)) {
4064 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4068 switch (lckdat->l_whence) {
4070 lckdat->l_start += offset;
4073 lckdat->l_start += vap.va_size;
4081 if (lckdat->l_start < 0)
4086 lckdat->l_start -= offset;
4089 lckdat->l_start -= vap.va_size;
4097 lckdat->l_whence = (short)whence;
4102 * Free or allocate space in a file. Currently, this function only
4103 * supports the `F_FREESP' command. However, this command is somewhat
4104 * misnamed, as its functionality includes the ability to allocate as
4105 * well as free space.
4107 * IN: ip - inode of file to free data in.
4108 * cmd - action to take (only F_FREESP supported).
4109 * bfp - section of file to free/alloc.
4110 * flag - current file open mode flags.
4111 * offset - current file offset.
4112 * cr - credentials of caller [UNUSED].
4114 * RETURN: 0 if success
4115 * error code if failure
4118 * ip - ctime|mtime updated
4122 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4123 offset_t offset, cred_t *cr)
4125 znode_t *zp = ITOZ(ip);
4126 zfs_sb_t *zsb = ITOZSB(ip);
4133 if (cmd != F_FREESP) {
4138 if ((error = convoff(ip, bfp, 0, offset))) {
4143 if (bfp->l_len < 0) {
4149 len = bfp->l_len; /* 0 means from off to end of file */
4151 error = zfs_freesp(zp, off, len, flag, TRUE);
4156 EXPORT_SYMBOL(zfs_space);
4160 zfs_fid(struct inode *ip, fid_t *fidp)
4162 znode_t *zp = ITOZ(ip);
4163 zfs_sb_t *zsb = ITOZSB(ip);
4166 uint64_t object = zp->z_id;
4173 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4174 &gen64, sizeof (uint64_t))) != 0) {
4179 gen = (uint32_t)gen64;
4181 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4182 if (fidp->fid_len < size) {
4183 fidp->fid_len = size;
4188 zfid = (zfid_short_t *)fidp;
4190 zfid->zf_len = size;
4192 for (i = 0; i < sizeof (zfid->zf_object); i++)
4193 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4195 /* Must have a non-zero generation number to distinguish from .zfs */
4198 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4199 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4201 if (size == LONG_FID_LEN) {
4202 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4205 zlfid = (zfid_long_t *)fidp;
4207 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4208 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4210 /* XXX - this should be the generation number for the objset */
4211 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4212 zlfid->zf_setgen[i] = 0;
4218 EXPORT_SYMBOL(zfs_fid);
4222 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4224 znode_t *zp = ITOZ(ip);
4225 zfs_sb_t *zsb = ITOZSB(ip);
4227 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4231 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4236 EXPORT_SYMBOL(zfs_getsecattr);
4240 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4242 znode_t *zp = ITOZ(ip);
4243 zfs_sb_t *zsb = ITOZSB(ip);
4245 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4246 zilog_t *zilog = zsb->z_log;
4251 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4253 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4254 zil_commit(zilog, 0);
4259 EXPORT_SYMBOL(zfs_setsecattr);
4261 #ifdef HAVE_UIO_ZEROCOPY
4263 * Tunable, both must be a power of 2.
4265 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4266 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4267 * an arcbuf for a partial block read
4269 int zcr_blksz_min = (1 << 10); /* 1K */
4270 int zcr_blksz_max = (1 << 17); /* 128K */
4274 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4276 znode_t *zp = ITOZ(ip);
4277 zfs_sb_t *zsb = ITOZSB(ip);
4278 int max_blksz = zsb->z_max_blksz;
4279 uio_t *uio = &xuio->xu_uio;
4280 ssize_t size = uio->uio_resid;
4281 offset_t offset = uio->uio_loffset;
4286 int preamble, postamble;
4288 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4296 * Loan out an arc_buf for write if write size is bigger than
4297 * max_blksz, and the file's block size is also max_blksz.
4300 if (size < blksz || zp->z_blksz != blksz) {
4305 * Caller requests buffers for write before knowing where the
4306 * write offset might be (e.g. NFS TCP write).
4311 preamble = P2PHASE(offset, blksz);
4313 preamble = blksz - preamble;
4318 postamble = P2PHASE(size, blksz);
4321 fullblk = size / blksz;
4322 (void) dmu_xuio_init(xuio,
4323 (preamble != 0) + fullblk + (postamble != 0));
4326 * Have to fix iov base/len for partial buffers. They
4327 * currently represent full arc_buf's.
4330 /* data begins in the middle of the arc_buf */
4331 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4334 (void) dmu_xuio_add(xuio, abuf,
4335 blksz - preamble, preamble);
4338 for (i = 0; i < fullblk; i++) {
4339 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4342 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4346 /* data ends in the middle of the arc_buf */
4347 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4350 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4355 * Loan out an arc_buf for read if the read size is larger than
4356 * the current file block size. Block alignment is not
4357 * considered. Partial arc_buf will be loaned out for read.
4359 blksz = zp->z_blksz;
4360 if (blksz < zcr_blksz_min)
4361 blksz = zcr_blksz_min;
4362 if (blksz > zcr_blksz_max)
4363 blksz = zcr_blksz_max;
4364 /* avoid potential complexity of dealing with it */
4365 if (blksz > max_blksz) {
4370 maxsize = zp->z_size - uio->uio_loffset;
4384 uio->uio_extflg = UIO_XUIO;
4385 XUIO_XUZC_RW(xuio) = ioflag;
4392 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4396 int ioflag = XUIO_XUZC_RW(xuio);
4398 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4400 i = dmu_xuio_cnt(xuio);
4402 abuf = dmu_xuio_arcbuf(xuio, i);
4404 * if abuf == NULL, it must be a write buffer
4405 * that has been returned in zfs_write().
4408 dmu_return_arcbuf(abuf);
4409 ASSERT(abuf || ioflag == UIO_WRITE);
4412 dmu_xuio_fini(xuio);
4415 #endif /* HAVE_UIO_ZEROCOPY */
4417 #if defined(_KERNEL) && defined(HAVE_SPL)
4418 module_param(zfs_read_chunk_size, long, 0644);
4419 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");