4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
26 /* Portions Copyright 2007 Jeremy Teo */
27 /* Portions Copyright 2010 Robert Milkowski */
30 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
37 #include <sys/vfs_opreg.h>
41 #include <sys/taskq.h>
43 #include <sys/vmsystm.h>
44 #include <sys/atomic.h>
46 #include <sys/pathname.h>
47 #include <sys/cmn_err.h>
48 #include <sys/errno.h>
49 #include <sys/unistd.h>
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/fs/zfs.h>
55 #include <sys/dmu_objset.h>
61 #include <sys/dirent.h>
62 #include <sys/policy.h>
63 #include <sys/sunddi.h>
66 #include "fs/fs_subr.h"
67 #include <sys/zfs_ctldir.h>
68 #include <sys/zfs_fuid.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_vnops.h>
72 #include <sys/zfs_rlock.h>
73 #include <sys/extdirent.h>
74 #include <sys/kidmap.h>
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
90 * (1) A check must be made in each zfs thread for a mounted file system.
91 * This is done avoiding races using ZFS_ENTER(zsb).
92 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
96 * (2) iput() should always be the last thing except for zil_commit()
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
104 * If you must call iput() within a tx then use iput_ASYNC().
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
109 * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
110 * This is critical because we don't want to block while holding locks.
111 * Note, in particular, that if a lock is sometimes acquired before
112 * the tx assigns, and sometimes after (e.g. z_lock), then failing to
113 * use a non-blocking assign can deadlock the system. The scenario:
115 * Thread A has grabbed a lock before calling dmu_tx_assign().
116 * Thread B is in an already-assigned tx, and blocks for this lock.
117 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
118 * forever, because the previous txg can't quiesce until B's tx commits.
120 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
121 * then drop all locks, call dmu_tx_wait(), and try again.
123 * (5) If the operation succeeded, generate the intent log entry for it
124 * before dropping locks. This ensures that the ordering of events
125 * in the intent log matches the order in which they actually occurred.
126 * During ZIL replay the zfs_log_* functions will update the sequence
127 * number to indicate the zil transaction has replayed.
129 * (6) At the end of each vnode op, the DMU tx must always commit,
130 * regardless of whether there were any errors.
132 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
133 * to ensure that synchronous semantics are provided when necessary.
135 * In general, this is how things should be ordered in each vnode op:
137 * ZFS_ENTER(zsb); // exit if unmounted
139 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
140 * rw_enter(...); // grab any other locks you need
141 * tx = dmu_tx_create(...); // get DMU tx
142 * dmu_tx_hold_*(); // hold each object you might modify
143 * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign
145 * rw_exit(...); // drop locks
146 * zfs_dirent_unlock(dl); // unlock directory entry
147 * iput(...); // release held vnodes
148 * if (error == ERESTART) {
153 * dmu_tx_abort(tx); // abort DMU tx
154 * ZFS_EXIT(zsb); // finished in zfs
155 * return (error); // really out of space
157 * error = do_real_work(); // do whatever this VOP does
159 * zfs_log_*(...); // on success, make ZIL entry
160 * dmu_tx_commit(tx); // commit DMU tx -- error or not
161 * rw_exit(...); // drop locks
162 * zfs_dirent_unlock(dl); // unlock directory entry
163 * iput(...); // release held vnodes
164 * zil_commit(zilog, foid); // synchronous when necessary
165 * ZFS_EXIT(zsb); // finished in zfs
166 * return (error); // done, report error
170 * Virus scanning is unsupported. It would be possible to add a hook
171 * here to performance the required virus scan. This could be done
172 * entirely in the kernel or potentially as an update to invoke a
176 zfs_vscan(struct inode
*ip
, cred_t
*cr
, int async
)
183 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
185 znode_t
*zp
= ITOZ(ip
);
186 zfs_sb_t
*zsb
= ITOZSB(ip
);
191 /* Honor ZFS_APPENDONLY file attribute */
192 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
193 ((flag
& O_APPEND
) == 0)) {
195 return (SET_ERROR(EPERM
));
198 /* Virus scan eligible files on open */
199 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
200 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
201 if (zfs_vscan(ip
, cr
, 0) != 0) {
203 return (SET_ERROR(EACCES
));
207 /* Keep a count of the synchronous opens in the znode */
209 atomic_inc_32(&zp
->z_sync_cnt
);
214 EXPORT_SYMBOL(zfs_open
);
218 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
220 znode_t
*zp
= ITOZ(ip
);
221 zfs_sb_t
*zsb
= ITOZSB(ip
);
227 * Zero the synchronous opens in the znode. Under Linux the
228 * zfs_close() hook is not symmetric with zfs_open(), it is
229 * only called once when the last reference is dropped.
234 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
235 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
236 VERIFY(zfs_vscan(ip
, cr
, 1) == 0);
241 EXPORT_SYMBOL(zfs_close
);
243 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
245 * Lseek support for finding holes (cmd == SEEK_HOLE) and
246 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
249 zfs_holey_common(struct inode
*ip
, int cmd
, loff_t
*off
)
251 znode_t
*zp
= ITOZ(ip
);
252 uint64_t noff
= (uint64_t)*off
; /* new offset */
257 file_sz
= zp
->z_size
;
258 if (noff
>= file_sz
) {
259 return (SET_ERROR(ENXIO
));
262 if (cmd
== SEEK_HOLE
)
267 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
270 if ((error
== ESRCH
) || (noff
> file_sz
)) {
272 * Handle the virtual hole at the end of file.
278 return (SET_ERROR(ENXIO
));
288 zfs_holey(struct inode
*ip
, int cmd
, loff_t
*off
)
290 znode_t
*zp
= ITOZ(ip
);
291 zfs_sb_t
*zsb
= ITOZSB(ip
);
297 error
= zfs_holey_common(ip
, cmd
, off
);
302 EXPORT_SYMBOL(zfs_holey
);
303 #endif /* SEEK_HOLE && SEEK_DATA */
307 * When a file is memory mapped, we must keep the IO data synchronized
308 * between the DMU cache and the memory mapped pages. What this means:
310 * On Write: If we find a memory mapped page, we write to *both*
311 * the page and the dmu buffer.
314 update_pages(struct inode
*ip
, int64_t start
, int len
,
315 objset_t
*os
, uint64_t oid
)
317 struct address_space
*mp
= ip
->i_mapping
;
323 off
= start
& (PAGE_CACHE_SIZE
-1);
324 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
325 nbytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
327 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
329 if (mapping_writably_mapped(mp
))
330 flush_dcache_page(pp
);
333 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
337 if (mapping_writably_mapped(mp
))
338 flush_dcache_page(pp
);
340 mark_page_accessed(pp
);
344 page_cache_release(pp
);
353 * When a file is memory mapped, we must keep the IO data synchronized
354 * between the DMU cache and the memory mapped pages. What this means:
356 * On Read: We "read" preferentially from memory mapped pages,
357 * else we default from the dmu buffer.
359 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
360 * the file is memory mapped.
363 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
365 struct address_space
*mp
= ip
->i_mapping
;
367 znode_t
*zp
= ITOZ(ip
);
368 objset_t
*os
= ITOZSB(ip
)->z_os
;
375 start
= uio
->uio_loffset
;
376 off
= start
& (PAGE_CACHE_SIZE
-1);
377 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
378 bytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
380 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
382 ASSERT(PageUptodate(pp
));
385 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
388 if (mapping_writably_mapped(mp
))
389 flush_dcache_page(pp
);
391 mark_page_accessed(pp
);
393 page_cache_release(pp
);
395 error
= dmu_read_uio(os
, zp
->z_id
, uio
, bytes
);
407 unsigned long zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
410 * Read bytes from specified file into supplied buffer.
412 * IN: ip - inode of file to be read from.
413 * uio - structure supplying read location, range info,
415 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
416 * O_DIRECT flag; used to bypass page cache.
417 * cr - credentials of caller.
419 * OUT: uio - updated offset and range, buffer filled.
421 * RETURN: 0 on success, error code on failure.
424 * inode - atime updated if byte count > 0
428 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
430 znode_t
*zp
= ITOZ(ip
);
431 zfs_sb_t
*zsb
= ITOZSB(ip
);
436 #ifdef HAVE_UIO_ZEROCOPY
438 #endif /* HAVE_UIO_ZEROCOPY */
444 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
446 return (SET_ERROR(EACCES
));
450 * Validate file offset
452 if (uio
->uio_loffset
< (offset_t
)0) {
454 return (SET_ERROR(EINVAL
));
458 * Fasttrack empty reads
460 if (uio
->uio_resid
== 0) {
466 * Check for mandatory locks
468 if (mandatory_lock(ip
) &&
469 !lock_may_read(ip
, uio
->uio_loffset
, uio
->uio_resid
)) {
471 return (SET_ERROR(EAGAIN
));
475 * If we're in FRSYNC mode, sync out this znode before reading it.
477 if (ioflag
& FRSYNC
|| zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
478 zil_commit(zsb
->z_log
, zp
->z_id
);
481 * Lock the range against changes.
483 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
486 * If we are reading past end-of-file we can skip
487 * to the end; but we might still need to set atime.
489 if (uio
->uio_loffset
>= zp
->z_size
) {
494 ASSERT(uio
->uio_loffset
< zp
->z_size
);
495 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
497 #ifdef HAVE_UIO_ZEROCOPY
498 if ((uio
->uio_extflg
== UIO_XUIO
) &&
499 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
501 int blksz
= zp
->z_blksz
;
502 uint64_t offset
= uio
->uio_loffset
;
504 xuio
= (xuio_t
*)uio
;
506 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
509 ASSERT(offset
+ n
<= blksz
);
512 (void) dmu_xuio_init(xuio
, nblk
);
514 if (vn_has_cached_data(ip
)) {
516 * For simplicity, we always allocate a full buffer
517 * even if we only expect to read a portion of a block.
519 while (--nblk
>= 0) {
520 (void) dmu_xuio_add(xuio
,
521 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
526 #endif /* HAVE_UIO_ZEROCOPY */
529 nbytes
= MIN(n
, zfs_read_chunk_size
-
530 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
532 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
533 error
= mappedread(ip
, nbytes
, uio
);
535 error
= dmu_read_uio(os
, zp
->z_id
, uio
, nbytes
);
538 /* convert checksum errors into IO errors */
540 error
= SET_ERROR(EIO
);
547 zfs_range_unlock(rl
);
549 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
550 zfs_inode_update(zp
);
554 EXPORT_SYMBOL(zfs_read
);
557 * Write the bytes to a file.
559 * IN: ip - inode of file to be written to.
560 * uio - structure supplying write location, range info,
562 * ioflag - FAPPEND flag set if in append mode.
563 * O_DIRECT flag; used to bypass page cache.
564 * cr - credentials of caller.
566 * OUT: uio - updated offset and range.
568 * RETURN: 0 if success
569 * error code if failure
572 * ip - ctime|mtime updated if byte count > 0
577 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
579 znode_t
*zp
= ITOZ(ip
);
580 rlim64_t limit
= uio
->uio_limit
;
581 ssize_t start_resid
= uio
->uio_resid
;
585 zfs_sb_t
*zsb
= ZTOZSB(zp
);
590 int max_blksz
= zsb
->z_max_blksz
;
593 iovec_t
*aiov
= NULL
;
596 iovec_t
*iovp
= uio
->uio_iov
;
599 sa_bulk_attr_t bulk
[4];
600 uint64_t mtime
[2], ctime
[2];
601 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
604 * Fasttrack empty write
610 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
616 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
617 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
618 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
619 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
623 * If immutable or not appending then return EPERM
625 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
626 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
627 (uio
->uio_loffset
< zp
->z_size
))) {
629 return (SET_ERROR(EPERM
));
635 * Validate file offset
637 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
640 return (SET_ERROR(EINVAL
));
644 * Check for mandatory locks before calling zfs_range_lock()
645 * in order to prevent a deadlock with locks set via fcntl().
647 if (mandatory_lock(ip
) && !lock_may_write(ip
, woff
, n
)) {
649 return (SET_ERROR(EAGAIN
));
653 * Pre-fault the pages to ensure slow (eg NFS) pages
655 * Skip this if uio contains loaned arc_buf.
657 #ifdef HAVE_UIO_ZEROCOPY
658 if ((uio
->uio_extflg
== UIO_XUIO
) &&
659 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
660 xuio
= (xuio_t
*)uio
;
663 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
666 * If in append mode, set the io offset pointer to eof.
668 if (ioflag
& FAPPEND
) {
670 * Obtain an appending range lock to guarantee file append
671 * semantics. We reset the write offset once we have the lock.
673 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
675 if (rl
->r_len
== UINT64_MAX
) {
677 * We overlocked the file because this write will cause
678 * the file block size to increase.
679 * Note that zp_size cannot change with this lock held.
683 uio
->uio_loffset
= woff
;
686 * Note that if the file block size will change as a result of
687 * this write, then this range lock will lock the entire file
688 * so that we can re-write the block safely.
690 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
694 zfs_range_unlock(rl
);
696 return (SET_ERROR(EFBIG
));
699 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
702 /* Will this write extend the file length? */
703 write_eof
= (woff
+ n
> zp
->z_size
);
705 end_size
= MAX(zp
->z_size
, woff
+ n
);
708 * Write the file in reasonable size chunks. Each chunk is written
709 * in a separate transaction; this keeps the intent log records small
710 * and allows us to do more fine-grained space accounting.
714 woff
= uio
->uio_loffset
;
716 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
717 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
719 dmu_return_arcbuf(abuf
);
720 error
= SET_ERROR(EDQUOT
);
724 if (xuio
&& abuf
== NULL
) {
725 ASSERT(i_iov
< iovcnt
);
727 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
728 dmu_xuio_clear(xuio
, i_iov
);
729 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
730 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
731 aiov
->iov_len
== arc_buf_size(abuf
)));
733 } else if (abuf
== NULL
&& n
>= max_blksz
&&
734 woff
>= zp
->z_size
&&
735 P2PHASE(woff
, max_blksz
) == 0 &&
736 zp
->z_blksz
== max_blksz
) {
738 * This write covers a full block. "Borrow" a buffer
739 * from the dmu so that we can fill it before we enter
740 * a transaction. This avoids the possibility of
741 * holding up the transaction if the data copy hangs
742 * up on a pagefault (e.g., from an NFS server mapping).
746 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
748 ASSERT(abuf
!= NULL
);
749 ASSERT(arc_buf_size(abuf
) == max_blksz
);
750 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
751 UIO_WRITE
, uio
, &cbytes
))) {
752 dmu_return_arcbuf(abuf
);
755 ASSERT(cbytes
== max_blksz
);
759 * Start a transaction.
761 tx
= dmu_tx_create(zsb
->z_os
);
762 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
763 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
764 zfs_sa_upgrade_txholds(tx
, zp
);
765 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
767 if (error
== ERESTART
) {
774 dmu_return_arcbuf(abuf
);
779 * If zfs_range_lock() over-locked we grow the blocksize
780 * and then reduce the lock range. This will only happen
781 * on the first iteration since zfs_range_reduce() will
782 * shrink down r_len to the appropriate size.
784 if (rl
->r_len
== UINT64_MAX
) {
787 if (zp
->z_blksz
> max_blksz
) {
788 ASSERT(!ISP2(zp
->z_blksz
));
789 new_blksz
= MIN(end_size
, SPA_MAXBLOCKSIZE
);
791 new_blksz
= MIN(end_size
, max_blksz
);
793 zfs_grow_blocksize(zp
, new_blksz
, tx
);
794 zfs_range_reduce(rl
, woff
, n
);
798 * XXX - should we really limit each write to z_max_blksz?
799 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
801 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
804 tx_bytes
= uio
->uio_resid
;
805 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
807 tx_bytes
-= uio
->uio_resid
;
810 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
812 * If this is not a full block write, but we are
813 * extending the file past EOF and this data starts
814 * block-aligned, use assign_arcbuf(). Otherwise,
815 * write via dmu_write().
817 if (tx_bytes
< max_blksz
&& (!write_eof
||
818 aiov
->iov_base
!= abuf
->b_data
)) {
820 dmu_write(zsb
->z_os
, zp
->z_id
, woff
,
821 aiov
->iov_len
, aiov
->iov_base
, tx
);
822 dmu_return_arcbuf(abuf
);
823 xuio_stat_wbuf_copied();
825 ASSERT(xuio
|| tx_bytes
== max_blksz
);
826 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
829 ASSERT(tx_bytes
<= uio
->uio_resid
);
830 uioskip(uio
, tx_bytes
);
833 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
834 update_pages(ip
, woff
, tx_bytes
, zsb
->z_os
, zp
->z_id
);
837 * If we made no progress, we're done. If we made even
838 * partial progress, update the znode and ZIL accordingly.
841 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
842 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
849 * Clear Set-UID/Set-GID bits on successful write if not
850 * privileged and at least one of the excute bits is set.
852 * It would be nice to to this after all writes have
853 * been done, but that would still expose the ISUID/ISGID
854 * to another app after the partial write is committed.
856 * Note: we don't call zfs_fuid_map_id() here because
857 * user 0 is not an ephemeral uid.
859 mutex_enter(&zp
->z_acl_lock
);
860 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
861 (S_IXUSR
>> 6))) != 0 &&
862 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
863 secpolicy_vnode_setid_retain(cr
,
864 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
866 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
867 newmode
= zp
->z_mode
;
868 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
),
869 (void *)&newmode
, sizeof (uint64_t), tx
);
871 mutex_exit(&zp
->z_acl_lock
);
873 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
877 * Update the file size (zp_size) if it has changed;
878 * account for possible concurrent updates.
880 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
881 (void) atomic_cas_64(&zp
->z_size
, end_size
,
886 * If we are replaying and eof is non zero then force
887 * the file size to the specified eof. Note, there's no
888 * concurrency during replay.
890 if (zsb
->z_replay
&& zsb
->z_replay_eof
!= 0)
891 zp
->z_size
= zsb
->z_replay_eof
;
893 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
895 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
);
900 ASSERT(tx_bytes
== nbytes
);
904 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
907 zfs_range_unlock(rl
);
910 * If we're in replay mode, or we made no progress, return error.
911 * Otherwise, it's at least a partial write, so it's successful.
913 if (zsb
->z_replay
|| uio
->uio_resid
== start_resid
) {
918 if (ioflag
& (FSYNC
| FDSYNC
) ||
919 zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
920 zil_commit(zilog
, zp
->z_id
);
922 zfs_inode_update(zp
);
926 EXPORT_SYMBOL(zfs_write
);
929 iput_async(struct inode
*ip
, taskq_t
*taskq
)
931 ASSERT(atomic_read(&ip
->i_count
) > 0);
932 if (atomic_read(&ip
->i_count
) == 1)
933 taskq_dispatch(taskq
, (task_func_t
*)iput
, ip
, TQ_PUSHPAGE
);
939 zfs_get_done(zgd_t
*zgd
, int error
)
941 znode_t
*zp
= zgd
->zgd_private
;
942 objset_t
*os
= ZTOZSB(zp
)->z_os
;
945 dmu_buf_rele(zgd
->zgd_db
, zgd
);
947 zfs_range_unlock(zgd
->zgd_rl
);
950 * Release the vnode asynchronously as we currently have the
951 * txg stopped from syncing.
953 iput_async(ZTOI(zp
), dsl_pool_iput_taskq(dmu_objset_pool(os
)));
955 if (error
== 0 && zgd
->zgd_bp
)
956 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
958 kmem_free(zgd
, sizeof (zgd_t
));
962 static int zil_fault_io
= 0;
966 * Get data to generate a TX_WRITE intent log record.
969 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
972 objset_t
*os
= zsb
->z_os
;
974 uint64_t object
= lr
->lr_foid
;
975 uint64_t offset
= lr
->lr_offset
;
976 uint64_t size
= lr
->lr_length
;
977 blkptr_t
*bp
= &lr
->lr_blkptr
;
986 * Nothing to do if the file has been removed
988 if (zfs_zget(zsb
, object
, &zp
) != 0)
989 return (SET_ERROR(ENOENT
));
990 if (zp
->z_unlinked
) {
992 * Release the vnode asynchronously as we currently have the
993 * txg stopped from syncing.
995 iput_async(ZTOI(zp
), dsl_pool_iput_taskq(dmu_objset_pool(os
)));
996 return (SET_ERROR(ENOENT
));
999 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_PUSHPAGE
);
1000 zgd
->zgd_zilog
= zsb
->z_log
;
1001 zgd
->zgd_private
= zp
;
1004 * Write records come in two flavors: immediate and indirect.
1005 * For small writes it's cheaper to store the data with the
1006 * log record (immediate); for large writes it's cheaper to
1007 * sync the data and get a pointer to it (indirect) so that
1008 * we don't have to write the data twice.
1010 if (buf
!= NULL
) { /* immediate write */
1011 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
1012 /* test for truncation needs to be done while range locked */
1013 if (offset
>= zp
->z_size
) {
1014 error
= SET_ERROR(ENOENT
);
1016 error
= dmu_read(os
, object
, offset
, size
, buf
,
1017 DMU_READ_NO_PREFETCH
);
1019 ASSERT(error
== 0 || error
== ENOENT
);
1020 } else { /* indirect write */
1022 * Have to lock the whole block to ensure when it's
1023 * written out and it's checksum is being calculated
1024 * that no one can change the data. We need to re-check
1025 * blocksize after we get the lock in case it's changed!
1030 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1032 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
,
1034 if (zp
->z_blksz
== size
)
1037 zfs_range_unlock(zgd
->zgd_rl
);
1039 /* test for truncation needs to be done while range locked */
1040 if (lr
->lr_offset
>= zp
->z_size
)
1041 error
= SET_ERROR(ENOENT
);
1044 error
= SET_ERROR(EIO
);
1049 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1050 DMU_READ_NO_PREFETCH
);
1053 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
1055 ASSERT(BP_IS_HOLE(bp
));
1062 ASSERT(db
->db_offset
== offset
);
1063 ASSERT(db
->db_size
== size
);
1065 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1067 ASSERT(error
|| lr
->lr_length
<= zp
->z_blksz
);
1070 * On success, we need to wait for the write I/O
1071 * initiated by dmu_sync() to complete before we can
1072 * release this dbuf. We will finish everything up
1073 * in the zfs_get_done() callback.
1078 if (error
== EALREADY
) {
1079 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1085 zfs_get_done(zgd
, error
);
1092 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1094 znode_t
*zp
= ITOZ(ip
);
1095 zfs_sb_t
*zsb
= ITOZSB(ip
);
1101 if (flag
& V_ACE_MASK
)
1102 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1104 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1109 EXPORT_SYMBOL(zfs_access
);
1112 * Lookup an entry in a directory, or an extended attribute directory.
1113 * If it exists, return a held inode reference for it.
1115 * IN: dip - inode of directory to search.
1116 * nm - name of entry to lookup.
1117 * flags - LOOKUP_XATTR set if looking for an attribute.
1118 * cr - credentials of caller.
1119 * direntflags - directory lookup flags
1120 * realpnp - returned pathname.
1122 * OUT: ipp - inode of located entry, NULL if not found.
1124 * RETURN: 0 on success, error code on failure.
1131 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1132 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1134 znode_t
*zdp
= ITOZ(dip
);
1135 zfs_sb_t
*zsb
= ITOZSB(dip
);
1139 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1141 if (!S_ISDIR(dip
->i_mode
)) {
1142 return (SET_ERROR(ENOTDIR
));
1143 } else if (zdp
->z_sa_hdl
== NULL
) {
1144 return (SET_ERROR(EIO
));
1147 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1148 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1157 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1160 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1165 if (tvp
== DNLC_NO_VNODE
) {
1167 return (SET_ERROR(ENOENT
));
1170 return (specvp_check(vpp
, cr
));
1173 #endif /* HAVE_DNLC */
1182 if (flags
& LOOKUP_XATTR
) {
1184 * We don't allow recursive attributes..
1185 * Maybe someday we will.
1187 if (zdp
->z_pflags
& ZFS_XATTR
) {
1189 return (SET_ERROR(EINVAL
));
1192 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1198 * Do we have permission to get into attribute directory?
1201 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1211 if (!S_ISDIR(dip
->i_mode
)) {
1213 return (SET_ERROR(ENOTDIR
));
1217 * Check accessibility of directory.
1220 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1225 if (zsb
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1226 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1228 return (SET_ERROR(EILSEQ
));
1231 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1232 if ((error
== 0) && (*ipp
))
1233 zfs_inode_update(ITOZ(*ipp
));
1238 EXPORT_SYMBOL(zfs_lookup
);
1241 * Attempt to create a new entry in a directory. If the entry
1242 * already exists, truncate the file if permissible, else return
1243 * an error. Return the ip of the created or trunc'd file.
1245 * IN: dip - inode of directory to put new file entry in.
1246 * name - name of new file entry.
1247 * vap - attributes of new file.
1248 * excl - flag indicating exclusive or non-exclusive mode.
1249 * mode - mode to open file with.
1250 * cr - credentials of caller.
1251 * flag - large file flag [UNUSED].
1252 * vsecp - ACL to be set
1254 * OUT: ipp - inode of created or trunc'd entry.
1256 * RETURN: 0 on success, error code on failure.
1259 * dip - ctime|mtime updated if new entry created
1260 * ip - ctime|mtime always, atime if new
1265 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1266 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1268 znode_t
*zp
, *dzp
= ITOZ(dip
);
1269 zfs_sb_t
*zsb
= ITOZSB(dip
);
1277 zfs_acl_ids_t acl_ids
;
1278 boolean_t fuid_dirtied
;
1279 boolean_t have_acl
= B_FALSE
;
1282 * If we have an ephemeral id, ACL, or XVATTR then
1283 * make sure file system is at proper version
1289 if (zsb
->z_use_fuids
== B_FALSE
&&
1290 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1291 return (SET_ERROR(EINVAL
));
1298 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
1299 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1301 return (SET_ERROR(EILSEQ
));
1304 if (vap
->va_mask
& ATTR_XVATTR
) {
1305 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1306 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1314 if (*name
== '\0') {
1316 * Null component name refers to the directory itself.
1323 /* possible igrab(zp) */
1326 if (flag
& FIGNORECASE
)
1329 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1333 zfs_acl_ids_free(&acl_ids
);
1334 if (strcmp(name
, "..") == 0)
1335 error
= SET_ERROR(EISDIR
);
1345 * Create a new file object and update the directory
1348 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1350 zfs_acl_ids_free(&acl_ids
);
1355 * We only support the creation of regular files in
1356 * extended attribute directories.
1359 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1361 zfs_acl_ids_free(&acl_ids
);
1362 error
= SET_ERROR(EINVAL
);
1366 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1367 cr
, vsecp
, &acl_ids
)) != 0)
1371 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1372 zfs_acl_ids_free(&acl_ids
);
1373 error
= SET_ERROR(EDQUOT
);
1377 tx
= dmu_tx_create(os
);
1379 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1380 ZFS_SA_BASE_ATTR_SIZE
);
1382 fuid_dirtied
= zsb
->z_fuid_dirty
;
1384 zfs_fuid_txhold(zsb
, tx
);
1385 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1386 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1387 if (!zsb
->z_use_sa
&&
1388 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1389 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1390 0, acl_ids
.z_aclp
->z_acl_bytes
);
1392 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1394 zfs_dirent_unlock(dl
);
1395 if (error
== ERESTART
) {
1400 zfs_acl_ids_free(&acl_ids
);
1405 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1408 zfs_fuid_sync(zsb
, tx
);
1410 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1411 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1412 if (flag
& FIGNORECASE
)
1414 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1415 vsecp
, acl_ids
.z_fuidp
, vap
);
1416 zfs_acl_ids_free(&acl_ids
);
1419 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1422 zfs_acl_ids_free(&acl_ids
);
1426 * A directory entry already exists for this name.
1429 * Can't truncate an existing file if in exclusive mode.
1432 error
= SET_ERROR(EEXIST
);
1436 * Can't open a directory for writing.
1438 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1439 error
= SET_ERROR(EISDIR
);
1443 * Verify requested access to file.
1445 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1449 mutex_enter(&dzp
->z_lock
);
1451 mutex_exit(&dzp
->z_lock
);
1454 * Truncate regular files if requested.
1456 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1457 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1458 /* we can't hold any locks when calling zfs_freesp() */
1459 zfs_dirent_unlock(dl
);
1461 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1467 zfs_dirent_unlock(dl
);
1473 zfs_inode_update(dzp
);
1474 zfs_inode_update(zp
);
1478 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1479 zil_commit(zilog
, 0);
1484 EXPORT_SYMBOL(zfs_create
);
1487 * Remove an entry from a directory.
1489 * IN: dip - inode of directory to remove entry from.
1490 * name - name of entry to remove.
1491 * cr - credentials of caller.
1493 * RETURN: 0 if success
1494 * error code if failure
1498 * ip - ctime (if nlink > 0)
1501 uint64_t null_xattr
= 0;
1505 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
)
1507 znode_t
*zp
, *dzp
= ITOZ(dip
);
1510 zfs_sb_t
*zsb
= ITOZSB(dip
);
1513 uint64_t xattr_obj_unlinked
= 0;
1519 pathname_t
*realnmp
= NULL
;
1520 #ifdef HAVE_PN_UTILS
1522 #endif /* HAVE_PN_UTILS */
1530 #ifdef HAVE_PN_UTILS
1531 if (flags
& FIGNORECASE
) {
1536 #endif /* HAVE_PN_UTILS */
1542 * Attempt to lock directory; fail if entry doesn't exist.
1544 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1546 #ifdef HAVE_PN_UTILS
1549 #endif /* HAVE_PN_UTILS */
1556 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1561 * Need to use rmdir for removing directories.
1563 if (S_ISDIR(ip
->i_mode
)) {
1564 error
= SET_ERROR(EPERM
);
1570 dnlc_remove(dvp
, realnmp
->pn_buf
);
1572 dnlc_remove(dvp
, name
);
1573 #endif /* HAVE_DNLC */
1576 * We never delete the znode and always place it in the unlinked
1577 * set. The dentry cache will always hold the last reference and
1578 * is responsible for safely freeing the znode.
1581 tx
= dmu_tx_create(zsb
->z_os
);
1582 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1583 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1584 zfs_sa_upgrade_txholds(tx
, zp
);
1585 zfs_sa_upgrade_txholds(tx
, dzp
);
1587 /* are there any extended attributes? */
1588 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1589 &xattr_obj
, sizeof (xattr_obj
));
1590 if (error
== 0 && xattr_obj
) {
1591 error
= zfs_zget(zsb
, xattr_obj
, &xzp
);
1593 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
1594 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
1597 /* charge as an update -- would be nice not to charge at all */
1598 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1600 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1602 zfs_dirent_unlock(dl
);
1606 if (error
== ERESTART
) {
1611 #ifdef HAVE_PN_UTILS
1614 #endif /* HAVE_PN_UTILS */
1621 * Remove the directory entry.
1623 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1632 * Hold z_lock so that we can make sure that the ACL obj
1633 * hasn't changed. Could have been deleted due to
1636 mutex_enter(&zp
->z_lock
);
1637 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1638 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1639 mutex_exit(&zp
->z_lock
);
1640 zfs_unlinked_add(zp
, tx
);
1644 #ifdef HAVE_PN_UTILS
1645 if (flags
& FIGNORECASE
)
1647 #endif /* HAVE_PN_UTILS */
1648 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1652 #ifdef HAVE_PN_UTILS
1655 #endif /* HAVE_PN_UTILS */
1657 zfs_dirent_unlock(dl
);
1658 zfs_inode_update(dzp
);
1659 zfs_inode_update(zp
);
1661 zfs_inode_update(xzp
);
1667 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1668 zil_commit(zilog
, 0);
1673 EXPORT_SYMBOL(zfs_remove
);
1676 * Create a new directory and insert it into dip using the name
1677 * provided. Return a pointer to the inserted directory.
1679 * IN: dip - inode of directory to add subdir to.
1680 * dirname - name of new directory.
1681 * vap - attributes of new directory.
1682 * cr - credentials of caller.
1683 * vsecp - ACL to be set
1685 * OUT: ipp - inode of created directory.
1687 * RETURN: 0 if success
1688 * error code if failure
1691 * dip - ctime|mtime updated
1692 * ipp - ctime|mtime|atime updated
1696 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1697 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1699 znode_t
*zp
, *dzp
= ITOZ(dip
);
1700 zfs_sb_t
*zsb
= ITOZSB(dip
);
1708 gid_t gid
= crgetgid(cr
);
1709 zfs_acl_ids_t acl_ids
;
1710 boolean_t fuid_dirtied
;
1712 ASSERT(S_ISDIR(vap
->va_mode
));
1715 * If we have an ephemeral id, ACL, or XVATTR then
1716 * make sure file system is at proper version
1720 if (zsb
->z_use_fuids
== B_FALSE
&&
1721 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1722 return (SET_ERROR(EINVAL
));
1728 if (dzp
->z_pflags
& ZFS_XATTR
) {
1730 return (SET_ERROR(EINVAL
));
1733 if (zsb
->z_utf8
&& u8_validate(dirname
,
1734 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1736 return (SET_ERROR(EILSEQ
));
1738 if (flags
& FIGNORECASE
)
1741 if (vap
->va_mask
& ATTR_XVATTR
) {
1742 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1743 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1749 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1750 vsecp
, &acl_ids
)) != 0) {
1755 * First make sure the new directory doesn't exist.
1757 * Existence is checked first to make sure we don't return
1758 * EACCES instead of EEXIST which can cause some applications
1764 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1766 zfs_acl_ids_free(&acl_ids
);
1771 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1772 zfs_acl_ids_free(&acl_ids
);
1773 zfs_dirent_unlock(dl
);
1778 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1779 zfs_acl_ids_free(&acl_ids
);
1780 zfs_dirent_unlock(dl
);
1782 return (SET_ERROR(EDQUOT
));
1786 * Add a new entry to the directory.
1788 tx
= dmu_tx_create(zsb
->z_os
);
1789 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1790 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1791 fuid_dirtied
= zsb
->z_fuid_dirty
;
1793 zfs_fuid_txhold(zsb
, tx
);
1794 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1795 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1796 acl_ids
.z_aclp
->z_acl_bytes
);
1799 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1800 ZFS_SA_BASE_ATTR_SIZE
);
1802 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1804 zfs_dirent_unlock(dl
);
1805 if (error
== ERESTART
) {
1810 zfs_acl_ids_free(&acl_ids
);
1819 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1822 zfs_fuid_sync(zsb
, tx
);
1825 * Now put new name in parent dir.
1827 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1831 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1832 if (flags
& FIGNORECASE
)
1834 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1835 acl_ids
.z_fuidp
, vap
);
1837 zfs_acl_ids_free(&acl_ids
);
1841 zfs_dirent_unlock(dl
);
1843 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1844 zil_commit(zilog
, 0);
1846 zfs_inode_update(dzp
);
1847 zfs_inode_update(zp
);
1851 EXPORT_SYMBOL(zfs_mkdir
);
1854 * Remove a directory subdir entry. If the current working
1855 * directory is the same as the subdir to be removed, the
1858 * IN: dip - inode of directory to remove from.
1859 * name - name of directory to be removed.
1860 * cwd - inode of current working directory.
1861 * cr - credentials of caller.
1862 * flags - case flags
1864 * RETURN: 0 on success, error code on failure.
1867 * dip - ctime|mtime updated
1871 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
1874 znode_t
*dzp
= ITOZ(dip
);
1877 zfs_sb_t
*zsb
= ITOZSB(dip
);
1888 if (flags
& FIGNORECASE
)
1894 * Attempt to lock directory; fail if entry doesn't exist.
1896 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1904 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1908 if (!S_ISDIR(ip
->i_mode
)) {
1909 error
= SET_ERROR(ENOTDIR
);
1914 error
= SET_ERROR(EINVAL
);
1919 * Grab a lock on the directory to make sure that noone is
1920 * trying to add (or lookup) entries while we are removing it.
1922 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1925 * Grab a lock on the parent pointer to make sure we play well
1926 * with the treewalk and directory rename code.
1928 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1930 tx
= dmu_tx_create(zsb
->z_os
);
1931 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1932 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1933 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1934 zfs_sa_upgrade_txholds(tx
, zp
);
1935 zfs_sa_upgrade_txholds(tx
, dzp
);
1936 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
1938 rw_exit(&zp
->z_parent_lock
);
1939 rw_exit(&zp
->z_name_lock
);
1940 zfs_dirent_unlock(dl
);
1942 if (error
== ERESTART
) {
1952 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1955 uint64_t txtype
= TX_RMDIR
;
1956 if (flags
& FIGNORECASE
)
1958 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
1963 rw_exit(&zp
->z_parent_lock
);
1964 rw_exit(&zp
->z_name_lock
);
1966 zfs_dirent_unlock(dl
);
1968 zfs_inode_update(dzp
);
1969 zfs_inode_update(zp
);
1972 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1973 zil_commit(zilog
, 0);
1978 EXPORT_SYMBOL(zfs_rmdir
);
1981 * Read as many directory entries as will fit into the provided
1982 * dirent buffer from the given directory cursor position.
1984 * IN: ip - inode of directory to read.
1985 * dirent - buffer for directory entries.
1987 * OUT: dirent - filler buffer of directory entries.
1989 * RETURN: 0 if success
1990 * error code if failure
1993 * ip - atime updated
1995 * Note that the low 4 bits of the cookie returned by zap is always zero.
1996 * This allows us to use the low range for "special" directory entries:
1997 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
1998 * we use the offset 2 for the '.zfs' directory.
2002 zfs_readdir(struct inode
*ip
, struct dir_context
*ctx
, cred_t
*cr
)
2004 znode_t
*zp
= ITOZ(ip
);
2005 zfs_sb_t
*zsb
= ITOZSB(ip
);
2008 zap_attribute_t zap
;
2014 uint64_t offset
; /* must be unsigned; checks for < 1 */
2019 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
2020 &parent
, sizeof (parent
))) != 0)
2024 * Quit if directory has been removed (posix)
2032 prefetch
= zp
->z_zn_prefetch
;
2035 * Initialize the iterator cursor.
2039 * Start iteration from the beginning of the directory.
2041 zap_cursor_init(&zc
, os
, zp
->z_id
);
2044 * The offset is a serialized cursor.
2046 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2050 * Transform to file-system independent format
2055 * Special case `.', `..', and `.zfs'.
2058 (void) strcpy(zap
.za_name
, ".");
2059 zap
.za_normalization_conflict
= 0;
2062 } else if (offset
== 1) {
2063 (void) strcpy(zap
.za_name
, "..");
2064 zap
.za_normalization_conflict
= 0;
2067 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2068 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2069 zap
.za_normalization_conflict
= 0;
2070 objnum
= ZFSCTL_INO_ROOT
;
2076 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2077 if (error
== ENOENT
)
2084 * Allow multiple entries provided the first entry is
2085 * the object id. Non-zpl consumers may safely make
2086 * use of the additional space.
2088 * XXX: This should be a feature flag for compatibility
2090 if (zap
.za_integer_length
!= 8 ||
2091 zap
.za_num_integers
== 0) {
2092 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2093 "entry, obj = %lld, offset = %lld, "
2094 "length = %d, num = %lld\n",
2095 (u_longlong_t
)zp
->z_id
,
2096 (u_longlong_t
)offset
,
2097 zap
.za_integer_length
,
2098 (u_longlong_t
)zap
.za_num_integers
);
2099 error
= SET_ERROR(ENXIO
);
2103 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2104 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2107 done
= !dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2112 /* Prefetch znode */
2114 dmu_prefetch(os
, objnum
, 0, 0);
2118 * Move to the next entry, fill in the previous offset.
2120 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2121 zap_cursor_advance(&zc
);
2122 offset
= zap_cursor_serialize(&zc
);
2128 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2131 zap_cursor_fini(&zc
);
2132 if (error
== ENOENT
)
2135 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
2136 zfs_inode_update(zp
);
2143 EXPORT_SYMBOL(zfs_readdir
);
2145 ulong_t zfs_fsync_sync_cnt
= 4;
2148 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2150 znode_t
*zp
= ITOZ(ip
);
2151 zfs_sb_t
*zsb
= ITOZSB(ip
);
2153 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2155 if (zsb
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2158 zil_commit(zsb
->z_log
, zp
->z_id
);
2163 EXPORT_SYMBOL(zfs_fsync
);
2167 * Get the requested file attributes and place them in the provided
2170 * IN: ip - inode of file.
2171 * vap - va_mask identifies requested attributes.
2172 * If ATTR_XVATTR set, then optional attrs are requested
2173 * flags - ATTR_NOACLCHECK (CIFS server context)
2174 * cr - credentials of caller.
2176 * OUT: vap - attribute values.
2178 * RETURN: 0 (always succeeds)
2182 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2184 znode_t
*zp
= ITOZ(ip
);
2185 zfs_sb_t
*zsb
= ITOZSB(ip
);
2188 uint64_t mtime
[2], ctime
[2];
2189 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2190 xoptattr_t
*xoap
= NULL
;
2191 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2192 sa_bulk_attr_t bulk
[2];
2198 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2200 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
2201 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
2203 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2209 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2210 * Also, if we are the owner don't bother, since owner should
2211 * always be allowed to read basic attributes of file.
2213 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2214 (vap
->va_uid
!= crgetuid(cr
))) {
2215 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2223 * Return all attributes. It's cheaper to provide the answer
2224 * than to determine whether we were asked the question.
2227 mutex_enter(&zp
->z_lock
);
2228 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2229 vap
->va_mode
= zp
->z_mode
;
2230 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2231 vap
->va_nodeid
= zp
->z_id
;
2232 if ((zp
->z_id
== zsb
->z_root
) && zfs_show_ctldir(zp
))
2233 links
= zp
->z_links
+ 1;
2235 links
= zp
->z_links
;
2236 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2237 vap
->va_size
= i_size_read(ip
);
2238 vap
->va_rdev
= ip
->i_rdev
;
2239 vap
->va_seq
= ip
->i_generation
;
2242 * Add in any requested optional attributes and the create time.
2243 * Also set the corresponding bits in the returned attribute bitmap.
2245 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zsb
->z_use_fuids
) {
2246 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2248 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2249 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2252 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2253 xoap
->xoa_readonly
=
2254 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2255 XVA_SET_RTN(xvap
, XAT_READONLY
);
2258 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2260 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2261 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2264 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2266 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2267 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2270 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2271 xoap
->xoa_nounlink
=
2272 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2273 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2276 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2277 xoap
->xoa_immutable
=
2278 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2279 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2282 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2283 xoap
->xoa_appendonly
=
2284 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2285 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2288 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2290 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2291 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2294 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2296 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2297 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2300 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2301 xoap
->xoa_av_quarantined
=
2302 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2303 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2306 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2307 xoap
->xoa_av_modified
=
2308 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2309 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2312 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2313 S_ISREG(ip
->i_mode
)) {
2314 zfs_sa_get_scanstamp(zp
, xvap
);
2317 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2320 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zsb
),
2321 times
, sizeof (times
));
2322 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2323 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2326 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2327 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2328 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2330 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2331 xoap
->xoa_generation
= zp
->z_gen
;
2332 XVA_SET_RTN(xvap
, XAT_GEN
);
2335 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2337 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2338 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2341 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2343 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2344 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2348 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2349 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2350 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2352 mutex_exit(&zp
->z_lock
);
2354 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2356 if (zp
->z_blksz
== 0) {
2358 * Block size hasn't been set; suggest maximal I/O transfers.
2360 vap
->va_blksize
= zsb
->z_max_blksz
;
2366 EXPORT_SYMBOL(zfs_getattr
);
2369 * Get the basic file attributes and place them in the provided kstat
2370 * structure. The inode is assumed to be the authoritative source
2371 * for most of the attributes. However, the znode currently has the
2372 * authoritative atime, blksize, and block count.
2374 * IN: ip - inode of file.
2376 * OUT: sp - kstat values.
2378 * RETURN: 0 (always succeeds)
2382 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2384 znode_t
*zp
= ITOZ(ip
);
2385 zfs_sb_t
*zsb
= ITOZSB(ip
);
2390 mutex_enter(&zp
->z_lock
);
2392 generic_fillattr(ip
, sp
);
2393 ZFS_TIME_DECODE(&sp
->atime
, zp
->z_atime
);
2395 sa_object_size(zp
->z_sa_hdl
, (uint32_t *)&sp
->blksize
, &sp
->blocks
);
2396 if (unlikely(zp
->z_blksz
== 0)) {
2398 * Block size hasn't been set; suggest maximal I/O transfers.
2400 sp
->blksize
= zsb
->z_max_blksz
;
2403 mutex_exit(&zp
->z_lock
);
2409 EXPORT_SYMBOL(zfs_getattr_fast
);
2412 * Set the file attributes to the values contained in the
2415 * IN: ip - inode of file to be modified.
2416 * vap - new attribute values.
2417 * If ATTR_XVATTR set, then optional attrs are being set
2418 * flags - ATTR_UTIME set if non-default time values provided.
2419 * - ATTR_NOACLCHECK (CIFS context only).
2420 * cr - credentials of caller.
2422 * RETURN: 0 if success
2423 * error code if failure
2426 * ip - ctime updated, mtime updated if size changed.
2430 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2432 znode_t
*zp
= ITOZ(ip
);
2433 zfs_sb_t
*zsb
= ITOZSB(ip
);
2437 xvattr_t
*tmpxvattr
;
2438 uint_t mask
= vap
->va_mask
;
2439 uint_t saved_mask
= 0;
2442 uint64_t new_uid
, new_gid
;
2444 uint64_t mtime
[2], ctime
[2];
2446 int need_policy
= FALSE
;
2448 zfs_fuid_info_t
*fuidp
= NULL
;
2449 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2452 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2453 boolean_t fuid_dirtied
= B_FALSE
;
2454 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2455 int count
= 0, xattr_count
= 0;
2466 * Make sure that if we have ephemeral uid/gid or xvattr specified
2467 * that file system is at proper version level
2470 if (zsb
->z_use_fuids
== B_FALSE
&&
2471 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2472 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2473 (mask
& ATTR_XVATTR
))) {
2475 return (SET_ERROR(EINVAL
));
2478 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2480 return (SET_ERROR(EISDIR
));
2483 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2485 return (SET_ERROR(EINVAL
));
2489 * If this is an xvattr_t, then get a pointer to the structure of
2490 * optional attributes. If this is NULL, then we have a vattr_t.
2492 xoap
= xva_getxoptattr(xvap
);
2494 tmpxvattr
= kmem_alloc(sizeof(xvattr_t
), KM_SLEEP
);
2495 xva_init(tmpxvattr
);
2497 bulk
= kmem_alloc(sizeof(sa_bulk_attr_t
) * 7, KM_SLEEP
);
2498 xattr_bulk
= kmem_alloc(sizeof(sa_bulk_attr_t
) * 7, KM_SLEEP
);
2501 * Immutable files can only alter immutable bit and atime
2503 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2504 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2505 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2510 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2516 * Verify timestamps doesn't overflow 32 bits.
2517 * ZFS can handle large timestamps, but 32bit syscalls can't
2518 * handle times greater than 2039. This check should be removed
2519 * once large timestamps are fully supported.
2521 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2522 if (((mask
& ATTR_ATIME
) && TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2523 ((mask
& ATTR_MTIME
) && TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2533 /* Can this be moved to before the top label? */
2534 if (zfs_is_readonly(zsb
)) {
2540 * First validate permissions
2543 if (mask
& ATTR_SIZE
) {
2544 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2548 truncate_setsize(ip
, vap
->va_size
);
2551 * XXX - Note, we are not providing any open
2552 * mode flags here (like FNDELAY), so we may
2553 * block if there are locks present... this
2554 * should be addressed in openat().
2556 /* XXX - would it be OK to generate a log record here? */
2557 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2562 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2563 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2564 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2565 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2566 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2567 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2568 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2569 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2570 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2574 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2575 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2580 * NOTE: even if a new mode is being set,
2581 * we may clear S_ISUID/S_ISGID bits.
2584 if (!(mask
& ATTR_MODE
))
2585 vap
->va_mode
= zp
->z_mode
;
2588 * Take ownership or chgrp to group we are a member of
2591 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2592 take_group
= (mask
& ATTR_GID
) &&
2593 zfs_groupmember(zsb
, vap
->va_gid
, cr
);
2596 * If both ATTR_UID and ATTR_GID are set then take_owner and
2597 * take_group must both be set in order to allow taking
2600 * Otherwise, send the check through secpolicy_vnode_setattr()
2604 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2605 take_owner
&& take_group
) ||
2606 ((idmask
== ATTR_UID
) && take_owner
) ||
2607 ((idmask
== ATTR_GID
) && take_group
)) {
2608 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2609 skipaclchk
, cr
) == 0) {
2611 * Remove setuid/setgid for non-privileged users
2613 (void) secpolicy_setid_clear(vap
, cr
);
2614 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2623 mutex_enter(&zp
->z_lock
);
2624 oldva
.va_mode
= zp
->z_mode
;
2625 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2626 if (mask
& ATTR_XVATTR
) {
2628 * Update xvattr mask to include only those attributes
2629 * that are actually changing.
2631 * the bits will be restored prior to actually setting
2632 * the attributes so the caller thinks they were set.
2634 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2635 if (xoap
->xoa_appendonly
!=
2636 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2639 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2640 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2644 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2645 if (xoap
->xoa_nounlink
!=
2646 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2649 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2650 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2654 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2655 if (xoap
->xoa_immutable
!=
2656 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2659 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2660 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2664 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2665 if (xoap
->xoa_nodump
!=
2666 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2669 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2670 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2674 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2675 if (xoap
->xoa_av_modified
!=
2676 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2679 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2680 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2684 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2685 if ((!S_ISREG(ip
->i_mode
) &&
2686 xoap
->xoa_av_quarantined
) ||
2687 xoap
->xoa_av_quarantined
!=
2688 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2691 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2692 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2696 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2697 mutex_exit(&zp
->z_lock
);
2702 if (need_policy
== FALSE
&&
2703 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2704 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2709 mutex_exit(&zp
->z_lock
);
2711 if (mask
& ATTR_MODE
) {
2712 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2713 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2718 trim_mask
|= ATTR_MODE
;
2726 * If trim_mask is set then take ownership
2727 * has been granted or write_acl is present and user
2728 * has the ability to modify mode. In that case remove
2729 * UID|GID and or MODE from mask so that
2730 * secpolicy_vnode_setattr() doesn't revoke it.
2734 saved_mask
= vap
->va_mask
;
2735 vap
->va_mask
&= ~trim_mask
;
2737 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2738 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2743 vap
->va_mask
|= saved_mask
;
2747 * secpolicy_vnode_setattr, or take ownership may have
2750 mask
= vap
->va_mask
;
2752 if ((mask
& (ATTR_UID
| ATTR_GID
))) {
2753 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
2754 &xattr_obj
, sizeof (xattr_obj
));
2756 if (err
== 0 && xattr_obj
) {
2757 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2761 if (mask
& ATTR_UID
) {
2762 new_uid
= zfs_fuid_create(zsb
,
2763 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2764 if (new_uid
!= zp
->z_uid
&&
2765 zfs_fuid_overquota(zsb
, B_FALSE
, new_uid
)) {
2773 if (mask
& ATTR_GID
) {
2774 new_gid
= zfs_fuid_create(zsb
, (uint64_t)vap
->va_gid
,
2775 cr
, ZFS_GROUP
, &fuidp
);
2776 if (new_gid
!= zp
->z_gid
&&
2777 zfs_fuid_overquota(zsb
, B_TRUE
, new_gid
)) {
2785 tx
= dmu_tx_create(zsb
->z_os
);
2787 if (mask
& ATTR_MODE
) {
2788 uint64_t pmode
= zp
->z_mode
;
2790 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2792 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
2794 mutex_enter(&zp
->z_lock
);
2795 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2797 * Are we upgrading ACL from old V0 format
2800 if (zsb
->z_version
>= ZPL_VERSION_FUID
&&
2801 zfs_znode_acl_version(zp
) ==
2802 ZFS_ACL_VERSION_INITIAL
) {
2803 dmu_tx_hold_free(tx
, acl_obj
, 0,
2805 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2806 0, aclp
->z_acl_bytes
);
2808 dmu_tx_hold_write(tx
, acl_obj
, 0,
2811 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2812 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2813 0, aclp
->z_acl_bytes
);
2815 mutex_exit(&zp
->z_lock
);
2816 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2818 if ((mask
& ATTR_XVATTR
) &&
2819 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2820 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2822 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2826 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2829 fuid_dirtied
= zsb
->z_fuid_dirty
;
2831 zfs_fuid_txhold(zsb
, tx
);
2833 zfs_sa_upgrade_txholds(tx
, zp
);
2835 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
2837 if (err
== ERESTART
)
2844 * Set each attribute requested.
2845 * We group settings according to the locks they need to acquire.
2847 * Note: you cannot set ctime directly, although it will be
2848 * updated as a side-effect of calling this function.
2852 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2853 mutex_enter(&zp
->z_acl_lock
);
2854 mutex_enter(&zp
->z_lock
);
2856 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
2857 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2860 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2861 mutex_enter(&attrzp
->z_acl_lock
);
2862 mutex_enter(&attrzp
->z_lock
);
2863 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2864 SA_ZPL_FLAGS(zsb
), NULL
, &attrzp
->z_pflags
,
2865 sizeof (attrzp
->z_pflags
));
2868 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2870 if (mask
& ATTR_UID
) {
2871 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
2872 &new_uid
, sizeof (new_uid
));
2873 zp
->z_uid
= new_uid
;
2875 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2876 SA_ZPL_UID(zsb
), NULL
, &new_uid
,
2878 attrzp
->z_uid
= new_uid
;
2882 if (mask
& ATTR_GID
) {
2883 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
),
2884 NULL
, &new_gid
, sizeof (new_gid
));
2885 zp
->z_gid
= new_gid
;
2887 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2888 SA_ZPL_GID(zsb
), NULL
, &new_gid
,
2890 attrzp
->z_gid
= new_gid
;
2893 if (!(mask
& ATTR_MODE
)) {
2894 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
),
2895 NULL
, &new_mode
, sizeof (new_mode
));
2896 new_mode
= zp
->z_mode
;
2898 err
= zfs_acl_chown_setattr(zp
);
2901 err
= zfs_acl_chown_setattr(attrzp
);
2906 if (mask
& ATTR_MODE
) {
2907 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
2908 &new_mode
, sizeof (new_mode
));
2909 zp
->z_mode
= new_mode
;
2910 ASSERT3P(aclp
, !=, NULL
);
2911 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2913 if (zp
->z_acl_cached
)
2914 zfs_acl_free(zp
->z_acl_cached
);
2915 zp
->z_acl_cached
= aclp
;
2920 if (mask
& ATTR_ATIME
) {
2921 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
2922 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
2923 &zp
->z_atime
, sizeof (zp
->z_atime
));
2926 if (mask
& ATTR_MTIME
) {
2927 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2928 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
,
2929 mtime
, sizeof (mtime
));
2932 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2933 if (mask
& ATTR_SIZE
&& !(mask
& ATTR_MTIME
)) {
2934 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
),
2935 NULL
, mtime
, sizeof (mtime
));
2936 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2937 &ctime
, sizeof (ctime
));
2938 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
2940 } else if (mask
!= 0) {
2941 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2942 &ctime
, sizeof (ctime
));
2943 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
2946 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2947 SA_ZPL_CTIME(zsb
), NULL
,
2948 &ctime
, sizeof (ctime
));
2949 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2950 mtime
, ctime
, B_TRUE
);
2954 * Do this after setting timestamps to prevent timestamp
2955 * update from toggling bit
2958 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2961 * restore trimmed off masks
2962 * so that return masks can be set for caller.
2965 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2966 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2968 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2969 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2971 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2972 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2974 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2975 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2977 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2978 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2980 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
2981 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2984 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2985 ASSERT(S_ISREG(ip
->i_mode
));
2987 zfs_xvattr_set(zp
, xvap
, tx
);
2991 zfs_fuid_sync(zsb
, tx
);
2994 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
2996 mutex_exit(&zp
->z_lock
);
2997 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2998 mutex_exit(&zp
->z_acl_lock
);
3001 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3002 mutex_exit(&attrzp
->z_acl_lock
);
3003 mutex_exit(&attrzp
->z_lock
);
3006 if (err
== 0 && attrzp
) {
3007 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3018 zfs_fuid_info_free(fuidp
);
3024 if (err
== ERESTART
)
3027 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3029 zfs_inode_update(zp
);
3033 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3034 zil_commit(zilog
, 0);
3037 kmem_free(xattr_bulk
, sizeof(sa_bulk_attr_t
) * 7);
3038 kmem_free(bulk
, sizeof(sa_bulk_attr_t
) * 7);
3039 kmem_free(tmpxvattr
, sizeof(xvattr_t
));
3043 EXPORT_SYMBOL(zfs_setattr
);
3045 typedef struct zfs_zlock
{
3046 krwlock_t
*zl_rwlock
; /* lock we acquired */
3047 znode_t
*zl_znode
; /* znode we held */
3048 struct zfs_zlock
*zl_next
; /* next in list */
3052 * Drop locks and release vnodes that were held by zfs_rename_lock().
3055 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3059 while ((zl
= *zlpp
) != NULL
) {
3060 if (zl
->zl_znode
!= NULL
)
3061 iput(ZTOI(zl
->zl_znode
));
3062 rw_exit(zl
->zl_rwlock
);
3063 *zlpp
= zl
->zl_next
;
3064 kmem_free(zl
, sizeof (*zl
));
3069 * Search back through the directory tree, using the ".." entries.
3070 * Lock each directory in the chain to prevent concurrent renames.
3071 * Fail any attempt to move a directory into one of its own descendants.
3072 * XXX - z_parent_lock can overlap with map or grow locks
3075 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3079 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3080 uint64_t oidp
= zp
->z_id
;
3081 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3082 krw_t rw
= RW_WRITER
;
3085 * First pass write-locks szp and compares to zp->z_id.
3086 * Later passes read-lock zp and compare to zp->z_parent.
3089 if (!rw_tryenter(rwlp
, rw
)) {
3091 * Another thread is renaming in this path.
3092 * Note that if we are a WRITER, we don't have any
3093 * parent_locks held yet.
3095 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3097 * Drop our locks and restart
3099 zfs_rename_unlock(&zl
);
3103 rwlp
= &szp
->z_parent_lock
;
3108 * Wait for other thread to drop its locks
3114 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3115 zl
->zl_rwlock
= rwlp
;
3116 zl
->zl_znode
= NULL
;
3117 zl
->zl_next
= *zlpp
;
3120 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3121 return (SET_ERROR(EINVAL
));
3123 if (oidp
== rootid
) /* We've hit the top */
3126 if (rw
== RW_READER
) { /* i.e. not the first pass */
3127 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3132 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3133 &oidp
, sizeof (oidp
));
3134 rwlp
= &zp
->z_parent_lock
;
3137 } while (zp
->z_id
!= sdzp
->z_id
);
3143 * Move an entry from the provided source directory to the target
3144 * directory. Change the entry name as indicated.
3146 * IN: sdip - Source directory containing the "old entry".
3147 * snm - Old entry name.
3148 * tdip - Target directory to contain the "new entry".
3149 * tnm - New entry name.
3150 * cr - credentials of caller.
3151 * flags - case flags
3153 * RETURN: 0 on success, error code on failure.
3156 * sdip,tdip - ctime|mtime updated
3160 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3161 cred_t
*cr
, int flags
)
3163 znode_t
*tdzp
, *szp
, *tzp
;
3164 znode_t
*sdzp
= ITOZ(sdip
);
3165 zfs_sb_t
*zsb
= ITOZSB(sdip
);
3167 zfs_dirlock_t
*sdl
, *tdl
;
3170 int cmp
, serr
, terr
;
3175 ZFS_VERIFY_ZP(sdzp
);
3178 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3180 return (SET_ERROR(EXDEV
));
3184 ZFS_VERIFY_ZP(tdzp
);
3185 if (zsb
->z_utf8
&& u8_validate(tnm
,
3186 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3188 return (SET_ERROR(EILSEQ
));
3191 if (flags
& FIGNORECASE
)
3200 * This is to prevent the creation of links into attribute space
3201 * by renaming a linked file into/outof an attribute directory.
3202 * See the comment in zfs_link() for why this is considered bad.
3204 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3206 return (SET_ERROR(EINVAL
));
3210 * Lock source and target directory entries. To prevent deadlock,
3211 * a lock ordering must be defined. We lock the directory with
3212 * the smallest object id first, or if it's a tie, the one with
3213 * the lexically first name.
3215 if (sdzp
->z_id
< tdzp
->z_id
) {
3217 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3221 * First compare the two name arguments without
3222 * considering any case folding.
3224 int nofold
= (zsb
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3226 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3227 ASSERT(error
== 0 || !zsb
->z_utf8
);
3230 * POSIX: "If the old argument and the new argument
3231 * both refer to links to the same existing file,
3232 * the rename() function shall return successfully
3233 * and perform no other action."
3239 * If the file system is case-folding, then we may
3240 * have some more checking to do. A case-folding file
3241 * system is either supporting mixed case sensitivity
3242 * access or is completely case-insensitive. Note
3243 * that the file system is always case preserving.
3245 * In mixed sensitivity mode case sensitive behavior
3246 * is the default. FIGNORECASE must be used to
3247 * explicitly request case insensitive behavior.
3249 * If the source and target names provided differ only
3250 * by case (e.g., a request to rename 'tim' to 'Tim'),
3251 * we will treat this as a special case in the
3252 * case-insensitive mode: as long as the source name
3253 * is an exact match, we will allow this to proceed as
3254 * a name-change request.
3256 if ((zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
3257 (zsb
->z_case
== ZFS_CASE_MIXED
&&
3258 flags
& FIGNORECASE
)) &&
3259 u8_strcmp(snm
, tnm
, 0, zsb
->z_norm
, U8_UNICODE_LATEST
,
3262 * case preserving rename request, require exact
3271 * If the source and destination directories are the same, we should
3272 * grab the z_name_lock of that directory only once.
3276 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3280 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3281 ZEXISTS
| zflg
, NULL
, NULL
);
3282 terr
= zfs_dirent_lock(&tdl
,
3283 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3285 terr
= zfs_dirent_lock(&tdl
,
3286 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3287 serr
= zfs_dirent_lock(&sdl
,
3288 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3294 * Source entry invalid or not there.
3297 zfs_dirent_unlock(tdl
);
3303 rw_exit(&sdzp
->z_name_lock
);
3305 if (strcmp(snm
, "..") == 0)
3311 zfs_dirent_unlock(sdl
);
3315 rw_exit(&sdzp
->z_name_lock
);
3317 if (strcmp(tnm
, "..") == 0)
3324 * Must have write access at the source to remove the old entry
3325 * and write access at the target to create the new entry.
3326 * Note that if target and source are the same, this can be
3327 * done in a single check.
3330 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3333 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3335 * Check to make sure rename is valid.
3336 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3338 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3343 * Does target exist?
3347 * Source and target must be the same type.
3349 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3350 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3351 error
= SET_ERROR(ENOTDIR
);
3355 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3356 error
= SET_ERROR(EISDIR
);
3361 * POSIX dictates that when the source and target
3362 * entries refer to the same file object, rename
3363 * must do nothing and exit without error.
3365 if (szp
->z_id
== tzp
->z_id
) {
3371 tx
= dmu_tx_create(zsb
->z_os
);
3372 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3373 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3374 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3375 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3377 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3378 zfs_sa_upgrade_txholds(tx
, tdzp
);
3381 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3382 zfs_sa_upgrade_txholds(tx
, tzp
);
3385 zfs_sa_upgrade_txholds(tx
, szp
);
3386 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
3387 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3390 zfs_rename_unlock(&zl
);
3391 zfs_dirent_unlock(sdl
);
3392 zfs_dirent_unlock(tdl
);
3395 rw_exit(&sdzp
->z_name_lock
);
3400 if (error
== ERESTART
) {
3410 if (tzp
) /* Attempt to remove the existing target */
3411 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3414 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3416 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3418 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
3419 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3422 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3424 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3425 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3426 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3429 * At this point, we have successfully created
3430 * the target name, but have failed to remove
3431 * the source name. Since the create was done
3432 * with the ZRENAMING flag, there are
3433 * complications; for one, the link count is
3434 * wrong. The easiest way to deal with this
3435 * is to remove the newly created target, and
3436 * return the original error. This must
3437 * succeed; fortunately, it is very unlikely to
3438 * fail, since we just created it.
3440 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3441 ZRENAMING
, NULL
), ==, 0);
3449 zfs_rename_unlock(&zl
);
3451 zfs_dirent_unlock(sdl
);
3452 zfs_dirent_unlock(tdl
);
3454 zfs_inode_update(sdzp
);
3456 rw_exit(&sdzp
->z_name_lock
);
3459 zfs_inode_update(tdzp
);
3461 zfs_inode_update(szp
);
3464 zfs_inode_update(tzp
);
3468 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3469 zil_commit(zilog
, 0);
3474 EXPORT_SYMBOL(zfs_rename
);
3477 * Insert the indicated symbolic reference entry into the directory.
3479 * IN: dip - Directory to contain new symbolic link.
3480 * link - Name for new symlink entry.
3481 * vap - Attributes of new entry.
3482 * target - Target path of new symlink.
3484 * cr - credentials of caller.
3485 * flags - case flags
3487 * RETURN: 0 on success, error code on failure.
3490 * dip - ctime|mtime updated
3494 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
3495 struct inode
**ipp
, cred_t
*cr
, int flags
)
3497 znode_t
*zp
, *dzp
= ITOZ(dip
);
3500 zfs_sb_t
*zsb
= ITOZSB(dip
);
3502 uint64_t len
= strlen(link
);
3505 zfs_acl_ids_t acl_ids
;
3506 boolean_t fuid_dirtied
;
3507 uint64_t txtype
= TX_SYMLINK
;
3509 ASSERT(S_ISLNK(vap
->va_mode
));
3515 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
3516 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3518 return (SET_ERROR(EILSEQ
));
3520 if (flags
& FIGNORECASE
)
3523 if (len
> MAXPATHLEN
) {
3525 return (SET_ERROR(ENAMETOOLONG
));
3528 if ((error
= zfs_acl_ids_create(dzp
, 0,
3529 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3537 * Attempt to lock directory; fail if entry already exists.
3539 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3541 zfs_acl_ids_free(&acl_ids
);
3546 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3547 zfs_acl_ids_free(&acl_ids
);
3548 zfs_dirent_unlock(dl
);
3553 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
3554 zfs_acl_ids_free(&acl_ids
);
3555 zfs_dirent_unlock(dl
);
3557 return (SET_ERROR(EDQUOT
));
3559 tx
= dmu_tx_create(zsb
->z_os
);
3560 fuid_dirtied
= zsb
->z_fuid_dirty
;
3561 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3562 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3563 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3564 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3565 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3566 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3567 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3568 acl_ids
.z_aclp
->z_acl_bytes
);
3571 zfs_fuid_txhold(zsb
, tx
);
3572 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3574 zfs_dirent_unlock(dl
);
3575 if (error
== ERESTART
) {
3580 zfs_acl_ids_free(&acl_ids
);
3587 * Create a new object for the symlink.
3588 * for version 4 ZPL datsets the symlink will be an SA attribute
3590 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3593 zfs_fuid_sync(zsb
, tx
);
3595 mutex_enter(&zp
->z_lock
);
3597 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
),
3600 zfs_sa_symlink(zp
, link
, len
, tx
);
3601 mutex_exit(&zp
->z_lock
);
3604 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
3605 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3607 * Insert the new object into the directory.
3609 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3611 if (flags
& FIGNORECASE
)
3613 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3615 zfs_inode_update(dzp
);
3616 zfs_inode_update(zp
);
3618 zfs_acl_ids_free(&acl_ids
);
3622 zfs_dirent_unlock(dl
);
3626 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3627 zil_commit(zilog
, 0);
3632 EXPORT_SYMBOL(zfs_symlink
);
3635 * Return, in the buffer contained in the provided uio structure,
3636 * the symbolic path referred to by ip.
3638 * IN: ip - inode of symbolic link
3639 * uio - structure to contain the link path.
3640 * cr - credentials of caller.
3642 * RETURN: 0 if success
3643 * error code if failure
3646 * ip - atime updated
3650 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
3652 znode_t
*zp
= ITOZ(ip
);
3653 zfs_sb_t
*zsb
= ITOZSB(ip
);
3659 mutex_enter(&zp
->z_lock
);
3661 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3662 SA_ZPL_SYMLINK(zsb
), uio
);
3664 error
= zfs_sa_readlink(zp
, uio
);
3665 mutex_exit(&zp
->z_lock
);
3667 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
3668 zfs_inode_update(zp
);
3672 EXPORT_SYMBOL(zfs_readlink
);
3675 * Insert a new entry into directory tdip referencing sip.
3677 * IN: tdip - Directory to contain new entry.
3678 * sip - inode of new entry.
3679 * name - name of new entry.
3680 * cr - credentials of caller.
3682 * RETURN: 0 if success
3683 * error code if failure
3686 * tdip - ctime|mtime updated
3687 * sip - ctime updated
3691 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
)
3693 znode_t
*dzp
= ITOZ(tdip
);
3695 zfs_sb_t
*zsb
= ITOZSB(tdip
);
3704 ASSERT(S_ISDIR(tdip
->i_mode
));
3711 * POSIX dictates that we return EPERM here.
3712 * Better choices include ENOTSUP or EISDIR.
3714 if (S_ISDIR(sip
->i_mode
)) {
3716 return (SET_ERROR(EPERM
));
3719 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
3721 return (SET_ERROR(EXDEV
));
3727 /* Prevent links to .zfs/shares files */
3729 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
3730 &parent
, sizeof (uint64_t))) != 0) {
3734 if (parent
== zsb
->z_shares_dir
) {
3736 return (SET_ERROR(EPERM
));
3739 if (zsb
->z_utf8
&& u8_validate(name
,
3740 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3742 return (SET_ERROR(EILSEQ
));
3744 #ifdef HAVE_PN_UTILS
3745 if (flags
& FIGNORECASE
)
3747 #endif /* HAVE_PN_UTILS */
3750 * We do not support links between attributes and non-attributes
3751 * because of the potential security risk of creating links
3752 * into "normal" file space in order to circumvent restrictions
3753 * imposed in attribute space.
3755 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3757 return (SET_ERROR(EINVAL
));
3760 owner
= zfs_fuid_map_id(zsb
, szp
->z_uid
, cr
, ZFS_OWNER
);
3761 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3763 return (SET_ERROR(EPERM
));
3766 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3773 * Attempt to lock directory; fail if entry already exists.
3775 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3781 tx
= dmu_tx_create(zsb
->z_os
);
3782 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3783 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3784 zfs_sa_upgrade_txholds(tx
, szp
);
3785 zfs_sa_upgrade_txholds(tx
, dzp
);
3786 error
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3788 zfs_dirent_unlock(dl
);
3789 if (error
== ERESTART
) {
3799 error
= zfs_link_create(dl
, szp
, tx
, 0);
3802 uint64_t txtype
= TX_LINK
;
3803 #ifdef HAVE_PN_UTILS
3804 if (flags
& FIGNORECASE
)
3806 #endif /* HAVE_PN_UTILS */
3807 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3812 zfs_dirent_unlock(dl
);
3814 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3815 zil_commit(zilog
, 0);
3817 zfs_inode_update(dzp
);
3818 zfs_inode_update(szp
);
3822 EXPORT_SYMBOL(zfs_link
);
3825 zfs_putpage_commit_cb(void *arg
, int error
)
3827 struct page
*pp
= arg
;
3830 __set_page_dirty_nobuffers(pp
);
3832 if (error
!= ECANCELED
)
3838 end_page_writeback(pp
);
3842 * Push a page out to disk, once the page is on stable storage the
3843 * registered commit callback will be run as notification of completion.
3845 * IN: ip - page mapped for inode.
3846 * pp - page to push (page is locked)
3847 * wbc - writeback control data
3849 * RETURN: 0 if success
3850 * error code if failure
3853 * ip - ctime|mtime updated
3857 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3859 znode_t
*zp
= ITOZ(ip
);
3860 zfs_sb_t
*zsb
= ITOZSB(ip
);
3868 uint64_t mtime
[2], ctime
[2];
3869 sa_bulk_attr_t bulk
[3];
3876 ASSERT(PageLocked(pp
));
3878 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3879 offset
= i_size_read(ip
); /* File length in bytes */
3880 pglen
= MIN(PAGE_CACHE_SIZE
, /* Page length in bytes */
3881 P2ROUNDUP(offset
, PAGE_CACHE_SIZE
)-pgoff
);
3883 /* Page is beyond end of file */
3884 if (pgoff
>= offset
) {
3890 /* Truncate page length to end of file */
3891 if (pgoff
+ pglen
> offset
)
3892 pglen
= offset
- pgoff
;
3896 * FIXME: Allow mmap writes past its quota. The correct fix
3897 * is to register a page_mkwrite() handler to count the page
3898 * against its quota when it is about to be dirtied.
3900 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
3901 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
3906 set_page_writeback(pp
);
3909 rl
= zfs_range_lock(zp
, pgoff
, pglen
, RL_WRITER
);
3910 tx
= dmu_tx_create(zsb
->z_os
);
3912 sync
= ((zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
) ||
3913 (wbc
->sync_mode
== WB_SYNC_ALL
));
3915 dmu_tx_callback_register(tx
, zfs_putpage_commit_cb
, pp
);
3917 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3919 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3920 zfs_sa_upgrade_txholds(tx
, zp
);
3921 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3923 if (err
== ERESTART
)
3926 /* Will call all registered commit callbacks */
3930 * For the synchronous case the commit callback must be
3931 * explicitly called because there is no registered callback.
3934 zfs_putpage_commit_cb(pp
, ECANCELED
);
3936 zfs_range_unlock(rl
);
3942 ASSERT3U(pglen
, <=, PAGE_CACHE_SIZE
);
3943 dmu_write(zsb
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
3946 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
3947 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
3948 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zsb
), NULL
, &zp
->z_pflags
, 8);
3950 /* Preserve the mtime and ctime provided by the inode */
3951 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3952 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
3953 zp
->z_atime_dirty
= 0;
3956 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3958 zfs_log_write(zsb
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0);
3961 zfs_range_unlock(rl
);
3964 zil_commit(zsb
->z_log
, zp
->z_id
);
3965 zfs_putpage_commit_cb(pp
, err
);
3973 * Update the system attributes when the inode has been dirtied. For the
3974 * moment we only update the mode, atime, mtime, and ctime.
3977 zfs_dirty_inode(struct inode
*ip
, int flags
)
3979 znode_t
*zp
= ITOZ(ip
);
3980 zfs_sb_t
*zsb
= ITOZSB(ip
);
3982 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
3983 sa_bulk_attr_t bulk
[4];
3990 tx
= dmu_tx_create(zsb
->z_os
);
3992 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3993 zfs_sa_upgrade_txholds(tx
, zp
);
3995 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4001 mutex_enter(&zp
->z_lock
);
4002 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
4003 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
4004 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4005 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4007 /* Preserve the mode, mtime and ctime provided by the inode */
4008 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4009 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4010 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4014 zp
->z_atime_dirty
= 0;
4016 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4017 mutex_exit(&zp
->z_lock
);
4024 EXPORT_SYMBOL(zfs_dirty_inode
);
4028 zfs_inactive(struct inode
*ip
)
4030 znode_t
*zp
= ITOZ(ip
);
4031 zfs_sb_t
*zsb
= ITOZSB(ip
);
4034 if (zfsctl_is_node(ip
)) {
4035 zfsctl_inode_inactive(ip
);
4039 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_READER
);
4040 if (zp
->z_sa_hdl
== NULL
) {
4041 rw_exit(&zsb
->z_teardown_inactive_lock
);
4045 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4046 dmu_tx_t
*tx
= dmu_tx_create(zsb
->z_os
);
4048 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4049 zfs_sa_upgrade_txholds(tx
, zp
);
4050 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4054 mutex_enter(&zp
->z_lock
);
4055 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
),
4056 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
4057 zp
->z_atime_dirty
= 0;
4058 mutex_exit(&zp
->z_lock
);
4064 rw_exit(&zsb
->z_teardown_inactive_lock
);
4066 EXPORT_SYMBOL(zfs_inactive
);
4069 * Bounds-check the seek operation.
4071 * IN: ip - inode seeking within
4072 * ooff - old file offset
4073 * noffp - pointer to new file offset
4074 * ct - caller context
4076 * RETURN: 0 if success
4077 * EINVAL if new offset invalid
4081 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4083 if (S_ISDIR(ip
->i_mode
))
4085 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4087 EXPORT_SYMBOL(zfs_seek
);
4090 * Fill pages with data from the disk.
4093 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4095 znode_t
*zp
= ITOZ(ip
);
4096 zfs_sb_t
*zsb
= ITOZSB(ip
);
4098 struct page
*cur_pp
;
4099 u_offset_t io_off
, total
;
4106 io_len
= nr_pages
<< PAGE_CACHE_SHIFT
;
4107 i_size
= i_size_read(ip
);
4108 io_off
= page_offset(pl
[0]);
4110 if (io_off
+ io_len
> i_size
)
4111 io_len
= i_size
- io_off
;
4114 * Iterate over list of pages and read each page individually.
4118 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4122 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4126 /* convert checksum errors into IO errors */
4128 err
= SET_ERROR(EIO
);
4131 cur_pp
= pl
[++page_idx
];
4138 * Uses zfs_fillpage to read data from the file and fill the pages.
4140 * IN: ip - inode of file to get data from.
4141 * pl - list of pages to read
4142 * nr_pages - number of pages to read
4144 * RETURN: 0 on success, error code on failure.
4147 * vp - atime updated
4151 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4153 znode_t
*zp
= ITOZ(ip
);
4154 zfs_sb_t
*zsb
= ITOZSB(ip
);
4163 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4166 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
4171 EXPORT_SYMBOL(zfs_getpage
);
4174 * Check ZFS specific permissions to memory map a section of a file.
4176 * IN: ip - inode of the file to mmap
4178 * addrp - start address in memory region
4179 * len - length of memory region
4180 * vm_flags- address flags
4182 * RETURN: 0 if success
4183 * error code if failure
4187 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4188 unsigned long vm_flags
)
4190 znode_t
*zp
= ITOZ(ip
);
4191 zfs_sb_t
*zsb
= ITOZSB(ip
);
4196 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4197 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4199 return (SET_ERROR(EPERM
));
4202 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4203 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4205 return (SET_ERROR(EACCES
));
4208 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4210 return (SET_ERROR(ENXIO
));
4216 EXPORT_SYMBOL(zfs_map
);
4219 * convoff - converts the given data (start, whence) to the
4223 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4228 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4229 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED()) != 0))
4233 switch (lckdat
->l_whence
) {
4235 lckdat
->l_start
+= offset
;
4238 lckdat
->l_start
+= vap
.va_size
;
4243 return (SET_ERROR(EINVAL
));
4246 if (lckdat
->l_start
< 0)
4247 return (SET_ERROR(EINVAL
));
4251 lckdat
->l_start
-= offset
;
4254 lckdat
->l_start
-= vap
.va_size
;
4259 return (SET_ERROR(EINVAL
));
4262 lckdat
->l_whence
= (short)whence
;
4267 * Free or allocate space in a file. Currently, this function only
4268 * supports the `F_FREESP' command. However, this command is somewhat
4269 * misnamed, as its functionality includes the ability to allocate as
4270 * well as free space.
4272 * IN: ip - inode of file to free data in.
4273 * cmd - action to take (only F_FREESP supported).
4274 * bfp - section of file to free/alloc.
4275 * flag - current file open mode flags.
4276 * offset - current file offset.
4277 * cr - credentials of caller [UNUSED].
4279 * RETURN: 0 on success, error code on failure.
4282 * ip - ctime|mtime updated
4286 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4287 offset_t offset
, cred_t
*cr
)
4289 znode_t
*zp
= ITOZ(ip
);
4290 zfs_sb_t
*zsb
= ITOZSB(ip
);
4297 if (cmd
!= F_FREESP
) {
4299 return (SET_ERROR(EINVAL
));
4302 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4307 if (bfp
->l_len
< 0) {
4309 return (SET_ERROR(EINVAL
));
4313 * Permissions aren't checked on Solaris because on this OS
4314 * zfs_space() can only be called with an opened file handle.
4315 * On Linux we can get here through truncate_range() which
4316 * operates directly on inodes, so we need to check access rights.
4318 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4324 len
= bfp
->l_len
; /* 0 means from off to end of file */
4326 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4331 EXPORT_SYMBOL(zfs_space
);
4335 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4337 znode_t
*zp
= ITOZ(ip
);
4338 zfs_sb_t
*zsb
= ITOZSB(ip
);
4341 uint64_t object
= zp
->z_id
;
4348 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
),
4349 &gen64
, sizeof (uint64_t))) != 0) {
4354 gen
= (uint32_t)gen64
;
4356 size
= (zsb
->z_parent
!= zsb
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4357 if (fidp
->fid_len
< size
) {
4358 fidp
->fid_len
= size
;
4360 return (SET_ERROR(ENOSPC
));
4363 zfid
= (zfid_short_t
*)fidp
;
4365 zfid
->zf_len
= size
;
4367 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4368 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4370 /* Must have a non-zero generation number to distinguish from .zfs */
4373 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4374 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4376 if (size
== LONG_FID_LEN
) {
4377 uint64_t objsetid
= dmu_objset_id(zsb
->z_os
);
4380 zlfid
= (zfid_long_t
*)fidp
;
4382 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4383 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4385 /* XXX - this should be the generation number for the objset */
4386 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4387 zlfid
->zf_setgen
[i
] = 0;
4393 EXPORT_SYMBOL(zfs_fid
);
4397 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4399 znode_t
*zp
= ITOZ(ip
);
4400 zfs_sb_t
*zsb
= ITOZSB(ip
);
4402 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4406 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4411 EXPORT_SYMBOL(zfs_getsecattr
);
4415 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4417 znode_t
*zp
= ITOZ(ip
);
4418 zfs_sb_t
*zsb
= ITOZSB(ip
);
4420 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4421 zilog_t
*zilog
= zsb
->z_log
;
4426 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4428 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4429 zil_commit(zilog
, 0);
4434 EXPORT_SYMBOL(zfs_setsecattr
);
4436 #ifdef HAVE_UIO_ZEROCOPY
4438 * Tunable, both must be a power of 2.
4440 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4441 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4442 * an arcbuf for a partial block read
4444 int zcr_blksz_min
= (1 << 10); /* 1K */
4445 int zcr_blksz_max
= (1 << 17); /* 128K */
4449 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
4451 znode_t
*zp
= ITOZ(ip
);
4452 zfs_sb_t
*zsb
= ITOZSB(ip
);
4453 int max_blksz
= zsb
->z_max_blksz
;
4454 uio_t
*uio
= &xuio
->xu_uio
;
4455 ssize_t size
= uio
->uio_resid
;
4456 offset_t offset
= uio
->uio_loffset
;
4461 int preamble
, postamble
;
4463 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4464 return (SET_ERROR(EINVAL
));
4471 * Loan out an arc_buf for write if write size is bigger than
4472 * max_blksz, and the file's block size is also max_blksz.
4475 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4477 return (SET_ERROR(EINVAL
));
4480 * Caller requests buffers for write before knowing where the
4481 * write offset might be (e.g. NFS TCP write).
4486 preamble
= P2PHASE(offset
, blksz
);
4488 preamble
= blksz
- preamble
;
4493 postamble
= P2PHASE(size
, blksz
);
4496 fullblk
= size
/ blksz
;
4497 (void) dmu_xuio_init(xuio
,
4498 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4501 * Have to fix iov base/len for partial buffers. They
4502 * currently represent full arc_buf's.
4505 /* data begins in the middle of the arc_buf */
4506 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4509 (void) dmu_xuio_add(xuio
, abuf
,
4510 blksz
- preamble
, preamble
);
4513 for (i
= 0; i
< fullblk
; i
++) {
4514 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4517 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4521 /* data ends in the middle of the arc_buf */
4522 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4525 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4530 * Loan out an arc_buf for read if the read size is larger than
4531 * the current file block size. Block alignment is not
4532 * considered. Partial arc_buf will be loaned out for read.
4534 blksz
= zp
->z_blksz
;
4535 if (blksz
< zcr_blksz_min
)
4536 blksz
= zcr_blksz_min
;
4537 if (blksz
> zcr_blksz_max
)
4538 blksz
= zcr_blksz_max
;
4539 /* avoid potential complexity of dealing with it */
4540 if (blksz
> max_blksz
) {
4542 return (SET_ERROR(EINVAL
));
4545 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4551 return (SET_ERROR(EINVAL
));
4556 return (SET_ERROR(EINVAL
));
4559 uio
->uio_extflg
= UIO_XUIO
;
4560 XUIO_XUZC_RW(xuio
) = ioflag
;
4567 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
4571 int ioflag
= XUIO_XUZC_RW(xuio
);
4573 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4575 i
= dmu_xuio_cnt(xuio
);
4577 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4579 * if abuf == NULL, it must be a write buffer
4580 * that has been returned in zfs_write().
4583 dmu_return_arcbuf(abuf
);
4584 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4587 dmu_xuio_fini(xuio
);
4590 #endif /* HAVE_UIO_ZEROCOPY */
4592 #if defined(_KERNEL) && defined(HAVE_SPL)
4593 module_param(zfs_read_chunk_size
, long, 0644);
4594 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");