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.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
27 /* Portions Copyright 2010 Robert Milkowski */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
37 #include <sys/vfs_opreg.h>
41 #include <sys/taskq.h>
43 #include <sys/vmsystm.h>
44 #include <sys/atomic.h>
46 #include <sys/pathname.h>
47 #include <sys/cmn_err.h>
48 #include <sys/errno.h>
49 #include <sys/unistd.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/fs/zfs.h>
55 #include <sys/dmu_objset.h>
61 #include <sys/dirent.h>
62 #include <sys/policy.h>
63 #include <sys/sunddi.h>
66 #include "fs/fs_subr.h"
67 #include <sys/zfs_ctldir.h>
68 #include <sys/zfs_fuid.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_vnops.h>
72 #include <sys/zfs_rlock.h>
73 #include <sys/extdirent.h>
74 #include <sys/kidmap.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zsb).
92 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) iput() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
104 * If you must call iput() within a tx then use iput_ASYNC().
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
109 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
110 * This is critical because we don't want to block while holding locks.
111 * Note, in particular, that if a lock is sometimes acquired before
112 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
113 * use a non-blocking assign can deadlock the system. The scenario:
115 * Thread A has grabbed a lock before calling dmu_tx_assign().
116 * Thread B is in an already-assigned tx, and blocks for this lock.
117 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
118 * forever, because the previous txg can't quiesce until B's tx commits.
120 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
121 * then drop all locks, call dmu_tx_wait(), and try again.
123 * (5) If the operation succeeded, generate the intent log entry for it
124 * before dropping locks. This ensures that the ordering of events
125 * in the intent log matches the order in which they actually occurred.
126 * During ZIL replay the zfs_log_* functions will update the sequence
127 * number to indicate the zil transaction has replayed.
129 * (6) At the end of each vnode op, the DMU tx must always commit,
130 * regardless of whether there were any errors.
132 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
133 * to ensure that synchronous semantics are provided when necessary.
135 * In general, this is how things should be ordered in each vnode op:
137 * ZFS_ENTER(zsb); // exit if unmounted
139 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
140 * rw_enter(...); // grab any other locks you need
141 * tx = dmu_tx_create(...); // get DMU tx
142 * dmu_tx_hold_*(); // hold each object you might modify
143 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
145 * rw_exit(...); // drop locks
146 * zfs_dirent_unlock(dl); // unlock directory entry
147 * iput(...); // release held vnodes
148 * if (error == ERESTART) {
153 * dmu_tx_abort(tx); // abort DMU tx
154 * ZFS_EXIT(zsb); // finished in zfs
155 * return (error); // really out of space
157 * error = do_real_work(); // do whatever this VOP does
159 * zfs_log_*(...); // on success, make ZIL entry
160 * dmu_tx_commit(tx); // commit DMU tx -- error or not
161 * rw_exit(...); // drop locks
162 * zfs_dirent_unlock(dl); // unlock directory entry
163 * iput(...); // release held vnodes
164 * zil_commit(zilog, foid); // synchronous when necessary
165 * ZFS_EXIT(zsb); // finished in zfs
166 * return (error); // done, report error
170 * Virus scanning is unsupported. It would be possible to add a hook
171 * here to performance the required virus scan. This could be done
172 * entirely in the kernel or potentially as an update to invoke a
176 zfs_vscan(struct inode *ip, cred_t *cr, int async)
183 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
185 znode_t *zp = ITOZ(ip);
186 zfs_sb_t *zsb = ITOZSB(ip);
191 /* Honor ZFS_APPENDONLY file attribute */
192 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
193 ((flag & O_APPEND) == 0)) {
198 /* Virus scan eligible files on open */
199 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
200 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
201 if (zfs_vscan(ip, cr, 0) != 0) {
207 /* Keep a count of the synchronous opens in the znode */
209 atomic_inc_32(&zp->z_sync_cnt);
214 EXPORT_SYMBOL(zfs_open);
218 zfs_close(struct inode *ip, int flag, cred_t *cr)
220 znode_t *zp = ITOZ(ip);
221 zfs_sb_t *zsb = ITOZSB(ip);
227 * Zero the synchronous opens in the znode. Under Linux the
228 * zfs_close() hook is not symmetric with zfs_open(), it is
229 * only called once when the last reference is dropped.
234 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
235 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
236 VERIFY(zfs_vscan(ip, cr, 1) == 0);
241 EXPORT_SYMBOL(zfs_close);
245 * When a file is memory mapped, we must keep the IO data synchronized
246 * between the DMU cache and the memory mapped pages. What this means:
248 * On Write: If we find a memory mapped page, we write to *both*
249 * the page and the dmu buffer.
252 update_pages(struct inode *ip, int64_t start, int len,
253 objset_t *os, uint64_t oid)
255 struct address_space *mp = ip->i_mapping;
261 off = start & (PAGE_CACHE_SIZE-1);
262 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
263 nbytes = MIN(PAGE_CACHE_SIZE - off, len);
265 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
267 if (mapping_writably_mapped(mp))
268 flush_dcache_page(pp);
271 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
275 if (mapping_writably_mapped(mp))
276 flush_dcache_page(pp);
278 mark_page_accessed(pp);
282 page_cache_release(pp);
291 * When a file is memory mapped, we must keep the IO data synchronized
292 * between the DMU cache and the memory mapped pages. What this means:
294 * On Read: We "read" preferentially from memory mapped pages,
295 * else we default from the dmu buffer.
297 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
298 * the file is memory mapped.
301 mappedread(struct inode *ip, int nbytes, uio_t *uio)
303 struct address_space *mp = ip->i_mapping;
305 znode_t *zp = ITOZ(ip);
306 objset_t *os = ITOZSB(ip)->z_os;
313 start = uio->uio_loffset;
314 off = start & (PAGE_CACHE_SIZE-1);
315 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
316 bytes = MIN(PAGE_CACHE_SIZE - off, len);
318 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
320 ASSERT(PageUptodate(pp));
323 error = uiomove(pb + off, bytes, UIO_READ, uio);
326 if (mapping_writably_mapped(mp))
327 flush_dcache_page(pp);
329 mark_page_accessed(pp);
331 page_cache_release(pp);
333 error = dmu_read_uio(os, zp->z_id, uio, bytes);
345 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
348 * Read bytes from specified file into supplied buffer.
350 * IN: ip - inode of file to be read from.
351 * uio - structure supplying read location, range info,
353 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
354 * O_DIRECT flag; used to bypass page cache.
355 * cr - credentials of caller.
357 * OUT: uio - updated offset and range, buffer filled.
359 * RETURN: 0 if success
360 * error code if failure
363 * inode - atime updated if byte count > 0
367 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
369 znode_t *zp = ITOZ(ip);
370 zfs_sb_t *zsb = ITOZSB(ip);
375 #ifdef HAVE_UIO_ZEROCOPY
377 #endif /* HAVE_UIO_ZEROCOPY */
383 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
389 * Validate file offset
391 if (uio->uio_loffset < (offset_t)0) {
397 * Fasttrack empty reads
399 if (uio->uio_resid == 0) {
405 * Check for mandatory locks
407 if (mandatory_lock(ip) &&
408 !lock_may_read(ip, uio->uio_loffset, uio->uio_resid)) {
414 * If we're in FRSYNC mode, sync out this znode before reading it.
416 if (ioflag & FRSYNC || zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
417 zil_commit(zsb->z_log, zp->z_id);
420 * Lock the range against changes.
422 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
425 * If we are reading past end-of-file we can skip
426 * to the end; but we might still need to set atime.
428 if (uio->uio_loffset >= zp->z_size) {
433 ASSERT(uio->uio_loffset < zp->z_size);
434 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
436 #ifdef HAVE_UIO_ZEROCOPY
437 if ((uio->uio_extflg == UIO_XUIO) &&
438 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
440 int blksz = zp->z_blksz;
441 uint64_t offset = uio->uio_loffset;
443 xuio = (xuio_t *)uio;
445 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
448 ASSERT(offset + n <= blksz);
451 (void) dmu_xuio_init(xuio, nblk);
453 if (vn_has_cached_data(ip)) {
455 * For simplicity, we always allocate a full buffer
456 * even if we only expect to read a portion of a block.
458 while (--nblk >= 0) {
459 (void) dmu_xuio_add(xuio,
460 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
465 #endif /* HAVE_UIO_ZEROCOPY */
468 nbytes = MIN(n, zfs_read_chunk_size -
469 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
471 if (zp->z_is_mapped && !(ioflag & O_DIRECT))
472 error = mappedread(ip, nbytes, uio);
474 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
477 /* convert checksum errors into IO errors */
486 zfs_range_unlock(rl);
488 ZFS_ACCESSTIME_STAMP(zsb, zp);
489 zfs_inode_update(zp);
493 EXPORT_SYMBOL(zfs_read);
496 * Write the bytes to a file.
498 * IN: ip - inode of file to be written to.
499 * uio - structure supplying write location, range info,
501 * ioflag - FAPPEND flag set if in append mode.
502 * O_DIRECT flag; used to bypass page cache.
503 * cr - credentials of caller.
505 * OUT: uio - updated offset and range.
507 * RETURN: 0 if success
508 * error code if failure
511 * ip - ctime|mtime updated if byte count > 0
516 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
518 znode_t *zp = ITOZ(ip);
519 rlim64_t limit = uio->uio_limit;
520 ssize_t start_resid = uio->uio_resid;
524 zfs_sb_t *zsb = ZTOZSB(zp);
529 int max_blksz = zsb->z_max_blksz;
532 iovec_t *aiov = NULL;
535 iovec_t *iovp = uio->uio_iov;
538 sa_bulk_attr_t bulk[4];
539 uint64_t mtime[2], ctime[2];
540 ASSERTV(int iovcnt = uio->uio_iovcnt);
543 * Fasttrack empty write
549 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
555 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
556 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
557 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
558 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
562 * If immutable or not appending then return EPERM
564 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
565 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
566 (uio->uio_loffset < zp->z_size))) {
574 * Validate file offset
576 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
583 * Check for mandatory locks before calling zfs_range_lock()
584 * in order to prevent a deadlock with locks set via fcntl().
586 if (mandatory_lock(ip) && !lock_may_write(ip, woff, n)) {
591 #ifdef HAVE_UIO_ZEROCOPY
593 * Pre-fault the pages to ensure slow (eg NFS) pages
595 * Skip this if uio contains loaned arc_buf.
597 if ((uio->uio_extflg == UIO_XUIO) &&
598 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
599 xuio = (xuio_t *)uio;
601 uio_prefaultpages(MIN(n, max_blksz), uio);
602 #endif /* HAVE_UIO_ZEROCOPY */
605 * If in append mode, set the io offset pointer to eof.
607 if (ioflag & FAPPEND) {
609 * Obtain an appending range lock to guarantee file append
610 * semantics. We reset the write offset once we have the lock.
612 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
614 if (rl->r_len == UINT64_MAX) {
616 * We overlocked the file because this write will cause
617 * the file block size to increase.
618 * Note that zp_size cannot change with this lock held.
622 uio->uio_loffset = woff;
625 * Note that if the file block size will change as a result of
626 * this write, then this range lock will lock the entire file
627 * so that we can re-write the block safely.
629 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
633 zfs_range_unlock(rl);
638 if ((woff + n) > limit || woff > (limit - n))
641 /* Will this write extend the file length? */
642 write_eof = (woff + n > zp->z_size);
644 end_size = MAX(zp->z_size, woff + n);
647 * Write the file in reasonable size chunks. Each chunk is written
648 * in a separate transaction; this keeps the intent log records small
649 * and allows us to do more fine-grained space accounting.
653 woff = uio->uio_loffset;
655 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
656 zfs_owner_overquota(zsb, zp, B_TRUE)) {
658 dmu_return_arcbuf(abuf);
663 if (xuio && abuf == NULL) {
664 ASSERT(i_iov < iovcnt);
666 abuf = dmu_xuio_arcbuf(xuio, i_iov);
667 dmu_xuio_clear(xuio, i_iov);
668 ASSERT((aiov->iov_base == abuf->b_data) ||
669 ((char *)aiov->iov_base - (char *)abuf->b_data +
670 aiov->iov_len == arc_buf_size(abuf)));
672 } else if (abuf == NULL && n >= max_blksz &&
673 woff >= zp->z_size &&
674 P2PHASE(woff, max_blksz) == 0 &&
675 zp->z_blksz == max_blksz) {
677 * This write covers a full block. "Borrow" a buffer
678 * from the dmu so that we can fill it before we enter
679 * a transaction. This avoids the possibility of
680 * holding up the transaction if the data copy hangs
681 * up on a pagefault (e.g., from an NFS server mapping).
685 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
687 ASSERT(abuf != NULL);
688 ASSERT(arc_buf_size(abuf) == max_blksz);
689 if ((error = uiocopy(abuf->b_data, max_blksz,
690 UIO_WRITE, uio, &cbytes))) {
691 dmu_return_arcbuf(abuf);
694 ASSERT(cbytes == max_blksz);
698 * Start a transaction.
700 tx = dmu_tx_create(zsb->z_os);
701 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
702 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
703 zfs_sa_upgrade_txholds(tx, zp);
704 error = dmu_tx_assign(tx, TXG_NOWAIT);
706 if (error == ERESTART) {
713 dmu_return_arcbuf(abuf);
718 * If zfs_range_lock() over-locked we grow the blocksize
719 * and then reduce the lock range. This will only happen
720 * on the first iteration since zfs_range_reduce() will
721 * shrink down r_len to the appropriate size.
723 if (rl->r_len == UINT64_MAX) {
726 if (zp->z_blksz > max_blksz) {
727 ASSERT(!ISP2(zp->z_blksz));
728 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
730 new_blksz = MIN(end_size, max_blksz);
732 zfs_grow_blocksize(zp, new_blksz, tx);
733 zfs_range_reduce(rl, woff, n);
737 * XXX - should we really limit each write to z_max_blksz?
738 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
740 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
743 tx_bytes = uio->uio_resid;
744 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
746 tx_bytes -= uio->uio_resid;
749 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
751 * If this is not a full block write, but we are
752 * extending the file past EOF and this data starts
753 * block-aligned, use assign_arcbuf(). Otherwise,
754 * write via dmu_write().
756 if (tx_bytes < max_blksz && (!write_eof ||
757 aiov->iov_base != abuf->b_data)) {
759 dmu_write(zsb->z_os, zp->z_id, woff,
760 aiov->iov_len, aiov->iov_base, tx);
761 dmu_return_arcbuf(abuf);
762 xuio_stat_wbuf_copied();
764 ASSERT(xuio || tx_bytes == max_blksz);
765 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
768 ASSERT(tx_bytes <= uio->uio_resid);
769 uioskip(uio, tx_bytes);
772 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
773 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
776 * If we made no progress, we're done. If we made even
777 * partial progress, update the znode and ZIL accordingly.
780 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
781 (void *)&zp->z_size, sizeof (uint64_t), tx);
788 * Clear Set-UID/Set-GID bits on successful write if not
789 * privileged and at least one of the excute bits is set.
791 * It would be nice to to this after all writes have
792 * been done, but that would still expose the ISUID/ISGID
793 * to another app after the partial write is committed.
795 * Note: we don't call zfs_fuid_map_id() here because
796 * user 0 is not an ephemeral uid.
798 mutex_enter(&zp->z_acl_lock);
799 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
800 (S_IXUSR >> 6))) != 0 &&
801 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
802 secpolicy_vnode_setid_retain(cr,
803 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
805 zp->z_mode &= ~(S_ISUID | S_ISGID);
806 newmode = zp->z_mode;
807 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
808 (void *)&newmode, sizeof (uint64_t), tx);
810 mutex_exit(&zp->z_acl_lock);
812 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
816 * Update the file size (zp_size) if it has changed;
817 * account for possible concurrent updates.
819 while ((end_size = zp->z_size) < uio->uio_loffset) {
820 (void) atomic_cas_64(&zp->z_size, end_size,
825 * If we are replaying and eof is non zero then force
826 * the file size to the specified eof. Note, there's no
827 * concurrency during replay.
829 if (zsb->z_replay && zsb->z_replay_eof != 0)
830 zp->z_size = zsb->z_replay_eof;
832 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
834 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
839 ASSERT(tx_bytes == nbytes);
843 uio_prefaultpages(MIN(n, max_blksz), uio);
846 zfs_range_unlock(rl);
849 * If we're in replay mode, or we made no progress, return error.
850 * Otherwise, it's at least a partial write, so it's successful.
852 if (zsb->z_replay || uio->uio_resid == start_resid) {
857 if (ioflag & (FSYNC | FDSYNC) ||
858 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
859 zil_commit(zilog, zp->z_id);
861 zfs_inode_update(zp);
865 EXPORT_SYMBOL(zfs_write);
868 iput_async(struct inode *ip, taskq_t *taskq)
870 ASSERT(atomic_read(&ip->i_count) > 0);
871 if (atomic_read(&ip->i_count) == 1)
872 taskq_dispatch(taskq, (task_func_t *)iput, ip, TQ_PUSHPAGE);
878 zfs_get_done(zgd_t *zgd, int error)
880 znode_t *zp = zgd->zgd_private;
881 objset_t *os = ZTOZSB(zp)->z_os;
884 dmu_buf_rele(zgd->zgd_db, zgd);
886 zfs_range_unlock(zgd->zgd_rl);
889 * Release the vnode asynchronously as we currently have the
890 * txg stopped from syncing.
892 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
894 if (error == 0 && zgd->zgd_bp)
895 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
897 kmem_free(zgd, sizeof (zgd_t));
901 static int zil_fault_io = 0;
905 * Get data to generate a TX_WRITE intent log record.
908 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
911 objset_t *os = zsb->z_os;
913 uint64_t object = lr->lr_foid;
914 uint64_t offset = lr->lr_offset;
915 uint64_t size = lr->lr_length;
916 blkptr_t *bp = &lr->lr_blkptr;
925 * Nothing to do if the file has been removed
927 if (zfs_zget(zsb, object, &zp) != 0)
929 if (zp->z_unlinked) {
931 * Release the vnode asynchronously as we currently have the
932 * txg stopped from syncing.
934 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
938 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_PUSHPAGE);
939 zgd->zgd_zilog = zsb->z_log;
940 zgd->zgd_private = zp;
943 * Write records come in two flavors: immediate and indirect.
944 * For small writes it's cheaper to store the data with the
945 * log record (immediate); for large writes it's cheaper to
946 * sync the data and get a pointer to it (indirect) so that
947 * we don't have to write the data twice.
949 if (buf != NULL) { /* immediate write */
950 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
951 /* test for truncation needs to be done while range locked */
952 if (offset >= zp->z_size) {
955 error = dmu_read(os, object, offset, size, buf,
956 DMU_READ_NO_PREFETCH);
958 ASSERT(error == 0 || error == ENOENT);
959 } else { /* indirect write */
961 * Have to lock the whole block to ensure when it's
962 * written out and it's checksum is being calculated
963 * that no one can change the data. We need to re-check
964 * blocksize after we get the lock in case it's changed!
969 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
971 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
973 if (zp->z_blksz == size)
976 zfs_range_unlock(zgd->zgd_rl);
978 /* test for truncation needs to be done while range locked */
979 if (lr->lr_offset >= zp->z_size)
988 error = dmu_buf_hold(os, object, offset, zgd, &db,
989 DMU_READ_NO_PREFETCH);
995 ASSERT(db->db_offset == offset);
996 ASSERT(db->db_size == size);
998 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1000 ASSERT(error || lr->lr_length <= zp->z_blksz);
1003 * On success, we need to wait for the write I/O
1004 * initiated by dmu_sync() to complete before we can
1005 * release this dbuf. We will finish everything up
1006 * in the zfs_get_done() callback.
1011 if (error == EALREADY) {
1012 lr->lr_common.lrc_txtype = TX_WRITE2;
1018 zfs_get_done(zgd, error);
1025 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1027 znode_t *zp = ITOZ(ip);
1028 zfs_sb_t *zsb = ITOZSB(ip);
1034 if (flag & V_ACE_MASK)
1035 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1037 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1042 EXPORT_SYMBOL(zfs_access);
1045 * Lookup an entry in a directory, or an extended attribute directory.
1046 * If it exists, return a held inode reference for it.
1048 * IN: dip - inode of directory to search.
1049 * nm - name of entry to lookup.
1050 * flags - LOOKUP_XATTR set if looking for an attribute.
1051 * cr - credentials of caller.
1052 * direntflags - directory lookup flags
1053 * realpnp - returned pathname.
1055 * OUT: ipp - inode of located entry, NULL if not found.
1057 * RETURN: 0 if success
1058 * error code if failure
1065 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1066 cred_t *cr, int *direntflags, pathname_t *realpnp)
1068 znode_t *zdp = ITOZ(dip);
1069 zfs_sb_t *zsb = ITOZSB(dip);
1073 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1075 if (!S_ISDIR(dip->i_mode)) {
1077 } else if (zdp->z_sa_hdl == NULL) {
1081 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1082 error = zfs_fastaccesschk_execute(zdp, cr);
1091 vnode_t *tvp = dnlc_lookup(dvp, nm);
1094 error = zfs_fastaccesschk_execute(zdp, cr);
1099 if (tvp == DNLC_NO_VNODE) {
1104 return (specvp_check(vpp, cr));
1107 #endif /* HAVE_DNLC */
1116 if (flags & LOOKUP_XATTR) {
1118 * We don't allow recursive attributes..
1119 * Maybe someday we will.
1121 if (zdp->z_pflags & ZFS_XATTR) {
1126 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1132 * Do we have permission to get into attribute directory?
1135 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1145 if (!S_ISDIR(dip->i_mode)) {
1151 * Check accessibility of directory.
1154 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1159 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
1160 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1165 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1166 if ((error == 0) && (*ipp))
1167 zfs_inode_update(ITOZ(*ipp));
1172 EXPORT_SYMBOL(zfs_lookup);
1175 * Attempt to create a new entry in a directory. If the entry
1176 * already exists, truncate the file if permissible, else return
1177 * an error. Return the ip of the created or trunc'd file.
1179 * IN: dip - inode of directory to put new file entry in.
1180 * name - name of new file entry.
1181 * vap - attributes of new file.
1182 * excl - flag indicating exclusive or non-exclusive mode.
1183 * mode - mode to open file with.
1184 * cr - credentials of caller.
1185 * flag - large file flag [UNUSED].
1186 * vsecp - ACL to be set
1188 * OUT: ipp - inode of created or trunc'd entry.
1190 * RETURN: 0 if success
1191 * error code if failure
1194 * dip - ctime|mtime updated if new entry created
1195 * ip - ctime|mtime always, atime if new
1200 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1201 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1203 znode_t *zp, *dzp = ITOZ(dip);
1204 zfs_sb_t *zsb = ITOZSB(dip);
1212 zfs_acl_ids_t acl_ids;
1213 boolean_t fuid_dirtied;
1214 boolean_t have_acl = B_FALSE;
1217 * If we have an ephemeral id, ACL, or XVATTR then
1218 * make sure file system is at proper version
1224 if (zsb->z_use_fuids == B_FALSE &&
1225 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1233 if (zsb->z_utf8 && u8_validate(name, strlen(name),
1234 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1239 if (vap->va_mask & ATTR_XVATTR) {
1240 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1241 crgetuid(cr), cr, vap->va_mode)) != 0) {
1249 if (*name == '\0') {
1251 * Null component name refers to the directory itself.
1258 /* possible igrab(zp) */
1261 if (flag & FIGNORECASE)
1264 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1268 zfs_acl_ids_free(&acl_ids);
1269 if (strcmp(name, "..") == 0)
1280 * Create a new file object and update the directory
1283 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1285 zfs_acl_ids_free(&acl_ids);
1290 * We only support the creation of regular files in
1291 * extended attribute directories.
1294 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1296 zfs_acl_ids_free(&acl_ids);
1301 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1302 cr, vsecp, &acl_ids)) != 0)
1306 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1307 zfs_acl_ids_free(&acl_ids);
1312 tx = dmu_tx_create(os);
1314 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1315 ZFS_SA_BASE_ATTR_SIZE);
1317 fuid_dirtied = zsb->z_fuid_dirty;
1319 zfs_fuid_txhold(zsb, tx);
1320 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1321 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1322 if (!zsb->z_use_sa &&
1323 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1324 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1325 0, acl_ids.z_aclp->z_acl_bytes);
1327 error = dmu_tx_assign(tx, TXG_NOWAIT);
1329 zfs_dirent_unlock(dl);
1330 if (error == ERESTART) {
1335 zfs_acl_ids_free(&acl_ids);
1340 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1343 zfs_fuid_sync(zsb, tx);
1345 (void) zfs_link_create(dl, zp, tx, ZNEW);
1346 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1347 if (flag & FIGNORECASE)
1349 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1350 vsecp, acl_ids.z_fuidp, vap);
1351 zfs_acl_ids_free(&acl_ids);
1354 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1357 zfs_acl_ids_free(&acl_ids);
1361 * A directory entry already exists for this name.
1364 * Can't truncate an existing file if in exclusive mode.
1371 * Can't open a directory for writing.
1373 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1378 * Verify requested access to file.
1380 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1384 mutex_enter(&dzp->z_lock);
1386 mutex_exit(&dzp->z_lock);
1389 * Truncate regular files if requested.
1391 if (S_ISREG(ZTOI(zp)->i_mode) &&
1392 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1393 /* we can't hold any locks when calling zfs_freesp() */
1394 zfs_dirent_unlock(dl);
1396 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1402 zfs_dirent_unlock(dl);
1408 zfs_inode_update(dzp);
1409 zfs_inode_update(zp);
1413 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1414 zil_commit(zilog, 0);
1419 EXPORT_SYMBOL(zfs_create);
1422 * Remove an entry from a directory.
1424 * IN: dip - inode of directory to remove entry from.
1425 * name - name of entry to remove.
1426 * cr - credentials of caller.
1428 * RETURN: 0 if success
1429 * error code if failure
1433 * ip - ctime (if nlink > 0)
1436 uint64_t null_xattr = 0;
1440 zfs_remove(struct inode *dip, char *name, cred_t *cr)
1442 znode_t *zp, *dzp = ITOZ(dip);
1445 zfs_sb_t *zsb = ITOZSB(dip);
1448 uint64_t xattr_obj_unlinked = 0;
1454 pathname_t *realnmp = NULL;
1455 #ifdef HAVE_PN_UTILS
1457 #endif /* HAVE_PN_UTILS */
1465 #ifdef HAVE_PN_UTILS
1466 if (flags & FIGNORECASE) {
1471 #endif /* HAVE_PN_UTILS */
1477 * Attempt to lock directory; fail if entry doesn't exist.
1479 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1481 #ifdef HAVE_PN_UTILS
1484 #endif /* HAVE_PN_UTILS */
1491 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1496 * Need to use rmdir for removing directories.
1498 if (S_ISDIR(ip->i_mode)) {
1505 dnlc_remove(dvp, realnmp->pn_buf);
1507 dnlc_remove(dvp, name);
1508 #endif /* HAVE_DNLC */
1511 * We never delete the znode and always place it in the unlinked
1512 * set. The dentry cache will always hold the last reference and
1513 * is responsible for safely freeing the znode.
1516 tx = dmu_tx_create(zsb->z_os);
1517 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1518 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1519 zfs_sa_upgrade_txholds(tx, zp);
1520 zfs_sa_upgrade_txholds(tx, dzp);
1522 /* are there any extended attributes? */
1523 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1524 &xattr_obj, sizeof (xattr_obj));
1525 if (error == 0 && xattr_obj) {
1526 error = zfs_zget(zsb, xattr_obj, &xzp);
1528 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1529 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1532 /* charge as an update -- would be nice not to charge at all */
1533 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1535 error = dmu_tx_assign(tx, TXG_NOWAIT);
1537 zfs_dirent_unlock(dl);
1541 if (error == ERESTART) {
1546 #ifdef HAVE_PN_UTILS
1549 #endif /* HAVE_PN_UTILS */
1556 * Remove the directory entry.
1558 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1567 * Hold z_lock so that we can make sure that the ACL obj
1568 * hasn't changed. Could have been deleted due to
1571 mutex_enter(&zp->z_lock);
1572 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1573 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1574 mutex_exit(&zp->z_lock);
1575 zfs_unlinked_add(zp, tx);
1579 #ifdef HAVE_PN_UTILS
1580 if (flags & FIGNORECASE)
1582 #endif /* HAVE_PN_UTILS */
1583 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1587 #ifdef HAVE_PN_UTILS
1590 #endif /* HAVE_PN_UTILS */
1592 zfs_dirent_unlock(dl);
1593 zfs_inode_update(dzp);
1594 zfs_inode_update(zp);
1596 zfs_inode_update(xzp);
1602 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1603 zil_commit(zilog, 0);
1608 EXPORT_SYMBOL(zfs_remove);
1611 * Create a new directory and insert it into dip using the name
1612 * provided. Return a pointer to the inserted directory.
1614 * IN: dip - inode of directory to add subdir to.
1615 * dirname - name of new directory.
1616 * vap - attributes of new directory.
1617 * cr - credentials of caller.
1618 * vsecp - ACL to be set
1620 * OUT: ipp - inode of created directory.
1622 * RETURN: 0 if success
1623 * error code if failure
1626 * dip - ctime|mtime updated
1627 * ipp - ctime|mtime|atime updated
1631 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1632 cred_t *cr, int flags, vsecattr_t *vsecp)
1634 znode_t *zp, *dzp = ITOZ(dip);
1635 zfs_sb_t *zsb = ITOZSB(dip);
1643 gid_t gid = crgetgid(cr);
1644 zfs_acl_ids_t acl_ids;
1645 boolean_t fuid_dirtied;
1647 ASSERT(S_ISDIR(vap->va_mode));
1650 * If we have an ephemeral id, ACL, or XVATTR then
1651 * make sure file system is at proper version
1655 if (zsb->z_use_fuids == B_FALSE &&
1656 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1663 if (dzp->z_pflags & ZFS_XATTR) {
1668 if (zsb->z_utf8 && u8_validate(dirname,
1669 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1673 if (flags & FIGNORECASE)
1676 if (vap->va_mask & ATTR_XVATTR) {
1677 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1678 crgetuid(cr), cr, vap->va_mode)) != 0) {
1684 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1685 vsecp, &acl_ids)) != 0) {
1690 * First make sure the new directory doesn't exist.
1692 * Existence is checked first to make sure we don't return
1693 * EACCES instead of EEXIST which can cause some applications
1699 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1701 zfs_acl_ids_free(&acl_ids);
1706 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1707 zfs_acl_ids_free(&acl_ids);
1708 zfs_dirent_unlock(dl);
1713 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1714 zfs_acl_ids_free(&acl_ids);
1715 zfs_dirent_unlock(dl);
1721 * Add a new entry to the directory.
1723 tx = dmu_tx_create(zsb->z_os);
1724 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1725 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1726 fuid_dirtied = zsb->z_fuid_dirty;
1728 zfs_fuid_txhold(zsb, tx);
1729 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1730 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1731 acl_ids.z_aclp->z_acl_bytes);
1734 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1735 ZFS_SA_BASE_ATTR_SIZE);
1737 error = dmu_tx_assign(tx, TXG_NOWAIT);
1739 zfs_dirent_unlock(dl);
1740 if (error == ERESTART) {
1745 zfs_acl_ids_free(&acl_ids);
1754 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1757 zfs_fuid_sync(zsb, tx);
1760 * Now put new name in parent dir.
1762 (void) zfs_link_create(dl, zp, tx, ZNEW);
1766 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1767 if (flags & FIGNORECASE)
1769 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1770 acl_ids.z_fuidp, vap);
1772 zfs_acl_ids_free(&acl_ids);
1776 zfs_dirent_unlock(dl);
1778 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1779 zil_commit(zilog, 0);
1781 zfs_inode_update(dzp);
1782 zfs_inode_update(zp);
1786 EXPORT_SYMBOL(zfs_mkdir);
1789 * Remove a directory subdir entry. If the current working
1790 * directory is the same as the subdir to be removed, the
1793 * IN: dip - inode of directory to remove from.
1794 * name - name of directory to be removed.
1795 * cwd - inode of current working directory.
1796 * cr - credentials of caller.
1797 * flags - case flags
1799 * RETURN: 0 if success
1800 * error code if failure
1803 * dip - ctime|mtime updated
1807 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1810 znode_t *dzp = ITOZ(dip);
1813 zfs_sb_t *zsb = ITOZSB(dip);
1824 if (flags & FIGNORECASE)
1830 * Attempt to lock directory; fail if entry doesn't exist.
1832 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1840 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1844 if (!S_ISDIR(ip->i_mode)) {
1855 * Grab a lock on the directory to make sure that noone is
1856 * trying to add (or lookup) entries while we are removing it.
1858 rw_enter(&zp->z_name_lock, RW_WRITER);
1861 * Grab a lock on the parent pointer to make sure we play well
1862 * with the treewalk and directory rename code.
1864 rw_enter(&zp->z_parent_lock, RW_WRITER);
1866 tx = dmu_tx_create(zsb->z_os);
1867 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1868 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1869 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1870 zfs_sa_upgrade_txholds(tx, zp);
1871 zfs_sa_upgrade_txholds(tx, dzp);
1872 error = dmu_tx_assign(tx, TXG_NOWAIT);
1874 rw_exit(&zp->z_parent_lock);
1875 rw_exit(&zp->z_name_lock);
1876 zfs_dirent_unlock(dl);
1878 if (error == ERESTART) {
1888 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1891 uint64_t txtype = TX_RMDIR;
1892 if (flags & FIGNORECASE)
1894 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
1899 rw_exit(&zp->z_parent_lock);
1900 rw_exit(&zp->z_name_lock);
1902 zfs_dirent_unlock(dl);
1904 zfs_inode_update(dzp);
1905 zfs_inode_update(zp);
1908 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1909 zil_commit(zilog, 0);
1914 EXPORT_SYMBOL(zfs_rmdir);
1917 * Read as many directory entries as will fit into the provided
1918 * dirent buffer from the given directory cursor position.
1920 * IN: ip - inode of directory to read.
1921 * dirent - buffer for directory entries.
1923 * OUT: dirent - filler buffer of directory entries.
1925 * RETURN: 0 if success
1926 * error code if failure
1929 * ip - atime updated
1931 * Note that the low 4 bits of the cookie returned by zap is always zero.
1932 * This allows us to use the low range for "special" directory entries:
1933 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1934 * we use the offset 2 for the '.zfs' directory.
1938 zfs_readdir(struct inode *ip, void *dirent, filldir_t filldir,
1939 loff_t *pos, cred_t *cr)
1941 znode_t *zp = ITOZ(ip);
1942 zfs_sb_t *zsb = ITOZSB(ip);
1945 zap_attribute_t zap;
1955 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
1956 &parent, sizeof (parent))) != 0)
1960 * Quit if directory has been removed (posix)
1967 prefetch = zp->z_zn_prefetch;
1970 * Initialize the iterator cursor.
1974 * Start iteration from the beginning of the directory.
1976 zap_cursor_init(&zc, os, zp->z_id);
1979 * The offset is a serialized cursor.
1981 zap_cursor_init_serialized(&zc, os, zp->z_id, *pos);
1985 * Transform to file-system independent format
1992 * Special case `.', `..', and `.zfs'.
1995 (void) strcpy(zap.za_name, ".");
1996 zap.za_normalization_conflict = 0;
1998 } else if (*pos == 1) {
1999 (void) strcpy(zap.za_name, "..");
2000 zap.za_normalization_conflict = 0;
2002 } else if (*pos == 2 && zfs_show_ctldir(zp)) {
2003 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2004 zap.za_normalization_conflict = 0;
2005 objnum = ZFSCTL_INO_ROOT;
2010 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2011 if (error == ENOENT)
2018 * Allow multiple entries provided the first entry is
2019 * the object id. Non-zpl consumers may safely make
2020 * use of the additional space.
2022 * XXX: This should be a feature flag for compatibility
2024 if (zap.za_integer_length != 8 ||
2025 zap.za_num_integers == 0) {
2026 cmn_err(CE_WARN, "zap_readdir: bad directory "
2027 "entry, obj = %lld, offset = %lld, "
2028 "length = %d, num = %lld\n",
2029 (u_longlong_t)zp->z_id,
2031 zap.za_integer_length,
2032 (u_longlong_t)zap.za_num_integers);
2037 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2039 done = filldir(dirent, zap.za_name, strlen(zap.za_name),
2040 zap_cursor_serialize(&zc), objnum, 0);
2045 /* Prefetch znode */
2047 dmu_prefetch(os, objnum, 0, 0);
2050 if (*pos > 2 || (*pos == 2 && !zfs_show_ctldir(zp))) {
2051 zap_cursor_advance(&zc);
2052 *pos = zap_cursor_serialize(&zc);
2057 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2060 zap_cursor_fini(&zc);
2061 if (error == ENOENT)
2064 ZFS_ACCESSTIME_STAMP(zsb, zp);
2065 zfs_inode_update(zp);
2072 EXPORT_SYMBOL(zfs_readdir);
2074 ulong_t zfs_fsync_sync_cnt = 4;
2077 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2079 znode_t *zp = ITOZ(ip);
2080 zfs_sb_t *zsb = ITOZSB(ip);
2082 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2084 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2087 zil_commit(zsb->z_log, zp->z_id);
2092 EXPORT_SYMBOL(zfs_fsync);
2096 * Get the requested file attributes and place them in the provided
2099 * IN: ip - inode of file.
2100 * vap - va_mask identifies requested attributes.
2101 * If ATTR_XVATTR set, then optional attrs are requested
2102 * flags - ATTR_NOACLCHECK (CIFS server context)
2103 * cr - credentials of caller.
2105 * OUT: vap - attribute values.
2107 * RETURN: 0 (always succeeds)
2111 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2113 znode_t *zp = ITOZ(ip);
2114 zfs_sb_t *zsb = ITOZSB(ip);
2117 uint64_t mtime[2], ctime[2];
2118 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2119 xoptattr_t *xoap = NULL;
2120 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2121 sa_bulk_attr_t bulk[2];
2127 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2129 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2130 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2132 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2138 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2139 * Also, if we are the owner don't bother, since owner should
2140 * always be allowed to read basic attributes of file.
2142 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2143 (vap->va_uid != crgetuid(cr))) {
2144 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2152 * Return all attributes. It's cheaper to provide the answer
2153 * than to determine whether we were asked the question.
2156 mutex_enter(&zp->z_lock);
2157 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2158 vap->va_mode = zp->z_mode;
2159 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2160 vap->va_nodeid = zp->z_id;
2161 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2162 links = zp->z_links + 1;
2164 links = zp->z_links;
2165 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2166 vap->va_size = i_size_read(ip);
2167 vap->va_rdev = ip->i_rdev;
2168 vap->va_seq = ip->i_generation;
2171 * Add in any requested optional attributes and the create time.
2172 * Also set the corresponding bits in the returned attribute bitmap.
2174 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2175 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2177 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2178 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2181 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2182 xoap->xoa_readonly =
2183 ((zp->z_pflags & ZFS_READONLY) != 0);
2184 XVA_SET_RTN(xvap, XAT_READONLY);
2187 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2189 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2190 XVA_SET_RTN(xvap, XAT_SYSTEM);
2193 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2195 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2196 XVA_SET_RTN(xvap, XAT_HIDDEN);
2199 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2200 xoap->xoa_nounlink =
2201 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2202 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2205 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2206 xoap->xoa_immutable =
2207 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2208 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2211 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2212 xoap->xoa_appendonly =
2213 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2214 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2217 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2219 ((zp->z_pflags & ZFS_NODUMP) != 0);
2220 XVA_SET_RTN(xvap, XAT_NODUMP);
2223 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2225 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2226 XVA_SET_RTN(xvap, XAT_OPAQUE);
2229 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2230 xoap->xoa_av_quarantined =
2231 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2232 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2235 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2236 xoap->xoa_av_modified =
2237 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2238 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2241 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2242 S_ISREG(ip->i_mode)) {
2243 zfs_sa_get_scanstamp(zp, xvap);
2246 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2249 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2250 times, sizeof (times));
2251 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2252 XVA_SET_RTN(xvap, XAT_CREATETIME);
2255 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2256 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2257 XVA_SET_RTN(xvap, XAT_REPARSE);
2259 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2260 xoap->xoa_generation = zp->z_gen;
2261 XVA_SET_RTN(xvap, XAT_GEN);
2264 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2266 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2267 XVA_SET_RTN(xvap, XAT_OFFLINE);
2270 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2272 ((zp->z_pflags & ZFS_SPARSE) != 0);
2273 XVA_SET_RTN(xvap, XAT_SPARSE);
2277 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2278 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2279 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2281 mutex_exit(&zp->z_lock);
2283 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2285 if (zp->z_blksz == 0) {
2287 * Block size hasn't been set; suggest maximal I/O transfers.
2289 vap->va_blksize = zsb->z_max_blksz;
2295 EXPORT_SYMBOL(zfs_getattr);
2298 * Get the basic file attributes and place them in the provided kstat
2299 * structure. The inode is assumed to be the authoritative source
2300 * for most of the attributes. However, the znode currently has the
2301 * authoritative atime, blksize, and block count.
2303 * IN: ip - inode of file.
2305 * OUT: sp - kstat values.
2307 * RETURN: 0 (always succeeds)
2311 zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2313 znode_t *zp = ITOZ(ip);
2314 zfs_sb_t *zsb = ITOZSB(ip);
2319 mutex_enter(&zp->z_lock);
2321 generic_fillattr(ip, sp);
2322 ZFS_TIME_DECODE(&sp->atime, zp->z_atime);
2324 sa_object_size(zp->z_sa_hdl, (uint32_t *)&sp->blksize, &sp->blocks);
2325 if (unlikely(zp->z_blksz == 0)) {
2327 * Block size hasn't been set; suggest maximal I/O transfers.
2329 sp->blksize = zsb->z_max_blksz;
2332 mutex_exit(&zp->z_lock);
2338 EXPORT_SYMBOL(zfs_getattr_fast);
2341 * Set the file attributes to the values contained in the
2344 * IN: ip - inode of file to be modified.
2345 * vap - new attribute values.
2346 * If ATTR_XVATTR set, then optional attrs are being set
2347 * flags - ATTR_UTIME set if non-default time values provided.
2348 * - ATTR_NOACLCHECK (CIFS context only).
2349 * cr - credentials of caller.
2351 * RETURN: 0 if success
2352 * error code if failure
2355 * ip - ctime updated, mtime updated if size changed.
2359 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2361 znode_t *zp = ITOZ(ip);
2362 zfs_sb_t *zsb = ITOZSB(ip);
2366 xvattr_t *tmpxvattr;
2367 uint_t mask = vap->va_mask;
2371 uint64_t new_uid, new_gid;
2373 uint64_t mtime[2], ctime[2];
2375 int need_policy = FALSE;
2377 zfs_fuid_info_t *fuidp = NULL;
2378 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2381 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2382 boolean_t fuid_dirtied = B_FALSE;
2383 sa_bulk_attr_t *bulk, *xattr_bulk;
2384 int count = 0, xattr_count = 0;
2395 * Make sure that if we have ephemeral uid/gid or xvattr specified
2396 * that file system is at proper version level
2399 if (zsb->z_use_fuids == B_FALSE &&
2400 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2401 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2402 (mask & ATTR_XVATTR))) {
2407 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2412 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2418 * If this is an xvattr_t, then get a pointer to the structure of
2419 * optional attributes. If this is NULL, then we have a vattr_t.
2421 xoap = xva_getxoptattr(xvap);
2423 tmpxvattr = kmem_alloc(sizeof(xvattr_t), KM_SLEEP);
2424 xva_init(tmpxvattr);
2426 bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2427 xattr_bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2430 * Immutable files can only alter immutable bit and atime
2432 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2433 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2434 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2439 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2445 * Verify timestamps doesn't overflow 32 bits.
2446 * ZFS can handle large timestamps, but 32bit syscalls can't
2447 * handle times greater than 2039. This check should be removed
2448 * once large timestamps are fully supported.
2450 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2451 if (((mask & ATTR_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2452 ((mask & ATTR_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2462 /* Can this be moved to before the top label? */
2463 if (zfs_is_readonly(zsb)) {
2469 * First validate permissions
2472 if (mask & ATTR_SIZE) {
2473 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2477 truncate_setsize(ip, vap->va_size);
2480 * XXX - Note, we are not providing any open
2481 * mode flags here (like FNDELAY), so we may
2482 * block if there are locks present... this
2483 * should be addressed in openat().
2485 /* XXX - would it be OK to generate a log record here? */
2486 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2491 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2492 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2493 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2494 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2495 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2496 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2497 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2498 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2499 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2503 if (mask & (ATTR_UID|ATTR_GID)) {
2504 int idmask = (mask & (ATTR_UID|ATTR_GID));
2509 * NOTE: even if a new mode is being set,
2510 * we may clear S_ISUID/S_ISGID bits.
2513 if (!(mask & ATTR_MODE))
2514 vap->va_mode = zp->z_mode;
2517 * Take ownership or chgrp to group we are a member of
2520 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2521 take_group = (mask & ATTR_GID) &&
2522 zfs_groupmember(zsb, vap->va_gid, cr);
2525 * If both ATTR_UID and ATTR_GID are set then take_owner and
2526 * take_group must both be set in order to allow taking
2529 * Otherwise, send the check through secpolicy_vnode_setattr()
2533 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2534 take_owner && take_group) ||
2535 ((idmask == ATTR_UID) && take_owner) ||
2536 ((idmask == ATTR_GID) && take_group)) {
2537 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2538 skipaclchk, cr) == 0) {
2540 * Remove setuid/setgid for non-privileged users
2542 (void) secpolicy_setid_clear(vap, cr);
2543 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2552 mutex_enter(&zp->z_lock);
2553 oldva.va_mode = zp->z_mode;
2554 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2555 if (mask & ATTR_XVATTR) {
2557 * Update xvattr mask to include only those attributes
2558 * that are actually changing.
2560 * the bits will be restored prior to actually setting
2561 * the attributes so the caller thinks they were set.
2563 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2564 if (xoap->xoa_appendonly !=
2565 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2568 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2569 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2573 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2574 if (xoap->xoa_nounlink !=
2575 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2578 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2579 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2583 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2584 if (xoap->xoa_immutable !=
2585 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2588 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2589 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2593 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2594 if (xoap->xoa_nodump !=
2595 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2598 XVA_CLR_REQ(xvap, XAT_NODUMP);
2599 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2603 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2604 if (xoap->xoa_av_modified !=
2605 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2608 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2609 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2613 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2614 if ((!S_ISREG(ip->i_mode) &&
2615 xoap->xoa_av_quarantined) ||
2616 xoap->xoa_av_quarantined !=
2617 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2620 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2621 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2625 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2626 mutex_exit(&zp->z_lock);
2631 if (need_policy == FALSE &&
2632 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2633 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2638 mutex_exit(&zp->z_lock);
2640 if (mask & ATTR_MODE) {
2641 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2642 err = secpolicy_setid_setsticky_clear(ip, vap,
2647 trim_mask |= ATTR_MODE;
2655 * If trim_mask is set then take ownership
2656 * has been granted or write_acl is present and user
2657 * has the ability to modify mode. In that case remove
2658 * UID|GID and or MODE from mask so that
2659 * secpolicy_vnode_setattr() doesn't revoke it.
2663 saved_mask = vap->va_mask;
2664 vap->va_mask &= ~trim_mask;
2666 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2667 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2672 vap->va_mask |= saved_mask;
2676 * secpolicy_vnode_setattr, or take ownership may have
2679 mask = vap->va_mask;
2681 if ((mask & (ATTR_UID | ATTR_GID))) {
2682 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2683 &xattr_obj, sizeof (xattr_obj));
2685 if (err == 0 && xattr_obj) {
2686 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2690 if (mask & ATTR_UID) {
2691 new_uid = zfs_fuid_create(zsb,
2692 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2693 if (new_uid != zp->z_uid &&
2694 zfs_fuid_overquota(zsb, B_FALSE, new_uid)) {
2702 if (mask & ATTR_GID) {
2703 new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2704 cr, ZFS_GROUP, &fuidp);
2705 if (new_gid != zp->z_gid &&
2706 zfs_fuid_overquota(zsb, B_TRUE, new_gid)) {
2714 tx = dmu_tx_create(zsb->z_os);
2716 if (mask & ATTR_MODE) {
2717 uint64_t pmode = zp->z_mode;
2719 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2721 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2723 mutex_enter(&zp->z_lock);
2724 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2726 * Are we upgrading ACL from old V0 format
2729 if (zsb->z_version >= ZPL_VERSION_FUID &&
2730 zfs_znode_acl_version(zp) ==
2731 ZFS_ACL_VERSION_INITIAL) {
2732 dmu_tx_hold_free(tx, acl_obj, 0,
2734 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2735 0, aclp->z_acl_bytes);
2737 dmu_tx_hold_write(tx, acl_obj, 0,
2740 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2741 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2742 0, aclp->z_acl_bytes);
2744 mutex_exit(&zp->z_lock);
2745 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2747 if ((mask & ATTR_XVATTR) &&
2748 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2749 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2751 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2755 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2758 fuid_dirtied = zsb->z_fuid_dirty;
2760 zfs_fuid_txhold(zsb, tx);
2762 zfs_sa_upgrade_txholds(tx, zp);
2764 err = dmu_tx_assign(tx, TXG_NOWAIT);
2766 if (err == ERESTART)
2773 * Set each attribute requested.
2774 * We group settings according to the locks they need to acquire.
2776 * Note: you cannot set ctime directly, although it will be
2777 * updated as a side-effect of calling this function.
2781 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2782 mutex_enter(&zp->z_acl_lock);
2783 mutex_enter(&zp->z_lock);
2785 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2786 &zp->z_pflags, sizeof (zp->z_pflags));
2789 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2790 mutex_enter(&attrzp->z_acl_lock);
2791 mutex_enter(&attrzp->z_lock);
2792 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2793 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2794 sizeof (attrzp->z_pflags));
2797 if (mask & (ATTR_UID|ATTR_GID)) {
2799 if (mask & ATTR_UID) {
2800 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2801 &new_uid, sizeof (new_uid));
2802 zp->z_uid = new_uid;
2804 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2805 SA_ZPL_UID(zsb), NULL, &new_uid,
2807 attrzp->z_uid = new_uid;
2811 if (mask & ATTR_GID) {
2812 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2813 NULL, &new_gid, sizeof (new_gid));
2814 zp->z_gid = new_gid;
2816 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2817 SA_ZPL_GID(zsb), NULL, &new_gid,
2819 attrzp->z_gid = new_gid;
2822 if (!(mask & ATTR_MODE)) {
2823 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2824 NULL, &new_mode, sizeof (new_mode));
2825 new_mode = zp->z_mode;
2827 err = zfs_acl_chown_setattr(zp);
2830 err = zfs_acl_chown_setattr(attrzp);
2835 if (mask & ATTR_MODE) {
2836 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
2837 &new_mode, sizeof (new_mode));
2838 zp->z_mode = new_mode;
2839 ASSERT3P(aclp, !=, NULL);
2840 err = zfs_aclset_common(zp, aclp, cr, tx);
2842 if (zp->z_acl_cached)
2843 zfs_acl_free(zp->z_acl_cached);
2844 zp->z_acl_cached = aclp;
2849 if (mask & ATTR_ATIME) {
2850 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2851 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
2852 &zp->z_atime, sizeof (zp->z_atime));
2855 if (mask & ATTR_MTIME) {
2856 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2857 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
2858 mtime, sizeof (mtime));
2861 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2862 if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) {
2863 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
2864 NULL, mtime, sizeof (mtime));
2865 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2866 &ctime, sizeof (ctime));
2867 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
2869 } else if (mask != 0) {
2870 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2871 &ctime, sizeof (ctime));
2872 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
2875 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2876 SA_ZPL_CTIME(zsb), NULL,
2877 &ctime, sizeof (ctime));
2878 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2879 mtime, ctime, B_TRUE);
2883 * Do this after setting timestamps to prevent timestamp
2884 * update from toggling bit
2887 if (xoap && (mask & ATTR_XVATTR)) {
2890 * restore trimmed off masks
2891 * so that return masks can be set for caller.
2894 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
2895 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2897 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
2898 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2900 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
2901 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2903 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
2904 XVA_SET_REQ(xvap, XAT_NODUMP);
2906 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
2907 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2909 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
2910 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2913 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2914 ASSERT(S_ISREG(ip->i_mode));
2916 zfs_xvattr_set(zp, xvap, tx);
2920 zfs_fuid_sync(zsb, tx);
2923 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2925 mutex_exit(&zp->z_lock);
2926 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2927 mutex_exit(&zp->z_acl_lock);
2930 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2931 mutex_exit(&attrzp->z_acl_lock);
2932 mutex_exit(&attrzp->z_lock);
2935 if (err == 0 && attrzp) {
2936 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2947 zfs_fuid_info_free(fuidp);
2953 if (err == ERESTART)
2956 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2958 zfs_inode_update(zp);
2962 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
2963 zil_commit(zilog, 0);
2966 kmem_free(xattr_bulk, sizeof(sa_bulk_attr_t) * 7);
2967 kmem_free(bulk, sizeof(sa_bulk_attr_t) * 7);
2968 kmem_free(tmpxvattr, sizeof(xvattr_t));
2972 EXPORT_SYMBOL(zfs_setattr);
2974 typedef struct zfs_zlock {
2975 krwlock_t *zl_rwlock; /* lock we acquired */
2976 znode_t *zl_znode; /* znode we held */
2977 struct zfs_zlock *zl_next; /* next in list */
2981 * Drop locks and release vnodes that were held by zfs_rename_lock().
2984 zfs_rename_unlock(zfs_zlock_t **zlpp)
2988 while ((zl = *zlpp) != NULL) {
2989 if (zl->zl_znode != NULL)
2990 iput(ZTOI(zl->zl_znode));
2991 rw_exit(zl->zl_rwlock);
2992 *zlpp = zl->zl_next;
2993 kmem_free(zl, sizeof (*zl));
2998 * Search back through the directory tree, using the ".." entries.
2999 * Lock each directory in the chain to prevent concurrent renames.
3000 * Fail any attempt to move a directory into one of its own descendants.
3001 * XXX - z_parent_lock can overlap with map or grow locks
3004 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3008 uint64_t rootid = ZTOZSB(zp)->z_root;
3009 uint64_t oidp = zp->z_id;
3010 krwlock_t *rwlp = &szp->z_parent_lock;
3011 krw_t rw = RW_WRITER;
3014 * First pass write-locks szp and compares to zp->z_id.
3015 * Later passes read-lock zp and compare to zp->z_parent.
3018 if (!rw_tryenter(rwlp, rw)) {
3020 * Another thread is renaming in this path.
3021 * Note that if we are a WRITER, we don't have any
3022 * parent_locks held yet.
3024 if (rw == RW_READER && zp->z_id > szp->z_id) {
3026 * Drop our locks and restart
3028 zfs_rename_unlock(&zl);
3032 rwlp = &szp->z_parent_lock;
3037 * Wait for other thread to drop its locks
3043 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3044 zl->zl_rwlock = rwlp;
3045 zl->zl_znode = NULL;
3046 zl->zl_next = *zlpp;
3049 if (oidp == szp->z_id) /* We're a descendant of szp */
3052 if (oidp == rootid) /* We've hit the top */
3055 if (rw == RW_READER) { /* i.e. not the first pass */
3056 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3061 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3062 &oidp, sizeof (oidp));
3063 rwlp = &zp->z_parent_lock;
3066 } while (zp->z_id != sdzp->z_id);
3072 * Move an entry from the provided source directory to the target
3073 * directory. Change the entry name as indicated.
3075 * IN: sdip - Source directory containing the "old entry".
3076 * snm - Old entry name.
3077 * tdip - Target directory to contain the "new entry".
3078 * tnm - New entry name.
3079 * cr - credentials of caller.
3080 * flags - case flags
3082 * RETURN: 0 if success
3083 * error code if failure
3086 * sdip,tdip - ctime|mtime updated
3090 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3091 cred_t *cr, int flags)
3093 znode_t *tdzp, *szp, *tzp;
3094 znode_t *sdzp = ITOZ(sdip);
3095 zfs_sb_t *zsb = ITOZSB(sdip);
3097 zfs_dirlock_t *sdl, *tdl;
3100 int cmp, serr, terr;
3105 ZFS_VERIFY_ZP(sdzp);
3108 if (tdip->i_sb != sdip->i_sb) {
3114 ZFS_VERIFY_ZP(tdzp);
3115 if (zsb->z_utf8 && u8_validate(tnm,
3116 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3121 if (flags & FIGNORECASE)
3130 * This is to prevent the creation of links into attribute space
3131 * by renaming a linked file into/outof an attribute directory.
3132 * See the comment in zfs_link() for why this is considered bad.
3134 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3140 * Lock source and target directory entries. To prevent deadlock,
3141 * a lock ordering must be defined. We lock the directory with
3142 * the smallest object id first, or if it's a tie, the one with
3143 * the lexically first name.
3145 if (sdzp->z_id < tdzp->z_id) {
3147 } else if (sdzp->z_id > tdzp->z_id) {
3151 * First compare the two name arguments without
3152 * considering any case folding.
3154 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3156 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3157 ASSERT(error == 0 || !zsb->z_utf8);
3160 * POSIX: "If the old argument and the new argument
3161 * both refer to links to the same existing file,
3162 * the rename() function shall return successfully
3163 * and perform no other action."
3169 * If the file system is case-folding, then we may
3170 * have some more checking to do. A case-folding file
3171 * system is either supporting mixed case sensitivity
3172 * access or is completely case-insensitive. Note
3173 * that the file system is always case preserving.
3175 * In mixed sensitivity mode case sensitive behavior
3176 * is the default. FIGNORECASE must be used to
3177 * explicitly request case insensitive behavior.
3179 * If the source and target names provided differ only
3180 * by case (e.g., a request to rename 'tim' to 'Tim'),
3181 * we will treat this as a special case in the
3182 * case-insensitive mode: as long as the source name
3183 * is an exact match, we will allow this to proceed as
3184 * a name-change request.
3186 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3187 (zsb->z_case == ZFS_CASE_MIXED &&
3188 flags & FIGNORECASE)) &&
3189 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3192 * case preserving rename request, require exact
3201 * If the source and destination directories are the same, we should
3202 * grab the z_name_lock of that directory only once.
3206 rw_enter(&sdzp->z_name_lock, RW_READER);
3210 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3211 ZEXISTS | zflg, NULL, NULL);
3212 terr = zfs_dirent_lock(&tdl,
3213 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3215 terr = zfs_dirent_lock(&tdl,
3216 tdzp, tnm, &tzp, zflg, NULL, NULL);
3217 serr = zfs_dirent_lock(&sdl,
3218 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3224 * Source entry invalid or not there.
3227 zfs_dirent_unlock(tdl);
3233 rw_exit(&sdzp->z_name_lock);
3235 if (strcmp(snm, "..") == 0)
3241 zfs_dirent_unlock(sdl);
3245 rw_exit(&sdzp->z_name_lock);
3247 if (strcmp(tnm, "..") == 0)
3254 * Must have write access at the source to remove the old entry
3255 * and write access at the target to create the new entry.
3256 * Note that if target and source are the same, this can be
3257 * done in a single check.
3260 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3263 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3265 * Check to make sure rename is valid.
3266 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3268 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3273 * Does target exist?
3277 * Source and target must be the same type.
3279 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3280 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3285 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3291 * POSIX dictates that when the source and target
3292 * entries refer to the same file object, rename
3293 * must do nothing and exit without error.
3295 if (szp->z_id == tzp->z_id) {
3301 tx = dmu_tx_create(zsb->z_os);
3302 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3303 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3304 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3305 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3307 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3308 zfs_sa_upgrade_txholds(tx, tdzp);
3311 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3312 zfs_sa_upgrade_txholds(tx, tzp);
3315 zfs_sa_upgrade_txholds(tx, szp);
3316 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3317 error = dmu_tx_assign(tx, TXG_NOWAIT);
3320 zfs_rename_unlock(&zl);
3321 zfs_dirent_unlock(sdl);
3322 zfs_dirent_unlock(tdl);
3325 rw_exit(&sdzp->z_name_lock);
3330 if (error == ERESTART) {
3340 if (tzp) /* Attempt to remove the existing target */
3341 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3344 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3346 szp->z_pflags |= ZFS_AV_MODIFIED;
3348 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3349 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3352 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3354 zfs_log_rename(zilog, tx, TX_RENAME |
3355 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3356 sdl->dl_name, tdzp, tdl->dl_name, szp);
3359 * At this point, we have successfully created
3360 * the target name, but have failed to remove
3361 * the source name. Since the create was done
3362 * with the ZRENAMING flag, there are
3363 * complications; for one, the link count is
3364 * wrong. The easiest way to deal with this
3365 * is to remove the newly created target, and
3366 * return the original error. This must
3367 * succeed; fortunately, it is very unlikely to
3368 * fail, since we just created it.
3370 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3371 ZRENAMING, NULL), ==, 0);
3379 zfs_rename_unlock(&zl);
3381 zfs_dirent_unlock(sdl);
3382 zfs_dirent_unlock(tdl);
3384 zfs_inode_update(sdzp);
3386 rw_exit(&sdzp->z_name_lock);
3389 zfs_inode_update(tdzp);
3391 zfs_inode_update(szp);
3394 zfs_inode_update(tzp);
3398 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3399 zil_commit(zilog, 0);
3404 EXPORT_SYMBOL(zfs_rename);
3407 * Insert the indicated symbolic reference entry into the directory.
3409 * IN: dip - Directory to contain new symbolic link.
3410 * link - Name for new symlink entry.
3411 * vap - Attributes of new entry.
3412 * target - Target path of new symlink.
3414 * cr - credentials of caller.
3415 * flags - case flags
3417 * RETURN: 0 if success
3418 * error code if failure
3421 * dip - ctime|mtime updated
3425 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3426 struct inode **ipp, cred_t *cr, int flags)
3428 znode_t *zp, *dzp = ITOZ(dip);
3431 zfs_sb_t *zsb = ITOZSB(dip);
3433 uint64_t len = strlen(link);
3436 zfs_acl_ids_t acl_ids;
3437 boolean_t fuid_dirtied;
3438 uint64_t txtype = TX_SYMLINK;
3440 ASSERT(S_ISLNK(vap->va_mode));
3446 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3447 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3451 if (flags & FIGNORECASE)
3454 if (len > MAXPATHLEN) {
3456 return (ENAMETOOLONG);
3459 if ((error = zfs_acl_ids_create(dzp, 0,
3460 vap, cr, NULL, &acl_ids)) != 0) {
3468 * Attempt to lock directory; fail if entry already exists.
3470 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3472 zfs_acl_ids_free(&acl_ids);
3477 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3478 zfs_acl_ids_free(&acl_ids);
3479 zfs_dirent_unlock(dl);
3484 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3485 zfs_acl_ids_free(&acl_ids);
3486 zfs_dirent_unlock(dl);
3490 tx = dmu_tx_create(zsb->z_os);
3491 fuid_dirtied = zsb->z_fuid_dirty;
3492 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3493 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3494 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3495 ZFS_SA_BASE_ATTR_SIZE + len);
3496 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3497 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3498 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3499 acl_ids.z_aclp->z_acl_bytes);
3502 zfs_fuid_txhold(zsb, tx);
3503 error = dmu_tx_assign(tx, TXG_NOWAIT);
3505 zfs_dirent_unlock(dl);
3506 if (error == ERESTART) {
3511 zfs_acl_ids_free(&acl_ids);
3518 * Create a new object for the symlink.
3519 * for version 4 ZPL datsets the symlink will be an SA attribute
3521 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3524 zfs_fuid_sync(zsb, tx);
3526 mutex_enter(&zp->z_lock);
3528 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3531 zfs_sa_symlink(zp, link, len, tx);
3532 mutex_exit(&zp->z_lock);
3535 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3536 &zp->z_size, sizeof (zp->z_size), tx);
3538 * Insert the new object into the directory.
3540 (void) zfs_link_create(dl, zp, tx, ZNEW);
3542 if (flags & FIGNORECASE)
3544 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3546 zfs_inode_update(dzp);
3547 zfs_inode_update(zp);
3549 zfs_acl_ids_free(&acl_ids);
3553 zfs_dirent_unlock(dl);
3557 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3558 zil_commit(zilog, 0);
3563 EXPORT_SYMBOL(zfs_symlink);
3566 * Return, in the buffer contained in the provided uio structure,
3567 * the symbolic path referred to by ip.
3569 * IN: ip - inode of symbolic link
3570 * uio - structure to contain the link path.
3571 * cr - credentials of caller.
3573 * RETURN: 0 if success
3574 * error code if failure
3577 * ip - atime updated
3581 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3583 znode_t *zp = ITOZ(ip);
3584 zfs_sb_t *zsb = ITOZSB(ip);
3590 mutex_enter(&zp->z_lock);
3592 error = sa_lookup_uio(zp->z_sa_hdl,
3593 SA_ZPL_SYMLINK(zsb), uio);
3595 error = zfs_sa_readlink(zp, uio);
3596 mutex_exit(&zp->z_lock);
3598 ZFS_ACCESSTIME_STAMP(zsb, zp);
3599 zfs_inode_update(zp);
3603 EXPORT_SYMBOL(zfs_readlink);
3606 * Insert a new entry into directory tdip referencing sip.
3608 * IN: tdip - Directory to contain new entry.
3609 * sip - inode of new entry.
3610 * name - name of new entry.
3611 * cr - credentials of caller.
3613 * RETURN: 0 if success
3614 * error code if failure
3617 * tdip - ctime|mtime updated
3618 * sip - ctime updated
3622 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr)
3624 znode_t *dzp = ITOZ(tdip);
3626 zfs_sb_t *zsb = ITOZSB(tdip);
3635 ASSERT(S_ISDIR(tdip->i_mode));
3642 * POSIX dictates that we return EPERM here.
3643 * Better choices include ENOTSUP or EISDIR.
3645 if (S_ISDIR(sip->i_mode)) {
3650 if (sip->i_sb != tdip->i_sb) {
3658 /* Prevent links to .zfs/shares files */
3660 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3661 &parent, sizeof (uint64_t))) != 0) {
3665 if (parent == zsb->z_shares_dir) {
3670 if (zsb->z_utf8 && u8_validate(name,
3671 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3675 #ifdef HAVE_PN_UTILS
3676 if (flags & FIGNORECASE)
3678 #endif /* HAVE_PN_UTILS */
3681 * We do not support links between attributes and non-attributes
3682 * because of the potential security risk of creating links
3683 * into "normal" file space in order to circumvent restrictions
3684 * imposed in attribute space.
3686 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3691 owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER);
3692 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3697 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3704 * Attempt to lock directory; fail if entry already exists.
3706 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3712 tx = dmu_tx_create(zsb->z_os);
3713 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3714 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3715 zfs_sa_upgrade_txholds(tx, szp);
3716 zfs_sa_upgrade_txholds(tx, dzp);
3717 error = dmu_tx_assign(tx, TXG_NOWAIT);
3719 zfs_dirent_unlock(dl);
3720 if (error == ERESTART) {
3730 error = zfs_link_create(dl, szp, tx, 0);
3733 uint64_t txtype = TX_LINK;
3734 #ifdef HAVE_PN_UTILS
3735 if (flags & FIGNORECASE)
3737 #endif /* HAVE_PN_UTILS */
3738 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3743 zfs_dirent_unlock(dl);
3745 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3746 zil_commit(zilog, 0);
3748 zfs_inode_update(dzp);
3749 zfs_inode_update(szp);
3753 EXPORT_SYMBOL(zfs_link);
3756 zfs_putpage_commit_cb(void *arg, int error)
3758 struct page *pp = arg;
3761 __set_page_dirty_nobuffers(pp);
3763 if (error != ECANCELED)
3769 end_page_writeback(pp);
3773 * Push a page out to disk, once the page is on stable storage the
3774 * registered commit callback will be run as notification of completion.
3776 * IN: ip - page mapped for inode.
3777 * pp - page to push (page is locked)
3778 * wbc - writeback control data
3780 * RETURN: 0 if success
3781 * error code if failure
3784 * ip - ctime|mtime updated
3788 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
3790 znode_t *zp = ITOZ(ip);
3791 zfs_sb_t *zsb = ITOZSB(ip);
3799 uint64_t mtime[2], ctime[2];
3800 sa_bulk_attr_t bulk[3];
3807 ASSERT(PageLocked(pp));
3809 pgoff = page_offset(pp); /* Page byte-offset in file */
3810 offset = i_size_read(ip); /* File length in bytes */
3811 pglen = MIN(PAGE_CACHE_SIZE, /* Page length in bytes */
3812 P2ROUNDUP(offset, PAGE_CACHE_SIZE)-pgoff);
3814 /* Page is beyond end of file */
3815 if (pgoff >= offset) {
3821 /* Truncate page length to end of file */
3822 if (pgoff + pglen > offset)
3823 pglen = offset - pgoff;
3827 * FIXME: Allow mmap writes past its quota. The correct fix
3828 * is to register a page_mkwrite() handler to count the page
3829 * against its quota when it is about to be dirtied.
3831 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
3832 zfs_owner_overquota(zsb, zp, B_TRUE)) {
3837 set_page_writeback(pp);
3840 rl = zfs_range_lock(zp, pgoff, pglen, RL_WRITER);
3841 tx = dmu_tx_create(zsb->z_os);
3843 sync = ((zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) ||
3844 (wbc->sync_mode == WB_SYNC_ALL));
3846 dmu_tx_callback_register(tx, zfs_putpage_commit_cb, pp);
3848 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
3850 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3851 zfs_sa_upgrade_txholds(tx, zp);
3852 err = dmu_tx_assign(tx, TXG_NOWAIT);
3854 if (err == ERESTART)
3857 /* Will call all registered commit callbacks */
3861 * For the synchronous case the commit callback must be
3862 * explicitly called because there is no registered callback.
3865 zfs_putpage_commit_cb(pp, ECANCELED);
3867 zfs_range_unlock(rl);
3873 ASSERT3U(pglen, <=, PAGE_CACHE_SIZE);
3874 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
3877 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3878 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3879 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
3881 /* Preserve the mtime and ctime provided by the inode */
3882 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
3883 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
3884 zp->z_atime_dirty = 0;
3887 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3889 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0);
3892 zfs_range_unlock(rl);
3895 zil_commit(zsb->z_log, zp->z_id);
3896 zfs_putpage_commit_cb(pp, err);
3904 * Update the system attributes when the inode has been dirtied. For the
3905 * moment we're conservative and only update the atime, mtime, and ctime.
3908 zfs_dirty_inode(struct inode *ip, int flags)
3910 znode_t *zp = ITOZ(ip);
3911 zfs_sb_t *zsb = ITOZSB(ip);
3913 uint64_t atime[2], mtime[2], ctime[2];
3914 sa_bulk_attr_t bulk[3];
3921 tx = dmu_tx_create(zsb->z_os);
3923 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3924 zfs_sa_upgrade_txholds(tx, zp);
3926 error = dmu_tx_assign(tx, TXG_WAIT);
3932 mutex_enter(&zp->z_lock);
3933 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
3934 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3935 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3937 /* Preserve the mtime and ctime provided by the inode */
3938 ZFS_TIME_ENCODE(&ip->i_atime, atime);
3939 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
3940 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
3941 zp->z_atime_dirty = 0;
3943 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3944 mutex_exit(&zp->z_lock);
3951 EXPORT_SYMBOL(zfs_dirty_inode);
3955 zfs_inactive(struct inode *ip)
3957 znode_t *zp = ITOZ(ip);
3958 zfs_sb_t *zsb = ITOZSB(ip);
3961 if (zfsctl_is_node(ip)) {
3962 zfsctl_inode_inactive(ip);
3966 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
3967 if (zp->z_sa_hdl == NULL) {
3968 rw_exit(&zsb->z_teardown_inactive_lock);
3972 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3973 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
3975 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3976 zfs_sa_upgrade_txholds(tx, zp);
3977 error = dmu_tx_assign(tx, TXG_WAIT);
3981 mutex_enter(&zp->z_lock);
3982 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
3983 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
3984 zp->z_atime_dirty = 0;
3985 mutex_exit(&zp->z_lock);
3991 rw_exit(&zsb->z_teardown_inactive_lock);
3993 EXPORT_SYMBOL(zfs_inactive);
3996 * Bounds-check the seek operation.
3998 * IN: ip - inode seeking within
3999 * ooff - old file offset
4000 * noffp - pointer to new file offset
4001 * ct - caller context
4003 * RETURN: 0 if success
4004 * EINVAL if new offset invalid
4008 zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
4010 if (S_ISDIR(ip->i_mode))
4012 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4014 EXPORT_SYMBOL(zfs_seek);
4017 * Fill pages with data from the disk.
4020 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
4022 znode_t *zp = ITOZ(ip);
4023 zfs_sb_t *zsb = ITOZSB(ip);
4025 struct page *cur_pp;
4026 u_offset_t io_off, total;
4033 io_len = nr_pages << PAGE_CACHE_SHIFT;
4034 i_size = i_size_read(ip);
4035 io_off = page_offset(pl[0]);
4037 if (io_off + io_len > i_size)
4038 io_len = i_size - io_off;
4041 * Iterate over list of pages and read each page individually.
4045 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4049 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4053 /* convert checksum errors into IO errors */
4058 cur_pp = pl[++page_idx];
4065 * Uses zfs_fillpage to read data from the file and fill the pages.
4067 * IN: ip - inode of file to get data from.
4068 * pl - list of pages to read
4069 * nr_pages - number of pages to read
4071 * RETURN: 0 if success
4072 * error code if failure
4075 * vp - atime updated
4079 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
4081 znode_t *zp = ITOZ(ip);
4082 zfs_sb_t *zsb = ITOZSB(ip);
4091 err = zfs_fillpage(ip, pl, nr_pages);
4094 ZFS_ACCESSTIME_STAMP(zsb, zp);
4099 EXPORT_SYMBOL(zfs_getpage);
4102 * Check ZFS specific permissions to memory map a section of a file.
4104 * IN: ip - inode of the file to mmap
4106 * addrp - start address in memory region
4107 * len - length of memory region
4108 * vm_flags- address flags
4110 * RETURN: 0 if success
4111 * error code if failure
4115 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4116 unsigned long vm_flags)
4118 znode_t *zp = ITOZ(ip);
4119 zfs_sb_t *zsb = ITOZSB(ip);
4124 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
4125 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4130 if ((vm_flags & (VM_READ | VM_EXEC)) &&
4131 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4136 if (off < 0 || len > MAXOFFSET_T - off) {
4144 EXPORT_SYMBOL(zfs_map);
4147 * convoff - converts the given data (start, whence) to the
4151 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4156 if ((lckdat->l_whence == 2) || (whence == 2)) {
4157 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4161 switch (lckdat->l_whence) {
4163 lckdat->l_start += offset;
4166 lckdat->l_start += vap.va_size;
4174 if (lckdat->l_start < 0)
4179 lckdat->l_start -= offset;
4182 lckdat->l_start -= vap.va_size;
4190 lckdat->l_whence = (short)whence;
4195 * Free or allocate space in a file. Currently, this function only
4196 * supports the `F_FREESP' command. However, this command is somewhat
4197 * misnamed, as its functionality includes the ability to allocate as
4198 * well as free space.
4200 * IN: ip - inode of file to free data in.
4201 * cmd - action to take (only F_FREESP supported).
4202 * bfp - section of file to free/alloc.
4203 * flag - current file open mode flags.
4204 * offset - current file offset.
4205 * cr - credentials of caller [UNUSED].
4207 * RETURN: 0 if success
4208 * error code if failure
4211 * ip - ctime|mtime updated
4215 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4216 offset_t offset, cred_t *cr)
4218 znode_t *zp = ITOZ(ip);
4219 zfs_sb_t *zsb = ITOZSB(ip);
4226 if (cmd != F_FREESP) {
4231 if ((error = convoff(ip, bfp, 0, offset))) {
4236 if (bfp->l_len < 0) {
4242 * Permissions aren't checked on Solaris because on this OS
4243 * zfs_space() can only be called with an opened file handle.
4244 * On Linux we can get here through truncate_range() which
4245 * operates directly on inodes, so we need to check access rights.
4247 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4253 len = bfp->l_len; /* 0 means from off to end of file */
4255 error = zfs_freesp(zp, off, len, flag, TRUE);
4260 EXPORT_SYMBOL(zfs_space);
4264 zfs_fid(struct inode *ip, fid_t *fidp)
4266 znode_t *zp = ITOZ(ip);
4267 zfs_sb_t *zsb = ITOZSB(ip);
4270 uint64_t object = zp->z_id;
4277 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4278 &gen64, sizeof (uint64_t))) != 0) {
4283 gen = (uint32_t)gen64;
4285 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4286 if (fidp->fid_len < size) {
4287 fidp->fid_len = size;
4292 zfid = (zfid_short_t *)fidp;
4294 zfid->zf_len = size;
4296 for (i = 0; i < sizeof (zfid->zf_object); i++)
4297 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4299 /* Must have a non-zero generation number to distinguish from .zfs */
4302 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4303 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4305 if (size == LONG_FID_LEN) {
4306 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4309 zlfid = (zfid_long_t *)fidp;
4311 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4312 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4314 /* XXX - this should be the generation number for the objset */
4315 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4316 zlfid->zf_setgen[i] = 0;
4322 EXPORT_SYMBOL(zfs_fid);
4326 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4328 znode_t *zp = ITOZ(ip);
4329 zfs_sb_t *zsb = ITOZSB(ip);
4331 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4335 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4340 EXPORT_SYMBOL(zfs_getsecattr);
4344 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4346 znode_t *zp = ITOZ(ip);
4347 zfs_sb_t *zsb = ITOZSB(ip);
4349 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4350 zilog_t *zilog = zsb->z_log;
4355 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4357 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4358 zil_commit(zilog, 0);
4363 EXPORT_SYMBOL(zfs_setsecattr);
4365 #ifdef HAVE_UIO_ZEROCOPY
4367 * Tunable, both must be a power of 2.
4369 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4370 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4371 * an arcbuf for a partial block read
4373 int zcr_blksz_min = (1 << 10); /* 1K */
4374 int zcr_blksz_max = (1 << 17); /* 128K */
4378 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4380 znode_t *zp = ITOZ(ip);
4381 zfs_sb_t *zsb = ITOZSB(ip);
4382 int max_blksz = zsb->z_max_blksz;
4383 uio_t *uio = &xuio->xu_uio;
4384 ssize_t size = uio->uio_resid;
4385 offset_t offset = uio->uio_loffset;
4390 int preamble, postamble;
4392 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4400 * Loan out an arc_buf for write if write size is bigger than
4401 * max_blksz, and the file's block size is also max_blksz.
4404 if (size < blksz || zp->z_blksz != blksz) {
4409 * Caller requests buffers for write before knowing where the
4410 * write offset might be (e.g. NFS TCP write).
4415 preamble = P2PHASE(offset, blksz);
4417 preamble = blksz - preamble;
4422 postamble = P2PHASE(size, blksz);
4425 fullblk = size / blksz;
4426 (void) dmu_xuio_init(xuio,
4427 (preamble != 0) + fullblk + (postamble != 0));
4430 * Have to fix iov base/len for partial buffers. They
4431 * currently represent full arc_buf's.
4434 /* data begins in the middle of the arc_buf */
4435 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4438 (void) dmu_xuio_add(xuio, abuf,
4439 blksz - preamble, preamble);
4442 for (i = 0; i < fullblk; i++) {
4443 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4446 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4450 /* data ends in the middle of the arc_buf */
4451 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4454 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4459 * Loan out an arc_buf for read if the read size is larger than
4460 * the current file block size. Block alignment is not
4461 * considered. Partial arc_buf will be loaned out for read.
4463 blksz = zp->z_blksz;
4464 if (blksz < zcr_blksz_min)
4465 blksz = zcr_blksz_min;
4466 if (blksz > zcr_blksz_max)
4467 blksz = zcr_blksz_max;
4468 /* avoid potential complexity of dealing with it */
4469 if (blksz > max_blksz) {
4474 maxsize = zp->z_size - uio->uio_loffset;
4488 uio->uio_extflg = UIO_XUIO;
4489 XUIO_XUZC_RW(xuio) = ioflag;
4496 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4500 int ioflag = XUIO_XUZC_RW(xuio);
4502 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4504 i = dmu_xuio_cnt(xuio);
4506 abuf = dmu_xuio_arcbuf(xuio, i);
4508 * if abuf == NULL, it must be a write buffer
4509 * that has been returned in zfs_write().
4512 dmu_return_arcbuf(abuf);
4513 ASSERT(abuf || ioflag == UIO_WRITE);
4516 dmu_xuio_fini(xuio);
4519 #endif /* HAVE_UIO_ZEROCOPY */
4521 #if defined(_KERNEL) && defined(HAVE_SPL)
4522 module_param(zfs_read_chunk_size, long, 0644);
4523 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");