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 https://opensource.org/licenses/CDDL-1.0.
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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc.
29 /* Portions Copyright 2007 Jeremy Teo */
30 /* Portions Copyright 2010 Robert Milkowski */
32 #include <sys/types.h>
33 #include <sys/param.h>
35 #include <sys/sysmacros.h>
37 #include <sys/uio_impl.h>
41 #include <sys/cmn_err.h>
42 #include <sys/errno.h>
43 #include <sys/zfs_dir.h>
44 #include <sys/zfs_acl.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/fs/zfs.h>
48 #include <sys/dmu_objset.h>
52 #include <sys/policy.h>
53 #include <sys/zfs_vnops.h>
54 #include <sys/zfs_quota.h>
55 #include <sys/zfs_vfsops.h>
56 #include <sys/zfs_znode.h>
59 static ulong_t zfs_fsync_sync_cnt
= 4;
62 zfs_fsync(znode_t
*zp
, int syncflag
, cred_t
*cr
)
65 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
67 (void) tsd_set(zfs_fsyncer_key
, (void *)zfs_fsync_sync_cnt
);
69 if (zfsvfs
->z_os
->os_sync
!= ZFS_SYNC_DISABLED
) {
70 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
72 atomic_inc_32(&zp
->z_sync_writes_cnt
);
73 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
74 atomic_dec_32(&zp
->z_sync_writes_cnt
);
75 zfs_exit(zfsvfs
, FTAG
);
78 tsd_set(zfs_fsyncer_key
, NULL
);
84 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
86 * Lseek support for finding holes (cmd == SEEK_HOLE) and
87 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
90 zfs_holey_common(znode_t
*zp
, ulong_t cmd
, loff_t
*off
)
92 zfs_locked_range_t
*lr
;
93 uint64_t noff
= (uint64_t)*off
; /* new offset */
99 if (noff
>= file_sz
) {
100 return (SET_ERROR(ENXIO
));
103 if (cmd
== F_SEEK_HOLE
)
108 /* Flush any mmap()'d data to disk */
109 if (zn_has_cached_data(zp
))
110 zn_flush_cached_data(zp
, B_FALSE
);
112 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, 0, file_sz
, RL_READER
);
113 error
= dmu_offset_next(ZTOZSB(zp
)->z_os
, zp
->z_id
, hole
, &noff
);
114 zfs_rangelock_exit(lr
);
117 return (SET_ERROR(ENXIO
));
119 /* File was dirty, so fall back to using generic logic */
120 if (error
== EBUSY
) {
128 * We could find a hole that begins after the logical end-of-file,
129 * because dmu_offset_next() only works on whole blocks. If the
130 * EOF falls mid-block, then indicate that the "virtual hole"
131 * at the end of the file begins at the logical EOF, rather than
132 * at the end of the last block.
134 if (noff
> file_sz
) {
146 zfs_holey(znode_t
*zp
, ulong_t cmd
, loff_t
*off
)
148 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
151 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
154 error
= zfs_holey_common(zp
, cmd
, off
);
156 zfs_exit(zfsvfs
, FTAG
);
159 #endif /* SEEK_HOLE && SEEK_DATA */
162 zfs_access(znode_t
*zp
, int mode
, int flag
, cred_t
*cr
)
164 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
167 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
170 if (flag
& V_ACE_MASK
)
171 error
= zfs_zaccess(zp
, mode
, flag
, B_FALSE
, cr
, NULL
);
173 error
= zfs_zaccess_rwx(zp
, mode
, flag
, cr
, NULL
);
175 zfs_exit(zfsvfs
, FTAG
);
179 static uint64_t zfs_vnops_read_chunk_size
= 1024 * 1024; /* Tunable */
182 * Read bytes from specified file into supplied buffer.
184 * IN: zp - inode of file to be read from.
185 * uio - structure supplying read location, range info,
187 * ioflag - O_SYNC flags; used to provide FRSYNC semantics.
188 * O_DIRECT flag; used to bypass page cache.
189 * cr - credentials of caller.
191 * OUT: uio - updated offset and range, buffer filled.
193 * RETURN: 0 on success, error code on failure.
196 * inode - atime updated if byte count > 0
199 zfs_read(struct znode
*zp
, zfs_uio_t
*uio
, int ioflag
, cred_t
*cr
)
203 boolean_t frsync
= B_FALSE
;
205 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
206 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
209 if (zp
->z_pflags
& ZFS_AV_QUARANTINED
) {
210 zfs_exit(zfsvfs
, FTAG
);
211 return (SET_ERROR(EACCES
));
214 /* We don't copy out anything useful for directories. */
215 if (Z_ISDIR(ZTOTYPE(zp
))) {
216 zfs_exit(zfsvfs
, FTAG
);
217 return (SET_ERROR(EISDIR
));
221 * Validate file offset
223 if (zfs_uio_offset(uio
) < (offset_t
)0) {
224 zfs_exit(zfsvfs
, FTAG
);
225 return (SET_ERROR(EINVAL
));
229 * Fasttrack empty reads
231 if (zfs_uio_resid(uio
) == 0) {
232 zfs_exit(zfsvfs
, FTAG
);
238 * If we're in FRSYNC mode, sync out this znode before reading it.
239 * Only do this for non-snapshots.
241 * Some platforms do not support FRSYNC and instead map it
242 * to O_SYNC, which results in unnecessary calls to zil_commit. We
243 * only honor FRSYNC requests on platforms which support it.
245 frsync
= !!(ioflag
& FRSYNC
);
248 (frsync
|| zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
))
249 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
252 * Lock the range against changes.
254 zfs_locked_range_t
*lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
255 zfs_uio_offset(uio
), zfs_uio_resid(uio
), RL_READER
);
258 * If we are reading past end-of-file we can skip
259 * to the end; but we might still need to set atime.
261 if (zfs_uio_offset(uio
) >= zp
->z_size
) {
266 ASSERT(zfs_uio_offset(uio
) < zp
->z_size
);
267 #if defined(__linux__)
268 ssize_t start_offset
= zfs_uio_offset(uio
);
270 ssize_t n
= MIN(zfs_uio_resid(uio
), zp
->z_size
- zfs_uio_offset(uio
));
271 ssize_t start_resid
= n
;
274 ssize_t nbytes
= MIN(n
, zfs_vnops_read_chunk_size
-
275 P2PHASE(zfs_uio_offset(uio
), zfs_vnops_read_chunk_size
));
277 if (zfs_uio_segflg(uio
) == UIO_NOCOPY
)
278 error
= mappedread_sf(zp
, nbytes
, uio
);
281 if (zn_has_cached_data(zp
) && !(ioflag
& O_DIRECT
)) {
282 error
= mappedread(zp
, nbytes
, uio
);
284 error
= dmu_read_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
289 /* convert checksum errors into IO errors */
291 error
= SET_ERROR(EIO
);
293 #if defined(__linux__)
295 * if we actually read some bytes, bubbling EFAULT
296 * up to become EAGAIN isn't what we want here...
298 * ...on Linux, at least. On FBSD, doing this breaks.
300 if (error
== EFAULT
&&
301 (zfs_uio_offset(uio
) - start_offset
) != 0)
310 int64_t nread
= start_resid
- n
;
311 dataset_kstats_update_read_kstats(&zfsvfs
->z_kstat
, nread
);
312 task_io_account_read(nread
);
314 zfs_rangelock_exit(lr
);
316 ZFS_ACCESSTIME_STAMP(zfsvfs
, zp
);
317 zfs_exit(zfsvfs
, FTAG
);
322 zfs_clear_setid_bits_if_necessary(zfsvfs_t
*zfsvfs
, znode_t
*zp
, cred_t
*cr
,
323 uint64_t *clear_setid_bits_txgp
, dmu_tx_t
*tx
)
325 zilog_t
*zilog
= zfsvfs
->z_log
;
326 const uint64_t uid
= KUID_TO_SUID(ZTOUID(zp
));
328 ASSERT(clear_setid_bits_txgp
!= NULL
);
332 * Clear Set-UID/Set-GID bits on successful write if not
333 * privileged and at least one of the execute bits is set.
335 * It would be nice to do this after all writes have
336 * been done, but that would still expose the ISUID/ISGID
337 * to another app after the partial write is committed.
339 * Note: we don't call zfs_fuid_map_id() here because
340 * user 0 is not an ephemeral uid.
342 mutex_enter(&zp
->z_acl_lock
);
343 if ((zp
->z_mode
& (S_IXUSR
| (S_IXUSR
>> 3) | (S_IXUSR
>> 6))) != 0 &&
344 (zp
->z_mode
& (S_ISUID
| S_ISGID
)) != 0 &&
345 secpolicy_vnode_setid_retain(zp
, cr
,
346 ((zp
->z_mode
& S_ISUID
) != 0 && uid
== 0)) != 0) {
349 zp
->z_mode
&= ~(S_ISUID
| S_ISGID
);
350 newmode
= zp
->z_mode
;
351 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_MODE(zfsvfs
),
352 (void *)&newmode
, sizeof (uint64_t), tx
);
354 mutex_exit(&zp
->z_acl_lock
);
357 * Make sure SUID/SGID bits will be removed when we replay the
358 * log. If the setid bits are keep coming back, don't log more
359 * than one TX_SETATTR per transaction group.
361 if (*clear_setid_bits_txgp
!= dmu_tx_get_txg(tx
)) {
364 va
.va_mask
= ATTR_MODE
;
365 va
.va_nodeid
= zp
->z_id
;
366 va
.va_mode
= newmode
;
367 zfs_log_setattr(zilog
, tx
, TX_SETATTR
, zp
, &va
,
369 *clear_setid_bits_txgp
= dmu_tx_get_txg(tx
);
372 mutex_exit(&zp
->z_acl_lock
);
377 * Write the bytes to a file.
379 * IN: zp - znode of file to be written to.
380 * uio - structure supplying write location, range info,
382 * ioflag - O_APPEND flag set if in append mode.
383 * O_DIRECT flag; used to bypass page cache.
384 * cr - credentials of caller.
386 * OUT: uio - updated offset and range.
388 * RETURN: 0 if success
389 * error code if failure
392 * ip - ctime|mtime updated if byte count > 0
395 zfs_write(znode_t
*zp
, zfs_uio_t
*uio
, int ioflag
, cred_t
*cr
)
397 int error
= 0, error1
;
398 ssize_t start_resid
= zfs_uio_resid(uio
);
399 uint64_t clear_setid_bits_txg
= 0;
402 * Fasttrack empty write
404 ssize_t n
= start_resid
;
408 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
409 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
412 sa_bulk_attr_t bulk
[4];
414 uint64_t mtime
[2], ctime
[2];
415 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
416 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
417 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
419 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
423 * Callers might not be able to detect properly that we are read-only,
424 * so check it explicitly here.
426 if (zfs_is_readonly(zfsvfs
)) {
427 zfs_exit(zfsvfs
, FTAG
);
428 return (SET_ERROR(EROFS
));
432 * If immutable or not appending then return EPERM.
433 * Intentionally allow ZFS_READONLY through here.
434 * See zfs_zaccess_common()
436 if ((zp
->z_pflags
& ZFS_IMMUTABLE
) ||
437 ((zp
->z_pflags
& ZFS_APPENDONLY
) && !(ioflag
& O_APPEND
) &&
438 (zfs_uio_offset(uio
) < zp
->z_size
))) {
439 zfs_exit(zfsvfs
, FTAG
);
440 return (SET_ERROR(EPERM
));
444 * Validate file offset
446 offset_t woff
= ioflag
& O_APPEND
? zp
->z_size
: zfs_uio_offset(uio
);
448 zfs_exit(zfsvfs
, FTAG
);
449 return (SET_ERROR(EINVAL
));
452 const uint64_t max_blksz
= zfsvfs
->z_max_blksz
;
455 * Pre-fault the pages to ensure slow (eg NFS) pages
457 * Skip this if uio contains loaned arc_buf.
459 if (zfs_uio_prefaultpages(MIN(n
, max_blksz
), uio
)) {
460 zfs_exit(zfsvfs
, FTAG
);
461 return (SET_ERROR(EFAULT
));
465 * If in append mode, set the io offset pointer to eof.
467 zfs_locked_range_t
*lr
;
468 if (ioflag
& O_APPEND
) {
470 * Obtain an appending range lock to guarantee file append
471 * semantics. We reset the write offset once we have the lock.
473 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, 0, n
, RL_APPEND
);
474 woff
= lr
->lr_offset
;
475 if (lr
->lr_length
== UINT64_MAX
) {
477 * We overlocked the file because this write will cause
478 * the file block size to increase.
479 * Note that zp_size cannot change with this lock held.
483 zfs_uio_setoffset(uio
, woff
);
486 * Note that if the file block size will change as a result of
487 * this write, then this range lock will lock the entire file
488 * so that we can re-write the block safely.
490 lr
= zfs_rangelock_enter(&zp
->z_rangelock
, woff
, n
, RL_WRITER
);
493 if (zn_rlimit_fsize(zp
, uio
)) {
494 zfs_rangelock_exit(lr
);
495 zfs_exit(zfsvfs
, FTAG
);
496 return (SET_ERROR(EFBIG
));
499 const rlim64_t limit
= MAXOFFSET_T
;
502 zfs_rangelock_exit(lr
);
503 zfs_exit(zfsvfs
, FTAG
);
504 return (SET_ERROR(EFBIG
));
507 if (n
> limit
- woff
)
510 uint64_t end_size
= MAX(zp
->z_size
, woff
+ n
);
511 zilog_t
*zilog
= zfsvfs
->z_log
;
513 const uint64_t uid
= KUID_TO_SUID(ZTOUID(zp
));
514 const uint64_t gid
= KGID_TO_SGID(ZTOGID(zp
));
515 const uint64_t projid
= zp
->z_projid
;
518 * Write the file in reasonable size chunks. Each chunk is written
519 * in a separate transaction; this keeps the intent log records small
520 * and allows us to do more fine-grained space accounting.
523 woff
= zfs_uio_offset(uio
);
525 if (zfs_id_overblockquota(zfsvfs
, DMU_USERUSED_OBJECT
, uid
) ||
526 zfs_id_overblockquota(zfsvfs
, DMU_GROUPUSED_OBJECT
, gid
) ||
527 (projid
!= ZFS_DEFAULT_PROJID
&&
528 zfs_id_overblockquota(zfsvfs
, DMU_PROJECTUSED_OBJECT
,
530 error
= SET_ERROR(EDQUOT
);
534 arc_buf_t
*abuf
= NULL
;
535 if (n
>= max_blksz
&& woff
>= zp
->z_size
&&
536 P2PHASE(woff
, max_blksz
) == 0 &&
537 zp
->z_blksz
== max_blksz
) {
539 * This write covers a full block. "Borrow" a buffer
540 * from the dmu so that we can fill it before we enter
541 * a transaction. This avoids the possibility of
542 * holding up the transaction if the data copy hangs
543 * up on a pagefault (e.g., from an NFS server mapping).
547 abuf
= dmu_request_arcbuf(sa_get_db(zp
->z_sa_hdl
),
549 ASSERT(abuf
!= NULL
);
550 ASSERT(arc_buf_size(abuf
) == max_blksz
);
551 if ((error
= zfs_uiocopy(abuf
->b_data
, max_blksz
,
552 UIO_WRITE
, uio
, &cbytes
))) {
553 dmu_return_arcbuf(abuf
);
556 ASSERT3S(cbytes
, ==, max_blksz
);
560 * Start a transaction.
562 dmu_tx_t
*tx
= dmu_tx_create(zfsvfs
->z_os
);
563 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_FALSE
);
564 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)sa_get_db(zp
->z_sa_hdl
);
566 dmu_tx_hold_write_by_dnode(tx
, DB_DNODE(db
), woff
,
569 zfs_sa_upgrade_txholds(tx
, zp
);
570 error
= dmu_tx_assign(tx
, TXG_WAIT
);
574 dmu_return_arcbuf(abuf
);
579 * NB: We must call zfs_clear_setid_bits_if_necessary before
580 * committing the transaction!
584 * If rangelock_enter() over-locked we grow the blocksize
585 * and then reduce the lock range. This will only happen
586 * on the first iteration since rangelock_reduce() will
587 * shrink down lr_length to the appropriate size.
589 if (lr
->lr_length
== UINT64_MAX
) {
592 if (zp
->z_blksz
> max_blksz
) {
594 * File's blocksize is already larger than the
595 * "recordsize" property. Only let it grow to
596 * the next power of 2.
598 ASSERT(!ISP2(zp
->z_blksz
));
599 new_blksz
= MIN(end_size
,
600 1 << highbit64(zp
->z_blksz
));
602 new_blksz
= MIN(end_size
, max_blksz
);
604 zfs_grow_blocksize(zp
, new_blksz
, tx
);
605 zfs_rangelock_reduce(lr
, woff
, n
);
609 * XXX - should we really limit each write to z_max_blksz?
610 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
612 const ssize_t nbytes
=
613 MIN(n
, max_blksz
- P2PHASE(woff
, max_blksz
));
617 tx_bytes
= zfs_uio_resid(uio
);
618 zfs_uio_fault_disable(uio
, B_TRUE
);
619 error
= dmu_write_uio_dbuf(sa_get_db(zp
->z_sa_hdl
),
621 zfs_uio_fault_disable(uio
, B_FALSE
);
623 if (error
== EFAULT
) {
624 zfs_clear_setid_bits_if_necessary(zfsvfs
, zp
,
625 cr
, &clear_setid_bits_txg
, tx
);
628 * Account for partial writes before
629 * continuing the loop.
630 * Update needs to occur before the next
631 * zfs_uio_prefaultpages, or prefaultpages may
632 * error, and we may break the loop early.
634 if (tx_bytes
!= zfs_uio_resid(uio
))
635 n
-= tx_bytes
- zfs_uio_resid(uio
);
636 if (zfs_uio_prefaultpages(MIN(n
, max_blksz
),
644 * On FreeBSD, EFAULT should be propagated back to the
645 * VFS, which will handle faulting and will retry.
647 if (error
!= 0 && error
!= EFAULT
) {
648 zfs_clear_setid_bits_if_necessary(zfsvfs
, zp
,
649 cr
, &clear_setid_bits_txg
, tx
);
653 tx_bytes
-= zfs_uio_resid(uio
);
655 /* Implied by abuf != NULL: */
656 ASSERT3S(n
, >=, max_blksz
);
657 ASSERT0(P2PHASE(woff
, max_blksz
));
659 * We can simplify nbytes to MIN(n, max_blksz) since
660 * P2PHASE(woff, max_blksz) is 0, and knowing
661 * n >= max_blksz lets us simplify further:
663 ASSERT3S(nbytes
, ==, max_blksz
);
665 * Thus, we're writing a full block at a block-aligned
666 * offset and extending the file past EOF.
668 * dmu_assign_arcbuf_by_dbuf() will directly assign the
669 * arc buffer to a dbuf.
671 error
= dmu_assign_arcbuf_by_dbuf(
672 sa_get_db(zp
->z_sa_hdl
), woff
, abuf
, tx
);
675 * XXX This might not be necessary if
676 * dmu_assign_arcbuf_by_dbuf is guaranteed
679 zfs_clear_setid_bits_if_necessary(zfsvfs
, zp
,
680 cr
, &clear_setid_bits_txg
, tx
);
681 dmu_return_arcbuf(abuf
);
685 ASSERT3S(nbytes
, <=, zfs_uio_resid(uio
));
686 zfs_uioskip(uio
, nbytes
);
689 if (tx_bytes
&& zn_has_cached_data(zp
) &&
690 !(ioflag
& O_DIRECT
)) {
691 update_pages(zp
, woff
, tx_bytes
, zfsvfs
->z_os
);
695 * If we made no progress, we're done. If we made even
696 * partial progress, update the znode and ZIL accordingly.
699 (void) sa_update(zp
->z_sa_hdl
, SA_ZPL_SIZE(zfsvfs
),
700 (void *)&zp
->z_size
, sizeof (uint64_t), tx
);
706 zfs_clear_setid_bits_if_necessary(zfsvfs
, zp
, cr
,
707 &clear_setid_bits_txg
, tx
);
709 zfs_tstamp_update_setup(zp
, CONTENT_MODIFIED
, mtime
, ctime
);
712 * Update the file size (zp_size) if it has changed;
713 * account for possible concurrent updates.
715 while ((end_size
= zp
->z_size
) < zfs_uio_offset(uio
)) {
716 (void) atomic_cas_64(&zp
->z_size
, end_size
,
717 zfs_uio_offset(uio
));
718 ASSERT(error
== 0 || error
== EFAULT
);
721 * If we are replaying and eof is non zero then force
722 * the file size to the specified eof. Note, there's no
723 * concurrency during replay.
725 if (zfsvfs
->z_replay
&& zfsvfs
->z_replay_eof
!= 0)
726 zp
->z_size
= zfsvfs
->z_replay_eof
;
728 error1
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
730 /* Avoid clobbering EFAULT. */
734 * NB: During replay, the TX_SETATTR record logged by
735 * zfs_clear_setid_bits_if_necessary must precede any of
736 * the TX_WRITE records logged here.
738 zfs_log_write(zilog
, tx
, TX_WRITE
, zp
, woff
, tx_bytes
, ioflag
,
745 ASSERT3S(tx_bytes
, ==, nbytes
);
749 if (zfs_uio_prefaultpages(MIN(n
, max_blksz
), uio
)) {
750 error
= SET_ERROR(EFAULT
);
756 zfs_znode_update_vfs(zp
);
757 zfs_rangelock_exit(lr
);
760 * If we're in replay mode, or we made no progress, or the
761 * uio data is inaccessible return an error. Otherwise, it's
762 * at least a partial write, so it's successful.
764 if (zfsvfs
->z_replay
|| zfs_uio_resid(uio
) == start_resid
||
766 zfs_exit(zfsvfs
, FTAG
);
770 if (ioflag
& (O_SYNC
| O_DSYNC
) ||
771 zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
772 zil_commit(zilog
, zp
->z_id
);
774 const int64_t nwritten
= start_resid
- zfs_uio_resid(uio
);
775 dataset_kstats_update_write_kstats(&zfsvfs
->z_kstat
, nwritten
);
776 task_io_account_write(nwritten
);
778 zfs_exit(zfsvfs
, FTAG
);
783 zfs_getsecattr(znode_t
*zp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
785 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
787 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
789 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
791 error
= zfs_getacl(zp
, vsecp
, skipaclchk
, cr
);
792 zfs_exit(zfsvfs
, FTAG
);
798 zfs_setsecattr(znode_t
*zp
, vsecattr_t
*vsecp
, int flag
, cred_t
*cr
)
800 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
802 boolean_t skipaclchk
= (flag
& ATTR_NOACLCHECK
) ? B_TRUE
: B_FALSE
;
803 zilog_t
*zilog
= zfsvfs
->z_log
;
805 if ((error
= zfs_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
808 error
= zfs_setacl(zp
, vsecp
, skipaclchk
, cr
);
810 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
811 zil_commit(zilog
, 0);
813 zfs_exit(zfsvfs
, FTAG
);
818 static int zil_fault_io
= 0;
821 static void zfs_get_done(zgd_t
*zgd
, int error
);
824 * Get data to generate a TX_WRITE intent log record.
827 zfs_get_data(void *arg
, uint64_t gen
, lr_write_t
*lr
, char *buf
,
828 struct lwb
*lwb
, zio_t
*zio
)
830 zfsvfs_t
*zfsvfs
= arg
;
831 objset_t
*os
= zfsvfs
->z_os
;
833 uint64_t object
= lr
->lr_foid
;
834 uint64_t offset
= lr
->lr_offset
;
835 uint64_t size
= lr
->lr_length
;
841 ASSERT3P(lwb
, !=, NULL
);
842 ASSERT3P(zio
, !=, NULL
);
843 ASSERT3U(size
, !=, 0);
846 * Nothing to do if the file has been removed
848 if (zfs_zget(zfsvfs
, object
, &zp
) != 0)
849 return (SET_ERROR(ENOENT
));
850 if (zp
->z_unlinked
) {
852 * Release the vnode asynchronously as we currently have the
853 * txg stopped from syncing.
856 return (SET_ERROR(ENOENT
));
858 /* check if generation number matches */
859 if (sa_lookup(zp
->z_sa_hdl
, SA_ZPL_GEN(zfsvfs
), &zp_gen
,
860 sizeof (zp_gen
)) != 0) {
862 return (SET_ERROR(EIO
));
866 return (SET_ERROR(ENOENT
));
869 zgd
= (zgd_t
*)kmem_zalloc(sizeof (zgd_t
), KM_SLEEP
);
871 zgd
->zgd_private
= zp
;
874 * Write records come in two flavors: immediate and indirect.
875 * For small writes it's cheaper to store the data with the
876 * log record (immediate); for large writes it's cheaper to
877 * sync the data and get a pointer to it (indirect) so that
878 * we don't have to write the data twice.
880 if (buf
!= NULL
) { /* immediate write */
881 zgd
->zgd_lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
882 offset
, size
, RL_READER
);
883 /* test for truncation needs to be done while range locked */
884 if (offset
>= zp
->z_size
) {
885 error
= SET_ERROR(ENOENT
);
887 error
= dmu_read(os
, object
, offset
, size
, buf
,
888 DMU_READ_NO_PREFETCH
);
890 ASSERT(error
== 0 || error
== ENOENT
);
891 } else { /* indirect write */
893 * Have to lock the whole block to ensure when it's
894 * written out and its checksum is being calculated
895 * that no one can change the data. We need to re-check
896 * blocksize after we get the lock in case it's changed!
901 blkoff
= ISP2(size
) ? P2PHASE(offset
, size
) : offset
;
903 zgd
->zgd_lr
= zfs_rangelock_enter(&zp
->z_rangelock
,
904 offset
, size
, RL_READER
);
905 if (zp
->z_blksz
== size
)
908 zfs_rangelock_exit(zgd
->zgd_lr
);
910 /* test for truncation needs to be done while range locked */
911 if (lr
->lr_offset
>= zp
->z_size
)
912 error
= SET_ERROR(ENOENT
);
915 error
= SET_ERROR(EIO
);
920 error
= dmu_buf_hold(os
, object
, offset
, zgd
, &db
,
921 DMU_READ_NO_PREFETCH
);
924 blkptr_t
*bp
= &lr
->lr_blkptr
;
929 ASSERT(db
->db_offset
== offset
);
930 ASSERT(db
->db_size
== size
);
932 error
= dmu_sync(zio
, lr
->lr_common
.lrc_txg
,
934 ASSERT(error
|| lr
->lr_length
<= size
);
937 * On success, we need to wait for the write I/O
938 * initiated by dmu_sync() to complete before we can
939 * release this dbuf. We will finish everything up
940 * in the zfs_get_done() callback.
945 if (error
== EALREADY
) {
946 lr
->lr_common
.lrc_txtype
= TX_WRITE2
;
948 * TX_WRITE2 relies on the data previously
949 * written by the TX_WRITE that caused
950 * EALREADY. We zero out the BP because
951 * it is the old, currently-on-disk BP.
960 zfs_get_done(zgd
, error
);
967 zfs_get_done(zgd_t
*zgd
, int error
)
970 znode_t
*zp
= zgd
->zgd_private
;
973 dmu_buf_rele(zgd
->zgd_db
, zgd
);
975 zfs_rangelock_exit(zgd
->zgd_lr
);
978 * Release the vnode asynchronously as we currently have the
979 * txg stopped from syncing.
983 kmem_free(zgd
, sizeof (zgd_t
));
986 EXPORT_SYMBOL(zfs_access
);
987 EXPORT_SYMBOL(zfs_fsync
);
988 EXPORT_SYMBOL(zfs_holey
);
989 EXPORT_SYMBOL(zfs_read
);
990 EXPORT_SYMBOL(zfs_write
);
991 EXPORT_SYMBOL(zfs_getsecattr
);
992 EXPORT_SYMBOL(zfs_setsecattr
);
994 ZFS_MODULE_PARAM(zfs_vnops
, zfs_vnops_
, read_chunk_size
, U64
, ZMOD_RW
,
995 "Bytes to read per chunk");