1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2016-2018 Christoph Hellwig.
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_btree.h"
16 #include "xfs_bmap_btree.h"
18 #include "xfs_bmap_util.h"
19 #include "xfs_errortag.h"
20 #include "xfs_error.h"
21 #include "xfs_trans.h"
22 #include "xfs_trans_space.h"
23 #include "xfs_inode_item.h"
24 #include "xfs_iomap.h"
25 #include "xfs_trace.h"
26 #include "xfs_quota.h"
27 #include "xfs_dquot_item.h"
28 #include "xfs_dquot.h"
29 #include "xfs_reflink.h"
32 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
36 xfs_alert_fsblock_zero(
38 xfs_bmbt_irec_t
*imap
)
40 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
41 "Access to block zero in inode %llu "
42 "start_block: %llx start_off: %llx "
43 "blkcnt: %llx extent-state: %x",
44 (unsigned long long)ip
->i_ino
,
45 (unsigned long long)imap
->br_startblock
,
46 (unsigned long long)imap
->br_startoff
,
47 (unsigned long long)imap
->br_blockcount
,
56 struct xfs_bmbt_irec
*imap
,
59 struct xfs_mount
*mp
= ip
->i_mount
;
61 if (unlikely(!imap
->br_startblock
&& !XFS_IS_REALTIME_INODE(ip
)))
62 return xfs_alert_fsblock_zero(ip
, imap
);
64 if (imap
->br_startblock
== HOLESTARTBLOCK
) {
65 iomap
->addr
= IOMAP_NULL_ADDR
;
66 iomap
->type
= IOMAP_HOLE
;
67 } else if (imap
->br_startblock
== DELAYSTARTBLOCK
||
68 isnullstartblock(imap
->br_startblock
)) {
69 iomap
->addr
= IOMAP_NULL_ADDR
;
70 iomap
->type
= IOMAP_DELALLOC
;
72 iomap
->addr
= BBTOB(xfs_fsb_to_db(ip
, imap
->br_startblock
));
73 if (imap
->br_state
== XFS_EXT_UNWRITTEN
)
74 iomap
->type
= IOMAP_UNWRITTEN
;
76 iomap
->type
= IOMAP_MAPPED
;
78 iomap
->offset
= XFS_FSB_TO_B(mp
, imap
->br_startoff
);
79 iomap
->length
= XFS_FSB_TO_B(mp
, imap
->br_blockcount
);
80 iomap
->bdev
= xfs_find_bdev_for_inode(VFS_I(ip
));
81 iomap
->dax_dev
= xfs_find_daxdev_for_inode(VFS_I(ip
));
83 if (xfs_ipincount(ip
) &&
84 (ip
->i_itemp
->ili_fsync_fields
& ~XFS_ILOG_TIMESTAMP
))
85 iomap
->flags
|= IOMAP_F_DIRTY
;
87 iomap
->flags
|= IOMAP_F_SHARED
;
95 xfs_fileoff_t offset_fsb
,
96 xfs_fileoff_t end_fsb
)
98 iomap
->addr
= IOMAP_NULL_ADDR
;
99 iomap
->type
= IOMAP_HOLE
;
100 iomap
->offset
= XFS_FSB_TO_B(ip
->i_mount
, offset_fsb
);
101 iomap
->length
= XFS_FSB_TO_B(ip
->i_mount
, end_fsb
- offset_fsb
);
102 iomap
->bdev
= xfs_find_bdev_for_inode(VFS_I(ip
));
103 iomap
->dax_dev
= xfs_find_daxdev_for_inode(VFS_I(ip
));
108 struct xfs_inode
*ip
,
109 xfs_extlen_t extsize
)
111 struct xfs_mount
*mp
= ip
->i_mount
;
112 xfs_extlen_t align
= 0;
114 if (!XFS_IS_REALTIME_INODE(ip
)) {
116 * Round up the allocation request to a stripe unit
117 * (m_dalign) boundary if the file size is >= stripe unit
118 * size, and we are allocating past the allocation eof.
120 * If mounted with the "-o swalloc" option the alignment is
121 * increased from the strip unit size to the stripe width.
123 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
124 align
= mp
->m_swidth
;
125 else if (mp
->m_dalign
)
126 align
= mp
->m_dalign
;
128 if (align
&& XFS_ISIZE(ip
) < XFS_FSB_TO_B(mp
, align
))
133 * Always round up the allocation request to an extent boundary
134 * (when file on a real-time subvolume or has di_extsize hint).
138 align
= roundup_64(align
, extsize
);
147 xfs_iomap_eof_align_last_fsb(
148 struct xfs_inode
*ip
,
149 xfs_extlen_t extsize
,
150 xfs_fileoff_t
*last_fsb
)
152 xfs_extlen_t align
= xfs_eof_alignment(ip
, extsize
);
155 xfs_fileoff_t new_last_fsb
= roundup_64(*last_fsb
, align
);
158 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
162 *last_fsb
= new_last_fsb
;
168 xfs_iomap_write_direct(
172 xfs_bmbt_irec_t
*imap
,
175 xfs_mount_t
*mp
= ip
->i_mount
;
176 xfs_fileoff_t offset_fsb
;
177 xfs_fileoff_t last_fsb
;
178 xfs_filblks_t count_fsb
, resaligned
;
184 uint qblocks
, resblks
, resrtextents
;
187 int bmapi_flags
= XFS_BMAPI_PREALLOC
;
190 rt
= XFS_IS_REALTIME_INODE(ip
);
191 extsz
= xfs_get_extsz_hint(ip
);
192 lockmode
= XFS_ILOCK_SHARED
; /* locked by caller */
194 ASSERT(xfs_isilocked(ip
, lockmode
));
196 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
197 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
198 if ((offset
+ count
) > XFS_ISIZE(ip
)) {
200 * Assert that the in-core extent list is present since this can
201 * call xfs_iread_extents() and we only have the ilock shared.
202 * This should be safe because the lock was held around a bmapi
203 * call in the caller and we only need it to access the in-core
206 ASSERT(XFS_IFORK_PTR(ip
, XFS_DATA_FORK
)->if_flags
&
208 error
= xfs_iomap_eof_align_last_fsb(ip
, extsz
, &last_fsb
);
212 if (nmaps
&& (imap
->br_startblock
== HOLESTARTBLOCK
))
213 last_fsb
= min(last_fsb
, (xfs_fileoff_t
)
214 imap
->br_blockcount
+
217 count_fsb
= last_fsb
- offset_fsb
;
218 ASSERT(count_fsb
> 0);
219 resaligned
= xfs_aligned_fsb_count(offset_fsb
, count_fsb
, extsz
);
222 resrtextents
= qblocks
= resaligned
;
223 resrtextents
/= mp
->m_sb
.sb_rextsize
;
224 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
225 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
228 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resaligned
);
229 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
233 * Drop the shared lock acquired by the caller, attach the dquot if
234 * necessary and move on to transaction setup.
236 xfs_iunlock(ip
, lockmode
);
237 error
= xfs_qm_dqattach(ip
);
242 * For DAX, we do not allocate unwritten extents, but instead we zero
243 * the block before we commit the transaction. Ideally we'd like to do
244 * this outside the transaction context, but if we commit and then crash
245 * we may not have zeroed the blocks and this will be exposed on
246 * recovery of the allocation. Hence we must zero before commit.
248 * Further, if we are mapping unwritten extents here, we need to zero
249 * and convert them to written so that we don't need an unwritten extent
250 * callback for DAX. This also means that we need to be able to dip into
251 * the reserve block pool for bmbt block allocation if there is no space
252 * left but we need to do unwritten extent conversion.
254 if (IS_DAX(VFS_I(ip
))) {
255 bmapi_flags
= XFS_BMAPI_CONVERT
| XFS_BMAPI_ZERO
;
256 if (imap
->br_state
== XFS_EXT_UNWRITTEN
) {
257 tflags
|= XFS_TRANS_RESERVE
;
258 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0) << 1;
261 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, resrtextents
,
266 lockmode
= XFS_ILOCK_EXCL
;
267 xfs_ilock(ip
, lockmode
);
269 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, qblocks
, 0, quota_flag
);
271 goto out_trans_cancel
;
273 xfs_trans_ijoin(tp
, ip
, 0);
276 * From this point onwards we overwrite the imap pointer that the
280 error
= xfs_bmapi_write(tp
, ip
, offset_fsb
, count_fsb
,
281 bmapi_flags
, resblks
, imap
, &nimaps
);
286 * Complete the transaction
288 error
= xfs_trans_commit(tp
);
293 * Copy any maps to caller's array and return any error.
300 if (!(imap
->br_startblock
|| XFS_IS_REALTIME_INODE(ip
)))
301 error
= xfs_alert_fsblock_zero(ip
, imap
);
304 xfs_iunlock(ip
, lockmode
);
308 xfs_trans_unreserve_quota_nblks(tp
, ip
, (long)qblocks
, 0, quota_flag
);
310 xfs_trans_cancel(tp
);
315 xfs_quota_need_throttle(
316 struct xfs_inode
*ip
,
318 xfs_fsblock_t alloc_blocks
)
320 struct xfs_dquot
*dq
= xfs_inode_dquot(ip
, type
);
322 if (!dq
|| !xfs_this_quota_on(ip
->i_mount
, type
))
325 /* no hi watermark, no throttle */
326 if (!dq
->q_prealloc_hi_wmark
)
329 /* under the lo watermark, no throttle */
330 if (dq
->q_res_bcount
+ alloc_blocks
< dq
->q_prealloc_lo_wmark
)
337 xfs_quota_calc_throttle(
338 struct xfs_inode
*ip
,
340 xfs_fsblock_t
*qblocks
,
346 struct xfs_dquot
*dq
= xfs_inode_dquot(ip
, type
);
348 /* no dq, or over hi wmark, squash the prealloc completely */
349 if (!dq
|| dq
->q_res_bcount
>= dq
->q_prealloc_hi_wmark
) {
355 freesp
= dq
->q_prealloc_hi_wmark
- dq
->q_res_bcount
;
356 if (freesp
< dq
->q_low_space
[XFS_QLOWSP_5_PCNT
]) {
358 if (freesp
< dq
->q_low_space
[XFS_QLOWSP_3_PCNT
])
360 if (freesp
< dq
->q_low_space
[XFS_QLOWSP_1_PCNT
])
364 if (freesp
< *qfreesp
)
367 /* only overwrite the throttle values if we are more aggressive */
368 if ((freesp
>> shift
) < (*qblocks
>> *qshift
)) {
375 * If we are doing a write at the end of the file and there are no allocations
376 * past this one, then extend the allocation out to the file system's write
379 * If we don't have a user specified preallocation size, dynamically increase
380 * the preallocation size as the size of the file grows. Cap the maximum size
381 * at a single extent or less if the filesystem is near full. The closer the
382 * filesystem is to full, the smaller the maximum prealocation.
384 * As an exception we don't do any preallocation at all if the file is smaller
385 * than the minimum preallocation and we are using the default dynamic
386 * preallocation scheme, as it is likely this is the only write to the file that
387 * is going to be done.
389 * We clean up any extra space left over when the file is closed in
393 xfs_iomap_prealloc_size(
394 struct xfs_inode
*ip
,
398 struct xfs_iext_cursor
*icur
)
400 struct xfs_mount
*mp
= ip
->i_mount
;
401 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, whichfork
);
402 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
403 struct xfs_bmbt_irec prev
;
406 xfs_fsblock_t qblocks
;
408 xfs_fsblock_t alloc_blocks
= 0;
410 if (offset
+ count
<= XFS_ISIZE(ip
))
413 if (!(mp
->m_flags
& XFS_MOUNT_DFLT_IOSIZE
) &&
414 (XFS_ISIZE(ip
) < XFS_FSB_TO_B(mp
, mp
->m_writeio_blocks
)))
418 * If an explicit allocsize is set, the file is small, or we
419 * are writing behind a hole, then use the minimum prealloc:
421 if ((mp
->m_flags
& XFS_MOUNT_DFLT_IOSIZE
) ||
422 XFS_ISIZE(ip
) < XFS_FSB_TO_B(mp
, mp
->m_dalign
) ||
423 !xfs_iext_peek_prev_extent(ifp
, icur
, &prev
) ||
424 prev
.br_startoff
+ prev
.br_blockcount
< offset_fsb
)
425 return mp
->m_writeio_blocks
;
428 * Determine the initial size of the preallocation. We are beyond the
429 * current EOF here, but we need to take into account whether this is
430 * a sparse write or an extending write when determining the
431 * preallocation size. Hence we need to look up the extent that ends
432 * at the current write offset and use the result to determine the
433 * preallocation size.
435 * If the extent is a hole, then preallocation is essentially disabled.
436 * Otherwise we take the size of the preceding data extent as the basis
437 * for the preallocation size. If the size of the extent is greater than
438 * half the maximum extent length, then use the current offset as the
439 * basis. This ensures that for large files the preallocation size
440 * always extends to MAXEXTLEN rather than falling short due to things
441 * like stripe unit/width alignment of real extents.
443 if (prev
.br_blockcount
<= (MAXEXTLEN
>> 1))
444 alloc_blocks
= prev
.br_blockcount
<< 1;
446 alloc_blocks
= XFS_B_TO_FSB(mp
, offset
);
449 qblocks
= alloc_blocks
;
452 * MAXEXTLEN is not a power of two value but we round the prealloc down
453 * to the nearest power of two value after throttling. To prevent the
454 * round down from unconditionally reducing the maximum supported prealloc
455 * size, we round up first, apply appropriate throttling, round down and
456 * cap the value to MAXEXTLEN.
458 alloc_blocks
= XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN
),
461 freesp
= percpu_counter_read_positive(&mp
->m_fdblocks
);
462 if (freesp
< mp
->m_low_space
[XFS_LOWSP_5_PCNT
]) {
464 if (freesp
< mp
->m_low_space
[XFS_LOWSP_4_PCNT
])
466 if (freesp
< mp
->m_low_space
[XFS_LOWSP_3_PCNT
])
468 if (freesp
< mp
->m_low_space
[XFS_LOWSP_2_PCNT
])
470 if (freesp
< mp
->m_low_space
[XFS_LOWSP_1_PCNT
])
475 * Check each quota to cap the prealloc size, provide a shift value to
476 * throttle with and adjust amount of available space.
478 if (xfs_quota_need_throttle(ip
, XFS_DQ_USER
, alloc_blocks
))
479 xfs_quota_calc_throttle(ip
, XFS_DQ_USER
, &qblocks
, &qshift
,
481 if (xfs_quota_need_throttle(ip
, XFS_DQ_GROUP
, alloc_blocks
))
482 xfs_quota_calc_throttle(ip
, XFS_DQ_GROUP
, &qblocks
, &qshift
,
484 if (xfs_quota_need_throttle(ip
, XFS_DQ_PROJ
, alloc_blocks
))
485 xfs_quota_calc_throttle(ip
, XFS_DQ_PROJ
, &qblocks
, &qshift
,
489 * The final prealloc size is set to the minimum of free space available
490 * in each of the quotas and the overall filesystem.
492 * The shift throttle value is set to the maximum value as determined by
493 * the global low free space values and per-quota low free space values.
495 alloc_blocks
= min(alloc_blocks
, qblocks
);
496 shift
= max(shift
, qshift
);
499 alloc_blocks
>>= shift
;
501 * rounddown_pow_of_two() returns an undefined result if we pass in
505 alloc_blocks
= rounddown_pow_of_two(alloc_blocks
);
506 if (alloc_blocks
> MAXEXTLEN
)
507 alloc_blocks
= MAXEXTLEN
;
510 * If we are still trying to allocate more space than is
511 * available, squash the prealloc hard. This can happen if we
512 * have a large file on a small filesystem and the above
513 * lowspace thresholds are smaller than MAXEXTLEN.
515 while (alloc_blocks
&& alloc_blocks
>= freesp
)
518 if (alloc_blocks
< mp
->m_writeio_blocks
)
519 alloc_blocks
= mp
->m_writeio_blocks
;
520 trace_xfs_iomap_prealloc_size(ip
, alloc_blocks
, shift
,
521 mp
->m_writeio_blocks
);
526 xfs_file_iomap_begin_delay(
533 struct xfs_inode
*ip
= XFS_I(inode
);
534 struct xfs_mount
*mp
= ip
->i_mount
;
535 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
536 xfs_fileoff_t maxbytes_fsb
=
537 XFS_B_TO_FSB(mp
, mp
->m_super
->s_maxbytes
);
538 xfs_fileoff_t end_fsb
;
539 struct xfs_bmbt_irec imap
, cmap
;
540 struct xfs_iext_cursor icur
, ccur
;
541 xfs_fsblock_t prealloc_blocks
= 0;
542 bool eof
= false, cow_eof
= false, shared
= false;
543 int whichfork
= XFS_DATA_FORK
;
546 ASSERT(!XFS_IS_REALTIME_INODE(ip
));
547 ASSERT(!xfs_get_extsz_hint(ip
));
549 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
551 if (unlikely(XFS_TEST_ERROR(
552 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
553 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
554 mp
, XFS_ERRTAG_BMAPIFORMAT
))) {
555 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
556 error
= -EFSCORRUPTED
;
560 XFS_STATS_INC(mp
, xs_blk_mapw
);
562 if (!(ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) {
563 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
568 end_fsb
= min(XFS_B_TO_FSB(mp
, offset
+ count
), maxbytes_fsb
);
571 * Search the data fork fork first to look up our source mapping. We
572 * always need the data fork map, as we have to return it to the
573 * iomap code so that the higher level write code can read data in to
574 * perform read-modify-write cycles for unaligned writes.
576 eof
= !xfs_iext_lookup_extent(ip
, &ip
->i_df
, offset_fsb
, &icur
, &imap
);
578 imap
.br_startoff
= end_fsb
; /* fake hole until the end */
580 /* We never need to allocate blocks for zeroing a hole. */
581 if ((flags
& IOMAP_ZERO
) && imap
.br_startoff
> offset_fsb
) {
582 xfs_hole_to_iomap(ip
, iomap
, offset_fsb
, imap
.br_startoff
);
587 * Search the COW fork extent list even if we did not find a data fork
588 * extent. This serves two purposes: first this implements the
589 * speculative preallocation using cowextsize, so that we also unshare
590 * block adjacent to shared blocks instead of just the shared blocks
591 * themselves. Second the lookup in the extent list is generally faster
592 * than going out to the shared extent tree.
594 if (xfs_is_cow_inode(ip
)) {
596 ASSERT(!xfs_is_reflink_inode(ip
));
597 xfs_ifork_init_cow(ip
);
599 cow_eof
= !xfs_iext_lookup_extent(ip
, ip
->i_cowfp
, offset_fsb
,
601 if (!cow_eof
&& cmap
.br_startoff
<= offset_fsb
) {
602 trace_xfs_reflink_cow_found(ip
, &cmap
);
603 whichfork
= XFS_COW_FORK
;
608 if (imap
.br_startoff
<= offset_fsb
) {
610 * For reflink files we may need a delalloc reservation when
611 * overwriting shared extents. This includes zeroing of
612 * existing extents that contain data.
614 if (!xfs_is_cow_inode(ip
) ||
615 ((flags
& IOMAP_ZERO
) && imap
.br_state
!= XFS_EXT_NORM
)) {
616 trace_xfs_iomap_found(ip
, offset
, count
, XFS_DATA_FORK
,
621 xfs_trim_extent(&imap
, offset_fsb
, end_fsb
- offset_fsb
);
623 /* Trim the mapping to the nearest shared extent boundary. */
624 error
= xfs_inode_need_cow(ip
, &imap
, &shared
);
628 /* Not shared? Just report the (potentially capped) extent. */
630 trace_xfs_iomap_found(ip
, offset
, count
, XFS_DATA_FORK
,
636 * Fork all the shared blocks from our write offset until the
639 whichfork
= XFS_COW_FORK
;
640 end_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
643 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
644 * pages to keep the chunks of work done where somewhat
645 * symmetric with the work writeback does. This is a completely
646 * arbitrary number pulled out of thin air.
648 * Note that the values needs to be less than 32-bits wide until
649 * the lower level functions are updated.
651 count
= min_t(loff_t
, count
, 1024 * PAGE_SIZE
);
652 end_fsb
= min(XFS_B_TO_FSB(mp
, offset
+ count
), maxbytes_fsb
);
654 if (xfs_is_always_cow_inode(ip
))
655 whichfork
= XFS_COW_FORK
;
658 error
= xfs_qm_dqattach_locked(ip
, false);
663 prealloc_blocks
= xfs_iomap_prealloc_size(ip
, whichfork
, offset
,
665 if (prealloc_blocks
) {
667 xfs_off_t end_offset
;
668 xfs_fileoff_t p_end_fsb
;
670 end_offset
= XFS_WRITEIO_ALIGN(mp
, offset
+ count
- 1);
671 p_end_fsb
= XFS_B_TO_FSBT(mp
, end_offset
) +
674 align
= xfs_eof_alignment(ip
, 0);
676 p_end_fsb
= roundup_64(p_end_fsb
, align
);
678 p_end_fsb
= min(p_end_fsb
, maxbytes_fsb
);
679 ASSERT(p_end_fsb
> offset_fsb
);
680 prealloc_blocks
= p_end_fsb
- end_fsb
;
685 error
= xfs_bmapi_reserve_delalloc(ip
, whichfork
, offset_fsb
,
686 end_fsb
- offset_fsb
, prealloc_blocks
,
687 whichfork
== XFS_DATA_FORK
? &imap
: &cmap
,
688 whichfork
== XFS_DATA_FORK
? &icur
: &ccur
,
689 whichfork
== XFS_DATA_FORK
? eof
: cow_eof
);
695 /* retry without any preallocation */
696 trace_xfs_delalloc_enospc(ip
, offset
, count
);
697 if (prealloc_blocks
) {
707 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
708 * them out if the write happens to fail.
710 iomap
->flags
|= IOMAP_F_NEW
;
711 trace_xfs_iomap_alloc(ip
, offset
, count
, whichfork
,
712 whichfork
== XFS_DATA_FORK
? &imap
: &cmap
);
714 if (whichfork
== XFS_COW_FORK
) {
715 if (imap
.br_startoff
> offset_fsb
) {
716 xfs_trim_extent(&cmap
, offset_fsb
,
717 imap
.br_startoff
- offset_fsb
);
718 error
= xfs_bmbt_to_iomap(ip
, iomap
, &cmap
, true);
721 /* ensure we only report blocks we have a reservation for */
722 xfs_trim_extent(&imap
, cmap
.br_startoff
, cmap
.br_blockcount
);
725 error
= xfs_bmbt_to_iomap(ip
, iomap
, &imap
, shared
);
727 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
732 xfs_iomap_write_unwritten(
738 xfs_mount_t
*mp
= ip
->i_mount
;
739 xfs_fileoff_t offset_fsb
;
740 xfs_filblks_t count_fsb
;
741 xfs_filblks_t numblks_fsb
;
744 xfs_bmbt_irec_t imap
;
745 struct inode
*inode
= VFS_I(ip
);
750 trace_xfs_unwritten_convert(ip
, offset
, count
);
752 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
753 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
754 count_fsb
= (xfs_filblks_t
)(count_fsb
- offset_fsb
);
757 * Reserve enough blocks in this transaction for two complete extent
758 * btree splits. We may be converting the middle part of an unwritten
759 * extent and in this case we will insert two new extents in the btree
760 * each of which could cause a full split.
762 * This reservation amount will be used in the first call to
763 * xfs_bmbt_split() to select an AG with enough space to satisfy the
764 * rest of the operation.
766 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0) << 1;
770 * Set up a transaction to convert the range of extents
771 * from unwritten to real. Do allocations in a loop until
772 * we have covered the range passed in.
774 * Note that we can't risk to recursing back into the filesystem
775 * here as we might be asked to write out the same inode that we
776 * complete here and might deadlock on the iolock.
778 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, 0,
779 XFS_TRANS_RESERVE
, &tp
);
783 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
784 xfs_trans_ijoin(tp
, ip
, 0);
787 * Modify the unwritten extent state of the buffer.
790 error
= xfs_bmapi_write(tp
, ip
, offset_fsb
, count_fsb
,
791 XFS_BMAPI_CONVERT
, resblks
, &imap
,
794 goto error_on_bmapi_transaction
;
797 * Log the updated inode size as we go. We have to be careful
798 * to only log it up to the actual write offset if it is
799 * halfway into a block.
801 i_size
= XFS_FSB_TO_B(mp
, offset_fsb
+ count_fsb
);
802 if (i_size
> offset
+ count
)
803 i_size
= offset
+ count
;
804 if (update_isize
&& i_size
> i_size_read(inode
))
805 i_size_write(inode
, i_size
);
806 i_size
= xfs_new_eof(ip
, i_size
);
808 ip
->i_d
.di_size
= i_size
;
809 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
812 error
= xfs_trans_commit(tp
);
813 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
817 if (!(imap
.br_startblock
|| XFS_IS_REALTIME_INODE(ip
)))
818 return xfs_alert_fsblock_zero(ip
, &imap
);
820 if ((numblks_fsb
= imap
.br_blockcount
) == 0) {
822 * The numblks_fsb value should always get
823 * smaller, otherwise the loop is stuck.
825 ASSERT(imap
.br_blockcount
);
828 offset_fsb
+= numblks_fsb
;
829 count_fsb
-= numblks_fsb
;
830 } while (count_fsb
> 0);
834 error_on_bmapi_transaction
:
835 xfs_trans_cancel(tp
);
836 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
843 struct xfs_bmbt_irec
*imap
,
847 imap
->br_startblock
== HOLESTARTBLOCK
||
848 imap
->br_startblock
== DELAYSTARTBLOCK
||
849 (IS_DAX(inode
) && imap
->br_state
== XFS_EXT_UNWRITTEN
);
853 needs_cow_for_zeroing(
854 struct xfs_bmbt_irec
*imap
,
858 imap
->br_startblock
!= HOLESTARTBLOCK
&&
859 imap
->br_state
!= XFS_EXT_UNWRITTEN
;
864 struct xfs_inode
*ip
,
868 unsigned mode
= XFS_ILOCK_SHARED
;
869 bool is_write
= flags
& (IOMAP_WRITE
| IOMAP_ZERO
);
872 * COW writes may allocate delalloc space or convert unwritten COW
873 * extents, so we need to make sure to take the lock exclusively here.
875 if (xfs_is_cow_inode(ip
) && is_write
) {
877 * FIXME: It could still overwrite on unshared extents and not
880 if (flags
& IOMAP_NOWAIT
)
882 mode
= XFS_ILOCK_EXCL
;
886 * Extents not yet cached requires exclusive access, don't block. This
887 * is an opencoded xfs_ilock_data_map_shared() call but with
888 * non-blocking behaviour.
890 if (!(ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) {
891 if (flags
& IOMAP_NOWAIT
)
893 mode
= XFS_ILOCK_EXCL
;
897 if (flags
& IOMAP_NOWAIT
) {
898 if (!xfs_ilock_nowait(ip
, mode
))
905 * The reflink iflag could have changed since the earlier unlocked
906 * check, so if we got ILOCK_SHARED for a write and but we're now a
907 * reflink inode we have to switch to ILOCK_EXCL and relock.
909 if (mode
== XFS_ILOCK_SHARED
&& is_write
&& xfs_is_cow_inode(ip
)) {
910 xfs_iunlock(ip
, mode
);
911 mode
= XFS_ILOCK_EXCL
;
920 xfs_file_iomap_begin(
927 struct xfs_inode
*ip
= XFS_I(inode
);
928 struct xfs_mount
*mp
= ip
->i_mount
;
929 struct xfs_bmbt_irec imap
;
930 xfs_fileoff_t offset_fsb
, end_fsb
;
931 int nimaps
= 1, error
= 0;
935 if (XFS_FORCED_SHUTDOWN(mp
))
938 if ((flags
& (IOMAP_WRITE
| IOMAP_ZERO
)) && !(flags
& IOMAP_DIRECT
) &&
939 !IS_DAX(inode
) && !xfs_get_extsz_hint(ip
)) {
940 /* Reserve delalloc blocks for regular writeback. */
941 return xfs_file_iomap_begin_delay(inode
, offset
, length
, flags
,
946 * Lock the inode in the manner required for the specified operation and
947 * check for as many conditions that would result in blocking as
948 * possible. This removes most of the non-blocking checks from the
949 * mapping code below.
951 error
= xfs_ilock_for_iomap(ip
, flags
, &lockmode
);
955 ASSERT(offset
<= mp
->m_super
->s_maxbytes
);
956 if (offset
> mp
->m_super
->s_maxbytes
- length
)
957 length
= mp
->m_super
->s_maxbytes
- offset
;
958 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
959 end_fsb
= XFS_B_TO_FSB(mp
, offset
+ length
);
961 error
= xfs_bmapi_read(ip
, offset_fsb
, end_fsb
- offset_fsb
, &imap
,
966 if (flags
& IOMAP_REPORT
) {
967 /* Trim the mapping to the nearest shared extent boundary. */
968 error
= xfs_reflink_trim_around_shared(ip
, &imap
, &shared
);
973 /* Non-modifying mapping requested, so we are done */
974 if (!(flags
& (IOMAP_WRITE
| IOMAP_ZERO
)))
978 * Break shared extents if necessary. Checks for non-blocking IO have
979 * been done up front, so we don't need to do them here.
981 if (xfs_is_cow_inode(ip
)) {
982 struct xfs_bmbt_irec cmap
;
983 bool directio
= (flags
& IOMAP_DIRECT
);
985 /* if zeroing doesn't need COW allocation, then we are done. */
986 if ((flags
& IOMAP_ZERO
) &&
987 !needs_cow_for_zeroing(&imap
, nimaps
))
990 /* may drop and re-acquire the ilock */
992 error
= xfs_reflink_allocate_cow(ip
, &cmap
, &shared
, &lockmode
,
998 * For buffered writes we need to report the address of the
999 * previous block (if there was any) so that the higher level
1000 * write code can perform read-modify-write operations; we
1001 * won't need the CoW fork mapping until writeback. For direct
1002 * I/O, which must be block aligned, we need to report the
1003 * newly allocated address. If the data fork has a hole, copy
1004 * the COW fork mapping to avoid allocating to the data fork.
1006 if (directio
|| imap
.br_startblock
== HOLESTARTBLOCK
)
1009 end_fsb
= imap
.br_startoff
+ imap
.br_blockcount
;
1010 length
= XFS_FSB_TO_B(mp
, end_fsb
) - offset
;
1013 /* Don't need to allocate over holes when doing zeroing operations. */
1014 if (flags
& IOMAP_ZERO
)
1017 if (!imap_needs_alloc(inode
, &imap
, nimaps
))
1020 /* If nowait is set bail since we are going to make allocations. */
1021 if (flags
& IOMAP_NOWAIT
) {
1027 * We cap the maximum length we map to a sane size to keep the chunks
1028 * of work done where somewhat symmetric with the work writeback does.
1029 * This is a completely arbitrary number pulled out of thin air as a
1030 * best guess for initial testing.
1032 * Note that the values needs to be less than 32-bits wide until the
1033 * lower level functions are updated.
1035 length
= min_t(loff_t
, length
, 1024 * PAGE_SIZE
);
1038 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
1041 if (lockmode
== XFS_ILOCK_EXCL
)
1042 xfs_ilock_demote(ip
, lockmode
);
1043 error
= xfs_iomap_write_direct(ip
, offset
, length
, &imap
,
1048 iomap
->flags
|= IOMAP_F_NEW
;
1049 trace_xfs_iomap_alloc(ip
, offset
, length
, XFS_DATA_FORK
, &imap
);
1052 return xfs_bmbt_to_iomap(ip
, iomap
, &imap
, shared
);
1056 xfs_iunlock(ip
, lockmode
);
1057 trace_xfs_iomap_found(ip
, offset
, length
, XFS_DATA_FORK
, &imap
);
1061 xfs_iunlock(ip
, lockmode
);
1066 xfs_file_iomap_end_delalloc(
1067 struct xfs_inode
*ip
,
1071 struct iomap
*iomap
)
1073 struct xfs_mount
*mp
= ip
->i_mount
;
1074 xfs_fileoff_t start_fsb
;
1075 xfs_fileoff_t end_fsb
;
1079 * Behave as if the write failed if drop writes is enabled. Set the NEW
1080 * flag to force delalloc cleanup.
1082 if (XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_DROP_WRITES
)) {
1083 iomap
->flags
|= IOMAP_F_NEW
;
1088 * start_fsb refers to the first unused block after a short write. If
1089 * nothing was written, round offset down to point at the first block in
1092 if (unlikely(!written
))
1093 start_fsb
= XFS_B_TO_FSBT(mp
, offset
);
1095 start_fsb
= XFS_B_TO_FSB(mp
, offset
+ written
);
1096 end_fsb
= XFS_B_TO_FSB(mp
, offset
+ length
);
1099 * Trim delalloc blocks if they were allocated by this write and we
1100 * didn't manage to write the whole range.
1102 * We don't need to care about racing delalloc as we hold i_mutex
1103 * across the reserve/allocate/unreserve calls. If there are delalloc
1104 * blocks in the range, they are ours.
1106 if ((iomap
->flags
& IOMAP_F_NEW
) && start_fsb
< end_fsb
) {
1107 truncate_pagecache_range(VFS_I(ip
), XFS_FSB_TO_B(mp
, start_fsb
),
1108 XFS_FSB_TO_B(mp
, end_fsb
) - 1);
1110 error
= xfs_bmap_punch_delalloc_range(ip
, start_fsb
,
1111 end_fsb
- start_fsb
);
1112 if (error
&& !XFS_FORCED_SHUTDOWN(mp
)) {
1113 xfs_alert(mp
, "%s: unable to clean up ino %lld",
1114 __func__
, ip
->i_ino
);
1124 struct inode
*inode
,
1129 struct iomap
*iomap
)
1131 if ((flags
& IOMAP_WRITE
) && iomap
->type
== IOMAP_DELALLOC
)
1132 return xfs_file_iomap_end_delalloc(XFS_I(inode
), offset
,
1133 length
, written
, iomap
);
1137 const struct iomap_ops xfs_iomap_ops
= {
1138 .iomap_begin
= xfs_file_iomap_begin
,
1139 .iomap_end
= xfs_file_iomap_end
,
1143 xfs_seek_iomap_begin(
1144 struct inode
*inode
,
1148 struct iomap
*iomap
)
1150 struct xfs_inode
*ip
= XFS_I(inode
);
1151 struct xfs_mount
*mp
= ip
->i_mount
;
1152 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
1153 xfs_fileoff_t end_fsb
= XFS_B_TO_FSB(mp
, offset
+ length
);
1154 xfs_fileoff_t cow_fsb
= NULLFILEOFF
, data_fsb
= NULLFILEOFF
;
1155 struct xfs_iext_cursor icur
;
1156 struct xfs_bmbt_irec imap
, cmap
;
1160 if (XFS_FORCED_SHUTDOWN(mp
))
1163 lockmode
= xfs_ilock_data_map_shared(ip
);
1164 if (!(ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) {
1165 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
1170 if (xfs_iext_lookup_extent(ip
, &ip
->i_df
, offset_fsb
, &icur
, &imap
)) {
1172 * If we found a data extent we are done.
1174 if (imap
.br_startoff
<= offset_fsb
)
1176 data_fsb
= imap
.br_startoff
;
1179 * Fake a hole until the end of the file.
1181 data_fsb
= min(XFS_B_TO_FSB(mp
, offset
+ length
),
1182 XFS_B_TO_FSB(mp
, mp
->m_super
->s_maxbytes
));
1186 * If a COW fork extent covers the hole, report it - capped to the next
1189 if (xfs_inode_has_cow_data(ip
) &&
1190 xfs_iext_lookup_extent(ip
, ip
->i_cowfp
, offset_fsb
, &icur
, &cmap
))
1191 cow_fsb
= cmap
.br_startoff
;
1192 if (cow_fsb
!= NULLFILEOFF
&& cow_fsb
<= offset_fsb
) {
1193 if (data_fsb
< cow_fsb
+ cmap
.br_blockcount
)
1194 end_fsb
= min(end_fsb
, data_fsb
);
1195 xfs_trim_extent(&cmap
, offset_fsb
, end_fsb
);
1196 error
= xfs_bmbt_to_iomap(ip
, iomap
, &cmap
, true);
1198 * This is a COW extent, so we must probe the page cache
1199 * because there could be dirty page cache being backed
1202 iomap
->type
= IOMAP_UNWRITTEN
;
1207 * Else report a hole, capped to the next found data or COW extent.
1209 if (cow_fsb
!= NULLFILEOFF
&& cow_fsb
< data_fsb
)
1210 imap
.br_blockcount
= cow_fsb
- offset_fsb
;
1212 imap
.br_blockcount
= data_fsb
- offset_fsb
;
1213 imap
.br_startoff
= offset_fsb
;
1214 imap
.br_startblock
= HOLESTARTBLOCK
;
1215 imap
.br_state
= XFS_EXT_NORM
;
1217 xfs_trim_extent(&imap
, offset_fsb
, end_fsb
);
1218 error
= xfs_bmbt_to_iomap(ip
, iomap
, &imap
, false);
1220 xfs_iunlock(ip
, lockmode
);
1224 const struct iomap_ops xfs_seek_iomap_ops
= {
1225 .iomap_begin
= xfs_seek_iomap_begin
,
1229 xfs_xattr_iomap_begin(
1230 struct inode
*inode
,
1234 struct iomap
*iomap
)
1236 struct xfs_inode
*ip
= XFS_I(inode
);
1237 struct xfs_mount
*mp
= ip
->i_mount
;
1238 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
1239 xfs_fileoff_t end_fsb
= XFS_B_TO_FSB(mp
, offset
+ length
);
1240 struct xfs_bmbt_irec imap
;
1241 int nimaps
= 1, error
= 0;
1244 if (XFS_FORCED_SHUTDOWN(mp
))
1247 lockmode
= xfs_ilock_attr_map_shared(ip
);
1249 /* if there are no attribute fork or extents, return ENOENT */
1250 if (!XFS_IFORK_Q(ip
) || !ip
->i_d
.di_anextents
) {
1255 ASSERT(ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
);
1256 error
= xfs_bmapi_read(ip
, offset_fsb
, end_fsb
- offset_fsb
, &imap
,
1257 &nimaps
, XFS_BMAPI_ATTRFORK
);
1259 xfs_iunlock(ip
, lockmode
);
1264 return xfs_bmbt_to_iomap(ip
, iomap
, &imap
, false);
1267 const struct iomap_ops xfs_xattr_iomap_ops
= {
1268 .iomap_begin
= xfs_xattr_iomap_begin
,