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 mode = XFS_ILOCK_SHARED;
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;
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,
776 &lockmode, flags & IOMAP_DIRECT);
781 end_fsb = imap.br_startoff + imap.br_blockcount;
782 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
785 if (imap_needs_alloc(inode, flags, &imap, nimaps))
786 goto allocate_blocks;
789 * NOWAIT and OVERWRITE I/O needs to span the entire requested I/O with
790 * a single map so that we avoid partial IO failures due to the rest of
791 * the I/O range not covered by this map triggering an EAGAIN condition
792 * when it is subsequently mapped and aborting the I/O.
794 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY)) {
796 if (!imap_spans_range(&imap, offset_fsb, end_fsb))
801 * For overwrite only I/O, we cannot convert unwritten extents without
802 * requiring sub-block zeroing. This can only be done under an
803 * exclusive IOLOCK, hence return -EAGAIN if this is not a written
804 * extent to tell the caller to try again.
806 if (flags & IOMAP_OVERWRITE_ONLY) {
808 if (imap.br_state != XFS_EXT_NORM &&
809 ((offset | length) & mp->m_blockmask))
813 xfs_iunlock(ip, lockmode);
814 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
815 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, iomap_flags);
819 if (flags & (IOMAP_NOWAIT | IOMAP_OVERWRITE_ONLY))
823 * We cap the maximum length we map to a sane size to keep the chunks
824 * of work done where somewhat symmetric with the work writeback does.
825 * This is a completely arbitrary number pulled out of thin air as a
826 * best guess for initial testing.
828 * Note that the values needs to be less than 32-bits wide until the
829 * lower level functions are updated.
831 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
832 end_fsb = xfs_iomap_end_fsb(mp, offset, length);
834 if (offset + length > XFS_ISIZE(ip))
835 end_fsb = xfs_iomap_eof_align_last_fsb(ip, end_fsb);
836 else if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
837 end_fsb = min(end_fsb, imap.br_startoff + imap.br_blockcount);
838 xfs_iunlock(ip, lockmode);
840 error = xfs_iomap_write_direct(ip, offset_fsb, end_fsb - offset_fsb,
845 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
846 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
847 iomap_flags | IOMAP_F_NEW);
850 xfs_iunlock(ip, lockmode);
851 length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
852 trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
853 if (imap.br_startblock != HOLESTARTBLOCK) {
854 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0);
858 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, IOMAP_F_SHARED);
862 xfs_iunlock(ip, lockmode);
866 const struct iomap_ops xfs_direct_write_iomap_ops = {
867 .iomap_begin = xfs_direct_write_iomap_begin,
871 xfs_buffered_write_iomap_begin(
877 struct iomap *srcmap)
879 struct xfs_inode *ip = XFS_I(inode);
880 struct xfs_mount *mp = ip->i_mount;
881 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
882 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count);
883 struct xfs_bmbt_irec imap, cmap;
884 struct xfs_iext_cursor icur, ccur;
885 xfs_fsblock_t prealloc_blocks = 0;
886 bool eof = false, cow_eof = false, shared = false;
887 int allocfork = XFS_DATA_FORK;
890 if (xfs_is_shutdown(mp))
893 /* we can't use delayed allocations when using extent size hints */
894 if (xfs_get_extsz_hint(ip))
895 return xfs_direct_write_iomap_begin(inode, offset, count,
896 flags, iomap, srcmap);
898 ASSERT(!XFS_IS_REALTIME_INODE(ip));
900 xfs_ilock(ip, XFS_ILOCK_EXCL);
902 if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(&ip->i_df)) ||
903 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
904 error = -EFSCORRUPTED;
908 XFS_STATS_INC(mp, xs_blk_mapw);
910 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
915 * Search the data fork first to look up our source mapping. We
916 * always need the data fork map, as we have to return it to the
917 * iomap code so that the higher level write code can read data in to
918 * perform read-modify-write cycles for unaligned writes.
920 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
922 imap.br_startoff = end_fsb; /* fake hole until the end */
924 /* We never need to allocate blocks for zeroing a hole. */
925 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
926 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
931 * Search the COW fork extent list even if we did not find a data fork
932 * extent. This serves two purposes: first this implements the
933 * speculative preallocation using cowextsize, so that we also unshare
934 * block adjacent to shared blocks instead of just the shared blocks
935 * themselves. Second the lookup in the extent list is generally faster
936 * than going out to the shared extent tree.
938 if (xfs_is_cow_inode(ip)) {
940 ASSERT(!xfs_is_reflink_inode(ip));
941 xfs_ifork_init_cow(ip);
943 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
945 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
946 trace_xfs_reflink_cow_found(ip, &cmap);
951 if (imap.br_startoff <= offset_fsb) {
953 * For reflink files we may need a delalloc reservation when
954 * overwriting shared extents. This includes zeroing of
955 * existing extents that contain data.
957 if (!xfs_is_cow_inode(ip) ||
958 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
959 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
964 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
966 /* Trim the mapping to the nearest shared extent boundary. */
967 error = xfs_bmap_trim_cow(ip, &imap, &shared);
971 /* Not shared? Just report the (potentially capped) extent. */
973 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
979 * Fork all the shared blocks from our write offset until the
982 allocfork = XFS_COW_FORK;
983 end_fsb = imap.br_startoff + imap.br_blockcount;
986 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
987 * pages to keep the chunks of work done where somewhat
988 * symmetric with the work writeback does. This is a completely
989 * arbitrary number pulled out of thin air.
991 * Note that the values needs to be less than 32-bits wide until
992 * the lower level functions are updated.
994 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
995 end_fsb = xfs_iomap_end_fsb(mp, offset, count);
997 if (xfs_is_always_cow_inode(ip))
998 allocfork = XFS_COW_FORK;
1001 error = xfs_qm_dqattach_locked(ip, false);
1005 if (eof && offset + count > XFS_ISIZE(ip)) {
1007 * Determine the initial size of the preallocation.
1008 * We clean up any extra preallocation when the file is closed.
1010 if (xfs_has_allocsize(mp))
1011 prealloc_blocks = mp->m_allocsize_blocks;
1013 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork,
1014 offset, count, &icur);
1015 if (prealloc_blocks) {
1017 xfs_off_t end_offset;
1018 xfs_fileoff_t p_end_fsb;
1020 end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1);
1021 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
1024 align = xfs_eof_alignment(ip);
1026 p_end_fsb = roundup_64(p_end_fsb, align);
1028 p_end_fsb = min(p_end_fsb,
1029 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1030 ASSERT(p_end_fsb > offset_fsb);
1031 prealloc_blocks = p_end_fsb - end_fsb;
1036 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
1037 end_fsb - offset_fsb, prealloc_blocks,
1038 allocfork == XFS_DATA_FORK ? &imap : &cmap,
1039 allocfork == XFS_DATA_FORK ? &icur : &ccur,
1040 allocfork == XFS_DATA_FORK ? eof : cow_eof);
1046 /* retry without any preallocation */
1047 trace_xfs_delalloc_enospc(ip, offset, count);
1048 if (prealloc_blocks) {
1049 prealloc_blocks = 0;
1057 if (allocfork == XFS_COW_FORK) {
1058 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
1063 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
1064 * them out if the write happens to fail.
1066 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1067 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
1068 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, IOMAP_F_NEW);
1071 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1072 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1075 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1076 if (imap.br_startoff <= offset_fsb) {
1077 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, flags, 0);
1080 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
1084 xfs_trim_extent(&cmap, offset_fsb, imap.br_startoff - offset_fsb);
1085 return xfs_bmbt_to_iomap(ip, iomap, &cmap, flags, 0);
1088 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1093 xfs_buffered_write_iomap_end(
1094 struct inode *inode,
1099 struct iomap *iomap)
1101 struct xfs_inode *ip = XFS_I(inode);
1102 struct xfs_mount *mp = ip->i_mount;
1103 xfs_fileoff_t start_fsb;
1104 xfs_fileoff_t end_fsb;
1107 if (iomap->type != IOMAP_DELALLOC)
1111 * Behave as if the write failed if drop writes is enabled. Set the NEW
1112 * flag to force delalloc cleanup.
1114 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
1115 iomap->flags |= IOMAP_F_NEW;
1120 * start_fsb refers to the first unused block after a short write. If
1121 * nothing was written, round offset down to point at the first block in
1124 if (unlikely(!written))
1125 start_fsb = XFS_B_TO_FSBT(mp, offset);
1127 start_fsb = XFS_B_TO_FSB(mp, offset + written);
1128 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1131 * Trim delalloc blocks if they were allocated by this write and we
1132 * didn't manage to write the whole range.
1134 * We don't need to care about racing delalloc as we hold i_mutex
1135 * across the reserve/allocate/unreserve calls. If there are delalloc
1136 * blocks in the range, they are ours.
1138 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
1139 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1140 XFS_FSB_TO_B(mp, end_fsb) - 1);
1142 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1143 end_fsb - start_fsb);
1144 if (error && !xfs_is_shutdown(mp)) {
1145 xfs_alert(mp, "%s: unable to clean up ino %lld",
1146 __func__, ip->i_ino);
1154 const struct iomap_ops xfs_buffered_write_iomap_ops = {
1155 .iomap_begin = xfs_buffered_write_iomap_begin,
1156 .iomap_end = xfs_buffered_write_iomap_end,
1160 xfs_read_iomap_begin(
1161 struct inode *inode,
1165 struct iomap *iomap,
1166 struct iomap *srcmap)
1168 struct xfs_inode *ip = XFS_I(inode);
1169 struct xfs_mount *mp = ip->i_mount;
1170 struct xfs_bmbt_irec imap;
1171 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1172 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1173 int nimaps = 1, error = 0;
1174 bool shared = false;
1177 ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
1179 if (xfs_is_shutdown(mp))
1182 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1185 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1187 if (!error && (flags & IOMAP_REPORT))
1188 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
1189 xfs_iunlock(ip, lockmode);
1193 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1194 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
1195 shared ? IOMAP_F_SHARED : 0);
1198 const struct iomap_ops xfs_read_iomap_ops = {
1199 .iomap_begin = xfs_read_iomap_begin,
1203 xfs_seek_iomap_begin(
1204 struct inode *inode,
1208 struct iomap *iomap,
1209 struct iomap *srcmap)
1211 struct xfs_inode *ip = XFS_I(inode);
1212 struct xfs_mount *mp = ip->i_mount;
1213 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1214 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1215 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1216 struct xfs_iext_cursor icur;
1217 struct xfs_bmbt_irec imap, cmap;
1221 if (xfs_is_shutdown(mp))
1224 lockmode = xfs_ilock_data_map_shared(ip);
1225 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1229 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1231 * If we found a data extent we are done.
1233 if (imap.br_startoff <= offset_fsb)
1235 data_fsb = imap.br_startoff;
1238 * Fake a hole until the end of the file.
1240 data_fsb = xfs_iomap_end_fsb(mp, offset, length);
1244 * If a COW fork extent covers the hole, report it - capped to the next
1247 if (xfs_inode_has_cow_data(ip) &&
1248 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1249 cow_fsb = cmap.br_startoff;
1250 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1251 if (data_fsb < cow_fsb + cmap.br_blockcount)
1252 end_fsb = min(end_fsb, data_fsb);
1253 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1254 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, flags,
1257 * This is a COW extent, so we must probe the page cache
1258 * because there could be dirty page cache being backed
1261 iomap->type = IOMAP_UNWRITTEN;
1266 * Else report a hole, capped to the next found data or COW extent.
1268 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1269 imap.br_blockcount = cow_fsb - offset_fsb;
1271 imap.br_blockcount = data_fsb - offset_fsb;
1272 imap.br_startoff = offset_fsb;
1273 imap.br_startblock = HOLESTARTBLOCK;
1274 imap.br_state = XFS_EXT_NORM;
1276 xfs_trim_extent(&imap, offset_fsb, end_fsb);
1277 error = xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1279 xfs_iunlock(ip, lockmode);
1283 const struct iomap_ops xfs_seek_iomap_ops = {
1284 .iomap_begin = xfs_seek_iomap_begin,
1288 xfs_xattr_iomap_begin(
1289 struct inode *inode,
1293 struct iomap *iomap,
1294 struct iomap *srcmap)
1296 struct xfs_inode *ip = XFS_I(inode);
1297 struct xfs_mount *mp = ip->i_mount;
1298 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1299 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1300 struct xfs_bmbt_irec imap;
1301 int nimaps = 1, error = 0;
1304 if (xfs_is_shutdown(mp))
1307 lockmode = xfs_ilock_attr_map_shared(ip);
1309 /* if there are no attribute fork or extents, return ENOENT */
1310 if (!XFS_IFORK_Q(ip) || !ip->i_afp->if_nextents) {
1315 ASSERT(ip->i_afp->if_format != XFS_DINODE_FMT_LOCAL);
1316 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1317 &nimaps, XFS_BMAPI_ATTRFORK);
1319 xfs_iunlock(ip, lockmode);
1324 return xfs_bmbt_to_iomap(ip, iomap, &imap, flags, 0);
1327 const struct iomap_ops xfs_xattr_iomap_ops = {
1328 .iomap_begin = xfs_xattr_iomap_begin,
1333 struct xfs_inode *ip,
1338 struct inode *inode = VFS_I(ip);
1341 return dax_zero_range(inode, pos, len, did_zero,
1342 &xfs_direct_write_iomap_ops);
1343 return iomap_zero_range(inode, pos, len, did_zero,
1344 &xfs_buffered_write_iomap_ops);
1349 struct xfs_inode *ip,
1353 struct inode *inode = VFS_I(ip);
1356 return dax_truncate_page(inode, pos, did_zero,
1357 &xfs_direct_write_iomap_ops);
1358 return iomap_truncate_page(inode, pos, did_zero,
1359 &xfs_buffered_write_iomap_ops);