2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2016 Christoph Hellwig.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/iomap.h>
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_log_format.h"
25 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_inode.h"
29 #include "xfs_btree.h"
30 #include "xfs_bmap_btree.h"
32 #include "xfs_bmap_util.h"
33 #include "xfs_error.h"
34 #include "xfs_trans.h"
35 #include "xfs_trans_space.h"
36 #include "xfs_iomap.h"
37 #include "xfs_trace.h"
38 #include "xfs_icache.h"
39 #include "xfs_quota.h"
40 #include "xfs_dquot_item.h"
41 #include "xfs_dquot.h"
42 #include "xfs_reflink.h"
45 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
52 struct xfs_bmbt_irec
*imap
)
54 struct xfs_mount
*mp
= ip
->i_mount
;
56 if (imap
->br_startblock
== HOLESTARTBLOCK
) {
57 iomap
->blkno
= IOMAP_NULL_BLOCK
;
58 iomap
->type
= IOMAP_HOLE
;
59 } else if (imap
->br_startblock
== DELAYSTARTBLOCK
) {
60 iomap
->blkno
= IOMAP_NULL_BLOCK
;
61 iomap
->type
= IOMAP_DELALLOC
;
63 iomap
->blkno
= xfs_fsb_to_db(ip
, imap
->br_startblock
);
64 if (imap
->br_state
== XFS_EXT_UNWRITTEN
)
65 iomap
->type
= IOMAP_UNWRITTEN
;
67 iomap
->type
= IOMAP_MAPPED
;
69 iomap
->offset
= XFS_FSB_TO_B(mp
, imap
->br_startoff
);
70 iomap
->length
= XFS_FSB_TO_B(mp
, imap
->br_blockcount
);
71 iomap
->bdev
= xfs_find_bdev_for_inode(VFS_I(ip
));
79 struct xfs_mount
*mp
= ip
->i_mount
;
80 xfs_extlen_t align
= 0;
82 if (!XFS_IS_REALTIME_INODE(ip
)) {
84 * Round up the allocation request to a stripe unit
85 * (m_dalign) boundary if the file size is >= stripe unit
86 * size, and we are allocating past the allocation eof.
88 * If mounted with the "-o swalloc" option the alignment is
89 * increased from the strip unit size to the stripe width.
91 if (mp
->m_swidth
&& (mp
->m_flags
& XFS_MOUNT_SWALLOC
))
93 else if (mp
->m_dalign
)
96 if (align
&& XFS_ISIZE(ip
) < XFS_FSB_TO_B(mp
, align
))
101 * Always round up the allocation request to an extent boundary
102 * (when file on a real-time subvolume or has di_extsize hint).
106 align
= roundup_64(align
, extsize
);
115 xfs_iomap_eof_align_last_fsb(
116 struct xfs_inode
*ip
,
117 xfs_extlen_t extsize
,
118 xfs_fileoff_t
*last_fsb
)
120 xfs_extlen_t align
= xfs_eof_alignment(ip
, extsize
);
123 xfs_fileoff_t new_last_fsb
= roundup_64(*last_fsb
, align
);
126 error
= xfs_bmap_eof(ip
, new_last_fsb
, XFS_DATA_FORK
, &eof
);
130 *last_fsb
= new_last_fsb
;
136 xfs_alert_fsblock_zero(
138 xfs_bmbt_irec_t
*imap
)
140 xfs_alert_tag(ip
->i_mount
, XFS_PTAG_FSBLOCK_ZERO
,
141 "Access to block zero in inode %llu "
142 "start_block: %llx start_off: %llx "
143 "blkcnt: %llx extent-state: %x",
144 (unsigned long long)ip
->i_ino
,
145 (unsigned long long)imap
->br_startblock
,
146 (unsigned long long)imap
->br_startoff
,
147 (unsigned long long)imap
->br_blockcount
,
149 return -EFSCORRUPTED
;
153 xfs_iomap_write_direct(
157 xfs_bmbt_irec_t
*imap
,
160 xfs_mount_t
*mp
= ip
->i_mount
;
161 xfs_fileoff_t offset_fsb
;
162 xfs_fileoff_t last_fsb
;
163 xfs_filblks_t count_fsb
, resaligned
;
164 xfs_fsblock_t firstfsb
;
165 xfs_extlen_t extsz
, temp
;
170 struct xfs_defer_ops dfops
;
171 uint qblocks
, resblks
, resrtextents
;
174 int bmapi_flags
= XFS_BMAPI_PREALLOC
;
177 rt
= XFS_IS_REALTIME_INODE(ip
);
178 extsz
= xfs_get_extsz_hint(ip
);
179 lockmode
= XFS_ILOCK_SHARED
; /* locked by caller */
181 ASSERT(xfs_isilocked(ip
, lockmode
));
183 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
184 last_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)(offset
+ count
)));
185 if ((offset
+ count
) > XFS_ISIZE(ip
)) {
187 * Assert that the in-core extent list is present since this can
188 * call xfs_iread_extents() and we only have the ilock shared.
189 * This should be safe because the lock was held around a bmapi
190 * call in the caller and we only need it to access the in-core
193 ASSERT(XFS_IFORK_PTR(ip
, XFS_DATA_FORK
)->if_flags
&
195 error
= xfs_iomap_eof_align_last_fsb(ip
, extsz
, &last_fsb
);
199 if (nmaps
&& (imap
->br_startblock
== HOLESTARTBLOCK
))
200 last_fsb
= MIN(last_fsb
, (xfs_fileoff_t
)
201 imap
->br_blockcount
+
204 count_fsb
= last_fsb
- offset_fsb
;
205 ASSERT(count_fsb
> 0);
207 resaligned
= count_fsb
;
208 if (unlikely(extsz
)) {
209 if ((temp
= do_mod(offset_fsb
, extsz
)))
211 if ((temp
= do_mod(resaligned
, extsz
)))
212 resaligned
+= extsz
- temp
;
216 resrtextents
= qblocks
= resaligned
;
217 resrtextents
/= mp
->m_sb
.sb_rextsize
;
218 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
219 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
222 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resaligned
);
223 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
227 * Drop the shared lock acquired by the caller, attach the dquot if
228 * necessary and move on to transaction setup.
230 xfs_iunlock(ip
, lockmode
);
231 error
= xfs_qm_dqattach(ip
, 0);
236 * For DAX, we do not allocate unwritten extents, but instead we zero
237 * the block before we commit the transaction. Ideally we'd like to do
238 * this outside the transaction context, but if we commit and then crash
239 * we may not have zeroed the blocks and this will be exposed on
240 * recovery of the allocation. Hence we must zero before commit.
242 * Further, if we are mapping unwritten extents here, we need to zero
243 * and convert them to written so that we don't need an unwritten extent
244 * callback for DAX. This also means that we need to be able to dip into
245 * the reserve block pool for bmbt block allocation if there is no space
246 * left but we need to do unwritten extent conversion.
248 if (IS_DAX(VFS_I(ip
))) {
249 bmapi_flags
= XFS_BMAPI_CONVERT
| XFS_BMAPI_ZERO
;
250 if (ISUNWRITTEN(imap
)) {
251 tflags
|= XFS_TRANS_RESERVE
;
252 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0) << 1;
255 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, resrtextents
,
260 lockmode
= XFS_ILOCK_EXCL
;
261 xfs_ilock(ip
, lockmode
);
263 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, qblocks
, 0, quota_flag
);
265 goto out_trans_cancel
;
267 xfs_trans_ijoin(tp
, ip
, 0);
270 * From this point onwards we overwrite the imap pointer that the
273 xfs_defer_init(&dfops
, &firstfsb
);
275 error
= xfs_bmapi_write(tp
, ip
, offset_fsb
, count_fsb
,
276 bmapi_flags
, &firstfsb
, resblks
, imap
,
279 goto out_bmap_cancel
;
282 * Complete the transaction
284 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
286 goto out_bmap_cancel
;
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_defer_cancel(&dfops
);
309 xfs_trans_unreserve_quota_nblks(tp
, ip
, (long)qblocks
, 0, quota_flag
);
311 xfs_trans_cancel(tp
);
316 xfs_quota_need_throttle(
317 struct xfs_inode
*ip
,
319 xfs_fsblock_t alloc_blocks
)
321 struct xfs_dquot
*dq
= xfs_inode_dquot(ip
, type
);
323 if (!dq
|| !xfs_this_quota_on(ip
->i_mount
, type
))
326 /* no hi watermark, no throttle */
327 if (!dq
->q_prealloc_hi_wmark
)
330 /* under the lo watermark, no throttle */
331 if (dq
->q_res_bcount
+ alloc_blocks
< dq
->q_prealloc_lo_wmark
)
338 xfs_quota_calc_throttle(
339 struct xfs_inode
*ip
,
341 xfs_fsblock_t
*qblocks
,
347 struct xfs_dquot
*dq
= xfs_inode_dquot(ip
, type
);
349 /* no dq, or over hi wmark, squash the prealloc completely */
350 if (!dq
|| dq
->q_res_bcount
>= dq
->q_prealloc_hi_wmark
) {
356 freesp
= dq
->q_prealloc_hi_wmark
- dq
->q_res_bcount
;
357 if (freesp
< dq
->q_low_space
[XFS_QLOWSP_5_PCNT
]) {
359 if (freesp
< dq
->q_low_space
[XFS_QLOWSP_3_PCNT
])
361 if (freesp
< dq
->q_low_space
[XFS_QLOWSP_1_PCNT
])
365 if (freesp
< *qfreesp
)
368 /* only overwrite the throttle values if we are more aggressive */
369 if ((freesp
>> shift
) < (*qblocks
>> *qshift
)) {
376 * If we are doing a write at the end of the file and there are no allocations
377 * past this one, then extend the allocation out to the file system's write
380 * If we don't have a user specified preallocation size, dynamically increase
381 * the preallocation size as the size of the file grows. Cap the maximum size
382 * at a single extent or less if the filesystem is near full. The closer the
383 * filesystem is to full, the smaller the maximum prealocation.
385 * As an exception we don't do any preallocation at all if the file is smaller
386 * than the minimum preallocation and we are using the default dynamic
387 * preallocation scheme, as it is likely this is the only write to the file that
388 * is going to be done.
390 * We clean up any extra space left over when the file is closed in
394 xfs_iomap_prealloc_size(
395 struct xfs_inode
*ip
,
399 struct xfs_bmbt_irec
*prev
)
401 struct xfs_mount
*mp
= ip
->i_mount
;
402 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
405 xfs_fsblock_t qblocks
;
407 xfs_fsblock_t alloc_blocks
= 0;
409 if (offset
+ count
<= XFS_ISIZE(ip
))
412 if (!(mp
->m_flags
& XFS_MOUNT_DFLT_IOSIZE
) &&
413 (XFS_ISIZE(ip
) < XFS_FSB_TO_B(mp
, mp
->m_writeio_blocks
)))
417 * If an explicit allocsize is set, the file is small, or we
418 * are writing behind a hole, then use the minimum prealloc:
420 if ((mp
->m_flags
& XFS_MOUNT_DFLT_IOSIZE
) ||
421 XFS_ISIZE(ip
) < XFS_FSB_TO_B(mp
, mp
->m_dalign
) ||
423 prev
->br_startoff
+ prev
->br_blockcount
< offset_fsb
)
424 return mp
->m_writeio_blocks
;
427 * Determine the initial size of the preallocation. We are beyond the
428 * current EOF here, but we need to take into account whether this is
429 * a sparse write or an extending write when determining the
430 * preallocation size. Hence we need to look up the extent that ends
431 * at the current write offset and use the result to determine the
432 * preallocation size.
434 * If the extent is a hole, then preallocation is essentially disabled.
435 * Otherwise we take the size of the preceding data extent as the basis
436 * for the preallocation size. If the size of the extent is greater than
437 * half the maximum extent length, then use the current offset as the
438 * basis. This ensures that for large files the preallocation size
439 * always extends to MAXEXTLEN rather than falling short due to things
440 * like stripe unit/width alignment of real extents.
442 if (prev
->br_blockcount
<= (MAXEXTLEN
>> 1))
443 alloc_blocks
= prev
->br_blockcount
<< 1;
445 alloc_blocks
= XFS_B_TO_FSB(mp
, offset
);
448 qblocks
= alloc_blocks
;
451 * MAXEXTLEN is not a power of two value but we round the prealloc down
452 * to the nearest power of two value after throttling. To prevent the
453 * round down from unconditionally reducing the maximum supported prealloc
454 * size, we round up first, apply appropriate throttling, round down and
455 * cap the value to MAXEXTLEN.
457 alloc_blocks
= XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN
),
460 freesp
= percpu_counter_read_positive(&mp
->m_fdblocks
);
461 if (freesp
< mp
->m_low_space
[XFS_LOWSP_5_PCNT
]) {
463 if (freesp
< mp
->m_low_space
[XFS_LOWSP_4_PCNT
])
465 if (freesp
< mp
->m_low_space
[XFS_LOWSP_3_PCNT
])
467 if (freesp
< mp
->m_low_space
[XFS_LOWSP_2_PCNT
])
469 if (freesp
< mp
->m_low_space
[XFS_LOWSP_1_PCNT
])
474 * Check each quota to cap the prealloc size, provide a shift value to
475 * throttle with and adjust amount of available space.
477 if (xfs_quota_need_throttle(ip
, XFS_DQ_USER
, alloc_blocks
))
478 xfs_quota_calc_throttle(ip
, XFS_DQ_USER
, &qblocks
, &qshift
,
480 if (xfs_quota_need_throttle(ip
, XFS_DQ_GROUP
, alloc_blocks
))
481 xfs_quota_calc_throttle(ip
, XFS_DQ_GROUP
, &qblocks
, &qshift
,
483 if (xfs_quota_need_throttle(ip
, XFS_DQ_PROJ
, alloc_blocks
))
484 xfs_quota_calc_throttle(ip
, XFS_DQ_PROJ
, &qblocks
, &qshift
,
488 * The final prealloc size is set to the minimum of free space available
489 * in each of the quotas and the overall filesystem.
491 * The shift throttle value is set to the maximum value as determined by
492 * the global low free space values and per-quota low free space values.
494 alloc_blocks
= MIN(alloc_blocks
, qblocks
);
495 shift
= MAX(shift
, qshift
);
498 alloc_blocks
>>= shift
;
500 * rounddown_pow_of_two() returns an undefined result if we pass in
504 alloc_blocks
= rounddown_pow_of_two(alloc_blocks
);
505 if (alloc_blocks
> MAXEXTLEN
)
506 alloc_blocks
= MAXEXTLEN
;
509 * If we are still trying to allocate more space than is
510 * available, squash the prealloc hard. This can happen if we
511 * have a large file on a small filesystem and the above
512 * lowspace thresholds are smaller than MAXEXTLEN.
514 while (alloc_blocks
&& alloc_blocks
>= freesp
)
517 if (alloc_blocks
< mp
->m_writeio_blocks
)
518 alloc_blocks
= mp
->m_writeio_blocks
;
519 trace_xfs_iomap_prealloc_size(ip
, alloc_blocks
, shift
,
520 mp
->m_writeio_blocks
);
525 xfs_file_iomap_begin_delay(
532 struct xfs_inode
*ip
= XFS_I(inode
);
533 struct xfs_mount
*mp
= ip
->i_mount
;
534 struct xfs_ifork
*ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
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
, orig_end_fsb
;
539 int error
= 0, eof
= 0;
540 struct xfs_bmbt_irec got
;
541 struct xfs_bmbt_irec prev
;
544 ASSERT(!XFS_IS_REALTIME_INODE(ip
));
545 ASSERT(!xfs_get_extsz_hint(ip
));
547 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
549 if (unlikely(XFS_TEST_ERROR(
550 (XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_EXTENTS
&&
551 XFS_IFORK_FORMAT(ip
, XFS_DATA_FORK
) != XFS_DINODE_FMT_BTREE
),
552 mp
, XFS_ERRTAG_BMAPIFORMAT
, XFS_RANDOM_BMAPIFORMAT
))) {
553 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
554 error
= -EFSCORRUPTED
;
558 XFS_STATS_INC(mp
, xs_blk_mapw
);
560 if (!(ifp
->if_flags
& XFS_IFEXTENTS
)) {
561 error
= xfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
566 xfs_bmap_search_extents(ip
, offset_fsb
, XFS_DATA_FORK
, &eof
, &idx
,
568 if (!eof
&& got
.br_startoff
<= offset_fsb
) {
569 trace_xfs_iomap_found(ip
, offset
, count
, 0, &got
);
573 error
= xfs_qm_dqattach_locked(ip
, 0);
578 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES pages
579 * to keep the chunks of work done where somewhat symmetric with the
580 * work writeback does. This is a completely arbitrary number pulled
581 * out of thin air as a best guess for initial testing.
583 * Note that the values needs to be less than 32-bits wide until
584 * the lower level functions are updated.
586 count
= min_t(loff_t
, count
, 1024 * PAGE_SIZE
);
587 end_fsb
= orig_end_fsb
=
588 min(XFS_B_TO_FSB(mp
, offset
+ count
), maxbytes_fsb
);
591 xfs_fsblock_t prealloc_blocks
;
594 xfs_iomap_prealloc_size(ip
, offset
, count
, idx
, &prev
);
595 if (prealloc_blocks
) {
597 xfs_off_t end_offset
;
599 end_offset
= XFS_WRITEIO_ALIGN(mp
, offset
+ count
- 1);
600 end_fsb
= XFS_B_TO_FSBT(mp
, end_offset
) +
603 align
= xfs_eof_alignment(ip
, 0);
605 end_fsb
= roundup_64(end_fsb
, align
);
607 end_fsb
= min(end_fsb
, maxbytes_fsb
);
608 ASSERT(end_fsb
> offset_fsb
);
613 error
= xfs_bmapi_reserve_delalloc(ip
, XFS_DATA_FORK
, offset_fsb
,
614 end_fsb
- offset_fsb
, &got
,
621 /* retry without any preallocation */
622 trace_xfs_delalloc_enospc(ip
, offset
, count
);
623 if (end_fsb
!= orig_end_fsb
) {
624 end_fsb
= orig_end_fsb
;
633 * Tag the inode as speculatively preallocated so we can reclaim this
634 * space on demand, if necessary.
636 if (end_fsb
!= orig_end_fsb
)
637 xfs_inode_set_eofblocks_tag(ip
);
639 trace_xfs_iomap_alloc(ip
, offset
, count
, 0, &got
);
641 if (isnullstartblock(got
.br_startblock
))
642 got
.br_startblock
= DELAYSTARTBLOCK
;
644 if (!got
.br_startblock
) {
645 error
= xfs_alert_fsblock_zero(ip
, &got
);
650 xfs_bmbt_to_iomap(ip
, iomap
, &got
);
653 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
658 * Pass in a delayed allocate extent, convert it to real extents;
659 * return to the caller the extent we create which maps on top of
660 * the originating callers request.
662 * Called without a lock on the inode.
664 * We no longer bother to look at the incoming map - all we have to
665 * guarantee is that whatever we allocate fills the required range.
668 xfs_iomap_write_allocate(
672 xfs_bmbt_irec_t
*imap
)
674 xfs_mount_t
*mp
= ip
->i_mount
;
675 xfs_fileoff_t offset_fsb
, last_block
;
676 xfs_fileoff_t end_fsb
, map_start_fsb
;
677 xfs_fsblock_t first_block
;
678 struct xfs_defer_ops dfops
;
679 xfs_filblks_t count_fsb
;
686 if (whichfork
== XFS_COW_FORK
)
687 flags
|= XFS_BMAPI_COWFORK
;
690 * Make sure that the dquots are there.
692 error
= xfs_qm_dqattach(ip
, 0);
696 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
697 count_fsb
= imap
->br_blockcount
;
698 map_start_fsb
= imap
->br_startoff
;
700 XFS_STATS_ADD(mp
, xs_xstrat_bytes
, XFS_FSB_TO_B(mp
, count_fsb
));
702 while (count_fsb
!= 0) {
704 * Set up a transaction with which to allocate the
705 * backing store for the file. Do allocations in a
706 * loop until we get some space in the range we are
707 * interested in. The other space that might be allocated
708 * is in the delayed allocation extent on which we sit
709 * but before our buffer starts.
712 while (nimaps
== 0) {
713 nres
= XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
);
715 * We have already reserved space for the extent and any
716 * indirect blocks when creating the delalloc extent,
717 * there is no need to reserve space in this transaction
720 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, 0,
721 0, XFS_TRANS_RESERVE
, &tp
);
725 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
726 xfs_trans_ijoin(tp
, ip
, 0);
728 xfs_defer_init(&dfops
, &first_block
);
731 * it is possible that the extents have changed since
732 * we did the read call as we dropped the ilock for a
733 * while. We have to be careful about truncates or hole
734 * punchs here - we are not allowed to allocate
735 * non-delalloc blocks here.
737 * The only protection against truncation is the pages
738 * for the range we are being asked to convert are
739 * locked and hence a truncate will block on them
742 * As a result, if we go beyond the range we really
743 * need and hit an delalloc extent boundary followed by
744 * a hole while we have excess blocks in the map, we
745 * will fill the hole incorrectly and overrun the
746 * transaction reservation.
748 * Using a single map prevents this as we are forced to
749 * check each map we look for overlap with the desired
750 * range and abort as soon as we find it. Also, given
751 * that we only return a single map, having one beyond
752 * what we can return is probably a bit silly.
754 * We also need to check that we don't go beyond EOF;
755 * this is a truncate optimisation as a truncate sets
756 * the new file size before block on the pages we
757 * currently have locked under writeback. Because they
758 * are about to be tossed, we don't need to write them
762 end_fsb
= XFS_B_TO_FSB(mp
, XFS_ISIZE(ip
));
763 error
= xfs_bmap_last_offset(ip
, &last_block
,
768 last_block
= XFS_FILEOFF_MAX(last_block
, end_fsb
);
769 if ((map_start_fsb
+ count_fsb
) > last_block
) {
770 count_fsb
= last_block
- map_start_fsb
;
771 if (count_fsb
== 0) {
778 * From this point onwards we overwrite the imap
779 * pointer that the caller gave to us.
781 error
= xfs_bmapi_write(tp
, ip
, map_start_fsb
,
782 count_fsb
, flags
, &first_block
,
788 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
792 error
= xfs_trans_commit(tp
);
796 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
800 * See if we were able to allocate an extent that
801 * covers at least part of the callers request
803 if (!(imap
->br_startblock
|| XFS_IS_REALTIME_INODE(ip
)))
804 return xfs_alert_fsblock_zero(ip
, imap
);
806 if ((offset_fsb
>= imap
->br_startoff
) &&
807 (offset_fsb
< (imap
->br_startoff
+
808 imap
->br_blockcount
))) {
809 XFS_STATS_INC(mp
, xs_xstrat_quick
);
814 * So far we have not mapped the requested part of the
815 * file, just surrounding data, try again.
817 count_fsb
-= imap
->br_blockcount
;
818 map_start_fsb
= imap
->br_startoff
+ imap
->br_blockcount
;
822 xfs_defer_cancel(&dfops
);
823 xfs_trans_cancel(tp
);
825 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
830 xfs_iomap_write_unwritten(
835 xfs_mount_t
*mp
= ip
->i_mount
;
836 xfs_fileoff_t offset_fsb
;
837 xfs_filblks_t count_fsb
;
838 xfs_filblks_t numblks_fsb
;
839 xfs_fsblock_t firstfsb
;
842 xfs_bmbt_irec_t imap
;
843 struct xfs_defer_ops dfops
;
848 trace_xfs_unwritten_convert(ip
, offset
, count
);
850 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
851 count_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)offset
+ count
);
852 count_fsb
= (xfs_filblks_t
)(count_fsb
- offset_fsb
);
855 * Reserve enough blocks in this transaction for two complete extent
856 * btree splits. We may be converting the middle part of an unwritten
857 * extent and in this case we will insert two new extents in the btree
858 * each of which could cause a full split.
860 * This reservation amount will be used in the first call to
861 * xfs_bmbt_split() to select an AG with enough space to satisfy the
862 * rest of the operation.
864 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0) << 1;
868 * Set up a transaction to convert the range of extents
869 * from unwritten to real. Do allocations in a loop until
870 * we have covered the range passed in.
872 * Note that we can't risk to recursing back into the filesystem
873 * here as we might be asked to write out the same inode that we
874 * complete here and might deadlock on the iolock.
876 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_write
, resblks
, 0,
877 XFS_TRANS_RESERVE
| XFS_TRANS_NOFS
, &tp
);
881 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
882 xfs_trans_ijoin(tp
, ip
, 0);
885 * Modify the unwritten extent state of the buffer.
887 xfs_defer_init(&dfops
, &firstfsb
);
889 error
= xfs_bmapi_write(tp
, ip
, offset_fsb
, count_fsb
,
890 XFS_BMAPI_CONVERT
, &firstfsb
, resblks
,
891 &imap
, &nimaps
, &dfops
);
893 goto error_on_bmapi_transaction
;
896 * Log the updated inode size as we go. We have to be careful
897 * to only log it up to the actual write offset if it is
898 * halfway into a block.
900 i_size
= XFS_FSB_TO_B(mp
, offset_fsb
+ count_fsb
);
901 if (i_size
> offset
+ count
)
902 i_size
= offset
+ count
;
904 i_size
= xfs_new_eof(ip
, i_size
);
906 ip
->i_d
.di_size
= i_size
;
907 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
910 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
912 goto error_on_bmapi_transaction
;
914 error
= xfs_trans_commit(tp
);
915 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
919 if (!(imap
.br_startblock
|| XFS_IS_REALTIME_INODE(ip
)))
920 return xfs_alert_fsblock_zero(ip
, &imap
);
922 if ((numblks_fsb
= imap
.br_blockcount
) == 0) {
924 * The numblks_fsb value should always get
925 * smaller, otherwise the loop is stuck.
927 ASSERT(imap
.br_blockcount
);
930 offset_fsb
+= numblks_fsb
;
931 count_fsb
-= numblks_fsb
;
932 } while (count_fsb
> 0);
936 error_on_bmapi_transaction
:
937 xfs_defer_cancel(&dfops
);
938 xfs_trans_cancel(tp
);
939 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
943 static inline bool imap_needs_alloc(struct inode
*inode
,
944 struct xfs_bmbt_irec
*imap
, int nimaps
)
947 imap
->br_startblock
== HOLESTARTBLOCK
||
948 imap
->br_startblock
== DELAYSTARTBLOCK
||
949 (IS_DAX(inode
) && ISUNWRITTEN(imap
));
953 xfs_file_iomap_begin(
960 struct xfs_inode
*ip
= XFS_I(inode
);
961 struct xfs_mount
*mp
= ip
->i_mount
;
962 struct xfs_bmbt_irec imap
;
963 xfs_fileoff_t offset_fsb
, end_fsb
;
964 bool shared
, trimmed
;
965 int nimaps
= 1, error
= 0;
968 if (XFS_FORCED_SHUTDOWN(mp
))
971 if ((flags
& (IOMAP_WRITE
| IOMAP_ZERO
)) && xfs_is_reflink_inode(ip
)) {
972 error
= xfs_reflink_reserve_cow_range(ip
, offset
, length
);
977 if ((flags
& IOMAP_WRITE
) && !IS_DAX(inode
) &&
978 !xfs_get_extsz_hint(ip
)) {
979 /* Reserve delalloc blocks for regular writeback. */
980 return xfs_file_iomap_begin_delay(inode
, offset
, length
, flags
,
984 lockmode
= xfs_ilock_data_map_shared(ip
);
986 ASSERT(offset
<= mp
->m_super
->s_maxbytes
);
987 if ((xfs_fsize_t
)offset
+ length
> mp
->m_super
->s_maxbytes
)
988 length
= mp
->m_super
->s_maxbytes
- offset
;
989 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
990 end_fsb
= XFS_B_TO_FSB(mp
, offset
+ length
);
992 error
= xfs_bmapi_read(ip
, offset_fsb
, end_fsb
- offset_fsb
, &imap
,
995 xfs_iunlock(ip
, lockmode
);
999 /* Trim the mapping to the nearest shared extent boundary. */
1000 error
= xfs_reflink_trim_around_shared(ip
, &imap
, &shared
, &trimmed
);
1002 xfs_iunlock(ip
, lockmode
);
1006 if ((flags
& IOMAP_WRITE
) && imap_needs_alloc(inode
, &imap
, nimaps
)) {
1008 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
1009 * pages to keep the chunks of work done where somewhat symmetric
1010 * with the work writeback does. This is a completely arbitrary
1011 * number pulled out of thin air as a best guess for initial
1014 * Note that the values needs to be less than 32-bits wide until
1015 * the lower level functions are updated.
1017 length
= min_t(loff_t
, length
, 1024 * PAGE_SIZE
);
1019 * xfs_iomap_write_direct() expects the shared lock. It
1020 * is unlocked on return.
1022 if (lockmode
== XFS_ILOCK_EXCL
)
1023 xfs_ilock_demote(ip
, lockmode
);
1024 error
= xfs_iomap_write_direct(ip
, offset
, length
, &imap
,
1029 iomap
->flags
= IOMAP_F_NEW
;
1030 trace_xfs_iomap_alloc(ip
, offset
, length
, 0, &imap
);
1034 xfs_iunlock(ip
, lockmode
);
1035 trace_xfs_iomap_found(ip
, offset
, length
, 0, &imap
);
1038 xfs_bmbt_to_iomap(ip
, iomap
, &imap
);
1040 iomap
->flags
|= IOMAP_F_SHARED
;
1045 xfs_file_iomap_end_delalloc(
1046 struct xfs_inode
*ip
,
1051 struct xfs_mount
*mp
= ip
->i_mount
;
1052 xfs_fileoff_t start_fsb
;
1053 xfs_fileoff_t end_fsb
;
1056 start_fsb
= XFS_B_TO_FSB(mp
, offset
+ written
);
1057 end_fsb
= XFS_B_TO_FSB(mp
, offset
+ length
);
1060 * Trim back delalloc blocks if we didn't manage to write the whole
1063 * We don't need to care about racing delalloc as we hold i_mutex
1064 * across the reserve/allocate/unreserve calls. If there are delalloc
1065 * blocks in the range, they are ours.
1067 if (start_fsb
< end_fsb
) {
1068 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1069 error
= xfs_bmap_punch_delalloc_range(ip
, start_fsb
,
1070 end_fsb
- start_fsb
);
1071 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1073 if (error
&& !XFS_FORCED_SHUTDOWN(mp
)) {
1074 xfs_alert(mp
, "%s: unable to clean up ino %lld",
1075 __func__
, ip
->i_ino
);
1085 struct inode
*inode
,
1090 struct iomap
*iomap
)
1092 if ((flags
& IOMAP_WRITE
) && iomap
->type
== IOMAP_DELALLOC
)
1093 return xfs_file_iomap_end_delalloc(XFS_I(inode
), offset
,
1098 struct iomap_ops xfs_iomap_ops
= {
1099 .iomap_begin
= xfs_file_iomap_begin
,
1100 .iomap_end
= xfs_file_iomap_end
,
1104 xfs_xattr_iomap_begin(
1105 struct inode
*inode
,
1109 struct iomap
*iomap
)
1111 struct xfs_inode
*ip
= XFS_I(inode
);
1112 struct xfs_mount
*mp
= ip
->i_mount
;
1113 xfs_fileoff_t offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
1114 xfs_fileoff_t end_fsb
= XFS_B_TO_FSB(mp
, offset
+ length
);
1115 struct xfs_bmbt_irec imap
;
1116 int nimaps
= 1, error
= 0;
1119 if (XFS_FORCED_SHUTDOWN(mp
))
1122 lockmode
= xfs_ilock_data_map_shared(ip
);
1124 /* if there are no attribute fork or extents, return ENOENT */
1125 if (XFS_IFORK_Q(ip
) || !ip
->i_d
.di_anextents
) {
1130 ASSERT(ip
->i_d
.di_aformat
!= XFS_DINODE_FMT_LOCAL
);
1131 error
= xfs_bmapi_read(ip
, offset_fsb
, end_fsb
- offset_fsb
, &imap
,
1132 &nimaps
, XFS_BMAPI_ENTIRE
| XFS_BMAPI_ATTRFORK
);
1134 xfs_iunlock(ip
, lockmode
);
1138 xfs_bmbt_to_iomap(ip
, iomap
, &imap
);
1144 struct iomap_ops xfs_xattr_iomap_ops
= {
1145 .iomap_begin
= xfs_xattr_iomap_begin
,