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 zfs_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) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
110 * dmu_tx_assign(). This is critical because we don't want to block
111 * while holding locks.
113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
114 * reduces lock contention and CPU usage when we must wait (note that if
115 * throughput is constrained by the storage, nearly every transaction
118 * Note, in particular, that if a lock is sometimes acquired before
119 * the tx assigns, and sometimes after (e.g. z_lock), then failing
120 * to use a non-blocking assign can deadlock the system. The scenario:
122 * Thread A has grabbed a lock before calling dmu_tx_assign().
123 * Thread B is in an already-assigned tx, and blocks for this lock.
124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
125 * forever, because the previous txg can't quiesce until B's tx commits.
127 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
130 * to indicate that this operation has already called dmu_tx_wait().
131 * This will ensure that we don't retry forever, waiting a short bit
134 * (5) If the operation succeeded, generate the intent log entry for it
135 * before dropping locks. This ensures that the ordering of events
136 * in the intent log matches the order in which they actually occurred.
137 * During ZIL replay the zfs_log_* functions will update the sequence
138 * number to indicate the zil transaction has replayed.
140 * (6) At the end of each vnode op, the DMU tx must always commit,
141 * regardless of whether there were any errors.
143 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
144 * to ensure that synchronous semantics are provided when necessary.
146 * In general, this is how things should be ordered in each vnode op:
148 * ZFS_ENTER(zsb); // exit if unmounted
150 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
151 * rw_enter(...); // grab any other locks you need
152 * tx = dmu_tx_create(...); // get DMU tx
153 * dmu_tx_hold_*(); // hold each object you might modify
154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
156 * rw_exit(...); // drop locks
157 * zfs_dirent_unlock(dl); // unlock directory entry
158 * iput(...); // release held vnodes
159 * if (error == ERESTART) {
165 * dmu_tx_abort(tx); // abort DMU tx
166 * ZFS_EXIT(zsb); // finished in zfs
167 * return (error); // really out of space
169 * error = do_real_work(); // do whatever this VOP does
171 * zfs_log_*(...); // on success, make ZIL entry
172 * dmu_tx_commit(tx); // commit DMU tx -- error or not
173 * rw_exit(...); // drop locks
174 * zfs_dirent_unlock(dl); // unlock directory entry
175 * iput(...); // release held vnodes
176 * zil_commit(zilog, foid); // synchronous when necessary
177 * ZFS_EXIT(zsb); // finished in zfs
178 * return (error); // done, report error
182 * Virus scanning is unsupported. It would be possible to add a hook
183 * here to performance the required virus scan. This could be done
184 * entirely in the kernel or potentially as an update to invoke a
188 zfs_vscan(struct inode
*ip
, cred_t
*cr
, int async
)
195 zfs_open(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
197 znode_t
*zp
= ITOZ(ip
);
198 zfs_sb_t
*zsb
= ITOZSB(ip
);
203 /* Honor ZFS_APPENDONLY file attribute */
204 if ((mode
& FMODE_WRITE
) && (zp
->z_pflags
& ZFS_APPENDONLY
) &&
205 ((flag
& O_APPEND
) == 0)) {
207 return (SET_ERROR(EPERM
));
210 /* Virus scan eligible files on open */
211 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
212 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0) {
213 if (zfs_vscan(ip
, cr
, 0) != 0) {
215 return (SET_ERROR(EACCES
));
219 /* Keep a count of the synchronous opens in the znode */
221 atomic_inc_32(&zp
->z_sync_cnt
);
226 EXPORT_SYMBOL(zfs_open
);
230 zfs_close(struct inode
*ip
, int flag
, cred_t
*cr
)
232 znode_t
*zp
= ITOZ(ip
);
233 zfs_sb_t
*zsb
= ITOZSB(ip
);
238 /* Decrement the synchronous opens in the znode */
240 atomic_dec_32(&zp
->z_sync_cnt
);
242 if (!zfs_has_ctldir(zp
) && zsb
->z_vscan
&& S_ISREG(ip
->i_mode
) &&
243 !(zp
->z_pflags
& ZFS_AV_QUARANTINED
) && zp
->z_size
> 0)
244 VERIFY(zfs_vscan(ip
, cr
, 1) == 0);
249 EXPORT_SYMBOL(zfs_close
);
251 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
253 * Lseek support for finding holes (cmd == SEEK_HOLE) and
254 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
257 zfs_holey_common(struct inode
*ip
, int cmd
, loff_t
*off
)
259 znode_t
*zp
= ITOZ(ip
);
260 uint64_t noff
= (uint64_t)*off
; /* new offset */
265 file_sz
= zp
->z_size
;
266 if (noff
>= file_sz
) {
267 return (SET_ERROR(ENXIO
));
270 if (cmd
== SEEK_HOLE
)
275 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
278 return (SET_ERROR(ENXIO
));
281 * We could find a hole that begins after the logical end-of-file,
282 * because dmu_offset_next() only works on whole blocks. If the
283 * EOF falls mid-block, then indicate that the "virtual hole"
284 * at the end of the file begins at the logical EOF, rather than
285 * at the end of the last block.
287 if (noff
> file_sz
) {
299 zfs_holey(struct inode
*ip
, int cmd
, loff_t
*off
)
301 znode_t
*zp
= ITOZ(ip
);
302 zfs_sb_t
*zsb
= ITOZSB(ip
);
308 error
= zfs_holey_common(ip
, cmd
, off
);
313 EXPORT_SYMBOL(zfs_holey
);
314 #endif /* SEEK_HOLE && SEEK_DATA */
318 * When a file is memory mapped, we must keep the IO data synchronized
319 * between the DMU cache and the memory mapped pages. What this means:
321 * On Write: If we find a memory mapped page, we write to *both*
322 * the page and the dmu buffer.
325 update_pages(struct inode
*ip
, int64_t start
, int len
,
326 objset_t
*os
, uint64_t oid
)
328 struct address_space
*mp
= ip
->i_mapping
;
334 off
= start
& (PAGE_CACHE_SIZE
-1);
335 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
336 nbytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
338 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
340 if (mapping_writably_mapped(mp
))
341 flush_dcache_page(pp
);
344 (void) dmu_read(os
, oid
, start
+off
, nbytes
, pb
+off
,
348 if (mapping_writably_mapped(mp
))
349 flush_dcache_page(pp
);
351 mark_page_accessed(pp
);
355 page_cache_release(pp
);
364 * When a file is memory mapped, we must keep the IO data synchronized
365 * between the DMU cache and the memory mapped pages. What this means:
367 * On Read: We "read" preferentially from memory mapped pages,
368 * else we default from the dmu buffer.
370 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
371 * the file is memory mapped.
374 mappedread(struct inode
*ip
, int nbytes
, uio_t
*uio
)
376 struct address_space
*mp
= ip
->i_mapping
;
378 znode_t
*zp
= ITOZ(ip
);
379 objset_t
*os
= ITOZSB(ip
)->z_os
;
386 start
= uio
->uio_loffset
;
387 off
= start
& (PAGE_CACHE_SIZE
-1);
388 for (start
&= PAGE_CACHE_MASK
; len
> 0; start
+= PAGE_CACHE_SIZE
) {
389 bytes
= MIN(PAGE_CACHE_SIZE
- off
, len
);
391 pp
= find_lock_page(mp
, start
>> PAGE_CACHE_SHIFT
);
393 ASSERT(PageUptodate(pp
));
396 error
= uiomove(pb
+ off
, bytes
, UIO_READ
, uio
);
399 if (mapping_writably_mapped(mp
))
400 flush_dcache_page(pp
);
402 mark_page_accessed(pp
);
404 page_cache_release(pp
);
406 error
= dmu_read_uio(os
, zp
->z_id
, uio
, bytes
);
418 unsigned long zfs_read_chunk_size
= 1024 * 1024; /* Tunable */
421 * Read bytes from specified file into supplied buffer.
423 * IN: ip - inode of file to be read from.
424 * uio - structure supplying read location, range info,
426 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
427 * O_DIRECT flag; used to bypass page cache.
428 * cr - credentials of caller.
430 * OUT: uio - updated offset and range, buffer filled.
432 * RETURN: 0 on success, error code on failure.
435 * inode - atime updated if byte count > 0
439 zfs_read(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
441 znode_t
*zp
= ITOZ(ip
);
442 zfs_sb_t
*zsb
= ITOZSB(ip
);
447 #ifdef HAVE_UIO_ZEROCOPY
449 #endif /* HAVE_UIO_ZEROCOPY */
455 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
457 return (SET_ERROR(EACCES
));
461 * Validate file offset
463 if (uio
->uio_loffset
< (offset_t
)0) {
465 return (SET_ERROR(EINVAL
));
469 * Fasttrack empty reads
471 if (uio
->uio_resid
== 0) {
477 * If we're in FRSYNC mode, sync out this znode before reading it.
479 if (ioflag
& FRSYNC
|| zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
480 zil_commit(zsb
->z_log
, zp
->z_id
);
483 * Lock the range against changes.
485 rl
= zfs_range_lock(zp
, uio
->uio_loffset
, uio
->uio_resid
, RL_READER
);
488 * If we are reading past end-of-file we can skip
489 * to the end; but we might still need to set atime.
491 if (uio
->uio_loffset
>= zp
->z_size
) {
496 ASSERT(uio
->uio_loffset
< zp
->z_size
);
497 n
= MIN(uio
->uio_resid
, zp
->z_size
- uio
->uio_loffset
);
499 #ifdef HAVE_UIO_ZEROCOPY
500 if ((uio
->uio_extflg
== UIO_XUIO
) &&
501 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
)) {
503 int blksz
= zp
->z_blksz
;
504 uint64_t offset
= uio
->uio_loffset
;
506 xuio
= (xuio_t
*)uio
;
508 nblk
= (P2ROUNDUP(offset
+ n
, blksz
) - P2ALIGN(offset
,
511 ASSERT(offset
+ n
<= blksz
);
514 (void) dmu_xuio_init(xuio
, nblk
);
516 if (vn_has_cached_data(ip
)) {
518 * For simplicity, we always allocate a full buffer
519 * even if we only expect to read a portion of a block.
521 while (--nblk
>= 0) {
522 (void) dmu_xuio_add(xuio
,
523 dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
528 #endif /* HAVE_UIO_ZEROCOPY */
531 nbytes
= MIN(n
, zfs_read_chunk_size
-
532 P2PHASE(uio
->uio_loffset
, zfs_read_chunk_size
));
534 if (zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
535 error
= mappedread(ip
, nbytes
, uio
);
537 error
= dmu_read_uio(os
, zp
->z_id
, uio
, nbytes
);
540 /* convert checksum errors into IO errors */
542 error
= SET_ERROR(EIO
);
549 zfs_range_unlock(rl
);
551 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
555 EXPORT_SYMBOL(zfs_read
);
558 * Write the bytes to a file.
560 * IN: ip - inode of file to be written to.
561 * uio - structure supplying write location, range info,
563 * ioflag - FAPPEND flag set if in append mode.
564 * O_DIRECT flag; used to bypass page cache.
565 * cr - credentials of caller.
567 * OUT: uio - updated offset and range.
569 * RETURN: 0 if success
570 * error code if failure
573 * ip - ctime|mtime updated if byte count > 0
578 zfs_write(struct inode
*ip
, uio_t
*uio
, int ioflag
, cred_t
*cr
)
580 znode_t
*zp
= ITOZ(ip
);
581 rlim64_t limit
= uio
->uio_limit
;
582 ssize_t start_resid
= uio
->uio_resid
;
586 zfs_sb_t
*zsb
= ZTOZSB(zp
);
591 int max_blksz
= zsb
->z_max_blksz
;
594 iovec_t
*aiov
= NULL
;
597 iovec_t
*iovp
= uio
->uio_iov
;
600 sa_bulk_attr_t bulk
[4];
601 uint64_t mtime
[2], ctime
[2];
602 ASSERTV(int iovcnt
= uio
->uio_iovcnt
);
605 * Fasttrack empty write
611 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
617 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
618 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
619 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zsb
), NULL
, &zp
->z_size
, 8);
620 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
624 * If immutable or not appending then return EPERM
626 if ((zp
->z_pflags
& (ZFS_IMMUTABLE
| ZFS_READONLY
)) ||
627 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& FAPPEND
) &&
628 (uio
->uio_loffset
< zp
->z_size
))) {
630 return (SET_ERROR(EPERM
));
636 * Validate file offset
638 woff
= ioflag
& FAPPEND
? zp
->z_size
: uio
->uio_loffset
;
641 return (SET_ERROR(EINVAL
));
645 * Pre-fault the pages to ensure slow (eg NFS) pages
647 * Skip this if uio contains loaned arc_buf.
649 #ifdef HAVE_UIO_ZEROCOPY
650 if ((uio
->uio_extflg
== UIO_XUIO
) &&
651 (((xuio_t
*)uio
)->xu_type
== UIOTYPE_ZEROCOPY
))
652 xuio
= (xuio_t
*)uio
;
655 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
658 * If in append mode, set the io offset pointer to eof.
660 if (ioflag
& FAPPEND
) {
662 * Obtain an appending range lock to guarantee file append
663 * semantics. We reset the write offset once we have the lock.
665 rl
= zfs_range_lock(zp
, 0, n
, RL_APPEND
);
667 if (rl
->r_len
== UINT64_MAX
) {
669 * We overlocked the file because this write will cause
670 * the file block size to increase.
671 * Note that zp_size cannot change with this lock held.
675 uio
->uio_loffset
= woff
;
678 * Note that if the file block size will change as a result of
679 * this write, then this range lock will lock the entire file
680 * so that we can re-write the block safely.
682 rl
= zfs_range_lock(zp
, woff
, n
, RL_WRITER
);
686 zfs_range_unlock(rl
);
688 return (SET_ERROR(EFBIG
));
691 if ((woff
+ n
) > limit
|| woff
> (limit
- n
))
694 /* Will this write extend the file length? */
695 write_eof
= (woff
+ n
> zp
->z_size
);
697 end_size
= MAX(zp
->z_size
, woff
+ n
);
700 * Write the file in reasonable size chunks. Each chunk is written
701 * in a separate transaction; this keeps the intent log records small
702 * and allows us to do more fine-grained space accounting.
706 woff
= uio
->uio_loffset
;
707 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
708 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
710 dmu_return_arcbuf(abuf
);
711 error
= SET_ERROR(EDQUOT
);
715 if (xuio
&& abuf
== NULL
) {
716 ASSERT(i_iov
< iovcnt
);
718 abuf
= dmu_xuio_arcbuf(xuio
, i_iov
);
719 dmu_xuio_clear(xuio
, i_iov
);
720 ASSERT((aiov
->iov_base
== abuf
->b_data
) ||
721 ((char *)aiov
->iov_base
- (char *)abuf
->b_data
+
722 aiov
->iov_len
== arc_buf_size(abuf
)));
724 } else if (abuf
== NULL
&& n
>= max_blksz
&&
725 woff
>= zp
->z_size
&&
726 P2PHASE(woff
, max_blksz
) == 0 &&
727 zp
->z_blksz
== max_blksz
) {
729 * This write covers a full block. "Borrow" a buffer
730 * from the dmu so that we can fill it before we enter
731 * a transaction. This avoids the possibility of
732 * holding up the transaction if the data copy hangs
733 * up on a pagefault (e.g., from an NFS server mapping).
737 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
739 ASSERT(abuf
!= NULL
);
740 ASSERT(arc_buf_size(abuf
) == max_blksz
);
741 if ((error
= uiocopy(abuf
->b_data
, max_blksz
,
742 UIO_WRITE
, uio
, &cbytes
))) {
743 dmu_return_arcbuf(abuf
);
746 ASSERT(cbytes
== max_blksz
);
750 * Start a transaction.
752 tx
= dmu_tx_create(zsb
->z_os
);
753 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
754 dmu_tx_hold_write(tx
, zp
->z_id
, woff
, MIN(n
, max_blksz
));
755 zfs_sa_upgrade_txholds(tx
, zp
);
756 error
= dmu_tx_assign(tx
, TXG_WAIT
);
760 dmu_return_arcbuf(abuf
);
765 * If zfs_range_lock() over-locked we grow the blocksize
766 * and then reduce the lock range. This will only happen
767 * on the first iteration since zfs_range_reduce() will
768 * shrink down r_len to the appropriate size.
770 if (rl
->r_len
== UINT64_MAX
) {
773 if (zp
->z_blksz
> max_blksz
) {
775 * File's blocksize is already larger than the
776 * "recordsize" property. Only let it grow to
777 * the next power of 2.
779 ASSERT(!ISP2(zp
->z_blksz
));
780 new_blksz
= MIN(end_size
,
781 1 << highbit64(zp
->z_blksz
));
783 new_blksz
= MIN(end_size
, max_blksz
);
785 zfs_grow_blocksize(zp
, new_blksz
, tx
);
786 zfs_range_reduce(rl
, woff
, n
);
790 * XXX - should we really limit each write to z_max_blksz?
791 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
793 nbytes
= MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
796 tx_bytes
= uio
->uio_resid
;
797 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
799 tx_bytes
-= uio
->uio_resid
;
802 ASSERT(xuio
== NULL
|| tx_bytes
== aiov
->iov_len
);
804 * If this is not a full block write, but we are
805 * extending the file past EOF and this data starts
806 * block-aligned, use assign_arcbuf(). Otherwise,
807 * write via dmu_write().
809 if (tx_bytes
< max_blksz
&& (!write_eof
||
810 aiov
->iov_base
!= abuf
->b_data
)) {
812 dmu_write(zsb
->z_os
, zp
->z_id
, woff
,
813 aiov
->iov_len
, aiov
->iov_base
, tx
);
814 dmu_return_arcbuf(abuf
);
815 xuio_stat_wbuf_copied();
817 ASSERT(xuio
|| tx_bytes
== max_blksz
);
818 dmu_assign_arcbuf(sa_get_db(zp
->z_sa_hdl
),
821 ASSERT(tx_bytes
<= uio
->uio_resid
);
822 uioskip(uio
, tx_bytes
);
825 if (tx_bytes
&& zp
->z_is_mapped
&& !(ioflag
& O_DIRECT
))
826 update_pages(ip
, woff
, tx_bytes
, zsb
->z_os
, zp
->z_id
);
829 * If we made no progress, we're done. If we made even
830 * partial progress, update the znode and ZIL accordingly.
833 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
834 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
841 * Clear Set-UID/Set-GID bits on successful write if not
842 * privileged and at least one of the excute bits is set.
844 * It would be nice to to this after all writes have
845 * been done, but that would still expose the ISUID/ISGID
846 * to another app after the partial write is committed.
848 * Note: we don't call zfs_fuid_map_id() here because
849 * user 0 is not an ephemeral uid.
851 mutex_enter(&zp
->z_acl_lock
);
852 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) |
853 (S_IXUSR
>> 6))) != 0 &&
854 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
855 secpolicy_vnode_setid_retain(cr
,
856 (zp
->z_mode
& S_ISUID
) != 0 && zp
->z_uid
== 0) != 0) {
858 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
859 newmode
= zp
->z_mode
;
860 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zsb
),
861 (void *)&newmode
, sizeof (uint64_t), tx
);
863 mutex_exit(&zp
->z_acl_lock
);
865 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
869 * Update the file size (zp_size) if it has changed;
870 * account for possible concurrent updates.
872 while ((end_size
= zp
->z_size
) < uio
->uio_loffset
) {
873 (void) atomic_cas_64(&zp
->z_size
, end_size
,
878 * If we are replaying and eof is non zero then force
879 * the file size to the specified eof. Note, there's no
880 * concurrency during replay.
882 if (zsb
->z_replay
&& zsb
->z_replay_eof
!= 0)
883 zp
->z_size
= zsb
->z_replay_eof
;
885 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
887 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
893 ASSERT(tx_bytes
== nbytes
);
897 uio_prefaultpages(MIN(n
, max_blksz
), uio
);
900 zfs_range_unlock(rl
);
903 * If we're in replay mode, or we made no progress, return error.
904 * Otherwise, it's at least a partial write, so it's successful.
906 if (zsb
->z_replay
|| uio
->uio_resid
== start_resid
) {
911 if (ioflag
& (FSYNC
| FDSYNC
) ||
912 zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
913 zil_commit(zilog
, zp
->z_id
);
915 zfs_inode_update(zp
);
919 EXPORT_SYMBOL(zfs_write
);
922 zfs_iput_async(struct inode
*ip
)
924 objset_t
*os
= ITOZSB(ip
)->z_os
;
926 ASSERT(atomic_read(&ip
->i_count
) > 0);
929 if (atomic_read(&ip
->i_count
) == 1)
930 taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os
)),
931 (task_func_t
*)iput
, ip
, TQ_SLEEP
);
937 zfs_get_done(zgd_t
*zgd
, int error
)
939 znode_t
*zp
= zgd
->zgd_private
;
942 dmu_buf_rele(zgd
->zgd_db
, zgd
);
944 zfs_range_unlock(zgd
->zgd_rl
);
947 * Release the vnode asynchronously as we currently have the
948 * txg stopped from syncing.
950 zfs_iput_async(ZTOI(zp
));
952 if (error
== 0 && zgd
->zgd_bp
)
953 zil_add_block(zgd
->zgd_zilog
, zgd
->zgd_bp
);
955 kmem_free(zgd
, sizeof (zgd_t
));
959 static int zil_fault_io
= 0;
963 * Get data to generate a TX_WRITE intent log record.
966 zfs_get_data(void *arg
, lr_write_t
*lr
, char *buf
, zio_t
*zio
)
969 objset_t
*os
= zsb
->z_os
;
971 uint64_t object
= lr
->lr_foid
;
972 uint64_t offset
= lr
->lr_offset
;
973 uint64_t size
= lr
->lr_length
;
974 blkptr_t
*bp
= &lr
->lr_blkptr
;
983 * Nothing to do if the file has been removed
985 if (zfs_zget(zsb
, object
, &zp
) != 0)
986 return (SET_ERROR(ENOENT
));
987 if (zp
->z_unlinked
) {
989 * Release the vnode asynchronously as we currently have the
990 * txg stopped from syncing.
992 zfs_iput_async(ZTOI(zp
));
993 return (SET_ERROR(ENOENT
));
996 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
997 zgd
->zgd_zilog
= zsb
->z_log
;
998 zgd
->zgd_private
= zp
;
1001 * Write records come in two flavors: immediate and indirect.
1002 * For small writes it's cheaper to store the data with the
1003 * log record (immediate); for large writes it's cheaper to
1004 * sync the data and get a pointer to it (indirect) so that
1005 * we don't have to write the data twice.
1007 if (buf
!= NULL
) { /* immediate write */
1008 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
, RL_READER
);
1009 /* test for truncation needs to be done while range locked */
1010 if (offset
>= zp
->z_size
) {
1011 error
= SET_ERROR(ENOENT
);
1013 error
= dmu_read(os
, object
, offset
, size
, buf
,
1014 DMU_READ_NO_PREFETCH
);
1016 ASSERT(error
== 0 || error
== ENOENT
);
1017 } else { /* indirect write */
1019 * Have to lock the whole block to ensure when it's
1020 * written out and it's checksum is being calculated
1021 * that no one can change the data. We need to re-check
1022 * blocksize after we get the lock in case it's changed!
1027 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
1029 zgd
->zgd_rl
= zfs_range_lock(zp
, offset
, size
,
1031 if (zp
->z_blksz
== size
)
1034 zfs_range_unlock(zgd
->zgd_rl
);
1036 /* test for truncation needs to be done while range locked */
1037 if (lr
->lr_offset
>= zp
->z_size
)
1038 error
= SET_ERROR(ENOENT
);
1041 error
= SET_ERROR(EIO
);
1046 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
1047 DMU_READ_NO_PREFETCH
);
1050 blkptr_t
*obp
= dmu_buf_get_blkptr(db
);
1052 ASSERT(BP_IS_HOLE(bp
));
1059 ASSERT(db
->db_offset
== offset
);
1060 ASSERT(db
->db_size
== size
);
1062 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
1064 ASSERT(error
|| lr
->lr_length
<= zp
->z_blksz
);
1067 * On success, we need to wait for the write I/O
1068 * initiated by dmu_sync() to complete before we can
1069 * release this dbuf. We will finish everything up
1070 * in the zfs_get_done() callback.
1075 if (error
== EALREADY
) {
1076 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
1082 zfs_get_done(zgd
, error
);
1089 zfs_access(struct inode
*ip
, int mode
, int flag
, cred_t
*cr
)
1091 znode_t
*zp
= ITOZ(ip
);
1092 zfs_sb_t
*zsb
= ITOZSB(ip
);
1098 if (flag
& V_ACE_MASK
)
1099 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
);
1101 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
);
1106 EXPORT_SYMBOL(zfs_access
);
1109 * Lookup an entry in a directory, or an extended attribute directory.
1110 * If it exists, return a held inode reference for it.
1112 * IN: dip - inode of directory to search.
1113 * nm - name of entry to lookup.
1114 * flags - LOOKUP_XATTR set if looking for an attribute.
1115 * cr - credentials of caller.
1116 * direntflags - directory lookup flags
1117 * realpnp - returned pathname.
1119 * OUT: ipp - inode of located entry, NULL if not found.
1121 * RETURN: 0 on success, error code on failure.
1128 zfs_lookup(struct inode
*dip
, char *nm
, struct inode
**ipp
, int flags
,
1129 cred_t
*cr
, int *direntflags
, pathname_t
*realpnp
)
1131 znode_t
*zdp
= ITOZ(dip
);
1132 zfs_sb_t
*zsb
= ITOZSB(dip
);
1136 if (!(flags
& (LOOKUP_XATTR
| FIGNORECASE
))) {
1138 if (!S_ISDIR(dip
->i_mode
)) {
1139 return (SET_ERROR(ENOTDIR
));
1140 } else if (zdp
->z_sa_hdl
== NULL
) {
1141 return (SET_ERROR(EIO
));
1144 if (nm
[0] == 0 || (nm
[0] == '.' && nm
[1] == '\0')) {
1145 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1154 vnode_t
*tvp
= dnlc_lookup(dvp
, nm
);
1157 error
= zfs_fastaccesschk_execute(zdp
, cr
);
1162 if (tvp
== DNLC_NO_VNODE
) {
1164 return (SET_ERROR(ENOENT
));
1167 return (specvp_check(vpp
, cr
));
1170 #endif /* HAVE_DNLC */
1179 if (flags
& LOOKUP_XATTR
) {
1181 * We don't allow recursive attributes..
1182 * Maybe someday we will.
1184 if (zdp
->z_pflags
& ZFS_XATTR
) {
1186 return (SET_ERROR(EINVAL
));
1189 if ((error
= zfs_get_xattrdir(zdp
, ipp
, cr
, flags
))) {
1195 * Do we have permission to get into attribute directory?
1198 if ((error
= zfs_zaccess(ITOZ(*ipp
), ACE_EXECUTE
, 0,
1208 if (!S_ISDIR(dip
->i_mode
)) {
1210 return (SET_ERROR(ENOTDIR
));
1214 * Check accessibility of directory.
1217 if ((error
= zfs_zaccess(zdp
, ACE_EXECUTE
, 0, B_FALSE
, cr
))) {
1222 if (zsb
->z_utf8
&& u8_validate(nm
, strlen(nm
),
1223 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1225 return (SET_ERROR(EILSEQ
));
1228 error
= zfs_dirlook(zdp
, nm
, ipp
, flags
, direntflags
, realpnp
);
1229 if ((error
== 0) && (*ipp
))
1230 zfs_inode_update(ITOZ(*ipp
));
1235 EXPORT_SYMBOL(zfs_lookup
);
1238 * Attempt to create a new entry in a directory. If the entry
1239 * already exists, truncate the file if permissible, else return
1240 * an error. Return the ip of the created or trunc'd file.
1242 * IN: dip - inode of directory to put new file entry in.
1243 * name - name of new file entry.
1244 * vap - attributes of new file.
1245 * excl - flag indicating exclusive or non-exclusive mode.
1246 * mode - mode to open file with.
1247 * cr - credentials of caller.
1248 * flag - large file flag [UNUSED].
1249 * vsecp - ACL to be set
1251 * OUT: ipp - inode of created or trunc'd entry.
1253 * RETURN: 0 on success, error code on failure.
1256 * dip - ctime|mtime updated if new entry created
1257 * ip - ctime|mtime always, atime if new
1262 zfs_create(struct inode
*dip
, char *name
, vattr_t
*vap
, int excl
,
1263 int mode
, struct inode
**ipp
, cred_t
*cr
, int flag
, vsecattr_t
*vsecp
)
1265 znode_t
*zp
, *dzp
= ITOZ(dip
);
1266 zfs_sb_t
*zsb
= ITOZSB(dip
);
1274 zfs_acl_ids_t acl_ids
;
1275 boolean_t fuid_dirtied
;
1276 boolean_t have_acl
= B_FALSE
;
1277 boolean_t waited
= B_FALSE
;
1280 * If we have an ephemeral id, ACL, or XVATTR then
1281 * make sure file system is at proper version
1287 if (zsb
->z_use_fuids
== B_FALSE
&&
1288 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1289 return (SET_ERROR(EINVAL
));
1296 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
1297 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1299 return (SET_ERROR(EILSEQ
));
1302 if (vap
->va_mask
& ATTR_XVATTR
) {
1303 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1304 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1312 if (*name
== '\0') {
1314 * Null component name refers to the directory itself.
1321 /* possible igrab(zp) */
1324 if (flag
& FIGNORECASE
)
1327 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1331 zfs_acl_ids_free(&acl_ids
);
1332 if (strcmp(name
, "..") == 0)
1333 error
= SET_ERROR(EISDIR
);
1343 * Create a new file object and update the directory
1346 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
1348 zfs_acl_ids_free(&acl_ids
);
1353 * We only support the creation of regular files in
1354 * extended attribute directories.
1357 if ((dzp
->z_pflags
& ZFS_XATTR
) && !S_ISREG(vap
->va_mode
)) {
1359 zfs_acl_ids_free(&acl_ids
);
1360 error
= SET_ERROR(EINVAL
);
1364 if (!have_acl
&& (error
= zfs_acl_ids_create(dzp
, 0, vap
,
1365 cr
, vsecp
, &acl_ids
)) != 0)
1369 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1370 zfs_acl_ids_free(&acl_ids
);
1371 error
= SET_ERROR(EDQUOT
);
1375 tx
= dmu_tx_create(os
);
1377 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1378 ZFS_SA_BASE_ATTR_SIZE
);
1380 fuid_dirtied
= zsb
->z_fuid_dirty
;
1382 zfs_fuid_txhold(zsb
, tx
);
1383 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
1384 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
1385 if (!zsb
->z_use_sa
&&
1386 acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1387 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
1388 0, acl_ids
.z_aclp
->z_acl_bytes
);
1390 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1392 zfs_dirent_unlock(dl
);
1393 if (error
== ERESTART
) {
1399 zfs_acl_ids_free(&acl_ids
);
1404 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1407 zfs_fuid_sync(zsb
, tx
);
1409 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1410 txtype
= zfs_log_create_txtype(Z_FILE
, vsecp
, vap
);
1411 if (flag
& FIGNORECASE
)
1413 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, name
,
1414 vsecp
, acl_ids
.z_fuidp
, vap
);
1415 zfs_acl_ids_free(&acl_ids
);
1418 int aflags
= (flag
& FAPPEND
) ? V_APPEND
: 0;
1421 zfs_acl_ids_free(&acl_ids
);
1425 * A directory entry already exists for this name.
1428 * Can't truncate an existing file if in exclusive mode.
1431 error
= SET_ERROR(EEXIST
);
1435 * Can't open a directory for writing.
1437 if (S_ISDIR(ZTOI(zp
)->i_mode
)) {
1438 error
= SET_ERROR(EISDIR
);
1442 * Verify requested access to file.
1444 if (mode
&& (error
= zfs_zaccess_rwx(zp
, mode
, aflags
, cr
))) {
1448 mutex_enter(&dzp
->z_lock
);
1450 mutex_exit(&dzp
->z_lock
);
1453 * Truncate regular files if requested.
1455 if (S_ISREG(ZTOI(zp
)->i_mode
) &&
1456 (vap
->va_mask
& ATTR_SIZE
) && (vap
->va_size
== 0)) {
1457 /* we can't hold any locks when calling zfs_freesp() */
1458 zfs_dirent_unlock(dl
);
1460 error
= zfs_freesp(zp
, 0, 0, mode
, TRUE
);
1466 zfs_dirent_unlock(dl
);
1472 zfs_inode_update(dzp
);
1473 zfs_inode_update(zp
);
1477 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1478 zil_commit(zilog
, 0);
1483 EXPORT_SYMBOL(zfs_create
);
1486 * Remove an entry from a directory.
1488 * IN: dip - inode of directory to remove entry from.
1489 * name - name of entry to remove.
1490 * cr - credentials of caller.
1492 * RETURN: 0 if success
1493 * error code if failure
1497 * ip - ctime (if nlink > 0)
1500 uint64_t null_xattr
= 0;
1504 zfs_remove(struct inode
*dip
, char *name
, cred_t
*cr
)
1506 znode_t
*zp
, *dzp
= ITOZ(dip
);
1509 zfs_sb_t
*zsb
= ITOZSB(dip
);
1512 uint64_t xattr_obj_unlinked
= 0;
1518 pathname_t
*realnmp
= NULL
;
1519 #ifdef HAVE_PN_UTILS
1521 #endif /* HAVE_PN_UTILS */
1524 boolean_t waited
= B_FALSE
;
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
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1602 zfs_dirent_unlock(dl
);
1606 if (error
== ERESTART
) {
1612 #ifdef HAVE_PN_UTILS
1615 #endif /* HAVE_PN_UTILS */
1622 * Remove the directory entry.
1624 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, &unlinked
);
1633 * Hold z_lock so that we can make sure that the ACL obj
1634 * hasn't changed. Could have been deleted due to
1637 mutex_enter(&zp
->z_lock
);
1638 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
1639 &xattr_obj_unlinked
, sizeof (xattr_obj_unlinked
));
1640 mutex_exit(&zp
->z_lock
);
1641 zfs_unlinked_add(zp
, tx
);
1645 #ifdef HAVE_PN_UTILS
1646 if (flags
& FIGNORECASE
)
1648 #endif /* HAVE_PN_UTILS */
1649 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, obj
);
1653 #ifdef HAVE_PN_UTILS
1656 #endif /* HAVE_PN_UTILS */
1658 zfs_dirent_unlock(dl
);
1659 zfs_inode_update(dzp
);
1660 zfs_inode_update(zp
);
1662 zfs_inode_update(xzp
);
1668 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1669 zil_commit(zilog
, 0);
1674 EXPORT_SYMBOL(zfs_remove
);
1677 * Create a new directory and insert it into dip using the name
1678 * provided. Return a pointer to the inserted directory.
1680 * IN: dip - inode of directory to add subdir to.
1681 * dirname - name of new directory.
1682 * vap - attributes of new directory.
1683 * cr - credentials of caller.
1684 * vsecp - ACL to be set
1686 * OUT: ipp - inode of created directory.
1688 * RETURN: 0 if success
1689 * error code if failure
1692 * dip - ctime|mtime updated
1693 * ipp - ctime|mtime|atime updated
1697 zfs_mkdir(struct inode
*dip
, char *dirname
, vattr_t
*vap
, struct inode
**ipp
,
1698 cred_t
*cr
, int flags
, vsecattr_t
*vsecp
)
1700 znode_t
*zp
, *dzp
= ITOZ(dip
);
1701 zfs_sb_t
*zsb
= ITOZSB(dip
);
1709 gid_t gid
= crgetgid(cr
);
1710 zfs_acl_ids_t acl_ids
;
1711 boolean_t fuid_dirtied
;
1712 boolean_t waited
= B_FALSE
;
1714 ASSERT(S_ISDIR(vap
->va_mode
));
1717 * If we have an ephemeral id, ACL, or XVATTR then
1718 * make sure file system is at proper version
1722 if (zsb
->z_use_fuids
== B_FALSE
&&
1723 (vsecp
|| IS_EPHEMERAL(uid
) || IS_EPHEMERAL(gid
)))
1724 return (SET_ERROR(EINVAL
));
1730 if (dzp
->z_pflags
& ZFS_XATTR
) {
1732 return (SET_ERROR(EINVAL
));
1735 if (zsb
->z_utf8
&& u8_validate(dirname
,
1736 strlen(dirname
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
1738 return (SET_ERROR(EILSEQ
));
1740 if (flags
& FIGNORECASE
)
1743 if (vap
->va_mask
& ATTR_XVATTR
) {
1744 if ((error
= secpolicy_xvattr((xvattr_t
*)vap
,
1745 crgetuid(cr
), cr
, vap
->va_mode
)) != 0) {
1751 if ((error
= zfs_acl_ids_create(dzp
, 0, vap
, cr
,
1752 vsecp
, &acl_ids
)) != 0) {
1757 * First make sure the new directory doesn't exist.
1759 * Existence is checked first to make sure we don't return
1760 * EACCES instead of EEXIST which can cause some applications
1766 if ((error
= zfs_dirent_lock(&dl
, dzp
, dirname
, &zp
, zf
,
1768 zfs_acl_ids_free(&acl_ids
);
1773 if ((error
= zfs_zaccess(dzp
, ACE_ADD_SUBDIRECTORY
, 0, B_FALSE
, cr
))) {
1774 zfs_acl_ids_free(&acl_ids
);
1775 zfs_dirent_unlock(dl
);
1780 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
1781 zfs_acl_ids_free(&acl_ids
);
1782 zfs_dirent_unlock(dl
);
1784 return (SET_ERROR(EDQUOT
));
1788 * Add a new entry to the directory.
1790 tx
= dmu_tx_create(zsb
->z_os
);
1791 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, dirname
);
1792 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
1793 fuid_dirtied
= zsb
->z_fuid_dirty
;
1795 zfs_fuid_txhold(zsb
, tx
);
1796 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
1797 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
1798 acl_ids
.z_aclp
->z_acl_bytes
);
1801 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
1802 ZFS_SA_BASE_ATTR_SIZE
);
1804 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1806 zfs_dirent_unlock(dl
);
1807 if (error
== ERESTART
) {
1813 zfs_acl_ids_free(&acl_ids
);
1822 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
1825 zfs_fuid_sync(zsb
, tx
);
1828 * Now put new name in parent dir.
1830 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
1834 txtype
= zfs_log_create_txtype(Z_DIR
, vsecp
, vap
);
1835 if (flags
& FIGNORECASE
)
1837 zfs_log_create(zilog
, tx
, txtype
, dzp
, zp
, dirname
, vsecp
,
1838 acl_ids
.z_fuidp
, vap
);
1840 zfs_acl_ids_free(&acl_ids
);
1844 zfs_dirent_unlock(dl
);
1846 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1847 zil_commit(zilog
, 0);
1849 zfs_inode_update(dzp
);
1850 zfs_inode_update(zp
);
1854 EXPORT_SYMBOL(zfs_mkdir
);
1857 * Remove a directory subdir entry. If the current working
1858 * directory is the same as the subdir to be removed, the
1861 * IN: dip - inode of directory to remove from.
1862 * name - name of directory to be removed.
1863 * cwd - inode of current working directory.
1864 * cr - credentials of caller.
1865 * flags - case flags
1867 * RETURN: 0 on success, error code on failure.
1870 * dip - ctime|mtime updated
1874 zfs_rmdir(struct inode
*dip
, char *name
, struct inode
*cwd
, cred_t
*cr
,
1877 znode_t
*dzp
= ITOZ(dip
);
1880 zfs_sb_t
*zsb
= ITOZSB(dip
);
1886 boolean_t waited
= B_FALSE
;
1892 if (flags
& FIGNORECASE
)
1898 * Attempt to lock directory; fail if entry doesn't exist.
1900 if ((error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
,
1908 if ((error
= zfs_zaccess_delete(dzp
, zp
, cr
))) {
1912 if (!S_ISDIR(ip
->i_mode
)) {
1913 error
= SET_ERROR(ENOTDIR
);
1918 error
= SET_ERROR(EINVAL
);
1923 * Grab a lock on the directory to make sure that noone is
1924 * trying to add (or lookup) entries while we are removing it.
1926 rw_enter(&zp
->z_name_lock
, RW_WRITER
);
1929 * Grab a lock on the parent pointer to make sure we play well
1930 * with the treewalk and directory rename code.
1932 rw_enter(&zp
->z_parent_lock
, RW_WRITER
);
1934 tx
= dmu_tx_create(zsb
->z_os
);
1935 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, name
);
1936 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
1937 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
1938 zfs_sa_upgrade_txholds(tx
, zp
);
1939 zfs_sa_upgrade_txholds(tx
, dzp
);
1940 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
1942 rw_exit(&zp
->z_parent_lock
);
1943 rw_exit(&zp
->z_name_lock
);
1944 zfs_dirent_unlock(dl
);
1946 if (error
== ERESTART
) {
1957 error
= zfs_link_destroy(dl
, zp
, tx
, zflg
, NULL
);
1960 uint64_t txtype
= TX_RMDIR
;
1961 if (flags
& FIGNORECASE
)
1963 zfs_log_remove(zilog
, tx
, txtype
, dzp
, name
, ZFS_NO_OBJECT
);
1968 rw_exit(&zp
->z_parent_lock
);
1969 rw_exit(&zp
->z_name_lock
);
1971 zfs_dirent_unlock(dl
);
1973 zfs_inode_update(dzp
);
1974 zfs_inode_update(zp
);
1977 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
1978 zil_commit(zilog
, 0);
1983 EXPORT_SYMBOL(zfs_rmdir
);
1986 * Read as many directory entries as will fit into the provided
1987 * dirent buffer from the given directory cursor position.
1989 * IN: ip - inode of directory to read.
1990 * dirent - buffer for directory entries.
1992 * OUT: dirent - filler buffer of directory entries.
1994 * RETURN: 0 if success
1995 * error code if failure
1998 * ip - atime updated
2000 * Note that the low 4 bits of the cookie returned by zap is always zero.
2001 * This allows us to use the low range for "special" directory entries:
2002 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2003 * we use the offset 2 for the '.zfs' directory.
2007 zfs_readdir(struct inode
*ip
, struct dir_context
*ctx
, cred_t
*cr
)
2009 znode_t
*zp
= ITOZ(ip
);
2010 zfs_sb_t
*zsb
= ITOZSB(ip
);
2013 zap_attribute_t zap
;
2019 uint64_t offset
; /* must be unsigned; checks for < 1 */
2024 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
2025 &parent
, sizeof (parent
))) != 0)
2029 * Quit if directory has been removed (posix)
2037 prefetch
= zp
->z_zn_prefetch
;
2040 * Initialize the iterator cursor.
2044 * Start iteration from the beginning of the directory.
2046 zap_cursor_init(&zc
, os
, zp
->z_id
);
2049 * The offset is a serialized cursor.
2051 zap_cursor_init_serialized(&zc
, os
, zp
->z_id
, offset
);
2055 * Transform to file-system independent format
2060 * Special case `.', `..', and `.zfs'.
2063 (void) strcpy(zap
.za_name
, ".");
2064 zap
.za_normalization_conflict
= 0;
2067 } else if (offset
== 1) {
2068 (void) strcpy(zap
.za_name
, "..");
2069 zap
.za_normalization_conflict
= 0;
2072 } else if (offset
== 2 && zfs_show_ctldir(zp
)) {
2073 (void) strcpy(zap
.za_name
, ZFS_CTLDIR_NAME
);
2074 zap
.za_normalization_conflict
= 0;
2075 objnum
= ZFSCTL_INO_ROOT
;
2081 if ((error
= zap_cursor_retrieve(&zc
, &zap
))) {
2082 if (error
== ENOENT
)
2089 * Allow multiple entries provided the first entry is
2090 * the object id. Non-zpl consumers may safely make
2091 * use of the additional space.
2093 * XXX: This should be a feature flag for compatibility
2095 if (zap
.za_integer_length
!= 8 ||
2096 zap
.za_num_integers
== 0) {
2097 cmn_err(CE_WARN
, "zap_readdir: bad directory "
2098 "entry, obj = %lld, offset = %lld, "
2099 "length = %d, num = %lld\n",
2100 (u_longlong_t
)zp
->z_id
,
2101 (u_longlong_t
)offset
,
2102 zap
.za_integer_length
,
2103 (u_longlong_t
)zap
.za_num_integers
);
2104 error
= SET_ERROR(ENXIO
);
2108 objnum
= ZFS_DIRENT_OBJ(zap
.za_first_integer
);
2109 type
= ZFS_DIRENT_TYPE(zap
.za_first_integer
);
2112 done
= !dir_emit(ctx
, zap
.za_name
, strlen(zap
.za_name
),
2117 /* Prefetch znode */
2119 dmu_prefetch(os
, objnum
, 0, 0);
2123 * Move to the next entry, fill in the previous offset.
2125 if (offset
> 2 || (offset
== 2 && !zfs_show_ctldir(zp
))) {
2126 zap_cursor_advance(&zc
);
2127 offset
= zap_cursor_serialize(&zc
);
2133 zp
->z_zn_prefetch
= B_FALSE
; /* a lookup will re-enable pre-fetching */
2136 zap_cursor_fini(&zc
);
2137 if (error
== ENOENT
)
2140 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
2147 EXPORT_SYMBOL(zfs_readdir
);
2149 ulong_t zfs_fsync_sync_cnt
= 4;
2152 zfs_fsync(struct inode
*ip
, int syncflag
, cred_t
*cr
)
2154 znode_t
*zp
= ITOZ(ip
);
2155 zfs_sb_t
*zsb
= ITOZSB(ip
);
2157 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
2159 if (zsb
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
2162 zil_commit(zsb
->z_log
, zp
->z_id
);
2165 tsd_set(zfs_fsyncer_key
, NULL
);
2169 EXPORT_SYMBOL(zfs_fsync
);
2173 * Get the requested file attributes and place them in the provided
2176 * IN: ip - inode of file.
2177 * vap - va_mask identifies requested attributes.
2178 * If ATTR_XVATTR set, then optional attrs are requested
2179 * flags - ATTR_NOACLCHECK (CIFS server context)
2180 * cr - credentials of caller.
2182 * OUT: vap - attribute values.
2184 * RETURN: 0 (always succeeds)
2188 zfs_getattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2190 znode_t
*zp
= ITOZ(ip
);
2191 zfs_sb_t
*zsb
= ITOZSB(ip
);
2194 uint64_t mtime
[2], ctime
[2];
2195 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2196 xoptattr_t
*xoap
= NULL
;
2197 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2198 sa_bulk_attr_t bulk
[2];
2204 zfs_fuid_map_ids(zp
, cr
, &vap
->va_uid
, &vap
->va_gid
);
2206 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
2207 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
2209 if ((error
= sa_bulk_lookup(zp
->z_sa_hdl
, bulk
, count
)) != 0) {
2215 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2216 * Also, if we are the owner don't bother, since owner should
2217 * always be allowed to read basic attributes of file.
2219 if (!(zp
->z_pflags
& ZFS_ACL_TRIVIAL
) &&
2220 (vap
->va_uid
!= crgetuid(cr
))) {
2221 if ((error
= zfs_zaccess(zp
, ACE_READ_ATTRIBUTES
, 0,
2229 * Return all attributes. It's cheaper to provide the answer
2230 * than to determine whether we were asked the question.
2233 mutex_enter(&zp
->z_lock
);
2234 vap
->va_type
= vn_mode_to_vtype(zp
->z_mode
);
2235 vap
->va_mode
= zp
->z_mode
;
2236 vap
->va_fsid
= ZTOI(zp
)->i_sb
->s_dev
;
2237 vap
->va_nodeid
= zp
->z_id
;
2238 if ((zp
->z_id
== zsb
->z_root
) && zfs_show_ctldir(zp
))
2239 links
= zp
->z_links
+ 1;
2241 links
= zp
->z_links
;
2242 vap
->va_nlink
= MIN(links
, ZFS_LINK_MAX
);
2243 vap
->va_size
= i_size_read(ip
);
2244 vap
->va_rdev
= ip
->i_rdev
;
2245 vap
->va_seq
= ip
->i_generation
;
2248 * Add in any requested optional attributes and the create time.
2249 * Also set the corresponding bits in the returned attribute bitmap.
2251 if ((xoap
= xva_getxoptattr(xvap
)) != NULL
&& zsb
->z_use_fuids
) {
2252 if (XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
)) {
2254 ((zp
->z_pflags
& ZFS_ARCHIVE
) != 0);
2255 XVA_SET_RTN(xvap
, XAT_ARCHIVE
);
2258 if (XVA_ISSET_REQ(xvap
, XAT_READONLY
)) {
2259 xoap
->xoa_readonly
=
2260 ((zp
->z_pflags
& ZFS_READONLY
) != 0);
2261 XVA_SET_RTN(xvap
, XAT_READONLY
);
2264 if (XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)) {
2266 ((zp
->z_pflags
& ZFS_SYSTEM
) != 0);
2267 XVA_SET_RTN(xvap
, XAT_SYSTEM
);
2270 if (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
)) {
2272 ((zp
->z_pflags
& ZFS_HIDDEN
) != 0);
2273 XVA_SET_RTN(xvap
, XAT_HIDDEN
);
2276 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2277 xoap
->xoa_nounlink
=
2278 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0);
2279 XVA_SET_RTN(xvap
, XAT_NOUNLINK
);
2282 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2283 xoap
->xoa_immutable
=
2284 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0);
2285 XVA_SET_RTN(xvap
, XAT_IMMUTABLE
);
2288 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2289 xoap
->xoa_appendonly
=
2290 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0);
2291 XVA_SET_RTN(xvap
, XAT_APPENDONLY
);
2294 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2296 ((zp
->z_pflags
& ZFS_NODUMP
) != 0);
2297 XVA_SET_RTN(xvap
, XAT_NODUMP
);
2300 if (XVA_ISSET_REQ(xvap
, XAT_OPAQUE
)) {
2302 ((zp
->z_pflags
& ZFS_OPAQUE
) != 0);
2303 XVA_SET_RTN(xvap
, XAT_OPAQUE
);
2306 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2307 xoap
->xoa_av_quarantined
=
2308 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0);
2309 XVA_SET_RTN(xvap
, XAT_AV_QUARANTINED
);
2312 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2313 xoap
->xoa_av_modified
=
2314 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0);
2315 XVA_SET_RTN(xvap
, XAT_AV_MODIFIED
);
2318 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) &&
2319 S_ISREG(ip
->i_mode
)) {
2320 zfs_sa_get_scanstamp(zp
, xvap
);
2323 if (XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)) {
2326 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_CRTIME(zsb
),
2327 times
, sizeof (times
));
2328 ZFS_TIME_DECODE(&xoap
->xoa_createtime
, times
);
2329 XVA_SET_RTN(xvap
, XAT_CREATETIME
);
2332 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2333 xoap
->xoa_reparse
= ((zp
->z_pflags
& ZFS_REPARSE
) != 0);
2334 XVA_SET_RTN(xvap
, XAT_REPARSE
);
2336 if (XVA_ISSET_REQ(xvap
, XAT_GEN
)) {
2337 xoap
->xoa_generation
= zp
->z_gen
;
2338 XVA_SET_RTN(xvap
, XAT_GEN
);
2341 if (XVA_ISSET_REQ(xvap
, XAT_OFFLINE
)) {
2343 ((zp
->z_pflags
& ZFS_OFFLINE
) != 0);
2344 XVA_SET_RTN(xvap
, XAT_OFFLINE
);
2347 if (XVA_ISSET_REQ(xvap
, XAT_SPARSE
)) {
2349 ((zp
->z_pflags
& ZFS_SPARSE
) != 0);
2350 XVA_SET_RTN(xvap
, XAT_SPARSE
);
2354 ZFS_TIME_DECODE(&vap
->va_atime
, zp
->z_atime
);
2355 ZFS_TIME_DECODE(&vap
->va_mtime
, mtime
);
2356 ZFS_TIME_DECODE(&vap
->va_ctime
, ctime
);
2358 mutex_exit(&zp
->z_lock
);
2360 sa_object_size(zp
->z_sa_hdl
, &vap
->va_blksize
, &vap
->va_nblocks
);
2362 if (zp
->z_blksz
== 0) {
2364 * Block size hasn't been set; suggest maximal I/O transfers.
2366 vap
->va_blksize
= zsb
->z_max_blksz
;
2372 EXPORT_SYMBOL(zfs_getattr
);
2375 * Get the basic file attributes and place them in the provided kstat
2376 * structure. The inode is assumed to be the authoritative source
2377 * for most of the attributes. However, the znode currently has the
2378 * authoritative atime, blksize, and block count.
2380 * IN: ip - inode of file.
2382 * OUT: sp - kstat values.
2384 * RETURN: 0 (always succeeds)
2388 zfs_getattr_fast(struct inode
*ip
, struct kstat
*sp
)
2390 znode_t
*zp
= ITOZ(ip
);
2391 zfs_sb_t
*zsb
= ITOZSB(ip
);
2393 u_longlong_t nblocks
;
2398 mutex_enter(&zp
->z_lock
);
2400 generic_fillattr(ip
, sp
);
2401 ZFS_TIME_DECODE(&sp
->atime
, zp
->z_atime
);
2403 sa_object_size(zp
->z_sa_hdl
, &blksize
, &nblocks
);
2404 sp
->blksize
= blksize
;
2405 sp
->blocks
= nblocks
;
2407 if (unlikely(zp
->z_blksz
== 0)) {
2409 * Block size hasn't been set; suggest maximal I/O transfers.
2411 sp
->blksize
= zsb
->z_max_blksz
;
2414 mutex_exit(&zp
->z_lock
);
2420 EXPORT_SYMBOL(zfs_getattr_fast
);
2423 * Set the file attributes to the values contained in the
2426 * IN: ip - inode of file to be modified.
2427 * vap - new attribute values.
2428 * If ATTR_XVATTR set, then optional attrs are being set
2429 * flags - ATTR_UTIME set if non-default time values provided.
2430 * - ATTR_NOACLCHECK (CIFS context only).
2431 * cr - credentials of caller.
2433 * RETURN: 0 if success
2434 * error code if failure
2437 * ip - ctime updated, mtime updated if size changed.
2441 zfs_setattr(struct inode
*ip
, vattr_t
*vap
, int flags
, cred_t
*cr
)
2443 znode_t
*zp
= ITOZ(ip
);
2444 zfs_sb_t
*zsb
= ITOZSB(ip
);
2448 xvattr_t
*tmpxvattr
;
2449 uint_t mask
= vap
->va_mask
;
2450 uint_t saved_mask
= 0;
2453 uint64_t new_uid
, new_gid
;
2455 uint64_t mtime
[2], ctime
[2];
2457 int need_policy
= FALSE
;
2459 zfs_fuid_info_t
*fuidp
= NULL
;
2460 xvattr_t
*xvap
= (xvattr_t
*)vap
; /* vap may be an xvattr_t * */
2463 boolean_t skipaclchk
= (flags
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
2464 boolean_t fuid_dirtied
= B_FALSE
;
2465 sa_bulk_attr_t
*bulk
, *xattr_bulk
;
2466 int count
= 0, xattr_count
= 0;
2477 * Make sure that if we have ephemeral uid/gid or xvattr specified
2478 * that file system is at proper version level
2481 if (zsb
->z_use_fuids
== B_FALSE
&&
2482 (((mask
& ATTR_UID
) && IS_EPHEMERAL(vap
->va_uid
)) ||
2483 ((mask
& ATTR_GID
) && IS_EPHEMERAL(vap
->va_gid
)) ||
2484 (mask
& ATTR_XVATTR
))) {
2486 return (SET_ERROR(EINVAL
));
2489 if (mask
& ATTR_SIZE
&& S_ISDIR(ip
->i_mode
)) {
2491 return (SET_ERROR(EISDIR
));
2494 if (mask
& ATTR_SIZE
&& !S_ISREG(ip
->i_mode
) && !S_ISFIFO(ip
->i_mode
)) {
2496 return (SET_ERROR(EINVAL
));
2500 * If this is an xvattr_t, then get a pointer to the structure of
2501 * optional attributes. If this is NULL, then we have a vattr_t.
2503 xoap
= xva_getxoptattr(xvap
);
2505 tmpxvattr
= kmem_alloc(sizeof (xvattr_t
), KM_SLEEP
);
2506 xva_init(tmpxvattr
);
2508 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2509 xattr_bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * 7, KM_SLEEP
);
2512 * Immutable files can only alter immutable bit and atime
2514 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) &&
2515 ((mask
& (ATTR_SIZE
|ATTR_UID
|ATTR_GID
|ATTR_MTIME
|ATTR_MODE
)) ||
2516 ((mask
& ATTR_XVATTR
) && XVA_ISSET_REQ(xvap
, XAT_CREATETIME
)))) {
2521 if ((mask
& ATTR_SIZE
) && (zp
->z_pflags
& ZFS_READONLY
)) {
2527 * Verify timestamps doesn't overflow 32 bits.
2528 * ZFS can handle large timestamps, but 32bit syscalls can't
2529 * handle times greater than 2039. This check should be removed
2530 * once large timestamps are fully supported.
2532 if (mask
& (ATTR_ATIME
| ATTR_MTIME
)) {
2533 if (((mask
& ATTR_ATIME
) &&
2534 TIMESPEC_OVERFLOW(&vap
->va_atime
)) ||
2535 ((mask
& ATTR_MTIME
) &&
2536 TIMESPEC_OVERFLOW(&vap
->va_mtime
))) {
2546 /* Can this be moved to before the top label? */
2547 if (zfs_is_readonly(zsb
)) {
2553 * First validate permissions
2556 if (mask
& ATTR_SIZE
) {
2557 err
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, skipaclchk
, cr
);
2562 * XXX - Note, we are not providing any open
2563 * mode flags here (like FNDELAY), so we may
2564 * block if there are locks present... this
2565 * should be addressed in openat().
2567 /* XXX - would it be OK to generate a log record here? */
2568 err
= zfs_freesp(zp
, vap
->va_size
, 0, 0, FALSE
);
2573 if (mask
& (ATTR_ATIME
|ATTR_MTIME
) ||
2574 ((mask
& ATTR_XVATTR
) && (XVA_ISSET_REQ(xvap
, XAT_HIDDEN
) ||
2575 XVA_ISSET_REQ(xvap
, XAT_READONLY
) ||
2576 XVA_ISSET_REQ(xvap
, XAT_ARCHIVE
) ||
2577 XVA_ISSET_REQ(xvap
, XAT_OFFLINE
) ||
2578 XVA_ISSET_REQ(xvap
, XAT_SPARSE
) ||
2579 XVA_ISSET_REQ(xvap
, XAT_CREATETIME
) ||
2580 XVA_ISSET_REQ(xvap
, XAT_SYSTEM
)))) {
2581 need_policy
= zfs_zaccess(zp
, ACE_WRITE_ATTRIBUTES
, 0,
2585 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2586 int idmask
= (mask
& (ATTR_UID
|ATTR_GID
));
2591 * NOTE: even if a new mode is being set,
2592 * we may clear S_ISUID/S_ISGID bits.
2595 if (!(mask
& ATTR_MODE
))
2596 vap
->va_mode
= zp
->z_mode
;
2599 * Take ownership or chgrp to group we are a member of
2602 take_owner
= (mask
& ATTR_UID
) && (vap
->va_uid
== crgetuid(cr
));
2603 take_group
= (mask
& ATTR_GID
) &&
2604 zfs_groupmember(zsb
, vap
->va_gid
, cr
);
2607 * If both ATTR_UID and ATTR_GID are set then take_owner and
2608 * take_group must both be set in order to allow taking
2611 * Otherwise, send the check through secpolicy_vnode_setattr()
2615 if (((idmask
== (ATTR_UID
|ATTR_GID
)) &&
2616 take_owner
&& take_group
) ||
2617 ((idmask
== ATTR_UID
) && take_owner
) ||
2618 ((idmask
== ATTR_GID
) && take_group
)) {
2619 if (zfs_zaccess(zp
, ACE_WRITE_OWNER
, 0,
2620 skipaclchk
, cr
) == 0) {
2622 * Remove setuid/setgid for non-privileged users
2624 (void) secpolicy_setid_clear(vap
, cr
);
2625 trim_mask
= (mask
& (ATTR_UID
|ATTR_GID
));
2634 mutex_enter(&zp
->z_lock
);
2635 oldva
.va_mode
= zp
->z_mode
;
2636 zfs_fuid_map_ids(zp
, cr
, &oldva
.va_uid
, &oldva
.va_gid
);
2637 if (mask
& ATTR_XVATTR
) {
2639 * Update xvattr mask to include only those attributes
2640 * that are actually changing.
2642 * the bits will be restored prior to actually setting
2643 * the attributes so the caller thinks they were set.
2645 if (XVA_ISSET_REQ(xvap
, XAT_APPENDONLY
)) {
2646 if (xoap
->xoa_appendonly
!=
2647 ((zp
->z_pflags
& ZFS_APPENDONLY
) != 0)) {
2650 XVA_CLR_REQ(xvap
, XAT_APPENDONLY
);
2651 XVA_SET_REQ(tmpxvattr
, XAT_APPENDONLY
);
2655 if (XVA_ISSET_REQ(xvap
, XAT_NOUNLINK
)) {
2656 if (xoap
->xoa_nounlink
!=
2657 ((zp
->z_pflags
& ZFS_NOUNLINK
) != 0)) {
2660 XVA_CLR_REQ(xvap
, XAT_NOUNLINK
);
2661 XVA_SET_REQ(tmpxvattr
, XAT_NOUNLINK
);
2665 if (XVA_ISSET_REQ(xvap
, XAT_IMMUTABLE
)) {
2666 if (xoap
->xoa_immutable
!=
2667 ((zp
->z_pflags
& ZFS_IMMUTABLE
) != 0)) {
2670 XVA_CLR_REQ(xvap
, XAT_IMMUTABLE
);
2671 XVA_SET_REQ(tmpxvattr
, XAT_IMMUTABLE
);
2675 if (XVA_ISSET_REQ(xvap
, XAT_NODUMP
)) {
2676 if (xoap
->xoa_nodump
!=
2677 ((zp
->z_pflags
& ZFS_NODUMP
) != 0)) {
2680 XVA_CLR_REQ(xvap
, XAT_NODUMP
);
2681 XVA_SET_REQ(tmpxvattr
, XAT_NODUMP
);
2685 if (XVA_ISSET_REQ(xvap
, XAT_AV_MODIFIED
)) {
2686 if (xoap
->xoa_av_modified
!=
2687 ((zp
->z_pflags
& ZFS_AV_MODIFIED
) != 0)) {
2690 XVA_CLR_REQ(xvap
, XAT_AV_MODIFIED
);
2691 XVA_SET_REQ(tmpxvattr
, XAT_AV_MODIFIED
);
2695 if (XVA_ISSET_REQ(xvap
, XAT_AV_QUARANTINED
)) {
2696 if ((!S_ISREG(ip
->i_mode
) &&
2697 xoap
->xoa_av_quarantined
) ||
2698 xoap
->xoa_av_quarantined
!=
2699 ((zp
->z_pflags
& ZFS_AV_QUARANTINED
) != 0)) {
2702 XVA_CLR_REQ(xvap
, XAT_AV_QUARANTINED
);
2703 XVA_SET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
);
2707 if (XVA_ISSET_REQ(xvap
, XAT_REPARSE
)) {
2708 mutex_exit(&zp
->z_lock
);
2713 if (need_policy
== FALSE
&&
2714 (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
) ||
2715 XVA_ISSET_REQ(xvap
, XAT_OPAQUE
))) {
2720 mutex_exit(&zp
->z_lock
);
2722 if (mask
& ATTR_MODE
) {
2723 if (zfs_zaccess(zp
, ACE_WRITE_ACL
, 0, skipaclchk
, cr
) == 0) {
2724 err
= secpolicy_setid_setsticky_clear(ip
, vap
,
2729 trim_mask
|= ATTR_MODE
;
2737 * If trim_mask is set then take ownership
2738 * has been granted or write_acl is present and user
2739 * has the ability to modify mode. In that case remove
2740 * UID|GID and or MODE from mask so that
2741 * secpolicy_vnode_setattr() doesn't revoke it.
2745 saved_mask
= vap
->va_mask
;
2746 vap
->va_mask
&= ~trim_mask
;
2748 err
= secpolicy_vnode_setattr(cr
, ip
, vap
, &oldva
, flags
,
2749 (int (*)(void *, int, cred_t
*))zfs_zaccess_unix
, zp
);
2754 vap
->va_mask
|= saved_mask
;
2758 * secpolicy_vnode_setattr, or take ownership may have
2761 mask
= vap
->va_mask
;
2763 if ((mask
& (ATTR_UID
| ATTR_GID
))) {
2764 err
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zsb
),
2765 &xattr_obj
, sizeof (xattr_obj
));
2767 if (err
== 0 && xattr_obj
) {
2768 err
= zfs_zget(ZTOZSB(zp
), xattr_obj
, &attrzp
);
2772 if (mask
& ATTR_UID
) {
2773 new_uid
= zfs_fuid_create(zsb
,
2774 (uint64_t)vap
->va_uid
, cr
, ZFS_OWNER
, &fuidp
);
2775 if (new_uid
!= zp
->z_uid
&&
2776 zfs_fuid_overquota(zsb
, B_FALSE
, new_uid
)) {
2784 if (mask
& ATTR_GID
) {
2785 new_gid
= zfs_fuid_create(zsb
, (uint64_t)vap
->va_gid
,
2786 cr
, ZFS_GROUP
, &fuidp
);
2787 if (new_gid
!= zp
->z_gid
&&
2788 zfs_fuid_overquota(zsb
, B_TRUE
, new_gid
)) {
2796 tx
= dmu_tx_create(zsb
->z_os
);
2798 if (mask
& ATTR_MODE
) {
2799 uint64_t pmode
= zp
->z_mode
;
2801 new_mode
= (pmode
& S_IFMT
) | (vap
->va_mode
& ~S_IFMT
);
2803 zfs_acl_chmod_setattr(zp
, &aclp
, new_mode
);
2805 mutex_enter(&zp
->z_lock
);
2806 if (!zp
->z_is_sa
&& ((acl_obj
= zfs_external_acl(zp
)) != 0)) {
2808 * Are we upgrading ACL from old V0 format
2811 if (zsb
->z_version
>= ZPL_VERSION_FUID
&&
2812 zfs_znode_acl_version(zp
) ==
2813 ZFS_ACL_VERSION_INITIAL
) {
2814 dmu_tx_hold_free(tx
, acl_obj
, 0,
2816 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2817 0, aclp
->z_acl_bytes
);
2819 dmu_tx_hold_write(tx
, acl_obj
, 0,
2822 } else if (!zp
->z_is_sa
&& aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
2823 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
,
2824 0, aclp
->z_acl_bytes
);
2826 mutex_exit(&zp
->z_lock
);
2827 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2829 if ((mask
& ATTR_XVATTR
) &&
2830 XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2831 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
2833 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
2837 dmu_tx_hold_sa(tx
, attrzp
->z_sa_hdl
, B_FALSE
);
2840 fuid_dirtied
= zsb
->z_fuid_dirty
;
2842 zfs_fuid_txhold(zsb
, tx
);
2844 zfs_sa_upgrade_txholds(tx
, zp
);
2846 err
= dmu_tx_assign(tx
, TXG_WAIT
);
2852 * Set each attribute requested.
2853 * We group settings according to the locks they need to acquire.
2855 * Note: you cannot set ctime directly, although it will be
2856 * updated as a side-effect of calling this function.
2860 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2861 mutex_enter(&zp
->z_acl_lock
);
2862 mutex_enter(&zp
->z_lock
);
2864 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
2865 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
2868 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
2869 mutex_enter(&attrzp
->z_acl_lock
);
2870 mutex_enter(&attrzp
->z_lock
);
2871 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2872 SA_ZPL_FLAGS(zsb
), NULL
, &attrzp
->z_pflags
,
2873 sizeof (attrzp
->z_pflags
));
2876 if (mask
& (ATTR_UID
|ATTR_GID
)) {
2878 if (mask
& ATTR_UID
) {
2879 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
,
2880 &new_uid
, sizeof (new_uid
));
2881 zp
->z_uid
= new_uid
;
2883 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2884 SA_ZPL_UID(zsb
), NULL
, &new_uid
,
2886 attrzp
->z_uid
= new_uid
;
2890 if (mask
& ATTR_GID
) {
2891 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
),
2892 NULL
, &new_gid
, sizeof (new_gid
));
2893 zp
->z_gid
= new_gid
;
2895 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2896 SA_ZPL_GID(zsb
), NULL
, &new_gid
,
2898 attrzp
->z_gid
= new_gid
;
2901 if (!(mask
& ATTR_MODE
)) {
2902 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
),
2903 NULL
, &new_mode
, sizeof (new_mode
));
2904 new_mode
= zp
->z_mode
;
2906 err
= zfs_acl_chown_setattr(zp
);
2909 err
= zfs_acl_chown_setattr(attrzp
);
2914 if (mask
& ATTR_MODE
) {
2915 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
,
2916 &new_mode
, sizeof (new_mode
));
2917 zp
->z_mode
= new_mode
;
2918 ASSERT3P(aclp
, !=, NULL
);
2919 err
= zfs_aclset_common(zp
, aclp
, cr
, tx
);
2921 if (zp
->z_acl_cached
)
2922 zfs_acl_free(zp
->z_acl_cached
);
2923 zp
->z_acl_cached
= aclp
;
2928 if (mask
& ATTR_ATIME
) {
2929 ZFS_TIME_ENCODE(&vap
->va_atime
, zp
->z_atime
);
2930 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zsb
), NULL
,
2931 &zp
->z_atime
, sizeof (zp
->z_atime
));
2934 if (mask
& ATTR_MTIME
) {
2935 ZFS_TIME_ENCODE(&vap
->va_mtime
, mtime
);
2936 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
,
2937 mtime
, sizeof (mtime
));
2940 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
2941 if (mask
& ATTR_SIZE
&& !(mask
& ATTR_MTIME
)) {
2942 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
),
2943 NULL
, mtime
, sizeof (mtime
));
2944 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2945 &ctime
, sizeof (ctime
));
2946 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
,
2948 } else if (mask
!= 0) {
2949 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
,
2950 &ctime
, sizeof (ctime
));
2951 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
2954 SA_ADD_BULK_ATTR(xattr_bulk
, xattr_count
,
2955 SA_ZPL_CTIME(zsb
), NULL
,
2956 &ctime
, sizeof (ctime
));
2957 zfs_tstamp_update_setup(attrzp
, STATE_CHANGED
,
2958 mtime
, ctime
, B_TRUE
);
2962 * Do this after setting timestamps to prevent timestamp
2963 * update from toggling bit
2966 if (xoap
&& (mask
& ATTR_XVATTR
)) {
2969 * restore trimmed off masks
2970 * so that return masks can be set for caller.
2973 if (XVA_ISSET_REQ(tmpxvattr
, XAT_APPENDONLY
)) {
2974 XVA_SET_REQ(xvap
, XAT_APPENDONLY
);
2976 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NOUNLINK
)) {
2977 XVA_SET_REQ(xvap
, XAT_NOUNLINK
);
2979 if (XVA_ISSET_REQ(tmpxvattr
, XAT_IMMUTABLE
)) {
2980 XVA_SET_REQ(xvap
, XAT_IMMUTABLE
);
2982 if (XVA_ISSET_REQ(tmpxvattr
, XAT_NODUMP
)) {
2983 XVA_SET_REQ(xvap
, XAT_NODUMP
);
2985 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_MODIFIED
)) {
2986 XVA_SET_REQ(xvap
, XAT_AV_MODIFIED
);
2988 if (XVA_ISSET_REQ(tmpxvattr
, XAT_AV_QUARANTINED
)) {
2989 XVA_SET_REQ(xvap
, XAT_AV_QUARANTINED
);
2992 if (XVA_ISSET_REQ(xvap
, XAT_AV_SCANSTAMP
))
2993 ASSERT(S_ISREG(ip
->i_mode
));
2995 zfs_xvattr_set(zp
, xvap
, tx
);
2999 zfs_fuid_sync(zsb
, tx
);
3002 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, vap
, mask
, fuidp
);
3004 mutex_exit(&zp
->z_lock
);
3005 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3006 mutex_exit(&zp
->z_acl_lock
);
3009 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
))
3010 mutex_exit(&attrzp
->z_acl_lock
);
3011 mutex_exit(&attrzp
->z_lock
);
3014 if (err
== 0 && attrzp
) {
3015 err2
= sa_bulk_update(attrzp
->z_sa_hdl
, xattr_bulk
,
3026 zfs_fuid_info_free(fuidp
);
3032 if (err
== ERESTART
)
3035 err2
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
3037 zfs_inode_update(zp
);
3041 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3042 zil_commit(zilog
, 0);
3045 kmem_free(xattr_bulk
, sizeof (sa_bulk_attr_t
) * 7);
3046 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * 7);
3047 kmem_free(tmpxvattr
, sizeof (xvattr_t
));
3051 EXPORT_SYMBOL(zfs_setattr
);
3053 typedef struct zfs_zlock
{
3054 krwlock_t
*zl_rwlock
; /* lock we acquired */
3055 znode_t
*zl_znode
; /* znode we held */
3056 struct zfs_zlock
*zl_next
; /* next in list */
3060 * Drop locks and release vnodes that were held by zfs_rename_lock().
3063 zfs_rename_unlock(zfs_zlock_t
**zlpp
)
3067 while ((zl
= *zlpp
) != NULL
) {
3068 if (zl
->zl_znode
!= NULL
)
3069 iput(ZTOI(zl
->zl_znode
));
3070 rw_exit(zl
->zl_rwlock
);
3071 *zlpp
= zl
->zl_next
;
3072 kmem_free(zl
, sizeof (*zl
));
3077 * Search back through the directory tree, using the ".." entries.
3078 * Lock each directory in the chain to prevent concurrent renames.
3079 * Fail any attempt to move a directory into one of its own descendants.
3080 * XXX - z_parent_lock can overlap with map or grow locks
3083 zfs_rename_lock(znode_t
*szp
, znode_t
*tdzp
, znode_t
*sdzp
, zfs_zlock_t
**zlpp
)
3087 uint64_t rootid
= ZTOZSB(zp
)->z_root
;
3088 uint64_t oidp
= zp
->z_id
;
3089 krwlock_t
*rwlp
= &szp
->z_parent_lock
;
3090 krw_t rw
= RW_WRITER
;
3093 * First pass write-locks szp and compares to zp->z_id.
3094 * Later passes read-lock zp and compare to zp->z_parent.
3097 if (!rw_tryenter(rwlp
, rw
)) {
3099 * Another thread is renaming in this path.
3100 * Note that if we are a WRITER, we don't have any
3101 * parent_locks held yet.
3103 if (rw
== RW_READER
&& zp
->z_id
> szp
->z_id
) {
3105 * Drop our locks and restart
3107 zfs_rename_unlock(&zl
);
3111 rwlp
= &szp
->z_parent_lock
;
3116 * Wait for other thread to drop its locks
3122 zl
= kmem_alloc(sizeof (*zl
), KM_SLEEP
);
3123 zl
->zl_rwlock
= rwlp
;
3124 zl
->zl_znode
= NULL
;
3125 zl
->zl_next
= *zlpp
;
3128 if (oidp
== szp
->z_id
) /* We're a descendant of szp */
3129 return (SET_ERROR(EINVAL
));
3131 if (oidp
== rootid
) /* We've hit the top */
3134 if (rw
== RW_READER
) { /* i.e. not the first pass */
3135 int error
= zfs_zget(ZTOZSB(zp
), oidp
, &zp
);
3140 (void) sa_lookup(zp
->z_sa_hdl
, SA_ZPL_PARENT(ZTOZSB(zp
)),
3141 &oidp
, sizeof (oidp
));
3142 rwlp
= &zp
->z_parent_lock
;
3145 } while (zp
->z_id
!= sdzp
->z_id
);
3151 * Move an entry from the provided source directory to the target
3152 * directory. Change the entry name as indicated.
3154 * IN: sdip - Source directory containing the "old entry".
3155 * snm - Old entry name.
3156 * tdip - Target directory to contain the "new entry".
3157 * tnm - New entry name.
3158 * cr - credentials of caller.
3159 * flags - case flags
3161 * RETURN: 0 on success, error code on failure.
3164 * sdip,tdip - ctime|mtime updated
3168 zfs_rename(struct inode
*sdip
, char *snm
, struct inode
*tdip
, char *tnm
,
3169 cred_t
*cr
, int flags
)
3171 znode_t
*tdzp
, *szp
, *tzp
;
3172 znode_t
*sdzp
= ITOZ(sdip
);
3173 zfs_sb_t
*zsb
= ITOZSB(sdip
);
3175 zfs_dirlock_t
*sdl
, *tdl
;
3178 int cmp
, serr
, terr
;
3181 boolean_t waited
= B_FALSE
;
3184 ZFS_VERIFY_ZP(sdzp
);
3187 if (tdip
->i_sb
!= sdip
->i_sb
|| zfsctl_is_node(tdip
)) {
3189 return (SET_ERROR(EXDEV
));
3193 ZFS_VERIFY_ZP(tdzp
);
3194 if (zsb
->z_utf8
&& u8_validate(tnm
,
3195 strlen(tnm
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3197 return (SET_ERROR(EILSEQ
));
3200 if (flags
& FIGNORECASE
)
3209 * This is to prevent the creation of links into attribute space
3210 * by renaming a linked file into/outof an attribute directory.
3211 * See the comment in zfs_link() for why this is considered bad.
3213 if ((tdzp
->z_pflags
& ZFS_XATTR
) != (sdzp
->z_pflags
& ZFS_XATTR
)) {
3215 return (SET_ERROR(EINVAL
));
3219 * Lock source and target directory entries. To prevent deadlock,
3220 * a lock ordering must be defined. We lock the directory with
3221 * the smallest object id first, or if it's a tie, the one with
3222 * the lexically first name.
3224 if (sdzp
->z_id
< tdzp
->z_id
) {
3226 } else if (sdzp
->z_id
> tdzp
->z_id
) {
3230 * First compare the two name arguments without
3231 * considering any case folding.
3233 int nofold
= (zsb
->z_norm
& ~U8_TEXTPREP_TOUPPER
);
3235 cmp
= u8_strcmp(snm
, tnm
, 0, nofold
, U8_UNICODE_LATEST
, &error
);
3236 ASSERT(error
== 0 || !zsb
->z_utf8
);
3239 * POSIX: "If the old argument and the new argument
3240 * both refer to links to the same existing file,
3241 * the rename() function shall return successfully
3242 * and perform no other action."
3248 * If the file system is case-folding, then we may
3249 * have some more checking to do. A case-folding file
3250 * system is either supporting mixed case sensitivity
3251 * access or is completely case-insensitive. Note
3252 * that the file system is always case preserving.
3254 * In mixed sensitivity mode case sensitive behavior
3255 * is the default. FIGNORECASE must be used to
3256 * explicitly request case insensitive behavior.
3258 * If the source and target names provided differ only
3259 * by case (e.g., a request to rename 'tim' to 'Tim'),
3260 * we will treat this as a special case in the
3261 * case-insensitive mode: as long as the source name
3262 * is an exact match, we will allow this to proceed as
3263 * a name-change request.
3265 if ((zsb
->z_case
== ZFS_CASE_INSENSITIVE
||
3266 (zsb
->z_case
== ZFS_CASE_MIXED
&&
3267 flags
& FIGNORECASE
)) &&
3268 u8_strcmp(snm
, tnm
, 0, zsb
->z_norm
, U8_UNICODE_LATEST
,
3271 * case preserving rename request, require exact
3280 * If the source and destination directories are the same, we should
3281 * grab the z_name_lock of that directory only once.
3285 rw_enter(&sdzp
->z_name_lock
, RW_READER
);
3289 serr
= zfs_dirent_lock(&sdl
, sdzp
, snm
, &szp
,
3290 ZEXISTS
| zflg
, NULL
, NULL
);
3291 terr
= zfs_dirent_lock(&tdl
,
3292 tdzp
, tnm
, &tzp
, ZRENAMING
| zflg
, NULL
, NULL
);
3294 terr
= zfs_dirent_lock(&tdl
,
3295 tdzp
, tnm
, &tzp
, zflg
, NULL
, NULL
);
3296 serr
= zfs_dirent_lock(&sdl
,
3297 sdzp
, snm
, &szp
, ZEXISTS
| ZRENAMING
| zflg
,
3303 * Source entry invalid or not there.
3306 zfs_dirent_unlock(tdl
);
3312 rw_exit(&sdzp
->z_name_lock
);
3314 if (strcmp(snm
, "..") == 0)
3320 zfs_dirent_unlock(sdl
);
3324 rw_exit(&sdzp
->z_name_lock
);
3326 if (strcmp(tnm
, "..") == 0)
3333 * Must have write access at the source to remove the old entry
3334 * and write access at the target to create the new entry.
3335 * Note that if target and source are the same, this can be
3336 * done in a single check.
3339 if ((error
= zfs_zaccess_rename(sdzp
, szp
, tdzp
, tzp
, cr
)))
3342 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3344 * Check to make sure rename is valid.
3345 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3347 if ((error
= zfs_rename_lock(szp
, tdzp
, sdzp
, &zl
)))
3352 * Does target exist?
3356 * Source and target must be the same type.
3358 if (S_ISDIR(ZTOI(szp
)->i_mode
)) {
3359 if (!S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3360 error
= SET_ERROR(ENOTDIR
);
3364 if (S_ISDIR(ZTOI(tzp
)->i_mode
)) {
3365 error
= SET_ERROR(EISDIR
);
3370 * POSIX dictates that when the source and target
3371 * entries refer to the same file object, rename
3372 * must do nothing and exit without error.
3374 if (szp
->z_id
== tzp
->z_id
) {
3380 tx
= dmu_tx_create(zsb
->z_os
);
3381 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3382 dmu_tx_hold_sa(tx
, sdzp
->z_sa_hdl
, B_FALSE
);
3383 dmu_tx_hold_zap(tx
, sdzp
->z_id
, FALSE
, snm
);
3384 dmu_tx_hold_zap(tx
, tdzp
->z_id
, TRUE
, tnm
);
3386 dmu_tx_hold_sa(tx
, tdzp
->z_sa_hdl
, B_FALSE
);
3387 zfs_sa_upgrade_txholds(tx
, tdzp
);
3390 dmu_tx_hold_sa(tx
, tzp
->z_sa_hdl
, B_FALSE
);
3391 zfs_sa_upgrade_txholds(tx
, tzp
);
3394 zfs_sa_upgrade_txholds(tx
, szp
);
3395 dmu_tx_hold_zap(tx
, zsb
->z_unlinkedobj
, FALSE
, NULL
);
3396 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3399 zfs_rename_unlock(&zl
);
3400 zfs_dirent_unlock(sdl
);
3401 zfs_dirent_unlock(tdl
);
3404 rw_exit(&sdzp
->z_name_lock
);
3409 if (error
== ERESTART
) {
3420 if (tzp
) /* Attempt to remove the existing target */
3421 error
= zfs_link_destroy(tdl
, tzp
, tx
, zflg
, NULL
);
3424 error
= zfs_link_create(tdl
, szp
, tx
, ZRENAMING
);
3426 szp
->z_pflags
|= ZFS_AV_MODIFIED
;
3428 error
= sa_update(szp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
3429 (void *)&szp
->z_pflags
, sizeof (uint64_t), tx
);
3432 error
= zfs_link_destroy(sdl
, szp
, tx
, ZRENAMING
, NULL
);
3434 zfs_log_rename(zilog
, tx
, TX_RENAME
|
3435 (flags
& FIGNORECASE
? TX_CI
: 0), sdzp
,
3436 sdl
->dl_name
, tdzp
, tdl
->dl_name
, szp
);
3439 * At this point, we have successfully created
3440 * the target name, but have failed to remove
3441 * the source name. Since the create was done
3442 * with the ZRENAMING flag, there are
3443 * complications; for one, the link count is
3444 * wrong. The easiest way to deal with this
3445 * is to remove the newly created target, and
3446 * return the original error. This must
3447 * succeed; fortunately, it is very unlikely to
3448 * fail, since we just created it.
3450 VERIFY3U(zfs_link_destroy(tdl
, szp
, tx
,
3451 ZRENAMING
, NULL
), ==, 0);
3459 zfs_rename_unlock(&zl
);
3461 zfs_dirent_unlock(sdl
);
3462 zfs_dirent_unlock(tdl
);
3464 zfs_inode_update(sdzp
);
3466 rw_exit(&sdzp
->z_name_lock
);
3469 zfs_inode_update(tdzp
);
3471 zfs_inode_update(szp
);
3474 zfs_inode_update(tzp
);
3478 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3479 zil_commit(zilog
, 0);
3484 EXPORT_SYMBOL(zfs_rename
);
3487 * Insert the indicated symbolic reference entry into the directory.
3489 * IN: dip - Directory to contain new symbolic link.
3490 * link - Name for new symlink entry.
3491 * vap - Attributes of new entry.
3492 * target - Target path of new symlink.
3494 * cr - credentials of caller.
3495 * flags - case flags
3497 * RETURN: 0 on success, error code on failure.
3500 * dip - ctime|mtime updated
3504 zfs_symlink(struct inode
*dip
, char *name
, vattr_t
*vap
, char *link
,
3505 struct inode
**ipp
, cred_t
*cr
, int flags
)
3507 znode_t
*zp
, *dzp
= ITOZ(dip
);
3510 zfs_sb_t
*zsb
= ITOZSB(dip
);
3512 uint64_t len
= strlen(link
);
3515 zfs_acl_ids_t acl_ids
;
3516 boolean_t fuid_dirtied
;
3517 uint64_t txtype
= TX_SYMLINK
;
3518 boolean_t waited
= B_FALSE
;
3520 ASSERT(S_ISLNK(vap
->va_mode
));
3526 if (zsb
->z_utf8
&& u8_validate(name
, strlen(name
),
3527 NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3529 return (SET_ERROR(EILSEQ
));
3531 if (flags
& FIGNORECASE
)
3534 if (len
> MAXPATHLEN
) {
3536 return (SET_ERROR(ENAMETOOLONG
));
3539 if ((error
= zfs_acl_ids_create(dzp
, 0,
3540 vap
, cr
, NULL
, &acl_ids
)) != 0) {
3548 * Attempt to lock directory; fail if entry already exists.
3550 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zflg
, NULL
, NULL
);
3552 zfs_acl_ids_free(&acl_ids
);
3557 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3558 zfs_acl_ids_free(&acl_ids
);
3559 zfs_dirent_unlock(dl
);
3564 if (zfs_acl_ids_overquota(zsb
, &acl_ids
)) {
3565 zfs_acl_ids_free(&acl_ids
);
3566 zfs_dirent_unlock(dl
);
3568 return (SET_ERROR(EDQUOT
));
3570 tx
= dmu_tx_create(zsb
->z_os
);
3571 fuid_dirtied
= zsb
->z_fuid_dirty
;
3572 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0, MAX(1, len
));
3573 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3574 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
3575 ZFS_SA_BASE_ATTR_SIZE
+ len
);
3576 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
3577 if (!zsb
->z_use_sa
&& acl_ids
.z_aclp
->z_acl_bytes
> ZFS_ACE_SPACE
) {
3578 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
3579 acl_ids
.z_aclp
->z_acl_bytes
);
3582 zfs_fuid_txhold(zsb
, tx
);
3583 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3585 zfs_dirent_unlock(dl
);
3586 if (error
== ERESTART
) {
3592 zfs_acl_ids_free(&acl_ids
);
3599 * Create a new object for the symlink.
3600 * for version 4 ZPL datsets the symlink will be an SA attribute
3602 zfs_mknode(dzp
, vap
, tx
, cr
, 0, &zp
, &acl_ids
);
3605 zfs_fuid_sync(zsb
, tx
);
3607 mutex_enter(&zp
->z_lock
);
3609 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_SYMLINK(zsb
),
3612 zfs_sa_symlink(zp
, link
, len
, tx
);
3613 mutex_exit(&zp
->z_lock
);
3616 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zsb
),
3617 &zp
->z_size
, sizeof (zp
->z_size
), tx
);
3619 * Insert the new object into the directory.
3621 (void) zfs_link_create(dl
, zp
, tx
, ZNEW
);
3623 if (flags
& FIGNORECASE
)
3625 zfs_log_symlink(zilog
, tx
, txtype
, dzp
, zp
, name
, link
);
3627 zfs_inode_update(dzp
);
3628 zfs_inode_update(zp
);
3630 zfs_acl_ids_free(&acl_ids
);
3634 zfs_dirent_unlock(dl
);
3638 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3639 zil_commit(zilog
, 0);
3644 EXPORT_SYMBOL(zfs_symlink
);
3647 * Return, in the buffer contained in the provided uio structure,
3648 * the symbolic path referred to by ip.
3650 * IN: ip - inode of symbolic link
3651 * uio - structure to contain the link path.
3652 * cr - credentials of caller.
3654 * RETURN: 0 if success
3655 * error code if failure
3658 * ip - atime updated
3662 zfs_readlink(struct inode
*ip
, uio_t
*uio
, cred_t
*cr
)
3664 znode_t
*zp
= ITOZ(ip
);
3665 zfs_sb_t
*zsb
= ITOZSB(ip
);
3671 mutex_enter(&zp
->z_lock
);
3673 error
= sa_lookup_uio(zp
->z_sa_hdl
,
3674 SA_ZPL_SYMLINK(zsb
), uio
);
3676 error
= zfs_sa_readlink(zp
, uio
);
3677 mutex_exit(&zp
->z_lock
);
3679 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
3683 EXPORT_SYMBOL(zfs_readlink
);
3686 * Insert a new entry into directory tdip referencing sip.
3688 * IN: tdip - Directory to contain new entry.
3689 * sip - inode of new entry.
3690 * name - name of new entry.
3691 * cr - credentials of caller.
3693 * RETURN: 0 if success
3694 * error code if failure
3697 * tdip - ctime|mtime updated
3698 * sip - ctime updated
3702 zfs_link(struct inode
*tdip
, struct inode
*sip
, char *name
, cred_t
*cr
)
3704 znode_t
*dzp
= ITOZ(tdip
);
3706 zfs_sb_t
*zsb
= ITOZSB(tdip
);
3714 boolean_t waited
= B_FALSE
;
3716 ASSERT(S_ISDIR(tdip
->i_mode
));
3723 * POSIX dictates that we return EPERM here.
3724 * Better choices include ENOTSUP or EISDIR.
3726 if (S_ISDIR(sip
->i_mode
)) {
3728 return (SET_ERROR(EPERM
));
3731 if (sip
->i_sb
!= tdip
->i_sb
|| zfsctl_is_node(sip
)) {
3733 return (SET_ERROR(EXDEV
));
3739 /* Prevent links to .zfs/shares files */
3741 if ((error
= sa_lookup(szp
->z_sa_hdl
, SA_ZPL_PARENT(zsb
),
3742 &parent
, sizeof (uint64_t))) != 0) {
3746 if (parent
== zsb
->z_shares_dir
) {
3748 return (SET_ERROR(EPERM
));
3751 if (zsb
->z_utf8
&& u8_validate(name
,
3752 strlen(name
), NULL
, U8_VALIDATE_ENTIRE
, &error
) < 0) {
3754 return (SET_ERROR(EILSEQ
));
3756 #ifdef HAVE_PN_UTILS
3757 if (flags
& FIGNORECASE
)
3759 #endif /* HAVE_PN_UTILS */
3762 * We do not support links between attributes and non-attributes
3763 * because of the potential security risk of creating links
3764 * into "normal" file space in order to circumvent restrictions
3765 * imposed in attribute space.
3767 if ((szp
->z_pflags
& ZFS_XATTR
) != (dzp
->z_pflags
& ZFS_XATTR
)) {
3769 return (SET_ERROR(EINVAL
));
3772 owner
= zfs_fuid_map_id(zsb
, szp
->z_uid
, cr
, ZFS_OWNER
);
3773 if (owner
!= crgetuid(cr
) && secpolicy_basic_link(cr
) != 0) {
3775 return (SET_ERROR(EPERM
));
3778 if ((error
= zfs_zaccess(dzp
, ACE_ADD_FILE
, 0, B_FALSE
, cr
))) {
3785 * Attempt to lock directory; fail if entry already exists.
3787 error
= zfs_dirent_lock(&dl
, dzp
, name
, &tzp
, zf
, NULL
, NULL
);
3793 tx
= dmu_tx_create(zsb
->z_os
);
3794 dmu_tx_hold_sa(tx
, szp
->z_sa_hdl
, B_FALSE
);
3795 dmu_tx_hold_zap(tx
, dzp
->z_id
, TRUE
, name
);
3796 zfs_sa_upgrade_txholds(tx
, szp
);
3797 zfs_sa_upgrade_txholds(tx
, dzp
);
3798 error
= dmu_tx_assign(tx
, waited
? TXG_WAITED
: TXG_NOWAIT
);
3800 zfs_dirent_unlock(dl
);
3801 if (error
== ERESTART
) {
3812 error
= zfs_link_create(dl
, szp
, tx
, 0);
3815 uint64_t txtype
= TX_LINK
;
3816 #ifdef HAVE_PN_UTILS
3817 if (flags
& FIGNORECASE
)
3819 #endif /* HAVE_PN_UTILS */
3820 zfs_log_link(zilog
, tx
, txtype
, dzp
, szp
, name
);
3825 zfs_dirent_unlock(dl
);
3827 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
3828 zil_commit(zilog
, 0);
3830 zfs_inode_update(dzp
);
3831 zfs_inode_update(szp
);
3835 EXPORT_SYMBOL(zfs_link
);
3838 zfs_putpage_commit_cb(void *arg
)
3840 struct page
*pp
= arg
;
3843 end_page_writeback(pp
);
3847 * Push a page out to disk, once the page is on stable storage the
3848 * registered commit callback will be run as notification of completion.
3850 * IN: ip - page mapped for inode.
3851 * pp - page to push (page is locked)
3852 * wbc - writeback control data
3854 * RETURN: 0 if success
3855 * error code if failure
3858 * ip - ctime|mtime updated
3862 zfs_putpage(struct inode
*ip
, struct page
*pp
, struct writeback_control
*wbc
)
3864 znode_t
*zp
= ITOZ(ip
);
3865 zfs_sb_t
*zsb
= ITOZSB(ip
);
3873 uint64_t mtime
[2], ctime
[2];
3874 sa_bulk_attr_t bulk
[3];
3880 ASSERT(PageLocked(pp
));
3882 pgoff
= page_offset(pp
); /* Page byte-offset in file */
3883 offset
= i_size_read(ip
); /* File length in bytes */
3884 pglen
= MIN(PAGE_CACHE_SIZE
, /* Page length in bytes */
3885 P2ROUNDUP(offset
, PAGE_CACHE_SIZE
)-pgoff
);
3887 /* Page is beyond end of file */
3888 if (pgoff
>= offset
) {
3894 /* Truncate page length to end of file */
3895 if (pgoff
+ pglen
> offset
)
3896 pglen
= offset
- pgoff
;
3900 * FIXME: Allow mmap writes past its quota. The correct fix
3901 * is to register a page_mkwrite() handler to count the page
3902 * against its quota when it is about to be dirtied.
3904 if (zfs_owner_overquota(zsb
, zp
, B_FALSE
) ||
3905 zfs_owner_overquota(zsb
, zp
, B_TRUE
)) {
3911 * The ordering here is critical and must adhere to the following
3912 * rules in order to avoid deadlocking in either zfs_read() or
3913 * zfs_free_range() due to a lock inversion.
3915 * 1) The page must be unlocked prior to acquiring the range lock.
3916 * This is critical because zfs_read() calls find_lock_page()
3917 * which may block on the page lock while holding the range lock.
3919 * 2) Before setting or clearing write back on a page the range lock
3920 * must be held in order to prevent a lock inversion with the
3921 * zfs_free_range() function.
3924 rl
= zfs_range_lock(zp
, pgoff
, pglen
, RL_WRITER
);
3925 set_page_writeback(pp
);
3927 tx
= dmu_tx_create(zsb
->z_os
);
3928 dmu_tx_hold_write(tx
, zp
->z_id
, pgoff
, pglen
);
3929 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
3930 zfs_sa_upgrade_txholds(tx
, zp
);
3932 err
= dmu_tx_assign(tx
, TXG_NOWAIT
);
3934 if (err
== ERESTART
)
3938 __set_page_dirty_nobuffers(pp
);
3940 end_page_writeback(pp
);
3941 zfs_range_unlock(rl
);
3947 ASSERT3U(pglen
, <=, PAGE_CACHE_SIZE
);
3948 dmu_write(zsb
->z_os
, zp
->z_id
, pgoff
, pglen
, va
, tx
);
3951 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
3952 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
3953 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_FLAGS(zsb
), NULL
, &zp
->z_pflags
, 8);
3955 /* Preserve the mtime and ctime provided by the inode */
3956 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
3957 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
3958 zp
->z_atime_dirty
= 0;
3961 err
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
3963 zfs_log_write(zsb
->z_log
, tx
, TX_WRITE
, zp
, pgoff
, pglen
, 0,
3964 zfs_putpage_commit_cb
, pp
);
3967 zfs_range_unlock(rl
);
3969 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
3971 * Note that this is rarely called under writepages(), because
3972 * writepages() normally handles the entire commit for
3973 * performance reasons.
3975 if (zsb
->z_log
!= NULL
)
3976 zil_commit(zsb
->z_log
, zp
->z_id
);
3984 * Update the system attributes when the inode has been dirtied. For the
3985 * moment we only update the mode, atime, mtime, and ctime.
3988 zfs_dirty_inode(struct inode
*ip
, int flags
)
3990 znode_t
*zp
= ITOZ(ip
);
3991 zfs_sb_t
*zsb
= ITOZSB(ip
);
3993 uint64_t mode
, atime
[2], mtime
[2], ctime
[2];
3994 sa_bulk_attr_t bulk
[4];
3998 if (zfs_is_readonly(zsb
) || dmu_objset_is_snapshot(zsb
->z_os
))
4004 tx
= dmu_tx_create(zsb
->z_os
);
4006 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4007 zfs_sa_upgrade_txholds(tx
, zp
);
4009 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4015 mutex_enter(&zp
->z_lock
);
4016 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
4017 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_ATIME(zsb
), NULL
, &atime
, 16);
4018 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
4019 SA_ADD_BULK_ATTR(bulk
, cnt
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
4021 /* Preserve the mode, mtime and ctime provided by the inode */
4022 ZFS_TIME_ENCODE(&ip
->i_atime
, atime
);
4023 ZFS_TIME_ENCODE(&ip
->i_mtime
, mtime
);
4024 ZFS_TIME_ENCODE(&ip
->i_ctime
, ctime
);
4028 zp
->z_atime_dirty
= 0;
4030 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, cnt
, tx
);
4031 mutex_exit(&zp
->z_lock
);
4038 EXPORT_SYMBOL(zfs_dirty_inode
);
4042 zfs_inactive(struct inode
*ip
)
4044 znode_t
*zp
= ITOZ(ip
);
4045 zfs_sb_t
*zsb
= ITOZSB(ip
);
4048 if (zfsctl_is_node(ip
)) {
4049 zfsctl_inode_inactive(ip
);
4053 rw_enter(&zsb
->z_teardown_inactive_lock
, RW_READER
);
4054 if (zp
->z_sa_hdl
== NULL
) {
4055 rw_exit(&zsb
->z_teardown_inactive_lock
);
4059 if (zp
->z_atime_dirty
&& zp
->z_unlinked
== 0) {
4060 dmu_tx_t
*tx
= dmu_tx_create(zsb
->z_os
);
4062 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
4063 zfs_sa_upgrade_txholds(tx
, zp
);
4064 error
= dmu_tx_assign(tx
, TXG_WAIT
);
4068 mutex_enter(&zp
->z_lock
);
4069 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_ATIME(zsb
),
4070 (void *)&zp
->z_atime
, sizeof (zp
->z_atime
), tx
);
4071 zp
->z_atime_dirty
= 0;
4072 mutex_exit(&zp
->z_lock
);
4078 rw_exit(&zsb
->z_teardown_inactive_lock
);
4080 EXPORT_SYMBOL(zfs_inactive
);
4083 * Bounds-check the seek operation.
4085 * IN: ip - inode seeking within
4086 * ooff - old file offset
4087 * noffp - pointer to new file offset
4088 * ct - caller context
4090 * RETURN: 0 if success
4091 * EINVAL if new offset invalid
4095 zfs_seek(struct inode
*ip
, offset_t ooff
, offset_t
*noffp
)
4097 if (S_ISDIR(ip
->i_mode
))
4099 return ((*noffp
< 0 || *noffp
> MAXOFFSET_T
) ? EINVAL
: 0);
4101 EXPORT_SYMBOL(zfs_seek
);
4104 * Fill pages with data from the disk.
4107 zfs_fillpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4109 znode_t
*zp
= ITOZ(ip
);
4110 zfs_sb_t
*zsb
= ITOZSB(ip
);
4112 struct page
*cur_pp
;
4113 u_offset_t io_off
, total
;
4120 io_len
= nr_pages
<< PAGE_CACHE_SHIFT
;
4121 i_size
= i_size_read(ip
);
4122 io_off
= page_offset(pl
[0]);
4124 if (io_off
+ io_len
> i_size
)
4125 io_len
= i_size
- io_off
;
4128 * Iterate over list of pages and read each page individually.
4132 for (total
= io_off
+ io_len
; io_off
< total
; io_off
+= PAGESIZE
) {
4136 err
= dmu_read(os
, zp
->z_id
, io_off
, PAGESIZE
, va
,
4140 /* convert checksum errors into IO errors */
4142 err
= SET_ERROR(EIO
);
4145 cur_pp
= pl
[++page_idx
];
4152 * Uses zfs_fillpage to read data from the file and fill the pages.
4154 * IN: ip - inode of file to get data from.
4155 * pl - list of pages to read
4156 * nr_pages - number of pages to read
4158 * RETURN: 0 on success, error code on failure.
4161 * vp - atime updated
4165 zfs_getpage(struct inode
*ip
, struct page
*pl
[], int nr_pages
)
4167 znode_t
*zp
= ITOZ(ip
);
4168 zfs_sb_t
*zsb
= ITOZSB(ip
);
4177 err
= zfs_fillpage(ip
, pl
, nr_pages
);
4180 ZFS_ACCESSTIME_STAMP(zsb
, zp
);
4185 EXPORT_SYMBOL(zfs_getpage
);
4188 * Check ZFS specific permissions to memory map a section of a file.
4190 * IN: ip - inode of the file to mmap
4192 * addrp - start address in memory region
4193 * len - length of memory region
4194 * vm_flags- address flags
4196 * RETURN: 0 if success
4197 * error code if failure
4201 zfs_map(struct inode
*ip
, offset_t off
, caddr_t
*addrp
, size_t len
,
4202 unsigned long vm_flags
)
4204 znode_t
*zp
= ITOZ(ip
);
4205 zfs_sb_t
*zsb
= ITOZSB(ip
);
4210 if ((vm_flags
& VM_WRITE
) && (zp
->z_pflags
&
4211 (ZFS_IMMUTABLE
| ZFS_READONLY
| ZFS_APPENDONLY
))) {
4213 return (SET_ERROR(EPERM
));
4216 if ((vm_flags
& (VM_READ
| VM_EXEC
)) &&
4217 (zp
->z_pflags
& ZFS_AV_QUARANTINED
)) {
4219 return (SET_ERROR(EACCES
));
4222 if (off
< 0 || len
> MAXOFFSET_T
- off
) {
4224 return (SET_ERROR(ENXIO
));
4230 EXPORT_SYMBOL(zfs_map
);
4233 * convoff - converts the given data (start, whence) to the
4237 convoff(struct inode
*ip
, flock64_t
*lckdat
, int whence
, offset_t offset
)
4242 if ((lckdat
->l_whence
== 2) || (whence
== 2)) {
4243 if ((error
= zfs_getattr(ip
, &vap
, 0, CRED()) != 0))
4247 switch (lckdat
->l_whence
) {
4249 lckdat
->l_start
+= offset
;
4252 lckdat
->l_start
+= vap
.va_size
;
4257 return (SET_ERROR(EINVAL
));
4260 if (lckdat
->l_start
< 0)
4261 return (SET_ERROR(EINVAL
));
4265 lckdat
->l_start
-= offset
;
4268 lckdat
->l_start
-= vap
.va_size
;
4273 return (SET_ERROR(EINVAL
));
4276 lckdat
->l_whence
= (short)whence
;
4281 * Free or allocate space in a file. Currently, this function only
4282 * supports the `F_FREESP' command. However, this command is somewhat
4283 * misnamed, as its functionality includes the ability to allocate as
4284 * well as free space.
4286 * IN: ip - inode of file to free data in.
4287 * cmd - action to take (only F_FREESP supported).
4288 * bfp - section of file to free/alloc.
4289 * flag - current file open mode flags.
4290 * offset - current file offset.
4291 * cr - credentials of caller [UNUSED].
4293 * RETURN: 0 on success, error code on failure.
4296 * ip - ctime|mtime updated
4300 zfs_space(struct inode
*ip
, int cmd
, flock64_t
*bfp
, int flag
,
4301 offset_t offset
, cred_t
*cr
)
4303 znode_t
*zp
= ITOZ(ip
);
4304 zfs_sb_t
*zsb
= ITOZSB(ip
);
4311 if (cmd
!= F_FREESP
) {
4313 return (SET_ERROR(EINVAL
));
4316 if ((error
= convoff(ip
, bfp
, 0, offset
))) {
4321 if (bfp
->l_len
< 0) {
4323 return (SET_ERROR(EINVAL
));
4327 * Permissions aren't checked on Solaris because on this OS
4328 * zfs_space() can only be called with an opened file handle.
4329 * On Linux we can get here through truncate_range() which
4330 * operates directly on inodes, so we need to check access rights.
4332 if ((error
= zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
))) {
4338 len
= bfp
->l_len
; /* 0 means from off to end of file */
4340 error
= zfs_freesp(zp
, off
, len
, flag
, TRUE
);
4345 EXPORT_SYMBOL(zfs_space
);
4349 zfs_fid(struct inode
*ip
, fid_t
*fidp
)
4351 znode_t
*zp
= ITOZ(ip
);
4352 zfs_sb_t
*zsb
= ITOZSB(ip
);
4355 uint64_t object
= zp
->z_id
;
4362 if ((error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zsb
),
4363 &gen64
, sizeof (uint64_t))) != 0) {
4368 gen
= (uint32_t)gen64
;
4370 size
= (zsb
->z_parent
!= zsb
) ? LONG_FID_LEN
: SHORT_FID_LEN
;
4371 if (fidp
->fid_len
< size
) {
4372 fidp
->fid_len
= size
;
4374 return (SET_ERROR(ENOSPC
));
4377 zfid
= (zfid_short_t
*)fidp
;
4379 zfid
->zf_len
= size
;
4381 for (i
= 0; i
< sizeof (zfid
->zf_object
); i
++)
4382 zfid
->zf_object
[i
] = (uint8_t)(object
>> (8 * i
));
4384 /* Must have a non-zero generation number to distinguish from .zfs */
4387 for (i
= 0; i
< sizeof (zfid
->zf_gen
); i
++)
4388 zfid
->zf_gen
[i
] = (uint8_t)(gen
>> (8 * i
));
4390 if (size
== LONG_FID_LEN
) {
4391 uint64_t objsetid
= dmu_objset_id(zsb
->z_os
);
4394 zlfid
= (zfid_long_t
*)fidp
;
4396 for (i
= 0; i
< sizeof (zlfid
->zf_setid
); i
++)
4397 zlfid
->zf_setid
[i
] = (uint8_t)(objsetid
>> (8 * i
));
4399 /* XXX - this should be the generation number for the objset */
4400 for (i
= 0; i
< sizeof (zlfid
->zf_setgen
); i
++)
4401 zlfid
->zf_setgen
[i
] = 0;
4407 EXPORT_SYMBOL(zfs_fid
);
4411 zfs_getsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4413 znode_t
*zp
= ITOZ(ip
);
4414 zfs_sb_t
*zsb
= ITOZSB(ip
);
4416 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4420 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
4425 EXPORT_SYMBOL(zfs_getsecattr
);
4429 zfs_setsecattr(struct inode
*ip
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
4431 znode_t
*zp
= ITOZ(ip
);
4432 zfs_sb_t
*zsb
= ITOZSB(ip
);
4434 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
4435 zilog_t
*zilog
= zsb
->z_log
;
4440 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
4442 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
4443 zil_commit(zilog
, 0);
4448 EXPORT_SYMBOL(zfs_setsecattr
);
4450 #ifdef HAVE_UIO_ZEROCOPY
4452 * Tunable, both must be a power of 2.
4454 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4455 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
4456 * an arcbuf for a partial block read
4458 int zcr_blksz_min
= (1 << 10); /* 1K */
4459 int zcr_blksz_max
= (1 << 17); /* 128K */
4463 zfs_reqzcbuf(struct inode
*ip
, enum uio_rw ioflag
, xuio_t
*xuio
, cred_t
*cr
)
4465 znode_t
*zp
= ITOZ(ip
);
4466 zfs_sb_t
*zsb
= ITOZSB(ip
);
4467 int max_blksz
= zsb
->z_max_blksz
;
4468 uio_t
*uio
= &xuio
->xu_uio
;
4469 ssize_t size
= uio
->uio_resid
;
4470 offset_t offset
= uio
->uio_loffset
;
4475 int preamble
, postamble
;
4477 if (xuio
->xu_type
!= UIOTYPE_ZEROCOPY
)
4478 return (SET_ERROR(EINVAL
));
4485 * Loan out an arc_buf for write if write size is bigger than
4486 * max_blksz, and the file's block size is also max_blksz.
4489 if (size
< blksz
|| zp
->z_blksz
!= blksz
) {
4491 return (SET_ERROR(EINVAL
));
4494 * Caller requests buffers for write before knowing where the
4495 * write offset might be (e.g. NFS TCP write).
4500 preamble
= P2PHASE(offset
, blksz
);
4502 preamble
= blksz
- preamble
;
4507 postamble
= P2PHASE(size
, blksz
);
4510 fullblk
= size
/ blksz
;
4511 (void) dmu_xuio_init(xuio
,
4512 (preamble
!= 0) + fullblk
+ (postamble
!= 0));
4515 * Have to fix iov base/len for partial buffers. They
4516 * currently represent full arc_buf's.
4519 /* data begins in the middle of the arc_buf */
4520 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4523 (void) dmu_xuio_add(xuio
, abuf
,
4524 blksz
- preamble
, preamble
);
4527 for (i
= 0; i
< fullblk
; i
++) {
4528 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4531 (void) dmu_xuio_add(xuio
, abuf
, 0, blksz
);
4535 /* data ends in the middle of the arc_buf */
4536 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
4539 (void) dmu_xuio_add(xuio
, abuf
, 0, postamble
);
4544 * Loan out an arc_buf for read if the read size is larger than
4545 * the current file block size. Block alignment is not
4546 * considered. Partial arc_buf will be loaned out for read.
4548 blksz
= zp
->z_blksz
;
4549 if (blksz
< zcr_blksz_min
)
4550 blksz
= zcr_blksz_min
;
4551 if (blksz
> zcr_blksz_max
)
4552 blksz
= zcr_blksz_max
;
4553 /* avoid potential complexity of dealing with it */
4554 if (blksz
> max_blksz
) {
4556 return (SET_ERROR(EINVAL
));
4559 maxsize
= zp
->z_size
- uio
->uio_loffset
;
4565 return (SET_ERROR(EINVAL
));
4570 return (SET_ERROR(EINVAL
));
4573 uio
->uio_extflg
= UIO_XUIO
;
4574 XUIO_XUZC_RW(xuio
) = ioflag
;
4581 zfs_retzcbuf(struct inode
*ip
, xuio_t
*xuio
, cred_t
*cr
)
4585 int ioflag
= XUIO_XUZC_RW(xuio
);
4587 ASSERT(xuio
->xu_type
== UIOTYPE_ZEROCOPY
);
4589 i
= dmu_xuio_cnt(xuio
);
4591 abuf
= dmu_xuio_arcbuf(xuio
, i
);
4593 * if abuf == NULL, it must be a write buffer
4594 * that has been returned in zfs_write().
4597 dmu_return_arcbuf(abuf
);
4598 ASSERT(abuf
|| ioflag
== UIO_WRITE
);
4601 dmu_xuio_fini(xuio
);
4604 #endif /* HAVE_UIO_ZEROCOPY */
4606 #if defined(_KERNEL) && defined(HAVE_SPL)
4607 module_param(zfs_read_chunk_size
, long, 0644);
4608 MODULE_PARM_DESC(zfs_read_chunk_size
, "Bytes to read per chunk");