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"
31 #define XFS_ALLOC_ALIGN(mp, off) \
32 (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
35 xfs_alert_fsblock_zero(
37 xfs_bmbt_irec_t *imap)
39 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
40 "Access to block zero in inode %llu "
41 "start_block: %llx start_off: %llx "
42 "blkcnt: %llx extent-state: %x",
43 (unsigned long long)ip->i_ino,
44 (unsigned long long)imap->br_startblock,
45 (unsigned long long)imap->br_startoff,
46 (unsigned long long)imap->br_blockcount,
55 struct xfs_bmbt_irec *imap,
56 unsigned int mapping_flags,
59 struct xfs_mount *mp = ip->i_mount;
60 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
62 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
63 return xfs_alert_fsblock_zero(ip, imap);
65 if (imap->br_startblock == HOLESTARTBLOCK) {
66 iomap->addr = IOMAP_NULL_ADDR;
67 iomap->type = IOMAP_HOLE;
68 } else if (imap->br_startblock == DELAYSTARTBLOCK ||
69 isnullstartblock(imap->br_startblock)) {
70 iomap->addr = IOMAP_NULL_ADDR;
71 iomap->type = IOMAP_DELALLOC;
73 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
74 if (mapping_flags & IOMAP_DAX)
75 iomap->addr += target->bt_dax_part_off;
77 if (imap->br_state == XFS_EXT_UNWRITTEN)
78 iomap->type = IOMAP_UNWRITTEN;
80 iomap->type = IOMAP_MAPPED;
83 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
84 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
85 if (mapping_flags & IOMAP_DAX)
86 iomap->dax_dev = target->bt_daxdev;
88 iomap->bdev = target->bt_bdev;
89 iomap->flags = iomap_flags;
91 if (xfs_ipincount(ip) &&
92 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
93 iomap->flags |= IOMAP_F_DIRTY;
101 xfs_fileoff_t offset_fsb,
102 xfs_fileoff_t end_fsb)
104 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
106 iomap->addr = IOMAP_NULL_ADDR;
107 iomap->type = IOMAP_HOLE;
108 iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
109 iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
110 iomap->bdev = target->bt_bdev;
111 iomap->dax_dev = target->bt_daxdev;
114 static inline xfs_fileoff_t
116 struct xfs_mount *mp,
120 ASSERT(offset <= mp->m_super->s_maxbytes);
121 return min(XFS_B_TO_FSB(mp, offset + count),
122 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
127 struct xfs_inode *ip)
129 struct xfs_mount *mp = ip->i_mount;
130 xfs_extlen_t align = 0;
132 if (!XFS_IS_REALTIME_INODE(ip)) {
134 * Round up the allocation request to a stripe unit
135 * (m_dalign) boundary if the file size is >= stripe unit
136 * size, and we are allocating past the allocation eof.
138 * If mounted with the "-o swalloc" option the alignment is
139 * increased from the strip unit size to the stripe width.
141 if (mp->m_swidth && xfs_has_swalloc(mp))
142 align = mp->m_swidth;
143 else if (mp->m_dalign)
144 align = mp->m_dalign;
146 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
154 * Check if last_fsb is outside the last extent, and if so grow it to the next
155 * stripe unit boundary.
158 xfs_iomap_eof_align_last_fsb(
159 struct xfs_inode *ip,
160 xfs_fileoff_t end_fsb)
162 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
163 xfs_extlen_t extsz = xfs_get_extsz_hint(ip);
164 xfs_extlen_t align = xfs_eof_alignment(ip);
165 struct xfs_bmbt_irec irec;
166 struct xfs_iext_cursor icur;
168 ASSERT(!xfs_need_iread_extents(ifp));
171 * Always round up the allocation request to the extent hint boundary.
175 align = roundup_64(align, extsz);
181 xfs_fileoff_t aligned_end_fsb = roundup_64(end_fsb, align);
183 xfs_iext_last(ifp, &icur);
184 if (!xfs_iext_get_extent(ifp, &icur, &irec) ||
185 aligned_end_fsb >= irec.br_startoff + irec.br_blockcount)
186 return aligned_end_fsb;
193 xfs_iomap_write_direct(
194 struct xfs_inode *ip,
195 xfs_fileoff_t offset_fsb,
196 xfs_fileoff_t count_fsb,
198 struct xfs_bmbt_irec *imap)
200 struct xfs_mount *mp = ip->i_mount;
201 struct xfs_trans *tp;
202 xfs_filblks_t resaligned;
204 unsigned int dblocks, rblocks;
207 int bmapi_flags = XFS_BMAPI_PREALLOC;
208 int nr_exts = XFS_IEXT_ADD_NOSPLIT_CNT;
210 ASSERT(count_fsb > 0);
212 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb,
213 xfs_get_extsz_hint(ip));
214 if (unlikely(XFS_IS_REALTIME_INODE(ip))) {
215 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
216 rblocks = resaligned;
218 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
222 error = xfs_qm_dqattach(ip);
227 * For DAX, we do not allocate unwritten extents, but instead we zero
228 * the block before we commit the transaction. Ideally we'd like to do
229 * this outside the transaction context, but if we commit and then crash
230 * we may not have zeroed the blocks and this will be exposed on
231 * recovery of the allocation. Hence we must zero before commit.
233 * Further, if we are mapping unwritten extents here, we need to zero
234 * and convert them to written so that we don't need an unwritten extent
235 * callback for DAX. This also means that we need to be able to dip into
236 * the reserve block pool for bmbt block allocation if there is no space
237 * left but we need to do unwritten extent conversion.
239 if (flags & IOMAP_DAX) {
240 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
241 if (imap->br_state == XFS_EXT_UNWRITTEN) {
243 nr_exts = XFS_IEXT_WRITE_UNWRITTEN_CNT;
244 dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
248 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, dblocks,
249 rblocks, force, &tp);
253 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK, nr_exts);
255 error = xfs_iext_count_upgrade(tp, ip, nr_exts);
257 goto out_trans_cancel;
260 * From this point onwards we overwrite the imap pointer that the
264 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flags, 0,
267 goto out_trans_cancel;
270 * Complete the transaction
272 error = xfs_trans_commit(tp);
277 * Copy any maps to caller's array and return any error.
284 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
285 error = xfs_alert_fsblock_zero(ip, imap);
288 xfs_iunlock(ip, XFS_ILOCK_EXCL);
292 xfs_trans_cancel(tp);
297 xfs_quota_need_throttle(
298 struct xfs_inode *ip,
300 xfs_fsblock_t alloc_blocks)
302 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
304 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
307 /* no hi watermark, no throttle */
308 if (!dq->q_prealloc_hi_wmark)
311 /* under the lo watermark, no throttle */
312 if (dq->q_blk.reserved + alloc_blocks < dq->q_prealloc_lo_wmark)
319 xfs_quota_calc_throttle(
320 struct xfs_inode *ip,
322 xfs_fsblock_t *qblocks,
326 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
330 /* no dq, or over hi wmark, squash the prealloc completely */
331 if (!dq || dq->q_blk.reserved >= dq->q_prealloc_hi_wmark) {
337 freesp = dq->q_prealloc_hi_wmark - dq->q_blk.reserved;
338 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
340 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
342 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
346 if (freesp < *qfreesp)
349 /* only overwrite the throttle values if we are more aggressive */
350 if ((freesp >> shift) < (*qblocks >> *qshift)) {
357 * If we don't have a user specified preallocation size, dynamically increase
358 * the preallocation size as the size of the file grows. Cap the maximum size
359 * at a single extent or less if the filesystem is near full. The closer the
360 * filesystem is to being full, the smaller the maximum preallocation.
363 xfs_iomap_prealloc_size(
364 struct xfs_inode *ip,
368 struct xfs_iext_cursor *icur)
370 struct xfs_iext_cursor ncur = *icur;
371 struct xfs_bmbt_irec prev, got;
372 struct xfs_mount *mp = ip->i_mount;
373 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
374 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
376 xfs_fsblock_t qblocks;
377 xfs_fsblock_t alloc_blocks = 0;
383 * As an exception we don't do any preallocation at all if the file is
384 * smaller than the minimum preallocation and we are using the default
385 * dynamic preallocation scheme, as it is likely this is the only write
386 * to the file that is going to be done.
388 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_allocsize_blocks))
392 * Use the minimum preallocation size for small files or if we are
393 * writing right after a hole.
395 if (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
396 !xfs_iext_prev_extent(ifp, &ncur, &prev) ||
397 prev.br_startoff + prev.br_blockcount < offset_fsb)
398 return mp->m_allocsize_blocks;
401 * Take the size of the preceding data extents as the basis for the
402 * preallocation size. Note that we don't care if the previous extents
403 * are written or not.
405 plen = prev.br_blockcount;
406 while (xfs_iext_prev_extent(ifp, &ncur, &got)) {
407 if (plen > XFS_MAX_BMBT_EXTLEN / 2 ||
408 isnullstartblock(got.br_startblock) ||
409 got.br_startoff + got.br_blockcount != prev.br_startoff ||
410 got.br_startblock + got.br_blockcount != prev.br_startblock)
412 plen += got.br_blockcount;
417 * If the size of the extents is greater than half the maximum extent
418 * length, then use the current offset as the basis. This ensures that
419 * for large files the preallocation size always extends to
420 * XFS_BMBT_MAX_EXTLEN rather than falling short due to things like stripe
421 * unit/width alignment of real extents.
423 alloc_blocks = plen * 2;
424 if (alloc_blocks > XFS_MAX_BMBT_EXTLEN)
425 alloc_blocks = XFS_B_TO_FSB(mp, offset);
426 qblocks = alloc_blocks;
429 * XFS_BMBT_MAX_EXTLEN is not a power of two value but we round the prealloc
430 * down to the nearest power of two value after throttling. To prevent
431 * the round down from unconditionally reducing the maximum supported
432 * prealloc size, we round up first, apply appropriate throttling, round
433 * down and cap the value to XFS_BMBT_MAX_EXTLEN.
435 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(XFS_MAX_BMBT_EXTLEN),
438 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
439 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
441 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
443 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
445 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
447 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
452 * Check each quota to cap the prealloc size, provide a shift value to
453 * throttle with and adjust amount of available space.
455 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_USER, alloc_blocks))
456 xfs_quota_calc_throttle(ip, XFS_DQTYPE_USER, &qblocks, &qshift,
458 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_GROUP, alloc_blocks))
459 xfs_quota_calc_throttle(ip, XFS_DQTYPE_GROUP, &qblocks, &qshift,
461 if (xfs_quota_need_throttle(ip, XFS_DQTYPE_PROJ, alloc_blocks))
462 xfs_quota_calc_throttle(ip, XFS_DQTYPE_PROJ, &qblocks, &qshift,
466 * The final prealloc size is set to the minimum of free space available
467 * in each of the quotas and the overall filesystem.
469 * The shift throttle value is set to the maximum value as determined by
470 * the global low free space values and per-quota low free space values.
472 alloc_blocks = min(alloc_blocks, qblocks);
473 shift = max(shift, qshift);
476 alloc_blocks >>= shift;
478 * rounddown_pow_of_two() returns an undefined result if we pass in
482 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
483 if (alloc_blocks > XFS_MAX_BMBT_EXTLEN)
484 alloc_blocks = XFS_MAX_BMBT_EXTLEN;
487 * If we are still trying to allocate more space than is
488 * available, squash the prealloc hard. This can happen if we
489 * have a large file on a small filesystem and the above
490 * lowspace thresholds are smaller than XFS_BMBT_MAX_EXTLEN.
492 while (alloc_blocks && alloc_blocks >= freesp)
494 if (alloc_blocks < mp->m_allocsize_blocks)
495 alloc_blocks = mp->m_allocsize_blocks;
496 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
497 mp->m_allocsize_blocks);
502 xfs_iomap_write_unwritten(
508 xfs_mount_t *mp = ip->i_mount;
509 xfs_fileoff_t offset_fsb;
510 xfs_filblks_t count_fsb;
511 xfs_filblks_t numblks_fsb;
514 xfs_bmbt_irec_t imap;
515 struct inode *inode = VFS_I(ip);
520 trace_xfs_unwritten_convert(ip, offset, count);
522 offset_fsb = XFS_B_TO_FSBT(mp, offset);
523 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
524 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
527 * Reserve enough blocks in this transaction for two complete extent
528 * btree splits. We may be converting the middle part of an unwritten
529 * extent and in this case we will insert two new extents in the btree
530 * each of which could cause a full split.
532 * This reservation amount will be used in the first call to
533 * xfs_bmbt_split() to select an AG with enough space to satisfy the
534 * rest of the operation.
536 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
538 /* Attach dquots so that bmbt splits are accounted correctly. */
539 error = xfs_qm_dqattach(ip);
545 * Set up a transaction to convert the range of extents
546 * from unwritten to real. Do allocations in a loop until
547 * we have covered the range passed in.
549 * Note that we can't risk to recursing back into the filesystem
550 * here as we might be asked to write out the same inode that we
551 * complete here and might deadlock on the iolock.
553 error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks,
558 error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
559 XFS_IEXT_WRITE_UNWRITTEN_CNT);
561 error = xfs_iext_count_upgrade(tp, ip,
562 XFS_IEXT_WRITE_UNWRITTEN_CNT);
564 goto error_on_bmapi_transaction;
567 * Modify the unwritten extent state of the buffer.
570 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
571 XFS_BMAPI_CONVERT, resblks, &imap,
574 goto error_on_bmapi_transaction;
577 * Log the updated inode size as we go. We have to be careful
578 * to only log it up to the actual write offset if it is
579 * halfway into a block.
581 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
582 if (i_size > offset + count)
583 i_size = offset + count;
584 if (update_isize && i_size > i_size_read(inode))
585 i_size_write(inode, i_size);
586 i_size = xfs_new_eof(ip, i_size);
588 ip->i_disk_size = i_size;
589 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
592 error = xfs_trans_commit(tp);
593 xfs_iunlock(ip, XFS_ILOCK_EXCL);
597 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock)))
598 return xfs_alert_fsblock_zero(ip, &imap);
600 if ((numblks_fsb = imap.br_blockcount) == 0) {
602 * The numblks_fsb value should always get
603 * smaller, otherwise the loop is stuck.
605 ASSERT(imap.br_blockcount);
608 offset_fsb += numblks_fsb;
609 count_fsb -= numblks_fsb;
610 } while (count_fsb > 0);
614 error_on_bmapi_transaction:
615 xfs_trans_cancel(tp);
616 xfs_iunlock(ip, XFS_ILOCK_EXCL);
624 struct xfs_bmbt_irec *imap,
627 /* don't allocate blocks when just zeroing */
628 if (flags & IOMAP_ZERO)
631 imap->br_startblock == HOLESTARTBLOCK ||
632 imap->br_startblock == DELAYSTARTBLOCK)
634 /* we convert unwritten extents before copying the data for DAX */
635 if ((flags & IOMAP_DAX) && imap->br_state == XFS_EXT_UNWRITTEN)
642 struct xfs_inode *ip,
644 struct xfs_bmbt_irec *imap,
647 if (!xfs_is_cow_inode(ip))
650 /* when zeroing we don't have to COW holes or unwritten extents */
651 if (flags & IOMAP_ZERO) {
653 imap->br_startblock == HOLESTARTBLOCK ||
654 imap->br_state == XFS_EXT_UNWRITTEN)
663 struct xfs_inode *ip,
667 unsigned int mode = *lockmode;
668 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
671 * COW writes may allocate delalloc space or convert unwritten COW
672 * extents, so we need to make sure to take the lock exclusively here.
674 if (xfs_is_cow_inode(ip) && is_write)
675 mode = XFS_ILOCK_EXCL;
678 * Extents not yet cached requires exclusive access, don't block. This
679 * is an opencoded xfs_ilock_data_map_shared() call but with
680 * non-blocking behaviour.
682 if (xfs_need_iread_extents(&ip->i_df)) {
683 if (flags & IOMAP_NOWAIT)
685 mode = XFS_ILOCK_EXCL;
689 if (flags & IOMAP_NOWAIT) {
690 if (!xfs_ilock_nowait(ip, mode))
697 * The reflink iflag could have changed since the earlier unlocked
698 * check, so if we got ILOCK_SHARED for a write and but we're now a
699 * reflink inode we have to switch to ILOCK_EXCL and relock.
701 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
702 xfs_iunlock(ip, mode);
703 mode = XFS_ILOCK_EXCL;
712 * Check that the imap we are going to return to the caller spans the entire
713 * range that the caller requested for the IO.
717 struct xfs_bmbt_irec *imap,
718 xfs_fileoff_t offset_fsb,
719 xfs_fileoff_t end_fsb)
721 if (imap->br_startoff > offset_fsb)
723 if (imap->br_startoff + imap->br_blockcount < end_fsb)
729 xfs_direct_write_iomap_begin(
735 struct iomap *srcmap)
737 struct xfs_inode *ip = XFS_I(inode);
738 struct xfs_mount *mp = ip->i_mount;
739 struct xfs_bmbt_irec imap, cmap;
740 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
741 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
742 int nimaps = 1, error = 0;
745 unsigned int lockmode = XFS_ILOCK_SHARED;
747 ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO));
749 if (xfs_is_shutdown(mp))
753 * Writes that span EOF might trigger an IO size update on completion,
754 * so consider them to be dirty for the purposes of O_DSYNC even if
755 * there is no other metadata changes pending or have been made here.
757 if (offset + length > i_size_read(inode))
758 iomap_flags |= IOMAP_F_DIRTY;
760 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
764 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
769 if (imap_needs_cow(ip, flags, &imap, nimaps)) {
771 if (flags & IOMAP_NOWAIT)
774 /* may drop and re-acquire the ilock */
775 error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared,
777 (flags & IOMAP_DIRECT) || IS_DAX(inode));
782 end_fsb = imap.br_startoff + imap.br_blockcount;
783 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
786 if (imap_needs_alloc(inode, flags, &imap, nimaps))
787 goto allocate_blocks;
790 * NOWAIT and OVERWRITE I/O needs to span the entire requested I/O with
791 * a single map so that we avoid partial IO failures due to the rest of
792 * the I/O range not covered by this map triggering an EAGAIN condition
793 * when it is subsequently mapped and aborting the I/O.
795 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) {
797 if (!imap_spans_range(&imap, offset_fsb, end_fsb))
802 * For overwrite only I/O, we cannot convert unwritten extents without
803 * requiring sub-block zeroing. This can only be done under an
804 * exclusive IOLOCK, hence return -EAGAIN if this is not a written
805 * extent to tell the caller to try again.
807 if (flags & IOMAP_OVERWRITE_ONLY) {
809 if (imap.br_state != XFS_EXT_NORM &&
810 ((offset | length) & mp->m_blockmask))
814 xfs_iunlock(ip, lockmode);
815 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
816 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags);
820 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY))
824 * We cap the maximum length we map to a sane size to keep the chunks
825 * of work done where somewhat symmetric with the work writeback does.
826 * This is a completely arbitrary number pulled out of thin air as a
827 * best guess for initial testing.
829 * Note that the values needs to be less than 32-bits wide until the
830 * lower level functions are updated.
832 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
833 end_fsb = xfs_iomap_end_fsb(mp, offset, length);
835 if (offset + length > XFS_ISIZE(ip))
836 end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
837 else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
838 end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount);
839 xfs_iunlock(ip, lockmode);
841 error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb,
846 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
847 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
848 iomap_flags | IOMAP_F_NEW);
851 xfs_iunlock(ip, lockmode);
852 length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
853 trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
854 if (imap.br_startblock != HOLESTARTBLOCK) {
855 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0);
859 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED);
863 xfs_iunlock(ip, lockmode);
867 const struct iomap_ops xfs_direct_write_iomap_ops = {
868 .iomap_begin = xfs_direct_write_iomap_begin,
872 xfs_dax_write_iomap_end(
880 struct xfs_inode *ip = XFS_I(inode);
882 if (!xfs_is_cow_inode(ip))
886 xfs_reflink_cancel_cow_range(ip, pos, length, true);
890 return xfs_reflink_end_cow(ip, pos, written);
893 const struct iomap_ops xfs_dax_write_iomap_ops = {
894 .iomap_begin = xfs_direct_write_iomap_begin,
895 .iomap_end = xfs_dax_write_iomap_end,
899 xfs_buffered_write_iomap_begin(
905 struct iomap *srcmap)
907 struct xfs_inode *ip = XFS_I(inode);
908 struct xfs_mount *mp = ip->i_mount;
909 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
910 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count);
911 struct xfs_bmbt_irec imap, cmap;
912 struct xfs_iext_cursor icur, ccur;
913 xfs_fsblock_t prealloc_blocks = 0;
914 bool eof = false, cow_eof = false, shared = false;
915 int allocfork = XFS_DATA_FORK;
917 unsigned int lockmode = XFS_ILOCK_EXCL;
919 if (xfs_is_shutdown(mp))
922 /* we can't use delayed allocations when using extent size hints */
923 if (xfs_get_extsz_hint(ip))
924 return xfs_direct_write_iomap_begin(inode, offset, count,
925 flags, iomap, srcmap);
927 ASSERT(!XFS_IS_REALTIME_INODE(ip));
929 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
933 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) ||
934 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
935 error = -EFSCORRUPTED;
939 XFS_STATS_INC(mp, xs_blk_mapw);
941 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
946 * Search the data fork first to look up our source mapping. We
947 * always need the data fork map, as we have to return it to the
948 * iomap code so that the higher level write code can read data in to
949 * perform read-modify-write cycles for unaligned writes.
951 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
953 imap.br_startoff = end_fsb; /* fake hole until the end */
955 /* We never need to allocate blocks for zeroing a hole. */
956 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
957 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
962 * Search the COW fork extent list even if we did not find a data fork
963 * extent. This serves two purposes: first this implements the
964 * speculative preallocation using cowextsize, so that we also unshare
965 * block adjacent to shared blocks instead of just the shared blocks
966 * themselves. Second the lookup in the extent list is generally faster
967 * than going out to the shared extent tree.
969 if (xfs_is_cow_inode(ip)) {
971 ASSERT(!xfs_is_reflink_inode(ip));
972 xfs_ifork_init_cow(ip);
974 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
976 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
977 trace_xfs_reflink_cow_found(ip, &cmap);
982 if (imap.br_startoff <= offset_fsb) {
984 * For reflink files we may need a delalloc reservation when
985 * overwriting shared extents. This includes zeroing of
986 * existing extents that contain data.
988 if (!xfs_is_cow_inode(ip) ||
989 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
990 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
995 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
997 /* Trim the mapping to the nearest shared extent boundary. */
998 error = xfs_bmap_trim_cow(ip, &imap, &shared);
1002 /* Not shared? Just report the (potentially capped) extent. */
1004 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
1010 * Fork all the shared blocks from our write offset until the
1011 * end of the extent.
1013 allocfork = XFS_COW_FORK;
1014 end_fsb = imap.br_startoff + imap.br_blockcount;
1017 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
1018 * pages to keep the chunks of work done where somewhat
1019 * symmetric with the work writeback does. This is a completely
1020 * arbitrary number pulled out of thin air.
1022 * Note that the values needs to be less than 32-bits wide until
1023 * the lower level functions are updated.
1025 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
1026 end_fsb = xfs_iomap_end_fsb(mp, offset, count);
1028 if (xfs_is_always_cow_inode(ip))
1029 allocfork = XFS_COW_FORK;
1032 error = xfs_qm_dqattach_locked(ip, false);
1036 if (eof && offset + count > XFS_ISIZE(ip)) {
1038 * Determine the initial size of the preallocation.
1039 * We clean up any extra preallocation when the file is closed.
1041 if (xfs_has_allocsize(mp))
1042 prealloc_blocks = mp->m_allocsize_blocks;
1044 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork,
1045 offset, count, &icur);
1046 if (prealloc_blocks) {
1048 xfs_off_t end_offset;
1049 xfs_fileoff_t p_end_fsb;
1051 end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1);
1052 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
1055 align = xfs_eof_alignment(ip);
1057 p_end_fsb = roundup_64(p_end_fsb, align);
1059 p_end_fsb = min(p_end_fsb,
1060 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1061 ASSERT(p_end_fsb > offset_fsb);
1062 prealloc_blocks = p_end_fsb - end_fsb;
1067 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
1068 end_fsb - offset_fsb, prealloc_blocks,
1069 allocfork == XFS_DATA_FORK ? &imap : &cmap,
1070 allocfork == XFS_DATA_FORK ? &icur : &ccur,
1071 allocfork == XFS_DATA_FORK ? eof : cow_eof);
1077 /* retry without any preallocation */
1078 trace_xfs_delalloc_enospc(ip, offset, count);
1079 if (prealloc_blocks) {
1080 prealloc_blocks = 0;
1088 if (allocfork == XFS_COW_FORK) {
1089 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
1094 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
1095 * them out if the write happens to fail.
1097 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1098 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
1099 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_NEW);
1102 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1103 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1106 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1107 if (imap.br_startoff <= offset_fsb) {
1108 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0);
1111 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
1115 xfs_trim_extent(&cmap, offset_fsb, imap.br_startoff - offset_fsb);
1116 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, 0);
1119 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1124 xfs_buffered_write_iomap_end(
1125 struct inode *inode,
1130 struct iomap *iomap)
1132 struct xfs_inode *ip = XFS_I(inode);
1133 struct xfs_mount *mp = ip->i_mount;
1134 xfs_fileoff_t start_fsb;
1135 xfs_fileoff_t end_fsb;
1138 if (iomap->type != IOMAP_DELALLOC)
1142 * Behave as if the write failed if drop writes is enabled. Set the NEW
1143 * flag to force delalloc cleanup.
1145 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
1146 iomap->flags |= IOMAP_F_NEW;
1151 * start_fsb refers to the first unused block after a short write. If
1152 * nothing was written, round offset down to point at the first block in
1155 if (unlikely(!written))
1156 start_fsb = XFS_B_TO_FSBT(mp, offset);
1158 start_fsb = XFS_B_TO_FSB(mp, offset + written);
1159 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1162 * Trim delalloc blocks if they were allocated by this write and we
1163 * didn't manage to write the whole range.
1165 * We don't need to care about racing delalloc as we hold i_mutex
1166 * across the reserve/allocate/unreserve calls. If there are delalloc
1167 * blocks in the range, they are ours.
1169 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
1170 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1171 XFS_FSB_TO_B(mp, end_fsb) - 1);
1173 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1174 end_fsb - start_fsb);
1175 if (error && !xfs_is_shutdown(mp)) {
1176 xfs_alert(mp, "%s: unable to clean up ino %lld",
1177 __func__, ip->i_ino);
1185 const struct iomap_ops xfs_buffered_write_iomap_ops = {
1186 .iomap_begin = xfs_buffered_write_iomap_begin,
1187 .iomap_end = xfs_buffered_write_iomap_end,
1191 xfs_read_iomap_begin(
1192 struct inode *inode,
1196 struct iomap *iomap,
1197 struct iomap *srcmap)
1199 struct xfs_inode *ip = XFS_I(inode);
1200 struct xfs_mount *mp = ip->i_mount;
1201 struct xfs_bmbt_irec imap;
1202 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1203 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1204 int nimaps = 1, error = 0;
1205 bool shared = false;
1206 unsigned int lockmode = XFS_ILOCK_SHARED;
1208 ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
1210 if (xfs_is_shutdown(mp))
1213 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1216 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1218 if (!error && (flags & IOMAP_REPORT))
1219 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
1220 xfs_iunlock(ip, lockmode);
1224 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1225 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
1226 shared ? IOMAP_F_SHARED : 0);
1229 const struct iomap_ops xfs_read_iomap_ops = {
1230 .iomap_begin = xfs_read_iomap_begin,
1234 xfs_seek_iomap_begin(
1235 struct inode *inode,
1239 struct iomap *iomap,
1240 struct iomap *srcmap)
1242 struct xfs_inode *ip = XFS_I(inode);
1243 struct xfs_mount *mp = ip->i_mount;
1244 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1245 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1246 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1247 struct xfs_iext_cursor icur;
1248 struct xfs_bmbt_irec imap, cmap;
1252 if (xfs_is_shutdown(mp))
1255 lockmode = xfs_ilock_data_map_shared(ip);
1256 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1260 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1262 * If we found a data extent we are done.
1264 if (imap.br_startoff <= offset_fsb)
1266 data_fsb = imap.br_startoff;
1269 * Fake a hole until the end of the file.
1271 data_fsb = xfs_iomap_end_fsb(mp, offset, length);
1275 * If a COW fork extent covers the hole, report it - capped to the next
1278 if (xfs_inode_has_cow_data(ip) &&
1279 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1280 cow_fsb = cmap.br_startoff;
1281 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1282 if (data_fsb < cow_fsb + cmap.br_blockcount)
1283 end_fsb = min(end_fsb, data_fsb);
1284 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1285 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
1288 * This is a COW extent, so we must probe the page cache
1289 * because there could be dirty page cache being backed
1292 iomap->type = IOMAP_UNWRITTEN;
1297 * Else report a hole, capped to the next found data or COW extent.
1299 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1300 imap.br_blockcount = cow_fsb - offset_fsb;
1302 imap.br_blockcount = data_fsb - offset_fsb;
1303 imap.br_startoff = offset_fsb;
1304 imap.br_startblock = HOLESTARTBLOCK;
1305 imap.br_state = XFS_EXT_NORM;
1307 xfs_trim_extent(&imap, offset_fsb, end_fsb);
1308 error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1310 xfs_iunlock(ip, lockmode);
1314 const struct iomap_ops xfs_seek_iomap_ops = {
1315 .iomap_begin = xfs_seek_iomap_begin,
1319 xfs_xattr_iomap_begin(
1320 struct inode *inode,
1324 struct iomap *iomap,
1325 struct iomap *srcmap)
1327 struct xfs_inode *ip = XFS_I(inode);
1328 struct xfs_mount *mp = ip->i_mount;
1329 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1330 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1331 struct xfs_bmbt_irec imap;
1332 int nimaps = 1, error = 0;
1335 if (xfs_is_shutdown(mp))
1338 lockmode = xfs_ilock_attr_map_shared(ip);
1340 /* if there are no attribute fork or extents, return ENOENT */
1341 if (!xfs_inode_has_attr_fork(ip) || !ip->i_af.if_nextents) {
1346 ASSERT(ip->i_af.if_format != XFS_DINODE_FMT_LOCAL);
1347 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1348 &nimaps, XFS_BMAPI_ATTRFORK);
1350 xfs_iunlock(ip, lockmode);
1355 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1358 const struct iomap_ops xfs_xattr_iomap_ops = {
1359 .iomap_begin = xfs_xattr_iomap_begin,
1364 struct xfs_inode *ip,
1369 struct inode *inode = VFS_I(ip);
1372 return dax_zero_range(inode, pos, len, did_zero,
1373 &xfs_direct_write_iomap_ops);
1374 return iomap_zero_range(inode, pos, len, did_zero,
1375 &xfs_buffered_write_iomap_ops);
1380 struct xfs_inode *ip,
1384 struct inode *inode = VFS_I(ip);
1387 return dax_truncate_page(inode, pos, did_zero,
1388 &xfs_direct_write_iomap_ops);
1389 return iomap_truncate_page(inode, pos, did_zero,
1390 &xfs_buffered_write_iomap_ops);