1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2016-2018 Christoph Hellwig.
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_btree.h"
16 #include "xfs_bmap_btree.h"
18 #include "xfs_bmap_util.h"
19 #include "xfs_errortag.h"
20 #include "xfs_error.h"
21 #include "xfs_trans.h"
22 #include "xfs_trans_space.h"
23 #include "xfs_inode_item.h"
24 #include "xfs_iomap.h"
25 #include "xfs_trace.h"
26 #include "xfs_quota.h"
27 #include "xfs_dquot_item.h"
28 #include "xfs_dquot.h"
29 #include "xfs_reflink.h"
32 #define XFS_WRITEIO_ALIGN(mp,off) (((off) >> mp->m_writeio_log) \
36 xfs_alert_fsblock_zero(
38 xfs_bmbt_irec_t *imap)
40 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
41 "Access to block zero in inode %llu "
42 "start_block: %llx start_off: %llx "
43 "blkcnt: %llx extent-state: %x",
44 (unsigned long long)ip->i_ino,
45 (unsigned long long)imap->br_startblock,
46 (unsigned long long)imap->br_startoff,
47 (unsigned long long)imap->br_blockcount,
56 struct xfs_bmbt_irec *imap,
59 struct xfs_mount *mp = ip->i_mount;
61 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
62 return xfs_alert_fsblock_zero(ip, imap);
64 if (imap->br_startblock == HOLESTARTBLOCK) {
65 iomap->addr = IOMAP_NULL_ADDR;
66 iomap->type = IOMAP_HOLE;
67 } else if (imap->br_startblock == DELAYSTARTBLOCK ||
68 isnullstartblock(imap->br_startblock)) {
69 iomap->addr = IOMAP_NULL_ADDR;
70 iomap->type = IOMAP_DELALLOC;
72 iomap->addr = BBTOB(xfs_fsb_to_db(ip, imap->br_startblock));
73 if (imap->br_state == XFS_EXT_UNWRITTEN)
74 iomap->type = IOMAP_UNWRITTEN;
76 iomap->type = IOMAP_MAPPED;
78 iomap->offset = XFS_FSB_TO_B(mp, imap->br_startoff);
79 iomap->length = XFS_FSB_TO_B(mp, imap->br_blockcount);
80 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
81 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
84 if (xfs_ipincount(ip) &&
85 (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
86 iomap->flags |= IOMAP_F_DIRTY;
94 xfs_fileoff_t offset_fsb,
95 xfs_fileoff_t end_fsb)
97 iomap->addr = IOMAP_NULL_ADDR;
98 iomap->type = IOMAP_HOLE;
99 iomap->offset = XFS_FSB_TO_B(ip->i_mount, offset_fsb);
100 iomap->length = XFS_FSB_TO_B(ip->i_mount, end_fsb - offset_fsb);
101 iomap->bdev = xfs_find_bdev_for_inode(VFS_I(ip));
102 iomap->dax_dev = xfs_find_daxdev_for_inode(VFS_I(ip));
105 static inline xfs_fileoff_t
107 struct xfs_mount *mp,
111 ASSERT(offset <= mp->m_super->s_maxbytes);
112 return min(XFS_B_TO_FSB(mp, offset + count),
113 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
118 struct xfs_inode *ip,
119 xfs_extlen_t extsize)
121 struct xfs_mount *mp = ip->i_mount;
122 xfs_extlen_t align = 0;
124 if (!XFS_IS_REALTIME_INODE(ip)) {
126 * Round up the allocation request to a stripe unit
127 * (m_dalign) boundary if the file size is >= stripe unit
128 * size, and we are allocating past the allocation eof.
130 * If mounted with the "-o swalloc" option the alignment is
131 * increased from the strip unit size to the stripe width.
133 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
134 align = mp->m_swidth;
135 else if (mp->m_dalign)
136 align = mp->m_dalign;
138 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
143 * Always round up the allocation request to an extent boundary
144 * (when file on a real-time subvolume or has di_extsize hint).
148 align = roundup_64(align, extsize);
157 xfs_iomap_eof_align_last_fsb(
158 struct xfs_inode *ip,
159 xfs_extlen_t extsize,
160 xfs_fileoff_t *last_fsb)
162 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
165 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
168 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
172 *last_fsb = new_last_fsb;
178 xfs_iomap_write_direct(
182 xfs_bmbt_irec_t *imap,
185 xfs_mount_t *mp = ip->i_mount;
186 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
187 xfs_fileoff_t last_fsb = xfs_iomap_end_fsb(mp, offset, count);
188 xfs_filblks_t count_fsb, resaligned;
194 uint qblocks, resblks, resrtextents;
197 int bmapi_flags = XFS_BMAPI_PREALLOC;
200 rt = XFS_IS_REALTIME_INODE(ip);
201 extsz = xfs_get_extsz_hint(ip);
202 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
204 ASSERT(xfs_isilocked(ip, lockmode));
206 if ((offset + count) > XFS_ISIZE(ip)) {
208 * Assert that the in-core extent list is present since this can
209 * call xfs_iread_extents() and we only have the ilock shared.
210 * This should be safe because the lock was held around a bmapi
211 * call in the caller and we only need it to access the in-core
214 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
216 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
220 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
221 last_fsb = min(last_fsb, (xfs_fileoff_t)
222 imap->br_blockcount +
225 count_fsb = last_fsb - offset_fsb;
226 ASSERT(count_fsb > 0);
227 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
230 resrtextents = qblocks = resaligned;
231 resrtextents /= mp->m_sb.sb_rextsize;
232 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
233 quota_flag = XFS_QMOPT_RES_RTBLKS;
236 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
237 quota_flag = XFS_QMOPT_RES_REGBLKS;
241 * Drop the shared lock acquired by the caller, attach the dquot if
242 * necessary and move on to transaction setup.
244 xfs_iunlock(ip, lockmode);
245 error = xfs_qm_dqattach(ip);
250 * For DAX, we do not allocate unwritten extents, but instead we zero
251 * the block before we commit the transaction. Ideally we'd like to do
252 * this outside the transaction context, but if we commit and then crash
253 * we may not have zeroed the blocks and this will be exposed on
254 * recovery of the allocation. Hence we must zero before commit.
256 * Further, if we are mapping unwritten extents here, we need to zero
257 * and convert them to written so that we don't need an unwritten extent
258 * callback for DAX. This also means that we need to be able to dip into
259 * the reserve block pool for bmbt block allocation if there is no space
260 * left but we need to do unwritten extent conversion.
262 if (IS_DAX(VFS_I(ip))) {
263 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
264 if (imap->br_state == XFS_EXT_UNWRITTEN) {
265 tflags |= XFS_TRANS_RESERVE;
266 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
269 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
274 lockmode = XFS_ILOCK_EXCL;
275 xfs_ilock(ip, lockmode);
277 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
279 goto out_trans_cancel;
281 xfs_trans_ijoin(tp, ip, 0);
284 * From this point onwards we overwrite the imap pointer that the
288 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
289 bmapi_flags, resblks, imap, &nimaps);
294 * Complete the transaction
296 error = xfs_trans_commit(tp);
301 * Copy any maps to caller's array and return any error.
308 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
309 error = xfs_alert_fsblock_zero(ip, imap);
312 xfs_iunlock(ip, lockmode);
316 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
318 xfs_trans_cancel(tp);
323 xfs_quota_need_throttle(
324 struct xfs_inode *ip,
326 xfs_fsblock_t alloc_blocks)
328 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
330 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
333 /* no hi watermark, no throttle */
334 if (!dq->q_prealloc_hi_wmark)
337 /* under the lo watermark, no throttle */
338 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
345 xfs_quota_calc_throttle(
346 struct xfs_inode *ip,
348 xfs_fsblock_t *qblocks,
354 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
356 /* no dq, or over hi wmark, squash the prealloc completely */
357 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
363 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
364 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
366 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
368 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
372 if (freesp < *qfreesp)
375 /* only overwrite the throttle values if we are more aggressive */
376 if ((freesp >> shift) < (*qblocks >> *qshift)) {
383 * If we are doing a write at the end of the file and there are no allocations
384 * past this one, then extend the allocation out to the file system's write
387 * If we don't have a user specified preallocation size, dynamically increase
388 * the preallocation size as the size of the file grows. Cap the maximum size
389 * at a single extent or less if the filesystem is near full. The closer the
390 * filesystem is to full, the smaller the maximum prealocation.
392 * As an exception we don't do any preallocation at all if the file is smaller
393 * than the minimum preallocation and we are using the default dynamic
394 * preallocation scheme, as it is likely this is the only write to the file that
395 * is going to be done.
397 * We clean up any extra space left over when the file is closed in
401 xfs_iomap_prealloc_size(
402 struct xfs_inode *ip,
406 struct xfs_iext_cursor *icur)
408 struct xfs_mount *mp = ip->i_mount;
409 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
410 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
411 struct xfs_bmbt_irec prev;
414 xfs_fsblock_t qblocks;
416 xfs_fsblock_t alloc_blocks = 0;
418 if (offset + count <= XFS_ISIZE(ip))
421 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
422 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
426 * If an explicit allocsize is set, the file is small, or we
427 * are writing behind a hole, then use the minimum prealloc:
429 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
430 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
431 !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
432 prev.br_startoff + prev.br_blockcount < offset_fsb)
433 return mp->m_writeio_blocks;
436 * Determine the initial size of the preallocation. We are beyond the
437 * current EOF here, but we need to take into account whether this is
438 * a sparse write or an extending write when determining the
439 * preallocation size. Hence we need to look up the extent that ends
440 * at the current write offset and use the result to determine the
441 * preallocation size.
443 * If the extent is a hole, then preallocation is essentially disabled.
444 * Otherwise we take the size of the preceding data extent as the basis
445 * for the preallocation size. If the size of the extent is greater than
446 * half the maximum extent length, then use the current offset as the
447 * basis. This ensures that for large files the preallocation size
448 * always extends to MAXEXTLEN rather than falling short due to things
449 * like stripe unit/width alignment of real extents.
451 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
452 alloc_blocks = prev.br_blockcount << 1;
454 alloc_blocks = XFS_B_TO_FSB(mp, offset);
457 qblocks = alloc_blocks;
460 * MAXEXTLEN is not a power of two value but we round the prealloc down
461 * to the nearest power of two value after throttling. To prevent the
462 * round down from unconditionally reducing the maximum supported prealloc
463 * size, we round up first, apply appropriate throttling, round down and
464 * cap the value to MAXEXTLEN.
466 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
469 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
470 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
472 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
474 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
476 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
478 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
483 * Check each quota to cap the prealloc size, provide a shift value to
484 * throttle with and adjust amount of available space.
486 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
487 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
489 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
490 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
492 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
493 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
497 * The final prealloc size is set to the minimum of free space available
498 * in each of the quotas and the overall filesystem.
500 * The shift throttle value is set to the maximum value as determined by
501 * the global low free space values and per-quota low free space values.
503 alloc_blocks = min(alloc_blocks, qblocks);
504 shift = max(shift, qshift);
507 alloc_blocks >>= shift;
509 * rounddown_pow_of_two() returns an undefined result if we pass in
513 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
514 if (alloc_blocks > MAXEXTLEN)
515 alloc_blocks = MAXEXTLEN;
518 * If we are still trying to allocate more space than is
519 * available, squash the prealloc hard. This can happen if we
520 * have a large file on a small filesystem and the above
521 * lowspace thresholds are smaller than MAXEXTLEN.
523 while (alloc_blocks && alloc_blocks >= freesp)
526 if (alloc_blocks < mp->m_writeio_blocks)
527 alloc_blocks = mp->m_writeio_blocks;
528 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
529 mp->m_writeio_blocks);
534 xfs_iomap_write_unwritten(
540 xfs_mount_t *mp = ip->i_mount;
541 xfs_fileoff_t offset_fsb;
542 xfs_filblks_t count_fsb;
543 xfs_filblks_t numblks_fsb;
546 xfs_bmbt_irec_t imap;
547 struct inode *inode = VFS_I(ip);
552 trace_xfs_unwritten_convert(ip, offset, count);
554 offset_fsb = XFS_B_TO_FSBT(mp, offset);
555 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
556 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
559 * Reserve enough blocks in this transaction for two complete extent
560 * btree splits. We may be converting the middle part of an unwritten
561 * extent and in this case we will insert two new extents in the btree
562 * each of which could cause a full split.
564 * This reservation amount will be used in the first call to
565 * xfs_bmbt_split() to select an AG with enough space to satisfy the
566 * rest of the operation.
568 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
572 * Set up a transaction to convert the range of extents
573 * from unwritten to real. Do allocations in a loop until
574 * we have covered the range passed in.
576 * Note that we can't risk to recursing back into the filesystem
577 * here as we might be asked to write out the same inode that we
578 * complete here and might deadlock on the iolock.
580 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
581 XFS_TRANS_RESERVE, &tp);
585 xfs_ilock(ip, XFS_ILOCK_EXCL);
586 xfs_trans_ijoin(tp, ip, 0);
589 * Modify the unwritten extent state of the buffer.
592 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
593 XFS_BMAPI_CONVERT, resblks, &imap,
596 goto error_on_bmapi_transaction;
599 * Log the updated inode size as we go. We have to be careful
600 * to only log it up to the actual write offset if it is
601 * halfway into a block.
603 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
604 if (i_size > offset + count)
605 i_size = offset + count;
606 if (update_isize && i_size > i_size_read(inode))
607 i_size_write(inode, i_size);
608 i_size = xfs_new_eof(ip, i_size);
610 ip->i_d.di_size = i_size;
611 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
614 error = xfs_trans_commit(tp);
615 xfs_iunlock(ip, XFS_ILOCK_EXCL);
619 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock)))
620 return xfs_alert_fsblock_zero(ip, &imap);
622 if ((numblks_fsb = imap.br_blockcount) == 0) {
624 * The numblks_fsb value should always get
625 * smaller, otherwise the loop is stuck.
627 ASSERT(imap.br_blockcount);
630 offset_fsb += numblks_fsb;
631 count_fsb -= numblks_fsb;
632 } while (count_fsb > 0);
636 error_on_bmapi_transaction:
637 xfs_trans_cancel(tp);
638 xfs_iunlock(ip, XFS_ILOCK_EXCL);
646 struct xfs_bmbt_irec *imap,
649 /* don't allocate blocks when just zeroing */
650 if (flags & IOMAP_ZERO)
653 imap->br_startblock == HOLESTARTBLOCK ||
654 imap->br_startblock == DELAYSTARTBLOCK)
656 /* we convert unwritten extents before copying the data for DAX */
657 if (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN)
664 struct xfs_inode *ip,
666 struct xfs_bmbt_irec *imap,
669 if (!xfs_is_cow_inode(ip))
672 /* when zeroing we don't have to COW holes or unwritten extents */
673 if (flags & IOMAP_ZERO) {
675 imap->br_startblock == HOLESTARTBLOCK ||
676 imap->br_state == XFS_EXT_UNWRITTEN)
685 struct xfs_inode *ip,
689 unsigned mode = XFS_ILOCK_SHARED;
690 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
693 * COW writes may allocate delalloc space or convert unwritten COW
694 * extents, so we need to make sure to take the lock exclusively here.
696 if (xfs_is_cow_inode(ip) && is_write) {
698 * FIXME: It could still overwrite on unshared extents and not
701 if (flags & IOMAP_NOWAIT)
703 mode = XFS_ILOCK_EXCL;
707 * Extents not yet cached requires exclusive access, don't block. This
708 * is an opencoded xfs_ilock_data_map_shared() call but with
709 * non-blocking behaviour.
711 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
712 if (flags & IOMAP_NOWAIT)
714 mode = XFS_ILOCK_EXCL;
718 if (flags & IOMAP_NOWAIT) {
719 if (!xfs_ilock_nowait(ip, mode))
726 * The reflink iflag could have changed since the earlier unlocked
727 * check, so if we got ILOCK_SHARED for a write and but we're now a
728 * reflink inode we have to switch to ILOCK_EXCL and relock.
730 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
731 xfs_iunlock(ip, mode);
732 mode = XFS_ILOCK_EXCL;
741 xfs_direct_write_iomap_begin(
747 struct iomap *srcmap)
749 struct xfs_inode *ip = XFS_I(inode);
750 struct xfs_mount *mp = ip->i_mount;
751 struct xfs_bmbt_irec imap, cmap;
752 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
753 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
754 int nimaps = 1, error = 0;
759 ASSERT(flags & (IOMAP_WRITE | IOMAP_ZERO));
761 if (XFS_FORCED_SHUTDOWN(mp))
765 * Writes that span EOF might trigger an IO size update on completion,
766 * so consider them to be dirty for the purposes of O_DSYNC even if
767 * there is no other metadata changes pending or have been made here.
769 if (offset + length > i_size_read(inode))
770 iomap_flags |= IOMAP_F_DIRTY;
773 * Lock the inode in the manner required for the specified operation and
774 * check for as many conditions that would result in blocking as
775 * possible. This removes most of the non-blocking checks from the
776 * mapping code below.
778 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
782 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
788 * Break shared extents if necessary. Checks for non-blocking IO have
789 * been done up front, so we don't need to do them here.
791 if (imap_needs_cow(ip, flags, &imap, nimaps)) {
792 /* may drop and re-acquire the ilock */
793 error = xfs_reflink_allocate_cow(ip, &imap, &cmap, &shared,
794 &lockmode, flags & IOMAP_DIRECT);
799 end_fsb = imap.br_startoff + imap.br_blockcount;
800 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
803 if (imap_needs_alloc(inode, flags, &imap, nimaps))
804 goto allocate_blocks;
806 xfs_iunlock(ip, lockmode);
807 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
808 return xfs_bmbt_to_iomap(ip, iomap, &imap, iomap_flags);
812 if (flags & IOMAP_NOWAIT)
816 * We cap the maximum length we map to a sane size to keep the chunks
817 * of work done where somewhat symmetric with the work writeback does.
818 * This is a completely arbitrary number pulled out of thin air as a
819 * best guess for initial testing.
821 * Note that the values needs to be less than 32-bits wide until the
822 * lower level functions are updated.
824 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
827 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
830 if (lockmode == XFS_ILOCK_EXCL)
831 xfs_ilock_demote(ip, lockmode);
832 error = xfs_iomap_write_direct(ip, offset, length, &imap, nimaps);
836 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
837 return xfs_bmbt_to_iomap(ip, iomap, &imap, iomap_flags | IOMAP_F_NEW);
840 xfs_iunlock(ip, lockmode);
841 length = XFS_FSB_TO_B(mp, cmap.br_startoff + cmap.br_blockcount);
842 trace_xfs_iomap_found(ip, offset, length - offset, XFS_COW_FORK, &cmap);
843 if (imap.br_startblock != HOLESTARTBLOCK) {
844 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, 0);
848 return xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
851 xfs_iunlock(ip, lockmode);
855 const struct iomap_ops xfs_direct_write_iomap_ops = {
856 .iomap_begin = xfs_direct_write_iomap_begin,
860 xfs_buffered_write_iomap_begin(
866 struct iomap *srcmap)
868 struct xfs_inode *ip = XFS_I(inode);
869 struct xfs_mount *mp = ip->i_mount;
870 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
871 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, count);
872 struct xfs_bmbt_irec imap, cmap;
873 struct xfs_iext_cursor icur, ccur;
874 xfs_fsblock_t prealloc_blocks = 0;
875 bool eof = false, cow_eof = false, shared = false;
876 int allocfork = XFS_DATA_FORK;
879 /* we can't use delayed allocations when using extent size hints */
880 if (xfs_get_extsz_hint(ip))
881 return xfs_direct_write_iomap_begin(inode, offset, count,
882 flags, iomap, srcmap);
884 ASSERT(!XFS_IS_REALTIME_INODE(ip));
886 xfs_ilock(ip, XFS_ILOCK_EXCL);
888 if (unlikely(XFS_TEST_ERROR(
889 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
890 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
891 mp, XFS_ERRTAG_BMAPIFORMAT))) {
892 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
893 error = -EFSCORRUPTED;
897 XFS_STATS_INC(mp, xs_blk_mapw);
899 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
900 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
906 * Search the data fork fork first to look up our source mapping. We
907 * always need the data fork map, as we have to return it to the
908 * iomap code so that the higher level write code can read data in to
909 * perform read-modify-write cycles for unaligned writes.
911 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
913 imap.br_startoff = end_fsb; /* fake hole until the end */
915 /* We never need to allocate blocks for zeroing a hole. */
916 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
917 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
922 * Search the COW fork extent list even if we did not find a data fork
923 * extent. This serves two purposes: first this implements the
924 * speculative preallocation using cowextsize, so that we also unshare
925 * block adjacent to shared blocks instead of just the shared blocks
926 * themselves. Second the lookup in the extent list is generally faster
927 * than going out to the shared extent tree.
929 if (xfs_is_cow_inode(ip)) {
931 ASSERT(!xfs_is_reflink_inode(ip));
932 xfs_ifork_init_cow(ip);
934 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
936 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
937 trace_xfs_reflink_cow_found(ip, &cmap);
942 if (imap.br_startoff <= offset_fsb) {
944 * For reflink files we may need a delalloc reservation when
945 * overwriting shared extents. This includes zeroing of
946 * existing extents that contain data.
948 if (!xfs_is_cow_inode(ip) ||
949 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
950 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
955 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
957 /* Trim the mapping to the nearest shared extent boundary. */
958 error = xfs_inode_need_cow(ip, &imap, &shared);
962 /* Not shared? Just report the (potentially capped) extent. */
964 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
970 * Fork all the shared blocks from our write offset until the
973 allocfork = XFS_COW_FORK;
974 end_fsb = imap.br_startoff + imap.br_blockcount;
977 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
978 * pages to keep the chunks of work done where somewhat
979 * symmetric with the work writeback does. This is a completely
980 * arbitrary number pulled out of thin air.
982 * Note that the values needs to be less than 32-bits wide until
983 * the lower level functions are updated.
985 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
986 end_fsb = xfs_iomap_end_fsb(mp, offset, count);
988 if (xfs_is_always_cow_inode(ip))
989 allocfork = XFS_COW_FORK;
992 error = xfs_qm_dqattach_locked(ip, false);
997 prealloc_blocks = xfs_iomap_prealloc_size(ip, allocfork, offset,
999 if (prealloc_blocks) {
1001 xfs_off_t end_offset;
1002 xfs_fileoff_t p_end_fsb;
1004 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
1005 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
1008 align = xfs_eof_alignment(ip, 0);
1010 p_end_fsb = roundup_64(p_end_fsb, align);
1012 p_end_fsb = min(p_end_fsb,
1013 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1014 ASSERT(p_end_fsb > offset_fsb);
1015 prealloc_blocks = p_end_fsb - end_fsb;
1020 error = xfs_bmapi_reserve_delalloc(ip, allocfork, offset_fsb,
1021 end_fsb - offset_fsb, prealloc_blocks,
1022 allocfork == XFS_DATA_FORK ? &imap : &cmap,
1023 allocfork == XFS_DATA_FORK ? &icur : &ccur,
1024 allocfork == XFS_DATA_FORK ? eof : cow_eof);
1030 /* retry without any preallocation */
1031 trace_xfs_delalloc_enospc(ip, offset, count);
1032 if (prealloc_blocks) {
1033 prealloc_blocks = 0;
1041 if (allocfork == XFS_COW_FORK) {
1042 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &cmap);
1047 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
1048 * them out if the write happens to fail.
1050 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1051 trace_xfs_iomap_alloc(ip, offset, count, allocfork, &imap);
1052 return xfs_bmbt_to_iomap(ip, iomap, &imap, IOMAP_F_NEW);
1055 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1056 return xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1059 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1060 if (imap.br_startoff <= offset_fsb) {
1061 error = xfs_bmbt_to_iomap(ip, srcmap, &imap, 0);
1065 xfs_trim_extent(&cmap, offset_fsb,
1066 imap.br_startoff - offset_fsb);
1068 return xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
1071 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1076 xfs_buffered_write_iomap_end(
1077 struct inode *inode,
1082 struct iomap *iomap)
1084 struct xfs_inode *ip = XFS_I(inode);
1085 struct xfs_mount *mp = ip->i_mount;
1086 xfs_fileoff_t start_fsb;
1087 xfs_fileoff_t end_fsb;
1090 if (iomap->type != IOMAP_DELALLOC)
1094 * Behave as if the write failed if drop writes is enabled. Set the NEW
1095 * flag to force delalloc cleanup.
1097 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
1098 iomap->flags |= IOMAP_F_NEW;
1103 * start_fsb refers to the first unused block after a short write. If
1104 * nothing was written, round offset down to point at the first block in
1107 if (unlikely(!written))
1108 start_fsb = XFS_B_TO_FSBT(mp, offset);
1110 start_fsb = XFS_B_TO_FSB(mp, offset + written);
1111 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1114 * Trim delalloc blocks if they were allocated by this write and we
1115 * didn't manage to write the whole range.
1117 * We don't need to care about racing delalloc as we hold i_mutex
1118 * across the reserve/allocate/unreserve calls. If there are delalloc
1119 * blocks in the range, they are ours.
1121 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
1122 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1123 XFS_FSB_TO_B(mp, end_fsb) - 1);
1125 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1126 end_fsb - start_fsb);
1127 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1128 xfs_alert(mp, "%s: unable to clean up ino %lld",
1129 __func__, ip->i_ino);
1137 const struct iomap_ops xfs_buffered_write_iomap_ops = {
1138 .iomap_begin = xfs_buffered_write_iomap_begin,
1139 .iomap_end = xfs_buffered_write_iomap_end,
1143 xfs_read_iomap_begin(
1144 struct inode *inode,
1148 struct iomap *iomap,
1149 struct iomap *srcmap)
1151 struct xfs_inode *ip = XFS_I(inode);
1152 struct xfs_mount *mp = ip->i_mount;
1153 struct xfs_bmbt_irec imap;
1154 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1155 xfs_fileoff_t end_fsb = xfs_iomap_end_fsb(mp, offset, length);
1156 int nimaps = 1, error = 0;
1157 bool shared = false;
1160 ASSERT(!(flags & (IOMAP_WRITE | IOMAP_ZERO)));
1162 if (XFS_FORCED_SHUTDOWN(mp))
1165 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
1168 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1170 if (!error && (flags & IOMAP_REPORT))
1171 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
1172 xfs_iunlock(ip, lockmode);
1176 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1177 return xfs_bmbt_to_iomap(ip, iomap, &imap, shared ? IOMAP_F_SHARED : 0);
1180 const struct iomap_ops xfs_read_iomap_ops = {
1181 .iomap_begin = xfs_read_iomap_begin,
1185 xfs_seek_iomap_begin(
1186 struct inode *inode,
1190 struct iomap *iomap,
1191 struct iomap *srcmap)
1193 struct xfs_inode *ip = XFS_I(inode);
1194 struct xfs_mount *mp = ip->i_mount;
1195 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1196 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1197 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1198 struct xfs_iext_cursor icur;
1199 struct xfs_bmbt_irec imap, cmap;
1203 if (XFS_FORCED_SHUTDOWN(mp))
1206 lockmode = xfs_ilock_data_map_shared(ip);
1207 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
1208 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1213 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1215 * If we found a data extent we are done.
1217 if (imap.br_startoff <= offset_fsb)
1219 data_fsb = imap.br_startoff;
1222 * Fake a hole until the end of the file.
1224 data_fsb = xfs_iomap_end_fsb(mp, offset, length);
1228 * If a COW fork extent covers the hole, report it - capped to the next
1231 if (xfs_inode_has_cow_data(ip) &&
1232 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1233 cow_fsb = cmap.br_startoff;
1234 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1235 if (data_fsb < cow_fsb + cmap.br_blockcount)
1236 end_fsb = min(end_fsb, data_fsb);
1237 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1238 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
1240 * This is a COW extent, so we must probe the page cache
1241 * because there could be dirty page cache being backed
1244 iomap->type = IOMAP_UNWRITTEN;
1249 * Else report a hole, capped to the next found data or COW extent.
1251 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1252 imap.br_blockcount = cow_fsb - offset_fsb;
1254 imap.br_blockcount = data_fsb - offset_fsb;
1255 imap.br_startoff = offset_fsb;
1256 imap.br_startblock = HOLESTARTBLOCK;
1257 imap.br_state = XFS_EXT_NORM;
1259 xfs_trim_extent(&imap, offset_fsb, end_fsb);
1260 error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1262 xfs_iunlock(ip, lockmode);
1266 const struct iomap_ops xfs_seek_iomap_ops = {
1267 .iomap_begin = xfs_seek_iomap_begin,
1271 xfs_xattr_iomap_begin(
1272 struct inode *inode,
1276 struct iomap *iomap,
1277 struct iomap *srcmap)
1279 struct xfs_inode *ip = XFS_I(inode);
1280 struct xfs_mount *mp = ip->i_mount;
1281 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1282 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1283 struct xfs_bmbt_irec imap;
1284 int nimaps = 1, error = 0;
1287 if (XFS_FORCED_SHUTDOWN(mp))
1290 lockmode = xfs_ilock_attr_map_shared(ip);
1292 /* if there are no attribute fork or extents, return ENOENT */
1293 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
1298 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1299 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1300 &nimaps, XFS_BMAPI_ATTRFORK);
1302 xfs_iunlock(ip, lockmode);
1307 return xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1310 const struct iomap_ops xfs_xattr_iomap_ops = {
1311 .iomap_begin = xfs_xattr_iomap_begin,