1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2016-2018 Christoph Hellwig.
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_btree.h"
16 #include "xfs_bmap_btree.h"
18 #include "xfs_bmap_util.h"
19 #include "xfs_errortag.h"
20 #include "xfs_error.h"
21 #include "xfs_trans.h"
22 #include "xfs_trans_space.h"
23 #include "xfs_inode_item.h"
24 #include "xfs_iomap.h"
25 #include "xfs_trace.h"
26 #include "xfs_quota.h"
27 #include "xfs_dquot_item.h"
28 #include "xfs_dquot.h"
29 #include "xfs_reflink.h"
32 #define XFS_ALLOC_ALIGN(mp, off) \
33 (((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
36 xfs_alert_fsblock_zero(
38 xfs_bmbt_irec_t *imap)
40 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
41 "Access to block zero in inode %llu "
42 "start_block: %llx start_off: %llx "
43 "blkcnt: %llx extent-state: %x",
44 (unsigned long long)ip->i_ino,
45 (unsigned long long)imap->br_startblock,
46 (unsigned long long)imap->br_startoff,
47 (unsigned long long)imap->br_blockcount,
56 struct xfs_bmbt_irec *imap,
59 struct xfs_mount *mp = ip->i_mount;
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 (imap->br_state == XFS_EXT_UNWRITTEN)
75 iomap->type = IOMAP_UNWRITTEN;
77 iomap->type = IOMAP_MAPPED;
79 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
80 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
81 iomap->bdev = target->bt_bdev;
82 iomap->dax_dev = target->bt_daxdev;
85 if (xfs_ipincount(ip) &&
86 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
87 iomap->flags |= IOMAP_F_DIRTY;
95 xfs_fileoff_t offset_fsb,
96 xfs_fileoff_t end_fsb)
98 struct xfs_buftarg *target = xfs_inode_buftarg(ip);
100 iomap->addr = IOMAP_NULL_ADDR;
101 iomap->type = IOMAP_HOLE;
102 iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
103 iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
104 iomap->bdev = target->bt_bdev;
105 iomap->dax_dev = target->bt_daxdev;
108 static inline xfs_fileoff_t
110 struct xfs_mount *mp,
114 ASSERT(offset <= mp->m_super->s_maxbytes);
115 return min(XFS_B_TO_FSB(mp, offset + count),
116 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
121 struct xfs_inode *ip)
123 struct xfs_mount *mp = ip->i_mount;
124 xfs_extlen_t align = 0;
126 if (!XFS_IS_REALTIME_INODE(ip)) {
128 * Round up the allocation request to a stripe unit
129 * (m_dalign) boundary if the file size is >= stripe unit
130 * size, and we are allocating past the allocation eof.
132 * If mounted with the "-o swalloc" option the alignment is
133 * increased from the strip unit size to the stripe width.
135 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
136 align = mp->m_swidth;
137 else if (mp->m_dalign)
138 align = mp->m_dalign;
140 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
148 * Check if last_fsb is outside the last extent, and if so grow it to the next
149 * stripe unit boundary.
152 xfs_iomap_eof_align_last_fsb(
153 struct xfs_inode *ip,
154 xfs_fileoff_t end_fsb)
156 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
157 xfs_extlen_t extsz = xfs_get_extsz_hint(ip);
158 xfs_extlen_t align = xfs_eof_alignment(ip);
159 struct xfs_bmbt_irec irec;
160 struct xfs_iext_cursor icur;
162 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
165 * Always round up the allocation request to the extent hint boundary.
169 align = roundup_64(align, extsz);
175 xfs_fileoff_t aligned_end_fsb = roundup_64(end_fsb, align);
177 xfs_iext_last(ifp, &icur);
178 if (!xfs_iext_get_extent(ifp, &icur, &irec) ||
179 aligned_end_fsb >= irec.br_startoff + irec.br_blockcount)
180 return aligned_end_fsb;
187 xfs_iomap_write_direct(
191 xfs_bmbt_irec_t *imap,
194 xfs_mount_t *mp = ip->i_mount;
195 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
196 xfs_fileoff_t last_fsb = xfs_iomap_end_fsb(mp, offset, count);
197 xfs_filblks_t count_fsb, resaligned;
203 uint qblocks, resblks, resrtextents;
206 int bmapi_flags = XFS_BMAPI_PREALLOC;
209 rt = XFS_IS_REALTIME_INODE(ip);
210 extsz = xfs_get_extsz_hint(ip);
211 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
213 ASSERT(xfs_isilocked(ip, lockmode));
215 if (offset + count > XFS_ISIZE(ip)) {
216 last_fsb = xfs_iomap_eof_align_last_fsb(ip, last_fsb);
218 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
219 last_fsb = min(last_fsb, (xfs_fileoff_t)
220 imap->br_blockcount +
223 count_fsb = last_fsb - offset_fsb;
224 ASSERT(count_fsb > 0);
225 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
228 resrtextents = qblocks = resaligned;
229 resrtextents /= mp->m_sb.sb_rextsize;
230 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
231 quota_flag = XFS_QMOPT_RES_RTBLKS;
234 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
235 quota_flag = XFS_QMOPT_RES_REGBLKS;
239 * Drop the shared lock acquired by the caller, attach the dquot if
240 * necessary and move on to transaction setup.
242 xfs_iunlock(ip, lockmode);
243 error = xfs_qm_dqattach(ip);
248 * For DAX, we do not allocate unwritten extents, but instead we zero
249 * the block before we commit the transaction. Ideally we'd like to do
250 * this outside the transaction context, but if we commit and then crash
251 * we may not have zeroed the blocks and this will be exposed on
252 * recovery of the allocation. Hence we must zero before commit.
254 * Further, if we are mapping unwritten extents here, we need to zero
255 * and convert them to written so that we don't need an unwritten extent
256 * callback for DAX. This also means that we need to be able to dip into
257 * the reserve block pool for bmbt block allocation if there is no space
258 * left but we need to do unwritten extent conversion.
260 if (IS_DAX(VFS_I(ip))) {
261 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
262 if (imap->br_state == XFS_EXT_UNWRITTEN) {
263 tflags |= XFS_TRANS_RESERVE;
264 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
267 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
272 lockmode = XFS_ILOCK_EXCL;
273 xfs_ilock(ip, lockmode);
275 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
277 goto out_trans_cancel;
279 xfs_trans_ijoin(tp, ip, 0);
282 * From this point onwards we overwrite the imap pointer that the
286 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb, bmapi_flags, 0,
292 * Complete the transaction
294 error = xfs_trans_commit(tp);
299 * Copy any maps to caller's array and return any error.
306 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
307 error = xfs_alert_fsblock_zero(ip, imap);
310 xfs_iunlock(ip, lockmode);
314 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
316 xfs_trans_cancel(tp);
321 xfs_quota_need_throttle(
322 struct xfs_inode *ip,
324 xfs_fsblock_t alloc_blocks)
326 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
328 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
331 /* no hi watermark, no throttle */
332 if (!dq->q_prealloc_hi_wmark)
335 /* under the lo watermark, no throttle */
336 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
343 xfs_quota_calc_throttle(
344 struct xfs_inode *ip,
346 xfs_fsblock_t *qblocks,
352 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
354 /* no dq, or over hi wmark, squash the prealloc completely */
355 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
361 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
362 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
364 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
366 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
370 if (freesp < *qfreesp)
373 /* only overwrite the throttle values if we are more aggressive */
374 if ((freesp >> shift) < (*qblocks >> *qshift)) {
381 * If we are doing a write at the end of the file and there are no allocations
382 * past this one, then extend the allocation out to the file system's write
385 * If we don't have a user specified preallocation size, dynamically increase
386 * the preallocation size as the size of the file grows. Cap the maximum size
387 * at a single extent or less if the filesystem is near full. The closer the
388 * filesystem is to full, the smaller the maximum prealocation.
390 * As an exception we don't do any preallocation at all if the file is smaller
391 * than the minimum preallocation and we are using the default dynamic
392 * preallocation scheme, as it is likely this is the only write to the file that
393 * is going to be done.
395 * We clean up any extra space left over when the file is closed in
399 xfs_iomap_prealloc_size(
400 struct xfs_inode *ip,
404 struct xfs_iext_cursor *icur)
406 struct xfs_mount *mp = ip->i_mount;
407 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
408 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
409 struct xfs_bmbt_irec prev;
412 xfs_fsblock_t qblocks;
414 xfs_fsblock_t alloc_blocks = 0;
416 if (offset + count <= XFS_ISIZE(ip))
419 if (!(mp->m_flags & XFS_MOUNT_ALLOCSIZE) &&
420 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_allocsize_blocks)))
424 * If an explicit allocsize is set, the file is small, or we
425 * are writing behind a hole, then use the minimum prealloc:
427 if ((mp->m_flags & XFS_MOUNT_ALLOCSIZE) ||
428 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
429 !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
430 prev.br_startoff + prev.br_blockcount < offset_fsb)
431 return mp->m_allocsize_blocks;
434 * Determine the initial size of the preallocation. We are beyond the
435 * current EOF here, but we need to take into account whether this is
436 * a sparse write or an extending write when determining the
437 * preallocation size. Hence we need to look up the extent that ends
438 * at the current write offset and use the result to determine the
439 * preallocation size.
441 * If the extent is a hole, then preallocation is essentially disabled.
442 * Otherwise we take the size of the preceding data extent as the basis
443 * for the preallocation size. If the size of the extent is greater than
444 * half the maximum extent length, then use the current offset as the
445 * basis. This ensures that for large files the preallocation size
446 * always extends to MAXEXTLEN rather than falling short due to things
447 * like stripe unit/width alignment of real extents.
449 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
450 alloc_blocks = prev.br_blockcount << 1;
452 alloc_blocks = XFS_B_TO_FSB(mp, offset);
455 qblocks = alloc_blocks;
458 * MAXEXTLEN is not a power of two value but we round the prealloc down
459 * to the nearest power of two value after throttling. To prevent the
460 * round down from unconditionally reducing the maximum supported prealloc
461 * size, we round up first, apply appropriate throttling, round down and
462 * cap the value to MAXEXTLEN.
464 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
467 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
468 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
470 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
472 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
474 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
476 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
481 * Check each quota to cap the prealloc size, provide a shift value to
482 * throttle with and adjust amount of available space.
484 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
485 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
487 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
488 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
490 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
491 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
495 * The final prealloc size is set to the minimum of free space available
496 * in each of the quotas and the overall filesystem.
498 * The shift throttle value is set to the maximum value as determined by
499 * the global low free space values and per-quota low free space values.
501 alloc_blocks = min(alloc_blocks, qblocks);
502 shift = max(shift, qshift);
505 alloc_blocks >>= shift;
507 * rounddown_pow_of_two() returns an undefined result if we pass in
511 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
512 if (alloc_blocks > MAXEXTLEN)
513 alloc_blocks = MAXEXTLEN;
516 * If we are still trying to allocate more space than is
517 * available, squash the prealloc hard. This can happen if we
518 * have a large file on a small filesystem and the above
519 * lowspace thresholds are smaller than MAXEXTLEN.
521 while (alloc_blocks && alloc_blocks >= freesp)
524 if (alloc_blocks < mp->m_allocsize_blocks)
525 alloc_blocks = mp->m_allocsize_blocks;
526 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
527 mp->m_allocsize_blocks);
532 xfs_iomap_write_unwritten(
538 xfs_mount_t *mp = ip->i_mount;
539 xfs_fileoff_t offset_fsb;
540 xfs_filblks_t count_fsb;
541 xfs_filblks_t numblks_fsb;
544 xfs_bmbt_irec_t imap;
545 struct inode *inode = VFS_I(ip);
550 trace_xfs_unwritten_convert(ip, offset, count);
552 offset_fsb = XFS_B_TO_FSBT(mp, offset);
553 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
554 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
557 * Reserve enough blocks in this transaction for two complete extent
558 * btree splits. We may be converting the middle part of an unwritten
559 * extent and in this case we will insert two new extents in the btree
560 * each of which could cause a full split.
562 * This reservation amount will be used in the first call to
563 * xfs_bmbt_split() to select an AG with enough space to satisfy the
564 * rest of the operation.
566 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
570 * Set up a transaction to convert the range of extents
571 * from unwritten to real. Do allocations in a loop until
572 * we have covered the range passed in.
574 * Note that we can't risk to recursing back into the filesystem
575 * here as we might be asked to write out the same inode that we
576 * complete here and might deadlock on the iolock.
578 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
579 XFS_TRANS_RESERVE, &tp);
583 xfs_ilock(ip, XFS_ILOCK_EXCL);
584 xfs_trans_ijoin(tp, ip, 0);
587 * Modify the unwritten extent state of the buffer.
590 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
591 XFS_BMAPI_CONVERT, resblks, &imap,
594 goto error_on_bmapi_transaction;
597 * Log the updated inode size as we go. We have to be careful
598 * to only log it up to the actual write offset if it is
599 * halfway into a block.
601 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
602 if (i_size > offset + count)
603 i_size = offset + count;
604 if (update_isize && i_size > i_size_read(inode))
605 i_size_write(inode, i_size);
606 i_size = xfs_new_eof(ip, i_size);
608 ip->i_d.di_size = i_size;
609 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
612 error = xfs_trans_commit(tp);
613 xfs_iunlock(ip, XFS_ILOCK_EXCL);
617 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock)))
618 return xfs_alert_fsblock_zero(ip, &imap);
620 if ((numblks_fsb = imap.br_blockcount) == 0) {
622 * The numblks_fsb value should always get
623 * smaller, otherwise the loop is stuck.
625 ASSERT(imap.br_blockcount);
628 offset_fsb += numblks_fsb;
629 count_fsb -= numblks_fsb;
630 } while (count_fsb > 0);
634 error_on_bmapi_transaction:
635 xfs_trans_cancel(tp);
636 xfs_iunlock(ip, XFS_ILOCK_EXCL);
644 struct xfs_bmbt_irec *imap,
647 /* don't allocate blocks when just zeroing */
648 if (flags & IOMAP_ZERO)
651 imap->br_startblock == HOLESTARTBLOCK ||
652 imap->br_startblock == DELAYSTARTBLOCK)
654 /* we convert unwritten extents before copying the data for DAX */
655 if (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN)
662 struct xfs_inode *ip,
664 struct xfs_bmbt_irec *imap,
667 if (!xfs_is_cow_inode(ip))
670 /* when zeroing we don't have to COW holes or unwritten extents */
671 if (flags & IOMAP_ZERO) {
673 imap->br_startblock == HOLESTARTBLOCK ||
674 imap->br_state == XFS_EXT_UNWRITTEN)
683 struct xfs_inode *ip,
687 unsigned mode = XFS_ILOCK_SHARED;
688 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
691 * COW writes may allocate delalloc space or convert unwritten COW
692 * extents, so we need to make sure to take the lock exclusively here.
694 if (xfs_is_cow_inode(ip) && is_write)
695 mode = XFS_ILOCK_EXCL;
698 * Extents not yet cached requires exclusive access, don't block. This
699 * is an opencoded xfs_ilock_data_map_shared() call but with
700 * non-blocking behaviour.
702 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
703 if (flags & IOMAP_NOWAIT)
705 mode = XFS_ILOCK_EXCL;
709 if (flags & IOMAP_NOWAIT) {
710 if (!xfs_ilock_nowait(ip, mode))
717 * The reflink iflag could have changed since the earlier unlocked
718 * check, so if we got ILOCK_SHARED for a write and but we're now a
719 * reflink inode we have to switch to ILOCK_EXCL and relock.
721 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
722 xfs_iunlock(ip, mode);
723 mode = XFS_ILOCK_EXCL;
732 xfs_direct_write_iomap_begin(
738 struct iomap *srcmap)
740 struct xfs_inode *ip = XFS_I(inode);
741 struct xfs_mount *mp = ip->i_mount;
742 struct xfs_bmbt_irec imap, cmap;
743 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
744 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
745 int nimaps = 1, error = 0;
750 ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO));
752 if (XFS_FORCED_SHUTDOWN(mp))
756 * Writes that span EOF might trigger an IO size update on completion,
757 * so consider them to be dirty for the purposes of O_DSYNC even if
758 * there is no other metadata changes pending or have been made here.
760 if (offset + length > i_size_read(inode))
761 iomap_flags |= IOMAP_F_DIRTY;
763 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
767 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
772 if (imap_needs_cow(ip, flags, &imap, nimaps)) {
774 if (flags & IOMAP_NOWAIT)
777 /* may drop and re-acquire the ilock */
778 error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared,
779 &lockmode, flags & IOMAP_DIRECT);
784 end_fsb = imap.br_startoff + imap.br_blockcount;
785 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
788 if (imap_needs_alloc(inode, flags, &imap, nimaps))
789 goto allocate_blocks;
791 xfs_iunlock(ip, lockmode);
792 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
793 return xfs_bmbt_to_iomap(ip, iomap, &imap, iomap_flags);
797 if (flags & IOMAP_NOWAIT)
801 * We cap the maximum length we map to a sane size to keep the chunks
802 * of work done where somewhat symmetric with the work writeback does.
803 * This is a completely arbitrary number pulled out of thin air as a
804 * best guess for initial testing.
806 * Note that the values needs to be less than 32-bits wide until the
807 * lower level functions are updated.
809 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
812 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
815 if (lockmode == XFS_ILOCK_EXCL)
816 xfs_ilock_demote(ip, lockmode);
817 error = xfs_iomap_write_direct(ip, offset, length, &imap, nimaps);
821 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
822 return xfs_bmbt_to_iomap(ip, iomap, &imap, iomap_flags | IOMAP_F_NEW);
825 xfs_iunlock(ip, lockmode);
826 length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
827 trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
828 if (imap.br_startblock != HOLESTARTBLOCK) {
829 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, 0);
833 return xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
836 xfs_iunlock(ip, lockmode);
840 const struct iomap_ops xfs_direct_write_iomap_ops = {
841 .iomap_begin = xfs_direct_write_iomap_begin,
845 xfs_buffered_write_iomap_begin(
851 struct iomap *srcmap)
853 struct xfs_inode *ip = XFS_I(inode);
854 struct xfs_mount *mp = ip->i_mount;
855 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
856 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count);
857 struct xfs_bmbt_irec imap, cmap;
858 struct xfs_iext_cursor icur, ccur;
859 xfs_fsblock_t prealloc_blocks = 0;
860 bool eof = false, cow_eof = false, shared = false;
861 int allocfork = XFS_DATA_FORK;
864 /* we can't use delayed allocations when using extent size hints */
865 if (xfs_get_extsz_hint(ip))
866 return xfs_direct_write_iomap_begin(inode, offset, count,
867 flags, iomap, srcmap);
869 ASSERT(!XFS_IS_REALTIME_INODE(ip));
871 xfs_ilock(ip, XFS_ILOCK_EXCL);
873 if (unlikely(XFS_TEST_ERROR(
874 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
875 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
876 mp, XFS_ERRTAG_BMAPIFORMAT))) {
877 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
878 error = -EFSCORRUPTED;
882 XFS_STATS_INC(mp, xs_blk_mapw);
884 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
885 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
891 * Search the data fork fork first to look up our source mapping. We
892 * always need the data fork map, as we have to return it to the
893 * iomap code so that the higher level write code can read data in to
894 * perform read-modify-write cycles for unaligned writes.
896 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
898 imap.br_startoff = end_fsb; /* fake hole until the end */
900 /* We never need to allocate blocks for zeroing a hole. */
901 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
902 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
907 * Search the COW fork extent list even if we did not find a data fork
908 * extent. This serves two purposes: first this implements the
909 * speculative preallocation using cowextsize, so that we also unshare
910 * block adjacent to shared blocks instead of just the shared blocks
911 * themselves. Second the lookup in the extent list is generally faster
912 * than going out to the shared extent tree.
914 if (xfs_is_cow_inode(ip)) {
916 ASSERT(!xfs_is_reflink_inode(ip));
917 xfs_ifork_init_cow(ip);
919 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
921 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
922 trace_xfs_reflink_cow_found(ip, &cmap);
927 if (imap.br_startoff <= offset_fsb) {
929 * For reflink files we may need a delalloc reservation when
930 * overwriting shared extents. This includes zeroing of
931 * existing extents that contain data.
933 if (!xfs_is_cow_inode(ip) ||
934 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
935 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
940 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
942 /* Trim the mapping to the nearest shared extent boundary. */
943 error = xfs_inode_need_cow(ip, &imap, &shared);
947 /* Not shared? Just report the (potentially capped) extent. */
949 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
955 * Fork all the shared blocks from our write offset until the
958 allocfork = XFS_COW_FORK;
959 end_fsb = imap.br_startoff + imap.br_blockcount;
962 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
963 * pages to keep the chunks of work done where somewhat
964 * symmetric with the work writeback does. This is a completely
965 * arbitrary number pulled out of thin air.
967 * Note that the values needs to be less than 32-bits wide until
968 * the lower level functions are updated.
970 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
971 end_fsb = xfs_iomap_end_fsb(mp, offset, count);
973 if (xfs_is_always_cow_inode(ip))
974 allocfork = XFS_COW_FORK;
977 error = xfs_qm_dqattach_locked(ip, false);
982 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork, offset,
984 if (prealloc_blocks) {
986 xfs_off_t end_offset;
987 xfs_fileoff_t p_end_fsb;
989 end_offset = XFS_ALLOC_ALIGN(mp, offset + count - 1);
990 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
993 align = xfs_eof_alignment(ip);
995 p_end_fsb = roundup_64(p_end_fsb, align);
997 p_end_fsb = min(p_end_fsb,
998 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
999 ASSERT(p_end_fsb > offset_fsb);
1000 prealloc_blocks = p_end_fsb - end_fsb;
1005 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
1006 end_fsb - offset_fsb, prealloc_blocks,
1007 allocfork == XFS_DATA_FORK ? &imap : &cmap,
1008 allocfork == XFS_DATA_FORK ? &icur : &ccur,
1009 allocfork == XFS_DATA_FORK ? eof : cow_eof);
1015 /* retry without any preallocation */
1016 trace_xfs_delalloc_enospc(ip, offset, count);
1017 if (prealloc_blocks) {
1018 prealloc_blocks = 0;
1026 if (allocfork == XFS_COW_FORK) {
1027 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
1032 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
1033 * them out if the write happens to fail.
1035 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1036 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
1037 return xfs_bmbt_to_iomap(ip, iomap, &imap, IOMAP_F_NEW);
1040 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1041 return xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1044 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1045 if (imap.br_startoff <= offset_fsb) {
1046 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, 0);
1050 xfs_trim_extent(&cmap, offset_fsb,
1051 imap.br_startoff - offset_fsb);
1053 return xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
1056 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1061 xfs_buffered_write_iomap_end(
1062 struct inode *inode,
1067 struct iomap *iomap)
1069 struct xfs_inode *ip = XFS_I(inode);
1070 struct xfs_mount *mp = ip->i_mount;
1071 xfs_fileoff_t start_fsb;
1072 xfs_fileoff_t end_fsb;
1075 if (iomap->type != IOMAP_DELALLOC)
1079 * Behave as if the write failed if drop writes is enabled. Set the NEW
1080 * flag to force delalloc cleanup.
1082 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
1083 iomap->flags |= IOMAP_F_NEW;
1088 * start_fsb refers to the first unused block after a short write. If
1089 * nothing was written, round offset down to point at the first block in
1092 if (unlikely(!written))
1093 start_fsb = XFS_B_TO_FSBT(mp, offset);
1095 start_fsb = XFS_B_TO_FSB(mp, offset + written);
1096 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1099 * Trim delalloc blocks if they were allocated by this write and we
1100 * didn't manage to write the whole range.
1102 * We don't need to care about racing delalloc as we hold i_mutex
1103 * across the reserve/allocate/unreserve calls. If there are delalloc
1104 * blocks in the range, they are ours.
1106 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
1107 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1108 XFS_FSB_TO_B(mp, end_fsb) - 1);
1110 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1111 end_fsb - start_fsb);
1112 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1113 xfs_alert(mp, "%s: unable to clean up ino %lld",
1114 __func__, ip->i_ino);
1122 const struct iomap_ops xfs_buffered_write_iomap_ops = {
1123 .iomap_begin = xfs_buffered_write_iomap_begin,
1124 .iomap_end = xfs_buffered_write_iomap_end,
1128 xfs_read_iomap_begin(
1129 struct inode *inode,
1133 struct iomap *iomap,
1134 struct iomap *srcmap)
1136 struct xfs_inode *ip = XFS_I(inode);
1137 struct xfs_mount *mp = ip->i_mount;
1138 struct xfs_bmbt_irec imap;
1139 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1140 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1141 int nimaps = 1, error = 0;
1142 bool shared = false;
1145 ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
1147 if (XFS_FORCED_SHUTDOWN(mp))
1150 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1153 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1155 if (!error && (flags & IOMAP_REPORT))
1156 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
1157 xfs_iunlock(ip, lockmode);
1161 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1162 return xfs_bmbt_to_iomap(ip, iomap, &imap, shared ? IOMAP_F_SHARED : 0);
1165 const struct iomap_ops xfs_read_iomap_ops = {
1166 .iomap_begin = xfs_read_iomap_begin,
1170 xfs_seek_iomap_begin(
1171 struct inode *inode,
1175 struct iomap *iomap,
1176 struct iomap *srcmap)
1178 struct xfs_inode *ip = XFS_I(inode);
1179 struct xfs_mount *mp = ip->i_mount;
1180 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1181 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1182 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1183 struct xfs_iext_cursor icur;
1184 struct xfs_bmbt_irec imap, cmap;
1188 if (XFS_FORCED_SHUTDOWN(mp))
1191 lockmode = xfs_ilock_data_map_shared(ip);
1192 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
1193 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1198 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1200 * If we found a data extent we are done.
1202 if (imap.br_startoff <= offset_fsb)
1204 data_fsb = imap.br_startoff;
1207 * Fake a hole until the end of the file.
1209 data_fsb = xfs_iomap_end_fsb(mp, offset, length);
1213 * If a COW fork extent covers the hole, report it - capped to the next
1216 if (xfs_inode_has_cow_data(ip) &&
1217 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1218 cow_fsb = cmap.br_startoff;
1219 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1220 if (data_fsb < cow_fsb + cmap.br_blockcount)
1221 end_fsb = min(end_fsb, data_fsb);
1222 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1223 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
1225 * This is a COW extent, so we must probe the page cache
1226 * because there could be dirty page cache being backed
1229 iomap->type = IOMAP_UNWRITTEN;
1234 * Else report a hole, capped to the next found data or COW extent.
1236 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1237 imap.br_blockcount = cow_fsb - offset_fsb;
1239 imap.br_blockcount = data_fsb - offset_fsb;
1240 imap.br_startoff = offset_fsb;
1241 imap.br_startblock = HOLESTARTBLOCK;
1242 imap.br_state = XFS_EXT_NORM;
1244 xfs_trim_extent(&imap, offset_fsb, end_fsb);
1245 error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1247 xfs_iunlock(ip, lockmode);
1251 const struct iomap_ops xfs_seek_iomap_ops = {
1252 .iomap_begin = xfs_seek_iomap_begin,
1256 xfs_xattr_iomap_begin(
1257 struct inode *inode,
1261 struct iomap *iomap,
1262 struct iomap *srcmap)
1264 struct xfs_inode *ip = XFS_I(inode);
1265 struct xfs_mount *mp = ip->i_mount;
1266 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1267 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1268 struct xfs_bmbt_irec imap;
1269 int nimaps = 1, error = 0;
1272 if (XFS_FORCED_SHUTDOWN(mp))
1275 lockmode = xfs_ilock_attr_map_shared(ip);
1277 /* if there are no attribute fork or extents, return ENOENT */
1278 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
1283 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1284 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1285 &nimaps, XFS_BMAPI_ATTRFORK);
1287 xfs_iunlock(ip, lockmode);
1292 return xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1295 const struct iomap_ops xfs_xattr_iomap_ops = {
1296 .iomap_begin = xfs_xattr_iomap_begin,