4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 /* Portions Copyright 2007 Jeremy Teo */
26 /* Portions Copyright 2010 Robert Milkowski */
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
36 #include <sys/vfs_opreg.h>
40 #include <sys/taskq.h>
42 #include <sys/vmsystm.h>
43 #include <sys/atomic.h>
45 #include <sys/pathname.h>
46 #include <sys/cmn_err.h>
47 #include <sys/errno.h>
48 #include <sys/unistd.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/fs/zfs.h>
54 #include <sys/dmu_objset.h>
60 #include <sys/dirent.h>
61 #include <sys/policy.h>
62 #include <sys/sunddi.h>
65 #include "fs/fs_subr.h"
66 #include <sys/zfs_fuid.h>
67 #include <sys/zfs_sa.h>
68 #include <sys/zfs_vnops.h>
70 #include <sys/zfs_rlock.h>
71 #include <sys/extdirent.h>
72 #include <sys/kidmap.h>
79 * Each vnode op performs some logical unit of work. To do this, the ZPL must
80 * properly lock its in-core state, create a DMU transaction, do the work,
81 * record this work in the intent log (ZIL), commit the DMU transaction,
82 * and wait for the intent log to commit if it is a synchronous operation.
83 * Moreover, the vnode ops must work in both normal and log replay context.
84 * The ordering of events is important to avoid deadlocks and references
85 * to freed memory. The example below illustrates the following Big Rules:
87 * (1) A check must be made in each zfs thread for a mounted file system.
88 * This is done avoiding races using ZFS_ENTER(zsb).
89 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
90 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
91 * can return EIO from the calling function.
93 * (2) iput() should always be the last thing except for zil_commit()
94 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
95 * First, if it's the last reference, the vnode/znode
96 * can be freed, so the zp may point to freed memory. Second, the last
97 * reference will call zfs_zinactive(), which may induce a lot of work --
98 * pushing cached pages (which acquires range locks) and syncing out
99 * cached atime changes. Third, zfs_zinactive() may require a new tx,
100 * which could deadlock the system if you were already holding one.
101 * If you must call iput() within a tx then use iput_ASYNC().
103 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
104 * as they can span dmu_tx_assign() calls.
106 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
107 * This is critical because we don't want to block while holding locks.
108 * Note, in particular, that if a lock is sometimes acquired before
109 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
110 * use a non-blocking assign can deadlock the system. The scenario:
112 * Thread A has grabbed a lock before calling dmu_tx_assign().
113 * Thread B is in an already-assigned tx, and blocks for this lock.
114 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
115 * forever, because the previous txg can't quiesce until B's tx commits.
117 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
118 * then drop all locks, call dmu_tx_wait(), and try again.
120 * (5) If the operation succeeded, generate the intent log entry for it
121 * before dropping locks. This ensures that the ordering of events
122 * in the intent log matches the order in which they actually occurred.
123 * During ZIL replay the zfs_log_* functions will update the sequence
124 * number to indicate the zil transaction has replayed.
126 * (6) At the end of each vnode op, the DMU tx must always commit,
127 * regardless of whether there were any errors.
129 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
130 * to ensure that synchronous semantics are provided when necessary.
132 * In general, this is how things should be ordered in each vnode op:
134 * ZFS_ENTER(zsb); // exit if unmounted
136 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
137 * rw_enter(...); // grab any other locks you need
138 * tx = dmu_tx_create(...); // get DMU tx
139 * dmu_tx_hold_*(); // hold each object you might modify
140 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
142 * rw_exit(...); // drop locks
143 * zfs_dirent_unlock(dl); // unlock directory entry
144 * iput(...); // release held vnodes
145 * if (error == ERESTART) {
150 * dmu_tx_abort(tx); // abort DMU tx
151 * ZFS_EXIT(zsb); // finished in zfs
152 * return (error); // really out of space
154 * error = do_real_work(); // do whatever this VOP does
156 * zfs_log_*(...); // on success, make ZIL entry
157 * dmu_tx_commit(tx); // commit DMU tx -- error or not
158 * rw_exit(...); // drop locks
159 * zfs_dirent_unlock(dl); // unlock directory entry
160 * iput(...); // release held vnodes
161 * zil_commit(zilog, foid); // synchronous when necessary
162 * ZFS_EXIT(zsb); // finished in zfs
163 * return (error); // done, report error
167 * Virus scanning is unsupported. It would be possible to add a hook
168 * here to performance the required virus scan. This could be done
169 * entirely in the kernel or potentially as an update to invoke a
173 zfs_vscan(struct inode *ip, cred_t *cr, int async)
180 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
182 znode_t *zp = ITOZ(ip);
183 zfs_sb_t *zsb = ITOZSB(ip);
188 /* Honor ZFS_APPENDONLY file attribute */
189 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
190 ((flag & O_APPEND) == 0)) {
195 /* Virus scan eligible files on open */
196 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
197 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
198 if (zfs_vscan(ip, cr, 0) != 0) {
204 /* Keep a count of the synchronous opens in the znode */
206 atomic_inc_32(&zp->z_sync_cnt);
211 EXPORT_SYMBOL(zfs_open);
215 zfs_close(struct inode *ip, int flag, cred_t *cr)
217 znode_t *zp = ITOZ(ip);
218 zfs_sb_t *zsb = ITOZSB(ip);
224 * Zero the synchronous opens in the znode. Under Linux the
225 * zfs_close() hook is not symmetric with zfs_open(), it is
226 * only called once when the last reference is dropped.
231 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
232 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
233 VERIFY(zfs_vscan(ip, cr, 1) == 0);
238 EXPORT_SYMBOL(zfs_close);
242 * When a file is memory mapped, we must keep the IO data synchronized
243 * between the DMU cache and the memory mapped pages. What this means:
245 * On Write: If we find a memory mapped page, we write to *both*
246 * the page and the dmu buffer.
249 update_pages(struct inode *ip, int64_t start, int len,
250 objset_t *os, uint64_t oid)
252 struct address_space *mp = ip->i_mapping;
258 off = start & (PAGE_CACHE_SIZE-1);
259 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
260 nbytes = MIN(PAGE_CACHE_SIZE - off, len);
262 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
264 if (mapping_writably_mapped(mp))
265 flush_dcache_page(pp);
268 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
272 if (mapping_writably_mapped(mp))
273 flush_dcache_page(pp);
275 mark_page_accessed(pp);
279 page_cache_release(pp);
288 * When a file is memory mapped, we must keep the IO data synchronized
289 * between the DMU cache and the memory mapped pages. What this means:
291 * On Read: We "read" preferentially from memory mapped pages,
292 * else we default from the dmu buffer.
294 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
295 * the file is memory mapped.
298 mappedread(struct inode *ip, int nbytes, uio_t *uio)
300 struct address_space *mp = ip->i_mapping;
302 znode_t *zp = ITOZ(ip);
303 objset_t *os = ITOZSB(ip)->z_os;
310 start = uio->uio_loffset;
311 off = start & (PAGE_CACHE_SIZE-1);
312 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
313 bytes = MIN(PAGE_CACHE_SIZE - off, len);
315 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
317 ASSERT(PageUptodate(pp));
320 error = uiomove(pb + off, bytes, UIO_READ, uio);
323 if (mapping_writably_mapped(mp))
324 flush_dcache_page(pp);
326 mark_page_accessed(pp);
328 page_cache_release(pp);
330 error = dmu_read_uio(os, zp->z_id, uio, bytes);
342 unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
345 * Read bytes from specified file into supplied buffer.
347 * IN: ip - inode of file to be read from.
348 * uio - structure supplying read location, range info,
350 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
351 * O_DIRECT flag; used to bypass page cache.
352 * cr - credentials of caller.
354 * OUT: uio - updated offset and range, buffer filled.
356 * RETURN: 0 if success
357 * error code if failure
360 * inode - atime updated if byte count > 0
364 zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
366 znode_t *zp = ITOZ(ip);
367 zfs_sb_t *zsb = ITOZSB(ip);
372 #ifdef HAVE_UIO_ZEROCOPY
374 #endif /* HAVE_UIO_ZEROCOPY */
380 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
386 * Validate file offset
388 if (uio->uio_loffset < (offset_t)0) {
394 * Fasttrack empty reads
396 if (uio->uio_resid == 0) {
401 #ifdef HAVE_MANDLOCKS
403 * Check for mandatory locks
405 if (MANDMODE(zp->z_mode)) {
406 if (error = chklock(ip, FREAD,
407 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
412 #endif /* HAVE_MANDLOCK */
415 * If we're in FRSYNC mode, sync out this znode before reading it.
417 if (ioflag & FRSYNC || zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
418 zil_commit(zsb->z_log, zp->z_id);
421 * Lock the range against changes.
423 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
426 * If we are reading past end-of-file we can skip
427 * to the end; but we might still need to set atime.
429 if (uio->uio_loffset >= zp->z_size) {
434 ASSERT(uio->uio_loffset < zp->z_size);
435 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
437 #ifdef HAVE_UIO_ZEROCOPY
438 if ((uio->uio_extflg == UIO_XUIO) &&
439 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
441 int blksz = zp->z_blksz;
442 uint64_t offset = uio->uio_loffset;
444 xuio = (xuio_t *)uio;
446 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
449 ASSERT(offset + n <= blksz);
452 (void) dmu_xuio_init(xuio, nblk);
454 if (vn_has_cached_data(ip)) {
456 * For simplicity, we always allocate a full buffer
457 * even if we only expect to read a portion of a block.
459 while (--nblk >= 0) {
460 (void) dmu_xuio_add(xuio,
461 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
466 #endif /* HAVE_UIO_ZEROCOPY */
469 nbytes = MIN(n, zfs_read_chunk_size -
470 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
472 if (zp->z_is_mapped && !(ioflag & O_DIRECT))
473 error = mappedread(ip, nbytes, uio);
475 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
478 /* convert checksum errors into IO errors */
487 zfs_range_unlock(rl);
489 ZFS_ACCESSTIME_STAMP(zsb, zp);
490 zfs_inode_update(zp);
494 EXPORT_SYMBOL(zfs_read);
497 * Write the bytes to a file.
499 * IN: ip - inode of file to be written to.
500 * uio - structure supplying write location, range info,
502 * ioflag - FAPPEND flag set if in append mode.
503 * O_DIRECT flag; used to bypass page cache.
504 * cr - credentials of caller.
506 * OUT: uio - updated offset and range.
508 * RETURN: 0 if success
509 * error code if failure
512 * ip - ctime|mtime updated if byte count > 0
517 zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
519 znode_t *zp = ITOZ(ip);
520 rlim64_t limit = uio->uio_limit;
521 ssize_t start_resid = uio->uio_resid;
525 zfs_sb_t *zsb = ZTOZSB(zp);
530 int max_blksz = zsb->z_max_blksz;
533 iovec_t *aiov = NULL;
536 iovec_t *iovp = uio->uio_iov;
539 sa_bulk_attr_t bulk[4];
540 uint64_t mtime[2], ctime[2];
541 ASSERTV(int iovcnt = uio->uio_iovcnt);
544 * Fasttrack empty write
550 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
556 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
557 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
558 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
559 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
563 * If immutable or not appending then return EPERM
565 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
566 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
567 (uio->uio_loffset < zp->z_size))) {
575 * Validate file offset
577 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
583 #ifdef HAVE_MANDLOCKS
585 * Check for mandatory locks before calling zfs_range_lock()
586 * in order to prevent a deadlock with locks set via fcntl().
588 if (MANDMODE((mode_t)zp->z_mode) &&
589 (error = chklock(ip, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
593 #endif /* HAVE_MANDLOCKS */
595 #ifdef HAVE_UIO_ZEROCOPY
597 * Pre-fault the pages to ensure slow (eg NFS) pages
599 * Skip this if uio contains loaned arc_buf.
601 if ((uio->uio_extflg == UIO_XUIO) &&
602 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
603 xuio = (xuio_t *)uio;
605 uio_prefaultpages(MIN(n, max_blksz), uio);
606 #endif /* HAVE_UIO_ZEROCOPY */
609 * If in append mode, set the io offset pointer to eof.
611 if (ioflag & FAPPEND) {
613 * Obtain an appending range lock to guarantee file append
614 * semantics. We reset the write offset once we have the lock.
616 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
618 if (rl->r_len == UINT64_MAX) {
620 * We overlocked the file because this write will cause
621 * the file block size to increase.
622 * Note that zp_size cannot change with this lock held.
626 uio->uio_loffset = woff;
629 * Note that if the file block size will change as a result of
630 * this write, then this range lock will lock the entire file
631 * so that we can re-write the block safely.
633 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
637 zfs_range_unlock(rl);
642 if ((woff + n) > limit || woff > (limit - n))
645 /* Will this write extend the file length? */
646 write_eof = (woff + n > zp->z_size);
648 end_size = MAX(zp->z_size, woff + n);
651 * Write the file in reasonable size chunks. Each chunk is written
652 * in a separate transaction; this keeps the intent log records small
653 * and allows us to do more fine-grained space accounting.
657 woff = uio->uio_loffset;
659 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
660 zfs_owner_overquota(zsb, zp, B_TRUE)) {
662 dmu_return_arcbuf(abuf);
667 if (xuio && abuf == NULL) {
668 ASSERT(i_iov < iovcnt);
670 abuf = dmu_xuio_arcbuf(xuio, i_iov);
671 dmu_xuio_clear(xuio, i_iov);
672 ASSERT((aiov->iov_base == abuf->b_data) ||
673 ((char *)aiov->iov_base - (char *)abuf->b_data +
674 aiov->iov_len == arc_buf_size(abuf)));
676 } else if (abuf == NULL && n >= max_blksz &&
677 woff >= zp->z_size &&
678 P2PHASE(woff, max_blksz) == 0 &&
679 zp->z_blksz == max_blksz) {
681 * This write covers a full block. "Borrow" a buffer
682 * from the dmu so that we can fill it before we enter
683 * a transaction. This avoids the possibility of
684 * holding up the transaction if the data copy hangs
685 * up on a pagefault (e.g., from an NFS server mapping).
689 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
691 ASSERT(abuf != NULL);
692 ASSERT(arc_buf_size(abuf) == max_blksz);
693 if ((error = uiocopy(abuf->b_data, max_blksz,
694 UIO_WRITE, uio, &cbytes))) {
695 dmu_return_arcbuf(abuf);
698 ASSERT(cbytes == max_blksz);
702 * Start a transaction.
704 tx = dmu_tx_create(zsb->z_os);
705 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
706 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
707 zfs_sa_upgrade_txholds(tx, zp);
708 error = dmu_tx_assign(tx, TXG_NOWAIT);
710 if (error == ERESTART) {
717 dmu_return_arcbuf(abuf);
722 * If zfs_range_lock() over-locked we grow the blocksize
723 * and then reduce the lock range. This will only happen
724 * on the first iteration since zfs_range_reduce() will
725 * shrink down r_len to the appropriate size.
727 if (rl->r_len == UINT64_MAX) {
730 if (zp->z_blksz > max_blksz) {
731 ASSERT(!ISP2(zp->z_blksz));
732 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
734 new_blksz = MIN(end_size, max_blksz);
736 zfs_grow_blocksize(zp, new_blksz, tx);
737 zfs_range_reduce(rl, woff, n);
741 * XXX - should we really limit each write to z_max_blksz?
742 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
744 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
747 tx_bytes = uio->uio_resid;
748 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
750 tx_bytes -= uio->uio_resid;
753 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
755 * If this is not a full block write, but we are
756 * extending the file past EOF and this data starts
757 * block-aligned, use assign_arcbuf(). Otherwise,
758 * write via dmu_write().
760 if (tx_bytes < max_blksz && (!write_eof ||
761 aiov->iov_base != abuf->b_data)) {
763 dmu_write(zsb->z_os, zp->z_id, woff,
764 aiov->iov_len, aiov->iov_base, tx);
765 dmu_return_arcbuf(abuf);
766 xuio_stat_wbuf_copied();
768 ASSERT(xuio || tx_bytes == max_blksz);
769 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
772 ASSERT(tx_bytes <= uio->uio_resid);
773 uioskip(uio, tx_bytes);
776 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
777 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
780 * If we made no progress, we're done. If we made even
781 * partial progress, update the znode and ZIL accordingly.
784 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
785 (void *)&zp->z_size, sizeof (uint64_t), tx);
792 * Clear Set-UID/Set-GID bits on successful write if not
793 * privileged and at least one of the excute bits is set.
795 * It would be nice to to this after all writes have
796 * been done, but that would still expose the ISUID/ISGID
797 * to another app after the partial write is committed.
799 * Note: we don't call zfs_fuid_map_id() here because
800 * user 0 is not an ephemeral uid.
802 mutex_enter(&zp->z_acl_lock);
803 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
804 (S_IXUSR >> 6))) != 0 &&
805 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
806 secpolicy_vnode_setid_retain(cr,
807 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
809 zp->z_mode &= ~(S_ISUID | S_ISGID);
810 newmode = zp->z_mode;
811 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
812 (void *)&newmode, sizeof (uint64_t), tx);
814 mutex_exit(&zp->z_acl_lock);
816 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
820 * Update the file size (zp_size) if it has changed;
821 * account for possible concurrent updates.
823 while ((end_size = zp->z_size) < uio->uio_loffset) {
824 (void) atomic_cas_64(&zp->z_size, end_size,
829 * If we are replaying and eof is non zero then force
830 * the file size to the specified eof. Note, there's no
831 * concurrency during replay.
833 if (zsb->z_replay && zsb->z_replay_eof != 0)
834 zp->z_size = zsb->z_replay_eof;
836 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
838 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
843 ASSERT(tx_bytes == nbytes);
847 uio_prefaultpages(MIN(n, max_blksz), uio);
850 zfs_range_unlock(rl);
853 * If we're in replay mode, or we made no progress, return error.
854 * Otherwise, it's at least a partial write, so it's successful.
856 if (zsb->z_replay || uio->uio_resid == start_resid) {
861 if (ioflag & (FSYNC | FDSYNC) ||
862 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
863 zil_commit(zilog, zp->z_id);
865 zfs_inode_update(zp);
869 EXPORT_SYMBOL(zfs_write);
872 iput_async(struct inode *ip, taskq_t *taskq)
874 ASSERT(atomic_read(&ip->i_count) > 0);
875 if (atomic_read(&ip->i_count) == 1)
876 taskq_dispatch(taskq, (task_func_t *)iput, ip, TQ_SLEEP);
882 zfs_get_done(zgd_t *zgd, int error)
884 znode_t *zp = zgd->zgd_private;
885 objset_t *os = ZTOZSB(zp)->z_os;
888 dmu_buf_rele(zgd->zgd_db, zgd);
890 zfs_range_unlock(zgd->zgd_rl);
893 * Release the vnode asynchronously as we currently have the
894 * txg stopped from syncing.
896 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
898 if (error == 0 && zgd->zgd_bp)
899 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
901 kmem_free(zgd, sizeof (zgd_t));
905 static int zil_fault_io = 0;
909 * Get data to generate a TX_WRITE intent log record.
912 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
915 objset_t *os = zsb->z_os;
917 uint64_t object = lr->lr_foid;
918 uint64_t offset = lr->lr_offset;
919 uint64_t size = lr->lr_length;
920 blkptr_t *bp = &lr->lr_blkptr;
929 * Nothing to do if the file has been removed
931 if (zfs_zget(zsb, object, &zp) != 0)
933 if (zp->z_unlinked) {
935 * Release the vnode asynchronously as we currently have the
936 * txg stopped from syncing.
938 iput_async(ZTOI(zp), dsl_pool_iput_taskq(dmu_objset_pool(os)));
942 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
943 zgd->zgd_zilog = zsb->z_log;
944 zgd->zgd_private = zp;
947 * Write records come in two flavors: immediate and indirect.
948 * For small writes it's cheaper to store the data with the
949 * log record (immediate); for large writes it's cheaper to
950 * sync the data and get a pointer to it (indirect) so that
951 * we don't have to write the data twice.
953 if (buf != NULL) { /* immediate write */
954 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
955 /* test for truncation needs to be done while range locked */
956 if (offset >= zp->z_size) {
959 error = dmu_read(os, object, offset, size, buf,
960 DMU_READ_NO_PREFETCH);
962 ASSERT(error == 0 || error == ENOENT);
963 } else { /* indirect write */
965 * Have to lock the whole block to ensure when it's
966 * written out and it's checksum is being calculated
967 * that no one can change the data. We need to re-check
968 * blocksize after we get the lock in case it's changed!
973 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
975 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
977 if (zp->z_blksz == size)
980 zfs_range_unlock(zgd->zgd_rl);
982 /* test for truncation needs to be done while range locked */
983 if (lr->lr_offset >= zp->z_size)
992 error = dmu_buf_hold(os, object, offset, zgd, &db,
993 DMU_READ_NO_PREFETCH);
999 ASSERT(db->db_offset == offset);
1000 ASSERT(db->db_size == size);
1002 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1004 ASSERT(error || lr->lr_length <= zp->z_blksz);
1007 * On success, we need to wait for the write I/O
1008 * initiated by dmu_sync() to complete before we can
1009 * release this dbuf. We will finish everything up
1010 * in the zfs_get_done() callback.
1015 if (error == EALREADY) {
1016 lr->lr_common.lrc_txtype = TX_WRITE2;
1022 zfs_get_done(zgd, error);
1029 zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
1031 znode_t *zp = ITOZ(ip);
1032 zfs_sb_t *zsb = ITOZSB(ip);
1038 if (flag & V_ACE_MASK)
1039 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1041 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1046 EXPORT_SYMBOL(zfs_access);
1049 * Lookup an entry in a directory, or an extended attribute directory.
1050 * If it exists, return a held inode reference for it.
1052 * IN: dip - inode of directory to search.
1053 * nm - name of entry to lookup.
1054 * flags - LOOKUP_XATTR set if looking for an attribute.
1055 * cr - credentials of caller.
1056 * direntflags - directory lookup flags
1057 * realpnp - returned pathname.
1059 * OUT: ipp - inode of located entry, NULL if not found.
1061 * RETURN: 0 if success
1062 * error code if failure
1069 zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1070 cred_t *cr, int *direntflags, pathname_t *realpnp)
1072 znode_t *zdp = ITOZ(dip);
1073 zfs_sb_t *zsb = ITOZSB(dip);
1077 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1079 if (!S_ISDIR(dip->i_mode)) {
1081 } else if (zdp->z_sa_hdl == NULL) {
1085 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1086 error = zfs_fastaccesschk_execute(zdp, cr);
1095 vnode_t *tvp = dnlc_lookup(dvp, nm);
1098 error = zfs_fastaccesschk_execute(zdp, cr);
1103 if (tvp == DNLC_NO_VNODE) {
1108 return (specvp_check(vpp, cr));
1111 #endif /* HAVE_DNLC */
1120 if (flags & LOOKUP_XATTR) {
1122 * If the xattr property is off, refuse the lookup request.
1124 if (!(zsb->z_flags & ZSB_XATTR_USER)) {
1130 * We don't allow recursive attributes..
1131 * Maybe someday we will.
1133 if (zdp->z_pflags & ZFS_XATTR) {
1138 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1144 * Do we have permission to get into attribute directory?
1147 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
1157 if (!S_ISDIR(dip->i_mode)) {
1163 * Check accessibility of directory.
1166 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
1171 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
1172 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1177 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1178 if ((error == 0) && (*ipp))
1179 zfs_inode_update(ITOZ(*ipp));
1184 EXPORT_SYMBOL(zfs_lookup);
1187 * Attempt to create a new entry in a directory. If the entry
1188 * already exists, truncate the file if permissible, else return
1189 * an error. Return the ip of the created or trunc'd file.
1191 * IN: dip - inode of directory to put new file entry in.
1192 * name - name of new file entry.
1193 * vap - attributes of new file.
1194 * excl - flag indicating exclusive or non-exclusive mode.
1195 * mode - mode to open file with.
1196 * cr - credentials of caller.
1197 * flag - large file flag [UNUSED].
1198 * vsecp - ACL to be set
1200 * OUT: ipp - inode of created or trunc'd entry.
1202 * RETURN: 0 if success
1203 * error code if failure
1206 * dip - ctime|mtime updated if new entry created
1207 * ip - ctime|mtime always, atime if new
1212 zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1213 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
1215 znode_t *zp, *dzp = ITOZ(dip);
1216 zfs_sb_t *zsb = ITOZSB(dip);
1224 zfs_acl_ids_t acl_ids;
1225 boolean_t fuid_dirtied;
1226 boolean_t have_acl = B_FALSE;
1229 * If we have an ephemeral id, ACL, or XVATTR then
1230 * make sure file system is at proper version
1236 if (zsb->z_use_fuids == B_FALSE &&
1237 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1245 if (zsb->z_utf8 && u8_validate(name, strlen(name),
1246 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1251 if (vap->va_mask & ATTR_XVATTR) {
1252 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1253 crgetuid(cr), cr, vap->va_mode)) != 0) {
1261 if (*name == '\0') {
1263 * Null component name refers to the directory itself.
1270 /* possible igrab(zp) */
1273 if (flag & FIGNORECASE)
1276 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1280 zfs_acl_ids_free(&acl_ids);
1281 if (strcmp(name, "..") == 0)
1292 * Create a new file object and update the directory
1295 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
1297 zfs_acl_ids_free(&acl_ids);
1302 * We only support the creation of regular files in
1303 * extended attribute directories.
1306 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
1308 zfs_acl_ids_free(&acl_ids);
1313 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1314 cr, vsecp, &acl_ids)) != 0)
1318 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1319 zfs_acl_ids_free(&acl_ids);
1324 tx = dmu_tx_create(os);
1326 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1327 ZFS_SA_BASE_ATTR_SIZE);
1329 fuid_dirtied = zsb->z_fuid_dirty;
1331 zfs_fuid_txhold(zsb, tx);
1332 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1333 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1334 if (!zsb->z_use_sa &&
1335 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1336 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1337 0, acl_ids.z_aclp->z_acl_bytes);
1339 error = dmu_tx_assign(tx, TXG_NOWAIT);
1341 zfs_dirent_unlock(dl);
1342 if (error == ERESTART) {
1347 zfs_acl_ids_free(&acl_ids);
1352 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1355 zfs_fuid_sync(zsb, tx);
1357 (void) zfs_link_create(dl, zp, tx, ZNEW);
1358 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1359 if (flag & FIGNORECASE)
1361 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1362 vsecp, acl_ids.z_fuidp, vap);
1363 zfs_acl_ids_free(&acl_ids);
1366 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1369 zfs_acl_ids_free(&acl_ids);
1373 * A directory entry already exists for this name.
1376 * Can't truncate an existing file if in exclusive mode.
1383 * Can't open a directory for writing.
1385 if (S_ISDIR(ZTOI(zp)->i_mode)) {
1390 * Verify requested access to file.
1392 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1396 mutex_enter(&dzp->z_lock);
1398 mutex_exit(&dzp->z_lock);
1401 * Truncate regular files if requested.
1403 if (S_ISREG(ZTOI(zp)->i_mode) &&
1404 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
1405 /* we can't hold any locks when calling zfs_freesp() */
1406 zfs_dirent_unlock(dl);
1408 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1414 zfs_dirent_unlock(dl);
1420 zfs_inode_update(dzp);
1421 zfs_inode_update(zp);
1425 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1426 zil_commit(zilog, 0);
1431 EXPORT_SYMBOL(zfs_create);
1434 * Remove an entry from a directory.
1436 * IN: dip - inode of directory to remove entry from.
1437 * name - name of entry to remove.
1438 * cr - credentials of caller.
1440 * RETURN: 0 if success
1441 * error code if failure
1445 * ip - ctime (if nlink > 0)
1448 uint64_t null_xattr = 0;
1452 zfs_remove(struct inode *dip, char *name, cred_t *cr)
1454 znode_t *zp, *dzp = ITOZ(dip);
1457 zfs_sb_t *zsb = ITOZSB(dip);
1460 uint64_t xattr_obj_unlinked = 0;
1466 pathname_t *realnmp = NULL;
1467 #ifdef HAVE_PN_UTILS
1469 #endif /* HAVE_PN_UTILS */
1477 #ifdef HAVE_PN_UTILS
1478 if (flags & FIGNORECASE) {
1483 #endif /* HAVE_PN_UTILS */
1489 * Attempt to lock directory; fail if entry doesn't exist.
1491 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1493 #ifdef HAVE_PN_UTILS
1496 #endif /* HAVE_PN_UTILS */
1503 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1508 * Need to use rmdir for removing directories.
1510 if (S_ISDIR(ip->i_mode)) {
1517 dnlc_remove(dvp, realnmp->pn_buf);
1519 dnlc_remove(dvp, name);
1520 #endif /* HAVE_DNLC */
1523 * We never delete the znode and always place it in the unlinked
1524 * set. The dentry cache will always hold the last reference and
1525 * is responsible for safely freeing the znode.
1528 tx = dmu_tx_create(zsb->z_os);
1529 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1530 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1531 zfs_sa_upgrade_txholds(tx, zp);
1532 zfs_sa_upgrade_txholds(tx, dzp);
1534 /* are there any extended attributes? */
1535 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1536 &xattr_obj, sizeof (xattr_obj));
1537 if (error == 0 && xattr_obj) {
1538 error = zfs_zget(zsb, xattr_obj, &xzp);
1539 ASSERT3U(error, ==, 0);
1540 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1541 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1544 /* charge as an update -- would be nice not to charge at all */
1545 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1547 error = dmu_tx_assign(tx, TXG_NOWAIT);
1549 zfs_dirent_unlock(dl);
1553 if (error == ERESTART) {
1558 #ifdef HAVE_PN_UTILS
1561 #endif /* HAVE_PN_UTILS */
1568 * Remove the directory entry.
1570 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1579 * Hold z_lock so that we can make sure that the ACL obj
1580 * hasn't changed. Could have been deleted due to
1583 mutex_enter(&zp->z_lock);
1584 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
1585 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1586 mutex_exit(&zp->z_lock);
1587 zfs_unlinked_add(zp, tx);
1591 #ifdef HAVE_PN_UTILS
1592 if (flags & FIGNORECASE)
1594 #endif /* HAVE_PN_UTILS */
1595 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1599 #ifdef HAVE_PN_UTILS
1602 #endif /* HAVE_PN_UTILS */
1604 zfs_dirent_unlock(dl);
1605 zfs_inode_update(dzp);
1606 zfs_inode_update(zp);
1608 zfs_inode_update(xzp);
1614 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1615 zil_commit(zilog, 0);
1620 EXPORT_SYMBOL(zfs_remove);
1623 * Create a new directory and insert it into dip using the name
1624 * provided. Return a pointer to the inserted directory.
1626 * IN: dip - inode of directory to add subdir to.
1627 * dirname - name of new directory.
1628 * vap - attributes of new directory.
1629 * cr - credentials of caller.
1630 * vsecp - ACL to be set
1632 * OUT: ipp - inode of created directory.
1634 * RETURN: 0 if success
1635 * error code if failure
1638 * dip - ctime|mtime updated
1639 * ipp - ctime|mtime|atime updated
1643 zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1644 cred_t *cr, int flags, vsecattr_t *vsecp)
1646 znode_t *zp, *dzp = ITOZ(dip);
1647 zfs_sb_t *zsb = ITOZSB(dip);
1655 gid_t gid = crgetgid(cr);
1656 zfs_acl_ids_t acl_ids;
1657 boolean_t fuid_dirtied;
1659 ASSERT(S_ISDIR(vap->va_mode));
1662 * If we have an ephemeral id, ACL, or XVATTR then
1663 * make sure file system is at proper version
1667 if (zsb->z_use_fuids == B_FALSE &&
1668 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1675 if (dzp->z_pflags & ZFS_XATTR) {
1680 if (zsb->z_utf8 && u8_validate(dirname,
1681 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1685 if (flags & FIGNORECASE)
1688 if (vap->va_mask & ATTR_XVATTR) {
1689 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1690 crgetuid(cr), cr, vap->va_mode)) != 0) {
1696 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1697 vsecp, &acl_ids)) != 0) {
1702 * First make sure the new directory doesn't exist.
1704 * Existence is checked first to make sure we don't return
1705 * EACCES instead of EEXIST which can cause some applications
1711 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1713 zfs_acl_ids_free(&acl_ids);
1718 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
1719 zfs_acl_ids_free(&acl_ids);
1720 zfs_dirent_unlock(dl);
1725 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1726 zfs_acl_ids_free(&acl_ids);
1727 zfs_dirent_unlock(dl);
1733 * Add a new entry to the directory.
1735 tx = dmu_tx_create(zsb->z_os);
1736 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1737 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1738 fuid_dirtied = zsb->z_fuid_dirty;
1740 zfs_fuid_txhold(zsb, tx);
1741 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1742 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1743 acl_ids.z_aclp->z_acl_bytes);
1746 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1747 ZFS_SA_BASE_ATTR_SIZE);
1749 error = dmu_tx_assign(tx, TXG_NOWAIT);
1751 zfs_dirent_unlock(dl);
1752 if (error == ERESTART) {
1757 zfs_acl_ids_free(&acl_ids);
1766 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1769 zfs_fuid_sync(zsb, tx);
1772 * Now put new name in parent dir.
1774 (void) zfs_link_create(dl, zp, tx, ZNEW);
1778 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1779 if (flags & FIGNORECASE)
1781 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1782 acl_ids.z_fuidp, vap);
1784 zfs_acl_ids_free(&acl_ids);
1788 zfs_dirent_unlock(dl);
1790 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1791 zil_commit(zilog, 0);
1793 zfs_inode_update(dzp);
1794 zfs_inode_update(zp);
1798 EXPORT_SYMBOL(zfs_mkdir);
1801 * Remove a directory subdir entry. If the current working
1802 * directory is the same as the subdir to be removed, the
1805 * IN: dip - inode of directory to remove from.
1806 * name - name of directory to be removed.
1807 * cwd - inode of current working directory.
1808 * cr - credentials of caller.
1809 * flags - case flags
1811 * RETURN: 0 if success
1812 * error code if failure
1815 * dip - ctime|mtime updated
1819 zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1822 znode_t *dzp = ITOZ(dip);
1825 zfs_sb_t *zsb = ITOZSB(dip);
1836 if (flags & FIGNORECASE)
1842 * Attempt to lock directory; fail if entry doesn't exist.
1844 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1852 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
1856 if (!S_ISDIR(ip->i_mode)) {
1867 * Grab a lock on the directory to make sure that noone is
1868 * trying to add (or lookup) entries while we are removing it.
1870 rw_enter(&zp->z_name_lock, RW_WRITER);
1873 * Grab a lock on the parent pointer to make sure we play well
1874 * with the treewalk and directory rename code.
1876 rw_enter(&zp->z_parent_lock, RW_WRITER);
1878 tx = dmu_tx_create(zsb->z_os);
1879 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1880 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1881 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
1882 zfs_sa_upgrade_txholds(tx, zp);
1883 zfs_sa_upgrade_txholds(tx, dzp);
1884 error = dmu_tx_assign(tx, TXG_NOWAIT);
1886 rw_exit(&zp->z_parent_lock);
1887 rw_exit(&zp->z_name_lock);
1888 zfs_dirent_unlock(dl);
1890 if (error == ERESTART) {
1900 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1903 uint64_t txtype = TX_RMDIR;
1904 if (flags & FIGNORECASE)
1906 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
1911 rw_exit(&zp->z_parent_lock);
1912 rw_exit(&zp->z_name_lock);
1914 zfs_dirent_unlock(dl);
1918 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
1919 zil_commit(zilog, 0);
1921 zfs_inode_update(dzp);
1922 zfs_inode_update(zp);
1926 EXPORT_SYMBOL(zfs_rmdir);
1929 * Read as many directory entries as will fit into the provided
1930 * dirent buffer from the given directory cursor position.
1932 * IN: ip - inode of directory to read.
1933 * dirent - buffer for directory entries.
1935 * OUT: dirent - filler buffer of directory entries.
1937 * RETURN: 0 if success
1938 * error code if failure
1941 * ip - atime updated
1943 * Note that the low 4 bits of the cookie returned by zap is always zero.
1944 * This allows us to use the low range for "special" directory entries:
1945 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1946 * we use the offset 2 for the '.zfs' directory.
1950 zfs_readdir(struct inode *ip, void *dirent, filldir_t filldir,
1951 loff_t *pos, cred_t *cr)
1953 znode_t *zp = ITOZ(ip);
1954 zfs_sb_t *zsb = ITOZSB(ip);
1957 zap_attribute_t zap;
1967 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
1968 &parent, sizeof (parent))) != 0)
1972 * Quit if directory has been removed (posix)
1979 prefetch = zp->z_zn_prefetch;
1982 * Initialize the iterator cursor.
1986 * Start iteration from the beginning of the directory.
1988 zap_cursor_init(&zc, os, zp->z_id);
1991 * The offset is a serialized cursor.
1993 zap_cursor_init_serialized(&zc, os, zp->z_id, *pos);
1997 * Transform to file-system independent format
2004 * Special case `.', `..', and `.zfs'.
2007 (void) strcpy(zap.za_name, ".");
2008 zap.za_normalization_conflict = 0;
2010 } else if (*pos == 1) {
2011 (void) strcpy(zap.za_name, "..");
2012 zap.za_normalization_conflict = 0;
2014 } else if (*pos == 2 && zfs_show_ctldir(zp)) {
2015 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2016 zap.za_normalization_conflict = 0;
2017 objnum = ZFSCTL_INO_ROOT;
2022 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2023 if (error == ENOENT)
2029 if (zap.za_integer_length != 8 ||
2030 zap.za_num_integers != 1) {
2031 cmn_err(CE_WARN, "zap_readdir: bad directory "
2032 "entry, obj = %lld, offset = %lld\n",
2033 (u_longlong_t)zp->z_id,
2034 (u_longlong_t)*pos);
2039 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2041 done = filldir(dirent, zap.za_name, strlen(zap.za_name),
2042 zap_cursor_serialize(&zc), objnum, 0);
2047 /* Prefetch znode */
2049 dmu_prefetch(os, objnum, 0, 0);
2053 zap_cursor_advance(&zc);
2054 *pos = zap_cursor_serialize(&zc);
2059 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2062 zap_cursor_fini(&zc);
2063 if (error == ENOENT)
2066 ZFS_ACCESSTIME_STAMP(zsb, zp);
2067 zfs_inode_update(zp);
2074 EXPORT_SYMBOL(zfs_readdir);
2076 ulong_t zfs_fsync_sync_cnt = 4;
2079 zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
2081 znode_t *zp = ITOZ(ip);
2082 zfs_sb_t *zsb = ITOZSB(ip);
2084 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2086 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2089 zil_commit(zsb->z_log, zp->z_id);
2094 EXPORT_SYMBOL(zfs_fsync);
2098 * Get the requested file attributes and place them in the provided
2101 * IN: ip - inode of file.
2102 * vap - va_mask identifies requested attributes.
2103 * If ATTR_XVATTR set, then optional attrs are requested
2104 * flags - ATTR_NOACLCHECK (CIFS server context)
2105 * cr - credentials of caller.
2107 * OUT: vap - attribute values.
2109 * RETURN: 0 (always succeeds)
2113 zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2115 znode_t *zp = ITOZ(ip);
2116 zfs_sb_t *zsb = ITOZSB(ip);
2119 uint64_t mtime[2], ctime[2];
2120 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2121 xoptattr_t *xoap = NULL;
2122 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2123 sa_bulk_attr_t bulk[2];
2129 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2131 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2132 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
2134 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2140 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2141 * Also, if we are the owner don't bother, since owner should
2142 * always be allowed to read basic attributes of file.
2144 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2145 (vap->va_uid != crgetuid(cr))) {
2146 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2154 * Return all attributes. It's cheaper to provide the answer
2155 * than to determine whether we were asked the question.
2158 mutex_enter(&zp->z_lock);
2159 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2160 vap->va_mode = zp->z_mode;
2161 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
2162 vap->va_nodeid = zp->z_id;
2163 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
2164 links = zp->z_links + 1;
2166 links = zp->z_links;
2167 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2168 vap->va_size = i_size_read(ip);
2169 vap->va_rdev = ip->i_rdev;
2170 vap->va_seq = ip->i_generation;
2173 * Add in any requested optional attributes and the create time.
2174 * Also set the corresponding bits in the returned attribute bitmap.
2176 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2177 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2179 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2180 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2183 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2184 xoap->xoa_readonly =
2185 ((zp->z_pflags & ZFS_READONLY) != 0);
2186 XVA_SET_RTN(xvap, XAT_READONLY);
2189 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2191 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2192 XVA_SET_RTN(xvap, XAT_SYSTEM);
2195 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2197 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2198 XVA_SET_RTN(xvap, XAT_HIDDEN);
2201 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2202 xoap->xoa_nounlink =
2203 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2204 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2207 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2208 xoap->xoa_immutable =
2209 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2210 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2213 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2214 xoap->xoa_appendonly =
2215 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2216 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2219 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2221 ((zp->z_pflags & ZFS_NODUMP) != 0);
2222 XVA_SET_RTN(xvap, XAT_NODUMP);
2225 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2227 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2228 XVA_SET_RTN(xvap, XAT_OPAQUE);
2231 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2232 xoap->xoa_av_quarantined =
2233 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2234 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2237 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2238 xoap->xoa_av_modified =
2239 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2240 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2243 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2244 S_ISREG(ip->i_mode)) {
2245 zfs_sa_get_scanstamp(zp, xvap);
2248 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2251 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2252 times, sizeof (times));
2253 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2254 XVA_SET_RTN(xvap, XAT_CREATETIME);
2257 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2258 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2259 XVA_SET_RTN(xvap, XAT_REPARSE);
2261 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2262 xoap->xoa_generation = zp->z_gen;
2263 XVA_SET_RTN(xvap, XAT_GEN);
2266 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2268 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2269 XVA_SET_RTN(xvap, XAT_OFFLINE);
2272 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2274 ((zp->z_pflags & ZFS_SPARSE) != 0);
2275 XVA_SET_RTN(xvap, XAT_SPARSE);
2279 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2280 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2281 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2283 mutex_exit(&zp->z_lock);
2285 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2287 if (zp->z_blksz == 0) {
2289 * Block size hasn't been set; suggest maximal I/O transfers.
2291 vap->va_blksize = zsb->z_max_blksz;
2297 EXPORT_SYMBOL(zfs_getattr);
2300 * Set the file attributes to the values contained in the
2303 * IN: ip - inode of file to be modified.
2304 * vap - new attribute values.
2305 * If ATTR_XVATTR set, then optional attrs are being set
2306 * flags - ATTR_UTIME set if non-default time values provided.
2307 * - ATTR_NOACLCHECK (CIFS context only).
2308 * cr - credentials of caller.
2310 * RETURN: 0 if success
2311 * error code if failure
2314 * ip - ctime updated, mtime updated if size changed.
2318 zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
2320 znode_t *zp = ITOZ(ip);
2321 zfs_sb_t *zsb = ITOZSB(ip);
2325 xvattr_t *tmpxvattr;
2326 uint_t mask = vap->va_mask;
2330 uint64_t new_uid, new_gid;
2332 uint64_t mtime[2], ctime[2];
2334 int need_policy = FALSE;
2336 zfs_fuid_info_t *fuidp = NULL;
2337 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2340 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2341 boolean_t fuid_dirtied = B_FALSE;
2342 sa_bulk_attr_t *bulk, *xattr_bulk;
2343 int count = 0, xattr_count = 0;
2354 * Make sure that if we have ephemeral uid/gid or xvattr specified
2355 * that file system is at proper version level
2358 if (zsb->z_use_fuids == B_FALSE &&
2359 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2360 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2361 (mask & ATTR_XVATTR))) {
2366 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2371 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2377 * If this is an xvattr_t, then get a pointer to the structure of
2378 * optional attributes. If this is NULL, then we have a vattr_t.
2380 xoap = xva_getxoptattr(xvap);
2382 tmpxvattr = kmem_alloc(sizeof(xvattr_t), KM_SLEEP);
2383 xva_init(tmpxvattr);
2385 bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2386 xattr_bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 7, KM_SLEEP);
2389 * Immutable files can only alter immutable bit and atime
2391 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2392 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2393 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2398 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2404 * Verify timestamps doesn't overflow 32 bits.
2405 * ZFS can handle large timestamps, but 32bit syscalls can't
2406 * handle times greater than 2039. This check should be removed
2407 * once large timestamps are fully supported.
2409 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
2410 if (((mask & ATTR_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2411 ((mask & ATTR_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2421 /* Can this be moved to before the top label? */
2422 if (zsb->z_vfs->mnt_flags & MNT_READONLY) {
2428 * First validate permissions
2431 if (mask & ATTR_SIZE) {
2432 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2437 * XXX - Note, we are not providing any open
2438 * mode flags here (like FNDELAY), so we may
2439 * block if there are locks present... this
2440 * should be addressed in openat().
2442 /* XXX - would it be OK to generate a log record here? */
2443 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2447 /* Careful negative Linux return code here */
2448 err = -vmtruncate(ip, vap->va_size);
2453 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2454 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2455 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2456 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2457 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2458 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2459 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2460 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2461 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2465 if (mask & (ATTR_UID|ATTR_GID)) {
2466 int idmask = (mask & (ATTR_UID|ATTR_GID));
2471 * NOTE: even if a new mode is being set,
2472 * we may clear S_ISUID/S_ISGID bits.
2475 if (!(mask & ATTR_MODE))
2476 vap->va_mode = zp->z_mode;
2479 * Take ownership or chgrp to group we are a member of
2482 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
2483 take_group = (mask & ATTR_GID) &&
2484 zfs_groupmember(zsb, vap->va_gid, cr);
2487 * If both ATTR_UID and ATTR_GID are set then take_owner and
2488 * take_group must both be set in order to allow taking
2491 * Otherwise, send the check through secpolicy_vnode_setattr()
2495 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2496 take_owner && take_group) ||
2497 ((idmask == ATTR_UID) && take_owner) ||
2498 ((idmask == ATTR_GID) && take_group)) {
2499 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2500 skipaclchk, cr) == 0) {
2502 * Remove setuid/setgid for non-privileged users
2504 (void) secpolicy_setid_clear(vap, cr);
2505 trim_mask = (mask & (ATTR_UID|ATTR_GID));
2514 mutex_enter(&zp->z_lock);
2515 oldva.va_mode = zp->z_mode;
2516 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2517 if (mask & ATTR_XVATTR) {
2519 * Update xvattr mask to include only those attributes
2520 * that are actually changing.
2522 * the bits will be restored prior to actually setting
2523 * the attributes so the caller thinks they were set.
2525 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2526 if (xoap->xoa_appendonly !=
2527 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2530 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2531 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
2535 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2536 if (xoap->xoa_nounlink !=
2537 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2540 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2541 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
2545 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2546 if (xoap->xoa_immutable !=
2547 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2550 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2551 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
2555 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2556 if (xoap->xoa_nodump !=
2557 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2560 XVA_CLR_REQ(xvap, XAT_NODUMP);
2561 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
2565 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2566 if (xoap->xoa_av_modified !=
2567 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2570 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2571 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
2575 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2576 if ((!S_ISREG(ip->i_mode) &&
2577 xoap->xoa_av_quarantined) ||
2578 xoap->xoa_av_quarantined !=
2579 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2582 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2583 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
2587 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2588 mutex_exit(&zp->z_lock);
2593 if (need_policy == FALSE &&
2594 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2595 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2600 mutex_exit(&zp->z_lock);
2602 if (mask & ATTR_MODE) {
2603 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2604 err = secpolicy_setid_setsticky_clear(ip, vap,
2609 trim_mask |= ATTR_MODE;
2617 * If trim_mask is set then take ownership
2618 * has been granted or write_acl is present and user
2619 * has the ability to modify mode. In that case remove
2620 * UID|GID and or MODE from mask so that
2621 * secpolicy_vnode_setattr() doesn't revoke it.
2625 saved_mask = vap->va_mask;
2626 vap->va_mask &= ~trim_mask;
2628 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
2629 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2634 vap->va_mask |= saved_mask;
2638 * secpolicy_vnode_setattr, or take ownership may have
2641 mask = vap->va_mask;
2643 if ((mask & (ATTR_UID | ATTR_GID))) {
2644 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
2645 &xattr_obj, sizeof (xattr_obj));
2647 if (err == 0 && xattr_obj) {
2648 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
2652 if (mask & ATTR_UID) {
2653 new_uid = zfs_fuid_create(zsb,
2654 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2655 if (new_uid != zp->z_uid &&
2656 zfs_fuid_overquota(zsb, B_FALSE, new_uid)) {
2664 if (mask & ATTR_GID) {
2665 new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
2666 cr, ZFS_GROUP, &fuidp);
2667 if (new_gid != zp->z_gid &&
2668 zfs_fuid_overquota(zsb, B_TRUE, new_gid)) {
2676 tx = dmu_tx_create(zsb->z_os);
2678 if (mask & ATTR_MODE) {
2679 uint64_t pmode = zp->z_mode;
2681 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2683 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
2685 mutex_enter(&zp->z_lock);
2686 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
2688 * Are we upgrading ACL from old V0 format
2691 if (zsb->z_version >= ZPL_VERSION_FUID &&
2692 zfs_znode_acl_version(zp) ==
2693 ZFS_ACL_VERSION_INITIAL) {
2694 dmu_tx_hold_free(tx, acl_obj, 0,
2696 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2697 0, aclp->z_acl_bytes);
2699 dmu_tx_hold_write(tx, acl_obj, 0,
2702 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
2703 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2704 0, aclp->z_acl_bytes);
2706 mutex_exit(&zp->z_lock);
2707 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2709 if ((mask & ATTR_XVATTR) &&
2710 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2711 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2713 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2717 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
2720 fuid_dirtied = zsb->z_fuid_dirty;
2722 zfs_fuid_txhold(zsb, tx);
2724 zfs_sa_upgrade_txholds(tx, zp);
2726 err = dmu_tx_assign(tx, TXG_NOWAIT);
2728 if (err == ERESTART)
2735 * Set each attribute requested.
2736 * We group settings according to the locks they need to acquire.
2738 * Note: you cannot set ctime directly, although it will be
2739 * updated as a side-effect of calling this function.
2743 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2744 mutex_enter(&zp->z_acl_lock);
2745 mutex_enter(&zp->z_lock);
2747 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
2748 &zp->z_pflags, sizeof (zp->z_pflags));
2751 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2752 mutex_enter(&attrzp->z_acl_lock);
2753 mutex_enter(&attrzp->z_lock);
2754 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2755 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
2756 sizeof (attrzp->z_pflags));
2759 if (mask & (ATTR_UID|ATTR_GID)) {
2761 if (mask & ATTR_UID) {
2762 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
2763 &new_uid, sizeof (new_uid));
2764 zp->z_uid = new_uid;
2766 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2767 SA_ZPL_UID(zsb), NULL, &new_uid,
2769 attrzp->z_uid = new_uid;
2773 if (mask & ATTR_GID) {
2774 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
2775 NULL, &new_gid, sizeof (new_gid));
2776 zp->z_gid = new_gid;
2778 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2779 SA_ZPL_GID(zsb), NULL, &new_gid,
2781 attrzp->z_gid = new_gid;
2784 if (!(mask & ATTR_MODE)) {
2785 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
2786 NULL, &new_mode, sizeof (new_mode));
2787 new_mode = zp->z_mode;
2789 err = zfs_acl_chown_setattr(zp);
2792 err = zfs_acl_chown_setattr(attrzp);
2797 if (mask & ATTR_MODE) {
2798 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
2799 &new_mode, sizeof (new_mode));
2800 zp->z_mode = new_mode;
2801 ASSERT3P(aclp, !=, NULL);
2802 err = zfs_aclset_common(zp, aclp, cr, tx);
2803 ASSERT3U(err, ==, 0);
2804 if (zp->z_acl_cached)
2805 zfs_acl_free(zp->z_acl_cached);
2806 zp->z_acl_cached = aclp;
2811 if (mask & ATTR_ATIME) {
2812 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
2813 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
2814 &zp->z_atime, sizeof (zp->z_atime));
2817 if (mask & ATTR_MTIME) {
2818 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
2819 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
2820 mtime, sizeof (mtime));
2823 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2824 if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) {
2825 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
2826 NULL, mtime, sizeof (mtime));
2827 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2828 &ctime, sizeof (ctime));
2829 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
2831 } else if (mask != 0) {
2832 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
2833 &ctime, sizeof (ctime));
2834 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
2837 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
2838 SA_ZPL_CTIME(zsb), NULL,
2839 &ctime, sizeof (ctime));
2840 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2841 mtime, ctime, B_TRUE);
2845 * Do this after setting timestamps to prevent timestamp
2846 * update from toggling bit
2849 if (xoap && (mask & ATTR_XVATTR)) {
2852 * restore trimmed off masks
2853 * so that return masks can be set for caller.
2856 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
2857 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2859 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
2860 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2862 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
2863 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2865 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
2866 XVA_SET_REQ(xvap, XAT_NODUMP);
2868 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
2869 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2871 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
2872 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2875 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2876 ASSERT(S_ISREG(ip->i_mode));
2878 zfs_xvattr_set(zp, xvap, tx);
2882 zfs_fuid_sync(zsb, tx);
2885 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2887 mutex_exit(&zp->z_lock);
2888 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2889 mutex_exit(&zp->z_acl_lock);
2892 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
2893 mutex_exit(&attrzp->z_acl_lock);
2894 mutex_exit(&attrzp->z_lock);
2897 if (err == 0 && attrzp) {
2898 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
2909 zfs_fuid_info_free(fuidp);
2915 if (err == ERESTART)
2918 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
2920 zfs_inode_update(zp);
2924 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
2925 zil_commit(zilog, 0);
2928 kmem_free(xattr_bulk, sizeof(sa_bulk_attr_t) * 7);
2929 kmem_free(bulk, sizeof(sa_bulk_attr_t) * 7);
2930 kmem_free(tmpxvattr, sizeof(xvattr_t));
2934 EXPORT_SYMBOL(zfs_setattr);
2936 typedef struct zfs_zlock {
2937 krwlock_t *zl_rwlock; /* lock we acquired */
2938 znode_t *zl_znode; /* znode we held */
2939 struct zfs_zlock *zl_next; /* next in list */
2943 * Drop locks and release vnodes that were held by zfs_rename_lock().
2946 zfs_rename_unlock(zfs_zlock_t **zlpp)
2950 while ((zl = *zlpp) != NULL) {
2951 if (zl->zl_znode != NULL)
2952 iput(ZTOI(zl->zl_znode));
2953 rw_exit(zl->zl_rwlock);
2954 *zlpp = zl->zl_next;
2955 kmem_free(zl, sizeof (*zl));
2960 * Search back through the directory tree, using the ".." entries.
2961 * Lock each directory in the chain to prevent concurrent renames.
2962 * Fail any attempt to move a directory into one of its own descendants.
2963 * XXX - z_parent_lock can overlap with map or grow locks
2966 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2970 uint64_t rootid = ZTOZSB(zp)->z_root;
2971 uint64_t oidp = zp->z_id;
2972 krwlock_t *rwlp = &szp->z_parent_lock;
2973 krw_t rw = RW_WRITER;
2976 * First pass write-locks szp and compares to zp->z_id.
2977 * Later passes read-lock zp and compare to zp->z_parent.
2980 if (!rw_tryenter(rwlp, rw)) {
2982 * Another thread is renaming in this path.
2983 * Note that if we are a WRITER, we don't have any
2984 * parent_locks held yet.
2986 if (rw == RW_READER && zp->z_id > szp->z_id) {
2988 * Drop our locks and restart
2990 zfs_rename_unlock(&zl);
2994 rwlp = &szp->z_parent_lock;
2999 * Wait for other thread to drop its locks
3005 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3006 zl->zl_rwlock = rwlp;
3007 zl->zl_znode = NULL;
3008 zl->zl_next = *zlpp;
3011 if (oidp == szp->z_id) /* We're a descendant of szp */
3014 if (oidp == rootid) /* We've hit the top */
3017 if (rw == RW_READER) { /* i.e. not the first pass */
3018 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
3023 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
3024 &oidp, sizeof (oidp));
3025 rwlp = &zp->z_parent_lock;
3028 } while (zp->z_id != sdzp->z_id);
3034 * Move an entry from the provided source directory to the target
3035 * directory. Change the entry name as indicated.
3037 * IN: sdip - Source directory containing the "old entry".
3038 * snm - Old entry name.
3039 * tdip - Target directory to contain the "new entry".
3040 * tnm - New entry name.
3041 * cr - credentials of caller.
3042 * flags - case flags
3044 * RETURN: 0 if success
3045 * error code if failure
3048 * sdip,tdip - ctime|mtime updated
3052 zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3053 cred_t *cr, int flags)
3055 znode_t *tdzp, *szp, *tzp;
3056 znode_t *sdzp = ITOZ(sdip);
3057 zfs_sb_t *zsb = ITOZSB(sdip);
3059 zfs_dirlock_t *sdl, *tdl;
3062 int cmp, serr, terr;
3067 ZFS_VERIFY_ZP(sdzp);
3070 if (tdip->i_sb != sdip->i_sb) {
3076 ZFS_VERIFY_ZP(tdzp);
3077 if (zsb->z_utf8 && u8_validate(tnm,
3078 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3083 if (flags & FIGNORECASE)
3092 * This is to prevent the creation of links into attribute space
3093 * by renaming a linked file into/outof an attribute directory.
3094 * See the comment in zfs_link() for why this is considered bad.
3096 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3102 * Lock source and target directory entries. To prevent deadlock,
3103 * a lock ordering must be defined. We lock the directory with
3104 * the smallest object id first, or if it's a tie, the one with
3105 * the lexically first name.
3107 if (sdzp->z_id < tdzp->z_id) {
3109 } else if (sdzp->z_id > tdzp->z_id) {
3113 * First compare the two name arguments without
3114 * considering any case folding.
3116 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
3118 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3119 ASSERT(error == 0 || !zsb->z_utf8);
3122 * POSIX: "If the old argument and the new argument
3123 * both refer to links to the same existing file,
3124 * the rename() function shall return successfully
3125 * and perform no other action."
3131 * If the file system is case-folding, then we may
3132 * have some more checking to do. A case-folding file
3133 * system is either supporting mixed case sensitivity
3134 * access or is completely case-insensitive. Note
3135 * that the file system is always case preserving.
3137 * In mixed sensitivity mode case sensitive behavior
3138 * is the default. FIGNORECASE must be used to
3139 * explicitly request case insensitive behavior.
3141 * If the source and target names provided differ only
3142 * by case (e.g., a request to rename 'tim' to 'Tim'),
3143 * we will treat this as a special case in the
3144 * case-insensitive mode: as long as the source name
3145 * is an exact match, we will allow this to proceed as
3146 * a name-change request.
3148 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3149 (zsb->z_case == ZFS_CASE_MIXED &&
3150 flags & FIGNORECASE)) &&
3151 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
3154 * case preserving rename request, require exact
3163 * If the source and destination directories are the same, we should
3164 * grab the z_name_lock of that directory only once.
3168 rw_enter(&sdzp->z_name_lock, RW_READER);
3172 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3173 ZEXISTS | zflg, NULL, NULL);
3174 terr = zfs_dirent_lock(&tdl,
3175 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3177 terr = zfs_dirent_lock(&tdl,
3178 tdzp, tnm, &tzp, zflg, NULL, NULL);
3179 serr = zfs_dirent_lock(&sdl,
3180 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3186 * Source entry invalid or not there.
3189 zfs_dirent_unlock(tdl);
3195 rw_exit(&sdzp->z_name_lock);
3197 if (strcmp(snm, "..") == 0)
3203 zfs_dirent_unlock(sdl);
3207 rw_exit(&sdzp->z_name_lock);
3209 if (strcmp(tnm, "..") == 0)
3216 * Must have write access at the source to remove the old entry
3217 * and write access at the target to create the new entry.
3218 * Note that if target and source are the same, this can be
3219 * done in a single check.
3222 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
3225 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3227 * Check to make sure rename is valid.
3228 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3230 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
3235 * Does target exist?
3239 * Source and target must be the same type.
3241 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3242 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
3247 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
3253 * POSIX dictates that when the source and target
3254 * entries refer to the same file object, rename
3255 * must do nothing and exit without error.
3257 if (szp->z_id == tzp->z_id) {
3263 tx = dmu_tx_create(zsb->z_os);
3264 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3265 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3266 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3267 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3269 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3270 zfs_sa_upgrade_txholds(tx, tdzp);
3273 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3274 zfs_sa_upgrade_txholds(tx, tzp);
3277 zfs_sa_upgrade_txholds(tx, szp);
3278 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
3279 error = dmu_tx_assign(tx, TXG_NOWAIT);
3282 zfs_rename_unlock(&zl);
3283 zfs_dirent_unlock(sdl);
3284 zfs_dirent_unlock(tdl);
3287 rw_exit(&sdzp->z_name_lock);
3292 if (error == ERESTART) {
3302 if (tzp) /* Attempt to remove the existing target */
3303 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3306 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3308 szp->z_pflags |= ZFS_AV_MODIFIED;
3310 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
3311 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3312 ASSERT3U(error, ==, 0);
3314 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3316 zfs_log_rename(zilog, tx, TX_RENAME |
3317 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3318 sdl->dl_name, tdzp, tdl->dl_name, szp);
3321 * At this point, we have successfully created
3322 * the target name, but have failed to remove
3323 * the source name. Since the create was done
3324 * with the ZRENAMING flag, there are
3325 * complications; for one, the link count is
3326 * wrong. The easiest way to deal with this
3327 * is to remove the newly created target, and
3328 * return the original error. This must
3329 * succeed; fortunately, it is very unlikely to
3330 * fail, since we just created it.
3332 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3333 ZRENAMING, NULL), ==, 0);
3341 zfs_rename_unlock(&zl);
3343 zfs_dirent_unlock(sdl);
3344 zfs_dirent_unlock(tdl);
3346 zfs_inode_update(sdzp);
3348 rw_exit(&sdzp->z_name_lock);
3351 zfs_inode_update(tdzp);
3353 zfs_inode_update(szp);
3356 zfs_inode_update(tzp);
3360 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3361 zil_commit(zilog, 0);
3366 EXPORT_SYMBOL(zfs_rename);
3369 * Insert the indicated symbolic reference entry into the directory.
3371 * IN: dip - Directory to contain new symbolic link.
3372 * link - Name for new symlink entry.
3373 * vap - Attributes of new entry.
3374 * target - Target path of new symlink.
3376 * cr - credentials of caller.
3377 * flags - case flags
3379 * RETURN: 0 if success
3380 * error code if failure
3383 * dip - ctime|mtime updated
3387 zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3388 struct inode **ipp, cred_t *cr, int flags)
3390 znode_t *zp, *dzp = ITOZ(dip);
3393 zfs_sb_t *zsb = ITOZSB(dip);
3395 uint64_t len = strlen(link);
3398 zfs_acl_ids_t acl_ids;
3399 boolean_t fuid_dirtied;
3400 uint64_t txtype = TX_SYMLINK;
3402 ASSERT(S_ISLNK(vap->va_mode));
3408 if (zsb->z_utf8 && u8_validate(name, strlen(name),
3409 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3413 if (flags & FIGNORECASE)
3416 if (len > MAXPATHLEN) {
3418 return (ENAMETOOLONG);
3421 if ((error = zfs_acl_ids_create(dzp, 0,
3422 vap, cr, NULL, &acl_ids)) != 0) {
3430 * Attempt to lock directory; fail if entry already exists.
3432 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3434 zfs_acl_ids_free(&acl_ids);
3439 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3440 zfs_acl_ids_free(&acl_ids);
3441 zfs_dirent_unlock(dl);
3446 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
3447 zfs_acl_ids_free(&acl_ids);
3448 zfs_dirent_unlock(dl);
3452 tx = dmu_tx_create(zsb->z_os);
3453 fuid_dirtied = zsb->z_fuid_dirty;
3454 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3455 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3456 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3457 ZFS_SA_BASE_ATTR_SIZE + len);
3458 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3459 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3460 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3461 acl_ids.z_aclp->z_acl_bytes);
3464 zfs_fuid_txhold(zsb, tx);
3465 error = dmu_tx_assign(tx, TXG_NOWAIT);
3467 zfs_dirent_unlock(dl);
3468 if (error == ERESTART) {
3473 zfs_acl_ids_free(&acl_ids);
3480 * Create a new object for the symlink.
3481 * for version 4 ZPL datsets the symlink will be an SA attribute
3483 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3486 zfs_fuid_sync(zsb, tx);
3488 mutex_enter(&zp->z_lock);
3490 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
3493 zfs_sa_symlink(zp, link, len, tx);
3494 mutex_exit(&zp->z_lock);
3497 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
3498 &zp->z_size, sizeof (zp->z_size), tx);
3500 * Insert the new object into the directory.
3502 (void) zfs_link_create(dl, zp, tx, ZNEW);
3504 if (flags & FIGNORECASE)
3506 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3508 zfs_inode_update(dzp);
3509 zfs_inode_update(zp);
3511 zfs_acl_ids_free(&acl_ids);
3515 zfs_dirent_unlock(dl);
3519 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3520 zil_commit(zilog, 0);
3525 EXPORT_SYMBOL(zfs_symlink);
3528 * Return, in the buffer contained in the provided uio structure,
3529 * the symbolic path referred to by ip.
3531 * IN: ip - inode of symbolic link
3532 * uio - structure to contain the link path.
3533 * cr - credentials of caller.
3535 * RETURN: 0 if success
3536 * error code if failure
3539 * ip - atime updated
3543 zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
3545 znode_t *zp = ITOZ(ip);
3546 zfs_sb_t *zsb = ITOZSB(ip);
3552 mutex_enter(&zp->z_lock);
3554 error = sa_lookup_uio(zp->z_sa_hdl,
3555 SA_ZPL_SYMLINK(zsb), uio);
3557 error = zfs_sa_readlink(zp, uio);
3558 mutex_exit(&zp->z_lock);
3560 ZFS_ACCESSTIME_STAMP(zsb, zp);
3561 zfs_inode_update(zp);
3565 EXPORT_SYMBOL(zfs_readlink);
3568 * Insert a new entry into directory tdip referencing sip.
3570 * IN: tdip - Directory to contain new entry.
3571 * sip - inode of new entry.
3572 * name - name of new entry.
3573 * cr - credentials of caller.
3575 * RETURN: 0 if success
3576 * error code if failure
3579 * tdip - ctime|mtime updated
3580 * sip - ctime updated
3584 zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr)
3586 znode_t *dzp = ITOZ(tdip);
3588 zfs_sb_t *zsb = ITOZSB(tdip);
3597 ASSERT(S_ISDIR(tdip->i_mode));
3604 * POSIX dictates that we return EPERM here.
3605 * Better choices include ENOTSUP or EISDIR.
3607 if (S_ISDIR(sip->i_mode)) {
3612 if (sip->i_sb != tdip->i_sb) {
3620 /* Prevent links to .zfs/shares files */
3622 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
3623 &parent, sizeof (uint64_t))) != 0) {
3627 if (parent == zsb->z_shares_dir) {
3632 if (zsb->z_utf8 && u8_validate(name,
3633 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3637 #ifdef HAVE_PN_UTILS
3638 if (flags & FIGNORECASE)
3640 #endif /* HAVE_PN_UTILS */
3643 * We do not support links between attributes and non-attributes
3644 * because of the potential security risk of creating links
3645 * into "normal" file space in order to circumvent restrictions
3646 * imposed in attribute space.
3648 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3653 owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER);
3654 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3659 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3666 * Attempt to lock directory; fail if entry already exists.
3668 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3674 tx = dmu_tx_create(zsb->z_os);
3675 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3676 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3677 zfs_sa_upgrade_txholds(tx, szp);
3678 zfs_sa_upgrade_txholds(tx, dzp);
3679 error = dmu_tx_assign(tx, TXG_NOWAIT);
3681 zfs_dirent_unlock(dl);
3682 if (error == ERESTART) {
3692 error = zfs_link_create(dl, szp, tx, 0);
3695 uint64_t txtype = TX_LINK;
3696 #ifdef HAVE_PN_UTILS
3697 if (flags & FIGNORECASE)
3699 #endif /* HAVE_PN_UTILS */
3700 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3705 zfs_dirent_unlock(dl);
3707 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
3708 zil_commit(zilog, 0);
3710 zfs_inode_update(dzp);
3711 zfs_inode_update(szp);
3715 EXPORT_SYMBOL(zfs_link);
3719 * zfs_null_putapage() is used when the file system has been force
3720 * unmounted. It just drops the pages.
3724 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3725 size_t *lenp, int flags, cred_t *cr)
3727 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3732 * Push a page out to disk, klustering if possible.
3734 * IN: vp - file to push page to.
3735 * pp - page to push.
3736 * flags - additional flags.
3737 * cr - credentials of caller.
3739 * OUT: offp - start of range pushed.
3740 * lenp - len of range pushed.
3742 * RETURN: 0 if success
3743 * error code if failure
3745 * NOTE: callers must have locked the page to be pushed. On
3746 * exit, the page (and all other pages in the kluster) must be
3751 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3752 size_t *lenp, int flags, cred_t *cr)
3754 znode_t *zp = VTOZ(vp);
3755 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3757 u_offset_t off, koff;
3764 * If our blocksize is bigger than the page size, try to kluster
3765 * multiple pages so that we write a full block (thus avoiding
3766 * a read-modify-write).
3768 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
3769 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3770 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
3771 ASSERT(koff <= zp->z_size);
3772 if (koff + klen > zp->z_size)
3773 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
3774 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
3776 ASSERT3U(btop(len), ==, btopr(len));
3779 * Can't push pages past end-of-file.
3781 if (off >= zp->z_size) {
3782 /* ignore all pages */
3785 } else if (off + len > zp->z_size) {
3786 int npages = btopr(zp->z_size - off);
3789 page_list_break(&pp, &trunc, npages);
3790 /* ignore pages past end of file */
3792 pvn_write_done(trunc, flags);
3793 len = zp->z_size - off;
3796 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
3797 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
3802 tx = dmu_tx_create(zfsvfs->z_os);
3803 dmu_tx_hold_write(tx, zp->z_id, off, len);
3805 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3806 zfs_sa_upgrade_txholds(tx, zp);
3807 err = dmu_tx_assign(tx, TXG_NOWAIT);
3809 if (err == ERESTART) {
3818 if (zp->z_blksz <= PAGESIZE) {
3819 caddr_t va = zfs_map_page(pp, S_READ);
3820 ASSERT3U(len, <=, PAGESIZE);
3821 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
3822 zfs_unmap_page(pp, va);
3824 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
3828 uint64_t mtime[2], ctime[2];
3829 sa_bulk_attr_t bulk[3];
3832 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3834 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3836 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3838 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3840 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
3845 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3855 * Copy the portion of the file indicated from pages into the file.
3856 * The pages are stored in a page list attached to the files vnode.
3858 * IN: vp - vnode of file to push page data to.
3859 * off - position in file to put data.
3860 * len - amount of data to write.
3861 * flags - flags to control the operation.
3862 * cr - credentials of caller.
3863 * ct - caller context.
3865 * RETURN: 0 if success
3866 * error code if failure
3869 * vp - ctime|mtime updated
3873 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
3875 znode_t *zp = VTOZ(vp);
3876 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3888 * Align this request to the file block size in case we kluster.
3889 * XXX - this can result in pretty aggresive locking, which can
3890 * impact simultanious read/write access. One option might be
3891 * to break up long requests (len == 0) into block-by-block
3892 * operations to get narrower locking.
3894 blksz = zp->z_blksz;
3896 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
3899 if (len > 0 && ISP2(blksz))
3900 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
3906 * Search the entire vp list for pages >= io_off.
3908 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
3909 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
3912 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
3914 if (off > zp->z_size) {
3915 /* past end of file */
3916 zfs_range_unlock(rl);
3921 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
3923 for (off = io_off; io_off < off + len; io_off += io_len) {
3924 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
3925 pp = page_lookup(vp, io_off,
3926 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3928 pp = page_lookup_nowait(vp, io_off,
3929 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3932 if (pp != NULL && pvn_getdirty(pp, flags)) {
3936 * Found a dirty page to push
3938 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
3946 zfs_range_unlock(rl);
3947 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3948 zil_commit(zfsvfs->z_log, zp->z_id);
3952 #endif /* HAVE_MMAP */
3956 zfs_inactive(struct inode *ip)
3958 znode_t *zp = ITOZ(ip);
3959 zfs_sb_t *zsb = ITOZSB(ip);
3962 #ifdef HAVE_SNAPSHOT
3963 /* Early return for snapshot inode? */
3964 #endif /* HAVE_SNAPSHOT */
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);
4018 * Pre-filter the generic locking function to trap attempts to place
4019 * a mandatory lock on a memory mapped file.
4022 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4023 flk_callback_t *flk_cbp, cred_t *cr)
4025 znode_t *zp = VTOZ(vp);
4026 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4032 * We are following the UFS semantics with respect to mapcnt
4033 * here: If we see that the file is mapped already, then we will
4034 * return an error, but we don't worry about races between this
4035 * function and zfs_map().
4037 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4042 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4046 * If we can't find a page in the cache, we will create a new page
4047 * and fill it with file data. For efficiency, we may try to fill
4048 * multiple pages at once (klustering) to fill up the supplied page
4049 * list. Note that the pages to be filled are held with an exclusive
4050 * lock to prevent access by other threads while they are being filled.
4053 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4054 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4056 znode_t *zp = VTOZ(vp);
4057 page_t *pp, *cur_pp;
4058 objset_t *os = zp->z_zfsvfs->z_os;
4059 u_offset_t io_off, total;
4063 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4065 * We only have a single page, don't bother klustering
4069 pp = page_create_va(vp, io_off, io_len,
4070 PG_EXCL | PG_WAIT, seg, addr);
4073 * Try to find enough pages to fill the page list
4075 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4076 &io_len, off, plsz, 0);
4080 * The page already exists, nothing to do here.
4087 * Fill the pages in the kluster.
4090 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4093 ASSERT3U(io_off, ==, cur_pp->p_offset);
4094 va = zfs_map_page(cur_pp, S_WRITE);
4095 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4097 zfs_unmap_page(cur_pp, va);
4099 /* On error, toss the entire kluster */
4100 pvn_read_done(pp, B_ERROR);
4101 /* convert checksum errors into IO errors */
4106 cur_pp = cur_pp->p_next;
4110 * Fill in the page list array from the kluster starting
4111 * from the desired offset `off'.
4112 * NOTE: the page list will always be null terminated.
4114 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4115 ASSERT(pl == NULL || (*pl)->p_offset == off);
4121 * Return pointers to the pages for the file region [off, off + len]
4122 * in the pl array. If plsz is greater than len, this function may
4123 * also return page pointers from after the specified region
4124 * (i.e. the region [off, off + plsz]). These additional pages are
4125 * only returned if they are already in the cache, or were created as
4126 * part of a klustered read.
4128 * IN: vp - vnode of file to get data from.
4129 * off - position in file to get data from.
4130 * len - amount of data to retrieve.
4131 * plsz - length of provided page list.
4132 * seg - segment to obtain pages for.
4133 * addr - virtual address of fault.
4134 * rw - mode of created pages.
4135 * cr - credentials of caller.
4136 * ct - caller context.
4138 * OUT: protp - protection mode of created pages.
4139 * pl - list of pages created.
4141 * RETURN: 0 if success
4142 * error code if failure
4145 * vp - atime updated
4149 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4150 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4151 enum seg_rw rw, cred_t *cr)
4153 znode_t *zp = VTOZ(vp);
4154 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4158 /* we do our own caching, faultahead is unnecessary */
4161 else if (len > plsz)
4164 len = P2ROUNDUP(len, PAGESIZE);
4165 ASSERT(plsz >= len);
4174 * Loop through the requested range [off, off + len) looking
4175 * for pages. If we don't find a page, we will need to create
4176 * a new page and fill it with data from the file.
4179 if (*pl = page_lookup(vp, off, SE_SHARED))
4181 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4184 ASSERT3U((*pl)->p_offset, ==, off);
4188 ASSERT3U(len, >=, PAGESIZE);
4191 ASSERT3U(plsz, >=, PAGESIZE);
4198 * Fill out the page array with any pages already in the cache.
4201 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4208 * Release any pages we have previously locked.
4213 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4223 * Request a memory map for a section of a file. This code interacts
4224 * with common code and the VM system as follows:
4226 * common code calls mmap(), which ends up in smmap_common()
4228 * this calls VOP_MAP(), which takes you into (say) zfs
4230 * zfs_map() calls as_map(), passing segvn_create() as the callback
4232 * segvn_create() creates the new segment and calls VOP_ADDMAP()
4234 * zfs_addmap() updates z_mapcnt
4238 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4239 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
4241 znode_t *zp = VTOZ(vp);
4242 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4243 segvn_crargs_t vn_a;
4249 if ((prot & PROT_WRITE) && (zp->z_pflags &
4250 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4255 if ((prot & (PROT_READ | PROT_EXEC)) &&
4256 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4261 if (vp->v_flag & VNOMAP) {
4266 if (off < 0 || len > MAXOFFSET_T - off) {
4271 if (vp->v_type != VREG) {
4277 * If file is locked, disallow mapping.
4279 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4285 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4293 vn_a.offset = (u_offset_t)off;
4294 vn_a.type = flags & MAP_TYPE;
4296 vn_a.maxprot = maxprot;
4299 vn_a.flags = flags & ~MAP_TYPE;
4301 vn_a.lgrp_mem_policy_flags = 0;
4303 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4312 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4313 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
4315 uint64_t pages = btopr(len);
4317 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4322 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4323 * more accurate mtime for the associated file. Since we don't have a way of
4324 * detecting when the data was actually modified, we have to resort to
4325 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4326 * last page is pushed. The problem occurs when the msync() call is omitted,
4327 * which by far the most common case:
4335 * putpage() via fsflush
4337 * If we wait until fsflush to come along, we can have a modification time that
4338 * is some arbitrary point in the future. In order to prevent this in the
4339 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4344 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4345 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
4347 uint64_t pages = btopr(len);
4349 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4350 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4352 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4353 vn_has_cached_data(vp))
4354 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
4358 #endif /* HAVE_MMAP */
4361 * convoff - converts the given data (start, whence) to the
4365 convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4370 if ((lckdat->l_whence == 2) || (whence == 2)) {
4371 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
4375 switch (lckdat->l_whence) {
4377 lckdat->l_start += offset;
4380 lckdat->l_start += vap.va_size;
4388 if (lckdat->l_start < 0)
4393 lckdat->l_start -= offset;
4396 lckdat->l_start -= vap.va_size;
4404 lckdat->l_whence = (short)whence;
4409 * Free or allocate space in a file. Currently, this function only
4410 * supports the `F_FREESP' command. However, this command is somewhat
4411 * misnamed, as its functionality includes the ability to allocate as
4412 * well as free space.
4414 * IN: ip - inode of file to free data in.
4415 * cmd - action to take (only F_FREESP supported).
4416 * bfp - section of file to free/alloc.
4417 * flag - current file open mode flags.
4418 * offset - current file offset.
4419 * cr - credentials of caller [UNUSED].
4421 * RETURN: 0 if success
4422 * error code if failure
4425 * ip - ctime|mtime updated
4429 zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4430 offset_t offset, cred_t *cr)
4432 znode_t *zp = ITOZ(ip);
4433 zfs_sb_t *zsb = ITOZSB(ip);
4440 if (cmd != F_FREESP) {
4445 if ((error = convoff(ip, bfp, 0, offset))) {
4450 if (bfp->l_len < 0) {
4456 len = bfp->l_len; /* 0 means from off to end of file */
4458 error = zfs_freesp(zp, off, len, flag, TRUE);
4463 EXPORT_SYMBOL(zfs_space);
4467 zfs_fid(struct inode *ip, fid_t *fidp)
4469 znode_t *zp = ITOZ(ip);
4470 zfs_sb_t *zsb = ITOZSB(ip);
4473 uint64_t object = zp->z_id;
4480 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
4481 &gen64, sizeof (uint64_t))) != 0) {
4486 gen = (uint32_t)gen64;
4488 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
4489 if (fidp->fid_len < size) {
4490 fidp->fid_len = size;
4495 zfid = (zfid_short_t *)fidp;
4497 zfid->zf_len = size;
4499 for (i = 0; i < sizeof (zfid->zf_object); i++)
4500 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4502 /* Must have a non-zero generation number to distinguish from .zfs */
4505 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4506 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4508 if (size == LONG_FID_LEN) {
4509 uint64_t objsetid = dmu_objset_id(zsb->z_os);
4512 zlfid = (zfid_long_t *)fidp;
4514 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4515 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4517 /* XXX - this should be the generation number for the objset */
4518 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4519 zlfid->zf_setgen[i] = 0;
4525 EXPORT_SYMBOL(zfs_fid);
4529 zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4531 znode_t *zp = ITOZ(ip);
4532 zfs_sb_t *zsb = ITOZSB(ip);
4534 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4538 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4543 EXPORT_SYMBOL(zfs_getsecattr);
4547 zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
4549 znode_t *zp = ITOZ(ip);
4550 zfs_sb_t *zsb = ITOZSB(ip);
4552 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4553 zilog_t *zilog = zsb->z_log;
4558 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4560 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
4561 zil_commit(zilog, 0);
4566 EXPORT_SYMBOL(zfs_setsecattr);
4568 #ifdef HAVE_UIO_ZEROCOPY
4570 * Tunable, both must be a power of 2.
4572 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4573 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4574 * an arcbuf for a partial block read
4576 int zcr_blksz_min = (1 << 10); /* 1K */
4577 int zcr_blksz_max = (1 << 17); /* 128K */
4581 zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
4583 znode_t *zp = ITOZ(ip);
4584 zfs_sb_t *zsb = ITOZSB(ip);
4585 int max_blksz = zsb->z_max_blksz;
4586 uio_t *uio = &xuio->xu_uio;
4587 ssize_t size = uio->uio_resid;
4588 offset_t offset = uio->uio_loffset;
4593 int preamble, postamble;
4595 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4603 * Loan out an arc_buf for write if write size is bigger than
4604 * max_blksz, and the file's block size is also max_blksz.
4607 if (size < blksz || zp->z_blksz != blksz) {
4612 * Caller requests buffers for write before knowing where the
4613 * write offset might be (e.g. NFS TCP write).
4618 preamble = P2PHASE(offset, blksz);
4620 preamble = blksz - preamble;
4625 postamble = P2PHASE(size, blksz);
4628 fullblk = size / blksz;
4629 (void) dmu_xuio_init(xuio,
4630 (preamble != 0) + fullblk + (postamble != 0));
4633 * Have to fix iov base/len for partial buffers. They
4634 * currently represent full arc_buf's.
4637 /* data begins in the middle of the arc_buf */
4638 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4641 (void) dmu_xuio_add(xuio, abuf,
4642 blksz - preamble, preamble);
4645 for (i = 0; i < fullblk; i++) {
4646 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4649 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4653 /* data ends in the middle of the arc_buf */
4654 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4657 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4662 * Loan out an arc_buf for read if the read size is larger than
4663 * the current file block size. Block alignment is not
4664 * considered. Partial arc_buf will be loaned out for read.
4666 blksz = zp->z_blksz;
4667 if (blksz < zcr_blksz_min)
4668 blksz = zcr_blksz_min;
4669 if (blksz > zcr_blksz_max)
4670 blksz = zcr_blksz_max;
4671 /* avoid potential complexity of dealing with it */
4672 if (blksz > max_blksz) {
4677 maxsize = zp->z_size - uio->uio_loffset;
4691 uio->uio_extflg = UIO_XUIO;
4692 XUIO_XUZC_RW(xuio) = ioflag;
4699 zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
4703 int ioflag = XUIO_XUZC_RW(xuio);
4705 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4707 i = dmu_xuio_cnt(xuio);
4709 abuf = dmu_xuio_arcbuf(xuio, i);
4711 * if abuf == NULL, it must be a write buffer
4712 * that has been returned in zfs_write().
4715 dmu_return_arcbuf(abuf);
4716 ASSERT(abuf || ioflag == UIO_WRITE);
4719 dmu_xuio_fini(xuio);
4722 #endif /* HAVE_UIO_ZEROCOPY */
4724 #if defined(_KERNEL) && defined(HAVE_SPL)
4725 module_param(zfs_read_chunk_size, long, 0644);
4726 MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");