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));
107 struct xfs_inode *ip,
108 xfs_extlen_t extsize)
110 struct xfs_mount *mp = ip->i_mount;
111 xfs_extlen_t align = 0;
113 if (!XFS_IS_REALTIME_INODE(ip)) {
115 * Round up the allocation request to a stripe unit
116 * (m_dalign) boundary if the file size is >= stripe unit
117 * size, and we are allocating past the allocation eof.
119 * If mounted with the "-o swalloc" option the alignment is
120 * increased from the strip unit size to the stripe width.
122 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
123 align = mp->m_swidth;
124 else if (mp->m_dalign)
125 align = mp->m_dalign;
127 if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
132 * Always round up the allocation request to an extent boundary
133 * (when file on a real-time subvolume or has di_extsize hint).
137 align = roundup_64(align, extsize);
146 xfs_iomap_eof_align_last_fsb(
147 struct xfs_inode *ip,
148 xfs_extlen_t extsize,
149 xfs_fileoff_t *last_fsb)
151 xfs_extlen_t align = xfs_eof_alignment(ip, extsize);
154 xfs_fileoff_t new_last_fsb = roundup_64(*last_fsb, align);
157 error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
161 *last_fsb = new_last_fsb;
167 xfs_iomap_write_direct(
171 xfs_bmbt_irec_t *imap,
174 xfs_mount_t *mp = ip->i_mount;
175 xfs_fileoff_t offset_fsb;
176 xfs_fileoff_t last_fsb;
177 xfs_filblks_t count_fsb, resaligned;
183 uint qblocks, resblks, resrtextents;
186 int bmapi_flags = XFS_BMAPI_PREALLOC;
189 rt = XFS_IS_REALTIME_INODE(ip);
190 extsz = xfs_get_extsz_hint(ip);
191 lockmode = XFS_ILOCK_SHARED; /* locked by caller */
193 ASSERT(xfs_isilocked(ip, lockmode));
195 offset_fsb = XFS_B_TO_FSBT(mp, offset);
196 last_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)(offset + count)));
197 if ((offset + count) > XFS_ISIZE(ip)) {
199 * Assert that the in-core extent list is present since this can
200 * call xfs_iread_extents() and we only have the ilock shared.
201 * This should be safe because the lock was held around a bmapi
202 * call in the caller and we only need it to access the in-core
205 ASSERT(XFS_IFORK_PTR(ip, XFS_DATA_FORK)->if_flags &
207 error = xfs_iomap_eof_align_last_fsb(ip, extsz, &last_fsb);
211 if (nmaps && (imap->br_startblock == HOLESTARTBLOCK))
212 last_fsb = min(last_fsb, (xfs_fileoff_t)
213 imap->br_blockcount +
216 count_fsb = last_fsb - offset_fsb;
217 ASSERT(count_fsb > 0);
218 resaligned = xfs_aligned_fsb_count(offset_fsb, count_fsb, extsz);
221 resrtextents = qblocks = resaligned;
222 resrtextents /= mp->m_sb.sb_rextsize;
223 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
224 quota_flag = XFS_QMOPT_RES_RTBLKS;
227 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resaligned);
228 quota_flag = XFS_QMOPT_RES_REGBLKS;
232 * Drop the shared lock acquired by the caller, attach the dquot if
233 * necessary and move on to transaction setup.
235 xfs_iunlock(ip, lockmode);
236 error = xfs_qm_dqattach(ip);
241 * For DAX, we do not allocate unwritten extents, but instead we zero
242 * the block before we commit the transaction. Ideally we'd like to do
243 * this outside the transaction context, but if we commit and then crash
244 * we may not have zeroed the blocks and this will be exposed on
245 * recovery of the allocation. Hence we must zero before commit.
247 * Further, if we are mapping unwritten extents here, we need to zero
248 * and convert them to written so that we don't need an unwritten extent
249 * callback for DAX. This also means that we need to be able to dip into
250 * the reserve block pool for bmbt block allocation if there is no space
251 * left but we need to do unwritten extent conversion.
253 if (IS_DAX(VFS_I(ip))) {
254 bmapi_flags = XFS_BMAPI_CONVERT | XFS_BMAPI_ZERO;
255 if (imap->br_state == XFS_EXT_UNWRITTEN) {
256 tflags |= XFS_TRANS_RESERVE;
257 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
260 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, resrtextents,
265 lockmode = XFS_ILOCK_EXCL;
266 xfs_ilock(ip, lockmode);
268 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
270 goto out_trans_cancel;
272 xfs_trans_ijoin(tp, ip, 0);
275 * From this point onwards we overwrite the imap pointer that the
279 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
280 bmapi_flags, resblks, imap, &nimaps);
285 * Complete the transaction
287 error = xfs_trans_commit(tp);
292 * Copy any maps to caller's array and return any error.
299 if (unlikely(!xfs_valid_startblock(ip, imap->br_startblock)))
300 error = xfs_alert_fsblock_zero(ip, imap);
303 xfs_iunlock(ip, lockmode);
307 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
309 xfs_trans_cancel(tp);
314 xfs_quota_need_throttle(
315 struct xfs_inode *ip,
317 xfs_fsblock_t alloc_blocks)
319 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
321 if (!dq || !xfs_this_quota_on(ip->i_mount, type))
324 /* no hi watermark, no throttle */
325 if (!dq->q_prealloc_hi_wmark)
328 /* under the lo watermark, no throttle */
329 if (dq->q_res_bcount + alloc_blocks < dq->q_prealloc_lo_wmark)
336 xfs_quota_calc_throttle(
337 struct xfs_inode *ip,
339 xfs_fsblock_t *qblocks,
345 struct xfs_dquot *dq = xfs_inode_dquot(ip, type);
347 /* no dq, or over hi wmark, squash the prealloc completely */
348 if (!dq || dq->q_res_bcount >= dq->q_prealloc_hi_wmark) {
354 freesp = dq->q_prealloc_hi_wmark - dq->q_res_bcount;
355 if (freesp < dq->q_low_space[XFS_QLOWSP_5_PCNT]) {
357 if (freesp < dq->q_low_space[XFS_QLOWSP_3_PCNT])
359 if (freesp < dq->q_low_space[XFS_QLOWSP_1_PCNT])
363 if (freesp < *qfreesp)
366 /* only overwrite the throttle values if we are more aggressive */
367 if ((freesp >> shift) < (*qblocks >> *qshift)) {
374 * If we are doing a write at the end of the file and there are no allocations
375 * past this one, then extend the allocation out to the file system's write
378 * If we don't have a user specified preallocation size, dynamically increase
379 * the preallocation size as the size of the file grows. Cap the maximum size
380 * at a single extent or less if the filesystem is near full. The closer the
381 * filesystem is to full, the smaller the maximum prealocation.
383 * As an exception we don't do any preallocation at all if the file is smaller
384 * than the minimum preallocation and we are using the default dynamic
385 * preallocation scheme, as it is likely this is the only write to the file that
386 * is going to be done.
388 * We clean up any extra space left over when the file is closed in
392 xfs_iomap_prealloc_size(
393 struct xfs_inode *ip,
397 struct xfs_iext_cursor *icur)
399 struct xfs_mount *mp = ip->i_mount;
400 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
401 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
402 struct xfs_bmbt_irec prev;
405 xfs_fsblock_t qblocks;
407 xfs_fsblock_t alloc_blocks = 0;
409 if (offset + count <= XFS_ISIZE(ip))
412 if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) &&
413 (XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_writeio_blocks)))
417 * If an explicit allocsize is set, the file is small, or we
418 * are writing behind a hole, then use the minimum prealloc:
420 if ((mp->m_flags & XFS_MOUNT_DFLT_IOSIZE) ||
421 XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, mp->m_dalign) ||
422 !xfs_iext_peek_prev_extent(ifp, icur, &prev) ||
423 prev.br_startoff + prev.br_blockcount < offset_fsb)
424 return mp->m_writeio_blocks;
427 * Determine the initial size of the preallocation. We are beyond the
428 * current EOF here, but we need to take into account whether this is
429 * a sparse write or an extending write when determining the
430 * preallocation size. Hence we need to look up the extent that ends
431 * at the current write offset and use the result to determine the
432 * preallocation size.
434 * If the extent is a hole, then preallocation is essentially disabled.
435 * Otherwise we take the size of the preceding data extent as the basis
436 * for the preallocation size. If the size of the extent is greater than
437 * half the maximum extent length, then use the current offset as the
438 * basis. This ensures that for large files the preallocation size
439 * always extends to MAXEXTLEN rather than falling short due to things
440 * like stripe unit/width alignment of real extents.
442 if (prev.br_blockcount <= (MAXEXTLEN >> 1))
443 alloc_blocks = prev.br_blockcount << 1;
445 alloc_blocks = XFS_B_TO_FSB(mp, offset);
448 qblocks = alloc_blocks;
451 * MAXEXTLEN is not a power of two value but we round the prealloc down
452 * to the nearest power of two value after throttling. To prevent the
453 * round down from unconditionally reducing the maximum supported prealloc
454 * size, we round up first, apply appropriate throttling, round down and
455 * cap the value to MAXEXTLEN.
457 alloc_blocks = XFS_FILEOFF_MIN(roundup_pow_of_two(MAXEXTLEN),
460 freesp = percpu_counter_read_positive(&mp->m_fdblocks);
461 if (freesp < mp->m_low_space[XFS_LOWSP_5_PCNT]) {
463 if (freesp < mp->m_low_space[XFS_LOWSP_4_PCNT])
465 if (freesp < mp->m_low_space[XFS_LOWSP_3_PCNT])
467 if (freesp < mp->m_low_space[XFS_LOWSP_2_PCNT])
469 if (freesp < mp->m_low_space[XFS_LOWSP_1_PCNT])
474 * Check each quota to cap the prealloc size, provide a shift value to
475 * throttle with and adjust amount of available space.
477 if (xfs_quota_need_throttle(ip, XFS_DQ_USER, alloc_blocks))
478 xfs_quota_calc_throttle(ip, XFS_DQ_USER, &qblocks, &qshift,
480 if (xfs_quota_need_throttle(ip, XFS_DQ_GROUP, alloc_blocks))
481 xfs_quota_calc_throttle(ip, XFS_DQ_GROUP, &qblocks, &qshift,
483 if (xfs_quota_need_throttle(ip, XFS_DQ_PROJ, alloc_blocks))
484 xfs_quota_calc_throttle(ip, XFS_DQ_PROJ, &qblocks, &qshift,
488 * The final prealloc size is set to the minimum of free space available
489 * in each of the quotas and the overall filesystem.
491 * The shift throttle value is set to the maximum value as determined by
492 * the global low free space values and per-quota low free space values.
494 alloc_blocks = min(alloc_blocks, qblocks);
495 shift = max(shift, qshift);
498 alloc_blocks >>= shift;
500 * rounddown_pow_of_two() returns an undefined result if we pass in
504 alloc_blocks = rounddown_pow_of_two(alloc_blocks);
505 if (alloc_blocks > MAXEXTLEN)
506 alloc_blocks = MAXEXTLEN;
509 * If we are still trying to allocate more space than is
510 * available, squash the prealloc hard. This can happen if we
511 * have a large file on a small filesystem and the above
512 * lowspace thresholds are smaller than MAXEXTLEN.
514 while (alloc_blocks && alloc_blocks >= freesp)
517 if (alloc_blocks < mp->m_writeio_blocks)
518 alloc_blocks = mp->m_writeio_blocks;
519 trace_xfs_iomap_prealloc_size(ip, alloc_blocks, shift,
520 mp->m_writeio_blocks);
525 xfs_file_iomap_begin_delay(
532 struct xfs_inode *ip = XFS_I(inode);
533 struct xfs_mount *mp = ip->i_mount;
534 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
535 xfs_fileoff_t maxbytes_fsb =
536 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
537 xfs_fileoff_t end_fsb;
538 struct xfs_bmbt_irec imap, cmap;
539 struct xfs_iext_cursor icur, ccur;
540 xfs_fsblock_t prealloc_blocks = 0;
541 bool eof = false, cow_eof = false, shared = false;
543 int whichfork = XFS_DATA_FORK;
546 ASSERT(!XFS_IS_REALTIME_INODE(ip));
547 ASSERT(!xfs_get_extsz_hint(ip));
549 xfs_ilock(ip, XFS_ILOCK_EXCL);
551 if (unlikely(XFS_TEST_ERROR(
552 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
553 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
554 mp, XFS_ERRTAG_BMAPIFORMAT))) {
555 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
556 error = -EFSCORRUPTED;
560 XFS_STATS_INC(mp, xs_blk_mapw);
562 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
563 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
568 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
571 * Search the data fork fork first to look up our source mapping. We
572 * always need the data fork map, as we have to return it to the
573 * iomap code so that the higher level write code can read data in to
574 * perform read-modify-write cycles for unaligned writes.
576 eof = !xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap);
578 imap.br_startoff = end_fsb; /* fake hole until the end */
580 /* We never need to allocate blocks for zeroing a hole. */
581 if ((flags & IOMAP_ZERO) && imap.br_startoff > offset_fsb) {
582 xfs_hole_to_iomap(ip, iomap, offset_fsb, imap.br_startoff);
587 * Search the COW fork extent list even if we did not find a data fork
588 * extent. This serves two purposes: first this implements the
589 * speculative preallocation using cowextsize, so that we also unshare
590 * block adjacent to shared blocks instead of just the shared blocks
591 * themselves. Second the lookup in the extent list is generally faster
592 * than going out to the shared extent tree.
594 if (xfs_is_cow_inode(ip)) {
596 ASSERT(!xfs_is_reflink_inode(ip));
597 xfs_ifork_init_cow(ip);
599 cow_eof = !xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb,
601 if (!cow_eof && cmap.br_startoff <= offset_fsb) {
602 trace_xfs_reflink_cow_found(ip, &cmap);
603 whichfork = XFS_COW_FORK;
608 if (imap.br_startoff <= offset_fsb) {
610 * For reflink files we may need a delalloc reservation when
611 * overwriting shared extents. This includes zeroing of
612 * existing extents that contain data.
614 if (!xfs_is_cow_inode(ip) ||
615 ((flags & IOMAP_ZERO) && imap.br_state != XFS_EXT_NORM)) {
616 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
621 xfs_trim_extent(&imap, offset_fsb, end_fsb - offset_fsb);
623 /* Trim the mapping to the nearest shared extent boundary. */
624 error = xfs_inode_need_cow(ip, &imap, &shared);
628 /* Not shared? Just report the (potentially capped) extent. */
630 trace_xfs_iomap_found(ip, offset, count, XFS_DATA_FORK,
636 * Fork all the shared blocks from our write offset until the
639 whichfork = XFS_COW_FORK;
640 end_fsb = imap.br_startoff + imap.br_blockcount;
643 * We cap the maximum length we map here to MAX_WRITEBACK_PAGES
644 * pages to keep the chunks of work done where somewhat
645 * symmetric with the work writeback does. This is a completely
646 * arbitrary number pulled out of thin air.
648 * Note that the values needs to be less than 32-bits wide until
649 * the lower level functions are updated.
651 count = min_t(loff_t, count, 1024 * PAGE_SIZE);
652 end_fsb = min(XFS_B_TO_FSB(mp, offset + count), maxbytes_fsb);
654 if (xfs_is_always_cow_inode(ip))
655 whichfork = XFS_COW_FORK;
658 error = xfs_qm_dqattach_locked(ip, false);
663 prealloc_blocks = xfs_iomap_prealloc_size(ip, whichfork, offset,
665 if (prealloc_blocks) {
667 xfs_off_t end_offset;
668 xfs_fileoff_t p_end_fsb;
670 end_offset = XFS_WRITEIO_ALIGN(mp, offset + count - 1);
671 p_end_fsb = XFS_B_TO_FSBT(mp, end_offset) +
674 align = xfs_eof_alignment(ip, 0);
676 p_end_fsb = roundup_64(p_end_fsb, align);
678 p_end_fsb = min(p_end_fsb, maxbytes_fsb);
679 ASSERT(p_end_fsb > offset_fsb);
680 prealloc_blocks = p_end_fsb - end_fsb;
685 error = xfs_bmapi_reserve_delalloc(ip, whichfork, offset_fsb,
686 end_fsb - offset_fsb, prealloc_blocks,
687 whichfork == XFS_DATA_FORK ? &imap : &cmap,
688 whichfork == XFS_DATA_FORK ? &icur : &ccur,
689 whichfork == XFS_DATA_FORK ? eof : cow_eof);
695 /* retry without any preallocation */
696 trace_xfs_delalloc_enospc(ip, offset, count);
697 if (prealloc_blocks) {
707 * Flag newly allocated delalloc blocks with IOMAP_F_NEW so we punch
708 * them out if the write happens to fail.
710 if (whichfork == XFS_DATA_FORK) {
711 iomap_flags |= IOMAP_F_NEW;
712 trace_xfs_iomap_alloc(ip, offset, count, whichfork, &imap);
714 trace_xfs_iomap_alloc(ip, offset, count, whichfork, &cmap);
717 if (whichfork == XFS_COW_FORK) {
718 if (imap.br_startoff > offset_fsb) {
719 xfs_trim_extent(&cmap, offset_fsb,
720 imap.br_startoff - offset_fsb);
721 error = xfs_bmbt_to_iomap(ip, iomap, &cmap,
725 /* ensure we only report blocks we have a reservation for */
726 xfs_trim_extent(&imap, cmap.br_startoff, cmap.br_blockcount);
730 iomap_flags |= IOMAP_F_SHARED;
731 error = xfs_bmbt_to_iomap(ip, iomap, &imap, iomap_flags);
733 xfs_iunlock(ip, XFS_ILOCK_EXCL);
738 xfs_iomap_write_unwritten(
744 xfs_mount_t *mp = ip->i_mount;
745 xfs_fileoff_t offset_fsb;
746 xfs_filblks_t count_fsb;
747 xfs_filblks_t numblks_fsb;
750 xfs_bmbt_irec_t imap;
751 struct inode *inode = VFS_I(ip);
756 trace_xfs_unwritten_convert(ip, offset, count);
758 offset_fsb = XFS_B_TO_FSBT(mp, offset);
759 count_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
760 count_fsb = (xfs_filblks_t)(count_fsb - offset_fsb);
763 * Reserve enough blocks in this transaction for two complete extent
764 * btree splits. We may be converting the middle part of an unwritten
765 * extent and in this case we will insert two new extents in the btree
766 * each of which could cause a full split.
768 * This reservation amount will be used in the first call to
769 * xfs_bmbt_split() to select an AG with enough space to satisfy the
770 * rest of the operation.
772 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0) << 1;
776 * Set up a transaction to convert the range of extents
777 * from unwritten to real. Do allocations in a loop until
778 * we have covered the range passed in.
780 * Note that we can't risk to recursing back into the filesystem
781 * here as we might be asked to write out the same inode that we
782 * complete here and might deadlock on the iolock.
784 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0,
785 XFS_TRANS_RESERVE, &tp);
789 xfs_ilock(ip, XFS_ILOCK_EXCL);
790 xfs_trans_ijoin(tp, ip, 0);
793 * Modify the unwritten extent state of the buffer.
796 error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
797 XFS_BMAPI_CONVERT, resblks, &imap,
800 goto error_on_bmapi_transaction;
803 * Log the updated inode size as we go. We have to be careful
804 * to only log it up to the actual write offset if it is
805 * halfway into a block.
807 i_size = XFS_FSB_TO_B(mp, offset_fsb + count_fsb);
808 if (i_size > offset + count)
809 i_size = offset + count;
810 if (update_isize && i_size > i_size_read(inode))
811 i_size_write(inode, i_size);
812 i_size = xfs_new_eof(ip, i_size);
814 ip->i_d.di_size = i_size;
815 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
818 error = xfs_trans_commit(tp);
819 xfs_iunlock(ip, XFS_ILOCK_EXCL);
823 if (unlikely(!xfs_valid_startblock(ip, imap.br_startblock)))
824 return xfs_alert_fsblock_zero(ip, &imap);
826 if ((numblks_fsb = imap.br_blockcount) == 0) {
828 * The numblks_fsb value should always get
829 * smaller, otherwise the loop is stuck.
831 ASSERT(imap.br_blockcount);
834 offset_fsb += numblks_fsb;
835 count_fsb -= numblks_fsb;
836 } while (count_fsb > 0);
840 error_on_bmapi_transaction:
841 xfs_trans_cancel(tp);
842 xfs_iunlock(ip, XFS_ILOCK_EXCL);
849 struct xfs_bmbt_irec *imap,
853 imap->br_startblock == HOLESTARTBLOCK ||
854 imap->br_startblock == DELAYSTARTBLOCK ||
855 (IS_DAX(inode) && imap->br_state == XFS_EXT_UNWRITTEN);
859 needs_cow_for_zeroing(
860 struct xfs_bmbt_irec *imap,
864 imap->br_startblock != HOLESTARTBLOCK &&
865 imap->br_state != XFS_EXT_UNWRITTEN;
870 struct xfs_inode *ip,
874 unsigned mode = XFS_ILOCK_SHARED;
875 bool is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
878 * COW writes may allocate delalloc space or convert unwritten COW
879 * extents, so we need to make sure to take the lock exclusively here.
881 if (xfs_is_cow_inode(ip) && is_write) {
883 * FIXME: It could still overwrite on unshared extents and not
886 if (flags & IOMAP_NOWAIT)
888 mode = XFS_ILOCK_EXCL;
892 * Extents not yet cached requires exclusive access, don't block. This
893 * is an opencoded xfs_ilock_data_map_shared() call but with
894 * non-blocking behaviour.
896 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
897 if (flags & IOMAP_NOWAIT)
899 mode = XFS_ILOCK_EXCL;
903 if (flags & IOMAP_NOWAIT) {
904 if (!xfs_ilock_nowait(ip, mode))
911 * The reflink iflag could have changed since the earlier unlocked
912 * check, so if we got ILOCK_SHARED for a write and but we're now a
913 * reflink inode we have to switch to ILOCK_EXCL and relock.
915 if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_cow_inode(ip)) {
916 xfs_iunlock(ip, mode);
917 mode = XFS_ILOCK_EXCL;
926 xfs_file_iomap_begin(
932 struct iomap *srcmap)
934 struct xfs_inode *ip = XFS_I(inode);
935 struct xfs_mount *mp = ip->i_mount;
936 struct xfs_bmbt_irec imap;
937 xfs_fileoff_t offset_fsb, end_fsb;
938 int nimaps = 1, error = 0;
943 if (XFS_FORCED_SHUTDOWN(mp))
946 if ((flags & (IOMAP_WRITE | IOMAP_ZERO)) && !(flags & IOMAP_DIRECT) &&
947 !IS_DAX(inode) && !xfs_get_extsz_hint(ip)) {
948 /* Reserve delalloc blocks for regular writeback. */
949 return xfs_file_iomap_begin_delay(inode, offset, length, flags,
954 * Lock the inode in the manner required for the specified operation and
955 * check for as many conditions that would result in blocking as
956 * possible. This removes most of the non-blocking checks from the
957 * mapping code below.
959 error = xfs_ilock_for_iomap(ip, flags, &lockmode);
963 ASSERT(offset <= mp->m_super->s_maxbytes);
964 if (offset > mp->m_super->s_maxbytes - length)
965 length = mp->m_super->s_maxbytes - offset;
966 offset_fsb = XFS_B_TO_FSBT(mp, offset);
967 end_fsb = XFS_B_TO_FSB(mp, offset + length);
969 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
974 if (flags & IOMAP_REPORT) {
975 /* Trim the mapping to the nearest shared extent boundary. */
976 error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
981 /* Non-modifying mapping requested, so we are done */
982 if (!(flags & (IOMAP_WRITE | IOMAP_ZERO)))
986 * Break shared extents if necessary. Checks for non-blocking IO have
987 * been done up front, so we don't need to do them here.
989 if (xfs_is_cow_inode(ip)) {
990 struct xfs_bmbt_irec cmap;
991 bool directio = (flags & IOMAP_DIRECT);
993 /* if zeroing doesn't need COW allocation, then we are done. */
994 if ((flags & IOMAP_ZERO) &&
995 !needs_cow_for_zeroing(&imap, nimaps))
998 /* may drop and re-acquire the ilock */
1000 error = xfs_reflink_allocate_cow(ip, &cmap, &shared, &lockmode,
1006 * For buffered writes we need to report the address of the
1007 * previous block (if there was any) so that the higher level
1008 * write code can perform read-modify-write operations; we
1009 * won't need the CoW fork mapping until writeback. For direct
1010 * I/O, which must be block aligned, we need to report the
1011 * newly allocated address. If the data fork has a hole, copy
1012 * the COW fork mapping to avoid allocating to the data fork.
1014 if (directio || imap.br_startblock == HOLESTARTBLOCK)
1017 end_fsb = imap.br_startoff + imap.br_blockcount;
1018 length = XFS_FSB_TO_B(mp, end_fsb) - offset;
1021 /* Don't need to allocate over holes when doing zeroing operations. */
1022 if (flags & IOMAP_ZERO)
1025 if (!imap_needs_alloc(inode, &imap, nimaps))
1028 /* If nowait is set bail since we are going to make allocations. */
1029 if (flags & IOMAP_NOWAIT) {
1035 * We cap the maximum length we map to a sane size to keep the chunks
1036 * of work done where somewhat symmetric with the work writeback does.
1037 * This is a completely arbitrary number pulled out of thin air as a
1038 * best guess for initial testing.
1040 * Note that the values needs to be less than 32-bits wide until the
1041 * lower level functions are updated.
1043 length = min_t(loff_t, length, 1024 * PAGE_SIZE);
1046 * xfs_iomap_write_direct() expects the shared lock. It is unlocked on
1049 if (lockmode == XFS_ILOCK_EXCL)
1050 xfs_ilock_demote(ip, lockmode);
1051 error = xfs_iomap_write_direct(ip, offset, length, &imap,
1056 iomap_flags |= IOMAP_F_NEW;
1057 trace_xfs_iomap_alloc(ip, offset, length, XFS_DATA_FORK, &imap);
1061 * Writes that span EOF might trigger an IO size update on completion,
1062 * so consider them to be dirty for the purposes of O_DSYNC even if
1063 * there is no other metadata changes pending or have been made here.
1065 if ((flags & IOMAP_WRITE) && offset + length > i_size_read(inode))
1066 iomap_flags |= IOMAP_F_DIRTY;
1068 iomap_flags |= IOMAP_F_SHARED;
1069 return xfs_bmbt_to_iomap(ip, iomap, &imap, iomap_flags);
1073 xfs_iunlock(ip, lockmode);
1074 trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
1078 xfs_iunlock(ip, lockmode);
1083 xfs_file_iomap_end_delalloc(
1084 struct xfs_inode *ip,
1088 struct iomap *iomap)
1090 struct xfs_mount *mp = ip->i_mount;
1091 xfs_fileoff_t start_fsb;
1092 xfs_fileoff_t end_fsb;
1096 * Behave as if the write failed if drop writes is enabled. Set the NEW
1097 * flag to force delalloc cleanup.
1099 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DROP_WRITES)) {
1100 iomap->flags |= IOMAP_F_NEW;
1105 * start_fsb refers to the first unused block after a short write. If
1106 * nothing was written, round offset down to point at the first block in
1109 if (unlikely(!written))
1110 start_fsb = XFS_B_TO_FSBT(mp, offset);
1112 start_fsb = XFS_B_TO_FSB(mp, offset + written);
1113 end_fsb = XFS_B_TO_FSB(mp, offset + length);
1116 * Trim delalloc blocks if they were allocated by this write and we
1117 * didn't manage to write the whole range.
1119 * We don't need to care about racing delalloc as we hold i_mutex
1120 * across the reserve/allocate/unreserve calls. If there are delalloc
1121 * blocks in the range, they are ours.
1123 if ((iomap->flags & IOMAP_F_NEW) && start_fsb < end_fsb) {
1124 truncate_pagecache_range(VFS_I(ip), XFS_FSB_TO_B(mp, start_fsb),
1125 XFS_FSB_TO_B(mp, end_fsb) - 1);
1127 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1128 end_fsb - start_fsb);
1129 if (error && !XFS_FORCED_SHUTDOWN(mp)) {
1130 xfs_alert(mp, "%s: unable to clean up ino %lld",
1131 __func__, ip->i_ino);
1141 struct inode *inode,
1146 struct iomap *iomap)
1148 if ((flags & IOMAP_WRITE) && iomap->type == IOMAP_DELALLOC)
1149 return xfs_file_iomap_end_delalloc(XFS_I(inode), offset,
1150 length, written, iomap);
1154 const struct iomap_ops xfs_iomap_ops = {
1155 .iomap_begin = xfs_file_iomap_begin,
1156 .iomap_end = xfs_file_iomap_end,
1160 xfs_seek_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 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1171 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1172 xfs_fileoff_t cow_fsb = NULLFILEOFF, data_fsb = NULLFILEOFF;
1173 struct xfs_iext_cursor icur;
1174 struct xfs_bmbt_irec imap, cmap;
1178 if (XFS_FORCED_SHUTDOWN(mp))
1181 lockmode = xfs_ilock_data_map_shared(ip);
1182 if (!(ip->i_df.if_flags & XFS_IFEXTENTS)) {
1183 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
1188 if (xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) {
1190 * If we found a data extent we are done.
1192 if (imap.br_startoff <= offset_fsb)
1194 data_fsb = imap.br_startoff;
1197 * Fake a hole until the end of the file.
1199 data_fsb = min(XFS_B_TO_FSB(mp, offset + length),
1200 XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes));
1204 * If a COW fork extent covers the hole, report it - capped to the next
1207 if (xfs_inode_has_cow_data(ip) &&
1208 xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &cmap))
1209 cow_fsb = cmap.br_startoff;
1210 if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
1211 if (data_fsb < cow_fsb + cmap.br_blockcount)
1212 end_fsb = min(end_fsb, data_fsb);
1213 xfs_trim_extent(&cmap, offset_fsb, end_fsb);
1214 error = xfs_bmbt_to_iomap(ip, iomap, &cmap, IOMAP_F_SHARED);
1216 * This is a COW extent, so we must probe the page cache
1217 * because there could be dirty page cache being backed
1220 iomap->type = IOMAP_UNWRITTEN;
1225 * Else report a hole, capped to the next found data or COW extent.
1227 if (cow_fsb != NULLFILEOFF && cow_fsb < data_fsb)
1228 imap.br_blockcount = cow_fsb - offset_fsb;
1230 imap.br_blockcount = data_fsb - offset_fsb;
1231 imap.br_startoff = offset_fsb;
1232 imap.br_startblock = HOLESTARTBLOCK;
1233 imap.br_state = XFS_EXT_NORM;
1235 xfs_trim_extent(&imap, offset_fsb, end_fsb);
1236 error = xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1238 xfs_iunlock(ip, lockmode);
1242 const struct iomap_ops xfs_seek_iomap_ops = {
1243 .iomap_begin = xfs_seek_iomap_begin,
1247 xfs_xattr_iomap_begin(
1248 struct inode *inode,
1252 struct iomap *iomap,
1253 struct iomap *srcmap)
1255 struct xfs_inode *ip = XFS_I(inode);
1256 struct xfs_mount *mp = ip->i_mount;
1257 xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
1258 xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + length);
1259 struct xfs_bmbt_irec imap;
1260 int nimaps = 1, error = 0;
1263 if (XFS_FORCED_SHUTDOWN(mp))
1266 lockmode = xfs_ilock_attr_map_shared(ip);
1268 /* if there are no attribute fork or extents, return ENOENT */
1269 if (!XFS_IFORK_Q(ip) || !ip->i_d.di_anextents) {
1274 ASSERT(ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL);
1275 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
1276 &nimaps, XFS_BMAPI_ATTRFORK);
1278 xfs_iunlock(ip, lockmode);
1283 return xfs_bmbt_to_iomap(ip, iomap, &imap, 0);
1286 const struct iomap_ops xfs_xattr_iomap_ops = {
1287 .iomap_begin = xfs_xattr_iomap_begin,