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);
226 * Zero the synchronous opens in the znode. Under Linux the
227 * zfs_close() hook is not symmetric with zfs_open(), it is
228 * only called once when the last reference is dropped.
233 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
234 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
235 VERIFY(zfs_vscan(ip, cr, 1) == 0);
240 EXPORT_SYMBOL(zfs_close);
244 * When a file is memory mapped, we must keep the IO data synchronized
245 * between the DMU cache and the memory mapped pages. What this means:
247 * On Write: If we find a memory mapped page, we write to *both*
248 * the page and the dmu buffer.
251 update_pages(struct inode *ip, int64_t start, int len,
252 objset_t *os, uint64_t oid)
254 struct address_space *mp = ip->i_mapping;
260 off = start & (PAGE_CACHE_SIZE-1);
261 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
262 nbytes = MIN(PAGE_CACHE_SIZE - off, len);
264 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
266 if (mapping_writably_mapped(mp))
267 flush_dcache_page(pp);
270 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
274 if (mapping_writably_mapped(mp))
275 flush_dcache_page(pp);
277 mark_page_accessed(pp);
281 page_cache_release(pp);
290 * When a file is memory mapped, we must keep the IO data synchronized
291 * between the DMU cache and the memory mapped pages. What this means:
293 * On Read: We "read" preferentially from memory mapped pages,
294 * else we default from the dmu buffer.
296 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
297 * the file is memory mapped.
300 mappedread(struct inode *ip, int nbytes, uio_t *uio)
302 struct address_space *mp = ip->i_mapping;
304 znode_t *zp = ITOZ(ip);
305 objset_t *os = ITOZSB(ip)->z_os;
312 start = uio->uio_loffset;
313 off = start & (PAGE_CACHE_SIZE-1);
314 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
315 bytes = MIN(PAGE_CACHE_SIZE - off, len);
317 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
319 ASSERT(PageUptodate(pp));
322 error = uiomove(pb + off, bytes, UIO_READ, uio);
325 if (mapping_writably_mapped(mp))
326 flush_dcache_page(pp);
328 mark_page_accessed(pp);
330 page_cache_release(pp);
332 error = dmu_read_uio(os, zp->z_id, uio, bytes);
344 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
347 * Read bytes from specified file into supplied buffer.
349 * IN: ip - inode of file to be read from.
350 * uio - structure supplying read location, range info,
352 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
353 * O_DIRECT flag; used to bypass page cache.
354 * cr - credentials of caller.
356 * OUT: uio - updated offset and range, buffer filled.
358 * RETURN: 0 if success
359 * error code if failure
362 * inode - atime updated if byte count > 0
366 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
368 znode_t *zp = ITOZ(ip);
369 zfs_sb_t *zsb = ITOZSB(ip);
374 #ifdef HAVE_UIO_ZEROCOPY
376 #endif /* HAVE_UIO_ZEROCOPY */
382 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
388 * Validate file offset
390 if (uio->uio_loffset < (offset_t)0) {
396 * Fasttrack empty reads
398 if (uio->uio_resid == 0) {
404 * Check for mandatory locks
406 if (mandatory_lock(ip) &&
407 !lock_may_read(ip, uio->uio_loffset, uio->uio_resid)) {
413 * If we're in FRSYNC mode, sync out this znode before reading it.
415 if (ioflag & FRSYNC || zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
416 zil_commit(zsb->z_log, zp->z_id);
419 * Lock the range against changes.
421 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
424 * If we are reading past end-of-file we can skip
425 * to the end; but we might still need to set atime.
427 if (uio->uio_loffset >= zp->z_size) {
432 ASSERT(uio->uio_loffset < zp->z_size);
433 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
435 #ifdef HAVE_UIO_ZEROCOPY
436 if ((uio->uio_extflg == UIO_XUIO) &&
437 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
439 int blksz = zp->z_blksz;
440 uint64_t offset = uio->uio_loffset;
442 xuio = (xuio_t *)uio;
444 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
447 ASSERT(offset + n <= blksz);
450 (void) dmu_xuio_init(xuio, nblk);
452 if (vn_has_cached_data(ip)) {
454 * For simplicity, we always allocate a full buffer
455 * even if we only expect to read a portion of a block.
457 while (--nblk >= 0) {
458 (void) dmu_xuio_add(xuio,
459 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
464 #endif /* HAVE_UIO_ZEROCOPY */
467 nbytes = MIN(n, zfs_read_chunk_size -
468 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
470 if (zp->z_is_mapped && !(ioflag & O_DIRECT))
471 error = mappedread(ip, nbytes, uio);
473 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
476 /* convert checksum errors into IO errors */
485 zfs_range_unlock(rl);
487 ZFS_ACCESSTIME_STAMP(zsb, zp);
488 zfs_inode_update(zp);
492 EXPORT_SYMBOL(zfs_read);
495 * Write the bytes to a file.
497 * IN: ip - inode of file to be written to.
498 * uio - structure supplying write location, range info,
500 * ioflag - FAPPEND flag set if in append mode.
501 * O_DIRECT flag; used to bypass page cache.
502 * cr - credentials of caller.
504 * OUT: uio - updated offset and range.
506 * RETURN: 0 if success
507 * error code if failure
510 * ip - ctime|mtime updated if byte count > 0
515 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
517 znode_t *zp = ITOZ(ip);
518 rlim64_t limit = uio->uio_limit;
519 ssize_t start_resid = uio->uio_resid;
523 zfs_sb_t *zsb = ZTOZSB(zp);
528 int max_blksz = zsb->z_max_blksz;
531 iovec_t *aiov = NULL;
534 iovec_t *iovp = uio->uio_iov;
537 sa_bulk_attr_t bulk[4];
538 uint64_t mtime[2], ctime[2];
539 ASSERTV(int iovcnt = uio->uio_iovcnt);
542 * Fasttrack empty write
548 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
554 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
555 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
556 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
557 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
561 * If immutable or not appending then return EPERM
563 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
564 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
565 (uio->uio_loffset < zp->z_size))) {
573 * Validate file offset
575 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
582 * Check for mandatory locks before calling zfs_range_lock()
583 * in order to prevent a deadlock with locks set via fcntl().
585 if (mandatory_lock(ip) && !lock_may_write(ip, woff, n)) {
590 #ifdef HAVE_UIO_ZEROCOPY
592 * Pre-fault the pages to ensure slow (eg NFS) pages
594 * Skip this if uio contains loaned arc_buf.
596 if ((uio->uio_extflg == UIO_XUIO) &&
597 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
598 xuio = (xuio_t *)uio;
600 uio_prefaultpages(MIN(n, max_blksz), uio);
601 #endif /* HAVE_UIO_ZEROCOPY */
604 * If in append mode, set the io offset pointer to eof.
606 if (ioflag & FAPPEND) {
608 * Obtain an appending range lock to guarantee file append
609 * semantics. We reset the write offset once we have the lock.
611 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
613 if (rl->r_len == UINT64_MAX) {
615 * We overlocked the file because this write will cause
616 * the file block size to increase.
617 * Note that zp_size cannot change with this lock held.
621 uio->uio_loffset = woff;
624 * Note that if the file block size will change as a result of
625 * this write, then this range lock will lock the entire file
626 * so that we can re-write the block safely.
628 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
632 zfs_range_unlock(rl);
637 if ((woff + n) > limit || woff > (limit - n))
640 /* Will this write extend the file length? */
641 write_eof = (woff + n > zp->z_size);
643 end_size = MAX(zp->z_size, woff + n);
646 * Write the file in reasonable size chunks. Each chunk is written
647 * in a separate transaction; this keeps the intent log records small
648 * and allows us to do more fine-grained space accounting.
652 woff = uio->uio_loffset;
654 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
655 zfs_owner_overquota(zsb, zp, B_TRUE)) {
657 dmu_return_arcbuf(abuf);
662 if (xuio && abuf == NULL) {
663 ASSERT(i_iov < iovcnt);
665 abuf = dmu_xuio_arcbuf(xuio, i_iov);
666 dmu_xuio_clear(xuio, i_iov);
667 ASSERT((aiov->iov_base == abuf->b_data) ||
668 ((char *)aiov->iov_base - (char *)abuf->b_data +
669 aiov->iov_len == arc_buf_size(abuf)));
671 } else if (abuf == NULL && n >= max_blksz &&
672 woff >= zp->z_size &&
673 P2PHASE(woff, max_blksz) == 0 &&
674 zp->z_blksz == max_blksz) {
676 * This write covers a full block. "Borrow" a buffer
677 * from the dmu so that we can fill it before we enter
678 * a transaction. This avoids the possibility of
679 * holding up the transaction if the data copy hangs
680 * up on a pagefault (e.g., from an NFS server mapping).
684 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
686 ASSERT(abuf != NULL);
687 ASSERT(arc_buf_size(abuf) == max_blksz);
688 if ((error = uiocopy(abuf->b_data, max_blksz,
689 UIO_WRITE, uio, &cbytes))) {
690 dmu_return_arcbuf(abuf);
693 ASSERT(cbytes == max_blksz);
697 * Start a transaction.
699 tx = dmu_tx_create(zsb->z_os);
700 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
701 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
702 zfs_sa_upgrade_txholds(tx, zp);
703 error = dmu_tx_assign(tx, TXG_NOWAIT);
705 if (error == ERESTART) {
712 dmu_return_arcbuf(abuf);
717 * If zfs_range_lock() over-locked we grow the blocksize
718 * and then reduce the lock range. This will only happen
719 * on the first iteration since zfs_range_reduce() will
720 * shrink down r_len to the appropriate size.
722 if (rl->r_len == UINT64_MAX) {
725 if (zp->z_blksz > max_blksz) {
726 ASSERT(!ISP2(zp->z_blksz));
727 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
729 new_blksz = MIN(end_size, max_blksz);
731 zfs_grow_blocksize(zp, new_blksz, tx);
732 zfs_range_reduce(rl, woff, n);
736 * XXX - should we really limit each write to z_max_blksz?
737 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
739 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
742 tx_bytes = uio->uio_resid;
743 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
745 tx_bytes -= uio->uio_resid;
748 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
750 * If this is not a full block write, but we are
751 * extending the file past EOF and this data starts
752 * block-aligned, use assign_arcbuf(). Otherwise,
753 * write via dmu_write().
755 if (tx_bytes < max_blksz && (!write_eof ||
756 aiov->iov_base != abuf->b_data)) {
758 dmu_write(zsb->z_os, zp->z_id, woff,
759 aiov->iov_len, aiov->iov_base, tx);
760 dmu_return_arcbuf(abuf);
761 xuio_stat_wbuf_copied();
763 ASSERT(xuio || tx_bytes == max_blksz);
764 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
767 ASSERT(tx_bytes <= uio->uio_resid);
768 uioskip(uio, tx_bytes);
771 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
772 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
775 * If we made no progress, we're done. If we made even
776 * partial progress, update the znode and ZIL accordingly.
779 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
780 (void *)&zp->z_size, sizeof (uint64_t), tx);
787 * Clear Set-UID/Set-GID bits on successful write if not
788 * privileged and at least one of the excute bits is set.
790 * It would be nice to to this after all writes have
791 * been done, but that would still expose the ISUID/ISGID
792 * to another app after the partial write is committed.
794 * Note: we don't call zfs_fuid_map_id() here because
795 * user 0 is not an ephemeral uid.
797 mutex_enter(&zp->z_acl_lock);
798 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
799 (S_IXUSR >> 6))) != 0 &&
800 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
801 secpolicy_vnode_setid_retain(cr,
802 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
804 zp->z_mode &= ~(S_ISUID | S_ISGID);
805 newmode = zp->z_mode;
806 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
807 (void *)&newmode, sizeof (uint64_t), tx);
809 mutex_exit(&zp->z_acl_lock);
811 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
815 * Update the file size (zp_size) if it has changed;
816 * account for possible concurrent updates.
818 while ((end_size = zp->z_size) < uio->uio_loffset) {
819 (void) atomic_cas_64(&zp->z_size, end_size,
824 * If we are replaying and eof is non zero then force
825 * the file size to the specified eof. Note, there's no
826 * concurrency during replay.
828 if (zsb->z_replay && zsb->z_replay_eof != 0)
829 zp->z_size = zsb->z_replay_eof;
831 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
833 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
838 ASSERT(tx_bytes == nbytes);
842 uio_prefaultpages(MIN(n, max_blksz), uio);
845 zfs_range_unlock(rl);
848 * If we're in replay mode, or we made no progress, return error.
849 * Otherwise, it's at least a partial write, so it's successful.
851 if (zsb->z_replay || uio->uio_resid == start_resid) {
856 if (ioflag & (FSYNC | FDSYNC) ||
857 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
858 zil_commit(zilog, zp->z_id);
860 zfs_inode_update(zp);
864 EXPORT_SYMBOL(zfs_write);
867 iput_async(struct inode *ip, taskq_t *taskq)
869 ASSERT(atomic_read(&ip->i_count) > 0);
870 if (atomic_read(&ip->i_count) == 1)
871 taskq_dispatch(taskq, (task_func_t *)iput, ip, TQ_PUSHPAGE);
877 zfs_get_done(zgd_t *zgd, int error)
879 znode_t *zp = zgd->zgd_private;
880 objset_t *os = ZTOZSB(zp)->z_os;
883 dmu_buf_rele(zgd->zgd_db, zgd);
885 zfs_range_unlock(zgd->zgd_rl);
888 * Release the vnode asynchronously as we currently have the
889 * txg stopped from syncing.
891 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
893 if (error == 0 && zgd->zgd_bp)
894 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
896 kmem_free(zgd, sizeof (zgd_t));
900 static int zil_fault_io = 0;
904 * Get data to generate a TX_WRITE intent log record.
907 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
910 objset_t *os = zsb->z_os;
912 uint64_t object = lr->lr_foid;
913 uint64_t offset = lr->lr_offset;
914 uint64_t size = lr->lr_length;
915 blkptr_t *bp = &lr->lr_blkptr;
924 * Nothing to do if the file has been removed
926 if (zfs_zget(zsb, object, &zp) != 0)
928 if (zp->z_unlinked) {
930 * Release the vnode asynchronously as we currently have the
931 * txg stopped from syncing.
933 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
937 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_PUSHPAGE);
938 zgd->zgd_zilog = zsb->z_log;
939 zgd->zgd_private = zp;
942 * Write records come in two flavors: immediate and indirect.
943 * For small writes it's cheaper to store the data with the
944 * log record (immediate); for large writes it's cheaper to
945 * sync the data and get a pointer to it (indirect) so that
946 * we don't have to write the data twice.
948 if (buf != NULL) { /* immediate write */
949 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
950 /* test for truncation needs to be done while range locked */
951 if (offset >= zp->z_size) {
954 error = dmu_read(os, object, offset, size, buf,
955 DMU_READ_NO_PREFETCH);
957 ASSERT(error == 0 || error == ENOENT);
958 } else { /* indirect write */
960 * Have to lock the whole block to ensure when it's
961 * written out and it's checksum is being calculated
962 * that no one can change the data. We need to re-check
963 * blocksize after we get the lock in case it's changed!
968 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
970 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
972 if (zp->z_blksz == size)
975 zfs_range_unlock(zgd->zgd_rl);
977 /* test for truncation needs to be done while range locked */
978 if (lr->lr_offset >= zp->z_size)
987 error = dmu_buf_hold(os, object, offset, zgd, &db,
988 DMU_READ_NO_PREFETCH);
994 ASSERT(db->db_offset == offset);
995 ASSERT(db->db_size == size);
997 error = dmu_sync(zio, lr->lr_common.lrc_txg,
999 ASSERT(error || lr->lr_length <= zp->z_blksz);
1002 * On success, we need to wait for the write I/O
1003 * initiated by dmu_sync() to complete before we can
1004 * release this dbuf. We will finish everything up
1005 * in the zfs_get_done() callback.
1010 if (error == EALREADY) {
1011 lr->lr_common.lrc_txtype = TX_WRITE2;
1017 zfs_get_done(zgd, error);
1024 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1026 znode_t *zp = ITOZ(ip);
1027 zfs_sb_t *zsb = ITOZSB(ip);
1033 if (flag & V_ACE_MASK)
1034 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1036 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1041 EXPORT_SYMBOL(zfs_access);
1044 * Lookup an entry in a directory, or an extended attribute directory.
1045 * If it exists, return a held inode reference for it.
1047 * IN: dip - inode of directory to search.
1048 * nm - name of entry to lookup.
1049 * flags - LOOKUP_XATTR set if looking for an attribute.
1050 * cr - credentials of caller.
1051 * direntflags - directory lookup flags
1052 * realpnp - returned pathname.
1054 * OUT: ipp - inode of located entry, NULL if not found.
1056 * RETURN: 0 if success
1057 * error code if failure
1064 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1065 cred_t *cr, int *direntflags, pathname_t *realpnp)
1067 znode_t *zdp = ITOZ(dip);
1068 zfs_sb_t *zsb = ITOZSB(dip);
1072 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1074 if (!S_ISDIR(dip->i_mode)) {
1076 } else if (zdp->z_sa_hdl == NULL) {
1080 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1081 error = zfs_fastaccesschk_execute(zdp, cr);
1090 vnode_t *tvp = dnlc_lookup(dvp, nm);
1093 error = zfs_fastaccesschk_execute(zdp, cr);
1098 if (tvp == DNLC_NO_VNODE) {
1103 return (specvp_check(vpp, cr));
1106 #endif /* HAVE_DNLC */
1115 if (flags & LOOKUP_XATTR) {
1117 * We don't allow recursive attributes..
1118 * Maybe someday we will.
1120 if (zdp->z_pflags & ZFS_XATTR) {
1125 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1131 * Do we have permission to get into attribute directory?
1134 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1144 if (!S_ISDIR(dip->i_mode)) {
1150 * Check accessibility of directory.
1153 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1158 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
1159 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1164 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1165 if ((error == 0) && (*ipp))
1166 zfs_inode_update(ITOZ(*ipp));
1171 EXPORT_SYMBOL(zfs_lookup);
1174 * Attempt to create a new entry in a directory. If the entry
1175 * already exists, truncate the file if permissible, else return
1176 * an error. Return the ip of the created or trunc'd file.
1178 * IN: dip - inode of directory to put new file entry in.
1179 * name - name of new file entry.
1180 * vap - attributes of new file.
1181 * excl - flag indicating exclusive or non-exclusive mode.
1182 * mode - mode to open file with.
1183 * cr - credentials of caller.
1184 * flag - large file flag [UNUSED].
1185 * vsecp - ACL to be set
1187 * OUT: ipp - inode of created or trunc'd entry.
1189 * RETURN: 0 if success
1190 * error code if failure
1193 * dip - ctime|mtime updated if new entry created
1194 * ip - ctime|mtime always, atime if new
1199 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1200 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1202 znode_t *zp, *dzp = ITOZ(dip);
1203 zfs_sb_t *zsb = ITOZSB(dip);
1211 zfs_acl_ids_t acl_ids;
1212 boolean_t fuid_dirtied;
1213 boolean_t have_acl = B_FALSE;
1216 * If we have an ephemeral id, ACL, or XVATTR then
1217 * make sure file system is at proper version
1223 if (zsb->z_use_fuids == B_FALSE &&
1224 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1232 if (zsb->z_utf8 && u8_validate(name, strlen(name),
1233 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1238 if (vap->va_mask & ATTR_XVATTR) {
1239 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1240 crgetuid(cr), cr, vap->va_mode)) != 0) {
1248 if (*name == '\0') {
1250 * Null component name refers to the directory itself.
1257 /* possible igrab(zp) */
1260 if (flag & FIGNORECASE)
1263 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1267 zfs_acl_ids_free(&acl_ids);
1268 if (strcmp(name, "..") == 0)
1279 * Create a new file object and update the directory
1282 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1284 zfs_acl_ids_free(&acl_ids);
1289 * We only support the creation of regular files in
1290 * extended attribute directories.
1293 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1295 zfs_acl_ids_free(&acl_ids);
1300 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1301 cr, vsecp, &acl_ids)) != 0)
1305 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1306 zfs_acl_ids_free(&acl_ids);
1311 tx = dmu_tx_create(os);
1313 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1314 ZFS_SA_BASE_ATTR_SIZE);
1316 fuid_dirtied = zsb->z_fuid_dirty;
1318 zfs_fuid_txhold(zsb, tx);
1319 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1320 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1321 if (!zsb->z_use_sa &&
1322 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1323 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1324 0, acl_ids.z_aclp->z_acl_bytes);
1326 error = dmu_tx_assign(tx, TXG_NOWAIT);
1328 zfs_dirent_unlock(dl);
1329 if (error == ERESTART) {
1334 zfs_acl_ids_free(&acl_ids);
1339 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1342 zfs_fuid_sync(zsb, tx);
1344 (void) zfs_link_create(dl, zp, tx, ZNEW);
1345 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1346 if (flag & FIGNORECASE)
1348 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1349 vsecp, acl_ids.z_fuidp, vap);
1350 zfs_acl_ids_free(&acl_ids);
1353 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1356 zfs_acl_ids_free(&acl_ids);
1360 * A directory entry already exists for this name.
1363 * Can't truncate an existing file if in exclusive mode.
1370 * Can't open a directory for writing.
1372 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1377 * Verify requested access to file.
1379 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1383 mutex_enter(&dzp->z_lock);
1385 mutex_exit(&dzp->z_lock);
1388 * Truncate regular files if requested.
1390 if (S_ISREG(ZTOI(zp)->i_mode) &&
1391 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1392 /* we can't hold any locks when calling zfs_freesp() */
1393 zfs_dirent_unlock(dl);
1395 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1401 zfs_dirent_unlock(dl);
1407 zfs_inode_update(dzp);
1408 zfs_inode_update(zp);
1412 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1413 zil_commit(zilog, 0);
1418 EXPORT_SYMBOL(zfs_create);
1421 * Remove an entry from a directory.
1423 * IN: dip - inode of directory to remove entry from.
1424 * name - name of entry to remove.
1425 * cr - credentials of caller.
1427 * RETURN: 0 if success
1428 * error code if failure
1432 * ip - ctime (if nlink > 0)
1435 uint64_t null_xattr = 0;
1439 zfs_remove(struct inode *dip, char *name, cred_t *cr)
1441 znode_t *zp, *dzp = ITOZ(dip);
1444 zfs_sb_t *zsb = ITOZSB(dip);
1447 uint64_t xattr_obj_unlinked = 0;
1453 pathname_t *realnmp = NULL;
1454 #ifdef HAVE_PN_UTILS
1456 #endif /* HAVE_PN_UTILS */
1464 #ifdef HAVE_PN_UTILS
1465 if (flags & FIGNORECASE) {
1470 #endif /* HAVE_PN_UTILS */
1476 * Attempt to lock directory; fail if entry doesn't exist.
1478 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1480 #ifdef HAVE_PN_UTILS
1483 #endif /* HAVE_PN_UTILS */
1490 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1495 * Need to use rmdir for removing directories.
1497 if (S_ISDIR(ip->i_mode)) {
1504 dnlc_remove(dvp, realnmp->pn_buf);
1506 dnlc_remove(dvp, name);
1507 #endif /* HAVE_DNLC */
1510 * We never delete the znode and always place it in the unlinked
1511 * set. The dentry cache will always hold the last reference and
1512 * is responsible for safely freeing the znode.
1515 tx = dmu_tx_create(zsb->z_os);
1516 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1517 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1518 zfs_sa_upgrade_txholds(tx, zp);
1519 zfs_sa_upgrade_txholds(tx, dzp);
1521 /* are there any extended attributes? */
1522 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1523 &xattr_obj, sizeof (xattr_obj));
1524 if (error == 0 && xattr_obj) {
1525 error = zfs_zget(zsb, xattr_obj, &xzp);
1526 ASSERT3U(error, ==, 0);
1527 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1528 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1531 /* charge as an update -- would be nice not to charge at all */
1532 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1534 error = dmu_tx_assign(tx, TXG_NOWAIT);
1536 zfs_dirent_unlock(dl);
1540 if (error == ERESTART) {
1545 #ifdef HAVE_PN_UTILS
1548 #endif /* HAVE_PN_UTILS */
1555 * Remove the directory entry.
1557 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1566 * Hold z_lock so that we can make sure that the ACL obj
1567 * hasn't changed. Could have been deleted due to
1570 mutex_enter(&zp->z_lock);
1571 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1572 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1573 mutex_exit(&zp->z_lock);
1574 zfs_unlinked_add(zp, tx);
1578 #ifdef HAVE_PN_UTILS
1579 if (flags & FIGNORECASE)
1581 #endif /* HAVE_PN_UTILS */
1582 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1586 #ifdef HAVE_PN_UTILS
1589 #endif /* HAVE_PN_UTILS */
1591 zfs_dirent_unlock(dl);
1592 zfs_inode_update(dzp);
1593 zfs_inode_update(zp);
1595 zfs_inode_update(xzp);
1601 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1602 zil_commit(zilog, 0);
1607 EXPORT_SYMBOL(zfs_remove);
1610 * Create a new directory and insert it into dip using the name
1611 * provided. Return a pointer to the inserted directory.
1613 * IN: dip - inode of directory to add subdir to.
1614 * dirname - name of new directory.
1615 * vap - attributes of new directory.
1616 * cr - credentials of caller.
1617 * vsecp - ACL to be set
1619 * OUT: ipp - inode of created directory.
1621 * RETURN: 0 if success
1622 * error code if failure
1625 * dip - ctime|mtime updated
1626 * ipp - ctime|mtime|atime updated
1630 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1631 cred_t *cr, int flags, vsecattr_t *vsecp)
1633 znode_t *zp, *dzp = ITOZ(dip);
1634 zfs_sb_t *zsb = ITOZSB(dip);
1642 gid_t gid = crgetgid(cr);
1643 zfs_acl_ids_t acl_ids;
1644 boolean_t fuid_dirtied;
1646 ASSERT(S_ISDIR(vap->va_mode));
1649 * If we have an ephemeral id, ACL, or XVATTR then
1650 * make sure file system is at proper version
1654 if (zsb->z_use_fuids == B_FALSE &&
1655 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1662 if (dzp->z_pflags & ZFS_XATTR) {
1667 if (zsb->z_utf8 && u8_validate(dirname,
1668 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1672 if (flags & FIGNORECASE)
1675 if (vap->va_mask & ATTR_XVATTR) {
1676 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1677 crgetuid(cr), cr, vap->va_mode)) != 0) {
1683 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1684 vsecp, &acl_ids)) != 0) {
1689 * First make sure the new directory doesn't exist.
1691 * Existence is checked first to make sure we don't return
1692 * EACCES instead of EEXIST which can cause some applications
1698 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1700 zfs_acl_ids_free(&acl_ids);
1705 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1706 zfs_acl_ids_free(&acl_ids);
1707 zfs_dirent_unlock(dl);
1712 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1713 zfs_acl_ids_free(&acl_ids);
1714 zfs_dirent_unlock(dl);
1720 * Add a new entry to the directory.
1722 tx = dmu_tx_create(zsb->z_os);
1723 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1724 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1725 fuid_dirtied = zsb->z_fuid_dirty;
1727 zfs_fuid_txhold(zsb, tx);
1728 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1729 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1730 acl_ids.z_aclp->z_acl_bytes);
1733 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1734 ZFS_SA_BASE_ATTR_SIZE);
1736 error = dmu_tx_assign(tx, TXG_NOWAIT);
1738 zfs_dirent_unlock(dl);
1739 if (error == ERESTART) {
1744 zfs_acl_ids_free(&acl_ids);
1753 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1756 zfs_fuid_sync(zsb, tx);
1759 * Now put new name in parent dir.
1761 (void) zfs_link_create(dl, zp, tx, ZNEW);
1765 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1766 if (flags & FIGNORECASE)
1768 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1769 acl_ids.z_fuidp, vap);
1771 zfs_acl_ids_free(&acl_ids);
1775 zfs_dirent_unlock(dl);
1777 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1778 zil_commit(zilog, 0);
1780 zfs_inode_update(dzp);
1781 zfs_inode_update(zp);
1785 EXPORT_SYMBOL(zfs_mkdir);
1788 * Remove a directory subdir entry. If the current working
1789 * directory is the same as the subdir to be removed, the
1792 * IN: dip - inode of directory to remove from.
1793 * name - name of directory to be removed.
1794 * cwd - inode of current working directory.
1795 * cr - credentials of caller.
1796 * flags - case flags
1798 * RETURN: 0 if success
1799 * error code if failure
1802 * dip - ctime|mtime updated
1806 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1809 znode_t *dzp = ITOZ(dip);
1812 zfs_sb_t *zsb = ITOZSB(dip);
1823 if (flags & FIGNORECASE)
1829 * Attempt to lock directory; fail if entry doesn't exist.
1831 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1839 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1843 if (!S_ISDIR(ip->i_mode)) {
1854 * Grab a lock on the directory to make sure that noone is
1855 * trying to add (or lookup) entries while we are removing it.
1857 rw_enter(&zp->z_name_lock, RW_WRITER);
1860 * Grab a lock on the parent pointer to make sure we play well
1861 * with the treewalk and directory rename code.
1863 rw_enter(&zp->z_parent_lock, RW_WRITER);
1865 tx = dmu_tx_create(zsb->z_os);
1866 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1867 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1868 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1869 zfs_sa_upgrade_txholds(tx, zp);
1870 zfs_sa_upgrade_txholds(tx, dzp);
1871 error = dmu_tx_assign(tx, TXG_NOWAIT);
1873 rw_exit(&zp->z_parent_lock);
1874 rw_exit(&zp->z_name_lock);
1875 zfs_dirent_unlock(dl);
1877 if (error == ERESTART) {
1887 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1890 uint64_t txtype = TX_RMDIR;
1891 if (flags & FIGNORECASE)
1893 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
1898 rw_exit(&zp->z_parent_lock);
1899 rw_exit(&zp->z_name_lock);
1901 zfs_dirent_unlock(dl);
1903 zfs_inode_update(dzp);
1904 zfs_inode_update(zp);
1907 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1908 zil_commit(zilog, 0);
1913 EXPORT_SYMBOL(zfs_rmdir);
1916 * Read as many directory entries as will fit into the provided
1917 * dirent buffer from the given directory cursor position.
1919 * IN: ip - inode of directory to read.
1920 * dirent - buffer for directory entries.
1922 * OUT: dirent - filler buffer of directory entries.
1924 * RETURN: 0 if success
1925 * error code if failure
1928 * ip - atime updated
1930 * Note that the low 4 bits of the cookie returned by zap is always zero.
1931 * This allows us to use the low range for "special" directory entries:
1932 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1933 * we use the offset 2 for the '.zfs' directory.
1937 zfs_readdir(struct inode *ip, void *dirent, filldir_t filldir,
1938 loff_t *pos, cred_t *cr)
1940 znode_t *zp = ITOZ(ip);
1941 zfs_sb_t *zsb = ITOZSB(ip);
1944 zap_attribute_t zap;
1954 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
1955 &parent, sizeof (parent))) != 0)
1959 * Quit if directory has been removed (posix)
1966 prefetch = zp->z_zn_prefetch;
1969 * Initialize the iterator cursor.
1973 * Start iteration from the beginning of the directory.
1975 zap_cursor_init(&zc, os, zp->z_id);
1978 * The offset is a serialized cursor.
1980 zap_cursor_init_serialized(&zc, os, zp->z_id, *pos);
1984 * Transform to file-system independent format
1991 * Special case `.', `..', and `.zfs'.
1994 (void) strcpy(zap.za_name, ".");
1995 zap.za_normalization_conflict = 0;
1997 } else if (*pos == 1) {
1998 (void) strcpy(zap.za_name, "..");
1999 zap.za_normalization_conflict = 0;
2001 } else if (*pos == 2 && zfs_show_ctldir(zp)) {
2002 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2003 zap.za_normalization_conflict = 0;
2004 objnum = ZFSCTL_INO_ROOT;
2009 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2010 if (error == ENOENT)
2017 * Allow multiple entries provided the first entry is
2018 * the object id. Non-zpl consumers may safely make
2019 * use of the additional space.
2021 * XXX: This should be a feature flag for compatibility
2023 if (zap.za_integer_length != 8 ||
2024 zap.za_num_integers == 0) {
2025 cmn_err(CE_WARN, "zap_readdir: bad directory "
2026 "entry, obj = %lld, offset = %lld, "
2027 "length = %d, num = %lld\n",
2028 (u_longlong_t)zp->z_id,
2030 zap.za_integer_length,
2031 (u_longlong_t)zap.za_num_integers);
2036 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2038 done = filldir(dirent, zap.za_name, strlen(zap.za_name),
2039 zap_cursor_serialize(&zc), objnum, 0);
2044 /* Prefetch znode */
2046 dmu_prefetch(os, objnum, 0, 0);
2049 if (*pos > 2 || (*pos == 2 && !zfs_show_ctldir(zp))) {
2050 zap_cursor_advance(&zc);
2051 *pos = zap_cursor_serialize(&zc);
2056 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2059 zap_cursor_fini(&zc);
2060 if (error == ENOENT)
2063 ZFS_ACCESSTIME_STAMP(zsb, zp);
2064 zfs_inode_update(zp);
2071 EXPORT_SYMBOL(zfs_readdir);
2074 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2076 znode_t *zp = ITOZ(ip);
2077 zfs_sb_t *zsb = ITOZSB(ip);
2079 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2082 zil_commit(zsb->z_log, zp->z_id);
2087 EXPORT_SYMBOL(zfs_fsync);
2091 * Get the requested file attributes and place them in the provided
2094 * IN: ip - inode of file.
2095 * vap - va_mask identifies requested attributes.
2096 * If ATTR_XVATTR set, then optional attrs are requested
2097 * flags - ATTR_NOACLCHECK (CIFS server context)
2098 * cr - credentials of caller.
2100 * OUT: vap - attribute values.
2102 * RETURN: 0 (always succeeds)
2106 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2108 znode_t *zp = ITOZ(ip);
2109 zfs_sb_t *zsb = ITOZSB(ip);
2112 uint64_t mtime[2], ctime[2];
2113 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2114 xoptattr_t *xoap = NULL;
2115 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2116 sa_bulk_attr_t bulk[2];
2122 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2124 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2125 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2127 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2133 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2134 * Also, if we are the owner don't bother, since owner should
2135 * always be allowed to read basic attributes of file.
2137 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2138 (vap->va_uid != crgetuid(cr))) {
2139 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2147 * Return all attributes. It's cheaper to provide the answer
2148 * than to determine whether we were asked the question.
2151 mutex_enter(&zp->z_lock);
2152 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2153 vap->va_mode = zp->z_mode;
2154 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2155 vap->va_nodeid = zp->z_id;
2156 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2157 links = zp->z_links + 1;
2159 links = zp->z_links;
2160 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2161 vap->va_size = i_size_read(ip);
2162 vap->va_rdev = ip->i_rdev;
2163 vap->va_seq = ip->i_generation;
2166 * Add in any requested optional attributes and the create time.
2167 * Also set the corresponding bits in the returned attribute bitmap.
2169 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2170 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2172 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2173 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2176 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2177 xoap->xoa_readonly =
2178 ((zp->z_pflags & ZFS_READONLY) != 0);
2179 XVA_SET_RTN(xvap, XAT_READONLY);
2182 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2184 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2185 XVA_SET_RTN(xvap, XAT_SYSTEM);
2188 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2190 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2191 XVA_SET_RTN(xvap, XAT_HIDDEN);
2194 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2195 xoap->xoa_nounlink =
2196 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2197 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2200 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2201 xoap->xoa_immutable =
2202 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2203 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2206 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2207 xoap->xoa_appendonly =
2208 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2209 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2212 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2214 ((zp->z_pflags & ZFS_NODUMP) != 0);
2215 XVA_SET_RTN(xvap, XAT_NODUMP);
2218 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2220 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2221 XVA_SET_RTN(xvap, XAT_OPAQUE);
2224 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2225 xoap->xoa_av_quarantined =
2226 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2227 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2230 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2231 xoap->xoa_av_modified =
2232 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2233 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2236 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2237 S_ISREG(ip->i_mode)) {
2238 zfs_sa_get_scanstamp(zp, xvap);
2241 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2244 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2245 times, sizeof (times));
2246 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2247 XVA_SET_RTN(xvap, XAT_CREATETIME);
2250 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2251 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2252 XVA_SET_RTN(xvap, XAT_REPARSE);
2254 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2255 xoap->xoa_generation = zp->z_gen;
2256 XVA_SET_RTN(xvap, XAT_GEN);
2259 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2261 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2262 XVA_SET_RTN(xvap, XAT_OFFLINE);
2265 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2267 ((zp->z_pflags & ZFS_SPARSE) != 0);
2268 XVA_SET_RTN(xvap, XAT_SPARSE);
2272 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2273 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2274 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2276 mutex_exit(&zp->z_lock);
2278 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2280 if (zp->z_blksz == 0) {
2282 * Block size hasn't been set; suggest maximal I/O transfers.
2284 vap->va_blksize = zsb->z_max_blksz;
2290 EXPORT_SYMBOL(zfs_getattr);
2293 * Get the basic file attributes and place them in the provided kstat
2294 * structure. The inode is assumed to be the authoritative source
2295 * for most of the attributes. However, the znode currently has the
2296 * authoritative atime, blksize, and block count.
2298 * IN: ip - inode of file.
2300 * OUT: sp - kstat values.
2302 * RETURN: 0 (always succeeds)
2306 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2308 znode_t *zp = ITOZ(ip);
2309 zfs_sb_t *zsb = ITOZSB(ip);
2314 mutex_enter(&zp->z_lock);
2316 generic_fillattr(ip, sp);
2317 ZFS_TIME_DECODE(&sp->atime, zp->z_atime);
2319 sa_object_size(zp->z_sa_hdl, (uint32_t *)&sp->blksize, &sp->blocks);
2320 if (unlikely(zp->z_blksz == 0)) {
2322 * Block size hasn't been set; suggest maximal I/O transfers.
2324 sp->blksize = zsb->z_max_blksz;
2327 mutex_exit(&zp->z_lock);
2333 EXPORT_SYMBOL(zfs_getattr_fast);
2336 * Set the file attributes to the values contained in the
2339 * IN: ip - inode of file to be modified.
2340 * vap - new attribute values.
2341 * If ATTR_XVATTR set, then optional attrs are being set
2342 * flags - ATTR_UTIME set if non-default time values provided.
2343 * - ATTR_NOACLCHECK (CIFS context only).
2344 * cr - credentials of caller.
2346 * RETURN: 0 if success
2347 * error code if failure
2350 * ip - ctime updated, mtime updated if size changed.
2354 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2356 znode_t *zp = ITOZ(ip);
2357 zfs_sb_t *zsb = ITOZSB(ip);
2361 xvattr_t *tmpxvattr;
2362 uint_t mask = vap->va_mask;
2366 uint64_t new_uid, new_gid;
2368 uint64_t mtime[2], ctime[2];
2370 int need_policy = FALSE;
2372 zfs_fuid_info_t *fuidp = NULL;
2373 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2376 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2377 boolean_t fuid_dirtied = B_FALSE;
2378 sa_bulk_attr_t *bulk, *xattr_bulk;
2379 int count = 0, xattr_count = 0;
2390 * Make sure that if we have ephemeral uid/gid or xvattr specified
2391 * that file system is at proper version level
2394 if (zsb->z_use_fuids == B_FALSE &&
2395 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2396 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2397 (mask & ATTR_XVATTR))) {
2402 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2407 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2413 * If this is an xvattr_t, then get a pointer to the structure of
2414 * optional attributes. If this is NULL, then we have a vattr_t.
2416 xoap = xva_getxoptattr(xvap);
2418 tmpxvattr = kmem_alloc(sizeof(xvattr_t), KM_SLEEP);
2419 xva_init(tmpxvattr);
2421 bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2422 xattr_bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2425 * Immutable files can only alter immutable bit and atime
2427 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2428 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2429 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2434 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2440 * Verify timestamps doesn't overflow 32 bits.
2441 * ZFS can handle large timestamps, but 32bit syscalls can't
2442 * handle times greater than 2039. This check should be removed
2443 * once large timestamps are fully supported.
2445 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2446 if (((mask & ATTR_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2447 ((mask & ATTR_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2457 /* Can this be moved to before the top label? */
2458 if (zfs_is_readonly(zsb)) {
2464 * First validate permissions
2467 if (mask & ATTR_SIZE) {
2468 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2472 truncate_setsize(ip, vap->va_size);
2475 * XXX - Note, we are not providing any open
2476 * mode flags here (like FNDELAY), so we may
2477 * block if there are locks present... this
2478 * should be addressed in openat().
2480 /* XXX - would it be OK to generate a log record here? */
2481 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2486 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2487 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2488 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2489 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2490 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2491 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2492 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2493 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2494 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2498 if (mask & (ATTR_UID|ATTR_GID)) {
2499 int idmask = (mask & (ATTR_UID|ATTR_GID));
2504 * NOTE: even if a new mode is being set,
2505 * we may clear S_ISUID/S_ISGID bits.
2508 if (!(mask & ATTR_MODE))
2509 vap->va_mode = zp->z_mode;
2512 * Take ownership or chgrp to group we are a member of
2515 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2516 take_group = (mask & ATTR_GID) &&
2517 zfs_groupmember(zsb, vap->va_gid, cr);
2520 * If both ATTR_UID and ATTR_GID are set then take_owner and
2521 * take_group must both be set in order to allow taking
2524 * Otherwise, send the check through secpolicy_vnode_setattr()
2528 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2529 take_owner && take_group) ||
2530 ((idmask == ATTR_UID) && take_owner) ||
2531 ((idmask == ATTR_GID) && take_group)) {
2532 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2533 skipaclchk, cr) == 0) {
2535 * Remove setuid/setgid for non-privileged users
2537 (void) secpolicy_setid_clear(vap, cr);
2538 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2547 mutex_enter(&zp->z_lock);
2548 oldva.va_mode = zp->z_mode;
2549 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2550 if (mask & ATTR_XVATTR) {
2552 * Update xvattr mask to include only those attributes
2553 * that are actually changing.
2555 * the bits will be restored prior to actually setting
2556 * the attributes so the caller thinks they were set.
2558 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2559 if (xoap->xoa_appendonly !=
2560 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2563 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2564 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2568 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2569 if (xoap->xoa_nounlink !=
2570 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2573 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2574 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2578 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2579 if (xoap->xoa_immutable !=
2580 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2583 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2584 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2588 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2589 if (xoap->xoa_nodump !=
2590 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2593 XVA_CLR_REQ(xvap, XAT_NODUMP);
2594 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2598 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2599 if (xoap->xoa_av_modified !=
2600 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2603 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2604 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2608 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2609 if ((!S_ISREG(ip->i_mode) &&
2610 xoap->xoa_av_quarantined) ||
2611 xoap->xoa_av_quarantined !=
2612 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2615 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2616 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2620 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2621 mutex_exit(&zp->z_lock);
2626 if (need_policy == FALSE &&
2627 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2628 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2633 mutex_exit(&zp->z_lock);
2635 if (mask & ATTR_MODE) {
2636 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2637 err = secpolicy_setid_setsticky_clear(ip, vap,
2642 trim_mask |= ATTR_MODE;
2650 * If trim_mask is set then take ownership
2651 * has been granted or write_acl is present and user
2652 * has the ability to modify mode. In that case remove
2653 * UID|GID and or MODE from mask so that
2654 * secpolicy_vnode_setattr() doesn't revoke it.
2658 saved_mask = vap->va_mask;
2659 vap->va_mask &= ~trim_mask;
2661 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2662 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2667 vap->va_mask |= saved_mask;
2671 * secpolicy_vnode_setattr, or take ownership may have
2674 mask = vap->va_mask;
2676 if ((mask & (ATTR_UID | ATTR_GID))) {
2677 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2678 &xattr_obj, sizeof (xattr_obj));
2680 if (err == 0 && xattr_obj) {
2681 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2685 if (mask & ATTR_UID) {
2686 new_uid = zfs_fuid_create(zsb,
2687 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2688 if (new_uid != zp->z_uid &&
2689 zfs_fuid_overquota(zsb, B_FALSE, new_uid)) {
2697 if (mask & ATTR_GID) {
2698 new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2699 cr, ZFS_GROUP, &fuidp);
2700 if (new_gid != zp->z_gid &&
2701 zfs_fuid_overquota(zsb, B_TRUE, new_gid)) {
2709 tx = dmu_tx_create(zsb->z_os);
2711 if (mask & ATTR_MODE) {
2712 uint64_t pmode = zp->z_mode;
2714 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2716 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2718 mutex_enter(&zp->z_lock);
2719 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2721 * Are we upgrading ACL from old V0 format
2724 if (zsb->z_version >= ZPL_VERSION_FUID &&
2725 zfs_znode_acl_version(zp) ==
2726 ZFS_ACL_VERSION_INITIAL) {
2727 dmu_tx_hold_free(tx, acl_obj, 0,
2729 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2730 0, aclp->z_acl_bytes);
2732 dmu_tx_hold_write(tx, acl_obj, 0,
2735 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2736 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2737 0, aclp->z_acl_bytes);
2739 mutex_exit(&zp->z_lock);
2740 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2742 if ((mask & ATTR_XVATTR) &&
2743 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2744 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2746 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2750 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2753 fuid_dirtied = zsb->z_fuid_dirty;
2755 zfs_fuid_txhold(zsb, tx);
2757 zfs_sa_upgrade_txholds(tx, zp);
2759 err = dmu_tx_assign(tx, TXG_NOWAIT);
2761 if (err == ERESTART)
2768 * Set each attribute requested.
2769 * We group settings according to the locks they need to acquire.
2771 * Note: you cannot set ctime directly, although it will be
2772 * updated as a side-effect of calling this function.
2776 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2777 mutex_enter(&zp->z_acl_lock);
2778 mutex_enter(&zp->z_lock);
2780 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2781 &zp->z_pflags, sizeof (zp->z_pflags));
2784 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2785 mutex_enter(&attrzp->z_acl_lock);
2786 mutex_enter(&attrzp->z_lock);
2787 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2788 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2789 sizeof (attrzp->z_pflags));
2792 if (mask & (ATTR_UID|ATTR_GID)) {
2794 if (mask & ATTR_UID) {
2795 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2796 &new_uid, sizeof (new_uid));
2797 zp->z_uid = new_uid;
2799 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2800 SA_ZPL_UID(zsb), NULL, &new_uid,
2802 attrzp->z_uid = new_uid;
2806 if (mask & ATTR_GID) {
2807 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2808 NULL, &new_gid, sizeof (new_gid));
2809 zp->z_gid = new_gid;
2811 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2812 SA_ZPL_GID(zsb), NULL, &new_gid,
2814 attrzp->z_gid = new_gid;
2817 if (!(mask & ATTR_MODE)) {
2818 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2819 NULL, &new_mode, sizeof (new_mode));
2820 new_mode = zp->z_mode;
2822 err = zfs_acl_chown_setattr(zp);
2825 err = zfs_acl_chown_setattr(attrzp);
2830 if (mask & ATTR_MODE) {
2831 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
2832 &new_mode, sizeof (new_mode));
2833 zp->z_mode = new_mode;
2834 ASSERT3P(aclp, !=, NULL);
2835 err = zfs_aclset_common(zp, aclp, cr, tx);
2836 ASSERT3U(err, ==, 0);
2837 if (zp->z_acl_cached)
2838 zfs_acl_free(zp->z_acl_cached);
2839 zp->z_acl_cached = aclp;
2844 if (mask & ATTR_ATIME) {
2845 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2846 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
2847 &zp->z_atime, sizeof (zp->z_atime));
2850 if (mask & ATTR_MTIME) {
2851 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2852 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
2853 mtime, sizeof (mtime));
2856 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2857 if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) {
2858 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
2859 NULL, mtime, sizeof (mtime));
2860 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2861 &ctime, sizeof (ctime));
2862 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
2864 } else if (mask != 0) {
2865 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2866 &ctime, sizeof (ctime));
2867 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
2870 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2871 SA_ZPL_CTIME(zsb), NULL,
2872 &ctime, sizeof (ctime));
2873 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2874 mtime, ctime, B_TRUE);
2878 * Do this after setting timestamps to prevent timestamp
2879 * update from toggling bit
2882 if (xoap && (mask & ATTR_XVATTR)) {
2885 * restore trimmed off masks
2886 * so that return masks can be set for caller.
2889 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
2890 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2892 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
2893 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2895 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
2896 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2898 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
2899 XVA_SET_REQ(xvap, XAT_NODUMP);
2901 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
2902 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2904 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
2905 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2908 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2909 ASSERT(S_ISREG(ip->i_mode));
2911 zfs_xvattr_set(zp, xvap, tx);
2915 zfs_fuid_sync(zsb, tx);
2918 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2920 mutex_exit(&zp->z_lock);
2921 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2922 mutex_exit(&zp->z_acl_lock);
2925 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2926 mutex_exit(&attrzp->z_acl_lock);
2927 mutex_exit(&attrzp->z_lock);
2930 if (err == 0 && attrzp) {
2931 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2942 zfs_fuid_info_free(fuidp);
2948 if (err == ERESTART)
2951 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2953 zfs_inode_update(zp);
2957 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
2958 zil_commit(zilog, 0);
2961 kmem_free(xattr_bulk, sizeof(sa_bulk_attr_t) * 7);
2962 kmem_free(bulk, sizeof(sa_bulk_attr_t) * 7);
2963 kmem_free(tmpxvattr, sizeof(xvattr_t));
2967 EXPORT_SYMBOL(zfs_setattr);
2969 typedef struct zfs_zlock {
2970 krwlock_t *zl_rwlock; /* lock we acquired */
2971 znode_t *zl_znode; /* znode we held */
2972 struct zfs_zlock *zl_next; /* next in list */
2976 * Drop locks and release vnodes that were held by zfs_rename_lock().
2979 zfs_rename_unlock(zfs_zlock_t **zlpp)
2983 while ((zl = *zlpp) != NULL) {
2984 if (zl->zl_znode != NULL)
2985 iput(ZTOI(zl->zl_znode));
2986 rw_exit(zl->zl_rwlock);
2987 *zlpp = zl->zl_next;
2988 kmem_free(zl, sizeof (*zl));
2993 * Search back through the directory tree, using the ".." entries.
2994 * Lock each directory in the chain to prevent concurrent renames.
2995 * Fail any attempt to move a directory into one of its own descendants.
2996 * XXX - z_parent_lock can overlap with map or grow locks
2999 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3003 uint64_t rootid = ZTOZSB(zp)->z_root;
3004 uint64_t oidp = zp->z_id;
3005 krwlock_t *rwlp = &szp->z_parent_lock;
3006 krw_t rw = RW_WRITER;
3009 * First pass write-locks szp and compares to zp->z_id.
3010 * Later passes read-lock zp and compare to zp->z_parent.
3013 if (!rw_tryenter(rwlp, rw)) {
3015 * Another thread is renaming in this path.
3016 * Note that if we are a WRITER, we don't have any
3017 * parent_locks held yet.
3019 if (rw == RW_READER && zp->z_id > szp->z_id) {
3021 * Drop our locks and restart
3023 zfs_rename_unlock(&zl);
3027 rwlp = &szp->z_parent_lock;
3032 * Wait for other thread to drop its locks
3038 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3039 zl->zl_rwlock = rwlp;
3040 zl->zl_znode = NULL;
3041 zl->zl_next = *zlpp;
3044 if (oidp == szp->z_id) /* We're a descendant of szp */
3047 if (oidp == rootid) /* We've hit the top */
3050 if (rw == RW_READER) { /* i.e. not the first pass */
3051 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3056 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3057 &oidp, sizeof (oidp));
3058 rwlp = &zp->z_parent_lock;
3061 } while (zp->z_id != sdzp->z_id);
3067 * Move an entry from the provided source directory to the target
3068 * directory. Change the entry name as indicated.
3070 * IN: sdip - Source directory containing the "old entry".
3071 * snm - Old entry name.
3072 * tdip - Target directory to contain the "new entry".
3073 * tnm - New entry name.
3074 * cr - credentials of caller.
3075 * flags - case flags
3077 * RETURN: 0 if success
3078 * error code if failure
3081 * sdip,tdip - ctime|mtime updated
3085 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3086 cred_t *cr, int flags)
3088 znode_t *tdzp, *szp, *tzp;
3089 znode_t *sdzp = ITOZ(sdip);
3090 zfs_sb_t *zsb = ITOZSB(sdip);
3092 zfs_dirlock_t *sdl, *tdl;
3095 int cmp, serr, terr;
3100 ZFS_VERIFY_ZP(sdzp);
3103 if (tdip->i_sb != sdip->i_sb) {
3109 ZFS_VERIFY_ZP(tdzp);
3110 if (zsb->z_utf8 && u8_validate(tnm,
3111 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3116 if (flags & FIGNORECASE)
3125 * This is to prevent the creation of links into attribute space
3126 * by renaming a linked file into/outof an attribute directory.
3127 * See the comment in zfs_link() for why this is considered bad.
3129 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3135 * Lock source and target directory entries. To prevent deadlock,
3136 * a lock ordering must be defined. We lock the directory with
3137 * the smallest object id first, or if it's a tie, the one with
3138 * the lexically first name.
3140 if (sdzp->z_id < tdzp->z_id) {
3142 } else if (sdzp->z_id > tdzp->z_id) {
3146 * First compare the two name arguments without
3147 * considering any case folding.
3149 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3151 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3152 ASSERT(error == 0 || !zsb->z_utf8);
3155 * POSIX: "If the old argument and the new argument
3156 * both refer to links to the same existing file,
3157 * the rename() function shall return successfully
3158 * and perform no other action."
3164 * If the file system is case-folding, then we may
3165 * have some more checking to do. A case-folding file
3166 * system is either supporting mixed case sensitivity
3167 * access or is completely case-insensitive. Note
3168 * that the file system is always case preserving.
3170 * In mixed sensitivity mode case sensitive behavior
3171 * is the default. FIGNORECASE must be used to
3172 * explicitly request case insensitive behavior.
3174 * If the source and target names provided differ only
3175 * by case (e.g., a request to rename 'tim' to 'Tim'),
3176 * we will treat this as a special case in the
3177 * case-insensitive mode: as long as the source name
3178 * is an exact match, we will allow this to proceed as
3179 * a name-change request.
3181 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3182 (zsb->z_case == ZFS_CASE_MIXED &&
3183 flags & FIGNORECASE)) &&
3184 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3187 * case preserving rename request, require exact
3196 * If the source and destination directories are the same, we should
3197 * grab the z_name_lock of that directory only once.
3201 rw_enter(&sdzp->z_name_lock, RW_READER);
3205 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3206 ZEXISTS | zflg, NULL, NULL);
3207 terr = zfs_dirent_lock(&tdl,
3208 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3210 terr = zfs_dirent_lock(&tdl,
3211 tdzp, tnm, &tzp, zflg, NULL, NULL);
3212 serr = zfs_dirent_lock(&sdl,
3213 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3219 * Source entry invalid or not there.
3222 zfs_dirent_unlock(tdl);
3228 rw_exit(&sdzp->z_name_lock);
3230 if (strcmp(snm, "..") == 0)
3236 zfs_dirent_unlock(sdl);
3240 rw_exit(&sdzp->z_name_lock);
3242 if (strcmp(tnm, "..") == 0)
3249 * Must have write access at the source to remove the old entry
3250 * and write access at the target to create the new entry.
3251 * Note that if target and source are the same, this can be
3252 * done in a single check.
3255 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3258 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3260 * Check to make sure rename is valid.
3261 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3263 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3268 * Does target exist?
3272 * Source and target must be the same type.
3274 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3275 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3280 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3286 * POSIX dictates that when the source and target
3287 * entries refer to the same file object, rename
3288 * must do nothing and exit without error.
3290 if (szp->z_id == tzp->z_id) {
3296 tx = dmu_tx_create(zsb->z_os);
3297 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3298 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3299 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3300 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3302 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3303 zfs_sa_upgrade_txholds(tx, tdzp);
3306 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3307 zfs_sa_upgrade_txholds(tx, tzp);
3310 zfs_sa_upgrade_txholds(tx, szp);
3311 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3312 error = dmu_tx_assign(tx, TXG_NOWAIT);
3315 zfs_rename_unlock(&zl);
3316 zfs_dirent_unlock(sdl);
3317 zfs_dirent_unlock(tdl);
3320 rw_exit(&sdzp->z_name_lock);
3325 if (error == ERESTART) {
3335 if (tzp) /* Attempt to remove the existing target */
3336 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3339 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3341 szp->z_pflags |= ZFS_AV_MODIFIED;
3343 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3344 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3345 ASSERT3U(error, ==, 0);
3347 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3349 zfs_log_rename(zilog, tx, TX_RENAME |
3350 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3351 sdl->dl_name, tdzp, tdl->dl_name, szp);
3354 * At this point, we have successfully created
3355 * the target name, but have failed to remove
3356 * the source name. Since the create was done
3357 * with the ZRENAMING flag, there are
3358 * complications; for one, the link count is
3359 * wrong. The easiest way to deal with this
3360 * is to remove the newly created target, and
3361 * return the original error. This must
3362 * succeed; fortunately, it is very unlikely to
3363 * fail, since we just created it.
3365 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3366 ZRENAMING, NULL), ==, 0);
3374 zfs_rename_unlock(&zl);
3376 zfs_dirent_unlock(sdl);
3377 zfs_dirent_unlock(tdl);
3379 zfs_inode_update(sdzp);
3381 rw_exit(&sdzp->z_name_lock);
3384 zfs_inode_update(tdzp);
3386 zfs_inode_update(szp);
3389 zfs_inode_update(tzp);
3393 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3394 zil_commit(zilog, 0);
3399 EXPORT_SYMBOL(zfs_rename);
3402 * Insert the indicated symbolic reference entry into the directory.
3404 * IN: dip - Directory to contain new symbolic link.
3405 * link - Name for new symlink entry.
3406 * vap - Attributes of new entry.
3407 * target - Target path of new symlink.
3409 * cr - credentials of caller.
3410 * flags - case flags
3412 * RETURN: 0 if success
3413 * error code if failure
3416 * dip - ctime|mtime updated
3420 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3421 struct inode **ipp, cred_t *cr, int flags)
3423 znode_t *zp, *dzp = ITOZ(dip);
3426 zfs_sb_t *zsb = ITOZSB(dip);
3428 uint64_t len = strlen(link);
3431 zfs_acl_ids_t acl_ids;
3432 boolean_t fuid_dirtied;
3433 uint64_t txtype = TX_SYMLINK;
3435 ASSERT(S_ISLNK(vap->va_mode));
3441 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3442 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3446 if (flags & FIGNORECASE)
3449 if (len > MAXPATHLEN) {
3451 return (ENAMETOOLONG);
3454 if ((error = zfs_acl_ids_create(dzp, 0,
3455 vap, cr, NULL, &acl_ids)) != 0) {
3463 * Attempt to lock directory; fail if entry already exists.
3465 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3467 zfs_acl_ids_free(&acl_ids);
3472 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3473 zfs_acl_ids_free(&acl_ids);
3474 zfs_dirent_unlock(dl);
3479 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3480 zfs_acl_ids_free(&acl_ids);
3481 zfs_dirent_unlock(dl);
3485 tx = dmu_tx_create(zsb->z_os);
3486 fuid_dirtied = zsb->z_fuid_dirty;
3487 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3488 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3489 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3490 ZFS_SA_BASE_ATTR_SIZE + len);
3491 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3492 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3493 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3494 acl_ids.z_aclp->z_acl_bytes);
3497 zfs_fuid_txhold(zsb, tx);
3498 error = dmu_tx_assign(tx, TXG_NOWAIT);
3500 zfs_dirent_unlock(dl);
3501 if (error == ERESTART) {
3506 zfs_acl_ids_free(&acl_ids);
3513 * Create a new object for the symlink.
3514 * for version 4 ZPL datsets the symlink will be an SA attribute
3516 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3519 zfs_fuid_sync(zsb, tx);
3521 mutex_enter(&zp->z_lock);
3523 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3526 zfs_sa_symlink(zp, link, len, tx);
3527 mutex_exit(&zp->z_lock);
3530 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3531 &zp->z_size, sizeof (zp->z_size), tx);
3533 * Insert the new object into the directory.
3535 (void) zfs_link_create(dl, zp, tx, ZNEW);
3537 if (flags & FIGNORECASE)
3539 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3541 zfs_inode_update(dzp);
3542 zfs_inode_update(zp);
3544 zfs_acl_ids_free(&acl_ids);
3548 zfs_dirent_unlock(dl);
3552 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3553 zil_commit(zilog, 0);
3558 EXPORT_SYMBOL(zfs_symlink);
3561 * Return, in the buffer contained in the provided uio structure,
3562 * the symbolic path referred to by ip.
3564 * IN: ip - inode of symbolic link
3565 * uio - structure to contain the link path.
3566 * cr - credentials of caller.
3568 * RETURN: 0 if success
3569 * error code if failure
3572 * ip - atime updated
3576 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3578 znode_t *zp = ITOZ(ip);
3579 zfs_sb_t *zsb = ITOZSB(ip);
3585 mutex_enter(&zp->z_lock);
3587 error = sa_lookup_uio(zp->z_sa_hdl,
3588 SA_ZPL_SYMLINK(zsb), uio);
3590 error = zfs_sa_readlink(zp, uio);
3591 mutex_exit(&zp->z_lock);
3593 ZFS_ACCESSTIME_STAMP(zsb, zp);
3594 zfs_inode_update(zp);
3598 EXPORT_SYMBOL(zfs_readlink);
3601 * Insert a new entry into directory tdip referencing sip.
3603 * IN: tdip - Directory to contain new entry.
3604 * sip - inode of new entry.
3605 * name - name of new entry.
3606 * cr - credentials of caller.
3608 * RETURN: 0 if success
3609 * error code if failure
3612 * tdip - ctime|mtime updated
3613 * sip - ctime updated
3617 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr)
3619 znode_t *dzp = ITOZ(tdip);
3621 zfs_sb_t *zsb = ITOZSB(tdip);
3630 ASSERT(S_ISDIR(tdip->i_mode));
3637 * POSIX dictates that we return EPERM here.
3638 * Better choices include ENOTSUP or EISDIR.
3640 if (S_ISDIR(sip->i_mode)) {
3645 if (sip->i_sb != tdip->i_sb) {
3653 /* Prevent links to .zfs/shares files */
3655 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3656 &parent, sizeof (uint64_t))) != 0) {
3660 if (parent == zsb->z_shares_dir) {
3665 if (zsb->z_utf8 && u8_validate(name,
3666 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3670 #ifdef HAVE_PN_UTILS
3671 if (flags & FIGNORECASE)
3673 #endif /* HAVE_PN_UTILS */
3676 * We do not support links between attributes and non-attributes
3677 * because of the potential security risk of creating links
3678 * into "normal" file space in order to circumvent restrictions
3679 * imposed in attribute space.
3681 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3686 owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER);
3687 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3692 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3699 * Attempt to lock directory; fail if entry already exists.
3701 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3707 tx = dmu_tx_create(zsb->z_os);
3708 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3709 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3710 zfs_sa_upgrade_txholds(tx, szp);
3711 zfs_sa_upgrade_txholds(tx, dzp);
3712 error = dmu_tx_assign(tx, TXG_NOWAIT);
3714 zfs_dirent_unlock(dl);
3715 if (error == ERESTART) {
3725 error = zfs_link_create(dl, szp, tx, 0);
3728 uint64_t txtype = TX_LINK;
3729 #ifdef HAVE_PN_UTILS
3730 if (flags & FIGNORECASE)
3732 #endif /* HAVE_PN_UTILS */
3733 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3738 zfs_dirent_unlock(dl);
3740 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3741 zil_commit(zilog, 0);
3743 zfs_inode_update(dzp);
3744 zfs_inode_update(szp);
3748 EXPORT_SYMBOL(zfs_link);
3751 zfs_putpage_commit_cb(void *arg, int error)
3753 struct page *pp = arg;
3756 __set_page_dirty_nobuffers(pp);
3758 if (error != ECANCELED)
3764 end_page_writeback(pp);
3768 * Push a page out to disk, once the page is on stable storage the
3769 * registered commit callback will be run as notification of completion.
3771 * IN: ip - page mapped for inode.
3772 * pp - page to push (page is locked)
3773 * wbc - writeback control data
3775 * RETURN: 0 if success
3776 * error code if failure
3779 * ip - ctime|mtime updated
3783 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
3785 znode_t *zp = ITOZ(ip);
3786 zfs_sb_t *zsb = ITOZSB(ip);
3794 uint64_t mtime[2], ctime[2];
3795 sa_bulk_attr_t bulk[3];
3802 ASSERT(PageLocked(pp));
3804 pgoff = page_offset(pp); /* Page byte-offset in file */
3805 offset = i_size_read(ip); /* File length in bytes */
3806 pglen = MIN(PAGE_CACHE_SIZE, /* Page length in bytes */
3807 P2ROUNDUP(offset, PAGE_CACHE_SIZE)-pgoff);
3809 /* Page is beyond end of file */
3810 if (pgoff >= offset) {
3816 /* Truncate page length to end of file */
3817 if (pgoff + pglen > offset)
3818 pglen = offset - pgoff;
3822 * FIXME: Allow mmap writes past its quota. The correct fix
3823 * is to register a page_mkwrite() handler to count the page
3824 * against its quota when it is about to be dirtied.
3826 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
3827 zfs_owner_overquota(zsb, zp, B_TRUE)) {
3832 set_page_writeback(pp);
3835 rl = zfs_range_lock(zp, pgoff, pglen, RL_WRITER);
3836 tx = dmu_tx_create(zsb->z_os);
3838 sync = ((zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) ||
3839 (wbc->sync_mode == WB_SYNC_ALL));
3841 dmu_tx_callback_register(tx, zfs_putpage_commit_cb, pp);
3843 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
3845 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3846 zfs_sa_upgrade_txholds(tx, zp);
3847 err = dmu_tx_assign(tx, TXG_NOWAIT);
3849 if (err == ERESTART)
3852 /* Will call all registered commit callbacks */
3856 * For the synchronous case the commit callback must be
3857 * explicitly called because there is no registered callback.
3860 zfs_putpage_commit_cb(pp, ECANCELED);
3862 zfs_range_unlock(rl);
3868 ASSERT3U(pglen, <=, PAGE_CACHE_SIZE);
3869 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
3872 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3873 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3874 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
3876 /* Preserve the mtime and ctime provided by the inode */
3877 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
3878 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
3879 zp->z_atime_dirty = 0;
3882 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3884 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0);
3887 zfs_range_unlock(rl);
3890 zil_commit(zsb->z_log, zp->z_id);
3891 zfs_putpage_commit_cb(pp, err);
3899 * Update the system attributes when the inode has been dirtied. For the
3900 * moment we're conservative and only update the atime, mtime, and ctime.
3903 zfs_dirty_inode(struct inode *ip, int flags)
3905 znode_t *zp = ITOZ(ip);
3906 zfs_sb_t *zsb = ITOZSB(ip);
3908 uint64_t atime[2], mtime[2], ctime[2];
3909 sa_bulk_attr_t bulk[3];
3916 tx = dmu_tx_create(zsb->z_os);
3918 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3919 zfs_sa_upgrade_txholds(tx, zp);
3921 error = dmu_tx_assign(tx, TXG_WAIT);
3927 mutex_enter(&zp->z_lock);
3928 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
3929 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3930 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3932 /* Preserve the mtime and ctime provided by the inode */
3933 ZFS_TIME_ENCODE(&ip->i_atime, atime);
3934 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
3935 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
3936 zp->z_atime_dirty = 0;
3938 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3939 mutex_exit(&zp->z_lock);
3946 EXPORT_SYMBOL(zfs_dirty_inode);
3950 zfs_inactive(struct inode *ip)
3952 znode_t *zp = ITOZ(ip);
3953 zfs_sb_t *zsb = ITOZSB(ip);
3956 if (zfsctl_is_node(ip)) {
3957 zfsctl_inode_inactive(ip);
3961 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
3962 if (zp->z_sa_hdl == NULL) {
3963 rw_exit(&zsb->z_teardown_inactive_lock);
3967 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3968 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
3970 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3971 zfs_sa_upgrade_txholds(tx, zp);
3972 error = dmu_tx_assign(tx, TXG_WAIT);
3976 mutex_enter(&zp->z_lock);
3977 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
3978 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3979 zp->z_atime_dirty = 0;
3980 mutex_exit(&zp->z_lock);
3986 rw_exit(&zsb->z_teardown_inactive_lock);
3988 EXPORT_SYMBOL(zfs_inactive);
3991 * Bounds-check the seek operation.
3993 * IN: ip - inode seeking within
3994 * ooff - old file offset
3995 * noffp - pointer to new file offset
3996 * ct - caller context
3998 * RETURN: 0 if success
3999 * EINVAL if new offset invalid
4003 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4005 if (S_ISDIR(ip->i_mode))
4007 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4009 EXPORT_SYMBOL(zfs_seek);
4012 * Fill pages with data from the disk.
4015 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4017 znode_t *zp = ITOZ(ip);
4018 zfs_sb_t *zsb = ITOZSB(ip);
4020 struct page *cur_pp;
4021 u_offset_t io_off, total;
4028 io_len = nr_pages << PAGE_CACHE_SHIFT;
4029 i_size = i_size_read(ip);
4030 io_off = page_offset(pl[0]);
4032 if (io_off + io_len > i_size)
4033 io_len = i_size - io_off;
4036 * Iterate over list of pages and read each page individually.
4040 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4044 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4048 /* convert checksum errors into IO errors */
4053 cur_pp = pl[++page_idx];
4060 * Uses zfs_fillpage to read data from the file and fill the pages.
4062 * IN: ip - inode of file to get data from.
4063 * pl - list of pages to read
4064 * nr_pages - number of pages to read
4066 * RETURN: 0 if success
4067 * error code if failure
4070 * vp - atime updated
4074 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4076 znode_t *zp = ITOZ(ip);
4077 zfs_sb_t *zsb = ITOZSB(ip);
4086 err = zfs_fillpage(ip, pl, nr_pages);
4089 ZFS_ACCESSTIME_STAMP(zsb, zp);
4094 EXPORT_SYMBOL(zfs_getpage);
4097 * Check ZFS specific permissions to memory map a section of a file.
4099 * IN: ip - inode of the file to mmap
4101 * addrp - start address in memory region
4102 * len - length of memory region
4103 * vm_flags- address flags
4105 * RETURN: 0 if success
4106 * error code if failure
4110 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4111 unsigned long vm_flags)
4113 znode_t *zp = ITOZ(ip);
4114 zfs_sb_t *zsb = ITOZSB(ip);
4119 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4120 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4125 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4126 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4131 if (off < 0 || len > MAXOFFSET_T - off) {
4139 EXPORT_SYMBOL(zfs_map);
4142 * convoff - converts the given data (start, whence) to the
4146 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4151 if ((lckdat->l_whence == 2) || (whence == 2)) {
4152 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4156 switch (lckdat->l_whence) {
4158 lckdat->l_start += offset;
4161 lckdat->l_start += vap.va_size;
4169 if (lckdat->l_start < 0)
4174 lckdat->l_start -= offset;
4177 lckdat->l_start -= vap.va_size;
4185 lckdat->l_whence = (short)whence;
4190 * Free or allocate space in a file. Currently, this function only
4191 * supports the `F_FREESP' command. However, this command is somewhat
4192 * misnamed, as its functionality includes the ability to allocate as
4193 * well as free space.
4195 * IN: ip - inode of file to free data in.
4196 * cmd - action to take (only F_FREESP supported).
4197 * bfp - section of file to free/alloc.
4198 * flag - current file open mode flags.
4199 * offset - current file offset.
4200 * cr - credentials of caller [UNUSED].
4202 * RETURN: 0 if success
4203 * error code if failure
4206 * ip - ctime|mtime updated
4210 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4211 offset_t offset, cred_t *cr)
4213 znode_t *zp = ITOZ(ip);
4214 zfs_sb_t *zsb = ITOZSB(ip);
4221 if (cmd != F_FREESP) {
4226 if ((error = convoff(ip, bfp, 0, offset))) {
4231 if (bfp->l_len < 0) {
4237 * Permissions aren't checked on Solaris because on this OS
4238 * zfs_space() can only be called with an opened file handle.
4239 * On Linux we can get here through truncate_range() which
4240 * operates directly on inodes, so we need to check access rights.
4242 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4248 len = bfp->l_len; /* 0 means from off to end of file */
4250 error = zfs_freesp(zp, off, len, flag, TRUE);
4255 EXPORT_SYMBOL(zfs_space);
4259 zfs_fid(struct inode *ip, fid_t *fidp)
4261 znode_t *zp = ITOZ(ip);
4262 zfs_sb_t *zsb = ITOZSB(ip);
4265 uint64_t object = zp->z_id;
4272 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4273 &gen64, sizeof (uint64_t))) != 0) {
4278 gen = (uint32_t)gen64;
4280 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4281 if (fidp->fid_len < size) {
4282 fidp->fid_len = size;
4287 zfid = (zfid_short_t *)fidp;
4289 zfid->zf_len = size;
4291 for (i = 0; i < sizeof (zfid->zf_object); i++)
4292 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4294 /* Must have a non-zero generation number to distinguish from .zfs */
4297 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4298 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4300 if (size == LONG_FID_LEN) {
4301 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4304 zlfid = (zfid_long_t *)fidp;
4306 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4307 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4309 /* XXX - this should be the generation number for the objset */
4310 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4311 zlfid->zf_setgen[i] = 0;
4317 EXPORT_SYMBOL(zfs_fid);
4321 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4323 znode_t *zp = ITOZ(ip);
4324 zfs_sb_t *zsb = ITOZSB(ip);
4326 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4330 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4335 EXPORT_SYMBOL(zfs_getsecattr);
4339 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4341 znode_t *zp = ITOZ(ip);
4342 zfs_sb_t *zsb = ITOZSB(ip);
4344 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4345 zilog_t *zilog = zsb->z_log;
4350 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4352 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4353 zil_commit(zilog, 0);
4358 EXPORT_SYMBOL(zfs_setsecattr);
4360 #ifdef HAVE_UIO_ZEROCOPY
4362 * Tunable, both must be a power of 2.
4364 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4365 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4366 * an arcbuf for a partial block read
4368 int zcr_blksz_min = (1 << 10); /* 1K */
4369 int zcr_blksz_max = (1 << 17); /* 128K */
4373 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4375 znode_t *zp = ITOZ(ip);
4376 zfs_sb_t *zsb = ITOZSB(ip);
4377 int max_blksz = zsb->z_max_blksz;
4378 uio_t *uio = &xuio->xu_uio;
4379 ssize_t size = uio->uio_resid;
4380 offset_t offset = uio->uio_loffset;
4385 int preamble, postamble;
4387 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4395 * Loan out an arc_buf for write if write size is bigger than
4396 * max_blksz, and the file's block size is also max_blksz.
4399 if (size < blksz || zp->z_blksz != blksz) {
4404 * Caller requests buffers for write before knowing where the
4405 * write offset might be (e.g. NFS TCP write).
4410 preamble = P2PHASE(offset, blksz);
4412 preamble = blksz - preamble;
4417 postamble = P2PHASE(size, blksz);
4420 fullblk = size / blksz;
4421 (void) dmu_xuio_init(xuio,
4422 (preamble != 0) + fullblk + (postamble != 0));
4425 * Have to fix iov base/len for partial buffers. They
4426 * currently represent full arc_buf's.
4429 /* data begins in the middle of the arc_buf */
4430 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4433 (void) dmu_xuio_add(xuio, abuf,
4434 blksz - preamble, preamble);
4437 for (i = 0; i < fullblk; i++) {
4438 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4441 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4445 /* data ends in the middle of the arc_buf */
4446 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4449 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4454 * Loan out an arc_buf for read if the read size is larger than
4455 * the current file block size. Block alignment is not
4456 * considered. Partial arc_buf will be loaned out for read.
4458 blksz = zp->z_blksz;
4459 if (blksz < zcr_blksz_min)
4460 blksz = zcr_blksz_min;
4461 if (blksz > zcr_blksz_max)
4462 blksz = zcr_blksz_max;
4463 /* avoid potential complexity of dealing with it */
4464 if (blksz > max_blksz) {
4469 maxsize = zp->z_size - uio->uio_loffset;
4483 uio->uio_extflg = UIO_XUIO;
4484 XUIO_XUZC_RW(xuio) = ioflag;
4491 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4495 int ioflag = XUIO_XUZC_RW(xuio);
4497 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4499 i = dmu_xuio_cnt(xuio);
4501 abuf = dmu_xuio_arcbuf(xuio, i);
4503 * if abuf == NULL, it must be a write buffer
4504 * that has been returned in zfs_write().
4507 dmu_return_arcbuf(abuf);
4508 ASSERT(abuf || ioflag == UIO_WRITE);
4511 dmu_xuio_fini(xuio);
4514 #endif /* HAVE_UIO_ZEROCOPY */
4516 #if defined(_KERNEL) && defined(HAVE_SPL)
4517 module_param(zfs_read_chunk_size, long, 0644);
4518 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");