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
168 * When a file is memory mapped, we must keep the IO data synchronized
169 * between the DMU cache and the memory mapped pages. What this means:
171 * On Write: If we find a memory mapped page, we write to *both*
172 * the page and the dmu buffer.
175 update_pages(struct inode
*ip
, int64_t start
, int len
,
176 objset_t
*os
, uint64_t oid
)
178 struct address_space
*mp
= ip
->i_mapping
;
184 off
= start
& (PAGE_CACHE_SIZE
-1);
185 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
186 nbytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
188 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
190 if (mapping_writably_mapped(mp
))
191 flush_dcache_page(pp
);
194 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
198 if (mapping_writably_mapped(mp
))
199 flush_dcache_page(pp
);
201 mark_page_accessed(pp
);
205 page_cache_release(pp
);
214 * When a file is memory mapped, we must keep the IO data synchronized
215 * between the DMU cache and the memory mapped pages. What this means:
217 * On Read: We "read" preferentially from memory mapped pages,
218 * else we default from the dmu buffer.
220 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
221 * the file is memory mapped.
224 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
226 struct address_space
*mp
= ip
->i_mapping
;
228 znode_t
*zp
= ITOZ(ip
);
229 objset_t
*os
= ITOZSB(ip
)->z_os
;
236 start
= uio
->uio_loffset
;
237 off
= start
& (PAGE_CACHE_SIZE
-1);
238 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
239 bytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
241 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
243 ASSERT(PageUptodate(pp
));
246 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
249 if (mapping_writably_mapped(mp
))
250 flush_dcache_page(pp
);
252 mark_page_accessed(pp
);
254 page_cache_release(pp
);
256 error
= dmu_read_uio(os
, zp
->z_id
, uio
, bytes
);
268 offset_t zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
271 * Read bytes from specified file into supplied buffer.
273 * IN: ip - inode of file to be read from.
274 * uio - structure supplying read location, range info,
276 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
277 * O_DIRECT flag; used to bypass page cache.
278 * cr - credentials of caller.
280 * OUT: uio - updated offset and range, buffer filled.
282 * RETURN: 0 if success
283 * error code if failure
286 * inode - atime updated if byte count > 0
290 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
292 znode_t
*zp
= ITOZ(ip
);
293 zfs_sb_t
*zsb
= ITOZSB(ip
);
298 #ifdef HAVE_UIO_ZEROCOPY
300 #endif /* HAVE_UIO_ZEROCOPY */
306 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
312 * Validate file offset
314 if (uio
->uio_loffset
< (offset_t
)0) {
320 * Fasttrack empty reads
322 if (uio
->uio_resid
== 0) {
327 #ifdef HAVE_MANDLOCKS
329 * Check for mandatory locks
331 if (MANDMODE(zp
->z_mode
)) {
332 if (error
= chklock(ip
, FREAD
,
333 uio
->uio_loffset
, uio
->uio_resid
, uio
->uio_fmode
, ct
)) {
338 #endif /* HAVE_MANDLOCK */
341 * If we're in FRSYNC mode, sync out this znode before reading it.
343 if (ioflag
& FRSYNC
|| zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
344 zil_commit(zsb
->z_log
, zp
->z_id
);
347 * Lock the range against changes.
349 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
352 * If we are reading past end-of-file we can skip
353 * to the end; but we might still need to set atime.
355 if (uio
->uio_loffset
>= zp
->z_size
) {
360 ASSERT(uio
->uio_loffset
< zp
->z_size
);
361 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
363 #ifdef HAVE_UIO_ZEROCOPY
364 if ((uio
->uio_extflg
== UIO_XUIO
) &&
365 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
367 int blksz
= zp
->z_blksz
;
368 uint64_t offset
= uio
->uio_loffset
;
370 xuio
= (xuio_t
*)uio
;
372 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
375 ASSERT(offset
+ n
<= blksz
);
378 (void) dmu_xuio_init(xuio
, nblk
);
380 if (vn_has_cached_data(ip
)) {
382 * For simplicity, we always allocate a full buffer
383 * even if we only expect to read a portion of a block.
385 while (--nblk
>= 0) {
386 (void) dmu_xuio_add(xuio
,
387 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
392 #endif /* HAVE_UIO_ZEROCOPY */
395 nbytes
= MIN(n
, zfs_read_chunk_size
-
396 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
398 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
399 error
= mappedread(ip
, nbytes
, uio
);
401 error
= dmu_read_uio(os
, zp
->z_id
, uio
, nbytes
);
404 /* convert checksum errors into IO errors */
413 zfs_range_unlock(rl
);
415 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
416 zfs_inode_update(zp
);
420 EXPORT_SYMBOL(zfs_read
);
423 * Write the bytes to a file.
425 * IN: ip - inode of file to be written to.
426 * uio - structure supplying write location, range info,
428 * ioflag - FAPPEND flag set if in append mode.
429 * O_DIRECT flag; used to bypass page cache.
430 * cr - credentials of caller.
432 * OUT: uio - updated offset and range.
434 * RETURN: 0 if success
435 * error code if failure
438 * ip - ctime|mtime updated if byte count > 0
443 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
445 znode_t
*zp
= ITOZ(ip
);
446 rlim64_t limit
= uio
->uio_limit
;
447 ssize_t start_resid
= uio
->uio_resid
;
451 zfs_sb_t
*zsb
= ZTOZSB(zp
);
456 int max_blksz
= zsb
->z_max_blksz
;
459 iovec_t
*aiov
= NULL
;
462 iovec_t
*iovp
= uio
->uio_iov
;
465 sa_bulk_attr_t bulk
[4];
466 uint64_t mtime
[2], ctime
[2];
467 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
470 * Fasttrack empty write
476 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
482 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
483 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
484 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
485 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
489 * If immutable or not appending then return EPERM
491 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
492 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
493 (uio
->uio_loffset
< zp
->z_size
))) {
501 * Validate file offset
503 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
509 #ifdef HAVE_MANDLOCKS
511 * Check for mandatory locks before calling zfs_range_lock()
512 * in order to prevent a deadlock with locks set via fcntl().
514 if (MANDMODE((mode_t
)zp
->z_mode
) &&
515 (error
= chklock(ip
, FWRITE
, woff
, n
, uio
->uio_fmode
, ct
)) != 0) {
519 #endif /* HAVE_MANDLOCKS */
521 #ifdef HAVE_UIO_ZEROCOPY
523 * Pre-fault the pages to ensure slow (eg NFS) pages
525 * Skip this if uio contains loaned arc_buf.
527 if ((uio
->uio_extflg
== UIO_XUIO
) &&
528 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
529 xuio
= (xuio_t
*)uio
;
531 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
532 #endif /* HAVE_UIO_ZEROCOPY */
535 * If in append mode, set the io offset pointer to eof.
537 if (ioflag
& FAPPEND
) {
539 * Obtain an appending range lock to guarantee file append
540 * semantics. We reset the write offset once we have the lock.
542 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
544 if (rl
->r_len
== UINT64_MAX
) {
546 * We overlocked the file because this write will cause
547 * the file block size to increase.
548 * Note that zp_size cannot change with this lock held.
552 uio
->uio_loffset
= woff
;
555 * Note that if the file block size will change as a result of
556 * this write, then this range lock will lock the entire file
557 * so that we can re-write the block safely.
559 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
563 zfs_range_unlock(rl
);
568 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
571 /* Will this write extend the file length? */
572 write_eof
= (woff
+ n
> zp
->z_size
);
574 end_size
= MAX(zp
->z_size
, woff
+ n
);
577 * Write the file in reasonable size chunks. Each chunk is written
578 * in a separate transaction; this keeps the intent log records small
579 * and allows us to do more fine-grained space accounting.
583 woff
= uio
->uio_loffset
;
585 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
586 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
588 dmu_return_arcbuf(abuf
);
593 if (xuio
&& abuf
== NULL
) {
594 ASSERT(i_iov
< iovcnt
);
596 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
597 dmu_xuio_clear(xuio
, i_iov
);
598 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
599 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
600 aiov
->iov_len
== arc_buf_size(abuf
)));
602 } else if (abuf
== NULL
&& n
>= max_blksz
&&
603 woff
>= zp
->z_size
&&
604 P2PHASE(woff
, max_blksz
) == 0 &&
605 zp
->z_blksz
== max_blksz
) {
607 * This write covers a full block. "Borrow" a buffer
608 * from the dmu so that we can fill it before we enter
609 * a transaction. This avoids the possibility of
610 * holding up the transaction if the data copy hangs
611 * up on a pagefault (e.g., from an NFS server mapping).
615 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
617 ASSERT(abuf
!= NULL
);
618 ASSERT(arc_buf_size(abuf
) == max_blksz
);
619 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
620 UIO_WRITE
, uio
, &cbytes
))) {
621 dmu_return_arcbuf(abuf
);
624 ASSERT(cbytes
== max_blksz
);
628 * Start a transaction.
630 tx
= dmu_tx_create(zsb
->z_os
);
631 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
632 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
633 zfs_sa_upgrade_txholds(tx
, zp
);
634 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
636 if (error
== ERESTART
) {
643 dmu_return_arcbuf(abuf
);
648 * If zfs_range_lock() over-locked we grow the blocksize
649 * and then reduce the lock range. This will only happen
650 * on the first iteration since zfs_range_reduce() will
651 * shrink down r_len to the appropriate size.
653 if (rl
->r_len
== UINT64_MAX
) {
656 if (zp
->z_blksz
> max_blksz
) {
657 ASSERT(!ISP2(zp
->z_blksz
));
658 new_blksz
= MIN(end_size
, SPA_MAXBLOCKSIZE
);
660 new_blksz
= MIN(end_size
, max_blksz
);
662 zfs_grow_blocksize(zp
, new_blksz
, tx
);
663 zfs_range_reduce(rl
, woff
, n
);
667 * XXX - should we really limit each write to z_max_blksz?
668 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
670 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
673 tx_bytes
= uio
->uio_resid
;
674 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
676 tx_bytes
-= uio
->uio_resid
;
679 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
681 * If this is not a full block write, but we are
682 * extending the file past EOF and this data starts
683 * block-aligned, use assign_arcbuf(). Otherwise,
684 * write via dmu_write().
686 if (tx_bytes
< max_blksz
&& (!write_eof
||
687 aiov
->iov_base
!= abuf
->b_data
)) {
689 dmu_write(zsb
->z_os
, zp
->z_id
, woff
,
690 aiov
->iov_len
, aiov
->iov_base
, tx
);
691 dmu_return_arcbuf(abuf
);
692 xuio_stat_wbuf_copied();
694 ASSERT(xuio
|| tx_bytes
== max_blksz
);
695 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
698 ASSERT(tx_bytes
<= uio
->uio_resid
);
699 uioskip(uio
, tx_bytes
);
702 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
703 update_pages(ip
, woff
, tx_bytes
, zsb
->z_os
, zp
->z_id
);
706 * If we made no progress, we're done. If we made even
707 * partial progress, update the znode and ZIL accordingly.
710 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
711 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
718 * Clear Set-UID/Set-GID bits on successful write if not
719 * privileged and at least one of the excute bits is set.
721 * It would be nice to to this after all writes have
722 * been done, but that would still expose the ISUID/ISGID
723 * to another app after the partial write is committed.
725 * Note: we don't call zfs_fuid_map_id() here because
726 * user 0 is not an ephemeral uid.
728 mutex_enter(&zp
->z_acl_lock
);
729 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
730 (S_IXUSR
>> 6))) != 0 &&
731 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
732 secpolicy_vnode_setid_retain(cr
,
733 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
735 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
736 newmode
= zp
->z_mode
;
737 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
),
738 (void *)&newmode
, sizeof (uint64_t), tx
);
740 mutex_exit(&zp
->z_acl_lock
);
742 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
746 * Update the file size (zp_size) if it has changed;
747 * account for possible concurrent updates.
749 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
750 (void) atomic_cas_64(&zp
->z_size
, end_size
,
755 * If we are replaying and eof is non zero then force
756 * the file size to the specified eof. Note, there's no
757 * concurrency during replay.
759 if (zsb
->z_replay
&& zsb
->z_replay_eof
!= 0)
760 zp
->z_size
= zsb
->z_replay_eof
;
762 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
764 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
);
769 ASSERT(tx_bytes
== nbytes
);
773 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
776 zfs_range_unlock(rl
);
779 * If we're in replay mode, or we made no progress, return error.
780 * Otherwise, it's at least a partial write, so it's successful.
782 if (zsb
->z_replay
|| uio
->uio_resid
== start_resid
) {
787 if (ioflag
& (FSYNC
| FDSYNC
) ||
788 zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
789 zil_commit(zilog
, zp
->z_id
);
791 zfs_inode_update(zp
);
795 EXPORT_SYMBOL(zfs_write
);
798 iput_async(struct inode
*ip
, taskq_t
*taskq
)
800 ASSERT(atomic_read(&ip
->i_count
) > 0);
801 if (atomic_read(&ip
->i_count
) == 1)
802 taskq_dispatch(taskq
, (task_func_t
*)iput
, ip
, TQ_SLEEP
);
808 zfs_get_done(zgd_t
*zgd
, int error
)
810 znode_t
*zp
= zgd
->zgd_private
;
811 objset_t
*os
= ZTOZSB(zp
)->z_os
;
814 dmu_buf_rele(zgd
->zgd_db
, zgd
);
816 zfs_range_unlock(zgd
->zgd_rl
);
819 * Release the vnode asynchronously as we currently have the
820 * txg stopped from syncing.
822 iput_async(ZTOI(zp
), dsl_pool_iput_taskq(dmu_objset_pool(os
)));
824 if (error
== 0 && zgd
->zgd_bp
)
825 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
827 kmem_free(zgd
, sizeof (zgd_t
));
831 static int zil_fault_io
= 0;
835 * Get data to generate a TX_WRITE intent log record.
838 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
841 objset_t
*os
= zsb
->z_os
;
843 uint64_t object
= lr
->lr_foid
;
844 uint64_t offset
= lr
->lr_offset
;
845 uint64_t size
= lr
->lr_length
;
846 blkptr_t
*bp
= &lr
->lr_blkptr
;
855 * Nothing to do if the file has been removed
857 if (zfs_zget(zsb
, object
, &zp
) != 0)
859 if (zp
->z_unlinked
) {
861 * Release the vnode asynchronously as we currently have the
862 * txg stopped from syncing.
864 iput_async(ZTOI(zp
), dsl_pool_iput_taskq(dmu_objset_pool(os
)));
868 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
869 zgd
->zgd_zilog
= zsb
->z_log
;
870 zgd
->zgd_private
= zp
;
873 * Write records come in two flavors: immediate and indirect.
874 * For small writes it's cheaper to store the data with the
875 * log record (immediate); for large writes it's cheaper to
876 * sync the data and get a pointer to it (indirect) so that
877 * we don't have to write the data twice.
879 if (buf
!= NULL
) { /* immediate write */
880 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
881 /* test for truncation needs to be done while range locked */
882 if (offset
>= zp
->z_size
) {
885 error
= dmu_read(os
, object
, offset
, size
, buf
,
886 DMU_READ_NO_PREFETCH
);
888 ASSERT(error
== 0 || error
== ENOENT
);
889 } else { /* indirect write */
891 * Have to lock the whole block to ensure when it's
892 * written out and it's checksum is being calculated
893 * that no one can change the data. We need to re-check
894 * blocksize after we get the lock in case it's changed!
899 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
901 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
,
903 if (zp
->z_blksz
== size
)
906 zfs_range_unlock(zgd
->zgd_rl
);
908 /* test for truncation needs to be done while range locked */
909 if (lr
->lr_offset
>= zp
->z_size
)
918 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
919 DMU_READ_NO_PREFETCH
);
925 ASSERT(db
->db_offset
== offset
);
926 ASSERT(db
->db_size
== size
);
928 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
930 ASSERT(error
|| lr
->lr_length
<= zp
->z_blksz
);
933 * On success, we need to wait for the write I/O
934 * initiated by dmu_sync() to complete before we can
935 * release this dbuf. We will finish everything up
936 * in the zfs_get_done() callback.
941 if (error
== EALREADY
) {
942 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
948 zfs_get_done(zgd
, error
);
955 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
957 znode_t
*zp
= ITOZ(ip
);
958 zfs_sb_t
*zsb
= ITOZSB(ip
);
964 if (flag
& V_ACE_MASK
)
965 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
967 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
972 EXPORT_SYMBOL(zfs_access
);
975 * Lookup an entry in a directory, or an extended attribute directory.
976 * If it exists, return a held inode reference for it.
978 * IN: dip - inode of directory to search.
979 * nm - name of entry to lookup.
980 * flags - LOOKUP_XATTR set if looking for an attribute.
981 * cr - credentials of caller.
982 * direntflags - directory lookup flags
983 * realpnp - returned pathname.
985 * OUT: ipp - inode of located entry, NULL if not found.
987 * RETURN: 0 if success
988 * error code if failure
995 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
996 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
998 znode_t
*zdp
= ITOZ(dip
);
999 zfs_sb_t
*zsb
= ITOZSB(dip
);
1003 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1005 if (!S_ISDIR(dip
->i_mode
)) {
1007 } else if (zdp
->z_sa_hdl
== NULL
) {
1011 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1012 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1021 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1024 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1029 if (tvp
== DNLC_NO_VNODE
) {
1034 return (specvp_check(vpp
, cr
));
1037 #endif /* HAVE_DNLC */
1046 if (flags
& LOOKUP_XATTR
) {
1048 * If the xattr property is off, refuse the lookup request.
1050 if (!(zsb
->z_flags
& ZSB_XATTR_USER
)) {
1056 * We don't allow recursive attributes..
1057 * Maybe someday we will.
1059 if (zdp
->z_pflags
& ZFS_XATTR
) {
1064 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1070 * Do we have permission to get into attribute directory?
1073 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1083 if (!S_ISDIR(dip
->i_mode
)) {
1089 * Check accessibility of directory.
1092 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1097 if (zsb
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1098 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1103 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1104 if ((error
== 0) && (*ipp
))
1105 zfs_inode_update(ITOZ(*ipp
));
1110 EXPORT_SYMBOL(zfs_lookup
);
1113 * Attempt to create a new entry in a directory. If the entry
1114 * already exists, truncate the file if permissible, else return
1115 * an error. Return the ip of the created or trunc'd file.
1117 * IN: dip - inode of directory to put new file entry in.
1118 * name - name of new file entry.
1119 * vap - attributes of new file.
1120 * excl - flag indicating exclusive or non-exclusive mode.
1121 * mode - mode to open file with.
1122 * cr - credentials of caller.
1123 * flag - large file flag [UNUSED].
1124 * vsecp - ACL to be set
1126 * OUT: ipp - inode of created or trunc'd entry.
1128 * RETURN: 0 if success
1129 * error code if failure
1132 * dip - ctime|mtime updated if new entry created
1133 * ip - ctime|mtime always, atime if new
1138 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1139 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1141 znode_t
*zp
, *dzp
= ITOZ(dip
);
1142 zfs_sb_t
*zsb
= ITOZSB(dip
);
1150 zfs_acl_ids_t acl_ids
;
1151 boolean_t fuid_dirtied
;
1152 boolean_t have_acl
= B_FALSE
;
1155 * If we have an ephemeral id, ACL, or XVATTR then
1156 * make sure file system is at proper version
1162 if (zsb
->z_use_fuids
== B_FALSE
&&
1163 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1171 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
1172 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1178 if (vap
->va_mask
& AT_XVATTR
) {
1179 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1180 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1185 #endif /* HAVE_XVATTR */
1189 if (*name
== '\0') {
1191 * Null component name refers to the directory itself.
1198 /* possible igrab(zp) */
1201 if (flag
& FIGNORECASE
)
1204 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1208 zfs_acl_ids_free(&acl_ids
);
1209 if (strcmp(name
, "..") == 0)
1220 * Create a new file object and update the directory
1223 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1225 zfs_acl_ids_free(&acl_ids
);
1230 * We only support the creation of regular files in
1231 * extended attribute directories.
1234 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1236 zfs_acl_ids_free(&acl_ids
);
1241 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1242 cr
, vsecp
, &acl_ids
)) != 0)
1246 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1247 zfs_acl_ids_free(&acl_ids
);
1252 tx
= dmu_tx_create(os
);
1254 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1255 ZFS_SA_BASE_ATTR_SIZE
);
1257 fuid_dirtied
= zsb
->z_fuid_dirty
;
1259 zfs_fuid_txhold(zsb
, tx
);
1260 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1261 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1262 if (!zsb
->z_use_sa
&&
1263 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1264 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1265 0, acl_ids
.z_aclp
->z_acl_bytes
);
1267 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1269 zfs_dirent_unlock(dl
);
1270 if (error
== ERESTART
) {
1275 zfs_acl_ids_free(&acl_ids
);
1280 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1283 zfs_fuid_sync(zsb
, tx
);
1285 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1286 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1287 if (flag
& FIGNORECASE
)
1289 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1290 vsecp
, acl_ids
.z_fuidp
, vap
);
1291 zfs_acl_ids_free(&acl_ids
);
1294 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1297 zfs_acl_ids_free(&acl_ids
);
1301 * A directory entry already exists for this name.
1304 * Can't truncate an existing file if in exclusive mode.
1311 * Can't open a directory for writing.
1313 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1318 * Verify requested access to file.
1320 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1324 mutex_enter(&dzp
->z_lock
);
1326 mutex_exit(&dzp
->z_lock
);
1329 * Truncate regular files if requested.
1331 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1332 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1333 /* we can't hold any locks when calling zfs_freesp() */
1334 zfs_dirent_unlock(dl
);
1336 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1342 zfs_dirent_unlock(dl
);
1348 zfs_inode_update(dzp
);
1349 zfs_inode_update(zp
);
1353 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1354 zil_commit(zilog
, 0);
1359 EXPORT_SYMBOL(zfs_create
);
1362 * Remove an entry from a directory.
1364 * IN: dip - inode of directory to remove entry from.
1365 * name - name of entry to remove.
1366 * cr - credentials of caller.
1368 * RETURN: 0 if success
1369 * error code if failure
1373 * ip - ctime (if nlink > 0)
1376 uint64_t null_xattr
= 0;
1380 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
)
1382 znode_t
*zp
, *dzp
= ITOZ(dip
);
1385 zfs_sb_t
*zsb
= ITOZSB(dip
);
1388 uint64_t xattr_obj_unlinked
= 0;
1394 pathname_t
*realnmp
= NULL
;
1395 #ifdef HAVE_PN_UTILS
1397 #endif /* HAVE_PN_UTILS */
1405 #ifdef HAVE_PN_UTILS
1406 if (flags
& FIGNORECASE
) {
1411 #endif /* HAVE_PN_UTILS */
1417 * Attempt to lock directory; fail if entry doesn't exist.
1419 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1421 #ifdef HAVE_PN_UTILS
1424 #endif /* HAVE_PN_UTILS */
1431 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1436 * Need to use rmdir for removing directories.
1438 if (S_ISDIR(ip
->i_mode
)) {
1445 dnlc_remove(dvp
, realnmp
->pn_buf
);
1447 dnlc_remove(dvp
, name
);
1448 #endif /* HAVE_DNLC */
1451 * We never delete the znode and always place it in the unlinked
1452 * set. The dentry cache will always hold the last reference and
1453 * is responsible for safely freeing the znode.
1456 tx
= dmu_tx_create(zsb
->z_os
);
1457 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1458 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1459 zfs_sa_upgrade_txholds(tx
, zp
);
1460 zfs_sa_upgrade_txholds(tx
, dzp
);
1462 /* are there any extended attributes? */
1463 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1464 &xattr_obj
, sizeof (xattr_obj
));
1465 if (error
== 0 && xattr_obj
) {
1466 error
= zfs_zget(zsb
, xattr_obj
, &xzp
);
1467 ASSERT3U(error
, ==, 0);
1468 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1469 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1472 /* charge as an update -- would be nice not to charge at all */
1473 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1475 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1477 zfs_dirent_unlock(dl
);
1481 if (error
== ERESTART
) {
1486 #ifdef HAVE_PN_UTILS
1489 #endif /* HAVE_PN_UTILS */
1496 * Remove the directory entry.
1498 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1507 * Hold z_lock so that we can make sure that the ACL obj
1508 * hasn't changed. Could have been deleted due to
1511 mutex_enter(&zp
->z_lock
);
1512 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1513 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1514 mutex_exit(&zp
->z_lock
);
1515 zfs_unlinked_add(zp
, tx
);
1519 #ifdef HAVE_PN_UTILS
1520 if (flags
& FIGNORECASE
)
1522 #endif /* HAVE_PN_UTILS */
1523 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1527 #ifdef HAVE_PN_UTILS
1530 #endif /* HAVE_PN_UTILS */
1532 zfs_dirent_unlock(dl
);
1533 zfs_inode_update(dzp
);
1534 zfs_inode_update(zp
);
1536 zfs_inode_update(xzp
);
1542 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1543 zil_commit(zilog
, 0);
1548 EXPORT_SYMBOL(zfs_remove
);
1551 * Create a new directory and insert it into dip using the name
1552 * provided. Return a pointer to the inserted directory.
1554 * IN: dip - inode of directory to add subdir to.
1555 * dirname - name of new directory.
1556 * vap - attributes of new directory.
1557 * cr - credentials of caller.
1558 * vsecp - ACL to be set
1560 * OUT: ipp - inode of created directory.
1562 * RETURN: 0 if success
1563 * error code if failure
1566 * dip - ctime|mtime updated
1567 * ipp - ctime|mtime|atime updated
1571 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1572 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1574 znode_t
*zp
, *dzp
= ITOZ(dip
);
1575 zfs_sb_t
*zsb
= ITOZSB(dip
);
1583 gid_t gid
= crgetgid(cr
);
1584 zfs_acl_ids_t acl_ids
;
1585 boolean_t fuid_dirtied
;
1587 ASSERT(S_ISDIR(vap
->va_mode
));
1590 * If we have an ephemeral id, ACL, or XVATTR then
1591 * make sure file system is at proper version
1595 if (zsb
->z_use_fuids
== B_FALSE
&&
1596 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1603 if (dzp
->z_pflags
& ZFS_XATTR
) {
1608 if (zsb
->z_utf8
&& u8_validate(dirname
,
1609 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1613 if (flags
& FIGNORECASE
)
1617 if (vap
->va_mask
& AT_XVATTR
) {
1618 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1619 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1624 #endif /* HAVE_XVATTR */
1626 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1627 vsecp
, &acl_ids
)) != 0) {
1632 * First make sure the new directory doesn't exist.
1634 * Existence is checked first to make sure we don't return
1635 * EACCES instead of EEXIST which can cause some applications
1641 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1643 zfs_acl_ids_free(&acl_ids
);
1648 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1649 zfs_acl_ids_free(&acl_ids
);
1650 zfs_dirent_unlock(dl
);
1655 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1656 zfs_acl_ids_free(&acl_ids
);
1657 zfs_dirent_unlock(dl
);
1663 * Add a new entry to the directory.
1665 tx
= dmu_tx_create(zsb
->z_os
);
1666 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1667 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1668 fuid_dirtied
= zsb
->z_fuid_dirty
;
1670 zfs_fuid_txhold(zsb
, tx
);
1671 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1672 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1673 acl_ids
.z_aclp
->z_acl_bytes
);
1676 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1677 ZFS_SA_BASE_ATTR_SIZE
);
1679 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1681 zfs_dirent_unlock(dl
);
1682 if (error
== ERESTART
) {
1687 zfs_acl_ids_free(&acl_ids
);
1696 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1699 zfs_fuid_sync(zsb
, tx
);
1702 * Now put new name in parent dir.
1704 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1708 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1709 if (flags
& FIGNORECASE
)
1711 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1712 acl_ids
.z_fuidp
, vap
);
1714 zfs_acl_ids_free(&acl_ids
);
1718 zfs_dirent_unlock(dl
);
1720 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1721 zil_commit(zilog
, 0);
1723 zfs_inode_update(dzp
);
1724 zfs_inode_update(zp
);
1728 EXPORT_SYMBOL(zfs_mkdir
);
1731 * Remove a directory subdir entry. If the current working
1732 * directory is the same as the subdir to be removed, the
1735 * IN: dip - inode of directory to remove from.
1736 * name - name of directory to be removed.
1737 * cwd - inode of current working directory.
1738 * cr - credentials of caller.
1739 * flags - case flags
1741 * RETURN: 0 if success
1742 * error code if failure
1745 * dip - ctime|mtime updated
1749 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
1752 znode_t
*dzp
= ITOZ(dip
);
1755 zfs_sb_t
*zsb
= ITOZSB(dip
);
1766 if (flags
& FIGNORECASE
)
1772 * Attempt to lock directory; fail if entry doesn't exist.
1774 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1782 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1786 if (!S_ISDIR(ip
->i_mode
)) {
1797 * Grab a lock on the directory to make sure that noone is
1798 * trying to add (or lookup) entries while we are removing it.
1800 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1803 * Grab a lock on the parent pointer to make sure we play well
1804 * with the treewalk and directory rename code.
1806 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1808 tx
= dmu_tx_create(zsb
->z_os
);
1809 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1810 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1811 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1812 zfs_sa_upgrade_txholds(tx
, zp
);
1813 zfs_sa_upgrade_txholds(tx
, dzp
);
1814 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1816 rw_exit(&zp
->z_parent_lock
);
1817 rw_exit(&zp
->z_name_lock
);
1818 zfs_dirent_unlock(dl
);
1820 if (error
== ERESTART
) {
1830 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1833 uint64_t txtype
= TX_RMDIR
;
1834 if (flags
& FIGNORECASE
)
1836 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
1841 rw_exit(&zp
->z_parent_lock
);
1842 rw_exit(&zp
->z_name_lock
);
1844 zfs_dirent_unlock(dl
);
1848 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1849 zil_commit(zilog
, 0);
1851 zfs_inode_update(dzp
);
1852 zfs_inode_update(zp
);
1856 EXPORT_SYMBOL(zfs_rmdir
);
1859 * Read as many directory entries as will fit into the provided
1860 * dirent buffer from the given directory cursor position.
1862 * IN: ip - inode of directory to read.
1863 * dirent - buffer for directory entries.
1865 * OUT: dirent - filler buffer of directory entries.
1867 * RETURN: 0 if success
1868 * error code if failure
1871 * ip - atime updated
1873 * Note that the low 4 bits of the cookie returned by zap is always zero.
1874 * This allows us to use the low range for "special" directory entries:
1875 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1876 * we use the offset 2 for the '.zfs' directory.
1880 zfs_readdir(struct inode
*ip
, void *dirent
, filldir_t filldir
,
1881 loff_t
*pos
, cred_t
*cr
)
1883 znode_t
*zp
= ITOZ(ip
);
1884 zfs_sb_t
*zsb
= ITOZSB(ip
);
1887 zap_attribute_t zap
;
1897 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
1898 &parent
, sizeof (parent
))) != 0)
1902 * Quit if directory has been removed (posix)
1909 prefetch
= zp
->z_zn_prefetch
;
1912 * Initialize the iterator cursor.
1916 * Start iteration from the beginning of the directory.
1918 zap_cursor_init(&zc
, os
, zp
->z_id
);
1921 * The offset is a serialized cursor.
1923 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, *pos
);
1927 * Transform to file-system independent format
1934 * Special case `.', `..', and `.zfs'.
1937 (void) strcpy(zap
.za_name
, ".");
1938 zap
.za_normalization_conflict
= 0;
1940 } else if (*pos
== 1) {
1941 (void) strcpy(zap
.za_name
, "..");
1942 zap
.za_normalization_conflict
= 0;
1944 } else if (*pos
== 2 && zfs_show_ctldir(zp
)) {
1945 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
1946 zap
.za_normalization_conflict
= 0;
1947 objnum
= ZFSCTL_INO_ROOT
;
1952 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
1953 if (error
== ENOENT
)
1959 if (zap
.za_integer_length
!= 8 ||
1960 zap
.za_num_integers
!= 1) {
1961 cmn_err(CE_WARN
, "zap_readdir: bad directory "
1962 "entry, obj = %lld, offset = %lld\n",
1963 (u_longlong_t
)zp
->z_id
,
1964 (u_longlong_t
)*pos
);
1969 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
1971 done
= filldir(dirent
, zap
.za_name
, strlen(zap
.za_name
),
1972 zap_cursor_serialize(&zc
), objnum
, 0);
1977 /* Prefetch znode */
1979 dmu_prefetch(os
, objnum
, 0, 0);
1983 zap_cursor_advance(&zc
);
1984 *pos
= zap_cursor_serialize(&zc
);
1989 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
1992 zap_cursor_fini(&zc
);
1993 if (error
== ENOENT
)
1996 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
1997 zfs_inode_update(zp
);
2004 EXPORT_SYMBOL(zfs_readdir
);
2006 ulong_t zfs_fsync_sync_cnt
= 4;
2009 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2011 znode_t
*zp
= ITOZ(ip
);
2012 zfs_sb_t
*zsb
= ITOZSB(ip
);
2014 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2016 if (zsb
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2019 zil_commit(zsb
->z_log
, zp
->z_id
);
2024 EXPORT_SYMBOL(zfs_fsync
);
2028 * Get the requested file attributes and place them in the provided
2031 * IN: ip - inode of file.
2032 * stat - kstat structure to fill in.
2033 * flags - ATTR_NOACLCHECK (CIFS server context)
2034 * cr - credentials of caller.
2036 * OUT: stat - filled in kstat values.
2040 zfs_getattr(struct inode
*ip
, struct kstat
*stat
, int flags
, cred_t
*cr
)
2042 znode_t
*zp
= ITOZ(ip
);
2043 zfs_sb_t
*zsb
= ITOZSB(ip
);
2046 uint64_t mtime
[2], ctime
[2];
2048 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2049 sa_bulk_attr_t bulk
[2];
2055 zfs_fuid_map_ids(zp
, cr
, &stat
->uid
, &stat
->gid
);
2057 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
2058 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
2060 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2066 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2067 * Also, if we are the owner don't bother, since owner should
2068 * always be allowed to read basic attributes of file.
2070 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2071 (stat
->uid
!= crgetuid(cr
))) {
2072 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2080 * Return all attributes. It's cheaper to provide the answer
2081 * than to determine whether we were asked the question.
2084 mutex_enter(&zp
->z_lock
);
2085 stat
->ino
= ip
->i_ino
;
2086 stat
->mode
= zp
->z_mode
;
2087 stat
->uid
= zp
->z_uid
;
2088 stat
->gid
= zp
->z_gid
;
2089 if ((zp
->z_id
== zsb
->z_root
) && zfs_show_ctldir(zp
))
2090 links
= zp
->z_links
+ 1;
2092 links
= zp
->z_links
;
2093 stat
->nlink
= MIN(links
, ZFS_LINK_MAX
);
2094 stat
->size
= i_size_read(ip
);
2095 stat
->rdev
= ip
->i_rdev
;
2096 stat
->dev
= ip
->i_rdev
;
2098 ZFS_TIME_DECODE(&stat
->atime
, zp
->z_atime
);
2099 ZFS_TIME_DECODE(&stat
->mtime
, mtime
);
2100 ZFS_TIME_DECODE(&stat
->ctime
, ctime
);
2102 mutex_exit(&zp
->z_lock
);
2104 sa_object_size(zp
->z_sa_hdl
, &blksz
, &stat
->blocks
);
2105 stat
->blksize
= (1 << ip
->i_blkbits
);
2107 if (zp
->z_blksz
== 0) {
2109 * Block size hasn't been set; suggest maximal I/O transfers.
2111 stat
->blksize
= zsb
->z_max_blksz
;
2117 EXPORT_SYMBOL(zfs_getattr
);
2120 * Set the file attributes to the values contained in the
2123 * IN: ip - inode of file to be modified.
2124 * vap - new attribute values.
2125 * If AT_XVATTR set, then optional attrs are being set
2126 * flags - ATTR_UTIME set if non-default time values provided.
2127 * - ATTR_NOACLCHECK (CIFS context only).
2128 * cr - credentials of caller.
2130 * RETURN: 0 if success
2131 * error code if failure
2134 * ip - ctime updated, mtime updated if size changed.
2138 zfs_setattr(struct inode
*ip
, struct iattr
*attr
, int flags
, cred_t
*cr
)
2140 znode_t
*zp
= ITOZ(ip
);
2141 zfs_sb_t
*zsb
= ITOZSB(ip
);
2145 uint_t mask
= attr
->ia_valid
;
2149 uint64_t new_uid
, new_gid
;
2151 uint64_t mtime
[2], ctime
[2];
2153 int need_policy
= FALSE
;
2155 zfs_fuid_info_t
*fuidp
= NULL
;
2156 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2157 zfs_acl_t
*aclp
= NULL
;
2158 boolean_t fuid_dirtied
= B_FALSE
;
2159 sa_bulk_attr_t bulk
[7], xattr_bulk
[7];
2160 int count
= 0, xattr_count
= 0;
2171 * Make sure that if we have ephemeral uid/gid or xvattr specified
2172 * that file system is at proper version level
2174 if (zsb
->z_use_fuids
== B_FALSE
&&
2175 (((mask
& ATTR_UID
) && IS_EPHEMERAL(attr
->ia_uid
)) ||
2176 ((mask
& ATTR_GID
) && IS_EPHEMERAL(attr
->ia_gid
)))) {
2181 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2186 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2191 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2200 /* Can this be moved to before the top label? */
2201 if (zsb
->z_vfs
->mnt_flags
& MNT_READONLY
) {
2207 * First validate permissions
2210 if (mask
& ATTR_SIZE
) {
2211 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2217 * XXX - Note, we are not providing any open
2218 * mode flags here (like FNDELAY), so we may
2219 * block if there are locks present... this
2220 * should be addressed in openat().
2222 /* XXX - would it be OK to generate a log record here? */
2223 err
= zfs_freesp(zp
, attr
->ia_size
, 0, 0, FALSE
);
2229 /* Careful negative Linux return code here */
2230 err
= -vmtruncate(ip
, attr
->ia_size
);
2237 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2238 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2243 * NOTE: even if a new mode is being set,
2244 * we may clear S_ISUID/S_ISGID bits.
2247 if (!(mask
& ATTR_MODE
))
2248 attr
->ia_mode
= zp
->z_mode
;
2251 * Take ownership or chgrp to group we are a member of
2254 take_owner
= (mask
& ATTR_UID
) &&
2255 (attr
->ia_uid
== crgetuid(cr
));
2256 take_group
= (mask
& ATTR_GID
) &&
2257 zfs_groupmember(zsb
, attr
->ia_gid
, cr
);
2260 * If both AT_UID and AT_GID are set then take_owner and
2261 * take_group must both be set in order to allow taking
2264 * Otherwise, send the check through secpolicy_vnode_setattr()
2268 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2269 take_owner
&& take_group
) ||
2270 ((idmask
== ATTR_UID
) && take_owner
) ||
2271 ((idmask
== ATTR_GID
) && take_group
)) {
2272 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2273 skipaclchk
, cr
) == 0) {
2275 * Remove setuid/setgid for non-privileged users
2277 secpolicy_setid_clear(attr
, cr
);
2278 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2287 mutex_enter(&zp
->z_lock
);
2288 oldva
.va_mode
= zp
->z_mode
;
2289 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2291 mutex_exit(&zp
->z_lock
);
2293 if (mask
& ATTR_MODE
) {
2294 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2295 err
= secpolicy_setid_setsticky_clear(ip
, attr
,
2301 trim_mask
|= ATTR_MODE
;
2309 * If trim_mask is set then take ownership
2310 * has been granted or write_acl is present and user
2311 * has the ability to modify mode. In that case remove
2312 * UID|GID and or MODE from mask so that
2313 * secpolicy_vnode_setattr() doesn't revoke it.
2317 saved_mask
= attr
->ia_valid
;
2318 attr
->ia_valid
&= ~trim_mask
;
2320 err
= secpolicy_vnode_setattr(cr
, ip
, attr
, &oldva
, flags
,
2321 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2328 attr
->ia_valid
|= saved_mask
;
2332 * secpolicy_vnode_setattr, or take ownership may have
2335 mask
= attr
->ia_valid
;
2337 if ((mask
& (ATTR_UID
| ATTR_GID
))) {
2338 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
2339 &xattr_obj
, sizeof (xattr_obj
));
2341 if (err
== 0 && xattr_obj
) {
2342 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2346 if (mask
& ATTR_UID
) {
2347 new_uid
= zfs_fuid_create(zsb
,
2348 (uint64_t)attr
->ia_uid
, cr
, ZFS_OWNER
, &fuidp
);
2349 if (new_uid
!= zp
->z_uid
&&
2350 zfs_fuid_overquota(zsb
, B_FALSE
, new_uid
)) {
2358 if (mask
& ATTR_GID
) {
2359 new_gid
= zfs_fuid_create(zsb
, (uint64_t)attr
->ia_gid
,
2360 cr
, ZFS_GROUP
, &fuidp
);
2361 if (new_gid
!= zp
->z_gid
&&
2362 zfs_fuid_overquota(zsb
, B_TRUE
, new_gid
)) {
2370 tx
= dmu_tx_create(zsb
->z_os
);
2372 if (mask
& ATTR_MODE
) {
2373 uint64_t pmode
= zp
->z_mode
;
2375 new_mode
= (pmode
& S_IFMT
) | (attr
->ia_mode
& ~S_IFMT
);
2377 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
2379 mutex_enter(&zp
->z_lock
);
2380 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2382 * Are we upgrading ACL from old V0 format
2385 if (zsb
->z_version
>= ZPL_VERSION_FUID
&&
2386 zfs_znode_acl_version(zp
) ==
2387 ZFS_ACL_VERSION_INITIAL
) {
2388 dmu_tx_hold_free(tx
, acl_obj
, 0,
2390 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2391 0, aclp
->z_acl_bytes
);
2393 dmu_tx_hold_write(tx
, acl_obj
, 0,
2396 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2397 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2398 0, aclp
->z_acl_bytes
);
2400 mutex_exit(&zp
->z_lock
);
2401 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2403 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2407 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2410 fuid_dirtied
= zsb
->z_fuid_dirty
;
2412 zfs_fuid_txhold(zsb
, tx
);
2414 zfs_sa_upgrade_txholds(tx
, zp
);
2416 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
2418 if (err
== ERESTART
)
2425 * Set each attribute requested.
2426 * We group settings according to the locks they need to acquire.
2428 * Note: you cannot set ctime directly, although it will be
2429 * updated as a side-effect of calling this function.
2433 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2434 mutex_enter(&zp
->z_acl_lock
);
2435 mutex_enter(&zp
->z_lock
);
2437 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
2438 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2441 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2442 mutex_enter(&attrzp
->z_acl_lock
);
2443 mutex_enter(&attrzp
->z_lock
);
2444 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2445 SA_ZPL_FLAGS(zsb
), NULL
, &attrzp
->z_pflags
,
2446 sizeof (attrzp
->z_pflags
));
2449 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2451 if (mask
& ATTR_UID
) {
2452 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
2453 &new_uid
, sizeof (new_uid
));
2454 zp
->z_uid
= new_uid
;
2456 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2457 SA_ZPL_UID(zsb
), NULL
, &new_uid
,
2459 attrzp
->z_uid
= new_uid
;
2463 if (mask
& ATTR_GID
) {
2464 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
),
2465 NULL
, &new_gid
, sizeof (new_gid
));
2466 zp
->z_gid
= new_gid
;
2468 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2469 SA_ZPL_GID(zsb
), NULL
, &new_gid
,
2471 attrzp
->z_gid
= new_gid
;
2474 if (!(mask
& ATTR_MODE
)) {
2475 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
),
2476 NULL
, &new_mode
, sizeof (new_mode
));
2477 new_mode
= zp
->z_mode
;
2479 err
= zfs_acl_chown_setattr(zp
);
2482 err
= zfs_acl_chown_setattr(attrzp
);
2487 if (mask
& ATTR_MODE
) {
2488 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
2489 &new_mode
, sizeof (new_mode
));
2490 zp
->z_mode
= new_mode
;
2491 ASSERT3U((uintptr_t)aclp
, !=, NULL
);
2492 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2493 ASSERT3U(err
, ==, 0);
2494 if (zp
->z_acl_cached
)
2495 zfs_acl_free(zp
->z_acl_cached
);
2496 zp
->z_acl_cached
= aclp
;
2501 if (mask
& ATTR_ATIME
) {
2502 ZFS_TIME_ENCODE(&attr
->ia_atime
, zp
->z_atime
);
2503 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
2504 &zp
->z_atime
, sizeof (zp
->z_atime
));
2507 if (mask
& ATTR_MTIME
) {
2508 ZFS_TIME_ENCODE(&attr
->ia_mtime
, mtime
);
2509 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
,
2510 mtime
, sizeof (mtime
));
2513 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2514 if (mask
& ATTR_SIZE
&& !(mask
& ATTR_MTIME
)) {
2515 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
),
2516 NULL
, mtime
, sizeof (mtime
));
2517 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2518 &ctime
, sizeof (ctime
));
2519 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
2521 } else if (mask
!= 0) {
2522 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2523 &ctime
, sizeof (ctime
));
2524 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
2527 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2528 SA_ZPL_CTIME(zsb
), NULL
,
2529 &ctime
, sizeof (ctime
));
2530 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2531 mtime
, ctime
, B_TRUE
);
2535 * Do this after setting timestamps to prevent timestamp
2536 * update from toggling bit
2540 zfs_fuid_sync(zsb
, tx
);
2543 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, attr
, mask
, fuidp
);
2545 mutex_exit(&zp
->z_lock
);
2546 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2547 mutex_exit(&zp
->z_acl_lock
);
2550 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2551 mutex_exit(&attrzp
->z_acl_lock
);
2552 mutex_exit(&attrzp
->z_lock
);
2555 if (err
== 0 && attrzp
) {
2556 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
2567 zfs_fuid_info_free(fuidp
);
2573 if (err
== ERESTART
)
2576 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
2578 zfs_inode_update(zp
);
2582 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
2583 zil_commit(zilog
, 0);
2588 EXPORT_SYMBOL(zfs_setattr
);
2590 typedef struct zfs_zlock
{
2591 krwlock_t
*zl_rwlock
; /* lock we acquired */
2592 znode_t
*zl_znode
; /* znode we held */
2593 struct zfs_zlock
*zl_next
; /* next in list */
2597 * Drop locks and release vnodes that were held by zfs_rename_lock().
2600 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
2604 while ((zl
= *zlpp
) != NULL
) {
2605 if (zl
->zl_znode
!= NULL
)
2606 iput(ZTOI(zl
->zl_znode
));
2607 rw_exit(zl
->zl_rwlock
);
2608 *zlpp
= zl
->zl_next
;
2609 kmem_free(zl
, sizeof (*zl
));
2614 * Search back through the directory tree, using the ".." entries.
2615 * Lock each directory in the chain to prevent concurrent renames.
2616 * Fail any attempt to move a directory into one of its own descendants.
2617 * XXX - z_parent_lock can overlap with map or grow locks
2620 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
2624 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
2625 uint64_t oidp
= zp
->z_id
;
2626 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
2627 krw_t rw
= RW_WRITER
;
2630 * First pass write-locks szp and compares to zp->z_id.
2631 * Later passes read-lock zp and compare to zp->z_parent.
2634 if (!rw_tryenter(rwlp
, rw
)) {
2636 * Another thread is renaming in this path.
2637 * Note that if we are a WRITER, we don't have any
2638 * parent_locks held yet.
2640 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
2642 * Drop our locks and restart
2644 zfs_rename_unlock(&zl
);
2648 rwlp
= &szp
->z_parent_lock
;
2653 * Wait for other thread to drop its locks
2659 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
2660 zl
->zl_rwlock
= rwlp
;
2661 zl
->zl_znode
= NULL
;
2662 zl
->zl_next
= *zlpp
;
2665 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
2668 if (oidp
== rootid
) /* We've hit the top */
2671 if (rw
== RW_READER
) { /* i.e. not the first pass */
2672 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
2677 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
2678 &oidp
, sizeof (oidp
));
2679 rwlp
= &zp
->z_parent_lock
;
2682 } while (zp
->z_id
!= sdzp
->z_id
);
2688 * Move an entry from the provided source directory to the target
2689 * directory. Change the entry name as indicated.
2691 * IN: sdip - Source directory containing the "old entry".
2692 * snm - Old entry name.
2693 * tdip - Target directory to contain the "new entry".
2694 * tnm - New entry name.
2695 * cr - credentials of caller.
2696 * flags - case flags
2698 * RETURN: 0 if success
2699 * error code if failure
2702 * sdip,tdip - ctime|mtime updated
2706 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
2707 cred_t
*cr
, int flags
)
2709 znode_t
*tdzp
, *szp
, *tzp
;
2710 znode_t
*sdzp
= ITOZ(sdip
);
2711 zfs_sb_t
*zsb
= ITOZSB(sdip
);
2713 zfs_dirlock_t
*sdl
, *tdl
;
2716 int cmp
, serr
, terr
;
2721 ZFS_VERIFY_ZP(sdzp
);
2724 if (tdip
->i_sb
!= sdip
->i_sb
) {
2730 ZFS_VERIFY_ZP(tdzp
);
2731 if (zsb
->z_utf8
&& u8_validate(tnm
,
2732 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
2737 if (flags
& FIGNORECASE
)
2746 * This is to prevent the creation of links into attribute space
2747 * by renaming a linked file into/outof an attribute directory.
2748 * See the comment in zfs_link() for why this is considered bad.
2750 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
2756 * Lock source and target directory entries. To prevent deadlock,
2757 * a lock ordering must be defined. We lock the directory with
2758 * the smallest object id first, or if it's a tie, the one with
2759 * the lexically first name.
2761 if (sdzp
->z_id
< tdzp
->z_id
) {
2763 } else if (sdzp
->z_id
> tdzp
->z_id
) {
2767 * First compare the two name arguments without
2768 * considering any case folding.
2770 int nofold
= (zsb
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
2772 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
2773 ASSERT(error
== 0 || !zsb
->z_utf8
);
2776 * POSIX: "If the old argument and the new argument
2777 * both refer to links to the same existing file,
2778 * the rename() function shall return successfully
2779 * and perform no other action."
2785 * If the file system is case-folding, then we may
2786 * have some more checking to do. A case-folding file
2787 * system is either supporting mixed case sensitivity
2788 * access or is completely case-insensitive. Note
2789 * that the file system is always case preserving.
2791 * In mixed sensitivity mode case sensitive behavior
2792 * is the default. FIGNORECASE must be used to
2793 * explicitly request case insensitive behavior.
2795 * If the source and target names provided differ only
2796 * by case (e.g., a request to rename 'tim' to 'Tim'),
2797 * we will treat this as a special case in the
2798 * case-insensitive mode: as long as the source name
2799 * is an exact match, we will allow this to proceed as
2800 * a name-change request.
2802 if ((zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
2803 (zsb
->z_case
== ZFS_CASE_MIXED
&&
2804 flags
& FIGNORECASE
)) &&
2805 u8_strcmp(snm
, tnm
, 0, zsb
->z_norm
, U8_UNICODE_LATEST
,
2808 * case preserving rename request, require exact
2817 * If the source and destination directories are the same, we should
2818 * grab the z_name_lock of that directory only once.
2822 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
2826 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
2827 ZEXISTS
| zflg
, NULL
, NULL
);
2828 terr
= zfs_dirent_lock(&tdl
,
2829 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
2831 terr
= zfs_dirent_lock(&tdl
,
2832 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
2833 serr
= zfs_dirent_lock(&sdl
,
2834 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
2840 * Source entry invalid or not there.
2843 zfs_dirent_unlock(tdl
);
2849 rw_exit(&sdzp
->z_name_lock
);
2851 if (strcmp(snm
, "..") == 0)
2857 zfs_dirent_unlock(sdl
);
2861 rw_exit(&sdzp
->z_name_lock
);
2863 if (strcmp(tnm
, "..") == 0)
2870 * Must have write access at the source to remove the old entry
2871 * and write access at the target to create the new entry.
2872 * Note that if target and source are the same, this can be
2873 * done in a single check.
2876 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
2879 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
2881 * Check to make sure rename is valid.
2882 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
2884 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
2889 * Does target exist?
2893 * Source and target must be the same type.
2895 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
2896 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
2901 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
2907 * POSIX dictates that when the source and target
2908 * entries refer to the same file object, rename
2909 * must do nothing and exit without error.
2911 if (szp
->z_id
== tzp
->z_id
) {
2917 tx
= dmu_tx_create(zsb
->z_os
);
2918 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
2919 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
2920 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
2921 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
2923 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
2924 zfs_sa_upgrade_txholds(tx
, tdzp
);
2927 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
2928 zfs_sa_upgrade_txholds(tx
, tzp
);
2931 zfs_sa_upgrade_txholds(tx
, szp
);
2932 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
2933 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
2936 zfs_rename_unlock(&zl
);
2937 zfs_dirent_unlock(sdl
);
2938 zfs_dirent_unlock(tdl
);
2941 rw_exit(&sdzp
->z_name_lock
);
2946 if (error
== ERESTART
) {
2956 if (tzp
) /* Attempt to remove the existing target */
2957 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
2960 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
2962 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
2964 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
2965 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
2966 ASSERT3U(error
, ==, 0);
2968 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
2970 zfs_log_rename(zilog
, tx
, TX_RENAME
|
2971 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
2972 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
2975 * At this point, we have successfully created
2976 * the target name, but have failed to remove
2977 * the source name. Since the create was done
2978 * with the ZRENAMING flag, there are
2979 * complications; for one, the link count is
2980 * wrong. The easiest way to deal with this
2981 * is to remove the newly created target, and
2982 * return the original error. This must
2983 * succeed; fortunately, it is very unlikely to
2984 * fail, since we just created it.
2986 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
2987 ZRENAMING
, NULL
), ==, 0);
2995 zfs_rename_unlock(&zl
);
2997 zfs_dirent_unlock(sdl
);
2998 zfs_dirent_unlock(tdl
);
3000 zfs_inode_update(sdzp
);
3002 rw_exit(&sdzp
->z_name_lock
);
3005 zfs_inode_update(tdzp
);
3007 zfs_inode_update(szp
);
3010 zfs_inode_update(tzp
);
3014 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3015 zil_commit(zilog
, 0);
3020 EXPORT_SYMBOL(zfs_rename
);
3023 * Insert the indicated symbolic reference entry into the directory.
3025 * IN: dip - Directory to contain new symbolic link.
3026 * link - Name for new symlink entry.
3027 * vap - Attributes of new entry.
3028 * target - Target path of new symlink.
3030 * cr - credentials of caller.
3031 * flags - case flags
3033 * RETURN: 0 if success
3034 * error code if failure
3037 * dip - ctime|mtime updated
3041 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
3042 struct inode
**ipp
, cred_t
*cr
, int flags
)
3044 znode_t
*zp
, *dzp
= ITOZ(dip
);
3047 zfs_sb_t
*zsb
= ITOZSB(dip
);
3049 uint64_t len
= strlen(link
);
3052 zfs_acl_ids_t acl_ids
;
3053 boolean_t fuid_dirtied
;
3054 uint64_t txtype
= TX_SYMLINK
;
3056 ASSERT(S_ISLNK(vap
->va_mode
));
3062 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
3063 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3067 if (flags
& FIGNORECASE
)
3070 if (len
> MAXPATHLEN
) {
3072 return (ENAMETOOLONG
);
3075 if ((error
= zfs_acl_ids_create(dzp
, 0,
3076 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3084 * Attempt to lock directory; fail if entry already exists.
3086 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3088 zfs_acl_ids_free(&acl_ids
);
3093 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3094 zfs_acl_ids_free(&acl_ids
);
3095 zfs_dirent_unlock(dl
);
3100 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
3101 zfs_acl_ids_free(&acl_ids
);
3102 zfs_dirent_unlock(dl
);
3106 tx
= dmu_tx_create(zsb
->z_os
);
3107 fuid_dirtied
= zsb
->z_fuid_dirty
;
3108 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3109 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3110 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3111 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3112 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3113 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3114 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3115 acl_ids
.z_aclp
->z_acl_bytes
);
3118 zfs_fuid_txhold(zsb
, tx
);
3119 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3121 zfs_dirent_unlock(dl
);
3122 if (error
== ERESTART
) {
3127 zfs_acl_ids_free(&acl_ids
);
3134 * Create a new object for the symlink.
3135 * for version 4 ZPL datsets the symlink will be an SA attribute
3137 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3140 zfs_fuid_sync(zsb
, tx
);
3142 mutex_enter(&zp
->z_lock
);
3144 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
),
3147 zfs_sa_symlink(zp
, link
, len
, tx
);
3148 mutex_exit(&zp
->z_lock
);
3151 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
3152 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3154 * Insert the new object into the directory.
3156 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3158 if (flags
& FIGNORECASE
)
3160 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3162 zfs_inode_update(dzp
);
3163 zfs_inode_update(zp
);
3165 zfs_acl_ids_free(&acl_ids
);
3169 zfs_dirent_unlock(dl
);
3174 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3175 zil_commit(zilog
, 0);
3180 EXPORT_SYMBOL(zfs_symlink
);
3183 * Return, in the buffer contained in the provided uio structure,
3184 * the symbolic path referred to by ip.
3186 * IN: dentry - dentry of symbolic link.
3187 * nd - namedata for symlink
3189 * RETURN: 0 if success
3190 * error code if failure
3193 * ip - atime updated
3197 zfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
3199 struct inode
*ip
= dentry
->d_inode
;
3200 znode_t
*zp
= ITOZ(ip
);
3201 zfs_sb_t
*zsb
= ITOZSB(ip
);
3209 iov
.iov_len
= MAXPATHLEN
+ 1;
3210 iov
.iov_base
= kmem_zalloc(iov
.iov_len
, KM_SLEEP
);
3214 uio
.uio_resid
= iov
.iov_len
;
3215 uio
.uio_segflg
= UIO_SYSSPACE
;
3217 mutex_enter(&zp
->z_lock
);
3219 error
= sa_lookup_uio(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
), &uio
);
3221 error
= zfs_sa_readlink(zp
, &uio
);
3222 mutex_exit(&zp
->z_lock
);
3224 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
3225 zfs_inode_update(zp
);
3228 kmem_free(iov
.iov_base
, iov
.iov_len
);
3229 nd_set_link(nd
, ERR_PTR(error
));
3231 nd_set_link(nd
, iov
.iov_base
);
3237 EXPORT_SYMBOL(zfs_follow_link
);
3240 * Insert a new entry into directory tdip referencing sip.
3242 * IN: tdip - Directory to contain new entry.
3243 * sip - inode of new entry.
3244 * name - name of new entry.
3245 * cr - credentials of caller.
3247 * RETURN: 0 if success
3248 * error code if failure
3251 * tdip - ctime|mtime updated
3252 * sip - ctime updated
3256 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
)
3258 znode_t
*dzp
= ITOZ(tdip
);
3260 zfs_sb_t
*zsb
= ITOZSB(tdip
);
3269 ASSERT(S_ISDIR(tdip
->i_mode
));
3276 * POSIX dictates that we return EPERM here.
3277 * Better choices include ENOTSUP or EISDIR.
3279 if (S_ISDIR(sip
->i_mode
)) {
3284 if (sip
->i_sb
!= tdip
->i_sb
) {
3292 /* Prevent links to .zfs/shares files */
3294 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
3295 &parent
, sizeof (uint64_t))) != 0) {
3299 if (parent
== zsb
->z_shares_dir
) {
3304 if (zsb
->z_utf8
&& u8_validate(name
,
3305 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3309 #ifdef HAVE_PN_UTILS
3310 if (flags
& FIGNORECASE
)
3312 #endif /* HAVE_PN_UTILS */
3315 * We do not support links between attributes and non-attributes
3316 * because of the potential security risk of creating links
3317 * into "normal" file space in order to circumvent restrictions
3318 * imposed in attribute space.
3320 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3325 owner
= zfs_fuid_map_id(zsb
, szp
->z_uid
, cr
, ZFS_OWNER
);
3326 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3331 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3338 * Attempt to lock directory; fail if entry already exists.
3340 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3346 tx
= dmu_tx_create(zsb
->z_os
);
3347 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3348 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3349 zfs_sa_upgrade_txholds(tx
, szp
);
3350 zfs_sa_upgrade_txholds(tx
, dzp
);
3351 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3353 zfs_dirent_unlock(dl
);
3354 if (error
== ERESTART
) {
3364 error
= zfs_link_create(dl
, szp
, tx
, 0);
3367 uint64_t txtype
= TX_LINK
;
3368 #ifdef HAVE_PN_UTILS
3369 if (flags
& FIGNORECASE
)
3371 #endif /* HAVE_PN_UTILS */
3372 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3377 zfs_dirent_unlock(dl
);
3379 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3380 zil_commit(zilog
, 0);
3382 zfs_inode_update(dzp
);
3383 zfs_inode_update(szp
);
3387 EXPORT_SYMBOL(zfs_link
);
3391 * zfs_null_putapage() is used when the file system has been force
3392 * unmounted. It just drops the pages.
3396 zfs_null_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
3397 size_t *lenp
, int flags
, cred_t
*cr
)
3399 pvn_write_done(pp
, B_INVAL
|B_FORCE
|B_ERROR
);
3404 * Push a page out to disk, klustering if possible.
3406 * IN: vp - file to push page to.
3407 * pp - page to push.
3408 * flags - additional flags.
3409 * cr - credentials of caller.
3411 * OUT: offp - start of range pushed.
3412 * lenp - len of range pushed.
3414 * RETURN: 0 if success
3415 * error code if failure
3417 * NOTE: callers must have locked the page to be pushed. On
3418 * exit, the page (and all other pages in the kluster) must be
3423 zfs_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
,
3424 size_t *lenp
, int flags
, cred_t
*cr
)
3426 znode_t
*zp
= VTOZ(vp
);
3427 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3429 u_offset_t off
, koff
;
3436 * If our blocksize is bigger than the page size, try to kluster
3437 * multiple pages so that we write a full block (thus avoiding
3438 * a read-modify-write).
3440 if (off
< zp
->z_size
&& zp
->z_blksz
> PAGESIZE
) {
3441 klen
= P2ROUNDUP((ulong_t
)zp
->z_blksz
, PAGESIZE
);
3442 koff
= ISP2(klen
) ? P2ALIGN(off
, (u_offset_t
)klen
) : 0;
3443 ASSERT(koff
<= zp
->z_size
);
3444 if (koff
+ klen
> zp
->z_size
)
3445 klen
= P2ROUNDUP(zp
->z_size
- koff
, (uint64_t)PAGESIZE
);
3446 pp
= pvn_write_kluster(vp
, pp
, &off
, &len
, koff
, klen
, flags
);
3448 ASSERT3U(btop(len
), ==, btopr(len
));
3451 * Can't push pages past end-of-file.
3453 if (off
>= zp
->z_size
) {
3454 /* ignore all pages */
3457 } else if (off
+ len
> zp
->z_size
) {
3458 int npages
= btopr(zp
->z_size
- off
);
3461 page_list_break(&pp
, &trunc
, npages
);
3462 /* ignore pages past end of file */
3464 pvn_write_done(trunc
, flags
);
3465 len
= zp
->z_size
- off
;
3468 if (zfs_owner_overquota(zfsvfs
, zp
, B_FALSE
) ||
3469 zfs_owner_overquota(zfsvfs
, zp
, B_TRUE
)) {
3474 tx
= dmu_tx_create(zfsvfs
->z_os
);
3475 dmu_tx_hold_write(tx
, zp
->z_id
, off
, len
);
3477 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3478 zfs_sa_upgrade_txholds(tx
, zp
);
3479 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3481 if (err
== ERESTART
) {
3490 if (zp
->z_blksz
<= PAGESIZE
) {
3491 caddr_t va
= zfs_map_page(pp
, S_READ
);
3492 ASSERT3U(len
, <=, PAGESIZE
);
3493 dmu_write(zfsvfs
->z_os
, zp
->z_id
, off
, len
, va
, tx
);
3494 zfs_unmap_page(pp
, va
);
3496 err
= dmu_write_pages(zfsvfs
->z_os
, zp
->z_id
, off
, len
, pp
, tx
);
3500 uint64_t mtime
[2], ctime
[2];
3501 sa_bulk_attr_t bulk
[3];
3504 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
3506 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
3508 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
3510 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
3512 zfs_log_write(zfsvfs
->z_log
, tx
, TX_WRITE
, zp
, off
, len
, 0);
3517 pvn_write_done(pp
, (err
? B_ERROR
: 0) | flags
);
3527 * Copy the portion of the file indicated from pages into the file.
3528 * The pages are stored in a page list attached to the files vnode.
3530 * IN: vp - vnode of file to push page data to.
3531 * off - position in file to put data.
3532 * len - amount of data to write.
3533 * flags - flags to control the operation.
3534 * cr - credentials of caller.
3535 * ct - caller context.
3537 * RETURN: 0 if success
3538 * error code if failure
3541 * vp - ctime|mtime updated
3545 zfs_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
3546 caller_context_t
*ct
)
3548 znode_t
*zp
= VTOZ(vp
);
3549 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3561 * Align this request to the file block size in case we kluster.
3562 * XXX - this can result in pretty aggresive locking, which can
3563 * impact simultanious read/write access. One option might be
3564 * to break up long requests (len == 0) into block-by-block
3565 * operations to get narrower locking.
3567 blksz
= zp
->z_blksz
;
3569 io_off
= P2ALIGN_TYPED(off
, blksz
, u_offset_t
);
3572 if (len
> 0 && ISP2(blksz
))
3573 io_len
= P2ROUNDUP_TYPED(len
+ (off
- io_off
), blksz
, size_t);
3579 * Search the entire vp list for pages >= io_off.
3581 rl
= zfs_range_lock(zp
, io_off
, UINT64_MAX
, RL_WRITER
);
3582 error
= pvn_vplist_dirty(vp
, io_off
, zfs_putapage
, flags
, cr
);
3585 rl
= zfs_range_lock(zp
, io_off
, io_len
, RL_WRITER
);
3587 if (off
> zp
->z_size
) {
3588 /* past end of file */
3589 zfs_range_unlock(rl
);
3594 len
= MIN(io_len
, P2ROUNDUP(zp
->z_size
, PAGESIZE
) - io_off
);
3596 for (off
= io_off
; io_off
< off
+ len
; io_off
+= io_len
) {
3597 if ((flags
& B_INVAL
) || ((flags
& B_ASYNC
) == 0)) {
3598 pp
= page_lookup(vp
, io_off
,
3599 (flags
& (B_INVAL
| B_FREE
)) ? SE_EXCL
: SE_SHARED
);
3601 pp
= page_lookup_nowait(vp
, io_off
,
3602 (flags
& B_FREE
) ? SE_EXCL
: SE_SHARED
);
3605 if (pp
!= NULL
&& pvn_getdirty(pp
, flags
)) {
3609 * Found a dirty page to push
3611 err
= zfs_putapage(vp
, pp
, &io_off
, &io_len
, flags
, cr
);
3619 zfs_range_unlock(rl
);
3620 if ((flags
& B_ASYNC
) == 0 || zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3621 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
3625 #endif /* HAVE_MMAP */
3629 zfs_inactive(struct inode
*ip
)
3631 znode_t
*zp
= ITOZ(ip
);
3632 zfs_sb_t
*zsb
= ITOZSB(ip
);
3635 truncate_inode_pages(&ip
->i_data
, 0);
3637 #ifdef HAVE_SNAPSHOT
3638 /* Early return for snapshot inode? */
3639 #endif /* HAVE_SNAPSHOT */
3641 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_READER
);
3642 if (zp
->z_sa_hdl
== NULL
) {
3643 rw_exit(&zsb
->z_teardown_inactive_lock
);
3647 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
3648 dmu_tx_t
*tx
= dmu_tx_create(zsb
->z_os
);
3650 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3651 zfs_sa_upgrade_txholds(tx
, zp
);
3652 error
= dmu_tx_assign(tx
, TXG_WAIT
);
3656 mutex_enter(&zp
->z_lock
);
3657 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
),
3658 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
3659 zp
->z_atime_dirty
= 0;
3660 mutex_exit(&zp
->z_lock
);
3666 rw_exit(&zsb
->z_teardown_inactive_lock
);
3668 EXPORT_SYMBOL(zfs_inactive
);
3671 * Bounds-check the seek operation.
3673 * IN: ip - inode seeking within
3674 * ooff - old file offset
3675 * noffp - pointer to new file offset
3676 * ct - caller context
3678 * RETURN: 0 if success
3679 * EINVAL if new offset invalid
3683 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
,
3684 caller_context_t
*ct
)
3686 if (S_ISDIR(ip
->i_mode
))
3688 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
3690 EXPORT_SYMBOL(zfs_seek
);
3694 * Pre-filter the generic locking function to trap attempts to place
3695 * a mandatory lock on a memory mapped file.
3698 zfs_frlock(vnode_t
*vp
, int cmd
, flock64_t
*bfp
, int flag
, offset_t offset
,
3699 flk_callback_t
*flk_cbp
, cred_t
*cr
, caller_context_t
*ct
)
3701 znode_t
*zp
= VTOZ(vp
);
3702 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3708 * We are following the UFS semantics with respect to mapcnt
3709 * here: If we see that the file is mapped already, then we will
3710 * return an error, but we don't worry about races between this
3711 * function and zfs_map().
3713 if (zp
->z_mapcnt
> 0 && MANDMODE(zp
->z_mode
)) {
3718 return (fs_frlock(vp
, cmd
, bfp
, flag
, offset
, flk_cbp
, cr
, ct
));
3722 * If we can't find a page in the cache, we will create a new page
3723 * and fill it with file data. For efficiency, we may try to fill
3724 * multiple pages at once (klustering) to fill up the supplied page
3725 * list. Note that the pages to be filled are held with an exclusive
3726 * lock to prevent access by other threads while they are being filled.
3729 zfs_fillpage(vnode_t
*vp
, u_offset_t off
, struct seg
*seg
,
3730 caddr_t addr
, page_t
*pl
[], size_t plsz
, enum seg_rw rw
)
3732 znode_t
*zp
= VTOZ(vp
);
3733 page_t
*pp
, *cur_pp
;
3734 objset_t
*os
= zp
->z_zfsvfs
->z_os
;
3735 u_offset_t io_off
, total
;
3739 if (plsz
== PAGESIZE
|| zp
->z_blksz
<= PAGESIZE
) {
3741 * We only have a single page, don't bother klustering
3745 pp
= page_create_va(vp
, io_off
, io_len
,
3746 PG_EXCL
| PG_WAIT
, seg
, addr
);
3749 * Try to find enough pages to fill the page list
3751 pp
= pvn_read_kluster(vp
, off
, seg
, addr
, &io_off
,
3752 &io_len
, off
, plsz
, 0);
3756 * The page already exists, nothing to do here.
3763 * Fill the pages in the kluster.
3766 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
3769 ASSERT3U(io_off
, ==, cur_pp
->p_offset
);
3770 va
= zfs_map_page(cur_pp
, S_WRITE
);
3771 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
3773 zfs_unmap_page(cur_pp
, va
);
3775 /* On error, toss the entire kluster */
3776 pvn_read_done(pp
, B_ERROR
);
3777 /* convert checksum errors into IO errors */
3782 cur_pp
= cur_pp
->p_next
;
3786 * Fill in the page list array from the kluster starting
3787 * from the desired offset `off'.
3788 * NOTE: the page list will always be null terminated.
3790 pvn_plist_init(pp
, pl
, plsz
, off
, io_len
, rw
);
3791 ASSERT(pl
== NULL
|| (*pl
)->p_offset
== off
);
3797 * Return pointers to the pages for the file region [off, off + len]
3798 * in the pl array. If plsz is greater than len, this function may
3799 * also return page pointers from after the specified region
3800 * (i.e. the region [off, off + plsz]). These additional pages are
3801 * only returned if they are already in the cache, or were created as
3802 * part of a klustered read.
3804 * IN: vp - vnode of file to get data from.
3805 * off - position in file to get data from.
3806 * len - amount of data to retrieve.
3807 * plsz - length of provided page list.
3808 * seg - segment to obtain pages for.
3809 * addr - virtual address of fault.
3810 * rw - mode of created pages.
3811 * cr - credentials of caller.
3812 * ct - caller context.
3814 * OUT: protp - protection mode of created pages.
3815 * pl - list of pages created.
3817 * RETURN: 0 if success
3818 * error code if failure
3821 * vp - atime updated
3825 zfs_getpage(vnode_t
*vp
, offset_t off
, size_t len
, uint_t
*protp
,
3826 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
3827 enum seg_rw rw
, cred_t
*cr
, caller_context_t
*ct
)
3829 znode_t
*zp
= VTOZ(vp
);
3830 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3834 /* we do our own caching, faultahead is unnecessary */
3837 else if (len
> plsz
)
3840 len
= P2ROUNDUP(len
, PAGESIZE
);
3841 ASSERT(plsz
>= len
);
3850 * Loop through the requested range [off, off + len) looking
3851 * for pages. If we don't find a page, we will need to create
3852 * a new page and fill it with data from the file.
3855 if (*pl
= page_lookup(vp
, off
, SE_SHARED
))
3857 else if (err
= zfs_fillpage(vp
, off
, seg
, addr
, pl
, plsz
, rw
))
3860 ASSERT3U((*pl
)->p_offset
, ==, off
);
3864 ASSERT3U(len
, >=, PAGESIZE
);
3867 ASSERT3U(plsz
, >=, PAGESIZE
);
3874 * Fill out the page array with any pages already in the cache.
3877 (*pl
++ = page_lookup_nowait(vp
, off
, SE_SHARED
))) {
3884 * Release any pages we have previously locked.
3889 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
3899 * Request a memory map for a section of a file. This code interacts
3900 * with common code and the VM system as follows:
3902 * common code calls mmap(), which ends up in smmap_common()
3904 * this calls VOP_MAP(), which takes you into (say) zfs
3906 * zfs_map() calls as_map(), passing segvn_create() as the callback
3908 * segvn_create() creates the new segment and calls VOP_ADDMAP()
3910 * zfs_addmap() updates z_mapcnt
3914 zfs_map(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t
*addrp
,
3915 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
3916 caller_context_t
*ct
)
3918 znode_t
*zp
= VTOZ(vp
);
3919 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
3920 segvn_crargs_t vn_a
;
3926 if ((prot
& PROT_WRITE
) && (zp
->z_pflags
&
3927 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
3932 if ((prot
& (PROT_READ
| PROT_EXEC
)) &&
3933 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
3938 if (vp
->v_flag
& VNOMAP
) {
3943 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
3948 if (vp
->v_type
!= VREG
) {
3954 * If file is locked, disallow mapping.
3956 if (MANDMODE(zp
->z_mode
) && vn_has_flocks(vp
)) {
3962 error
= choose_addr(as
, addrp
, len
, off
, ADDR_VACALIGN
, flags
);
3970 vn_a
.offset
= (u_offset_t
)off
;
3971 vn_a
.type
= flags
& MAP_TYPE
;
3973 vn_a
.maxprot
= maxprot
;
3976 vn_a
.flags
= flags
& ~MAP_TYPE
;
3978 vn_a
.lgrp_mem_policy_flags
= 0;
3980 error
= as_map(as
, *addrp
, len
, segvn_create
, &vn_a
);
3989 zfs_addmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
3990 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
3991 caller_context_t
*ct
)
3993 uint64_t pages
= btopr(len
);
3995 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, pages
);
4000 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4001 * more accurate mtime for the associated file. Since we don't have a way of
4002 * detecting when the data was actually modified, we have to resort to
4003 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4004 * last page is pushed. The problem occurs when the msync() call is omitted,
4005 * which by far the most common case:
4013 * putpage() via fsflush
4015 * If we wait until fsflush to come along, we can have a modification time that
4016 * is some arbitrary point in the future. In order to prevent this in the
4017 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4022 zfs_delmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
4023 size_t len
, uint_t prot
, uint_t maxprot
, uint_t flags
, cred_t
*cr
,
4024 caller_context_t
*ct
)
4026 uint64_t pages
= btopr(len
);
4028 ASSERT3U(VTOZ(vp
)->z_mapcnt
, >=, pages
);
4029 atomic_add_64(&VTOZ(vp
)->z_mapcnt
, -pages
);
4031 if ((flags
& MAP_SHARED
) && (prot
& PROT_WRITE
) &&
4032 vn_has_cached_data(vp
))
4033 (void) VOP_PUTPAGE(vp
, off
, len
, B_ASYNC
, cr
, ct
);
4037 #endif /* HAVE_MMAP */
4040 * convoff - converts the given data (start, whence) to the
4044 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4049 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4050 if ((error
= zfs_getattr(ip
, &stat
, 0, CRED()) != 0))
4054 switch (lckdat
->l_whence
) {
4056 lckdat
->l_start
+= offset
;
4059 lckdat
->l_start
+= stat
.size
;
4067 if (lckdat
->l_start
< 0)
4072 lckdat
->l_start
-= offset
;
4075 lckdat
->l_start
-= stat
.size
;
4083 lckdat
->l_whence
= (short)whence
;
4088 * Free or allocate space in a file. Currently, this function only
4089 * supports the `F_FREESP' command. However, this command is somewhat
4090 * misnamed, as its functionality includes the ability to allocate as
4091 * well as free space.
4093 * IN: ip - inode of file to free data in.
4094 * cmd - action to take (only F_FREESP supported).
4095 * bfp - section of file to free/alloc.
4096 * flag - current file open mode flags.
4097 * offset - current file offset.
4098 * cr - credentials of caller [UNUSED].
4100 * RETURN: 0 if success
4101 * error code if failure
4104 * ip - ctime|mtime updated
4108 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4109 offset_t offset
, cred_t
*cr
)
4111 znode_t
*zp
= ITOZ(ip
);
4112 zfs_sb_t
*zsb
= ITOZSB(ip
);
4119 if (cmd
!= F_FREESP
) {
4124 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4129 if (bfp
->l_len
< 0) {
4135 len
= bfp
->l_len
; /* 0 means from off to end of file */
4137 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4142 EXPORT_SYMBOL(zfs_space
);
4146 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4148 znode_t
*zp
= ITOZ(ip
);
4149 zfs_sb_t
*zsb
= ITOZSB(ip
);
4152 uint64_t object
= zp
->z_id
;
4159 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
),
4160 &gen64
, sizeof (uint64_t))) != 0) {
4165 gen
= (uint32_t)gen64
;
4167 size
= (zsb
->z_parent
!= zsb
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4168 if (fidp
->fid_len
< size
) {
4169 fidp
->fid_len
= size
;
4174 zfid
= (zfid_short_t
*)fidp
;
4176 zfid
->zf_len
= size
;
4178 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4179 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4181 /* Must have a non-zero generation number to distinguish from .zfs */
4184 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4185 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4187 if (size
== LONG_FID_LEN
) {
4188 uint64_t objsetid
= dmu_objset_id(zsb
->z_os
);
4191 zlfid
= (zfid_long_t
*)fidp
;
4193 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4194 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4196 /* XXX - this should be the generation number for the objset */
4197 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4198 zlfid
->zf_setgen
[i
] = 0;
4204 EXPORT_SYMBOL(zfs_fid
);
4208 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4210 znode_t
*zp
= ITOZ(ip
);
4211 zfs_sb_t
*zsb
= ITOZSB(ip
);
4213 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4217 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4222 EXPORT_SYMBOL(zfs_getsecattr
);
4226 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4228 znode_t
*zp
= ITOZ(ip
);
4229 zfs_sb_t
*zsb
= ITOZSB(ip
);
4231 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4232 zilog_t
*zilog
= zsb
->z_log
;
4237 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4239 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4240 zil_commit(zilog
, 0);
4245 EXPORT_SYMBOL(zfs_setsecattr
);
4247 #ifdef HAVE_UIO_ZEROCOPY
4249 * Tunable, both must be a power of 2.
4251 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4252 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4253 * an arcbuf for a partial block read
4255 int zcr_blksz_min
= (1 << 10); /* 1K */
4256 int zcr_blksz_max
= (1 << 17); /* 128K */
4260 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
4262 znode_t
*zp
= ITOZ(ip
);
4263 zfs_sb_t
*zsb
= ITOZSB(ip
);
4264 int max_blksz
= zsb
->z_max_blksz
;
4265 uio_t
*uio
= &xuio
->xu_uio
;
4266 ssize_t size
= uio
->uio_resid
;
4267 offset_t offset
= uio
->uio_loffset
;
4272 int preamble
, postamble
;
4274 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4282 * Loan out an arc_buf for write if write size is bigger than
4283 * max_blksz, and the file's block size is also max_blksz.
4286 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4291 * Caller requests buffers for write before knowing where the
4292 * write offset might be (e.g. NFS TCP write).
4297 preamble
= P2PHASE(offset
, blksz
);
4299 preamble
= blksz
- preamble
;
4304 postamble
= P2PHASE(size
, blksz
);
4307 fullblk
= size
/ blksz
;
4308 (void) dmu_xuio_init(xuio
,
4309 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4312 * Have to fix iov base/len for partial buffers. They
4313 * currently represent full arc_buf's.
4316 /* data begins in the middle of the arc_buf */
4317 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4320 (void) dmu_xuio_add(xuio
, abuf
,
4321 blksz
- preamble
, preamble
);
4324 for (i
= 0; i
< fullblk
; i
++) {
4325 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4328 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4332 /* data ends in the middle of the arc_buf */
4333 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4336 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4341 * Loan out an arc_buf for read if the read size is larger than
4342 * the current file block size. Block alignment is not
4343 * considered. Partial arc_buf will be loaned out for read.
4345 blksz
= zp
->z_blksz
;
4346 if (blksz
< zcr_blksz_min
)
4347 blksz
= zcr_blksz_min
;
4348 if (blksz
> zcr_blksz_max
)
4349 blksz
= zcr_blksz_max
;
4350 /* avoid potential complexity of dealing with it */
4351 if (blksz
> max_blksz
) {
4356 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4370 uio
->uio_extflg
= UIO_XUIO
;
4371 XUIO_XUZC_RW(xuio
) = ioflag
;
4378 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
4382 int ioflag
= XUIO_XUZC_RW(xuio
);
4384 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4386 i
= dmu_xuio_cnt(xuio
);
4388 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4390 * if abuf == NULL, it must be a write buffer
4391 * that has been returned in zfs_write().
4394 dmu_return_arcbuf(abuf
);
4395 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4398 dmu_xuio_fini(xuio
);
4401 #endif /* HAVE_UIO_ZEROCOPY */